/[suikacvs]/markup/html/whatpm/Whatpm/HTML.pm.src
Suika

Diff of /markup/html/whatpm/Whatpm/HTML.pm.src

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.56 by wakaba, Sat Aug 11 07:19:18 2007 UTC revision 1.100 by wakaba, Sun Mar 9 04:08:41 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)
 ## "{U+FEFF}..." in UTF-16BE/UTF-16LE is three or four characters?  
 ## "{U+FEFF}..." in GB18030?  
14    
15  my $permitted_slash_tag_name = {  my $permitted_slash_tag_name = {
16    base => 1,    base => 1,
# Line 19  my $permitted_slash_tag_name = { Line 18  my $permitted_slash_tag_name = {
18    meta => 1,    meta => 1,
19    hr => 1,    hr => 1,
20    br => 1,    br => 1,
21    img=> 1,    img => 1,
22    embed => 1,    embed => 1,
23    param => 1,    param => 1,
24    area => 1,    area => 1,
# Line 84  my $formatting_category = { Line 83  my $formatting_category = {
83  };  };
84  # $phrasing_category: all other elements  # $phrasing_category: all other elements
85    
86    sub parse_byte_string ($$$$;$) {
87      my $self = ref $_[0] ? shift : shift->new;
88      my $charset = shift;
89      my $bytes_s = ref $_[0] ? $_[0] : \($_[0]);
90      my $s;
91      
92      if (defined $charset) {
93        require Encode; ## TODO: decode(utf8) don't delete BOM
94        $s = \ (Encode::decode ($charset, $$bytes_s));
95        $self->{input_encoding} = lc $charset; ## TODO: normalize name
96        $self->{confident} = 1;
97      } else {
98        ## TODO: Implement HTML5 detection algorithm
99        require Whatpm::Charset::UniversalCharDet;
100        $charset = Whatpm::Charset::UniversalCharDet->detect_byte_string
101            (substr ($$bytes_s, 0, 1024));
102        $charset ||= 'windows-1252';
103        $s = \ (Encode::decode ($charset, $$bytes_s));
104        $self->{input_encoding} = $charset;
105        $self->{confident} = 0;
106      }
107    
108      $self->{change_encoding} = sub {
109        my $self = shift;
110        my $charset = lc shift;
111        ## TODO: if $charset is supported
112        ## TODO: normalize charset name
113    
114        ## "Change the encoding" algorithm:
115    
116        ## Step 1    
117        if ($charset eq 'utf-16') { ## ISSUE: UTF-16BE -> UTF-8? UTF-16LE -> UTF-8?
118          $charset = 'utf-8';
119        }
120    
121        ## Step 2
122        if (defined $self->{input_encoding} and
123            $self->{input_encoding} eq $charset) {
124          $self->{confident} = 1;
125          return;
126        }
127    
128        !!!parse-error (type => 'charset label detected:'.$self->{input_encoding}.
129            ':'.$charset, level => 'w');
130    
131        ## Step 3
132        # if (can) {
133          ## change the encoding on the fly.
134          #$self->{confident} = 1;
135          #return;
136        # }
137    
138        ## Step 4
139        throw Whatpm::HTML::RestartParser (charset => $charset);
140      }; # $self->{change_encoding}
141    
142      my @args = @_; shift @args; # $s
143      my $return;
144      try {
145        $return = $self->parse_char_string ($s, @args);  
146      } catch Whatpm::HTML::RestartParser with {
147        my $charset = shift->{charset};
148        $s = \ (Encode::decode ($charset, $$bytes_s));    
149        $self->{input_encoding} = $charset; ## TODO: normalize
150        $self->{confident} = 1;
151        $return = $self->parse_char_string ($s, @args);
152      };
153      return $return;
154    } # parse_byte_string
155    
156    ## NOTE: HTML5 spec says that the encoding layer MUST NOT strip BOM
157    ## and the HTML layer MUST ignore it.  However, we does strip BOM in
158    ## the encoding layer and the HTML layer does not ignore any U+FEFF,
159    ## because the core part of our HTML parser expects a string of character,
160    ## not a string of bytes or code units or anything which might contain a BOM.
161    ## Therefore, any parser interface that accepts a string of bytes,
162    ## such as |parse_byte_string| in this module, must ensure that it does
163    ## strip the BOM and never strip any ZWNBSP.
164    
165    *parse_char_string = \&parse_string;
166    
167  sub parse_string ($$$;$) {  sub parse_string ($$$;$) {
168    my $self = shift->new;    my $self = ref $_[0] ? shift : shift->new;
169    my $s = \$_[0];    my $s = ref $_[0] ? $_[0] : \($_[0]);
170    $self->{document} = $_[1];    $self->{document} = $_[1];
171      @{$self->{document}->child_nodes} = ();
172    
173    ## NOTE: |set_inner_html| copies most of this method's code    ## NOTE: |set_inner_html| copies most of this method's code
174    
175      $self->{confident} = 1 unless exists $self->{confident};
176      $self->{document}->input_encoding ($self->{input_encoding})
177          if defined $self->{input_encoding};
178    
179    my $i = 0;    my $i = 0;
180    my $line = 1;    my $line = 1;
181    my $column = 0;    my $column = 0;
182    $self->{set_next_input_character} = sub {    $self->{set_next_char} = sub {
183      my $self = shift;      my $self = shift;
184    
185      pop @{$self->{prev_input_character}};      pop @{$self->{prev_char}};
186      unshift @{$self->{prev_input_character}}, $self->{next_input_character};      unshift @{$self->{prev_char}}, $self->{next_char};
187    
188      $self->{next_input_character} = -1 and return if $i >= length $$s;      $self->{next_char} = -1 and return if $i >= length $$s;
189      $self->{next_input_character} = ord substr $$s, $i++, 1;      $self->{next_char} = ord substr $$s, $i++, 1;
190      $column++;      $column++;
191            
192      if ($self->{next_input_character} == 0x000A) { # LF      if ($self->{next_char} == 0x000A) { # LF
193        $line++;        $line++;
194        $column = 0;        $column = 0;
195      } elsif ($self->{next_input_character} == 0x000D) { # CR      } elsif ($self->{next_char} == 0x000D) { # CR
196        $i++ if substr ($$s, $i, 1) eq "\x0A";        $i++ if substr ($$s, $i, 1) eq "\x0A";
197        $self->{next_input_character} = 0x000A; # LF # MUST        $self->{next_char} = 0x000A; # LF # MUST
198        $line++;        $line++;
199        $column = 0;        $column = 0;
200      } elsif ($self->{next_input_character} > 0x10FFFF) {      } elsif ($self->{next_char} > 0x10FFFF) {
201        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
202      } elsif ($self->{next_input_character} == 0x0000) { # NULL      } elsif ($self->{next_char} == 0x0000) { # NULL
203        !!!parse-error (type => 'NULL');        !!!parse-error (type => 'NULL');
204        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
205      }      }
206    };    };
207    $self->{prev_input_character} = [-1, -1, -1];    $self->{prev_char} = [-1, -1, -1];
208    $self->{next_input_character} = -1;    $self->{next_char} = -1;
209    
210    my $onerror = $_[2] || sub {    my $onerror = $_[2] || sub {
211      my (%opt) = @_;      my (%opt) = @_;
# Line 141  sub parse_string ($$$;$) { Line 226  sub parse_string ($$$;$) {
226  sub new ($) {  sub new ($) {
227    my $class = shift;    my $class = shift;
228    my $self = bless {}, $class;    my $self = bless {}, $class;
229    $self->{set_next_input_character} = sub {    $self->{set_next_char} = sub {
230      $self->{next_input_character} = -1;      $self->{next_char} = -1;
231    };    };
232    $self->{parse_error} = sub {    $self->{parse_error} = sub {
233      #      #
234    };    };
235      $self->{change_encoding} = sub {
236        # if ($_[0] is a supported encoding) {
237        #   run "change the encoding" algorithm;
238        #   throw Whatpm::HTML::RestartParser (charset => $new_encoding);
239        # }
240      };
241      $self->{application_cache_selection} = sub {
242        #
243      };
244    return $self;    return $self;
245  } # new  } # new
246    
# Line 159  sub CDATA_CONTENT_MODEL () { CM_LIMITED_ Line 253  sub CDATA_CONTENT_MODEL () { CM_LIMITED_
253  sub RCDATA_CONTENT_MODEL () { CM_ENTITY | CM_LIMITED_MARKUP }  sub RCDATA_CONTENT_MODEL () { CM_ENTITY | CM_LIMITED_MARKUP }
254  sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }  sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }
255    
256    sub DATA_STATE () { 0 }
257    sub ENTITY_DATA_STATE () { 1 }
258    sub TAG_OPEN_STATE () { 2 }
259    sub CLOSE_TAG_OPEN_STATE () { 3 }
260    sub TAG_NAME_STATE () { 4 }
261    sub BEFORE_ATTRIBUTE_NAME_STATE () { 5 }
262    sub ATTRIBUTE_NAME_STATE () { 6 }
263    sub AFTER_ATTRIBUTE_NAME_STATE () { 7 }
264    sub BEFORE_ATTRIBUTE_VALUE_STATE () { 8 }
265    sub ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE () { 9 }
266    sub ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE () { 10 }
267    sub ATTRIBUTE_VALUE_UNQUOTED_STATE () { 11 }
268    sub ENTITY_IN_ATTRIBUTE_VALUE_STATE () { 12 }
269    sub MARKUP_DECLARATION_OPEN_STATE () { 13 }
270    sub COMMENT_START_STATE () { 14 }
271    sub COMMENT_START_DASH_STATE () { 15 }
272    sub COMMENT_STATE () { 16 }
273    sub COMMENT_END_STATE () { 17 }
274    sub COMMENT_END_DASH_STATE () { 18 }
275    sub BOGUS_COMMENT_STATE () { 19 }
276    sub DOCTYPE_STATE () { 20 }
277    sub BEFORE_DOCTYPE_NAME_STATE () { 21 }
278    sub DOCTYPE_NAME_STATE () { 22 }
279    sub AFTER_DOCTYPE_NAME_STATE () { 23 }
280    sub BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE () { 24 }
281    sub DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE () { 25 }
282    sub DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE () { 26 }
283    sub AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE () { 27 }
284    sub BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 28 }
285    sub DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE () { 29 }
286    sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE () { 30 }
287    sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }
288    sub BOGUS_DOCTYPE_STATE () { 32 }
289    sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }
290    
291  sub DOCTYPE_TOKEN () { 1 }  sub DOCTYPE_TOKEN () { 1 }
292  sub COMMENT_TOKEN () { 2 }  sub COMMENT_TOKEN () { 2 }
293  sub START_TAG_TOKEN () { 3 }  sub START_TAG_TOKEN () { 3 }
# Line 175  sub ROW_IMS ()        { 0b10000000 } Line 304  sub ROW_IMS ()        { 0b10000000 }
304  sub BODY_AFTER_IMS () { 0b100000000 }  sub BODY_AFTER_IMS () { 0b100000000 }
305  sub FRAME_IMS ()      { 0b1000000000 }  sub FRAME_IMS ()      { 0b1000000000 }
306    
307    ## NOTE: "initial" and "before html" insertion modes have no constants.
308    
309    ## NOTE: "after after body" insertion mode.
310  sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }  sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }
311    
312    ## NOTE: "after after frameset" insertion mode.
313  sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }  sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }
314    
315  sub IN_HEAD_IM () { HEAD_IMS | 0b00 }  sub IN_HEAD_IM () { HEAD_IMS | 0b00 }
316  sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }  sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }
317  sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }  sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }
# Line 197  sub IN_COLUMN_GROUP_IM () { 0b10 } Line 332  sub IN_COLUMN_GROUP_IM () { 0b10 }
332    
333  sub _initialize_tokenizer ($) {  sub _initialize_tokenizer ($) {
334    my $self = shift;    my $self = shift;
335    $self->{state} = 'data'; # MUST    $self->{state} = DATA_STATE; # MUST
336    $self->{content_model} = PCDATA_CONTENT_MODEL; # be    $self->{content_model} = PCDATA_CONTENT_MODEL; # be
337    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE
338    undef $self->{current_attribute};    undef $self->{current_attribute};
339    undef $self->{last_emitted_start_tag_name};    undef $self->{last_emitted_start_tag_name};
340    undef $self->{last_attribute_value_state};    undef $self->{last_attribute_value_state};
341    $self->{char} = [];    $self->{char} = [];
342    # $self->{next_input_character}    # $self->{next_char}
343    !!!next-input-character;    !!!next-input-character;
344    $self->{token} = [];    $self->{token} = [];
345    # $self->{escape}    # $self->{escape}
# Line 217  sub _initialize_tokenizer ($) { Line 352  sub _initialize_tokenizer ($) {
352  ##   ->{tag_name} (START_TAG_TOKEN, END_TAG_TOKEN)  ##   ->{tag_name} (START_TAG_TOKEN, END_TAG_TOKEN)
353  ##   ->{public_identifier} (DOCTYPE_TOKEN)  ##   ->{public_identifier} (DOCTYPE_TOKEN)
354  ##   ->{system_identifier} (DOCTYPE_TOKEN)  ##   ->{system_identifier} (DOCTYPE_TOKEN)
355  ##   ->{correct} == 1 or 0 (DOCTYPE_TOKEN)  ##   ->{quirks} == 1 or 0 (DOCTYPE_TOKEN): "force-quirks" flag
356  ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)  ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)
357    ##        ->{name}
358    ##        ->{value}
359    ##        ->{has_reference} == 1 or 0
360  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
361    
362  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
# Line 229  sub _initialize_tokenizer ($) { Line 367  sub _initialize_tokenizer ($) {
367  ## has completed loading.  If one has, then it MUST be executed  ## has completed loading.  If one has, then it MUST be executed
368  ## and removed from the list.  ## and removed from the list.
369    
370    ## NOTE: HTML5 "Writing HTML documents" section, applied to
371    ## documents and not to user agents and conformance checkers,
372    ## contains some requirements that are not detected by the
373    ## parsing algorithm:
374    ## - Some requirements on character encoding declarations. ## TODO
375    ## - "Elements MUST NOT contain content that their content model disallows."
376    ##   ... Some are parse error, some are not (will be reported by c.c.).
377    ## - Polytheistic slash SHOULD NOT be used. (Applied only to atheists.) ## TODO
378    ## - Text (in elements, attributes, and comments) SHOULD NOT contain
379    ##   control characters other than space characters. ## TODO: (what is control character? C0, C1 and DEL?  Unicode control character?)
380    
381    ## TODO: HTML5 poses authors two SHOULD-level requirements that cannot
382    ## be detected by the HTML5 parsing algorithm:
383    ## - Text,
384    
385  sub _get_next_token ($) {  sub _get_next_token ($) {
386    my $self = shift;    my $self = shift;
387    if (@{$self->{token}}) {    if (@{$self->{token}}) {
# Line 236  sub _get_next_token ($) { Line 389  sub _get_next_token ($) {
389    }    }
390    
391    A: {    A: {
392      if ($self->{state} eq 'data') {      if ($self->{state} == DATA_STATE) {
393        if ($self->{next_input_character} == 0x0026) { # &        if ($self->{next_char} == 0x0026) { # &
394          if ($self->{content_model} & CM_ENTITY) { # PCDATA | RCDATA          if ($self->{content_model} & CM_ENTITY and # PCDATA | RCDATA
395            $self->{state} = 'entity data';              not $self->{escape}) {
396              !!!cp (1);
397              $self->{state} = ENTITY_DATA_STATE;
398            !!!next-input-character;            !!!next-input-character;
399            redo A;            redo A;
400          } else {          } else {
401              !!!cp (2);
402            #            #
403          }          }
404        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
405          if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA          if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
406            unless ($self->{escape}) {            unless ($self->{escape}) {
407              if ($self->{prev_input_character}->[0] == 0x002D and # -              if ($self->{prev_char}->[0] == 0x002D and # -
408                  $self->{prev_input_character}->[1] == 0x0021 and # !                  $self->{prev_char}->[1] == 0x0021 and # !
409                  $self->{prev_input_character}->[2] == 0x003C) { # <                  $self->{prev_char}->[2] == 0x003C) { # <
410                  !!!cp (3);
411                $self->{escape} = 1;                $self->{escape} = 1;
412                } else {
413                  !!!cp (4);
414              }              }
415              } else {
416                !!!cp (5);
417            }            }
418          }          }
419                    
420          #          #
421        } elsif ($self->{next_input_character} == 0x003C) { # <        } elsif ($self->{next_char} == 0x003C) { # <
422          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA
423              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA
424               not $self->{escape})) {               not $self->{escape})) {
425            $self->{state} = 'tag open';            !!!cp (6);
426              $self->{state} = TAG_OPEN_STATE;
427            !!!next-input-character;            !!!next-input-character;
428            redo A;            redo A;
429          } else {          } else {
430              !!!cp (7);
431            #            #
432          }          }
433        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
434          if ($self->{escape} and          if ($self->{escape} and
435              ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA              ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA
436            if ($self->{prev_input_character}->[0] == 0x002D and # -            if ($self->{prev_char}->[0] == 0x002D and # -
437                $self->{prev_input_character}->[1] == 0x002D) { # -                $self->{prev_char}->[1] == 0x002D) { # -
438                !!!cp (8);
439              delete $self->{escape};              delete $self->{escape};
440              } else {
441                !!!cp (9);
442            }            }
443            } else {
444              !!!cp (10);
445          }          }
446                    
447          #          #
448        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
449            !!!cp (11);
450          !!!emit ({type => END_OF_FILE_TOKEN});          !!!emit ({type => END_OF_FILE_TOKEN});
451          last A; ## TODO: ok?          last A; ## TODO: ok?
452          } else {
453            !!!cp (12);
454        }        }
455        # Anything else        # Anything else
456        my $token = {type => CHARACTER_TOKEN,        my $token = {type => CHARACTER_TOKEN,
457                     data => chr $self->{next_input_character}};                     data => chr $self->{next_char}};
458        ## Stay in the data state        ## Stay in the data state
459        !!!next-input-character;        !!!next-input-character;
460    
461        !!!emit ($token);        !!!emit ($token);
462    
463        redo A;        redo A;
464      } elsif ($self->{state} eq 'entity data') {      } elsif ($self->{state} == ENTITY_DATA_STATE) {
465        ## (cannot happen in CDATA state)        ## (cannot happen in CDATA state)
466                
467        my $token = $self->_tokenize_attempt_to_consume_an_entity (0);        my $token = $self->_tokenize_attempt_to_consume_an_entity (0, -1);
468    
469        $self->{state} = 'data';        $self->{state} = DATA_STATE;
470        # next-input-character is already done        # next-input-character is already done
471    
472        unless (defined $token) {        unless (defined $token) {
473            !!!cp (13);
474          !!!emit ({type => CHARACTER_TOKEN, data => '&'});          !!!emit ({type => CHARACTER_TOKEN, data => '&'});
475        } else {        } else {
476            !!!cp (14);
477          !!!emit ($token);          !!!emit ($token);
478        }        }
479    
480        redo A;        redo A;
481      } elsif ($self->{state} eq 'tag open') {      } elsif ($self->{state} == TAG_OPEN_STATE) {
482        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
483          if ($self->{next_input_character} == 0x002F) { # /          if ($self->{next_char} == 0x002F) { # /
484              !!!cp (15);
485            !!!next-input-character;            !!!next-input-character;
486            $self->{state} = 'close tag open';            $self->{state} = CLOSE_TAG_OPEN_STATE;
487            redo A;            redo A;
488          } else {          } else {
489              !!!cp (16);
490            ## reconsume            ## reconsume
491            $self->{state} = 'data';            $self->{state} = DATA_STATE;
492    
493            !!!emit ({type => CHARACTER_TOKEN, data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<'});
494    
495            redo A;            redo A;
496          }          }
497        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA
498          if ($self->{next_input_character} == 0x0021) { # !          if ($self->{next_char} == 0x0021) { # !
499            $self->{state} = 'markup declaration open';            !!!cp (17);
500              $self->{state} = MARKUP_DECLARATION_OPEN_STATE;
501            !!!next-input-character;            !!!next-input-character;
502            redo A;            redo A;
503          } elsif ($self->{next_input_character} == 0x002F) { # /          } elsif ($self->{next_char} == 0x002F) { # /
504            $self->{state} = 'close tag open';            !!!cp (18);
505              $self->{state} = CLOSE_TAG_OPEN_STATE;
506            !!!next-input-character;            !!!next-input-character;
507            redo A;            redo A;
508          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_char} and
509                   $self->{next_input_character} <= 0x005A) { # A..Z                   $self->{next_char} <= 0x005A) { # A..Z
510              !!!cp (19);
511            $self->{current_token}            $self->{current_token}
512              = {type => START_TAG_TOKEN,              = {type => START_TAG_TOKEN,
513                 tag_name => chr ($self->{next_input_character} + 0x0020)};                 tag_name => chr ($self->{next_char} + 0x0020)};
514            $self->{state} = 'tag name';            $self->{state} = TAG_NAME_STATE;
515            !!!next-input-character;            !!!next-input-character;
516            redo A;            redo A;
517          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_char} and
518                   $self->{next_input_character} <= 0x007A) { # a..z                   $self->{next_char} <= 0x007A) { # a..z
519              !!!cp (20);
520            $self->{current_token} = {type => START_TAG_TOKEN,            $self->{current_token} = {type => START_TAG_TOKEN,
521                              tag_name => chr ($self->{next_input_character})};                              tag_name => chr ($self->{next_char})};
522            $self->{state} = 'tag name';            $self->{state} = TAG_NAME_STATE;
523            !!!next-input-character;            !!!next-input-character;
524            redo A;            redo A;
525          } elsif ($self->{next_input_character} == 0x003E) { # >          } elsif ($self->{next_char} == 0x003E) { # >
526              !!!cp (21);
527            !!!parse-error (type => 'empty start tag');            !!!parse-error (type => 'empty start tag');
528            $self->{state} = 'data';            $self->{state} = DATA_STATE;
529            !!!next-input-character;            !!!next-input-character;
530    
531            !!!emit ({type => CHARACTER_TOKEN, data => '<>'});            !!!emit ({type => CHARACTER_TOKEN, data => '<>'});
532    
533            redo A;            redo A;
534          } elsif ($self->{next_input_character} == 0x003F) { # ?          } elsif ($self->{next_char} == 0x003F) { # ?
535              !!!cp (22);
536            !!!parse-error (type => 'pio');            !!!parse-error (type => 'pio');
537            $self->{state} = 'bogus comment';            $self->{state} = BOGUS_COMMENT_STATE;
538            ## $self->{next_input_character} is intentionally left as is            ## $self->{next_char} is intentionally left as is
539            redo A;            redo A;
540          } else {          } else {
541              !!!cp (23);
542            !!!parse-error (type => 'bare stago');            !!!parse-error (type => 'bare stago');
543            $self->{state} = 'data';            $self->{state} = DATA_STATE;
544            ## reconsume            ## reconsume
545    
546            !!!emit ({type => CHARACTER_TOKEN, data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<'});
# Line 368  sub _get_next_token ($) { Line 550  sub _get_next_token ($) {
550        } else {        } else {
551          die "$0: $self->{content_model} in tag open";          die "$0: $self->{content_model} in tag open";
552        }        }
553      } elsif ($self->{state} eq 'close tag open') {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
554        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
555          if (defined $self->{last_emitted_start_tag_name}) {          if (defined $self->{last_emitted_start_tag_name}) {
556            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>
557            my @next_char;            my @next_char;
558            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++) {
559              push @next_char, $self->{next_input_character};              push @next_char, $self->{next_char};
560              my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);              my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);
561              my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;              my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;
562              if ($self->{next_input_character} == $c or $self->{next_input_character} == $C) {              if ($self->{next_char} == $c or $self->{next_char} == $C) {
563                  !!!cp (24);
564                !!!next-input-character;                !!!next-input-character;
565                next TAGNAME;                next TAGNAME;
566              } else {              } else {
567                $self->{next_input_character} = shift @next_char; # reconsume                !!!cp (25);
568                  $self->{next_char} = shift @next_char; # reconsume
569                !!!back-next-input-character (@next_char);                !!!back-next-input-character (@next_char);
570                $self->{state} = 'data';                $self->{state} = DATA_STATE;
571    
572                !!!emit ({type => CHARACTER_TOKEN, data => '</'});                !!!emit ({type => CHARACTER_TOKEN, data => '</'});
573        
574                redo A;                redo A;
575              }              }
576            }            }
577            push @next_char, $self->{next_input_character};            push @next_char, $self->{next_char};
578                
579            unless ($self->{next_input_character} == 0x0009 or # HT            unless ($self->{next_char} == 0x0009 or # HT
580                    $self->{next_input_character} == 0x000A or # LF                    $self->{next_char} == 0x000A or # LF
581                    $self->{next_input_character} == 0x000B or # VT                    $self->{next_char} == 0x000B or # VT
582                    $self->{next_input_character} == 0x000C or # FF                    $self->{next_char} == 0x000C or # FF
583                    $self->{next_input_character} == 0x0020 or # SP                    $self->{next_char} == 0x0020 or # SP
584                    $self->{next_input_character} == 0x003E or # >                    $self->{next_char} == 0x003E or # >
585                    $self->{next_input_character} == 0x002F or # /                    $self->{next_char} == 0x002F or # /
586                    $self->{next_input_character} == -1) {                    $self->{next_char} == -1) {
587              $self->{next_input_character} = shift @next_char; # reconsume              !!!cp (26);
588                $self->{next_char} = shift @next_char; # reconsume
589              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
590              $self->{state} = 'data';              $self->{state} = DATA_STATE;
591              !!!emit ({type => CHARACTER_TOKEN, data => '</'});              !!!emit ({type => CHARACTER_TOKEN, data => '</'});
592              redo A;              redo A;
593            } else {            } else {
594              $self->{next_input_character} = shift @next_char;              !!!cp (27);
595                $self->{next_char} = shift @next_char;
596              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
597              # and consume...              # and consume...
598            }            }
599          } else {          } else {
600            ## No start tag token has ever been emitted            ## No start tag token has ever been emitted
601              !!!cp (28);
602            # next-input-character is already done            # next-input-character is already done
603            $self->{state} = 'data';            $self->{state} = DATA_STATE;
604            !!!emit ({type => CHARACTER_TOKEN, data => '</'});            !!!emit ({type => CHARACTER_TOKEN, data => '</'});
605            redo A;            redo A;
606          }          }
607        }        }
608                
609        if (0x0041 <= $self->{next_input_character} and        if (0x0041 <= $self->{next_char} and
610            $self->{next_input_character} <= 0x005A) { # A..Z            $self->{next_char} <= 0x005A) { # A..Z
611            !!!cp (29);
612          $self->{current_token} = {type => END_TAG_TOKEN,          $self->{current_token} = {type => END_TAG_TOKEN,
613                            tag_name => chr ($self->{next_input_character} + 0x0020)};                            tag_name => chr ($self->{next_char} + 0x0020)};
614          $self->{state} = 'tag name';          $self->{state} = TAG_NAME_STATE;
615          !!!next-input-character;          !!!next-input-character;
616          redo A;          redo A;
617        } elsif (0x0061 <= $self->{next_input_character} and        } elsif (0x0061 <= $self->{next_char} and
618                 $self->{next_input_character} <= 0x007A) { # a..z                 $self->{next_char} <= 0x007A) { # a..z
619            !!!cp (30);
620          $self->{current_token} = {type => END_TAG_TOKEN,          $self->{current_token} = {type => END_TAG_TOKEN,
621                            tag_name => chr ($self->{next_input_character})};                            tag_name => chr ($self->{next_char})};
622          $self->{state} = 'tag name';          $self->{state} = TAG_NAME_STATE;
623          !!!next-input-character;          !!!next-input-character;
624          redo A;          redo A;
625        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
626            !!!cp (31);
627          !!!parse-error (type => 'empty end tag');          !!!parse-error (type => 'empty end tag');
628          $self->{state} = 'data';          $self->{state} = DATA_STATE;
629          !!!next-input-character;          !!!next-input-character;
630          redo A;          redo A;
631        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
632            !!!cp (32);
633          !!!parse-error (type => 'bare etago');          !!!parse-error (type => 'bare etago');
634          $self->{state} = 'data';          $self->{state} = DATA_STATE;
635          # reconsume          # reconsume
636    
637          !!!emit ({type => CHARACTER_TOKEN, data => '</'});          !!!emit ({type => CHARACTER_TOKEN, data => '</'});
638    
639          redo A;          redo A;
640        } else {        } else {
641            !!!cp (33);
642          !!!parse-error (type => 'bogus end tag');          !!!parse-error (type => 'bogus end tag');
643          $self->{state} = 'bogus comment';          $self->{state} = BOGUS_COMMENT_STATE;
644          ## $self->{next_input_character} is intentionally left as is          ## $self->{next_char} is intentionally left as is
645          redo A;          redo A;
646        }        }
647      } elsif ($self->{state} eq 'tag name') {      } elsif ($self->{state} == TAG_NAME_STATE) {
648        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
649            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
650            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
651            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
652            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
653          $self->{state} = 'before attribute name';          !!!cp (34);
654            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
655          !!!next-input-character;          !!!next-input-character;
656          redo A;          redo A;
657        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
658          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
659              !!!cp (35);
660            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
661                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
662            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
663          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
664            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
665            if ($self->{current_token}->{attributes}) {            #if ($self->{current_token}->{attributes}) {
666              !!!parse-error (type => 'end tag attribute');            #  ## NOTE: This should never be reached.
667            }            #  !!! cp (36);
668              #  !!! parse-error (type => 'end tag attribute');
669              #} else {
670                !!!cp (37);
671              #}
672          } else {          } else {
673            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
674          }          }
675          $self->{state} = 'data';          $self->{state} = DATA_STATE;
676          !!!next-input-character;          !!!next-input-character;
677    
678          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
679    
680          redo A;          redo A;
681        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
682                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
683          $self->{current_token}->{tag_name} .= chr ($self->{next_input_character} + 0x0020);          !!!cp (38);
684            $self->{current_token}->{tag_name} .= chr ($self->{next_char} + 0x0020);
685            # start tag or end tag            # start tag or end tag
686          ## Stay in this state          ## Stay in this state
687          !!!next-input-character;          !!!next-input-character;
688          redo A;          redo A;
689        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
690          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
691          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
692              !!!cp (39);
693            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
694                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
695            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
696          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
697            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
698            if ($self->{current_token}->{attributes}) {            #if ($self->{current_token}->{attributes}) {
699              !!!parse-error (type => 'end tag attribute');            #  ## NOTE: This state should never be reached.
700            }            #  !!! cp (40);
701              #  !!! parse-error (type => 'end tag attribute');
702              #} else {
703                !!!cp (41);
704              #}
705          } else {          } else {
706            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
707          }          }
708          $self->{state} = 'data';          $self->{state} = DATA_STATE;
709          # reconsume          # reconsume
710    
711          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
712    
713          redo A;          redo A;
714        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
715          !!!next-input-character;          !!!next-input-character;
716          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_char} == 0x003E and # >
717              $self->{current_token}->{type} == START_TAG_TOKEN and              $self->{current_token}->{type} == START_TAG_TOKEN and
718              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
719            # permitted slash            # permitted slash
720              !!!cp (42);
721            #            #
722          } else {          } else {
723              !!!cp (43);
724            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
725          }          }
726          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
727          # next-input-character is already done          # next-input-character is already done
728          redo A;          redo A;
729        } else {        } else {
730          $self->{current_token}->{tag_name} .= chr $self->{next_input_character};          !!!cp (44);
731            $self->{current_token}->{tag_name} .= chr $self->{next_char};
732            # start tag or end tag            # start tag or end tag
733          ## Stay in the state          ## Stay in the state
734          !!!next-input-character;          !!!next-input-character;
735          redo A;          redo A;
736        }        }
737      } elsif ($self->{state} eq 'before attribute name') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_NAME_STATE) {
738        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
739            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
740            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
741            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
742            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
743            !!!cp (45);
744          ## Stay in the state          ## Stay in the state
745          !!!next-input-character;          !!!next-input-character;
746          redo A;          redo A;
747        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
748          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
749              !!!cp (46);
750            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
751                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
752            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
753          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
754            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
755            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
756                !!!cp (47);
757              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
758              } else {
759                !!!cp (48);
760            }            }
761          } else {          } else {
762            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
763          }          }
764          $self->{state} = 'data';          $self->{state} = DATA_STATE;
765          !!!next-input-character;          !!!next-input-character;
766    
767          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
768    
769          redo A;          redo A;
770        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
771                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
772          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          !!!cp (49);
773            $self->{current_attribute} = {name => chr ($self->{next_char} + 0x0020),
774                                value => ''};                                value => ''};
775          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
776          !!!next-input-character;          !!!next-input-character;
777          redo A;          redo A;
778        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
779          !!!next-input-character;          !!!next-input-character;
780          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_char} == 0x003E and # >
781              $self->{current_token}->{type} == START_TAG_TOKEN and              $self->{current_token}->{type} == START_TAG_TOKEN and
782              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
783            # permitted slash            # permitted slash
784              !!!cp (50);
785            #            #
786          } else {          } else {
787              !!!cp (51);
788            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
789          }          }
790          ## Stay in the state          ## Stay in the state
791          # next-input-character is already done          # next-input-character is already done
792          redo A;          redo A;
793        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
794          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
795          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
796              !!!cp (52);
797            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
798                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
799            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
800          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
801            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
802            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
803                !!!cp (53);
804              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
805              } else {
806                !!!cp (54);
807            }            }
808          } else {          } else {
809            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
810          }          }
811          $self->{state} = 'data';          $self->{state} = DATA_STATE;
812          # reconsume          # reconsume
813    
814          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
815    
816          redo A;          redo A;
817        } else {        } else {
818          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          if ({
819                 0x0022 => 1, # "
820                 0x0027 => 1, # '
821                 0x003D => 1, # =
822                }->{$self->{next_char}}) {
823              !!!cp (55);
824              !!!parse-error (type => 'bad attribute name');
825            } else {
826              !!!cp (56);
827            }
828            $self->{current_attribute} = {name => chr ($self->{next_char}),
829                                value => ''};                                value => ''};
830          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
831          !!!next-input-character;          !!!next-input-character;
832          redo A;          redo A;
833        }        }
834      } elsif ($self->{state} eq 'attribute name') {      } elsif ($self->{state} == ATTRIBUTE_NAME_STATE) {
835        my $before_leave = sub {        my $before_leave = sub {
836          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
837              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
838              !!!cp (57);
839            !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name});            !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name});
840            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
841          } else {          } else {
842              !!!cp (58);
843            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}
844              = $self->{current_attribute};              = $self->{current_attribute};
845          }          }
846        }; # $before_leave        }; # $before_leave
847    
848        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
849            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
850            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
851            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
852            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
853            !!!cp (59);
854          $before_leave->();          $before_leave->();
855          $self->{state} = 'after attribute name';          $self->{state} = AFTER_ATTRIBUTE_NAME_STATE;
856          !!!next-input-character;          !!!next-input-character;
857          redo A;          redo A;
858        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_char} == 0x003D) { # =
859            !!!cp (60);
860          $before_leave->();          $before_leave->();
861          $self->{state} = 'before attribute value';          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
862          !!!next-input-character;          !!!next-input-character;
863          redo A;          redo A;
864        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
865          $before_leave->();          $before_leave->();
866          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
867              !!!cp (61);
868            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
869                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
870            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
871          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
872              !!!cp (62);
873            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
874            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
875              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 642  sub _get_next_token ($) { Line 877  sub _get_next_token ($) {
877          } else {          } else {
878            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
879          }          }
880          $self->{state} = 'data';          $self->{state} = DATA_STATE;
881          !!!next-input-character;          !!!next-input-character;
882    
883          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
884    
885          redo A;          redo A;
886        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
887                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
888          $self->{current_attribute}->{name} .= chr ($self->{next_input_character} + 0x0020);          !!!cp (63);
889            $self->{current_attribute}->{name} .= chr ($self->{next_char} + 0x0020);
890          ## Stay in the state          ## Stay in the state
891          !!!next-input-character;          !!!next-input-character;
892          redo A;          redo A;
893        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
894          $before_leave->();          $before_leave->();
895          !!!next-input-character;          !!!next-input-character;
896          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_char} == 0x003E and # >
897              $self->{current_token}->{type} == START_TAG_TOKEN and              $self->{current_token}->{type} == START_TAG_TOKEN and
898              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
899            # permitted slash            # permitted slash
900              !!!cp (64);
901            #            #
902          } else {          } else {
903              !!!cp (65);
904            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
905          }          }
906          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
907          # next-input-character is already done          # next-input-character is already done
908          redo A;          redo A;
909        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
910          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
911          $before_leave->();          $before_leave->();
912          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
913              !!!cp (66);
914            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
915                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
916            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
917          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
918            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
919            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
920                !!!cp (67);
921              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
922              } else {
923                ## NOTE: This state should never be reached.
924                !!!cp (68);
925            }            }
926          } else {          } else {
927            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
928          }          }
929          $self->{state} = 'data';          $self->{state} = DATA_STATE;
930          # reconsume          # reconsume
931    
932          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
933    
934          redo A;          redo A;
935        } else {        } else {
936          $self->{current_attribute}->{name} .= chr ($self->{next_input_character});          if ($self->{next_char} == 0x0022 or # "
937                $self->{next_char} == 0x0027) { # '
938              !!!cp (69);
939              !!!parse-error (type => 'bad attribute name');
940            } else {
941              !!!cp (70);
942            }
943            $self->{current_attribute}->{name} .= chr ($self->{next_char});
944          ## Stay in the state          ## Stay in the state
945          !!!next-input-character;          !!!next-input-character;
946          redo A;          redo A;
947        }        }
948      } elsif ($self->{state} eq 'after attribute name') {      } elsif ($self->{state} == AFTER_ATTRIBUTE_NAME_STATE) {
949        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
950            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
951            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
952            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
953            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
954            !!!cp (71);
955          ## Stay in the state          ## Stay in the state
956          !!!next-input-character;          !!!next-input-character;
957          redo A;          redo A;
958        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_char} == 0x003D) { # =
959          $self->{state} = 'before attribute value';          !!!cp (72);
960            $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
961          !!!next-input-character;          !!!next-input-character;
962          redo A;          redo A;
963        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
964          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
965              !!!cp (73);
966            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
967                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
968            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
969          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
970            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
971            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
972                !!!cp (74);
973              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
974              } else {
975                ## NOTE: This state should never be reached.
976                !!!cp (75);
977            }            }
978          } else {          } else {
979            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
980          }          }
981          $self->{state} = 'data';          $self->{state} = DATA_STATE;
982          !!!next-input-character;          !!!next-input-character;
983    
984          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
985    
986          redo A;          redo A;
987        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
988                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
989          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          !!!cp (76);
990            $self->{current_attribute} = {name => chr ($self->{next_char} + 0x0020),
991                                value => ''};                                value => ''};
992          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
993          !!!next-input-character;          !!!next-input-character;
994          redo A;          redo A;
995        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
996          !!!next-input-character;          !!!next-input-character;
997          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_char} == 0x003E and # >
998              $self->{current_token}->{type} == START_TAG_TOKEN and              $self->{current_token}->{type} == START_TAG_TOKEN and
999              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
1000            # permitted slash            # permitted slash
1001              !!!cp (77);
1002            #            #
1003          } else {          } else {
1004              !!!cp (78);
1005            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
1006            ## TODO: Different error type for <aa / bb> than <aa/>            ## TODO: Different error type for <aa / bb> than <aa/>
1007          }          }
1008          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1009          # next-input-character is already done          # next-input-character is already done
1010          redo A;          redo A;
1011        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1012          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1013          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1014              !!!cp (79);
1015            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
1016                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
1017            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1018          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1019            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1020            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1021                !!!cp (80);
1022              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1023              } else {
1024                ## NOTE: This state should never be reached.
1025                !!!cp (81);
1026            }            }
1027          } else {          } else {
1028            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1029          }          }
1030          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1031          # reconsume          # reconsume
1032    
1033          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1034    
1035          redo A;          redo A;
1036        } else {        } else {
1037          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          !!!cp (82);
1038            $self->{current_attribute} = {name => chr ($self->{next_char}),
1039                                value => ''};                                value => ''};
1040          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
1041          !!!next-input-character;          !!!next-input-character;
1042          redo A;                  redo A;        
1043        }        }
1044      } elsif ($self->{state} eq 'before attribute value') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_VALUE_STATE) {
1045        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1046            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1047            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1048            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1049            $self->{next_input_character} == 0x0020) { # SP                  $self->{next_char} == 0x0020) { # SP      
1050            !!!cp (83);
1051          ## Stay in the state          ## Stay in the state
1052          !!!next-input-character;          !!!next-input-character;
1053          redo A;          redo A;
1054        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
1055          $self->{state} = 'attribute value (double-quoted)';          !!!cp (84);
1056            $self->{state} = ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE;
1057          !!!next-input-character;          !!!next-input-character;
1058          redo A;          redo A;
1059        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1060          $self->{state} = 'attribute value (unquoted)';          !!!cp (85);
1061            $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1062          ## reconsume          ## reconsume
1063          redo A;          redo A;
1064        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
1065          $self->{state} = 'attribute value (single-quoted)';          !!!cp (86);
1066            $self->{state} = ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE;
1067          !!!next-input-character;          !!!next-input-character;
1068          redo A;          redo A;
1069        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1070          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1071              !!!cp (87);
1072            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
1073                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
1074            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1075          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1076            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1077            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1078                !!!cp (88);
1079              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1080              } else {
1081                ## NOTE: This state should never be reached.
1082                !!!cp (89);
1083            }            }
1084          } else {          } else {
1085            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1086          }          }
1087          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1088          !!!next-input-character;          !!!next-input-character;
1089    
1090          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1091    
1092          redo A;          redo A;
1093        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1094          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1095          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1096              !!!cp (90);
1097            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
1098                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
1099            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1100          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1101            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1102            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1103                !!!cp (91);
1104              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1105              } else {
1106                ## NOTE: This state should never be reached.
1107                !!!cp (92);
1108            }            }
1109          } else {          } else {
1110            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1111          }          }
1112          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1113          ## reconsume          ## reconsume
1114    
1115          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1116    
1117          redo A;          redo A;
1118        } else {        } else {
1119          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          if ($self->{next_char} == 0x003D) { # =
1120          $self->{state} = 'attribute value (unquoted)';            !!!cp (93);
1121              !!!parse-error (type => 'bad attribute value');
1122            } else {
1123              !!!cp (94);
1124            }
1125            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1126            $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1127          !!!next-input-character;          !!!next-input-character;
1128          redo A;          redo A;
1129        }        }
1130      } elsif ($self->{state} eq 'attribute value (double-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE) {
1131        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
1132          $self->{state} = 'before attribute name';          !!!cp (95);
1133            $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1134          !!!next-input-character;          !!!next-input-character;
1135          redo A;          redo A;
1136        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1137          $self->{last_attribute_value_state} = 'attribute value (double-quoted)';          !!!cp (96);
1138          $self->{state} = 'entity in attribute value';          $self->{last_attribute_value_state} = $self->{state};
1139            $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1140          !!!next-input-character;          !!!next-input-character;
1141          redo A;          redo A;
1142        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1143          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1144          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1145              !!!cp (97);
1146            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
1147                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
1148            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1149          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1150            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1151            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1152                !!!cp (98);
1153              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1154              } else {
1155                ## NOTE: This state should never be reached.
1156                !!!cp (99);
1157            }            }
1158          } else {          } else {
1159            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1160          }          }
1161          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1162          ## reconsume          ## reconsume
1163    
1164          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1165    
1166          redo A;          redo A;
1167        } else {        } else {
1168          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          !!!cp (100);
1169            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1170          ## Stay in the state          ## Stay in the state
1171          !!!next-input-character;          !!!next-input-character;
1172          redo A;          redo A;
1173        }        }
1174      } elsif ($self->{state} eq 'attribute value (single-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE) {
1175        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
1176          $self->{state} = 'before attribute name';          !!!cp (101);
1177            $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1178          !!!next-input-character;          !!!next-input-character;
1179          redo A;          redo A;
1180        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1181          $self->{last_attribute_value_state} = 'attribute value (single-quoted)';          !!!cp (102);
1182          $self->{state} = 'entity in attribute value';          $self->{last_attribute_value_state} = $self->{state};
1183            $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1184          !!!next-input-character;          !!!next-input-character;
1185          redo A;          redo A;
1186        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1187          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1188          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1189              !!!cp (103);
1190            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
1191                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
1192            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1193          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1194            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1195            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1196                !!!cp (104);
1197              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1198              } else {
1199                ## NOTE: This state should never be reached.
1200                !!!cp (105);
1201            }            }
1202          } else {          } else {
1203            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1204          }          }
1205          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1206          ## reconsume          ## reconsume
1207    
1208          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1209    
1210          redo A;          redo A;
1211        } else {        } else {
1212          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          !!!cp (106);
1213            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1214          ## Stay in the state          ## Stay in the state
1215          !!!next-input-character;          !!!next-input-character;
1216          redo A;          redo A;
1217        }        }
1218      } elsif ($self->{state} eq 'attribute value (unquoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_UNQUOTED_STATE) {
1219        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1220            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1221            $self->{next_input_character} == 0x000B or # HT            $self->{next_char} == 0x000B or # HT
1222            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1223            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1224          $self->{state} = 'before attribute name';          !!!cp (107);
1225          !!!next-input-character;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1226          redo A;          !!!next-input-character;
1227        } elsif ($self->{next_input_character} == 0x0026) { # &          redo A;
1228          $self->{last_attribute_value_state} = 'attribute value (unquoted)';        } elsif ($self->{next_char} == 0x0026) { # &
1229          $self->{state} = 'entity in attribute value';          !!!cp (108);
1230            $self->{last_attribute_value_state} = $self->{state};
1231            $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1232          !!!next-input-character;          !!!next-input-character;
1233          redo A;          redo A;
1234        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1235          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1236              !!!cp (109);
1237            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
1238                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
1239            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1240          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1241            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1242            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1243                !!!cp (110);
1244              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1245              } else {
1246                ## NOTE: This state should never be reached.
1247                !!!cp (111);
1248            }            }
1249          } else {          } else {
1250            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1251          }          }
1252          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1253          !!!next-input-character;          !!!next-input-character;
1254    
1255          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1256    
1257          redo A;          redo A;
1258        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1259          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1260          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1261              !!!cp (112);
1262            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
1263                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
1264            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1265          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1266            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1267            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1268                !!!cp (113);
1269              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1270              } else {
1271                ## NOTE: This state should never be reached.
1272                !!!cp (114);
1273            }            }
1274          } else {          } else {
1275            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1276          }          }
1277          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1278          ## reconsume          ## reconsume
1279    
1280          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1281    
1282          redo A;          redo A;
1283        } else {        } else {
1284          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          if ({
1285                 0x0022 => 1, # "
1286                 0x0027 => 1, # '
1287                 0x003D => 1, # =
1288                }->{$self->{next_char}}) {
1289              !!!cp (115);
1290              !!!parse-error (type => 'bad attribute value');
1291            } else {
1292              !!!cp (116);
1293            }
1294            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1295          ## Stay in the state          ## Stay in the state
1296          !!!next-input-character;          !!!next-input-character;
1297          redo A;          redo A;
1298        }        }
1299      } elsif ($self->{state} eq 'entity in attribute value') {      } elsif ($self->{state} == ENTITY_IN_ATTRIBUTE_VALUE_STATE) {
1300        my $token = $self->_tokenize_attempt_to_consume_an_entity (1);        my $token = $self->_tokenize_attempt_to_consume_an_entity
1301              (1,
1302               $self->{last_attribute_value_state}
1303                 == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE ? 0x0022 : # "
1304               $self->{last_attribute_value_state}
1305                 == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE ? 0x0027 : # '
1306               -1);
1307    
1308        unless (defined $token) {        unless (defined $token) {
1309            !!!cp (117);
1310          $self->{current_attribute}->{value} .= '&';          $self->{current_attribute}->{value} .= '&';
1311        } else {        } else {
1312            !!!cp (118);
1313          $self->{current_attribute}->{value} .= $token->{data};          $self->{current_attribute}->{value} .= $token->{data};
1314            $self->{current_attribute}->{has_reference} = $token->{has_reference};
1315          ## 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"
1316        }        }
1317    
1318        $self->{state} = $self->{last_attribute_value_state};        $self->{state} = $self->{last_attribute_value_state};
1319        # next-input-character is already done        # next-input-character is already done
1320        redo A;        redo A;
1321      } elsif ($self->{state} eq 'bogus comment') {      } elsif ($self->{state} == AFTER_ATTRIBUTE_VALUE_QUOTED_STATE) {
1322          if ($self->{next_char} == 0x0009 or # HT
1323              $self->{next_char} == 0x000A or # LF
1324              $self->{next_char} == 0x000B or # VT
1325              $self->{next_char} == 0x000C or # FF
1326              $self->{next_char} == 0x0020) { # SP
1327            !!!cp (118);
1328            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1329            !!!next-input-character;
1330            redo A;
1331          } elsif ($self->{next_char} == 0x003E) { # >
1332            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1333              !!!cp (119);
1334              $self->{current_token}->{first_start_tag}
1335                  = not defined $self->{last_emitted_start_tag_name};
1336              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1337            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1338              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1339              if ($self->{current_token}->{attributes}) {
1340                !!!cp (120);
1341                !!!parse-error (type => 'end tag attribute');
1342              } else {
1343                ## NOTE: This state should never be reached.
1344                !!!cp (121);
1345              }
1346            } else {
1347              die "$0: $self->{current_token}->{type}: Unknown token type";
1348            }
1349            $self->{state} = DATA_STATE;
1350            !!!next-input-character;
1351    
1352            !!!emit ($self->{current_token}); # start tag or end tag
1353    
1354            redo A;
1355          } elsif ($self->{next_char} == 0x002F) { # /
1356            !!!next-input-character;
1357            if ($self->{next_char} == 0x003E and # >
1358                $self->{current_token}->{type} == START_TAG_TOKEN and
1359                $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
1360              # permitted slash
1361              !!!cp (122);
1362              #
1363            } else {
1364              !!!cp (123);
1365              !!!parse-error (type => 'nestc');
1366            }
1367            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1368            # next-input-character is already done
1369            redo A;
1370          } else {
1371            !!!cp (124);
1372            !!!parse-error (type => 'no space between attributes');
1373            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1374            ## reconsume
1375            redo A;
1376          }
1377        } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
1378        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1379                
1380        my $token = {type => COMMENT_TOKEN, data => ''};        my $token = {type => COMMENT_TOKEN, data => ''};
1381    
1382        BC: {        BC: {
1383          if ($self->{next_input_character} == 0x003E) { # >          if ($self->{next_char} == 0x003E) { # >
1384            $self->{state} = 'data';            !!!cp (124);
1385              $self->{state} = DATA_STATE;
1386            !!!next-input-character;            !!!next-input-character;
1387    
1388            !!!emit ($token);            !!!emit ($token);
1389    
1390            redo A;            redo A;
1391          } elsif ($self->{next_input_character} == -1) {          } elsif ($self->{next_char} == -1) {
1392            $self->{state} = 'data';            !!!cp (125);
1393              $self->{state} = DATA_STATE;
1394            ## reconsume            ## reconsume
1395    
1396            !!!emit ($token);            !!!emit ($token);
1397    
1398            redo A;            redo A;
1399          } else {          } else {
1400            $token->{data} .= chr ($self->{next_input_character});            !!!cp (126);
1401              $token->{data} .= chr ($self->{next_char});
1402            !!!next-input-character;            !!!next-input-character;
1403            redo BC;            redo BC;
1404          }          }
1405        } # BC        } # BC
1406      } elsif ($self->{state} eq 'markup declaration open') {  
1407          die "$0: _get_next_token: unexpected case [BC]";
1408        } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
1409        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1410    
1411        my @next_char;        my @next_char;
1412        push @next_char, $self->{next_input_character};        push @next_char, $self->{next_char};
1413                
1414        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
1415          !!!next-input-character;          !!!next-input-character;
1416          push @next_char, $self->{next_input_character};          push @next_char, $self->{next_char};
1417          if ($self->{next_input_character} == 0x002D) { # -          if ($self->{next_char} == 0x002D) { # -
1418              !!!cp (127);
1419            $self->{current_token} = {type => COMMENT_TOKEN, data => ''};            $self->{current_token} = {type => COMMENT_TOKEN, data => ''};
1420            $self->{state} = 'comment start';            $self->{state} = COMMENT_START_STATE;
1421            !!!next-input-character;            !!!next-input-character;
1422            redo A;            redo A;
1423            } else {
1424              !!!cp (128);
1425          }          }
1426        } elsif ($self->{next_input_character} == 0x0044 or # D        } elsif ($self->{next_char} == 0x0044 or # D
1427                 $self->{next_input_character} == 0x0064) { # d                 $self->{next_char} == 0x0064) { # d
1428          !!!next-input-character;          !!!next-input-character;
1429          push @next_char, $self->{next_input_character};          push @next_char, $self->{next_char};
1430          if ($self->{next_input_character} == 0x004F or # O          if ($self->{next_char} == 0x004F or # O
1431              $self->{next_input_character} == 0x006F) { # o              $self->{next_char} == 0x006F) { # o
1432            !!!next-input-character;            !!!next-input-character;
1433            push @next_char, $self->{next_input_character};            push @next_char, $self->{next_char};
1434            if ($self->{next_input_character} == 0x0043 or # C            if ($self->{next_char} == 0x0043 or # C
1435                $self->{next_input_character} == 0x0063) { # c                $self->{next_char} == 0x0063) { # c
1436              !!!next-input-character;              !!!next-input-character;
1437              push @next_char, $self->{next_input_character};              push @next_char, $self->{next_char};
1438              if ($self->{next_input_character} == 0x0054 or # T              if ($self->{next_char} == 0x0054 or # T
1439                  $self->{next_input_character} == 0x0074) { # t                  $self->{next_char} == 0x0074) { # t
1440                !!!next-input-character;                !!!next-input-character;
1441                push @next_char, $self->{next_input_character};                push @next_char, $self->{next_char};
1442                if ($self->{next_input_character} == 0x0059 or # Y                if ($self->{next_char} == 0x0059 or # Y
1443                    $self->{next_input_character} == 0x0079) { # y                    $self->{next_char} == 0x0079) { # y
1444                  !!!next-input-character;                  !!!next-input-character;
1445                  push @next_char, $self->{next_input_character};                  push @next_char, $self->{next_char};
1446                  if ($self->{next_input_character} == 0x0050 or # P                  if ($self->{next_char} == 0x0050 or # P
1447                      $self->{next_input_character} == 0x0070) { # p                      $self->{next_char} == 0x0070) { # p
1448                    !!!next-input-character;                    !!!next-input-character;
1449                    push @next_char, $self->{next_input_character};                    push @next_char, $self->{next_char};
1450                    if ($self->{next_input_character} == 0x0045 or # E                    if ($self->{next_char} == 0x0045 or # E
1451                        $self->{next_input_character} == 0x0065) { # e                        $self->{next_char} == 0x0065) { # e
1452                      ## ISSUE: What a stupid code this is!                      !!!cp (129);
1453                      $self->{state} = 'DOCTYPE';                      ## TODO: What a stupid code this is!
1454                        $self->{state} = DOCTYPE_STATE;
1455                      !!!next-input-character;                      !!!next-input-character;
1456                      redo A;                      redo A;
1457                      } else {
1458                        !!!cp (130);
1459                    }                    }
1460                    } else {
1461                      !!!cp (131);
1462                  }                  }
1463                  } else {
1464                    !!!cp (132);
1465                }                }
1466                } else {
1467                  !!!cp (133);
1468              }              }
1469              } else {
1470                !!!cp (134);
1471            }            }
1472            } else {
1473              !!!cp (135);
1474          }          }
1475          } else {
1476            !!!cp (136);
1477        }        }
1478    
1479        !!!parse-error (type => 'bogus comment');        !!!parse-error (type => 'bogus comment');
1480        $self->{next_input_character} = shift @next_char;        $self->{next_char} = shift @next_char;
1481        !!!back-next-input-character (@next_char);        !!!back-next-input-character (@next_char);
1482        $self->{state} = 'bogus comment';        $self->{state} = BOGUS_COMMENT_STATE;
1483        redo A;        redo A;
1484                
1485        ## ISSUE: typos in spec: chacacters, is is a parse error        ## ISSUE: typos in spec: chacacters, is is a parse error
1486        ## 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?
1487      } elsif ($self->{state} eq 'comment start') {      } elsif ($self->{state} == COMMENT_START_STATE) {
1488        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
1489          $self->{state} = 'comment start dash';          !!!cp (137);
1490            $self->{state} = COMMENT_START_DASH_STATE;
1491          !!!next-input-character;          !!!next-input-character;
1492          redo A;          redo A;
1493        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1494            !!!cp (138);
1495          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
1496          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1497          !!!next-input-character;          !!!next-input-character;
1498    
1499          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
1500    
1501          redo A;          redo A;
1502        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1503            !!!cp (139);
1504          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1505          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1506          ## reconsume          ## reconsume
1507    
1508          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
1509    
1510          redo A;          redo A;
1511        } else {        } else {
1512            !!!cp (140);
1513          $self->{current_token}->{data} # comment          $self->{current_token}->{data} # comment
1514              .= chr ($self->{next_input_character});              .= chr ($self->{next_char});
1515          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
1516          !!!next-input-character;          !!!next-input-character;
1517          redo A;          redo A;
1518        }        }
1519      } elsif ($self->{state} eq 'comment start dash') {      } elsif ($self->{state} == COMMENT_START_DASH_STATE) {
1520        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
1521          $self->{state} = 'comment end';          !!!cp (141);
1522            $self->{state} = COMMENT_END_STATE;
1523          !!!next-input-character;          !!!next-input-character;
1524          redo A;          redo A;
1525        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1526            !!!cp (142);
1527          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
1528          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1529          !!!next-input-character;          !!!next-input-character;
1530    
1531          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
1532    
1533          redo A;          redo A;
1534        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1535            !!!cp (143);
1536          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1537          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1538          ## reconsume          ## reconsume
1539    
1540          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
1541    
1542          redo A;          redo A;
1543        } else {        } else {
1544            !!!cp (144);
1545          $self->{current_token}->{data} # comment          $self->{current_token}->{data} # comment
1546              .= '-' . chr ($self->{next_input_character});              .= '-' . chr ($self->{next_char});
1547          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
1548          !!!next-input-character;          !!!next-input-character;
1549          redo A;          redo A;
1550        }        }
1551      } elsif ($self->{state} eq 'comment') {      } elsif ($self->{state} == COMMENT_STATE) {
1552        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
1553          $self->{state} = 'comment end dash';          !!!cp (145);
1554            $self->{state} = COMMENT_END_DASH_STATE;
1555          !!!next-input-character;          !!!next-input-character;
1556          redo A;          redo A;
1557        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1558            !!!cp (146);
1559          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1560          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1561          ## reconsume          ## reconsume
1562    
1563          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
1564    
1565          redo A;          redo A;
1566        } else {        } else {
1567          $self->{current_token}->{data} .= chr ($self->{next_input_character}); # comment          !!!cp (147);
1568            $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
1569          ## Stay in the state          ## Stay in the state
1570          !!!next-input-character;          !!!next-input-character;
1571          redo A;          redo A;
1572        }        }
1573      } elsif ($self->{state} eq 'comment end dash') {      } elsif ($self->{state} == COMMENT_END_DASH_STATE) {
1574        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
1575          $self->{state} = 'comment end';          !!!cp (148);
1576            $self->{state} = COMMENT_END_STATE;
1577          !!!next-input-character;          !!!next-input-character;
1578          redo A;          redo A;
1579        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1580            !!!cp (149);
1581          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1582          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1583          ## reconsume          ## reconsume
1584    
1585          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
1586    
1587          redo A;          redo A;
1588        } else {        } else {
1589          $self->{current_token}->{data} .= '-' . chr ($self->{next_input_character}); # comment          !!!cp (150);
1590          $self->{state} = 'comment';          $self->{current_token}->{data} .= '-' . chr ($self->{next_char}); # comment
1591            $self->{state} = COMMENT_STATE;
1592          !!!next-input-character;          !!!next-input-character;
1593          redo A;          redo A;
1594        }        }
1595      } elsif ($self->{state} eq 'comment end') {      } elsif ($self->{state} == COMMENT_END_STATE) {
1596        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_char} == 0x003E) { # >
1597          $self->{state} = 'data';          !!!cp (151);
1598            $self->{state} = DATA_STATE;
1599          !!!next-input-character;          !!!next-input-character;
1600    
1601          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
1602    
1603          redo A;          redo A;
1604        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
1605            !!!cp (152);
1606          !!!parse-error (type => 'dash in comment');          !!!parse-error (type => 'dash in comment');
1607          $self->{current_token}->{data} .= '-'; # comment          $self->{current_token}->{data} .= '-'; # comment
1608          ## Stay in the state          ## Stay in the state
1609          !!!next-input-character;          !!!next-input-character;
1610          redo A;          redo A;
1611        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1612            !!!cp (153);
1613          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1614          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1615          ## reconsume          ## reconsume
1616    
1617          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
1618    
1619          redo A;          redo A;
1620        } else {        } else {
1621            !!!cp (154);
1622          !!!parse-error (type => 'dash in comment');          !!!parse-error (type => 'dash in comment');
1623          $self->{current_token}->{data} .= '--' . chr ($self->{next_input_character}); # comment          $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment
1624          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
1625          !!!next-input-character;          !!!next-input-character;
1626          redo A;          redo A;
1627        }        }
1628      } elsif ($self->{state} eq 'DOCTYPE') {      } elsif ($self->{state} == DOCTYPE_STATE) {
1629        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1630            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1631            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1632            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1633            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1634          $self->{state} = 'before DOCTYPE name';          !!!cp (155);
1635            $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
1636          !!!next-input-character;          !!!next-input-character;
1637          redo A;          redo A;
1638        } else {        } else {
1639            !!!cp (156);
1640          !!!parse-error (type => 'no space before DOCTYPE name');          !!!parse-error (type => 'no space before DOCTYPE name');
1641          $self->{state} = 'before DOCTYPE name';          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
1642          ## reconsume          ## reconsume
1643          redo A;          redo A;
1644        }        }
1645      } elsif ($self->{state} eq 'before DOCTYPE name') {      } elsif ($self->{state} == BEFORE_DOCTYPE_NAME_STATE) {
1646        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1647            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1648            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1649            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1650            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1651            !!!cp (157);
1652          ## Stay in the state          ## Stay in the state
1653          !!!next-input-character;          !!!next-input-character;
1654          redo A;          redo A;
1655        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1656            !!!cp (158);
1657          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
1658          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1659          !!!next-input-character;          !!!next-input-character;
1660    
1661          !!!emit ({type => DOCTYPE_TOKEN}); # incorrect          !!!emit ({type => DOCTYPE_TOKEN, quirks => 1});
1662    
1663          redo A;          redo A;
1664        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1665            !!!cp (159);
1666          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
1667          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1668          ## reconsume          ## reconsume
1669    
1670          !!!emit ({type => DOCTYPE_TOKEN}); # incorrect          !!!emit ({type => DOCTYPE_TOKEN, quirks => 1});
1671    
1672          redo A;          redo A;
1673        } else {        } else {
1674            !!!cp (160);
1675          $self->{current_token}          $self->{current_token}
1676              = {type => DOCTYPE_TOKEN,              = {type => DOCTYPE_TOKEN,
1677                 name => chr ($self->{next_input_character}),                 name => chr ($self->{next_char}),
1678                 correct => 1};                 #quirks => 0,
1679                  };
1680  ## ISSUE: "Set the token's name name to the" in the spec  ## ISSUE: "Set the token's name name to the" in the spec
1681          $self->{state} = 'DOCTYPE name';          $self->{state} = DOCTYPE_NAME_STATE;
1682          !!!next-input-character;          !!!next-input-character;
1683          redo A;          redo A;
1684        }        }
1685      } elsif ($self->{state} eq 'DOCTYPE name') {      } elsif ($self->{state} == DOCTYPE_NAME_STATE) {
1686  ## ISSUE: Redundant "First," in the spec.  ## ISSUE: Redundant "First," in the spec.
1687        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1688            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1689            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1690            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1691            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1692          $self->{state} = 'after DOCTYPE name';          !!!cp (161);
1693            $self->{state} = AFTER_DOCTYPE_NAME_STATE;
1694          !!!next-input-character;          !!!next-input-character;
1695          redo A;          redo A;
1696        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1697          $self->{state} = 'data';          !!!cp (162);
1698            $self->{state} = DATA_STATE;
1699          !!!next-input-character;          !!!next-input-character;
1700    
1701          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1702    
1703          redo A;          redo A;
1704        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1705            !!!cp (163);
1706          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1707          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1708          ## reconsume          ## reconsume
1709    
1710          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
1711          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1712    
1713          redo A;          redo A;
1714        } else {        } else {
1715            !!!cp (164);
1716          $self->{current_token}->{name}          $self->{current_token}->{name}
1717            .= chr ($self->{next_input_character}); # DOCTYPE            .= chr ($self->{next_char}); # DOCTYPE
1718          ## Stay in the state          ## Stay in the state
1719          !!!next-input-character;          !!!next-input-character;
1720          redo A;          redo A;
1721        }        }
1722      } elsif ($self->{state} eq 'after DOCTYPE name') {      } elsif ($self->{state} == AFTER_DOCTYPE_NAME_STATE) {
1723        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1724            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1725            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1726            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1727            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1728            !!!cp (165);
1729          ## Stay in the state          ## Stay in the state
1730          !!!next-input-character;          !!!next-input-character;
1731          redo A;          redo A;
1732        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1733          $self->{state} = 'data';          !!!cp (166);
1734            $self->{state} = DATA_STATE;
1735          !!!next-input-character;          !!!next-input-character;
1736    
1737          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1738    
1739          redo A;          redo A;
1740        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1741            !!!cp (167);
1742          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1743          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1744          ## reconsume          ## reconsume
1745    
1746          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
1747          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1748    
1749          redo A;          redo A;
1750        } elsif ($self->{next_input_character} == 0x0050 or # P        } elsif ($self->{next_char} == 0x0050 or # P
1751                 $self->{next_input_character} == 0x0070) { # p                 $self->{next_char} == 0x0070) { # p
1752          !!!next-input-character;          !!!next-input-character;
1753          if ($self->{next_input_character} == 0x0055 or # U          if ($self->{next_char} == 0x0055 or # U
1754              $self->{next_input_character} == 0x0075) { # u              $self->{next_char} == 0x0075) { # u
1755            !!!next-input-character;            !!!next-input-character;
1756            if ($self->{next_input_character} == 0x0042 or # B            if ($self->{next_char} == 0x0042 or # B
1757                $self->{next_input_character} == 0x0062) { # b                $self->{next_char} == 0x0062) { # b
1758              !!!next-input-character;              !!!next-input-character;
1759              if ($self->{next_input_character} == 0x004C or # L              if ($self->{next_char} == 0x004C or # L
1760                  $self->{next_input_character} == 0x006C) { # l                  $self->{next_char} == 0x006C) { # l
1761                !!!next-input-character;                !!!next-input-character;
1762                if ($self->{next_input_character} == 0x0049 or # I                if ($self->{next_char} == 0x0049 or # I
1763                    $self->{next_input_character} == 0x0069) { # i                    $self->{next_char} == 0x0069) { # i
1764                  !!!next-input-character;                  !!!next-input-character;
1765                  if ($self->{next_input_character} == 0x0043 or # C                  if ($self->{next_char} == 0x0043 or # C
1766                      $self->{next_input_character} == 0x0063) { # c                      $self->{next_char} == 0x0063) { # c
1767                    $self->{state} = 'before DOCTYPE public identifier';                    !!!cp (168);
1768                      $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1769                    !!!next-input-character;                    !!!next-input-character;
1770                    redo A;                    redo A;
1771                    } else {
1772                      !!!cp (169);
1773                  }                  }
1774                  } else {
1775                    !!!cp (170);
1776                }                }
1777                } else {
1778                  !!!cp (171);
1779              }              }
1780              } else {
1781                !!!cp (172);
1782            }            }
1783            } else {
1784              !!!cp (173);
1785          }          }
1786    
1787          #          #
1788        } elsif ($self->{next_input_character} == 0x0053 or # S        } elsif ($self->{next_char} == 0x0053 or # S
1789                 $self->{next_input_character} == 0x0073) { # s                 $self->{next_char} == 0x0073) { # s
1790          !!!next-input-character;          !!!next-input-character;
1791          if ($self->{next_input_character} == 0x0059 or # Y          if ($self->{next_char} == 0x0059 or # Y
1792              $self->{next_input_character} == 0x0079) { # y              $self->{next_char} == 0x0079) { # y
1793            !!!next-input-character;            !!!next-input-character;
1794            if ($self->{next_input_character} == 0x0053 or # S            if ($self->{next_char} == 0x0053 or # S
1795                $self->{next_input_character} == 0x0073) { # s                $self->{next_char} == 0x0073) { # s
1796              !!!next-input-character;              !!!next-input-character;
1797              if ($self->{next_input_character} == 0x0054 or # T              if ($self->{next_char} == 0x0054 or # T
1798                  $self->{next_input_character} == 0x0074) { # t                  $self->{next_char} == 0x0074) { # t
1799                !!!next-input-character;                !!!next-input-character;
1800                if ($self->{next_input_character} == 0x0045 or # E                if ($self->{next_char} == 0x0045 or # E
1801                    $self->{next_input_character} == 0x0065) { # e                    $self->{next_char} == 0x0065) { # e
1802                  !!!next-input-character;                  !!!next-input-character;
1803                  if ($self->{next_input_character} == 0x004D or # M                  if ($self->{next_char} == 0x004D or # M
1804                      $self->{next_input_character} == 0x006D) { # m                      $self->{next_char} == 0x006D) { # m
1805                    $self->{state} = 'before DOCTYPE system identifier';                    !!!cp (174);
1806                      $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
1807                    !!!next-input-character;                    !!!next-input-character;
1808                    redo A;                    redo A;
1809                    } else {
1810                      !!!cp (175);
1811                  }                  }
1812                  } else {
1813                    !!!cp (176);
1814                }                }
1815                } else {
1816                  !!!cp (177);
1817              }              }
1818              } else {
1819                !!!cp (178);
1820            }            }
1821            } else {
1822              !!!cp (179);
1823          }          }
1824    
1825          #          #
1826        } else {        } else {
1827            !!!cp (180);
1828          !!!next-input-character;          !!!next-input-character;
1829          #          #
1830        }        }
1831    
1832        !!!parse-error (type => 'string after DOCTYPE name');        !!!parse-error (type => 'string after DOCTYPE name');
1833        $self->{state} = 'bogus DOCTYPE';        $self->{current_token}->{quirks} = 1;
1834    
1835          $self->{state} = BOGUS_DOCTYPE_STATE;
1836        # next-input-character is already done        # next-input-character is already done
1837        redo A;        redo A;
1838      } elsif ($self->{state} eq 'before DOCTYPE public identifier') {      } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
1839        if ({        if ({
1840              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1841              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
1842            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
1843            !!!cp (181);
1844          ## Stay in the state          ## Stay in the state
1845          !!!next-input-character;          !!!next-input-character;
1846          redo A;          redo A;
1847        } elsif ($self->{next_input_character} eq 0x0022) { # "        } elsif ($self->{next_char} eq 0x0022) { # "
1848            !!!cp (182);
1849          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{current_token}->{public_identifier} = ''; # DOCTYPE
1850          $self->{state} = 'DOCTYPE public identifier (double-quoted)';          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;
1851          !!!next-input-character;          !!!next-input-character;
1852          redo A;          redo A;
1853        } elsif ($self->{next_input_character} eq 0x0027) { # '        } elsif ($self->{next_char} eq 0x0027) { # '
1854            !!!cp (183);
1855          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{current_token}->{public_identifier} = ''; # DOCTYPE
1856          $self->{state} = 'DOCTYPE public identifier (single-quoted)';          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;
1857          !!!next-input-character;          !!!next-input-character;
1858          redo A;          redo A;
1859        } elsif ($self->{next_input_character} eq 0x003E) { # >        } elsif ($self->{next_char} eq 0x003E) { # >
1860            !!!cp (184);
1861          !!!parse-error (type => 'no PUBLIC literal');          !!!parse-error (type => 'no PUBLIC literal');
1862    
1863          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1864          !!!next-input-character;          !!!next-input-character;
1865    
1866          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
1867          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1868    
1869          redo A;          redo A;
1870        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1871            !!!cp (185);
1872          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1873    
1874          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1875          ## reconsume          ## reconsume
1876    
1877          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
1878          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1879    
1880          redo A;          redo A;
1881        } else {        } else {
1882            !!!cp (186);
1883          !!!parse-error (type => 'string after PUBLIC');          !!!parse-error (type => 'string after PUBLIC');
1884          $self->{state} = 'bogus DOCTYPE';          $self->{current_token}->{quirks} = 1;
1885    
1886            $self->{state} = BOGUS_DOCTYPE_STATE;
1887          !!!next-input-character;          !!!next-input-character;
1888          redo A;          redo A;
1889        }        }
1890      } elsif ($self->{state} eq 'DOCTYPE public identifier (double-quoted)') {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {
1891        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
1892          $self->{state} = 'after DOCTYPE public identifier';          !!!cp (187);
1893            $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1894            !!!next-input-character;
1895            redo A;
1896          } elsif ($self->{next_char} == 0x003E) { # >
1897            !!!cp (188);
1898            !!!parse-error (type => 'unclosed PUBLIC literal');
1899    
1900            $self->{state} = DATA_STATE;
1901          !!!next-input-character;          !!!next-input-character;
1902    
1903            $self->{current_token}->{quirks} = 1;
1904            !!!emit ($self->{current_token}); # DOCTYPE
1905    
1906          redo A;          redo A;
1907        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1908            !!!cp (189);
1909          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
1910    
1911          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1912          ## reconsume          ## reconsume
1913    
1914          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
1915          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1916    
1917          redo A;          redo A;
1918        } else {        } else {
1919            !!!cp (190);
1920          $self->{current_token}->{public_identifier} # DOCTYPE          $self->{current_token}->{public_identifier} # DOCTYPE
1921              .= chr $self->{next_input_character};              .= chr $self->{next_char};
1922          ## Stay in the state          ## Stay in the state
1923          !!!next-input-character;          !!!next-input-character;
1924          redo A;          redo A;
1925        }        }
1926      } elsif ($self->{state} eq 'DOCTYPE public identifier (single-quoted)') {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {
1927        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
1928          $self->{state} = 'after DOCTYPE public identifier';          !!!cp (191);
1929            $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1930            !!!next-input-character;
1931            redo A;
1932          } elsif ($self->{next_char} == 0x003E) { # >
1933            !!!cp (192);
1934            !!!parse-error (type => 'unclosed PUBLIC literal');
1935    
1936            $self->{state} = DATA_STATE;
1937          !!!next-input-character;          !!!next-input-character;
1938    
1939            $self->{current_token}->{quirks} = 1;
1940            !!!emit ($self->{current_token}); # DOCTYPE
1941    
1942          redo A;          redo A;
1943        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1944            !!!cp (193);
1945          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
1946    
1947          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1948          ## reconsume          ## reconsume
1949    
1950          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
1951          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1952    
1953          redo A;          redo A;
1954        } else {        } else {
1955            !!!cp (194);
1956          $self->{current_token}->{public_identifier} # DOCTYPE          $self->{current_token}->{public_identifier} # DOCTYPE
1957              .= chr $self->{next_input_character};              .= chr $self->{next_char};
1958          ## Stay in the state          ## Stay in the state
1959          !!!next-input-character;          !!!next-input-character;
1960          redo A;          redo A;
1961        }        }
1962      } elsif ($self->{state} eq 'after DOCTYPE public identifier') {      } elsif ($self->{state} == AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
1963        if ({        if ({
1964              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1965              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
1966            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
1967            !!!cp (195);
1968          ## Stay in the state          ## Stay in the state
1969          !!!next-input-character;          !!!next-input-character;
1970          redo A;          redo A;
1971        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
1972            !!!cp (196);
1973          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1974          $self->{state} = 'DOCTYPE system identifier (double-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
1975          !!!next-input-character;          !!!next-input-character;
1976          redo A;          redo A;
1977        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
1978            !!!cp (197);
1979          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1980          $self->{state} = 'DOCTYPE system identifier (single-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
1981          !!!next-input-character;          !!!next-input-character;
1982          redo A;          redo A;
1983        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1984          $self->{state} = 'data';          !!!cp (198);
1985            $self->{state} = DATA_STATE;
1986          !!!next-input-character;          !!!next-input-character;
1987    
1988          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1989    
1990          redo A;          redo A;
1991        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1992            !!!cp (199);
1993          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1994    
1995          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1996          ## reconsume          ## reconsume
1997    
1998          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
1999          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2000    
2001          redo A;          redo A;
2002        } else {        } else {
2003            !!!cp (200);
2004          !!!parse-error (type => 'string after PUBLIC literal');          !!!parse-error (type => 'string after PUBLIC literal');
2005          $self->{state} = 'bogus DOCTYPE';          $self->{current_token}->{quirks} = 1;
2006    
2007            $self->{state} = BOGUS_DOCTYPE_STATE;
2008          !!!next-input-character;          !!!next-input-character;
2009          redo A;          redo A;
2010        }        }
2011      } elsif ($self->{state} eq 'before DOCTYPE system identifier') {      } elsif ($self->{state} == BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
2012        if ({        if ({
2013              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2014              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2015            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2016            !!!cp (201);
2017          ## Stay in the state          ## Stay in the state
2018          !!!next-input-character;          !!!next-input-character;
2019          redo A;          redo A;
2020        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
2021            !!!cp (202);
2022          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2023          $self->{state} = 'DOCTYPE system identifier (double-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2024          !!!next-input-character;          !!!next-input-character;
2025          redo A;          redo A;
2026        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
2027            !!!cp (203);
2028          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2029          $self->{state} = 'DOCTYPE system identifier (single-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2030          !!!next-input-character;          !!!next-input-character;
2031          redo A;          redo A;
2032        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2033            !!!cp (204);
2034          !!!parse-error (type => 'no SYSTEM literal');          !!!parse-error (type => 'no SYSTEM literal');
2035          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2036          !!!next-input-character;          !!!next-input-character;
2037    
2038          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2039          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2040    
2041          redo A;          redo A;
2042        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2043            !!!cp (205);
2044          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2045    
2046          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2047          ## reconsume          ## reconsume
2048    
2049          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2050          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2051    
2052          redo A;          redo A;
2053        } else {        } else {
2054            !!!cp (206);
2055          !!!parse-error (type => 'string after SYSTEM');          !!!parse-error (type => 'string after SYSTEM');
2056          $self->{state} = 'bogus DOCTYPE';          $self->{current_token}->{quirks} = 1;
2057    
2058            $self->{state} = BOGUS_DOCTYPE_STATE;
2059          !!!next-input-character;          !!!next-input-character;
2060          redo A;          redo A;
2061        }        }
2062      } elsif ($self->{state} eq 'DOCTYPE system identifier (double-quoted)') {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2063        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
2064          $self->{state} = 'after DOCTYPE system identifier';          !!!cp (207);
2065            $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2066            !!!next-input-character;
2067            redo A;
2068          } elsif ($self->{next_char} == 0x003E) { # >
2069            !!!cp (208);
2070            !!!parse-error (type => 'unclosed PUBLIC literal');
2071    
2072            $self->{state} = DATA_STATE;
2073          !!!next-input-character;          !!!next-input-character;
2074    
2075            $self->{current_token}->{quirks} = 1;
2076            !!!emit ($self->{current_token}); # DOCTYPE
2077    
2078          redo A;          redo A;
2079        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2080            !!!cp (209);
2081          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2082    
2083          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2084          ## reconsume          ## reconsume
2085    
2086          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2087          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2088    
2089          redo A;          redo A;
2090        } else {        } else {
2091            !!!cp (210);
2092          $self->{current_token}->{system_identifier} # DOCTYPE          $self->{current_token}->{system_identifier} # DOCTYPE
2093              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2094          ## Stay in the state          ## Stay in the state
2095          !!!next-input-character;          !!!next-input-character;
2096          redo A;          redo A;
2097        }        }
2098      } elsif ($self->{state} eq 'DOCTYPE system identifier (single-quoted)') {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {
2099        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
2100          $self->{state} = 'after DOCTYPE system identifier';          !!!cp (211);
2101            $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2102          !!!next-input-character;          !!!next-input-character;
2103          redo A;          redo A;
2104        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == 0x003E) { # >
2105            !!!cp (212);
2106            !!!parse-error (type => 'unclosed PUBLIC literal');
2107    
2108            $self->{state} = DATA_STATE;
2109            !!!next-input-character;
2110    
2111            $self->{current_token}->{quirks} = 1;
2112            !!!emit ($self->{current_token}); # DOCTYPE
2113    
2114            redo A;
2115          } elsif ($self->{next_char} == -1) {
2116            !!!cp (213);
2117          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2118    
2119          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2120          ## reconsume          ## reconsume
2121    
2122          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2123          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2124    
2125          redo A;          redo A;
2126        } else {        } else {
2127            !!!cp (214);
2128          $self->{current_token}->{system_identifier} # DOCTYPE          $self->{current_token}->{system_identifier} # DOCTYPE
2129              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2130          ## Stay in the state          ## Stay in the state
2131          !!!next-input-character;          !!!next-input-character;
2132          redo A;          redo A;
2133        }        }
2134      } elsif ($self->{state} eq 'after DOCTYPE system identifier') {      } elsif ($self->{state} == AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
2135        if ({        if ({
2136              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2137              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2138            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2139            !!!cp (215);
2140          ## Stay in the state          ## Stay in the state
2141          !!!next-input-character;          !!!next-input-character;
2142          redo A;          redo A;
2143        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2144          $self->{state} = 'data';          !!!cp (216);
2145            $self->{state} = DATA_STATE;
2146          !!!next-input-character;          !!!next-input-character;
2147    
2148          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2149    
2150          redo A;          redo A;
2151        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2152            !!!cp (217);
2153          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2154    
2155          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2156          ## reconsume          ## reconsume
2157    
2158          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2159          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2160    
2161          redo A;          redo A;
2162        } else {        } else {
2163            !!!cp (218);
2164          !!!parse-error (type => 'string after SYSTEM literal');          !!!parse-error (type => 'string after SYSTEM literal');
2165          $self->{state} = 'bogus DOCTYPE';          #$self->{current_token}->{quirks} = 1;
2166    
2167            $self->{state} = BOGUS_DOCTYPE_STATE;
2168          !!!next-input-character;          !!!next-input-character;
2169          redo A;          redo A;
2170        }        }
2171      } elsif ($self->{state} eq 'bogus DOCTYPE') {      } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {
2172        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_char} == 0x003E) { # >
2173          $self->{state} = 'data';          !!!cp (219);
2174            $self->{state} = DATA_STATE;
2175          !!!next-input-character;          !!!next-input-character;
2176    
         delete $self->{current_token}->{correct};  
2177          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2178    
2179          redo A;          redo A;
2180        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2181            !!!cp (220);
2182          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2183          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2184          ## reconsume          ## reconsume
2185    
         delete $self->{current_token}->{correct};  
2186          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2187    
2188          redo A;          redo A;
2189        } else {        } else {
2190            !!!cp (221);
2191          ## Stay in the state          ## Stay in the state
2192          !!!next-input-character;          !!!next-input-character;
2193          redo A;          redo A;
# Line 1644  sub _get_next_token ($) { Line 2200  sub _get_next_token ($) {
2200    die "$0: _get_next_token: unexpected case";    die "$0: _get_next_token: unexpected case";
2201  } # _get_next_token  } # _get_next_token
2202    
2203  sub _tokenize_attempt_to_consume_an_entity ($$) {  sub _tokenize_attempt_to_consume_an_entity ($$$) {
2204    my ($self, $in_attr) = @_;    my ($self, $in_attr, $additional) = @_;
2205    
2206    if ({    if ({
2207         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,
2208         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR
2209        }->{$self->{next_input_character}}) {         $additional => 1,
2210          }->{$self->{next_char}}) {
2211        !!!cp (1001);
2212      ## Don't consume      ## Don't consume
2213      ## No error      ## No error
2214      return undef;      return undef;
2215    } elsif ($self->{next_input_character} == 0x0023) { # #    } elsif ($self->{next_char} == 0x0023) { # #
2216      !!!next-input-character;      !!!next-input-character;
2217      if ($self->{next_input_character} == 0x0078 or # x      if ($self->{next_char} == 0x0078 or # x
2218          $self->{next_input_character} == 0x0058) { # X          $self->{next_char} == 0x0058) { # X
2219        my $code;        my $code;
2220        X: {        X: {
2221          my $x_char = $self->{next_input_character};          my $x_char = $self->{next_char};
2222          !!!next-input-character;          !!!next-input-character;
2223          if (0x0030 <= $self->{next_input_character} and          if (0x0030 <= $self->{next_char} and
2224              $self->{next_input_character} <= 0x0039) { # 0..9              $self->{next_char} <= 0x0039) { # 0..9
2225              !!!cp (1002);
2226            $code ||= 0;            $code ||= 0;
2227            $code *= 0x10;            $code *= 0x10;
2228            $code += $self->{next_input_character} - 0x0030;            $code += $self->{next_char} - 0x0030;
2229            redo X;            redo X;
2230          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_char} and
2231                   $self->{next_input_character} <= 0x0066) { # a..f                   $self->{next_char} <= 0x0066) { # a..f
2232              !!!cp (1003);
2233            $code ||= 0;            $code ||= 0;
2234            $code *= 0x10;            $code *= 0x10;
2235            $code += $self->{next_input_character} - 0x0060 + 9;            $code += $self->{next_char} - 0x0060 + 9;
2236            redo X;            redo X;
2237          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_char} and
2238                   $self->{next_input_character} <= 0x0046) { # A..F                   $self->{next_char} <= 0x0046) { # A..F
2239              !!!cp (1004);
2240            $code ||= 0;            $code ||= 0;
2241            $code *= 0x10;            $code *= 0x10;
2242            $code += $self->{next_input_character} - 0x0040 + 9;            $code += $self->{next_char} - 0x0040 + 9;
2243            redo X;            redo X;
2244          } elsif (not defined $code) { # no hexadecimal digit          } elsif (not defined $code) { # no hexadecimal digit
2245              !!!cp (1005);
2246            !!!parse-error (type => 'bare hcro');            !!!parse-error (type => 'bare hcro');
2247            !!!back-next-input-character ($x_char, $self->{next_input_character});            !!!back-next-input-character ($x_char, $self->{next_char});
2248            $self->{next_input_character} = 0x0023; # #            $self->{next_char} = 0x0023; # #
2249            return undef;            return undef;
2250          } elsif ($self->{next_input_character} == 0x003B) { # ;          } elsif ($self->{next_char} == 0x003B) { # ;
2251              !!!cp (1006);
2252            !!!next-input-character;            !!!next-input-character;
2253          } else {          } else {
2254              !!!cp (1007);
2255            !!!parse-error (type => 'no refc');            !!!parse-error (type => 'no refc');
2256          }          }
2257    
2258          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2259              !!!cp (1008);
2260            !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);            !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);
2261            $code = 0xFFFD;            $code = 0xFFFD;
2262          } elsif ($code > 0x10FFFF) {          } elsif ($code > 0x10FFFF) {
2263              !!!cp (1009);
2264            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);
2265            $code = 0xFFFD;            $code = 0xFFFD;
2266          } elsif ($code == 0x000D) {          } elsif ($code == 0x000D) {
2267              !!!cp (1010);
2268            !!!parse-error (type => 'CR character reference');            !!!parse-error (type => 'CR character reference');
2269            $code = 0x000A;            $code = 0x000A;
2270          } elsif (0x80 <= $code and $code <= 0x9F) {          } elsif (0x80 <= $code and $code <= 0x9F) {
2271              !!!cp (1011);
2272            !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);            !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);
2273            $code = $c1_entity_char->{$code};            $code = $c1_entity_char->{$code};
2274          }          }
2275    
2276          return {type => CHARACTER_TOKEN, data => chr $code};          return {type => CHARACTER_TOKEN, data => chr $code,
2277                    has_reference => 1};
2278        } # X        } # X
2279      } elsif (0x0030 <= $self->{next_input_character} and      } elsif (0x0030 <= $self->{next_char} and
2280               $self->{next_input_character} <= 0x0039) { # 0..9               $self->{next_char} <= 0x0039) { # 0..9
2281        my $code = $self->{next_input_character} - 0x0030;        my $code = $self->{next_char} - 0x0030;
2282        !!!next-input-character;        !!!next-input-character;
2283                
2284        while (0x0030 <= $self->{next_input_character} and        while (0x0030 <= $self->{next_char} and
2285                  $self->{next_input_character} <= 0x0039) { # 0..9                  $self->{next_char} <= 0x0039) { # 0..9
2286            !!!cp (1012);
2287          $code *= 10;          $code *= 10;
2288          $code += $self->{next_input_character} - 0x0030;          $code += $self->{next_char} - 0x0030;
2289                    
2290          !!!next-input-character;          !!!next-input-character;
2291        }        }
2292    
2293        if ($self->{next_input_character} == 0x003B) { # ;        if ($self->{next_char} == 0x003B) { # ;
2294            !!!cp (1013);
2295          !!!next-input-character;          !!!next-input-character;
2296        } else {        } else {
2297            !!!cp (1014);
2298          !!!parse-error (type => 'no refc');          !!!parse-error (type => 'no refc');
2299        }        }
2300    
2301        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2302            !!!cp (1015);
2303          !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);          !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);
2304          $code = 0xFFFD;          $code = 0xFFFD;
2305        } elsif ($code > 0x10FFFF) {        } elsif ($code > 0x10FFFF) {
2306            !!!cp (1016);
2307          !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);          !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);
2308          $code = 0xFFFD;          $code = 0xFFFD;
2309        } elsif ($code == 0x000D) {        } elsif ($code == 0x000D) {
2310            !!!cp (1017);
2311          !!!parse-error (type => 'CR character reference');          !!!parse-error (type => 'CR character reference');
2312          $code = 0x000A;          $code = 0x000A;
2313        } elsif (0x80 <= $code and $code <= 0x9F) {        } elsif (0x80 <= $code and $code <= 0x9F) {
2314            !!!cp (1018);
2315          !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);          !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);
2316          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
2317        }        }
2318                
2319        return {type => CHARACTER_TOKEN, data => chr $code};        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1};
2320      } else {      } else {
2321          !!!cp (1019);
2322        !!!parse-error (type => 'bare nero');        !!!parse-error (type => 'bare nero');
2323        !!!back-next-input-character ($self->{next_input_character});        !!!back-next-input-character ($self->{next_char});
2324        $self->{next_input_character} = 0x0023; # #        $self->{next_char} = 0x0023; # #
2325        return undef;        return undef;
2326      }      }
2327    } elsif ((0x0041 <= $self->{next_input_character} and    } elsif ((0x0041 <= $self->{next_char} and
2328              $self->{next_input_character} <= 0x005A) or              $self->{next_char} <= 0x005A) or
2329             (0x0061 <= $self->{next_input_character} and             (0x0061 <= $self->{next_char} and
2330              $self->{next_input_character} <= 0x007A)) {              $self->{next_char} <= 0x007A)) {
2331      my $entity_name = chr $self->{next_input_character};      my $entity_name = chr $self->{next_char};
2332      !!!next-input-character;      !!!next-input-character;
2333    
2334      my $value = $entity_name;      my $value = $entity_name;
# Line 1761  sub _tokenize_attempt_to_consume_an_enti Line 2338  sub _tokenize_attempt_to_consume_an_enti
2338    
2339      while (length $entity_name < 10 and      while (length $entity_name < 10 and
2340             ## NOTE: Some number greater than the maximum length of entity name             ## NOTE: Some number greater than the maximum length of entity name
2341             ((0x0041 <= $self->{next_input_character} and # a             ((0x0041 <= $self->{next_char} and # a
2342               $self->{next_input_character} <= 0x005A) or # x               $self->{next_char} <= 0x005A) or # x
2343              (0x0061 <= $self->{next_input_character} and # a              (0x0061 <= $self->{next_char} and # a
2344               $self->{next_input_character} <= 0x007A) or # z               $self->{next_char} <= 0x007A) or # z
2345              (0x0030 <= $self->{next_input_character} and # 0              (0x0030 <= $self->{next_char} and # 0
2346               $self->{next_input_character} <= 0x0039) or # 9               $self->{next_char} <= 0x0039) or # 9
2347              $self->{next_input_character} == 0x003B)) { # ;              $self->{next_char} == 0x003B)) { # ;
2348        $entity_name .= chr $self->{next_input_character};        $entity_name .= chr $self->{next_char};
2349        if (defined $EntityChar->{$entity_name}) {        if (defined $EntityChar->{$entity_name}) {
2350          if ($self->{next_input_character} == 0x003B) { # ;          if ($self->{next_char} == 0x003B) { # ;
2351              !!!cp (1020);
2352            $value = $EntityChar->{$entity_name};            $value = $EntityChar->{$entity_name};
2353            $match = 1;            $match = 1;
2354            !!!next-input-character;            !!!next-input-character;
2355            last;            last;
2356          } else {          } else {
2357              !!!cp (1021);
2358            $value = $EntityChar->{$entity_name};            $value = $EntityChar->{$entity_name};
2359            $match = -1;            $match = -1;
2360            !!!next-input-character;            !!!next-input-character;
2361          }          }
2362        } else {        } else {
2363          $value .= chr $self->{next_input_character};          !!!cp (1022);
2364            $value .= chr $self->{next_char};
2365          $match *= 2;          $match *= 2;
2366          !!!next-input-character;          !!!next-input-character;
2367        }        }
2368      }      }
2369            
2370      if ($match > 0) {      if ($match > 0) {
2371        return {type => CHARACTER_TOKEN, data => $value};        !!!cp (1023);
2372          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};
2373      } elsif ($match < 0) {      } elsif ($match < 0) {
2374        !!!parse-error (type => 'no refc');        !!!parse-error (type => 'no refc');
2375        if ($in_attr and $match < -1) {        if ($in_attr and $match < -1) {
2376            !!!cp (1024);
2377          return {type => CHARACTER_TOKEN, data => '&'.$entity_name};          return {type => CHARACTER_TOKEN, data => '&'.$entity_name};
2378        } else {        } else {
2379          return {type => CHARACTER_TOKEN, data => $value};          !!!cp (1025);
2380            return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};
2381        }        }
2382      } else {      } else {
2383          !!!cp (1026);
2384        !!!parse-error (type => 'bare ero');        !!!parse-error (type => 'bare ero');
2385        ## NOTE: No characters are consumed in the spec.        ## NOTE: "No characters are consumed" in the spec.
2386        return {type => CHARACTER_TOKEN, data => '&'.$value};        return {type => CHARACTER_TOKEN, data => '&'.$value};
2387      }      }
2388    } else {    } else {
2389        !!!cp (1027);
2390      ## no characters are consumed      ## no characters are consumed
2391      !!!parse-error (type => 'bare ero');      !!!parse-error (type => 'bare ero');
2392      return undef;      return undef;
# Line 1841  sub _construct_tree ($) { Line 2426  sub _construct_tree ($) {
2426        
2427    !!!next-token;    !!!next-token;
2428    
   $self->{insertion_mode} = BEFORE_HEAD_IM;  
2429    undef $self->{form_element};    undef $self->{form_element};
2430    undef $self->{head_element};    undef $self->{head_element};
2431    $self->{open_elements} = [];    $self->{open_elements} = [];
2432    undef $self->{inner_html_node};    undef $self->{inner_html_node};
2433    
2434      ## NOTE: The "initial" insertion mode.
2435    $self->_tree_construction_initial; # MUST    $self->_tree_construction_initial; # MUST
2436    
2437      ## NOTE: The "before html" insertion mode.
2438    $self->_tree_construction_root_element;    $self->_tree_construction_root_element;
2439      $self->{insertion_mode} = BEFORE_HEAD_IM;
2440    
2441      ## NOTE: The "before head" insertion mode and so on.
2442    $self->_tree_construction_main;    $self->_tree_construction_main;
2443  } # _construct_tree  } # _construct_tree
2444    
2445  sub _tree_construction_initial ($) {  sub _tree_construction_initial ($) {
2446    my $self = shift;    my $self = shift;
2447    
2448      ## NOTE: "initial" insertion mode
2449    
2450    INITIAL: {    INITIAL: {
2451      if ($token->{type} == DOCTYPE_TOKEN) {      if ($token->{type} == DOCTYPE_TOKEN) {
2452        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"
# Line 1865  sub _tree_construction_initial ($) { Line 2458  sub _tree_construction_initial ($) {
2458        if (not defined $token->{name} or # <!DOCTYPE>        if (not defined $token->{name} or # <!DOCTYPE>
2459            defined $token->{public_identifier} or            defined $token->{public_identifier} or
2460            defined $token->{system_identifier}) {            defined $token->{system_identifier}) {
2461            !!!cp ('t1');
2462          !!!parse-error (type => 'not HTML5');          !!!parse-error (type => 'not HTML5');
2463        } elsif ($doctype_name ne 'HTML') {        } elsif ($doctype_name ne 'HTML') {
2464            !!!cp ('t2');
2465          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)
2466          !!!parse-error (type => 'not HTML5');          !!!parse-error (type => 'not HTML5');
2467          } else {
2468            !!!cp ('t3');
2469        }        }
2470                
2471        my $doctype = $self->{document}->create_document_type_definition        my $doctype = $self->{document}->create_document_type_definition
# Line 1881  sub _tree_construction_initial ($) { Line 2478  sub _tree_construction_initial ($) {
2478        ## ISSUE: internalSubset = null??        ## ISSUE: internalSubset = null??
2479        $self->{document}->append_child ($doctype);        $self->{document}->append_child ($doctype);
2480                
2481        if (not $token->{correct} or $doctype_name ne 'HTML') {        if ($token->{quirks} or $doctype_name ne 'HTML') {
2482            !!!cp ('t4');
2483          $self->{document}->manakai_compat_mode ('quirks');          $self->{document}->manakai_compat_mode ('quirks');
2484        } elsif (defined $token->{public_identifier}) {        } elsif (defined $token->{public_identifier}) {
2485          my $pubid = $token->{public_identifier};          my $pubid = $token->{public_identifier};
# Line 1935  sub _tree_construction_initial ($) { Line 2533  sub _tree_construction_initial ($) {
2533            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,
2534            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,
2535            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,
2536              "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//EN" => 1,
2537              "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//EN" => 1,
2538              "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//EN" => 1,
2539            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,
2540            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,
2541            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,
# Line 1957  sub _tree_construction_initial ($) { Line 2558  sub _tree_construction_initial ($) {
2558            "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" => 1,            "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" => 1,
2559            "HTML" => 1,            "HTML" => 1,
2560          }->{$pubid}) {          }->{$pubid}) {
2561              !!!cp ('t5');
2562            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
2563          } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or          } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or
2564                   $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {                   $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {
2565            if (defined $token->{system_identifier}) {            if (defined $token->{system_identifier}) {
2566                !!!cp ('t6');
2567              $self->{document}->manakai_compat_mode ('quirks');              $self->{document}->manakai_compat_mode ('quirks');
2568            } else {            } else {
2569                !!!cp ('t7');
2570              $self->{document}->manakai_compat_mode ('limited quirks');              $self->{document}->manakai_compat_mode ('limited quirks');
2571            }            }
2572          } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 Frameset//EN" or          } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 FRAMESET//EN" or
2573                   $pubid eq "-//W3C//DTD XHTML 1.0 Transitional//EN") {                   $pubid eq "-//W3C//DTD XHTML 1.0 TRANSITIONAL//EN") {
2574              !!!cp ('t8');
2575            $self->{document}->manakai_compat_mode ('limited quirks');            $self->{document}->manakai_compat_mode ('limited quirks');
2576            } else {
2577              !!!cp ('t9');
2578          }          }
2579          } else {
2580            !!!cp ('t10');
2581        }        }
2582        if (defined $token->{system_identifier}) {        if (defined $token->{system_identifier}) {
2583          my $sysid = $token->{system_identifier};          my $sysid = $token->{system_identifier};
2584          $sysid =~ tr/A-Z/a-z/;          $sysid =~ tr/A-Z/a-z/;
2585          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") {
2586              ## TODO: Check the spec: PUBLIC "(limited quirks)" "(quirks)"
2587            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
2588              !!!cp ('t11');
2589            } else {
2590              !!!cp ('t12');
2591          }          }
2592          } else {
2593            !!!cp ('t13');
2594        }        }
2595                
2596        ## Go to the root element phase.        ## Go to the "before html" insertion mode.
2597        !!!next-token;        !!!next-token;
2598        return;        return;
2599      } elsif ({      } elsif ({
# Line 1986  sub _tree_construction_initial ($) { Line 2601  sub _tree_construction_initial ($) {
2601                END_TAG_TOKEN, 1,                END_TAG_TOKEN, 1,
2602                END_OF_FILE_TOKEN, 1,                END_OF_FILE_TOKEN, 1,
2603               }->{$token->{type}}) {               }->{$token->{type}}) {
2604          !!!cp ('t14');
2605        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE');
2606        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
2607        ## Go to the root element phase        ## Go to the "before html" insertion mode.
2608        ## reprocess        ## reprocess
2609        return;        return;
2610      } elsif ($token->{type} == CHARACTER_TOKEN) {      } elsif ($token->{type} == CHARACTER_TOKEN) {
# Line 1996  sub _tree_construction_initial ($) { Line 2612  sub _tree_construction_initial ($) {
2612          ## Ignore the token          ## Ignore the token
2613    
2614          unless (length $token->{data}) {          unless (length $token->{data}) {
2615            ## Stay in the phase            !!!cp ('t15');
2616              ## Stay in the insertion mode.
2617            !!!next-token;            !!!next-token;
2618            redo INITIAL;            redo INITIAL;
2619            } else {
2620              !!!cp ('t16');
2621          }          }
2622          } else {
2623            !!!cp ('t17');
2624        }        }
2625    
2626        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE');
2627        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
2628        ## Go to the root element phase        ## Go to the "before html" insertion mode.
2629        ## reprocess        ## reprocess
2630        return;        return;
2631      } elsif ($token->{type} == COMMENT_TOKEN) {      } elsif ($token->{type} == COMMENT_TOKEN) {
2632          !!!cp ('t18');
2633        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
2634        $self->{document}->append_child ($comment);        $self->{document}->append_child ($comment);
2635                
2636        ## Stay in the phase.        ## Stay in the insertion mode.
2637        !!!next-token;        !!!next-token;
2638        redo INITIAL;        redo INITIAL;
2639      } else {      } else {
2640        die "$0: $token->{type}: Unknown token type";        die "$0: $token->{type}: Unknown token type";
2641      }      }
2642    } # INITIAL    } # INITIAL
2643    
2644      die "$0: _tree_construction_initial: This should be never reached";
2645  } # _tree_construction_initial  } # _tree_construction_initial
2646    
2647  sub _tree_construction_root_element ($) {  sub _tree_construction_root_element ($) {
2648    my $self = shift;    my $self = shift;
2649    
2650      ## NOTE: "before html" insertion mode.
2651        
2652    B: {    B: {
2653        if ($token->{type} == DOCTYPE_TOKEN) {        if ($token->{type} == DOCTYPE_TOKEN) {
2654            !!!cp ('t19');
2655          !!!parse-error (type => 'in html:#DOCTYPE');          !!!parse-error (type => 'in html:#DOCTYPE');
2656          ## Ignore the token          ## Ignore the token
2657          ## Stay in the phase          ## Stay in the insertion mode.
2658          !!!next-token;          !!!next-token;
2659          redo B;          redo B;
2660        } elsif ($token->{type} == COMMENT_TOKEN) {        } elsif ($token->{type} == COMMENT_TOKEN) {
2661            !!!cp ('t20');
2662          my $comment = $self->{document}->create_comment ($token->{data});          my $comment = $self->{document}->create_comment ($token->{data});
2663          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
2664          ## Stay in the phase          ## Stay in the insertion mode.
2665          !!!next-token;          !!!next-token;
2666          redo B;          redo B;
2667        } elsif ($token->{type} == CHARACTER_TOKEN) {        } elsif ($token->{type} == CHARACTER_TOKEN) {
# Line 2041  sub _tree_construction_root_element ($) Line 2669  sub _tree_construction_root_element ($)
2669            ## Ignore the token.            ## Ignore the token.
2670    
2671            unless (length $token->{data}) {            unless (length $token->{data}) {
2672              ## Stay in the phase              !!!cp ('t21');
2673                ## Stay in the insertion mode.
2674              !!!next-token;              !!!next-token;
2675              redo B;              redo B;
2676              } else {
2677                !!!cp ('t22');
2678            }            }
2679            } else {
2680              !!!cp ('t23');
2681          }          }
2682    
2683            $self->{application_cache_selection}->(undef);
2684    
2685          #          #
2686          } elsif ($token->{type} == START_TAG_TOKEN) {
2687            if ($token->{tag_name} eq 'html') {
2688              my $root_element;
2689              !!!create-element ($root_element, $token->{tag_name}, $token->{attributes});
2690              $self->{document}->append_child ($root_element);
2691              push @{$self->{open_elements}}, [$root_element, 'html'];
2692    
2693              if ($token->{attributes}->{manifest}) {
2694                !!!cp ('t24');
2695                $self->{application_cache_selection}
2696                    ->($token->{attributes}->{manifest}->{value});
2697                ## ISSUE: No relative reference resolution?
2698              } else {
2699                !!!cp ('t25');
2700                $self->{application_cache_selection}->(undef);
2701              }
2702    
2703              !!!next-token;
2704              return; ## Go to the "before head" insertion mode.
2705            } else {
2706              !!!cp ('t25.1');
2707              #
2708            }
2709        } elsif ({        } elsif ({
                 START_TAG_TOKEN, 1,  
2710                  END_TAG_TOKEN, 1,                  END_TAG_TOKEN, 1,
2711                  END_OF_FILE_TOKEN, 1,                  END_OF_FILE_TOKEN, 1,
2712                 }->{$token->{type}}) {                 }->{$token->{type}}) {
2713          ## ISSUE: There is an issue in the spec          !!!cp ('t26');
2714          #          #
2715        } else {        } else {
2716          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
2717        }        }
2718        my $root_element; !!!create-element ($root_element, 'html');  
2719        $self->{document}->append_child ($root_element);      my $root_element; !!!create-element ($root_element, 'html');
2720        push @{$self->{open_elements}}, [$root_element, 'html'];      $self->{document}->append_child ($root_element);
2721        ## reprocess      push @{$self->{open_elements}}, [$root_element, 'html'];
2722        #redo B;  
2723        return; ## Go to the main phase.      $self->{application_cache_selection}->(undef);
2724    
2725        ## NOTE: Reprocess the token.
2726        return; ## Go to the "before head" insertion mode.
2727    
2728        ## ISSUE: There is an issue in the spec
2729    } # B    } # B
2730    
2731      die "$0: _tree_construction_root_element: This should never be reached";
2732  } # _tree_construction_root_element  } # _tree_construction_root_element
2733    
2734  sub _reset_insertion_mode ($) {  sub _reset_insertion_mode ($) {
# Line 2078  sub _reset_insertion_mode ($) { Line 2743  sub _reset_insertion_mode ($) {
2743            
2744      ## Step 3      ## Step 3
2745      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"!?  
2746        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
2747          $last = 1;          $last = 1;
2748          if (defined $self->{inner_html_node}) {          if (defined $self->{inner_html_node}) {
2749            if ($self->{inner_html_node}->[1] eq 'td' or            if ($self->{inner_html_node}->[1] eq 'td' or
2750                $self->{inner_html_node}->[1] eq 'th') {                $self->{inner_html_node}->[1] eq 'th') {
2751                !!!cp ('t27');
2752              #              #
2753            } else {            } else {
2754                !!!cp ('t28');
2755              $node = $self->{inner_html_node};              $node = $self->{inner_html_node};
2756            }            }
2757          }          }
# Line 2098  sub _reset_insertion_mode ($) { Line 2760  sub _reset_insertion_mode ($) {
2760        ## Step 4..13        ## Step 4..13
2761        my $new_mode = {        my $new_mode = {
2762                        select => IN_SELECT_IM,                        select => IN_SELECT_IM,
2763                          ## NOTE: |option| and |optgroup| do not set
2764                          ## insertion mode to "in select" by themselves.
2765                        td => IN_CELL_IM,                        td => IN_CELL_IM,
2766                        th => IN_CELL_IM,                        th => IN_CELL_IM,
2767                        tr => IN_ROW_IM,                        tr => IN_ROW_IM,
# Line 2116  sub _reset_insertion_mode ($) { Line 2780  sub _reset_insertion_mode ($) {
2780        ## Step 14        ## Step 14
2781        if ($node->[1] eq 'html') {        if ($node->[1] eq 'html') {
2782          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
2783              !!!cp ('t29');
2784            $self->{insertion_mode} = BEFORE_HEAD_IM;            $self->{insertion_mode} = BEFORE_HEAD_IM;
2785          } else {          } else {
2786              ## ISSUE: Can this state be reached?
2787              !!!cp ('t30');
2788            $self->{insertion_mode} = AFTER_HEAD_IM;            $self->{insertion_mode} = AFTER_HEAD_IM;
2789          }          }
2790          return;          return;
2791          } else {
2792            !!!cp ('t31');
2793        }        }
2794                
2795        ## Step 15        ## Step 15
# Line 2133  sub _reset_insertion_mode ($) { Line 2802  sub _reset_insertion_mode ($) {
2802        ## Step 17        ## Step 17
2803        redo S3;        redo S3;
2804      } # S3      } # S3
2805    
2806      die "$0: _reset_insertion_mode: This line should never be reached";
2807  } # _reset_insertion_mode  } # _reset_insertion_mode
2808    
2809  sub _tree_construction_main ($) {  sub _tree_construction_main ($) {
# Line 2154  sub _tree_construction_main ($) { Line 2825  sub _tree_construction_main ($) {
2825      return if $entry->[0] eq '#marker';      return if $entry->[0] eq '#marker';
2826      for (@{$self->{open_elements}}) {      for (@{$self->{open_elements}}) {
2827        if ($entry->[0] eq $_->[0]) {        if ($entry->[0] eq $_->[0]) {
2828            !!!cp ('t32');
2829          return;          return;
2830        }        }
2831      }      }
# Line 2168  sub _tree_construction_main ($) { Line 2840  sub _tree_construction_main ($) {
2840    
2841        ## Step 6        ## Step 6
2842        if ($entry->[0] eq '#marker') {        if ($entry->[0] eq '#marker') {
2843            !!!cp ('t33_1');
2844          #          #
2845        } else {        } else {
2846          my $in_open_elements;          my $in_open_elements;
2847          OE: for (@{$self->{open_elements}}) {          OE: for (@{$self->{open_elements}}) {
2848            if ($entry->[0] eq $_->[0]) {            if ($entry->[0] eq $_->[0]) {
2849                !!!cp ('t33');
2850              $in_open_elements = 1;              $in_open_elements = 1;
2851              last OE;              last OE;
2852            }            }
2853          }          }
2854          if ($in_open_elements) {          if ($in_open_elements) {
2855              !!!cp ('t34');
2856            #            #
2857          } else {          } else {
2858              ## NOTE: <!DOCTYPE HTML><p><b><i><u></p> <p>X
2859              !!!cp ('t35');
2860            redo S4;            redo S4;
2861          }          }
2862        }        }
# Line 2202  sub _tree_construction_main ($) { Line 2879  sub _tree_construction_main ($) {
2879    
2880        ## Step 11        ## Step 11
2881        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {
2882            !!!cp ('t36');
2883          ## Step 7'          ## Step 7'
2884          $i++;          $i++;
2885          $entry = $active_formatting_elements->[$i];          $entry = $active_formatting_elements->[$i];
2886                    
2887          redo S7;          redo S7;
2888        }        }
2889    
2890          !!!cp ('t37');
2891      } # S7      } # S7
2892    }; # $reconstruct_active_formatting_elements    }; # $reconstruct_active_formatting_elements
2893    
2894    my $clear_up_to_marker = sub {    my $clear_up_to_marker = sub {
2895      for (reverse 0..$#$active_formatting_elements) {      for (reverse 0..$#$active_formatting_elements) {
2896        if ($active_formatting_elements->[$_]->[0] eq '#marker') {        if ($active_formatting_elements->[$_]->[0] eq '#marker') {
2897            !!!cp ('t38');
2898          splice @$active_formatting_elements, $_;          splice @$active_formatting_elements, $_;
2899          return;          return;
2900        }        }
2901      }      }
2902    
2903        !!!cp ('t39');
2904    }; # $clear_up_to_marker    }; # $clear_up_to_marker
2905    
2906    my $parse_rcdata = sub ($$) {    my $insert;
2907      my ($content_model_flag, $insert) = @_;  
2908      my $parse_rcdata = sub ($) {
2909        my ($content_model_flag) = @_;
2910    
2911      ## Step 1      ## Step 1
2912      my $start_tag_name = $token->{tag_name};      my $start_tag_name = $token->{tag_name};
# Line 2229  sub _tree_construction_main ($) { Line 2914  sub _tree_construction_main ($) {
2914      !!!create-element ($el, $start_tag_name, $token->{attributes});      !!!create-element ($el, $start_tag_name, $token->{attributes});
2915    
2916      ## Step 2      ## Step 2
2917      $insert->($el); # /context node/->append_child ($el)      $insert->($el);
2918    
2919      ## Step 3      ## Step 3
2920      $self->{content_model} = $content_model_flag; # CDATA or RCDATA      $self->{content_model} = $content_model_flag; # CDATA or RCDATA
# Line 2239  sub _tree_construction_main ($) { Line 2924  sub _tree_construction_main ($) {
2924      my $text = '';      my $text = '';
2925      !!!next-token;      !!!next-token;
2926      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
2927          !!!cp ('t40');
2928        $text .= $token->{data};        $text .= $token->{data};
2929        !!!next-token;        !!!next-token;
2930      }      }
2931    
2932      ## Step 5      ## Step 5
2933      if (length $text) {      if (length $text) {
2934          !!!cp ('t41');
2935        my $text = $self->{document}->create_text_node ($text);        my $text = $self->{document}->create_text_node ($text);
2936        $el->append_child ($text);        $el->append_child ($text);
2937      }      }
# Line 2253  sub _tree_construction_main ($) { Line 2940  sub _tree_construction_main ($) {
2940      $self->{content_model} = PCDATA_CONTENT_MODEL;      $self->{content_model} = PCDATA_CONTENT_MODEL;
2941    
2942      ## Step 7      ## Step 7
2943      if ($token->{type} == END_TAG_TOKEN and $token->{tag_name} eq $start_tag_name) {      if ($token->{type} == END_TAG_TOKEN and
2944            $token->{tag_name} eq $start_tag_name) {
2945          !!!cp ('t42');
2946        ## 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});  
2947      } else {      } else {
2948        die "$0: $content_model_flag in parse_rcdata";        ## NOTE: An end-of-file token.
2949          if ($content_model_flag == CDATA_CONTENT_MODEL) {
2950            !!!cp ('t43');
2951            !!!parse-error (type => 'in CDATA:#'.$token->{type});
2952          } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
2953            !!!cp ('t44');
2954            !!!parse-error (type => 'in RCDATA:#'.$token->{type});
2955          } else {
2956            die "$0: $content_model_flag in parse_rcdata";
2957          }
2958      }      }
2959      !!!next-token;      !!!next-token;
2960    }; # $parse_rcdata    }; # $parse_rcdata
2961    
2962    my $script_start_tag = sub ($) {    my $script_start_tag = sub () {
     my $insert = $_[0];  
2963      my $script_el;      my $script_el;
2964      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, 'script', $token->{attributes});
2965      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
# Line 2277  sub _tree_construction_main ($) { Line 2970  sub _tree_construction_main ($) {
2970      my $text = '';      my $text = '';
2971      !!!next-token;      !!!next-token;
2972      while ($token->{type} == CHARACTER_TOKEN) {      while ($token->{type} == CHARACTER_TOKEN) {
2973          !!!cp ('t45');
2974        $text .= $token->{data};        $text .= $token->{data};
2975        !!!next-token;        !!!next-token;
2976      } # stop if non-character token or tokenizer stops tokenising      } # stop if non-character token or tokenizer stops tokenising
2977      if (length $text) {      if (length $text) {
2978          !!!cp ('t46');
2979        $script_el->manakai_append_text ($text);        $script_el->manakai_append_text ($text);
2980      }      }
2981                                
# Line 2288  sub _tree_construction_main ($) { Line 2983  sub _tree_construction_main ($) {
2983    
2984      if ($token->{type} == END_TAG_TOKEN and      if ($token->{type} == END_TAG_TOKEN and
2985          $token->{tag_name} eq 'script') {          $token->{tag_name} eq 'script') {
2986          !!!cp ('t47');
2987        ## Ignore the token        ## Ignore the token
2988      } else {      } else {
2989          !!!cp ('t48');
2990        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!parse-error (type => 'in CDATA:#'.$token->{type});
2991        ## ISSUE: And ignore?        ## ISSUE: And ignore?
2992        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
2993      }      }
2994            
2995      if (defined $self->{inner_html_node}) {      if (defined $self->{inner_html_node}) {
2996          !!!cp ('t49');
2997        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
2998      } else {      } else {
2999          !!!cp ('t50');
3000        ## TODO: $old_insertion_point = current insertion point        ## TODO: $old_insertion_point = current insertion point
3001        ## TODO: insertion point = just before the next input character        ## TODO: insertion point = just before the next input character
3002    
# Line 2320  sub _tree_construction_main ($) { Line 3019  sub _tree_construction_main ($) {
3019        my $formatting_element_i_in_active;        my $formatting_element_i_in_active;
3020        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3021          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {
3022              !!!cp ('t51');
3023            $formatting_element = $active_formatting_elements->[$_];            $formatting_element = $active_formatting_elements->[$_];
3024            $formatting_element_i_in_active = $_;            $formatting_element_i_in_active = $_;
3025            last AFE;            last AFE;
3026          } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {          } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {
3027              !!!cp ('t52');
3028            last AFE;            last AFE;
3029          }          }
3030        } # AFE        } # AFE
3031        unless (defined $formatting_element) {        unless (defined $formatting_element) {
3032            !!!cp ('t53');
3033          !!!parse-error (type => 'unmatched end tag:'.$tag_name);          !!!parse-error (type => 'unmatched end tag:'.$tag_name);
3034          ## Ignore the token          ## Ignore the token
3035          !!!next-token;          !!!next-token;
# Line 2340  sub _tree_construction_main ($) { Line 3042  sub _tree_construction_main ($) {
3042          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
3043          if ($node->[0] eq $formatting_element->[0]) {          if ($node->[0] eq $formatting_element->[0]) {
3044            if ($in_scope) {            if ($in_scope) {
3045                !!!cp ('t54');
3046              $formatting_element_i_in_open = $_;              $formatting_element_i_in_open = $_;
3047              last INSCOPE;              last INSCOPE;
3048            } else { # in open elements but not in scope            } else { # in open elements but not in scope
3049                !!!cp ('t55');
3050              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3051              ## Ignore the token              ## Ignore the token
3052              !!!next-token;              !!!next-token;
# Line 2352  sub _tree_construction_main ($) { Line 3056  sub _tree_construction_main ($) {
3056                    table => 1, caption => 1, td => 1, th => 1,                    table => 1, caption => 1, td => 1, th => 1,
3057                    button => 1, marquee => 1, object => 1, html => 1,                    button => 1, marquee => 1, object => 1, html => 1,
3058                   }->{$node->[1]}) {                   }->{$node->[1]}) {
3059              !!!cp ('t56');
3060            $in_scope = 0;            $in_scope = 0;
3061          }          }
3062        } # INSCOPE        } # INSCOPE
3063        unless (defined $formatting_element_i_in_open) {        unless (defined $formatting_element_i_in_open) {
3064            !!!cp ('t57');
3065          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3066          pop @$active_formatting_elements; # $formatting_element          pop @$active_formatting_elements; # $formatting_element
3067          !!!next-token; ## TODO: ok?          !!!next-token; ## TODO: ok?
3068          return;          return;
3069        }        }
3070        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
3071            !!!cp ('t58');
3072          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3073        }        }
3074                
# Line 2374  sub _tree_construction_main ($) { Line 3081  sub _tree_construction_main ($) {
3081              #not $phrasing_category->{$node->[1]} and              #not $phrasing_category->{$node->[1]} and
3082              ($special_category->{$node->[1]} or              ($special_category->{$node->[1]} or
3083               $scoping_category->{$node->[1]})) {               $scoping_category->{$node->[1]})) {
3084              !!!cp ('t59');
3085            $furthest_block = $node;            $furthest_block = $node;
3086            $furthest_block_i_in_open = $_;            $furthest_block_i_in_open = $_;
3087          } elsif ($node->[0] eq $formatting_element->[0]) {          } elsif ($node->[0] eq $formatting_element->[0]) {
3088              !!!cp ('t60');
3089            last OE;            last OE;
3090          }          }
3091        } # OE        } # OE
3092                
3093        ## Step 3        ## Step 3
3094        unless (defined $furthest_block) { # MUST        unless (defined $furthest_block) { # MUST
3095            !!!cp ('t61');
3096          splice @{$self->{open_elements}}, $formatting_element_i_in_open;          splice @{$self->{open_elements}}, $formatting_element_i_in_open;
3097          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;
3098          !!!next-token;          !!!next-token;
# Line 2395  sub _tree_construction_main ($) { Line 3105  sub _tree_construction_main ($) {
3105        ## Step 5        ## Step 5
3106        my $furthest_block_parent = $furthest_block->[0]->parent_node;        my $furthest_block_parent = $furthest_block->[0]->parent_node;
3107        if (defined $furthest_block_parent) {        if (defined $furthest_block_parent) {
3108            !!!cp ('t62');
3109          $furthest_block_parent->remove_child ($furthest_block->[0]);          $furthest_block_parent->remove_child ($furthest_block->[0]);
3110        }        }
3111                
# Line 2417  sub _tree_construction_main ($) { Line 3128  sub _tree_construction_main ($) {
3128          S7S2: {          S7S2: {
3129            for (reverse 0..$#$active_formatting_elements) {            for (reverse 0..$#$active_formatting_elements) {
3130              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
3131                  !!!cp ('t63');
3132                $node_i_in_active = $_;                $node_i_in_active = $_;
3133                last S7S2;                last S7S2;
3134              }              }
# Line 2430  sub _tree_construction_main ($) { Line 3142  sub _tree_construction_main ($) {
3142                    
3143          ## Step 4          ## Step 4
3144          if ($last_node->[0] eq $furthest_block->[0]) {          if ($last_node->[0] eq $furthest_block->[0]) {
3145              !!!cp ('t64');
3146            $bookmark_prev_el = $node->[0];            $bookmark_prev_el = $node->[0];
3147          }          }
3148                    
3149          ## Step 5          ## Step 5
3150          if ($node->[0]->has_child_nodes ()) {          if ($node->[0]->has_child_nodes ()) {
3151              !!!cp ('t65');
3152            my $clone = [$node->[0]->clone_node (0), $node->[1]];            my $clone = [$node->[0]->clone_node (0), $node->[1]];
3153            $active_formatting_elements->[$node_i_in_active] = $clone;            $active_formatting_elements->[$node_i_in_active] = $clone;
3154            $self->{open_elements}->[$node_i_in_open] = $clone;            $self->{open_elements}->[$node_i_in_open] = $clone;
# Line 2469  sub _tree_construction_main ($) { Line 3183  sub _tree_construction_main ($) {
3183        my $i;        my $i;
3184        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3185          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {
3186              !!!cp ('t66');
3187            splice @$active_formatting_elements, $_, 1;            splice @$active_formatting_elements, $_, 1;
3188            $i-- and last AFE if defined $i;            $i-- and last AFE if defined $i;
3189          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {
3190              !!!cp ('t67');
3191            $i = $_;            $i = $_;
3192          }          }
3193        } # AFE        } # AFE
# Line 2481  sub _tree_construction_main ($) { Line 3197  sub _tree_construction_main ($) {
3197        undef $i;        undef $i;
3198        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3199          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {
3200              !!!cp ('t68');
3201            splice @{$self->{open_elements}}, $_, 1;            splice @{$self->{open_elements}}, $_, 1;
3202            $i-- and last OE if defined $i;            $i-- and last OE if defined $i;
3203          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {
3204              !!!cp ('t69');
3205            $i = $_;            $i = $_;
3206          }          }
3207        } # OE        } # OE
# Line 2494  sub _tree_construction_main ($) { Line 3212  sub _tree_construction_main ($) {
3212      } # FET      } # FET
3213    }; # $formatting_end_tag    }; # $formatting_end_tag
3214    
3215    my $insert_to_current = sub {    ## NOTE: $open_tables->[-1]->[0] is the "current table".
3216      ## NOTE: $open_tables->[-1]->[1] is the "tainted" flag.
3217      my $open_tables = [[$self->{open_elements}->[0]->[0]]];
3218    
3219      $insert = my $insert_to_current = sub {
3220      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);
3221    }; # $insert_to_current    }; # $insert_to_current
3222    
3223    my $insert_to_foster = sub {    my $insert_to_foster = sub {
3224                         my $child = shift;      my $child = shift;
3225                         if ({      if ({
3226                              table => 1, tbody => 1, tfoot => 1,           table => 1, tbody => 1, tfoot => 1, thead => 1, tr => 1,
3227                              thead => 1, tr => 1,          }->{$self->{open_elements}->[-1]->[1]}) {
3228                             }->{$self->{open_elements}->[-1]->[1]}) {        # MUST
3229                           # MUST        my $foster_parent_element;
3230                           my $foster_parent_element;        my $next_sibling;
                          my $next_sibling;  
3231                           OE: for (reverse 0..$#{$self->{open_elements}}) {                           OE: for (reverse 0..$#{$self->{open_elements}}) {
3232                             if ($self->{open_elements}->[$_]->[1] eq 'table') {                             if ($self->{open_elements}->[$_]->[1] eq 'table') {
3233                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3234                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
3235                                   !!!cp ('t70');
3236                                 $foster_parent_element = $parent;                                 $foster_parent_element = $parent;
3237                                 $next_sibling = $self->{open_elements}->[$_]->[0];                                 $next_sibling = $self->{open_elements}->[$_]->[0];
3238                               } else {                               } else {
3239                                   !!!cp ('t71');
3240                                 $foster_parent_element                                 $foster_parent_element
3241                                   = $self->{open_elements}->[$_ - 1]->[0];                                   = $self->{open_elements}->[$_ - 1]->[0];
3242                               }                               }
# Line 2524  sub _tree_construction_main ($) { Line 3247  sub _tree_construction_main ($) {
3247                             unless defined $foster_parent_element;                             unless defined $foster_parent_element;
3248                           $foster_parent_element->insert_before                           $foster_parent_element->insert_before
3249                             ($child, $next_sibling);                             ($child, $next_sibling);
3250                         } else {        $open_tables->[-1]->[1] = 1; # tainted
3251                           $self->{open_elements}->[-1]->[0]->append_child ($child);      } else {
3252                         }        !!!cp ('t72');
3253          $self->{open_elements}->[-1]->[0]->append_child ($child);
3254        }
3255    }; # $insert_to_foster    }; # $insert_to_foster
3256    
   my $insert;  
   
3257    B: {    B: {
3258      if ($token->{type} == DOCTYPE_TOKEN) {      if ($token->{type} == DOCTYPE_TOKEN) {
3259          !!!cp ('t73');
3260        !!!parse-error (type => 'DOCTYPE in the middle');        !!!parse-error (type => 'DOCTYPE in the middle');
3261        ## Ignore the token        ## Ignore the token
3262        ## Stay in the phase        ## Stay in the phase
# Line 2540  sub _tree_construction_main ($) { Line 3264  sub _tree_construction_main ($) {
3264        redo B;        redo B;
3265      } elsif ($token->{type} == END_OF_FILE_TOKEN) {      } elsif ($token->{type} == END_OF_FILE_TOKEN) {
3266        if ($self->{insertion_mode} & AFTER_HTML_IMS) {        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
3267            !!!cp ('t74');
3268          #          #
3269        } else {        } else {
3270          ## Generate implied end tags          ## Generate implied end tags
3271          if ({          while ({
3272               dd => 1, dt => 1, li => 1, p => 1, td => 1, th => 1, tr => 1,                  dd => 1, dt => 1, li => 1, p => 1,
3273               tbody => 1, tfoot=> 1, thead => 1,                 }->{$self->{open_elements}->[-1]->[1]}) {
3274              }->{$self->{open_elements}->[-1]->[1]}) {            !!!cp ('t75');
3275            !!!back-token;            pop @{$self->{open_elements}};
           $token = {type => END_TAG_TOKEN, tag_name => $self->{open_elements}->[-1]->[1]};  
           redo B;  
3276          }          }
3277                    
3278          if (@{$self->{open_elements}} > 2 or          if (@{$self->{open_elements}} > 2 or
3279              (@{$self->{open_elements}} == 2 and $self->{open_elements}->[1]->[1] ne 'body')) {              (@{$self->{open_elements}} == 2 and $self->{open_elements}->[1]->[1] ne 'body')) {
3280              !!!cp ('t76');
3281            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3282          } elsif (defined $self->{inner_html_node} and          } elsif (defined $self->{inner_html_node} and
3283                   @{$self->{open_elements}} > 1 and                   @{$self->{open_elements}} > 1 and
3284                   $self->{open_elements}->[1]->[1] ne 'body') {                   $self->{open_elements}->[1]->[1] ne 'body') {
3285    ## ISSUE: This case is never reached.
3286              !!!cp ('t77');
3287            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3288            } else {
3289              !!!cp ('t78');
3290          }          }
3291    
3292          ## ISSUE: There is an issue in the spec.          ## ISSUE: There is an issue in the spec.
# Line 2569  sub _tree_construction_main ($) { Line 3297  sub _tree_construction_main ($) {
3297      } elsif ($token->{type} == START_TAG_TOKEN and      } elsif ($token->{type} == START_TAG_TOKEN and
3298               $token->{tag_name} eq 'html') {               $token->{tag_name} eq 'html') {
3299        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
3300          ## Turn into the main phase          !!!cp ('t79');
3301          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html:html');
3302          $self->{insertion_mode} = AFTER_BODY_IM;          $self->{insertion_mode} = AFTER_BODY_IM;
3303        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
3304          ## Turn into the main phase          !!!cp ('t80');
3305          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html:html');
3306          $self->{insertion_mode} = AFTER_FRAMESET_IM;          $self->{insertion_mode} = AFTER_FRAMESET_IM;
3307          } else {
3308            !!!cp ('t81');
3309        }        }
3310    
3311  ## ISSUE: "aa<html>" is not a parse error.        !!!cp ('t82');
3312  ## ISSUE: "<html>" in fragment is not a parse error.        !!!parse-error (type => 'not first start tag');
       unless ($token->{first_start_tag}) {  
         !!!parse-error (type => 'not first start tag');  
       }  
3313        my $top_el = $self->{open_elements}->[0]->[0];        my $top_el = $self->{open_elements}->[0]->[0];
3314        for my $attr_name (keys %{$token->{attributes}}) {        for my $attr_name (keys %{$token->{attributes}}) {
3315          unless ($top_el->has_attribute_ns (undef, $attr_name)) {          unless ($top_el->has_attribute_ns (undef, $attr_name)) {
3316              !!!cp ('t84');
3317            $top_el->set_attribute_ns            $top_el->set_attribute_ns
3318              (undef, [undef, $attr_name],              (undef, [undef, $attr_name],
3319               $token->{attributes}->{$attr_name}->{value});               $token->{attributes}->{$attr_name}->{value});
# Line 2596  sub _tree_construction_main ($) { Line 3324  sub _tree_construction_main ($) {
3324      } elsif ($token->{type} == COMMENT_TOKEN) {      } elsif ($token->{type} == COMMENT_TOKEN) {
3325        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
3326        if ($self->{insertion_mode} & AFTER_HTML_IMS) {        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
3327            !!!cp ('t85');
3328          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
3329        } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {        } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {
3330            !!!cp ('t86');
3331          $self->{open_elements}->[0]->[0]->append_child ($comment);          $self->{open_elements}->[0]->[0]->append_child ($comment);
3332        } else {        } else {
3333            !!!cp ('t87');
3334          $self->{open_elements}->[-1]->[0]->append_child ($comment);          $self->{open_elements}->[-1]->[0]->append_child ($comment);
3335        }        }
3336        !!!next-token;        !!!next-token;
# Line 2607  sub _tree_construction_main ($) { Line 3338  sub _tree_construction_main ($) {
3338      } elsif ($self->{insertion_mode} & HEAD_IMS) {      } elsif ($self->{insertion_mode} & HEAD_IMS) {
3339        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
3340          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
3341            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3342                !!!cp ('t88.2');
3343                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
3344              } else {
3345                !!!cp ('t88.1');
3346                ## Ignore the token.
3347                !!!next-token;
3348                redo B;
3349              }
3350            unless (length $token->{data}) {            unless (length $token->{data}) {
3351                !!!cp ('t88');
3352              !!!next-token;              !!!next-token;
3353              redo B;              redo B;
3354            }            }
3355          }          }
3356    
3357          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3358              !!!cp ('t89');
3359            ## As if <head>            ## As if <head>
3360            !!!create-element ($self->{head_element}, 'head');            !!!create-element ($self->{head_element}, 'head');
3361            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
# Line 2625  sub _tree_construction_main ($) { Line 3366  sub _tree_construction_main ($) {
3366    
3367            ## Reprocess in the "after head" insertion mode...            ## Reprocess in the "after head" insertion mode...
3368          } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {          } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3369              !!!cp ('t90');
3370            ## As if </noscript>            ## As if </noscript>
3371            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
3372            !!!parse-error (type => 'in noscript:#character');            !!!parse-error (type => 'in noscript:#character');
# Line 2635  sub _tree_construction_main ($) { Line 3377  sub _tree_construction_main ($) {
3377    
3378            ## Reprocess in the "after head" insertion mode...            ## Reprocess in the "after head" insertion mode...
3379          } elsif ($self->{insertion_mode} == IN_HEAD_IM) {          } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3380              !!!cp ('t91');
3381            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
3382    
3383            ## Reprocess in the "after head" insertion mode...            ## Reprocess in the "after head" insertion mode...
3384            } else {
3385              !!!cp ('t92');
3386          }          }
3387    
3388              ## "after head" insertion mode              ## "after head" insertion mode
# Line 2649  sub _tree_construction_main ($) { Line 3394  sub _tree_construction_main ($) {
3394            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
3395              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
3396                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3397                    !!!cp ('t93');
3398                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});
3399                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3400                  push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];                  push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];
# Line 2656  sub _tree_construction_main ($) { Line 3402  sub _tree_construction_main ($) {
3402                  !!!next-token;                  !!!next-token;
3403                  redo B;                  redo B;
3404                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
3405                    !!!cp ('t94');
3406                  #                  #
3407                } else {                } else {
3408                    !!!cp ('t95');
3409                  !!!parse-error (type => 'in head:head'); # or in head noscript                  !!!parse-error (type => 'in head:head'); # or in head noscript
3410                  ## Ignore the token                  ## Ignore the token
3411                  !!!next-token;                  !!!next-token;
3412                  redo B;                  redo B;
3413                }                }
3414              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3415                  !!!cp ('t96');
3416                ## As if <head>                ## As if <head>
3417                !!!create-element ($self->{head_element}, 'head');                !!!create-element ($self->{head_element}, 'head');
3418                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
# Line 2671  sub _tree_construction_main ($) { Line 3420  sub _tree_construction_main ($) {
3420    
3421                $self->{insertion_mode} = IN_HEAD_IM;                $self->{insertion_mode} = IN_HEAD_IM;
3422                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
3423                } else {
3424                  !!!cp ('t97');
3425              }              }
3426    
3427              if ($token->{tag_name} eq 'base') {              if ($token->{tag_name} eq 'base') {
3428                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3429                    !!!cp ('t98');
3430                  ## As if </noscript>                  ## As if </noscript>
3431                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3432                  !!!parse-error (type => 'in noscript:base');                  !!!parse-error (type => 'in noscript:base');
3433                                
3434                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
3435                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3436                  } else {
3437                    !!!cp ('t99');
3438                }                }
3439    
3440                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3441                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3442                    !!!cp ('t100');
3443                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
3444                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3445                  } else {
3446                    !!!cp ('t101');
3447                }                }
3448                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
3449                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
3450                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
3451                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
3452                !!!next-token;                !!!next-token;
3453                redo B;                redo B;
3454              } elsif ($token->{tag_name} eq 'link') {              } elsif ($token->{tag_name} eq 'link') {
3455                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3456                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3457                    !!!cp ('t102');
3458                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
3459                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3460                  } else {
3461                    !!!cp ('t103');
3462                }                }
3463                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
3464                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
3465                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
3466                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
3467                !!!next-token;                !!!next-token;
3468                redo B;                redo B;
3469              } elsif ($token->{tag_name} eq 'meta') {              } elsif ($token->{tag_name} eq 'meta') {
3470                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3471                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3472                    !!!cp ('t104');
3473                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
3474                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3475                  } else {
3476                    !!!cp ('t105');
3477                }                }
3478                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
3479                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
3480    
3481                unless ($self->{confident}) {                unless ($self->{confident}) {
                 my $charset;  
3482                  if ($token->{attributes}->{charset}) { ## TODO: And if supported                  if ($token->{attributes}->{charset}) { ## TODO: And if supported
3483                    $charset = $token->{attributes}->{charset}->{value};                    !!!cp ('t106');
3484                  }                    $self->{change_encoding}
3485                  if ($token->{attributes}->{'http-equiv'}) {                        ->($self, $token->{attributes}->{charset}->{value});
3486                      
3487                      $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
3488                          ->set_user_data (manakai_has_reference =>
3489                                               $token->{attributes}->{charset}
3490                                                   ->{has_reference});
3491                    } elsif ($token->{attributes}->{content}) {
3492                    ## 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.
3493                    if ($token->{attributes}->{'http-equiv'}->{value}                    if ($token->{attributes}->{content}->{value}
3494                        =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=                        =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
3495                              [\x09-\x0D\x20]*=
3496                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
3497                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
3498                      $charset = defined $1 ? $1 : defined $2 ? $2 : $3;                      !!!cp ('t107');
3499                    } ## TODO: And if supported                      $self->{change_encoding}
3500                            ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);
3501                        $meta_el->[0]->get_attribute_node_ns (undef, 'content')
3502                            ->set_user_data (manakai_has_reference =>
3503                                                 $token->{attributes}->{content}
3504                                                       ->{has_reference});
3505                      } else {
3506                        !!!cp ('t108');
3507                      }
3508                    }
3509                  } else {
3510                    if ($token->{attributes}->{charset}) {
3511                      !!!cp ('t109');
3512                      $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
3513                          ->set_user_data (manakai_has_reference =>
3514                                               $token->{attributes}->{charset}
3515                                                   ->{has_reference});
3516                    }
3517                    if ($token->{attributes}->{content}) {
3518                      !!!cp ('t110');
3519                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')
3520                          ->set_user_data (manakai_has_reference =>
3521                                               $token->{attributes}->{content}
3522                                                   ->{has_reference});
3523                  }                  }
                 ## TODO: Change the encoding  
3524                }                }
3525    
3526                ## TODO: Extracting |charset| from |meta|.                pop @{$self->{open_elements}} # <head>
               pop @{$self->{open_elements}}  
3527                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
3528                !!!next-token;                !!!next-token;
3529                redo B;                redo B;
3530              } elsif ($token->{tag_name} eq 'title') {              } elsif ($token->{tag_name} eq 'title') {
3531                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3532                    !!!cp ('t111');
3533                  ## As if </noscript>                  ## As if </noscript>
3534                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3535                  !!!parse-error (type => 'in noscript:title');                  !!!parse-error (type => 'in noscript:title');
# Line 2746  sub _tree_construction_main ($) { Line 3537  sub _tree_construction_main ($) {
3537                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
3538                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3539                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
3540                    !!!cp ('t112');
3541                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
3542                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3543                  } else {
3544                    !!!cp ('t113');
3545                }                }
3546    
3547                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3548                my $parent = defined $self->{head_element} ? $self->{head_element}                my $parent = defined $self->{head_element} ? $self->{head_element}
3549                    : $self->{open_elements}->[-1]->[0];                    : $self->{open_elements}->[-1]->[0];
3550                $parse_rcdata->(RCDATA_CONTENT_MODEL,                $parse_rcdata->(RCDATA_CONTENT_MODEL);
3551                                sub { $parent->append_child ($_[0]) });                pop @{$self->{open_elements}} # <head>
               pop @{$self->{open_elements}}  
3552                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
3553                redo B;                redo B;
3554              } elsif ($token->{tag_name} eq 'style') {              } elsif ($token->{tag_name} eq 'style') {
# Line 2763  sub _tree_construction_main ($) { Line 3556  sub _tree_construction_main ($) {
3556                ## insertion mode IN_HEAD_IM)                ## insertion mode IN_HEAD_IM)
3557                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3558                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3559                    !!!cp ('t114');
3560                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
3561                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3562                  } else {
3563                    !!!cp ('t115');
3564                }                }
3565                $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);                $parse_rcdata->(CDATA_CONTENT_MODEL);
3566                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
3567                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
3568                redo B;                redo B;
3569              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
3570                if ($self->{insertion_mode} == IN_HEAD_IM) {                if ($self->{insertion_mode} == IN_HEAD_IM) {
3571                    !!!cp ('t116');
3572                  ## NOTE: and scripting is disalbed                  ## NOTE: and scripting is disalbed
3573                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes});
3574                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
3575                  !!!next-token;                  !!!next-token;
3576                  redo B;                  redo B;
3577                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3578                    !!!cp ('t117');
3579                  !!!parse-error (type => 'in noscript:noscript');                  !!!parse-error (type => 'in noscript:noscript');
3580                  ## Ignore the token                  ## Ignore the token
3581                  !!!next-token;                  !!!next-token;
3582                  redo B;                  redo B;
3583                } else {                } else {
3584                    !!!cp ('t118');
3585                  #                  #
3586                }                }
3587              } elsif ($token->{tag_name} eq 'script') {              } elsif ($token->{tag_name} eq 'script') {
3588                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3589                    !!!cp ('t119');
3590                  ## As if </noscript>                  ## As if </noscript>
3591                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3592                  !!!parse-error (type => 'in noscript:script');                  !!!parse-error (type => 'in noscript:script');
# Line 2794  sub _tree_construction_main ($) { Line 3594  sub _tree_construction_main ($) {
3594                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
3595                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3596                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
3597                    !!!cp ('t120');
3598                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
3599                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3600                  } else {
3601                    !!!cp ('t121');
3602                }                }
3603    
3604                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3605                $script_start_tag->($insert_to_current);                $script_start_tag->();
3606                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
3607                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
3608                redo B;                redo B;
3609              } elsif ($token->{tag_name} eq 'body' or              } elsif ($token->{tag_name} eq 'body' or
3610                       $token->{tag_name} eq 'frameset') {                       $token->{tag_name} eq 'frameset') {
3611                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3612                    !!!cp ('t122');
3613                  ## As if </noscript>                  ## As if </noscript>
3614                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3615                  !!!parse-error (type => 'in noscript:'.$token->{tag_name});                  !!!parse-error (type => 'in noscript:'.$token->{tag_name});
# Line 2816  sub _tree_construction_main ($) { Line 3620  sub _tree_construction_main ($) {
3620                                    
3621                  ## Reprocess in the "after head" insertion mode...                  ## Reprocess in the "after head" insertion mode...
3622                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3623                    !!!cp ('t124');
3624                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3625                                    
3626                  ## Reprocess in the "after head" insertion mode...                  ## Reprocess in the "after head" insertion mode...
3627                  } else {
3628                    !!!cp ('t125');
3629                }                }
3630    
3631                ## "after head" insertion mode                ## "after head" insertion mode
3632                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
3633                if ($token->{tag_name} eq 'body') {                if ($token->{tag_name} eq 'body') {
3634                    !!!cp ('t126');
3635                  $self->{insertion_mode} = IN_BODY_IM;                  $self->{insertion_mode} = IN_BODY_IM;
3636                } elsif ($token->{tag_name} eq 'frameset') {                } elsif ($token->{tag_name} eq 'frameset') {
3637                    !!!cp ('t127');
3638                  $self->{insertion_mode} = IN_FRAMESET_IM;                  $self->{insertion_mode} = IN_FRAMESET_IM;
3639                } else {                } else {
3640                  die "$0: tag name: $self->{tag_name}";                  die "$0: tag name: $self->{tag_name}";
# Line 2833  sub _tree_construction_main ($) { Line 3642  sub _tree_construction_main ($) {
3642                !!!next-token;                !!!next-token;
3643                redo B;                redo B;
3644              } else {              } else {
3645                  !!!cp ('t128');
3646                #                #
3647              }              }
3648    
3649              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3650                  !!!cp ('t129');
3651                ## As if </noscript>                ## As if </noscript>
3652                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3653                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});
# Line 2847  sub _tree_construction_main ($) { Line 3658  sub _tree_construction_main ($) {
3658    
3659                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
3660              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3661                  !!!cp ('t130');
3662                ## As if </head>                ## As if </head>
3663                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3664    
3665                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
3666                } else {
3667                  !!!cp ('t131');
3668              }              }
3669    
3670              ## "after head" insertion mode              ## "after head" insertion mode
# Line 2862  sub _tree_construction_main ($) { Line 3676  sub _tree_construction_main ($) {
3676            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
3677              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
3678                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3679                    !!!cp ('t132');
3680                  ## As if <head>                  ## As if <head>
3681                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, 'head');
3682                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
# Line 2873  sub _tree_construction_main ($) { Line 3688  sub _tree_construction_main ($) {
3688                  !!!next-token;                  !!!next-token;
3689                  redo B;                  redo B;
3690                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3691                    !!!cp ('t133');
3692                  ## As if </noscript>                  ## As if </noscript>
3693                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3694                  !!!parse-error (type => 'in noscript:script');                  !!!parse-error (type => 'in noscript:/head');
3695                                    
3696                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3697                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
# Line 2883  sub _tree_construction_main ($) { Line 3699  sub _tree_construction_main ($) {
3699                  !!!next-token;                  !!!next-token;
3700                  redo B;                  redo B;
3701                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3702                    !!!cp ('t134');
3703                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3704                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
3705                  !!!next-token;                  !!!next-token;
3706                  redo B;                  redo B;
3707                } else {                } else {
3708                    !!!cp ('t135');
3709                  #                  #
3710                }                }
3711              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
3712                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3713                    !!!cp ('t136');
3714                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3715                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
3716                  !!!next-token;                  !!!next-token;
3717                  redo B;                  redo B;
3718                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3719                    !!!cp ('t137');
3720                  !!!parse-error (type => 'unmatched end tag:noscript');                  !!!parse-error (type => 'unmatched end tag:noscript');
3721                  ## Ignore the token ## ISSUE: An issue in the spec.                  ## Ignore the token ## ISSUE: An issue in the spec.
3722                  !!!next-token;                  !!!next-token;
3723                  redo B;                  redo B;
3724                } else {                } else {
3725                    !!!cp ('t138');
3726                  #                  #
3727                }                }
3728              } elsif ({              } elsif ({
3729                        body => 1, html => 1,                        body => 1, html => 1,
3730                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3731                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3732                    !!!cp ('t139');
3733                  ## As if <head>                  ## As if <head>
3734                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, 'head');
3735                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
# Line 2916  sub _tree_construction_main ($) { Line 3738  sub _tree_construction_main ($) {
3738                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
3739                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3740                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3741                    !!!cp ('t140');
3742                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3743                  ## Ignore the token                  ## Ignore the token
3744                  !!!next-token;                  !!!next-token;
3745                  redo B;                  redo B;
3746                  } else {
3747                    !!!cp ('t141');
3748                }                }
3749                                
3750                #                #
# Line 2927  sub _tree_construction_main ($) { Line 3752  sub _tree_construction_main ($) {
3752                        p => 1, br => 1,                        p => 1, br => 1,
3753                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3754                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3755                    !!!cp ('t142');
3756                  ## As if <head>                  ## As if <head>
3757                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, 'head');
3758                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
# Line 2934  sub _tree_construction_main ($) { Line 3760  sub _tree_construction_main ($) {
3760    
3761                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
3762                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3763                  } else {
3764                    !!!cp ('t143');
3765                }                }
3766    
3767                #                #
3768              } else {              } else {
3769                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3770                    !!!cp ('t144');
3771                  #                  #
3772                } else {                } else {
3773                    !!!cp ('t145');
3774                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3775                  ## Ignore the token                  ## Ignore the token
3776                  !!!next-token;                  !!!next-token;
# Line 2949  sub _tree_construction_main ($) { Line 3779  sub _tree_construction_main ($) {
3779              }              }
3780    
3781              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3782                  !!!cp ('t146');
3783                ## As if </noscript>                ## As if </noscript>
3784                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3785                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});
# Line 2959  sub _tree_construction_main ($) { Line 3790  sub _tree_construction_main ($) {
3790    
3791                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
3792              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3793                  !!!cp ('t147');
3794                ## As if </head>                ## As if </head>
3795                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3796    
3797                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
3798              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3799    ## ISSUE: This case cannot be reached?
3800                  !!!cp ('t148');
3801                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3802                ## Ignore the token ## ISSUE: An issue in the spec.                ## Ignore the token ## ISSUE: An issue in the spec.
3803                !!!next-token;                !!!next-token;
3804                redo B;                redo B;
3805                } else {
3806                  !!!cp ('t149');
3807              }              }
3808    
3809              ## "after head" insertion mode              ## "after head" insertion mode
# Line 2983  sub _tree_construction_main ($) { Line 3819  sub _tree_construction_main ($) {
3819            ## ISSUE: An issue in the spec.            ## ISSUE: An issue in the spec.
3820      } elsif ($self->{insertion_mode} & BODY_IMS) {      } elsif ($self->{insertion_mode} & BODY_IMS) {
3821            if ($token->{type} == CHARACTER_TOKEN) {            if ($token->{type} == CHARACTER_TOKEN) {
3822                !!!cp ('t150');
3823              ## NOTE: There is a code clone of "character in body".              ## NOTE: There is a code clone of "character in body".
3824              $reconstruct_active_formatting_elements->($insert_to_current);              $reconstruct_active_formatting_elements->($insert_to_current);
3825                            
# Line 3001  sub _tree_construction_main ($) { Line 3838  sub _tree_construction_main ($) {
3838                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3839                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
3840                    if ($node->[1] eq 'td' or $node->[1] eq 'th') {                    if ($node->[1] eq 'td' or $node->[1] eq 'th') {
3841                        !!!cp ('t151');
3842                      $tn = $node->[1];                      $tn = $node->[1];
3843                      last INSCOPE;                      last INSCOPE;
3844                    } elsif ({                    } elsif ({
3845                              table => 1, html => 1,                              table => 1, html => 1,
3846                             }->{$node->[1]}) {                             }->{$node->[1]}) {
3847                        !!!cp ('t152');
3848                      last INSCOPE;                      last INSCOPE;
3849                    }                    }
3850                  } # INSCOPE                  } # INSCOPE
3851                    unless (defined $tn) {                    unless (defined $tn) {
3852                        !!!cp ('t153');
3853    ## TODO: This error type is wrong.
3854                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3855                      ## Ignore the token                      ## Ignore the token
3856                      !!!next-token;                      !!!next-token;
3857                      redo B;                      redo B;
3858                    }                    }
3859                                    
3860                    !!!cp ('t154');
3861                  ## Close the cell                  ## Close the cell
3862                  !!!back-token; # <?>                  !!!back-token; # <?>
3863                  $token = {type => END_TAG_TOKEN, tag_name => $tn};                  $token = {type => END_TAG_TOKEN, tag_name => $tn};
# Line 3029  sub _tree_construction_main ($) { Line 3871  sub _tree_construction_main ($) {
3871                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3872                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
3873                    if ($node->[1] eq 'caption') {                    if ($node->[1] eq 'caption') {
3874                        !!!cp ('t155');
3875                      $i = $_;                      $i = $_;
3876                      last INSCOPE;                      last INSCOPE;
3877                    } elsif ({                    } elsif ({
3878                              table => 1, html => 1,                              table => 1, html => 1,
3879                             }->{$node->[1]}) {                             }->{$node->[1]}) {
3880                        !!!cp ('t156');
3881                      last INSCOPE;                      last INSCOPE;
3882                    }                    }
3883                  } # INSCOPE                  } # INSCOPE
3884                    unless (defined $i) {                    unless (defined $i) {
3885                        !!!cp ('t157');
3886    ## TODO: this type is wrong.
3887                      !!!parse-error (type => 'unmatched end tag:caption');                      !!!parse-error (type => 'unmatched end tag:caption');
3888                      ## Ignore the token                      ## Ignore the token
3889                      !!!next-token;                      !!!next-token;
# Line 3045  sub _tree_construction_main ($) { Line 3891  sub _tree_construction_main ($) {
3891                    }                    }
3892                                    
3893                  ## generate implied end tags                  ## generate implied end tags
3894                  if ({                  while ({
3895                       dd => 1, dt => 1, li => 1, p => 1,                          dd => 1, dt => 1, li => 1, p => 1,
3896                       td => 1, th => 1, tr => 1,                         }->{$self->{open_elements}->[-1]->[1]}) {
3897                       tbody => 1, tfoot=> 1, thead => 1,                    !!!cp ('t158');
3898                      }->{$self->{open_elements}->[-1]->[1]}) {                    pop @{$self->{open_elements}};
                   !!!back-token; # <?>  
                   $token = {type => END_TAG_TOKEN, tag_name => 'caption'};  
                   !!!back-token;  
                   $token = {type => END_TAG_TOKEN,  
                             tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                   redo B;  
3899                  }                  }
3900    
3901                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {
3902                      !!!cp ('t159');
3903                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3904                    } else {
3905                      !!!cp ('t160');
3906                  }                  }
3907                                    
3908                  splice @{$self->{open_elements}}, $i;                  splice @{$self->{open_elements}}, $i;
# Line 3071  sub _tree_construction_main ($) { Line 3914  sub _tree_construction_main ($) {
3914                  ## reprocess                  ## reprocess
3915                  redo B;                  redo B;
3916                } else {                } else {
3917                    !!!cp ('t161');
3918                  #                  #
3919                }                }
3920              } else {              } else {
3921                  !!!cp ('t162');
3922                #                #
3923              }              }
3924            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
# Line 3084  sub _tree_construction_main ($) { Line 3929  sub _tree_construction_main ($) {
3929                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3930                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
3931                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[1] eq $token->{tag_name}) {
3932                        !!!cp ('t163');
3933                      $i = $_;                      $i = $_;
3934                      last INSCOPE;                      last INSCOPE;
3935                    } elsif ({                    } elsif ({
3936                              table => 1, html => 1,                              table => 1, html => 1,
3937                             }->{$node->[1]}) {                             }->{$node->[1]}) {
3938                        !!!cp ('t164');
3939                      last INSCOPE;                      last INSCOPE;
3940                    }                    }
3941                  } # INSCOPE                  } # INSCOPE
3942                    unless (defined $i) {                    unless (defined $i) {
3943                        !!!cp ('t165');
3944                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3945                      ## Ignore the token                      ## Ignore the token
3946                      !!!next-token;                      !!!next-token;
# Line 3100  sub _tree_construction_main ($) { Line 3948  sub _tree_construction_main ($) {
3948                    }                    }
3949                                    
3950                  ## generate implied end tags                  ## generate implied end tags
3951                  if ({                  while ({
3952                       dd => 1, dt => 1, li => 1, p => 1,                          dd => 1, dt => 1, li => 1, p => 1,
3953                       td => ($token->{tag_name} eq 'th'),                         }->{$self->{open_elements}->[-1]->[1]}) {
3954                       th => ($token->{tag_name} eq 'td'),                    !!!cp ('t166');
3955                       tr => 1,                    pop @{$self->{open_elements}};
                      tbody => 1, tfoot=> 1, thead => 1,  
                     }->{$self->{open_elements}->[-1]->[1]}) {  
                   !!!back-token;  
                   $token = {type => END_TAG_TOKEN,  
                             tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                   redo B;  
3956                  }                  }
3957                    
3958                  if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {                  if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
3959                      !!!cp ('t167');
3960                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3961                    } else {
3962                      !!!cp ('t168');
3963                  }                  }
3964                                    
3965                  splice @{$self->{open_elements}}, $i;                  splice @{$self->{open_elements}}, $i;
# Line 3126  sub _tree_construction_main ($) { Line 3971  sub _tree_construction_main ($) {
3971                  !!!next-token;                  !!!next-token;
3972                  redo B;                  redo B;
3973                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
3974                    !!!cp ('t169');
3975                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3976                  ## Ignore the token                  ## Ignore the token
3977                  !!!next-token;                  !!!next-token;
3978                  redo B;                  redo B;
3979                } else {                } else {
3980                    !!!cp ('t170');
3981                  #                  #
3982                }                }
3983              } elsif ($token->{tag_name} eq 'caption') {              } elsif ($token->{tag_name} eq 'caption') {
# Line 3140  sub _tree_construction_main ($) { Line 3987  sub _tree_construction_main ($) {
3987                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3988                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
3989                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[1] eq $token->{tag_name}) {
3990                        !!!cp ('t171');
3991                      $i = $_;                      $i = $_;
3992                      last INSCOPE;                      last INSCOPE;
3993                    } elsif ({                    } elsif ({
3994                              table => 1, html => 1,                              table => 1, html => 1,
3995                             }->{$node->[1]}) {                             }->{$node->[1]}) {
3996                        !!!cp ('t172');
3997                      last INSCOPE;                      last INSCOPE;
3998                    }                    }
3999                  } # INSCOPE                  } # INSCOPE
4000                    unless (defined $i) {                    unless (defined $i) {
4001                        !!!cp ('t173');
4002                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4003                      ## Ignore the token                      ## Ignore the token
4004                      !!!next-token;                      !!!next-token;
# Line 3156  sub _tree_construction_main ($) { Line 4006  sub _tree_construction_main ($) {
4006                    }                    }
4007                                    
4008                  ## generate implied end tags                  ## generate implied end tags
4009                  if ({                  while ({
4010                       dd => 1, dt => 1, li => 1, p => 1,                          dd => 1, dt => 1, li => 1, p => 1,
4011                       td => 1, th => 1, tr => 1,                         }->{$self->{open_elements}->[-1]->[1]}) {
4012                       tbody => 1, tfoot=> 1, thead => 1,                    !!!cp ('t174');
4013                      }->{$self->{open_elements}->[-1]->[1]}) {                    pop @{$self->{open_elements}};
                   !!!back-token;  
                   $token = {type => END_TAG_TOKEN,  
                             tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                   redo B;  
4014                  }                  }
4015                                    
4016                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {
4017                      !!!cp ('t175');
4018                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4019                    } else {
4020                      !!!cp ('t176');
4021                  }                  }
4022                                    
4023                  splice @{$self->{open_elements}}, $i;                  splice @{$self->{open_elements}}, $i;
# Line 3180  sub _tree_construction_main ($) { Line 4029  sub _tree_construction_main ($) {
4029                  !!!next-token;                  !!!next-token;
4030                  redo B;                  redo B;
4031                } elsif ($self->{insertion_mode} == IN_CELL_IM) {                } elsif ($self->{insertion_mode} == IN_CELL_IM) {
4032                    !!!cp ('t177');
4033                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4034                  ## Ignore the token                  ## Ignore the token
4035                  !!!next-token;                  !!!next-token;
4036                  redo B;                  redo B;
4037                } else {                } else {
4038                    !!!cp ('t178');
4039                  #                  #
4040                }                }
4041              } elsif ({              } elsif ({
# Line 3198  sub _tree_construction_main ($) { Line 4049  sub _tree_construction_main ($) {
4049                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4050                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4051                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq $token->{tag_name}) {
4052                      !!!cp ('t179');
4053                    $i = $_;                    $i = $_;
4054                    last INSCOPE;                    last INSCOPE;
4055                  } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {                  } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {
4056                      !!!cp ('t180');
4057                    $tn = $node->[1];                    $tn = $node->[1];
4058                    ## NOTE: There is exactly one |td| or |th| element                    ## NOTE: There is exactly one |td| or |th| element
4059                    ## in scope in the stack of open elements by definition.                    ## in scope in the stack of open elements by definition.
4060                  } elsif ({                  } elsif ({
4061                            table => 1, html => 1,                            table => 1, html => 1,
4062                           }->{$node->[1]}) {                           }->{$node->[1]}) {
4063                      !!!cp ('t181');
4064                    last INSCOPE;                    last INSCOPE;
4065                  }                  }
4066                } # INSCOPE                } # INSCOPE
4067                unless (defined $i) {                unless (defined $i) {
4068                    !!!cp ('t182');
4069                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4070                  ## Ignore the token                  ## Ignore the token
4071                  !!!next-token;                  !!!next-token;
4072                  redo B;                  redo B;
4073                  } else {
4074                    !!!cp ('t183');
4075                }                }
4076    
4077                ## Close the cell                ## Close the cell
# Line 3231  sub _tree_construction_main ($) { Line 4088  sub _tree_construction_main ($) {
4088                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4089                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4090                  if ($node->[1] eq 'caption') {                  if ($node->[1] eq 'caption') {
4091                      !!!cp ('t184');
4092                    $i = $_;                    $i = $_;
4093                    last INSCOPE;                    last INSCOPE;
4094                  } elsif ({                  } elsif ({
4095                            table => 1, html => 1,                            table => 1, html => 1,
4096                           }->{$node->[1]}) {                           }->{$node->[1]}) {
4097                      !!!cp ('t185');
4098                    last INSCOPE;                    last INSCOPE;
4099                  }                  }
4100                } # INSCOPE                } # INSCOPE
4101                unless (defined $i) {                unless (defined $i) {
4102                    !!!cp ('t186');
4103                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!parse-error (type => 'unmatched end tag:caption');
4104                  ## Ignore the token                  ## Ignore the token
4105                  !!!next-token;                  !!!next-token;
# Line 3247  sub _tree_construction_main ($) { Line 4107  sub _tree_construction_main ($) {
4107                }                }
4108                                
4109                ## generate implied end tags                ## generate implied end tags
4110                if ({                while ({
4111                     dd => 1, dt => 1, li => 1, p => 1,                        dd => 1, dt => 1, li => 1, p => 1,
4112                     td => 1, th => 1, tr => 1,                       }->{$self->{open_elements}->[-1]->[1]}) {
4113                     tbody => 1, tfoot=> 1, thead => 1,                  !!!cp ('t187');
4114                    }->{$self->{open_elements}->[-1]->[1]}) {                  pop @{$self->{open_elements}};
                 !!!back-token; # </table>  
                 $token = {type => END_TAG_TOKEN, tag_name => 'caption'};  
                 !!!back-token;  
                 $token = {type => END_TAG_TOKEN,  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
4115                }                }
4116    
4117                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                if ($self->{open_elements}->[-1]->[1] ne 'caption') {
4118                    !!!cp ('t188');
4119                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4120                  } else {
4121                    !!!cp ('t189');
4122                }                }
4123    
4124                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
# Line 3276  sub _tree_construction_main ($) { Line 4133  sub _tree_construction_main ($) {
4133                        body => 1, col => 1, colgroup => 1, html => 1,                        body => 1, col => 1, colgroup => 1, html => 1,
4134                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4135                if ($self->{insertion_mode} & BODY_TABLE_IMS) {                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
4136                    !!!cp ('t190');
4137                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4138                  ## Ignore the token                  ## Ignore the token
4139                  !!!next-token;                  !!!next-token;
4140                  redo B;                  redo B;
4141                } else {                } else {
4142                    !!!cp ('t191');
4143                  #                  #
4144                }                }
4145              } elsif ({              } elsif ({
# Line 3288  sub _tree_construction_main ($) { Line 4147  sub _tree_construction_main ($) {
4147                        thead => 1, tr => 1,                        thead => 1, tr => 1,
4148                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
4149                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
4150                  !!!cp ('t192');
4151                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4152                ## Ignore the token                ## Ignore the token
4153                !!!next-token;                !!!next-token;
4154                redo B;                redo B;
4155              } else {              } else {
4156                  !!!cp ('t193');
4157                #                #
4158              }              }
4159        } else {        } else {
# Line 3302  sub _tree_construction_main ($) { Line 4163  sub _tree_construction_main ($) {
4163        $insert = $insert_to_current;        $insert = $insert_to_current;
4164        #        #
4165      } elsif ($self->{insertion_mode} & TABLE_IMS) {      } elsif ($self->{insertion_mode} & TABLE_IMS) {
4166            if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
4167              ## NOTE: There are "character in table" code clones.          if (not $open_tables->[-1]->[1] and # tainted
4168              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              $token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4169                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4170                                
4171                unless (length $token->{data}) {            unless (length $token->{data}) {
4172                  !!!next-token;              !!!cp ('t194');
4173                  redo B;              !!!next-token;
4174                }              redo B;
4175              }            } else {
4176                !!!cp ('t195');
4177              }
4178            }
4179    
4180              !!!parse-error (type => 'in table:#character');              !!!parse-error (type => 'in table:#character');
4181    
# Line 3333  sub _tree_construction_main ($) { Line 4197  sub _tree_construction_main ($) {
4197                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  if ($self->{open_elements}->[$_]->[1] eq 'table') {
4198                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
4199                    if (defined $parent and $parent->node_type == 1) {                    if (defined $parent and $parent->node_type == 1) {
4200                        !!!cp ('t196');
4201                      $foster_parent_element = $parent;                      $foster_parent_element = $parent;
4202                      $next_sibling = $self->{open_elements}->[$_]->[0];                      $next_sibling = $self->{open_elements}->[$_]->[0];
4203                      $prev_sibling = $next_sibling->previous_sibling;                      $prev_sibling = $next_sibling->previous_sibling;
4204                    } else {                    } else {
4205                        !!!cp ('t197');
4206                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];
4207                      $prev_sibling = $foster_parent_element->last_child;                      $prev_sibling = $foster_parent_element->last_child;
4208                    }                    }
# Line 3348  sub _tree_construction_main ($) { Line 4214  sub _tree_construction_main ($) {
4214                  unless defined $foster_parent_element;                  unless defined $foster_parent_element;
4215                if (defined $prev_sibling and                if (defined $prev_sibling and
4216                    $prev_sibling->node_type == 3) {                    $prev_sibling->node_type == 3) {
4217                    !!!cp ('t198');
4218                  $prev_sibling->manakai_append_text ($token->{data});                  $prev_sibling->manakai_append_text ($token->{data});
4219                } else {                } else {
4220                    !!!cp ('t199');
4221                  $foster_parent_element->insert_before                  $foster_parent_element->insert_before
4222                    ($self->{document}->create_text_node ($token->{data}),                    ($self->{document}->create_text_node ($token->{data}),
4223                     $next_sibling);                     $next_sibling);
4224                }                }
4225              } else {            $open_tables->[-1]->[1] = 1; # tainted
4226                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          } else {
4227              }            !!!cp ('t200');
4228              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4229            }
4230                            
4231              !!!next-token;          !!!next-token;
4232              redo B;          redo B;
4233            } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
4234              if ({              if ({
4235                   tr => ($self->{insertion_mode} != IN_ROW_IM),                   tr => ($self->{insertion_mode} != IN_ROW_IM),
4236                   th => 1, td => 1,                   th => 1, td => 1,
# Line 3369  sub _tree_construction_main ($) { Line 4239  sub _tree_construction_main ($) {
4239                  ## Clear back to table context                  ## Clear back to table context
4240                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while ($self->{open_elements}->[-1]->[1] ne 'table' and
4241                         $self->{open_elements}->[-1]->[1] ne 'html') {                         $self->{open_elements}->[-1]->[1] ne 'html') {
4242                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t201');
4243                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4244                  }                  }
4245                                    
# Line 3380  sub _tree_construction_main ($) { Line 4250  sub _tree_construction_main ($) {
4250    
4251                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
4252                  unless ($token->{tag_name} eq 'tr') {                  unless ($token->{tag_name} eq 'tr') {
4253                      !!!cp ('t202');
4254                    !!!parse-error (type => 'missing start tag:tr');                    !!!parse-error (type => 'missing start tag:tr');
4255                  }                  }
4256                                    
# Line 3387  sub _tree_construction_main ($) { Line 4258  sub _tree_construction_main ($) {
4258                  while (not {                  while (not {
4259                    tbody => 1, tfoot => 1, thead => 1, html => 1,                    tbody => 1, tfoot => 1, thead => 1, html => 1,
4260                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
4261                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t203');
4262                      ## ISSUE: Can this case be reached?
4263                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4264                  }                  }
4265                                    
4266                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
4267                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
4268                      !!!cp ('t204');
4269                    !!!insert-element ($token->{tag_name}, $token->{attributes});                    !!!insert-element ($token->{tag_name}, $token->{attributes});
4270                    !!!next-token;                    !!!next-token;
4271                    redo B;                    redo B;
4272                  } else {                  } else {
4273                      !!!cp ('t205');
4274                    !!!insert-element ('tr');                    !!!insert-element ('tr');
4275                    ## reprocess in the "in row" insertion mode                    ## reprocess in the "in row" insertion mode
4276                  }                  }
4277                  } else {
4278                    !!!cp ('t206');
4279                }                }
4280    
4281                ## Clear back to table row context                ## Clear back to table row context
4282                while (not {                while (not {
4283                  tr => 1, html => 1,                  tr => 1, html => 1,
4284                }->{$self->{open_elements}->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
4285                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t207');
4286                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4287                }                }
4288                                
# Line 3429  sub _tree_construction_main ($) { Line 4305  sub _tree_construction_main ($) {
4305                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4306                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4307                    if ($node->[1] eq 'tr') {                    if ($node->[1] eq 'tr') {
4308                        !!!cp ('t208');
4309                      $i = $_;                      $i = $_;
4310                      last INSCOPE;                      last INSCOPE;
4311                    } elsif ({                    } elsif ({
4312                              table => 1, html => 1,                              html => 1,
4313    
4314                                ## NOTE: This element does not appear here, maybe.
4315                                table => 1,
4316                             }->{$node->[1]}) {                             }->{$node->[1]}) {
4317                        !!!cp ('t209');
4318                      last INSCOPE;                      last INSCOPE;
4319                    }                    }
4320                  } # INSCOPE                  } # INSCOPE
4321                  unless (defined $i) {                  unless (defined $i) {
4322                    !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});                   !!!cp ('t210');
4323    ## TODO: This type is wrong.
4324                     !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});
4325                    ## Ignore the token                    ## Ignore the token
4326                    !!!next-token;                    !!!next-token;
4327                    redo B;                    redo B;
# Line 3448  sub _tree_construction_main ($) { Line 4331  sub _tree_construction_main ($) {
4331                  while (not {                  while (not {
4332                    tr => 1, html => 1,                    tr => 1, html => 1,
4333                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
4334                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t211');
4335                      ## ISSUE: Can this case be reached?
4336                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4337                  }                  }
4338                                    
4339                  pop @{$self->{open_elements}}; # tr                  pop @{$self->{open_elements}}; # tr
4340                  $self->{insertion_mode} = IN_TABLE_BODY_IM;                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
4341                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
4342                      !!!cp ('t212');
4343                    ## reprocess                    ## reprocess
4344                    redo B;                    redo B;
4345                  } else {                  } else {
4346                      !!!cp ('t213');
4347                    ## reprocess in the "in table body" insertion mode...                    ## reprocess in the "in table body" insertion mode...
4348                  }                  }
4349                }                }
# Line 3470  sub _tree_construction_main ($) { Line 4356  sub _tree_construction_main ($) {
4356                    if ({                    if ({
4357                         tbody => 1, thead => 1, tfoot => 1,                         tbody => 1, thead => 1, tfoot => 1,
4358                        }->{$node->[1]}) {                        }->{$node->[1]}) {
4359                        !!!cp ('t214');
4360                      $i = $_;                      $i = $_;
4361                      last INSCOPE;                      last INSCOPE;
4362                    } elsif ({                    } elsif ({
4363                              table => 1, html => 1,                              table => 1, html => 1,
4364                             }->{$node->[1]}) {                             }->{$node->[1]}) {
4365                        !!!cp ('t215');
4366                      last INSCOPE;                      last INSCOPE;
4367                    }                    }
4368                  } # INSCOPE                  } # INSCOPE
4369                  unless (defined $i) {                  unless (defined $i) {
4370                      !!!cp ('t216');
4371    ## TODO: This erorr type ios wrong.
4372                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4373                    ## Ignore the token                    ## Ignore the token
4374                    !!!next-token;                    !!!next-token;
# Line 3489  sub _tree_construction_main ($) { Line 4379  sub _tree_construction_main ($) {
4379                  while (not {                  while (not {
4380                    tbody => 1, tfoot => 1, thead => 1, html => 1,                    tbody => 1, tfoot => 1, thead => 1, html => 1,
4381                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
4382                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t217');
4383                      ## ISSUE: Can this state be reached?
4384                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4385                  }                  }
4386                                    
# Line 3503  sub _tree_construction_main ($) { Line 4394  sub _tree_construction_main ($) {
4394                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4395                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
4396                  ## reprocess in "in table" insertion mode...                  ## reprocess in "in table" insertion mode...
4397                  } else {
4398                    !!!cp ('t218');
4399                }                }
4400    
4401                if ($token->{tag_name} eq 'col') {                if ($token->{tag_name} eq 'col') {
4402                  ## Clear back to table context                  ## Clear back to table context
4403                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while ($self->{open_elements}->[-1]->[1] ne 'table' and
4404                         $self->{open_elements}->[-1]->[1] ne 'html') {                         $self->{open_elements}->[-1]->[1] ne 'html') {
4405                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t219');
4406                      ## ISSUE: Can this state be reached?
4407                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4408                  }                  }
4409                                    
# Line 3525  sub _tree_construction_main ($) { Line 4419  sub _tree_construction_main ($) {
4419                  ## Clear back to table context                  ## Clear back to table context
4420                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while ($self->{open_elements}->[-1]->[1] ne 'table' and
4421                         $self->{open_elements}->[-1]->[1] ne 'html') {                         $self->{open_elements}->[-1]->[1] ne 'html') {
4422                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t220');
4423                      ## ISSUE: Can this state be reached?
4424                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4425                  }                  }
4426                                    
# Line 3546  sub _tree_construction_main ($) { Line 4441  sub _tree_construction_main ($) {
4441                  die "$0: in table: <>: $token->{tag_name}";                  die "$0: in table: <>: $token->{tag_name}";
4442                }                }
4443              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
               ## NOTE: There are code clones for this "table in table"  
4444                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4445    
4446                ## As if </table>                ## As if </table>
# Line 3555  sub _tree_construction_main ($) { Line 4449  sub _tree_construction_main ($) {
4449                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4450                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4451                  if ($node->[1] eq 'table') {                  if ($node->[1] eq 'table') {
4452                      !!!cp ('t221');
4453                    $i = $_;                    $i = $_;
4454                    last INSCOPE;                    last INSCOPE;
4455                  } elsif ({                  } elsif ({
4456                            table => 1, html => 1,                            #table => 1,
4457                              html => 1,
4458                           }->{$node->[1]}) {                           }->{$node->[1]}) {
4459                      !!!cp ('t222');
4460                    last INSCOPE;                    last INSCOPE;
4461                  }                  }
4462                } # INSCOPE                } # INSCOPE
4463                unless (defined $i) {                unless (defined $i) {
4464                    !!!cp ('t223');
4465    ## TODO: The following is wrong, maybe.
4466                  !!!parse-error (type => 'unmatched end tag:table');                  !!!parse-error (type => 'unmatched end tag:table');
4467                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
4468                  !!!next-token;                  !!!next-token;
# Line 3571  sub _tree_construction_main ($) { Line 4470  sub _tree_construction_main ($) {
4470                }                }
4471                                
4472                ## generate implied end tags                ## generate implied end tags
4473                if ({                while ({
4474                     dd => 1, dt => 1, li => 1, p => 1,                        dd => 1, dt => 1, li => 1, p => 1,
4475                     td => 1, th => 1, tr => 1,                       }->{$self->{open_elements}->[-1]->[1]}) {
4476                     tbody => 1, tfoot=> 1, thead => 1,                  !!!cp ('t224');
4477                    }->{$self->{open_elements}->[-1]->[1]}) {                  pop @{$self->{open_elements}};
                 !!!back-token; # <table>  
                 $token = {type => END_TAG_TOKEN, tag_name => 'table'};  
                 !!!back-token;  
                 $token = {type => END_TAG_TOKEN,  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
4478                }                }
4479    
4480                if ($self->{open_elements}->[-1]->[1] ne 'table') {                if ($self->{open_elements}->[-1]->[1] ne 'table') {
4481                    !!!cp ('t225');
4482    ## ISSUE: Can this case be reached?
4483                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4484                  } else {
4485                    !!!cp ('t226');
4486                }                }
4487    
4488                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
4489                  pop @{$open_tables};
4490    
4491                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
4492    
4493                ## reprocess                ## reprocess
4494                redo B;                redo B;
4495            } elsif ($token->{tag_name} eq 'style') {
4496              if (not $open_tables->[-1]->[1]) { # tainted
4497                !!!cp ('t227.8');
4498                ## NOTE: This is a "as if in head" code clone.
4499                $parse_rcdata->(CDATA_CONTENT_MODEL);
4500                redo B;
4501              } else {
4502                !!!cp ('t227.7');
4503                #
4504              }
4505            } elsif ($token->{tag_name} eq 'script') {
4506              if (not $open_tables->[-1]->[1]) { # tainted
4507                !!!cp ('t227.6');
4508                ## NOTE: This is a "as if in head" code clone.
4509                $script_start_tag->();
4510                redo B;
4511              } else {
4512                !!!cp ('t227.5');
4513                #
4514              }
4515            } elsif ($token->{tag_name} eq 'input') {
4516              if (not $open_tables->[-1]->[1]) { # tainted
4517                if ($token->{attributes}->{type}) { ## TODO: case
4518                  my $type = lc $token->{attributes}->{type}->{value};
4519                  if ($type eq 'hidden') {
4520                    !!!cp ('t227.3');
4521                    !!!parse-error (type => 'in table:'.$token->{tag_name});
4522    
4523                    !!!insert-element ($token->{tag_name}, $token->{attributes});
4524    
4525                    ## TODO: form element pointer
4526    
4527                    pop @{$self->{open_elements}};
4528    
4529                    !!!next-token;
4530                    redo B;
4531                  } else {
4532                    !!!cp ('t227.2');
4533                    #
4534                  }
4535              } else {              } else {
4536                  !!!cp ('t227.1');
4537                #                #
4538              }              }
4539            } elsif ($token->{type} == END_TAG_TOKEN) {            } else {
4540                !!!cp ('t227.4');
4541                #
4542              }
4543            } else {
4544              !!!cp ('t227');
4545              #
4546            }
4547    
4548            !!!parse-error (type => 'in table:'.$token->{tag_name});
4549    
4550            $insert = $insert_to_foster;
4551            #
4552          } elsif ($token->{type} == END_TAG_TOKEN) {
4553              if ($token->{tag_name} eq 'tr' and              if ($token->{tag_name} eq 'tr' and
4554                  $self->{insertion_mode} == IN_ROW_IM) {                  $self->{insertion_mode} == IN_ROW_IM) {
4555                ## have an element in table scope                ## have an element in table scope
# Line 3605  sub _tree_construction_main ($) { Line 4557  sub _tree_construction_main ($) {
4557                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4558                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4559                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq $token->{tag_name}) {
4560                      !!!cp ('t228');
4561                    $i = $_;                    $i = $_;
4562                    last INSCOPE;                    last INSCOPE;
4563                  } elsif ({                  } elsif ({
4564                            table => 1, html => 1,                            table => 1, html => 1,
4565                           }->{$node->[1]}) {                           }->{$node->[1]}) {
4566                      !!!cp ('t229');
4567                    last INSCOPE;                    last INSCOPE;
4568                  }                  }
4569                } # INSCOPE                } # INSCOPE
4570                unless (defined $i) {                unless (defined $i) {
4571                    !!!cp ('t230');
4572                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4573                  ## Ignore the token                  ## Ignore the token
4574                  !!!next-token;                  !!!next-token;
4575                  redo B;                  redo B;
4576                  } else {
4577                    !!!cp ('t232');
4578                }                }
4579    
4580                ## Clear back to table row context                ## Clear back to table row context
4581                while (not {                while (not {
4582                  tr => 1, html => 1,                  tr => 1, html => 1,
4583                }->{$self->{open_elements}->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
4584                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t231');
4585    ## ISSUE: Can this state be reached?
4586                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4587                }                }
4588    
# Line 3640  sub _tree_construction_main ($) { Line 4598  sub _tree_construction_main ($) {
4598                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4599                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4600                    if ($node->[1] eq 'tr') {                    if ($node->[1] eq 'tr') {
4601                        !!!cp ('t233');
4602                      $i = $_;                      $i = $_;
4603                      last INSCOPE;                      last INSCOPE;
4604                    } elsif ({                    } elsif ({
4605                              table => 1, html => 1,                              table => 1, html => 1,
4606                             }->{$node->[1]}) {                             }->{$node->[1]}) {
4607                        !!!cp ('t234');
4608                      last INSCOPE;                      last INSCOPE;
4609                    }                    }
4610                  } # INSCOPE                  } # INSCOPE
4611                  unless (defined $i) {                  unless (defined $i) {
4612                      !!!cp ('t235');
4613    ## TODO: The following is wrong.
4614                    !!!parse-error (type => 'unmatched end tag:'.$token->{type});                    !!!parse-error (type => 'unmatched end tag:'.$token->{type});
4615                    ## Ignore the token                    ## Ignore the token
4616                    !!!next-token;                    !!!next-token;
# Line 3659  sub _tree_construction_main ($) { Line 4621  sub _tree_construction_main ($) {
4621                  while (not {                  while (not {
4622                    tr => 1, html => 1,                    tr => 1, html => 1,
4623                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
4624                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t236');
4625    ## ISSUE: Can this state be reached?
4626                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4627                  }                  }
4628                                    
# Line 3676  sub _tree_construction_main ($) { Line 4639  sub _tree_construction_main ($) {
4639                    if ({                    if ({
4640                         tbody => 1, thead => 1, tfoot => 1,                         tbody => 1, thead => 1, tfoot => 1,
4641                        }->{$node->[1]}) {                        }->{$node->[1]}) {
4642                        !!!cp ('t237');
4643                      $i = $_;                      $i = $_;
4644                      last INSCOPE;                      last INSCOPE;
4645                    } elsif ({                    } elsif ({
4646                              table => 1, html => 1,                              table => 1, html => 1,
4647                             }->{$node->[1]}) {                             }->{$node->[1]}) {
4648                        !!!cp ('t238');
4649                      last INSCOPE;                      last INSCOPE;
4650                    }                    }
4651                  } # INSCOPE                  } # INSCOPE
4652                  unless (defined $i) {                  unless (defined $i) {
4653                      !!!cp ('t239');
4654                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4655                    ## Ignore the token                    ## Ignore the token
4656                    !!!next-token;                    !!!next-token;
# Line 3695  sub _tree_construction_main ($) { Line 4661  sub _tree_construction_main ($) {
4661                  while (not {                  while (not {
4662                    tbody => 1, tfoot => 1, thead => 1, html => 1,                    tbody => 1, tfoot => 1, thead => 1, html => 1,
4663                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
4664                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t240');
4665                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4666                  }                  }
4667                                    
# Line 3711  sub _tree_construction_main ($) { Line 4677  sub _tree_construction_main ($) {
4677                  ## reprocess in the "in table" insertion mode...                  ## reprocess in the "in table" insertion mode...
4678                }                }
4679    
4680                  ## NOTE: </table> in the "in table" insertion mode.
4681                  ## When you edit the code fragment below, please ensure that
4682                  ## the code for <table> in the "in table" insertion mode
4683                  ## is synced with it.
4684    
4685                ## have a table element in table scope                ## have a table element in table scope
4686                my $i;                my $i;
4687                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4688                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4689                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq $token->{tag_name}) {
4690                      !!!cp ('t241');
4691                    $i = $_;                    $i = $_;
4692                    last INSCOPE;                    last INSCOPE;
4693                  } elsif ({                  } elsif ({
4694                            table => 1, html => 1,                            table => 1, html => 1,
4695                           }->{$node->[1]}) {                           }->{$node->[1]}) {
4696                      !!!cp ('t242');
4697                    last INSCOPE;                    last INSCOPE;
4698                  }                  }
4699                } # INSCOPE                } # INSCOPE
4700                unless (defined $i) {                unless (defined $i) {
4701                    !!!cp ('t243');
4702                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4703                  ## Ignore the token                  ## Ignore the token
4704                  !!!next-token;                  !!!next-token;
4705                  redo B;                  redo B;
4706                }                }
   
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                    tbody => 1, tfoot=> 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token;  
                 $token = {type => END_TAG_TOKEN,  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
                 
               if ($self->{open_elements}->[-1]->[1] ne 'table') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               }  
4707                                    
4708                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
4709                  pop @{$open_tables};
4710                                
4711                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
4712                                
# Line 3763  sub _tree_construction_main ($) { Line 4722  sub _tree_construction_main ($) {
4722                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4723                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4724                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[1] eq $token->{tag_name}) {
4725                        !!!cp ('t247');
4726                      $i = $_;                      $i = $_;
4727                      last INSCOPE;                      last INSCOPE;
4728                    } elsif ({                    } elsif ({
4729                              table => 1, html => 1,                              table => 1, html => 1,
4730                             }->{$node->[1]}) {                             }->{$node->[1]}) {
4731                        !!!cp ('t248');
4732                      last INSCOPE;                      last INSCOPE;
4733                    }                    }
4734                  } # INSCOPE                  } # INSCOPE
4735                    unless (defined $i) {                    unless (defined $i) {
4736                        !!!cp ('t249');
4737                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4738                      ## Ignore the token                      ## Ignore the token
4739                      !!!next-token;                      !!!next-token;
# Line 3784  sub _tree_construction_main ($) { Line 4746  sub _tree_construction_main ($) {
4746                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4747                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4748                    if ($node->[1] eq 'tr') {                    if ($node->[1] eq 'tr') {
4749                        !!!cp ('t250');
4750                      $i = $_;                      $i = $_;
4751                      last INSCOPE;                      last INSCOPE;
4752                    } elsif ({                    } elsif ({
4753                              table => 1, html => 1,                              table => 1, html => 1,
4754                             }->{$node->[1]}) {                             }->{$node->[1]}) {
4755                        !!!cp ('t251');
4756                      last INSCOPE;                      last INSCOPE;
4757                    }                    }
4758                  } # INSCOPE                  } # INSCOPE
4759                    unless (defined $i) {                    unless (defined $i) {
4760                        !!!cp ('t252');
4761                      !!!parse-error (type => 'unmatched end tag:tr');                      !!!parse-error (type => 'unmatched end tag:tr');
4762                      ## Ignore the token                      ## Ignore the token
4763                      !!!next-token;                      !!!next-token;
# Line 3803  sub _tree_construction_main ($) { Line 4768  sub _tree_construction_main ($) {
4768                  while (not {                  while (not {
4769                    tr => 1, html => 1,                    tr => 1, html => 1,
4770                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
4771                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t253');
4772    ## ISSUE: Can this case be reached?
4773                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4774                  }                  }
4775                                    
# Line 3817  sub _tree_construction_main ($) { Line 4783  sub _tree_construction_main ($) {
4783                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4784                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4785                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq $token->{tag_name}) {
4786                      !!!cp ('t254');
4787                    $i = $_;                    $i = $_;
4788                    last INSCOPE;                    last INSCOPE;
4789                  } elsif ({                  } elsif ({
4790                            table => 1, html => 1,                            table => 1, html => 1,
4791                           }->{$node->[1]}) {                           }->{$node->[1]}) {
4792                      !!!cp ('t255');
4793                    last INSCOPE;                    last INSCOPE;
4794                  }                  }
4795                } # INSCOPE                } # INSCOPE
4796                unless (defined $i) {                unless (defined $i) {
4797                    !!!cp ('t256');
4798                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4799                  ## Ignore the token                  ## Ignore the token
4800                  !!!next-token;                  !!!next-token;
# Line 3836  sub _tree_construction_main ($) { Line 4805  sub _tree_construction_main ($) {
4805                while (not {                while (not {
4806                  tbody => 1, tfoot => 1, thead => 1, html => 1,                  tbody => 1, tfoot => 1, thead => 1, html => 1,
4807                }->{$self->{open_elements}->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
4808                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t257');
4809    ## ISSUE: Can this case be reached?
4810                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4811                }                }
4812    
# Line 3850  sub _tree_construction_main ($) { Line 4820  sub _tree_construction_main ($) {
4820                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM
4821                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
4822                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4823                  !!!cp ('t258');
4824                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4825                ## Ignore the token                ## Ignore the token
4826                !!!next-token;                !!!next-token;
4827                redo B;                redo B;
4828              } else {          } else {
4829                #            !!!cp ('t259');
4830              }            !!!parse-error (type => 'in table:/'.$token->{tag_name});
           } else {  
             die "$0: $token->{type}: Unknown token type";  
           }  
   
       !!!parse-error (type => 'in table:'.$token->{tag_name});  
4831    
4832        $insert = $insert_to_foster;            $insert = $insert_to_foster;
4833        #            #
4834            }
4835          } else {
4836            die "$0: $token->{type}: Unknown token type";
4837          }
4838      } elsif ($self->{insertion_mode} == IN_COLUMN_GROUP_IM) {      } elsif ($self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
4839            if ($token->{type} == CHARACTER_TOKEN) {            if ($token->{type} == CHARACTER_TOKEN) {
4840              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4841                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4842                unless (length $token->{data}) {                unless (length $token->{data}) {
4843                    !!!cp ('t260');
4844                  !!!next-token;                  !!!next-token;
4845                  redo B;                  redo B;
4846                }                }
4847              }              }
4848                            
4849                !!!cp ('t261');
4850              #              #
4851            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
4852              if ($token->{tag_name} eq 'col') {              if ($token->{tag_name} eq 'col') {
4853                  !!!cp ('t262');
4854                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
4855                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4856                !!!next-token;                !!!next-token;
4857                redo B;                redo B;
4858              } else {              } else {
4859                  !!!cp ('t263');
4860                #                #
4861              }              }
4862            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
4863              if ($token->{tag_name} eq 'colgroup') {              if ($token->{tag_name} eq 'colgroup') {
4864                if ($self->{open_elements}->[-1]->[1] eq 'html') {                if ($self->{open_elements}->[-1]->[1] eq 'html') {
4865                    !!!cp ('t264');
4866                  !!!parse-error (type => 'unmatched end tag:colgroup');                  !!!parse-error (type => 'unmatched end tag:colgroup');
4867                  ## Ignore the token                  ## Ignore the token
4868                  !!!next-token;                  !!!next-token;
4869                  redo B;                  redo B;
4870                } else {                } else {
4871                    !!!cp ('t265');
4872                  pop @{$self->{open_elements}}; # colgroup                  pop @{$self->{open_elements}}; # colgroup
4873                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
4874                  !!!next-token;                  !!!next-token;
4875                  redo B;                              redo B;            
4876                }                }
4877              } elsif ($token->{tag_name} eq 'col') {              } elsif ($token->{tag_name} eq 'col') {
4878                  !!!cp ('t266');
4879                !!!parse-error (type => 'unmatched end tag:col');                !!!parse-error (type => 'unmatched end tag:col');
4880                ## Ignore the token                ## Ignore the token
4881                !!!next-token;                !!!next-token;
4882                redo B;                redo B;
4883              } else {              } else {
4884                  !!!cp ('t267');
4885                #                #
4886              }              }
4887            } else {            } else {
4888              #              die "$0: $token->{type}: Unknown token type";
4889            }            }
4890    
4891            ## As if </colgroup>            ## As if </colgroup>
4892            if ($self->{open_elements}->[-1]->[1] eq 'html') {            if ($self->{open_elements}->[-1]->[1] eq 'html') {
4893                !!!cp ('t269');
4894              !!!parse-error (type => 'unmatched end tag:colgroup');              !!!parse-error (type => 'unmatched end tag:colgroup');
4895              ## Ignore the token              ## Ignore the token
4896              !!!next-token;              !!!next-token;
4897              redo B;              redo B;
4898            } else {            } else {
4899                !!!cp ('t270');
4900              pop @{$self->{open_elements}}; # colgroup              pop @{$self->{open_elements}}; # colgroup
4901              $self->{insertion_mode} = IN_TABLE_IM;              $self->{insertion_mode} = IN_TABLE_IM;
4902              ## reprocess              ## reprocess
4903              redo B;              redo B;
4904            }            }
4905      } elsif ($self->{insertion_mode} == IN_SELECT_IM) {      } elsif ($self->{insertion_mode} == IN_SELECT_IM) {
4906            if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
4907              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          !!!cp ('t271');
4908              !!!next-token;          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4909              redo B;          !!!next-token;
4910            } elsif ($token->{type} == START_TAG_TOKEN) {          redo B;
4911          } elsif ($token->{type} == START_TAG_TOKEN) {
4912              if ($token->{tag_name} eq 'option') {              if ($token->{tag_name} eq 'option') {
4913                if ($self->{open_elements}->[-1]->[1] eq 'option') {                if ($self->{open_elements}->[-1]->[1] eq 'option') {
4914                    !!!cp ('t272');
4915                  ## As if </option>                  ## As if </option>
4916                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4917                  } else {
4918                    !!!cp ('t273');
4919                }                }
4920    
4921                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
# Line 3939  sub _tree_construction_main ($) { Line 4923  sub _tree_construction_main ($) {
4923                redo B;                redo B;
4924              } elsif ($token->{tag_name} eq 'optgroup') {              } elsif ($token->{tag_name} eq 'optgroup') {
4925                if ($self->{open_elements}->[-1]->[1] eq 'option') {                if ($self->{open_elements}->[-1]->[1] eq 'option') {
4926                    !!!cp ('t274');
4927                  ## As if </option>                  ## As if </option>
4928                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4929                  } else {
4930                    !!!cp ('t275');
4931                }                }
4932    
4933                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {
4934                    !!!cp ('t276');
4935                  ## As if </optgroup>                  ## As if </optgroup>
4936                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4937                  } else {
4938                    !!!cp ('t277');
4939                }                }
4940    
4941                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
4942                !!!next-token;                !!!next-token;
4943                redo B;                redo B;
4944              } elsif ($token->{tag_name} eq 'select') {              } elsif ($token->{tag_name} eq 'select') {
4945    ## TODO: The type below is not good - <select> is replaced by </select>
4946                !!!parse-error (type => 'not closed:select');                !!!parse-error (type => 'not closed:select');
4947                ## As if </select> instead                ## As if </select> instead
4948                ## have an element in table scope                ## have an element in table scope
# Line 3959  sub _tree_construction_main ($) { Line 4950  sub _tree_construction_main ($) {
4950                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4951                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4952                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq $token->{tag_name}) {
4953                      !!!cp ('t278');
4954                    $i = $_;                    $i = $_;
4955                    last INSCOPE;                    last INSCOPE;
4956                  } elsif ({                  } elsif ({
4957                            table => 1, html => 1,                            table => 1, html => 1,
4958                           }->{$node->[1]}) {                           }->{$node->[1]}) {
4959                      !!!cp ('t279');
4960                    last INSCOPE;                    last INSCOPE;
4961                  }                  }
4962                } # INSCOPE                } # INSCOPE
4963                unless (defined $i) {                unless (defined $i) {
4964                    !!!cp ('t280');
4965                  !!!parse-error (type => 'unmatched end tag:select');                  !!!parse-error (type => 'unmatched end tag:select');
4966                  ## Ignore the token                  ## Ignore the token
4967                  !!!next-token;                  !!!next-token;
4968                  redo B;                  redo B;
4969                }                }
4970                                
4971                  !!!cp ('t281');
4972                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
4973    
4974                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
4975    
4976                !!!next-token;                !!!next-token;
4977                redo B;                redo B;
4978              } else {          } else {
4979                #            !!!cp ('t282');
4980              }            !!!parse-error (type => 'in select:'.$token->{tag_name});
4981            } elsif ($token->{type} == END_TAG_TOKEN) {            ## Ignore the token
4982              !!!next-token;
4983              redo B;
4984            }
4985          } elsif ($token->{type} == END_TAG_TOKEN) {
4986              if ($token->{tag_name} eq 'optgroup') {              if ($token->{tag_name} eq 'optgroup') {
4987                if ($self->{open_elements}->[-1]->[1] eq 'option' and                if ($self->{open_elements}->[-1]->[1] eq 'option' and
4988                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {
4989                    !!!cp ('t283');
4990                  ## As if </option>                  ## As if </option>
4991                  splice @{$self->{open_elements}}, -2;                  splice @{$self->{open_elements}}, -2;
4992                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {
4993                    !!!cp ('t284');
4994                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4995                } else {                } else {
4996                    !!!cp ('t285');
4997                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4998                  ## Ignore the token                  ## Ignore the token
4999                }                }
# Line 3999  sub _tree_construction_main ($) { Line 5001  sub _tree_construction_main ($) {
5001                redo B;                redo B;
5002              } elsif ($token->{tag_name} eq 'option') {              } elsif ($token->{tag_name} eq 'option') {
5003                if ($self->{open_elements}->[-1]->[1] eq 'option') {                if ($self->{open_elements}->[-1]->[1] eq 'option') {
5004                    !!!cp ('t286');
5005                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5006                } else {                } else {
5007                    !!!cp ('t287');
5008                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5009                  ## Ignore the token                  ## Ignore the token
5010                }                }
# Line 4012  sub _tree_construction_main ($) { Line 5016  sub _tree_construction_main ($) {
5016                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5017                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5018                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq $token->{tag_name}) {
5019                      !!!cp ('t288');
5020                    $i = $_;                    $i = $_;
5021                    last INSCOPE;                    last INSCOPE;
5022                  } elsif ({                  } elsif ({
5023                            table => 1, html => 1,                            table => 1, html => 1,
5024                           }->{$node->[1]}) {                           }->{$node->[1]}) {
5025                      !!!cp ('t289');
5026                    last INSCOPE;                    last INSCOPE;
5027                  }                  }
5028                } # INSCOPE                } # INSCOPE
5029                unless (defined $i) {                unless (defined $i) {
5030                    !!!cp ('t290');
5031                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5032                  ## Ignore the token                  ## Ignore the token
5033                  !!!next-token;                  !!!next-token;
5034                  redo B;                  redo B;
5035                }                }
5036                                
5037                  !!!cp ('t291');
5038                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5039    
5040                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
# Line 4037  sub _tree_construction_main ($) { Line 5045  sub _tree_construction_main ($) {
5045                        caption => 1, table => 1, tbody => 1,                        caption => 1, table => 1, tbody => 1,
5046                        tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                        tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
5047                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5048    ## TODO: The following is wrong?
5049                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5050                                
5051                ## have an element in table scope                ## have an element in table scope
# Line 4044  sub _tree_construction_main ($) { Line 5053  sub _tree_construction_main ($) {
5053                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5054                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5055                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq $token->{tag_name}) {
5056                      !!!cp ('t292');
5057                    $i = $_;                    $i = $_;
5058                    last INSCOPE;                    last INSCOPE;
5059                  } elsif ({                  } elsif ({
5060                            table => 1, html => 1,                            table => 1, html => 1,
5061                           }->{$node->[1]}) {                           }->{$node->[1]}) {
5062                      !!!cp ('t293');
5063                    last INSCOPE;                    last INSCOPE;
5064                  }                  }
5065                } # INSCOPE                } # INSCOPE
5066                unless (defined $i) {                unless (defined $i) {
5067                    !!!cp ('t294');
5068                  ## Ignore the token                  ## Ignore the token
5069                  !!!next-token;                  !!!next-token;
5070                  redo B;                  redo B;
# Line 4064  sub _tree_construction_main ($) { Line 5076  sub _tree_construction_main ($) {
5076                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5077                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5078                  if ($node->[1] eq 'select') {                  if ($node->[1] eq 'select') {
5079                      !!!cp ('t295');
5080                    $i = $_;                    $i = $_;
5081                    last INSCOPE;                    last INSCOPE;
5082                  } elsif ({                  } elsif ({
5083                            table => 1, html => 1,                            table => 1, html => 1,
5084                           }->{$node->[1]}) {                           }->{$node->[1]}) {
5085    ## ISSUE: Can this state be reached?
5086                      !!!cp ('t296');
5087                    last INSCOPE;                    last INSCOPE;
5088                  }                  }
5089                } # INSCOPE                } # INSCOPE
5090                unless (defined $i) {                unless (defined $i) {
5091                    !!!cp ('t297');
5092    ## TODO: The following error type is correct?
5093                  !!!parse-error (type => 'unmatched end tag:select');                  !!!parse-error (type => 'unmatched end tag:select');
5094                  ## Ignore the </select> token                  ## Ignore the </select> token
5095                  !!!next-token; ## TODO: ok?                  !!!next-token; ## TODO: ok?
5096                  redo B;                  redo B;
5097                }                }
5098                                
5099                  !!!cp ('t298');
5100                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5101    
5102                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5103    
5104                ## reprocess                ## reprocess
5105                redo B;                redo B;
5106              } else {          } else {
5107                #            !!!cp ('t299');
5108              }            !!!parse-error (type => 'in select:/'.$token->{tag_name});
           } else {  
             #  
           }  
   
           !!!parse-error (type => 'in select:'.$token->{tag_name});  
5109            ## Ignore the token            ## Ignore the token
5110            !!!next-token;            !!!next-token;
5111            redo B;            redo B;
5112            }
5113          } else {
5114            die "$0: $token->{type}: Unknown token type";
5115          }
5116      } elsif ($self->{insertion_mode} & BODY_AFTER_IMS) {      } elsif ($self->{insertion_mode} & BODY_AFTER_IMS) {
5117        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
5118          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
# Line 4106  sub _tree_construction_main ($) { Line 5123  sub _tree_construction_main ($) {
5123            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5124                        
5125            unless (length $token->{data}) {            unless (length $token->{data}) {
5126                !!!cp ('t300');
5127              !!!next-token;              !!!next-token;
5128              redo B;              redo B;
5129            }            }
5130          }          }
5131                    
5132          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
5133              !!!cp ('t301');
5134            !!!parse-error (type => 'after html:#character');            !!!parse-error (type => 'after html:#character');
5135    
5136            ## Reprocess in the "main" phase, "after body" insertion mode...            ## Reprocess in the "after body" insertion mode.
5137            } else {
5138              !!!cp ('t302');
5139          }          }
5140                    
5141          ## "after body" insertion mode          ## "after body" insertion mode
# Line 4125  sub _tree_construction_main ($) { Line 5146  sub _tree_construction_main ($) {
5146          redo B;          redo B;
5147        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5148          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
5149              !!!cp ('t303');
5150            !!!parse-error (type => 'after html:'.$token->{tag_name});            !!!parse-error (type => 'after html:'.$token->{tag_name});
5151                        
5152            ## Reprocess in the "main" phase, "after body" insertion mode...            ## Reprocess in the "after body" insertion mode.
5153            } else {
5154              !!!cp ('t304');
5155          }          }
5156    
5157          ## "after body" insertion mode          ## "after body" insertion mode
# Line 4138  sub _tree_construction_main ($) { Line 5162  sub _tree_construction_main ($) {
5162          redo B;          redo B;
5163        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
5164          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
5165              !!!cp ('t305');
5166            !!!parse-error (type => 'after html:/'.$token->{tag_name});            !!!parse-error (type => 'after html:/'.$token->{tag_name});
5167                        
5168            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
5169            ## Reprocess in the "main" phase, "after body" insertion mode...            ## Reprocess in the "after body" insertion mode.
5170            } else {
5171              !!!cp ('t306');
5172          }          }
5173    
5174          ## "after body" insertion mode          ## "after body" insertion mode
5175          if ($token->{tag_name} eq 'html') {          if ($token->{tag_name} eq 'html') {
5176            if (defined $self->{inner_html_node}) {            if (defined $self->{inner_html_node}) {
5177                !!!cp ('t307');
5178              !!!parse-error (type => 'unmatched end tag:html');              !!!parse-error (type => 'unmatched end tag:html');
5179              ## Ignore the token              ## Ignore the token
5180              !!!next-token;              !!!next-token;
5181              redo B;              redo B;
5182            } else {            } else {
5183                !!!cp ('t308');
5184              $self->{insertion_mode} = AFTER_HTML_BODY_IM;              $self->{insertion_mode} = AFTER_HTML_BODY_IM;
5185              !!!next-token;              !!!next-token;
5186              redo B;              redo B;
5187            }            }
5188          } else {          } else {
5189              !!!cp ('t309');
5190            !!!parse-error (type => 'after body:/'.$token->{tag_name});            !!!parse-error (type => 'after body:/'.$token->{tag_name});
5191    
5192            $self->{insertion_mode} = IN_BODY_IM;            $self->{insertion_mode} = IN_BODY_IM;
# Line 4172  sub _tree_construction_main ($) { Line 5202  sub _tree_construction_main ($) {
5202            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5203                        
5204            unless (length $token->{data}) {            unless (length $token->{data}) {
5205                !!!cp ('t310');
5206              !!!next-token;              !!!next-token;
5207              redo B;              redo B;
5208            }            }
# Line 4179  sub _tree_construction_main ($) { Line 5210  sub _tree_construction_main ($) {
5210                    
5211          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
5212            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
5213                !!!cp ('t311');
5214              !!!parse-error (type => 'in frameset:#character');              !!!parse-error (type => 'in frameset:#character');
5215            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
5216                !!!cp ('t312');
5217              !!!parse-error (type => 'after frameset:#character');              !!!parse-error (type => 'after frameset:#character');
5218            } else { # "after html frameset"            } else { # "after html frameset"
5219                !!!cp ('t313');
5220              !!!parse-error (type => 'after html:#character');              !!!parse-error (type => 'after html:#character');
5221    
5222              $self->{insertion_mode} = AFTER_FRAMESET_IM;              $self->{insertion_mode} = AFTER_FRAMESET_IM;
5223              ## Reprocess in the "main" phase, "after frameset"...              ## Reprocess in the "after frameset" insertion mode.
5224              !!!parse-error (type => 'after frameset:#character');              !!!parse-error (type => 'after frameset:#character');
5225            }            }
5226                        
5227            ## Ignore the token.            ## Ignore the token.
5228            if (length $token->{data}) {            if (length $token->{data}) {
5229                !!!cp ('t314');
5230              ## reprocess the rest of characters              ## reprocess the rest of characters
5231            } else {            } else {
5232                !!!cp ('t315');
5233              !!!next-token;              !!!next-token;
5234            }            }
5235            redo B;            redo B;
# Line 4202  sub _tree_construction_main ($) { Line 5238  sub _tree_construction_main ($) {
5238          die qq[$0: Character "$token->{data}"];          die qq[$0: Character "$token->{data}"];
5239        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5240          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
5241              !!!cp ('t316');
5242            !!!parse-error (type => 'after html:'.$token->{tag_name});            !!!parse-error (type => 'after html:'.$token->{tag_name});
5243    
5244            $self->{insertion_mode} = AFTER_FRAMESET_IM;            $self->{insertion_mode} = AFTER_FRAMESET_IM;
5245            ## Process in the "main" phase, "after frameset" insertion mode...            ## Process in the "after frameset" insertion mode.
5246          }          } else {
5247              !!!cp ('t317');
5248            }
5249    
5250          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
5251              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
5252              !!!cp ('t318');
5253            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes});
5254            !!!next-token;            !!!next-token;
5255            redo B;            redo B;
5256          } elsif ($token->{tag_name} eq 'frame' and          } elsif ($token->{tag_name} eq 'frame' and
5257                   $self->{insertion_mode} == IN_FRAMESET_IM) {                   $self->{insertion_mode} == IN_FRAMESET_IM) {
5258              !!!cp ('t319');
5259            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes});
5260            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
5261            !!!next-token;            !!!next-token;
5262            redo B;            redo B;
5263          } elsif ($token->{tag_name} eq 'noframes') {          } elsif ($token->{tag_name} eq 'noframes') {
5264              !!!cp ('t320');
5265            ## NOTE: As if in body.            ## NOTE: As if in body.
5266            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);            $parse_rcdata->(CDATA_CONTENT_MODEL);
5267            redo B;            redo B;
5268          } else {          } else {
5269            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
5270                !!!cp ('t321');
5271              !!!parse-error (type => 'in frameset:'.$token->{tag_name});              !!!parse-error (type => 'in frameset:'.$token->{tag_name});
5272            } else {            } else {
5273                !!!cp ('t322');
5274              !!!parse-error (type => 'after frameset:'.$token->{tag_name});              !!!parse-error (type => 'after frameset:'.$token->{tag_name});
5275            }            }
5276            ## Ignore the token            ## Ignore the token
# Line 4235  sub _tree_construction_main ($) { Line 5279  sub _tree_construction_main ($) {
5279          }          }
5280        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
5281          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
5282              !!!cp ('t323');
5283            !!!parse-error (type => 'after html:/'.$token->{tag_name});            !!!parse-error (type => 'after html:/'.$token->{tag_name});
5284    
5285            $self->{insertion_mode} = AFTER_FRAMESET_IM;            $self->{insertion_mode} = AFTER_FRAMESET_IM;
5286            ## Process in the "main" phase, "after frameset" insertion mode...            ## Process in the "after frameset" insertion mode.
5287            } else {
5288              !!!cp ('t324');
5289          }          }
5290    
5291          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
5292              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
5293            if ($self->{open_elements}->[-1]->[1] eq 'html' and            if ($self->{open_elements}->[-1]->[1] eq 'html' and
5294                @{$self->{open_elements}} == 1) {                @{$self->{open_elements}} == 1) {
5295                !!!cp ('t325');
5296              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5297              ## Ignore the token              ## Ignore the token
5298              !!!next-token;              !!!next-token;
5299            } else {            } else {
5300                !!!cp ('t326');
5301              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
5302              !!!next-token;              !!!next-token;
5303            }            }
5304    
5305            if (not defined $self->{inner_html_node} and            if (not defined $self->{inner_html_node} and
5306                $self->{open_elements}->[-1]->[1] ne 'frameset') {                $self->{open_elements}->[-1]->[1] ne 'frameset') {
5307                !!!cp ('t327');
5308              $self->{insertion_mode} = AFTER_FRAMESET_IM;              $self->{insertion_mode} = AFTER_FRAMESET_IM;
5309              } else {
5310                !!!cp ('t328');
5311            }            }
5312            redo B;            redo B;
5313          } elsif ($token->{tag_name} eq 'html' and          } elsif ($token->{tag_name} eq 'html' and
5314                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {
5315              !!!cp ('t329');
5316            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
5317            !!!next-token;            !!!next-token;
5318            redo B;            redo B;
5319          } else {          } else {
5320            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
5321                !!!cp ('t330');
5322              !!!parse-error (type => 'in frameset:/'.$token->{tag_name});              !!!parse-error (type => 'in frameset:/'.$token->{tag_name});
5323            } else {            } else {
5324                !!!cp ('t331');
5325              !!!parse-error (type => 'after frameset:/'.$token->{tag_name});              !!!parse-error (type => 'after frameset:/'.$token->{tag_name});
5326            }            }
5327            ## Ignore the token            ## Ignore the token
# Line 4285  sub _tree_construction_main ($) { Line 5340  sub _tree_construction_main ($) {
5340      ## "in body" insertion mode      ## "in body" insertion mode
5341      if ($token->{type} == START_TAG_TOKEN) {      if ($token->{type} == START_TAG_TOKEN) {
5342        if ($token->{tag_name} eq 'script') {        if ($token->{tag_name} eq 'script') {
5343            !!!cp ('t332');
5344          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
5345          $script_start_tag->($insert);          $script_start_tag->();
5346          redo B;          redo B;
5347        } elsif ($token->{tag_name} eq 'style') {        } elsif ($token->{tag_name} eq 'style') {
5348            !!!cp ('t333');
5349          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
5350          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);          $parse_rcdata->(CDATA_CONTENT_MODEL);
5351          redo B;          redo B;
5352        } elsif ({        } elsif ({
5353                  base => 1, link => 1,                  base => 1, link => 1,
5354                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
5355            !!!cp ('t334');
5356          ## NOTE: This is an "as if in head" code clone, only "-t" differs          ## NOTE: This is an "as if in head" code clone, only "-t" differs
5357          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5358          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
# Line 4303  sub _tree_construction_main ($) { Line 5361  sub _tree_construction_main ($) {
5361        } elsif ($token->{tag_name} eq 'meta') {        } elsif ($token->{tag_name} eq 'meta') {
5362          ## NOTE: This is an "as if in head" code clone, only "-t" differs          ## NOTE: This is an "as if in head" code clone, only "-t" differs
5363          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5364          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.          my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
5365    
5366          unless ($self->{confident}) {          unless ($self->{confident}) {
           my $charset;  
5367            if ($token->{attributes}->{charset}) { ## TODO: And if supported            if ($token->{attributes}->{charset}) { ## TODO: And if supported
5368              $charset = $token->{attributes}->{charset}->{value};              !!!cp ('t335');
5369            }              $self->{change_encoding}
5370            if ($token->{attributes}->{'http-equiv'}) {                  ->($self, $token->{attributes}->{charset}->{value});
5371                
5372                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
5373                    ->set_user_data (manakai_has_reference =>
5374                                         $token->{attributes}->{charset}
5375                                             ->{has_reference});
5376              } elsif ($token->{attributes}->{content}) {
5377              ## 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.
5378              if ($token->{attributes}->{'http-equiv'}->{value}              if ($token->{attributes}->{content}->{value}
5379                  =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=                  =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
5380                        [\x09-\x0D\x20]*=
5381                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
5382                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
5383                $charset = defined $1 ? $1 : defined $2 ? $2 : $3;                !!!cp ('t336');
5384              } ## TODO: And if supported                $self->{change_encoding}
5385                      ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);
5386                  $meta_el->[0]->get_attribute_node_ns (undef, 'content')
5387                      ->set_user_data (manakai_has_reference =>
5388                                           $token->{attributes}->{content}
5389                                                 ->{has_reference});
5390                }
5391              }
5392            } else {
5393              if ($token->{attributes}->{charset}) {
5394                !!!cp ('t337');
5395                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
5396                    ->set_user_data (manakai_has_reference =>
5397                                         $token->{attributes}->{charset}
5398                                             ->{has_reference});
5399              }
5400              if ($token->{attributes}->{content}) {
5401                !!!cp ('t338');
5402                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
5403                    ->set_user_data (manakai_has_reference =>
5404                                         $token->{attributes}->{content}
5405                                             ->{has_reference});
5406            }            }
           ## TODO: Change the encoding  
5407          }          }
5408    
5409          !!!next-token;          !!!next-token;
5410          redo B;          redo B;
5411        } elsif ($token->{tag_name} eq 'title') {        } elsif ($token->{tag_name} eq 'title') {
5412          !!!parse-error (type => 'in body:title');          !!!cp ('t341');
5413          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
5414          $parse_rcdata->(RCDATA_CONTENT_MODEL, sub {          $parse_rcdata->(RCDATA_CONTENT_MODEL);
           if (defined $self->{head_element}) {  
             $self->{head_element}->append_child ($_[0]);  
           } else {  
             $insert->($_[0]);  
           }  
         });  
5415          redo B;          redo B;
5416        } elsif ($token->{tag_name} eq 'body') {        } elsif ($token->{tag_name} eq 'body') {
5417          !!!parse-error (type => 'in body:body');          !!!parse-error (type => 'in body:body');
5418                                
5419          if (@{$self->{open_elements}} == 1 or          if (@{$self->{open_elements}} == 1 or
5420              $self->{open_elements}->[1]->[1] ne 'body') {              $self->{open_elements}->[1]->[1] ne 'body') {
5421              !!!cp ('t342');
5422            ## Ignore the token            ## Ignore the token
5423          } else {          } else {
5424            my $body_el = $self->{open_elements}->[1]->[0];            my $body_el = $self->{open_elements}->[1]->[0];
5425            for my $attr_name (keys %{$token->{attributes}}) {            for my $attr_name (keys %{$token->{attributes}}) {
5426              unless ($body_el->has_attribute_ns (undef, $attr_name)) {              unless ($body_el->has_attribute_ns (undef, $attr_name)) {
5427                  !!!cp ('t343');
5428                $body_el->set_attribute_ns                $body_el->set_attribute_ns
5429                  (undef, [undef, $attr_name],                  (undef, [undef, $attr_name],
5430                   $token->{attributes}->{$attr_name}->{value});                   $token->{attributes}->{$attr_name}->{value});
# Line 4355  sub _tree_construction_main ($) { Line 5435  sub _tree_construction_main ($) {
5435          redo B;          redo B;
5436        } elsif ({        } elsif ({
5437                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
5438                  div => 1, dl => 1, fieldset => 1, listing => 1,                  div => 1, dl => 1, fieldset => 1,
5439                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
5440                  menu => 1, ol => 1, p => 1, ul => 1,                  menu => 1, ol => 1, p => 1, ul => 1,
5441                  pre => 1,                  pre => 1, listing => 1,
5442                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
5443          ## has a p element in scope          ## has a p element in scope
5444          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
5445            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
5446                !!!cp ('t344');
5447              !!!back-token;              !!!back-token;
5448              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
5449              redo B;              redo B;
# Line 4369  sub _tree_construction_main ($) { Line 5451  sub _tree_construction_main ($) {
5451                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
5452                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
5453                     }->{$_->[1]}) {                     }->{$_->[1]}) {
5454                !!!cp ('t345');
5455              last INSCOPE;              last INSCOPE;
5456            }            }
5457          } # INSCOPE          } # INSCOPE
5458                        
5459          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5460          if ($token->{tag_name} eq 'pre') {          if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {
5461            !!!next-token;            !!!next-token;
5462            if ($token->{type} == CHARACTER_TOKEN) {            if ($token->{type} == CHARACTER_TOKEN) {
5463              $token->{data} =~ s/^\x0A//;              $token->{data} =~ s/^\x0A//;
5464              unless (length $token->{data}) {              unless (length $token->{data}) {
5465                  !!!cp ('t346');
5466                !!!next-token;                !!!next-token;
5467                } else {
5468                  !!!cp ('t349');
5469              }              }
5470              } else {
5471                !!!cp ('t348');
5472            }            }
5473          } else {          } else {
5474              !!!cp ('t347');
5475            !!!next-token;            !!!next-token;
5476          }          }
5477          redo B;          redo B;
5478        } elsif ($token->{tag_name} eq 'form') {        } elsif ($token->{tag_name} eq 'form') {
5479          if (defined $self->{form_element}) {          if (defined $self->{form_element}) {
5480              !!!cp ('t350');
5481            !!!parse-error (type => 'in form:form');            !!!parse-error (type => 'in form:form');
5482            ## Ignore the token            ## Ignore the token
5483            !!!next-token;            !!!next-token;
# Line 4396  sub _tree_construction_main ($) { Line 5486  sub _tree_construction_main ($) {
5486            ## has a p element in scope            ## has a p element in scope
5487            INSCOPE: for (reverse @{$self->{open_elements}}) {            INSCOPE: for (reverse @{$self->{open_elements}}) {
5488              if ($_->[1] eq 'p') {              if ($_->[1] eq 'p') {
5489                  !!!cp ('t351');
5490                !!!back-token;                !!!back-token;
5491                $token = {type => END_TAG_TOKEN, tag_name => 'p'};                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
5492                redo B;                redo B;
# Line 4403  sub _tree_construction_main ($) { Line 5494  sub _tree_construction_main ($) {
5494                        table => 1, caption => 1, td => 1, th => 1,                        table => 1, caption => 1, td => 1, th => 1,
5495                        button => 1, marquee => 1, object => 1, html => 1,                        button => 1, marquee => 1, object => 1, html => 1,
5496                       }->{$_->[1]}) {                       }->{$_->[1]}) {
5497                  !!!cp ('t352');
5498                last INSCOPE;                last INSCOPE;
5499              }              }
5500            } # INSCOPE            } # INSCOPE
# Line 4416  sub _tree_construction_main ($) { Line 5508  sub _tree_construction_main ($) {
5508          ## has a p element in scope          ## has a p element in scope
5509          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
5510            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
5511                !!!cp ('t353');
5512              !!!back-token;              !!!back-token;
5513              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
5514              redo B;              redo B;
# Line 4423  sub _tree_construction_main ($) { Line 5516  sub _tree_construction_main ($) {
5516                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
5517                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
5518                     }->{$_->[1]}) {                     }->{$_->[1]}) {
5519                !!!cp ('t354');
5520              last INSCOPE;              last INSCOPE;
5521            }            }
5522          } # INSCOPE          } # INSCOPE
# Line 4434  sub _tree_construction_main ($) { Line 5528  sub _tree_construction_main ($) {
5528            ## Step 2            ## Step 2
5529            if ($node->[1] eq 'li') {            if ($node->[1] eq 'li') {
5530              if ($i != -1) {              if ($i != -1) {
5531                  !!!cp ('t355');
5532                !!!parse-error (type => 'end tag missing:'.                !!!parse-error (type => 'end tag missing:'.
5533                                $self->{open_elements}->[-1]->[1]);                                $self->{open_elements}->[-1]->[1]);
5534                } else {
5535                  !!!cp ('t356');
5536              }              }
5537              splice @{$self->{open_elements}}, $i;              splice @{$self->{open_elements}}, $i;
5538              last LI;              last LI;
5539              } else {
5540                !!!cp ('t357');
5541            }            }
5542                        
5543            ## Step 3            ## Step 3
# Line 4447  sub _tree_construction_main ($) { Line 5546  sub _tree_construction_main ($) {
5546                ($special_category->{$node->[1]} or                ($special_category->{$node->[1]} or
5547                 $scoping_category->{$node->[1]}) and                 $scoping_category->{$node->[1]}) and
5548                $node->[1] ne 'address' and $node->[1] ne 'div') {                $node->[1] ne 'address' and $node->[1] ne 'div') {
5549                !!!cp ('t358');
5550              last LI;              last LI;
5551            }            }
5552                        
5553              !!!cp ('t359');
5554            ## Step 4            ## Step 4
5555            $i--;            $i--;
5556            $node = $self->{open_elements}->[$i];            $node = $self->{open_elements}->[$i];
# Line 4463  sub _tree_construction_main ($) { Line 5564  sub _tree_construction_main ($) {
5564          ## has a p element in scope          ## has a p element in scope
5565          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
5566            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
5567                !!!cp ('t360');
5568              !!!back-token;              !!!back-token;
5569              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
5570              redo B;              redo B;
# Line 4470  sub _tree_construction_main ($) { Line 5572  sub _tree_construction_main ($) {
5572                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
5573                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
5574                     }->{$_->[1]}) {                     }->{$_->[1]}) {
5575                !!!cp ('t361');
5576              last INSCOPE;              last INSCOPE;
5577            }            }
5578          } # INSCOPE          } # INSCOPE
# Line 4481  sub _tree_construction_main ($) { Line 5584  sub _tree_construction_main ($) {
5584            ## Step 2            ## Step 2
5585            if ($node->[1] eq 'dt' or $node->[1] eq 'dd') {            if ($node->[1] eq 'dt' or $node->[1] eq 'dd') {
5586              if ($i != -1) {              if ($i != -1) {
5587                  !!!cp ('t362');
5588                !!!parse-error (type => 'end tag missing:'.                !!!parse-error (type => 'end tag missing:'.
5589                                $self->{open_elements}->[-1]->[1]);                                $self->{open_elements}->[-1]->[1]);
5590                } else {
5591                  !!!cp ('t363');
5592              }              }
5593              splice @{$self->{open_elements}}, $i;              splice @{$self->{open_elements}}, $i;
5594              last LI;              last LI;
5595              } else {
5596                !!!cp ('t364');
5597            }            }
5598                        
5599            ## Step 3            ## Step 3
# Line 4494  sub _tree_construction_main ($) { Line 5602  sub _tree_construction_main ($) {
5602                ($special_category->{$node->[1]} or                ($special_category->{$node->[1]} or
5603                 $scoping_category->{$node->[1]}) and                 $scoping_category->{$node->[1]}) and
5604                $node->[1] ne 'address' and $node->[1] ne 'div') {                $node->[1] ne 'address' and $node->[1] ne 'div') {
5605                !!!cp ('t365');
5606              last LI;              last LI;
5607            }            }
5608                        
5609              !!!cp ('t366');
5610            ## Step 4            ## Step 4
5611            $i--;            $i--;
5612            $node = $self->{open_elements}->[$i];            $node = $self->{open_elements}->[$i];
# Line 4510  sub _tree_construction_main ($) { Line 5620  sub _tree_construction_main ($) {
5620          ## has a p element in scope          ## has a p element in scope
5621          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
5622            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
5623                !!!cp ('t367');
5624              !!!back-token;              !!!back-token;
5625              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
5626              redo B;              redo B;
# Line 4517  sub _tree_construction_main ($) { Line 5628  sub _tree_construction_main ($) {
5628                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
5629                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
5630                     }->{$_->[1]}) {                     }->{$_->[1]}) {
5631                !!!cp ('t368');
5632              last INSCOPE;              last INSCOPE;
5633            }            }
5634          } # INSCOPE          } # INSCOPE
# Line 4527  sub _tree_construction_main ($) { Line 5639  sub _tree_construction_main ($) {
5639                        
5640          !!!next-token;          !!!next-token;
5641          redo B;          redo B;
       } elsif ({  
                 h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
                }->{$token->{tag_name}}) {  
         ## has a p element in scope  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => END_TAG_TOKEN, tag_name => 'p'};  
             redo B;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## NOTE: See <http://html5.org/tools/web-apps-tracker?from=925&to=926>  
         ## has an element in scope  
         #my $i;  
         #INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
         #  my $node = $self->{open_elements}->[$_];  
         #  if ({  
         #       h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
         #      }->{$node->[1]}) {  
         #    $i = $_;  
         #    last INSCOPE;  
         #  } elsif ({  
         #            table => 1, caption => 1, td => 1, th => 1,  
         #            button => 1, marquee => 1, object => 1, html => 1,  
         #           }->{$node->[1]}) {  
         #    last INSCOPE;  
         #  }  
         #} # INSCOPE  
         #    
         #if (defined $i) {  
         #  !!! parse-error (type => 'in hn:hn');  
         #  splice @{$self->{open_elements}}, $i;  
         #}  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         !!!next-token;  
         redo B;  
5642        } elsif ($token->{tag_name} eq 'a') {        } elsif ($token->{tag_name} eq 'a') {
5643          AFE: for my $i (reverse 0..$#$active_formatting_elements) {          AFE: for my $i (reverse 0..$#$active_formatting_elements) {
5644            my $node = $active_formatting_elements->[$i];            my $node = $active_formatting_elements->[$i];
5645            if ($node->[1] eq 'a') {            if ($node->[1] eq 'a') {
5646                !!!cp ('t371');
5647              !!!parse-error (type => 'in a:a');              !!!parse-error (type => 'in a:a');
5648                            
5649              !!!back-token;              !!!back-token;
# Line 4584  sub _tree_construction_main ($) { Line 5652  sub _tree_construction_main ($) {
5652                            
5653              AFE2: for (reverse 0..$#$active_formatting_elements) {              AFE2: for (reverse 0..$#$active_formatting_elements) {
5654                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
5655                    !!!cp ('t372');
5656                  splice @$active_formatting_elements, $_, 1;                  splice @$active_formatting_elements, $_, 1;
5657                  last AFE2;                  last AFE2;
5658                }                }
5659              } # AFE2              } # AFE2
5660              OE: for (reverse 0..$#{$self->{open_elements}}) {              OE: for (reverse 0..$#{$self->{open_elements}}) {
5661                if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {                if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {
5662                    !!!cp ('t373');
5663                  splice @{$self->{open_elements}}, $_, 1;                  splice @{$self->{open_elements}}, $_, 1;
5664                  last OE;                  last OE;
5665                }                }
5666              } # OE              } # OE
5667              last AFE;              last AFE;
5668            } elsif ($node->[0] eq '#marker') {            } elsif ($node->[0] eq '#marker') {
5669                !!!cp ('t374');
5670              last AFE;              last AFE;
5671            }            }
5672          } # AFE          } # AFE
# Line 4612  sub _tree_construction_main ($) { Line 5683  sub _tree_construction_main ($) {
5683                  s => 1, small => 1, strile => 1,                  s => 1, small => 1, strile => 1,
5684                  strong => 1, tt => 1, u => 1,                  strong => 1, tt => 1, u => 1,
5685                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
5686            !!!cp ('t375');
5687          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
5688                    
5689          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
# Line 4626  sub _tree_construction_main ($) { Line 5698  sub _tree_construction_main ($) {
5698          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5699            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
5700            if ($node->[1] eq 'nobr') {            if ($node->[1] eq 'nobr') {
5701              !!!parse-error (type => 'not closed:nobr');              !!!cp ('t376');
5702                !!!parse-error (type => 'in nobr:nobr');
5703              !!!back-token;              !!!back-token;
5704              $token = {type => END_TAG_TOKEN, tag_name => 'nobr'};              $token = {type => END_TAG_TOKEN, tag_name => 'nobr'};
5705              redo B;              redo B;
# Line 4634  sub _tree_construction_main ($) { Line 5707  sub _tree_construction_main ($) {
5707                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
5708                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
5709                     }->{$node->[1]}) {                     }->{$node->[1]}) {
5710                !!!cp ('t377');
5711              last INSCOPE;              last INSCOPE;
5712            }            }
5713          } # INSCOPE          } # INSCOPE
# Line 4648  sub _tree_construction_main ($) { Line 5722  sub _tree_construction_main ($) {
5722          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5723            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
5724            if ($node->[1] eq 'button') {            if ($node->[1] eq 'button') {
5725                !!!cp ('t378');
5726              !!!parse-error (type => 'in button:button');              !!!parse-error (type => 'in button:button');
5727              !!!back-token;              !!!back-token;
5728              $token = {type => END_TAG_TOKEN, tag_name => 'button'};              $token = {type => END_TAG_TOKEN, tag_name => 'button'};
# Line 4656  sub _tree_construction_main ($) { Line 5731  sub _tree_construction_main ($) {
5731                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
5732                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
5733                     }->{$node->[1]}) {                     }->{$node->[1]}) {
5734                !!!cp ('t379');
5735              last INSCOPE;              last INSCOPE;
5736            }            }
5737          } # INSCOPE          } # INSCOPE
# Line 4663  sub _tree_construction_main ($) { Line 5739  sub _tree_construction_main ($) {
5739          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
5740                        
5741          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5742    
5743            ## TODO: associate with $self->{form_element} if defined
5744    
5745          push @$active_formatting_elements, ['#marker', ''];          push @$active_formatting_elements, ['#marker', ''];
5746    
5747          !!!next-token;          !!!next-token;
5748          redo B;          redo B;
5749        } elsif ($token->{tag_name} eq 'marquee' or        } elsif ($token->{tag_name} eq 'marquee' or
5750                 $token->{tag_name} eq 'object') {                 $token->{tag_name} eq 'object') {
5751            !!!cp ('t380');
5752          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
5753                    
5754          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
# Line 4677  sub _tree_construction_main ($) { Line 5757  sub _tree_construction_main ($) {
5757          !!!next-token;          !!!next-token;
5758          redo B;          redo B;
5759        } elsif ($token->{tag_name} eq 'xmp') {        } elsif ($token->{tag_name} eq 'xmp') {
5760            !!!cp ('t381');
5761          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
5762          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);          $parse_rcdata->(CDATA_CONTENT_MODEL);
5763          redo B;          redo B;
5764        } elsif ($token->{tag_name} eq 'table') {        } elsif ($token->{tag_name} eq 'table') {
5765          ## has a p element in scope          ## has a p element in scope
5766          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
5767            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
5768                !!!cp ('t382');
5769              !!!back-token;              !!!back-token;
5770              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
5771              redo B;              redo B;
# Line 4691  sub _tree_construction_main ($) { Line 5773  sub _tree_construction_main ($) {
5773                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
5774                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
5775                     }->{$_->[1]}) {                     }->{$_->[1]}) {
5776                !!!cp ('t383');
5777              last INSCOPE;              last INSCOPE;
5778            }            }
5779          } # INSCOPE          } # INSCOPE
5780                        
5781          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5782                      push @{$open_tables}, [$self->{open_elements}->[-1]->[0]];
5783    
5784          $self->{insertion_mode} = IN_TABLE_IM;          $self->{insertion_mode} = IN_TABLE_IM;
5785                        
5786          !!!next-token;          !!!next-token;
# Line 4707  sub _tree_construction_main ($) { Line 5791  sub _tree_construction_main ($) {
5791                  image => 1,                  image => 1,
5792                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
5793          if ($token->{tag_name} eq 'image') {          if ($token->{tag_name} eq 'image') {
5794              !!!cp ('t384');
5795            !!!parse-error (type => 'image');            !!!parse-error (type => 'image');
5796            $token->{tag_name} = 'img';            $token->{tag_name} = 'img';
5797            } else {
5798              !!!cp ('t385');
5799          }          }
5800    
5801          ## NOTE: There is an "as if <br>" code clone.          ## NOTE: There is an "as if <br>" code clone.
# Line 4723  sub _tree_construction_main ($) { Line 5810  sub _tree_construction_main ($) {
5810          ## has a p element in scope          ## has a p element in scope
5811          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
5812            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
5813                !!!cp ('t386');
5814              !!!back-token;              !!!back-token;
5815              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
5816              redo B;              redo B;
# Line 4730  sub _tree_construction_main ($) { Line 5818  sub _tree_construction_main ($) {
5818                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
5819                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
5820                     }->{$_->[1]}) {                     }->{$_->[1]}) {
5821                !!!cp ('t387');
5822              last INSCOPE;              last INSCOPE;
5823            }            }
5824          } # INSCOPE          } # INSCOPE
# Line 4740  sub _tree_construction_main ($) { Line 5829  sub _tree_construction_main ($) {
5829          !!!next-token;          !!!next-token;
5830          redo B;          redo B;
5831        } elsif ($token->{tag_name} eq 'input') {        } elsif ($token->{tag_name} eq 'input') {
5832            !!!cp ('t388');
5833          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
5834                    
5835          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
# Line 4752  sub _tree_construction_main ($) { Line 5842  sub _tree_construction_main ($) {
5842          !!!parse-error (type => 'isindex');          !!!parse-error (type => 'isindex');
5843                    
5844          if (defined $self->{form_element}) {          if (defined $self->{form_element}) {
5845              !!!cp ('t389');
5846            ## Ignore the token            ## Ignore the token
5847            !!!next-token;            !!!next-token;
5848            redo B;            redo B;
# Line 4771  sub _tree_construction_main ($) { Line 5862  sub _tree_construction_main ($) {
5862                          {type => START_TAG_TOKEN, tag_name => 'label'},                          {type => START_TAG_TOKEN, tag_name => 'label'},
5863                         );                         );
5864            if ($prompt_attr) {            if ($prompt_attr) {
5865                !!!cp ('t390');
5866              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value}};              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value}};
5867            } else {            } else {
5868                !!!cp ('t391');
5869              push @tokens, {type => CHARACTER_TOKEN,              push @tokens, {type => CHARACTER_TOKEN,
5870                             data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD                             data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD
5871              ## TODO: make this configurable              ## TODO: make this configurable
# Line 4804  sub _tree_construction_main ($) { Line 5897  sub _tree_construction_main ($) {
5897          if ($token->{type} == CHARACTER_TOKEN) {          if ($token->{type} == CHARACTER_TOKEN) {
5898            $token->{data} =~ s/^\x0A//;            $token->{data} =~ s/^\x0A//;
5899            unless (length $token->{data}) {            unless (length $token->{data}) {
5900                !!!cp ('t392');
5901              !!!next-token;              !!!next-token;
5902              } else {
5903                !!!cp ('t393');
5904            }            }
5905            } else {
5906              !!!cp ('t394');
5907          }          }
5908          while ($token->{type} == CHARACTER_TOKEN) {          while ($token->{type} == CHARACTER_TOKEN) {
5909              !!!cp ('t395');
5910            $text .= $token->{data};            $text .= $token->{data};
5911            !!!next-token;            !!!next-token;
5912          }          }
5913          if (length $text) {          if (length $text) {
5914              !!!cp ('t396');
5915            $el->manakai_append_text ($text);            $el->manakai_append_text ($text);
5916          }          }
5917                    
# Line 4819  sub _tree_construction_main ($) { Line 5919  sub _tree_construction_main ($) {
5919                    
5920          if ($token->{type} == END_TAG_TOKEN and          if ($token->{type} == END_TAG_TOKEN and
5921              $token->{tag_name} eq $tag_name) {              $token->{tag_name} eq $tag_name) {
5922              !!!cp ('t397');
5923            ## Ignore the token            ## Ignore the token
5924          } else {          } else {
5925              !!!cp ('t398');
5926            !!!parse-error (type => 'in RCDATA:#'.$token->{type});            !!!parse-error (type => 'in RCDATA:#'.$token->{type});
5927          }          }
5928          !!!next-token;          !!!next-token;
# Line 4831  sub _tree_construction_main ($) { Line 5933  sub _tree_construction_main ($) {
5933                  noframes => 1,                  noframes => 1,
5934                  noscript => 0, ## TODO: 1 if scripting is enabled                  noscript => 0, ## TODO: 1 if scripting is enabled
5935                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
5936          ## NOTE: There are two "as if in body" code clones.          !!!cp ('t399');
5937          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);          ## NOTE: There is an "as if in body" code clone.
5938            $parse_rcdata->(CDATA_CONTENT_MODEL);
5939          redo B;          redo B;
5940        } elsif ($token->{tag_name} eq 'select') {        } elsif ($token->{tag_name} eq 'select') {
5941            !!!cp ('t400');
5942          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
5943                    
5944          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5945    
5946            ## TODO: associate with $self->{form_element} if defined
5947                    
5948          $self->{insertion_mode} = IN_SELECT_IM;          $self->{insertion_mode} = IN_SELECT_IM;
5949          !!!next-token;          !!!next-token;
# Line 4848  sub _tree_construction_main ($) { Line 5954  sub _tree_construction_main ($) {
5954                  tbody => 1, td => 1, tfoot => 1, th => 1,                  tbody => 1, td => 1, tfoot => 1, th => 1,
5955                  thead => 1, tr => 1,                  thead => 1, tr => 1,
5956                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
5957            !!!cp ('t401');
5958          !!!parse-error (type => 'in body:'.$token->{tag_name});          !!!parse-error (type => 'in body:'.$token->{tag_name});
5959          ## Ignore the token          ## Ignore the token
5960          !!!next-token;          !!!next-token;
# Line 4855  sub _tree_construction_main ($) { Line 5962  sub _tree_construction_main ($) {
5962                    
5963          ## ISSUE: An issue on HTML5 new elements in the spec.          ## ISSUE: An issue on HTML5 new elements in the spec.
5964        } else {        } else {
5965            !!!cp ('t402');
5966          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
5967                    
5968          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
# Line 4872  sub _tree_construction_main ($) { Line 5980  sub _tree_construction_main ($) {
5980                         th => 1, tr => 1, body => 1, html => 1,                         th => 1, tr => 1, body => 1, html => 1,
5981                       tbody => 1, tfoot => 1, thead => 1,                       tbody => 1, tfoot => 1, thead => 1,
5982                      }->{$_->[1]}) {                      }->{$_->[1]}) {
5983                  !!!cp ('t403');
5984                !!!parse-error (type => 'not closed:'.$_->[1]);                !!!parse-error (type => 'not closed:'.$_->[1]);
5985                } else {
5986                  !!!cp ('t404');
5987              }              }
5988            }            }
5989    
# Line 4880  sub _tree_construction_main ($) { Line 5991  sub _tree_construction_main ($) {
5991            !!!next-token;            !!!next-token;
5992            redo B;            redo B;
5993          } else {          } else {
5994              !!!cp ('t405');
5995            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5996            ## Ignore the token            ## Ignore the token
5997            !!!next-token;            !!!next-token;
# Line 4889  sub _tree_construction_main ($) { Line 6001  sub _tree_construction_main ($) {
6001          if (@{$self->{open_elements}} > 1 and $self->{open_elements}->[1]->[1] eq 'body') {          if (@{$self->{open_elements}} > 1 and $self->{open_elements}->[1]->[1] eq 'body') {
6002            ## ISSUE: There is an issue in the spec.            ## ISSUE: There is an issue in the spec.
6003            if ($self->{open_elements}->[-1]->[1] ne 'body') {            if ($self->{open_elements}->[-1]->[1] ne 'body') {
6004                !!!cp ('t406');
6005              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);
6006              } else {
6007                !!!cp ('t407');
6008            }            }
6009            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
6010            ## reprocess            ## reprocess
6011            redo B;            redo B;
6012          } else {          } else {
6013              !!!cp ('t408');
6014            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
6015            ## Ignore the token            ## Ignore the token
6016            !!!next-token;            !!!next-token;
# Line 4904  sub _tree_construction_main ($) { Line 6020  sub _tree_construction_main ($) {
6020                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
6021                  div => 1, dl => 1, fieldset => 1, listing => 1,                  div => 1, dl => 1, fieldset => 1, listing => 1,
6022                  menu => 1, ol => 1, pre => 1, ul => 1,                  menu => 1, ol => 1, pre => 1, ul => 1,
                 p => 1,  
6023                  dd => 1, dt => 1, li => 1,                  dd => 1, dt => 1, li => 1,
6024                  button => 1, marquee => 1, object => 1,                  button => 1, marquee => 1, object => 1,
6025                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
# Line 4913  sub _tree_construction_main ($) { Line 6028  sub _tree_construction_main ($) {
6028          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6029            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6030            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] eq $token->{tag_name}) {
6031              ## generate implied end tags              !!!cp ('t410');
             if ({  
                  dd => ($token->{tag_name} ne 'dd'),  
                  dt => ($token->{tag_name} ne 'dt'),  
                  li => ($token->{tag_name} ne 'li'),  
                  p => ($token->{tag_name} ne 'p'),  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot=> 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => END_TAG_TOKEN,  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               redo B;  
             }  
6032              $i = $_;              $i = $_;
6033              last INSCOPE unless $token->{tag_name} eq 'p';              last INSCOPE;
6034            } elsif ({            } elsif ({
6035                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
6036                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
6037                     }->{$node->[1]}) {                     }->{$node->[1]}) {
6038                !!!cp ('t411');
6039              last INSCOPE;              last INSCOPE;
6040            }            }
6041          } # INSCOPE          } # INSCOPE
6042            
6043          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          unless (defined $i) { # has an element in scope
6044            if (defined $i) {            !!!cp ('t413');
6045              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
6046            } else {
6047              ## Step 1. generate implied end tags
6048              while ({
6049                      dd => ($token->{tag_name} ne 'dd'),
6050                      dt => ($token->{tag_name} ne 'dt'),
6051                      li => ($token->{tag_name} ne 'li'),
6052                      p => 1,
6053                     }->{$self->{open_elements}->[-1]->[1]}) {
6054                !!!cp ('t409');
6055                pop @{$self->{open_elements}};
6056              }
6057    
6058              ## Step 2.
6059              if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
6060                !!!cp ('t412');
6061              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
6062            } else {            } else {
6063              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t414');
6064            }            }
6065          }  
6066                      ## Step 3.
         if (defined $i) {  
6067            splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
6068          } elsif ($token->{tag_name} eq 'p') {  
6069            ## As if <p>, then reprocess the current token            ## Step 4.
6070            my $el;            $clear_up_to_marker->()
6071            !!!create-element ($el, 'p');                if {
6072            $insert->($el);                  button => 1, marquee => 1, object => 1,
6073                  }->{$token->{tag_name}};
6074          }          }
         $clear_up_to_marker->()  
           if {  
             button => 1, marquee => 1, object => 1,  
           }->{$token->{tag_name}};  
6075          !!!next-token;          !!!next-token;
6076          redo B;          redo B;
6077        } elsif ($token->{tag_name} eq 'form') {        } elsif ($token->{tag_name} eq 'form') {
6078            undef $self->{form_element};
6079    
6080          ## has an element in scope          ## has an element in scope
6081            my $i;
6082          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6083            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6084            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] eq $token->{tag_name}) {
6085              ## generate implied end tags              !!!cp ('t418');
6086              if ({              $i = $_;
                  dd => 1, dt => 1, li => 1, p => 1,  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot=> 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => END_TAG_TOKEN,  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               redo B;  
             }  
6087              last INSCOPE;              last INSCOPE;
6088            } elsif ({            } elsif ({
6089                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
6090                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
6091                     }->{$node->[1]}) {                     }->{$node->[1]}) {
6092                !!!cp ('t419');
6093              last INSCOPE;              last INSCOPE;
6094            }            }
6095          } # INSCOPE          } # INSCOPE
6096            
6097          if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {          unless (defined $i) { # has an element in scope
6098            pop @{$self->{open_elements}};            !!!cp ('t421');
6099              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
6100          } else {          } else {
6101            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            ## Step 1. generate implied end tags
6102              while ({
6103                      dd => 1, dt => 1, li => 1, p => 1,
6104                     }->{$self->{open_elements}->[-1]->[1]}) {
6105                !!!cp ('t417');
6106                pop @{$self->{open_elements}};
6107              }
6108              
6109              ## Step 2.
6110              if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
6111                !!!cp ('t417.1');
6112                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
6113              } else {
6114                !!!cp ('t420');
6115              }  
6116              
6117              ## Step 3.
6118              splice @{$self->{open_elements}}, $i;
6119          }          }
6120    
         undef $self->{form_element};  
6121          !!!next-token;          !!!next-token;
6122          redo B;          redo B;
6123        } elsif ({        } elsif ({
# Line 5003  sub _tree_construction_main ($) { Line 6130  sub _tree_construction_main ($) {
6130            if ({            if ({
6131                 h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                 h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
6132                }->{$node->[1]}) {                }->{$node->[1]}) {
6133              ## generate implied end tags              !!!cp ('t423');
             if ({  
                  dd => 1, dt => 1, li => 1, p => 1,  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot=> 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => END_TAG_TOKEN,  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               redo B;  
             }  
6134              $i = $_;              $i = $_;
6135              last INSCOPE;              last INSCOPE;
6136            } elsif ({            } elsif ({
6137                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
6138                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
6139                     }->{$node->[1]}) {                     }->{$node->[1]}) {
6140                !!!cp ('t424');
6141              last INSCOPE;              last INSCOPE;
6142            }            }
6143          } # INSCOPE          } # INSCOPE
6144            
6145          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          unless (defined $i) { # has an element in scope
6146            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            !!!cp ('t425.1');
6147              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
6148            } else {
6149              ## Step 1. generate implied end tags
6150              while ({
6151                      dd => 1, dt => 1, li => 1, p => 1,
6152                     }->{$self->{open_elements}->[-1]->[1]}) {
6153                !!!cp ('t422');
6154                pop @{$self->{open_elements}};
6155              }
6156              
6157              ## Step 2.
6158              if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
6159                !!!cp ('t425');
6160                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
6161              } else {
6162                !!!cp ('t426');
6163              }
6164    
6165              ## Step 3.
6166              splice @{$self->{open_elements}}, $i;
6167          }          }
6168                    
6169          splice @{$self->{open_elements}}, $i if defined $i;          !!!next-token;
6170            redo B;
6171          } elsif ($token->{tag_name} eq 'p') {
6172            ## has an element in scope
6173            my $i;
6174            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6175              my $node = $self->{open_elements}->[$_];
6176              if ($node->[1] eq $token->{tag_name}) {
6177                !!!cp ('t410.1');
6178                $i = $_;
6179                last INSCOPE;
6180              } elsif ({
6181                        table => 1, caption => 1, td => 1, th => 1,
6182                        button => 1, marquee => 1, object => 1, html => 1,
6183                       }->{$node->[1]}) {
6184                !!!cp ('t411.1');
6185                last INSCOPE;
6186              }
6187            } # INSCOPE
6188    
6189            if (defined $i) {
6190              if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
6191                !!!cp ('t412.1');
6192                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
6193              } else {
6194                !!!cp ('t414.1');
6195              }
6196    
6197              splice @{$self->{open_elements}}, $i;
6198            } else {
6199              !!!cp ('t413.1');
6200              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
6201    
6202              !!!cp ('t415.1');
6203              ## As if <p>, then reprocess the current token
6204              my $el;
6205              !!!create-element ($el, 'p');
6206              $insert->($el);
6207              ## NOTE: Not inserted into |$self->{open_elements}|.
6208            }
6209    
6210          !!!next-token;          !!!next-token;
6211          redo B;          redo B;
6212        } elsif ({        } elsif ({
# Line 5037  sub _tree_construction_main ($) { Line 6215  sub _tree_construction_main ($) {
6215                  nobr => 1, s => 1, small => 1, strile => 1,                  nobr => 1, s => 1, small => 1, strile => 1,
6216                  strong => 1, tt => 1, u => 1,                  strong => 1, tt => 1, u => 1,
6217                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6218            !!!cp ('t427');
6219          $formatting_end_tag->($token->{tag_name});          $formatting_end_tag->($token->{tag_name});
6220          redo B;          redo B;
6221        } elsif ($token->{tag_name} eq 'br') {        } elsif ($token->{tag_name} eq 'br') {
6222            !!!cp ('t428');
6223          !!!parse-error (type => 'unmatched end tag:br');          !!!parse-error (type => 'unmatched end tag:br');
6224    
6225          ## As if <br>          ## As if <br>
# Line 5064  sub _tree_construction_main ($) { Line 6244  sub _tree_construction_main ($) {
6244                  table => 1, textarea => 1, wbr => 1,                  table => 1, textarea => 1, wbr => 1,
6245                  noscript => 0, ## TODO: if scripting is enabled                  noscript => 0, ## TODO: if scripting is enabled
6246                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6247            !!!cp ('t429');
6248          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
6249          ## Ignore the token          ## Ignore the token
6250          !!!next-token;          !!!next-token;
# Line 5081  sub _tree_construction_main ($) { Line 6262  sub _tree_construction_main ($) {
6262            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] eq $token->{tag_name}) {
6263              ## Step 1              ## Step 1
6264              ## generate implied end tags              ## generate implied end tags
6265              if ({              while ({
6266                   dd => 1, dt => 1, li => 1, p => 1,                      dd => 1, dt => 1, li => 1, p => 1,
6267                   td => 1, th => 1, tr => 1,                     }->{$self->{open_elements}->[-1]->[1]}) {
6268                   tbody => 1, tfoot => 1, thead => 1,                !!!cp ('t430');
6269                  }->{$self->{open_elements}->[-1]->[1]}) {                ## ISSUE: Can this case be reached?
6270                !!!back-token;                pop @{$self->{open_elements}};
               $token = {type => END_TAG_TOKEN,  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               redo B;  
6271              }              }
6272                    
6273              ## Step 2              ## Step 2
6274              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {
6275                  !!!cp ('t431');
6276                  ## NOTE: <x><y></x>
6277                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
6278                } else {
6279                  !!!cp ('t432');
6280              }              }
6281                            
6282              ## Step 3              ## Step 3
# Line 5108  sub _tree_construction_main ($) { Line 6290  sub _tree_construction_main ($) {
6290                  #not $phrasing_category->{$node->[1]} and                  #not $phrasing_category->{$node->[1]} and
6291                  ($special_category->{$node->[1]} or                  ($special_category->{$node->[1]} or
6292                   $scoping_category->{$node->[1]})) {                   $scoping_category->{$node->[1]})) {
6293                  !!!cp ('t433');
6294                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
6295                ## Ignore the token                ## Ignore the token
6296                !!!next-token;                !!!next-token;
6297                last S2;                last S2;
6298              }              }
6299    
6300                !!!cp ('t434');
6301            }            }
6302                        
6303            ## Step 4            ## Step 4
# Line 5128  sub _tree_construction_main ($) { Line 6313  sub _tree_construction_main ($) {
6313      redo B;      redo B;
6314    } # B    } # B
6315    
   ## NOTE: The "trailing end" phase in HTML5 is split into  
   ## two insertion modes: "after html body" and "after html frameset".  
   ## NOTE: States in the main stage is preserved while  
   ## the parser stays in the trailing end phase. # MUST  
   
6316    ## Stop parsing # MUST    ## Stop parsing # MUST
6317        
6318    ## TODO: script stuffs    ## TODO: script stuffs
# Line 5144  sub set_inner_html ($$$) { Line 6324  sub set_inner_html ($$$) {
6324    my $s = \$_[0];    my $s = \$_[0];
6325    my $onerror = $_[1];    my $onerror = $_[1];
6326    
6327      ## ISSUE: Should {confident} be true?
6328    
6329    my $nt = $node->node_type;    my $nt = $node->node_type;
6330    if ($nt == 9) {    if ($nt == 9) {
6331      # MUST      # MUST
# Line 5172  sub set_inner_html ($$$) { Line 6354  sub set_inner_html ($$$) {
6354      my $p = $class->new;      my $p = $class->new;
6355      $p->{document} = $doc;      $p->{document} = $doc;
6356    
6357      ## Step 9 # MUST      ## Step 8 # MUST
6358      my $i = 0;      my $i = 0;
6359      my $line = 1;      my $line = 1;
6360      my $column = 0;      my $column = 0;
6361      $p->{set_next_input_character} = sub {      $p->{set_next_char} = sub {
6362        my $self = shift;        my $self = shift;
6363    
6364        pop @{$self->{prev_input_character}};        pop @{$self->{prev_char}};
6365        unshift @{$self->{prev_input_character}}, $self->{next_input_character};        unshift @{$self->{prev_char}}, $self->{next_char};
6366    
6367        $self->{next_input_character} = -1 and return if $i >= length $$s;        $self->{next_char} = -1 and return if $i >= length $$s;
6368        $self->{next_input_character} = ord substr $$s, $i++, 1;        $self->{next_char} = ord substr $$s, $i++, 1;
6369        $column++;        $column++;
6370    
6371        if ($self->{next_input_character} == 0x000A) { # LF        if ($self->{next_char} == 0x000A) { # LF
6372          $line++;          $line++;
6373          $column = 0;          $column = 0;
6374        } elsif ($self->{next_input_character} == 0x000D) { # CR          !!!cp ('i1');
6375          } elsif ($self->{next_char} == 0x000D) { # CR
6376          $i++ if substr ($$s, $i, 1) eq "\x0A";          $i++ if substr ($$s, $i, 1) eq "\x0A";
6377          $self->{next_input_character} = 0x000A; # LF # MUST          $self->{next_char} = 0x000A; # LF # MUST
6378          $line++;          $line++;
6379          $column = 0;          $column = 0;
6380        } elsif ($self->{next_input_character} > 0x10FFFF) {          !!!cp ('i2');
6381          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        } elsif ($self->{next_char} > 0x10FFFF) {
6382        } elsif ($self->{next_input_character} == 0x0000) { # NULL          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
6383            !!!cp ('i3');
6384          } elsif ($self->{next_char} == 0x0000) { # NULL
6385            !!!cp ('i4');
6386          !!!parse-error (type => 'NULL');          !!!parse-error (type => 'NULL');
6387          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
6388        }        }
6389      };      };
6390      $p->{prev_input_character} = [-1, -1, -1];      $p->{prev_char} = [-1, -1, -1];
6391      $p->{next_input_character} = -1;      $p->{next_char} = -1;
6392            
6393      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
6394        my (%opt) = @_;        my (%opt) = @_;
# Line 5216  sub set_inner_html ($$$) { Line 6402  sub set_inner_html ($$$) {
6402      $p->_initialize_tree_constructor;      $p->_initialize_tree_constructor;
6403    
6404      ## Step 2      ## Step 2
6405      my $node_ln = $node->local_name;      my $node_ln = $node->manakai_local_name;
6406      $p->{content_model} = {      $p->{content_model} = {
6407        title => RCDATA_CONTENT_MODEL,        title => RCDATA_CONTENT_MODEL,
6408        textarea => RCDATA_CONTENT_MODEL,        textarea => RCDATA_CONTENT_MODEL,
# Line 5235  sub set_inner_html ($$$) { Line 6421  sub set_inner_html ($$$) {
6421    
6422      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $node_ln];
6423    
6424      ## Step 4      ## Step 3
6425      my $root = $doc->create_element_ns      my $root = $doc->create_element_ns
6426        ('http://www.w3.org/1999/xhtml', [undef, 'html']);        ('http://www.w3.org/1999/xhtml', [undef, 'html']);
6427    
6428      ## Step 5 # MUST      ## Step 4 # MUST
6429      $doc->append_child ($root);      $doc->append_child ($root);
6430    
6431      ## Step 6 # MUST      ## Step 5 # MUST
6432      push @{$p->{open_elements}}, [$root, 'html'];      push @{$p->{open_elements}}, [$root, 'html'];
6433    
6434      undef $p->{head_element};      undef $p->{head_element};
6435    
6436      ## Step 7 # MUST      ## Step 6 # MUST
6437      $p->_reset_insertion_mode;      $p->_reset_insertion_mode;
6438    
6439      ## Step 8 # MUST      ## Step 7 # MUST
6440      my $anode = $node;      my $anode = $node;
6441      AN: while (defined $anode) {      AN: while (defined $anode) {
6442        if ($anode->node_type == 1) {        if ($anode->node_type == 1) {
6443          my $nsuri = $anode->namespace_uri;          my $nsuri = $anode->namespace_uri;
6444          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {
6445            if ($anode->local_name eq 'form') { ## TODO: case?            if ($anode->manakai_local_name eq 'form') {
6446                !!!cp ('i5');
6447              $p->{form_element} = $anode;              $p->{form_element} = $anode;
6448              last AN;              last AN;
6449            }            }
# Line 5265  sub set_inner_html ($$$) { Line 6452  sub set_inner_html ($$$) {
6452        $anode = $anode->parent_node;        $anode = $anode->parent_node;
6453      } # AN      } # AN
6454            
6455      ## Step 3 # MUST      ## Step 9 # MUST
     ## Step 10 # MUST  
6456      {      {
6457        my $self = $p;        my $self = $p;
6458        !!!next-token;        !!!next-token;
6459      }      }
6460      $p->_tree_construction_main;      $p->_tree_construction_main;
6461    
6462      ## Step 11 # MUST      ## Step 10 # MUST
6463      my @cn = @{$node->child_nodes};      my @cn = @{$node->child_nodes};
6464      for (@cn) {      for (@cn) {
6465        $node->remove_child ($_);        $node->remove_child ($_);
6466      }      }
6467      ## ISSUE: mutation events? read-only?      ## ISSUE: mutation events? read-only?
6468    
6469      ## Step 12 # MUST      ## Step 11 # MUST
6470      @cn = @{$root->child_nodes};      @cn = @{$root->child_nodes};
6471      for (@cn) {      for (@cn) {
6472        $this_doc->adopt_node ($_);        $this_doc->adopt_node ($_);
# Line 5296  sub set_inner_html ($$$) { Line 6482  sub set_inner_html ($$$) {
6482    
6483  } # tree construction stage  } # tree construction stage
6484    
6485  sub get_inner_html ($$$) {  package Whatpm::HTML::RestartParser;
6486    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  
6487    
6488  1;  1;
6489  # $Date$  # $Date$

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

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24