/[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.20 by wakaba, Sat Jun 23 14:25:05 2007 UTC revision 1.69 by wakaba, Sun Feb 17 12:39:32 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
12    ## strip BOM and the HTML layer MUST ignore it.  Whether we can do it
13    ## is not yet clear.
14    ## "{U+FEFF}..." in UTF-16BE/UTF-16LE is three or four characters?
15    ## "{U+FEFF}..." in GB18030?
16    
17  my $permitted_slash_tag_name = {  my $permitted_slash_tag_name = {
18    base => 1,    base => 1,
19    link => 1,    link => 1,
# Line 78  my $formatting_category = { Line 85  my $formatting_category = {
85  };  };
86  # $phrasing_category: all other elements  # $phrasing_category: all other elements
87    
88    sub parse_byte_string ($$$$;$) {
89      my $self = ref $_[0] ? shift : shift->new;
90      my $charset = shift;
91      my $bytes_s = ref $_[0] ? $_[0] : \($_[0]);
92      my $s;
93      
94      if (defined $charset) {
95        require Encode; ## TODO: decode(utf8) don't delete BOM
96        $s = \ (Encode::decode ($charset, $$bytes_s));
97        $self->{input_encoding} = lc $charset; ## TODO: normalize name
98        $self->{confident} = 1;
99      } else {
100        ## TODO: Implement HTML5 detection algorithm
101        require Whatpm::Charset::UniversalCharDet;
102        $charset = Whatpm::Charset::UniversalCharDet->detect_byte_string
103            (substr ($$bytes_s, 0, 1024));
104        $charset ||= 'windows-1252';
105        $s = \ (Encode::decode ($charset, $$bytes_s));
106        $self->{input_encoding} = $charset;
107        $self->{confident} = 0;
108      }
109    
110      $self->{change_encoding} = sub {
111        my $self = shift;
112        my $charset = lc shift;
113        ## TODO: if $charset is supported
114        ## TODO: normalize charset name
115    
116        ## "Change the encoding" algorithm:
117    
118        ## Step 1    
119        if ($charset eq 'utf-16') { ## ISSUE: UTF-16BE -> UTF-8? UTF-16LE -> UTF-8?
120          $charset = 'utf-8';
121        }
122    
123        ## Step 2
124        if (defined $self->{input_encoding} and
125            $self->{input_encoding} eq $charset) {
126          $self->{confident} = 1;
127          return;
128        }
129    
130        !!!parse-error (type => 'charset label detected:'.$self->{input_encoding}.
131            ':'.$charset, level => 'w');
132    
133        ## Step 3
134        # if (can) {
135          ## change the encoding on the fly.
136          #$self->{confident} = 1;
137          #return;
138        # }
139    
140        ## Step 4
141        throw Whatpm::HTML::RestartParser (charset => $charset);
142      }; # $self->{change_encoding}
143    
144      my @args = @_; shift @args; # $s
145      my $return;
146      try {
147        $return = $self->parse_char_string ($s, @args);  
148      } catch Whatpm::HTML::RestartParser with {
149        my $charset = shift->{charset};
150        $s = \ (Encode::decode ($charset, $$bytes_s));    
151        $self->{input_encoding} = $charset; ## TODO: normalize
152        $self->{confident} = 1;
153        $return = $self->parse_char_string ($s, @args);
154      };
155      return $return;
156    } # parse_byte_string
157    
158    *parse_char_string = \&parse_string;
159    
160  sub parse_string ($$$;$) {  sub parse_string ($$$;$) {
161    my $self = shift->new;    my $self = ref $_[0] ? shift : shift->new;
162    my $s = \$_[0];    my $s = ref $_[0] ? $_[0] : \($_[0]);
163    $self->{document} = $_[1];    $self->{document} = $_[1];
164      @{$self->{document}->child_nodes} = ();
165    
166    ## NOTE: |set_inner_html| copies most of this method's code    ## NOTE: |set_inner_html| copies most of this method's code
167    
168      $self->{confident} = 1 unless exists $self->{confident};
169      $self->{document}->input_encoding ($self->{input_encoding})
170          if defined $self->{input_encoding};
171    
172    my $i = 0;    my $i = 0;
173    my $line = 1;    my $line = 1;
174    my $column = 0;    my $column = 0;
# Line 141  sub new ($) { Line 225  sub new ($) {
225    $self->{parse_error} = sub {    $self->{parse_error} = sub {
226      #      #
227    };    };
228      $self->{change_encoding} = sub {
229        # if ($_[0] is a supported encoding) {
230        #   run "change the encoding" algorithm;
231        #   throw Whatpm::HTML::RestartParser (charset => $new_encoding);
232        # }
233      };
234      $self->{application_cache_selection} = sub {
235        #
236      };
237    return $self;    return $self;
238  } # new  } # new
239    
240    sub CM_ENTITY () { 0b001 } # & markup in data
241    sub CM_LIMITED_MARKUP () { 0b010 } # < markup in data (limited)
242    sub CM_FULL_MARKUP () { 0b100 } # < markup in data (any)
243    
244    sub PLAINTEXT_CONTENT_MODEL () { 0 }
245    sub CDATA_CONTENT_MODEL () { CM_LIMITED_MARKUP }
246    sub RCDATA_CONTENT_MODEL () { CM_ENTITY | CM_LIMITED_MARKUP }
247    sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }
248    
249    sub DATA_STATE () { 0 }
250    sub ENTITY_DATA_STATE () { 1 }
251    sub TAG_OPEN_STATE () { 2 }
252    sub CLOSE_TAG_OPEN_STATE () { 3 }
253    sub TAG_NAME_STATE () { 4 }
254    sub BEFORE_ATTRIBUTE_NAME_STATE () { 5 }
255    sub ATTRIBUTE_NAME_STATE () { 6 }
256    sub AFTER_ATTRIBUTE_NAME_STATE () { 7 }
257    sub BEFORE_ATTRIBUTE_VALUE_STATE () { 8 }
258    sub ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE () { 9 }
259    sub ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE () { 10 }
260    sub ATTRIBUTE_VALUE_UNQUOTED_STATE () { 11 }
261    sub ENTITY_IN_ATTRIBUTE_VALUE_STATE () { 12 }
262    sub MARKUP_DECLARATION_OPEN_STATE () { 13 }
263    sub COMMENT_START_STATE () { 14 }
264    sub COMMENT_START_DASH_STATE () { 15 }
265    sub COMMENT_STATE () { 16 }
266    sub COMMENT_END_STATE () { 17 }
267    sub COMMENT_END_DASH_STATE () { 18 }
268    sub BOGUS_COMMENT_STATE () { 19 }
269    sub DOCTYPE_STATE () { 20 }
270    sub BEFORE_DOCTYPE_NAME_STATE () { 21 }
271    sub DOCTYPE_NAME_STATE () { 22 }
272    sub AFTER_DOCTYPE_NAME_STATE () { 23 }
273    sub BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE () { 24 }
274    sub DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE () { 25 }
275    sub DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE () { 26 }
276    sub AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE () { 27 }
277    sub BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 28 }
278    sub DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE () { 29 }
279    sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE () { 30 }
280    sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }
281    sub BOGUS_DOCTYPE_STATE () { 32 }
282    
283    sub DOCTYPE_TOKEN () { 1 }
284    sub COMMENT_TOKEN () { 2 }
285    sub START_TAG_TOKEN () { 3 }
286    sub END_TAG_TOKEN () { 4 }
287    sub END_OF_FILE_TOKEN () { 5 }
288    sub CHARACTER_TOKEN () { 6 }
289    
290    sub AFTER_HTML_IMS () { 0b100 }
291    sub HEAD_IMS ()       { 0b1000 }
292    sub BODY_IMS ()       { 0b10000 }
293    sub BODY_TABLE_IMS () { 0b100000 }
294    sub TABLE_IMS ()      { 0b1000000 }
295    sub ROW_IMS ()        { 0b10000000 }
296    sub BODY_AFTER_IMS () { 0b100000000 }
297    sub FRAME_IMS ()      { 0b1000000000 }
298    
299    sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }
300    sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }
301    sub IN_HEAD_IM () { HEAD_IMS | 0b00 }
302    sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }
303    sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }
304    sub BEFORE_HEAD_IM () { HEAD_IMS | 0b11 }
305    sub IN_BODY_IM () { BODY_IMS }
306    sub IN_CELL_IM () { BODY_IMS | BODY_TABLE_IMS | 0b01 }
307    sub IN_CAPTION_IM () { BODY_IMS | BODY_TABLE_IMS | 0b10 }
308    sub IN_ROW_IM () { TABLE_IMS | ROW_IMS | 0b01 }
309    sub IN_TABLE_BODY_IM () { TABLE_IMS | ROW_IMS | 0b10 }
310    sub IN_TABLE_IM () { TABLE_IMS }
311    sub AFTER_BODY_IM () { BODY_AFTER_IMS }
312    sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }
313    sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }
314    sub IN_SELECT_IM () { 0b01 }
315    sub IN_COLUMN_GROUP_IM () { 0b10 }
316    
317  ## Implementations MUST act as if state machine in the spec  ## Implementations MUST act as if state machine in the spec
318    
319  sub _initialize_tokenizer ($) {  sub _initialize_tokenizer ($) {
320    my $self = shift;    my $self = shift;
321    $self->{state} = 'data'; # MUST    $self->{state} = DATA_STATE; # MUST
322    $self->{content_model_flag} = 'PCDATA'; # be    $self->{content_model} = PCDATA_CONTENT_MODEL; # be
323    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE
324    undef $self->{current_attribute};    undef $self->{current_attribute};
325    undef $self->{last_emitted_start_tag_name};    undef $self->{last_emitted_start_tag_name};
# Line 162  sub _initialize_tokenizer ($) { Line 332  sub _initialize_tokenizer ($) {
332  } # _initialize_tokenizer  } # _initialize_tokenizer
333    
334  ## A token has:  ## A token has:
335  ##   ->{type} eq 'DOCTYPE', 'start tag', 'end tag', 'comment',  ##   ->{type} == DOCTYPE_TOKEN, START_TAG_TOKEN, END_TAG_TOKEN, COMMENT_TOKEN,
336  ##       'character', or 'end-of-file'  ##       CHARACTER_TOKEN, or END_OF_FILE_TOKEN
337  ##   ->{name} (DOCTYPE, start tag (tag name), end tag (tag name))  ##   ->{name} (DOCTYPE_TOKEN)
338  ##   ->{public_identifier} (DOCTYPE)  ##   ->{tag_name} (START_TAG_TOKEN, END_TAG_TOKEN)
339  ##   ->{system_identifier} (DOCTYPE)  ##   ->{public_identifier} (DOCTYPE_TOKEN)
340  ##   ->{correct} == 1 or 0 (DOCTYPE)  ##   ->{system_identifier} (DOCTYPE_TOKEN)
341  ##   ->{attributes} isa HASH (start tag, end tag)  ##   ->{correct} == 1 or 0 (DOCTYPE_TOKEN)
342  ##   ->{data} (comment, character)  ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)
343    ##        ->{name}
344    ##        ->{value}
345    ##        ->{has_reference} == 1 or 0
346    ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
347    
348  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
349    
# Line 179  sub _initialize_tokenizer ($) { Line 353  sub _initialize_tokenizer ($) {
353  ## has completed loading.  If one has, then it MUST be executed  ## has completed loading.  If one has, then it MUST be executed
354  ## and removed from the list.  ## and removed from the list.
355    
356    ## NOTE: HTML5 "Writing HTML documents" section, applied to
357    ## documents and not to user agents and conformance checkers,
358    ## contains some requirements that are not detected by the
359    ## parsing algorithm:
360    ## - Some requirements on character encoding declarations. ## TODO
361    ## - "Elements MUST NOT contain content that their content model disallows."
362    ##   ... Some are parse error, some are not (will be reported by c.c.).
363    ## - Polytheistic slash SHOULD NOT be used. (Applied only to atheists.) ## TODO
364    ## - Text (in elements, attributes, and comments) SHOULD NOT contain
365    ##   control characters other than space characters. ## TODO: (what is control character? C0, C1 and DEL?  Unicode control character?)
366    
367    ## TODO: HTML5 poses authors two SHOULD-level requirements that cannot
368    ## be detected by the HTML5 parsing algorithm:
369    ## - Text,
370    
371  sub _get_next_token ($) {  sub _get_next_token ($) {
372    my $self = shift;    my $self = shift;
373    if (@{$self->{token}}) {    if (@{$self->{token}}) {
# Line 186  sub _get_next_token ($) { Line 375  sub _get_next_token ($) {
375    }    }
376    
377    A: {    A: {
378      if ($self->{state} eq 'data') {      if ($self->{state} == DATA_STATE) {
379        if ($self->{next_input_character} == 0x0026) { # &        if ($self->{next_input_character} == 0x0026) { # &
380          if ($self->{content_model_flag} eq 'PCDATA' or          if ($self->{content_model} & CM_ENTITY) { # PCDATA | RCDATA
381              $self->{content_model_flag} eq 'RCDATA') {            $self->{state} = ENTITY_DATA_STATE;
           $self->{state} = 'entity data';  
382            !!!next-input-character;            !!!next-input-character;
383            redo A;            redo A;
384          } else {          } else {
385            #            #
386          }          }
387        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_input_character} == 0x002D) { # -
388          if ($self->{content_model_flag} eq 'RCDATA' or          if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
             $self->{content_model_flag} eq 'CDATA') {  
389            unless ($self->{escape}) {            unless ($self->{escape}) {
390              if ($self->{prev_input_character}->[0] == 0x002D and # -              if ($self->{prev_input_character}->[0] == 0x002D and # -
391                  $self->{prev_input_character}->[1] == 0x0021 and # !                  $self->{prev_input_character}->[1] == 0x0021 and # !
# Line 210  sub _get_next_token ($) { Line 397  sub _get_next_token ($) {
397                    
398          #          #
399        } elsif ($self->{next_input_character} == 0x003C) { # <        } elsif ($self->{next_input_character} == 0x003C) { # <
400          if ($self->{content_model_flag} eq 'PCDATA' or          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA
401              (($self->{content_model_flag} eq 'CDATA' or              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA
               $self->{content_model_flag} eq 'RCDATA') and  
402               not $self->{escape})) {               not $self->{escape})) {
403            $self->{state} = 'tag open';            $self->{state} = TAG_OPEN_STATE;
404            !!!next-input-character;            !!!next-input-character;
405            redo A;            redo A;
406          } else {          } else {
# Line 222  sub _get_next_token ($) { Line 408  sub _get_next_token ($) {
408          }          }
409        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
410          if ($self->{escape} and          if ($self->{escape} and
411              ($self->{content_model_flag} eq 'RCDATA' or              ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA
              $self->{content_model_flag} eq 'CDATA')) {  
412            if ($self->{prev_input_character}->[0] == 0x002D and # -            if ($self->{prev_input_character}->[0] == 0x002D and # -
413                $self->{prev_input_character}->[1] == 0x002D) { # -                $self->{prev_input_character}->[1] == 0x002D) { # -
414              delete $self->{escape};              delete $self->{escape};
# Line 232  sub _get_next_token ($) { Line 417  sub _get_next_token ($) {
417                    
418          #          #
419        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
420          !!!emit ({type => 'end-of-file'});          !!!emit ({type => END_OF_FILE_TOKEN});
421          last A; ## TODO: ok?          last A; ## TODO: ok?
422        }        }
423        # Anything else        # Anything else
424        my $token = {type => 'character',        my $token = {type => CHARACTER_TOKEN,
425                     data => chr $self->{next_input_character}};                     data => chr $self->{next_input_character}};
426        ## Stay in the data state        ## Stay in the data state
427        !!!next-input-character;        !!!next-input-character;
# Line 244  sub _get_next_token ($) { Line 429  sub _get_next_token ($) {
429        !!!emit ($token);        !!!emit ($token);
430    
431        redo A;        redo A;
432      } elsif ($self->{state} eq 'entity data') {      } elsif ($self->{state} == ENTITY_DATA_STATE) {
433        ## (cannot happen in CDATA state)        ## (cannot happen in CDATA state)
434                
435        my $token = $self->_tokenize_attempt_to_consume_an_entity;        my $token = $self->_tokenize_attempt_to_consume_an_entity (0);
436    
437        $self->{state} = 'data';        $self->{state} = DATA_STATE;
438        # next-input-character is already done        # next-input-character is already done
439    
440        unless (defined $token) {        unless (defined $token) {
441          !!!emit ({type => 'character', data => '&'});          !!!emit ({type => CHARACTER_TOKEN, data => '&'});
442        } else {        } else {
443          !!!emit ($token);          !!!emit ($token);
444        }        }
445    
446        redo A;        redo A;
447      } elsif ($self->{state} eq 'tag open') {      } elsif ($self->{state} == TAG_OPEN_STATE) {
448        if ($self->{content_model_flag} eq 'RCDATA' or        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
           $self->{content_model_flag} eq 'CDATA') {  
449          if ($self->{next_input_character} == 0x002F) { # /          if ($self->{next_input_character} == 0x002F) { # /
450            !!!next-input-character;            !!!next-input-character;
451            $self->{state} = 'close tag open';            $self->{state} = CLOSE_TAG_OPEN_STATE;
452            redo A;            redo A;
453          } else {          } else {
454            ## reconsume            ## reconsume
455            $self->{state} = 'data';            $self->{state} = DATA_STATE;
456    
457            !!!emit ({type => 'character', data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<'});
458    
459            redo A;            redo A;
460          }          }
461        } elsif ($self->{content_model_flag} eq 'PCDATA') {        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA
462          if ($self->{next_input_character} == 0x0021) { # !          if ($self->{next_input_character} == 0x0021) { # !
463            $self->{state} = 'markup declaration open';            $self->{state} = MARKUP_DECLARATION_OPEN_STATE;
464            !!!next-input-character;            !!!next-input-character;
465            redo A;            redo A;
466          } elsif ($self->{next_input_character} == 0x002F) { # /          } elsif ($self->{next_input_character} == 0x002F) { # /
467            $self->{state} = 'close tag open';            $self->{state} = CLOSE_TAG_OPEN_STATE;
468            !!!next-input-character;            !!!next-input-character;
469            redo A;            redo A;
470          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_input_character} and
471                   $self->{next_input_character} <= 0x005A) { # A..Z                   $self->{next_input_character} <= 0x005A) { # A..Z
472            $self->{current_token}            $self->{current_token}
473              = {type => 'start tag',              = {type => START_TAG_TOKEN,
474                 tag_name => chr ($self->{next_input_character} + 0x0020)};                 tag_name => chr ($self->{next_input_character} + 0x0020)};
475            $self->{state} = 'tag name';            $self->{state} = TAG_NAME_STATE;
476            !!!next-input-character;            !!!next-input-character;
477            redo A;            redo A;
478          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_input_character} and
479                   $self->{next_input_character} <= 0x007A) { # a..z                   $self->{next_input_character} <= 0x007A) { # a..z
480            $self->{current_token} = {type => 'start tag',            $self->{current_token} = {type => START_TAG_TOKEN,
481                              tag_name => chr ($self->{next_input_character})};                              tag_name => chr ($self->{next_input_character})};
482            $self->{state} = 'tag name';            $self->{state} = TAG_NAME_STATE;
483            !!!next-input-character;            !!!next-input-character;
484            redo A;            redo A;
485          } elsif ($self->{next_input_character} == 0x003E) { # >          } elsif ($self->{next_input_character} == 0x003E) { # >
486            !!!parse-error (type => 'empty start tag');            !!!parse-error (type => 'empty start tag');
487            $self->{state} = 'data';            $self->{state} = DATA_STATE;
488            !!!next-input-character;            !!!next-input-character;
489    
490            !!!emit ({type => 'character', data => '<>'});            !!!emit ({type => CHARACTER_TOKEN, data => '<>'});
491    
492            redo A;            redo A;
493          } elsif ($self->{next_input_character} == 0x003F) { # ?          } elsif ($self->{next_input_character} == 0x003F) { # ?
494            !!!parse-error (type => 'pio');            !!!parse-error (type => 'pio');
495            $self->{state} = 'bogus comment';            $self->{state} = BOGUS_COMMENT_STATE;
496            ## $self->{next_input_character} is intentionally left as is            ## $self->{next_input_character} is intentionally left as is
497            redo A;            redo A;
498          } else {          } else {
499            !!!parse-error (type => 'bare stago');            !!!parse-error (type => 'bare stago');
500            $self->{state} = 'data';            $self->{state} = DATA_STATE;
501            ## reconsume            ## reconsume
502    
503            !!!emit ({type => 'character', data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<'});
504    
505            redo A;            redo A;
506          }          }
507        } else {        } else {
508          die "$0: $self->{content_model_flag}: Unknown content model flag";          die "$0: $self->{content_model} in tag open";
509        }        }
510      } elsif ($self->{state} eq 'close tag open') {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
511        if ($self->{content_model_flag} eq 'RCDATA' or        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
512            $self->{content_model_flag} eq 'CDATA') {          if (defined $self->{last_emitted_start_tag_name}) {
513          my @next_char;            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>
514          TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {            my @next_char;
515              TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {
516                push @next_char, $self->{next_input_character};
517                my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);
518                my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;
519                if ($self->{next_input_character} == $c or $self->{next_input_character} == $C) {
520                  !!!next-input-character;
521                  next TAGNAME;
522                } else {
523                  $self->{next_input_character} = shift @next_char; # reconsume
524                  !!!back-next-input-character (@next_char);
525                  $self->{state} = DATA_STATE;
526    
527                  !!!emit ({type => CHARACTER_TOKEN, data => '</'});
528      
529                  redo A;
530                }
531              }
532            push @next_char, $self->{next_input_character};            push @next_char, $self->{next_input_character};
533            my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);        
534            my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;            unless ($self->{next_input_character} == 0x0009 or # HT
535            if ($self->{next_input_character} == $c or $self->{next_input_character} == $C) {                    $self->{next_input_character} == 0x000A or # LF
536              !!!next-input-character;                    $self->{next_input_character} == 0x000B or # VT
537              next TAGNAME;                    $self->{next_input_character} == 0x000C or # FF
538            } else {                    $self->{next_input_character} == 0x0020 or # SP
539                      $self->{next_input_character} == 0x003E or # >
540                      $self->{next_input_character} == 0x002F or # /
541                      $self->{next_input_character} == -1) {
542              $self->{next_input_character} = shift @next_char; # reconsume              $self->{next_input_character} = shift @next_char; # reconsume
543              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
544              $self->{state} = 'data';              $self->{state} = DATA_STATE;
545                !!!emit ({type => CHARACTER_TOKEN, data => '</'});
             !!!emit ({type => 'character', data => '</'});  
   
546              redo A;              redo A;
547              } else {
548                $self->{next_input_character} = shift @next_char;
549                !!!back-next-input-character (@next_char);
550                # and consume...
551            }            }
         }  
         push @next_char, $self->{next_input_character};  
       
         unless ($self->{next_input_character} == 0x0009 or # HT  
                 $self->{next_input_character} == 0x000A or # LF  
                 $self->{next_input_character} == 0x000B or # VT  
                 $self->{next_input_character} == 0x000C or # FF  
                 $self->{next_input_character} == 0x0020 or # SP  
                 $self->{next_input_character} == 0x003E or # >  
                 $self->{next_input_character} == 0x002F or # /  
                 $self->{next_input_character} == -1) {  
           $self->{next_input_character} = shift @next_char; # reconsume  
           !!!back-next-input-character (@next_char);  
           $self->{state} = 'data';  
   
           !!!emit ({type => 'character', data => '</'});  
   
           redo A;  
552          } else {          } else {
553            $self->{next_input_character} = shift @next_char;            ## No start tag token has ever been emitted
554            !!!back-next-input-character (@next_char);            # next-input-character is already done
555            # and consume...            $self->{state} = DATA_STATE;
556              !!!emit ({type => CHARACTER_TOKEN, data => '</'});
557              redo A;
558          }          }
559        }        }
560                
561        if (0x0041 <= $self->{next_input_character} and        if (0x0041 <= $self->{next_input_character} and
562            $self->{next_input_character} <= 0x005A) { # A..Z            $self->{next_input_character} <= 0x005A) { # A..Z
563          $self->{current_token} = {type => 'end tag',          $self->{current_token} = {type => END_TAG_TOKEN,
564                            tag_name => chr ($self->{next_input_character} + 0x0020)};                            tag_name => chr ($self->{next_input_character} + 0x0020)};
565          $self->{state} = 'tag name';          $self->{state} = TAG_NAME_STATE;
566          !!!next-input-character;          !!!next-input-character;
567          redo A;          redo A;
568        } elsif (0x0061 <= $self->{next_input_character} and        } elsif (0x0061 <= $self->{next_input_character} and
569                 $self->{next_input_character} <= 0x007A) { # a..z                 $self->{next_input_character} <= 0x007A) { # a..z
570          $self->{current_token} = {type => 'end tag',          $self->{current_token} = {type => END_TAG_TOKEN,
571                            tag_name => chr ($self->{next_input_character})};                            tag_name => chr ($self->{next_input_character})};
572          $self->{state} = 'tag name';          $self->{state} = TAG_NAME_STATE;
573          !!!next-input-character;          !!!next-input-character;
574          redo A;          redo A;
575        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
576          !!!parse-error (type => 'empty end tag');          !!!parse-error (type => 'empty end tag');
577          $self->{state} = 'data';          $self->{state} = DATA_STATE;
578          !!!next-input-character;          !!!next-input-character;
579          redo A;          redo A;
580        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
581          !!!parse-error (type => 'bare etago');          !!!parse-error (type => 'bare etago');
582          $self->{state} = 'data';          $self->{state} = DATA_STATE;
583          # reconsume          # reconsume
584    
585          !!!emit ({type => 'character', data => '</'});          !!!emit ({type => CHARACTER_TOKEN, data => '</'});
586    
587          redo A;          redo A;
588        } else {        } else {
589          !!!parse-error (type => 'bogus end tag');          !!!parse-error (type => 'bogus end tag');
590          $self->{state} = 'bogus comment';          $self->{state} = BOGUS_COMMENT_STATE;
591          ## $self->{next_input_character} is intentionally left as is          ## $self->{next_input_character} is intentionally left as is
592          redo A;          redo A;
593        }        }
594      } elsif ($self->{state} eq 'tag name') {      } elsif ($self->{state} == TAG_NAME_STATE) {
595        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
596            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
597            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
598            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
599            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
600          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
601          !!!next-input-character;          !!!next-input-character;
602          redo A;          redo A;
603        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
604          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
605              $self->{current_token}->{first_start_tag}
606                  = not defined $self->{last_emitted_start_tag_name};
607            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
608          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
609            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
610            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
611              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
612            }            }
613          } else {          } else {
614            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
615          }          }
616          $self->{state} = 'data';          $self->{state} = DATA_STATE;
617          !!!next-input-character;          !!!next-input-character;
618    
619          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
620    
621          redo A;          redo A;
622        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_input_character} and
# Line 437  sub _get_next_token ($) { Line 628  sub _get_next_token ($) {
628          redo A;          redo A;
629        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
630          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
631          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
632              $self->{current_token}->{first_start_tag}
633                  = not defined $self->{last_emitted_start_tag_name};
634            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
635          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
636            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
637            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
638              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
639            }            }
640          } else {          } else {
641            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
642          }          }
643          $self->{state} = 'data';          $self->{state} = DATA_STATE;
644          # reconsume          # reconsume
645    
646          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
647    
648          redo A;          redo A;
649        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_input_character} == 0x002F) { # /
650          !!!next-input-character;          !!!next-input-character;
651          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_input_character} == 0x003E and # >
652              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
653              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
654            # permitted slash            # permitted slash
655            #            #
656          } else {          } else {
657            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
658          }          }
659          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
660          # next-input-character is already done          # next-input-character is already done
661          redo A;          redo A;
662        } else {        } else {
# Line 474  sub _get_next_token ($) { Line 666  sub _get_next_token ($) {
666          !!!next-input-character;          !!!next-input-character;
667          redo A;          redo A;
668        }        }
669      } elsif ($self->{state} eq 'before attribute name') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_NAME_STATE) {
670        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
671            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
672            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
# Line 484  sub _get_next_token ($) { Line 676  sub _get_next_token ($) {
676          !!!next-input-character;          !!!next-input-character;
677          redo A;          redo A;
678        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
679          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
680              $self->{current_token}->{first_start_tag}
681                  = not defined $self->{last_emitted_start_tag_name};
682            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
683          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
684            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
685            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
686              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
687            }            }
688          } else {          } else {
689            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
690          }          }
691          $self->{state} = 'data';          $self->{state} = DATA_STATE;
692          !!!next-input-character;          !!!next-input-character;
693    
694          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
695    
696          redo A;          redo A;
697        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_input_character} and
698                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_input_character} <= 0x005A) { # A..Z
699          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),
700                                value => ''};                                value => ''};
701          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
702          !!!next-input-character;          !!!next-input-character;
703          redo A;          redo A;
704        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_input_character} == 0x002F) { # /
705          !!!next-input-character;          !!!next-input-character;
706          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_input_character} == 0x003E and # >
707              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
708              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
709            # permitted slash            # permitted slash
710            #            #
# Line 523  sub _get_next_token ($) { Line 716  sub _get_next_token ($) {
716          redo A;          redo A;
717        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
718          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
719          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
720              $self->{current_token}->{first_start_tag}
721                  = not defined $self->{last_emitted_start_tag_name};
722            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
723          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
724            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
725            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
726              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
727            }            }
728          } else {          } else {
729            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
730          }          }
731          $self->{state} = 'data';          $self->{state} = DATA_STATE;
732          # reconsume          # reconsume
733    
734          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
735    
736          redo A;          redo A;
737        } else {        } else {
738          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          $self->{current_attribute} = {name => chr ($self->{next_input_character}),
739                                value => ''};                                value => ''};
740          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
741          !!!next-input-character;          !!!next-input-character;
742          redo A;          redo A;
743        }        }
744      } elsif ($self->{state} eq 'attribute name') {      } elsif ($self->{state} == ATTRIBUTE_NAME_STATE) {
745        my $before_leave = sub {        my $before_leave = sub {
746          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
747              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
748            !!!parse-error (type => 'dupulicate attribute');            !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name});
749            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
750          } else {          } else {
751            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}
# Line 565  sub _get_next_token ($) { Line 759  sub _get_next_token ($) {
759            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
760            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
761          $before_leave->();          $before_leave->();
762          $self->{state} = 'after attribute name';          $self->{state} = AFTER_ATTRIBUTE_NAME_STATE;
763          !!!next-input-character;          !!!next-input-character;
764          redo A;          redo A;
765        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_input_character} == 0x003D) { # =
766          $before_leave->();          $before_leave->();
767          $self->{state} = 'before attribute value';          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
768          !!!next-input-character;          !!!next-input-character;
769          redo A;          redo A;
770        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
771          $before_leave->();          $before_leave->();
772          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
773              $self->{current_token}->{first_start_tag}
774                  = not defined $self->{last_emitted_start_tag_name};
775            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
776          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
777            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
778            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
779              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
780            }            }
781          } else {          } else {
782            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
783          }          }
784          $self->{state} = 'data';          $self->{state} = DATA_STATE;
785          !!!next-input-character;          !!!next-input-character;
786    
787          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
788    
789          redo A;          redo A;
790        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_input_character} and
# Line 602  sub _get_next_token ($) { Line 797  sub _get_next_token ($) {
797          $before_leave->();          $before_leave->();
798          !!!next-input-character;          !!!next-input-character;
799          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_input_character} == 0x003E and # >
800              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
801              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
802            # permitted slash            # permitted slash
803            #            #
804          } else {          } else {
805            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
806          }          }
807          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
808          # next-input-character is already done          # next-input-character is already done
809          redo A;          redo A;
810        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
811          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
812          $before_leave->();          $before_leave->();
813          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
814              $self->{current_token}->{first_start_tag}
815                  = not defined $self->{last_emitted_start_tag_name};
816            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
817          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
818            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
819            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
820              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
821            }            }
822          } else {          } else {
823            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
824          }          }
825          $self->{state} = 'data';          $self->{state} = DATA_STATE;
826          # reconsume          # reconsume
827    
828          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
829    
830          redo A;          redo A;
831        } else {        } else {
# Line 638  sub _get_next_token ($) { Line 834  sub _get_next_token ($) {
834          !!!next-input-character;          !!!next-input-character;
835          redo A;          redo A;
836        }        }
837      } elsif ($self->{state} eq 'after attribute name') {      } elsif ($self->{state} == AFTER_ATTRIBUTE_NAME_STATE) {
838        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
839            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
840            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
# Line 648  sub _get_next_token ($) { Line 844  sub _get_next_token ($) {
844          !!!next-input-character;          !!!next-input-character;
845          redo A;          redo A;
846        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_input_character} == 0x003D) { # =
847          $self->{state} = 'before attribute value';          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
848          !!!next-input-character;          !!!next-input-character;
849          redo A;          redo A;
850        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
851          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
852              $self->{current_token}->{first_start_tag}
853                  = not defined $self->{last_emitted_start_tag_name};
854            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
855          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
856            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
857            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
858              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
859            }            }
860          } else {          } else {
861            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
862          }          }
863          $self->{state} = 'data';          $self->{state} = DATA_STATE;
864          !!!next-input-character;          !!!next-input-character;
865    
866          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
867    
868          redo A;          redo A;
869        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_input_character} and
870                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_input_character} <= 0x005A) { # A..Z
871          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),
872                                value => ''};                                value => ''};
873          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
874          !!!next-input-character;          !!!next-input-character;
875          redo A;          redo A;
876        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_input_character} == 0x002F) { # /
877          !!!next-input-character;          !!!next-input-character;
878          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_input_character} == 0x003E and # >
879              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
880              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
881            # permitted slash            # permitted slash
882            #            #
883          } else {          } else {
884            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
885              ## TODO: Different error type for <aa / bb> than <aa/>
886          }          }
887          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
888          # next-input-character is already done          # next-input-character is already done
889          redo A;          redo A;
890        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
891          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
892          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
893              $self->{current_token}->{first_start_tag}
894                  = not defined $self->{last_emitted_start_tag_name};
895            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
896          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
897            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
898            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
899              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
900            }            }
901          } else {          } else {
902            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
903          }          }
904          $self->{state} = 'data';          $self->{state} = DATA_STATE;
905          # reconsume          # reconsume
906    
907          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
908    
909          redo A;          redo A;
910        } else {        } else {
911          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          $self->{current_attribute} = {name => chr ($self->{next_input_character}),
912                                value => ''};                                value => ''};
913          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
914          !!!next-input-character;          !!!next-input-character;
915          redo A;                  redo A;        
916        }        }
917      } elsif ($self->{state} eq 'before attribute value') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_VALUE_STATE) {
918        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
919            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
920            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
# Line 725  sub _get_next_token ($) { Line 924  sub _get_next_token ($) {
924          !!!next-input-character;          !!!next-input-character;
925          redo A;          redo A;
926        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_input_character} == 0x0022) { # "
927          $self->{state} = 'attribute value (double-quoted)';          $self->{state} = ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE;
928          !!!next-input-character;          !!!next-input-character;
929          redo A;          redo A;
930        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_input_character} == 0x0026) { # &
931          $self->{state} = 'attribute value (unquoted)';          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
932          ## reconsume          ## reconsume
933          redo A;          redo A;
934        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_input_character} == 0x0027) { # '
935          $self->{state} = 'attribute value (single-quoted)';          $self->{state} = ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE;
936          !!!next-input-character;          !!!next-input-character;
937          redo A;          redo A;
938        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
939          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
940              $self->{current_token}->{first_start_tag}
941                  = not defined $self->{last_emitted_start_tag_name};
942            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
943          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
944            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
945            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
946              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
947            }            }
948          } else {          } else {
949            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
950          }          }
951          $self->{state} = 'data';          $self->{state} = DATA_STATE;
952          !!!next-input-character;          !!!next-input-character;
953    
954          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
955    
956          redo A;          redo A;
957        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
958          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
959          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
960              $self->{current_token}->{first_start_tag}
961                  = not defined $self->{last_emitted_start_tag_name};
962            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
963          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
964            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
965            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
966              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
967            }            }
968          } else {          } else {
969            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
970          }          }
971          $self->{state} = 'data';          $self->{state} = DATA_STATE;
972          ## reconsume          ## reconsume
973    
974          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
975    
976          redo A;          redo A;
977        } else {        } else {
978          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});
979          $self->{state} = 'attribute value (unquoted)';          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
980          !!!next-input-character;          !!!next-input-character;
981          redo A;          redo A;
982        }        }
983      } elsif ($self->{state} eq 'attribute value (double-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE) {
984        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_input_character} == 0x0022) { # "
985          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
986          !!!next-input-character;          !!!next-input-character;
987          redo A;          redo A;
988        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_input_character} == 0x0026) { # &
989          $self->{last_attribute_value_state} = 'attribute value (double-quoted)';          $self->{last_attribute_value_state} = $self->{state};
990          $self->{state} = 'entity in attribute value';          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
991          !!!next-input-character;          !!!next-input-character;
992          redo A;          redo A;
993        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
994          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
995          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
996              $self->{current_token}->{first_start_tag}
997                  = not defined $self->{last_emitted_start_tag_name};
998            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
999          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1000            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1001            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1002              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1003            }            }
1004          } else {          } else {
1005            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1006          }          }
1007          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1008          ## reconsume          ## reconsume
1009    
1010          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1011    
1012          redo A;          redo A;
1013        } else {        } else {
# Line 814  sub _get_next_token ($) { Line 1016  sub _get_next_token ($) {
1016          !!!next-input-character;          !!!next-input-character;
1017          redo A;          redo A;
1018        }        }
1019      } elsif ($self->{state} eq 'attribute value (single-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE) {
1020        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_input_character} == 0x0027) { # '
1021          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1022          !!!next-input-character;          !!!next-input-character;
1023          redo A;          redo A;
1024        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_input_character} == 0x0026) { # &
1025          $self->{last_attribute_value_state} = 'attribute value (single-quoted)';          $self->{last_attribute_value_state} = $self->{state};
1026          $self->{state} = 'entity in attribute value';          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1027          !!!next-input-character;          !!!next-input-character;
1028          redo A;          redo A;
1029        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1030          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1031          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1032              $self->{current_token}->{first_start_tag}
1033                  = not defined $self->{last_emitted_start_tag_name};
1034            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1035          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1036            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1037            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1038              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1039            }            }
1040          } else {          } else {
1041            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1042          }          }
1043          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1044          ## reconsume          ## reconsume
1045    
1046          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1047    
1048          redo A;          redo A;
1049        } else {        } else {
# Line 849  sub _get_next_token ($) { Line 1052  sub _get_next_token ($) {
1052          !!!next-input-character;          !!!next-input-character;
1053          redo A;          redo A;
1054        }        }
1055      } elsif ($self->{state} eq 'attribute value (unquoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_UNQUOTED_STATE) {
1056        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1057            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1058            $self->{next_input_character} == 0x000B or # HT            $self->{next_input_character} == 0x000B or # HT
1059            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
1060            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
1061          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1062          !!!next-input-character;          !!!next-input-character;
1063          redo A;          redo A;
1064        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_input_character} == 0x0026) { # &
1065          $self->{last_attribute_value_state} = 'attribute value (unquoted)';          $self->{last_attribute_value_state} = $self->{state};
1066          $self->{state} = 'entity in attribute value';          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1067          !!!next-input-character;          !!!next-input-character;
1068          redo A;          redo A;
1069        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1070          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1071              $self->{current_token}->{first_start_tag}
1072                  = not defined $self->{last_emitted_start_tag_name};
1073            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1074          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1075            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1076            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1077              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1078            }            }
1079          } else {          } else {
1080            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1081          }          }
1082          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1083          !!!next-input-character;          !!!next-input-character;
1084    
1085          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1086    
1087          redo A;          redo A;
1088        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1089          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1090          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1091              $self->{current_token}->{first_start_tag}
1092                  = not defined $self->{last_emitted_start_tag_name};
1093            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1094          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1095            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1096            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1097              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1098            }            }
1099          } else {          } else {
1100            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1101          }          }
1102          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1103          ## reconsume          ## reconsume
1104    
1105          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1106    
1107          redo A;          redo A;
1108        } else {        } else {
# Line 906  sub _get_next_token ($) { Line 1111  sub _get_next_token ($) {
1111          !!!next-input-character;          !!!next-input-character;
1112          redo A;          redo A;
1113        }        }
1114      } elsif ($self->{state} eq 'entity in attribute value') {      } elsif ($self->{state} == ENTITY_IN_ATTRIBUTE_VALUE_STATE) {
1115        my $token = $self->_tokenize_attempt_to_consume_an_entity;        my $token = $self->_tokenize_attempt_to_consume_an_entity (1);
1116    
1117        unless (defined $token) {        unless (defined $token) {
1118          $self->{current_attribute}->{value} .= '&';          $self->{current_attribute}->{value} .= '&';
1119        } else {        } else {
1120          $self->{current_attribute}->{value} .= $token->{data};          $self->{current_attribute}->{value} .= $token->{data};
1121            $self->{current_attribute}->{has_reference} = $token->{has_reference};
1122          ## 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"
1123        }        }
1124    
1125        $self->{state} = $self->{last_attribute_value_state};        $self->{state} = $self->{last_attribute_value_state};
1126        # next-input-character is already done        # next-input-character is already done
1127        redo A;        redo A;
1128      } elsif ($self->{state} eq 'bogus comment') {      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
1129        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1130                
1131        my $token = {type => 'comment', data => ''};        my $token = {type => COMMENT_TOKEN, data => ''};
1132    
1133        BC: {        BC: {
1134          if ($self->{next_input_character} == 0x003E) { # >          if ($self->{next_input_character} == 0x003E) { # >
1135            $self->{state} = 'data';            $self->{state} = DATA_STATE;
1136            !!!next-input-character;            !!!next-input-character;
1137    
1138            !!!emit ($token);            !!!emit ($token);
1139    
1140            redo A;            redo A;
1141          } elsif ($self->{next_input_character} == -1) {          } elsif ($self->{next_input_character} == -1) {
1142            $self->{state} = 'data';            $self->{state} = DATA_STATE;
1143            ## reconsume            ## reconsume
1144    
1145            !!!emit ($token);            !!!emit ($token);
# Line 945  sub _get_next_token ($) { Line 1151  sub _get_next_token ($) {
1151            redo BC;            redo BC;
1152          }          }
1153        } # BC        } # BC
1154      } elsif ($self->{state} eq 'markup declaration open') {      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
1155        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1156    
1157        my @next_char;        my @next_char;
# Line 955  sub _get_next_token ($) { Line 1161  sub _get_next_token ($) {
1161          !!!next-input-character;          !!!next-input-character;
1162          push @next_char, $self->{next_input_character};          push @next_char, $self->{next_input_character};
1163          if ($self->{next_input_character} == 0x002D) { # -          if ($self->{next_input_character} == 0x002D) { # -
1164            $self->{current_token} = {type => 'comment', data => ''};            $self->{current_token} = {type => COMMENT_TOKEN, data => ''};
1165            $self->{state} = 'comment';            $self->{state} = COMMENT_START_STATE;
1166            !!!next-input-character;            !!!next-input-character;
1167            redo A;            redo A;
1168          }          }
# Line 987  sub _get_next_token ($) { Line 1193  sub _get_next_token ($) {
1193                    if ($self->{next_input_character} == 0x0045 or # E                    if ($self->{next_input_character} == 0x0045 or # E
1194                        $self->{next_input_character} == 0x0065) { # e                        $self->{next_input_character} == 0x0065) { # e
1195                      ## ISSUE: What a stupid code this is!                      ## ISSUE: What a stupid code this is!
1196                      $self->{state} = 'DOCTYPE';                      $self->{state} = DOCTYPE_STATE;
1197                      !!!next-input-character;                      !!!next-input-character;
1198                      redo A;                      redo A;
1199                    }                    }
# Line 998  sub _get_next_token ($) { Line 1204  sub _get_next_token ($) {
1204          }          }
1205        }        }
1206    
1207        !!!parse-error (type => 'bogus comment open');        !!!parse-error (type => 'bogus comment');
1208        $self->{next_input_character} = shift @next_char;        $self->{next_input_character} = shift @next_char;
1209        !!!back-next-input-character (@next_char);        !!!back-next-input-character (@next_char);
1210        $self->{state} = 'bogus comment';        $self->{state} = BOGUS_COMMENT_STATE;
1211        redo A;        redo A;
1212                
1213        ## ISSUE: typos in spec: chacacters, is is a parse error        ## ISSUE: typos in spec: chacacters, is is a parse error
1214        ## 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?
1215      } elsif ($self->{state} eq 'comment') {      } elsif ($self->{state} == COMMENT_START_STATE) {
1216        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_input_character} == 0x002D) { # -
1217          $self->{state} = 'comment dash';          $self->{state} = COMMENT_START_DASH_STATE;
1218            !!!next-input-character;
1219            redo A;
1220          } elsif ($self->{next_input_character} == 0x003E) { # >
1221            !!!parse-error (type => 'bogus comment');
1222            $self->{state} = DATA_STATE;
1223          !!!next-input-character;          !!!next-input-character;
1224    
1225            !!!emit ($self->{current_token}); # comment
1226    
1227          redo A;          redo A;
1228        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1229          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1230          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1231            ## reconsume
1232    
1233            !!!emit ($self->{current_token}); # comment
1234    
1235            redo A;
1236          } else {
1237            $self->{current_token}->{data} # comment
1238                .= chr ($self->{next_input_character});
1239            $self->{state} = COMMENT_STATE;
1240            !!!next-input-character;
1241            redo A;
1242          }
1243        } elsif ($self->{state} == COMMENT_START_DASH_STATE) {
1244          if ($self->{next_input_character} == 0x002D) { # -
1245            $self->{state} = COMMENT_END_STATE;
1246            !!!next-input-character;
1247            redo A;
1248          } elsif ($self->{next_input_character} == 0x003E) { # >
1249            !!!parse-error (type => 'bogus comment');
1250            $self->{state} = DATA_STATE;
1251            !!!next-input-character;
1252    
1253            !!!emit ($self->{current_token}); # comment
1254    
1255            redo A;
1256          } elsif ($self->{next_input_character} == -1) {
1257            !!!parse-error (type => 'unclosed comment');
1258            $self->{state} = DATA_STATE;
1259            ## reconsume
1260    
1261            !!!emit ($self->{current_token}); # comment
1262    
1263            redo A;
1264          } else {
1265            $self->{current_token}->{data} # comment
1266                .= '-' . chr ($self->{next_input_character});
1267            $self->{state} = COMMENT_STATE;
1268            !!!next-input-character;
1269            redo A;
1270          }
1271        } elsif ($self->{state} == COMMENT_STATE) {
1272          if ($self->{next_input_character} == 0x002D) { # -
1273            $self->{state} = COMMENT_END_DASH_STATE;
1274            !!!next-input-character;
1275            redo A;
1276          } elsif ($self->{next_input_character} == -1) {
1277            !!!parse-error (type => 'unclosed comment');
1278            $self->{state} = DATA_STATE;
1279          ## reconsume          ## reconsume
1280    
1281          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
1282    
1283          redo A;          redo A;
1284        } else {        } else {
# Line 1026  sub _get_next_token ($) { Line 1287  sub _get_next_token ($) {
1287          !!!next-input-character;          !!!next-input-character;
1288          redo A;          redo A;
1289        }        }
1290      } elsif ($self->{state} eq 'comment dash') {      } elsif ($self->{state} == COMMENT_END_DASH_STATE) {
1291        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_input_character} == 0x002D) { # -
1292          $self->{state} = 'comment end';          $self->{state} = COMMENT_END_STATE;
1293          !!!next-input-character;          !!!next-input-character;
1294          redo A;          redo A;
1295        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1296          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1297          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1298          ## reconsume          ## reconsume
1299    
1300          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
1301    
1302          redo A;          redo A;
1303        } else {        } else {
1304          $self->{current_token}->{data} .= '-' . chr ($self->{next_input_character}); # comment          $self->{current_token}->{data} .= '-' . chr ($self->{next_input_character}); # comment
1305          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
1306          !!!next-input-character;          !!!next-input-character;
1307          redo A;          redo A;
1308        }        }
1309      } elsif ($self->{state} eq 'comment end') {      } elsif ($self->{state} == COMMENT_END_STATE) {
1310        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_input_character} == 0x003E) { # >
1311          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1312          !!!next-input-character;          !!!next-input-character;
1313    
1314          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
1315    
1316          redo A;          redo A;
1317        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_input_character} == 0x002D) { # -
# Line 1063  sub _get_next_token ($) { Line 1322  sub _get_next_token ($) {
1322          redo A;          redo A;
1323        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1324          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1325          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1326          ## reconsume          ## reconsume
1327    
1328          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
1329    
1330          redo A;          redo A;
1331        } else {        } else {
1332          !!!parse-error (type => 'dash in comment');          !!!parse-error (type => 'dash in comment');
1333          $self->{current_token}->{data} .= '--' . chr ($self->{next_input_character}); # comment          $self->{current_token}->{data} .= '--' . chr ($self->{next_input_character}); # comment
1334          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
1335          !!!next-input-character;          !!!next-input-character;
1336          redo A;          redo A;
1337        }        }
1338      } elsif ($self->{state} eq 'DOCTYPE') {      } elsif ($self->{state} == DOCTYPE_STATE) {
1339        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1340            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1341            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
1342            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
1343            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
1344          $self->{state} = 'before DOCTYPE name';          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
1345          !!!next-input-character;          !!!next-input-character;
1346          redo A;          redo A;
1347        } else {        } else {
1348          !!!parse-error (type => 'no space before DOCTYPE name');          !!!parse-error (type => 'no space before DOCTYPE name');
1349          $self->{state} = 'before DOCTYPE name';          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
1350          ## reconsume          ## reconsume
1351          redo A;          redo A;
1352        }        }
1353      } elsif ($self->{state} eq 'before DOCTYPE name') {      } elsif ($self->{state} == BEFORE_DOCTYPE_NAME_STATE) {
1354        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1355            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1356            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
# Line 1103  sub _get_next_token ($) { Line 1361  sub _get_next_token ($) {
1361          redo A;          redo A;
1362        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1363          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
1364          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1365          !!!next-input-character;          !!!next-input-character;
1366    
1367          !!!emit ({type => 'DOCTYPE'}); # incorrect          !!!emit ({type => DOCTYPE_TOKEN}); # incorrect
1368    
1369          redo A;          redo A;
1370        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1371          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
1372          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1373          ## reconsume          ## reconsume
1374    
1375          !!!emit ({type => 'DOCTYPE'}); # incorrect          !!!emit ({type => DOCTYPE_TOKEN}); # incorrect
1376    
1377          redo A;          redo A;
1378        } else {        } else {
1379          $self->{current_token}          $self->{current_token}
1380              = {type => 'DOCTYPE',              = {type => DOCTYPE_TOKEN,
1381                 name => chr ($self->{next_input_character}),                 name => chr ($self->{next_input_character}),
1382                 correct => 1};                 correct => 1};
1383  ## ISSUE: "Set the token's name name to the" in the spec  ## ISSUE: "Set the token's name name to the" in the spec
1384          $self->{state} = 'DOCTYPE name';          $self->{state} = DOCTYPE_NAME_STATE;
1385          !!!next-input-character;          !!!next-input-character;
1386          redo A;          redo A;
1387        }        }
1388      } elsif ($self->{state} eq 'DOCTYPE name') {      } elsif ($self->{state} == DOCTYPE_NAME_STATE) {
1389  ## ISSUE: Redundant "First," in the spec.  ## ISSUE: Redundant "First," in the spec.
1390        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1391            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1392            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
1393            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
1394            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
1395          $self->{state} = 'after DOCTYPE name';          $self->{state} = AFTER_DOCTYPE_NAME_STATE;
1396          !!!next-input-character;          !!!next-input-character;
1397          redo A;          redo A;
1398        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1399          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1400          !!!next-input-character;          !!!next-input-character;
1401    
1402          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1403    
1404          redo A;          redo A;
1405        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1406          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1407          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1408          ## reconsume          ## reconsume
1409    
1410          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
1411          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1412    
1413          redo A;          redo A;
1414        } else {        } else {
# Line 1162  sub _get_next_token ($) { Line 1418  sub _get_next_token ($) {
1418          !!!next-input-character;          !!!next-input-character;
1419          redo A;          redo A;
1420        }        }
1421      } elsif ($self->{state} eq 'after DOCTYPE name') {      } elsif ($self->{state} == AFTER_DOCTYPE_NAME_STATE) {
1422        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1423            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1424            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
# Line 1172  sub _get_next_token ($) { Line 1428  sub _get_next_token ($) {
1428          !!!next-input-character;          !!!next-input-character;
1429          redo A;          redo A;
1430        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1431          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1432          !!!next-input-character;          !!!next-input-character;
1433    
1434          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1435    
1436          redo A;          redo A;
1437        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1438          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1439          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1440          ## reconsume          ## reconsume
1441    
1442          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
1443          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1444    
1445          redo A;          redo A;
1446        } elsif ($self->{next_input_character} == 0x0050 or # P        } elsif ($self->{next_input_character} == 0x0050 or # P
# Line 1206  sub _get_next_token ($) { Line 1460  sub _get_next_token ($) {
1460                  !!!next-input-character;                  !!!next-input-character;
1461                  if ($self->{next_input_character} == 0x0043 or # C                  if ($self->{next_input_character} == 0x0043 or # C
1462                      $self->{next_input_character} == 0x0063) { # c                      $self->{next_input_character} == 0x0063) { # c
1463                    $self->{state} = 'before DOCTYPE public identifier';                    $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1464                    !!!next-input-character;                    !!!next-input-character;
1465                    redo A;                    redo A;
1466                  }                  }
# Line 1233  sub _get_next_token ($) { Line 1487  sub _get_next_token ($) {
1487                  !!!next-input-character;                  !!!next-input-character;
1488                  if ($self->{next_input_character} == 0x004D or # M                  if ($self->{next_input_character} == 0x004D or # M
1489                      $self->{next_input_character} == 0x006D) { # m                      $self->{next_input_character} == 0x006D) { # m
1490                    $self->{state} = 'before DOCTYPE system identifier';                    $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
1491                    !!!next-input-character;                    !!!next-input-character;
1492                    redo A;                    redo A;
1493                  }                  }
# Line 1249  sub _get_next_token ($) { Line 1503  sub _get_next_token ($) {
1503        }        }
1504    
1505        !!!parse-error (type => 'string after DOCTYPE name');        !!!parse-error (type => 'string after DOCTYPE name');
1506        $self->{state} = 'bogus DOCTYPE';        $self->{state} = BOGUS_DOCTYPE_STATE;
1507        # next-input-character is already done        # next-input-character is already done
1508        redo A;        redo A;
1509      } elsif ($self->{state} eq 'before DOCTYPE public identifier') {      } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
1510        if ({        if ({
1511              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1512              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
# Line 1262  sub _get_next_token ($) { Line 1516  sub _get_next_token ($) {
1516          redo A;          redo A;
1517        } elsif ($self->{next_input_character} eq 0x0022) { # "        } elsif ($self->{next_input_character} eq 0x0022) { # "
1518          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{current_token}->{public_identifier} = ''; # DOCTYPE
1519          $self->{state} = 'DOCTYPE public identifier (double-quoted)';          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;
1520          !!!next-input-character;          !!!next-input-character;
1521          redo A;          redo A;
1522        } elsif ($self->{next_input_character} eq 0x0027) { # '        } elsif ($self->{next_input_character} eq 0x0027) { # '
1523          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{current_token}->{public_identifier} = ''; # DOCTYPE
1524          $self->{state} = 'DOCTYPE public identifier (single-quoted)';          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;
1525          !!!next-input-character;          !!!next-input-character;
1526          redo A;          redo A;
1527        } elsif ($self->{next_input_character} eq 0x003E) { # >        } elsif ($self->{next_input_character} eq 0x003E) { # >
1528          !!!parse-error (type => 'no PUBLIC literal');          !!!parse-error (type => 'no PUBLIC literal');
1529    
1530          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1531          !!!next-input-character;          !!!next-input-character;
1532    
1533          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
1534          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1535    
1536          redo A;          redo A;
1537        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1538          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1539    
1540          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1541          ## reconsume          ## reconsume
1542    
1543          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
1544          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1545    
1546          redo A;          redo A;
1547        } else {        } else {
1548          !!!parse-error (type => 'string after PUBLIC');          !!!parse-error (type => 'string after PUBLIC');
1549          $self->{state} = 'bogus DOCTYPE';          $self->{state} = BOGUS_DOCTYPE_STATE;
1550          !!!next-input-character;          !!!next-input-character;
1551          redo A;          redo A;
1552        }        }
1553      } elsif ($self->{state} eq 'DOCTYPE public identifier (double-quoted)') {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {
1554        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_input_character} == 0x0022) { # "
1555          $self->{state} = 'after DOCTYPE public identifier';          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1556          !!!next-input-character;          !!!next-input-character;
1557          redo A;          redo A;
1558          } elsif ($self->{next_input_character} == 0x003E) { # >
1559            !!!parse-error (type => 'unclosed PUBLIC literal');
1560    
1561            $self->{state} = DATA_STATE;
1562            !!!next-input-character;
1563    
1564            delete $self->{current_token}->{correct};
1565            !!!emit ($self->{current_token}); # DOCTYPE
1566    
1567            redo A;
1568        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1569          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
1570    
1571          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1572          ## reconsume          ## reconsume
1573    
1574          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
1575          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1576    
1577          redo A;          redo A;
1578        } else {        } else {
# Line 1321  sub _get_next_token ($) { Line 1582  sub _get_next_token ($) {
1582          !!!next-input-character;          !!!next-input-character;
1583          redo A;          redo A;
1584        }        }
1585      } elsif ($self->{state} eq 'DOCTYPE public identifier (single-quoted)') {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {
1586        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_input_character} == 0x0027) { # '
1587          $self->{state} = 'after DOCTYPE public identifier';          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1588          !!!next-input-character;          !!!next-input-character;
1589          redo A;          redo A;
1590          } elsif ($self->{next_input_character} == 0x003E) { # >
1591            !!!parse-error (type => 'unclosed PUBLIC literal');
1592    
1593            $self->{state} = DATA_STATE;
1594            !!!next-input-character;
1595    
1596            delete $self->{current_token}->{correct};
1597            !!!emit ($self->{current_token}); # DOCTYPE
1598    
1599            redo A;
1600        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1601          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
1602    
1603          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1604          ## reconsume          ## reconsume
1605    
1606          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
1607          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1608    
1609          redo A;          redo A;
1610        } else {        } else {
# Line 1344  sub _get_next_token ($) { Line 1614  sub _get_next_token ($) {
1614          !!!next-input-character;          !!!next-input-character;
1615          redo A;          redo A;
1616        }        }
1617      } elsif ($self->{state} eq 'after DOCTYPE public identifier') {      } elsif ($self->{state} == AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
1618        if ({        if ({
1619              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1620              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
# Line 1354  sub _get_next_token ($) { Line 1624  sub _get_next_token ($) {
1624          redo A;          redo A;
1625        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_input_character} == 0x0022) { # "
1626          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1627          $self->{state} = 'DOCTYPE system identifier (double-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
1628          !!!next-input-character;          !!!next-input-character;
1629          redo A;          redo A;
1630        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_input_character} == 0x0027) { # '
1631          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1632          $self->{state} = 'DOCTYPE system identifier (single-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
1633          !!!next-input-character;          !!!next-input-character;
1634          redo A;          redo A;
1635        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1636          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1637          !!!next-input-character;          !!!next-input-character;
1638    
1639          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1640    
1641          redo A;          redo A;
1642        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1643          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1644    
1645          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1646          ## recomsume          ## reconsume
1647    
1648          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
1649          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1650    
1651          redo A;          redo A;
1652        } else {        } else {
1653          !!!parse-error (type => 'string after PUBLIC literal');          !!!parse-error (type => 'string after PUBLIC literal');
1654          $self->{state} = 'bogus DOCTYPE';          $self->{state} = BOGUS_DOCTYPE_STATE;
1655          !!!next-input-character;          !!!next-input-character;
1656          redo A;          redo A;
1657        }        }
1658      } elsif ($self->{state} eq 'before DOCTYPE system identifier') {      } elsif ($self->{state} == BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
1659        if ({        if ({
1660              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1661              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
# Line 1397  sub _get_next_token ($) { Line 1665  sub _get_next_token ($) {
1665          redo A;          redo A;
1666        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_input_character} == 0x0022) { # "
1667          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1668          $self->{state} = 'DOCTYPE system identifier (double-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
1669          !!!next-input-character;          !!!next-input-character;
1670          redo A;          redo A;
1671        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_input_character} == 0x0027) { # '
1672          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1673          $self->{state} = 'DOCTYPE system identifier (single-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
1674          !!!next-input-character;          !!!next-input-character;
1675          redo A;          redo A;
1676        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1677          !!!parse-error (type => 'no SYSTEM literal');          !!!parse-error (type => 'no SYSTEM literal');
1678          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1679          !!!next-input-character;          !!!next-input-character;
1680    
1681          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
1682          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1683    
1684          redo A;          redo A;
1685        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1686          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1687    
1688          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1689          ## recomsume          ## reconsume
1690    
1691          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
1692          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1693    
1694          redo A;          redo A;
1695        } else {        } else {
1696          !!!parse-error (type => 'string after PUBLIC literal');          !!!parse-error (type => 'string after SYSTEM');
1697          $self->{state} = 'bogus DOCTYPE';          $self->{state} = BOGUS_DOCTYPE_STATE;
1698          !!!next-input-character;          !!!next-input-character;
1699          redo A;          redo A;
1700        }        }
1701      } elsif ($self->{state} eq 'DOCTYPE system identifier (double-quoted)') {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {
1702        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_input_character} == 0x0022) { # "
1703          $self->{state} = 'after DOCTYPE system identifier';          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
1704          !!!next-input-character;          !!!next-input-character;
1705          redo A;          redo A;
1706          } elsif ($self->{next_input_character} == 0x003E) { # >
1707            !!!parse-error (type => 'unclosed PUBLIC literal');
1708    
1709            $self->{state} = DATA_STATE;
1710            !!!next-input-character;
1711    
1712            delete $self->{current_token}->{correct};
1713            !!!emit ($self->{current_token}); # DOCTYPE
1714    
1715            redo A;
1716        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1717          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
1718    
1719          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1720          ## reconsume          ## reconsume
1721    
1722          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
1723          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1724    
1725          redo A;          redo A;
1726        } else {        } else {
# Line 1455  sub _get_next_token ($) { Line 1730  sub _get_next_token ($) {
1730          !!!next-input-character;          !!!next-input-character;
1731          redo A;          redo A;
1732        }        }
1733      } elsif ($self->{state} eq 'DOCTYPE system identifier (single-quoted)') {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {
1734        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_input_character} == 0x0027) { # '
1735          $self->{state} = 'after DOCTYPE system identifier';          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
1736          !!!next-input-character;          !!!next-input-character;
1737          redo A;          redo A;
1738          } elsif ($self->{next_input_character} == 0x003E) { # >
1739            !!!parse-error (type => 'unclosed PUBLIC literal');
1740    
1741            $self->{state} = DATA_STATE;
1742            !!!next-input-character;
1743    
1744            delete $self->{current_token}->{correct};
1745            !!!emit ($self->{current_token}); # DOCTYPE
1746    
1747            redo A;
1748        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1749          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
1750    
1751          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1752          ## reconsume          ## reconsume
1753    
1754          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
1755          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1756    
1757          redo A;          redo A;
1758        } else {        } else {
# Line 1478  sub _get_next_token ($) { Line 1762  sub _get_next_token ($) {
1762          !!!next-input-character;          !!!next-input-character;
1763          redo A;          redo A;
1764        }        }
1765      } elsif ($self->{state} eq 'after DOCTYPE system identifier') {      } elsif ($self->{state} == AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
1766        if ({        if ({
1767              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1768              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
# Line 1487  sub _get_next_token ($) { Line 1771  sub _get_next_token ($) {
1771          !!!next-input-character;          !!!next-input-character;
1772          redo A;          redo A;
1773        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1774          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1775          !!!next-input-character;          !!!next-input-character;
1776    
1777          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1778    
1779          redo A;          redo A;
1780        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1781          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1782    
1783          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1784          ## recomsume          ## reconsume
1785    
1786          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
1787          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1788    
1789          redo A;          redo A;
1790        } else {        } else {
1791          !!!parse-error (type => 'string after SYSTEM literal');          !!!parse-error (type => 'string after SYSTEM literal');
1792          $self->{state} = 'bogus DOCTYPE';          $self->{state} = BOGUS_DOCTYPE_STATE;
1793          !!!next-input-character;          !!!next-input-character;
1794          redo A;          redo A;
1795        }        }
1796      } elsif ($self->{state} eq 'bogus DOCTYPE') {      } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {
1797        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_input_character} == 0x003E) { # >
1798          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1799          !!!next-input-character;          !!!next-input-character;
1800    
1801          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
1802          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1803    
1804          redo A;          redo A;
1805        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1806          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1807          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1808          ## reconsume          ## reconsume
1809    
1810          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
1811          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1812    
1813          redo A;          redo A;
1814        } else {        } else {
# Line 1544  sub _get_next_token ($) { Line 1824  sub _get_next_token ($) {
1824    die "$0: _get_next_token: unexpected case";    die "$0: _get_next_token: unexpected case";
1825  } # _get_next_token  } # _get_next_token
1826    
1827  sub _tokenize_attempt_to_consume_an_entity ($) {  sub _tokenize_attempt_to_consume_an_entity ($$) {
1828    my $self = shift;    my ($self, $in_attr) = @_;
1829    
1830    if ({    if ({
1831         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,
# Line 1558  sub _tokenize_attempt_to_consume_an_enti Line 1838  sub _tokenize_attempt_to_consume_an_enti
1838      !!!next-input-character;      !!!next-input-character;
1839      if ($self->{next_input_character} == 0x0078 or # x      if ($self->{next_input_character} == 0x0078 or # x
1840          $self->{next_input_character} == 0x0058) { # X          $self->{next_input_character} == 0x0058) { # X
1841        my $num;        my $code;
1842        X: {        X: {
1843          my $x_char = $self->{next_input_character};          my $x_char = $self->{next_input_character};
1844          !!!next-input-character;          !!!next-input-character;
1845          if (0x0030 <= $self->{next_input_character} and          if (0x0030 <= $self->{next_input_character} and
1846              $self->{next_input_character} <= 0x0039) { # 0..9              $self->{next_input_character} <= 0x0039) { # 0..9
1847            $num ||= 0;            $code ||= 0;
1848            $num *= 0x10;            $code *= 0x10;
1849            $num += $self->{next_input_character} - 0x0030;            $code += $self->{next_input_character} - 0x0030;
1850            redo X;            redo X;
1851          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_input_character} and
1852                   $self->{next_input_character} <= 0x0066) { # a..f                   $self->{next_input_character} <= 0x0066) { # a..f
1853            ## ISSUE: the spec says U+0078, which is apparently incorrect            $code ||= 0;
1854            $num ||= 0;            $code *= 0x10;
1855            $num *= 0x10;            $code += $self->{next_input_character} - 0x0060 + 9;
           $num += $self->{next_input_character} - 0x0060 + 9;  
1856            redo X;            redo X;
1857          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_input_character} and
1858                   $self->{next_input_character} <= 0x0046) { # A..F                   $self->{next_input_character} <= 0x0046) { # A..F
1859            ## ISSUE: the spec says U+0058, which is apparently incorrect            $code ||= 0;
1860            $num ||= 0;            $code *= 0x10;
1861            $num *= 0x10;            $code += $self->{next_input_character} - 0x0040 + 9;
           $num += $self->{next_input_character} - 0x0040 + 9;  
1862            redo X;            redo X;
1863          } elsif (not defined $num) { # no hexadecimal digit          } elsif (not defined $code) { # no hexadecimal digit
1864            !!!parse-error (type => 'bare hcro');            !!!parse-error (type => 'bare hcro');
1865              !!!back-next-input-character ($x_char, $self->{next_input_character});
1866            $self->{next_input_character} = 0x0023; # #            $self->{next_input_character} = 0x0023; # #
           !!!back-next-input-character ($x_char);  
1867            return undef;            return undef;
1868          } elsif ($self->{next_input_character} == 0x003B) { # ;          } elsif ($self->{next_input_character} == 0x003B) { # ;
1869            !!!next-input-character;            !!!next-input-character;
# Line 1593  sub _tokenize_attempt_to_consume_an_enti Line 1871  sub _tokenize_attempt_to_consume_an_enti
1871            !!!parse-error (type => 'no refc');            !!!parse-error (type => 'no refc');
1872          }          }
1873    
1874          ## TODO: check the definition for |a valid Unicode character|.          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
1875          ## <http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2006-December/thread.html#8189>            !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);
1876          if ($num > 1114111 or $num == 0) {            $code = 0xFFFD;
1877            $num = 0xFFFD; # REPLACEMENT CHARACTER          } elsif ($code > 0x10FFFF) {
1878            ## ISSUE: Why this is not an error?            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);
1879          } elsif (0x80 <= $num and $num <= 0x9F) {            $code = 0xFFFD;
1880            !!!parse-error (type => sprintf 'c1 entity:U+%04X', $num);          } elsif ($code == 0x000D) {
1881            $num = $c1_entity_char->{$num};            !!!parse-error (type => 'CR character reference');
1882              $code = 0x000A;
1883            } elsif (0x80 <= $code and $code <= 0x9F) {
1884              !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);
1885              $code = $c1_entity_char->{$code};
1886          }          }
1887    
1888          return {type => 'character', data => chr $num};          return {type => CHARACTER_TOKEN, data => chr $code,
1889                    has_reference => 1};
1890        } # X        } # X
1891      } elsif (0x0030 <= $self->{next_input_character} and      } elsif (0x0030 <= $self->{next_input_character} and
1892               $self->{next_input_character} <= 0x0039) { # 0..9               $self->{next_input_character} <= 0x0039) { # 0..9
# Line 1624  sub _tokenize_attempt_to_consume_an_enti Line 1907  sub _tokenize_attempt_to_consume_an_enti
1907          !!!parse-error (type => 'no refc');          !!!parse-error (type => 'no refc');
1908        }        }
1909    
1910        ## TODO: check the definition for |a valid Unicode character|.        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
1911        if ($code > 1114111 or $code == 0) {          !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);
1912          $code = 0xFFFD; # REPLACEMENT CHARACTER          $code = 0xFFFD;
1913          ## ISSUE: Why this is not an error?        } elsif ($code > 0x10FFFF) {
1914            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);
1915            $code = 0xFFFD;
1916          } elsif ($code == 0x000D) {
1917            !!!parse-error (type => 'CR character reference');
1918            $code = 0x000A;
1919        } elsif (0x80 <= $code and $code <= 0x9F) {        } elsif (0x80 <= $code and $code <= 0x9F) {
1920          !!!parse-error (type => sprintf 'c1 entity:U+%04X', $code);          !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);
1921          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
1922        }        }
1923                
1924        return {type => 'character', data => chr $code};        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1};
1925      } else {      } else {
1926        !!!parse-error (type => 'bare nero');        !!!parse-error (type => 'bare nero');
1927        !!!back-next-input-character ($self->{next_input_character});        !!!back-next-input-character ($self->{next_input_character});
# Line 1648  sub _tokenize_attempt_to_consume_an_enti Line 1936  sub _tokenize_attempt_to_consume_an_enti
1936      !!!next-input-character;      !!!next-input-character;
1937    
1938      my $value = $entity_name;      my $value = $entity_name;
1939      my $match;      my $match = 0;
1940      require Whatpm::_NamedEntityList;      require Whatpm::_NamedEntityList;
1941      our $EntityChar;      our $EntityChar;
1942    
# Line 1663  sub _tokenize_attempt_to_consume_an_enti Line 1951  sub _tokenize_attempt_to_consume_an_enti
1951              $self->{next_input_character} == 0x003B)) { # ;              $self->{next_input_character} == 0x003B)) { # ;
1952        $entity_name .= chr $self->{next_input_character};        $entity_name .= chr $self->{next_input_character};
1953        if (defined $EntityChar->{$entity_name}) {        if (defined $EntityChar->{$entity_name}) {
         $value = $EntityChar->{$entity_name};  
1954          if ($self->{next_input_character} == 0x003B) { # ;          if ($self->{next_input_character} == 0x003B) { # ;
1955              $value = $EntityChar->{$entity_name};
1956            $match = 1;            $match = 1;
1957            !!!next-input-character;            !!!next-input-character;
1958            last;            last;
1959          } else {          } else {
1960              $value = $EntityChar->{$entity_name};
1961            $match = -1;            $match = -1;
1962              !!!next-input-character;
1963          }          }
1964        } else {        } else {
1965          $value .= chr $self->{next_input_character};          $value .= chr $self->{next_input_character};
1966            $match *= 2;
1967            !!!next-input-character;
1968        }        }
       !!!next-input-character;  
1969      }      }
1970            
1971      if ($match > 0) {      if ($match > 0) {
1972        return {type => 'character', data => $value};        return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};
1973      } elsif ($match < 0) {      } elsif ($match < 0) {
1974        !!!parse-error (type => 'refc');        !!!parse-error (type => 'no refc');
1975        return {type => 'character', data => $value};        if ($in_attr and $match < -1) {
1976            return {type => CHARACTER_TOKEN, data => '&'.$entity_name};
1977          } else {
1978            return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};
1979          }
1980      } else {      } else {
1981        !!!parse-error (type => 'bare ero');        !!!parse-error (type => 'bare ero');
1982        ## NOTE: No characters are consumed in the spec.        ## NOTE: "No characters are consumed" in the spec.
1983        !!!back-token ({type => 'character', data => $value});        return {type => CHARACTER_TOKEN, data => '&'.$value};
       return undef;  
1984      }      }
1985    } else {    } else {
1986      ## no characters are consumed      ## no characters are consumed
# Line 1728  sub _construct_tree ($) { Line 2022  sub _construct_tree ($) {
2022        
2023    !!!next-token;    !!!next-token;
2024    
2025    $self->{insertion_mode} = 'before head';    $self->{insertion_mode} = BEFORE_HEAD_IM;
2026    undef $self->{form_element};    undef $self->{form_element};
2027    undef $self->{head_element};    undef $self->{head_element};
2028    $self->{open_elements} = [];    $self->{open_elements} = [];
# Line 1742  sub _construct_tree ($) { Line 2036  sub _construct_tree ($) {
2036  sub _tree_construction_initial ($) {  sub _tree_construction_initial ($) {
2037    my $self = shift;    my $self = shift;
2038    INITIAL: {    INITIAL: {
2039      if ($token->{type} eq 'DOCTYPE') {      if ($token->{type} == DOCTYPE_TOKEN) {
2040        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"
2041        ## error, switch to a conformance checking mode for another        ## error, switch to a conformance checking mode for another
2042        ## language.        ## language.
# Line 1869  sub _tree_construction_initial ($) { Line 2163  sub _tree_construction_initial ($) {
2163        !!!next-token;        !!!next-token;
2164        return;        return;
2165      } elsif ({      } elsif ({
2166                'start tag' => 1,                START_TAG_TOKEN, 1,
2167                'end tag' => 1,                END_TAG_TOKEN, 1,
2168                'end-of-file' => 1,                END_OF_FILE_TOKEN, 1,
2169               }->{$token->{type}}) {               }->{$token->{type}}) {
2170        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE');
2171        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
2172        ## Go to the root element phase        ## Go to the root element phase
2173        ## reprocess        ## reprocess
2174        return;        return;
2175      } elsif ($token->{type} eq 'character') {      } elsif ($token->{type} == CHARACTER_TOKEN) {
2176        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
2177          ## Ignore the token          ## Ignore the token
2178    
2179          unless (length $token->{data}) {          unless (length $token->{data}) {
2180            ## Stay in the phase            ## Stay in the phase
2181            !!!next-token;            !!!next-token;
# Line 1893  sub _tree_construction_initial ($) { Line 2188  sub _tree_construction_initial ($) {
2188        ## Go to the root element phase        ## Go to the root element phase
2189        ## reprocess        ## reprocess
2190        return;        return;
2191      } elsif ($token->{type} eq 'comment') {      } elsif ($token->{type} == COMMENT_TOKEN) {
2192        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
2193        $self->{document}->append_child ($comment);        $self->{document}->append_child ($comment);
2194                
# Line 1901  sub _tree_construction_initial ($) { Line 2196  sub _tree_construction_initial ($) {
2196        !!!next-token;        !!!next-token;
2197        redo INITIAL;        redo INITIAL;
2198      } else {      } else {
2199        die "$0: $token->{type}: Unknown token";        die "$0: $token->{type}: Unknown token type";
2200      }      }
2201    } # INITIAL    } # INITIAL
2202  } # _tree_construction_initial  } # _tree_construction_initial
# Line 1910  sub _tree_construction_root_element ($) Line 2205  sub _tree_construction_root_element ($)
2205    my $self = shift;    my $self = shift;
2206        
2207    B: {    B: {
2208        if ($token->{type} eq 'DOCTYPE') {        if ($token->{type} == DOCTYPE_TOKEN) {
2209          !!!parse-error (type => 'in html:#DOCTYPE');          !!!parse-error (type => 'in html:#DOCTYPE');
2210          ## Ignore the token          ## Ignore the token
2211          ## Stay in the phase          ## Stay in the phase
2212          !!!next-token;          !!!next-token;
2213          redo B;          redo B;
2214        } elsif ($token->{type} eq 'comment') {        } elsif ($token->{type} == COMMENT_TOKEN) {
2215          my $comment = $self->{document}->create_comment ($token->{data});          my $comment = $self->{document}->create_comment ($token->{data});
2216          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
2217          ## Stay in the phase          ## Stay in the phase
2218          !!!next-token;          !!!next-token;
2219          redo B;          redo B;
2220        } elsif ($token->{type} eq 'character') {        } elsif ($token->{type} == CHARACTER_TOKEN) {
2221          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
2222            $self->{document}->manakai_append_text ($1);            ## Ignore the token.
2223            ## ISSUE: DOM3 Core does not allow Document > Text  
2224            unless (length $token->{data}) {            unless (length $token->{data}) {
2225              ## Stay in the phase              ## Stay in the phase
2226              !!!next-token;              !!!next-token;
2227              redo B;              redo B;
2228            }            }
2229          }          }
2230    
2231            $self->{application_cache_selection}->(undef);
2232    
2233            #
2234          } elsif ($token->{type} == START_TAG_TOKEN) {
2235            if ($token->{tag_name} eq 'html' and
2236                $token->{attributes}->{manifest}) {
2237              $self->{application_cache_selection}
2238                   ->($token->{attributes}->{manifest}->{value});
2239              ## ISSUE: No relative reference resolution?
2240            } else {
2241              $self->{application_cache_selection}->(undef);
2242            }
2243    
2244            ## ISSUE: There is an issue in the spec
2245          #          #
2246        } elsif ({        } elsif ({
2247                  'start tag' => 1,                  END_TAG_TOKEN, 1,
2248                  'end tag' => 1,                  END_OF_FILE_TOKEN, 1,
                 'end-of-file' => 1,  
2249                 }->{$token->{type}}) {                 }->{$token->{type}}) {
2250            $self->{application_cache_selection}->(undef);
2251    
2252          ## ISSUE: There is an issue in the spec          ## ISSUE: There is an issue in the spec
2253          #          #
2254        } else {        } else {
2255          die "$0: $token->{type}: Unknown token";          die "$0: $token->{type}: Unknown token type";
2256        }        }
2257    
2258        my $root_element; !!!create-element ($root_element, 'html');        my $root_element; !!!create-element ($root_element, 'html');
2259        $self->{document}->append_child ($root_element);        $self->{document}->append_child ($root_element);
2260        push @{$self->{open_elements}}, [$root_element, 'html'];        push @{$self->{open_elements}}, [$root_element, 'html'];
       #$phase = 'main';  
2261        ## reprocess        ## reprocess
2262        #redo B;        #redo B;
2263        return;        return; ## Go to the main phase.
2264    } # B    } # B
2265  } # _tree_construction_root_element  } # _tree_construction_root_element
2266    
# Line 1965  sub _reset_insertion_mode ($) { Line 2276  sub _reset_insertion_mode ($) {
2276            
2277      ## Step 3      ## Step 3
2278      S3: {      S3: {
2279        $last = 1 if $self->{open_elements}->[0]->[0] eq $node->[0];        ## ISSUE: Oops! "If node is the first node in the stack of open
2280        if (defined $self->{inner_html_node}) {        ## elements, then set last to true. If the context element of the
2281          if ($self->{inner_html_node}->[1] eq 'td' or        ## HTML fragment parsing algorithm is neither a td element nor a
2282              $self->{inner_html_node}->[1] eq 'th') {        ## th element, then set node to the context element. (fragment case)":
2283            #        ## The second "if" is in the scope of the first "if"!?
2284          } else {        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
2285            $node = $self->{inner_html_node};          $last = 1;
2286            if (defined $self->{inner_html_node}) {
2287              if ($self->{inner_html_node}->[1] eq 'td' or
2288                  $self->{inner_html_node}->[1] eq 'th') {
2289                #
2290              } else {
2291                $node = $self->{inner_html_node};
2292              }
2293          }          }
2294        }        }
2295            
2296        ## Step 4..13        ## Step 4..13
2297        my $new_mode = {        my $new_mode = {
2298                        select => 'in select',                        select => IN_SELECT_IM,
2299                        td => 'in cell',                        td => IN_CELL_IM,
2300                        th => 'in cell',                        th => IN_CELL_IM,
2301                        tr => 'in row',                        tr => IN_ROW_IM,
2302                        tbody => 'in table body',                        tbody => IN_TABLE_BODY_IM,
2303                        thead => 'in table head',                        thead => IN_TABLE_BODY_IM,
2304                        tfoot => 'in table foot',                        tfoot => IN_TABLE_BODY_IM,
2305                        caption => 'in caption',                        caption => IN_CAPTION_IM,
2306                        colgroup => 'in column group',                        colgroup => IN_COLUMN_GROUP_IM,
2307                        table => 'in table',                        table => IN_TABLE_IM,
2308                        head => 'in body', # not in head!                        head => IN_BODY_IM, # not in head!
2309                        body => 'in body',                        body => IN_BODY_IM,
2310                        frameset => 'in frameset',                        frameset => IN_FRAMESET_IM,
2311                       }->{$node->[1]};                       }->{$node->[1]};
2312        $self->{insertion_mode} = $new_mode and return if defined $new_mode;        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
2313                
2314        ## Step 14        ## Step 14
2315        if ($node->[1] eq 'html') {        if ($node->[1] eq 'html') {
2316          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
2317            $self->{insertion_mode} = 'before head';            $self->{insertion_mode} = BEFORE_HEAD_IM;
2318          } else {          } else {
2319            $self->{insertion_mode} = 'after head';            $self->{insertion_mode} = AFTER_HEAD_IM;
2320          }          }
2321          return;          return;
2322        }        }
2323                
2324        ## Step 15        ## Step 15
2325        $self->{insertion_mode} = 'in body' and return if $last;        $self->{insertion_mode} = IN_BODY_IM and return if $last;
2326                
2327        ## Step 16        ## Step 16
2328        $i--;        $i--;
# Line 2018  sub _reset_insertion_mode ($) { Line 2336  sub _reset_insertion_mode ($) {
2336  sub _tree_construction_main ($) {  sub _tree_construction_main ($) {
2337    my $self = shift;    my $self = shift;
2338    
   my $phase = 'main';  
   
2339    my $active_formatting_elements = [];    my $active_formatting_elements = [];
2340    
2341    my $reconstruct_active_formatting_elements = sub { # MUST    my $reconstruct_active_formatting_elements = sub { # MUST
# Line 2102  sub _tree_construction_main ($) { Line 2418  sub _tree_construction_main ($) {
2418      }      }
2419    }; # $clear_up_to_marker    }; # $clear_up_to_marker
2420    
2421    my $style_start_tag = sub {    my $parse_rcdata = sub ($$) {
2422      my $style_el; !!!create-element ($style_el, 'style', $token->{attributes});      my ($content_model_flag, $insert) = @_;
2423      ## $self->{insertion_mode} eq 'in head' and ... (always true)  
2424      (($self->{insertion_mode} eq 'in head' and defined $self->{head_element})      ## Step 1
2425       ? $self->{head_element} : $self->{open_elements}->[-1]->[0])      my $start_tag_name = $token->{tag_name};
2426        ->append_child ($style_el);      my $el;
2427      $self->{content_model_flag} = 'CDATA';      !!!create-element ($el, $start_tag_name, $token->{attributes});
2428    
2429        ## Step 2
2430        $insert->($el); # /context node/->append_child ($el)
2431    
2432        ## Step 3
2433        $self->{content_model} = $content_model_flag; # CDATA or RCDATA
2434      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
2435                  
2436        ## Step 4
2437      my $text = '';      my $text = '';
2438      !!!next-token;      !!!next-token;
2439      while ($token->{type} eq 'character') {      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
2440        $text .= $token->{data};        $text .= $token->{data};
2441        !!!next-token;        !!!next-token;
2442      } # stop if non-character token or tokenizer stops tokenising      }
2443    
2444        ## Step 5
2445      if (length $text) {      if (length $text) {
2446        $style_el->manakai_append_text ($text);        my $text = $self->{document}->create_text_node ($text);
2447          $el->append_child ($text);
2448      }      }
2449        
2450      $self->{content_model_flag} = 'PCDATA';      ## Step 6
2451                      $self->{content_model} = PCDATA_CONTENT_MODEL;
2452      if ($token->{type} eq 'end tag' and $token->{tag_name} eq 'style') {  
2453        ## Step 7
2454        if ($token->{type} == END_TAG_TOKEN and $token->{tag_name} eq $start_tag_name) {
2455        ## Ignore the token        ## Ignore the token
2456      } else {      } elsif ($content_model_flag == CDATA_CONTENT_MODEL) {
2457        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!parse-error (type => 'in CDATA:#'.$token->{type});
2458        ## ISSUE: And ignore?      } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
2459          !!!parse-error (type => 'in RCDATA:#'.$token->{type});
2460        } else {
2461          die "$0: $content_model_flag in parse_rcdata";
2462      }      }
2463      !!!next-token;      !!!next-token;
2464    }; # $style_start_tag    }; # $parse_rcdata
2465    
2466    my $script_start_tag = sub {    my $script_start_tag = sub ($) {
2467        my $insert = $_[0];
2468      my $script_el;      my $script_el;
2469      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, 'script', $token->{attributes});
2470      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
2471    
2472      $self->{content_model_flag} = 'CDATA';      $self->{content_model} = CDATA_CONTENT_MODEL;
2473      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
2474            
2475      my $text = '';      my $text = '';
2476      !!!next-token;      !!!next-token;
2477      while ($token->{type} eq 'character') {      while ($token->{type} == CHARACTER_TOKEN) {
2478        $text .= $token->{data};        $text .= $token->{data};
2479        !!!next-token;        !!!next-token;
2480      } # stop if non-character token or tokenizer stops tokenising      } # stop if non-character token or tokenizer stops tokenising
# Line 2150  sub _tree_construction_main ($) { Line 2482  sub _tree_construction_main ($) {
2482        $script_el->manakai_append_text ($text);        $script_el->manakai_append_text ($text);
2483      }      }
2484                                
2485      $self->{content_model_flag} = 'PCDATA';      $self->{content_model} = PCDATA_CONTENT_MODEL;
2486    
2487      if ($token->{type} eq 'end tag' and      if ($token->{type} == END_TAG_TOKEN and
2488          $token->{tag_name} eq 'script') {          $token->{tag_name} eq 'script') {
2489        ## Ignore the token        ## Ignore the token
2490      } else {      } else {
# Line 2166  sub _tree_construction_main ($) { Line 2498  sub _tree_construction_main ($) {
2498      } else {      } else {
2499        ## TODO: $old_insertion_point = current insertion point        ## TODO: $old_insertion_point = current insertion point
2500        ## TODO: insertion point = just before the next input character        ## TODO: insertion point = just before the next input character
2501          
2502        (($self->{insertion_mode} eq 'in head' and defined $self->{head_element})        $insert->($script_el);
        ? $self->{head_element} : $self->{open_elements}->[-1]->[0])->append_child ($script_el);  
2503                
2504        ## TODO: insertion point = $old_insertion_point (might be "undefined")        ## TODO: insertion point = $old_insertion_point (might be "undefined")
2505                
# Line 2362  sub _tree_construction_main ($) { Line 2693  sub _tree_construction_main ($) {
2693    }; # $formatting_end_tag    }; # $formatting_end_tag
2694    
2695    my $insert_to_current = sub {    my $insert_to_current = sub {
2696      $self->{open_elements}->[-1]->[0]->append_child (shift);      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);
2697    }; # $insert_to_current    }; # $insert_to_current
2698    
2699    my $insert_to_foster = sub {    my $insert_to_foster = sub {
# Line 2396  sub _tree_construction_main ($) { Line 2727  sub _tree_construction_main ($) {
2727                         }                         }
2728    }; # $insert_to_foster    }; # $insert_to_foster
2729    
2730    my $in_body = sub {    my $insert;
     my $insert = shift;  
     if ($token->{type} eq 'start tag') {  
       if ($token->{tag_name} eq 'script') {  
         $script_start_tag->();  
         return;  
       } elsif ($token->{tag_name} eq 'style') {  
         $style_start_tag->();  
         return;  
       } elsif ({  
                 base => 1, link => 1, meta => 1,  
                }->{$token->{tag_name}}) {  
         !!!parse-error (type => 'in body:'.$token->{tag_name});  
         ## NOTE: This is an "as if in head" code clone  
         my $el;  
         !!!create-element ($el, $token->{tag_name}, $token->{attributes});  
         if (defined $self->{head_element}) {  
           $self->{head_element}->append_child ($el);  
         } else {  
           $insert->($el);  
         }  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'title') {  
         !!!parse-error (type => 'in body:title');  
         ## NOTE: There is an "as if in head" code clone  
         my $title_el;  
         !!!create-element ($title_el, 'title', $token->{attributes});  
         (defined $self->{head_element} ? $self->{head_element} : $self->{open_elements}->[-1]->[0])  
           ->append_child ($title_el);  
         $self->{content_model_flag} = 'RCDATA';  
         delete $self->{escape}; # MUST  
           
         my $text = '';  
         !!!next-token;  
         while ($token->{type} eq 'character') {  
           $text .= $token->{data};  
           !!!next-token;  
         }  
         if (length $text) {  
           $title_el->manakai_append_text ($text);  
         }  
           
         $self->{content_model_flag} = 'PCDATA';  
           
         if ($token->{type} eq 'end tag' and  
             $token->{tag_name} eq 'title') {  
           ## Ignore the token  
         } else {  
           !!!parse-error (type => 'in RCDATA:#'.$token->{type});  
           ## ISSUE: And ignore?  
         }  
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'body') {  
         !!!parse-error (type => 'in body:body');  
                 
         if (@{$self->{open_elements}} == 1 or  
             $self->{open_elements}->[1]->[1] ne 'body') {  
           ## Ignore the token  
         } else {  
           my $body_el = $self->{open_elements}->[1]->[0];  
           for my $attr_name (keys %{$token->{attributes}}) {  
             unless ($body_el->has_attribute_ns (undef, $attr_name)) {  
               $body_el->set_attribute_ns  
                 (undef, [undef, $attr_name],  
                  $token->{attributes}->{$attr_name}->{value});  
             }  
           }  
         }  
         !!!next-token;  
         return;  
       } elsif ({  
                 address => 1, blockquote => 1, center => 1, dir => 1,  
                 div => 1, dl => 1, fieldset => 1, listing => 1,  
                 menu => 1, ol => 1, p => 1, ul => 1,  
                 pre => 1,  
                }->{$token->{tag_name}}) {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         if ($token->{tag_name} eq 'pre') {  
           !!!next-token;  
           if ($token->{type} eq 'character') {  
             $token->{data} =~ s/^\x0A//;  
             unless (length $token->{data}) {  
               !!!next-token;  
             }  
           }  
         } else {  
           !!!next-token;  
         }  
         return;  
       } elsif ($token->{tag_name} eq 'form') {  
         if (defined $self->{form_element}) {  
           !!!parse-error (type => 'in form:form');  
           ## Ignore the token  
           !!!next-token;  
           return;  
         } else {  
           ## has a p element in scope  
           INSCOPE: for (reverse @{$self->{open_elements}}) {  
             if ($_->[1] eq 'p') {  
               !!!back-token;  
               $token = {type => 'end tag', tag_name => 'p'};  
               return;  
             } elsif ({  
                       table => 1, caption => 1, td => 1, th => 1,  
                       button => 1, marquee => 1, object => 1, html => 1,  
                      }->{$_->[1]}) {  
               last INSCOPE;  
             }  
           } # INSCOPE  
               
           !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
           $self->{form_element} = $self->{open_elements}->[-1]->[0];  
           !!!next-token;  
           return;  
         }  
       } elsif ($token->{tag_name} eq 'li') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## Step 1  
         my $i = -1;  
         my $node = $self->{open_elements}->[$i];  
         LI: {  
           ## Step 2  
           if ($node->[1] eq 'li') {  
             if ($i != -1) {  
               !!!parse-error (type => 'end tag missing:'.  
                               $self->{open_elements}->[-1]->[1]);  
               ## TODO: test  
             }  
             splice @{$self->{open_elements}}, $i;  
             last LI;  
           }  
             
           ## Step 3  
           if (not $formatting_category->{$node->[1]} and  
               #not $phrasing_category->{$node->[1]} and  
               ($special_category->{$node->[1]} or  
                $scoping_category->{$node->[1]}) and  
               $node->[1] ne 'address' and $node->[1] ne 'div') {  
             last LI;  
           }  
             
           ## Step 4  
           $i--;  
           $node = $self->{open_elements}->[$i];  
           redo LI;  
         } # LI  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'dd' or $token->{tag_name} eq 'dt') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## Step 1  
         my $i = -1;  
         my $node = $self->{open_elements}->[$i];  
         LI: {  
           ## Step 2  
           if ($node->[1] eq 'dt' or $node->[1] eq 'dd') {  
             if ($i != -1) {  
               !!!parse-error (type => 'end tag missing:'.  
                               $self->{open_elements}->[-1]->[1]);  
               ## TODO: test  
             }  
             splice @{$self->{open_elements}}, $i;  
             last LI;  
           }  
             
           ## Step 3  
           if (not $formatting_category->{$node->[1]} and  
               #not $phrasing_category->{$node->[1]} and  
               ($special_category->{$node->[1]} or  
                $scoping_category->{$node->[1]}) and  
               $node->[1] ne 'address' and $node->[1] ne 'div') {  
             last LI;  
           }  
             
           ## Step 4  
           $i--;  
           $node = $self->{open_elements}->[$i];  
           redo LI;  
         } # LI  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'plaintext') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         $self->{content_model_flag} = 'PLAINTEXT';  
             
         !!!next-token;  
         return;  
       } elsif ({  
                 h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
                }->{$token->{tag_name}}) {  
         ## has a p element in scope  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## has an element in scope  
         my $i;  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ({  
                h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
               }->{$node->[1]}) {  
             $i = $_;  
             last INSCOPE;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         if (defined $i) {  
           !!!parse-error (type => 'in hn:hn');  
           splice @{$self->{open_elements}}, $i;  
         }  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'a') {  
         AFE: for my $i (reverse 0..$#$active_formatting_elements) {  
           my $node = $active_formatting_elements->[$i];  
           if ($node->[1] eq 'a') {  
             !!!parse-error (type => 'in a:a');  
               
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'a'};  
             $formatting_end_tag->($token->{tag_name});  
               
             AFE2: for (reverse 0..$#$active_formatting_elements) {  
               if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {  
                 splice @$active_formatting_elements, $_, 1;  
                 last AFE2;  
               }  
             } # AFE2  
             OE: for (reverse 0..$#{$self->{open_elements}}) {  
               if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {  
                 splice @{$self->{open_elements}}, $_, 1;  
                 last OE;  
               }  
             } # OE  
             last AFE;  
           } elsif ($node->[0] eq '#marker') {  
             last AFE;  
           }  
         } # AFE  
             
         $reconstruct_active_formatting_elements->($insert_to_current);  
   
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, $self->{open_elements}->[-1];  
   
         !!!next-token;  
         return;  
       } elsif ({  
                 b => 1, big => 1, em => 1, font => 1, i => 1,  
                 s => 1, small => 1, strile => 1,  
                 strong => 1, tt => 1, u => 1,  
                }->{$token->{tag_name}}) {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, $self->{open_elements}->[-1];  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'nobr') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
   
         ## has a |nobr| element in scope  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq 'nobr') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'nobr'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, $self->{open_elements}->[-1];  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'button') {  
         ## has a button element in scope  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq 'button') {  
             !!!parse-error (type => 'in button:button');  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'button'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         $reconstruct_active_formatting_elements->($insert_to_current);  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, ['#marker', ''];  
   
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'marquee' or  
                $token->{tag_name} eq 'object') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, ['#marker', ''];  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'xmp') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
           
         $self->{content_model_flag} = 'CDATA';  
         delete $self->{escape}; # MUST  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'table') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         $self->{insertion_mode} = 'in table';  
             
         !!!next-token;  
         return;  
       } elsif ({  
                 area => 1, basefont => 1, bgsound => 1, br => 1,  
                 embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,  
                 image => 1,  
                }->{$token->{tag_name}}) {  
         if ($token->{tag_name} eq 'image') {  
           !!!parse-error (type => 'image');  
           $token->{tag_name} = 'img';  
         }  
           
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         pop @{$self->{open_elements}};  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'hr') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         pop @{$self->{open_elements}};  
             
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'input') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         ## TODO: associate with $self->{form_element} if defined  
         pop @{$self->{open_elements}};  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'isindex') {  
         !!!parse-error (type => 'isindex');  
           
         if (defined $self->{form_element}) {  
           ## Ignore the token  
           !!!next-token;  
           return;  
         } else {  
           my $at = $token->{attributes};  
           $at->{name} = {name => 'name', value => 'isindex'};  
           my @tokens = (  
                         {type => 'start tag', tag_name => 'form'},  
                         {type => 'start tag', tag_name => 'hr'},  
                         {type => 'start tag', tag_name => 'p'},  
                         {type => 'start tag', tag_name => 'label'},  
                         {type => 'character',  
                          data => 'This is a searchable index. Insert your search keywords here: '}, # SHOULD  
                         ## TODO: make this configurable  
                         {type => 'start tag', tag_name => 'input', attributes => $at},  
                         #{type => 'character', data => ''}, # SHOULD  
                         {type => 'end tag', tag_name => 'label'},  
                         {type => 'end tag', tag_name => 'p'},  
                         {type => 'start tag', tag_name => 'hr'},  
                         {type => 'end tag', tag_name => 'form'},  
                        );  
           $token = shift @tokens;  
           !!!back-token (@tokens);  
           return;  
         }  
       } elsif ({  
                 textarea => 1,  
                 iframe => 1,  
                 noembed => 1,  
                 noframes => 1,  
                 noscript => 0, ## TODO: 1 if scripting is enabled  
                }->{$token->{tag_name}}) {  
         my $tag_name = $token->{tag_name};  
         my $el;  
         !!!create-element ($el, $token->{tag_name}, $token->{attributes});  
           
         if ($token->{tag_name} eq 'textarea') {  
           ## TODO: $self->{form_element} if defined  
           $self->{content_model_flag} = 'RCDATA';  
         } else {  
           $self->{content_model_flag} = 'CDATA';  
         }  
         delete $self->{escape}; # MUST  
           
         $insert->($el);  
           
         my $text = '';  
         if ($token->{tag_name} eq 'textarea') {  
           !!!next-token;  
           if ($token->{type} eq 'character') {  
             $token->{data} =~ s/^\x0A//;  
             unless (length $token->{data}) {  
               !!!next-token;  
             }  
           }  
         } else {  
           !!!next-token;  
         }  
         while ($token->{type} eq 'character') {  
           $text .= $token->{data};  
           !!!next-token;  
         }  
         if (length $text) {  
           $el->manakai_append_text ($text);  
         }  
           
         $self->{content_model_flag} = 'PCDATA';  
           
         if ($token->{type} eq 'end tag' and  
             $token->{tag_name} eq $tag_name) {  
           ## Ignore the token  
         } else {  
           if ($token->{tag_name} eq 'textarea') {  
             !!!parse-error (type => 'in RCDATA:#'.$token->{type});  
           } else {  
             !!!parse-error (type => 'in CDATA:#'.$token->{type});  
           }  
           ## ISSUE: And ignore?  
         }  
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'select') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
           
         $self->{insertion_mode} = 'in select';  
         !!!next-token;  
         return;  
       } elsif ({  
                 caption => 1, col => 1, colgroup => 1, frame => 1,  
                 frameset => 1, head => 1, option => 1, optgroup => 1,  
                 tbody => 1, td => 1, tfoot => 1, th => 1,  
                 thead => 1, tr => 1,  
                }->{$token->{tag_name}}) {  
         !!!parse-error (type => 'in body:'.$token->{tag_name});  
         ## Ignore the token  
         !!!next-token;  
         return;  
           
         ## ISSUE: An issue on HTML5 new elements in the spec.  
       } else {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
           
         !!!next-token;  
         return;  
       }  
     } elsif ($token->{type} eq 'end tag') {  
       if ($token->{tag_name} eq 'body') {  
         if (@{$self->{open_elements}} > 1 and  
             $self->{open_elements}->[1]->[1] eq 'body') {  
           for (@{$self->{open_elements}}) {  
             unless ({  
                        dd => 1, dt => 1, li => 1, p => 1, td => 1,  
                        th => 1, tr => 1, body => 1, html => 1,  
                     }->{$_->[1]}) {  
               !!!parse-error (type => 'not closed:'.$_->[1]);  
             }  
           }  
   
           $self->{insertion_mode} = 'after body';  
           !!!next-token;  
           return;  
         } else {  
           !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
           ## Ignore the token  
           !!!next-token;  
           return;  
         }  
       } elsif ($token->{tag_name} eq 'html') {  
         if (@{$self->{open_elements}} > 1 and $self->{open_elements}->[1]->[1] eq 'body') {  
           ## ISSUE: There is an issue in the spec.  
           if ($self->{open_elements}->[-1]->[1] ne 'body') {  
             !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);  
           }  
           $self->{insertion_mode} = 'after body';  
           ## reprocess  
           return;  
         } else {  
           !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
           ## Ignore the token  
           !!!next-token;  
           return;  
         }  
       } elsif ({  
                 address => 1, blockquote => 1, center => 1, dir => 1,  
                 div => 1, dl => 1, fieldset => 1, listing => 1,  
                 menu => 1, ol => 1, pre => 1, ul => 1,  
                 p => 1,  
                 dd => 1, dt => 1, li => 1,  
                 button => 1, marquee => 1, object => 1,  
                }->{$token->{tag_name}}) {  
         ## has an element in scope  
         my $i;  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq $token->{tag_name}) {  
             ## generate implied end tags  
             if ({  
                  dd => ($token->{tag_name} ne 'dd'),  
                  dt => ($token->{tag_name} ne 'dt'),  
                  li => ($token->{tag_name} ne 'li'),  
                  p => ($token->{tag_name} ne 'p'),  
                  td => 1, th => 1, tr => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => 'end tag',  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               return;  
             }  
             $i = $_;  
             last INSCOPE unless $token->{tag_name} eq 'p';  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
           
         if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         }  
           
         splice @{$self->{open_elements}}, $i if defined $i;  
         $clear_up_to_marker->()  
           if {  
             button => 1, marquee => 1, object => 1,  
           }->{$token->{tag_name}};  
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'form') {  
         ## has an element in scope  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq $token->{tag_name}) {  
             ## generate implied end tags  
             if ({  
                  dd => 1, dt => 1, li => 1, p => 1,  
                  td => 1, th => 1, tr => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => 'end tag',  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               return;  
             }  
             last INSCOPE;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
           
         if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {  
           pop @{$self->{open_elements}};  
         } else {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         }  
   
         undef $self->{form_element};  
         !!!next-token;  
         return;  
       } elsif ({  
                 h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
                }->{$token->{tag_name}}) {  
         ## has an element in scope  
         my $i;  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ({  
                h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
               }->{$node->[1]}) {  
             ## generate implied end tags  
             if ({  
                  dd => 1, dt => 1, li => 1, p => 1,  
                  td => 1, th => 1, tr => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => 'end tag',  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               return;  
             }  
             $i = $_;  
             last INSCOPE;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
           
         if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         }  
           
         splice @{$self->{open_elements}}, $i if defined $i;  
         !!!next-token;  
         return;  
       } elsif ({  
                 a => 1,  
                 b => 1, big => 1, em => 1, font => 1, i => 1,  
                 nobr => 1, s => 1, small => 1, strile => 1,  
                 strong => 1, tt => 1, u => 1,  
                }->{$token->{tag_name}}) {  
         $formatting_end_tag->($token->{tag_name});  
 ## TODO: <http://html5.org/tools/web-apps-tracker?from=883&to=884>  
         return;  
       } elsif ({  
                 caption => 1, col => 1, colgroup => 1, frame => 1,  
                 frameset => 1, head => 1, option => 1, optgroup => 1,  
                 tbody => 1, td => 1, tfoot => 1, th => 1,  
                 thead => 1, tr => 1,  
                 area => 1, basefont => 1, bgsound => 1, br => 1,  
                 embed => 1, hr => 1, iframe => 1, image => 1,  
                 img => 1, input => 1, isindex => 1, noembed => 1,  
                 noframes => 1, param => 1, select => 1, spacer => 1,  
                 table => 1, textarea => 1, wbr => 1,  
                 noscript => 0, ## TODO: if scripting is enabled  
                }->{$token->{tag_name}}) {  
         !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
         ## Ignore the token  
         !!!next-token;  
         return;  
           
         ## ISSUE: Issue on HTML5 new elements in spec  
           
       } else {  
         ## Step 1  
         my $node_i = -1;  
         my $node = $self->{open_elements}->[$node_i];  
   
         ## Step 2  
         S2: {  
           if ($node->[1] eq $token->{tag_name}) {  
             ## Step 1  
             ## generate implied end tags  
             if ({  
                  dd => 1, dt => 1, li => 1, p => 1,  
                  td => 1, th => 1, tr => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => 'end tag',  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               return;  
             }  
           
             ## Step 2  
             if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {  
               !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
             }  
               
             ## Step 3  
             splice @{$self->{open_elements}}, $node_i;  
   
             !!!next-token;  
             last S2;  
           } else {  
             ## Step 3  
             if (not $formatting_category->{$node->[1]} and  
                 #not $phrasing_category->{$node->[1]} and  
                 ($special_category->{$node->[1]} or  
                  $scoping_category->{$node->[1]})) {  
               !!!parse-error (type => 'not closed:'.$node->[1]);  
               ## Ignore the token  
               !!!next-token;  
               last S2;  
             }  
           }  
             
           ## Step 4  
           $node_i--;  
           $node = $self->{open_elements}->[$node_i];  
             
           ## Step 5;  
           redo S2;  
         } # S2  
         return;  
       }  
     }  
   }; # $in_body  
2731    
2732    B: {    B: {
2733      if ($phase eq 'main') {      if ($token->{type} == DOCTYPE_TOKEN) {
2734        if ($token->{type} eq 'DOCTYPE') {        !!!parse-error (type => 'DOCTYPE in the middle');
2735          !!!parse-error (type => 'in html:#DOCTYPE');        ## Ignore the token
2736          ## Ignore the token        ## Stay in the phase
2737          ## Stay in the phase        !!!next-token;
2738          !!!next-token;        redo B;
2739          redo B;      } elsif ($token->{type} == END_OF_FILE_TOKEN) {
2740        } elsif ($token->{type} eq 'start tag' and        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
2741                 $token->{tag_name} eq 'html') {          #
2742          ## TODO: unless it is the first start tag token, parse-error        } else {
         my $top_el = $self->{open_elements}->[0]->[0];  
         for my $attr_name (keys %{$token->{attributes}}) {  
           unless ($top_el->has_attribute_ns (undef, $attr_name)) {  
             $top_el->set_attribute_ns  
               (undef, [undef, $attr_name],  
                $token->{attributes}->{$attr_name}->{value});  
           }  
         }  
         !!!next-token;  
         redo B;  
       } elsif ($token->{type} eq 'end-of-file') {  
2743          ## Generate implied end tags          ## Generate implied end tags
2744          if ({          if ({
2745               dd => 1, dt => 1, li => 1, p => 1, td => 1, th => 1, tr => 1,               dd => 1, dt => 1, li => 1, p => 1, td => 1, th => 1, tr => 1,
2746                 tbody => 1, tfoot=> 1, thead => 1,
2747              }->{$self->{open_elements}->[-1]->[1]}) {              }->{$self->{open_elements}->[-1]->[1]}) {
2748            !!!back-token;            !!!back-token;
2749            $token = {type => 'end tag', tag_name => $self->{open_elements}->[-1]->[1]};            $token = {type => END_TAG_TOKEN, tag_name => $self->{open_elements}->[-1]->[1]};
2750            redo B;            redo B;
2751          }          }
2752                    
# Line 3259  sub _tree_construction_main ($) { Line 2759  sub _tree_construction_main ($) {
2759            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
2760          }          }
2761    
         ## Stop parsing  
         last B;  
   
2762          ## ISSUE: There is an issue in the spec.          ## ISSUE: There is an issue in the spec.
2763          }
2764    
2765          ## Stop parsing
2766          last B;
2767        } elsif ($token->{type} == START_TAG_TOKEN and
2768                 $token->{tag_name} eq 'html') {
2769          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
2770            ## Turn into the main phase
2771            !!!parse-error (type => 'after html:html');
2772            $self->{insertion_mode} = AFTER_BODY_IM;
2773          } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
2774            ## Turn into the main phase
2775            !!!parse-error (type => 'after html:html');
2776            $self->{insertion_mode} = AFTER_FRAMESET_IM;
2777          }
2778    
2779    ## ISSUE: "aa<html>" is not a parse error.
2780    ## ISSUE: "<html>" in fragment is not a parse error.
2781          unless ($token->{first_start_tag}) {
2782            !!!parse-error (type => 'not first start tag');
2783          }
2784          my $top_el = $self->{open_elements}->[0]->[0];
2785          for my $attr_name (keys %{$token->{attributes}}) {
2786            unless ($top_el->has_attribute_ns (undef, $attr_name)) {
2787              $top_el->set_attribute_ns
2788                (undef, [undef, $attr_name],
2789                 $token->{attributes}->{$attr_name}->{value});
2790            }
2791          }
2792          !!!next-token;
2793          redo B;
2794        } elsif ($token->{type} == COMMENT_TOKEN) {
2795          my $comment = $self->{document}->create_comment ($token->{data});
2796          if ($self->{insertion_mode} & AFTER_HTML_IMS) {
2797            $self->{document}->append_child ($comment);
2798          } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {
2799            $self->{open_elements}->[0]->[0]->append_child ($comment);
2800        } else {        } else {
2801          if ($self->{insertion_mode} eq 'before head') {          $self->{open_elements}->[-1]->[0]->append_child ($comment);
2802            if ($token->{type} eq 'character') {        }
2803              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {        !!!next-token;
2804                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);        redo B;
2805                unless (length $token->{data}) {      } elsif ($self->{insertion_mode} & HEAD_IMS) {
2806                  !!!next-token;        if ($token->{type} == CHARACTER_TOKEN) {
2807                  redo B;          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
2808                }            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
2809              }            unless (length $token->{data}) {
             ## As if <head>  
             !!!create-element ($self->{head_element}, 'head');  
             $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});  
             push @{$self->{open_elements}}, [$self->{head_element}, 'head'];  
             $self->{insertion_mode} = 'in head';  
             ## reprocess  
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
2810              !!!next-token;              !!!next-token;
2811              redo B;              redo B;
2812            } elsif ($token->{type} eq 'start tag') {            }
2813              my $attr = $token->{tag_name} eq 'head' ? $token->{attributes} : {};          }
2814              !!!create-element ($self->{head_element}, 'head', $attr);  
2815              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2816              push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            ## As if <head>
2817              $self->{insertion_mode} = 'in head';            !!!create-element ($self->{head_element}, 'head');
2818              if ($token->{tag_name} eq 'head') {            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2819                !!!next-token;            push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2820              #} elsif ({  
2821              #          base => 1, link => 1, meta => 1,            ## Reprocess in the "in head" insertion mode...
2822              #          script => 1, style => 1, title => 1,            pop @{$self->{open_elements}};
2823              #         }->{$token->{tag_name}}) {  
2824              #  ## reprocess            ## Reprocess in the "after head" insertion mode...
2825              } else {          } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2826                ## reprocess            ## As if </noscript>
2827              }            pop @{$self->{open_elements}};
2828              !!!parse-error (type => 'in noscript:#character');
2829              
2830              ## Reprocess in the "in head" insertion mode...
2831              ## As if </head>
2832              pop @{$self->{open_elements}};
2833    
2834              ## Reprocess in the "after head" insertion mode...
2835            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2836              pop @{$self->{open_elements}};
2837    
2838              ## Reprocess in the "after head" insertion mode...
2839            }
2840    
2841                ## "after head" insertion mode
2842                ## As if <body>
2843                !!!insert-element ('body');
2844                $self->{insertion_mode} = IN_BODY_IM;
2845                ## reprocess
2846              redo B;              redo B;
2847            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == START_TAG_TOKEN) {
2848              if ($token->{tag_name} eq 'html') {              if ($token->{tag_name} eq 'head') {
2849                  if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2850                    !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});
2851                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2852                    push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];
2853                    $self->{insertion_mode} = IN_HEAD_IM;
2854                    !!!next-token;
2855                    redo B;
2856                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
2857                    #
2858                  } else {
2859                    !!!parse-error (type => 'in head:head'); # or in head noscript
2860                    ## Ignore the token
2861                    !!!next-token;
2862                    redo B;
2863                  }
2864                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2865                ## As if <head>                ## As if <head>
2866                !!!create-element ($self->{head_element}, 'head');                !!!create-element ($self->{head_element}, 'head');
2867                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2868                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2869                $self->{insertion_mode} = 'in head';  
2870                ## reprocess                $self->{insertion_mode} = IN_HEAD_IM;
2871                redo B;                ## Reprocess in the "in head" insertion mode...
             } else {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
2872              }              }
2873            } else {  
2874              die "$0: $token->{type}: Unknown type";              if ($token->{tag_name} eq 'base') {
2875            }                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2876          } elsif ($self->{insertion_mode} eq 'in head') {                  ## As if </noscript>
2877            if ($token->{type} eq 'character') {                  pop @{$self->{open_elements}};
2878              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {                  !!!parse-error (type => 'in noscript:base');
2879                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                
2880                unless (length $token->{data}) {                  $self->{insertion_mode} = IN_HEAD_IM;
2881                  !!!next-token;                  ## Reprocess in the "in head" insertion mode...
                 redo B;  
2882                }                }
             }  
               
             #  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'title') {  
               ## NOTE: There is an "as if in head" code clone  
               my $title_el;  
               !!!create-element ($title_el, 'title', $token->{attributes});  
               (defined $self->{head_element} ? $self->{head_element} : $self->{open_elements}->[-1]->[0])  
                 ->append_child ($title_el);  
               $self->{content_model_flag} = 'RCDATA';  
               delete $self->{escape}; # MUST  
2883    
2884                my $text = '';                ## NOTE: There is a "as if in head" code clone.
2885                  if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2886                    !!!parse-error (type => 'after head:'.$token->{tag_name});
2887                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2888                  }
2889                  !!!insert-element ($token->{tag_name}, $token->{attributes});
2890                  pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
2891                  pop @{$self->{open_elements}}
2892                      if $self->{insertion_mode} == AFTER_HEAD_IM;
2893                !!!next-token;                !!!next-token;
2894                while ($token->{type} eq 'character') {                redo B;
2895                  $text .= $token->{data};              } elsif ($token->{tag_name} eq 'link') {
2896                  !!!next-token;                ## NOTE: There is a "as if in head" code clone.
2897                  if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2898                    !!!parse-error (type => 'after head:'.$token->{tag_name});
2899                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2900                }                }
2901                if (length $text) {                !!!insert-element ($token->{tag_name}, $token->{attributes});
2902                  $title_el->manakai_append_text ($text);                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
2903                  pop @{$self->{open_elements}}
2904                      if $self->{insertion_mode} == AFTER_HEAD_IM;
2905                  !!!next-token;
2906                  redo B;
2907                } elsif ($token->{tag_name} eq 'meta') {
2908                  ## NOTE: There is a "as if in head" code clone.
2909                  if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2910                    !!!parse-error (type => 'after head:'.$token->{tag_name});
2911                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2912                }                }
2913                                !!!insert-element ($token->{tag_name}, $token->{attributes});
2914                $self->{content_model_flag} = 'PCDATA';                my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
2915                  
2916                if ($token->{type} eq 'end tag' and                unless ($self->{confident}) {
2917                    $token->{tag_name} eq 'title') {                  if ($token->{attributes}->{charset}) { ## TODO: And if supported
2918                  ## Ignore the token                    $self->{change_encoding}
2919                          ->($self, $token->{attributes}->{charset}->{value});
2920                      
2921                      $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
2922                          ->set_user_data (manakai_has_reference =>
2923                                               $token->{attributes}->{charset}
2924                                                   ->{has_reference});
2925                    } elsif ($token->{attributes}->{content}) {
2926                      ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.
2927                      if ($token->{attributes}->{content}->{value}
2928                          =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=
2929                              [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
2930                              ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
2931                        $self->{change_encoding}
2932                            ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);
2933                        $meta_el->[0]->get_attribute_node_ns (undef, 'content')
2934                            ->set_user_data (manakai_has_reference =>
2935                                                 $token->{attributes}->{content}
2936                                                       ->{has_reference});
2937                      }
2938                    }
2939                } else {                } else {
2940                  !!!parse-error (type => 'in RCDATA:#'.$token->{type});                  if ($token->{attributes}->{charset}) {
2941                  ## ISSUE: And ignore?                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
2942                          ->set_user_data (manakai_has_reference =>
2943                                               $token->{attributes}->{charset}
2944                                                   ->{has_reference});
2945                    }
2946                    if ($token->{attributes}->{content}) {
2947                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')
2948                          ->set_user_data (manakai_has_reference =>
2949                                               $token->{attributes}->{content}
2950                                                   ->{has_reference});
2951                    }
2952                }                }
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'style') {  
               $style_start_tag->();  
               redo B;  
             } elsif ($token->{tag_name} eq 'script') {  
               $script_start_tag->();  
               redo B;  
             } elsif ({base => 1, link => 1, meta => 1}->{$token->{tag_name}}) {  
               ## NOTE: There are "as if in head" code clones  
               my $el;  
               !!!create-element ($el, $token->{tag_name}, $token->{attributes});  
               (defined $self->{head_element} ? $self->{head_element} : $self->{open_elements}->[-1]->[0])  
                 ->append_child ($el);  
2953    
2954                  pop @{$self->{open_elements}}
2955                      if $self->{insertion_mode} == AFTER_HEAD_IM;
2956                !!!next-token;                !!!next-token;
2957                redo B;                redo B;
2958              } elsif ($token->{tag_name} eq 'head') {              } elsif ($token->{tag_name} eq 'title') {
2959                !!!parse-error (type => 'in head:head');                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2960                ## Ignore the token                  ## As if </noscript>
2961                !!!next-token;                  pop @{$self->{open_elements}};
2962                    !!!parse-error (type => 'in noscript:title');
2963                  
2964                    $self->{insertion_mode} = IN_HEAD_IM;
2965                    ## Reprocess in the "in head" insertion mode...
2966                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
2967                    !!!parse-error (type => 'after head:'.$token->{tag_name});
2968                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2969                  }
2970    
2971                  ## NOTE: There is a "as if in head" code clone.
2972                  my $parent = defined $self->{head_element} ? $self->{head_element}
2973                      : $self->{open_elements}->[-1]->[0];
2974                  $parse_rcdata->(RCDATA_CONTENT_MODEL,
2975                                  sub { $parent->append_child ($_[0]) });
2976                  pop @{$self->{open_elements}}
2977                      if $self->{insertion_mode} == AFTER_HEAD_IM;
2978                redo B;                redo B;
2979              } else {              } elsif ($token->{tag_name} eq 'style') {
2980                #                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
2981              }                ## insertion mode IN_HEAD_IM)
2982            } elsif ($token->{type} eq 'end tag') {                ## NOTE: There is a "as if in head" code clone.
2983              if ($token->{tag_name} eq 'head') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2984                if ($self->{open_elements}->[-1]->[1] eq 'head') {                  !!!parse-error (type => 'after head:'.$token->{tag_name});
2985                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2986                  }
2987                  $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);
2988                  pop @{$self->{open_elements}}
2989                      if $self->{insertion_mode} == AFTER_HEAD_IM;
2990                  redo B;
2991                } elsif ($token->{tag_name} eq 'noscript') {
2992                  if ($self->{insertion_mode} == IN_HEAD_IM) {
2993                    ## NOTE: and scripting is disalbed
2994                    !!!insert-element ($token->{tag_name}, $token->{attributes});
2995                    $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
2996                    !!!next-token;
2997                    redo B;
2998                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2999                    !!!parse-error (type => 'in noscript:noscript');
3000                    ## Ignore the token
3001                    !!!next-token;
3002                    redo B;
3003                  } else {
3004                    #
3005                  }
3006                } elsif ($token->{tag_name} eq 'script') {
3007                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3008                    ## As if </noscript>
3009                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3010                    !!!parse-error (type => 'in noscript:script');
3011                  
3012                    $self->{insertion_mode} = IN_HEAD_IM;
3013                    ## Reprocess in the "in head" insertion mode...
3014                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
3015                    !!!parse-error (type => 'after head:'.$token->{tag_name});
3016                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3017                  }
3018    
3019                  ## NOTE: There is a "as if in head" code clone.
3020                  $script_start_tag->($insert_to_current);
3021                  pop @{$self->{open_elements}}
3022                      if $self->{insertion_mode} == AFTER_HEAD_IM;
3023                  redo B;
3024                } elsif ($token->{tag_name} eq 'body' or
3025                         $token->{tag_name} eq 'frameset') {
3026                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3027                    ## As if </noscript>
3028                    pop @{$self->{open_elements}};
3029                    !!!parse-error (type => 'in noscript:'.$token->{tag_name});
3030                    
3031                    ## Reprocess in the "in head" insertion mode...
3032                    ## As if </head>
3033                    pop @{$self->{open_elements}};
3034                    
3035                    ## Reprocess in the "after head" insertion mode...
3036                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3037                    pop @{$self->{open_elements}};
3038                    
3039                    ## Reprocess in the "after head" insertion mode...
3040                  }
3041    
3042                  ## "after head" insertion mode
3043                  !!!insert-element ($token->{tag_name}, $token->{attributes});
3044                  if ($token->{tag_name} eq 'body') {
3045                    $self->{insertion_mode} = IN_BODY_IM;
3046                  } elsif ($token->{tag_name} eq 'frameset') {
3047                    $self->{insertion_mode} = IN_FRAMESET_IM;
3048                } else {                } else {
3049                  !!!parse-error (type => 'unmatched end tag:head');                  die "$0: tag name: $self->{tag_name}";
3050                }                }
               $self->{insertion_mode} = 'after head';  
3051                !!!next-token;                !!!next-token;
3052                redo B;                redo B;
             } elsif ($token->{tag_name} eq 'html') {  
               #  
3053              } else {              } else {
3054                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                #
               ## Ignore the token  
               !!!next-token;  
               redo B;  
3055              }              }
           } else {  
             #  
           }  
3056    
3057            if ($self->{open_elements}->[-1]->[1] eq 'head') {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3058              ## As if </head>                ## As if </noscript>
3059              pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3060            }                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});
3061            $self->{insertion_mode} = 'after head';                
3062            ## reprocess                ## Reprocess in the "in head" insertion mode...
3063            redo B;                ## As if </head>
3064                  pop @{$self->{open_elements}};
3065    
3066            ## ISSUE: An issue in the spec.                ## Reprocess in the "after head" insertion mode...
3067          } elsif ($self->{insertion_mode} eq 'after head') {              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3068            if ($token->{type} eq 'character') {                ## As if </head>
3069              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {                pop @{$self->{open_elements}};
3070                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
3071                unless (length $token->{data}) {                ## Reprocess in the "after head" insertion mode...
                 !!!next-token;  
                 redo B;  
               }  
3072              }              }
               
             #  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'body') {  
               !!!insert-element ('body', $token->{attributes});  
               $self->{insertion_mode} = 'in body';  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'frameset') {  
               !!!insert-element ('frameset', $token->{attributes});  
               $self->{insertion_mode} = 'in frameset';  
               !!!next-token;  
               redo B;  
             } elsif ({  
                       base => 1, link => 1, meta => 1,  
                       script => 1, style => 1, title => 1,  
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'after head:'.$token->{tag_name});  
               $self->{insertion_mode} = 'in head';  
               ## reprocess  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
             
           ## As if <body>  
           !!!insert-element ('body');  
           $self->{insertion_mode} = 'in body';  
           ## reprocess  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'in body') {  
           if ($token->{type} eq 'character') {  
             ## NOTE: There is a code clone of "character in body".  
             $reconstruct_active_formatting_elements->($insert_to_current);  
               
             $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
3073    
3074              !!!next-token;              ## "after head" insertion mode
3075              redo B;              ## As if <body>
3076            } elsif ($token->{type} eq 'comment') {              !!!insert-element ('body');
3077              ## NOTE: There is a code clone of "comment in body".              $self->{insertion_mode} = IN_BODY_IM;
3078              my $comment = $self->{document}->create_comment ($token->{data});              ## reprocess
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } else {  
             $in_body->($insert_to_current);  
3079              redo B;              redo B;
3080            }            } elsif ($token->{type} == END_TAG_TOKEN) {
3081          } elsif ($self->{insertion_mode} eq 'in table') {              if ($token->{tag_name} eq 'head') {
3082            if ($token->{type} eq 'character') {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3083              ## NOTE: There are "character in table" code clones.                  ## As if <head>
3084              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {                  !!!create-element ($self->{head_element}, 'head');
3085                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3086                                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3087                unless (length $token->{data}) {  
3088                    ## Reprocess in the "in head" insertion mode...
3089                    pop @{$self->{open_elements}};
3090                    $self->{insertion_mode} = AFTER_HEAD_IM;
3091                    !!!next-token;
3092                    redo B;
3093                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3094                    ## As if </noscript>
3095                    pop @{$self->{open_elements}};
3096                    !!!parse-error (type => 'in noscript:script');
3097                    
3098                    ## Reprocess in the "in head" insertion mode...
3099                    pop @{$self->{open_elements}};
3100                    $self->{insertion_mode} = AFTER_HEAD_IM;
3101                    !!!next-token;
3102                    redo B;
3103                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3104                    pop @{$self->{open_elements}};
3105                    $self->{insertion_mode} = AFTER_HEAD_IM;
3106                  !!!next-token;                  !!!next-token;
3107                  redo B;                  redo B;
               }  
             }  
   
             !!!parse-error (type => 'in table:#character');  
   
             ## As if in body, but insert into foster parent element  
             ## ISSUE: Spec says that "whenever a node would be inserted  
             ## into the current node" while characters might not be  
             ## result in a new Text node.  
             $reconstruct_active_formatting_elements->($insert_to_foster);  
               
             if ({  
                  table => 1, tbody => 1, tfoot => 1,  
                  thead => 1, tr => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               # MUST  
               my $foster_parent_element;  
               my $next_sibling;  
               my $prev_sibling;  
               OE: for (reverse 0..$#{$self->{open_elements}}) {  
                 if ($self->{open_elements}->[$_]->[1] eq 'table') {  
                   my $parent = $self->{open_elements}->[$_]->[0]->parent_node;  
                   if (defined $parent and $parent->node_type == 1) {  
                     $foster_parent_element = $parent;  
                     $next_sibling = $self->{open_elements}->[$_]->[0];  
                     $prev_sibling = $next_sibling->previous_sibling;  
                   } else {  
                     $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];  
                     $prev_sibling = $foster_parent_element->last_child;  
                   }  
                   last OE;  
                 }  
               } # OE  
               $foster_parent_element = $self->{open_elements}->[0]->[0] and  
               $prev_sibling = $foster_parent_element->last_child  
                 unless defined $foster_parent_element;  
               if (defined $prev_sibling and  
                   $prev_sibling->node_type == 3) {  
                 $prev_sibling->manakai_append_text ($token->{data});  
3108                } else {                } else {
3109                  $foster_parent_element->insert_before                  #
                   ($self->{document}->create_text_node ($token->{data}),  
                    $next_sibling);  
3110                }                }
3111              } else {              } elsif ($token->{tag_name} eq 'noscript') {
3112                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
             }  
               
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ({  
                  caption => 1,  
                  colgroup => 1,  
                  tbody => 1, tfoot => 1, thead => 1,  
                 }->{$token->{tag_name}}) {  
               ## Clear back to table context  
               while ($self->{open_elements}->[-1]->[1] ne 'table' and  
                      $self->{open_elements}->[-1]->[1] ne 'html') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
3113                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3114                    $self->{insertion_mode} = IN_HEAD_IM;
3115                    !!!next-token;
3116                    redo B;
3117                  } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3118                    !!!parse-error (type => 'unmatched end tag:noscript');
3119                    ## Ignore the token ## ISSUE: An issue in the spec.
3120                    !!!next-token;
3121                    redo B;
3122                  } else {
3123                    #
3124                }                }
   
               push @$active_formatting_elements, ['#marker', '']  
                 if $token->{tag_name} eq 'caption';  
   
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               $self->{insertion_mode} = {  
                                  caption => 'in caption',  
                                  colgroup => 'in column group',  
                                  tbody => 'in table body',  
                                  tfoot => 'in table body',  
                                  thead => 'in table body',  
                                 }->{$token->{tag_name}};  
               !!!next-token;  
               redo B;  
3125              } elsif ({              } elsif ({
3126                        col => 1,                        body => 1, html => 1,
                       td => 1, th => 1, tr => 1,  
3127                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3128                ## Clear back to table context                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3129                while ($self->{open_elements}->[-1]->[1] ne 'table' and                  ## As if <head>
3130                       $self->{open_elements}->[-1]->[1] ne 'html') {                  !!!create-element ($self->{head_element}, 'head');
3131                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3132                  pop @{$self->{open_elements}};                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3133                }  
3134                    $self->{insertion_mode} = IN_HEAD_IM;
3135                !!!insert-element ($token->{tag_name} eq 'col' ? 'colgroup' : 'tbody');                  ## Reprocess in the "in head" insertion mode...
3136                $self->{insertion_mode} = $token->{tag_name} eq 'col'                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3137                  ? 'in column group' : 'in table body';                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3138                ## reprocess                  ## Ignore the token
               redo B;  
             } elsif ($token->{tag_name} eq 'table') {  
               ## NOTE: There are code clones for this "table in table"  
               !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
   
               ## As if </table>  
               ## have a table element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'table') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:table');  
                 ## Ignore tokens </table><table>  
3139                  !!!next-token;                  !!!next-token;
3140                  redo B;                  redo B;
3141                }                }
3142                                
3143                ## generate implied end tags                #
3144                if ({              } elsif ({
3145                     dd => 1, dt => 1, li => 1, p => 1,                        p => 1, br => 1,
3146                     td => 1, th => 1, tr => 1,                       }->{$token->{tag_name}}) {
3147                    }->{$self->{open_elements}->[-1]->[1]}) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3148                  !!!back-token; # <table>                  ## As if <head>
3149                  $token = {type => 'end tag', tag_name => 'table'};                  !!!create-element ($self->{head_element}, 'head');
3150                  !!!back-token;                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3151                  $token = {type => 'end tag',                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
3152    
3153                if ($self->{open_elements}->[-1]->[1] ne 'table') {                  $self->{insertion_mode} = IN_HEAD_IM;
3154                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  ## Reprocess in the "in head" insertion mode...
3155                }                }
3156    
               splice @{$self->{open_elements}}, $i;  
   
               $self->_reset_insertion_mode;  
   
               ## reprocess  
               redo B;  
             } else {  
3157                #                #
3158              }              } else {
3159            } elsif ($token->{type} eq 'end tag') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3160              if ($token->{tag_name} eq 'table') {                  #
3161                ## have a table element in table scope                } else {
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
3162                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3163                  ## Ignore the token                  ## Ignore the token
3164                  !!!next-token;                  !!!next-token;
3165                  redo B;                  redo B;
3166                }                }
3167                              }
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
   
               if ($self->{open_elements}->[-1]->[1] ne 'table') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               }  
3168    
3169                splice @{$self->{open_elements}}, $i;              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3170                  ## As if </noscript>
3171                  pop @{$self->{open_elements}};
3172                  !!!parse-error (type => 'in noscript:/'.$token->{tag_name});
3173                  
3174                  ## Reprocess in the "in head" insertion mode...
3175                  ## As if </head>
3176                  pop @{$self->{open_elements}};
3177    
3178                $self->_reset_insertion_mode;                ## Reprocess in the "after head" insertion mode...
3179                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3180                  ## As if </head>
3181                  pop @{$self->{open_elements}};
3182    
3183                !!!next-token;                ## Reprocess in the "after head" insertion mode...
3184                redo B;              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
             } elsif ({  
                       body => 1, caption => 1, col => 1, colgroup => 1,  
                       html => 1, tbody => 1, td => 1, tfoot => 1, th => 1,  
                       thead => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
3185                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3186                ## Ignore the token                ## Ignore the token ## ISSUE: An issue in the spec.
3187                !!!next-token;                !!!next-token;
3188                redo B;                redo B;
             } else {  
               #  
3189              }              }
3190    
3191                ## "after head" insertion mode
3192                ## As if <body>
3193                !!!insert-element ('body');
3194                $self->{insertion_mode} = IN_BODY_IM;
3195                ## reprocess
3196                redo B;
3197            } else {            } else {
3198              #              die "$0: $token->{type}: Unknown token type";
3199            }            }
3200    
3201            !!!parse-error (type => 'in table:'.$token->{tag_name});            ## ISSUE: An issue in the spec.
3202            $in_body->($insert_to_foster);      } elsif ($self->{insertion_mode} & BODY_IMS) {
3203            redo B;            if ($token->{type} == CHARACTER_TOKEN) {
3204          } elsif ($self->{insertion_mode} eq 'in caption') {              ## NOTE: There is a code clone of "character in body".
           if ($token->{type} eq 'character') {  
             ## NOTE: This is a code clone of "character in body".  
3205              $reconstruct_active_formatting_elements->($insert_to_current);              $reconstruct_active_formatting_elements->($insert_to_current);
3206                            
3207              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
3208    
3209              !!!next-token;              !!!next-token;
3210              redo B;              redo B;
3211            } elsif ($token->{type} eq 'comment') {            } elsif ($token->{type} == START_TAG_TOKEN) {
             ## NOTE: This is a code clone of "comment in body".  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
3212              if ({              if ({
3213                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
3214                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,
3215                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
3216                !!!parse-error (type => 'not closed:caption');                if ($self->{insertion_mode} == IN_CELL_IM) {
3217                    ## have an element in table scope
3218                    my $tn;
3219                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3220                      my $node = $self->{open_elements}->[$_];
3221                      if ($node->[1] eq 'td' or $node->[1] eq 'th') {
3222                        $tn = $node->[1];
3223                        last INSCOPE;
3224                      } elsif ({
3225                                table => 1, html => 1,
3226                               }->{$node->[1]}) {
3227                        last INSCOPE;
3228                      }
3229                    } # INSCOPE
3230                      unless (defined $tn) {
3231                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3232                        ## Ignore the token
3233                        !!!next-token;
3234                        redo B;
3235                      }
3236                    
3237                    ## Close the cell
3238                    !!!back-token; # <?>
3239                    $token = {type => END_TAG_TOKEN, tag_name => $tn};
3240                    redo B;
3241                  } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
3242                    !!!parse-error (type => 'not closed:caption');
3243                    
3244                    ## As if </caption>
3245                    ## have a table element in table scope
3246                    my $i;
3247                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3248                      my $node = $self->{open_elements}->[$_];
3249                      if ($node->[1] eq 'caption') {
3250                        $i = $_;
3251                        last INSCOPE;
3252                      } elsif ({
3253                                table => 1, html => 1,
3254                               }->{$node->[1]}) {
3255                        last INSCOPE;
3256                      }
3257                    } # INSCOPE
3258                      unless (defined $i) {
3259                        !!!parse-error (type => 'unmatched end tag:caption');
3260                        ## Ignore the token
3261                        !!!next-token;
3262                        redo B;
3263                      }
3264                    
3265                    ## generate implied end tags
3266                    if ({
3267                         dd => 1, dt => 1, li => 1, p => 1,
3268                         td => 1, th => 1, tr => 1,
3269                         tbody => 1, tfoot=> 1, thead => 1,
3270                        }->{$self->{open_elements}->[-1]->[1]}) {
3271                      !!!back-token; # <?>
3272                      $token = {type => END_TAG_TOKEN, tag_name => 'caption'};
3273                      !!!back-token;
3274                      $token = {type => END_TAG_TOKEN,
3275                                tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3276                      redo B;
3277                    }
3278    
3279                ## As if </caption>                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {
3280                ## have a table element in table scope                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'caption') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
3281                  }                  }
3282                } # INSCOPE                  
3283                unless (defined $i) {                  splice @{$self->{open_elements}}, $i;
3284                  !!!parse-error (type => 'unmatched end tag:caption');                  
3285                    $clear_up_to_marker->();
3286                    
3287                    $self->{insertion_mode} = IN_TABLE_IM;
3288                    
3289                    ## reprocess
3290                    redo B;
3291                  } else {
3292                    #
3293                  }
3294                } else {
3295                  #
3296                }
3297              } elsif ($token->{type} == END_TAG_TOKEN) {
3298                if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {
3299                  if ($self->{insertion_mode} == IN_CELL_IM) {
3300                    ## have an element in table scope
3301                    my $i;
3302                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3303                      my $node = $self->{open_elements}->[$_];
3304                      if ($node->[1] eq $token->{tag_name}) {
3305                        $i = $_;
3306                        last INSCOPE;
3307                      } elsif ({
3308                                table => 1, html => 1,
3309                               }->{$node->[1]}) {
3310                        last INSCOPE;
3311                      }
3312                    } # INSCOPE
3313                      unless (defined $i) {
3314                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3315                        ## Ignore the token
3316                        !!!next-token;
3317                        redo B;
3318                      }
3319                    
3320                    ## generate implied end tags
3321                    if ({
3322                         dd => 1, dt => 1, li => 1, p => 1,
3323                         td => ($token->{tag_name} eq 'th'),
3324                         th => ($token->{tag_name} eq 'td'),
3325                         tr => 1,
3326                         tbody => 1, tfoot=> 1, thead => 1,
3327                        }->{$self->{open_elements}->[-1]->[1]}) {
3328                      !!!back-token;
3329                      $token = {type => END_TAG_TOKEN,
3330                                tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3331                      redo B;
3332                    }
3333                    
3334                    if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
3335                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3336                    }
3337                    
3338                    splice @{$self->{open_elements}}, $i;
3339                    
3340                    $clear_up_to_marker->();
3341                    
3342                    $self->{insertion_mode} = IN_ROW_IM;
3343                    
3344                    !!!next-token;
3345                    redo B;
3346                  } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
3347                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3348                  ## Ignore the token                  ## Ignore the token
3349                  !!!next-token;                  !!!next-token;
3350                  redo B;                  redo B;
3351                  } else {
3352                    #
3353                }                }
3354                              } elsif ($token->{tag_name} eq 'caption') {
3355                ## generate implied end tags                if ($self->{insertion_mode} == IN_CAPTION_IM) {
3356                if ({                  ## have a table element in table scope
3357                     dd => 1, dt => 1, li => 1, p => 1,                  my $i;
3358                     td => 1, th => 1, tr => 1,                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3359                    }->{$self->{open_elements}->[-1]->[1]}) {                    my $node = $self->{open_elements}->[$_];
3360                  !!!back-token; # <?>                    if ($node->[1] eq $token->{tag_name}) {
3361                  $token = {type => 'end tag', tag_name => 'caption'};                      $i = $_;
3362                  !!!back-token;                      last INSCOPE;
3363                  $token = {type => 'end tag',                    } elsif ({
3364                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                              table => 1, html => 1,
3365                               }->{$node->[1]}) {
3366                        last INSCOPE;
3367                      }
3368                    } # INSCOPE
3369                      unless (defined $i) {
3370                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3371                        ## Ignore the token
3372                        !!!next-token;
3373                        redo B;
3374                      }
3375                    
3376                    ## generate implied end tags
3377                    if ({
3378                         dd => 1, dt => 1, li => 1, p => 1,
3379                         td => 1, th => 1, tr => 1,
3380                         tbody => 1, tfoot=> 1, thead => 1,
3381                        }->{$self->{open_elements}->[-1]->[1]}) {
3382                      !!!back-token;
3383                      $token = {type => END_TAG_TOKEN,
3384                                tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3385                      redo B;
3386                    }
3387                    
3388                    if ($self->{open_elements}->[-1]->[1] ne 'caption') {
3389                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3390                    }
3391                    
3392                    splice @{$self->{open_elements}}, $i;
3393                    
3394                    $clear_up_to_marker->();
3395                    
3396                    $self->{insertion_mode} = IN_TABLE_IM;
3397                    
3398                    !!!next-token;
3399                  redo B;                  redo B;
3400                  } elsif ($self->{insertion_mode} == IN_CELL_IM) {
3401                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3402                    ## Ignore the token
3403                    !!!next-token;
3404                    redo B;
3405                  } else {
3406                    #
3407                }                }
3408                } elsif ({
3409                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                        table => 1, tbody => 1, tfoot => 1,
3410                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                        thead => 1, tr => 1,
3411                }                       }->{$token->{tag_name}} and
3412                         $self->{insertion_mode} == IN_CELL_IM) {
3413                splice @{$self->{open_elements}}, $i;                ## have an element in table scope
   
               $clear_up_to_marker->();  
   
               $self->{insertion_mode} = 'in table';  
   
               ## reprocess  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'caption') {  
               ## have a table element in table scope  
3414                my $i;                my $i;
3415                  my $tn;
3416                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3417                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
3418                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq $token->{tag_name}) {
3419                    $i = $_;                    $i = $_;
3420                    last INSCOPE;                    last INSCOPE;
3421                    } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {
3422                      $tn = $node->[1];
3423                      ## NOTE: There is exactly one |td| or |th| element
3424                      ## in scope in the stack of open elements by definition.
3425                  } elsif ({                  } elsif ({
3426                            table => 1, html => 1,                            table => 1, html => 1,
3427                           }->{$node->[1]}) {                           }->{$node->[1]}) {
# Line 3798  sub _tree_construction_main ($) { Line 3434  sub _tree_construction_main ($) {
3434                  !!!next-token;                  !!!next-token;
3435                  redo B;                  redo B;
3436                }                }
                 
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
3437    
3438                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                ## Close the cell
3439                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!back-token; # </?>
3440                }                $token = {type => END_TAG_TOKEN, tag_name => $tn};
   
               splice @{$self->{open_elements}}, $i;  
   
               $clear_up_to_marker->();  
   
               $self->{insertion_mode} = 'in table';  
   
               !!!next-token;  
3441                redo B;                redo B;
3442              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table' and
3443                         $self->{insertion_mode} == IN_CAPTION_IM) {
3444                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed:caption');
3445    
3446                ## As if </caption>                ## As if </caption>
# Line 3850  sub _tree_construction_main ($) { Line 3468  sub _tree_construction_main ($) {
3468                if ({                if ({
3469                     dd => 1, dt => 1, li => 1, p => 1,                     dd => 1, dt => 1, li => 1, p => 1,
3470                     td => 1, th => 1, tr => 1,                     td => 1, th => 1, tr => 1,
3471                       tbody => 1, tfoot=> 1, thead => 1,
3472                    }->{$self->{open_elements}->[-1]->[1]}) {                    }->{$self->{open_elements}->[-1]->[1]}) {
3473                  !!!back-token; # </table>                  !!!back-token; # </table>
3474                  $token = {type => 'end tag', tag_name => 'caption'};                  $token = {type => END_TAG_TOKEN, tag_name => 'caption'};
3475                  !!!back-token;                  !!!back-token;
3476                  $token = {type => 'end tag',                  $token = {type => END_TAG_TOKEN,
3477                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3478                  redo B;                  redo B;
3479                }                }
# Line 3867  sub _tree_construction_main ($) { Line 3486  sub _tree_construction_main ($) {
3486    
3487                $clear_up_to_marker->();                $clear_up_to_marker->();
3488    
3489                $self->{insertion_mode} = 'in table';                $self->{insertion_mode} = IN_TABLE_IM;
3490    
3491                ## reprocess                ## reprocess
3492                redo B;                redo B;
3493              } elsif ({              } elsif ({
3494                        body => 1, col => 1, colgroup => 1,                        body => 1, col => 1, colgroup => 1, html => 1,
                       html => 1, tbody => 1, td => 1, tfoot => 1,  
                       th => 1, thead => 1, tr => 1,  
3495                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3496                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
3497                ## Ignore the token                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
                 
           $in_body->($insert_to_current);  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'in column group') {  
           if ($token->{type} eq 'character') {  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
               
             #  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'col') {  
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               pop @{$self->{open_elements}};  
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'colgroup') {  
               if ($self->{open_elements}->[-1]->[1] eq 'html') {  
                 !!!parse-error (type => 'unmatched end tag:colgroup');  
3498                  ## Ignore the token                  ## Ignore the token
3499                  !!!next-token;                  !!!next-token;
3500                  redo B;                  redo B;
3501                } else {                } else {
3502                  pop @{$self->{open_elements}}; # colgroup                  #
                 $self->{insertion_mode} = 'in table';  
                 !!!next-token;  
                 redo B;              
3503                }                }
3504              } elsif ($token->{tag_name} eq 'col') {              } elsif ({
3505                !!!parse-error (type => 'unmatched end tag:col');                        tbody => 1, tfoot => 1,
3506                          thead => 1, tr => 1,
3507                         }->{$token->{tag_name}} and
3508                         $self->{insertion_mode} == IN_CAPTION_IM) {
3509                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3510                ## Ignore the token                ## Ignore the token
3511                !!!next-token;                !!!next-token;
3512                redo B;                redo B;
3513              } else {              } else {
3514                #                #
3515              }              }
3516            } else {        } else {
3517              #          die "$0: $token->{type}: Unknown token type";
3518            }        }
3519    
3520            ## As if </colgroup>        $insert = $insert_to_current;
3521            if ($self->{open_elements}->[-1]->[1] eq 'html') {        #
3522              !!!parse-error (type => 'unmatched end tag:colgroup');      } elsif ($self->{insertion_mode} & TABLE_IMS) {
3523              ## Ignore the token        if ($token->{type} == CHARACTER_TOKEN) {
             !!!next-token;  
             redo B;  
           } else {  
             pop @{$self->{open_elements}}; # colgroup  
             $self->{insertion_mode} = 'in table';  
             ## reprocess  
             redo B;  
           }  
         } elsif ($self->{insertion_mode} eq 'in table body') {  
           if ($token->{type} eq 'character') {  
             ## NOTE: This is a "character in table" code clone.  
3524              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
3525                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
3526                                
# Line 3969  sub _tree_construction_main ($) { Line 3537  sub _tree_construction_main ($) {
3537              ## into the current node" while characters might not be              ## into the current node" while characters might not be
3538              ## result in a new Text node.              ## result in a new Text node.
3539              $reconstruct_active_formatting_elements->($insert_to_foster);              $reconstruct_active_formatting_elements->($insert_to_foster);
3540                
3541              if ({              if ({
3542                   table => 1, tbody => 1, tfoot => 1,                   table => 1, tbody => 1, tfoot => 1,
3543                   thead => 1, tr => 1,                   thead => 1, tr => 1,
# Line 4009  sub _tree_construction_main ($) { Line 3577  sub _tree_construction_main ($) {
3577                            
3578              !!!next-token;              !!!next-token;
3579              redo B;              redo B;
3580            } elsif ($token->{type} eq 'comment') {        } elsif ($token->{type} == START_TAG_TOKEN) {
             ## Copied from 'in table'  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
3581              if ({              if ({
3582                   tr => 1,                   tr => ($self->{insertion_mode} != IN_ROW_IM),
3583                   th => 1, td => 1,                   th => 1, td => 1,
3584                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
3585                unless ($token->{tag_name} eq 'tr') {                if ($self->{insertion_mode} == IN_TABLE_IM) {
3586                  !!!parse-error (type => 'missing start tag:tr');                  ## Clear back to table context
3587                    while ($self->{open_elements}->[-1]->[1] ne 'table' and
3588                           $self->{open_elements}->[-1]->[1] ne 'html') {
3589                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3590                      pop @{$self->{open_elements}};
3591                    }
3592                    
3593                    !!!insert-element ('tbody');
3594                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
3595                    ## reprocess in the "in table body" insertion mode...
3596                }                }
3597    
3598                ## Clear back to table body context                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
3599                    unless ($token->{tag_name} eq 'tr') {
3600                      !!!parse-error (type => 'missing start tag:tr');
3601                    }
3602                    
3603                    ## Clear back to table body context
3604                    while (not {
3605                      tbody => 1, tfoot => 1, thead => 1, html => 1,
3606                    }->{$self->{open_elements}->[-1]->[1]}) {
3607                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3608                      pop @{$self->{open_elements}};
3609                    }
3610                    
3611                    $self->{insertion_mode} = IN_ROW_IM;
3612                    if ($token->{tag_name} eq 'tr') {
3613                      !!!insert-element ($token->{tag_name}, $token->{attributes});
3614                      !!!next-token;
3615                      redo B;
3616                    } else {
3617                      !!!insert-element ('tr');
3618                      ## reprocess in the "in row" insertion mode
3619                    }
3620                  }
3621    
3622                  ## Clear back to table row context
3623                while (not {                while (not {
3624                  tbody => 1, tfoot => 1, thead => 1, html => 1,                  tr => 1, html => 1,
3625                }->{$self->{open_elements}->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
3626                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3627                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3628                }                }
3629                                
3630                $self->{insertion_mode} = 'in row';                !!!insert-element ($token->{tag_name}, $token->{attributes});
3631                if ($token->{tag_name} eq 'tr') {                $self->{insertion_mode} = IN_CELL_IM;
3632                  !!!insert-element ($token->{tag_name}, $token->{attributes});  
3633                  !!!next-token;                push @$active_formatting_elements, ['#marker', ''];
3634                } else {                
3635                  !!!insert-element ('tr');                !!!next-token;
                 ## reprocess  
               }  
3636                redo B;                redo B;
3637              } elsif ({              } elsif ({
3638                        caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
3639                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
3640                          tr => 1, # $self->{insertion_mode} == IN_ROW_IM
3641                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3642                ## have an element in table scope                if ($self->{insertion_mode} == IN_ROW_IM) {
3643                my $i;                  ## As if </tr>
3644                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  ## have an element in table scope
3645                  my $node = $self->{open_elements}->[$_];                  my $i;
3646                  if ({                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3647                       tbody => 1, thead => 1, tfoot => 1,                    my $node = $self->{open_elements}->[$_];
3648                      }->{$node->[1]}) {                    if ($node->[1] eq 'tr') {
3649                    $i = $_;                      $i = $_;
3650                    last INSCOPE;                      last INSCOPE;
3651                  } elsif ({                    } elsif ({
3652                            table => 1, html => 1,                              table => 1, html => 1,
3653                           }->{$node->[1]}) {                             }->{$node->[1]}) {
3654                    last INSCOPE;                      last INSCOPE;
3655                      }
3656                    } # INSCOPE
3657                    unless (defined $i) {
3658                      !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});
3659                      ## Ignore the token
3660                      !!!next-token;
3661                      redo B;
3662                    }
3663                    
3664                    ## Clear back to table row context
3665                    while (not {
3666                      tr => 1, html => 1,
3667                    }->{$self->{open_elements}->[-1]->[1]}) {
3668                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3669                      pop @{$self->{open_elements}};
3670                    }
3671                    
3672                    pop @{$self->{open_elements}}; # tr
3673                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
3674                    if ($token->{tag_name} eq 'tr') {
3675                      ## reprocess
3676                      redo B;
3677                    } else {
3678                      ## reprocess in the "in table body" insertion mode...
3679                  }                  }
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
3680                }                }
3681    
3682                ## Clear back to table body context                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
3683                while (not {                  ## have an element in table scope
3684                  tbody => 1, tfoot => 1, thead => 1, html => 1,                  my $i;
3685                }->{$self->{open_elements}->[-1]->[1]}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3686                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    my $node = $self->{open_elements}->[$_];
3687                      if ({
3688                           tbody => 1, thead => 1, tfoot => 1,
3689                          }->{$node->[1]}) {
3690                        $i = $_;
3691                        last INSCOPE;
3692                      } elsif ({
3693                                table => 1, html => 1,
3694                               }->{$node->[1]}) {
3695                        last INSCOPE;
3696                      }
3697                    } # INSCOPE
3698                    unless (defined $i) {
3699                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3700                      ## Ignore the token
3701                      !!!next-token;
3702                      redo B;
3703                    }
3704    
3705                    ## Clear back to table body context
3706                    while (not {
3707                      tbody => 1, tfoot => 1, thead => 1, html => 1,
3708                    }->{$self->{open_elements}->[-1]->[1]}) {
3709                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3710                      pop @{$self->{open_elements}};
3711                    }
3712                    
3713                    ## As if <{current node}>
3714                    ## have an element in table scope
3715                    ## true by definition
3716                    
3717                    ## Clear back to table body context
3718                    ## nop by definition
3719                    
3720                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3721                    $self->{insertion_mode} = IN_TABLE_IM;
3722                    ## reprocess in "in table" insertion mode...
3723                }                }
3724    
3725                ## As if <{current node}>                if ($token->{tag_name} eq 'col') {
3726                ## have an element in table scope                  ## Clear back to table context
3727                ## true by definition                  while ($self->{open_elements}->[-1]->[1] ne 'table' and
3728                           $self->{open_elements}->[-1]->[1] ne 'html') {
3729                ## Clear back to table body context                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3730                ## nop by definition                    pop @{$self->{open_elements}};
3731                    }
3732                pop @{$self->{open_elements}};                  
3733                $self->{insertion_mode} = 'in table';                  !!!insert-element ('colgroup');
3734                ## reprocess                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
3735                redo B;                  ## reprocess
3736                    redo B;
3737                  } elsif ({
3738                            caption => 1,
3739                            colgroup => 1,
3740                            tbody => 1, tfoot => 1, thead => 1,
3741                           }->{$token->{tag_name}}) {
3742                    ## Clear back to table context
3743                    while ($self->{open_elements}->[-1]->[1] ne 'table' and
3744                           $self->{open_elements}->[-1]->[1] ne 'html') {
3745                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3746                      pop @{$self->{open_elements}};
3747                    }
3748                    
3749                    push @$active_formatting_elements, ['#marker', '']
3750                        if $token->{tag_name} eq 'caption';
3751                    
3752                    !!!insert-element ($token->{tag_name}, $token->{attributes});
3753                    $self->{insertion_mode} = {
3754                                               caption => IN_CAPTION_IM,
3755                                               colgroup => IN_COLUMN_GROUP_IM,
3756                                               tbody => IN_TABLE_BODY_IM,
3757                                               tfoot => IN_TABLE_BODY_IM,
3758                                               thead => IN_TABLE_BODY_IM,
3759                                              }->{$token->{tag_name}};
3760                    !!!next-token;
3761                    redo B;
3762                  } else {
3763                    die "$0: in table: <>: $token->{tag_name}";
3764                  }
3765              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
3766                ## NOTE: This is a code clone of "table in table"                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
               !!!parse-error (type => 'not closed:table');  
3767    
3768                ## As if </table>                ## As if </table>
3769                ## have a table element in table scope                ## have a table element in table scope
# Line 4115  sub _tree_construction_main ($) { Line 3790  sub _tree_construction_main ($) {
3790                if ({                if ({
3791                     dd => 1, dt => 1, li => 1, p => 1,                     dd => 1, dt => 1, li => 1, p => 1,
3792                     td => 1, th => 1, tr => 1,                     td => 1, th => 1, tr => 1,
3793                       tbody => 1, tfoot=> 1, thead => 1,
3794                    }->{$self->{open_elements}->[-1]->[1]}) {                    }->{$self->{open_elements}->[-1]->[1]}) {
3795                  !!!back-token; # <table>                  !!!back-token; # <table>
3796                  $token = {type => 'end tag', tag_name => 'table'};                  $token = {type => END_TAG_TOKEN, tag_name => 'table'};
3797                  !!!back-token;                  !!!back-token;
3798                  $token = {type => 'end tag',                  $token = {type => END_TAG_TOKEN,
3799                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3800                  redo B;                  redo B;
3801                }                }
# Line 4130  sub _tree_construction_main ($) { Line 3806  sub _tree_construction_main ($) {
3806    
3807                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
3808    
3809                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
3810    
3811                ## reprocess                ## reprocess
3812                redo B;                redo B;
3813              } else {          } else {
3814                #            !!!parse-error (type => 'in table:'.$token->{tag_name});
3815              }  
3816            } elsif ($token->{type} eq 'end tag') {            $insert = $insert_to_foster;
3817              if ({            #
3818                   tbody => 1, tfoot => 1, thead => 1,          }
3819                  }->{$token->{tag_name}}) {        } elsif ($token->{type} == END_TAG_TOKEN) {
3820                if ($token->{tag_name} eq 'tr' and
3821                    $self->{insertion_mode} == IN_ROW_IM) {
3822                ## have an element in table scope                ## have an element in table scope
3823                my $i;                my $i;
3824                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 4161  sub _tree_construction_main ($) { Line 3839  sub _tree_construction_main ($) {
3839                  redo B;                  redo B;
3840                }                }
3841    
3842                ## Clear back to table body context                ## Clear back to table row context
3843                while (not {                while (not {
3844                  tbody => 1, tfoot => 1, thead => 1, html => 1,                  tr => 1, html => 1,
3845                }->{$self->{open_elements}->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
3846                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3847                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3848                }                }
3849    
3850                pop @{$self->{open_elements}};                pop @{$self->{open_elements}}; # tr
3851                $self->{insertion_mode} = 'in table';                $self->{insertion_mode} = IN_TABLE_BODY_IM;
3852                !!!next-token;                !!!next-token;
3853                redo B;                redo B;
3854              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
3855                ## have an element in table scope                if ($self->{insertion_mode} == IN_ROW_IM) {
3856                my $i;                  ## As if </tr>
3857                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  ## have an element in table scope
3858                  my $node = $self->{open_elements}->[$_];                  my $i;
3859                  if ({                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3860                       tbody => 1, thead => 1, tfoot => 1,                    my $node = $self->{open_elements}->[$_];
3861                      }->{$node->[1]}) {                    if ($node->[1] eq 'tr') {
3862                    $i = $_;                      $i = $_;
3863                    last INSCOPE;                      last INSCOPE;
3864                  } elsif ({                    } elsif ({
3865                            table => 1, html => 1,                              table => 1, html => 1,
3866                           }->{$node->[1]}) {                             }->{$node->[1]}) {
3867                    last INSCOPE;                      last INSCOPE;
3868                      }
3869                    } # INSCOPE
3870                    unless (defined $i) {
3871                      !!!parse-error (type => 'unmatched end tag:'.$token->{type});
3872                      ## Ignore the token
3873                      !!!next-token;
3874                      redo B;
3875                  }                  }
3876                } # INSCOPE                  
3877                unless (defined $i) {                  ## Clear back to table row context
3878                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  while (not {
3879                  ## Ignore the token                    tr => 1, html => 1,
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Clear back to table body context  
               while (not {  
                 tbody => 1, tfoot => 1, thead => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
   
               ## As if <{current node}>  
               ## have an element in table scope  
               ## true by definition  
   
               ## Clear back to table body context  
               ## nop by definition  
   
               pop @{$self->{open_elements}};  
               $self->{insertion_mode} = 'in table';  
               ## reprocess  
               redo B;  
             } elsif ({  
                       body => 1, caption => 1, col => 1, colgroup => 1,  
                       html => 1, td => 1, th => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
             
           ## As if in table  
           !!!parse-error (type => 'in table:'.$token->{tag_name});  
           $in_body->($insert_to_foster);  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'in row') {  
           if ($token->{type} eq 'character') {  
             ## NOTE: This is a "character in table" code clone.  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
                 
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
   
             !!!parse-error (type => 'in table:#character');  
   
             ## As if in body, but insert into foster parent element  
             ## ISSUE: Spec says that "whenever a node would be inserted  
             ## into the current node" while characters might not be  
             ## result in a new Text node.  
             $reconstruct_active_formatting_elements->($insert_to_foster);  
               
             if ({  
                  table => 1, tbody => 1, tfoot => 1,  
                  thead => 1, tr => 1,  
3880                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
3881                # MUST                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3882                my $foster_parent_element;                    pop @{$self->{open_elements}};
3883                my $next_sibling;                  }
3884                my $prev_sibling;                  
3885                OE: for (reverse 0..$#{$self->{open_elements}}) {                  pop @{$self->{open_elements}}; # tr
3886                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
3887                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                  ## reprocess in the "in table body" insertion mode...
3888                    if (defined $parent and $parent->node_type == 1) {                }
3889                      $foster_parent_element = $parent;  
3890                      $next_sibling = $self->{open_elements}->[$_]->[0];                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
3891                      $prev_sibling = $next_sibling->previous_sibling;                  ## have an element in table scope
3892                    } else {                  my $i;
3893                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3894                      $prev_sibling = $foster_parent_element->last_child;                    my $node = $self->{open_elements}->[$_];
3895                      if ({
3896                           tbody => 1, thead => 1, tfoot => 1,
3897                          }->{$node->[1]}) {
3898                        $i = $_;
3899                        last INSCOPE;
3900                      } elsif ({
3901                                table => 1, html => 1,
3902                               }->{$node->[1]}) {
3903                        last INSCOPE;
3904                    }                    }
3905                    last OE;                  } # INSCOPE
3906                    unless (defined $i) {
3907                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3908                      ## Ignore the token
3909                      !!!next-token;
3910                      redo B;
3911                  }                  }
3912                } # OE                  
3913                $foster_parent_element = $self->{open_elements}->[0]->[0] and                  ## Clear back to table body context
3914                $prev_sibling = $foster_parent_element->last_child                  while (not {
3915                  unless defined $foster_parent_element;                    tbody => 1, tfoot => 1, thead => 1, html => 1,
3916                if (defined $prev_sibling and                  }->{$self->{open_elements}->[-1]->[1]}) {
3917                    $prev_sibling->node_type == 3) {                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3918                  $prev_sibling->manakai_append_text ($token->{data});                    pop @{$self->{open_elements}};
               } else {  
                 $foster_parent_element->insert_before  
                   ($self->{document}->create_text_node ($token->{data}),  
                    $next_sibling);  
               }  
             } else {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
             }  
               
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             ## Copied from 'in table'  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'th' or  
                 $token->{tag_name} eq 'td') {  
               ## Clear back to table row context  
               while (not {  
                 tr => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
                 
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               $self->{insertion_mode} = 'in cell';  
   
               push @$active_formatting_elements, ['#marker', ''];  
                 
               !!!next-token;  
               redo B;  
             } elsif ({  
                       caption => 1, col => 1, colgroup => 1,  
                       tbody => 1, tfoot => 1, thead => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               ## As if </tr>  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'tr') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
3919                  }                  }
3920                } # INSCOPE                  
3921                unless (defined $i) {                  ## As if <{current node}>
3922                  !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});                  ## have an element in table scope
3923                  ## Ignore the token                  ## true by definition
3924                  !!!next-token;                  
3925                  redo B;                  ## Clear back to table body context
3926                }                  ## nop by definition
3927                    
               ## Clear back to table row context  
               while (not {  
                 tr => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
3928                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3929                    $self->{insertion_mode} = IN_TABLE_IM;
3930                    ## reprocess in the "in table" insertion mode...
3931                }                }
3932    
               pop @{$self->{open_elements}}; # tr  
               $self->{insertion_mode} = 'in table body';  
               ## reprocess  
               redo B;  
             } elsif ($token->{tag_name} eq 'table') {  
               ## NOTE: This is a code clone of "table in table"  
               !!!parse-error (type => 'not closed:table');  
   
               ## As if </table>  
3933                ## have a table element in table scope                ## have a table element in table scope
3934                my $i;                my $i;
3935                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3936                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
3937                  if ($node->[1] eq 'table') {                  if ($node->[1] eq $token->{tag_name}) {
3938                    $i = $_;                    $i = $_;
3939                    last INSCOPE;                    last INSCOPE;
3940                  } elsif ({                  } elsif ({
# Line 4373  sub _tree_construction_main ($) { Line 3944  sub _tree_construction_main ($) {
3944                  }                  }
3945                } # INSCOPE                } # INSCOPE
3946                unless (defined $i) {                unless (defined $i) {
3947                  !!!parse-error (type => 'unmatched end tag:table');                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3948                  ## Ignore tokens </table><table>                  ## Ignore the token
3949                  !!!next-token;                  !!!next-token;
3950                  redo B;                  redo B;
3951                }                }
3952                  
3953                ## generate implied end tags                ## generate implied end tags
3954                if ({                if ({
3955                     dd => 1, dt => 1, li => 1, p => 1,                     dd => 1, dt => 1, li => 1, p => 1,
3956                     td => 1, th => 1, tr => 1,                     td => 1, th => 1, tr => 1,
3957                       tbody => 1, tfoot=> 1, thead => 1,
3958                    }->{$self->{open_elements}->[-1]->[1]}) {                    }->{$self->{open_elements}->[-1]->[1]}) {
                 !!!back-token; # <table>  
                 $token = {type => 'end tag', tag_name => 'table'};  
3959                  !!!back-token;                  !!!back-token;
3960                  $token = {type => 'end tag',                  $token = {type => END_TAG_TOKEN,
3961                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3962                  redo B;                  redo B;
3963                }                }
3964                  
3965                if ($self->{open_elements}->[-1]->[1] ne 'table') {                if ($self->{open_elements}->[-1]->[1] ne 'table') {
3966                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3967                }                }
3968                    
3969                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
3970                  
3971                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
3972                  
               ## reprocess  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'tr') {  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Clear back to table row context  
               while (not {  
                 tr => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
   
               pop @{$self->{open_elements}}; # tr  
               $self->{insertion_mode} = 'in table body';  
3973                !!!next-token;                !!!next-token;
3974                redo B;                redo B;
             } elsif ($token->{tag_name} eq 'table') {  
               ## As if </tr>  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'tr') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{type});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Clear back to table row context  
               while (not {  
                 tr => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
   
               pop @{$self->{open_elements}}; # tr  
               $self->{insertion_mode} = 'in table body';  
               ## reprocess  
               redo B;  
3975              } elsif ({              } elsif ({
3976                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
3977                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}} and
3978                ## have an element in table scope                       $self->{insertion_mode} & ROW_IMS) {
3979                my $i;                if ($self->{insertion_mode} == IN_ROW_IM) {
3980                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  ## have an element in table scope
3981                  my $node = $self->{open_elements}->[$_];                  my $i;
3982                  if ($node->[1] eq $token->{tag_name}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3983                    $i = $_;                    my $node = $self->{open_elements}->[$_];
3984                    last INSCOPE;                    if ($node->[1] eq $token->{tag_name}) {
3985                  } elsif ({                      $i = $_;
3986                            table => 1, html => 1,                      last INSCOPE;
3987                           }->{$node->[1]}) {                    } elsif ({
3988                    last INSCOPE;                              table => 1, html => 1,
3989                               }->{$node->[1]}) {
3990                        last INSCOPE;
3991                      }
3992                    } # INSCOPE
3993                      unless (defined $i) {
3994                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3995                        ## Ignore the token
3996                        !!!next-token;
3997                        redo B;
3998                      }
3999                    
4000                    ## As if </tr>
4001                    ## have an element in table scope
4002                    my $i;
4003                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4004                      my $node = $self->{open_elements}->[$_];
4005                      if ($node->[1] eq 'tr') {
4006                        $i = $_;
4007                        last INSCOPE;
4008                      } elsif ({
4009                                table => 1, html => 1,
4010                               }->{$node->[1]}) {
4011                        last INSCOPE;
4012                      }
4013                    } # INSCOPE
4014                      unless (defined $i) {
4015                        !!!parse-error (type => 'unmatched end tag:tr');
4016                        ## Ignore the token
4017                        !!!next-token;
4018                        redo B;
4019                      }
4020                    
4021                    ## Clear back to table row context
4022                    while (not {
4023                      tr => 1, html => 1,
4024                    }->{$self->{open_elements}->[-1]->[1]}) {
4025                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4026                      pop @{$self->{open_elements}};
4027                  }                  }
4028                } # INSCOPE                  
4029                unless (defined $i) {                  pop @{$self->{open_elements}}; # tr
4030                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
4031                  ## Ignore the token                  ## reprocess in the "in table body" insertion mode...
                 !!!next-token;  
                 redo B;  
4032                }                }
4033    
               ## As if </tr>  
4034                ## have an element in table scope                ## have an element in table scope
4035                my $i;                my $i;
4036                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4037                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4038                  if ($node->[1] eq 'tr') {                  if ($node->[1] eq $token->{tag_name}) {
4039                    $i = $_;                    $i = $_;
4040                    last INSCOPE;                    last INSCOPE;
4041                  } elsif ({                  } elsif ({
# Line 4511  sub _tree_construction_main ($) { Line 4045  sub _tree_construction_main ($) {
4045                  }                  }
4046                } # INSCOPE                } # INSCOPE
4047                unless (defined $i) {                unless (defined $i) {
4048                  !!!parse-error (type => 'unmatched end tag:tr');                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4049                  ## Ignore the token                  ## Ignore the token
4050                  !!!next-token;                  !!!next-token;
4051                  redo B;                  redo B;
4052                }                }
4053    
4054                ## Clear back to table row context                ## Clear back to table body context
4055                while (not {                while (not {
4056                  tr => 1, html => 1,                  tbody => 1, tfoot => 1, thead => 1, html => 1,
4057                }->{$self->{open_elements}->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
4058                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4059                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4060                }                }
4061    
4062                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}};
4063                $self->{insertion_mode} = 'in table body';                $self->{insertion_mode} = IN_TABLE_IM;
4064                ## reprocess                !!!next-token;
4065                redo B;                redo B;
4066              } elsif ({              } elsif ({
4067                        body => 1, caption => 1, col => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
4068                        colgroup => 1, html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
4069                          tr => 1, # $self->{insertion_mode} == IN_ROW_IM
4070                          tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
4071                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4072                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4073                ## Ignore the token                ## Ignore the token
4074                !!!next-token;                !!!next-token;
4075                redo B;                redo B;
4076              } else {          } else {
4077                #            !!!parse-error (type => 'in table:/'.$token->{tag_name});
             }  
           } else {  
             #  
           }  
   
           ## As if in table  
           !!!parse-error (type => 'in table:'.$token->{tag_name});  
           $in_body->($insert_to_foster);  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'in cell') {  
           if ($token->{type} eq 'character') {  
             ## NOTE: This is a code clone of "character in body".  
             $reconstruct_active_formatting_elements->($insert_to_current);  
               
             $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
4078    
4079              !!!next-token;            $insert = $insert_to_foster;
4080              redo B;            #
4081            } elsif ($token->{type} eq 'comment') {          }
4082              ## NOTE: This is a code clone of "comment in body".        } else {
4083              my $comment = $self->{document}->create_comment ($token->{data});          die "$0: $token->{type}: Unknown token type";
4084              $self->{open_elements}->[-1]->[0]->append_child ($comment);        }
4085              !!!next-token;      } elsif ($self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
4086              redo B;            if ($token->{type} == CHARACTER_TOKEN) {
4087            } elsif ($token->{type} eq 'start tag') {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4088              if ({                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4089                   caption => 1, col => 1, colgroup => 1,                unless (length $token->{data}) {
                  tbody => 1, td => 1, tfoot => 1, th => 1,  
                  thead => 1, tr => 1,  
                 }->{$token->{tag_name}}) {  
               ## have an element in table scope  
               my $tn;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'td' or $node->[1] eq 'th') {  
                   $tn = $node->[1];  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $tn) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
4090                  !!!next-token;                  !!!next-token;
4091                  redo B;                  redo B;
4092                }                }
4093                }
4094                ## Close the cell              
4095                !!!back-token; # <?>              #
4096                $token = {type => 'end tag', tag_name => $tn};            } elsif ($token->{type} == START_TAG_TOKEN) {
4097                if ($token->{tag_name} eq 'col') {
4098                  !!!insert-element ($token->{tag_name}, $token->{attributes});
4099                  pop @{$self->{open_elements}};
4100                  !!!next-token;
4101                redo B;                redo B;
4102              } else {              } else {
4103                #                #
4104              }              }
4105            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
4106              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {              if ($token->{tag_name} eq 'colgroup') {
4107                ## have an element in table scope                if ($self->{open_elements}->[-1]->[1] eq 'html') {
4108                my $i;                  !!!parse-error (type => 'unmatched end tag:colgroup');
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
4109                  ## Ignore the token                  ## Ignore the token
4110                  !!!next-token;                  !!!next-token;
4111                  redo B;                  redo B;
4112                  } else {
4113                    pop @{$self->{open_elements}}; # colgroup
4114                    $self->{insertion_mode} = IN_TABLE_IM;
4115                    !!!next-token;
4116                    redo B;            
4117                }                }
4118                              } elsif ($token->{tag_name} eq 'col') {
4119                ## generate implied end tags                !!!parse-error (type => 'unmatched end tag:col');
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => ($token->{tag_name} eq 'th'),  
                    th => ($token->{tag_name} eq 'td'),  
                    tr => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
   
               if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               }  
   
               splice @{$self->{open_elements}}, $i;  
   
               $clear_up_to_marker->();  
   
               $self->{insertion_mode} = 'in row';  
   
               !!!next-token;  
               redo B;  
             } elsif ({  
                       body => 1, caption => 1, col => 1,  
                       colgroup => 1, html => 1,  
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
4120                ## Ignore the token                ## Ignore the token
4121                !!!next-token;                !!!next-token;
4122                redo B;                redo B;
             } elsif ({  
                       table => 1, tbody => 1, tfoot => 1,  
                       thead => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               ## have an element in table scope  
               my $i;  
               my $tn;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {  
                   $tn = $node->[1];  
                   ## NOTE: There is exactly one |td| or |th| element  
                   ## in scope in the stack of open elements by definition.  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Close the cell  
               !!!back-token; # </?>  
               $token = {type => 'end tag', tag_name => $tn};  
               redo B;  
4123              } else {              } else {
4124                #                #
4125              }              }
4126            } else {            } else {
4127              #              #
4128            }            }
4129              
4130            $in_body->($insert_to_current);            ## As if </colgroup>
4131            redo B;            if ($self->{open_elements}->[-1]->[1] eq 'html') {
4132          } elsif ($self->{insertion_mode} eq 'in select') {              !!!parse-error (type => 'unmatched end tag:colgroup');
4133            if ($token->{type} eq 'character') {              ## Ignore the token
             $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
4134              !!!next-token;              !!!next-token;
4135              redo B;              redo B;
4136            } elsif ($token->{type} eq 'comment') {            } else {
4137              my $comment = $self->{document}->create_comment ($token->{data});              pop @{$self->{open_elements}}; # colgroup
4138              $self->{open_elements}->[-1]->[0]->append_child ($comment);              $self->{insertion_mode} = IN_TABLE_IM;
4139              !!!next-token;              ## reprocess
4140              redo B;              redo B;
4141            } elsif ($token->{type} eq 'start tag') {            }
4142        } elsif ($self->{insertion_mode} == IN_SELECT_IM) {
4143          if ($token->{type} == CHARACTER_TOKEN) {
4144            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4145            !!!next-token;
4146            redo B;
4147          } elsif ($token->{type} == START_TAG_TOKEN) {
4148              if ($token->{tag_name} eq 'option') {              if ($token->{tag_name} eq 'option') {
4149                if ($self->{open_elements}->[-1]->[1] eq 'option') {                if ($self->{open_elements}->[-1]->[1] eq 'option') {
4150                  ## As if </option>                  ## As if </option>
# Line 4756  sub _tree_construction_main ($) { Line 4197  sub _tree_construction_main ($) {
4197    
4198                !!!next-token;                !!!next-token;
4199                redo B;                redo B;
4200              } else {          } else {
4201                #            !!!parse-error (type => 'in select:'.$token->{tag_name});
4202              }            ## Ignore the token
4203            } elsif ($token->{type} eq 'end tag') {            !!!next-token;
4204              redo B;
4205            }
4206          } elsif ($token->{type} == END_TAG_TOKEN) {
4207              if ($token->{tag_name} eq 'optgroup') {              if ($token->{tag_name} eq 'optgroup') {
4208                if ($self->{open_elements}->[-1]->[1] eq 'option' and                if ($self->{open_elements}->[-1]->[1] eq 'option' and
4209                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {
# Line 4861  sub _tree_construction_main ($) { Line 4305  sub _tree_construction_main ($) {
4305    
4306                ## reprocess                ## reprocess
4307                redo B;                redo B;
4308              } else {          } else {
4309                #            !!!parse-error (type => 'in select:/'.$token->{tag_name});
             }  
           } else {  
             #  
           }  
   
           !!!parse-error (type => 'in select:'.$token->{tag_name});  
4310            ## Ignore the token            ## Ignore the token
4311            !!!next-token;            !!!next-token;
4312            redo B;            redo B;
4313          } elsif ($self->{insertion_mode} eq 'after body') {          }
4314            if ($token->{type} eq 'character') {        } else {
4315              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          die "$0: $token->{type}: Unknown token type";
4316                ## As if in body        }
4317                $reconstruct_active_formatting_elements->($insert_to_current);      } elsif ($self->{insertion_mode} & BODY_AFTER_IMS) {
4318          if ($token->{type} == CHARACTER_TOKEN) {
4319            if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4320              my $data = $1;
4321              ## As if in body
4322              $reconstruct_active_formatting_elements->($insert_to_current);
4323                                
4324                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4325              
4326              unless (length $token->{data}) {
4327                !!!next-token;
4328                redo B;
4329              }
4330            }
4331            
4332            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4333              !!!parse-error (type => 'after html:#character');
4334    
4335                unless (length $token->{data}) {            ## Reprocess in the "main" phase, "after body" insertion mode...
4336                  !!!next-token;          }
4337                  redo B;          
4338                }          ## "after body" insertion mode
4339              }          !!!parse-error (type => 'after body:#character');
4340                
4341              #          $self->{insertion_mode} = IN_BODY_IM;
4342              !!!parse-error (type => 'after body:#'.$token->{type});          ## reprocess
4343            } elsif ($token->{type} eq 'comment') {          redo B;
4344              my $comment = $self->{document}->create_comment ($token->{data});        } elsif ($token->{type} == START_TAG_TOKEN) {
4345              $self->{open_elements}->[0]->[0]->append_child ($comment);          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4346              !!!parse-error (type => 'after html:'.$token->{tag_name});
4347              
4348              ## Reprocess in the "main" phase, "after body" insertion mode...
4349            }
4350    
4351            ## "after body" insertion mode
4352            !!!parse-error (type => 'after body:'.$token->{tag_name});
4353    
4354            $self->{insertion_mode} = IN_BODY_IM;
4355            ## reprocess
4356            redo B;
4357          } elsif ($token->{type} == END_TAG_TOKEN) {
4358            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4359              !!!parse-error (type => 'after html:/'.$token->{tag_name});
4360              
4361              $self->{insertion_mode} = AFTER_BODY_IM;
4362              ## Reprocess in the "main" phase, "after body" insertion mode...
4363            }
4364    
4365            ## "after body" insertion mode
4366            if ($token->{tag_name} eq 'html') {
4367              if (defined $self->{inner_html_node}) {
4368                !!!parse-error (type => 'unmatched end tag:html');
4369                ## Ignore the token
4370              !!!next-token;              !!!next-token;
4371              redo B;              redo B;
           } elsif ($token->{type} eq 'start tag') {  
             !!!parse-error (type => 'after body:'.$token->{tag_name});  
             #  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'html') {  
               if (defined $self->{inner_html_node}) {  
                 !!!parse-error (type => 'unmatched end tag:html');  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               } else {  
                 $phase = 'trailing end';  
                 !!!next-token;  
                 redo B;  
               }  
             } else {  
               !!!parse-error (type => 'after body:/'.$token->{tag_name});  
             }  
4372            } else {            } else {
4373              !!!parse-error (type => 'after body:#'.$token->{type});              $self->{insertion_mode} = AFTER_HTML_BODY_IM;
4374                !!!next-token;
4375                redo B;
4376            }            }
4377            } else {
4378              !!!parse-error (type => 'after body:/'.$token->{tag_name});
4379    
4380            $self->{insertion_mode} = 'in body';            $self->{insertion_mode} = IN_BODY_IM;
4381            ## reprocess            ## reprocess
4382            redo B;            redo B;
4383          } elsif ($self->{insertion_mode} eq 'in frameset') {          }
4384            if ($token->{type} eq 'character') {        } else {
4385              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          die "$0: $token->{type}: Unknown token type";
4386                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});        }
4387        } elsif ($self->{insertion_mode} & FRAME_IMS) {
4388                unless (length $token->{data}) {        if ($token->{type} == CHARACTER_TOKEN) {
4389                  !!!next-token;          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4390                  redo B;            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4391                }            
4392              }            unless (length $token->{data}) {
   
             #  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
4393              !!!next-token;              !!!next-token;
4394              redo B;              redo B;
4395            } elsif ($token->{type} eq 'start tag') {            }
4396              if ($token->{tag_name} eq 'frameset') {          }
4397                !!!insert-element ($token->{tag_name}, $token->{attributes});          
4398                !!!next-token;          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
4399                redo B;            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4400              } elsif ($token->{tag_name} eq 'frame') {              !!!parse-error (type => 'in frameset:#character');
4401                !!!insert-element ($token->{tag_name}, $token->{attributes});            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
4402                pop @{$self->{open_elements}};              !!!parse-error (type => 'after frameset:#character');
4403                !!!next-token;            } else { # "after html frameset"
4404                redo B;              !!!parse-error (type => 'after html:#character');
4405              } elsif ($token->{tag_name} eq 'noframes') {  
4406                $in_body->($insert_to_current);              $self->{insertion_mode} = AFTER_FRAMESET_IM;
4407                redo B;              ## Reprocess in the "main" phase, "after frameset"...
4408              } else {              !!!parse-error (type => 'after frameset:#character');
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'frameset') {  
               if ($self->{open_elements}->[-1]->[1] eq 'html' and  
                   @{$self->{open_elements}} == 1) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
               } else {  
                 pop @{$self->{open_elements}};  
                 !!!next-token;  
               }  
                 
               ## if not inner_html and  
               if ($self->{open_elements}->[-1]->[1] ne 'frameset') {  
                 $self->{insertion_mode} = 'after frameset';  
               }  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
4409            }            }
4410                        
4411            if (defined $token->{tag_name}) {            ## Ignore the token.
4412              if (length $token->{data}) {
4413                ## reprocess the rest of characters
4414              } else {
4415                !!!next-token;
4416              }
4417              redo B;
4418            }
4419            
4420            die qq[$0: Character "$token->{data}"];
4421          } elsif ($token->{type} == START_TAG_TOKEN) {
4422            if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4423              !!!parse-error (type => 'after html:'.$token->{tag_name});
4424    
4425              $self->{insertion_mode} = AFTER_FRAMESET_IM;
4426              ## Process in the "main" phase, "after frameset" insertion mode...
4427            }
4428    
4429            if ($token->{tag_name} eq 'frameset' and
4430                $self->{insertion_mode} == IN_FRAMESET_IM) {
4431              !!!insert-element ($token->{tag_name}, $token->{attributes});
4432              !!!next-token;
4433              redo B;
4434            } elsif ($token->{tag_name} eq 'frame' and
4435                     $self->{insertion_mode} == IN_FRAMESET_IM) {
4436              !!!insert-element ($token->{tag_name}, $token->{attributes});
4437              pop @{$self->{open_elements}};
4438              !!!next-token;
4439              redo B;
4440            } elsif ($token->{tag_name} eq 'noframes') {
4441              ## NOTE: As if in body.
4442              $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);
4443              redo B;
4444            } else {
4445              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4446              !!!parse-error (type => 'in frameset:'.$token->{tag_name});              !!!parse-error (type => 'in frameset:'.$token->{tag_name});
4447            } else {            } else {
4448              !!!parse-error (type => 'in frameset:#'.$token->{type});              !!!parse-error (type => 'after frameset:'.$token->{tag_name});
4449            }            }
4450            ## Ignore the token            ## Ignore the token
4451            !!!next-token;            !!!next-token;
4452            redo B;            redo B;
4453          } elsif ($self->{insertion_mode} eq 'after frameset') {          }
4454            if ($token->{type} eq 'character') {        } elsif ($token->{type} == END_TAG_TOKEN) {
4455              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4456                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});            !!!parse-error (type => 'after html:/'.$token->{tag_name});
4457    
4458                unless (length $token->{data}) {            $self->{insertion_mode} = AFTER_FRAMESET_IM;
4459                  !!!next-token;            ## Process in the "main" phase, "after frameset" insertion mode...
4460                  redo B;          }
               }  
             }  
4461    
4462              #          if ($token->{tag_name} eq 'frameset' and
4463            } elsif ($token->{type} eq 'comment') {              $self->{insertion_mode} == IN_FRAMESET_IM) {
4464              my $comment = $self->{document}->create_comment ($token->{data});            if ($self->{open_elements}->[-1]->[1] eq 'html' and
4465              $self->{open_elements}->[-1]->[0]->append_child ($comment);                @{$self->{open_elements}} == 1) {
4466                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4467                ## Ignore the token
4468              !!!next-token;              !!!next-token;
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'noframes') {  
               $in_body->($insert_to_current);  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'html') {  
               $phase = 'trailing end';  
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
4469            } else {            } else {
4470              #              pop @{$self->{open_elements}};
4471                !!!next-token;
4472            }            }
4473              
4474            if (defined $token->{tag_name}) {            if (not defined $self->{inner_html_node} and
4475              !!!parse-error (type => 'after frameset:'.$token->{tag_name});                $self->{open_elements}->[-1]->[1] ne 'frameset') {
4476                $self->{insertion_mode} = AFTER_FRAMESET_IM;
4477              }
4478              redo B;
4479            } elsif ($token->{tag_name} eq 'html' and
4480                     $self->{insertion_mode} == AFTER_FRAMESET_IM) {
4481              $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
4482              !!!next-token;
4483              redo B;
4484            } else {
4485              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4486                !!!parse-error (type => 'in frameset:/'.$token->{tag_name});
4487            } else {            } else {
4488              !!!parse-error (type => 'after frameset:#'.$token->{type});              !!!parse-error (type => 'after frameset:/'.$token->{tag_name});
4489            }            }
4490            ## Ignore the token            ## Ignore the token
4491            !!!next-token;            !!!next-token;
4492            redo B;            redo B;
4493            }
4494          } else {
4495            die "$0: $token->{type}: Unknown token type";
4496          }
4497    
4498          ## ISSUE: An issue in spec here
4499        } else {
4500          die "$0: $self->{insertion_mode}: Unknown insertion mode";
4501        }
4502    
4503        ## "in body" insertion mode
4504        if ($token->{type} == START_TAG_TOKEN) {
4505          if ($token->{tag_name} eq 'script') {
4506            ## NOTE: This is an "as if in head" code clone
4507            $script_start_tag->($insert);
4508            redo B;
4509          } elsif ($token->{tag_name} eq 'style') {
4510            ## NOTE: This is an "as if in head" code clone
4511            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
4512            redo B;
4513          } elsif ({
4514                    base => 1, link => 1,
4515                   }->{$token->{tag_name}}) {
4516            ## NOTE: This is an "as if in head" code clone, only "-t" differs
4517            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4518            pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4519            !!!next-token;
4520            redo B;
4521          } elsif ($token->{tag_name} eq 'meta') {
4522            ## NOTE: This is an "as if in head" code clone, only "-t" differs
4523            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4524            my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4525    
4526            unless ($self->{confident}) {
4527              if ($token->{attributes}->{charset}) { ## TODO: And if supported
4528                $self->{change_encoding}
4529                    ->($self, $token->{attributes}->{charset}->{value});
4530                
4531                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4532                    ->set_user_data (manakai_has_reference =>
4533                                         $token->{attributes}->{charset}
4534                                             ->{has_reference});
4535              } elsif ($token->{attributes}->{content}) {
4536                ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.
4537                if ($token->{attributes}->{content}->{value}
4538                    =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=
4539                        [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4540                        ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
4541                  $self->{change_encoding}
4542                      ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);
4543                  $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4544                      ->set_user_data (manakai_has_reference =>
4545                                           $token->{attributes}->{content}
4546                                                 ->{has_reference});
4547                }
4548              }
4549            } else {
4550              if ($token->{attributes}->{charset}) {
4551                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4552                    ->set_user_data (manakai_has_reference =>
4553                                         $token->{attributes}->{charset}
4554                                             ->{has_reference});
4555              }
4556              if ($token->{attributes}->{content}) {
4557                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4558                    ->set_user_data (manakai_has_reference =>
4559                                         $token->{attributes}->{content}
4560                                             ->{has_reference});
4561              }
4562            }
4563    
4564            ## ISSUE: An issue in spec there          !!!next-token;
4565            redo B;
4566          } elsif ($token->{tag_name} eq 'title') {
4567            !!!parse-error (type => 'in body:title');
4568            ## NOTE: This is an "as if in head" code clone
4569            $parse_rcdata->(RCDATA_CONTENT_MODEL, sub {
4570              if (defined $self->{head_element}) {
4571                $self->{head_element}->append_child ($_[0]);
4572              } else {
4573                $insert->($_[0]);
4574              }
4575            });
4576            redo B;
4577          } elsif ($token->{tag_name} eq 'body') {
4578            !!!parse-error (type => 'in body:body');
4579                  
4580            if (@{$self->{open_elements}} == 1 or
4581                $self->{open_elements}->[1]->[1] ne 'body') {
4582              ## Ignore the token
4583          } else {          } else {
4584            die "$0: $self->{insertion_mode}: Unknown insertion mode";            my $body_el = $self->{open_elements}->[1]->[0];
4585              for my $attr_name (keys %{$token->{attributes}}) {
4586                unless ($body_el->has_attribute_ns (undef, $attr_name)) {
4587                  $body_el->set_attribute_ns
4588                    (undef, [undef, $attr_name],
4589                     $token->{attributes}->{$attr_name}->{value});
4590                }
4591              }
4592          }          }
       }  
     } elsif ($phase eq 'trailing end') {  
       ## states in the main stage is preserved yet # MUST  
         
       if ($token->{type} eq 'DOCTYPE') {  
         !!!parse-error (type => 'after html:#DOCTYPE');  
         ## Ignore the token  
4593          !!!next-token;          !!!next-token;
4594          redo B;          redo B;
4595        } elsif ($token->{type} eq 'comment') {        } elsif ({
4596          my $comment = $self->{document}->create_comment ($token->{data});                  address => 1, blockquote => 1, center => 1, dir => 1,
4597          $self->{document}->append_child ($comment);                  div => 1, dl => 1, fieldset => 1, listing => 1,
4598                    menu => 1, ol => 1, p => 1, ul => 1,
4599                    pre => 1,
4600                   }->{$token->{tag_name}}) {
4601            ## has a p element in scope
4602            INSCOPE: for (reverse @{$self->{open_elements}}) {
4603              if ($_->[1] eq 'p') {
4604                !!!back-token;
4605                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4606                redo B;
4607              } elsif ({
4608                        table => 1, caption => 1, td => 1, th => 1,
4609                        button => 1, marquee => 1, object => 1, html => 1,
4610                       }->{$_->[1]}) {
4611                last INSCOPE;
4612              }
4613            } # INSCOPE
4614              
4615            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4616            if ($token->{tag_name} eq 'pre') {
4617              !!!next-token;
4618              if ($token->{type} == CHARACTER_TOKEN) {
4619                $token->{data} =~ s/^\x0A//;
4620                unless (length $token->{data}) {
4621                  !!!next-token;
4622                }
4623              }
4624            } else {
4625              !!!next-token;
4626            }
4627            redo B;
4628          } elsif ($token->{tag_name} eq 'form') {
4629            if (defined $self->{form_element}) {
4630              !!!parse-error (type => 'in form:form');
4631              ## Ignore the token
4632              !!!next-token;
4633              redo B;
4634            } else {
4635              ## has a p element in scope
4636              INSCOPE: for (reverse @{$self->{open_elements}}) {
4637                if ($_->[1] eq 'p') {
4638                  !!!back-token;
4639                  $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4640                  redo B;
4641                } elsif ({
4642                          table => 1, caption => 1, td => 1, th => 1,
4643                          button => 1, marquee => 1, object => 1, html => 1,
4644                         }->{$_->[1]}) {
4645                  last INSCOPE;
4646                }
4647              } # INSCOPE
4648                
4649              !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4650              $self->{form_element} = $self->{open_elements}->[-1]->[0];
4651              !!!next-token;
4652              redo B;
4653            }
4654          } elsif ($token->{tag_name} eq 'li') {
4655            ## has a p element in scope
4656            INSCOPE: for (reverse @{$self->{open_elements}}) {
4657              if ($_->[1] eq 'p') {
4658                !!!back-token;
4659                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4660                redo B;
4661              } elsif ({
4662                        table => 1, caption => 1, td => 1, th => 1,
4663                        button => 1, marquee => 1, object => 1, html => 1,
4664                       }->{$_->[1]}) {
4665                last INSCOPE;
4666              }
4667            } # INSCOPE
4668              
4669            ## Step 1
4670            my $i = -1;
4671            my $node = $self->{open_elements}->[$i];
4672            LI: {
4673              ## Step 2
4674              if ($node->[1] eq 'li') {
4675                if ($i != -1) {
4676                  !!!parse-error (type => 'end tag missing:'.
4677                                  $self->{open_elements}->[-1]->[1]);
4678                }
4679                splice @{$self->{open_elements}}, $i;
4680                last LI;
4681              }
4682              
4683              ## Step 3
4684              if (not $formatting_category->{$node->[1]} and
4685                  #not $phrasing_category->{$node->[1]} and
4686                  ($special_category->{$node->[1]} or
4687                   $scoping_category->{$node->[1]}) and
4688                  $node->[1] ne 'address' and $node->[1] ne 'div') {
4689                last LI;
4690              }
4691              
4692              ## Step 4
4693              $i--;
4694              $node = $self->{open_elements}->[$i];
4695              redo LI;
4696            } # LI
4697              
4698            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4699          !!!next-token;          !!!next-token;
4700          redo B;          redo B;
4701        } elsif ($token->{type} eq 'character') {        } elsif ($token->{tag_name} eq 'dd' or $token->{tag_name} eq 'dt') {
4702          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          ## has a p element in scope
4703            my $data = $1;          INSCOPE: for (reverse @{$self->{open_elements}}) {
4704            ## As if in the main phase.            if ($_->[1] eq 'p') {
4705            ## NOTE: The insertion mode in the main phase              !!!back-token;
4706            ## just before the phase has been changed to the trailing              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4707            ## end phase is either "after body" or "after frameset".              redo B;
4708            $reconstruct_active_formatting_elements->($insert_to_current)            } elsif ({
4709              if $phase eq 'main';                      table => 1, caption => 1, td => 1, th => 1,
4710                        button => 1, marquee => 1, object => 1, html => 1,
4711                       }->{$_->[1]}) {
4712                last INSCOPE;
4713              }
4714            } # INSCOPE
4715              
4716            ## Step 1
4717            my $i = -1;
4718            my $node = $self->{open_elements}->[$i];
4719            LI: {
4720              ## Step 2
4721              if ($node->[1] eq 'dt' or $node->[1] eq 'dd') {
4722                if ($i != -1) {
4723                  !!!parse-error (type => 'end tag missing:'.
4724                                  $self->{open_elements}->[-1]->[1]);
4725                }
4726                splice @{$self->{open_elements}}, $i;
4727                last LI;
4728              }
4729              
4730              ## Step 3
4731              if (not $formatting_category->{$node->[1]} and
4732                  #not $phrasing_category->{$node->[1]} and
4733                  ($special_category->{$node->[1]} or
4734                   $scoping_category->{$node->[1]}) and
4735                  $node->[1] ne 'address' and $node->[1] ne 'div') {
4736                last LI;
4737              }
4738              
4739              ## Step 4
4740              $i--;
4741              $node = $self->{open_elements}->[$i];
4742              redo LI;
4743            } # LI
4744              
4745            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4746            !!!next-token;
4747            redo B;
4748          } elsif ($token->{tag_name} eq 'plaintext') {
4749            ## has a p element in scope
4750            INSCOPE: for (reverse @{$self->{open_elements}}) {
4751              if ($_->[1] eq 'p') {
4752                !!!back-token;
4753                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4754                redo B;
4755              } elsif ({
4756                        table => 1, caption => 1, td => 1, th => 1,
4757                        button => 1, marquee => 1, object => 1, html => 1,
4758                       }->{$_->[1]}) {
4759                last INSCOPE;
4760              }
4761            } # INSCOPE
4762              
4763            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4764              
4765            $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
4766              
4767            !!!next-token;
4768            redo B;
4769          } elsif ({
4770                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
4771                   }->{$token->{tag_name}}) {
4772            ## has a p element in scope
4773            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4774              my $node = $self->{open_elements}->[$_];
4775              if ($node->[1] eq 'p') {
4776                !!!back-token;
4777                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4778                redo B;
4779              } elsif ({
4780                        table => 1, caption => 1, td => 1, th => 1,
4781                        button => 1, marquee => 1, object => 1, html => 1,
4782                       }->{$node->[1]}) {
4783                last INSCOPE;
4784              }
4785            } # INSCOPE
4786              
4787            ## NOTE: See <http://html5.org/tools/web-apps-tracker?from=925&to=926>
4788            ## has an element in scope
4789            #my $i;
4790            #INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4791            #  my $node = $self->{open_elements}->[$_];
4792            #  if ({
4793            #       h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
4794            #      }->{$node->[1]}) {
4795            #    $i = $_;
4796            #    last INSCOPE;
4797            #  } elsif ({
4798            #            table => 1, caption => 1, td => 1, th => 1,
4799            #            button => 1, marquee => 1, object => 1, html => 1,
4800            #           }->{$node->[1]}) {
4801            #    last INSCOPE;
4802            #  }
4803            #} # INSCOPE
4804            #  
4805            #if (defined $i) {
4806            #  !!! parse-error (type => 'in hn:hn');
4807            #  splice @{$self->{open_elements}}, $i;
4808            #}
4809              
4810            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4811              
4812            !!!next-token;
4813            redo B;
4814          } elsif ($token->{tag_name} eq 'a') {
4815            AFE: for my $i (reverse 0..$#$active_formatting_elements) {
4816              my $node = $active_formatting_elements->[$i];
4817              if ($node->[1] eq 'a') {
4818                !!!parse-error (type => 'in a:a');
4819                
4820                !!!back-token;
4821                $token = {type => END_TAG_TOKEN, tag_name => 'a'};
4822                $formatting_end_tag->($token->{tag_name});
4823                
4824                AFE2: for (reverse 0..$#$active_formatting_elements) {
4825                  if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
4826                    splice @$active_formatting_elements, $_, 1;
4827                    last AFE2;
4828                  }
4829                } # AFE2
4830                OE: for (reverse 0..$#{$self->{open_elements}}) {
4831                  if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {
4832                    splice @{$self->{open_elements}}, $_, 1;
4833                    last OE;
4834                  }
4835                } # OE
4836                last AFE;
4837              } elsif ($node->[0] eq '#marker') {
4838                last AFE;
4839              }
4840            } # AFE
4841              
4842            $reconstruct_active_formatting_elements->($insert_to_current);
4843    
4844            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4845            push @$active_formatting_elements, $self->{open_elements}->[-1];
4846    
4847            !!!next-token;
4848            redo B;
4849          } elsif ({
4850                    b => 1, big => 1, em => 1, font => 1, i => 1,
4851                    s => 1, small => 1, strile => 1,
4852                    strong => 1, tt => 1, u => 1,
4853                   }->{$token->{tag_name}}) {
4854            $reconstruct_active_formatting_elements->($insert_to_current);
4855            
4856            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4857            push @$active_formatting_elements, $self->{open_elements}->[-1];
4858            
4859            !!!next-token;
4860            redo B;
4861          } elsif ($token->{tag_name} eq 'nobr') {
4862            $reconstruct_active_formatting_elements->($insert_to_current);
4863    
4864            ## has a |nobr| element in scope
4865            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4866              my $node = $self->{open_elements}->[$_];
4867              if ($node->[1] eq 'nobr') {
4868                !!!parse-error (type => 'in nobr:nobr');
4869                !!!back-token;
4870                $token = {type => END_TAG_TOKEN, tag_name => 'nobr'};
4871                redo B;
4872              } elsif ({
4873                        table => 1, caption => 1, td => 1, th => 1,
4874                        button => 1, marquee => 1, object => 1, html => 1,
4875                       }->{$node->[1]}) {
4876                last INSCOPE;
4877              }
4878            } # INSCOPE
4879            
4880            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4881            push @$active_formatting_elements, $self->{open_elements}->[-1];
4882            
4883            !!!next-token;
4884            redo B;
4885          } elsif ($token->{tag_name} eq 'button') {
4886            ## has a button element in scope
4887            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4888              my $node = $self->{open_elements}->[$_];
4889              if ($node->[1] eq 'button') {
4890                !!!parse-error (type => 'in button:button');
4891                !!!back-token;
4892                $token = {type => END_TAG_TOKEN, tag_name => 'button'};
4893                redo B;
4894              } elsif ({
4895                        table => 1, caption => 1, td => 1, th => 1,
4896                        button => 1, marquee => 1, object => 1, html => 1,
4897                       }->{$node->[1]}) {
4898                last INSCOPE;
4899              }
4900            } # INSCOPE
4901              
4902            $reconstruct_active_formatting_elements->($insert_to_current);
4903              
4904            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4905            push @$active_formatting_elements, ['#marker', ''];
4906    
4907            !!!next-token;
4908            redo B;
4909          } elsif ($token->{tag_name} eq 'marquee' or
4910                   $token->{tag_name} eq 'object') {
4911            $reconstruct_active_formatting_elements->($insert_to_current);
4912            
4913            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4914            push @$active_formatting_elements, ['#marker', ''];
4915            
4916            !!!next-token;
4917            redo B;
4918          } elsif ($token->{tag_name} eq 'xmp') {
4919            $reconstruct_active_formatting_elements->($insert_to_current);
4920            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
4921            redo B;
4922          } elsif ($token->{tag_name} eq 'table') {
4923            ## has a p element in scope
4924            INSCOPE: for (reverse @{$self->{open_elements}}) {
4925              if ($_->[1] eq 'p') {
4926                !!!back-token;
4927                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4928                redo B;
4929              } elsif ({
4930                        table => 1, caption => 1, td => 1, th => 1,
4931                        button => 1, marquee => 1, object => 1, html => 1,
4932                       }->{$_->[1]}) {
4933                last INSCOPE;
4934              }
4935            } # INSCOPE
4936              
4937            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4938              
4939            $self->{insertion_mode} = IN_TABLE_IM;
4940              
4941            !!!next-token;
4942            redo B;
4943          } elsif ({
4944                    area => 1, basefont => 1, bgsound => 1, br => 1,
4945                    embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
4946                    image => 1,
4947                   }->{$token->{tag_name}}) {
4948            if ($token->{tag_name} eq 'image') {
4949              !!!parse-error (type => 'image');
4950              $token->{tag_name} = 'img';
4951            }
4952    
4953            ## NOTE: There is an "as if <br>" code clone.
4954            $reconstruct_active_formatting_elements->($insert_to_current);
4955            
4956            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4957            pop @{$self->{open_elements}};
4958            
4959            !!!next-token;
4960            redo B;
4961          } elsif ($token->{tag_name} eq 'hr') {
4962            ## has a p element in scope
4963            INSCOPE: for (reverse @{$self->{open_elements}}) {
4964              if ($_->[1] eq 'p') {
4965                !!!back-token;
4966                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4967                redo B;
4968              } elsif ({
4969                        table => 1, caption => 1, td => 1, th => 1,
4970                        button => 1, marquee => 1, object => 1, html => 1,
4971                       }->{$_->[1]}) {
4972                last INSCOPE;
4973              }
4974            } # INSCOPE
4975                        
4976            $self->{open_elements}->[-1]->[0]->manakai_append_text ($data);          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4977            pop @{$self->{open_elements}};
4978                        
4979            !!!next-token;
4980            redo B;
4981          } elsif ($token->{tag_name} eq 'input') {
4982            $reconstruct_active_formatting_elements->($insert_to_current);
4983            
4984            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4985            ## TODO: associate with $self->{form_element} if defined
4986            pop @{$self->{open_elements}};
4987            
4988            !!!next-token;
4989            redo B;
4990          } elsif ($token->{tag_name} eq 'isindex') {
4991            !!!parse-error (type => 'isindex');
4992            
4993            if (defined $self->{form_element}) {
4994              ## Ignore the token
4995              !!!next-token;
4996              redo B;
4997            } else {
4998              my $at = $token->{attributes};
4999              my $form_attrs;
5000              $form_attrs->{action} = $at->{action} if $at->{action};
5001              my $prompt_attr = $at->{prompt};
5002              $at->{name} = {name => 'name', value => 'isindex'};
5003              delete $at->{action};
5004              delete $at->{prompt};
5005              my @tokens = (
5006                            {type => START_TAG_TOKEN, tag_name => 'form',
5007                             attributes => $form_attrs},
5008                            {type => START_TAG_TOKEN, tag_name => 'hr'},
5009                            {type => START_TAG_TOKEN, tag_name => 'p'},
5010                            {type => START_TAG_TOKEN, tag_name => 'label'},
5011                           );
5012              if ($prompt_attr) {
5013                push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value}};
5014              } else {
5015                push @tokens, {type => CHARACTER_TOKEN,
5016                               data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD
5017                ## TODO: make this configurable
5018              }
5019              push @tokens,
5020                            {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at},
5021                            #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
5022                            {type => END_TAG_TOKEN, tag_name => 'label'},
5023                            {type => END_TAG_TOKEN, tag_name => 'p'},
5024                            {type => START_TAG_TOKEN, tag_name => 'hr'},
5025                            {type => END_TAG_TOKEN, tag_name => 'form'};
5026              $token = shift @tokens;
5027              !!!back-token (@tokens);
5028              redo B;
5029            }
5030          } elsif ($token->{tag_name} eq 'textarea') {
5031            my $tag_name = $token->{tag_name};
5032            my $el;
5033            !!!create-element ($el, $token->{tag_name}, $token->{attributes});
5034            
5035            ## TODO: $self->{form_element} if defined
5036            $self->{content_model} = RCDATA_CONTENT_MODEL;
5037            delete $self->{escape}; # MUST
5038            
5039            $insert->($el);
5040            
5041            my $text = '';
5042            !!!next-token;
5043            if ($token->{type} == CHARACTER_TOKEN) {
5044              $token->{data} =~ s/^\x0A//;
5045            unless (length $token->{data}) {            unless (length $token->{data}) {
5046              !!!next-token;              !!!next-token;
             redo B;  
5047            }            }
5048          }          }
5049            while ($token->{type} == CHARACTER_TOKEN) {
5050              $text .= $token->{data};
5051              !!!next-token;
5052            }
5053            if (length $text) {
5054              $el->manakai_append_text ($text);
5055            }
5056            
5057            $self->{content_model} = PCDATA_CONTENT_MODEL;
5058            
5059            if ($token->{type} == END_TAG_TOKEN and
5060                $token->{tag_name} eq $tag_name) {
5061              ## Ignore the token
5062            } else {
5063              !!!parse-error (type => 'in RCDATA:#'.$token->{type});
5064            }
5065            !!!next-token;
5066            redo B;
5067          } elsif ({
5068                    iframe => 1,
5069                    noembed => 1,
5070                    noframes => 1,
5071                    noscript => 0, ## TODO: 1 if scripting is enabled
5072                   }->{$token->{tag_name}}) {
5073            ## NOTE: There is an "as if in body" code clone.
5074            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
5075            redo B;
5076          } elsif ($token->{tag_name} eq 'select') {
5077            $reconstruct_active_formatting_elements->($insert_to_current);
5078            
5079            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5080            
5081            $self->{insertion_mode} = IN_SELECT_IM;
5082            !!!next-token;
5083            redo B;
5084          } elsif ({
5085                    caption => 1, col => 1, colgroup => 1, frame => 1,
5086                    frameset => 1, head => 1, option => 1, optgroup => 1,
5087                    tbody => 1, td => 1, tfoot => 1, th => 1,
5088                    thead => 1, tr => 1,
5089                   }->{$token->{tag_name}}) {
5090            !!!parse-error (type => 'in body:'.$token->{tag_name});
5091            ## Ignore the token
5092            !!!next-token;
5093            redo B;
5094            
5095            ## ISSUE: An issue on HTML5 new elements in the spec.
5096          } else {
5097            $reconstruct_active_formatting_elements->($insert_to_current);
5098            
5099            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5100            
5101            !!!next-token;
5102            redo B;
5103          }
5104        } elsif ($token->{type} == END_TAG_TOKEN) {
5105          if ($token->{tag_name} eq 'body') {
5106            if (@{$self->{open_elements}} > 1 and
5107                $self->{open_elements}->[1]->[1] eq 'body') {
5108              for (@{$self->{open_elements}}) {
5109                unless ({
5110                           dd => 1, dt => 1, li => 1, p => 1, td => 1,
5111                           th => 1, tr => 1, body => 1, html => 1,
5112                         tbody => 1, tfoot => 1, thead => 1,
5113                        }->{$_->[1]}) {
5114                  !!!parse-error (type => 'not closed:'.$_->[1]);
5115                }
5116              }
5117    
5118          !!!parse-error (type => 'after html:#character');            $self->{insertion_mode} = AFTER_BODY_IM;
5119          $phase = 'main';            !!!next-token;
5120          ## reprocess            redo B;
5121            } else {
5122              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5123              ## Ignore the token
5124              !!!next-token;
5125              redo B;
5126            }
5127          } elsif ($token->{tag_name} eq 'html') {
5128            if (@{$self->{open_elements}} > 1 and $self->{open_elements}->[1]->[1] eq 'body') {
5129              ## ISSUE: There is an issue in the spec.
5130              if ($self->{open_elements}->[-1]->[1] ne 'body') {
5131                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);
5132              }
5133              $self->{insertion_mode} = AFTER_BODY_IM;
5134              ## reprocess
5135              redo B;
5136            } else {
5137              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5138              ## Ignore the token
5139              !!!next-token;
5140              redo B;
5141            }
5142          } elsif ({
5143                    address => 1, blockquote => 1, center => 1, dir => 1,
5144                    div => 1, dl => 1, fieldset => 1, listing => 1,
5145                    menu => 1, ol => 1, pre => 1, ul => 1,
5146                    p => 1,
5147                    dd => 1, dt => 1, li => 1,
5148                    button => 1, marquee => 1, object => 1,
5149                   }->{$token->{tag_name}}) {
5150            ## has an element in scope
5151            my $i;
5152            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5153              my $node = $self->{open_elements}->[$_];
5154              if ($node->[1] eq $token->{tag_name}) {
5155                ## generate implied end tags
5156                if ({
5157                     dd => ($token->{tag_name} ne 'dd'),
5158                     dt => ($token->{tag_name} ne 'dt'),
5159                     li => ($token->{tag_name} ne 'li'),
5160                     p => ($token->{tag_name} ne 'p'),
5161                     td => 1, th => 1, tr => 1,
5162                     tbody => 1, tfoot=> 1, thead => 1,
5163                    }->{$self->{open_elements}->[-1]->[1]}) {
5164                  !!!back-token;
5165                  $token = {type => END_TAG_TOKEN,
5166                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5167                  redo B;
5168                }
5169                $i = $_;
5170                last INSCOPE unless $token->{tag_name} eq 'p';
5171              } elsif ({
5172                        table => 1, caption => 1, td => 1, th => 1,
5173                        button => 1, marquee => 1, object => 1, html => 1,
5174                       }->{$node->[1]}) {
5175                last INSCOPE;
5176              }
5177            } # INSCOPE
5178            
5179            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
5180              if (defined $i) {
5181                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
5182              } else {
5183                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5184              }
5185            }
5186            
5187            if (defined $i) {
5188              splice @{$self->{open_elements}}, $i;
5189            } elsif ($token->{tag_name} eq 'p') {
5190              ## As if <p>, then reprocess the current token
5191              my $el;
5192              !!!create-element ($el, 'p');
5193              $insert->($el);
5194            }
5195            $clear_up_to_marker->()
5196              if {
5197                button => 1, marquee => 1, object => 1,
5198              }->{$token->{tag_name}};
5199            !!!next-token;
5200          redo B;          redo B;
5201        } elsif ($token->{type} eq 'start tag' or        } elsif ($token->{tag_name} eq 'form') {
5202                 $token->{type} eq 'end tag') {          ## has an element in scope
5203          !!!parse-error (type => 'after html:'.$token->{tag_name});          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5204          $phase = 'main';            my $node = $self->{open_elements}->[$_];
5205          ## reprocess            if ($node->[1] eq $token->{tag_name}) {
5206                ## generate implied end tags
5207                if ({
5208                     dd => 1, dt => 1, li => 1, p => 1,
5209                     td => 1, th => 1, tr => 1,
5210                     tbody => 1, tfoot=> 1, thead => 1,
5211                    }->{$self->{open_elements}->[-1]->[1]}) {
5212                  !!!back-token;
5213                  $token = {type => END_TAG_TOKEN,
5214                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5215                  redo B;
5216                }
5217                last INSCOPE;
5218              } elsif ({
5219                        table => 1, caption => 1, td => 1, th => 1,
5220                        button => 1, marquee => 1, object => 1, html => 1,
5221                       }->{$node->[1]}) {
5222                last INSCOPE;
5223              }
5224            } # INSCOPE
5225            
5226            if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {
5227              pop @{$self->{open_elements}};
5228            } else {
5229              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5230            }
5231    
5232            undef $self->{form_element};
5233            !!!next-token;
5234            redo B;
5235          } elsif ({
5236                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
5237                   }->{$token->{tag_name}}) {
5238            ## has an element in scope
5239            my $i;
5240            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5241              my $node = $self->{open_elements}->[$_];
5242              if ({
5243                   h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
5244                  }->{$node->[1]}) {
5245                ## generate implied end tags
5246                if ({
5247                     dd => 1, dt => 1, li => 1, p => 1,
5248                     td => 1, th => 1, tr => 1,
5249                     tbody => 1, tfoot=> 1, thead => 1,
5250                    }->{$self->{open_elements}->[-1]->[1]}) {
5251                  !!!back-token;
5252                  $token = {type => END_TAG_TOKEN,
5253                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5254                  redo B;
5255                }
5256                $i = $_;
5257                last INSCOPE;
5258              } elsif ({
5259                        table => 1, caption => 1, td => 1, th => 1,
5260                        button => 1, marquee => 1, object => 1, html => 1,
5261                       }->{$node->[1]}) {
5262                last INSCOPE;
5263              }
5264            } # INSCOPE
5265            
5266            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
5267              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5268            }
5269            
5270            splice @{$self->{open_elements}}, $i if defined $i;
5271            !!!next-token;
5272            redo B;
5273          } elsif ({
5274                    a => 1,
5275                    b => 1, big => 1, em => 1, font => 1, i => 1,
5276                    nobr => 1, s => 1, small => 1, strile => 1,
5277                    strong => 1, tt => 1, u => 1,
5278                   }->{$token->{tag_name}}) {
5279            $formatting_end_tag->($token->{tag_name});
5280            redo B;
5281          } elsif ($token->{tag_name} eq 'br') {
5282            !!!parse-error (type => 'unmatched end tag:br');
5283    
5284            ## As if <br>
5285            $reconstruct_active_formatting_elements->($insert_to_current);
5286            
5287            my $el;
5288            !!!create-element ($el, 'br');
5289            $insert->($el);
5290            
5291            ## Ignore the token.
5292            !!!next-token;
5293          redo B;          redo B;
5294        } elsif ($token->{type} eq 'end-of-file') {        } elsif ({
5295          ## Stop parsing                  caption => 1, col => 1, colgroup => 1, frame => 1,
5296          last B;                  frameset => 1, head => 1, option => 1, optgroup => 1,
5297                    tbody => 1, td => 1, tfoot => 1, th => 1,
5298                    thead => 1, tr => 1,
5299                    area => 1, basefont => 1, bgsound => 1,
5300                    embed => 1, hr => 1, iframe => 1, image => 1,
5301                    img => 1, input => 1, isindex => 1, noembed => 1,
5302                    noframes => 1, param => 1, select => 1, spacer => 1,
5303                    table => 1, textarea => 1, wbr => 1,
5304                    noscript => 0, ## TODO: if scripting is enabled
5305                   }->{$token->{tag_name}}) {
5306            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5307            ## Ignore the token
5308            !!!next-token;
5309            redo B;
5310            
5311            ## ISSUE: Issue on HTML5 new elements in spec
5312            
5313        } else {        } else {
5314          die "$0: $token->{type}: Unknown token";          ## Step 1
5315            my $node_i = -1;
5316            my $node = $self->{open_elements}->[$node_i];
5317    
5318            ## Step 2
5319            S2: {
5320              if ($node->[1] eq $token->{tag_name}) {
5321                ## Step 1
5322                ## generate implied end tags
5323                if ({
5324                     dd => 1, dt => 1, li => 1, p => 1,
5325                     td => 1, th => 1, tr => 1,
5326                     tbody => 1, tfoot => 1, thead => 1,
5327                    }->{$self->{open_elements}->[-1]->[1]}) {
5328                  !!!back-token;
5329                  $token = {type => END_TAG_TOKEN,
5330                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5331                  redo B;
5332                }
5333            
5334                ## Step 2
5335                if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {
5336                  ## NOTE: <x><y></x>
5337                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
5338                }
5339                
5340                ## Step 3
5341                splice @{$self->{open_elements}}, $node_i;
5342    
5343                !!!next-token;
5344                last S2;
5345              } else {
5346                ## Step 3
5347                if (not $formatting_category->{$node->[1]} and
5348                    #not $phrasing_category->{$node->[1]} and
5349                    ($special_category->{$node->[1]} or
5350                     $scoping_category->{$node->[1]})) {
5351                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5352                  ## Ignore the token
5353                  !!!next-token;
5354                  last S2;
5355                }
5356              }
5357              
5358              ## Step 4
5359              $node_i--;
5360              $node = $self->{open_elements}->[$node_i];
5361              
5362              ## Step 5;
5363              redo S2;
5364            } # S2
5365            redo B;
5366        }        }
5367      }      }
5368        redo B;
5369    } # B    } # B
5370    
5371      ## NOTE: The "trailing end" phase in HTML5 is split into
5372      ## two insertion modes: "after html body" and "after html frameset".
5373      ## NOTE: States in the main stage is preserved while
5374      ## the parser stays in the trailing end phase. # MUST
5375    
5376    ## Stop parsing # MUST    ## Stop parsing # MUST
5377        
5378    ## TODO: script stuffs    ## TODO: script stuffs
# Line 5094  sub set_inner_html ($$$) { Line 5384  sub set_inner_html ($$$) {
5384    my $s = \$_[0];    my $s = \$_[0];
5385    my $onerror = $_[1];    my $onerror = $_[1];
5386    
5387      ## ISSUE: Should {confident} be true?
5388    
5389    my $nt = $node->node_type;    my $nt = $node->node_type;
5390    if ($nt == 9) {    if ($nt == 9) {
5391      # MUST      # MUST
# Line 5167  sub set_inner_html ($$$) { Line 5459  sub set_inner_html ($$$) {
5459    
5460      ## Step 2      ## Step 2
5461      my $node_ln = $node->local_name;      my $node_ln = $node->local_name;
5462      $p->{content_model_flag} = {      $p->{content_model} = {
5463        title => 'RCDATA',        title => RCDATA_CONTENT_MODEL,
5464        textarea => 'RCDATA',        textarea => RCDATA_CONTENT_MODEL,
5465        style => 'CDATA',        style => CDATA_CONTENT_MODEL,
5466        script => 'CDATA',        script => CDATA_CONTENT_MODEL,
5467        xmp => 'CDATA',        xmp => CDATA_CONTENT_MODEL,
5468        iframe => 'CDATA',        iframe => CDATA_CONTENT_MODEL,
5469        noembed => 'CDATA',        noembed => CDATA_CONTENT_MODEL,
5470        noframes => 'CDATA',        noframes => CDATA_CONTENT_MODEL,
5471        noscript => 'CDATA',        noscript => CDATA_CONTENT_MODEL,
5472        plaintext => 'PLAINTEXT',        plaintext => PLAINTEXT_CONTENT_MODEL,
5473      }->{$node_ln} || 'PCDATA';      }->{$node_ln};
5474         ## ISSUE: What is "the name of the element"? local name?      $p->{content_model} = PCDATA_CONTENT_MODEL
5475            unless defined $p->{content_model};
5476            ## ISSUE: What is "the name of the element"? local name?
5477    
5478      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $node_ln];
5479    
# Line 5244  sub set_inner_html ($$$) { Line 5538  sub set_inner_html ($$$) {
5538    
5539  } # tree construction stage  } # tree construction stage
5540    
5541  sub get_inner_html ($$$) {  package Whatpm::HTML::RestartParser;
5542    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 = lc $child->tag_name; ## ISSUE: Definition of "lowercase"  
       $s .= '<' . $tag_name;  
   
       ## ISSUE: Non-html elements  
   
       my @attrs = @{$child->attributes}; # sort order MUST be stable  
       for my $attr (@attrs) { # order is implementation dependent  
         my $attr_name = lc $attr->name; ## ISSUE: Definition of "lowercase"  
         $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};  
   
       if (not $in_cdata and {  
         style => 1, script => 1, xmp => 1, iframe => 1,  
         noembed => 1, noframes => 1, noscript => 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  
5543    
5544  1;  1;
5545  # $Date$  # $Date$

Legend:
Removed from v.1.20  
changed lines
  Added in v.1.69

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24