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

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

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

revision 1.44 by wakaba, Sat Jul 21 07:34:32 2007 UTC revision 1.71 by wakaba, Sun Mar 2 03:39:41 2008 UTC
# Line 1  Line 1 
1  package Whatpm::HTML;  package Whatpm::HTML;
2  use strict;  use strict;
3  our $VERSION=do{my @r=(q$Revision$=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};  our $VERSION=do{my @r=(q$Revision$=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};
4    use Error qw(:try);
5    
6  ## ISSUE:  ## ISSUE:
7  ## var doc = implementation.createDocument (null, null, null);  ## var doc = implementation.createDocument (null, null, null);
8  ## doc.write ('');  ## doc.write ('');
9  ## alert (doc.compatMode);  ## alert (doc.compatMode);
10    
11  ## ISSUE: HTML5 revision 967 says that the encoding layer MUST NOT  ## TODO: Control charcters and noncharacters are not allowed (HTML5 revision 1263)
12  ## strip BOM and the HTML layer MUST ignore it.  Whether we can do it  ## TODO: 1252 parse error (revision 1264)
13  ## is not yet clear.  ## TODO: 8859-11 = 874 (revision 1271)
 ## "{U+FEFF}..." in UTF-16BE/UTF-16LE is three or four characters?  
 ## "{U+FEFF}..." in GB18030?  
14    
15  my $permitted_slash_tag_name = {  my $permitted_slash_tag_name = {
16    base => 1,    base => 1,
# Line 19  my $permitted_slash_tag_name = { Line 18  my $permitted_slash_tag_name = {
18    meta => 1,    meta => 1,
19    hr => 1,    hr => 1,
20    br => 1,    br => 1,
21    img=> 1,    img => 1,
22    embed => 1,    embed => 1,
23    param => 1,    param => 1,
24    area => 1,    area => 1,
# Line 84  my $formatting_category = { Line 83  my $formatting_category = {
83  };  };
84  # $phrasing_category: all other elements  # $phrasing_category: all other elements
85    
86    sub parse_byte_string ($$$$;$) {
87      my $self = ref $_[0] ? shift : shift->new;
88      my $charset = shift;
89      my $bytes_s = ref $_[0] ? $_[0] : \($_[0]);
90      my $s;
91      
92      if (defined $charset) {
93        require Encode; ## TODO: decode(utf8) don't delete BOM
94        $s = \ (Encode::decode ($charset, $$bytes_s));
95        $self->{input_encoding} = lc $charset; ## TODO: normalize name
96        $self->{confident} = 1;
97      } else {
98        ## TODO: Implement HTML5 detection algorithm
99        require Whatpm::Charset::UniversalCharDet;
100        $charset = Whatpm::Charset::UniversalCharDet->detect_byte_string
101            (substr ($$bytes_s, 0, 1024));
102        $charset ||= 'windows-1252';
103        $s = \ (Encode::decode ($charset, $$bytes_s));
104        $self->{input_encoding} = $charset;
105        $self->{confident} = 0;
106      }
107    
108      $self->{change_encoding} = sub {
109        my $self = shift;
110        my $charset = lc shift;
111        ## TODO: if $charset is supported
112        ## TODO: normalize charset name
113    
114        ## "Change the encoding" algorithm:
115    
116        ## Step 1    
117        if ($charset eq 'utf-16') { ## ISSUE: UTF-16BE -> UTF-8? UTF-16LE -> UTF-8?
118          $charset = 'utf-8';
119        }
120    
121        ## Step 2
122        if (defined $self->{input_encoding} and
123            $self->{input_encoding} eq $charset) {
124          $self->{confident} = 1;
125          return;
126        }
127    
128        !!!parse-error (type => 'charset label detected:'.$self->{input_encoding}.
129            ':'.$charset, level => 'w');
130    
131        ## Step 3
132        # if (can) {
133          ## change the encoding on the fly.
134          #$self->{confident} = 1;
135          #return;
136        # }
137    
138        ## Step 4
139        throw Whatpm::HTML::RestartParser (charset => $charset);
140      }; # $self->{change_encoding}
141    
142      my @args = @_; shift @args; # $s
143      my $return;
144      try {
145        $return = $self->parse_char_string ($s, @args);  
146      } catch Whatpm::HTML::RestartParser with {
147        my $charset = shift->{charset};
148        $s = \ (Encode::decode ($charset, $$bytes_s));    
149        $self->{input_encoding} = $charset; ## TODO: normalize
150        $self->{confident} = 1;
151        $return = $self->parse_char_string ($s, @args);
152      };
153      return $return;
154    } # parse_byte_string
155    
156    ## NOTE: HTML5 spec says that the encoding layer MUST NOT strip BOM
157    ## and the HTML layer MUST ignore it.  However, we does strip BOM in
158    ## the encoding layer and the HTML layer does not ignore any U+FEFF,
159    ## because the core part of our HTML parser expects a string of character,
160    ## not a string of bytes or code units or anything which might contain a BOM.
161    ## Therefore, any parser interface that accepts a string of bytes,
162    ## such as |parse_byte_string| in this module, must ensure that it does
163    ## strip the BOM and never strip any ZWNBSP.
164    
165    *parse_char_string = \&parse_string;
166    
167  sub parse_string ($$$;$) {  sub parse_string ($$$;$) {
168    my $self = shift->new;    my $self = ref $_[0] ? shift : shift->new;
169    my $s = \$_[0];    my $s = ref $_[0] ? $_[0] : \($_[0]);
170    $self->{document} = $_[1];    $self->{document} = $_[1];
171      @{$self->{document}->child_nodes} = ();
172    
173    ## NOTE: |set_inner_html| copies most of this method's code    ## NOTE: |set_inner_html| copies most of this method's code
174    
175      $self->{confident} = 1 unless exists $self->{confident};
176      $self->{document}->input_encoding ($self->{input_encoding})
177          if defined $self->{input_encoding};
178    
179    my $i = 0;    my $i = 0;
180    my $line = 1;    my $line = 1;
181    my $column = 0;    my $column = 0;
# Line 147  sub new ($) { Line 232  sub new ($) {
232    $self->{parse_error} = sub {    $self->{parse_error} = sub {
233      #      #
234    };    };
235      $self->{change_encoding} = sub {
236        # if ($_[0] is a supported encoding) {
237        #   run "change the encoding" algorithm;
238        #   throw Whatpm::HTML::RestartParser (charset => $new_encoding);
239        # }
240      };
241      $self->{application_cache_selection} = sub {
242        #
243      };
244    return $self;    return $self;
245  } # new  } # new
246    
# Line 159  sub CDATA_CONTENT_MODEL () { CM_LIMITED_ Line 253  sub CDATA_CONTENT_MODEL () { CM_LIMITED_
253  sub RCDATA_CONTENT_MODEL () { CM_ENTITY | CM_LIMITED_MARKUP }  sub RCDATA_CONTENT_MODEL () { CM_ENTITY | CM_LIMITED_MARKUP }
254  sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }  sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }
255    
256    sub DATA_STATE () { 0 }
257    sub ENTITY_DATA_STATE () { 1 }
258    sub TAG_OPEN_STATE () { 2 }
259    sub CLOSE_TAG_OPEN_STATE () { 3 }
260    sub TAG_NAME_STATE () { 4 }
261    sub BEFORE_ATTRIBUTE_NAME_STATE () { 5 }
262    sub ATTRIBUTE_NAME_STATE () { 6 }
263    sub AFTER_ATTRIBUTE_NAME_STATE () { 7 }
264    sub BEFORE_ATTRIBUTE_VALUE_STATE () { 8 }
265    sub ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE () { 9 }
266    sub ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE () { 10 }
267    sub ATTRIBUTE_VALUE_UNQUOTED_STATE () { 11 }
268    sub ENTITY_IN_ATTRIBUTE_VALUE_STATE () { 12 }
269    sub MARKUP_DECLARATION_OPEN_STATE () { 13 }
270    sub COMMENT_START_STATE () { 14 }
271    sub COMMENT_START_DASH_STATE () { 15 }
272    sub COMMENT_STATE () { 16 }
273    sub COMMENT_END_STATE () { 17 }
274    sub COMMENT_END_DASH_STATE () { 18 }
275    sub BOGUS_COMMENT_STATE () { 19 }
276    sub DOCTYPE_STATE () { 20 }
277    sub BEFORE_DOCTYPE_NAME_STATE () { 21 }
278    sub DOCTYPE_NAME_STATE () { 22 }
279    sub AFTER_DOCTYPE_NAME_STATE () { 23 }
280    sub BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE () { 24 }
281    sub DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE () { 25 }
282    sub DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE () { 26 }
283    sub AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE () { 27 }
284    sub BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 28 }
285    sub DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE () { 29 }
286    sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE () { 30 }
287    sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }
288    sub BOGUS_DOCTYPE_STATE () { 32 }
289    
290    sub DOCTYPE_TOKEN () { 1 }
291    sub COMMENT_TOKEN () { 2 }
292    sub START_TAG_TOKEN () { 3 }
293    sub END_TAG_TOKEN () { 4 }
294    sub END_OF_FILE_TOKEN () { 5 }
295    sub CHARACTER_TOKEN () { 6 }
296    
297    sub AFTER_HTML_IMS () { 0b100 }
298    sub HEAD_IMS ()       { 0b1000 }
299    sub BODY_IMS ()       { 0b10000 }
300    sub BODY_TABLE_IMS () { 0b100000 }
301    sub TABLE_IMS ()      { 0b1000000 }
302    sub ROW_IMS ()        { 0b10000000 }
303    sub BODY_AFTER_IMS () { 0b100000000 }
304    sub FRAME_IMS ()      { 0b1000000000 }
305    
306    sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }
307    sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }
308    sub IN_HEAD_IM () { HEAD_IMS | 0b00 }
309    sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }
310    sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }
311    sub BEFORE_HEAD_IM () { HEAD_IMS | 0b11 }
312    sub IN_BODY_IM () { BODY_IMS }
313    sub IN_CELL_IM () { BODY_IMS | BODY_TABLE_IMS | 0b01 }
314    sub IN_CAPTION_IM () { BODY_IMS | BODY_TABLE_IMS | 0b10 }
315    sub IN_ROW_IM () { TABLE_IMS | ROW_IMS | 0b01 }
316    sub IN_TABLE_BODY_IM () { TABLE_IMS | ROW_IMS | 0b10 }
317    sub IN_TABLE_IM () { TABLE_IMS }
318    sub AFTER_BODY_IM () { BODY_AFTER_IMS }
319    sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }
320    sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }
321    sub IN_SELECT_IM () { 0b01 }
322    sub IN_COLUMN_GROUP_IM () { 0b10 }
323    
324  ## Implementations MUST act as if state machine in the spec  ## Implementations MUST act as if state machine in the spec
325    
326  sub _initialize_tokenizer ($) {  sub _initialize_tokenizer ($) {
327    my $self = shift;    my $self = shift;
328    $self->{state} = 'data'; # MUST    $self->{state} = DATA_STATE; # MUST
329    $self->{content_model} = PCDATA_CONTENT_MODEL; # be    $self->{content_model} = PCDATA_CONTENT_MODEL; # be
330    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE
331    undef $self->{current_attribute};    undef $self->{current_attribute};
# Line 177  sub _initialize_tokenizer ($) { Line 339  sub _initialize_tokenizer ($) {
339  } # _initialize_tokenizer  } # _initialize_tokenizer
340    
341  ## A token has:  ## A token has:
342  ##   ->{type} eq 'DOCTYPE', 'start tag', 'end tag', 'comment',  ##   ->{type} == DOCTYPE_TOKEN, START_TAG_TOKEN, END_TAG_TOKEN, COMMENT_TOKEN,
343  ##       'character', or 'end-of-file'  ##       CHARACTER_TOKEN, or END_OF_FILE_TOKEN
344  ##   ->{name} (DOCTYPE, start tag (tag name), end tag (tag name))  ##   ->{name} (DOCTYPE_TOKEN)
345  ##   ->{public_identifier} (DOCTYPE)  ##   ->{tag_name} (START_TAG_TOKEN, END_TAG_TOKEN)
346  ##   ->{system_identifier} (DOCTYPE)  ##   ->{public_identifier} (DOCTYPE_TOKEN)
347  ##   ->{correct} == 1 or 0 (DOCTYPE)  ##   ->{system_identifier} (DOCTYPE_TOKEN)
348  ##   ->{attributes} isa HASH (start tag, end tag)  ##   ->{correct} == 1 or 0 (DOCTYPE_TOKEN)
349  ##   ->{data} (comment, character)  ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)
350    ##        ->{name}
351    ##        ->{value}
352    ##        ->{has_reference} == 1 or 0
353    ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
354    
355  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
356    
# Line 194  sub _initialize_tokenizer ($) { Line 360  sub _initialize_tokenizer ($) {
360  ## has completed loading.  If one has, then it MUST be executed  ## has completed loading.  If one has, then it MUST be executed
361  ## and removed from the list.  ## and removed from the list.
362    
363    ## NOTE: HTML5 "Writing HTML documents" section, applied to
364    ## documents and not to user agents and conformance checkers,
365    ## contains some requirements that are not detected by the
366    ## parsing algorithm:
367    ## - Some requirements on character encoding declarations. ## TODO
368    ## - "Elements MUST NOT contain content that their content model disallows."
369    ##   ... Some are parse error, some are not (will be reported by c.c.).
370    ## - Polytheistic slash SHOULD NOT be used. (Applied only to atheists.) ## TODO
371    ## - Text (in elements, attributes, and comments) SHOULD NOT contain
372    ##   control characters other than space characters. ## TODO: (what is control character? C0, C1 and DEL?  Unicode control character?)
373    
374    ## TODO: HTML5 poses authors two SHOULD-level requirements that cannot
375    ## be detected by the HTML5 parsing algorithm:
376    ## - Text,
377    
378  sub _get_next_token ($) {  sub _get_next_token ($) {
379    my $self = shift;    my $self = shift;
380    if (@{$self->{token}}) {    if (@{$self->{token}}) {
# Line 201  sub _get_next_token ($) { Line 382  sub _get_next_token ($) {
382    }    }
383    
384    A: {    A: {
385      if ($self->{state} eq 'data') {      if ($self->{state} == DATA_STATE) {
386        if ($self->{next_input_character} == 0x0026) { # &        if ($self->{next_input_character} == 0x0026) { # &
387          if ($self->{content_model} & CM_ENTITY) { # PCDATA | RCDATA          if ($self->{content_model} & CM_ENTITY) { # PCDATA | RCDATA
388            $self->{state} = 'entity data';            $self->{state} = ENTITY_DATA_STATE;
389            !!!next-input-character;            !!!next-input-character;
390            redo A;            redo A;
391          } else {          } else {
# Line 226  sub _get_next_token ($) { Line 407  sub _get_next_token ($) {
407          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA
408              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA
409               not $self->{escape})) {               not $self->{escape})) {
410            $self->{state} = 'tag open';            $self->{state} = TAG_OPEN_STATE;
411            !!!next-input-character;            !!!next-input-character;
412            redo A;            redo A;
413          } else {          } else {
# Line 243  sub _get_next_token ($) { Line 424  sub _get_next_token ($) {
424                    
425          #          #
426        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
427          !!!emit ({type => 'end-of-file'});          !!!emit ({type => END_OF_FILE_TOKEN});
428          last A; ## TODO: ok?          last A; ## TODO: ok?
429        }        }
430        # Anything else        # Anything else
431        my $token = {type => 'character',        my $token = {type => CHARACTER_TOKEN,
432                     data => chr $self->{next_input_character}};                     data => chr $self->{next_input_character}};
433        ## Stay in the data state        ## Stay in the data state
434        !!!next-input-character;        !!!next-input-character;
# Line 255  sub _get_next_token ($) { Line 436  sub _get_next_token ($) {
436        !!!emit ($token);        !!!emit ($token);
437    
438        redo A;        redo A;
439      } elsif ($self->{state} eq 'entity data') {      } elsif ($self->{state} == ENTITY_DATA_STATE) {
440        ## (cannot happen in CDATA state)        ## (cannot happen in CDATA state)
441                
442        my $token = $self->_tokenize_attempt_to_consume_an_entity (0);        my $token = $self->_tokenize_attempt_to_consume_an_entity (0);
443    
444        $self->{state} = 'data';        $self->{state} = DATA_STATE;
445        # next-input-character is already done        # next-input-character is already done
446    
447        unless (defined $token) {        unless (defined $token) {
448          !!!emit ({type => 'character', data => '&'});          !!!emit ({type => CHARACTER_TOKEN, data => '&'});
449        } else {        } else {
450          !!!emit ($token);          !!!emit ($token);
451        }        }
452    
453        redo A;        redo A;
454      } elsif ($self->{state} eq 'tag open') {      } elsif ($self->{state} == TAG_OPEN_STATE) {
455        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
456          if ($self->{next_input_character} == 0x002F) { # /          if ($self->{next_input_character} == 0x002F) { # /
457            !!!next-input-character;            !!!next-input-character;
458            $self->{state} = 'close tag open';            $self->{state} = CLOSE_TAG_OPEN_STATE;
459            redo A;            redo A;
460          } else {          } else {
461            ## reconsume            ## reconsume
462            $self->{state} = 'data';            $self->{state} = DATA_STATE;
463    
464            !!!emit ({type => 'character', data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<'});
465    
466            redo A;            redo A;
467          }          }
468        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA
469          if ($self->{next_input_character} == 0x0021) { # !          if ($self->{next_input_character} == 0x0021) { # !
470            $self->{state} = 'markup declaration open';            $self->{state} = MARKUP_DECLARATION_OPEN_STATE;
471            !!!next-input-character;            !!!next-input-character;
472            redo A;            redo A;
473          } elsif ($self->{next_input_character} == 0x002F) { # /          } elsif ($self->{next_input_character} == 0x002F) { # /
474            $self->{state} = 'close tag open';            $self->{state} = CLOSE_TAG_OPEN_STATE;
475            !!!next-input-character;            !!!next-input-character;
476            redo A;            redo A;
477          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_input_character} and
478                   $self->{next_input_character} <= 0x005A) { # A..Z                   $self->{next_input_character} <= 0x005A) { # A..Z
479            $self->{current_token}            $self->{current_token}
480              = {type => 'start tag',              = {type => START_TAG_TOKEN,
481                 tag_name => chr ($self->{next_input_character} + 0x0020)};                 tag_name => chr ($self->{next_input_character} + 0x0020)};
482            $self->{state} = 'tag name';            $self->{state} = TAG_NAME_STATE;
483            !!!next-input-character;            !!!next-input-character;
484            redo A;            redo A;
485          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_input_character} and
486                   $self->{next_input_character} <= 0x007A) { # a..z                   $self->{next_input_character} <= 0x007A) { # a..z
487            $self->{current_token} = {type => 'start tag',            $self->{current_token} = {type => START_TAG_TOKEN,
488                              tag_name => chr ($self->{next_input_character})};                              tag_name => chr ($self->{next_input_character})};
489            $self->{state} = 'tag name';            $self->{state} = TAG_NAME_STATE;
490            !!!next-input-character;            !!!next-input-character;
491            redo A;            redo A;
492          } elsif ($self->{next_input_character} == 0x003E) { # >          } elsif ($self->{next_input_character} == 0x003E) { # >
493            !!!parse-error (type => 'empty start tag');            !!!parse-error (type => 'empty start tag');
494            $self->{state} = 'data';            $self->{state} = DATA_STATE;
495            !!!next-input-character;            !!!next-input-character;
496    
497            !!!emit ({type => 'character', data => '<>'});            !!!emit ({type => CHARACTER_TOKEN, data => '<>'});
498    
499            redo A;            redo A;
500          } elsif ($self->{next_input_character} == 0x003F) { # ?          } elsif ($self->{next_input_character} == 0x003F) { # ?
501            !!!parse-error (type => 'pio');            !!!parse-error (type => 'pio');
502            $self->{state} = 'bogus comment';            $self->{state} = BOGUS_COMMENT_STATE;
503            ## $self->{next_input_character} is intentionally left as is            ## $self->{next_input_character} is intentionally left as is
504            redo A;            redo A;
505          } else {          } else {
506            !!!parse-error (type => 'bare stago');            !!!parse-error (type => 'bare stago');
507            $self->{state} = 'data';            $self->{state} = DATA_STATE;
508            ## reconsume            ## reconsume
509    
510            !!!emit ({type => 'character', data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<'});
511    
512            redo A;            redo A;
513          }          }
514        } else {        } else {
515          die "$0: $self->{content_model} in tag open";          die "$0: $self->{content_model} in tag open";
516        }        }
517      } elsif ($self->{state} eq 'close tag open') {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
518        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
519          if (defined $self->{last_emitted_start_tag_name}) {          if (defined $self->{last_emitted_start_tag_name}) {
520            ## 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 529  sub _get_next_token ($) {
529              } else {              } else {
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    
534                !!!emit ({type => 'character', data => '</'});                !!!emit ({type => CHARACTER_TOKEN, data => '</'});
535        
536                redo A;                redo A;
537              }              }
# Line 367  sub _get_next_token ($) { Line 548  sub _get_next_token ($) {
548                    $self->{next_input_character} == -1) {                    $self->{next_input_character} == -1) {
549              $self->{next_input_character} = shift @next_char; # reconsume              $self->{next_input_character} = shift @next_char; # reconsume
550              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
551              $self->{state} = 'data';              $self->{state} = DATA_STATE;
552              !!!emit ({type => 'character', data => '</'});              !!!emit ({type => CHARACTER_TOKEN, data => '</'});
553              redo A;              redo A;
554            } else {            } else {
555              $self->{next_input_character} = shift @next_char;              $self->{next_input_character} = shift @next_char;
# Line 378  sub _get_next_token ($) { Line 559  sub _get_next_token ($) {
559          } else {          } else {
560            ## No start tag token has ever been emitted            ## No start tag token has ever been emitted
561            # next-input-character is already done            # next-input-character is already done
562            $self->{state} = 'data';            $self->{state} = DATA_STATE;
563            !!!emit ({type => 'character', data => '</'});            !!!emit ({type => CHARACTER_TOKEN, data => '</'});
564            redo A;            redo A;
565          }          }
566        }        }
567                
568        if (0x0041 <= $self->{next_input_character} and        if (0x0041 <= $self->{next_input_character} and
569            $self->{next_input_character} <= 0x005A) { # A..Z            $self->{next_input_character} <= 0x005A) { # A..Z
570          $self->{current_token} = {type => 'end tag',          $self->{current_token} = {type => END_TAG_TOKEN,
571                            tag_name => chr ($self->{next_input_character} + 0x0020)};                            tag_name => chr ($self->{next_input_character} + 0x0020)};
572          $self->{state} = 'tag name';          $self->{state} = TAG_NAME_STATE;
573          !!!next-input-character;          !!!next-input-character;
574          redo A;          redo A;
575        } elsif (0x0061 <= $self->{next_input_character} and        } elsif (0x0061 <= $self->{next_input_character} and
576                 $self->{next_input_character} <= 0x007A) { # a..z                 $self->{next_input_character} <= 0x007A) { # a..z
577          $self->{current_token} = {type => 'end tag',          $self->{current_token} = {type => END_TAG_TOKEN,
578                            tag_name => chr ($self->{next_input_character})};                            tag_name => chr ($self->{next_input_character})};
579          $self->{state} = 'tag name';          $self->{state} = TAG_NAME_STATE;
580          !!!next-input-character;          !!!next-input-character;
581          redo A;          redo A;
582        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
583          !!!parse-error (type => 'empty end tag');          !!!parse-error (type => 'empty end tag');
584          $self->{state} = 'data';          $self->{state} = DATA_STATE;
585          !!!next-input-character;          !!!next-input-character;
586          redo A;          redo A;
587        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
588          !!!parse-error (type => 'bare etago');          !!!parse-error (type => 'bare etago');
589          $self->{state} = 'data';          $self->{state} = DATA_STATE;
590          # reconsume          # reconsume
591    
592          !!!emit ({type => 'character', data => '</'});          !!!emit ({type => CHARACTER_TOKEN, data => '</'});
593    
594          redo A;          redo A;
595        } else {        } else {
596          !!!parse-error (type => 'bogus end tag');          !!!parse-error (type => 'bogus end tag');
597          $self->{state} = 'bogus comment';          $self->{state} = BOGUS_COMMENT_STATE;
598          ## $self->{next_input_character} is intentionally left as is          ## $self->{next_input_character} is intentionally left as is
599          redo A;          redo A;
600        }        }
601      } elsif ($self->{state} eq 'tag name') {      } elsif ($self->{state} == TAG_NAME_STATE) {
602        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
603            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
604            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
605            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
606            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
607          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
608          !!!next-input-character;          !!!next-input-character;
609          redo A;          redo A;
610        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
611          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
612            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
613                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
614            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
615          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
616            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
617            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
618              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 439  sub _get_next_token ($) { Line 620  sub _get_next_token ($) {
620          } else {          } else {
621            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
622          }          }
623          $self->{state} = 'data';          $self->{state} = DATA_STATE;
624          !!!next-input-character;          !!!next-input-character;
625    
626          !!!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 635  sub _get_next_token ($) {
635          redo A;          redo A;
636        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
637          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
638          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
639            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
640                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
641            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
642          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
643            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
644            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
645              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 466  sub _get_next_token ($) { Line 647  sub _get_next_token ($) {
647          } else {          } else {
648            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
649          }          }
650          $self->{state} = 'data';          $self->{state} = DATA_STATE;
651          # reconsume          # reconsume
652    
653          !!!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 656  sub _get_next_token ($) {
656        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_input_character} == 0x002F) { # /
657          !!!next-input-character;          !!!next-input-character;
658          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_input_character} == 0x003E and # >
659              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
660              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
661            # permitted slash            # permitted slash
662            #            #
663          } else {          } else {
664            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
665          }          }
666          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
667          # next-input-character is already done          # next-input-character is already done
668          redo A;          redo A;
669        } else {        } else {
# Line 492  sub _get_next_token ($) { Line 673  sub _get_next_token ($) {
673          !!!next-input-character;          !!!next-input-character;
674          redo A;          redo A;
675        }        }
676      } elsif ($self->{state} eq 'before attribute name') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_NAME_STATE) {
677        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
678            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
679            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
# Line 502  sub _get_next_token ($) { Line 683  sub _get_next_token ($) {
683          !!!next-input-character;          !!!next-input-character;
684          redo A;          redo A;
685        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
686          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
687            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
688                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
689            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
690          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
691            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
692            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
693              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 514  sub _get_next_token ($) { Line 695  sub _get_next_token ($) {
695          } else {          } else {
696            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
697          }          }
698          $self->{state} = 'data';          $self->{state} = DATA_STATE;
699          !!!next-input-character;          !!!next-input-character;
700    
701          !!!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 705  sub _get_next_token ($) {
705                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_input_character} <= 0x005A) { # A..Z
706          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),
707                                value => ''};                                value => ''};
708          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
709          !!!next-input-character;          !!!next-input-character;
710          redo A;          redo A;
711        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_input_character} == 0x002F) { # /
712          !!!next-input-character;          !!!next-input-character;
713          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_input_character} == 0x003E and # >
714              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
715              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
716            # permitted slash            # permitted slash
717            #            #
# Line 542  sub _get_next_token ($) { Line 723  sub _get_next_token ($) {
723          redo A;          redo A;
724        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
725          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
726          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
727            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
728                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
729            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
730          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
731            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
732            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
733              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 554  sub _get_next_token ($) { Line 735  sub _get_next_token ($) {
735          } else {          } else {
736            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
737          }          }
738          $self->{state} = 'data';          $self->{state} = DATA_STATE;
739          # reconsume          # reconsume
740    
741          !!!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 744  sub _get_next_token ($) {
744        } else {        } else {
745          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          $self->{current_attribute} = {name => chr ($self->{next_input_character}),
746                                value => ''};                                value => ''};
747          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
748          !!!next-input-character;          !!!next-input-character;
749          redo A;          redo A;
750        }        }
751      } elsif ($self->{state} eq 'attribute name') {      } elsif ($self->{state} == ATTRIBUTE_NAME_STATE) {
752        my $before_leave = sub {        my $before_leave = sub {
753          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
754              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
# Line 585  sub _get_next_token ($) { Line 766  sub _get_next_token ($) {
766            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
767            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
768          $before_leave->();          $before_leave->();
769          $self->{state} = 'after attribute name';          $self->{state} = AFTER_ATTRIBUTE_NAME_STATE;
770          !!!next-input-character;          !!!next-input-character;
771          redo A;          redo A;
772        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_input_character} == 0x003D) { # =
773          $before_leave->();          $before_leave->();
774          $self->{state} = 'before attribute value';          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
775          !!!next-input-character;          !!!next-input-character;
776          redo A;          redo A;
777        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
778          $before_leave->();          $before_leave->();
779          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
780            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
781                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
782            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
783          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
784            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
785            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
786              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 607  sub _get_next_token ($) { Line 788  sub _get_next_token ($) {
788          } else {          } else {
789            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
790          }          }
791          $self->{state} = 'data';          $self->{state} = DATA_STATE;
792          !!!next-input-character;          !!!next-input-character;
793    
794          !!!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 804  sub _get_next_token ($) {
804          $before_leave->();          $before_leave->();
805          !!!next-input-character;          !!!next-input-character;
806          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_input_character} == 0x003E and # >
807              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
808              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
809            # permitted slash            # permitted slash
810            #            #
811          } else {          } else {
812            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
813          }          }
814          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
815          # next-input-character is already done          # next-input-character is already done
816          redo A;          redo A;
817        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
818          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
819          $before_leave->();          $before_leave->();
820          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
821            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
822                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
823            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
824          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
825            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
826            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
827              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 648  sub _get_next_token ($) { Line 829  sub _get_next_token ($) {
829          } else {          } else {
830            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
831          }          }
832          $self->{state} = 'data';          $self->{state} = DATA_STATE;
833          # reconsume          # reconsume
834    
835          !!!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 841  sub _get_next_token ($) {
841          !!!next-input-character;          !!!next-input-character;
842          redo A;          redo A;
843        }        }
844      } elsif ($self->{state} eq 'after attribute name') {      } elsif ($self->{state} == AFTER_ATTRIBUTE_NAME_STATE) {
845        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
846            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
847            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
# Line 670  sub _get_next_token ($) { Line 851  sub _get_next_token ($) {
851          !!!next-input-character;          !!!next-input-character;
852          redo A;          redo A;
853        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_input_character} == 0x003D) { # =
854          $self->{state} = 'before attribute value';          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
855          !!!next-input-character;          !!!next-input-character;
856          redo A;          redo A;
857        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
858          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
859            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
860                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
861            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
862          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
863            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
864            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
865              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 686  sub _get_next_token ($) { Line 867  sub _get_next_token ($) {
867          } else {          } else {
868            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
869          }          }
870          $self->{state} = 'data';          $self->{state} = DATA_STATE;
871          !!!next-input-character;          !!!next-input-character;
872    
873          !!!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 877  sub _get_next_token ($) {
877                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_input_character} <= 0x005A) { # A..Z
878          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),
879                                value => ''};                                value => ''};
880          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
881          !!!next-input-character;          !!!next-input-character;
882          redo A;          redo A;
883        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_input_character} == 0x002F) { # /
884          !!!next-input-character;          !!!next-input-character;
885          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_input_character} == 0x003E and # >
886              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
887              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
888            # permitted slash            # permitted slash
889            #            #
# Line 710  sub _get_next_token ($) { Line 891  sub _get_next_token ($) {
891            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
892            ## TODO: Different error type for <aa / bb> than <aa/>            ## TODO: Different error type for <aa / bb> than <aa/>
893          }          }
894          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
895          # next-input-character is already done          # next-input-character is already done
896          redo A;          redo A;
897        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
898          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
899          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
900            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
901                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
902            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
903          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
904            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
905            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
906              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 727  sub _get_next_token ($) { Line 908  sub _get_next_token ($) {
908          } else {          } else {
909            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
910          }          }
911          $self->{state} = 'data';          $self->{state} = DATA_STATE;
912          # reconsume          # reconsume
913    
914          !!!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 917  sub _get_next_token ($) {
917        } else {        } else {
918          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          $self->{current_attribute} = {name => chr ($self->{next_input_character}),
919                                value => ''};                                value => ''};
920          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
921          !!!next-input-character;          !!!next-input-character;
922          redo A;                  redo A;        
923        }        }
924      } elsif ($self->{state} eq 'before attribute value') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_VALUE_STATE) {
925        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
926            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
927            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
# Line 750  sub _get_next_token ($) { Line 931  sub _get_next_token ($) {
931          !!!next-input-character;          !!!next-input-character;
932          redo A;          redo A;
933        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_input_character} == 0x0022) { # "
934          $self->{state} = 'attribute value (double-quoted)';          $self->{state} = ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE;
935          !!!next-input-character;          !!!next-input-character;
936          redo A;          redo A;
937        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_input_character} == 0x0026) { # &
938          $self->{state} = 'attribute value (unquoted)';          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
939          ## reconsume          ## reconsume
940          redo A;          redo A;
941        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_input_character} == 0x0027) { # '
942          $self->{state} = 'attribute value (single-quoted)';          $self->{state} = ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE;
943          !!!next-input-character;          !!!next-input-character;
944          redo A;          redo A;
945        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
946          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
947            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
948                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
949            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
950          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
951            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
952            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
953              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 774  sub _get_next_token ($) { Line 955  sub _get_next_token ($) {
955          } else {          } else {
956            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
957          }          }
958          $self->{state} = 'data';          $self->{state} = DATA_STATE;
959          !!!next-input-character;          !!!next-input-character;
960    
961          !!!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 963  sub _get_next_token ($) {
963          redo A;          redo A;
964        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
965          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
966          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
967            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
968                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
969            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
970          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
971            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
972            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
973              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 794  sub _get_next_token ($) { Line 975  sub _get_next_token ($) {
975          } else {          } else {
976            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
977          }          }
978          $self->{state} = 'data';          $self->{state} = DATA_STATE;
979          ## reconsume          ## reconsume
980    
981          !!!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 983  sub _get_next_token ($) {
983          redo A;          redo A;
984        } else {        } else {
985          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});
986          $self->{state} = 'attribute value (unquoted)';          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
987          !!!next-input-character;          !!!next-input-character;
988          redo A;          redo A;
989        }        }
990      } elsif ($self->{state} eq 'attribute value (double-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE) {
991        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_input_character} == 0x0022) { # "
992          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
993          !!!next-input-character;          !!!next-input-character;
994          redo A;          redo A;
995        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_input_character} == 0x0026) { # &
996          $self->{last_attribute_value_state} = 'attribute value (double-quoted)';          $self->{last_attribute_value_state} = $self->{state};
997          $self->{state} = 'entity in attribute value';          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
998          !!!next-input-character;          !!!next-input-character;
999          redo A;          redo A;
1000        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1001          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1002          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1003            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
1004                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
1005            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1006          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1007            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1008            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1009              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 830  sub _get_next_token ($) { Line 1011  sub _get_next_token ($) {
1011          } else {          } else {
1012            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1013          }          }
1014          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1015          ## reconsume          ## reconsume
1016    
1017          !!!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 1023  sub _get_next_token ($) {
1023          !!!next-input-character;          !!!next-input-character;
1024          redo A;          redo A;
1025        }        }
1026      } elsif ($self->{state} eq 'attribute value (single-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE) {
1027        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_input_character} == 0x0027) { # '
1028          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1029          !!!next-input-character;          !!!next-input-character;
1030          redo A;          redo A;
1031        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_input_character} == 0x0026) { # &
1032          $self->{last_attribute_value_state} = 'attribute value (single-quoted)';          $self->{last_attribute_value_state} = $self->{state};
1033          $self->{state} = 'entity in attribute value';          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1034          !!!next-input-character;          !!!next-input-character;
1035          redo A;          redo A;
1036        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1037          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1038          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1039            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
1040                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
1041            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1042          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1043            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1044            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1045              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 866  sub _get_next_token ($) { Line 1047  sub _get_next_token ($) {
1047          } else {          } else {
1048            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1049          }          }
1050          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1051          ## reconsume          ## reconsume
1052    
1053          !!!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 1059  sub _get_next_token ($) {
1059          !!!next-input-character;          !!!next-input-character;
1060          redo A;          redo A;
1061        }        }
1062      } elsif ($self->{state} eq 'attribute value (unquoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_UNQUOTED_STATE) {
1063        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1064            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1065            $self->{next_input_character} == 0x000B or # HT            $self->{next_input_character} == 0x000B or # HT
1066            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
1067            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
1068          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1069          !!!next-input-character;          !!!next-input-character;
1070          redo A;          redo A;
1071        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_input_character} == 0x0026) { # &
1072          $self->{last_attribute_value_state} = 'attribute value (unquoted)';          $self->{last_attribute_value_state} = $self->{state};
1073          $self->{state} = 'entity in attribute value';          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1074          !!!next-input-character;          !!!next-input-character;
1075          redo A;          redo A;
1076        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1077          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1078            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
1079                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
1080            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1081          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1082            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1083            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1084              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 905  sub _get_next_token ($) { Line 1086  sub _get_next_token ($) {
1086          } else {          } else {
1087            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1088          }          }
1089          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1090          !!!next-input-character;          !!!next-input-character;
1091    
1092          !!!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 1094  sub _get_next_token ($) {
1094          redo A;          redo A;
1095        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1096          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1097          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1098            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
1099                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
1100            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1101          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1102            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1103            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1104              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 925  sub _get_next_token ($) { Line 1106  sub _get_next_token ($) {
1106          } else {          } else {
1107            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1108          }          }
1109          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1110          ## reconsume          ## reconsume
1111    
1112          !!!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 1118  sub _get_next_token ($) {
1118          !!!next-input-character;          !!!next-input-character;
1119          redo A;          redo A;
1120        }        }
1121      } elsif ($self->{state} eq 'entity in attribute value') {      } elsif ($self->{state} == ENTITY_IN_ATTRIBUTE_VALUE_STATE) {
1122        my $token = $self->_tokenize_attempt_to_consume_an_entity (1);        my $token = $self->_tokenize_attempt_to_consume_an_entity (1);
1123    
1124        unless (defined $token) {        unless (defined $token) {
1125          $self->{current_attribute}->{value} .= '&';          $self->{current_attribute}->{value} .= '&';
1126        } else {        } else {
1127          $self->{current_attribute}->{value} .= $token->{data};          $self->{current_attribute}->{value} .= $token->{data};
1128            $self->{current_attribute}->{has_reference} = $token->{has_reference};
1129          ## ISSUE: spec says "append the returned character token to the current attribute's value"          ## ISSUE: spec says "append the returned character token to the current attribute's value"
1130        }        }
1131    
1132        $self->{state} = $self->{last_attribute_value_state};        $self->{state} = $self->{last_attribute_value_state};
1133        # next-input-character is already done        # next-input-character is already done
1134        redo A;        redo A;
1135      } elsif ($self->{state} eq 'bogus comment') {      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
1136        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1137                
1138        my $token = {type => 'comment', data => ''};        my $token = {type => COMMENT_TOKEN, data => ''};
1139    
1140        BC: {        BC: {
1141          if ($self->{next_input_character} == 0x003E) { # >          if ($self->{next_input_character} == 0x003E) { # >
1142            $self->{state} = 'data';            $self->{state} = DATA_STATE;
1143            !!!next-input-character;            !!!next-input-character;
1144    
1145            !!!emit ($token);            !!!emit ($token);
1146    
1147            redo A;            redo A;
1148          } elsif ($self->{next_input_character} == -1) {          } elsif ($self->{next_input_character} == -1) {
1149            $self->{state} = 'data';            $self->{state} = DATA_STATE;
1150            ## reconsume            ## reconsume
1151    
1152            !!!emit ($token);            !!!emit ($token);
# Line 976  sub _get_next_token ($) { Line 1158  sub _get_next_token ($) {
1158            redo BC;            redo BC;
1159          }          }
1160        } # BC        } # BC
1161      } elsif ($self->{state} eq 'markup declaration open') {      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
1162        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1163    
1164        my @next_char;        my @next_char;
# Line 986  sub _get_next_token ($) { Line 1168  sub _get_next_token ($) {
1168          !!!next-input-character;          !!!next-input-character;
1169          push @next_char, $self->{next_input_character};          push @next_char, $self->{next_input_character};
1170          if ($self->{next_input_character} == 0x002D) { # -          if ($self->{next_input_character} == 0x002D) { # -
1171            $self->{current_token} = {type => 'comment', data => ''};            $self->{current_token} = {type => COMMENT_TOKEN, data => ''};
1172            $self->{state} = 'comment start';            $self->{state} = COMMENT_START_STATE;
1173            !!!next-input-character;            !!!next-input-character;
1174            redo A;            redo A;
1175          }          }
# Line 1018  sub _get_next_token ($) { Line 1200  sub _get_next_token ($) {
1200                    if ($self->{next_input_character} == 0x0045 or # E                    if ($self->{next_input_character} == 0x0045 or # E
1201                        $self->{next_input_character} == 0x0065) { # e                        $self->{next_input_character} == 0x0065) { # e
1202                      ## ISSUE: What a stupid code this is!                      ## ISSUE: What a stupid code this is!
1203                      $self->{state} = 'DOCTYPE';                      $self->{state} = DOCTYPE_STATE;
1204                      !!!next-input-character;                      !!!next-input-character;
1205                      redo A;                      redo A;
1206                    }                    }
# Line 1032  sub _get_next_token ($) { Line 1214  sub _get_next_token ($) {
1214        !!!parse-error (type => 'bogus comment');        !!!parse-error (type => 'bogus comment');
1215        $self->{next_input_character} = shift @next_char;        $self->{next_input_character} = shift @next_char;
1216        !!!back-next-input-character (@next_char);        !!!back-next-input-character (@next_char);
1217        $self->{state} = 'bogus comment';        $self->{state} = BOGUS_COMMENT_STATE;
1218        redo A;        redo A;
1219                
1220        ## ISSUE: typos in spec: chacacters, is is a parse error        ## ISSUE: typos in spec: chacacters, is is a parse error
1221        ## 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?
1222      } elsif ($self->{state} eq 'comment start') {      } elsif ($self->{state} == COMMENT_START_STATE) {
1223        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_input_character} == 0x002D) { # -
1224          $self->{state} = 'comment start dash';          $self->{state} = COMMENT_START_DASH_STATE;
1225          !!!next-input-character;          !!!next-input-character;
1226          redo A;          redo A;
1227        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1228          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
1229          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1230          !!!next-input-character;          !!!next-input-character;
1231    
1232          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1052  sub _get_next_token ($) { Line 1234  sub _get_next_token ($) {
1234          redo A;          redo A;
1235        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1236          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1237          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1238          ## reconsume          ## reconsume
1239    
1240          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1061  sub _get_next_token ($) { Line 1243  sub _get_next_token ($) {
1243        } else {        } else {
1244          $self->{current_token}->{data} # comment          $self->{current_token}->{data} # comment
1245              .= chr ($self->{next_input_character});              .= chr ($self->{next_input_character});
1246          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
1247          !!!next-input-character;          !!!next-input-character;
1248          redo A;          redo A;
1249        }        }
1250      } elsif ($self->{state} eq 'comment start dash') {      } elsif ($self->{state} == COMMENT_START_DASH_STATE) {
1251        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_input_character} == 0x002D) { # -
1252          $self->{state} = 'comment end';          $self->{state} = COMMENT_END_STATE;
1253          !!!next-input-character;          !!!next-input-character;
1254          redo A;          redo A;
1255        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1256          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
1257          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1258          !!!next-input-character;          !!!next-input-character;
1259    
1260          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1080  sub _get_next_token ($) { Line 1262  sub _get_next_token ($) {
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 1089  sub _get_next_token ($) { Line 1271  sub _get_next_token ($) {
1271        } else {        } else {
1272          $self->{current_token}->{data} # comment          $self->{current_token}->{data} # comment
1273              .= '-' . chr ($self->{next_input_character});              .= '-' . chr ($self->{next_input_character});
1274          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
1275          !!!next-input-character;          !!!next-input-character;
1276          redo A;          redo A;
1277        }        }
1278      } elsif ($self->{state} eq 'comment') {      } elsif ($self->{state} == COMMENT_STATE) {
1279        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_input_character} == 0x002D) { # -
1280          $self->{state} = 'comment end dash';          $self->{state} = COMMENT_END_DASH_STATE;
1281          !!!next-input-character;          !!!next-input-character;
1282          redo A;          redo A;
1283        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1284          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1285          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1286          ## reconsume          ## reconsume
1287    
1288          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1112  sub _get_next_token ($) { Line 1294  sub _get_next_token ($) {
1294          !!!next-input-character;          !!!next-input-character;
1295          redo A;          redo A;
1296        }        }
1297      } elsif ($self->{state} eq 'comment end dash') {      } elsif ($self->{state} == COMMENT_END_DASH_STATE) {
1298        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_input_character} == 0x002D) { # -
1299          $self->{state} = 'comment end';          $self->{state} = COMMENT_END_STATE;
1300          !!!next-input-character;          !!!next-input-character;
1301          redo A;          redo A;
1302        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1303          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1304          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1305          ## reconsume          ## reconsume
1306    
1307          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1127  sub _get_next_token ($) { Line 1309  sub _get_next_token ($) {
1309          redo A;          redo A;
1310        } else {        } else {
1311          $self->{current_token}->{data} .= '-' . chr ($self->{next_input_character}); # comment          $self->{current_token}->{data} .= '-' . chr ($self->{next_input_character}); # comment
1312          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
1313          !!!next-input-character;          !!!next-input-character;
1314          redo A;          redo A;
1315        }        }
1316      } elsif ($self->{state} eq 'comment end') {      } elsif ($self->{state} == COMMENT_END_STATE) {
1317        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_input_character} == 0x003E) { # >
1318          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1319          !!!next-input-character;          !!!next-input-character;
1320    
1321          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1147  sub _get_next_token ($) { Line 1329  sub _get_next_token ($) {
1329          redo A;          redo A;
1330        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1331          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1332          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1333          ## reconsume          ## reconsume
1334    
1335          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1156  sub _get_next_token ($) { Line 1338  sub _get_next_token ($) {
1338        } else {        } else {
1339          !!!parse-error (type => 'dash in comment');          !!!parse-error (type => 'dash in comment');
1340          $self->{current_token}->{data} .= '--' . chr ($self->{next_input_character}); # comment          $self->{current_token}->{data} .= '--' . chr ($self->{next_input_character}); # comment
1341          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
1342          !!!next-input-character;          !!!next-input-character;
1343          redo A;          redo A;
1344        }        }
1345      } elsif ($self->{state} eq 'DOCTYPE') {      } elsif ($self->{state} == DOCTYPE_STATE) {
1346        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1347            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1348            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
1349            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
1350            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
1351          $self->{state} = 'before DOCTYPE name';          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
1352          !!!next-input-character;          !!!next-input-character;
1353          redo A;          redo A;
1354        } else {        } else {
1355          !!!parse-error (type => 'no space before DOCTYPE name');          !!!parse-error (type => 'no space before DOCTYPE name');
1356          $self->{state} = 'before DOCTYPE name';          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
1357          ## reconsume          ## reconsume
1358          redo A;          redo A;
1359        }        }
1360      } elsif ($self->{state} eq 'before DOCTYPE name') {      } elsif ($self->{state} == BEFORE_DOCTYPE_NAME_STATE) {
1361        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1362            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1363            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
# Line 1186  sub _get_next_token ($) { Line 1368  sub _get_next_token ($) {
1368          redo A;          redo A;
1369        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1370          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
1371          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1372          !!!next-input-character;          !!!next-input-character;
1373    
1374          !!!emit ({type => 'DOCTYPE'}); # incorrect          !!!emit ({type => DOCTYPE_TOKEN}); # incorrect
1375    
1376          redo A;          redo A;
1377        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1378          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
1379          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1380          ## reconsume          ## reconsume
1381    
1382          !!!emit ({type => 'DOCTYPE'}); # incorrect          !!!emit ({type => DOCTYPE_TOKEN}); # incorrect
1383    
1384          redo A;          redo A;
1385        } else {        } else {
1386          $self->{current_token}          $self->{current_token}
1387              = {type => 'DOCTYPE',              = {type => DOCTYPE_TOKEN,
1388                 name => chr ($self->{next_input_character}),                 name => chr ($self->{next_input_character}),
1389                 correct => 1};                 correct => 1};
1390  ## ISSUE: "Set the token's name name to the" in the spec  ## ISSUE: "Set the token's name name to the" in the spec
1391          $self->{state} = 'DOCTYPE name';          $self->{state} = DOCTYPE_NAME_STATE;
1392          !!!next-input-character;          !!!next-input-character;
1393          redo A;          redo A;
1394        }        }
1395      } elsif ($self->{state} eq 'DOCTYPE name') {      } elsif ($self->{state} == DOCTYPE_NAME_STATE) {
1396  ## ISSUE: Redundant "First," in the spec.  ## ISSUE: Redundant "First," in the spec.
1397        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1398            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1399            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
1400            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
1401            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
1402          $self->{state} = 'after DOCTYPE name';          $self->{state} = AFTER_DOCTYPE_NAME_STATE;
1403          !!!next-input-character;          !!!next-input-character;
1404          redo A;          redo A;
1405        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1406          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1407          !!!next-input-character;          !!!next-input-character;
1408    
1409          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
# Line 1229  sub _get_next_token ($) { Line 1411  sub _get_next_token ($) {
1411          redo A;          redo A;
1412        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1413          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1414          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1415          ## reconsume          ## reconsume
1416    
1417          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1243  sub _get_next_token ($) { Line 1425  sub _get_next_token ($) {
1425          !!!next-input-character;          !!!next-input-character;
1426          redo A;          redo A;
1427        }        }
1428      } elsif ($self->{state} eq 'after DOCTYPE name') {      } elsif ($self->{state} == AFTER_DOCTYPE_NAME_STATE) {
1429        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1430            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1431            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
# Line 1253  sub _get_next_token ($) { Line 1435  sub _get_next_token ($) {
1435          !!!next-input-character;          !!!next-input-character;
1436          redo A;          redo A;
1437        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1438          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1439          !!!next-input-character;          !!!next-input-character;
1440    
1441          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
# Line 1261  sub _get_next_token ($) { Line 1443  sub _get_next_token ($) {
1443          redo A;          redo A;
1444        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1445          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1446          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1447          ## reconsume          ## reconsume
1448    
1449          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1285  sub _get_next_token ($) { Line 1467  sub _get_next_token ($) {
1467                  !!!next-input-character;                  !!!next-input-character;
1468                  if ($self->{next_input_character} == 0x0043 or # C                  if ($self->{next_input_character} == 0x0043 or # C
1469                      $self->{next_input_character} == 0x0063) { # c                      $self->{next_input_character} == 0x0063) { # c
1470                    $self->{state} = 'before DOCTYPE public identifier';                    $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1471                    !!!next-input-character;                    !!!next-input-character;
1472                    redo A;                    redo A;
1473                  }                  }
# Line 1312  sub _get_next_token ($) { Line 1494  sub _get_next_token ($) {
1494                  !!!next-input-character;                  !!!next-input-character;
1495                  if ($self->{next_input_character} == 0x004D or # M                  if ($self->{next_input_character} == 0x004D or # M
1496                      $self->{next_input_character} == 0x006D) { # m                      $self->{next_input_character} == 0x006D) { # m
1497                    $self->{state} = 'before DOCTYPE system identifier';                    $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
1498                    !!!next-input-character;                    !!!next-input-character;
1499                    redo A;                    redo A;
1500                  }                  }
# Line 1328  sub _get_next_token ($) { Line 1510  sub _get_next_token ($) {
1510        }        }
1511    
1512        !!!parse-error (type => 'string after DOCTYPE name');        !!!parse-error (type => 'string after DOCTYPE name');
1513        $self->{state} = 'bogus DOCTYPE';        $self->{state} = BOGUS_DOCTYPE_STATE;
1514        # next-input-character is already done        # next-input-character is already done
1515        redo A;        redo A;
1516      } elsif ($self->{state} eq 'before DOCTYPE public identifier') {      } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
1517        if ({        if ({
1518              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1519              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
# Line 1341  sub _get_next_token ($) { Line 1523  sub _get_next_token ($) {
1523          redo A;          redo A;
1524        } elsif ($self->{next_input_character} eq 0x0022) { # "        } elsif ($self->{next_input_character} eq 0x0022) { # "
1525          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{current_token}->{public_identifier} = ''; # DOCTYPE
1526          $self->{state} = 'DOCTYPE public identifier (double-quoted)';          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;
1527          !!!next-input-character;          !!!next-input-character;
1528          redo A;          redo A;
1529        } elsif ($self->{next_input_character} eq 0x0027) { # '        } elsif ($self->{next_input_character} eq 0x0027) { # '
1530          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{current_token}->{public_identifier} = ''; # DOCTYPE
1531          $self->{state} = 'DOCTYPE public identifier (single-quoted)';          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;
1532          !!!next-input-character;          !!!next-input-character;
1533          redo A;          redo A;
1534        } elsif ($self->{next_input_character} eq 0x003E) { # >        } elsif ($self->{next_input_character} eq 0x003E) { # >
1535          !!!parse-error (type => 'no PUBLIC literal');          !!!parse-error (type => 'no PUBLIC literal');
1536    
1537          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1538          !!!next-input-character;          !!!next-input-character;
1539    
1540          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1362  sub _get_next_token ($) { Line 1544  sub _get_next_token ($) {
1544        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1545          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1546    
1547          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1548          ## reconsume          ## reconsume
1549    
1550          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1371  sub _get_next_token ($) { Line 1553  sub _get_next_token ($) {
1553          redo A;          redo A;
1554        } else {        } else {
1555          !!!parse-error (type => 'string after PUBLIC');          !!!parse-error (type => 'string after PUBLIC');
1556          $self->{state} = 'bogus DOCTYPE';          $self->{state} = BOGUS_DOCTYPE_STATE;
1557          !!!next-input-character;          !!!next-input-character;
1558          redo A;          redo A;
1559        }        }
1560      } elsif ($self->{state} eq 'DOCTYPE public identifier (double-quoted)') {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {
1561        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_input_character} == 0x0022) { # "
1562          $self->{state} = 'after DOCTYPE public identifier';          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1563          !!!next-input-character;          !!!next-input-character;
1564          redo A;          redo A;
1565          } elsif ($self->{next_input_character} == 0x003E) { # >
1566            !!!parse-error (type => 'unclosed PUBLIC literal');
1567    
1568            $self->{state} = DATA_STATE;
1569            !!!next-input-character;
1570    
1571            delete $self->{current_token}->{correct};
1572            !!!emit ($self->{current_token}); # DOCTYPE
1573    
1574            redo A;
1575        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1576          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
1577    
1578          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1579          ## reconsume          ## reconsume
1580    
1581          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1397  sub _get_next_token ($) { Line 1589  sub _get_next_token ($) {
1589          !!!next-input-character;          !!!next-input-character;
1590          redo A;          redo A;
1591        }        }
1592      } elsif ($self->{state} eq 'DOCTYPE public identifier (single-quoted)') {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {
1593        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_input_character} == 0x0027) { # '
1594          $self->{state} = 'after DOCTYPE public identifier';          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1595          !!!next-input-character;          !!!next-input-character;
1596          redo A;          redo A;
1597          } elsif ($self->{next_input_character} == 0x003E) { # >
1598            !!!parse-error (type => 'unclosed PUBLIC literal');
1599    
1600            $self->{state} = DATA_STATE;
1601            !!!next-input-character;
1602    
1603            delete $self->{current_token}->{correct};
1604            !!!emit ($self->{current_token}); # DOCTYPE
1605    
1606            redo A;
1607        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1608          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
1609    
1610          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1611          ## reconsume          ## reconsume
1612    
1613          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1419  sub _get_next_token ($) { Line 1621  sub _get_next_token ($) {
1621          !!!next-input-character;          !!!next-input-character;
1622          redo A;          redo A;
1623        }        }
1624      } elsif ($self->{state} eq 'after DOCTYPE public identifier') {      } elsif ($self->{state} == AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
1625        if ({        if ({
1626              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1627              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
# Line 1429  sub _get_next_token ($) { Line 1631  sub _get_next_token ($) {
1631          redo A;          redo A;
1632        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_input_character} == 0x0022) { # "
1633          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1634          $self->{state} = 'DOCTYPE system identifier (double-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
1635          !!!next-input-character;          !!!next-input-character;
1636          redo A;          redo A;
1637        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_input_character} == 0x0027) { # '
1638          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1639          $self->{state} = 'DOCTYPE system identifier (single-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
1640          !!!next-input-character;          !!!next-input-character;
1641          redo A;          redo A;
1642        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1643          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1644          !!!next-input-character;          !!!next-input-character;
1645    
1646          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
# Line 1447  sub _get_next_token ($) { Line 1649  sub _get_next_token ($) {
1649        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1650          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1651    
1652          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1653          ## reconsume          ## reconsume
1654    
1655          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1456  sub _get_next_token ($) { Line 1658  sub _get_next_token ($) {
1658          redo A;          redo A;
1659        } else {        } else {
1660          !!!parse-error (type => 'string after PUBLIC literal');          !!!parse-error (type => 'string after PUBLIC literal');
1661          $self->{state} = 'bogus DOCTYPE';          $self->{state} = BOGUS_DOCTYPE_STATE;
1662          !!!next-input-character;          !!!next-input-character;
1663          redo A;          redo A;
1664        }        }
1665      } elsif ($self->{state} eq 'before DOCTYPE system identifier') {      } elsif ($self->{state} == BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
1666        if ({        if ({
1667              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1668              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
# Line 1470  sub _get_next_token ($) { Line 1672  sub _get_next_token ($) {
1672          redo A;          redo A;
1673        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_input_character} == 0x0022) { # "
1674          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1675          $self->{state} = 'DOCTYPE system identifier (double-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
1676          !!!next-input-character;          !!!next-input-character;
1677          redo A;          redo A;
1678        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_input_character} == 0x0027) { # '
1679          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1680          $self->{state} = 'DOCTYPE system identifier (single-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
1681          !!!next-input-character;          !!!next-input-character;
1682          redo A;          redo A;
1683        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1684          !!!parse-error (type => 'no SYSTEM literal');          !!!parse-error (type => 'no SYSTEM literal');
1685          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1686          !!!next-input-character;          !!!next-input-character;
1687    
1688          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1490  sub _get_next_token ($) { Line 1692  sub _get_next_token ($) {
1692        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1693          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1694    
1695          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1696          ## reconsume          ## reconsume
1697    
1698          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1499  sub _get_next_token ($) { Line 1701  sub _get_next_token ($) {
1701          redo A;          redo A;
1702        } else {        } else {
1703          !!!parse-error (type => 'string after SYSTEM');          !!!parse-error (type => 'string after SYSTEM');
1704          $self->{state} = 'bogus DOCTYPE';          $self->{state} = BOGUS_DOCTYPE_STATE;
1705          !!!next-input-character;          !!!next-input-character;
1706          redo A;          redo A;
1707        }        }
1708      } elsif ($self->{state} eq 'DOCTYPE system identifier (double-quoted)') {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {
1709        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_input_character} == 0x0022) { # "
1710          $self->{state} = 'after DOCTYPE system identifier';          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
1711          !!!next-input-character;          !!!next-input-character;
1712          redo A;          redo A;
1713          } elsif ($self->{next_input_character} == 0x003E) { # >
1714            !!!parse-error (type => 'unclosed PUBLIC literal');
1715    
1716            $self->{state} = DATA_STATE;
1717            !!!next-input-character;
1718    
1719            delete $self->{current_token}->{correct};
1720            !!!emit ($self->{current_token}); # DOCTYPE
1721    
1722            redo A;
1723        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1724          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
1725    
1726          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1727          ## reconsume          ## reconsume
1728    
1729          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1525  sub _get_next_token ($) { Line 1737  sub _get_next_token ($) {
1737          !!!next-input-character;          !!!next-input-character;
1738          redo A;          redo A;
1739        }        }
1740      } elsif ($self->{state} eq 'DOCTYPE system identifier (single-quoted)') {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {
1741        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_input_character} == 0x0027) { # '
1742          $self->{state} = 'after DOCTYPE system identifier';          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
1743          !!!next-input-character;          !!!next-input-character;
1744          redo A;          redo A;
1745          } elsif ($self->{next_input_character} == 0x003E) { # >
1746            !!!parse-error (type => 'unclosed PUBLIC literal');
1747    
1748            $self->{state} = DATA_STATE;
1749            !!!next-input-character;
1750    
1751            delete $self->{current_token}->{correct};
1752            !!!emit ($self->{current_token}); # DOCTYPE
1753    
1754            redo A;
1755        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1756          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
1757    
1758          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1759          ## reconsume          ## reconsume
1760    
1761          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1547  sub _get_next_token ($) { Line 1769  sub _get_next_token ($) {
1769          !!!next-input-character;          !!!next-input-character;
1770          redo A;          redo A;
1771        }        }
1772      } elsif ($self->{state} eq 'after DOCTYPE system identifier') {      } elsif ($self->{state} == AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
1773        if ({        if ({
1774              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1775              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
# Line 1556  sub _get_next_token ($) { Line 1778  sub _get_next_token ($) {
1778          !!!next-input-character;          !!!next-input-character;
1779          redo A;          redo A;
1780        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1781          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1782          !!!next-input-character;          !!!next-input-character;
1783    
1784          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
# Line 1565  sub _get_next_token ($) { Line 1787  sub _get_next_token ($) {
1787        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1788          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1789    
1790          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1791          ## reconsume          ## reconsume
1792    
1793          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1574  sub _get_next_token ($) { Line 1796  sub _get_next_token ($) {
1796          redo A;          redo A;
1797        } else {        } else {
1798          !!!parse-error (type => 'string after SYSTEM literal');          !!!parse-error (type => 'string after SYSTEM literal');
1799          $self->{state} = 'bogus DOCTYPE';          $self->{state} = BOGUS_DOCTYPE_STATE;
1800          !!!next-input-character;          !!!next-input-character;
1801          redo A;          redo A;
1802        }        }
1803      } elsif ($self->{state} eq 'bogus DOCTYPE') {      } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {
1804        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_input_character} == 0x003E) { # >
1805          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1806          !!!next-input-character;          !!!next-input-character;
1807    
1808          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1589  sub _get_next_token ($) { Line 1811  sub _get_next_token ($) {
1811          redo A;          redo A;
1812        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1813          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1814          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1815          ## reconsume          ## reconsume
1816    
1817          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1670  sub _tokenize_attempt_to_consume_an_enti Line 1892  sub _tokenize_attempt_to_consume_an_enti
1892            $code = $c1_entity_char->{$code};            $code = $c1_entity_char->{$code};
1893          }          }
1894    
1895          return {type => 'character', data => chr $code};          return {type => CHARACTER_TOKEN, data => chr $code,
1896                    has_reference => 1};
1897        } # X        } # X
1898      } elsif (0x0030 <= $self->{next_input_character} and      } elsif (0x0030 <= $self->{next_input_character} and
1899               $self->{next_input_character} <= 0x0039) { # 0..9               $self->{next_input_character} <= 0x0039) { # 0..9
# Line 1705  sub _tokenize_attempt_to_consume_an_enti Line 1928  sub _tokenize_attempt_to_consume_an_enti
1928          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
1929        }        }
1930                
1931        return {type => 'character', data => chr $code};        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1};
1932      } else {      } else {
1933        !!!parse-error (type => 'bare nero');        !!!parse-error (type => 'bare nero');
1934        !!!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 1976  sub _tokenize_attempt_to_consume_an_enti
1976      }      }
1977            
1978      if ($match > 0) {      if ($match > 0) {
1979        return {type => 'character', data => $value};        return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};
1980      } elsif ($match < 0) {      } elsif ($match < 0) {
1981        !!!parse-error (type => 'no refc');        !!!parse-error (type => 'no refc');
1982        if ($in_attr and $match < -1) {        if ($in_attr and $match < -1) {
1983          return {type => 'character', data => '&'.$entity_name};          return {type => CHARACTER_TOKEN, data => '&'.$entity_name};
1984        } else {        } else {
1985          return {type => 'character', data => $value};          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};
1986        }        }
1987      } else {      } else {
1988        !!!parse-error (type => 'bare ero');        !!!parse-error (type => 'bare ero');
1989        ## NOTE: No characters are consumed in the spec.        ## NOTE: "No characters are consumed" in the spec.
1990        return {type => 'character', data => '&'.$value};        return {type => CHARACTER_TOKEN, data => '&'.$value};
1991      }      }
1992    } else {    } else {
1993      ## no characters are consumed      ## no characters are consumed
# Line 1806  sub _construct_tree ($) { Line 2029  sub _construct_tree ($) {
2029        
2030    !!!next-token;    !!!next-token;
2031    
2032    $self->{insertion_mode} = 'before head';    $self->{insertion_mode} = BEFORE_HEAD_IM;
2033    undef $self->{form_element};    undef $self->{form_element};
2034    undef $self->{head_element};    undef $self->{head_element};
2035    $self->{open_elements} = [];    $self->{open_elements} = [];
# Line 1820  sub _construct_tree ($) { Line 2043  sub _construct_tree ($) {
2043  sub _tree_construction_initial ($) {  sub _tree_construction_initial ($) {
2044    my $self = shift;    my $self = shift;
2045    INITIAL: {    INITIAL: {
2046      if ($token->{type} eq 'DOCTYPE') {      if ($token->{type} == DOCTYPE_TOKEN) {
2047        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"
2048        ## error, switch to a conformance checking mode for another        ## error, switch to a conformance checking mode for another
2049        ## language.        ## language.
# Line 1947  sub _tree_construction_initial ($) { Line 2170  sub _tree_construction_initial ($) {
2170        !!!next-token;        !!!next-token;
2171        return;        return;
2172      } elsif ({      } elsif ({
2173                'start tag' => 1,                START_TAG_TOKEN, 1,
2174                'end tag' => 1,                END_TAG_TOKEN, 1,
2175                'end-of-file' => 1,                END_OF_FILE_TOKEN, 1,
2176               }->{$token->{type}}) {               }->{$token->{type}}) {
2177        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE');
2178        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
2179        ## Go to the root element phase        ## Go to the root element phase
2180        ## reprocess        ## reprocess
2181        return;        return;
2182      } elsif ($token->{type} eq 'character') {      } elsif ($token->{type} == CHARACTER_TOKEN) {
2183        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
2184          ## Ignore the token          ## Ignore the token
2185    
# Line 1972  sub _tree_construction_initial ($) { Line 2195  sub _tree_construction_initial ($) {
2195        ## Go to the root element phase        ## Go to the root element phase
2196        ## reprocess        ## reprocess
2197        return;        return;
2198      } elsif ($token->{type} eq 'comment') {      } elsif ($token->{type} == COMMENT_TOKEN) {
2199        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
2200        $self->{document}->append_child ($comment);        $self->{document}->append_child ($comment);
2201                
# Line 1980  sub _tree_construction_initial ($) { Line 2203  sub _tree_construction_initial ($) {
2203        !!!next-token;        !!!next-token;
2204        redo INITIAL;        redo INITIAL;
2205      } else {      } else {
2206        die "$0: $token->{type}: Unknown token";        die "$0: $token->{type}: Unknown token type";
2207      }      }
2208    } # INITIAL    } # INITIAL
2209  } # _tree_construction_initial  } # _tree_construction_initial
# Line 1989  sub _tree_construction_root_element ($) Line 2212  sub _tree_construction_root_element ($)
2212    my $self = shift;    my $self = shift;
2213        
2214    B: {    B: {
2215        if ($token->{type} eq 'DOCTYPE') {        if ($token->{type} == DOCTYPE_TOKEN) {
2216          !!!parse-error (type => 'in html:#DOCTYPE');          !!!parse-error (type => 'in html:#DOCTYPE');
2217          ## Ignore the token          ## Ignore the token
2218          ## Stay in the phase          ## Stay in the phase
2219          !!!next-token;          !!!next-token;
2220          redo B;          redo B;
2221        } elsif ($token->{type} eq 'comment') {        } elsif ($token->{type} == COMMENT_TOKEN) {
2222          my $comment = $self->{document}->create_comment ($token->{data});          my $comment = $self->{document}->create_comment ($token->{data});
2223          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
2224          ## Stay in the phase          ## Stay in the phase
2225          !!!next-token;          !!!next-token;
2226          redo B;          redo B;
2227        } elsif ($token->{type} eq 'character') {        } elsif ($token->{type} == CHARACTER_TOKEN) {
2228          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
2229            ## Ignore the token.            ## Ignore the token.
2230    
# Line 2011  sub _tree_construction_root_element ($) Line 2234  sub _tree_construction_root_element ($)
2234              redo B;              redo B;
2235            }            }
2236          }          }
2237    
2238            $self->{application_cache_selection}->(undef);
2239    
2240            #
2241          } elsif ($token->{type} == START_TAG_TOKEN) {
2242            if ($token->{tag_name} eq 'html' and
2243                $token->{attributes}->{manifest}) {
2244              $self->{application_cache_selection}
2245                   ->($token->{attributes}->{manifest}->{value});
2246              ## ISSUE: No relative reference resolution?
2247            } else {
2248              $self->{application_cache_selection}->(undef);
2249            }
2250    
2251            ## ISSUE: There is an issue in the spec
2252          #          #
2253        } elsif ({        } elsif ({
2254                  'start tag' => 1,                  END_TAG_TOKEN, 1,
2255                  'end tag' => 1,                  END_OF_FILE_TOKEN, 1,
                 'end-of-file' => 1,  
2256                 }->{$token->{type}}) {                 }->{$token->{type}}) {
2257            $self->{application_cache_selection}->(undef);
2258    
2259          ## ISSUE: There is an issue in the spec          ## ISSUE: There is an issue in the spec
2260          #          #
2261        } else {        } else {
2262          die "$0: $token->{type}: Unknown token";          die "$0: $token->{type}: Unknown token type";
2263        }        }
2264    
2265        my $root_element; !!!create-element ($root_element, 'html');        my $root_element; !!!create-element ($root_element, 'html');
2266        $self->{document}->append_child ($root_element);        $self->{document}->append_child ($root_element);
2267        push @{$self->{open_elements}}, [$root_element, 'html'];        push @{$self->{open_elements}}, [$root_element, 'html'];
# Line 2062  sub _reset_insertion_mode ($) { Line 2302  sub _reset_insertion_mode ($) {
2302            
2303        ## Step 4..13        ## Step 4..13
2304        my $new_mode = {        my $new_mode = {
2305                        select => 'in select',                        select => IN_SELECT_IM,
2306                        td => 'in cell',                        td => IN_CELL_IM,
2307                        th => 'in cell',                        th => IN_CELL_IM,
2308                        tr => 'in row',                        tr => IN_ROW_IM,
2309                        tbody => 'in table body',                        tbody => IN_TABLE_BODY_IM,
2310                        thead => 'in table head',                        thead => IN_TABLE_BODY_IM,
2311                        tfoot => 'in table foot',                        tfoot => IN_TABLE_BODY_IM,
2312                        caption => 'in caption',                        caption => IN_CAPTION_IM,
2313                        colgroup => 'in column group',                        colgroup => IN_COLUMN_GROUP_IM,
2314                        table => 'in table',                        table => IN_TABLE_IM,
2315                        head => 'in body', # not in head!                        head => IN_BODY_IM, # not in head!
2316                        body => 'in body',                        body => IN_BODY_IM,
2317                        frameset => 'in frameset',                        frameset => IN_FRAMESET_IM,
2318                       }->{$node->[1]};                       }->{$node->[1]};
2319        $self->{insertion_mode} = $new_mode and return if defined $new_mode;        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
2320                
2321        ## Step 14        ## Step 14
2322        if ($node->[1] eq 'html') {        if ($node->[1] eq 'html') {
2323          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
2324            $self->{insertion_mode} = 'before head';            $self->{insertion_mode} = BEFORE_HEAD_IM;
2325          } else {          } else {
2326            $self->{insertion_mode} = 'after head';            $self->{insertion_mode} = AFTER_HEAD_IM;
2327          }          }
2328          return;          return;
2329        }        }
2330                
2331        ## Step 15        ## Step 15
2332        $self->{insertion_mode} = 'in body' and return if $last;        $self->{insertion_mode} = IN_BODY_IM and return if $last;
2333                
2334        ## Step 16        ## Step 16
2335        $i--;        $i--;
# Line 2103  sub _reset_insertion_mode ($) { Line 2343  sub _reset_insertion_mode ($) {
2343  sub _tree_construction_main ($) {  sub _tree_construction_main ($) {
2344    my $self = shift;    my $self = shift;
2345    
   my $previous_insertion_mode;  
   
2346    my $active_formatting_elements = [];    my $active_formatting_elements = [];
2347    
2348    my $reconstruct_active_formatting_elements = sub { # MUST    my $reconstruct_active_formatting_elements = sub { # MUST
# Line 2205  sub _tree_construction_main ($) { Line 2443  sub _tree_construction_main ($) {
2443      ## Step 4      ## Step 4
2444      my $text = '';      my $text = '';
2445      !!!next-token;      !!!next-token;
2446      while ($token->{type} eq 'character') { # or until stop tokenizing      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
2447        $text .= $token->{data};        $text .= $token->{data};
2448        !!!next-token;        !!!next-token;
2449      }      }
# Line 2220  sub _tree_construction_main ($) { Line 2458  sub _tree_construction_main ($) {
2458      $self->{content_model} = PCDATA_CONTENT_MODEL;      $self->{content_model} = PCDATA_CONTENT_MODEL;
2459    
2460      ## Step 7      ## Step 7
2461      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) {
2462        ## Ignore the token        ## Ignore the token
2463      } elsif ($content_model_flag == CDATA_CONTENT_MODEL) {      } elsif ($content_model_flag == CDATA_CONTENT_MODEL) {
2464        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!parse-error (type => 'in CDATA:#'.$token->{type});
# Line 2243  sub _tree_construction_main ($) { Line 2481  sub _tree_construction_main ($) {
2481            
2482      my $text = '';      my $text = '';
2483      !!!next-token;      !!!next-token;
2484      while ($token->{type} eq 'character') {      while ($token->{type} == CHARACTER_TOKEN) {
2485        $text .= $token->{data};        $text .= $token->{data};
2486        !!!next-token;        !!!next-token;
2487      } # 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 2491  sub _tree_construction_main ($) {
2491                                
2492      $self->{content_model} = PCDATA_CONTENT_MODEL;      $self->{content_model} = PCDATA_CONTENT_MODEL;
2493    
2494      if ($token->{type} eq 'end tag' and      if ($token->{type} == END_TAG_TOKEN and
2495          $token->{tag_name} eq 'script') {          $token->{tag_name} eq 'script') {
2496        ## Ignore the token        ## Ignore the token
2497      } else {      } else {
# Line 2496  sub _tree_construction_main ($) { Line 2734  sub _tree_construction_main ($) {
2734                         }                         }
2735    }; # $insert_to_foster    }; # $insert_to_foster
2736    
2737    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  
2738    
2739    B: {    B: {
2740      if ($token->{type} eq 'DOCTYPE') {      if ($token->{type} == DOCTYPE_TOKEN) {
2741        !!!parse-error (type => 'DOCTYPE in the middle');        !!!parse-error (type => 'DOCTYPE in the middle');
2742        ## Ignore the token        ## Ignore the token
2743        ## Stay in the phase        ## Stay in the phase
2744        !!!next-token;        !!!next-token;
2745        redo B;        redo B;
2746      } elsif ($token->{type} eq 'end-of-file') {      } elsif ($token->{type} == END_OF_FILE_TOKEN) {
2747        if ($token->{insertion_mode} ne 'trailing end') {        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
2748            #
2749          } else {
2750          ## Generate implied end tags          ## Generate implied end tags
2751          if ({          if ({
2752               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,
2753               tbody => 1, tfoot=> 1, thead => 1,               tbody => 1, tfoot=> 1, thead => 1,
2754              }->{$self->{open_elements}->[-1]->[1]}) {              }->{$self->{open_elements}->[-1]->[1]}) {
2755            !!!back-token;            !!!back-token;
2756            $token = {type => 'end tag', tag_name => $self->{open_elements}->[-1]->[1]};            $token = {type => END_TAG_TOKEN, tag_name => $self->{open_elements}->[-1]->[1]};
2757            redo B;            redo B;
2758          }          }
2759                    
# Line 3375  sub _tree_construction_main ($) { Line 2771  sub _tree_construction_main ($) {
2771    
2772        ## Stop parsing        ## Stop parsing
2773        last B;        last B;
2774      } elsif ($token->{type} eq 'start tag' and      } elsif ($token->{type} == START_TAG_TOKEN and
2775               $token->{tag_name} eq 'html') {               $token->{tag_name} eq 'html') {
2776        if ($self->{insertion_mode} eq 'trailing end') {        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
2777          ## Turn into the main phase          ## Turn into the main phase
2778          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html:html');
2779          $self->{insertion_mode} = $previous_insertion_mode;          $self->{insertion_mode} = AFTER_BODY_IM;
2780          } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
2781            ## Turn into the main phase
2782            !!!parse-error (type => 'after html:html');
2783            $self->{insertion_mode} = AFTER_FRAMESET_IM;
2784        }        }
2785    
2786  ## ISSUE: "aa<html>" is not a parse error.  ## ISSUE: "aa<html>" is not a parse error.
# Line 3398  sub _tree_construction_main ($) { Line 2798  sub _tree_construction_main ($) {
2798        }        }
2799        !!!next-token;        !!!next-token;
2800        redo B;        redo B;
2801      } elsif ($token->{type} eq 'comment') {      } elsif ($token->{type} == COMMENT_TOKEN) {
2802        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
2803        if ($self->{insertion_mode} eq 'trailing end') {        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
2804          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
2805        } elsif ($self->{insertion_mode} eq 'after body') {        } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {
2806          $self->{open_elements}->[0]->[0]->append_child ($comment);          $self->{open_elements}->[0]->[0]->append_child ($comment);
2807        } else {        } else {
2808          $self->{open_elements}->[-1]->[0]->append_child ($comment);          $self->{open_elements}->[-1]->[0]->append_child ($comment);
2809        }        }
2810        !!!next-token;        !!!next-token;
2811        redo B;        redo B;
2812      } elsif ($self->{insertion_mode} eq 'before head') {      } elsif ($self->{insertion_mode} & HEAD_IMS) {
2813            if ($token->{type} eq 'character') {        if ($token->{type} == CHARACTER_TOKEN) {
2814              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
2815                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
2816                unless (length $token->{data}) {            unless (length $token->{data}) {
2817                  !!!next-token;              !!!next-token;
2818                  redo B;              redo B;
2819                }            }
2820              }          }
2821              ## As if <head>  
2822              !!!create-element ($self->{head_element}, 'head');          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2823              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});            ## As if <head>
2824              push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            !!!create-element ($self->{head_element}, 'head');
2825              $self->{insertion_mode} = 'in head';            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2826              push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2827    
2828              ## Reprocess in the "in head" insertion mode...
2829              pop @{$self->{open_elements}};
2830    
2831              ## Reprocess in the "after head" insertion mode...
2832            } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2833              ## As if </noscript>
2834              pop @{$self->{open_elements}};
2835              !!!parse-error (type => 'in noscript:#character');
2836              
2837              ## Reprocess in the "in head" insertion mode...
2838              ## As if </head>
2839              pop @{$self->{open_elements}};
2840    
2841              ## Reprocess in the "after head" insertion mode...
2842            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2843              pop @{$self->{open_elements}};
2844    
2845              ## Reprocess in the "after head" insertion mode...
2846            }
2847    
2848                ## "after head" insertion mode
2849                ## As if <body>
2850                !!!insert-element ('body');
2851                $self->{insertion_mode} = IN_BODY_IM;
2852              ## reprocess              ## reprocess
2853              redo B;              redo B;
2854            } 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';  
2855              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
2856                !!!next-token;                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2857              #} elsif ({                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});
2858              #          base => 1, link => 1, meta => 1,                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2859              #          script => 1, style => 1, title => 1,                  push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];
2860              #         }->{$token->{tag_name}}) {                  $self->{insertion_mode} = IN_HEAD_IM;
2861              #  ## reprocess                  !!!next-token;
2862              } else {                  redo B;
2863                ## reprocess                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
2864              }                  #
2865              redo B;                } else {
2866            } elsif ($token->{type} eq 'end tag') {                  !!!parse-error (type => 'in head:head'); # or in head noscript
2867              if ({                  ## Ignore the token
2868                   head => 1, body => 1, html => 1,                  !!!next-token;
2869                   p => 1, br => 1,                  redo B;
2870                  }->{$token->{tag_name}}) {                }
2871                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2872                ## As if <head>                ## As if <head>
2873                !!!create-element ($self->{head_element}, 'head');                !!!create-element ($self->{head_element}, 'head');
2874                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2875                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2876                $self->{insertion_mode} = 'in head';  
2877                ## reprocess                $self->{insertion_mode} = IN_HEAD_IM;
2878                redo B;                ## Reprocess in the "in head" insertion mode...
             } else {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token ## ISSUE: An issue in the spec.  
               !!!next-token;  
               redo B;  
2879              }              }
2880            } else {  
2881              die "$0: $token->{type}: Unknown type";              if ($token->{tag_name} eq 'base') {
2882            }                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2883          } elsif ($self->{insertion_mode} eq 'in head' or                  ## As if </noscript>
2884                   $self->{insertion_mode} eq 'in head noscript' or                  pop @{$self->{open_elements}};
2885                   $self->{insertion_mode} eq 'after head') {                  !!!parse-error (type => 'in noscript:base');
2886            if ($token->{type} eq 'character') {                
2887              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {                  $self->{insertion_mode} = IN_HEAD_IM;
2888                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                  ## Reprocess in the "in head" insertion mode...
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
2889                }                }
2890              }  
               
             #  
           } elsif ($token->{type} eq 'start tag') {  
             if ({base => ($self->{insertion_mode} eq 'in head' or  
                           $self->{insertion_mode} eq 'after head'),  
                  link => 1}->{$token->{tag_name}}) {  
2891                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
2892                if ($self->{insertion_mode} eq 'after head') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2893                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
2894                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2895                }                }
2896                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
2897                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
2898                pop @{$self->{open_elements}}                pop @{$self->{open_elements}}
2899                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
2900                !!!next-token;                !!!next-token;
2901                redo B;                redo B;
2902              } elsif ($token->{tag_name} eq 'meta') {              } elsif ($token->{tag_name} eq 'link') {
2903                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
2904                if ($self->{insertion_mode} eq 'after head') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2905                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
2906                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2907                }                }
2908                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
2909                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
2910                  pop @{$self->{open_elements}}
2911                      if $self->{insertion_mode} == AFTER_HEAD_IM;
2912                  !!!next-token;
2913                  redo B;
2914                } elsif ($token->{tag_name} eq 'meta') {
2915                  ## NOTE: There is a "as if in head" code clone.
2916                  if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2917                    !!!parse-error (type => 'after head:'.$token->{tag_name});
2918                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2919                  }
2920                  !!!insert-element ($token->{tag_name}, $token->{attributes});
2921                  my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
2922    
2923                unless ($self->{confident}) {                unless ($self->{confident}) {
                 my $charset;  
2924                  if ($token->{attributes}->{charset}) { ## TODO: And if supported                  if ($token->{attributes}->{charset}) { ## TODO: And if supported
2925                    $charset = $token->{attributes}->{charset}->{value};                    $self->{change_encoding}
2926                  }                        ->($self, $token->{attributes}->{charset}->{value});
2927                  if ($token->{attributes}->{'http-equiv'}) {                    
2928                      $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
2929                          ->set_user_data (manakai_has_reference =>
2930                                               $token->{attributes}->{charset}
2931                                                   ->{has_reference});
2932                    } elsif ($token->{attributes}->{content}) {
2933                    ## 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.
2934                    if ($token->{attributes}->{'http-equiv'}->{value}                    if ($token->{attributes}->{content}->{value}
2935                        =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=                        =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
2936                              [\x09-\x0D\x20]*=
2937                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
2938                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
2939                      $charset = defined $1 ? $1 : defined $2 ? $2 : $3;                      $self->{change_encoding}
2940                    } ## TODO: And if supported                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);
2941                        $meta_el->[0]->get_attribute_node_ns (undef, 'content')
2942                            ->set_user_data (manakai_has_reference =>
2943                                                 $token->{attributes}->{content}
2944                                                       ->{has_reference});
2945                      }
2946                    }
2947                  } else {
2948                    if ($token->{attributes}->{charset}) {
2949                      $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
2950                          ->set_user_data (manakai_has_reference =>
2951                                               $token->{attributes}->{charset}
2952                                                   ->{has_reference});
2953                    }
2954                    if ($token->{attributes}->{content}) {
2955                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')
2956                          ->set_user_data (manakai_has_reference =>
2957                                               $token->{attributes}->{content}
2958                                                   ->{has_reference});
2959                  }                  }
                 ## TODO: Change the encoding  
2960                }                }
2961    
               ## TODO: Extracting |charset| from |meta|.  
2962                pop @{$self->{open_elements}}                pop @{$self->{open_elements}}
2963                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
2964                !!!next-token;                !!!next-token;
2965                redo B;                redo B;
2966              } elsif ($token->{tag_name} eq 'title' and              } elsif ($token->{tag_name} eq 'title') {
2967                       $self->{insertion_mode} eq 'in head') {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2968                ## NOTE: There is a "as if in head" code clone.                  ## As if </noscript>
2969                if ($self->{insertion_mode} eq 'after head') {                  pop @{$self->{open_elements}};
2970                    !!!parse-error (type => 'in noscript:title');
2971                  
2972                    $self->{insertion_mode} = IN_HEAD_IM;
2973                    ## Reprocess in the "in head" insertion mode...
2974                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
2975                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
2976                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2977                }                }
2978    
2979                  ## NOTE: There is a "as if in head" code clone.
2980                my $parent = defined $self->{head_element} ? $self->{head_element}                my $parent = defined $self->{head_element} ? $self->{head_element}
2981                    : $self->{open_elements}->[-1]->[0];                    : $self->{open_elements}->[-1]->[0];
2982                $parse_rcdata->(RCDATA_CONTENT_MODEL,                $parse_rcdata->(RCDATA_CONTENT_MODEL,
2983                                sub { $parent->append_child ($_[0]) });                                sub { $parent->append_child ($_[0]) });
2984                pop @{$self->{open_elements}}                pop @{$self->{open_elements}}
2985                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
2986                redo B;                redo B;
2987              } elsif ($token->{tag_name} eq 'style') {              } elsif ($token->{tag_name} eq 'style') {
2988                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
2989                ## insertion mode 'in head')                ## insertion mode IN_HEAD_IM)
2990                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
2991                if ($self->{insertion_mode} eq 'after head') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2992                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
2993                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2994                }                }
2995                $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);                $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);
2996                pop @{$self->{open_elements}}                pop @{$self->{open_elements}}
2997                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
2998                redo B;                redo B;
2999              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
3000                if ($self->{insertion_mode} eq 'in head') {                if ($self->{insertion_mode} == IN_HEAD_IM) {
3001                  ## NOTE: and scripting is disalbed                  ## NOTE: and scripting is disalbed
3002                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes});
3003                  $self->{insertion_mode} = 'in head noscript';                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
3004                  !!!next-token;                  !!!next-token;
3005                  redo B;                  redo B;
3006                } elsif ($self->{insertion_mode} eq 'in head noscript') {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3007                  !!!parse-error (type => 'in noscript:noscript');                  !!!parse-error (type => 'in noscript:noscript');
3008                  ## Ignore the token                  ## Ignore the token
3009                  !!!next-token;                  !!!next-token;
# Line 3563  sub _tree_construction_main ($) { Line 3011  sub _tree_construction_main ($) {
3011                } else {                } else {
3012                  #                  #
3013                }                }
3014              } elsif ($token->{tag_name} eq 'head' and              } elsif ($token->{tag_name} eq 'script') {
3015                       $self->{insertion_mode} ne 'after head') {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3016                !!!parse-error (type => 'in head:head'); # or in head noscript                  ## As if </noscript>
3017                ## Ignore the token                  pop @{$self->{open_elements}};
3018                !!!next-token;                  !!!parse-error (type => 'in noscript:script');
3019                redo B;                
3020              } elsif ($self->{insertion_mode} ne 'in head noscript' and                  $self->{insertion_mode} = IN_HEAD_IM;
3021                       $token->{tag_name} eq 'script') {                  ## Reprocess in the "in head" insertion mode...
3022                if ($self->{insertion_mode} eq 'after head') {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
3023                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
3024                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3025                }                }
3026    
3027                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3028                $script_start_tag->($insert_to_current);                $script_start_tag->($insert_to_current);
3029                pop @{$self->{open_elements}}                pop @{$self->{open_elements}}
3030                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
               redo B;  
             } elsif ($self->{insertion_mode} eq 'after head' and  
                      $token->{tag_name} eq 'body') {  
               !!!insert-element ('body', $token->{attributes});  
               $self->{insertion_mode} = 'in body';  
               !!!next-token;  
3031                redo B;                redo B;
3032              } elsif ($self->{insertion_mode} eq 'after head' and              } elsif ($token->{tag_name} eq 'body' or
3033                       $token->{tag_name} eq 'frameset') {                       $token->{tag_name} eq 'frameset') {
3034                !!!insert-element ('frameset', $token->{attributes});                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3035                $self->{insertion_mode} = 'in frameset';                  ## As if </noscript>
3036                    pop @{$self->{open_elements}};
3037                    !!!parse-error (type => 'in noscript:'.$token->{tag_name});
3038                    
3039                    ## Reprocess in the "in head" insertion mode...
3040                    ## As if </head>
3041                    pop @{$self->{open_elements}};
3042                    
3043                    ## Reprocess in the "after head" insertion mode...
3044                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3045                    pop @{$self->{open_elements}};
3046                    
3047                    ## Reprocess in the "after head" insertion mode...
3048                  }
3049    
3050                  ## "after head" insertion mode
3051                  !!!insert-element ($token->{tag_name}, $token->{attributes});
3052                  if ($token->{tag_name} eq 'body') {
3053                    $self->{insertion_mode} = IN_BODY_IM;
3054                  } elsif ($token->{tag_name} eq 'frameset') {
3055                    $self->{insertion_mode} = IN_FRAMESET_IM;
3056                  } else {
3057                    die "$0: tag name: $self->{tag_name}";
3058                  }
3059                !!!next-token;                !!!next-token;
3060                redo B;                redo B;
3061              } else {              } else {
3062                #                #
3063              }              }
3064            } elsif ($token->{type} eq 'end tag') {  
3065              if ($self->{insertion_mode} eq 'in head' and              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3066                  $token->{tag_name} eq 'head') {                ## As if </noscript>
3067                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3068                $self->{insertion_mode} = 'after head';                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});
3069                !!!next-token;                
3070                redo B;                ## Reprocess in the "in head" insertion mode...
3071              } elsif ($self->{insertion_mode} eq 'in head noscript' and                ## As if </head>
                 $token->{tag_name} eq 'noscript') {  
3072                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3073                $self->{insertion_mode} = 'in head';  
3074                !!!next-token;                ## Reprocess in the "after head" insertion mode...
3075                redo B;              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3076              } elsif ($self->{insertion_mode} eq 'in head' and                ## As if </head>
3077                       {                pop @{$self->{open_elements}};
3078    
3079                  ## Reprocess in the "after head" insertion mode...
3080                }
3081    
3082                ## "after head" insertion mode
3083                ## As if <body>
3084                !!!insert-element ('body');
3085                $self->{insertion_mode} = IN_BODY_IM;
3086                ## reprocess
3087                redo B;
3088              } elsif ($token->{type} == END_TAG_TOKEN) {
3089                if ($token->{tag_name} eq 'head') {
3090                  if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3091                    ## As if <head>
3092                    !!!create-element ($self->{head_element}, 'head');
3093                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3094                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3095    
3096                    ## Reprocess in the "in head" insertion mode...
3097                    pop @{$self->{open_elements}};
3098                    $self->{insertion_mode} = AFTER_HEAD_IM;
3099                    !!!next-token;
3100                    redo B;
3101                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3102                    ## As if </noscript>
3103                    pop @{$self->{open_elements}};
3104                    !!!parse-error (type => 'in noscript:script');
3105                    
3106                    ## Reprocess in the "in head" insertion mode...
3107                    pop @{$self->{open_elements}};
3108                    $self->{insertion_mode} = AFTER_HEAD_IM;
3109                    !!!next-token;
3110                    redo B;
3111                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3112                    pop @{$self->{open_elements}};
3113                    $self->{insertion_mode} = AFTER_HEAD_IM;
3114                    !!!next-token;
3115                    redo B;
3116                  } else {
3117                    #
3118                  }
3119                } elsif ($token->{tag_name} eq 'noscript') {
3120                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3121                    pop @{$self->{open_elements}};
3122                    $self->{insertion_mode} = IN_HEAD_IM;
3123                    !!!next-token;
3124                    redo B;
3125                  } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3126                    !!!parse-error (type => 'unmatched end tag:noscript');
3127                    ## Ignore the token ## ISSUE: An issue in the spec.
3128                    !!!next-token;
3129                    redo B;
3130                  } else {
3131                    #
3132                  }
3133                } elsif ({
3134                        body => 1, html => 1,                        body => 1, html => 1,
                       p => 1, br => 1,  
3135                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3136                  if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3137                    ## As if <head>
3138                    !!!create-element ($self->{head_element}, 'head');
3139                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3140                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3141    
3142                    $self->{insertion_mode} = IN_HEAD_IM;
3143                    ## Reprocess in the "in head" insertion mode...
3144                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3145                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3146                    ## Ignore the token
3147                    !!!next-token;
3148                    redo B;
3149                  }
3150                  
3151                #                #
3152              } elsif ($self->{insertion_mode} eq 'in head noscript' and              } elsif ({
                      {  
3153                        p => 1, br => 1,                        p => 1, br => 1,
3154                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3155                  if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3156                    ## As if <head>
3157                    !!!create-element ($self->{head_element}, 'head');
3158                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3159                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3160    
3161                    $self->{insertion_mode} = IN_HEAD_IM;
3162                    ## Reprocess in the "in head" insertion mode...
3163                  }
3164    
3165                #                #
3166              } elsif ($self->{insertion_mode} ne 'after head') {              } else {
3167                  if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3168                    #
3169                  } else {
3170                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3171                    ## Ignore the token
3172                    !!!next-token;
3173                    redo B;
3174                  }
3175                }
3176    
3177                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3178                  ## As if </noscript>
3179                  pop @{$self->{open_elements}};
3180                  !!!parse-error (type => 'in noscript:/'.$token->{tag_name});
3181                  
3182                  ## Reprocess in the "in head" insertion mode...
3183                  ## As if </head>
3184                  pop @{$self->{open_elements}};
3185    
3186                  ## Reprocess in the "after head" insertion mode...
3187                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3188                  ## As if </head>
3189                  pop @{$self->{open_elements}};
3190    
3191                  ## Reprocess in the "after head" insertion mode...
3192                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3193                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3194                ## Ignore the token                ## Ignore the token ## ISSUE: An issue in the spec.
3195                !!!next-token;                !!!next-token;
3196                redo B;                redo B;
             } else {  
               #  
3197              }              }
           } else {  
             #  
           }  
3198    
3199            ## As if </head> or </noscript> or <body>              ## "after head" insertion mode
3200            if ($self->{insertion_mode} eq 'in head') {              ## As if <body>
             pop @{$self->{open_elements}};  
             $self->{insertion_mode} = 'after head';  
           } elsif ($self->{insertion_mode} eq 'in head noscript') {  
             pop @{$self->{open_elements}};  
             !!!parse-error (type => 'in noscript:'.(defined $token->{tag_name} ? ($token->{type} eq 'end tag' ? '/' : '') . $token->{tag_name} : '#' . $token->{type}));  
             $self->{insertion_mode} = 'in head';  
           } else { # 'after head'  
3201              !!!insert-element ('body');              !!!insert-element ('body');
3202              $self->{insertion_mode} = 'in body';              $self->{insertion_mode} = IN_BODY_IM;
3203                ## reprocess
3204                redo B;
3205              } else {
3206                die "$0: $token->{type}: Unknown token type";
3207            }            }
           ## reprocess  
           redo B;  
3208    
3209            ## ISSUE: An issue in the spec.            ## ISSUE: An issue in the spec.
3210          } elsif ($self->{insertion_mode} eq 'in body' or      } elsif ($self->{insertion_mode} & BODY_IMS) {
3211                   $self->{insertion_mode} eq 'in cell' or            if ($token->{type} == CHARACTER_TOKEN) {
                  $self->{insertion_mode} eq 'in caption') {  
           if ($token->{type} eq 'character') {  
3212              ## NOTE: There is a code clone of "character in body".              ## NOTE: There is a code clone of "character in body".
3213              $reconstruct_active_formatting_elements->($insert_to_current);              $reconstruct_active_formatting_elements->($insert_to_current);
3214                            
# Line 3658  sub _tree_construction_main ($) { Line 3216  sub _tree_construction_main ($) {
3216    
3217              !!!next-token;              !!!next-token;
3218              redo B;              redo B;
3219            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} == START_TAG_TOKEN) {
3220              if ({              if ({
3221                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
3222                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,
3223                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
3224                if ($self->{insertion_mode} eq 'in cell') {                if ($self->{insertion_mode} == IN_CELL_IM) {
3225                  ## have an element in table scope                  ## have an element in table scope
3226                  my $tn;                  my $tn;
3227                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 3686  sub _tree_construction_main ($) { Line 3244  sub _tree_construction_main ($) {
3244                                    
3245                  ## Close the cell                  ## Close the cell
3246                  !!!back-token; # <?>                  !!!back-token; # <?>
3247                  $token = {type => 'end tag', tag_name => $tn};                  $token = {type => END_TAG_TOKEN, tag_name => $tn};
3248                  redo B;                  redo B;
3249                } elsif ($self->{insertion_mode} eq 'in caption') {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
3250                  !!!parse-error (type => 'not closed:caption');                  !!!parse-error (type => 'not closed:caption');
3251                                    
3252                  ## As if </caption>                  ## As if </caption>
# Line 3719  sub _tree_construction_main ($) { Line 3277  sub _tree_construction_main ($) {
3277                       tbody => 1, tfoot=> 1, thead => 1,                       tbody => 1, tfoot=> 1, thead => 1,
3278                      }->{$self->{open_elements}->[-1]->[1]}) {                      }->{$self->{open_elements}->[-1]->[1]}) {
3279                    !!!back-token; # <?>                    !!!back-token; # <?>
3280                    $token = {type => 'end tag', tag_name => 'caption'};                    $token = {type => END_TAG_TOKEN, tag_name => 'caption'};
3281                    !!!back-token;                    !!!back-token;
3282                    $token = {type => 'end tag',                    $token = {type => END_TAG_TOKEN,
3283                              tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                              tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3284                    redo B;                    redo B;
3285                  }                  }
# Line 3734  sub _tree_construction_main ($) { Line 3292  sub _tree_construction_main ($) {
3292                                    
3293                  $clear_up_to_marker->();                  $clear_up_to_marker->();
3294                                    
3295                  $self->{insertion_mode} = 'in table';                  $self->{insertion_mode} = IN_TABLE_IM;
3296                                    
3297                  ## reprocess                  ## reprocess
3298                  redo B;                  redo B;
# Line 3744  sub _tree_construction_main ($) { Line 3302  sub _tree_construction_main ($) {
3302              } else {              } else {
3303                #                #
3304              }              }
3305            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
3306              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {
3307                if ($self->{insertion_mode} eq 'in cell') {                if ($self->{insertion_mode} == IN_CELL_IM) {
3308                  ## have an element in table scope                  ## have an element in table scope
3309                  my $i;                  my $i;
3310                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 3776  sub _tree_construction_main ($) { Line 3334  sub _tree_construction_main ($) {
3334                       tbody => 1, tfoot=> 1, thead => 1,                       tbody => 1, tfoot=> 1, thead => 1,
3335                      }->{$self->{open_elements}->[-1]->[1]}) {                      }->{$self->{open_elements}->[-1]->[1]}) {
3336                    !!!back-token;                    !!!back-token;
3337                    $token = {type => 'end tag',                    $token = {type => END_TAG_TOKEN,
3338                              tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                              tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3339                    redo B;                    redo B;
3340                  }                  }
# Line 3789  sub _tree_construction_main ($) { Line 3347  sub _tree_construction_main ($) {
3347                                    
3348                  $clear_up_to_marker->();                  $clear_up_to_marker->();
3349                                    
3350                  $self->{insertion_mode} = 'in row';                  $self->{insertion_mode} = IN_ROW_IM;
3351                                    
3352                  !!!next-token;                  !!!next-token;
3353                  redo B;                  redo B;
3354                } elsif ($self->{insertion_mode} eq 'in caption') {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
3355                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3356                  ## Ignore the token                  ## Ignore the token
3357                  !!!next-token;                  !!!next-token;
# Line 3802  sub _tree_construction_main ($) { Line 3360  sub _tree_construction_main ($) {
3360                  #                  #
3361                }                }
3362              } elsif ($token->{tag_name} eq 'caption') {              } elsif ($token->{tag_name} eq 'caption') {
3363                if ($self->{insertion_mode} eq 'in caption') {                if ($self->{insertion_mode} == IN_CAPTION_IM) {
3364                  ## have a table element in table scope                  ## have a table element in table scope
3365                  my $i;                  my $i;
3366                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 3830  sub _tree_construction_main ($) { Line 3388  sub _tree_construction_main ($) {
3388                       tbody => 1, tfoot=> 1, thead => 1,                       tbody => 1, tfoot=> 1, thead => 1,
3389                      }->{$self->{open_elements}->[-1]->[1]}) {                      }->{$self->{open_elements}->[-1]->[1]}) {
3390                    !!!back-token;                    !!!back-token;
3391                    $token = {type => 'end tag',                    $token = {type => END_TAG_TOKEN,
3392                              tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                              tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3393                    redo B;                    redo B;
3394                  }                  }
# Line 3843  sub _tree_construction_main ($) { Line 3401  sub _tree_construction_main ($) {
3401                                    
3402                  $clear_up_to_marker->();                  $clear_up_to_marker->();
3403                                    
3404                  $self->{insertion_mode} = 'in table';                  $self->{insertion_mode} = IN_TABLE_IM;
3405                                    
3406                  !!!next-token;                  !!!next-token;
3407                  redo B;                  redo B;
3408                } elsif ($self->{insertion_mode} eq 'in cell') {                } elsif ($self->{insertion_mode} == IN_CELL_IM) {
3409                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3410                  ## Ignore the token                  ## Ignore the token
3411                  !!!next-token;                  !!!next-token;
# Line 3859  sub _tree_construction_main ($) { Line 3417  sub _tree_construction_main ($) {
3417                        table => 1, tbody => 1, tfoot => 1,                        table => 1, tbody => 1, tfoot => 1,
3418                        thead => 1, tr => 1,                        thead => 1, tr => 1,
3419                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
3420                       $self->{insertion_mode} eq 'in cell') {                       $self->{insertion_mode} == IN_CELL_IM) {
3421                ## have an element in table scope                ## have an element in table scope
3422                my $i;                my $i;
3423                my $tn;                my $tn;
# Line 3887  sub _tree_construction_main ($) { Line 3445  sub _tree_construction_main ($) {
3445    
3446                ## Close the cell                ## Close the cell
3447                !!!back-token; # </?>                !!!back-token; # </?>
3448                $token = {type => 'end tag', tag_name => $tn};                $token = {type => END_TAG_TOKEN, tag_name => $tn};
3449                redo B;                redo B;
3450              } elsif ($token->{tag_name} eq 'table' and              } elsif ($token->{tag_name} eq 'table' and
3451                       $self->{insertion_mode} eq 'in caption') {                       $self->{insertion_mode} == IN_CAPTION_IM) {
3452                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed:caption');
3453    
3454                ## As if </caption>                ## As if </caption>
# Line 3921  sub _tree_construction_main ($) { Line 3479  sub _tree_construction_main ($) {
3479                     tbody => 1, tfoot=> 1, thead => 1,                     tbody => 1, tfoot=> 1, thead => 1,
3480                    }->{$self->{open_elements}->[-1]->[1]}) {                    }->{$self->{open_elements}->[-1]->[1]}) {
3481                  !!!back-token; # </table>                  !!!back-token; # </table>
3482                  $token = {type => 'end tag', tag_name => 'caption'};                  $token = {type => END_TAG_TOKEN, tag_name => 'caption'};
3483                  !!!back-token;                  !!!back-token;
3484                  $token = {type => 'end tag',                  $token = {type => END_TAG_TOKEN,
3485                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3486                  redo B;                  redo B;
3487                }                }
# Line 3936  sub _tree_construction_main ($) { Line 3494  sub _tree_construction_main ($) {
3494    
3495                $clear_up_to_marker->();                $clear_up_to_marker->();
3496    
3497                $self->{insertion_mode} = 'in table';                $self->{insertion_mode} = IN_TABLE_IM;
3498    
3499                ## reprocess                ## reprocess
3500                redo B;                redo B;
3501              } elsif ({              } elsif ({
3502                        body => 1, col => 1, colgroup => 1, html => 1,                        body => 1, col => 1, colgroup => 1, html => 1,
3503                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3504                if ($self->{insertion_mode} eq 'in cell' or                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
                   $self->{insertion_mode} eq 'in caption') {  
3505                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3506                  ## Ignore the token                  ## Ignore the token
3507                  !!!next-token;                  !!!next-token;
# Line 3956  sub _tree_construction_main ($) { Line 3513  sub _tree_construction_main ($) {
3513                        tbody => 1, tfoot => 1,                        tbody => 1, tfoot => 1,
3514                        thead => 1, tr => 1,                        thead => 1, tr => 1,
3515                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
3516                       $self->{insertion_mode} eq 'in caption') {                       $self->{insertion_mode} == IN_CAPTION_IM) {
3517                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3518                ## Ignore the token                ## Ignore the token
3519                !!!next-token;                !!!next-token;
# Line 3964  sub _tree_construction_main ($) { Line 3521  sub _tree_construction_main ($) {
3521              } else {              } else {
3522                #                #
3523              }              }
3524            } else {        } else {
3525              #          die "$0: $token->{type}: Unknown token type";
3526            }        }
3527              
3528            $in_body->($insert_to_current);        $insert = $insert_to_current;
3529            redo B;        #
3530          } elsif ($self->{insertion_mode} eq 'in table') {      } elsif ($self->{insertion_mode} & TABLE_IMS) {
3531            if ($token->{type} eq 'character') {        if ($token->{type} == CHARACTER_TOKEN) {
             ## NOTE: There are "character in table" code clones.  
3532              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
3533                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
3534                                
# Line 4029  sub _tree_construction_main ($) { Line 3585  sub _tree_construction_main ($) {
3585                            
3586              !!!next-token;              !!!next-token;
3587              redo B;              redo B;
3588            } elsif ($token->{type} eq 'start tag') {        } elsif ($token->{type} == START_TAG_TOKEN) {
3589              if ({              if ({
3590                   caption => 1,                   tr => ($self->{insertion_mode} != IN_ROW_IM),
3591                   colgroup => 1,                   th => 1, td => 1,
                  tbody => 1, tfoot => 1, thead => 1,  
3592                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
3593                ## Clear back to table context                if ($self->{insertion_mode} == IN_TABLE_IM) {
3594                while ($self->{open_elements}->[-1]->[1] ne 'table' and                  ## Clear back to table context
3595                       $self->{open_elements}->[-1]->[1] ne 'html') {                  while ($self->{open_elements}->[-1]->[1] ne 'table' and
3596                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                         $self->{open_elements}->[-1]->[1] ne 'html') {
3597                  pop @{$self->{open_elements}};                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3598                      pop @{$self->{open_elements}};
3599                    }
3600                    
3601                    !!!insert-element ('tbody');
3602                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
3603                    ## reprocess in the "in table body" insertion mode...
3604                }                }
3605    
3606                push @$active_formatting_elements, ['#marker', '']                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
3607                  if $token->{tag_name} eq 'caption';                  unless ($token->{tag_name} eq 'tr') {
3608                      !!!parse-error (type => 'missing start tag:tr');
3609                    }
3610                    
3611                    ## Clear back to table body context
3612                    while (not {
3613                      tbody => 1, tfoot => 1, thead => 1, html => 1,
3614                    }->{$self->{open_elements}->[-1]->[1]}) {
3615                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3616                      pop @{$self->{open_elements}};
3617                    }
3618                    
3619                    $self->{insertion_mode} = IN_ROW_IM;
3620                    if ($token->{tag_name} eq 'tr') {
3621                      !!!insert-element ($token->{tag_name}, $token->{attributes});
3622                      !!!next-token;
3623                      redo B;
3624                    } else {
3625                      !!!insert-element ('tr');
3626                      ## reprocess in the "in row" insertion mode
3627                    }
3628                  }
3629    
3630                  ## Clear back to table row context
3631                  while (not {
3632                    tr => 1, html => 1,
3633                  }->{$self->{open_elements}->[-1]->[1]}) {
3634                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3635                    pop @{$self->{open_elements}};
3636                  }
3637                  
3638                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
3639                $self->{insertion_mode} = {                $self->{insertion_mode} = IN_CELL_IM;
3640                                   caption => 'in caption',  
3641                                   colgroup => 'in column group',                push @$active_formatting_elements, ['#marker', ''];
3642                                   tbody => 'in table body',                
                                  tfoot => 'in table body',  
                                  thead => 'in table body',  
                                 }->{$token->{tag_name}};  
3643                !!!next-token;                !!!next-token;
3644                redo B;                redo B;
3645              } elsif ({              } elsif ({
3646                        col => 1,                        caption => 1, col => 1, colgroup => 1,
3647                        td => 1, th => 1, tr => 1,                        tbody => 1, tfoot => 1, thead => 1,
3648                          tr => 1, # $self->{insertion_mode} == IN_ROW_IM
3649                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3650                ## Clear back to table context                if ($self->{insertion_mode} == IN_ROW_IM) {
3651                while ($self->{open_elements}->[-1]->[1] ne 'table' and                  ## As if </tr>
3652                       $self->{open_elements}->[-1]->[1] ne 'html') {                  ## have an element in table scope
3653                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  my $i;
3654                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3655                      my $node = $self->{open_elements}->[$_];
3656                      if ($node->[1] eq 'tr') {
3657                        $i = $_;
3658                        last INSCOPE;
3659                      } elsif ({
3660                                table => 1, html => 1,
3661                               }->{$node->[1]}) {
3662                        last INSCOPE;
3663                      }
3664                    } # INSCOPE
3665                    unless (defined $i) {
3666                      !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});
3667                      ## Ignore the token
3668                      !!!next-token;
3669                      redo B;
3670                    }
3671                    
3672                    ## Clear back to table row context
3673                    while (not {
3674                      tr => 1, html => 1,
3675                    }->{$self->{open_elements}->[-1]->[1]}) {
3676                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3677                      pop @{$self->{open_elements}};
3678                    }
3679                    
3680                    pop @{$self->{open_elements}}; # tr
3681                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
3682                    if ($token->{tag_name} eq 'tr') {
3683                      ## reprocess
3684                      redo B;
3685                    } else {
3686                      ## reprocess in the "in table body" insertion mode...
3687                    }
3688                  }
3689    
3690                  if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
3691                    ## have an element in table scope
3692                    my $i;
3693                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3694                      my $node = $self->{open_elements}->[$_];
3695                      if ({
3696                           tbody => 1, thead => 1, tfoot => 1,
3697                          }->{$node->[1]}) {
3698                        $i = $_;
3699                        last INSCOPE;
3700                      } elsif ({
3701                                table => 1, html => 1,
3702                               }->{$node->[1]}) {
3703                        last INSCOPE;
3704                      }
3705                    } # INSCOPE
3706                    unless (defined $i) {
3707                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3708                      ## Ignore the token
3709                      !!!next-token;
3710                      redo B;
3711                    }
3712    
3713                    ## Clear back to table body context
3714                    while (not {
3715                      tbody => 1, tfoot => 1, thead => 1, html => 1,
3716                    }->{$self->{open_elements}->[-1]->[1]}) {
3717                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3718                      pop @{$self->{open_elements}};
3719                    }
3720                    
3721                    ## As if <{current node}>
3722                    ## have an element in table scope
3723                    ## true by definition
3724                    
3725                    ## Clear back to table body context
3726                    ## nop by definition
3727                    
3728                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3729                    $self->{insertion_mode} = IN_TABLE_IM;
3730                    ## reprocess in "in table" insertion mode...
3731                }                }
3732    
3733                !!!insert-element ($token->{tag_name} eq 'col' ? 'colgroup' : 'tbody');                if ($token->{tag_name} eq 'col') {
3734                $self->{insertion_mode} = $token->{tag_name} eq 'col'                  ## Clear back to table context
3735                  ? 'in column group' : 'in table body';                  while ($self->{open_elements}->[-1]->[1] ne 'table' and
3736                ## reprocess                         $self->{open_elements}->[-1]->[1] ne 'html') {
3737                redo B;                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3738                      pop @{$self->{open_elements}};
3739                    }
3740                    
3741                    !!!insert-element ('colgroup');
3742                    $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
3743                    ## reprocess
3744                    redo B;
3745                  } elsif ({
3746                            caption => 1,
3747                            colgroup => 1,
3748                            tbody => 1, tfoot => 1, thead => 1,
3749                           }->{$token->{tag_name}}) {
3750                    ## Clear back to table context
3751                    while ($self->{open_elements}->[-1]->[1] ne 'table' and
3752                           $self->{open_elements}->[-1]->[1] ne 'html') {
3753                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3754                      pop @{$self->{open_elements}};
3755                    }
3756                    
3757                    push @$active_formatting_elements, ['#marker', '']
3758                        if $token->{tag_name} eq 'caption';
3759                    
3760                    !!!insert-element ($token->{tag_name}, $token->{attributes});
3761                    $self->{insertion_mode} = {
3762                                               caption => IN_CAPTION_IM,
3763                                               colgroup => IN_COLUMN_GROUP_IM,
3764                                               tbody => IN_TABLE_BODY_IM,
3765                                               tfoot => IN_TABLE_BODY_IM,
3766                                               thead => IN_TABLE_BODY_IM,
3767                                              }->{$token->{tag_name}};
3768                    !!!next-token;
3769                    redo B;
3770                  } else {
3771                    die "$0: in table: <>: $token->{tag_name}";
3772                  }
3773              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
               ## NOTE: There are code clones for this "table in table"  
3774                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3775    
3776                ## As if </table>                ## As if </table>
# Line 4103  sub _tree_construction_main ($) { Line 3801  sub _tree_construction_main ($) {
3801                     tbody => 1, tfoot=> 1, thead => 1,                     tbody => 1, tfoot=> 1, thead => 1,
3802                    }->{$self->{open_elements}->[-1]->[1]}) {                    }->{$self->{open_elements}->[-1]->[1]}) {
3803                  !!!back-token; # <table>                  !!!back-token; # <table>
3804                  $token = {type => 'end tag', tag_name => 'table'};                  $token = {type => END_TAG_TOKEN, tag_name => 'table'};
3805                  !!!back-token;                  !!!back-token;
3806                  $token = {type => 'end tag',                  $token = {type => END_TAG_TOKEN,
3807                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3808                  redo B;                  redo B;
3809                }                }
# Line 4120  sub _tree_construction_main ($) { Line 3818  sub _tree_construction_main ($) {
3818    
3819                ## reprocess                ## reprocess
3820                redo B;                redo B;
3821              } else {          } else {
3822                #            !!!parse-error (type => 'in table:'.$token->{tag_name});
3823              }  
3824            } elsif ($token->{type} eq 'end tag') {            $insert = $insert_to_foster;
3825              if ($token->{tag_name} eq 'table') {            #
3826                ## have a table element in table scope          }
3827          } elsif ($token->{type} == END_TAG_TOKEN) {
3828                if ($token->{tag_name} eq 'tr' and
3829                    $self->{insertion_mode} == IN_ROW_IM) {
3830                  ## have an element in table scope
3831                my $i;                my $i;
3832                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3833                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
# Line 4144  sub _tree_construction_main ($) { Line 3846  sub _tree_construction_main ($) {
3846                  !!!next-token;                  !!!next-token;
3847                  redo B;                  redo B;
3848                }                }
                 
               ## 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  
                 redo B;  
               }  
3849    
3850                if ($self->{open_elements}->[-1]->[1] ne 'table') {                ## Clear back to table row context
3851                  while (not {
3852                    tr => 1, html => 1,
3853                  }->{$self->{open_elements}->[-1]->[1]}) {
3854                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3855                    pop @{$self->{open_elements}};
3856                }                }
3857    
3858                splice @{$self->{open_elements}}, $i;                pop @{$self->{open_elements}}; # tr
3859                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
               $self->_reset_insertion_mode;  
   
               !!!next-token;  
               redo B;  
             } elsif ({  
                       body => 1, caption => 1, col => 1, colgroup => 1,  
                       html => 1, tbody => 1, td => 1, tfoot => 1, th => 1,  
                       thead => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
   
           !!!parse-error (type => 'in table:'.$token->{tag_name});  
           $in_body->($insert_to_foster);  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'in column group') {  
           if ($token->{type} eq 'character') {  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
               
             #  
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'col') {  
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               pop @{$self->{open_elements}};  
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'colgroup') {  
               if ($self->{open_elements}->[-1]->[1] eq 'html') {  
                 !!!parse-error (type => 'unmatched end tag:colgroup');  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               } else {  
                 pop @{$self->{open_elements}}; # colgroup  
                 $self->{insertion_mode} = 'in table';  
                 !!!next-token;  
                 redo B;              
               }  
             } elsif ($token->{tag_name} eq 'col') {  
               !!!parse-error (type => 'unmatched end tag:col');  
               ## Ignore the token  
3860                !!!next-token;                !!!next-token;
3861                redo B;                redo B;
3862              } else {              } elsif ($token->{tag_name} eq 'table') {
3863                #                if ($self->{insertion_mode} == IN_ROW_IM) {
3864              }                  ## As if </tr>
3865            } else {                  ## have an element in table scope
3866              #                  my $i;
3867            }                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3868                      my $node = $self->{open_elements}->[$_];
3869            ## As if </colgroup>                    if ($node->[1] eq 'tr') {
3870            if ($self->{open_elements}->[-1]->[1] eq 'html') {                      $i = $_;
3871              !!!parse-error (type => 'unmatched end tag:colgroup');                      last INSCOPE;
3872              ## Ignore the token                    } elsif ({
3873              !!!next-token;                              table => 1, html => 1,
3874              redo B;                             }->{$node->[1]}) {
3875            } else {                      last INSCOPE;
             pop @{$self->{open_elements}}; # colgroup  
             $self->{insertion_mode} = 'in table';  
             ## reprocess  
             redo B;  
           }  
         } elsif ($self->{insertion_mode} eq 'in table body') {  
           if ($token->{type} eq 'character') {  
             ## NOTE: This is a "character in table" code clone.  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
                 
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
   
             !!!parse-error (type => 'in table:#character');  
   
             ## As if in body, but insert into foster parent element  
             ## ISSUE: Spec says that "whenever a node would be inserted  
             ## into the current node" while characters might not be  
             ## result in a new Text node.  
             $reconstruct_active_formatting_elements->($insert_to_foster);  
   
             if ({  
                  table => 1, tbody => 1, tfoot => 1,  
                  thead => 1, tr => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               # MUST  
               my $foster_parent_element;  
               my $next_sibling;  
               my $prev_sibling;  
               OE: for (reverse 0..$#{$self->{open_elements}}) {  
                 if ($self->{open_elements}->[$_]->[1] eq 'table') {  
                   my $parent = $self->{open_elements}->[$_]->[0]->parent_node;  
                   if (defined $parent and $parent->node_type == 1) {  
                     $foster_parent_element = $parent;  
                     $next_sibling = $self->{open_elements}->[$_]->[0];  
                     $prev_sibling = $next_sibling->previous_sibling;  
                   } else {  
                     $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];  
                     $prev_sibling = $foster_parent_element->last_child;  
3876                    }                    }
3877                    last OE;                  } # INSCOPE
3878                    unless (defined $i) {
3879                      !!!parse-error (type => 'unmatched end tag:'.$token->{type});
3880                      ## Ignore the token
3881                      !!!next-token;
3882                      redo B;
3883                  }                  }
3884                } # OE                  
3885                $foster_parent_element = $self->{open_elements}->[0]->[0] and                  ## Clear back to table row context
3886                $prev_sibling = $foster_parent_element->last_child                  while (not {
3887                  unless defined $foster_parent_element;                    tr => 1, html => 1,
3888                if (defined $prev_sibling and                  }->{$self->{open_elements}->[-1]->[1]}) {
3889                    $prev_sibling->node_type == 3) {                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3890                  $prev_sibling->manakai_append_text ($token->{data});                    pop @{$self->{open_elements}};
               } else {  
                 $foster_parent_element->insert_before  
                   ($self->{document}->create_text_node ($token->{data}),  
                    $next_sibling);  
               }  
             } else {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
             }  
               
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ({  
                  tr => 1,  
                  th => 1, td => 1,  
                 }->{$token->{tag_name}}) {  
               unless ($token->{tag_name} eq 'tr') {  
                 !!!parse-error (type => 'missing start tag:tr');  
               }  
   
               ## Clear back to table body context  
               while (not {  
                 tbody => 1, tfoot => 1, thead => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
                 
               $self->{insertion_mode} = 'in row';  
               if ($token->{tag_name} eq 'tr') {  
                 !!!insert-element ($token->{tag_name}, $token->{attributes});  
                 !!!next-token;  
               } else {  
                 !!!insert-element ('tr');  
                 ## reprocess  
               }  
               redo B;  
             } elsif ({  
                       caption => 1, col => 1, colgroup => 1,  
                       tbody => 1, tfoot => 1, thead => 1,  
                      }->{$token->{tag_name}}) {  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ({  
                      tbody => 1, thead => 1, tfoot => 1,  
                     }->{$node->[1]}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
3891                  }                  }
3892                } # INSCOPE                  
3893                unless (defined $i) {                  pop @{$self->{open_elements}}; # tr
3894                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
3895                  ## Ignore the token                  ## reprocess in the "in table body" insertion mode...
                 !!!next-token;  
                 redo B;  
3896                }                }
3897    
3898                ## Clear back to table body context                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
3899                while (not {                  ## have an element in table scope
3900                  tbody => 1, tfoot => 1, thead => 1, html => 1,                  my $i;
3901                }->{$self->{open_elements}->[-1]->[1]}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3902                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    my $node = $self->{open_elements}->[$_];
3903                      if ({
3904                           tbody => 1, thead => 1, tfoot => 1,
3905                          }->{$node->[1]}) {
3906                        $i = $_;
3907                        last INSCOPE;
3908                      } elsif ({
3909                                table => 1, html => 1,
3910                               }->{$node->[1]}) {
3911                        last INSCOPE;
3912                      }
3913                    } # INSCOPE
3914                    unless (defined $i) {
3915                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3916                      ## Ignore the token
3917                      !!!next-token;
3918                      redo B;
3919                    }
3920                    
3921                    ## Clear back to table body context
3922                    while (not {
3923                      tbody => 1, tfoot => 1, thead => 1, html => 1,
3924                    }->{$self->{open_elements}->[-1]->[1]}) {
3925                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3926                      pop @{$self->{open_elements}};
3927                    }
3928                    
3929                    ## As if <{current node}>
3930                    ## have an element in table scope
3931                    ## true by definition
3932                    
3933                    ## Clear back to table body context
3934                    ## nop by definition
3935                    
3936                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3937                    $self->{insertion_mode} = IN_TABLE_IM;
3938                    ## reprocess in the "in table" insertion mode...
3939                }                }
3940    
               ## As if <{current node}>  
               ## have an element in table scope  
               ## true by definition  
   
               ## Clear back to table body context  
               ## nop by definition  
   
               pop @{$self->{open_elements}};  
               $self->{insertion_mode} = 'in table';  
               ## reprocess  
               redo B;  
             } elsif ($token->{tag_name} eq 'table') {  
               ## NOTE: This is a code clone of "table in table"  
               !!!parse-error (type => 'not closed:table');  
   
               ## As if </table>  
3941                ## have a table element in table scope                ## have a table element in table scope
3942                my $i;                my $i;
3943                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3944                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
3945                  if ($node->[1] eq 'table') {                  if ($node->[1] eq $token->{tag_name}) {
3946                    $i = $_;                    $i = $_;
3947                    last INSCOPE;                    last INSCOPE;
3948                  } elsif ({                  } elsif ({
# Line 4392  sub _tree_construction_main ($) { Line 3952  sub _tree_construction_main ($) {
3952                  }                  }
3953                } # INSCOPE                } # INSCOPE
3954                unless (defined $i) {                unless (defined $i) {
3955                  !!!parse-error (type => 'unmatched end tag:table');                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3956                  ## Ignore tokens </table><table>                  ## Ignore the token
3957                  !!!next-token;                  !!!next-token;
3958                  redo B;                  redo B;
3959                }                }
3960                  
3961                ## generate implied end tags                ## generate implied end tags
3962                if ({                if ({
3963                     dd => 1, dt => 1, li => 1, p => 1,                     dd => 1, dt => 1, li => 1, p => 1,
3964                     td => 1, th => 1, tr => 1,                     td => 1, th => 1, tr => 1,
3965                     tbody => 1, tfoot=> 1, thead => 1,                     tbody => 1, tfoot=> 1, thead => 1,
3966                    }->{$self->{open_elements}->[-1]->[1]}) {                    }->{$self->{open_elements}->[-1]->[1]}) {
                 !!!back-token; # <table>  
                 $token = {type => 'end tag', tag_name => 'table'};  
3967                  !!!back-token;                  !!!back-token;
3968                  $token = {type => 'end tag',                  $token = {type => END_TAG_TOKEN,
3969                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3970                  redo B;                  redo B;
3971                }                }
3972                  
3973                if ($self->{open_elements}->[-1]->[1] ne 'table') {                if ($self->{open_elements}->[-1]->[1] ne 'table') {
3974                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3975                }                }
3976                    
3977                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
3978                  
3979                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
3980                  
3981                ## reprocess                !!!next-token;
3982                redo B;                redo B;
3983              } else {              } elsif ({
3984                #                        tbody => 1, tfoot => 1, thead => 1,
3985              }                       }->{$token->{tag_name}} and
3986            } elsif ($token->{type} eq 'end tag') {                       $self->{insertion_mode} & ROW_IMS) {
3987              if ({                if ($self->{insertion_mode} == IN_ROW_IM) {
3988                   tbody => 1, tfoot => 1, thead => 1,                  ## have an element in table scope
3989                  }->{$token->{tag_name}}) {                  my $i;
3990                ## have an element in table scope                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3991                my $i;                    my $node = $self->{open_elements}->[$_];
3992                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                    if ($node->[1] eq $token->{tag_name}) {
3993                  my $node = $self->{open_elements}->[$_];                      $i = $_;
3994                  if ($node->[1] eq $token->{tag_name}) {                      last INSCOPE;
3995                    $i = $_;                    } elsif ({
3996                    last INSCOPE;                              table => 1, html => 1,
3997                  } elsif ({                             }->{$node->[1]}) {
3998                            table => 1, html => 1,                      last INSCOPE;
3999                           }->{$node->[1]}) {                    }
4000                    last INSCOPE;                  } # INSCOPE
4001                      unless (defined $i) {
4002                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4003                        ## Ignore the token
4004                        !!!next-token;
4005                        redo B;
4006                      }
4007                    
4008                    ## As if </tr>
4009                    ## have an element in table scope
4010                    my $i;
4011                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4012                      my $node = $self->{open_elements}->[$_];
4013                      if ($node->[1] eq 'tr') {
4014                        $i = $_;
4015                        last INSCOPE;
4016                      } elsif ({
4017                                table => 1, html => 1,
4018                               }->{$node->[1]}) {
4019                        last INSCOPE;
4020                      }
4021                    } # INSCOPE
4022                      unless (defined $i) {
4023                        !!!parse-error (type => 'unmatched end tag:tr');
4024                        ## Ignore the token
4025                        !!!next-token;
4026                        redo B;
4027                      }
4028                    
4029                    ## Clear back to table row context
4030                    while (not {
4031                      tr => 1, html => 1,
4032                    }->{$self->{open_elements}->[-1]->[1]}) {
4033                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4034                      pop @{$self->{open_elements}};
4035                  }                  }
4036                } # INSCOPE                  
4037                unless (defined $i) {                  pop @{$self->{open_elements}}; # tr
4038                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
4039                  ## Ignore the token                  ## reprocess in the "in table body" insertion mode...
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Clear back to table body context  
               while (not {  
                 tbody => 1, tfoot => 1, thead => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
4040                }                }
4041    
               pop @{$self->{open_elements}};  
               $self->{insertion_mode} = 'in table';  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'table') {  
4042                ## have an element in table scope                ## have an element in table scope
4043                my $i;                my $i;
4044                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4045                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4046                  if ({                  if ($node->[1] eq $token->{tag_name}) {
                      tbody => 1, thead => 1, tfoot => 1,  
                     }->{$node->[1]}) {  
4047                    $i = $_;                    $i = $_;
4048                    last INSCOPE;                    last INSCOPE;
4049                  } elsif ({                  } elsif ({
# Line 4492  sub _tree_construction_main ($) { Line 4067  sub _tree_construction_main ($) {
4067                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4068                }                }
4069    
               ## As if <{current node}>  
               ## have an element in table scope  
               ## true by definition  
   
               ## Clear back to table body context  
               ## nop by definition  
   
4070                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4071                $self->{insertion_mode} = 'in table';                $self->{insertion_mode} = IN_TABLE_IM;
4072                ## reprocess                !!!next-token;
4073                redo B;                redo B;
4074              } elsif ({              } elsif ({
4075                        body => 1, caption => 1, col => 1, colgroup => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
4076                        html => 1, td => 1, th => 1, tr => 1,                        html => 1, td => 1, th => 1,
4077                          tr => 1, # $self->{insertion_mode} == IN_ROW_IM
4078                          tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
4079                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4080                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4081                ## Ignore the token                ## Ignore the token
4082                !!!next-token;                !!!next-token;
4083                redo B;                redo B;
4084              } else {          } else {
4085                #            !!!parse-error (type => 'in table:/'.$token->{tag_name});
4086              }  
4087            } else {            $insert = $insert_to_foster;
4088              #            #
4089            }          }
4090                    } else {
4091            ## As if in table          die "$0: $token->{type}: Unknown token type";
4092            !!!parse-error (type => 'in table:'.$token->{tag_name});        }
4093            $in_body->($insert_to_foster);      } elsif ($self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
4094            redo B;            if ($token->{type} == CHARACTER_TOKEN) {
         } elsif ($self->{insertion_mode} eq 'in row') {  
           if ($token->{type} eq 'character') {  
             ## NOTE: This is a "character in table" code clone.  
4095              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4096                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
                 
4097                unless (length $token->{data}) {                unless (length $token->{data}) {
4098                  !!!next-token;                  !!!next-token;
4099                  redo B;                  redo B;
4100                }                }
4101              }              }
   
             !!!parse-error (type => 'in table:#character');  
   
             ## As if in body, but insert into foster parent element  
             ## ISSUE: Spec says that "whenever a node would be inserted  
             ## into the current node" while characters might not be  
             ## result in a new Text node.  
             $reconstruct_active_formatting_elements->($insert_to_foster);  
               
             if ({  
                  table => 1, tbody => 1, tfoot => 1,  
                  thead => 1, tr => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               # MUST  
               my $foster_parent_element;  
               my $next_sibling;  
               my $prev_sibling;  
               OE: for (reverse 0..$#{$self->{open_elements}}) {  
                 if ($self->{open_elements}->[$_]->[1] eq 'table') {  
                   my $parent = $self->{open_elements}->[$_]->[0]->parent_node;  
                   if (defined $parent and $parent->node_type == 1) {  
                     $foster_parent_element = $parent;  
                     $next_sibling = $self->{open_elements}->[$_]->[0];  
                     $prev_sibling = $next_sibling->previous_sibling;  
                   } else {  
                     $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];  
                     $prev_sibling = $foster_parent_element->last_child;  
                   }  
                   last OE;  
                 }  
               } # OE  
               $foster_parent_element = $self->{open_elements}->[0]->[0] and  
               $prev_sibling = $foster_parent_element->last_child  
                 unless defined $foster_parent_element;  
               if (defined $prev_sibling and  
                   $prev_sibling->node_type == 3) {  
                 $prev_sibling->manakai_append_text ($token->{data});  
               } else {  
                 $foster_parent_element->insert_before  
                   ($self->{document}->create_text_node ($token->{data}),  
                    $next_sibling);  
               }  
             } else {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
             }  
4102                            
4103              !!!next-token;              #
4104              redo B;            } elsif ($token->{type} == START_TAG_TOKEN) {
4105            } elsif ($token->{type} eq 'start tag') {              if ($token->{tag_name} eq 'col') {
             if ($token->{tag_name} eq 'th' or  
                 $token->{tag_name} eq 'td') {  
               ## Clear back to table row context  
               while (not {  
                 tr => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
                 
4106                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
4107                $self->{insertion_mode} = 'in cell';                pop @{$self->{open_elements}};
   
               push @$active_formatting_elements, ['#marker', ''];  
                 
4108                !!!next-token;                !!!next-token;
4109                redo B;                redo B;
4110              } elsif ({              } else {
                       caption => 1, col => 1, colgroup => 1,  
                       tbody => 1, tfoot => 1, thead => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               ## As if </tr>  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'tr') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Clear back to table row context  
               while (not {  
                 tr => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
   
               pop @{$self->{open_elements}}; # tr  
               $self->{insertion_mode} = 'in table body';  
               ## reprocess  
               redo B;  
             } elsif ($token->{tag_name} eq 'table') {  
               ## NOTE: This is a code clone of "table in table"  
               !!!parse-error (type => 'not closed:table');  
   
               ## As if </table>  
               ## have a table element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'table') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:table');  
                 ## Ignore tokens </table><table>  
                 !!!next-token;  
                 redo B;  
               }  
                 
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                    tbody => 1, tfoot=> 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token; # <table>  
                 $token = {type => 'end tag', tag_name => 'table'};  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
   
               if ($self->{open_elements}->[-1]->[1] ne 'table') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               }  
   
               splice @{$self->{open_elements}}, $i;  
   
               $self->_reset_insertion_mode;  
   
               ## reprocess  
               redo B;  
             } else {  
4111                #                #
4112              }              }
4113            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
4114              if ($token->{tag_name} eq 'tr') {              if ($token->{tag_name} eq 'colgroup') {
4115                ## have an element in table scope                if ($self->{open_elements}->[-1]->[1] eq 'html') {
4116                my $i;                  !!!parse-error (type => 'unmatched end tag:colgroup');
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Clear back to table row context  
               while (not {  
                 tr => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
   
               pop @{$self->{open_elements}}; # tr  
               $self->{insertion_mode} = 'in table body';  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'table') {  
               ## As if </tr>  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'tr') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{type});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Clear back to table row context  
               while (not {  
                 tr => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
   
               pop @{$self->{open_elements}}; # tr  
               $self->{insertion_mode} = 'in table body';  
               ## reprocess  
               redo B;  
             } elsif ({  
                       tbody => 1, tfoot => 1, thead => 1,  
                      }->{$token->{tag_name}}) {  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
4117                  ## Ignore the token                  ## Ignore the token
4118                  !!!next-token;                  !!!next-token;
4119                  redo B;                  redo B;
4120                }                } else {
4121                    pop @{$self->{open_elements}}; # colgroup
4122                ## As if </tr>                  $self->{insertion_mode} = IN_TABLE_IM;
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'tr') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:tr');  
                 ## Ignore the token  
4123                  !!!next-token;                  !!!next-token;
4124                  redo B;                  redo B;            
               }  
   
               ## Clear back to table row context  
               while (not {  
                 tr => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
4125                }                }
4126                } elsif ($token->{tag_name} eq 'col') {
4127                pop @{$self->{open_elements}}; # tr                !!!parse-error (type => 'unmatched end tag:col');
               $self->{insertion_mode} = 'in table body';  
               ## reprocess  
               redo B;  
             } elsif ({  
                       body => 1, caption => 1, col => 1,  
                       colgroup => 1, html => 1, td => 1, th => 1,  
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
4128                ## Ignore the token                ## Ignore the token
4129                !!!next-token;                !!!next-token;
4130                redo B;                redo B;
4131              } else {              } else {
4132                #                #
4133              }              }
4134            } else {            } else {
4135              #              #
4136            }            }
4137    
4138            ## As if in table            ## As if </colgroup>
4139            !!!parse-error (type => 'in table:'.$token->{tag_name});            if ($self->{open_elements}->[-1]->[1] eq 'html') {
4140            $in_body->($insert_to_foster);              !!!parse-error (type => 'unmatched end tag:colgroup');
4141            redo B;              ## Ignore the token
         } elsif ($self->{insertion_mode} eq 'in select') {  
           if ($token->{type} eq 'character') {  
             $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
4142              !!!next-token;              !!!next-token;
4143              redo B;              redo B;
4144            } elsif ($token->{type} eq 'start tag') {            } else {
4145                pop @{$self->{open_elements}}; # colgroup
4146                $self->{insertion_mode} = IN_TABLE_IM;
4147                ## reprocess
4148                redo B;
4149              }
4150        } elsif ($self->{insertion_mode} == IN_SELECT_IM) {
4151          if ($token->{type} == CHARACTER_TOKEN) {
4152            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4153            !!!next-token;
4154            redo B;
4155          } elsif ($token->{type} == START_TAG_TOKEN) {
4156              if ($token->{tag_name} eq 'option') {              if ($token->{tag_name} eq 'option') {
4157                if ($self->{open_elements}->[-1]->[1] eq 'option') {                if ($self->{open_elements}->[-1]->[1] eq 'option') {
4158                  ## As if </option>                  ## As if </option>
# Line 4889  sub _tree_construction_main ($) { Line 4205  sub _tree_construction_main ($) {
4205    
4206                !!!next-token;                !!!next-token;
4207                redo B;                redo B;
4208              } else {          } else {
4209                #            !!!parse-error (type => 'in select:'.$token->{tag_name});
4210              }            ## Ignore the token
4211            } elsif ($token->{type} eq 'end tag') {            !!!next-token;
4212              redo B;
4213            }
4214          } elsif ($token->{type} == END_TAG_TOKEN) {
4215              if ($token->{tag_name} eq 'optgroup') {              if ($token->{tag_name} eq 'optgroup') {
4216                if ($self->{open_elements}->[-1]->[1] eq 'option' and                if ($self->{open_elements}->[-1]->[1] eq 'option' and
4217                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {
# Line 4994  sub _tree_construction_main ($) { Line 4313  sub _tree_construction_main ($) {
4313    
4314                ## reprocess                ## reprocess
4315                redo B;                redo B;
4316              } else {          } else {
4317                #            !!!parse-error (type => 'in select:/'.$token->{tag_name});
             }  
           } else {  
             #  
           }  
   
           !!!parse-error (type => 'in select:'.$token->{tag_name});  
4318            ## Ignore the token            ## Ignore the token
4319            !!!next-token;            !!!next-token;
4320            redo B;            redo B;
4321          } elsif ($self->{insertion_mode} eq 'after body') {          }
4322            if ($token->{type} eq 'character') {        } else {
4323              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          die "$0: $token->{type}: Unknown token type";
4324                my $data = $1;        }
4325                ## As if in body      } elsif ($self->{insertion_mode} & BODY_AFTER_IMS) {
4326                $reconstruct_active_formatting_elements->($insert_to_current);        if ($token->{type} == CHARACTER_TOKEN) {
4327            if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4328              my $data = $1;
4329              ## As if in body
4330              $reconstruct_active_formatting_elements->($insert_to_current);
4331                                
4332                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4333              
4334              unless (length $token->{data}) {
4335                !!!next-token;
4336                redo B;
4337              }
4338            }
4339            
4340            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4341              !!!parse-error (type => 'after html:#character');
4342    
4343                unless (length $token->{data}) {            ## Reprocess in the "main" phase, "after body" insertion mode...
4344                  !!!next-token;          }
4345                  redo B;          
4346                }          ## "after body" insertion mode
4347              }          !!!parse-error (type => 'after body:#character');
4348                
4349              #          $self->{insertion_mode} = IN_BODY_IM;
4350              !!!parse-error (type => 'after body:#character');          ## reprocess
4351            } elsif ($token->{type} eq 'start tag') {          redo B;
4352              !!!parse-error (type => 'after body:'.$token->{tag_name});        } elsif ($token->{type} == START_TAG_TOKEN) {
4353              #          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4354            } elsif ($token->{type} eq 'end tag') {            !!!parse-error (type => 'after html:'.$token->{tag_name});
4355              if ($token->{tag_name} eq 'html') {            
4356                if (defined $self->{inner_html_node}) {            ## Reprocess in the "main" phase, "after body" insertion mode...
4357                  !!!parse-error (type => 'unmatched end tag:html');          }
4358                  ## Ignore the token  
4359                  !!!next-token;          ## "after body" insertion mode
4360                  redo B;          !!!parse-error (type => 'after body:'.$token->{tag_name});
4361                } else {  
4362                  $previous_insertion_mode = $self->{insertion_mode};          $self->{insertion_mode} = IN_BODY_IM;
4363                  $self->{insertion_mode} = 'trailing end';          ## reprocess
4364                  !!!next-token;          redo B;
4365                  redo B;        } elsif ($token->{type} == END_TAG_TOKEN) {
4366                }          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4367              } else {            !!!parse-error (type => 'after html:/'.$token->{tag_name});
4368                !!!parse-error (type => 'after body:/'.$token->{tag_name});            
4369              }            $self->{insertion_mode} = AFTER_BODY_IM;
4370              ## Reprocess in the "main" phase, "after body" insertion mode...
4371            }
4372    
4373            ## "after body" insertion mode
4374            if ($token->{tag_name} eq 'html') {
4375              if (defined $self->{inner_html_node}) {
4376                !!!parse-error (type => 'unmatched end tag:html');
4377                ## Ignore the token
4378                !!!next-token;
4379                redo B;
4380            } else {            } else {
4381              die "$0: $token->{type}: Unknown token type";              $self->{insertion_mode} = AFTER_HTML_BODY_IM;
4382                !!!next-token;
4383                redo B;
4384            }            }
4385            } else {
4386              !!!parse-error (type => 'after body:/'.$token->{tag_name});
4387    
4388            $self->{insertion_mode} = 'in body';            $self->{insertion_mode} = IN_BODY_IM;
4389            ## reprocess            ## reprocess
4390            redo B;            redo B;
4391      } elsif ($self->{insertion_mode} eq 'in frameset') {          }
4392        if ($token->{type} eq 'character') {        } else {
4393            die "$0: $token->{type}: Unknown token type";
4394          }
4395        } elsif ($self->{insertion_mode} & FRAME_IMS) {
4396          if ($token->{type} == CHARACTER_TOKEN) {
4397          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4398            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4399              
4400            unless (length $token->{data}) {            unless (length $token->{data}) {
4401              !!!next-token;              !!!next-token;
4402              redo B;              redo B;
4403            }            }
4404          }          }
4405            
4406            if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
4407              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4408                !!!parse-error (type => 'in frameset:#character');
4409              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
4410                !!!parse-error (type => 'after frameset:#character');
4411              } else { # "after html frameset"
4412                !!!parse-error (type => 'after html:#character');
4413    
4414                $self->{insertion_mode} = AFTER_FRAMESET_IM;
4415                ## Reprocess in the "main" phase, "after frameset"...
4416                !!!parse-error (type => 'after frameset:#character');
4417              }
4418              
4419              ## Ignore the token.
4420              if (length $token->{data}) {
4421                ## reprocess the rest of characters
4422              } else {
4423                !!!next-token;
4424              }
4425              redo B;
4426            }
4427            
4428            die qq[$0: Character "$token->{data}"];
4429          } elsif ($token->{type} == START_TAG_TOKEN) {
4430            if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4431              !!!parse-error (type => 'after html:'.$token->{tag_name});
4432    
4433          !!!parse-error (type => 'in frameset:#character');            $self->{insertion_mode} = AFTER_FRAMESET_IM;
4434          ## Ignore the token            ## Process in the "main" phase, "after frameset" insertion mode...
4435          !!!next-token;          }
4436          redo B;  
4437        } elsif ($token->{type} eq 'start tag') {          if ($token->{tag_name} eq 'frameset' and
4438          if ($token->{tag_name} eq 'frameset') {              $self->{insertion_mode} == IN_FRAMESET_IM) {
4439            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes});
4440            !!!next-token;            !!!next-token;
4441            redo B;            redo B;
4442          } elsif ($token->{tag_name} eq 'frame') {          } elsif ($token->{tag_name} eq 'frame' and
4443                     $self->{insertion_mode} == IN_FRAMESET_IM) {
4444            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes});
4445            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
4446            !!!next-token;            !!!next-token;
# Line 5078  sub _tree_construction_main ($) { Line 4450  sub _tree_construction_main ($) {
4450            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);
4451            redo B;            redo B;
4452          } else {          } else {
4453            !!!parse-error (type => 'in frameset:'.$token->{tag_name});            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4454                !!!parse-error (type => 'in frameset:'.$token->{tag_name});
4455              } else {
4456                !!!parse-error (type => 'after frameset:'.$token->{tag_name});
4457              }
4458            ## Ignore the token            ## Ignore the token
4459            !!!next-token;            !!!next-token;
4460            redo B;            redo B;
4461          }          }
4462        } elsif ($token->{type} eq 'end tag') {        } elsif ($token->{type} == END_TAG_TOKEN) {
4463          if ($token->{tag_name} eq 'frameset') {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4464              !!!parse-error (type => 'after html:/'.$token->{tag_name});
4465    
4466              $self->{insertion_mode} = AFTER_FRAMESET_IM;
4467              ## Process in the "main" phase, "after frameset" insertion mode...
4468            }
4469    
4470            if ($token->{tag_name} eq 'frameset' and
4471                $self->{insertion_mode} == IN_FRAMESET_IM) {
4472            if ($self->{open_elements}->[-1]->[1] eq 'html' and            if ($self->{open_elements}->[-1]->[1] eq 'html' and
4473                @{$self->{open_elements}} == 1) {                @{$self->{open_elements}} == 1) {
4474              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
# Line 5097  sub _tree_construction_main ($) { Line 4481  sub _tree_construction_main ($) {
4481    
4482            if (not defined $self->{inner_html_node} and            if (not defined $self->{inner_html_node} and
4483                $self->{open_elements}->[-1]->[1] ne 'frameset') {                $self->{open_elements}->[-1]->[1] ne 'frameset') {
4484              $self->{insertion_mode} = 'after frameset';              $self->{insertion_mode} = AFTER_FRAMESET_IM;
4485            }            }
4486            redo B;            redo B;
4487            } elsif ($token->{tag_name} eq 'html' and
4488                     $self->{insertion_mode} == AFTER_FRAMESET_IM) {
4489              $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
4490              !!!next-token;
4491              redo B;
4492          } else {          } else {
4493            !!!parse-error (type => 'in frameset:/'.$token->{tag_name});            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4494                !!!parse-error (type => 'in frameset:/'.$token->{tag_name});
4495              } else {
4496                !!!parse-error (type => 'after frameset:/'.$token->{tag_name});
4497              }
4498            ## Ignore the token            ## Ignore the token
4499            !!!next-token;            !!!next-token;
4500            redo B;            redo B;
# Line 5109  sub _tree_construction_main ($) { Line 4502  sub _tree_construction_main ($) {
4502        } else {        } else {
4503          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
4504        }        }
     } 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);  
4505    
4506                unless (length $token->{data}) {        ## ISSUE: An issue in spec here
4507                  !!!next-token;      } else {
4508                  redo B;        die "$0: $self->{insertion_mode}: Unknown insertion mode";
4509                }      }
             }  
4510    
4511              if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {      ## "in body" insertion mode
4512                !!!parse-error (type => 'after frameset:#character');      if ($token->{type} == START_TAG_TOKEN) {
4513          if ($token->{tag_name} eq 'script') {
4514            ## NOTE: This is an "as if in head" code clone
4515            $script_start_tag->($insert);
4516            redo B;
4517          } elsif ($token->{tag_name} eq 'style') {
4518            ## NOTE: This is an "as if in head" code clone
4519            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
4520            redo B;
4521          } elsif ({
4522                    base => 1, link => 1,
4523                   }->{$token->{tag_name}}) {
4524            ## NOTE: This is an "as if in head" code clone, only "-t" differs
4525            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4526            pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4527            !!!next-token;
4528            redo B;
4529          } elsif ($token->{tag_name} eq 'meta') {
4530            ## NOTE: This is an "as if in head" code clone, only "-t" differs
4531            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4532            my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4533    
4534                ## Ignore the token.          unless ($self->{confident}) {
4535                if (length $token->{data}) {            if ($token->{attributes}->{charset}) { ## TODO: And if supported
4536                  ## reprocess the rest of characters              $self->{change_encoding}
4537                } else {                  ->($self, $token->{attributes}->{charset}->{value});
4538                  !!!next-token;              
4539                }              $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4540                redo B;                  ->set_user_data (manakai_has_reference =>
4541                                         $token->{attributes}->{charset}
4542                                             ->{has_reference});
4543              } elsif ($token->{attributes}->{content}) {
4544                ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.
4545                if ($token->{attributes}->{content}->{value}
4546                    =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
4547                        [\x09-\x0D\x20]*=
4548                        [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4549                        ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
4550                  $self->{change_encoding}
4551                      ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);
4552                  $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4553                      ->set_user_data (manakai_has_reference =>
4554                                           $token->{attributes}->{content}
4555                                                 ->{has_reference});
4556              }              }
4557              }
         die qq[$0: Character "$token->{data}"];  
       } elsif ($token->{type} eq 'start tag') {  
         if ($token->{tag_name} eq 'noframes') {  
           ## NOTE: As if in body.  
           $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);  
           redo B;  
4558          } else {          } else {
4559            !!!parse-error (type => 'after frameset:'.$token->{tag_name});            if ($token->{attributes}->{charset}) {
4560                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4561                    ->set_user_data (manakai_has_reference =>
4562                                         $token->{attributes}->{charset}
4563                                             ->{has_reference});
4564              }
4565              if ($token->{attributes}->{content}) {
4566                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4567                    ->set_user_data (manakai_has_reference =>
4568                                         $token->{attributes}->{content}
4569                                             ->{has_reference});
4570              }
4571            }
4572    
4573            !!!next-token;
4574            redo B;
4575          } elsif ($token->{tag_name} eq 'title') {
4576            !!!parse-error (type => 'in body:title');
4577            ## NOTE: This is an "as if in head" code clone
4578            $parse_rcdata->(RCDATA_CONTENT_MODEL, sub {
4579              if (defined $self->{head_element}) {
4580                $self->{head_element}->append_child ($_[0]);
4581              } else {
4582                $insert->($_[0]);
4583              }
4584            });
4585            redo B;
4586          } elsif ($token->{tag_name} eq 'body') {
4587            !!!parse-error (type => 'in body:body');
4588                  
4589            if (@{$self->{open_elements}} == 1 or
4590                $self->{open_elements}->[1]->[1] ne 'body') {
4591            ## Ignore the token            ## Ignore the token
4592            } else {
4593              my $body_el = $self->{open_elements}->[1]->[0];
4594              for my $attr_name (keys %{$token->{attributes}}) {
4595                unless ($body_el->has_attribute_ns (undef, $attr_name)) {
4596                  $body_el->set_attribute_ns
4597                    (undef, [undef, $attr_name],
4598                     $token->{attributes}->{$attr_name}->{value});
4599                }
4600              }
4601            }
4602            !!!next-token;
4603            redo B;
4604          } elsif ({
4605                    address => 1, blockquote => 1, center => 1, dir => 1,
4606                    div => 1, dl => 1, fieldset => 1, listing => 1,
4607                    menu => 1, ol => 1, p => 1, ul => 1,
4608                    pre => 1,
4609                   }->{$token->{tag_name}}) {
4610            ## has a p element in scope
4611            INSCOPE: for (reverse @{$self->{open_elements}}) {
4612              if ($_->[1] eq 'p') {
4613                !!!back-token;
4614                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4615                redo B;
4616              } elsif ({
4617                        table => 1, caption => 1, td => 1, th => 1,
4618                        button => 1, marquee => 1, object => 1, html => 1,
4619                       }->{$_->[1]}) {
4620                last INSCOPE;
4621              }
4622            } # INSCOPE
4623              
4624            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4625            if ($token->{tag_name} eq 'pre') {
4626              !!!next-token;
4627              if ($token->{type} == CHARACTER_TOKEN) {
4628                $token->{data} =~ s/^\x0A//;
4629                unless (length $token->{data}) {
4630                  !!!next-token;
4631                }
4632              }
4633            } else {
4634            !!!next-token;            !!!next-token;
           redo B;  
4635          }          }
4636        } elsif ($token->{type} eq 'end tag') {          redo B;
4637          if ($token->{tag_name} eq 'html') {        } elsif ($token->{tag_name} eq 'form') {
4638            $previous_insertion_mode = $self->{insertion_mode};          if (defined $self->{form_element}) {
4639            $self->{insertion_mode} = 'trailing end';            !!!parse-error (type => 'in form:form');
4640              ## Ignore the token
4641            !!!next-token;            !!!next-token;
4642            redo B;            redo B;
4643          } else {          } else {
4644            !!!parse-error (type => 'after frameset:/'.$token->{tag_name});            ## has a p element in scope
4645            ## Ignore the token            INSCOPE: for (reverse @{$self->{open_elements}}) {
4646                if ($_->[1] eq 'p') {
4647                  !!!back-token;
4648                  $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4649                  redo B;
4650                } elsif ({
4651                          table => 1, caption => 1, td => 1, th => 1,
4652                          button => 1, marquee => 1, object => 1, html => 1,
4653                         }->{$_->[1]}) {
4654                  last INSCOPE;
4655                }
4656              } # INSCOPE
4657                
4658              !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4659              $self->{form_element} = $self->{open_elements}->[-1]->[0];
4660            !!!next-token;            !!!next-token;
4661            redo B;            redo B;
4662          }          }
4663        } else {        } elsif ($token->{tag_name} eq 'li') {
4664          die "$0: $token->{type}: Unknown token type";          ## has a p element in scope
4665        }          INSCOPE: for (reverse @{$self->{open_elements}}) {
4666              if ($_->[1] eq 'p') {
4667                !!!back-token;
4668                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4669                redo B;
4670              } elsif ({
4671                        table => 1, caption => 1, td => 1, th => 1,
4672                        button => 1, marquee => 1, object => 1, html => 1,
4673                       }->{$_->[1]}) {
4674                last INSCOPE;
4675              }
4676            } # INSCOPE
4677              
4678            ## Step 1
4679            my $i = -1;
4680            my $node = $self->{open_elements}->[$i];
4681            LI: {
4682              ## Step 2
4683              if ($node->[1] eq 'li') {
4684                if ($i != -1) {
4685                  !!!parse-error (type => 'end tag missing:'.
4686                                  $self->{open_elements}->[-1]->[1]);
4687                }
4688                splice @{$self->{open_elements}}, $i;
4689                last LI;
4690              }
4691              
4692              ## Step 3
4693              if (not $formatting_category->{$node->[1]} and
4694                  #not $phrasing_category->{$node->[1]} and
4695                  ($special_category->{$node->[1]} or
4696                   $scoping_category->{$node->[1]}) and
4697                  $node->[1] ne 'address' and $node->[1] ne 'div') {
4698                last LI;
4699              }
4700              
4701              ## Step 4
4702              $i--;
4703              $node = $self->{open_elements}->[$i];
4704              redo LI;
4705            } # LI
4706              
4707            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4708            !!!next-token;
4709            redo B;
4710          } elsif ($token->{tag_name} eq 'dd' or $token->{tag_name} eq 'dt') {
4711            ## has a p element in scope
4712            INSCOPE: for (reverse @{$self->{open_elements}}) {
4713              if ($_->[1] eq 'p') {
4714                !!!back-token;
4715                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4716                redo B;
4717              } elsif ({
4718                        table => 1, caption => 1, td => 1, th => 1,
4719                        button => 1, marquee => 1, object => 1, html => 1,
4720                       }->{$_->[1]}) {
4721                last INSCOPE;
4722              }
4723            } # INSCOPE
4724              
4725            ## Step 1
4726            my $i = -1;
4727            my $node = $self->{open_elements}->[$i];
4728            LI: {
4729              ## Step 2
4730              if ($node->[1] eq 'dt' or $node->[1] eq 'dd') {
4731                if ($i != -1) {
4732                  !!!parse-error (type => 'end tag missing:'.
4733                                  $self->{open_elements}->[-1]->[1]);
4734                }
4735                splice @{$self->{open_elements}}, $i;
4736                last LI;
4737              }
4738              
4739              ## Step 3
4740              if (not $formatting_category->{$node->[1]} and
4741                  #not $phrasing_category->{$node->[1]} and
4742                  ($special_category->{$node->[1]} or
4743                   $scoping_category->{$node->[1]}) and
4744                  $node->[1] ne 'address' and $node->[1] ne 'div') {
4745                last LI;
4746              }
4747              
4748              ## Step 4
4749              $i--;
4750              $node = $self->{open_elements}->[$i];
4751              redo LI;
4752            } # LI
4753              
4754            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4755            !!!next-token;
4756            redo B;
4757          } elsif ($token->{tag_name} eq 'plaintext') {
4758            ## has a p element in scope
4759            INSCOPE: for (reverse @{$self->{open_elements}}) {
4760              if ($_->[1] eq 'p') {
4761                !!!back-token;
4762                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4763                redo B;
4764              } elsif ({
4765                        table => 1, caption => 1, td => 1, th => 1,
4766                        button => 1, marquee => 1, object => 1, html => 1,
4767                       }->{$_->[1]}) {
4768                last INSCOPE;
4769              }
4770            } # INSCOPE
4771              
4772            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4773              
4774            $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
4775              
4776            !!!next-token;
4777            redo B;
4778          } elsif ({
4779                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
4780                   }->{$token->{tag_name}}) {
4781            ## has a p element in scope
4782            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4783              my $node = $self->{open_elements}->[$_];
4784              if ($node->[1] eq 'p') {
4785                !!!back-token;
4786                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4787                redo B;
4788              } elsif ({
4789                        table => 1, caption => 1, td => 1, th => 1,
4790                        button => 1, marquee => 1, object => 1, html => 1,
4791                       }->{$node->[1]}) {
4792                last INSCOPE;
4793              }
4794            } # INSCOPE
4795              
4796            ## NOTE: See <http://html5.org/tools/web-apps-tracker?from=925&to=926>
4797            ## has an element in scope
4798            #my $i;
4799            #INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4800            #  my $node = $self->{open_elements}->[$_];
4801            #  if ({
4802            #       h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
4803            #      }->{$node->[1]}) {
4804            #    $i = $_;
4805            #    last INSCOPE;
4806            #  } elsif ({
4807            #            table => 1, caption => 1, td => 1, th => 1,
4808            #            button => 1, marquee => 1, object => 1, html => 1,
4809            #           }->{$node->[1]}) {
4810            #    last INSCOPE;
4811            #  }
4812            #} # INSCOPE
4813            #  
4814            #if (defined $i) {
4815            #  !!! parse-error (type => 'in hn:hn');
4816            #  splice @{$self->{open_elements}}, $i;
4817            #}
4818              
4819            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4820              
4821            !!!next-token;
4822            redo B;
4823          } elsif ($token->{tag_name} eq 'a') {
4824            AFE: for my $i (reverse 0..$#$active_formatting_elements) {
4825              my $node = $active_formatting_elements->[$i];
4826              if ($node->[1] eq 'a') {
4827                !!!parse-error (type => 'in a:a');
4828                
4829                !!!back-token;
4830                $token = {type => END_TAG_TOKEN, tag_name => 'a'};
4831                $formatting_end_tag->($token->{tag_name});
4832                
4833                AFE2: for (reverse 0..$#$active_formatting_elements) {
4834                  if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
4835                    splice @$active_formatting_elements, $_, 1;
4836                    last AFE2;
4837                  }
4838                } # AFE2
4839                OE: for (reverse 0..$#{$self->{open_elements}}) {
4840                  if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {
4841                    splice @{$self->{open_elements}}, $_, 1;
4842                    last OE;
4843                  }
4844                } # OE
4845                last AFE;
4846              } elsif ($node->[0] eq '#marker') {
4847                last AFE;
4848              }
4849            } # AFE
4850              
4851            $reconstruct_active_formatting_elements->($insert_to_current);
4852    
4853        ## ISSUE: An issue in spec here          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4854      } elsif ($self->{insertion_mode} eq 'trailing end') {          push @$active_formatting_elements, $self->{open_elements}->[-1];
4855        ## states in the main stage is preserved yet # MUST  
4856                  !!!next-token;
4857        if ($token->{type} eq 'character') {          redo B;
4858          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {        } elsif ({
4859            my $data = $1;                  b => 1, big => 1, em => 1, font => 1, i => 1,
4860            ## As if in the main phase.                  s => 1, small => 1, strile => 1,
4861            ## NOTE: The insertion mode in the main phase                  strong => 1, tt => 1, u => 1,
4862            ## just before the phase has been changed to the trailing                 }->{$token->{tag_name}}) {
4863            ## end phase is either "after body" or "after frameset".          $reconstruct_active_formatting_elements->($insert_to_current);
4864            $reconstruct_active_formatting_elements->($insert_to_current);          
4865            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4866            push @$active_formatting_elements, $self->{open_elements}->[-1];
4867            
4868            !!!next-token;
4869            redo B;
4870          } elsif ($token->{tag_name} eq 'nobr') {
4871            $reconstruct_active_formatting_elements->($insert_to_current);
4872    
4873            ## has a |nobr| element in scope
4874            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4875              my $node = $self->{open_elements}->[$_];
4876              if ($node->[1] eq 'nobr') {
4877                !!!parse-error (type => 'in nobr:nobr');
4878                !!!back-token;
4879                $token = {type => END_TAG_TOKEN, tag_name => 'nobr'};
4880                redo B;
4881              } elsif ({
4882                        table => 1, caption => 1, td => 1, th => 1,
4883                        button => 1, marquee => 1, object => 1, html => 1,
4884                       }->{$node->[1]}) {
4885                last INSCOPE;
4886              }
4887            } # INSCOPE
4888            
4889            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4890            push @$active_formatting_elements, $self->{open_elements}->[-1];
4891            
4892            !!!next-token;
4893            redo B;
4894          } elsif ($token->{tag_name} eq 'button') {
4895            ## has a button element in scope
4896            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4897              my $node = $self->{open_elements}->[$_];
4898              if ($node->[1] eq 'button') {
4899                !!!parse-error (type => 'in button:button');
4900                !!!back-token;
4901                $token = {type => END_TAG_TOKEN, tag_name => 'button'};
4902                redo B;
4903              } elsif ({
4904                        table => 1, caption => 1, td => 1, th => 1,
4905                        button => 1, marquee => 1, object => 1, html => 1,
4906                       }->{$node->[1]}) {
4907                last INSCOPE;
4908              }
4909            } # INSCOPE
4910              
4911            $reconstruct_active_formatting_elements->($insert_to_current);
4912              
4913            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4914            push @$active_formatting_elements, ['#marker', ''];
4915    
4916            !!!next-token;
4917            redo B;
4918          } elsif ($token->{tag_name} eq 'marquee' or
4919                   $token->{tag_name} eq 'object') {
4920            $reconstruct_active_formatting_elements->($insert_to_current);
4921            
4922            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4923            push @$active_formatting_elements, ['#marker', ''];
4924            
4925            !!!next-token;
4926            redo B;
4927          } elsif ($token->{tag_name} eq 'xmp') {
4928            $reconstruct_active_formatting_elements->($insert_to_current);
4929            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
4930            redo B;
4931          } elsif ($token->{tag_name} eq 'table') {
4932            ## has a p element in scope
4933            INSCOPE: for (reverse @{$self->{open_elements}}) {
4934              if ($_->[1] eq 'p') {
4935                !!!back-token;
4936                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4937                redo B;
4938              } elsif ({
4939                        table => 1, caption => 1, td => 1, th => 1,
4940                        button => 1, marquee => 1, object => 1, html => 1,
4941                       }->{$_->[1]}) {
4942                last INSCOPE;
4943              }
4944            } # INSCOPE
4945              
4946            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4947                        
4948            $self->{open_elements}->[-1]->[0]->manakai_append_text ($data);          $self->{insertion_mode} = IN_TABLE_IM;
4949                        
4950            !!!next-token;
4951            redo B;
4952          } elsif ({
4953                    area => 1, basefont => 1, bgsound => 1, br => 1,
4954                    embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
4955                    image => 1,
4956                   }->{$token->{tag_name}}) {
4957            if ($token->{tag_name} eq 'image') {
4958              !!!parse-error (type => 'image');
4959              $token->{tag_name} = 'img';
4960            }
4961    
4962            ## NOTE: There is an "as if <br>" code clone.
4963            $reconstruct_active_formatting_elements->($insert_to_current);
4964            
4965            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4966            pop @{$self->{open_elements}};
4967            
4968            !!!next-token;
4969            redo B;
4970          } elsif ($token->{tag_name} eq 'hr') {
4971            ## has a p element in scope
4972            INSCOPE: for (reverse @{$self->{open_elements}}) {
4973              if ($_->[1] eq 'p') {
4974                !!!back-token;
4975                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4976                redo B;
4977              } elsif ({
4978                        table => 1, caption => 1, td => 1, th => 1,
4979                        button => 1, marquee => 1, object => 1, html => 1,
4980                       }->{$_->[1]}) {
4981                last INSCOPE;
4982              }
4983            } # INSCOPE
4984              
4985            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4986            pop @{$self->{open_elements}};
4987              
4988            !!!next-token;
4989            redo B;
4990          } elsif ($token->{tag_name} eq 'input') {
4991            $reconstruct_active_formatting_elements->($insert_to_current);
4992            
4993            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4994            ## TODO: associate with $self->{form_element} if defined
4995            pop @{$self->{open_elements}};
4996            
4997            !!!next-token;
4998            redo B;
4999          } elsif ($token->{tag_name} eq 'isindex') {
5000            !!!parse-error (type => 'isindex');
5001            
5002            if (defined $self->{form_element}) {
5003              ## Ignore the token
5004              !!!next-token;
5005              redo B;
5006            } else {
5007              my $at = $token->{attributes};
5008              my $form_attrs;
5009              $form_attrs->{action} = $at->{action} if $at->{action};
5010              my $prompt_attr = $at->{prompt};
5011              $at->{name} = {name => 'name', value => 'isindex'};
5012              delete $at->{action};
5013              delete $at->{prompt};
5014              my @tokens = (
5015                            {type => START_TAG_TOKEN, tag_name => 'form',
5016                             attributes => $form_attrs},
5017                            {type => START_TAG_TOKEN, tag_name => 'hr'},
5018                            {type => START_TAG_TOKEN, tag_name => 'p'},
5019                            {type => START_TAG_TOKEN, tag_name => 'label'},
5020                           );
5021              if ($prompt_attr) {
5022                push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value}};
5023              } else {
5024                push @tokens, {type => CHARACTER_TOKEN,
5025                               data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD
5026                ## TODO: make this configurable
5027              }
5028              push @tokens,
5029                            {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at},
5030                            #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
5031                            {type => END_TAG_TOKEN, tag_name => 'label'},
5032                            {type => END_TAG_TOKEN, tag_name => 'p'},
5033                            {type => START_TAG_TOKEN, tag_name => 'hr'},
5034                            {type => END_TAG_TOKEN, tag_name => 'form'};
5035              $token = shift @tokens;
5036              !!!back-token (@tokens);
5037              redo B;
5038            }
5039          } elsif ($token->{tag_name} eq 'textarea') {
5040            my $tag_name = $token->{tag_name};
5041            my $el;
5042            !!!create-element ($el, $token->{tag_name}, $token->{attributes});
5043            
5044            ## TODO: $self->{form_element} if defined
5045            $self->{content_model} = RCDATA_CONTENT_MODEL;
5046            delete $self->{escape}; # MUST
5047            
5048            $insert->($el);
5049            
5050            my $text = '';
5051            !!!next-token;
5052            if ($token->{type} == CHARACTER_TOKEN) {
5053              $token->{data} =~ s/^\x0A//;
5054            unless (length $token->{data}) {            unless (length $token->{data}) {
5055              !!!next-token;              !!!next-token;
             redo B;  
5056            }            }
5057          }          }
5058            while ($token->{type} == CHARACTER_TOKEN) {
5059              $text .= $token->{data};
5060              !!!next-token;
5061            }
5062            if (length $text) {
5063              $el->manakai_append_text ($text);
5064            }
5065            
5066            $self->{content_model} = PCDATA_CONTENT_MODEL;
5067            
5068            if ($token->{type} == END_TAG_TOKEN and
5069                $token->{tag_name} eq $tag_name) {
5070              ## Ignore the token
5071            } else {
5072              !!!parse-error (type => 'in RCDATA:#'.$token->{type});
5073            }
5074            !!!next-token;
5075            redo B;
5076          } elsif ({
5077                    iframe => 1,
5078                    noembed => 1,
5079                    noframes => 1,
5080                    noscript => 0, ## TODO: 1 if scripting is enabled
5081                   }->{$token->{tag_name}}) {
5082            ## NOTE: There is an "as if in body" code clone.
5083            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
5084            redo B;
5085          } elsif ($token->{tag_name} eq 'select') {
5086            $reconstruct_active_formatting_elements->($insert_to_current);
5087            
5088            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5089            
5090            $self->{insertion_mode} = IN_SELECT_IM;
5091            !!!next-token;
5092            redo B;
5093          } elsif ({
5094                    caption => 1, col => 1, colgroup => 1, frame => 1,
5095                    frameset => 1, head => 1, option => 1, optgroup => 1,
5096                    tbody => 1, td => 1, tfoot => 1, th => 1,
5097                    thead => 1, tr => 1,
5098                   }->{$token->{tag_name}}) {
5099            !!!parse-error (type => 'in body:'.$token->{tag_name});
5100            ## Ignore the token
5101            !!!next-token;
5102            redo B;
5103            
5104            ## ISSUE: An issue on HTML5 new elements in the spec.
5105          } else {
5106            $reconstruct_active_formatting_elements->($insert_to_current);
5107            
5108            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5109            
5110            !!!next-token;
5111            redo B;
5112          }
5113        } elsif ($token->{type} == END_TAG_TOKEN) {
5114          if ($token->{tag_name} eq 'body') {
5115            if (@{$self->{open_elements}} > 1 and
5116                $self->{open_elements}->[1]->[1] eq 'body') {
5117              for (@{$self->{open_elements}}) {
5118                unless ({
5119                           dd => 1, dt => 1, li => 1, p => 1, td => 1,
5120                           th => 1, tr => 1, body => 1, html => 1,
5121                         tbody => 1, tfoot => 1, thead => 1,
5122                        }->{$_->[1]}) {
5123                  !!!parse-error (type => 'not closed:'.$_->[1]);
5124                }
5125              }
5126    
5127          !!!parse-error (type => 'after html:#character');            $self->{insertion_mode} = AFTER_BODY_IM;
5128          $self->{insertion_mode} = $previous_insertion_mode;            !!!next-token;
5129          ## reprocess            redo B;
5130            } else {
5131              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5132              ## Ignore the token
5133              !!!next-token;
5134              redo B;
5135            }
5136          } elsif ($token->{tag_name} eq 'html') {
5137            if (@{$self->{open_elements}} > 1 and $self->{open_elements}->[1]->[1] eq 'body') {
5138              ## ISSUE: There is an issue in the spec.
5139              if ($self->{open_elements}->[-1]->[1] ne 'body') {
5140                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);
5141              }
5142              $self->{insertion_mode} = AFTER_BODY_IM;
5143              ## reprocess
5144              redo B;
5145            } else {
5146              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5147              ## Ignore the token
5148              !!!next-token;
5149              redo B;
5150            }
5151          } elsif ({
5152                    address => 1, blockquote => 1, center => 1, dir => 1,
5153                    div => 1, dl => 1, fieldset => 1, listing => 1,
5154                    menu => 1, ol => 1, pre => 1, ul => 1,
5155                    p => 1,
5156                    dd => 1, dt => 1, li => 1,
5157                    button => 1, marquee => 1, object => 1,
5158                   }->{$token->{tag_name}}) {
5159            ## has an element in scope
5160            my $i;
5161            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5162              my $node = $self->{open_elements}->[$_];
5163              if ($node->[1] eq $token->{tag_name}) {
5164                ## generate implied end tags
5165                if ({
5166                     dd => ($token->{tag_name} ne 'dd'),
5167                     dt => ($token->{tag_name} ne 'dt'),
5168                     li => ($token->{tag_name} ne 'li'),
5169                     p => ($token->{tag_name} ne 'p'),
5170                     td => 1, th => 1, tr => 1,
5171                     tbody => 1, tfoot=> 1, thead => 1,
5172                    }->{$self->{open_elements}->[-1]->[1]}) {
5173                  !!!back-token;
5174                  $token = {type => END_TAG_TOKEN,
5175                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5176                  redo B;
5177                }
5178                $i = $_;
5179                last INSCOPE unless $token->{tag_name} eq 'p';
5180              } elsif ({
5181                        table => 1, caption => 1, td => 1, th => 1,
5182                        button => 1, marquee => 1, object => 1, html => 1,
5183                       }->{$node->[1]}) {
5184                last INSCOPE;
5185              }
5186            } # INSCOPE
5187            
5188            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
5189              if (defined $i) {
5190                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
5191              } else {
5192                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5193              }
5194            }
5195            
5196            if (defined $i) {
5197              splice @{$self->{open_elements}}, $i;
5198            } elsif ($token->{tag_name} eq 'p') {
5199              ## As if <p>, then reprocess the current token
5200              my $el;
5201              !!!create-element ($el, 'p');
5202              $insert->($el);
5203            }
5204            $clear_up_to_marker->()
5205              if {
5206                button => 1, marquee => 1, object => 1,
5207              }->{$token->{tag_name}};
5208            !!!next-token;
5209          redo B;          redo B;
5210        } elsif ($token->{type} eq 'start tag') {        } elsif ($token->{tag_name} eq 'form') {
5211          !!!parse-error (type => 'after html:'.$token->{tag_name});          ## has an element in scope
5212          $self->{insertion_mode} = $previous_insertion_mode;          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5213          ## reprocess            my $node = $self->{open_elements}->[$_];
5214              if ($node->[1] eq $token->{tag_name}) {
5215                ## generate implied end tags
5216                if ({
5217                     dd => 1, dt => 1, li => 1, p => 1,
5218                     td => 1, th => 1, tr => 1,
5219                     tbody => 1, tfoot=> 1, thead => 1,
5220                    }->{$self->{open_elements}->[-1]->[1]}) {
5221                  !!!back-token;
5222                  $token = {type => END_TAG_TOKEN,
5223                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5224                  redo B;
5225                }
5226                last INSCOPE;
5227              } elsif ({
5228                        table => 1, caption => 1, td => 1, th => 1,
5229                        button => 1, marquee => 1, object => 1, html => 1,
5230                       }->{$node->[1]}) {
5231                last INSCOPE;
5232              }
5233            } # INSCOPE
5234            
5235            if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {
5236              pop @{$self->{open_elements}};
5237            } else {
5238              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5239            }
5240    
5241            undef $self->{form_element};
5242            !!!next-token;
5243          redo B;          redo B;
5244        } elsif ($token->{type} eq 'end tag') {        } elsif ({
5245          !!!parse-error (type => 'after html:/'.$token->{tag_name});                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
5246          $self->{insertion_mode} = $previous_insertion_mode;                 }->{$token->{tag_name}}) {
5247          ## reprocess          ## has an element in scope
5248            my $i;
5249            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5250              my $node = $self->{open_elements}->[$_];
5251              if ({
5252                   h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
5253                  }->{$node->[1]}) {
5254                ## generate implied end tags
5255                if ({
5256                     dd => 1, dt => 1, li => 1, p => 1,
5257                     td => 1, th => 1, tr => 1,
5258                     tbody => 1, tfoot=> 1, thead => 1,
5259                    }->{$self->{open_elements}->[-1]->[1]}) {
5260                  !!!back-token;
5261                  $token = {type => END_TAG_TOKEN,
5262                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5263                  redo B;
5264                }
5265                $i = $_;
5266                last INSCOPE;
5267              } elsif ({
5268                        table => 1, caption => 1, td => 1, th => 1,
5269                        button => 1, marquee => 1, object => 1, html => 1,
5270                       }->{$node->[1]}) {
5271                last INSCOPE;
5272              }
5273            } # INSCOPE
5274            
5275            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
5276              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5277            }
5278            
5279            splice @{$self->{open_elements}}, $i if defined $i;
5280            !!!next-token;
5281            redo B;
5282          } elsif ({
5283                    a => 1,
5284                    b => 1, big => 1, em => 1, font => 1, i => 1,
5285                    nobr => 1, s => 1, small => 1, strile => 1,
5286                    strong => 1, tt => 1, u => 1,
5287                   }->{$token->{tag_name}}) {
5288            $formatting_end_tag->($token->{tag_name});
5289            redo B;
5290          } elsif ($token->{tag_name} eq 'br') {
5291            !!!parse-error (type => 'unmatched end tag:br');
5292    
5293            ## As if <br>
5294            $reconstruct_active_formatting_elements->($insert_to_current);
5295            
5296            my $el;
5297            !!!create-element ($el, 'br');
5298            $insert->($el);
5299            
5300            ## Ignore the token.
5301            !!!next-token;
5302            redo B;
5303          } elsif ({
5304                    caption => 1, col => 1, colgroup => 1, frame => 1,
5305                    frameset => 1, head => 1, option => 1, optgroup => 1,
5306                    tbody => 1, td => 1, tfoot => 1, th => 1,
5307                    thead => 1, tr => 1,
5308                    area => 1, basefont => 1, bgsound => 1,
5309                    embed => 1, hr => 1, iframe => 1, image => 1,
5310                    img => 1, input => 1, isindex => 1, noembed => 1,
5311                    noframes => 1, param => 1, select => 1, spacer => 1,
5312                    table => 1, textarea => 1, wbr => 1,
5313                    noscript => 0, ## TODO: if scripting is enabled
5314                   }->{$token->{tag_name}}) {
5315            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5316            ## Ignore the token
5317            !!!next-token;
5318          redo B;          redo B;
5319            
5320            ## ISSUE: Issue on HTML5 new elements in spec
5321            
5322        } else {        } else {
5323          die "$0: $token->{type}: Unknown token";          ## Step 1
5324            my $node_i = -1;
5325            my $node = $self->{open_elements}->[$node_i];
5326    
5327            ## Step 2
5328            S2: {
5329              if ($node->[1] eq $token->{tag_name}) {
5330                ## Step 1
5331                ## generate implied end tags
5332                if ({
5333                     dd => 1, dt => 1, li => 1, p => 1,
5334                     td => 1, th => 1, tr => 1,
5335                     tbody => 1, tfoot => 1, thead => 1,
5336                    }->{$self->{open_elements}->[-1]->[1]}) {
5337                  !!!back-token;
5338                  $token = {type => END_TAG_TOKEN,
5339                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5340                  redo B;
5341                }
5342            
5343                ## Step 2
5344                if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {
5345                  ## NOTE: <x><y></x>
5346                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
5347                }
5348                
5349                ## Step 3
5350                splice @{$self->{open_elements}}, $node_i;
5351    
5352                !!!next-token;
5353                last S2;
5354              } else {
5355                ## Step 3
5356                if (not $formatting_category->{$node->[1]} and
5357                    #not $phrasing_category->{$node->[1]} and
5358                    ($special_category->{$node->[1]} or
5359                     $scoping_category->{$node->[1]})) {
5360                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5361                  ## Ignore the token
5362                  !!!next-token;
5363                  last S2;
5364                }
5365              }
5366              
5367              ## Step 4
5368              $node_i--;
5369              $node = $self->{open_elements}->[$node_i];
5370              
5371              ## Step 5;
5372              redo S2;
5373            } # S2
5374            redo B;
5375        }        }
     } else {  
       die "$0: $self->{insertion_mode}: Unknown insertion mode";  
5376      }      }
5377        redo B;
5378    } # B    } # B
5379    
5380      ## NOTE: The "trailing end" phase in HTML5 is split into
5381      ## two insertion modes: "after html body" and "after html frameset".
5382      ## NOTE: States in the main stage is preserved while
5383      ## the parser stays in the trailing end phase. # MUST
5384    
5385    ## Stop parsing # MUST    ## Stop parsing # MUST
5386        
5387    ## TODO: script stuffs    ## TODO: script stuffs
# Line 5214  sub set_inner_html ($$$) { Line 5393  sub set_inner_html ($$$) {
5393    my $s = \$_[0];    my $s = \$_[0];
5394    my $onerror = $_[1];    my $onerror = $_[1];
5395    
5396      ## ISSUE: Should {confident} be true?
5397    
5398    my $nt = $node->node_type;    my $nt = $node->node_type;
5399    if ($nt == 9) {    if ($nt == 9) {
5400      # MUST      # MUST
# Line 5286  sub set_inner_html ($$$) { Line 5467  sub set_inner_html ($$$) {
5467      $p->_initialize_tree_constructor;      $p->_initialize_tree_constructor;
5468    
5469      ## Step 2      ## Step 2
5470      my $node_ln = $node->local_name;      my $node_ln = $node->manakai_local_name;
5471      $p->{content_model} = {      $p->{content_model} = {
5472        title => RCDATA_CONTENT_MODEL,        title => RCDATA_CONTENT_MODEL,
5473        textarea => RCDATA_CONTENT_MODEL,        textarea => RCDATA_CONTENT_MODEL,
# Line 5326  sub set_inner_html ($$$) { Line 5507  sub set_inner_html ($$$) {
5507        if ($anode->node_type == 1) {        if ($anode->node_type == 1) {
5508          my $nsuri = $anode->namespace_uri;          my $nsuri = $anode->namespace_uri;
5509          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {
5510            if ($anode->local_name eq 'form') { ## TODO: case?            if ($anode->manakai_local_name eq 'form') {
5511              $p->{form_element} = $anode;              $p->{form_element} = $anode;
5512              last AN;              last AN;
5513            }            }
# Line 5366  sub set_inner_html ($$$) { Line 5547  sub set_inner_html ($$$) {
5547    
5548  } # tree construction stage  } # tree construction stage
5549    
5550  sub get_inner_html ($$$) {  package Whatpm::HTML::RestartParser;
5551    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  
5552    
5553  1;  1;
5554  # $Date$  # $Date$

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24