/[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.49 by wakaba, Sat Jul 21 10:39:45 2007 UTC revision 1.63 by wakaba, Sun Nov 11 06:54:36 2007 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);
# Line 84  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;
96        $s = \ (Encode::decode ($charset, $$bytes_s));
97        $self->{input_encoding} = lc $charset; ## TODO: normalize name ## TODO: set $doc->input_encoding
98        $self->{confident} = 1;
99      } else {
100        $s = ref $_[0] ? $_[0] : \($_[0]);
101        $self->{confident} = 0;
102      }
103    
104      $self->{change_encoding} = sub {
105        my $self = shift;
106        my $charset = lc shift;
107        ## TODO: if $charset is supported
108        ## TODO: normalize charset name
109    
110        ## "Change the encoding" algorithm:
111    
112        ## Step 1    
113        if ($charset eq 'utf-16') { ## ISSUE: UTF-16BE -> UTF-8? UTF-16LE -> UTF-8?
114          $charset = 'utf-8';
115        }
116    
117        ## Step 2
118        if (defined $self->{input_encoding} and
119            $self->{input_encoding} eq $charset) {
120          $self->{confident} = 1;
121          return;
122        }
123    
124        !!!parse-error (type => 'charset label detected', level => 'w');
125    
126        ## Step 3
127        # if (can) {
128          ## change the encoding on the fly.
129          #$self->{confident} = 1;
130          #return;
131        # }
132    
133        ## Step 4
134        throw Whatpm::HTML::RestartParser (charset => $charset);
135      }; # $self->{change_encoding}
136    
137      my @args = @_; shift @args; # $s
138      my $return;
139      try {
140        $return = $self->parse_char_string ($s, @args);  
141      } catch Whatpm::HTML::RestartParser with {
142        my $charset = shift->{charset};
143        $s = \ (Encode::decode ($charset, $$bytes_s));    
144        $self->{input_encoding} = $charset; ## TODO: $doc->input_encoding;
145        $self->{confident} = 1;
146        $return = $self->parse_char_string ($s, @args);
147      };
148      return $return;
149    } # parse_byte_string
150    
151    *parse_char_string = \&parse_string;
152    
153  sub parse_string ($$$;$) {  sub parse_string ($$$;$) {
154    my $self = shift->new;    my $self = ref $_[0] ? shift : shift->new;
155    my $s = \$_[0];    my $s = ref $_[0] ? $_[0] : \($_[0]);
156    $self->{document} = $_[1];    $self->{document} = $_[1];
157      @{$self->{document}->child_nodes} = ();
158    
159    ## NOTE: |set_inner_html| copies most of this method's code    ## NOTE: |set_inner_html| copies most of this method's code
160    
161      $self->{confident} = 1 unless exists $self->{confident};
162    
163    my $i = 0;    my $i = 0;
164    my $line = 1;    my $line = 1;
165    my $column = 0;    my $column = 0;
# Line 147  sub new ($) { Line 216  sub new ($) {
216    $self->{parse_error} = sub {    $self->{parse_error} = sub {
217      #      #
218    };    };
219      $self->{change_encoding} = sub {
220        # if ($_[0] is a supported encoding) {
221        #   run "change the encoding" algorithm;
222        #   throw Whatpm::HTML::RestartParser (charset => $new_encoding);
223        # }
224      };
225      $self->{application_cache_selection} = sub {
226        #
227      };
228    return $self;    return $self;
229  } # new  } # new
230    
# Line 159  sub CDATA_CONTENT_MODEL () { CM_LIMITED_ Line 237  sub CDATA_CONTENT_MODEL () { CM_LIMITED_
237  sub RCDATA_CONTENT_MODEL () { CM_ENTITY | CM_LIMITED_MARKUP }  sub RCDATA_CONTENT_MODEL () { CM_ENTITY | CM_LIMITED_MARKUP }
238  sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }  sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }
239    
240    sub DATA_STATE () { 0 }
241    sub ENTITY_DATA_STATE () { 1 }
242    sub TAG_OPEN_STATE () { 2 }
243    sub CLOSE_TAG_OPEN_STATE () { 3 }
244    sub TAG_NAME_STATE () { 4 }
245    sub BEFORE_ATTRIBUTE_NAME_STATE () { 5 }
246    sub ATTRIBUTE_NAME_STATE () { 6 }
247    sub AFTER_ATTRIBUTE_NAME_STATE () { 7 }
248    sub BEFORE_ATTRIBUTE_VALUE_STATE () { 8 }
249    sub ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE () { 9 }
250    sub ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE () { 10 }
251    sub ATTRIBUTE_VALUE_UNQUOTED_STATE () { 11 }
252    sub ENTITY_IN_ATTRIBUTE_VALUE_STATE () { 12 }
253    sub MARKUP_DECLARATION_OPEN_STATE () { 13 }
254    sub COMMENT_START_STATE () { 14 }
255    sub COMMENT_START_DASH_STATE () { 15 }
256    sub COMMENT_STATE () { 16 }
257    sub COMMENT_END_STATE () { 17 }
258    sub COMMENT_END_DASH_STATE () { 18 }
259    sub BOGUS_COMMENT_STATE () { 19 }
260    sub DOCTYPE_STATE () { 20 }
261    sub BEFORE_DOCTYPE_NAME_STATE () { 21 }
262    sub DOCTYPE_NAME_STATE () { 22 }
263    sub AFTER_DOCTYPE_NAME_STATE () { 23 }
264    sub BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE () { 24 }
265    sub DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE () { 25 }
266    sub DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE () { 26 }
267    sub AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE () { 27 }
268    sub BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 28 }
269    sub DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE () { 29 }
270    sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE () { 30 }
271    sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }
272    sub BOGUS_DOCTYPE_STATE () { 32 }
273    
274    sub DOCTYPE_TOKEN () { 1 }
275    sub COMMENT_TOKEN () { 2 }
276    sub START_TAG_TOKEN () { 3 }
277    sub END_TAG_TOKEN () { 4 }
278    sub END_OF_FILE_TOKEN () { 5 }
279    sub CHARACTER_TOKEN () { 6 }
280    
281    sub AFTER_HTML_IMS () { 0b100 }
282    sub HEAD_IMS ()       { 0b1000 }
283    sub BODY_IMS ()       { 0b10000 }
284    sub BODY_TABLE_IMS () { 0b100000 }
285    sub TABLE_IMS ()      { 0b1000000 }
286    sub ROW_IMS ()        { 0b10000000 }
287    sub BODY_AFTER_IMS () { 0b100000000 }
288    sub FRAME_IMS ()      { 0b1000000000 }
289    
290    sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }
291    sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }
292    sub IN_HEAD_IM () { HEAD_IMS | 0b00 }
293    sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }
294    sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }
295    sub BEFORE_HEAD_IM () { HEAD_IMS | 0b11 }
296    sub IN_BODY_IM () { BODY_IMS }
297    sub IN_CELL_IM () { BODY_IMS | BODY_TABLE_IMS | 0b01 }
298    sub IN_CAPTION_IM () { BODY_IMS | BODY_TABLE_IMS | 0b10 }
299    sub IN_ROW_IM () { TABLE_IMS | ROW_IMS | 0b01 }
300    sub IN_TABLE_BODY_IM () { TABLE_IMS | ROW_IMS | 0b10 }
301    sub IN_TABLE_IM () { TABLE_IMS }
302    sub AFTER_BODY_IM () { BODY_AFTER_IMS }
303    sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }
304    sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }
305    sub IN_SELECT_IM () { 0b01 }
306    sub IN_COLUMN_GROUP_IM () { 0b10 }
307    
308  ## Implementations MUST act as if state machine in the spec  ## Implementations MUST act as if state machine in the spec
309    
310  sub _initialize_tokenizer ($) {  sub _initialize_tokenizer ($) {
311    my $self = shift;    my $self = shift;
312    $self->{state} = 'data'; # MUST    $self->{state} = DATA_STATE; # MUST
313    $self->{content_model} = PCDATA_CONTENT_MODEL; # be    $self->{content_model} = PCDATA_CONTENT_MODEL; # be
314    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE
315    undef $self->{current_attribute};    undef $self->{current_attribute};
# Line 177  sub _initialize_tokenizer ($) { Line 323  sub _initialize_tokenizer ($) {
323  } # _initialize_tokenizer  } # _initialize_tokenizer
324    
325  ## A token has:  ## A token has:
326  ##   ->{type} eq 'DOCTYPE', 'start tag', 'end tag', 'comment',  ##   ->{type} == DOCTYPE_TOKEN, START_TAG_TOKEN, END_TAG_TOKEN, COMMENT_TOKEN,
327  ##       'character', or 'end-of-file'  ##       CHARACTER_TOKEN, or END_OF_FILE_TOKEN
328  ##   ->{name} (DOCTYPE, start tag (tag name), end tag (tag name))  ##   ->{name} (DOCTYPE_TOKEN)
329  ##   ->{public_identifier} (DOCTYPE)  ##   ->{tag_name} (START_TAG_TOKEN, END_TAG_TOKEN)
330  ##   ->{system_identifier} (DOCTYPE)  ##   ->{public_identifier} (DOCTYPE_TOKEN)
331  ##   ->{correct} == 1 or 0 (DOCTYPE)  ##   ->{system_identifier} (DOCTYPE_TOKEN)
332  ##   ->{attributes} isa HASH (start tag, end tag)  ##   ->{correct} == 1 or 0 (DOCTYPE_TOKEN)
333  ##   ->{data} (comment, character)  ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)
334    ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
335    
336  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
337    
# Line 194  sub _initialize_tokenizer ($) { Line 341  sub _initialize_tokenizer ($) {
341  ## has completed loading.  If one has, then it MUST be executed  ## has completed loading.  If one has, then it MUST be executed
342  ## and removed from the list.  ## and removed from the list.
343    
344    ## NOTE: HTML5 "Writing HTML documents" section, applied to
345    ## documents and not to user agents and conformance checkers,
346    ## contains some requirements that are not detected by the
347    ## parsing algorithm:
348    ## - Some requirements on character encoding declarations. ## TODO
349    ## - "Elements MUST NOT contain content that their content model disallows."
350    ##   ... Some are parse error, some are not (will be reported by c.c.).
351    ## - Polytheistic slash SHOULD NOT be used. (Applied only to atheists.) ## TODO
352    ## - Text (in elements, attributes, and comments) SHOULD NOT contain
353    ##   control characters other than space characters. ## TODO: (what is control character? C0, C1 and DEL?  Unicode control character?)
354    
355    ## TODO: HTML5 poses authors two SHOULD-level requirements that cannot
356    ## be detected by the HTML5 parsing algorithm:
357    ## - Text,
358    
359  sub _get_next_token ($) {  sub _get_next_token ($) {
360    my $self = shift;    my $self = shift;
361    if (@{$self->{token}}) {    if (@{$self->{token}}) {
# Line 201  sub _get_next_token ($) { Line 363  sub _get_next_token ($) {
363    }    }
364    
365    A: {    A: {
366      if ($self->{state} eq 'data') {      if ($self->{state} == DATA_STATE) {
367        if ($self->{next_input_character} == 0x0026) { # &        if ($self->{next_input_character} == 0x0026) { # &
368          if ($self->{content_model} & CM_ENTITY) { # PCDATA | RCDATA          if ($self->{content_model} & CM_ENTITY) { # PCDATA | RCDATA
369            $self->{state} = 'entity data';            $self->{state} = ENTITY_DATA_STATE;
370            !!!next-input-character;            !!!next-input-character;
371            redo A;            redo A;
372          } else {          } else {
# Line 226  sub _get_next_token ($) { Line 388  sub _get_next_token ($) {
388          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA
389              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA
390               not $self->{escape})) {               not $self->{escape})) {
391            $self->{state} = 'tag open';            $self->{state} = TAG_OPEN_STATE;
392            !!!next-input-character;            !!!next-input-character;
393            redo A;            redo A;
394          } else {          } else {
# Line 243  sub _get_next_token ($) { Line 405  sub _get_next_token ($) {
405                    
406          #          #
407        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
408          !!!emit ({type => 'end-of-file'});          !!!emit ({type => END_OF_FILE_TOKEN});
409          last A; ## TODO: ok?          last A; ## TODO: ok?
410        }        }
411        # Anything else        # Anything else
412        my $token = {type => 'character',        my $token = {type => CHARACTER_TOKEN,
413                     data => chr $self->{next_input_character}};                     data => chr $self->{next_input_character}};
414        ## Stay in the data state        ## Stay in the data state
415        !!!next-input-character;        !!!next-input-character;
# Line 255  sub _get_next_token ($) { Line 417  sub _get_next_token ($) {
417        !!!emit ($token);        !!!emit ($token);
418    
419        redo A;        redo A;
420      } elsif ($self->{state} eq 'entity data') {      } elsif ($self->{state} == ENTITY_DATA_STATE) {
421        ## (cannot happen in CDATA state)        ## (cannot happen in CDATA state)
422                
423        my $token = $self->_tokenize_attempt_to_consume_an_entity (0);        my $token = $self->_tokenize_attempt_to_consume_an_entity (0);
424    
425        $self->{state} = 'data';        $self->{state} = DATA_STATE;
426        # next-input-character is already done        # next-input-character is already done
427    
428        unless (defined $token) {        unless (defined $token) {
429          !!!emit ({type => 'character', data => '&'});          !!!emit ({type => CHARACTER_TOKEN, data => '&'});
430        } else {        } else {
431          !!!emit ($token);          !!!emit ($token);
432        }        }
433    
434        redo A;        redo A;
435      } elsif ($self->{state} eq 'tag open') {      } elsif ($self->{state} == TAG_OPEN_STATE) {
436        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
437          if ($self->{next_input_character} == 0x002F) { # /          if ($self->{next_input_character} == 0x002F) { # /
438            !!!next-input-character;            !!!next-input-character;
439            $self->{state} = 'close tag open';            $self->{state} = CLOSE_TAG_OPEN_STATE;
440            redo A;            redo A;
441          } else {          } else {
442            ## reconsume            ## reconsume
443            $self->{state} = 'data';            $self->{state} = DATA_STATE;
444    
445            !!!emit ({type => 'character', data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<'});
446    
447            redo A;            redo A;
448          }          }
449        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA
450          if ($self->{next_input_character} == 0x0021) { # !          if ($self->{next_input_character} == 0x0021) { # !
451            $self->{state} = 'markup declaration open';            $self->{state} = MARKUP_DECLARATION_OPEN_STATE;
452            !!!next-input-character;            !!!next-input-character;
453            redo A;            redo A;
454          } elsif ($self->{next_input_character} == 0x002F) { # /          } elsif ($self->{next_input_character} == 0x002F) { # /
455            $self->{state} = 'close tag open';            $self->{state} = CLOSE_TAG_OPEN_STATE;
456            !!!next-input-character;            !!!next-input-character;
457            redo A;            redo A;
458          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_input_character} and
459                   $self->{next_input_character} <= 0x005A) { # A..Z                   $self->{next_input_character} <= 0x005A) { # A..Z
460            $self->{current_token}            $self->{current_token}
461              = {type => 'start tag',              = {type => START_TAG_TOKEN,
462                 tag_name => chr ($self->{next_input_character} + 0x0020)};                 tag_name => chr ($self->{next_input_character} + 0x0020)};
463            $self->{state} = 'tag name';            $self->{state} = TAG_NAME_STATE;
464            !!!next-input-character;            !!!next-input-character;
465            redo A;            redo A;
466          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_input_character} and
467                   $self->{next_input_character} <= 0x007A) { # a..z                   $self->{next_input_character} <= 0x007A) { # a..z
468            $self->{current_token} = {type => 'start tag',            $self->{current_token} = {type => START_TAG_TOKEN,
469                              tag_name => chr ($self->{next_input_character})};                              tag_name => chr ($self->{next_input_character})};
470            $self->{state} = 'tag name';            $self->{state} = TAG_NAME_STATE;
471            !!!next-input-character;            !!!next-input-character;
472            redo A;            redo A;
473          } elsif ($self->{next_input_character} == 0x003E) { # >          } elsif ($self->{next_input_character} == 0x003E) { # >
474            !!!parse-error (type => 'empty start tag');            !!!parse-error (type => 'empty start tag');
475            $self->{state} = 'data';            $self->{state} = DATA_STATE;
476            !!!next-input-character;            !!!next-input-character;
477    
478            !!!emit ({type => 'character', data => '<>'});            !!!emit ({type => CHARACTER_TOKEN, data => '<>'});
479    
480            redo A;            redo A;
481          } elsif ($self->{next_input_character} == 0x003F) { # ?          } elsif ($self->{next_input_character} == 0x003F) { # ?
482            !!!parse-error (type => 'pio');            !!!parse-error (type => 'pio');
483            $self->{state} = 'bogus comment';            $self->{state} = BOGUS_COMMENT_STATE;
484            ## $self->{next_input_character} is intentionally left as is            ## $self->{next_input_character} is intentionally left as is
485            redo A;            redo A;
486          } else {          } else {
487            !!!parse-error (type => 'bare stago');            !!!parse-error (type => 'bare stago');
488            $self->{state} = 'data';            $self->{state} = DATA_STATE;
489            ## reconsume            ## reconsume
490    
491            !!!emit ({type => 'character', data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<'});
492    
493            redo A;            redo A;
494          }          }
495        } else {        } else {
496          die "$0: $self->{content_model} in tag open";          die "$0: $self->{content_model} in tag open";
497        }        }
498      } elsif ($self->{state} eq 'close tag open') {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
499        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
500          if (defined $self->{last_emitted_start_tag_name}) {          if (defined $self->{last_emitted_start_tag_name}) {
501            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>
# Line 348  sub _get_next_token ($) { Line 510  sub _get_next_token ($) {
510              } else {              } else {
511                $self->{next_input_character} = shift @next_char; # reconsume                $self->{next_input_character} = shift @next_char; # reconsume
512                !!!back-next-input-character (@next_char);                !!!back-next-input-character (@next_char);
513                $self->{state} = 'data';                $self->{state} = DATA_STATE;
514    
515                !!!emit ({type => 'character', data => '</'});                !!!emit ({type => CHARACTER_TOKEN, data => '</'});
516        
517                redo A;                redo A;
518              }              }
# Line 367  sub _get_next_token ($) { Line 529  sub _get_next_token ($) {
529                    $self->{next_input_character} == -1) {                    $self->{next_input_character} == -1) {
530              $self->{next_input_character} = shift @next_char; # reconsume              $self->{next_input_character} = shift @next_char; # reconsume
531              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
532              $self->{state} = 'data';              $self->{state} = DATA_STATE;
533              !!!emit ({type => 'character', data => '</'});              !!!emit ({type => CHARACTER_TOKEN, data => '</'});
534              redo A;              redo A;
535            } else {            } else {
536              $self->{next_input_character} = shift @next_char;              $self->{next_input_character} = shift @next_char;
# Line 378  sub _get_next_token ($) { Line 540  sub _get_next_token ($) {
540          } else {          } else {
541            ## No start tag token has ever been emitted            ## No start tag token has ever been emitted
542            # next-input-character is already done            # next-input-character is already done
543            $self->{state} = 'data';            $self->{state} = DATA_STATE;
544            !!!emit ({type => 'character', data => '</'});            !!!emit ({type => CHARACTER_TOKEN, data => '</'});
545            redo A;            redo A;
546          }          }
547        }        }
548                
549        if (0x0041 <= $self->{next_input_character} and        if (0x0041 <= $self->{next_input_character} and
550            $self->{next_input_character} <= 0x005A) { # A..Z            $self->{next_input_character} <= 0x005A) { # A..Z
551          $self->{current_token} = {type => 'end tag',          $self->{current_token} = {type => END_TAG_TOKEN,
552                            tag_name => chr ($self->{next_input_character} + 0x0020)};                            tag_name => chr ($self->{next_input_character} + 0x0020)};
553          $self->{state} = 'tag name';          $self->{state} = TAG_NAME_STATE;
554          !!!next-input-character;          !!!next-input-character;
555          redo A;          redo A;
556        } elsif (0x0061 <= $self->{next_input_character} and        } elsif (0x0061 <= $self->{next_input_character} and
557                 $self->{next_input_character} <= 0x007A) { # a..z                 $self->{next_input_character} <= 0x007A) { # a..z
558          $self->{current_token} = {type => 'end tag',          $self->{current_token} = {type => END_TAG_TOKEN,
559                            tag_name => chr ($self->{next_input_character})};                            tag_name => chr ($self->{next_input_character})};
560          $self->{state} = 'tag name';          $self->{state} = TAG_NAME_STATE;
561          !!!next-input-character;          !!!next-input-character;
562          redo A;          redo A;
563        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
564          !!!parse-error (type => 'empty end tag');          !!!parse-error (type => 'empty end tag');
565          $self->{state} = 'data';          $self->{state} = DATA_STATE;
566          !!!next-input-character;          !!!next-input-character;
567          redo A;          redo A;
568        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
569          !!!parse-error (type => 'bare etago');          !!!parse-error (type => 'bare etago');
570          $self->{state} = 'data';          $self->{state} = DATA_STATE;
571          # reconsume          # reconsume
572    
573          !!!emit ({type => 'character', data => '</'});          !!!emit ({type => CHARACTER_TOKEN, data => '</'});
574    
575          redo A;          redo A;
576        } else {        } else {
577          !!!parse-error (type => 'bogus end tag');          !!!parse-error (type => 'bogus end tag');
578          $self->{state} = 'bogus comment';          $self->{state} = BOGUS_COMMENT_STATE;
579          ## $self->{next_input_character} is intentionally left as is          ## $self->{next_input_character} is intentionally left as is
580          redo A;          redo A;
581        }        }
582      } elsif ($self->{state} eq 'tag name') {      } elsif ($self->{state} == TAG_NAME_STATE) {
583        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
584            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
585            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
586            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
587            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
588          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
589          !!!next-input-character;          !!!next-input-character;
590          redo A;          redo A;
591        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
592          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
593            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
594                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
595            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
596          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
597            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
598            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
599              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 439  sub _get_next_token ($) { Line 601  sub _get_next_token ($) {
601          } else {          } else {
602            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
603          }          }
604          $self->{state} = 'data';          $self->{state} = DATA_STATE;
605          !!!next-input-character;          !!!next-input-character;
606    
607          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 454  sub _get_next_token ($) { Line 616  sub _get_next_token ($) {
616          redo A;          redo A;
617        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
618          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
619          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
620            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
621                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
622            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
623          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
624            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
625            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
626              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 466  sub _get_next_token ($) { Line 628  sub _get_next_token ($) {
628          } else {          } else {
629            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
630          }          }
631          $self->{state} = 'data';          $self->{state} = DATA_STATE;
632          # reconsume          # reconsume
633    
634          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 475  sub _get_next_token ($) { Line 637  sub _get_next_token ($) {
637        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_input_character} == 0x002F) { # /
638          !!!next-input-character;          !!!next-input-character;
639          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_input_character} == 0x003E and # >
640              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
641              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
642            # permitted slash            # permitted slash
643            #            #
644          } else {          } else {
645            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
646          }          }
647          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
648          # next-input-character is already done          # next-input-character is already done
649          redo A;          redo A;
650        } else {        } else {
# Line 492  sub _get_next_token ($) { Line 654  sub _get_next_token ($) {
654          !!!next-input-character;          !!!next-input-character;
655          redo A;          redo A;
656        }        }
657      } elsif ($self->{state} eq 'before attribute name') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_NAME_STATE) {
658        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
659            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
660            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
# Line 502  sub _get_next_token ($) { Line 664  sub _get_next_token ($) {
664          !!!next-input-character;          !!!next-input-character;
665          redo A;          redo A;
666        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
667          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
668            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
669                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
670            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
671          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
672            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
673            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
674              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 514  sub _get_next_token ($) { Line 676  sub _get_next_token ($) {
676          } else {          } else {
677            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
678          }          }
679          $self->{state} = 'data';          $self->{state} = DATA_STATE;
680          !!!next-input-character;          !!!next-input-character;
681    
682          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 524  sub _get_next_token ($) { Line 686  sub _get_next_token ($) {
686                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_input_character} <= 0x005A) { # A..Z
687          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),
688                                value => ''};                                value => ''};
689          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
690          !!!next-input-character;          !!!next-input-character;
691          redo A;          redo A;
692        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_input_character} == 0x002F) { # /
693          !!!next-input-character;          !!!next-input-character;
694          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_input_character} == 0x003E and # >
695              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
696              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
697            # permitted slash            # permitted slash
698            #            #
# Line 542  sub _get_next_token ($) { Line 704  sub _get_next_token ($) {
704          redo A;          redo A;
705        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
706          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
707          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
708            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
709                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
710            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
711          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
712            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
713            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
714              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 554  sub _get_next_token ($) { Line 716  sub _get_next_token ($) {
716          } else {          } else {
717            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
718          }          }
719          $self->{state} = 'data';          $self->{state} = DATA_STATE;
720          # reconsume          # reconsume
721    
722          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 563  sub _get_next_token ($) { Line 725  sub _get_next_token ($) {
725        } else {        } else {
726          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          $self->{current_attribute} = {name => chr ($self->{next_input_character}),
727                                value => ''};                                value => ''};
728          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
729          !!!next-input-character;          !!!next-input-character;
730          redo A;          redo A;
731        }        }
732      } elsif ($self->{state} eq 'attribute name') {      } elsif ($self->{state} == ATTRIBUTE_NAME_STATE) {
733        my $before_leave = sub {        my $before_leave = sub {
734          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
735              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
# Line 585  sub _get_next_token ($) { Line 747  sub _get_next_token ($) {
747            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
748            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
749          $before_leave->();          $before_leave->();
750          $self->{state} = 'after attribute name';          $self->{state} = AFTER_ATTRIBUTE_NAME_STATE;
751          !!!next-input-character;          !!!next-input-character;
752          redo A;          redo A;
753        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_input_character} == 0x003D) { # =
754          $before_leave->();          $before_leave->();
755          $self->{state} = 'before attribute value';          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
756          !!!next-input-character;          !!!next-input-character;
757          redo A;          redo A;
758        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
759          $before_leave->();          $before_leave->();
760          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
761            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
762                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
763            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
764          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
765            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
766            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
767              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 607  sub _get_next_token ($) { Line 769  sub _get_next_token ($) {
769          } else {          } else {
770            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
771          }          }
772          $self->{state} = 'data';          $self->{state} = DATA_STATE;
773          !!!next-input-character;          !!!next-input-character;
774    
775          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 623  sub _get_next_token ($) { Line 785  sub _get_next_token ($) {
785          $before_leave->();          $before_leave->();
786          !!!next-input-character;          !!!next-input-character;
787          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_input_character} == 0x003E and # >
788              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
789              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
790            # permitted slash            # permitted slash
791            #            #
792          } else {          } else {
793            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
794          }          }
795          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
796          # next-input-character is already done          # next-input-character is already done
797          redo A;          redo A;
798        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
799          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
800          $before_leave->();          $before_leave->();
801          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
802            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
803                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
804            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
805          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
806            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
807            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
808              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 648  sub _get_next_token ($) { Line 810  sub _get_next_token ($) {
810          } else {          } else {
811            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
812          }          }
813          $self->{state} = 'data';          $self->{state} = DATA_STATE;
814          # reconsume          # reconsume
815    
816          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 660  sub _get_next_token ($) { Line 822  sub _get_next_token ($) {
822          !!!next-input-character;          !!!next-input-character;
823          redo A;          redo A;
824        }        }
825      } elsif ($self->{state} eq 'after attribute name') {      } elsif ($self->{state} == AFTER_ATTRIBUTE_NAME_STATE) {
826        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
827            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
828            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
# Line 670  sub _get_next_token ($) { Line 832  sub _get_next_token ($) {
832          !!!next-input-character;          !!!next-input-character;
833          redo A;          redo A;
834        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_input_character} == 0x003D) { # =
835          $self->{state} = 'before attribute value';          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
836          !!!next-input-character;          !!!next-input-character;
837          redo A;          redo A;
838        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
839          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
840            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
841                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
842            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
843          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
844            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
845            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
846              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 686  sub _get_next_token ($) { Line 848  sub _get_next_token ($) {
848          } else {          } else {
849            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
850          }          }
851          $self->{state} = 'data';          $self->{state} = DATA_STATE;
852          !!!next-input-character;          !!!next-input-character;
853    
854          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 696  sub _get_next_token ($) { Line 858  sub _get_next_token ($) {
858                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_input_character} <= 0x005A) { # A..Z
859          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),
860                                value => ''};                                value => ''};
861          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
862          !!!next-input-character;          !!!next-input-character;
863          redo A;          redo A;
864        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_input_character} == 0x002F) { # /
865          !!!next-input-character;          !!!next-input-character;
866          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_input_character} == 0x003E and # >
867              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
868              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
869            # permitted slash            # permitted slash
870            #            #
# Line 710  sub _get_next_token ($) { Line 872  sub _get_next_token ($) {
872            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
873            ## TODO: Different error type for <aa / bb> than <aa/>            ## TODO: Different error type for <aa / bb> than <aa/>
874          }          }
875          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
876          # next-input-character is already done          # next-input-character is already done
877          redo A;          redo A;
878        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
879          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
880          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
881            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
882                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
883            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
884          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
885            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
886            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
887              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 727  sub _get_next_token ($) { Line 889  sub _get_next_token ($) {
889          } else {          } else {
890            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
891          }          }
892          $self->{state} = 'data';          $self->{state} = DATA_STATE;
893          # reconsume          # reconsume
894    
895          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 736  sub _get_next_token ($) { Line 898  sub _get_next_token ($) {
898        } else {        } else {
899          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          $self->{current_attribute} = {name => chr ($self->{next_input_character}),
900                                value => ''};                                value => ''};
901          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
902          !!!next-input-character;          !!!next-input-character;
903          redo A;                  redo A;        
904        }        }
905      } elsif ($self->{state} eq 'before attribute value') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_VALUE_STATE) {
906        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
907            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
908            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
# Line 750  sub _get_next_token ($) { Line 912  sub _get_next_token ($) {
912          !!!next-input-character;          !!!next-input-character;
913          redo A;          redo A;
914        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_input_character} == 0x0022) { # "
915          $self->{state} = 'attribute value (double-quoted)';          $self->{state} = ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE;
916          !!!next-input-character;          !!!next-input-character;
917          redo A;          redo A;
918        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_input_character} == 0x0026) { # &
919          $self->{state} = 'attribute value (unquoted)';          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
920          ## reconsume          ## reconsume
921          redo A;          redo A;
922        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_input_character} == 0x0027) { # '
923          $self->{state} = 'attribute value (single-quoted)';          $self->{state} = ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE;
924          !!!next-input-character;          !!!next-input-character;
925          redo A;          redo A;
926        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
927          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
928            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
929                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
930            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
931          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
932            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
933            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
934              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 774  sub _get_next_token ($) { Line 936  sub _get_next_token ($) {
936          } else {          } else {
937            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
938          }          }
939          $self->{state} = 'data';          $self->{state} = DATA_STATE;
940          !!!next-input-character;          !!!next-input-character;
941    
942          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 782  sub _get_next_token ($) { Line 944  sub _get_next_token ($) {
944          redo A;          redo A;
945        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
946          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
947          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
948            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
949                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
950            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
951          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
952            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
953            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
954              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 794  sub _get_next_token ($) { Line 956  sub _get_next_token ($) {
956          } else {          } else {
957            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
958          }          }
959          $self->{state} = 'data';          $self->{state} = DATA_STATE;
960          ## reconsume          ## reconsume
961    
962          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 802  sub _get_next_token ($) { Line 964  sub _get_next_token ($) {
964          redo A;          redo A;
965        } else {        } else {
966          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});
967          $self->{state} = 'attribute value (unquoted)';          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
968          !!!next-input-character;          !!!next-input-character;
969          redo A;          redo A;
970        }        }
971      } elsif ($self->{state} eq 'attribute value (double-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE) {
972        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_input_character} == 0x0022) { # "
973          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
974          !!!next-input-character;          !!!next-input-character;
975          redo A;          redo A;
976        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_input_character} == 0x0026) { # &
977          $self->{last_attribute_value_state} = 'attribute value (double-quoted)';          $self->{last_attribute_value_state} = $self->{state};
978          $self->{state} = 'entity in attribute value';          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
979          !!!next-input-character;          !!!next-input-character;
980          redo A;          redo A;
981        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
982          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
983          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
984            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
985                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
986            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
987          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
988            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
989            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
990              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 830  sub _get_next_token ($) { Line 992  sub _get_next_token ($) {
992          } else {          } else {
993            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
994          }          }
995          $self->{state} = 'data';          $self->{state} = DATA_STATE;
996          ## reconsume          ## reconsume
997    
998          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 842  sub _get_next_token ($) { Line 1004  sub _get_next_token ($) {
1004          !!!next-input-character;          !!!next-input-character;
1005          redo A;          redo A;
1006        }        }
1007      } elsif ($self->{state} eq 'attribute value (single-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE) {
1008        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_input_character} == 0x0027) { # '
1009          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1010          !!!next-input-character;          !!!next-input-character;
1011          redo A;          redo A;
1012        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_input_character} == 0x0026) { # &
1013          $self->{last_attribute_value_state} = 'attribute value (single-quoted)';          $self->{last_attribute_value_state} = $self->{state};
1014          $self->{state} = 'entity in attribute value';          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1015          !!!next-input-character;          !!!next-input-character;
1016          redo A;          redo A;
1017        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1018          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1019          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1020            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
1021                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
1022            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1023          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1024            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1025            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1026              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 866  sub _get_next_token ($) { Line 1028  sub _get_next_token ($) {
1028          } else {          } else {
1029            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1030          }          }
1031          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1032          ## reconsume          ## reconsume
1033    
1034          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 878  sub _get_next_token ($) { Line 1040  sub _get_next_token ($) {
1040          !!!next-input-character;          !!!next-input-character;
1041          redo A;          redo A;
1042        }        }
1043      } elsif ($self->{state} eq 'attribute value (unquoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_UNQUOTED_STATE) {
1044        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1045            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1046            $self->{next_input_character} == 0x000B or # HT            $self->{next_input_character} == 0x000B or # HT
1047            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
1048            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
1049          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1050          !!!next-input-character;          !!!next-input-character;
1051          redo A;          redo A;
1052        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_input_character} == 0x0026) { # &
1053          $self->{last_attribute_value_state} = 'attribute value (unquoted)';          $self->{last_attribute_value_state} = $self->{state};
1054          $self->{state} = 'entity in attribute value';          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1055          !!!next-input-character;          !!!next-input-character;
1056          redo A;          redo A;
1057        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1058          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1059            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
1060                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
1061            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1062          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1063            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1064            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1065              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 905  sub _get_next_token ($) { Line 1067  sub _get_next_token ($) {
1067          } else {          } else {
1068            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1069          }          }
1070          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1071          !!!next-input-character;          !!!next-input-character;
1072    
1073          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 913  sub _get_next_token ($) { Line 1075  sub _get_next_token ($) {
1075          redo A;          redo A;
1076        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1077          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1078          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1079            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
1080                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
1081            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1082          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1083            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1084            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1085              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 925  sub _get_next_token ($) { Line 1087  sub _get_next_token ($) {
1087          } else {          } else {
1088            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1089          }          }
1090          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1091          ## reconsume          ## reconsume
1092    
1093          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 937  sub _get_next_token ($) { Line 1099  sub _get_next_token ($) {
1099          !!!next-input-character;          !!!next-input-character;
1100          redo A;          redo A;
1101        }        }
1102      } elsif ($self->{state} eq 'entity in attribute value') {      } elsif ($self->{state} == ENTITY_IN_ATTRIBUTE_VALUE_STATE) {
1103        my $token = $self->_tokenize_attempt_to_consume_an_entity (1);        my $token = $self->_tokenize_attempt_to_consume_an_entity (1);
1104    
1105        unless (defined $token) {        unless (defined $token) {
# Line 950  sub _get_next_token ($) { Line 1112  sub _get_next_token ($) {
1112        $self->{state} = $self->{last_attribute_value_state};        $self->{state} = $self->{last_attribute_value_state};
1113        # next-input-character is already done        # next-input-character is already done
1114        redo A;        redo A;
1115      } elsif ($self->{state} eq 'bogus comment') {      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
1116        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1117                
1118        my $token = {type => 'comment', data => ''};        my $token = {type => COMMENT_TOKEN, data => ''};
1119    
1120        BC: {        BC: {
1121          if ($self->{next_input_character} == 0x003E) { # >          if ($self->{next_input_character} == 0x003E) { # >
1122            $self->{state} = 'data';            $self->{state} = DATA_STATE;
1123            !!!next-input-character;            !!!next-input-character;
1124    
1125            !!!emit ($token);            !!!emit ($token);
1126    
1127            redo A;            redo A;
1128          } elsif ($self->{next_input_character} == -1) {          } elsif ($self->{next_input_character} == -1) {
1129            $self->{state} = 'data';            $self->{state} = DATA_STATE;
1130            ## reconsume            ## reconsume
1131    
1132            !!!emit ($token);            !!!emit ($token);
# Line 976  sub _get_next_token ($) { Line 1138  sub _get_next_token ($) {
1138            redo BC;            redo BC;
1139          }          }
1140        } # BC        } # BC
1141      } elsif ($self->{state} eq 'markup declaration open') {      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
1142        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1143    
1144        my @next_char;        my @next_char;
# Line 986  sub _get_next_token ($) { Line 1148  sub _get_next_token ($) {
1148          !!!next-input-character;          !!!next-input-character;
1149          push @next_char, $self->{next_input_character};          push @next_char, $self->{next_input_character};
1150          if ($self->{next_input_character} == 0x002D) { # -          if ($self->{next_input_character} == 0x002D) { # -
1151            $self->{current_token} = {type => 'comment', data => ''};            $self->{current_token} = {type => COMMENT_TOKEN, data => ''};
1152            $self->{state} = 'comment start';            $self->{state} = COMMENT_START_STATE;
1153            !!!next-input-character;            !!!next-input-character;
1154            redo A;            redo A;
1155          }          }
# Line 1018  sub _get_next_token ($) { Line 1180  sub _get_next_token ($) {
1180                    if ($self->{next_input_character} == 0x0045 or # E                    if ($self->{next_input_character} == 0x0045 or # E
1181                        $self->{next_input_character} == 0x0065) { # e                        $self->{next_input_character} == 0x0065) { # e
1182                      ## ISSUE: What a stupid code this is!                      ## ISSUE: What a stupid code this is!
1183                      $self->{state} = 'DOCTYPE';                      $self->{state} = DOCTYPE_STATE;
1184                      !!!next-input-character;                      !!!next-input-character;
1185                      redo A;                      redo A;
1186                    }                    }
# Line 1032  sub _get_next_token ($) { Line 1194  sub _get_next_token ($) {
1194        !!!parse-error (type => 'bogus comment');        !!!parse-error (type => 'bogus comment');
1195        $self->{next_input_character} = shift @next_char;        $self->{next_input_character} = shift @next_char;
1196        !!!back-next-input-character (@next_char);        !!!back-next-input-character (@next_char);
1197        $self->{state} = 'bogus comment';        $self->{state} = BOGUS_COMMENT_STATE;
1198        redo A;        redo A;
1199                
1200        ## ISSUE: typos in spec: chacacters, is is a parse error        ## ISSUE: typos in spec: chacacters, is is a parse error
1201        ## 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?
1202      } elsif ($self->{state} eq 'comment start') {      } elsif ($self->{state} == COMMENT_START_STATE) {
1203        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_input_character} == 0x002D) { # -
1204          $self->{state} = 'comment start dash';          $self->{state} = COMMENT_START_DASH_STATE;
1205          !!!next-input-character;          !!!next-input-character;
1206          redo A;          redo A;
1207        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1208          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
1209          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1210          !!!next-input-character;          !!!next-input-character;
1211    
1212          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1052  sub _get_next_token ($) { Line 1214  sub _get_next_token ($) {
1214          redo A;          redo A;
1215        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1216          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1217          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1218          ## reconsume          ## reconsume
1219    
1220          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1061  sub _get_next_token ($) { Line 1223  sub _get_next_token ($) {
1223        } else {        } else {
1224          $self->{current_token}->{data} # comment          $self->{current_token}->{data} # comment
1225              .= chr ($self->{next_input_character});              .= chr ($self->{next_input_character});
1226          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
1227          !!!next-input-character;          !!!next-input-character;
1228          redo A;          redo A;
1229        }        }
1230      } elsif ($self->{state} eq 'comment start dash') {      } elsif ($self->{state} == COMMENT_START_DASH_STATE) {
1231        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_input_character} == 0x002D) { # -
1232          $self->{state} = 'comment end';          $self->{state} = COMMENT_END_STATE;
1233          !!!next-input-character;          !!!next-input-character;
1234          redo A;          redo A;
1235        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1236          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
1237          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1238          !!!next-input-character;          !!!next-input-character;
1239    
1240          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1080  sub _get_next_token ($) { Line 1242  sub _get_next_token ($) {
1242          redo A;          redo A;
1243        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1244          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1245          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1246          ## reconsume          ## reconsume
1247    
1248          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1089  sub _get_next_token ($) { Line 1251  sub _get_next_token ($) {
1251        } else {        } else {
1252          $self->{current_token}->{data} # comment          $self->{current_token}->{data} # comment
1253              .= '-' . chr ($self->{next_input_character});              .= '-' . chr ($self->{next_input_character});
1254          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
1255          !!!next-input-character;          !!!next-input-character;
1256          redo A;          redo A;
1257        }        }
1258      } elsif ($self->{state} eq 'comment') {      } elsif ($self->{state} == COMMENT_STATE) {
1259        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_input_character} == 0x002D) { # -
1260          $self->{state} = 'comment end dash';          $self->{state} = COMMENT_END_DASH_STATE;
1261          !!!next-input-character;          !!!next-input-character;
1262          redo A;          redo A;
1263        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1264          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1265          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1266          ## reconsume          ## reconsume
1267    
1268          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1112  sub _get_next_token ($) { Line 1274  sub _get_next_token ($) {
1274          !!!next-input-character;          !!!next-input-character;
1275          redo A;          redo A;
1276        }        }
1277      } elsif ($self->{state} eq 'comment end dash') {      } elsif ($self->{state} == COMMENT_END_DASH_STATE) {
1278        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_input_character} == 0x002D) { # -
1279          $self->{state} = 'comment end';          $self->{state} = COMMENT_END_STATE;
1280          !!!next-input-character;          !!!next-input-character;
1281          redo A;          redo A;
1282        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1283          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1284          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1285          ## reconsume          ## reconsume
1286    
1287          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1127  sub _get_next_token ($) { Line 1289  sub _get_next_token ($) {
1289          redo A;          redo A;
1290        } else {        } else {
1291          $self->{current_token}->{data} .= '-' . chr ($self->{next_input_character}); # comment          $self->{current_token}->{data} .= '-' . chr ($self->{next_input_character}); # comment
1292          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
1293          !!!next-input-character;          !!!next-input-character;
1294          redo A;          redo A;
1295        }        }
1296      } elsif ($self->{state} eq 'comment end') {      } elsif ($self->{state} == COMMENT_END_STATE) {
1297        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_input_character} == 0x003E) { # >
1298          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1299          !!!next-input-character;          !!!next-input-character;
1300    
1301          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1147  sub _get_next_token ($) { Line 1309  sub _get_next_token ($) {
1309          redo A;          redo A;
1310        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1311          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1312          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1313          ## reconsume          ## reconsume
1314    
1315          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1156  sub _get_next_token ($) { Line 1318  sub _get_next_token ($) {
1318        } else {        } else {
1319          !!!parse-error (type => 'dash in comment');          !!!parse-error (type => 'dash in comment');
1320          $self->{current_token}->{data} .= '--' . chr ($self->{next_input_character}); # comment          $self->{current_token}->{data} .= '--' . chr ($self->{next_input_character}); # comment
1321          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
1322          !!!next-input-character;          !!!next-input-character;
1323          redo A;          redo A;
1324        }        }
1325      } elsif ($self->{state} eq 'DOCTYPE') {      } elsif ($self->{state} == DOCTYPE_STATE) {
1326        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1327            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1328            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
1329            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
1330            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
1331          $self->{state} = 'before DOCTYPE name';          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
1332          !!!next-input-character;          !!!next-input-character;
1333          redo A;          redo A;
1334        } else {        } else {
1335          !!!parse-error (type => 'no space before DOCTYPE name');          !!!parse-error (type => 'no space before DOCTYPE name');
1336          $self->{state} = 'before DOCTYPE name';          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
1337          ## reconsume          ## reconsume
1338          redo A;          redo A;
1339        }        }
1340      } elsif ($self->{state} eq 'before DOCTYPE name') {      } elsif ($self->{state} == BEFORE_DOCTYPE_NAME_STATE) {
1341        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1342            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1343            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
# Line 1186  sub _get_next_token ($) { Line 1348  sub _get_next_token ($) {
1348          redo A;          redo A;
1349        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1350          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
1351          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1352          !!!next-input-character;          !!!next-input-character;
1353    
1354          !!!emit ({type => 'DOCTYPE'}); # incorrect          !!!emit ({type => DOCTYPE_TOKEN}); # incorrect
1355    
1356          redo A;          redo A;
1357        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1358          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
1359          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1360          ## reconsume          ## reconsume
1361    
1362          !!!emit ({type => 'DOCTYPE'}); # incorrect          !!!emit ({type => DOCTYPE_TOKEN}); # incorrect
1363    
1364          redo A;          redo A;
1365        } else {        } else {
1366          $self->{current_token}          $self->{current_token}
1367              = {type => 'DOCTYPE',              = {type => DOCTYPE_TOKEN,
1368                 name => chr ($self->{next_input_character}),                 name => chr ($self->{next_input_character}),
1369                 correct => 1};                 correct => 1};
1370  ## ISSUE: "Set the token's name name to the" in the spec  ## ISSUE: "Set the token's name name to the" in the spec
1371          $self->{state} = 'DOCTYPE name';          $self->{state} = DOCTYPE_NAME_STATE;
1372          !!!next-input-character;          !!!next-input-character;
1373          redo A;          redo A;
1374        }        }
1375      } elsif ($self->{state} eq 'DOCTYPE name') {      } elsif ($self->{state} == DOCTYPE_NAME_STATE) {
1376  ## ISSUE: Redundant "First," in the spec.  ## ISSUE: Redundant "First," in the spec.
1377        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1378            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1379            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
1380            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
1381            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
1382          $self->{state} = 'after DOCTYPE name';          $self->{state} = AFTER_DOCTYPE_NAME_STATE;
1383          !!!next-input-character;          !!!next-input-character;
1384          redo A;          redo A;
1385        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1386          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1387          !!!next-input-character;          !!!next-input-character;
1388    
1389          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
# Line 1229  sub _get_next_token ($) { Line 1391  sub _get_next_token ($) {
1391          redo A;          redo A;
1392        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1393          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1394          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1395          ## reconsume          ## reconsume
1396    
1397          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1243  sub _get_next_token ($) { Line 1405  sub _get_next_token ($) {
1405          !!!next-input-character;          !!!next-input-character;
1406          redo A;          redo A;
1407        }        }
1408      } elsif ($self->{state} eq 'after DOCTYPE name') {      } elsif ($self->{state} == AFTER_DOCTYPE_NAME_STATE) {
1409        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1410            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1411            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
# Line 1253  sub _get_next_token ($) { Line 1415  sub _get_next_token ($) {
1415          !!!next-input-character;          !!!next-input-character;
1416          redo A;          redo A;
1417        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1418          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1419          !!!next-input-character;          !!!next-input-character;
1420    
1421          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
# Line 1261  sub _get_next_token ($) { Line 1423  sub _get_next_token ($) {
1423          redo A;          redo A;
1424        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1425          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1426          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1427          ## reconsume          ## reconsume
1428    
1429          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1285  sub _get_next_token ($) { Line 1447  sub _get_next_token ($) {
1447                  !!!next-input-character;                  !!!next-input-character;
1448                  if ($self->{next_input_character} == 0x0043 or # C                  if ($self->{next_input_character} == 0x0043 or # C
1449                      $self->{next_input_character} == 0x0063) { # c                      $self->{next_input_character} == 0x0063) { # c
1450                    $self->{state} = 'before DOCTYPE public identifier';                    $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1451                    !!!next-input-character;                    !!!next-input-character;
1452                    redo A;                    redo A;
1453                  }                  }
# Line 1312  sub _get_next_token ($) { Line 1474  sub _get_next_token ($) {
1474                  !!!next-input-character;                  !!!next-input-character;
1475                  if ($self->{next_input_character} == 0x004D or # M                  if ($self->{next_input_character} == 0x004D or # M
1476                      $self->{next_input_character} == 0x006D) { # m                      $self->{next_input_character} == 0x006D) { # m
1477                    $self->{state} = 'before DOCTYPE system identifier';                    $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
1478                    !!!next-input-character;                    !!!next-input-character;
1479                    redo A;                    redo A;
1480                  }                  }
# Line 1328  sub _get_next_token ($) { Line 1490  sub _get_next_token ($) {
1490        }        }
1491    
1492        !!!parse-error (type => 'string after DOCTYPE name');        !!!parse-error (type => 'string after DOCTYPE name');
1493        $self->{state} = 'bogus DOCTYPE';        $self->{state} = BOGUS_DOCTYPE_STATE;
1494        # next-input-character is already done        # next-input-character is already done
1495        redo A;        redo A;
1496      } elsif ($self->{state} eq 'before DOCTYPE public identifier') {      } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
1497        if ({        if ({
1498              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1499              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
# Line 1341  sub _get_next_token ($) { Line 1503  sub _get_next_token ($) {
1503          redo A;          redo A;
1504        } elsif ($self->{next_input_character} eq 0x0022) { # "        } elsif ($self->{next_input_character} eq 0x0022) { # "
1505          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{current_token}->{public_identifier} = ''; # DOCTYPE
1506          $self->{state} = 'DOCTYPE public identifier (double-quoted)';          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;
1507          !!!next-input-character;          !!!next-input-character;
1508          redo A;          redo A;
1509        } elsif ($self->{next_input_character} eq 0x0027) { # '        } elsif ($self->{next_input_character} eq 0x0027) { # '
1510          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{current_token}->{public_identifier} = ''; # DOCTYPE
1511          $self->{state} = 'DOCTYPE public identifier (single-quoted)';          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;
1512          !!!next-input-character;          !!!next-input-character;
1513          redo A;          redo A;
1514        } elsif ($self->{next_input_character} eq 0x003E) { # >        } elsif ($self->{next_input_character} eq 0x003E) { # >
1515          !!!parse-error (type => 'no PUBLIC literal');          !!!parse-error (type => 'no PUBLIC literal');
1516    
1517          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1518          !!!next-input-character;          !!!next-input-character;
1519    
1520          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1362  sub _get_next_token ($) { Line 1524  sub _get_next_token ($) {
1524        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1525          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1526    
1527          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1528          ## reconsume          ## reconsume
1529    
1530          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1371  sub _get_next_token ($) { Line 1533  sub _get_next_token ($) {
1533          redo A;          redo A;
1534        } else {        } else {
1535          !!!parse-error (type => 'string after PUBLIC');          !!!parse-error (type => 'string after PUBLIC');
1536          $self->{state} = 'bogus DOCTYPE';          $self->{state} = BOGUS_DOCTYPE_STATE;
1537          !!!next-input-character;          !!!next-input-character;
1538          redo A;          redo A;
1539        }        }
1540      } elsif ($self->{state} eq 'DOCTYPE public identifier (double-quoted)') {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {
1541        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_input_character} == 0x0022) { # "
1542          $self->{state} = 'after DOCTYPE public identifier';          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1543          !!!next-input-character;          !!!next-input-character;
1544          redo A;          redo A;
1545        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1546          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
1547    
1548          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1549          ## reconsume          ## reconsume
1550    
1551          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1397  sub _get_next_token ($) { Line 1559  sub _get_next_token ($) {
1559          !!!next-input-character;          !!!next-input-character;
1560          redo A;          redo A;
1561        }        }
1562      } elsif ($self->{state} eq 'DOCTYPE public identifier (single-quoted)') {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {
1563        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_input_character} == 0x0027) { # '
1564          $self->{state} = 'after DOCTYPE public identifier';          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1565          !!!next-input-character;          !!!next-input-character;
1566          redo A;          redo A;
1567        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1568          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
1569    
1570          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1571          ## reconsume          ## reconsume
1572    
1573          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1419  sub _get_next_token ($) { Line 1581  sub _get_next_token ($) {
1581          !!!next-input-character;          !!!next-input-character;
1582          redo A;          redo A;
1583        }        }
1584      } elsif ($self->{state} eq 'after DOCTYPE public identifier') {      } elsif ($self->{state} == AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
1585        if ({        if ({
1586              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1587              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
# Line 1429  sub _get_next_token ($) { Line 1591  sub _get_next_token ($) {
1591          redo A;          redo A;
1592        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_input_character} == 0x0022) { # "
1593          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1594          $self->{state} = 'DOCTYPE system identifier (double-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
1595          !!!next-input-character;          !!!next-input-character;
1596          redo A;          redo A;
1597        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_input_character} == 0x0027) { # '
1598          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1599          $self->{state} = 'DOCTYPE system identifier (single-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
1600          !!!next-input-character;          !!!next-input-character;
1601          redo A;          redo A;
1602        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1603          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1604          !!!next-input-character;          !!!next-input-character;
1605    
1606          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
# Line 1447  sub _get_next_token ($) { Line 1609  sub _get_next_token ($) {
1609        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1610          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1611    
1612          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1613          ## reconsume          ## reconsume
1614    
1615          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1456  sub _get_next_token ($) { Line 1618  sub _get_next_token ($) {
1618          redo A;          redo A;
1619        } else {        } else {
1620          !!!parse-error (type => 'string after PUBLIC literal');          !!!parse-error (type => 'string after PUBLIC literal');
1621          $self->{state} = 'bogus DOCTYPE';          $self->{state} = BOGUS_DOCTYPE_STATE;
1622          !!!next-input-character;          !!!next-input-character;
1623          redo A;          redo A;
1624        }        }
1625      } elsif ($self->{state} eq 'before DOCTYPE system identifier') {      } elsif ($self->{state} == BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
1626        if ({        if ({
1627              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1628              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
# Line 1470  sub _get_next_token ($) { Line 1632  sub _get_next_token ($) {
1632          redo A;          redo A;
1633        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_input_character} == 0x0022) { # "
1634          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1635          $self->{state} = 'DOCTYPE system identifier (double-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
1636          !!!next-input-character;          !!!next-input-character;
1637          redo A;          redo A;
1638        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_input_character} == 0x0027) { # '
1639          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1640          $self->{state} = 'DOCTYPE system identifier (single-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
1641          !!!next-input-character;          !!!next-input-character;
1642          redo A;          redo A;
1643        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1644          !!!parse-error (type => 'no SYSTEM literal');          !!!parse-error (type => 'no SYSTEM literal');
1645          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1646          !!!next-input-character;          !!!next-input-character;
1647    
1648          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1490  sub _get_next_token ($) { Line 1652  sub _get_next_token ($) {
1652        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1653          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1654    
1655          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1656          ## reconsume          ## reconsume
1657    
1658          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1499  sub _get_next_token ($) { Line 1661  sub _get_next_token ($) {
1661          redo A;          redo A;
1662        } else {        } else {
1663          !!!parse-error (type => 'string after SYSTEM');          !!!parse-error (type => 'string after SYSTEM');
1664          $self->{state} = 'bogus DOCTYPE';          $self->{state} = BOGUS_DOCTYPE_STATE;
1665          !!!next-input-character;          !!!next-input-character;
1666          redo A;          redo A;
1667        }        }
1668      } elsif ($self->{state} eq 'DOCTYPE system identifier (double-quoted)') {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {
1669        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_input_character} == 0x0022) { # "
1670          $self->{state} = 'after DOCTYPE system identifier';          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
1671          !!!next-input-character;          !!!next-input-character;
1672          redo A;          redo A;
1673        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1674          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
1675    
1676          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1677          ## reconsume          ## reconsume
1678    
1679          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1525  sub _get_next_token ($) { Line 1687  sub _get_next_token ($) {
1687          !!!next-input-character;          !!!next-input-character;
1688          redo A;          redo A;
1689        }        }
1690      } elsif ($self->{state} eq 'DOCTYPE system identifier (single-quoted)') {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {
1691        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_input_character} == 0x0027) { # '
1692          $self->{state} = 'after DOCTYPE system identifier';          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
1693          !!!next-input-character;          !!!next-input-character;
1694          redo A;          redo A;
1695        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1696          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
1697    
1698          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1699          ## reconsume          ## reconsume
1700    
1701          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1547  sub _get_next_token ($) { Line 1709  sub _get_next_token ($) {
1709          !!!next-input-character;          !!!next-input-character;
1710          redo A;          redo A;
1711        }        }
1712      } elsif ($self->{state} eq 'after DOCTYPE system identifier') {      } elsif ($self->{state} == AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
1713        if ({        if ({
1714              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1715              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
# Line 1556  sub _get_next_token ($) { Line 1718  sub _get_next_token ($) {
1718          !!!next-input-character;          !!!next-input-character;
1719          redo A;          redo A;
1720        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1721          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1722          !!!next-input-character;          !!!next-input-character;
1723    
1724          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
# Line 1565  sub _get_next_token ($) { Line 1727  sub _get_next_token ($) {
1727        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1728          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1729    
1730          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1731          ## reconsume          ## reconsume
1732    
1733          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1574  sub _get_next_token ($) { Line 1736  sub _get_next_token ($) {
1736          redo A;          redo A;
1737        } else {        } else {
1738          !!!parse-error (type => 'string after SYSTEM literal');          !!!parse-error (type => 'string after SYSTEM literal');
1739          $self->{state} = 'bogus DOCTYPE';          $self->{state} = BOGUS_DOCTYPE_STATE;
1740          !!!next-input-character;          !!!next-input-character;
1741          redo A;          redo A;
1742        }        }
1743      } elsif ($self->{state} eq 'bogus DOCTYPE') {      } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {
1744        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_input_character} == 0x003E) { # >
1745          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1746          !!!next-input-character;          !!!next-input-character;
1747    
1748          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1589  sub _get_next_token ($) { Line 1751  sub _get_next_token ($) {
1751          redo A;          redo A;
1752        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1753          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1754          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1755          ## reconsume          ## reconsume
1756    
1757          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1670  sub _tokenize_attempt_to_consume_an_enti Line 1832  sub _tokenize_attempt_to_consume_an_enti
1832            $code = $c1_entity_char->{$code};            $code = $c1_entity_char->{$code};
1833          }          }
1834    
1835          return {type => 'character', data => chr $code};          return {type => CHARACTER_TOKEN, data => chr $code};
1836        } # X        } # X
1837      } elsif (0x0030 <= $self->{next_input_character} and      } elsif (0x0030 <= $self->{next_input_character} and
1838               $self->{next_input_character} <= 0x0039) { # 0..9               $self->{next_input_character} <= 0x0039) { # 0..9
# Line 1705  sub _tokenize_attempt_to_consume_an_enti Line 1867  sub _tokenize_attempt_to_consume_an_enti
1867          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
1868        }        }
1869                
1870        return {type => 'character', data => chr $code};        return {type => CHARACTER_TOKEN, data => chr $code};
1871      } else {      } else {
1872        !!!parse-error (type => 'bare nero');        !!!parse-error (type => 'bare nero');
1873        !!!back-next-input-character ($self->{next_input_character});        !!!back-next-input-character ($self->{next_input_character});
# Line 1753  sub _tokenize_attempt_to_consume_an_enti Line 1915  sub _tokenize_attempt_to_consume_an_enti
1915      }      }
1916            
1917      if ($match > 0) {      if ($match > 0) {
1918        return {type => 'character', data => $value};        return {type => CHARACTER_TOKEN, data => $value};
1919      } elsif ($match < 0) {      } elsif ($match < 0) {
1920        !!!parse-error (type => 'no refc');        !!!parse-error (type => 'no refc');
1921        if ($in_attr and $match < -1) {        if ($in_attr and $match < -1) {
1922          return {type => 'character', data => '&'.$entity_name};          return {type => CHARACTER_TOKEN, data => '&'.$entity_name};
1923        } else {        } else {
1924          return {type => 'character', data => $value};          return {type => CHARACTER_TOKEN, data => $value};
1925        }        }
1926      } else {      } else {
1927        !!!parse-error (type => 'bare ero');        !!!parse-error (type => 'bare ero');
1928        ## NOTE: No characters are consumed in the spec.        ## NOTE: No characters are consumed in the spec.
1929        return {type => 'character', data => '&'.$value};        return {type => CHARACTER_TOKEN, data => '&'.$value};
1930      }      }
1931    } else {    } else {
1932      ## no characters are consumed      ## no characters are consumed
# Line 1806  sub _construct_tree ($) { Line 1968  sub _construct_tree ($) {
1968        
1969    !!!next-token;    !!!next-token;
1970    
1971    $self->{insertion_mode} = 'before head';    $self->{insertion_mode} = BEFORE_HEAD_IM;
1972    undef $self->{form_element};    undef $self->{form_element};
1973    undef $self->{head_element};    undef $self->{head_element};
1974    $self->{open_elements} = [];    $self->{open_elements} = [];
# Line 1820  sub _construct_tree ($) { Line 1982  sub _construct_tree ($) {
1982  sub _tree_construction_initial ($) {  sub _tree_construction_initial ($) {
1983    my $self = shift;    my $self = shift;
1984    INITIAL: {    INITIAL: {
1985      if ($token->{type} eq 'DOCTYPE') {      if ($token->{type} == DOCTYPE_TOKEN) {
1986        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"
1987        ## error, switch to a conformance checking mode for another        ## error, switch to a conformance checking mode for another
1988        ## language.        ## language.
# Line 1947  sub _tree_construction_initial ($) { Line 2109  sub _tree_construction_initial ($) {
2109        !!!next-token;        !!!next-token;
2110        return;        return;
2111      } elsif ({      } elsif ({
2112                'start tag' => 1,                START_TAG_TOKEN, 1,
2113                'end tag' => 1,                END_TAG_TOKEN, 1,
2114                'end-of-file' => 1,                END_OF_FILE_TOKEN, 1,
2115               }->{$token->{type}}) {               }->{$token->{type}}) {
2116        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE');
2117        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
2118        ## Go to the root element phase        ## Go to the root element phase
2119        ## reprocess        ## reprocess
2120        return;        return;
2121      } elsif ($token->{type} eq 'character') {      } elsif ($token->{type} == CHARACTER_TOKEN) {
2122        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
2123          ## Ignore the token          ## Ignore the token
2124    
# Line 1972  sub _tree_construction_initial ($) { Line 2134  sub _tree_construction_initial ($) {
2134        ## Go to the root element phase        ## Go to the root element phase
2135        ## reprocess        ## reprocess
2136        return;        return;
2137      } elsif ($token->{type} eq 'comment') {      } elsif ($token->{type} == COMMENT_TOKEN) {
2138        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
2139        $self->{document}->append_child ($comment);        $self->{document}->append_child ($comment);
2140                
# Line 1980  sub _tree_construction_initial ($) { Line 2142  sub _tree_construction_initial ($) {
2142        !!!next-token;        !!!next-token;
2143        redo INITIAL;        redo INITIAL;
2144      } else {      } else {
2145        die "$0: $token->{type}: Unknown token";        die "$0: $token->{type}: Unknown token type";
2146      }      }
2147    } # INITIAL    } # INITIAL
2148  } # _tree_construction_initial  } # _tree_construction_initial
# Line 1989  sub _tree_construction_root_element ($) Line 2151  sub _tree_construction_root_element ($)
2151    my $self = shift;    my $self = shift;
2152        
2153    B: {    B: {
2154        if ($token->{type} eq 'DOCTYPE') {        if ($token->{type} == DOCTYPE_TOKEN) {
2155          !!!parse-error (type => 'in html:#DOCTYPE');          !!!parse-error (type => 'in html:#DOCTYPE');
2156          ## Ignore the token          ## Ignore the token
2157          ## Stay in the phase          ## Stay in the phase
2158          !!!next-token;          !!!next-token;
2159          redo B;          redo B;
2160        } elsif ($token->{type} eq 'comment') {        } elsif ($token->{type} == COMMENT_TOKEN) {
2161          my $comment = $self->{document}->create_comment ($token->{data});          my $comment = $self->{document}->create_comment ($token->{data});
2162          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
2163          ## Stay in the phase          ## Stay in the phase
2164          !!!next-token;          !!!next-token;
2165          redo B;          redo B;
2166        } elsif ($token->{type} eq 'character') {        } elsif ($token->{type} == CHARACTER_TOKEN) {
2167          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
2168            ## Ignore the token.            ## Ignore the token.
2169    
# Line 2011  sub _tree_construction_root_element ($) Line 2173  sub _tree_construction_root_element ($)
2173              redo B;              redo B;
2174            }            }
2175          }          }
2176    
2177            $self->{application_cache_selection}->(undef);
2178    
2179            #
2180          } elsif ($token->{type} == START_TAG_TOKEN) {
2181            if ($token->{tag_name} eq 'html' and
2182                $token->{attributes}->{manifest}) { ## ISSUE: Spec spells as "application"
2183              $self->{application_cache_selection}
2184                   ->($token->{attributes}->{manifest}->{value});
2185              ## ISSUE: No relative reference resolution?
2186            } else {
2187              $self->{application_cache_selection}->(undef);
2188            }
2189    
2190            ## ISSUE: There is an issue in the spec
2191          #          #
2192        } elsif ({        } elsif ({
2193                  'start tag' => 1,                  END_TAG_TOKEN, 1,
2194                  'end tag' => 1,                  END_OF_FILE_TOKEN, 1,
                 'end-of-file' => 1,  
2195                 }->{$token->{type}}) {                 }->{$token->{type}}) {
2196            $self->{application_cache_selection}->(undef);
2197    
2198          ## ISSUE: There is an issue in the spec          ## ISSUE: There is an issue in the spec
2199          #          #
2200        } else {        } else {
2201          die "$0: $token->{type}: Unknown token";          die "$0: $token->{type}: Unknown token type";
2202        }        }
2203    
2204        my $root_element; !!!create-element ($root_element, 'html');        my $root_element; !!!create-element ($root_element, 'html');
2205        $self->{document}->append_child ($root_element);        $self->{document}->append_child ($root_element);
2206        push @{$self->{open_elements}}, [$root_element, 'html'];        push @{$self->{open_elements}}, [$root_element, 'html'];
# Line 2062  sub _reset_insertion_mode ($) { Line 2241  sub _reset_insertion_mode ($) {
2241            
2242        ## Step 4..13        ## Step 4..13
2243        my $new_mode = {        my $new_mode = {
2244                        select => 'in select',                        select => IN_SELECT_IM,
2245                        td => 'in cell',                        td => IN_CELL_IM,
2246                        th => 'in cell',                        th => IN_CELL_IM,
2247                        tr => 'in row',                        tr => IN_ROW_IM,
2248                        tbody => 'in table body',                        tbody => IN_TABLE_BODY_IM,
2249                        thead => 'in table body',                        thead => IN_TABLE_BODY_IM,
2250                        tfoot => 'in table body',                        tfoot => IN_TABLE_BODY_IM,
2251                        caption => 'in caption',                        caption => IN_CAPTION_IM,
2252                        colgroup => 'in column group',                        colgroup => IN_COLUMN_GROUP_IM,
2253                        table => 'in table',                        table => IN_TABLE_IM,
2254                        head => 'in body', # not in head!                        head => IN_BODY_IM, # not in head!
2255                        body => 'in body',                        body => IN_BODY_IM,
2256                        frameset => 'in frameset',                        frameset => IN_FRAMESET_IM,
2257                       }->{$node->[1]};                       }->{$node->[1]};
2258        $self->{insertion_mode} = $new_mode and return if defined $new_mode;        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
2259                
2260        ## Step 14        ## Step 14
2261        if ($node->[1] eq 'html') {        if ($node->[1] eq 'html') {
2262          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
2263            $self->{insertion_mode} = 'before head';            $self->{insertion_mode} = BEFORE_HEAD_IM;
2264          } else {          } else {
2265            $self->{insertion_mode} = 'after head';            $self->{insertion_mode} = AFTER_HEAD_IM;
2266          }          }
2267          return;          return;
2268        }        }
2269                
2270        ## Step 15        ## Step 15
2271        $self->{insertion_mode} = 'in body' and return if $last;        $self->{insertion_mode} = IN_BODY_IM and return if $last;
2272                
2273        ## Step 16        ## Step 16
2274        $i--;        $i--;
# Line 2103  sub _reset_insertion_mode ($) { Line 2282  sub _reset_insertion_mode ($) {
2282  sub _tree_construction_main ($) {  sub _tree_construction_main ($) {
2283    my $self = shift;    my $self = shift;
2284    
   my $previous_insertion_mode;  
   
2285    my $active_formatting_elements = [];    my $active_formatting_elements = [];
2286    
2287    my $reconstruct_active_formatting_elements = sub { # MUST    my $reconstruct_active_formatting_elements = sub { # MUST
# Line 2205  sub _tree_construction_main ($) { Line 2382  sub _tree_construction_main ($) {
2382      ## Step 4      ## Step 4
2383      my $text = '';      my $text = '';
2384      !!!next-token;      !!!next-token;
2385      while ($token->{type} eq 'character') { # or until stop tokenizing      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
2386        $text .= $token->{data};        $text .= $token->{data};
2387        !!!next-token;        !!!next-token;
2388      }      }
# Line 2220  sub _tree_construction_main ($) { Line 2397  sub _tree_construction_main ($) {
2397      $self->{content_model} = PCDATA_CONTENT_MODEL;      $self->{content_model} = PCDATA_CONTENT_MODEL;
2398    
2399      ## Step 7      ## Step 7
2400      if ($token->{type} eq 'end tag' and $token->{tag_name} eq $start_tag_name) {      if ($token->{type} == END_TAG_TOKEN and $token->{tag_name} eq $start_tag_name) {
2401        ## Ignore the token        ## Ignore the token
2402      } elsif ($content_model_flag == CDATA_CONTENT_MODEL) {      } elsif ($content_model_flag == CDATA_CONTENT_MODEL) {
2403        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!parse-error (type => 'in CDATA:#'.$token->{type});
# Line 2243  sub _tree_construction_main ($) { Line 2420  sub _tree_construction_main ($) {
2420            
2421      my $text = '';      my $text = '';
2422      !!!next-token;      !!!next-token;
2423      while ($token->{type} eq 'character') {      while ($token->{type} == CHARACTER_TOKEN) {
2424        $text .= $token->{data};        $text .= $token->{data};
2425        !!!next-token;        !!!next-token;
2426      } # stop if non-character token or tokenizer stops tokenising      } # stop if non-character token or tokenizer stops tokenising
# Line 2253  sub _tree_construction_main ($) { Line 2430  sub _tree_construction_main ($) {
2430                                
2431      $self->{content_model} = PCDATA_CONTENT_MODEL;      $self->{content_model} = PCDATA_CONTENT_MODEL;
2432    
2433      if ($token->{type} eq 'end tag' and      if ($token->{type} == END_TAG_TOKEN and
2434          $token->{tag_name} eq 'script') {          $token->{tag_name} eq 'script') {
2435        ## Ignore the token        ## Ignore the token
2436      } else {      } else {
# Line 2496  sub _tree_construction_main ($) { Line 2673  sub _tree_construction_main ($) {
2673                         }                         }
2674    }; # $insert_to_foster    }; # $insert_to_foster
2675    
2676    my $in_body = sub {    my $insert;
     my $insert = shift;  
     if ($token->{type} eq 'start tag') {  
       if ($token->{tag_name} eq 'script') {  
         ## NOTE: This is an "as if in head" code clone  
         $script_start_tag->($insert);  
         return;  
       } elsif ($token->{tag_name} eq 'style') {  
         ## NOTE: This is an "as if in head" code clone  
         $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);  
         return;  
       } elsif ({  
                 base => 1, link => 1,  
                }->{$token->{tag_name}}) {  
         ## NOTE: This is an "as if in head" code clone, only "-t" differs  
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.  
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'meta') {  
         ## NOTE: This is an "as if in head" code clone, only "-t" differs  
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.  
   
         unless ($self->{confident}) {  
           my $charset;  
           if ($token->{attributes}->{charset}) { ## TODO: And if supported  
             $charset = $token->{attributes}->{charset}->{value};  
           }  
           if ($token->{attributes}->{'http-equiv'}) {  
             ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.  
             if ($token->{attributes}->{'http-equiv'}->{value}  
                 =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=  
                     [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|  
                     ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {  
               $charset = defined $1 ? $1 : defined $2 ? $2 : $3;  
             } ## TODO: And if supported  
           }  
           ## TODO: Change the encoding  
         }  
   
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'title') {  
         !!!parse-error (type => 'in body:title');  
         ## NOTE: This is an "as if in head" code clone  
         $parse_rcdata->(RCDATA_CONTENT_MODEL, sub {  
           if (defined $self->{head_element}) {  
             $self->{head_element}->append_child ($_[0]);  
           } else {  
             $insert->($_[0]);  
           }  
         });  
         return;  
       } elsif ($token->{tag_name} eq 'body') {  
         !!!parse-error (type => 'in body:body');  
                 
         if (@{$self->{open_elements}} == 1 or  
             $self->{open_elements}->[1]->[1] ne 'body') {  
           ## Ignore the token  
         } else {  
           my $body_el = $self->{open_elements}->[1]->[0];  
           for my $attr_name (keys %{$token->{attributes}}) {  
             unless ($body_el->has_attribute_ns (undef, $attr_name)) {  
               $body_el->set_attribute_ns  
                 (undef, [undef, $attr_name],  
                  $token->{attributes}->{$attr_name}->{value});  
             }  
           }  
         }  
         !!!next-token;  
         return;  
       } elsif ({  
                 address => 1, blockquote => 1, center => 1, dir => 1,  
                 div => 1, dl => 1, fieldset => 1, listing => 1,  
                 menu => 1, ol => 1, p => 1, ul => 1,  
                 pre => 1,  
                }->{$token->{tag_name}}) {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         if ($token->{tag_name} eq 'pre') {  
           !!!next-token;  
           if ($token->{type} eq 'character') {  
             $token->{data} =~ s/^\x0A//;  
             unless (length $token->{data}) {  
               !!!next-token;  
             }  
           }  
         } else {  
           !!!next-token;  
         }  
         return;  
       } elsif ($token->{tag_name} eq 'form') {  
         if (defined $self->{form_element}) {  
           !!!parse-error (type => 'in form:form');  
           ## Ignore the token  
           !!!next-token;  
           return;  
         } else {  
           ## has a p element in scope  
           INSCOPE: for (reverse @{$self->{open_elements}}) {  
             if ($_->[1] eq 'p') {  
               !!!back-token;  
               $token = {type => 'end tag', tag_name => 'p'};  
               return;  
             } elsif ({  
                       table => 1, caption => 1, td => 1, th => 1,  
                       button => 1, marquee => 1, object => 1, html => 1,  
                      }->{$_->[1]}) {  
               last INSCOPE;  
             }  
           } # INSCOPE  
               
           !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
           $self->{form_element} = $self->{open_elements}->[-1]->[0];  
           !!!next-token;  
           return;  
         }  
       } elsif ($token->{tag_name} eq 'li') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## Step 1  
         my $i = -1;  
         my $node = $self->{open_elements}->[$i];  
         LI: {  
           ## Step 2  
           if ($node->[1] eq 'li') {  
             if ($i != -1) {  
               !!!parse-error (type => 'end tag missing:'.  
                               $self->{open_elements}->[-1]->[1]);  
             }  
             splice @{$self->{open_elements}}, $i;  
             last LI;  
           }  
             
           ## Step 3  
           if (not $formatting_category->{$node->[1]} and  
               #not $phrasing_category->{$node->[1]} and  
               ($special_category->{$node->[1]} or  
                $scoping_category->{$node->[1]}) and  
               $node->[1] ne 'address' and $node->[1] ne 'div') {  
             last LI;  
           }  
             
           ## Step 4  
           $i--;  
           $node = $self->{open_elements}->[$i];  
           redo LI;  
         } # LI  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'dd' or $token->{tag_name} eq 'dt') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## Step 1  
         my $i = -1;  
         my $node = $self->{open_elements}->[$i];  
         LI: {  
           ## Step 2  
           if ($node->[1] eq 'dt' or $node->[1] eq 'dd') {  
             if ($i != -1) {  
               !!!parse-error (type => 'end tag missing:'.  
                               $self->{open_elements}->[-1]->[1]);  
             }  
             splice @{$self->{open_elements}}, $i;  
             last LI;  
           }  
             
           ## Step 3  
           if (not $formatting_category->{$node->[1]} and  
               #not $phrasing_category->{$node->[1]} and  
               ($special_category->{$node->[1]} or  
                $scoping_category->{$node->[1]}) and  
               $node->[1] ne 'address' and $node->[1] ne 'div') {  
             last LI;  
           }  
             
           ## Step 4  
           $i--;  
           $node = $self->{open_elements}->[$i];  
           redo LI;  
         } # LI  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'plaintext') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         $self->{content_model} = PLAINTEXT_CONTENT_MODEL;  
             
         !!!next-token;  
         return;  
       } elsif ({  
                 h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
                }->{$token->{tag_name}}) {  
         ## has a p element in scope  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## NOTE: See <http://html5.org/tools/web-apps-tracker?from=925&to=926>  
         ## has an element in scope  
         #my $i;  
         #INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
         #  my $node = $self->{open_elements}->[$_];  
         #  if ({  
         #       h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
         #      }->{$node->[1]}) {  
         #    $i = $_;  
         #    last INSCOPE;  
         #  } elsif ({  
         #            table => 1, caption => 1, td => 1, th => 1,  
         #            button => 1, marquee => 1, object => 1, html => 1,  
         #           }->{$node->[1]}) {  
         #    last INSCOPE;  
         #  }  
         #} # INSCOPE  
         #    
         #if (defined $i) {  
         #  !!! parse-error (type => 'in hn:hn');  
         #  splice @{$self->{open_elements}}, $i;  
         #}  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'a') {  
         AFE: for my $i (reverse 0..$#$active_formatting_elements) {  
           my $node = $active_formatting_elements->[$i];  
           if ($node->[1] eq 'a') {  
             !!!parse-error (type => 'in a:a');  
               
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'a'};  
             $formatting_end_tag->($token->{tag_name});  
               
             AFE2: for (reverse 0..$#$active_formatting_elements) {  
               if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {  
                 splice @$active_formatting_elements, $_, 1;  
                 last AFE2;  
               }  
             } # AFE2  
             OE: for (reverse 0..$#{$self->{open_elements}}) {  
               if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {  
                 splice @{$self->{open_elements}}, $_, 1;  
                 last OE;  
               }  
             } # OE  
             last AFE;  
           } elsif ($node->[0] eq '#marker') {  
             last AFE;  
           }  
         } # AFE  
             
         $reconstruct_active_formatting_elements->($insert_to_current);  
   
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, $self->{open_elements}->[-1];  
   
         !!!next-token;  
         return;  
       } elsif ({  
                 b => 1, big => 1, em => 1, font => 1, i => 1,  
                 s => 1, small => 1, strile => 1,  
                 strong => 1, tt => 1, u => 1,  
                }->{$token->{tag_name}}) {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, $self->{open_elements}->[-1];  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'nobr') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
   
         ## has a |nobr| element in scope  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq 'nobr') {  
             !!!parse-error (type => 'not closed:nobr');  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'nobr'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, $self->{open_elements}->[-1];  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'button') {  
         ## has a button element in scope  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq 'button') {  
             !!!parse-error (type => 'in button:button');  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'button'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         $reconstruct_active_formatting_elements->($insert_to_current);  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, ['#marker', ''];  
   
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'marquee' or  
                $token->{tag_name} eq 'object') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, ['#marker', ''];  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'xmp') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
         $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);  
         return;  
       } elsif ($token->{tag_name} eq 'table') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         $self->{insertion_mode} = 'in table';  
             
         !!!next-token;  
         return;  
       } elsif ({  
                 area => 1, basefont => 1, bgsound => 1, br => 1,  
                 embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,  
                 image => 1,  
                }->{$token->{tag_name}}) {  
         if ($token->{tag_name} eq 'image') {  
           !!!parse-error (type => 'image');  
           $token->{tag_name} = 'img';  
         }  
   
         ## NOTE: There is an "as if <br>" code clone.  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         pop @{$self->{open_elements}};  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'hr') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         pop @{$self->{open_elements}};  
             
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'input') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         ## TODO: associate with $self->{form_element} if defined  
         pop @{$self->{open_elements}};  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'isindex') {  
         !!!parse-error (type => 'isindex');  
           
         if (defined $self->{form_element}) {  
           ## Ignore the token  
           !!!next-token;  
           return;  
         } else {  
           my $at = $token->{attributes};  
           my $form_attrs;  
           $form_attrs->{action} = $at->{action} if $at->{action};  
           my $prompt_attr = $at->{prompt};  
           $at->{name} = {name => 'name', value => 'isindex'};  
           delete $at->{action};  
           delete $at->{prompt};  
           my @tokens = (  
                         {type => 'start tag', tag_name => 'form',  
                          attributes => $form_attrs},  
                         {type => 'start tag', tag_name => 'hr'},  
                         {type => 'start tag', tag_name => 'p'},  
                         {type => 'start tag', tag_name => 'label'},  
                        );  
           if ($prompt_attr) {  
             push @tokens, {type => 'character', data => $prompt_attr->{value}};  
           } else {  
             push @tokens, {type => 'character',  
                            data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD  
             ## TODO: make this configurable  
           }  
           push @tokens,  
                         {type => 'start tag', tag_name => 'input', attributes => $at},  
                         #{type => 'character', data => ''}, # SHOULD  
                         {type => 'end tag', tag_name => 'label'},  
                         {type => 'end tag', tag_name => 'p'},  
                         {type => 'start tag', tag_name => 'hr'},  
                         {type => 'end tag', tag_name => 'form'};  
           $token = shift @tokens;  
           !!!back-token (@tokens);  
           return;  
         }  
       } elsif ($token->{tag_name} eq 'textarea') {  
         my $tag_name = $token->{tag_name};  
         my $el;  
         !!!create-element ($el, $token->{tag_name}, $token->{attributes});  
           
         ## TODO: $self->{form_element} if defined  
         $self->{content_model} = RCDATA_CONTENT_MODEL;  
         delete $self->{escape}; # MUST  
           
         $insert->($el);  
           
         my $text = '';  
         !!!next-token;  
         if ($token->{type} eq 'character') {  
           $token->{data} =~ s/^\x0A//;  
           unless (length $token->{data}) {  
             !!!next-token;  
           }  
         }  
         while ($token->{type} eq 'character') {  
           $text .= $token->{data};  
           !!!next-token;  
         }  
         if (length $text) {  
           $el->manakai_append_text ($text);  
         }  
           
         $self->{content_model} = PCDATA_CONTENT_MODEL;  
           
         if ($token->{type} eq 'end tag' and  
             $token->{tag_name} eq $tag_name) {  
           ## Ignore the token  
         } else {  
           !!!parse-error (type => 'in RCDATA:#'.$token->{type});  
         }  
         !!!next-token;  
         return;  
       } elsif ({  
                 iframe => 1,  
                 noembed => 1,  
                 noframes => 1,  
                 noscript => 0, ## TODO: 1 if scripting is enabled  
                }->{$token->{tag_name}}) {  
         ## NOTE: There are two "as if in body" code clones.  
         $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);  
         return;  
       } elsif ($token->{tag_name} eq 'select') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
           
         $self->{insertion_mode} = 'in select';  
         !!!next-token;  
         return;  
       } elsif ({  
                 caption => 1, col => 1, colgroup => 1, frame => 1,  
                 frameset => 1, head => 1, option => 1, optgroup => 1,  
                 tbody => 1, td => 1, tfoot => 1, th => 1,  
                 thead => 1, tr => 1,  
                }->{$token->{tag_name}}) {  
         !!!parse-error (type => 'in body:'.$token->{tag_name});  
         ## Ignore the token  
         !!!next-token;  
         return;  
           
         ## ISSUE: An issue on HTML5 new elements in the spec.  
       } else {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
           
         !!!next-token;  
         return;  
       }  
     } elsif ($token->{type} eq 'end tag') {  
       if ($token->{tag_name} eq 'body') {  
         if (@{$self->{open_elements}} > 1 and  
             $self->{open_elements}->[1]->[1] eq 'body') {  
           for (@{$self->{open_elements}}) {  
             unless ({  
                        dd => 1, dt => 1, li => 1, p => 1, td => 1,  
                        th => 1, tr => 1, body => 1, html => 1,  
                      tbody => 1, tfoot => 1, thead => 1,  
                     }->{$_->[1]}) {  
               !!!parse-error (type => 'not closed:'.$_->[1]);  
             }  
           }  
   
           $self->{insertion_mode} = 'after body';  
           !!!next-token;  
           return;  
         } else {  
           !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
           ## Ignore the token  
           !!!next-token;  
           return;  
         }  
       } elsif ($token->{tag_name} eq 'html') {  
         if (@{$self->{open_elements}} > 1 and $self->{open_elements}->[1]->[1] eq 'body') {  
           ## ISSUE: There is an issue in the spec.  
           if ($self->{open_elements}->[-1]->[1] ne 'body') {  
             !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);  
           }  
           $self->{insertion_mode} = 'after body';  
           ## reprocess  
           return;  
         } else {  
           !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
           ## Ignore the token  
           !!!next-token;  
           return;  
         }  
       } elsif ({  
                 address => 1, blockquote => 1, center => 1, dir => 1,  
                 div => 1, dl => 1, fieldset => 1, listing => 1,  
                 menu => 1, ol => 1, pre => 1, ul => 1,  
                 p => 1,  
                 dd => 1, dt => 1, li => 1,  
                 button => 1, marquee => 1, object => 1,  
                }->{$token->{tag_name}}) {  
         ## has an element in scope  
         my $i;  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq $token->{tag_name}) {  
             ## generate implied end tags  
             if ({  
                  dd => ($token->{tag_name} ne 'dd'),  
                  dt => ($token->{tag_name} ne 'dt'),  
                  li => ($token->{tag_name} ne 'li'),  
                  p => ($token->{tag_name} ne 'p'),  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot=> 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => 'end tag',  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               return;  
             }  
             $i = $_;  
             last INSCOPE unless $token->{tag_name} eq 'p';  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
           
         if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {  
           if (defined $i) {  
             !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
           } else {  
             !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
           }  
         }  
           
         if (defined $i) {  
           splice @{$self->{open_elements}}, $i;  
         } elsif ($token->{tag_name} eq 'p') {  
           ## As if <p>, then reprocess the current token  
           my $el;  
           !!!create-element ($el, 'p');  
           $insert->($el);  
         }  
         $clear_up_to_marker->()  
           if {  
             button => 1, marquee => 1, object => 1,  
           }->{$token->{tag_name}};  
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'form') {  
         ## has an element in scope  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq $token->{tag_name}) {  
             ## generate implied end tags  
             if ({  
                  dd => 1, dt => 1, li => 1, p => 1,  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot=> 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => 'end tag',  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               return;  
             }  
             last INSCOPE;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
           
         if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {  
           pop @{$self->{open_elements}};  
         } else {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         }  
   
         undef $self->{form_element};  
         !!!next-token;  
         return;  
       } elsif ({  
                 h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
                }->{$token->{tag_name}}) {  
         ## has an element in scope  
         my $i;  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ({  
                h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
               }->{$node->[1]}) {  
             ## generate implied end tags  
             if ({  
                  dd => 1, dt => 1, li => 1, p => 1,  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot=> 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => 'end tag',  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               return;  
             }  
             $i = $_;  
             last INSCOPE;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
           
         if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         }  
           
         splice @{$self->{open_elements}}, $i if defined $i;  
         !!!next-token;  
         return;  
       } elsif ({  
                 a => 1,  
                 b => 1, big => 1, em => 1, font => 1, i => 1,  
                 nobr => 1, s => 1, small => 1, strile => 1,  
                 strong => 1, tt => 1, u => 1,  
                }->{$token->{tag_name}}) {  
         $formatting_end_tag->($token->{tag_name});  
         return;  
       } elsif ($token->{tag_name} eq 'br') {  
         !!!parse-error (type => 'unmatched end tag:br');  
   
         ## As if <br>  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         my $el;  
         !!!create-element ($el, 'br');  
         $insert->($el);  
           
         ## Ignore the token.  
         !!!next-token;  
         return;  
       } elsif ({  
                 caption => 1, col => 1, colgroup => 1, frame => 1,  
                 frameset => 1, head => 1, option => 1, optgroup => 1,  
                 tbody => 1, td => 1, tfoot => 1, th => 1,  
                 thead => 1, tr => 1,  
                 area => 1, basefont => 1, bgsound => 1,  
                 embed => 1, hr => 1, iframe => 1, image => 1,  
                 img => 1, input => 1, isindex => 1, noembed => 1,  
                 noframes => 1, param => 1, select => 1, spacer => 1,  
                 table => 1, textarea => 1, wbr => 1,  
                 noscript => 0, ## TODO: if scripting is enabled  
                }->{$token->{tag_name}}) {  
         !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
         ## Ignore the token  
         !!!next-token;  
         return;  
           
         ## ISSUE: Issue on HTML5 new elements in spec  
           
       } else {  
         ## Step 1  
         my $node_i = -1;  
         my $node = $self->{open_elements}->[$node_i];  
   
         ## Step 2  
         S2: {  
           if ($node->[1] eq $token->{tag_name}) {  
             ## Step 1  
             ## generate implied end tags  
             if ({  
                  dd => 1, dt => 1, li => 1, p => 1,  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot=> 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => 'end tag',  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               return;  
             }  
           
             ## Step 2  
             if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {  
               !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
             }  
               
             ## Step 3  
             splice @{$self->{open_elements}}, $node_i;  
   
             !!!next-token;  
             last S2;  
           } else {  
             ## Step 3  
             if (not $formatting_category->{$node->[1]} and  
                 #not $phrasing_category->{$node->[1]} and  
                 ($special_category->{$node->[1]} or  
                  $scoping_category->{$node->[1]})) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
               !!!next-token;  
               last S2;  
             }  
           }  
             
           ## Step 4  
           $node_i--;  
           $node = $self->{open_elements}->[$node_i];  
             
           ## Step 5;  
           redo S2;  
         } # S2  
         return;  
       }  
     }  
   }; # $in_body  
2677    
2678    B: {    B: {
2679      if ($token->{type} eq 'DOCTYPE') {      if ($token->{type} == DOCTYPE_TOKEN) {
2680        !!!parse-error (type => 'DOCTYPE in the middle');        !!!parse-error (type => 'DOCTYPE in the middle');
2681        ## Ignore the token        ## Ignore the token
2682        ## Stay in the phase        ## Stay in the phase
2683        !!!next-token;        !!!next-token;
2684        redo B;        redo B;
2685      } elsif ($token->{type} eq 'end-of-file') {      } elsif ($token->{type} == END_OF_FILE_TOKEN) {
2686        if ($token->{insertion_mode} ne 'trailing end') {        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
2687            #
2688          } else {
2689          ## Generate implied end tags          ## Generate implied end tags
2690          if ({          if ({
2691               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,
2692               tbody => 1, tfoot=> 1, thead => 1,               tbody => 1, tfoot=> 1, thead => 1,
2693              }->{$self->{open_elements}->[-1]->[1]}) {              }->{$self->{open_elements}->[-1]->[1]}) {
2694            !!!back-token;            !!!back-token;
2695            $token = {type => 'end tag', tag_name => $self->{open_elements}->[-1]->[1]};            $token = {type => END_TAG_TOKEN, tag_name => $self->{open_elements}->[-1]->[1]};
2696            redo B;            redo B;
2697          }          }
2698                    
# Line 3375  sub _tree_construction_main ($) { Line 2710  sub _tree_construction_main ($) {
2710    
2711        ## Stop parsing        ## Stop parsing
2712        last B;        last B;
2713      } elsif ($token->{type} eq 'start tag' and      } elsif ($token->{type} == START_TAG_TOKEN and
2714               $token->{tag_name} eq 'html') {               $token->{tag_name} eq 'html') {
2715        if ($self->{insertion_mode} eq 'trailing end') {        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
2716            ## Turn into the main phase
2717            !!!parse-error (type => 'after html:html');
2718            $self->{insertion_mode} = AFTER_BODY_IM;
2719          } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
2720          ## Turn into the main phase          ## Turn into the main phase
2721          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html:html');
2722          $self->{insertion_mode} = $previous_insertion_mode;          $self->{insertion_mode} = AFTER_FRAMESET_IM;
2723        }        }
2724    
2725  ## ISSUE: "aa<html>" is not a parse error.  ## ISSUE: "aa<html>" is not a parse error.
# Line 3398  sub _tree_construction_main ($) { Line 2737  sub _tree_construction_main ($) {
2737        }        }
2738        !!!next-token;        !!!next-token;
2739        redo B;        redo B;
2740      } elsif ($token->{type} eq 'comment') {      } elsif ($token->{type} == COMMENT_TOKEN) {
2741        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
2742        if ($self->{insertion_mode} eq 'trailing end') {        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
2743          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
2744        } elsif ($self->{insertion_mode} eq 'after body') {        } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {
2745          $self->{open_elements}->[0]->[0]->append_child ($comment);          $self->{open_elements}->[0]->[0]->append_child ($comment);
2746        } else {        } else {
2747          $self->{open_elements}->[-1]->[0]->append_child ($comment);          $self->{open_elements}->[-1]->[0]->append_child ($comment);
2748        }        }
2749        !!!next-token;        !!!next-token;
2750        redo B;        redo B;
2751      } elsif ($self->{insertion_mode} eq 'before head') {      } elsif ($self->{insertion_mode} & HEAD_IMS) {
2752            if ($token->{type} eq 'character') {        if ($token->{type} == CHARACTER_TOKEN) {
2753              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
2754                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
2755                unless (length $token->{data}) {            unless (length $token->{data}) {
2756                  !!!next-token;              !!!next-token;
2757                  redo B;              redo B;
2758                }            }
2759              }          }
2760              ## As if <head>  
2761              !!!create-element ($self->{head_element}, 'head');          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2762              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});            ## As if <head>
2763              push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            !!!create-element ($self->{head_element}, 'head');
2764              $self->{insertion_mode} = 'in head';            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2765              push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2766    
2767              ## Reprocess in the "in head" insertion mode...
2768              pop @{$self->{open_elements}};
2769    
2770              ## Reprocess in the "after head" insertion mode...
2771            } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2772              ## As if </noscript>
2773              pop @{$self->{open_elements}};
2774              !!!parse-error (type => 'in noscript:#character');
2775              
2776              ## Reprocess in the "in head" insertion mode...
2777              ## As if </head>
2778              pop @{$self->{open_elements}};
2779    
2780              ## Reprocess in the "after head" insertion mode...
2781            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2782              pop @{$self->{open_elements}};
2783    
2784              ## Reprocess in the "after head" insertion mode...
2785            }
2786    
2787                ## "after head" insertion mode
2788                ## As if <body>
2789                !!!insert-element ('body');
2790                $self->{insertion_mode} = IN_BODY_IM;
2791              ## reprocess              ## reprocess
2792              redo B;              redo B;
2793            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} == START_TAG_TOKEN) {
             my $attr = $token->{tag_name} eq 'head' ? $token->{attributes} : {};  
             !!!create-element ($self->{head_element}, 'head', $attr);  
             $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});  
             push @{$self->{open_elements}}, [$self->{head_element}, 'head'];  
             $self->{insertion_mode} = 'in head';  
2794              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
2795                !!!next-token;                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2796              } else {                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});
2797                ## reprocess                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2798              }                  push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];
2799              redo B;                  $self->{insertion_mode} = IN_HEAD_IM;
2800            } elsif ($token->{type} eq 'end tag') {                  !!!next-token;
2801              if ({                  redo B;
2802                   head => 1, body => 1, html => 1,                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
2803                   p => 1, br => 1,                  #
2804                  }->{$token->{tag_name}}) {                } else {
2805                    !!!parse-error (type => 'in head:head'); # or in head noscript
2806                    ## Ignore the token
2807                    !!!next-token;
2808                    redo B;
2809                  }
2810                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2811                ## As if <head>                ## As if <head>
2812                !!!create-element ($self->{head_element}, 'head');                !!!create-element ($self->{head_element}, 'head');
2813                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2814                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
               $self->{insertion_mode} = 'in head';  
               ## reprocess  
               redo B;  
             } else {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token ## ISSUE: An issue in the spec.  
               !!!next-token;  
               redo B;  
             }  
           } else {  
             die "$0: $token->{type}: Unknown type";  
           }  
         } elsif ($self->{insertion_mode} eq 'in head' or  
                  $self->{insertion_mode} eq 'in head noscript' or  
                  $self->{insertion_mode} eq 'after head') {  
           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;  
               }  
             }  
               
             if ($self->{insertion_mode} eq 'in head noscript') {  
               ## As if </noscript>  
               pop @{$self->{open_elements}};  
               !!!parse-error (type => 'in noscript:#character');  
                 
               ## Reprocess in the "in head" insertion mode...  
               ## As if </head>  
               pop @{$self->{open_elements}};  
2815    
2816                ## Reprocess in the "after head" insertion mode...                $self->{insertion_mode} = IN_HEAD_IM;
2817              } elsif ($self->{insertion_mode} eq 'in head') {                ## Reprocess in the "in head" insertion mode...
               pop @{$self->{open_elements}};  
   
               ## Reprocess in the "after head" insertion mode...  
2818              }              }
2819    
             ## 'after head' insertion mode  
             ## As if <body>  
             !!!insert-element ('body');  
             $self->{insertion_mode} = 'in body';  
             ## reprocess  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
2820              if ($token->{tag_name} eq 'base') {              if ($token->{tag_name} eq 'base') {
2821                if ($self->{insertion_mode} eq 'in head noscript') {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2822                  ## As if </noscript>                  ## As if </noscript>
2823                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
2824                  !!!parse-error (type => 'in noscript:base');                  !!!parse-error (type => 'in noscript:base');
2825                                
2826                  $self->{insertion_mode} = 'in head';                  $self->{insertion_mode} = IN_HEAD_IM;
2827                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
2828                }                }
2829    
2830                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
2831                if ($self->{insertion_mode} eq 'after head') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2832                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
2833                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2834                }                }
2835                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
2836                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
2837                pop @{$self->{open_elements}}                pop @{$self->{open_elements}}
2838                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
2839                !!!next-token;                !!!next-token;
2840                redo B;                redo B;
2841              } elsif ($token->{tag_name} eq 'link') {              } elsif ($token->{tag_name} eq 'link') {
2842                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
2843                if ($self->{insertion_mode} eq 'after head') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2844                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
2845                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2846                }                }
2847                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
2848                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
2849                pop @{$self->{open_elements}}                pop @{$self->{open_elements}}
2850                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
2851                !!!next-token;                !!!next-token;
2852                redo B;                redo B;
2853              } elsif ($token->{tag_name} eq 'meta') {              } elsif ($token->{tag_name} eq 'meta') {
2854                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
2855                if ($self->{insertion_mode} eq 'after head') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2856                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
2857                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2858                }                }
# Line 3536  sub _tree_construction_main ($) { Line 2860  sub _tree_construction_main ($) {
2860                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
2861    
2862                unless ($self->{confident}) {                unless ($self->{confident}) {
                 my $charset;  
2863                  if ($token->{attributes}->{charset}) { ## TODO: And if supported                  if ($token->{attributes}->{charset}) { ## TODO: And if supported
2864                    $charset = $token->{attributes}->{charset}->{value};                    $self->{change_encoding}
2865                  }                        ->($self, $token->{attributes}->{charset}->{value});
2866                  if ($token->{attributes}->{'http-equiv'}) {                  } elsif ($token->{attributes}->{content}) {
2867                    ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.                    ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.
2868                    if ($token->{attributes}->{'http-equiv'}->{value}                    if ($token->{attributes}->{content}->{value}
2869                        =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=                        =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=
2870                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
2871                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
2872                      $charset = defined $1 ? $1 : defined $2 ? $2 : $3;                      $self->{change_encoding}
2873                    } ## TODO: And if supported                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);
2874                      }
2875                  }                  }
                 ## TODO: Change the encoding  
2876                }                }
2877    
               ## TODO: Extracting |charset| from |meta|.  
2878                pop @{$self->{open_elements}}                pop @{$self->{open_elements}}
2879                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
2880                !!!next-token;                !!!next-token;
2881                redo B;                redo B;
2882              } elsif ($token->{tag_name} eq 'title') {              } elsif ($token->{tag_name} eq 'title') {
2883                if ($self->{insertion_mode} eq 'in head noscript') {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2884                  ## As if </noscript>                  ## As if </noscript>
2885                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
2886                  !!!parse-error (type => 'in noscript:title');                  !!!parse-error (type => 'in noscript:title');
2887                                
2888                  $self->{insertion_mode} = 'in head';                  $self->{insertion_mode} = IN_HEAD_IM;
2889                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
2890                } elsif ($self->{insertion_mode} eq 'after head') {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
2891                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
2892                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2893                }                }
# Line 3576  sub _tree_construction_main ($) { Line 2898  sub _tree_construction_main ($) {
2898                $parse_rcdata->(RCDATA_CONTENT_MODEL,                $parse_rcdata->(RCDATA_CONTENT_MODEL,
2899                                sub { $parent->append_child ($_[0]) });                                sub { $parent->append_child ($_[0]) });
2900                pop @{$self->{open_elements}}                pop @{$self->{open_elements}}
2901                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
2902                redo B;                redo B;
2903              } elsif ($token->{tag_name} eq 'style') {              } elsif ($token->{tag_name} eq 'style') {
2904                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
2905                ## insertion mode 'in head')                ## insertion mode IN_HEAD_IM)
2906                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
2907                if ($self->{insertion_mode} eq 'after head') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2908                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
2909                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2910                }                }
2911                $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);                $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);
2912                pop @{$self->{open_elements}}                pop @{$self->{open_elements}}
2913                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
2914                redo B;                redo B;
2915              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
2916                if ($self->{insertion_mode} eq 'in head') {                if ($self->{insertion_mode} == IN_HEAD_IM) {
2917                  ## NOTE: and scripting is disalbed                  ## NOTE: and scripting is disalbed
2918                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes});
2919                  $self->{insertion_mode} = 'in head noscript';                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
2920                  !!!next-token;                  !!!next-token;
2921                  redo B;                  redo B;
2922                } elsif ($self->{insertion_mode} eq 'in head noscript') {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2923                  !!!parse-error (type => 'in noscript:noscript');                  !!!parse-error (type => 'in noscript:noscript');
2924                  ## Ignore the token                  ## Ignore the token
2925                  !!!next-token;                  !!!next-token;
# Line 3605  sub _tree_construction_main ($) { Line 2927  sub _tree_construction_main ($) {
2927                } else {                } else {
2928                  #                  #
2929                }                }
             } elsif ($token->{tag_name} eq 'head') {  
               if ($self->{insertion_mode} ne 'after head') {  
                 !!!parse-error (type => 'in head:head'); # or in head noscript  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               } else {  
                 #  
               }  
2930              } elsif ($token->{tag_name} eq 'script') {              } elsif ($token->{tag_name} eq 'script') {
2931                if ($self->{insertion_mode} eq 'in head noscript') {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2932                  ## As if </noscript>                  ## As if </noscript>
2933                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
2934                  !!!parse-error (type => 'in noscript:script');                  !!!parse-error (type => 'in noscript:script');
2935                                
2936                  $self->{insertion_mode} = 'in head';                  $self->{insertion_mode} = IN_HEAD_IM;
2937                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
2938                } elsif ($self->{insertion_mode} eq 'after head') {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
2939                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
2940                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2941                }                }
# Line 3630  sub _tree_construction_main ($) { Line 2943  sub _tree_construction_main ($) {
2943                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
2944                $script_start_tag->($insert_to_current);                $script_start_tag->($insert_to_current);
2945                pop @{$self->{open_elements}}                pop @{$self->{open_elements}}
2946                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
2947                redo B;                redo B;
2948              } elsif ($token->{tag_name} eq 'body' or              } elsif ($token->{tag_name} eq 'body' or
2949                       $token->{tag_name} eq 'frameset') {                       $token->{tag_name} eq 'frameset') {
2950                if ($self->{insertion_mode} eq 'in head noscript') {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2951                  ## As if </noscript>                  ## As if </noscript>
2952                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
2953                  !!!parse-error (type => 'in noscript:'.$token->{tag_name});                  !!!parse-error (type => 'in noscript:'.$token->{tag_name});
# Line 3644  sub _tree_construction_main ($) { Line 2957  sub _tree_construction_main ($) {
2957                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
2958                                    
2959                  ## Reprocess in the "after head" insertion mode...                  ## Reprocess in the "after head" insertion mode...
2960                } elsif ($self->{insertion_mode} eq 'in head') {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2961                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
2962                                    
2963                  ## Reprocess in the "after head" insertion mode...                  ## Reprocess in the "after head" insertion mode...
# Line 3652  sub _tree_construction_main ($) { Line 2965  sub _tree_construction_main ($) {
2965    
2966                ## "after head" insertion mode                ## "after head" insertion mode
2967                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
2968                $self->{insertion_mode} = 'in '.$token->{tag_name};                if ($token->{tag_name} eq 'body') {
2969                    $self->{insertion_mode} = IN_BODY_IM;
2970                  } elsif ($token->{tag_name} eq 'frameset') {
2971                    $self->{insertion_mode} = IN_FRAMESET_IM;
2972                  } else {
2973                    die "$0: tag name: $self->{tag_name}";
2974                  }
2975                !!!next-token;                !!!next-token;
2976                redo B;                redo B;
2977              } else {              } else {
2978                #                #
2979              }              }
2980    
2981              if ($self->{insertion_mode} eq 'in head noscript') {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2982                ## As if </noscript>                ## As if </noscript>
2983                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
2984                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});
# Line 3669  sub _tree_construction_main ($) { Line 2988  sub _tree_construction_main ($) {
2988                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
2989    
2990                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
2991              } elsif ($self->{insertion_mode} eq 'in head') {              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2992                ## As if </head>                ## As if </head>
2993                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
2994    
# Line 3679  sub _tree_construction_main ($) { Line 2998  sub _tree_construction_main ($) {
2998              ## "after head" insertion mode              ## "after head" insertion mode
2999              ## As if <body>              ## As if <body>
3000              !!!insert-element ('body');              !!!insert-element ('body');
3001              $self->{insertion_mode} = 'in body';              $self->{insertion_mode} = IN_BODY_IM;
3002              ## reprocess              ## reprocess
3003              redo B;              redo B;
3004            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
3005              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
3006                if ($self->{insertion_mode} eq 'in head noscript') {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3007                    ## As if <head>
3008                    !!!create-element ($self->{head_element}, 'head');
3009                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3010                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3011    
3012                    ## Reprocess in the "in head" insertion mode...
3013                    pop @{$self->{open_elements}};
3014                    $self->{insertion_mode} = AFTER_HEAD_IM;
3015                    !!!next-token;
3016                    redo B;
3017                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3018                  ## As if </noscript>                  ## As if </noscript>
3019                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3020                  !!!parse-error (type => 'in noscript:script');                  !!!parse-error (type => 'in noscript:script');
3021                                    
                 $self->{insertion_mode} = 'in head';  
3022                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
               }  
                 
               if ($self->{insertion_mode} eq 'in head') {  
3023                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3024                  $self->{insertion_mode} = 'after head';                  $self->{insertion_mode} = AFTER_HEAD_IM;
3025                    !!!next-token;
3026                    redo B;
3027                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3028                    pop @{$self->{open_elements}};
3029                    $self->{insertion_mode} = AFTER_HEAD_IM;
3030                  !!!next-token;                  !!!next-token;
3031                  redo B;                  redo B;
3032                } else {                } else {
3033                  #                  #
3034                }                }
3035              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
3036                if ($self->{insertion_mode} eq 'in head noscript') {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3037                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3038                  $self->{insertion_mode} = 'in head';                  $self->{insertion_mode} = IN_HEAD_IM;
3039                    !!!next-token;
3040                    redo B;
3041                  } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3042                    !!!parse-error (type => 'unmatched end tag:noscript');
3043                    ## Ignore the token ## ISSUE: An issue in the spec.
3044                  !!!next-token;                  !!!next-token;
3045                  redo B;                  redo B;
3046                } else {                } else {
# Line 3713  sub _tree_construction_main ($) { Line 3049  sub _tree_construction_main ($) {
3049              } elsif ({              } elsif ({
3050                        body => 1, html => 1,                        body => 1, html => 1,
3051                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3052                if ($self->{insertion_mode} eq 'in head noscript') {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3053                    ## As if <head>
3054                    !!!create-element ($self->{head_element}, 'head');
3055                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3056                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3057    
3058                    $self->{insertion_mode} = IN_HEAD_IM;
3059                    ## Reprocess in the "in head" insertion mode...
3060                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3061                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3062                  ## Ignore the token                  ## Ignore the token
3063                  !!!next-token;                  !!!next-token;
3064                  redo B;                  redo B;
               } else { # 'in head', 'after head'  
                 #  
3065                }                }
3066                  
3067                  #
3068              } elsif ({              } elsif ({
3069                        p => 1, br => 1,                        p => 1, br => 1,
3070                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3071                  if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3072                    ## As if <head>
3073                    !!!create-element ($self->{head_element}, 'head');
3074                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3075                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3076    
3077                    $self->{insertion_mode} = IN_HEAD_IM;
3078                    ## Reprocess in the "in head" insertion mode...
3079                  }
3080    
3081                #                #
3082              } else {              } else {
3083                if ($self->{insertion_mode} ne 'after head') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3084                    #
3085                  } else {
3086                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3087                  ## Ignore the token                  ## Ignore the token
3088                  !!!next-token;                  !!!next-token;
3089                  redo B;                  redo B;
               } else {  
                 #  
3090                }                }
3091              }              }
3092    
3093              if ($self->{insertion_mode} eq 'in head noscript') {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3094                ## As if </noscript>                ## As if </noscript>
3095                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3096                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});
# Line 3746  sub _tree_construction_main ($) { Line 3100  sub _tree_construction_main ($) {
3100                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3101    
3102                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
3103              } elsif ($self->{insertion_mode} eq 'in head') {              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3104                ## As if </head>                ## As if </head>
3105                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3106    
3107                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
3108                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3109                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3110                  ## Ignore the token ## ISSUE: An issue in the spec.
3111                  !!!next-token;
3112                  redo B;
3113              }              }
3114    
3115              ## "after head" insertion mode              ## "after head" insertion mode
3116              ## As if <body>              ## As if <body>
3117              !!!insert-element ('body');              !!!insert-element ('body');
3118              $self->{insertion_mode} = 'in body';              $self->{insertion_mode} = IN_BODY_IM;
3119              ## reprocess              ## reprocess
3120              redo B;              redo B;
3121            } else {            } else {
# Line 3764  sub _tree_construction_main ($) { Line 3123  sub _tree_construction_main ($) {
3123            }            }
3124    
3125            ## ISSUE: An issue in the spec.            ## ISSUE: An issue in the spec.
3126          } elsif ($self->{insertion_mode} eq 'in body' or      } elsif ($self->{insertion_mode} & BODY_IMS) {
3127                   $self->{insertion_mode} eq 'in cell' or            if ($token->{type} == CHARACTER_TOKEN) {
                  $self->{insertion_mode} eq 'in caption') {  
           if ($token->{type} eq 'character') {  
3128              ## NOTE: There is a code clone of "character in body".              ## NOTE: There is a code clone of "character in body".
3129              $reconstruct_active_formatting_elements->($insert_to_current);              $reconstruct_active_formatting_elements->($insert_to_current);
3130                            
# Line 3775  sub _tree_construction_main ($) { Line 3132  sub _tree_construction_main ($) {
3132    
3133              !!!next-token;              !!!next-token;
3134              redo B;              redo B;
3135            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} == START_TAG_TOKEN) {
3136              if ({              if ({
3137                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
3138                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,
3139                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
3140                if ($self->{insertion_mode} eq 'in cell') {                if ($self->{insertion_mode} == IN_CELL_IM) {
3141                  ## have an element in table scope                  ## have an element in table scope
3142                  my $tn;                  my $tn;
3143                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 3803  sub _tree_construction_main ($) { Line 3160  sub _tree_construction_main ($) {
3160                                    
3161                  ## Close the cell                  ## Close the cell
3162                  !!!back-token; # <?>                  !!!back-token; # <?>
3163                  $token = {type => 'end tag', tag_name => $tn};                  $token = {type => END_TAG_TOKEN, tag_name => $tn};
3164                  redo B;                  redo B;
3165                } elsif ($self->{insertion_mode} eq 'in caption') {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
3166                  !!!parse-error (type => 'not closed:caption');                  !!!parse-error (type => 'not closed:caption');
3167                                    
3168                  ## As if </caption>                  ## As if </caption>
# Line 3836  sub _tree_construction_main ($) { Line 3193  sub _tree_construction_main ($) {
3193                       tbody => 1, tfoot=> 1, thead => 1,                       tbody => 1, tfoot=> 1, thead => 1,
3194                      }->{$self->{open_elements}->[-1]->[1]}) {                      }->{$self->{open_elements}->[-1]->[1]}) {
3195                    !!!back-token; # <?>                    !!!back-token; # <?>
3196                    $token = {type => 'end tag', tag_name => 'caption'};                    $token = {type => END_TAG_TOKEN, tag_name => 'caption'};
3197                    !!!back-token;                    !!!back-token;
3198                    $token = {type => 'end tag',                    $token = {type => END_TAG_TOKEN,
3199                              tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                              tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3200                    redo B;                    redo B;
3201                  }                  }
# Line 3851  sub _tree_construction_main ($) { Line 3208  sub _tree_construction_main ($) {
3208                                    
3209                  $clear_up_to_marker->();                  $clear_up_to_marker->();
3210                                    
3211                  $self->{insertion_mode} = 'in table';                  $self->{insertion_mode} = IN_TABLE_IM;
3212                                    
3213                  ## reprocess                  ## reprocess
3214                  redo B;                  redo B;
# Line 3861  sub _tree_construction_main ($) { Line 3218  sub _tree_construction_main ($) {
3218              } else {              } else {
3219                #                #
3220              }              }
3221            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
3222              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {
3223                if ($self->{insertion_mode} eq 'in cell') {                if ($self->{insertion_mode} == IN_CELL_IM) {
3224                  ## have an element in table scope                  ## have an element in table scope
3225                  my $i;                  my $i;
3226                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 3893  sub _tree_construction_main ($) { Line 3250  sub _tree_construction_main ($) {
3250                       tbody => 1, tfoot=> 1, thead => 1,                       tbody => 1, tfoot=> 1, thead => 1,
3251                      }->{$self->{open_elements}->[-1]->[1]}) {                      }->{$self->{open_elements}->[-1]->[1]}) {
3252                    !!!back-token;                    !!!back-token;
3253                    $token = {type => 'end tag',                    $token = {type => END_TAG_TOKEN,
3254                              tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                              tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3255                    redo B;                    redo B;
3256                  }                  }
# Line 3906  sub _tree_construction_main ($) { Line 3263  sub _tree_construction_main ($) {
3263                                    
3264                  $clear_up_to_marker->();                  $clear_up_to_marker->();
3265                                    
3266                  $self->{insertion_mode} = 'in row';                  $self->{insertion_mode} = IN_ROW_IM;
3267                                    
3268                  !!!next-token;                  !!!next-token;
3269                  redo B;                  redo B;
3270                } elsif ($self->{insertion_mode} eq 'in caption') {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
3271                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3272                  ## Ignore the token                  ## Ignore the token
3273                  !!!next-token;                  !!!next-token;
# Line 3919  sub _tree_construction_main ($) { Line 3276  sub _tree_construction_main ($) {
3276                  #                  #
3277                }                }
3278              } elsif ($token->{tag_name} eq 'caption') {              } elsif ($token->{tag_name} eq 'caption') {
3279                if ($self->{insertion_mode} eq 'in caption') {                if ($self->{insertion_mode} == IN_CAPTION_IM) {
3280                  ## have a table element in table scope                  ## have a table element in table scope
3281                  my $i;                  my $i;
3282                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 3947  sub _tree_construction_main ($) { Line 3304  sub _tree_construction_main ($) {
3304                       tbody => 1, tfoot=> 1, thead => 1,                       tbody => 1, tfoot=> 1, thead => 1,
3305                      }->{$self->{open_elements}->[-1]->[1]}) {                      }->{$self->{open_elements}->[-1]->[1]}) {
3306                    !!!back-token;                    !!!back-token;
3307                    $token = {type => 'end tag',                    $token = {type => END_TAG_TOKEN,
3308                              tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                              tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3309                    redo B;                    redo B;
3310                  }                  }
# Line 3960  sub _tree_construction_main ($) { Line 3317  sub _tree_construction_main ($) {
3317                                    
3318                  $clear_up_to_marker->();                  $clear_up_to_marker->();
3319                                    
3320                  $self->{insertion_mode} = 'in table';                  $self->{insertion_mode} = IN_TABLE_IM;
3321                                    
3322                  !!!next-token;                  !!!next-token;
3323                  redo B;                  redo B;
3324                } elsif ($self->{insertion_mode} eq 'in cell') {                } elsif ($self->{insertion_mode} == IN_CELL_IM) {
3325                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3326                  ## Ignore the token                  ## Ignore the token
3327                  !!!next-token;                  !!!next-token;
# Line 3976  sub _tree_construction_main ($) { Line 3333  sub _tree_construction_main ($) {
3333                        table => 1, tbody => 1, tfoot => 1,                        table => 1, tbody => 1, tfoot => 1,
3334                        thead => 1, tr => 1,                        thead => 1, tr => 1,
3335                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
3336                       $self->{insertion_mode} eq 'in cell') {                       $self->{insertion_mode} == IN_CELL_IM) {
3337                ## have an element in table scope                ## have an element in table scope
3338                my $i;                my $i;
3339                my $tn;                my $tn;
# Line 4004  sub _tree_construction_main ($) { Line 3361  sub _tree_construction_main ($) {
3361    
3362                ## Close the cell                ## Close the cell
3363                !!!back-token; # </?>                !!!back-token; # </?>
3364                $token = {type => 'end tag', tag_name => $tn};                $token = {type => END_TAG_TOKEN, tag_name => $tn};
3365                redo B;                redo B;
3366              } elsif ($token->{tag_name} eq 'table' and              } elsif ($token->{tag_name} eq 'table' and
3367                       $self->{insertion_mode} eq 'in caption') {                       $self->{insertion_mode} == IN_CAPTION_IM) {
3368                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed:caption');
3369    
3370                ## As if </caption>                ## As if </caption>
# Line 4038  sub _tree_construction_main ($) { Line 3395  sub _tree_construction_main ($) {
3395                     tbody => 1, tfoot=> 1, thead => 1,                     tbody => 1, tfoot=> 1, thead => 1,
3396                    }->{$self->{open_elements}->[-1]->[1]}) {                    }->{$self->{open_elements}->[-1]->[1]}) {
3397                  !!!back-token; # </table>                  !!!back-token; # </table>
3398                  $token = {type => 'end tag', tag_name => 'caption'};                  $token = {type => END_TAG_TOKEN, tag_name => 'caption'};
3399                  !!!back-token;                  !!!back-token;
3400                  $token = {type => 'end tag',                  $token = {type => END_TAG_TOKEN,
3401                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3402                  redo B;                  redo B;
3403                }                }
# Line 4053  sub _tree_construction_main ($) { Line 3410  sub _tree_construction_main ($) {
3410    
3411                $clear_up_to_marker->();                $clear_up_to_marker->();
3412    
3413                $self->{insertion_mode} = 'in table';                $self->{insertion_mode} = IN_TABLE_IM;
3414    
3415                ## reprocess                ## reprocess
3416                redo B;                redo B;
3417              } elsif ({              } elsif ({
3418                        body => 1, col => 1, colgroup => 1, html => 1,                        body => 1, col => 1, colgroup => 1, html => 1,
3419                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3420                if ($self->{insertion_mode} eq 'in cell' or                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
                   $self->{insertion_mode} eq 'in caption') {  
3421                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3422                  ## Ignore the token                  ## Ignore the token
3423                  !!!next-token;                  !!!next-token;
# Line 4073  sub _tree_construction_main ($) { Line 3429  sub _tree_construction_main ($) {
3429                        tbody => 1, tfoot => 1,                        tbody => 1, tfoot => 1,
3430                        thead => 1, tr => 1,                        thead => 1, tr => 1,
3431                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
3432                       $self->{insertion_mode} eq 'in caption') {                       $self->{insertion_mode} == IN_CAPTION_IM) {
3433                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3434                ## Ignore the token                ## Ignore the token
3435                !!!next-token;                !!!next-token;
# Line 4081  sub _tree_construction_main ($) { Line 3437  sub _tree_construction_main ($) {
3437              } else {              } else {
3438                #                #
3439              }              }
3440            } else {        } else {
3441              #          die "$0: $token->{type}: Unknown token type";
3442            }        }
3443              
3444            $in_body->($insert_to_current);        $insert = $insert_to_current;
3445            redo B;        #
3446          } elsif ($self->{insertion_mode} eq 'in row' or      } elsif ($self->{insertion_mode} & TABLE_IMS) {
3447                   $self->{insertion_mode} eq 'in table body' or        if ($token->{type} == CHARACTER_TOKEN) {
                  $self->{insertion_mode} eq 'in table') {  
           if ($token->{type} eq 'character') {  
             ## NOTE: There are "character in table" code clones.  
3448              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
3449                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
3450                                
# Line 4148  sub _tree_construction_main ($) { Line 3501  sub _tree_construction_main ($) {
3501                            
3502              !!!next-token;              !!!next-token;
3503              redo B;              redo B;
3504            } elsif ($token->{type} eq 'start tag') {        } elsif ($token->{type} == START_TAG_TOKEN) {
3505              if ({              if ({
3506                   tr => ($self->{insertion_mode} ne 'in row'),                   tr => ($self->{insertion_mode} != IN_ROW_IM),
3507                   th => 1, td => 1,                   th => 1, td => 1,
3508                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
3509                if ($self->{insertion_mode} eq 'in table') {                if ($self->{insertion_mode} == IN_TABLE_IM) {
3510                  ## Clear back to table context                  ## Clear back to table context
3511                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while ($self->{open_elements}->[-1]->[1] ne 'table' and
3512                         $self->{open_elements}->[-1]->[1] ne 'html') {                         $self->{open_elements}->[-1]->[1] ne 'html') {
# Line 4162  sub _tree_construction_main ($) { Line 3515  sub _tree_construction_main ($) {
3515                  }                  }
3516                                    
3517                  !!!insert-element ('tbody');                  !!!insert-element ('tbody');
3518                  $self->{insertion_mode} = 'in table body';                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
3519                  ## reprocess in the "in table body" insertion mode...                  ## reprocess in the "in table body" insertion mode...
3520                }                }
3521    
3522                if ($self->{insertion_mode} eq 'in table body') {                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
3523                  unless ($token->{tag_name} eq 'tr') {                  unless ($token->{tag_name} eq 'tr') {
3524                    !!!parse-error (type => 'missing start tag:tr');                    !!!parse-error (type => 'missing start tag:tr');
3525                  }                  }
# Line 4179  sub _tree_construction_main ($) { Line 3532  sub _tree_construction_main ($) {
3532                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
3533                  }                  }
3534                                    
3535                  $self->{insertion_mode} = 'in row';                  $self->{insertion_mode} = IN_ROW_IM;
3536                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
3537                    !!!insert-element ($token->{tag_name}, $token->{attributes});                    !!!insert-element ($token->{tag_name}, $token->{attributes});
3538                    !!!next-token;                    !!!next-token;
# Line 4199  sub _tree_construction_main ($) { Line 3552  sub _tree_construction_main ($) {
3552                }                }
3553                                
3554                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
3555                $self->{insertion_mode} = 'in cell';                $self->{insertion_mode} = IN_CELL_IM;
3556    
3557                push @$active_formatting_elements, ['#marker', ''];                push @$active_formatting_elements, ['#marker', ''];
3558                                
# Line 4208  sub _tree_construction_main ($) { Line 3561  sub _tree_construction_main ($) {
3561              } elsif ({              } elsif ({
3562                        caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
3563                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
3564                        tr => 1, # $self->{insertion_mode} eq 'in row'                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM
3565                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3566                if ($self->{insertion_mode} eq 'in row') {                if ($self->{insertion_mode} == IN_ROW_IM) {
3567                  ## As if </tr>                  ## As if </tr>
3568                  ## have an element in table scope                  ## have an element in table scope
3569                  my $i;                  my $i;
# Line 4241  sub _tree_construction_main ($) { Line 3594  sub _tree_construction_main ($) {
3594                  }                  }
3595                                    
3596                  pop @{$self->{open_elements}}; # tr                  pop @{$self->{open_elements}}; # tr
3597                  $self->{insertion_mode} = 'in table body';                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
3598                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
3599                    ## reprocess                    ## reprocess
3600                    redo B;                    redo B;
# Line 4250  sub _tree_construction_main ($) { Line 3603  sub _tree_construction_main ($) {
3603                  }                  }
3604                }                }
3605    
3606                if ($self->{insertion_mode} eq 'in table body') {                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
3607                  ## have an element in table scope                  ## have an element in table scope
3608                  my $i;                  my $i;
3609                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 4289  sub _tree_construction_main ($) { Line 3642  sub _tree_construction_main ($) {
3642                  ## nop by definition                  ## nop by definition
3643                                    
3644                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3645                  $self->{insertion_mode} = 'in table';                  $self->{insertion_mode} = IN_TABLE_IM;
3646                  ## reprocess in "in table" insertion mode...                  ## reprocess in "in table" insertion mode...
3647                }                }
3648    
# Line 4302  sub _tree_construction_main ($) { Line 3655  sub _tree_construction_main ($) {
3655                  }                  }
3656                                    
3657                  !!!insert-element ('colgroup');                  !!!insert-element ('colgroup');
3658                  $self->{insertion_mode} = 'in column group';                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
3659                  ## reprocess                  ## reprocess
3660                  redo B;                  redo B;
3661                } elsif ({                } elsif ({
# Line 4322  sub _tree_construction_main ($) { Line 3675  sub _tree_construction_main ($) {
3675                                    
3676                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes});
3677                  $self->{insertion_mode} = {                  $self->{insertion_mode} = {
3678                                             caption => 'in caption',                                             caption => IN_CAPTION_IM,
3679                                             colgroup => 'in column group',                                             colgroup => IN_COLUMN_GROUP_IM,
3680                                             tbody => 'in table body',                                             tbody => IN_TABLE_BODY_IM,
3681                                             tfoot => 'in table body',                                             tfoot => IN_TABLE_BODY_IM,
3682                                             thead => 'in table body',                                             thead => IN_TABLE_BODY_IM,
3683                                            }->{$token->{tag_name}};                                            }->{$token->{tag_name}};
3684                  !!!next-token;                  !!!next-token;
3685                  redo B;                  redo B;
# Line 4334  sub _tree_construction_main ($) { Line 3687  sub _tree_construction_main ($) {
3687                  die "$0: in table: <>: $token->{tag_name}";                  die "$0: in table: <>: $token->{tag_name}";
3688                }                }
3689              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
               ## NOTE: There are code clones for this "table in table"  
3690                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3691    
3692                ## As if </table>                ## As if </table>
# Line 4365  sub _tree_construction_main ($) { Line 3717  sub _tree_construction_main ($) {
3717                     tbody => 1, tfoot=> 1, thead => 1,                     tbody => 1, tfoot=> 1, thead => 1,
3718                    }->{$self->{open_elements}->[-1]->[1]}) {                    }->{$self->{open_elements}->[-1]->[1]}) {
3719                  !!!back-token; # <table>                  !!!back-token; # <table>
3720                  $token = {type => 'end tag', tag_name => 'table'};                  $token = {type => END_TAG_TOKEN, tag_name => 'table'};
3721                  !!!back-token;                  !!!back-token;
3722                  $token = {type => 'end tag',                  $token = {type => END_TAG_TOKEN,
3723                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3724                  redo B;                  redo B;
3725                }                }
# Line 4382  sub _tree_construction_main ($) { Line 3734  sub _tree_construction_main ($) {
3734    
3735                ## reprocess                ## reprocess
3736                redo B;                redo B;
3737              } else {          } else {
3738                #            !!!parse-error (type => 'in table:'.$token->{tag_name});
3739              }  
3740            } elsif ($token->{type} eq 'end tag') {            $insert = $insert_to_foster;
3741              #
3742            }
3743          } elsif ($token->{type} == END_TAG_TOKEN) {
3744              if ($token->{tag_name} eq 'tr' and              if ($token->{tag_name} eq 'tr' and
3745                  $self->{insertion_mode} eq 'in row') {                  $self->{insertion_mode} == IN_ROW_IM) {
3746                ## have an element in table scope                ## have an element in table scope
3747                my $i;                my $i;
3748                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 4417  sub _tree_construction_main ($) { Line 3772  sub _tree_construction_main ($) {
3772                }                }
3773    
3774                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}}; # tr
3775                $self->{insertion_mode} = 'in table body';                $self->{insertion_mode} = IN_TABLE_BODY_IM;
3776                !!!next-token;                !!!next-token;
3777                redo B;                redo B;
3778              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
3779                if ($self->{insertion_mode} eq 'in row') {                if ($self->{insertion_mode} == IN_ROW_IM) {
3780                  ## As if </tr>                  ## As if </tr>
3781                  ## have an element in table scope                  ## have an element in table scope
3782                  my $i;                  my $i;
# Line 4452  sub _tree_construction_main ($) { Line 3807  sub _tree_construction_main ($) {
3807                  }                  }
3808                                    
3809                  pop @{$self->{open_elements}}; # tr                  pop @{$self->{open_elements}}; # tr
3810                  $self->{insertion_mode} = 'in table body';                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
3811                  ## reprocess in the "in table body" insertion mode...                  ## reprocess in the "in table body" insertion mode...
3812                }                }
3813    
3814                if ($self->{insertion_mode} eq 'in table body') {                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
3815                  ## have an element in table scope                  ## have an element in table scope
3816                  my $i;                  my $i;
3817                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 4495  sub _tree_construction_main ($) { Line 3850  sub _tree_construction_main ($) {
3850                  ## nop by definition                  ## nop by definition
3851                                    
3852                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3853                  $self->{insertion_mode} = 'in table';                  $self->{insertion_mode} = IN_TABLE_IM;
3854                  ## reprocess in the "in table" insertion mode...                  ## reprocess in the "in table" insertion mode...
3855                }                }
3856    
# Line 4526  sub _tree_construction_main ($) { Line 3881  sub _tree_construction_main ($) {
3881                     tbody => 1, tfoot=> 1, thead => 1,                     tbody => 1, tfoot=> 1, thead => 1,
3882                    }->{$self->{open_elements}->[-1]->[1]}) {                    }->{$self->{open_elements}->[-1]->[1]}) {
3883                  !!!back-token;                  !!!back-token;
3884                  $token = {type => 'end tag',                  $token = {type => END_TAG_TOKEN,
3885                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3886                  redo B;                  redo B;
3887                }                }
# Line 4544  sub _tree_construction_main ($) { Line 3899  sub _tree_construction_main ($) {
3899              } elsif ({              } elsif ({
3900                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
3901                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
3902                       ($self->{insertion_mode} eq 'in row' or                       $self->{insertion_mode} & ROW_IMS) {
3903                        $self->{insertion_mode} eq 'in table body')) {                if ($self->{insertion_mode} == IN_ROW_IM) {
               if ($self->{insertion_mode} eq 'in row') {  
3904                  ## have an element in table scope                  ## have an element in table scope
3905                  my $i;                  my $i;
3906                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 4597  sub _tree_construction_main ($) { Line 3951  sub _tree_construction_main ($) {
3951                  }                  }
3952                                    
3953                  pop @{$self->{open_elements}}; # tr                  pop @{$self->{open_elements}}; # tr
3954                  $self->{insertion_mode} = 'in table body';                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
3955                  ## reprocess in the "in table body" insertion mode...                  ## reprocess in the "in table body" insertion mode...
3956                }                }
3957    
# Line 4630  sub _tree_construction_main ($) { Line 3984  sub _tree_construction_main ($) {
3984                }                }
3985    
3986                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3987                $self->{insertion_mode} = 'in table';                $self->{insertion_mode} = IN_TABLE_IM;
3988                !!!next-token;                !!!next-token;
3989                redo B;                redo B;
3990              } elsif ({              } elsif ({
3991                        body => 1, caption => 1, col => 1, colgroup => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
3992                        html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
3993                        tr => 1, # $self->{insertion_mode} eq 'in row'                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM
3994                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} eq 'in table'                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
3995                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3996                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3997                ## Ignore the token                ## Ignore the token
3998                !!!next-token;                !!!next-token;
3999                redo B;                redo B;
4000              } else {          } else {
4001                #            !!!parse-error (type => 'in table:/'.$token->{tag_name});
             }  
           } else {  
             die "$0: $token->{type}: Unknown token type";  
           }  
4002    
4003            !!!parse-error (type => 'in table:'.$token->{tag_name});            $insert = $insert_to_foster;
4004            $in_body->($insert_to_foster);            #
4005            redo B;          }
4006          } elsif ($self->{insertion_mode} eq 'in column group') {        } else {
4007            if ($token->{type} eq 'character') {          die "$0: $token->{type}: Unknown token type";
4008          }
4009        } elsif ($self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
4010              if ($token->{type} == CHARACTER_TOKEN) {
4011              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4012                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4013                unless (length $token->{data}) {                unless (length $token->{data}) {
# Line 4664  sub _tree_construction_main ($) { Line 4017  sub _tree_construction_main ($) {
4017              }              }
4018                            
4019              #              #
4020            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} == START_TAG_TOKEN) {
4021              if ($token->{tag_name} eq 'col') {              if ($token->{tag_name} eq 'col') {
4022                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
4023                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
# Line 4673  sub _tree_construction_main ($) { Line 4026  sub _tree_construction_main ($) {
4026              } else {              } else {
4027                #                #
4028              }              }
4029            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
4030              if ($token->{tag_name} eq 'colgroup') {              if ($token->{tag_name} eq 'colgroup') {
4031                if ($self->{open_elements}->[-1]->[1] eq 'html') {                if ($self->{open_elements}->[-1]->[1] eq 'html') {
4032                  !!!parse-error (type => 'unmatched end tag:colgroup');                  !!!parse-error (type => 'unmatched end tag:colgroup');
# Line 4682  sub _tree_construction_main ($) { Line 4035  sub _tree_construction_main ($) {
4035                  redo B;                  redo B;
4036                } else {                } else {
4037                  pop @{$self->{open_elements}}; # colgroup                  pop @{$self->{open_elements}}; # colgroup
4038                  $self->{insertion_mode} = 'in table';                  $self->{insertion_mode} = IN_TABLE_IM;
4039                  !!!next-token;                  !!!next-token;
4040                  redo B;                              redo B;            
4041                }                }
# Line 4706  sub _tree_construction_main ($) { Line 4059  sub _tree_construction_main ($) {
4059              redo B;              redo B;
4060            } else {            } else {
4061              pop @{$self->{open_elements}}; # colgroup              pop @{$self->{open_elements}}; # colgroup
4062              $self->{insertion_mode} = 'in table';              $self->{insertion_mode} = IN_TABLE_IM;
4063              ## reprocess              ## reprocess
4064              redo B;              redo B;
4065            }            }
4066          } elsif ($self->{insertion_mode} eq 'in select') {      } elsif ($self->{insertion_mode} == IN_SELECT_IM) {
4067            if ($token->{type} eq 'character') {        if ($token->{type} == CHARACTER_TOKEN) {
4068              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4069              !!!next-token;          !!!next-token;
4070              redo B;          redo B;
4071            } elsif ($token->{type} eq 'start tag') {        } elsif ($token->{type} == START_TAG_TOKEN) {
4072              if ($token->{tag_name} eq 'option') {              if ($token->{tag_name} eq 'option') {
4073                if ($self->{open_elements}->[-1]->[1] eq 'option') {                if ($self->{open_elements}->[-1]->[1] eq 'option') {
4074                  ## As if </option>                  ## As if </option>
# Line 4768  sub _tree_construction_main ($) { Line 4121  sub _tree_construction_main ($) {
4121    
4122                !!!next-token;                !!!next-token;
4123                redo B;                redo B;
4124              } else {          } else {
4125                #            !!!parse-error (type => 'in select:'.$token->{tag_name});
4126              }            ## Ignore the token
4127            } elsif ($token->{type} eq 'end tag') {            !!!next-token;
4128              redo B;
4129            }
4130          } elsif ($token->{type} == END_TAG_TOKEN) {
4131              if ($token->{tag_name} eq 'optgroup') {              if ($token->{tag_name} eq 'optgroup') {
4132                if ($self->{open_elements}->[-1]->[1] eq 'option' and                if ($self->{open_elements}->[-1]->[1] eq 'option' and
4133                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {
# Line 4873  sub _tree_construction_main ($) { Line 4229  sub _tree_construction_main ($) {
4229    
4230                ## reprocess                ## reprocess
4231                redo B;                redo B;
4232              } else {          } else {
4233                #            !!!parse-error (type => 'in select:/'.$token->{tag_name});
             }  
           } else {  
             #  
           }  
   
           !!!parse-error (type => 'in select:'.$token->{tag_name});  
4234            ## Ignore the token            ## Ignore the token
4235            !!!next-token;            !!!next-token;
4236            redo B;            redo B;
4237          } elsif ($self->{insertion_mode} eq 'after body') {          }
4238            if ($token->{type} eq 'character') {        } else {
4239              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          die "$0: $token->{type}: Unknown token type";
4240                my $data = $1;        }
4241                ## As if in body      } elsif ($self->{insertion_mode} & BODY_AFTER_IMS) {
4242                $reconstruct_active_formatting_elements->($insert_to_current);        if ($token->{type} == CHARACTER_TOKEN) {
4243            if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4244              my $data = $1;
4245              ## As if in body
4246              $reconstruct_active_formatting_elements->($insert_to_current);
4247                                
4248                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4249              
4250              unless (length $token->{data}) {
4251                !!!next-token;
4252                redo B;
4253              }
4254            }
4255            
4256            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4257              !!!parse-error (type => 'after html:#character');
4258    
4259                unless (length $token->{data}) {            ## Reprocess in the "main" phase, "after body" insertion mode...
4260                  !!!next-token;          }
4261                  redo B;          
4262                }          ## "after body" insertion mode
4263              }          !!!parse-error (type => 'after body:#character');
4264                
4265              #          $self->{insertion_mode} = IN_BODY_IM;
4266              !!!parse-error (type => 'after body:#character');          ## reprocess
4267            } elsif ($token->{type} eq 'start tag') {          redo B;
4268              !!!parse-error (type => 'after body:'.$token->{tag_name});        } elsif ($token->{type} == START_TAG_TOKEN) {
4269              #          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4270            } elsif ($token->{type} eq 'end tag') {            !!!parse-error (type => 'after html:'.$token->{tag_name});
4271              if ($token->{tag_name} eq 'html') {            
4272                if (defined $self->{inner_html_node}) {            ## Reprocess in the "main" phase, "after body" insertion mode...
4273                  !!!parse-error (type => 'unmatched end tag:html');          }
4274                  ## Ignore the token  
4275                  !!!next-token;          ## "after body" insertion mode
4276                  redo B;          !!!parse-error (type => 'after body:'.$token->{tag_name});
4277                } else {  
4278                  $previous_insertion_mode = $self->{insertion_mode};          $self->{insertion_mode} = IN_BODY_IM;
4279                  $self->{insertion_mode} = 'trailing end';          ## reprocess
4280                  !!!next-token;          redo B;
4281                  redo B;        } elsif ($token->{type} == END_TAG_TOKEN) {
4282                }          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4283              } else {            !!!parse-error (type => 'after html:/'.$token->{tag_name});
4284                !!!parse-error (type => 'after body:/'.$token->{tag_name});            
4285              }            $self->{insertion_mode} = AFTER_BODY_IM;
4286              ## Reprocess in the "main" phase, "after body" insertion mode...
4287            }
4288    
4289            ## "after body" insertion mode
4290            if ($token->{tag_name} eq 'html') {
4291              if (defined $self->{inner_html_node}) {
4292                !!!parse-error (type => 'unmatched end tag:html');
4293                ## Ignore the token
4294                !!!next-token;
4295                redo B;
4296            } else {            } else {
4297              die "$0: $token->{type}: Unknown token type";              $self->{insertion_mode} = AFTER_HTML_BODY_IM;
4298                !!!next-token;
4299                redo B;
4300            }            }
4301            } else {
4302              !!!parse-error (type => 'after body:/'.$token->{tag_name});
4303    
4304            $self->{insertion_mode} = 'in body';            $self->{insertion_mode} = IN_BODY_IM;
4305            ## reprocess            ## reprocess
4306            redo B;            redo B;
4307      } elsif ($self->{insertion_mode} eq 'in frameset') {          }
4308        if ($token->{type} eq 'character') {        } else {
4309            die "$0: $token->{type}: Unknown token type";
4310          }
4311        } elsif ($self->{insertion_mode} & FRAME_IMS) {
4312          if ($token->{type} == CHARACTER_TOKEN) {
4313          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4314            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4315              
4316            unless (length $token->{data}) {            unless (length $token->{data}) {
4317              !!!next-token;              !!!next-token;
4318              redo B;              redo B;
4319            }            }
4320          }          }
4321            
4322            if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
4323              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4324                !!!parse-error (type => 'in frameset:#character');
4325              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
4326                !!!parse-error (type => 'after frameset:#character');
4327              } else { # "after html frameset"
4328                !!!parse-error (type => 'after html:#character');
4329    
4330                $self->{insertion_mode} = AFTER_FRAMESET_IM;
4331                ## Reprocess in the "main" phase, "after frameset"...
4332                !!!parse-error (type => 'after frameset:#character');
4333              }
4334              
4335              ## Ignore the token.
4336              if (length $token->{data}) {
4337                ## reprocess the rest of characters
4338              } else {
4339                !!!next-token;
4340              }
4341              redo B;
4342            }
4343            
4344            die qq[$0: Character "$token->{data}"];
4345          } elsif ($token->{type} == START_TAG_TOKEN) {
4346            if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4347              !!!parse-error (type => 'after html:'.$token->{tag_name});
4348    
4349          !!!parse-error (type => 'in frameset:#character');            $self->{insertion_mode} = AFTER_FRAMESET_IM;
4350          ## Ignore the token            ## Process in the "main" phase, "after frameset" insertion mode...
4351          !!!next-token;          }
4352          redo B;  
4353        } elsif ($token->{type} eq 'start tag') {          if ($token->{tag_name} eq 'frameset' and
4354          if ($token->{tag_name} eq 'frameset') {              $self->{insertion_mode} == IN_FRAMESET_IM) {
4355            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes});
4356            !!!next-token;            !!!next-token;
4357            redo B;            redo B;
4358          } elsif ($token->{tag_name} eq 'frame') {          } elsif ($token->{tag_name} eq 'frame' and
4359                     $self->{insertion_mode} == IN_FRAMESET_IM) {
4360            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes});
4361            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
4362            !!!next-token;            !!!next-token;
# Line 4957  sub _tree_construction_main ($) { Line 4366  sub _tree_construction_main ($) {
4366            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);
4367            redo B;            redo B;
4368          } else {          } else {
4369            !!!parse-error (type => 'in frameset:'.$token->{tag_name});            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4370                !!!parse-error (type => 'in frameset:'.$token->{tag_name});
4371              } else {
4372                !!!parse-error (type => 'after frameset:'.$token->{tag_name});
4373              }
4374            ## Ignore the token            ## Ignore the token
4375            !!!next-token;            !!!next-token;
4376            redo B;            redo B;
4377          }          }
4378        } elsif ($token->{type} eq 'end tag') {        } elsif ($token->{type} == END_TAG_TOKEN) {
4379          if ($token->{tag_name} eq 'frameset') {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4380              !!!parse-error (type => 'after html:/'.$token->{tag_name});
4381    
4382              $self->{insertion_mode} = AFTER_FRAMESET_IM;
4383              ## Process in the "main" phase, "after frameset" insertion mode...
4384            }
4385    
4386            if ($token->{tag_name} eq 'frameset' and
4387                $self->{insertion_mode} == IN_FRAMESET_IM) {
4388            if ($self->{open_elements}->[-1]->[1] eq 'html' and            if ($self->{open_elements}->[-1]->[1] eq 'html' and
4389                @{$self->{open_elements}} == 1) {                @{$self->{open_elements}} == 1) {
4390              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
# Line 4976  sub _tree_construction_main ($) { Line 4397  sub _tree_construction_main ($) {
4397    
4398            if (not defined $self->{inner_html_node} and            if (not defined $self->{inner_html_node} and
4399                $self->{open_elements}->[-1]->[1] ne 'frameset') {                $self->{open_elements}->[-1]->[1] ne 'frameset') {
4400              $self->{insertion_mode} = 'after frameset';              $self->{insertion_mode} = AFTER_FRAMESET_IM;
4401            }            }
4402            redo B;            redo B;
4403            } elsif ($token->{tag_name} eq 'html' and
4404                     $self->{insertion_mode} == AFTER_FRAMESET_IM) {
4405              $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
4406              !!!next-token;
4407              redo B;
4408          } else {          } else {
4409            !!!parse-error (type => 'in frameset:/'.$token->{tag_name});            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4410                !!!parse-error (type => 'in frameset:/'.$token->{tag_name});
4411              } else {
4412                !!!parse-error (type => 'after frameset:/'.$token->{tag_name});
4413              }
4414            ## Ignore the token            ## Ignore the token
4415            !!!next-token;            !!!next-token;
4416            redo B;            redo B;
# Line 4988  sub _tree_construction_main ($) { Line 4418  sub _tree_construction_main ($) {
4418        } else {        } else {
4419          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
4420        }        }
     } elsif ($self->{insertion_mode} eq 'after frameset') {  
       if ($token->{type} eq 'character') {  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
4421    
4422                unless (length $token->{data}) {        ## ISSUE: An issue in spec here
4423                  !!!next-token;      } else {
4424                  redo B;        die "$0: $self->{insertion_mode}: Unknown insertion mode";
4425                }      }
             }  
4426    
4427              if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {      ## "in body" insertion mode
4428                !!!parse-error (type => 'after frameset:#character');      if ($token->{type} == START_TAG_TOKEN) {
4429          if ($token->{tag_name} eq 'script') {
4430            ## NOTE: This is an "as if in head" code clone
4431            $script_start_tag->($insert);
4432            redo B;
4433          } elsif ($token->{tag_name} eq 'style') {
4434            ## NOTE: This is an "as if in head" code clone
4435            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
4436            redo B;
4437          } elsif ({
4438                    base => 1, link => 1,
4439                   }->{$token->{tag_name}}) {
4440            ## NOTE: This is an "as if in head" code clone, only "-t" differs
4441            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4442            pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4443            !!!next-token;
4444            redo B;
4445          } elsif ($token->{tag_name} eq 'meta') {
4446            ## NOTE: This is an "as if in head" code clone, only "-t" differs
4447            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4448            pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4449    
4450                ## Ignore the token.          unless ($self->{confident}) {
4451                if (length $token->{data}) {            if ($token->{attributes}->{charset}) { ## TODO: And if supported
4452                  ## reprocess the rest of characters              $self->{change_encoding}
4453                } else {                  ->($self, $token->{attributes}->{charset}->{value});
4454                  !!!next-token;            } elsif ($token->{attributes}->{content}) {
4455                }              ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.
4456                redo B;              if ($token->{attributes}->{content}->{value}
4457                    =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=
4458                        [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4459                        ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
4460                  $self->{change_encoding}
4461                      ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);
4462              }              }
4463              }
4464            }
4465    
4466          die qq[$0: Character "$token->{data}"];          !!!next-token;
4467        } elsif ($token->{type} eq 'start tag') {          redo B;
4468          if ($token->{tag_name} eq 'noframes') {        } elsif ($token->{tag_name} eq 'title') {
4469            ## NOTE: As if in body.          !!!parse-error (type => 'in body:title');
4470            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);          ## NOTE: This is an "as if in head" code clone
4471            redo B;          $parse_rcdata->(RCDATA_CONTENT_MODEL, sub {
4472          } else {            if (defined $self->{head_element}) {
4473            !!!parse-error (type => 'after frameset:'.$token->{tag_name});              $self->{head_element}->append_child ($_[0]);
4474              } else {
4475                $insert->($_[0]);
4476              }
4477            });
4478            redo B;
4479          } elsif ($token->{tag_name} eq 'body') {
4480            !!!parse-error (type => 'in body:body');
4481                  
4482            if (@{$self->{open_elements}} == 1 or
4483                $self->{open_elements}->[1]->[1] ne 'body') {
4484            ## Ignore the token            ## Ignore the token
4485            } else {
4486              my $body_el = $self->{open_elements}->[1]->[0];
4487              for my $attr_name (keys %{$token->{attributes}}) {
4488                unless ($body_el->has_attribute_ns (undef, $attr_name)) {
4489                  $body_el->set_attribute_ns
4490                    (undef, [undef, $attr_name],
4491                     $token->{attributes}->{$attr_name}->{value});
4492                }
4493              }
4494            }
4495            !!!next-token;
4496            redo B;
4497          } elsif ({
4498                    address => 1, blockquote => 1, center => 1, dir => 1,
4499                    div => 1, dl => 1, fieldset => 1, listing => 1,
4500                    menu => 1, ol => 1, p => 1, ul => 1,
4501                    pre => 1,
4502                   }->{$token->{tag_name}}) {
4503            ## has a p element in scope
4504            INSCOPE: for (reverse @{$self->{open_elements}}) {
4505              if ($_->[1] eq 'p') {
4506                !!!back-token;
4507                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4508                redo B;
4509              } elsif ({
4510                        table => 1, caption => 1, td => 1, th => 1,
4511                        button => 1, marquee => 1, object => 1, html => 1,
4512                       }->{$_->[1]}) {
4513                last INSCOPE;
4514              }
4515            } # INSCOPE
4516              
4517            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4518            if ($token->{tag_name} eq 'pre') {
4519              !!!next-token;
4520              if ($token->{type} == CHARACTER_TOKEN) {
4521                $token->{data} =~ s/^\x0A//;
4522                unless (length $token->{data}) {
4523                  !!!next-token;
4524                }
4525              }
4526            } else {
4527            !!!next-token;            !!!next-token;
           redo B;  
4528          }          }
4529        } elsif ($token->{type} eq 'end tag') {          redo B;
4530          if ($token->{tag_name} eq 'html') {        } elsif ($token->{tag_name} eq 'form') {
4531            $previous_insertion_mode = $self->{insertion_mode};          if (defined $self->{form_element}) {
4532            $self->{insertion_mode} = 'trailing end';            !!!parse-error (type => 'in form:form');
4533              ## Ignore the token
4534            !!!next-token;            !!!next-token;
4535            redo B;            redo B;
4536          } else {          } else {
4537            !!!parse-error (type => 'after frameset:/'.$token->{tag_name});            ## has a p element in scope
4538            ## Ignore the token            INSCOPE: for (reverse @{$self->{open_elements}}) {
4539                if ($_->[1] eq 'p') {
4540                  !!!back-token;
4541                  $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4542                  redo B;
4543                } elsif ({
4544                          table => 1, caption => 1, td => 1, th => 1,
4545                          button => 1, marquee => 1, object => 1, html => 1,
4546                         }->{$_->[1]}) {
4547                  last INSCOPE;
4548                }
4549              } # INSCOPE
4550                
4551              !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4552              $self->{form_element} = $self->{open_elements}->[-1]->[0];
4553            !!!next-token;            !!!next-token;
4554            redo B;            redo B;
4555          }          }
4556        } else {        } elsif ($token->{tag_name} eq 'li') {
4557          die "$0: $token->{type}: Unknown token type";          ## has a p element in scope
4558        }          INSCOPE: for (reverse @{$self->{open_elements}}) {
4559              if ($_->[1] eq 'p') {
4560                !!!back-token;
4561                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4562                redo B;
4563              } elsif ({
4564                        table => 1, caption => 1, td => 1, th => 1,
4565                        button => 1, marquee => 1, object => 1, html => 1,
4566                       }->{$_->[1]}) {
4567                last INSCOPE;
4568              }
4569            } # INSCOPE
4570              
4571            ## Step 1
4572            my $i = -1;
4573            my $node = $self->{open_elements}->[$i];
4574            LI: {
4575              ## Step 2
4576              if ($node->[1] eq 'li') {
4577                if ($i != -1) {
4578                  !!!parse-error (type => 'end tag missing:'.
4579                                  $self->{open_elements}->[-1]->[1]);
4580                }
4581                splice @{$self->{open_elements}}, $i;
4582                last LI;
4583              }
4584              
4585              ## Step 3
4586              if (not $formatting_category->{$node->[1]} and
4587                  #not $phrasing_category->{$node->[1]} and
4588                  ($special_category->{$node->[1]} or
4589                   $scoping_category->{$node->[1]}) and
4590                  $node->[1] ne 'address' and $node->[1] ne 'div') {
4591                last LI;
4592              }
4593              
4594              ## Step 4
4595              $i--;
4596              $node = $self->{open_elements}->[$i];
4597              redo LI;
4598            } # LI
4599              
4600            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4601            !!!next-token;
4602            redo B;
4603          } elsif ($token->{tag_name} eq 'dd' or $token->{tag_name} eq 'dt') {
4604            ## has a p element in scope
4605            INSCOPE: for (reverse @{$self->{open_elements}}) {
4606              if ($_->[1] eq 'p') {
4607                !!!back-token;
4608                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4609                redo B;
4610              } elsif ({
4611                        table => 1, caption => 1, td => 1, th => 1,
4612                        button => 1, marquee => 1, object => 1, html => 1,
4613                       }->{$_->[1]}) {
4614                last INSCOPE;
4615              }
4616            } # INSCOPE
4617              
4618            ## Step 1
4619            my $i = -1;
4620            my $node = $self->{open_elements}->[$i];
4621            LI: {
4622              ## Step 2
4623              if ($node->[1] eq 'dt' or $node->[1] eq 'dd') {
4624                if ($i != -1) {
4625                  !!!parse-error (type => 'end tag missing:'.
4626                                  $self->{open_elements}->[-1]->[1]);
4627                }
4628                splice @{$self->{open_elements}}, $i;
4629                last LI;
4630              }
4631              
4632              ## Step 3
4633              if (not $formatting_category->{$node->[1]} and
4634                  #not $phrasing_category->{$node->[1]} and
4635                  ($special_category->{$node->[1]} or
4636                   $scoping_category->{$node->[1]}) and
4637                  $node->[1] ne 'address' and $node->[1] ne 'div') {
4638                last LI;
4639              }
4640              
4641              ## Step 4
4642              $i--;
4643              $node = $self->{open_elements}->[$i];
4644              redo LI;
4645            } # LI
4646              
4647            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4648            !!!next-token;
4649            redo B;
4650          } elsif ($token->{tag_name} eq 'plaintext') {
4651            ## has a p element in scope
4652            INSCOPE: for (reverse @{$self->{open_elements}}) {
4653              if ($_->[1] eq 'p') {
4654                !!!back-token;
4655                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4656                redo B;
4657              } elsif ({
4658                        table => 1, caption => 1, td => 1, th => 1,
4659                        button => 1, marquee => 1, object => 1, html => 1,
4660                       }->{$_->[1]}) {
4661                last INSCOPE;
4662              }
4663            } # INSCOPE
4664              
4665            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4666              
4667            $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
4668              
4669            !!!next-token;
4670            redo B;
4671          } elsif ({
4672                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
4673                   }->{$token->{tag_name}}) {
4674            ## has a p element in scope
4675            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4676              my $node = $self->{open_elements}->[$_];
4677              if ($node->[1] eq 'p') {
4678                !!!back-token;
4679                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4680                redo B;
4681              } elsif ({
4682                        table => 1, caption => 1, td => 1, th => 1,
4683                        button => 1, marquee => 1, object => 1, html => 1,
4684                       }->{$node->[1]}) {
4685                last INSCOPE;
4686              }
4687            } # INSCOPE
4688              
4689            ## NOTE: See <http://html5.org/tools/web-apps-tracker?from=925&to=926>
4690            ## has an element in scope
4691            #my $i;
4692            #INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4693            #  my $node = $self->{open_elements}->[$_];
4694            #  if ({
4695            #       h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
4696            #      }->{$node->[1]}) {
4697            #    $i = $_;
4698            #    last INSCOPE;
4699            #  } elsif ({
4700            #            table => 1, caption => 1, td => 1, th => 1,
4701            #            button => 1, marquee => 1, object => 1, html => 1,
4702            #           }->{$node->[1]}) {
4703            #    last INSCOPE;
4704            #  }
4705            #} # INSCOPE
4706            #  
4707            #if (defined $i) {
4708            #  !!! parse-error (type => 'in hn:hn');
4709            #  splice @{$self->{open_elements}}, $i;
4710            #}
4711              
4712            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4713              
4714            !!!next-token;
4715            redo B;
4716          } elsif ($token->{tag_name} eq 'a') {
4717            AFE: for my $i (reverse 0..$#$active_formatting_elements) {
4718              my $node = $active_formatting_elements->[$i];
4719              if ($node->[1] eq 'a') {
4720                !!!parse-error (type => 'in a:a');
4721                
4722                !!!back-token;
4723                $token = {type => END_TAG_TOKEN, tag_name => 'a'};
4724                $formatting_end_tag->($token->{tag_name});
4725                
4726                AFE2: for (reverse 0..$#$active_formatting_elements) {
4727                  if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
4728                    splice @$active_formatting_elements, $_, 1;
4729                    last AFE2;
4730                  }
4731                } # AFE2
4732                OE: for (reverse 0..$#{$self->{open_elements}}) {
4733                  if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {
4734                    splice @{$self->{open_elements}}, $_, 1;
4735                    last OE;
4736                  }
4737                } # OE
4738                last AFE;
4739              } elsif ($node->[0] eq '#marker') {
4740                last AFE;
4741              }
4742            } # AFE
4743              
4744            $reconstruct_active_formatting_elements->($insert_to_current);
4745    
4746        ## ISSUE: An issue in spec here          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4747      } elsif ($self->{insertion_mode} eq 'trailing end') {          push @$active_formatting_elements, $self->{open_elements}->[-1];
4748        ## states in the main stage is preserved yet # MUST  
4749                  !!!next-token;
4750        if ($token->{type} eq 'character') {          redo B;
4751          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {        } elsif ({
4752            my $data = $1;                  b => 1, big => 1, em => 1, font => 1, i => 1,
4753            ## As if in the main phase.                  s => 1, small => 1, strile => 1,
4754            ## NOTE: The insertion mode in the main phase                  strong => 1, tt => 1, u => 1,
4755            ## just before the phase has been changed to the trailing                 }->{$token->{tag_name}}) {
4756            ## end phase is either "after body" or "after frameset".          $reconstruct_active_formatting_elements->($insert_to_current);
4757            $reconstruct_active_formatting_elements->($insert_to_current);          
4758            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4759            push @$active_formatting_elements, $self->{open_elements}->[-1];
4760            
4761            !!!next-token;
4762            redo B;
4763          } elsif ($token->{tag_name} eq 'nobr') {
4764            $reconstruct_active_formatting_elements->($insert_to_current);
4765    
4766            ## has a |nobr| element in scope
4767            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4768              my $node = $self->{open_elements}->[$_];
4769              if ($node->[1] eq 'nobr') {
4770                !!!parse-error (type => 'in nobr:nobr');
4771                !!!back-token;
4772                $token = {type => END_TAG_TOKEN, tag_name => 'nobr'};
4773                redo B;
4774              } elsif ({
4775                        table => 1, caption => 1, td => 1, th => 1,
4776                        button => 1, marquee => 1, object => 1, html => 1,
4777                       }->{$node->[1]}) {
4778                last INSCOPE;
4779              }
4780            } # INSCOPE
4781            
4782            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4783            push @$active_formatting_elements, $self->{open_elements}->[-1];
4784            
4785            !!!next-token;
4786            redo B;
4787          } elsif ($token->{tag_name} eq 'button') {
4788            ## has a button element in scope
4789            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4790              my $node = $self->{open_elements}->[$_];
4791              if ($node->[1] eq 'button') {
4792                !!!parse-error (type => 'in button:button');
4793                !!!back-token;
4794                $token = {type => END_TAG_TOKEN, tag_name => 'button'};
4795                redo B;
4796              } elsif ({
4797                        table => 1, caption => 1, td => 1, th => 1,
4798                        button => 1, marquee => 1, object => 1, html => 1,
4799                       }->{$node->[1]}) {
4800                last INSCOPE;
4801              }
4802            } # INSCOPE
4803                        
4804            $self->{open_elements}->[-1]->[0]->manakai_append_text ($data);          $reconstruct_active_formatting_elements->($insert_to_current);
4805                        
4806            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4807            push @$active_formatting_elements, ['#marker', ''];
4808    
4809            !!!next-token;
4810            redo B;
4811          } elsif ($token->{tag_name} eq 'marquee' or
4812                   $token->{tag_name} eq 'object') {
4813            $reconstruct_active_formatting_elements->($insert_to_current);
4814            
4815            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4816            push @$active_formatting_elements, ['#marker', ''];
4817            
4818            !!!next-token;
4819            redo B;
4820          } elsif ($token->{tag_name} eq 'xmp') {
4821            $reconstruct_active_formatting_elements->($insert_to_current);
4822            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
4823            redo B;
4824          } elsif ($token->{tag_name} eq 'table') {
4825            ## has a p element in scope
4826            INSCOPE: for (reverse @{$self->{open_elements}}) {
4827              if ($_->[1] eq 'p') {
4828                !!!back-token;
4829                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4830                redo B;
4831              } elsif ({
4832                        table => 1, caption => 1, td => 1, th => 1,
4833                        button => 1, marquee => 1, object => 1, html => 1,
4834                       }->{$_->[1]}) {
4835                last INSCOPE;
4836              }
4837            } # INSCOPE
4838              
4839            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4840              
4841            $self->{insertion_mode} = IN_TABLE_IM;
4842              
4843            !!!next-token;
4844            redo B;
4845          } elsif ({
4846                    area => 1, basefont => 1, bgsound => 1, br => 1,
4847                    embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
4848                    image => 1,
4849                   }->{$token->{tag_name}}) {
4850            if ($token->{tag_name} eq 'image') {
4851              !!!parse-error (type => 'image');
4852              $token->{tag_name} = 'img';
4853            }
4854    
4855            ## NOTE: There is an "as if <br>" code clone.
4856            $reconstruct_active_formatting_elements->($insert_to_current);
4857            
4858            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4859            pop @{$self->{open_elements}};
4860            
4861            !!!next-token;
4862            redo B;
4863          } elsif ($token->{tag_name} eq 'hr') {
4864            ## has a p element in scope
4865            INSCOPE: for (reverse @{$self->{open_elements}}) {
4866              if ($_->[1] eq 'p') {
4867                !!!back-token;
4868                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4869                redo B;
4870              } elsif ({
4871                        table => 1, caption => 1, td => 1, th => 1,
4872                        button => 1, marquee => 1, object => 1, html => 1,
4873                       }->{$_->[1]}) {
4874                last INSCOPE;
4875              }
4876            } # INSCOPE
4877              
4878            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4879            pop @{$self->{open_elements}};
4880              
4881            !!!next-token;
4882            redo B;
4883          } elsif ($token->{tag_name} eq 'input') {
4884            $reconstruct_active_formatting_elements->($insert_to_current);
4885            
4886            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4887            ## TODO: associate with $self->{form_element} if defined
4888            pop @{$self->{open_elements}};
4889            
4890            !!!next-token;
4891            redo B;
4892          } elsif ($token->{tag_name} eq 'isindex') {
4893            !!!parse-error (type => 'isindex');
4894            
4895            if (defined $self->{form_element}) {
4896              ## Ignore the token
4897              !!!next-token;
4898              redo B;
4899            } else {
4900              my $at = $token->{attributes};
4901              my $form_attrs;
4902              $form_attrs->{action} = $at->{action} if $at->{action};
4903              my $prompt_attr = $at->{prompt};
4904              $at->{name} = {name => 'name', value => 'isindex'};
4905              delete $at->{action};
4906              delete $at->{prompt};
4907              my @tokens = (
4908                            {type => START_TAG_TOKEN, tag_name => 'form',
4909                             attributes => $form_attrs},
4910                            {type => START_TAG_TOKEN, tag_name => 'hr'},
4911                            {type => START_TAG_TOKEN, tag_name => 'p'},
4912                            {type => START_TAG_TOKEN, tag_name => 'label'},
4913                           );
4914              if ($prompt_attr) {
4915                push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value}};
4916              } else {
4917                push @tokens, {type => CHARACTER_TOKEN,
4918                               data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD
4919                ## TODO: make this configurable
4920              }
4921              push @tokens,
4922                            {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at},
4923                            #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
4924                            {type => END_TAG_TOKEN, tag_name => 'label'},
4925                            {type => END_TAG_TOKEN, tag_name => 'p'},
4926                            {type => START_TAG_TOKEN, tag_name => 'hr'},
4927                            {type => END_TAG_TOKEN, tag_name => 'form'};
4928              $token = shift @tokens;
4929              !!!back-token (@tokens);
4930              redo B;
4931            }
4932          } elsif ($token->{tag_name} eq 'textarea') {
4933            my $tag_name = $token->{tag_name};
4934            my $el;
4935            !!!create-element ($el, $token->{tag_name}, $token->{attributes});
4936            
4937            ## TODO: $self->{form_element} if defined
4938            $self->{content_model} = RCDATA_CONTENT_MODEL;
4939            delete $self->{escape}; # MUST
4940            
4941            $insert->($el);
4942            
4943            my $text = '';
4944            !!!next-token;
4945            if ($token->{type} == CHARACTER_TOKEN) {
4946              $token->{data} =~ s/^\x0A//;
4947            unless (length $token->{data}) {            unless (length $token->{data}) {
4948              !!!next-token;              !!!next-token;
             redo B;  
4949            }            }
4950          }          }
4951            while ($token->{type} == CHARACTER_TOKEN) {
4952              $text .= $token->{data};
4953              !!!next-token;
4954            }
4955            if (length $text) {
4956              $el->manakai_append_text ($text);
4957            }
4958            
4959            $self->{content_model} = PCDATA_CONTENT_MODEL;
4960            
4961            if ($token->{type} == END_TAG_TOKEN and
4962                $token->{tag_name} eq $tag_name) {
4963              ## Ignore the token
4964            } else {
4965              !!!parse-error (type => 'in RCDATA:#'.$token->{type});
4966            }
4967            !!!next-token;
4968            redo B;
4969          } elsif ({
4970                    iframe => 1,
4971                    noembed => 1,
4972                    noframes => 1,
4973                    noscript => 0, ## TODO: 1 if scripting is enabled
4974                   }->{$token->{tag_name}}) {
4975            ## NOTE: There is an "as if in body" code clone.
4976            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
4977            redo B;
4978          } elsif ($token->{tag_name} eq 'select') {
4979            $reconstruct_active_formatting_elements->($insert_to_current);
4980            
4981            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4982            
4983            $self->{insertion_mode} = IN_SELECT_IM;
4984            !!!next-token;
4985            redo B;
4986          } elsif ({
4987                    caption => 1, col => 1, colgroup => 1, frame => 1,
4988                    frameset => 1, head => 1, option => 1, optgroup => 1,
4989                    tbody => 1, td => 1, tfoot => 1, th => 1,
4990                    thead => 1, tr => 1,
4991                   }->{$token->{tag_name}}) {
4992            !!!parse-error (type => 'in body:'.$token->{tag_name});
4993            ## Ignore the token
4994            !!!next-token;
4995            redo B;
4996            
4997            ## ISSUE: An issue on HTML5 new elements in the spec.
4998          } else {
4999            $reconstruct_active_formatting_elements->($insert_to_current);
5000            
5001            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5002            
5003            !!!next-token;
5004            redo B;
5005          }
5006        } elsif ($token->{type} == END_TAG_TOKEN) {
5007          if ($token->{tag_name} eq 'body') {
5008            if (@{$self->{open_elements}} > 1 and
5009                $self->{open_elements}->[1]->[1] eq 'body') {
5010              for (@{$self->{open_elements}}) {
5011                unless ({
5012                           dd => 1, dt => 1, li => 1, p => 1, td => 1,
5013                           th => 1, tr => 1, body => 1, html => 1,
5014                         tbody => 1, tfoot => 1, thead => 1,
5015                        }->{$_->[1]}) {
5016                  !!!parse-error (type => 'not closed:'.$_->[1]);
5017                }
5018              }
5019    
5020          !!!parse-error (type => 'after html:#character');            $self->{insertion_mode} = AFTER_BODY_IM;
5021          $self->{insertion_mode} = $previous_insertion_mode;            !!!next-token;
5022          ## reprocess            redo B;
5023            } else {
5024              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5025              ## Ignore the token
5026              !!!next-token;
5027              redo B;
5028            }
5029          } elsif ($token->{tag_name} eq 'html') {
5030            if (@{$self->{open_elements}} > 1 and $self->{open_elements}->[1]->[1] eq 'body') {
5031              ## ISSUE: There is an issue in the spec.
5032              if ($self->{open_elements}->[-1]->[1] ne 'body') {
5033                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);
5034              }
5035              $self->{insertion_mode} = AFTER_BODY_IM;
5036              ## reprocess
5037              redo B;
5038            } else {
5039              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5040              ## Ignore the token
5041              !!!next-token;
5042              redo B;
5043            }
5044          } elsif ({
5045                    address => 1, blockquote => 1, center => 1, dir => 1,
5046                    div => 1, dl => 1, fieldset => 1, listing => 1,
5047                    menu => 1, ol => 1, pre => 1, ul => 1,
5048                    p => 1,
5049                    dd => 1, dt => 1, li => 1,
5050                    button => 1, marquee => 1, object => 1,
5051                   }->{$token->{tag_name}}) {
5052            ## has an element in scope
5053            my $i;
5054            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5055              my $node = $self->{open_elements}->[$_];
5056              if ($node->[1] eq $token->{tag_name}) {
5057                ## generate implied end tags
5058                if ({
5059                     dd => ($token->{tag_name} ne 'dd'),
5060                     dt => ($token->{tag_name} ne 'dt'),
5061                     li => ($token->{tag_name} ne 'li'),
5062                     p => ($token->{tag_name} ne 'p'),
5063                     td => 1, th => 1, tr => 1,
5064                     tbody => 1, tfoot=> 1, thead => 1,
5065                    }->{$self->{open_elements}->[-1]->[1]}) {
5066                  !!!back-token;
5067                  $token = {type => END_TAG_TOKEN,
5068                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5069                  redo B;
5070                }
5071                $i = $_;
5072                last INSCOPE unless $token->{tag_name} eq 'p';
5073              } elsif ({
5074                        table => 1, caption => 1, td => 1, th => 1,
5075                        button => 1, marquee => 1, object => 1, html => 1,
5076                       }->{$node->[1]}) {
5077                last INSCOPE;
5078              }
5079            } # INSCOPE
5080            
5081            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
5082              if (defined $i) {
5083                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
5084              } else {
5085                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5086              }
5087            }
5088            
5089            if (defined $i) {
5090              splice @{$self->{open_elements}}, $i;
5091            } elsif ($token->{tag_name} eq 'p') {
5092              ## As if <p>, then reprocess the current token
5093              my $el;
5094              !!!create-element ($el, 'p');
5095              $insert->($el);
5096            }
5097            $clear_up_to_marker->()
5098              if {
5099                button => 1, marquee => 1, object => 1,
5100              }->{$token->{tag_name}};
5101            !!!next-token;
5102          redo B;          redo B;
5103        } elsif ($token->{type} eq 'start tag') {        } elsif ($token->{tag_name} eq 'form') {
5104          !!!parse-error (type => 'after html:'.$token->{tag_name});          ## has an element in scope
5105          $self->{insertion_mode} = $previous_insertion_mode;          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5106          ## reprocess            my $node = $self->{open_elements}->[$_];
5107              if ($node->[1] eq $token->{tag_name}) {
5108                ## generate implied end tags
5109                if ({
5110                     dd => 1, dt => 1, li => 1, p => 1,
5111                     td => 1, th => 1, tr => 1,
5112                     tbody => 1, tfoot=> 1, thead => 1,
5113                    }->{$self->{open_elements}->[-1]->[1]}) {
5114                  !!!back-token;
5115                  $token = {type => END_TAG_TOKEN,
5116                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5117                  redo B;
5118                }
5119                last INSCOPE;
5120              } elsif ({
5121                        table => 1, caption => 1, td => 1, th => 1,
5122                        button => 1, marquee => 1, object => 1, html => 1,
5123                       }->{$node->[1]}) {
5124                last INSCOPE;
5125              }
5126            } # INSCOPE
5127            
5128            if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {
5129              pop @{$self->{open_elements}};
5130            } else {
5131              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5132            }
5133    
5134            undef $self->{form_element};
5135            !!!next-token;
5136          redo B;          redo B;
5137        } elsif ($token->{type} eq 'end tag') {        } elsif ({
5138          !!!parse-error (type => 'after html:/'.$token->{tag_name});                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
5139          $self->{insertion_mode} = $previous_insertion_mode;                 }->{$token->{tag_name}}) {
5140          ## reprocess          ## has an element in scope
5141            my $i;
5142            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5143              my $node = $self->{open_elements}->[$_];
5144              if ({
5145                   h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
5146                  }->{$node->[1]}) {
5147                ## generate implied end tags
5148                if ({
5149                     dd => 1, dt => 1, li => 1, p => 1,
5150                     td => 1, th => 1, tr => 1,
5151                     tbody => 1, tfoot=> 1, thead => 1,
5152                    }->{$self->{open_elements}->[-1]->[1]}) {
5153                  !!!back-token;
5154                  $token = {type => END_TAG_TOKEN,
5155                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5156                  redo B;
5157                }
5158                $i = $_;
5159                last INSCOPE;
5160              } elsif ({
5161                        table => 1, caption => 1, td => 1, th => 1,
5162                        button => 1, marquee => 1, object => 1, html => 1,
5163                       }->{$node->[1]}) {
5164                last INSCOPE;
5165              }
5166            } # INSCOPE
5167            
5168            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
5169              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5170            }
5171            
5172            splice @{$self->{open_elements}}, $i if defined $i;
5173            !!!next-token;
5174            redo B;
5175          } elsif ({
5176                    a => 1,
5177                    b => 1, big => 1, em => 1, font => 1, i => 1,
5178                    nobr => 1, s => 1, small => 1, strile => 1,
5179                    strong => 1, tt => 1, u => 1,
5180                   }->{$token->{tag_name}}) {
5181            $formatting_end_tag->($token->{tag_name});
5182            redo B;
5183          } elsif ($token->{tag_name} eq 'br') {
5184            !!!parse-error (type => 'unmatched end tag:br');
5185    
5186            ## As if <br>
5187            $reconstruct_active_formatting_elements->($insert_to_current);
5188            
5189            my $el;
5190            !!!create-element ($el, 'br');
5191            $insert->($el);
5192            
5193            ## Ignore the token.
5194            !!!next-token;
5195            redo B;
5196          } elsif ({
5197                    caption => 1, col => 1, colgroup => 1, frame => 1,
5198                    frameset => 1, head => 1, option => 1, optgroup => 1,
5199                    tbody => 1, td => 1, tfoot => 1, th => 1,
5200                    thead => 1, tr => 1,
5201                    area => 1, basefont => 1, bgsound => 1,
5202                    embed => 1, hr => 1, iframe => 1, image => 1,
5203                    img => 1, input => 1, isindex => 1, noembed => 1,
5204                    noframes => 1, param => 1, select => 1, spacer => 1,
5205                    table => 1, textarea => 1, wbr => 1,
5206                    noscript => 0, ## TODO: if scripting is enabled
5207                   }->{$token->{tag_name}}) {
5208            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5209            ## Ignore the token
5210            !!!next-token;
5211          redo B;          redo B;
5212            
5213            ## ISSUE: Issue on HTML5 new elements in spec
5214            
5215        } else {        } else {
5216          die "$0: $token->{type}: Unknown token";          ## Step 1
5217            my $node_i = -1;
5218            my $node = $self->{open_elements}->[$node_i];
5219    
5220            ## Step 2
5221            S2: {
5222              if ($node->[1] eq $token->{tag_name}) {
5223                ## Step 1
5224                ## generate implied end tags
5225                if ({
5226                     dd => 1, dt => 1, li => 1, p => 1,
5227                     td => 1, th => 1, tr => 1,
5228                     tbody => 1, tfoot => 1, thead => 1,
5229                    }->{$self->{open_elements}->[-1]->[1]}) {
5230                  !!!back-token;
5231                  $token = {type => END_TAG_TOKEN,
5232                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5233                  redo B;
5234                }
5235            
5236                ## Step 2
5237                if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {
5238                  ## NOTE: <x><y></x>
5239                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
5240                }
5241                
5242                ## Step 3
5243                splice @{$self->{open_elements}}, $node_i;
5244    
5245                !!!next-token;
5246                last S2;
5247              } else {
5248                ## Step 3
5249                if (not $formatting_category->{$node->[1]} and
5250                    #not $phrasing_category->{$node->[1]} and
5251                    ($special_category->{$node->[1]} or
5252                     $scoping_category->{$node->[1]})) {
5253                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5254                  ## Ignore the token
5255                  !!!next-token;
5256                  last S2;
5257                }
5258              }
5259              
5260              ## Step 4
5261              $node_i--;
5262              $node = $self->{open_elements}->[$node_i];
5263              
5264              ## Step 5;
5265              redo S2;
5266            } # S2
5267            redo B;
5268        }        }
     } else {  
       die "$0: $self->{insertion_mode}: Unknown insertion mode";  
5269      }      }
5270        redo B;
5271    } # B    } # B
5272    
5273      ## NOTE: The "trailing end" phase in HTML5 is split into
5274      ## two insertion modes: "after html body" and "after html frameset".
5275      ## NOTE: States in the main stage is preserved while
5276      ## the parser stays in the trailing end phase. # MUST
5277    
5278    ## Stop parsing # MUST    ## Stop parsing # MUST
5279        
5280    ## TODO: script stuffs    ## TODO: script stuffs
# Line 5093  sub set_inner_html ($$$) { Line 5286  sub set_inner_html ($$$) {
5286    my $s = \$_[0];    my $s = \$_[0];
5287    my $onerror = $_[1];    my $onerror = $_[1];
5288    
5289      ## ISSUE: Should {confident} be true?
5290    
5291    my $nt = $node->node_type;    my $nt = $node->node_type;
5292    if ($nt == 9) {    if ($nt == 9) {
5293      # MUST      # MUST
# Line 5245  sub set_inner_html ($$$) { Line 5440  sub set_inner_html ($$$) {
5440    
5441  } # tree construction stage  } # tree construction stage
5442    
5443  sub get_inner_html ($$$) {  package Whatpm::HTML::RestartParser;
5444    my (undef, $node, $on_error) = @_;  push our @ISA, 'Error';
   
   ## Step 1  
   my $s = '';  
   
   my $in_cdata;  
   my $parent = $node;  
   while (defined $parent) {  
     if ($parent->node_type == 1 and  
         $parent->namespace_uri eq 'http://www.w3.org/1999/xhtml' and  
         {  
           style => 1, script => 1, xmp => 1, iframe => 1,  
           noembed => 1, noframes => 1, noscript => 1,  
         }->{$parent->local_name}) { ## TODO: case thingy  
       $in_cdata = 1;  
     }  
     $parent = $parent->parent_node;  
   }  
   
   ## Step 2  
   my @node = @{$node->child_nodes};  
   C: while (@node) {  
     my $child = shift @node;  
     unless (ref $child) {  
       if ($child eq 'cdata-out') {  
         $in_cdata = 0;  
       } else {  
         $s .= $child; # end tag  
       }  
       next C;  
     }  
       
     my $nt = $child->node_type;  
     if ($nt == 1) { # Element  
       my $tag_name = $child->tag_name; ## TODO: manakai_tag_name  
       $s .= '<' . $tag_name;  
       ## NOTE: Non-HTML case:  
       ## <http://permalink.gmane.org/gmane.org.w3c.whatwg.discuss/11191>  
   
       my @attrs = @{$child->attributes}; # sort order MUST be stable  
       for my $attr (@attrs) { # order is implementation dependent  
         my $attr_name = $attr->name; ## TODO: manakai_name  
         $s .= ' ' . $attr_name . '="';  
         my $attr_value = $attr->value;  
         ## escape  
         $attr_value =~ s/&/&amp;/g;  
         $attr_value =~ s/</&lt;/g;  
         $attr_value =~ s/>/&gt;/g;  
         $attr_value =~ s/"/&quot;/g;  
         $s .= $attr_value . '"';  
       }  
       $s .= '>';  
         
       next C if {  
         area => 1, base => 1, basefont => 1, bgsound => 1,  
         br => 1, col => 1, embed => 1, frame => 1, hr => 1,  
         img => 1, input => 1, link => 1, meta => 1, param => 1,  
         spacer => 1, wbr => 1,  
       }->{$tag_name};  
   
       $s .= "\x0A" if $tag_name eq 'pre' or $tag_name eq 'textarea';  
   
       if (not $in_cdata and {  
         style => 1, script => 1, xmp => 1, iframe => 1,  
         noembed => 1, noframes => 1, noscript => 1,  
         plaintext => 1,  
       }->{$tag_name}) {  
         unshift @node, 'cdata-out';  
         $in_cdata = 1;  
       }  
   
       unshift @node, @{$child->child_nodes}, '</' . $tag_name . '>';  
     } elsif ($nt == 3 or $nt == 4) {  
       if ($in_cdata) {  
         $s .= $child->data;  
       } else {  
         my $value = $child->data;  
         $value =~ s/&/&amp;/g;  
         $value =~ s/</&lt;/g;  
         $value =~ s/>/&gt;/g;  
         $value =~ s/"/&quot;/g;  
         $s .= $value;  
       }  
     } elsif ($nt == 8) {  
       $s .= '<!--' . $child->data . '-->';  
     } elsif ($nt == 10) {  
       $s .= '<!DOCTYPE ' . $child->name . '>';  
     } elsif ($nt == 5) { # entrefs  
       push @node, @{$child->child_nodes};  
     } else {  
       $on_error->($child) if defined $on_error;  
     }  
     ## ISSUE: This code does not support PIs.  
   } # C  
     
   ## Step 3  
   return \$s;  
 } # get_inner_html  
5445    
5446  1;  1;
5447  # $Date$  # $Date$

Legend:
Removed from v.1.49  
changed lines
  Added in v.1.63

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24