/[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.42 by wakaba, Sat Jul 21 06:59:16 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_char} = -1 and return if $i >= length $$s;
327        $self->{next_char} = ord substr $$s, $i++, 1;
328    
329      $self->{next_input_character} = -1 and return if $i >= length $$s;      ($self->{line_prev}, $self->{column_prev})
330      $self->{next_input_character} = ord substr $$s, $i++, 1;          = ($self->{line}, $self->{column});
331      $column++;      $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 {
387        #
388      };
389    return $self;    return $self;
390  } # new  } # new
391    
# Line 159  sub CDATA_CONTENT_MODEL () { CM_LIMITED_ Line 398  sub CDATA_CONTENT_MODEL () { CM_LIMITED_
398  sub RCDATA_CONTENT_MODEL () { CM_ENTITY | CM_LIMITED_MARKUP }  sub RCDATA_CONTENT_MODEL () { CM_ENTITY | CM_LIMITED_MARKUP }
399  sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }  sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }
400    
401    sub DATA_STATE () { 0 }
402    sub ENTITY_DATA_STATE () { 1 }
403    sub TAG_OPEN_STATE () { 2 }
404    sub CLOSE_TAG_OPEN_STATE () { 3 }
405    sub TAG_NAME_STATE () { 4 }
406    sub BEFORE_ATTRIBUTE_NAME_STATE () { 5 }
407    sub ATTRIBUTE_NAME_STATE () { 6 }
408    sub AFTER_ATTRIBUTE_NAME_STATE () { 7 }
409    sub BEFORE_ATTRIBUTE_VALUE_STATE () { 8 }
410    sub ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE () { 9 }
411    sub ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE () { 10 }
412    sub ATTRIBUTE_VALUE_UNQUOTED_STATE () { 11 }
413    sub ENTITY_IN_ATTRIBUTE_VALUE_STATE () { 12 }
414    sub MARKUP_DECLARATION_OPEN_STATE () { 13 }
415    sub COMMENT_START_STATE () { 14 }
416    sub COMMENT_START_DASH_STATE () { 15 }
417    sub COMMENT_STATE () { 16 }
418    sub COMMENT_END_STATE () { 17 }
419    sub COMMENT_END_DASH_STATE () { 18 }
420    sub BOGUS_COMMENT_STATE () { 19 }
421    sub DOCTYPE_STATE () { 20 }
422    sub BEFORE_DOCTYPE_NAME_STATE () { 21 }
423    sub DOCTYPE_NAME_STATE () { 22 }
424    sub AFTER_DOCTYPE_NAME_STATE () { 23 }
425    sub BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE () { 24 }
426    sub DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE () { 25 }
427    sub DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE () { 26 }
428    sub AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE () { 27 }
429    sub BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 28 }
430    sub DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE () { 29 }
431    sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE () { 30 }
432    sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }
433    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 }
438    sub COMMENT_TOKEN () { 2 }
439    sub START_TAG_TOKEN () { 3 }
440    sub END_TAG_TOKEN () { 4 }
441    sub END_OF_FILE_TOKEN () { 5 }
442    sub CHARACTER_TOKEN () { 6 }
443    
444    sub AFTER_HTML_IMS () { 0b100 }
445    sub HEAD_IMS ()       { 0b1000 }
446    sub BODY_IMS ()       { 0b10000 }
447    sub BODY_TABLE_IMS () { 0b100000 }
448    sub TABLE_IMS ()      { 0b1000000 }
449    sub ROW_IMS ()        { 0b10000000 }
450    sub BODY_AFTER_IMS () { 0b100000000 }
451    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 }
458    
459    ## NOTE: "after after frameset" insertion mode.
460    sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }
461    
462    sub IN_HEAD_IM () { HEAD_IMS | 0b00 }
463    sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }
464    sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }
465    sub BEFORE_HEAD_IM () { HEAD_IMS | 0b11 }
466    sub IN_BODY_IM () { BODY_IMS }
467    sub IN_CELL_IM () { BODY_IMS | BODY_TABLE_IMS | 0b01 }
468    sub IN_CAPTION_IM () { BODY_IMS | BODY_TABLE_IMS | 0b10 }
469    sub IN_ROW_IM () { TABLE_IMS | ROW_IMS | 0b01 }
470    sub IN_TABLE_BODY_IM () { TABLE_IMS | ROW_IMS | 0b10 }
471    sub IN_TABLE_IM () { TABLE_IMS }
472    sub AFTER_BODY_IM () { BODY_AFTER_IMS }
473    sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }
474    sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }
475    sub IN_SELECT_IM () { SELECT_IMS | 0b01 }
476    sub IN_SELECT_IN_TABLE_IM () { SELECT_IMS | 0b10 }
477    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
480    
481  sub _initialize_tokenizer ($) {  sub _initialize_tokenizer ($) {
482    my $self = shift;    my $self = shift;
483    $self->{state} = 'data'; # MUST    $self->{state} = DATA_STATE; # MUST
484    $self->{content_model} = PCDATA_CONTENT_MODEL; # be    $self->{content_model} = PCDATA_CONTENT_MODEL; # be
485    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE
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}
495  } # _initialize_tokenizer  } # _initialize_tokenizer
496    
497  ## A token has:  ## A token has:
498  ##   ->{type} eq 'DOCTYPE', 'start tag', 'end tag', 'comment',  ##   ->{type} == DOCTYPE_TOKEN, START_TAG_TOKEN, END_TAG_TOKEN, COMMENT_TOKEN,
499  ##       'character', or 'end-of-file'  ##       CHARACTER_TOKEN, or END_OF_FILE_TOKEN
500  ##   ->{name} (DOCTYPE, start tag (tag name), end tag (tag name))  ##   ->{name} (DOCTYPE_TOKEN)
501  ##   ->{public_identifier} (DOCTYPE)  ##   ->{tag_name} (START_TAG_TOKEN, END_TAG_TOKEN)
502  ##   ->{system_identifier} (DOCTYPE)  ##   ->{public_identifier} (DOCTYPE_TOKEN)
503  ##   ->{correct} == 1 or 0 (DOCTYPE)  ##   ->{system_identifier} (DOCTYPE_TOKEN)
504  ##   ->{attributes} isa HASH (start tag, end tag)  ##   ->{quirks} == 1 or 0 (DOCTYPE_TOKEN): "force-quirks" flag
505  ##   ->{data} (comment, character)  ##   ->{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)
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 194  sub _initialize_tokenizer ($) { Line 523  sub _initialize_tokenizer ($) {
523  ## has completed loading.  If one has, then it MUST be executed  ## has completed loading.  If one has, then it MUST be executed
524  ## and removed from the list.  ## and removed from the list.
525    
526    ## NOTE: HTML5 "Writing HTML documents" section, applied to
527    ## documents and not to user agents and conformance checkers,
528    ## contains some requirements that are not detected by the
529    ## parsing algorithm:
530    ## - Some requirements on character encoding declarations. ## TODO
531    ## - "Elements MUST NOT contain content that their content model disallows."
532    ##   ... Some are parse error, some are not (will be reported by c.c.).
533    ## - Polytheistic slash SHOULD NOT be used. (Applied only to atheists.) ## TODO
534    ## - Text (in elements, attributes, and comments) SHOULD NOT contain
535    ##   control characters other than space characters. ## TODO: (what is control character? C0, C1 and DEL?  Unicode control character?)
536    
537    ## TODO: HTML5 poses authors two SHOULD-level requirements that cannot
538    ## be detected by the HTML5 parsing algorithm:
539    ## - Text,
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} eq 'data') {      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            $self->{state} = 'entity data';              not $self->{escape}) {
562              !!!cp (1);
563              $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            $self->{state} = 'tag open';            !!!cp (6);
592              $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'});          !!!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',        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    
630        !!!emit ($token);        !!!emit ($token);
631    
632        redo A;        redo A;
633      } elsif ($self->{state} eq 'entity data') {      } 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';        $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', 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} eq 'tag open') {      } 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';            $self->{state} = CLOSE_TAG_OPEN_STATE;
660            redo A;            redo A;
661          } else {          } else {
662              !!!cp (16);
663            ## reconsume            ## reconsume
664            $self->{state} = 'data';            $self->{state} = DATA_STATE;
665    
666            !!!emit ({type => 'character', 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            $self->{state} = 'markup declaration open';            !!!cp (17);
676              $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            $self->{state} = 'close tag open';            !!!cp (18);
681              $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',              = {type => START_TAG_TOKEN,
689                 tag_name => chr ($self->{next_input_character} + 0x0020)};                 tag_name => chr ($self->{next_char} + 0x0020),
690            $self->{state} = 'tag name';                 line => $self->{line_prev},
691                   column => $self->{column_prev}};
692              $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            $self->{current_token} = {type => 'start tag',            !!!cp (20);
698                              tag_name => chr ($self->{next_input_character})};            $self->{current_token} = {type => START_TAG_TOKEN,
699            $self->{state} = 'tag name';                                      tag_name => chr ($self->{next_char}),
700                                        line => $self->{line_prev},
701                                        column => $self->{column_prev}};
702              $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            $self->{state} = 'data';            !!!parse-error (type => 'empty start tag',
708                              line => $self->{line_prev},
709                              column => $self->{column_prev});
710              $self->{state} = DATA_STATE;
711            !!!next-input-character;            !!!next-input-character;
712    
713            !!!emit ({type => 'character', 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            $self->{state} = 'bogus comment';            !!!parse-error (type => 'pio',
722            ## $self->{next_input_character} is intentionally left as is                            line => $self->{line_prev},
723                              column => $self->{column_prev});
724              $self->{state} = BOGUS_COMMENT_STATE;
725              $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';            $self->{state} = DATA_STATE;
735            ## reconsume            ## reconsume
736    
737            !!!emit ({type => 'character', data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
738                        line => $self->{line_prev},
739                        column => $self->{column_prev},
740                       });
741    
742            redo A;            redo A;
743          }          }
744        } else {        } else {
745          die "$0: $self->{content_model} in tag open";          die "$0: $self->{content_model} in tag open";
746        }        }
747      } elsif ($self->{state} eq 'close tag open') {      } 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';                $self->{state} = DATA_STATE;
767    
768                !!!emit ({type => 'character', 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';              $self->{state} = DATA_STATE;
789              !!!emit ({type => 'character', 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';            $self->{state} = DATA_STATE;
804            !!!emit ({type => 'character', 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',          !!!cp (29);
814                            tag_name => chr ($self->{next_input_character} + 0x0020)};          $self->{current_token}
815          $self->{state} = 'tag name';              = {type => END_TAG_TOKEN,
816          !!!next-input-character;                 tag_name => chr ($self->{next_char} + 0x0020),
817          redo A;                 line => $l, column => $c};
818        } elsif (0x0061 <= $self->{next_input_character} and          $self->{state} = TAG_NAME_STATE;
819                 $self->{next_input_character} <= 0x007A) { # a..z          !!!next-input-character;
820          $self->{current_token} = {type => 'end tag',          redo A;
821                            tag_name => chr ($self->{next_input_character})};        } elsif (0x0061 <= $self->{next_char} and
822          $self->{state} = 'tag name';                 $self->{next_char} <= 0x007A) { # a..z
823          !!!next-input-character;          !!!cp (30);
824          redo A;          $self->{current_token} = {type => END_TAG_TOKEN,
825        } elsif ($self->{next_input_character} == 0x003E) { # >                                    tag_name => chr ($self->{next_char}),
826          !!!parse-error (type => 'empty end tag');                                    line => $l, column => $c};
827          $self->{state} = 'data';          $self->{state} = TAG_NAME_STATE;
828            !!!next-input-character;
829            redo A;
830          } elsif ($self->{next_char} == 0x003E) { # >
831            !!!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;
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';          $self->{state} = DATA_STATE;
842          # reconsume          # reconsume
843    
844          !!!emit ({type => 'character', 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';          $self->{state} = BOGUS_COMMENT_STATE;
853          ## $self->{next_input_character} is intentionally left as is          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
854          redo A;                                    line => $self->{line_prev}, # "<" of "</"
855        }                                    column => $self->{column_prev} - 1,
856      } elsif ($self->{state} eq 'tag name') {                                   };
857        if ($self->{next_input_character} == 0x0009 or # HT          ## $self->{next_char} is intentionally left as is
858            $self->{next_input_character} == 0x000A or # LF          redo A;
859            $self->{next_input_character} == 0x000B or # VT        }
860            $self->{next_input_character} == 0x000C or # FF      } elsif ($self->{state} == TAG_NAME_STATE) {
861            $self->{next_input_character} == 0x0020) { # SP        if ($self->{next_char} == 0x0009 or # HT
862          $self->{state} = 'before attribute name';            $self->{next_char} == 0x000A or # LF
863          !!!next-input-character;            $self->{next_char} == 0x000B or # VT
864          redo A;            $self->{next_char} == 0x000C or # FF
865        } elsif ($self->{next_input_character} == 0x003E) { # >            $self->{next_char} == 0x0020) { # SP
866          if ($self->{current_token}->{type} eq 'start tag') {          !!!cp (34);
867            $self->{current_token}->{first_start_tag}          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
868                = not defined $self->{last_emitted_start_tag_name};          !!!next-input-character;
869            redo A;
870          } elsif ($self->{next_char} == 0x003E) { # >
871            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
872              !!!cp (35);
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} eq 'end tag') {          } 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          }          }
886          $self->{state} = 'data';          $self->{state} = DATA_STATE;
887          !!!next-input-character;          !!!next-input-character;
888    
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} eq 'start tag') {          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} eq 'end tag') {          } 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          }          }
917          $self->{state} = 'data';          $self->{state} = DATA_STATE;
918          # reconsume          # reconsume
919    
920          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
921    
922          redo A;          redo A;
923        } elsif ($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} eq 'start tag' and  
             $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {  
           # permitted slash  
           #  
         } else {  
           !!!parse-error (type => 'nestc');  
         }  
         $self->{state} = 'before attribute name';  
         # next-input-character is already done  
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} eq 'before attribute name') {      } 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} eq 'start tag') {          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} eq 'end tag') {          } 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";
960          }          }
961          $self->{state} = 'data';          $self->{state} = DATA_STATE;
962          !!!next-input-character;          !!!next-input-character;
963    
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          $self->{state} = 'attribute name';              = {name => chr ($self->{next_char} + 0x0020),
972                   value => '',
973                   line => $self->{line}, column => $self->{column}};
974            $self->{state} = ATTRIBUTE_NAME_STATE;
975            !!!next-input-character;
976            redo A;
977          } elsif ($self->{next_char} == 0x002F) { # /
978            !!!cp (50);
979            $self->{state} = SELF_CLOSING_START_TAG_STATE;
980          !!!next-input-character;          !!!next-input-character;
981          redo A;          redo A;
982        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == -1) {
         !!!next-input-character;  
         if ($self->{next_input_character} == 0x003E and # >  
             $self->{current_token}->{type} eq 'start tag' and  
             $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {  
           # permitted slash  
           #  
         } else {  
           !!!parse-error (type => 'nestc');  
         }  
         ## Stay in the state  
         # next-input-character is already done  
         redo A;  
       } elsif ($self->{next_input_character} == -1) {  
983          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
984          if ($self->{current_token}->{type} eq 'start tag') {          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} eq 'end tag') {          } 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";
997          }          }
998          $self->{state} = 'data';          $self->{state} = DATA_STATE;
999          # reconsume          # reconsume
1000    
1001          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
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          $self->{state} = 'attribute name';               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;
1020          !!!next-input-character;          !!!next-input-character;
1021          redo A;          redo A;
1022        }        }
1023      } elsif ($self->{state} eq 'attribute name') {      } elsif ($self->{state} == ATTRIBUTE_NAME_STATE) {
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';          $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';          $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} eq 'start tag') {          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} eq 'end tag') {          } 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 607  sub _get_next_token ($) { Line 1064  sub _get_next_token ($) {
1064          } else {          } else {
1065            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1066          }          }
1067          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1068          !!!next-input-character;          !!!next-input-character;
1069    
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} eq 'start tag' and  
             $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {  
           # permitted slash  
           #  
         } else {  
           !!!parse-error (type => 'nestc');  
         }  
         $self->{state} = 'before attribute name';  
         # next-input-character is already done  
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} eq 'start tag') {          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} eq 'end tag') {          } 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";
1103          }          }
1104          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1105          # reconsume          # reconsume
1106    
1107          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
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} eq 'after attribute name') {      } 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          $self->{state} = 'before attribute value';          !!!cp (72);
1135            $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} eq 'start tag') {          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} eq 'end tag') {          } 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";
1153          }          }
1154          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1155          !!!next-input-character;          !!!next-input-character;
1156    
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          $self->{state} = 'attribute name';              = {name => chr ($self->{next_char} + 0x0020),
1165                   value => '',
1166                   line => $self->{line}, column => $self->{column}};
1167            $self->{state} = ATTRIBUTE_NAME_STATE;
1168            !!!next-input-character;
1169            redo A;
1170          } elsif ($self->{next_char} == 0x002F) { # /
1171            !!!cp (77);
1172            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1173          !!!next-input-character;          !!!next-input-character;
1174          redo A;          redo A;
1175        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == -1) {
         !!!next-input-character;  
         if ($self->{next_input_character} == 0x003E and # >  
             $self->{current_token}->{type} eq 'start tag' and  
             $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {  
           # permitted slash  
           #  
         } else {  
           !!!parse-error (type => 'nestc');  
           ## TODO: Different error type for <aa / bb> than <aa/>  
         }  
         $self->{state} = 'before attribute name';  
         # next-input-character is already done  
         redo A;  
       } elsif ($self->{next_input_character} == -1) {  
1176          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1177          if ($self->{current_token}->{type} eq 'start tag') {          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} eq 'end tag') {          } 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";
1191          }          }
1192          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1193          # reconsume          # reconsume
1194    
1195          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
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          $self->{state} = 'attribute name';              = {name => chr ($self->{next_char}),
1202                   value => '',
1203                   line => $self->{line}, column => $self->{column}};
1204            $self->{state} = ATTRIBUTE_NAME_STATE;
1205          !!!next-input-character;          !!!next-input-character;
1206          redo A;                  redo A;        
1207        }        }
1208      } elsif ($self->{state} eq 'before attribute value') {      } 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          $self->{state} = 'attribute value (double-quoted)';          !!!cp (84);
1220            $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          $self->{state} = 'attribute value (unquoted)';          !!!cp (85);
1225            $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          $self->{state} = 'attribute value (single-quoted)';          !!!cp (86);
1230            $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} eq 'start tag') {          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} eq 'end tag') {          } 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";
1248          }          }
1249          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1250          !!!next-input-character;          !!!next-input-character;
1251    
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} eq 'start tag') {          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} eq 'end tag') {          } 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";
1271          }          }
1272          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1273          ## reconsume          ## reconsume
1274    
1275          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
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          $self->{state} = 'attribute value (unquoted)';            !!!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;
1287          !!!next-input-character;          !!!next-input-character;
1288          redo A;          redo A;
1289        }        }
1290      } elsif ($self->{state} eq 'attribute value (double-quoted)') {      } 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';          !!!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          $self->{last_attribute_value_state} = 'attribute value (double-quoted)';          !!!cp (96);
1298          $self->{state} = 'entity in attribute value';          $self->{last_attribute_value_state} = $self->{state};
1299            $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} eq 'start tag') {          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} eq 'end tag') {          } 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";
1318          }          }
1319          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1320          ## reconsume          ## reconsume
1321    
1322          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
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} eq 'attribute value (single-quoted)') {      } 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';          !!!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          $self->{last_attribute_value_state} = 'attribute value (single-quoted)';          !!!cp (102);
1340          $self->{state} = 'entity in attribute value';          $self->{last_attribute_value_state} = $self->{state};
1341            $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} eq 'start tag') {          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} eq 'end tag') {          } 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";
1360          }          }
1361          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1362          ## reconsume          ## reconsume
1363    
1364          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
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} eq 'attribute value (unquoted)') {      } 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          $self->{state} = 'before attribute name';          !!!cp (107);
1381          !!!next-input-character;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1382          redo A;          !!!next-input-character;
1383        } elsif ($self->{next_input_character} == 0x0026) { # &          redo A;
1384          $self->{last_attribute_value_state} = 'attribute value (unquoted)';        } elsif ($self->{next_char} == 0x0026) { # &
1385          $self->{state} = 'entity in attribute value';          !!!cp (108);
1386          !!!next-input-character;          $self->{last_attribute_value_state} = $self->{state};
1387          redo A;          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1388        } elsif ($self->{next_input_character} == 0x003E) { # >          !!!next-input-character;
1389          if ($self->{current_token}->{type} eq 'start tag') {          redo A;
1390            $self->{current_token}->{first_start_tag}        } elsif ($self->{next_char} == 0x003E) { # >
1391                = not defined $self->{last_emitted_start_tag_name};          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1392              !!!cp (109);
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} eq 'end tag') {          } 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";
1405          }          }
1406          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1407          !!!next-input-character;          !!!next-input-character;
1408    
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} eq 'start tag') {          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} eq 'end tag') {          } 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";
1428          }          }
1429          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1430          ## reconsume          ## reconsume
1431    
1432          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
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} eq 'entity in attribute value') {      } 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} eq 'bogus comment') {      } 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) {
1551        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1552                
1553        my $token = {type => 'comment', 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            $self->{state} = 'data';            !!!cp (124);
1559              $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            $self->{state} = 'data';            !!!cp (125);
1567              $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      } elsif ($self->{state} eq 'markup declaration open') {  
1581          die "$0: _get_next_token: unexpected case [BC]";
1582        } 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', data => ''};            !!!cp (127);
1595            $self->{state} = 'comment start';            $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1596                                        line => $l, column => $c,
1597                                       };
1598              $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                      $self->{state} = 'DOCTYPE';                      ## TODO: What a stupid code this is!
1632                        $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';        $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} eq 'comment start') {      } elsif ($self->{state} == COMMENT_START_STATE) {
1673        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
1674          $self->{state} = 'comment start dash';          !!!cp (137);
1675            $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';          $self->{state} = DATA_STATE;
1682          !!!next-input-character;          !!!next-input-character;
1683    
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';          $self->{state} = DATA_STATE;
1691          ## reconsume          ## reconsume
1692    
1693          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
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';          $self->{state} = COMMENT_STATE;
1701          !!!next-input-character;          !!!next-input-character;
1702          redo A;          redo A;
1703        }        }
1704      } elsif ($self->{state} eq 'comment start dash') {      } elsif ($self->{state} == COMMENT_START_DASH_STATE) {
1705        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
1706          $self->{state} = 'comment end';          !!!cp (141);
1707            $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';          $self->{state} = DATA_STATE;
1714          !!!next-input-character;          !!!next-input-character;
1715    
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';          $self->{state} = DATA_STATE;
1723          ## reconsume          ## reconsume
1724    
1725          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
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';          $self->{state} = COMMENT_STATE;
1733          !!!next-input-character;          !!!next-input-character;
1734          redo A;          redo A;
1735        }        }
1736      } elsif ($self->{state} eq 'comment') {      } elsif ($self->{state} == COMMENT_STATE) {
1737        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
1738          $self->{state} = 'comment end dash';          !!!cp (145);
1739            $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';          $self->{state} = DATA_STATE;
1746          ## reconsume          ## reconsume
1747    
1748          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
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} eq 'comment end dash') {      } elsif ($self->{state} == COMMENT_END_DASH_STATE) {
1759        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
1760          $self->{state} = 'comment end';          !!!cp (148);
1761            $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';          $self->{state} = DATA_STATE;
1768          ## reconsume          ## reconsume
1769    
1770          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
1771    
1772          redo A;          redo A;
1773        } else {        } else {
1774          $self->{current_token}->{data} .= '-' . chr ($self->{next_input_character}); # comment          !!!cp (150);
1775          $self->{state} = 'comment';          $self->{current_token}->{data} .= '-' . chr ($self->{next_char}); # comment
1776            $self->{state} = COMMENT_STATE;
1777          !!!next-input-character;          !!!next-input-character;
1778          redo A;          redo A;
1779        }        }
1780      } elsif ($self->{state} eq 'comment end') {      } elsif ($self->{state} == COMMENT_END_STATE) {
1781        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_char} == 0x003E) { # >
1782          $self->{state} = 'data';          !!!cp (151);
1783            $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';          $self->{state} = DATA_STATE;
1802          ## reconsume          ## reconsume
1803    
1804          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
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          $self->{state} = 'comment';                          line => $self->{line_prev},
1811                            column => $self->{column_prev});
1812            $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment
1813            $self->{state} = COMMENT_STATE;
1814          !!!next-input-character;          !!!next-input-character;
1815          redo A;          redo A;
1816        }        }
1817      } elsif ($self->{state} eq 'DOCTYPE') {      } 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          $self->{state} = 'before DOCTYPE name';          !!!cp (155);
1824            $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';          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
1831          ## reconsume          ## reconsume
1832          redo A;          redo A;
1833        }        }
1834      } elsif ($self->{state} eq 'before DOCTYPE name') {      } 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';          $self->{state} = DATA_STATE;
1848          !!!next-input-character;          !!!next-input-character;
1849    
1850          !!!emit ({type => 'DOCTYPE'}); # 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';          $self->{state} = DATA_STATE;
1857          ## reconsume          ## reconsume
1858    
1859          !!!emit ({type => 'DOCTYPE'}); # 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',          $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';          $self->{state} = DOCTYPE_NAME_STATE;
1868          !!!next-input-character;          !!!next-input-character;
1869          redo A;          redo A;
1870        }        }
1871      } elsif ($self->{state} eq 'DOCTYPE name') {      } 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          $self->{state} = 'after DOCTYPE name';          !!!cp (161);
1879            $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          $self->{state} = 'data';          !!!cp (162);
1884            $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';          $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} eq 'after DOCTYPE name') {      } 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          $self->{state} = 'data';          !!!cp (166);
1920            $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';          $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                    $self->{state} = 'before DOCTYPE public identifier';                    !!!cp (168);
1954                      $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                    $self->{state} = 'before DOCTYPE system identifier';                    !!!cp (174);
1992                      $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->{state} = 'bogus DOCTYPE';        $self->{current_token}->{quirks} = 1;
2020    
2021          $self->{state} = BOGUS_DOCTYPE_STATE;
2022        # next-input-character is already done        # next-input-character is already done
2023        redo A;        redo A;
2024      } elsif ($self->{state} eq 'before DOCTYPE public identifier') {      } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
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)';          $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)';          $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';          $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';          $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->{state} = 'bogus DOCTYPE';          $self->{current_token}->{quirks} = 1;
2071    
2072            $self->{state} = BOGUS_DOCTYPE_STATE;
2073          !!!next-input-character;          !!!next-input-character;
2074          redo A;          redo A;
2075        }        }
2076      } elsif ($self->{state} eq 'DOCTYPE public identifier (double-quoted)') {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2077        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
2078          $self->{state} = 'after DOCTYPE public identifier';          !!!cp (187);
2079            $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');          !!!parse-error (type => 'unclosed PUBLIC literal');
2085    
2086          $self->{state} = 'data';          $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');
2096    
2097            $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} eq 'DOCTYPE public identifier (single-quoted)') {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {
2113        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
2114          $self->{state} = 'after DOCTYPE public identifier';          !!!cp (191);
2115            $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');          !!!parse-error (type => 'unclosed PUBLIC literal');
2121    
2122          $self->{state} = 'data';          $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');
2132    
2133            $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;
2147        }        }
2148      } elsif ($self->{state} eq 'after DOCTYPE public identifier') {      } elsif ($self->{state} == AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
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)';          $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)';          $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          $self->{state} = 'data';          !!!cp (198);
2171            $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';          $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->{state} = 'bogus DOCTYPE';          $self->{current_token}->{quirks} = 1;
2192    
2193            $self->{state} = BOGUS_DOCTYPE_STATE;
2194          !!!next-input-character;          !!!next-input-character;
2195          redo A;          redo A;
2196        }        }
2197      } elsif ($self->{state} eq 'before DOCTYPE system identifier') {      } elsif ($self->{state} == BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
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)';          $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)';          $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';          $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';          $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->{state} = 'bogus DOCTYPE';          $self->{current_token}->{quirks} = 1;
2243    
2244            $self->{state} = BOGUS_DOCTYPE_STATE;
2245          !!!next-input-character;          !!!next-input-character;
2246          redo A;          redo A;
2247        }        }
2248      } elsif ($self->{state} eq 'DOCTYPE system identifier (double-quoted)') {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2249        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
2250          $self->{state} = 'after DOCTYPE system identifier';          !!!cp (207);
2251            $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';          $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} eq 'DOCTYPE system identifier (single-quoted)') {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {
2285        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
2286          $self->{state} = 'after DOCTYPE system identifier';          !!!cp (211);
2287            $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2288            !!!next-input-character;
2289            redo A;
2290          } 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;          !!!next-input-character;
2296    
2297            $self->{current_token}->{quirks} = 1;
2298            !!!emit ($self->{current_token}); # DOCTYPE
2299    
2300          redo A;          redo A;
2301        } elsif ($self->{next_input_character} == -1) {        } 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';          $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;
2319        }        }
2320      } elsif ($self->{state} eq 'after DOCTYPE system identifier') {      } elsif ($self->{state} == AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
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          $self->{state} = 'data';          !!!cp (216);
2331            $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';          $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->{state} = 'bogus DOCTYPE';          #$self->{current_token}->{quirks} = 1;
2352    
2353            $self->{state} = BOGUS_DOCTYPE_STATE;
2354          !!!next-input-character;          !!!next-input-character;
2355          redo A;          redo A;
2356        }        }
2357      } elsif ($self->{state} eq 'bogus DOCTYPE') {      } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {
2358        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_char} == 0x003E) { # >
2359          $self->{state} = 'data';          !!!cp (219);
2360            $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';          $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 1609  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', 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', 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 1726  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', 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', data => '&'.$entity_name};          !!!cp (1024);
2571        } else {          return {type => CHARACTER_TOKEN, data => '&'.$entity_name,
2572          return {type => 'character', 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', 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 1806  sub _construct_tree ($) { Line 2626  sub _construct_tree ($) {
2626        
2627    !!!next-token;    !!!next-token;
2628    
   $self->{insertion_mode} = 'before head';  
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} eq 'DOCTYPE') {      if ($token->{type} == DOCTYPE_TOKEN) {
2652        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"
2653        ## error, switch to a conformance checking mode for another        ## error, switch to a conformance checking mode for another
2654        ## language.        ## language.
# Line 1830  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 1846  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 1900  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 1922  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 ({
2802                'start tag' => 1,                START_TAG_TOKEN, 1,
2803                'end tag' => 1,                END_TAG_TOKEN, 1,
2804                'end-of-file' => 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} eq 'character') {      } 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} eq 'comment') {      } 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";        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} eq 'DOCTYPE') {        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} eq 'comment') {        } 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} eq 'character') {        } elsif ($token->{type} == CHARACTER_TOKEN) {
2871          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
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);
2887    
2888          #          #
2889          } elsif ($token->{type} == START_TAG_TOKEN) {
2890            if ($token->{tag_name} eq 'html') {
2891              my $root_element;
2892              !!!create-element ($root_element, $token->{tag_name}, $token->{attributes}, $token);
2893              $self->{document}->append_child ($root_element);
2894              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 {
2915              !!!cp ('t25.1');
2916              #
2917            }
2918        } elsif ({        } elsif ({
2919                  'start tag' => 1,                  END_TAG_TOKEN, 1,
2920                  'end tag' => 1,                  END_OF_FILE_TOKEN, 1,
                 'end-of-file' => 1,  
2921                 }->{$token->{type}}) {                 }->{$token->{type}}) {
2922          ## ISSUE: There is an issue in the spec          !!!cp ('t26');
2923          #          #
2924        } else {        } else {
2925          die "$0: $token->{type}: Unknown token";          die "$0: $token->{type}: Unknown token type";
2926        }        }
2927        my $root_element; !!!create-element ($root_element, 'html');  
2928        $self->{document}->append_child ($root_element);      my $root_element; !!!create-element ($root_element, 'html',, $token);
2929        push @{$self->{open_elements}}, [$root_element, 'html'];      $self->{document}->append_child ($root_element);
2930        ## reprocess      push @{$self->{open_elements}}, [$root_element, $el_category->{html}];
2931        #redo B;  
2932        return; ## Go to the main phase.      $self->{application_cache_selection}->(undef);
2933    
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 2043  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 2062  sub _reset_insertion_mode ($) { Line 2968  sub _reset_insertion_mode ($) {
2968            
2969        ## Step 4..13        ## Step 4..13
2970        my $new_mode = {        my $new_mode = {
2971                        select => 'in select',                        select => IN_SELECT_IM,
2972                        td => 'in cell',                        ## NOTE: |option| and |optgroup| do not set
2973                        th => 'in cell',                        ## insertion mode to "in select" by themselves.
2974                        tr => 'in row',                        td => IN_CELL_IM,
2975                        tbody => 'in table body',                        th => IN_CELL_IM,
2976                        thead => 'in table head',                        tr => IN_ROW_IM,
2977                        tfoot => 'in table foot',                        tbody => IN_TABLE_BODY_IM,
2978                        caption => 'in caption',                        thead => IN_TABLE_BODY_IM,
2979                        colgroup => 'in column group',                        tfoot => IN_TABLE_BODY_IM,
2980                        table => 'in table',                        caption => IN_CAPTION_IM,
2981                        head => 'in body', # not in head!                        colgroup => IN_COLUMN_GROUP_IM,
2982                        body => 'in body',                        table => IN_TABLE_IM,
2983                        frameset => 'in frameset',                        head => IN_BODY_IM, # not in head!
2984                       }->{$node->[1]};                        body => IN_BODY_IM,
2985                          frameset => IN_FRAMESET_IM,
2986                         }->{$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            $self->{insertion_mode} = 'before head';            !!!cp ('t29');
2994              $self->{insertion_mode} = BEFORE_HEAD_IM;
2995          } else {          } else {
2996            $self->{insertion_mode} = 'after head';            ## ISSUE: Can this state be reached?
2997              !!!cp ('t30');
2998              $self->{insertion_mode} = AFTER_HEAD_IM;
2999          }          }
3000          return;          return;
3001          } else {
3002            !!!cp ('t31');
3003        }        }
3004                
3005        ## Step 15        ## Step 15
3006        $self->{insertion_mode} = 'in body' and return if $last;        $self->{insertion_mode} = IN_BODY_IM and return if $last;
3007                
3008        ## Step 16        ## Step 16
3009        $i--;        $i--;
# Line 2098  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 ($) {
3020    my $self = shift;    my $self = shift;
3021    
   my $previous_insertion_mode;  
   
3022    my $active_formatting_elements = [];    my $active_formatting_elements = [];
3023    
3024    my $reconstruct_active_formatting_elements = sub { # MUST    my $reconstruct_active_formatting_elements = sub { # MUST
# Line 2121  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 2135  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 2169  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 2204  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} eq 'character') { # 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 2220  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} eq 'end tag' 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} eq 'character') {      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                                
3194      $self->{content_model} = PCDATA_CONTENT_MODEL;      $self->{content_model} = PCDATA_CONTENT_MODEL;
3195    
3196      if ($token->{type} eq 'end tag' 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 2278  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 2307  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 2337  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 2362  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 2384  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 2397  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 2419  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 2436  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 2448  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 2461  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 2491  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    }; # $insert_to_foster        $self->{open_elements}->[-1]->[0]->append_child ($child);
   
   my $in_body = sub {  
     my $insert = shift;  
     if ($token->{type} eq 'start tag') {  
       if ($token->{tag_name} eq 'script') {  
         ## NOTE: This is an "as if in head" code clone  
         $script_start_tag->($insert);  
         return;  
       } elsif ($token->{tag_name} eq 'style') {  
         ## NOTE: This is an "as if in head" code clone  
         $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);  
         return;  
       } elsif ({  
                 base => 1, link => 1,  
                }->{$token->{tag_name}}) {  
         ## NOTE: This is an "as if in head" code clone, only "-t" differs  
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.  
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'meta') {  
         ## NOTE: This is an "as if in head" code clone, only "-t" differs  
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.  
   
         unless ($self->{confident}) {  
           my $charset;  
           if ($token->{attributes}->{charset}) { ## TODO: And if supported  
             $charset = $token->{attributes}->{charset}->{value};  
           }  
           if ($token->{attributes}->{'http-equiv'}) {  
             ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.  
             if ($token->{attributes}->{'http-equiv'}->{value}  
                 =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=  
                     [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|  
                     ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {  
               $charset = defined $1 ? $1 : defined $2 ? $2 : $3;  
             } ## TODO: And if supported  
           }  
           ## TODO: Change the encoding  
         }  
   
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'title') {  
         !!!parse-error (type => 'in body:title');  
         ## NOTE: This is an "as if in head" code clone  
         $parse_rcdata->(RCDATA_CONTENT_MODEL, sub {  
           if (defined $self->{head_element}) {  
             $self->{head_element}->append_child ($_[0]);  
           } else {  
             $insert->($_[0]);  
           }  
         });  
         return;  
       } elsif ($token->{tag_name} eq 'body') {  
         !!!parse-error (type => 'in body:body');  
                 
         if (@{$self->{open_elements}} == 1 or  
             $self->{open_elements}->[1]->[1] ne 'body') {  
           ## Ignore the token  
         } else {  
           my $body_el = $self->{open_elements}->[1]->[0];  
           for my $attr_name (keys %{$token->{attributes}}) {  
             unless ($body_el->has_attribute_ns (undef, $attr_name)) {  
               $body_el->set_attribute_ns  
                 (undef, [undef, $attr_name],  
                  $token->{attributes}->{$attr_name}->{value});  
             }  
           }  
         }  
         !!!next-token;  
         return;  
       } elsif ({  
                 address => 1, blockquote => 1, center => 1, dir => 1,  
                 div => 1, dl => 1, fieldset => 1, listing => 1,  
                 menu => 1, ol => 1, p => 1, ul => 1,  
                 pre => 1,  
                }->{$token->{tag_name}}) {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } 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});  
         if ($token->{tag_name} eq 'pre') {  
           !!!next-token;  
           if ($token->{type} eq 'character') {  
             $token->{data} =~ s/^\x0A//;  
             unless (length $token->{data}) {  
               !!!next-token;  
             }  
           }  
         } else {  
           !!!next-token;  
         }  
         return;  
       } elsif ($token->{tag_name} eq 'form') {  
         if (defined $self->{form_element}) {  
           !!!parse-error (type => 'in form:form');  
           ## Ignore the token  
           !!!next-token;  
           return;  
         } else {  
           ## has a p element in scope  
           INSCOPE: for (reverse @{$self->{open_elements}}) {  
             if ($_->[1] eq 'p') {  
               !!!back-token;  
               $token = {type => 'end tag', tag_name => 'p'};  
               return;  
             } 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];  
           !!!next-token;  
           return;  
         }  
       } 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', tag_name => 'p'};  
             return;  
           } 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;  
         return;  
       } elsif ($token->{tag_name} eq 'dd' or $token->{tag_name} eq 'dt') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## Step 1  
         my $i = -1;  
         my $node = $self->{open_elements}->[$i];  
         LI: {  
           ## Step 2  
           if ($node->[1] eq 'dt' or $node->[1] eq 'dd') {  
             if ($i != -1) {  
               !!!parse-error (type => 'end tag missing:'.  
                               $self->{open_elements}->[-1]->[1]);  
             }  
             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;  
         return;  
       } elsif ($token->{tag_name} eq 'plaintext') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } 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->{content_model} = PLAINTEXT_CONTENT_MODEL;  
             
         !!!next-token;  
         return;  
       } 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', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## NOTE: See <http://html5.org/tools/web-apps-tracker?from=925&to=926>  
         ## has an element in scope  
         #my $i;  
         #INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
         #  my $node = $self->{open_elements}->[$_];  
         #  if ({  
         #       h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
         #      }->{$node->[1]}) {  
         #    $i = $_;  
         #    last INSCOPE;  
         #  } elsif ({  
         #            table => 1, caption => 1, td => 1, th => 1,  
         #            button => 1, marquee => 1, object => 1, html => 1,  
         #           }->{$node->[1]}) {  
         #    last INSCOPE;  
         #  }  
         #} # INSCOPE  
         #    
         #if (defined $i) {  
         #  !!! parse-error (type => 'in hn:hn');  
         #  splice @{$self->{open_elements}}, $i;  
         #}  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'a') {  
         AFE: for my $i (reverse 0..$#$active_formatting_elements) {  
           my $node = $active_formatting_elements->[$i];  
           if ($node->[1] eq 'a') {  
             !!!parse-error (type => 'in a:a');  
               
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'a'};  
             $formatting_end_tag->($token->{tag_name});  
               
             AFE2: for (reverse 0..$#$active_formatting_elements) {  
               if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {  
                 splice @$active_formatting_elements, $_, 1;  
                 last AFE2;  
               }  
             } # AFE2  
             OE: for (reverse 0..$#{$self->{open_elements}}) {  
               if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {  
                 splice @{$self->{open_elements}}, $_, 1;  
                 last OE;  
               }  
             } # OE  
             last AFE;  
           } elsif ($node->[0] eq '#marker') {  
             last AFE;  
           }  
         } # AFE  
             
         $reconstruct_active_formatting_elements->($insert_to_current);  
   
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, $self->{open_elements}->[-1];  
   
         !!!next-token;  
         return;  
       } elsif ({  
                 b => 1, big => 1, em => 1, font => 1, i => 1,  
                 s => 1, small => 1, strile => 1,  
                 strong => 1, tt => 1, u => 1,  
                }->{$token->{tag_name}}) {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, $self->{open_elements}->[-1];  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'nobr') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
   
         ## has a |nobr| element in scope  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq 'nobr') {  
             !!!parse-error (type => 'not closed:nobr');  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'nobr'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, $self->{open_elements}->[-1];  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'button') {  
         ## has a button element in scope  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq 'button') {  
             !!!parse-error (type => 'in button:button');  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'button'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         $reconstruct_active_formatting_elements->($insert_to_current);  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, ['#marker', ''];  
   
         !!!next-token;  
         return;  
       } 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});  
         push @$active_formatting_elements, ['#marker', ''];  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'xmp') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
         $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);  
         return;  
       } 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', tag_name => 'p'};  
             return;  
           } 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';  
             
         !!!next-token;  
         return;  
       } elsif ({  
                 area => 1, basefont => 1, bgsound => 1, br => 1,  
                 embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,  
                 image => 1,  
                }->{$token->{tag_name}}) {  
         if ($token->{tag_name} eq 'image') {  
           !!!parse-error (type => 'image');  
           $token->{tag_name} = 'img';  
         }  
   
         ## NOTE: There is an "as if <br>" code clone.  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         pop @{$self->{open_elements}};  
           
         !!!next-token;  
         return;  
       } 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', tag_name => 'p'};  
             return;  
           } 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;  
         return;  
       } 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;  
         return;  
       } elsif ($token->{tag_name} eq 'isindex') {  
         !!!parse-error (type => 'isindex');  
           
         if (defined $self->{form_element}) {  
           ## Ignore the token  
           !!!next-token;  
           return;  
         } else {  
           my $at = $token->{attributes};  
           my $form_attrs;  
           $form_attrs->{action} = $at->{action} if $at->{action};  
           my $prompt_attr = $at->{prompt};  
           $at->{name} = {name => 'name', value => 'isindex'};  
           delete $at->{action};  
           delete $at->{prompt};  
           my @tokens = (  
                         {type => 'start tag', tag_name => 'form',  
                          attributes => $form_attrs},  
                         {type => 'start tag', tag_name => 'hr'},  
                         {type => 'start tag', tag_name => 'p'},  
                         {type => 'start tag', tag_name => 'label'},  
                        );  
           if ($prompt_attr) {  
             push @tokens, {type => 'character', data => $prompt_attr->{value}};  
           } else {  
             push @tokens, {type => 'character',  
                            data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD  
             ## TODO: make this configurable  
           }  
           push @tokens,  
                         {type => 'start tag', tag_name => 'input', attributes => $at},  
                         #{type => 'character', data => ''}, # SHOULD  
                         {type => 'end tag', tag_name => 'label'},  
                         {type => 'end tag', tag_name => 'p'},  
                         {type => 'start tag', tag_name => 'hr'},  
                         {type => 'end tag', tag_name => 'form'};  
           $token = shift @tokens;  
           !!!back-token (@tokens);  
           return;  
         }  
       } elsif ($token->{tag_name} eq 'textarea') {  
         my $tag_name = $token->{tag_name};  
         my $el;  
         !!!create-element ($el, $token->{tag_name}, $token->{attributes});  
           
         ## TODO: $self->{form_element} if defined  
         $self->{content_model} = RCDATA_CONTENT_MODEL;  
         delete $self->{escape}; # MUST  
           
         $insert->($el);  
           
         my $text = '';  
         !!!next-token;  
         if ($token->{type} eq 'character') {  
           $token->{data} =~ s/^\x0A//;  
           unless (length $token->{data}) {  
             !!!next-token;  
           }  
         }  
         while ($token->{type} eq 'character') {  
           $text .= $token->{data};  
           !!!next-token;  
         }  
         if (length $text) {  
           $el->manakai_append_text ($text);  
         }  
           
         $self->{content_model} = PCDATA_CONTENT_MODEL;  
           
         if ($token->{type} eq 'end tag' and  
             $token->{tag_name} eq $tag_name) {  
           ## Ignore the token  
         } else {  
           !!!parse-error (type => 'in RCDATA:#'.$token->{type});  
         }  
         !!!next-token;  
         return;  
       } elsif ({  
                 iframe => 1,  
                 noembed => 1,  
                 noframes => 1,  
                 noscript => 0, ## TODO: 1 if scripting is enabled  
                }->{$token->{tag_name}}) {  
         $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);  
         return;  
       } 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';  
         !!!next-token;  
         return;  
       } elsif ({  
                 caption => 1, col => 1, colgroup => 1, frame => 1,  
                 frameset => 1, head => 1, option => 1, optgroup => 1,  
                 tbody => 1, td => 1, tfoot => 1, th => 1,  
                 thead => 1, tr => 1,  
                }->{$token->{tag_name}}) {  
         !!!parse-error (type => 'in body:'.$token->{tag_name});  
         ## Ignore the token  
         !!!next-token;  
         return;  
           
         ## ISSUE: An issue on HTML5 new elements in the spec.  
       } else {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
           
         !!!next-token;  
         return;  
       }  
     } elsif ($token->{type} eq 'end tag') {  
       if ($token->{tag_name} eq 'body') {  
         if (@{$self->{open_elements}} > 1 and  
             $self->{open_elements}->[1]->[1] eq 'body') {  
           for (@{$self->{open_elements}}) {  
             unless ({  
                        dd => 1, dt => 1, li => 1, p => 1, td => 1,  
                        th => 1, tr => 1, body => 1, html => 1,  
                      tbody => 1, tfoot => 1, thead => 1,  
                     }->{$_->[1]}) {  
               !!!parse-error (type => 'not closed:'.$_->[1]);  
             }  
           }  
   
           $self->{insertion_mode} = 'after body';  
           !!!next-token;  
           return;  
         } else {  
           !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
           ## Ignore the token  
           !!!next-token;  
           return;  
         }  
       } elsif ($token->{tag_name} eq 'html') {  
         if (@{$self->{open_elements}} > 1 and $self->{open_elements}->[1]->[1] eq 'body') {  
           ## ISSUE: There is an issue in the spec.  
           if ($self->{open_elements}->[-1]->[1] ne 'body') {  
             !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);  
           }  
           $self->{insertion_mode} = 'after body';  
           ## reprocess  
           return;  
         } else {  
           !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
           ## Ignore the token  
           !!!next-token;  
           return;  
         }  
       } elsif ({  
                 address => 1, blockquote => 1, center => 1, dir => 1,  
                 div => 1, dl => 1, fieldset => 1, listing => 1,  
                 menu => 1, ol => 1, pre => 1, ul => 1,  
                 p => 1,  
                 dd => 1, dt => 1, li => 1,  
                 button => 1, marquee => 1, object => 1,  
                }->{$token->{tag_name}}) {  
         ## has an element in scope  
         my $i;  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq $token->{tag_name}) {  
             ## generate implied end tags  
             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',  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               return;  
             }  
             $i = $_;  
             last INSCOPE unless $token->{tag_name} eq 'p';  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
           
         if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {  
           if (defined $i) {  
             !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
           } else {  
             !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
           }  
         }  
           
         if (defined $i) {  
           splice @{$self->{open_elements}}, $i;  
         } elsif ($token->{tag_name} eq 'p') {  
           ## As if <p>, then reprocess the current token  
           my $el;  
           !!!create-element ($el, 'p');  
           $insert->($el);  
         }  
         $clear_up_to_marker->()  
           if {  
             button => 1, marquee => 1, object => 1,  
           }->{$token->{tag_name}};  
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'form') {  
         ## has an element in scope  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq $token->{tag_name}) {  
             ## 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',  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               return;  
             }  
             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 ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {  
           pop @{$self->{open_elements}};  
         } else {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         }  
   
         undef $self->{form_element};  
         !!!next-token;  
         return;  
       } elsif ({  
                 h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
                }->{$token->{tag_name}}) {  
         ## 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]}) {  
             ## 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',  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               return;  
             }  
             $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 ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         }  
           
         splice @{$self->{open_elements}}, $i if defined $i;  
         !!!next-token;  
         return;  
       } elsif ({  
                 a => 1,  
                 b => 1, big => 1, em => 1, font => 1, i => 1,  
                 nobr => 1, s => 1, small => 1, strile => 1,  
                 strong => 1, tt => 1, u => 1,  
                }->{$token->{tag_name}}) {  
         $formatting_end_tag->($token->{tag_name});  
         return;  
       } elsif ($token->{tag_name} eq 'br') {  
         !!!parse-error (type => 'unmatched end tag:br');  
   
         ## As if <br>  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         my $el;  
         !!!create-element ($el, 'br');  
         $insert->($el);  
           
         ## Ignore the token.  
         !!!next-token;  
         return;  
       } elsif ({  
                 caption => 1, col => 1, colgroup => 1, frame => 1,  
                 frameset => 1, head => 1, option => 1, optgroup => 1,  
                 tbody => 1, td => 1, tfoot => 1, th => 1,  
                 thead => 1, tr => 1,  
                 area => 1, basefont => 1, bgsound => 1,  
                 embed => 1, hr => 1, iframe => 1, image => 1,  
                 img => 1, input => 1, isindex => 1, noembed => 1,  
                 noframes => 1, param => 1, select => 1, spacer => 1,  
                 table => 1, textarea => 1, wbr => 1,  
                 noscript => 0, ## TODO: if scripting is enabled  
                }->{$token->{tag_name}}) {  
         !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
         ## Ignore the token  
         !!!next-token;  
         return;  
           
         ## ISSUE: Issue on HTML5 new elements in spec  
           
       } else {  
         ## Step 1  
         my $node_i = -1;  
         my $node = $self->{open_elements}->[$node_i];  
   
         ## Step 2  
         S2: {  
           if ($node->[1] eq $token->{tag_name}) {  
             ## Step 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',  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               return;  
             }  
           
             ## Step 2  
             if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {  
               !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
             }  
               
             ## Step 3  
             splice @{$self->{open_elements}}, $node_i;  
   
             !!!next-token;  
             last S2;  
           } else {  
             ## Step 3  
             if (not $formatting_category->{$node->[1]} and  
                 #not $phrasing_category->{$node->[1]} and  
                 ($special_category->{$node->[1]} or  
                  $scoping_category->{$node->[1]})) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
               !!!next-token;  
               last S2;  
             }  
           }  
             
           ## Step 4  
           $node_i--;  
           $node = $self->{open_elements}->[$node_i];  
             
           ## Step 5;  
           redo S2;  
         } # S2  
         return;  
       }  
3495      }      }
3496    }; # $in_body    }; # $insert_to_foster
3497    
3498    B: {    B: {
3499      if ($token->{type} eq 'DOCTYPE') {      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;
3506      } elsif ($token->{type} eq 'end-of-file') {      } elsif ($token->{type} == START_TAG_TOKEN and
       if ($token->{insertion_mode} ne 'trailing end') {  
         ## 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', 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;  
     } elsif ($token->{type} eq 'start tag' and  
3507               $token->{tag_name} eq 'html') {               $token->{tag_name} eq 'html') {
3508        if ($self->{insertion_mode} eq 'trailing end') {        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} = $previous_insertion_mode;          $self->{insertion_mode} = AFTER_BODY_IM;
3512          } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
3513            !!!cp ('t80');
3514            !!!parse-error (type => 'after html:html', token => $token);
3515            $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} eq 'comment') {      } 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} eq 'trailing end') {        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} eq 'after body') {        } 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;
3547        redo B;        redo B;
3548      } elsif ($self->{insertion_mode} eq 'before head') {      } elsif ($self->{insertion_mode} & HEAD_IMS) {
3549            if ($token->{type} eq 'character') {        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                unless (length $token->{data}) {              !!!cp ('t88.2');
3553                  !!!next-token;              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
3554                  redo B;            } else {
3555                }              !!!cp ('t88.1');
3556              }              ## Ignore the token.
3557              ## As if <head>              !!!next-token;
             !!!create-element ($self->{head_element}, 'head');  
             $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});  
             push @{$self->{open_elements}}, [$self->{head_element}, 'head'];  
             $self->{insertion_mode} = 'in head';  
             ## reprocess  
3558              redo B;              redo B;
3559            } elsif ($token->{type} eq 'start tag') {            }
3560              my $attr = $token->{tag_name} eq 'head' ? $token->{attributes} : {};            unless (length $token->{data}) {
3561              !!!create-element ($self->{head_element}, 'head', $attr);              !!!cp ('t88');
3562              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              !!!next-token;
             push @{$self->{open_elements}}, [$self->{head_element}, 'head'];  
             $self->{insertion_mode} = 'in head';  
             if ($token->{tag_name} eq 'head') {  
               !!!next-token;  
             #} elsif ({  
             #          base => 1, link => 1, meta => 1,  
             #          script => 1, style => 1, title => 1,  
             #         }->{$token->{tag_name}}) {  
             #  ## reprocess  
             } else {  
               ## reprocess  
             }  
3563              redo B;              redo B;
3564            } elsif ($token->{type} eq 'end tag') {            }
3565              if ({          }
3566                   head => 1, body => 1, html => 1,  
3567                   p => 1, br => 1,          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3568                  }->{$token->{tag_name}}) {            !!!cp ('t89');
3569              ## As if <head>
3570              !!!create-element ($self->{head_element}, 'head',, $token);
3571              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3572              push @{$self->{open_elements}},
3573                  [$self->{head_element}, $el_category->{head}];
3574    
3575              ## Reprocess in the "in head" insertion mode...
3576              pop @{$self->{open_elements}};
3577    
3578              ## Reprocess in the "after head" insertion mode...
3579            } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3580              !!!cp ('t90');
3581              ## As if </noscript>
3582              pop @{$self->{open_elements}};
3583              !!!parse-error (type => 'in noscript:#character', token => $token);
3584              
3585              ## Reprocess in the "in head" insertion mode...
3586              ## As if </head>
3587              pop @{$self->{open_elements}};
3588    
3589              ## Reprocess in the "after head" insertion mode...
3590            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3591              !!!cp ('t91');
3592              pop @{$self->{open_elements}};
3593    
3594              ## Reprocess in the "after head" insertion mode...
3595            } else {
3596              !!!cp ('t92');
3597            }
3598    
3599            ## "after head" insertion mode
3600            ## As if <body>
3601            !!!insert-element ('body',, $token);
3602            $self->{insertion_mode} = IN_BODY_IM;
3603            ## 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;
3618              } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
3619                !!!cp ('t94');
3620                #
3621              } else {
3622                !!!cp ('t95');
3623                !!!parse-error (type => 'in head:head', token => $token); # or in head noscript
3624                ## Ignore the token
3625                !!!nack ('t95.1');
3626                !!!next-token;
3627                redo B;
3628              }
3629            } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3630                  !!!cp ('t96');
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->{insertion_mode} = 'in head';                    [$self->{head_element}, $el_category->{head}];
3636                ## reprocess  
3637                redo B;                $self->{insertion_mode} = IN_HEAD_IM;
3638                  ## Reprocess in the "in head" insertion mode...
3639              } else {              } else {
3640                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!cp ('t97');
               ## Ignore the token ## ISSUE: An issue in the spec.  
               !!!next-token;  
               redo B;  
3641              }              }
3642            } else {  
3643              die "$0: $token->{type}: Unknown type";              if ($token->{tag_name} eq 'base') {
3644            }                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3645          } elsif ($self->{insertion_mode} eq 'in head' or                  !!!cp ('t98');
3646                   $self->{insertion_mode} eq 'in head noscript' or                  ## As if </noscript>
3647                   $self->{insertion_mode} eq 'after head') {                  pop @{$self->{open_elements}};
3648            if ($token->{type} eq 'character') {                  !!!parse-error (type => 'in noscript:base', token => $token);
3649              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {                
3650                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                  $self->{insertion_mode} = IN_HEAD_IM;
3651                unless (length $token->{data}) {                  ## Reprocess in the "in head" insertion mode...
3652                  !!!next-token;                } else {
3653                  redo B;                  !!!cp ('t99');
3654                }                }
3655              }  
               
             #  
           } elsif ($token->{type} eq 'start tag') {  
             if ({base => ($self->{insertion_mode} eq 'in head' or  
                           $self->{insertion_mode} eq 'after head'),  
                  link => 1}->{$token->{tag_name}}) {  
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} eq 'after head') {                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} eq 'after head';                    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 'meta') {              } 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} eq 'after head') {                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}} # <head>
3685                      if $self->{insertion_mode} == AFTER_HEAD_IM;
3686                  !!!ack ('t103.1');
3687                  !!!next-token;
3688                  redo B;
3689                } elsif ($token->{tag_name} eq 'meta') {
3690                  ## NOTE: There is a "as if in head" code clone.
3691                  if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3692                    !!!cp ('t104');
3693                    !!!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}, $token);
3700                  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>
3750                pop @{$self->{open_elements}}                    if $self->{insertion_mode} == AFTER_HEAD_IM;
3751                    if $self->{insertion_mode} eq 'after head';                !!!ack ('t110.1');
3752                !!!next-token;                !!!next-token;
3753                redo B;                redo B;
3754              } elsif ($token->{tag_name} eq 'title' and              } elsif ($token->{tag_name} eq 'title') {
3755                       $self->{insertion_mode} eq 'in head') {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3756                ## NOTE: There is a "as if in head" code clone.                  !!!cp ('t111');
3757                if ($self->{insertion_mode} eq 'after head') {                  ## As if </noscript>
3758                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  pop @{$self->{open_elements}};
3759                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'in noscript:title', token => $token);
3760                  
3761                    $self->{insertion_mode} = IN_HEAD_IM;
3762                    ## Reprocess in the "in head" insertion mode...
3763                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
3764                    !!!cp ('t112');
3765                    !!!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.
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>
3777                pop @{$self->{open_elements}}                    if $self->{insertion_mode} == AFTER_HEAD_IM;
                   if $self->{insertion_mode} eq 'after head';  
3778                redo B;                redo B;
3779              } elsif ($token->{tag_name} eq 'style') {              } elsif ($token->{tag_name} eq 'style') {
3780                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
3781                ## insertion mode 'in head')                ## 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} eq 'after head') {                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                $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);                      [$self->{head_element}, $el_category->{head}];
3788                pop @{$self->{open_elements}}                } else {
3789                    if $self->{insertion_mode} eq 'after head';                  !!!cp ('t115');
3790                  }
3791                  $parse_rcdata->(CDATA_CONTENT_MODEL);
3792                  pop @{$self->{open_elements}} # <head>
3793                      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} eq 'in head') {                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';                  $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} eq 'in head noscript') {                } 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 'head' and              } elsif ($token->{tag_name} eq 'script') {
3816                       $self->{insertion_mode} ne 'after head') {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3817                !!!parse-error (type => 'in head:head'); # or in head noscript                  !!!cp ('t119');
3818                ## Ignore the token                  ## As if </noscript>
3819                !!!next-token;                  pop @{$self->{open_elements}};
3820                redo B;                  !!!parse-error (type => 'in noscript:script', token => $token);
3821              } elsif ($self->{insertion_mode} ne 'in head noscript' and                
3822                       $token->{tag_name} eq 'script') {                  $self->{insertion_mode} = IN_HEAD_IM;
3823                if ($self->{insertion_mode} eq 'after head') {                  ## Reprocess in the "in head" insertion mode...
3824                  !!!parse-error (type => 'after head:'.$token->{tag_name});                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
3825                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!cp ('t120');
3826                    !!!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} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
               redo B;  
             } elsif ($self->{insertion_mode} eq 'after head' and  
                      $token->{tag_name} eq 'body') {  
               !!!insert-element ('body', $token->{attributes});  
               $self->{insertion_mode} = 'in body';  
               !!!next-token;  
3837                redo B;                redo B;
3838              } elsif ($self->{insertion_mode} eq 'after head' and              } elsif ($token->{tag_name} eq 'body' or
3839                       $token->{tag_name} eq 'frameset') {                       $token->{tag_name} eq 'frameset') {
3840                !!!insert-element ('frameset', $token->{attributes});                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3841                $self->{insertion_mode} = 'in frameset';                  !!!cp ('t122');
3842                    ## As if </noscript>
3843                    pop @{$self->{open_elements}};
3844                    !!!parse-error (type => 'in noscript:'.$token->{tag_name}, token => $token);
3845                    
3846                    ## Reprocess in the "in head" insertion mode...
3847                    ## As if </head>
3848                    pop @{$self->{open_elements}};
3849                    
3850                    ## Reprocess in the "after head" insertion mode...
3851                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3852                    !!!cp ('t124');
3853                    pop @{$self->{open_elements}};
3854                    
3855                    ## Reprocess in the "after head" insertion mode...
3856                  } else {
3857                    !!!cp ('t125');
3858                  }
3859    
3860                  ## "after head" insertion mode
3861                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
3862                  if ($token->{tag_name} eq 'body') {
3863                    !!!cp ('t126');
3864                    $self->{insertion_mode} = IN_BODY_IM;
3865                  } elsif ($token->{tag_name} eq 'frameset') {
3866                    !!!cp ('t127');
3867                    $self->{insertion_mode} = IN_FRAMESET_IM;
3868                  } else {
3869                    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            } elsif ($token->{type} eq 'end tag') {  
3879              if ($self->{insertion_mode} eq 'in head' and              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3880                  $token->{tag_name} eq 'head') {                !!!cp ('t129');
3881                  ## As if </noscript>
3882                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3883                $self->{insertion_mode} = 'after head';                !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);
3884                !!!next-token;                
3885                redo B;                ## Reprocess in the "in head" insertion mode...
3886              } elsif ($self->{insertion_mode} eq 'in head noscript' and                ## As if </head>
                 $token->{tag_name} eq 'noscript') {  
3887                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3888                $self->{insertion_mode} = 'in head';  
3889                !!!next-token;                ## Reprocess in the "after head" insertion mode...
3890                redo B;              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3891              } elsif ($self->{insertion_mode} eq 'in head' and                !!!cp ('t130');
3892                       {                ## As if </head>
3893                  pop @{$self->{open_elements}};
3894    
3895                  ## Reprocess in the "after head" insertion mode...
3896                } else {
3897                  !!!cp ('t131');
3898                }
3899    
3900                ## "after head" insertion mode
3901                ## As if <body>
3902                !!!insert-element ('body',, $token);
3903                $self->{insertion_mode} = IN_BODY_IM;
3904                ## reprocess
3905                !!!ack-later;
3906                redo B;
3907              } elsif ($token->{type} == END_TAG_TOKEN) {
3908                if ($token->{tag_name} eq 'head') {
3909                  if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3910                    !!!cp ('t132');
3911                    ## As if <head>
3912                    !!!create-element ($self->{head_element}, 'head',, $token);
3913                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3914                    push @{$self->{open_elements}},
3915                        [$self->{head_element}, $el_category->{head}];
3916    
3917                    ## Reprocess in the "in head" insertion mode...
3918                    pop @{$self->{open_elements}};
3919                    $self->{insertion_mode} = AFTER_HEAD_IM;
3920                    !!!next-token;
3921                    redo B;
3922                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3923                    !!!cp ('t133');
3924                    ## As if </noscript>
3925                    pop @{$self->{open_elements}};
3926                    !!!parse-error (type => 'in noscript:/head', token => $token);
3927                    
3928                    ## Reprocess in the "in head" insertion mode...
3929                    pop @{$self->{open_elements}};
3930                    $self->{insertion_mode} = AFTER_HEAD_IM;
3931                    !!!next-token;
3932                    redo B;
3933                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3934                    !!!cp ('t134');
3935                    pop @{$self->{open_elements}};
3936                    $self->{insertion_mode} = AFTER_HEAD_IM;
3937                    !!!next-token;
3938                    redo B;
3939                  } else {
3940                    !!!cp ('t135');
3941                    #
3942                  }
3943                } elsif ($token->{tag_name} eq 'noscript') {
3944                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3945                    !!!cp ('t136');
3946                    pop @{$self->{open_elements}};
3947                    $self->{insertion_mode} = IN_HEAD_IM;
3948                    !!!next-token;
3949                    redo B;
3950                  } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3951                    !!!cp ('t137');
3952                    !!!parse-error (type => 'unmatched end tag:noscript', token => $token);
3953                    ## Ignore the token ## ISSUE: An issue in the spec.
3954                    !!!next-token;
3955                    redo B;
3956                  } else {
3957                    !!!cp ('t138');
3958                    #
3959                  }
3960                } elsif ({
3961                        body => 1, html => 1,                        body => 1, html => 1,
                       p => 1, br => 1,  
3962                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3963                  if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3964                    !!!cp ('t139');
3965                    ## As if <head>
3966                    !!!create-element ($self->{head_element}, 'head',, $token);
3967                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3968                    push @{$self->{open_elements}},
3969                        [$self->{head_element}, $el_category->{head}];
3970    
3971                    $self->{insertion_mode} = IN_HEAD_IM;
3972                    ## Reprocess in the "in head" insertion mode...
3973                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3974                    !!!cp ('t140');
3975                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
3976                    ## Ignore the token
3977                    !!!next-token;
3978                    redo B;
3979                  } else {
3980                    !!!cp ('t141');
3981                  }
3982                  
3983                #                #
3984              } elsif ($self->{insertion_mode} eq 'in head noscript' and              } elsif ({
                      {  
3985                        p => 1, br => 1,                        p => 1, br => 1,
3986                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3987                  if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3988                    !!!cp ('t142');
3989                    ## As if <head>
3990                    !!!create-element ($self->{head_element}, 'head',, $token);
3991                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3992                    push @{$self->{open_elements}},
3993                        [$self->{head_element}, $el_category->{head}];
3994    
3995                    $self->{insertion_mode} = IN_HEAD_IM;
3996                    ## Reprocess in the "in head" insertion mode...
3997                  } else {
3998                    !!!cp ('t143');
3999                  }
4000    
4001                #                #
4002              } elsif ($self->{insertion_mode} ne 'after head') {              } else {
4003                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4004                ## Ignore the token                  !!!cp ('t144');
4005                    #
4006                  } else {
4007                    !!!cp ('t145');
4008                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4009                    ## Ignore the token
4010                    !!!next-token;
4011                    redo B;
4012                  }
4013                }
4014    
4015                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4016                  !!!cp ('t146');
4017                  ## As if </noscript>
4018                  pop @{$self->{open_elements}};
4019                  !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);
4020                  
4021                  ## Reprocess in the "in head" insertion mode...
4022                  ## As if </head>
4023                  pop @{$self->{open_elements}};
4024    
4025                  ## Reprocess in the "after head" insertion mode...
4026                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4027                  !!!cp ('t147');
4028                  ## As if </head>
4029                  pop @{$self->{open_elements}};
4030    
4031                  ## Reprocess in the "after head" insertion mode...
4032                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4033    ## 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.
4037                !!!next-token;                !!!next-token;
4038                redo B;                redo B;
4039              } else {              } else {
4040                #                !!!cp ('t149');
4041              }              }
           } else {  
             #  
           }  
4042    
4043            ## As if </head> or </noscript> or <body>              ## "after head" insertion mode
4044            if ($self->{insertion_mode} eq 'in head') {              ## As if <body>
4045              pop @{$self->{open_elements}};              !!!insert-element ('body',, $token);
4046              $self->{insertion_mode} = 'after head';              $self->{insertion_mode} = IN_BODY_IM;
4047            } elsif ($self->{insertion_mode} eq 'in head noscript') {              ## reprocess
4048              pop @{$self->{open_elements}};              redo B;
4049              !!!parse-error (type => 'in noscript:'.(defined $token->{tag_name} ? ($token->{type} eq 'end tag' ? '/' : '') . $token->{tag_name} : '#' . $token->{type}));        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4050              $self->{insertion_mode} = 'in head';          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4051            } else { # 'after head'            !!!cp ('t149.1');
4052              !!!insert-element ('body');  
4053              $self->{insertion_mode} = 'in body';            ## NOTE: As if <head>
4054            }            !!!create-element ($self->{head_element}, 'head',, $token);
4055            ## reprocess            $self->{open_elements}->[-1]->[0]->append_child
4056            redo B;                ($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} eq 'in body' or      } elsif ($self->{insertion_mode} & BODY_IMS) {
4109                   $self->{insertion_mode} eq 'in caption') {            if ($token->{type} == CHARACTER_TOKEN) {
4110            if ($token->{type} eq 'character') {              !!!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 3656  sub _tree_construction_main ($) { Line 4115  sub _tree_construction_main ($) {
4115    
4116              !!!next-token;              !!!next-token;
4117              redo B;              redo B;
4118            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} == START_TAG_TOKEN) {
4119              if ({              if ({
4120                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
4121                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,
4122                  }->{$token->{tag_name}} and                  }->{$token->{tag_name}}) {
4123                  $self->{insertion_mode} eq 'in caption') {                if ($self->{insertion_mode} == IN_CELL_IM) {
4124                !!!parse-error (type => 'not closed:caption');                  ## have an element in table scope
4125                    for (reverse 0..$#{$self->{open_elements}}) {
4126                ## As if </caption>                    my $node = $self->{open_elements}->[$_];
4127                ## have a table element in table scope                    if ($node->[1] & TABLE_CELL_EL) {
4128                my $i;                      !!!cp ('t151');
4129                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
4130                  my $node = $self->{open_elements}->[$_];                      ## Close the cell
4131                  if ($node->[1] eq 'caption') {                      !!!back-token; # <x>
4132                    $i = $_;                      $token = {type => END_TAG_TOKEN,
4133                    last INSCOPE;                                tag_name => $node->[0]->manakai_local_name,
4134                  } elsif ({                                line => $token->{line},
4135                            table => 1, html => 1,                                column => $token->{column}};
4136                           }->{$node->[1]}) {                      redo B;
4137                    last INSCOPE;                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
4138                        !!!cp ('t152');
4139                        ## ISSUE: This case can never be reached, maybe.
4140                        last;
4141                      }
4142                  }                  }
4143                } # INSCOPE  
4144                unless (defined $i) {                  !!!cp ('t153');
4145                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!parse-error (type => 'start tag not allowed',
4146                        value => $token->{tag_name}, token => $token);
4147                  ## Ignore the token                  ## Ignore the token
4148                    !!!nack ('t153.1');
4149                  !!!next-token;                  !!!next-token;
4150                  redo B;                  redo B;
4151                }                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4152                                  !!!parse-error (type => 'not closed:caption', token => $token);
4153                ## generate implied end tags                  
4154                if ({                  ## NOTE: As if </caption>.
4155                     dd => 1, dt => 1, li => 1, p => 1,                  ## have a table element in table scope
4156                     td => 1, th => 1, tr => 1,                  my $i;
4157                     tbody => 1, tfoot=> 1, thead => 1,                  INSCOPE: {
4158                    }->{$self->{open_elements}->[-1]->[1]}) {                    for (reverse 0..$#{$self->{open_elements}}) {
4159                  !!!back-token; # <?>                      my $node = $self->{open_elements}->[$_];
4160                  $token = {type => 'end tag', tag_name => 'caption'};                      if ($node->[1] & CAPTION_EL) {
4161                  !!!back-token;                        !!!cp ('t155');
4162                  $token = {type => 'end tag',                        $i = $_;
4163                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                        last INSCOPE;
4164                  redo B;                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4165                }                        !!!cp ('t156');
4166                          last;
4167                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                      }
4168                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    }
               }  
   
               splice @{$self->{open_elements}}, $i;  
   
               $clear_up_to_marker->();  
4169    
4170                $self->{insertion_mode} = 'in table';                    !!!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
4178                    
4179                    ## generate implied end tags
4180                    while ($self->{open_elements}->[-1]->[1]
4181                               & END_TAG_OPTIONAL_EL) {
4182                      !!!cp ('t158');
4183                      pop @{$self->{open_elements}};
4184                    }
4185    
4186                ## reprocess                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4187                redo B;                    !!!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;
4197                    
4198                    $clear_up_to_marker->();
4199                    
4200                    $self->{insertion_mode} = IN_TABLE_IM;
4201                    
4202                    ## reprocess
4203                    !!!ack-later;
4204                    redo B;
4205                  } else {
4206                    !!!cp ('t161');
4207                    #
4208                  }
4209              } else {              } else {
4210                  !!!cp ('t162');
4211                #                #
4212              }              }
4213            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
4214              if ($token->{tag_name} eq 'caption' and              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {
4215                  $self->{insertion_mode} eq 'in caption') {                if ($self->{insertion_mode} == IN_CELL_IM) {
4216                ## have a table element in table scope                  ## have an element in table scope
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                    $i = $_;                      !!!cp ('t163');
4222                    last INSCOPE;                      $i = $_;
4223                  } elsif ({                      last INSCOPE;
4224                            table => 1, html => 1,                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
4225                           }->{$node->[1]}) {                      !!!cp ('t164');
4226                    last INSCOPE;                      last INSCOPE;
4227                      }
4228                    } # INSCOPE
4229                      unless (defined $i) {
4230                        !!!cp ('t165');
4231                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4232                        ## Ignore the token
4233                        !!!next-token;
4234                        redo B;
4235                      }
4236                    
4237                    ## generate implied end tags
4238                    while ($self->{open_elements}->[-1]->[1]
4239                               & END_TAG_OPTIONAL_EL) {
4240                      !!!cp ('t166');
4241                      pop @{$self->{open_elements}};
4242                  }                  }
4243                } # INSCOPE  
4244                unless (defined $i) {                  if ($self->{open_elements}->[-1]->[0]->manakai_local_name
4245                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                          ne $token->{tag_name}) {
4246                  ## Ignore the token                    !!!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;
4256                    
4257                    $clear_up_to_marker->();
4258                    
4259                    $self->{insertion_mode} = IN_ROW_IM;
4260                    
4261                  !!!next-token;                  !!!next-token;
4262                  redo B;                  redo B;
4263                }                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4264                                  !!!cp ('t169');
4265                ## generate implied end tags                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4266                if ({                  ## Ignore the token
4267                     dd => 1, dt => 1, li => 1, p => 1,                  !!!next-token;
                    td => 1, th => 1, tr => 1,  
                    tbody => 1, tfoot=> 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
4268                  redo B;                  redo B;
4269                  } else {
4270                    !!!cp ('t170');
4271                    #
4272                }                }
4273                } elsif ($token->{tag_name} eq 'caption') {
4274                  if ($self->{insertion_mode} == IN_CAPTION_IM) {
4275                    ## have a table element in table scope
4276                    my $i;
4277                    INSCOPE: {
4278                      for (reverse 0..$#{$self->{open_elements}}) {
4279                        my $node = $self->{open_elements}->[$_];
4280                        if ($node->[1] & CAPTION_EL) {
4281                          !!!cp ('t171');
4282                          $i = $_;
4283                          last INSCOPE;
4284                        } elsif ($node->[1] & TABLE_SCOPING_EL) {
4285                          !!!cp ('t172');
4286                          last;
4287                        }
4288                      }
4289    
4290                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                    !!!cp ('t173');
4291                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'unmatched end tag',
4292                }                                    value => $token->{tag_name}, token => $token);
4293                      ## Ignore the token
4294                splice @{$self->{open_elements}}, $i;                    !!!next-token;
4295                      redo B;
4296                $clear_up_to_marker->();                  } # INSCOPE
4297                    
4298                $self->{insertion_mode} = 'in table';                  ## generate implied end tags
4299                    while ($self->{open_elements}->[-1]->[1]
4300                !!!next-token;                             & END_TAG_OPTIONAL_EL) {
4301                redo B;                    !!!cp ('t174');
4302              } elsif ($token->{tag_name} eq 'table' and                    pop @{$self->{open_elements}};
                      $self->{insertion_mode} eq 'in caption') {  
               !!!parse-error (type => 'not closed:caption');  
   
               ## As if </caption>  
               ## have a table element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'caption') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
4303                  }                  }
4304                } # INSCOPE                  
4305                unless (defined $i) {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4306                  !!!parse-error (type => 'unmatched end tag:caption');                    !!!cp ('t175');
4307                  ## Ignore the token                    !!!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;
4316                    
4317                    $clear_up_to_marker->();
4318                    
4319                    $self->{insertion_mode} = IN_TABLE_IM;
4320                    
4321                  !!!next-token;                  !!!next-token;
4322                  redo B;                  redo B;
4323                }                } elsif ($self->{insertion_mode} == IN_CELL_IM) {
4324                                  !!!cp ('t177');
4325                ## generate implied end tags                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4326                if ({                  ## Ignore the token
                    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; # </table>  
                 $token = {type => 'end tag', tag_name => 'caption'};  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
   
               if ($self->{open_elements}->[-1]->[1] ne 'caption') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               }  
   
               splice @{$self->{open_elements}}, $i;  
   
               $clear_up_to_marker->();  
   
               $self->{insertion_mode} = 'in table';  
   
               ## reprocess  
               redo B;  
             } elsif ({  
                       body => 1, col => 1, colgroup => 1,  
                       html => 1, tbody => 1, td => 1, tfoot => 1,  
                       th => 1, thead => 1, tr => 1,  
                      }->{$token->{tag_name}} and  
                      $self->{insertion_mode} eq 'in caption') {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
   
           $in_body->($insert_to_current);  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'in table') {  
           if ($token->{type} eq 'character') {  
             ## NOTE: There are "character in table" code clones.  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
                 
               unless (length $token->{data}) {  
4327                  !!!next-token;                  !!!next-token;
4328                  redo B;                  redo B;
               }  
             }  
   
             !!!parse-error (type => 'in table:#character');  
   
             ## As if in body, but insert into foster parent element  
             ## ISSUE: Spec says that "whenever a node would be inserted  
             ## into the current node" while characters might not be  
             ## result in a new Text node.  
             $reconstruct_active_formatting_elements->($insert_to_foster);  
               
             if ({  
                  table => 1, tbody => 1, tfoot => 1,  
                  thead => 1, tr => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               # MUST  
               my $foster_parent_element;  
               my $next_sibling;  
               my $prev_sibling;  
               OE: for (reverse 0..$#{$self->{open_elements}}) {  
                 if ($self->{open_elements}->[$_]->[1] eq 'table') {  
                   my $parent = $self->{open_elements}->[$_]->[0]->parent_node;  
                   if (defined $parent and $parent->node_type == 1) {  
                     $foster_parent_element = $parent;  
                     $next_sibling = $self->{open_elements}->[$_]->[0];  
                     $prev_sibling = $next_sibling->previous_sibling;  
                   } else {  
                     $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];  
                     $prev_sibling = $foster_parent_element->last_child;  
                   }  
                   last OE;  
                 }  
               } # OE  
               $foster_parent_element = $self->{open_elements}->[0]->[0] and  
               $prev_sibling = $foster_parent_element->last_child  
                 unless defined $foster_parent_element;  
               if (defined $prev_sibling and  
                   $prev_sibling->node_type == 3) {  
                 $prev_sibling->manakai_append_text ($token->{data});  
4329                } else {                } else {
4330                  $foster_parent_element->insert_before                  !!!cp ('t178');
4331                    ($self->{document}->create_text_node ($token->{data}),                  #
                    $next_sibling);  
               }  
             } else {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
             }  
               
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ({  
                  caption => 1,  
                  colgroup => 1,  
                  tbody => 1, tfoot => 1, thead => 1,  
                 }->{$token->{tag_name}}) {  
               ## Clear back to table context  
               while ($self->{open_elements}->[-1]->[1] ne 'table' and  
                      $self->{open_elements}->[-1]->[1] ne 'html') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
4332                }                }
   
               push @$active_formatting_elements, ['#marker', '']  
                 if $token->{tag_name} eq 'caption';  
   
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               $self->{insertion_mode} = {  
                                  caption => 'in caption',  
                                  colgroup => 'in column group',  
                                  tbody => 'in table body',  
                                  tfoot => 'in table body',  
                                  thead => 'in table body',  
                                 }->{$token->{tag_name}};  
               !!!next-token;  
               redo B;  
4333              } elsif ({              } elsif ({
4334                        col => 1,                        table => 1, tbody => 1, tfoot => 1,
4335                        td => 1, th => 1, tr => 1,                        thead => 1, tr => 1,
4336                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}} and
4337                ## Clear back to table context                       $self->{insertion_mode} == IN_CELL_IM) {
4338                while ($self->{open_elements}->[-1]->[1] ne 'table' and                ## have an element in table scope
                      $self->{open_elements}->[-1]->[1] ne 'html') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
   
               !!!insert-element ($token->{tag_name} eq 'col' ? 'colgroup' : 'tbody');  
               $self->{insertion_mode} = $token->{tag_name} eq 'col'  
                 ? 'in column group' : 'in table body';  
               ## reprocess  
               redo B;  
             } elsif ($token->{tag_name} eq 'table') {  
               ## NOTE: There are code clones for this "table in table"  
               !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
   
               ## As if </table>  
               ## have a table element in table scope  
4339                my $i;                my $i;
4340                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                my $tn;
4341                  my $node = $self->{open_elements}->[$_];                INSCOPE: {
4342                  if ($node->[1] eq 'table') {                  for (reverse 0..$#{$self->{open_elements}}) {
4343                    $i = $_;                    my $node = $self->{open_elements}->[$_];
4344                    last INSCOPE;                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
4345                  } elsif ({                      !!!cp ('t179');
4346                            table => 1, html => 1,                      $i = $_;
4347                           }->{$node->[1]}) {  
4348                    last INSCOPE;                      ## Close the cell
4349                        !!!back-token; # </x>
4350                        $token = {type => END_TAG_TOKEN, tag_name => $tn,
4351                                  line => $token->{line},
4352                                  column => $token->{column}};
4353                        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:table');                  !!!parse-error (type => 'unmatched end tag',
4368                  ## Ignore tokens </table><table>                      value => $token->{tag_name}, token => $token);
4369                    ## Ignore the token
4370                  !!!next-token;                  !!!next-token;
4371                  redo B;                  redo B;
4372                }                } # INSCOPE
4373                              } elsif ($token->{tag_name} eq 'table' and
4374                ## generate implied end tags                       $self->{insertion_mode} == IN_CAPTION_IM) {
4375                if ({                !!!parse-error (type => 'not closed:caption', token => $token);
                    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; # <table>  
                 $token = {type => 'end tag', tag_name => 'table'};  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           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]);  
               }  
   
               splice @{$self->{open_elements}}, $i;  
   
               $self->_reset_insertion_mode;  
4376    
4377                ## reprocess                ## As if </caption>
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'table') {  
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 $token->{tag_name}) {                  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:'.$token->{tag_name});                  !!!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;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
4403                }                }
4404    
4405                if ($self->{open_elements}->[-1]->[1] ne 'table') {                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;
4416    
4417                $self->_reset_insertion_mode;                $clear_up_to_marker->();
4418    
4419                !!!next-token;                $self->{insertion_mode} = IN_TABLE_IM;
4420    
4421                  ## reprocess
4422                redo B;                redo B;
4423              } elsif ({              } elsif ({
4424                        body => 1, caption => 1, col => 1, colgroup => 1,                        body => 1, col => 1, colgroup => 1, html => 1,
                       html => 1, tbody => 1, td => 1, tfoot => 1, th => 1,  
                       thead => 1, tr => 1,  
4425                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4426                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
4427                ## Ignore the token                  !!!cp ('t190');
4428                !!!next-token;                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
   
           !!!parse-error (type => 'in table:'.$token->{tag_name});  
           $in_body->($insert_to_foster);  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'in column group') {  
           if ($token->{type} eq 'character') {  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
               
             #  
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'col') {  
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               pop @{$self->{open_elements}};  
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'colgroup') {  
               if ($self->{open_elements}->[-1]->[1] eq 'html') {  
                 !!!parse-error (type => 'unmatched end tag:colgroup');  
4429                  ## Ignore the token                  ## Ignore the token
4430                  !!!next-token;                  !!!next-token;
4431                  redo B;                  redo B;
4432                } else {                } else {
4433                  pop @{$self->{open_elements}}; # colgroup                  !!!cp ('t191');
4434                  $self->{insertion_mode} = 'in table';                  #
                 !!!next-token;  
                 redo B;              
4435                }                }
4436              } elsif ($token->{tag_name} eq 'col') {              } elsif ({
4437                !!!parse-error (type => 'unmatched end tag:col');                        tbody => 1, tfoot => 1,
4438                          thead => 1, tr => 1,
4439                         }->{$token->{tag_name}} and
4440                         $self->{insertion_mode} == IN_CAPTION_IM) {
4441                  !!!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            } else {        } 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            ## As if </colgroup>          ## Stop parsing.
4460            if ($self->{open_elements}->[-1]->[1] eq 'html') {          last B;
4461              !!!parse-error (type => 'unmatched end tag:colgroup');        } else {
4462              ## Ignore the token          die "$0: $token->{type}: Unknown token type";
4463          }
4464    
4465          $insert = $insert_to_current;
4466          #
4467        } elsif ($self->{insertion_mode} & TABLE_IMS) {
4468          if ($token->{type} == CHARACTER_TOKEN) {
4469            if (not $open_tables->[-1]->[1] and # tainted
4470                $token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4471              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4472                  
4473              unless (length $token->{data}) {
4474                !!!cp ('t194');
4475              !!!next-token;              !!!next-token;
4476              redo B;              redo B;
4477            } else {            } else {
4478              pop @{$self->{open_elements}}; # colgroup              !!!cp ('t195');
             $self->{insertion_mode} = 'in table';  
             ## reprocess  
             redo B;  
4479            }            }
4480          } elsif ($self->{insertion_mode} eq 'in table body') {          }
           if ($token->{type} eq 'character') {  
             ## NOTE: This is a "character in table" code clone.  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
                 
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
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
4486              ## into the current node" while characters might not be              ## into the current node" while characters might not be
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 4151  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} eq 'start tag') {        } elsif ($token->{type} == START_TAG_TOKEN) {
4533              if ({              if ({
4534                   tr => 1,                   tr => ($self->{insertion_mode} != IN_ROW_IM),
4535                   th => 1, td => 1,                   th => 1, td => 1,
4536                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
4537                unless ($token->{tag_name} eq 'tr') {                if ($self->{insertion_mode} == IN_TABLE_IM) {
4538                  !!!parse-error (type => 'missing start tag:tr');                  ## Clear back to table context
4539                    while (not ($self->{open_elements}->[-1]->[1]
4540                                    & TABLE_SCOPING_EL)) {
4541                      !!!cp ('t201');
4542                      pop @{$self->{open_elements}};
4543                    }
4544                    
4545                    !!!insert-element ('tbody',, $token);
4546                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
4547                    ## reprocess in the "in table body" insertion mode...
4548                }                }
4549    
4550                ## Clear back to table body context                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
4551                while (not {                  unless ($token->{tag_name} eq 'tr') {
4552                  tbody => 1, tfoot => 1, thead => 1, html => 1,                    !!!cp ('t202');
4553                }->{$self->{open_elements}->[-1]->[1]}) {                    !!!parse-error (type => 'missing start tag:tr', token => $token);
4554                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  }
4555                    
4556                    ## Clear back to table body context
4557                    while (not ($self->{open_elements}->[-1]->[1]
4558                                    & TABLE_ROWS_SCOPING_EL)) {
4559                      !!!cp ('t203');
4560                      ## ISSUE: Can this case be reached?
4561                      pop @{$self->{open_elements}};
4562                    }
4563                    
4564                    $self->{insertion_mode} = IN_ROW_IM;
4565                    if ($token->{tag_name} eq 'tr') {
4566                      !!!cp ('t204');
4567                      !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4568                      !!!nack ('t204');
4569                      !!!next-token;
4570                      redo B;
4571                    } else {
4572                      !!!cp ('t205');
4573                      !!!insert-element ('tr',, $token);
4574                      ## reprocess in the "in row" insertion mode
4575                    }
4576                  } else {
4577                    !!!cp ('t206');
4578                  }
4579    
4580                  ## Clear back to table row context
4581                  while (not ($self->{open_elements}->[-1]->[1]
4582                                  & TABLE_ROW_SCOPING_EL)) {
4583                    !!!cp ('t207');
4584                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4585                }                }
4586                                
4587                $self->{insertion_mode} = 'in row';                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4588                if ($token->{tag_name} eq 'tr') {                $self->{insertion_mode} = IN_CELL_IM;
4589                  !!!insert-element ($token->{tag_name}, $token->{attributes});  
4590                  !!!next-token;                push @$active_formatting_elements, ['#marker', ''];
4591                } else {                
4592                  !!!insert-element ('tr');                !!!nack ('t207.1');
4593                  ## reprocess                !!!next-token;
               }  
4594                redo B;                redo B;
4595              } elsif ({              } elsif ({
4596                        caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
4597                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
4598                          tr => 1, # $self->{insertion_mode} == IN_ROW_IM
4599                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4600                ## have an element in table scope                if ($self->{insertion_mode} == IN_ROW_IM) {
4601                my $i;                  ## As if </tr>
4602                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  ## have an element in table scope
4603                  my $node = $self->{open_elements}->[$_];                  my $i;
4604                  if ({                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4605                       tbody => 1, thead => 1, tfoot => 1,                    my $node = $self->{open_elements}->[$_];
4606                      }->{$node->[1]}) {                    if ($node->[1] & TABLE_ROW_EL) {
4607                    $i = $_;                      !!!cp ('t208');
4608                    last INSCOPE;                      $i = $_;
4609                  } elsif ({                      last INSCOPE;
4610                            table => 1, html => 1,                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
4611                           }->{$node->[1]}) {                      !!!cp ('t209');
4612                    last INSCOPE;                      last INSCOPE;
4613                      }
4614                    } # INSCOPE
4615                    unless (defined $i) {
4616                      !!!cp ('t210');
4617    ## TODO: This type is wrong.
4618                      !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name}, token => $token);
4619                      ## Ignore the token
4620                      !!!nack ('t210.1');
4621                      !!!next-token;
4622                      redo B;
4623                    }
4624                    
4625                    ## Clear back to table row context
4626                    while (not ($self->{open_elements}->[-1]->[1]
4627                                    & TABLE_ROW_SCOPING_EL)) {
4628                      !!!cp ('t211');
4629                      ## ISSUE: Can this case be reached?
4630                      pop @{$self->{open_elements}};
4631                    }
4632                    
4633                    pop @{$self->{open_elements}}; # tr
4634                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
4635                    if ($token->{tag_name} eq 'tr') {
4636                      !!!cp ('t212');
4637                      ## reprocess
4638                      !!!ack-later;
4639                      redo B;
4640                    } else {
4641                      !!!cp ('t213');
4642                      ## reprocess in the "in table body" insertion mode...
4643                  }                  }
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
4644                }                }
4645    
4646                ## Clear back to table body context                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
4647                while (not {                  ## have an element in table scope
4648                  tbody => 1, tfoot => 1, thead => 1, html => 1,                  my $i;
4649                }->{$self->{open_elements}->[-1]->[1]}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4650                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    my $node = $self->{open_elements}->[$_];
4651                      if ($node->[1] & TABLE_ROW_GROUP_EL) {
4652                        !!!cp ('t214');
4653                        $i = $_;
4654                        last INSCOPE;
4655                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4656                        !!!cp ('t215');
4657                        last INSCOPE;
4658                      }
4659                    } # INSCOPE
4660                    unless (defined $i) {
4661                      !!!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
4665                      !!!nack ('t216.1');
4666                      !!!next-token;
4667                      redo B;
4668                    }
4669    
4670                    ## Clear back to table body context
4671                    while (not ($self->{open_elements}->[-1]->[1]
4672                                    & TABLE_ROWS_SCOPING_EL)) {
4673                      !!!cp ('t217');
4674                      ## ISSUE: Can this state be reached?
4675                      pop @{$self->{open_elements}};
4676                    }
4677                    
4678                    ## As if <{current node}>
4679                    ## have an element in table scope
4680                    ## true by definition
4681                    
4682                    ## Clear back to table body context
4683                    ## nop by definition
4684                    
4685                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4686                    $self->{insertion_mode} = IN_TABLE_IM;
4687                    ## reprocess in "in table" insertion mode...
4688                  } else {
4689                    !!!cp ('t218');
4690                }                }
4691    
4692                ## As if <{current node}>                if ($token->{tag_name} eq 'col') {
4693                ## have an element in table scope                  ## Clear back to table context
4694                ## true by definition                  while (not ($self->{open_elements}->[-1]->[1]
4695                                    & TABLE_SCOPING_EL)) {
4696                ## Clear back to table body context                    !!!cp ('t219');
4697                ## nop by definition                    ## ISSUE: Can this state be reached?
4698                      pop @{$self->{open_elements}};
4699                pop @{$self->{open_elements}};                  }
4700                $self->{insertion_mode} = 'in table';                  
4701                ## reprocess                  !!!insert-element ('colgroup',, $token);
4702                redo B;                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
4703                    ## reprocess
4704                    !!!ack-later;
4705                    redo B;
4706                  } elsif ({
4707                            caption => 1,
4708                            colgroup => 1,
4709                            tbody => 1, tfoot => 1, thead => 1,
4710                           }->{$token->{tag_name}}) {
4711                    ## Clear back to table context
4712                    while (not ($self->{open_elements}->[-1]->[1]
4713                                    & TABLE_SCOPING_EL)) {
4714                      !!!cp ('t220');
4715                      ## ISSUE: Can this state be reached?
4716                      pop @{$self->{open_elements}};
4717                    }
4718                    
4719                    push @$active_formatting_elements, ['#marker', '']
4720                        if $token->{tag_name} eq 'caption';
4721                    
4722                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4723                    $self->{insertion_mode} = {
4724                                               caption => IN_CAPTION_IM,
4725                                               colgroup => IN_COLUMN_GROUP_IM,
4726                                               tbody => IN_TABLE_BODY_IM,
4727                                               tfoot => IN_TABLE_BODY_IM,
4728                                               thead => IN_TABLE_BODY_IM,
4729                                              }->{$token->{tag_name}};
4730                    !!!next-token;
4731                    !!!nack ('t220.1');
4732                    redo B;
4733                  } else {
4734                    die "$0: in table: <>: $token->{tag_name}";
4735                  }
4736              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
4737                ## NOTE: This is a code clone of "table in table"                !!!parse-error (type => 'not closed',
4738                !!!parse-error (type => 'not closed:table');                                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', tag_name => 'table'};  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           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                #          } elsif ($token->{tag_name} eq 'style') {
4793              }            if (not $open_tables->[-1]->[1]) { # tainted
4794            } elsif ($token->{type} eq 'end tag') {              !!!cp ('t227.8');
4795              if ({              ## NOTE: This is a "as if in head" code clone.
4796                   tbody => 1, tfoot => 1, thead => 1,              $parse_rcdata->(CDATA_CONTENT_MODEL);
4797                  }->{$token->{tag_name}}) {              redo B;
4798                ## have an element in table scope            } else {
4799                my $i;              !!!cp ('t227.7');
4800                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {              #
4801                  my $node = $self->{open_elements}->[$_];            }
4802                  if ($node->[1] eq $token->{tag_name}) {          } elsif ($token->{tag_name} eq 'script') {
4803                    $i = $_;            if (not $open_tables->[-1]->[1]) { # tainted
4804                    last INSCOPE;              !!!cp ('t227.6');
4805                  } elsif ({              ## NOTE: This is a "as if in head" code clone.
4806                            table => 1, html => 1,              $script_start_tag->();
4807                           }->{$node->[1]}) {              redo B;
4808                    last INSCOPE;            } else {
4809                  }              !!!cp ('t227.5');
4810                } # INSCOPE              #
4811                unless (defined $i) {            }
4812                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          } elsif ($token->{tag_name} eq 'input') {
4813                  ## Ignore the token            if (not $open_tables->[-1]->[1]) { # tainted
4814                  !!!next-token;              if ($token->{attributes}->{type}) { ## TODO: case
4815                  redo B;                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-element ($token->{tag_name}, $token->{attributes}, $token);
4821    
4822                    ## TODO: form element pointer
4823    
               ## Clear back to table body context  
               while (not {  
                 tbody => 1, tfoot => 1, thead => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
4824                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
               }  
4825    
               pop @{$self->{open_elements}};  
               $self->{insertion_mode} = 'in table';  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'table') {  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ({  
                      tbody => 1, thead => 1, tfoot => 1,  
                     }->{$node->[1]}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
4826                  !!!next-token;                  !!!next-token;
4827                    !!!ack ('t227.2.1');
4828                  redo B;                  redo B;
4829                  } else {
4830                    !!!cp ('t227.2');
4831                    #
4832                }                }
   
               ## Clear back to table body context  
               while (not {  
                 tbody => 1, tfoot => 1, thead => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
   
               ## As if <{current node}>  
               ## have an element in table scope  
               ## true by definition  
   
               ## Clear back to table body context  
               ## nop by definition  
   
               pop @{$self->{open_elements}};  
               $self->{insertion_mode} = 'in table';  
               ## reprocess  
               redo B;  
             } elsif ({  
                       body => 1, caption => 1, col => 1, colgroup => 1,  
                       html => 1, td => 1, th => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
4833              } else {              } else {
4834                  !!!cp ('t227.1');
4835                #                #
4836              }              }
4837            } else {            } else {
4838                !!!cp ('t227.4');
4839              #              #
4840            }            }
4841                      } else {
4842            ## As if in table            !!!cp ('t227');
4843            !!!parse-error (type => 'in table:'.$token->{tag_name});            #
4844            $in_body->($insert_to_foster);          }
           redo B;  
         } elsif ($self->{insertion_mode} eq 'in row') {  
           if ($token->{type} eq 'character') {  
             ## NOTE: This is a "character in table" code clone.  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
                 
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
4845    
4846              !!!parse-error (type => 'in table:#character');          !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);
4847    
4848              ## As if in body, but insert into foster parent element          $insert = $insert_to_foster;
4849              ## ISSUE: Spec says that "whenever a node would be inserted          #
4850              ## into the current node" while characters might not be        } elsif ($token->{type} == END_TAG_TOKEN) {
4851              ## result in a new Text node.              if ($token->{tag_name} eq 'tr' and
4852              $reconstruct_active_formatting_elements->($insert_to_foster);                  $self->{insertion_mode} == IN_ROW_IM) {
               
             if ({  
                  table => 1, tbody => 1, tfoot => 1,  
                  thead => 1, tr => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               # MUST  
               my $foster_parent_element;  
               my $next_sibling;  
               my $prev_sibling;  
               OE: for (reverse 0..$#{$self->{open_elements}}) {  
                 if ($self->{open_elements}->[$_]->[1] eq 'table') {  
                   my $parent = $self->{open_elements}->[$_]->[0]->parent_node;  
                   if (defined $parent and $parent->node_type == 1) {  
                     $foster_parent_element = $parent;  
                     $next_sibling = $self->{open_elements}->[$_]->[0];  
                     $prev_sibling = $next_sibling->previous_sibling;  
                   } else {  
                     $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];  
                     $prev_sibling = $foster_parent_element->last_child;  
                   }  
                   last OE;  
                 }  
               } # OE  
               $foster_parent_element = $self->{open_elements}->[0]->[0] and  
               $prev_sibling = $foster_parent_element->last_child  
                 unless defined $foster_parent_element;  
               if (defined $prev_sibling and  
                   $prev_sibling->node_type == 3) {  
                 $prev_sibling->manakai_append_text ($token->{data});  
               } else {  
                 $foster_parent_element->insert_before  
                   ($self->{document}->create_text_node ($token->{data}),  
                    $next_sibling);  
               }  
             } else {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
             }  
               
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'th' or  
                 $token->{tag_name} eq 'td') {  
               ## Clear back to table row context  
               while (not {  
                 tr => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
                 
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               $self->{insertion_mode} = 'in cell';  
   
               push @$active_formatting_elements, ['#marker', ''];  
                 
               !!!next-token;  
               redo B;  
             } elsif ({  
                       caption => 1, col => 1, colgroup => 1,  
                       tbody => 1, tfoot => 1, thead => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               ## As if </tr>  
4853                ## have an element in table scope                ## have an element in table scope
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 'tr') {                  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 => 'unmacthed 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';                $self->{insertion_mode} = IN_TABLE_BODY_IM;
4887                ## reprocess                !!!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                ## NOTE: This is a code clone of "table in table"                if ($self->{insertion_mode} == IN_ROW_IM) {
4892                !!!parse-error (type => 'not closed:table');                  ## As if </tr>
4893                    ## have an element in table scope
4894                ## As if </table>                  my $i;
4895                ## have a table element in table scope                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4896                my $i;                    my $node = $self->{open_elements}->[$_];
4897                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                    if ($node->[1] & TABLE_ROW_EL) {
4898                  my $node = $self->{open_elements}->[$_];                      !!!cp ('t233');
4899                  if ($node->[1] eq 'table') {                      $i = $_;
4900                    $i = $_;                      last INSCOPE;
4901                    last INSCOPE;                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
4902                  } elsif ({                      !!!cp ('t234');
4903                            table => 1, html => 1,                      last INSCOPE;
4904                           }->{$node->[1]}) {                    }
4905                    last INSCOPE;                  } # INSCOPE
4906                    unless (defined $i) {
4907                      !!!cp ('t235');
4908    ## TODO: The following is wrong.
4909                      !!!parse-error (type => 'unmatched end tag:'.$token->{type}, token => $token);
4910                      ## Ignore the token
4911                      !!!nack ('t236.1');
4912                      !!!next-token;
4913                      redo B;
4914                  }                  }
4915                } # INSCOPE                  
4916                unless (defined $i) {                  ## Clear back to table row context
4917                  !!!parse-error (type => 'unmatched end tag:table');                  while (not ($self->{open_elements}->[-1]->[1]
4918                  ## Ignore tokens </table><table>                                  & TABLE_ROW_SCOPING_EL)) {
4919                  !!!next-token;                    !!!cp ('t236');
4920                  redo B;  ## ISSUE: Can this state be reached?
4921                }                    pop @{$self->{open_elements}};
                 
               ## 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; # <table>  
                 $token = {type => 'end tag', tag_name => 'table'};  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           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]);  
               }  
   
               splice @{$self->{open_elements}}, $i;  
   
               $self->_reset_insertion_mode;  
   
               ## reprocess  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'tr') {  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
4922                  }                  }
4923                } # INSCOPE                  
4924                unless (defined $i) {                  pop @{$self->{open_elements}}; # tr
4925                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
4926                  ## Ignore the token                  ## reprocess in the "in table body" insertion mode...
4927                  !!!next-token;                }
4928                  redo B;  
4929                }                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
4930                    ## have an element in table scope
4931                ## Clear back to table row context                  my $i;
4932                while (not {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4933                  tr => 1, html => 1,                    my $node = $self->{open_elements}->[$_];
4934                }->{$self->{open_elements}->[-1]->[1]}) {                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
4935                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                      !!!cp ('t237');
4936                        $i = $_;
4937                        last INSCOPE;
4938                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4939                        !!!cp ('t238');
4940                        last INSCOPE;
4941                      }
4942                    } # INSCOPE
4943                    unless (defined $i) {
4944                      !!!cp ('t239');
4945                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4946                      ## Ignore the token
4947                      !!!nack ('t239.1');
4948                      !!!next-token;
4949                      redo B;
4950                    }
4951                    
4952                    ## Clear back to table body context
4953                    while (not ($self->{open_elements}->[-1]->[1]
4954                                    & TABLE_ROWS_SCOPING_EL)) {
4955                      !!!cp ('t240');
4956                      pop @{$self->{open_elements}};
4957                    }
4958                    
4959                    ## As if <{current node}>
4960                    ## have an element in table scope
4961                    ## true by definition
4962                    
4963                    ## Clear back to table body context
4964                    ## nop by definition
4965                    
4966                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4967                    $self->{insertion_mode} = IN_TABLE_IM;
4968                    ## reprocess in the "in table" insertion mode...
4969                }                }
4970    
4971                pop @{$self->{open_elements}}; # tr                ## NOTE: </table> in the "in table" insertion mode.
4972                $self->{insertion_mode} = 'in table body';                ## When you edit the code fragment below, please ensure that
4973                !!!next-token;                ## the code for <table> in the "in table" insertion mode
4974                redo B;                ## is synced with it.
4975              } elsif ($token->{tag_name} eq 'table') {  
4976                ## As if </tr>                ## have a table element in table scope
               ## have an 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 'tr') {                  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->{type});                  !!!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                }                }
4997                    
4998                ## Clear back to table row context                splice @{$self->{open_elements}}, $i;
4999                while (not {                pop @{$open_tables};
5000                  tr => 1, html => 1,                
5001                }->{$self->{open_elements}->[-1]->[1]}) {                $self->_reset_insertion_mode;
5002                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                
5003                  pop @{$self->{open_elements}};                !!!next-token;
               }  
   
               pop @{$self->{open_elements}}; # tr  
               $self->{insertion_mode} = 'in table body';  
               ## reprocess  
5004                redo B;                redo B;
5005              } elsif ({              } elsif ({
5006                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
5007                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}} and
5008                ## have an element in table scope                       $self->{insertion_mode} & ROW_IMS) {
5009                my $i;                if ($self->{insertion_mode} == IN_ROW_IM) {
5010                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  ## have an element in table scope
5011                  my $node = $self->{open_elements}->[$_];                  my $i;
5012                  if ($node->[1] eq $token->{tag_name}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5013                    $i = $_;                    my $node = $self->{open_elements}->[$_];
5014                    last INSCOPE;                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5015                  } elsif ({                      !!!cp ('t247');
5016                            table => 1, html => 1,                      $i = $_;
5017                           }->{$node->[1]}) {                      last INSCOPE;
5018                    last INSCOPE;                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5019                        !!!cp ('t248');
5020                        last INSCOPE;
5021                      }
5022                    } # INSCOPE
5023                      unless (defined $i) {
5024                        !!!cp ('t249');
5025                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5026                        ## Ignore the token
5027                        !!!nack ('t249.1');
5028                        !!!next-token;
5029                        redo B;
5030                      }
5031                    
5032                    ## As if </tr>
5033                    ## have an element in table scope
5034                    my $i;
5035                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5036                      my $node = $self->{open_elements}->[$_];
5037                      if ($node->[1] & TABLE_ROW_EL) {
5038                        !!!cp ('t250');
5039                        $i = $_;
5040                        last INSCOPE;
5041                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5042                        !!!cp ('t251');
5043                        last INSCOPE;
5044                      }
5045                    } # INSCOPE
5046                      unless (defined $i) {
5047                        !!!cp ('t252');
5048                        !!!parse-error (type => 'unmatched end tag:tr', token => $token);
5049                        ## Ignore the token
5050                        !!!nack ('t252.1');
5051                        !!!next-token;
5052                        redo B;
5053                      }
5054                    
5055                    ## Clear back to table row context
5056                    while (not ($self->{open_elements}->[-1]->[1]
5057                                    & TABLE_ROW_SCOPING_EL)) {
5058                      !!!cp ('t253');
5059    ## ISSUE: Can this case be reached?
5060                      pop @{$self->{open_elements}};
5061                  }                  }
5062                } # INSCOPE                  
5063                unless (defined $i) {                  pop @{$self->{open_elements}}; # tr
5064                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
5065                  ## Ignore the token                  ## reprocess in the "in table body" insertion mode...
                 !!!next-token;  
                 redo B;  
5066                }                }
5067    
               ## As if </tr>  
5068                ## have an element in table scope                ## have an element in table scope
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 'tr') {                  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:tr');                  !!!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 row context                ## Clear back to table body context
5091                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5092                  tr => 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}}; # tr                pop @{$self->{open_elements}};
5099                $self->{insertion_mode} = 'in table body';                $self->{insertion_mode} = IN_TABLE_IM;
5100                ## reprocess                !!!nack ('t257.1');
5101                  !!!next-token;
5102                redo B;                redo B;
5103              } elsif ({              } elsif ({
5104                        body => 1, caption => 1, col => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
5105                        colgroup => 1, html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
5106                          tr => 1, # $self->{insertion_mode} == IN_ROW_IM
5107                          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              } else {             !!!next-token;
               #  
             }  
           } else {  
             #  
           }  
   
           ## As if in table  
           !!!parse-error (type => 'in table:'.$token->{tag_name});  
           $in_body->($insert_to_foster);  
5114            redo B;            redo B;
5115          } elsif ($self->{insertion_mode} eq 'in cell') {          } else {
5116            if ($token->{type} eq 'character') {            !!!cp ('t259');
5117              ## NOTE: This is a code clone of "character in body".            !!!parse-error (type => 'in table:/'.$token->{tag_name}, token => $token);
             $reconstruct_active_formatting_elements->($insert_to_current);  
               
             $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
5118    
5119              !!!next-token;            $insert = $insert_to_foster;
5120              redo B;            #
5121            } elsif ($token->{type} eq 'start tag') {          }
5122              if ({        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5123                   caption => 1, col => 1, colgroup => 1,          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
5124                   tbody => 1, td => 1, tfoot => 1, th => 1,                  @{$self->{open_elements}} == 1) { # redundant, maybe
5125                   thead => 1, tr => 1,            !!!parse-error (type => 'in body:#eof', token => $token);
5126                  }->{$token->{tag_name}}) {            !!!cp ('t259.1');
5127                ## have an element in table scope            #
5128                my $tn;          } else {
5129                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            !!!cp ('t259.2');
5130                  my $node = $self->{open_elements}->[$_];            #
5131                  if ($node->[1] eq 'td' or $node->[1] eq 'th') {          }
5132                    $tn = $node->[1];  
5133                    last INSCOPE;          ## Stop parsing
5134                  } elsif ({          last B;
5135                            table => 1, html => 1,        } else {
5136                           }->{$node->[1]}) {          die "$0: $token->{type}: Unknown token type";
5137                    last INSCOPE;        }
5138                  }      } elsif ($self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
5139                } # INSCOPE            if ($token->{type} == CHARACTER_TOKEN) {
5140                unless (defined $tn) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5141                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5142                  ## Ignore the token                unless (length $token->{data}) {
5143                    !!!cp ('t260');
5144                  !!!next-token;                  !!!next-token;
5145                  redo B;                  redo B;
5146                }                }
5147                }
5148                ## Close the cell              
5149                !!!back-token; # <?>              !!!cp ('t261');
5150                $token = {type => 'end tag', tag_name => $tn};              #
5151              } elsif ($token->{type} == START_TAG_TOKEN) {
5152                if ($token->{tag_name} eq 'col') {
5153                  !!!cp ('t262');
5154                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5155                  pop @{$self->{open_elements}};
5156                  !!!ack ('t262.1');
5157                  !!!next-token;
5158                redo B;                redo B;
5159              } else {              } else {
5160                  !!!cp ('t263');
5161                #                #
5162              }              }
5163            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
5164              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {              if ($token->{tag_name} eq 'colgroup') {
5165                ## have an element in table scope                if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5166                my $i;                  !!!cp ('t264');
5167                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
5168                  ## Ignore the token                  ## Ignore the token
5169                  !!!next-token;                  !!!next-token;
5170                  redo B;                  redo B;
5171                  } else {
5172                    !!!cp ('t265');
5173                    pop @{$self->{open_elements}}; # colgroup
5174                    $self->{insertion_mode} = IN_TABLE_IM;
5175                    !!!next-token;
5176                    redo B;            
5177                }                }
5178                              } elsif ($token->{tag_name} eq 'col') {
5179                ## generate implied end tags                !!!cp ('t266');
5180                if ({                !!!parse-error (type => 'unmatched end tag:col', token => $token);
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => ($token->{tag_name} eq 'th'),  
                    th => ($token->{tag_name} eq 'td'),  
                    tr => 1,  
                    tbody => 1, tfoot=> 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
   
               if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               }  
   
               splice @{$self->{open_elements}}, $i;  
   
               $clear_up_to_marker->();  
   
               $self->{insertion_mode} = 'in row';  
   
               !!!next-token;  
               redo B;  
             } elsif ({  
                       body => 1, caption => 1, col => 1,  
                       colgroup => 1, html => 1,  
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
5181                ## Ignore the token                ## Ignore the token
5182                !!!next-token;                !!!next-token;
5183                redo B;                redo B;
             } elsif ({  
                       table => 1, tbody => 1, tfoot => 1,  
                       thead => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               ## have an element in table scope  
               my $i;  
               my $tn;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {  
                   $tn = $node->[1];  
                   ## NOTE: There is exactly one |td| or |th| element  
                   ## in scope in the stack of open elements by definition.  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Close the cell  
               !!!back-token; # </?>  
               $token = {type => 'end tag', tag_name => $tn};  
               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            $in_body->($insert_to_current);            ## 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;            redo B;
5201          } elsif ($self->{insertion_mode} eq 'in select') {          }
5202            if ($token->{type} eq 'character') {        } else {
5203              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          die "$0: $token->{type}: Unknown token type";
5204          }
5205    
5206              ## As if </colgroup>
5207              if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5208                !!!cp ('t269');
5209    ## TODO: Wrong error type?
5210                !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);
5211                ## Ignore the token
5212                !!!nack ('t269.1');
5213              !!!next-token;              !!!next-token;
5214              redo B;              redo B;
5215            } elsif ($token->{type} eq 'start tag') {            } else {
5216              if ($token->{tag_name} eq 'option') {              !!!cp ('t270');
5217                if ($self->{open_elements}->[-1]->[1] eq 'option') {              pop @{$self->{open_elements}}; # colgroup
5218                  ## As if </option>              $self->{insertion_mode} = IN_TABLE_IM;
5219                  pop @{$self->{open_elements}};              !!!ack-later;
5220                }              ## reprocess
5221                redo B;
5222              }
5223        } elsif ($self->{insertion_mode} & SELECT_IMS) {
5224          if ($token->{type} == CHARACTER_TOKEN) {
5225            !!!cp ('t271');
5226            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5227            !!!next-token;
5228            redo B;
5229          } elsif ($token->{type} == START_TAG_TOKEN) {
5230            if ($token->{tag_name} eq 'option') {
5231              if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5232                !!!cp ('t272');
5233                ## 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              } else {              !!!next-token;
5306                #              redo B;
5307              } else {
5308                !!!cp ('t281.1');
5309                !!!ack-later;
5310                ## Reprocess the token.
5311                redo B;
5312              }
5313            } else {
5314              !!!cp ('t282');
5315              !!!parse-error (type => 'in select:'.$token->{tag_name}, token => $token);
5316              ## Ignore the token
5317              !!!nack ('t282.1');
5318              !!!next-token;
5319              redo B;
5320            }
5321          } elsif ($token->{type} == END_TAG_TOKEN) {
5322            if ($token->{tag_name} eq 'optgroup') {
5323              if ($self->{open_elements}->[-1]->[1] & OPTION_EL and
5324                  $self->{open_elements}->[-2]->[1] & OPTGROUP_EL) {
5325                !!!cp ('t283');
5326                ## As if </option>
5327                splice @{$self->{open_elements}}, -2;
5328              } elsif ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
5329                !!!cp ('t284');
5330                pop @{$self->{open_elements}};
5331              } else {
5332                !!!cp ('t285');
5333                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5334                ## Ignore the token
5335              }
5336              !!!nack ('t285.1');
5337              !!!next-token;
5338              redo B;
5339            } elsif ($token->{tag_name} eq 'option') {
5340              if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5341                !!!cp ('t286');
5342                pop @{$self->{open_elements}};
5343              } else {
5344                !!!cp ('t287');
5345                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5346                ## Ignore the token
5347              }
5348              !!!nack ('t287.1');
5349              !!!next-token;
5350              redo B;
5351            } elsif ($token->{tag_name} eq 'select') {
5352              ## have an element in table scope
5353              my $i;
5354              INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5355                my $node = $self->{open_elements}->[$_];
5356                if ($node->[1] & SELECT_EL) {
5357                  !!!cp ('t288');
5358                  $i = $_;
5359                  last INSCOPE;
5360                } elsif ($node->[1] & TABLE_SCOPING_EL) {
5361                  !!!cp ('t289');
5362                  last INSCOPE;
5363              }              }
5364            } elsif ($token->{type} eq 'end tag') {            } # INSCOPE
5365              if ($token->{tag_name} eq 'optgroup') {            unless (defined $i) {
5366                if ($self->{open_elements}->[-1]->[1] eq 'option' and              !!!cp ('t290');
5367                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5368                  ## As if </option>              ## Ignore the token
5369                  splice @{$self->{open_elements}}, -2;              !!!nack ('t290.1');
5370                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {              !!!next-token;
5371                  pop @{$self->{open_elements}};              redo B;
5372                } else {            }
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
               }  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'option') {  
               if ($self->{open_elements}->[-1]->[1] eq 'option') {  
                 pop @{$self->{open_elements}};  
               } else {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
               }  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'select') {  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
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                ## have an element in table scope  ## TODO: The following is wrong?
5388                my $i;            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
5389                                
5390                ## As if </select>            ## have an element in table scope
5391                ## have an element in table scope            my $i;
5392                undef $i;            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5393                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {              my $node = $self->{open_elements}->[$_];
5394                  my $node = $self->{open_elements}->[$_];              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5395                  if ($node->[1] eq 'select') {                !!!cp ('t292');
5396                    $i = $_;                $i = $_;
5397                    last INSCOPE;                last INSCOPE;
5398                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5399                            table => 1, html => 1,                !!!cp ('t293');
5400                           }->{$node->[1]}) {                last INSCOPE;
5401                    last INSCOPE;              }
5402                  }            } # INSCOPE
5403                } # INSCOPE            unless (defined $i) {
5404                unless (defined $i) {              !!!cp ('t294');
5405                  !!!parse-error (type => 'unmatched end tag:select');              ## Ignore the token
5406                  ## Ignore the </select> token              !!!nack ('t294.1');
5407                  !!!next-token; ## TODO: ok?              !!!next-token;
5408                  redo B;              redo B;
5409                }            }
5410                                
5411                splice @{$self->{open_elements}}, $i;            ## As if </select>
5412              ## have an element in table scope
5413                $self->_reset_insertion_mode;            undef $i;
5414              INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5415                ## reprocess              my $node = $self->{open_elements}->[$_];
5416                redo B;              if ($node->[1] & SELECT_EL) {
5417              } else {                !!!cp ('t295');
5418                #                $i = $_;
5419                  last INSCOPE;
5420                } elsif ($node->[1] & TABLE_SCOPING_EL) {
5421    ## ISSUE: Can this state be reached?
5422                  !!!cp ('t296');
5423                  last INSCOPE;
5424              }              }
5425            } else {            } # INSCOPE
5426              #            unless (defined $i) {
5427                !!!cp ('t297');
5428    ## TODO: The following error type is correct?
5429                !!!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              !!!cp ('t298');
5437              splice @{$self->{open_elements}}, $i;
5438    
5439              $self->_reset_insertion_mode;
5440    
5441            !!!parse-error (type => 'in select:'.$token->{tag_name});            !!!ack-later;
5442              ## reprocess
5443              redo B;
5444            } else {
5445              !!!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          } elsif ($self->{insertion_mode} eq 'after body') {          }
5452            if ($token->{type} eq 'character') {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5453              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
5454                my $data = $1;                  @{$self->{open_elements}} == 1) { # redundant, maybe
5455                ## As if in body            !!!cp ('t299.1');
5456                $reconstruct_active_formatting_elements->($insert_to_current);            !!!parse-error (type => 'in body:#eof', token => $token);
5457            } else {
5458              !!!cp ('t299.2');
5459            }
5460    
5461            ## Stop parsing.
5462            last B;
5463          } else {
5464            die "$0: $token->{type}: Unknown token type";
5465          }
5466        } elsif ($self->{insertion_mode} & BODY_AFTER_IMS) {
5467          if ($token->{type} == CHARACTER_TOKEN) {
5468            if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5469              my $data = $1;
5470              ## As if in body
5471              $reconstruct_active_formatting_elements->($insert_to_current);
5472                                
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}) {
5476                !!!cp ('t300');
5477                !!!next-token;
5478                redo B;
5479              }
5480            }
5481            
5482            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
5483              !!!cp ('t301');
5484              !!!parse-error (type => 'after html:#character', token => $token);
5485    
5486                unless (length $token->{data}) {            ## Reprocess in the "after body" insertion mode.
5487                  !!!next-token;          } else {
5488                  redo B;            !!!cp ('t302');
5489                }          }
5490              }          
5491                        ## "after body" insertion mode
5492              #          !!!parse-error (type => 'after body:#character', token => $token);
5493              !!!parse-error (type => 'after body:#character');  
5494            } elsif ($token->{type} eq 'start tag') {          $self->{insertion_mode} = IN_BODY_IM;
5495              !!!parse-error (type => 'after body:'.$token->{tag_name});          ## reprocess
5496              #          redo B;
5497            } elsif ($token->{type} eq 'end tag') {        } elsif ($token->{type} == START_TAG_TOKEN) {
5498              if ($token->{tag_name} eq 'html') {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
5499                if (defined $self->{inner_html_node}) {            !!!cp ('t303');
5500                  !!!parse-error (type => 'unmatched end tag:html');            !!!parse-error (type => 'after html:'.$token->{tag_name}, token => $token);
5501                  ## Ignore the token            
5502                  !!!next-token;            ## Reprocess in the "after body" insertion mode.
5503                  redo B;          } else {
5504                } else {            !!!cp ('t304');
5505                  $previous_insertion_mode = $self->{insertion_mode};          }
5506                  $self->{insertion_mode} = 'trailing end';  
5507                  !!!next-token;          ## "after body" insertion mode
5508                  redo B;          !!!parse-error (type => 'after body:'.$token->{tag_name}, token => $token);
5509                }  
5510              } else {          $self->{insertion_mode} = IN_BODY_IM;
5511                !!!parse-error (type => 'after body:/'.$token->{tag_name});          !!!ack-later;
5512              }          ## reprocess
5513            redo B;
5514          } elsif ($token->{type} == END_TAG_TOKEN) {
5515            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
5516              !!!cp ('t305');
5517              !!!parse-error (type => 'after html:/'.$token->{tag_name}, token => $token);
5518              
5519              $self->{insertion_mode} = AFTER_BODY_IM;
5520              ## Reprocess in the "after body" insertion mode.
5521            } else {
5522              !!!cp ('t306');
5523            }
5524    
5525            ## "after body" insertion mode
5526            if ($token->{tag_name} eq 'html') {
5527              if (defined $self->{inner_html_node}) {
5528                !!!cp ('t307');
5529                !!!parse-error (type => 'unmatched end tag:html', token => $token);
5530                ## Ignore the token
5531                !!!next-token;
5532                redo B;
5533            } else {            } else {
5534              die "$0: $token->{type}: Unknown token type";              !!!cp ('t308');
5535                $self->{insertion_mode} = AFTER_HTML_BODY_IM;
5536                !!!next-token;
5537                redo B;
5538            }            }
5539            } else {
5540              !!!cp ('t309');
5541              !!!parse-error (type => 'after body:/'.$token->{tag_name}, token => $token);
5542    
5543            $self->{insertion_mode} = 'in body';            $self->{insertion_mode} = IN_BODY_IM;
5544            ## reprocess            ## reprocess
5545            redo B;            redo B;
5546      } elsif ($self->{insertion_mode} eq 'in frameset') {          }
5547        if ($token->{type} eq 'character') {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5548            !!!cp ('t309.2');
5549            ## Stop parsing
5550            last B;
5551          } else {
5552            die "$0: $token->{type}: Unknown token type";
5553          }
5554        } elsif ($self->{insertion_mode} & FRAME_IMS) {
5555          if ($token->{type} == CHARACTER_TOKEN) {
5556          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
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            }            }
5564          }          }
5565            
5566          !!!parse-error (type => 'in frameset:#character');          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
5567          ## Ignore the token            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
5568          !!!next-token;              !!!cp ('t311');
5569          redo B;              !!!parse-error (type => 'in frameset:#character', token => $token);
5570        } elsif ($token->{type} eq 'start tag') {            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
5571          if ($token->{tag_name} eq 'frameset') {              !!!cp ('t312');
5572            !!!insert-element ($token->{tag_name}, $token->{attributes});              !!!parse-error (type => 'after frameset:#character', token => $token);
5573              } else { # "after html frameset"
5574                !!!cp ('t313');
5575                !!!parse-error (type => 'after html:#character', token => $token);
5576    
5577                $self->{insertion_mode} = AFTER_FRAMESET_IM;
5578                ## Reprocess in the "after frameset" insertion mode.
5579                !!!parse-error (type => 'after frameset:#character', token => $token);
5580              }
5581              
5582              ## Ignore the token.
5583              if (length $token->{data}) {
5584                !!!cp ('t314');
5585                ## reprocess the rest of characters
5586              } else {
5587                !!!cp ('t315');
5588                !!!next-token;
5589              }
5590              redo B;
5591            }
5592            
5593            die qq[$0: Character "$token->{data}"];
5594          } elsif ($token->{type} == START_TAG_TOKEN) {
5595            if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
5596              !!!cp ('t316');
5597              !!!parse-error (type => 'after html:'.$token->{tag_name}, token => $token);
5598    
5599              $self->{insertion_mode} = AFTER_FRAMESET_IM;
5600              ## Process in the "after frameset" insertion mode.
5601            } else {
5602              !!!cp ('t317');
5603            }
5604    
5605            if ($token->{tag_name} eq 'frameset' and
5606                $self->{insertion_mode} == IN_FRAMESET_IM) {
5607              !!!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') {          } elsif ($token->{tag_name} eq 'frame' and
5613            !!!insert-element ($token->{tag_name}, $token->{attributes});                   $self->{insertion_mode} == IN_FRAMESET_IM) {
5614              !!!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            $in_body->($insert_to_current);            !!!cp ('t320');
5622              ## NOTE: As if in body.
5623              $parse_rcdata->(CDATA_CONTENT_MODEL);
5624            redo B;            redo B;
5625          } else {          } else {
5626            !!!parse-error (type => 'in frameset:'.$token->{tag_name});            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
5627                !!!cp ('t321');
5628                !!!parse-error (type => 'in frameset:'.$token->{tag_name}, token => $token);
5629              } else {
5630                !!!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} eq 'end tag') {        } elsif ($token->{type} == END_TAG_TOKEN) {
5639          if ($token->{tag_name} eq 'frameset') {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
5640            if ($self->{open_elements}->[-1]->[1] eq 'html' and            !!!cp ('t323');
5641              !!!parse-error (type => 'after html:/'.$token->{tag_name}, token => $token);
5642    
5643              $self->{insertion_mode} = AFTER_FRAMESET_IM;
5644              ## Process in the "after frameset" insertion mode.
5645            } else {
5646              !!!cp ('t324');
5647            }
5648    
5649            if ($token->{tag_name} eq 'frameset' and
5650                $self->{insertion_mode} == IN_FRAMESET_IM) {
5651              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              $self->{insertion_mode} = 'after frameset';              !!!cp ('t327');
5666                $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
5672                     $self->{insertion_mode} == AFTER_FRAMESET_IM) {
5673              !!!cp ('t329');
5674              $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
5675              !!!next-token;
5676              redo B;
5677          } else {          } else {
5678            !!!parse-error (type => 'in frameset:/'.$token->{tag_name});            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
5679                !!!cp ('t330');
5680                !!!parse-error (type => 'in frameset:/'.$token->{tag_name}, token => $token);
5681              } else {
5682                !!!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        }        }
     } elsif ($self->{insertion_mode} eq 'after frameset') {  
       if ($token->{type} eq 'character') {  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
5703    
5704                unless (length $token->{data}) {        ## ISSUE: An issue in spec here
5705                  !!!next-token;      } else {
5706                  redo B;        die "$0: $self->{insertion_mode}: Unknown insertion mode";
5707                }      }
             }  
5708    
5709              if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {      ## "in body" insertion mode
5710                !!!parse-error (type => 'after frameset:#character');      if ($token->{type} == START_TAG_TOKEN) {
5711          if ($token->{tag_name} eq 'script') {
5712            !!!cp ('t332');
5713            ## NOTE: This is an "as if in head" code clone
5714            $script_start_tag->();
5715            redo B;
5716          } elsif ($token->{tag_name} eq 'style') {
5717            !!!cp ('t333');
5718            ## NOTE: This is an "as if in head" code clone
5719            $parse_rcdata->(CDATA_CONTENT_MODEL);
5720            redo B;
5721          } elsif ({
5722                    base => 1, link => 1,
5723                   }->{$token->{tag_name}}) {
5724            !!!cp ('t334');
5725            ## NOTE: This is an "as if in head" code clone, only "-t" differs
5726            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
5727            pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
5728            !!!ack ('t334.1');
5729            !!!next-token;
5730            redo B;
5731          } elsif ($token->{tag_name} eq 'meta') {
5732            ## NOTE: This is an "as if in head" code clone, only "-t" differs
5733            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
5734            my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
5735    
5736                ## Ignore the token.          unless ($self->{confident}) {
5737                if (length $token->{data}) {            if ($token->{attributes}->{charset}) { ## TODO: And if supported
5738                  ## reprocess the rest of characters              !!!cp ('t335');
5739                } else {              $self->{change_encoding}
5740                  !!!next-token;                  ->($self, $token->{attributes}->{charset}->{value}, $token);
5741                }              
5742                redo B;              $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.
5748                if ($token->{attributes}->{content}->{value}
5749                    =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
5750                        [\x09-\x0D\x20]*=
5751                        [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
5752                        ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
5753                  !!!cp ('t336');
5754                  $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              }
5777            }
5778    
5779          die qq[$0: Character "$token->{data}"];          !!!ack ('t338.1');
5780        } elsif ($token->{type} eq 'start tag') {          !!!next-token;
5781          if ($token->{tag_name} eq 'noframes') {          redo B;
5782            $in_body->($insert_to_current);        } elsif ($token->{tag_name} eq 'title') {
5783            redo B;          !!!cp ('t341');
5784            ## NOTE: This is an "as if in head" code clone
5785            $parse_rcdata->(RCDATA_CONTENT_MODEL);
5786            redo B;
5787          } elsif ($token->{tag_name} eq 'body') {
5788            !!!parse-error (type => 'in body:body', token => $token);
5789                  
5790            if (@{$self->{open_elements}} == 1 or
5791                not ($self->{open_elements}->[1]->[1] & BODY_EL)) {
5792              !!!cp ('t342');
5793              ## Ignore the token
5794          } else {          } else {
5795            !!!parse-error (type => 'after frameset:'.$token->{tag_name});            my $body_el = $self->{open_elements}->[1]->[0];
5796              for my $attr_name (keys %{$token->{attributes}}) {
5797                unless ($body_el->has_attribute_ns (undef, $attr_name)) {
5798                  !!!cp ('t343');
5799                  $body_el->set_attribute_ns
5800                    (undef, [undef, $attr_name],
5801                     $token->{attributes}->{$attr_name}->{value});
5802                }
5803              }
5804            }
5805            !!!nack ('t343.1');
5806            !!!next-token;
5807            redo B;
5808          } elsif ({
5809                    address => 1, blockquote => 1, center => 1, dir => 1,
5810                    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,
5813                    pre => 1, listing => 1,
5814                    form => 1,
5815                    table => 1,
5816                    hr => 1,
5817                   }->{$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            ## Ignore the token
5822              !!!nack ('t350.1');
5823            !!!next-token;            !!!next-token;
5824            redo B;            redo B;
5825          }          }
5826        } elsif ($token->{type} eq 'end tag') {  
5827          if ($token->{tag_name} eq 'html') {          ## has a p element in scope
5828            $previous_insertion_mode = $self->{insertion_mode};          INSCOPE: for (reverse @{$self->{open_elements}}) {
5829            $self->{insertion_mode} = 'trailing end';            if ($_->[1] & P_EL) {
5830                !!!cp ('t344');
5831                !!!back-token; # <form>
5832                $token = {type => END_TAG_TOKEN, tag_name => 'p',
5833                          line => $token->{line}, column => $token->{column}};
5834                redo B;
5835              } elsif ($_->[1] & SCOPING_EL) {
5836                !!!cp ('t345');
5837                last INSCOPE;
5838              }
5839            } # INSCOPE
5840              
5841            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
5842            if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {
5843              !!!nack ('t346.1');
5844              !!!next-token;
5845              if ($token->{type} == CHARACTER_TOKEN) {
5846                $token->{data} =~ s/^\x0A//;
5847                unless (length $token->{data}) {
5848                  !!!cp ('t346');
5849                  !!!next-token;
5850                } else {
5851                  !!!cp ('t349');
5852                }
5853              } else {
5854                !!!cp ('t348');
5855              }
5856            } 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;
5862            } elsif ($token->{tag_name} eq 'table') {
5863              !!!cp ('t382');
5864              push @{$open_tables}, [$self->{open_elements}->[-1]->[0]];
5865              
5866              $self->{insertion_mode} = IN_TABLE_IM;
5867    
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            !!!parse-error (type => 'after frameset:/'.$token->{tag_name});            !!!nack ('t347.1');
5878              !!!next-token;
5879            }
5880            redo B;
5881          } elsif ({li => 1, dt => 1, dd => 1}->{$token->{tag_name}}) {
5882            ## has a p element in scope
5883            INSCOPE: for (reverse @{$self->{open_elements}}) {
5884              if ($_->[1] & P_EL) {
5885                !!!cp ('t353');
5886                !!!back-token; # <x>
5887                $token = {type => END_TAG_TOKEN, tag_name => 'p',
5888                          line => $token->{line}, column => $token->{column}};
5889                redo B;
5890              } elsif ($_->[1] & SCOPING_EL) {
5891                !!!cp ('t354');
5892                last INSCOPE;
5893              }
5894            } # INSCOPE
5895              
5896            ## Step 1
5897            my $i = -1;
5898            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: {
5903              ## Step 2
5904              if ($li_or_dtdd->{$node->[0]->manakai_local_name}) {
5905                if ($i != -1) {
5906                  !!!cp ('t355');
5907                  !!!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;
5915                last LI;
5916              } else {
5917                !!!cp ('t357');
5918              }
5919              
5920              ## Step 3
5921              if (not ($node->[1] & FORMATTING_EL) and
5922                  #not $phrasing_category->{$node->[1]} and
5923                  ($node->[1] & SPECIAL_EL or
5924                   $node->[1] & SCOPING_EL) and
5925                  not ($node->[1] & ADDRESS_EL) and
5926                  not ($node->[1] & DIV_EL)) {
5927                !!!cp ('t358');
5928                last LI;
5929              }
5930              
5931              !!!cp ('t359');
5932              ## Step 4
5933              $i--;
5934              $node = $self->{open_elements}->[$i];
5935              redo LI;
5936            } # LI
5937              
5938            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
5939            !!!nack ('t359.1');
5940            !!!next-token;
5941            redo B;
5942          } elsif ($token->{tag_name} eq 'plaintext') {
5943            ## has a p element in scope
5944            INSCOPE: for (reverse @{$self->{open_elements}}) {
5945              if ($_->[1] & P_EL) {
5946                !!!cp ('t367');
5947                !!!back-token; # <plaintext>
5948                $token = {type => END_TAG_TOKEN, tag_name => 'p',
5949                          line => $token->{line}, column => $token->{column}};
5950                redo B;
5951              } elsif ($_->[1] & SCOPING_EL) {
5952                !!!cp ('t368');
5953                last INSCOPE;
5954              }
5955            } # INSCOPE
5956              
5957            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
5958              
5959            $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
5960              
5961            !!!nack ('t368.1');
5962            !!!next-token;
5963            redo B;
5964          } elsif ($token->{tag_name} eq 'a') {
5965            AFE: for my $i (reverse 0..$#$active_formatting_elements) {
5966              my $node = $active_formatting_elements->[$i];
5967              if ($node->[1] & A_EL) {
5968                !!!cp ('t371');
5969                !!!parse-error (type => 'in a:a', token => $token);
5970                
5971                !!!back-token; # <a>
5972                $token = {type => END_TAG_TOKEN, tag_name => 'a',
5973                          line => $token->{line}, column => $token->{column}};
5974                $formatting_end_tag->($token);
5975                
5976                AFE2: for (reverse 0..$#$active_formatting_elements) {
5977                  if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
5978                    !!!cp ('t372');
5979                    splice @$active_formatting_elements, $_, 1;
5980                    last AFE2;
5981                  }
5982                } # AFE2
5983                OE: for (reverse 0..$#{$self->{open_elements}}) {
5984                  if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {
5985                    !!!cp ('t373');
5986                    splice @{$self->{open_elements}}, $_, 1;
5987                    last OE;
5988                  }
5989                } # OE
5990                last AFE;
5991              } elsif ($node->[0] eq '#marker') {
5992                !!!cp ('t374');
5993                last AFE;
5994              }
5995            } # AFE
5996              
5997            $reconstruct_active_formatting_elements->($insert_to_current);
5998    
5999            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6000            push @$active_formatting_elements, $self->{open_elements}->[-1];
6001    
6002            !!!nack ('t374.1');
6003            !!!next-token;
6004            redo B;
6005          } elsif ($token->{tag_name} eq 'nobr') {
6006            $reconstruct_active_formatting_elements->($insert_to_current);
6007    
6008            ## has a |nobr| element in scope
6009            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6010              my $node = $self->{open_elements}->[$_];
6011              if ($node->[1] & NOBR_EL) {
6012                !!!cp ('t376');
6013                !!!parse-error (type => 'in nobr:nobr', token => $token);
6014                !!!back-token; # <nobr>
6015                $token = {type => END_TAG_TOKEN, tag_name => 'nobr',
6016                          line => $token->{line}, column => $token->{column}};
6017                redo B;
6018              } elsif ($node->[1] & SCOPING_EL) {
6019                !!!cp ('t377');
6020                last INSCOPE;
6021              }
6022            } # INSCOPE
6023            
6024            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6025            push @$active_formatting_elements, $self->{open_elements}->[-1];
6026            
6027            !!!nack ('t377.1');
6028            !!!next-token;
6029            redo B;
6030          } elsif ($token->{tag_name} eq 'button') {
6031            ## has a button element in scope
6032            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6033              my $node = $self->{open_elements}->[$_];
6034              if ($node->[1] & BUTTON_EL) {
6035                !!!cp ('t378');
6036                !!!parse-error (type => 'in button:button', token => $token);
6037                !!!back-token; # <button>
6038                $token = {type => END_TAG_TOKEN, tag_name => 'button',
6039                          line => $token->{line}, column => $token->{column}};
6040                redo B;
6041              } elsif ($node->[1] & SCOPING_EL) {
6042                !!!cp ('t379');
6043                last INSCOPE;
6044              }
6045            } # INSCOPE
6046              
6047            $reconstruct_active_formatting_elements->($insert_to_current);
6048              
6049            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6050    
6051            ## TODO: associate with $self->{form_element} if defined
6052    
6053            push @$active_formatting_elements, ['#marker', ''];
6054    
6055            !!!nack ('t379.1');
6056            !!!next-token;
6057            redo B;
6058          } elsif ({
6059                    xmp => 1,
6060                    iframe => 1,
6061                    noembed => 1,
6062                    noframes => 1,
6063                    noscript => 0, ## TODO: 1 if scripting is enabled
6064                   }->{$token->{tag_name}}) {
6065            if ($token->{tag_name} eq 'xmp') {
6066              !!!cp ('t381');
6067              $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            $parse_rcdata->(CDATA_CONTENT_MODEL);
6073            redo B;
6074          } elsif ($token->{tag_name} eq 'isindex') {
6075            !!!parse-error (type => 'isindex', token => $token);
6076            
6077            if (defined $self->{form_element}) {
6078              !!!cp ('t389');
6079            ## Ignore the token            ## Ignore the token
6080              !!!nack ('t389'); ## NOTE: Not acknowledged.
6081              !!!next-token;
6082              redo B;
6083            } else {
6084              my $at = $token->{attributes};
6085              my $form_attrs;
6086              $form_attrs->{action} = $at->{action} if $at->{action};
6087              my $prompt_attr = $at->{prompt};
6088              $at->{name} = {name => 'name', value => 'isindex'};
6089              delete $at->{action};
6090              delete $at->{prompt};
6091              my @tokens = (
6092                            {type => START_TAG_TOKEN, tag_name => 'form',
6093                             attributes => $form_attrs,
6094                             line => $token->{line}, column => $token->{column}},
6095                            {type => START_TAG_TOKEN, tag_name => 'hr',
6096                             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) {
6103                !!!cp ('t390');
6104                push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value},
6105                               #line => $token->{line}, column => $token->{column},
6106                              };
6107              } else {
6108                !!!cp ('t391');
6109                push @tokens, {type => CHARACTER_TOKEN,
6110                               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
6114              }
6115              push @tokens,
6116                            {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at,
6117                             line => $token->{line}, column => $token->{column}},
6118                            #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
6119                            {type => END_TAG_TOKEN, tag_name => 'label',
6120                             line => $token->{line}, column => $token->{column}},
6121                            {type => END_TAG_TOKEN, tag_name => 'p',
6122                             line => $token->{line}, column => $token->{column}},
6123                            {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);
6129            !!!next-token;            !!!next-token;
6130            redo B;            redo B;
6131          }          }
6132          } elsif ($token->{tag_name} eq 'textarea') {
6133            my $tag_name = $token->{tag_name};
6134            my $el;
6135            !!!create-element ($el, $token->{tag_name}, $token->{attributes}, $token);
6136            
6137            ## TODO: $self->{form_element} if defined
6138            $self->{content_model} = RCDATA_CONTENT_MODEL;
6139            delete $self->{escape}; # MUST
6140            
6141            $insert->($el);
6142            
6143            my $text = '';
6144            !!!nack ('t392.1');
6145            !!!next-token;
6146            if ($token->{type} == CHARACTER_TOKEN) {
6147              $token->{data} =~ s/^\x0A//;
6148              unless (length $token->{data}) {
6149                !!!cp ('t392');
6150                !!!next-token;
6151              } else {
6152                !!!cp ('t393');
6153              }
6154            } else {
6155              !!!cp ('t394');
6156            }
6157            while ($token->{type} == CHARACTER_TOKEN) {
6158              !!!cp ('t395');
6159              $text .= $token->{data};
6160              !!!next-token;
6161            }
6162            if (length $text) {
6163              !!!cp ('t396');
6164              $el->manakai_append_text ($text);
6165            }
6166            
6167            $self->{content_model} = PCDATA_CONTENT_MODEL;
6168            
6169            if ($token->{type} == END_TAG_TOKEN and
6170                $token->{tag_name} eq $tag_name) {
6171              !!!cp ('t397');
6172              ## Ignore the token
6173            } else {
6174              !!!cp ('t398');
6175              !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);
6176            }
6177            !!!next-token;
6178            redo B;
6179          } elsif ({
6180                    caption => 1, col => 1, colgroup => 1, frame => 1,
6181                    frameset => 1, head => 1, option => 1, optgroup => 1,
6182                    tbody => 1, td => 1, tfoot => 1, th => 1,
6183                    thead => 1, tr => 1,
6184                   }->{$token->{tag_name}}) {
6185            !!!cp ('t401');
6186            !!!parse-error (type => 'in body:'.$token->{tag_name}, token => $token);
6187            ## Ignore the token
6188            !!!nack ('t401.1'); ## NOTE: |<col/>| or |<frame/>| here is an error.
6189            !!!next-token;
6190            redo B;
6191            
6192            ## ISSUE: An issue on HTML5 new elements in the spec.
6193        } else {        } else {
6194          die "$0: $token->{type}: Unknown token type";          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);
6204            
6205            !!!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;
6252            redo B;
6253        }        }
6254        } elsif ($token->{type} == END_TAG_TOKEN) {
6255          if ($token->{tag_name} eq 'body') {
6256            ## has a |body| element in scope
6257            my $i;
6258            INSCOPE: {
6259              for (reverse @{$self->{open_elements}}) {
6260                if ($_->[1] & BODY_EL) {
6261                  !!!cp ('t405');
6262                  $i = $_;
6263                  last INSCOPE;
6264                } elsif ($_->[1] & SCOPING_EL) {
6265                  !!!cp ('t405.1');
6266                  last;
6267                }
6268              }
6269    
6270        ## ISSUE: An issue in spec here            !!!parse-error (type => 'start tag not allowed',
6271      } elsif ($self->{insertion_mode} eq 'trailing end') {                            value => $token->{tag_name}, token => $token);
6272        ## states in the main stage is preserved yet # MUST            ## NOTE: Ignore the token.
6273                    !!!next-token;
6274        if ($token->{type} eq 'character') {            redo B;
6275          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          } # INSCOPE
6276            my $data = $1;  
6277            ## As if in the main phase.          for (@{$self->{open_elements}}) {
6278            ## NOTE: The insertion mode in the main phase            unless ($_->[1] & ALL_END_TAG_OPTIONAL_EL) {
6279            ## just before the phase has been changed to the trailing              !!!cp ('t403');
6280            ## end phase is either "after body" or "after frameset".              !!!parse-error (type => 'not closed',
6281            $reconstruct_active_formatting_elements->($insert_to_current);                              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') {
6293            ## 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.
6298              unless ($self->{open_elements}->[-1]->[1] & BODY_EL) {
6299                !!!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;
6308              ## reprocess
6309              redo B;
6310            } else {
6311              !!!cp ('t408');
6312              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6313              ## Ignore the token
6314              !!!next-token;
6315              redo B;
6316            }
6317          } elsif ({
6318                    address => 1, blockquote => 1, center => 1, dir => 1,
6319                    div => 1, dl => 1, fieldset => 1, listing => 1,
6320                    menu => 1, ol => 1, pre => 1, ul => 1,
6321                    dd => 1, dt => 1, li => 1,
6322                    applet => 1, button => 1, marquee => 1, object => 1,
6323                   }->{$token->{tag_name}}) {
6324            ## has an element in scope
6325            my $i;
6326            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6327              my $node = $self->{open_elements}->[$_];
6328              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6329                !!!cp ('t410');
6330                $i = $_;
6331                last INSCOPE;
6332              } elsif ($node->[1] & SCOPING_EL) {
6333                !!!cp ('t411');
6334                last INSCOPE;
6335              }
6336            } # INSCOPE
6337    
6338            unless (defined $i) { # has an element in scope
6339              !!!cp ('t413');
6340              !!!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 {
6362                !!!cp ('t414');
6363              }
6364    
6365              ## Step 3.
6366              splice @{$self->{open_elements}}, $i;
6367    
6368              ## Step 4.
6369              $clear_up_to_marker->()
6370                  if {
6371                    applet => 1, button => 1, marquee => 1, object => 1,
6372                  }->{$token->{tag_name}};
6373            }
6374            !!!next-token;
6375            redo B;
6376          } elsif ($token->{tag_name} eq 'form') {
6377            undef $self->{form_element};
6378    
6379            ## has an element in scope
6380            my $i;
6381            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6382              my $node = $self->{open_elements}->[$_];
6383              if ($node->[1] & FORM_EL) {
6384                !!!cp ('t418');
6385                $i = $_;
6386                last INSCOPE;
6387              } elsif ($node->[1] & SCOPING_EL) {
6388                !!!cp ('t419');
6389                last INSCOPE;
6390              }
6391            } # INSCOPE
6392    
6393            unless (defined $i) { # has an element in scope
6394              !!!cp ('t421');
6395              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6396            } 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            $self->{open_elements}->[-1]->[0]->manakai_append_text ($data);            ## 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            unless (length $token->{data}) {            ## Step 3.
6416              !!!next-token;            splice @{$self->{open_elements}}, $i;
6417              redo B;          }
6418    
6419            !!!next-token;
6420            redo B;
6421          } elsif ({
6422                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
6423                   }->{$token->{tag_name}}) {
6424            ## has an element in scope
6425            my $i;
6426            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6427              my $node = $self->{open_elements}->[$_];
6428              if ($node->[1] & HEADING_EL) {
6429                !!!cp ('t423');
6430                $i = $_;
6431                last INSCOPE;
6432              } elsif ($node->[1] & SCOPING_EL) {
6433                !!!cp ('t424');
6434                last INSCOPE;
6435              }
6436            } # INSCOPE
6437    
6438            unless (defined $i) { # has an element in scope
6439              !!!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            !!!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          !!!parse-error (type => 'after html:#character');          if (defined $i) {
6479          $self->{insertion_mode} = $previous_insertion_mode;            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6480          ## reprocess                    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;
6504          redo B;          redo B;
6505        } elsif ($token->{type} eq 'start tag') {        } elsif ({
6506          !!!parse-error (type => 'after html:'.$token->{tag_name});                  a => 1,
6507          $self->{insertion_mode} = $previous_insertion_mode;                  b => 1, big => 1, em => 1, font => 1, i => 1,
6508          ## reprocess                  nobr => 1, s => 1, small => 1, strile => 1,
6509                    strong => 1, tt => 1, u => 1,
6510                   }->{$token->{tag_name}}) {
6511            !!!cp ('t427');
6512            $formatting_end_tag->($token);
6513          redo B;          redo B;
6514        } elsif ($token->{type} eq 'end tag') {        } elsif ($token->{tag_name} eq 'br') {
6515          !!!parse-error (type => 'after html:/'.$token->{tag_name});          !!!cp ('t428');
6516          $self->{insertion_mode} = $previous_insertion_mode;          !!!parse-error (type => 'unmatched end tag:br', token => $token);
6517          ## reprocess  
6518            ## As if <br>
6519            $reconstruct_active_formatting_elements->($insert_to_current);
6520            
6521            my $el;
6522            !!!create-element ($el, 'br',, $token);
6523            $insert->($el);
6524            
6525            ## Ignore the token.
6526            !!!next-token;
6527          redo B;          redo B;
6528          } elsif ({
6529                    caption => 1, col => 1, colgroup => 1, frame => 1,
6530                    frameset => 1, head => 1, option => 1, optgroup => 1,
6531                    tbody => 1, td => 1, tfoot => 1, th => 1,
6532                    thead => 1, tr => 1,
6533                    area => 1, basefont => 1, bgsound => 1,
6534                    embed => 1, hr => 1, iframe => 1, image => 1,
6535                    img => 1, input => 1, isindex => 1, noembed => 1,
6536                    noframes => 1, param => 1, select => 1, spacer => 1,
6537                    table => 1, textarea => 1, wbr => 1,
6538                    noscript => 0, ## TODO: if scripting is enabled
6539                   }->{$token->{tag_name}}) {
6540            !!!cp ('t429');
6541            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6542            ## Ignore the token
6543            !!!next-token;
6544            redo B;
6545            
6546            ## ISSUE: Issue on HTML5 new elements in spec
6547            
6548        } else {        } else {
6549          die "$0: $token->{type}: Unknown token";          ## Step 1
6550            my $node_i = -1;
6551            my $node = $self->{open_elements}->[$node_i];
6552    
6553            ## Step 2
6554            S2: {
6555              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6556                ## Step 1
6557                ## generate implied end tags
6558                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
6559                  !!!cp ('t430');
6560                  ## ISSUE: Can this case be reached?
6561                  pop @{$self->{open_elements}};
6562                }
6563            
6564                ## Step 2
6565                if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6566                        ne $token->{tag_name}) {
6567                  !!!cp ('t431');
6568                  ## NOTE: <x><y></x>
6569                  !!!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
6578                splice @{$self->{open_elements}}, $node_i;
6579    
6580                !!!next-token;
6581                last S2;
6582              } else {
6583                ## Step 3
6584                if (not ($node->[1] & FORMATTING_EL) and
6585                    #not $phrasing_category->{$node->[1]} and
6586                    ($node->[1] & SPECIAL_EL or
6587                     $node->[1] & SCOPING_EL)) {
6588                  !!!cp ('t433');
6589                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6590                  ## Ignore the token
6591                  !!!next-token;
6592                  last S2;
6593                }
6594    
6595                !!!cp ('t434');
6596              }
6597              
6598              ## Step 4
6599              $node_i--;
6600              $node = $self->{open_elements}->[$node_i];
6601              
6602              ## Step 5;
6603              redo S2;
6604            } # S2
6605            redo B;
6606        }        }
     } else {  
       die "$0: $self->{insertion_mode}: Unknown insertion mode";  
6607      }      }
6608        redo B;
6609    } # B    } # B
6610    
6611    ## Stop parsing # MUST    ## Stop parsing # MUST
# Line 5213  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 5241  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 5302  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 5334  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 5358  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 5365  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.42  
changed lines
  Added in v.1.125

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24