/[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.61 by wakaba, Sun Nov 4 04:15:06 2007 UTC revision 1.125 by wakaba, Sat Apr 12 10:41:31 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?  sub A_EL () { 0b1 }
16    sub ADDRESS_EL () { 0b10 }
17  my $permitted_slash_tag_name = {  sub BODY_EL () { 0b100 }
18    base => 1,  sub BUTTON_EL () { 0b1000 }
19    link => 1,  sub CAPTION_EL () { 0b10000 }
20    meta => 1,  sub DD_EL () { 0b100000 }
21    hr => 1,  sub DIV_EL () { 0b1000000 }
22    br => 1,  sub DT_EL () { 0b10000000 }
23    img=> 1,  sub FORM_EL () { 0b100000000 }
24    embed => 1,  sub FORMATTING_EL () { 0b1000000000 }
25    param => 1,  sub FRAMESET_EL () { 0b10000000000 }
26    area => 1,  sub HEADING_EL () { 0b100000000000 }
27    col => 1,  sub HTML_EL () { 0b1000000000000 }
28    input => 1,  sub LI_EL () { 0b10000000000000 }
29    sub NOBR_EL () { 0b100000000000000 }
30    sub OPTION_EL () { 0b1000000000000000 }
31    sub OPTGROUP_EL () { 0b10000000000000000 }
32    sub P_EL () { 0b100000000000000000 }
33    sub SELECT_EL () { 0b1000000000000000000 }
34    sub TABLE_EL () { 0b10000000000000000000 }
35    sub TABLE_CELL_EL () { 0b100000000000000000000 }
36    sub TABLE_ROW_EL () { 0b1000000000000000000000 }
37    sub TABLE_ROW_GROUP_EL () { 0b10000000000000000000000 }
38    sub MISC_SCOPING_EL () { 0b100000000000000000000000 }
39    sub MISC_SPECIAL_EL () { 0b1000000000000000000000000 }
40    
41    sub TABLE_ROWS_EL () {
42      TABLE_EL |
43      TABLE_ROW_EL |
44      TABLE_ROW_GROUP_EL
45    }
46    
47    sub END_TAG_OPTIONAL_EL () {
48      DD_EL |
49      DT_EL |
50      LI_EL |
51      P_EL
52    }
53    
54    sub ALL_END_TAG_OPTIONAL_EL () {
55      END_TAG_OPTIONAL_EL |
56      BODY_EL |
57      HTML_EL |
58      TABLE_CELL_EL |
59      TABLE_ROW_EL |
60      TABLE_ROW_GROUP_EL
61    }
62    
63    sub SCOPING_EL () {
64      BUTTON_EL |
65      CAPTION_EL |
66      HTML_EL |
67      TABLE_EL |
68      TABLE_CELL_EL |
69      MISC_SCOPING_EL
70    }
71    
72    sub TABLE_SCOPING_EL () {
73      HTML_EL |
74      TABLE_EL
75    }
76    
77    sub TABLE_ROWS_SCOPING_EL () {
78      HTML_EL |
79      TABLE_ROW_GROUP_EL
80    }
81    
82    sub TABLE_ROW_SCOPING_EL () {
83      HTML_EL |
84      TABLE_ROW_EL
85    }
86    
87    sub SPECIAL_EL () {
88      ADDRESS_EL |
89      BODY_EL |
90      DIV_EL |
91      END_TAG_OPTIONAL_EL |
92      FORM_EL |
93      FRAMESET_EL |
94      HEADING_EL |
95      OPTION_EL |
96      OPTGROUP_EL |
97      SELECT_EL |
98      TABLE_ROW_EL |
99      TABLE_ROW_GROUP_EL |
100      MISC_SPECIAL_EL
101    }
102    
103    my $el_category = {
104      a => A_EL | FORMATTING_EL,
105      address => ADDRESS_EL,
106      applet => MISC_SCOPING_EL,
107      area => MISC_SPECIAL_EL,
108      b => FORMATTING_EL,
109      base => MISC_SPECIAL_EL,
110      basefont => MISC_SPECIAL_EL,
111      bgsound => MISC_SPECIAL_EL,
112      big => FORMATTING_EL,
113      blockquote => MISC_SPECIAL_EL,
114      body => BODY_EL,
115      br => MISC_SPECIAL_EL,
116      button => BUTTON_EL,
117      caption => CAPTION_EL,
118      center => MISC_SPECIAL_EL,
119      col => MISC_SPECIAL_EL,
120      colgroup => MISC_SPECIAL_EL,
121      dd => DD_EL,
122      dir => MISC_SPECIAL_EL,
123      div => DIV_EL,
124      dl => MISC_SPECIAL_EL,
125      dt => DT_EL,
126      em => FORMATTING_EL,
127      embed => MISC_SPECIAL_EL,
128      fieldset => MISC_SPECIAL_EL,
129      font => FORMATTING_EL,
130      form => FORM_EL,
131      frame => MISC_SPECIAL_EL,
132      frameset => FRAMESET_EL,
133      h1 => HEADING_EL,
134      h2 => HEADING_EL,
135      h3 => HEADING_EL,
136      h4 => HEADING_EL,
137      h5 => HEADING_EL,
138      h6 => HEADING_EL,
139      head => MISC_SPECIAL_EL,
140      hr => MISC_SPECIAL_EL,
141      html => HTML_EL,
142      i => FORMATTING_EL,
143      iframe => MISC_SPECIAL_EL,
144      img => MISC_SPECIAL_EL,
145      input => MISC_SPECIAL_EL,
146      isindex => MISC_SPECIAL_EL,
147      li => LI_EL,
148      link => MISC_SPECIAL_EL,
149      listing => MISC_SPECIAL_EL,
150      marquee => MISC_SCOPING_EL,
151      menu => MISC_SPECIAL_EL,
152      meta => MISC_SPECIAL_EL,
153      nobr => NOBR_EL | FORMATTING_EL,
154      noembed => MISC_SPECIAL_EL,
155      noframes => MISC_SPECIAL_EL,
156      noscript => MISC_SPECIAL_EL,
157      object => MISC_SCOPING_EL,
158      ol => MISC_SPECIAL_EL,
159      optgroup => OPTGROUP_EL,
160      option => OPTION_EL,
161      p => P_EL,
162      param => MISC_SPECIAL_EL,
163      plaintext => MISC_SPECIAL_EL,
164      pre => MISC_SPECIAL_EL,
165      s => FORMATTING_EL,
166      script => MISC_SPECIAL_EL,
167      select => SELECT_EL,
168      small => FORMATTING_EL,
169      spacer => MISC_SPECIAL_EL,
170      strike => FORMATTING_EL,
171      strong => FORMATTING_EL,
172      style => MISC_SPECIAL_EL,
173      table => TABLE_EL,
174      tbody => TABLE_ROW_GROUP_EL,
175      td => TABLE_CELL_EL,
176      textarea => MISC_SPECIAL_EL,
177      tfoot => TABLE_ROW_GROUP_EL,
178      th => TABLE_CELL_EL,
179      thead => TABLE_ROW_GROUP_EL,
180      title => MISC_SPECIAL_EL,
181      tr => TABLE_ROW_EL,
182      tt => FORMATTING_EL,
183      u => FORMATTING_EL,
184      ul => MISC_SPECIAL_EL,
185      wbr => MISC_SPECIAL_EL,
186  };  };
187    
188  my $c1_entity_char = {  my $c1_entity_char = {
# Line 62  my $c1_entity_char = { Line 220  my $c1_entity_char = {
220    0x9F => 0x0178,    0x9F => 0x0178,
221  }; # $c1_entity_char  }; # $c1_entity_char
222    
223  my $special_category = {  sub parse_byte_string ($$$$;$) {
224    address => 1, area => 1, base => 1, basefont => 1, bgsound => 1,    my $self = ref $_[0] ? shift : shift->new;
225    blockquote => 1, body => 1, br => 1, center => 1, col => 1, colgroup => 1,    my $charset = shift;
226    dd => 1, dir => 1, div => 1, dl => 1, dt => 1, embed => 1, fieldset => 1,    my $bytes_s = ref $_[0] ? $_[0] : \($_[0]);
227    form => 1, frame => 1, frameset => 1, h1 => 1, h2 => 1, h3 => 1,    my $s;
228    h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, iframe => 1, image => 1,    
229    img => 1, input => 1, isindex => 1, li => 1, link => 1, listing => 1,    if (defined $charset) {
230    menu => 1, meta => 1, noembed => 1, noframes => 1, noscript => 1,      require Encode; ## TODO: decode(utf8) don't delete BOM
231    ol => 1, optgroup => 1, option => 1, p => 1, param => 1, plaintext => 1,      $s = \ (Encode::decode ($charset, $$bytes_s));
232    pre => 1, script => 1, select => 1, spacer => 1, style => 1, tbody => 1,      $self->{input_encoding} = lc $charset; ## TODO: normalize name
233    textarea => 1, tfoot => 1, thead => 1, title => 1, tr => 1, ul => 1, wbr => 1,      $self->{confident} = 1;
234  };    } else {
235  my $scoping_category = {      ## TODO: Implement HTML5 detection algorithm
236    button => 1, caption => 1, html => 1, marquee => 1, object => 1,      require Whatpm::Charset::UniversalCharDet;
237    table => 1, td => 1, th => 1,      $charset = Whatpm::Charset::UniversalCharDet->detect_byte_string
238  };          (substr ($$bytes_s, 0, 1024));
239  my $formatting_category = {      $charset ||= 'windows-1252';
240    a => 1, b => 1, big => 1, em => 1, font => 1, i => 1, nobr => 1,      $s = \ (Encode::decode ($charset, $$bytes_s));
241    s => 1, small => 1, strile => 1, strong => 1, tt => 1, u => 1,      $self->{input_encoding} = $charset;
242  };      $self->{confident} = 0;
243  # $phrasing_category: all other elements    }
244    
245      $self->{change_encoding} = sub {
246        my $self = shift;
247        my $charset = lc shift;
248        my $token = shift;
249        ## TODO: if $charset is supported
250        ## TODO: normalize charset name
251    
252        ## "Change the encoding" algorithm:
253    
254        ## Step 1    
255        if ($charset eq 'utf-16') { ## ISSUE: UTF-16BE -> UTF-8? UTF-16LE -> UTF-8?
256          $charset = 'utf-8';
257        }
258    
259        ## Step 2
260        if (defined $self->{input_encoding} and
261            $self->{input_encoding} eq $charset) {
262          $self->{confident} = 1;
263          return;
264        }
265    
266        !!!parse-error (type => 'charset label detected:'.$self->{input_encoding}.
267            ':'.$charset, level => 'w', token => $token);
268    
269        ## Step 3
270        # if (can) {
271          ## change the encoding on the fly.
272          #$self->{confident} = 1;
273          #return;
274        # }
275    
276        ## Step 4
277        throw Whatpm::HTML::RestartParser (charset => $charset);
278      }; # $self->{change_encoding}
279    
280      my @args = @_; shift @args; # $s
281      my $return;
282      try {
283        $return = $self->parse_char_string ($s, @args);  
284      } catch Whatpm::HTML::RestartParser with {
285        my $charset = shift->{charset};
286        $s = \ (Encode::decode ($charset, $$bytes_s));    
287        $self->{input_encoding} = $charset; ## TODO: normalize
288        $self->{confident} = 1;
289        $return = $self->parse_char_string ($s, @args);
290      };
291      return $return;
292    } # parse_byte_string
293    
294    ## NOTE: HTML5 spec says that the encoding layer MUST NOT strip BOM
295    ## and the HTML layer MUST ignore it.  However, we does strip BOM in
296    ## the encoding layer and the HTML layer does not ignore any U+FEFF,
297    ## because the core part of our HTML parser expects a string of character,
298    ## not a string of bytes or code units or anything which might contain a BOM.
299    ## Therefore, any parser interface that accepts a string of bytes,
300    ## such as |parse_byte_string| in this module, must ensure that it does
301    ## strip the BOM and never strip any ZWNBSP.
302    
303    *parse_char_string = \&parse_string;
304    
305  sub parse_string ($$$;$) {  sub parse_string ($$$;$) {
306    my $self = shift->new;    my $self = ref $_[0] ? shift : shift->new;
307    my $s = \$_[0];    my $s = ref $_[0] ? $_[0] : \($_[0]);
308    $self->{document} = $_[1];    $self->{document} = $_[1];
309      @{$self->{document}->child_nodes} = ();
310    
311    ## NOTE: |set_inner_html| copies most of this method's code    ## NOTE: |set_inner_html| copies most of this method's code
312    
313      $self->{confident} = 1 unless exists $self->{confident};
314      $self->{document}->input_encoding ($self->{input_encoding})
315          if defined $self->{input_encoding};
316    
317    my $i = 0;    my $i = 0;
318    my $line = 1;    $self->{line_prev} = $self->{line} = 1;
319    my $column = 0;    $self->{column_prev} = $self->{column} = 0;
320    $self->{set_next_input_character} = sub {    $self->{set_next_char} = sub {
321      my $self = shift;      my $self = shift;
322    
323      pop @{$self->{prev_input_character}};      pop @{$self->{prev_char}};
324      unshift @{$self->{prev_input_character}}, $self->{next_input_character};      unshift @{$self->{prev_char}}, $self->{next_char};
325    
326      $self->{next_input_character} = -1 and return if $i >= length $$s;      $self->{next_char} = -1 and return if $i >= length $$s;
327      $self->{next_input_character} = ord substr $$s, $i++, 1;      $self->{next_char} = ord substr $$s, $i++, 1;
328      $column++;  
329        ($self->{line_prev}, $self->{column_prev})
330            = ($self->{line}, $self->{column});
331        $self->{column}++;
332            
333      if ($self->{next_input_character} == 0x000A) { # LF      if ($self->{next_char} == 0x000A) { # LF
334        $line++;        $self->{line}++;
335        $column = 0;        $self->{column} = 0;
336      } elsif ($self->{next_input_character} == 0x000D) { # CR      } elsif ($self->{next_char} == 0x000D) { # CR
337        $i++ if substr ($$s, $i, 1) eq "\x0A";        $i++ if substr ($$s, $i, 1) eq "\x0A";
338        $self->{next_input_character} = 0x000A; # LF # MUST        $self->{next_char} = 0x000A; # LF # MUST
339        $line++;        $self->{line}++;
340        $column = 0;        $self->{column} = 0;
341      } elsif ($self->{next_input_character} > 0x10FFFF) {      } elsif ($self->{next_char} > 0x10FFFF) {
342        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
343      } elsif ($self->{next_input_character} == 0x0000) { # NULL      } elsif ($self->{next_char} == 0x0000) { # NULL
344        !!!parse-error (type => 'NULL');        !!!parse-error (type => 'NULL');
345        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
346      }      }
347    };    };
348    $self->{prev_input_character} = [-1, -1, -1];    $self->{prev_char} = [-1, -1, -1];
349    $self->{next_input_character} = -1;    $self->{next_char} = -1;
350    
351    my $onerror = $_[2] || sub {    my $onerror = $_[2] || sub {
352      my (%opt) = @_;      my (%opt) = @_;
353      warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";      my $line = $opt{token} ? $opt{token}->{line} : $opt{line};
354        my $column = $opt{token} ? $opt{token}->{column} : $opt{column};
355        warn "Parse error ($opt{type}) at line $line column $column\n";
356    };    };
357    $self->{parse_error} = sub {    $self->{parse_error} = sub {
358      $onerror->(@_, line => $line, column => $column);      $onerror->(line => $self->{line}, column => $self->{column}, @_);
359    };    };
360    
361    $self->_initialize_tokenizer;    $self->_initialize_tokenizer;
# Line 135  sub parse_string ($$$;$) { Line 363  sub parse_string ($$$;$) {
363    $self->_construct_tree;    $self->_construct_tree;
364    $self->_terminate_tree_constructor;    $self->_terminate_tree_constructor;
365    
366      delete $self->{parse_error}; # remove loop
367    
368    return $self->{document};    return $self->{document};
369  } # parse_string  } # parse_string
370    
371  sub new ($) {  sub new ($) {
372    my $class = shift;    my $class = shift;
373    my $self = bless {}, $class;    my $self = bless {}, $class;
374    $self->{set_next_input_character} = sub {    $self->{set_next_char} = sub {
375      $self->{next_input_character} = -1;      $self->{next_char} = -1;
376    };    };
377    $self->{parse_error} = sub {    $self->{parse_error} = sub {
378      #      #
379    };    };
380      $self->{change_encoding} = sub {
381        # if ($_[0] is a supported encoding) {
382        #   run "change the encoding" algorithm;
383        #   throw Whatpm::HTML::RestartParser (charset => $new_encoding);
384        # }
385      };
386    $self->{application_cache_selection} = sub {    $self->{application_cache_selection} = sub {
387      #      #
388    };    };
# Line 195  sub DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUO Line 431  sub DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUO
431  sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE () { 30 }  sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE () { 30 }
432  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }
433  sub BOGUS_DOCTYPE_STATE () { 32 }  sub BOGUS_DOCTYPE_STATE () { 32 }
434    sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }
435    sub SELF_CLOSING_START_TAG_STATE () { 34 }
436    
437  sub DOCTYPE_TOKEN () { 1 }  sub DOCTYPE_TOKEN () { 1 }
438  sub COMMENT_TOKEN () { 2 }  sub COMMENT_TOKEN () { 2 }
# Line 211  sub TABLE_IMS ()      { 0b1000000 } Line 449  sub TABLE_IMS ()      { 0b1000000 }
449  sub ROW_IMS ()        { 0b10000000 }  sub ROW_IMS ()        { 0b10000000 }
450  sub BODY_AFTER_IMS () { 0b100000000 }  sub BODY_AFTER_IMS () { 0b100000000 }
451  sub FRAME_IMS ()      { 0b1000000000 }  sub FRAME_IMS ()      { 0b1000000000 }
452    sub SELECT_IMS ()     { 0b10000000000 }
453    
454    ## NOTE: "initial" and "before html" insertion modes have no constants.
455    
456    ## NOTE: "after after body" insertion mode.
457  sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }  sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }
458    
459    ## NOTE: "after after frameset" insertion mode.
460  sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }  sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }
461    
462  sub IN_HEAD_IM () { HEAD_IMS | 0b00 }  sub IN_HEAD_IM () { HEAD_IMS | 0b00 }
463  sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }  sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }
464  sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }  sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }
# Line 227  sub IN_TABLE_IM () { TABLE_IMS } Line 472  sub IN_TABLE_IM () { TABLE_IMS }
472  sub AFTER_BODY_IM () { BODY_AFTER_IMS }  sub AFTER_BODY_IM () { BODY_AFTER_IMS }
473  sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }  sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }
474  sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }  sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }
475  sub IN_SELECT_IM () { 0b01 }  sub IN_SELECT_IM () { SELECT_IMS | 0b01 }
476    sub IN_SELECT_IN_TABLE_IM () { SELECT_IMS | 0b10 }
477  sub IN_COLUMN_GROUP_IM () { 0b10 }  sub IN_COLUMN_GROUP_IM () { 0b10 }
478    
479  ## Implementations MUST act as if state machine in the spec  ## Implementations MUST act as if state machine in the spec
# Line 240  sub _initialize_tokenizer ($) { Line 486  sub _initialize_tokenizer ($) {
486    undef $self->{current_attribute};    undef $self->{current_attribute};
487    undef $self->{last_emitted_start_tag_name};    undef $self->{last_emitted_start_tag_name};
488    undef $self->{last_attribute_value_state};    undef $self->{last_attribute_value_state};
489      delete $self->{self_closing};
490    $self->{char} = [];    $self->{char} = [];
491    # $self->{next_input_character}    # $self->{next_char}
492    !!!next-input-character;    !!!next-input-character;
493    $self->{token} = [];    $self->{token} = [];
494    # $self->{escape}    # $self->{escape}
# Line 254  sub _initialize_tokenizer ($) { Line 501  sub _initialize_tokenizer ($) {
501  ##   ->{tag_name} (START_TAG_TOKEN, END_TAG_TOKEN)  ##   ->{tag_name} (START_TAG_TOKEN, END_TAG_TOKEN)
502  ##   ->{public_identifier} (DOCTYPE_TOKEN)  ##   ->{public_identifier} (DOCTYPE_TOKEN)
503  ##   ->{system_identifier} (DOCTYPE_TOKEN)  ##   ->{system_identifier} (DOCTYPE_TOKEN)
504  ##   ->{correct} == 1 or 0 (DOCTYPE_TOKEN)  ##   ->{quirks} == 1 or 0 (DOCTYPE_TOKEN): "force-quirks" flag
505  ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)  ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)
506    ##        ->{name}
507    ##        ->{value}
508    ##        ->{has_reference} == 1 or 0
509  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
510    ## NOTE: The "self-closing flag" is hold as |$self->{self_closing}|.
511    ##     |->{self_closing}| is used to save the value of |$self->{self_closing}|
512    ##     while the token is pushed back to the stack.
513    
514    ## ISSUE: "When a DOCTYPE token is created, its
515    ## <i>self-closing flag</i> must be unset (its other state is that it
516    ## be set), and its attributes list must be empty.": Wrong subject?
517    
518  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
519    
# Line 283  sub _initialize_tokenizer ($) { Line 540  sub _initialize_tokenizer ($) {
540    
541  sub _get_next_token ($) {  sub _get_next_token ($) {
542    my $self = shift;    my $self = shift;
543    
544      if ($self->{self_closing}) {
545        !!!parse-error (type => 'nestc', token => $self->{current_token});
546        ## NOTE: The |self_closing| flag is only set by start tag token.
547        ## In addition, when a start tag token is emitted, it is always set to
548        ## |current_token|.
549        delete $self->{self_closing};
550      }
551    
552    if (@{$self->{token}}) {    if (@{$self->{token}}) {
553        $self->{self_closing} = $self->{token}->[0]->{self_closing};
554      return shift @{$self->{token}};      return shift @{$self->{token}};
555    }    }
556    
557    A: {    A: {
558      if ($self->{state} == DATA_STATE) {      if ($self->{state} == DATA_STATE) {
559        if ($self->{next_input_character} == 0x0026) { # &        if ($self->{next_char} == 0x0026) { # &
560          if ($self->{content_model} & CM_ENTITY) { # PCDATA | RCDATA          if ($self->{content_model} & CM_ENTITY and # PCDATA | RCDATA
561                not $self->{escape}) {
562              !!!cp (1);
563            $self->{state} = ENTITY_DATA_STATE;            $self->{state} = ENTITY_DATA_STATE;
564            !!!next-input-character;            !!!next-input-character;
565            redo A;            redo A;
566          } else {          } else {
567              !!!cp (2);
568            #            #
569          }          }
570        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
571          if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA          if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
572            unless ($self->{escape}) {            unless ($self->{escape}) {
573              if ($self->{prev_input_character}->[0] == 0x002D and # -              if ($self->{prev_char}->[0] == 0x002D and # -
574                  $self->{prev_input_character}->[1] == 0x0021 and # !                  $self->{prev_char}->[1] == 0x0021 and # !
575                  $self->{prev_input_character}->[2] == 0x003C) { # <                  $self->{prev_char}->[2] == 0x003C) { # <
576                  !!!cp (3);
577                $self->{escape} = 1;                $self->{escape} = 1;
578                } else {
579                  !!!cp (4);
580              }              }
581              } else {
582                !!!cp (5);
583            }            }
584          }          }
585                    
586          #          #
587        } elsif ($self->{next_input_character} == 0x003C) { # <        } elsif ($self->{next_char} == 0x003C) { # <
588          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA
589              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA
590               not $self->{escape})) {               not $self->{escape})) {
591              !!!cp (6);
592            $self->{state} = TAG_OPEN_STATE;            $self->{state} = TAG_OPEN_STATE;
593            !!!next-input-character;            !!!next-input-character;
594            redo A;            redo A;
595          } else {          } else {
596              !!!cp (7);
597            #            #
598          }          }
599        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
600          if ($self->{escape} and          if ($self->{escape} and
601              ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA              ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA
602            if ($self->{prev_input_character}->[0] == 0x002D and # -            if ($self->{prev_char}->[0] == 0x002D and # -
603                $self->{prev_input_character}->[1] == 0x002D) { # -                $self->{prev_char}->[1] == 0x002D) { # -
604                !!!cp (8);
605              delete $self->{escape};              delete $self->{escape};
606              } else {
607                !!!cp (9);
608            }            }
609            } else {
610              !!!cp (10);
611          }          }
612                    
613          #          #
614        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
615          !!!emit ({type => END_OF_FILE_TOKEN});          !!!cp (11);
616            !!!emit ({type => END_OF_FILE_TOKEN,
617                      line => $self->{line}, column => $self->{column}});
618          last A; ## TODO: ok?          last A; ## TODO: ok?
619          } else {
620            !!!cp (12);
621        }        }
622        # Anything else        # Anything else
623        my $token = {type => CHARACTER_TOKEN,        my $token = {type => CHARACTER_TOKEN,
624                     data => chr $self->{next_input_character}};                     data => chr $self->{next_char},
625                       line => $self->{line}, column => $self->{column},
626                      };
627        ## Stay in the data state        ## Stay in the data state
628        !!!next-input-character;        !!!next-input-character;
629    
# Line 344  sub _get_next_token ($) { Line 632  sub _get_next_token ($) {
632        redo A;        redo A;
633      } elsif ($self->{state} == ENTITY_DATA_STATE) {      } elsif ($self->{state} == ENTITY_DATA_STATE) {
634        ## (cannot happen in CDATA state)        ## (cannot happen in CDATA state)
635    
636          my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
637                
638        my $token = $self->_tokenize_attempt_to_consume_an_entity (0);        my $token = $self->_tokenize_attempt_to_consume_an_entity (0, -1);
639    
640        $self->{state} = DATA_STATE;        $self->{state} = DATA_STATE;
641        # next-input-character is already done        # next-input-character is already done
642    
643        unless (defined $token) {        unless (defined $token) {
644          !!!emit ({type => CHARACTER_TOKEN, data => '&'});          !!!cp (13);
645            !!!emit ({type => CHARACTER_TOKEN, data => '&',
646                      line => $l, column => $c,
647                     });
648        } else {        } else {
649            !!!cp (14);
650          !!!emit ($token);          !!!emit ($token);
651        }        }
652    
653        redo A;        redo A;
654      } elsif ($self->{state} == TAG_OPEN_STATE) {      } elsif ($self->{state} == TAG_OPEN_STATE) {
655        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
656          if ($self->{next_input_character} == 0x002F) { # /          if ($self->{next_char} == 0x002F) { # /
657              !!!cp (15);
658            !!!next-input-character;            !!!next-input-character;
659            $self->{state} = CLOSE_TAG_OPEN_STATE;            $self->{state} = CLOSE_TAG_OPEN_STATE;
660            redo A;            redo A;
661          } else {          } else {
662              !!!cp (16);
663            ## reconsume            ## reconsume
664            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
665    
666            !!!emit ({type => CHARACTER_TOKEN, data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
667                        line => $self->{line_prev},
668                        column => $self->{column_prev},
669                       });
670    
671            redo A;            redo A;
672          }          }
673        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA
674          if ($self->{next_input_character} == 0x0021) { # !          if ($self->{next_char} == 0x0021) { # !
675              !!!cp (17);
676            $self->{state} = MARKUP_DECLARATION_OPEN_STATE;            $self->{state} = MARKUP_DECLARATION_OPEN_STATE;
677            !!!next-input-character;            !!!next-input-character;
678            redo A;            redo A;
679          } elsif ($self->{next_input_character} == 0x002F) { # /          } elsif ($self->{next_char} == 0x002F) { # /
680              !!!cp (18);
681            $self->{state} = CLOSE_TAG_OPEN_STATE;            $self->{state} = CLOSE_TAG_OPEN_STATE;
682            !!!next-input-character;            !!!next-input-character;
683            redo A;            redo A;
684          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_char} and
685                   $self->{next_input_character} <= 0x005A) { # A..Z                   $self->{next_char} <= 0x005A) { # A..Z
686              !!!cp (19);
687            $self->{current_token}            $self->{current_token}
688              = {type => START_TAG_TOKEN,              = {type => START_TAG_TOKEN,
689                 tag_name => chr ($self->{next_input_character} + 0x0020)};                 tag_name => chr ($self->{next_char} + 0x0020),
690                   line => $self->{line_prev},
691                   column => $self->{column_prev}};
692            $self->{state} = TAG_NAME_STATE;            $self->{state} = TAG_NAME_STATE;
693            !!!next-input-character;            !!!next-input-character;
694            redo A;            redo A;
695          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_char} and
696                   $self->{next_input_character} <= 0x007A) { # a..z                   $self->{next_char} <= 0x007A) { # a..z
697              !!!cp (20);
698            $self->{current_token} = {type => START_TAG_TOKEN,            $self->{current_token} = {type => START_TAG_TOKEN,
699                              tag_name => chr ($self->{next_input_character})};                                      tag_name => chr ($self->{next_char}),
700                                        line => $self->{line_prev},
701                                        column => $self->{column_prev}};
702            $self->{state} = TAG_NAME_STATE;            $self->{state} = TAG_NAME_STATE;
703            !!!next-input-character;            !!!next-input-character;
704            redo A;            redo A;
705          } elsif ($self->{next_input_character} == 0x003E) { # >          } elsif ($self->{next_char} == 0x003E) { # >
706            !!!parse-error (type => 'empty start tag');            !!!cp (21);
707              !!!parse-error (type => 'empty start tag',
708                              line => $self->{line_prev},
709                              column => $self->{column_prev});
710            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
711            !!!next-input-character;            !!!next-input-character;
712    
713            !!!emit ({type => CHARACTER_TOKEN, data => '<>'});            !!!emit ({type => CHARACTER_TOKEN, data => '<>',
714                        line => $self->{line_prev},
715                        column => $self->{column_prev},
716                       });
717    
718            redo A;            redo A;
719          } elsif ($self->{next_input_character} == 0x003F) { # ?          } elsif ($self->{next_char} == 0x003F) { # ?
720            !!!parse-error (type => 'pio');            !!!cp (22);
721              !!!parse-error (type => 'pio',
722                              line => $self->{line_prev},
723                              column => $self->{column_prev});
724            $self->{state} = BOGUS_COMMENT_STATE;            $self->{state} = BOGUS_COMMENT_STATE;
725            ## $self->{next_input_character} is intentionally left as is            $self->{current_token} = {type => COMMENT_TOKEN, data => '',
726                                        line => $self->{line_prev},
727                                        column => $self->{column_prev},
728                                       };
729              ## $self->{next_char} is intentionally left as is
730            redo A;            redo A;
731          } else {          } else {
732              !!!cp (23);
733            !!!parse-error (type => 'bare stago');            !!!parse-error (type => 'bare stago');
734            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
735            ## reconsume            ## reconsume
736    
737            !!!emit ({type => CHARACTER_TOKEN, data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
738                        line => $self->{line_prev},
739                        column => $self->{column_prev},
740                       });
741    
742            redo A;            redo A;
743          }          }
# Line 421  sub _get_next_token ($) { Line 745  sub _get_next_token ($) {
745          die "$0: $self->{content_model} in tag open";          die "$0: $self->{content_model} in tag open";
746        }        }
747      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
748          my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1); # "<"of"</"
749        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
750          if (defined $self->{last_emitted_start_tag_name}) {          if (defined $self->{last_emitted_start_tag_name}) {
751    
752            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>
753            my @next_char;            my @next_char;
754            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++) {
755              push @next_char, $self->{next_input_character};              push @next_char, $self->{next_char};
756              my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);              my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);
757              my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;              my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;
758              if ($self->{next_input_character} == $c or $self->{next_input_character} == $C) {              if ($self->{next_char} == $c or $self->{next_char} == $C) {
759                  !!!cp (24);
760                !!!next-input-character;                !!!next-input-character;
761                next TAGNAME;                next TAGNAME;
762              } else {              } else {
763                $self->{next_input_character} = shift @next_char; # reconsume                !!!cp (25);
764                  $self->{next_char} = shift @next_char; # reconsume
765                !!!back-next-input-character (@next_char);                !!!back-next-input-character (@next_char);
766                $self->{state} = DATA_STATE;                $self->{state} = DATA_STATE;
767    
768                !!!emit ({type => CHARACTER_TOKEN, data => '</'});                !!!emit ({type => CHARACTER_TOKEN, data => '</',
769                            line => $l, column => $c,
770                           });
771        
772                redo A;                redo A;
773              }              }
774            }            }
775            push @next_char, $self->{next_input_character};            push @next_char, $self->{next_char};
776                
777            unless ($self->{next_input_character} == 0x0009 or # HT            unless ($self->{next_char} == 0x0009 or # HT
778                    $self->{next_input_character} == 0x000A or # LF                    $self->{next_char} == 0x000A or # LF
779                    $self->{next_input_character} == 0x000B or # VT                    $self->{next_char} == 0x000B or # VT
780                    $self->{next_input_character} == 0x000C or # FF                    $self->{next_char} == 0x000C or # FF
781                    $self->{next_input_character} == 0x0020 or # SP                    $self->{next_char} == 0x0020 or # SP
782                    $self->{next_input_character} == 0x003E or # >                    $self->{next_char} == 0x003E or # >
783                    $self->{next_input_character} == 0x002F or # /                    $self->{next_char} == 0x002F or # /
784                    $self->{next_input_character} == -1) {                    $self->{next_char} == -1) {
785              $self->{next_input_character} = shift @next_char; # reconsume              !!!cp (26);
786                $self->{next_char} = shift @next_char; # reconsume
787              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
788              $self->{state} = DATA_STATE;              $self->{state} = DATA_STATE;
789              !!!emit ({type => CHARACTER_TOKEN, data => '</'});              !!!emit ({type => CHARACTER_TOKEN, data => '</',
790                          line => $l, column => $c,
791                         });
792              redo A;              redo A;
793            } else {            } else {
794              $self->{next_input_character} = shift @next_char;              !!!cp (27);
795                $self->{next_char} = shift @next_char;
796              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
797              # and consume...              # and consume...
798            }            }
799          } else {          } else {
800            ## No start tag token has ever been emitted            ## No start tag token has ever been emitted
801              !!!cp (28);
802            # next-input-character is already done            # next-input-character is already done
803            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
804            !!!emit ({type => CHARACTER_TOKEN, data => '</'});            !!!emit ({type => CHARACTER_TOKEN, data => '</',
805                        line => $l, column => $c,
806                       });
807            redo A;            redo A;
808          }          }
809        }        }
810                
811        if (0x0041 <= $self->{next_input_character} and        if (0x0041 <= $self->{next_char} and
812            $self->{next_input_character} <= 0x005A) { # A..Z            $self->{next_char} <= 0x005A) { # A..Z
813          $self->{current_token} = {type => END_TAG_TOKEN,          !!!cp (29);
814                            tag_name => chr ($self->{next_input_character} + 0x0020)};          $self->{current_token}
815                = {type => END_TAG_TOKEN,
816                   tag_name => chr ($self->{next_char} + 0x0020),
817                   line => $l, column => $c};
818          $self->{state} = TAG_NAME_STATE;          $self->{state} = TAG_NAME_STATE;
819          !!!next-input-character;          !!!next-input-character;
820          redo A;          redo A;
821        } elsif (0x0061 <= $self->{next_input_character} and        } elsif (0x0061 <= $self->{next_char} and
822                 $self->{next_input_character} <= 0x007A) { # a..z                 $self->{next_char} <= 0x007A) { # a..z
823            !!!cp (30);
824          $self->{current_token} = {type => END_TAG_TOKEN,          $self->{current_token} = {type => END_TAG_TOKEN,
825                            tag_name => chr ($self->{next_input_character})};                                    tag_name => chr ($self->{next_char}),
826                                      line => $l, column => $c};
827          $self->{state} = TAG_NAME_STATE;          $self->{state} = TAG_NAME_STATE;
828          !!!next-input-character;          !!!next-input-character;
829          redo A;          redo A;
830        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
831          !!!parse-error (type => 'empty end tag');          !!!cp (31);
832            !!!parse-error (type => 'empty end tag',
833                            line => $self->{line_prev}, ## "<" in "</>"
834                            column => $self->{column_prev} - 1);
835          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
836          !!!next-input-character;          !!!next-input-character;
837          redo A;          redo A;
838        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
839            !!!cp (32);
840          !!!parse-error (type => 'bare etago');          !!!parse-error (type => 'bare etago');
841          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
842          # reconsume          # reconsume
843    
844          !!!emit ({type => CHARACTER_TOKEN, data => '</'});          !!!emit ({type => CHARACTER_TOKEN, data => '</',
845                      line => $l, column => $c,
846                     });
847    
848          redo A;          redo A;
849        } else {        } else {
850            !!!cp (33);
851          !!!parse-error (type => 'bogus end tag');          !!!parse-error (type => 'bogus end tag');
852          $self->{state} = BOGUS_COMMENT_STATE;          $self->{state} = BOGUS_COMMENT_STATE;
853          ## $self->{next_input_character} is intentionally left as is          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
854                                      line => $self->{line_prev}, # "<" of "</"
855                                      column => $self->{column_prev} - 1,
856                                     };
857            ## $self->{next_char} is intentionally left as is
858          redo A;          redo A;
859        }        }
860      } elsif ($self->{state} == TAG_NAME_STATE) {      } elsif ($self->{state} == TAG_NAME_STATE) {
861        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
862            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
863            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
864            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
865            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
866            !!!cp (34);
867          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
868          !!!next-input-character;          !!!next-input-character;
869          redo A;          redo A;
870        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
871          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
872            $self->{current_token}->{first_start_tag}            !!!cp (35);
               = not defined $self->{last_emitted_start_tag_name};  
873            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
874          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
875            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
876            if ($self->{current_token}->{attributes}) {            #if ($self->{current_token}->{attributes}) {
877              !!!parse-error (type => 'end tag attribute');            #  ## NOTE: This should never be reached.
878            }            #  !!! cp (36);
879              #  !!! parse-error (type => 'end tag attribute');
880              #} else {
881                !!!cp (37);
882              #}
883          } else {          } else {
884            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
885          }          }
# Line 532  sub _get_next_token ($) { Line 889  sub _get_next_token ($) {
889          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
890    
891          redo A;          redo A;
892        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
893                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
894          $self->{current_token}->{tag_name} .= chr ($self->{next_input_character} + 0x0020);          !!!cp (38);
895            $self->{current_token}->{tag_name} .= chr ($self->{next_char} + 0x0020);
896            # start tag or end tag            # start tag or end tag
897          ## Stay in this state          ## Stay in this state
898          !!!next-input-character;          !!!next-input-character;
899          redo A;          redo A;
900        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
901          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
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 (39);
               = 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 state should never be reached.
909            }            #  !!! cp (40);
910              #  !!! parse-error (type => 'end tag attribute');
911              #} else {
912                !!!cp (41);
913              #}
914          } else {          } else {
915            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
916          }          }
# Line 559  sub _get_next_token ($) { Line 920  sub _get_next_token ($) {
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 ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
924            !!!cp (42);
925            $self->{state} = SELF_CLOSING_START_TAG_STATE;
926          !!!next-input-character;          !!!next-input-character;
         if ($self->{next_input_character} == 0x003E and # >  
             $self->{current_token}->{type} == START_TAG_TOKEN and  
             $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {  
           # permitted slash  
           #  
         } else {  
           !!!parse-error (type => 'nestc');  
         }  
         $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;  
         # next-input-character is already done  
927          redo A;          redo A;
928        } else {        } else {
929          $self->{current_token}->{tag_name} .= chr $self->{next_input_character};          !!!cp (44);
930            $self->{current_token}->{tag_name} .= chr $self->{next_char};
931            # start tag or end tag            # start tag or end tag
932          ## Stay in the state          ## Stay in the state
933          !!!next-input-character;          !!!next-input-character;
934          redo A;          redo A;
935        }        }
936      } elsif ($self->{state} == BEFORE_ATTRIBUTE_NAME_STATE) {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_NAME_STATE) {
937        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
938            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
939            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
940            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
941            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
942            !!!cp (45);
943          ## Stay in the state          ## Stay in the state
944          !!!next-input-character;          !!!next-input-character;
945          redo A;          redo A;
946        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
947          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
948            $self->{current_token}->{first_start_tag}            !!!cp (46);
               = not defined $self->{last_emitted_start_tag_name};  
949            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
950          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
951            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
952            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
953                !!!cp (47);
954              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
955              } else {
956                !!!cp (48);
957            }            }
958          } else {          } else {
959            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 607  sub _get_next_token ($) { Line 964  sub _get_next_token ($) {
964          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
965    
966          redo A;          redo A;
967        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
968                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
969          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          !!!cp (49);
970                                value => ''};          $self->{current_attribute}
971                = {name => chr ($self->{next_char} + 0x0020),
972                   value => '',
973                   line => $self->{line}, column => $self->{column}};
974          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
975          !!!next-input-character;          !!!next-input-character;
976          redo A;          redo A;
977        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
978            !!!cp (50);
979            $self->{state} = SELF_CLOSING_START_TAG_STATE;
980          !!!next-input-character;          !!!next-input-character;
         if ($self->{next_input_character} == 0x003E and # >  
             $self->{current_token}->{type} == START_TAG_TOKEN and  
             $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {  
           # permitted slash  
           #  
         } else {  
           !!!parse-error (type => 'nestc');  
         }  
         ## Stay in the state  
         # next-input-character is already done  
981          redo A;          redo A;
982        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
983          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
984          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
985            $self->{current_token}->{first_start_tag}            !!!cp (52);
               = not defined $self->{last_emitted_start_tag_name};  
986            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
987          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
988            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
989            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
990                !!!cp (53);
991              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
992              } else {
993                !!!cp (54);
994            }            }
995          } else {          } else {
996            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 648  sub _get_next_token ($) { Line 1002  sub _get_next_token ($) {
1002    
1003          redo A;          redo A;
1004        } else {        } else {
1005          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          if ({
1006                                value => ''};               0x0022 => 1, # "
1007                 0x0027 => 1, # '
1008                 0x003D => 1, # =
1009                }->{$self->{next_char}}) {
1010              !!!cp (55);
1011              !!!parse-error (type => 'bad attribute name');
1012            } else {
1013              !!!cp (56);
1014            }
1015            $self->{current_attribute}
1016                = {name => chr ($self->{next_char}),
1017                   value => '',
1018                   line => $self->{line}, column => $self->{column}};
1019          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1020          !!!next-input-character;          !!!next-input-character;
1021          redo A;          redo A;
# Line 658  sub _get_next_token ($) { Line 1024  sub _get_next_token ($) {
1024        my $before_leave = sub {        my $before_leave = sub {
1025          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
1026              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
1027            !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name});            !!!cp (57);
1028              !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name}, line => $self->{current_attribute}->{line}, column => $self->{current_attribute}->{column});
1029            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
1030          } else {          } else {
1031              !!!cp (58);
1032            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}
1033              = $self->{current_attribute};              = $self->{current_attribute};
1034          }          }
1035        }; # $before_leave        }; # $before_leave
1036    
1037        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1038            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1039            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1040            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1041            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1042            !!!cp (59);
1043          $before_leave->();          $before_leave->();
1044          $self->{state} = AFTER_ATTRIBUTE_NAME_STATE;          $self->{state} = AFTER_ATTRIBUTE_NAME_STATE;
1045          !!!next-input-character;          !!!next-input-character;
1046          redo A;          redo A;
1047        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_char} == 0x003D) { # =
1048            !!!cp (60);
1049          $before_leave->();          $before_leave->();
1050          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
1051          !!!next-input-character;          !!!next-input-character;
1052          redo A;          redo A;
1053        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1054          $before_leave->();          $before_leave->();
1055          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1056            $self->{current_token}->{first_start_tag}            !!!cp (61);
               = not defined $self->{last_emitted_start_tag_name};  
1057            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1058          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1059              !!!cp (62);
1060            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1061            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1062              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 700  sub _get_next_token ($) { Line 1070  sub _get_next_token ($) {
1070          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1071    
1072          redo A;          redo A;
1073        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1074                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1075          $self->{current_attribute}->{name} .= chr ($self->{next_input_character} + 0x0020);          !!!cp (63);
1076            $self->{current_attribute}->{name} .= chr ($self->{next_char} + 0x0020);
1077          ## Stay in the state          ## Stay in the state
1078          !!!next-input-character;          !!!next-input-character;
1079          redo A;          redo A;
1080        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1081            !!!cp (64);
1082          $before_leave->();          $before_leave->();
1083            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1084          !!!next-input-character;          !!!next-input-character;
         if ($self->{next_input_character} == 0x003E and # >  
             $self->{current_token}->{type} == START_TAG_TOKEN and  
             $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {  
           # permitted slash  
           #  
         } else {  
           !!!parse-error (type => 'nestc');  
         }  
         $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;  
         # next-input-character is already done  
1085          redo A;          redo A;
1086        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1087          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1088          $before_leave->();          $before_leave->();
1089          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1090            $self->{current_token}->{first_start_tag}            !!!cp (66);
               = not defined $self->{last_emitted_start_tag_name};  
1091            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1092          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1093            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1094            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1095                !!!cp (67);
1096              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1097              } else {
1098                ## NOTE: This state should never be reached.
1099                !!!cp (68);
1100            }            }
1101          } else {          } else {
1102            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 742  sub _get_next_token ($) { Line 1108  sub _get_next_token ($) {
1108    
1109          redo A;          redo A;
1110        } else {        } else {
1111          $self->{current_attribute}->{name} .= chr ($self->{next_input_character});          if ($self->{next_char} == 0x0022 or # "
1112                $self->{next_char} == 0x0027) { # '
1113              !!!cp (69);
1114              !!!parse-error (type => 'bad attribute name');
1115            } else {
1116              !!!cp (70);
1117            }
1118            $self->{current_attribute}->{name} .= chr ($self->{next_char});
1119          ## Stay in the state          ## Stay in the state
1120          !!!next-input-character;          !!!next-input-character;
1121          redo A;          redo A;
1122        }        }
1123      } elsif ($self->{state} == AFTER_ATTRIBUTE_NAME_STATE) {      } elsif ($self->{state} == AFTER_ATTRIBUTE_NAME_STATE) {
1124        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1125            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1126            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1127            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1128            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1129            !!!cp (71);
1130          ## Stay in the state          ## Stay in the state
1131          !!!next-input-character;          !!!next-input-character;
1132          redo A;          redo A;
1133        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_char} == 0x003D) { # =
1134            !!!cp (72);
1135          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
1136          !!!next-input-character;          !!!next-input-character;
1137          redo A;          redo A;
1138        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1139          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1140            $self->{current_token}->{first_start_tag}            !!!cp (73);
               = not defined $self->{last_emitted_start_tag_name};  
1141            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1142          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1143            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1144            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1145                !!!cp (74);
1146              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1147              } else {
1148                ## NOTE: This state should never be reached.
1149                !!!cp (75);
1150            }            }
1151          } else {          } else {
1152            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 779  sub _get_next_token ($) { Line 1157  sub _get_next_token ($) {
1157          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1158    
1159          redo A;          redo A;
1160        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1161                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1162          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          !!!cp (76);
1163                                value => ''};          $self->{current_attribute}
1164                = {name => chr ($self->{next_char} + 0x0020),
1165                   value => '',
1166                   line => $self->{line}, column => $self->{column}};
1167          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1168          !!!next-input-character;          !!!next-input-character;
1169          redo A;          redo A;
1170        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1171            !!!cp (77);
1172            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1173          !!!next-input-character;          !!!next-input-character;
         if ($self->{next_input_character} == 0x003E and # >  
             $self->{current_token}->{type} == START_TAG_TOKEN and  
             $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {  
           # permitted slash  
           #  
         } else {  
           !!!parse-error (type => 'nestc');  
           ## TODO: Different error type for <aa / bb> than <aa/>  
         }  
         $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;  
         # next-input-character is already done  
1174          redo A;          redo A;
1175        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1176          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1177          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1178            $self->{current_token}->{first_start_tag}            !!!cp (79);
               = not defined $self->{last_emitted_start_tag_name};  
1179            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1180          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1181            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1182            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1183                !!!cp (80);
1184              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1185              } else {
1186                ## NOTE: This state should never be reached.
1187                !!!cp (81);
1188            }            }
1189          } else {          } else {
1190            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 821  sub _get_next_token ($) { Line 1196  sub _get_next_token ($) {
1196    
1197          redo A;          redo A;
1198        } else {        } else {
1199          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          !!!cp (82);
1200                                value => ''};          $self->{current_attribute}
1201                = {name => chr ($self->{next_char}),
1202                   value => '',
1203                   line => $self->{line}, column => $self->{column}};
1204          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1205          !!!next-input-character;          !!!next-input-character;
1206          redo A;                  redo A;        
1207        }        }
1208      } elsif ($self->{state} == BEFORE_ATTRIBUTE_VALUE_STATE) {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_VALUE_STATE) {
1209        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1210            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1211            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1212            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1213            $self->{next_input_character} == 0x0020) { # SP                  $self->{next_char} == 0x0020) { # SP      
1214            !!!cp (83);
1215          ## Stay in the state          ## Stay in the state
1216          !!!next-input-character;          !!!next-input-character;
1217          redo A;          redo A;
1218        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
1219            !!!cp (84);
1220          $self->{state} = ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE;          $self->{state} = ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE;
1221          !!!next-input-character;          !!!next-input-character;
1222          redo A;          redo A;
1223        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1224            !!!cp (85);
1225          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1226          ## reconsume          ## reconsume
1227          redo A;          redo A;
1228        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
1229            !!!cp (86);
1230          $self->{state} = ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE;          $self->{state} = ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE;
1231          !!!next-input-character;          !!!next-input-character;
1232          redo A;          redo A;
1233        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1234          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1235            $self->{current_token}->{first_start_tag}            !!!cp (87);
               = not defined $self->{last_emitted_start_tag_name};  
1236            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1237          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1238            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1239            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1240                !!!cp (88);
1241              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1242              } else {
1243                ## NOTE: This state should never be reached.
1244                !!!cp (89);
1245            }            }
1246          } else {          } else {
1247            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 867  sub _get_next_token ($) { Line 1252  sub _get_next_token ($) {
1252          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1253    
1254          redo A;          redo A;
1255        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1256          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1257          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1258            $self->{current_token}->{first_start_tag}            !!!cp (90);
               = not defined $self->{last_emitted_start_tag_name};  
1259            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1260          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1261            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1262            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1263                !!!cp (91);
1264              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1265              } else {
1266                ## NOTE: This state should never be reached.
1267                !!!cp (92);
1268            }            }
1269          } else {          } else {
1270            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 888  sub _get_next_token ($) { Line 1276  sub _get_next_token ($) {
1276    
1277          redo A;          redo A;
1278        } else {        } else {
1279          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          if ($self->{next_char} == 0x003D) { # =
1280              !!!cp (93);
1281              !!!parse-error (type => 'bad attribute value');
1282            } else {
1283              !!!cp (94);
1284            }
1285            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1286          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1287          !!!next-input-character;          !!!next-input-character;
1288          redo A;          redo A;
1289        }        }
1290      } elsif ($self->{state} == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE) {      } elsif ($self->{state} == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE) {
1291        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
1292          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          !!!cp (95);
1293            $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1294          !!!next-input-character;          !!!next-input-character;
1295          redo A;          redo A;
1296        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1297            !!!cp (96);
1298          $self->{last_attribute_value_state} = $self->{state};          $self->{last_attribute_value_state} = $self->{state};
1299          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1300          !!!next-input-character;          !!!next-input-character;
1301          redo A;          redo A;
1302        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1303          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1304          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1305            $self->{current_token}->{first_start_tag}            !!!cp (97);
               = not defined $self->{last_emitted_start_tag_name};  
1306            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1307          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1308            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1309            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1310                !!!cp (98);
1311              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1312              } else {
1313                ## NOTE: This state should never be reached.
1314                !!!cp (99);
1315            }            }
1316          } else {          } else {
1317            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 924  sub _get_next_token ($) { Line 1323  sub _get_next_token ($) {
1323    
1324          redo A;          redo A;
1325        } else {        } else {
1326          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          !!!cp (100);
1327            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1328          ## Stay in the state          ## Stay in the state
1329          !!!next-input-character;          !!!next-input-character;
1330          redo A;          redo A;
1331        }        }
1332      } elsif ($self->{state} == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE) {      } elsif ($self->{state} == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE) {
1333        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
1334          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          !!!cp (101);
1335            $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1336          !!!next-input-character;          !!!next-input-character;
1337          redo A;          redo A;
1338        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1339            !!!cp (102);
1340          $self->{last_attribute_value_state} = $self->{state};          $self->{last_attribute_value_state} = $self->{state};
1341          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1342          !!!next-input-character;          !!!next-input-character;
1343          redo A;          redo A;
1344        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1345          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1346          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1347            $self->{current_token}->{first_start_tag}            !!!cp (103);
               = not defined $self->{last_emitted_start_tag_name};  
1348            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1349          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1350            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1351            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1352                !!!cp (104);
1353              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1354              } else {
1355                ## NOTE: This state should never be reached.
1356                !!!cp (105);
1357            }            }
1358          } else {          } else {
1359            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 960  sub _get_next_token ($) { Line 1365  sub _get_next_token ($) {
1365    
1366          redo A;          redo A;
1367        } else {        } else {
1368          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          !!!cp (106);
1369            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1370          ## Stay in the state          ## Stay in the state
1371          !!!next-input-character;          !!!next-input-character;
1372          redo A;          redo A;
1373        }        }
1374      } elsif ($self->{state} == ATTRIBUTE_VALUE_UNQUOTED_STATE) {      } elsif ($self->{state} == ATTRIBUTE_VALUE_UNQUOTED_STATE) {
1375        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1376            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1377            $self->{next_input_character} == 0x000B or # HT            $self->{next_char} == 0x000B or # HT
1378            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1379            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1380            !!!cp (107);
1381          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1382          !!!next-input-character;          !!!next-input-character;
1383          redo A;          redo A;
1384        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1385            !!!cp (108);
1386          $self->{last_attribute_value_state} = $self->{state};          $self->{last_attribute_value_state} = $self->{state};
1387          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1388          !!!next-input-character;          !!!next-input-character;
1389          redo A;          redo A;
1390        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1391          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1392            $self->{current_token}->{first_start_tag}            !!!cp (109);
               = not defined $self->{last_emitted_start_tag_name};  
1393            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1394          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1395            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1396            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1397                !!!cp (110);
1398              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1399              } else {
1400                ## NOTE: This state should never be reached.
1401                !!!cp (111);
1402            }            }
1403          } else {          } else {
1404            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 998  sub _get_next_token ($) { Line 1409  sub _get_next_token ($) {
1409          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1410    
1411          redo A;          redo A;
1412        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1413          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1414          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1415            $self->{current_token}->{first_start_tag}            !!!cp (112);
               = not defined $self->{last_emitted_start_tag_name};  
1416            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1417          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1418            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1419            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1420                !!!cp (113);
1421              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1422              } else {
1423                ## NOTE: This state should never be reached.
1424                !!!cp (114);
1425            }            }
1426          } else {          } else {
1427            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 1019  sub _get_next_token ($) { Line 1433  sub _get_next_token ($) {
1433    
1434          redo A;          redo A;
1435        } else {        } else {
1436          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          if ({
1437                 0x0022 => 1, # "
1438                 0x0027 => 1, # '
1439                 0x003D => 1, # =
1440                }->{$self->{next_char}}) {
1441              !!!cp (115);
1442              !!!parse-error (type => 'bad attribute value');
1443            } else {
1444              !!!cp (116);
1445            }
1446            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1447          ## Stay in the state          ## Stay in the state
1448          !!!next-input-character;          !!!next-input-character;
1449          redo A;          redo A;
1450        }        }
1451      } elsif ($self->{state} == ENTITY_IN_ATTRIBUTE_VALUE_STATE) {      } elsif ($self->{state} == ENTITY_IN_ATTRIBUTE_VALUE_STATE) {
1452        my $token = $self->_tokenize_attempt_to_consume_an_entity (1);        my $token = $self->_tokenize_attempt_to_consume_an_entity
1453              (1,
1454               $self->{last_attribute_value_state}
1455                 == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE ? 0x0022 : # "
1456               $self->{last_attribute_value_state}
1457                 == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE ? 0x0027 : # '
1458               -1);
1459    
1460        unless (defined $token) {        unless (defined $token) {
1461            !!!cp (117);
1462          $self->{current_attribute}->{value} .= '&';          $self->{current_attribute}->{value} .= '&';
1463        } else {        } else {
1464            !!!cp (118);
1465          $self->{current_attribute}->{value} .= $token->{data};          $self->{current_attribute}->{value} .= $token->{data};
1466            $self->{current_attribute}->{has_reference} = $token->{has_reference};
1467          ## 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"
1468        }        }
1469    
1470        $self->{state} = $self->{last_attribute_value_state};        $self->{state} = $self->{last_attribute_value_state};
1471        # next-input-character is already done        # next-input-character is already done
1472        redo A;        redo A;
1473        } elsif ($self->{state} == AFTER_ATTRIBUTE_VALUE_QUOTED_STATE) {
1474          if ($self->{next_char} == 0x0009 or # HT
1475              $self->{next_char} == 0x000A or # LF
1476              $self->{next_char} == 0x000B or # VT
1477              $self->{next_char} == 0x000C or # FF
1478              $self->{next_char} == 0x0020) { # SP
1479            !!!cp (118);
1480            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1481            !!!next-input-character;
1482            redo A;
1483          } elsif ($self->{next_char} == 0x003E) { # >
1484            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1485              !!!cp (119);
1486              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1487            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1488              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1489              if ($self->{current_token}->{attributes}) {
1490                !!!cp (120);
1491                !!!parse-error (type => 'end tag attribute');
1492              } else {
1493                ## NOTE: This state should never be reached.
1494                !!!cp (121);
1495              }
1496            } else {
1497              die "$0: $self->{current_token}->{type}: Unknown token type";
1498            }
1499            $self->{state} = DATA_STATE;
1500            !!!next-input-character;
1501    
1502            !!!emit ($self->{current_token}); # start tag or end tag
1503    
1504            redo A;
1505          } elsif ($self->{next_char} == 0x002F) { # /
1506            !!!cp (122);
1507            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1508            !!!next-input-character;
1509            redo A;
1510          } else {
1511            !!!cp ('124.1');
1512            !!!parse-error (type => 'no space between attributes');
1513            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1514            ## reconsume
1515            redo A;
1516          }
1517        } elsif ($self->{state} == SELF_CLOSING_START_TAG_STATE) {
1518          if ($self->{next_char} == 0x003E) { # >
1519            if ($self->{current_token}->{type} == END_TAG_TOKEN) {
1520              !!!cp ('124.2');
1521              !!!parse-error (type => 'nestc', token => $self->{current_token});
1522              ## TODO: Different type than slash in start tag
1523              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1524              if ($self->{current_token}->{attributes}) {
1525                !!!cp ('124.4');
1526                !!!parse-error (type => 'end tag attribute');
1527              } else {
1528                !!!cp ('124.5');
1529              }
1530              ## TODO: Test |<title></title/>|
1531            } else {
1532              !!!cp ('124.3');
1533              $self->{self_closing} = 1;
1534            }
1535    
1536            $self->{state} = DATA_STATE;
1537            !!!next-input-character;
1538    
1539            !!!emit ($self->{current_token}); # start tag or end tag
1540    
1541            redo A;
1542          } else {
1543            !!!cp ('124.4');
1544            !!!parse-error (type => 'nestc');
1545            ## TODO: This error type is wrong.
1546            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1547            ## Reconsume.
1548            redo A;
1549          }
1550      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
1551        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1552                
1553        my $token = {type => COMMENT_TOKEN, data => ''};        ## NOTE: Set by the previous state
1554          #my $token = {type => COMMENT_TOKEN, data => ''};
1555    
1556        BC: {        BC: {
1557          if ($self->{next_input_character} == 0x003E) { # >          if ($self->{next_char} == 0x003E) { # >
1558              !!!cp (124);
1559            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1560            !!!next-input-character;            !!!next-input-character;
1561    
1562            !!!emit ($token);            !!!emit ($self->{current_token}); # comment
1563    
1564            redo A;            redo A;
1565          } elsif ($self->{next_input_character} == -1) {          } elsif ($self->{next_char} == -1) {
1566              !!!cp (125);
1567            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1568            ## reconsume            ## reconsume
1569    
1570            !!!emit ($token);            !!!emit ($self->{current_token}); # comment
1571    
1572            redo A;            redo A;
1573          } else {          } else {
1574            $token->{data} .= chr ($self->{next_input_character});            !!!cp (126);
1575              $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
1576            !!!next-input-character;            !!!next-input-character;
1577            redo BC;            redo BC;
1578          }          }
1579        } # BC        } # BC
1580    
1581          die "$0: _get_next_token: unexpected case [BC]";
1582      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
1583        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1584    
1585          my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1);
1586    
1587        my @next_char;        my @next_char;
1588        push @next_char, $self->{next_input_character};        push @next_char, $self->{next_char};
1589                
1590        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
1591          !!!next-input-character;          !!!next-input-character;
1592          push @next_char, $self->{next_input_character};          push @next_char, $self->{next_char};
1593          if ($self->{next_input_character} == 0x002D) { # -          if ($self->{next_char} == 0x002D) { # -
1594            $self->{current_token} = {type => COMMENT_TOKEN, data => ''};            !!!cp (127);
1595              $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1596                                        line => $l, column => $c,
1597                                       };
1598            $self->{state} = COMMENT_START_STATE;            $self->{state} = COMMENT_START_STATE;
1599            !!!next-input-character;            !!!next-input-character;
1600            redo A;            redo A;
1601            } else {
1602              !!!cp (128);
1603          }          }
1604        } elsif ($self->{next_input_character} == 0x0044 or # D        } elsif ($self->{next_char} == 0x0044 or # D
1605                 $self->{next_input_character} == 0x0064) { # d                 $self->{next_char} == 0x0064) { # d
1606          !!!next-input-character;          !!!next-input-character;
1607          push @next_char, $self->{next_input_character};          push @next_char, $self->{next_char};
1608          if ($self->{next_input_character} == 0x004F or # O          if ($self->{next_char} == 0x004F or # O
1609              $self->{next_input_character} == 0x006F) { # o              $self->{next_char} == 0x006F) { # o
1610            !!!next-input-character;            !!!next-input-character;
1611            push @next_char, $self->{next_input_character};            push @next_char, $self->{next_char};
1612            if ($self->{next_input_character} == 0x0043 or # C            if ($self->{next_char} == 0x0043 or # C
1613                $self->{next_input_character} == 0x0063) { # c                $self->{next_char} == 0x0063) { # c
1614              !!!next-input-character;              !!!next-input-character;
1615              push @next_char, $self->{next_input_character};              push @next_char, $self->{next_char};
1616              if ($self->{next_input_character} == 0x0054 or # T              if ($self->{next_char} == 0x0054 or # T
1617                  $self->{next_input_character} == 0x0074) { # t                  $self->{next_char} == 0x0074) { # t
1618                !!!next-input-character;                !!!next-input-character;
1619                push @next_char, $self->{next_input_character};                push @next_char, $self->{next_char};
1620                if ($self->{next_input_character} == 0x0059 or # Y                if ($self->{next_char} == 0x0059 or # Y
1621                    $self->{next_input_character} == 0x0079) { # y                    $self->{next_char} == 0x0079) { # y
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} == 0x0050 or # P                  if ($self->{next_char} == 0x0050 or # P
1625                      $self->{next_input_character} == 0x0070) { # p                      $self->{next_char} == 0x0070) { # p
1626                    !!!next-input-character;                    !!!next-input-character;
1627                    push @next_char, $self->{next_input_character};                    push @next_char, $self->{next_char};
1628                    if ($self->{next_input_character} == 0x0045 or # E                    if ($self->{next_char} == 0x0045 or # E
1629                        $self->{next_input_character} == 0x0065) { # e                        $self->{next_char} == 0x0065) { # e
1630                      ## ISSUE: What a stupid code this is!                      !!!cp (129);
1631                        ## TODO: What a stupid code this is!
1632                      $self->{state} = DOCTYPE_STATE;                      $self->{state} = DOCTYPE_STATE;
1633                        $self->{current_token} = {type => DOCTYPE_TOKEN,
1634                                                  quirks => 1,
1635                                                  line => $l, column => $c,
1636                                                 };
1637                      !!!next-input-character;                      !!!next-input-character;
1638                      redo A;                      redo A;
1639                      } else {
1640                        !!!cp (130);
1641                    }                    }
1642                    } else {
1643                      !!!cp (131);
1644                  }                  }
1645                  } else {
1646                    !!!cp (132);
1647                }                }
1648                } else {
1649                  !!!cp (133);
1650              }              }
1651              } else {
1652                !!!cp (134);
1653            }            }
1654            } else {
1655              !!!cp (135);
1656          }          }
1657          } else {
1658            !!!cp (136);
1659        }        }
1660    
1661        !!!parse-error (type => 'bogus comment');        !!!parse-error (type => 'bogus comment');
1662        $self->{next_input_character} = shift @next_char;        $self->{next_char} = shift @next_char;
1663        !!!back-next-input-character (@next_char);        !!!back-next-input-character (@next_char);
1664        $self->{state} = BOGUS_COMMENT_STATE;        $self->{state} = BOGUS_COMMENT_STATE;
1665          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1666                                    line => $l, column => $c,
1667                                   };
1668        redo A;        redo A;
1669                
1670        ## ISSUE: typos in spec: chacacters, is is a parse error        ## ISSUE: typos in spec: chacacters, is is a parse error
1671        ## 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?
1672      } elsif ($self->{state} == COMMENT_START_STATE) {      } elsif ($self->{state} == COMMENT_START_STATE) {
1673        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
1674            !!!cp (137);
1675          $self->{state} = COMMENT_START_DASH_STATE;          $self->{state} = COMMENT_START_DASH_STATE;
1676          !!!next-input-character;          !!!next-input-character;
1677          redo A;          redo A;
1678        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1679            !!!cp (138);
1680          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
1681          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1682          !!!next-input-character;          !!!next-input-character;
# Line 1137  sub _get_next_token ($) { Line 1684  sub _get_next_token ($) {
1684          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
1685    
1686          redo A;          redo A;
1687        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1688            !!!cp (139);
1689          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1690          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1691          ## reconsume          ## reconsume
# Line 1146  sub _get_next_token ($) { Line 1694  sub _get_next_token ($) {
1694    
1695          redo A;          redo A;
1696        } else {        } else {
1697            !!!cp (140);
1698          $self->{current_token}->{data} # comment          $self->{current_token}->{data} # comment
1699              .= chr ($self->{next_input_character});              .= chr ($self->{next_char});
1700          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
1701          !!!next-input-character;          !!!next-input-character;
1702          redo A;          redo A;
1703        }        }
1704      } elsif ($self->{state} == COMMENT_START_DASH_STATE) {      } elsif ($self->{state} == COMMENT_START_DASH_STATE) {
1705        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
1706            !!!cp (141);
1707          $self->{state} = COMMENT_END_STATE;          $self->{state} = COMMENT_END_STATE;
1708          !!!next-input-character;          !!!next-input-character;
1709          redo A;          redo A;
1710        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1711            !!!cp (142);
1712          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
1713          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1714          !!!next-input-character;          !!!next-input-character;
# Line 1165  sub _get_next_token ($) { Line 1716  sub _get_next_token ($) {
1716          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
1717    
1718          redo A;          redo A;
1719        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1720            !!!cp (143);
1721          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1722          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1723          ## reconsume          ## reconsume
# Line 1174  sub _get_next_token ($) { Line 1726  sub _get_next_token ($) {
1726    
1727          redo A;          redo A;
1728        } else {        } else {
1729            !!!cp (144);
1730          $self->{current_token}->{data} # comment          $self->{current_token}->{data} # comment
1731              .= '-' . chr ($self->{next_input_character});              .= '-' . chr ($self->{next_char});
1732          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
1733          !!!next-input-character;          !!!next-input-character;
1734          redo A;          redo A;
1735        }        }
1736      } elsif ($self->{state} == COMMENT_STATE) {      } elsif ($self->{state} == COMMENT_STATE) {
1737        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
1738            !!!cp (145);
1739          $self->{state} = COMMENT_END_DASH_STATE;          $self->{state} = COMMENT_END_DASH_STATE;
1740          !!!next-input-character;          !!!next-input-character;
1741          redo A;          redo A;
1742        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1743            !!!cp (146);
1744          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1745          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1746          ## reconsume          ## reconsume
# Line 1194  sub _get_next_token ($) { Line 1749  sub _get_next_token ($) {
1749    
1750          redo A;          redo A;
1751        } else {        } else {
1752          $self->{current_token}->{data} .= chr ($self->{next_input_character}); # comment          !!!cp (147);
1753            $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
1754          ## Stay in the state          ## Stay in the state
1755          !!!next-input-character;          !!!next-input-character;
1756          redo A;          redo A;
1757        }        }
1758      } elsif ($self->{state} == COMMENT_END_DASH_STATE) {      } elsif ($self->{state} == COMMENT_END_DASH_STATE) {
1759        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
1760            !!!cp (148);
1761          $self->{state} = COMMENT_END_STATE;          $self->{state} = COMMENT_END_STATE;
1762          !!!next-input-character;          !!!next-input-character;
1763          redo A;          redo A;
1764        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1765            !!!cp (149);
1766          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1767          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1768          ## reconsume          ## reconsume
# Line 1213  sub _get_next_token ($) { Line 1771  sub _get_next_token ($) {
1771    
1772          redo A;          redo A;
1773        } else {        } else {
1774          $self->{current_token}->{data} .= '-' . chr ($self->{next_input_character}); # comment          !!!cp (150);
1775            $self->{current_token}->{data} .= '-' . chr ($self->{next_char}); # comment
1776          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
1777          !!!next-input-character;          !!!next-input-character;
1778          redo A;          redo A;
1779        }        }
1780      } elsif ($self->{state} == COMMENT_END_STATE) {      } elsif ($self->{state} == COMMENT_END_STATE) {
1781        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_char} == 0x003E) { # >
1782            !!!cp (151);
1783          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1784          !!!next-input-character;          !!!next-input-character;
1785    
1786          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
1787    
1788          redo A;          redo A;
1789        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
1790          !!!parse-error (type => 'dash in comment');          !!!cp (152);
1791            !!!parse-error (type => 'dash in comment',
1792                            line => $self->{line_prev},
1793                            column => $self->{column_prev});
1794          $self->{current_token}->{data} .= '-'; # comment          $self->{current_token}->{data} .= '-'; # comment
1795          ## Stay in the state          ## Stay in the state
1796          !!!next-input-character;          !!!next-input-character;
1797          redo A;          redo A;
1798        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1799            !!!cp (153);
1800          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1801          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1802          ## reconsume          ## reconsume
# Line 1241  sub _get_next_token ($) { Line 1805  sub _get_next_token ($) {
1805    
1806          redo A;          redo A;
1807        } else {        } else {
1808          !!!parse-error (type => 'dash in comment');          !!!cp (154);
1809          $self->{current_token}->{data} .= '--' . chr ($self->{next_input_character}); # comment          !!!parse-error (type => 'dash in comment',
1810                            line => $self->{line_prev},
1811                            column => $self->{column_prev});
1812            $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment
1813          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
1814          !!!next-input-character;          !!!next-input-character;
1815          redo A;          redo A;
1816        }        }
1817      } elsif ($self->{state} == DOCTYPE_STATE) {      } elsif ($self->{state} == DOCTYPE_STATE) {
1818        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1819            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1820            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1821            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1822            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1823            !!!cp (155);
1824          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
1825          !!!next-input-character;          !!!next-input-character;
1826          redo A;          redo A;
1827        } else {        } else {
1828            !!!cp (156);
1829          !!!parse-error (type => 'no space before DOCTYPE name');          !!!parse-error (type => 'no space before DOCTYPE name');
1830          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
1831          ## reconsume          ## reconsume
1832          redo A;          redo A;
1833        }        }
1834      } elsif ($self->{state} == BEFORE_DOCTYPE_NAME_STATE) {      } elsif ($self->{state} == BEFORE_DOCTYPE_NAME_STATE) {
1835        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1836            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1837            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1838            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1839            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1840            !!!cp (157);
1841          ## Stay in the state          ## Stay in the state
1842          !!!next-input-character;          !!!next-input-character;
1843          redo A;          redo A;
1844        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1845            !!!cp (158);
1846          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
1847          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1848          !!!next-input-character;          !!!next-input-character;
1849    
1850          !!!emit ({type => DOCTYPE_TOKEN}); # incorrect          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
1851    
1852          redo A;          redo A;
1853        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1854            !!!cp (159);
1855          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
1856          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1857          ## reconsume          ## reconsume
1858    
1859          !!!emit ({type => DOCTYPE_TOKEN}); # incorrect          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
1860    
1861          redo A;          redo A;
1862        } else {        } else {
1863          $self->{current_token}          !!!cp (160);
1864              = {type => DOCTYPE_TOKEN,          $self->{current_token}->{name} = chr $self->{next_char};
1865                 name => chr ($self->{next_input_character}),          delete $self->{current_token}->{quirks};
                correct => 1};  
1866  ## ISSUE: "Set the token's name name to the" in the spec  ## ISSUE: "Set the token's name name to the" in the spec
1867          $self->{state} = DOCTYPE_NAME_STATE;          $self->{state} = DOCTYPE_NAME_STATE;
1868          !!!next-input-character;          !!!next-input-character;
# Line 1299  sub _get_next_token ($) { Line 1870  sub _get_next_token ($) {
1870        }        }
1871      } elsif ($self->{state} == DOCTYPE_NAME_STATE) {      } elsif ($self->{state} == DOCTYPE_NAME_STATE) {
1872  ## ISSUE: Redundant "First," in the spec.  ## ISSUE: Redundant "First," in the spec.
1873        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1874            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1875            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1876            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1877            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1878            !!!cp (161);
1879          $self->{state} = AFTER_DOCTYPE_NAME_STATE;          $self->{state} = AFTER_DOCTYPE_NAME_STATE;
1880          !!!next-input-character;          !!!next-input-character;
1881          redo A;          redo A;
1882        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1883            !!!cp (162);
1884          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1885          !!!next-input-character;          !!!next-input-character;
1886    
1887          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1888    
1889          redo A;          redo A;
1890        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1891            !!!cp (163);
1892          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1893          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1894          ## reconsume          ## reconsume
1895    
1896          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
1897          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1898    
1899          redo A;          redo A;
1900        } else {        } else {
1901            !!!cp (164);
1902          $self->{current_token}->{name}          $self->{current_token}->{name}
1903            .= chr ($self->{next_input_character}); # DOCTYPE            .= chr ($self->{next_char}); # DOCTYPE
1904          ## Stay in the state          ## Stay in the state
1905          !!!next-input-character;          !!!next-input-character;
1906          redo A;          redo A;
1907        }        }
1908      } elsif ($self->{state} == AFTER_DOCTYPE_NAME_STATE) {      } elsif ($self->{state} == AFTER_DOCTYPE_NAME_STATE) {
1909        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1910            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1911            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1912            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1913            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1914            !!!cp (165);
1915          ## Stay in the state          ## Stay in the state
1916          !!!next-input-character;          !!!next-input-character;
1917          redo A;          redo A;
1918        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1919            !!!cp (166);
1920          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1921          !!!next-input-character;          !!!next-input-character;
1922    
1923          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1924    
1925          redo A;          redo A;
1926        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1927            !!!cp (167);
1928          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1929          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1930          ## reconsume          ## reconsume
1931    
1932          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
1933          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1934    
1935          redo A;          redo A;
1936        } elsif ($self->{next_input_character} == 0x0050 or # P        } elsif ($self->{next_char} == 0x0050 or # P
1937                 $self->{next_input_character} == 0x0070) { # p                 $self->{next_char} == 0x0070) { # p
1938          !!!next-input-character;          !!!next-input-character;
1939          if ($self->{next_input_character} == 0x0055 or # U          if ($self->{next_char} == 0x0055 or # U
1940              $self->{next_input_character} == 0x0075) { # u              $self->{next_char} == 0x0075) { # u
1941            !!!next-input-character;            !!!next-input-character;
1942            if ($self->{next_input_character} == 0x0042 or # B            if ($self->{next_char} == 0x0042 or # B
1943                $self->{next_input_character} == 0x0062) { # b                $self->{next_char} == 0x0062) { # b
1944              !!!next-input-character;              !!!next-input-character;
1945              if ($self->{next_input_character} == 0x004C or # L              if ($self->{next_char} == 0x004C or # L
1946                  $self->{next_input_character} == 0x006C) { # l                  $self->{next_char} == 0x006C) { # l
1947                !!!next-input-character;                !!!next-input-character;
1948                if ($self->{next_input_character} == 0x0049 or # I                if ($self->{next_char} == 0x0049 or # I
1949                    $self->{next_input_character} == 0x0069) { # i                    $self->{next_char} == 0x0069) { # i
1950                  !!!next-input-character;                  !!!next-input-character;
1951                  if ($self->{next_input_character} == 0x0043 or # C                  if ($self->{next_char} == 0x0043 or # C
1952                      $self->{next_input_character} == 0x0063) { # c                      $self->{next_char} == 0x0063) { # c
1953                      !!!cp (168);
1954                    $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;                    $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1955                    !!!next-input-character;                    !!!next-input-character;
1956                    redo A;                    redo A;
1957                    } else {
1958                      !!!cp (169);
1959                  }                  }
1960                  } else {
1961                    !!!cp (170);
1962                }                }
1963                } else {
1964                  !!!cp (171);
1965              }              }
1966              } else {
1967                !!!cp (172);
1968            }            }
1969            } else {
1970              !!!cp (173);
1971          }          }
1972    
1973          #          #
1974        } elsif ($self->{next_input_character} == 0x0053 or # S        } elsif ($self->{next_char} == 0x0053 or # S
1975                 $self->{next_input_character} == 0x0073) { # s                 $self->{next_char} == 0x0073) { # s
1976          !!!next-input-character;          !!!next-input-character;
1977          if ($self->{next_input_character} == 0x0059 or # Y          if ($self->{next_char} == 0x0059 or # Y
1978              $self->{next_input_character} == 0x0079) { # y              $self->{next_char} == 0x0079) { # y
1979            !!!next-input-character;            !!!next-input-character;
1980            if ($self->{next_input_character} == 0x0053 or # S            if ($self->{next_char} == 0x0053 or # S
1981                $self->{next_input_character} == 0x0073) { # s                $self->{next_char} == 0x0073) { # s
1982              !!!next-input-character;              !!!next-input-character;
1983              if ($self->{next_input_character} == 0x0054 or # T              if ($self->{next_char} == 0x0054 or # T
1984                  $self->{next_input_character} == 0x0074) { # t                  $self->{next_char} == 0x0074) { # t
1985                !!!next-input-character;                !!!next-input-character;
1986                if ($self->{next_input_character} == 0x0045 or # E                if ($self->{next_char} == 0x0045 or # E
1987                    $self->{next_input_character} == 0x0065) { # e                    $self->{next_char} == 0x0065) { # e
1988                  !!!next-input-character;                  !!!next-input-character;
1989                  if ($self->{next_input_character} == 0x004D or # M                  if ($self->{next_char} == 0x004D or # M
1990                      $self->{next_input_character} == 0x006D) { # m                      $self->{next_char} == 0x006D) { # m
1991                      !!!cp (174);
1992                    $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;                    $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
1993                    !!!next-input-character;                    !!!next-input-character;
1994                    redo A;                    redo A;
1995                    } else {
1996                      !!!cp (175);
1997                  }                  }
1998                  } else {
1999                    !!!cp (176);
2000                }                }
2001                } else {
2002                  !!!cp (177);
2003              }              }
2004              } else {
2005                !!!cp (178);
2006            }            }
2007            } else {
2008              !!!cp (179);
2009          }          }
2010    
2011          #          #
2012        } else {        } else {
2013            !!!cp (180);
2014          !!!next-input-character;          !!!next-input-character;
2015          #          #
2016        }        }
2017    
2018        !!!parse-error (type => 'string after DOCTYPE name');        !!!parse-error (type => 'string after DOCTYPE name');
2019          $self->{current_token}->{quirks} = 1;
2020    
2021        $self->{state} = BOGUS_DOCTYPE_STATE;        $self->{state} = BOGUS_DOCTYPE_STATE;
2022        # next-input-character is already done        # next-input-character is already done
2023        redo A;        redo A;
# Line 1422  sub _get_next_token ($) { Line 2025  sub _get_next_token ($) {
2025        if ({        if ({
2026              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2027              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2028            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2029            !!!cp (181);
2030          ## Stay in the state          ## Stay in the state
2031          !!!next-input-character;          !!!next-input-character;
2032          redo A;          redo A;
2033        } elsif ($self->{next_input_character} eq 0x0022) { # "        } elsif ($self->{next_char} eq 0x0022) { # "
2034            !!!cp (182);
2035          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{current_token}->{public_identifier} = ''; # DOCTYPE
2036          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;
2037          !!!next-input-character;          !!!next-input-character;
2038          redo A;          redo A;
2039        } elsif ($self->{next_input_character} eq 0x0027) { # '        } elsif ($self->{next_char} eq 0x0027) { # '
2040            !!!cp (183);
2041          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{current_token}->{public_identifier} = ''; # DOCTYPE
2042          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;
2043          !!!next-input-character;          !!!next-input-character;
2044          redo A;          redo A;
2045        } elsif ($self->{next_input_character} eq 0x003E) { # >        } elsif ($self->{next_char} eq 0x003E) { # >
2046            !!!cp (184);
2047          !!!parse-error (type => 'no PUBLIC literal');          !!!parse-error (type => 'no PUBLIC literal');
2048    
2049          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2050          !!!next-input-character;          !!!next-input-character;
2051    
2052          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2053          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2054    
2055          redo A;          redo A;
2056        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2057            !!!cp (185);
2058          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2059    
2060          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2061          ## reconsume          ## reconsume
2062    
2063          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2064          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2065    
2066          redo A;          redo A;
2067        } else {        } else {
2068            !!!cp (186);
2069          !!!parse-error (type => 'string after PUBLIC');          !!!parse-error (type => 'string after PUBLIC');
2070            $self->{current_token}->{quirks} = 1;
2071    
2072          $self->{state} = BOGUS_DOCTYPE_STATE;          $self->{state} = BOGUS_DOCTYPE_STATE;
2073          !!!next-input-character;          !!!next-input-character;
2074          redo A;          redo A;
2075        }        }
2076      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2077        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
2078            !!!cp (187);
2079          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2080          !!!next-input-character;          !!!next-input-character;
2081          redo A;          redo A;
2082        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == 0x003E) { # >
2083            !!!cp (188);
2084            !!!parse-error (type => 'unclosed PUBLIC literal');
2085    
2086            $self->{state} = DATA_STATE;
2087            !!!next-input-character;
2088    
2089            $self->{current_token}->{quirks} = 1;
2090            !!!emit ($self->{current_token}); # DOCTYPE
2091    
2092            redo A;
2093          } elsif ($self->{next_char} == -1) {
2094            !!!cp (189);
2095          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
2096    
2097          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2098          ## reconsume          ## reconsume
2099    
2100          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2101          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2102    
2103          redo A;          redo A;
2104        } else {        } else {
2105            !!!cp (190);
2106          $self->{current_token}->{public_identifier} # DOCTYPE          $self->{current_token}->{public_identifier} # DOCTYPE
2107              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2108          ## Stay in the state          ## Stay in the state
2109          !!!next-input-character;          !!!next-input-character;
2110          redo A;          redo A;
2111        }        }
2112      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {
2113        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
2114            !!!cp (191);
2115          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2116          !!!next-input-character;          !!!next-input-character;
2117          redo A;          redo A;
2118        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == 0x003E) { # >
2119            !!!cp (192);
2120            !!!parse-error (type => 'unclosed PUBLIC literal');
2121    
2122            $self->{state} = DATA_STATE;
2123            !!!next-input-character;
2124    
2125            $self->{current_token}->{quirks} = 1;
2126            !!!emit ($self->{current_token}); # DOCTYPE
2127    
2128            redo A;
2129          } elsif ($self->{next_char} == -1) {
2130            !!!cp (193);
2131          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
2132    
2133          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2134          ## reconsume          ## reconsume
2135    
2136          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2137          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2138    
2139          redo A;          redo A;
2140        } else {        } else {
2141            !!!cp (194);
2142          $self->{current_token}->{public_identifier} # DOCTYPE          $self->{current_token}->{public_identifier} # DOCTYPE
2143              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2144          ## Stay in the state          ## Stay in the state
2145          !!!next-input-character;          !!!next-input-character;
2146          redo A;          redo A;
# Line 1510  sub _get_next_token ($) { Line 2149  sub _get_next_token ($) {
2149        if ({        if ({
2150              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2151              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2152            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2153            !!!cp (195);
2154          ## Stay in the state          ## Stay in the state
2155          !!!next-input-character;          !!!next-input-character;
2156          redo A;          redo A;
2157        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
2158            !!!cp (196);
2159          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2160          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2161          !!!next-input-character;          !!!next-input-character;
2162          redo A;          redo A;
2163        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
2164            !!!cp (197);
2165          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2166          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2167          !!!next-input-character;          !!!next-input-character;
2168          redo A;          redo A;
2169        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2170            !!!cp (198);
2171          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2172          !!!next-input-character;          !!!next-input-character;
2173    
2174          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2175    
2176          redo A;          redo A;
2177        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2178            !!!cp (199);
2179          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2180    
2181          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2182          ## reconsume          ## reconsume
2183    
2184          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2185          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2186    
2187          redo A;          redo A;
2188        } else {        } else {
2189            !!!cp (200);
2190          !!!parse-error (type => 'string after PUBLIC literal');          !!!parse-error (type => 'string after PUBLIC literal');
2191            $self->{current_token}->{quirks} = 1;
2192    
2193          $self->{state} = BOGUS_DOCTYPE_STATE;          $self->{state} = BOGUS_DOCTYPE_STATE;
2194          !!!next-input-character;          !!!next-input-character;
2195          redo A;          redo A;
# Line 1551  sub _get_next_token ($) { Line 2198  sub _get_next_token ($) {
2198        if ({        if ({
2199              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2200              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2201            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2202            !!!cp (201);
2203          ## Stay in the state          ## Stay in the state
2204          !!!next-input-character;          !!!next-input-character;
2205          redo A;          redo A;
2206        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
2207            !!!cp (202);
2208          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2209          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2210          !!!next-input-character;          !!!next-input-character;
2211          redo A;          redo A;
2212        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
2213            !!!cp (203);
2214          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2215          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2216          !!!next-input-character;          !!!next-input-character;
2217          redo A;          redo A;
2218        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2219            !!!cp (204);
2220          !!!parse-error (type => 'no SYSTEM literal');          !!!parse-error (type => 'no SYSTEM literal');
2221          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2222          !!!next-input-character;          !!!next-input-character;
2223    
2224          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2225          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2226    
2227          redo A;          redo A;
2228        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2229            !!!cp (205);
2230          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2231    
2232          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2233          ## reconsume          ## reconsume
2234    
2235          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2236          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2237    
2238          redo A;          redo A;
2239        } else {        } else {
2240            !!!cp (206);
2241          !!!parse-error (type => 'string after SYSTEM');          !!!parse-error (type => 'string after SYSTEM');
2242            $self->{current_token}->{quirks} = 1;
2243    
2244          $self->{state} = BOGUS_DOCTYPE_STATE;          $self->{state} = BOGUS_DOCTYPE_STATE;
2245          !!!next-input-character;          !!!next-input-character;
2246          redo A;          redo A;
2247        }        }
2248      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2249        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
2250            !!!cp (207);
2251          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2252          !!!next-input-character;          !!!next-input-character;
2253          redo A;          redo A;
2254        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == 0x003E) { # >
2255            !!!cp (208);
2256            !!!parse-error (type => 'unclosed PUBLIC literal');
2257    
2258            $self->{state} = DATA_STATE;
2259            !!!next-input-character;
2260    
2261            $self->{current_token}->{quirks} = 1;
2262            !!!emit ($self->{current_token}); # DOCTYPE
2263    
2264            redo A;
2265          } elsif ($self->{next_char} == -1) {
2266            !!!cp (209);
2267          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2268    
2269          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2270          ## reconsume          ## reconsume
2271    
2272          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2273          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2274    
2275          redo A;          redo A;
2276        } else {        } else {
2277            !!!cp (210);
2278          $self->{current_token}->{system_identifier} # DOCTYPE          $self->{current_token}->{system_identifier} # DOCTYPE
2279              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2280          ## Stay in the state          ## Stay in the state
2281          !!!next-input-character;          !!!next-input-character;
2282          redo A;          redo A;
2283        }        }
2284      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {
2285        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
2286            !!!cp (211);
2287          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2288          !!!next-input-character;          !!!next-input-character;
2289          redo A;          redo A;
2290        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == 0x003E) { # >
2291            !!!cp (212);
2292            !!!parse-error (type => 'unclosed PUBLIC literal');
2293    
2294            $self->{state} = DATA_STATE;
2295            !!!next-input-character;
2296    
2297            $self->{current_token}->{quirks} = 1;
2298            !!!emit ($self->{current_token}); # DOCTYPE
2299    
2300            redo A;
2301          } elsif ($self->{next_char} == -1) {
2302            !!!cp (213);
2303          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2304    
2305          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2306          ## reconsume          ## reconsume
2307    
2308          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2309          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2310    
2311          redo A;          redo A;
2312        } else {        } else {
2313            !!!cp (214);
2314          $self->{current_token}->{system_identifier} # DOCTYPE          $self->{current_token}->{system_identifier} # DOCTYPE
2315              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2316          ## Stay in the state          ## Stay in the state
2317          !!!next-input-character;          !!!next-input-character;
2318          redo A;          redo A;
# Line 1638  sub _get_next_token ($) { Line 2321  sub _get_next_token ($) {
2321        if ({        if ({
2322              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2323              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2324            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2325            !!!cp (215);
2326          ## Stay in the state          ## Stay in the state
2327          !!!next-input-character;          !!!next-input-character;
2328          redo A;          redo A;
2329        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2330            !!!cp (216);
2331          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2332          !!!next-input-character;          !!!next-input-character;
2333    
2334          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2335    
2336          redo A;          redo A;
2337        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2338            !!!cp (217);
2339          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2340    
2341          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2342          ## reconsume          ## reconsume
2343    
2344          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2345          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2346    
2347          redo A;          redo A;
2348        } else {        } else {
2349            !!!cp (218);
2350          !!!parse-error (type => 'string after SYSTEM literal');          !!!parse-error (type => 'string after SYSTEM literal');
2351            #$self->{current_token}->{quirks} = 1;
2352    
2353          $self->{state} = BOGUS_DOCTYPE_STATE;          $self->{state} = BOGUS_DOCTYPE_STATE;
2354          !!!next-input-character;          !!!next-input-character;
2355          redo A;          redo A;
2356        }        }
2357      } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {      } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {
2358        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_char} == 0x003E) { # >
2359            !!!cp (219);
2360          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2361          !!!next-input-character;          !!!next-input-character;
2362    
         delete $self->{current_token}->{correct};  
2363          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2364    
2365          redo A;          redo A;
2366        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2367            !!!cp (220);
2368          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2369          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2370          ## reconsume          ## reconsume
2371    
         delete $self->{current_token}->{correct};  
2372          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2373    
2374          redo A;          redo A;
2375        } else {        } else {
2376            !!!cp (221);
2377          ## Stay in the state          ## Stay in the state
2378          !!!next-input-character;          !!!next-input-character;
2379          redo A;          redo A;
# Line 1696  sub _get_next_token ($) { Line 2386  sub _get_next_token ($) {
2386    die "$0: _get_next_token: unexpected case";    die "$0: _get_next_token: unexpected case";
2387  } # _get_next_token  } # _get_next_token
2388    
2389  sub _tokenize_attempt_to_consume_an_entity ($$) {  sub _tokenize_attempt_to_consume_an_entity ($$$) {
2390    my ($self, $in_attr) = @_;    my ($self, $in_attr, $additional) = @_;
2391    
2392      my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
2393    
2394    if ({    if ({
2395         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,
2396         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR
2397        }->{$self->{next_input_character}}) {         $additional => 1,
2398          }->{$self->{next_char}}) {
2399        !!!cp (1001);
2400      ## Don't consume      ## Don't consume
2401      ## No error      ## No error
2402      return undef;      return undef;
2403    } elsif ($self->{next_input_character} == 0x0023) { # #    } elsif ($self->{next_char} == 0x0023) { # #
2404      !!!next-input-character;      !!!next-input-character;
2405      if ($self->{next_input_character} == 0x0078 or # x      if ($self->{next_char} == 0x0078 or # x
2406          $self->{next_input_character} == 0x0058) { # X          $self->{next_char} == 0x0058) { # X
2407        my $code;        my $code;
2408        X: {        X: {
2409          my $x_char = $self->{next_input_character};          my $x_char = $self->{next_char};
2410          !!!next-input-character;          !!!next-input-character;
2411          if (0x0030 <= $self->{next_input_character} and          if (0x0030 <= $self->{next_char} and
2412              $self->{next_input_character} <= 0x0039) { # 0..9              $self->{next_char} <= 0x0039) { # 0..9
2413              !!!cp (1002);
2414            $code ||= 0;            $code ||= 0;
2415            $code *= 0x10;            $code *= 0x10;
2416            $code += $self->{next_input_character} - 0x0030;            $code += $self->{next_char} - 0x0030;
2417            redo X;            redo X;
2418          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_char} and
2419                   $self->{next_input_character} <= 0x0066) { # a..f                   $self->{next_char} <= 0x0066) { # a..f
2420              !!!cp (1003);
2421            $code ||= 0;            $code ||= 0;
2422            $code *= 0x10;            $code *= 0x10;
2423            $code += $self->{next_input_character} - 0x0060 + 9;            $code += $self->{next_char} - 0x0060 + 9;
2424            redo X;            redo X;
2425          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_char} and
2426                   $self->{next_input_character} <= 0x0046) { # A..F                   $self->{next_char} <= 0x0046) { # A..F
2427              !!!cp (1004);
2428            $code ||= 0;            $code ||= 0;
2429            $code *= 0x10;            $code *= 0x10;
2430            $code += $self->{next_input_character} - 0x0040 + 9;            $code += $self->{next_char} - 0x0040 + 9;
2431            redo X;            redo X;
2432          } elsif (not defined $code) { # no hexadecimal digit          } elsif (not defined $code) { # no hexadecimal digit
2433            !!!parse-error (type => 'bare hcro');            !!!cp (1005);
2434            !!!back-next-input-character ($x_char, $self->{next_input_character});            !!!parse-error (type => 'bare hcro', line => $l, column => $c);
2435            $self->{next_input_character} = 0x0023; # #            !!!back-next-input-character ($x_char, $self->{next_char});
2436              $self->{next_char} = 0x0023; # #
2437            return undef;            return undef;
2438          } elsif ($self->{next_input_character} == 0x003B) { # ;          } elsif ($self->{next_char} == 0x003B) { # ;
2439              !!!cp (1006);
2440            !!!next-input-character;            !!!next-input-character;
2441          } else {          } else {
2442            !!!parse-error (type => 'no refc');            !!!cp (1007);
2443              !!!parse-error (type => 'no refc', line => $l, column => $c);
2444          }          }
2445    
2446          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2447            !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);            !!!cp (1008);
2448              !!!parse-error (type => (sprintf 'invalid character reference:U+%04X', $code), line => $l, column => $c);
2449            $code = 0xFFFD;            $code = 0xFFFD;
2450          } elsif ($code > 0x10FFFF) {          } elsif ($code > 0x10FFFF) {
2451            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);            !!!cp (1009);
2452              !!!parse-error (type => (sprintf 'invalid character reference:U-%08X', $code), line => $l, column => $c);
2453            $code = 0xFFFD;            $code = 0xFFFD;
2454          } elsif ($code == 0x000D) {          } elsif ($code == 0x000D) {
2455            !!!parse-error (type => 'CR character reference');            !!!cp (1010);
2456              !!!parse-error (type => 'CR character reference', line => $l, column => $c);
2457            $code = 0x000A;            $code = 0x000A;
2458          } elsif (0x80 <= $code and $code <= 0x9F) {          } elsif (0x80 <= $code and $code <= 0x9F) {
2459            !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);            !!!cp (1011);
2460              !!!parse-error (type => (sprintf 'C1 character reference:U+%04X', $code), line => $l, column => $c);
2461            $code = $c1_entity_char->{$code};            $code = $c1_entity_char->{$code};
2462          }          }
2463    
2464          return {type => CHARACTER_TOKEN, data => chr $code};          return {type => CHARACTER_TOKEN, data => chr $code,
2465                    has_reference => 1,
2466                    line => $l, column => $c,
2467                   };
2468        } # X        } # X
2469      } elsif (0x0030 <= $self->{next_input_character} and      } elsif (0x0030 <= $self->{next_char} and
2470               $self->{next_input_character} <= 0x0039) { # 0..9               $self->{next_char} <= 0x0039) { # 0..9
2471        my $code = $self->{next_input_character} - 0x0030;        my $code = $self->{next_char} - 0x0030;
2472        !!!next-input-character;        !!!next-input-character;
2473                
2474        while (0x0030 <= $self->{next_input_character} and        while (0x0030 <= $self->{next_char} and
2475                  $self->{next_input_character} <= 0x0039) { # 0..9                  $self->{next_char} <= 0x0039) { # 0..9
2476            !!!cp (1012);
2477          $code *= 10;          $code *= 10;
2478          $code += $self->{next_input_character} - 0x0030;          $code += $self->{next_char} - 0x0030;
2479                    
2480          !!!next-input-character;          !!!next-input-character;
2481        }        }
2482    
2483        if ($self->{next_input_character} == 0x003B) { # ;        if ($self->{next_char} == 0x003B) { # ;
2484            !!!cp (1013);
2485          !!!next-input-character;          !!!next-input-character;
2486        } else {        } else {
2487          !!!parse-error (type => 'no refc');          !!!cp (1014);
2488            !!!parse-error (type => 'no refc', line => $l, column => $c);
2489        }        }
2490    
2491        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2492          !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);          !!!cp (1015);
2493            !!!parse-error (type => (sprintf 'invalid character reference:U+%04X', $code), line => $l, column => $c);
2494          $code = 0xFFFD;          $code = 0xFFFD;
2495        } elsif ($code > 0x10FFFF) {        } elsif ($code > 0x10FFFF) {
2496          !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);          !!!cp (1016);
2497            !!!parse-error (type => (sprintf 'invalid character reference:U-%08X', $code), line => $l, column => $c);
2498          $code = 0xFFFD;          $code = 0xFFFD;
2499        } elsif ($code == 0x000D) {        } elsif ($code == 0x000D) {
2500          !!!parse-error (type => 'CR character reference');          !!!cp (1017);
2501            !!!parse-error (type => 'CR character reference', line => $l, column => $c);
2502          $code = 0x000A;          $code = 0x000A;
2503        } elsif (0x80 <= $code and $code <= 0x9F) {        } elsif (0x80 <= $code and $code <= 0x9F) {
2504          !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);          !!!cp (1018);
2505            !!!parse-error (type => (sprintf 'C1 character reference:U+%04X', $code), line => $l, column => $c);
2506          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
2507        }        }
2508                
2509        return {type => CHARACTER_TOKEN, data => chr $code};        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1,
2510                  line => $l, column => $c,
2511                 };
2512      } else {      } else {
2513        !!!parse-error (type => 'bare nero');        !!!cp (1019);
2514        !!!back-next-input-character ($self->{next_input_character});        !!!parse-error (type => 'bare nero', line => $l, column => $c);
2515        $self->{next_input_character} = 0x0023; # #        !!!back-next-input-character ($self->{next_char});
2516          $self->{next_char} = 0x0023; # #
2517        return undef;        return undef;
2518      }      }
2519    } elsif ((0x0041 <= $self->{next_input_character} and    } elsif ((0x0041 <= $self->{next_char} and
2520              $self->{next_input_character} <= 0x005A) or              $self->{next_char} <= 0x005A) or
2521             (0x0061 <= $self->{next_input_character} and             (0x0061 <= $self->{next_char} and
2522              $self->{next_input_character} <= 0x007A)) {              $self->{next_char} <= 0x007A)) {
2523      my $entity_name = chr $self->{next_input_character};      my $entity_name = chr $self->{next_char};
2524      !!!next-input-character;      !!!next-input-character;
2525    
2526      my $value = $entity_name;      my $value = $entity_name;
# Line 1813  sub _tokenize_attempt_to_consume_an_enti Line 2530  sub _tokenize_attempt_to_consume_an_enti
2530    
2531      while (length $entity_name < 10 and      while (length $entity_name < 10 and
2532             ## NOTE: Some number greater than the maximum length of entity name             ## NOTE: Some number greater than the maximum length of entity name
2533             ((0x0041 <= $self->{next_input_character} and # a             ((0x0041 <= $self->{next_char} and # a
2534               $self->{next_input_character} <= 0x005A) or # x               $self->{next_char} <= 0x005A) or # x
2535              (0x0061 <= $self->{next_input_character} and # a              (0x0061 <= $self->{next_char} and # a
2536               $self->{next_input_character} <= 0x007A) or # z               $self->{next_char} <= 0x007A) or # z
2537              (0x0030 <= $self->{next_input_character} and # 0              (0x0030 <= $self->{next_char} and # 0
2538               $self->{next_input_character} <= 0x0039) or # 9               $self->{next_char} <= 0x0039) or # 9
2539              $self->{next_input_character} == 0x003B)) { # ;              $self->{next_char} == 0x003B)) { # ;
2540        $entity_name .= chr $self->{next_input_character};        $entity_name .= chr $self->{next_char};
2541        if (defined $EntityChar->{$entity_name}) {        if (defined $EntityChar->{$entity_name}) {
2542          if ($self->{next_input_character} == 0x003B) { # ;          if ($self->{next_char} == 0x003B) { # ;
2543              !!!cp (1020);
2544            $value = $EntityChar->{$entity_name};            $value = $EntityChar->{$entity_name};
2545            $match = 1;            $match = 1;
2546            !!!next-input-character;            !!!next-input-character;
2547            last;            last;
2548          } else {          } else {
2549              !!!cp (1021);
2550            $value = $EntityChar->{$entity_name};            $value = $EntityChar->{$entity_name};
2551            $match = -1;            $match = -1;
2552            !!!next-input-character;            !!!next-input-character;
2553          }          }
2554        } else {        } else {
2555          $value .= chr $self->{next_input_character};          !!!cp (1022);
2556            $value .= chr $self->{next_char};
2557          $match *= 2;          $match *= 2;
2558          !!!next-input-character;          !!!next-input-character;
2559        }        }
2560      }      }
2561            
2562      if ($match > 0) {      if ($match > 0) {
2563        return {type => CHARACTER_TOKEN, data => $value};        !!!cp (1023);
2564          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
2565                  line => $l, column => $c,
2566                 };
2567      } elsif ($match < 0) {      } elsif ($match < 0) {
2568        !!!parse-error (type => 'no refc');        !!!parse-error (type => 'no refc', line => $l, column => $c);
2569        if ($in_attr and $match < -1) {        if ($in_attr and $match < -1) {
2570          return {type => CHARACTER_TOKEN, data => '&'.$entity_name};          !!!cp (1024);
2571        } else {          return {type => CHARACTER_TOKEN, data => '&'.$entity_name,
2572          return {type => CHARACTER_TOKEN, data => $value};                  line => $l, column => $c,
2573                   };
2574          } else {
2575            !!!cp (1025);
2576            return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
2577                    line => $l, column => $c,
2578                   };
2579        }        }
2580      } else {      } else {
2581        !!!parse-error (type => 'bare ero');        !!!cp (1026);
2582        ## NOTE: No characters are consumed in the spec.        !!!parse-error (type => 'bare ero', line => $l, column => $c);
2583        return {type => CHARACTER_TOKEN, data => '&'.$value};        ## NOTE: "No characters are consumed" in the spec.
2584          return {type => CHARACTER_TOKEN, data => '&'.$value,
2585                  line => $l, column => $c,
2586                 };
2587      }      }
2588    } else {    } else {
2589        !!!cp (1027);
2590      ## no characters are consumed      ## no characters are consumed
2591      !!!parse-error (type => 'bare ero');      !!!parse-error (type => 'bare ero', line => $l, column => $c);
2592      return undef;      return undef;
2593    }    }
2594  } # _tokenize_attempt_to_consume_an_entity  } # _tokenize_attempt_to_consume_an_entity
# Line 1893  sub _construct_tree ($) { Line 2626  sub _construct_tree ($) {
2626        
2627    !!!next-token;    !!!next-token;
2628    
   $self->{insertion_mode} = BEFORE_HEAD_IM;  
2629    undef $self->{form_element};    undef $self->{form_element};
2630    undef $self->{head_element};    undef $self->{head_element};
2631    $self->{open_elements} = [];    $self->{open_elements} = [];
2632    undef $self->{inner_html_node};    undef $self->{inner_html_node};
2633    
2634      ## NOTE: The "initial" insertion mode.
2635    $self->_tree_construction_initial; # MUST    $self->_tree_construction_initial; # MUST
2636    
2637      ## NOTE: The "before html" insertion mode.
2638    $self->_tree_construction_root_element;    $self->_tree_construction_root_element;
2639      $self->{insertion_mode} = BEFORE_HEAD_IM;
2640    
2641      ## NOTE: The "before head" insertion mode and so on.
2642    $self->_tree_construction_main;    $self->_tree_construction_main;
2643  } # _construct_tree  } # _construct_tree
2644    
2645  sub _tree_construction_initial ($) {  sub _tree_construction_initial ($) {
2646    my $self = shift;    my $self = shift;
2647    
2648      ## NOTE: "initial" insertion mode
2649    
2650    INITIAL: {    INITIAL: {
2651      if ($token->{type} == DOCTYPE_TOKEN) {      if ($token->{type} == DOCTYPE_TOKEN) {
2652        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"
# Line 1917  sub _tree_construction_initial ($) { Line 2658  sub _tree_construction_initial ($) {
2658        if (not defined $token->{name} or # <!DOCTYPE>        if (not defined $token->{name} or # <!DOCTYPE>
2659            defined $token->{public_identifier} or            defined $token->{public_identifier} or
2660            defined $token->{system_identifier}) {            defined $token->{system_identifier}) {
2661          !!!parse-error (type => 'not HTML5');          !!!cp ('t1');
2662            !!!parse-error (type => 'not HTML5', token => $token);
2663        } elsif ($doctype_name ne 'HTML') {        } elsif ($doctype_name ne 'HTML') {
2664            !!!cp ('t2');
2665          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)
2666          !!!parse-error (type => 'not HTML5');          !!!parse-error (type => 'not HTML5', token => $token);
2667          } else {
2668            !!!cp ('t3');
2669        }        }
2670                
2671        my $doctype = $self->{document}->create_document_type_definition        my $doctype = $self->{document}->create_document_type_definition
2672          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
2673          ## NOTE: Default value for both |public_id| and |system_id| attributes
2674          ## are empty strings, so that we don't set any value in missing cases.
2675        $doctype->public_id ($token->{public_identifier})        $doctype->public_id ($token->{public_identifier})
2676            if defined $token->{public_identifier};            if defined $token->{public_identifier};
2677        $doctype->system_id ($token->{system_identifier})        $doctype->system_id ($token->{system_identifier})
# Line 1933  sub _tree_construction_initial ($) { Line 2680  sub _tree_construction_initial ($) {
2680        ## ISSUE: internalSubset = null??        ## ISSUE: internalSubset = null??
2681        $self->{document}->append_child ($doctype);        $self->{document}->append_child ($doctype);
2682                
2683        if (not $token->{correct} or $doctype_name ne 'HTML') {        if ($token->{quirks} or $doctype_name ne 'HTML') {
2684            !!!cp ('t4');
2685          $self->{document}->manakai_compat_mode ('quirks');          $self->{document}->manakai_compat_mode ('quirks');
2686        } elsif (defined $token->{public_identifier}) {        } elsif (defined $token->{public_identifier}) {
2687          my $pubid = $token->{public_identifier};          my $pubid = $token->{public_identifier};
# Line 1987  sub _tree_construction_initial ($) { Line 2735  sub _tree_construction_initial ($) {
2735            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,
2736            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,
2737            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,
2738              "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//EN" => 1,
2739              "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//EN" => 1,
2740              "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//EN" => 1,
2741            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,
2742            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,
2743            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,
# Line 2009  sub _tree_construction_initial ($) { Line 2760  sub _tree_construction_initial ($) {
2760            "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" => 1,            "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" => 1,
2761            "HTML" => 1,            "HTML" => 1,
2762          }->{$pubid}) {          }->{$pubid}) {
2763              !!!cp ('t5');
2764            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
2765          } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or          } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or
2766                   $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {                   $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {
2767            if (defined $token->{system_identifier}) {            if (defined $token->{system_identifier}) {
2768                !!!cp ('t6');
2769              $self->{document}->manakai_compat_mode ('quirks');              $self->{document}->manakai_compat_mode ('quirks');
2770            } else {            } else {
2771                !!!cp ('t7');
2772              $self->{document}->manakai_compat_mode ('limited quirks');              $self->{document}->manakai_compat_mode ('limited quirks');
2773            }            }
2774          } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 Frameset//EN" or          } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 FRAMESET//EN" or
2775                   $pubid eq "-//W3C//DTD XHTML 1.0 Transitional//EN") {                   $pubid eq "-//W3C//DTD XHTML 1.0 TRANSITIONAL//EN") {
2776              !!!cp ('t8');
2777            $self->{document}->manakai_compat_mode ('limited quirks');            $self->{document}->manakai_compat_mode ('limited quirks');
2778            } else {
2779              !!!cp ('t9');
2780          }          }
2781          } else {
2782            !!!cp ('t10');
2783        }        }
2784        if (defined $token->{system_identifier}) {        if (defined $token->{system_identifier}) {
2785          my $sysid = $token->{system_identifier};          my $sysid = $token->{system_identifier};
2786          $sysid =~ tr/A-Z/a-z/;          $sysid =~ tr/A-Z/a-z/;
2787          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") {
2788              ## TODO: Check the spec: PUBLIC "(limited quirks)" "(quirks)"
2789            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
2790              !!!cp ('t11');
2791            } else {
2792              !!!cp ('t12');
2793          }          }
2794          } else {
2795            !!!cp ('t13');
2796        }        }
2797                
2798        ## Go to the root element phase.        ## Go to the "before html" insertion mode.
2799        !!!next-token;        !!!next-token;
2800        return;        return;
2801      } elsif ({      } elsif ({
# Line 2038  sub _tree_construction_initial ($) { Line 2803  sub _tree_construction_initial ($) {
2803                END_TAG_TOKEN, 1,                END_TAG_TOKEN, 1,
2804                END_OF_FILE_TOKEN, 1,                END_OF_FILE_TOKEN, 1,
2805               }->{$token->{type}}) {               }->{$token->{type}}) {
2806        !!!parse-error (type => 'no DOCTYPE');        !!!cp ('t14');
2807          !!!parse-error (type => 'no DOCTYPE', token => $token);
2808        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
2809        ## Go to the root element phase        ## Go to the "before html" insertion mode.
2810        ## reprocess        ## reprocess
2811          !!!ack-later;
2812        return;        return;
2813      } elsif ($token->{type} == CHARACTER_TOKEN) {      } elsif ($token->{type} == CHARACTER_TOKEN) {
2814        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
2815          ## Ignore the token          ## Ignore the token
2816    
2817          unless (length $token->{data}) {          unless (length $token->{data}) {
2818            ## Stay in the phase            !!!cp ('t15');
2819              ## Stay in the insertion mode.
2820            !!!next-token;            !!!next-token;
2821            redo INITIAL;            redo INITIAL;
2822            } else {
2823              !!!cp ('t16');
2824          }          }
2825          } else {
2826            !!!cp ('t17');
2827        }        }
2828    
2829        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE', token => $token);
2830        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
2831        ## Go to the root element phase        ## Go to the "before html" insertion mode.
2832        ## reprocess        ## reprocess
2833        return;        return;
2834      } elsif ($token->{type} == COMMENT_TOKEN) {      } elsif ($token->{type} == COMMENT_TOKEN) {
2835          !!!cp ('t18');
2836        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
2837        $self->{document}->append_child ($comment);        $self->{document}->append_child ($comment);
2838                
2839        ## Stay in the phase.        ## Stay in the insertion mode.
2840        !!!next-token;        !!!next-token;
2841        redo INITIAL;        redo INITIAL;
2842      } else {      } else {
2843        die "$0: $token->{type}: Unknown token type";        die "$0: $token->{type}: Unknown token type";
2844      }      }
2845    } # INITIAL    } # INITIAL
2846    
2847      die "$0: _tree_construction_initial: This should be never reached";
2848  } # _tree_construction_initial  } # _tree_construction_initial
2849    
2850  sub _tree_construction_root_element ($) {  sub _tree_construction_root_element ($) {
2851    my $self = shift;    my $self = shift;
2852    
2853      ## NOTE: "before html" insertion mode.
2854        
2855    B: {    B: {
2856        if ($token->{type} == DOCTYPE_TOKEN) {        if ($token->{type} == DOCTYPE_TOKEN) {
2857          !!!parse-error (type => 'in html:#DOCTYPE');          !!!cp ('t19');
2858            !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
2859          ## Ignore the token          ## Ignore the token
2860          ## Stay in the phase          ## Stay in the insertion mode.
2861          !!!next-token;          !!!next-token;
2862          redo B;          redo B;
2863        } elsif ($token->{type} == COMMENT_TOKEN) {        } elsif ($token->{type} == COMMENT_TOKEN) {
2864            !!!cp ('t20');
2865          my $comment = $self->{document}->create_comment ($token->{data});          my $comment = $self->{document}->create_comment ($token->{data});
2866          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
2867          ## Stay in the phase          ## Stay in the insertion mode.
2868          !!!next-token;          !!!next-token;
2869          redo B;          redo B;
2870        } elsif ($token->{type} == CHARACTER_TOKEN) {        } elsif ($token->{type} == CHARACTER_TOKEN) {
# Line 2093  sub _tree_construction_root_element ($) Line 2872  sub _tree_construction_root_element ($)
2872            ## Ignore the token.            ## Ignore the token.
2873    
2874            unless (length $token->{data}) {            unless (length $token->{data}) {
2875              ## Stay in the phase              !!!cp ('t21');
2876                ## Stay in the insertion mode.
2877              !!!next-token;              !!!next-token;
2878              redo B;              redo B;
2879              } else {
2880                !!!cp ('t22');
2881            }            }
2882            } else {
2883              !!!cp ('t23');
2884          }          }
2885    
2886          $self->{application_cache_selection}->(undef);          $self->{application_cache_selection}->(undef);
2887    
2888          #          #
2889        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
2890          if ($token->{tag_name} eq 'html' and          if ($token->{tag_name} eq 'html') {
2891              $token->{attributes}->{manifest}) { ## ISSUE: Spec spells as "application"            my $root_element;
2892            $self->{application_cache_selection}            !!!create-element ($root_element, $token->{tag_name}, $token->{attributes}, $token);
2893                 ->($token->{attributes}->{manifest}->{value});            $self->{document}->append_child ($root_element);
2894            ## ISSUE: No relative reference resolution?            push @{$self->{open_elements}},
2895                  [$root_element, $el_category->{html}];
2896    
2897              if ($token->{attributes}->{manifest}) {
2898                !!!cp ('t24');
2899                $self->{application_cache_selection}
2900                    ->($token->{attributes}->{manifest}->{value});
2901                ## ISSUE: Spec is unclear on relative references.
2902                ## According to Hixie (#whatwg 2008-03-19), it should be
2903                ## resolved against the base URI of the document in HTML
2904                ## or xml:base of the element in XHTML.
2905              } else {
2906                !!!cp ('t25');
2907                $self->{application_cache_selection}->(undef);
2908              }
2909    
2910              !!!nack ('t25c');
2911    
2912              !!!next-token;
2913              return; ## Go to the "before head" insertion mode.
2914          } else {          } else {
2915            $self->{application_cache_selection}->(undef);            !!!cp ('t25.1');
2916              #
2917          }          }
   
         ## ISSUE: There is an issue in the spec  
         #  
2918        } elsif ({        } elsif ({
2919                  END_TAG_TOKEN, 1,                  END_TAG_TOKEN, 1,
2920                  END_OF_FILE_TOKEN, 1,                  END_OF_FILE_TOKEN, 1,
2921                 }->{$token->{type}}) {                 }->{$token->{type}}) {
2922          $self->{application_cache_selection}->(undef);          !!!cp ('t26');
   
         ## ISSUE: There is an issue in the spec  
2923          #          #
2924        } else {        } else {
2925          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
2926        }        }
2927    
2928        my $root_element; !!!create-element ($root_element, 'html');      my $root_element; !!!create-element ($root_element, 'html',, $token);
2929        $self->{document}->append_child ($root_element);      $self->{document}->append_child ($root_element);
2930        push @{$self->{open_elements}}, [$root_element, 'html'];      push @{$self->{open_elements}}, [$root_element, $el_category->{html}];
2931        ## reprocess  
2932        #redo B;      $self->{application_cache_selection}->(undef);
2933        return; ## Go to the main phase.  
2934        ## NOTE: Reprocess the token.
2935        !!!ack-later;
2936        return; ## Go to the "before head" insertion mode.
2937    
2938        ## ISSUE: There is an issue in the spec
2939    } # B    } # B
2940    
2941      die "$0: _tree_construction_root_element: This should never be reached";
2942  } # _tree_construction_root_element  } # _tree_construction_root_element
2943    
2944  sub _reset_insertion_mode ($) {  sub _reset_insertion_mode ($) {
# Line 2147  sub _reset_insertion_mode ($) { Line 2953  sub _reset_insertion_mode ($) {
2953            
2954      ## Step 3      ## Step 3
2955      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"!?  
2956        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
2957          $last = 1;          $last = 1;
2958          if (defined $self->{inner_html_node}) {          if (defined $self->{inner_html_node}) {
2959            if ($self->{inner_html_node}->[1] eq 'td' or            if ($self->{inner_html_node}->[1] & TABLE_CELL_EL) {
2960                $self->{inner_html_node}->[1] eq 'th') {              !!!cp ('t27');
2961              #              #
2962            } else {            } else {
2963                !!!cp ('t28');
2964              $node = $self->{inner_html_node};              $node = $self->{inner_html_node};
2965            }            }
2966          }          }
# Line 2167  sub _reset_insertion_mode ($) { Line 2969  sub _reset_insertion_mode ($) {
2969        ## Step 4..13        ## Step 4..13
2970        my $new_mode = {        my $new_mode = {
2971                        select => IN_SELECT_IM,                        select => IN_SELECT_IM,
2972                          ## NOTE: |option| and |optgroup| do not set
2973                          ## insertion mode to "in select" by themselves.
2974                        td => IN_CELL_IM,                        td => IN_CELL_IM,
2975                        th => IN_CELL_IM,                        th => IN_CELL_IM,
2976                        tr => IN_ROW_IM,                        tr => IN_ROW_IM,
# Line 2179  sub _reset_insertion_mode ($) { Line 2983  sub _reset_insertion_mode ($) {
2983                        head => IN_BODY_IM, # not in head!                        head => IN_BODY_IM, # not in head!
2984                        body => IN_BODY_IM,                        body => IN_BODY_IM,
2985                        frameset => IN_FRAMESET_IM,                        frameset => IN_FRAMESET_IM,
2986                       }->{$node->[1]};                       }->{$node->[0]->manakai_local_name};
2987                         ## TODO: Foreign namespace case OK?
2988        $self->{insertion_mode} = $new_mode and return if defined $new_mode;        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
2989                
2990        ## Step 14        ## Step 14
2991        if ($node->[1] eq 'html') {        if ($node->[1] & HTML_EL) {
2992          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
2993              !!!cp ('t29');
2994            $self->{insertion_mode} = BEFORE_HEAD_IM;            $self->{insertion_mode} = BEFORE_HEAD_IM;
2995          } else {          } else {
2996              ## ISSUE: Can this state be reached?
2997              !!!cp ('t30');
2998            $self->{insertion_mode} = AFTER_HEAD_IM;            $self->{insertion_mode} = AFTER_HEAD_IM;
2999          }          }
3000          return;          return;
3001          } else {
3002            !!!cp ('t31');
3003        }        }
3004                
3005        ## Step 15        ## Step 15
# Line 2202  sub _reset_insertion_mode ($) { Line 3012  sub _reset_insertion_mode ($) {
3012        ## Step 17        ## Step 17
3013        redo S3;        redo S3;
3014      } # S3      } # S3
3015    
3016      die "$0: _reset_insertion_mode: This line should never be reached";
3017  } # _reset_insertion_mode  } # _reset_insertion_mode
3018    
3019  sub _tree_construction_main ($) {  sub _tree_construction_main ($) {
# Line 2223  sub _tree_construction_main ($) { Line 3035  sub _tree_construction_main ($) {
3035      return if $entry->[0] eq '#marker';      return if $entry->[0] eq '#marker';
3036      for (@{$self->{open_elements}}) {      for (@{$self->{open_elements}}) {
3037        if ($entry->[0] eq $_->[0]) {        if ($entry->[0] eq $_->[0]) {
3038            !!!cp ('t32');
3039          return;          return;
3040        }        }
3041      }      }
# Line 2237  sub _tree_construction_main ($) { Line 3050  sub _tree_construction_main ($) {
3050    
3051        ## Step 6        ## Step 6
3052        if ($entry->[0] eq '#marker') {        if ($entry->[0] eq '#marker') {
3053            !!!cp ('t33_1');
3054          #          #
3055        } else {        } else {
3056          my $in_open_elements;          my $in_open_elements;
3057          OE: for (@{$self->{open_elements}}) {          OE: for (@{$self->{open_elements}}) {
3058            if ($entry->[0] eq $_->[0]) {            if ($entry->[0] eq $_->[0]) {
3059                !!!cp ('t33');
3060              $in_open_elements = 1;              $in_open_elements = 1;
3061              last OE;              last OE;
3062            }            }
3063          }          }
3064          if ($in_open_elements) {          if ($in_open_elements) {
3065              !!!cp ('t34');
3066            #            #
3067          } else {          } else {
3068              ## NOTE: <!DOCTYPE HTML><p><b><i><u></p> <p>X
3069              !!!cp ('t35');
3070            redo S4;            redo S4;
3071          }          }
3072        }        }
# Line 2271  sub _tree_construction_main ($) { Line 3089  sub _tree_construction_main ($) {
3089    
3090        ## Step 11        ## Step 11
3091        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {
3092            !!!cp ('t36');
3093          ## Step 7'          ## Step 7'
3094          $i++;          $i++;
3095          $entry = $active_formatting_elements->[$i];          $entry = $active_formatting_elements->[$i];
3096                    
3097          redo S7;          redo S7;
3098        }        }
3099    
3100          !!!cp ('t37');
3101      } # S7      } # S7
3102    }; # $reconstruct_active_formatting_elements    }; # $reconstruct_active_formatting_elements
3103    
3104    my $clear_up_to_marker = sub {    my $clear_up_to_marker = sub {
3105      for (reverse 0..$#$active_formatting_elements) {      for (reverse 0..$#$active_formatting_elements) {
3106        if ($active_formatting_elements->[$_]->[0] eq '#marker') {        if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3107            !!!cp ('t38');
3108          splice @$active_formatting_elements, $_;          splice @$active_formatting_elements, $_;
3109          return;          return;
3110        }        }
3111      }      }
3112    
3113        !!!cp ('t39');
3114    }; # $clear_up_to_marker    }; # $clear_up_to_marker
3115    
3116    my $parse_rcdata = sub ($$) {    my $insert;
3117      my ($content_model_flag, $insert) = @_;  
3118      my $parse_rcdata = sub ($) {
3119        my ($content_model_flag) = @_;
3120    
3121      ## Step 1      ## Step 1
3122      my $start_tag_name = $token->{tag_name};      my $start_tag_name = $token->{tag_name};
3123      my $el;      my $el;
3124      !!!create-element ($el, $start_tag_name, $token->{attributes});      !!!create-element ($el, $start_tag_name, $token->{attributes}, $token);
3125    
3126      ## Step 2      ## Step 2
3127      $insert->($el); # /context node/->append_child ($el)      $insert->($el);
3128    
3129      ## Step 3      ## Step 3
3130      $self->{content_model} = $content_model_flag; # CDATA or RCDATA      $self->{content_model} = $content_model_flag; # CDATA or RCDATA
# Line 2306  sub _tree_construction_main ($) { Line 3132  sub _tree_construction_main ($) {
3132    
3133      ## Step 4      ## Step 4
3134      my $text = '';      my $text = '';
3135        !!!nack ('t40.1');
3136      !!!next-token;      !!!next-token;
3137      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
3138          !!!cp ('t40');
3139        $text .= $token->{data};        $text .= $token->{data};
3140        !!!next-token;        !!!next-token;
3141      }      }
3142    
3143      ## Step 5      ## Step 5
3144      if (length $text) {      if (length $text) {
3145          !!!cp ('t41');
3146        my $text = $self->{document}->create_text_node ($text);        my $text = $self->{document}->create_text_node ($text);
3147        $el->append_child ($text);        $el->append_child ($text);
3148      }      }
# Line 2322  sub _tree_construction_main ($) { Line 3151  sub _tree_construction_main ($) {
3151      $self->{content_model} = PCDATA_CONTENT_MODEL;      $self->{content_model} = PCDATA_CONTENT_MODEL;
3152    
3153      ## Step 7      ## Step 7
3154      if ($token->{type} == END_TAG_TOKEN and $token->{tag_name} eq $start_tag_name) {      if ($token->{type} == END_TAG_TOKEN and
3155            $token->{tag_name} eq $start_tag_name) {
3156          !!!cp ('t42');
3157        ## 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});  
3158      } else {      } else {
3159        die "$0: $content_model_flag in parse_rcdata";        ## NOTE: An end-of-file token.
3160          if ($content_model_flag == CDATA_CONTENT_MODEL) {
3161            !!!cp ('t43');
3162            !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);
3163          } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
3164            !!!cp ('t44');
3165            !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);
3166          } else {
3167            die "$0: $content_model_flag in parse_rcdata";
3168          }
3169      }      }
3170      !!!next-token;      !!!next-token;
3171    }; # $parse_rcdata    }; # $parse_rcdata
3172    
3173    my $script_start_tag = sub ($) {    my $script_start_tag = sub () {
     my $insert = $_[0];  
3174      my $script_el;      my $script_el;
3175      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, 'script', $token->{attributes}, $token);
3176      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
3177    
3178      $self->{content_model} = CDATA_CONTENT_MODEL;      $self->{content_model} = CDATA_CONTENT_MODEL;
3179      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
3180            
3181      my $text = '';      my $text = '';
3182        !!!nack ('t45.1');
3183      !!!next-token;      !!!next-token;
3184      while ($token->{type} == CHARACTER_TOKEN) {      while ($token->{type} == CHARACTER_TOKEN) {
3185          !!!cp ('t45');
3186        $text .= $token->{data};        $text .= $token->{data};
3187        !!!next-token;        !!!next-token;
3188      } # stop if non-character token or tokenizer stops tokenising      } # stop if non-character token or tokenizer stops tokenising
3189      if (length $text) {      if (length $text) {
3190          !!!cp ('t46');
3191        $script_el->manakai_append_text ($text);        $script_el->manakai_append_text ($text);
3192      }      }
3193                                
# Line 2357  sub _tree_construction_main ($) { Line 3195  sub _tree_construction_main ($) {
3195    
3196      if ($token->{type} == END_TAG_TOKEN and      if ($token->{type} == END_TAG_TOKEN and
3197          $token->{tag_name} eq 'script') {          $token->{tag_name} eq 'script') {
3198          !!!cp ('t47');
3199        ## Ignore the token        ## Ignore the token
3200      } else {      } else {
3201        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!cp ('t48');
3202          !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);
3203        ## ISSUE: And ignore?        ## ISSUE: And ignore?
3204        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
3205      }      }
3206            
3207      if (defined $self->{inner_html_node}) {      if (defined $self->{inner_html_node}) {
3208          !!!cp ('t49');
3209        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
3210      } else {      } else {
3211          !!!cp ('t50');
3212        ## TODO: $old_insertion_point = current insertion point        ## TODO: $old_insertion_point = current insertion point
3213        ## TODO: insertion point = just before the next input character        ## TODO: insertion point = just before the next input character
3214    
# Line 2380  sub _tree_construction_main ($) { Line 3222  sub _tree_construction_main ($) {
3222      !!!next-token;      !!!next-token;
3223    }; # $script_start_tag    }; # $script_start_tag
3224    
3225      ## NOTE: $open_tables->[-1]->[0] is the "current table" element node.
3226      ## NOTE: $open_tables->[-1]->[1] is the "tainted" flag.
3227      my $open_tables = [[$self->{open_elements}->[0]->[0]]];
3228    
3229    my $formatting_end_tag = sub {    my $formatting_end_tag = sub {
3230      my $tag_name = shift;      my $end_tag_token = shift;
3231        my $tag_name = $end_tag_token->{tag_name};
3232    
3233        ## NOTE: The adoption agency algorithm (AAA).
3234    
3235      FET: {      FET: {
3236        ## Step 1        ## Step 1
3237        my $formatting_element;        my $formatting_element;
3238        my $formatting_element_i_in_active;        my $formatting_element_i_in_active;
3239        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3240          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {          if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3241              !!!cp ('t52');
3242              last AFE;
3243            } elsif ($active_formatting_elements->[$_]->[0]->manakai_local_name
3244                         eq $tag_name) {
3245              !!!cp ('t51');
3246            $formatting_element = $active_formatting_elements->[$_];            $formatting_element = $active_formatting_elements->[$_];
3247            $formatting_element_i_in_active = $_;            $formatting_element_i_in_active = $_;
3248            last AFE;            last AFE;
         } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {  
           last AFE;  
3249          }          }
3250        } # AFE        } # AFE
3251        unless (defined $formatting_element) {        unless (defined $formatting_element) {
3252          !!!parse-error (type => 'unmatched end tag:'.$tag_name);          !!!cp ('t53');
3253            !!!parse-error (type => 'unmatched end tag:'.$tag_name, token => $end_tag_token);
3254          ## Ignore the token          ## Ignore the token
3255          !!!next-token;          !!!next-token;
3256          return;          return;
# Line 2409  sub _tree_construction_main ($) { Line 3262  sub _tree_construction_main ($) {
3262          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
3263          if ($node->[0] eq $formatting_element->[0]) {          if ($node->[0] eq $formatting_element->[0]) {
3264            if ($in_scope) {            if ($in_scope) {
3265                !!!cp ('t54');
3266              $formatting_element_i_in_open = $_;              $formatting_element_i_in_open = $_;
3267              last INSCOPE;              last INSCOPE;
3268            } else { # in open elements but not in scope            } else { # in open elements but not in scope
3269              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t55');
3270                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name},
3271                                token => $end_tag_token);
3272              ## Ignore the token              ## Ignore the token
3273              !!!next-token;              !!!next-token;
3274              return;              return;
3275            }            }
3276          } elsif ({          } elsif ($node->[1] & SCOPING_EL) {
3277                    table => 1, caption => 1, td => 1, th => 1,            !!!cp ('t56');
                   button => 1, marquee => 1, object => 1, html => 1,  
                  }->{$node->[1]}) {  
3278            $in_scope = 0;            $in_scope = 0;
3279          }          }
3280        } # INSCOPE        } # INSCOPE
3281        unless (defined $formatting_element_i_in_open) {        unless (defined $formatting_element_i_in_open) {
3282          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!cp ('t57');
3283            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name},
3284                            token => $end_tag_token);
3285          pop @$active_formatting_elements; # $formatting_element          pop @$active_formatting_elements; # $formatting_element
3286          !!!next-token; ## TODO: ok?          !!!next-token; ## TODO: ok?
3287          return;          return;
3288        }        }
3289        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
3290          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);          !!!cp ('t58');
3291            !!!parse-error (type => 'not closed',
3292                            value => $self->{open_elements}->[-1]->[0]
3293                                ->manakai_local_name,
3294                            token => $end_tag_token);
3295        }        }
3296                
3297        ## Step 2        ## Step 2
# Line 2439  sub _tree_construction_main ($) { Line 3299  sub _tree_construction_main ($) {
3299        my $furthest_block_i_in_open;        my $furthest_block_i_in_open;
3300        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3301          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
3302          if (not $formatting_category->{$node->[1]} and          if (not ($node->[1] & FORMATTING_EL) and
3303              #not $phrasing_category->{$node->[1]} and              #not $phrasing_category->{$node->[1]} and
3304              ($special_category->{$node->[1]} or              ($node->[1] & SPECIAL_EL or
3305               $scoping_category->{$node->[1]})) {               $node->[1] & SCOPING_EL)) { ## Scoping is redundant, maybe
3306              !!!cp ('t59');
3307            $furthest_block = $node;            $furthest_block = $node;
3308            $furthest_block_i_in_open = $_;            $furthest_block_i_in_open = $_;
3309          } elsif ($node->[0] eq $formatting_element->[0]) {          } elsif ($node->[0] eq $formatting_element->[0]) {
3310              !!!cp ('t60');
3311            last OE;            last OE;
3312          }          }
3313        } # OE        } # OE
3314                
3315        ## Step 3        ## Step 3
3316        unless (defined $furthest_block) { # MUST        unless (defined $furthest_block) { # MUST
3317            !!!cp ('t61');
3318          splice @{$self->{open_elements}}, $formatting_element_i_in_open;          splice @{$self->{open_elements}}, $formatting_element_i_in_open;
3319          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;
3320          !!!next-token;          !!!next-token;
# Line 2464  sub _tree_construction_main ($) { Line 3327  sub _tree_construction_main ($) {
3327        ## Step 5        ## Step 5
3328        my $furthest_block_parent = $furthest_block->[0]->parent_node;        my $furthest_block_parent = $furthest_block->[0]->parent_node;
3329        if (defined $furthest_block_parent) {        if (defined $furthest_block_parent) {
3330            !!!cp ('t62');
3331          $furthest_block_parent->remove_child ($furthest_block->[0]);          $furthest_block_parent->remove_child ($furthest_block->[0]);
3332        }        }
3333                
# Line 2486  sub _tree_construction_main ($) { Line 3350  sub _tree_construction_main ($) {
3350          S7S2: {          S7S2: {
3351            for (reverse 0..$#$active_formatting_elements) {            for (reverse 0..$#$active_formatting_elements) {
3352              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
3353                  !!!cp ('t63');
3354                $node_i_in_active = $_;                $node_i_in_active = $_;
3355                last S7S2;                last S7S2;
3356              }              }
# Line 2499  sub _tree_construction_main ($) { Line 3364  sub _tree_construction_main ($) {
3364                    
3365          ## Step 4          ## Step 4
3366          if ($last_node->[0] eq $furthest_block->[0]) {          if ($last_node->[0] eq $furthest_block->[0]) {
3367              !!!cp ('t64');
3368            $bookmark_prev_el = $node->[0];            $bookmark_prev_el = $node->[0];
3369          }          }
3370                    
3371          ## Step 5          ## Step 5
3372          if ($node->[0]->has_child_nodes ()) {          if ($node->[0]->has_child_nodes ()) {
3373              !!!cp ('t65');
3374            my $clone = [$node->[0]->clone_node (0), $node->[1]];            my $clone = [$node->[0]->clone_node (0), $node->[1]];
3375            $active_formatting_elements->[$node_i_in_active] = $clone;            $active_formatting_elements->[$node_i_in_active] = $clone;
3376            $self->{open_elements}->[$node_i_in_open] = $clone;            $self->{open_elements}->[$node_i_in_open] = $clone;
# Line 2521  sub _tree_construction_main ($) { Line 3388  sub _tree_construction_main ($) {
3388        } # S7          } # S7  
3389                
3390        ## Step 8        ## Step 8
3391        $common_ancestor_node->[0]->append_child ($last_node->[0]);        if ($common_ancestor_node->[1] & TABLE_ROWS_EL) {
3392            my $foster_parent_element;
3393            my $next_sibling;
3394            OE: for (reverse 0..$#{$self->{open_elements}}) {
3395              if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
3396                                 my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3397                                 if (defined $parent and $parent->node_type == 1) {
3398                                   !!!cp ('t65.1');
3399                                   $foster_parent_element = $parent;
3400                                   $next_sibling = $self->{open_elements}->[$_]->[0];
3401                                 } else {
3402                                   !!!cp ('t65.2');
3403                                   $foster_parent_element
3404                                     = $self->{open_elements}->[$_ - 1]->[0];
3405                                 }
3406                                 last OE;
3407                               }
3408                             } # OE
3409                             $foster_parent_element = $self->{open_elements}->[0]->[0]
3410                               unless defined $foster_parent_element;
3411            $foster_parent_element->insert_before ($last_node->[0], $next_sibling);
3412            $open_tables->[-1]->[1] = 1; # tainted
3413          } else {
3414            !!!cp ('t65.3');
3415            $common_ancestor_node->[0]->append_child ($last_node->[0]);
3416          }
3417                
3418        ## Step 9        ## Step 9
3419        my $clone = [$formatting_element->[0]->clone_node (0),        my $clone = [$formatting_element->[0]->clone_node (0),
# Line 2538  sub _tree_construction_main ($) { Line 3430  sub _tree_construction_main ($) {
3430        my $i;        my $i;
3431        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3432          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {
3433              !!!cp ('t66');
3434            splice @$active_formatting_elements, $_, 1;            splice @$active_formatting_elements, $_, 1;
3435            $i-- and last AFE if defined $i;            $i-- and last AFE if defined $i;
3436          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {
3437              !!!cp ('t67');
3438            $i = $_;            $i = $_;
3439          }          }
3440        } # AFE        } # AFE
# Line 2550  sub _tree_construction_main ($) { Line 3444  sub _tree_construction_main ($) {
3444        undef $i;        undef $i;
3445        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3446          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {
3447              !!!cp ('t68');
3448            splice @{$self->{open_elements}}, $_, 1;            splice @{$self->{open_elements}}, $_, 1;
3449            $i-- and last OE if defined $i;            $i-- and last OE if defined $i;
3450          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {
3451              !!!cp ('t69');
3452            $i = $_;            $i = $_;
3453          }          }
3454        } # OE        } # OE
# Line 2563  sub _tree_construction_main ($) { Line 3459  sub _tree_construction_main ($) {
3459      } # FET      } # FET
3460    }; # $formatting_end_tag    }; # $formatting_end_tag
3461    
3462    my $insert_to_current = sub {    $insert = my $insert_to_current = sub {
3463      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);
3464    }; # $insert_to_current    }; # $insert_to_current
3465    
3466    my $insert_to_foster = sub {    my $insert_to_foster = sub {
3467                         my $child = shift;      my $child = shift;
3468                         if ({      if ($self->{open_elements}->[-1]->[1] & TABLE_ROWS_EL) {
3469                              table => 1, tbody => 1, tfoot => 1,        # MUST
3470                              thead => 1, tr => 1,        my $foster_parent_element;
3471                             }->{$self->{open_elements}->[-1]->[1]}) {        my $next_sibling;
3472                           # MUST        OE: for (reverse 0..$#{$self->{open_elements}}) {
3473                           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') {  
3474                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3475                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
3476                                   !!!cp ('t70');
3477                                 $foster_parent_element = $parent;                                 $foster_parent_element = $parent;
3478                                 $next_sibling = $self->{open_elements}->[$_]->[0];                                 $next_sibling = $self->{open_elements}->[$_]->[0];
3479                               } else {                               } else {
3480                                   !!!cp ('t71');
3481                                 $foster_parent_element                                 $foster_parent_element
3482                                   = $self->{open_elements}->[$_ - 1]->[0];                                   = $self->{open_elements}->[$_ - 1]->[0];
3483                               }                               }
# Line 2593  sub _tree_construction_main ($) { Line 3488  sub _tree_construction_main ($) {
3488                             unless defined $foster_parent_element;                             unless defined $foster_parent_element;
3489                           $foster_parent_element->insert_before                           $foster_parent_element->insert_before
3490                             ($child, $next_sibling);                             ($child, $next_sibling);
3491                         } else {        $open_tables->[-1]->[1] = 1; # tainted
3492                           $self->{open_elements}->[-1]->[0]->append_child ($child);      } else {
3493                         }        !!!cp ('t72');
3494          $self->{open_elements}->[-1]->[0]->append_child ($child);
3495        }
3496    }; # $insert_to_foster    }; # $insert_to_foster
3497    
   my $insert;  
   
3498    B: {    B: {
3499      if ($token->{type} == DOCTYPE_TOKEN) {      if ($token->{type} == DOCTYPE_TOKEN) {
3500        !!!parse-error (type => 'DOCTYPE in the middle');        !!!cp ('t73');
3501          !!!parse-error (type => 'DOCTYPE in the middle', token => $token);
3502        ## Ignore the token        ## Ignore the token
3503        ## Stay in the phase        ## Stay in the phase
3504        !!!next-token;        !!!next-token;
3505        redo B;        redo 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;  
3506      } elsif ($token->{type} == START_TAG_TOKEN and      } elsif ($token->{type} == START_TAG_TOKEN and
3507               $token->{tag_name} eq 'html') {               $token->{tag_name} eq 'html') {
3508        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
3509          ## Turn into the main phase          !!!cp ('t79');
3510          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html:html', token => $token);
3511          $self->{insertion_mode} = AFTER_BODY_IM;          $self->{insertion_mode} = AFTER_BODY_IM;
3512        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
3513          ## Turn into the main phase          !!!cp ('t80');
3514          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html:html', token => $token);
3515          $self->{insertion_mode} = AFTER_FRAMESET_IM;          $self->{insertion_mode} = AFTER_FRAMESET_IM;
3516          } else {
3517            !!!cp ('t81');
3518        }        }
3519    
3520  ## ISSUE: "aa<html>" is not a parse error.        !!!cp ('t82');
3521  ## 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');  
       }  
3522        my $top_el = $self->{open_elements}->[0]->[0];        my $top_el = $self->{open_elements}->[0]->[0];
3523        for my $attr_name (keys %{$token->{attributes}}) {        for my $attr_name (keys %{$token->{attributes}}) {
3524          unless ($top_el->has_attribute_ns (undef, $attr_name)) {          unless ($top_el->has_attribute_ns (undef, $attr_name)) {
3525              !!!cp ('t84');
3526            $top_el->set_attribute_ns            $top_el->set_attribute_ns
3527              (undef, [undef, $attr_name],              (undef, [undef, $attr_name],
3528               $token->{attributes}->{$attr_name}->{value});               $token->{attributes}->{$attr_name}->{value});
3529          }          }
3530        }        }
3531          !!!nack ('t84.1');
3532        !!!next-token;        !!!next-token;
3533        redo B;        redo B;
3534      } elsif ($token->{type} == COMMENT_TOKEN) {      } elsif ($token->{type} == COMMENT_TOKEN) {
3535        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
3536        if ($self->{insertion_mode} & AFTER_HTML_IMS) {        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
3537            !!!cp ('t85');
3538          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
3539        } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {        } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {
3540            !!!cp ('t86');
3541          $self->{open_elements}->[0]->[0]->append_child ($comment);          $self->{open_elements}->[0]->[0]->append_child ($comment);
3542        } else {        } else {
3543            !!!cp ('t87');
3544          $self->{open_elements}->[-1]->[0]->append_child ($comment);          $self->{open_elements}->[-1]->[0]->append_child ($comment);
3545        }        }
3546        !!!next-token;        !!!next-token;
# Line 2676  sub _tree_construction_main ($) { Line 3548  sub _tree_construction_main ($) {
3548      } elsif ($self->{insertion_mode} & HEAD_IMS) {      } elsif ($self->{insertion_mode} & HEAD_IMS) {
3549        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
3550          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
3551            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3552                !!!cp ('t88.2');
3553                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
3554              } else {
3555                !!!cp ('t88.1');
3556                ## Ignore the token.
3557                !!!next-token;
3558                redo B;
3559              }
3560            unless (length $token->{data}) {            unless (length $token->{data}) {
3561                !!!cp ('t88');
3562              !!!next-token;              !!!next-token;
3563              redo B;              redo B;
3564            }            }
3565          }          }
3566    
3567          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3568              !!!cp ('t89');
3569            ## As if <head>            ## As if <head>
3570            !!!create-element ($self->{head_element}, 'head');            !!!create-element ($self->{head_element}, 'head',, $token);
3571            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3572            push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            push @{$self->{open_elements}},
3573                  [$self->{head_element}, $el_category->{head}];
3574    
3575            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
3576            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
3577    
3578            ## Reprocess in the "after head" insertion mode...            ## Reprocess in the "after head" insertion mode...
3579          } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {          } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3580              !!!cp ('t90');
3581            ## As if </noscript>            ## As if </noscript>
3582            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
3583            !!!parse-error (type => 'in noscript:#character');            !!!parse-error (type => 'in noscript:#character', token => $token);
3584                        
3585            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
3586            ## As if </head>            ## As if </head>
# Line 2704  sub _tree_construction_main ($) { Line 3588  sub _tree_construction_main ($) {
3588    
3589            ## Reprocess in the "after head" insertion mode...            ## Reprocess in the "after head" insertion mode...
3590          } elsif ($self->{insertion_mode} == IN_HEAD_IM) {          } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3591              !!!cp ('t91');
3592            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
3593    
3594            ## Reprocess in the "after head" insertion mode...            ## Reprocess in the "after head" insertion mode...
3595            } else {
3596              !!!cp ('t92');
3597          }          }
3598    
3599              ## "after head" insertion mode          ## "after head" insertion mode
3600              ## As if <body>          ## As if <body>
3601              !!!insert-element ('body');          !!!insert-element ('body',, $token);
3602              $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
3603              ## reprocess          ## reprocess
3604            redo B;
3605          } elsif ($token->{type} == START_TAG_TOKEN) {
3606            if ($token->{tag_name} eq 'head') {
3607              if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3608                !!!cp ('t93');
3609                !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes}, $token);
3610                $self->{open_elements}->[-1]->[0]->append_child
3611                    ($self->{head_element});
3612                push @{$self->{open_elements}},
3613                    [$self->{head_element}, $el_category->{head}];
3614                $self->{insertion_mode} = IN_HEAD_IM;
3615                !!!nack ('t93.1');
3616                !!!next-token;
3617              redo B;              redo B;
3618            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
3619              if ($token->{tag_name} eq 'head') {              !!!cp ('t94');
3620                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {              #
3621                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});            } else {
3622                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              !!!cp ('t95');
3623                  push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];              !!!parse-error (type => 'in head:head', token => $token); # or in head noscript
3624                  $self->{insertion_mode} = IN_HEAD_IM;              ## Ignore the token
3625                  !!!next-token;              !!!nack ('t95.1');
3626                  redo B;              !!!next-token;
3627                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {              redo B;
3628                  #            }
3629                } else {          } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3630                  !!!parse-error (type => 'in head:head'); # or in head noscript                !!!cp ('t96');
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
             } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {  
3631                ## As if <head>                ## As if <head>
3632                !!!create-element ($self->{head_element}, 'head');                !!!create-element ($self->{head_element}, 'head',, $token);
3633                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3634                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                push @{$self->{open_elements}},
3635                      [$self->{head_element}, $el_category->{head}];
3636    
3637                $self->{insertion_mode} = IN_HEAD_IM;                $self->{insertion_mode} = IN_HEAD_IM;
3638                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
3639                } else {
3640                  !!!cp ('t97');
3641              }              }
3642    
3643              if ($token->{tag_name} eq 'base') {              if ($token->{tag_name} eq 'base') {
3644                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3645                    !!!cp ('t98');
3646                  ## As if </noscript>                  ## As if </noscript>
3647                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3648                  !!!parse-error (type => 'in noscript:base');                  !!!parse-error (type => 'in noscript:base', token => $token);
3649                                
3650                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
3651                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3652                  } else {
3653                    !!!cp ('t99');
3654                }                }
3655    
3656                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3657                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3658                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t100');
3659                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
3660                    push @{$self->{open_elements}},
3661                        [$self->{head_element}, $el_category->{head}];
3662                  } else {
3663                    !!!cp ('t101');
3664                }                }
3665                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
3666                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
3667                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
3668                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
3669                  !!!nack ('t101.1');
3670                !!!next-token;                !!!next-token;
3671                redo B;                redo B;
3672              } elsif ($token->{tag_name} eq 'link') {              } elsif ($token->{tag_name} eq 'link') {
3673                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3674                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3675                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t102');
3676                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
3677                    push @{$self->{open_elements}},
3678                        [$self->{head_element}, $el_category->{head}];
3679                  } else {
3680                    !!!cp ('t103');
3681                }                }
3682                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
3683                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
3684                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
3685                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
3686                  !!!ack ('t103.1');
3687                !!!next-token;                !!!next-token;
3688                redo B;                redo B;
3689              } elsif ($token->{tag_name} eq 'meta') {              } elsif ($token->{tag_name} eq 'meta') {
3690                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3691                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3692                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t104');
3693                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
3694                    push @{$self->{open_elements}},
3695                        [$self->{head_element}, $el_category->{head}];
3696                  } else {
3697                    !!!cp ('t105');
3698                }                }
3699                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
3700                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.
3701    
3702                unless ($self->{confident}) {                unless ($self->{confident}) {
                 my $charset;  
3703                  if ($token->{attributes}->{charset}) { ## TODO: And if supported                  if ($token->{attributes}->{charset}) { ## TODO: And if supported
3704                    $charset = $token->{attributes}->{charset}->{value};                    !!!cp ('t106');
3705                  }                    $self->{change_encoding}
3706                  if ($token->{attributes}->{'http-equiv'}) {                        ->($self, $token->{attributes}->{charset}->{value},
3707                             $token);
3708                      
3709                      $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
3710                          ->set_user_data (manakai_has_reference =>
3711                                               $token->{attributes}->{charset}
3712                                                   ->{has_reference});
3713                    } elsif ($token->{attributes}->{content}) {
3714                    ## 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.
3715                    if ($token->{attributes}->{'http-equiv'}->{value}                    if ($token->{attributes}->{content}->{value}
3716                        =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=                        =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
3717                              [\x09-\x0D\x20]*=
3718                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
3719                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
3720                      $charset = defined $1 ? $1 : defined $2 ? $2 : $3;                      !!!cp ('t107');
3721                    } ## TODO: And if supported                      $self->{change_encoding}
3722                            ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,
3723                               $token);
3724                        $meta_el->[0]->get_attribute_node_ns (undef, 'content')
3725                            ->set_user_data (manakai_has_reference =>
3726                                                 $token->{attributes}->{content}
3727                                                       ->{has_reference});
3728                      } else {
3729                        !!!cp ('t108');
3730                      }
3731                    }
3732                  } else {
3733                    if ($token->{attributes}->{charset}) {
3734                      !!!cp ('t109');
3735                      $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
3736                          ->set_user_data (manakai_has_reference =>
3737                                               $token->{attributes}->{charset}
3738                                                   ->{has_reference});
3739                    }
3740                    if ($token->{attributes}->{content}) {
3741                      !!!cp ('t110');
3742                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')
3743                          ->set_user_data (manakai_has_reference =>
3744                                               $token->{attributes}->{content}
3745                                                   ->{has_reference});
3746                  }                  }
                 ## TODO: Change the encoding  
3747                }                }
3748    
3749                ## TODO: Extracting |charset| from |meta|.                pop @{$self->{open_elements}} # <head>
               pop @{$self->{open_elements}}  
3750                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
3751                  !!!ack ('t110.1');
3752                !!!next-token;                !!!next-token;
3753                redo B;                redo B;
3754              } elsif ($token->{tag_name} eq 'title') {              } elsif ($token->{tag_name} eq 'title') {
3755                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3756                    !!!cp ('t111');
3757                  ## As if </noscript>                  ## As if </noscript>
3758                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3759                  !!!parse-error (type => 'in noscript:title');                  !!!parse-error (type => 'in noscript:title', token => $token);
3760                                
3761                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
3762                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3763                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
3764                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t112');
3765                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
3766                    push @{$self->{open_elements}},
3767                        [$self->{head_element}, $el_category->{head}];
3768                  } else {
3769                    !!!cp ('t113');
3770                }                }
3771    
3772                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3773                my $parent = defined $self->{head_element} ? $self->{head_element}                my $parent = defined $self->{head_element} ? $self->{head_element}
3774                    : $self->{open_elements}->[-1]->[0];                    : $self->{open_elements}->[-1]->[0];
3775                $parse_rcdata->(RCDATA_CONTENT_MODEL,                $parse_rcdata->(RCDATA_CONTENT_MODEL);
3776                                sub { $parent->append_child ($_[0]) });                pop @{$self->{open_elements}} # <head>
               pop @{$self->{open_elements}}  
3777                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
3778                redo B;                redo B;
3779              } elsif ($token->{tag_name} eq 'style') {              } elsif ($token->{tag_name} eq 'style') {
# Line 2832  sub _tree_construction_main ($) { Line 3781  sub _tree_construction_main ($) {
3781                ## insertion mode IN_HEAD_IM)                ## insertion mode IN_HEAD_IM)
3782                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3783                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3784                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t114');
3785                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
3786                    push @{$self->{open_elements}},
3787                        [$self->{head_element}, $el_category->{head}];
3788                  } else {
3789                    !!!cp ('t115');
3790                }                }
3791                $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);                $parse_rcdata->(CDATA_CONTENT_MODEL);
3792                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
3793                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
3794                redo B;                redo B;
3795              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
3796                if ($self->{insertion_mode} == IN_HEAD_IM) {                if ($self->{insertion_mode} == IN_HEAD_IM) {
3797                    !!!cp ('t116');
3798                  ## NOTE: and scripting is disalbed                  ## NOTE: and scripting is disalbed
3799                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
3800                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
3801                    !!!nack ('t116.1');
3802                  !!!next-token;                  !!!next-token;
3803                  redo B;                  redo B;
3804                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3805                  !!!parse-error (type => 'in noscript:noscript');                  !!!cp ('t117');
3806                    !!!parse-error (type => 'in noscript:noscript', token => $token);
3807                  ## Ignore the token                  ## Ignore the token
3808                    !!!nack ('t117.1');
3809                  !!!next-token;                  !!!next-token;
3810                  redo B;                  redo B;
3811                } else {                } else {
3812                    !!!cp ('t118');
3813                  #                  #
3814                }                }
3815              } elsif ($token->{tag_name} eq 'script') {              } elsif ($token->{tag_name} eq 'script') {
3816                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3817                    !!!cp ('t119');
3818                  ## As if </noscript>                  ## As if </noscript>
3819                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3820                  !!!parse-error (type => 'in noscript:script');                  !!!parse-error (type => 'in noscript:script', token => $token);
3821                                
3822                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
3823                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3824                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
3825                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t120');
3826                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
3827                    push @{$self->{open_elements}},
3828                        [$self->{head_element}, $el_category->{head}];
3829                  } else {
3830                    !!!cp ('t121');
3831                }                }
3832    
3833                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3834                $script_start_tag->($insert_to_current);                $script_start_tag->();
3835                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
3836                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
3837                redo B;                redo B;
3838              } elsif ($token->{tag_name} eq 'body' or              } elsif ($token->{tag_name} eq 'body' or
3839                       $token->{tag_name} eq 'frameset') {                       $token->{tag_name} eq 'frameset') {
3840                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3841                    !!!cp ('t122');
3842                  ## As if </noscript>                  ## As if </noscript>
3843                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3844                  !!!parse-error (type => 'in noscript:'.$token->{tag_name});                  !!!parse-error (type => 'in noscript:'.$token->{tag_name}, token => $token);
3845                                    
3846                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3847                  ## As if </head>                  ## As if </head>
# Line 2885  sub _tree_construction_main ($) { Line 3849  sub _tree_construction_main ($) {
3849                                    
3850                  ## Reprocess in the "after head" insertion mode...                  ## Reprocess in the "after head" insertion mode...
3851                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3852                    !!!cp ('t124');
3853                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3854                                    
3855                  ## Reprocess in the "after head" insertion mode...                  ## Reprocess in the "after head" insertion mode...
3856                  } else {
3857                    !!!cp ('t125');
3858                }                }
3859    
3860                ## "after head" insertion mode                ## "after head" insertion mode
3861                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
3862                if ($token->{tag_name} eq 'body') {                if ($token->{tag_name} eq 'body') {
3863                    !!!cp ('t126');
3864                  $self->{insertion_mode} = IN_BODY_IM;                  $self->{insertion_mode} = IN_BODY_IM;
3865                } elsif ($token->{tag_name} eq 'frameset') {                } elsif ($token->{tag_name} eq 'frameset') {
3866                    !!!cp ('t127');
3867                  $self->{insertion_mode} = IN_FRAMESET_IM;                  $self->{insertion_mode} = IN_FRAMESET_IM;
3868                } else {                } else {
3869                  die "$0: tag name: $self->{tag_name}";                  die "$0: tag name: $self->{tag_name}";
3870                }                }
3871                  !!!nack ('t127.1');
3872                !!!next-token;                !!!next-token;
3873                redo B;                redo B;
3874              } else {              } else {
3875                  !!!cp ('t128');
3876                #                #
3877              }              }
3878    
3879              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3880                  !!!cp ('t129');
3881                ## As if </noscript>                ## As if </noscript>
3882                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3883                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);
3884                                
3885                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
3886                ## As if </head>                ## As if </head>
# Line 2916  sub _tree_construction_main ($) { Line 3888  sub _tree_construction_main ($) {
3888    
3889                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
3890              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3891                  !!!cp ('t130');
3892                ## As if </head>                ## As if </head>
3893                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3894    
3895                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
3896                } else {
3897                  !!!cp ('t131');
3898              }              }
3899    
3900              ## "after head" insertion mode              ## "after head" insertion mode
3901              ## As if <body>              ## As if <body>
3902              !!!insert-element ('body');              !!!insert-element ('body',, $token);
3903              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
3904              ## reprocess              ## reprocess
3905                !!!ack-later;
3906              redo B;              redo B;
3907            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
3908              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
3909                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3910                    !!!cp ('t132');
3911                  ## As if <head>                  ## As if <head>
3912                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, 'head',, $token);
3913                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3914                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
3915                        [$self->{head_element}, $el_category->{head}];
3916    
3917                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3918                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
# Line 2942  sub _tree_construction_main ($) { Line 3920  sub _tree_construction_main ($) {
3920                  !!!next-token;                  !!!next-token;
3921                  redo B;                  redo B;
3922                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3923                    !!!cp ('t133');
3924                  ## As if </noscript>                  ## As if </noscript>
3925                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3926                  !!!parse-error (type => 'in noscript:script');                  !!!parse-error (type => 'in noscript:/head', token => $token);
3927                                    
3928                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3929                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
# Line 2952  sub _tree_construction_main ($) { Line 3931  sub _tree_construction_main ($) {
3931                  !!!next-token;                  !!!next-token;
3932                  redo B;                  redo B;
3933                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3934                    !!!cp ('t134');
3935                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3936                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
3937                  !!!next-token;                  !!!next-token;
3938                  redo B;                  redo B;
3939                } else {                } else {
3940                    !!!cp ('t135');
3941                  #                  #
3942                }                }
3943              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
3944                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3945                    !!!cp ('t136');
3946                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3947                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
3948                  !!!next-token;                  !!!next-token;
3949                  redo B;                  redo B;
3950                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3951                  !!!parse-error (type => 'unmatched end tag:noscript');                  !!!cp ('t137');
3952                    !!!parse-error (type => 'unmatched end tag:noscript', token => $token);
3953                  ## Ignore the token ## ISSUE: An issue in the spec.                  ## Ignore the token ## ISSUE: An issue in the spec.
3954                  !!!next-token;                  !!!next-token;
3955                  redo B;                  redo B;
3956                } else {                } else {
3957                    !!!cp ('t138');
3958                  #                  #
3959                }                }
3960              } elsif ({              } elsif ({
3961                        body => 1, html => 1,                        body => 1, html => 1,
3962                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3963                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3964                    !!!cp ('t139');
3965                  ## As if <head>                  ## As if <head>
3966                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, 'head',, $token);
3967                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3968                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
3969                        [$self->{head_element}, $el_category->{head}];
3970    
3971                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
3972                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3973                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3974                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t140');
3975                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
3976                  ## Ignore the token                  ## Ignore the token
3977                  !!!next-token;                  !!!next-token;
3978                  redo B;                  redo B;
3979                  } else {
3980                    !!!cp ('t141');
3981                }                }
3982                                
3983                #                #
# Line 2996  sub _tree_construction_main ($) { Line 3985  sub _tree_construction_main ($) {
3985                        p => 1, br => 1,                        p => 1, br => 1,
3986                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3987                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3988                    !!!cp ('t142');
3989                  ## As if <head>                  ## As if <head>
3990                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, 'head',, $token);
3991                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3992                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
3993                        [$self->{head_element}, $el_category->{head}];
3994    
3995                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
3996                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3997                  } else {
3998                    !!!cp ('t143');
3999                }                }
4000    
4001                #                #
4002              } else {              } else {
4003                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4004                    !!!cp ('t144');
4005                  #                  #
4006                } else {                } else {
4007                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t145');
4008                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4009                  ## Ignore the token                  ## Ignore the token
4010                  !!!next-token;                  !!!next-token;
4011                  redo B;                  redo B;
# Line 3018  sub _tree_construction_main ($) { Line 4013  sub _tree_construction_main ($) {
4013              }              }
4014    
4015              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4016                  !!!cp ('t146');
4017                ## As if </noscript>                ## As if </noscript>
4018                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4019                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);
4020                                
4021                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
4022                ## As if </head>                ## As if </head>
# Line 3028  sub _tree_construction_main ($) { Line 4024  sub _tree_construction_main ($) {
4024    
4025                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
4026              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4027                  !!!cp ('t147');
4028                ## As if </head>                ## As if </head>
4029                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4030    
4031                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
4032              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4033                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  ## ISSUE: This case cannot be reached?
4034                  !!!cp ('t148');
4035                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4036                ## Ignore the token ## ISSUE: An issue in the spec.                ## Ignore the token ## ISSUE: An issue in the spec.
4037                !!!next-token;                !!!next-token;
4038                redo B;                redo B;
4039                } else {
4040                  !!!cp ('t149');
4041              }              }
4042    
4043              ## "after head" insertion mode              ## "after head" insertion mode
4044              ## As if <body>              ## As if <body>
4045              !!!insert-element ('body');              !!!insert-element ('body',, $token);
4046              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
4047              ## reprocess              ## reprocess
4048              redo B;              redo B;
4049            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4050              die "$0: $token->{type}: Unknown token type";          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4051            }            !!!cp ('t149.1');
4052    
4053              ## NOTE: As if <head>
4054              !!!create-element ($self->{head_element}, 'head',, $token);
4055              $self->{open_elements}->[-1]->[0]->append_child
4056                  ($self->{head_element});
4057              #push @{$self->{open_elements}},
4058              #    [$self->{head_element}, $el_category->{head}];
4059              #$self->{insertion_mode} = IN_HEAD_IM;
4060              ## NOTE: Reprocess.
4061    
4062              ## NOTE: As if </head>
4063              #pop @{$self->{open_elements}};
4064              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4065              ## NOTE: Reprocess.
4066              
4067              #
4068            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4069              !!!cp ('t149.2');
4070    
4071              ## NOTE: As if </head>
4072              pop @{$self->{open_elements}};
4073              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4074              ## NOTE: Reprocess.
4075    
4076              #
4077            } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4078              !!!cp ('t149.3');
4079    
4080              !!!parse-error (type => 'in noscript:#eof', token => $token);
4081    
4082              ## As if </noscript>
4083              pop @{$self->{open_elements}};
4084              #$self->{insertion_mode} = IN_HEAD_IM;
4085              ## NOTE: Reprocess.
4086    
4087              ## NOTE: As if </head>
4088              pop @{$self->{open_elements}};
4089              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4090              ## NOTE: Reprocess.
4091    
4092              #
4093            } else {
4094              !!!cp ('t149.4');
4095              #
4096            }
4097    
4098            ## NOTE: As if <body>
4099            !!!insert-element ('body',, $token);
4100            $self->{insertion_mode} = IN_BODY_IM;
4101            ## NOTE: Reprocess.
4102            redo B;
4103          } else {
4104            die "$0: $token->{type}: Unknown token type";
4105          }
4106    
4107            ## ISSUE: An issue in the spec.            ## ISSUE: An issue in the spec.
4108      } elsif ($self->{insertion_mode} & BODY_IMS) {      } elsif ($self->{insertion_mode} & BODY_IMS) {
4109            if ($token->{type} == CHARACTER_TOKEN) {            if ($token->{type} == CHARACTER_TOKEN) {
4110                !!!cp ('t150');
4111              ## NOTE: There is a code clone of "character in body".              ## NOTE: There is a code clone of "character in body".
4112              $reconstruct_active_formatting_elements->($insert_to_current);              $reconstruct_active_formatting_elements->($insert_to_current);
4113                            
# Line 3066  sub _tree_construction_main ($) { Line 4122  sub _tree_construction_main ($) {
4122                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
4123                if ($self->{insertion_mode} == IN_CELL_IM) {                if ($self->{insertion_mode} == IN_CELL_IM) {
4124                  ## have an element in table scope                  ## have an element in table scope
4125                  my $tn;                  for (reverse 0..$#{$self->{open_elements}}) {
                 INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
4126                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4127                    if ($node->[1] eq 'td' or $node->[1] eq 'th') {                    if ($node->[1] & TABLE_CELL_EL) {
4128                      $tn = $node->[1];                      !!!cp ('t151');
4129                      last INSCOPE;  
4130                    } elsif ({                      ## Close the cell
4131                              table => 1, html => 1,                      !!!back-token; # <x>
4132                             }->{$node->[1]}) {                      $token = {type => END_TAG_TOKEN,
4133                      last INSCOPE;                                tag_name => $node->[0]->manakai_local_name,
4134                    }                                line => $token->{line},
4135                  } # INSCOPE                                column => $token->{column}};
                   unless (defined $tn) {  
                     !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                     ## Ignore the token  
                     !!!next-token;  
4136                      redo B;                      redo B;
4137                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4138                        !!!cp ('t152');
4139                        ## ISSUE: This case can never be reached, maybe.
4140                        last;
4141                    }                    }
4142                                    }
4143                  ## Close the cell  
4144                  !!!back-token; # <?>                  !!!cp ('t153');
4145                  $token = {type => END_TAG_TOKEN, tag_name => $tn};                  !!!parse-error (type => 'start tag not allowed',
4146                        value => $token->{tag_name}, token => $token);
4147                    ## Ignore the token
4148                    !!!nack ('t153.1');
4149                    !!!next-token;
4150                  redo B;                  redo B;
4151                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4152                  !!!parse-error (type => 'not closed:caption');                  !!!parse-error (type => 'not closed:caption', token => $token);
4153                                    
4154                  ## As if </caption>                  ## NOTE: As if </caption>.
4155                  ## have a table element in table scope                  ## have a table element in table scope
4156                  my $i;                  my $i;
4157                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
4158                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
4159                    if ($node->[1] eq 'caption') {                      my $node = $self->{open_elements}->[$_];
4160                      $i = $_;                      if ($node->[1] & CAPTION_EL) {
4161                      last INSCOPE;                        !!!cp ('t155');
4162                    } elsif ({                        $i = $_;
4163                              table => 1, html => 1,                        last INSCOPE;
4164                             }->{$node->[1]}) {                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4165                      last INSCOPE;                        !!!cp ('t156');
4166                          last;
4167                        }
4168                    }                    }
4169    
4170                      !!!cp ('t157');
4171                      !!!parse-error (type => 'start tag not allowed',
4172                                      value => $token->{tag_name}, token => $token);
4173                      ## Ignore the token
4174                      !!!nack ('t157.1');
4175                      !!!next-token;
4176                      redo B;
4177                  } # INSCOPE                  } # INSCOPE
                   unless (defined $i) {  
                     !!!parse-error (type => 'unmatched end tag:caption');  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
                   }  
4178                                    
4179                  ## generate implied end tags                  ## generate implied end tags
4180                  if ({                  while ($self->{open_elements}->[-1]->[1]
4181                       dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
4182                       td => 1, th => 1, tr => 1,                    !!!cp ('t158');
4183                       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;  
4184                  }                  }
4185    
4186                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4187                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t159');
4188                      !!!parse-error (type => 'not closed',
4189                                      value => $self->{open_elements}->[-1]->[0]
4190                                          ->manakai_local_name,
4191                                      token => $token);
4192                    } else {
4193                      !!!cp ('t160');
4194                  }                  }
4195                                    
4196                  splice @{$self->{open_elements}}, $i;                  splice @{$self->{open_elements}}, $i;
# Line 3138  sub _tree_construction_main ($) { Line 4200  sub _tree_construction_main ($) {
4200                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
4201                                    
4202                  ## reprocess                  ## reprocess
4203                    !!!ack-later;
4204                  redo B;                  redo B;
4205                } else {                } else {
4206                    !!!cp ('t161');
4207                  #                  #
4208                }                }
4209              } else {              } else {
4210                  !!!cp ('t162');
4211                #                #
4212              }              }
4213            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
# Line 3152  sub _tree_construction_main ($) { Line 4217  sub _tree_construction_main ($) {
4217                  my $i;                  my $i;
4218                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4219                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4220                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
4221                        !!!cp ('t163');
4222                      $i = $_;                      $i = $_;
4223                      last INSCOPE;                      last INSCOPE;
4224                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
4225                              table => 1, html => 1,                      !!!cp ('t164');
                            }->{$node->[1]}) {  
4226                      last INSCOPE;                      last INSCOPE;
4227                    }                    }
4228                  } # INSCOPE                  } # INSCOPE
4229                    unless (defined $i) {                    unless (defined $i) {
4230                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!cp ('t165');
4231                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4232                      ## Ignore the token                      ## Ignore the token
4233                      !!!next-token;                      !!!next-token;
4234                      redo B;                      redo B;
4235                    }                    }
4236                                    
4237                  ## generate implied end tags                  ## generate implied end tags
4238                  if ({                  while ($self->{open_elements}->[-1]->[1]
4239                       dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
4240                       td => ($token->{tag_name} eq 'th'),                    !!!cp ('t166');
4241                       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;  
4242                  }                  }
4243                    
4244                  if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {                  if ($self->{open_elements}->[-1]->[0]->manakai_local_name
4245                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                          ne $token->{tag_name}) {
4246                      !!!cp ('t167');
4247                      !!!parse-error (type => 'not closed',
4248                                      value => $self->{open_elements}->[-1]->[0]
4249                                          ->manakai_local_name,
4250                                      token => $token);
4251                    } else {
4252                      !!!cp ('t168');
4253                  }                  }
4254                                    
4255                  splice @{$self->{open_elements}}, $i;                  splice @{$self->{open_elements}}, $i;
# Line 3195  sub _tree_construction_main ($) { Line 4261  sub _tree_construction_main ($) {
4261                  !!!next-token;                  !!!next-token;
4262                  redo B;                  redo B;
4263                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4264                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t169');
4265                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4266                  ## Ignore the token                  ## Ignore the token
4267                  !!!next-token;                  !!!next-token;
4268                  redo B;                  redo B;
4269                } else {                } else {
4270                    !!!cp ('t170');
4271                  #                  #
4272                }                }
4273              } elsif ($token->{tag_name} eq 'caption') {              } elsif ($token->{tag_name} eq 'caption') {
4274                if ($self->{insertion_mode} == IN_CAPTION_IM) {                if ($self->{insertion_mode} == IN_CAPTION_IM) {
4275                  ## have a table element in table scope                  ## have a table element in table scope
4276                  my $i;                  my $i;
4277                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
4278                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
4279                    if ($node->[1] eq $token->{tag_name}) {                      my $node = $self->{open_elements}->[$_];
4280                      $i = $_;                      if ($node->[1] & CAPTION_EL) {
4281                      last INSCOPE;                        !!!cp ('t171');
4282                    } elsif ({                        $i = $_;
4283                              table => 1, html => 1,                        last INSCOPE;
4284                             }->{$node->[1]}) {                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4285                      last INSCOPE;                        !!!cp ('t172');
4286                          last;
4287                        }
4288                    }                    }
4289    
4290                      !!!cp ('t173');
4291                      !!!parse-error (type => 'unmatched end tag',
4292                                      value => $token->{tag_name}, token => $token);
4293                      ## Ignore the token
4294                      !!!next-token;
4295                      redo B;
4296                  } # INSCOPE                  } # INSCOPE
                   unless (defined $i) {  
                     !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
                   }  
4297                                    
4298                  ## generate implied end tags                  ## generate implied end tags
4299                  if ({                  while ($self->{open_elements}->[-1]->[1]
4300                       dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
4301                       td => 1, th => 1, tr => 1,                    !!!cp ('t174');
4302                       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;  
4303                  }                  }
4304                                    
4305                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4306                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t175');
4307                      !!!parse-error (type => 'not closed',
4308                                      value => $self->{open_elements}->[-1]->[0]
4309                                          ->manakai_local_name,
4310                                      token => $token);
4311                    } else {
4312                      !!!cp ('t176');
4313                  }                  }
4314                                    
4315                  splice @{$self->{open_elements}}, $i;                  splice @{$self->{open_elements}}, $i;
# Line 3249  sub _tree_construction_main ($) { Line 4321  sub _tree_construction_main ($) {
4321                  !!!next-token;                  !!!next-token;
4322                  redo B;                  redo B;
4323                } elsif ($self->{insertion_mode} == IN_CELL_IM) {                } elsif ($self->{insertion_mode} == IN_CELL_IM) {
4324                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t177');
4325                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4326                  ## Ignore the token                  ## Ignore the token
4327                  !!!next-token;                  !!!next-token;
4328                  redo B;                  redo B;
4329                } else {                } else {
4330                    !!!cp ('t178');
4331                  #                  #
4332                }                }
4333              } elsif ({              } elsif ({
# Line 3264  sub _tree_construction_main ($) { Line 4338  sub _tree_construction_main ($) {
4338                ## have an element in table scope                ## have an element in table scope
4339                my $i;                my $i;
4340                my $tn;                my $tn;
4341                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: {
4342                  my $node = $self->{open_elements}->[$_];                  for (reverse 0..$#{$self->{open_elements}}) {
4343                  if ($node->[1] eq $token->{tag_name}) {                    my $node = $self->{open_elements}->[$_];
4344                    $i = $_;                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
4345                    last INSCOPE;                      !!!cp ('t179');
4346                  } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {                      $i = $_;
4347                    $tn = $node->[1];  
4348                    ## NOTE: There is exactly one |td| or |th| element                      ## Close the cell
4349                    ## in scope in the stack of open elements by definition.                      !!!back-token; # </x>
4350                  } elsif ({                      $token = {type => END_TAG_TOKEN, tag_name => $tn,
4351                            table => 1, html => 1,                                line => $token->{line},
4352                           }->{$node->[1]}) {                                column => $token->{column}};
4353                    last INSCOPE;                      redo B;
4354                      } elsif ($node->[1] & TABLE_CELL_EL) {
4355                        !!!cp ('t180');
4356                        $tn = $node->[0]->manakai_local_name;
4357                        ## NOTE: There is exactly one |td| or |th| element
4358                        ## in scope in the stack of open elements by definition.
4359                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4360                        ## ISSUE: Can this be reached?
4361                        !!!cp ('t181');
4362                        last;
4363                      }
4364                  }                  }
4365                } # INSCOPE  
4366                unless (defined $i) {                  !!!cp ('t182');
4367                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
4368                        value => $token->{tag_name}, token => $token);
4369                  ## Ignore the token                  ## Ignore the token
4370                  !!!next-token;                  !!!next-token;
4371                  redo B;                  redo B;
4372                }                } # INSCOPE
   
               ## Close the cell  
               !!!back-token; # </?>  
               $token = {type => END_TAG_TOKEN, tag_name => $tn};  
               redo B;  
4373              } elsif ($token->{tag_name} eq 'table' and              } elsif ($token->{tag_name} eq 'table' and
4374                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
4375                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed:caption', token => $token);
4376    
4377                ## As if </caption>                ## As if </caption>
4378                ## have a table element in table scope                ## have a table element in table scope
4379                my $i;                my $i;
4380                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4381                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4382                  if ($node->[1] eq 'caption') {                  if ($node->[1] & CAPTION_EL) {
4383                      !!!cp ('t184');
4384                    $i = $_;                    $i = $_;
4385                    last INSCOPE;                    last INSCOPE;
4386                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
4387                            table => 1, html => 1,                    !!!cp ('t185');
                          }->{$node->[1]}) {  
4388                    last INSCOPE;                    last INSCOPE;
4389                  }                  }
4390                } # INSCOPE                } # INSCOPE
4391                unless (defined $i) {                unless (defined $i) {
4392                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!cp ('t186');
4393                    !!!parse-error (type => 'unmatched end tag:caption', token => $token);
4394                  ## Ignore the token                  ## Ignore the token
4395                  !!!next-token;                  !!!next-token;
4396                  redo B;                  redo B;
4397                }                }
4398                                
4399                ## generate implied end tags                ## generate implied end tags
4400                if ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
4401                     dd => 1, dt => 1, li => 1, p => 1,                  !!!cp ('t187');
4402                     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;  
4403                }                }
4404    
4405                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4406                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t188');
4407                    !!!parse-error (type => 'not closed',
4408                                    value => $self->{open_elements}->[-1]->[0]
4409                                        ->manakai_local_name,
4410                                    token => $token);
4411                  } else {
4412                    !!!cp ('t189');
4413                }                }
4414    
4415                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
# Line 3345  sub _tree_construction_main ($) { Line 4424  sub _tree_construction_main ($) {
4424                        body => 1, col => 1, colgroup => 1, html => 1,                        body => 1, col => 1, colgroup => 1, html => 1,
4425                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4426                if ($self->{insertion_mode} & BODY_TABLE_IMS) {                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
4427                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t190');
4428                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4429                  ## Ignore the token                  ## Ignore the token
4430                  !!!next-token;                  !!!next-token;
4431                  redo B;                  redo B;
4432                } else {                } else {
4433                    !!!cp ('t191');
4434                  #                  #
4435                }                }
4436              } elsif ({              } elsif ({
# Line 3357  sub _tree_construction_main ($) { Line 4438  sub _tree_construction_main ($) {
4438                        thead => 1, tr => 1,                        thead => 1, tr => 1,
4439                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
4440                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
4441                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!cp ('t192');
4442                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4443                ## Ignore the token                ## Ignore the token
4444                !!!next-token;                !!!next-token;
4445                redo B;                redo B;
4446              } else {              } else {
4447                  !!!cp ('t193');
4448                #                #
4449              }              }
4450          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4451            for my $entry (@{$self->{open_elements}}) {
4452              unless ($entry->[1] & ALL_END_TAG_OPTIONAL_EL) {
4453                !!!cp ('t75');
4454                !!!parse-error (type => 'in body:#eof', token => $token);
4455                last;
4456              }
4457            }
4458    
4459            ## Stop parsing.
4460            last B;
4461        } else {        } else {
4462          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
4463        }        }
# Line 3372  sub _tree_construction_main ($) { Line 4466  sub _tree_construction_main ($) {
4466        #        #
4467      } elsif ($self->{insertion_mode} & TABLE_IMS) {      } elsif ($self->{insertion_mode} & TABLE_IMS) {
4468        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
4469              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if (not $open_tables->[-1]->[1] and # tainted
4470                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);              $token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4471              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4472                                
4473                unless (length $token->{data}) {            unless (length $token->{data}) {
4474                  !!!next-token;              !!!cp ('t194');
4475                  redo B;              !!!next-token;
4476                }              redo B;
4477              }            } else {
4478                !!!cp ('t195');
4479              }
4480            }
4481    
4482              !!!parse-error (type => 'in table:#character');              !!!parse-error (type => 'in table:#character', token => $token);
4483    
4484              ## As if in body, but insert into foster parent element              ## As if in body, but insert into foster parent element
4485              ## ISSUE: Spec says that "whenever a node would be inserted              ## ISSUE: Spec says that "whenever a node would be inserted
# Line 3389  sub _tree_construction_main ($) { Line 4487  sub _tree_construction_main ($) {
4487              ## result in a new Text node.              ## result in a new Text node.
4488              $reconstruct_active_formatting_elements->($insert_to_foster);              $reconstruct_active_formatting_elements->($insert_to_foster);
4489                            
4490              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]}) {  
4491                # MUST                # MUST
4492                my $foster_parent_element;                my $foster_parent_element;
4493                my $next_sibling;                my $next_sibling;
4494                my $prev_sibling;                my $prev_sibling;
4495                OE: for (reverse 0..$#{$self->{open_elements}}) {                OE: for (reverse 0..$#{$self->{open_elements}}) {
4496                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
4497                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
4498                    if (defined $parent and $parent->node_type == 1) {                    if (defined $parent and $parent->node_type == 1) {
4499                        !!!cp ('t196');
4500                      $foster_parent_element = $parent;                      $foster_parent_element = $parent;
4501                      $next_sibling = $self->{open_elements}->[$_]->[0];                      $next_sibling = $self->{open_elements}->[$_]->[0];
4502                      $prev_sibling = $next_sibling->previous_sibling;                      $prev_sibling = $next_sibling->previous_sibling;
4503                    } else {                    } else {
4504                        !!!cp ('t197');
4505                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];
4506                      $prev_sibling = $foster_parent_element->last_child;                      $prev_sibling = $foster_parent_element->last_child;
4507                    }                    }
# Line 3416  sub _tree_construction_main ($) { Line 4513  sub _tree_construction_main ($) {
4513                  unless defined $foster_parent_element;                  unless defined $foster_parent_element;
4514                if (defined $prev_sibling and                if (defined $prev_sibling and
4515                    $prev_sibling->node_type == 3) {                    $prev_sibling->node_type == 3) {
4516                    !!!cp ('t198');
4517                  $prev_sibling->manakai_append_text ($token->{data});                  $prev_sibling->manakai_append_text ($token->{data});
4518                } else {                } else {
4519                    !!!cp ('t199');
4520                  $foster_parent_element->insert_before                  $foster_parent_element->insert_before
4521                    ($self->{document}->create_text_node ($token->{data}),                    ($self->{document}->create_text_node ($token->{data}),
4522                     $next_sibling);                     $next_sibling);
4523                }                }
4524              } else {            $open_tables->[-1]->[1] = 1; # tainted
4525                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          } else {
4526              }            !!!cp ('t200');
4527              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4528            }
4529                            
4530              !!!next-token;          !!!next-token;
4531              redo B;          redo B;
4532        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
4533              if ({              if ({
4534                   tr => ($self->{insertion_mode} != IN_ROW_IM),                   tr => ($self->{insertion_mode} != IN_ROW_IM),
# Line 3435  sub _tree_construction_main ($) { Line 4536  sub _tree_construction_main ($) {
4536                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
4537                if ($self->{insertion_mode} == IN_TABLE_IM) {                if ($self->{insertion_mode} == IN_TABLE_IM) {
4538                  ## Clear back to table context                  ## Clear back to table context
4539                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
4540                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
4541                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t201');
4542                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4543                  }                  }
4544                                    
4545                  !!!insert-element ('tbody');                  !!!insert-element ('tbody',, $token);
4546                  $self->{insertion_mode} = IN_TABLE_BODY_IM;                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
4547                  ## reprocess in the "in table body" insertion mode...                  ## reprocess in the "in table body" insertion mode...
4548                }                }
4549    
4550                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
4551                  unless ($token->{tag_name} eq 'tr') {                  unless ($token->{tag_name} eq 'tr') {
4552                    !!!parse-error (type => 'missing start tag:tr');                    !!!cp ('t202');
4553                      !!!parse-error (type => 'missing start tag:tr', token => $token);
4554                  }                  }
4555                                    
4556                  ## Clear back to table body context                  ## Clear back to table body context
4557                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
4558                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
4559                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t203');
4560                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    ## ISSUE: Can this case be reached?
4561                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4562                  }                  }
4563                                    
4564                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
4565                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
4566                    !!!insert-element ($token->{tag_name}, $token->{attributes});                    !!!cp ('t204');
4567                      !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4568                      !!!nack ('t204');
4569                    !!!next-token;                    !!!next-token;
4570                    redo B;                    redo B;
4571                  } else {                  } else {
4572                    !!!insert-element ('tr');                    !!!cp ('t205');
4573                      !!!insert-element ('tr',, $token);
4574                    ## reprocess in the "in row" insertion mode                    ## reprocess in the "in row" insertion mode
4575                  }                  }
4576                  } else {
4577                    !!!cp ('t206');
4578                }                }
4579    
4580                ## Clear back to table row context                ## Clear back to table row context
4581                while (not {                while (not ($self->{open_elements}->[-1]->[1]
4582                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
4583                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t207');
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
4584                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4585                }                }
4586                                
4587                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4588                $self->{insertion_mode} = IN_CELL_IM;                $self->{insertion_mode} = IN_CELL_IM;
4589    
4590                push @$active_formatting_elements, ['#marker', ''];                push @$active_formatting_elements, ['#marker', ''];
4591                                
4592                  !!!nack ('t207.1');
4593                !!!next-token;                !!!next-token;
4594                redo B;                redo B;
4595              } elsif ({              } elsif ({
# Line 3496  sub _tree_construction_main ($) { Line 4603  sub _tree_construction_main ($) {
4603                  my $i;                  my $i;
4604                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4605                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4606                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
4607                        !!!cp ('t208');
4608                      $i = $_;                      $i = $_;
4609                      last INSCOPE;                      last INSCOPE;
4610                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
4611                              table => 1, html => 1,                      !!!cp ('t209');
                            }->{$node->[1]}) {  
4612                      last INSCOPE;                      last INSCOPE;
4613                    }                    }
4614                  } # INSCOPE                  } # INSCOPE
4615                  unless (defined $i) {                  unless (defined $i) {
4616                    !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});                    !!!cp ('t210');
4617    ## TODO: This type is wrong.
4618                      !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name}, token => $token);
4619                    ## Ignore the token                    ## Ignore the token
4620                      !!!nack ('t210.1');
4621                    !!!next-token;                    !!!next-token;
4622                    redo B;                    redo B;
4623                  }                  }
4624                                    
4625                  ## Clear back to table row context                  ## Clear back to table row context
4626                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
4627                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
4628                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t211');
4629                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    ## ISSUE: Can this case be reached?
4630                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4631                  }                  }
4632                                    
4633                  pop @{$self->{open_elements}}; # tr                  pop @{$self->{open_elements}}; # tr
4634                  $self->{insertion_mode} = IN_TABLE_BODY_IM;                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
4635                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
4636                      !!!cp ('t212');
4637                    ## reprocess                    ## reprocess
4638                      !!!ack-later;
4639                    redo B;                    redo B;
4640                  } else {                  } else {
4641                      !!!cp ('t213');
4642                    ## reprocess in the "in table body" insertion mode...                    ## reprocess in the "in table body" insertion mode...
4643                  }                  }
4644                }                }
# Line 3535  sub _tree_construction_main ($) { Line 4648  sub _tree_construction_main ($) {
4648                  my $i;                  my $i;
4649                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4650                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4651                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
4652                         tbody => 1, thead => 1, tfoot => 1,                      !!!cp ('t214');
                       }->{$node->[1]}) {  
4653                      $i = $_;                      $i = $_;
4654                      last INSCOPE;                      last INSCOPE;
4655                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
4656                              table => 1, html => 1,                      !!!cp ('t215');
                            }->{$node->[1]}) {  
4657                      last INSCOPE;                      last INSCOPE;
4658                    }                    }
4659                  } # INSCOPE                  } # INSCOPE
4660                  unless (defined $i) {                  unless (defined $i) {
4661                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!cp ('t216');
4662    ## TODO: This erorr type ios wrong.
4663                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4664                    ## Ignore the token                    ## Ignore the token
4665                      !!!nack ('t216.1');
4666                    !!!next-token;                    !!!next-token;
4667                    redo B;                    redo B;
4668                  }                  }
4669    
4670                  ## Clear back to table body context                  ## Clear back to table body context
4671                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
4672                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
4673                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t217');
4674                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    ## ISSUE: Can this state be reached?
4675                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4676                  }                  }
4677                                    
# Line 3571  sub _tree_construction_main ($) { Line 4685  sub _tree_construction_main ($) {
4685                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4686                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
4687                  ## reprocess in "in table" insertion mode...                  ## reprocess in "in table" insertion mode...
4688                  } else {
4689                    !!!cp ('t218');
4690                }                }
4691    
4692                if ($token->{tag_name} eq 'col') {                if ($token->{tag_name} eq 'col') {
4693                  ## Clear back to table context                  ## Clear back to table context
4694                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
4695                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
4696                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t219');
4697                      ## ISSUE: Can this state be reached?
4698                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4699                  }                  }
4700                                    
4701                  !!!insert-element ('colgroup');                  !!!insert-element ('colgroup',, $token);
4702                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
4703                  ## reprocess                  ## reprocess
4704                    !!!ack-later;
4705                  redo B;                  redo B;
4706                } elsif ({                } elsif ({
4707                          caption => 1,                          caption => 1,
# Line 3591  sub _tree_construction_main ($) { Line 4709  sub _tree_construction_main ($) {
4709                          tbody => 1, tfoot => 1, thead => 1,                          tbody => 1, tfoot => 1, thead => 1,
4710                         }->{$token->{tag_name}}) {                         }->{$token->{tag_name}}) {
4711                  ## Clear back to table context                  ## Clear back to table context
4712                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
4713                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
4714                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t220');
4715                      ## ISSUE: Can this state be reached?
4716                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4717                  }                  }
4718                                    
4719                  push @$active_formatting_elements, ['#marker', '']                  push @$active_formatting_elements, ['#marker', '']
4720                      if $token->{tag_name} eq 'caption';                      if $token->{tag_name} eq 'caption';
4721                                    
4722                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4723                  $self->{insertion_mode} = {                  $self->{insertion_mode} = {
4724                                             caption => IN_CAPTION_IM,                                             caption => IN_CAPTION_IM,
4725                                             colgroup => IN_COLUMN_GROUP_IM,                                             colgroup => IN_COLUMN_GROUP_IM,
# Line 3609  sub _tree_construction_main ($) { Line 4728  sub _tree_construction_main ($) {
4728                                             thead => IN_TABLE_BODY_IM,                                             thead => IN_TABLE_BODY_IM,
4729                                            }->{$token->{tag_name}};                                            }->{$token->{tag_name}};
4730                  !!!next-token;                  !!!next-token;
4731                    !!!nack ('t220.1');
4732                  redo B;                  redo B;
4733                } else {                } else {
4734                  die "$0: in table: <>: $token->{tag_name}";                  die "$0: in table: <>: $token->{tag_name}";
4735                }                }
4736              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
4737                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed',
4738                                  value => $self->{open_elements}->[-1]->[0]
4739                                      ->manakai_local_name,
4740                                  token => $token);
4741    
4742                ## As if </table>                ## As if </table>
4743                ## have a table element in table scope                ## have a table element in table scope
4744                my $i;                my $i;
4745                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4746                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4747                  if ($node->[1] eq 'table') {                  if ($node->[1] & TABLE_EL) {
4748                      !!!cp ('t221');
4749                    $i = $_;                    $i = $_;
4750                    last INSCOPE;                    last INSCOPE;
4751                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
4752                            table => 1, html => 1,                    !!!cp ('t222');
                          }->{$node->[1]}) {  
4753                    last INSCOPE;                    last INSCOPE;
4754                  }                  }
4755                } # INSCOPE                } # INSCOPE
4756                unless (defined $i) {                unless (defined $i) {
4757                  !!!parse-error (type => 'unmatched end tag:table');                  !!!cp ('t223');
4758    ## TODO: The following is wrong, maybe.
4759                    !!!parse-error (type => 'unmatched end tag:table', token => $token);
4760                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
4761                    !!!nack ('t223.1');
4762                  !!!next-token;                  !!!next-token;
4763                  redo B;                  redo B;
4764                }                }
4765                                
4766    ## TODO: Followings are removed from the latest spec.
4767                ## generate implied end tags                ## generate implied end tags
4768                if ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
4769                     dd => 1, dt => 1, li => 1, p => 1,                  !!!cp ('t224');
4770                     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;  
4771                }                }
4772    
4773                if ($self->{open_elements}->[-1]->[1] ne 'table') {                unless ($self->{open_elements}->[-1]->[1] & TABLE_EL) {
4774                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t225');
4775                    ## NOTE: |<table><tr><table>|
4776                    !!!parse-error (type => 'not closed',
4777                                    value => $self->{open_elements}->[-1]->[0]
4778                                        ->manakai_local_name,
4779                                    token => $token);
4780                  } else {
4781                    !!!cp ('t226');
4782                }                }
4783    
4784                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
4785                  pop @{$open_tables};
4786    
4787                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
4788    
4789                ## reprocess            ## reprocess
4790                redo B;            !!!ack-later;
4791          } else {            redo B;
4792            !!!parse-error (type => 'in table:'.$token->{tag_name});          } elsif ($token->{tag_name} eq 'style') {
4793              if (not $open_tables->[-1]->[1]) { # tainted
4794                !!!cp ('t227.8');
4795                ## NOTE: This is a "as if in head" code clone.
4796                $parse_rcdata->(CDATA_CONTENT_MODEL);
4797                redo B;
4798              } else {
4799                !!!cp ('t227.7');
4800                #
4801              }
4802            } elsif ($token->{tag_name} eq 'script') {
4803              if (not $open_tables->[-1]->[1]) { # tainted
4804                !!!cp ('t227.6');
4805                ## NOTE: This is a "as if in head" code clone.
4806                $script_start_tag->();
4807                redo B;
4808              } else {
4809                !!!cp ('t227.5');
4810                #
4811              }
4812            } elsif ($token->{tag_name} eq 'input') {
4813              if (not $open_tables->[-1]->[1]) { # tainted
4814                if ($token->{attributes}->{type}) { ## TODO: case
4815                  my $type = lc $token->{attributes}->{type}->{value};
4816                  if ($type eq 'hidden') {
4817                    !!!cp ('t227.3');
4818                    !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);
4819    
4820            $insert = $insert_to_foster;                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4821    
4822                    ## TODO: form element pointer
4823    
4824                    pop @{$self->{open_elements}};
4825    
4826                    !!!next-token;
4827                    !!!ack ('t227.2.1');
4828                    redo B;
4829                  } else {
4830                    !!!cp ('t227.2');
4831                    #
4832                  }
4833                } else {
4834                  !!!cp ('t227.1');
4835                  #
4836                }
4837              } else {
4838                !!!cp ('t227.4');
4839                #
4840              }
4841            } else {
4842              !!!cp ('t227');
4843            #            #
4844          }          }
4845    
4846            !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);
4847    
4848            $insert = $insert_to_foster;
4849            #
4850        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
4851              if ($token->{tag_name} eq 'tr' and              if ($token->{tag_name} eq 'tr' and
4852                  $self->{insertion_mode} == IN_ROW_IM) {                  $self->{insertion_mode} == IN_ROW_IM) {
# Line 3674  sub _tree_construction_main ($) { Line 4854  sub _tree_construction_main ($) {
4854                my $i;                my $i;
4855                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4856                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4857                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_ROW_EL) {
4858                      !!!cp ('t228');
4859                    $i = $_;                    $i = $_;
4860                    last INSCOPE;                    last INSCOPE;
4861                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
4862                            table => 1, html => 1,                    !!!cp ('t229');
                          }->{$node->[1]}) {  
4863                    last INSCOPE;                    last INSCOPE;
4864                  }                  }
4865                } # INSCOPE                } # INSCOPE
4866                unless (defined $i) {                unless (defined $i) {
4867                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t230');
4868                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4869                  ## Ignore the token                  ## Ignore the token
4870                    !!!nack ('t230.1');
4871                  !!!next-token;                  !!!next-token;
4872                  redo B;                  redo B;
4873                  } else {
4874                    !!!cp ('t232');
4875                }                }
4876    
4877                ## Clear back to table row context                ## Clear back to table row context
4878                while (not {                while (not ($self->{open_elements}->[-1]->[1]
4879                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
4880                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t231');
4881                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this state be reached?
4882                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4883                }                }
4884    
4885                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}}; # tr
4886                $self->{insertion_mode} = IN_TABLE_BODY_IM;                $self->{insertion_mode} = IN_TABLE_BODY_IM;
4887                !!!next-token;                !!!next-token;
4888                  !!!nack ('t231.1');
4889                redo B;                redo B;
4890              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
4891                if ($self->{insertion_mode} == IN_ROW_IM) {                if ($self->{insertion_mode} == IN_ROW_IM) {
# Line 3709  sub _tree_construction_main ($) { Line 4894  sub _tree_construction_main ($) {
4894                  my $i;                  my $i;
4895                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4896                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4897                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
4898                        !!!cp ('t233');
4899                      $i = $_;                      $i = $_;
4900                      last INSCOPE;                      last INSCOPE;
4901                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
4902                              table => 1, html => 1,                      !!!cp ('t234');
                            }->{$node->[1]}) {  
4903                      last INSCOPE;                      last INSCOPE;
4904                    }                    }
4905                  } # INSCOPE                  } # INSCOPE
4906                  unless (defined $i) {                  unless (defined $i) {
4907                    !!!parse-error (type => 'unmatched end tag:'.$token->{type});                    !!!cp ('t235');
4908    ## TODO: The following is wrong.
4909                      !!!parse-error (type => 'unmatched end tag:'.$token->{type}, token => $token);
4910                    ## Ignore the token                    ## Ignore the token
4911                      !!!nack ('t236.1');
4912                    !!!next-token;                    !!!next-token;
4913                    redo B;                    redo B;
4914                  }                  }
4915                                    
4916                  ## Clear back to table row context                  ## Clear back to table row context
4917                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
4918                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
4919                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t236');
4920                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this state be reached?
4921                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4922                  }                  }
4923                                    
# Line 3743  sub _tree_construction_main ($) { Line 4931  sub _tree_construction_main ($) {
4931                  my $i;                  my $i;
4932                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4933                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4934                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
4935                         tbody => 1, thead => 1, tfoot => 1,                      !!!cp ('t237');
                       }->{$node->[1]}) {  
4936                      $i = $_;                      $i = $_;
4937                      last INSCOPE;                      last INSCOPE;
4938                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
4939                              table => 1, html => 1,                      !!!cp ('t238');
                            }->{$node->[1]}) {  
4940                      last INSCOPE;                      last INSCOPE;
4941                    }                    }
4942                  } # INSCOPE                  } # INSCOPE
4943                  unless (defined $i) {                  unless (defined $i) {
4944                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!cp ('t239');
4945                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4946                    ## Ignore the token                    ## Ignore the token
4947                      !!!nack ('t239.1');
4948                    !!!next-token;                    !!!next-token;
4949                    redo B;                    redo B;
4950                  }                  }
4951                                    
4952                  ## Clear back to table body context                  ## Clear back to table body context
4953                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
4954                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
4955                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t240');
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
4956                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4957                  }                  }
4958                                    
# Line 3781  sub _tree_construction_main ($) { Line 4968  sub _tree_construction_main ($) {
4968                  ## reprocess in the "in table" insertion mode...                  ## reprocess in the "in table" insertion mode...
4969                }                }
4970    
4971                  ## NOTE: </table> in the "in table" insertion mode.
4972                  ## When you edit the code fragment below, please ensure that
4973                  ## the code for <table> in the "in table" insertion mode
4974                  ## is synced with it.
4975    
4976                ## have a table element in table scope                ## have a table element in table scope
4977                my $i;                my $i;
4978                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4979                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4980                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_EL) {
4981                      !!!cp ('t241');
4982                    $i = $_;                    $i = $_;
4983                    last INSCOPE;                    last INSCOPE;
4984                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
4985                            table => 1, html => 1,                    !!!cp ('t242');
                          }->{$node->[1]}) {  
4986                    last INSCOPE;                    last INSCOPE;
4987                  }                  }
4988                } # INSCOPE                } # INSCOPE
4989                unless (defined $i) {                unless (defined $i) {
4990                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t243');
4991                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4992                  ## Ignore the token                  ## Ignore the token
4993                    !!!nack ('t243.1');
4994                  !!!next-token;                  !!!next-token;
4995                  redo B;                  redo B;
4996                }                }
   
               ## 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]);  
               }  
4997                                    
4998                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
4999                  pop @{$open_tables};
5000                                
5001                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5002                                
# Line 3832  sub _tree_construction_main ($) { Line 5011  sub _tree_construction_main ($) {
5011                  my $i;                  my $i;
5012                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5013                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5014                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5015                        !!!cp ('t247');
5016                      $i = $_;                      $i = $_;
5017                      last INSCOPE;                      last INSCOPE;
5018                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5019                              table => 1, html => 1,                      !!!cp ('t248');
                            }->{$node->[1]}) {  
5020                      last INSCOPE;                      last INSCOPE;
5021                    }                    }
5022                  } # INSCOPE                  } # INSCOPE
5023                    unless (defined $i) {                    unless (defined $i) {
5024                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!cp ('t249');
5025                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5026                      ## Ignore the token                      ## Ignore the token
5027                        !!!nack ('t249.1');
5028                      !!!next-token;                      !!!next-token;
5029                      redo B;                      redo B;
5030                    }                    }
# Line 3853  sub _tree_construction_main ($) { Line 5034  sub _tree_construction_main ($) {
5034                  my $i;                  my $i;
5035                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5036                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5037                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5038                        !!!cp ('t250');
5039                      $i = $_;                      $i = $_;
5040                      last INSCOPE;                      last INSCOPE;
5041                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5042                              table => 1, html => 1,                      !!!cp ('t251');
                            }->{$node->[1]}) {  
5043                      last INSCOPE;                      last INSCOPE;
5044                    }                    }
5045                  } # INSCOPE                  } # INSCOPE
5046                    unless (defined $i) {                    unless (defined $i) {
5047                      !!!parse-error (type => 'unmatched end tag:tr');                      !!!cp ('t252');
5048                        !!!parse-error (type => 'unmatched end tag:tr', token => $token);
5049                      ## Ignore the token                      ## Ignore the token
5050                        !!!nack ('t252.1');
5051                      !!!next-token;                      !!!next-token;
5052                      redo B;                      redo B;
5053                    }                    }
5054                                    
5055                  ## Clear back to table row context                  ## Clear back to table row context
5056                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5057                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
5058                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t253');
5059                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this case be reached?
5060                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5061                  }                  }
5062                                    
# Line 3886  sub _tree_construction_main ($) { Line 5069  sub _tree_construction_main ($) {
5069                my $i;                my $i;
5070                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5071                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5072                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5073                      !!!cp ('t254');
5074                    $i = $_;                    $i = $_;
5075                    last INSCOPE;                    last INSCOPE;
5076                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5077                            table => 1, html => 1,                    !!!cp ('t255');
                          }->{$node->[1]}) {  
5078                    last INSCOPE;                    last INSCOPE;
5079                  }                  }
5080                } # INSCOPE                } # INSCOPE
5081                unless (defined $i) {                unless (defined $i) {
5082                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t256');
5083                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5084                  ## Ignore the token                  ## Ignore the token
5085                    !!!nack ('t256.1');
5086                  !!!next-token;                  !!!next-token;
5087                  redo B;                  redo B;
5088                }                }
5089    
5090                ## Clear back to table body context                ## Clear back to table body context
5091                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5092                  tbody => 1, tfoot => 1, thead => 1, html => 1,                                & TABLE_ROWS_SCOPING_EL)) {
5093                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t257');
5094                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this case be reached?
5095                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5096                }                }
5097    
5098                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5099                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
5100                  !!!nack ('t257.1');
5101                !!!next-token;                !!!next-token;
5102                redo B;                redo B;
5103              } elsif ({              } elsif ({
# Line 3920  sub _tree_construction_main ($) { Line 5106  sub _tree_construction_main ($) {
5106                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM
5107                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
5108                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5109                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!cp ('t258');
5110                ## Ignore the token            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5111                !!!next-token;            ## Ignore the token
5112                redo B;            !!!nack ('t258.1');
5113               !!!next-token;
5114              redo B;
5115          } else {          } else {
5116            !!!parse-error (type => 'in table:/'.$token->{tag_name});            !!!cp ('t259');
5117              !!!parse-error (type => 'in table:/'.$token->{tag_name}, token => $token);
5118    
5119            $insert = $insert_to_foster;            $insert = $insert_to_foster;
5120            #            #
5121          }          }
5122          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5123            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
5124                    @{$self->{open_elements}} == 1) { # redundant, maybe
5125              !!!parse-error (type => 'in body:#eof', token => $token);
5126              !!!cp ('t259.1');
5127              #
5128            } else {
5129              !!!cp ('t259.2');
5130              #
5131            }
5132    
5133            ## Stop parsing
5134            last B;
5135        } else {        } else {
5136          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5137        }        }
# Line 3938  sub _tree_construction_main ($) { Line 5140  sub _tree_construction_main ($) {
5140              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5141                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5142                unless (length $token->{data}) {                unless (length $token->{data}) {
5143                    !!!cp ('t260');
5144                  !!!next-token;                  !!!next-token;
5145                  redo B;                  redo B;
5146                }                }
5147              }              }
5148                            
5149                !!!cp ('t261');
5150              #              #
5151            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
5152              if ($token->{tag_name} eq 'col') {              if ($token->{tag_name} eq 'col') {
5153                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!cp ('t262');
5154                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5155                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5156                  !!!ack ('t262.1');
5157                !!!next-token;                !!!next-token;
5158                redo B;                redo B;
5159              } else {              } else {
5160                  !!!cp ('t263');
5161                #                #
5162              }              }
5163            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
5164              if ($token->{tag_name} eq 'colgroup') {              if ($token->{tag_name} eq 'colgroup') {
5165                if ($self->{open_elements}->[-1]->[1] eq 'html') {                if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5166                  !!!parse-error (type => 'unmatched end tag:colgroup');                  !!!cp ('t264');
5167                    !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);
5168                  ## Ignore the token                  ## Ignore the token
5169                  !!!next-token;                  !!!next-token;
5170                  redo B;                  redo B;
5171                } else {                } else {
5172                    !!!cp ('t265');
5173                  pop @{$self->{open_elements}}; # colgroup                  pop @{$self->{open_elements}}; # colgroup
5174                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
5175                  !!!next-token;                  !!!next-token;
5176                  redo B;                              redo B;            
5177                }                }
5178              } elsif ($token->{tag_name} eq 'col') {              } elsif ($token->{tag_name} eq 'col') {
5179                !!!parse-error (type => 'unmatched end tag:col');                !!!cp ('t266');
5180                  !!!parse-error (type => 'unmatched end tag:col', token => $token);
5181                ## Ignore the token                ## Ignore the token
5182                !!!next-token;                !!!next-token;
5183                redo B;                redo B;
5184              } else {              } else {
5185                  !!!cp ('t267');
5186                #                #
5187              }              }
5188            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5189              #          if ($self->{open_elements}->[-1]->[1] & HTML_EL and
5190            }              @{$self->{open_elements}} == 1) { # redundant, maybe
5191              !!!cp ('t270.2');
5192              ## Stop parsing.
5193              last B;
5194            } else {
5195              ## NOTE: As if </colgroup>.
5196              !!!cp ('t270.1');
5197              pop @{$self->{open_elements}}; # colgroup
5198              $self->{insertion_mode} = IN_TABLE_IM;
5199              ## Reprocess.
5200              redo B;
5201            }
5202          } else {
5203            die "$0: $token->{type}: Unknown token type";
5204          }
5205    
5206            ## As if </colgroup>            ## As if </colgroup>
5207            if ($self->{open_elements}->[-1]->[1] eq 'html') {            if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5208              !!!parse-error (type => 'unmatched end tag:colgroup');              !!!cp ('t269');
5209    ## TODO: Wrong error type?
5210                !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);
5211              ## Ignore the token              ## Ignore the token
5212                !!!nack ('t269.1');
5213              !!!next-token;              !!!next-token;
5214              redo B;              redo B;
5215            } else {            } else {
5216                !!!cp ('t270');
5217              pop @{$self->{open_elements}}; # colgroup              pop @{$self->{open_elements}}; # colgroup
5218              $self->{insertion_mode} = IN_TABLE_IM;              $self->{insertion_mode} = IN_TABLE_IM;
5219                !!!ack-later;
5220              ## reprocess              ## reprocess
5221              redo B;              redo B;
5222            }            }
5223      } elsif ($self->{insertion_mode} == IN_SELECT_IM) {      } elsif ($self->{insertion_mode} & SELECT_IMS) {
5224        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
5225            !!!cp ('t271');
5226          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5227          !!!next-token;          !!!next-token;
5228          redo B;          redo B;
5229        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5230              if ($token->{tag_name} eq 'option') {          if ($token->{tag_name} eq 'option') {
5231                if ($self->{open_elements}->[-1]->[1] eq 'option') {            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5232                  ## As if </option>              !!!cp ('t272');
5233                  pop @{$self->{open_elements}};              ## As if </option>
5234                }              pop @{$self->{open_elements}};
5235              } else {
5236                !!!cp ('t273');
5237              }
5238    
5239                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5240                !!!next-token;            !!!nack ('t273.1');
5241                redo B;            !!!next-token;
5242              } elsif ($token->{tag_name} eq 'optgroup') {            redo B;
5243                if ($self->{open_elements}->[-1]->[1] eq 'option') {          } elsif ($token->{tag_name} eq 'optgroup') {
5244                  ## As if </option>            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5245                  pop @{$self->{open_elements}};              !!!cp ('t274');
5246                }              ## As if </option>
5247                pop @{$self->{open_elements}};
5248              } else {
5249                !!!cp ('t275');
5250              }
5251    
5252                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            if ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
5253                  ## As if </optgroup>              !!!cp ('t276');
5254                  pop @{$self->{open_elements}};              ## As if </optgroup>
5255                }              pop @{$self->{open_elements}};
5256              } else {
5257                !!!cp ('t277');
5258              }
5259    
5260                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5261                !!!next-token;            !!!nack ('t277.1');
5262                redo B;            !!!next-token;
5263              } elsif ($token->{tag_name} eq 'select') {            redo B;
5264                !!!parse-error (type => 'not closed:select');          } elsif ($token->{tag_name} eq 'select' or
5265                ## As if </select> instead                   $token->{tag_name} eq 'input' or
5266                ## have an element in table scope                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
5267                my $i;                    {
5268                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                     caption => 1, table => 1,
5269                  my $node = $self->{open_elements}->[$_];                     tbody => 1, tfoot => 1, thead => 1,
5270                  if ($node->[1] eq $token->{tag_name}) {                     tr => 1, td => 1, th => 1,
5271                    $i = $_;                    }->{$token->{tag_name}})) {
5272                    last INSCOPE;            ## TODO: The type below is not good - <select> is replaced by </select>
5273                  } elsif ({            !!!parse-error (type => 'not closed:select', token => $token);
5274                            table => 1, html => 1,            ## NOTE: As if the token were </select> (<select> case) or
5275                           }->{$node->[1]}) {            ## as if there were </select> (otherwise).
5276                    last INSCOPE;            ## have an element in table scope
5277                  }            my $i;
5278                } # INSCOPE            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5279                unless (defined $i) {              my $node = $self->{open_elements}->[$_];
5280                  !!!parse-error (type => 'unmatched end tag:select');              if ($node->[1] & SELECT_EL) {
5281                  ## Ignore the token                !!!cp ('t278');
5282                  !!!next-token;                $i = $_;
5283                  redo B;                last INSCOPE;
5284                }              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5285                  !!!cp ('t279');
5286                  last INSCOPE;
5287                }
5288              } # INSCOPE
5289              unless (defined $i) {
5290                !!!cp ('t280');
5291                !!!parse-error (type => 'unmatched end tag:select', token => $token);
5292                ## Ignore the token
5293                !!!nack ('t280.1');
5294                !!!next-token;
5295                redo B;
5296              }
5297                                
5298                splice @{$self->{open_elements}}, $i;            !!!cp ('t281');
5299              splice @{$self->{open_elements}}, $i;
5300    
5301                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
5302    
5303                !!!next-token;            if ($token->{tag_name} eq 'select') {
5304                redo B;              !!!nack ('t281.2');
5305                !!!next-token;
5306                redo B;
5307              } else {
5308                !!!cp ('t281.1');
5309                !!!ack-later;
5310                ## Reprocess the token.
5311                redo B;
5312              }
5313          } else {          } else {
5314            !!!parse-error (type => 'in select:'.$token->{tag_name});            !!!cp ('t282');
5315              !!!parse-error (type => 'in select:'.$token->{tag_name}, token => $token);
5316            ## Ignore the token            ## Ignore the token
5317              !!!nack ('t282.1');
5318            !!!next-token;            !!!next-token;
5319            redo B;            redo B;
5320          }          }
5321        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
5322              if ($token->{tag_name} eq 'optgroup') {          if ($token->{tag_name} eq 'optgroup') {
5323                if ($self->{open_elements}->[-1]->[1] eq 'option' and            if ($self->{open_elements}->[-1]->[1] & OPTION_EL and
5324                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {                $self->{open_elements}->[-2]->[1] & OPTGROUP_EL) {
5325                  ## As if </option>              !!!cp ('t283');
5326                  splice @{$self->{open_elements}}, -2;              ## As if </option>
5327                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {              splice @{$self->{open_elements}}, -2;
5328                  pop @{$self->{open_elements}};            } elsif ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
5329                } else {              !!!cp ('t284');
5330                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              pop @{$self->{open_elements}};
5331                  ## Ignore the token            } else {
5332                }              !!!cp ('t285');
5333                !!!next-token;              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5334                redo B;              ## Ignore the token
5335              } elsif ($token->{tag_name} eq 'option') {            }
5336                if ($self->{open_elements}->[-1]->[1] eq 'option') {            !!!nack ('t285.1');
5337                  pop @{$self->{open_elements}};            !!!next-token;
5338                } else {            redo B;
5339                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          } elsif ($token->{tag_name} eq 'option') {
5340                  ## Ignore the token            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5341                }              !!!cp ('t286');
5342                !!!next-token;              pop @{$self->{open_elements}};
5343                redo B;            } else {
5344              } elsif ($token->{tag_name} eq 'select') {              !!!cp ('t287');
5345                ## have an element in table scope              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5346                my $i;              ## Ignore the token
5347                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            }
5348                  my $node = $self->{open_elements}->[$_];            !!!nack ('t287.1');
5349                  if ($node->[1] eq $token->{tag_name}) {            !!!next-token;
5350                    $i = $_;            redo B;
5351                    last INSCOPE;          } elsif ($token->{tag_name} eq 'select') {
5352                  } elsif ({            ## have an element in table scope
5353                            table => 1, html => 1,            my $i;
5354                           }->{$node->[1]}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5355                    last INSCOPE;              my $node = $self->{open_elements}->[$_];
5356                  }              if ($node->[1] & SELECT_EL) {
5357                } # INSCOPE                !!!cp ('t288');
5358                unless (defined $i) {                $i = $_;
5359                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                last INSCOPE;
5360                  ## Ignore the token              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5361                  !!!next-token;                !!!cp ('t289');
5362                  redo B;                last INSCOPE;
5363                }              }
5364              } # INSCOPE
5365              unless (defined $i) {
5366                !!!cp ('t290');
5367                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5368                ## Ignore the token
5369                !!!nack ('t290.1');
5370                !!!next-token;
5371                redo B;
5372              }
5373                                
5374                splice @{$self->{open_elements}}, $i;            !!!cp ('t291');
5375              splice @{$self->{open_elements}}, $i;
5376    
5377                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
5378    
5379                !!!next-token;            !!!nack ('t291.1');
5380                redo B;            !!!next-token;
5381              } elsif ({            redo B;
5382                        caption => 1, table => 1, tbody => 1,          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
5383                        tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                   {
5384                       }->{$token->{tag_name}}) {                    caption => 1, table => 1, tbody => 1,
5385                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
5386                     }->{$token->{tag_name}}) {
5387    ## TODO: The following is wrong?
5388              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5389                                
5390                ## have an element in table scope            ## have an element in table scope
5391                my $i;            my $i;
5392                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5393                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
5394                  if ($node->[1] eq $token->{tag_name}) {              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5395                    $i = $_;                !!!cp ('t292');
5396                    last INSCOPE;                $i = $_;
5397                  } elsif ({                last INSCOPE;
5398                            table => 1, html => 1,              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5399                           }->{$node->[1]}) {                !!!cp ('t293');
5400                    last INSCOPE;                last INSCOPE;
5401                  }              }
5402                } # INSCOPE            } # INSCOPE
5403                unless (defined $i) {            unless (defined $i) {
5404                  ## Ignore the token              !!!cp ('t294');
5405                  !!!next-token;              ## Ignore the token
5406                  redo B;              !!!nack ('t294.1');
5407                }              !!!next-token;
5408                redo B;
5409              }
5410                                
5411                ## As if </select>            ## As if </select>
5412                ## have an element in table scope            ## have an element in table scope
5413                undef $i;            undef $i;
5414                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5415                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
5416                  if ($node->[1] eq 'select') {              if ($node->[1] & SELECT_EL) {
5417                    $i = $_;                !!!cp ('t295');
5418                    last INSCOPE;                $i = $_;
5419                  } elsif ({                last INSCOPE;
5420                            table => 1, html => 1,              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5421                           }->{$node->[1]}) {  ## ISSUE: Can this state be reached?
5422                    last INSCOPE;                !!!cp ('t296');
5423                  }                last INSCOPE;
5424                } # INSCOPE              }
5425                unless (defined $i) {            } # INSCOPE
5426                  !!!parse-error (type => 'unmatched end tag:select');            unless (defined $i) {
5427                  ## Ignore the </select> token              !!!cp ('t297');
5428                  !!!next-token; ## TODO: ok?  ## TODO: The following error type is correct?
5429                  redo B;              !!!parse-error (type => 'unmatched end tag:select', token => $token);
5430                }              ## Ignore the </select> token
5431                !!!nack ('t297.1');
5432                !!!next-token; ## TODO: ok?
5433                redo B;
5434              }
5435                                
5436                splice @{$self->{open_elements}}, $i;            !!!cp ('t298');
5437              splice @{$self->{open_elements}}, $i;
5438    
5439                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
5440    
5441                ## reprocess            !!!ack-later;
5442                redo B;            ## reprocess
5443              redo B;
5444          } else {          } else {
5445            !!!parse-error (type => 'in select:/'.$token->{tag_name});            !!!cp ('t299');
5446              !!!parse-error (type => 'in select:/'.$token->{tag_name}, token => $token);
5447            ## Ignore the token            ## Ignore the token
5448              !!!nack ('t299.3');
5449            !!!next-token;            !!!next-token;
5450            redo B;            redo B;
5451          }          }
5452          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5453            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
5454                    @{$self->{open_elements}} == 1) { # redundant, maybe
5455              !!!cp ('t299.1');
5456              !!!parse-error (type => 'in body:#eof', token => $token);
5457            } else {
5458              !!!cp ('t299.2');
5459            }
5460    
5461            ## Stop parsing.
5462            last B;
5463        } else {        } else {
5464          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5465        }        }
# Line 4175  sub _tree_construction_main ($) { Line 5473  sub _tree_construction_main ($) {
5473            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5474                        
5475            unless (length $token->{data}) {            unless (length $token->{data}) {
5476                !!!cp ('t300');
5477              !!!next-token;              !!!next-token;
5478              redo B;              redo B;
5479            }            }
5480          }          }
5481                    
5482          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
5483            !!!parse-error (type => 'after html:#character');            !!!cp ('t301');
5484              !!!parse-error (type => 'after html:#character', token => $token);
5485    
5486            ## Reprocess in the "main" phase, "after body" insertion mode...            ## Reprocess in the "after body" insertion mode.
5487            } else {
5488              !!!cp ('t302');
5489          }          }
5490                    
5491          ## "after body" insertion mode          ## "after body" insertion mode
5492          !!!parse-error (type => 'after body:#character');          !!!parse-error (type => 'after body:#character', token => $token);
5493    
5494          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
5495          ## reprocess          ## reprocess
5496          redo B;          redo B;
5497        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5498          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
5499            !!!parse-error (type => 'after html:'.$token->{tag_name});            !!!cp ('t303');
5500              !!!parse-error (type => 'after html:'.$token->{tag_name}, token => $token);
5501                        
5502            ## Reprocess in the "main" phase, "after body" insertion mode...            ## Reprocess in the "after body" insertion mode.
5503            } else {
5504              !!!cp ('t304');
5505          }          }
5506    
5507          ## "after body" insertion mode          ## "after body" insertion mode
5508          !!!parse-error (type => 'after body:'.$token->{tag_name});          !!!parse-error (type => 'after body:'.$token->{tag_name}, token => $token);
5509    
5510          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
5511            !!!ack-later;
5512          ## reprocess          ## reprocess
5513          redo B;          redo B;
5514        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
5515          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
5516            !!!parse-error (type => 'after html:/'.$token->{tag_name});            !!!cp ('t305');
5517              !!!parse-error (type => 'after html:/'.$token->{tag_name}, token => $token);
5518                        
5519            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
5520            ## Reprocess in the "main" phase, "after body" insertion mode...            ## Reprocess in the "after body" insertion mode.
5521            } else {
5522              !!!cp ('t306');
5523          }          }
5524    
5525          ## "after body" insertion mode          ## "after body" insertion mode
5526          if ($token->{tag_name} eq 'html') {          if ($token->{tag_name} eq 'html') {
5527            if (defined $self->{inner_html_node}) {            if (defined $self->{inner_html_node}) {
5528              !!!parse-error (type => 'unmatched end tag:html');              !!!cp ('t307');
5529                !!!parse-error (type => 'unmatched end tag:html', token => $token);
5530              ## Ignore the token              ## Ignore the token
5531              !!!next-token;              !!!next-token;
5532              redo B;              redo B;
5533            } else {            } else {
5534                !!!cp ('t308');
5535              $self->{insertion_mode} = AFTER_HTML_BODY_IM;              $self->{insertion_mode} = AFTER_HTML_BODY_IM;
5536              !!!next-token;              !!!next-token;
5537              redo B;              redo B;
5538            }            }
5539          } else {          } else {
5540            !!!parse-error (type => 'after body:/'.$token->{tag_name});            !!!cp ('t309');
5541              !!!parse-error (type => 'after body:/'.$token->{tag_name}, token => $token);
5542    
5543            $self->{insertion_mode} = IN_BODY_IM;            $self->{insertion_mode} = IN_BODY_IM;
5544            ## reprocess            ## reprocess
5545            redo B;            redo B;
5546          }          }
5547          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5548            !!!cp ('t309.2');
5549            ## Stop parsing
5550            last B;
5551        } else {        } else {
5552          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5553        }        }
# Line 4241  sub _tree_construction_main ($) { Line 5557  sub _tree_construction_main ($) {
5557            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5558                        
5559            unless (length $token->{data}) {            unless (length $token->{data}) {
5560                !!!cp ('t310');
5561              !!!next-token;              !!!next-token;
5562              redo B;              redo B;
5563            }            }
# Line 4248  sub _tree_construction_main ($) { Line 5565  sub _tree_construction_main ($) {
5565                    
5566          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
5567            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
5568              !!!parse-error (type => 'in frameset:#character');              !!!cp ('t311');
5569                !!!parse-error (type => 'in frameset:#character', token => $token);
5570            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
5571              !!!parse-error (type => 'after frameset:#character');              !!!cp ('t312');
5572                !!!parse-error (type => 'after frameset:#character', token => $token);
5573            } else { # "after html frameset"            } else { # "after html frameset"
5574              !!!parse-error (type => 'after html:#character');              !!!cp ('t313');
5575                !!!parse-error (type => 'after html:#character', token => $token);
5576    
5577              $self->{insertion_mode} = AFTER_FRAMESET_IM;              $self->{insertion_mode} = AFTER_FRAMESET_IM;
5578              ## Reprocess in the "main" phase, "after frameset"...              ## Reprocess in the "after frameset" insertion mode.
5579              !!!parse-error (type => 'after frameset:#character');              !!!parse-error (type => 'after frameset:#character', token => $token);
5580            }            }
5581                        
5582            ## Ignore the token.            ## Ignore the token.
5583            if (length $token->{data}) {            if (length $token->{data}) {
5584                !!!cp ('t314');
5585              ## reprocess the rest of characters              ## reprocess the rest of characters
5586            } else {            } else {
5587                !!!cp ('t315');
5588              !!!next-token;              !!!next-token;
5589            }            }
5590            redo B;            redo B;
# Line 4271  sub _tree_construction_main ($) { Line 5593  sub _tree_construction_main ($) {
5593          die qq[$0: Character "$token->{data}"];          die qq[$0: Character "$token->{data}"];
5594        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5595          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
5596            !!!parse-error (type => 'after html:'.$token->{tag_name});            !!!cp ('t316');
5597              !!!parse-error (type => 'after html:'.$token->{tag_name}, token => $token);
5598    
5599            $self->{insertion_mode} = AFTER_FRAMESET_IM;            $self->{insertion_mode} = AFTER_FRAMESET_IM;
5600            ## Process in the "main" phase, "after frameset" insertion mode...            ## Process in the "after frameset" insertion mode.
5601          }          } else {
5602              !!!cp ('t317');
5603            }
5604    
5605          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
5606              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
5607            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!cp ('t318');
5608              !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5609              !!!nack ('t318.1');
5610            !!!next-token;            !!!next-token;
5611            redo B;            redo B;
5612          } elsif ($token->{tag_name} eq 'frame' and          } elsif ($token->{tag_name} eq 'frame' and
5613                   $self->{insertion_mode} == IN_FRAMESET_IM) {                   $self->{insertion_mode} == IN_FRAMESET_IM) {
5614            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!cp ('t319');
5615              !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5616            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
5617              !!!ack ('t319.1');
5618            !!!next-token;            !!!next-token;
5619            redo B;            redo B;
5620          } elsif ($token->{tag_name} eq 'noframes') {          } elsif ($token->{tag_name} eq 'noframes') {
5621              !!!cp ('t320');
5622            ## NOTE: As if in body.            ## NOTE: As if in body.
5623            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);            $parse_rcdata->(CDATA_CONTENT_MODEL);
5624            redo B;            redo B;
5625          } else {          } else {
5626            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
5627              !!!parse-error (type => 'in frameset:'.$token->{tag_name});              !!!cp ('t321');
5628                !!!parse-error (type => 'in frameset:'.$token->{tag_name}, token => $token);
5629            } else {            } else {
5630              !!!parse-error (type => 'after frameset:'.$token->{tag_name});              !!!cp ('t322');
5631                !!!parse-error (type => 'after frameset:'.$token->{tag_name}, token => $token);
5632            }            }
5633            ## Ignore the token            ## Ignore the token
5634              !!!nack ('t322.1');
5635            !!!next-token;            !!!next-token;
5636            redo B;            redo B;
5637          }          }
5638        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
5639          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
5640            !!!parse-error (type => 'after html:/'.$token->{tag_name});            !!!cp ('t323');
5641              !!!parse-error (type => 'after html:/'.$token->{tag_name}, token => $token);
5642    
5643            $self->{insertion_mode} = AFTER_FRAMESET_IM;            $self->{insertion_mode} = AFTER_FRAMESET_IM;
5644            ## Process in the "main" phase, "after frameset" insertion mode...            ## Process in the "after frameset" insertion mode.
5645            } else {
5646              !!!cp ('t324');
5647          }          }
5648    
5649          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
5650              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
5651            if ($self->{open_elements}->[-1]->[1] eq 'html' and            if ($self->{open_elements}->[-1]->[1] & HTML_EL and
5652                @{$self->{open_elements}} == 1) {                @{$self->{open_elements}} == 1) {
5653              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t325');
5654                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5655              ## Ignore the token              ## Ignore the token
5656              !!!next-token;              !!!next-token;
5657            } else {            } else {
5658                !!!cp ('t326');
5659              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
5660              !!!next-token;              !!!next-token;
5661            }            }
5662    
5663            if (not defined $self->{inner_html_node} and            if (not defined $self->{inner_html_node} and
5664                $self->{open_elements}->[-1]->[1] ne 'frameset') {                not ($self->{open_elements}->[-1]->[1] & FRAMESET_EL)) {
5665                !!!cp ('t327');
5666              $self->{insertion_mode} = AFTER_FRAMESET_IM;              $self->{insertion_mode} = AFTER_FRAMESET_IM;
5667              } else {
5668                !!!cp ('t328');
5669            }            }
5670            redo B;            redo B;
5671          } elsif ($token->{tag_name} eq 'html' and          } elsif ($token->{tag_name} eq 'html' and
5672                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {
5673              !!!cp ('t329');
5674            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
5675            !!!next-token;            !!!next-token;
5676            redo B;            redo B;
5677          } else {          } else {
5678            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
5679              !!!parse-error (type => 'in frameset:/'.$token->{tag_name});              !!!cp ('t330');
5680                !!!parse-error (type => 'in frameset:/'.$token->{tag_name}, token => $token);
5681            } else {            } else {
5682              !!!parse-error (type => 'after frameset:/'.$token->{tag_name});              !!!cp ('t331');
5683                !!!parse-error (type => 'after frameset:/'.$token->{tag_name}, token => $token);
5684            }            }
5685            ## Ignore the token            ## Ignore the token
5686            !!!next-token;            !!!next-token;
5687            redo B;            redo B;
5688          }          }
5689          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5690            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
5691                    @{$self->{open_elements}} == 1) { # redundant, maybe
5692              !!!cp ('t331.1');
5693              !!!parse-error (type => 'in body:#eof', token => $token);
5694            } else {
5695              !!!cp ('t331.2');
5696            }
5697            
5698            ## Stop parsing
5699            last B;
5700        } else {        } else {
5701          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5702        }        }
# Line 4354  sub _tree_construction_main ($) { Line 5709  sub _tree_construction_main ($) {
5709      ## "in body" insertion mode      ## "in body" insertion mode
5710      if ($token->{type} == START_TAG_TOKEN) {      if ($token->{type} == START_TAG_TOKEN) {
5711        if ($token->{tag_name} eq 'script') {        if ($token->{tag_name} eq 'script') {
5712            !!!cp ('t332');
5713          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
5714          $script_start_tag->($insert);          $script_start_tag->();
5715          redo B;          redo B;
5716        } elsif ($token->{tag_name} eq 'style') {        } elsif ($token->{tag_name} eq 'style') {
5717            !!!cp ('t333');
5718          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
5719          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);          $parse_rcdata->(CDATA_CONTENT_MODEL);
5720          redo B;          redo B;
5721        } elsif ({        } elsif ({
5722                  base => 1, link => 1,                  base => 1, link => 1,
5723                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
5724            !!!cp ('t334');
5725          ## 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
5726          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
5727          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
5728            !!!ack ('t334.1');
5729          !!!next-token;          !!!next-token;
5730          redo B;          redo B;
5731        } elsif ($token->{tag_name} eq 'meta') {        } elsif ($token->{tag_name} eq 'meta') {
5732          ## 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
5733          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
5734          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.
5735    
5736          unless ($self->{confident}) {          unless ($self->{confident}) {
           my $charset;  
5737            if ($token->{attributes}->{charset}) { ## TODO: And if supported            if ($token->{attributes}->{charset}) { ## TODO: And if supported
5738              $charset = $token->{attributes}->{charset}->{value};              !!!cp ('t335');
5739            }              $self->{change_encoding}
5740            if ($token->{attributes}->{'http-equiv'}) {                  ->($self, $token->{attributes}->{charset}->{value}, $token);
5741                
5742                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
5743                    ->set_user_data (manakai_has_reference =>
5744                                         $token->{attributes}->{charset}
5745                                             ->{has_reference});
5746              } elsif ($token->{attributes}->{content}) {
5747              ## 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.
5748              if ($token->{attributes}->{'http-equiv'}->{value}              if ($token->{attributes}->{content}->{value}
5749                  =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=                  =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
5750                        [\x09-\x0D\x20]*=
5751                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
5752                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
5753                $charset = defined $1 ? $1 : defined $2 ? $2 : $3;                !!!cp ('t336');
5754              } ## TODO: And if supported                $self->{change_encoding}
5755                      ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);
5756                  $meta_el->[0]->get_attribute_node_ns (undef, 'content')
5757                      ->set_user_data (manakai_has_reference =>
5758                                           $token->{attributes}->{content}
5759                                                 ->{has_reference});
5760                }
5761              }
5762            } else {
5763              if ($token->{attributes}->{charset}) {
5764                !!!cp ('t337');
5765                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
5766                    ->set_user_data (manakai_has_reference =>
5767                                         $token->{attributes}->{charset}
5768                                             ->{has_reference});
5769              }
5770              if ($token->{attributes}->{content}) {
5771                !!!cp ('t338');
5772                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
5773                    ->set_user_data (manakai_has_reference =>
5774                                         $token->{attributes}->{content}
5775                                             ->{has_reference});
5776            }            }
           ## TODO: Change the encoding  
5777          }          }
5778    
5779            !!!ack ('t338.1');
5780          !!!next-token;          !!!next-token;
5781          redo B;          redo B;
5782        } elsif ($token->{tag_name} eq 'title') {        } elsif ($token->{tag_name} eq 'title') {
5783          !!!parse-error (type => 'in body:title');          !!!cp ('t341');
5784          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
5785          $parse_rcdata->(RCDATA_CONTENT_MODEL, sub {          $parse_rcdata->(RCDATA_CONTENT_MODEL);
           if (defined $self->{head_element}) {  
             $self->{head_element}->append_child ($_[0]);  
           } else {  
             $insert->($_[0]);  
           }  
         });  
5786          redo B;          redo B;
5787        } elsif ($token->{tag_name} eq 'body') {        } elsif ($token->{tag_name} eq 'body') {
5788          !!!parse-error (type => 'in body:body');          !!!parse-error (type => 'in body:body', token => $token);
5789                                
5790          if (@{$self->{open_elements}} == 1 or          if (@{$self->{open_elements}} == 1 or
5791              $self->{open_elements}->[1]->[1] ne 'body') {              not ($self->{open_elements}->[1]->[1] & BODY_EL)) {
5792              !!!cp ('t342');
5793            ## Ignore the token            ## Ignore the token
5794          } else {          } else {
5795            my $body_el = $self->{open_elements}->[1]->[0];            my $body_el = $self->{open_elements}->[1]->[0];
5796            for my $attr_name (keys %{$token->{attributes}}) {            for my $attr_name (keys %{$token->{attributes}}) {
5797              unless ($body_el->has_attribute_ns (undef, $attr_name)) {              unless ($body_el->has_attribute_ns (undef, $attr_name)) {
5798                  !!!cp ('t343');
5799                $body_el->set_attribute_ns                $body_el->set_attribute_ns
5800                  (undef, [undef, $attr_name],                  (undef, [undef, $attr_name],
5801                   $token->{attributes}->{$attr_name}->{value});                   $token->{attributes}->{$attr_name}->{value});
5802              }              }
5803            }            }
5804          }          }
5805            !!!nack ('t343.1');
5806          !!!next-token;          !!!next-token;
5807          redo B;          redo B;
5808        } elsif ({        } elsif ({
5809                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
5810                  div => 1, dl => 1, fieldset => 1, listing => 1,                  div => 1, dl => 1, fieldset => 1,
5811                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
5812                  menu => 1, ol => 1, p => 1, ul => 1,                  menu => 1, ol => 1, p => 1, ul => 1,
5813                  pre => 1,                  pre => 1, listing => 1,
5814                    form => 1,
5815                    table => 1,
5816                    hr => 1,
5817                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
5818            if ($token->{tag_name} eq 'form' and defined $self->{form_element}) {
5819              !!!cp ('t350');
5820              !!!parse-error (type => 'in form:form', token => $token);
5821              ## Ignore the token
5822              !!!nack ('t350.1');
5823              !!!next-token;
5824              redo B;
5825            }
5826    
5827          ## has a p element in scope          ## has a p element in scope
5828          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
5829            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
5830              !!!back-token;              !!!cp ('t344');
5831              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              !!!back-token; # <form>
5832                $token = {type => END_TAG_TOKEN, tag_name => 'p',
5833                          line => $token->{line}, column => $token->{column}};
5834              redo B;              redo B;
5835            } elsif ({            } elsif ($_->[1] & SCOPING_EL) {
5836                      table => 1, caption => 1, td => 1, th => 1,              !!!cp ('t345');
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
5837              last INSCOPE;              last INSCOPE;
5838            }            }
5839          } # INSCOPE          } # INSCOPE
5840                        
5841          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
5842          if ($token->{tag_name} eq 'pre') {          if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {
5843              !!!nack ('t346.1');
5844            !!!next-token;            !!!next-token;
5845            if ($token->{type} == CHARACTER_TOKEN) {            if ($token->{type} == CHARACTER_TOKEN) {
5846              $token->{data} =~ s/^\x0A//;              $token->{data} =~ s/^\x0A//;
5847              unless (length $token->{data}) {              unless (length $token->{data}) {
5848                  !!!cp ('t346');
5849                !!!next-token;                !!!next-token;
5850                } else {
5851                  !!!cp ('t349');
5852              }              }
5853              } else {
5854                !!!cp ('t348');
5855            }            }
5856          } else {          } elsif ($token->{tag_name} eq 'form') {
5857              !!!cp ('t347.1');
5858              $self->{form_element} = $self->{open_elements}->[-1]->[0];
5859    
5860              !!!nack ('t347.2');
5861            !!!next-token;            !!!next-token;
5862          }          } elsif ($token->{tag_name} eq 'table') {
5863          redo B;            !!!cp ('t382');
5864        } elsif ($token->{tag_name} eq 'form') {            push @{$open_tables}, [$self->{open_elements}->[-1]->[0]];
5865          if (defined $self->{form_element}) {            
5866            !!!parse-error (type => 'in form:form');            $self->{insertion_mode} = IN_TABLE_IM;
5867            ## Ignore the token  
5868              !!!nack ('t382.1');
5869              !!!next-token;
5870            } elsif ($token->{tag_name} eq 'hr') {
5871              !!!cp ('t386');
5872              pop @{$self->{open_elements}};
5873            
5874              !!!nack ('t386.1');
5875            !!!next-token;            !!!next-token;
           redo B;  
5876          } else {          } else {
5877            ## 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];  
5878            !!!next-token;            !!!next-token;
           redo B;  
5879          }          }
       } elsif ($token->{tag_name} eq 'li') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => END_TAG_TOKEN, tag_name => 'p'};  
             redo B;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## Step 1  
         my $i = -1;  
         my $node = $self->{open_elements}->[$i];  
         LI: {  
           ## Step 2  
           if ($node->[1] eq '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;  
5880          redo B;          redo B;
5881        } elsif ($token->{tag_name} eq 'dd' or $token->{tag_name} eq 'dt') {        } elsif ({li => 1, dt => 1, dd => 1}->{$token->{tag_name}}) {
5882          ## has a p element in scope          ## has a p element in scope
5883          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
5884            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
5885              !!!back-token;              !!!cp ('t353');
5886              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              !!!back-token; # <x>
5887                $token = {type => END_TAG_TOKEN, tag_name => 'p',
5888                          line => $token->{line}, column => $token->{column}};
5889              redo B;              redo B;
5890            } elsif ({            } elsif ($_->[1] & SCOPING_EL) {
5891                      table => 1, caption => 1, td => 1, th => 1,              !!!cp ('t354');
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
5892              last INSCOPE;              last INSCOPE;
5893            }            }
5894          } # INSCOPE          } # INSCOPE
# Line 4546  sub _tree_construction_main ($) { Line 5896  sub _tree_construction_main ($) {
5896          ## Step 1          ## Step 1
5897          my $i = -1;          my $i = -1;
5898          my $node = $self->{open_elements}->[$i];          my $node = $self->{open_elements}->[$i];
5899            my $li_or_dtdd = {li => {li => 1},
5900                              dt => {dt => 1, dd => 1},
5901                              dd => {dt => 1, dd => 1}}->{$token->{tag_name}};
5902          LI: {          LI: {
5903            ## Step 2            ## Step 2
5904            if ($node->[1] eq 'dt' or $node->[1] eq 'dd') {            if ($li_or_dtdd->{$node->[0]->manakai_local_name}) {
5905              if ($i != -1) {              if ($i != -1) {
5906                !!!parse-error (type => 'end tag missing:'.                !!!cp ('t355');
5907                                $self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed',
5908                                  value => $self->{open_elements}->[-1]->[0]
5909                                      ->manakai_local_name,
5910                                  token => $token);
5911                } else {
5912                  !!!cp ('t356');
5913              }              }
5914              splice @{$self->{open_elements}}, $i;              splice @{$self->{open_elements}}, $i;
5915              last LI;              last LI;
5916              } else {
5917                !!!cp ('t357');
5918            }            }
5919                        
5920            ## Step 3            ## Step 3
5921            if (not $formatting_category->{$node->[1]} and            if (not ($node->[1] & FORMATTING_EL) and
5922                #not $phrasing_category->{$node->[1]} and                #not $phrasing_category->{$node->[1]} and
5923                ($special_category->{$node->[1]} or                ($node->[1] & SPECIAL_EL or
5924                 $scoping_category->{$node->[1]}) and                 $node->[1] & SCOPING_EL) and
5925                $node->[1] ne 'address' and $node->[1] ne 'div') {                not ($node->[1] & ADDRESS_EL) and
5926                  not ($node->[1] & DIV_EL)) {
5927                !!!cp ('t358');
5928              last LI;              last LI;
5929            }            }
5930                        
5931              !!!cp ('t359');
5932            ## Step 4            ## Step 4
5933            $i--;            $i--;
5934            $node = $self->{open_elements}->[$i];            $node = $self->{open_elements}->[$i];
5935            redo LI;            redo LI;
5936          } # LI          } # LI
5937                        
5938          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
5939            !!!nack ('t359.1');
5940          !!!next-token;          !!!next-token;
5941          redo B;          redo B;
5942        } elsif ($token->{tag_name} eq 'plaintext') {        } elsif ($token->{tag_name} eq 'plaintext') {
5943          ## has a p element in scope          ## has a p element in scope
5944          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
5945            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
5946              !!!back-token;              !!!cp ('t367');
5947              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              !!!back-token; # <plaintext>
5948                $token = {type => END_TAG_TOKEN, tag_name => 'p',
5949                          line => $token->{line}, column => $token->{column}};
5950              redo B;              redo B;
5951            } elsif ({            } elsif ($_->[1] & SCOPING_EL) {
5952                      table => 1, caption => 1, td => 1, th => 1,              !!!cp ('t368');
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
5953              last INSCOPE;              last INSCOPE;
5954            }            }
5955          } # INSCOPE          } # INSCOPE
5956                        
5957          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
5958                        
5959          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
5960                        
5961          !!!next-token;          !!!nack ('t368.1');
         redo 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});  
             
5962          !!!next-token;          !!!next-token;
5963          redo B;          redo B;
5964        } elsif ($token->{tag_name} eq 'a') {        } elsif ($token->{tag_name} eq 'a') {
5965          AFE: for my $i (reverse 0..$#$active_formatting_elements) {          AFE: for my $i (reverse 0..$#$active_formatting_elements) {
5966            my $node = $active_formatting_elements->[$i];            my $node = $active_formatting_elements->[$i];
5967            if ($node->[1] eq 'a') {            if ($node->[1] & A_EL) {
5968              !!!parse-error (type => 'in a:a');              !!!cp ('t371');
5969                !!!parse-error (type => 'in a:a', token => $token);
5970                            
5971              !!!back-token;              !!!back-token; # <a>
5972              $token = {type => END_TAG_TOKEN, tag_name => 'a'};              $token = {type => END_TAG_TOKEN, tag_name => 'a',
5973              $formatting_end_tag->($token->{tag_name});                        line => $token->{line}, column => $token->{column}};
5974                $formatting_end_tag->($token);
5975                            
5976              AFE2: for (reverse 0..$#$active_formatting_elements) {              AFE2: for (reverse 0..$#$active_formatting_elements) {
5977                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
5978                    !!!cp ('t372');
5979                  splice @$active_formatting_elements, $_, 1;                  splice @$active_formatting_elements, $_, 1;
5980                  last AFE2;                  last AFE2;
5981                }                }
5982              } # AFE2              } # AFE2
5983              OE: for (reverse 0..$#{$self->{open_elements}}) {              OE: for (reverse 0..$#{$self->{open_elements}}) {
5984                if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {                if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {
5985                    !!!cp ('t373');
5986                  splice @{$self->{open_elements}}, $_, 1;                  splice @{$self->{open_elements}}, $_, 1;
5987                  last OE;                  last OE;
5988                }                }
5989              } # OE              } # OE
5990              last AFE;              last AFE;
5991            } elsif ($node->[0] eq '#marker') {            } elsif ($node->[0] eq '#marker') {
5992                !!!cp ('t374');
5993              last AFE;              last AFE;
5994            }            }
5995          } # AFE          } # AFE
5996                        
5997          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
5998    
5999          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6000          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
6001    
6002          !!!next-token;          !!!nack ('t374.1');
         redo 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];  
           
6003          !!!next-token;          !!!next-token;
6004          redo B;          redo B;
6005        } elsif ($token->{tag_name} eq 'nobr') {        } elsif ($token->{tag_name} eq 'nobr') {
# Line 4694  sub _tree_construction_main ($) { Line 6008  sub _tree_construction_main ($) {
6008          ## has a |nobr| element in scope          ## has a |nobr| element in scope
6009          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6010            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6011            if ($node->[1] eq 'nobr') {            if ($node->[1] & NOBR_EL) {
6012              !!!parse-error (type => 'in nobr:nobr');              !!!cp ('t376');
6013              !!!back-token;              !!!parse-error (type => 'in nobr:nobr', token => $token);
6014              $token = {type => END_TAG_TOKEN, tag_name => 'nobr'};              !!!back-token; # <nobr>
6015                $token = {type => END_TAG_TOKEN, tag_name => 'nobr',
6016                          line => $token->{line}, column => $token->{column}};
6017              redo B;              redo B;
6018            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
6019                      table => 1, caption => 1, td => 1, th => 1,              !!!cp ('t377');
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6020              last INSCOPE;              last INSCOPE;
6021            }            }
6022          } # INSCOPE          } # INSCOPE
6023                    
6024          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6025          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
6026                    
6027            !!!nack ('t377.1');
6028          !!!next-token;          !!!next-token;
6029          redo B;          redo B;
6030        } elsif ($token->{tag_name} eq 'button') {        } elsif ($token->{tag_name} eq 'button') {
6031          ## has a button element in scope          ## has a button element in scope
6032          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6033            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6034            if ($node->[1] eq 'button') {            if ($node->[1] & BUTTON_EL) {
6035              !!!parse-error (type => 'in button:button');              !!!cp ('t378');
6036              !!!back-token;              !!!parse-error (type => 'in button:button', token => $token);
6037              $token = {type => END_TAG_TOKEN, tag_name => 'button'};              !!!back-token; # <button>
6038                $token = {type => END_TAG_TOKEN, tag_name => 'button',
6039                          line => $token->{line}, column => $token->{column}};
6040              redo B;              redo B;
6041            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
6042                      table => 1, caption => 1, td => 1, th => 1,              !!!cp ('t379');
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6043              last INSCOPE;              last INSCOPE;
6044            }            }
6045          } # INSCOPE          } # INSCOPE
6046                        
6047          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6048                        
6049          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6050          push @$active_formatting_elements, ['#marker', ''];  
6051            ## TODO: associate with $self->{form_element} if defined
6052    
         !!!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});  
6053          push @$active_formatting_elements, ['#marker', ''];          push @$active_formatting_elements, ['#marker', ''];
6054            
6055          !!!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;  
             
6056          !!!next-token;          !!!next-token;
6057          redo B;          redo B;
6058        } elsif ({        } elsif ({
6059                  area => 1, basefont => 1, bgsound => 1, br => 1,                  xmp => 1,
6060                  embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,                  iframe => 1,
6061                  image => 1,                  noembed => 1,
6062                    noframes => 1,
6063                    noscript => 0, ## TODO: 1 if scripting is enabled
6064                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6065          if ($token->{tag_name} eq 'image') {          if ($token->{tag_name} eq 'xmp') {
6066            !!!parse-error (type => 'image');            !!!cp ('t381');
6067            $token->{tag_name} = 'img';            $reconstruct_active_formatting_elements->($insert_to_current);
6068            } else {
6069              !!!cp ('t399');
6070          }          }
6071            ## NOTE: There is an "as if in body" code clone.
6072          ## NOTE: There is an "as if <br>" code clone.          $parse_rcdata->(CDATA_CONTENT_MODEL);
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!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;  
6073          redo B;          redo B;
6074        } elsif ($token->{tag_name} eq 'isindex') {        } elsif ($token->{tag_name} eq 'isindex') {
6075          !!!parse-error (type => 'isindex');          !!!parse-error (type => 'isindex', token => $token);
6076                    
6077          if (defined $self->{form_element}) {          if (defined $self->{form_element}) {
6078              !!!cp ('t389');
6079            ## Ignore the token            ## Ignore the token
6080              !!!nack ('t389'); ## NOTE: Not acknowledged.
6081            !!!next-token;            !!!next-token;
6082            redo B;            redo B;
6083          } else {          } else {
# Line 4834  sub _tree_construction_main ($) { Line 6090  sub _tree_construction_main ($) {
6090            delete $at->{prompt};            delete $at->{prompt};
6091            my @tokens = (            my @tokens = (
6092                          {type => START_TAG_TOKEN, tag_name => 'form',                          {type => START_TAG_TOKEN, tag_name => 'form',
6093                           attributes => $form_attrs},                           attributes => $form_attrs,
6094                          {type => START_TAG_TOKEN, tag_name => 'hr'},                           line => $token->{line}, column => $token->{column}},
6095                          {type => START_TAG_TOKEN, tag_name => 'p'},                          {type => START_TAG_TOKEN, tag_name => 'hr',
6096                          {type => START_TAG_TOKEN, tag_name => 'label'},                           line => $token->{line}, column => $token->{column}},
6097                            {type => START_TAG_TOKEN, tag_name => 'p',
6098                             line => $token->{line}, column => $token->{column}},
6099                            {type => START_TAG_TOKEN, tag_name => 'label',
6100                             line => $token->{line}, column => $token->{column}},
6101                         );                         );
6102            if ($prompt_attr) {            if ($prompt_attr) {
6103              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value}};              !!!cp ('t390');
6104                push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value},
6105                               #line => $token->{line}, column => $token->{column},
6106                              };
6107            } else {            } else {
6108                !!!cp ('t391');
6109              push @tokens, {type => CHARACTER_TOKEN,              push @tokens, {type => CHARACTER_TOKEN,
6110                             data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD                             data => 'This is a searchable index. Insert your search keywords here: ',
6111                               #line => $token->{line}, column => $token->{column},
6112                              }; # SHOULD
6113              ## TODO: make this configurable              ## TODO: make this configurable
6114            }            }
6115            push @tokens,            push @tokens,
6116                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at},                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at,
6117                             line => $token->{line}, column => $token->{column}},
6118                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
6119                          {type => END_TAG_TOKEN, tag_name => 'label'},                          {type => END_TAG_TOKEN, tag_name => 'label',
6120                          {type => END_TAG_TOKEN, tag_name => 'p'},                           line => $token->{line}, column => $token->{column}},
6121                          {type => START_TAG_TOKEN, tag_name => 'hr'},                          {type => END_TAG_TOKEN, tag_name => 'p',
6122                          {type => END_TAG_TOKEN, tag_name => 'form'};                           line => $token->{line}, column => $token->{column}},
6123            $token = shift @tokens;                          {type => START_TAG_TOKEN, tag_name => 'hr',
6124                             line => $token->{line}, column => $token->{column}},
6125                            {type => END_TAG_TOKEN, tag_name => 'form',
6126                             line => $token->{line}, column => $token->{column}};
6127              !!!nack ('t391.1'); ## NOTE: Not acknowledged.
6128            !!!back-token (@tokens);            !!!back-token (@tokens);
6129              !!!next-token;
6130            redo B;            redo B;
6131          }          }
6132        } elsif ($token->{tag_name} eq 'textarea') {        } elsif ($token->{tag_name} eq 'textarea') {
6133          my $tag_name = $token->{tag_name};          my $tag_name = $token->{tag_name};
6134          my $el;          my $el;
6135          !!!create-element ($el, $token->{tag_name}, $token->{attributes});          !!!create-element ($el, $token->{tag_name}, $token->{attributes}, $token);
6136                    
6137          ## TODO: $self->{form_element} if defined          ## TODO: $self->{form_element} if defined
6138          $self->{content_model} = RCDATA_CONTENT_MODEL;          $self->{content_model} = RCDATA_CONTENT_MODEL;
# Line 4869  sub _tree_construction_main ($) { Line 6141  sub _tree_construction_main ($) {
6141          $insert->($el);          $insert->($el);
6142                    
6143          my $text = '';          my $text = '';
6144            !!!nack ('t392.1');
6145          !!!next-token;          !!!next-token;
6146          if ($token->{type} == CHARACTER_TOKEN) {          if ($token->{type} == CHARACTER_TOKEN) {
6147            $token->{data} =~ s/^\x0A//;            $token->{data} =~ s/^\x0A//;
6148            unless (length $token->{data}) {            unless (length $token->{data}) {
6149                !!!cp ('t392');
6150              !!!next-token;              !!!next-token;
6151              } else {
6152                !!!cp ('t393');
6153            }            }
6154            } else {
6155              !!!cp ('t394');
6156          }          }
6157          while ($token->{type} == CHARACTER_TOKEN) {          while ($token->{type} == CHARACTER_TOKEN) {
6158              !!!cp ('t395');
6159            $text .= $token->{data};            $text .= $token->{data};
6160            !!!next-token;            !!!next-token;
6161          }          }
6162          if (length $text) {          if (length $text) {
6163              !!!cp ('t396');
6164            $el->manakai_append_text ($text);            $el->manakai_append_text ($text);
6165          }          }
6166                    
# Line 4888  sub _tree_construction_main ($) { Line 6168  sub _tree_construction_main ($) {
6168                    
6169          if ($token->{type} == END_TAG_TOKEN and          if ($token->{type} == END_TAG_TOKEN and
6170              $token->{tag_name} eq $tag_name) {              $token->{tag_name} eq $tag_name) {
6171              !!!cp ('t397');
6172            ## Ignore the token            ## Ignore the token
6173          } else {          } else {
6174            !!!parse-error (type => 'in RCDATA:#'.$token->{type});            !!!cp ('t398');
6175              !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);
6176          }          }
6177          !!!next-token;          !!!next-token;
6178          redo B;          redo B;
6179        } elsif ({        } elsif ({
                 iframe => 1,  
                 noembed => 1,  
                 noframes => 1,  
                 noscript => 0, ## TODO: 1 if scripting is enabled  
                }->{$token->{tag_name}}) {  
         ## NOTE: There is an "as if in body" code clone.  
         $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);  
         redo B;  
       } elsif ($token->{tag_name} eq 'select') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
           
         $self->{insertion_mode} = IN_SELECT_IM;  
         !!!next-token;  
         redo B;  
       } elsif ({  
6180                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
6181                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
6182                  tbody => 1, td => 1, tfoot => 1, th => 1,                  tbody => 1, td => 1, tfoot => 1, th => 1,
6183                  thead => 1, tr => 1,                  thead => 1, tr => 1,
6184                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6185          !!!parse-error (type => 'in body:'.$token->{tag_name});          !!!cp ('t401');
6186            !!!parse-error (type => 'in body:'.$token->{tag_name}, token => $token);
6187          ## Ignore the token          ## Ignore the token
6188            !!!nack ('t401.1'); ## NOTE: |<col/>| or |<frame/>| here is an error.
6189          !!!next-token;          !!!next-token;
6190          redo B;          redo B;
6191                    
6192          ## ISSUE: An issue on HTML5 new elements in the spec.          ## ISSUE: An issue on HTML5 new elements in the spec.
6193        } else {        } else {
6194            if ($token->{tag_name} eq 'image') {
6195              !!!cp ('t384');
6196              !!!parse-error (type => 'image', token => $token);
6197              $token->{tag_name} = 'img';
6198            } else {
6199              !!!cp ('t385');
6200            }
6201    
6202            ## NOTE: There is an "as if <br>" code clone.
6203          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6204                    
6205          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6206    
6207            if ({
6208                 applet => 1, marquee => 1, object => 1,
6209                }->{$token->{tag_name}}) {
6210              !!!cp ('t380');
6211              push @$active_formatting_elements, ['#marker', ''];
6212              !!!nack ('t380.1');
6213            } elsif ({
6214                      b => 1, big => 1, em => 1, font => 1, i => 1,
6215                      s => 1, small => 1, strile => 1,
6216                      strong => 1, tt => 1, u => 1,
6217                     }->{$token->{tag_name}}) {
6218              !!!cp ('t375');
6219              push @$active_formatting_elements, $self->{open_elements}->[-1];
6220              !!!nack ('t375.1');
6221            } elsif ($token->{tag_name} eq 'input') {
6222              !!!cp ('t388');
6223              ## TODO: associate with $self->{form_element} if defined
6224              pop @{$self->{open_elements}};
6225              !!!ack ('t388.2');
6226            } elsif ({
6227                      area => 1, basefont => 1, bgsound => 1, br => 1,
6228                      embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
6229                      #image => 1,
6230                     }->{$token->{tag_name}}) {
6231              !!!cp ('t388.1');
6232              pop @{$self->{open_elements}};
6233              !!!ack ('t388.3');
6234            } elsif ($token->{tag_name} eq 'select') {
6235              ## TODO: associate with $self->{form_element} if defined
6236            
6237              if ($self->{insertion_mode} & TABLE_IMS or
6238                  $self->{insertion_mode} & BODY_TABLE_IMS or
6239                  $self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
6240                !!!cp ('t400.1');
6241                $self->{insertion_mode} = IN_SELECT_IN_TABLE_IM;
6242              } else {
6243                !!!cp ('t400.2');
6244                $self->{insertion_mode} = IN_SELECT_IM;
6245              }
6246              !!!nack ('t400.3');
6247            } else {
6248              !!!nack ('t402');
6249            }
6250                    
6251          !!!next-token;          !!!next-token;
6252          redo B;          redo B;
6253        }        }
6254      } elsif ($token->{type} == END_TAG_TOKEN) {      } elsif ($token->{type} == END_TAG_TOKEN) {
6255        if ($token->{tag_name} eq 'body') {        if ($token->{tag_name} eq 'body') {
6256          if (@{$self->{open_elements}} > 1 and          ## has a |body| element in scope
6257              $self->{open_elements}->[1]->[1] eq 'body') {          my $i;
6258            for (@{$self->{open_elements}}) {          INSCOPE: {
6259              unless ({            for (reverse @{$self->{open_elements}}) {
6260                         dd => 1, dt => 1, li => 1, p => 1, td => 1,              if ($_->[1] & BODY_EL) {
6261                         th => 1, tr => 1, body => 1, html => 1,                !!!cp ('t405');
6262                       tbody => 1, tfoot => 1, thead => 1,                $i = $_;
6263                      }->{$_->[1]}) {                last INSCOPE;
6264                !!!parse-error (type => 'not closed:'.$_->[1]);              } elsif ($_->[1] & SCOPING_EL) {
6265                  !!!cp ('t405.1');
6266                  last;
6267              }              }
6268            }            }
6269    
6270            $self->{insertion_mode} = AFTER_BODY_IM;            !!!parse-error (type => 'start tag not allowed',
6271            !!!next-token;                            value => $token->{tag_name}, token => $token);
6272            redo B;            ## NOTE: Ignore the token.
         } else {  
           !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
           ## Ignore the token  
6273            !!!next-token;            !!!next-token;
6274            redo B;            redo B;
6275            } # INSCOPE
6276    
6277            for (@{$self->{open_elements}}) {
6278              unless ($_->[1] & ALL_END_TAG_OPTIONAL_EL) {
6279                !!!cp ('t403');
6280                !!!parse-error (type => 'not closed',
6281                                value => $_->[0]->manakai_local_name,
6282                                token => $token);
6283                last;
6284              } else {
6285                !!!cp ('t404');
6286              }
6287          }          }
6288    
6289            $self->{insertion_mode} = AFTER_BODY_IM;
6290            !!!next-token;
6291            redo B;
6292        } elsif ($token->{tag_name} eq 'html') {        } elsif ($token->{tag_name} eq 'html') {
6293          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
6294            ## up-to-date, though it has same effect as speced.
6295            if (@{$self->{open_elements}} > 1 and
6296                $self->{open_elements}->[1]->[1] & BODY_EL) {
6297            ## ISSUE: There is an issue in the spec.            ## ISSUE: There is an issue in the spec.
6298            if ($self->{open_elements}->[-1]->[1] ne 'body') {            unless ($self->{open_elements}->[-1]->[1] & BODY_EL) {
6299              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);              !!!cp ('t406');
6300                !!!parse-error (type => 'not closed',
6301                                value => $self->{open_elements}->[1]->[0]
6302                                    ->manakai_local_name,
6303                                token => $token);
6304              } else {
6305                !!!cp ('t407');
6306            }            }
6307            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
6308            ## reprocess            ## reprocess
6309            redo B;            redo B;
6310          } else {          } else {
6311            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!cp ('t408');
6312              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6313            ## Ignore the token            ## Ignore the token
6314            !!!next-token;            !!!next-token;
6315            redo B;            redo B;
# Line 4973  sub _tree_construction_main ($) { Line 6318  sub _tree_construction_main ($) {
6318                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
6319                  div => 1, dl => 1, fieldset => 1, listing => 1,                  div => 1, dl => 1, fieldset => 1, listing => 1,
6320                  menu => 1, ol => 1, pre => 1, ul => 1,                  menu => 1, ol => 1, pre => 1, ul => 1,
                 p => 1,  
6321                  dd => 1, dt => 1, li => 1,                  dd => 1, dt => 1, li => 1,
6322                  button => 1, marquee => 1, object => 1,                  applet => 1, button => 1, marquee => 1, object => 1,
6323                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6324          ## has an element in scope          ## has an element in scope
6325          my $i;          my $i;
6326          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6327            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6328            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6329              ## 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;  
             }  
6330              $i = $_;              $i = $_;
6331              last INSCOPE unless $token->{tag_name} eq 'p';              last INSCOPE;
6332            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
6333                      table => 1, caption => 1, td => 1, th => 1,              !!!cp ('t411');
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6334              last INSCOPE;              last INSCOPE;
6335            }            }
6336          } # INSCOPE          } # INSCOPE
6337            
6338          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          unless (defined $i) { # has an element in scope
6339            if (defined $i) {            !!!cp ('t413');
6340              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6341            } else {
6342              ## Step 1. generate implied end tags
6343              while ({
6344                      dd => ($token->{tag_name} ne 'dd'),
6345                      dt => ($token->{tag_name} ne 'dt'),
6346                      li => ($token->{tag_name} ne 'li'),
6347                      p => 1,
6348                     }->{$self->{open_elements}->[-1]->[0]->manakai_local_name}) {
6349                !!!cp ('t409');
6350                pop @{$self->{open_elements}};
6351              }
6352    
6353              ## Step 2.
6354              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6355                      ne $token->{tag_name}) {
6356                !!!cp ('t412');
6357                !!!parse-error (type => 'not closed',
6358                                value => $self->{open_elements}->[-1]->[0]
6359                                    ->manakai_local_name,
6360                                token => $token);
6361            } else {            } else {
6362              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t414');
6363            }            }
6364          }  
6365                      ## Step 3.
         if (defined $i) {  
6366            splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
6367          } elsif ($token->{tag_name} eq 'p') {  
6368            ## As if <p>, then reprocess the current token            ## Step 4.
6369            my $el;            $clear_up_to_marker->()
6370            !!!create-element ($el, 'p');                if {
6371            $insert->($el);                  applet => 1, button => 1, marquee => 1, object => 1,
6372                  }->{$token->{tag_name}};
6373          }          }
         $clear_up_to_marker->()  
           if {  
             button => 1, marquee => 1, object => 1,  
           }->{$token->{tag_name}};  
6374          !!!next-token;          !!!next-token;
6375          redo B;          redo B;
6376        } elsif ($token->{tag_name} eq 'form') {        } elsif ($token->{tag_name} eq 'form') {
6377            undef $self->{form_element};
6378    
6379          ## has an element in scope          ## has an element in scope
6380            my $i;
6381          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6382            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6383            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] & FORM_EL) {
6384              ## generate implied end tags              !!!cp ('t418');
6385              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;  
             }  
6386              last INSCOPE;              last INSCOPE;
6387            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
6388                      table => 1, caption => 1, td => 1, th => 1,              !!!cp ('t419');
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6389              last INSCOPE;              last INSCOPE;
6390            }            }
6391          } # INSCOPE          } # INSCOPE
6392            
6393          if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {          unless (defined $i) { # has an element in scope
6394            pop @{$self->{open_elements}};            !!!cp ('t421');
6395          } else {            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6396            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          } else {
6397              ## Step 1. generate implied end tags
6398              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
6399                !!!cp ('t417');
6400                pop @{$self->{open_elements}};
6401              }
6402              
6403              ## Step 2.
6404              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6405                      ne $token->{tag_name}) {
6406                !!!cp ('t417.1');
6407                !!!parse-error (type => 'not closed',
6408                                value => $self->{open_elements}->[-1]->[0]
6409                                    ->manakai_local_name,
6410                                token => $token);
6411              } else {
6412                !!!cp ('t420');
6413              }  
6414              
6415              ## Step 3.
6416              splice @{$self->{open_elements}}, $i;
6417          }          }
6418    
         undef $self->{form_element};  
6419          !!!next-token;          !!!next-token;
6420          redo B;          redo B;
6421        } elsif ({        } elsif ({
# Line 5069  sub _tree_construction_main ($) { Line 6425  sub _tree_construction_main ($) {
6425          my $i;          my $i;
6426          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6427            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6428            if ({            if ($node->[1] & HEADING_EL) {
6429                 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;  
             }  
6430              $i = $_;              $i = $_;
6431              last INSCOPE;              last INSCOPE;
6432            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
6433                      table => 1, caption => 1, td => 1, th => 1,              !!!cp ('t424');
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6434              last INSCOPE;              last INSCOPE;
6435            }            }
6436          } # INSCOPE          } # INSCOPE
6437            
6438          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          unless (defined $i) { # has an element in scope
6439            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!cp ('t425.1');
6440              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6441            } else {
6442              ## Step 1. generate implied end tags
6443              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
6444                !!!cp ('t422');
6445                pop @{$self->{open_elements}};
6446              }
6447              
6448              ## Step 2.
6449              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6450                      ne $token->{tag_name}) {
6451                !!!cp ('t425');
6452                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6453              } else {
6454                !!!cp ('t426');
6455              }
6456    
6457              ## Step 3.
6458              splice @{$self->{open_elements}}, $i;
6459          }          }
6460                    
6461          splice @{$self->{open_elements}}, $i if defined $i;          !!!next-token;
6462            redo B;
6463          } elsif ($token->{tag_name} eq 'p') {
6464            ## has an element in scope
6465            my $i;
6466            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6467              my $node = $self->{open_elements}->[$_];
6468              if ($node->[1] & P_EL) {
6469                !!!cp ('t410.1');
6470                $i = $_;
6471                last INSCOPE;
6472              } elsif ($node->[1] & SCOPING_EL) {
6473                !!!cp ('t411.1');
6474                last INSCOPE;
6475              }
6476            } # INSCOPE
6477    
6478            if (defined $i) {
6479              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6480                      ne $token->{tag_name}) {
6481                !!!cp ('t412.1');
6482                !!!parse-error (type => 'not closed',
6483                                value => $self->{open_elements}->[-1]->[0]
6484                                    ->manakai_local_name,
6485                                token => $token);
6486              } else {
6487                !!!cp ('t414.1');
6488              }
6489    
6490              splice @{$self->{open_elements}}, $i;
6491            } else {
6492              !!!cp ('t413.1');
6493              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6494    
6495              !!!cp ('t415.1');
6496              ## As if <p>, then reprocess the current token
6497              my $el;
6498              !!!create-element ($el, 'p',, $token);
6499              $insert->($el);
6500              ## NOTE: Not inserted into |$self->{open_elements}|.
6501            }
6502    
6503          !!!next-token;          !!!next-token;
6504          redo B;          redo B;
6505        } elsif ({        } elsif ({
# Line 5106  sub _tree_construction_main ($) { Line 6508  sub _tree_construction_main ($) {
6508                  nobr => 1, s => 1, small => 1, strile => 1,                  nobr => 1, s => 1, small => 1, strile => 1,
6509                  strong => 1, tt => 1, u => 1,                  strong => 1, tt => 1, u => 1,
6510                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6511          $formatting_end_tag->($token->{tag_name});          !!!cp ('t427');
6512            $formatting_end_tag->($token);
6513          redo B;          redo B;
6514        } elsif ($token->{tag_name} eq 'br') {        } elsif ($token->{tag_name} eq 'br') {
6515          !!!parse-error (type => 'unmatched end tag:br');          !!!cp ('t428');
6516            !!!parse-error (type => 'unmatched end tag:br', token => $token);
6517    
6518          ## As if <br>          ## As if <br>
6519          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6520                    
6521          my $el;          my $el;
6522          !!!create-element ($el, 'br');          !!!create-element ($el, 'br',, $token);
6523          $insert->($el);          $insert->($el);
6524                    
6525          ## Ignore the token.          ## Ignore the token.
# Line 5133  sub _tree_construction_main ($) { Line 6537  sub _tree_construction_main ($) {
6537                  table => 1, textarea => 1, wbr => 1,                  table => 1, textarea => 1, wbr => 1,
6538                  noscript => 0, ## TODO: if scripting is enabled                  noscript => 0, ## TODO: if scripting is enabled
6539                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6540          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!cp ('t429');
6541            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6542          ## Ignore the token          ## Ignore the token
6543          !!!next-token;          !!!next-token;
6544          redo B;          redo B;
# Line 5147  sub _tree_construction_main ($) { Line 6552  sub _tree_construction_main ($) {
6552    
6553          ## Step 2          ## Step 2
6554          S2: {          S2: {
6555            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6556              ## Step 1              ## Step 1
6557              ## generate implied end tags              ## generate implied end tags
6558              if ({              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
6559                   dd => 1, dt => 1, li => 1, p => 1,                !!!cp ('t430');
6560                   td => 1, th => 1, tr => 1,                ## ISSUE: Can this case be reached?
6561                   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;  
6562              }              }
6563                    
6564              ## Step 2              ## Step 2
6565              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6566                        ne $token->{tag_name}) {
6567                  !!!cp ('t431');
6568                ## NOTE: <x><y></x>                ## NOTE: <x><y></x>
6569                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed',
6570                                  value => $self->{open_elements}->[-1]->[0]
6571                                      ->manakai_local_name,
6572                                  token => $token);
6573                } else {
6574                  !!!cp ('t432');
6575              }              }
6576                            
6577              ## Step 3              ## Step 3
# Line 5174  sub _tree_construction_main ($) { Line 6581  sub _tree_construction_main ($) {
6581              last S2;              last S2;
6582            } else {            } else {
6583              ## Step 3              ## Step 3
6584              if (not $formatting_category->{$node->[1]} and              if (not ($node->[1] & FORMATTING_EL) and
6585                  #not $phrasing_category->{$node->[1]} and                  #not $phrasing_category->{$node->[1]} and
6586                  ($special_category->{$node->[1]} or                  ($node->[1] & SPECIAL_EL or
6587                   $scoping_category->{$node->[1]})) {                   $node->[1] & SCOPING_EL)) {
6588                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!cp ('t433');
6589                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6590                ## Ignore the token                ## Ignore the token
6591                !!!next-token;                !!!next-token;
6592                last S2;                last S2;
6593              }              }
6594    
6595                !!!cp ('t434');
6596            }            }
6597                        
6598            ## Step 4            ## Step 4
# Line 5198  sub _tree_construction_main ($) { Line 6608  sub _tree_construction_main ($) {
6608      redo B;      redo B;
6609    } # B    } # B
6610    
   ## 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  
   
6611    ## Stop parsing # MUST    ## Stop parsing # MUST
6612        
6613    ## TODO: script stuffs    ## TODO: script stuffs
# Line 5214  sub set_inner_html ($$$) { Line 6619  sub set_inner_html ($$$) {
6619    my $s = \$_[0];    my $s = \$_[0];
6620    my $onerror = $_[1];    my $onerror = $_[1];
6621    
6622      ## ISSUE: Should {confident} be true?
6623    
6624    my $nt = $node->node_type;    my $nt = $node->node_type;
6625    if ($nt == 9) {    if ($nt == 9) {
6626      # MUST      # MUST
# Line 5242  sub set_inner_html ($$$) { Line 6649  sub set_inner_html ($$$) {
6649      my $p = $class->new;      my $p = $class->new;
6650      $p->{document} = $doc;      $p->{document} = $doc;
6651    
6652      ## Step 9 # MUST      ## Step 8 # MUST
6653      my $i = 0;      my $i = 0;
6654      my $line = 1;      $p->{line_prev} = $p->{line} = 1;
6655      my $column = 0;      $p->{column_prev} = $p->{column} = 0;
6656      $p->{set_next_input_character} = sub {      $p->{set_next_char} = sub {
6657        my $self = shift;        my $self = shift;
6658    
6659        pop @{$self->{prev_input_character}};        pop @{$self->{prev_char}};
6660        unshift @{$self->{prev_input_character}}, $self->{next_input_character};        unshift @{$self->{prev_char}}, $self->{next_char};
6661    
6662          $self->{next_char} = -1 and return if $i >= length $$s;
6663          $self->{next_char} = ord substr $$s, $i++, 1;
6664    
6665        $self->{next_input_character} = -1 and return if $i >= length $$s;        ($p->{line_prev}, $p->{column_prev}) = ($p->{line}, $p->{column});
6666        $self->{next_input_character} = ord substr $$s, $i++, 1;        $p->{column}++;
6667        $column++;  
6668          if ($self->{next_char} == 0x000A) { # LF
6669        if ($self->{next_input_character} == 0x000A) { # LF          $p->{line}++;
6670          $line++;          $p->{column} = 0;
6671          $column = 0;          !!!cp ('i1');
6672        } elsif ($self->{next_input_character} == 0x000D) { # CR        } elsif ($self->{next_char} == 0x000D) { # CR
6673          $i++ if substr ($$s, $i, 1) eq "\x0A";          $i++ if substr ($$s, $i, 1) eq "\x0A";
6674          $self->{next_input_character} = 0x000A; # LF # MUST          $self->{next_char} = 0x000A; # LF # MUST
6675          $line++;          $p->{line}++;
6676          $column = 0;          $p->{column} = 0;
6677        } elsif ($self->{next_input_character} > 0x10FFFF) {          !!!cp ('i2');
6678          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        } elsif ($self->{next_char} > 0x10FFFF) {
6679        } elsif ($self->{next_input_character} == 0x0000) { # NULL          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
6680            !!!cp ('i3');
6681          } elsif ($self->{next_char} == 0x0000) { # NULL
6682            !!!cp ('i4');
6683          !!!parse-error (type => 'NULL');          !!!parse-error (type => 'NULL');
6684          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
6685        }        }
6686      };      };
6687      $p->{prev_input_character} = [-1, -1, -1];      $p->{prev_char} = [-1, -1, -1];
6688      $p->{next_input_character} = -1;      $p->{next_char} = -1;
6689            
6690      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
6691        my (%opt) = @_;        my (%opt) = @_;
6692        warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";        my $line = $opt{line};
6693          my $column = $opt{column};
6694          if (defined $opt{token} and defined $opt{token}->{line}) {
6695            $line = $opt{token}->{line};
6696            $column = $opt{token}->{column};
6697          }
6698          warn "Parse error ($opt{type}) at line $line column $column\n";
6699      };      };
6700      $p->{parse_error} = sub {      $p->{parse_error} = sub {
6701        $ponerror->(@_, line => $line, column => $column);        $ponerror->(line => $p->{line}, column => $p->{column}, @_);
6702      };      };
6703            
6704      $p->_initialize_tokenizer;      $p->_initialize_tokenizer;
6705      $p->_initialize_tree_constructor;      $p->_initialize_tree_constructor;
6706    
6707      ## Step 2      ## Step 2
6708      my $node_ln = $node->local_name;      my $node_ln = $node->manakai_local_name;
6709      $p->{content_model} = {      $p->{content_model} = {
6710        title => RCDATA_CONTENT_MODEL,        title => RCDATA_CONTENT_MODEL,
6711        textarea => RCDATA_CONTENT_MODEL,        textarea => RCDATA_CONTENT_MODEL,
# Line 5303  sub set_inner_html ($$$) { Line 6722  sub set_inner_html ($$$) {
6722          unless defined $p->{content_model};          unless defined $p->{content_model};
6723          ## ISSUE: What is "the name of the element"? local name?          ## ISSUE: What is "the name of the element"? local name?
6724    
6725      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $el_category->{$node_ln}];
6726          ## TODO: Foreign element OK?
6727    
6728      ## Step 4      ## Step 3
6729      my $root = $doc->create_element_ns      my $root = $doc->create_element_ns
6730        ('http://www.w3.org/1999/xhtml', [undef, 'html']);        ('http://www.w3.org/1999/xhtml', [undef, 'html']);
6731    
6732      ## Step 5 # MUST      ## Step 4 # MUST
6733      $doc->append_child ($root);      $doc->append_child ($root);
6734    
6735      ## Step 6 # MUST      ## Step 5 # MUST
6736      push @{$p->{open_elements}}, [$root, 'html'];      push @{$p->{open_elements}}, [$root, $el_category->{html}];
6737    
6738      undef $p->{head_element};      undef $p->{head_element};
6739    
6740      ## Step 7 # MUST      ## Step 6 # MUST
6741      $p->_reset_insertion_mode;      $p->_reset_insertion_mode;
6742    
6743      ## Step 8 # MUST      ## Step 7 # MUST
6744      my $anode = $node;      my $anode = $node;
6745      AN: while (defined $anode) {      AN: while (defined $anode) {
6746        if ($anode->node_type == 1) {        if ($anode->node_type == 1) {
6747          my $nsuri = $anode->namespace_uri;          my $nsuri = $anode->namespace_uri;
6748          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {
6749            if ($anode->local_name eq 'form') { ## TODO: case?            if ($anode->manakai_local_name eq 'form') {
6750                !!!cp ('i5');
6751              $p->{form_element} = $anode;              $p->{form_element} = $anode;
6752              last AN;              last AN;
6753            }            }
# Line 5335  sub set_inner_html ($$$) { Line 6756  sub set_inner_html ($$$) {
6756        $anode = $anode->parent_node;        $anode = $anode->parent_node;
6757      } # AN      } # AN
6758            
6759      ## Step 3 # MUST      ## Step 9 # MUST
     ## Step 10 # MUST  
6760      {      {
6761        my $self = $p;        my $self = $p;
6762        !!!next-token;        !!!next-token;
6763      }      }
6764      $p->_tree_construction_main;      $p->_tree_construction_main;
6765    
6766      ## Step 11 # MUST      ## Step 10 # MUST
6767      my @cn = @{$node->child_nodes};      my @cn = @{$node->child_nodes};
6768      for (@cn) {      for (@cn) {
6769        $node->remove_child ($_);        $node->remove_child ($_);
6770      }      }
6771      ## ISSUE: mutation events? read-only?      ## ISSUE: mutation events? read-only?
6772    
6773      ## Step 12 # MUST      ## Step 11 # MUST
6774      @cn = @{$root->child_nodes};      @cn = @{$root->child_nodes};
6775      for (@cn) {      for (@cn) {
6776        $this_doc->adopt_node ($_);        $this_doc->adopt_node ($_);
# Line 5359  sub set_inner_html ($$$) { Line 6779  sub set_inner_html ($$$) {
6779      ## ISSUE: mutation events?      ## ISSUE: mutation events?
6780    
6781      $p->_terminate_tree_constructor;      $p->_terminate_tree_constructor;
6782    
6783        delete $p->{parse_error}; # delete loop
6784    } else {    } else {
6785      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";
6786    }    }
# Line 5366  sub set_inner_html ($$$) { Line 6788  sub set_inner_html ($$$) {
6788    
6789  } # tree construction stage  } # tree construction stage
6790    
6791  sub get_inner_html ($$$) {  package Whatpm::HTML::RestartParser;
6792    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  
6793    
6794  1;  1;
6795  # $Date$  # $Date$

Legend:
Removed from v.1.61  
changed lines
  Added in v.1.125

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24