/[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.44 by wakaba, Sat Jul 21 07:34:32 2007 UTC revision 1.123 by wakaba, Sun Apr 6 10:32:00 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    sub BODY_EL () { 0b100 }
18    sub BUTTON_EL () { 0b1000 }
19    sub CAPTION_EL () { 0b10000 }
20    sub DD_EL () { 0b100000 }
21    sub DIV_EL () { 0b1000000 }
22    sub DT_EL () { 0b10000000 }
23    sub FORM_EL () { 0b100000000 }
24    sub FORMATTING_EL () { 0b1000000000 }
25    sub FRAMESET_EL () { 0b10000000000 }
26    sub HEADING_EL () { 0b100000000000 }
27    sub HTML_EL () { 0b1000000000000 }
28    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 $permitted_slash_tag_name = {  my $permitted_slash_tag_name = {
189    base => 1,    base => 1,
# Line 19  my $permitted_slash_tag_name = { Line 191  my $permitted_slash_tag_name = {
191    meta => 1,    meta => 1,
192    hr => 1,    hr => 1,
193    br => 1,    br => 1,
194    img=> 1,    img => 1,
195    embed => 1,    embed => 1,
196    param => 1,    param => 1,
197    area => 1,    area => 1,
# Line 75  my $special_category = { Line 247  my $special_category = {
247    textarea => 1, tfoot => 1, thead => 1, title => 1, tr => 1, ul => 1, wbr => 1,    textarea => 1, tfoot => 1, thead => 1, title => 1, tr => 1, ul => 1, wbr => 1,
248  };  };
249  my $scoping_category = {  my $scoping_category = {
250    button => 1, caption => 1, html => 1, marquee => 1, object => 1,    applet => 1, button => 1, caption => 1, html => 1, marquee => 1, object => 1,
251    table => 1, td => 1, th => 1,    table => 1, td => 1, th => 1,
252  };  };
253  my $formatting_category = {  my $formatting_category = {
# Line 84  my $formatting_category = { Line 256  my $formatting_category = {
256  };  };
257  # $phrasing_category: all other elements  # $phrasing_category: all other elements
258    
259    sub parse_byte_string ($$$$;$) {
260      my $self = ref $_[0] ? shift : shift->new;
261      my $charset = shift;
262      my $bytes_s = ref $_[0] ? $_[0] : \($_[0]);
263      my $s;
264      
265      if (defined $charset) {
266        require Encode; ## TODO: decode(utf8) don't delete BOM
267        $s = \ (Encode::decode ($charset, $$bytes_s));
268        $self->{input_encoding} = lc $charset; ## TODO: normalize name
269        $self->{confident} = 1;
270      } else {
271        ## TODO: Implement HTML5 detection algorithm
272        require Whatpm::Charset::UniversalCharDet;
273        $charset = Whatpm::Charset::UniversalCharDet->detect_byte_string
274            (substr ($$bytes_s, 0, 1024));
275        $charset ||= 'windows-1252';
276        $s = \ (Encode::decode ($charset, $$bytes_s));
277        $self->{input_encoding} = $charset;
278        $self->{confident} = 0;
279      }
280    
281      $self->{change_encoding} = sub {
282        my $self = shift;
283        my $charset = lc shift;
284        my $token = shift;
285        ## TODO: if $charset is supported
286        ## TODO: normalize charset name
287    
288        ## "Change the encoding" algorithm:
289    
290        ## Step 1    
291        if ($charset eq 'utf-16') { ## ISSUE: UTF-16BE -> UTF-8? UTF-16LE -> UTF-8?
292          $charset = 'utf-8';
293        }
294    
295        ## Step 2
296        if (defined $self->{input_encoding} and
297            $self->{input_encoding} eq $charset) {
298          $self->{confident} = 1;
299          return;
300        }
301    
302        !!!parse-error (type => 'charset label detected:'.$self->{input_encoding}.
303            ':'.$charset, level => 'w', token => $token);
304    
305        ## Step 3
306        # if (can) {
307          ## change the encoding on the fly.
308          #$self->{confident} = 1;
309          #return;
310        # }
311    
312        ## Step 4
313        throw Whatpm::HTML::RestartParser (charset => $charset);
314      }; # $self->{change_encoding}
315    
316      my @args = @_; shift @args; # $s
317      my $return;
318      try {
319        $return = $self->parse_char_string ($s, @args);  
320      } catch Whatpm::HTML::RestartParser with {
321        my $charset = shift->{charset};
322        $s = \ (Encode::decode ($charset, $$bytes_s));    
323        $self->{input_encoding} = $charset; ## TODO: normalize
324        $self->{confident} = 1;
325        $return = $self->parse_char_string ($s, @args);
326      };
327      return $return;
328    } # parse_byte_string
329    
330    ## NOTE: HTML5 spec says that the encoding layer MUST NOT strip BOM
331    ## and the HTML layer MUST ignore it.  However, we does strip BOM in
332    ## the encoding layer and the HTML layer does not ignore any U+FEFF,
333    ## because the core part of our HTML parser expects a string of character,
334    ## not a string of bytes or code units or anything which might contain a BOM.
335    ## Therefore, any parser interface that accepts a string of bytes,
336    ## such as |parse_byte_string| in this module, must ensure that it does
337    ## strip the BOM and never strip any ZWNBSP.
338    
339    *parse_char_string = \&parse_string;
340    
341  sub parse_string ($$$;$) {  sub parse_string ($$$;$) {
342    my $self = shift->new;    my $self = ref $_[0] ? shift : shift->new;
343    my $s = \$_[0];    my $s = ref $_[0] ? $_[0] : \($_[0]);
344    $self->{document} = $_[1];    $self->{document} = $_[1];
345      @{$self->{document}->child_nodes} = ();
346    
347    ## NOTE: |set_inner_html| copies most of this method's code    ## NOTE: |set_inner_html| copies most of this method's code
348    
349      $self->{confident} = 1 unless exists $self->{confident};
350      $self->{document}->input_encoding ($self->{input_encoding})
351          if defined $self->{input_encoding};
352    
353    my $i = 0;    my $i = 0;
354    my $line = 1;    $self->{line_prev} = $self->{line} = 1;
355    my $column = 0;    $self->{column_prev} = $self->{column} = 0;
356    $self->{set_next_input_character} = sub {    $self->{set_next_char} = sub {
357      my $self = shift;      my $self = shift;
358    
359      pop @{$self->{prev_input_character}};      pop @{$self->{prev_char}};
360      unshift @{$self->{prev_input_character}}, $self->{next_input_character};      unshift @{$self->{prev_char}}, $self->{next_char};
361    
362      $self->{next_input_character} = -1 and return if $i >= length $$s;      $self->{next_char} = -1 and return if $i >= length $$s;
363      $self->{next_input_character} = ord substr $$s, $i++, 1;      $self->{next_char} = ord substr $$s, $i++, 1;
364      $column++;  
365        ($self->{line_prev}, $self->{column_prev})
366            = ($self->{line}, $self->{column});
367        $self->{column}++;
368            
369      if ($self->{next_input_character} == 0x000A) { # LF      if ($self->{next_char} == 0x000A) { # LF
370        $line++;        $self->{line}++;
371        $column = 0;        $self->{column} = 0;
372      } elsif ($self->{next_input_character} == 0x000D) { # CR      } elsif ($self->{next_char} == 0x000D) { # CR
373        $i++ if substr ($$s, $i, 1) eq "\x0A";        $i++ if substr ($$s, $i, 1) eq "\x0A";
374        $self->{next_input_character} = 0x000A; # LF # MUST        $self->{next_char} = 0x000A; # LF # MUST
375        $line++;        $self->{line}++;
376        $column = 0;        $self->{column} = 0;
377      } elsif ($self->{next_input_character} > 0x10FFFF) {      } elsif ($self->{next_char} > 0x10FFFF) {
378        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
379      } elsif ($self->{next_input_character} == 0x0000) { # NULL      } elsif ($self->{next_char} == 0x0000) { # NULL
380        !!!parse-error (type => 'NULL');        !!!parse-error (type => 'NULL');
381        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
382      }      }
383    };    };
384    $self->{prev_input_character} = [-1, -1, -1];    $self->{prev_char} = [-1, -1, -1];
385    $self->{next_input_character} = -1;    $self->{next_char} = -1;
386    
387    my $onerror = $_[2] || sub {    my $onerror = $_[2] || sub {
388      my (%opt) = @_;      my (%opt) = @_;
389      warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";      my $line = $opt{token} ? $opt{token}->{line} : $opt{line};
390        my $column = $opt{token} ? $opt{token}->{column} : $opt{column};
391        warn "Parse error ($opt{type}) at line $line column $column\n";
392    };    };
393    $self->{parse_error} = sub {    $self->{parse_error} = sub {
394      $onerror->(@_, line => $line, column => $column);      $onerror->(line => $self->{line}, column => $self->{column}, @_);
395    };    };
396    
397    $self->_initialize_tokenizer;    $self->_initialize_tokenizer;
# Line 135  sub parse_string ($$$;$) { Line 399  sub parse_string ($$$;$) {
399    $self->_construct_tree;    $self->_construct_tree;
400    $self->_terminate_tree_constructor;    $self->_terminate_tree_constructor;
401    
402      delete $self->{parse_error}; # remove loop
403    
404    return $self->{document};    return $self->{document};
405  } # parse_string  } # parse_string
406    
407  sub new ($) {  sub new ($) {
408    my $class = shift;    my $class = shift;
409    my $self = bless {}, $class;    my $self = bless {}, $class;
410    $self->{set_next_input_character} = sub {    $self->{set_next_char} = sub {
411      $self->{next_input_character} = -1;      $self->{next_char} = -1;
412    };    };
413    $self->{parse_error} = sub {    $self->{parse_error} = sub {
414      #      #
415    };    };
416      $self->{change_encoding} = sub {
417        # if ($_[0] is a supported encoding) {
418        #   run "change the encoding" algorithm;
419        #   throw Whatpm::HTML::RestartParser (charset => $new_encoding);
420        # }
421      };
422      $self->{application_cache_selection} = sub {
423        #
424      };
425    return $self;    return $self;
426  } # new  } # new
427    
# Line 159  sub CDATA_CONTENT_MODEL () { CM_LIMITED_ Line 434  sub CDATA_CONTENT_MODEL () { CM_LIMITED_
434  sub RCDATA_CONTENT_MODEL () { CM_ENTITY | CM_LIMITED_MARKUP }  sub RCDATA_CONTENT_MODEL () { CM_ENTITY | CM_LIMITED_MARKUP }
435  sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }  sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }
436    
437    sub DATA_STATE () { 0 }
438    sub ENTITY_DATA_STATE () { 1 }
439    sub TAG_OPEN_STATE () { 2 }
440    sub CLOSE_TAG_OPEN_STATE () { 3 }
441    sub TAG_NAME_STATE () { 4 }
442    sub BEFORE_ATTRIBUTE_NAME_STATE () { 5 }
443    sub ATTRIBUTE_NAME_STATE () { 6 }
444    sub AFTER_ATTRIBUTE_NAME_STATE () { 7 }
445    sub BEFORE_ATTRIBUTE_VALUE_STATE () { 8 }
446    sub ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE () { 9 }
447    sub ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE () { 10 }
448    sub ATTRIBUTE_VALUE_UNQUOTED_STATE () { 11 }
449    sub ENTITY_IN_ATTRIBUTE_VALUE_STATE () { 12 }
450    sub MARKUP_DECLARATION_OPEN_STATE () { 13 }
451    sub COMMENT_START_STATE () { 14 }
452    sub COMMENT_START_DASH_STATE () { 15 }
453    sub COMMENT_STATE () { 16 }
454    sub COMMENT_END_STATE () { 17 }
455    sub COMMENT_END_DASH_STATE () { 18 }
456    sub BOGUS_COMMENT_STATE () { 19 }
457    sub DOCTYPE_STATE () { 20 }
458    sub BEFORE_DOCTYPE_NAME_STATE () { 21 }
459    sub DOCTYPE_NAME_STATE () { 22 }
460    sub AFTER_DOCTYPE_NAME_STATE () { 23 }
461    sub BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE () { 24 }
462    sub DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE () { 25 }
463    sub DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE () { 26 }
464    sub AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE () { 27 }
465    sub BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 28 }
466    sub DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE () { 29 }
467    sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE () { 30 }
468    sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }
469    sub BOGUS_DOCTYPE_STATE () { 32 }
470    sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }
471    
472    sub DOCTYPE_TOKEN () { 1 }
473    sub COMMENT_TOKEN () { 2 }
474    sub START_TAG_TOKEN () { 3 }
475    sub END_TAG_TOKEN () { 4 }
476    sub END_OF_FILE_TOKEN () { 5 }
477    sub CHARACTER_TOKEN () { 6 }
478    
479    sub AFTER_HTML_IMS () { 0b100 }
480    sub HEAD_IMS ()       { 0b1000 }
481    sub BODY_IMS ()       { 0b10000 }
482    sub BODY_TABLE_IMS () { 0b100000 }
483    sub TABLE_IMS ()      { 0b1000000 }
484    sub ROW_IMS ()        { 0b10000000 }
485    sub BODY_AFTER_IMS () { 0b100000000 }
486    sub FRAME_IMS ()      { 0b1000000000 }
487    sub SELECT_IMS ()     { 0b10000000000 }
488    
489    ## NOTE: "initial" and "before html" insertion modes have no constants.
490    
491    ## NOTE: "after after body" insertion mode.
492    sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }
493    
494    ## NOTE: "after after frameset" insertion mode.
495    sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }
496    
497    sub IN_HEAD_IM () { HEAD_IMS | 0b00 }
498    sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }
499    sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }
500    sub BEFORE_HEAD_IM () { HEAD_IMS | 0b11 }
501    sub IN_BODY_IM () { BODY_IMS }
502    sub IN_CELL_IM () { BODY_IMS | BODY_TABLE_IMS | 0b01 }
503    sub IN_CAPTION_IM () { BODY_IMS | BODY_TABLE_IMS | 0b10 }
504    sub IN_ROW_IM () { TABLE_IMS | ROW_IMS | 0b01 }
505    sub IN_TABLE_BODY_IM () { TABLE_IMS | ROW_IMS | 0b10 }
506    sub IN_TABLE_IM () { TABLE_IMS }
507    sub AFTER_BODY_IM () { BODY_AFTER_IMS }
508    sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }
509    sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }
510    sub IN_SELECT_IM () { SELECT_IMS | 0b01 }
511    sub IN_SELECT_IN_TABLE_IM () { SELECT_IMS | 0b10 }
512    sub IN_COLUMN_GROUP_IM () { 0b10 }
513    
514  ## Implementations MUST act as if state machine in the spec  ## Implementations MUST act as if state machine in the spec
515    
516  sub _initialize_tokenizer ($) {  sub _initialize_tokenizer ($) {
517    my $self = shift;    my $self = shift;
518    $self->{state} = 'data'; # MUST    $self->{state} = DATA_STATE; # MUST
519    $self->{content_model} = PCDATA_CONTENT_MODEL; # be    $self->{content_model} = PCDATA_CONTENT_MODEL; # be
520    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE
521    undef $self->{current_attribute};    undef $self->{current_attribute};
522    undef $self->{last_emitted_start_tag_name};    undef $self->{last_emitted_start_tag_name};
523    undef $self->{last_attribute_value_state};    undef $self->{last_attribute_value_state};
524    $self->{char} = [];    $self->{char} = [];
525    # $self->{next_input_character}    # $self->{next_char}
526    !!!next-input-character;    !!!next-input-character;
527    $self->{token} = [];    $self->{token} = [];
528    # $self->{escape}    # $self->{escape}
529  } # _initialize_tokenizer  } # _initialize_tokenizer
530    
531  ## A token has:  ## A token has:
532  ##   ->{type} eq 'DOCTYPE', 'start tag', 'end tag', 'comment',  ##   ->{type} == DOCTYPE_TOKEN, START_TAG_TOKEN, END_TAG_TOKEN, COMMENT_TOKEN,
533  ##       'character', or 'end-of-file'  ##       CHARACTER_TOKEN, or END_OF_FILE_TOKEN
534  ##   ->{name} (DOCTYPE, start tag (tag name), end tag (tag name))  ##   ->{name} (DOCTYPE_TOKEN)
535  ##   ->{public_identifier} (DOCTYPE)  ##   ->{tag_name} (START_TAG_TOKEN, END_TAG_TOKEN)
536  ##   ->{system_identifier} (DOCTYPE)  ##   ->{public_identifier} (DOCTYPE_TOKEN)
537  ##   ->{correct} == 1 or 0 (DOCTYPE)  ##   ->{system_identifier} (DOCTYPE_TOKEN)
538  ##   ->{attributes} isa HASH (start tag, end tag)  ##   ->{quirks} == 1 or 0 (DOCTYPE_TOKEN): "force-quirks" flag
539  ##   ->{data} (comment, character)  ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)
540    ##        ->{name}
541    ##        ->{value}
542    ##        ->{has_reference} == 1 or 0
543    ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
544    
545  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
546    
# Line 194  sub _initialize_tokenizer ($) { Line 550  sub _initialize_tokenizer ($) {
550  ## has completed loading.  If one has, then it MUST be executed  ## has completed loading.  If one has, then it MUST be executed
551  ## and removed from the list.  ## and removed from the list.
552    
553    ## NOTE: HTML5 "Writing HTML documents" section, applied to
554    ## documents and not to user agents and conformance checkers,
555    ## contains some requirements that are not detected by the
556    ## parsing algorithm:
557    ## - Some requirements on character encoding declarations. ## TODO
558    ## - "Elements MUST NOT contain content that their content model disallows."
559    ##   ... Some are parse error, some are not (will be reported by c.c.).
560    ## - Polytheistic slash SHOULD NOT be used. (Applied only to atheists.) ## TODO
561    ## - Text (in elements, attributes, and comments) SHOULD NOT contain
562    ##   control characters other than space characters. ## TODO: (what is control character? C0, C1 and DEL?  Unicode control character?)
563    
564    ## TODO: HTML5 poses authors two SHOULD-level requirements that cannot
565    ## be detected by the HTML5 parsing algorithm:
566    ## - Text,
567    
568  sub _get_next_token ($) {  sub _get_next_token ($) {
569    my $self = shift;    my $self = shift;
570    if (@{$self->{token}}) {    if (@{$self->{token}}) {
# Line 201  sub _get_next_token ($) { Line 572  sub _get_next_token ($) {
572    }    }
573    
574    A: {    A: {
575      if ($self->{state} eq 'data') {      if ($self->{state} == DATA_STATE) {
576        if ($self->{next_input_character} == 0x0026) { # &        if ($self->{next_char} == 0x0026) { # &
577          if ($self->{content_model} & CM_ENTITY) { # PCDATA | RCDATA          if ($self->{content_model} & CM_ENTITY and # PCDATA | RCDATA
578            $self->{state} = 'entity data';              not $self->{escape}) {
579              !!!cp (1);
580              $self->{state} = ENTITY_DATA_STATE;
581            !!!next-input-character;            !!!next-input-character;
582            redo A;            redo A;
583          } else {          } else {
584              !!!cp (2);
585            #            #
586          }          }
587        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
588          if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA          if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
589            unless ($self->{escape}) {            unless ($self->{escape}) {
590              if ($self->{prev_input_character}->[0] == 0x002D and # -              if ($self->{prev_char}->[0] == 0x002D and # -
591                  $self->{prev_input_character}->[1] == 0x0021 and # !                  $self->{prev_char}->[1] == 0x0021 and # !
592                  $self->{prev_input_character}->[2] == 0x003C) { # <                  $self->{prev_char}->[2] == 0x003C) { # <
593                  !!!cp (3);
594                $self->{escape} = 1;                $self->{escape} = 1;
595                } else {
596                  !!!cp (4);
597              }              }
598              } else {
599                !!!cp (5);
600            }            }
601          }          }
602                    
603          #          #
604        } elsif ($self->{next_input_character} == 0x003C) { # <        } elsif ($self->{next_char} == 0x003C) { # <
605          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA
606              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA
607               not $self->{escape})) {               not $self->{escape})) {
608            $self->{state} = 'tag open';            !!!cp (6);
609              $self->{state} = TAG_OPEN_STATE;
610            !!!next-input-character;            !!!next-input-character;
611            redo A;            redo A;
612          } else {          } else {
613              !!!cp (7);
614            #            #
615          }          }
616        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
617          if ($self->{escape} and          if ($self->{escape} and
618              ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA              ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA
619            if ($self->{prev_input_character}->[0] == 0x002D and # -            if ($self->{prev_char}->[0] == 0x002D and # -
620                $self->{prev_input_character}->[1] == 0x002D) { # -                $self->{prev_char}->[1] == 0x002D) { # -
621                !!!cp (8);
622              delete $self->{escape};              delete $self->{escape};
623              } else {
624                !!!cp (9);
625            }            }
626            } else {
627              !!!cp (10);
628          }          }
629                    
630          #          #
631        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
632          !!!emit ({type => 'end-of-file'});          !!!cp (11);
633            !!!emit ({type => END_OF_FILE_TOKEN,
634                      line => $self->{line}, column => $self->{column}});
635          last A; ## TODO: ok?          last A; ## TODO: ok?
636          } else {
637            !!!cp (12);
638        }        }
639        # Anything else        # Anything else
640        my $token = {type => 'character',        my $token = {type => CHARACTER_TOKEN,
641                     data => chr $self->{next_input_character}};                     data => chr $self->{next_char},
642                       line => $self->{line}, column => $self->{column},
643                      };
644        ## Stay in the data state        ## Stay in the data state
645        !!!next-input-character;        !!!next-input-character;
646    
647        !!!emit ($token);        !!!emit ($token);
648    
649        redo A;        redo A;
650      } elsif ($self->{state} eq 'entity data') {      } elsif ($self->{state} == ENTITY_DATA_STATE) {
651        ## (cannot happen in CDATA state)        ## (cannot happen in CDATA state)
652    
653          my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
654                
655        my $token = $self->_tokenize_attempt_to_consume_an_entity (0);        my $token = $self->_tokenize_attempt_to_consume_an_entity (0, -1);
656    
657        $self->{state} = 'data';        $self->{state} = DATA_STATE;
658        # next-input-character is already done        # next-input-character is already done
659    
660        unless (defined $token) {        unless (defined $token) {
661          !!!emit ({type => 'character', data => '&'});          !!!cp (13);
662            !!!emit ({type => CHARACTER_TOKEN, data => '&',
663                      line => $l, column => $c,
664                     });
665        } else {        } else {
666            !!!cp (14);
667          !!!emit ($token);          !!!emit ($token);
668        }        }
669    
670        redo A;        redo A;
671      } elsif ($self->{state} eq 'tag open') {      } elsif ($self->{state} == TAG_OPEN_STATE) {
672        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
673          if ($self->{next_input_character} == 0x002F) { # /          if ($self->{next_char} == 0x002F) { # /
674              !!!cp (15);
675            !!!next-input-character;            !!!next-input-character;
676            $self->{state} = 'close tag open';            $self->{state} = CLOSE_TAG_OPEN_STATE;
677            redo A;            redo A;
678          } else {          } else {
679              !!!cp (16);
680            ## reconsume            ## reconsume
681            $self->{state} = 'data';            $self->{state} = DATA_STATE;
682    
683            !!!emit ({type => 'character', data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
684                        line => $self->{line_prev},
685                        column => $self->{column_prev},
686                       });
687    
688            redo A;            redo A;
689          }          }
690        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA
691          if ($self->{next_input_character} == 0x0021) { # !          if ($self->{next_char} == 0x0021) { # !
692            $self->{state} = 'markup declaration open';            !!!cp (17);
693              $self->{state} = MARKUP_DECLARATION_OPEN_STATE;
694            !!!next-input-character;            !!!next-input-character;
695            redo A;            redo A;
696          } elsif ($self->{next_input_character} == 0x002F) { # /          } elsif ($self->{next_char} == 0x002F) { # /
697            $self->{state} = 'close tag open';            !!!cp (18);
698              $self->{state} = CLOSE_TAG_OPEN_STATE;
699            !!!next-input-character;            !!!next-input-character;
700            redo A;            redo A;
701          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_char} and
702                   $self->{next_input_character} <= 0x005A) { # A..Z                   $self->{next_char} <= 0x005A) { # A..Z
703              !!!cp (19);
704            $self->{current_token}            $self->{current_token}
705              = {type => 'start tag',              = {type => START_TAG_TOKEN,
706                 tag_name => chr ($self->{next_input_character} + 0x0020)};                 tag_name => chr ($self->{next_char} + 0x0020),
707            $self->{state} = 'tag name';                 line => $self->{line_prev},
708                   column => $self->{column_prev}};
709              $self->{state} = TAG_NAME_STATE;
710            !!!next-input-character;            !!!next-input-character;
711            redo A;            redo A;
712          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_char} and
713                   $self->{next_input_character} <= 0x007A) { # a..z                   $self->{next_char} <= 0x007A) { # a..z
714            $self->{current_token} = {type => 'start tag',            !!!cp (20);
715                              tag_name => chr ($self->{next_input_character})};            $self->{current_token} = {type => START_TAG_TOKEN,
716            $self->{state} = 'tag name';                                      tag_name => chr ($self->{next_char}),
717                                        line => $self->{line_prev},
718                                        column => $self->{column_prev}};
719              $self->{state} = TAG_NAME_STATE;
720            !!!next-input-character;            !!!next-input-character;
721            redo A;            redo A;
722          } elsif ($self->{next_input_character} == 0x003E) { # >          } elsif ($self->{next_char} == 0x003E) { # >
723            !!!parse-error (type => 'empty start tag');            !!!cp (21);
724            $self->{state} = 'data';            !!!parse-error (type => 'empty start tag',
725                              line => $self->{line_prev},
726                              column => $self->{column_prev});
727              $self->{state} = DATA_STATE;
728            !!!next-input-character;            !!!next-input-character;
729    
730            !!!emit ({type => 'character', data => '<>'});            !!!emit ({type => CHARACTER_TOKEN, data => '<>',
731                        line => $self->{line_prev},
732                        column => $self->{column_prev},
733                       });
734    
735            redo A;            redo A;
736          } elsif ($self->{next_input_character} == 0x003F) { # ?          } elsif ($self->{next_char} == 0x003F) { # ?
737            !!!parse-error (type => 'pio');            !!!cp (22);
738            $self->{state} = 'bogus comment';            !!!parse-error (type => 'pio',
739            ## $self->{next_input_character} is intentionally left as is                            line => $self->{line_prev},
740                              column => $self->{column_prev});
741              $self->{state} = BOGUS_COMMENT_STATE;
742              $self->{current_token} = {type => COMMENT_TOKEN, data => '',
743                                        line => $self->{line_prev},
744                                        column => $self->{column_prev},
745                                       };
746              ## $self->{next_char} is intentionally left as is
747            redo A;            redo A;
748          } else {          } else {
749              !!!cp (23);
750            !!!parse-error (type => 'bare stago');            !!!parse-error (type => 'bare stago');
751            $self->{state} = 'data';            $self->{state} = DATA_STATE;
752            ## reconsume            ## reconsume
753    
754            !!!emit ({type => 'character', data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
755                        line => $self->{line_prev},
756                        column => $self->{column_prev},
757                       });
758    
759            redo A;            redo A;
760          }          }
761        } else {        } else {
762          die "$0: $self->{content_model} in tag open";          die "$0: $self->{content_model} in tag open";
763        }        }
764      } elsif ($self->{state} eq 'close tag open') {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
765          my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1); # "<"of"</"
766        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
767          if (defined $self->{last_emitted_start_tag_name}) {          if (defined $self->{last_emitted_start_tag_name}) {
768    
769            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>
770            my @next_char;            my @next_char;
771            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++) {
772              push @next_char, $self->{next_input_character};              push @next_char, $self->{next_char};
773              my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);              my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);
774              my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;              my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;
775              if ($self->{next_input_character} == $c or $self->{next_input_character} == $C) {              if ($self->{next_char} == $c or $self->{next_char} == $C) {
776                  !!!cp (24);
777                !!!next-input-character;                !!!next-input-character;
778                next TAGNAME;                next TAGNAME;
779              } else {              } else {
780                $self->{next_input_character} = shift @next_char; # reconsume                !!!cp (25);
781                  $self->{next_char} = shift @next_char; # reconsume
782                !!!back-next-input-character (@next_char);                !!!back-next-input-character (@next_char);
783                $self->{state} = 'data';                $self->{state} = DATA_STATE;
784    
785                !!!emit ({type => 'character', data => '</'});                !!!emit ({type => CHARACTER_TOKEN, data => '</',
786                            line => $l, column => $c,
787                           });
788        
789                redo A;                redo A;
790              }              }
791            }            }
792            push @next_char, $self->{next_input_character};            push @next_char, $self->{next_char};
793                
794            unless ($self->{next_input_character} == 0x0009 or # HT            unless ($self->{next_char} == 0x0009 or # HT
795                    $self->{next_input_character} == 0x000A or # LF                    $self->{next_char} == 0x000A or # LF
796                    $self->{next_input_character} == 0x000B or # VT                    $self->{next_char} == 0x000B or # VT
797                    $self->{next_input_character} == 0x000C or # FF                    $self->{next_char} == 0x000C or # FF
798                    $self->{next_input_character} == 0x0020 or # SP                    $self->{next_char} == 0x0020 or # SP
799                    $self->{next_input_character} == 0x003E or # >                    $self->{next_char} == 0x003E or # >
800                    $self->{next_input_character} == 0x002F or # /                    $self->{next_char} == 0x002F or # /
801                    $self->{next_input_character} == -1) {                    $self->{next_char} == -1) {
802              $self->{next_input_character} = shift @next_char; # reconsume              !!!cp (26);
803                $self->{next_char} = shift @next_char; # reconsume
804              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
805              $self->{state} = 'data';              $self->{state} = DATA_STATE;
806              !!!emit ({type => 'character', data => '</'});              !!!emit ({type => CHARACTER_TOKEN, data => '</',
807                          line => $l, column => $c,
808                         });
809              redo A;              redo A;
810            } else {            } else {
811              $self->{next_input_character} = shift @next_char;              !!!cp (27);
812                $self->{next_char} = shift @next_char;
813              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
814              # and consume...              # and consume...
815            }            }
816          } else {          } else {
817            ## No start tag token has ever been emitted            ## No start tag token has ever been emitted
818              !!!cp (28);
819            # next-input-character is already done            # next-input-character is already done
820            $self->{state} = 'data';            $self->{state} = DATA_STATE;
821            !!!emit ({type => 'character', data => '</'});            !!!emit ({type => CHARACTER_TOKEN, data => '</',
822                        line => $l, column => $c,
823                       });
824            redo A;            redo A;
825          }          }
826        }        }
827                
828        if (0x0041 <= $self->{next_input_character} and        if (0x0041 <= $self->{next_char} and
829            $self->{next_input_character} <= 0x005A) { # A..Z            $self->{next_char} <= 0x005A) { # A..Z
830          $self->{current_token} = {type => 'end tag',          !!!cp (29);
831                            tag_name => chr ($self->{next_input_character} + 0x0020)};          $self->{current_token}
832          $self->{state} = 'tag name';              = {type => END_TAG_TOKEN,
833          !!!next-input-character;                 tag_name => chr ($self->{next_char} + 0x0020),
834          redo A;                 line => $l, column => $c};
835        } elsif (0x0061 <= $self->{next_input_character} and          $self->{state} = TAG_NAME_STATE;
836                 $self->{next_input_character} <= 0x007A) { # a..z          !!!next-input-character;
837          $self->{current_token} = {type => 'end tag',          redo A;
838                            tag_name => chr ($self->{next_input_character})};        } elsif (0x0061 <= $self->{next_char} and
839          $self->{state} = 'tag name';                 $self->{next_char} <= 0x007A) { # a..z
840          !!!next-input-character;          !!!cp (30);
841          redo A;          $self->{current_token} = {type => END_TAG_TOKEN,
842        } elsif ($self->{next_input_character} == 0x003E) { # >                                    tag_name => chr ($self->{next_char}),
843          !!!parse-error (type => 'empty end tag');                                    line => $l, column => $c};
844          $self->{state} = 'data';          $self->{state} = TAG_NAME_STATE;
845            !!!next-input-character;
846            redo A;
847          } elsif ($self->{next_char} == 0x003E) { # >
848            !!!cp (31);
849            !!!parse-error (type => 'empty end tag',
850                            line => $self->{line_prev}, ## "<" in "</>"
851                            column => $self->{column_prev} - 1);
852            $self->{state} = DATA_STATE;
853          !!!next-input-character;          !!!next-input-character;
854          redo A;          redo A;
855        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
856            !!!cp (32);
857          !!!parse-error (type => 'bare etago');          !!!parse-error (type => 'bare etago');
858          $self->{state} = 'data';          $self->{state} = DATA_STATE;
859          # reconsume          # reconsume
860    
861          !!!emit ({type => 'character', data => '</'});          !!!emit ({type => CHARACTER_TOKEN, data => '</',
862                      line => $l, column => $c,
863                     });
864    
865          redo A;          redo A;
866        } else {        } else {
867            !!!cp (33);
868          !!!parse-error (type => 'bogus end tag');          !!!parse-error (type => 'bogus end tag');
869          $self->{state} = 'bogus comment';          $self->{state} = BOGUS_COMMENT_STATE;
870          ## $self->{next_input_character} is intentionally left as is          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
871          redo A;                                    line => $self->{line_prev}, # "<" of "</"
872        }                                    column => $self->{column_prev} - 1,
873      } elsif ($self->{state} eq 'tag name') {                                   };
874        if ($self->{next_input_character} == 0x0009 or # HT          ## $self->{next_char} is intentionally left as is
875            $self->{next_input_character} == 0x000A or # LF          redo A;
876            $self->{next_input_character} == 0x000B or # VT        }
877            $self->{next_input_character} == 0x000C or # FF      } elsif ($self->{state} == TAG_NAME_STATE) {
878            $self->{next_input_character} == 0x0020) { # SP        if ($self->{next_char} == 0x0009 or # HT
879          $self->{state} = 'before attribute name';            $self->{next_char} == 0x000A or # LF
880          !!!next-input-character;            $self->{next_char} == 0x000B or # VT
881          redo A;            $self->{next_char} == 0x000C or # FF
882        } elsif ($self->{next_input_character} == 0x003E) { # >            $self->{next_char} == 0x0020) { # SP
883          if ($self->{current_token}->{type} eq 'start tag') {          !!!cp (34);
884            $self->{current_token}->{first_start_tag}          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
885                = not defined $self->{last_emitted_start_tag_name};          !!!next-input-character;
886            redo A;
887          } elsif ($self->{next_char} == 0x003E) { # >
888            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
889              !!!cp (35);
890            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
891          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
892            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
893            if ($self->{current_token}->{attributes}) {            #if ($self->{current_token}->{attributes}) {
894              !!!parse-error (type => 'end tag attribute');            #  ## NOTE: This should never be reached.
895            }            #  !!! cp (36);
896              #  !!! parse-error (type => 'end tag attribute');
897              #} else {
898                !!!cp (37);
899              #}
900          } else {          } else {
901            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
902          }          }
903          $self->{state} = 'data';          $self->{state} = DATA_STATE;
904          !!!next-input-character;          !!!next-input-character;
905    
906          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
907    
908          redo A;          redo A;
909        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
910                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
911          $self->{current_token}->{tag_name} .= chr ($self->{next_input_character} + 0x0020);          !!!cp (38);
912            $self->{current_token}->{tag_name} .= chr ($self->{next_char} + 0x0020);
913            # start tag or end tag            # start tag or end tag
914          ## Stay in this state          ## Stay in this state
915          !!!next-input-character;          !!!next-input-character;
916          redo A;          redo A;
917        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
918          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
919          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
920            $self->{current_token}->{first_start_tag}            !!!cp (39);
               = not defined $self->{last_emitted_start_tag_name};  
921            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
922          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
923            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
924            if ($self->{current_token}->{attributes}) {            #if ($self->{current_token}->{attributes}) {
925              !!!parse-error (type => 'end tag attribute');            #  ## NOTE: This state should never be reached.
926            }            #  !!! cp (40);
927              #  !!! parse-error (type => 'end tag attribute');
928              #} else {
929                !!!cp (41);
930              #}
931          } else {          } else {
932            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
933          }          }
934          $self->{state} = 'data';          $self->{state} = DATA_STATE;
935          # reconsume          # reconsume
936    
937          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
938    
939          redo A;          redo A;
940        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
941          !!!next-input-character;          !!!next-input-character;
942          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_char} == 0x003E and # >
943              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
944              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
945            # permitted slash            # permitted slash
946              !!!cp (42);
947            #            #
948          } else {          } else {
949              !!!cp (43);
950            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
951          }          }
952          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
953          # next-input-character is already done          # next-input-character is already done
954          redo A;          redo A;
955        } else {        } else {
956          $self->{current_token}->{tag_name} .= chr $self->{next_input_character};          !!!cp (44);
957            $self->{current_token}->{tag_name} .= chr $self->{next_char};
958            # start tag or end tag            # start tag or end tag
959          ## Stay in the state          ## Stay in the state
960          !!!next-input-character;          !!!next-input-character;
961          redo A;          redo A;
962        }        }
963      } elsif ($self->{state} eq 'before attribute name') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_NAME_STATE) {
964        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
965            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
966            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
967            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
968            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
969            !!!cp (45);
970          ## Stay in the state          ## Stay in the state
971          !!!next-input-character;          !!!next-input-character;
972          redo A;          redo A;
973        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
974          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
975            $self->{current_token}->{first_start_tag}            !!!cp (46);
               = not defined $self->{last_emitted_start_tag_name};  
976            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
977          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
978            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
979            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
980                !!!cp (47);
981              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
982              } else {
983                !!!cp (48);
984            }            }
985          } else {          } else {
986            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
987          }          }
988          $self->{state} = 'data';          $self->{state} = DATA_STATE;
989          !!!next-input-character;          !!!next-input-character;
990    
991          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
992    
993          redo A;          redo A;
994        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
995                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
996          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          !!!cp (49);
997                                value => ''};          $self->{current_attribute}
998          $self->{state} = 'attribute name';              = {name => chr ($self->{next_char} + 0x0020),
999                   value => '',
1000                   line => $self->{line}, column => $self->{column}};
1001            $self->{state} = ATTRIBUTE_NAME_STATE;
1002          !!!next-input-character;          !!!next-input-character;
1003          redo A;          redo A;
1004        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1005          !!!next-input-character;          !!!next-input-character;
1006          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_char} == 0x003E and # >
1007              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
1008              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
1009            # permitted slash            # permitted slash
1010              !!!cp (50);
1011            #            #
1012          } else {          } else {
1013              !!!cp (51);
1014            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
1015          }          }
1016          ## Stay in the state          ## Stay in the state
1017          # next-input-character is already done          # next-input-character is already done
1018          redo A;          redo A;
1019        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1020          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1021          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1022            $self->{current_token}->{first_start_tag}            !!!cp (52);
               = not defined $self->{last_emitted_start_tag_name};  
1023            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1024          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1025            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1026            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1027                !!!cp (53);
1028              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1029              } else {
1030                !!!cp (54);
1031            }            }
1032          } else {          } else {
1033            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1034          }          }
1035          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1036          # reconsume          # reconsume
1037    
1038          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1039    
1040          redo A;          redo A;
1041        } else {        } else {
1042          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          if ({
1043                                value => ''};               0x0022 => 1, # "
1044          $self->{state} = 'attribute name';               0x0027 => 1, # '
1045                 0x003D => 1, # =
1046                }->{$self->{next_char}}) {
1047              !!!cp (55);
1048              !!!parse-error (type => 'bad attribute name');
1049            } else {
1050              !!!cp (56);
1051            }
1052            $self->{current_attribute}
1053                = {name => chr ($self->{next_char}),
1054                   value => '',
1055                   line => $self->{line}, column => $self->{column}};
1056            $self->{state} = ATTRIBUTE_NAME_STATE;
1057          !!!next-input-character;          !!!next-input-character;
1058          redo A;          redo A;
1059        }        }
1060      } elsif ($self->{state} eq 'attribute name') {      } elsif ($self->{state} == ATTRIBUTE_NAME_STATE) {
1061        my $before_leave = sub {        my $before_leave = sub {
1062          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
1063              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
1064            !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name});            !!!cp (57);
1065              !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name}, line => $self->{current_attribute}->{line}, column => $self->{current_attribute}->{column});
1066            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
1067          } else {          } else {
1068              !!!cp (58);
1069            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}
1070              = $self->{current_attribute};              = $self->{current_attribute};
1071          }          }
1072        }; # $before_leave        }; # $before_leave
1073    
1074        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1075            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1076            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1077            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1078            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1079            !!!cp (59);
1080          $before_leave->();          $before_leave->();
1081          $self->{state} = 'after attribute name';          $self->{state} = AFTER_ATTRIBUTE_NAME_STATE;
1082          !!!next-input-character;          !!!next-input-character;
1083          redo A;          redo A;
1084        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_char} == 0x003D) { # =
1085            !!!cp (60);
1086          $before_leave->();          $before_leave->();
1087          $self->{state} = 'before attribute value';          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
1088          !!!next-input-character;          !!!next-input-character;
1089          redo A;          redo A;
1090        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1091          $before_leave->();          $before_leave->();
1092          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1093            $self->{current_token}->{first_start_tag}            !!!cp (61);
               = not defined $self->{last_emitted_start_tag_name};  
1094            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1095          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1096              !!!cp (62);
1097            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1098            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1099              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 607  sub _get_next_token ($) { Line 1101  sub _get_next_token ($) {
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          !!!next-input-character;          !!!next-input-character;
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        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1111                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1112          $self->{current_attribute}->{name} .= chr ($self->{next_input_character} + 0x0020);          !!!cp (63);
1113            $self->{current_attribute}->{name} .= chr ($self->{next_char} + 0x0020);
1114          ## Stay in the state          ## Stay in the state
1115          !!!next-input-character;          !!!next-input-character;
1116          redo A;          redo A;
1117        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1118          $before_leave->();          $before_leave->();
1119          !!!next-input-character;          !!!next-input-character;
1120          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_char} == 0x003E and # >
1121              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
1122              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
1123            # permitted slash            # permitted slash
1124              !!!cp (64);
1125            #            #
1126          } else {          } else {
1127              !!!cp (65);
1128            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
1129          }          }
1130          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1131          # next-input-character is already done          # next-input-character is already done
1132          redo A;          redo A;
1133        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1134          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1135          $before_leave->();          $before_leave->();
1136          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1137            $self->{current_token}->{first_start_tag}            !!!cp (66);
               = not defined $self->{last_emitted_start_tag_name};  
1138            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1139          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1140            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1141            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1142                !!!cp (67);
1143              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1144              } else {
1145                ## NOTE: This state should never be reached.
1146                !!!cp (68);
1147            }            }
1148          } else {          } else {
1149            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1150          }          }
1151          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1152          # reconsume          # reconsume
1153    
1154          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1155    
1156          redo A;          redo A;
1157        } else {        } else {
1158          $self->{current_attribute}->{name} .= chr ($self->{next_input_character});          if ($self->{next_char} == 0x0022 or # "
1159                $self->{next_char} == 0x0027) { # '
1160              !!!cp (69);
1161              !!!parse-error (type => 'bad attribute name');
1162            } else {
1163              !!!cp (70);
1164            }
1165            $self->{current_attribute}->{name} .= chr ($self->{next_char});
1166          ## Stay in the state          ## Stay in the state
1167          !!!next-input-character;          !!!next-input-character;
1168          redo A;          redo A;
1169        }        }
1170      } elsif ($self->{state} eq 'after attribute name') {      } elsif ($self->{state} == AFTER_ATTRIBUTE_NAME_STATE) {
1171        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1172            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1173            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1174            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1175            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1176            !!!cp (71);
1177          ## Stay in the state          ## Stay in the state
1178          !!!next-input-character;          !!!next-input-character;
1179          redo A;          redo A;
1180        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_char} == 0x003D) { # =
1181          $self->{state} = 'before attribute value';          !!!cp (72);
1182            $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
1183          !!!next-input-character;          !!!next-input-character;
1184          redo A;          redo A;
1185        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1186          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1187            $self->{current_token}->{first_start_tag}            !!!cp (73);
               = not defined $self->{last_emitted_start_tag_name};  
1188            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1189          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1190            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1191            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1192                !!!cp (74);
1193              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1194              } else {
1195                ## NOTE: This state should never be reached.
1196                !!!cp (75);
1197            }            }
1198          } else {          } else {
1199            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1200          }          }
1201          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1202          !!!next-input-character;          !!!next-input-character;
1203    
1204          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1205    
1206          redo A;          redo A;
1207        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1208                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1209          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          !!!cp (76);
1210                                value => ''};          $self->{current_attribute}
1211          $self->{state} = 'attribute name';              = {name => chr ($self->{next_char} + 0x0020),
1212                   value => '',
1213                   line => $self->{line}, column => $self->{column}};
1214            $self->{state} = ATTRIBUTE_NAME_STATE;
1215          !!!next-input-character;          !!!next-input-character;
1216          redo A;          redo A;
1217        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1218          !!!next-input-character;          !!!next-input-character;
1219          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_char} == 0x003E and # >
1220              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
1221              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
1222            # permitted slash            # permitted slash
1223              !!!cp (77);
1224            #            #
1225          } else {          } else {
1226              !!!cp (78);
1227            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
1228            ## TODO: Different error type for <aa / bb> than <aa/>            ## TODO: Different error type for <aa / bb> than <aa/>
1229          }          }
1230          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1231          # next-input-character is already done          # next-input-character is already done
1232          redo A;          redo A;
1233        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1234          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1235          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1236            $self->{current_token}->{first_start_tag}            !!!cp (79);
               = not defined $self->{last_emitted_start_tag_name};  
1237            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1238          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1239            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1240            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1241                !!!cp (80);
1242              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1243              } else {
1244                ## NOTE: This state should never be reached.
1245                !!!cp (81);
1246            }            }
1247          } else {          } else {
1248            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1249          }          }
1250          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1251          # reconsume          # reconsume
1252    
1253          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1254    
1255          redo A;          redo A;
1256        } else {        } else {
1257          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          !!!cp (82);
1258                                value => ''};          $self->{current_attribute}
1259          $self->{state} = 'attribute name';              = {name => chr ($self->{next_char}),
1260                   value => '',
1261                   line => $self->{line}, column => $self->{column}};
1262            $self->{state} = ATTRIBUTE_NAME_STATE;
1263          !!!next-input-character;          !!!next-input-character;
1264          redo A;                  redo A;        
1265        }        }
1266      } elsif ($self->{state} eq 'before attribute value') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_VALUE_STATE) {
1267        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1268            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1269            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1270            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1271            $self->{next_input_character} == 0x0020) { # SP                  $self->{next_char} == 0x0020) { # SP      
1272            !!!cp (83);
1273          ## Stay in the state          ## Stay in the state
1274          !!!next-input-character;          !!!next-input-character;
1275          redo A;          redo A;
1276        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
1277          $self->{state} = 'attribute value (double-quoted)';          !!!cp (84);
1278            $self->{state} = ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE;
1279          !!!next-input-character;          !!!next-input-character;
1280          redo A;          redo A;
1281        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1282          $self->{state} = 'attribute value (unquoted)';          !!!cp (85);
1283            $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1284          ## reconsume          ## reconsume
1285          redo A;          redo A;
1286        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
1287          $self->{state} = 'attribute value (single-quoted)';          !!!cp (86);
1288            $self->{state} = ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE;
1289          !!!next-input-character;          !!!next-input-character;
1290          redo A;          redo A;
1291        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1292          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1293            $self->{current_token}->{first_start_tag}            !!!cp (87);
               = not defined $self->{last_emitted_start_tag_name};  
1294            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1295          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1296            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1297            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1298                !!!cp (88);
1299              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1300              } else {
1301                ## NOTE: This state should never be reached.
1302                !!!cp (89);
1303            }            }
1304          } else {          } else {
1305            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1306          }          }
1307          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1308          !!!next-input-character;          !!!next-input-character;
1309    
1310          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1311    
1312          redo A;          redo A;
1313        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1314          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1315          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1316            $self->{current_token}->{first_start_tag}            !!!cp (90);
               = not defined $self->{last_emitted_start_tag_name};  
1317            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1318          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1319            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1320            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1321                !!!cp (91);
1322              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1323              } else {
1324                ## NOTE: This state should never be reached.
1325                !!!cp (92);
1326            }            }
1327          } else {          } else {
1328            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1329          }          }
1330          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1331          ## reconsume          ## reconsume
1332    
1333          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1334    
1335          redo A;          redo A;
1336        } else {        } else {
1337          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          if ($self->{next_char} == 0x003D) { # =
1338          $self->{state} = 'attribute value (unquoted)';            !!!cp (93);
1339              !!!parse-error (type => 'bad attribute value');
1340            } else {
1341              !!!cp (94);
1342            }
1343            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1344            $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1345          !!!next-input-character;          !!!next-input-character;
1346          redo A;          redo A;
1347        }        }
1348      } elsif ($self->{state} eq 'attribute value (double-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE) {
1349        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
1350          $self->{state} = 'before attribute name';          !!!cp (95);
1351            $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1352          !!!next-input-character;          !!!next-input-character;
1353          redo A;          redo A;
1354        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1355          $self->{last_attribute_value_state} = 'attribute value (double-quoted)';          !!!cp (96);
1356          $self->{state} = 'entity in attribute value';          $self->{last_attribute_value_state} = $self->{state};
1357            $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1358          !!!next-input-character;          !!!next-input-character;
1359          redo A;          redo A;
1360        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1361          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1362          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1363            $self->{current_token}->{first_start_tag}            !!!cp (97);
               = not defined $self->{last_emitted_start_tag_name};  
1364            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1365          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1366            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1367            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1368                !!!cp (98);
1369              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1370              } else {
1371                ## NOTE: This state should never be reached.
1372                !!!cp (99);
1373            }            }
1374          } else {          } else {
1375            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1376          }          }
1377          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1378          ## reconsume          ## reconsume
1379    
1380          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1381    
1382          redo A;          redo A;
1383        } else {        } else {
1384          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          !!!cp (100);
1385            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1386          ## Stay in the state          ## Stay in the state
1387          !!!next-input-character;          !!!next-input-character;
1388          redo A;          redo A;
1389        }        }
1390      } elsif ($self->{state} eq 'attribute value (single-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE) {
1391        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
1392          $self->{state} = 'before attribute name';          !!!cp (101);
1393            $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1394          !!!next-input-character;          !!!next-input-character;
1395          redo A;          redo A;
1396        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1397          $self->{last_attribute_value_state} = 'attribute value (single-quoted)';          !!!cp (102);
1398          $self->{state} = 'entity in attribute value';          $self->{last_attribute_value_state} = $self->{state};
1399            $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1400          !!!next-input-character;          !!!next-input-character;
1401          redo A;          redo A;
1402        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1403          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1404          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1405            $self->{current_token}->{first_start_tag}            !!!cp (103);
               = not defined $self->{last_emitted_start_tag_name};  
1406            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1407          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1408            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1409            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1410                !!!cp (104);
1411              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1412              } else {
1413                ## NOTE: This state should never be reached.
1414                !!!cp (105);
1415            }            }
1416          } else {          } else {
1417            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1418          }          }
1419          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1420          ## reconsume          ## reconsume
1421    
1422          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1423    
1424          redo A;          redo A;
1425        } else {        } else {
1426          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          !!!cp (106);
1427            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1428          ## Stay in the state          ## Stay in the state
1429          !!!next-input-character;          !!!next-input-character;
1430          redo A;          redo A;
1431        }        }
1432      } elsif ($self->{state} eq 'attribute value (unquoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_UNQUOTED_STATE) {
1433        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1434            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1435            $self->{next_input_character} == 0x000B or # HT            $self->{next_char} == 0x000B or # HT
1436            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1437            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1438          $self->{state} = 'before attribute name';          !!!cp (107);
1439          !!!next-input-character;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1440          redo A;          !!!next-input-character;
1441        } elsif ($self->{next_input_character} == 0x0026) { # &          redo A;
1442          $self->{last_attribute_value_state} = 'attribute value (unquoted)';        } elsif ($self->{next_char} == 0x0026) { # &
1443          $self->{state} = 'entity in attribute value';          !!!cp (108);
1444          !!!next-input-character;          $self->{last_attribute_value_state} = $self->{state};
1445          redo A;          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1446        } elsif ($self->{next_input_character} == 0x003E) { # >          !!!next-input-character;
1447          if ($self->{current_token}->{type} eq 'start tag') {          redo A;
1448            $self->{current_token}->{first_start_tag}        } elsif ($self->{next_char} == 0x003E) { # >
1449                = not defined $self->{last_emitted_start_tag_name};          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1450              !!!cp (109);
1451            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1452          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1453            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1454            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1455                !!!cp (110);
1456              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1457              } else {
1458                ## NOTE: This state should never be reached.
1459                !!!cp (111);
1460            }            }
1461          } else {          } else {
1462            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1463          }          }
1464          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1465          !!!next-input-character;          !!!next-input-character;
1466    
1467          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1468    
1469          redo A;          redo A;
1470        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1471          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1472          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1473            $self->{current_token}->{first_start_tag}            !!!cp (112);
               = not defined $self->{last_emitted_start_tag_name};  
1474            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1475          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1476            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1477            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1478                !!!cp (113);
1479              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1480              } else {
1481                ## NOTE: This state should never be reached.
1482                !!!cp (114);
1483            }            }
1484          } else {          } else {
1485            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1486          }          }
1487          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1488          ## reconsume          ## reconsume
1489    
1490          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1491    
1492          redo A;          redo A;
1493        } else {        } else {
1494          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          if ({
1495                 0x0022 => 1, # "
1496                 0x0027 => 1, # '
1497                 0x003D => 1, # =
1498                }->{$self->{next_char}}) {
1499              !!!cp (115);
1500              !!!parse-error (type => 'bad attribute value');
1501            } else {
1502              !!!cp (116);
1503            }
1504            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1505          ## Stay in the state          ## Stay in the state
1506          !!!next-input-character;          !!!next-input-character;
1507          redo A;          redo A;
1508        }        }
1509      } elsif ($self->{state} eq 'entity in attribute value') {      } elsif ($self->{state} == ENTITY_IN_ATTRIBUTE_VALUE_STATE) {
1510        my $token = $self->_tokenize_attempt_to_consume_an_entity (1);        my $token = $self->_tokenize_attempt_to_consume_an_entity
1511              (1,
1512               $self->{last_attribute_value_state}
1513                 == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE ? 0x0022 : # "
1514               $self->{last_attribute_value_state}
1515                 == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE ? 0x0027 : # '
1516               -1);
1517    
1518        unless (defined $token) {        unless (defined $token) {
1519            !!!cp (117);
1520          $self->{current_attribute}->{value} .= '&';          $self->{current_attribute}->{value} .= '&';
1521        } else {        } else {
1522            !!!cp (118);
1523          $self->{current_attribute}->{value} .= $token->{data};          $self->{current_attribute}->{value} .= $token->{data};
1524            $self->{current_attribute}->{has_reference} = $token->{has_reference};
1525          ## 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"
1526        }        }
1527    
1528        $self->{state} = $self->{last_attribute_value_state};        $self->{state} = $self->{last_attribute_value_state};
1529        # next-input-character is already done        # next-input-character is already done
1530        redo A;        redo A;
1531      } elsif ($self->{state} eq 'bogus comment') {      } elsif ($self->{state} == AFTER_ATTRIBUTE_VALUE_QUOTED_STATE) {
1532          if ($self->{next_char} == 0x0009 or # HT
1533              $self->{next_char} == 0x000A or # LF
1534              $self->{next_char} == 0x000B or # VT
1535              $self->{next_char} == 0x000C or # FF
1536              $self->{next_char} == 0x0020) { # SP
1537            !!!cp (118);
1538            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1539            !!!next-input-character;
1540            redo A;
1541          } elsif ($self->{next_char} == 0x003E) { # >
1542            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1543              !!!cp (119);
1544              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1545            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1546              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1547              if ($self->{current_token}->{attributes}) {
1548                !!!cp (120);
1549                !!!parse-error (type => 'end tag attribute');
1550              } else {
1551                ## NOTE: This state should never be reached.
1552                !!!cp (121);
1553              }
1554            } else {
1555              die "$0: $self->{current_token}->{type}: Unknown token type";
1556            }
1557            $self->{state} = DATA_STATE;
1558            !!!next-input-character;
1559    
1560            !!!emit ($self->{current_token}); # start tag or end tag
1561    
1562            redo A;
1563          } elsif ($self->{next_char} == 0x002F) { # /
1564            !!!next-input-character;
1565            if ($self->{next_char} == 0x003E and # >
1566                $self->{current_token}->{type} == START_TAG_TOKEN and
1567                $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
1568              # permitted slash
1569              !!!cp (122);
1570              #
1571            } else {
1572              !!!cp (123);
1573              !!!parse-error (type => 'nestc');
1574            }
1575            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1576            # next-input-character is already done
1577            redo A;
1578          } else {
1579            !!!cp (124);
1580            !!!parse-error (type => 'no space between attributes');
1581            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1582            ## reconsume
1583            redo A;
1584          }
1585        } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
1586        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1587                
1588        my $token = {type => 'comment', data => ''};        ## NOTE: Set by the previous state
1589          #my $token = {type => COMMENT_TOKEN, data => ''};
1590    
1591        BC: {        BC: {
1592          if ($self->{next_input_character} == 0x003E) { # >          if ($self->{next_char} == 0x003E) { # >
1593            $self->{state} = 'data';            !!!cp (124);
1594              $self->{state} = DATA_STATE;
1595            !!!next-input-character;            !!!next-input-character;
1596    
1597            !!!emit ($token);            !!!emit ($self->{current_token}); # comment
1598    
1599            redo A;            redo A;
1600          } elsif ($self->{next_input_character} == -1) {          } elsif ($self->{next_char} == -1) {
1601            $self->{state} = 'data';            !!!cp (125);
1602              $self->{state} = DATA_STATE;
1603            ## reconsume            ## reconsume
1604    
1605            !!!emit ($token);            !!!emit ($self->{current_token}); # comment
1606    
1607            redo A;            redo A;
1608          } else {          } else {
1609            $token->{data} .= chr ($self->{next_input_character});            !!!cp (126);
1610              $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
1611            !!!next-input-character;            !!!next-input-character;
1612            redo BC;            redo BC;
1613          }          }
1614        } # BC        } # BC
1615      } elsif ($self->{state} eq 'markup declaration open') {  
1616          die "$0: _get_next_token: unexpected case [BC]";
1617        } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
1618        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1619    
1620          my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1);
1621    
1622        my @next_char;        my @next_char;
1623        push @next_char, $self->{next_input_character};        push @next_char, $self->{next_char};
1624                
1625        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
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} == 0x002D) { # -          if ($self->{next_char} == 0x002D) { # -
1629            $self->{current_token} = {type => 'comment', data => ''};            !!!cp (127);
1630            $self->{state} = 'comment start';            $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1631                                        line => $l, column => $c,
1632                                       };
1633              $self->{state} = COMMENT_START_STATE;
1634            !!!next-input-character;            !!!next-input-character;
1635            redo A;            redo A;
1636            } else {
1637              !!!cp (128);
1638          }          }
1639        } elsif ($self->{next_input_character} == 0x0044 or # D        } elsif ($self->{next_char} == 0x0044 or # D
1640                 $self->{next_input_character} == 0x0064) { # d                 $self->{next_char} == 0x0064) { # d
1641          !!!next-input-character;          !!!next-input-character;
1642          push @next_char, $self->{next_input_character};          push @next_char, $self->{next_char};
1643          if ($self->{next_input_character} == 0x004F or # O          if ($self->{next_char} == 0x004F or # O
1644              $self->{next_input_character} == 0x006F) { # o              $self->{next_char} == 0x006F) { # o
1645            !!!next-input-character;            !!!next-input-character;
1646            push @next_char, $self->{next_input_character};            push @next_char, $self->{next_char};
1647            if ($self->{next_input_character} == 0x0043 or # C            if ($self->{next_char} == 0x0043 or # C
1648                $self->{next_input_character} == 0x0063) { # c                $self->{next_char} == 0x0063) { # c
1649              !!!next-input-character;              !!!next-input-character;
1650              push @next_char, $self->{next_input_character};              push @next_char, $self->{next_char};
1651              if ($self->{next_input_character} == 0x0054 or # T              if ($self->{next_char} == 0x0054 or # T
1652                  $self->{next_input_character} == 0x0074) { # t                  $self->{next_char} == 0x0074) { # t
1653                !!!next-input-character;                !!!next-input-character;
1654                push @next_char, $self->{next_input_character};                push @next_char, $self->{next_char};
1655                if ($self->{next_input_character} == 0x0059 or # Y                if ($self->{next_char} == 0x0059 or # Y
1656                    $self->{next_input_character} == 0x0079) { # y                    $self->{next_char} == 0x0079) { # y
1657                  !!!next-input-character;                  !!!next-input-character;
1658                  push @next_char, $self->{next_input_character};                  push @next_char, $self->{next_char};
1659                  if ($self->{next_input_character} == 0x0050 or # P                  if ($self->{next_char} == 0x0050 or # P
1660                      $self->{next_input_character} == 0x0070) { # p                      $self->{next_char} == 0x0070) { # p
1661                    !!!next-input-character;                    !!!next-input-character;
1662                    push @next_char, $self->{next_input_character};                    push @next_char, $self->{next_char};
1663                    if ($self->{next_input_character} == 0x0045 or # E                    if ($self->{next_char} == 0x0045 or # E
1664                        $self->{next_input_character} == 0x0065) { # e                        $self->{next_char} == 0x0065) { # e
1665                      ## ISSUE: What a stupid code this is!                      !!!cp (129);
1666                      $self->{state} = 'DOCTYPE';                      ## TODO: What a stupid code this is!
1667                        $self->{state} = DOCTYPE_STATE;
1668                        $self->{current_token} = {type => DOCTYPE_TOKEN,
1669                                                  quirks => 1,
1670                                                  line => $l, column => $c,
1671                                                 };
1672                      !!!next-input-character;                      !!!next-input-character;
1673                      redo A;                      redo A;
1674                      } else {
1675                        !!!cp (130);
1676                    }                    }
1677                    } else {
1678                      !!!cp (131);
1679                  }                  }
1680                  } else {
1681                    !!!cp (132);
1682                }                }
1683                } else {
1684                  !!!cp (133);
1685              }              }
1686              } else {
1687                !!!cp (134);
1688            }            }
1689            } else {
1690              !!!cp (135);
1691          }          }
1692          } else {
1693            !!!cp (136);
1694        }        }
1695    
1696        !!!parse-error (type => 'bogus comment');        !!!parse-error (type => 'bogus comment');
1697        $self->{next_input_character} = shift @next_char;        $self->{next_char} = shift @next_char;
1698        !!!back-next-input-character (@next_char);        !!!back-next-input-character (@next_char);
1699        $self->{state} = 'bogus comment';        $self->{state} = BOGUS_COMMENT_STATE;
1700          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1701                                    line => $l, column => $c,
1702                                   };
1703        redo A;        redo A;
1704                
1705        ## ISSUE: typos in spec: chacacters, is is a parse error        ## ISSUE: typos in spec: chacacters, is is a parse error
1706        ## 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?
1707      } elsif ($self->{state} eq 'comment start') {      } elsif ($self->{state} == COMMENT_START_STATE) {
1708        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
1709          $self->{state} = 'comment start dash';          !!!cp (137);
1710            $self->{state} = COMMENT_START_DASH_STATE;
1711          !!!next-input-character;          !!!next-input-character;
1712          redo A;          redo A;
1713        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1714            !!!cp (138);
1715          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
1716          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1717          !!!next-input-character;          !!!next-input-character;
1718    
1719          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
1720    
1721          redo A;          redo A;
1722        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1723            !!!cp (139);
1724          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1725          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1726          ## reconsume          ## reconsume
1727    
1728          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
1729    
1730          redo A;          redo A;
1731        } else {        } else {
1732            !!!cp (140);
1733          $self->{current_token}->{data} # comment          $self->{current_token}->{data} # comment
1734              .= chr ($self->{next_input_character});              .= chr ($self->{next_char});
1735          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
1736          !!!next-input-character;          !!!next-input-character;
1737          redo A;          redo A;
1738        }        }
1739      } elsif ($self->{state} eq 'comment start dash') {      } elsif ($self->{state} == COMMENT_START_DASH_STATE) {
1740        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
1741          $self->{state} = 'comment end';          !!!cp (141);
1742            $self->{state} = COMMENT_END_STATE;
1743          !!!next-input-character;          !!!next-input-character;
1744          redo A;          redo A;
1745        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1746            !!!cp (142);
1747          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
1748          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1749          !!!next-input-character;          !!!next-input-character;
1750    
1751          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
1752    
1753          redo A;          redo A;
1754        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1755            !!!cp (143);
1756          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1757          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1758          ## reconsume          ## reconsume
1759    
1760          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
1761    
1762          redo A;          redo A;
1763        } else {        } else {
1764            !!!cp (144);
1765          $self->{current_token}->{data} # comment          $self->{current_token}->{data} # comment
1766              .= '-' . chr ($self->{next_input_character});              .= '-' . chr ($self->{next_char});
1767          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
1768          !!!next-input-character;          !!!next-input-character;
1769          redo A;          redo A;
1770        }        }
1771      } elsif ($self->{state} eq 'comment') {      } elsif ($self->{state} == COMMENT_STATE) {
1772        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
1773          $self->{state} = 'comment end dash';          !!!cp (145);
1774            $self->{state} = COMMENT_END_DASH_STATE;
1775          !!!next-input-character;          !!!next-input-character;
1776          redo A;          redo A;
1777        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1778            !!!cp (146);
1779          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1780          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1781          ## reconsume          ## reconsume
1782    
1783          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
1784    
1785          redo A;          redo A;
1786        } else {        } else {
1787          $self->{current_token}->{data} .= chr ($self->{next_input_character}); # comment          !!!cp (147);
1788            $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
1789          ## Stay in the state          ## Stay in the state
1790          !!!next-input-character;          !!!next-input-character;
1791          redo A;          redo A;
1792        }        }
1793      } elsif ($self->{state} eq 'comment end dash') {      } elsif ($self->{state} == COMMENT_END_DASH_STATE) {
1794        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
1795          $self->{state} = 'comment end';          !!!cp (148);
1796            $self->{state} = COMMENT_END_STATE;
1797          !!!next-input-character;          !!!next-input-character;
1798          redo A;          redo A;
1799        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1800            !!!cp (149);
1801          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1802          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1803          ## reconsume          ## reconsume
1804    
1805          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
1806    
1807          redo A;          redo A;
1808        } else {        } else {
1809          $self->{current_token}->{data} .= '-' . chr ($self->{next_input_character}); # comment          !!!cp (150);
1810          $self->{state} = 'comment';          $self->{current_token}->{data} .= '-' . chr ($self->{next_char}); # comment
1811            $self->{state} = COMMENT_STATE;
1812          !!!next-input-character;          !!!next-input-character;
1813          redo A;          redo A;
1814        }        }
1815      } elsif ($self->{state} eq 'comment end') {      } elsif ($self->{state} == COMMENT_END_STATE) {
1816        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_char} == 0x003E) { # >
1817          $self->{state} = 'data';          !!!cp (151);
1818            $self->{state} = DATA_STATE;
1819          !!!next-input-character;          !!!next-input-character;
1820    
1821          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
1822    
1823          redo A;          redo A;
1824        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
1825          !!!parse-error (type => 'dash in comment');          !!!cp (152);
1826            !!!parse-error (type => 'dash in comment',
1827                            line => $self->{line_prev},
1828                            column => $self->{column_prev});
1829          $self->{current_token}->{data} .= '-'; # comment          $self->{current_token}->{data} .= '-'; # comment
1830          ## Stay in the state          ## Stay in the state
1831          !!!next-input-character;          !!!next-input-character;
1832          redo A;          redo A;
1833        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1834            !!!cp (153);
1835          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1836          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1837          ## reconsume          ## reconsume
1838    
1839          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
1840    
1841          redo A;          redo A;
1842        } else {        } else {
1843          !!!parse-error (type => 'dash in comment');          !!!cp (154);
1844          $self->{current_token}->{data} .= '--' . chr ($self->{next_input_character}); # comment          !!!parse-error (type => 'dash in comment',
1845          $self->{state} = 'comment';                          line => $self->{line_prev},
1846                            column => $self->{column_prev});
1847            $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment
1848            $self->{state} = COMMENT_STATE;
1849          !!!next-input-character;          !!!next-input-character;
1850          redo A;          redo A;
1851        }        }
1852      } elsif ($self->{state} eq 'DOCTYPE') {      } elsif ($self->{state} == DOCTYPE_STATE) {
1853        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1854            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1855            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1856            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1857            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1858          $self->{state} = 'before DOCTYPE name';          !!!cp (155);
1859            $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
1860          !!!next-input-character;          !!!next-input-character;
1861          redo A;          redo A;
1862        } else {        } else {
1863            !!!cp (156);
1864          !!!parse-error (type => 'no space before DOCTYPE name');          !!!parse-error (type => 'no space before DOCTYPE name');
1865          $self->{state} = 'before DOCTYPE name';          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
1866          ## reconsume          ## reconsume
1867          redo A;          redo A;
1868        }        }
1869      } elsif ($self->{state} eq 'before DOCTYPE name') {      } elsif ($self->{state} == BEFORE_DOCTYPE_NAME_STATE) {
1870        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1871            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1872            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1873            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1874            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1875            !!!cp (157);
1876          ## Stay in the state          ## Stay in the state
1877          !!!next-input-character;          !!!next-input-character;
1878          redo A;          redo A;
1879        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1880            !!!cp (158);
1881          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
1882          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1883          !!!next-input-character;          !!!next-input-character;
1884    
1885          !!!emit ({type => 'DOCTYPE'}); # incorrect          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
1886    
1887          redo A;          redo A;
1888        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1889            !!!cp (159);
1890          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
1891          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1892          ## reconsume          ## reconsume
1893    
1894          !!!emit ({type => 'DOCTYPE'}); # incorrect          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
1895    
1896          redo A;          redo A;
1897        } else {        } else {
1898          $self->{current_token}          !!!cp (160);
1899              = {type => 'DOCTYPE',          $self->{current_token}->{name} = chr $self->{next_char};
1900                 name => chr ($self->{next_input_character}),          delete $self->{current_token}->{quirks};
                correct => 1};  
1901  ## ISSUE: "Set the token's name name to the" in the spec  ## ISSUE: "Set the token's name name to the" in the spec
1902          $self->{state} = 'DOCTYPE name';          $self->{state} = DOCTYPE_NAME_STATE;
1903          !!!next-input-character;          !!!next-input-character;
1904          redo A;          redo A;
1905        }        }
1906      } elsif ($self->{state} eq 'DOCTYPE name') {      } elsif ($self->{state} == DOCTYPE_NAME_STATE) {
1907  ## ISSUE: Redundant "First," in the spec.  ## ISSUE: Redundant "First," in the spec.
1908        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1909            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1910            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1911            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1912            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1913          $self->{state} = 'after DOCTYPE name';          !!!cp (161);
1914            $self->{state} = AFTER_DOCTYPE_NAME_STATE;
1915          !!!next-input-character;          !!!next-input-character;
1916          redo A;          redo A;
1917        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1918          $self->{state} = 'data';          !!!cp (162);
1919            $self->{state} = DATA_STATE;
1920          !!!next-input-character;          !!!next-input-character;
1921    
1922          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1923    
1924          redo A;          redo A;
1925        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1926            !!!cp (163);
1927          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1928          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1929          ## reconsume          ## reconsume
1930    
1931          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
1932          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1933    
1934          redo A;          redo A;
1935        } else {        } else {
1936            !!!cp (164);
1937          $self->{current_token}->{name}          $self->{current_token}->{name}
1938            .= chr ($self->{next_input_character}); # DOCTYPE            .= chr ($self->{next_char}); # DOCTYPE
1939          ## Stay in the state          ## Stay in the state
1940          !!!next-input-character;          !!!next-input-character;
1941          redo A;          redo A;
1942        }        }
1943      } elsif ($self->{state} eq 'after DOCTYPE name') {      } elsif ($self->{state} == AFTER_DOCTYPE_NAME_STATE) {
1944        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1945            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1946            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1947            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1948            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1949            !!!cp (165);
1950          ## Stay in the state          ## Stay in the state
1951          !!!next-input-character;          !!!next-input-character;
1952          redo A;          redo A;
1953        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1954          $self->{state} = 'data';          !!!cp (166);
1955            $self->{state} = DATA_STATE;
1956          !!!next-input-character;          !!!next-input-character;
1957    
1958          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1959    
1960          redo A;          redo A;
1961        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1962            !!!cp (167);
1963          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1964          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1965          ## reconsume          ## reconsume
1966    
1967          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
1968          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1969    
1970          redo A;          redo A;
1971        } elsif ($self->{next_input_character} == 0x0050 or # P        } elsif ($self->{next_char} == 0x0050 or # P
1972                 $self->{next_input_character} == 0x0070) { # p                 $self->{next_char} == 0x0070) { # p
1973          !!!next-input-character;          !!!next-input-character;
1974          if ($self->{next_input_character} == 0x0055 or # U          if ($self->{next_char} == 0x0055 or # U
1975              $self->{next_input_character} == 0x0075) { # u              $self->{next_char} == 0x0075) { # u
1976            !!!next-input-character;            !!!next-input-character;
1977            if ($self->{next_input_character} == 0x0042 or # B            if ($self->{next_char} == 0x0042 or # B
1978                $self->{next_input_character} == 0x0062) { # b                $self->{next_char} == 0x0062) { # b
1979              !!!next-input-character;              !!!next-input-character;
1980              if ($self->{next_input_character} == 0x004C or # L              if ($self->{next_char} == 0x004C or # L
1981                  $self->{next_input_character} == 0x006C) { # l                  $self->{next_char} == 0x006C) { # l
1982                !!!next-input-character;                !!!next-input-character;
1983                if ($self->{next_input_character} == 0x0049 or # I                if ($self->{next_char} == 0x0049 or # I
1984                    $self->{next_input_character} == 0x0069) { # i                    $self->{next_char} == 0x0069) { # i
1985                  !!!next-input-character;                  !!!next-input-character;
1986                  if ($self->{next_input_character} == 0x0043 or # C                  if ($self->{next_char} == 0x0043 or # C
1987                      $self->{next_input_character} == 0x0063) { # c                      $self->{next_char} == 0x0063) { # c
1988                    $self->{state} = 'before DOCTYPE public identifier';                    !!!cp (168);
1989                      $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1990                    !!!next-input-character;                    !!!next-input-character;
1991                    redo A;                    redo A;
1992                    } else {
1993                      !!!cp (169);
1994                  }                  }
1995                  } else {
1996                    !!!cp (170);
1997                }                }
1998                } else {
1999                  !!!cp (171);
2000              }              }
2001              } else {
2002                !!!cp (172);
2003            }            }
2004            } else {
2005              !!!cp (173);
2006          }          }
2007    
2008          #          #
2009        } elsif ($self->{next_input_character} == 0x0053 or # S        } elsif ($self->{next_char} == 0x0053 or # S
2010                 $self->{next_input_character} == 0x0073) { # s                 $self->{next_char} == 0x0073) { # s
2011          !!!next-input-character;          !!!next-input-character;
2012          if ($self->{next_input_character} == 0x0059 or # Y          if ($self->{next_char} == 0x0059 or # Y
2013              $self->{next_input_character} == 0x0079) { # y              $self->{next_char} == 0x0079) { # y
2014            !!!next-input-character;            !!!next-input-character;
2015            if ($self->{next_input_character} == 0x0053 or # S            if ($self->{next_char} == 0x0053 or # S
2016                $self->{next_input_character} == 0x0073) { # s                $self->{next_char} == 0x0073) { # s
2017              !!!next-input-character;              !!!next-input-character;
2018              if ($self->{next_input_character} == 0x0054 or # T              if ($self->{next_char} == 0x0054 or # T
2019                  $self->{next_input_character} == 0x0074) { # t                  $self->{next_char} == 0x0074) { # t
2020                !!!next-input-character;                !!!next-input-character;
2021                if ($self->{next_input_character} == 0x0045 or # E                if ($self->{next_char} == 0x0045 or # E
2022                    $self->{next_input_character} == 0x0065) { # e                    $self->{next_char} == 0x0065) { # e
2023                  !!!next-input-character;                  !!!next-input-character;
2024                  if ($self->{next_input_character} == 0x004D or # M                  if ($self->{next_char} == 0x004D or # M
2025                      $self->{next_input_character} == 0x006D) { # m                      $self->{next_char} == 0x006D) { # m
2026                    $self->{state} = 'before DOCTYPE system identifier';                    !!!cp (174);
2027                      $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2028                    !!!next-input-character;                    !!!next-input-character;
2029                    redo A;                    redo A;
2030                    } else {
2031                      !!!cp (175);
2032                  }                  }
2033                  } else {
2034                    !!!cp (176);
2035                }                }
2036                } else {
2037                  !!!cp (177);
2038              }              }
2039              } else {
2040                !!!cp (178);
2041            }            }
2042            } else {
2043              !!!cp (179);
2044          }          }
2045    
2046          #          #
2047        } else {        } else {
2048            !!!cp (180);
2049          !!!next-input-character;          !!!next-input-character;
2050          #          #
2051        }        }
2052    
2053        !!!parse-error (type => 'string after DOCTYPE name');        !!!parse-error (type => 'string after DOCTYPE name');
2054        $self->{state} = 'bogus DOCTYPE';        $self->{current_token}->{quirks} = 1;
2055    
2056          $self->{state} = BOGUS_DOCTYPE_STATE;
2057        # next-input-character is already done        # next-input-character is already done
2058        redo A;        redo A;
2059      } elsif ($self->{state} eq 'before DOCTYPE public identifier') {      } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
2060        if ({        if ({
2061              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2062              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2063            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2064            !!!cp (181);
2065          ## Stay in the state          ## Stay in the state
2066          !!!next-input-character;          !!!next-input-character;
2067          redo A;          redo A;
2068        } elsif ($self->{next_input_character} eq 0x0022) { # "        } elsif ($self->{next_char} eq 0x0022) { # "
2069            !!!cp (182);
2070          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{current_token}->{public_identifier} = ''; # DOCTYPE
2071          $self->{state} = 'DOCTYPE public identifier (double-quoted)';          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;
2072          !!!next-input-character;          !!!next-input-character;
2073          redo A;          redo A;
2074        } elsif ($self->{next_input_character} eq 0x0027) { # '        } elsif ($self->{next_char} eq 0x0027) { # '
2075            !!!cp (183);
2076          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{current_token}->{public_identifier} = ''; # DOCTYPE
2077          $self->{state} = 'DOCTYPE public identifier (single-quoted)';          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;
2078          !!!next-input-character;          !!!next-input-character;
2079          redo A;          redo A;
2080        } elsif ($self->{next_input_character} eq 0x003E) { # >        } elsif ($self->{next_char} eq 0x003E) { # >
2081            !!!cp (184);
2082          !!!parse-error (type => 'no PUBLIC literal');          !!!parse-error (type => 'no PUBLIC literal');
2083    
2084          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2085          !!!next-input-character;          !!!next-input-character;
2086    
2087          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2088          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2089    
2090          redo A;          redo A;
2091        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2092            !!!cp (185);
2093          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2094    
2095          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2096          ## reconsume          ## reconsume
2097    
2098          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2099          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2100    
2101          redo A;          redo A;
2102        } else {        } else {
2103            !!!cp (186);
2104          !!!parse-error (type => 'string after PUBLIC');          !!!parse-error (type => 'string after PUBLIC');
2105          $self->{state} = 'bogus DOCTYPE';          $self->{current_token}->{quirks} = 1;
2106    
2107            $self->{state} = BOGUS_DOCTYPE_STATE;
2108          !!!next-input-character;          !!!next-input-character;
2109          redo A;          redo A;
2110        }        }
2111      } elsif ($self->{state} eq 'DOCTYPE public identifier (double-quoted)') {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2112        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
2113          $self->{state} = 'after DOCTYPE public identifier';          !!!cp (187);
2114            $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2115            !!!next-input-character;
2116            redo A;
2117          } elsif ($self->{next_char} == 0x003E) { # >
2118            !!!cp (188);
2119            !!!parse-error (type => 'unclosed PUBLIC literal');
2120    
2121            $self->{state} = DATA_STATE;
2122          !!!next-input-character;          !!!next-input-character;
2123    
2124            $self->{current_token}->{quirks} = 1;
2125            !!!emit ($self->{current_token}); # DOCTYPE
2126    
2127          redo A;          redo A;
2128        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2129            !!!cp (189);
2130          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
2131    
2132          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2133          ## reconsume          ## reconsume
2134    
2135          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2136          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2137    
2138          redo A;          redo A;
2139        } else {        } else {
2140            !!!cp (190);
2141          $self->{current_token}->{public_identifier} # DOCTYPE          $self->{current_token}->{public_identifier} # DOCTYPE
2142              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2143          ## Stay in the state          ## Stay in the state
2144          !!!next-input-character;          !!!next-input-character;
2145          redo A;          redo A;
2146        }        }
2147      } elsif ($self->{state} eq 'DOCTYPE public identifier (single-quoted)') {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {
2148        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
2149          $self->{state} = 'after DOCTYPE public identifier';          !!!cp (191);
2150            $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2151            !!!next-input-character;
2152            redo A;
2153          } elsif ($self->{next_char} == 0x003E) { # >
2154            !!!cp (192);
2155            !!!parse-error (type => 'unclosed PUBLIC literal');
2156    
2157            $self->{state} = DATA_STATE;
2158          !!!next-input-character;          !!!next-input-character;
2159    
2160            $self->{current_token}->{quirks} = 1;
2161            !!!emit ($self->{current_token}); # DOCTYPE
2162    
2163          redo A;          redo A;
2164        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2165            !!!cp (193);
2166          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
2167    
2168          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2169          ## reconsume          ## reconsume
2170    
2171          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2172          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2173    
2174          redo A;          redo A;
2175        } else {        } else {
2176            !!!cp (194);
2177          $self->{current_token}->{public_identifier} # DOCTYPE          $self->{current_token}->{public_identifier} # DOCTYPE
2178              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2179          ## Stay in the state          ## Stay in the state
2180          !!!next-input-character;          !!!next-input-character;
2181          redo A;          redo A;
2182        }        }
2183      } elsif ($self->{state} eq 'after DOCTYPE public identifier') {      } elsif ($self->{state} == AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
2184        if ({        if ({
2185              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2186              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2187            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2188            !!!cp (195);
2189          ## Stay in the state          ## Stay in the state
2190          !!!next-input-character;          !!!next-input-character;
2191          redo A;          redo A;
2192        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
2193            !!!cp (196);
2194          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2195          $self->{state} = 'DOCTYPE system identifier (double-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2196          !!!next-input-character;          !!!next-input-character;
2197          redo A;          redo A;
2198        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
2199            !!!cp (197);
2200          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2201          $self->{state} = 'DOCTYPE system identifier (single-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2202          !!!next-input-character;          !!!next-input-character;
2203          redo A;          redo A;
2204        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2205          $self->{state} = 'data';          !!!cp (198);
2206            $self->{state} = DATA_STATE;
2207          !!!next-input-character;          !!!next-input-character;
2208    
2209          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2210    
2211          redo A;          redo A;
2212        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2213            !!!cp (199);
2214          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2215    
2216          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2217          ## reconsume          ## reconsume
2218    
2219          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2220          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2221    
2222          redo A;          redo A;
2223        } else {        } else {
2224            !!!cp (200);
2225          !!!parse-error (type => 'string after PUBLIC literal');          !!!parse-error (type => 'string after PUBLIC literal');
2226          $self->{state} = 'bogus DOCTYPE';          $self->{current_token}->{quirks} = 1;
2227    
2228            $self->{state} = BOGUS_DOCTYPE_STATE;
2229          !!!next-input-character;          !!!next-input-character;
2230          redo A;          redo A;
2231        }        }
2232      } elsif ($self->{state} eq 'before DOCTYPE system identifier') {      } elsif ($self->{state} == BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
2233        if ({        if ({
2234              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2235              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2236            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2237            !!!cp (201);
2238          ## Stay in the state          ## Stay in the state
2239          !!!next-input-character;          !!!next-input-character;
2240          redo A;          redo A;
2241        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
2242            !!!cp (202);
2243          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2244          $self->{state} = 'DOCTYPE system identifier (double-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2245          !!!next-input-character;          !!!next-input-character;
2246          redo A;          redo A;
2247        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
2248            !!!cp (203);
2249          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2250          $self->{state} = 'DOCTYPE system identifier (single-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2251          !!!next-input-character;          !!!next-input-character;
2252          redo A;          redo A;
2253        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2254            !!!cp (204);
2255          !!!parse-error (type => 'no SYSTEM literal');          !!!parse-error (type => 'no SYSTEM literal');
2256          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2257          !!!next-input-character;          !!!next-input-character;
2258    
2259          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2260          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2261    
2262          redo A;          redo A;
2263        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2264            !!!cp (205);
2265          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2266    
2267          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2268          ## reconsume          ## reconsume
2269    
2270          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2271          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2272    
2273          redo A;          redo A;
2274        } else {        } else {
2275            !!!cp (206);
2276          !!!parse-error (type => 'string after SYSTEM');          !!!parse-error (type => 'string after SYSTEM');
2277          $self->{state} = 'bogus DOCTYPE';          $self->{current_token}->{quirks} = 1;
2278    
2279            $self->{state} = BOGUS_DOCTYPE_STATE;
2280          !!!next-input-character;          !!!next-input-character;
2281          redo A;          redo A;
2282        }        }
2283      } elsif ($self->{state} eq 'DOCTYPE system identifier (double-quoted)') {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2284        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
2285          $self->{state} = 'after DOCTYPE system identifier';          !!!cp (207);
2286            $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2287          !!!next-input-character;          !!!next-input-character;
2288          redo A;          redo A;
2289        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == 0x003E) { # >
2290            !!!cp (208);
2291            !!!parse-error (type => 'unclosed PUBLIC literal');
2292    
2293            $self->{state} = DATA_STATE;
2294            !!!next-input-character;
2295    
2296            $self->{current_token}->{quirks} = 1;
2297            !!!emit ($self->{current_token}); # DOCTYPE
2298    
2299            redo A;
2300          } elsif ($self->{next_char} == -1) {
2301            !!!cp (209);
2302          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2303    
2304          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2305          ## reconsume          ## reconsume
2306    
2307          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2308          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2309    
2310          redo A;          redo A;
2311        } else {        } else {
2312            !!!cp (210);
2313          $self->{current_token}->{system_identifier} # DOCTYPE          $self->{current_token}->{system_identifier} # DOCTYPE
2314              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2315          ## Stay in the state          ## Stay in the state
2316          !!!next-input-character;          !!!next-input-character;
2317          redo A;          redo A;
2318        }        }
2319      } elsif ($self->{state} eq 'DOCTYPE system identifier (single-quoted)') {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {
2320        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
2321          $self->{state} = 'after DOCTYPE system identifier';          !!!cp (211);
2322            $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2323          !!!next-input-character;          !!!next-input-character;
2324          redo A;          redo A;
2325        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == 0x003E) { # >
2326            !!!cp (212);
2327            !!!parse-error (type => 'unclosed PUBLIC literal');
2328    
2329            $self->{state} = DATA_STATE;
2330            !!!next-input-character;
2331    
2332            $self->{current_token}->{quirks} = 1;
2333            !!!emit ($self->{current_token}); # DOCTYPE
2334    
2335            redo A;
2336          } elsif ($self->{next_char} == -1) {
2337            !!!cp (213);
2338          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2339    
2340          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2341          ## reconsume          ## reconsume
2342    
2343          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2344          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2345    
2346          redo A;          redo A;
2347        } else {        } else {
2348            !!!cp (214);
2349          $self->{current_token}->{system_identifier} # DOCTYPE          $self->{current_token}->{system_identifier} # DOCTYPE
2350              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2351          ## Stay in the state          ## Stay in the state
2352          !!!next-input-character;          !!!next-input-character;
2353          redo A;          redo A;
2354        }        }
2355      } elsif ($self->{state} eq 'after DOCTYPE system identifier') {      } elsif ($self->{state} == AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
2356        if ({        if ({
2357              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2358              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2359            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2360            !!!cp (215);
2361          ## Stay in the state          ## Stay in the state
2362          !!!next-input-character;          !!!next-input-character;
2363          redo A;          redo A;
2364        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2365          $self->{state} = 'data';          !!!cp (216);
2366            $self->{state} = DATA_STATE;
2367          !!!next-input-character;          !!!next-input-character;
2368    
2369          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2370    
2371          redo A;          redo A;
2372        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2373            !!!cp (217);
2374          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2375    
2376          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2377          ## reconsume          ## reconsume
2378    
2379          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2380          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2381    
2382          redo A;          redo A;
2383        } else {        } else {
2384            !!!cp (218);
2385          !!!parse-error (type => 'string after SYSTEM literal');          !!!parse-error (type => 'string after SYSTEM literal');
2386          $self->{state} = 'bogus DOCTYPE';          #$self->{current_token}->{quirks} = 1;
2387    
2388            $self->{state} = BOGUS_DOCTYPE_STATE;
2389          !!!next-input-character;          !!!next-input-character;
2390          redo A;          redo A;
2391        }        }
2392      } elsif ($self->{state} eq 'bogus DOCTYPE') {      } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {
2393        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_char} == 0x003E) { # >
2394          $self->{state} = 'data';          !!!cp (219);
2395            $self->{state} = DATA_STATE;
2396          !!!next-input-character;          !!!next-input-character;
2397    
         delete $self->{current_token}->{correct};  
2398          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2399    
2400          redo A;          redo A;
2401        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2402            !!!cp (220);
2403          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2404          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2405          ## reconsume          ## reconsume
2406    
         delete $self->{current_token}->{correct};  
2407          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2408    
2409          redo A;          redo A;
2410        } else {        } else {
2411            !!!cp (221);
2412          ## Stay in the state          ## Stay in the state
2413          !!!next-input-character;          !!!next-input-character;
2414          redo A;          redo A;
# Line 1609  sub _get_next_token ($) { Line 2421  sub _get_next_token ($) {
2421    die "$0: _get_next_token: unexpected case";    die "$0: _get_next_token: unexpected case";
2422  } # _get_next_token  } # _get_next_token
2423    
2424  sub _tokenize_attempt_to_consume_an_entity ($$) {  sub _tokenize_attempt_to_consume_an_entity ($$$) {
2425    my ($self, $in_attr) = @_;    my ($self, $in_attr, $additional) = @_;
2426    
2427      my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
2428    
2429    if ({    if ({
2430         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,
2431         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR
2432        }->{$self->{next_input_character}}) {         $additional => 1,
2433          }->{$self->{next_char}}) {
2434        !!!cp (1001);
2435      ## Don't consume      ## Don't consume
2436      ## No error      ## No error
2437      return undef;      return undef;
2438    } elsif ($self->{next_input_character} == 0x0023) { # #    } elsif ($self->{next_char} == 0x0023) { # #
2439      !!!next-input-character;      !!!next-input-character;
2440      if ($self->{next_input_character} == 0x0078 or # x      if ($self->{next_char} == 0x0078 or # x
2441          $self->{next_input_character} == 0x0058) { # X          $self->{next_char} == 0x0058) { # X
2442        my $code;        my $code;
2443        X: {        X: {
2444          my $x_char = $self->{next_input_character};          my $x_char = $self->{next_char};
2445          !!!next-input-character;          !!!next-input-character;
2446          if (0x0030 <= $self->{next_input_character} and          if (0x0030 <= $self->{next_char} and
2447              $self->{next_input_character} <= 0x0039) { # 0..9              $self->{next_char} <= 0x0039) { # 0..9
2448              !!!cp (1002);
2449            $code ||= 0;            $code ||= 0;
2450            $code *= 0x10;            $code *= 0x10;
2451            $code += $self->{next_input_character} - 0x0030;            $code += $self->{next_char} - 0x0030;
2452            redo X;            redo X;
2453          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_char} and
2454                   $self->{next_input_character} <= 0x0066) { # a..f                   $self->{next_char} <= 0x0066) { # a..f
2455              !!!cp (1003);
2456            $code ||= 0;            $code ||= 0;
2457            $code *= 0x10;            $code *= 0x10;
2458            $code += $self->{next_input_character} - 0x0060 + 9;            $code += $self->{next_char} - 0x0060 + 9;
2459            redo X;            redo X;
2460          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_char} and
2461                   $self->{next_input_character} <= 0x0046) { # A..F                   $self->{next_char} <= 0x0046) { # A..F
2462              !!!cp (1004);
2463            $code ||= 0;            $code ||= 0;
2464            $code *= 0x10;            $code *= 0x10;
2465            $code += $self->{next_input_character} - 0x0040 + 9;            $code += $self->{next_char} - 0x0040 + 9;
2466            redo X;            redo X;
2467          } elsif (not defined $code) { # no hexadecimal digit          } elsif (not defined $code) { # no hexadecimal digit
2468            !!!parse-error (type => 'bare hcro');            !!!cp (1005);
2469            !!!back-next-input-character ($x_char, $self->{next_input_character});            !!!parse-error (type => 'bare hcro', line => $l, column => $c);
2470            $self->{next_input_character} = 0x0023; # #            !!!back-next-input-character ($x_char, $self->{next_char});
2471              $self->{next_char} = 0x0023; # #
2472            return undef;            return undef;
2473          } elsif ($self->{next_input_character} == 0x003B) { # ;          } elsif ($self->{next_char} == 0x003B) { # ;
2474              !!!cp (1006);
2475            !!!next-input-character;            !!!next-input-character;
2476          } else {          } else {
2477            !!!parse-error (type => 'no refc');            !!!cp (1007);
2478              !!!parse-error (type => 'no refc', line => $l, column => $c);
2479          }          }
2480    
2481          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2482            !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);            !!!cp (1008);
2483              !!!parse-error (type => (sprintf 'invalid character reference:U+%04X', $code), line => $l, column => $c);
2484            $code = 0xFFFD;            $code = 0xFFFD;
2485          } elsif ($code > 0x10FFFF) {          } elsif ($code > 0x10FFFF) {
2486            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);            !!!cp (1009);
2487              !!!parse-error (type => (sprintf 'invalid character reference:U-%08X', $code), line => $l, column => $c);
2488            $code = 0xFFFD;            $code = 0xFFFD;
2489          } elsif ($code == 0x000D) {          } elsif ($code == 0x000D) {
2490            !!!parse-error (type => 'CR character reference');            !!!cp (1010);
2491              !!!parse-error (type => 'CR character reference', line => $l, column => $c);
2492            $code = 0x000A;            $code = 0x000A;
2493          } elsif (0x80 <= $code and $code <= 0x9F) {          } elsif (0x80 <= $code and $code <= 0x9F) {
2494            !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);            !!!cp (1011);
2495              !!!parse-error (type => (sprintf 'C1 character reference:U+%04X', $code), line => $l, column => $c);
2496            $code = $c1_entity_char->{$code};            $code = $c1_entity_char->{$code};
2497          }          }
2498    
2499          return {type => 'character', data => chr $code};          return {type => CHARACTER_TOKEN, data => chr $code,
2500                    has_reference => 1,
2501                    line => $l, column => $c,
2502                   };
2503        } # X        } # X
2504      } elsif (0x0030 <= $self->{next_input_character} and      } elsif (0x0030 <= $self->{next_char} and
2505               $self->{next_input_character} <= 0x0039) { # 0..9               $self->{next_char} <= 0x0039) { # 0..9
2506        my $code = $self->{next_input_character} - 0x0030;        my $code = $self->{next_char} - 0x0030;
2507        !!!next-input-character;        !!!next-input-character;
2508                
2509        while (0x0030 <= $self->{next_input_character} and        while (0x0030 <= $self->{next_char} and
2510                  $self->{next_input_character} <= 0x0039) { # 0..9                  $self->{next_char} <= 0x0039) { # 0..9
2511            !!!cp (1012);
2512          $code *= 10;          $code *= 10;
2513          $code += $self->{next_input_character} - 0x0030;          $code += $self->{next_char} - 0x0030;
2514                    
2515          !!!next-input-character;          !!!next-input-character;
2516        }        }
2517    
2518        if ($self->{next_input_character} == 0x003B) { # ;        if ($self->{next_char} == 0x003B) { # ;
2519            !!!cp (1013);
2520          !!!next-input-character;          !!!next-input-character;
2521        } else {        } else {
2522          !!!parse-error (type => 'no refc');          !!!cp (1014);
2523            !!!parse-error (type => 'no refc', line => $l, column => $c);
2524        }        }
2525    
2526        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2527          !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);          !!!cp (1015);
2528            !!!parse-error (type => (sprintf 'invalid character reference:U+%04X', $code), line => $l, column => $c);
2529          $code = 0xFFFD;          $code = 0xFFFD;
2530        } elsif ($code > 0x10FFFF) {        } elsif ($code > 0x10FFFF) {
2531          !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);          !!!cp (1016);
2532            !!!parse-error (type => (sprintf 'invalid character reference:U-%08X', $code), line => $l, column => $c);
2533          $code = 0xFFFD;          $code = 0xFFFD;
2534        } elsif ($code == 0x000D) {        } elsif ($code == 0x000D) {
2535          !!!parse-error (type => 'CR character reference');          !!!cp (1017);
2536            !!!parse-error (type => 'CR character reference', line => $l, column => $c);
2537          $code = 0x000A;          $code = 0x000A;
2538        } elsif (0x80 <= $code and $code <= 0x9F) {        } elsif (0x80 <= $code and $code <= 0x9F) {
2539          !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);          !!!cp (1018);
2540            !!!parse-error (type => (sprintf 'C1 character reference:U+%04X', $code), line => $l, column => $c);
2541          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
2542        }        }
2543                
2544        return {type => 'character', data => chr $code};        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1,
2545                  line => $l, column => $c,
2546                 };
2547      } else {      } else {
2548        !!!parse-error (type => 'bare nero');        !!!cp (1019);
2549        !!!back-next-input-character ($self->{next_input_character});        !!!parse-error (type => 'bare nero', line => $l, column => $c);
2550        $self->{next_input_character} = 0x0023; # #        !!!back-next-input-character ($self->{next_char});
2551          $self->{next_char} = 0x0023; # #
2552        return undef;        return undef;
2553      }      }
2554    } elsif ((0x0041 <= $self->{next_input_character} and    } elsif ((0x0041 <= $self->{next_char} and
2555              $self->{next_input_character} <= 0x005A) or              $self->{next_char} <= 0x005A) or
2556             (0x0061 <= $self->{next_input_character} and             (0x0061 <= $self->{next_char} and
2557              $self->{next_input_character} <= 0x007A)) {              $self->{next_char} <= 0x007A)) {
2558      my $entity_name = chr $self->{next_input_character};      my $entity_name = chr $self->{next_char};
2559      !!!next-input-character;      !!!next-input-character;
2560    
2561      my $value = $entity_name;      my $value = $entity_name;
# Line 1726  sub _tokenize_attempt_to_consume_an_enti Line 2565  sub _tokenize_attempt_to_consume_an_enti
2565    
2566      while (length $entity_name < 10 and      while (length $entity_name < 10 and
2567             ## NOTE: Some number greater than the maximum length of entity name             ## NOTE: Some number greater than the maximum length of entity name
2568             ((0x0041 <= $self->{next_input_character} and # a             ((0x0041 <= $self->{next_char} and # a
2569               $self->{next_input_character} <= 0x005A) or # x               $self->{next_char} <= 0x005A) or # x
2570              (0x0061 <= $self->{next_input_character} and # a              (0x0061 <= $self->{next_char} and # a
2571               $self->{next_input_character} <= 0x007A) or # z               $self->{next_char} <= 0x007A) or # z
2572              (0x0030 <= $self->{next_input_character} and # 0              (0x0030 <= $self->{next_char} and # 0
2573               $self->{next_input_character} <= 0x0039) or # 9               $self->{next_char} <= 0x0039) or # 9
2574              $self->{next_input_character} == 0x003B)) { # ;              $self->{next_char} == 0x003B)) { # ;
2575        $entity_name .= chr $self->{next_input_character};        $entity_name .= chr $self->{next_char};
2576        if (defined $EntityChar->{$entity_name}) {        if (defined $EntityChar->{$entity_name}) {
2577          if ($self->{next_input_character} == 0x003B) { # ;          if ($self->{next_char} == 0x003B) { # ;
2578              !!!cp (1020);
2579            $value = $EntityChar->{$entity_name};            $value = $EntityChar->{$entity_name};
2580            $match = 1;            $match = 1;
2581            !!!next-input-character;            !!!next-input-character;
2582            last;            last;
2583          } else {          } else {
2584              !!!cp (1021);
2585            $value = $EntityChar->{$entity_name};            $value = $EntityChar->{$entity_name};
2586            $match = -1;            $match = -1;
2587            !!!next-input-character;            !!!next-input-character;
2588          }          }
2589        } else {        } else {
2590          $value .= chr $self->{next_input_character};          !!!cp (1022);
2591            $value .= chr $self->{next_char};
2592          $match *= 2;          $match *= 2;
2593          !!!next-input-character;          !!!next-input-character;
2594        }        }
2595      }      }
2596            
2597      if ($match > 0) {      if ($match > 0) {
2598        return {type => 'character', data => $value};        !!!cp (1023);
2599          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
2600                  line => $l, column => $c,
2601                 };
2602      } elsif ($match < 0) {      } elsif ($match < 0) {
2603        !!!parse-error (type => 'no refc');        !!!parse-error (type => 'no refc', line => $l, column => $c);
2604        if ($in_attr and $match < -1) {        if ($in_attr and $match < -1) {
2605          return {type => 'character', data => '&'.$entity_name};          !!!cp (1024);
2606        } else {          return {type => CHARACTER_TOKEN, data => '&'.$entity_name,
2607          return {type => 'character', data => $value};                  line => $l, column => $c,
2608                   };
2609          } else {
2610            !!!cp (1025);
2611            return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
2612                    line => $l, column => $c,
2613                   };
2614        }        }
2615      } else {      } else {
2616        !!!parse-error (type => 'bare ero');        !!!cp (1026);
2617        ## NOTE: No characters are consumed in the spec.        !!!parse-error (type => 'bare ero', line => $l, column => $c);
2618        return {type => 'character', data => '&'.$value};        ## NOTE: "No characters are consumed" in the spec.
2619          return {type => CHARACTER_TOKEN, data => '&'.$value,
2620                  line => $l, column => $c,
2621                 };
2622      }      }
2623    } else {    } else {
2624        !!!cp (1027);
2625      ## no characters are consumed      ## no characters are consumed
2626      !!!parse-error (type => 'bare ero');      !!!parse-error (type => 'bare ero', line => $l, column => $c);
2627      return undef;      return undef;
2628    }    }
2629  } # _tokenize_attempt_to_consume_an_entity  } # _tokenize_attempt_to_consume_an_entity
# Line 1806  sub _construct_tree ($) { Line 2661  sub _construct_tree ($) {
2661        
2662    !!!next-token;    !!!next-token;
2663    
   $self->{insertion_mode} = 'before head';  
2664    undef $self->{form_element};    undef $self->{form_element};
2665    undef $self->{head_element};    undef $self->{head_element};
2666    $self->{open_elements} = [];    $self->{open_elements} = [];
2667    undef $self->{inner_html_node};    undef $self->{inner_html_node};
2668    
2669      ## NOTE: The "initial" insertion mode.
2670    $self->_tree_construction_initial; # MUST    $self->_tree_construction_initial; # MUST
2671    
2672      ## NOTE: The "before html" insertion mode.
2673    $self->_tree_construction_root_element;    $self->_tree_construction_root_element;
2674      $self->{insertion_mode} = BEFORE_HEAD_IM;
2675    
2676      ## NOTE: The "before head" insertion mode and so on.
2677    $self->_tree_construction_main;    $self->_tree_construction_main;
2678  } # _construct_tree  } # _construct_tree
2679    
2680  sub _tree_construction_initial ($) {  sub _tree_construction_initial ($) {
2681    my $self = shift;    my $self = shift;
2682    
2683      ## NOTE: "initial" insertion mode
2684    
2685    INITIAL: {    INITIAL: {
2686      if ($token->{type} eq 'DOCTYPE') {      if ($token->{type} == DOCTYPE_TOKEN) {
2687        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"
2688        ## error, switch to a conformance checking mode for another        ## error, switch to a conformance checking mode for another
2689        ## language.        ## language.
# Line 1830  sub _tree_construction_initial ($) { Line 2693  sub _tree_construction_initial ($) {
2693        if (not defined $token->{name} or # <!DOCTYPE>        if (not defined $token->{name} or # <!DOCTYPE>
2694            defined $token->{public_identifier} or            defined $token->{public_identifier} or
2695            defined $token->{system_identifier}) {            defined $token->{system_identifier}) {
2696          !!!parse-error (type => 'not HTML5');          !!!cp ('t1');
2697            !!!parse-error (type => 'not HTML5', token => $token);
2698        } elsif ($doctype_name ne 'HTML') {        } elsif ($doctype_name ne 'HTML') {
2699            !!!cp ('t2');
2700          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)
2701          !!!parse-error (type => 'not HTML5');          !!!parse-error (type => 'not HTML5', token => $token);
2702          } else {
2703            !!!cp ('t3');
2704        }        }
2705                
2706        my $doctype = $self->{document}->create_document_type_definition        my $doctype = $self->{document}->create_document_type_definition
2707          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
2708          ## NOTE: Default value for both |public_id| and |system_id| attributes
2709          ## are empty strings, so that we don't set any value in missing cases.
2710        $doctype->public_id ($token->{public_identifier})        $doctype->public_id ($token->{public_identifier})
2711            if defined $token->{public_identifier};            if defined $token->{public_identifier};
2712        $doctype->system_id ($token->{system_identifier})        $doctype->system_id ($token->{system_identifier})
# Line 1846  sub _tree_construction_initial ($) { Line 2715  sub _tree_construction_initial ($) {
2715        ## ISSUE: internalSubset = null??        ## ISSUE: internalSubset = null??
2716        $self->{document}->append_child ($doctype);        $self->{document}->append_child ($doctype);
2717                
2718        if (not $token->{correct} or $doctype_name ne 'HTML') {        if ($token->{quirks} or $doctype_name ne 'HTML') {
2719            !!!cp ('t4');
2720          $self->{document}->manakai_compat_mode ('quirks');          $self->{document}->manakai_compat_mode ('quirks');
2721        } elsif (defined $token->{public_identifier}) {        } elsif (defined $token->{public_identifier}) {
2722          my $pubid = $token->{public_identifier};          my $pubid = $token->{public_identifier};
# Line 1900  sub _tree_construction_initial ($) { Line 2770  sub _tree_construction_initial ($) {
2770            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,
2771            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,
2772            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,
2773              "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//EN" => 1,
2774              "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//EN" => 1,
2775              "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//EN" => 1,
2776            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,
2777            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,
2778            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,
# Line 1922  sub _tree_construction_initial ($) { Line 2795  sub _tree_construction_initial ($) {
2795            "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" => 1,            "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" => 1,
2796            "HTML" => 1,            "HTML" => 1,
2797          }->{$pubid}) {          }->{$pubid}) {
2798              !!!cp ('t5');
2799            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
2800          } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or          } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or
2801                   $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {                   $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {
2802            if (defined $token->{system_identifier}) {            if (defined $token->{system_identifier}) {
2803                !!!cp ('t6');
2804              $self->{document}->manakai_compat_mode ('quirks');              $self->{document}->manakai_compat_mode ('quirks');
2805            } else {            } else {
2806                !!!cp ('t7');
2807              $self->{document}->manakai_compat_mode ('limited quirks');              $self->{document}->manakai_compat_mode ('limited quirks');
2808            }            }
2809          } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 Frameset//EN" or          } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 FRAMESET//EN" or
2810                   $pubid eq "-//W3C//DTD XHTML 1.0 Transitional//EN") {                   $pubid eq "-//W3C//DTD XHTML 1.0 TRANSITIONAL//EN") {
2811              !!!cp ('t8');
2812            $self->{document}->manakai_compat_mode ('limited quirks');            $self->{document}->manakai_compat_mode ('limited quirks');
2813            } else {
2814              !!!cp ('t9');
2815          }          }
2816          } else {
2817            !!!cp ('t10');
2818        }        }
2819        if (defined $token->{system_identifier}) {        if (defined $token->{system_identifier}) {
2820          my $sysid = $token->{system_identifier};          my $sysid = $token->{system_identifier};
2821          $sysid =~ tr/A-Z/a-z/;          $sysid =~ tr/A-Z/a-z/;
2822          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") {
2823              ## TODO: Check the spec: PUBLIC "(limited quirks)" "(quirks)"
2824            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
2825              !!!cp ('t11');
2826            } else {
2827              !!!cp ('t12');
2828          }          }
2829          } else {
2830            !!!cp ('t13');
2831        }        }
2832                
2833        ## Go to the root element phase.        ## Go to the "before html" insertion mode.
2834        !!!next-token;        !!!next-token;
2835        return;        return;
2836      } elsif ({      } elsif ({
2837                'start tag' => 1,                START_TAG_TOKEN, 1,
2838                'end tag' => 1,                END_TAG_TOKEN, 1,
2839                'end-of-file' => 1,                END_OF_FILE_TOKEN, 1,
2840               }->{$token->{type}}) {               }->{$token->{type}}) {
2841        !!!parse-error (type => 'no DOCTYPE');        !!!cp ('t14');
2842          !!!parse-error (type => 'no DOCTYPE', token => $token);
2843        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
2844        ## Go to the root element phase        ## Go to the "before html" insertion mode.
2845        ## reprocess        ## reprocess
2846        return;        return;
2847      } elsif ($token->{type} eq 'character') {      } elsif ($token->{type} == CHARACTER_TOKEN) {
2848        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
2849          ## Ignore the token          ## Ignore the token
2850    
2851          unless (length $token->{data}) {          unless (length $token->{data}) {
2852            ## Stay in the phase            !!!cp ('t15');
2853              ## Stay in the insertion mode.
2854            !!!next-token;            !!!next-token;
2855            redo INITIAL;            redo INITIAL;
2856            } else {
2857              !!!cp ('t16');
2858          }          }
2859          } else {
2860            !!!cp ('t17');
2861        }        }
2862    
2863        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE', token => $token);
2864        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
2865        ## Go to the root element phase        ## Go to the "before html" insertion mode.
2866        ## reprocess        ## reprocess
2867        return;        return;
2868      } elsif ($token->{type} eq 'comment') {      } elsif ($token->{type} == COMMENT_TOKEN) {
2869          !!!cp ('t18');
2870        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
2871        $self->{document}->append_child ($comment);        $self->{document}->append_child ($comment);
2872                
2873        ## Stay in the phase.        ## Stay in the insertion mode.
2874        !!!next-token;        !!!next-token;
2875        redo INITIAL;        redo INITIAL;
2876      } else {      } else {
2877        die "$0: $token->{type}: Unknown token";        die "$0: $token->{type}: Unknown token type";
2878      }      }
2879    } # INITIAL    } # INITIAL
2880    
2881      die "$0: _tree_construction_initial: This should be never reached";
2882  } # _tree_construction_initial  } # _tree_construction_initial
2883    
2884  sub _tree_construction_root_element ($) {  sub _tree_construction_root_element ($) {
2885    my $self = shift;    my $self = shift;
2886    
2887      ## NOTE: "before html" insertion mode.
2888        
2889    B: {    B: {
2890        if ($token->{type} eq 'DOCTYPE') {        if ($token->{type} == DOCTYPE_TOKEN) {
2891          !!!parse-error (type => 'in html:#DOCTYPE');          !!!cp ('t19');
2892            !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
2893          ## Ignore the token          ## Ignore the token
2894          ## Stay in the phase          ## Stay in the insertion mode.
2895          !!!next-token;          !!!next-token;
2896          redo B;          redo B;
2897        } elsif ($token->{type} eq 'comment') {        } elsif ($token->{type} == COMMENT_TOKEN) {
2898            !!!cp ('t20');
2899          my $comment = $self->{document}->create_comment ($token->{data});          my $comment = $self->{document}->create_comment ($token->{data});
2900          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
2901          ## Stay in the phase          ## Stay in the insertion mode.
2902          !!!next-token;          !!!next-token;
2903          redo B;          redo B;
2904        } elsif ($token->{type} eq 'character') {        } elsif ($token->{type} == CHARACTER_TOKEN) {
2905          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
2906            ## Ignore the token.            ## Ignore the token.
2907    
2908            unless (length $token->{data}) {            unless (length $token->{data}) {
2909              ## Stay in the phase              !!!cp ('t21');
2910                ## Stay in the insertion mode.
2911              !!!next-token;              !!!next-token;
2912              redo B;              redo B;
2913              } else {
2914                !!!cp ('t22');
2915            }            }
2916            } else {
2917              !!!cp ('t23');
2918          }          }
2919    
2920            $self->{application_cache_selection}->(undef);
2921    
2922          #          #
2923          } elsif ($token->{type} == START_TAG_TOKEN) {
2924            if ($token->{tag_name} eq 'html') {
2925              my $root_element;
2926              !!!create-element ($root_element, $token->{tag_name}, $token->{attributes}, $token);
2927              $self->{document}->append_child ($root_element);
2928              push @{$self->{open_elements}},
2929                  [$root_element, $el_category->{html}];
2930    
2931              if ($token->{attributes}->{manifest}) {
2932                !!!cp ('t24');
2933                $self->{application_cache_selection}
2934                    ->($token->{attributes}->{manifest}->{value});
2935                ## ISSUE: Spec is unclear on relative references.
2936                ## According to Hixie (#whatwg 2008-03-19), it should be
2937                ## resolved against the base URI of the document in HTML
2938                ## or xml:base of the element in XHTML.
2939              } else {
2940                !!!cp ('t25');
2941                $self->{application_cache_selection}->(undef);
2942              }
2943    
2944              !!!next-token;
2945              return; ## Go to the "before head" insertion mode.
2946            } else {
2947              !!!cp ('t25.1');
2948              #
2949            }
2950        } elsif ({        } elsif ({
2951                  'start tag' => 1,                  END_TAG_TOKEN, 1,
2952                  'end tag' => 1,                  END_OF_FILE_TOKEN, 1,
                 'end-of-file' => 1,  
2953                 }->{$token->{type}}) {                 }->{$token->{type}}) {
2954          ## ISSUE: There is an issue in the spec          !!!cp ('t26');
2955          #          #
2956        } else {        } else {
2957          die "$0: $token->{type}: Unknown token";          die "$0: $token->{type}: Unknown token type";
2958        }        }
2959        my $root_element; !!!create-element ($root_element, 'html');  
2960        $self->{document}->append_child ($root_element);      my $root_element; !!!create-element ($root_element, 'html',, $token);
2961        push @{$self->{open_elements}}, [$root_element, 'html'];      $self->{document}->append_child ($root_element);
2962        ## reprocess      push @{$self->{open_elements}}, [$root_element, $el_category->{html}];
2963        #redo B;  
2964        return; ## Go to the main phase.      $self->{application_cache_selection}->(undef);
2965    
2966        ## NOTE: Reprocess the token.
2967        return; ## Go to the "before head" insertion mode.
2968    
2969        ## ISSUE: There is an issue in the spec
2970    } # B    } # B
2971    
2972      die "$0: _tree_construction_root_element: This should never be reached";
2973  } # _tree_construction_root_element  } # _tree_construction_root_element
2974    
2975  sub _reset_insertion_mode ($) {  sub _reset_insertion_mode ($) {
# Line 2043  sub _reset_insertion_mode ($) { Line 2984  sub _reset_insertion_mode ($) {
2984            
2985      ## Step 3      ## Step 3
2986      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"!?  
2987        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
2988          $last = 1;          $last = 1;
2989          if (defined $self->{inner_html_node}) {          if (defined $self->{inner_html_node}) {
2990            if ($self->{inner_html_node}->[1] eq 'td' or            if ($self->{inner_html_node}->[1] & TABLE_CELL_EL) {
2991                $self->{inner_html_node}->[1] eq 'th') {              !!!cp ('t27');
2992              #              #
2993            } else {            } else {
2994                !!!cp ('t28');
2995              $node = $self->{inner_html_node};              $node = $self->{inner_html_node};
2996            }            }
2997          }          }
# Line 2062  sub _reset_insertion_mode ($) { Line 2999  sub _reset_insertion_mode ($) {
2999            
3000        ## Step 4..13        ## Step 4..13
3001        my $new_mode = {        my $new_mode = {
3002                        select => 'in select',                        select => IN_SELECT_IM,
3003                        td => 'in cell',                        ## NOTE: |option| and |optgroup| do not set
3004                        th => 'in cell',                        ## insertion mode to "in select" by themselves.
3005                        tr => 'in row',                        td => IN_CELL_IM,
3006                        tbody => 'in table body',                        th => IN_CELL_IM,
3007                        thead => 'in table head',                        tr => IN_ROW_IM,
3008                        tfoot => 'in table foot',                        tbody => IN_TABLE_BODY_IM,
3009                        caption => 'in caption',                        thead => IN_TABLE_BODY_IM,
3010                        colgroup => 'in column group',                        tfoot => IN_TABLE_BODY_IM,
3011                        table => 'in table',                        caption => IN_CAPTION_IM,
3012                        head => 'in body', # not in head!                        colgroup => IN_COLUMN_GROUP_IM,
3013                        body => 'in body',                        table => IN_TABLE_IM,
3014                        frameset => 'in frameset',                        head => IN_BODY_IM, # not in head!
3015                       }->{$node->[1]};                        body => IN_BODY_IM,
3016                          frameset => IN_FRAMESET_IM,
3017                         }->{$node->[0]->manakai_local_name};
3018                         ## TODO: Foreign namespace case OK?
3019        $self->{insertion_mode} = $new_mode and return if defined $new_mode;        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
3020                
3021        ## Step 14        ## Step 14
3022        if ($node->[1] eq 'html') {        if ($node->[1] & HTML_EL) {
3023          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
3024            $self->{insertion_mode} = 'before head';            !!!cp ('t29');
3025              $self->{insertion_mode} = BEFORE_HEAD_IM;
3026          } else {          } else {
3027            $self->{insertion_mode} = 'after head';            ## ISSUE: Can this state be reached?
3028              !!!cp ('t30');
3029              $self->{insertion_mode} = AFTER_HEAD_IM;
3030          }          }
3031          return;          return;
3032          } else {
3033            !!!cp ('t31');
3034        }        }
3035                
3036        ## Step 15        ## Step 15
3037        $self->{insertion_mode} = 'in body' and return if $last;        $self->{insertion_mode} = IN_BODY_IM and return if $last;
3038                
3039        ## Step 16        ## Step 16
3040        $i--;        $i--;
# Line 2098  sub _reset_insertion_mode ($) { Line 3043  sub _reset_insertion_mode ($) {
3043        ## Step 17        ## Step 17
3044        redo S3;        redo S3;
3045      } # S3      } # S3
3046    
3047      die "$0: _reset_insertion_mode: This line should never be reached";
3048  } # _reset_insertion_mode  } # _reset_insertion_mode
3049    
3050  sub _tree_construction_main ($) {  sub _tree_construction_main ($) {
3051    my $self = shift;    my $self = shift;
3052    
   my $previous_insertion_mode;  
   
3053    my $active_formatting_elements = [];    my $active_formatting_elements = [];
3054    
3055    my $reconstruct_active_formatting_elements = sub { # MUST    my $reconstruct_active_formatting_elements = sub { # MUST
# Line 2121  sub _tree_construction_main ($) { Line 3066  sub _tree_construction_main ($) {
3066      return if $entry->[0] eq '#marker';      return if $entry->[0] eq '#marker';
3067      for (@{$self->{open_elements}}) {      for (@{$self->{open_elements}}) {
3068        if ($entry->[0] eq $_->[0]) {        if ($entry->[0] eq $_->[0]) {
3069            !!!cp ('t32');
3070          return;          return;
3071        }        }
3072      }      }
# Line 2135  sub _tree_construction_main ($) { Line 3081  sub _tree_construction_main ($) {
3081    
3082        ## Step 6        ## Step 6
3083        if ($entry->[0] eq '#marker') {        if ($entry->[0] eq '#marker') {
3084            !!!cp ('t33_1');
3085          #          #
3086        } else {        } else {
3087          my $in_open_elements;          my $in_open_elements;
3088          OE: for (@{$self->{open_elements}}) {          OE: for (@{$self->{open_elements}}) {
3089            if ($entry->[0] eq $_->[0]) {            if ($entry->[0] eq $_->[0]) {
3090                !!!cp ('t33');
3091              $in_open_elements = 1;              $in_open_elements = 1;
3092              last OE;              last OE;
3093            }            }
3094          }          }
3095          if ($in_open_elements) {          if ($in_open_elements) {
3096              !!!cp ('t34');
3097            #            #
3098          } else {          } else {
3099              ## NOTE: <!DOCTYPE HTML><p><b><i><u></p> <p>X
3100              !!!cp ('t35');
3101            redo S4;            redo S4;
3102          }          }
3103        }        }
# Line 2169  sub _tree_construction_main ($) { Line 3120  sub _tree_construction_main ($) {
3120    
3121        ## Step 11        ## Step 11
3122        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {
3123            !!!cp ('t36');
3124          ## Step 7'          ## Step 7'
3125          $i++;          $i++;
3126          $entry = $active_formatting_elements->[$i];          $entry = $active_formatting_elements->[$i];
3127                    
3128          redo S7;          redo S7;
3129        }        }
3130    
3131          !!!cp ('t37');
3132      } # S7      } # S7
3133    }; # $reconstruct_active_formatting_elements    }; # $reconstruct_active_formatting_elements
3134    
3135    my $clear_up_to_marker = sub {    my $clear_up_to_marker = sub {
3136      for (reverse 0..$#$active_formatting_elements) {      for (reverse 0..$#$active_formatting_elements) {
3137        if ($active_formatting_elements->[$_]->[0] eq '#marker') {        if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3138            !!!cp ('t38');
3139          splice @$active_formatting_elements, $_;          splice @$active_formatting_elements, $_;
3140          return;          return;
3141        }        }
3142      }      }
3143    
3144        !!!cp ('t39');
3145    }; # $clear_up_to_marker    }; # $clear_up_to_marker
3146    
3147    my $parse_rcdata = sub ($$) {    my $insert;
3148      my ($content_model_flag, $insert) = @_;  
3149      my $parse_rcdata = sub ($) {
3150        my ($content_model_flag) = @_;
3151    
3152      ## Step 1      ## Step 1
3153      my $start_tag_name = $token->{tag_name};      my $start_tag_name = $token->{tag_name};
3154      my $el;      my $el;
3155      !!!create-element ($el, $start_tag_name, $token->{attributes});      !!!create-element ($el, $start_tag_name, $token->{attributes}, $token);
3156    
3157      ## Step 2      ## Step 2
3158      $insert->($el); # /context node/->append_child ($el)      $insert->($el);
3159    
3160      ## Step 3      ## Step 3
3161      $self->{content_model} = $content_model_flag; # CDATA or RCDATA      $self->{content_model} = $content_model_flag; # CDATA or RCDATA
# Line 2205  sub _tree_construction_main ($) { Line 3164  sub _tree_construction_main ($) {
3164      ## Step 4      ## Step 4
3165      my $text = '';      my $text = '';
3166      !!!next-token;      !!!next-token;
3167      while ($token->{type} eq 'character') { # or until stop tokenizing      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
3168          !!!cp ('t40');
3169        $text .= $token->{data};        $text .= $token->{data};
3170        !!!next-token;        !!!next-token;
3171      }      }
3172    
3173      ## Step 5      ## Step 5
3174      if (length $text) {      if (length $text) {
3175          !!!cp ('t41');
3176        my $text = $self->{document}->create_text_node ($text);        my $text = $self->{document}->create_text_node ($text);
3177        $el->append_child ($text);        $el->append_child ($text);
3178      }      }
# Line 2220  sub _tree_construction_main ($) { Line 3181  sub _tree_construction_main ($) {
3181      $self->{content_model} = PCDATA_CONTENT_MODEL;      $self->{content_model} = PCDATA_CONTENT_MODEL;
3182    
3183      ## Step 7      ## Step 7
3184      if ($token->{type} eq 'end tag' and $token->{tag_name} eq $start_tag_name) {      if ($token->{type} == END_TAG_TOKEN and
3185            $token->{tag_name} eq $start_tag_name) {
3186          !!!cp ('t42');
3187        ## 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});  
3188      } else {      } else {
3189        die "$0: $content_model_flag in parse_rcdata";        ## NOTE: An end-of-file token.
3190          if ($content_model_flag == CDATA_CONTENT_MODEL) {
3191            !!!cp ('t43');
3192            !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);
3193          } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
3194            !!!cp ('t44');
3195            !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);
3196          } else {
3197            die "$0: $content_model_flag in parse_rcdata";
3198          }
3199      }      }
3200      !!!next-token;      !!!next-token;
3201    }; # $parse_rcdata    }; # $parse_rcdata
3202    
3203    my $script_start_tag = sub ($) {    my $script_start_tag = sub () {
     my $insert = $_[0];  
3204      my $script_el;      my $script_el;
3205      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, 'script', $token->{attributes}, $token);
3206      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
3207    
3208      $self->{content_model} = CDATA_CONTENT_MODEL;      $self->{content_model} = CDATA_CONTENT_MODEL;
# Line 2243  sub _tree_construction_main ($) { Line 3210  sub _tree_construction_main ($) {
3210            
3211      my $text = '';      my $text = '';
3212      !!!next-token;      !!!next-token;
3213      while ($token->{type} eq 'character') {      while ($token->{type} == CHARACTER_TOKEN) {
3214          !!!cp ('t45');
3215        $text .= $token->{data};        $text .= $token->{data};
3216        !!!next-token;        !!!next-token;
3217      } # stop if non-character token or tokenizer stops tokenising      } # stop if non-character token or tokenizer stops tokenising
3218      if (length $text) {      if (length $text) {
3219          !!!cp ('t46');
3220        $script_el->manakai_append_text ($text);        $script_el->manakai_append_text ($text);
3221      }      }
3222                                
3223      $self->{content_model} = PCDATA_CONTENT_MODEL;      $self->{content_model} = PCDATA_CONTENT_MODEL;
3224    
3225      if ($token->{type} eq 'end tag' and      if ($token->{type} == END_TAG_TOKEN and
3226          $token->{tag_name} eq 'script') {          $token->{tag_name} eq 'script') {
3227          !!!cp ('t47');
3228        ## Ignore the token        ## Ignore the token
3229      } else {      } else {
3230        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!cp ('t48');
3231          !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);
3232        ## ISSUE: And ignore?        ## ISSUE: And ignore?
3233        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
3234      }      }
3235            
3236      if (defined $self->{inner_html_node}) {      if (defined $self->{inner_html_node}) {
3237          !!!cp ('t49');
3238        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
3239      } else {      } else {
3240          !!!cp ('t50');
3241        ## TODO: $old_insertion_point = current insertion point        ## TODO: $old_insertion_point = current insertion point
3242        ## TODO: insertion point = just before the next input character        ## TODO: insertion point = just before the next input character
3243    
# Line 2278  sub _tree_construction_main ($) { Line 3251  sub _tree_construction_main ($) {
3251      !!!next-token;      !!!next-token;
3252    }; # $script_start_tag    }; # $script_start_tag
3253    
3254      ## NOTE: $open_tables->[-1]->[0] is the "current table" element node.
3255      ## NOTE: $open_tables->[-1]->[1] is the "tainted" flag.
3256      my $open_tables = [[$self->{open_elements}->[0]->[0]]];
3257    
3258    my $formatting_end_tag = sub {    my $formatting_end_tag = sub {
3259      my $tag_name = shift;      my $end_tag_token = shift;
3260        my $tag_name = $end_tag_token->{tag_name};
3261    
3262        ## NOTE: The adoption agency algorithm (AAA).
3263    
3264      FET: {      FET: {
3265        ## Step 1        ## Step 1
3266        my $formatting_element;        my $formatting_element;
3267        my $formatting_element_i_in_active;        my $formatting_element_i_in_active;
3268        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3269          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {          if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3270              !!!cp ('t52');
3271              last AFE;
3272            } elsif ($active_formatting_elements->[$_]->[0]->manakai_local_name
3273                         eq $tag_name) {
3274              !!!cp ('t51');
3275            $formatting_element = $active_formatting_elements->[$_];            $formatting_element = $active_formatting_elements->[$_];
3276            $formatting_element_i_in_active = $_;            $formatting_element_i_in_active = $_;
3277            last AFE;            last AFE;
         } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {  
           last AFE;  
3278          }          }
3279        } # AFE        } # AFE
3280        unless (defined $formatting_element) {        unless (defined $formatting_element) {
3281          !!!parse-error (type => 'unmatched end tag:'.$tag_name);          !!!cp ('t53');
3282            !!!parse-error (type => 'unmatched end tag:'.$tag_name, token => $end_tag_token);
3283          ## Ignore the token          ## Ignore the token
3284          !!!next-token;          !!!next-token;
3285          return;          return;
# Line 2307  sub _tree_construction_main ($) { Line 3291  sub _tree_construction_main ($) {
3291          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
3292          if ($node->[0] eq $formatting_element->[0]) {          if ($node->[0] eq $formatting_element->[0]) {
3293            if ($in_scope) {            if ($in_scope) {
3294                !!!cp ('t54');
3295              $formatting_element_i_in_open = $_;              $formatting_element_i_in_open = $_;
3296              last INSCOPE;              last INSCOPE;
3297            } else { # in open elements but not in scope            } else { # in open elements but not in scope
3298              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t55');
3299                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name},
3300                                token => $end_tag_token);
3301              ## Ignore the token              ## Ignore the token
3302              !!!next-token;              !!!next-token;
3303              return;              return;
3304            }            }
3305          } elsif ({          } elsif ($node->[1] & SCOPING_EL) {
3306                    table => 1, caption => 1, td => 1, th => 1,            !!!cp ('t56');
                   button => 1, marquee => 1, object => 1, html => 1,  
                  }->{$node->[1]}) {  
3307            $in_scope = 0;            $in_scope = 0;
3308          }          }
3309        } # INSCOPE        } # INSCOPE
3310        unless (defined $formatting_element_i_in_open) {        unless (defined $formatting_element_i_in_open) {
3311          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!cp ('t57');
3312            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name},
3313                            token => $end_tag_token);
3314          pop @$active_formatting_elements; # $formatting_element          pop @$active_formatting_elements; # $formatting_element
3315          !!!next-token; ## TODO: ok?          !!!next-token; ## TODO: ok?
3316          return;          return;
3317        }        }
3318        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
3319          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);          !!!cp ('t58');
3320            !!!parse-error (type => 'not closed',
3321                            value => $self->{open_elements}->[-1]->[0]
3322                                ->manakai_local_name,
3323                            token => $end_tag_token);
3324        }        }
3325                
3326        ## Step 2        ## Step 2
# Line 2337  sub _tree_construction_main ($) { Line 3328  sub _tree_construction_main ($) {
3328        my $furthest_block_i_in_open;        my $furthest_block_i_in_open;
3329        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3330          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
3331          if (not $formatting_category->{$node->[1]} and          if (not ($node->[1] & FORMATTING_EL) and
3332              #not $phrasing_category->{$node->[1]} and              #not $phrasing_category->{$node->[1]} and
3333              ($special_category->{$node->[1]} or              ($node->[1] & SPECIAL_EL or
3334               $scoping_category->{$node->[1]})) {               $node->[1] & SCOPING_EL)) { ## Scoping is redundant, maybe
3335              !!!cp ('t59');
3336            $furthest_block = $node;            $furthest_block = $node;
3337            $furthest_block_i_in_open = $_;            $furthest_block_i_in_open = $_;
3338          } elsif ($node->[0] eq $formatting_element->[0]) {          } elsif ($node->[0] eq $formatting_element->[0]) {
3339              !!!cp ('t60');
3340            last OE;            last OE;
3341          }          }
3342        } # OE        } # OE
3343                
3344        ## Step 3        ## Step 3
3345        unless (defined $furthest_block) { # MUST        unless (defined $furthest_block) { # MUST
3346            !!!cp ('t61');
3347          splice @{$self->{open_elements}}, $formatting_element_i_in_open;          splice @{$self->{open_elements}}, $formatting_element_i_in_open;
3348          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;
3349          !!!next-token;          !!!next-token;
# Line 2362  sub _tree_construction_main ($) { Line 3356  sub _tree_construction_main ($) {
3356        ## Step 5        ## Step 5
3357        my $furthest_block_parent = $furthest_block->[0]->parent_node;        my $furthest_block_parent = $furthest_block->[0]->parent_node;
3358        if (defined $furthest_block_parent) {        if (defined $furthest_block_parent) {
3359            !!!cp ('t62');
3360          $furthest_block_parent->remove_child ($furthest_block->[0]);          $furthest_block_parent->remove_child ($furthest_block->[0]);
3361        }        }
3362                
# Line 2384  sub _tree_construction_main ($) { Line 3379  sub _tree_construction_main ($) {
3379          S7S2: {          S7S2: {
3380            for (reverse 0..$#$active_formatting_elements) {            for (reverse 0..$#$active_formatting_elements) {
3381              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
3382                  !!!cp ('t63');
3383                $node_i_in_active = $_;                $node_i_in_active = $_;
3384                last S7S2;                last S7S2;
3385              }              }
# Line 2397  sub _tree_construction_main ($) { Line 3393  sub _tree_construction_main ($) {
3393                    
3394          ## Step 4          ## Step 4
3395          if ($last_node->[0] eq $furthest_block->[0]) {          if ($last_node->[0] eq $furthest_block->[0]) {
3396              !!!cp ('t64');
3397            $bookmark_prev_el = $node->[0];            $bookmark_prev_el = $node->[0];
3398          }          }
3399                    
3400          ## Step 5          ## Step 5
3401          if ($node->[0]->has_child_nodes ()) {          if ($node->[0]->has_child_nodes ()) {
3402              !!!cp ('t65');
3403            my $clone = [$node->[0]->clone_node (0), $node->[1]];            my $clone = [$node->[0]->clone_node (0), $node->[1]];
3404            $active_formatting_elements->[$node_i_in_active] = $clone;            $active_formatting_elements->[$node_i_in_active] = $clone;
3405            $self->{open_elements}->[$node_i_in_open] = $clone;            $self->{open_elements}->[$node_i_in_open] = $clone;
# Line 2419  sub _tree_construction_main ($) { Line 3417  sub _tree_construction_main ($) {
3417        } # S7          } # S7  
3418                
3419        ## Step 8        ## Step 8
3420        $common_ancestor_node->[0]->append_child ($last_node->[0]);        if ($common_ancestor_node->[1] & TABLE_ROWS_EL) {
3421            my $foster_parent_element;
3422            my $next_sibling;
3423            OE: for (reverse 0..$#{$self->{open_elements}}) {
3424              if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
3425                                 my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3426                                 if (defined $parent and $parent->node_type == 1) {
3427                                   !!!cp ('t65.1');
3428                                   $foster_parent_element = $parent;
3429                                   $next_sibling = $self->{open_elements}->[$_]->[0];
3430                                 } else {
3431                                   !!!cp ('t65.2');
3432                                   $foster_parent_element
3433                                     = $self->{open_elements}->[$_ - 1]->[0];
3434                                 }
3435                                 last OE;
3436                               }
3437                             } # OE
3438                             $foster_parent_element = $self->{open_elements}->[0]->[0]
3439                               unless defined $foster_parent_element;
3440            $foster_parent_element->insert_before ($last_node->[0], $next_sibling);
3441            $open_tables->[-1]->[1] = 1; # tainted
3442          } else {
3443            !!!cp ('t65.3');
3444            $common_ancestor_node->[0]->append_child ($last_node->[0]);
3445          }
3446                
3447        ## Step 9        ## Step 9
3448        my $clone = [$formatting_element->[0]->clone_node (0),        my $clone = [$formatting_element->[0]->clone_node (0),
# Line 2436  sub _tree_construction_main ($) { Line 3459  sub _tree_construction_main ($) {
3459        my $i;        my $i;
3460        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3461          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {
3462              !!!cp ('t66');
3463            splice @$active_formatting_elements, $_, 1;            splice @$active_formatting_elements, $_, 1;
3464            $i-- and last AFE if defined $i;            $i-- and last AFE if defined $i;
3465          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {
3466              !!!cp ('t67');
3467            $i = $_;            $i = $_;
3468          }          }
3469        } # AFE        } # AFE
# Line 2448  sub _tree_construction_main ($) { Line 3473  sub _tree_construction_main ($) {
3473        undef $i;        undef $i;
3474        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3475          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {
3476              !!!cp ('t68');
3477            splice @{$self->{open_elements}}, $_, 1;            splice @{$self->{open_elements}}, $_, 1;
3478            $i-- and last OE if defined $i;            $i-- and last OE if defined $i;
3479          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {
3480              !!!cp ('t69');
3481            $i = $_;            $i = $_;
3482          }          }
3483        } # OE        } # OE
# Line 2461  sub _tree_construction_main ($) { Line 3488  sub _tree_construction_main ($) {
3488      } # FET      } # FET
3489    }; # $formatting_end_tag    }; # $formatting_end_tag
3490    
3491    my $insert_to_current = sub {    $insert = my $insert_to_current = sub {
3492      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);
3493    }; # $insert_to_current    }; # $insert_to_current
3494    
3495    my $insert_to_foster = sub {    my $insert_to_foster = sub {
3496                         my $child = shift;      my $child = shift;
3497                         if ({      if ($self->{open_elements}->[-1]->[1] & TABLE_ROWS_EL) {
3498                              table => 1, tbody => 1, tfoot => 1,        # MUST
3499                              thead => 1, tr => 1,        my $foster_parent_element;
3500                             }->{$self->{open_elements}->[-1]->[1]}) {        my $next_sibling;
3501                           # MUST        OE: for (reverse 0..$#{$self->{open_elements}}) {
3502                           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') {  
3503                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3504                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
3505                                   !!!cp ('t70');
3506                                 $foster_parent_element = $parent;                                 $foster_parent_element = $parent;
3507                                 $next_sibling = $self->{open_elements}->[$_]->[0];                                 $next_sibling = $self->{open_elements}->[$_]->[0];
3508                               } else {                               } else {
3509                                   !!!cp ('t71');
3510                                 $foster_parent_element                                 $foster_parent_element
3511                                   = $self->{open_elements}->[$_ - 1]->[0];                                   = $self->{open_elements}->[$_ - 1]->[0];
3512                               }                               }
# Line 2491  sub _tree_construction_main ($) { Line 3517  sub _tree_construction_main ($) {
3517                             unless defined $foster_parent_element;                             unless defined $foster_parent_element;
3518                           $foster_parent_element->insert_before                           $foster_parent_element->insert_before
3519                             ($child, $next_sibling);                             ($child, $next_sibling);
3520                         } else {        $open_tables->[-1]->[1] = 1; # tainted
3521                           $self->{open_elements}->[-1]->[0]->append_child ($child);      } else {
3522                         }        !!!cp ('t72');
3523    }; # $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}}) {  
         ## NOTE: There are two "as if in body" code clones.  
         $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;  
       }  
3524      }      }
3525    }; # $in_body    }; # $insert_to_foster
3526    
3527    B: {    B: {
3528      if ($token->{type} eq 'DOCTYPE') {      if ($token->{type} == DOCTYPE_TOKEN) {
3529        !!!parse-error (type => 'DOCTYPE in the middle');        !!!cp ('t73');
3530          !!!parse-error (type => 'DOCTYPE in the middle', token => $token);
3531        ## Ignore the token        ## Ignore the token
3532        ## Stay in the phase        ## Stay in the phase
3533        !!!next-token;        !!!next-token;
3534        redo B;        redo B;
3535      } 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  
3536               $token->{tag_name} eq 'html') {               $token->{tag_name} eq 'html') {
3537        if ($self->{insertion_mode} eq 'trailing end') {        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
3538          ## Turn into the main phase          !!!cp ('t79');
3539          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html:html', token => $token);
3540          $self->{insertion_mode} = $previous_insertion_mode;          $self->{insertion_mode} = AFTER_BODY_IM;
3541          } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
3542            !!!cp ('t80');
3543            !!!parse-error (type => 'after html:html', token => $token);
3544            $self->{insertion_mode} = AFTER_FRAMESET_IM;
3545          } else {
3546            !!!cp ('t81');
3547        }        }
3548    
3549  ## ISSUE: "aa<html>" is not a parse error.        !!!cp ('t82');
3550  ## 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');  
       }  
3551        my $top_el = $self->{open_elements}->[0]->[0];        my $top_el = $self->{open_elements}->[0]->[0];
3552        for my $attr_name (keys %{$token->{attributes}}) {        for my $attr_name (keys %{$token->{attributes}}) {
3553          unless ($top_el->has_attribute_ns (undef, $attr_name)) {          unless ($top_el->has_attribute_ns (undef, $attr_name)) {
3554              !!!cp ('t84');
3555            $top_el->set_attribute_ns            $top_el->set_attribute_ns
3556              (undef, [undef, $attr_name],              (undef, [undef, $attr_name],
3557               $token->{attributes}->{$attr_name}->{value});               $token->{attributes}->{$attr_name}->{value});
# Line 3398  sub _tree_construction_main ($) { Line 3559  sub _tree_construction_main ($) {
3559        }        }
3560        !!!next-token;        !!!next-token;
3561        redo B;        redo B;
3562      } elsif ($token->{type} eq 'comment') {      } elsif ($token->{type} == COMMENT_TOKEN) {
3563        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
3564        if ($self->{insertion_mode} eq 'trailing end') {        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
3565            !!!cp ('t85');
3566          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
3567        } elsif ($self->{insertion_mode} eq 'after body') {        } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {
3568            !!!cp ('t86');
3569          $self->{open_elements}->[0]->[0]->append_child ($comment);          $self->{open_elements}->[0]->[0]->append_child ($comment);
3570        } else {        } else {
3571            !!!cp ('t87');
3572          $self->{open_elements}->[-1]->[0]->append_child ($comment);          $self->{open_elements}->[-1]->[0]->append_child ($comment);
3573        }        }
3574        !!!next-token;        !!!next-token;
3575        redo B;        redo B;
3576      } elsif ($self->{insertion_mode} eq 'before head') {      } elsif ($self->{insertion_mode} & HEAD_IMS) {
3577            if ($token->{type} eq 'character') {        if ($token->{type} == CHARACTER_TOKEN) {
3578              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
3579                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3580                unless (length $token->{data}) {              !!!cp ('t88.2');
3581                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
3582              } else {
3583                !!!cp ('t88.1');
3584                ## Ignore the token.
3585                !!!next-token;
3586                redo B;
3587              }
3588              unless (length $token->{data}) {
3589                !!!cp ('t88');
3590                !!!next-token;
3591                redo B;
3592              }
3593            }
3594    
3595            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3596              !!!cp ('t89');
3597              ## As if <head>
3598              !!!create-element ($self->{head_element}, 'head',, $token);
3599              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3600              push @{$self->{open_elements}},
3601                  [$self->{head_element}, $el_category->{head}];
3602    
3603              ## Reprocess in the "in head" insertion mode...
3604              pop @{$self->{open_elements}};
3605    
3606              ## Reprocess in the "after head" insertion mode...
3607            } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3608              !!!cp ('t90');
3609              ## As if </noscript>
3610              pop @{$self->{open_elements}};
3611              !!!parse-error (type => 'in noscript:#character', token => $token);
3612              
3613              ## Reprocess in the "in head" insertion mode...
3614              ## As if </head>
3615              pop @{$self->{open_elements}};
3616    
3617              ## Reprocess in the "after head" insertion mode...
3618            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3619              !!!cp ('t91');
3620              pop @{$self->{open_elements}};
3621    
3622              ## Reprocess in the "after head" insertion mode...
3623            } else {
3624              !!!cp ('t92');
3625            }
3626    
3627            ## "after head" insertion mode
3628            ## As if <body>
3629            !!!insert-element ('body',, $token);
3630            $self->{insertion_mode} = IN_BODY_IM;
3631            ## reprocess
3632            redo B;
3633          } elsif ($token->{type} == START_TAG_TOKEN) {
3634            if ($token->{tag_name} eq 'head') {
3635              if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3636                !!!cp ('t93');
3637                !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes}, $token);
3638                $self->{open_elements}->[-1]->[0]->append_child
3639                    ($self->{head_element});
3640                push @{$self->{open_elements}},
3641                    [$self->{head_element}, $el_category->{head}];
3642                $self->{insertion_mode} = IN_HEAD_IM;
3643                !!!next-token;
3644                redo B;
3645                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
3646                    !!!cp ('t94');
3647                    #
3648                  } else {
3649                    !!!cp ('t95');
3650                    !!!parse-error (type => 'in head:head', token => $token); # or in head noscript
3651                    ## Ignore the token
3652                  !!!next-token;                  !!!next-token;
3653                  redo B;                  redo B;
3654                }                }
3655              }              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3656              ## As if <head>                !!!cp ('t96');
             !!!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  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             my $attr = $token->{tag_name} eq 'head' ? $token->{attributes} : {};  
             !!!create-element ($self->{head_element}, 'head', $attr);  
             $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});  
             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  
             }  
             redo B;  
           } elsif ($token->{type} eq 'end tag') {  
             if ({  
                  head => 1, body => 1, html => 1,  
                  p => 1, br => 1,  
                 }->{$token->{tag_name}}) {  
3657                ## As if <head>                ## As if <head>
3658                !!!create-element ($self->{head_element}, 'head');                !!!create-element ($self->{head_element}, 'head',, $token);
3659                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3660                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                push @{$self->{open_elements}},
3661                $self->{insertion_mode} = 'in head';                    [$self->{head_element}, $el_category->{head}];
3662                ## reprocess  
3663                redo B;                $self->{insertion_mode} = IN_HEAD_IM;
3664                  ## Reprocess in the "in head" insertion mode...
3665              } else {              } else {
3666                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!cp ('t97');
               ## Ignore the token ## ISSUE: An issue in the spec.  
               !!!next-token;  
               redo B;  
3667              }              }
3668            } else {  
3669              die "$0: $token->{type}: Unknown type";              if ($token->{tag_name} eq 'base') {
3670            }                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3671          } elsif ($self->{insertion_mode} eq 'in head' or                  !!!cp ('t98');
3672                   $self->{insertion_mode} eq 'in head noscript' or                  ## As if </noscript>
3673                   $self->{insertion_mode} eq 'after head') {                  pop @{$self->{open_elements}};
3674            if ($token->{type} eq 'character') {                  !!!parse-error (type => 'in noscript:base', token => $token);
3675              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {                
3676                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                  $self->{insertion_mode} = IN_HEAD_IM;
3677                unless (length $token->{data}) {                  ## Reprocess in the "in head" insertion mode...
3678                  !!!next-token;                } else {
3679                  redo B;                  !!!cp ('t99');
3680                }                }
3681              }  
               
             #  
           } 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}}) {  
3682                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3683                if ($self->{insertion_mode} eq 'after head') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3684                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t100');
3685                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
3686                    push @{$self->{open_elements}},
3687                        [$self->{head_element}, $el_category->{head}];
3688                  } else {
3689                    !!!cp ('t101');
3690                }                }
3691                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
3692                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
3693                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
3694                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
3695                !!!next-token;                !!!next-token;
3696                redo B;                redo B;
3697              } elsif ($token->{tag_name} eq 'meta') {              } elsif ($token->{tag_name} eq 'link') {
3698                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3699                if ($self->{insertion_mode} eq 'after head') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3700                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t102');
3701                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
3702                    push @{$self->{open_elements}},
3703                        [$self->{head_element}, $el_category->{head}];
3704                  } else {
3705                    !!!cp ('t103');
3706                }                }
3707                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
3708                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
3709                  pop @{$self->{open_elements}} # <head>
3710                      if $self->{insertion_mode} == AFTER_HEAD_IM;
3711                  !!!next-token;
3712                  redo B;
3713                } elsif ($token->{tag_name} eq 'meta') {
3714                  ## NOTE: There is a "as if in head" code clone.
3715                  if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3716                    !!!cp ('t104');
3717                    !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
3718                    push @{$self->{open_elements}},
3719                        [$self->{head_element}, $el_category->{head}];
3720                  } else {
3721                    !!!cp ('t105');
3722                  }
3723                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
3724                  my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
3725    
3726                unless ($self->{confident}) {                unless ($self->{confident}) {
                 my $charset;  
3727                  if ($token->{attributes}->{charset}) { ## TODO: And if supported                  if ($token->{attributes}->{charset}) { ## TODO: And if supported
3728                    $charset = $token->{attributes}->{charset}->{value};                    !!!cp ('t106');
3729                  }                    $self->{change_encoding}
3730                  if ($token->{attributes}->{'http-equiv'}) {                        ->($self, $token->{attributes}->{charset}->{value},
3731                             $token);
3732                      
3733                      $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
3734                          ->set_user_data (manakai_has_reference =>
3735                                               $token->{attributes}->{charset}
3736                                                   ->{has_reference});
3737                    } elsif ($token->{attributes}->{content}) {
3738                    ## 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.
3739                    if ($token->{attributes}->{'http-equiv'}->{value}                    if ($token->{attributes}->{content}->{value}
3740                        =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=                        =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
3741                              [\x09-\x0D\x20]*=
3742                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
3743                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
3744                      $charset = defined $1 ? $1 : defined $2 ? $2 : $3;                      !!!cp ('t107');
3745                    } ## TODO: And if supported                      $self->{change_encoding}
3746                            ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,
3747                               $token);
3748                        $meta_el->[0]->get_attribute_node_ns (undef, 'content')
3749                            ->set_user_data (manakai_has_reference =>
3750                                                 $token->{attributes}->{content}
3751                                                       ->{has_reference});
3752                      } else {
3753                        !!!cp ('t108');
3754                      }
3755                    }
3756                  } else {
3757                    if ($token->{attributes}->{charset}) {
3758                      !!!cp ('t109');
3759                      $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
3760                          ->set_user_data (manakai_has_reference =>
3761                                               $token->{attributes}->{charset}
3762                                                   ->{has_reference});
3763                    }
3764                    if ($token->{attributes}->{content}) {
3765                      !!!cp ('t110');
3766                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')
3767                          ->set_user_data (manakai_has_reference =>
3768                                               $token->{attributes}->{content}
3769                                                   ->{has_reference});
3770                  }                  }
                 ## TODO: Change the encoding  
3771                }                }
3772    
3773                ## TODO: Extracting |charset| from |meta|.                pop @{$self->{open_elements}} # <head>
3774                pop @{$self->{open_elements}}                    if $self->{insertion_mode} == AFTER_HEAD_IM;
                   if $self->{insertion_mode} eq 'after head';  
3775                !!!next-token;                !!!next-token;
3776                redo B;                redo B;
3777              } elsif ($token->{tag_name} eq 'title' and              } elsif ($token->{tag_name} eq 'title') {
3778                       $self->{insertion_mode} eq 'in head') {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3779                ## NOTE: There is a "as if in head" code clone.                  !!!cp ('t111');
3780                if ($self->{insertion_mode} eq 'after head') {                  ## As if </noscript>
3781                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  pop @{$self->{open_elements}};
3782                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'in noscript:title', token => $token);
3783                  
3784                    $self->{insertion_mode} = IN_HEAD_IM;
3785                    ## Reprocess in the "in head" insertion mode...
3786                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
3787                    !!!cp ('t112');
3788                    !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
3789                    push @{$self->{open_elements}},
3790                        [$self->{head_element}, $el_category->{head}];
3791                  } else {
3792                    !!!cp ('t113');
3793                }                }
3794    
3795                  ## NOTE: There is a "as if in head" code clone.
3796                my $parent = defined $self->{head_element} ? $self->{head_element}                my $parent = defined $self->{head_element} ? $self->{head_element}
3797                    : $self->{open_elements}->[-1]->[0];                    : $self->{open_elements}->[-1]->[0];
3798                $parse_rcdata->(RCDATA_CONTENT_MODEL,                $parse_rcdata->(RCDATA_CONTENT_MODEL);
3799                                sub { $parent->append_child ($_[0]) });                pop @{$self->{open_elements}} # <head>
3800                pop @{$self->{open_elements}}                    if $self->{insertion_mode} == AFTER_HEAD_IM;
                   if $self->{insertion_mode} eq 'after head';  
3801                redo B;                redo B;
3802              } elsif ($token->{tag_name} eq 'style') {              } elsif ($token->{tag_name} eq 'style') {
3803                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
3804                ## insertion mode 'in head')                ## insertion mode IN_HEAD_IM)
3805                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3806                if ($self->{insertion_mode} eq 'after head') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3807                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t114');
3808                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
3809                }                  push @{$self->{open_elements}},
3810                $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);                      [$self->{head_element}, $el_category->{head}];
3811                pop @{$self->{open_elements}}                } else {
3812                    if $self->{insertion_mode} eq 'after head';                  !!!cp ('t115');
3813                  }
3814                  $parse_rcdata->(CDATA_CONTENT_MODEL);
3815                  pop @{$self->{open_elements}} # <head>
3816                      if $self->{insertion_mode} == AFTER_HEAD_IM;
3817                redo B;                redo B;
3818              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
3819                if ($self->{insertion_mode} eq 'in head') {                if ($self->{insertion_mode} == IN_HEAD_IM) {
3820                    !!!cp ('t116');
3821                  ## NOTE: and scripting is disalbed                  ## NOTE: and scripting is disalbed
3822                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
3823                  $self->{insertion_mode} = 'in head noscript';                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
3824                  !!!next-token;                  !!!next-token;
3825                  redo B;                  redo B;
3826                } elsif ($self->{insertion_mode} eq 'in head noscript') {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3827                  !!!parse-error (type => 'in noscript:noscript');                  !!!cp ('t117');
3828                    !!!parse-error (type => 'in noscript:noscript', token => $token);
3829                  ## Ignore the token                  ## Ignore the token
3830                  !!!next-token;                  !!!next-token;
3831                  redo B;                  redo B;
3832                } else {                } else {
3833                    !!!cp ('t118');
3834                  #                  #
3835                }                }
3836              } elsif ($token->{tag_name} eq 'head' and              } elsif ($token->{tag_name} eq 'script') {
3837                       $self->{insertion_mode} ne 'after head') {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3838                !!!parse-error (type => 'in head:head'); # or in head noscript                  !!!cp ('t119');
3839                ## Ignore the token                  ## As if </noscript>
3840                !!!next-token;                  pop @{$self->{open_elements}};
3841                redo B;                  !!!parse-error (type => 'in noscript:script', token => $token);
3842              } elsif ($self->{insertion_mode} ne 'in head noscript' and                
3843                       $token->{tag_name} eq 'script') {                  $self->{insertion_mode} = IN_HEAD_IM;
3844                if ($self->{insertion_mode} eq 'after head') {                  ## Reprocess in the "in head" insertion mode...
3845                  !!!parse-error (type => 'after head:'.$token->{tag_name});                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
3846                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!cp ('t120');
3847                    !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
3848                    push @{$self->{open_elements}},
3849                        [$self->{head_element}, $el_category->{head}];
3850                  } else {
3851                    !!!cp ('t121');
3852                }                }
3853    
3854                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3855                $script_start_tag->($insert_to_current);                $script_start_tag->();
3856                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
3857                    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;  
3858                redo B;                redo B;
3859              } elsif ($self->{insertion_mode} eq 'after head' and              } elsif ($token->{tag_name} eq 'body' or
3860                       $token->{tag_name} eq 'frameset') {                       $token->{tag_name} eq 'frameset') {
3861                !!!insert-element ('frameset', $token->{attributes});                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3862                $self->{insertion_mode} = 'in frameset';                  !!!cp ('t122');
3863                    ## As if </noscript>
3864                    pop @{$self->{open_elements}};
3865                    !!!parse-error (type => 'in noscript:'.$token->{tag_name}, token => $token);
3866                    
3867                    ## Reprocess in the "in head" insertion mode...
3868                    ## As if </head>
3869                    pop @{$self->{open_elements}};
3870                    
3871                    ## Reprocess in the "after head" insertion mode...
3872                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3873                    !!!cp ('t124');
3874                    pop @{$self->{open_elements}};
3875                    
3876                    ## Reprocess in the "after head" insertion mode...
3877                  } else {
3878                    !!!cp ('t125');
3879                  }
3880    
3881                  ## "after head" insertion mode
3882                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
3883                  if ($token->{tag_name} eq 'body') {
3884                    !!!cp ('t126');
3885                    $self->{insertion_mode} = IN_BODY_IM;
3886                  } elsif ($token->{tag_name} eq 'frameset') {
3887                    !!!cp ('t127');
3888                    $self->{insertion_mode} = IN_FRAMESET_IM;
3889                  } else {
3890                    die "$0: tag name: $self->{tag_name}";
3891                  }
3892                !!!next-token;                !!!next-token;
3893                redo B;                redo B;
3894              } else {              } else {
3895                  !!!cp ('t128');
3896                #                #
3897              }              }
3898            } elsif ($token->{type} eq 'end tag') {  
3899              if ($self->{insertion_mode} eq 'in head' and              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3900                  $token->{tag_name} eq 'head') {                !!!cp ('t129');
3901                  ## As if </noscript>
3902                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3903                $self->{insertion_mode} = 'after head';                !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);
3904                !!!next-token;                
3905                redo B;                ## Reprocess in the "in head" insertion mode...
3906              } elsif ($self->{insertion_mode} eq 'in head noscript' and                ## As if </head>
                 $token->{tag_name} eq 'noscript') {  
3907                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3908                $self->{insertion_mode} = 'in head';  
3909                !!!next-token;                ## Reprocess in the "after head" insertion mode...
3910                redo B;              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3911              } elsif ($self->{insertion_mode} eq 'in head' and                !!!cp ('t130');
3912                       {                ## As if </head>
3913                  pop @{$self->{open_elements}};
3914    
3915                  ## Reprocess in the "after head" insertion mode...
3916                } else {
3917                  !!!cp ('t131');
3918                }
3919    
3920                ## "after head" insertion mode
3921                ## As if <body>
3922                !!!insert-element ('body',, $token);
3923                $self->{insertion_mode} = IN_BODY_IM;
3924                ## reprocess
3925                redo B;
3926              } elsif ($token->{type} == END_TAG_TOKEN) {
3927                if ($token->{tag_name} eq 'head') {
3928                  if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3929                    !!!cp ('t132');
3930                    ## As if <head>
3931                    !!!create-element ($self->{head_element}, 'head',, $token);
3932                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3933                    push @{$self->{open_elements}},
3934                        [$self->{head_element}, $el_category->{head}];
3935    
3936                    ## Reprocess in the "in head" insertion mode...
3937                    pop @{$self->{open_elements}};
3938                    $self->{insertion_mode} = AFTER_HEAD_IM;
3939                    !!!next-token;
3940                    redo B;
3941                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3942                    !!!cp ('t133');
3943                    ## As if </noscript>
3944                    pop @{$self->{open_elements}};
3945                    !!!parse-error (type => 'in noscript:/head', token => $token);
3946                    
3947                    ## Reprocess in the "in head" insertion mode...
3948                    pop @{$self->{open_elements}};
3949                    $self->{insertion_mode} = AFTER_HEAD_IM;
3950                    !!!next-token;
3951                    redo B;
3952                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3953                    !!!cp ('t134');
3954                    pop @{$self->{open_elements}};
3955                    $self->{insertion_mode} = AFTER_HEAD_IM;
3956                    !!!next-token;
3957                    redo B;
3958                  } else {
3959                    !!!cp ('t135');
3960                    #
3961                  }
3962                } elsif ($token->{tag_name} eq 'noscript') {
3963                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3964                    !!!cp ('t136');
3965                    pop @{$self->{open_elements}};
3966                    $self->{insertion_mode} = IN_HEAD_IM;
3967                    !!!next-token;
3968                    redo B;
3969                  } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3970                    !!!cp ('t137');
3971                    !!!parse-error (type => 'unmatched end tag:noscript', token => $token);
3972                    ## Ignore the token ## ISSUE: An issue in the spec.
3973                    !!!next-token;
3974                    redo B;
3975                  } else {
3976                    !!!cp ('t138');
3977                    #
3978                  }
3979                } elsif ({
3980                        body => 1, html => 1,                        body => 1, html => 1,
                       p => 1, br => 1,  
3981                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3982                  if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3983                    !!!cp ('t139');
3984                    ## As if <head>
3985                    !!!create-element ($self->{head_element}, 'head',, $token);
3986                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3987                    push @{$self->{open_elements}},
3988                        [$self->{head_element}, $el_category->{head}];
3989    
3990                    $self->{insertion_mode} = IN_HEAD_IM;
3991                    ## Reprocess in the "in head" insertion mode...
3992                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3993                    !!!cp ('t140');
3994                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
3995                    ## Ignore the token
3996                    !!!next-token;
3997                    redo B;
3998                  } else {
3999                    !!!cp ('t141');
4000                  }
4001                  
4002                #                #
4003              } elsif ($self->{insertion_mode} eq 'in head noscript' and              } elsif ({
                      {  
4004                        p => 1, br => 1,                        p => 1, br => 1,
4005                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4006                  if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4007                    !!!cp ('t142');
4008                    ## As if <head>
4009                    !!!create-element ($self->{head_element}, 'head',, $token);
4010                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4011                    push @{$self->{open_elements}},
4012                        [$self->{head_element}, $el_category->{head}];
4013    
4014                    $self->{insertion_mode} = IN_HEAD_IM;
4015                    ## Reprocess in the "in head" insertion mode...
4016                  } else {
4017                    !!!cp ('t143');
4018                  }
4019    
4020                #                #
4021              } elsif ($self->{insertion_mode} ne 'after head') {              } else {
4022                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4023                ## Ignore the token                  !!!cp ('t144');
4024                    #
4025                  } else {
4026                    !!!cp ('t145');
4027                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4028                    ## Ignore the token
4029                    !!!next-token;
4030                    redo B;
4031                  }
4032                }
4033    
4034                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4035                  !!!cp ('t146');
4036                  ## As if </noscript>
4037                  pop @{$self->{open_elements}};
4038                  !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);
4039                  
4040                  ## Reprocess in the "in head" insertion mode...
4041                  ## As if </head>
4042                  pop @{$self->{open_elements}};
4043    
4044                  ## Reprocess in the "after head" insertion mode...
4045                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4046                  !!!cp ('t147');
4047                  ## As if </head>
4048                  pop @{$self->{open_elements}};
4049    
4050                  ## Reprocess in the "after head" insertion mode...
4051                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4052    ## ISSUE: This case cannot be reached?
4053                  !!!cp ('t148');
4054                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4055                  ## Ignore the token ## ISSUE: An issue in the spec.
4056                !!!next-token;                !!!next-token;
4057                redo B;                redo B;
4058              } else {              } else {
4059                #                !!!cp ('t149');
4060              }              }
           } else {  
             #  
           }  
4061    
4062            ## As if </head> or </noscript> or <body>              ## "after head" insertion mode
4063            if ($self->{insertion_mode} eq 'in head') {              ## As if <body>
4064              pop @{$self->{open_elements}};              !!!insert-element ('body',, $token);
4065              $self->{insertion_mode} = 'after head';              $self->{insertion_mode} = IN_BODY_IM;
4066            } elsif ($self->{insertion_mode} eq 'in head noscript') {              ## reprocess
4067              pop @{$self->{open_elements}};              redo B;
4068              !!!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) {
4069              $self->{insertion_mode} = 'in head';          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4070            } else { # 'after head'            !!!cp ('t149.1');
4071              !!!insert-element ('body');  
4072              $self->{insertion_mode} = 'in body';            ## NOTE: As if <head>
4073            }            !!!create-element ($self->{head_element}, 'head',, $token);
4074            ## reprocess            $self->{open_elements}->[-1]->[0]->append_child
4075            redo B;                ($self->{head_element});
4076              #push @{$self->{open_elements}},
4077              #    [$self->{head_element}, $el_category->{head}];
4078              #$self->{insertion_mode} = IN_HEAD_IM;
4079              ## NOTE: Reprocess.
4080    
4081              ## NOTE: As if </head>
4082              #pop @{$self->{open_elements}};
4083              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4084              ## NOTE: Reprocess.
4085              
4086              #
4087            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4088              !!!cp ('t149.2');
4089    
4090              ## NOTE: As if </head>
4091              pop @{$self->{open_elements}};
4092              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4093              ## NOTE: Reprocess.
4094    
4095              #
4096            } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4097              !!!cp ('t149.3');
4098    
4099              !!!parse-error (type => 'in noscript:#eof', token => $token);
4100    
4101              ## As if </noscript>
4102              pop @{$self->{open_elements}};
4103              #$self->{insertion_mode} = IN_HEAD_IM;
4104              ## NOTE: Reprocess.
4105    
4106              ## NOTE: As if </head>
4107              pop @{$self->{open_elements}};
4108              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4109              ## NOTE: Reprocess.
4110    
4111              #
4112            } else {
4113              !!!cp ('t149.4');
4114              #
4115            }
4116    
4117            ## NOTE: As if <body>
4118            !!!insert-element ('body',, $token);
4119            $self->{insertion_mode} = IN_BODY_IM;
4120            ## NOTE: Reprocess.
4121            redo B;
4122          } else {
4123            die "$0: $token->{type}: Unknown token type";
4124          }
4125    
4126            ## ISSUE: An issue in the spec.            ## ISSUE: An issue in the spec.
4127          } elsif ($self->{insertion_mode} eq 'in body' or      } elsif ($self->{insertion_mode} & BODY_IMS) {
4128                   $self->{insertion_mode} eq 'in cell' or            if ($token->{type} == CHARACTER_TOKEN) {
4129                   $self->{insertion_mode} eq 'in caption') {              !!!cp ('t150');
           if ($token->{type} eq 'character') {  
4130              ## NOTE: There is a code clone of "character in body".              ## NOTE: There is a code clone of "character in body".
4131              $reconstruct_active_formatting_elements->($insert_to_current);              $reconstruct_active_formatting_elements->($insert_to_current);
4132                            
# Line 3658  sub _tree_construction_main ($) { Line 4134  sub _tree_construction_main ($) {
4134    
4135              !!!next-token;              !!!next-token;
4136              redo B;              redo B;
4137            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} == START_TAG_TOKEN) {
4138              if ({              if ({
4139                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
4140                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,
4141                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
4142                if ($self->{insertion_mode} eq 'in cell') {                if ($self->{insertion_mode} == IN_CELL_IM) {
4143                  ## have an element in table scope                  ## have an element in table scope
4144                  my $tn;                  for (reverse 0..$#{$self->{open_elements}}) {
                 INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
4145                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4146                    if ($node->[1] eq 'td' or $node->[1] eq 'th') {                    if ($node->[1] & TABLE_CELL_EL) {
4147                      $tn = $node->[1];                      !!!cp ('t151');
4148                      last INSCOPE;  
4149                    } elsif ({                      ## Close the cell
4150                              table => 1, html => 1,                      !!!back-token; # <?>
4151                             }->{$node->[1]}) {                      $token = {type => END_TAG_TOKEN,
4152                      last INSCOPE;                                tag_name => $node->[0]->manakai_local_name,
4153                    }                                line => $token->{line},
4154                  } # INSCOPE                                column => $token->{column}};
                   unless (defined $tn) {  
                     !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                     ## Ignore the token  
                     !!!next-token;  
4155                      redo B;                      redo B;
4156                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4157                        !!!cp ('t152');
4158                        ## ISSUE: This case can never be reached, maybe.
4159                        last;
4160                    }                    }
4161                                    }
4162                  ## Close the cell  
4163                  !!!back-token; # <?>                  !!!cp ('t153');
4164                  $token = {type => 'end tag', tag_name => $tn};                  !!!parse-error (type => 'start tag not allowed',
4165                        value => $token->{tag_name}, token => $token);
4166                    ## Ignore the token
4167                    !!!next-token;
4168                  redo B;                  redo B;
4169                } elsif ($self->{insertion_mode} eq 'in caption') {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4170                  !!!parse-error (type => 'not closed:caption');                  !!!parse-error (type => 'not closed:caption', token => $token);
4171                                    
4172                  ## As if </caption>                  ## NOTE: As if </caption>.
4173                  ## have a table element in table scope                  ## have a table element in table scope
4174                  my $i;                  my $i;
4175                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
4176                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
4177                    if ($node->[1] eq 'caption') {                      my $node = $self->{open_elements}->[$_];
4178                      $i = $_;                      if ($node->[1] & CAPTION_EL) {
4179                      last INSCOPE;                        !!!cp ('t155');
4180                    } elsif ({                        $i = $_;
4181                              table => 1, html => 1,                        last INSCOPE;
4182                             }->{$node->[1]}) {                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4183                      last INSCOPE;                        !!!cp ('t156');
4184                          last;
4185                        }
4186                    }                    }
4187    
4188                      !!!cp ('t157');
4189                      !!!parse-error (type => 'start tag not allowed',
4190                                      value => $token->{tag_name}, token => $token);
4191                      ## Ignore the token
4192                      !!!next-token;
4193                      redo B;
4194                  } # INSCOPE                  } # INSCOPE
                   unless (defined $i) {  
                     !!!parse-error (type => 'unmatched end tag:caption');  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
                   }  
4195                                    
4196                  ## generate implied end tags                  ## generate implied end tags
4197                  if ({                  while ($self->{open_elements}->[-1]->[1]
4198                       dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
4199                       td => 1, th => 1, tr => 1,                    !!!cp ('t158');
4200                       tbody => 1, tfoot=> 1, thead => 1,                    pop @{$self->{open_elements}};
                     }->{$self->{open_elements}->[-1]->[1]}) {  
                   !!!back-token; # <?>  
                   $token = {type => 'end tag', tag_name => 'caption'};  
                   !!!back-token;  
                   $token = {type => 'end tag',  
                             tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                   redo B;  
4201                  }                  }
4202    
4203                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4204                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t159');
4205                      !!!parse-error (type => 'not closed',
4206                                      value => $self->{open_elements}->[-1]->[0]
4207                                          ->manakai_local_name,
4208                                      token => $token);
4209                    } else {
4210                      !!!cp ('t160');
4211                  }                  }
4212                                    
4213                  splice @{$self->{open_elements}}, $i;                  splice @{$self->{open_elements}}, $i;
4214                                    
4215                  $clear_up_to_marker->();                  $clear_up_to_marker->();
4216                                    
4217                  $self->{insertion_mode} = 'in table';                  $self->{insertion_mode} = IN_TABLE_IM;
4218                                    
4219                  ## reprocess                  ## reprocess
4220                  redo B;                  redo B;
4221                } else {                } else {
4222                    !!!cp ('t161');
4223                  #                  #
4224                }                }
4225              } else {              } else {
4226                  !!!cp ('t162');
4227                #                #
4228              }              }
4229            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
4230              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {
4231                if ($self->{insertion_mode} eq 'in cell') {                if ($self->{insertion_mode} == IN_CELL_IM) {
4232                  ## have an element in table scope                  ## have an element in table scope
4233                  my $i;                  my $i;
4234                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4235                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4236                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
4237                        !!!cp ('t163');
4238                      $i = $_;                      $i = $_;
4239                      last INSCOPE;                      last INSCOPE;
4240                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
4241                              table => 1, html => 1,                      !!!cp ('t164');
                            }->{$node->[1]}) {  
4242                      last INSCOPE;                      last INSCOPE;
4243                    }                    }
4244                  } # INSCOPE                  } # INSCOPE
4245                    unless (defined $i) {                    unless (defined $i) {
4246                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!cp ('t165');
4247                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4248                      ## Ignore the token                      ## Ignore the token
4249                      !!!next-token;                      !!!next-token;
4250                      redo B;                      redo B;
4251                    }                    }
4252                                    
4253                  ## generate implied end tags                  ## generate implied end tags
4254                  if ({                  while ($self->{open_elements}->[-1]->[1]
4255                       dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
4256                       td => ($token->{tag_name} eq 'th'),                    !!!cp ('t166');
4257                       th => ($token->{tag_name} eq 'td'),                    pop @{$self->{open_elements}};
                      tr => 1,  
                      tbody => 1, tfoot=> 1, thead => 1,  
                     }->{$self->{open_elements}->[-1]->[1]}) {  
                   !!!back-token;  
                   $token = {type => 'end tag',  
                             tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                   redo B;  
4258                  }                  }
4259                    
4260                  if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {                  if ($self->{open_elements}->[-1]->[0]->manakai_local_name
4261                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                          ne $token->{tag_name}) {
4262                      !!!cp ('t167');
4263                      !!!parse-error (type => 'not closed',
4264                                      value => $self->{open_elements}->[-1]->[0]
4265                                          ->manakai_local_name,
4266                                      token => $token);
4267                    } else {
4268                      !!!cp ('t168');
4269                  }                  }
4270                                    
4271                  splice @{$self->{open_elements}}, $i;                  splice @{$self->{open_elements}}, $i;
4272                                    
4273                  $clear_up_to_marker->();                  $clear_up_to_marker->();
4274                                    
4275                  $self->{insertion_mode} = 'in row';                  $self->{insertion_mode} = IN_ROW_IM;
4276                                    
4277                  !!!next-token;                  !!!next-token;
4278                  redo B;                  redo B;
4279                } elsif ($self->{insertion_mode} eq 'in caption') {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4280                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t169');
4281                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4282                  ## Ignore the token                  ## Ignore the token
4283                  !!!next-token;                  !!!next-token;
4284                  redo B;                  redo B;
4285                } else {                } else {
4286                    !!!cp ('t170');
4287                  #                  #
4288                }                }
4289              } elsif ($token->{tag_name} eq 'caption') {              } elsif ($token->{tag_name} eq 'caption') {
4290                if ($self->{insertion_mode} eq 'in caption') {                if ($self->{insertion_mode} == IN_CAPTION_IM) {
4291                  ## have a table element in table scope                  ## have a table element in table scope
4292                  my $i;                  my $i;
4293                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
4294                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
4295                    if ($node->[1] eq $token->{tag_name}) {                      my $node = $self->{open_elements}->[$_];
4296                      $i = $_;                      if ($node->[1] & CAPTION_EL) {
4297                      last INSCOPE;                        !!!cp ('t171');
4298                    } elsif ({                        $i = $_;
4299                              table => 1, html => 1,                        last INSCOPE;
4300                             }->{$node->[1]}) {                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4301                      last INSCOPE;                        !!!cp ('t172');
4302                          last;
4303                        }
4304                    }                    }
4305    
4306                      !!!cp ('t173');
4307                      !!!parse-error (type => 'unmatched end tag',
4308                                      value => $token->{tag_name}, token => $token);
4309                      ## Ignore the token
4310                      !!!next-token;
4311                      redo B;
4312                  } # INSCOPE                  } # INSCOPE
                   unless (defined $i) {  
                     !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
                   }  
4313                                    
4314                  ## generate implied end tags                  ## generate implied end tags
4315                  if ({                  while ($self->{open_elements}->[-1]->[1]
4316                       dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
4317                       td => 1, th => 1, tr => 1,                    !!!cp ('t174');
4318                       tbody => 1, tfoot=> 1, thead => 1,                    pop @{$self->{open_elements}};
                     }->{$self->{open_elements}->[-1]->[1]}) {  
                   !!!back-token;  
                   $token = {type => 'end tag',  
                             tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                   redo B;  
4319                  }                  }
4320                                    
4321                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4322                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t175');
4323                      !!!parse-error (type => 'not closed',
4324                                      value => $self->{open_elements}->[-1]->[0]
4325                                          ->manakai_local_name,
4326                                      token => $token);
4327                    } else {
4328                      !!!cp ('t176');
4329                  }                  }
4330                                    
4331                  splice @{$self->{open_elements}}, $i;                  splice @{$self->{open_elements}}, $i;
4332                                    
4333                  $clear_up_to_marker->();                  $clear_up_to_marker->();
4334                                    
4335                  $self->{insertion_mode} = 'in table';                  $self->{insertion_mode} = IN_TABLE_IM;
4336                                    
4337                  !!!next-token;                  !!!next-token;
4338                  redo B;                  redo B;
4339                } elsif ($self->{insertion_mode} eq 'in cell') {                } elsif ($self->{insertion_mode} == IN_CELL_IM) {
4340                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t177');
4341                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4342                  ## Ignore the token                  ## Ignore the token
4343                  !!!next-token;                  !!!next-token;
4344                  redo B;                  redo B;
4345                } else {                } else {
4346                    !!!cp ('t178');
4347                  #                  #
4348                }                }
4349              } elsif ({              } elsif ({
4350                        table => 1, tbody => 1, tfoot => 1,                        table => 1, tbody => 1, tfoot => 1,
4351                        thead => 1, tr => 1,                        thead => 1, tr => 1,
4352                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
4353                       $self->{insertion_mode} eq 'in cell') {                       $self->{insertion_mode} == IN_CELL_IM) {
4354                ## have an element in table scope                ## have an element in table scope
4355                my $i;                my $i;
4356                my $tn;                my $tn;
4357                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: {
4358                  my $node = $self->{open_elements}->[$_];                  for (reverse 0..$#{$self->{open_elements}}) {
4359                  if ($node->[1] eq $token->{tag_name}) {                    my $node = $self->{open_elements}->[$_];
4360                    $i = $_;                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
4361                    last INSCOPE;                      !!!cp ('t179');
4362                  } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {                      $i = $_;
4363                    $tn = $node->[1];  
4364                    ## NOTE: There is exactly one |td| or |th| element                      ## Close the cell
4365                    ## in scope in the stack of open elements by definition.                      !!!back-token; # </?>
4366                  } elsif ({                      $token = {type => END_TAG_TOKEN, tag_name => $tn,
4367                            table => 1, html => 1,                                line => $token->{line},
4368                           }->{$node->[1]}) {                                column => $token->{column}};
4369                    last INSCOPE;                      redo B;
4370                      } elsif ($node->[1] & TABLE_CELL_EL) {
4371                        !!!cp ('t180');
4372                        $tn = $node->[0]->manakai_local_name;
4373                        ## NOTE: There is exactly one |td| or |th| element
4374                        ## in scope in the stack of open elements by definition.
4375                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4376                        ## ISSUE: Can this be reached?
4377                        !!!cp ('t181');
4378                        last;
4379                      }
4380                  }                  }
4381                } # INSCOPE  
4382                unless (defined $i) {                  !!!cp ('t182');
4383                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
4384                        value => $token->{tag_name}, token => $token);
4385                  ## Ignore the token                  ## Ignore the token
4386                  !!!next-token;                  !!!next-token;
4387                  redo B;                  redo B;
4388                }                } # INSCOPE
   
               ## Close the cell  
               !!!back-token; # </?>  
               $token = {type => 'end tag', tag_name => $tn};  
               redo B;  
4389              } elsif ($token->{tag_name} eq 'table' and              } elsif ($token->{tag_name} eq 'table' and
4390                       $self->{insertion_mode} eq 'in caption') {                       $self->{insertion_mode} == IN_CAPTION_IM) {
4391                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed:caption', token => $token);
4392    
4393                ## As if </caption>                ## As if </caption>
4394                ## have a table element in table scope                ## have a table element in table scope
4395                my $i;                my $i;
4396                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4397                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4398                  if ($node->[1] eq 'caption') {                  if ($node->[1] & CAPTION_EL) {
4399                      !!!cp ('t184');
4400                    $i = $_;                    $i = $_;
4401                    last INSCOPE;                    last INSCOPE;
4402                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
4403                            table => 1, html => 1,                    !!!cp ('t185');
                          }->{$node->[1]}) {  
4404                    last INSCOPE;                    last INSCOPE;
4405                  }                  }
4406                } # INSCOPE                } # INSCOPE
4407                unless (defined $i) {                unless (defined $i) {
4408                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!cp ('t186');
4409                    !!!parse-error (type => 'unmatched end tag:caption', token => $token);
4410                  ## Ignore the token                  ## Ignore the token
4411                  !!!next-token;                  !!!next-token;
4412                  redo B;                  redo B;
4413                }                }
4414                                
4415                ## generate implied end tags                ## generate implied end tags
4416                if ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
4417                     dd => 1, dt => 1, li => 1, p => 1,                  !!!cp ('t187');
4418                     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 => 'caption'};  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
4419                }                }
4420    
4421                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4422                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t188');
4423                    !!!parse-error (type => 'not closed',
4424                                    value => $self->{open_elements}->[-1]->[0]
4425                                        ->manakai_local_name,
4426                                    token => $token);
4427                  } else {
4428                    !!!cp ('t189');
4429                }                }
4430    
4431                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
4432    
4433                $clear_up_to_marker->();                $clear_up_to_marker->();
4434    
4435                $self->{insertion_mode} = 'in table';                $self->{insertion_mode} = IN_TABLE_IM;
4436    
4437                ## reprocess                ## reprocess
4438                redo B;                redo B;
4439              } elsif ({              } elsif ({
4440                        body => 1, col => 1, colgroup => 1, html => 1,                        body => 1, col => 1, colgroup => 1, html => 1,
4441                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4442                if ($self->{insertion_mode} eq 'in cell' or                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
4443                    $self->{insertion_mode} eq 'in caption') {                  !!!cp ('t190');
4444                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4445                  ## Ignore the token                  ## Ignore the token
4446                  !!!next-token;                  !!!next-token;
4447                  redo B;                  redo B;
4448                } else {                } else {
4449                    !!!cp ('t191');
4450                  #                  #
4451                }                }
4452              } elsif ({              } elsif ({
4453                        tbody => 1, tfoot => 1,                        tbody => 1, tfoot => 1,
4454                        thead => 1, tr => 1,                        thead => 1, tr => 1,
4455                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
4456                       $self->{insertion_mode} eq 'in caption') {                       $self->{insertion_mode} == IN_CAPTION_IM) {
4457                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!cp ('t192');
4458                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4459                ## Ignore the token                ## Ignore the token
4460                !!!next-token;                !!!next-token;
4461                redo B;                redo B;
4462              } else {              } else {
4463                  !!!cp ('t193');
4464                #                #
4465              }              }
4466            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4467              #          for my $entry (@{$self->{open_elements}}) {
4468              unless ($entry->[1] & ALL_END_TAG_OPTIONAL_EL) {
4469                !!!cp ('t75');
4470                !!!parse-error (type => 'in body:#eof', token => $token);
4471                last;
4472            }            }
4473                      }
4474            $in_body->($insert_to_current);  
4475            redo B;          ## Stop parsing.
4476          } elsif ($self->{insertion_mode} eq 'in table') {          last B;
4477            if ($token->{type} eq 'character') {        } else {
4478              ## NOTE: There are "character in table" code clones.          die "$0: $token->{type}: Unknown token type";
4479              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {        }
4480                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
4481          $insert = $insert_to_current;
4482          #
4483        } elsif ($self->{insertion_mode} & TABLE_IMS) {
4484          if ($token->{type} == CHARACTER_TOKEN) {
4485            if (not $open_tables->[-1]->[1] and # tainted
4486                $token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4487              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4488                                
4489                unless (length $token->{data}) {            unless (length $token->{data}) {
4490                  !!!next-token;              !!!cp ('t194');
4491                  redo B;              !!!next-token;
4492                }              redo B;
4493              }            } else {
4494                !!!cp ('t195');
4495              }
4496            }
4497    
4498              !!!parse-error (type => 'in table:#character');              !!!parse-error (type => 'in table:#character', token => $token);
4499    
4500              ## As if in body, but insert into foster parent element              ## As if in body, but insert into foster parent element
4501              ## ISSUE: Spec says that "whenever a node would be inserted              ## ISSUE: Spec says that "whenever a node would be inserted
# Line 3990  sub _tree_construction_main ($) { Line 4503  sub _tree_construction_main ($) {
4503              ## result in a new Text node.              ## result in a new Text node.
4504              $reconstruct_active_formatting_elements->($insert_to_foster);              $reconstruct_active_formatting_elements->($insert_to_foster);
4505                            
4506              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]}) {  
4507                # MUST                # MUST
4508                my $foster_parent_element;                my $foster_parent_element;
4509                my $next_sibling;                my $next_sibling;
4510                my $prev_sibling;                my $prev_sibling;
4511                OE: for (reverse 0..$#{$self->{open_elements}}) {                OE: for (reverse 0..$#{$self->{open_elements}}) {
4512                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
4513                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
4514                    if (defined $parent and $parent->node_type == 1) {                    if (defined $parent and $parent->node_type == 1) {
4515                        !!!cp ('t196');
4516                      $foster_parent_element = $parent;                      $foster_parent_element = $parent;
4517                      $next_sibling = $self->{open_elements}->[$_]->[0];                      $next_sibling = $self->{open_elements}->[$_]->[0];
4518                      $prev_sibling = $next_sibling->previous_sibling;                      $prev_sibling = $next_sibling->previous_sibling;
4519                    } else {                    } else {
4520                        !!!cp ('t197');
4521                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];
4522                      $prev_sibling = $foster_parent_element->last_child;                      $prev_sibling = $foster_parent_element->last_child;
4523                    }                    }
# Line 4017  sub _tree_construction_main ($) { Line 4529  sub _tree_construction_main ($) {
4529                  unless defined $foster_parent_element;                  unless defined $foster_parent_element;
4530                if (defined $prev_sibling and                if (defined $prev_sibling and
4531                    $prev_sibling->node_type == 3) {                    $prev_sibling->node_type == 3) {
4532                    !!!cp ('t198');
4533                  $prev_sibling->manakai_append_text ($token->{data});                  $prev_sibling->manakai_append_text ($token->{data});
4534                } else {                } else {
4535                    !!!cp ('t199');
4536                  $foster_parent_element->insert_before                  $foster_parent_element->insert_before
4537                    ($self->{document}->create_text_node ($token->{data}),                    ($self->{document}->create_text_node ($token->{data}),
4538                     $next_sibling);                     $next_sibling);
4539                }                }
4540              } else {            $open_tables->[-1]->[1] = 1; # tainted
4541                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          } else {
4542              }            !!!cp ('t200');
4543              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4544            }
4545                            
4546              !!!next-token;          !!!next-token;
4547              redo B;          redo B;
4548            } elsif ($token->{type} eq 'start tag') {        } elsif ($token->{type} == START_TAG_TOKEN) {
4549              if ({              if ({
4550                   caption => 1,                   tr => ($self->{insertion_mode} != IN_ROW_IM),
4551                   colgroup => 1,                   th => 1, td => 1,
                  tbody => 1, tfoot => 1, thead => 1,  
4552                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
4553                ## Clear back to table context                if ($self->{insertion_mode} == IN_TABLE_IM) {
4554                while ($self->{open_elements}->[-1]->[1] ne 'table' and                  ## Clear back to table context
4555                       $self->{open_elements}->[-1]->[1] ne 'html') {                  while (not ($self->{open_elements}->[-1]->[1]
4556                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                                  & TABLE_SCOPING_EL)) {
4557                  pop @{$self->{open_elements}};                    !!!cp ('t201');
4558                }                    pop @{$self->{open_elements}};
   
               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;  
             } elsif ({  
                       col => 1,  
                       td => 1, th => 1, tr => 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}};  
               }  
   
               !!!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  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'table') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
4559                  }                  }
4560                } # INSCOPE                  
4561                unless (defined $i) {                  !!!insert-element ('tbody',, $token);
4562                  !!!parse-error (type => 'unmatched end tag:table');                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
4563                  ## Ignore tokens </table><table>                  ## reprocess in the "in table body" insertion mode...
                 !!!next-token;  
                 redo B;  
               }  
                 
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                    tbody => 1, tfoot=> 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token; # <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]);  
4564                }                }
4565    
4566                splice @{$self->{open_elements}}, $i;                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
4567                    unless ($token->{tag_name} eq 'tr') {
4568                $self->_reset_insertion_mode;                    !!!cp ('t202');
4569                      !!!parse-error (type => 'missing start tag:tr', token => $token);
               ## reprocess  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'table') {  
               ## 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 $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
4570                  }                  }
4571                } # INSCOPE                  
4572                unless (defined $i) {                  ## Clear back to table body context
4573                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  while (not ($self->{open_elements}->[-1]->[1]
4574                  ## Ignore the token                                  & TABLE_ROWS_SCOPING_EL)) {
4575                  !!!next-token;                    !!!cp ('t203');
4576                  redo B;                    ## ISSUE: Can this case be reached?
4577                }                    pop @{$self->{open_elements}};
4578                                  }
4579                ## generate implied end tags                  
4580                if ({                  $self->{insertion_mode} = IN_ROW_IM;
4581                     dd => 1, dt => 1, li => 1, p => 1,                  if ($token->{tag_name} eq 'tr') {
4582                     td => 1, th => 1, tr => 1,                    !!!cp ('t204');
4583                     tbody => 1, tfoot=> 1, thead => 1,                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4584                    }->{$self->{open_elements}->[-1]->[1]}) {                    !!!next-token;
4585                  !!!back-token;                    redo B;
4586                  $token = {type => 'end tag',                  } else {
4587                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                    !!!cp ('t205');
4588                  redo B;                    !!!insert-element ('tr',, $token);
4589                      ## reprocess in the "in row" insertion mode
4590                    }
4591                  } else {
4592                    !!!cp ('t206');
4593                }                }
4594    
4595                if ($self->{open_elements}->[-1]->[1] ne 'table') {                ## Clear back to table row context
4596                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                while (not ($self->{open_elements}->[-1]->[1]
4597                                  & TABLE_ROW_SCOPING_EL)) {
4598                    !!!cp ('t207');
4599                    pop @{$self->{open_elements}};
4600                }                }
4601                  
4602                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4603                  $self->{insertion_mode} = IN_CELL_IM;
4604    
4605                splice @{$self->{open_elements}}, $i;                push @$active_formatting_elements, ['#marker', ''];
4606                  
               $self->_reset_insertion_mode;  
   
4607                !!!next-token;                !!!next-token;
4608                redo B;                redo B;
4609              } elsif ({              } elsif ({
4610                        body => 1, caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
4611                        html => 1, tbody => 1, td => 1, tfoot => 1, th => 1,                        tbody => 1, tfoot => 1, thead => 1,
4612                        thead => 1, tr => 1,                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM
4613                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4614                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                if ($self->{insertion_mode} == IN_ROW_IM) {
4615                ## Ignore the token                  ## As if </tr>
4616                !!!next-token;                  ## have an element in table scope
4617                redo B;                  my $i;
4618              } else {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4619                #                    my $node = $self->{open_elements}->[$_];
4620              }                    if ($node->[1] & TABLE_ROW_EL) {
4621            } else {                      !!!cp ('t208');
4622              #                      $i = $_;
4623            }                      last INSCOPE;
4624                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4625            !!!parse-error (type => 'in table:'.$token->{tag_name});                      !!!cp ('t209');
4626            $in_body->($insert_to_foster);                      last INSCOPE;
4627            redo B;                    }
4628          } elsif ($self->{insertion_mode} eq 'in column group') {                  } # INSCOPE
4629            if ($token->{type} eq 'character') {                  unless (defined $i) {
4630              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {                   !!!cp ('t210');
4631                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  ## TODO: This type is wrong.
4632                unless (length $token->{data}) {                   !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name}, token => $token);
4633                  !!!next-token;                    ## Ignore the token
4634                  redo B;                    !!!next-token;
4635                }                    redo B;
4636              }                  }
4637                                
4638              #                  ## Clear back to table row context
4639            } elsif ($token->{type} eq 'start tag') {                  while (not ($self->{open_elements}->[-1]->[1]
4640              if ($token->{tag_name} eq 'col') {                                  & TABLE_ROW_SCOPING_EL)) {
4641                !!!insert-element ($token->{tag_name}, $token->{attributes});                    !!!cp ('t211');
4642                pop @{$self->{open_elements}};                    ## ISSUE: Can this case be reached?
4643                !!!next-token;                    pop @{$self->{open_elements}};
4644                redo B;                  }
4645              } else {                  
4646                #                  pop @{$self->{open_elements}}; # tr
4647              }                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
4648            } elsif ($token->{type} eq 'end tag') {                  if ($token->{tag_name} eq 'tr') {
4649              if ($token->{tag_name} eq 'colgroup') {                    !!!cp ('t212');
4650                if ($self->{open_elements}->[-1]->[1] eq 'html') {                    ## reprocess
4651                  !!!parse-error (type => 'unmatched end tag:colgroup');                    redo B;
4652                  ## Ignore the token                  } else {
4653                  !!!next-token;                    !!!cp ('t213');
4654                  redo B;                    ## reprocess in the "in table body" insertion mode...
4655                } else {                  }
                 pop @{$self->{open_elements}}; # colgroup  
                 $self->{insertion_mode} = 'in table';  
                 !!!next-token;  
                 redo B;              
               }  
             } elsif ($token->{tag_name} eq 'col') {  
               !!!parse-error (type => 'unmatched end tag:col');  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
   
           ## As if </colgroup>  
           if ($self->{open_elements}->[-1]->[1] eq 'html') {  
             !!!parse-error (type => 'unmatched end tag:colgroup');  
             ## Ignore the token  
             !!!next-token;  
             redo B;  
           } else {  
             pop @{$self->{open_elements}}; # colgroup  
             $self->{insertion_mode} = 'in table';  
             ## reprocess  
             redo B;  
           }  
         } 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;  
4656                }                }
             }  
   
             !!!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);  
4657    
4658              if ({                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
4659                   table => 1, tbody => 1, tfoot => 1,                  ## have an element in table scope
4660                   thead => 1, tr => 1,                  my $i;
4661                  }->{$self->{open_elements}->[-1]->[1]}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4662                # MUST                    my $node = $self->{open_elements}->[$_];
4663                my $foster_parent_element;                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
4664                my $next_sibling;                      !!!cp ('t214');
4665                my $prev_sibling;                      $i = $_;
4666                OE: for (reverse 0..$#{$self->{open_elements}}) {                      last INSCOPE;
4667                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
4668                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                      !!!cp ('t215');
4669                    if (defined $parent and $parent->node_type == 1) {                      last INSCOPE;
                     $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;  
4670                    }                    }
4671                    last OE;                  } # INSCOPE
4672                    unless (defined $i) {
4673                      !!!cp ('t216');
4674    ## TODO: This erorr type ios wrong.
4675                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4676                      ## Ignore the token
4677                      !!!next-token;
4678                      redo B;
4679                  }                  }
               } # 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 ({  
                  tr => 1,  
                  th => 1, td => 1,  
                 }->{$token->{tag_name}}) {  
               unless ($token->{tag_name} eq 'tr') {  
                 !!!parse-error (type => 'missing start tag:tr');  
               }  
4680    
4681                ## Clear back to table body context                  ## Clear back to table body context
4682                while (not {                  while (not ($self->{open_elements}->[-1]->[1]
4683                  tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
4684                }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t217');
4685                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    ## ISSUE: Can this state be reached?
4686                      pop @{$self->{open_elements}};
4687                    }
4688                    
4689                    ## As if <{current node}>
4690                    ## have an element in table scope
4691                    ## true by definition
4692                    
4693                    ## Clear back to table body context
4694                    ## nop by definition
4695                    
4696                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4697                }                  $self->{insertion_mode} = IN_TABLE_IM;
4698                                  ## reprocess in "in table" insertion mode...
               $self->{insertion_mode} = 'in row';  
               if ($token->{tag_name} eq 'tr') {  
                 !!!insert-element ($token->{tag_name}, $token->{attributes});  
                 !!!next-token;  
4699                } else {                } else {
4700                  !!!insert-element ('tr');                  !!!cp ('t218');
                 ## reprocess  
4701                }                }
4702                redo B;  
4703              } elsif ({                if ($token->{tag_name} eq 'col') {
4704                        caption => 1, col => 1, colgroup => 1,                  ## Clear back to table context
4705                        tbody => 1, tfoot => 1, thead => 1,                  while (not ($self->{open_elements}->[-1]->[1]
4706                       }->{$token->{tag_name}}) {                                  & TABLE_SCOPING_EL)) {
4707                ## have an element in table scope                    !!!cp ('t219');
4708                my $i;                    ## ISSUE: Can this state be reached?
4709                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                    pop @{$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;  
4710                  }                  }
4711                } # INSCOPE                  
4712                unless (defined $i) {                  !!!insert-element ('colgroup',, $token);
4713                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
4714                  ## Ignore the token                  ## reprocess
4715                    redo B;
4716                  } elsif ({
4717                            caption => 1,
4718                            colgroup => 1,
4719                            tbody => 1, tfoot => 1, thead => 1,
4720                           }->{$token->{tag_name}}) {
4721                    ## Clear back to table context
4722                    while (not ($self->{open_elements}->[-1]->[1]
4723                                    & TABLE_SCOPING_EL)) {
4724                      !!!cp ('t220');
4725                      ## ISSUE: Can this state be reached?
4726                      pop @{$self->{open_elements}};
4727                    }
4728                    
4729                    push @$active_formatting_elements, ['#marker', '']
4730                        if $token->{tag_name} eq 'caption';
4731                    
4732                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4733                    $self->{insertion_mode} = {
4734                                               caption => IN_CAPTION_IM,
4735                                               colgroup => IN_COLUMN_GROUP_IM,
4736                                               tbody => IN_TABLE_BODY_IM,
4737                                               tfoot => IN_TABLE_BODY_IM,
4738                                               thead => IN_TABLE_BODY_IM,
4739                                              }->{$token->{tag_name}};
4740                  !!!next-token;                  !!!next-token;
4741                  redo B;                  redo B;
4742                  } else {
4743                    die "$0: in table: <>: $token->{tag_name}";
4744                }                }
   
               ## 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;  
4745              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
4746                ## NOTE: This is a code clone of "table in table"                !!!parse-error (type => 'not closed',
4747                !!!parse-error (type => 'not closed:table');                                value => $self->{open_elements}->[-1]->[0]
4748                                      ->manakai_local_name,
4749                                  token => $token);
4750    
4751                ## As if </table>                ## As if </table>
4752                ## have a table element in table scope                ## have a table element in table scope
4753                my $i;                my $i;
4754                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4755                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4756                  if ($node->[1] eq 'table') {                  if ($node->[1] & TABLE_EL) {
4757                      !!!cp ('t221');
4758                    $i = $_;                    $i = $_;
4759                    last INSCOPE;                    last INSCOPE;
4760                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
4761                            table => 1, html => 1,                    !!!cp ('t222');
                          }->{$node->[1]}) {  
4762                    last INSCOPE;                    last INSCOPE;
4763                  }                  }
4764                } # INSCOPE                } # INSCOPE
4765                unless (defined $i) {                unless (defined $i) {
4766                  !!!parse-error (type => 'unmatched end tag:table');                  !!!cp ('t223');
4767    ## TODO: The following is wrong, maybe.
4768                    !!!parse-error (type => 'unmatched end tag:table', token => $token);
4769                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
4770                  !!!next-token;                  !!!next-token;
4771                  redo B;                  redo B;
4772                }                }
4773                                
4774    ## TODO: Followings are removed from the latest spec.
4775                ## generate implied end tags                ## generate implied end tags
4776                if ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
4777                     dd => 1, dt => 1, li => 1, p => 1,                  !!!cp ('t224');
4778                     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;  
4779                }                }
4780    
4781                if ($self->{open_elements}->[-1]->[1] ne 'table') {                unless ($self->{open_elements}->[-1]->[1] & TABLE_EL) {
4782                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t225');
4783                    ## NOTE: |<table><tr><table>|
4784                    !!!parse-error (type => 'not closed',
4785                                    value => $self->{open_elements}->[-1]->[0]
4786                                        ->manakai_local_name,
4787                                    token => $token);
4788                  } else {
4789                    !!!cp ('t226');
4790                }                }
4791    
4792                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
4793                  pop @{$open_tables};
4794    
4795                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
4796    
4797                ## reprocess                ## reprocess
4798                redo B;                redo B;
4799              } else {          } elsif ($token->{tag_name} eq 'style') {
4800                #            if (not $open_tables->[-1]->[1]) { # tainted
4801              }              !!!cp ('t227.8');
4802            } elsif ($token->{type} eq 'end tag') {              ## NOTE: This is a "as if in head" code clone.
4803              if ({              $parse_rcdata->(CDATA_CONTENT_MODEL);
4804                   tbody => 1, tfoot => 1, thead => 1,              redo B;
4805                  }->{$token->{tag_name}}) {            } else {
4806                ## have an element in table scope              !!!cp ('t227.7');
4807                my $i;              #
4808                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            }
4809                  my $node = $self->{open_elements}->[$_];          } elsif ($token->{tag_name} eq 'script') {
4810                  if ($node->[1] eq $token->{tag_name}) {            if (not $open_tables->[-1]->[1]) { # tainted
4811                    $i = $_;              !!!cp ('t227.6');
4812                    last INSCOPE;              ## NOTE: This is a "as if in head" code clone.
4813                  } elsif ({              $script_start_tag->();
4814                            table => 1, html => 1,              redo B;
4815                           }->{$node->[1]}) {            } else {
4816                    last INSCOPE;              !!!cp ('t227.5');
4817                  }              #
4818                } # INSCOPE            }
4819                unless (defined $i) {          } elsif ($token->{tag_name} eq 'input') {
4820                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            if (not $open_tables->[-1]->[1]) { # tainted
4821                  ## Ignore the token              if ($token->{attributes}->{type}) { ## TODO: case
4822                  !!!next-token;                my $type = lc $token->{attributes}->{type}->{value};
4823                  redo B;                if ($type eq 'hidden') {
4824                }                  !!!cp ('t227.3');
4825                    !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);
4826    
4827                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4828    
4829                    ## TODO: form element pointer
4830    
               ## 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]);  
4831                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
               }  
4832    
               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  
4833                  !!!next-token;                  !!!next-token;
4834                  redo B;                  redo B;
4835                  } else {
4836                    !!!cp ('t227.2');
4837                    #
4838                }                }
   
               ## 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;  
4839              } else {              } else {
4840                  !!!cp ('t227.1');
4841                #                #
4842              }              }
4843            } else {            } else {
4844                !!!cp ('t227.4');
4845              #              #
4846            }            }
4847                      } else {
4848            ## As if in table            !!!cp ('t227');
4849            !!!parse-error (type => 'in table:'.$token->{tag_name});            #
4850            $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;  
               }  
             }  
   
             !!!parse-error (type => 'in table:#character');  
4851    
4852              ## As if in body, but insert into foster parent element          !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);
             ## 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});  
               } 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';  
4853    
4854                push @$active_formatting_elements, ['#marker', ''];          $insert = $insert_to_foster;
4855                          #
4856                !!!next-token;        } elsif ($token->{type} == END_TAG_TOKEN) {
4857                redo B;              if ($token->{tag_name} eq 'tr' and
4858              } elsif ({                  $self->{insertion_mode} == IN_ROW_IM) {
                       caption => 1, col => 1, colgroup => 1,  
                       tbody => 1, tfoot => 1, thead => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               ## As if </tr>  
4859                ## have an element in table scope                ## have an element in table scope
4860                my $i;                my $i;
4861                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4862                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4863                  if ($node->[1] eq 'tr') {                  if ($node->[1] & TABLE_ROW_EL) {
4864                      !!!cp ('t228');
4865                    $i = $_;                    $i = $_;
4866                    last INSCOPE;                    last INSCOPE;
4867                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
4868                            table => 1, html => 1,                    !!!cp ('t229');
                          }->{$node->[1]}) {  
4869                    last INSCOPE;                    last INSCOPE;
4870                  }                  }
4871                } # INSCOPE                } # INSCOPE
4872                unless (defined $i) {                unless (defined $i) {
4873                  !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});                  !!!cp ('t230');
4874                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4875                  ## Ignore the token                  ## Ignore the token
4876                  !!!next-token;                  !!!next-token;
4877                  redo B;                  redo B;
4878                  } else {
4879                    !!!cp ('t232');
4880                }                }
4881    
4882                ## Clear back to table row context                ## Clear back to table row context
4883                while (not {                while (not ($self->{open_elements}->[-1]->[1]
4884                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
4885                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t231');
4886                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this state be reached?
4887                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4888                }                }
4889    
4890                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}}; # tr
4891                $self->{insertion_mode} = 'in table body';                $self->{insertion_mode} = IN_TABLE_BODY_IM;
4892                ## reprocess                !!!next-token;
4893                redo B;                redo B;
4894              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
4895                ## NOTE: This is a code clone of "table in table"                if ($self->{insertion_mode} == IN_ROW_IM) {
4896                !!!parse-error (type => 'not closed:table');                  ## As if </tr>
4897                    ## have an element in table scope
4898                ## As if </table>                  my $i;
4899                ## have a table element in table scope                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4900                my $i;                    my $node = $self->{open_elements}->[$_];
4901                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                    if ($node->[1] & TABLE_ROW_EL) {
4902                  my $node = $self->{open_elements}->[$_];                      !!!cp ('t233');
4903                  if ($node->[1] eq 'table') {                      $i = $_;
4904                    $i = $_;                      last INSCOPE;
4905                    last INSCOPE;                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
4906                  } elsif ({                      !!!cp ('t234');
4907                            table => 1, html => 1,                      last INSCOPE;
4908                           }->{$node->[1]}) {                    }
4909                    last INSCOPE;                  } # INSCOPE
4910                    unless (defined $i) {
4911                      !!!cp ('t235');
4912    ## TODO: The following is wrong.
4913                      !!!parse-error (type => 'unmatched end tag:'.$token->{type}, token => $token);
4914                      ## Ignore the token
4915                      !!!next-token;
4916                      redo B;
4917                  }                  }
4918                } # INSCOPE                  
4919                unless (defined $i) {                  ## Clear back to table row context
4920                  !!!parse-error (type => 'unmatched end tag:table');                  while (not ($self->{open_elements}->[-1]->[1]
4921                  ## Ignore tokens </table><table>                                  & TABLE_ROW_SCOPING_EL)) {
4922                  !!!next-token;                    !!!cp ('t236');
4923                  redo B;  ## ISSUE: Can this state be reached?
4924                }                    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;  
4925                  }                  }
4926                } # INSCOPE                  
4927                unless (defined $i) {                  pop @{$self->{open_elements}}; # tr
4928                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
4929                  ## Ignore the token                  ## reprocess in the "in table body" insertion mode...
                 !!!next-token;  
                 redo B;  
4930                }                }
4931    
4932                ## Clear back to table row context                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
4933                while (not {                  ## have an element in table scope
4934                  tr => 1, html => 1,                  my $i;
4935                }->{$self->{open_elements}->[-1]->[1]}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4936                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    my $node = $self->{open_elements}->[$_];
4937                      if ($node->[1] & TABLE_ROW_GROUP_EL) {
4938                        !!!cp ('t237');
4939                        $i = $_;
4940                        last INSCOPE;
4941                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4942                        !!!cp ('t238');
4943                        last INSCOPE;
4944                      }
4945                    } # INSCOPE
4946                    unless (defined $i) {
4947                      !!!cp ('t239');
4948                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4949                      ## Ignore the token
4950                      !!!next-token;
4951                      redo B;
4952                    }
4953                    
4954                    ## Clear back to table body context
4955                    while (not ($self->{open_elements}->[-1]->[1]
4956                                    & TABLE_ROWS_SCOPING_EL)) {
4957                      !!!cp ('t240');
4958                      pop @{$self->{open_elements}};
4959                    }
4960                    
4961                    ## As if <{current node}>
4962                    ## have an element in table scope
4963                    ## true by definition
4964                    
4965                    ## Clear back to table body context
4966                    ## nop by definition
4967                    
4968                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4969                    $self->{insertion_mode} = IN_TABLE_IM;
4970                    ## reprocess in the "in table" insertion mode...
4971                }                }
4972    
4973                pop @{$self->{open_elements}}; # tr                ## NOTE: </table> in the "in table" insertion mode.
4974                $self->{insertion_mode} = 'in table body';                ## When you edit the code fragment below, please ensure that
4975                !!!next-token;                ## the code for <table> in the "in table" insertion mode
4976                redo B;                ## is synced with it.
4977              } elsif ($token->{tag_name} eq 'table') {  
4978                ## As if </tr>                ## have a table element in table scope
               ## have an element in table scope  
4979                my $i;                my $i;
4980                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4981                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4982                  if ($node->[1] eq 'tr') {                  if ($node->[1] & TABLE_EL) {
4983                      !!!cp ('t241');
4984                    $i = $_;                    $i = $_;
4985                    last INSCOPE;                    last INSCOPE;
4986                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
4987                            table => 1, html => 1,                    !!!cp ('t242');
                          }->{$node->[1]}) {  
4988                    last INSCOPE;                    last INSCOPE;
4989                  }                  }
4990                } # INSCOPE                } # INSCOPE
4991                unless (defined $i) {                unless (defined $i) {
4992                  !!!parse-error (type => 'unmatched end tag:'.$token->{type});                  !!!cp ('t243');
4993                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4994                  ## Ignore the token                  ## Ignore the token
4995                  !!!next-token;                  !!!next-token;
4996                  redo B;                  redo B;
4997                }                }
4998                    
4999                ## Clear back to table row context                splice @{$self->{open_elements}}, $i;
5000                while (not {                pop @{$open_tables};
5001                  tr => 1, html => 1,                
5002                }->{$self->{open_elements}->[-1]->[1]}) {                $self->_reset_insertion_mode;
5003                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                
5004                  pop @{$self->{open_elements}};                !!!next-token;
               }  
   
               pop @{$self->{open_elements}}; # tr  
               $self->{insertion_mode} = 'in table body';  
               ## reprocess  
5005                redo B;                redo B;
5006              } elsif ({              } elsif ({
5007                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
5008                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}} and
5009                ## have an element in table scope                       $self->{insertion_mode} & ROW_IMS) {
5010                my $i;                if ($self->{insertion_mode} == IN_ROW_IM) {
5011                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  ## have an element in table scope
5012                  my $node = $self->{open_elements}->[$_];                  my $i;
5013                  if ($node->[1] eq $token->{tag_name}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5014                    $i = $_;                    my $node = $self->{open_elements}->[$_];
5015                    last INSCOPE;                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5016                  } elsif ({                      !!!cp ('t247');
5017                            table => 1, html => 1,                      $i = $_;
5018                           }->{$node->[1]}) {                      last INSCOPE;
5019                    last INSCOPE;                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5020                        !!!cp ('t248');
5021                        last INSCOPE;
5022                      }
5023                    } # INSCOPE
5024                      unless (defined $i) {
5025                        !!!cp ('t249');
5026                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5027                        ## Ignore the token
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                        !!!next-token;
5051                        redo B;
5052                      }
5053                    
5054                    ## Clear back to table row context
5055                    while (not ($self->{open_elements}->[-1]->[1]
5056                                    & TABLE_ROW_SCOPING_EL)) {
5057                      !!!cp ('t253');
5058    ## ISSUE: Can this case be reached?
5059                      pop @{$self->{open_elements}};
5060                  }                  }
5061                } # INSCOPE                  
5062                unless (defined $i) {                  pop @{$self->{open_elements}}; # tr
5063                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
5064                  ## Ignore the token                  ## reprocess in the "in table body" insertion mode...
                 !!!next-token;  
                 redo B;  
5065                }                }
5066    
               ## As if </tr>  
5067                ## have an element in table scope                ## have an element in table scope
5068                my $i;                my $i;
5069                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5070                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5071                  if ($node->[1] eq 'tr') {                  if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5072                      !!!cp ('t254');
5073                    $i = $_;                    $i = $_;
5074                    last INSCOPE;                    last INSCOPE;
5075                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5076                            table => 1, html => 1,                    !!!cp ('t255');
                          }->{$node->[1]}) {  
5077                    last INSCOPE;                    last INSCOPE;
5078                  }                  }
5079                } # INSCOPE                } # INSCOPE
5080                unless (defined $i) {                unless (defined $i) {
5081                  !!!parse-error (type => 'unmatched end tag:tr');                  !!!cp ('t256');
5082                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5083                  ## Ignore the token                  ## Ignore the token
5084                  !!!next-token;                  !!!next-token;
5085                  redo B;                  redo B;
5086                }                }
5087    
5088                ## Clear back to table row context                ## Clear back to table body context
5089                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5090                  tr => 1, html => 1,                                & TABLE_ROWS_SCOPING_EL)) {
5091                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t257');
5092                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this case be reached?
5093                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5094                }                }
5095    
5096                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}};
5097                $self->{insertion_mode} = 'in table body';                $self->{insertion_mode} = IN_TABLE_IM;
5098                ## reprocess                !!!next-token;
5099                redo B;                redo B;
5100              } elsif ({              } elsif ({
5101                        body => 1, caption => 1, col => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
5102                        colgroup => 1, html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
5103                          tr => 1, # $self->{insertion_mode} == IN_ROW_IM
5104                          tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
5105                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5106                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!cp ('t258');
5107                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5108                ## Ignore the token                ## Ignore the token
5109                !!!next-token;                !!!next-token;
5110                redo B;                redo B;
5111              } else {          } else {
5112                #            !!!cp ('t259');
5113              }            !!!parse-error (type => 'in table:/'.$token->{tag_name}, token => $token);
           } else {  
             #  
           }  
   
           ## As if in table  
           !!!parse-error (type => 'in table:'.$token->{tag_name});  
           $in_body->($insert_to_foster);  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'in select') {  
           if ($token->{type} eq 'character') {  
             $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 'option') {  
               if ($self->{open_elements}->[-1]->[1] eq 'option') {  
                 ## As if </option>  
                 pop @{$self->{open_elements}};  
               }  
   
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'optgroup') {  
               if ($self->{open_elements}->[-1]->[1] eq 'option') {  
                 ## As if </option>  
                 pop @{$self->{open_elements}};  
               }  
5114    
5115                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            $insert = $insert_to_foster;
5116                  ## As if </optgroup>            #
5117                  pop @{$self->{open_elements}};          }
5118                }        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5119            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
5120                    @{$self->{open_elements}} == 1) { # redundant, maybe
5121              !!!parse-error (type => 'in body:#eof', token => $token);
5122              !!!cp ('t259.1');
5123              #
5124            } else {
5125              !!!cp ('t259.2');
5126              #
5127            }
5128    
5129                !!!insert-element ($token->{tag_name}, $token->{attributes});          ## Stop parsing
5130                !!!next-token;          last B;
5131                redo B;        } else {
5132              } elsif ($token->{tag_name} eq 'select') {          die "$0: $token->{type}: Unknown token type";
5133                !!!parse-error (type => 'not closed:select');        }
5134                ## As if </select> instead      } elsif ($self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
5135                ## have an element in table scope            if ($token->{type} == CHARACTER_TOKEN) {
5136                my $i;              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5137                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5138                  my $node = $self->{open_elements}->[$_];                unless (length $token->{data}) {
5139                  if ($node->[1] eq $token->{tag_name}) {                  !!!cp ('t260');
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:select');  
                 ## Ignore the token  
5140                  !!!next-token;                  !!!next-token;
5141                  redo B;                  redo B;
5142                }                }
5143                              }
5144                splice @{$self->{open_elements}}, $i;              
5145                !!!cp ('t261');
5146                $self->_reset_insertion_mode;              #
5147              } elsif ($token->{type} == START_TAG_TOKEN) {
5148                if ($token->{tag_name} eq 'col') {
5149                  !!!cp ('t262');
5150                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5151                  pop @{$self->{open_elements}};
5152                !!!next-token;                !!!next-token;
5153                redo B;                redo B;
5154              } else {              } else {
5155                  !!!cp ('t263');
5156                #                #
5157              }              }
5158            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
5159              if ($token->{tag_name} eq 'optgroup') {              if ($token->{tag_name} eq 'colgroup') {
5160                if ($self->{open_elements}->[-1]->[1] eq 'option' and                if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5161                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {                  !!!cp ('t264');
5162                  ## As if </option>                  !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);
                 splice @{$self->{open_elements}}, -2;  
               } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {  
                 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 '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});  
5163                  ## Ignore the token                  ## Ignore the token
5164                  !!!next-token;                  !!!next-token;
5165                  redo B;                  redo B;
5166                }                } else {
5167                                  !!!cp ('t265');
5168                splice @{$self->{open_elements}}, $i;                  pop @{$self->{open_elements}}; # colgroup
5169                    $self->{insertion_mode} = IN_TABLE_IM;
               $self->_reset_insertion_mode;  
   
               !!!next-token;  
               redo B;  
             } elsif ({  
                       caption => 1, table => 1, tbody => 1,  
                       tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,  
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 
               ## 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) {  
                 ## Ignore the token  
5170                  !!!next-token;                  !!!next-token;
5171                  redo B;                  redo B;            
               }  
                 
               ## As if </select>  
               ## have an element in table scope  
               undef $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'select') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:select');  
                 ## Ignore the </select> token  
                 !!!next-token; ## TODO: ok?  
                 redo B;  
5172                }                }
5173                              } elsif ($token->{tag_name} eq 'col') {
5174                splice @{$self->{open_elements}}, $i;                !!!cp ('t266');
5175                  !!!parse-error (type => 'unmatched end tag:col', token => $token);
5176                $self->_reset_insertion_mode;                ## Ignore the token
5177                  !!!next-token;
               ## reprocess  
5178                redo B;                redo B;
5179              } else {              } else {
5180                  !!!cp ('t267');
5181                #                #
5182              }              }
5183          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5184            if ($self->{open_elements}->[-1]->[1] & HTML_EL and
5185                @{$self->{open_elements}} == 1) { # redundant, maybe
5186              !!!cp ('t270.2');
5187              ## Stop parsing.
5188              last B;
5189            } else {
5190              ## NOTE: As if </colgroup>.
5191              !!!cp ('t270.1');
5192              pop @{$self->{open_elements}}; # colgroup
5193              $self->{insertion_mode} = IN_TABLE_IM;
5194              ## Reprocess.
5195              redo B;
5196            }
5197          } else {
5198            die "$0: $token->{type}: Unknown token type";
5199          }
5200    
5201              ## As if </colgroup>
5202              if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5203                !!!cp ('t269');
5204    ## TODO: Wrong error type?
5205                !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);
5206                ## Ignore the token
5207                !!!next-token;
5208                redo B;
5209            } else {            } else {
5210              #              !!!cp ('t270');
5211                pop @{$self->{open_elements}}; # colgroup
5212                $self->{insertion_mode} = IN_TABLE_IM;
5213                ## reprocess
5214                redo B;
5215              }
5216        } elsif ($self->{insertion_mode} & SELECT_IMS) {
5217          if ($token->{type} == CHARACTER_TOKEN) {
5218            !!!cp ('t271');
5219            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5220            !!!next-token;
5221            redo B;
5222          } elsif ($token->{type} == START_TAG_TOKEN) {
5223            if ($token->{tag_name} eq 'option') {
5224              if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5225                !!!cp ('t272');
5226                ## As if </option>
5227                pop @{$self->{open_elements}};
5228              } else {
5229                !!!cp ('t273');
5230            }            }
5231    
5232            !!!parse-error (type => 'in select:'.$token->{tag_name});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5233              !!!next-token;
5234              redo B;
5235            } elsif ($token->{tag_name} eq 'optgroup') {
5236              if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5237                !!!cp ('t274');
5238                ## As if </option>
5239                pop @{$self->{open_elements}};
5240              } else {
5241                !!!cp ('t275');
5242              }
5243    
5244              if ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
5245                !!!cp ('t276');
5246                ## As if </optgroup>
5247                pop @{$self->{open_elements}};
5248              } else {
5249                !!!cp ('t277');
5250              }
5251    
5252              !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5253              !!!next-token;
5254              redo B;
5255            } elsif ($token->{tag_name} eq 'select' or
5256                     $token->{tag_name} eq 'input' or
5257                     ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
5258                      {
5259                       caption => 1, table => 1,
5260                       tbody => 1, tfoot => 1, thead => 1,
5261                       tr => 1, td => 1, th => 1,
5262                      }->{$token->{tag_name}})) {
5263              ## TODO: The type below is not good - <select> is replaced by </select>
5264              !!!parse-error (type => 'not closed:select', token => $token);
5265              ## NOTE: As if the token were </select> (<select> case) or
5266              ## as if there were </select> (otherwise).
5267              ## have an element in table scope
5268              my $i;
5269              INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5270                my $node = $self->{open_elements}->[$_];
5271                if ($node->[1] & SELECT_EL) {
5272                  !!!cp ('t278');
5273                  $i = $_;
5274                  last INSCOPE;
5275                } elsif ($node->[1] & TABLE_SCOPING_EL) {
5276                  !!!cp ('t279');
5277                  last INSCOPE;
5278                }
5279              } # INSCOPE
5280              unless (defined $i) {
5281                !!!cp ('t280');
5282                !!!parse-error (type => 'unmatched end tag:select', token => $token);
5283                ## Ignore the token
5284                !!!next-token;
5285                redo B;
5286              }
5287                  
5288              !!!cp ('t281');
5289              splice @{$self->{open_elements}}, $i;
5290    
5291              $self->_reset_insertion_mode;
5292    
5293              if ($token->{tag_name} eq 'select') {
5294                !!!cp ('t281.2');
5295                !!!next-token;
5296                redo B;
5297              } else {
5298                !!!cp ('t281.1');
5299                ## Reprocess the token.
5300                redo B;
5301              }
5302            } else {
5303              !!!cp ('t282');
5304              !!!parse-error (type => 'in select:'.$token->{tag_name}, token => $token);
5305            ## Ignore the token            ## Ignore the token
5306            !!!next-token;            !!!next-token;
5307            redo B;            redo B;
5308          } elsif ($self->{insertion_mode} eq 'after body') {          }
5309            if ($token->{type} eq 'character') {        } elsif ($token->{type} == END_TAG_TOKEN) {
5310              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{tag_name} eq 'optgroup') {
5311                my $data = $1;            if ($self->{open_elements}->[-1]->[1] & OPTION_EL and
5312                ## As if in body                $self->{open_elements}->[-2]->[1] & OPTGROUP_EL) {
5313                $reconstruct_active_formatting_elements->($insert_to_current);              !!!cp ('t283');
5314                ## As if </option>
5315                splice @{$self->{open_elements}}, -2;
5316              } elsif ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
5317                !!!cp ('t284');
5318                pop @{$self->{open_elements}};
5319              } else {
5320                !!!cp ('t285');
5321                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5322                ## Ignore the token
5323              }
5324              !!!next-token;
5325              redo B;
5326            } elsif ($token->{tag_name} eq 'option') {
5327              if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5328                !!!cp ('t286');
5329                pop @{$self->{open_elements}};
5330              } else {
5331                !!!cp ('t287');
5332                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5333                ## Ignore the token
5334              }
5335              !!!next-token;
5336              redo B;
5337            } elsif ($token->{tag_name} eq 'select') {
5338              ## have an element in table scope
5339              my $i;
5340              INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5341                my $node = $self->{open_elements}->[$_];
5342                if ($node->[1] & SELECT_EL) {
5343                  !!!cp ('t288');
5344                  $i = $_;
5345                  last INSCOPE;
5346                } elsif ($node->[1] & TABLE_SCOPING_EL) {
5347                  !!!cp ('t289');
5348                  last INSCOPE;
5349                }
5350              } # INSCOPE
5351              unless (defined $i) {
5352                !!!cp ('t290');
5353                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5354                ## Ignore the token
5355                !!!next-token;
5356                redo B;
5357              }
5358                                
5359                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            !!!cp ('t291');
5360              splice @{$self->{open_elements}}, $i;
5361    
5362                unless (length $token->{data}) {            $self->_reset_insertion_mode;
5363                  !!!next-token;  
5364                  redo B;            !!!next-token;
5365                }            redo B;
5366            } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
5367                     {
5368                      caption => 1, table => 1, tbody => 1,
5369                      tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
5370                     }->{$token->{tag_name}}) {
5371    ## TODO: The following is wrong?
5372              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5373                  
5374              ## have an element in table scope
5375              my $i;
5376              INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5377                my $node = $self->{open_elements}->[$_];
5378                if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5379                  !!!cp ('t292');
5380                  $i = $_;
5381                  last INSCOPE;
5382                } elsif ($node->[1] & TABLE_SCOPING_EL) {
5383                  !!!cp ('t293');
5384                  last INSCOPE;
5385              }              }
5386                          } # INSCOPE
5387              #            unless (defined $i) {
5388              !!!parse-error (type => 'after body:#character');              !!!cp ('t294');
5389            } elsif ($token->{type} eq 'start tag') {              ## Ignore the token
5390              !!!parse-error (type => 'after body:'.$token->{tag_name});              !!!next-token;
5391              #              redo B;
5392            } elsif ($token->{type} eq 'end tag') {            }
5393              if ($token->{tag_name} eq 'html') {                
5394                if (defined $self->{inner_html_node}) {            ## As if </select>
5395                  !!!parse-error (type => 'unmatched end tag:html');            ## have an element in table scope
5396                  ## Ignore the token            undef $i;
5397                  !!!next-token;            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5398                  redo B;              my $node = $self->{open_elements}->[$_];
5399                } else {              if ($node->[1] & SELECT_EL) {
5400                  $previous_insertion_mode = $self->{insertion_mode};                !!!cp ('t295');
5401                  $self->{insertion_mode} = 'trailing end';                $i = $_;
5402                  !!!next-token;                last INSCOPE;
5403                  redo B;              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5404                }  ## ISSUE: Can this state be reached?
5405              } else {                !!!cp ('t296');
5406                !!!parse-error (type => 'after body:/'.$token->{tag_name});                last INSCOPE;
5407              }              }
5408            } else {            } # INSCOPE
5409              die "$0: $token->{type}: Unknown token type";            unless (defined $i) {
5410                !!!cp ('t297');
5411    ## TODO: The following error type is correct?
5412                !!!parse-error (type => 'unmatched end tag:select', token => $token);
5413                ## Ignore the </select> token
5414                !!!next-token; ## TODO: ok?
5415                redo B;
5416            }            }
5417                  
5418              !!!cp ('t298');
5419              splice @{$self->{open_elements}}, $i;
5420    
5421              $self->_reset_insertion_mode;
5422    
           $self->{insertion_mode} = 'in body';  
5423            ## reprocess            ## reprocess
5424            redo B;            redo B;
5425      } elsif ($self->{insertion_mode} eq 'in frameset') {          } else {
5426        if ($token->{type} eq 'character') {            !!!cp ('t299');
5427              !!!parse-error (type => 'in select:/'.$token->{tag_name}, token => $token);
5428              ## Ignore the token
5429              !!!next-token;
5430              redo B;
5431            }
5432          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5433            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
5434                    @{$self->{open_elements}} == 1) { # redundant, maybe
5435              !!!cp ('t299.1');
5436              !!!parse-error (type => 'in body:#eof', token => $token);
5437            } else {
5438              !!!cp ('t299.2');
5439            }
5440    
5441            ## Stop parsing.
5442            last B;
5443          } else {
5444            die "$0: $token->{type}: Unknown token type";
5445          }
5446        } elsif ($self->{insertion_mode} & BODY_AFTER_IMS) {
5447          if ($token->{type} == CHARACTER_TOKEN) {
5448          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5449              my $data = $1;
5450              ## As if in body
5451              $reconstruct_active_formatting_elements->($insert_to_current);
5452                  
5453            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5454              
5455            unless (length $token->{data}) {            unless (length $token->{data}) {
5456                !!!cp ('t300');
5457              !!!next-token;              !!!next-token;
5458              redo B;              redo B;
5459            }            }
5460          }          }
5461            
5462            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
5463              !!!cp ('t301');
5464              !!!parse-error (type => 'after html:#character', token => $token);
5465    
5466          !!!parse-error (type => 'in frameset:#character');            ## Reprocess in the "after body" insertion mode.
5467          ## Ignore the token          } else {
5468          !!!next-token;            !!!cp ('t302');
5469            }
5470            
5471            ## "after body" insertion mode
5472            !!!parse-error (type => 'after body:#character', token => $token);
5473    
5474            $self->{insertion_mode} = IN_BODY_IM;
5475            ## reprocess
5476            redo B;
5477          } elsif ($token->{type} == START_TAG_TOKEN) {
5478            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
5479              !!!cp ('t303');
5480              !!!parse-error (type => 'after html:'.$token->{tag_name}, token => $token);
5481              
5482              ## Reprocess in the "after body" insertion mode.
5483            } else {
5484              !!!cp ('t304');
5485            }
5486    
5487            ## "after body" insertion mode
5488            !!!parse-error (type => 'after body:'.$token->{tag_name}, token => $token);
5489    
5490            $self->{insertion_mode} = IN_BODY_IM;
5491            ## reprocess
5492          redo B;          redo B;
5493        } elsif ($token->{type} eq 'start tag') {        } elsif ($token->{type} == END_TAG_TOKEN) {
5494          if ($token->{tag_name} eq 'frameset') {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
5495            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!cp ('t305');
5496              !!!parse-error (type => 'after html:/'.$token->{tag_name}, token => $token);
5497              
5498              $self->{insertion_mode} = AFTER_BODY_IM;
5499              ## Reprocess in the "after body" insertion mode.
5500            } else {
5501              !!!cp ('t306');
5502            }
5503    
5504            ## "after body" insertion mode
5505            if ($token->{tag_name} eq 'html') {
5506              if (defined $self->{inner_html_node}) {
5507                !!!cp ('t307');
5508                !!!parse-error (type => 'unmatched end tag:html', token => $token);
5509                ## Ignore the token
5510                !!!next-token;
5511                redo B;
5512              } else {
5513                !!!cp ('t308');
5514                $self->{insertion_mode} = AFTER_HTML_BODY_IM;
5515                !!!next-token;
5516                redo B;
5517              }
5518            } else {
5519              !!!cp ('t309');
5520              !!!parse-error (type => 'after body:/'.$token->{tag_name}, token => $token);
5521    
5522              $self->{insertion_mode} = IN_BODY_IM;
5523              ## reprocess
5524              redo B;
5525            }
5526          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5527            !!!cp ('t309.2');
5528            ## Stop parsing
5529            last B;
5530          } else {
5531            die "$0: $token->{type}: Unknown token type";
5532          }
5533        } elsif ($self->{insertion_mode} & FRAME_IMS) {
5534          if ($token->{type} == CHARACTER_TOKEN) {
5535            if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5536              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5537              
5538              unless (length $token->{data}) {
5539                !!!cp ('t310');
5540                !!!next-token;
5541                redo B;
5542              }
5543            }
5544            
5545            if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
5546              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
5547                !!!cp ('t311');
5548                !!!parse-error (type => 'in frameset:#character', token => $token);
5549              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
5550                !!!cp ('t312');
5551                !!!parse-error (type => 'after frameset:#character', token => $token);
5552              } else { # "after html frameset"
5553                !!!cp ('t313');
5554                !!!parse-error (type => 'after html:#character', token => $token);
5555    
5556                $self->{insertion_mode} = AFTER_FRAMESET_IM;
5557                ## Reprocess in the "after frameset" insertion mode.
5558                !!!parse-error (type => 'after frameset:#character', token => $token);
5559              }
5560              
5561              ## Ignore the token.
5562              if (length $token->{data}) {
5563                !!!cp ('t314');
5564                ## reprocess the rest of characters
5565              } else {
5566                !!!cp ('t315');
5567                !!!next-token;
5568              }
5569              redo B;
5570            }
5571            
5572            die qq[$0: Character "$token->{data}"];
5573          } elsif ($token->{type} == START_TAG_TOKEN) {
5574            if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
5575              !!!cp ('t316');
5576              !!!parse-error (type => 'after html:'.$token->{tag_name}, token => $token);
5577    
5578              $self->{insertion_mode} = AFTER_FRAMESET_IM;
5579              ## Process in the "after frameset" insertion mode.
5580            } else {
5581              !!!cp ('t317');
5582            }
5583    
5584            if ($token->{tag_name} eq 'frameset' and
5585                $self->{insertion_mode} == IN_FRAMESET_IM) {
5586              !!!cp ('t318');
5587              !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5588            !!!next-token;            !!!next-token;
5589            redo B;            redo B;
5590          } elsif ($token->{tag_name} eq 'frame') {          } elsif ($token->{tag_name} eq 'frame' and
5591            !!!insert-element ($token->{tag_name}, $token->{attributes});                   $self->{insertion_mode} == IN_FRAMESET_IM) {
5592              !!!cp ('t319');
5593              !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5594            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
5595            !!!next-token;            !!!next-token;
5596            redo B;            redo B;
5597          } elsif ($token->{tag_name} eq 'noframes') {          } elsif ($token->{tag_name} eq 'noframes') {
5598              !!!cp ('t320');
5599            ## NOTE: As if in body.            ## NOTE: As if in body.
5600            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);            $parse_rcdata->(CDATA_CONTENT_MODEL);
5601            redo B;            redo B;
5602          } else {          } else {
5603            !!!parse-error (type => 'in frameset:'.$token->{tag_name});            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
5604                !!!cp ('t321');
5605                !!!parse-error (type => 'in frameset:'.$token->{tag_name}, token => $token);
5606              } else {
5607                !!!cp ('t322');
5608                !!!parse-error (type => 'after frameset:'.$token->{tag_name}, token => $token);
5609              }
5610            ## Ignore the token            ## Ignore the token
5611            !!!next-token;            !!!next-token;
5612            redo B;            redo B;
5613          }          }
5614        } elsif ($token->{type} eq 'end tag') {        } elsif ($token->{type} == END_TAG_TOKEN) {
5615          if ($token->{tag_name} eq 'frameset') {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
5616            if ($self->{open_elements}->[-1]->[1] eq 'html' and            !!!cp ('t323');
5617              !!!parse-error (type => 'after html:/'.$token->{tag_name}, token => $token);
5618    
5619              $self->{insertion_mode} = AFTER_FRAMESET_IM;
5620              ## Process in the "after frameset" insertion mode.
5621            } else {
5622              !!!cp ('t324');
5623            }
5624    
5625            if ($token->{tag_name} eq 'frameset' and
5626                $self->{insertion_mode} == IN_FRAMESET_IM) {
5627              if ($self->{open_elements}->[-1]->[1] & HTML_EL and
5628                @{$self->{open_elements}} == 1) {                @{$self->{open_elements}} == 1) {
5629              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t325');
5630                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5631              ## Ignore the token              ## Ignore the token
5632              !!!next-token;              !!!next-token;
5633            } else {            } else {
5634                !!!cp ('t326');
5635              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
5636              !!!next-token;              !!!next-token;
5637            }            }
5638    
5639            if (not defined $self->{inner_html_node} and            if (not defined $self->{inner_html_node} and
5640                $self->{open_elements}->[-1]->[1] ne 'frameset') {                not ($self->{open_elements}->[-1]->[1] & FRAMESET_EL)) {
5641              $self->{insertion_mode} = 'after frameset';              !!!cp ('t327');
5642                $self->{insertion_mode} = AFTER_FRAMESET_IM;
5643              } else {
5644                !!!cp ('t328');
5645            }            }
5646            redo B;            redo B;
5647            } elsif ($token->{tag_name} eq 'html' and
5648                     $self->{insertion_mode} == AFTER_FRAMESET_IM) {
5649              !!!cp ('t329');
5650              $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
5651              !!!next-token;
5652              redo B;
5653          } else {          } else {
5654            !!!parse-error (type => 'in frameset:/'.$token->{tag_name});            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
5655                !!!cp ('t330');
5656                !!!parse-error (type => 'in frameset:/'.$token->{tag_name}, token => $token);
5657              } else {
5658                !!!cp ('t331');
5659                !!!parse-error (type => 'after frameset:/'.$token->{tag_name}, token => $token);
5660              }
5661            ## Ignore the token            ## Ignore the token
5662            !!!next-token;            !!!next-token;
5663            redo B;            redo B;
5664          }          }
5665          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5666            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
5667                    @{$self->{open_elements}} == 1) { # redundant, maybe
5668              !!!cp ('t331.1');
5669              !!!parse-error (type => 'in body:#eof', token => $token);
5670            } else {
5671              !!!cp ('t331.2');
5672            }
5673            
5674            ## Stop parsing
5675            last B;
5676        } else {        } else {
5677          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5678        }        }
     } 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);  
5679    
5680                unless (length $token->{data}) {        ## ISSUE: An issue in spec here
5681                  !!!next-token;      } else {
5682                  redo B;        die "$0: $self->{insertion_mode}: Unknown insertion mode";
5683                }      }
             }  
5684    
5685              if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {      ## "in body" insertion mode
5686                !!!parse-error (type => 'after frameset:#character');      if ($token->{type} == START_TAG_TOKEN) {
5687          if ($token->{tag_name} eq 'script') {
5688            !!!cp ('t332');
5689            ## NOTE: This is an "as if in head" code clone
5690            $script_start_tag->();
5691            redo B;
5692          } elsif ($token->{tag_name} eq 'style') {
5693            !!!cp ('t333');
5694            ## NOTE: This is an "as if in head" code clone
5695            $parse_rcdata->(CDATA_CONTENT_MODEL);
5696            redo B;
5697          } elsif ({
5698                    base => 1, link => 1,
5699                   }->{$token->{tag_name}}) {
5700            !!!cp ('t334');
5701            ## NOTE: This is an "as if in head" code clone, only "-t" differs
5702            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
5703            pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
5704            !!!next-token;
5705            redo B;
5706          } elsif ($token->{tag_name} eq 'meta') {
5707            ## NOTE: This is an "as if in head" code clone, only "-t" differs
5708            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
5709            my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
5710    
5711                ## Ignore the token.          unless ($self->{confident}) {
5712                if (length $token->{data}) {            if ($token->{attributes}->{charset}) { ## TODO: And if supported
5713                  ## reprocess the rest of characters              !!!cp ('t335');
5714                } else {              $self->{change_encoding}
5715                  !!!next-token;                  ->($self, $token->{attributes}->{charset}->{value}, $token);
5716                }              
5717                redo B;              $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
5718                    ->set_user_data (manakai_has_reference =>
5719                                         $token->{attributes}->{charset}
5720                                             ->{has_reference});
5721              } elsif ($token->{attributes}->{content}) {
5722                ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.
5723                if ($token->{attributes}->{content}->{value}
5724                    =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
5725                        [\x09-\x0D\x20]*=
5726                        [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
5727                        ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
5728                  !!!cp ('t336');
5729                  $self->{change_encoding}
5730                      ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);
5731                  $meta_el->[0]->get_attribute_node_ns (undef, 'content')
5732                      ->set_user_data (manakai_has_reference =>
5733                                           $token->{attributes}->{content}
5734                                                 ->{has_reference});
5735              }              }
5736              }
5737            } else {
5738              if ($token->{attributes}->{charset}) {
5739                !!!cp ('t337');
5740                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
5741                    ->set_user_data (manakai_has_reference =>
5742                                         $token->{attributes}->{charset}
5743                                             ->{has_reference});
5744              }
5745              if ($token->{attributes}->{content}) {
5746                !!!cp ('t338');
5747                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
5748                    ->set_user_data (manakai_has_reference =>
5749                                         $token->{attributes}->{content}
5750                                             ->{has_reference});
5751              }
5752            }
5753    
5754          die qq[$0: Character "$token->{data}"];          !!!next-token;
5755        } elsif ($token->{type} eq 'start tag') {          redo B;
5756          if ($token->{tag_name} eq 'noframes') {        } elsif ($token->{tag_name} eq 'title') {
5757            ## NOTE: As if in body.          !!!cp ('t341');
5758            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);          ## NOTE: This is an "as if in head" code clone
5759            redo B;          $parse_rcdata->(RCDATA_CONTENT_MODEL);
5760            redo B;
5761          } elsif ($token->{tag_name} eq 'body') {
5762            !!!parse-error (type => 'in body:body', token => $token);
5763                  
5764            if (@{$self->{open_elements}} == 1 or
5765                not ($self->{open_elements}->[1]->[1] & BODY_EL)) {
5766              !!!cp ('t342');
5767              ## Ignore the token
5768          } else {          } else {
5769            !!!parse-error (type => 'after frameset:'.$token->{tag_name});            my $body_el = $self->{open_elements}->[1]->[0];
5770              for my $attr_name (keys %{$token->{attributes}}) {
5771                unless ($body_el->has_attribute_ns (undef, $attr_name)) {
5772                  !!!cp ('t343');
5773                  $body_el->set_attribute_ns
5774                    (undef, [undef, $attr_name],
5775                     $token->{attributes}->{$attr_name}->{value});
5776                }
5777              }
5778            }
5779            !!!next-token;
5780            redo B;
5781          } elsif ({
5782                    address => 1, blockquote => 1, center => 1, dir => 1,
5783                    div => 1, dl => 1, fieldset => 1,
5784                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
5785                    menu => 1, ol => 1, p => 1, ul => 1,
5786                    pre => 1, listing => 1,
5787                    form => 1,
5788                    table => 1,
5789                    hr => 1,
5790                   }->{$token->{tag_name}}) {
5791            if ($token->{tag_name} eq 'form' and defined $self->{form_element}) {
5792              !!!cp ('t350');
5793              !!!parse-error (type => 'in form:form', token => $token);
5794            ## Ignore the token            ## Ignore the token
5795            !!!next-token;            !!!next-token;
5796            redo B;            redo B;
5797          }          }
5798        } elsif ($token->{type} eq 'end tag') {  
5799          if ($token->{tag_name} eq 'html') {          ## has a p element in scope
5800            $previous_insertion_mode = $self->{insertion_mode};          INSCOPE: for (reverse @{$self->{open_elements}}) {
5801            $self->{insertion_mode} = 'trailing end';            if ($_->[1] & P_EL) {
5802                !!!cp ('t344');
5803                !!!back-token;
5804                $token = {type => END_TAG_TOKEN, tag_name => 'p',
5805                          line => $token->{line}, column => $token->{column}};
5806                redo B;
5807              } elsif ($_->[1] & SCOPING_EL) {
5808                !!!cp ('t345');
5809                last INSCOPE;
5810              }
5811            } # INSCOPE
5812              
5813            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
5814            if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {
5815              !!!next-token;
5816              if ($token->{type} == CHARACTER_TOKEN) {
5817                $token->{data} =~ s/^\x0A//;
5818                unless (length $token->{data}) {
5819                  !!!cp ('t346');
5820                  !!!next-token;
5821                } else {
5822                  !!!cp ('t349');
5823                }
5824              } else {
5825                !!!cp ('t348');
5826              }
5827            } elsif ($token->{tag_name} eq 'form') {
5828              !!!cp ('t347.1');
5829              $self->{form_element} = $self->{open_elements}->[-1]->[0];
5830    
5831              !!!next-token;
5832            } elsif ($token->{tag_name} eq 'table') {
5833              !!!cp ('t382');
5834              push @{$open_tables}, [$self->{open_elements}->[-1]->[0]];
5835              
5836              $self->{insertion_mode} = IN_TABLE_IM;
5837    
5838              !!!next-token;
5839            } elsif ($token->{tag_name} eq 'hr') {
5840              !!!cp ('t386');
5841              pop @{$self->{open_elements}};
5842            
5843            !!!next-token;            !!!next-token;
           redo B;  
5844          } else {          } else {
5845            !!!parse-error (type => 'after frameset:/'.$token->{tag_name});            !!!cp ('t347');
5846              !!!next-token;
5847            }
5848            redo B;
5849          } elsif ({li => 1, dt => 1, dd => 1}->{$token->{tag_name}}) {
5850            ## has a p element in scope
5851            INSCOPE: for (reverse @{$self->{open_elements}}) {
5852              if ($_->[1] & P_EL) {
5853                !!!cp ('t353');
5854                !!!back-token;
5855                $token = {type => END_TAG_TOKEN, tag_name => 'p',
5856                          line => $token->{line}, column => $token->{column}};
5857                redo B;
5858              } elsif ($_->[1] & SCOPING_EL) {
5859                !!!cp ('t354');
5860                last INSCOPE;
5861              }
5862            } # INSCOPE
5863              
5864            ## Step 1
5865            my $i = -1;
5866            my $node = $self->{open_elements}->[$i];
5867            my $li_or_dtdd = {li => {li => 1},
5868                              dt => {dt => 1, dd => 1},
5869                              dd => {dt => 1, dd => 1}}->{$token->{tag_name}};
5870            LI: {
5871              ## Step 2
5872              if ($li_or_dtdd->{$node->[0]->manakai_local_name}) {
5873                if ($i != -1) {
5874                  !!!cp ('t355');
5875                  !!!parse-error (type => 'not closed',
5876                                  value => $self->{open_elements}->[-1]->[0]
5877                                      ->manakai_local_name,
5878                                  token => $token);
5879                } else {
5880                  !!!cp ('t356');
5881                }
5882                splice @{$self->{open_elements}}, $i;
5883                last LI;
5884              } else {
5885                !!!cp ('t357');
5886              }
5887              
5888              ## Step 3
5889              if (not ($node->[1] & FORMATTING_EL) and
5890                  #not $phrasing_category->{$node->[1]} and
5891                  ($node->[1] & SPECIAL_EL or
5892                   $node->[1] & SCOPING_EL) and
5893                  not ($node->[1] & ADDRESS_EL) and
5894                  not ($node->[1] & DIV_EL)) {
5895                !!!cp ('t358');
5896                last LI;
5897              }
5898              
5899              !!!cp ('t359');
5900              ## Step 4
5901              $i--;
5902              $node = $self->{open_elements}->[$i];
5903              redo LI;
5904            } # LI
5905              
5906            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
5907            !!!next-token;
5908            redo B;
5909          } elsif ($token->{tag_name} eq 'plaintext') {
5910            ## has a p element in scope
5911            INSCOPE: for (reverse @{$self->{open_elements}}) {
5912              if ($_->[1] & P_EL) {
5913                !!!cp ('t367');
5914                !!!back-token;
5915                $token = {type => END_TAG_TOKEN, tag_name => 'p',
5916                          line => $token->{line}, column => $token->{column}};
5917                redo B;
5918              } elsif ($_->[1] & SCOPING_EL) {
5919                !!!cp ('t368');
5920                last INSCOPE;
5921              }
5922            } # INSCOPE
5923              
5924            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
5925              
5926            $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
5927              
5928            !!!next-token;
5929            redo B;
5930          } elsif ($token->{tag_name} eq 'a') {
5931            AFE: for my $i (reverse 0..$#$active_formatting_elements) {
5932              my $node = $active_formatting_elements->[$i];
5933              if ($node->[1] & A_EL) {
5934                !!!cp ('t371');
5935                !!!parse-error (type => 'in a:a', token => $token);
5936                
5937                !!!back-token;
5938                $token = {type => END_TAG_TOKEN, tag_name => 'a',
5939                          line => $token->{line}, column => $token->{column}};
5940                $formatting_end_tag->($token);
5941                
5942                AFE2: for (reverse 0..$#$active_formatting_elements) {
5943                  if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
5944                    !!!cp ('t372');
5945                    splice @$active_formatting_elements, $_, 1;
5946                    last AFE2;
5947                  }
5948                } # AFE2
5949                OE: for (reverse 0..$#{$self->{open_elements}}) {
5950                  if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {
5951                    !!!cp ('t373');
5952                    splice @{$self->{open_elements}}, $_, 1;
5953                    last OE;
5954                  }
5955                } # OE
5956                last AFE;
5957              } elsif ($node->[0] eq '#marker') {
5958                !!!cp ('t374');
5959                last AFE;
5960              }
5961            } # AFE
5962              
5963            $reconstruct_active_formatting_elements->($insert_to_current);
5964    
5965            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
5966            push @$active_formatting_elements, $self->{open_elements}->[-1];
5967    
5968            !!!next-token;
5969            redo B;
5970          } elsif ($token->{tag_name} eq 'nobr') {
5971            $reconstruct_active_formatting_elements->($insert_to_current);
5972    
5973            ## has a |nobr| element in scope
5974            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5975              my $node = $self->{open_elements}->[$_];
5976              if ($node->[1] & NOBR_EL) {
5977                !!!cp ('t376');
5978                !!!parse-error (type => 'in nobr:nobr', token => $token);
5979                !!!back-token;
5980                $token = {type => END_TAG_TOKEN, tag_name => 'nobr',
5981                          line => $token->{line}, column => $token->{column}};
5982                redo B;
5983              } elsif ($node->[1] & SCOPING_EL) {
5984                !!!cp ('t377');
5985                last INSCOPE;
5986              }
5987            } # INSCOPE
5988            
5989            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
5990            push @$active_formatting_elements, $self->{open_elements}->[-1];
5991            
5992            !!!next-token;
5993            redo B;
5994          } elsif ($token->{tag_name} eq 'button') {
5995            ## has a button element in scope
5996            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5997              my $node = $self->{open_elements}->[$_];
5998              if ($node->[1] & BUTTON_EL) {
5999                !!!cp ('t378');
6000                !!!parse-error (type => 'in button:button', token => $token);
6001                !!!back-token;
6002                $token = {type => END_TAG_TOKEN, tag_name => 'button',
6003                          line => $token->{line}, column => $token->{column}};
6004                redo B;
6005              } elsif ($node->[1] & SCOPING_EL) {
6006                !!!cp ('t379');
6007                last INSCOPE;
6008              }
6009            } # INSCOPE
6010              
6011            $reconstruct_active_formatting_elements->($insert_to_current);
6012              
6013            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6014    
6015            ## TODO: associate with $self->{form_element} if defined
6016    
6017            push @$active_formatting_elements, ['#marker', ''];
6018    
6019            !!!next-token;
6020            redo B;
6021          } elsif ({
6022                    xmp => 1,
6023                    iframe => 1,
6024                    noembed => 1,
6025                    noframes => 1,
6026                    noscript => 0, ## TODO: 1 if scripting is enabled
6027                   }->{$token->{tag_name}}) {
6028            if ($token->{tag_name} eq 'xmp') {
6029              !!!cp ('t381');
6030              $reconstruct_active_formatting_elements->($insert_to_current);
6031            } else {
6032              !!!cp ('t399');
6033            }
6034            ## NOTE: There is an "as if in body" code clone.
6035            $parse_rcdata->(CDATA_CONTENT_MODEL);
6036            redo B;
6037          } elsif ($token->{tag_name} eq 'isindex') {
6038            !!!parse-error (type => 'isindex', token => $token);
6039            
6040            if (defined $self->{form_element}) {
6041              !!!cp ('t389');
6042            ## Ignore the token            ## Ignore the token
6043            !!!next-token;            !!!next-token;
6044            redo B;            redo B;
6045            } else {
6046              my $at = $token->{attributes};
6047              my $form_attrs;
6048              $form_attrs->{action} = $at->{action} if $at->{action};
6049              my $prompt_attr = $at->{prompt};
6050              $at->{name} = {name => 'name', value => 'isindex'};
6051              delete $at->{action};
6052              delete $at->{prompt};
6053              my @tokens = (
6054                            {type => START_TAG_TOKEN, tag_name => 'form',
6055                             attributes => $form_attrs,
6056                             line => $token->{line}, column => $token->{column}},
6057                            {type => START_TAG_TOKEN, tag_name => 'hr',
6058                             line => $token->{line}, column => $token->{column}},
6059                            {type => START_TAG_TOKEN, tag_name => 'p',
6060                             line => $token->{line}, column => $token->{column}},
6061                            {type => START_TAG_TOKEN, tag_name => 'label',
6062                             line => $token->{line}, column => $token->{column}},
6063                           );
6064              if ($prompt_attr) {
6065                !!!cp ('t390');
6066                push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value},
6067                               #line => $token->{line}, column => $token->{column},
6068                              };
6069              } else {
6070                !!!cp ('t391');
6071                push @tokens, {type => CHARACTER_TOKEN,
6072                               data => 'This is a searchable index. Insert your search keywords here: ',
6073                               #line => $token->{line}, column => $token->{column},
6074                              }; # SHOULD
6075                ## TODO: make this configurable
6076              }
6077              push @tokens,
6078                            {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at,
6079                             line => $token->{line}, column => $token->{column}},
6080                            #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
6081                            {type => END_TAG_TOKEN, tag_name => 'label',
6082                             line => $token->{line}, column => $token->{column}},
6083                            {type => END_TAG_TOKEN, tag_name => 'p',
6084                             line => $token->{line}, column => $token->{column}},
6085                            {type => START_TAG_TOKEN, tag_name => 'hr',
6086                             line => $token->{line}, column => $token->{column}},
6087                            {type => END_TAG_TOKEN, tag_name => 'form',
6088                             line => $token->{line}, column => $token->{column}};
6089              $token = shift @tokens;
6090              !!!back-token (@tokens);
6091              redo B;
6092          }          }
6093          } elsif ($token->{tag_name} eq 'textarea') {
6094            my $tag_name = $token->{tag_name};
6095            my $el;
6096            !!!create-element ($el, $token->{tag_name}, $token->{attributes}, $token);
6097            
6098            ## TODO: $self->{form_element} if defined
6099            $self->{content_model} = RCDATA_CONTENT_MODEL;
6100            delete $self->{escape}; # MUST
6101            
6102            $insert->($el);
6103            
6104            my $text = '';
6105            !!!next-token;
6106            if ($token->{type} == CHARACTER_TOKEN) {
6107              $token->{data} =~ s/^\x0A//;
6108              unless (length $token->{data}) {
6109                !!!cp ('t392');
6110                !!!next-token;
6111              } else {
6112                !!!cp ('t393');
6113              }
6114            } else {
6115              !!!cp ('t394');
6116            }
6117            while ($token->{type} == CHARACTER_TOKEN) {
6118              !!!cp ('t395');
6119              $text .= $token->{data};
6120              !!!next-token;
6121            }
6122            if (length $text) {
6123              !!!cp ('t396');
6124              $el->manakai_append_text ($text);
6125            }
6126            
6127            $self->{content_model} = PCDATA_CONTENT_MODEL;
6128            
6129            if ($token->{type} == END_TAG_TOKEN and
6130                $token->{tag_name} eq $tag_name) {
6131              !!!cp ('t397');
6132              ## Ignore the token
6133            } else {
6134              !!!cp ('t398');
6135              !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);
6136            }
6137            !!!next-token;
6138            redo B;
6139          } elsif ({
6140                    caption => 1, col => 1, colgroup => 1, frame => 1,
6141                    frameset => 1, head => 1, option => 1, optgroup => 1,
6142                    tbody => 1, td => 1, tfoot => 1, th => 1,
6143                    thead => 1, tr => 1,
6144                   }->{$token->{tag_name}}) {
6145            !!!cp ('t401');
6146            !!!parse-error (type => 'in body:'.$token->{tag_name}, token => $token);
6147            ## Ignore the token
6148            !!!next-token;
6149            redo B;
6150            
6151            ## ISSUE: An issue on HTML5 new elements in the spec.
6152        } else {        } else {
6153          die "$0: $token->{type}: Unknown token type";          if ($token->{tag_name} eq 'image') {
6154              !!!cp ('t384');
6155              !!!parse-error (type => 'image', token => $token);
6156              $token->{tag_name} = 'img';
6157            } else {
6158              !!!cp ('t385');
6159            }
6160    
6161            ## NOTE: There is an "as if <br>" code clone.
6162            $reconstruct_active_formatting_elements->($insert_to_current);
6163            
6164            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6165    
6166            if ({
6167                 applet => 1, marquee => 1, object => 1,
6168                }->{$token->{tag_name}}) {
6169              !!!cp ('t380');
6170              push @$active_formatting_elements, ['#marker', ''];
6171            } elsif ({
6172                      b => 1, big => 1, em => 1, font => 1, i => 1,
6173                      s => 1, small => 1, strile => 1,
6174                      strong => 1, tt => 1, u => 1,
6175                     }->{$token->{tag_name}}) {
6176              !!!cp ('t375');
6177              push @$active_formatting_elements, $self->{open_elements}->[-1];
6178            } elsif ($token->{tag_name} eq 'input') {
6179              !!!cp ('t388');
6180              ## TODO: associate with $self->{form_element} if defined
6181              pop @{$self->{open_elements}};
6182            } elsif ({
6183                      area => 1, basefont => 1, bgsound => 1, br => 1,
6184                      embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
6185                      #image => 1,
6186                     }->{$token->{tag_name}}) {
6187              !!!cp ('t388.1');
6188              pop @{$self->{open_elements}};
6189            } elsif ($token->{tag_name} eq 'select') {
6190              ## TODO: associate with $self->{form_element} if defined
6191            
6192              if ($self->{insertion_mode} & TABLE_IMS or
6193                  $self->{insertion_mode} & BODY_TABLE_IMS or
6194                  $self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
6195                !!!cp ('t400.1');
6196                $self->{insertion_mode} = IN_SELECT_IN_TABLE_IM;
6197              } else {
6198                !!!cp ('t400.2');
6199                $self->{insertion_mode} = IN_SELECT_IM;
6200              }
6201            } else {
6202              !!!cp ('t402');
6203            }
6204            
6205            !!!next-token;
6206            redo B;
6207        }        }
6208        } elsif ($token->{type} == END_TAG_TOKEN) {
6209          if ($token->{tag_name} eq 'body') {
6210            ## has a |body| element in scope
6211            my $i;
6212            INSCOPE: {
6213              for (reverse @{$self->{open_elements}}) {
6214                if ($_->[1] & BODY_EL) {
6215                  !!!cp ('t405');
6216                  $i = $_;
6217                  last INSCOPE;
6218                } elsif ($_->[1] & SCOPING_EL) {
6219                  !!!cp ('t405.1');
6220                  last;
6221                }
6222              }
6223    
6224        ## ISSUE: An issue in spec here            !!!parse-error (type => 'start tag not allowed',
6225      } elsif ($self->{insertion_mode} eq 'trailing end') {                            value => $token->{tag_name}, token => $token);
6226        ## states in the main stage is preserved yet # MUST            ## NOTE: Ignore the token.
6227                    !!!next-token;
6228        if ($token->{type} eq 'character') {            redo B;
6229          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          } # INSCOPE
6230            my $data = $1;  
6231            ## As if in the main phase.          for (@{$self->{open_elements}}) {
6232            ## NOTE: The insertion mode in the main phase            unless ($_->[1] & ALL_END_TAG_OPTIONAL_EL) {
6233            ## just before the phase has been changed to the trailing              !!!cp ('t403');
6234            ## end phase is either "after body" or "after frameset".              !!!parse-error (type => 'not closed',
6235            $reconstruct_active_formatting_elements->($insert_to_current);                              value => $_->[0]->manakai_local_name,
6236                                token => $token);
6237                last;
6238              } else {
6239                !!!cp ('t404');
6240              }
6241            }
6242    
6243            $self->{insertion_mode} = AFTER_BODY_IM;
6244            !!!next-token;
6245            redo B;
6246          } elsif ($token->{tag_name} eq 'html') {
6247            ## TODO: Update this code.  It seems that the code below is not
6248            ## up-to-date, though it has same effect as speced.
6249            if (@{$self->{open_elements}} > 1 and
6250                $self->{open_elements}->[1]->[1] & BODY_EL) {
6251              ## ISSUE: There is an issue in the spec.
6252              unless ($self->{open_elements}->[-1]->[1] & BODY_EL) {
6253                !!!cp ('t406');
6254                !!!parse-error (type => 'not closed',
6255                                value => $self->{open_elements}->[1]->[0]
6256                                    ->manakai_local_name,
6257                                token => $token);
6258              } else {
6259                !!!cp ('t407');
6260              }
6261              $self->{insertion_mode} = AFTER_BODY_IM;
6262              ## reprocess
6263              redo B;
6264            } else {
6265              !!!cp ('t408');
6266              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6267              ## Ignore the token
6268              !!!next-token;
6269              redo B;
6270            }
6271          } elsif ({
6272                    address => 1, blockquote => 1, center => 1, dir => 1,
6273                    div => 1, dl => 1, fieldset => 1, listing => 1,
6274                    menu => 1, ol => 1, pre => 1, ul => 1,
6275                    dd => 1, dt => 1, li => 1,
6276                    applet => 1, button => 1, marquee => 1, object => 1,
6277                   }->{$token->{tag_name}}) {
6278            ## has an element in scope
6279            my $i;
6280            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6281              my $node = $self->{open_elements}->[$_];
6282              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6283                !!!cp ('t410');
6284                $i = $_;
6285                last INSCOPE;
6286              } elsif ($node->[1] & SCOPING_EL) {
6287                !!!cp ('t411');
6288                last INSCOPE;
6289              }
6290            } # INSCOPE
6291    
6292            unless (defined $i) { # has an element in scope
6293              !!!cp ('t413');
6294              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6295            } else {
6296              ## Step 1. generate implied end tags
6297              while ({
6298                      dd => ($token->{tag_name} ne 'dd'),
6299                      dt => ($token->{tag_name} ne 'dt'),
6300                      li => ($token->{tag_name} ne 'li'),
6301                      p => 1,
6302                     }->{$self->{open_elements}->[-1]->[0]->manakai_local_name}) {
6303                !!!cp ('t409');
6304                pop @{$self->{open_elements}};
6305              }
6306    
6307              ## Step 2.
6308              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6309                      ne $token->{tag_name}) {
6310                !!!cp ('t412');
6311                !!!parse-error (type => 'not closed',
6312                                value => $self->{open_elements}->[-1]->[0]
6313                                    ->manakai_local_name,
6314                                token => $token);
6315              } else {
6316                !!!cp ('t414');
6317              }
6318    
6319              ## Step 3.
6320              splice @{$self->{open_elements}}, $i;
6321    
6322              ## Step 4.
6323              $clear_up_to_marker->()
6324                  if {
6325                    applet => 1, button => 1, marquee => 1, object => 1,
6326                  }->{$token->{tag_name}};
6327            }
6328            !!!next-token;
6329            redo B;
6330          } elsif ($token->{tag_name} eq 'form') {
6331            undef $self->{form_element};
6332    
6333            ## has an element in scope
6334            my $i;
6335            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6336              my $node = $self->{open_elements}->[$_];
6337              if ($node->[1] & FORM_EL) {
6338                !!!cp ('t418');
6339                $i = $_;
6340                last INSCOPE;
6341              } elsif ($node->[1] & SCOPING_EL) {
6342                !!!cp ('t419');
6343                last INSCOPE;
6344              }
6345            } # INSCOPE
6346    
6347            unless (defined $i) { # has an element in scope
6348              !!!cp ('t421');
6349              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6350            } else {
6351              ## Step 1. generate implied end tags
6352              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
6353                !!!cp ('t417');
6354                pop @{$self->{open_elements}};
6355              }
6356                        
6357            $self->{open_elements}->[-1]->[0]->manakai_append_text ($data);            ## Step 2.
6358              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6359                      ne $token->{tag_name}) {
6360                !!!cp ('t417.1');
6361                !!!parse-error (type => 'not closed',
6362                                value => $self->{open_elements}->[-1]->[0]
6363                                    ->manakai_local_name,
6364                                token => $token);
6365              } else {
6366                !!!cp ('t420');
6367              }  
6368                        
6369            unless (length $token->{data}) {            ## Step 3.
6370              !!!next-token;            splice @{$self->{open_elements}}, $i;
6371              redo B;          }
6372    
6373            !!!next-token;
6374            redo B;
6375          } elsif ({
6376                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
6377                   }->{$token->{tag_name}}) {
6378            ## has an element in scope
6379            my $i;
6380            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6381              my $node = $self->{open_elements}->[$_];
6382              if ($node->[1] & HEADING_EL) {
6383                !!!cp ('t423');
6384                $i = $_;
6385                last INSCOPE;
6386              } elsif ($node->[1] & SCOPING_EL) {
6387                !!!cp ('t424');
6388                last INSCOPE;
6389            }            }
6390            } # INSCOPE
6391    
6392            unless (defined $i) { # has an element in scope
6393              !!!cp ('t425.1');
6394              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6395            } else {
6396              ## Step 1. generate implied end tags
6397              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
6398                !!!cp ('t422');
6399                pop @{$self->{open_elements}};
6400              }
6401              
6402              ## Step 2.
6403              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6404                      ne $token->{tag_name}) {
6405                !!!cp ('t425');
6406                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6407              } else {
6408                !!!cp ('t426');
6409              }
6410    
6411              ## Step 3.
6412              splice @{$self->{open_elements}}, $i;
6413          }          }
6414            
6415            !!!next-token;
6416            redo B;
6417          } elsif ($token->{tag_name} eq 'p') {
6418            ## has an element in scope
6419            my $i;
6420            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6421              my $node = $self->{open_elements}->[$_];
6422              if ($node->[1] & P_EL) {
6423                !!!cp ('t410.1');
6424                $i = $_;
6425                last INSCOPE;
6426              } elsif ($node->[1] & SCOPING_EL) {
6427                !!!cp ('t411.1');
6428                last INSCOPE;
6429              }
6430            } # INSCOPE
6431    
6432          !!!parse-error (type => 'after html:#character');          if (defined $i) {
6433          $self->{insertion_mode} = $previous_insertion_mode;            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6434          ## reprocess                    ne $token->{tag_name}) {
6435                !!!cp ('t412.1');
6436                !!!parse-error (type => 'not closed',
6437                                value => $self->{open_elements}->[-1]->[0]
6438                                    ->manakai_local_name,
6439                                token => $token);
6440              } else {
6441                !!!cp ('t414.1');
6442              }
6443    
6444              splice @{$self->{open_elements}}, $i;
6445            } else {
6446              !!!cp ('t413.1');
6447              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6448    
6449              !!!cp ('t415.1');
6450              ## As if <p>, then reprocess the current token
6451              my $el;
6452              !!!create-element ($el, 'p',, $token);
6453              $insert->($el);
6454              ## NOTE: Not inserted into |$self->{open_elements}|.
6455            }
6456    
6457            !!!next-token;
6458          redo B;          redo B;
6459        } elsif ($token->{type} eq 'start tag') {        } elsif ({
6460          !!!parse-error (type => 'after html:'.$token->{tag_name});                  a => 1,
6461          $self->{insertion_mode} = $previous_insertion_mode;                  b => 1, big => 1, em => 1, font => 1, i => 1,
6462          ## reprocess                  nobr => 1, s => 1, small => 1, strile => 1,
6463                    strong => 1, tt => 1, u => 1,
6464                   }->{$token->{tag_name}}) {
6465            !!!cp ('t427');
6466            $formatting_end_tag->($token);
6467          redo B;          redo B;
6468        } elsif ($token->{type} eq 'end tag') {        } elsif ($token->{tag_name} eq 'br') {
6469          !!!parse-error (type => 'after html:/'.$token->{tag_name});          !!!cp ('t428');
6470          $self->{insertion_mode} = $previous_insertion_mode;          !!!parse-error (type => 'unmatched end tag:br', token => $token);
6471          ## reprocess  
6472            ## As if <br>
6473            $reconstruct_active_formatting_elements->($insert_to_current);
6474            
6475            my $el;
6476            !!!create-element ($el, 'br',, $token);
6477            $insert->($el);
6478            
6479            ## Ignore the token.
6480            !!!next-token;
6481            redo B;
6482          } elsif ({
6483                    caption => 1, col => 1, colgroup => 1, frame => 1,
6484                    frameset => 1, head => 1, option => 1, optgroup => 1,
6485                    tbody => 1, td => 1, tfoot => 1, th => 1,
6486                    thead => 1, tr => 1,
6487                    area => 1, basefont => 1, bgsound => 1,
6488                    embed => 1, hr => 1, iframe => 1, image => 1,
6489                    img => 1, input => 1, isindex => 1, noembed => 1,
6490                    noframes => 1, param => 1, select => 1, spacer => 1,
6491                    table => 1, textarea => 1, wbr => 1,
6492                    noscript => 0, ## TODO: if scripting is enabled
6493                   }->{$token->{tag_name}}) {
6494            !!!cp ('t429');
6495            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6496            ## Ignore the token
6497            !!!next-token;
6498          redo B;          redo B;
6499            
6500            ## ISSUE: Issue on HTML5 new elements in spec
6501            
6502        } else {        } else {
6503          die "$0: $token->{type}: Unknown token";          ## Step 1
6504            my $node_i = -1;
6505            my $node = $self->{open_elements}->[$node_i];
6506    
6507            ## Step 2
6508            S2: {
6509              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6510                ## Step 1
6511                ## generate implied end tags
6512                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
6513                  !!!cp ('t430');
6514                  ## ISSUE: Can this case be reached?
6515                  pop @{$self->{open_elements}};
6516                }
6517            
6518                ## Step 2
6519                if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6520                        ne $token->{tag_name}) {
6521                  !!!cp ('t431');
6522                  ## NOTE: <x><y></x>
6523                  !!!parse-error (type => 'not closed',
6524                                  value => $self->{open_elements}->[-1]->[0]
6525                                      ->manakai_local_name,
6526                                  token => $token);
6527                } else {
6528                  !!!cp ('t432');
6529                }
6530                
6531                ## Step 3
6532                splice @{$self->{open_elements}}, $node_i;
6533    
6534                !!!next-token;
6535                last S2;
6536              } else {
6537                ## Step 3
6538                if (not ($node->[1] & FORMATTING_EL) and
6539                    #not $phrasing_category->{$node->[1]} and
6540                    ($node->[1] & SPECIAL_EL or
6541                     $node->[1] & SCOPING_EL)) {
6542                  !!!cp ('t433');
6543                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6544                  ## Ignore the token
6545                  !!!next-token;
6546                  last S2;
6547                }
6548    
6549                !!!cp ('t434');
6550              }
6551              
6552              ## Step 4
6553              $node_i--;
6554              $node = $self->{open_elements}->[$node_i];
6555              
6556              ## Step 5;
6557              redo S2;
6558            } # S2
6559            redo B;
6560        }        }
     } else {  
       die "$0: $self->{insertion_mode}: Unknown insertion mode";  
6561      }      }
6562        redo B;
6563    } # B    } # B
6564    
6565    ## Stop parsing # MUST    ## Stop parsing # MUST
# Line 5214  sub set_inner_html ($$$) { Line 6573  sub set_inner_html ($$$) {
6573    my $s = \$_[0];    my $s = \$_[0];
6574    my $onerror = $_[1];    my $onerror = $_[1];
6575    
6576      ## ISSUE: Should {confident} be true?
6577    
6578    my $nt = $node->node_type;    my $nt = $node->node_type;
6579    if ($nt == 9) {    if ($nt == 9) {
6580      # MUST      # MUST
# Line 5242  sub set_inner_html ($$$) { Line 6603  sub set_inner_html ($$$) {
6603      my $p = $class->new;      my $p = $class->new;
6604      $p->{document} = $doc;      $p->{document} = $doc;
6605    
6606      ## Step 9 # MUST      ## Step 8 # MUST
6607      my $i = 0;      my $i = 0;
6608      my $line = 1;      $p->{line_prev} = $p->{line} = 1;
6609      my $column = 0;      $p->{column_prev} = $p->{column} = 0;
6610      $p->{set_next_input_character} = sub {      $p->{set_next_char} = sub {
6611        my $self = shift;        my $self = shift;
6612    
6613        pop @{$self->{prev_input_character}};        pop @{$self->{prev_char}};
6614        unshift @{$self->{prev_input_character}}, $self->{next_input_character};        unshift @{$self->{prev_char}}, $self->{next_char};
6615    
6616          $self->{next_char} = -1 and return if $i >= length $$s;
6617          $self->{next_char} = ord substr $$s, $i++, 1;
6618    
6619          ($p->{line_prev}, $p->{column_prev}) = ($p->{line}, $p->{column});
6620          $p->{column}++;
6621    
6622        $self->{next_input_character} = -1 and return if $i >= length $$s;        if ($self->{next_char} == 0x000A) { # LF
6623        $self->{next_input_character} = ord substr $$s, $i++, 1;          $p->{line}++;
6624        $column++;          $p->{column} = 0;
6625            !!!cp ('i1');
6626        if ($self->{next_input_character} == 0x000A) { # LF        } elsif ($self->{next_char} == 0x000D) { # CR
         $line++;  
         $column = 0;  
       } elsif ($self->{next_input_character} == 0x000D) { # CR  
6627          $i++ if substr ($$s, $i, 1) eq "\x0A";          $i++ if substr ($$s, $i, 1) eq "\x0A";
6628          $self->{next_input_character} = 0x000A; # LF # MUST          $self->{next_char} = 0x000A; # LF # MUST
6629          $line++;          $p->{line}++;
6630          $column = 0;          $p->{column} = 0;
6631        } elsif ($self->{next_input_character} > 0x10FFFF) {          !!!cp ('i2');
6632          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        } elsif ($self->{next_char} > 0x10FFFF) {
6633        } elsif ($self->{next_input_character} == 0x0000) { # NULL          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
6634            !!!cp ('i3');
6635          } elsif ($self->{next_char} == 0x0000) { # NULL
6636            !!!cp ('i4');
6637          !!!parse-error (type => 'NULL');          !!!parse-error (type => 'NULL');
6638          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
6639        }        }
6640      };      };
6641      $p->{prev_input_character} = [-1, -1, -1];      $p->{prev_char} = [-1, -1, -1];
6642      $p->{next_input_character} = -1;      $p->{next_char} = -1;
6643            
6644      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
6645        my (%opt) = @_;        my (%opt) = @_;
6646        warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";        my $line = $opt{line};
6647          my $column = $opt{column};
6648          if (defined $opt{token} and defined $opt{token}->{line}) {
6649            $line = $opt{token}->{line};
6650            $column = $opt{token}->{column};
6651          }
6652          warn "Parse error ($opt{type}) at line $line column $column\n";
6653      };      };
6654      $p->{parse_error} = sub {      $p->{parse_error} = sub {
6655        $ponerror->(@_, line => $line, column => $column);        $ponerror->(line => $p->{line}, column => $p->{column}, @_);
6656      };      };
6657            
6658      $p->_initialize_tokenizer;      $p->_initialize_tokenizer;
6659      $p->_initialize_tree_constructor;      $p->_initialize_tree_constructor;
6660    
6661      ## Step 2      ## Step 2
6662      my $node_ln = $node->local_name;      my $node_ln = $node->manakai_local_name;
6663      $p->{content_model} = {      $p->{content_model} = {
6664        title => RCDATA_CONTENT_MODEL,        title => RCDATA_CONTENT_MODEL,
6665        textarea => RCDATA_CONTENT_MODEL,        textarea => RCDATA_CONTENT_MODEL,
# Line 5303  sub set_inner_html ($$$) { Line 6676  sub set_inner_html ($$$) {
6676          unless defined $p->{content_model};          unless defined $p->{content_model};
6677          ## ISSUE: What is "the name of the element"? local name?          ## ISSUE: What is "the name of the element"? local name?
6678    
6679      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $el_category->{$node_ln}];
6680          ## TODO: Foreign element OK?
6681    
6682      ## Step 4      ## Step 3
6683      my $root = $doc->create_element_ns      my $root = $doc->create_element_ns
6684        ('http://www.w3.org/1999/xhtml', [undef, 'html']);        ('http://www.w3.org/1999/xhtml', [undef, 'html']);
6685    
6686      ## Step 5 # MUST      ## Step 4 # MUST
6687      $doc->append_child ($root);      $doc->append_child ($root);
6688    
6689      ## Step 6 # MUST      ## Step 5 # MUST
6690      push @{$p->{open_elements}}, [$root, 'html'];      push @{$p->{open_elements}}, [$root, $el_category->{html}];
6691    
6692      undef $p->{head_element};      undef $p->{head_element};
6693    
6694      ## Step 7 # MUST      ## Step 6 # MUST
6695      $p->_reset_insertion_mode;      $p->_reset_insertion_mode;
6696    
6697      ## Step 8 # MUST      ## Step 7 # MUST
6698      my $anode = $node;      my $anode = $node;
6699      AN: while (defined $anode) {      AN: while (defined $anode) {
6700        if ($anode->node_type == 1) {        if ($anode->node_type == 1) {
6701          my $nsuri = $anode->namespace_uri;          my $nsuri = $anode->namespace_uri;
6702          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {
6703            if ($anode->local_name eq 'form') { ## TODO: case?            if ($anode->manakai_local_name eq 'form') {
6704                !!!cp ('i5');
6705              $p->{form_element} = $anode;              $p->{form_element} = $anode;
6706              last AN;              last AN;
6707            }            }
# Line 5335  sub set_inner_html ($$$) { Line 6710  sub set_inner_html ($$$) {
6710        $anode = $anode->parent_node;        $anode = $anode->parent_node;
6711      } # AN      } # AN
6712            
6713      ## Step 3 # MUST      ## Step 9 # MUST
     ## Step 10 # MUST  
6714      {      {
6715        my $self = $p;        my $self = $p;
6716        !!!next-token;        !!!next-token;
6717      }      }
6718      $p->_tree_construction_main;      $p->_tree_construction_main;
6719    
6720      ## Step 11 # MUST      ## Step 10 # MUST
6721      my @cn = @{$node->child_nodes};      my @cn = @{$node->child_nodes};
6722      for (@cn) {      for (@cn) {
6723        $node->remove_child ($_);        $node->remove_child ($_);
6724      }      }
6725      ## ISSUE: mutation events? read-only?      ## ISSUE: mutation events? read-only?
6726    
6727      ## Step 12 # MUST      ## Step 11 # MUST
6728      @cn = @{$root->child_nodes};      @cn = @{$root->child_nodes};
6729      for (@cn) {      for (@cn) {
6730        $this_doc->adopt_node ($_);        $this_doc->adopt_node ($_);
# Line 5359  sub set_inner_html ($$$) { Line 6733  sub set_inner_html ($$$) {
6733      ## ISSUE: mutation events?      ## ISSUE: mutation events?
6734    
6735      $p->_terminate_tree_constructor;      $p->_terminate_tree_constructor;
6736    
6737        delete $p->{parse_error}; # delete loop
6738    } else {    } else {
6739      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";
6740    }    }
# Line 5366  sub set_inner_html ($$$) { Line 6742  sub set_inner_html ($$$) {
6742    
6743  } # tree construction stage  } # tree construction stage
6744    
6745  sub get_inner_html ($$$) {  package Whatpm::HTML::RestartParser;
6746    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  
6747    
6748  1;  1;
6749  # $Date$  # $Date$

Legend:
Removed from v.1.44  
changed lines
  Added in v.1.123

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24