/[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.86 by wakaba, Thu Mar 6 15:56:52 2008 UTC revision 1.131 by wakaba, Sun Apr 13 05:54:28 2008 UTC
# Line 12  use Error qw(:try); Line 12  use Error qw(:try);
12  ## TODO: 1252 parse error (revision 1264)  ## TODO: 1252 parse error (revision 1264)
13  ## TODO: 8859-11 = 874 (revision 1271)  ## TODO: 8859-11 = 874 (revision 1271)
14    
15  my $permitted_slash_tag_name = {  my $HTML_NS = q<http://www.w3.org/1999/xhtml>;
16    base => 1,  my $MML_NS = q<http://www.w3.org/1998/Math/MathML>;
17    link => 1,  my $SVG_NS = q<http://www.w3.org/2000/svg>;
18    meta => 1,  my $XLINK_NS = q<http://www.w3.org/1999/xlink>;
19    hr => 1,  my $XML_NS = q<http://www.w3.org/XML/1998/namespace>;
20    br => 1,  my $XMLNS_NS = q<http://www.w3.org/2000/xmlns/>;
21    img => 1,  
22    embed => 1,  sub A_EL () { 0b1 }
23    param => 1,  sub ADDRESS_EL () { 0b10 }
24    area => 1,  sub BODY_EL () { 0b100 }
25    col => 1,  sub BUTTON_EL () { 0b1000 }
26    input => 1,  sub CAPTION_EL () { 0b10000 }
27    sub DD_EL () { 0b100000 }
28    sub DIV_EL () { 0b1000000 }
29    sub DT_EL () { 0b10000000 }
30    sub FORM_EL () { 0b100000000 }
31    sub FORMATTING_EL () { 0b1000000000 }
32    sub FRAMESET_EL () { 0b10000000000 }
33    sub HEADING_EL () { 0b100000000000 }
34    sub HTML_EL () { 0b1000000000000 }
35    sub LI_EL () { 0b10000000000000 }
36    sub NOBR_EL () { 0b100000000000000 }
37    sub OPTION_EL () { 0b1000000000000000 }
38    sub OPTGROUP_EL () { 0b10000000000000000 }
39    sub P_EL () { 0b100000000000000000 }
40    sub SELECT_EL () { 0b1000000000000000000 }
41    sub TABLE_EL () { 0b10000000000000000000 }
42    sub TABLE_CELL_EL () { 0b100000000000000000000 }
43    sub TABLE_ROW_EL () { 0b1000000000000000000000 }
44    sub TABLE_ROW_GROUP_EL () { 0b10000000000000000000000 }
45    sub MISC_SCOPING_EL () { 0b100000000000000000000000 }
46    sub MISC_SPECIAL_EL () { 0b1000000000000000000000000 }
47    sub FOREIGN_EL () { 0b10000000000000000000000000 }
48    sub FOREIGN_FLOW_CONTENT_EL () { 0b100000000000000000000000000 }
49    sub MML_AXML_EL () { 0b1000000000000000000000000000 }
50    
51    sub TABLE_ROWS_EL () {
52      TABLE_EL |
53      TABLE_ROW_EL |
54      TABLE_ROW_GROUP_EL
55    }
56    
57    sub END_TAG_OPTIONAL_EL () {
58      DD_EL |
59      DT_EL |
60      LI_EL |
61      P_EL
62    }
63    
64    sub ALL_END_TAG_OPTIONAL_EL () {
65      END_TAG_OPTIONAL_EL |
66      BODY_EL |
67      HTML_EL |
68      TABLE_CELL_EL |
69      TABLE_ROW_EL |
70      TABLE_ROW_GROUP_EL
71    }
72    
73    sub SCOPING_EL () {
74      BUTTON_EL |
75      CAPTION_EL |
76      HTML_EL |
77      TABLE_EL |
78      TABLE_CELL_EL |
79      MISC_SCOPING_EL
80    }
81    
82    sub TABLE_SCOPING_EL () {
83      HTML_EL |
84      TABLE_EL
85    }
86    
87    sub TABLE_ROWS_SCOPING_EL () {
88      HTML_EL |
89      TABLE_ROW_GROUP_EL
90    }
91    
92    sub TABLE_ROW_SCOPING_EL () {
93      HTML_EL |
94      TABLE_ROW_EL
95    }
96    
97    sub SPECIAL_EL () {
98      ADDRESS_EL |
99      BODY_EL |
100      DIV_EL |
101      END_TAG_OPTIONAL_EL |
102      FORM_EL |
103      FRAMESET_EL |
104      HEADING_EL |
105      OPTION_EL |
106      OPTGROUP_EL |
107      SELECT_EL |
108      TABLE_ROW_EL |
109      TABLE_ROW_GROUP_EL |
110      MISC_SPECIAL_EL
111    }
112    
113    my $el_category = {
114      a => A_EL | FORMATTING_EL,
115      address => ADDRESS_EL,
116      applet => MISC_SCOPING_EL,
117      area => MISC_SPECIAL_EL,
118      b => FORMATTING_EL,
119      base => MISC_SPECIAL_EL,
120      basefont => MISC_SPECIAL_EL,
121      bgsound => MISC_SPECIAL_EL,
122      big => FORMATTING_EL,
123      blockquote => MISC_SPECIAL_EL,
124      body => BODY_EL,
125      br => MISC_SPECIAL_EL,
126      button => BUTTON_EL,
127      caption => CAPTION_EL,
128      center => MISC_SPECIAL_EL,
129      col => MISC_SPECIAL_EL,
130      colgroup => MISC_SPECIAL_EL,
131      dd => DD_EL,
132      dir => MISC_SPECIAL_EL,
133      div => DIV_EL,
134      dl => MISC_SPECIAL_EL,
135      dt => DT_EL,
136      em => FORMATTING_EL,
137      embed => MISC_SPECIAL_EL,
138      fieldset => MISC_SPECIAL_EL,
139      font => FORMATTING_EL,
140      form => FORM_EL,
141      frame => MISC_SPECIAL_EL,
142      frameset => FRAMESET_EL,
143      h1 => HEADING_EL,
144      h2 => HEADING_EL,
145      h3 => HEADING_EL,
146      h4 => HEADING_EL,
147      h5 => HEADING_EL,
148      h6 => HEADING_EL,
149      head => MISC_SPECIAL_EL,
150      hr => MISC_SPECIAL_EL,
151      html => HTML_EL,
152      i => FORMATTING_EL,
153      iframe => MISC_SPECIAL_EL,
154      img => MISC_SPECIAL_EL,
155      input => MISC_SPECIAL_EL,
156      isindex => MISC_SPECIAL_EL,
157      li => LI_EL,
158      link => MISC_SPECIAL_EL,
159      listing => MISC_SPECIAL_EL,
160      marquee => MISC_SCOPING_EL,
161      menu => MISC_SPECIAL_EL,
162      meta => MISC_SPECIAL_EL,
163      nobr => NOBR_EL | FORMATTING_EL,
164      noembed => MISC_SPECIAL_EL,
165      noframes => MISC_SPECIAL_EL,
166      noscript => MISC_SPECIAL_EL,
167      object => MISC_SCOPING_EL,
168      ol => MISC_SPECIAL_EL,
169      optgroup => OPTGROUP_EL,
170      option => OPTION_EL,
171      p => P_EL,
172      param => MISC_SPECIAL_EL,
173      plaintext => MISC_SPECIAL_EL,
174      pre => MISC_SPECIAL_EL,
175      s => FORMATTING_EL,
176      script => MISC_SPECIAL_EL,
177      select => SELECT_EL,
178      small => FORMATTING_EL,
179      spacer => MISC_SPECIAL_EL,
180      strike => FORMATTING_EL,
181      strong => FORMATTING_EL,
182      style => MISC_SPECIAL_EL,
183      table => TABLE_EL,
184      tbody => TABLE_ROW_GROUP_EL,
185      td => TABLE_CELL_EL,
186      textarea => MISC_SPECIAL_EL,
187      tfoot => TABLE_ROW_GROUP_EL,
188      th => TABLE_CELL_EL,
189      thead => TABLE_ROW_GROUP_EL,
190      title => MISC_SPECIAL_EL,
191      tr => TABLE_ROW_EL,
192      tt => FORMATTING_EL,
193      u => FORMATTING_EL,
194      ul => MISC_SPECIAL_EL,
195      wbr => MISC_SPECIAL_EL,
196  };  };
197    
198    my $el_category_f = {
199      $MML_NS => {
200        'annotation-xml' => MML_AXML_EL,
201        mi => FOREIGN_FLOW_CONTENT_EL,
202        mo => FOREIGN_FLOW_CONTENT_EL,
203        mn => FOREIGN_FLOW_CONTENT_EL,
204        ms => FOREIGN_FLOW_CONTENT_EL,
205        mtext => FOREIGN_FLOW_CONTENT_EL,
206      },
207      $SVG_NS => {
208        foreignObject => FOREIGN_FLOW_CONTENT_EL,
209        desc => FOREIGN_FLOW_CONTENT_EL,
210        title => FOREIGN_FLOW_CONTENT_EL,
211      },
212      ## NOTE: In addition, FOREIGN_EL is set to non-HTML elements.
213    };
214    
215    my $svg_attr_name = {
216      attributetype => 'attributeType',
217      basefrequency => 'baseFrequency',
218      baseprofile => 'baseProfile',
219      calcmode => 'calcMode',
220      clippathunits => 'clipPathUnits',
221      contentscripttype => 'contentScriptType',
222      contentstyletype => 'contentStyleType',
223      diffuseconstant => 'diffuseConstant',
224      edgemode => 'edgeMode',
225      externalresourcesrequired => 'externalResourcesRequired',
226      fecolormatrix => 'feColorMatrix',
227      fecomposite => 'feComposite',
228      fegaussianblur => 'feGaussianBlur',
229      femorphology => 'feMorphology',
230      fetile => 'feTile',
231      filterres => 'filterRes',
232      filterunits => 'filterUnits',
233      glyphref => 'glyphRef',
234      gradienttransform => 'gradientTransform',
235      gradientunits => 'gradientUnits',
236      kernelmatrix => 'kernelMatrix',
237      kernelunitlength => 'kernelUnitLength',
238      keypoints => 'keyPoints',
239      keysplines => 'keySplines',
240      keytimes => 'keyTimes',
241      lengthadjust => 'lengthAdjust',
242      limitingconeangle => 'limitingConeAngle',
243      markerheight => 'markerHeight',
244      markerunits => 'markerUnits',
245      markerwidth => 'markerWidth',
246      maskcontentunits => 'maskContentUnits',
247      maskunits => 'maskUnits',
248      numoctaves => 'numOctaves',
249      pathlength => 'pathLength',
250      patterncontentunits => 'patternContentUnits',
251      patterntransform => 'patternTransform',
252      patternunits => 'patternUnits',
253      pointsatx => 'pointsAtX',
254      pointsaty => 'pointsAtY',
255      pointsatz => 'pointsAtZ',
256      preservealpha => 'preserveAlpha',
257      preserveaspectratio => 'preserveAspectRatio',
258      primitiveunits => 'primitiveUnits',
259      refx => 'refX',
260      refy => 'refY',
261      repeatcount => 'repeatCount',
262      repeatdur => 'repeatDur',
263      requiredextensions => 'requiredExtensions',
264      specularconstant => 'specularConstant',
265      specularexponent => 'specularExponent',
266      spreadmethod => 'spreadMethod',
267      startoffset => 'startOffset',
268      stddeviation => 'stdDeviation',
269      stitchtiles => 'stitchTiles',
270      surfacescale => 'surfaceScale',
271      systemlanguage => 'systemLanguage',
272      tablevalues => 'tableValues',
273      targetx => 'targetX',
274      targety => 'targetY',
275      textlength => 'textLength',
276      viewbox => 'viewBox',
277      viewtarget => 'viewTarget',
278      xchannelselector => 'xChannelSelector',
279      ychannelselector => 'yChannelSelector',
280      zoomandpan => 'zoomAndPan',
281    };
282    
283    my $foreign_attr_xname = {
284      'xlink:actuate' => [$XLINK_NS, ['xlink', 'actuate']],
285      'xlink:arcrole' => [$XLINK_NS, ['xlink', 'arcrole']],
286      'xlink:href' => [$XLINK_NS, ['xlink', 'href']],
287      'xlink:role' => [$XLINK_NS, ['xlink', 'role']],
288      'xlink:show' => [$XLINK_NS, ['xlink', 'show']],
289      'xlink:title' => [$XLINK_NS, ['xlink', 'title']],
290      'xlink:type' => [$XLINK_NS, ['xlink', 'type']],
291      'xml:base' => [$XML_NS, ['xml', 'base']],
292      'xml:lang' => [$XML_NS, ['xml', 'lang']],
293      'xml:space' => [$XML_NS, ['xml', 'space']],
294      'xmlns' => [$XMLNS_NS, [undef, 'xmlns']],
295      'xmlns:xlink' => [$XMLNS_NS, ['xmlns', 'xlink']],
296    };
297    
298    ## ISSUE: xmlns:xlink="non-xlink-ns" is not an error.
299    
300  my $c1_entity_char = {  my $c1_entity_char = {
301    0x80 => 0x20AC,    0x80 => 0x20AC,
302    0x81 => 0xFFFD,    0x81 => 0xFFFD,
# Line 61  my $c1_entity_char = { Line 332  my $c1_entity_char = {
332    0x9F => 0x0178,    0x9F => 0x0178,
333  }; # $c1_entity_char  }; # $c1_entity_char
334    
 my $special_category = {  
   address => 1, area => 1, base => 1, basefont => 1, bgsound => 1,  
   blockquote => 1, body => 1, br => 1, center => 1, col => 1, colgroup => 1,  
   dd => 1, dir => 1, div => 1, dl => 1, dt => 1, embed => 1, fieldset => 1,  
   form => 1, frame => 1, frameset => 1, h1 => 1, h2 => 1, h3 => 1,  
   h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, iframe => 1, image => 1,  
   img => 1, input => 1, isindex => 1, li => 1, link => 1, listing => 1,  
   menu => 1, meta => 1, noembed => 1, noframes => 1, noscript => 1,  
   ol => 1, optgroup => 1, option => 1, p => 1, param => 1, plaintext => 1,  
   pre => 1, script => 1, select => 1, spacer => 1, style => 1, tbody => 1,  
   textarea => 1, tfoot => 1, thead => 1, title => 1, tr => 1, ul => 1, wbr => 1,  
 };  
 my $scoping_category = {  
   button => 1, caption => 1, html => 1, marquee => 1, object => 1,  
   table => 1, td => 1, th => 1,  
 };  
 my $formatting_category = {  
   a => 1, b => 1, big => 1, em => 1, font => 1, i => 1, nobr => 1,  
   s => 1, small => 1, strile => 1, strong => 1, tt => 1, u => 1,  
 };  
 # $phrasing_category: all other elements  
   
335  sub parse_byte_string ($$$$;$) {  sub parse_byte_string ($$$$;$) {
336    my $self = ref $_[0] ? shift : shift->new;    my $self = ref $_[0] ? shift : shift->new;
337    my $charset = shift;    my $charset = shift;
# Line 108  sub parse_byte_string ($$$$;$) { Line 357  sub parse_byte_string ($$$$;$) {
357    $self->{change_encoding} = sub {    $self->{change_encoding} = sub {
358      my $self = shift;      my $self = shift;
359      my $charset = lc shift;      my $charset = lc shift;
360        my $token = shift;
361      ## TODO: if $charset is supported      ## TODO: if $charset is supported
362      ## TODO: normalize charset name      ## TODO: normalize charset name
363    
# Line 126  sub parse_byte_string ($$$$;$) { Line 376  sub parse_byte_string ($$$$;$) {
376      }      }
377    
378      !!!parse-error (type => 'charset label detected:'.$self->{input_encoding}.      !!!parse-error (type => 'charset label detected:'.$self->{input_encoding}.
379          ':'.$charset, level => 'w');          ':'.$charset, level => 'w', token => $token);
380    
381      ## Step 3      ## Step 3
382      # if (can) {      # if (can) {
# Line 177  sub parse_string ($$$;$) { Line 427  sub parse_string ($$$;$) {
427        if defined $self->{input_encoding};        if defined $self->{input_encoding};
428    
429    my $i = 0;    my $i = 0;
430    my $line = 1;    $self->{line_prev} = $self->{line} = 1;
431    my $column = 0;    $self->{column_prev} = $self->{column} = 0;
432    $self->{set_next_char} = sub {    $self->{set_next_char} = sub {
433      my $self = shift;      my $self = shift;
434    
# Line 187  sub parse_string ($$$;$) { Line 437  sub parse_string ($$$;$) {
437    
438      $self->{next_char} = -1 and return if $i >= length $$s;      $self->{next_char} = -1 and return if $i >= length $$s;
439      $self->{next_char} = ord substr $$s, $i++, 1;      $self->{next_char} = ord substr $$s, $i++, 1;
440      $column++;  
441        ($self->{line_prev}, $self->{column_prev})
442            = ($self->{line}, $self->{column});
443        $self->{column}++;
444            
445      if ($self->{next_char} == 0x000A) { # LF      if ($self->{next_char} == 0x000A) { # LF
446        $line++;        $self->{line}++;
447        $column = 0;        $self->{column} = 0;
448      } elsif ($self->{next_char} == 0x000D) { # CR      } elsif ($self->{next_char} == 0x000D) { # CR
449        $i++ if substr ($$s, $i, 1) eq "\x0A";        $i++ if substr ($$s, $i, 1) eq "\x0A";
450        $self->{next_char} = 0x000A; # LF # MUST        $self->{next_char} = 0x000A; # LF # MUST
451        $line++;        $self->{line}++;
452        $column = 0;        $self->{column} = 0;
453      } elsif ($self->{next_char} > 0x10FFFF) {      } elsif ($self->{next_char} > 0x10FFFF) {
454        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
455      } elsif ($self->{next_char} == 0x0000) { # NULL      } elsif ($self->{next_char} == 0x0000) { # NULL
# Line 209  sub parse_string ($$$;$) { Line 462  sub parse_string ($$$;$) {
462    
463    my $onerror = $_[2] || sub {    my $onerror = $_[2] || sub {
464      my (%opt) = @_;      my (%opt) = @_;
465      warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";      my $line = $opt{token} ? $opt{token}->{line} : $opt{line};
466        my $column = $opt{token} ? $opt{token}->{column} : $opt{column};
467        warn "Parse error ($opt{type}) at line $line column $column\n";
468    };    };
469    $self->{parse_error} = sub {    $self->{parse_error} = sub {
470      $onerror->(@_, line => $line, column => $column);      $onerror->(line => $self->{line}, column => $self->{column}, @_);
471    };    };
472    
473    $self->_initialize_tokenizer;    $self->_initialize_tokenizer;
# Line 220  sub parse_string ($$$;$) { Line 475  sub parse_string ($$$;$) {
475    $self->_construct_tree;    $self->_construct_tree;
476    $self->_terminate_tree_constructor;    $self->_terminate_tree_constructor;
477    
478      delete $self->{parse_error}; # remove loop
479    
480    return $self->{document};    return $self->{document};
481  } # parse_string  } # parse_string
482    
# Line 287  sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUO Line 544  sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUO
544  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }
545  sub BOGUS_DOCTYPE_STATE () { 32 }  sub BOGUS_DOCTYPE_STATE () { 32 }
546  sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }  sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }
547    sub SELF_CLOSING_START_TAG_STATE () { 34 }
548    sub CDATA_BLOCK_STATE () { 35 }
549    
550  sub DOCTYPE_TOKEN () { 1 }  sub DOCTYPE_TOKEN () { 1 }
551  sub COMMENT_TOKEN () { 2 }  sub COMMENT_TOKEN () { 2 }
# Line 303  sub TABLE_IMS ()      { 0b1000000 } Line 562  sub TABLE_IMS ()      { 0b1000000 }
562  sub ROW_IMS ()        { 0b10000000 }  sub ROW_IMS ()        { 0b10000000 }
563  sub BODY_AFTER_IMS () { 0b100000000 }  sub BODY_AFTER_IMS () { 0b100000000 }
564  sub FRAME_IMS ()      { 0b1000000000 }  sub FRAME_IMS ()      { 0b1000000000 }
565    sub SELECT_IMS ()     { 0b10000000000 }
566    sub IN_FOREIGN_CONTENT_IM () { 0b100000000000 }
567        ## NOTE: "in foreign content" insertion mode is special; it is combined
568        ## with the secondary insertion mode.  In this parser, they are stored
569        ## together in the bit-or'ed form.
570    
571  ## NOTE: "initial" and "before html" insertion modes have no constants.  ## NOTE: "initial" and "before html" insertion modes have no constants.
572    
# Line 325  sub IN_TABLE_IM () { TABLE_IMS } Line 589  sub IN_TABLE_IM () { TABLE_IMS }
589  sub AFTER_BODY_IM () { BODY_AFTER_IMS }  sub AFTER_BODY_IM () { BODY_AFTER_IMS }
590  sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }  sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }
591  sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }  sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }
592  sub IN_SELECT_IM () { 0b01 }  sub IN_SELECT_IM () { SELECT_IMS | 0b01 }
593    sub IN_SELECT_IN_TABLE_IM () { SELECT_IMS | 0b10 }
594  sub IN_COLUMN_GROUP_IM () { 0b10 }  sub IN_COLUMN_GROUP_IM () { 0b10 }
595    
596  ## Implementations MUST act as if state machine in the spec  ## Implementations MUST act as if state machine in the spec
# Line 338  sub _initialize_tokenizer ($) { Line 603  sub _initialize_tokenizer ($) {
603    undef $self->{current_attribute};    undef $self->{current_attribute};
604    undef $self->{last_emitted_start_tag_name};    undef $self->{last_emitted_start_tag_name};
605    undef $self->{last_attribute_value_state};    undef $self->{last_attribute_value_state};
606      delete $self->{self_closing};
607    $self->{char} = [];    $self->{char} = [];
608    # $self->{next_char}    # $self->{next_char}
609    !!!next-input-character;    !!!next-input-character;
# Line 358  sub _initialize_tokenizer ($) { Line 624  sub _initialize_tokenizer ($) {
624  ##        ->{value}  ##        ->{value}
625  ##        ->{has_reference} == 1 or 0  ##        ->{has_reference} == 1 or 0
626  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
627    ## NOTE: The "self-closing flag" is hold as |$self->{self_closing}|.
628    ##     |->{self_closing}| is used to save the value of |$self->{self_closing}|
629    ##     while the token is pushed back to the stack.
630    
631    ## ISSUE: "When a DOCTYPE token is created, its
632    ## <i>self-closing flag</i> must be unset (its other state is that it
633    ## be set), and its attributes list must be empty.": Wrong subject?
634    
635  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
636    
# Line 384  sub _initialize_tokenizer ($) { Line 657  sub _initialize_tokenizer ($) {
657    
658  sub _get_next_token ($) {  sub _get_next_token ($) {
659    my $self = shift;    my $self = shift;
660    
661      if ($self->{self_closing}) {
662        !!!parse-error (type => 'nestc', token => $self->{current_token});
663        ## NOTE: The |self_closing| flag is only set by start tag token.
664        ## In addition, when a start tag token is emitted, it is always set to
665        ## |current_token|.
666        delete $self->{self_closing};
667      }
668    
669    if (@{$self->{token}}) {    if (@{$self->{token}}) {
670        $self->{self_closing} = $self->{token}->[0]->{self_closing};
671      return shift @{$self->{token}};      return shift @{$self->{token}};
672    }    }
673    
# Line 447  sub _get_next_token ($) { Line 730  sub _get_next_token ($) {
730          #          #
731        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
732          !!!cp (11);          !!!cp (11);
733          !!!emit ({type => END_OF_FILE_TOKEN});          !!!emit ({type => END_OF_FILE_TOKEN,
734                      line => $self->{line}, column => $self->{column}});
735          last A; ## TODO: ok?          last A; ## TODO: ok?
736        } else {        } else {
737          !!!cp (12);          !!!cp (12);
738        }        }
739        # Anything else        # Anything else
740        my $token = {type => CHARACTER_TOKEN,        my $token = {type => CHARACTER_TOKEN,
741                     data => chr $self->{next_char}};                     data => chr $self->{next_char},
742                       line => $self->{line}, column => $self->{column},
743                      };
744        ## Stay in the data state        ## Stay in the data state
745        !!!next-input-character;        !!!next-input-character;
746    
# Line 463  sub _get_next_token ($) { Line 749  sub _get_next_token ($) {
749        redo A;        redo A;
750      } elsif ($self->{state} == ENTITY_DATA_STATE) {      } elsif ($self->{state} == ENTITY_DATA_STATE) {
751        ## (cannot happen in CDATA state)        ## (cannot happen in CDATA state)
752    
753          my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
754                
755        my $token = $self->_tokenize_attempt_to_consume_an_entity (0, -1);        my $token = $self->_tokenize_attempt_to_consume_an_entity (0, -1);
756    
# Line 471  sub _get_next_token ($) { Line 759  sub _get_next_token ($) {
759    
760        unless (defined $token) {        unless (defined $token) {
761          !!!cp (13);          !!!cp (13);
762          !!!emit ({type => CHARACTER_TOKEN, data => '&'});          !!!emit ({type => CHARACTER_TOKEN, data => '&',
763                      line => $l, column => $c,
764                     });
765        } else {        } else {
766          !!!cp (14);          !!!cp (14);
767          !!!emit ($token);          !!!emit ($token);
# Line 490  sub _get_next_token ($) { Line 780  sub _get_next_token ($) {
780            ## reconsume            ## reconsume
781            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
782    
783            !!!emit ({type => CHARACTER_TOKEN, data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
784                        line => $self->{line_prev},
785                        column => $self->{column_prev},
786                       });
787    
788            redo A;            redo A;
789          }          }
# Line 510  sub _get_next_token ($) { Line 803  sub _get_next_token ($) {
803            !!!cp (19);            !!!cp (19);
804            $self->{current_token}            $self->{current_token}
805              = {type => START_TAG_TOKEN,              = {type => START_TAG_TOKEN,
806                 tag_name => chr ($self->{next_char} + 0x0020)};                 tag_name => chr ($self->{next_char} + 0x0020),
807                   line => $self->{line_prev},
808                   column => $self->{column_prev}};
809            $self->{state} = TAG_NAME_STATE;            $self->{state} = TAG_NAME_STATE;
810            !!!next-input-character;            !!!next-input-character;
811            redo A;            redo A;
# Line 518  sub _get_next_token ($) { Line 813  sub _get_next_token ($) {
813                   $self->{next_char} <= 0x007A) { # a..z                   $self->{next_char} <= 0x007A) { # a..z
814            !!!cp (20);            !!!cp (20);
815            $self->{current_token} = {type => START_TAG_TOKEN,            $self->{current_token} = {type => START_TAG_TOKEN,
816                              tag_name => chr ($self->{next_char})};                                      tag_name => chr ($self->{next_char}),
817                                        line => $self->{line_prev},
818                                        column => $self->{column_prev}};
819            $self->{state} = TAG_NAME_STATE;            $self->{state} = TAG_NAME_STATE;
820            !!!next-input-character;            !!!next-input-character;
821            redo A;            redo A;
822          } elsif ($self->{next_char} == 0x003E) { # >          } elsif ($self->{next_char} == 0x003E) { # >
823            !!!cp (21);            !!!cp (21);
824            !!!parse-error (type => 'empty start tag');            !!!parse-error (type => 'empty start tag',
825                              line => $self->{line_prev},
826                              column => $self->{column_prev});
827            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
828            !!!next-input-character;            !!!next-input-character;
829    
830            !!!emit ({type => CHARACTER_TOKEN, data => '<>'});            !!!emit ({type => CHARACTER_TOKEN, data => '<>',
831                        line => $self->{line_prev},
832                        column => $self->{column_prev},
833                       });
834    
835            redo A;            redo A;
836          } elsif ($self->{next_char} == 0x003F) { # ?          } elsif ($self->{next_char} == 0x003F) { # ?
837            !!!cp (22);            !!!cp (22);
838            !!!parse-error (type => 'pio');            !!!parse-error (type => 'pio',
839                              line => $self->{line_prev},
840                              column => $self->{column_prev});
841            $self->{state} = BOGUS_COMMENT_STATE;            $self->{state} = BOGUS_COMMENT_STATE;
842              $self->{current_token} = {type => COMMENT_TOKEN, data => '',
843                                        line => $self->{line_prev},
844                                        column => $self->{column_prev},
845                                       };
846            ## $self->{next_char} is intentionally left as is            ## $self->{next_char} is intentionally left as is
847            redo A;            redo A;
848          } else {          } else {
# Line 543  sub _get_next_token ($) { Line 851  sub _get_next_token ($) {
851            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
852            ## reconsume            ## reconsume
853    
854            !!!emit ({type => CHARACTER_TOKEN, data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
855                        line => $self->{line_prev},
856                        column => $self->{column_prev},
857                       });
858    
859            redo A;            redo A;
860          }          }
# Line 551  sub _get_next_token ($) { Line 862  sub _get_next_token ($) {
862          die "$0: $self->{content_model} in tag open";          die "$0: $self->{content_model} in tag open";
863        }        }
864      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
865          my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1); # "<"of"</"
866        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
867          if (defined $self->{last_emitted_start_tag_name}) {          if (defined $self->{last_emitted_start_tag_name}) {
868    
869            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>
870            my @next_char;            my @next_char;
871            TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {            TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {
# Line 569  sub _get_next_token ($) { Line 882  sub _get_next_token ($) {
882                !!!back-next-input-character (@next_char);                !!!back-next-input-character (@next_char);
883                $self->{state} = DATA_STATE;                $self->{state} = DATA_STATE;
884    
885                !!!emit ({type => CHARACTER_TOKEN, data => '</'});                !!!emit ({type => CHARACTER_TOKEN, data => '</',
886                            line => $l, column => $c,
887                           });
888        
889                redo A;                redo A;
890              }              }
# Line 588  sub _get_next_token ($) { Line 903  sub _get_next_token ($) {
903              $self->{next_char} = shift @next_char; # reconsume              $self->{next_char} = shift @next_char; # reconsume
904              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
905              $self->{state} = DATA_STATE;              $self->{state} = DATA_STATE;
906              !!!emit ({type => CHARACTER_TOKEN, data => '</'});              !!!emit ({type => CHARACTER_TOKEN, data => '</',
907                          line => $l, column => $c,
908                         });
909              redo A;              redo A;
910            } else {            } else {
911              !!!cp (27);              !!!cp (27);
# Line 601  sub _get_next_token ($) { Line 918  sub _get_next_token ($) {
918            !!!cp (28);            !!!cp (28);
919            # next-input-character is already done            # next-input-character is already done
920            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
921            !!!emit ({type => CHARACTER_TOKEN, data => '</'});            !!!emit ({type => CHARACTER_TOKEN, data => '</',
922                        line => $l, column => $c,
923                       });
924            redo A;            redo A;
925          }          }
926        }        }
# Line 609  sub _get_next_token ($) { Line 928  sub _get_next_token ($) {
928        if (0x0041 <= $self->{next_char} and        if (0x0041 <= $self->{next_char} and
929            $self->{next_char} <= 0x005A) { # A..Z            $self->{next_char} <= 0x005A) { # A..Z
930          !!!cp (29);          !!!cp (29);
931          $self->{current_token} = {type => END_TAG_TOKEN,          $self->{current_token}
932                            tag_name => chr ($self->{next_char} + 0x0020)};              = {type => END_TAG_TOKEN,
933                   tag_name => chr ($self->{next_char} + 0x0020),
934                   line => $l, column => $c};
935          $self->{state} = TAG_NAME_STATE;          $self->{state} = TAG_NAME_STATE;
936          !!!next-input-character;          !!!next-input-character;
937          redo A;          redo A;
# Line 618  sub _get_next_token ($) { Line 939  sub _get_next_token ($) {
939                 $self->{next_char} <= 0x007A) { # a..z                 $self->{next_char} <= 0x007A) { # a..z
940          !!!cp (30);          !!!cp (30);
941          $self->{current_token} = {type => END_TAG_TOKEN,          $self->{current_token} = {type => END_TAG_TOKEN,
942                            tag_name => chr ($self->{next_char})};                                    tag_name => chr ($self->{next_char}),
943                                      line => $l, column => $c};
944          $self->{state} = TAG_NAME_STATE;          $self->{state} = TAG_NAME_STATE;
945          !!!next-input-character;          !!!next-input-character;
946          redo A;          redo A;
947        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
948          !!!cp (31);          !!!cp (31);
949          !!!parse-error (type => 'empty end tag');          !!!parse-error (type => 'empty end tag',
950                            line => $self->{line_prev}, ## "<" in "</>"
951                            column => $self->{column_prev} - 1);
952          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
953          !!!next-input-character;          !!!next-input-character;
954          redo A;          redo A;
# Line 634  sub _get_next_token ($) { Line 958  sub _get_next_token ($) {
958          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
959          # reconsume          # reconsume
960    
961          !!!emit ({type => CHARACTER_TOKEN, data => '</'});          !!!emit ({type => CHARACTER_TOKEN, data => '</',
962                      line => $l, column => $c,
963                     });
964    
965          redo A;          redo A;
966        } else {        } else {
967          !!!cp (33);          !!!cp (33);
968          !!!parse-error (type => 'bogus end tag');          !!!parse-error (type => 'bogus end tag');
969          $self->{state} = BOGUS_COMMENT_STATE;          $self->{state} = BOGUS_COMMENT_STATE;
970            $self->{current_token} = {type => COMMENT_TOKEN, data => '',
971                                      line => $self->{line_prev}, # "<" of "</"
972                                      column => $self->{column_prev} - 1,
973                                     };
974          ## $self->{next_char} is intentionally left as is          ## $self->{next_char} is intentionally left as is
975          redo A;          redo A;
976        }        }
# Line 657  sub _get_next_token ($) { Line 987  sub _get_next_token ($) {
987        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
988          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
989            !!!cp (35);            !!!cp (35);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
990            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
991          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
992            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 690  sub _get_next_token ($) { Line 1018  sub _get_next_token ($) {
1018          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1019          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1020            !!!cp (39);            !!!cp (39);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1021            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1022          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1023            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 712  sub _get_next_token ($) { Line 1038  sub _get_next_token ($) {
1038    
1039          redo A;          redo A;
1040        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1041            !!!cp (42);
1042            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1043          !!!next-input-character;          !!!next-input-character;
         if ($self->{next_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  
1044          redo A;          redo A;
1045        } else {        } else {
1046          !!!cp (44);          !!!cp (44);
# Line 747  sub _get_next_token ($) { Line 1063  sub _get_next_token ($) {
1063        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1064          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1065            !!!cp (46);            !!!cp (46);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1066            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1067          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1068            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 770  sub _get_next_token ($) { Line 1084  sub _get_next_token ($) {
1084        } elsif (0x0041 <= $self->{next_char} and        } elsif (0x0041 <= $self->{next_char} and
1085                 $self->{next_char} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1086          !!!cp (49);          !!!cp (49);
1087          $self->{current_attribute} = {name => chr ($self->{next_char} + 0x0020),          $self->{current_attribute}
1088                                value => ''};              = {name => chr ($self->{next_char} + 0x0020),
1089                   value => '',
1090                   line => $self->{line}, column => $self->{column}};
1091          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1092          !!!next-input-character;          !!!next-input-character;
1093          redo A;          redo A;
1094        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1095            !!!cp (50);
1096            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1097          !!!next-input-character;          !!!next-input-character;
         if ($self->{next_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  
1098          redo A;          redo A;
1099        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1100          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1101          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1102            !!!cp (52);            !!!cp (52);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1103            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1104          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1105            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 825  sub _get_next_token ($) { Line 1129  sub _get_next_token ($) {
1129          } else {          } else {
1130            !!!cp (56);            !!!cp (56);
1131          }          }
1132          $self->{current_attribute} = {name => chr ($self->{next_char}),          $self->{current_attribute}
1133                                value => ''};              = {name => chr ($self->{next_char}),
1134                   value => '',
1135                   line => $self->{line}, column => $self->{column}};
1136          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1137          !!!next-input-character;          !!!next-input-character;
1138          redo A;          redo A;
# Line 836  sub _get_next_token ($) { Line 1142  sub _get_next_token ($) {
1142          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
1143              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
1144            !!!cp (57);            !!!cp (57);
1145            !!!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});
1146            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
1147          } else {          } else {
1148            !!!cp (58);            !!!cp (58);
# Line 865  sub _get_next_token ($) { Line 1171  sub _get_next_token ($) {
1171          $before_leave->();          $before_leave->();
1172          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1173            !!!cp (61);            !!!cp (61);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1174            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1175          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1176            !!!cp (62);            !!!cp (62);
# Line 891  sub _get_next_token ($) { Line 1195  sub _get_next_token ($) {
1195          !!!next-input-character;          !!!next-input-character;
1196          redo A;          redo A;
1197        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1198            !!!cp (64);
1199          $before_leave->();          $before_leave->();
1200            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1201          !!!next-input-character;          !!!next-input-character;
         if ($self->{next_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  
1202          redo A;          redo A;
1203        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1204          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1205          $before_leave->();          $before_leave->();
1206          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1207            !!!cp (66);            !!!cp (66);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1208            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1209          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1210            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 963  sub _get_next_token ($) { Line 1255  sub _get_next_token ($) {
1255        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1256          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1257            !!!cp (73);            !!!cp (73);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1258            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1259          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1260            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 987  sub _get_next_token ($) { Line 1277  sub _get_next_token ($) {
1277        } elsif (0x0041 <= $self->{next_char} and        } elsif (0x0041 <= $self->{next_char} and
1278                 $self->{next_char} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1279          !!!cp (76);          !!!cp (76);
1280          $self->{current_attribute} = {name => chr ($self->{next_char} + 0x0020),          $self->{current_attribute}
1281                                value => ''};              = {name => chr ($self->{next_char} + 0x0020),
1282                   value => '',
1283                   line => $self->{line}, column => $self->{column}};
1284          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1285          !!!next-input-character;          !!!next-input-character;
1286          redo A;          redo A;
1287        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1288            !!!cp (77);
1289            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1290          !!!next-input-character;          !!!next-input-character;
         if ($self->{next_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  
1291          redo A;          redo A;
1292        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1293          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1294          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1295            !!!cp (79);            !!!cp (79);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1296            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1297          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1298            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1035  sub _get_next_token ($) { Line 1314  sub _get_next_token ($) {
1314          redo A;          redo A;
1315        } else {        } else {
1316          !!!cp (82);          !!!cp (82);
1317          $self->{current_attribute} = {name => chr ($self->{next_char}),          $self->{current_attribute}
1318                                value => ''};              = {name => chr ($self->{next_char}),
1319                   value => '',
1320                   line => $self->{line}, column => $self->{column}};
1321          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1322          !!!next-input-character;          !!!next-input-character;
1323          redo A;                  redo A;        
# Line 1069  sub _get_next_token ($) { Line 1350  sub _get_next_token ($) {
1350        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1351          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1352            !!!cp (87);            !!!cp (87);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1353            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1354          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1355            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1094  sub _get_next_token ($) { Line 1373  sub _get_next_token ($) {
1373          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1374          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1375            !!!cp (90);            !!!cp (90);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1376            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1377          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1378            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1143  sub _get_next_token ($) { Line 1420  sub _get_next_token ($) {
1420          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1421          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1422            !!!cp (97);            !!!cp (97);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1423            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1424          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1425            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1187  sub _get_next_token ($) { Line 1462  sub _get_next_token ($) {
1462          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1463          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1464            !!!cp (103);            !!!cp (103);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1465            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1466          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1467            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1234  sub _get_next_token ($) { Line 1507  sub _get_next_token ($) {
1507        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1508          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1509            !!!cp (109);            !!!cp (109);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1510            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1511          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1512            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1259  sub _get_next_token ($) { Line 1530  sub _get_next_token ($) {
1530          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1531          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1532            !!!cp (112);            !!!cp (112);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1533            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1534          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1535            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1331  sub _get_next_token ($) { Line 1600  sub _get_next_token ($) {
1600        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1601          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1602            !!!cp (119);            !!!cp (119);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1603            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1604          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1605            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1353  sub _get_next_token ($) { Line 1620  sub _get_next_token ($) {
1620    
1621          redo A;          redo A;
1622        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1623            !!!cp (122);
1624            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1625          !!!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  
1626          redo A;          redo A;
1627        } else {        } else {
1628          !!!cp (124);          !!!cp ('124.1');
1629          !!!parse-error (type => 'no space between attributes');          !!!parse-error (type => 'no space between attributes');
1630          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1631          ## reconsume          ## reconsume
1632          redo A;          redo A;
1633        }        }
1634        } elsif ($self->{state} == SELF_CLOSING_START_TAG_STATE) {
1635          if ($self->{next_char} == 0x003E) { # >
1636            if ($self->{current_token}->{type} == END_TAG_TOKEN) {
1637              !!!cp ('124.2');
1638              !!!parse-error (type => 'nestc', token => $self->{current_token});
1639              ## TODO: Different type than slash in start tag
1640              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1641              if ($self->{current_token}->{attributes}) {
1642                !!!cp ('124.4');
1643                !!!parse-error (type => 'end tag attribute');
1644              } else {
1645                !!!cp ('124.5');
1646              }
1647              ## TODO: Test |<title></title/>|
1648            } else {
1649              !!!cp ('124.3');
1650              $self->{self_closing} = 1;
1651            }
1652    
1653            $self->{state} = DATA_STATE;
1654            !!!next-input-character;
1655    
1656            !!!emit ($self->{current_token}); # start tag or end tag
1657    
1658            redo A;
1659          } else {
1660            !!!cp ('124.4');
1661            !!!parse-error (type => 'nestc');
1662            ## TODO: This error type is wrong.
1663            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1664            ## Reconsume.
1665            redo A;
1666          }
1667      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
1668        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1669                
1670        my $token = {type => COMMENT_TOKEN, data => ''};        ## NOTE: Set by the previous state
1671          #my $token = {type => COMMENT_TOKEN, data => ''};
1672    
1673        BC: {        BC: {
1674          if ($self->{next_char} == 0x003E) { # >          if ($self->{next_char} == 0x003E) { # >
# Line 1385  sub _get_next_token ($) { Line 1676  sub _get_next_token ($) {
1676            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1677            !!!next-input-character;            !!!next-input-character;
1678    
1679            !!!emit ($token);            !!!emit ($self->{current_token}); # comment
1680    
1681            redo A;            redo A;
1682          } elsif ($self->{next_char} == -1) {          } elsif ($self->{next_char} == -1) {
# Line 1393  sub _get_next_token ($) { Line 1684  sub _get_next_token ($) {
1684            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1685            ## reconsume            ## reconsume
1686    
1687            !!!emit ($token);            !!!emit ($self->{current_token}); # comment
1688    
1689            redo A;            redo A;
1690          } else {          } else {
1691            !!!cp (126);            !!!cp (126);
1692            $token->{data} .= chr ($self->{next_char});            $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
1693            !!!next-input-character;            !!!next-input-character;
1694            redo BC;            redo BC;
1695          }          }
# Line 1408  sub _get_next_token ($) { Line 1699  sub _get_next_token ($) {
1699      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
1700        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1701    
1702          my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1);
1703    
1704        my @next_char;        my @next_char;
1705        push @next_char, $self->{next_char};        push @next_char, $self->{next_char};
1706                
# Line 1416  sub _get_next_token ($) { Line 1709  sub _get_next_token ($) {
1709          push @next_char, $self->{next_char};          push @next_char, $self->{next_char};
1710          if ($self->{next_char} == 0x002D) { # -          if ($self->{next_char} == 0x002D) { # -
1711            !!!cp (127);            !!!cp (127);
1712            $self->{current_token} = {type => COMMENT_TOKEN, data => ''};            $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1713                                        line => $l, column => $c,
1714                                       };
1715            $self->{state} = COMMENT_START_STATE;            $self->{state} = COMMENT_START_STATE;
1716            !!!next-input-character;            !!!next-input-character;
1717            redo A;            redo A;
# Line 1452  sub _get_next_token ($) { Line 1747  sub _get_next_token ($) {
1747                      !!!cp (129);                      !!!cp (129);
1748                      ## TODO: What a stupid code this is!                      ## TODO: What a stupid code this is!
1749                      $self->{state} = DOCTYPE_STATE;                      $self->{state} = DOCTYPE_STATE;
1750                        $self->{current_token} = {type => DOCTYPE_TOKEN,
1751                                                  quirks => 1,
1752                                                  line => $l, column => $c,
1753                                                 };
1754                      !!!next-input-character;                      !!!next-input-character;
1755                      redo A;                      redo A;
1756                    } else {                    } else {
# Line 1472  sub _get_next_token ($) { Line 1771  sub _get_next_token ($) {
1771          } else {          } else {
1772            !!!cp (135);            !!!cp (135);
1773          }          }
1774          } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM and
1775                   $self->{open_elements}->[-1]->[1] & FOREIGN_EL and
1776                   $self->{next_char} == 0x005B) { # [
1777            !!!next-input-character;
1778            push @next_char, $self->{next_char};
1779            if ($self->{next_char} == 0x0043) { # C
1780              !!!next-input-character;
1781              push @next_char, $self->{next_char};
1782              if ($self->{next_char} == 0x0044) { # D
1783                !!!next-input-character;
1784                push @next_char, $self->{next_char};
1785                if ($self->{next_char} == 0x0041) { # A
1786                  !!!next-input-character;
1787                  push @next_char, $self->{next_char};
1788                  if ($self->{next_char} == 0x0054) { # T
1789                    !!!next-input-character;
1790                    push @next_char, $self->{next_char};
1791                    if ($self->{next_char} == 0x0041) { # A
1792                      !!!next-input-character;
1793                      push @next_char, $self->{next_char};
1794                      if ($self->{next_char} == 0x005B) { # [
1795                        !!!cp (135.1);
1796                        $self->{state} = CDATA_BLOCK_STATE;
1797                        !!!next-input-character;
1798                        redo A;
1799                      } else {
1800                        !!!cp (135.2);
1801                      }
1802                    } else {
1803                      !!!cp (135.3);
1804                    }
1805                  } else {
1806                    !!!cp (135.4);                
1807                  }
1808                } else {
1809                  !!!cp (135.5);
1810                }
1811              } else {
1812                !!!cp (135.6);
1813              }
1814            } else {
1815              !!!cp (135.7);
1816            }
1817        } else {        } else {
1818          !!!cp (136);          !!!cp (136);
1819        }        }
# Line 1480  sub _get_next_token ($) { Line 1822  sub _get_next_token ($) {
1822        $self->{next_char} = shift @next_char;        $self->{next_char} = shift @next_char;
1823        !!!back-next-input-character (@next_char);        !!!back-next-input-character (@next_char);
1824        $self->{state} = BOGUS_COMMENT_STATE;        $self->{state} = BOGUS_COMMENT_STATE;
1825          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1826                                    line => $l, column => $c,
1827                                   };
1828        redo A;        redo A;
1829                
1830        ## ISSUE: typos in spec: chacacters, is is a parse error        ## ISSUE: typos in spec: chacacters, is is a parse error
# Line 1603  sub _get_next_token ($) { Line 1948  sub _get_next_token ($) {
1948          redo A;          redo A;
1949        } elsif ($self->{next_char} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
1950          !!!cp (152);          !!!cp (152);
1951          !!!parse-error (type => 'dash in comment');          !!!parse-error (type => 'dash in comment',
1952                            line => $self->{line_prev},
1953                            column => $self->{column_prev});
1954          $self->{current_token}->{data} .= '-'; # comment          $self->{current_token}->{data} .= '-'; # comment
1955          ## Stay in the state          ## Stay in the state
1956          !!!next-input-character;          !!!next-input-character;
# Line 1619  sub _get_next_token ($) { Line 1966  sub _get_next_token ($) {
1966          redo A;          redo A;
1967        } else {        } else {
1968          !!!cp (154);          !!!cp (154);
1969          !!!parse-error (type => 'dash in comment');          !!!parse-error (type => 'dash in comment',
1970                            line => $self->{line_prev},
1971                            column => $self->{column_prev});
1972          $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment          $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment
1973          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
1974          !!!next-input-character;          !!!next-input-character;
# Line 1658  sub _get_next_token ($) { Line 2007  sub _get_next_token ($) {
2007          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2008          !!!next-input-character;          !!!next-input-character;
2009    
2010          !!!emit ({type => DOCTYPE_TOKEN, quirks => 1});          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2011    
2012          redo A;          redo A;
2013        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
# Line 1667  sub _get_next_token ($) { Line 2016  sub _get_next_token ($) {
2016          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2017          ## reconsume          ## reconsume
2018    
2019          !!!emit ({type => DOCTYPE_TOKEN, quirks => 1});          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2020    
2021          redo A;          redo A;
2022        } else {        } else {
2023          !!!cp (160);          !!!cp (160);
2024          $self->{current_token}          $self->{current_token}->{name} = chr $self->{next_char};
2025              = {type => DOCTYPE_TOKEN,          delete $self->{current_token}->{quirks};
                name => chr ($self->{next_char}),  
                #quirks => 0,  
               };  
2026  ## ISSUE: "Set the token's name name to the" in the spec  ## ISSUE: "Set the token's name name to the" in the spec
2027          $self->{state} = DOCTYPE_NAME_STATE;          $self->{state} = DOCTYPE_NAME_STATE;
2028          !!!next-input-character;          !!!next-input-character;
# Line 2192  sub _get_next_token ($) { Line 2538  sub _get_next_token ($) {
2538          !!!next-input-character;          !!!next-input-character;
2539          redo A;          redo A;
2540        }        }
2541        } elsif ($self->{state} == CDATA_BLOCK_STATE) {
2542          my $s = '';
2543          
2544          my ($l, $c) = ($self->{line}, $self->{column});
2545    
2546          CS: while ($self->{next_char} != -1) {
2547            if ($self->{next_char} == 0x005D) { # ]
2548              !!!next-input-character;
2549              if ($self->{next_char} == 0x005D) { # ]
2550                !!!next-input-character;
2551                MDC: {
2552                  if ($self->{next_char} == 0x003E) { # >
2553                    !!!cp (221.1);
2554                    !!!next-input-character;
2555                    last CS;
2556                  } elsif ($self->{next_char} == 0x005D) { # ]
2557                    !!!cp (221.2);
2558                    $s .= ']';
2559                    !!!next-input-character;
2560                    redo MDC;
2561                  } else {
2562                    !!!cp (221.3);
2563                    $s .= ']]';
2564                    #
2565                  }
2566                } # MDC
2567              } else {
2568                !!!cp (221.4);
2569                $s .= ']';
2570                #
2571              }
2572            } else {
2573              !!!cp (221.5);
2574              #
2575            }
2576            $s .= chr $self->{next_char};
2577            !!!next-input-character;
2578          } # CS
2579    
2580          $self->{state} = DATA_STATE;
2581          ## next-input-character done or EOF, which is reconsumed.
2582    
2583          if (length $s) {
2584            !!!cp (221.6);
2585            !!!emit ({type => CHARACTER_TOKEN, data => $s,
2586                      line => $l, column => $c});
2587          } else {
2588            !!!cp (221.7);
2589          }
2590    
2591          redo A;
2592    
2593          ## ISSUE: "text tokens" in spec.
2594          ## TODO: Streaming support
2595      } else {      } else {
2596        die "$0: $self->{state}: Unknown state";        die "$0: $self->{state}: Unknown state";
2597      }      }
# Line 2203  sub _get_next_token ($) { Line 2603  sub _get_next_token ($) {
2603  sub _tokenize_attempt_to_consume_an_entity ($$$) {  sub _tokenize_attempt_to_consume_an_entity ($$$) {
2604    my ($self, $in_attr, $additional) = @_;    my ($self, $in_attr, $additional) = @_;
2605    
2606      my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
2607    
2608    if ({    if ({
2609         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,
2610         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR
# Line 2243  sub _tokenize_attempt_to_consume_an_enti Line 2645  sub _tokenize_attempt_to_consume_an_enti
2645            redo X;            redo X;
2646          } elsif (not defined $code) { # no hexadecimal digit          } elsif (not defined $code) { # no hexadecimal digit
2647            !!!cp (1005);            !!!cp (1005);
2648            !!!parse-error (type => 'bare hcro');            !!!parse-error (type => 'bare hcro', line => $l, column => $c);
2649            !!!back-next-input-character ($x_char, $self->{next_char});            !!!back-next-input-character ($x_char, $self->{next_char});
2650            $self->{next_char} = 0x0023; # #            $self->{next_char} = 0x0023; # #
2651            return undef;            return undef;
# Line 2252  sub _tokenize_attempt_to_consume_an_enti Line 2654  sub _tokenize_attempt_to_consume_an_enti
2654            !!!next-input-character;            !!!next-input-character;
2655          } else {          } else {
2656            !!!cp (1007);            !!!cp (1007);
2657            !!!parse-error (type => 'no refc');            !!!parse-error (type => 'no refc', line => $l, column => $c);
2658          }          }
2659    
2660          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2661            !!!cp (1008);            !!!cp (1008);
2662            !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);            !!!parse-error (type => (sprintf 'invalid character reference:U+%04X', $code), line => $l, column => $c);
2663            $code = 0xFFFD;            $code = 0xFFFD;
2664          } elsif ($code > 0x10FFFF) {          } elsif ($code > 0x10FFFF) {
2665            !!!cp (1009);            !!!cp (1009);
2666            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);            !!!parse-error (type => (sprintf 'invalid character reference:U-%08X', $code), line => $l, column => $c);
2667            $code = 0xFFFD;            $code = 0xFFFD;
2668          } elsif ($code == 0x000D) {          } elsif ($code == 0x000D) {
2669            !!!cp (1010);            !!!cp (1010);
2670            !!!parse-error (type => 'CR character reference');            !!!parse-error (type => 'CR character reference', line => $l, column => $c);
2671            $code = 0x000A;            $code = 0x000A;
2672          } elsif (0x80 <= $code and $code <= 0x9F) {          } elsif (0x80 <= $code and $code <= 0x9F) {
2673            !!!cp (1011);            !!!cp (1011);
2674            !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);            !!!parse-error (type => (sprintf 'C1 character reference:U+%04X', $code), line => $l, column => $c);
2675            $code = $c1_entity_char->{$code};            $code = $c1_entity_char->{$code};
2676          }          }
2677    
2678          return {type => CHARACTER_TOKEN, data => chr $code,          return {type => CHARACTER_TOKEN, data => chr $code,
2679                  has_reference => 1};                  has_reference => 1,
2680                    line => $l, column => $c,
2681                   };
2682        } # X        } # X
2683      } elsif (0x0030 <= $self->{next_char} and      } elsif (0x0030 <= $self->{next_char} and
2684               $self->{next_char} <= 0x0039) { # 0..9               $self->{next_char} <= 0x0039) { # 0..9
# Line 2295  sub _tokenize_attempt_to_consume_an_enti Line 2699  sub _tokenize_attempt_to_consume_an_enti
2699          !!!next-input-character;          !!!next-input-character;
2700        } else {        } else {
2701          !!!cp (1014);          !!!cp (1014);
2702          !!!parse-error (type => 'no refc');          !!!parse-error (type => 'no refc', line => $l, column => $c);
2703        }        }
2704    
2705        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2706          !!!cp (1015);          !!!cp (1015);
2707          !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);          !!!parse-error (type => (sprintf 'invalid character reference:U+%04X', $code), line => $l, column => $c);
2708          $code = 0xFFFD;          $code = 0xFFFD;
2709        } elsif ($code > 0x10FFFF) {        } elsif ($code > 0x10FFFF) {
2710          !!!cp (1016);          !!!cp (1016);
2711          !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);          !!!parse-error (type => (sprintf 'invalid character reference:U-%08X', $code), line => $l, column => $c);
2712          $code = 0xFFFD;          $code = 0xFFFD;
2713        } elsif ($code == 0x000D) {        } elsif ($code == 0x000D) {
2714          !!!cp (1017);          !!!cp (1017);
2715          !!!parse-error (type => 'CR character reference');          !!!parse-error (type => 'CR character reference', line => $l, column => $c);
2716          $code = 0x000A;          $code = 0x000A;
2717        } elsif (0x80 <= $code and $code <= 0x9F) {        } elsif (0x80 <= $code and $code <= 0x9F) {
2718          !!!cp (1018);          !!!cp (1018);
2719          !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);          !!!parse-error (type => (sprintf 'C1 character reference:U+%04X', $code), line => $l, column => $c);
2720          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
2721        }        }
2722                
2723        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1};        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1,
2724                  line => $l, column => $c,
2725                 };
2726      } else {      } else {
2727        !!!cp (1019);        !!!cp (1019);
2728        !!!parse-error (type => 'bare nero');        !!!parse-error (type => 'bare nero', line => $l, column => $c);
2729        !!!back-next-input-character ($self->{next_char});        !!!back-next-input-character ($self->{next_char});
2730        $self->{next_char} = 0x0023; # #        $self->{next_char} = 0x0023; # #
2731        return undef;        return undef;
# Line 2336  sub _tokenize_attempt_to_consume_an_enti Line 2742  sub _tokenize_attempt_to_consume_an_enti
2742      require Whatpm::_NamedEntityList;      require Whatpm::_NamedEntityList;
2743      our $EntityChar;      our $EntityChar;
2744    
2745      while (length $entity_name < 10 and      while (length $entity_name < 30 and
2746             ## NOTE: Some number greater than the maximum length of entity name             ## NOTE: Some number greater than the maximum length of entity name
2747             ((0x0041 <= $self->{next_char} and # a             ((0x0041 <= $self->{next_char} and # a
2748               $self->{next_char} <= 0x005A) or # x               $self->{next_char} <= 0x005A) or # x
# Line 2369  sub _tokenize_attempt_to_consume_an_enti Line 2775  sub _tokenize_attempt_to_consume_an_enti
2775            
2776      if ($match > 0) {      if ($match > 0) {
2777        !!!cp (1023);        !!!cp (1023);
2778        return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};        return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
2779                  line => $l, column => $c,
2780                 };
2781      } elsif ($match < 0) {      } elsif ($match < 0) {
2782        !!!parse-error (type => 'no refc');        !!!parse-error (type => 'no refc', line => $l, column => $c);
2783        if ($in_attr and $match < -1) {        if ($in_attr and $match < -1) {
2784          !!!cp (1024);          !!!cp (1024);
2785          return {type => CHARACTER_TOKEN, data => '&'.$entity_name};          return {type => CHARACTER_TOKEN, data => '&'.$entity_name,
2786                    line => $l, column => $c,
2787                   };
2788        } else {        } else {
2789          !!!cp (1025);          !!!cp (1025);
2790          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
2791                    line => $l, column => $c,
2792                   };
2793        }        }
2794      } else {      } else {
2795        !!!cp (1026);        !!!cp (1026);
2796        !!!parse-error (type => 'bare ero');        !!!parse-error (type => 'bare ero', line => $l, column => $c);
2797        ## NOTE: "No characters are consumed" in the spec.        ## NOTE: "No characters are consumed" in the spec.
2798        return {type => CHARACTER_TOKEN, data => '&'.$value};        return {type => CHARACTER_TOKEN, data => '&'.$value,
2799                  line => $l, column => $c,
2800                 };
2801      }      }
2802    } else {    } else {
2803      !!!cp (1027);      !!!cp (1027);
2804      ## no characters are consumed      ## no characters are consumed
2805      !!!parse-error (type => 'bare ero');      !!!parse-error (type => 'bare ero', line => $l, column => $c);
2806      return undef;      return undef;
2807    }    }
2808  } # _tokenize_attempt_to_consume_an_entity  } # _tokenize_attempt_to_consume_an_entity
# Line 2459  sub _tree_construction_initial ($) { Line 2873  sub _tree_construction_initial ($) {
2873            defined $token->{public_identifier} or            defined $token->{public_identifier} or
2874            defined $token->{system_identifier}) {            defined $token->{system_identifier}) {
2875          !!!cp ('t1');          !!!cp ('t1');
2876          !!!parse-error (type => 'not HTML5');          !!!parse-error (type => 'not HTML5', token => $token);
2877        } elsif ($doctype_name ne 'HTML') {        } elsif ($doctype_name ne 'HTML') {
2878          !!!cp ('t2');          !!!cp ('t2');
2879          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)
2880          !!!parse-error (type => 'not HTML5');          !!!parse-error (type => 'not HTML5', token => $token);
2881        } else {        } else {
2882          !!!cp ('t3');          !!!cp ('t3');
2883        }        }
2884                
2885        my $doctype = $self->{document}->create_document_type_definition        my $doctype = $self->{document}->create_document_type_definition
2886          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
2887          ## NOTE: Default value for both |public_id| and |system_id| attributes
2888          ## are empty strings, so that we don't set any value in missing cases.
2889        $doctype->public_id ($token->{public_identifier})        $doctype->public_id ($token->{public_identifier})
2890            if defined $token->{public_identifier};            if defined $token->{public_identifier};
2891        $doctype->system_id ($token->{system_identifier})        $doctype->system_id ($token->{system_identifier})
# Line 2602  sub _tree_construction_initial ($) { Line 3018  sub _tree_construction_initial ($) {
3018                END_OF_FILE_TOKEN, 1,                END_OF_FILE_TOKEN, 1,
3019               }->{$token->{type}}) {               }->{$token->{type}}) {
3020        !!!cp ('t14');        !!!cp ('t14');
3021        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE', token => $token);
3022        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3023        ## Go to the "before html" insertion mode.        ## Go to the "before html" insertion mode.
3024        ## reprocess        ## reprocess
3025          !!!ack-later;
3026        return;        return;
3027      } elsif ($token->{type} == CHARACTER_TOKEN) {      } elsif ($token->{type} == CHARACTER_TOKEN) {
3028        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
# Line 2623  sub _tree_construction_initial ($) { Line 3040  sub _tree_construction_initial ($) {
3040          !!!cp ('t17');          !!!cp ('t17');
3041        }        }
3042    
3043        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE', token => $token);
3044        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3045        ## Go to the "before html" insertion mode.        ## Go to the "before html" insertion mode.
3046        ## reprocess        ## reprocess
# Line 2652  sub _tree_construction_root_element ($) Line 3069  sub _tree_construction_root_element ($)
3069    B: {    B: {
3070        if ($token->{type} == DOCTYPE_TOKEN) {        if ($token->{type} == DOCTYPE_TOKEN) {
3071          !!!cp ('t19');          !!!cp ('t19');
3072          !!!parse-error (type => 'in html:#DOCTYPE');          !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
3073          ## Ignore the token          ## Ignore the token
3074          ## Stay in the insertion mode.          ## Stay in the insertion mode.
3075          !!!next-token;          !!!next-token;
# Line 2686  sub _tree_construction_root_element ($) Line 3103  sub _tree_construction_root_element ($)
3103        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
3104          if ($token->{tag_name} eq 'html') {          if ($token->{tag_name} eq 'html') {
3105            my $root_element;            my $root_element;
3106            !!!create-element ($root_element, $token->{tag_name}, $token->{attributes});            !!!create-element ($root_element, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
3107            $self->{document}->append_child ($root_element);            $self->{document}->append_child ($root_element);
3108            push @{$self->{open_elements}}, [$root_element, 'html'];            push @{$self->{open_elements}},
3109                  [$root_element, $el_category->{html}];
3110    
3111            if ($token->{attributes}->{manifest}) {            if ($token->{attributes}->{manifest}) {
3112              !!!cp ('t24');              !!!cp ('t24');
3113              $self->{application_cache_selection}              $self->{application_cache_selection}
3114                  ->($token->{attributes}->{manifest}->{value});                  ->($token->{attributes}->{manifest}->{value});
3115              ## ISSUE: No relative reference resolution?              ## ISSUE: Spec is unclear on relative references.
3116                ## According to Hixie (#whatwg 2008-03-19), it should be
3117                ## resolved against the base URI of the document in HTML
3118                ## or xml:base of the element in XHTML.
3119            } else {            } else {
3120              !!!cp ('t25');              !!!cp ('t25');
3121              $self->{application_cache_selection}->(undef);              $self->{application_cache_selection}->(undef);
3122            }            }
3123    
3124              !!!nack ('t25c');
3125    
3126            !!!next-token;            !!!next-token;
3127            return; ## Go to the "before head" insertion mode.            return; ## Go to the "before head" insertion mode.
3128          } else {          } else {
# Line 2716  sub _tree_construction_root_element ($) Line 3139  sub _tree_construction_root_element ($)
3139          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
3140        }        }
3141    
3142      my $root_element; !!!create-element ($root_element, 'html');      my $root_element;
3143        !!!create-element ($root_element, $HTML_NS, 'html',, $token);
3144      $self->{document}->append_child ($root_element);      $self->{document}->append_child ($root_element);
3145      push @{$self->{open_elements}}, [$root_element, 'html'];      push @{$self->{open_elements}}, [$root_element, $el_category->{html}];
3146    
3147      $self->{application_cache_selection}->(undef);      $self->{application_cache_selection}->(undef);
3148    
3149      ## NOTE: Reprocess the token.      ## NOTE: Reprocess the token.
3150        !!!ack-later;
3151      return; ## Go to the "before head" insertion mode.      return; ## Go to the "before head" insertion mode.
3152    
3153      ## ISSUE: There is an issue in the spec      ## ISSUE: There is an issue in the spec
# Line 2743  sub _reset_insertion_mode ($) { Line 3168  sub _reset_insertion_mode ($) {
3168            
3169      ## Step 3      ## Step 3
3170      S3: {      S3: {
       ## ISSUE: Oops! "If node is the first node in the stack of open  
       ## elements, then set last to true. If the context element of the  
       ## HTML fragment parsing algorithm is neither a td element nor a  
       ## th element, then set node to the context element. (fragment case)":  
       ## The second "if" is in the scope of the first "if"!?  
3171        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
3172          $last = 1;          $last = 1;
3173          if (defined $self->{inner_html_node}) {          if (defined $self->{inner_html_node}) {
3174            if ($self->{inner_html_node}->[1] eq 'td' or            if ($self->{inner_html_node}->[1] & TABLE_CELL_EL) {
               $self->{inner_html_node}->[1] eq 'th') {  
3175              !!!cp ('t27');              !!!cp ('t27');
3176              #              #
3177            } else {            } else {
# Line 2762  sub _reset_insertion_mode ($) { Line 3181  sub _reset_insertion_mode ($) {
3181          }          }
3182        }        }
3183            
3184        ## Step 4..13      ## Step 4..14
3185        my $new_mode = {      my $new_mode;
3186        if ($node->[1] & FOREIGN_EL) {
3187          ## NOTE: Strictly spaking, the line below only applies to MathML and
3188          ## SVG elements.  Currently the HTML syntax supports only MathML and
3189          ## SVG elements as foreigners.
3190          $new_mode = $self->{insertion_mode} | IN_FOREIGN_CONTENT_IM;
3191          ## ISSUE: What is set as the secondary insertion mode?
3192        } else {
3193          $new_mode = {
3194                        select => IN_SELECT_IM,                        select => IN_SELECT_IM,
3195                        ## NOTE: |option| and |optgroup| do not set                        ## NOTE: |option| and |optgroup| do not set
3196                        ## insertion mode to "in select" by themselves.                        ## insertion mode to "in select" by themselves.
# Line 2779  sub _reset_insertion_mode ($) { Line 3206  sub _reset_insertion_mode ($) {
3206                        head => IN_BODY_IM, # not in head!                        head => IN_BODY_IM, # not in head!
3207                        body => IN_BODY_IM,                        body => IN_BODY_IM,
3208                        frameset => IN_FRAMESET_IM,                        frameset => IN_FRAMESET_IM,
3209                       }->{$node->[1]};                       }->{$node->[0]->manakai_local_name};
3210        $self->{insertion_mode} = $new_mode and return if defined $new_mode;      }
3211        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
3212                
3213        ## Step 14        ## Step 15
3214        if ($node->[1] eq 'html') {        if ($node->[1] & HTML_EL) {
3215          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
3216            !!!cp ('t29');            !!!cp ('t29');
3217            $self->{insertion_mode} = BEFORE_HEAD_IM;            $self->{insertion_mode} = BEFORE_HEAD_IM;
# Line 2797  sub _reset_insertion_mode ($) { Line 3225  sub _reset_insertion_mode ($) {
3225          !!!cp ('t31');          !!!cp ('t31');
3226        }        }
3227                
3228        ## Step 15        ## Step 16
3229        $self->{insertion_mode} = IN_BODY_IM and return if $last;        $self->{insertion_mode} = IN_BODY_IM and return if $last;
3230                
3231        ## Step 16        ## Step 17
3232        $i--;        $i--;
3233        $node = $self->{open_elements}->[$i];        $node = $self->{open_elements}->[$i];
3234                
3235        ## Step 17        ## Step 18
3236        redo S3;        redo S3;
3237      } # S3      } # S3
3238    
# Line 2908  sub _tree_construction_main ($) { Line 3336  sub _tree_construction_main ($) {
3336      !!!cp ('t39');      !!!cp ('t39');
3337    }; # $clear_up_to_marker    }; # $clear_up_to_marker
3338    
3339    my $parse_rcdata = sub ($$) {    my $insert;
3340      my ($content_model_flag, $insert) = @_;  
3341      my $parse_rcdata = sub ($) {
3342        my ($content_model_flag) = @_;
3343    
3344      ## Step 1      ## Step 1
3345      my $start_tag_name = $token->{tag_name};      my $start_tag_name = $token->{tag_name};
3346      my $el;      my $el;
3347      !!!create-element ($el, $start_tag_name, $token->{attributes});      !!!create-element ($el, $HTML_NS, $start_tag_name, $token->{attributes}, $token);
3348    
3349      ## Step 2      ## Step 2
3350      $insert->($el); # /context node/->append_child ($el)      $insert->($el);
3351    
3352      ## Step 3      ## Step 3
3353      $self->{content_model} = $content_model_flag; # CDATA or RCDATA      $self->{content_model} = $content_model_flag; # CDATA or RCDATA
# Line 2925  sub _tree_construction_main ($) { Line 3355  sub _tree_construction_main ($) {
3355    
3356      ## Step 4      ## Step 4
3357      my $text = '';      my $text = '';
3358        !!!nack ('t40.1');
3359      !!!next-token;      !!!next-token;
3360      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
3361        !!!cp ('t40');        !!!cp ('t40');
# Line 2947  sub _tree_construction_main ($) { Line 3378  sub _tree_construction_main ($) {
3378          $token->{tag_name} eq $start_tag_name) {          $token->{tag_name} eq $start_tag_name) {
3379        !!!cp ('t42');        !!!cp ('t42');
3380        ## Ignore the token        ## Ignore the token
     } elsif ($content_model_flag == CDATA_CONTENT_MODEL) {  
       !!!cp ('t43');  
       !!!parse-error (type => 'in CDATA:#'.$token->{type});  
     } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {  
       !!!cp ('t44');  
       !!!parse-error (type => 'in RCDATA:#'.$token->{type});  
3381      } else {      } else {
3382        die "$0: $content_model_flag in parse_rcdata";        ## NOTE: An end-of-file token.
3383          if ($content_model_flag == CDATA_CONTENT_MODEL) {
3384            !!!cp ('t43');
3385            !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);
3386          } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
3387            !!!cp ('t44');
3388            !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);
3389          } else {
3390            die "$0: $content_model_flag in parse_rcdata";
3391          }
3392      }      }
3393      !!!next-token;      !!!next-token;
3394    }; # $parse_rcdata    }; # $parse_rcdata
3395    
3396    my $script_start_tag = sub ($) {    my $script_start_tag = sub () {
     my $insert = $_[0];  
3397      my $script_el;      my $script_el;
3398      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, $HTML_NS, 'script', $token->{attributes}, $token);
3399      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
3400    
3401      $self->{content_model} = CDATA_CONTENT_MODEL;      $self->{content_model} = CDATA_CONTENT_MODEL;
3402      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
3403            
3404      my $text = '';      my $text = '';
3405        !!!nack ('t45.1');
3406      !!!next-token;      !!!next-token;
3407      while ($token->{type} == CHARACTER_TOKEN) {      while ($token->{type} == CHARACTER_TOKEN) {
3408        !!!cp ('t45');        !!!cp ('t45');
# Line 2988  sub _tree_construction_main ($) { Line 3422  sub _tree_construction_main ($) {
3422        ## Ignore the token        ## Ignore the token
3423      } else {      } else {
3424        !!!cp ('t48');        !!!cp ('t48');
3425        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);
3426        ## ISSUE: And ignore?        ## ISSUE: And ignore?
3427        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
3428      }      }
# Line 3011  sub _tree_construction_main ($) { Line 3445  sub _tree_construction_main ($) {
3445      !!!next-token;      !!!next-token;
3446    }; # $script_start_tag    }; # $script_start_tag
3447    
3448      ## NOTE: $open_tables->[-1]->[0] is the "current table" element node.
3449      ## NOTE: $open_tables->[-1]->[1] is the "tainted" flag.
3450      my $open_tables = [[$self->{open_elements}->[0]->[0]]];
3451    
3452    my $formatting_end_tag = sub {    my $formatting_end_tag = sub {
3453      my $tag_name = shift;      my $end_tag_token = shift;
3454        my $tag_name = $end_tag_token->{tag_name};
3455    
3456        ## NOTE: The adoption agency algorithm (AAA).
3457    
3458      FET: {      FET: {
3459        ## Step 1        ## Step 1
3460        my $formatting_element;        my $formatting_element;
3461        my $formatting_element_i_in_active;        my $formatting_element_i_in_active;
3462        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3463          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {          if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3464              !!!cp ('t52');
3465              last AFE;
3466            } elsif ($active_formatting_elements->[$_]->[0]->manakai_local_name
3467                         eq $tag_name) {
3468            !!!cp ('t51');            !!!cp ('t51');
3469            $formatting_element = $active_formatting_elements->[$_];            $formatting_element = $active_formatting_elements->[$_];
3470            $formatting_element_i_in_active = $_;            $formatting_element_i_in_active = $_;
3471            last AFE;            last AFE;
         } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {  
           !!!cp ('t52');  
           last AFE;  
3472          }          }
3473        } # AFE        } # AFE
3474        unless (defined $formatting_element) {        unless (defined $formatting_element) {
3475          !!!cp ('t53');          !!!cp ('t53');
3476          !!!parse-error (type => 'unmatched end tag:'.$tag_name);          !!!parse-error (type => 'unmatched end tag:'.$tag_name, token => $end_tag_token);
3477          ## Ignore the token          ## Ignore the token
3478          !!!next-token;          !!!next-token;
3479          return;          return;
# Line 3048  sub _tree_construction_main ($) { Line 3490  sub _tree_construction_main ($) {
3490              last INSCOPE;              last INSCOPE;
3491            } else { # in open elements but not in scope            } else { # in open elements but not in scope
3492              !!!cp ('t55');              !!!cp ('t55');
3493              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name},
3494                                token => $end_tag_token);
3495              ## Ignore the token              ## Ignore the token
3496              !!!next-token;              !!!next-token;
3497              return;              return;
3498            }            }
3499          } elsif ({          } elsif ($node->[1] & SCOPING_EL) {
                   table => 1, caption => 1, td => 1, th => 1,  
                   button => 1, marquee => 1, object => 1, html => 1,  
                  }->{$node->[1]}) {  
3500            !!!cp ('t56');            !!!cp ('t56');
3501            $in_scope = 0;            $in_scope = 0;
3502          }          }
3503        } # INSCOPE        } # INSCOPE
3504        unless (defined $formatting_element_i_in_open) {        unless (defined $formatting_element_i_in_open) {
3505          !!!cp ('t57');          !!!cp ('t57');
3506          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name},
3507                            token => $end_tag_token);
3508          pop @$active_formatting_elements; # $formatting_element          pop @$active_formatting_elements; # $formatting_element
3509          !!!next-token; ## TODO: ok?          !!!next-token; ## TODO: ok?
3510          return;          return;
3511        }        }
3512        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
3513          !!!cp ('t58');          !!!cp ('t58');
3514          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);          !!!parse-error (type => 'not closed',
3515                            value => $self->{open_elements}->[-1]->[0]
3516                                ->manakai_local_name,
3517                            token => $end_tag_token);
3518        }        }
3519                
3520        ## Step 2        ## Step 2
# Line 3078  sub _tree_construction_main ($) { Line 3522  sub _tree_construction_main ($) {
3522        my $furthest_block_i_in_open;        my $furthest_block_i_in_open;
3523        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3524          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
3525          if (not $formatting_category->{$node->[1]} and          if (not ($node->[1] & FORMATTING_EL) and
3526              #not $phrasing_category->{$node->[1]} and              #not $phrasing_category->{$node->[1]} and
3527              ($special_category->{$node->[1]} or              ($node->[1] & SPECIAL_EL or
3528               $scoping_category->{$node->[1]})) {               $node->[1] & SCOPING_EL)) { ## Scoping is redundant, maybe
3529            !!!cp ('t59');            !!!cp ('t59');
3530            $furthest_block = $node;            $furthest_block = $node;
3531            $furthest_block_i_in_open = $_;            $furthest_block_i_in_open = $_;
# Line 3167  sub _tree_construction_main ($) { Line 3611  sub _tree_construction_main ($) {
3611        } # S7          } # S7  
3612                
3613        ## Step 8        ## Step 8
3614        $common_ancestor_node->[0]->append_child ($last_node->[0]);        if ($common_ancestor_node->[1] & TABLE_ROWS_EL) {
3615            my $foster_parent_element;
3616            my $next_sibling;
3617            OE: for (reverse 0..$#{$self->{open_elements}}) {
3618              if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
3619                                 my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3620                                 if (defined $parent and $parent->node_type == 1) {
3621                                   !!!cp ('t65.1');
3622                                   $foster_parent_element = $parent;
3623                                   $next_sibling = $self->{open_elements}->[$_]->[0];
3624                                 } else {
3625                                   !!!cp ('t65.2');
3626                                   $foster_parent_element
3627                                     = $self->{open_elements}->[$_ - 1]->[0];
3628                                 }
3629                                 last OE;
3630                               }
3631                             } # OE
3632                             $foster_parent_element = $self->{open_elements}->[0]->[0]
3633                               unless defined $foster_parent_element;
3634            $foster_parent_element->insert_before ($last_node->[0], $next_sibling);
3635            $open_tables->[-1]->[1] = 1; # tainted
3636          } else {
3637            !!!cp ('t65.3');
3638            $common_ancestor_node->[0]->append_child ($last_node->[0]);
3639          }
3640                
3641        ## Step 9        ## Step 9
3642        my $clone = [$formatting_element->[0]->clone_node (0),        my $clone = [$formatting_element->[0]->clone_node (0),
# Line 3213  sub _tree_construction_main ($) { Line 3682  sub _tree_construction_main ($) {
3682      } # FET      } # FET
3683    }; # $formatting_end_tag    }; # $formatting_end_tag
3684    
3685    my $insert_to_current = sub {    $insert = my $insert_to_current = sub {
3686      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);
3687    }; # $insert_to_current    }; # $insert_to_current
3688    
3689    my $insert_to_foster = sub {    my $insert_to_foster = sub {
3690                         my $child = shift;      my $child = shift;
3691                         if ({      if ($self->{open_elements}->[-1]->[1] & TABLE_ROWS_EL) {
3692                              table => 1, tbody => 1, tfoot => 1,        # MUST
3693                              thead => 1, tr => 1,        my $foster_parent_element;
3694                             }->{$self->{open_elements}->[-1]->[1]}) {        my $next_sibling;
3695                           # MUST        OE: for (reverse 0..$#{$self->{open_elements}}) {
3696                           my $foster_parent_element;          if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
                          my $next_sibling;  
                          OE: for (reverse 0..$#{$self->{open_elements}}) {  
                            if ($self->{open_elements}->[$_]->[1] eq 'table') {  
3697                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3698                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
3699                                 !!!cp ('t70');                                 !!!cp ('t70');
# Line 3245  sub _tree_construction_main ($) { Line 3711  sub _tree_construction_main ($) {
3711                             unless defined $foster_parent_element;                             unless defined $foster_parent_element;
3712                           $foster_parent_element->insert_before                           $foster_parent_element->insert_before
3713                             ($child, $next_sibling);                             ($child, $next_sibling);
3714                         } else {        $open_tables->[-1]->[1] = 1; # tainted
3715                           !!!cp ('t72');      } else {
3716                           $self->{open_elements}->[-1]->[0]->append_child ($child);        !!!cp ('t72');
3717                         }        $self->{open_elements}->[-1]->[0]->append_child ($child);
3718        }
3719    }; # $insert_to_foster    }; # $insert_to_foster
3720    
3721    my $insert;    B: while (1) {
   
   B: {  
3722      if ($token->{type} == DOCTYPE_TOKEN) {      if ($token->{type} == DOCTYPE_TOKEN) {
3723        !!!cp ('t73');        !!!cp ('t73');
3724        !!!parse-error (type => 'DOCTYPE in the middle');        !!!parse-error (type => 'DOCTYPE in the middle', token => $token);
3725        ## Ignore the token        ## Ignore the token
3726        ## Stay in the phase        ## Stay in the phase
3727        !!!next-token;        !!!next-token;
3728        redo B;        next B;
     } elsif ($token->{type} == END_OF_FILE_TOKEN) {  
       if ($self->{insertion_mode} & AFTER_HTML_IMS) {  
         !!!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;  
3729      } elsif ($token->{type} == START_TAG_TOKEN and      } elsif ($token->{type} == START_TAG_TOKEN and
3730               $token->{tag_name} eq 'html') {               $token->{tag_name} eq 'html') {
3731        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
3732          !!!cp ('t79');          !!!cp ('t79');
3733          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html:html', token => $token);
3734          $self->{insertion_mode} = AFTER_BODY_IM;          $self->{insertion_mode} = AFTER_BODY_IM;
3735        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
3736          !!!cp ('t80');          !!!cp ('t80');
3737          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html:html', token => $token);
3738          $self->{insertion_mode} = AFTER_FRAMESET_IM;          $self->{insertion_mode} = AFTER_FRAMESET_IM;
3739        } else {        } else {
3740          !!!cp ('t81');          !!!cp ('t81');
3741        }        }
3742    
3743        !!!cp ('t82');        !!!cp ('t82');
3744        !!!parse-error (type => 'not first start tag');        !!!parse-error (type => 'not first start tag', token => $token);
3745        my $top_el = $self->{open_elements}->[0]->[0];        my $top_el = $self->{open_elements}->[0]->[0];
3746        for my $attr_name (keys %{$token->{attributes}}) {        for my $attr_name (keys %{$token->{attributes}}) {
3747          unless ($top_el->has_attribute_ns (undef, $attr_name)) {          unless ($top_el->has_attribute_ns (undef, $attr_name)) {
# Line 3318  sub _tree_construction_main ($) { Line 3751  sub _tree_construction_main ($) {
3751               $token->{attributes}->{$attr_name}->{value});               $token->{attributes}->{$attr_name}->{value});
3752          }          }
3753        }        }
3754          !!!nack ('t84.1');
3755        !!!next-token;        !!!next-token;
3756        redo B;        next B;
3757      } elsif ($token->{type} == COMMENT_TOKEN) {      } elsif ($token->{type} == COMMENT_TOKEN) {
3758        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
3759        if ($self->{insertion_mode} & AFTER_HTML_IMS) {        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
# Line 3333  sub _tree_construction_main ($) { Line 3767  sub _tree_construction_main ($) {
3767          $self->{open_elements}->[-1]->[0]->append_child ($comment);          $self->{open_elements}->[-1]->[0]->append_child ($comment);
3768        }        }
3769        !!!next-token;        !!!next-token;
3770        redo B;        next B;
3771      } elsif ($self->{insertion_mode} & HEAD_IMS) {      } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
3772          if ($token->{type} == CHARACTER_TOKEN) {
3773            !!!cp ('t87.1');
3774            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
3775            !!!next-token;
3776            next B;
3777          } elsif ($token->{type} == START_TAG_TOKEN) {
3778            if ((not {mglyph => 1, malignmark => 1}->{$token->{tag_name}} and
3779                 $self->{open_elements}->[-1]->[1] & FOREIGN_FLOW_CONTENT_EL) or
3780                not ($self->{open_elements}->[-1]->[1] & FOREIGN_EL) or
3781                ($token->{tag_name} eq 'svg' and
3782                 $self->{open_elements}->[-1]->[1] & MML_AXML_EL)) {
3783              ## NOTE: "using the rules for secondary insertion mode"then"continue"
3784              !!!cp ('t87.2');
3785              #
3786            } elsif ({
3787                      b => 1, big => 1, blockquote => 1, body => 1, br => 1,
3788                      center => 1, code => 1, dd => 1, div => 1, dl => 1, em => 1,
3789                      embed => 1, font => 1, h1 => 1, h2 => 1, h3 => 1, ## No h4!
3790                      h5 => 1, h6 => 1, head => 1, hr => 1, i => 1, img => 1,
3791                      li => 1, menu => 1, meta => 1, nobr => 1, p => 1, pre => 1,
3792                      ruby => 1, s => 1, small => 1, span => 1, strong => 1,
3793                      sub => 1, sup => 1, table => 1, tt => 1, u => 1, ul => 1,
3794                      var => 1,
3795                     }->{$token->{tag_name}}) {
3796              !!!cp ('t87.2');
3797              !!!parse-error (type => 'not closed',
3798                              value => $self->{open_elements}->[-1]->[0]
3799                                  ->manakai_local_name,
3800                              token => $token);
3801    
3802              pop @{$self->{open_elements}}
3803                  while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
3804    
3805              $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
3806              ## Reprocess.
3807              next B;
3808            } else {
3809              my $nsuri = $self->{open_elements}->[-1]->[0]->namespace_uri;
3810              my $tag_name = $token->{tag_name};
3811              if ($nsuri eq $SVG_NS) {
3812                $tag_name = {
3813                   altglyph => 'altGlyph',
3814                   altglyphdef => 'altGlyphDef',
3815                   altglyphitem => 'altGlyphItem',
3816                   animatecolor => 'animateColor',
3817                   animatemotion => 'animateMotion',
3818                   animatetransform => 'animateTransform',
3819                   clippath => 'clipPath',
3820                   feblend => 'feBlend',
3821                   fecolormatrix => 'feColorMatrix',
3822                   fecomponenttransfer => 'feComponentTransfer',
3823                   fecomposite => 'feComposite',
3824                   feconvolvematrix => 'feConvolveMatrix',
3825                   fediffuselighting => 'feDiffuseLighting',
3826                   fedisplacementmap => 'feDisplacementMap',
3827                   fedistantlight => 'feDistantLight',
3828                   feflood => 'feFlood',
3829                   fefunca => 'feFuncA',
3830                   fefuncb => 'feFuncB',
3831                   fefuncg => 'feFuncG',
3832                   fefuncr => 'feFuncR',
3833                   fegaussianblur => 'feGaussianBlur',
3834                   feimage => 'feImage',
3835                   femerge => 'feMerge',
3836                   femergenode => 'feMergeNode',
3837                   femorphology => 'feMorphology',
3838                   feoffset => 'feOffset',
3839                   fepointlight => 'fePointLight',
3840                   fespecularlighting => 'feSpecularLighting',
3841                   fespotlight => 'feSpotLight',
3842                   fetile => 'feTile',
3843                   feturbulence => 'feTurbulence',
3844                   foreignobject => 'foreignObject',
3845                   glyphref => 'glyphRef',
3846                   lineargradient => 'linearGradient',
3847                   radialgradient => 'radialGradient',
3848                   #solidcolor => 'solidColor', ## NOTE: Commented in spec (SVG1.2)
3849                   textpath => 'textPath',  
3850                }->{$tag_name} || $tag_name;
3851              }
3852    
3853              ## "adjust SVG attributes" (SVG only) - done in insert-element-f
3854    
3855              ## "adjust foreign attributes" - done in insert-element-f
3856    
3857              !!!insert-element-f ($nsuri, $tag_name, $token->{attributes}, $token);
3858    
3859              if ($self->{self_closing}) {
3860                pop @{$self->{open_elements}};
3861                !!!ack ('t87.3');
3862              } else {
3863                !!!cp ('t87.4');
3864              }
3865    
3866              !!!next-token;
3867              next B;
3868            }
3869          } elsif ($token->{type} == END_TAG_TOKEN) {
3870            ## NOTE: "using the rules for secondary insertion mode" then "continue"
3871            !!!cp ('t87.5');
3872            #
3873          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
3874            ## NOTE: "using the rules for secondary insertion mode" then "continue"
3875            !!!cp ('t87.6');
3876            #
3877            ## TODO: ...
3878          } else {
3879            die "$0: $token->{type}: Unknown token type";        
3880          }
3881        }
3882    
3883        if ($self->{insertion_mode} & HEAD_IMS) {
3884        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
3885          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
3886            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3887                !!!cp ('t88.2');
3888                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
3889              } else {
3890                !!!cp ('t88.1');
3891                ## Ignore the token.
3892                !!!next-token;
3893                next B;
3894              }
3895            unless (length $token->{data}) {            unless (length $token->{data}) {
3896              !!!cp ('t88');              !!!cp ('t88');
3897              !!!next-token;              !!!next-token;
3898              redo B;              next B;
3899            }            }
3900          }          }
3901    
3902          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3903            !!!cp ('t89');            !!!cp ('t89');
3904            ## As if <head>            ## As if <head>
3905            !!!create-element ($self->{head_element}, 'head');            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
3906            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3907            push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            push @{$self->{open_elements}},
3908                  [$self->{head_element}, $el_category->{head}];
3909    
3910            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
3911            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
# Line 3360  sub _tree_construction_main ($) { Line 3915  sub _tree_construction_main ($) {
3915            !!!cp ('t90');            !!!cp ('t90');
3916            ## As if </noscript>            ## As if </noscript>
3917            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
3918            !!!parse-error (type => 'in noscript:#character');            !!!parse-error (type => 'in noscript:#character', token => $token);
3919                        
3920            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
3921            ## As if </head>            ## As if </head>
# Line 3376  sub _tree_construction_main ($) { Line 3931  sub _tree_construction_main ($) {
3931            !!!cp ('t92');            !!!cp ('t92');
3932          }          }
3933    
3934              ## "after head" insertion mode          ## "after head" insertion mode
3935              ## As if <body>          ## As if <body>
3936              !!!insert-element ('body');          !!!insert-element ('body',, $token);
3937              $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
3938              ## reprocess          ## reprocess
3939              redo B;          next B;
3940            } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
3941              if ($token->{tag_name} eq 'head') {          if ($token->{tag_name} eq 'head') {
3942                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3943                  !!!cp ('t93');              !!!cp ('t93');
3944                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});              !!!create-element ($self->{head_element}, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
3945                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              $self->{open_elements}->[-1]->[0]->append_child
3946                  push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];                  ($self->{head_element});
3947                  $self->{insertion_mode} = IN_HEAD_IM;              push @{$self->{open_elements}},
3948                  !!!next-token;                  [$self->{head_element}, $el_category->{head}];
3949                  redo B;              $self->{insertion_mode} = IN_HEAD_IM;
3950                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {              !!!nack ('t93.1');
3951                  !!!cp ('t94');              !!!next-token;
3952                  #              next B;
3953                } else {            } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
3954                  !!!cp ('t95');              !!!cp ('t94');
3955                  !!!parse-error (type => 'in head:head'); # or in head noscript              #
3956                  ## Ignore the token            } else {
3957                  !!!next-token;              !!!cp ('t95');
3958                  redo B;              !!!parse-error (type => 'in head:head', token => $token); # or in head noscript
3959                }              ## Ignore the token
3960              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              !!!nack ('t95.1');
3961                !!!cp ('t96');              !!!next-token;
3962                ## As if <head>              next B;
3963                !!!create-element ($self->{head_element}, 'head');            }
3964                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});          } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3965                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            !!!cp ('t96');
3966              ## As if <head>
3967              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
3968              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3969              push @{$self->{open_elements}},
3970                  [$self->{head_element}, $el_category->{head}];
3971    
3972                $self->{insertion_mode} = IN_HEAD_IM;            $self->{insertion_mode} = IN_HEAD_IM;
3973                ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
3974              } else {          } else {
3975                !!!cp ('t97');            !!!cp ('t97');
3976              }          }
3977    
3978              if ($token->{tag_name} eq 'base') {              if ($token->{tag_name} eq 'base') {
3979                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3980                  !!!cp ('t98');                  !!!cp ('t98');
3981                  ## As if </noscript>                  ## As if </noscript>
3982                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3983                  !!!parse-error (type => 'in noscript:base');                  !!!parse-error (type => 'in noscript:base', token => $token);
3984                                
3985                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
3986                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
# Line 3431  sub _tree_construction_main ($) { Line 3991  sub _tree_construction_main ($) {
3991                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3992                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3993                  !!!cp ('t100');                  !!!cp ('t100');
3994                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
3995                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
3996                        [$self->{head_element}, $el_category->{head}];
3997                } else {                } else {
3998                  !!!cp ('t101');                  !!!cp ('t101');
3999                }                }
4000                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4001                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4002                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4003                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4004                  !!!nack ('t101.1');
4005                !!!next-token;                !!!next-token;
4006                redo B;                next B;
4007              } elsif ($token->{tag_name} eq 'link') {              } elsif ($token->{tag_name} eq 'link') {
4008                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4009                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4010                  !!!cp ('t102');                  !!!cp ('t102');
4011                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4012                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4013                        [$self->{head_element}, $el_category->{head}];
4014                } else {                } else {
4015                  !!!cp ('t103');                  !!!cp ('t103');
4016                }                }
4017                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4018                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4019                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4020                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4021                  !!!ack ('t103.1');
4022                !!!next-token;                !!!next-token;
4023                redo B;                next B;
4024              } elsif ($token->{tag_name} eq 'meta') {              } elsif ($token->{tag_name} eq 'meta') {
4025                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4026                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4027                  !!!cp ('t104');                  !!!cp ('t104');
4028                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4029                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4030                        [$self->{head_element}, $el_category->{head}];
4031                } else {                } else {
4032                  !!!cp ('t105');                  !!!cp ('t105');
4033                }                }
4034                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4035                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.
4036    
4037                unless ($self->{confident}) {                unless ($self->{confident}) {
4038                  if ($token->{attributes}->{charset}) { ## TODO: And if supported                  if ($token->{attributes}->{charset}) { ## TODO: And if supported
4039                    !!!cp ('t106');                    !!!cp ('t106');
4040                    $self->{change_encoding}                    $self->{change_encoding}
4041                        ->($self, $token->{attributes}->{charset}->{value});                        ->($self, $token->{attributes}->{charset}->{value},
4042                             $token);
4043                                        
4044                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4045                        ->set_user_data (manakai_has_reference =>                        ->set_user_data (manakai_has_reference =>
# Line 3488  sub _tree_construction_main ($) { Line 4054  sub _tree_construction_main ($) {
4054                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
4055                      !!!cp ('t107');                      !!!cp ('t107');
4056                      $self->{change_encoding}                      $self->{change_encoding}
4057                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,
4058                               $token);
4059                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4060                          ->set_user_data (manakai_has_reference =>                          ->set_user_data (manakai_has_reference =>
4061                                               $token->{attributes}->{content}                                               $token->{attributes}->{content}
# Line 3514  sub _tree_construction_main ($) { Line 4081  sub _tree_construction_main ($) {
4081                  }                  }
4082                }                }
4083    
4084                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4085                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4086                  !!!ack ('t110.1');
4087                !!!next-token;                !!!next-token;
4088                redo B;                next B;
4089              } elsif ($token->{tag_name} eq 'title') {              } elsif ($token->{tag_name} eq 'title') {
4090                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4091                  !!!cp ('t111');                  !!!cp ('t111');
4092                  ## As if </noscript>                  ## As if </noscript>
4093                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4094                  !!!parse-error (type => 'in noscript:title');                  !!!parse-error (type => 'in noscript:title', token => $token);
4095                                
4096                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4097                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4098                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4099                  !!!cp ('t112');                  !!!cp ('t112');
4100                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4101                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4102                        [$self->{head_element}, $el_category->{head}];
4103                } else {                } else {
4104                  !!!cp ('t113');                  !!!cp ('t113');
4105                }                }
# Line 3538  sub _tree_construction_main ($) { Line 4107  sub _tree_construction_main ($) {
4107                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4108                my $parent = defined $self->{head_element} ? $self->{head_element}                my $parent = defined $self->{head_element} ? $self->{head_element}
4109                    : $self->{open_elements}->[-1]->[0];                    : $self->{open_elements}->[-1]->[0];
4110                $parse_rcdata->(RCDATA_CONTENT_MODEL,                $parse_rcdata->(RCDATA_CONTENT_MODEL);
4111                                sub { $parent->append_child ($_[0]) });                pop @{$self->{open_elements}} # <head>
               pop @{$self->{open_elements}}  
4112                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4113                redo B;                next B;
4114              } elsif ($token->{tag_name} eq 'style') {              } elsif ($token->{tag_name} eq 'style') {
4115                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
4116                ## insertion mode IN_HEAD_IM)                ## insertion mode IN_HEAD_IM)
4117                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4118                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4119                  !!!cp ('t114');                  !!!cp ('t114');
4120                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4121                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4122                        [$self->{head_element}, $el_category->{head}];
4123                } else {                } else {
4124                  !!!cp ('t115');                  !!!cp ('t115');
4125                }                }
4126                $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);                $parse_rcdata->(CDATA_CONTENT_MODEL);
4127                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4128                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4129                redo B;                next B;
4130              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4131                if ($self->{insertion_mode} == IN_HEAD_IM) {                if ($self->{insertion_mode} == IN_HEAD_IM) {
4132                  !!!cp ('t116');                  !!!cp ('t116');
4133                  ## NOTE: and scripting is disalbed                  ## NOTE: and scripting is disalbed
4134                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4135                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
4136                    !!!nack ('t116.1');
4137                  !!!next-token;                  !!!next-token;
4138                  redo B;                  next B;
4139                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4140                  !!!cp ('t117');                  !!!cp ('t117');
4141                  !!!parse-error (type => 'in noscript:noscript');                  !!!parse-error (type => 'in noscript:noscript', token => $token);
4142                  ## Ignore the token                  ## Ignore the token
4143                    !!!nack ('t117.1');
4144                  !!!next-token;                  !!!next-token;
4145                  redo B;                  next B;
4146                } else {                } else {
4147                  !!!cp ('t118');                  !!!cp ('t118');
4148                  #                  #
# Line 3581  sub _tree_construction_main ($) { Line 4152  sub _tree_construction_main ($) {
4152                  !!!cp ('t119');                  !!!cp ('t119');
4153                  ## As if </noscript>                  ## As if </noscript>
4154                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4155                  !!!parse-error (type => 'in noscript:script');                  !!!parse-error (type => 'in noscript:script', token => $token);
4156                                
4157                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4158                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4159                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4160                  !!!cp ('t120');                  !!!cp ('t120');
4161                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4162                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4163                        [$self->{head_element}, $el_category->{head}];
4164                } else {                } else {
4165                  !!!cp ('t121');                  !!!cp ('t121');
4166                }                }
4167    
4168                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4169                $script_start_tag->($insert_to_current);                $script_start_tag->();
4170                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4171                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4172                redo B;                next B;
4173              } elsif ($token->{tag_name} eq 'body' or              } elsif ($token->{tag_name} eq 'body' or
4174                       $token->{tag_name} eq 'frameset') {                       $token->{tag_name} eq 'frameset') {
4175                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4176                  !!!cp ('t122');                  !!!cp ('t122');
4177                  ## As if </noscript>                  ## As if </noscript>
4178                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4179                  !!!parse-error (type => 'in noscript:'.$token->{tag_name});                  !!!parse-error (type => 'in noscript:'.$token->{tag_name}, token => $token);
4180                                    
4181                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4182                  ## As if </head>                  ## As if </head>
# Line 3621  sub _tree_construction_main ($) { Line 4193  sub _tree_construction_main ($) {
4193                }                }
4194    
4195                ## "after head" insertion mode                ## "after head" insertion mode
4196                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4197                if ($token->{tag_name} eq 'body') {                if ($token->{tag_name} eq 'body') {
4198                  !!!cp ('t126');                  !!!cp ('t126');
4199                  $self->{insertion_mode} = IN_BODY_IM;                  $self->{insertion_mode} = IN_BODY_IM;
# Line 3631  sub _tree_construction_main ($) { Line 4203  sub _tree_construction_main ($) {
4203                } else {                } else {
4204                  die "$0: tag name: $self->{tag_name}";                  die "$0: tag name: $self->{tag_name}";
4205                }                }
4206                  !!!nack ('t127.1');
4207                !!!next-token;                !!!next-token;
4208                redo B;                next B;
4209              } else {              } else {
4210                !!!cp ('t128');                !!!cp ('t128');
4211                #                #
# Line 3642  sub _tree_construction_main ($) { Line 4215  sub _tree_construction_main ($) {
4215                !!!cp ('t129');                !!!cp ('t129');
4216                ## As if </noscript>                ## As if </noscript>
4217                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4218                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);
4219                                
4220                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
4221                ## As if </head>                ## As if </head>
# Line 3661  sub _tree_construction_main ($) { Line 4234  sub _tree_construction_main ($) {
4234    
4235              ## "after head" insertion mode              ## "after head" insertion mode
4236              ## As if <body>              ## As if <body>
4237              !!!insert-element ('body');              !!!insert-element ('body',, $token);
4238              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
4239              ## reprocess              ## reprocess
4240              redo B;              !!!ack-later;
4241                next B;
4242            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
4243              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
4244                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4245                  !!!cp ('t132');                  !!!cp ('t132');
4246                  ## As if <head>                  ## As if <head>
4247                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4248                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4249                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4250                        [$self->{head_element}, $el_category->{head}];
4251    
4252                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4253                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4254                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4255                  !!!next-token;                  !!!next-token;
4256                  redo B;                  next B;
4257                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4258                  !!!cp ('t133');                  !!!cp ('t133');
4259                  ## As if </noscript>                  ## As if </noscript>
4260                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4261                  !!!parse-error (type => 'in noscript:/head');                  !!!parse-error (type => 'in noscript:/head', token => $token);
4262                                    
4263                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4264                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4265                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4266                  !!!next-token;                  !!!next-token;
4267                  redo B;                  next B;
4268                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4269                  !!!cp ('t134');                  !!!cp ('t134');
4270                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4271                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4272                  !!!next-token;                  !!!next-token;
4273                  redo B;                  next B;
4274                } else {                } else {
4275                  !!!cp ('t135');                  !!!cp ('t135');
4276                  #                  #
# Line 3706  sub _tree_construction_main ($) { Line 4281  sub _tree_construction_main ($) {
4281                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4282                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4283                  !!!next-token;                  !!!next-token;
4284                  redo B;                  next B;
4285                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4286                  !!!cp ('t137');                  !!!cp ('t137');
4287                  !!!parse-error (type => 'unmatched end tag:noscript');                  !!!parse-error (type => 'unmatched end tag:noscript', token => $token);
4288                  ## Ignore the token ## ISSUE: An issue in the spec.                  ## Ignore the token ## ISSUE: An issue in the spec.
4289                  !!!next-token;                  !!!next-token;
4290                  redo B;                  next B;
4291                } else {                } else {
4292                  !!!cp ('t138');                  !!!cp ('t138');
4293                  #                  #
# Line 3723  sub _tree_construction_main ($) { Line 4298  sub _tree_construction_main ($) {
4298                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4299                  !!!cp ('t139');                  !!!cp ('t139');
4300                  ## As if <head>                  ## As if <head>
4301                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4302                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4303                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4304                        [$self->{head_element}, $el_category->{head}];
4305    
4306                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4307                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4308                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4309                  !!!cp ('t140');                  !!!cp ('t140');
4310                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4311                  ## Ignore the token                  ## Ignore the token
4312                  !!!next-token;                  !!!next-token;
4313                  redo B;                  next B;
4314                } else {                } else {
4315                  !!!cp ('t141');                  !!!cp ('t141');
4316                }                }
# Line 3746  sub _tree_construction_main ($) { Line 4322  sub _tree_construction_main ($) {
4322                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4323                  !!!cp ('t142');                  !!!cp ('t142');
4324                  ## As if <head>                  ## As if <head>
4325                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4326                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4327                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4328                        [$self->{head_element}, $el_category->{head}];
4329    
4330                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4331                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
# Line 3763  sub _tree_construction_main ($) { Line 4340  sub _tree_construction_main ($) {
4340                  #                  #
4341                } else {                } else {
4342                  !!!cp ('t145');                  !!!cp ('t145');
4343                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4344                  ## Ignore the token                  ## Ignore the token
4345                  !!!next-token;                  !!!next-token;
4346                  redo B;                  next B;
4347                }                }
4348              }              }
4349    
# Line 3774  sub _tree_construction_main ($) { Line 4351  sub _tree_construction_main ($) {
4351                !!!cp ('t146');                !!!cp ('t146');
4352                ## As if </noscript>                ## As if </noscript>
4353                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4354                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);
4355                                
4356                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
4357                ## As if </head>                ## As if </head>
# Line 3790  sub _tree_construction_main ($) { Line 4367  sub _tree_construction_main ($) {
4367              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4368  ## ISSUE: This case cannot be reached?  ## ISSUE: This case cannot be reached?
4369                !!!cp ('t148');                !!!cp ('t148');
4370                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4371                ## Ignore the token ## ISSUE: An issue in the spec.                ## Ignore the token ## ISSUE: An issue in the spec.
4372                !!!next-token;                !!!next-token;
4373                redo B;                next B;
4374              } else {              } else {
4375                !!!cp ('t149');                !!!cp ('t149');
4376              }              }
4377    
4378              ## "after head" insertion mode              ## "after head" insertion mode
4379              ## As if <body>              ## As if <body>
4380              !!!insert-element ('body');              !!!insert-element ('body',, $token);
4381              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
4382              ## reprocess              ## reprocess
4383              redo B;              next B;
4384            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4385              die "$0: $token->{type}: Unknown token type";          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4386            }            !!!cp ('t149.1');
4387    
4388              ## NOTE: As if <head>
4389              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4390              $self->{open_elements}->[-1]->[0]->append_child
4391                  ($self->{head_element});
4392              #push @{$self->{open_elements}},
4393              #    [$self->{head_element}, $el_category->{head}];
4394              #$self->{insertion_mode} = IN_HEAD_IM;
4395              ## NOTE: Reprocess.
4396    
4397              ## NOTE: As if </head>
4398              #pop @{$self->{open_elements}};
4399              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4400              ## NOTE: Reprocess.
4401              
4402              #
4403            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4404              !!!cp ('t149.2');
4405    
4406              ## NOTE: As if </head>
4407              pop @{$self->{open_elements}};
4408              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4409              ## NOTE: Reprocess.
4410    
4411              #
4412            } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4413              !!!cp ('t149.3');
4414    
4415              !!!parse-error (type => 'in noscript:#eof', token => $token);
4416    
4417              ## As if </noscript>
4418              pop @{$self->{open_elements}};
4419              #$self->{insertion_mode} = IN_HEAD_IM;
4420              ## NOTE: Reprocess.
4421    
4422              ## NOTE: As if </head>
4423              pop @{$self->{open_elements}};
4424              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4425              ## NOTE: Reprocess.
4426    
4427              #
4428            } else {
4429              !!!cp ('t149.4');
4430              #
4431            }
4432    
4433            ## NOTE: As if <body>
4434            !!!insert-element ('body',, $token);
4435            $self->{insertion_mode} = IN_BODY_IM;
4436            ## NOTE: Reprocess.
4437            next B;
4438          } else {
4439            die "$0: $token->{type}: Unknown token type";
4440          }
4441    
4442            ## ISSUE: An issue in the spec.            ## ISSUE: An issue in the spec.
4443      } elsif ($self->{insertion_mode} & BODY_IMS) {      } elsif ($self->{insertion_mode} & BODY_IMS) {
# Line 3818  sub _tree_construction_main ($) { Line 4449  sub _tree_construction_main ($) {
4449              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4450    
4451              !!!next-token;              !!!next-token;
4452              redo B;              next B;
4453            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
4454              if ({              if ({
4455                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
# Line 3826  sub _tree_construction_main ($) { Line 4457  sub _tree_construction_main ($) {
4457                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
4458                if ($self->{insertion_mode} == IN_CELL_IM) {                if ($self->{insertion_mode} == IN_CELL_IM) {
4459                  ## have an element in table scope                  ## have an element in table scope
4460                  my $tn;                  for (reverse 0..$#{$self->{open_elements}}) {
                 INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
4461                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4462                    if ($node->[1] eq 'td' or $node->[1] eq 'th') {                    if ($node->[1] & TABLE_CELL_EL) {
4463                      !!!cp ('t151');                      !!!cp ('t151');
4464                      $tn = $node->[1];  
4465                      last INSCOPE;                      ## Close the cell
4466                    } elsif ({                      !!!back-token; # <x>
4467                              table => 1, html => 1,                      $token = {type => END_TAG_TOKEN,
4468                             }->{$node->[1]}) {                                tag_name => $node->[0]->manakai_local_name,
4469                                  line => $token->{line},
4470                                  column => $token->{column}};
4471                        next B;
4472                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4473                      !!!cp ('t152');                      !!!cp ('t152');
4474                      last INSCOPE;                      ## ISSUE: This case can never be reached, maybe.
4475                        last;
4476                    }                    }
4477                  } # INSCOPE                  }
4478                    unless (defined $tn) {  
4479                      !!!cp ('t153');                  !!!cp ('t153');
4480  ## TODO: This error type is wrong.                  !!!parse-error (type => 'start tag not allowed',
4481                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      value => $token->{tag_name}, token => $token);
4482                      ## Ignore the token                  ## Ignore the token
4483                      !!!next-token;                  !!!nack ('t153.1');
4484                      redo B;                  !!!next-token;
4485                    }                  next B;
                   
                 !!!cp ('t154');  
                 ## Close the cell  
                 !!!back-token; # <?>  
                 $token = {type => END_TAG_TOKEN, tag_name => $tn};  
                 redo B;  
4486                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4487                  !!!parse-error (type => 'not closed:caption');                  !!!parse-error (type => 'not closed:caption', token => $token);
4488                                    
4489                  ## As if </caption>                  ## NOTE: As if </caption>.
4490                  ## have a table element in table scope                  ## have a table element in table scope
4491                  my $i;                  my $i;
4492                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
4493                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
4494                    if ($node->[1] eq 'caption') {                      my $node = $self->{open_elements}->[$_];
4495                      !!!cp ('t155');                      if ($node->[1] & CAPTION_EL) {
4496                      $i = $_;                        !!!cp ('t155');
4497                      last INSCOPE;                        $i = $_;
4498                    } elsif ({                        last INSCOPE;
4499                              table => 1, html => 1,                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4500                             }->{$node->[1]}) {                        !!!cp ('t156');
4501                      !!!cp ('t156');                        last;
4502                      last INSCOPE;                      }
4503                    }                    }
4504    
4505                      !!!cp ('t157');
4506                      !!!parse-error (type => 'start tag not allowed',
4507                                      value => $token->{tag_name}, token => $token);
4508                      ## Ignore the token
4509                      !!!nack ('t157.1');
4510                      !!!next-token;
4511                      next B;
4512                  } # INSCOPE                  } # INSCOPE
                   unless (defined $i) {  
                     !!!cp ('t157');  
 ## TODO: this type is wrong.  
                     !!!parse-error (type => 'unmatched end tag:caption');  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
                   }  
4513                                    
4514                  ## generate implied end tags                  ## generate implied end tags
4515                  while ({                  while ($self->{open_elements}->[-1]->[1]
4516                          dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                        }->{$self->{open_elements}->[-1]->[1]}) {  
4517                    !!!cp ('t158');                    !!!cp ('t158');
4518                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4519                  }                  }
4520    
4521                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4522                    !!!cp ('t159');                    !!!cp ('t159');
4523                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed',
4524                                      value => $self->{open_elements}->[-1]->[0]
4525                                          ->manakai_local_name,
4526                                      token => $token);
4527                  } else {                  } else {
4528                    !!!cp ('t160');                    !!!cp ('t160');
4529                  }                  }
# Line 3904  sub _tree_construction_main ($) { Line 4535  sub _tree_construction_main ($) {
4535                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
4536                                    
4537                  ## reprocess                  ## reprocess
4538                  redo B;                  !!!ack-later;
4539                    next B;
4540                } else {                } else {
4541                  !!!cp ('t161');                  !!!cp ('t161');
4542                  #                  #
# Line 3920  sub _tree_construction_main ($) { Line 4552  sub _tree_construction_main ($) {
4552                  my $i;                  my $i;
4553                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4554                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4555                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
4556                      !!!cp ('t163');                      !!!cp ('t163');
4557                      $i = $_;                      $i = $_;
4558                      last INSCOPE;                      last INSCOPE;
4559                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
4560                      !!!cp ('t164');                      !!!cp ('t164');
4561                      last INSCOPE;                      last INSCOPE;
4562                    }                    }
4563                  } # INSCOPE                  } # INSCOPE
4564                    unless (defined $i) {                    unless (defined $i) {
4565                      !!!cp ('t165');                      !!!cp ('t165');
4566                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4567                      ## Ignore the token                      ## Ignore the token
4568                      !!!next-token;                      !!!next-token;
4569                      redo B;                      next B;
4570                    }                    }
4571                                    
4572                  ## generate implied end tags                  ## generate implied end tags
4573                  while ({                  while ($self->{open_elements}->[-1]->[1]
4574                          dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                        }->{$self->{open_elements}->[-1]->[1]}) {  
4575                    !!!cp ('t166');                    !!!cp ('t166');
4576                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4577                  }                  }
4578    
4579                  if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {                  if ($self->{open_elements}->[-1]->[0]->manakai_local_name
4580                            ne $token->{tag_name}) {
4581                    !!!cp ('t167');                    !!!cp ('t167');
4582                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed',
4583                                      value => $self->{open_elements}->[-1]->[0]
4584                                          ->manakai_local_name,
4585                                      token => $token);
4586                  } else {                  } else {
4587                    !!!cp ('t168');                    !!!cp ('t168');
4588                  }                  }
# Line 3961  sub _tree_construction_main ($) { Line 4594  sub _tree_construction_main ($) {
4594                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
4595                                    
4596                  !!!next-token;                  !!!next-token;
4597                  redo B;                  next B;
4598                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4599                  !!!cp ('t169');                  !!!cp ('t169');
4600                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4601                  ## Ignore the token                  ## Ignore the token
4602                  !!!next-token;                  !!!next-token;
4603                  redo B;                  next B;
4604                } else {                } else {
4605                  !!!cp ('t170');                  !!!cp ('t170');
4606                  #                  #
# Line 3976  sub _tree_construction_main ($) { Line 4609  sub _tree_construction_main ($) {
4609                if ($self->{insertion_mode} == IN_CAPTION_IM) {                if ($self->{insertion_mode} == IN_CAPTION_IM) {
4610                  ## have a table element in table scope                  ## have a table element in table scope
4611                  my $i;                  my $i;
4612                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
4613                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
4614                    if ($node->[1] eq $token->{tag_name}) {                      my $node = $self->{open_elements}->[$_];
4615                      !!!cp ('t171');                      if ($node->[1] & CAPTION_EL) {
4616                      $i = $_;                        !!!cp ('t171');
4617                      last INSCOPE;                        $i = $_;
4618                    } elsif ({                        last INSCOPE;
4619                              table => 1, html => 1,                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4620                             }->{$node->[1]}) {                        !!!cp ('t172');
4621                      !!!cp ('t172');                        last;
4622                      last INSCOPE;                      }
4623                    }                    }
4624    
4625                      !!!cp ('t173');
4626                      !!!parse-error (type => 'unmatched end tag',
4627                                      value => $token->{tag_name}, token => $token);
4628                      ## Ignore the token
4629                      !!!next-token;
4630                      next B;
4631                  } # INSCOPE                  } # INSCOPE
                   unless (defined $i) {  
                     !!!cp ('t173');  
                     !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
                   }  
4632                                    
4633                  ## generate implied end tags                  ## generate implied end tags
4634                  while ({                  while ($self->{open_elements}->[-1]->[1]
4635                          dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                        }->{$self->{open_elements}->[-1]->[1]}) {  
4636                    !!!cp ('t174');                    !!!cp ('t174');
4637                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4638                  }                  }
4639                                    
4640                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4641                    !!!cp ('t175');                    !!!cp ('t175');
4642                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed',
4643                                      value => $self->{open_elements}->[-1]->[0]
4644                                          ->manakai_local_name,
4645                                      token => $token);
4646                  } else {                  } else {
4647                    !!!cp ('t176');                    !!!cp ('t176');
4648                  }                  }
# Line 4019  sub _tree_construction_main ($) { Line 4654  sub _tree_construction_main ($) {
4654                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
4655                                    
4656                  !!!next-token;                  !!!next-token;
4657                  redo B;                  next B;
4658                } elsif ($self->{insertion_mode} == IN_CELL_IM) {                } elsif ($self->{insertion_mode} == IN_CELL_IM) {
4659                  !!!cp ('t177');                  !!!cp ('t177');
4660                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4661                  ## Ignore the token                  ## Ignore the token
4662                  !!!next-token;                  !!!next-token;
4663                  redo B;                  next B;
4664                } else {                } else {
4665                  !!!cp ('t178');                  !!!cp ('t178');
4666                  #                  #
# Line 4038  sub _tree_construction_main ($) { Line 4673  sub _tree_construction_main ($) {
4673                ## have an element in table scope                ## have an element in table scope
4674                my $i;                my $i;
4675                my $tn;                my $tn;
4676                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: {
4677                  my $node = $self->{open_elements}->[$_];                  for (reverse 0..$#{$self->{open_elements}}) {
4678                  if ($node->[1] eq $token->{tag_name}) {                    my $node = $self->{open_elements}->[$_];
4679                    !!!cp ('t179');                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
4680                    $i = $_;                      !!!cp ('t179');
4681                    last INSCOPE;                      $i = $_;
4682                  } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {  
4683                    !!!cp ('t180');                      ## Close the cell
4684                    $tn = $node->[1];                      !!!back-token; # </x>
4685                    ## NOTE: There is exactly one |td| or |th| element                      $token = {type => END_TAG_TOKEN, tag_name => $tn,
4686                    ## in scope in the stack of open elements by definition.                                line => $token->{line},
4687                  } elsif ({                                column => $token->{column}};
4688                            table => 1, html => 1,                      next B;
4689                           }->{$node->[1]}) {                    } elsif ($node->[1] & TABLE_CELL_EL) {
4690                    !!!cp ('t181');                      !!!cp ('t180');
4691                    last INSCOPE;                      $tn = $node->[0]->manakai_local_name;
4692                        ## NOTE: There is exactly one |td| or |th| element
4693                        ## in scope in the stack of open elements by definition.
4694                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4695                        ## ISSUE: Can this be reached?
4696                        !!!cp ('t181');
4697                        last;
4698                      }
4699                  }                  }
4700                } # INSCOPE  
               unless (defined $i) {  
4701                  !!!cp ('t182');                  !!!cp ('t182');
4702                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
4703                        value => $token->{tag_name}, token => $token);
4704                  ## Ignore the token                  ## Ignore the token
4705                  !!!next-token;                  !!!next-token;
4706                  redo B;                  next B;
4707                } else {                } # INSCOPE
                 !!!cp ('t183');  
               }  
   
               ## Close the cell  
               !!!back-token; # </?>  
               $token = {type => END_TAG_TOKEN, tag_name => $tn};  
               redo B;  
4708              } elsif ($token->{tag_name} eq 'table' and              } elsif ($token->{tag_name} eq 'table' and
4709                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
4710                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed:caption', token => $token);
4711    
4712                ## As if </caption>                ## As if </caption>
4713                ## have a table element in table scope                ## have a table element in table scope
4714                my $i;                my $i;
4715                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4716                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4717                  if ($node->[1] eq 'caption') {                  if ($node->[1] & CAPTION_EL) {
4718                    !!!cp ('t184');                    !!!cp ('t184');
4719                    $i = $_;                    $i = $_;
4720                    last INSCOPE;                    last INSCOPE;
4721                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
4722                    !!!cp ('t185');                    !!!cp ('t185');
4723                    last INSCOPE;                    last INSCOPE;
4724                  }                  }
4725                } # INSCOPE                } # INSCOPE
4726                unless (defined $i) {                unless (defined $i) {
4727                  !!!cp ('t186');                  !!!cp ('t186');
4728                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!parse-error (type => 'unmatched end tag:caption', token => $token);
4729                  ## Ignore the token                  ## Ignore the token
4730                  !!!next-token;                  !!!next-token;
4731                  redo B;                  next B;
4732                }                }
4733                                
4734                ## generate implied end tags                ## generate implied end tags
4735                while ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                       dd => 1, dt => 1, li => 1, p => 1,  
                      }->{$self->{open_elements}->[-1]->[1]}) {  
4736                  !!!cp ('t187');                  !!!cp ('t187');
4737                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4738                }                }
4739    
4740                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4741                  !!!cp ('t188');                  !!!cp ('t188');
4742                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed',
4743                                    value => $self->{open_elements}->[-1]->[0]
4744                                        ->manakai_local_name,
4745                                    token => $token);
4746                } else {                } else {
4747                  !!!cp ('t189');                  !!!cp ('t189');
4748                }                }
# Line 4120  sub _tree_construction_main ($) { Line 4754  sub _tree_construction_main ($) {
4754                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
4755    
4756                ## reprocess                ## reprocess
4757                redo B;                next B;
4758              } elsif ({              } elsif ({
4759                        body => 1, col => 1, colgroup => 1, html => 1,                        body => 1, col => 1, colgroup => 1, html => 1,
4760                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4761                if ($self->{insertion_mode} & BODY_TABLE_IMS) {                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
4762                  !!!cp ('t190');                  !!!cp ('t190');
4763                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4764                  ## Ignore the token                  ## Ignore the token
4765                  !!!next-token;                  !!!next-token;
4766                  redo B;                  next B;
4767                } else {                } else {
4768                  !!!cp ('t191');                  !!!cp ('t191');
4769                  #                  #
# Line 4140  sub _tree_construction_main ($) { Line 4774  sub _tree_construction_main ($) {
4774                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
4775                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
4776                !!!cp ('t192');                !!!cp ('t192');
4777                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4778                ## Ignore the token                ## Ignore the token
4779                !!!next-token;                !!!next-token;
4780                redo B;                next B;
4781              } else {              } else {
4782                !!!cp ('t193');                !!!cp ('t193');
4783                #                #
4784              }              }
4785          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4786            for my $entry (@{$self->{open_elements}}) {
4787              unless ($entry->[1] & ALL_END_TAG_OPTIONAL_EL) {
4788                !!!cp ('t75');
4789                !!!parse-error (type => 'in body:#eof', token => $token);
4790                last;
4791              }
4792            }
4793    
4794            ## Stop parsing.
4795            last B;
4796        } else {        } else {
4797          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
4798        }        }
# Line 4156  sub _tree_construction_main ($) { Line 4801  sub _tree_construction_main ($) {
4801        #        #
4802      } elsif ($self->{insertion_mode} & TABLE_IMS) {      } elsif ($self->{insertion_mode} & TABLE_IMS) {
4803        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
4804              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if (not $open_tables->[-1]->[1] and # tainted
4805                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);              $token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4806              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4807                                
4808                unless (length $token->{data}) {            unless (length $token->{data}) {
4809                  !!!cp ('t194');              !!!cp ('t194');
4810                  !!!next-token;              !!!next-token;
4811                  redo B;              next B;
4812                } else {            } else {
4813                  !!!cp ('t195');              !!!cp ('t195');
4814                }            }
4815              }          }
4816    
4817              !!!parse-error (type => 'in table:#character');              !!!parse-error (type => 'in table:#character', token => $token);
4818    
4819              ## As if in body, but insert into foster parent element              ## As if in body, but insert into foster parent element
4820              ## ISSUE: Spec says that "whenever a node would be inserted              ## ISSUE: Spec says that "whenever a node would be inserted
# Line 4176  sub _tree_construction_main ($) { Line 4822  sub _tree_construction_main ($) {
4822              ## result in a new Text node.              ## result in a new Text node.
4823              $reconstruct_active_formatting_elements->($insert_to_foster);              $reconstruct_active_formatting_elements->($insert_to_foster);
4824                            
4825              if ({              if ($self->{open_elements}->[-1]->[1] & TABLE_ROWS_EL) {
                  table => 1, tbody => 1, tfoot => 1,  
                  thead => 1, tr => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
4826                # MUST                # MUST
4827                my $foster_parent_element;                my $foster_parent_element;
4828                my $next_sibling;                my $next_sibling;
4829                my $prev_sibling;                my $prev_sibling;
4830                OE: for (reverse 0..$#{$self->{open_elements}}) {                OE: for (reverse 0..$#{$self->{open_elements}}) {
4831                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
4832                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
4833                    if (defined $parent and $parent->node_type == 1) {                    if (defined $parent and $parent->node_type == 1) {
4834                      !!!cp ('t196');                      !!!cp ('t196');
# Line 4213  sub _tree_construction_main ($) { Line 4856  sub _tree_construction_main ($) {
4856                    ($self->{document}->create_text_node ($token->{data}),                    ($self->{document}->create_text_node ($token->{data}),
4857                     $next_sibling);                     $next_sibling);
4858                }                }
4859              } else {            $open_tables->[-1]->[1] = 1; # tainted
4860                !!!cp ('t200');          } else {
4861                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});            !!!cp ('t200');
4862              }            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4863            }
4864                            
4865              !!!next-token;          !!!next-token;
4866              redo B;          next B;
4867        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
4868              if ({              if ({
4869                   tr => ($self->{insertion_mode} != IN_ROW_IM),                   tr => ($self->{insertion_mode} != IN_ROW_IM),
# Line 4227  sub _tree_construction_main ($) { Line 4871  sub _tree_construction_main ($) {
4871                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
4872                if ($self->{insertion_mode} == IN_TABLE_IM) {                if ($self->{insertion_mode} == IN_TABLE_IM) {
4873                  ## Clear back to table context                  ## Clear back to table context
4874                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
4875                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
4876                    !!!cp ('t201');                    !!!cp ('t201');
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
4877                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4878                  }                  }
4879                                    
4880                  !!!insert-element ('tbody');                  !!!insert-element ('tbody',, $token);
4881                  $self->{insertion_mode} = IN_TABLE_BODY_IM;                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
4882                  ## reprocess in the "in table body" insertion mode...                  ## reprocess in the "in table body" insertion mode...
4883                }                }
# Line 4242  sub _tree_construction_main ($) { Line 4885  sub _tree_construction_main ($) {
4885                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
4886                  unless ($token->{tag_name} eq 'tr') {                  unless ($token->{tag_name} eq 'tr') {
4887                    !!!cp ('t202');                    !!!cp ('t202');
4888                    !!!parse-error (type => 'missing start tag:tr');                    !!!parse-error (type => 'missing start tag:tr', token => $token);
4889                  }                  }
4890                                    
4891                  ## Clear back to table body context                  ## Clear back to table body context
4892                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
4893                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
4894                    !!!cp ('t203');                    !!!cp ('t203');
4895                    ## ISSUE: Can this case be reached?                    ## ISSUE: Can this case be reached?
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
4896                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4897                  }                  }
4898                                    
4899                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
4900                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
4901                    !!!cp ('t204');                    !!!cp ('t204');
4902                    !!!insert-element ($token->{tag_name}, $token->{attributes});                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4903                      !!!nack ('t204');
4904                    !!!next-token;                    !!!next-token;
4905                    redo B;                    next B;
4906                  } else {                  } else {
4907                    !!!cp ('t205');                    !!!cp ('t205');
4908                    !!!insert-element ('tr');                    !!!insert-element ('tr',, $token);
4909                    ## reprocess in the "in row" insertion mode                    ## reprocess in the "in row" insertion mode
4910                  }                  }
4911                } else {                } else {
# Line 4271  sub _tree_construction_main ($) { Line 4913  sub _tree_construction_main ($) {
4913                }                }
4914    
4915                ## Clear back to table row context                ## Clear back to table row context
4916                while (not {                while (not ($self->{open_elements}->[-1]->[1]
4917                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
4918                  !!!cp ('t207');                  !!!cp ('t207');
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
4919                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4920                }                }
4921                                
4922                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4923                $self->{insertion_mode} = IN_CELL_IM;                $self->{insertion_mode} = IN_CELL_IM;
4924    
4925                push @$active_formatting_elements, ['#marker', ''];                push @$active_formatting_elements, ['#marker', ''];
4926                                
4927                  !!!nack ('t207.1');
4928                !!!next-token;                !!!next-token;
4929                redo B;                next B;
4930              } elsif ({              } elsif ({
4931                        caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
4932                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
# Line 4297  sub _tree_construction_main ($) { Line 4938  sub _tree_construction_main ($) {
4938                  my $i;                  my $i;
4939                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4940                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4941                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
4942                      !!!cp ('t208');                      !!!cp ('t208');
4943                      $i = $_;                      $i = $_;
4944                      last INSCOPE;                      last INSCOPE;
4945                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             html => 1,  
   
                             ## NOTE: This element does not appear here, maybe.  
                             table => 1,  
                            }->{$node->[1]}) {  
4946                      !!!cp ('t209');                      !!!cp ('t209');
4947                      last INSCOPE;                      last INSCOPE;
4948                    }                    }
4949                  } # INSCOPE                  } # INSCOPE
4950                  unless (defined $i) {                  unless (defined $i) {
4951                   !!!cp ('t210');                    !!!cp ('t210');
4952  ## TODO: This type is wrong.  ## TODO: This type is wrong.
4953                   !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});                    !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name}, token => $token);
4954                    ## Ignore the token                    ## Ignore the token
4955                      !!!nack ('t210.1');
4956                    !!!next-token;                    !!!next-token;
4957                    redo B;                    next B;
4958                  }                  }
4959                                    
4960                  ## Clear back to table row context                  ## Clear back to table row context
4961                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
4962                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
4963                    !!!cp ('t211');                    !!!cp ('t211');
4964                    ## ISSUE: Can this case be reached?                    ## ISSUE: Can this case be reached?
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
4965                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4966                  }                  }
4967                                    
# Line 4335  sub _tree_construction_main ($) { Line 4970  sub _tree_construction_main ($) {
4970                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
4971                    !!!cp ('t212');                    !!!cp ('t212');
4972                    ## reprocess                    ## reprocess
4973                    redo B;                    !!!ack-later;
4974                      next B;
4975                  } else {                  } else {
4976                    !!!cp ('t213');                    !!!cp ('t213');
4977                    ## reprocess in the "in table body" insertion mode...                    ## reprocess in the "in table body" insertion mode...
# Line 4347  sub _tree_construction_main ($) { Line 4983  sub _tree_construction_main ($) {
4983                  my $i;                  my $i;
4984                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4985                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4986                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
                        tbody => 1, thead => 1, tfoot => 1,  
                       }->{$node->[1]}) {  
4987                      !!!cp ('t214');                      !!!cp ('t214');
4988                      $i = $_;                      $i = $_;
4989                      last INSCOPE;                      last INSCOPE;
4990                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
4991                      !!!cp ('t215');                      !!!cp ('t215');
4992                      last INSCOPE;                      last INSCOPE;
4993                    }                    }
# Line 4363  sub _tree_construction_main ($) { Line 4995  sub _tree_construction_main ($) {
4995                  unless (defined $i) {                  unless (defined $i) {
4996                    !!!cp ('t216');                    !!!cp ('t216');
4997  ## TODO: This erorr type ios wrong.  ## TODO: This erorr type ios wrong.
4998                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4999                    ## Ignore the token                    ## Ignore the token
5000                      !!!nack ('t216.1');
5001                    !!!next-token;                    !!!next-token;
5002                    redo B;                    next B;
5003                  }                  }
5004    
5005                  ## Clear back to table body context                  ## Clear back to table body context
5006                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5007                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5008                    !!!cp ('t217');                    !!!cp ('t217');
5009                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5010                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5011                  }                  }
5012                                    
# Line 4395  sub _tree_construction_main ($) { Line 5026  sub _tree_construction_main ($) {
5026    
5027                if ($token->{tag_name} eq 'col') {                if ($token->{tag_name} eq 'col') {
5028                  ## Clear back to table context                  ## Clear back to table context
5029                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5030                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5031                    !!!cp ('t219');                    !!!cp ('t219');
5032                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5033                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5034                  }                  }
5035                                    
5036                  !!!insert-element ('colgroup');                  !!!insert-element ('colgroup',, $token);
5037                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
5038                  ## reprocess                  ## reprocess
5039                  redo B;                  !!!ack-later;
5040                    next B;
5041                } elsif ({                } elsif ({
5042                          caption => 1,                          caption => 1,
5043                          colgroup => 1,                          colgroup => 1,
5044                          tbody => 1, tfoot => 1, thead => 1,                          tbody => 1, tfoot => 1, thead => 1,
5045                         }->{$token->{tag_name}}) {                         }->{$token->{tag_name}}) {
5046                  ## Clear back to table context                  ## Clear back to table context
5047                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5048                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5049                    !!!cp ('t220');                    !!!cp ('t220');
5050                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5051                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5052                  }                  }
5053                                    
5054                  push @$active_formatting_elements, ['#marker', '']                  push @$active_formatting_elements, ['#marker', '']
5055                      if $token->{tag_name} eq 'caption';                      if $token->{tag_name} eq 'caption';
5056                                    
5057                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5058                  $self->{insertion_mode} = {                  $self->{insertion_mode} = {
5059                                             caption => IN_CAPTION_IM,                                             caption => IN_CAPTION_IM,
5060                                             colgroup => IN_COLUMN_GROUP_IM,                                             colgroup => IN_COLUMN_GROUP_IM,
# Line 4433  sub _tree_construction_main ($) { Line 5063  sub _tree_construction_main ($) {
5063                                             thead => IN_TABLE_BODY_IM,                                             thead => IN_TABLE_BODY_IM,
5064                                            }->{$token->{tag_name}};                                            }->{$token->{tag_name}};
5065                  !!!next-token;                  !!!next-token;
5066                  redo B;                  !!!nack ('t220.1');
5067                    next B;
5068                } else {                } else {
5069                  die "$0: in table: <>: $token->{tag_name}";                  die "$0: in table: <>: $token->{tag_name}";
5070                }                }
5071              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5072                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed',
5073                                  value => $self->{open_elements}->[-1]->[0]
5074                                      ->manakai_local_name,
5075                                  token => $token);
5076    
5077                ## As if </table>                ## As if </table>
5078                ## have a table element in table scope                ## have a table element in table scope
5079                my $i;                my $i;
5080                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5081                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5082                  if ($node->[1] eq 'table') {                  if ($node->[1] & TABLE_EL) {
5083                    !!!cp ('t221');                    !!!cp ('t221');
5084                    $i = $_;                    $i = $_;
5085                    last INSCOPE;                    last INSCOPE;
5086                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           #table => 1,  
                           html => 1,  
                          }->{$node->[1]}) {  
5087                    !!!cp ('t222');                    !!!cp ('t222');
5088                    last INSCOPE;                    last INSCOPE;
5089                  }                  }
# Line 4460  sub _tree_construction_main ($) { Line 5091  sub _tree_construction_main ($) {
5091                unless (defined $i) {                unless (defined $i) {
5092                  !!!cp ('t223');                  !!!cp ('t223');
5093  ## TODO: The following is wrong, maybe.  ## TODO: The following is wrong, maybe.
5094                  !!!parse-error (type => 'unmatched end tag:table');                  !!!parse-error (type => 'unmatched end tag:table', token => $token);
5095                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
5096                    !!!nack ('t223.1');
5097                  !!!next-token;                  !!!next-token;
5098                  redo B;                  next B;
5099                }                }
5100                                
5101    ## TODO: Followings are removed from the latest spec.
5102                ## generate implied end tags                ## generate implied end tags
5103                while ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                       dd => 1, dt => 1, li => 1, p => 1,  
                      }->{$self->{open_elements}->[-1]->[1]}) {  
5104                  !!!cp ('t224');                  !!!cp ('t224');
5105                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5106                }                }
5107    
5108                if ($self->{open_elements}->[-1]->[1] ne 'table') {                unless ($self->{open_elements}->[-1]->[1] & TABLE_EL) {
5109                  !!!cp ('t225');                  !!!cp ('t225');
5110  ## ISSUE: Can this case be reached?                  ## NOTE: |<table><tr><table>|
5111                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed',
5112                                    value => $self->{open_elements}->[-1]->[0]
5113                                        ->manakai_local_name,
5114                                    token => $token);
5115                } else {                } else {
5116                  !!!cp ('t226');                  !!!cp ('t226');
5117                }                }
5118    
5119                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5120                  pop @{$open_tables};
5121    
5122                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5123    
5124                ## reprocess            ## reprocess
5125                redo B;            !!!ack-later;
5126              next B;
5127            } elsif ($token->{tag_name} eq 'style') {
5128              if (not $open_tables->[-1]->[1]) { # tainted
5129                !!!cp ('t227.8');
5130                ## NOTE: This is a "as if in head" code clone.
5131                $parse_rcdata->(CDATA_CONTENT_MODEL);
5132                next B;
5133              } else {
5134                !!!cp ('t227.7');
5135                #
5136              }
5137            } elsif ($token->{tag_name} eq 'script') {
5138              if (not $open_tables->[-1]->[1]) { # tainted
5139                !!!cp ('t227.6');
5140                ## NOTE: This is a "as if in head" code clone.
5141                $script_start_tag->();
5142                next B;
5143              } else {
5144                !!!cp ('t227.5');
5145                #
5146              }
5147            } elsif ($token->{tag_name} eq 'input') {
5148              if (not $open_tables->[-1]->[1]) { # tainted
5149                if ($token->{attributes}->{type}) { ## TODO: case
5150                  my $type = lc $token->{attributes}->{type}->{value};
5151                  if ($type eq 'hidden') {
5152                    !!!cp ('t227.3');
5153                    !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);
5154    
5155                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5156    
5157                    ## TODO: form element pointer
5158    
5159                    pop @{$self->{open_elements}};
5160    
5161                    !!!next-token;
5162                    !!!ack ('t227.2.1');
5163                    next B;
5164                  } else {
5165                    !!!cp ('t227.2');
5166                    #
5167                  }
5168                } else {
5169                  !!!cp ('t227.1');
5170                  #
5171                }
5172              } else {
5173                !!!cp ('t227.4');
5174                #
5175              }
5176          } else {          } else {
5177            !!!cp ('t227');            !!!cp ('t227');
           !!!parse-error (type => 'in table:'.$token->{tag_name});  
   
           $insert = $insert_to_foster;  
5178            #            #
5179          }          }
5180    
5181            !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);
5182    
5183            $insert = $insert_to_foster;
5184            #
5185        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
5186              if ($token->{tag_name} eq 'tr' and              if ($token->{tag_name} eq 'tr' and
5187                  $self->{insertion_mode} == IN_ROW_IM) {                  $self->{insertion_mode} == IN_ROW_IM) {
# Line 4502  sub _tree_construction_main ($) { Line 5189  sub _tree_construction_main ($) {
5189                my $i;                my $i;
5190                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5191                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5192                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_ROW_EL) {
5193                    !!!cp ('t228');                    !!!cp ('t228');
5194                    $i = $_;                    $i = $_;
5195                    last INSCOPE;                    last INSCOPE;
5196                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5197                    !!!cp ('t229');                    !!!cp ('t229');
5198                    last INSCOPE;                    last INSCOPE;
5199                  }                  }
5200                } # INSCOPE                } # INSCOPE
5201                unless (defined $i) {                unless (defined $i) {
5202                  !!!cp ('t230');                  !!!cp ('t230');
5203                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5204                  ## Ignore the token                  ## Ignore the token
5205                    !!!nack ('t230.1');
5206                  !!!next-token;                  !!!next-token;
5207                  redo B;                  next B;
5208                } else {                } else {
5209                  !!!cp ('t232');                  !!!cp ('t232');
5210                }                }
5211    
5212                ## Clear back to table row context                ## Clear back to table row context
5213                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5214                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
5215                  !!!cp ('t231');                  !!!cp ('t231');
5216  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5217                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5218                }                }
5219    
5220                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}}; # tr
5221                $self->{insertion_mode} = IN_TABLE_BODY_IM;                $self->{insertion_mode} = IN_TABLE_BODY_IM;
5222                !!!next-token;                !!!next-token;
5223                redo B;                !!!nack ('t231.1');
5224                  next B;
5225              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5226                if ($self->{insertion_mode} == IN_ROW_IM) {                if ($self->{insertion_mode} == IN_ROW_IM) {
5227                  ## As if </tr>                  ## As if </tr>
# Line 4544  sub _tree_construction_main ($) { Line 5229  sub _tree_construction_main ($) {
5229                  my $i;                  my $i;
5230                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5231                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5232                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5233                      !!!cp ('t233');                      !!!cp ('t233');
5234                      $i = $_;                      $i = $_;
5235                      last INSCOPE;                      last INSCOPE;
5236                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5237                      !!!cp ('t234');                      !!!cp ('t234');
5238                      last INSCOPE;                      last INSCOPE;
5239                    }                    }
# Line 4558  sub _tree_construction_main ($) { Line 5241  sub _tree_construction_main ($) {
5241                  unless (defined $i) {                  unless (defined $i) {
5242                    !!!cp ('t235');                    !!!cp ('t235');
5243  ## TODO: The following is wrong.  ## TODO: The following is wrong.
5244                    !!!parse-error (type => 'unmatched end tag:'.$token->{type});                    !!!parse-error (type => 'unmatched end tag:'.$token->{type}, token => $token);
5245                    ## Ignore the token                    ## Ignore the token
5246                      !!!nack ('t236.1');
5247                    !!!next-token;                    !!!next-token;
5248                    redo B;                    next B;
5249                  }                  }
5250                                    
5251                  ## Clear back to table row context                  ## Clear back to table row context
5252                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5253                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5254                    !!!cp ('t236');                    !!!cp ('t236');
5255  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5256                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5257                  }                  }
5258                                    
# Line 4584  sub _tree_construction_main ($) { Line 5266  sub _tree_construction_main ($) {
5266                  my $i;                  my $i;
5267                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5268                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5269                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
                        tbody => 1, thead => 1, tfoot => 1,  
                       }->{$node->[1]}) {  
5270                      !!!cp ('t237');                      !!!cp ('t237');
5271                      $i = $_;                      $i = $_;
5272                      last INSCOPE;                      last INSCOPE;
5273                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5274                      !!!cp ('t238');                      !!!cp ('t238');
5275                      last INSCOPE;                      last INSCOPE;
5276                    }                    }
5277                  } # INSCOPE                  } # INSCOPE
5278                  unless (defined $i) {                  unless (defined $i) {
5279                    !!!cp ('t239');                    !!!cp ('t239');
5280                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5281                    ## Ignore the token                    ## Ignore the token
5282                      !!!nack ('t239.1');
5283                    !!!next-token;                    !!!next-token;
5284                    redo B;                    next B;
5285                  }                  }
5286                                    
5287                  ## Clear back to table body context                  ## Clear back to table body context
5288                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5289                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5290                    !!!cp ('t240');                    !!!cp ('t240');
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5291                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5292                  }                  }
5293                                    
# Line 4626  sub _tree_construction_main ($) { Line 5303  sub _tree_construction_main ($) {
5303                  ## reprocess in the "in table" insertion mode...                  ## reprocess in the "in table" insertion mode...
5304                }                }
5305    
5306                  ## NOTE: </table> in the "in table" insertion mode.
5307                  ## When you edit the code fragment below, please ensure that
5308                  ## the code for <table> in the "in table" insertion mode
5309                  ## is synced with it.
5310    
5311                ## have a table element in table scope                ## have a table element in table scope
5312                my $i;                my $i;
5313                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5314                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5315                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_EL) {
5316                    !!!cp ('t241');                    !!!cp ('t241');
5317                    $i = $_;                    $i = $_;
5318                    last INSCOPE;                    last INSCOPE;
5319                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5320                    !!!cp ('t242');                    !!!cp ('t242');
5321                    last INSCOPE;                    last INSCOPE;
5322                  }                  }
5323                } # INSCOPE                } # INSCOPE
5324                unless (defined $i) {                unless (defined $i) {
5325                  !!!cp ('t243');                  !!!cp ('t243');
5326                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5327                  ## Ignore the token                  ## Ignore the token
5328                    !!!nack ('t243.1');
5329                  !!!next-token;                  !!!next-token;
5330                  redo B;                  next B;
               }  
   
               ## generate implied end tags  
               while ({  
                       dd => 1, dt => 1, li => 1, p => 1,  
                      }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!cp ('t244');  
 ## ISSUE: Can this case be reached?  
                 pop @{$self->{open_elements}};  
               }  
                 
               if ($self->{open_elements}->[-1]->[1] ne 'table') {  
                 !!!cp ('t245');  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               } else {  
                 !!!cp ('t246');  
5331                }                }
5332                                    
5333                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5334                  pop @{$open_tables};
5335                                
5336                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5337                                
5338                !!!next-token;                !!!next-token;
5339                redo B;                next B;
5340              } elsif ({              } elsif ({
5341                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
5342                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
# Line 4680  sub _tree_construction_main ($) { Line 5346  sub _tree_construction_main ($) {
5346                  my $i;                  my $i;
5347                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5348                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5349                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5350                      !!!cp ('t247');                      !!!cp ('t247');
5351                      $i = $_;                      $i = $_;
5352                      last INSCOPE;                      last INSCOPE;
5353                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5354                      !!!cp ('t248');                      !!!cp ('t248');
5355                      last INSCOPE;                      last INSCOPE;
5356                    }                    }
5357                  } # INSCOPE                  } # INSCOPE
5358                    unless (defined $i) {                    unless (defined $i) {
5359                      !!!cp ('t249');                      !!!cp ('t249');
5360                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5361                      ## Ignore the token                      ## Ignore the token
5362                        !!!nack ('t249.1');
5363                      !!!next-token;                      !!!next-token;
5364                      redo B;                      next B;
5365                    }                    }
5366                                    
5367                  ## As if </tr>                  ## As if </tr>
# Line 4704  sub _tree_construction_main ($) { Line 5369  sub _tree_construction_main ($) {
5369                  my $i;                  my $i;
5370                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5371                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5372                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5373                      !!!cp ('t250');                      !!!cp ('t250');
5374                      $i = $_;                      $i = $_;
5375                      last INSCOPE;                      last INSCOPE;
5376                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5377                      !!!cp ('t251');                      !!!cp ('t251');
5378                      last INSCOPE;                      last INSCOPE;
5379                    }                    }
5380                  } # INSCOPE                  } # INSCOPE
5381                    unless (defined $i) {                    unless (defined $i) {
5382                      !!!cp ('t252');                      !!!cp ('t252');
5383                      !!!parse-error (type => 'unmatched end tag:tr');                      !!!parse-error (type => 'unmatched end tag:tr', token => $token);
5384                      ## Ignore the token                      ## Ignore the token
5385                        !!!nack ('t252.1');
5386                      !!!next-token;                      !!!next-token;
5387                      redo B;                      next B;
5388                    }                    }
5389                                    
5390                  ## Clear back to table row context                  ## Clear back to table row context
5391                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5392                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5393                    !!!cp ('t253');                    !!!cp ('t253');
5394  ## ISSUE: Can this case be reached?  ## ISSUE: Can this case be reached?
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5395                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5396                  }                  }
5397                                    
# Line 4742  sub _tree_construction_main ($) { Line 5404  sub _tree_construction_main ($) {
5404                my $i;                my $i;
5405                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5406                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5407                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5408                    !!!cp ('t254');                    !!!cp ('t254');
5409                    $i = $_;                    $i = $_;
5410                    last INSCOPE;                    last INSCOPE;
5411                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5412                    !!!cp ('t255');                    !!!cp ('t255');
5413                    last INSCOPE;                    last INSCOPE;
5414                  }                  }
5415                } # INSCOPE                } # INSCOPE
5416                unless (defined $i) {                unless (defined $i) {
5417                  !!!cp ('t256');                  !!!cp ('t256');
5418                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5419                  ## Ignore the token                  ## Ignore the token
5420                    !!!nack ('t256.1');
5421                  !!!next-token;                  !!!next-token;
5422                  redo B;                  next B;
5423                }                }
5424    
5425                ## Clear back to table body context                ## Clear back to table body context
5426                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5427                  tbody => 1, tfoot => 1, thead => 1, html => 1,                                & TABLE_ROWS_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
5428                  !!!cp ('t257');                  !!!cp ('t257');
5429  ## ISSUE: Can this case be reached?  ## ISSUE: Can this case be reached?
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5430                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5431                }                }
5432    
5433                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5434                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
5435                  !!!nack ('t257.1');
5436                !!!next-token;                !!!next-token;
5437                redo B;                next B;
5438              } elsif ({              } elsif ({
5439                        body => 1, caption => 1, col => 1, colgroup => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
5440                        html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
5441                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM
5442                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
5443                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5444                !!!cp ('t258');            !!!cp ('t258');
5445                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5446                ## Ignore the token            ## Ignore the token
5447                !!!next-token;            !!!nack ('t258.1');
5448                redo B;             !!!next-token;
5449              next B;
5450          } else {          } else {
5451            !!!cp ('t259');            !!!cp ('t259');
5452            !!!parse-error (type => 'in table:/'.$token->{tag_name});            !!!parse-error (type => 'in table:/'.$token->{tag_name}, token => $token);
5453    
5454            $insert = $insert_to_foster;            $insert = $insert_to_foster;
5455            #            #
5456          }          }
5457          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5458            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
5459                    @{$self->{open_elements}} == 1) { # redundant, maybe
5460              !!!parse-error (type => 'in body:#eof', token => $token);
5461              !!!cp ('t259.1');
5462              #
5463            } else {
5464              !!!cp ('t259.2');
5465              #
5466            }
5467    
5468            ## Stop parsing
5469            last B;
5470        } else {        } else {
5471          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5472        }        }
# Line 4803  sub _tree_construction_main ($) { Line 5477  sub _tree_construction_main ($) {
5477                unless (length $token->{data}) {                unless (length $token->{data}) {
5478                  !!!cp ('t260');                  !!!cp ('t260');
5479                  !!!next-token;                  !!!next-token;
5480                  redo B;                  next B;
5481                }                }
5482              }              }
5483                            
# Line 4812  sub _tree_construction_main ($) { Line 5486  sub _tree_construction_main ($) {
5486            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
5487              if ($token->{tag_name} eq 'col') {              if ($token->{tag_name} eq 'col') {
5488                !!!cp ('t262');                !!!cp ('t262');
5489                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5490                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5491                  !!!ack ('t262.1');
5492                !!!next-token;                !!!next-token;
5493                redo B;                next B;
5494              } else {              } else {
5495                !!!cp ('t263');                !!!cp ('t263');
5496                #                #
5497              }              }
5498            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
5499              if ($token->{tag_name} eq 'colgroup') {              if ($token->{tag_name} eq 'colgroup') {
5500                if ($self->{open_elements}->[-1]->[1] eq 'html') {                if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5501                  !!!cp ('t264');                  !!!cp ('t264');
5502                  !!!parse-error (type => 'unmatched end tag:colgroup');                  !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);
5503                  ## Ignore the token                  ## Ignore the token
5504                  !!!next-token;                  !!!next-token;
5505                  redo B;                  next B;
5506                } else {                } else {
5507                  !!!cp ('t265');                  !!!cp ('t265');
5508                  pop @{$self->{open_elements}}; # colgroup                  pop @{$self->{open_elements}}; # colgroup
5509                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
5510                  !!!next-token;                  !!!next-token;
5511                  redo B;                              next B;            
5512                }                }
5513              } elsif ($token->{tag_name} eq 'col') {              } elsif ($token->{tag_name} eq 'col') {
5514                !!!cp ('t266');                !!!cp ('t266');
5515                !!!parse-error (type => 'unmatched end tag:col');                !!!parse-error (type => 'unmatched end tag:col', token => $token);
5516                ## Ignore the token                ## Ignore the token
5517                !!!next-token;                !!!next-token;
5518                redo B;                next B;
5519              } else {              } else {
5520                !!!cp ('t267');                !!!cp ('t267');
5521                #                #
5522              }              }
5523            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5524              die "$0: $token->{type}: Unknown token type";          if ($self->{open_elements}->[-1]->[1] & HTML_EL and
5525            }              @{$self->{open_elements}} == 1) { # redundant, maybe
5526              !!!cp ('t270.2');
5527              ## Stop parsing.
5528              last B;
5529            } else {
5530              ## NOTE: As if </colgroup>.
5531              !!!cp ('t270.1');
5532              pop @{$self->{open_elements}}; # colgroup
5533              $self->{insertion_mode} = IN_TABLE_IM;
5534              ## Reprocess.
5535              next B;
5536            }
5537          } else {
5538            die "$0: $token->{type}: Unknown token type";
5539          }
5540    
5541            ## As if </colgroup>            ## As if </colgroup>
5542            if ($self->{open_elements}->[-1]->[1] eq 'html') {            if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5543              !!!cp ('t269');              !!!cp ('t269');
5544              !!!parse-error (type => 'unmatched end tag:colgroup');  ## TODO: Wrong error type?
5545                !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);
5546              ## Ignore the token              ## Ignore the token
5547                !!!nack ('t269.1');
5548              !!!next-token;              !!!next-token;
5549              redo B;              next B;
5550            } else {            } else {
5551              !!!cp ('t270');              !!!cp ('t270');
5552              pop @{$self->{open_elements}}; # colgroup              pop @{$self->{open_elements}}; # colgroup
5553              $self->{insertion_mode} = IN_TABLE_IM;              $self->{insertion_mode} = IN_TABLE_IM;
5554                !!!ack-later;
5555              ## reprocess              ## reprocess
5556              redo B;              next B;
5557            }            }
5558      } elsif ($self->{insertion_mode} == IN_SELECT_IM) {      } elsif ($self->{insertion_mode} & SELECT_IMS) {
5559        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
5560          !!!cp ('t271');          !!!cp ('t271');
5561          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5562          !!!next-token;          !!!next-token;
5563          redo B;          next B;
5564        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5565              if ($token->{tag_name} eq 'option') {          if ($token->{tag_name} eq 'option') {
5566                if ($self->{open_elements}->[-1]->[1] eq 'option') {            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5567                  !!!cp ('t272');              !!!cp ('t272');
5568                  ## As if </option>              ## As if </option>
5569                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
5570                } else {            } else {
5571                  !!!cp ('t273');              !!!cp ('t273');
5572                }            }
5573    
5574                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5575                !!!next-token;            !!!nack ('t273.1');
5576                redo B;            !!!next-token;
5577              } elsif ($token->{tag_name} eq 'optgroup') {            next B;
5578                if ($self->{open_elements}->[-1]->[1] eq 'option') {          } elsif ($token->{tag_name} eq 'optgroup') {
5579                  !!!cp ('t274');            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5580                  ## As if </option>              !!!cp ('t274');
5581                  pop @{$self->{open_elements}};              ## As if </option>
5582                } else {              pop @{$self->{open_elements}};
5583                  !!!cp ('t275');            } else {
5584                }              !!!cp ('t275');
5585              }
5586    
5587                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            if ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
5588                  !!!cp ('t276');              !!!cp ('t276');
5589                  ## As if </optgroup>              ## As if </optgroup>
5590                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
5591                } else {            } else {
5592                  !!!cp ('t277');              !!!cp ('t277');
5593                }            }
5594    
5595                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5596                !!!next-token;            !!!nack ('t277.1');
5597                redo B;            !!!next-token;
5598              } elsif ($token->{tag_name} eq 'select') {            next B;
5599  ## TODO: The type below is not good - <select> is replaced by </select>          } elsif ($token->{tag_name} eq 'select' or
5600                !!!parse-error (type => 'not closed:select');                   $token->{tag_name} eq 'input' or
5601                ## As if </select> instead                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
5602                ## have an element in table scope                    {
5603                my $i;                     caption => 1, table => 1,
5604                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                     tbody => 1, tfoot => 1, thead => 1,
5605                  my $node = $self->{open_elements}->[$_];                     tr => 1, td => 1, th => 1,
5606                  if ($node->[1] eq $token->{tag_name}) {                    }->{$token->{tag_name}})) {
5607                    !!!cp ('t278');            ## TODO: The type below is not good - <select> is replaced by </select>
5608                    $i = $_;            !!!parse-error (type => 'not closed:select', token => $token);
5609                    last INSCOPE;            ## NOTE: As if the token were </select> (<select> case) or
5610                  } elsif ({            ## as if there were </select> (otherwise).
5611                            table => 1, html => 1,            ## have an element in table scope
5612                           }->{$node->[1]}) {            my $i;
5613                    !!!cp ('t279');            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5614                    last INSCOPE;              my $node = $self->{open_elements}->[$_];
5615                  }              if ($node->[1] & SELECT_EL) {
5616                } # INSCOPE                !!!cp ('t278');
5617                unless (defined $i) {                $i = $_;
5618                  !!!cp ('t280');                last INSCOPE;
5619                  !!!parse-error (type => 'unmatched end tag:select');              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5620                  ## Ignore the token                !!!cp ('t279');
5621                  !!!next-token;                last INSCOPE;
5622                  redo B;              }
5623                }            } # INSCOPE
5624              unless (defined $i) {
5625                !!!cp ('t280');
5626                !!!parse-error (type => 'unmatched end tag:select', token => $token);
5627                ## Ignore the token
5628                !!!nack ('t280.1');
5629                !!!next-token;
5630                next B;
5631              }
5632                                
5633                !!!cp ('t281');            !!!cp ('t281');
5634                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
5635    
5636                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
5637    
5638                !!!next-token;            if ($token->{tag_name} eq 'select') {
5639                redo B;              !!!nack ('t281.2');
5640                !!!next-token;
5641                next B;
5642              } else {
5643                !!!cp ('t281.1');
5644                !!!ack-later;
5645                ## Reprocess the token.
5646                next B;
5647              }
5648          } else {          } else {
5649            !!!cp ('t282');            !!!cp ('t282');
5650            !!!parse-error (type => 'in select:'.$token->{tag_name});            !!!parse-error (type => 'in select:'.$token->{tag_name}, token => $token);
5651            ## Ignore the token            ## Ignore the token
5652              !!!nack ('t282.1');
5653            !!!next-token;            !!!next-token;
5654            redo B;            next B;
5655          }          }
5656        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
5657              if ($token->{tag_name} eq 'optgroup') {          if ($token->{tag_name} eq 'optgroup') {
5658                if ($self->{open_elements}->[-1]->[1] eq 'option' and            if ($self->{open_elements}->[-1]->[1] & OPTION_EL and
5659                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {                $self->{open_elements}->[-2]->[1] & OPTGROUP_EL) {
5660                  !!!cp ('t283');              !!!cp ('t283');
5661                  ## As if </option>              ## As if </option>
5662                  splice @{$self->{open_elements}}, -2;              splice @{$self->{open_elements}}, -2;
5663                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            } elsif ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
5664                  !!!cp ('t284');              !!!cp ('t284');
5665                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
5666                } else {            } else {
5667                  !!!cp ('t285');              !!!cp ('t285');
5668                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5669                  ## Ignore the token              ## Ignore the token
5670                }            }
5671                !!!next-token;            !!!nack ('t285.1');
5672                redo B;            !!!next-token;
5673              } elsif ($token->{tag_name} eq 'option') {            next B;
5674                if ($self->{open_elements}->[-1]->[1] eq 'option') {          } elsif ($token->{tag_name} eq 'option') {
5675                  !!!cp ('t286');            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5676                  pop @{$self->{open_elements}};              !!!cp ('t286');
5677                } else {              pop @{$self->{open_elements}};
5678                  !!!cp ('t287');            } else {
5679                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t287');
5680                  ## Ignore the token              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5681                }              ## Ignore the token
5682                !!!next-token;            }
5683                redo B;            !!!nack ('t287.1');
5684              } elsif ($token->{tag_name} eq 'select') {            !!!next-token;
5685                ## have an element in table scope            next B;
5686                my $i;          } elsif ($token->{tag_name} eq 'select') {
5687                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            ## have an element in table scope
5688                  my $node = $self->{open_elements}->[$_];            my $i;
5689                  if ($node->[1] eq $token->{tag_name}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5690                    !!!cp ('t288');              my $node = $self->{open_elements}->[$_];
5691                    $i = $_;              if ($node->[1] & SELECT_EL) {
5692                    last INSCOPE;                !!!cp ('t288');
5693                  } elsif ({                $i = $_;
5694                            table => 1, html => 1,                last INSCOPE;
5695                           }->{$node->[1]}) {              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5696                    !!!cp ('t289');                !!!cp ('t289');
5697                    last INSCOPE;                last INSCOPE;
5698                  }              }
5699                } # INSCOPE            } # INSCOPE
5700                unless (defined $i) {            unless (defined $i) {
5701                  !!!cp ('t290');              !!!cp ('t290');
5702                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5703                  ## Ignore the token              ## Ignore the token
5704                  !!!next-token;              !!!nack ('t290.1');
5705                  redo B;              !!!next-token;
5706                }              next B;
5707              }
5708                                
5709                !!!cp ('t291');            !!!cp ('t291');
5710                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
5711    
5712                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
5713    
5714                !!!next-token;            !!!nack ('t291.1');
5715                redo B;            !!!next-token;
5716              } elsif ({            next B;
5717                        caption => 1, table => 1, tbody => 1,          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
5718                        tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                   {
5719                       }->{$token->{tag_name}}) {                    caption => 1, table => 1, tbody => 1,
5720                      tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
5721                     }->{$token->{tag_name}}) {
5722  ## TODO: The following is wrong?  ## TODO: The following is wrong?
5723                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5724                                
5725                ## have an element in table scope            ## have an element in table scope
5726                my $i;            my $i;
5727                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5728                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
5729                  if ($node->[1] eq $token->{tag_name}) {              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5730                    !!!cp ('t292');                !!!cp ('t292');
5731                    $i = $_;                $i = $_;
5732                    last INSCOPE;                last INSCOPE;
5733                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5734                            table => 1, html => 1,                !!!cp ('t293');
5735                           }->{$node->[1]}) {                last INSCOPE;
5736                    !!!cp ('t293');              }
5737                    last INSCOPE;            } # INSCOPE
5738                  }            unless (defined $i) {
5739                } # INSCOPE              !!!cp ('t294');
5740                unless (defined $i) {              ## Ignore the token
5741                  !!!cp ('t294');              !!!nack ('t294.1');
5742                  ## Ignore the token              !!!next-token;
5743                  !!!next-token;              next B;
5744                  redo B;            }
               }  
5745                                
5746                ## As if </select>            ## As if </select>
5747                ## have an element in table scope            ## have an element in table scope
5748                undef $i;            undef $i;
5749                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5750                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
5751                  if ($node->[1] eq 'select') {              if ($node->[1] & SELECT_EL) {
5752                    !!!cp ('t295');                !!!cp ('t295');
5753                    $i = $_;                $i = $_;
5754                    last INSCOPE;                last INSCOPE;
5755                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5756  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
5757                    !!!cp ('t296');                !!!cp ('t296');
5758                    last INSCOPE;                last INSCOPE;
5759                  }              }
5760                } # INSCOPE            } # INSCOPE
5761                unless (defined $i) {            unless (defined $i) {
5762                  !!!cp ('t297');              !!!cp ('t297');
5763  ## TODO: The following error type is correct?  ## TODO: The following error type is correct?
5764                  !!!parse-error (type => 'unmatched end tag:select');              !!!parse-error (type => 'unmatched end tag:select', token => $token);
5765                  ## Ignore the </select> token              ## Ignore the </select> token
5766                  !!!next-token; ## TODO: ok?              !!!nack ('t297.1');
5767                  redo B;              !!!next-token; ## TODO: ok?
5768                }              next B;
5769              }
5770                                
5771                !!!cp ('t298');            !!!cp ('t298');
5772                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
5773    
5774                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
5775    
5776                ## reprocess            !!!ack-later;
5777                redo B;            ## reprocess
5778              next B;
5779          } else {          } else {
5780            !!!cp ('t299');            !!!cp ('t299');
5781            !!!parse-error (type => 'in select:/'.$token->{tag_name});            !!!parse-error (type => 'in select:/'.$token->{tag_name}, token => $token);
5782            ## Ignore the token            ## Ignore the token
5783              !!!nack ('t299.3');
5784            !!!next-token;            !!!next-token;
5785            redo B;            next B;
5786          }          }
5787          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5788            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
5789                    @{$self->{open_elements}} == 1) { # redundant, maybe
5790              !!!cp ('t299.1');
5791              !!!parse-error (type => 'in body:#eof', token => $token);
5792            } else {
5793              !!!cp ('t299.2');
5794            }
5795    
5796            ## Stop parsing.
5797            last B;
5798        } else {        } else {
5799          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5800        }        }
# Line 5086  sub _tree_construction_main ($) { Line 5810  sub _tree_construction_main ($) {
5810            unless (length $token->{data}) {            unless (length $token->{data}) {
5811              !!!cp ('t300');              !!!cp ('t300');
5812              !!!next-token;              !!!next-token;
5813              redo B;              next B;
5814            }            }
5815          }          }
5816                    
5817          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
5818            !!!cp ('t301');            !!!cp ('t301');
5819            !!!parse-error (type => 'after html:#character');            !!!parse-error (type => 'after html:#character', token => $token);
5820    
5821            ## Reprocess in the "after body" insertion mode.            ## Reprocess in the "after body" insertion mode.
5822          } else {          } else {
# Line 5100  sub _tree_construction_main ($) { Line 5824  sub _tree_construction_main ($) {
5824          }          }
5825                    
5826          ## "after body" insertion mode          ## "after body" insertion mode
5827          !!!parse-error (type => 'after body:#character');          !!!parse-error (type => 'after body:#character', token => $token);
5828    
5829          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
5830          ## reprocess          ## reprocess
5831          redo B;          next B;
5832        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5833          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
5834            !!!cp ('t303');            !!!cp ('t303');
5835            !!!parse-error (type => 'after html:'.$token->{tag_name});            !!!parse-error (type => 'after html:'.$token->{tag_name}, token => $token);
5836                        
5837            ## Reprocess in the "after body" insertion mode.            ## Reprocess in the "after body" insertion mode.
5838          } else {          } else {
# Line 5116  sub _tree_construction_main ($) { Line 5840  sub _tree_construction_main ($) {
5840          }          }
5841    
5842          ## "after body" insertion mode          ## "after body" insertion mode
5843          !!!parse-error (type => 'after body:'.$token->{tag_name});          !!!parse-error (type => 'after body:'.$token->{tag_name}, token => $token);
5844    
5845          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
5846            !!!ack-later;
5847          ## reprocess          ## reprocess
5848          redo B;          next B;
5849        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
5850          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
5851            !!!cp ('t305');            !!!cp ('t305');
5852            !!!parse-error (type => 'after html:/'.$token->{tag_name});            !!!parse-error (type => 'after html:/'.$token->{tag_name}, token => $token);
5853                        
5854            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
5855            ## Reprocess in the "after body" insertion mode.            ## Reprocess in the "after body" insertion mode.
# Line 5136  sub _tree_construction_main ($) { Line 5861  sub _tree_construction_main ($) {
5861          if ($token->{tag_name} eq 'html') {          if ($token->{tag_name} eq 'html') {
5862            if (defined $self->{inner_html_node}) {            if (defined $self->{inner_html_node}) {
5863              !!!cp ('t307');              !!!cp ('t307');
5864              !!!parse-error (type => 'unmatched end tag:html');              !!!parse-error (type => 'unmatched end tag:html', token => $token);
5865              ## Ignore the token              ## Ignore the token
5866              !!!next-token;              !!!next-token;
5867              redo B;              next B;
5868            } else {            } else {
5869              !!!cp ('t308');              !!!cp ('t308');
5870              $self->{insertion_mode} = AFTER_HTML_BODY_IM;              $self->{insertion_mode} = AFTER_HTML_BODY_IM;
5871              !!!next-token;              !!!next-token;
5872              redo B;              next B;
5873            }            }
5874          } else {          } else {
5875            !!!cp ('t309');            !!!cp ('t309');
5876            !!!parse-error (type => 'after body:/'.$token->{tag_name});            !!!parse-error (type => 'after body:/'.$token->{tag_name}, token => $token);
5877    
5878            $self->{insertion_mode} = IN_BODY_IM;            $self->{insertion_mode} = IN_BODY_IM;
5879            ## reprocess            ## reprocess
5880            redo B;            next B;
5881          }          }
5882          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5883            !!!cp ('t309.2');
5884            ## Stop parsing
5885            last B;
5886        } else {        } else {
5887          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5888        }        }
# Line 5165  sub _tree_construction_main ($) { Line 5894  sub _tree_construction_main ($) {
5894            unless (length $token->{data}) {            unless (length $token->{data}) {
5895              !!!cp ('t310');              !!!cp ('t310');
5896              !!!next-token;              !!!next-token;
5897              redo B;              next B;
5898            }            }
5899          }          }
5900                    
5901          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
5902            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
5903              !!!cp ('t311');              !!!cp ('t311');
5904              !!!parse-error (type => 'in frameset:#character');              !!!parse-error (type => 'in frameset:#character', token => $token);
5905            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
5906              !!!cp ('t312');              !!!cp ('t312');
5907              !!!parse-error (type => 'after frameset:#character');              !!!parse-error (type => 'after frameset:#character', token => $token);
5908            } else { # "after html frameset"            } else { # "after html frameset"
5909              !!!cp ('t313');              !!!cp ('t313');
5910              !!!parse-error (type => 'after html:#character');              !!!parse-error (type => 'after html:#character', token => $token);
5911    
5912              $self->{insertion_mode} = AFTER_FRAMESET_IM;              $self->{insertion_mode} = AFTER_FRAMESET_IM;
5913              ## Reprocess in the "after frameset" insertion mode.              ## Reprocess in the "after frameset" insertion mode.
5914              !!!parse-error (type => 'after frameset:#character');              !!!parse-error (type => 'after frameset:#character', token => $token);
5915            }            }
5916                        
5917            ## Ignore the token.            ## Ignore the token.
# Line 5193  sub _tree_construction_main ($) { Line 5922  sub _tree_construction_main ($) {
5922              !!!cp ('t315');              !!!cp ('t315');
5923              !!!next-token;              !!!next-token;
5924            }            }
5925            redo B;            next B;
5926          }          }
5927                    
5928          die qq[$0: Character "$token->{data}"];          die qq[$0: Character "$token->{data}"];
5929        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5930          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
5931            !!!cp ('t316');            !!!cp ('t316');
5932            !!!parse-error (type => 'after html:'.$token->{tag_name});            !!!parse-error (type => 'after html:'.$token->{tag_name}, token => $token);
5933    
5934            $self->{insertion_mode} = AFTER_FRAMESET_IM;            $self->{insertion_mode} = AFTER_FRAMESET_IM;
5935            ## Process in the "after frameset" insertion mode.            ## Process in the "after frameset" insertion mode.
# Line 5211  sub _tree_construction_main ($) { Line 5940  sub _tree_construction_main ($) {
5940          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
5941              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
5942            !!!cp ('t318');            !!!cp ('t318');
5943            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5944              !!!nack ('t318.1');
5945            !!!next-token;            !!!next-token;
5946            redo B;            next B;
5947          } elsif ($token->{tag_name} eq 'frame' and          } elsif ($token->{tag_name} eq 'frame' and
5948                   $self->{insertion_mode} == IN_FRAMESET_IM) {                   $self->{insertion_mode} == IN_FRAMESET_IM) {
5949            !!!cp ('t319');            !!!cp ('t319');
5950            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5951            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
5952              !!!ack ('t319.1');
5953            !!!next-token;            !!!next-token;
5954            redo B;            next B;
5955          } elsif ($token->{tag_name} eq 'noframes') {          } elsif ($token->{tag_name} eq 'noframes') {
5956            !!!cp ('t320');            !!!cp ('t320');
5957            ## NOTE: As if in body.            ## NOTE: As if in body.
5958            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);            $parse_rcdata->(CDATA_CONTENT_MODEL);
5959            redo B;            next B;
5960          } else {          } else {
5961            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
5962              !!!cp ('t321');              !!!cp ('t321');
5963              !!!parse-error (type => 'in frameset:'.$token->{tag_name});              !!!parse-error (type => 'in frameset:'.$token->{tag_name}, token => $token);
5964            } else {            } else {
5965              !!!cp ('t322');              !!!cp ('t322');
5966              !!!parse-error (type => 'after frameset:'.$token->{tag_name});              !!!parse-error (type => 'after frameset:'.$token->{tag_name}, token => $token);
5967            }            }
5968            ## Ignore the token            ## Ignore the token
5969              !!!nack ('t322.1');
5970            !!!next-token;            !!!next-token;
5971            redo B;            next B;
5972          }          }
5973        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
5974          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
5975            !!!cp ('t323');            !!!cp ('t323');
5976            !!!parse-error (type => 'after html:/'.$token->{tag_name});            !!!parse-error (type => 'after html:/'.$token->{tag_name}, token => $token);
5977    
5978            $self->{insertion_mode} = AFTER_FRAMESET_IM;            $self->{insertion_mode} = AFTER_FRAMESET_IM;
5979            ## Process in the "after frameset" insertion mode.            ## Process in the "after frameset" insertion mode.
# Line 5251  sub _tree_construction_main ($) { Line 5983  sub _tree_construction_main ($) {
5983    
5984          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
5985              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
5986            if ($self->{open_elements}->[-1]->[1] eq 'html' and            if ($self->{open_elements}->[-1]->[1] & HTML_EL and
5987                @{$self->{open_elements}} == 1) {                @{$self->{open_elements}} == 1) {
5988              !!!cp ('t325');              !!!cp ('t325');
5989              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5990              ## Ignore the token              ## Ignore the token
5991              !!!next-token;              !!!next-token;
5992            } else {            } else {
# Line 5264  sub _tree_construction_main ($) { Line 5996  sub _tree_construction_main ($) {
5996            }            }
5997    
5998            if (not defined $self->{inner_html_node} and            if (not defined $self->{inner_html_node} and
5999                $self->{open_elements}->[-1]->[1] ne 'frameset') {                not ($self->{open_elements}->[-1]->[1] & FRAMESET_EL)) {
6000              !!!cp ('t327');              !!!cp ('t327');
6001              $self->{insertion_mode} = AFTER_FRAMESET_IM;              $self->{insertion_mode} = AFTER_FRAMESET_IM;
6002            } else {            } else {
6003              !!!cp ('t328');              !!!cp ('t328');
6004            }            }
6005            redo B;            next B;
6006          } elsif ($token->{tag_name} eq 'html' and          } elsif ($token->{tag_name} eq 'html' and
6007                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {
6008            !!!cp ('t329');            !!!cp ('t329');
6009            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
6010            !!!next-token;            !!!next-token;
6011            redo B;            next B;
6012          } else {          } else {
6013            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6014              !!!cp ('t330');              !!!cp ('t330');
6015              !!!parse-error (type => 'in frameset:/'.$token->{tag_name});              !!!parse-error (type => 'in frameset:/'.$token->{tag_name}, token => $token);
6016            } else {            } else {
6017              !!!cp ('t331');              !!!cp ('t331');
6018              !!!parse-error (type => 'after frameset:/'.$token->{tag_name});              !!!parse-error (type => 'after frameset:/'.$token->{tag_name}, token => $token);
6019            }            }
6020            ## Ignore the token            ## Ignore the token
6021            !!!next-token;            !!!next-token;
6022            redo B;            next B;
6023            }
6024          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6025            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6026                    @{$self->{open_elements}} == 1) { # redundant, maybe
6027              !!!cp ('t331.1');
6028              !!!parse-error (type => 'in body:#eof', token => $token);
6029            } else {
6030              !!!cp ('t331.2');
6031          }          }
6032            
6033            ## Stop parsing
6034            last B;
6035        } else {        } else {
6036          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6037        }        }
# Line 5303  sub _tree_construction_main ($) { Line 6046  sub _tree_construction_main ($) {
6046        if ($token->{tag_name} eq 'script') {        if ($token->{tag_name} eq 'script') {
6047          !!!cp ('t332');          !!!cp ('t332');
6048          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6049          $script_start_tag->($insert);          $script_start_tag->();
6050          redo B;          next B;
6051        } elsif ($token->{tag_name} eq 'style') {        } elsif ($token->{tag_name} eq 'style') {
6052          !!!cp ('t333');          !!!cp ('t333');
6053          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6054          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);          $parse_rcdata->(CDATA_CONTENT_MODEL);
6055          redo B;          next B;
6056        } elsif ({        } elsif ({
6057                  base => 1, link => 1,                  base => 1, link => 1,
6058                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6059          !!!cp ('t334');          !!!cp ('t334');
6060          ## NOTE: This is an "as if in head" code clone, only "-t" differs          ## NOTE: This is an "as if in head" code clone, only "-t" differs
6061          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6062          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
6063            !!!ack ('t334.1');
6064          !!!next-token;          !!!next-token;
6065          redo B;          next B;
6066        } elsif ($token->{tag_name} eq 'meta') {        } elsif ($token->{tag_name} eq 'meta') {
6067          ## NOTE: This is an "as if in head" code clone, only "-t" differs          ## NOTE: This is an "as if in head" code clone, only "-t" differs
6068          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6069          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.
6070    
6071          unless ($self->{confident}) {          unless ($self->{confident}) {
6072            if ($token->{attributes}->{charset}) { ## TODO: And if supported            if ($token->{attributes}->{charset}) { ## TODO: And if supported
6073              !!!cp ('t335');              !!!cp ('t335');
6074              $self->{change_encoding}              $self->{change_encoding}
6075                  ->($self, $token->{attributes}->{charset}->{value});                  ->($self, $token->{attributes}->{charset}->{value}, $token);
6076                            
6077              $meta_el->[0]->get_attribute_node_ns (undef, 'charset')              $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
6078                  ->set_user_data (manakai_has_reference =>                  ->set_user_data (manakai_has_reference =>
# Line 5343  sub _tree_construction_main ($) { Line 6087  sub _tree_construction_main ($) {
6087                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
6088                !!!cp ('t336');                !!!cp ('t336');
6089                $self->{change_encoding}                $self->{change_encoding}
6090                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);
6091                $meta_el->[0]->get_attribute_node_ns (undef, 'content')                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
6092                    ->set_user_data (manakai_has_reference =>                    ->set_user_data (manakai_has_reference =>
6093                                         $token->{attributes}->{content}                                         $token->{attributes}->{content}
# Line 5367  sub _tree_construction_main ($) { Line 6111  sub _tree_construction_main ($) {
6111            }            }
6112          }          }
6113    
6114            !!!ack ('t338.1');
6115          !!!next-token;          !!!next-token;
6116          redo B;          next B;
6117        } elsif ($token->{tag_name} eq 'title') {        } elsif ($token->{tag_name} eq 'title') {
6118          !!!cp ('t341');          !!!cp ('t341');
         !!!parse-error (type => 'in body:title');  
6119          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6120          $parse_rcdata->(RCDATA_CONTENT_MODEL, sub {          $parse_rcdata->(RCDATA_CONTENT_MODEL);
6121            if (defined $self->{head_element}) {          next B;
             !!!cp ('t339');  
             $self->{head_element}->append_child ($_[0]);  
           } else {  
             !!!cp ('t340');  
             $insert->($_[0]);  
           }  
         });  
         redo B;  
6122        } elsif ($token->{tag_name} eq 'body') {        } elsif ($token->{tag_name} eq 'body') {
6123          !!!parse-error (type => 'in body:body');          !!!parse-error (type => 'in body:body', token => $token);
6124                                
6125          if (@{$self->{open_elements}} == 1 or          if (@{$self->{open_elements}} == 1 or
6126              $self->{open_elements}->[1]->[1] ne 'body') {              not ($self->{open_elements}->[1]->[1] & BODY_EL)) {
6127            !!!cp ('t342');            !!!cp ('t342');
6128            ## Ignore the token            ## Ignore the token
6129          } else {          } else {
# Line 5401  sub _tree_construction_main ($) { Line 6137  sub _tree_construction_main ($) {
6137              }              }
6138            }            }
6139          }          }
6140            !!!nack ('t343.1');
6141          !!!next-token;          !!!next-token;
6142          redo B;          next B;
6143        } elsif ({        } elsif ({
6144                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
6145                  div => 1, dl => 1, fieldset => 1,                  div => 1, dl => 1, fieldset => 1,
6146                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
6147                  listing => 1, menu => 1, ol => 1, p => 1, ul => 1,                  menu => 1, ol => 1, p => 1, ul => 1,
6148                  pre => 1,                  pre => 1, listing => 1,
6149                    form => 1,
6150                    table => 1,
6151                    hr => 1,
6152                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6153            if ($token->{tag_name} eq 'form' and defined $self->{form_element}) {
6154              !!!cp ('t350');
6155              !!!parse-error (type => 'in form:form', token => $token);
6156              ## Ignore the token
6157              !!!nack ('t350.1');
6158              !!!next-token;
6159              next B;
6160            }
6161    
6162          ## has a p element in scope          ## has a p element in scope
6163          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6164            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6165              !!!cp ('t344');              !!!cp ('t344');
6166              !!!back-token;              !!!back-token; # <form>
6167              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6168              redo B;                        line => $token->{line}, column => $token->{column}};
6169            } elsif ({              next B;
6170                      table => 1, caption => 1, td => 1, th => 1,            } elsif ($_->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6171              !!!cp ('t345');              !!!cp ('t345');
6172              last INSCOPE;              last INSCOPE;
6173            }            }
6174          } # INSCOPE          } # INSCOPE
6175                        
6176          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6177          if ($token->{tag_name} eq 'pre') {          if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {
6178              !!!nack ('t346.1');
6179            !!!next-token;            !!!next-token;
6180            if ($token->{type} == CHARACTER_TOKEN) {            if ($token->{type} == CHARACTER_TOKEN) {
6181              $token->{data} =~ s/^\x0A//;              $token->{data} =~ s/^\x0A//;
# Line 5440  sub _tree_construction_main ($) { Line 6188  sub _tree_construction_main ($) {
6188            } else {            } else {
6189              !!!cp ('t348');              !!!cp ('t348');
6190            }            }
6191          } else {          } elsif ($token->{tag_name} eq 'form') {
6192            !!!cp ('t347');            !!!cp ('t347.1');
6193              $self->{form_element} = $self->{open_elements}->[-1]->[0];
6194    
6195              !!!nack ('t347.2');
6196            !!!next-token;            !!!next-token;
6197          }          } elsif ($token->{tag_name} eq 'table') {
6198          redo B;            !!!cp ('t382');
6199        } elsif ($token->{tag_name} eq 'form') {            push @{$open_tables}, [$self->{open_elements}->[-1]->[0]];
6200          if (defined $self->{form_element}) {            
6201            !!!cp ('t350');            $self->{insertion_mode} = IN_TABLE_IM;
6202            !!!parse-error (type => 'in form:form');  
6203            ## Ignore the token            !!!nack ('t382.1');
6204              !!!next-token;
6205            } elsif ($token->{tag_name} eq 'hr') {
6206              !!!cp ('t386');
6207              pop @{$self->{open_elements}};
6208            
6209              !!!nack ('t386.1');
6210            !!!next-token;            !!!next-token;
           redo B;  
6211          } else {          } else {
6212            ## has a p element in scope            !!!nack ('t347.1');
           INSCOPE: for (reverse @{$self->{open_elements}}) {  
             if ($_->[1] eq 'p') {  
               !!!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];  
6213            !!!next-token;            !!!next-token;
           redo B;  
6214          }          }
6215        } elsif ($token->{tag_name} eq 'li') {          next B;
6216          } elsif ({li => 1, dt => 1, dd => 1}->{$token->{tag_name}}) {
6217          ## has a p element in scope          ## has a p element in scope
6218          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6219            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6220              !!!cp ('t353');              !!!cp ('t353');
6221              !!!back-token;              !!!back-token; # <x>
6222              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6223              redo B;                        line => $token->{line}, column => $token->{column}};
6224            } elsif ({              next B;
6225                      table => 1, caption => 1, td => 1, th => 1,            } elsif ($_->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6226              !!!cp ('t354');              !!!cp ('t354');
6227              last INSCOPE;              last INSCOPE;
6228            }            }
# Line 5494  sub _tree_construction_main ($) { Line 6231  sub _tree_construction_main ($) {
6231          ## Step 1          ## Step 1
6232          my $i = -1;          my $i = -1;
6233          my $node = $self->{open_elements}->[$i];          my $node = $self->{open_elements}->[$i];
6234            my $li_or_dtdd = {li => {li => 1},
6235                              dt => {dt => 1, dd => 1},
6236                              dd => {dt => 1, dd => 1}}->{$token->{tag_name}};
6237          LI: {          LI: {
6238            ## Step 2            ## Step 2
6239            if ($node->[1] eq 'li') {            if ($li_or_dtdd->{$node->[0]->manakai_local_name}) {
6240              if ($i != -1) {              if ($i != -1) {
6241                !!!cp ('t355');                !!!cp ('t355');
6242                !!!parse-error (type => 'end tag missing:'.                !!!parse-error (type => 'not closed',
6243                                $self->{open_elements}->[-1]->[1]);                                value => $self->{open_elements}->[-1]->[0]
6244                                      ->manakai_local_name,
6245                                  token => $token);
6246              } else {              } else {
6247                !!!cp ('t356');                !!!cp ('t356');
6248              }              }
# Line 5511  sub _tree_construction_main ($) { Line 6253  sub _tree_construction_main ($) {
6253            }            }
6254                        
6255            ## Step 3            ## Step 3
6256            if (not $formatting_category->{$node->[1]} and            if (not ($node->[1] & FORMATTING_EL) and
6257                #not $phrasing_category->{$node->[1]} and                #not $phrasing_category->{$node->[1]} and
6258                ($special_category->{$node->[1]} or                ($node->[1] & SPECIAL_EL or
6259                 $scoping_category->{$node->[1]}) and                 $node->[1] & SCOPING_EL) and
6260                $node->[1] ne 'address' and $node->[1] ne 'div') {                not ($node->[1] & ADDRESS_EL) and
6261                  not ($node->[1] & DIV_EL)) {
6262              !!!cp ('t358');              !!!cp ('t358');
6263              last LI;              last LI;
6264            }            }
# Line 5527  sub _tree_construction_main ($) { Line 6270  sub _tree_construction_main ($) {
6270            redo LI;            redo LI;
6271          } # LI          } # LI
6272                        
6273          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6274            !!!nack ('t359.1');
6275          !!!next-token;          !!!next-token;
6276          redo B;          next B;
       } 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;  
6277        } elsif ($token->{tag_name} eq 'plaintext') {        } elsif ($token->{tag_name} eq 'plaintext') {
6278          ## has a p element in scope          ## has a p element in scope
6279          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6280            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6281              !!!cp ('t367');              !!!cp ('t367');
6282              !!!back-token;              !!!back-token; # <plaintext>
6283              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6284              redo B;                        line => $token->{line}, column => $token->{column}};
6285            } elsif ({              next B;
6286                      table => 1, caption => 1, td => 1, th => 1,            } elsif ($_->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6287              !!!cp ('t368');              !!!cp ('t368');
6288              last INSCOPE;              last INSCOPE;
6289            }            }
6290          } # INSCOPE          } # INSCOPE
6291                        
6292          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6293                        
6294          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
6295                        
6296            !!!nack ('t368.1');
6297          !!!next-token;          !!!next-token;
6298          redo B;          next B;
6299        } elsif ($token->{tag_name} eq 'a') {        } elsif ($token->{tag_name} eq 'a') {
6300          AFE: for my $i (reverse 0..$#$active_formatting_elements) {          AFE: for my $i (reverse 0..$#$active_formatting_elements) {
6301            my $node = $active_formatting_elements->[$i];            my $node = $active_formatting_elements->[$i];
6302            if ($node->[1] eq 'a') {            if ($node->[1] & A_EL) {
6303              !!!cp ('t371');              !!!cp ('t371');
6304              !!!parse-error (type => 'in a:a');              !!!parse-error (type => 'in a:a', token => $token);
6305                            
6306              !!!back-token;              !!!back-token; # <a>
6307              $token = {type => END_TAG_TOKEN, tag_name => 'a'};              $token = {type => END_TAG_TOKEN, tag_name => 'a',
6308              $formatting_end_tag->($token->{tag_name});                        line => $token->{line}, column => $token->{column}};
6309                $formatting_end_tag->($token);
6310                            
6311              AFE2: for (reverse 0..$#$active_formatting_elements) {              AFE2: for (reverse 0..$#$active_formatting_elements) {
6312                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
# Line 5643  sub _tree_construction_main ($) { Line 6331  sub _tree_construction_main ($) {
6331                        
6332          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6333    
6334          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6335          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
6336    
6337            !!!nack ('t374.1');
6338          !!!next-token;          !!!next-token;
6339          redo B;          next B;
       } elsif ({  
                 b => 1, big => 1, em => 1, font => 1, i => 1,  
                 s => 1, small => 1, strile => 1,  
                 strong => 1, tt => 1, u => 1,  
                }->{$token->{tag_name}}) {  
         !!!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;  
6340        } elsif ($token->{tag_name} eq 'nobr') {        } elsif ($token->{tag_name} eq 'nobr') {
6341          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6342    
6343          ## has a |nobr| element in scope          ## has a |nobr| element in scope
6344          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6345            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6346            if ($node->[1] eq 'nobr') {            if ($node->[1] & NOBR_EL) {
6347              !!!cp ('t376');              !!!cp ('t376');
6348              !!!parse-error (type => 'in nobr:nobr');              !!!parse-error (type => 'in nobr:nobr', token => $token);
6349              !!!back-token;              !!!back-token; # <nobr>
6350              $token = {type => END_TAG_TOKEN, tag_name => 'nobr'};              $token = {type => END_TAG_TOKEN, tag_name => 'nobr',
6351              redo B;                        line => $token->{line}, column => $token->{column}};
6352            } elsif ({              next B;
6353                      table => 1, caption => 1, td => 1, th => 1,            } elsif ($node->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6354              !!!cp ('t377');              !!!cp ('t377');
6355              last INSCOPE;              last INSCOPE;
6356            }            }
6357          } # INSCOPE          } # INSCOPE
6358                    
6359          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6360          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
6361                    
6362            !!!nack ('t377.1');
6363          !!!next-token;          !!!next-token;
6364          redo B;          next B;
6365        } elsif ($token->{tag_name} eq 'button') {        } elsif ($token->{tag_name} eq 'button') {
6366          ## has a button element in scope          ## has a button element in scope
6367          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6368            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6369            if ($node->[1] eq 'button') {            if ($node->[1] & BUTTON_EL) {
6370              !!!cp ('t378');              !!!cp ('t378');
6371              !!!parse-error (type => 'in button:button');              !!!parse-error (type => 'in button:button', token => $token);
6372              !!!back-token;              !!!back-token; # <button>
6373              $token = {type => END_TAG_TOKEN, tag_name => 'button'};              $token = {type => END_TAG_TOKEN, tag_name => 'button',
6374              redo B;                        line => $token->{line}, column => $token->{column}};
6375            } elsif ({              next B;
6376                      table => 1, caption => 1, td => 1, th => 1,            } elsif ($node->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6377              !!!cp ('t379');              !!!cp ('t379');
6378              last INSCOPE;              last INSCOPE;
6379            }            }
# Line 5708  sub _tree_construction_main ($) { Line 6381  sub _tree_construction_main ($) {
6381                        
6382          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6383                        
6384          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6385    
6386          ## TODO: associate with $self->{form_element} if defined          ## TODO: associate with $self->{form_element} if defined
6387    
6388          push @$active_formatting_elements, ['#marker', ''];          push @$active_formatting_elements, ['#marker', ''];
6389    
6390            !!!nack ('t379.1');
6391          !!!next-token;          !!!next-token;
6392          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, $insert);  
         redo B;  
       } elsif ($token->{tag_name} eq 'table') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!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});  
             
         $self->{insertion_mode} = IN_TABLE_IM;  
             
         !!!next-token;  
         redo B;  
6393        } elsif ({        } elsif ({
6394                  area => 1, basefont => 1, bgsound => 1, br => 1,                  xmp => 1,
6395                  embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,                  iframe => 1,
6396                  image => 1,                  noembed => 1,
6397                    noframes => 1,
6398                    noscript => 0, ## TODO: 1 if scripting is enabled
6399                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6400          if ($token->{tag_name} eq 'image') {          if ($token->{tag_name} eq 'xmp') {
6401            !!!cp ('t384');            !!!cp ('t381');
6402            !!!parse-error (type => 'image');            $reconstruct_active_formatting_elements->($insert_to_current);
           $token->{tag_name} = 'img';  
6403          } else {          } else {
6404            !!!cp ('t385');            !!!cp ('t399');
6405          }          }
6406            ## NOTE: There is an "as if in body" code clone.
6407          ## NOTE: There is an "as if <br>" code clone.          $parse_rcdata->(CDATA_CONTENT_MODEL);
6408          $reconstruct_active_formatting_elements->($insert_to_current);          next B;
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         pop @{$self->{open_elements}};  
           
         !!!next-token;  
         redo B;  
       } elsif ($token->{tag_name} eq 'hr') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!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;  
6409        } elsif ($token->{tag_name} eq 'isindex') {        } elsif ($token->{tag_name} eq 'isindex') {
6410          !!!parse-error (type => 'isindex');          !!!parse-error (type => 'isindex', token => $token);
6411                    
6412          if (defined $self->{form_element}) {          if (defined $self->{form_element}) {
6413            !!!cp ('t389');            !!!cp ('t389');
6414            ## Ignore the token            ## Ignore the token
6415              !!!nack ('t389'); ## NOTE: Not acknowledged.
6416            !!!next-token;            !!!next-token;
6417            redo B;            next B;
6418          } else {          } else {
6419            my $at = $token->{attributes};            my $at = $token->{attributes};
6420            my $form_attrs;            my $form_attrs;
# Line 5825  sub _tree_construction_main ($) { Line 6425  sub _tree_construction_main ($) {
6425            delete $at->{prompt};            delete $at->{prompt};
6426            my @tokens = (            my @tokens = (
6427                          {type => START_TAG_TOKEN, tag_name => 'form',                          {type => START_TAG_TOKEN, tag_name => 'form',
6428                           attributes => $form_attrs},                           attributes => $form_attrs,
6429                          {type => START_TAG_TOKEN, tag_name => 'hr'},                           line => $token->{line}, column => $token->{column}},
6430                          {type => START_TAG_TOKEN, tag_name => 'p'},                          {type => START_TAG_TOKEN, tag_name => 'hr',
6431                          {type => START_TAG_TOKEN, tag_name => 'label'},                           line => $token->{line}, column => $token->{column}},
6432                            {type => START_TAG_TOKEN, tag_name => 'p',
6433                             line => $token->{line}, column => $token->{column}},
6434                            {type => START_TAG_TOKEN, tag_name => 'label',
6435                             line => $token->{line}, column => $token->{column}},
6436                         );                         );
6437            if ($prompt_attr) {            if ($prompt_attr) {
6438              !!!cp ('t390');              !!!cp ('t390');
6439              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value}};              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value},
6440                               #line => $token->{line}, column => $token->{column},
6441                              };
6442            } else {            } else {
6443              !!!cp ('t391');              !!!cp ('t391');
6444              push @tokens, {type => CHARACTER_TOKEN,              push @tokens, {type => CHARACTER_TOKEN,
6445                             data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD                             data => 'This is a searchable index. Insert your search keywords here: ',
6446                               #line => $token->{line}, column => $token->{column},
6447                              }; # SHOULD
6448              ## TODO: make this configurable              ## TODO: make this configurable
6449            }            }
6450            push @tokens,            push @tokens,
6451                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at},                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at,
6452                             line => $token->{line}, column => $token->{column}},
6453                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
6454                          {type => END_TAG_TOKEN, tag_name => 'label'},                          {type => END_TAG_TOKEN, tag_name => 'label',
6455                          {type => END_TAG_TOKEN, tag_name => 'p'},                           line => $token->{line}, column => $token->{column}},
6456                          {type => START_TAG_TOKEN, tag_name => 'hr'},                          {type => END_TAG_TOKEN, tag_name => 'p',
6457                          {type => END_TAG_TOKEN, tag_name => 'form'};                           line => $token->{line}, column => $token->{column}},
6458            $token = shift @tokens;                          {type => START_TAG_TOKEN, tag_name => 'hr',
6459                             line => $token->{line}, column => $token->{column}},
6460                            {type => END_TAG_TOKEN, tag_name => 'form',
6461                             line => $token->{line}, column => $token->{column}};
6462              !!!nack ('t391.1'); ## NOTE: Not acknowledged.
6463            !!!back-token (@tokens);            !!!back-token (@tokens);
6464            redo B;            !!!next-token;
6465              next B;
6466          }          }
6467        } elsif ($token->{tag_name} eq 'textarea') {        } elsif ($token->{tag_name} eq 'textarea') {
6468          my $tag_name = $token->{tag_name};          my $tag_name = $token->{tag_name};
6469          my $el;          my $el;
6470          !!!create-element ($el, $token->{tag_name}, $token->{attributes});          !!!create-element ($el, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
6471                    
6472          ## TODO: $self->{form_element} if defined          ## TODO: $self->{form_element} if defined
6473          $self->{content_model} = RCDATA_CONTENT_MODEL;          $self->{content_model} = RCDATA_CONTENT_MODEL;
# Line 5862  sub _tree_construction_main ($) { Line 6476  sub _tree_construction_main ($) {
6476          $insert->($el);          $insert->($el);
6477                    
6478          my $text = '';          my $text = '';
6479            !!!nack ('t392.1');
6480          !!!next-token;          !!!next-token;
6481          if ($token->{type} == CHARACTER_TOKEN) {          if ($token->{type} == CHARACTER_TOKEN) {
6482            $token->{data} =~ s/^\x0A//;            $token->{data} =~ s/^\x0A//;
# Line 5892  sub _tree_construction_main ($) { Line 6507  sub _tree_construction_main ($) {
6507            ## Ignore the token            ## Ignore the token
6508          } else {          } else {
6509            !!!cp ('t398');            !!!cp ('t398');
6510            !!!parse-error (type => 'in RCDATA:#'.$token->{type});            !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);
6511          }          }
6512          !!!next-token;          !!!next-token;
6513          redo B;          next B;
6514        } elsif ({        } elsif ($token->{tag_name} eq 'math' or
6515                  iframe => 1,                 $token->{tag_name} eq 'svg') {
                 noembed => 1,  
                 noframes => 1,  
                 noscript => 0, ## TODO: 1 if scripting is enabled  
                }->{$token->{tag_name}}) {  
         !!!cp ('t399');  
         ## NOTE: There is an "as if in body" code clone.  
         $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);  
         redo B;  
       } elsif ($token->{tag_name} eq 'select') {  
         !!!cp ('t400');  
6516          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
6517    
6518          ## TODO: associate with $self->{form_element} if defined          ## "adjust SVG attributes" ('svg' only) - done in insert-element-f
6519    
6520            ## "adjust foreign attributes" - done in insert-element-f
6521            
6522            !!!insert-element-f ($token->{tag_name} eq 'math' ? $MML_NS : $SVG_NS, $token->{tag_name}, $token->{attributes}, $token);
6523                    
6524          $self->{insertion_mode} = IN_SELECT_IM;          if ($self->{self_closing}) {
6525              pop @{$self->{open_elements}};
6526              !!!ack ('t398.1');
6527            } else {
6528              !!!cp ('t398.2');
6529              $self->{insertion_mode} |= IN_FOREIGN_CONTENT_IM;
6530              ## NOTE: |<body><math><mi><svg>| -> "in foreign content" insertion
6531              ## mode, "in body" (not "in foreign content") secondary insertion
6532              ## mode, maybe.
6533            }
6534    
6535          !!!next-token;          !!!next-token;
6536          redo B;          next B;
6537        } elsif ({        } elsif ({
6538                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
6539                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 5924  sub _tree_construction_main ($) { Line 6541  sub _tree_construction_main ($) {
6541                  thead => 1, tr => 1,                  thead => 1, tr => 1,
6542                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6543          !!!cp ('t401');          !!!cp ('t401');
6544          !!!parse-error (type => 'in body:'.$token->{tag_name});          !!!parse-error (type => 'in body:'.$token->{tag_name}, token => $token);
6545          ## Ignore the token          ## Ignore the token
6546            !!!nack ('t401.1'); ## NOTE: |<col/>| or |<frame/>| here is an error.
6547          !!!next-token;          !!!next-token;
6548          redo B;          next B;
6549                    
6550          ## ISSUE: An issue on HTML5 new elements in the spec.          ## ISSUE: An issue on HTML5 new elements in the spec.
6551        } else {        } else {
6552          !!!cp ('t402');          if ($token->{tag_name} eq 'image') {
6553              !!!cp ('t384');
6554              !!!parse-error (type => 'image', token => $token);
6555              $token->{tag_name} = 'img';
6556            } else {
6557              !!!cp ('t385');
6558            }
6559    
6560            ## NOTE: There is an "as if <br>" code clone.
6561          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6562                    
6563          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6564    
6565            if ({
6566                 applet => 1, marquee => 1, object => 1,
6567                }->{$token->{tag_name}}) {
6568              !!!cp ('t380');
6569              push @$active_formatting_elements, ['#marker', ''];
6570              !!!nack ('t380.1');
6571            } elsif ({
6572                      b => 1, big => 1, em => 1, font => 1, i => 1,
6573                      s => 1, small => 1, strile => 1,
6574                      strong => 1, tt => 1, u => 1,
6575                     }->{$token->{tag_name}}) {
6576              !!!cp ('t375');
6577              push @$active_formatting_elements, $self->{open_elements}->[-1];
6578              !!!nack ('t375.1');
6579            } elsif ($token->{tag_name} eq 'input') {
6580              !!!cp ('t388');
6581              ## TODO: associate with $self->{form_element} if defined
6582              pop @{$self->{open_elements}};
6583              !!!ack ('t388.2');
6584            } elsif ({
6585                      area => 1, basefont => 1, bgsound => 1, br => 1,
6586                      embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
6587                      #image => 1,
6588                     }->{$token->{tag_name}}) {
6589              !!!cp ('t388.1');
6590              pop @{$self->{open_elements}};
6591              !!!ack ('t388.3');
6592            } elsif ($token->{tag_name} eq 'select') {
6593              ## TODO: associate with $self->{form_element} if defined
6594            
6595              if ($self->{insertion_mode} & TABLE_IMS or
6596                  $self->{insertion_mode} & BODY_TABLE_IMS or
6597                  $self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
6598                !!!cp ('t400.1');
6599                $self->{insertion_mode} = IN_SELECT_IN_TABLE_IM;
6600              } else {
6601                !!!cp ('t400.2');
6602                $self->{insertion_mode} = IN_SELECT_IM;
6603              }
6604              !!!nack ('t400.3');
6605            } else {
6606              !!!nack ('t402');
6607            }
6608                    
6609          !!!next-token;          !!!next-token;
6610          redo B;          next B;
6611        }        }
6612      } elsif ($token->{type} == END_TAG_TOKEN) {      } elsif ($token->{type} == END_TAG_TOKEN) {
6613        if ($token->{tag_name} eq 'body') {        if ($token->{tag_name} eq 'body') {
6614          if (@{$self->{open_elements}} > 1 and          ## has a |body| element in scope
6615              $self->{open_elements}->[1]->[1] eq 'body') {          my $i;
6616            for (@{$self->{open_elements}}) {          INSCOPE: {
6617              unless ({            for (reverse @{$self->{open_elements}}) {
6618                         dd => 1, dt => 1, li => 1, p => 1, td => 1,              if ($_->[1] & BODY_EL) {
6619                         th => 1, tr => 1, body => 1, html => 1,                !!!cp ('t405');
6620                       tbody => 1, tfoot => 1, thead => 1,                $i = $_;
6621                      }->{$_->[1]}) {                last INSCOPE;
6622                !!!cp ('t403');              } elsif ($_->[1] & SCOPING_EL) {
6623                !!!parse-error (type => 'not closed:'.$_->[1]);                !!!cp ('t405.1');
6624              } else {                last;
               !!!cp ('t404');  
6625              }              }
6626            }            }
6627    
6628            $self->{insertion_mode} = AFTER_BODY_IM;            !!!parse-error (type => 'start tag not allowed',
6629            !!!next-token;                            value => $token->{tag_name}, token => $token);
6630            redo B;            ## NOTE: Ignore the token.
         } else {  
           !!!cp ('t405');  
           !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
           ## Ignore the token  
6631            !!!next-token;            !!!next-token;
6632            redo B;            next B;
6633            } # INSCOPE
6634    
6635            for (@{$self->{open_elements}}) {
6636              unless ($_->[1] & ALL_END_TAG_OPTIONAL_EL) {
6637                !!!cp ('t403');
6638                !!!parse-error (type => 'not closed',
6639                                value => $_->[0]->manakai_local_name,
6640                                token => $token);
6641                last;
6642              } else {
6643                !!!cp ('t404');
6644              }
6645          }          }
6646    
6647            $self->{insertion_mode} = AFTER_BODY_IM;
6648            !!!next-token;
6649            next B;
6650        } elsif ($token->{tag_name} eq 'html') {        } elsif ($token->{tag_name} eq 'html') {
6651          if (@{$self->{open_elements}} > 1 and $self->{open_elements}->[1]->[1] eq 'body') {          ## TODO: Update this code.  It seems that the code below is not
6652            ## up-to-date, though it has same effect as speced.
6653            if (@{$self->{open_elements}} > 1 and
6654                $self->{open_elements}->[1]->[1] & BODY_EL) {
6655            ## ISSUE: There is an issue in the spec.            ## ISSUE: There is an issue in the spec.
6656            if ($self->{open_elements}->[-1]->[1] ne 'body') {            unless ($self->{open_elements}->[-1]->[1] & BODY_EL) {
6657              !!!cp ('t406');              !!!cp ('t406');
6658              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);              !!!parse-error (type => 'not closed',
6659                                value => $self->{open_elements}->[1]->[0]
6660                                    ->manakai_local_name,
6661                                token => $token);
6662            } else {            } else {
6663              !!!cp ('t407');              !!!cp ('t407');
6664            }            }
6665            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
6666            ## reprocess            ## reprocess
6667            redo B;            next B;
6668          } else {          } else {
6669            !!!cp ('t408');            !!!cp ('t408');
6670            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6671            ## Ignore the token            ## Ignore the token
6672            !!!next-token;            !!!next-token;
6673            redo B;            next B;
6674          }          }
6675        } elsif ({        } elsif ({
6676                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
6677                  div => 1, dl => 1, fieldset => 1, listing => 1,                  div => 1, dl => 1, fieldset => 1, listing => 1,
6678                  menu => 1, ol => 1, pre => 1, ul => 1,                  menu => 1, ol => 1, pre => 1, ul => 1,
                 p => 1,  
6679                  dd => 1, dt => 1, li => 1,                  dd => 1, dt => 1, li => 1,
6680                  button => 1, marquee => 1, object => 1,                  applet => 1, button => 1, marquee => 1, object => 1,
6681                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6682          ## has an element in scope          ## has an element in scope
6683          my $i;          my $i;
6684          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6685            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6686            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
             ## generate implied end tags  
             while ({  
                     dd => ($token->{tag_name} ne 'dd'),  
                     dt => ($token->{tag_name} ne 'dt'),  
                     li => ($token->{tag_name} ne 'li'),  
                     p => ($token->{tag_name} ne 'p'),  
                    }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!cp ('t409');  
               pop @{$self->{open_elements}};  
             }  
               
6687              !!!cp ('t410');              !!!cp ('t410');
6688              $i = $_;              $i = $_;
6689              last INSCOPE unless $token->{tag_name} eq 'p';              last INSCOPE;
6690            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6691              !!!cp ('t411');              !!!cp ('t411');
6692              last INSCOPE;              last INSCOPE;
6693            }            }
6694          } # INSCOPE          } # INSCOPE
6695            
6696          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          unless (defined $i) { # has an element in scope
6697            if (defined $i) {            !!!cp ('t413');
6698              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6699            } else {
6700              ## Step 1. generate implied end tags
6701              while ({
6702                      dd => ($token->{tag_name} ne 'dd'),
6703                      dt => ($token->{tag_name} ne 'dt'),
6704                      li => ($token->{tag_name} ne 'li'),
6705                      p => 1,
6706                     }->{$self->{open_elements}->[-1]->[0]->manakai_local_name}) {
6707                !!!cp ('t409');
6708                pop @{$self->{open_elements}};
6709              }
6710    
6711              ## Step 2.
6712              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6713                      ne $token->{tag_name}) {
6714              !!!cp ('t412');              !!!cp ('t412');
6715              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);              !!!parse-error (type => 'not closed',
6716                                value => $self->{open_elements}->[-1]->[0]
6717                                    ->manakai_local_name,
6718                                token => $token);
6719            } else {            } else {
6720              !!!cp ('t413');              !!!cp ('t414');
             !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
6721            }            }
6722          }  
6723                      ## Step 3.
         if (defined $i) {  
           !!!cp ('t414');  
6724            splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
6725          } elsif ($token->{tag_name} eq 'p') {  
6726            !!!cp ('t415');            ## Step 4.
6727            ## As if <p>, then reprocess the current token            $clear_up_to_marker->()
6728            my $el;                if {
6729            !!!create-element ($el, 'p');                  applet => 1, button => 1, marquee => 1, object => 1,
6730            $insert->($el);                }->{$token->{tag_name}};
         } else {  
           !!!cp ('t416');  
6731          }          }
         $clear_up_to_marker->()  
           if {  
             button => 1, marquee => 1, object => 1,  
           }->{$token->{tag_name}};  
6732          !!!next-token;          !!!next-token;
6733          redo B;          next B;
6734        } elsif ($token->{tag_name} eq 'form') {        } elsif ($token->{tag_name} eq 'form') {
6735            undef $self->{form_element};
6736    
6737          ## has an element in scope          ## has an element in scope
6738            my $i;
6739          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6740            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6741            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] & FORM_EL) {
             ## generate implied end tags  
             while ({  
                     dd => 1, dt => 1, li => 1, p => 1,  
                    }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!cp ('t417');  
               pop @{$self->{open_elements}};  
             }  
   
6742              !!!cp ('t418');              !!!cp ('t418');
6743                $i = $_;
6744              last INSCOPE;              last INSCOPE;
6745            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6746              !!!cp ('t419');              !!!cp ('t419');
6747              last INSCOPE;              last INSCOPE;
6748            }            }
6749          } # INSCOPE          } # INSCOPE
6750            
6751          if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {          unless (defined $i) { # has an element in scope
           !!!cp ('t420');  
           pop @{$self->{open_elements}};  
         } else {  
6752            !!!cp ('t421');            !!!cp ('t421');
6753            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6754            } else {
6755              ## Step 1. generate implied end tags
6756              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
6757                !!!cp ('t417');
6758                pop @{$self->{open_elements}};
6759              }
6760              
6761              ## Step 2.
6762              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6763                      ne $token->{tag_name}) {
6764                !!!cp ('t417.1');
6765                !!!parse-error (type => 'not closed',
6766                                value => $self->{open_elements}->[-1]->[0]
6767                                    ->manakai_local_name,
6768                                token => $token);
6769              } else {
6770                !!!cp ('t420');
6771              }  
6772              
6773              ## Step 3.
6774              splice @{$self->{open_elements}}, $i;
6775          }          }
6776    
         undef $self->{form_element};  
6777          !!!next-token;          !!!next-token;
6778          redo B;          next B;
6779        } elsif ({        } elsif ({
6780                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
6781                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
# Line 6091  sub _tree_construction_main ($) { Line 6783  sub _tree_construction_main ($) {
6783          my $i;          my $i;
6784          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6785            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6786            if ({            if ($node->[1] & HEADING_EL) {
                h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
               }->{$node->[1]}) {  
             ## generate implied end tags  
             while ({  
                     dd => 1, dt => 1, li => 1, p => 1,  
                    }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!cp ('t422');  
               pop @{$self->{open_elements}};  
             }  
   
6787              !!!cp ('t423');              !!!cp ('t423');
6788              $i = $_;              $i = $_;
6789              last INSCOPE;              last INSCOPE;
6790            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6791              !!!cp ('t424');              !!!cp ('t424');
6792              last INSCOPE;              last INSCOPE;
6793            }            }
6794          } # INSCOPE          } # INSCOPE
6795    
6796            unless (defined $i) { # has an element in scope
6797              !!!cp ('t425.1');
6798              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6799            } else {
6800              ## Step 1. generate implied end tags
6801              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
6802                !!!cp ('t422');
6803                pop @{$self->{open_elements}};
6804              }
6805              
6806              ## Step 2.
6807              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6808                      ne $token->{tag_name}) {
6809                !!!cp ('t425');
6810                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6811              } else {
6812                !!!cp ('t426');
6813              }
6814    
6815              ## Step 3.
6816              splice @{$self->{open_elements}}, $i;
6817            }
6818                    
6819          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          !!!next-token;
6820            !!!cp ('t425');          next B;
6821            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});        } elsif ($token->{tag_name} eq 'p') {
6822            ## has an element in scope
6823            my $i;
6824            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6825              my $node = $self->{open_elements}->[$_];
6826              if ($node->[1] & P_EL) {
6827                !!!cp ('t410.1');
6828                $i = $_;
6829                last INSCOPE;
6830              } elsif ($node->[1] & SCOPING_EL) {
6831                !!!cp ('t411.1');
6832                last INSCOPE;
6833              }
6834            } # INSCOPE
6835    
6836            if (defined $i) {
6837              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6838                      ne $token->{tag_name}) {
6839                !!!cp ('t412.1');
6840                !!!parse-error (type => 'not closed',
6841                                value => $self->{open_elements}->[-1]->[0]
6842                                    ->manakai_local_name,
6843                                token => $token);
6844              } else {
6845                !!!cp ('t414.1');
6846              }
6847    
6848              splice @{$self->{open_elements}}, $i;
6849          } else {          } else {
6850            !!!cp ('t426');            !!!cp ('t413.1');
6851              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6852    
6853              !!!cp ('t415.1');
6854              ## As if <p>, then reprocess the current token
6855              my $el;
6856              !!!create-element ($el, $HTML_NS, 'p',, $token);
6857              $insert->($el);
6858              ## NOTE: Not inserted into |$self->{open_elements}|.
6859          }          }
6860            
         splice @{$self->{open_elements}}, $i if defined $i;  
6861          !!!next-token;          !!!next-token;
6862          redo B;          next B;
6863        } elsif ({        } elsif ({
6864                  a => 1,                  a => 1,
6865                  b => 1, big => 1, em => 1, font => 1, i => 1,                  b => 1, big => 1, em => 1, font => 1, i => 1,
# Line 6131  sub _tree_construction_main ($) { Line 6867  sub _tree_construction_main ($) {
6867                  strong => 1, tt => 1, u => 1,                  strong => 1, tt => 1, u => 1,
6868                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6869          !!!cp ('t427');          !!!cp ('t427');
6870          $formatting_end_tag->($token->{tag_name});          $formatting_end_tag->($token);
6871          redo B;          next B;
6872        } elsif ($token->{tag_name} eq 'br') {        } elsif ($token->{tag_name} eq 'br') {
6873          !!!cp ('t428');          !!!cp ('t428');
6874          !!!parse-error (type => 'unmatched end tag:br');          !!!parse-error (type => 'unmatched end tag:br', token => $token);
6875    
6876          ## As if <br>          ## As if <br>
6877          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6878                    
6879          my $el;          my $el;
6880          !!!create-element ($el, 'br');          !!!create-element ($el, $HTML_NS, 'br',, $token);
6881          $insert->($el);          $insert->($el);
6882                    
6883          ## Ignore the token.          ## Ignore the token.
6884          !!!next-token;          !!!next-token;
6885          redo B;          next B;
6886        } elsif ({        } elsif ({
6887                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
6888                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 6160  sub _tree_construction_main ($) { Line 6896  sub _tree_construction_main ($) {
6896                  noscript => 0, ## TODO: if scripting is enabled                  noscript => 0, ## TODO: if scripting is enabled
6897                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6898          !!!cp ('t429');          !!!cp ('t429');
6899          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6900          ## Ignore the token          ## Ignore the token
6901          !!!next-token;          !!!next-token;
6902          redo B;          next B;
6903                    
6904          ## ISSUE: Issue on HTML5 new elements in spec          ## ISSUE: Issue on HTML5 new elements in spec
6905                    
# Line 6174  sub _tree_construction_main ($) { Line 6910  sub _tree_construction_main ($) {
6910    
6911          ## Step 2          ## Step 2
6912          S2: {          S2: {
6913            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6914              ## Step 1              ## Step 1
6915              ## generate implied end tags              ## generate implied end tags
6916              while ({              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                     dd => 1, dt => 1, li => 1, p => 1,  
                    }->{$self->{open_elements}->[-1]->[1]}) {  
6917                !!!cp ('t430');                !!!cp ('t430');
6918                ## ISSUE: Can this case be reached?                ## ISSUE: Can this case be reached?
6919                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
6920              }              }
6921                    
6922              ## Step 2              ## Step 2
6923              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6924                        ne $token->{tag_name}) {
6925                !!!cp ('t431');                !!!cp ('t431');
6926                ## NOTE: <x><y></x>                ## NOTE: <x><y></x>
6927                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed',
6928                                  value => $self->{open_elements}->[-1]->[0]
6929                                      ->manakai_local_name,
6930                                  token => $token);
6931              } else {              } else {
6932                !!!cp ('t432');                !!!cp ('t432');
6933              }              }
# Line 6201  sub _tree_construction_main ($) { Line 6939  sub _tree_construction_main ($) {
6939              last S2;              last S2;
6940            } else {            } else {
6941              ## Step 3              ## Step 3
6942              if (not $formatting_category->{$node->[1]} and              if (not ($node->[1] & FORMATTING_EL) and
6943                  #not $phrasing_category->{$node->[1]} and                  #not $phrasing_category->{$node->[1]} and
6944                  ($special_category->{$node->[1]} or                  ($node->[1] & SPECIAL_EL or
6945                   $scoping_category->{$node->[1]})) {                   $node->[1] & SCOPING_EL)) {
6946                !!!cp ('t433');                !!!cp ('t433');
6947                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6948                ## Ignore the token                ## Ignore the token
6949                !!!next-token;                !!!next-token;
6950                last S2;                last S2;
# Line 6222  sub _tree_construction_main ($) { Line 6960  sub _tree_construction_main ($) {
6960            ## Step 5;            ## Step 5;
6961            redo S2;            redo S2;
6962          } # S2          } # S2
6963          redo B;          next B;
6964        }        }
6965      }      }
6966      redo B;      next B;
6967      } continue { # B
6968        if ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
6969          ## NOTE: The code below is executed in cases where it does not have
6970          ## to be, but it it is harmless even in those cases.
6971          ## has an element in scope
6972          INSCOPE: {
6973            for (reverse 0..$#{$self->{open_elements}}) {
6974              my $node = $self->{open_elements}->[$_];
6975              if ($node->[1] & FOREIGN_EL) {
6976                last INSCOPE;
6977              } elsif ($node->[1] & SCOPING_EL) {
6978                last;
6979              }
6980            }
6981            
6982            ## NOTE: No foreign element in scope.
6983            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
6984          } # INSCOPE
6985        }
6986    } # B    } # B
6987    
6988    ## Stop parsing # MUST    ## Stop parsing # MUST
# Line 6271  sub set_inner_html ($$$) { Line 7028  sub set_inner_html ($$$) {
7028    
7029      ## Step 8 # MUST      ## Step 8 # MUST
7030      my $i = 0;      my $i = 0;
7031      my $line = 1;      $p->{line_prev} = $p->{line} = 1;
7032      my $column = 0;      $p->{column_prev} = $p->{column} = 0;
7033      $p->{set_next_char} = sub {      $p->{set_next_char} = sub {
7034        my $self = shift;        my $self = shift;
7035    
# Line 6281  sub set_inner_html ($$$) { Line 7038  sub set_inner_html ($$$) {
7038    
7039        $self->{next_char} = -1 and return if $i >= length $$s;        $self->{next_char} = -1 and return if $i >= length $$s;
7040        $self->{next_char} = ord substr $$s, $i++, 1;        $self->{next_char} = ord substr $$s, $i++, 1;
7041        $column++;  
7042          ($p->{line_prev}, $p->{column_prev}) = ($p->{line}, $p->{column});
7043          $p->{column}++;
7044    
7045        if ($self->{next_char} == 0x000A) { # LF        if ($self->{next_char} == 0x000A) { # LF
7046          $line++;          $p->{line}++;
7047          $column = 0;          $p->{column} = 0;
7048          !!!cp ('i1');          !!!cp ('i1');
7049        } elsif ($self->{next_char} == 0x000D) { # CR        } elsif ($self->{next_char} == 0x000D) { # CR
7050          $i++ if substr ($$s, $i, 1) eq "\x0A";          $i++ if substr ($$s, $i, 1) eq "\x0A";
7051          $self->{next_char} = 0x000A; # LF # MUST          $self->{next_char} = 0x000A; # LF # MUST
7052          $line++;          $p->{line}++;
7053          $column = 0;          $p->{column} = 0;
7054          !!!cp ('i2');          !!!cp ('i2');
7055        } elsif ($self->{next_char} > 0x10FFFF) {        } elsif ($self->{next_char} > 0x10FFFF) {
7056          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
# Line 6307  sub set_inner_html ($$$) { Line 7066  sub set_inner_html ($$$) {
7066            
7067      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
7068        my (%opt) = @_;        my (%opt) = @_;
7069        warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";        my $line = $opt{line};
7070          my $column = $opt{column};
7071          if (defined $opt{token} and defined $opt{token}->{line}) {
7072            $line = $opt{token}->{line};
7073            $column = $opt{token}->{column};
7074          }
7075          warn "Parse error ($opt{type}) at line $line column $column\n";
7076      };      };
7077      $p->{parse_error} = sub {      $p->{parse_error} = sub {
7078        $ponerror->(@_, line => $line, column => $column);        $ponerror->(line => $p->{line}, column => $p->{column}, @_);
7079      };      };
7080            
7081      $p->_initialize_tokenizer;      $p->_initialize_tokenizer;
# Line 6334  sub set_inner_html ($$$) { Line 7099  sub set_inner_html ($$$) {
7099          unless defined $p->{content_model};          unless defined $p->{content_model};
7100          ## ISSUE: What is "the name of the element"? local name?          ## ISSUE: What is "the name of the element"? local name?
7101    
7102      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $el_category->{$node_ln}];
7103          ## TODO: Foreign element OK?
7104    
7105      ## Step 3      ## Step 3
7106      my $root = $doc->create_element_ns      my $root = $doc->create_element_ns
# Line 6344  sub set_inner_html ($$$) { Line 7110  sub set_inner_html ($$$) {
7110      $doc->append_child ($root);      $doc->append_child ($root);
7111    
7112      ## Step 5 # MUST      ## Step 5 # MUST
7113      push @{$p->{open_elements}}, [$root, 'html'];      push @{$p->{open_elements}}, [$root, $el_category->{html}];
7114    
7115      undef $p->{head_element};      undef $p->{head_element};
7116    
# Line 6390  sub set_inner_html ($$$) { Line 7156  sub set_inner_html ($$$) {
7156      ## ISSUE: mutation events?      ## ISSUE: mutation events?
7157    
7158      $p->_terminate_tree_constructor;      $p->_terminate_tree_constructor;
7159    
7160        delete $p->{parse_error}; # delete loop
7161    } else {    } else {
7162      die "$0: |set_inner_html| is not defined for node of type $nt";      die "$0: |set_inner_html| is not defined for node of type $nt";
7163    }    }

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

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24