/[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.56 by wakaba, Sat Aug 11 07:19:18 2007 UTC revision 1.126 by wakaba, Sat Apr 12 14:54:33 2008 UTC
# Line 1  Line 1 
1  package Whatpm::HTML;  package Whatpm::HTML;
2  use strict;  use strict;
3  our $VERSION=do{my @r=(q$Revision$=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};  our $VERSION=do{my @r=(q$Revision$=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};
4    use Error qw(:try);
5    
6  ## ISSUE:  ## ISSUE:
7  ## var doc = implementation.createDocument (null, null, null);  ## var doc = implementation.createDocument (null, null, null);
8  ## doc.write ('');  ## doc.write ('');
9  ## alert (doc.compatMode);  ## alert (doc.compatMode);
10    
11  ## ISSUE: HTML5 revision 967 says that the encoding layer MUST NOT  ## TODO: Control charcters and noncharacters are not allowed (HTML5 revision 1263)
12  ## strip BOM and the HTML layer MUST ignore it.  Whether we can do it  ## TODO: 1252 parse error (revision 1264)
13  ## is not yet clear.  ## TODO: 8859-11 = 874 (revision 1271)
14  ## "{U+FEFF}..." in UTF-16BE/UTF-16LE is three or four characters?  
15  ## "{U+FEFF}..." in GB18030?  my $HTML_NS = q<http://www.w3.org/1999/xhtml>;
16    my $MML_NS = q<http://www.w3.org/1998/Math/MathML>;
17  my $permitted_slash_tag_name = {  my $SVG_NS = q<http://www.w3.org/2000/svg>;
18    base => 1,  my $XLINK_NS = q<http://www.w3.org/1999/xlink>;
19    link => 1,  my $XML_NS = q<http://www.w3.org/XML/1998/namespace>;
20    meta => 1,  my $XMLNS_NS = q<http://www.w3.org/2000/xmlns/>;
21    hr => 1,  
22    br => 1,  sub A_EL () { 0b1 }
23    img=> 1,  sub ADDRESS_EL () { 0b10 }
24    embed => 1,  sub BODY_EL () { 0b100 }
25    param => 1,  sub BUTTON_EL () { 0b1000 }
26    area => 1,  sub CAPTION_EL () { 0b10000 }
27    col => 1,  sub DD_EL () { 0b100000 }
28    input => 1,  sub DIV_EL () { 0b1000000 }
29    sub DT_EL () { 0b10000000 }
30    sub FORM_EL () { 0b100000000 }
31    sub FORMATTING_EL () { 0b1000000000 }
32    sub FRAMESET_EL () { 0b10000000000 }
33    sub HEADING_EL () { 0b100000000000 }
34    sub HTML_EL () { 0b1000000000000 }
35    sub LI_EL () { 0b10000000000000 }
36    sub NOBR_EL () { 0b100000000000000 }
37    sub OPTION_EL () { 0b1000000000000000 }
38    sub OPTGROUP_EL () { 0b10000000000000000 }
39    sub P_EL () { 0b100000000000000000 }
40    sub SELECT_EL () { 0b1000000000000000000 }
41    sub TABLE_EL () { 0b10000000000000000000 }
42    sub TABLE_CELL_EL () { 0b100000000000000000000 }
43    sub TABLE_ROW_EL () { 0b1000000000000000000000 }
44    sub TABLE_ROW_GROUP_EL () { 0b10000000000000000000000 }
45    sub MISC_SCOPING_EL () { 0b100000000000000000000000 }
46    sub MISC_SPECIAL_EL () { 0b1000000000000000000000000 }
47    sub FOREIGN_EL () { 0b10000000000000000000000000 }
48    sub FOREIGN_FLOW_CONTENT_EL () { 0b100000000000000000000000000 }
49    sub MML_AXML_EL () { 0b1000000000000000000000000000 }
50    
51    sub TABLE_ROWS_EL () {
52      TABLE_EL |
53      TABLE_ROW_EL |
54      TABLE_ROW_GROUP_EL
55    }
56    
57    sub END_TAG_OPTIONAL_EL () {
58      DD_EL |
59      DT_EL |
60      LI_EL |
61      P_EL
62    }
63    
64    sub ALL_END_TAG_OPTIONAL_EL () {
65      END_TAG_OPTIONAL_EL |
66      BODY_EL |
67      HTML_EL |
68      TABLE_CELL_EL |
69      TABLE_ROW_EL |
70      TABLE_ROW_GROUP_EL
71    }
72    
73    sub SCOPING_EL () {
74      BUTTON_EL |
75      CAPTION_EL |
76      HTML_EL |
77      TABLE_EL |
78      TABLE_CELL_EL |
79      MISC_SCOPING_EL
80    }
81    
82    sub TABLE_SCOPING_EL () {
83      HTML_EL |
84      TABLE_EL
85    }
86    
87    sub TABLE_ROWS_SCOPING_EL () {
88      HTML_EL |
89      TABLE_ROW_GROUP_EL
90    }
91    
92    sub TABLE_ROW_SCOPING_EL () {
93      HTML_EL |
94      TABLE_ROW_EL
95    }
96    
97    sub SPECIAL_EL () {
98      ADDRESS_EL |
99      BODY_EL |
100      DIV_EL |
101      END_TAG_OPTIONAL_EL |
102      FORM_EL |
103      FRAMESET_EL |
104      HEADING_EL |
105      OPTION_EL |
106      OPTGROUP_EL |
107      SELECT_EL |
108      TABLE_ROW_EL |
109      TABLE_ROW_GROUP_EL |
110      MISC_SPECIAL_EL
111    }
112    
113    my $el_category = {
114      a => A_EL | FORMATTING_EL,
115      address => ADDRESS_EL,
116      applet => MISC_SCOPING_EL,
117      area => MISC_SPECIAL_EL,
118      b => FORMATTING_EL,
119      base => MISC_SPECIAL_EL,
120      basefont => MISC_SPECIAL_EL,
121      bgsound => MISC_SPECIAL_EL,
122      big => FORMATTING_EL,
123      blockquote => MISC_SPECIAL_EL,
124      body => BODY_EL,
125      br => MISC_SPECIAL_EL,
126      button => BUTTON_EL,
127      caption => CAPTION_EL,
128      center => MISC_SPECIAL_EL,
129      col => MISC_SPECIAL_EL,
130      colgroup => MISC_SPECIAL_EL,
131      dd => DD_EL,
132      dir => MISC_SPECIAL_EL,
133      div => DIV_EL,
134      dl => MISC_SPECIAL_EL,
135      dt => DT_EL,
136      em => FORMATTING_EL,
137      embed => MISC_SPECIAL_EL,
138      fieldset => MISC_SPECIAL_EL,
139      font => FORMATTING_EL,
140      form => FORM_EL,
141      frame => MISC_SPECIAL_EL,
142      frameset => FRAMESET_EL,
143      h1 => HEADING_EL,
144      h2 => HEADING_EL,
145      h3 => HEADING_EL,
146      h4 => HEADING_EL,
147      h5 => HEADING_EL,
148      h6 => HEADING_EL,
149      head => MISC_SPECIAL_EL,
150      hr => MISC_SPECIAL_EL,
151      html => HTML_EL,
152      i => FORMATTING_EL,
153      iframe => MISC_SPECIAL_EL,
154      img => MISC_SPECIAL_EL,
155      input => MISC_SPECIAL_EL,
156      isindex => MISC_SPECIAL_EL,
157      li => LI_EL,
158      link => MISC_SPECIAL_EL,
159      listing => MISC_SPECIAL_EL,
160      marquee => MISC_SCOPING_EL,
161      menu => MISC_SPECIAL_EL,
162      meta => MISC_SPECIAL_EL,
163      nobr => NOBR_EL | FORMATTING_EL,
164      noembed => MISC_SPECIAL_EL,
165      noframes => MISC_SPECIAL_EL,
166      noscript => MISC_SPECIAL_EL,
167      object => MISC_SCOPING_EL,
168      ol => MISC_SPECIAL_EL,
169      optgroup => OPTGROUP_EL,
170      option => OPTION_EL,
171      p => P_EL,
172      param => MISC_SPECIAL_EL,
173      plaintext => MISC_SPECIAL_EL,
174      pre => MISC_SPECIAL_EL,
175      s => FORMATTING_EL,
176      script => MISC_SPECIAL_EL,
177      select => SELECT_EL,
178      small => FORMATTING_EL,
179      spacer => MISC_SPECIAL_EL,
180      strike => FORMATTING_EL,
181      strong => FORMATTING_EL,
182      style => MISC_SPECIAL_EL,
183      table => TABLE_EL,
184      tbody => TABLE_ROW_GROUP_EL,
185      td => TABLE_CELL_EL,
186      textarea => MISC_SPECIAL_EL,
187      tfoot => TABLE_ROW_GROUP_EL,
188      th => TABLE_CELL_EL,
189      thead => TABLE_ROW_GROUP_EL,
190      title => MISC_SPECIAL_EL,
191      tr => TABLE_ROW_EL,
192      tt => FORMATTING_EL,
193      u => FORMATTING_EL,
194      ul => MISC_SPECIAL_EL,
195      wbr => MISC_SPECIAL_EL,
196    };
197    
198    my $el_category_f = {
199      $MML_NS => {
200        'annotation-xml' => MML_AXML_EL,
201        mi => FOREIGN_FLOW_CONTENT_EL,
202        mo => FOREIGN_FLOW_CONTENT_EL,
203        mn => FOREIGN_FLOW_CONTENT_EL,
204        ms => FOREIGN_FLOW_CONTENT_EL,
205        mtext => FOREIGN_FLOW_CONTENT_EL,
206      },
207      $SVG_NS => {
208        foreignobject => FOREIGN_FLOW_CONTENT_EL, ## TODO: case
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 $c1_entity_char = {  my $c1_entity_char = {
# Line 62  my $c1_entity_char = { Line 247  my $c1_entity_char = {
247    0x9F => 0x0178,    0x9F => 0x0178,
248  }; # $c1_entity_char  }; # $c1_entity_char
249    
250  my $special_category = {  sub parse_byte_string ($$$$;$) {
251    address => 1, area => 1, base => 1, basefont => 1, bgsound => 1,    my $self = ref $_[0] ? shift : shift->new;
252    blockquote => 1, body => 1, br => 1, center => 1, col => 1, colgroup => 1,    my $charset = shift;
253    dd => 1, dir => 1, div => 1, dl => 1, dt => 1, embed => 1, fieldset => 1,    my $bytes_s = ref $_[0] ? $_[0] : \($_[0]);
254    form => 1, frame => 1, frameset => 1, h1 => 1, h2 => 1, h3 => 1,    my $s;
255    h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, iframe => 1, image => 1,    
256    img => 1, input => 1, isindex => 1, li => 1, link => 1, listing => 1,    if (defined $charset) {
257    menu => 1, meta => 1, noembed => 1, noframes => 1, noscript => 1,      require Encode; ## TODO: decode(utf8) don't delete BOM
258    ol => 1, optgroup => 1, option => 1, p => 1, param => 1, plaintext => 1,      $s = \ (Encode::decode ($charset, $$bytes_s));
259    pre => 1, script => 1, select => 1, spacer => 1, style => 1, tbody => 1,      $self->{input_encoding} = lc $charset; ## TODO: normalize name
260    textarea => 1, tfoot => 1, thead => 1, title => 1, tr => 1, ul => 1, wbr => 1,      $self->{confident} = 1;
261  };    } else {
262  my $scoping_category = {      ## TODO: Implement HTML5 detection algorithm
263    button => 1, caption => 1, html => 1, marquee => 1, object => 1,      require Whatpm::Charset::UniversalCharDet;
264    table => 1, td => 1, th => 1,      $charset = Whatpm::Charset::UniversalCharDet->detect_byte_string
265  };          (substr ($$bytes_s, 0, 1024));
266  my $formatting_category = {      $charset ||= 'windows-1252';
267    a => 1, b => 1, big => 1, em => 1, font => 1, i => 1, nobr => 1,      $s = \ (Encode::decode ($charset, $$bytes_s));
268    s => 1, small => 1, strile => 1, strong => 1, tt => 1, u => 1,      $self->{input_encoding} = $charset;
269  };      $self->{confident} = 0;
270  # $phrasing_category: all other elements    }
271    
272      $self->{change_encoding} = sub {
273        my $self = shift;
274        my $charset = lc shift;
275        my $token = shift;
276        ## TODO: if $charset is supported
277        ## TODO: normalize charset name
278    
279        ## "Change the encoding" algorithm:
280    
281        ## Step 1    
282        if ($charset eq 'utf-16') { ## ISSUE: UTF-16BE -> UTF-8? UTF-16LE -> UTF-8?
283          $charset = 'utf-8';
284        }
285    
286        ## Step 2
287        if (defined $self->{input_encoding} and
288            $self->{input_encoding} eq $charset) {
289          $self->{confident} = 1;
290          return;
291        }
292    
293        !!!parse-error (type => 'charset label detected:'.$self->{input_encoding}.
294            ':'.$charset, level => 'w', token => $token);
295    
296        ## Step 3
297        # if (can) {
298          ## change the encoding on the fly.
299          #$self->{confident} = 1;
300          #return;
301        # }
302    
303        ## Step 4
304        throw Whatpm::HTML::RestartParser (charset => $charset);
305      }; # $self->{change_encoding}
306    
307      my @args = @_; shift @args; # $s
308      my $return;
309      try {
310        $return = $self->parse_char_string ($s, @args);  
311      } catch Whatpm::HTML::RestartParser with {
312        my $charset = shift->{charset};
313        $s = \ (Encode::decode ($charset, $$bytes_s));    
314        $self->{input_encoding} = $charset; ## TODO: normalize
315        $self->{confident} = 1;
316        $return = $self->parse_char_string ($s, @args);
317      };
318      return $return;
319    } # parse_byte_string
320    
321    ## NOTE: HTML5 spec says that the encoding layer MUST NOT strip BOM
322    ## and the HTML layer MUST ignore it.  However, we does strip BOM in
323    ## the encoding layer and the HTML layer does not ignore any U+FEFF,
324    ## because the core part of our HTML parser expects a string of character,
325    ## not a string of bytes or code units or anything which might contain a BOM.
326    ## Therefore, any parser interface that accepts a string of bytes,
327    ## such as |parse_byte_string| in this module, must ensure that it does
328    ## strip the BOM and never strip any ZWNBSP.
329    
330    *parse_char_string = \&parse_string;
331    
332  sub parse_string ($$$;$) {  sub parse_string ($$$;$) {
333    my $self = shift->new;    my $self = ref $_[0] ? shift : shift->new;
334    my $s = \$_[0];    my $s = ref $_[0] ? $_[0] : \($_[0]);
335    $self->{document} = $_[1];    $self->{document} = $_[1];
336      @{$self->{document}->child_nodes} = ();
337    
338    ## NOTE: |set_inner_html| copies most of this method's code    ## NOTE: |set_inner_html| copies most of this method's code
339    
340      $self->{confident} = 1 unless exists $self->{confident};
341      $self->{document}->input_encoding ($self->{input_encoding})
342          if defined $self->{input_encoding};
343    
344    my $i = 0;    my $i = 0;
345    my $line = 1;    $self->{line_prev} = $self->{line} = 1;
346    my $column = 0;    $self->{column_prev} = $self->{column} = 0;
347    $self->{set_next_input_character} = sub {    $self->{set_next_char} = sub {
348      my $self = shift;      my $self = shift;
349    
350      pop @{$self->{prev_input_character}};      pop @{$self->{prev_char}};
351      unshift @{$self->{prev_input_character}}, $self->{next_input_character};      unshift @{$self->{prev_char}}, $self->{next_char};
352    
353      $self->{next_input_character} = -1 and return if $i >= length $$s;      $self->{next_char} = -1 and return if $i >= length $$s;
354      $self->{next_input_character} = ord substr $$s, $i++, 1;      $self->{next_char} = ord substr $$s, $i++, 1;
355      $column++;  
356        ($self->{line_prev}, $self->{column_prev})
357            = ($self->{line}, $self->{column});
358        $self->{column}++;
359            
360      if ($self->{next_input_character} == 0x000A) { # LF      if ($self->{next_char} == 0x000A) { # LF
361        $line++;        $self->{line}++;
362        $column = 0;        $self->{column} = 0;
363      } elsif ($self->{next_input_character} == 0x000D) { # CR      } elsif ($self->{next_char} == 0x000D) { # CR
364        $i++ if substr ($$s, $i, 1) eq "\x0A";        $i++ if substr ($$s, $i, 1) eq "\x0A";
365        $self->{next_input_character} = 0x000A; # LF # MUST        $self->{next_char} = 0x000A; # LF # MUST
366        $line++;        $self->{line}++;
367        $column = 0;        $self->{column} = 0;
368      } elsif ($self->{next_input_character} > 0x10FFFF) {      } elsif ($self->{next_char} > 0x10FFFF) {
369        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
370      } elsif ($self->{next_input_character} == 0x0000) { # NULL      } elsif ($self->{next_char} == 0x0000) { # NULL
371        !!!parse-error (type => 'NULL');        !!!parse-error (type => 'NULL');
372        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
373      }      }
374    };    };
375    $self->{prev_input_character} = [-1, -1, -1];    $self->{prev_char} = [-1, -1, -1];
376    $self->{next_input_character} = -1;    $self->{next_char} = -1;
377    
378    my $onerror = $_[2] || sub {    my $onerror = $_[2] || sub {
379      my (%opt) = @_;      my (%opt) = @_;
380      warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";      my $line = $opt{token} ? $opt{token}->{line} : $opt{line};
381        my $column = $opt{token} ? $opt{token}->{column} : $opt{column};
382        warn "Parse error ($opt{type}) at line $line column $column\n";
383    };    };
384    $self->{parse_error} = sub {    $self->{parse_error} = sub {
385      $onerror->(@_, line => $line, column => $column);      $onerror->(line => $self->{line}, column => $self->{column}, @_);
386    };    };
387    
388    $self->_initialize_tokenizer;    $self->_initialize_tokenizer;
# Line 135  sub parse_string ($$$;$) { Line 390  sub parse_string ($$$;$) {
390    $self->_construct_tree;    $self->_construct_tree;
391    $self->_terminate_tree_constructor;    $self->_terminate_tree_constructor;
392    
393      delete $self->{parse_error}; # remove loop
394    
395    return $self->{document};    return $self->{document};
396  } # parse_string  } # parse_string
397    
398  sub new ($) {  sub new ($) {
399    my $class = shift;    my $class = shift;
400    my $self = bless {}, $class;    my $self = bless {}, $class;
401    $self->{set_next_input_character} = sub {    $self->{set_next_char} = sub {
402      $self->{next_input_character} = -1;      $self->{next_char} = -1;
403    };    };
404    $self->{parse_error} = sub {    $self->{parse_error} = sub {
405      #      #
406    };    };
407      $self->{change_encoding} = sub {
408        # if ($_[0] is a supported encoding) {
409        #   run "change the encoding" algorithm;
410        #   throw Whatpm::HTML::RestartParser (charset => $new_encoding);
411        # }
412      };
413      $self->{application_cache_selection} = sub {
414        #
415      };
416    return $self;    return $self;
417  } # new  } # new
418    
# Line 159  sub CDATA_CONTENT_MODEL () { CM_LIMITED_ Line 425  sub CDATA_CONTENT_MODEL () { CM_LIMITED_
425  sub RCDATA_CONTENT_MODEL () { CM_ENTITY | CM_LIMITED_MARKUP }  sub RCDATA_CONTENT_MODEL () { CM_ENTITY | CM_LIMITED_MARKUP }
426  sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }  sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }
427    
428    sub DATA_STATE () { 0 }
429    sub ENTITY_DATA_STATE () { 1 }
430    sub TAG_OPEN_STATE () { 2 }
431    sub CLOSE_TAG_OPEN_STATE () { 3 }
432    sub TAG_NAME_STATE () { 4 }
433    sub BEFORE_ATTRIBUTE_NAME_STATE () { 5 }
434    sub ATTRIBUTE_NAME_STATE () { 6 }
435    sub AFTER_ATTRIBUTE_NAME_STATE () { 7 }
436    sub BEFORE_ATTRIBUTE_VALUE_STATE () { 8 }
437    sub ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE () { 9 }
438    sub ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE () { 10 }
439    sub ATTRIBUTE_VALUE_UNQUOTED_STATE () { 11 }
440    sub ENTITY_IN_ATTRIBUTE_VALUE_STATE () { 12 }
441    sub MARKUP_DECLARATION_OPEN_STATE () { 13 }
442    sub COMMENT_START_STATE () { 14 }
443    sub COMMENT_START_DASH_STATE () { 15 }
444    sub COMMENT_STATE () { 16 }
445    sub COMMENT_END_STATE () { 17 }
446    sub COMMENT_END_DASH_STATE () { 18 }
447    sub BOGUS_COMMENT_STATE () { 19 }
448    sub DOCTYPE_STATE () { 20 }
449    sub BEFORE_DOCTYPE_NAME_STATE () { 21 }
450    sub DOCTYPE_NAME_STATE () { 22 }
451    sub AFTER_DOCTYPE_NAME_STATE () { 23 }
452    sub BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE () { 24 }
453    sub DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE () { 25 }
454    sub DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE () { 26 }
455    sub AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE () { 27 }
456    sub BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 28 }
457    sub DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE () { 29 }
458    sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE () { 30 }
459    sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }
460    sub BOGUS_DOCTYPE_STATE () { 32 }
461    sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }
462    sub SELF_CLOSING_START_TAG_STATE () { 34 }
463    
464  sub DOCTYPE_TOKEN () { 1 }  sub DOCTYPE_TOKEN () { 1 }
465  sub COMMENT_TOKEN () { 2 }  sub COMMENT_TOKEN () { 2 }
466  sub START_TAG_TOKEN () { 3 }  sub START_TAG_TOKEN () { 3 }
# Line 174  sub TABLE_IMS ()      { 0b1000000 } Line 476  sub TABLE_IMS ()      { 0b1000000 }
476  sub ROW_IMS ()        { 0b10000000 }  sub ROW_IMS ()        { 0b10000000 }
477  sub BODY_AFTER_IMS () { 0b100000000 }  sub BODY_AFTER_IMS () { 0b100000000 }
478  sub FRAME_IMS ()      { 0b1000000000 }  sub FRAME_IMS ()      { 0b1000000000 }
479    sub SELECT_IMS ()     { 0b10000000000 }
480    sub IN_FOREIGN_CONTENT_IM () { 0b100000000000 }
481        ## NOTE: "in foreign content" insertion mode is special; it is combined
482        ## with the secondary insertion mode.  In this parser, they are stored
483        ## together in the bit-or'ed form.
484    
485    ## NOTE: "initial" and "before html" insertion modes have no constants.
486    
487    ## NOTE: "after after body" insertion mode.
488  sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }  sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }
489    
490    ## NOTE: "after after frameset" insertion mode.
491  sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }  sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }
492    
493  sub IN_HEAD_IM () { HEAD_IMS | 0b00 }  sub IN_HEAD_IM () { HEAD_IMS | 0b00 }
494  sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }  sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }
495  sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }  sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }
# Line 190  sub IN_TABLE_IM () { TABLE_IMS } Line 503  sub IN_TABLE_IM () { TABLE_IMS }
503  sub AFTER_BODY_IM () { BODY_AFTER_IMS }  sub AFTER_BODY_IM () { BODY_AFTER_IMS }
504  sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }  sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }
505  sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }  sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }
506  sub IN_SELECT_IM () { 0b01 }  sub IN_SELECT_IM () { SELECT_IMS | 0b01 }
507    sub IN_SELECT_IN_TABLE_IM () { SELECT_IMS | 0b10 }
508  sub IN_COLUMN_GROUP_IM () { 0b10 }  sub IN_COLUMN_GROUP_IM () { 0b10 }
509    
510  ## Implementations MUST act as if state machine in the spec  ## Implementations MUST act as if state machine in the spec
511    
512  sub _initialize_tokenizer ($) {  sub _initialize_tokenizer ($) {
513    my $self = shift;    my $self = shift;
514    $self->{state} = 'data'; # MUST    $self->{state} = DATA_STATE; # MUST
515    $self->{content_model} = PCDATA_CONTENT_MODEL; # be    $self->{content_model} = PCDATA_CONTENT_MODEL; # be
516    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE
517    undef $self->{current_attribute};    undef $self->{current_attribute};
518    undef $self->{last_emitted_start_tag_name};    undef $self->{last_emitted_start_tag_name};
519    undef $self->{last_attribute_value_state};    undef $self->{last_attribute_value_state};
520      delete $self->{self_closing};
521    $self->{char} = [];    $self->{char} = [];
522    # $self->{next_input_character}    # $self->{next_char}
523    !!!next-input-character;    !!!next-input-character;
524    $self->{token} = [];    $self->{token} = [];
525    # $self->{escape}    # $self->{escape}
# Line 217  sub _initialize_tokenizer ($) { Line 532  sub _initialize_tokenizer ($) {
532  ##   ->{tag_name} (START_TAG_TOKEN, END_TAG_TOKEN)  ##   ->{tag_name} (START_TAG_TOKEN, END_TAG_TOKEN)
533  ##   ->{public_identifier} (DOCTYPE_TOKEN)  ##   ->{public_identifier} (DOCTYPE_TOKEN)
534  ##   ->{system_identifier} (DOCTYPE_TOKEN)  ##   ->{system_identifier} (DOCTYPE_TOKEN)
535  ##   ->{correct} == 1 or 0 (DOCTYPE_TOKEN)  ##   ->{quirks} == 1 or 0 (DOCTYPE_TOKEN): "force-quirks" flag
536  ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)  ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)
537    ##        ->{name}
538    ##        ->{value}
539    ##        ->{has_reference} == 1 or 0
540  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
541    ## NOTE: The "self-closing flag" is hold as |$self->{self_closing}|.
542    ##     |->{self_closing}| is used to save the value of |$self->{self_closing}|
543    ##     while the token is pushed back to the stack.
544    
545    ## ISSUE: "When a DOCTYPE token is created, its
546    ## <i>self-closing flag</i> must be unset (its other state is that it
547    ## be set), and its attributes list must be empty.": Wrong subject?
548    
549  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
550    
# Line 229  sub _initialize_tokenizer ($) { Line 554  sub _initialize_tokenizer ($) {
554  ## has completed loading.  If one has, then it MUST be executed  ## has completed loading.  If one has, then it MUST be executed
555  ## and removed from the list.  ## and removed from the list.
556    
557    ## NOTE: HTML5 "Writing HTML documents" section, applied to
558    ## documents and not to user agents and conformance checkers,
559    ## contains some requirements that are not detected by the
560    ## parsing algorithm:
561    ## - Some requirements on character encoding declarations. ## TODO
562    ## - "Elements MUST NOT contain content that their content model disallows."
563    ##   ... Some are parse error, some are not (will be reported by c.c.).
564    ## - Polytheistic slash SHOULD NOT be used. (Applied only to atheists.) ## TODO
565    ## - Text (in elements, attributes, and comments) SHOULD NOT contain
566    ##   control characters other than space characters. ## TODO: (what is control character? C0, C1 and DEL?  Unicode control character?)
567    
568    ## TODO: HTML5 poses authors two SHOULD-level requirements that cannot
569    ## be detected by the HTML5 parsing algorithm:
570    ## - Text,
571    
572  sub _get_next_token ($) {  sub _get_next_token ($) {
573    my $self = shift;    my $self = shift;
574    
575      if ($self->{self_closing}) {
576        !!!parse-error (type => 'nestc', token => $self->{current_token});
577        ## NOTE: The |self_closing| flag is only set by start tag token.
578        ## In addition, when a start tag token is emitted, it is always set to
579        ## |current_token|.
580        delete $self->{self_closing};
581      }
582    
583    if (@{$self->{token}}) {    if (@{$self->{token}}) {
584        $self->{self_closing} = $self->{token}->[0]->{self_closing};
585      return shift @{$self->{token}};      return shift @{$self->{token}};
586    }    }
587    
588    A: {    A: {
589      if ($self->{state} eq 'data') {      if ($self->{state} == DATA_STATE) {
590        if ($self->{next_input_character} == 0x0026) { # &        if ($self->{next_char} == 0x0026) { # &
591          if ($self->{content_model} & CM_ENTITY) { # PCDATA | RCDATA          if ($self->{content_model} & CM_ENTITY and # PCDATA | RCDATA
592            $self->{state} = 'entity data';              not $self->{escape}) {
593              !!!cp (1);
594              $self->{state} = ENTITY_DATA_STATE;
595            !!!next-input-character;            !!!next-input-character;
596            redo A;            redo A;
597          } else {          } else {
598              !!!cp (2);
599            #            #
600          }          }
601        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
602          if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA          if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
603            unless ($self->{escape}) {            unless ($self->{escape}) {
604              if ($self->{prev_input_character}->[0] == 0x002D and # -              if ($self->{prev_char}->[0] == 0x002D and # -
605                  $self->{prev_input_character}->[1] == 0x0021 and # !                  $self->{prev_char}->[1] == 0x0021 and # !
606                  $self->{prev_input_character}->[2] == 0x003C) { # <                  $self->{prev_char}->[2] == 0x003C) { # <
607                  !!!cp (3);
608                $self->{escape} = 1;                $self->{escape} = 1;
609                } else {
610                  !!!cp (4);
611              }              }
612              } else {
613                !!!cp (5);
614            }            }
615          }          }
616                    
617          #          #
618        } elsif ($self->{next_input_character} == 0x003C) { # <        } elsif ($self->{next_char} == 0x003C) { # <
619          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA
620              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA
621               not $self->{escape})) {               not $self->{escape})) {
622            $self->{state} = 'tag open';            !!!cp (6);
623              $self->{state} = TAG_OPEN_STATE;
624            !!!next-input-character;            !!!next-input-character;
625            redo A;            redo A;
626          } else {          } else {
627              !!!cp (7);
628            #            #
629          }          }
630        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
631          if ($self->{escape} and          if ($self->{escape} and
632              ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA              ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA
633            if ($self->{prev_input_character}->[0] == 0x002D and # -            if ($self->{prev_char}->[0] == 0x002D and # -
634                $self->{prev_input_character}->[1] == 0x002D) { # -                $self->{prev_char}->[1] == 0x002D) { # -
635                !!!cp (8);
636              delete $self->{escape};              delete $self->{escape};
637              } else {
638                !!!cp (9);
639            }            }
640            } else {
641              !!!cp (10);
642          }          }
643                    
644          #          #
645        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
646          !!!emit ({type => END_OF_FILE_TOKEN});          !!!cp (11);
647            !!!emit ({type => END_OF_FILE_TOKEN,
648                      line => $self->{line}, column => $self->{column}});
649          last A; ## TODO: ok?          last A; ## TODO: ok?
650          } else {
651            !!!cp (12);
652        }        }
653        # Anything else        # Anything else
654        my $token = {type => CHARACTER_TOKEN,        my $token = {type => CHARACTER_TOKEN,
655                     data => chr $self->{next_input_character}};                     data => chr $self->{next_char},
656                       line => $self->{line}, column => $self->{column},
657                      };
658        ## Stay in the data state        ## Stay in the data state
659        !!!next-input-character;        !!!next-input-character;
660    
661        !!!emit ($token);        !!!emit ($token);
662    
663        redo A;        redo A;
664      } elsif ($self->{state} eq 'entity data') {      } elsif ($self->{state} == ENTITY_DATA_STATE) {
665        ## (cannot happen in CDATA state)        ## (cannot happen in CDATA state)
666    
667          my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
668                
669        my $token = $self->_tokenize_attempt_to_consume_an_entity (0);        my $token = $self->_tokenize_attempt_to_consume_an_entity (0, -1);
670    
671        $self->{state} = 'data';        $self->{state} = DATA_STATE;
672        # next-input-character is already done        # next-input-character is already done
673    
674        unless (defined $token) {        unless (defined $token) {
675          !!!emit ({type => CHARACTER_TOKEN, data => '&'});          !!!cp (13);
676            !!!emit ({type => CHARACTER_TOKEN, data => '&',
677                      line => $l, column => $c,
678                     });
679        } else {        } else {
680            !!!cp (14);
681          !!!emit ($token);          !!!emit ($token);
682        }        }
683    
684        redo A;        redo A;
685      } elsif ($self->{state} eq 'tag open') {      } elsif ($self->{state} == TAG_OPEN_STATE) {
686        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
687          if ($self->{next_input_character} == 0x002F) { # /          if ($self->{next_char} == 0x002F) { # /
688              !!!cp (15);
689            !!!next-input-character;            !!!next-input-character;
690            $self->{state} = 'close tag open';            $self->{state} = CLOSE_TAG_OPEN_STATE;
691            redo A;            redo A;
692          } else {          } else {
693              !!!cp (16);
694            ## reconsume            ## reconsume
695            $self->{state} = 'data';            $self->{state} = DATA_STATE;
696    
697            !!!emit ({type => CHARACTER_TOKEN, data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
698                        line => $self->{line_prev},
699                        column => $self->{column_prev},
700                       });
701    
702            redo A;            redo A;
703          }          }
704        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA
705          if ($self->{next_input_character} == 0x0021) { # !          if ($self->{next_char} == 0x0021) { # !
706            $self->{state} = 'markup declaration open';            !!!cp (17);
707              $self->{state} = MARKUP_DECLARATION_OPEN_STATE;
708            !!!next-input-character;            !!!next-input-character;
709            redo A;            redo A;
710          } elsif ($self->{next_input_character} == 0x002F) { # /          } elsif ($self->{next_char} == 0x002F) { # /
711            $self->{state} = 'close tag open';            !!!cp (18);
712              $self->{state} = CLOSE_TAG_OPEN_STATE;
713            !!!next-input-character;            !!!next-input-character;
714            redo A;            redo A;
715          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_char} and
716                   $self->{next_input_character} <= 0x005A) { # A..Z                   $self->{next_char} <= 0x005A) { # A..Z
717              !!!cp (19);
718            $self->{current_token}            $self->{current_token}
719              = {type => START_TAG_TOKEN,              = {type => START_TAG_TOKEN,
720                 tag_name => chr ($self->{next_input_character} + 0x0020)};                 tag_name => chr ($self->{next_char} + 0x0020),
721            $self->{state} = 'tag name';                 line => $self->{line_prev},
722                   column => $self->{column_prev}};
723              $self->{state} = TAG_NAME_STATE;
724            !!!next-input-character;            !!!next-input-character;
725            redo A;            redo A;
726          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_char} and
727                   $self->{next_input_character} <= 0x007A) { # a..z                   $self->{next_char} <= 0x007A) { # a..z
728              !!!cp (20);
729            $self->{current_token} = {type => START_TAG_TOKEN,            $self->{current_token} = {type => START_TAG_TOKEN,
730                              tag_name => chr ($self->{next_input_character})};                                      tag_name => chr ($self->{next_char}),
731            $self->{state} = 'tag name';                                      line => $self->{line_prev},
732                                        column => $self->{column_prev}};
733              $self->{state} = TAG_NAME_STATE;
734            !!!next-input-character;            !!!next-input-character;
735            redo A;            redo A;
736          } elsif ($self->{next_input_character} == 0x003E) { # >          } elsif ($self->{next_char} == 0x003E) { # >
737            !!!parse-error (type => 'empty start tag');            !!!cp (21);
738            $self->{state} = 'data';            !!!parse-error (type => 'empty start tag',
739                              line => $self->{line_prev},
740                              column => $self->{column_prev});
741              $self->{state} = DATA_STATE;
742            !!!next-input-character;            !!!next-input-character;
743    
744            !!!emit ({type => CHARACTER_TOKEN, data => '<>'});            !!!emit ({type => CHARACTER_TOKEN, data => '<>',
745                        line => $self->{line_prev},
746                        column => $self->{column_prev},
747                       });
748    
749            redo A;            redo A;
750          } elsif ($self->{next_input_character} == 0x003F) { # ?          } elsif ($self->{next_char} == 0x003F) { # ?
751            !!!parse-error (type => 'pio');            !!!cp (22);
752            $self->{state} = 'bogus comment';            !!!parse-error (type => 'pio',
753            ## $self->{next_input_character} is intentionally left as is                            line => $self->{line_prev},
754                              column => $self->{column_prev});
755              $self->{state} = BOGUS_COMMENT_STATE;
756              $self->{current_token} = {type => COMMENT_TOKEN, data => '',
757                                        line => $self->{line_prev},
758                                        column => $self->{column_prev},
759                                       };
760              ## $self->{next_char} is intentionally left as is
761            redo A;            redo A;
762          } else {          } else {
763              !!!cp (23);
764            !!!parse-error (type => 'bare stago');            !!!parse-error (type => 'bare stago');
765            $self->{state} = 'data';            $self->{state} = DATA_STATE;
766            ## reconsume            ## reconsume
767    
768            !!!emit ({type => CHARACTER_TOKEN, data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
769                        line => $self->{line_prev},
770                        column => $self->{column_prev},
771                       });
772    
773            redo A;            redo A;
774          }          }
775        } else {        } else {
776          die "$0: $self->{content_model} in tag open";          die "$0: $self->{content_model} in tag open";
777        }        }
778      } elsif ($self->{state} eq 'close tag open') {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
779          my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1); # "<"of"</"
780        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
781          if (defined $self->{last_emitted_start_tag_name}) {          if (defined $self->{last_emitted_start_tag_name}) {
782    
783            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>
784            my @next_char;            my @next_char;
785            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++) {
786              push @next_char, $self->{next_input_character};              push @next_char, $self->{next_char};
787              my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);              my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);
788              my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;              my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;
789              if ($self->{next_input_character} == $c or $self->{next_input_character} == $C) {              if ($self->{next_char} == $c or $self->{next_char} == $C) {
790                  !!!cp (24);
791                !!!next-input-character;                !!!next-input-character;
792                next TAGNAME;                next TAGNAME;
793              } else {              } else {
794                $self->{next_input_character} = shift @next_char; # reconsume                !!!cp (25);
795                  $self->{next_char} = shift @next_char; # reconsume
796                !!!back-next-input-character (@next_char);                !!!back-next-input-character (@next_char);
797                $self->{state} = 'data';                $self->{state} = DATA_STATE;
798    
799                !!!emit ({type => CHARACTER_TOKEN, data => '</'});                !!!emit ({type => CHARACTER_TOKEN, data => '</',
800                            line => $l, column => $c,
801                           });
802        
803                redo A;                redo A;
804              }              }
805            }            }
806            push @next_char, $self->{next_input_character};            push @next_char, $self->{next_char};
807                
808            unless ($self->{next_input_character} == 0x0009 or # HT            unless ($self->{next_char} == 0x0009 or # HT
809                    $self->{next_input_character} == 0x000A or # LF                    $self->{next_char} == 0x000A or # LF
810                    $self->{next_input_character} == 0x000B or # VT                    $self->{next_char} == 0x000B or # VT
811                    $self->{next_input_character} == 0x000C or # FF                    $self->{next_char} == 0x000C or # FF
812                    $self->{next_input_character} == 0x0020 or # SP                    $self->{next_char} == 0x0020 or # SP
813                    $self->{next_input_character} == 0x003E or # >                    $self->{next_char} == 0x003E or # >
814                    $self->{next_input_character} == 0x002F or # /                    $self->{next_char} == 0x002F or # /
815                    $self->{next_input_character} == -1) {                    $self->{next_char} == -1) {
816              $self->{next_input_character} = shift @next_char; # reconsume              !!!cp (26);
817                $self->{next_char} = shift @next_char; # reconsume
818              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
819              $self->{state} = 'data';              $self->{state} = DATA_STATE;
820              !!!emit ({type => CHARACTER_TOKEN, data => '</'});              !!!emit ({type => CHARACTER_TOKEN, data => '</',
821                          line => $l, column => $c,
822                         });
823              redo A;              redo A;
824            } else {            } else {
825              $self->{next_input_character} = shift @next_char;              !!!cp (27);
826                $self->{next_char} = shift @next_char;
827              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
828              # and consume...              # and consume...
829            }            }
830          } else {          } else {
831            ## No start tag token has ever been emitted            ## No start tag token has ever been emitted
832              !!!cp (28);
833            # next-input-character is already done            # next-input-character is already done
834            $self->{state} = 'data';            $self->{state} = DATA_STATE;
835            !!!emit ({type => CHARACTER_TOKEN, data => '</'});            !!!emit ({type => CHARACTER_TOKEN, data => '</',
836                        line => $l, column => $c,
837                       });
838            redo A;            redo A;
839          }          }
840        }        }
841                
842        if (0x0041 <= $self->{next_input_character} and        if (0x0041 <= $self->{next_char} and
843            $self->{next_input_character} <= 0x005A) { # A..Z            $self->{next_char} <= 0x005A) { # A..Z
844          $self->{current_token} = {type => END_TAG_TOKEN,          !!!cp (29);
845                            tag_name => chr ($self->{next_input_character} + 0x0020)};          $self->{current_token}
846          $self->{state} = 'tag name';              = {type => END_TAG_TOKEN,
847                   tag_name => chr ($self->{next_char} + 0x0020),
848                   line => $l, column => $c};
849            $self->{state} = TAG_NAME_STATE;
850          !!!next-input-character;          !!!next-input-character;
851          redo A;          redo A;
852        } elsif (0x0061 <= $self->{next_input_character} and        } elsif (0x0061 <= $self->{next_char} and
853                 $self->{next_input_character} <= 0x007A) { # a..z                 $self->{next_char} <= 0x007A) { # a..z
854            !!!cp (30);
855          $self->{current_token} = {type => END_TAG_TOKEN,          $self->{current_token} = {type => END_TAG_TOKEN,
856                            tag_name => chr ($self->{next_input_character})};                                    tag_name => chr ($self->{next_char}),
857          $self->{state} = 'tag name';                                    line => $l, column => $c};
858            $self->{state} = TAG_NAME_STATE;
859          !!!next-input-character;          !!!next-input-character;
860          redo A;          redo A;
861        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
862          !!!parse-error (type => 'empty end tag');          !!!cp (31);
863          $self->{state} = 'data';          !!!parse-error (type => 'empty end tag',
864                            line => $self->{line_prev}, ## "<" in "</>"
865                            column => $self->{column_prev} - 1);
866            $self->{state} = DATA_STATE;
867          !!!next-input-character;          !!!next-input-character;
868          redo A;          redo A;
869        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
870            !!!cp (32);
871          !!!parse-error (type => 'bare etago');          !!!parse-error (type => 'bare etago');
872          $self->{state} = 'data';          $self->{state} = DATA_STATE;
873          # reconsume          # reconsume
874    
875          !!!emit ({type => CHARACTER_TOKEN, data => '</'});          !!!emit ({type => CHARACTER_TOKEN, data => '</',
876                      line => $l, column => $c,
877                     });
878    
879          redo A;          redo A;
880        } else {        } else {
881            !!!cp (33);
882          !!!parse-error (type => 'bogus end tag');          !!!parse-error (type => 'bogus end tag');
883          $self->{state} = 'bogus comment';          $self->{state} = BOGUS_COMMENT_STATE;
884          ## $self->{next_input_character} is intentionally left as is          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
885          redo A;                                    line => $self->{line_prev}, # "<" of "</"
886        }                                    column => $self->{column_prev} - 1,
887      } elsif ($self->{state} eq 'tag name') {                                   };
888        if ($self->{next_input_character} == 0x0009 or # HT          ## $self->{next_char} is intentionally left as is
889            $self->{next_input_character} == 0x000A or # LF          redo A;
890            $self->{next_input_character} == 0x000B or # VT        }
891            $self->{next_input_character} == 0x000C or # FF      } elsif ($self->{state} == TAG_NAME_STATE) {
892            $self->{next_input_character} == 0x0020) { # SP        if ($self->{next_char} == 0x0009 or # HT
893          $self->{state} = 'before attribute name';            $self->{next_char} == 0x000A or # LF
894              $self->{next_char} == 0x000B or # VT
895              $self->{next_char} == 0x000C or # FF
896              $self->{next_char} == 0x0020) { # SP
897            !!!cp (34);
898            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
899          !!!next-input-character;          !!!next-input-character;
900          redo A;          redo A;
901        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
902          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
903            $self->{current_token}->{first_start_tag}            !!!cp (35);
               = not defined $self->{last_emitted_start_tag_name};  
904            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
905          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
906            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
907            if ($self->{current_token}->{attributes}) {            #if ($self->{current_token}->{attributes}) {
908              !!!parse-error (type => 'end tag attribute');            #  ## NOTE: This should never be reached.
909            }            #  !!! cp (36);
910              #  !!! parse-error (type => 'end tag attribute');
911              #} else {
912                !!!cp (37);
913              #}
914          } else {          } else {
915            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
916          }          }
917          $self->{state} = 'data';          $self->{state} = DATA_STATE;
918          !!!next-input-character;          !!!next-input-character;
919    
920          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
921    
922          redo A;          redo A;
923        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
924                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
925          $self->{current_token}->{tag_name} .= chr ($self->{next_input_character} + 0x0020);          !!!cp (38);
926            $self->{current_token}->{tag_name} .= chr ($self->{next_char} + 0x0020);
927            # start tag or end tag            # start tag or end tag
928          ## Stay in this state          ## Stay in this state
929          !!!next-input-character;          !!!next-input-character;
930          redo A;          redo A;
931        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
932          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
933          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
934            $self->{current_token}->{first_start_tag}            !!!cp (39);
               = not defined $self->{last_emitted_start_tag_name};  
935            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
936          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
937            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
938            if ($self->{current_token}->{attributes}) {            #if ($self->{current_token}->{attributes}) {
939              !!!parse-error (type => 'end tag attribute');            #  ## NOTE: This state should never be reached.
940            }            #  !!! cp (40);
941              #  !!! parse-error (type => 'end tag attribute');
942              #} else {
943                !!!cp (41);
944              #}
945          } else {          } else {
946            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
947          }          }
948          $self->{state} = 'data';          $self->{state} = DATA_STATE;
949          # reconsume          # reconsume
950    
951          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
952    
953          redo A;          redo A;
954        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
955            !!!cp (42);
956            $self->{state} = SELF_CLOSING_START_TAG_STATE;
957          !!!next-input-character;          !!!next-input-character;
         if ($self->{next_input_character} == 0x003E and # >  
             $self->{current_token}->{type} == START_TAG_TOKEN and  
             $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {  
           # permitted slash  
           #  
         } else {  
           !!!parse-error (type => 'nestc');  
         }  
         $self->{state} = 'before attribute name';  
         # next-input-character is already done  
958          redo A;          redo A;
959        } else {        } else {
960          $self->{current_token}->{tag_name} .= chr $self->{next_input_character};          !!!cp (44);
961            $self->{current_token}->{tag_name} .= chr $self->{next_char};
962            # start tag or end tag            # start tag or end tag
963          ## Stay in the state          ## Stay in the state
964          !!!next-input-character;          !!!next-input-character;
965          redo A;          redo A;
966        }        }
967      } elsif ($self->{state} eq 'before attribute name') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_NAME_STATE) {
968        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
969            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
970            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
971            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
972            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
973            !!!cp (45);
974          ## Stay in the state          ## Stay in the state
975          !!!next-input-character;          !!!next-input-character;
976          redo A;          redo A;
977        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
978          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
979            $self->{current_token}->{first_start_tag}            !!!cp (46);
               = not defined $self->{last_emitted_start_tag_name};  
980            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
981          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
982            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
983            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
984                !!!cp (47);
985              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
986              } else {
987                !!!cp (48);
988            }            }
989          } else {          } else {
990            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
991          }          }
992          $self->{state} = 'data';          $self->{state} = DATA_STATE;
993          !!!next-input-character;          !!!next-input-character;
994    
995          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
996    
997          redo A;          redo A;
998        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
999                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1000          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          !!!cp (49);
1001                                value => ''};          $self->{current_attribute}
1002          $self->{state} = 'attribute name';              = {name => chr ($self->{next_char} + 0x0020),
1003                   value => '',
1004                   line => $self->{line}, column => $self->{column}};
1005            $self->{state} = ATTRIBUTE_NAME_STATE;
1006            !!!next-input-character;
1007            redo A;
1008          } elsif ($self->{next_char} == 0x002F) { # /
1009            !!!cp (50);
1010            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1011          !!!next-input-character;          !!!next-input-character;
1012          redo A;          redo A;
1013        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == -1) {
         !!!next-input-character;  
         if ($self->{next_input_character} == 0x003E and # >  
             $self->{current_token}->{type} == START_TAG_TOKEN and  
             $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {  
           # permitted slash  
           #  
         } else {  
           !!!parse-error (type => 'nestc');  
         }  
         ## Stay in the state  
         # next-input-character is already done  
         redo A;  
       } elsif ($self->{next_input_character} == -1) {  
1014          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1015          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1016            $self->{current_token}->{first_start_tag}            !!!cp (52);
               = not defined $self->{last_emitted_start_tag_name};  
1017            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1018          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1019            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1020            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1021                !!!cp (53);
1022              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1023              } else {
1024                !!!cp (54);
1025            }            }
1026          } else {          } else {
1027            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1028          }          }
1029          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1030          # reconsume          # reconsume
1031    
1032          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1033    
1034          redo A;          redo A;
1035        } else {        } else {
1036          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          if ({
1037                                value => ''};               0x0022 => 1, # "
1038          $self->{state} = 'attribute name';               0x0027 => 1, # '
1039                 0x003D => 1, # =
1040                }->{$self->{next_char}}) {
1041              !!!cp (55);
1042              !!!parse-error (type => 'bad attribute name');
1043            } else {
1044              !!!cp (56);
1045            }
1046            $self->{current_attribute}
1047                = {name => chr ($self->{next_char}),
1048                   value => '',
1049                   line => $self->{line}, column => $self->{column}};
1050            $self->{state} = ATTRIBUTE_NAME_STATE;
1051          !!!next-input-character;          !!!next-input-character;
1052          redo A;          redo A;
1053        }        }
1054      } elsif ($self->{state} eq 'attribute name') {      } elsif ($self->{state} == ATTRIBUTE_NAME_STATE) {
1055        my $before_leave = sub {        my $before_leave = sub {
1056          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
1057              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
1058            !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name});            !!!cp (57);
1059              !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name}, line => $self->{current_attribute}->{line}, column => $self->{current_attribute}->{column});
1060            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
1061          } else {          } else {
1062              !!!cp (58);
1063            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}
1064              = $self->{current_attribute};              = $self->{current_attribute};
1065          }          }
1066        }; # $before_leave        }; # $before_leave
1067    
1068        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1069            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1070            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1071            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1072            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1073            !!!cp (59);
1074          $before_leave->();          $before_leave->();
1075          $self->{state} = 'after attribute name';          $self->{state} = AFTER_ATTRIBUTE_NAME_STATE;
1076          !!!next-input-character;          !!!next-input-character;
1077          redo A;          redo A;
1078        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_char} == 0x003D) { # =
1079            !!!cp (60);
1080          $before_leave->();          $before_leave->();
1081          $self->{state} = 'before attribute value';          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
1082          !!!next-input-character;          !!!next-input-character;
1083          redo A;          redo A;
1084        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1085          $before_leave->();          $before_leave->();
1086          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1087            $self->{current_token}->{first_start_tag}            !!!cp (61);
               = not defined $self->{last_emitted_start_tag_name};  
1088            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1089          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1090              !!!cp (62);
1091            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1092            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1093              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 642  sub _get_next_token ($) { Line 1095  sub _get_next_token ($) {
1095          } else {          } else {
1096            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1097          }          }
1098          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1099          !!!next-input-character;          !!!next-input-character;
1100    
1101          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1102    
1103          redo A;          redo A;
1104        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1105                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1106          $self->{current_attribute}->{name} .= chr ($self->{next_input_character} + 0x0020);          !!!cp (63);
1107            $self->{current_attribute}->{name} .= chr ($self->{next_char} + 0x0020);
1108          ## Stay in the state          ## Stay in the state
1109          !!!next-input-character;          !!!next-input-character;
1110          redo A;          redo A;
1111        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1112            !!!cp (64);
1113          $before_leave->();          $before_leave->();
1114            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1115          !!!next-input-character;          !!!next-input-character;
         if ($self->{next_input_character} == 0x003E and # >  
             $self->{current_token}->{type} == START_TAG_TOKEN and  
             $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {  
           # permitted slash  
           #  
         } else {  
           !!!parse-error (type => 'nestc');  
         }  
         $self->{state} = 'before attribute name';  
         # next-input-character is already done  
1116          redo A;          redo A;
1117        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1118          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1119          $before_leave->();          $before_leave->();
1120          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1121            $self->{current_token}->{first_start_tag}            !!!cp (66);
               = not defined $self->{last_emitted_start_tag_name};  
1122            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1123          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1124            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1125            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1126                !!!cp (67);
1127              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1128              } else {
1129                ## NOTE: This state should never be reached.
1130                !!!cp (68);
1131            }            }
1132          } else {          } else {
1133            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1134          }          }
1135          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1136          # reconsume          # reconsume
1137    
1138          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1139    
1140          redo A;          redo A;
1141        } else {        } else {
1142          $self->{current_attribute}->{name} .= chr ($self->{next_input_character});          if ($self->{next_char} == 0x0022 or # "
1143                $self->{next_char} == 0x0027) { # '
1144              !!!cp (69);
1145              !!!parse-error (type => 'bad attribute name');
1146            } else {
1147              !!!cp (70);
1148            }
1149            $self->{current_attribute}->{name} .= chr ($self->{next_char});
1150          ## Stay in the state          ## Stay in the state
1151          !!!next-input-character;          !!!next-input-character;
1152          redo A;          redo A;
1153        }        }
1154      } elsif ($self->{state} eq 'after attribute name') {      } elsif ($self->{state} == AFTER_ATTRIBUTE_NAME_STATE) {
1155        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1156            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1157            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1158            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1159            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1160            !!!cp (71);
1161          ## Stay in the state          ## Stay in the state
1162          !!!next-input-character;          !!!next-input-character;
1163          redo A;          redo A;
1164        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_char} == 0x003D) { # =
1165          $self->{state} = 'before attribute value';          !!!cp (72);
1166            $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
1167          !!!next-input-character;          !!!next-input-character;
1168          redo A;          redo A;
1169        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1170          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1171            $self->{current_token}->{first_start_tag}            !!!cp (73);
               = not defined $self->{last_emitted_start_tag_name};  
1172            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1173          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1174            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1175            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1176                !!!cp (74);
1177              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1178              } else {
1179                ## NOTE: This state should never be reached.
1180                !!!cp (75);
1181            }            }
1182          } else {          } else {
1183            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1184          }          }
1185          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1186          !!!next-input-character;          !!!next-input-character;
1187    
1188          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1189    
1190          redo A;          redo A;
1191        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1192                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1193          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          !!!cp (76);
1194                                value => ''};          $self->{current_attribute}
1195          $self->{state} = 'attribute name';              = {name => chr ($self->{next_char} + 0x0020),
1196                   value => '',
1197                   line => $self->{line}, column => $self->{column}};
1198            $self->{state} = ATTRIBUTE_NAME_STATE;
1199            !!!next-input-character;
1200            redo A;
1201          } elsif ($self->{next_char} == 0x002F) { # /
1202            !!!cp (77);
1203            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1204          !!!next-input-character;          !!!next-input-character;
1205          redo A;          redo A;
1206        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == -1) {
         !!!next-input-character;  
         if ($self->{next_input_character} == 0x003E and # >  
             $self->{current_token}->{type} == START_TAG_TOKEN and  
             $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {  
           # permitted slash  
           #  
         } else {  
           !!!parse-error (type => 'nestc');  
           ## TODO: Different error type for <aa / bb> than <aa/>  
         }  
         $self->{state} = 'before attribute name';  
         # next-input-character is already done  
         redo A;  
       } elsif ($self->{next_input_character} == -1) {  
1207          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1208          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1209            $self->{current_token}->{first_start_tag}            !!!cp (79);
               = not defined $self->{last_emitted_start_tag_name};  
1210            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1211          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1212            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1213            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1214                !!!cp (80);
1215              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1216              } else {
1217                ## NOTE: This state should never be reached.
1218                !!!cp (81);
1219            }            }
1220          } else {          } else {
1221            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1222          }          }
1223          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1224          # reconsume          # reconsume
1225    
1226          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1227    
1228          redo A;          redo A;
1229        } else {        } else {
1230          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          !!!cp (82);
1231                                value => ''};          $self->{current_attribute}
1232          $self->{state} = 'attribute name';              = {name => chr ($self->{next_char}),
1233                   value => '',
1234                   line => $self->{line}, column => $self->{column}};
1235            $self->{state} = ATTRIBUTE_NAME_STATE;
1236          !!!next-input-character;          !!!next-input-character;
1237          redo A;                  redo A;        
1238        }        }
1239      } elsif ($self->{state} eq 'before attribute value') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_VALUE_STATE) {
1240        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1241            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1242            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1243            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1244            $self->{next_input_character} == 0x0020) { # SP                  $self->{next_char} == 0x0020) { # SP      
1245            !!!cp (83);
1246          ## Stay in the state          ## Stay in the state
1247          !!!next-input-character;          !!!next-input-character;
1248          redo A;          redo A;
1249        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
1250          $self->{state} = 'attribute value (double-quoted)';          !!!cp (84);
1251            $self->{state} = ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE;
1252          !!!next-input-character;          !!!next-input-character;
1253          redo A;          redo A;
1254        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1255          $self->{state} = 'attribute value (unquoted)';          !!!cp (85);
1256            $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1257          ## reconsume          ## reconsume
1258          redo A;          redo A;
1259        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
1260          $self->{state} = 'attribute value (single-quoted)';          !!!cp (86);
1261            $self->{state} = ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE;
1262          !!!next-input-character;          !!!next-input-character;
1263          redo A;          redo A;
1264        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1265          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1266            $self->{current_token}->{first_start_tag}            !!!cp (87);
               = not defined $self->{last_emitted_start_tag_name};  
1267            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1268          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1269            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1270            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1271                !!!cp (88);
1272              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1273              } else {
1274                ## NOTE: This state should never be reached.
1275                !!!cp (89);
1276            }            }
1277          } else {          } else {
1278            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1279          }          }
1280          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1281          !!!next-input-character;          !!!next-input-character;
1282    
1283          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1284    
1285          redo A;          redo A;
1286        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1287          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1288          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1289            $self->{current_token}->{first_start_tag}            !!!cp (90);
               = not defined $self->{last_emitted_start_tag_name};  
1290            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1291          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1292            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1293            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1294                !!!cp (91);
1295              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1296              } else {
1297                ## NOTE: This state should never be reached.
1298                !!!cp (92);
1299            }            }
1300          } else {          } else {
1301            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1302          }          }
1303          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1304          ## reconsume          ## reconsume
1305    
1306          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1307    
1308          redo A;          redo A;
1309        } else {        } else {
1310          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          if ($self->{next_char} == 0x003D) { # =
1311          $self->{state} = 'attribute value (unquoted)';            !!!cp (93);
1312              !!!parse-error (type => 'bad attribute value');
1313            } else {
1314              !!!cp (94);
1315            }
1316            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1317            $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1318          !!!next-input-character;          !!!next-input-character;
1319          redo A;          redo A;
1320        }        }
1321      } elsif ($self->{state} eq 'attribute value (double-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE) {
1322        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
1323          $self->{state} = 'before attribute name';          !!!cp (95);
1324            $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1325          !!!next-input-character;          !!!next-input-character;
1326          redo A;          redo A;
1327        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1328          $self->{last_attribute_value_state} = 'attribute value (double-quoted)';          !!!cp (96);
1329          $self->{state} = 'entity in attribute value';          $self->{last_attribute_value_state} = $self->{state};
1330            $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1331          !!!next-input-character;          !!!next-input-character;
1332          redo A;          redo A;
1333        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1334          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1335          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1336            $self->{current_token}->{first_start_tag}            !!!cp (97);
               = not defined $self->{last_emitted_start_tag_name};  
1337            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1338          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1339            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1340            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1341                !!!cp (98);
1342              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1343              } else {
1344                ## NOTE: This state should never be reached.
1345                !!!cp (99);
1346            }            }
1347          } else {          } else {
1348            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1349          }          }
1350          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1351          ## reconsume          ## reconsume
1352    
1353          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1354    
1355          redo A;          redo A;
1356        } else {        } else {
1357          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          !!!cp (100);
1358            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1359          ## Stay in the state          ## Stay in the state
1360          !!!next-input-character;          !!!next-input-character;
1361          redo A;          redo A;
1362        }        }
1363      } elsif ($self->{state} eq 'attribute value (single-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE) {
1364        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
1365          $self->{state} = 'before attribute name';          !!!cp (101);
1366            $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1367          !!!next-input-character;          !!!next-input-character;
1368          redo A;          redo A;
1369        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1370          $self->{last_attribute_value_state} = 'attribute value (single-quoted)';          !!!cp (102);
1371          $self->{state} = 'entity in attribute value';          $self->{last_attribute_value_state} = $self->{state};
1372            $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1373          !!!next-input-character;          !!!next-input-character;
1374          redo A;          redo A;
1375        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1376          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1377          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1378            $self->{current_token}->{first_start_tag}            !!!cp (103);
               = not defined $self->{last_emitted_start_tag_name};  
1379            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1380          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1381            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1382            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1383                !!!cp (104);
1384              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1385              } else {
1386                ## NOTE: This state should never be reached.
1387                !!!cp (105);
1388            }            }
1389          } else {          } else {
1390            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1391          }          }
1392          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1393          ## reconsume          ## reconsume
1394    
1395          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1396    
1397          redo A;          redo A;
1398        } else {        } else {
1399          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          !!!cp (106);
1400            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1401          ## Stay in the state          ## Stay in the state
1402          !!!next-input-character;          !!!next-input-character;
1403          redo A;          redo A;
1404        }        }
1405      } elsif ($self->{state} eq 'attribute value (unquoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_UNQUOTED_STATE) {
1406        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1407            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1408            $self->{next_input_character} == 0x000B or # HT            $self->{next_char} == 0x000B or # HT
1409            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1410            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1411          $self->{state} = 'before attribute name';          !!!cp (107);
1412          !!!next-input-character;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1413          redo A;          !!!next-input-character;
1414        } elsif ($self->{next_input_character} == 0x0026) { # &          redo A;
1415          $self->{last_attribute_value_state} = 'attribute value (unquoted)';        } elsif ($self->{next_char} == 0x0026) { # &
1416          $self->{state} = 'entity in attribute value';          !!!cp (108);
1417            $self->{last_attribute_value_state} = $self->{state};
1418            $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1419          !!!next-input-character;          !!!next-input-character;
1420          redo A;          redo A;
1421        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1422          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1423            $self->{current_token}->{first_start_tag}            !!!cp (109);
               = not defined $self->{last_emitted_start_tag_name};  
1424            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1425          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1426            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1427            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1428                !!!cp (110);
1429              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1430              } else {
1431                ## NOTE: This state should never be reached.
1432                !!!cp (111);
1433            }            }
1434          } else {          } else {
1435            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1436          }          }
1437          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1438          !!!next-input-character;          !!!next-input-character;
1439    
1440          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1441    
1442          redo A;          redo A;
1443        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1444          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1445          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1446            $self->{current_token}->{first_start_tag}            !!!cp (112);
               = not defined $self->{last_emitted_start_tag_name};  
1447            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1448          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1449            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1450            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1451                !!!cp (113);
1452              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1453              } else {
1454                ## NOTE: This state should never be reached.
1455                !!!cp (114);
1456            }            }
1457          } else {          } else {
1458            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1459          }          }
1460          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1461          ## reconsume          ## reconsume
1462    
1463          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1464    
1465          redo A;          redo A;
1466        } else {        } else {
1467          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          if ({
1468                 0x0022 => 1, # "
1469                 0x0027 => 1, # '
1470                 0x003D => 1, # =
1471                }->{$self->{next_char}}) {
1472              !!!cp (115);
1473              !!!parse-error (type => 'bad attribute value');
1474            } else {
1475              !!!cp (116);
1476            }
1477            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1478          ## Stay in the state          ## Stay in the state
1479          !!!next-input-character;          !!!next-input-character;
1480          redo A;          redo A;
1481        }        }
1482      } elsif ($self->{state} eq 'entity in attribute value') {      } elsif ($self->{state} == ENTITY_IN_ATTRIBUTE_VALUE_STATE) {
1483        my $token = $self->_tokenize_attempt_to_consume_an_entity (1);        my $token = $self->_tokenize_attempt_to_consume_an_entity
1484              (1,
1485               $self->{last_attribute_value_state}
1486                 == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE ? 0x0022 : # "
1487               $self->{last_attribute_value_state}
1488                 == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE ? 0x0027 : # '
1489               -1);
1490    
1491        unless (defined $token) {        unless (defined $token) {
1492            !!!cp (117);
1493          $self->{current_attribute}->{value} .= '&';          $self->{current_attribute}->{value} .= '&';
1494        } else {        } else {
1495            !!!cp (118);
1496          $self->{current_attribute}->{value} .= $token->{data};          $self->{current_attribute}->{value} .= $token->{data};
1497            $self->{current_attribute}->{has_reference} = $token->{has_reference};
1498          ## ISSUE: spec says "append the returned character token to the current attribute's value"          ## ISSUE: spec says "append the returned character token to the current attribute's value"
1499        }        }
1500    
1501        $self->{state} = $self->{last_attribute_value_state};        $self->{state} = $self->{last_attribute_value_state};
1502        # next-input-character is already done        # next-input-character is already done
1503        redo A;        redo A;
1504      } elsif ($self->{state} eq 'bogus comment') {      } elsif ($self->{state} == AFTER_ATTRIBUTE_VALUE_QUOTED_STATE) {
1505          if ($self->{next_char} == 0x0009 or # HT
1506              $self->{next_char} == 0x000A or # LF
1507              $self->{next_char} == 0x000B or # VT
1508              $self->{next_char} == 0x000C or # FF
1509              $self->{next_char} == 0x0020) { # SP
1510            !!!cp (118);
1511            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1512            !!!next-input-character;
1513            redo A;
1514          } elsif ($self->{next_char} == 0x003E) { # >
1515            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1516              !!!cp (119);
1517              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1518            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1519              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1520              if ($self->{current_token}->{attributes}) {
1521                !!!cp (120);
1522                !!!parse-error (type => 'end tag attribute');
1523              } else {
1524                ## NOTE: This state should never be reached.
1525                !!!cp (121);
1526              }
1527            } else {
1528              die "$0: $self->{current_token}->{type}: Unknown token type";
1529            }
1530            $self->{state} = DATA_STATE;
1531            !!!next-input-character;
1532    
1533            !!!emit ($self->{current_token}); # start tag or end tag
1534    
1535            redo A;
1536          } elsif ($self->{next_char} == 0x002F) { # /
1537            !!!cp (122);
1538            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1539            !!!next-input-character;
1540            redo A;
1541          } else {
1542            !!!cp ('124.1');
1543            !!!parse-error (type => 'no space between attributes');
1544            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1545            ## reconsume
1546            redo A;
1547          }
1548        } elsif ($self->{state} == SELF_CLOSING_START_TAG_STATE) {
1549          if ($self->{next_char} == 0x003E) { # >
1550            if ($self->{current_token}->{type} == END_TAG_TOKEN) {
1551              !!!cp ('124.2');
1552              !!!parse-error (type => 'nestc', token => $self->{current_token});
1553              ## TODO: Different type than slash in start tag
1554              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1555              if ($self->{current_token}->{attributes}) {
1556                !!!cp ('124.4');
1557                !!!parse-error (type => 'end tag attribute');
1558              } else {
1559                !!!cp ('124.5');
1560              }
1561              ## TODO: Test |<title></title/>|
1562            } else {
1563              !!!cp ('124.3');
1564              $self->{self_closing} = 1;
1565            }
1566    
1567            $self->{state} = DATA_STATE;
1568            !!!next-input-character;
1569    
1570            !!!emit ($self->{current_token}); # start tag or end tag
1571    
1572            redo A;
1573          } else {
1574            !!!cp ('124.4');
1575            !!!parse-error (type => 'nestc');
1576            ## TODO: This error type is wrong.
1577            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1578            ## Reconsume.
1579            redo A;
1580          }
1581        } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
1582        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1583                
1584        my $token = {type => COMMENT_TOKEN, data => ''};        ## NOTE: Set by the previous state
1585          #my $token = {type => COMMENT_TOKEN, data => ''};
1586    
1587        BC: {        BC: {
1588          if ($self->{next_input_character} == 0x003E) { # >          if ($self->{next_char} == 0x003E) { # >
1589            $self->{state} = 'data';            !!!cp (124);
1590              $self->{state} = DATA_STATE;
1591            !!!next-input-character;            !!!next-input-character;
1592    
1593            !!!emit ($token);            !!!emit ($self->{current_token}); # comment
1594    
1595            redo A;            redo A;
1596          } elsif ($self->{next_input_character} == -1) {          } elsif ($self->{next_char} == -1) {
1597            $self->{state} = 'data';            !!!cp (125);
1598              $self->{state} = DATA_STATE;
1599            ## reconsume            ## reconsume
1600    
1601            !!!emit ($token);            !!!emit ($self->{current_token}); # comment
1602    
1603            redo A;            redo A;
1604          } else {          } else {
1605            $token->{data} .= chr ($self->{next_input_character});            !!!cp (126);
1606              $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
1607            !!!next-input-character;            !!!next-input-character;
1608            redo BC;            redo BC;
1609          }          }
1610        } # BC        } # BC
1611      } elsif ($self->{state} eq 'markup declaration open') {  
1612          die "$0: _get_next_token: unexpected case [BC]";
1613        } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
1614        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1615    
1616          my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1);
1617    
1618        my @next_char;        my @next_char;
1619        push @next_char, $self->{next_input_character};        push @next_char, $self->{next_char};
1620                
1621        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
1622          !!!next-input-character;          !!!next-input-character;
1623          push @next_char, $self->{next_input_character};          push @next_char, $self->{next_char};
1624          if ($self->{next_input_character} == 0x002D) { # -          if ($self->{next_char} == 0x002D) { # -
1625            $self->{current_token} = {type => COMMENT_TOKEN, data => ''};            !!!cp (127);
1626            $self->{state} = 'comment start';            $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1627                                        line => $l, column => $c,
1628                                       };
1629              $self->{state} = COMMENT_START_STATE;
1630            !!!next-input-character;            !!!next-input-character;
1631            redo A;            redo A;
1632            } else {
1633              !!!cp (128);
1634          }          }
1635        } elsif ($self->{next_input_character} == 0x0044 or # D        } elsif ($self->{next_char} == 0x0044 or # D
1636                 $self->{next_input_character} == 0x0064) { # d                 $self->{next_char} == 0x0064) { # d
1637          !!!next-input-character;          !!!next-input-character;
1638          push @next_char, $self->{next_input_character};          push @next_char, $self->{next_char};
1639          if ($self->{next_input_character} == 0x004F or # O          if ($self->{next_char} == 0x004F or # O
1640              $self->{next_input_character} == 0x006F) { # o              $self->{next_char} == 0x006F) { # o
1641            !!!next-input-character;            !!!next-input-character;
1642            push @next_char, $self->{next_input_character};            push @next_char, $self->{next_char};
1643            if ($self->{next_input_character} == 0x0043 or # C            if ($self->{next_char} == 0x0043 or # C
1644                $self->{next_input_character} == 0x0063) { # c                $self->{next_char} == 0x0063) { # c
1645              !!!next-input-character;              !!!next-input-character;
1646              push @next_char, $self->{next_input_character};              push @next_char, $self->{next_char};
1647              if ($self->{next_input_character} == 0x0054 or # T              if ($self->{next_char} == 0x0054 or # T
1648                  $self->{next_input_character} == 0x0074) { # t                  $self->{next_char} == 0x0074) { # t
1649                !!!next-input-character;                !!!next-input-character;
1650                push @next_char, $self->{next_input_character};                push @next_char, $self->{next_char};
1651                if ($self->{next_input_character} == 0x0059 or # Y                if ($self->{next_char} == 0x0059 or # Y
1652                    $self->{next_input_character} == 0x0079) { # y                    $self->{next_char} == 0x0079) { # y
1653                  !!!next-input-character;                  !!!next-input-character;
1654                  push @next_char, $self->{next_input_character};                  push @next_char, $self->{next_char};
1655                  if ($self->{next_input_character} == 0x0050 or # P                  if ($self->{next_char} == 0x0050 or # P
1656                      $self->{next_input_character} == 0x0070) { # p                      $self->{next_char} == 0x0070) { # p
1657                    !!!next-input-character;                    !!!next-input-character;
1658                    push @next_char, $self->{next_input_character};                    push @next_char, $self->{next_char};
1659                    if ($self->{next_input_character} == 0x0045 or # E                    if ($self->{next_char} == 0x0045 or # E
1660                        $self->{next_input_character} == 0x0065) { # e                        $self->{next_char} == 0x0065) { # e
1661                      ## ISSUE: What a stupid code this is!                      !!!cp (129);
1662                      $self->{state} = 'DOCTYPE';                      ## TODO: What a stupid code this is!
1663                        $self->{state} = DOCTYPE_STATE;
1664                        $self->{current_token} = {type => DOCTYPE_TOKEN,
1665                                                  quirks => 1,
1666                                                  line => $l, column => $c,
1667                                                 };
1668                      !!!next-input-character;                      !!!next-input-character;
1669                      redo A;                      redo A;
1670                      } else {
1671                        !!!cp (130);
1672                    }                    }
1673                    } else {
1674                      !!!cp (131);
1675                  }                  }
1676                  } else {
1677                    !!!cp (132);
1678                }                }
1679                } else {
1680                  !!!cp (133);
1681              }              }
1682              } else {
1683                !!!cp (134);
1684            }            }
1685            } else {
1686              !!!cp (135);
1687          }          }
1688          } else {
1689            !!!cp (136);
1690        }        }
1691    
1692        !!!parse-error (type => 'bogus comment');        !!!parse-error (type => 'bogus comment');
1693        $self->{next_input_character} = shift @next_char;        $self->{next_char} = shift @next_char;
1694        !!!back-next-input-character (@next_char);        !!!back-next-input-character (@next_char);
1695        $self->{state} = 'bogus comment';        $self->{state} = BOGUS_COMMENT_STATE;
1696          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1697                                    line => $l, column => $c,
1698                                   };
1699        redo A;        redo A;
1700                
1701        ## ISSUE: typos in spec: chacacters, is is a parse error        ## ISSUE: typos in spec: chacacters, is is a parse error
1702        ## ISSUE: spec is somewhat unclear on "is the first character that will be in the comment"; what is "that will be in the comment" is what the algorithm defines, isn't it?        ## ISSUE: spec is somewhat unclear on "is the first character that will be in the comment"; what is "that will be in the comment" is what the algorithm defines, isn't it?
1703      } elsif ($self->{state} eq 'comment start') {      } elsif ($self->{state} == COMMENT_START_STATE) {
1704        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
1705          $self->{state} = 'comment start dash';          !!!cp (137);
1706            $self->{state} = COMMENT_START_DASH_STATE;
1707          !!!next-input-character;          !!!next-input-character;
1708          redo A;          redo A;
1709        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1710            !!!cp (138);
1711          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
1712          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1713          !!!next-input-character;          !!!next-input-character;
1714    
1715          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
1716    
1717          redo A;          redo A;
1718        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1719            !!!cp (139);
1720          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1721          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1722          ## reconsume          ## reconsume
1723    
1724          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
1725    
1726          redo A;          redo A;
1727        } else {        } else {
1728            !!!cp (140);
1729          $self->{current_token}->{data} # comment          $self->{current_token}->{data} # comment
1730              .= chr ($self->{next_input_character});              .= chr ($self->{next_char});
1731          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
1732          !!!next-input-character;          !!!next-input-character;
1733          redo A;          redo A;
1734        }        }
1735      } elsif ($self->{state} eq 'comment start dash') {      } elsif ($self->{state} == COMMENT_START_DASH_STATE) {
1736        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
1737          $self->{state} = 'comment end';          !!!cp (141);
1738            $self->{state} = COMMENT_END_STATE;
1739          !!!next-input-character;          !!!next-input-character;
1740          redo A;          redo A;
1741        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1742            !!!cp (142);
1743          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
1744          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1745          !!!next-input-character;          !!!next-input-character;
1746    
1747          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
1748    
1749          redo A;          redo A;
1750        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1751            !!!cp (143);
1752          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1753          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1754          ## reconsume          ## reconsume
1755    
1756          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
1757    
1758          redo A;          redo A;
1759        } else {        } else {
1760            !!!cp (144);
1761          $self->{current_token}->{data} # comment          $self->{current_token}->{data} # comment
1762              .= '-' . chr ($self->{next_input_character});              .= '-' . chr ($self->{next_char});
1763          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
1764          !!!next-input-character;          !!!next-input-character;
1765          redo A;          redo A;
1766        }        }
1767      } elsif ($self->{state} eq 'comment') {      } elsif ($self->{state} == COMMENT_STATE) {
1768        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
1769          $self->{state} = 'comment end dash';          !!!cp (145);
1770            $self->{state} = COMMENT_END_DASH_STATE;
1771          !!!next-input-character;          !!!next-input-character;
1772          redo A;          redo A;
1773        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1774            !!!cp (146);
1775          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1776          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1777          ## reconsume          ## reconsume
1778    
1779          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
1780    
1781          redo A;          redo A;
1782        } else {        } else {
1783          $self->{current_token}->{data} .= chr ($self->{next_input_character}); # comment          !!!cp (147);
1784            $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
1785          ## Stay in the state          ## Stay in the state
1786          !!!next-input-character;          !!!next-input-character;
1787          redo A;          redo A;
1788        }        }
1789      } elsif ($self->{state} eq 'comment end dash') {      } elsif ($self->{state} == COMMENT_END_DASH_STATE) {
1790        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
1791          $self->{state} = 'comment end';          !!!cp (148);
1792            $self->{state} = COMMENT_END_STATE;
1793          !!!next-input-character;          !!!next-input-character;
1794          redo A;          redo A;
1795        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1796            !!!cp (149);
1797          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1798          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1799          ## reconsume          ## reconsume
1800    
1801          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
1802    
1803          redo A;          redo A;
1804        } else {        } else {
1805          $self->{current_token}->{data} .= '-' . chr ($self->{next_input_character}); # comment          !!!cp (150);
1806          $self->{state} = 'comment';          $self->{current_token}->{data} .= '-' . chr ($self->{next_char}); # comment
1807            $self->{state} = COMMENT_STATE;
1808          !!!next-input-character;          !!!next-input-character;
1809          redo A;          redo A;
1810        }        }
1811      } elsif ($self->{state} eq 'comment end') {      } elsif ($self->{state} == COMMENT_END_STATE) {
1812        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_char} == 0x003E) { # >
1813          $self->{state} = 'data';          !!!cp (151);
1814            $self->{state} = DATA_STATE;
1815          !!!next-input-character;          !!!next-input-character;
1816    
1817          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
1818    
1819          redo A;          redo A;
1820        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
1821          !!!parse-error (type => 'dash in comment');          !!!cp (152);
1822            !!!parse-error (type => 'dash in comment',
1823                            line => $self->{line_prev},
1824                            column => $self->{column_prev});
1825          $self->{current_token}->{data} .= '-'; # comment          $self->{current_token}->{data} .= '-'; # comment
1826          ## Stay in the state          ## Stay in the state
1827          !!!next-input-character;          !!!next-input-character;
1828          redo A;          redo A;
1829        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1830            !!!cp (153);
1831          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1832          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1833          ## reconsume          ## reconsume
1834    
1835          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
1836    
1837          redo A;          redo A;
1838        } else {        } else {
1839          !!!parse-error (type => 'dash in comment');          !!!cp (154);
1840          $self->{current_token}->{data} .= '--' . chr ($self->{next_input_character}); # comment          !!!parse-error (type => 'dash in comment',
1841          $self->{state} = 'comment';                          line => $self->{line_prev},
1842                            column => $self->{column_prev});
1843            $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment
1844            $self->{state} = COMMENT_STATE;
1845          !!!next-input-character;          !!!next-input-character;
1846          redo A;          redo A;
1847        }        }
1848      } elsif ($self->{state} eq 'DOCTYPE') {      } elsif ($self->{state} == DOCTYPE_STATE) {
1849        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1850            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1851            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1852            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1853            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1854          $self->{state} = 'before DOCTYPE name';          !!!cp (155);
1855            $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
1856          !!!next-input-character;          !!!next-input-character;
1857          redo A;          redo A;
1858        } else {        } else {
1859            !!!cp (156);
1860          !!!parse-error (type => 'no space before DOCTYPE name');          !!!parse-error (type => 'no space before DOCTYPE name');
1861          $self->{state} = 'before DOCTYPE name';          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
1862          ## reconsume          ## reconsume
1863          redo A;          redo A;
1864        }        }
1865      } elsif ($self->{state} eq 'before DOCTYPE name') {      } elsif ($self->{state} == BEFORE_DOCTYPE_NAME_STATE) {
1866        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1867            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1868            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1869            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1870            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1871            !!!cp (157);
1872          ## Stay in the state          ## Stay in the state
1873          !!!next-input-character;          !!!next-input-character;
1874          redo A;          redo A;
1875        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1876            !!!cp (158);
1877          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
1878          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1879          !!!next-input-character;          !!!next-input-character;
1880    
1881          !!!emit ({type => DOCTYPE_TOKEN}); # incorrect          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
1882    
1883          redo A;          redo A;
1884        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1885            !!!cp (159);
1886          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
1887          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1888          ## reconsume          ## reconsume
1889    
1890          !!!emit ({type => DOCTYPE_TOKEN}); # incorrect          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
1891    
1892          redo A;          redo A;
1893        } else {        } else {
1894          $self->{current_token}          !!!cp (160);
1895              = {type => DOCTYPE_TOKEN,          $self->{current_token}->{name} = chr $self->{next_char};
1896                 name => chr ($self->{next_input_character}),          delete $self->{current_token}->{quirks};
                correct => 1};  
1897  ## ISSUE: "Set the token's name name to the" in the spec  ## ISSUE: "Set the token's name name to the" in the spec
1898          $self->{state} = 'DOCTYPE name';          $self->{state} = DOCTYPE_NAME_STATE;
1899          !!!next-input-character;          !!!next-input-character;
1900          redo A;          redo A;
1901        }        }
1902      } elsif ($self->{state} eq 'DOCTYPE name') {      } elsif ($self->{state} == DOCTYPE_NAME_STATE) {
1903  ## ISSUE: Redundant "First," in the spec.  ## ISSUE: Redundant "First," in the spec.
1904        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1905            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1906            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1907            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1908            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1909          $self->{state} = 'after DOCTYPE name';          !!!cp (161);
1910            $self->{state} = AFTER_DOCTYPE_NAME_STATE;
1911          !!!next-input-character;          !!!next-input-character;
1912          redo A;          redo A;
1913        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1914          $self->{state} = 'data';          !!!cp (162);
1915            $self->{state} = DATA_STATE;
1916          !!!next-input-character;          !!!next-input-character;
1917    
1918          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1919    
1920          redo A;          redo A;
1921        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1922            !!!cp (163);
1923          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1924          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1925          ## reconsume          ## reconsume
1926    
1927          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
1928          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1929    
1930          redo A;          redo A;
1931        } else {        } else {
1932            !!!cp (164);
1933          $self->{current_token}->{name}          $self->{current_token}->{name}
1934            .= chr ($self->{next_input_character}); # DOCTYPE            .= chr ($self->{next_char}); # DOCTYPE
1935          ## Stay in the state          ## Stay in the state
1936          !!!next-input-character;          !!!next-input-character;
1937          redo A;          redo A;
1938        }        }
1939      } elsif ($self->{state} eq 'after DOCTYPE name') {      } elsif ($self->{state} == AFTER_DOCTYPE_NAME_STATE) {
1940        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1941            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1942            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1943            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1944            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1945            !!!cp (165);
1946          ## Stay in the state          ## Stay in the state
1947          !!!next-input-character;          !!!next-input-character;
1948          redo A;          redo A;
1949        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1950          $self->{state} = 'data';          !!!cp (166);
1951            $self->{state} = DATA_STATE;
1952          !!!next-input-character;          !!!next-input-character;
1953    
1954          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1955    
1956          redo A;          redo A;
1957        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1958            !!!cp (167);
1959          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1960          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1961          ## reconsume          ## reconsume
1962    
1963          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
1964          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1965    
1966          redo A;          redo A;
1967        } elsif ($self->{next_input_character} == 0x0050 or # P        } elsif ($self->{next_char} == 0x0050 or # P
1968                 $self->{next_input_character} == 0x0070) { # p                 $self->{next_char} == 0x0070) { # p
1969          !!!next-input-character;          !!!next-input-character;
1970          if ($self->{next_input_character} == 0x0055 or # U          if ($self->{next_char} == 0x0055 or # U
1971              $self->{next_input_character} == 0x0075) { # u              $self->{next_char} == 0x0075) { # u
1972            !!!next-input-character;            !!!next-input-character;
1973            if ($self->{next_input_character} == 0x0042 or # B            if ($self->{next_char} == 0x0042 or # B
1974                $self->{next_input_character} == 0x0062) { # b                $self->{next_char} == 0x0062) { # b
1975              !!!next-input-character;              !!!next-input-character;
1976              if ($self->{next_input_character} == 0x004C or # L              if ($self->{next_char} == 0x004C or # L
1977                  $self->{next_input_character} == 0x006C) { # l                  $self->{next_char} == 0x006C) { # l
1978                !!!next-input-character;                !!!next-input-character;
1979                if ($self->{next_input_character} == 0x0049 or # I                if ($self->{next_char} == 0x0049 or # I
1980                    $self->{next_input_character} == 0x0069) { # i                    $self->{next_char} == 0x0069) { # i
1981                  !!!next-input-character;                  !!!next-input-character;
1982                  if ($self->{next_input_character} == 0x0043 or # C                  if ($self->{next_char} == 0x0043 or # C
1983                      $self->{next_input_character} == 0x0063) { # c                      $self->{next_char} == 0x0063) { # c
1984                    $self->{state} = 'before DOCTYPE public identifier';                    !!!cp (168);
1985                      $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1986                    !!!next-input-character;                    !!!next-input-character;
1987                    redo A;                    redo A;
1988                    } else {
1989                      !!!cp (169);
1990                  }                  }
1991                  } else {
1992                    !!!cp (170);
1993                }                }
1994                } else {
1995                  !!!cp (171);
1996              }              }
1997              } else {
1998                !!!cp (172);
1999            }            }
2000            } else {
2001              !!!cp (173);
2002          }          }
2003    
2004          #          #
2005        } elsif ($self->{next_input_character} == 0x0053 or # S        } elsif ($self->{next_char} == 0x0053 or # S
2006                 $self->{next_input_character} == 0x0073) { # s                 $self->{next_char} == 0x0073) { # s
2007          !!!next-input-character;          !!!next-input-character;
2008          if ($self->{next_input_character} == 0x0059 or # Y          if ($self->{next_char} == 0x0059 or # Y
2009              $self->{next_input_character} == 0x0079) { # y              $self->{next_char} == 0x0079) { # y
2010            !!!next-input-character;            !!!next-input-character;
2011            if ($self->{next_input_character} == 0x0053 or # S            if ($self->{next_char} == 0x0053 or # S
2012                $self->{next_input_character} == 0x0073) { # s                $self->{next_char} == 0x0073) { # s
2013              !!!next-input-character;              !!!next-input-character;
2014              if ($self->{next_input_character} == 0x0054 or # T              if ($self->{next_char} == 0x0054 or # T
2015                  $self->{next_input_character} == 0x0074) { # t                  $self->{next_char} == 0x0074) { # t
2016                !!!next-input-character;                !!!next-input-character;
2017                if ($self->{next_input_character} == 0x0045 or # E                if ($self->{next_char} == 0x0045 or # E
2018                    $self->{next_input_character} == 0x0065) { # e                    $self->{next_char} == 0x0065) { # e
2019                  !!!next-input-character;                  !!!next-input-character;
2020                  if ($self->{next_input_character} == 0x004D or # M                  if ($self->{next_char} == 0x004D or # M
2021                      $self->{next_input_character} == 0x006D) { # m                      $self->{next_char} == 0x006D) { # m
2022                    $self->{state} = 'before DOCTYPE system identifier';                    !!!cp (174);
2023                      $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2024                    !!!next-input-character;                    !!!next-input-character;
2025                    redo A;                    redo A;
2026                    } else {
2027                      !!!cp (175);
2028                  }                  }
2029                  } else {
2030                    !!!cp (176);
2031                }                }
2032                } else {
2033                  !!!cp (177);
2034              }              }
2035              } else {
2036                !!!cp (178);
2037            }            }
2038            } else {
2039              !!!cp (179);
2040          }          }
2041    
2042          #          #
2043        } else {        } else {
2044            !!!cp (180);
2045          !!!next-input-character;          !!!next-input-character;
2046          #          #
2047        }        }
2048    
2049        !!!parse-error (type => 'string after DOCTYPE name');        !!!parse-error (type => 'string after DOCTYPE name');
2050        $self->{state} = 'bogus DOCTYPE';        $self->{current_token}->{quirks} = 1;
2051    
2052          $self->{state} = BOGUS_DOCTYPE_STATE;
2053        # next-input-character is already done        # next-input-character is already done
2054        redo A;        redo A;
2055      } elsif ($self->{state} eq 'before DOCTYPE public identifier') {      } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
2056        if ({        if ({
2057              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2058              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2059            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2060            !!!cp (181);
2061          ## Stay in the state          ## Stay in the state
2062          !!!next-input-character;          !!!next-input-character;
2063          redo A;          redo A;
2064        } elsif ($self->{next_input_character} eq 0x0022) { # "        } elsif ($self->{next_char} eq 0x0022) { # "
2065            !!!cp (182);
2066          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{current_token}->{public_identifier} = ''; # DOCTYPE
2067          $self->{state} = 'DOCTYPE public identifier (double-quoted)';          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;
2068          !!!next-input-character;          !!!next-input-character;
2069          redo A;          redo A;
2070        } elsif ($self->{next_input_character} eq 0x0027) { # '        } elsif ($self->{next_char} eq 0x0027) { # '
2071            !!!cp (183);
2072          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{current_token}->{public_identifier} = ''; # DOCTYPE
2073          $self->{state} = 'DOCTYPE public identifier (single-quoted)';          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;
2074          !!!next-input-character;          !!!next-input-character;
2075          redo A;          redo A;
2076        } elsif ($self->{next_input_character} eq 0x003E) { # >        } elsif ($self->{next_char} eq 0x003E) { # >
2077            !!!cp (184);
2078          !!!parse-error (type => 'no PUBLIC literal');          !!!parse-error (type => 'no PUBLIC literal');
2079    
2080          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2081          !!!next-input-character;          !!!next-input-character;
2082    
2083          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2084          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2085    
2086          redo A;          redo A;
2087        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2088            !!!cp (185);
2089          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2090    
2091          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2092          ## reconsume          ## reconsume
2093    
2094          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2095          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2096    
2097          redo A;          redo A;
2098        } else {        } else {
2099            !!!cp (186);
2100          !!!parse-error (type => 'string after PUBLIC');          !!!parse-error (type => 'string after PUBLIC');
2101          $self->{state} = 'bogus DOCTYPE';          $self->{current_token}->{quirks} = 1;
2102    
2103            $self->{state} = BOGUS_DOCTYPE_STATE;
2104          !!!next-input-character;          !!!next-input-character;
2105          redo A;          redo A;
2106        }        }
2107      } elsif ($self->{state} eq 'DOCTYPE public identifier (double-quoted)') {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2108        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
2109          $self->{state} = 'after DOCTYPE public identifier';          !!!cp (187);
2110            $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2111          !!!next-input-character;          !!!next-input-character;
2112          redo A;          redo A;
2113        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == 0x003E) { # >
2114            !!!cp (188);
2115          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
2116    
2117          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2118            !!!next-input-character;
2119    
2120            $self->{current_token}->{quirks} = 1;
2121            !!!emit ($self->{current_token}); # DOCTYPE
2122    
2123            redo A;
2124          } elsif ($self->{next_char} == -1) {
2125            !!!cp (189);
2126            !!!parse-error (type => 'unclosed PUBLIC literal');
2127    
2128            $self->{state} = DATA_STATE;
2129          ## reconsume          ## reconsume
2130    
2131          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2132          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2133    
2134          redo A;          redo A;
2135        } else {        } else {
2136            !!!cp (190);
2137          $self->{current_token}->{public_identifier} # DOCTYPE          $self->{current_token}->{public_identifier} # DOCTYPE
2138              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2139          ## Stay in the state          ## Stay in the state
2140          !!!next-input-character;          !!!next-input-character;
2141          redo A;          redo A;
2142        }        }
2143      } elsif ($self->{state} eq 'DOCTYPE public identifier (single-quoted)') {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {
2144        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
2145          $self->{state} = 'after DOCTYPE public identifier';          !!!cp (191);
2146            $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2147          !!!next-input-character;          !!!next-input-character;
2148          redo A;          redo A;
2149        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == 0x003E) { # >
2150            !!!cp (192);
2151          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
2152    
2153          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2154            !!!next-input-character;
2155    
2156            $self->{current_token}->{quirks} = 1;
2157            !!!emit ($self->{current_token}); # DOCTYPE
2158    
2159            redo A;
2160          } elsif ($self->{next_char} == -1) {
2161            !!!cp (193);
2162            !!!parse-error (type => 'unclosed PUBLIC literal');
2163    
2164            $self->{state} = DATA_STATE;
2165          ## reconsume          ## reconsume
2166    
2167          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2168          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2169    
2170          redo A;          redo A;
2171        } else {        } else {
2172            !!!cp (194);
2173          $self->{current_token}->{public_identifier} # DOCTYPE          $self->{current_token}->{public_identifier} # DOCTYPE
2174              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2175          ## Stay in the state          ## Stay in the state
2176          !!!next-input-character;          !!!next-input-character;
2177          redo A;          redo A;
2178        }        }
2179      } elsif ($self->{state} eq 'after DOCTYPE public identifier') {      } elsif ($self->{state} == AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
2180        if ({        if ({
2181              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2182              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2183            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2184            !!!cp (195);
2185          ## Stay in the state          ## Stay in the state
2186          !!!next-input-character;          !!!next-input-character;
2187          redo A;          redo A;
2188        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
2189            !!!cp (196);
2190          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2191          $self->{state} = 'DOCTYPE system identifier (double-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2192          !!!next-input-character;          !!!next-input-character;
2193          redo A;          redo A;
2194        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
2195            !!!cp (197);
2196          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2197          $self->{state} = 'DOCTYPE system identifier (single-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2198          !!!next-input-character;          !!!next-input-character;
2199          redo A;          redo A;
2200        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2201          $self->{state} = 'data';          !!!cp (198);
2202            $self->{state} = DATA_STATE;
2203          !!!next-input-character;          !!!next-input-character;
2204    
2205          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2206    
2207          redo A;          redo A;
2208        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2209            !!!cp (199);
2210          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2211    
2212          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2213          ## reconsume          ## reconsume
2214    
2215          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2216          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2217    
2218          redo A;          redo A;
2219        } else {        } else {
2220            !!!cp (200);
2221          !!!parse-error (type => 'string after PUBLIC literal');          !!!parse-error (type => 'string after PUBLIC literal');
2222          $self->{state} = 'bogus DOCTYPE';          $self->{current_token}->{quirks} = 1;
2223    
2224            $self->{state} = BOGUS_DOCTYPE_STATE;
2225          !!!next-input-character;          !!!next-input-character;
2226          redo A;          redo A;
2227        }        }
2228      } elsif ($self->{state} eq 'before DOCTYPE system identifier') {      } elsif ($self->{state} == BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
2229        if ({        if ({
2230              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2231              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2232            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2233            !!!cp (201);
2234          ## Stay in the state          ## Stay in the state
2235          !!!next-input-character;          !!!next-input-character;
2236          redo A;          redo A;
2237        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
2238            !!!cp (202);
2239          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2240          $self->{state} = 'DOCTYPE system identifier (double-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2241          !!!next-input-character;          !!!next-input-character;
2242          redo A;          redo A;
2243        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
2244            !!!cp (203);
2245          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2246          $self->{state} = 'DOCTYPE system identifier (single-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2247          !!!next-input-character;          !!!next-input-character;
2248          redo A;          redo A;
2249        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2250            !!!cp (204);
2251          !!!parse-error (type => 'no SYSTEM literal');          !!!parse-error (type => 'no SYSTEM literal');
2252          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2253          !!!next-input-character;          !!!next-input-character;
2254    
2255          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2256          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2257    
2258          redo A;          redo A;
2259        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2260            !!!cp (205);
2261          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2262    
2263          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2264          ## reconsume          ## reconsume
2265    
2266          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2267          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2268    
2269          redo A;          redo A;
2270        } else {        } else {
2271            !!!cp (206);
2272          !!!parse-error (type => 'string after SYSTEM');          !!!parse-error (type => 'string after SYSTEM');
2273          $self->{state} = 'bogus DOCTYPE';          $self->{current_token}->{quirks} = 1;
2274    
2275            $self->{state} = BOGUS_DOCTYPE_STATE;
2276          !!!next-input-character;          !!!next-input-character;
2277          redo A;          redo A;
2278        }        }
2279      } elsif ($self->{state} eq 'DOCTYPE system identifier (double-quoted)') {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2280        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
2281          $self->{state} = 'after DOCTYPE system identifier';          !!!cp (207);
2282            $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2283          !!!next-input-character;          !!!next-input-character;
2284          redo A;          redo A;
2285        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == 0x003E) { # >
2286            !!!cp (208);
2287            !!!parse-error (type => 'unclosed PUBLIC literal');
2288    
2289            $self->{state} = DATA_STATE;
2290            !!!next-input-character;
2291    
2292            $self->{current_token}->{quirks} = 1;
2293            !!!emit ($self->{current_token}); # DOCTYPE
2294    
2295            redo A;
2296          } elsif ($self->{next_char} == -1) {
2297            !!!cp (209);
2298          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2299    
2300          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2301          ## reconsume          ## reconsume
2302    
2303          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2304          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2305    
2306          redo A;          redo A;
2307        } else {        } else {
2308            !!!cp (210);
2309          $self->{current_token}->{system_identifier} # DOCTYPE          $self->{current_token}->{system_identifier} # DOCTYPE
2310              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2311          ## Stay in the state          ## Stay in the state
2312          !!!next-input-character;          !!!next-input-character;
2313          redo A;          redo A;
2314        }        }
2315      } elsif ($self->{state} eq 'DOCTYPE system identifier (single-quoted)') {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {
2316        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
2317          $self->{state} = 'after DOCTYPE system identifier';          !!!cp (211);
2318            $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2319          !!!next-input-character;          !!!next-input-character;
2320          redo A;          redo A;
2321        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == 0x003E) { # >
2322            !!!cp (212);
2323            !!!parse-error (type => 'unclosed PUBLIC literal');
2324    
2325            $self->{state} = DATA_STATE;
2326            !!!next-input-character;
2327    
2328            $self->{current_token}->{quirks} = 1;
2329            !!!emit ($self->{current_token}); # DOCTYPE
2330    
2331            redo A;
2332          } elsif ($self->{next_char} == -1) {
2333            !!!cp (213);
2334          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2335    
2336          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2337          ## reconsume          ## reconsume
2338    
2339          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2340          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2341    
2342          redo A;          redo A;
2343        } else {        } else {
2344            !!!cp (214);
2345          $self->{current_token}->{system_identifier} # DOCTYPE          $self->{current_token}->{system_identifier} # DOCTYPE
2346              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2347          ## Stay in the state          ## Stay in the state
2348          !!!next-input-character;          !!!next-input-character;
2349          redo A;          redo A;
2350        }        }
2351      } elsif ($self->{state} eq 'after DOCTYPE system identifier') {      } elsif ($self->{state} == AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
2352        if ({        if ({
2353              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2354              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2355            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2356            !!!cp (215);
2357          ## Stay in the state          ## Stay in the state
2358          !!!next-input-character;          !!!next-input-character;
2359          redo A;          redo A;
2360        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2361          $self->{state} = 'data';          !!!cp (216);
2362            $self->{state} = DATA_STATE;
2363          !!!next-input-character;          !!!next-input-character;
2364    
2365          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2366    
2367          redo A;          redo A;
2368        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2369            !!!cp (217);
2370          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2371    
2372          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2373          ## reconsume          ## reconsume
2374    
2375          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2376          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2377    
2378          redo A;          redo A;
2379        } else {        } else {
2380            !!!cp (218);
2381          !!!parse-error (type => 'string after SYSTEM literal');          !!!parse-error (type => 'string after SYSTEM literal');
2382          $self->{state} = 'bogus DOCTYPE';          #$self->{current_token}->{quirks} = 1;
2383    
2384            $self->{state} = BOGUS_DOCTYPE_STATE;
2385          !!!next-input-character;          !!!next-input-character;
2386          redo A;          redo A;
2387        }        }
2388      } elsif ($self->{state} eq 'bogus DOCTYPE') {      } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {
2389        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_char} == 0x003E) { # >
2390          $self->{state} = 'data';          !!!cp (219);
2391            $self->{state} = DATA_STATE;
2392          !!!next-input-character;          !!!next-input-character;
2393    
         delete $self->{current_token}->{correct};  
2394          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2395    
2396          redo A;          redo A;
2397        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2398            !!!cp (220);
2399          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2400          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2401          ## reconsume          ## reconsume
2402    
         delete $self->{current_token}->{correct};  
2403          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2404    
2405          redo A;          redo A;
2406        } else {        } else {
2407            !!!cp (221);
2408          ## Stay in the state          ## Stay in the state
2409          !!!next-input-character;          !!!next-input-character;
2410          redo A;          redo A;
# Line 1644  sub _get_next_token ($) { Line 2417  sub _get_next_token ($) {
2417    die "$0: _get_next_token: unexpected case";    die "$0: _get_next_token: unexpected case";
2418  } # _get_next_token  } # _get_next_token
2419    
2420  sub _tokenize_attempt_to_consume_an_entity ($$) {  sub _tokenize_attempt_to_consume_an_entity ($$$) {
2421    my ($self, $in_attr) = @_;    my ($self, $in_attr, $additional) = @_;
2422    
2423      my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
2424    
2425    if ({    if ({
2426         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,
2427         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR
2428        }->{$self->{next_input_character}}) {         $additional => 1,
2429          }->{$self->{next_char}}) {
2430        !!!cp (1001);
2431      ## Don't consume      ## Don't consume
2432      ## No error      ## No error
2433      return undef;      return undef;
2434    } elsif ($self->{next_input_character} == 0x0023) { # #    } elsif ($self->{next_char} == 0x0023) { # #
2435      !!!next-input-character;      !!!next-input-character;
2436      if ($self->{next_input_character} == 0x0078 or # x      if ($self->{next_char} == 0x0078 or # x
2437          $self->{next_input_character} == 0x0058) { # X          $self->{next_char} == 0x0058) { # X
2438        my $code;        my $code;
2439        X: {        X: {
2440          my $x_char = $self->{next_input_character};          my $x_char = $self->{next_char};
2441          !!!next-input-character;          !!!next-input-character;
2442          if (0x0030 <= $self->{next_input_character} and          if (0x0030 <= $self->{next_char} and
2443              $self->{next_input_character} <= 0x0039) { # 0..9              $self->{next_char} <= 0x0039) { # 0..9
2444              !!!cp (1002);
2445            $code ||= 0;            $code ||= 0;
2446            $code *= 0x10;            $code *= 0x10;
2447            $code += $self->{next_input_character} - 0x0030;            $code += $self->{next_char} - 0x0030;
2448            redo X;            redo X;
2449          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_char} and
2450                   $self->{next_input_character} <= 0x0066) { # a..f                   $self->{next_char} <= 0x0066) { # a..f
2451              !!!cp (1003);
2452            $code ||= 0;            $code ||= 0;
2453            $code *= 0x10;            $code *= 0x10;
2454            $code += $self->{next_input_character} - 0x0060 + 9;            $code += $self->{next_char} - 0x0060 + 9;
2455            redo X;            redo X;
2456          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_char} and
2457                   $self->{next_input_character} <= 0x0046) { # A..F                   $self->{next_char} <= 0x0046) { # A..F
2458              !!!cp (1004);
2459            $code ||= 0;            $code ||= 0;
2460            $code *= 0x10;            $code *= 0x10;
2461            $code += $self->{next_input_character} - 0x0040 + 9;            $code += $self->{next_char} - 0x0040 + 9;
2462            redo X;            redo X;
2463          } elsif (not defined $code) { # no hexadecimal digit          } elsif (not defined $code) { # no hexadecimal digit
2464            !!!parse-error (type => 'bare hcro');            !!!cp (1005);
2465            !!!back-next-input-character ($x_char, $self->{next_input_character});            !!!parse-error (type => 'bare hcro', line => $l, column => $c);
2466            $self->{next_input_character} = 0x0023; # #            !!!back-next-input-character ($x_char, $self->{next_char});
2467              $self->{next_char} = 0x0023; # #
2468            return undef;            return undef;
2469          } elsif ($self->{next_input_character} == 0x003B) { # ;          } elsif ($self->{next_char} == 0x003B) { # ;
2470              !!!cp (1006);
2471            !!!next-input-character;            !!!next-input-character;
2472          } else {          } else {
2473            !!!parse-error (type => 'no refc');            !!!cp (1007);
2474              !!!parse-error (type => 'no refc', line => $l, column => $c);
2475          }          }
2476    
2477          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2478            !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);            !!!cp (1008);
2479              !!!parse-error (type => (sprintf 'invalid character reference:U+%04X', $code), line => $l, column => $c);
2480            $code = 0xFFFD;            $code = 0xFFFD;
2481          } elsif ($code > 0x10FFFF) {          } elsif ($code > 0x10FFFF) {
2482            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);            !!!cp (1009);
2483              !!!parse-error (type => (sprintf 'invalid character reference:U-%08X', $code), line => $l, column => $c);
2484            $code = 0xFFFD;            $code = 0xFFFD;
2485          } elsif ($code == 0x000D) {          } elsif ($code == 0x000D) {
2486            !!!parse-error (type => 'CR character reference');            !!!cp (1010);
2487              !!!parse-error (type => 'CR character reference', line => $l, column => $c);
2488            $code = 0x000A;            $code = 0x000A;
2489          } elsif (0x80 <= $code and $code <= 0x9F) {          } elsif (0x80 <= $code and $code <= 0x9F) {
2490            !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);            !!!cp (1011);
2491              !!!parse-error (type => (sprintf 'C1 character reference:U+%04X', $code), line => $l, column => $c);
2492            $code = $c1_entity_char->{$code};            $code = $c1_entity_char->{$code};
2493          }          }
2494    
2495          return {type => CHARACTER_TOKEN, data => chr $code};          return {type => CHARACTER_TOKEN, data => chr $code,
2496                    has_reference => 1,
2497                    line => $l, column => $c,
2498                   };
2499        } # X        } # X
2500      } elsif (0x0030 <= $self->{next_input_character} and      } elsif (0x0030 <= $self->{next_char} and
2501               $self->{next_input_character} <= 0x0039) { # 0..9               $self->{next_char} <= 0x0039) { # 0..9
2502        my $code = $self->{next_input_character} - 0x0030;        my $code = $self->{next_char} - 0x0030;
2503        !!!next-input-character;        !!!next-input-character;
2504                
2505        while (0x0030 <= $self->{next_input_character} and        while (0x0030 <= $self->{next_char} and
2506                  $self->{next_input_character} <= 0x0039) { # 0..9                  $self->{next_char} <= 0x0039) { # 0..9
2507            !!!cp (1012);
2508          $code *= 10;          $code *= 10;
2509          $code += $self->{next_input_character} - 0x0030;          $code += $self->{next_char} - 0x0030;
2510                    
2511          !!!next-input-character;          !!!next-input-character;
2512        }        }
2513    
2514        if ($self->{next_input_character} == 0x003B) { # ;        if ($self->{next_char} == 0x003B) { # ;
2515            !!!cp (1013);
2516          !!!next-input-character;          !!!next-input-character;
2517        } else {        } else {
2518          !!!parse-error (type => 'no refc');          !!!cp (1014);
2519            !!!parse-error (type => 'no refc', line => $l, column => $c);
2520        }        }
2521    
2522        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2523          !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);          !!!cp (1015);
2524            !!!parse-error (type => (sprintf 'invalid character reference:U+%04X', $code), line => $l, column => $c);
2525          $code = 0xFFFD;          $code = 0xFFFD;
2526        } elsif ($code > 0x10FFFF) {        } elsif ($code > 0x10FFFF) {
2527          !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);          !!!cp (1016);
2528            !!!parse-error (type => (sprintf 'invalid character reference:U-%08X', $code), line => $l, column => $c);
2529          $code = 0xFFFD;          $code = 0xFFFD;
2530        } elsif ($code == 0x000D) {        } elsif ($code == 0x000D) {
2531          !!!parse-error (type => 'CR character reference');          !!!cp (1017);
2532            !!!parse-error (type => 'CR character reference', line => $l, column => $c);
2533          $code = 0x000A;          $code = 0x000A;
2534        } elsif (0x80 <= $code and $code <= 0x9F) {        } elsif (0x80 <= $code and $code <= 0x9F) {
2535          !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);          !!!cp (1018);
2536            !!!parse-error (type => (sprintf 'C1 character reference:U+%04X', $code), line => $l, column => $c);
2537          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
2538        }        }
2539                
2540        return {type => CHARACTER_TOKEN, data => chr $code};        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1,
2541                  line => $l, column => $c,
2542                 };
2543      } else {      } else {
2544        !!!parse-error (type => 'bare nero');        !!!cp (1019);
2545        !!!back-next-input-character ($self->{next_input_character});        !!!parse-error (type => 'bare nero', line => $l, column => $c);
2546        $self->{next_input_character} = 0x0023; # #        !!!back-next-input-character ($self->{next_char});
2547          $self->{next_char} = 0x0023; # #
2548        return undef;        return undef;
2549      }      }
2550    } elsif ((0x0041 <= $self->{next_input_character} and    } elsif ((0x0041 <= $self->{next_char} and
2551              $self->{next_input_character} <= 0x005A) or              $self->{next_char} <= 0x005A) or
2552             (0x0061 <= $self->{next_input_character} and             (0x0061 <= $self->{next_char} and
2553              $self->{next_input_character} <= 0x007A)) {              $self->{next_char} <= 0x007A)) {
2554      my $entity_name = chr $self->{next_input_character};      my $entity_name = chr $self->{next_char};
2555      !!!next-input-character;      !!!next-input-character;
2556    
2557      my $value = $entity_name;      my $value = $entity_name;
# Line 1761  sub _tokenize_attempt_to_consume_an_enti Line 2561  sub _tokenize_attempt_to_consume_an_enti
2561    
2562      while (length $entity_name < 10 and      while (length $entity_name < 10 and
2563             ## NOTE: Some number greater than the maximum length of entity name             ## NOTE: Some number greater than the maximum length of entity name
2564             ((0x0041 <= $self->{next_input_character} and # a             ((0x0041 <= $self->{next_char} and # a
2565               $self->{next_input_character} <= 0x005A) or # x               $self->{next_char} <= 0x005A) or # x
2566              (0x0061 <= $self->{next_input_character} and # a              (0x0061 <= $self->{next_char} and # a
2567               $self->{next_input_character} <= 0x007A) or # z               $self->{next_char} <= 0x007A) or # z
2568              (0x0030 <= $self->{next_input_character} and # 0              (0x0030 <= $self->{next_char} and # 0
2569               $self->{next_input_character} <= 0x0039) or # 9               $self->{next_char} <= 0x0039) or # 9
2570              $self->{next_input_character} == 0x003B)) { # ;              $self->{next_char} == 0x003B)) { # ;
2571        $entity_name .= chr $self->{next_input_character};        $entity_name .= chr $self->{next_char};
2572        if (defined $EntityChar->{$entity_name}) {        if (defined $EntityChar->{$entity_name}) {
2573          if ($self->{next_input_character} == 0x003B) { # ;          if ($self->{next_char} == 0x003B) { # ;
2574              !!!cp (1020);
2575            $value = $EntityChar->{$entity_name};            $value = $EntityChar->{$entity_name};
2576            $match = 1;            $match = 1;
2577            !!!next-input-character;            !!!next-input-character;
2578            last;            last;
2579          } else {          } else {
2580              !!!cp (1021);
2581            $value = $EntityChar->{$entity_name};            $value = $EntityChar->{$entity_name};
2582            $match = -1;            $match = -1;
2583            !!!next-input-character;            !!!next-input-character;
2584          }          }
2585        } else {        } else {
2586          $value .= chr $self->{next_input_character};          !!!cp (1022);
2587            $value .= chr $self->{next_char};
2588          $match *= 2;          $match *= 2;
2589          !!!next-input-character;          !!!next-input-character;
2590        }        }
2591      }      }
2592            
2593      if ($match > 0) {      if ($match > 0) {
2594        return {type => CHARACTER_TOKEN, data => $value};        !!!cp (1023);
2595          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
2596                  line => $l, column => $c,
2597                 };
2598      } elsif ($match < 0) {      } elsif ($match < 0) {
2599        !!!parse-error (type => 'no refc');        !!!parse-error (type => 'no refc', line => $l, column => $c);
2600        if ($in_attr and $match < -1) {        if ($in_attr and $match < -1) {
2601          return {type => CHARACTER_TOKEN, data => '&'.$entity_name};          !!!cp (1024);
2602        } else {          return {type => CHARACTER_TOKEN, data => '&'.$entity_name,
2603          return {type => CHARACTER_TOKEN, data => $value};                  line => $l, column => $c,
2604                   };
2605          } else {
2606            !!!cp (1025);
2607            return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
2608                    line => $l, column => $c,
2609                   };
2610        }        }
2611      } else {      } else {
2612        !!!parse-error (type => 'bare ero');        !!!cp (1026);
2613        ## NOTE: No characters are consumed in the spec.        !!!parse-error (type => 'bare ero', line => $l, column => $c);
2614        return {type => CHARACTER_TOKEN, data => '&'.$value};        ## NOTE: "No characters are consumed" in the spec.
2615          return {type => CHARACTER_TOKEN, data => '&'.$value,
2616                  line => $l, column => $c,
2617                 };
2618      }      }
2619    } else {    } else {
2620        !!!cp (1027);
2621      ## no characters are consumed      ## no characters are consumed
2622      !!!parse-error (type => 'bare ero');      !!!parse-error (type => 'bare ero', line => $l, column => $c);
2623      return undef;      return undef;
2624    }    }
2625  } # _tokenize_attempt_to_consume_an_entity  } # _tokenize_attempt_to_consume_an_entity
# Line 1841  sub _construct_tree ($) { Line 2657  sub _construct_tree ($) {
2657        
2658    !!!next-token;    !!!next-token;
2659    
   $self->{insertion_mode} = BEFORE_HEAD_IM;  
2660    undef $self->{form_element};    undef $self->{form_element};
2661    undef $self->{head_element};    undef $self->{head_element};
2662    $self->{open_elements} = [];    $self->{open_elements} = [];
2663    undef $self->{inner_html_node};    undef $self->{inner_html_node};
2664    
2665      ## NOTE: The "initial" insertion mode.
2666    $self->_tree_construction_initial; # MUST    $self->_tree_construction_initial; # MUST
2667    
2668      ## NOTE: The "before html" insertion mode.
2669    $self->_tree_construction_root_element;    $self->_tree_construction_root_element;
2670      $self->{insertion_mode} = BEFORE_HEAD_IM;
2671    
2672      ## NOTE: The "before head" insertion mode and so on.
2673    $self->_tree_construction_main;    $self->_tree_construction_main;
2674  } # _construct_tree  } # _construct_tree
2675    
2676  sub _tree_construction_initial ($) {  sub _tree_construction_initial ($) {
2677    my $self = shift;    my $self = shift;
2678    
2679      ## NOTE: "initial" insertion mode
2680    
2681    INITIAL: {    INITIAL: {
2682      if ($token->{type} == DOCTYPE_TOKEN) {      if ($token->{type} == DOCTYPE_TOKEN) {
2683        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"
# Line 1865  sub _tree_construction_initial ($) { Line 2689  sub _tree_construction_initial ($) {
2689        if (not defined $token->{name} or # <!DOCTYPE>        if (not defined $token->{name} or # <!DOCTYPE>
2690            defined $token->{public_identifier} or            defined $token->{public_identifier} or
2691            defined $token->{system_identifier}) {            defined $token->{system_identifier}) {
2692          !!!parse-error (type => 'not HTML5');          !!!cp ('t1');
2693            !!!parse-error (type => 'not HTML5', token => $token);
2694        } elsif ($doctype_name ne 'HTML') {        } elsif ($doctype_name ne 'HTML') {
2695            !!!cp ('t2');
2696          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)
2697          !!!parse-error (type => 'not HTML5');          !!!parse-error (type => 'not HTML5', token => $token);
2698          } else {
2699            !!!cp ('t3');
2700        }        }
2701                
2702        my $doctype = $self->{document}->create_document_type_definition        my $doctype = $self->{document}->create_document_type_definition
2703          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
2704          ## NOTE: Default value for both |public_id| and |system_id| attributes
2705          ## are empty strings, so that we don't set any value in missing cases.
2706        $doctype->public_id ($token->{public_identifier})        $doctype->public_id ($token->{public_identifier})
2707            if defined $token->{public_identifier};            if defined $token->{public_identifier};
2708        $doctype->system_id ($token->{system_identifier})        $doctype->system_id ($token->{system_identifier})
# Line 1881  sub _tree_construction_initial ($) { Line 2711  sub _tree_construction_initial ($) {
2711        ## ISSUE: internalSubset = null??        ## ISSUE: internalSubset = null??
2712        $self->{document}->append_child ($doctype);        $self->{document}->append_child ($doctype);
2713                
2714        if (not $token->{correct} or $doctype_name ne 'HTML') {        if ($token->{quirks} or $doctype_name ne 'HTML') {
2715            !!!cp ('t4');
2716          $self->{document}->manakai_compat_mode ('quirks');          $self->{document}->manakai_compat_mode ('quirks');
2717        } elsif (defined $token->{public_identifier}) {        } elsif (defined $token->{public_identifier}) {
2718          my $pubid = $token->{public_identifier};          my $pubid = $token->{public_identifier};
# Line 1935  sub _tree_construction_initial ($) { Line 2766  sub _tree_construction_initial ($) {
2766            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,
2767            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,
2768            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,
2769              "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//EN" => 1,
2770              "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//EN" => 1,
2771              "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//EN" => 1,
2772            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,
2773            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,
2774            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,
# Line 1957  sub _tree_construction_initial ($) { Line 2791  sub _tree_construction_initial ($) {
2791            "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" => 1,            "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" => 1,
2792            "HTML" => 1,            "HTML" => 1,
2793          }->{$pubid}) {          }->{$pubid}) {
2794              !!!cp ('t5');
2795            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
2796          } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or          } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or
2797                   $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {                   $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {
2798            if (defined $token->{system_identifier}) {            if (defined $token->{system_identifier}) {
2799                !!!cp ('t6');
2800              $self->{document}->manakai_compat_mode ('quirks');              $self->{document}->manakai_compat_mode ('quirks');
2801            } else {            } else {
2802                !!!cp ('t7');
2803              $self->{document}->manakai_compat_mode ('limited quirks');              $self->{document}->manakai_compat_mode ('limited quirks');
2804            }            }
2805          } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 Frameset//EN" or          } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 FRAMESET//EN" or
2806                   $pubid eq "-//W3C//DTD XHTML 1.0 Transitional//EN") {                   $pubid eq "-//W3C//DTD XHTML 1.0 TRANSITIONAL//EN") {
2807              !!!cp ('t8');
2808            $self->{document}->manakai_compat_mode ('limited quirks');            $self->{document}->manakai_compat_mode ('limited quirks');
2809            } else {
2810              !!!cp ('t9');
2811          }          }
2812          } else {
2813            !!!cp ('t10');
2814        }        }
2815        if (defined $token->{system_identifier}) {        if (defined $token->{system_identifier}) {
2816          my $sysid = $token->{system_identifier};          my $sysid = $token->{system_identifier};
2817          $sysid =~ tr/A-Z/a-z/;          $sysid =~ tr/A-Z/a-z/;
2818          if ($sysid eq "http://www.ibm.com/data/dtd/v11/ibmxhtml1-transitional.dtd") {          if ($sysid eq "http://www.ibm.com/data/dtd/v11/ibmxhtml1-transitional.dtd") {
2819              ## TODO: Check the spec: PUBLIC "(limited quirks)" "(quirks)"
2820            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
2821              !!!cp ('t11');
2822            } else {
2823              !!!cp ('t12');
2824          }          }
2825          } else {
2826            !!!cp ('t13');
2827        }        }
2828                
2829        ## Go to the root element phase.        ## Go to the "before html" insertion mode.
2830        !!!next-token;        !!!next-token;
2831        return;        return;
2832      } elsif ({      } elsif ({
# Line 1986  sub _tree_construction_initial ($) { Line 2834  sub _tree_construction_initial ($) {
2834                END_TAG_TOKEN, 1,                END_TAG_TOKEN, 1,
2835                END_OF_FILE_TOKEN, 1,                END_OF_FILE_TOKEN, 1,
2836               }->{$token->{type}}) {               }->{$token->{type}}) {
2837        !!!parse-error (type => 'no DOCTYPE');        !!!cp ('t14');
2838          !!!parse-error (type => 'no DOCTYPE', token => $token);
2839        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
2840        ## Go to the root element phase        ## Go to the "before html" insertion mode.
2841        ## reprocess        ## reprocess
2842          !!!ack-later;
2843        return;        return;
2844      } elsif ($token->{type} == CHARACTER_TOKEN) {      } elsif ($token->{type} == CHARACTER_TOKEN) {
2845        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
2846          ## Ignore the token          ## Ignore the token
2847    
2848          unless (length $token->{data}) {          unless (length $token->{data}) {
2849            ## Stay in the phase            !!!cp ('t15');
2850              ## Stay in the insertion mode.
2851            !!!next-token;            !!!next-token;
2852            redo INITIAL;            redo INITIAL;
2853            } else {
2854              !!!cp ('t16');
2855          }          }
2856          } else {
2857            !!!cp ('t17');
2858        }        }
2859    
2860        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE', token => $token);
2861        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
2862        ## Go to the root element phase        ## Go to the "before html" insertion mode.
2863        ## reprocess        ## reprocess
2864        return;        return;
2865      } elsif ($token->{type} == COMMENT_TOKEN) {      } elsif ($token->{type} == COMMENT_TOKEN) {
2866          !!!cp ('t18');
2867        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
2868        $self->{document}->append_child ($comment);        $self->{document}->append_child ($comment);
2869                
2870        ## Stay in the phase.        ## Stay in the insertion mode.
2871        !!!next-token;        !!!next-token;
2872        redo INITIAL;        redo INITIAL;
2873      } else {      } else {
2874        die "$0: $token->{type}: Unknown token type";        die "$0: $token->{type}: Unknown token type";
2875      }      }
2876    } # INITIAL    } # INITIAL
2877    
2878      die "$0: _tree_construction_initial: This should be never reached";
2879  } # _tree_construction_initial  } # _tree_construction_initial
2880    
2881  sub _tree_construction_root_element ($) {  sub _tree_construction_root_element ($) {
2882    my $self = shift;    my $self = shift;
2883    
2884      ## NOTE: "before html" insertion mode.
2885        
2886    B: {    B: {
2887        if ($token->{type} == DOCTYPE_TOKEN) {        if ($token->{type} == DOCTYPE_TOKEN) {
2888          !!!parse-error (type => 'in html:#DOCTYPE');          !!!cp ('t19');
2889            !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
2890          ## Ignore the token          ## Ignore the token
2891          ## Stay in the phase          ## Stay in the insertion mode.
2892          !!!next-token;          !!!next-token;
2893          redo B;          redo B;
2894        } elsif ($token->{type} == COMMENT_TOKEN) {        } elsif ($token->{type} == COMMENT_TOKEN) {
2895            !!!cp ('t20');
2896          my $comment = $self->{document}->create_comment ($token->{data});          my $comment = $self->{document}->create_comment ($token->{data});
2897          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
2898          ## Stay in the phase          ## Stay in the insertion mode.
2899          !!!next-token;          !!!next-token;
2900          redo B;          redo B;
2901        } elsif ($token->{type} == CHARACTER_TOKEN) {        } elsif ($token->{type} == CHARACTER_TOKEN) {
# Line 2041  sub _tree_construction_root_element ($) Line 2903  sub _tree_construction_root_element ($)
2903            ## Ignore the token.            ## Ignore the token.
2904    
2905            unless (length $token->{data}) {            unless (length $token->{data}) {
2906              ## Stay in the phase              !!!cp ('t21');
2907                ## Stay in the insertion mode.
2908              !!!next-token;              !!!next-token;
2909              redo B;              redo B;
2910              } else {
2911                !!!cp ('t22');
2912            }            }
2913            } else {
2914              !!!cp ('t23');
2915          }          }
2916    
2917            $self->{application_cache_selection}->(undef);
2918    
2919          #          #
2920          } elsif ($token->{type} == START_TAG_TOKEN) {
2921            if ($token->{tag_name} eq 'html') {
2922              my $root_element;
2923              !!!create-element ($root_element, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
2924              $self->{document}->append_child ($root_element);
2925              push @{$self->{open_elements}},
2926                  [$root_element, $el_category->{html}];
2927    
2928              if ($token->{attributes}->{manifest}) {
2929                !!!cp ('t24');
2930                $self->{application_cache_selection}
2931                    ->($token->{attributes}->{manifest}->{value});
2932                ## ISSUE: Spec is unclear on relative references.
2933                ## According to Hixie (#whatwg 2008-03-19), it should be
2934                ## resolved against the base URI of the document in HTML
2935                ## or xml:base of the element in XHTML.
2936              } else {
2937                !!!cp ('t25');
2938                $self->{application_cache_selection}->(undef);
2939              }
2940    
2941              !!!nack ('t25c');
2942    
2943              !!!next-token;
2944              return; ## Go to the "before head" insertion mode.
2945            } else {
2946              !!!cp ('t25.1');
2947              #
2948            }
2949        } elsif ({        } elsif ({
                 START_TAG_TOKEN, 1,  
2950                  END_TAG_TOKEN, 1,                  END_TAG_TOKEN, 1,
2951                  END_OF_FILE_TOKEN, 1,                  END_OF_FILE_TOKEN, 1,
2952                 }->{$token->{type}}) {                 }->{$token->{type}}) {
2953          ## ISSUE: There is an issue in the spec          !!!cp ('t26');
2954          #          #
2955        } else {        } else {
2956          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
2957        }        }
2958        my $root_element; !!!create-element ($root_element, 'html');  
2959        $self->{document}->append_child ($root_element);      my $root_element;
2960        push @{$self->{open_elements}}, [$root_element, 'html'];      !!!create-element ($root_element, $HTML_NS, 'html',, $token);
2961        ## reprocess      $self->{document}->append_child ($root_element);
2962        #redo B;      push @{$self->{open_elements}}, [$root_element, $el_category->{html}];
2963        return; ## Go to the main phase.  
2964        $self->{application_cache_selection}->(undef);
2965    
2966        ## NOTE: Reprocess the token.
2967        !!!ack-later;
2968        return; ## Go to the "before head" insertion mode.
2969    
2970        ## ISSUE: There is an issue in the spec
2971    } # B    } # B
2972    
2973      die "$0: _tree_construction_root_element: This should never be reached";
2974  } # _tree_construction_root_element  } # _tree_construction_root_element
2975    
2976  sub _reset_insertion_mode ($) {  sub _reset_insertion_mode ($) {
# Line 2078  sub _reset_insertion_mode ($) { Line 2985  sub _reset_insertion_mode ($) {
2985            
2986      ## Step 3      ## Step 3
2987      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"!?  
2988        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
2989          $last = 1;          $last = 1;
2990          if (defined $self->{inner_html_node}) {          if (defined $self->{inner_html_node}) {
2991            if ($self->{inner_html_node}->[1] eq 'td' or            if ($self->{inner_html_node}->[1] & TABLE_CELL_EL) {
2992                $self->{inner_html_node}->[1] eq 'th') {              !!!cp ('t27');
2993              #              #
2994            } else {            } else {
2995                !!!cp ('t28');
2996              $node = $self->{inner_html_node};              $node = $self->{inner_html_node};
2997            }            }
2998          }          }
2999        }        }
3000            
3001        ## Step 4..13      ## Step 4..14
3002        my $new_mode = {      my $new_mode;
3003        if ($node->[1] & FOREIGN_EL) {
3004          ## NOTE: Strictly spaking, the line below only applies to MathML and
3005          ## SVG elements.  Currently the HTML syntax supports only MathML and
3006          ## SVG elements as foreigners.
3007          $new_mode = $self->{insertion_mode} | IN_FOREIGN_CONTENT_IM;
3008          ## ISSUE: What is set as the secondary insertion mode?
3009        } else {
3010          $new_mode = {
3011                        select => IN_SELECT_IM,                        select => IN_SELECT_IM,
3012                          ## NOTE: |option| and |optgroup| do not set
3013                          ## insertion mode to "in select" by themselves.
3014                        td => IN_CELL_IM,                        td => IN_CELL_IM,
3015                        th => IN_CELL_IM,                        th => IN_CELL_IM,
3016                        tr => IN_ROW_IM,                        tr => IN_ROW_IM,
# Line 2110  sub _reset_insertion_mode ($) { Line 3023  sub _reset_insertion_mode ($) {
3023                        head => IN_BODY_IM, # not in head!                        head => IN_BODY_IM, # not in head!
3024                        body => IN_BODY_IM,                        body => IN_BODY_IM,
3025                        frameset => IN_FRAMESET_IM,                        frameset => IN_FRAMESET_IM,
3026                       }->{$node->[1]};                       }->{$node->[0]->manakai_local_name};
3027        $self->{insertion_mode} = $new_mode and return if defined $new_mode;      }
3028        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
3029                
3030        ## Step 14        ## Step 15
3031        if ($node->[1] eq 'html') {        if ($node->[1] & HTML_EL) {
3032          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
3033              !!!cp ('t29');
3034            $self->{insertion_mode} = BEFORE_HEAD_IM;            $self->{insertion_mode} = BEFORE_HEAD_IM;
3035          } else {          } else {
3036              ## ISSUE: Can this state be reached?
3037              !!!cp ('t30');
3038            $self->{insertion_mode} = AFTER_HEAD_IM;            $self->{insertion_mode} = AFTER_HEAD_IM;
3039          }          }
3040          return;          return;
3041          } else {
3042            !!!cp ('t31');
3043        }        }
3044                
3045        ## Step 15        ## Step 16
3046        $self->{insertion_mode} = IN_BODY_IM and return if $last;        $self->{insertion_mode} = IN_BODY_IM and return if $last;
3047                
3048        ## Step 16        ## Step 17
3049        $i--;        $i--;
3050        $node = $self->{open_elements}->[$i];        $node = $self->{open_elements}->[$i];
3051                
3052        ## Step 17        ## Step 18
3053        redo S3;        redo S3;
3054      } # S3      } # S3
3055    
3056      die "$0: _reset_insertion_mode: This line should never be reached";
3057  } # _reset_insertion_mode  } # _reset_insertion_mode
3058    
3059  sub _tree_construction_main ($) {  sub _tree_construction_main ($) {
# Line 2154  sub _tree_construction_main ($) { Line 3075  sub _tree_construction_main ($) {
3075      return if $entry->[0] eq '#marker';      return if $entry->[0] eq '#marker';
3076      for (@{$self->{open_elements}}) {      for (@{$self->{open_elements}}) {
3077        if ($entry->[0] eq $_->[0]) {        if ($entry->[0] eq $_->[0]) {
3078            !!!cp ('t32');
3079          return;          return;
3080        }        }
3081      }      }
# Line 2168  sub _tree_construction_main ($) { Line 3090  sub _tree_construction_main ($) {
3090    
3091        ## Step 6        ## Step 6
3092        if ($entry->[0] eq '#marker') {        if ($entry->[0] eq '#marker') {
3093            !!!cp ('t33_1');
3094          #          #
3095        } else {        } else {
3096          my $in_open_elements;          my $in_open_elements;
3097          OE: for (@{$self->{open_elements}}) {          OE: for (@{$self->{open_elements}}) {
3098            if ($entry->[0] eq $_->[0]) {            if ($entry->[0] eq $_->[0]) {
3099                !!!cp ('t33');
3100              $in_open_elements = 1;              $in_open_elements = 1;
3101              last OE;              last OE;
3102            }            }
3103          }          }
3104          if ($in_open_elements) {          if ($in_open_elements) {
3105              !!!cp ('t34');
3106            #            #
3107          } else {          } else {
3108              ## NOTE: <!DOCTYPE HTML><p><b><i><u></p> <p>X
3109              !!!cp ('t35');
3110            redo S4;            redo S4;
3111          }          }
3112        }        }
# Line 2202  sub _tree_construction_main ($) { Line 3129  sub _tree_construction_main ($) {
3129    
3130        ## Step 11        ## Step 11
3131        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {
3132            !!!cp ('t36');
3133          ## Step 7'          ## Step 7'
3134          $i++;          $i++;
3135          $entry = $active_formatting_elements->[$i];          $entry = $active_formatting_elements->[$i];
3136                    
3137          redo S7;          redo S7;
3138        }        }
3139    
3140          !!!cp ('t37');
3141      } # S7      } # S7
3142    }; # $reconstruct_active_formatting_elements    }; # $reconstruct_active_formatting_elements
3143    
3144    my $clear_up_to_marker = sub {    my $clear_up_to_marker = sub {
3145      for (reverse 0..$#$active_formatting_elements) {      for (reverse 0..$#$active_formatting_elements) {
3146        if ($active_formatting_elements->[$_]->[0] eq '#marker') {        if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3147            !!!cp ('t38');
3148          splice @$active_formatting_elements, $_;          splice @$active_formatting_elements, $_;
3149          return;          return;
3150        }        }
3151      }      }
3152    
3153        !!!cp ('t39');
3154    }; # $clear_up_to_marker    }; # $clear_up_to_marker
3155    
3156    my $parse_rcdata = sub ($$) {    my $insert;
3157      my ($content_model_flag, $insert) = @_;  
3158      my $parse_rcdata = sub ($) {
3159        my ($content_model_flag) = @_;
3160    
3161      ## Step 1      ## Step 1
3162      my $start_tag_name = $token->{tag_name};      my $start_tag_name = $token->{tag_name};
3163      my $el;      my $el;
3164      !!!create-element ($el, $start_tag_name, $token->{attributes});      !!!create-element ($el, $HTML_NS, $start_tag_name, $token->{attributes}, $token);
3165    
3166      ## Step 2      ## Step 2
3167      $insert->($el); # /context node/->append_child ($el)      $insert->($el);
3168    
3169      ## Step 3      ## Step 3
3170      $self->{content_model} = $content_model_flag; # CDATA or RCDATA      $self->{content_model} = $content_model_flag; # CDATA or RCDATA
# Line 2237  sub _tree_construction_main ($) { Line 3172  sub _tree_construction_main ($) {
3172    
3173      ## Step 4      ## Step 4
3174      my $text = '';      my $text = '';
3175        !!!nack ('t40.1');
3176      !!!next-token;      !!!next-token;
3177      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
3178          !!!cp ('t40');
3179        $text .= $token->{data};        $text .= $token->{data};
3180        !!!next-token;        !!!next-token;
3181      }      }
3182    
3183      ## Step 5      ## Step 5
3184      if (length $text) {      if (length $text) {
3185          !!!cp ('t41');
3186        my $text = $self->{document}->create_text_node ($text);        my $text = $self->{document}->create_text_node ($text);
3187        $el->append_child ($text);        $el->append_child ($text);
3188      }      }
# Line 2253  sub _tree_construction_main ($) { Line 3191  sub _tree_construction_main ($) {
3191      $self->{content_model} = PCDATA_CONTENT_MODEL;      $self->{content_model} = PCDATA_CONTENT_MODEL;
3192    
3193      ## Step 7      ## Step 7
3194      if ($token->{type} == END_TAG_TOKEN and $token->{tag_name} eq $start_tag_name) {      if ($token->{type} == END_TAG_TOKEN and
3195            $token->{tag_name} eq $start_tag_name) {
3196          !!!cp ('t42');
3197        ## Ignore the token        ## Ignore the token
     } elsif ($content_model_flag == CDATA_CONTENT_MODEL) {  
       !!!parse-error (type => 'in CDATA:#'.$token->{type});  
     } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {  
       !!!parse-error (type => 'in RCDATA:#'.$token->{type});  
3198      } else {      } else {
3199        die "$0: $content_model_flag in parse_rcdata";        ## NOTE: An end-of-file token.
3200          if ($content_model_flag == CDATA_CONTENT_MODEL) {
3201            !!!cp ('t43');
3202            !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);
3203          } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
3204            !!!cp ('t44');
3205            !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);
3206          } else {
3207            die "$0: $content_model_flag in parse_rcdata";
3208          }
3209      }      }
3210      !!!next-token;      !!!next-token;
3211    }; # $parse_rcdata    }; # $parse_rcdata
3212    
3213    my $script_start_tag = sub ($) {    my $script_start_tag = sub () {
     my $insert = $_[0];  
3214      my $script_el;      my $script_el;
3215      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, $HTML_NS, 'script', $token->{attributes}, $token);
3216      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
3217    
3218      $self->{content_model} = CDATA_CONTENT_MODEL;      $self->{content_model} = CDATA_CONTENT_MODEL;
3219      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
3220            
3221      my $text = '';      my $text = '';
3222        !!!nack ('t45.1');
3223      !!!next-token;      !!!next-token;
3224      while ($token->{type} == CHARACTER_TOKEN) {      while ($token->{type} == CHARACTER_TOKEN) {
3225          !!!cp ('t45');
3226        $text .= $token->{data};        $text .= $token->{data};
3227        !!!next-token;        !!!next-token;
3228      } # stop if non-character token or tokenizer stops tokenising      } # stop if non-character token or tokenizer stops tokenising
3229      if (length $text) {      if (length $text) {
3230          !!!cp ('t46');
3231        $script_el->manakai_append_text ($text);        $script_el->manakai_append_text ($text);
3232      }      }
3233                                
# Line 2288  sub _tree_construction_main ($) { Line 3235  sub _tree_construction_main ($) {
3235    
3236      if ($token->{type} == END_TAG_TOKEN and      if ($token->{type} == END_TAG_TOKEN and
3237          $token->{tag_name} eq 'script') {          $token->{tag_name} eq 'script') {
3238          !!!cp ('t47');
3239        ## Ignore the token        ## Ignore the token
3240      } else {      } else {
3241        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!cp ('t48');
3242          !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);
3243        ## ISSUE: And ignore?        ## ISSUE: And ignore?
3244        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
3245      }      }
3246            
3247      if (defined $self->{inner_html_node}) {      if (defined $self->{inner_html_node}) {
3248          !!!cp ('t49');
3249        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
3250      } else {      } else {
3251          !!!cp ('t50');
3252        ## TODO: $old_insertion_point = current insertion point        ## TODO: $old_insertion_point = current insertion point
3253        ## TODO: insertion point = just before the next input character        ## TODO: insertion point = just before the next input character
3254    
# Line 2311  sub _tree_construction_main ($) { Line 3262  sub _tree_construction_main ($) {
3262      !!!next-token;      !!!next-token;
3263    }; # $script_start_tag    }; # $script_start_tag
3264    
3265      ## NOTE: $open_tables->[-1]->[0] is the "current table" element node.
3266      ## NOTE: $open_tables->[-1]->[1] is the "tainted" flag.
3267      my $open_tables = [[$self->{open_elements}->[0]->[0]]];
3268    
3269    my $formatting_end_tag = sub {    my $formatting_end_tag = sub {
3270      my $tag_name = shift;      my $end_tag_token = shift;
3271        my $tag_name = $end_tag_token->{tag_name};
3272    
3273        ## NOTE: The adoption agency algorithm (AAA).
3274    
3275      FET: {      FET: {
3276        ## Step 1        ## Step 1
3277        my $formatting_element;        my $formatting_element;
3278        my $formatting_element_i_in_active;        my $formatting_element_i_in_active;
3279        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3280          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {          if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3281              !!!cp ('t52');
3282              last AFE;
3283            } elsif ($active_formatting_elements->[$_]->[0]->manakai_local_name
3284                         eq $tag_name) {
3285              !!!cp ('t51');
3286            $formatting_element = $active_formatting_elements->[$_];            $formatting_element = $active_formatting_elements->[$_];
3287            $formatting_element_i_in_active = $_;            $formatting_element_i_in_active = $_;
3288            last AFE;            last AFE;
         } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {  
           last AFE;  
3289          }          }
3290        } # AFE        } # AFE
3291        unless (defined $formatting_element) {        unless (defined $formatting_element) {
3292          !!!parse-error (type => 'unmatched end tag:'.$tag_name);          !!!cp ('t53');
3293            !!!parse-error (type => 'unmatched end tag:'.$tag_name, token => $end_tag_token);
3294          ## Ignore the token          ## Ignore the token
3295          !!!next-token;          !!!next-token;
3296          return;          return;
# Line 2340  sub _tree_construction_main ($) { Line 3302  sub _tree_construction_main ($) {
3302          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
3303          if ($node->[0] eq $formatting_element->[0]) {          if ($node->[0] eq $formatting_element->[0]) {
3304            if ($in_scope) {            if ($in_scope) {
3305                !!!cp ('t54');
3306              $formatting_element_i_in_open = $_;              $formatting_element_i_in_open = $_;
3307              last INSCOPE;              last INSCOPE;
3308            } else { # in open elements but not in scope            } else { # in open elements but not in scope
3309              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t55');
3310                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name},
3311                                token => $end_tag_token);
3312              ## Ignore the token              ## Ignore the token
3313              !!!next-token;              !!!next-token;
3314              return;              return;
3315            }            }
3316          } elsif ({          } elsif ($node->[1] & SCOPING_EL) {
3317                    table => 1, caption => 1, td => 1, th => 1,            !!!cp ('t56');
                   button => 1, marquee => 1, object => 1, html => 1,  
                  }->{$node->[1]}) {  
3318            $in_scope = 0;            $in_scope = 0;
3319          }          }
3320        } # INSCOPE        } # INSCOPE
3321        unless (defined $formatting_element_i_in_open) {        unless (defined $formatting_element_i_in_open) {
3322          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!cp ('t57');
3323            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name},
3324                            token => $end_tag_token);
3325          pop @$active_formatting_elements; # $formatting_element          pop @$active_formatting_elements; # $formatting_element
3326          !!!next-token; ## TODO: ok?          !!!next-token; ## TODO: ok?
3327          return;          return;
3328        }        }
3329        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
3330          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);          !!!cp ('t58');
3331            !!!parse-error (type => 'not closed',
3332                            value => $self->{open_elements}->[-1]->[0]
3333                                ->manakai_local_name,
3334                            token => $end_tag_token);
3335        }        }
3336                
3337        ## Step 2        ## Step 2
# Line 2370  sub _tree_construction_main ($) { Line 3339  sub _tree_construction_main ($) {
3339        my $furthest_block_i_in_open;        my $furthest_block_i_in_open;
3340        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3341          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
3342          if (not $formatting_category->{$node->[1]} and          if (not ($node->[1] & FORMATTING_EL) and
3343              #not $phrasing_category->{$node->[1]} and              #not $phrasing_category->{$node->[1]} and
3344              ($special_category->{$node->[1]} or              ($node->[1] & SPECIAL_EL or
3345               $scoping_category->{$node->[1]})) {               $node->[1] & SCOPING_EL)) { ## Scoping is redundant, maybe
3346              !!!cp ('t59');
3347            $furthest_block = $node;            $furthest_block = $node;
3348            $furthest_block_i_in_open = $_;            $furthest_block_i_in_open = $_;
3349          } elsif ($node->[0] eq $formatting_element->[0]) {          } elsif ($node->[0] eq $formatting_element->[0]) {
3350              !!!cp ('t60');
3351            last OE;            last OE;
3352          }          }
3353        } # OE        } # OE
3354                
3355        ## Step 3        ## Step 3
3356        unless (defined $furthest_block) { # MUST        unless (defined $furthest_block) { # MUST
3357            !!!cp ('t61');
3358          splice @{$self->{open_elements}}, $formatting_element_i_in_open;          splice @{$self->{open_elements}}, $formatting_element_i_in_open;
3359          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;
3360          !!!next-token;          !!!next-token;
# Line 2395  sub _tree_construction_main ($) { Line 3367  sub _tree_construction_main ($) {
3367        ## Step 5        ## Step 5
3368        my $furthest_block_parent = $furthest_block->[0]->parent_node;        my $furthest_block_parent = $furthest_block->[0]->parent_node;
3369        if (defined $furthest_block_parent) {        if (defined $furthest_block_parent) {
3370            !!!cp ('t62');
3371          $furthest_block_parent->remove_child ($furthest_block->[0]);          $furthest_block_parent->remove_child ($furthest_block->[0]);
3372        }        }
3373                
# Line 2417  sub _tree_construction_main ($) { Line 3390  sub _tree_construction_main ($) {
3390          S7S2: {          S7S2: {
3391            for (reverse 0..$#$active_formatting_elements) {            for (reverse 0..$#$active_formatting_elements) {
3392              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
3393                  !!!cp ('t63');
3394                $node_i_in_active = $_;                $node_i_in_active = $_;
3395                last S7S2;                last S7S2;
3396              }              }
# Line 2430  sub _tree_construction_main ($) { Line 3404  sub _tree_construction_main ($) {
3404                    
3405          ## Step 4          ## Step 4
3406          if ($last_node->[0] eq $furthest_block->[0]) {          if ($last_node->[0] eq $furthest_block->[0]) {
3407              !!!cp ('t64');
3408            $bookmark_prev_el = $node->[0];            $bookmark_prev_el = $node->[0];
3409          }          }
3410                    
3411          ## Step 5          ## Step 5
3412          if ($node->[0]->has_child_nodes ()) {          if ($node->[0]->has_child_nodes ()) {
3413              !!!cp ('t65');
3414            my $clone = [$node->[0]->clone_node (0), $node->[1]];            my $clone = [$node->[0]->clone_node (0), $node->[1]];
3415            $active_formatting_elements->[$node_i_in_active] = $clone;            $active_formatting_elements->[$node_i_in_active] = $clone;
3416            $self->{open_elements}->[$node_i_in_open] = $clone;            $self->{open_elements}->[$node_i_in_open] = $clone;
# Line 2452  sub _tree_construction_main ($) { Line 3428  sub _tree_construction_main ($) {
3428        } # S7          } # S7  
3429                
3430        ## Step 8        ## Step 8
3431        $common_ancestor_node->[0]->append_child ($last_node->[0]);        if ($common_ancestor_node->[1] & TABLE_ROWS_EL) {
3432            my $foster_parent_element;
3433            my $next_sibling;
3434            OE: for (reverse 0..$#{$self->{open_elements}}) {
3435              if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
3436                                 my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3437                                 if (defined $parent and $parent->node_type == 1) {
3438                                   !!!cp ('t65.1');
3439                                   $foster_parent_element = $parent;
3440                                   $next_sibling = $self->{open_elements}->[$_]->[0];
3441                                 } else {
3442                                   !!!cp ('t65.2');
3443                                   $foster_parent_element
3444                                     = $self->{open_elements}->[$_ - 1]->[0];
3445                                 }
3446                                 last OE;
3447                               }
3448                             } # OE
3449                             $foster_parent_element = $self->{open_elements}->[0]->[0]
3450                               unless defined $foster_parent_element;
3451            $foster_parent_element->insert_before ($last_node->[0], $next_sibling);
3452            $open_tables->[-1]->[1] = 1; # tainted
3453          } else {
3454            !!!cp ('t65.3');
3455            $common_ancestor_node->[0]->append_child ($last_node->[0]);
3456          }
3457                
3458        ## Step 9        ## Step 9
3459        my $clone = [$formatting_element->[0]->clone_node (0),        my $clone = [$formatting_element->[0]->clone_node (0),
# Line 2469  sub _tree_construction_main ($) { Line 3470  sub _tree_construction_main ($) {
3470        my $i;        my $i;
3471        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3472          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {
3473              !!!cp ('t66');
3474            splice @$active_formatting_elements, $_, 1;            splice @$active_formatting_elements, $_, 1;
3475            $i-- and last AFE if defined $i;            $i-- and last AFE if defined $i;
3476          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {
3477              !!!cp ('t67');
3478            $i = $_;            $i = $_;
3479          }          }
3480        } # AFE        } # AFE
# Line 2481  sub _tree_construction_main ($) { Line 3484  sub _tree_construction_main ($) {
3484        undef $i;        undef $i;
3485        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3486          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {
3487              !!!cp ('t68');
3488            splice @{$self->{open_elements}}, $_, 1;            splice @{$self->{open_elements}}, $_, 1;
3489            $i-- and last OE if defined $i;            $i-- and last OE if defined $i;
3490          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {
3491              !!!cp ('t69');
3492            $i = $_;            $i = $_;
3493          }          }
3494        } # OE        } # OE
# Line 2494  sub _tree_construction_main ($) { Line 3499  sub _tree_construction_main ($) {
3499      } # FET      } # FET
3500    }; # $formatting_end_tag    }; # $formatting_end_tag
3501    
3502    my $insert_to_current = sub {    $insert = my $insert_to_current = sub {
3503      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);
3504    }; # $insert_to_current    }; # $insert_to_current
3505    
3506    my $insert_to_foster = sub {    my $insert_to_foster = sub {
3507                         my $child = shift;      my $child = shift;
3508                         if ({      if ($self->{open_elements}->[-1]->[1] & TABLE_ROWS_EL) {
3509                              table => 1, tbody => 1, tfoot => 1,        # MUST
3510                              thead => 1, tr => 1,        my $foster_parent_element;
3511                             }->{$self->{open_elements}->[-1]->[1]}) {        my $next_sibling;
3512                           # MUST        OE: for (reverse 0..$#{$self->{open_elements}}) {
3513                           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') {  
3514                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3515                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
3516                                   !!!cp ('t70');
3517                                 $foster_parent_element = $parent;                                 $foster_parent_element = $parent;
3518                                 $next_sibling = $self->{open_elements}->[$_]->[0];                                 $next_sibling = $self->{open_elements}->[$_]->[0];
3519                               } else {                               } else {
3520                                   !!!cp ('t71');
3521                                 $foster_parent_element                                 $foster_parent_element
3522                                   = $self->{open_elements}->[$_ - 1]->[0];                                   = $self->{open_elements}->[$_ - 1]->[0];
3523                               }                               }
# Line 2524  sub _tree_construction_main ($) { Line 3528  sub _tree_construction_main ($) {
3528                             unless defined $foster_parent_element;                             unless defined $foster_parent_element;
3529                           $foster_parent_element->insert_before                           $foster_parent_element->insert_before
3530                             ($child, $next_sibling);                             ($child, $next_sibling);
3531                         } else {        $open_tables->[-1]->[1] = 1; # tainted
3532                           $self->{open_elements}->[-1]->[0]->append_child ($child);      } else {
3533                         }        !!!cp ('t72');
3534          $self->{open_elements}->[-1]->[0]->append_child ($child);
3535        }
3536    }; # $insert_to_foster    }; # $insert_to_foster
3537    
3538    my $insert;    B: while (1) {
   
   B: {  
3539      if ($token->{type} == DOCTYPE_TOKEN) {      if ($token->{type} == DOCTYPE_TOKEN) {
3540        !!!parse-error (type => 'DOCTYPE in the middle');        !!!cp ('t73');
3541          !!!parse-error (type => 'DOCTYPE in the middle', token => $token);
3542        ## Ignore the token        ## Ignore the token
3543        ## Stay in the phase        ## Stay in the phase
3544        !!!next-token;        !!!next-token;
3545        redo B;        next B;
     } elsif ($token->{type} == END_OF_FILE_TOKEN) {  
       if ($self->{insertion_mode} & AFTER_HTML_IMS) {  
         #  
       } else {  
         ## Generate implied end tags  
         if ({  
              dd => 1, dt => 1, li => 1, p => 1, td => 1, th => 1, tr => 1,  
              tbody => 1, tfoot=> 1, thead => 1,  
             }->{$self->{open_elements}->[-1]->[1]}) {  
           !!!back-token;  
           $token = {type => END_TAG_TOKEN, tag_name => $self->{open_elements}->[-1]->[1]};  
           redo B;  
         }  
           
         if (@{$self->{open_elements}} > 2 or  
             (@{$self->{open_elements}} == 2 and $self->{open_elements}->[1]->[1] ne 'body')) {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         } elsif (defined $self->{inner_html_node} and  
                  @{$self->{open_elements}} > 1 and  
                  $self->{open_elements}->[1]->[1] ne 'body') {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         }  
   
         ## ISSUE: There is an issue in the spec.  
       }  
   
       ## Stop parsing  
       last B;  
3546      } elsif ($token->{type} == START_TAG_TOKEN and      } elsif ($token->{type} == START_TAG_TOKEN and
3547               $token->{tag_name} eq 'html') {               $token->{tag_name} eq 'html') {
3548        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
3549          ## Turn into the main phase          !!!cp ('t79');
3550          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html:html', token => $token);
3551          $self->{insertion_mode} = AFTER_BODY_IM;          $self->{insertion_mode} = AFTER_BODY_IM;
3552        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
3553          ## Turn into the main phase          !!!cp ('t80');
3554          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html:html', token => $token);
3555          $self->{insertion_mode} = AFTER_FRAMESET_IM;          $self->{insertion_mode} = AFTER_FRAMESET_IM;
3556          } else {
3557            !!!cp ('t81');
3558        }        }
3559    
3560  ## ISSUE: "aa<html>" is not a parse error.        !!!cp ('t82');
3561  ## ISSUE: "<html>" in fragment is not a parse error.        !!!parse-error (type => 'not first start tag', token => $token);
       unless ($token->{first_start_tag}) {  
         !!!parse-error (type => 'not first start tag');  
       }  
3562        my $top_el = $self->{open_elements}->[0]->[0];        my $top_el = $self->{open_elements}->[0]->[0];
3563        for my $attr_name (keys %{$token->{attributes}}) {        for my $attr_name (keys %{$token->{attributes}}) {
3564          unless ($top_el->has_attribute_ns (undef, $attr_name)) {          unless ($top_el->has_attribute_ns (undef, $attr_name)) {
3565              !!!cp ('t84');
3566            $top_el->set_attribute_ns            $top_el->set_attribute_ns
3567              (undef, [undef, $attr_name],              (undef, [undef, $attr_name],
3568               $token->{attributes}->{$attr_name}->{value});               $token->{attributes}->{$attr_name}->{value});
3569          }          }
3570        }        }
3571          !!!nack ('t84.1');
3572        !!!next-token;        !!!next-token;
3573        redo B;        next B;
3574      } elsif ($token->{type} == COMMENT_TOKEN) {      } elsif ($token->{type} == COMMENT_TOKEN) {
3575        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
3576        if ($self->{insertion_mode} & AFTER_HTML_IMS) {        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
3577            !!!cp ('t85');
3578          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
3579        } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {        } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {
3580            !!!cp ('t86');
3581          $self->{open_elements}->[0]->[0]->append_child ($comment);          $self->{open_elements}->[0]->[0]->append_child ($comment);
3582        } else {        } else {
3583            !!!cp ('t87');
3584          $self->{open_elements}->[-1]->[0]->append_child ($comment);          $self->{open_elements}->[-1]->[0]->append_child ($comment);
3585        }        }
3586        !!!next-token;        !!!next-token;
3587        redo B;        next B;
3588      } elsif ($self->{insertion_mode} & HEAD_IMS) {      } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
3589          if ($token->{type} == CHARACTER_TOKEN) {
3590            !!!cp ('t87.1');
3591            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
3592            !!!next-token;
3593            next B;
3594          } elsif ($token->{type} == START_TAG_TOKEN) {
3595            if ($self->{open_elements}->[-1]->[1] & FOREIGN_FLOW_CONTENT_EL or
3596                not ($self->{open_elements}->[-1]->[1] & FOREIGN_EL) or
3597                ($token->{tag_name} eq 'svg' and
3598                 $self->{open_elements}->[-1]->[1] & MML_AXML_EL)) {
3599              ## NOTE: "using the rules for secondary insertion mode"then"continue"
3600              !!!cp ('t87.2');
3601              #
3602            } elsif ({
3603      ## TODO:
3604                     }->{$token->{tag_name}}) {
3605              !!!cp ('t87.2');
3606              !!!parse-error (type => 'not closed',
3607                              value => $self->{open_elements}->[-1]->[0]
3608                                  ->manakai_local_name,
3609                              token => $token);
3610    
3611              pop @{$self->{open_elements}}
3612                  while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
3613    
3614              $self->{insertion_mode} &= ~ $self->{insertion_mode};
3615              ## Reprocess.
3616              next B;
3617            } else {
3618              ## TODO: case fixup
3619    
3620              !!!insert-element-f ($self->{open_elements}->[-1]->[0]->namespace_uri, $token);
3621    
3622              if ($self->{self_closing}) {
3623                pop @{$self->{open_elements}};
3624                !!!ack ('t87.3');
3625              } else {
3626                !!!cp ('t87.4');
3627              }
3628    
3629              !!!next-token;
3630              next B;
3631            }
3632          } elsif ($token->{type} == END_TAG_TOKEN) {
3633            ## NOTE: "using the rules for secondary insertion mode" then "continue"
3634            !!!cp ('t87.5');
3635            #
3636          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
3637            ## NOTE: "using the rules for secondary insertion mode" then "continue"
3638            !!!cp ('t87.6');
3639            #
3640            ## TODO: ...
3641          } else {
3642            die "$0: $token->{type}: Unknown token type";        
3643          }
3644        }
3645    
3646        if ($self->{insertion_mode} & HEAD_IMS) {
3647        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
3648          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
3649            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3650                !!!cp ('t88.2');
3651                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
3652              } else {
3653                !!!cp ('t88.1');
3654                ## Ignore the token.
3655                !!!next-token;
3656                next B;
3657              }
3658            unless (length $token->{data}) {            unless (length $token->{data}) {
3659                !!!cp ('t88');
3660              !!!next-token;              !!!next-token;
3661              redo B;              next B;
3662            }            }
3663          }          }
3664    
3665          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3666              !!!cp ('t89');
3667            ## As if <head>            ## As if <head>
3668            !!!create-element ($self->{head_element}, 'head');            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
3669            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3670            push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            push @{$self->{open_elements}},
3671                  [$self->{head_element}, $el_category->{head}];
3672    
3673            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
3674            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
3675    
3676            ## Reprocess in the "after head" insertion mode...            ## Reprocess in the "after head" insertion mode...
3677          } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {          } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3678              !!!cp ('t90');
3679            ## As if </noscript>            ## As if </noscript>
3680            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
3681            !!!parse-error (type => 'in noscript:#character');            !!!parse-error (type => 'in noscript:#character', token => $token);
3682                        
3683            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
3684            ## As if </head>            ## As if </head>
# Line 2635  sub _tree_construction_main ($) { Line 3686  sub _tree_construction_main ($) {
3686    
3687            ## Reprocess in the "after head" insertion mode...            ## Reprocess in the "after head" insertion mode...
3688          } elsif ($self->{insertion_mode} == IN_HEAD_IM) {          } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3689              !!!cp ('t91');
3690            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
3691    
3692            ## Reprocess in the "after head" insertion mode...            ## Reprocess in the "after head" insertion mode...
3693            } else {
3694              !!!cp ('t92');
3695          }          }
3696    
3697              ## "after head" insertion mode          ## "after head" insertion mode
3698              ## As if <body>          ## As if <body>
3699              !!!insert-element ('body');          !!!insert-element ('body',, $token);
3700              $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
3701              ## reprocess          ## reprocess
3702              redo B;          next B;
3703            } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
3704              if ($token->{tag_name} eq 'head') {          if ($token->{tag_name} eq 'head') {
3705                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3706                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});              !!!cp ('t93');
3707                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              !!!create-element ($self->{head_element}, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
3708                  push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];              $self->{open_elements}->[-1]->[0]->append_child
3709                  $self->{insertion_mode} = IN_HEAD_IM;                  ($self->{head_element});
3710                  !!!next-token;              push @{$self->{open_elements}},
3711                  redo B;                  [$self->{head_element}, $el_category->{head}];
3712                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {              $self->{insertion_mode} = IN_HEAD_IM;
3713                  #              !!!nack ('t93.1');
3714                } else {              !!!next-token;
3715                  !!!parse-error (type => 'in head:head'); # or in head noscript              next B;
3716                  ## Ignore the token            } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
3717                  !!!next-token;              !!!cp ('t94');
3718                  redo B;              #
3719                }            } else {
3720              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              !!!cp ('t95');
3721                ## As if <head>              !!!parse-error (type => 'in head:head', token => $token); # or in head noscript
3722                !!!create-element ($self->{head_element}, 'head');              ## Ignore the token
3723                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              !!!nack ('t95.1');
3724                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];              !!!next-token;
3725                next B;
3726              }
3727            } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3728              !!!cp ('t96');
3729              ## As if <head>
3730              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
3731              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3732              push @{$self->{open_elements}},
3733                  [$self->{head_element}, $el_category->{head}];
3734    
3735                $self->{insertion_mode} = IN_HEAD_IM;            $self->{insertion_mode} = IN_HEAD_IM;
3736                ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
3737              }          } else {
3738              !!!cp ('t97');
3739            }
3740    
3741              if ($token->{tag_name} eq 'base') {              if ($token->{tag_name} eq 'base') {
3742                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3743                    !!!cp ('t98');
3744                  ## As if </noscript>                  ## As if </noscript>
3745                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3746                  !!!parse-error (type => 'in noscript:base');                  !!!parse-error (type => 'in noscript:base', token => $token);
3747                                
3748                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
3749                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3750                  } else {
3751                    !!!cp ('t99');
3752                }                }
3753    
3754                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3755                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3756                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t100');
3757                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
3758                    push @{$self->{open_elements}},
3759                        [$self->{head_element}, $el_category->{head}];
3760                  } else {
3761                    !!!cp ('t101');
3762                }                }
3763                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
3764                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
3765                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
3766                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
3767                  !!!nack ('t101.1');
3768                !!!next-token;                !!!next-token;
3769                redo B;                next B;
3770              } elsif ($token->{tag_name} eq 'link') {              } elsif ($token->{tag_name} eq 'link') {
3771                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3772                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3773                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t102');
3774                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
3775                    push @{$self->{open_elements}},
3776                        [$self->{head_element}, $el_category->{head}];
3777                  } else {
3778                    !!!cp ('t103');
3779                }                }
3780                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
3781                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
3782                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
3783                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
3784                  !!!ack ('t103.1');
3785                !!!next-token;                !!!next-token;
3786                redo B;                next B;
3787              } elsif ($token->{tag_name} eq 'meta') {              } elsif ($token->{tag_name} eq 'meta') {
3788                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3789                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3790                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t104');
3791                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
3792                    push @{$self->{open_elements}},
3793                        [$self->{head_element}, $el_category->{head}];
3794                  } else {
3795                    !!!cp ('t105');
3796                }                }
3797                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
3798                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.
3799    
3800                unless ($self->{confident}) {                unless ($self->{confident}) {
                 my $charset;  
3801                  if ($token->{attributes}->{charset}) { ## TODO: And if supported                  if ($token->{attributes}->{charset}) { ## TODO: And if supported
3802                    $charset = $token->{attributes}->{charset}->{value};                    !!!cp ('t106');
3803                  }                    $self->{change_encoding}
3804                  if ($token->{attributes}->{'http-equiv'}) {                        ->($self, $token->{attributes}->{charset}->{value},
3805                             $token);
3806                      
3807                      $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
3808                          ->set_user_data (manakai_has_reference =>
3809                                               $token->{attributes}->{charset}
3810                                                   ->{has_reference});
3811                    } elsif ($token->{attributes}->{content}) {
3812                    ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.                    ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.
3813                    if ($token->{attributes}->{'http-equiv'}->{value}                    if ($token->{attributes}->{content}->{value}
3814                        =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=                        =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
3815                              [\x09-\x0D\x20]*=
3816                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
3817                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
3818                      $charset = defined $1 ? $1 : defined $2 ? $2 : $3;                      !!!cp ('t107');
3819                    } ## TODO: And if supported                      $self->{change_encoding}
3820                            ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,
3821                               $token);
3822                        $meta_el->[0]->get_attribute_node_ns (undef, 'content')
3823                            ->set_user_data (manakai_has_reference =>
3824                                                 $token->{attributes}->{content}
3825                                                       ->{has_reference});
3826                      } else {
3827                        !!!cp ('t108');
3828                      }
3829                    }
3830                  } else {
3831                    if ($token->{attributes}->{charset}) {
3832                      !!!cp ('t109');
3833                      $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
3834                          ->set_user_data (manakai_has_reference =>
3835                                               $token->{attributes}->{charset}
3836                                                   ->{has_reference});
3837                    }
3838                    if ($token->{attributes}->{content}) {
3839                      !!!cp ('t110');
3840                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')
3841                          ->set_user_data (manakai_has_reference =>
3842                                               $token->{attributes}->{content}
3843                                                   ->{has_reference});
3844                  }                  }
                 ## TODO: Change the encoding  
3845                }                }
3846    
3847                ## TODO: Extracting |charset| from |meta|.                pop @{$self->{open_elements}} # <head>
               pop @{$self->{open_elements}}  
3848                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
3849                  !!!ack ('t110.1');
3850                !!!next-token;                !!!next-token;
3851                redo B;                next B;
3852              } elsif ($token->{tag_name} eq 'title') {              } elsif ($token->{tag_name} eq 'title') {
3853                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3854                    !!!cp ('t111');
3855                  ## As if </noscript>                  ## As if </noscript>
3856                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3857                  !!!parse-error (type => 'in noscript:title');                  !!!parse-error (type => 'in noscript:title', token => $token);
3858                                
3859                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
3860                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3861                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
3862                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t112');
3863                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
3864                    push @{$self->{open_elements}},
3865                        [$self->{head_element}, $el_category->{head}];
3866                  } else {
3867                    !!!cp ('t113');
3868                }                }
3869    
3870                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3871                my $parent = defined $self->{head_element} ? $self->{head_element}                my $parent = defined $self->{head_element} ? $self->{head_element}
3872                    : $self->{open_elements}->[-1]->[0];                    : $self->{open_elements}->[-1]->[0];
3873                $parse_rcdata->(RCDATA_CONTENT_MODEL,                $parse_rcdata->(RCDATA_CONTENT_MODEL);
3874                                sub { $parent->append_child ($_[0]) });                pop @{$self->{open_elements}} # <head>
               pop @{$self->{open_elements}}  
3875                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
3876                redo B;                next B;
3877              } elsif ($token->{tag_name} eq 'style') {              } elsif ($token->{tag_name} eq 'style') {
3878                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
3879                ## insertion mode IN_HEAD_IM)                ## insertion mode IN_HEAD_IM)
3880                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3881                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3882                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t114');
3883                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
3884                    push @{$self->{open_elements}},
3885                        [$self->{head_element}, $el_category->{head}];
3886                  } else {
3887                    !!!cp ('t115');
3888                }                }
3889                $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);                $parse_rcdata->(CDATA_CONTENT_MODEL);
3890                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
3891                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
3892                redo B;                next B;
3893              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
3894                if ($self->{insertion_mode} == IN_HEAD_IM) {                if ($self->{insertion_mode} == IN_HEAD_IM) {
3895                    !!!cp ('t116');
3896                  ## NOTE: and scripting is disalbed                  ## NOTE: and scripting is disalbed
3897                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
3898                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
3899                    !!!nack ('t116.1');
3900                  !!!next-token;                  !!!next-token;
3901                  redo B;                  next B;
3902                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3903                  !!!parse-error (type => 'in noscript:noscript');                  !!!cp ('t117');
3904                    !!!parse-error (type => 'in noscript:noscript', token => $token);
3905                  ## Ignore the token                  ## Ignore the token
3906                    !!!nack ('t117.1');
3907                  !!!next-token;                  !!!next-token;
3908                  redo B;                  next B;
3909                } else {                } else {
3910                    !!!cp ('t118');
3911                  #                  #
3912                }                }
3913              } elsif ($token->{tag_name} eq 'script') {              } elsif ($token->{tag_name} eq 'script') {
3914                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3915                    !!!cp ('t119');
3916                  ## As if </noscript>                  ## As if </noscript>
3917                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3918                  !!!parse-error (type => 'in noscript:script');                  !!!parse-error (type => 'in noscript:script', token => $token);
3919                                
3920                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
3921                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3922                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
3923                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t120');
3924                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
3925                    push @{$self->{open_elements}},
3926                        [$self->{head_element}, $el_category->{head}];
3927                  } else {
3928                    !!!cp ('t121');
3929                }                }
3930    
3931                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3932                $script_start_tag->($insert_to_current);                $script_start_tag->();
3933                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
3934                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
3935                redo B;                next B;
3936              } elsif ($token->{tag_name} eq 'body' or              } elsif ($token->{tag_name} eq 'body' or
3937                       $token->{tag_name} eq 'frameset') {                       $token->{tag_name} eq 'frameset') {
3938                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3939                    !!!cp ('t122');
3940                  ## As if </noscript>                  ## As if </noscript>
3941                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3942                  !!!parse-error (type => 'in noscript:'.$token->{tag_name});                  !!!parse-error (type => 'in noscript:'.$token->{tag_name}, token => $token);
3943                                    
3944                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3945                  ## As if </head>                  ## As if </head>
# Line 2816  sub _tree_construction_main ($) { Line 3947  sub _tree_construction_main ($) {
3947                                    
3948                  ## Reprocess in the "after head" insertion mode...                  ## Reprocess in the "after head" insertion mode...
3949                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3950                    !!!cp ('t124');
3951                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3952                                    
3953                  ## Reprocess in the "after head" insertion mode...                  ## Reprocess in the "after head" insertion mode...
3954                  } else {
3955                    !!!cp ('t125');
3956                }                }
3957    
3958                ## "after head" insertion mode                ## "after head" insertion mode
3959                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
3960                if ($token->{tag_name} eq 'body') {                if ($token->{tag_name} eq 'body') {
3961                    !!!cp ('t126');
3962                  $self->{insertion_mode} = IN_BODY_IM;                  $self->{insertion_mode} = IN_BODY_IM;
3963                } elsif ($token->{tag_name} eq 'frameset') {                } elsif ($token->{tag_name} eq 'frameset') {
3964                    !!!cp ('t127');
3965                  $self->{insertion_mode} = IN_FRAMESET_IM;                  $self->{insertion_mode} = IN_FRAMESET_IM;
3966                } else {                } else {
3967                  die "$0: tag name: $self->{tag_name}";                  die "$0: tag name: $self->{tag_name}";
3968                }                }
3969                  !!!nack ('t127.1');
3970                !!!next-token;                !!!next-token;
3971                redo B;                next B;
3972              } else {              } else {
3973                  !!!cp ('t128');
3974                #                #
3975              }              }
3976    
3977              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3978                  !!!cp ('t129');
3979                ## As if </noscript>                ## As if </noscript>
3980                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3981                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);
3982                                
3983                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
3984                ## As if </head>                ## As if </head>
# Line 2847  sub _tree_construction_main ($) { Line 3986  sub _tree_construction_main ($) {
3986    
3987                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
3988              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3989                  !!!cp ('t130');
3990                ## As if </head>                ## As if </head>
3991                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3992    
3993                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
3994                } else {
3995                  !!!cp ('t131');
3996              }              }
3997    
3998              ## "after head" insertion mode              ## "after head" insertion mode
3999              ## As if <body>              ## As if <body>
4000              !!!insert-element ('body');              !!!insert-element ('body',, $token);
4001              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
4002              ## reprocess              ## reprocess
4003              redo B;              !!!ack-later;
4004                next B;
4005            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
4006              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
4007                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4008                    !!!cp ('t132');
4009                  ## As if <head>                  ## As if <head>
4010                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4011                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4012                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4013                        [$self->{head_element}, $el_category->{head}];
4014    
4015                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4016                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4017                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4018                  !!!next-token;                  !!!next-token;
4019                  redo B;                  next B;
4020                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4021                    !!!cp ('t133');
4022                  ## As if </noscript>                  ## As if </noscript>
4023                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4024                  !!!parse-error (type => 'in noscript:script');                  !!!parse-error (type => 'in noscript:/head', token => $token);
4025                                    
4026                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4027                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4028                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4029                  !!!next-token;                  !!!next-token;
4030                  redo B;                  next B;
4031                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4032                    !!!cp ('t134');
4033                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4034                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4035                  !!!next-token;                  !!!next-token;
4036                  redo B;                  next B;
4037                } else {                } else {
4038                    !!!cp ('t135');
4039                  #                  #
4040                }                }
4041              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4042                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4043                    !!!cp ('t136');
4044                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4045                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4046                  !!!next-token;                  !!!next-token;
4047                  redo B;                  next B;
4048                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4049                  !!!parse-error (type => 'unmatched end tag:noscript');                  !!!cp ('t137');
4050                    !!!parse-error (type => 'unmatched end tag:noscript', token => $token);
4051                  ## Ignore the token ## ISSUE: An issue in the spec.                  ## Ignore the token ## ISSUE: An issue in the spec.
4052                  !!!next-token;                  !!!next-token;
4053                  redo B;                  next B;
4054                } else {                } else {
4055                    !!!cp ('t138');
4056                  #                  #
4057                }                }
4058              } elsif ({              } elsif ({
4059                        body => 1, html => 1,                        body => 1, html => 1,
4060                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4061                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4062                    !!!cp ('t139');
4063                  ## As if <head>                  ## As if <head>
4064                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4065                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4066                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4067                        [$self->{head_element}, $el_category->{head}];
4068    
4069                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4070                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4071                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4072                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t140');
4073                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4074                  ## Ignore the token                  ## Ignore the token
4075                  !!!next-token;                  !!!next-token;
4076                  redo B;                  next B;
4077                  } else {
4078                    !!!cp ('t141');
4079                }                }
4080                                
4081                #                #
# Line 2927  sub _tree_construction_main ($) { Line 4083  sub _tree_construction_main ($) {
4083                        p => 1, br => 1,                        p => 1, br => 1,
4084                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4085                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4086                    !!!cp ('t142');
4087                  ## As if <head>                  ## As if <head>
4088                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4089                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4090                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4091                        [$self->{head_element}, $el_category->{head}];
4092    
4093                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4094                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4095                  } else {
4096                    !!!cp ('t143');
4097                }                }
4098    
4099                #                #
4100              } else {              } else {
4101                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4102                    !!!cp ('t144');
4103                  #                  #
4104                } else {                } else {
4105                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t145');
4106                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4107                  ## Ignore the token                  ## Ignore the token
4108                  !!!next-token;                  !!!next-token;
4109                  redo B;                  next B;
4110                }                }
4111              }              }
4112    
4113              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4114                  !!!cp ('t146');
4115                ## As if </noscript>                ## As if </noscript>
4116                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4117                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);
4118                                
4119                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
4120                ## As if </head>                ## As if </head>
# Line 2959  sub _tree_construction_main ($) { Line 4122  sub _tree_construction_main ($) {
4122    
4123                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
4124              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4125                  !!!cp ('t147');
4126                ## As if </head>                ## As if </head>
4127                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4128    
4129                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
4130              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4131                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  ## ISSUE: This case cannot be reached?
4132                  !!!cp ('t148');
4133                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4134                ## Ignore the token ## ISSUE: An issue in the spec.                ## Ignore the token ## ISSUE: An issue in the spec.
4135                !!!next-token;                !!!next-token;
4136                redo B;                next B;
4137                } else {
4138                  !!!cp ('t149');
4139              }              }
4140    
4141              ## "after head" insertion mode              ## "after head" insertion mode
4142              ## As if <body>              ## As if <body>
4143              !!!insert-element ('body');              !!!insert-element ('body',, $token);
4144              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
4145              ## reprocess              ## reprocess
4146              redo B;              next B;
4147            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4148              die "$0: $token->{type}: Unknown token type";          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4149            }            !!!cp ('t149.1');
4150    
4151              ## NOTE: As if <head>
4152              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4153              $self->{open_elements}->[-1]->[0]->append_child
4154                  ($self->{head_element});
4155              #push @{$self->{open_elements}},
4156              #    [$self->{head_element}, $el_category->{head}];
4157              #$self->{insertion_mode} = IN_HEAD_IM;
4158              ## NOTE: Reprocess.
4159    
4160              ## NOTE: As if </head>
4161              #pop @{$self->{open_elements}};
4162              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4163              ## NOTE: Reprocess.
4164              
4165              #
4166            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4167              !!!cp ('t149.2');
4168    
4169              ## NOTE: As if </head>
4170              pop @{$self->{open_elements}};
4171              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4172              ## NOTE: Reprocess.
4173    
4174              #
4175            } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4176              !!!cp ('t149.3');
4177    
4178              !!!parse-error (type => 'in noscript:#eof', token => $token);
4179    
4180              ## As if </noscript>
4181              pop @{$self->{open_elements}};
4182              #$self->{insertion_mode} = IN_HEAD_IM;
4183              ## NOTE: Reprocess.
4184    
4185              ## NOTE: As if </head>
4186              pop @{$self->{open_elements}};
4187              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4188              ## NOTE: Reprocess.
4189    
4190              #
4191            } else {
4192              !!!cp ('t149.4');
4193              #
4194            }
4195    
4196            ## NOTE: As if <body>
4197            !!!insert-element ('body',, $token);
4198            $self->{insertion_mode} = IN_BODY_IM;
4199            ## NOTE: Reprocess.
4200            next B;
4201          } else {
4202            die "$0: $token->{type}: Unknown token type";
4203          }
4204    
4205            ## ISSUE: An issue in the spec.            ## ISSUE: An issue in the spec.
4206      } elsif ($self->{insertion_mode} & BODY_IMS) {      } elsif ($self->{insertion_mode} & BODY_IMS) {
4207            if ($token->{type} == CHARACTER_TOKEN) {            if ($token->{type} == CHARACTER_TOKEN) {
4208                !!!cp ('t150');
4209              ## NOTE: There is a code clone of "character in body".              ## NOTE: There is a code clone of "character in body".
4210              $reconstruct_active_formatting_elements->($insert_to_current);              $reconstruct_active_formatting_elements->($insert_to_current);
4211                            
4212              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4213    
4214              !!!next-token;              !!!next-token;
4215              redo B;              next B;
4216            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
4217              if ({              if ({
4218                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
# Line 2997  sub _tree_construction_main ($) { Line 4220  sub _tree_construction_main ($) {
4220                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
4221                if ($self->{insertion_mode} == IN_CELL_IM) {                if ($self->{insertion_mode} == IN_CELL_IM) {
4222                  ## have an element in table scope                  ## have an element in table scope
4223                  my $tn;                  for (reverse 0..$#{$self->{open_elements}}) {
                 INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
4224                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4225                    if ($node->[1] eq 'td' or $node->[1] eq 'th') {                    if ($node->[1] & TABLE_CELL_EL) {
4226                      $tn = $node->[1];                      !!!cp ('t151');
4227                      last INSCOPE;  
4228                    } elsif ({                      ## Close the cell
4229                              table => 1, html => 1,                      !!!back-token; # <x>
4230                             }->{$node->[1]}) {                      $token = {type => END_TAG_TOKEN,
4231                      last INSCOPE;                                tag_name => $node->[0]->manakai_local_name,
4232                    }                                line => $token->{line},
4233                  } # INSCOPE                                column => $token->{column}};
4234                    unless (defined $tn) {                      next B;
4235                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
4236                      ## Ignore the token                      !!!cp ('t152');
4237                      !!!next-token;                      ## ISSUE: This case can never be reached, maybe.
4238                      redo B;                      last;
4239                    }                    }
4240                                    }
4241                  ## Close the cell  
4242                  !!!back-token; # <?>                  !!!cp ('t153');
4243                  $token = {type => END_TAG_TOKEN, tag_name => $tn};                  !!!parse-error (type => 'start tag not allowed',
4244                  redo B;                      value => $token->{tag_name}, token => $token);
4245                    ## Ignore the token
4246                    !!!nack ('t153.1');
4247                    !!!next-token;
4248                    next B;
4249                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4250                  !!!parse-error (type => 'not closed:caption');                  !!!parse-error (type => 'not closed:caption', token => $token);
4251                                    
4252                  ## As if </caption>                  ## NOTE: As if </caption>.
4253                  ## have a table element in table scope                  ## have a table element in table scope
4254                  my $i;                  my $i;
4255                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
4256                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
4257                    if ($node->[1] eq 'caption') {                      my $node = $self->{open_elements}->[$_];
4258                      $i = $_;                      if ($node->[1] & CAPTION_EL) {
4259                      last INSCOPE;                        !!!cp ('t155');
4260                    } elsif ({                        $i = $_;
4261                              table => 1, html => 1,                        last INSCOPE;
4262                             }->{$node->[1]}) {                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4263                      last INSCOPE;                        !!!cp ('t156');
4264                          last;
4265                        }
4266                    }                    }
4267    
4268                      !!!cp ('t157');
4269                      !!!parse-error (type => 'start tag not allowed',
4270                                      value => $token->{tag_name}, token => $token);
4271                      ## Ignore the token
4272                      !!!nack ('t157.1');
4273                      !!!next-token;
4274                      next B;
4275                  } # INSCOPE                  } # INSCOPE
                   unless (defined $i) {  
                     !!!parse-error (type => 'unmatched end tag:caption');  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
                   }  
4276                                    
4277                  ## generate implied end tags                  ## generate implied end tags
4278                  if ({                  while ($self->{open_elements}->[-1]->[1]
4279                       dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
4280                       td => 1, th => 1, tr => 1,                    !!!cp ('t158');
4281                       tbody => 1, tfoot=> 1, thead => 1,                    pop @{$self->{open_elements}};
                     }->{$self->{open_elements}->[-1]->[1]}) {  
                   !!!back-token; # <?>  
                   $token = {type => END_TAG_TOKEN, tag_name => 'caption'};  
                   !!!back-token;  
                   $token = {type => END_TAG_TOKEN,  
                             tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                   redo B;  
4282                  }                  }
4283    
4284                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4285                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t159');
4286                      !!!parse-error (type => 'not closed',
4287                                      value => $self->{open_elements}->[-1]->[0]
4288                                          ->manakai_local_name,
4289                                      token => $token);
4290                    } else {
4291                      !!!cp ('t160');
4292                  }                  }
4293                                    
4294                  splice @{$self->{open_elements}}, $i;                  splice @{$self->{open_elements}}, $i;
# Line 3069  sub _tree_construction_main ($) { Line 4298  sub _tree_construction_main ($) {
4298                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
4299                                    
4300                  ## reprocess                  ## reprocess
4301                  redo B;                  !!!ack-later;
4302                    next B;
4303                } else {                } else {
4304                    !!!cp ('t161');
4305                  #                  #
4306                }                }
4307              } else {              } else {
4308                  !!!cp ('t162');
4309                #                #
4310              }              }
4311            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
# Line 3083  sub _tree_construction_main ($) { Line 4315  sub _tree_construction_main ($) {
4315                  my $i;                  my $i;
4316                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4317                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4318                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
4319                        !!!cp ('t163');
4320                      $i = $_;                      $i = $_;
4321                      last INSCOPE;                      last INSCOPE;
4322                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
4323                              table => 1, html => 1,                      !!!cp ('t164');
                            }->{$node->[1]}) {  
4324                      last INSCOPE;                      last INSCOPE;
4325                    }                    }
4326                  } # INSCOPE                  } # INSCOPE
4327                    unless (defined $i) {                    unless (defined $i) {
4328                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!cp ('t165');
4329                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4330                      ## Ignore the token                      ## Ignore the token
4331                      !!!next-token;                      !!!next-token;
4332                      redo B;                      next B;
4333                    }                    }
4334                                    
4335                  ## generate implied end tags                  ## generate implied end tags
4336                  if ({                  while ($self->{open_elements}->[-1]->[1]
4337                       dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
4338                       td => ($token->{tag_name} eq 'th'),                    !!!cp ('t166');
4339                       th => ($token->{tag_name} eq 'td'),                    pop @{$self->{open_elements}};
                      tr => 1,  
                      tbody => 1, tfoot=> 1, thead => 1,  
                     }->{$self->{open_elements}->[-1]->[1]}) {  
                   !!!back-token;  
                   $token = {type => END_TAG_TOKEN,  
                             tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                   redo B;  
4340                  }                  }
4341                    
4342                  if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {                  if ($self->{open_elements}->[-1]->[0]->manakai_local_name
4343                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                          ne $token->{tag_name}) {
4344                      !!!cp ('t167');
4345                      !!!parse-error (type => 'not closed',
4346                                      value => $self->{open_elements}->[-1]->[0]
4347                                          ->manakai_local_name,
4348                                      token => $token);
4349                    } else {
4350                      !!!cp ('t168');
4351                  }                  }
4352                                    
4353                  splice @{$self->{open_elements}}, $i;                  splice @{$self->{open_elements}}, $i;
# Line 3124  sub _tree_construction_main ($) { Line 4357  sub _tree_construction_main ($) {
4357                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
4358                                    
4359                  !!!next-token;                  !!!next-token;
4360                  redo B;                  next B;
4361                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4362                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t169');
4363                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4364                  ## Ignore the token                  ## Ignore the token
4365                  !!!next-token;                  !!!next-token;
4366                  redo B;                  next B;
4367                } else {                } else {
4368                    !!!cp ('t170');
4369                  #                  #
4370                }                }
4371              } elsif ($token->{tag_name} eq 'caption') {              } elsif ($token->{tag_name} eq 'caption') {
4372                if ($self->{insertion_mode} == IN_CAPTION_IM) {                if ($self->{insertion_mode} == IN_CAPTION_IM) {
4373                  ## have a table element in table scope                  ## have a table element in table scope
4374                  my $i;                  my $i;
4375                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
4376                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
4377                    if ($node->[1] eq $token->{tag_name}) {                      my $node = $self->{open_elements}->[$_];
4378                      $i = $_;                      if ($node->[1] & CAPTION_EL) {
4379                      last INSCOPE;                        !!!cp ('t171');
4380                    } elsif ({                        $i = $_;
4381                              table => 1, html => 1,                        last INSCOPE;
4382                             }->{$node->[1]}) {                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4383                      last INSCOPE;                        !!!cp ('t172');
4384                          last;
4385                        }
4386                    }                    }
4387    
4388                      !!!cp ('t173');
4389                      !!!parse-error (type => 'unmatched end tag',
4390                                      value => $token->{tag_name}, token => $token);
4391                      ## Ignore the token
4392                      !!!next-token;
4393                      next B;
4394                  } # INSCOPE                  } # INSCOPE
                   unless (defined $i) {  
                     !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
                   }  
4395                                    
4396                  ## generate implied end tags                  ## generate implied end tags
4397                  if ({                  while ($self->{open_elements}->[-1]->[1]
4398                       dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
4399                       td => 1, th => 1, tr => 1,                    !!!cp ('t174');
4400                       tbody => 1, tfoot=> 1, thead => 1,                    pop @{$self->{open_elements}};
                     }->{$self->{open_elements}->[-1]->[1]}) {  
                   !!!back-token;  
                   $token = {type => END_TAG_TOKEN,  
                             tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                   redo B;  
4401                  }                  }
4402                                    
4403                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4404                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t175');
4405                      !!!parse-error (type => 'not closed',
4406                                      value => $self->{open_elements}->[-1]->[0]
4407                                          ->manakai_local_name,
4408                                      token => $token);
4409                    } else {
4410                      !!!cp ('t176');
4411                  }                  }
4412                                    
4413                  splice @{$self->{open_elements}}, $i;                  splice @{$self->{open_elements}}, $i;
# Line 3178  sub _tree_construction_main ($) { Line 4417  sub _tree_construction_main ($) {
4417                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
4418                                    
4419                  !!!next-token;                  !!!next-token;
4420                  redo B;                  next B;
4421                } elsif ($self->{insertion_mode} == IN_CELL_IM) {                } elsif ($self->{insertion_mode} == IN_CELL_IM) {
4422                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t177');
4423                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4424                  ## Ignore the token                  ## Ignore the token
4425                  !!!next-token;                  !!!next-token;
4426                  redo B;                  next B;
4427                } else {                } else {
4428                    !!!cp ('t178');
4429                  #                  #
4430                }                }
4431              } elsif ({              } elsif ({
# Line 3195  sub _tree_construction_main ($) { Line 4436  sub _tree_construction_main ($) {
4436                ## have an element in table scope                ## have an element in table scope
4437                my $i;                my $i;
4438                my $tn;                my $tn;
4439                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: {
4440                  my $node = $self->{open_elements}->[$_];                  for (reverse 0..$#{$self->{open_elements}}) {
4441                  if ($node->[1] eq $token->{tag_name}) {                    my $node = $self->{open_elements}->[$_];
4442                    $i = $_;                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
4443                    last INSCOPE;                      !!!cp ('t179');
4444                  } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {                      $i = $_;
4445                    $tn = $node->[1];  
4446                    ## NOTE: There is exactly one |td| or |th| element                      ## Close the cell
4447                    ## in scope in the stack of open elements by definition.                      !!!back-token; # </x>
4448                  } elsif ({                      $token = {type => END_TAG_TOKEN, tag_name => $tn,
4449                            table => 1, html => 1,                                line => $token->{line},
4450                           }->{$node->[1]}) {                                column => $token->{column}};
4451                    last INSCOPE;                      next B;
4452                      } elsif ($node->[1] & TABLE_CELL_EL) {
4453                        !!!cp ('t180');
4454                        $tn = $node->[0]->manakai_local_name;
4455                        ## NOTE: There is exactly one |td| or |th| element
4456                        ## in scope in the stack of open elements by definition.
4457                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4458                        ## ISSUE: Can this be reached?
4459                        !!!cp ('t181');
4460                        last;
4461                      }
4462                  }                  }
4463                } # INSCOPE  
4464                unless (defined $i) {                  !!!cp ('t182');
4465                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
4466                        value => $token->{tag_name}, token => $token);
4467                  ## Ignore the token                  ## Ignore the token
4468                  !!!next-token;                  !!!next-token;
4469                  redo B;                  next B;
4470                }                } # INSCOPE
   
               ## Close the cell  
               !!!back-token; # </?>  
               $token = {type => END_TAG_TOKEN, tag_name => $tn};  
               redo B;  
4471              } elsif ($token->{tag_name} eq 'table' and              } elsif ($token->{tag_name} eq 'table' and
4472                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
4473                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed:caption', token => $token);
4474    
4475                ## As if </caption>                ## As if </caption>
4476                ## have a table element in table scope                ## have a table element in table scope
4477                my $i;                my $i;
4478                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4479                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4480                  if ($node->[1] eq 'caption') {                  if ($node->[1] & CAPTION_EL) {
4481                      !!!cp ('t184');
4482                    $i = $_;                    $i = $_;
4483                    last INSCOPE;                    last INSCOPE;
4484                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
4485                            table => 1, html => 1,                    !!!cp ('t185');
                          }->{$node->[1]}) {  
4486                    last INSCOPE;                    last INSCOPE;
4487                  }                  }
4488                } # INSCOPE                } # INSCOPE
4489                unless (defined $i) {                unless (defined $i) {
4490                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!cp ('t186');
4491                    !!!parse-error (type => 'unmatched end tag:caption', token => $token);
4492                  ## Ignore the token                  ## Ignore the token
4493                  !!!next-token;                  !!!next-token;
4494                  redo B;                  next B;
4495                }                }
4496                                
4497                ## generate implied end tags                ## generate implied end tags
4498                if ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
4499                     dd => 1, dt => 1, li => 1, p => 1,                  !!!cp ('t187');
4500                     td => 1, th => 1, tr => 1,                  pop @{$self->{open_elements}};
                    tbody => 1, tfoot=> 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token; # </table>  
                 $token = {type => END_TAG_TOKEN, tag_name => 'caption'};  
                 !!!back-token;  
                 $token = {type => END_TAG_TOKEN,  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
4501                }                }
4502    
4503                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4504                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t188');
4505                    !!!parse-error (type => 'not closed',
4506                                    value => $self->{open_elements}->[-1]->[0]
4507                                        ->manakai_local_name,
4508                                    token => $token);
4509                  } else {
4510                    !!!cp ('t189');
4511                }                }
4512    
4513                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
# Line 3271  sub _tree_construction_main ($) { Line 4517  sub _tree_construction_main ($) {
4517                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
4518    
4519                ## reprocess                ## reprocess
4520                redo B;                next B;
4521              } elsif ({              } elsif ({
4522                        body => 1, col => 1, colgroup => 1, html => 1,                        body => 1, col => 1, colgroup => 1, html => 1,
4523                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4524                if ($self->{insertion_mode} & BODY_TABLE_IMS) {                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
4525                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t190');
4526                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4527                  ## Ignore the token                  ## Ignore the token
4528                  !!!next-token;                  !!!next-token;
4529                  redo B;                  next B;
4530                } else {                } else {
4531                    !!!cp ('t191');
4532                  #                  #
4533                }                }
4534              } elsif ({              } elsif ({
# Line 3288  sub _tree_construction_main ($) { Line 4536  sub _tree_construction_main ($) {
4536                        thead => 1, tr => 1,                        thead => 1, tr => 1,
4537                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
4538                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
4539                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!cp ('t192');
4540                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4541                ## Ignore the token                ## Ignore the token
4542                !!!next-token;                !!!next-token;
4543                redo B;                next B;
4544              } else {              } else {
4545                  !!!cp ('t193');
4546                #                #
4547              }              }
4548          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4549            for my $entry (@{$self->{open_elements}}) {
4550              unless ($entry->[1] & ALL_END_TAG_OPTIONAL_EL) {
4551                !!!cp ('t75');
4552                !!!parse-error (type => 'in body:#eof', token => $token);
4553                last;
4554              }
4555            }
4556    
4557            ## Stop parsing.
4558            last B;
4559        } else {        } else {
4560          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
4561        }        }
# Line 3302  sub _tree_construction_main ($) { Line 4563  sub _tree_construction_main ($) {
4563        $insert = $insert_to_current;        $insert = $insert_to_current;
4564        #        #
4565      } elsif ($self->{insertion_mode} & TABLE_IMS) {      } elsif ($self->{insertion_mode} & TABLE_IMS) {
4566            if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
4567              ## NOTE: There are "character in table" code clones.          if (not $open_tables->[-1]->[1] and # tainted
4568              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              $token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4569                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4570                                
4571                unless (length $token->{data}) {            unless (length $token->{data}) {
4572                  !!!next-token;              !!!cp ('t194');
4573                  redo B;              !!!next-token;
4574                }              next B;
4575              }            } else {
4576                !!!cp ('t195');
4577              }
4578            }
4579    
4580              !!!parse-error (type => 'in table:#character');              !!!parse-error (type => 'in table:#character', token => $token);
4581    
4582              ## As if in body, but insert into foster parent element              ## As if in body, but insert into foster parent element
4583              ## ISSUE: Spec says that "whenever a node would be inserted              ## ISSUE: Spec says that "whenever a node would be inserted
# Line 3321  sub _tree_construction_main ($) { Line 4585  sub _tree_construction_main ($) {
4585              ## result in a new Text node.              ## result in a new Text node.
4586              $reconstruct_active_formatting_elements->($insert_to_foster);              $reconstruct_active_formatting_elements->($insert_to_foster);
4587                            
4588              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]}) {  
4589                # MUST                # MUST
4590                my $foster_parent_element;                my $foster_parent_element;
4591                my $next_sibling;                my $next_sibling;
4592                my $prev_sibling;                my $prev_sibling;
4593                OE: for (reverse 0..$#{$self->{open_elements}}) {                OE: for (reverse 0..$#{$self->{open_elements}}) {
4594                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
4595                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
4596                    if (defined $parent and $parent->node_type == 1) {                    if (defined $parent and $parent->node_type == 1) {
4597                        !!!cp ('t196');
4598                      $foster_parent_element = $parent;                      $foster_parent_element = $parent;
4599                      $next_sibling = $self->{open_elements}->[$_]->[0];                      $next_sibling = $self->{open_elements}->[$_]->[0];
4600                      $prev_sibling = $next_sibling->previous_sibling;                      $prev_sibling = $next_sibling->previous_sibling;
4601                    } else {                    } else {
4602                        !!!cp ('t197');
4603                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];
4604                      $prev_sibling = $foster_parent_element->last_child;                      $prev_sibling = $foster_parent_element->last_child;
4605                    }                    }
# Line 3348  sub _tree_construction_main ($) { Line 4611  sub _tree_construction_main ($) {
4611                  unless defined $foster_parent_element;                  unless defined $foster_parent_element;
4612                if (defined $prev_sibling and                if (defined $prev_sibling and
4613                    $prev_sibling->node_type == 3) {                    $prev_sibling->node_type == 3) {
4614                    !!!cp ('t198');
4615                  $prev_sibling->manakai_append_text ($token->{data});                  $prev_sibling->manakai_append_text ($token->{data});
4616                } else {                } else {
4617                    !!!cp ('t199');
4618                  $foster_parent_element->insert_before                  $foster_parent_element->insert_before
4619                    ($self->{document}->create_text_node ($token->{data}),                    ($self->{document}->create_text_node ($token->{data}),
4620                     $next_sibling);                     $next_sibling);
4621                }                }
4622              } else {            $open_tables->[-1]->[1] = 1; # tainted
4623                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          } else {
4624              }            !!!cp ('t200');
4625              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4626            }
4627                            
4628              !!!next-token;          !!!next-token;
4629              redo B;          next B;
4630            } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
4631              if ({              if ({
4632                   tr => ($self->{insertion_mode} != IN_ROW_IM),                   tr => ($self->{insertion_mode} != IN_ROW_IM),
4633                   th => 1, td => 1,                   th => 1, td => 1,
4634                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
4635                if ($self->{insertion_mode} == IN_TABLE_IM) {                if ($self->{insertion_mode} == IN_TABLE_IM) {
4636                  ## Clear back to table context                  ## Clear back to table context
4637                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
4638                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
4639                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t201');
4640                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4641                  }                  }
4642                                    
4643                  !!!insert-element ('tbody');                  !!!insert-element ('tbody',, $token);
4644                  $self->{insertion_mode} = IN_TABLE_BODY_IM;                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
4645                  ## reprocess in the "in table body" insertion mode...                  ## reprocess in the "in table body" insertion mode...
4646                }                }
4647    
4648                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
4649                  unless ($token->{tag_name} eq 'tr') {                  unless ($token->{tag_name} eq 'tr') {
4650                    !!!parse-error (type => 'missing start tag:tr');                    !!!cp ('t202');
4651                      !!!parse-error (type => 'missing start tag:tr', token => $token);
4652                  }                  }
4653                                    
4654                  ## Clear back to table body context                  ## Clear back to table body context
4655                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
4656                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
4657                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t203');
4658                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    ## ISSUE: Can this case be reached?
4659                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4660                  }                  }
4661                                    
4662                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
4663                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
4664                    !!!insert-element ($token->{tag_name}, $token->{attributes});                    !!!cp ('t204');
4665                      !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4666                      !!!nack ('t204');
4667                    !!!next-token;                    !!!next-token;
4668                    redo B;                    next B;
4669                  } else {                  } else {
4670                    !!!insert-element ('tr');                    !!!cp ('t205');
4671                      !!!insert-element ('tr',, $token);
4672                    ## reprocess in the "in row" insertion mode                    ## reprocess in the "in row" insertion mode
4673                  }                  }
4674                  } else {
4675                    !!!cp ('t206');
4676                }                }
4677    
4678                ## Clear back to table row context                ## Clear back to table row context
4679                while (not {                while (not ($self->{open_elements}->[-1]->[1]
4680                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
4681                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t207');
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
4682                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4683                }                }
4684                                
4685                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4686                $self->{insertion_mode} = IN_CELL_IM;                $self->{insertion_mode} = IN_CELL_IM;
4687    
4688                push @$active_formatting_elements, ['#marker', ''];                push @$active_formatting_elements, ['#marker', ''];
4689                                
4690                  !!!nack ('t207.1');
4691                !!!next-token;                !!!next-token;
4692                redo B;                next B;
4693              } elsif ({              } elsif ({
4694                        caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
4695                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
# Line 3428  sub _tree_construction_main ($) { Line 4701  sub _tree_construction_main ($) {
4701                  my $i;                  my $i;
4702                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4703                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4704                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
4705                        !!!cp ('t208');
4706                      $i = $_;                      $i = $_;
4707                      last INSCOPE;                      last INSCOPE;
4708                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
4709                              table => 1, html => 1,                      !!!cp ('t209');
                            }->{$node->[1]}) {  
4710                      last INSCOPE;                      last INSCOPE;
4711                    }                    }
4712                  } # INSCOPE                  } # INSCOPE
4713                  unless (defined $i) {                  unless (defined $i) {
4714                    !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});                    !!!cp ('t210');
4715    ## TODO: This type is wrong.
4716                      !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name}, token => $token);
4717                    ## Ignore the token                    ## Ignore the token
4718                      !!!nack ('t210.1');
4719                    !!!next-token;                    !!!next-token;
4720                    redo B;                    next B;
4721                  }                  }
4722                                    
4723                  ## Clear back to table row context                  ## Clear back to table row context
4724                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
4725                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
4726                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t211');
4727                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    ## ISSUE: Can this case be reached?
4728                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4729                  }                  }
4730                                    
4731                  pop @{$self->{open_elements}}; # tr                  pop @{$self->{open_elements}}; # tr
4732                  $self->{insertion_mode} = IN_TABLE_BODY_IM;                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
4733                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
4734                      !!!cp ('t212');
4735                    ## reprocess                    ## reprocess
4736                    redo B;                    !!!ack-later;
4737                      next B;
4738                  } else {                  } else {
4739                      !!!cp ('t213');
4740                    ## reprocess in the "in table body" insertion mode...                    ## reprocess in the "in table body" insertion mode...
4741                  }                  }
4742                }                }
# Line 3467  sub _tree_construction_main ($) { Line 4746  sub _tree_construction_main ($) {
4746                  my $i;                  my $i;
4747                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4748                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4749                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
4750                         tbody => 1, thead => 1, tfoot => 1,                      !!!cp ('t214');
                       }->{$node->[1]}) {  
4751                      $i = $_;                      $i = $_;
4752                      last INSCOPE;                      last INSCOPE;
4753                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
4754                              table => 1, html => 1,                      !!!cp ('t215');
                            }->{$node->[1]}) {  
4755                      last INSCOPE;                      last INSCOPE;
4756                    }                    }
4757                  } # INSCOPE                  } # INSCOPE
4758                  unless (defined $i) {                  unless (defined $i) {
4759                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!cp ('t216');
4760    ## TODO: This erorr type ios wrong.
4761                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4762                    ## Ignore the token                    ## Ignore the token
4763                      !!!nack ('t216.1');
4764                    !!!next-token;                    !!!next-token;
4765                    redo B;                    next B;
4766                  }                  }
4767    
4768                  ## Clear back to table body context                  ## Clear back to table body context
4769                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
4770                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
4771                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t217');
4772                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    ## ISSUE: Can this state be reached?
4773                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4774                  }                  }
4775                                    
# Line 3503  sub _tree_construction_main ($) { Line 4783  sub _tree_construction_main ($) {
4783                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4784                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
4785                  ## reprocess in "in table" insertion mode...                  ## reprocess in "in table" insertion mode...
4786                  } else {
4787                    !!!cp ('t218');
4788                }                }
4789    
4790                if ($token->{tag_name} eq 'col') {                if ($token->{tag_name} eq 'col') {
4791                  ## Clear back to table context                  ## Clear back to table context
4792                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
4793                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
4794                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t219');
4795                      ## ISSUE: Can this state be reached?
4796                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4797                  }                  }
4798                                    
4799                  !!!insert-element ('colgroup');                  !!!insert-element ('colgroup',, $token);
4800                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
4801                  ## reprocess                  ## reprocess
4802                  redo B;                  !!!ack-later;
4803                    next B;
4804                } elsif ({                } elsif ({
4805                          caption => 1,                          caption => 1,
4806                          colgroup => 1,                          colgroup => 1,
4807                          tbody => 1, tfoot => 1, thead => 1,                          tbody => 1, tfoot => 1, thead => 1,
4808                         }->{$token->{tag_name}}) {                         }->{$token->{tag_name}}) {
4809                  ## Clear back to table context                  ## Clear back to table context
4810                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
4811                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
4812                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t220');
4813                      ## ISSUE: Can this state be reached?
4814                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4815                  }                  }
4816                                    
4817                  push @$active_formatting_elements, ['#marker', '']                  push @$active_formatting_elements, ['#marker', '']
4818                      if $token->{tag_name} eq 'caption';                      if $token->{tag_name} eq 'caption';
4819                                    
4820                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4821                  $self->{insertion_mode} = {                  $self->{insertion_mode} = {
4822                                             caption => IN_CAPTION_IM,                                             caption => IN_CAPTION_IM,
4823                                             colgroup => IN_COLUMN_GROUP_IM,                                             colgroup => IN_COLUMN_GROUP_IM,
# Line 3541  sub _tree_construction_main ($) { Line 4826  sub _tree_construction_main ($) {
4826                                             thead => IN_TABLE_BODY_IM,                                             thead => IN_TABLE_BODY_IM,
4827                                            }->{$token->{tag_name}};                                            }->{$token->{tag_name}};
4828                  !!!next-token;                  !!!next-token;
4829                  redo B;                  !!!nack ('t220.1');
4830                    next B;
4831                } else {                } else {
4832                  die "$0: in table: <>: $token->{tag_name}";                  die "$0: in table: <>: $token->{tag_name}";
4833                }                }
4834              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
4835                ## NOTE: There are code clones for this "table in table"                !!!parse-error (type => 'not closed',
4836                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                                value => $self->{open_elements}->[-1]->[0]
4837                                      ->manakai_local_name,
4838                                  token => $token);
4839    
4840                ## As if </table>                ## As if </table>
4841                ## have a table element in table scope                ## have a table element in table scope
4842                my $i;                my $i;
4843                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4844                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4845                  if ($node->[1] eq 'table') {                  if ($node->[1] & TABLE_EL) {
4846                      !!!cp ('t221');
4847                    $i = $_;                    $i = $_;
4848                    last INSCOPE;                    last INSCOPE;
4849                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
4850                            table => 1, html => 1,                    !!!cp ('t222');
                          }->{$node->[1]}) {  
4851                    last INSCOPE;                    last INSCOPE;
4852                  }                  }
4853                } # INSCOPE                } # INSCOPE
4854                unless (defined $i) {                unless (defined $i) {
4855                  !!!parse-error (type => 'unmatched end tag:table');                  !!!cp ('t223');
4856    ## TODO: The following is wrong, maybe.
4857                    !!!parse-error (type => 'unmatched end tag:table', token => $token);
4858                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
4859                    !!!nack ('t223.1');
4860                  !!!next-token;                  !!!next-token;
4861                  redo B;                  next B;
4862                }                }
4863                                
4864    ## TODO: Followings are removed from the latest spec.
4865                ## generate implied end tags                ## generate implied end tags
4866                if ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
4867                     dd => 1, dt => 1, li => 1, p => 1,                  !!!cp ('t224');
4868                     td => 1, th => 1, tr => 1,                  pop @{$self->{open_elements}};
                    tbody => 1, tfoot=> 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token; # <table>  
                 $token = {type => END_TAG_TOKEN, tag_name => 'table'};  
                 !!!back-token;  
                 $token = {type => END_TAG_TOKEN,  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
4869                }                }
4870    
4871                if ($self->{open_elements}->[-1]->[1] ne 'table') {                unless ($self->{open_elements}->[-1]->[1] & TABLE_EL) {
4872                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t225');
4873                    ## NOTE: |<table><tr><table>|
4874                    !!!parse-error (type => 'not closed',
4875                                    value => $self->{open_elements}->[-1]->[0]
4876                                        ->manakai_local_name,
4877                                    token => $token);
4878                  } else {
4879                    !!!cp ('t226');
4880                }                }
4881    
4882                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
4883                  pop @{$open_tables};
4884    
4885                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
4886    
4887                ## reprocess            ## reprocess
4888                redo B;            !!!ack-later;
4889              next B;
4890            } elsif ($token->{tag_name} eq 'style') {
4891              if (not $open_tables->[-1]->[1]) { # tainted
4892                !!!cp ('t227.8');
4893                ## NOTE: This is a "as if in head" code clone.
4894                $parse_rcdata->(CDATA_CONTENT_MODEL);
4895                next B;
4896              } else {
4897                !!!cp ('t227.7');
4898                #
4899              }
4900            } elsif ($token->{tag_name} eq 'script') {
4901              if (not $open_tables->[-1]->[1]) { # tainted
4902                !!!cp ('t227.6');
4903                ## NOTE: This is a "as if in head" code clone.
4904                $script_start_tag->();
4905                next B;
4906              } else {
4907                !!!cp ('t227.5');
4908                #
4909              }
4910            } elsif ($token->{tag_name} eq 'input') {
4911              if (not $open_tables->[-1]->[1]) { # tainted
4912                if ($token->{attributes}->{type}) { ## TODO: case
4913                  my $type = lc $token->{attributes}->{type}->{value};
4914                  if ($type eq 'hidden') {
4915                    !!!cp ('t227.3');
4916                    !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);
4917    
4918                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4919    
4920                    ## TODO: form element pointer
4921    
4922                    pop @{$self->{open_elements}};
4923    
4924                    !!!next-token;
4925                    !!!ack ('t227.2.1');
4926                    next B;
4927                  } else {
4928                    !!!cp ('t227.2');
4929                    #
4930                  }
4931              } else {              } else {
4932                  !!!cp ('t227.1');
4933                #                #
4934              }              }
4935            } elsif ($token->{type} == END_TAG_TOKEN) {            } else {
4936                !!!cp ('t227.4');
4937                #
4938              }
4939            } else {
4940              !!!cp ('t227');
4941              #
4942            }
4943    
4944            !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);
4945    
4946            $insert = $insert_to_foster;
4947            #
4948          } elsif ($token->{type} == END_TAG_TOKEN) {
4949              if ($token->{tag_name} eq 'tr' and              if ($token->{tag_name} eq 'tr' and
4950                  $self->{insertion_mode} == IN_ROW_IM) {                  $self->{insertion_mode} == IN_ROW_IM) {
4951                ## have an element in table scope                ## have an element in table scope
4952                my $i;                my $i;
4953                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4954                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4955                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_ROW_EL) {
4956                      !!!cp ('t228');
4957                    $i = $_;                    $i = $_;
4958                    last INSCOPE;                    last INSCOPE;
4959                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
4960                            table => 1, html => 1,                    !!!cp ('t229');
                          }->{$node->[1]}) {  
4961                    last INSCOPE;                    last INSCOPE;
4962                  }                  }
4963                } # INSCOPE                } # INSCOPE
4964                unless (defined $i) {                unless (defined $i) {
4965                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t230');
4966                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4967                  ## Ignore the token                  ## Ignore the token
4968                    !!!nack ('t230.1');
4969                  !!!next-token;                  !!!next-token;
4970                  redo B;                  next B;
4971                  } else {
4972                    !!!cp ('t232');
4973                }                }
4974    
4975                ## Clear back to table row context                ## Clear back to table row context
4976                while (not {                while (not ($self->{open_elements}->[-1]->[1]
4977                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
4978                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t231');
4979                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this state be reached?
4980                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4981                }                }
4982    
4983                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}}; # tr
4984                $self->{insertion_mode} = IN_TABLE_BODY_IM;                $self->{insertion_mode} = IN_TABLE_BODY_IM;
4985                !!!next-token;                !!!next-token;
4986                redo B;                !!!nack ('t231.1');
4987                  next B;
4988              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
4989                if ($self->{insertion_mode} == IN_ROW_IM) {                if ($self->{insertion_mode} == IN_ROW_IM) {
4990                  ## As if </tr>                  ## As if </tr>
# Line 3639  sub _tree_construction_main ($) { Line 4992  sub _tree_construction_main ($) {
4992                  my $i;                  my $i;
4993                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4994                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4995                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
4996                        !!!cp ('t233');
4997                      $i = $_;                      $i = $_;
4998                      last INSCOPE;                      last INSCOPE;
4999                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5000                              table => 1, html => 1,                      !!!cp ('t234');
                            }->{$node->[1]}) {  
5001                      last INSCOPE;                      last INSCOPE;
5002                    }                    }
5003                  } # INSCOPE                  } # INSCOPE
5004                  unless (defined $i) {                  unless (defined $i) {
5005                    !!!parse-error (type => 'unmatched end tag:'.$token->{type});                    !!!cp ('t235');
5006    ## TODO: The following is wrong.
5007                      !!!parse-error (type => 'unmatched end tag:'.$token->{type}, token => $token);
5008                    ## Ignore the token                    ## Ignore the token
5009                      !!!nack ('t236.1');
5010                    !!!next-token;                    !!!next-token;
5011                    redo B;                    next B;
5012                  }                  }
5013                                    
5014                  ## Clear back to table row context                  ## Clear back to table row context
5015                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5016                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
5017                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t236');
5018                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this state be reached?
5019                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5020                  }                  }
5021                                    
# Line 3673  sub _tree_construction_main ($) { Line 5029  sub _tree_construction_main ($) {
5029                  my $i;                  my $i;
5030                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5031                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5032                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
5033                         tbody => 1, thead => 1, tfoot => 1,                      !!!cp ('t237');
                       }->{$node->[1]}) {  
5034                      $i = $_;                      $i = $_;
5035                      last INSCOPE;                      last INSCOPE;
5036                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5037                              table => 1, html => 1,                      !!!cp ('t238');
                            }->{$node->[1]}) {  
5038                      last INSCOPE;                      last INSCOPE;
5039                    }                    }
5040                  } # INSCOPE                  } # INSCOPE
5041                  unless (defined $i) {                  unless (defined $i) {
5042                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!cp ('t239');
5043                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5044                    ## Ignore the token                    ## Ignore the token
5045                      !!!nack ('t239.1');
5046                    !!!next-token;                    !!!next-token;
5047                    redo B;                    next B;
5048                  }                  }
5049                                    
5050                  ## Clear back to table body context                  ## Clear back to table body context
5051                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5052                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
5053                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t240');
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5054                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5055                  }                  }
5056                                    
# Line 3711  sub _tree_construction_main ($) { Line 5066  sub _tree_construction_main ($) {
5066                  ## reprocess in the "in table" insertion mode...                  ## reprocess in the "in table" insertion mode...
5067                }                }
5068    
5069                  ## NOTE: </table> in the "in table" insertion mode.
5070                  ## When you edit the code fragment below, please ensure that
5071                  ## the code for <table> in the "in table" insertion mode
5072                  ## is synced with it.
5073    
5074                ## have a table element in table scope                ## have a table element in table scope
5075                my $i;                my $i;
5076                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5077                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5078                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_EL) {
5079                      !!!cp ('t241');
5080                    $i = $_;                    $i = $_;
5081                    last INSCOPE;                    last INSCOPE;
5082                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5083                            table => 1, html => 1,                    !!!cp ('t242');
                          }->{$node->[1]}) {  
5084                    last INSCOPE;                    last INSCOPE;
5085                  }                  }
5086                } # INSCOPE                } # INSCOPE
5087                unless (defined $i) {                unless (defined $i) {
5088                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t243');
5089                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5090                  ## Ignore the token                  ## Ignore the token
5091                    !!!nack ('t243.1');
5092                  !!!next-token;                  !!!next-token;
5093                  redo B;                  next B;
               }  
   
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                    tbody => 1, tfoot=> 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token;  
                 $token = {type => END_TAG_TOKEN,  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
                 
               if ($self->{open_elements}->[-1]->[1] ne 'table') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5094                }                }
5095                                    
5096                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5097                  pop @{$open_tables};
5098                                
5099                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5100                                
5101                !!!next-token;                !!!next-token;
5102                redo B;                next B;
5103              } elsif ({              } elsif ({
5104                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
5105                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
# Line 3762  sub _tree_construction_main ($) { Line 5109  sub _tree_construction_main ($) {
5109                  my $i;                  my $i;
5110                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5111                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5112                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5113                        !!!cp ('t247');
5114                      $i = $_;                      $i = $_;
5115                      last INSCOPE;                      last INSCOPE;
5116                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5117                              table => 1, html => 1,                      !!!cp ('t248');
                            }->{$node->[1]}) {  
5118                      last INSCOPE;                      last INSCOPE;
5119                    }                    }
5120                  } # INSCOPE                  } # INSCOPE
5121                    unless (defined $i) {                    unless (defined $i) {
5122                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!cp ('t249');
5123                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5124                      ## Ignore the token                      ## Ignore the token
5125                        !!!nack ('t249.1');
5126                      !!!next-token;                      !!!next-token;
5127                      redo B;                      next B;
5128                    }                    }
5129                                    
5130                  ## As if </tr>                  ## As if </tr>
# Line 3783  sub _tree_construction_main ($) { Line 5132  sub _tree_construction_main ($) {
5132                  my $i;                  my $i;
5133                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5134                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5135                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5136                        !!!cp ('t250');
5137                      $i = $_;                      $i = $_;
5138                      last INSCOPE;                      last INSCOPE;
5139                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5140                              table => 1, html => 1,                      !!!cp ('t251');
                            }->{$node->[1]}) {  
5141                      last INSCOPE;                      last INSCOPE;
5142                    }                    }
5143                  } # INSCOPE                  } # INSCOPE
5144                    unless (defined $i) {                    unless (defined $i) {
5145                      !!!parse-error (type => 'unmatched end tag:tr');                      !!!cp ('t252');
5146                        !!!parse-error (type => 'unmatched end tag:tr', token => $token);
5147                      ## Ignore the token                      ## Ignore the token
5148                        !!!nack ('t252.1');
5149                      !!!next-token;                      !!!next-token;
5150                      redo B;                      next B;
5151                    }                    }
5152                                    
5153                  ## Clear back to table row context                  ## Clear back to table row context
5154                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5155                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
5156                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t253');
5157                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this case be reached?
5158                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5159                  }                  }
5160                                    
# Line 3816  sub _tree_construction_main ($) { Line 5167  sub _tree_construction_main ($) {
5167                my $i;                my $i;
5168                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5169                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5170                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5171                      !!!cp ('t254');
5172                    $i = $_;                    $i = $_;
5173                    last INSCOPE;                    last INSCOPE;
5174                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5175                            table => 1, html => 1,                    !!!cp ('t255');
                          }->{$node->[1]}) {  
5176                    last INSCOPE;                    last INSCOPE;
5177                  }                  }
5178                } # INSCOPE                } # INSCOPE
5179                unless (defined $i) {                unless (defined $i) {
5180                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t256');
5181                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5182                  ## Ignore the token                  ## Ignore the token
5183                    !!!nack ('t256.1');
5184                  !!!next-token;                  !!!next-token;
5185                  redo B;                  next B;
5186                }                }
5187    
5188                ## Clear back to table body context                ## Clear back to table body context
5189                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5190                  tbody => 1, tfoot => 1, thead => 1, html => 1,                                & TABLE_ROWS_SCOPING_EL)) {
5191                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t257');
5192                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this case be reached?
5193                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5194                }                }
5195    
5196                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5197                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
5198                  !!!nack ('t257.1');
5199                !!!next-token;                !!!next-token;
5200                redo B;                next B;
5201              } elsif ({              } elsif ({
5202                        body => 1, caption => 1, col => 1, colgroup => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
5203                        html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
5204                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM
5205                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
5206                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5207                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!cp ('t258');
5208                ## Ignore the token            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5209                !!!next-token;            ## Ignore the token
5210                redo B;            !!!nack ('t258.1');
5211              } else {             !!!next-token;
5212                #            next B;
5213              }          } else {
5214            } else {            !!!cp ('t259');
5215              die "$0: $token->{type}: Unknown token type";            !!!parse-error (type => 'in table:/'.$token->{tag_name}, token => $token);
           }  
5216    
5217        !!!parse-error (type => 'in table:'.$token->{tag_name});            $insert = $insert_to_foster;
5218              #
5219            }
5220          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5221            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
5222                    @{$self->{open_elements}} == 1) { # redundant, maybe
5223              !!!parse-error (type => 'in body:#eof', token => $token);
5224              !!!cp ('t259.1');
5225              #
5226            } else {
5227              !!!cp ('t259.2');
5228              #
5229            }
5230    
5231        $insert = $insert_to_foster;          ## Stop parsing
5232        #          last B;
5233          } else {
5234            die "$0: $token->{type}: Unknown token type";
5235          }
5236      } elsif ($self->{insertion_mode} == IN_COLUMN_GROUP_IM) {      } elsif ($self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
5237            if ($token->{type} == CHARACTER_TOKEN) {            if ($token->{type} == CHARACTER_TOKEN) {
5238              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5239                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5240                unless (length $token->{data}) {                unless (length $token->{data}) {
5241                    !!!cp ('t260');
5242                  !!!next-token;                  !!!next-token;
5243                  redo B;                  next B;
5244                }                }
5245              }              }
5246                            
5247                !!!cp ('t261');
5248              #              #
5249            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
5250              if ($token->{tag_name} eq 'col') {              if ($token->{tag_name} eq 'col') {
5251                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!cp ('t262');
5252                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5253                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5254                  !!!ack ('t262.1');
5255                !!!next-token;                !!!next-token;
5256                redo B;                next B;
5257              } else {              } else {
5258                  !!!cp ('t263');
5259                #                #
5260              }              }
5261            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
5262              if ($token->{tag_name} eq 'colgroup') {              if ($token->{tag_name} eq 'colgroup') {
5263                if ($self->{open_elements}->[-1]->[1] eq 'html') {                if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5264                  !!!parse-error (type => 'unmatched end tag:colgroup');                  !!!cp ('t264');
5265                    !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);
5266                  ## Ignore the token                  ## Ignore the token
5267                  !!!next-token;                  !!!next-token;
5268                  redo B;                  next B;
5269                } else {                } else {
5270                    !!!cp ('t265');
5271                  pop @{$self->{open_elements}}; # colgroup                  pop @{$self->{open_elements}}; # colgroup
5272                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
5273                  !!!next-token;                  !!!next-token;
5274                  redo B;                              next B;            
5275                }                }
5276              } elsif ($token->{tag_name} eq 'col') {              } elsif ($token->{tag_name} eq 'col') {
5277                !!!parse-error (type => 'unmatched end tag:col');                !!!cp ('t266');
5278                  !!!parse-error (type => 'unmatched end tag:col', token => $token);
5279                ## Ignore the token                ## Ignore the token
5280                !!!next-token;                !!!next-token;
5281                redo B;                next B;
5282              } else {              } else {
5283                  !!!cp ('t267');
5284                #                #
5285              }              }
5286            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5287              #          if ($self->{open_elements}->[-1]->[1] & HTML_EL and
5288            }              @{$self->{open_elements}} == 1) { # redundant, maybe
5289              !!!cp ('t270.2');
5290              ## Stop parsing.
5291              last B;
5292            } else {
5293              ## NOTE: As if </colgroup>.
5294              !!!cp ('t270.1');
5295              pop @{$self->{open_elements}}; # colgroup
5296              $self->{insertion_mode} = IN_TABLE_IM;
5297              ## Reprocess.
5298              next B;
5299            }
5300          } else {
5301            die "$0: $token->{type}: Unknown token type";
5302          }
5303    
5304            ## As if </colgroup>            ## As if </colgroup>
5305            if ($self->{open_elements}->[-1]->[1] eq 'html') {            if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5306              !!!parse-error (type => 'unmatched end tag:colgroup');              !!!cp ('t269');
5307    ## TODO: Wrong error type?
5308                !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);
5309              ## Ignore the token              ## Ignore the token
5310                !!!nack ('t269.1');
5311              !!!next-token;              !!!next-token;
5312              redo B;              next B;
5313            } else {            } else {
5314                !!!cp ('t270');
5315              pop @{$self->{open_elements}}; # colgroup              pop @{$self->{open_elements}}; # colgroup
5316              $self->{insertion_mode} = IN_TABLE_IM;              $self->{insertion_mode} = IN_TABLE_IM;
5317                !!!ack-later;
5318              ## reprocess              ## reprocess
5319              redo B;              next B;
5320              }
5321        } elsif ($self->{insertion_mode} & SELECT_IMS) {
5322          if ($token->{type} == CHARACTER_TOKEN) {
5323            !!!cp ('t271');
5324            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5325            !!!next-token;
5326            next B;
5327          } elsif ($token->{type} == START_TAG_TOKEN) {
5328            if ($token->{tag_name} eq 'option') {
5329              if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5330                !!!cp ('t272');
5331                ## As if </option>
5332                pop @{$self->{open_elements}};
5333              } else {
5334                !!!cp ('t273');
5335            }            }
     } elsif ($self->{insertion_mode} == IN_SELECT_IM) {  
           if ($token->{type} == CHARACTER_TOKEN) {  
             $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} == START_TAG_TOKEN) {  
             if ($token->{tag_name} eq 'option') {  
               if ($self->{open_elements}->[-1]->[1] eq 'option') {  
                 ## As if </option>  
                 pop @{$self->{open_elements}};  
               }  
5336    
5337                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5338                !!!next-token;            !!!nack ('t273.1');
5339                redo B;            !!!next-token;
5340              } elsif ($token->{tag_name} eq 'optgroup') {            next B;
5341                if ($self->{open_elements}->[-1]->[1] eq 'option') {          } elsif ($token->{tag_name} eq 'optgroup') {
5342                  ## As if </option>            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5343                  pop @{$self->{open_elements}};              !!!cp ('t274');
5344                }              ## As if </option>
5345                pop @{$self->{open_elements}};
5346              } else {
5347                !!!cp ('t275');
5348              }
5349    
5350                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            if ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
5351                  ## As if </optgroup>              !!!cp ('t276');
5352                  pop @{$self->{open_elements}};              ## As if </optgroup>
5353                }              pop @{$self->{open_elements}};
5354              } else {
5355                !!!cp ('t277');
5356              }
5357    
5358                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5359                !!!next-token;            !!!nack ('t277.1');
5360                redo B;            !!!next-token;
5361              } elsif ($token->{tag_name} eq 'select') {            next B;
5362                !!!parse-error (type => 'not closed:select');          } elsif ($token->{tag_name} eq 'select' or
5363                ## As if </select> instead                   $token->{tag_name} eq 'input' or
5364                ## have an element in table scope                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
5365                my $i;                    {
5366                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                     caption => 1, table => 1,
5367                  my $node = $self->{open_elements}->[$_];                     tbody => 1, tfoot => 1, thead => 1,
5368                  if ($node->[1] eq $token->{tag_name}) {                     tr => 1, td => 1, th => 1,
5369                    $i = $_;                    }->{$token->{tag_name}})) {
5370                    last INSCOPE;            ## TODO: The type below is not good - <select> is replaced by </select>
5371                  } elsif ({            !!!parse-error (type => 'not closed:select', token => $token);
5372                            table => 1, html => 1,            ## NOTE: As if the token were </select> (<select> case) or
5373                           }->{$node->[1]}) {            ## as if there were </select> (otherwise).
5374                    last INSCOPE;            ## have an element in table scope
5375                  }            my $i;
5376                } # INSCOPE            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5377                unless (defined $i) {              my $node = $self->{open_elements}->[$_];
5378                  !!!parse-error (type => 'unmatched end tag:select');              if ($node->[1] & SELECT_EL) {
5379                  ## Ignore the token                !!!cp ('t278');
5380                  !!!next-token;                $i = $_;
5381                  redo B;                last INSCOPE;
5382                }              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5383                  !!!cp ('t279');
5384                  last INSCOPE;
5385                }
5386              } # INSCOPE
5387              unless (defined $i) {
5388                !!!cp ('t280');
5389                !!!parse-error (type => 'unmatched end tag:select', token => $token);
5390                ## Ignore the token
5391                !!!nack ('t280.1');
5392                !!!next-token;
5393                next B;
5394              }
5395                                
5396                splice @{$self->{open_elements}}, $i;            !!!cp ('t281');
5397              splice @{$self->{open_elements}}, $i;
5398    
5399                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
5400    
5401                !!!next-token;            if ($token->{tag_name} eq 'select') {
5402                redo B;              !!!nack ('t281.2');
5403              } else {              !!!next-token;
5404                #              next B;
5405              } else {
5406                !!!cp ('t281.1');
5407                !!!ack-later;
5408                ## Reprocess the token.
5409                next B;
5410              }
5411            } else {
5412              !!!cp ('t282');
5413              !!!parse-error (type => 'in select:'.$token->{tag_name}, token => $token);
5414              ## Ignore the token
5415              !!!nack ('t282.1');
5416              !!!next-token;
5417              next B;
5418            }
5419          } elsif ($token->{type} == END_TAG_TOKEN) {
5420            if ($token->{tag_name} eq 'optgroup') {
5421              if ($self->{open_elements}->[-1]->[1] & OPTION_EL and
5422                  $self->{open_elements}->[-2]->[1] & OPTGROUP_EL) {
5423                !!!cp ('t283');
5424                ## As if </option>
5425                splice @{$self->{open_elements}}, -2;
5426              } elsif ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
5427                !!!cp ('t284');
5428                pop @{$self->{open_elements}};
5429              } else {
5430                !!!cp ('t285');
5431                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5432                ## Ignore the token
5433              }
5434              !!!nack ('t285.1');
5435              !!!next-token;
5436              next B;
5437            } elsif ($token->{tag_name} eq 'option') {
5438              if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5439                !!!cp ('t286');
5440                pop @{$self->{open_elements}};
5441              } else {
5442                !!!cp ('t287');
5443                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5444                ## Ignore the token
5445              }
5446              !!!nack ('t287.1');
5447              !!!next-token;
5448              next B;
5449            } elsif ($token->{tag_name} eq 'select') {
5450              ## have an element in table scope
5451              my $i;
5452              INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5453                my $node = $self->{open_elements}->[$_];
5454                if ($node->[1] & SELECT_EL) {
5455                  !!!cp ('t288');
5456                  $i = $_;
5457                  last INSCOPE;
5458                } elsif ($node->[1] & TABLE_SCOPING_EL) {
5459                  !!!cp ('t289');
5460                  last INSCOPE;
5461              }              }
5462            } elsif ($token->{type} == END_TAG_TOKEN) {            } # INSCOPE
5463              if ($token->{tag_name} eq 'optgroup') {            unless (defined $i) {
5464                if ($self->{open_elements}->[-1]->[1] eq 'option' and              !!!cp ('t290');
5465                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5466                  ## As if </option>              ## Ignore the token
5467                  splice @{$self->{open_elements}}, -2;              !!!nack ('t290.1');
5468                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {              !!!next-token;
5469                  pop @{$self->{open_elements}};              next B;
5470                } else {            }
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
               }  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'option') {  
               if ($self->{open_elements}->[-1]->[1] eq 'option') {  
                 pop @{$self->{open_elements}};  
               } else {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
               }  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'select') {  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
5471                                
5472                splice @{$self->{open_elements}}, $i;            !!!cp ('t291');
5473              splice @{$self->{open_elements}}, $i;
5474    
5475                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
5476    
5477                !!!next-token;            !!!nack ('t291.1');
5478                redo B;            !!!next-token;
5479              } elsif ({            next B;
5480                        caption => 1, table => 1, tbody => 1,          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
5481                        tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                   {
5482                       }->{$token->{tag_name}}) {                    caption => 1, table => 1, tbody => 1,
5483                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
5484                                   }->{$token->{tag_name}}) {
5485                ## have an element in table scope  ## TODO: The following is wrong?
5486                my $i;            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
5487                                
5488                ## As if </select>            ## have an element in table scope
5489                ## have an element in table scope            my $i;
5490                undef $i;            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5491                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {              my $node = $self->{open_elements}->[$_];
5492                  my $node = $self->{open_elements}->[$_];              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5493                  if ($node->[1] eq 'select') {                !!!cp ('t292');
5494                    $i = $_;                $i = $_;
5495                    last INSCOPE;                last INSCOPE;
5496                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5497                            table => 1, html => 1,                !!!cp ('t293');
5498                           }->{$node->[1]}) {                last INSCOPE;
5499                    last INSCOPE;              }
5500                  }            } # INSCOPE
5501                } # INSCOPE            unless (defined $i) {
5502                unless (defined $i) {              !!!cp ('t294');
5503                  !!!parse-error (type => 'unmatched end tag:select');              ## Ignore the token
5504                  ## Ignore the </select> token              !!!nack ('t294.1');
5505                  !!!next-token; ## TODO: ok?              !!!next-token;
5506                  redo B;              next B;
5507                }            }
5508                                
5509                splice @{$self->{open_elements}}, $i;            ## As if </select>
5510              ## have an element in table scope
5511                $self->_reset_insertion_mode;            undef $i;
5512              INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5513                ## reprocess              my $node = $self->{open_elements}->[$_];
5514                redo B;              if ($node->[1] & SELECT_EL) {
5515              } else {                !!!cp ('t295');
5516                #                $i = $_;
5517                  last INSCOPE;
5518                } elsif ($node->[1] & TABLE_SCOPING_EL) {
5519    ## ISSUE: Can this state be reached?
5520                  !!!cp ('t296');
5521                  last INSCOPE;
5522              }              }
5523            } else {            } # INSCOPE
5524              #            unless (defined $i) {
5525                !!!cp ('t297');
5526    ## TODO: The following error type is correct?
5527                !!!parse-error (type => 'unmatched end tag:select', token => $token);
5528                ## Ignore the </select> token
5529                !!!nack ('t297.1');
5530                !!!next-token; ## TODO: ok?
5531                next B;
5532            }            }
5533                  
5534              !!!cp ('t298');
5535              splice @{$self->{open_elements}}, $i;
5536    
5537              $self->_reset_insertion_mode;
5538    
5539            !!!parse-error (type => 'in select:'.$token->{tag_name});            !!!ack-later;
5540              ## reprocess
5541              next B;
5542            } else {
5543              !!!cp ('t299');
5544              !!!parse-error (type => 'in select:/'.$token->{tag_name}, token => $token);
5545            ## Ignore the token            ## Ignore the token
5546              !!!nack ('t299.3');
5547            !!!next-token;            !!!next-token;
5548            redo B;            next B;
5549            }
5550          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5551            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
5552                    @{$self->{open_elements}} == 1) { # redundant, maybe
5553              !!!cp ('t299.1');
5554              !!!parse-error (type => 'in body:#eof', token => $token);
5555            } else {
5556              !!!cp ('t299.2');
5557            }
5558    
5559            ## Stop parsing.
5560            last B;
5561          } else {
5562            die "$0: $token->{type}: Unknown token type";
5563          }
5564      } elsif ($self->{insertion_mode} & BODY_AFTER_IMS) {      } elsif ($self->{insertion_mode} & BODY_AFTER_IMS) {
5565        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
5566          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
# Line 4106  sub _tree_construction_main ($) { Line 5571  sub _tree_construction_main ($) {
5571            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5572                        
5573            unless (length $token->{data}) {            unless (length $token->{data}) {
5574                !!!cp ('t300');
5575              !!!next-token;              !!!next-token;
5576              redo B;              next B;
5577            }            }
5578          }          }
5579                    
5580          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
5581            !!!parse-error (type => 'after html:#character');            !!!cp ('t301');
5582              !!!parse-error (type => 'after html:#character', token => $token);
5583    
5584            ## Reprocess in the "main" phase, "after body" insertion mode...            ## Reprocess in the "after body" insertion mode.
5585            } else {
5586              !!!cp ('t302');
5587          }          }
5588                    
5589          ## "after body" insertion mode          ## "after body" insertion mode
5590          !!!parse-error (type => 'after body:#character');          !!!parse-error (type => 'after body:#character', token => $token);
5591    
5592          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
5593          ## reprocess          ## reprocess
5594          redo B;          next B;
5595        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5596          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
5597            !!!parse-error (type => 'after html:'.$token->{tag_name});            !!!cp ('t303');
5598              !!!parse-error (type => 'after html:'.$token->{tag_name}, token => $token);
5599                        
5600            ## Reprocess in the "main" phase, "after body" insertion mode...            ## Reprocess in the "after body" insertion mode.
5601            } else {
5602              !!!cp ('t304');
5603          }          }
5604    
5605          ## "after body" insertion mode          ## "after body" insertion mode
5606          !!!parse-error (type => 'after body:'.$token->{tag_name});          !!!parse-error (type => 'after body:'.$token->{tag_name}, token => $token);
5607    
5608          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
5609            !!!ack-later;
5610          ## reprocess          ## reprocess
5611          redo B;          next B;
5612        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
5613          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
5614            !!!parse-error (type => 'after html:/'.$token->{tag_name});            !!!cp ('t305');
5615              !!!parse-error (type => 'after html:/'.$token->{tag_name}, token => $token);
5616                        
5617            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
5618            ## Reprocess in the "main" phase, "after body" insertion mode...            ## Reprocess in the "after body" insertion mode.
5619            } else {
5620              !!!cp ('t306');
5621          }          }
5622    
5623          ## "after body" insertion mode          ## "after body" insertion mode
5624          if ($token->{tag_name} eq 'html') {          if ($token->{tag_name} eq 'html') {
5625            if (defined $self->{inner_html_node}) {            if (defined $self->{inner_html_node}) {
5626              !!!parse-error (type => 'unmatched end tag:html');              !!!cp ('t307');
5627                !!!parse-error (type => 'unmatched end tag:html', token => $token);
5628              ## Ignore the token              ## Ignore the token
5629              !!!next-token;              !!!next-token;
5630              redo B;              next B;
5631            } else {            } else {
5632                !!!cp ('t308');
5633              $self->{insertion_mode} = AFTER_HTML_BODY_IM;              $self->{insertion_mode} = AFTER_HTML_BODY_IM;
5634              !!!next-token;              !!!next-token;
5635              redo B;              next B;
5636            }            }
5637          } else {          } else {
5638            !!!parse-error (type => 'after body:/'.$token->{tag_name});            !!!cp ('t309');
5639              !!!parse-error (type => 'after body:/'.$token->{tag_name}, token => $token);
5640    
5641            $self->{insertion_mode} = IN_BODY_IM;            $self->{insertion_mode} = IN_BODY_IM;
5642            ## reprocess            ## reprocess
5643            redo B;            next B;
5644          }          }
5645          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5646            !!!cp ('t309.2');
5647            ## Stop parsing
5648            last B;
5649        } else {        } else {
5650          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5651        }        }
# Line 4172  sub _tree_construction_main ($) { Line 5655  sub _tree_construction_main ($) {
5655            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5656                        
5657            unless (length $token->{data}) {            unless (length $token->{data}) {
5658                !!!cp ('t310');
5659              !!!next-token;              !!!next-token;
5660              redo B;              next B;
5661            }            }
5662          }          }
5663                    
5664          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
5665            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
5666              !!!parse-error (type => 'in frameset:#character');              !!!cp ('t311');
5667                !!!parse-error (type => 'in frameset:#character', token => $token);
5668            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
5669              !!!parse-error (type => 'after frameset:#character');              !!!cp ('t312');
5670                !!!parse-error (type => 'after frameset:#character', token => $token);
5671            } else { # "after html frameset"            } else { # "after html frameset"
5672              !!!parse-error (type => 'after html:#character');              !!!cp ('t313');
5673                !!!parse-error (type => 'after html:#character', token => $token);
5674    
5675              $self->{insertion_mode} = AFTER_FRAMESET_IM;              $self->{insertion_mode} = AFTER_FRAMESET_IM;
5676              ## Reprocess in the "main" phase, "after frameset"...              ## Reprocess in the "after frameset" insertion mode.
5677              !!!parse-error (type => 'after frameset:#character');              !!!parse-error (type => 'after frameset:#character', token => $token);
5678            }            }
5679                        
5680            ## Ignore the token.            ## Ignore the token.
5681            if (length $token->{data}) {            if (length $token->{data}) {
5682                !!!cp ('t314');
5683              ## reprocess the rest of characters              ## reprocess the rest of characters
5684            } else {            } else {
5685                !!!cp ('t315');
5686              !!!next-token;              !!!next-token;
5687            }            }
5688            redo B;            next B;
5689          }          }
5690                    
5691          die qq[$0: Character "$token->{data}"];          die qq[$0: Character "$token->{data}"];
5692        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5693          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
5694            !!!parse-error (type => 'after html:'.$token->{tag_name});            !!!cp ('t316');
5695              !!!parse-error (type => 'after html:'.$token->{tag_name}, token => $token);
5696    
5697            $self->{insertion_mode} = AFTER_FRAMESET_IM;            $self->{insertion_mode} = AFTER_FRAMESET_IM;
5698            ## Process in the "main" phase, "after frameset" insertion mode...            ## Process in the "after frameset" insertion mode.
5699          }          } else {
5700              !!!cp ('t317');
5701            }
5702    
5703          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
5704              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
5705            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!cp ('t318');
5706              !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5707              !!!nack ('t318.1');
5708            !!!next-token;            !!!next-token;
5709            redo B;            next B;
5710          } elsif ($token->{tag_name} eq 'frame' and          } elsif ($token->{tag_name} eq 'frame' and
5711                   $self->{insertion_mode} == IN_FRAMESET_IM) {                   $self->{insertion_mode} == IN_FRAMESET_IM) {
5712            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!cp ('t319');
5713              !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5714            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
5715              !!!ack ('t319.1');
5716            !!!next-token;            !!!next-token;
5717            redo B;            next B;
5718          } elsif ($token->{tag_name} eq 'noframes') {          } elsif ($token->{tag_name} eq 'noframes') {
5719              !!!cp ('t320');
5720            ## NOTE: As if in body.            ## NOTE: As if in body.
5721            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);            $parse_rcdata->(CDATA_CONTENT_MODEL);
5722            redo B;            next B;
5723          } else {          } else {
5724            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
5725              !!!parse-error (type => 'in frameset:'.$token->{tag_name});              !!!cp ('t321');
5726                !!!parse-error (type => 'in frameset:'.$token->{tag_name}, token => $token);
5727            } else {            } else {
5728              !!!parse-error (type => 'after frameset:'.$token->{tag_name});              !!!cp ('t322');
5729                !!!parse-error (type => 'after frameset:'.$token->{tag_name}, token => $token);
5730            }            }
5731            ## Ignore the token            ## Ignore the token
5732              !!!nack ('t322.1');
5733            !!!next-token;            !!!next-token;
5734            redo B;            next B;
5735          }          }
5736        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
5737          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
5738            !!!parse-error (type => 'after html:/'.$token->{tag_name});            !!!cp ('t323');
5739              !!!parse-error (type => 'after html:/'.$token->{tag_name}, token => $token);
5740    
5741            $self->{insertion_mode} = AFTER_FRAMESET_IM;            $self->{insertion_mode} = AFTER_FRAMESET_IM;
5742            ## Process in the "main" phase, "after frameset" insertion mode...            ## Process in the "after frameset" insertion mode.
5743            } else {
5744              !!!cp ('t324');
5745          }          }
5746    
5747          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
5748              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
5749            if ($self->{open_elements}->[-1]->[1] eq 'html' and            if ($self->{open_elements}->[-1]->[1] & HTML_EL and
5750                @{$self->{open_elements}} == 1) {                @{$self->{open_elements}} == 1) {
5751              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t325');
5752                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5753              ## Ignore the token              ## Ignore the token
5754              !!!next-token;              !!!next-token;
5755            } else {            } else {
5756                !!!cp ('t326');
5757              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
5758              !!!next-token;              !!!next-token;
5759            }            }
5760    
5761            if (not defined $self->{inner_html_node} and            if (not defined $self->{inner_html_node} and
5762                $self->{open_elements}->[-1]->[1] ne 'frameset') {                not ($self->{open_elements}->[-1]->[1] & FRAMESET_EL)) {
5763                !!!cp ('t327');
5764              $self->{insertion_mode} = AFTER_FRAMESET_IM;              $self->{insertion_mode} = AFTER_FRAMESET_IM;
5765              } else {
5766                !!!cp ('t328');
5767            }            }
5768            redo B;            next B;
5769          } elsif ($token->{tag_name} eq 'html' and          } elsif ($token->{tag_name} eq 'html' and
5770                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {
5771              !!!cp ('t329');
5772            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
5773            !!!next-token;            !!!next-token;
5774            redo B;            next B;
5775          } else {          } else {
5776            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
5777              !!!parse-error (type => 'in frameset:/'.$token->{tag_name});              !!!cp ('t330');
5778                !!!parse-error (type => 'in frameset:/'.$token->{tag_name}, token => $token);
5779            } else {            } else {
5780              !!!parse-error (type => 'after frameset:/'.$token->{tag_name});              !!!cp ('t331');
5781                !!!parse-error (type => 'after frameset:/'.$token->{tag_name}, token => $token);
5782            }            }
5783            ## Ignore the token            ## Ignore the token
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 ('t331.1');
5791              !!!parse-error (type => 'in body:#eof', token => $token);
5792            } else {
5793              !!!cp ('t331.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 4285  sub _tree_construction_main ($) { Line 5807  sub _tree_construction_main ($) {
5807      ## "in body" insertion mode      ## "in body" insertion mode
5808      if ($token->{type} == START_TAG_TOKEN) {      if ($token->{type} == START_TAG_TOKEN) {
5809        if ($token->{tag_name} eq 'script') {        if ($token->{tag_name} eq 'script') {
5810            !!!cp ('t332');
5811          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
5812          $script_start_tag->($insert);          $script_start_tag->();
5813          redo B;          next B;
5814        } elsif ($token->{tag_name} eq 'style') {        } elsif ($token->{tag_name} eq 'style') {
5815            !!!cp ('t333');
5816          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
5817          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);          $parse_rcdata->(CDATA_CONTENT_MODEL);
5818          redo B;          next B;
5819        } elsif ({        } elsif ({
5820                  base => 1, link => 1,                  base => 1, link => 1,
5821                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
5822            !!!cp ('t334');
5823          ## 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
5824          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
5825          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
5826            !!!ack ('t334.1');
5827          !!!next-token;          !!!next-token;
5828          redo B;          next B;
5829        } elsif ($token->{tag_name} eq 'meta') {        } elsif ($token->{tag_name} eq 'meta') {
5830          ## 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
5831          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
5832          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.
5833    
5834          unless ($self->{confident}) {          unless ($self->{confident}) {
           my $charset;  
5835            if ($token->{attributes}->{charset}) { ## TODO: And if supported            if ($token->{attributes}->{charset}) { ## TODO: And if supported
5836              $charset = $token->{attributes}->{charset}->{value};              !!!cp ('t335');
5837            }              $self->{change_encoding}
5838            if ($token->{attributes}->{'http-equiv'}) {                  ->($self, $token->{attributes}->{charset}->{value}, $token);
5839                
5840                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
5841                    ->set_user_data (manakai_has_reference =>
5842                                         $token->{attributes}->{charset}
5843                                             ->{has_reference});
5844              } elsif ($token->{attributes}->{content}) {
5845              ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.              ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.
5846              if ($token->{attributes}->{'http-equiv'}->{value}              if ($token->{attributes}->{content}->{value}
5847                  =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=                  =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
5848                        [\x09-\x0D\x20]*=
5849                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
5850                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
5851                $charset = defined $1 ? $1 : defined $2 ? $2 : $3;                !!!cp ('t336');
5852              } ## TODO: And if supported                $self->{change_encoding}
5853                      ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);
5854                  $meta_el->[0]->get_attribute_node_ns (undef, 'content')
5855                      ->set_user_data (manakai_has_reference =>
5856                                           $token->{attributes}->{content}
5857                                                 ->{has_reference});
5858                }
5859              }
5860            } else {
5861              if ($token->{attributes}->{charset}) {
5862                !!!cp ('t337');
5863                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
5864                    ->set_user_data (manakai_has_reference =>
5865                                         $token->{attributes}->{charset}
5866                                             ->{has_reference});
5867              }
5868              if ($token->{attributes}->{content}) {
5869                !!!cp ('t338');
5870                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
5871                    ->set_user_data (manakai_has_reference =>
5872                                         $token->{attributes}->{content}
5873                                             ->{has_reference});
5874            }            }
           ## TODO: Change the encoding  
5875          }          }
5876    
5877            !!!ack ('t338.1');
5878          !!!next-token;          !!!next-token;
5879          redo B;          next B;
5880        } elsif ($token->{tag_name} eq 'title') {        } elsif ($token->{tag_name} eq 'title') {
5881          !!!parse-error (type => 'in body:title');          !!!cp ('t341');
5882          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
5883          $parse_rcdata->(RCDATA_CONTENT_MODEL, sub {          $parse_rcdata->(RCDATA_CONTENT_MODEL);
5884            if (defined $self->{head_element}) {          next B;
             $self->{head_element}->append_child ($_[0]);  
           } else {  
             $insert->($_[0]);  
           }  
         });  
         redo B;  
5885        } elsif ($token->{tag_name} eq 'body') {        } elsif ($token->{tag_name} eq 'body') {
5886          !!!parse-error (type => 'in body:body');          !!!parse-error (type => 'in body:body', token => $token);
5887                                
5888          if (@{$self->{open_elements}} == 1 or          if (@{$self->{open_elements}} == 1 or
5889              $self->{open_elements}->[1]->[1] ne 'body') {              not ($self->{open_elements}->[1]->[1] & BODY_EL)) {
5890              !!!cp ('t342');
5891            ## Ignore the token            ## Ignore the token
5892          } else {          } else {
5893            my $body_el = $self->{open_elements}->[1]->[0];            my $body_el = $self->{open_elements}->[1]->[0];
5894            for my $attr_name (keys %{$token->{attributes}}) {            for my $attr_name (keys %{$token->{attributes}}) {
5895              unless ($body_el->has_attribute_ns (undef, $attr_name)) {              unless ($body_el->has_attribute_ns (undef, $attr_name)) {
5896                  !!!cp ('t343');
5897                $body_el->set_attribute_ns                $body_el->set_attribute_ns
5898                  (undef, [undef, $attr_name],                  (undef, [undef, $attr_name],
5899                   $token->{attributes}->{$attr_name}->{value});                   $token->{attributes}->{$attr_name}->{value});
5900              }              }
5901            }            }
5902          }          }
5903            !!!nack ('t343.1');
5904          !!!next-token;          !!!next-token;
5905          redo B;          next B;
5906        } elsif ({        } elsif ({
5907                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
5908                  div => 1, dl => 1, fieldset => 1, listing => 1,                  div => 1, dl => 1, fieldset => 1,
5909                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
5910                  menu => 1, ol => 1, p => 1, ul => 1,                  menu => 1, ol => 1, p => 1, ul => 1,
5911                  pre => 1,                  pre => 1, listing => 1,
5912                    form => 1,
5913                    table => 1,
5914                    hr => 1,
5915                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
5916            if ($token->{tag_name} eq 'form' and defined $self->{form_element}) {
5917              !!!cp ('t350');
5918              !!!parse-error (type => 'in form:form', token => $token);
5919              ## Ignore the token
5920              !!!nack ('t350.1');
5921              !!!next-token;
5922              next B;
5923            }
5924    
5925          ## has a p element in scope          ## has a p element in scope
5926          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
5927            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
5928              !!!back-token;              !!!cp ('t344');
5929              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              !!!back-token; # <form>
5930              redo B;              $token = {type => END_TAG_TOKEN, tag_name => 'p',
5931            } elsif ({                        line => $token->{line}, column => $token->{column}};
5932                      table => 1, caption => 1, td => 1, th => 1,              next B;
5933                      button => 1, marquee => 1, object => 1, html => 1,            } elsif ($_->[1] & SCOPING_EL) {
5934                     }->{$_->[1]}) {              !!!cp ('t345');
5935              last INSCOPE;              last INSCOPE;
5936            }            }
5937          } # INSCOPE          } # INSCOPE
5938                        
5939          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
5940          if ($token->{tag_name} eq 'pre') {          if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {
5941              !!!nack ('t346.1');
5942            !!!next-token;            !!!next-token;
5943            if ($token->{type} == CHARACTER_TOKEN) {            if ($token->{type} == CHARACTER_TOKEN) {
5944              $token->{data} =~ s/^\x0A//;              $token->{data} =~ s/^\x0A//;
5945              unless (length $token->{data}) {              unless (length $token->{data}) {
5946                  !!!cp ('t346');
5947                !!!next-token;                !!!next-token;
5948                } else {
5949                  !!!cp ('t349');
5950              }              }
5951              } else {
5952                !!!cp ('t348');
5953            }            }
5954          } else {          } elsif ($token->{tag_name} eq 'form') {
5955              !!!cp ('t347.1');
5956              $self->{form_element} = $self->{open_elements}->[-1]->[0];
5957    
5958              !!!nack ('t347.2');
5959            !!!next-token;            !!!next-token;
5960          }          } elsif ($token->{tag_name} eq 'table') {
5961          redo B;            !!!cp ('t382');
5962        } elsif ($token->{tag_name} eq 'form') {            push @{$open_tables}, [$self->{open_elements}->[-1]->[0]];
5963          if (defined $self->{form_element}) {            
5964            !!!parse-error (type => 'in form:form');            $self->{insertion_mode} = IN_TABLE_IM;
5965            ## Ignore the token  
5966              !!!nack ('t382.1');
5967              !!!next-token;
5968            } elsif ($token->{tag_name} eq 'hr') {
5969              !!!cp ('t386');
5970              pop @{$self->{open_elements}};
5971            
5972              !!!nack ('t386.1');
5973            !!!next-token;            !!!next-token;
           redo B;  
5974          } else {          } else {
5975            ## has a p element in scope            !!!nack ('t347.1');
           INSCOPE: for (reverse @{$self->{open_elements}}) {  
             if ($_->[1] eq 'p') {  
               !!!back-token;  
               $token = {type => END_TAG_TOKEN, tag_name => 'p'};  
               redo B;  
             } elsif ({  
                       table => 1, caption => 1, td => 1, th => 1,  
                       button => 1, marquee => 1, object => 1, html => 1,  
                      }->{$_->[1]}) {  
               last INSCOPE;  
             }  
           } # INSCOPE  
               
           !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
           $self->{form_element} = $self->{open_elements}->[-1]->[0];  
5976            !!!next-token;            !!!next-token;
           redo B;  
5977          }          }
5978        } elsif ($token->{tag_name} eq 'li') {          next B;
5979          ## has a p element in scope        } elsif ({li => 1, dt => 1, dd => 1}->{$token->{tag_name}}) {
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => END_TAG_TOKEN, tag_name => 'p'};  
             redo B;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## Step 1  
         my $i = -1;  
         my $node = $self->{open_elements}->[$i];  
         LI: {  
           ## Step 2  
           if ($node->[1] eq 'li') {  
             if ($i != -1) {  
               !!!parse-error (type => 'end tag missing:'.  
                               $self->{open_elements}->[-1]->[1]);  
             }  
             splice @{$self->{open_elements}}, $i;  
             last LI;  
           }  
             
           ## Step 3  
           if (not $formatting_category->{$node->[1]} and  
               #not $phrasing_category->{$node->[1]} and  
               ($special_category->{$node->[1]} or  
                $scoping_category->{$node->[1]}) and  
               $node->[1] ne 'address' and $node->[1] ne 'div') {  
             last LI;  
           }  
             
           ## Step 4  
           $i--;  
           $node = $self->{open_elements}->[$i];  
           redo LI;  
         } # LI  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         !!!next-token;  
         redo B;  
       } elsif ($token->{tag_name} eq 'dd' or $token->{tag_name} eq 'dt') {  
5980          ## has a p element in scope          ## has a p element in scope
5981          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
5982            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
5983              !!!back-token;              !!!cp ('t353');
5984              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              !!!back-token; # <x>
5985              redo B;              $token = {type => END_TAG_TOKEN, tag_name => 'p',
5986            } elsif ({                        line => $token->{line}, column => $token->{column}};
5987                      table => 1, caption => 1, td => 1, th => 1,              next B;
5988                      button => 1, marquee => 1, object => 1, html => 1,            } elsif ($_->[1] & SCOPING_EL) {
5989                     }->{$_->[1]}) {              !!!cp ('t354');
5990              last INSCOPE;              last INSCOPE;
5991            }            }
5992          } # INSCOPE          } # INSCOPE
# Line 4477  sub _tree_construction_main ($) { Line 5994  sub _tree_construction_main ($) {
5994          ## Step 1          ## Step 1
5995          my $i = -1;          my $i = -1;
5996          my $node = $self->{open_elements}->[$i];          my $node = $self->{open_elements}->[$i];
5997            my $li_or_dtdd = {li => {li => 1},
5998                              dt => {dt => 1, dd => 1},
5999                              dd => {dt => 1, dd => 1}}->{$token->{tag_name}};
6000          LI: {          LI: {
6001            ## Step 2            ## Step 2
6002            if ($node->[1] eq 'dt' or $node->[1] eq 'dd') {            if ($li_or_dtdd->{$node->[0]->manakai_local_name}) {
6003              if ($i != -1) {              if ($i != -1) {
6004                !!!parse-error (type => 'end tag missing:'.                !!!cp ('t355');
6005                                $self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed',
6006                                  value => $self->{open_elements}->[-1]->[0]
6007                                      ->manakai_local_name,
6008                                  token => $token);
6009                } else {
6010                  !!!cp ('t356');
6011              }              }
6012              splice @{$self->{open_elements}}, $i;              splice @{$self->{open_elements}}, $i;
6013              last LI;              last LI;
6014              } else {
6015                !!!cp ('t357');
6016            }            }
6017                        
6018            ## Step 3            ## Step 3
6019            if (not $formatting_category->{$node->[1]} and            if (not ($node->[1] & FORMATTING_EL) and
6020                #not $phrasing_category->{$node->[1]} and                #not $phrasing_category->{$node->[1]} and
6021                ($special_category->{$node->[1]} or                ($node->[1] & SPECIAL_EL or
6022                 $scoping_category->{$node->[1]}) and                 $node->[1] & SCOPING_EL) and
6023                $node->[1] ne 'address' and $node->[1] ne 'div') {                not ($node->[1] & ADDRESS_EL) and
6024                  not ($node->[1] & DIV_EL)) {
6025                !!!cp ('t358');
6026              last LI;              last LI;
6027            }            }
6028                        
6029              !!!cp ('t359');
6030            ## Step 4            ## Step 4
6031            $i--;            $i--;
6032            $node = $self->{open_elements}->[$i];            $node = $self->{open_elements}->[$i];
6033            redo LI;            redo LI;
6034          } # LI          } # LI
6035                        
6036          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6037            !!!nack ('t359.1');
6038          !!!next-token;          !!!next-token;
6039          redo B;          next B;
6040        } elsif ($token->{tag_name} eq 'plaintext') {        } elsif ($token->{tag_name} eq 'plaintext') {
6041          ## has a p element in scope          ## has a p element in scope
6042          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6043            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6044              !!!back-token;              !!!cp ('t367');
6045              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              !!!back-token; # <plaintext>
6046              redo B;              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6047            } elsif ({                        line => $token->{line}, column => $token->{column}};
6048                      table => 1, caption => 1, td => 1, th => 1,              next B;
6049                      button => 1, marquee => 1, object => 1, html => 1,            } elsif ($_->[1] & SCOPING_EL) {
6050                     }->{$_->[1]}) {              !!!cp ('t368');
6051              last INSCOPE;              last INSCOPE;
6052            }            }
6053          } # INSCOPE          } # INSCOPE
6054                        
6055          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6056                        
6057          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
6058                        
6059            !!!nack ('t368.1');
6060          !!!next-token;          !!!next-token;
6061          redo B;          next B;
       } elsif ({  
                 h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
                }->{$token->{tag_name}}) {  
         ## has a p element in scope  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => END_TAG_TOKEN, tag_name => 'p'};  
             redo B;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## NOTE: See <http://html5.org/tools/web-apps-tracker?from=925&to=926>  
         ## has an element in scope  
         #my $i;  
         #INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
         #  my $node = $self->{open_elements}->[$_];  
         #  if ({  
         #       h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
         #      }->{$node->[1]}) {  
         #    $i = $_;  
         #    last INSCOPE;  
         #  } elsif ({  
         #            table => 1, caption => 1, td => 1, th => 1,  
         #            button => 1, marquee => 1, object => 1, html => 1,  
         #           }->{$node->[1]}) {  
         #    last INSCOPE;  
         #  }  
         #} # INSCOPE  
         #    
         #if (defined $i) {  
         #  !!! parse-error (type => 'in hn:hn');  
         #  splice @{$self->{open_elements}}, $i;  
         #}  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         !!!next-token;  
         redo B;  
6062        } elsif ($token->{tag_name} eq 'a') {        } elsif ($token->{tag_name} eq 'a') {
6063          AFE: for my $i (reverse 0..$#$active_formatting_elements) {          AFE: for my $i (reverse 0..$#$active_formatting_elements) {
6064            my $node = $active_formatting_elements->[$i];            my $node = $active_formatting_elements->[$i];
6065            if ($node->[1] eq 'a') {            if ($node->[1] & A_EL) {
6066              !!!parse-error (type => 'in a:a');              !!!cp ('t371');
6067                !!!parse-error (type => 'in a:a', token => $token);
6068                            
6069              !!!back-token;              !!!back-token; # <a>
6070              $token = {type => END_TAG_TOKEN, tag_name => 'a'};              $token = {type => END_TAG_TOKEN, tag_name => 'a',
6071              $formatting_end_tag->($token->{tag_name});                        line => $token->{line}, column => $token->{column}};
6072                $formatting_end_tag->($token);
6073                            
6074              AFE2: for (reverse 0..$#$active_formatting_elements) {              AFE2: for (reverse 0..$#$active_formatting_elements) {
6075                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
6076                    !!!cp ('t372');
6077                  splice @$active_formatting_elements, $_, 1;                  splice @$active_formatting_elements, $_, 1;
6078                  last AFE2;                  last AFE2;
6079                }                }
6080              } # AFE2              } # AFE2
6081              OE: for (reverse 0..$#{$self->{open_elements}}) {              OE: for (reverse 0..$#{$self->{open_elements}}) {
6082                if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {                if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {
6083                    !!!cp ('t373');
6084                  splice @{$self->{open_elements}}, $_, 1;                  splice @{$self->{open_elements}}, $_, 1;
6085                  last OE;                  last OE;
6086                }                }
6087              } # OE              } # OE
6088              last AFE;              last AFE;
6089            } elsif ($node->[0] eq '#marker') {            } elsif ($node->[0] eq '#marker') {
6090                !!!cp ('t374');
6091              last AFE;              last AFE;
6092            }            }
6093          } # AFE          } # AFE
6094                        
6095          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6096    
6097          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6098          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
6099    
6100            !!!nack ('t374.1');
6101          !!!next-token;          !!!next-token;
6102          redo B;          next B;
       } elsif ({  
                 b => 1, big => 1, em => 1, font => 1, i => 1,  
                 s => 1, small => 1, strile => 1,  
                 strong => 1, tt => 1, u => 1,  
                }->{$token->{tag_name}}) {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, $self->{open_elements}->[-1];  
           
         !!!next-token;  
         redo B;  
6103        } elsif ($token->{tag_name} eq 'nobr') {        } elsif ($token->{tag_name} eq 'nobr') {
6104          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6105    
6106          ## has a |nobr| element in scope          ## has a |nobr| element in scope
6107          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6108            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6109            if ($node->[1] eq 'nobr') {            if ($node->[1] & NOBR_EL) {
6110              !!!parse-error (type => 'not closed:nobr');              !!!cp ('t376');
6111              !!!back-token;              !!!parse-error (type => 'in nobr:nobr', token => $token);
6112              $token = {type => END_TAG_TOKEN, tag_name => 'nobr'};              !!!back-token; # <nobr>
6113              redo B;              $token = {type => END_TAG_TOKEN, tag_name => 'nobr',
6114            } elsif ({                        line => $token->{line}, column => $token->{column}};
6115                      table => 1, caption => 1, td => 1, th => 1,              next B;
6116                      button => 1, marquee => 1, object => 1, html => 1,            } elsif ($node->[1] & SCOPING_EL) {
6117                     }->{$node->[1]}) {              !!!cp ('t377');
6118              last INSCOPE;              last INSCOPE;
6119            }            }
6120          } # INSCOPE          } # INSCOPE
6121                    
6122          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6123          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
6124                    
6125            !!!nack ('t377.1');
6126          !!!next-token;          !!!next-token;
6127          redo B;          next B;
6128        } elsif ($token->{tag_name} eq 'button') {        } elsif ($token->{tag_name} eq 'button') {
6129          ## has a button element in scope          ## has a button element in scope
6130          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6131            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6132            if ($node->[1] eq 'button') {            if ($node->[1] & BUTTON_EL) {
6133              !!!parse-error (type => 'in button:button');              !!!cp ('t378');
6134              !!!back-token;              !!!parse-error (type => 'in button:button', token => $token);
6135              $token = {type => END_TAG_TOKEN, tag_name => 'button'};              !!!back-token; # <button>
6136              redo B;              $token = {type => END_TAG_TOKEN, tag_name => 'button',
6137            } elsif ({                        line => $token->{line}, column => $token->{column}};
6138                      table => 1, caption => 1, td => 1, th => 1,              next B;
6139                      button => 1, marquee => 1, object => 1, html => 1,            } elsif ($node->[1] & SCOPING_EL) {
6140                     }->{$node->[1]}) {              !!!cp ('t379');
6141              last INSCOPE;              last INSCOPE;
6142            }            }
6143          } # INSCOPE          } # INSCOPE
6144                        
6145          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6146                        
6147          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6148          push @$active_formatting_elements, ['#marker', ''];  
6149            ## TODO: associate with $self->{form_element} if defined
6150    
         !!!next-token;  
         redo B;  
       } elsif ($token->{tag_name} eq 'marquee' or  
                $token->{tag_name} eq 'object') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
6151          push @$active_formatting_elements, ['#marker', ''];          push @$active_formatting_elements, ['#marker', ''];
6152            
6153          !!!next-token;          !!!nack ('t379.1');
         redo B;  
       } elsif ($token->{tag_name} eq 'xmp') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
         $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);  
         redo B;  
       } elsif ($token->{tag_name} eq 'table') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => END_TAG_TOKEN, tag_name => 'p'};  
             redo B;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         $self->{insertion_mode} = IN_TABLE_IM;  
             
6154          !!!next-token;          !!!next-token;
6155          redo B;          next B;
6156        } elsif ({        } elsif ({
6157                  area => 1, basefont => 1, bgsound => 1, br => 1,                  xmp => 1,
6158                  embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,                  iframe => 1,
6159                  image => 1,                  noembed => 1,
6160                    noframes => 1,
6161                    noscript => 0, ## TODO: 1 if scripting is enabled
6162                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6163          if ($token->{tag_name} eq 'image') {          if ($token->{tag_name} eq 'xmp') {
6164            !!!parse-error (type => 'image');            !!!cp ('t381');
6165            $token->{tag_name} = 'img';            $reconstruct_active_formatting_elements->($insert_to_current);
6166            } else {
6167              !!!cp ('t399');
6168          }          }
6169            ## NOTE: There is an "as if in body" code clone.
6170          ## NOTE: There is an "as if <br>" code clone.          $parse_rcdata->(CDATA_CONTENT_MODEL);
6171          $reconstruct_active_formatting_elements->($insert_to_current);          next B;
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         pop @{$self->{open_elements}};  
           
         !!!next-token;  
         redo B;  
       } elsif ($token->{tag_name} eq 'hr') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => END_TAG_TOKEN, tag_name => 'p'};  
             redo B;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         pop @{$self->{open_elements}};  
             
         !!!next-token;  
         redo B;  
       } elsif ($token->{tag_name} eq 'input') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         ## TODO: associate with $self->{form_element} if defined  
         pop @{$self->{open_elements}};  
           
         !!!next-token;  
         redo B;  
6172        } elsif ($token->{tag_name} eq 'isindex') {        } elsif ($token->{tag_name} eq 'isindex') {
6173          !!!parse-error (type => 'isindex');          !!!parse-error (type => 'isindex', token => $token);
6174                    
6175          if (defined $self->{form_element}) {          if (defined $self->{form_element}) {
6176              !!!cp ('t389');
6177            ## Ignore the token            ## Ignore the token
6178              !!!nack ('t389'); ## NOTE: Not acknowledged.
6179            !!!next-token;            !!!next-token;
6180            redo B;            next B;
6181          } else {          } else {
6182            my $at = $token->{attributes};            my $at = $token->{attributes};
6183            my $form_attrs;            my $form_attrs;
# Line 4765  sub _tree_construction_main ($) { Line 6188  sub _tree_construction_main ($) {
6188            delete $at->{prompt};            delete $at->{prompt};
6189            my @tokens = (            my @tokens = (
6190                          {type => START_TAG_TOKEN, tag_name => 'form',                          {type => START_TAG_TOKEN, tag_name => 'form',
6191                           attributes => $form_attrs},                           attributes => $form_attrs,
6192                          {type => START_TAG_TOKEN, tag_name => 'hr'},                           line => $token->{line}, column => $token->{column}},
6193                          {type => START_TAG_TOKEN, tag_name => 'p'},                          {type => START_TAG_TOKEN, tag_name => 'hr',
6194                          {type => START_TAG_TOKEN, tag_name => 'label'},                           line => $token->{line}, column => $token->{column}},
6195                            {type => START_TAG_TOKEN, tag_name => 'p',
6196                             line => $token->{line}, column => $token->{column}},
6197                            {type => START_TAG_TOKEN, tag_name => 'label',
6198                             line => $token->{line}, column => $token->{column}},
6199                         );                         );
6200            if ($prompt_attr) {            if ($prompt_attr) {
6201              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value}};              !!!cp ('t390');
6202                push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value},
6203                               #line => $token->{line}, column => $token->{column},
6204                              };
6205            } else {            } else {
6206                !!!cp ('t391');
6207              push @tokens, {type => CHARACTER_TOKEN,              push @tokens, {type => CHARACTER_TOKEN,
6208                             data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD                             data => 'This is a searchable index. Insert your search keywords here: ',
6209                               #line => $token->{line}, column => $token->{column},
6210                              }; # SHOULD
6211              ## TODO: make this configurable              ## TODO: make this configurable
6212            }            }
6213            push @tokens,            push @tokens,
6214                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at},                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at,
6215                             line => $token->{line}, column => $token->{column}},
6216                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
6217                          {type => END_TAG_TOKEN, tag_name => 'label'},                          {type => END_TAG_TOKEN, tag_name => 'label',
6218                          {type => END_TAG_TOKEN, tag_name => 'p'},                           line => $token->{line}, column => $token->{column}},
6219                          {type => START_TAG_TOKEN, tag_name => 'hr'},                          {type => END_TAG_TOKEN, tag_name => 'p',
6220                          {type => END_TAG_TOKEN, tag_name => 'form'};                           line => $token->{line}, column => $token->{column}},
6221            $token = shift @tokens;                          {type => START_TAG_TOKEN, tag_name => 'hr',
6222                             line => $token->{line}, column => $token->{column}},
6223                            {type => END_TAG_TOKEN, tag_name => 'form',
6224                             line => $token->{line}, column => $token->{column}};
6225              !!!nack ('t391.1'); ## NOTE: Not acknowledged.
6226            !!!back-token (@tokens);            !!!back-token (@tokens);
6227            redo B;            !!!next-token;
6228              next B;
6229          }          }
6230        } elsif ($token->{tag_name} eq 'textarea') {        } elsif ($token->{tag_name} eq 'textarea') {
6231          my $tag_name = $token->{tag_name};          my $tag_name = $token->{tag_name};
6232          my $el;          my $el;
6233          !!!create-element ($el, $token->{tag_name}, $token->{attributes});          !!!create-element ($el, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
6234                    
6235          ## TODO: $self->{form_element} if defined          ## TODO: $self->{form_element} if defined
6236          $self->{content_model} = RCDATA_CONTENT_MODEL;          $self->{content_model} = RCDATA_CONTENT_MODEL;
# Line 4800  sub _tree_construction_main ($) { Line 6239  sub _tree_construction_main ($) {
6239          $insert->($el);          $insert->($el);
6240                    
6241          my $text = '';          my $text = '';
6242            !!!nack ('t392.1');
6243          !!!next-token;          !!!next-token;
6244          if ($token->{type} == CHARACTER_TOKEN) {          if ($token->{type} == CHARACTER_TOKEN) {
6245            $token->{data} =~ s/^\x0A//;            $token->{data} =~ s/^\x0A//;
6246            unless (length $token->{data}) {            unless (length $token->{data}) {
6247                !!!cp ('t392');
6248              !!!next-token;              !!!next-token;
6249              } else {
6250                !!!cp ('t393');
6251            }            }
6252            } else {
6253              !!!cp ('t394');
6254          }          }
6255          while ($token->{type} == CHARACTER_TOKEN) {          while ($token->{type} == CHARACTER_TOKEN) {
6256              !!!cp ('t395');
6257            $text .= $token->{data};            $text .= $token->{data};
6258            !!!next-token;            !!!next-token;
6259          }          }
6260          if (length $text) {          if (length $text) {
6261              !!!cp ('t396');
6262            $el->manakai_append_text ($text);            $el->manakai_append_text ($text);
6263          }          }
6264                    
# Line 4819  sub _tree_construction_main ($) { Line 6266  sub _tree_construction_main ($) {
6266                    
6267          if ($token->{type} == END_TAG_TOKEN and          if ($token->{type} == END_TAG_TOKEN and
6268              $token->{tag_name} eq $tag_name) {              $token->{tag_name} eq $tag_name) {
6269              !!!cp ('t397');
6270            ## Ignore the token            ## Ignore the token
6271          } else {          } else {
6272            !!!parse-error (type => 'in RCDATA:#'.$token->{type});            !!!cp ('t398');
6273              !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);
6274          }          }
6275          !!!next-token;          !!!next-token;
6276          redo B;          next B;
6277        } elsif ({        } elsif ($token->{tag_name} eq 'math' or
6278                  iframe => 1,                 $token->{tag_name} eq 'svg') {
                 noembed => 1,  
                 noframes => 1,  
                 noscript => 0, ## TODO: 1 if scripting is enabled  
                }->{$token->{tag_name}}) {  
         ## NOTE: There are two "as if in body" code clones.  
         $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);  
         redo B;  
       } elsif ($token->{tag_name} eq 'select') {  
6279          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6280                    
6281          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-f ($token->{tag_name} eq 'math' ? $MML_NS : $SVG_NS, $token);
6282                    
6283          $self->{insertion_mode} = IN_SELECT_IM;          if ($self->{self_closing}) {
6284              pop @{$self->{open_elements}};
6285              !!!ack ('t398.1');
6286            } else {
6287              !!!cp ('t398.2');
6288              $self->{insertion_mode} |= IN_FOREIGN_CONTENT_IM;
6289              ## NOTE: |<body><math><mi><svg>| -> "in foreign content" insertion
6290              ## mode, "in body" (not "in foreign content") secondary insertion
6291              ## mode, maybe.
6292            }
6293    
6294          !!!next-token;          !!!next-token;
6295          redo B;          next B;
6296        } elsif ({        } elsif ({
6297                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
6298                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
6299                  tbody => 1, td => 1, tfoot => 1, th => 1,                  tbody => 1, td => 1, tfoot => 1, th => 1,
6300                  thead => 1, tr => 1,                  thead => 1, tr => 1,
6301                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6302          !!!parse-error (type => 'in body:'.$token->{tag_name});          !!!cp ('t401');
6303            !!!parse-error (type => 'in body:'.$token->{tag_name}, token => $token);
6304          ## Ignore the token          ## Ignore the token
6305            !!!nack ('t401.1'); ## NOTE: |<col/>| or |<frame/>| here is an error.
6306          !!!next-token;          !!!next-token;
6307          redo B;          next B;
6308                    
6309          ## ISSUE: An issue on HTML5 new elements in the spec.          ## ISSUE: An issue on HTML5 new elements in the spec.
6310        } else {        } else {
6311            if ($token->{tag_name} eq 'image') {
6312              !!!cp ('t384');
6313              !!!parse-error (type => 'image', token => $token);
6314              $token->{tag_name} = 'img';
6315            } else {
6316              !!!cp ('t385');
6317            }
6318    
6319            ## NOTE: There is an "as if <br>" code clone.
6320          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6321                    
6322          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6323    
6324            if ({
6325                 applet => 1, marquee => 1, object => 1,
6326                }->{$token->{tag_name}}) {
6327              !!!cp ('t380');
6328              push @$active_formatting_elements, ['#marker', ''];
6329              !!!nack ('t380.1');
6330            } elsif ({
6331                      b => 1, big => 1, em => 1, font => 1, i => 1,
6332                      s => 1, small => 1, strile => 1,
6333                      strong => 1, tt => 1, u => 1,
6334                     }->{$token->{tag_name}}) {
6335              !!!cp ('t375');
6336              push @$active_formatting_elements, $self->{open_elements}->[-1];
6337              !!!nack ('t375.1');
6338            } elsif ($token->{tag_name} eq 'input') {
6339              !!!cp ('t388');
6340              ## TODO: associate with $self->{form_element} if defined
6341              pop @{$self->{open_elements}};
6342              !!!ack ('t388.2');
6343            } elsif ({
6344                      area => 1, basefont => 1, bgsound => 1, br => 1,
6345                      embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
6346                      #image => 1,
6347                     }->{$token->{tag_name}}) {
6348              !!!cp ('t388.1');
6349              pop @{$self->{open_elements}};
6350              !!!ack ('t388.3');
6351            } elsif ($token->{tag_name} eq 'select') {
6352              ## TODO: associate with $self->{form_element} if defined
6353            
6354              if ($self->{insertion_mode} & TABLE_IMS or
6355                  $self->{insertion_mode} & BODY_TABLE_IMS or
6356                  $self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
6357                !!!cp ('t400.1');
6358                $self->{insertion_mode} = IN_SELECT_IN_TABLE_IM;
6359              } else {
6360                !!!cp ('t400.2');
6361                $self->{insertion_mode} = IN_SELECT_IM;
6362              }
6363              !!!nack ('t400.3');
6364            } else {
6365              !!!nack ('t402');
6366            }
6367                    
6368          !!!next-token;          !!!next-token;
6369          redo B;          next B;
6370        }        }
6371      } elsif ($token->{type} == END_TAG_TOKEN) {      } elsif ($token->{type} == END_TAG_TOKEN) {
6372        if ($token->{tag_name} eq 'body') {        if ($token->{tag_name} eq 'body') {
6373          if (@{$self->{open_elements}} > 1 and          ## has a |body| element in scope
6374              $self->{open_elements}->[1]->[1] eq 'body') {          my $i;
6375            for (@{$self->{open_elements}}) {          INSCOPE: {
6376              unless ({            for (reverse @{$self->{open_elements}}) {
6377                         dd => 1, dt => 1, li => 1, p => 1, td => 1,              if ($_->[1] & BODY_EL) {
6378                         th => 1, tr => 1, body => 1, html => 1,                !!!cp ('t405');
6379                       tbody => 1, tfoot => 1, thead => 1,                $i = $_;
6380                      }->{$_->[1]}) {                last INSCOPE;
6381                !!!parse-error (type => 'not closed:'.$_->[1]);              } elsif ($_->[1] & SCOPING_EL) {
6382                  !!!cp ('t405.1');
6383                  last;
6384              }              }
6385            }            }
6386    
6387            $self->{insertion_mode} = AFTER_BODY_IM;            !!!parse-error (type => 'start tag not allowed',
6388                              value => $token->{tag_name}, token => $token);
6389              ## NOTE: Ignore the token.
6390            !!!next-token;            !!!next-token;
6391            redo B;            next B;
6392          } else {          } # INSCOPE
6393            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
6394            ## Ignore the token          for (@{$self->{open_elements}}) {
6395            !!!next-token;            unless ($_->[1] & ALL_END_TAG_OPTIONAL_EL) {
6396            redo B;              !!!cp ('t403');
6397                !!!parse-error (type => 'not closed',
6398                                value => $_->[0]->manakai_local_name,
6399                                token => $token);
6400                last;
6401              } else {
6402                !!!cp ('t404');
6403              }
6404          }          }
6405    
6406            $self->{insertion_mode} = AFTER_BODY_IM;
6407            !!!next-token;
6408            next B;
6409        } elsif ($token->{tag_name} eq 'html') {        } elsif ($token->{tag_name} eq 'html') {
6410          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
6411            ## up-to-date, though it has same effect as speced.
6412            if (@{$self->{open_elements}} > 1 and
6413                $self->{open_elements}->[1]->[1] & BODY_EL) {
6414            ## ISSUE: There is an issue in the spec.            ## ISSUE: There is an issue in the spec.
6415            if ($self->{open_elements}->[-1]->[1] ne 'body') {            unless ($self->{open_elements}->[-1]->[1] & BODY_EL) {
6416              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);              !!!cp ('t406');
6417                !!!parse-error (type => 'not closed',
6418                                value => $self->{open_elements}->[1]->[0]
6419                                    ->manakai_local_name,
6420                                token => $token);
6421              } else {
6422                !!!cp ('t407');
6423            }            }
6424            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
6425            ## reprocess            ## reprocess
6426            redo B;            next B;
6427          } else {          } else {
6428            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!cp ('t408');
6429              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6430            ## Ignore the token            ## Ignore the token
6431            !!!next-token;            !!!next-token;
6432            redo B;            next B;
6433          }          }
6434        } elsif ({        } elsif ({
6435                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
6436                  div => 1, dl => 1, fieldset => 1, listing => 1,                  div => 1, dl => 1, fieldset => 1, listing => 1,
6437                  menu => 1, ol => 1, pre => 1, ul => 1,                  menu => 1, ol => 1, pre => 1, ul => 1,
                 p => 1,  
6438                  dd => 1, dt => 1, li => 1,                  dd => 1, dt => 1, li => 1,
6439                  button => 1, marquee => 1, object => 1,                  applet => 1, button => 1, marquee => 1, object => 1,
6440                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6441          ## has an element in scope          ## has an element in scope
6442          my $i;          my $i;
6443          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6444            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6445            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6446              ## generate implied end tags              !!!cp ('t410');
             if ({  
                  dd => ($token->{tag_name} ne 'dd'),  
                  dt => ($token->{tag_name} ne 'dt'),  
                  li => ($token->{tag_name} ne 'li'),  
                  p => ($token->{tag_name} ne 'p'),  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot=> 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => END_TAG_TOKEN,  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               redo B;  
             }  
6447              $i = $_;              $i = $_;
6448              last INSCOPE unless $token->{tag_name} eq 'p';              last INSCOPE;
6449            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
6450                      table => 1, caption => 1, td => 1, th => 1,              !!!cp ('t411');
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6451              last INSCOPE;              last INSCOPE;
6452            }            }
6453          } # INSCOPE          } # INSCOPE
6454            
6455          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          unless (defined $i) { # has an element in scope
6456            if (defined $i) {            !!!cp ('t413');
6457              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6458            } else {
6459              ## Step 1. generate implied end tags
6460              while ({
6461                      dd => ($token->{tag_name} ne 'dd'),
6462                      dt => ($token->{tag_name} ne 'dt'),
6463                      li => ($token->{tag_name} ne 'li'),
6464                      p => 1,
6465                     }->{$self->{open_elements}->[-1]->[0]->manakai_local_name}) {
6466                !!!cp ('t409');
6467                pop @{$self->{open_elements}};
6468              }
6469    
6470              ## Step 2.
6471              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6472                      ne $token->{tag_name}) {
6473                !!!cp ('t412');
6474                !!!parse-error (type => 'not closed',
6475                                value => $self->{open_elements}->[-1]->[0]
6476                                    ->manakai_local_name,
6477                                token => $token);
6478            } else {            } else {
6479              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t414');
6480            }            }
6481          }  
6482                      ## Step 3.
         if (defined $i) {  
6483            splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
6484          } elsif ($token->{tag_name} eq 'p') {  
6485            ## As if <p>, then reprocess the current token            ## Step 4.
6486            my $el;            $clear_up_to_marker->()
6487            !!!create-element ($el, 'p');                if {
6488            $insert->($el);                  applet => 1, button => 1, marquee => 1, object => 1,
6489                  }->{$token->{tag_name}};
6490          }          }
         $clear_up_to_marker->()  
           if {  
             button => 1, marquee => 1, object => 1,  
           }->{$token->{tag_name}};  
6491          !!!next-token;          !!!next-token;
6492          redo B;          next B;
6493        } elsif ($token->{tag_name} eq 'form') {        } elsif ($token->{tag_name} eq 'form') {
6494            undef $self->{form_element};
6495    
6496          ## has an element in scope          ## has an element in scope
6497            my $i;
6498          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6499            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6500            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] & FORM_EL) {
6501              ## generate implied end tags              !!!cp ('t418');
6502              if ({              $i = $_;
                  dd => 1, dt => 1, li => 1, p => 1,  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot=> 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => END_TAG_TOKEN,  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               redo B;  
             }  
6503              last INSCOPE;              last INSCOPE;
6504            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
6505                      table => 1, caption => 1, td => 1, th => 1,              !!!cp ('t419');
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6506              last INSCOPE;              last INSCOPE;
6507            }            }
6508          } # INSCOPE          } # INSCOPE
6509            
6510          if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {          unless (defined $i) { # has an element in scope
6511            pop @{$self->{open_elements}};            !!!cp ('t421');
6512              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6513          } else {          } else {
6514            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            ## Step 1. generate implied end tags
6515              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
6516                !!!cp ('t417');
6517                pop @{$self->{open_elements}};
6518              }
6519              
6520              ## Step 2.
6521              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6522                      ne $token->{tag_name}) {
6523                !!!cp ('t417.1');
6524                !!!parse-error (type => 'not closed',
6525                                value => $self->{open_elements}->[-1]->[0]
6526                                    ->manakai_local_name,
6527                                token => $token);
6528              } else {
6529                !!!cp ('t420');
6530              }  
6531              
6532              ## Step 3.
6533              splice @{$self->{open_elements}}, $i;
6534          }          }
6535    
         undef $self->{form_element};  
6536          !!!next-token;          !!!next-token;
6537          redo B;          next B;
6538        } elsif ({        } elsif ({
6539                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
6540                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
# Line 5000  sub _tree_construction_main ($) { Line 6542  sub _tree_construction_main ($) {
6542          my $i;          my $i;
6543          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6544            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6545            if ({            if ($node->[1] & HEADING_EL) {
6546                 h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,              !!!cp ('t423');
               }->{$node->[1]}) {  
             ## generate implied end tags  
             if ({  
                  dd => 1, dt => 1, li => 1, p => 1,  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot=> 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => END_TAG_TOKEN,  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               redo B;  
             }  
6547              $i = $_;              $i = $_;
6548              last INSCOPE;              last INSCOPE;
6549            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
6550                      table => 1, caption => 1, td => 1, th => 1,              !!!cp ('t424');
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6551              last INSCOPE;              last INSCOPE;
6552            }            }
6553          } # INSCOPE          } # INSCOPE
6554            
6555          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          unless (defined $i) { # has an element in scope
6556            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            !!!cp ('t425.1');
6557              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6558            } else {
6559              ## Step 1. generate implied end tags
6560              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
6561                !!!cp ('t422');
6562                pop @{$self->{open_elements}};
6563              }
6564              
6565              ## Step 2.
6566              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6567                      ne $token->{tag_name}) {
6568                !!!cp ('t425');
6569                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6570              } else {
6571                !!!cp ('t426');
6572              }
6573    
6574              ## Step 3.
6575              splice @{$self->{open_elements}}, $i;
6576          }          }
6577                    
         splice @{$self->{open_elements}}, $i if defined $i;  
6578          !!!next-token;          !!!next-token;
6579          redo B;          next B;
6580          } elsif ($token->{tag_name} eq 'p') {
6581            ## has an element in scope
6582            my $i;
6583            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6584              my $node = $self->{open_elements}->[$_];
6585              if ($node->[1] & P_EL) {
6586                !!!cp ('t410.1');
6587                $i = $_;
6588                last INSCOPE;
6589              } elsif ($node->[1] & SCOPING_EL) {
6590                !!!cp ('t411.1');
6591                last INSCOPE;
6592              }
6593            } # INSCOPE
6594    
6595            if (defined $i) {
6596              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6597                      ne $token->{tag_name}) {
6598                !!!cp ('t412.1');
6599                !!!parse-error (type => 'not closed',
6600                                value => $self->{open_elements}->[-1]->[0]
6601                                    ->manakai_local_name,
6602                                token => $token);
6603              } else {
6604                !!!cp ('t414.1');
6605              }
6606    
6607              splice @{$self->{open_elements}}, $i;
6608            } else {
6609              !!!cp ('t413.1');
6610              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6611    
6612              !!!cp ('t415.1');
6613              ## As if <p>, then reprocess the current token
6614              my $el;
6615              !!!create-element ($el, $HTML_NS, 'p',, $token);
6616              $insert->($el);
6617              ## NOTE: Not inserted into |$self->{open_elements}|.
6618            }
6619    
6620            !!!next-token;
6621            next B;
6622        } elsif ({        } elsif ({
6623                  a => 1,                  a => 1,
6624                  b => 1, big => 1, em => 1, font => 1, i => 1,                  b => 1, big => 1, em => 1, font => 1, i => 1,
6625                  nobr => 1, s => 1, small => 1, strile => 1,                  nobr => 1, s => 1, small => 1, strile => 1,
6626                  strong => 1, tt => 1, u => 1,                  strong => 1, tt => 1, u => 1,
6627                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6628          $formatting_end_tag->($token->{tag_name});          !!!cp ('t427');
6629          redo B;          $formatting_end_tag->($token);
6630            next B;
6631        } elsif ($token->{tag_name} eq 'br') {        } elsif ($token->{tag_name} eq 'br') {
6632          !!!parse-error (type => 'unmatched end tag:br');          !!!cp ('t428');
6633            !!!parse-error (type => 'unmatched end tag:br', token => $token);
6634    
6635          ## As if <br>          ## As if <br>
6636          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6637                    
6638          my $el;          my $el;
6639          !!!create-element ($el, 'br');          !!!create-element ($el, $HTML_NS, 'br',, $token);
6640          $insert->($el);          $insert->($el);
6641                    
6642          ## Ignore the token.          ## Ignore the token.
6643          !!!next-token;          !!!next-token;
6644          redo B;          next B;
6645        } elsif ({        } elsif ({
6646                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
6647                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 5064  sub _tree_construction_main ($) { Line 6654  sub _tree_construction_main ($) {
6654                  table => 1, textarea => 1, wbr => 1,                  table => 1, textarea => 1, wbr => 1,
6655                  noscript => 0, ## TODO: if scripting is enabled                  noscript => 0, ## TODO: if scripting is enabled
6656                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6657          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!cp ('t429');
6658            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6659          ## Ignore the token          ## Ignore the token
6660          !!!next-token;          !!!next-token;
6661          redo B;          next B;
6662                    
6663          ## ISSUE: Issue on HTML5 new elements in spec          ## ISSUE: Issue on HTML5 new elements in spec
6664                    
# Line 5078  sub _tree_construction_main ($) { Line 6669  sub _tree_construction_main ($) {
6669    
6670          ## Step 2          ## Step 2
6671          S2: {          S2: {
6672            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6673              ## Step 1              ## Step 1
6674              ## generate implied end tags              ## generate implied end tags
6675              if ({              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
6676                   dd => 1, dt => 1, li => 1, p => 1,                !!!cp ('t430');
6677                   td => 1, th => 1, tr => 1,                ## ISSUE: Can this case be reached?
6678                   tbody => 1, tfoot => 1, thead => 1,                pop @{$self->{open_elements}};
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => END_TAG_TOKEN,  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               redo B;  
6679              }              }
6680                    
6681              ## Step 2              ## Step 2
6682              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6683                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                      ne $token->{tag_name}) {
6684                  !!!cp ('t431');
6685                  ## NOTE: <x><y></x>
6686                  !!!parse-error (type => 'not closed',
6687                                  value => $self->{open_elements}->[-1]->[0]
6688                                      ->manakai_local_name,
6689                                  token => $token);
6690                } else {
6691                  !!!cp ('t432');
6692              }              }
6693                            
6694              ## Step 3              ## Step 3
# Line 5104  sub _tree_construction_main ($) { Line 6698  sub _tree_construction_main ($) {
6698              last S2;              last S2;
6699            } else {            } else {
6700              ## Step 3              ## Step 3
6701              if (not $formatting_category->{$node->[1]} and              if (not ($node->[1] & FORMATTING_EL) and
6702                  #not $phrasing_category->{$node->[1]} and                  #not $phrasing_category->{$node->[1]} and
6703                  ($special_category->{$node->[1]} or                  ($node->[1] & SPECIAL_EL or
6704                   $scoping_category->{$node->[1]})) {                   $node->[1] & SCOPING_EL)) {
6705                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!cp ('t433');
6706                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6707                ## Ignore the token                ## Ignore the token
6708                !!!next-token;                !!!next-token;
6709                last S2;                last S2;
6710              }              }
6711    
6712                !!!cp ('t434');
6713            }            }
6714                        
6715            ## Step 4            ## Step 4
# Line 5122  sub _tree_construction_main ($) { Line 6719  sub _tree_construction_main ($) {
6719            ## Step 5;            ## Step 5;
6720            redo S2;            redo S2;
6721          } # S2          } # S2
6722          redo B;          next B;
6723        }        }
6724      }      }
6725      redo B;      next B;
6726      } continue { # B
6727        if ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
6728          ## NOTE: The code below is executed in cases where it does not have
6729          ## to be, but it it is harmless even in those cases.
6730          ## has an element in scope
6731          INSCOPE: {
6732            for (reverse 0..$#{$self->{open_elements}}) {
6733              my $node = $self->{open_elements}->[$_];
6734              if ($node->[1] & FOREIGN_EL) {
6735                last INSCOPE;
6736              } elsif ($node->[1] & SCOPING_EL) {
6737                last;
6738              }
6739            }
6740            
6741            ## NOTE: No foreign element in scope.
6742            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
6743          } # INSCOPE
6744        }
6745    } # B    } # B
6746    
   ## NOTE: The "trailing end" phase in HTML5 is split into  
   ## two insertion modes: "after html body" and "after html frameset".  
   ## NOTE: States in the main stage is preserved while  
   ## the parser stays in the trailing end phase. # MUST  
   
6747    ## Stop parsing # MUST    ## Stop parsing # MUST
6748        
6749    ## TODO: script stuffs    ## TODO: script stuffs
# Line 5144  sub set_inner_html ($$$) { Line 6755  sub set_inner_html ($$$) {
6755    my $s = \$_[0];    my $s = \$_[0];
6756    my $onerror = $_[1];    my $onerror = $_[1];
6757    
6758      ## ISSUE: Should {confident} be true?
6759    
6760    my $nt = $node->node_type;    my $nt = $node->node_type;
6761    if ($nt == 9) {    if ($nt == 9) {
6762      # MUST      # MUST
# Line 5172  sub set_inner_html ($$$) { Line 6785  sub set_inner_html ($$$) {
6785      my $p = $class->new;      my $p = $class->new;
6786      $p->{document} = $doc;      $p->{document} = $doc;
6787    
6788      ## Step 9 # MUST      ## Step 8 # MUST
6789      my $i = 0;      my $i = 0;
6790      my $line = 1;      $p->{line_prev} = $p->{line} = 1;
6791      my $column = 0;      $p->{column_prev} = $p->{column} = 0;
6792      $p->{set_next_input_character} = sub {      $p->{set_next_char} = sub {
6793        my $self = shift;        my $self = shift;
6794    
6795        pop @{$self->{prev_input_character}};        pop @{$self->{prev_char}};
6796        unshift @{$self->{prev_input_character}}, $self->{next_input_character};        unshift @{$self->{prev_char}}, $self->{next_char};
6797    
6798          $self->{next_char} = -1 and return if $i >= length $$s;
6799          $self->{next_char} = ord substr $$s, $i++, 1;
6800    
6801          ($p->{line_prev}, $p->{column_prev}) = ($p->{line}, $p->{column});
6802          $p->{column}++;
6803    
6804        $self->{next_input_character} = -1 and return if $i >= length $$s;        if ($self->{next_char} == 0x000A) { # LF
6805        $self->{next_input_character} = ord substr $$s, $i++, 1;          $p->{line}++;
6806        $column++;          $p->{column} = 0;
6807            !!!cp ('i1');
6808        if ($self->{next_input_character} == 0x000A) { # LF        } elsif ($self->{next_char} == 0x000D) { # CR
         $line++;  
         $column = 0;  
       } elsif ($self->{next_input_character} == 0x000D) { # CR  
6809          $i++ if substr ($$s, $i, 1) eq "\x0A";          $i++ if substr ($$s, $i, 1) eq "\x0A";
6810          $self->{next_input_character} = 0x000A; # LF # MUST          $self->{next_char} = 0x000A; # LF # MUST
6811          $line++;          $p->{line}++;
6812          $column = 0;          $p->{column} = 0;
6813        } elsif ($self->{next_input_character} > 0x10FFFF) {          !!!cp ('i2');
6814          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        } elsif ($self->{next_char} > 0x10FFFF) {
6815        } elsif ($self->{next_input_character} == 0x0000) { # NULL          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
6816            !!!cp ('i3');
6817          } elsif ($self->{next_char} == 0x0000) { # NULL
6818            !!!cp ('i4');
6819          !!!parse-error (type => 'NULL');          !!!parse-error (type => 'NULL');
6820          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
6821        }        }
6822      };      };
6823      $p->{prev_input_character} = [-1, -1, -1];      $p->{prev_char} = [-1, -1, -1];
6824      $p->{next_input_character} = -1;      $p->{next_char} = -1;
6825            
6826      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
6827        my (%opt) = @_;        my (%opt) = @_;
6828        warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";        my $line = $opt{line};
6829          my $column = $opt{column};
6830          if (defined $opt{token} and defined $opt{token}->{line}) {
6831            $line = $opt{token}->{line};
6832            $column = $opt{token}->{column};
6833          }
6834          warn "Parse error ($opt{type}) at line $line column $column\n";
6835      };      };
6836      $p->{parse_error} = sub {      $p->{parse_error} = sub {
6837        $ponerror->(@_, line => $line, column => $column);        $ponerror->(line => $p->{line}, column => $p->{column}, @_);
6838      };      };
6839            
6840      $p->_initialize_tokenizer;      $p->_initialize_tokenizer;
6841      $p->_initialize_tree_constructor;      $p->_initialize_tree_constructor;
6842    
6843      ## Step 2      ## Step 2
6844      my $node_ln = $node->local_name;      my $node_ln = $node->manakai_local_name;
6845      $p->{content_model} = {      $p->{content_model} = {
6846        title => RCDATA_CONTENT_MODEL,        title => RCDATA_CONTENT_MODEL,
6847        textarea => RCDATA_CONTENT_MODEL,        textarea => RCDATA_CONTENT_MODEL,
# Line 5233  sub set_inner_html ($$$) { Line 6858  sub set_inner_html ($$$) {
6858          unless defined $p->{content_model};          unless defined $p->{content_model};
6859          ## ISSUE: What is "the name of the element"? local name?          ## ISSUE: What is "the name of the element"? local name?
6860    
6861      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $el_category->{$node_ln}];
6862          ## TODO: Foreign element OK?
6863    
6864      ## Step 4      ## Step 3
6865      my $root = $doc->create_element_ns      my $root = $doc->create_element_ns
6866        ('http://www.w3.org/1999/xhtml', [undef, 'html']);        ('http://www.w3.org/1999/xhtml', [undef, 'html']);
6867    
6868      ## Step 5 # MUST      ## Step 4 # MUST
6869      $doc->append_child ($root);      $doc->append_child ($root);
6870    
6871      ## Step 6 # MUST      ## Step 5 # MUST
6872      push @{$p->{open_elements}}, [$root, 'html'];      push @{$p->{open_elements}}, [$root, $el_category->{html}];
6873    
6874      undef $p->{head_element};      undef $p->{head_element};
6875    
6876      ## Step 7 # MUST      ## Step 6 # MUST
6877      $p->_reset_insertion_mode;      $p->_reset_insertion_mode;
6878    
6879      ## Step 8 # MUST      ## Step 7 # MUST
6880      my $anode = $node;      my $anode = $node;
6881      AN: while (defined $anode) {      AN: while (defined $anode) {
6882        if ($anode->node_type == 1) {        if ($anode->node_type == 1) {
6883          my $nsuri = $anode->namespace_uri;          my $nsuri = $anode->namespace_uri;
6884          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {
6885            if ($anode->local_name eq 'form') { ## TODO: case?            if ($anode->manakai_local_name eq 'form') {
6886                !!!cp ('i5');
6887              $p->{form_element} = $anode;              $p->{form_element} = $anode;
6888              last AN;              last AN;
6889            }            }
# Line 5265  sub set_inner_html ($$$) { Line 6892  sub set_inner_html ($$$) {
6892        $anode = $anode->parent_node;        $anode = $anode->parent_node;
6893      } # AN      } # AN
6894            
6895      ## Step 3 # MUST      ## Step 9 # MUST
     ## Step 10 # MUST  
6896      {      {
6897        my $self = $p;        my $self = $p;
6898        !!!next-token;        !!!next-token;
6899      }      }
6900      $p->_tree_construction_main;      $p->_tree_construction_main;
6901    
6902      ## Step 11 # MUST      ## Step 10 # MUST
6903      my @cn = @{$node->child_nodes};      my @cn = @{$node->child_nodes};
6904      for (@cn) {      for (@cn) {
6905        $node->remove_child ($_);        $node->remove_child ($_);
6906      }      }
6907      ## ISSUE: mutation events? read-only?      ## ISSUE: mutation events? read-only?
6908    
6909      ## Step 12 # MUST      ## Step 11 # MUST
6910      @cn = @{$root->child_nodes};      @cn = @{$root->child_nodes};
6911      for (@cn) {      for (@cn) {
6912        $this_doc->adopt_node ($_);        $this_doc->adopt_node ($_);
# Line 5289  sub set_inner_html ($$$) { Line 6915  sub set_inner_html ($$$) {
6915      ## ISSUE: mutation events?      ## ISSUE: mutation events?
6916    
6917      $p->_terminate_tree_constructor;      $p->_terminate_tree_constructor;
6918    
6919        delete $p->{parse_error}; # delete loop
6920    } else {    } else {
6921      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";
6922    }    }
# Line 5296  sub set_inner_html ($$$) { Line 6924  sub set_inner_html ($$$) {
6924    
6925  } # tree construction stage  } # tree construction stage
6926    
6927  sub get_inner_html ($$$) {  package Whatpm::HTML::RestartParser;
6928    my (undef, $node, $on_error) = @_;  push our @ISA, 'Error';
   
   ## Step 1  
   my $s = '';  
   
   my $in_cdata;  
   my $parent = $node;  
   while (defined $parent) {  
     if ($parent->node_type == 1 and  
         $parent->namespace_uri eq 'http://www.w3.org/1999/xhtml' and  
         {  
           style => 1, script => 1, xmp => 1, iframe => 1,  
           noembed => 1, noframes => 1, noscript => 1,  
         }->{$parent->local_name}) { ## TODO: case thingy  
       $in_cdata = 1;  
     }  
     $parent = $parent->parent_node;  
   }  
   
   ## Step 2  
   my @node = @{$node->child_nodes};  
   C: while (@node) {  
     my $child = shift @node;  
     unless (ref $child) {  
       if ($child eq 'cdata-out') {  
         $in_cdata = 0;  
       } else {  
         $s .= $child; # end tag  
       }  
       next C;  
     }  
       
     my $nt = $child->node_type;  
     if ($nt == 1) { # Element  
       my $tag_name = $child->tag_name; ## TODO: manakai_tag_name  
       $s .= '<' . $tag_name;  
       ## NOTE: Non-HTML case:  
       ## <http://permalink.gmane.org/gmane.org.w3c.whatwg.discuss/11191>  
   
       my @attrs = @{$child->attributes}; # sort order MUST be stable  
       for my $attr (@attrs) { # order is implementation dependent  
         my $attr_name = $attr->name; ## TODO: manakai_name  
         $s .= ' ' . $attr_name . '="';  
         my $attr_value = $attr->value;  
         ## escape  
         $attr_value =~ s/&/&amp;/g;  
         $attr_value =~ s/</&lt;/g;  
         $attr_value =~ s/>/&gt;/g;  
         $attr_value =~ s/"/&quot;/g;  
         $s .= $attr_value . '"';  
       }  
       $s .= '>';  
         
       next C if {  
         area => 1, base => 1, basefont => 1, bgsound => 1,  
         br => 1, col => 1, embed => 1, frame => 1, hr => 1,  
         img => 1, input => 1, link => 1, meta => 1, param => 1,  
         spacer => 1, wbr => 1,  
       }->{$tag_name};  
   
       $s .= "\x0A" if $tag_name eq 'pre' or $tag_name eq 'textarea';  
   
       if (not $in_cdata and {  
         style => 1, script => 1, xmp => 1, iframe => 1,  
         noembed => 1, noframes => 1, noscript => 1,  
         plaintext => 1,  
       }->{$tag_name}) {  
         unshift @node, 'cdata-out';  
         $in_cdata = 1;  
       }  
   
       unshift @node, @{$child->child_nodes}, '</' . $tag_name . '>';  
     } elsif ($nt == 3 or $nt == 4) {  
       if ($in_cdata) {  
         $s .= $child->data;  
       } else {  
         my $value = $child->data;  
         $value =~ s/&/&amp;/g;  
         $value =~ s/</&lt;/g;  
         $value =~ s/>/&gt;/g;  
         $value =~ s/"/&quot;/g;  
         $s .= $value;  
       }  
     } elsif ($nt == 8) {  
       $s .= '<!--' . $child->data . '-->';  
     } elsif ($nt == 10) {  
       $s .= '<!DOCTYPE ' . $child->name . '>';  
     } elsif ($nt == 5) { # entrefs  
       push @node, @{$child->child_nodes};  
     } else {  
       $on_error->($child) if defined $on_error;  
     }  
     ## ISSUE: This code does not support PIs.  
   } # C  
     
   ## Step 3  
   return \$s;  
 } # get_inner_html  
6929    
6930  1;  1;
6931  # $Date$  # $Date$

Legend:
Removed from v.1.56  
changed lines
  Added in v.1.126

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24