/[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.1 by wakaba, Tue May 1 10:36:06 2007 UTC revision 1.56 by wakaba, Sat Aug 11 07:19:18 2007 UTC
# Line 1  Line 1 
1  package What::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    
5  ## This is an early version of an HTML parser.  ## ISSUE:
6    ## var doc = implementation.createDocument (null, null, null);
7    ## doc.write ('');
8    ## alert (doc.compatMode);
9    
10    ## ISSUE: HTML5 revision 967 says that the encoding layer MUST NOT
11    ## strip BOM and the HTML layer MUST ignore it.  Whether we can do it
12    ## is not yet clear.
13    ## "{U+FEFF}..." in UTF-16BE/UTF-16LE is three or four characters?
14    ## "{U+FEFF}..." in GB18030?
15    
16  my $permitted_slash_tag_name = {  my $permitted_slash_tag_name = {
17    base => 1,    base => 1,
# Line 18  my $permitted_slash_tag_name = { Line 27  my $permitted_slash_tag_name = {
27    input => 1,    input => 1,
28  };  };
29    
30  my $entity_char = {  my $c1_entity_char = {
31    AElig => "\x{00C6}",    0x80 => 0x20AC,
32    Aacute => "\x{00C1}",    0x81 => 0xFFFD,
33    Acirc => "\x{00C2}",    0x82 => 0x201A,
34    Agrave => "\x{00C0}",    0x83 => 0x0192,
35    Alpha => "\x{0391}",    0x84 => 0x201E,
36    Aring => "\x{00C5}",    0x85 => 0x2026,
37    Atilde => "\x{00C3}",    0x86 => 0x2020,
38    Auml => "\x{00C4}",    0x87 => 0x2021,
39    Beta => "\x{0392}",    0x88 => 0x02C6,
40    Ccedil => "\x{00C7}",    0x89 => 0x2030,
41    Chi => "\x{03A7}",    0x8A => 0x0160,
42    Dagger => "\x{2021}",    0x8B => 0x2039,
43    Delta => "\x{0394}",    0x8C => 0x0152,
44    ETH => "\x{00D0}",    0x8D => 0xFFFD,
45    Eacute => "\x{00C9}",    0x8E => 0x017D,
46    Ecirc => "\x{00CA}",    0x8F => 0xFFFD,
47    Egrave => "\x{00C8}",    0x90 => 0xFFFD,
48    Epsilon => "\x{0395}",    0x91 => 0x2018,
49    Eta => "\x{0397}",    0x92 => 0x2019,
50    Euml => "\x{00CB}",    0x93 => 0x201C,
51    Gamma => "\x{0393}",    0x94 => 0x201D,
52    Iacute => "\x{00CD}",    0x95 => 0x2022,
53    Icirc => "\x{00CE}",    0x96 => 0x2013,
54    Igrave => "\x{00CC}",    0x97 => 0x2014,
55    Iota => "\x{0399}",    0x98 => 0x02DC,
56    Iuml => "\x{00CF}",    0x99 => 0x2122,
57    Kappa => "\x{039A}",    0x9A => 0x0161,
58    Lambda => "\x{039B}",    0x9B => 0x203A,
59    Mu => "\x{039C}",    0x9C => 0x0153,
60    Ntilde => "\x{00D1}",    0x9D => 0xFFFD,
61    Nu => "\x{039D}",    0x9E => 0x017E,
62    OElig => "\x{0152}",    0x9F => 0x0178,
63    Oacute => "\x{00D3}",  }; # $c1_entity_char
   Ocirc => "\x{00D4}",  
   Ograve => "\x{00D2}",  
   Omega => "\x{03A9}",  
   Omicron => "\x{039F}",  
   Oslash => "\x{00D8}",  
   Otilde => "\x{00D5}",  
   Ouml => "\x{00D6}",  
   Phi => "\x{03A6}",  
   Pi => "\x{03A0}",  
   Prime => "\x{2033}",  
   Psi => "\x{03A8}",  
   Rho => "\x{03A1}",  
   Scaron => "\x{0160}",  
   Sigma => "\x{03A3}",  
   THORN => "\x{00DE}",  
   Tau => "\x{03A4}",  
   Theta => "\x{0398}",  
   Uacute => "\x{00DA}",  
   Ucirc => "\x{00DB}",  
   Ugrave => "\x{00D9}",  
   Upsilon => "\x{03A5}",  
   Uuml => "\x{00DC}",  
   Xi => "\x{039E}",  
   Yacute => "\x{00DD}",  
   Yuml => "\x{0178}",  
   Zeta => "\x{0396}",  
   aacute => "\x{00E1}",  
   acirc => "\x{00E2}",  
   acute => "\x{00B4}",  
   aelig => "\x{00E6}",  
   agrave => "\x{00E0}",  
   alefsym => "\x{2135}",  
   alpha => "\x{03B1}",  
   amp => "\x{0026}",  
   AMP => "\x{0026}",  
   and => "\x{2227}",  
   ang => "\x{2220}",  
   apos => "\x{0027}",  
   aring => "\x{00E5}",  
   asymp => "\x{2248}",  
   atilde => "\x{00E3}",  
   auml => "\x{00E4}",  
   bdquo => "\x{201E}",  
   beta => "\x{03B2}",  
   brvbar => "\x{00A6}",  
   bull => "\x{2022}",  
   cap => "\x{2229}",  
   ccedil => "\x{00E7}",  
   cedil => "\x{00B8}",  
   cent => "\x{00A2}",  
   chi => "\x{03C7}",  
   circ => "\x{02C6}",  
   clubs => "\x{2663}",  
   cong => "\x{2245}",  
   copy => "\x{00A9}",  
   COPY => "\x{00A9}",  
   crarr => "\x{21B5}",  
   cup => "\x{222A}",  
   curren => "\x{00A4}",  
   dArr => "\x{21D3}",  
   dagger => "\x{2020}",  
   darr => "\x{2193}",  
   deg => "\x{00B0}",  
   delta => "\x{03B4}",  
   diams => "\x{2666}",  
   divide => "\x{00F7}",  
   eacute => "\x{00E9}",  
   ecirc => "\x{00EA}",  
   egrave => "\x{00E8}",  
   empty => "\x{2205}",  
   emsp => "\x{2003}",  
   ensp => "\x{2002}",  
   epsilon => "\x{03B5}",  
   equiv => "\x{2261}",  
   eta => "\x{03B7}",  
   eth => "\x{00F0}",  
   euml => "\x{00EB}",  
   euro => "\x{20AC}",  
   exist => "\x{2203}",  
   fnof => "\x{0192}",  
   forall => "\x{2200}",  
   frac12 => "\x{00BD}",  
   frac14 => "\x{00BC}",  
   frac34 => "\x{00BE}",  
   frasl => "\x{2044}",  
   gamma => "\x{03B3}",  
   ge => "\x{2265}",  
   gt => "\x{003E}",  
   GT => "\x{003E}",  
   hArr => "\x{21D4}",  
   harr => "\x{2194}",  
   hearts => "\x{2665}",  
   hellip => "\x{2026}",  
   iacute => "\x{00ED}",  
   icirc => "\x{00EE}",  
   iexcl => "\x{00A1}",  
   igrave => "\x{00EC}",  
   image => "\x{2111}",  
   infin => "\x{221E}",  
   int => "\x{222B}",  
   iota => "\x{03B9}",  
   iquest => "\x{00BF}",  
   isin => "\x{2208}",  
   iuml => "\x{00EF}",  
   kappa => "\x{03BA}",  
   lArr => "\x{21D0}",  
   lambda => "\x{03BB}",  
   lang => "\x{2329}",  
   laquo => "\x{00AB}",  
   larr => "\x{2190}",  
   lceil => "\x{2308}",  
   ldquo => "\x{201C}",  
   le => "\x{2264}",  
   lfloor => "\x{230A}",  
   lowast => "\x{2217}",  
   loz => "\x{25CA}",  
   lrm => "\x{200E}",  
   lsaquo => "\x{2039}",  
   lsquo => "\x{2018}",  
   lt => "\x{003C}",  
   LT => "\x{003C}",  
   macr => "\x{00AF}",  
   mdash => "\x{2014}",  
   micro => "\x{00B5}",  
   middot => "\x{00B7}",  
   minus => "\x{2212}",  
   mu => "\x{03BC}",  
   nabla => "\x{2207}",  
   nbsp => "\x{00A0}",  
   ndash => "\x{2013}",  
   ne => "\x{2260}",  
   ni => "\x{220B}",  
   not => "\x{00AC}",  
   notin => "\x{2209}",  
   nsub => "\x{2284}",  
   ntilde => "\x{00F1}",  
   nu => "\x{03BD}",  
   oacute => "\x{00F3}",  
   ocirc => "\x{00F4}",  
   oelig => "\x{0153}",  
   ograve => "\x{00F2}",  
   oline => "\x{203E}",  
   omega => "\x{03C9}",  
   omicron => "\x{03BF}",  
   oplus => "\x{2295}",  
   or => "\x{2228}",  
   ordf => "\x{00AA}",  
   ordm => "\x{00BA}",  
   oslash => "\x{00F8}",  
   otilde => "\x{00F5}",  
   otimes => "\x{2297}",  
   ouml => "\x{00F6}",  
   para => "\x{00B6}",  
   part => "\x{2202}",  
   permil => "\x{2030}",  
   perp => "\x{22A5}",  
   phi => "\x{03C6}",  
   pi => "\x{03C0}",  
   piv => "\x{03D6}",  
   plusmn => "\x{00B1}",  
   pound => "\x{00A3}",  
   prime => "\x{2032}",  
   prod => "\x{220F}",  
   prop => "\x{221D}",  
   psi => "\x{03C8}",  
   quot => "\x{0022}",  
   QUOT => "\x{0022}",  
   rArr => "\x{21D2}",  
   radic => "\x{221A}",  
   rang => "\x{232A}",  
   raquo => "\x{00BB}",  
   rarr => "\x{2192}",  
   rceil => "\x{2309}",  
   rdquo => "\x{201D}",  
   real => "\x{211C}",  
   reg => "\x{00AE}",  
   REG => "\x{00AE}",  
   rfloor => "\x{230B}",  
   rho => "\x{03C1}",  
   rlm => "\x{200F}",  
   rsaquo => "\x{203A}",  
   rsquo => "\x{2019}",  
   sbquo => "\x{201A}",  
   scaron => "\x{0161}",  
   sdot => "\x{22C5}",  
   sect => "\x{00A7}",  
   shy => "\x{00AD}",  
   sigma => "\x{03C3}",  
   sigmaf => "\x{03C2}",  
   sim => "\x{223C}",  
   spades => "\x{2660}",  
   sub => "\x{2282}",  
   sube => "\x{2286}",  
   sum => "\x{2211}",  
   sup => "\x{2283}",  
   sup1 => "\x{00B9}",  
   sup2 => "\x{00B2}",  
   sup3 => "\x{00B3}",  
   supe => "\x{2287}",  
   szlig => "\x{00DF}",  
   tau => "\x{03C4}",  
   there4 => "\x{2234}",  
   theta => "\x{03B8}",  
   thetasym => "\x{03D1}",  
   thinsp => "\x{2009}",  
   thorn => "\x{00FE}",  
   tilde => "\x{02DC}",  
   times => "\x{00D7}",  
   trade => "\x{2122}",  
   uArr => "\x{21D1}",  
   uacute => "\x{00FA}",  
   uarr => "\x{2191}",  
   ucirc => "\x{00FB}",  
   ugrave => "\x{00F9}",  
   uml => "\x{00A8}",  
   upsih => "\x{03D2}",  
   upsilon => "\x{03C5}",  
   uuml => "\x{00FC}",  
   weierp => "\x{2118}",  
   xi => "\x{03BE}",  
   yacute => "\x{00FD}",  
   yen => "\x{00A5}",  
   yuml => "\x{00FF}",  
   zeta => "\x{03B6}",  
   zwj => "\x{200D}",  
   zwnj => "\x{200C}",  
 };  
64    
65  my $special_category = {  my $special_category = {
66    address => 1, area => 1, base => 1, basefont => 1, bgsound => 1,    address => 1, area => 1, base => 1, basefont => 1, bgsound => 1,
# Line 307  sub parse_string ($$$;$) { Line 89  sub parse_string ($$$;$) {
89    my $s = \$_[0];    my $s = \$_[0];
90    $self->{document} = $_[1];    $self->{document} = $_[1];
91    
92    my $i;    ## NOTE: |set_inner_html| copies most of this method's code
93    
94    my $i = 0;    my $i = 0;
95      my $line = 1;
96      my $column = 0;
97    $self->{set_next_input_character} = sub {    $self->{set_next_input_character} = sub {
98      my $self = shift;      my $self = shift;
99    
100        pop @{$self->{prev_input_character}};
101        unshift @{$self->{prev_input_character}}, $self->{next_input_character};
102    
103      $self->{next_input_character} = -1 and return if $i >= length $$s;      $self->{next_input_character} = -1 and return if $i >= length $$s;
104      $self->{next_input_character} = ord substr $$s, $i++, 1;      $self->{next_input_character} = ord substr $$s, $i++, 1;
105        $column++;
106            
107      if ($self->{next_input_character} == 0x000D) { # CR      if ($self->{next_input_character} == 0x000A) { # LF
108        if ($i >= length $$s) {        $line++;
109          #        $column = 0;
110        } else {      } elsif ($self->{next_input_character} == 0x000D) { # CR
111          my $next_char = ord substr $$s, $i++, 1;        $i++ if substr ($$s, $i, 1) eq "\x0A";
         if ($next_char == 0x000A) { # LF  
           #  
         } else {  
           push @{$self->{char}}, $next_char;  
         }  
       }  
112        $self->{next_input_character} = 0x000A; # LF # MUST        $self->{next_input_character} = 0x000A; # LF # MUST
113          $line++;
114          $column = 0;
115      } elsif ($self->{next_input_character} > 0x10FFFF) {      } elsif ($self->{next_input_character} > 0x10FFFF) {
116        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
117      } elsif ($self->{next_input_character} == 0x0000) { # NULL      } elsif ($self->{next_input_character} == 0x0000) { # NULL
118          !!!parse-error (type => 'NULL');
119        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
120      }      }
121    };    };
122      $self->{prev_input_character} = [-1, -1, -1];
123      $self->{next_input_character} = -1;
124    
125    $self->{parse_error} = $_[2] || sub {    my $onerror = $_[2] || sub {
126      warn "Parse error at character $i\n"; ## TODO: Report (line, column) pair      my (%opt) = @_;
127        warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";
128      };
129      $self->{parse_error} = sub {
130        $onerror->(@_, line => $line, column => $column);
131    };    };
132    
133    $self->_initialize_tokenizer;    $self->_initialize_tokenizer;
# Line 357  sub new ($) { Line 150  sub new ($) {
150    return $self;    return $self;
151  } # new  } # new
152    
153    sub CM_ENTITY () { 0b001 } # & markup in data
154    sub CM_LIMITED_MARKUP () { 0b010 } # < markup in data (limited)
155    sub CM_FULL_MARKUP () { 0b100 } # < markup in data (any)
156    
157    sub PLAINTEXT_CONTENT_MODEL () { 0 }
158    sub CDATA_CONTENT_MODEL () { CM_LIMITED_MARKUP }
159    sub RCDATA_CONTENT_MODEL () { CM_ENTITY | CM_LIMITED_MARKUP }
160    sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }
161    
162    sub DOCTYPE_TOKEN () { 1 }
163    sub COMMENT_TOKEN () { 2 }
164    sub START_TAG_TOKEN () { 3 }
165    sub END_TAG_TOKEN () { 4 }
166    sub END_OF_FILE_TOKEN () { 5 }
167    sub CHARACTER_TOKEN () { 6 }
168    
169    sub AFTER_HTML_IMS () { 0b100 }
170    sub HEAD_IMS ()       { 0b1000 }
171    sub BODY_IMS ()       { 0b10000 }
172    sub BODY_TABLE_IMS () { 0b100000 }
173    sub TABLE_IMS ()      { 0b1000000 }
174    sub ROW_IMS ()        { 0b10000000 }
175    sub BODY_AFTER_IMS () { 0b100000000 }
176    sub FRAME_IMS ()      { 0b1000000000 }
177    
178    sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }
179    sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }
180    sub IN_HEAD_IM () { HEAD_IMS | 0b00 }
181    sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }
182    sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }
183    sub BEFORE_HEAD_IM () { HEAD_IMS | 0b11 }
184    sub IN_BODY_IM () { BODY_IMS }
185    sub IN_CELL_IM () { BODY_IMS | BODY_TABLE_IMS | 0b01 }
186    sub IN_CAPTION_IM () { BODY_IMS | BODY_TABLE_IMS | 0b10 }
187    sub IN_ROW_IM () { TABLE_IMS | ROW_IMS | 0b01 }
188    sub IN_TABLE_BODY_IM () { TABLE_IMS | ROW_IMS | 0b10 }
189    sub IN_TABLE_IM () { TABLE_IMS }
190    sub AFTER_BODY_IM () { BODY_AFTER_IMS }
191    sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }
192    sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }
193    sub IN_SELECT_IM () { 0b01 }
194    sub IN_COLUMN_GROUP_IM () { 0b10 }
195    
196  ## Implementations MUST act as if state machine in the spec  ## Implementations MUST act as if state machine in the spec
197    
198  sub _initialize_tokenizer ($) {  sub _initialize_tokenizer ($) {
199    my $self = shift;    my $self = shift;
200    $self->{state} = 'data'; # MUST    $self->{state} = 'data'; # MUST
201    $self->{content_model_flag} = 'PCDATA'; # be    $self->{content_model} = PCDATA_CONTENT_MODEL; # be
202    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE
203    undef $self->{current_attribute};    undef $self->{current_attribute};
204    undef $self->{last_emitted_start_tag_name};    undef $self->{last_emitted_start_tag_name};
# Line 371  sub _initialize_tokenizer ($) { Line 207  sub _initialize_tokenizer ($) {
207    # $self->{next_input_character}    # $self->{next_input_character}
208    !!!next-input-character;    !!!next-input-character;
209    $self->{token} = [];    $self->{token} = [];
210      # $self->{escape}
211  } # _initialize_tokenizer  } # _initialize_tokenizer
212    
213  ## A token has:  ## A token has:
214  ##   ->{type} eq 'DOCTYPE', 'start tag', 'end tag', 'comment',  ##   ->{type} == DOCTYPE_TOKEN, START_TAG_TOKEN, END_TAG_TOKEN, COMMENT_TOKEN,
215  ##       'character', or 'end-of-file'  ##       CHARACTER_TOKEN, or END_OF_FILE_TOKEN
216  ##   ->{name} (DOCTYPE, start tag (tagname), end tag (tagname))  ##   ->{name} (DOCTYPE_TOKEN)
217      ## ISSUE: the spec need s/tagname/tag name/  ##   ->{tag_name} (START_TAG_TOKEN, END_TAG_TOKEN)
218  ##   ->{error} == 1 or 0 (DOCTYPE)  ##   ->{public_identifier} (DOCTYPE_TOKEN)
219  ##   ->{attributes} isa HASH (start tag, end tag)  ##   ->{system_identifier} (DOCTYPE_TOKEN)
220  ##   ->{data} (comment, character)  ##   ->{correct} == 1 or 0 (DOCTYPE_TOKEN)
221    ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)
222  ## Macros  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
 ##   Macros MUST be preceded by three EXCLAMATION MARKs.  
 ##   emit ($token)  
 ##     Emits the specified token.  
223    
224  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
225    
# Line 404  sub _get_next_token ($) { Line 238  sub _get_next_token ($) {
238    A: {    A: {
239      if ($self->{state} eq 'data') {      if ($self->{state} eq 'data') {
240        if ($self->{next_input_character} == 0x0026) { # &        if ($self->{next_input_character} == 0x0026) { # &
241          if ($self->{content_model_flag} eq 'PCDATA' or          if ($self->{content_model} & CM_ENTITY) { # PCDATA | RCDATA
             $self->{content_model_flag} eq 'RCDATA') {  
242            $self->{state} = 'entity data';            $self->{state} = 'entity data';
243            !!!next-input-character;            !!!next-input-character;
244            redo A;            redo A;
245          } else {          } else {
246            #            #
247          }          }
248          } elsif ($self->{next_input_character} == 0x002D) { # -
249            if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
250              unless ($self->{escape}) {
251                if ($self->{prev_input_character}->[0] == 0x002D and # -
252                    $self->{prev_input_character}->[1] == 0x0021 and # !
253                    $self->{prev_input_character}->[2] == 0x003C) { # <
254                  $self->{escape} = 1;
255                }
256              }
257            }
258            
259            #
260        } elsif ($self->{next_input_character} == 0x003C) { # <        } elsif ($self->{next_input_character} == 0x003C) { # <
261          if ($self->{content_model_flag} ne 'PLAINTEXT') {          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA
262                (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA
263                 not $self->{escape})) {
264            $self->{state} = 'tag open';            $self->{state} = 'tag open';
265            !!!next-input-character;            !!!next-input-character;
266            redo A;            redo A;
267          } else {          } else {
268            #            #
269          }          }
270          } elsif ($self->{next_input_character} == 0x003E) { # >
271            if ($self->{escape} and
272                ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA
273              if ($self->{prev_input_character}->[0] == 0x002D and # -
274                  $self->{prev_input_character}->[1] == 0x002D) { # -
275                delete $self->{escape};
276              }
277            }
278            
279            #
280        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
281          !!!emit ({type => 'end-of-file'});          !!!emit ({type => END_OF_FILE_TOKEN});
282          last A; ## TODO: ok?          last A; ## TODO: ok?
283        }        }
284        # Anything else        # Anything else
285        my $token = {type => 'character',        my $token = {type => CHARACTER_TOKEN,
286                     data => chr $self->{next_input_character}};                     data => chr $self->{next_input_character}};
287        ## Stay in the data state        ## Stay in the data state
288        !!!next-input-character;        !!!next-input-character;
# Line 436  sub _get_next_token ($) { Line 293  sub _get_next_token ($) {
293      } elsif ($self->{state} eq 'entity data') {      } elsif ($self->{state} eq 'entity data') {
294        ## (cannot happen in CDATA state)        ## (cannot happen in CDATA state)
295                
296        my $token = $self->_tokenize_attempt_to_consume_an_entity;        my $token = $self->_tokenize_attempt_to_consume_an_entity (0);
297    
298        $self->{state} = 'data';        $self->{state} = 'data';
299        # next-input-character is already done        # next-input-character is already done
300    
301        unless (defined $token) {        unless (defined $token) {
302          !!!emit ({type => 'character', data => '&'});          !!!emit ({type => CHARACTER_TOKEN, data => '&'});
303        } else {        } else {
304          !!!emit ($token);          !!!emit ($token);
305        }        }
306    
307        redo A;        redo A;
308      } elsif ($self->{state} eq 'tag open') {      } elsif ($self->{state} eq 'tag open') {
309        if ($self->{content_model_flag} eq 'RCDATA' or        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
           $self->{content_model_flag} eq 'CDATA') {  
310          if ($self->{next_input_character} == 0x002F) { # /          if ($self->{next_input_character} == 0x002F) { # /
311            !!!next-input-character;            !!!next-input-character;
312            $self->{state} = 'close tag open';            $self->{state} = 'close tag open';
# Line 459  sub _get_next_token ($) { Line 315  sub _get_next_token ($) {
315            ## reconsume            ## reconsume
316            $self->{state} = 'data';            $self->{state} = 'data';
317    
318            !!!emit ({type => 'character', data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<'});
319    
320            redo A;            redo A;
321          }          }
322        } elsif ($self->{content_model_flag} eq 'PCDATA') {        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA
323          if ($self->{next_input_character} == 0x0021) { # !          if ($self->{next_input_character} == 0x0021) { # !
324            $self->{state} = 'markup declaration open';            $self->{state} = 'markup declaration open';
325            !!!next-input-character;            !!!next-input-character;
# Line 475  sub _get_next_token ($) { Line 331  sub _get_next_token ($) {
331          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_input_character} and
332                   $self->{next_input_character} <= 0x005A) { # A..Z                   $self->{next_input_character} <= 0x005A) { # A..Z
333            $self->{current_token}            $self->{current_token}
334              = {type => 'start tag',              = {type => START_TAG_TOKEN,
335                 tag_name => chr ($self->{next_input_character} + 0x0020)};                 tag_name => chr ($self->{next_input_character} + 0x0020)};
336            $self->{state} = 'tag name';            $self->{state} = 'tag name';
337            !!!next-input-character;            !!!next-input-character;
338            redo A;            redo A;
339          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_input_character} and
340                   $self->{next_input_character} <= 0x007A) { # a..z                   $self->{next_input_character} <= 0x007A) { # a..z
341            $self->{current_token} = {type => 'start tag',            $self->{current_token} = {type => START_TAG_TOKEN,
342                              tag_name => chr ($self->{next_input_character})};                              tag_name => chr ($self->{next_input_character})};
343            $self->{state} = 'tag name';            $self->{state} = 'tag name';
344            !!!next-input-character;            !!!next-input-character;
345            redo A;            redo A;
346          } elsif ($self->{next_input_character} == 0x003E) { # >          } elsif ($self->{next_input_character} == 0x003E) { # >
347            !!!parse-error;            !!!parse-error (type => 'empty start tag');
348            $self->{state} = 'data';            $self->{state} = 'data';
349            !!!next-input-character;            !!!next-input-character;
350    
351            !!!emit ({type => 'character', data => '<>'});            !!!emit ({type => CHARACTER_TOKEN, data => '<>'});
352    
353            redo A;            redo A;
354          } elsif ($self->{next_input_character} == 0x003F) { # ?          } elsif ($self->{next_input_character} == 0x003F) { # ?
355            !!!parse-error;            !!!parse-error (type => 'pio');
356            $self->{state} = 'bogus comment';            $self->{state} = 'bogus comment';
357            ## $self->{next_input_character} is intentionally left as is            ## $self->{next_input_character} is intentionally left as is
358            redo A;            redo A;
359          } else {          } else {
360            !!!parse-error;            !!!parse-error (type => 'bare stago');
361            $self->{state} = 'data';            $self->{state} = 'data';
362            ## reconsume            ## reconsume
363    
364            !!!emit ({type => 'character', data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<'});
365    
366            redo A;            redo A;
367          }          }
368        } else {        } else {
369          die "$0: $self->{content_model_flag}: Unknown content model flag";          die "$0: $self->{content_model} in tag open";
370        }        }
371      } elsif ($self->{state} eq 'close tag open') {      } elsif ($self->{state} eq 'close tag open') {
372        if ($self->{content_model_flag} eq 'RCDATA' or        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
373            $self->{content_model_flag} eq 'CDATA') {          if (defined $self->{last_emitted_start_tag_name}) {
374          my @next_char;            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>
375          TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {            my @next_char;
376              TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {
377                push @next_char, $self->{next_input_character};
378                my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);
379                my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;
380                if ($self->{next_input_character} == $c or $self->{next_input_character} == $C) {
381                  !!!next-input-character;
382                  next TAGNAME;
383                } else {
384                  $self->{next_input_character} = shift @next_char; # reconsume
385                  !!!back-next-input-character (@next_char);
386                  $self->{state} = 'data';
387    
388                  !!!emit ({type => CHARACTER_TOKEN, data => '</'});
389      
390                  redo A;
391                }
392              }
393            push @next_char, $self->{next_input_character};            push @next_char, $self->{next_input_character};
394            my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);        
395            my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;            unless ($self->{next_input_character} == 0x0009 or # HT
396            if ($self->{next_input_character} == $c or $self->{next_input_character} == $C) {                    $self->{next_input_character} == 0x000A or # LF
397              !!!next-input-character;                    $self->{next_input_character} == 0x000B or # VT
398              next TAGNAME;                    $self->{next_input_character} == 0x000C or # FF
399            } else {                    $self->{next_input_character} == 0x0020 or # SP
400              !!!parse-error;                    $self->{next_input_character} == 0x003E or # >
401                      $self->{next_input_character} == 0x002F or # /
402                      $self->{next_input_character} == -1) {
403              $self->{next_input_character} = shift @next_char; # reconsume              $self->{next_input_character} = shift @next_char; # reconsume
404              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
405              $self->{state} = 'data';              $self->{state} = 'data';
406                !!!emit ({type => CHARACTER_TOKEN, data => '</'});
             !!!emit ({type => 'character', data => '</'});  
   
407              redo A;              redo A;
408              } else {
409                $self->{next_input_character} = shift @next_char;
410                !!!back-next-input-character (@next_char);
411                # and consume...
412            }            }
413          }          } else {
414          push @next_char, $self->{next_input_character};            ## No start tag token has ever been emitted
415                  # next-input-character is already done
         unless ($self->{next_input_character} == 0x0009 or # HT  
                 $self->{next_input_character} == 0x000A or # LF  
                 $self->{next_input_character} == 0x000B or # VT  
                 $self->{next_input_character} == 0x000C or # FF  
                 $self->{next_input_character} == 0x0020 or # SP  
                 $self->{next_input_character} == 0x003E or # >  
                 $self->{next_input_character} == 0x002F or # /  
                 $self->{next_input_character} == 0x003C or # <  
                 $self->{next_input_character} == -1) {  
           !!!parse-error;  
           $self->{next_input_character} = shift @next_char; # reconsume  
           !!!back-next-input-character (@next_char);  
416            $self->{state} = 'data';            $self->{state} = 'data';
417              !!!emit ({type => CHARACTER_TOKEN, data => '</'});
           !!!emit ({type => 'character', data => '</'});  
   
418            redo A;            redo A;
         } else {  
           $self->{next_input_character} = shift @next_char;  
           !!!back-next-input-character (@next_char);  
           # and consume...  
419          }          }
420        }        }
421                
422        if (0x0041 <= $self->{next_input_character} and        if (0x0041 <= $self->{next_input_character} and
423            $self->{next_input_character} <= 0x005A) { # A..Z            $self->{next_input_character} <= 0x005A) { # A..Z
424          $self->{current_token} = {type => 'end tag',          $self->{current_token} = {type => END_TAG_TOKEN,
425                            tag_name => chr ($self->{next_input_character} + 0x0020)};                            tag_name => chr ($self->{next_input_character} + 0x0020)};
426          $self->{state} = 'tag name';          $self->{state} = 'tag name';
427          !!!next-input-character;          !!!next-input-character;
428          redo A;          redo A;
429        } elsif (0x0061 <= $self->{next_input_character} and        } elsif (0x0061 <= $self->{next_input_character} and
430                 $self->{next_input_character} <= 0x007A) { # a..z                 $self->{next_input_character} <= 0x007A) { # a..z
431          $self->{current_token} = {type => 'end tag',          $self->{current_token} = {type => END_TAG_TOKEN,
432                            tag_name => chr ($self->{next_input_character})};                            tag_name => chr ($self->{next_input_character})};
433          $self->{state} = 'tag name';          $self->{state} = 'tag name';
434          !!!next-input-character;          !!!next-input-character;
435          redo A;          redo A;
436        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
437          !!!parse-error;          !!!parse-error (type => 'empty end tag');
438          $self->{state} = 'data';          $self->{state} = 'data';
439          !!!next-input-character;          !!!next-input-character;
440          redo A;          redo A;
441        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
442          !!!parse-error;          !!!parse-error (type => 'bare etago');
443          $self->{state} = 'data';          $self->{state} = 'data';
444          # reconsume          # reconsume
445    
446          !!!emit ({type => 'character', data => '</'});          !!!emit ({type => CHARACTER_TOKEN, data => '</'});
447    
448          redo A;          redo A;
449        } else {        } else {
450          !!!parse-error;          !!!parse-error (type => 'bogus end tag');
451          $self->{state} = 'bogus comment';          $self->{state} = 'bogus comment';
452          ## $self->{next_input_character} is intentionally left as is          ## $self->{next_input_character} is intentionally left as is
453          redo A;          redo A;
# Line 603  sub _get_next_token ($) { Line 462  sub _get_next_token ($) {
462          !!!next-input-character;          !!!next-input-character;
463          redo A;          redo A;
464        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
465          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
466              $self->{current_token}->{first_start_tag}
467                  = not defined $self->{last_emitted_start_tag_name};
468            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
469          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
470            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
471            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
472              !!!parse-error;              !!!parse-error (type => 'end tag attribute');
473            }            }
474          } else {          } else {
475            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 617  sub _get_next_token ($) { Line 478  sub _get_next_token ($) {
478          !!!next-input-character;          !!!next-input-character;
479    
480          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
481    
482          redo A;          redo A;
483        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_input_character} and
# Line 627  sub _get_next_token ($) { Line 487  sub _get_next_token ($) {
487          ## Stay in this state          ## Stay in this state
488          !!!next-input-character;          !!!next-input-character;
489          redo A;          redo A;
490        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_input_character} == -1) {
491                 $self->{next_input_character} == -1) {          !!!parse-error (type => 'unclosed tag');
492          !!!parse-error;          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
493          if ($self->{current_token}->{type} eq 'start tag') {            $self->{current_token}->{first_start_tag}
494                  = not defined $self->{last_emitted_start_tag_name};
495            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
496          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
497            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
498            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
499              !!!parse-error;              !!!parse-error (type => 'end tag attribute');
500            }            }
501          } else {          } else {
502            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 644  sub _get_next_token ($) { Line 505  sub _get_next_token ($) {
505          # reconsume          # reconsume
506    
507          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
508    
509          redo A;          redo A;
510        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_input_character} == 0x002F) { # /
511          !!!next-input-character;          !!!next-input-character;
512          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_input_character} == 0x003E and # >
513              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
514              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
515            # permitted slash            # permitted slash
516            #            #
517          } else {          } else {
518            !!!parse-error;            !!!parse-error (type => 'nestc');
519          }          }
520          $self->{state} = 'before attribute name';          $self->{state} = 'before attribute name';
521          # next-input-character is already done          # next-input-character is already done
# Line 677  sub _get_next_token ($) { Line 537  sub _get_next_token ($) {
537          !!!next-input-character;          !!!next-input-character;
538          redo A;          redo A;
539        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
540          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
541              $self->{current_token}->{first_start_tag}
542                  = not defined $self->{last_emitted_start_tag_name};
543            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
544          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
545            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
546            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
547              !!!parse-error;              !!!parse-error (type => 'end tag attribute');
548            }            }
549          } else {          } else {
550            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 691  sub _get_next_token ($) { Line 553  sub _get_next_token ($) {
553          !!!next-input-character;          !!!next-input-character;
554    
555          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
556    
557          redo A;          redo A;
558        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_input_character} and
# Line 704  sub _get_next_token ($) { Line 565  sub _get_next_token ($) {
565        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_input_character} == 0x002F) { # /
566          !!!next-input-character;          !!!next-input-character;
567          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_input_character} == 0x003E and # >
568              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
569              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
570            # permitted slash            # permitted slash
571            #            #
572          } else {          } else {
573            !!!parse-error;            !!!parse-error (type => 'nestc');
574          }          }
575          ## Stay in the state          ## Stay in the state
576          # next-input-character is already done          # next-input-character is already done
577          redo A;          redo A;
578        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_input_character} == -1) {
579                 $self->{next_input_character} == -1) {          !!!parse-error (type => 'unclosed tag');
580          !!!parse-error;          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
581          if ($self->{current_token}->{type} eq 'start tag') {            $self->{current_token}->{first_start_tag}
582                  = not defined $self->{last_emitted_start_tag_name};
583            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
584          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
585            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
586            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
587              !!!parse-error;              !!!parse-error (type => 'end tag attribute');
588            }            }
589          } else {          } else {
590            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 731  sub _get_next_token ($) { Line 593  sub _get_next_token ($) {
593          # reconsume          # reconsume
594    
595          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
596    
597          redo A;          redo A;
598        } else {        } else {
# Line 745  sub _get_next_token ($) { Line 606  sub _get_next_token ($) {
606        my $before_leave = sub {        my $before_leave = sub {
607          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
608              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
609            !!!parse-error;            !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name});
610            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
611          } else {          } else {
612            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}
# Line 769  sub _get_next_token ($) { Line 630  sub _get_next_token ($) {
630          redo A;          redo A;
631        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
632          $before_leave->();          $before_leave->();
633          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
634              $self->{current_token}->{first_start_tag}
635                  = not defined $self->{last_emitted_start_tag_name};
636            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
637          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
638            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
639            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
640              !!!parse-error;              !!!parse-error (type => 'end tag attribute');
641            }            }
642          } else {          } else {
643            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 783  sub _get_next_token ($) { Line 646  sub _get_next_token ($) {
646          !!!next-input-character;          !!!next-input-character;
647    
648          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
649    
650          redo A;          redo A;
651        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_input_character} and
# Line 796  sub _get_next_token ($) { Line 658  sub _get_next_token ($) {
658          $before_leave->();          $before_leave->();
659          !!!next-input-character;          !!!next-input-character;
660          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_input_character} == 0x003E and # >
661              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
662              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
663            # permitted slash            # permitted slash
664            #            #
665          } else {          } else {
666            !!!parse-error;            !!!parse-error (type => 'nestc');
667          }          }
668          $self->{state} = 'before attribute name';          $self->{state} = 'before attribute name';
669          # next-input-character is already done          # next-input-character is already done
670          redo A;          redo A;
671        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_input_character} == -1) {
672                 $self->{next_input_character} == -1) {          !!!parse-error (type => 'unclosed tag');
         !!!parse-error;  
673          $before_leave->();          $before_leave->();
674          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
675              $self->{current_token}->{first_start_tag}
676                  = not defined $self->{last_emitted_start_tag_name};
677            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
678          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
679            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
680            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
681              !!!parse-error;              !!!parse-error (type => 'end tag attribute');
682            }            }
683          } else {          } else {
684            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 824  sub _get_next_token ($) { Line 687  sub _get_next_token ($) {
687          # reconsume          # reconsume
688    
689          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
690    
691          redo A;          redo A;
692        } else {        } else {
# Line 847  sub _get_next_token ($) { Line 709  sub _get_next_token ($) {
709          !!!next-input-character;          !!!next-input-character;
710          redo A;          redo A;
711        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
712          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
713              $self->{current_token}->{first_start_tag}
714                  = not defined $self->{last_emitted_start_tag_name};
715            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
716          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
717            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
718            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
719              !!!parse-error;              !!!parse-error (type => 'end tag attribute');
720            }            }
721          } else {          } else {
722            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 861  sub _get_next_token ($) { Line 725  sub _get_next_token ($) {
725          !!!next-input-character;          !!!next-input-character;
726    
727          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
728    
729          redo A;          redo A;
730        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_input_character} and
# Line 874  sub _get_next_token ($) { Line 737  sub _get_next_token ($) {
737        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_input_character} == 0x002F) { # /
738          !!!next-input-character;          !!!next-input-character;
739          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_input_character} == 0x003E and # >
740              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
741              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
742            # permitted slash            # permitted slash
743            #            #
744          } else {          } else {
745            !!!parse-error;            !!!parse-error (type => 'nestc');
746              ## TODO: Different error type for <aa / bb> than <aa/>
747          }          }
748          $self->{state} = 'before attribute name';          $self->{state} = 'before attribute name';
749          # next-input-character is already done          # next-input-character is already done
750          redo A;          redo A;
751        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_input_character} == -1) {
752                 $self->{next_input_character} == -1) {          !!!parse-error (type => 'unclosed tag');
753          !!!parse-error;          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
754          if ($self->{current_token}->{type} eq 'start tag') {            $self->{current_token}->{first_start_tag}
755                  = not defined $self->{last_emitted_start_tag_name};
756            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
757          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
758            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
759            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
760              !!!parse-error;              !!!parse-error (type => 'end tag attribute');
761            }            }
762          } else {          } else {
763            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 901  sub _get_next_token ($) { Line 766  sub _get_next_token ($) {
766          # reconsume          # reconsume
767    
768          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
769    
770          redo A;          redo A;
771        } else {        } else {
# Line 933  sub _get_next_token ($) { Line 797  sub _get_next_token ($) {
797          !!!next-input-character;          !!!next-input-character;
798          redo A;          redo A;
799        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
800          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
801              $self->{current_token}->{first_start_tag}
802                  = not defined $self->{last_emitted_start_tag_name};
803            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
804          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
805            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
806            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
807              !!!parse-error;              !!!parse-error (type => 'end tag attribute');
808            }            }
809          } else {          } else {
810            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 947  sub _get_next_token ($) { Line 813  sub _get_next_token ($) {
813          !!!next-input-character;          !!!next-input-character;
814    
815          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
816    
817          redo A;          redo A;
818        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_input_character} == -1) {
819                 $self->{next_input_character} == -1) {          !!!parse-error (type => 'unclosed tag');
820          !!!parse-error;          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
821          if ($self->{current_token}->{type} eq 'start tag') {            $self->{current_token}->{first_start_tag}
822                  = 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_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
826            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
827              !!!parse-error;              !!!parse-error (type => 'end tag attribute');
828            }            }
829          } else {          } else {
830            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 967  sub _get_next_token ($) { Line 833  sub _get_next_token ($) {
833          ## reconsume          ## reconsume
834    
835          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
836    
837          redo A;          redo A;
838        } else {        } else {
# Line 987  sub _get_next_token ($) { Line 852  sub _get_next_token ($) {
852          !!!next-input-character;          !!!next-input-character;
853          redo A;          redo A;
854        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
855          !!!parse-error;          !!!parse-error (type => 'unclosed attribute value');
856          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
857              $self->{current_token}->{first_start_tag}
858                  = not defined $self->{last_emitted_start_tag_name};
859            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
860          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
861            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
862            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
863              !!!parse-error;              !!!parse-error (type => 'end tag attribute');
864            }            }
865          } else {          } else {
866            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 1002  sub _get_next_token ($) { Line 869  sub _get_next_token ($) {
869          ## reconsume          ## reconsume
870    
871          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
872    
873          redo A;          redo A;
874        } else {        } else {
# Line 1022  sub _get_next_token ($) { Line 888  sub _get_next_token ($) {
888          !!!next-input-character;          !!!next-input-character;
889          redo A;          redo A;
890        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
891          !!!parse-error;          !!!parse-error (type => 'unclosed attribute value');
892          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
893              $self->{current_token}->{first_start_tag}
894                  = not defined $self->{last_emitted_start_tag_name};
895            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
896          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
897            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
898            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
899              !!!parse-error;              !!!parse-error (type => 'end tag attribute');
900            }            }
901          } else {          } else {
902            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 1037  sub _get_next_token ($) { Line 905  sub _get_next_token ($) {
905          ## reconsume          ## reconsume
906    
907          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
908    
909          redo A;          redo A;
910        } else {        } else {
# Line 1061  sub _get_next_token ($) { Line 928  sub _get_next_token ($) {
928          !!!next-input-character;          !!!next-input-character;
929          redo A;          redo A;
930        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
931          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
932              $self->{current_token}->{first_start_tag}
933                  = not defined $self->{last_emitted_start_tag_name};
934            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
935          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
936            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
937            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
938              !!!parse-error;              !!!parse-error (type => 'end tag attribute');
939            }            }
940          } else {          } else {
941            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 1075  sub _get_next_token ($) { Line 944  sub _get_next_token ($) {
944          !!!next-input-character;          !!!next-input-character;
945    
946          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
947    
948          redo A;          redo A;
949        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_input_character} == -1) {
950                 $self->{next_input_character} == -1) {          !!!parse-error (type => 'unclosed tag');
951          !!!parse-error;          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
952          if ($self->{current_token}->{type} eq 'start tag') {            $self->{current_token}->{first_start_tag}
953                  = not defined $self->{last_emitted_start_tag_name};
954            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
955          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
956            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
957            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
958              !!!parse-error;              !!!parse-error (type => 'end tag attribute');
959            }            }
960          } else {          } else {
961            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 1095  sub _get_next_token ($) { Line 964  sub _get_next_token ($) {
964          ## reconsume          ## reconsume
965    
966          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
967    
968          redo A;          redo A;
969        } else {        } else {
# Line 1105  sub _get_next_token ($) { Line 973  sub _get_next_token ($) {
973          redo A;          redo A;
974        }        }
975      } elsif ($self->{state} eq 'entity in attribute value') {      } elsif ($self->{state} eq 'entity in attribute value') {
976        my $token = $self->_tokenize_attempt_to_consume_an_entity;        my $token = $self->_tokenize_attempt_to_consume_an_entity (1);
977    
978        unless (defined $token) {        unless (defined $token) {
979          $self->{current_attribute}->{value} .= '&';          $self->{current_attribute}->{value} .= '&';
# Line 1120  sub _get_next_token ($) { Line 988  sub _get_next_token ($) {
988      } elsif ($self->{state} eq 'bogus comment') {      } elsif ($self->{state} eq 'bogus comment') {
989        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
990                
991        my $token = {type => 'comment', data => ''};        my $token = {type => COMMENT_TOKEN, data => ''};
992    
993        BC: {        BC: {
994          if ($self->{next_input_character} == 0x003E) { # >          if ($self->{next_input_character} == 0x003E) { # >
# Line 1153  sub _get_next_token ($) { Line 1021  sub _get_next_token ($) {
1021          !!!next-input-character;          !!!next-input-character;
1022          push @next_char, $self->{next_input_character};          push @next_char, $self->{next_input_character};
1023          if ($self->{next_input_character} == 0x002D) { # -          if ($self->{next_input_character} == 0x002D) { # -
1024            $self->{current_token} = {type => 'comment', data => ''};            $self->{current_token} = {type => COMMENT_TOKEN, data => ''};
1025            $self->{state} = 'comment';            $self->{state} = 'comment start';
1026            !!!next-input-character;            !!!next-input-character;
1027            redo A;            redo A;
1028          }          }
# Line 1196  sub _get_next_token ($) { Line 1064  sub _get_next_token ($) {
1064          }          }
1065        }        }
1066    
1067        !!!parse-error;        !!!parse-error (type => 'bogus comment');
1068        $self->{next_input_character} = shift @next_char;        $self->{next_input_character} = shift @next_char;
1069        !!!back-next-input-character (@next_char);        !!!back-next-input-character (@next_char);
1070        $self->{state} = 'bogus comment';        $self->{state} = 'bogus comment';
# Line 1204  sub _get_next_token ($) { Line 1072  sub _get_next_token ($) {
1072                
1073        ## ISSUE: typos in spec: chacacters, is is a parse error        ## ISSUE: typos in spec: chacacters, is is a parse error
1074        ## 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?
1075        } elsif ($self->{state} eq 'comment start') {
1076          if ($self->{next_input_character} == 0x002D) { # -
1077            $self->{state} = 'comment start dash';
1078            !!!next-input-character;
1079            redo A;
1080          } elsif ($self->{next_input_character} == 0x003E) { # >
1081            !!!parse-error (type => 'bogus comment');
1082            $self->{state} = 'data';
1083            !!!next-input-character;
1084    
1085            !!!emit ($self->{current_token}); # comment
1086    
1087            redo A;
1088          } elsif ($self->{next_input_character} == -1) {
1089            !!!parse-error (type => 'unclosed comment');
1090            $self->{state} = 'data';
1091            ## reconsume
1092    
1093            !!!emit ($self->{current_token}); # comment
1094    
1095            redo A;
1096          } else {
1097            $self->{current_token}->{data} # comment
1098                .= chr ($self->{next_input_character});
1099            $self->{state} = 'comment';
1100            !!!next-input-character;
1101            redo A;
1102          }
1103        } elsif ($self->{state} eq 'comment start dash') {
1104          if ($self->{next_input_character} == 0x002D) { # -
1105            $self->{state} = 'comment end';
1106            !!!next-input-character;
1107            redo A;
1108          } elsif ($self->{next_input_character} == 0x003E) { # >
1109            !!!parse-error (type => 'bogus comment');
1110            $self->{state} = 'data';
1111            !!!next-input-character;
1112    
1113            !!!emit ($self->{current_token}); # comment
1114    
1115            redo A;
1116          } elsif ($self->{next_input_character} == -1) {
1117            !!!parse-error (type => 'unclosed comment');
1118            $self->{state} = 'data';
1119            ## reconsume
1120    
1121            !!!emit ($self->{current_token}); # comment
1122    
1123            redo A;
1124          } else {
1125            $self->{current_token}->{data} # comment
1126                .= '-' . chr ($self->{next_input_character});
1127            $self->{state} = 'comment';
1128            !!!next-input-character;
1129            redo A;
1130          }
1131      } elsif ($self->{state} eq 'comment') {      } elsif ($self->{state} eq 'comment') {
1132        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_input_character} == 0x002D) { # -
1133          $self->{state} = 'comment dash';          $self->{state} = 'comment end dash';
1134          !!!next-input-character;          !!!next-input-character;
1135          redo A;          redo A;
1136        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1137          !!!parse-error;          !!!parse-error (type => 'unclosed comment');
1138          $self->{state} = 'data';          $self->{state} = 'data';
1139          ## reconsume          ## reconsume
1140    
1141          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
1142    
1143          redo A;          redo A;
1144        } else {        } else {
# Line 1224  sub _get_next_token ($) { Line 1147  sub _get_next_token ($) {
1147          !!!next-input-character;          !!!next-input-character;
1148          redo A;          redo A;
1149        }        }
1150      } elsif ($self->{state} eq 'comment dash') {      } elsif ($self->{state} eq 'comment end dash') {
1151        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_input_character} == 0x002D) { # -
1152          $self->{state} = 'comment end';          $self->{state} = 'comment end';
1153          !!!next-input-character;          !!!next-input-character;
1154          redo A;          redo A;
1155        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1156          !!!parse-error;          !!!parse-error (type => 'unclosed comment');
1157          $self->{state} = 'data';          $self->{state} = 'data';
1158          ## reconsume          ## reconsume
1159    
1160          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
1161    
1162          redo A;          redo A;
1163        } else {        } else {
# Line 1250  sub _get_next_token ($) { Line 1172  sub _get_next_token ($) {
1172          !!!next-input-character;          !!!next-input-character;
1173    
1174          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
1175    
1176          redo A;          redo A;
1177        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_input_character} == 0x002D) { # -
1178          !!!parse-error;          !!!parse-error (type => 'dash in comment');
1179          $self->{current_token}->{data} .= '-'; # comment          $self->{current_token}->{data} .= '-'; # comment
1180          ## Stay in the state          ## Stay in the state
1181          !!!next-input-character;          !!!next-input-character;
1182          redo A;          redo A;
1183        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1184          !!!parse-error;          !!!parse-error (type => 'unclosed comment');
1185          $self->{state} = 'data';          $self->{state} = 'data';
1186          ## reconsume          ## reconsume
1187    
1188          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
1189    
1190          redo A;          redo A;
1191        } else {        } else {
1192          !!!parse-error;          !!!parse-error (type => 'dash in comment');
1193          $self->{current_token}->{data} .= '--' . chr ($self->{next_input_character}); # comment          $self->{current_token}->{data} .= '--' . chr ($self->{next_input_character}); # comment
1194          $self->{state} = 'comment';          $self->{state} = 'comment';
1195          !!!next-input-character;          !!!next-input-character;
# Line 1285  sub _get_next_token ($) { Line 1205  sub _get_next_token ($) {
1205          !!!next-input-character;          !!!next-input-character;
1206          redo A;          redo A;
1207        } else {        } else {
1208          !!!parse-error;          !!!parse-error (type => 'no space before DOCTYPE name');
1209          $self->{state} = 'before DOCTYPE name';          $self->{state} = 'before DOCTYPE name';
1210          ## reconsume          ## reconsume
1211          redo A;          redo A;
# Line 1299  sub _get_next_token ($) { Line 1219  sub _get_next_token ($) {
1219          ## Stay in the state          ## Stay in the state
1220          !!!next-input-character;          !!!next-input-character;
1221          redo A;          redo A;
       } elsif (0x0061 <= $self->{next_input_character} and  
                $self->{next_input_character} <= 0x007A) { # a..z  
         $self->{current_token} = {type => 'DOCTYPE',  
                           name => chr ($self->{next_input_character} - 0x0020),  
                           error => 1};  
         $self->{state} = 'DOCTYPE name';  
         !!!next-input-character;  
         redo A;  
1222        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1223          !!!parse-error;          !!!parse-error (type => 'no DOCTYPE name');
1224          $self->{state} = 'data';          $self->{state} = 'data';
1225          !!!next-input-character;          !!!next-input-character;
1226    
1227          !!!emit ({type => 'DOCTYPE', name => '', error => 1});          !!!emit ({type => DOCTYPE_TOKEN}); # incorrect
1228    
1229          redo A;          redo A;
1230        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1231          !!!parse-error;          !!!parse-error (type => 'no DOCTYPE name');
1232          $self->{state} = 'data';          $self->{state} = 'data';
1233          ## reconsume          ## reconsume
1234    
1235          !!!emit ({type => 'DOCTYPE', name => '', error => 1});          !!!emit ({type => DOCTYPE_TOKEN}); # incorrect
1236    
1237          redo A;          redo A;
1238        } else {        } else {
1239          $self->{current_token} = {type => 'DOCTYPE',          $self->{current_token}
1240                            name => chr ($self->{next_input_character}),              = {type => DOCTYPE_TOKEN,
1241                            error => 1};                 name => chr ($self->{next_input_character}),
1242                   correct => 1};
1243    ## ISSUE: "Set the token's name name to the" in the spec
1244          $self->{state} = 'DOCTYPE name';          $self->{state} = 'DOCTYPE name';
1245          !!!next-input-character;          !!!next-input-character;
1246          redo A;          redo A;
1247        }        }
1248      } elsif ($self->{state} eq 'DOCTYPE name') {      } elsif ($self->{state} eq 'DOCTYPE name') {
1249    ## ISSUE: Redundant "First," in the spec.
1250        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1251            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1252            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
1253            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
1254            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
         $self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML'); # DOCTYPE  
1255          $self->{state} = 'after DOCTYPE name';          $self->{state} = 'after DOCTYPE name';
1256          !!!next-input-character;          !!!next-input-character;
1257          redo A;          redo A;
1258        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
         $self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML'); # DOCTYPE  
1259          $self->{state} = 'data';          $self->{state} = 'data';
1260          !!!next-input-character;          !!!next-input-character;
1261    
1262          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1263    
1264          redo A;          redo A;
       } elsif (0x0061 <= $self->{next_input_character} and  
                $self->{next_input_character} <= 0x007A) { # a..z  
         $self->{current_token}->{name} .= chr ($self->{next_input_character} - 0x0020); # DOCTYPE  
         #$self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML');  
         ## Stay in the state  
         !!!next-input-character;  
         redo A;  
1265        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1266          !!!parse-error;          !!!parse-error (type => 'unclosed DOCTYPE');
         $self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML'); # DOCTYPE  
1267          $self->{state} = 'data';          $self->{state} = 'data';
1268          ## reconsume          ## reconsume
1269    
1270          !!!emit ($self->{current_token});          delete $self->{current_token}->{correct};
1271          undef $self->{current_token};          !!!emit ($self->{current_token}); # DOCTYPE
1272    
1273          redo A;          redo A;
1274        } else {        } else {
1275          $self->{current_token}->{name}          $self->{current_token}->{name}
1276            .= chr ($self->{next_input_character}); # DOCTYPE            .= chr ($self->{next_input_character}); # DOCTYPE
         #$self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML');  
1277          ## Stay in the state          ## Stay in the state
1278          !!!next-input-character;          !!!next-input-character;
1279          redo A;          redo A;
# Line 1389  sub _get_next_token ($) { Line 1292  sub _get_next_token ($) {
1292          !!!next-input-character;          !!!next-input-character;
1293    
1294          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1295    
1296          redo A;          redo A;
1297        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1298          !!!parse-error;          !!!parse-error (type => 'unclosed DOCTYPE');
1299            $self->{state} = 'data';
1300            ## reconsume
1301    
1302            delete $self->{current_token}->{correct};
1303            !!!emit ($self->{current_token}); # DOCTYPE
1304    
1305            redo A;
1306          } elsif ($self->{next_input_character} == 0x0050 or # P
1307                   $self->{next_input_character} == 0x0070) { # p
1308            !!!next-input-character;
1309            if ($self->{next_input_character} == 0x0055 or # U
1310                $self->{next_input_character} == 0x0075) { # u
1311              !!!next-input-character;
1312              if ($self->{next_input_character} == 0x0042 or # B
1313                  $self->{next_input_character} == 0x0062) { # b
1314                !!!next-input-character;
1315                if ($self->{next_input_character} == 0x004C or # L
1316                    $self->{next_input_character} == 0x006C) { # l
1317                  !!!next-input-character;
1318                  if ($self->{next_input_character} == 0x0049 or # I
1319                      $self->{next_input_character} == 0x0069) { # i
1320                    !!!next-input-character;
1321                    if ($self->{next_input_character} == 0x0043 or # C
1322                        $self->{next_input_character} == 0x0063) { # c
1323                      $self->{state} = 'before DOCTYPE public identifier';
1324                      !!!next-input-character;
1325                      redo A;
1326                    }
1327                  }
1328                }
1329              }
1330            }
1331    
1332            #
1333          } elsif ($self->{next_input_character} == 0x0053 or # S
1334                   $self->{next_input_character} == 0x0073) { # s
1335            !!!next-input-character;
1336            if ($self->{next_input_character} == 0x0059 or # Y
1337                $self->{next_input_character} == 0x0079) { # y
1338              !!!next-input-character;
1339              if ($self->{next_input_character} == 0x0053 or # S
1340                  $self->{next_input_character} == 0x0073) { # s
1341                !!!next-input-character;
1342                if ($self->{next_input_character} == 0x0054 or # T
1343                    $self->{next_input_character} == 0x0074) { # t
1344                  !!!next-input-character;
1345                  if ($self->{next_input_character} == 0x0045 or # E
1346                      $self->{next_input_character} == 0x0065) { # e
1347                    !!!next-input-character;
1348                    if ($self->{next_input_character} == 0x004D or # M
1349                        $self->{next_input_character} == 0x006D) { # m
1350                      $self->{state} = 'before DOCTYPE system identifier';
1351                      !!!next-input-character;
1352                      redo A;
1353                    }
1354                  }
1355                }
1356              }
1357            }
1358    
1359            #
1360          } else {
1361            !!!next-input-character;
1362            #
1363          }
1364    
1365          !!!parse-error (type => 'string after DOCTYPE name');
1366          $self->{state} = 'bogus DOCTYPE';
1367          # next-input-character is already done
1368          redo A;
1369        } elsif ($self->{state} eq 'before DOCTYPE public identifier') {
1370          if ({
1371                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1372                #0x000D => 1, # HT, LF, VT, FF, SP, CR
1373              }->{$self->{next_input_character}}) {
1374            ## Stay in the state
1375            !!!next-input-character;
1376            redo A;
1377          } elsif ($self->{next_input_character} eq 0x0022) { # "
1378            $self->{current_token}->{public_identifier} = ''; # DOCTYPE
1379            $self->{state} = 'DOCTYPE public identifier (double-quoted)';
1380            !!!next-input-character;
1381            redo A;
1382          } elsif ($self->{next_input_character} eq 0x0027) { # '
1383            $self->{current_token}->{public_identifier} = ''; # DOCTYPE
1384            $self->{state} = 'DOCTYPE public identifier (single-quoted)';
1385            !!!next-input-character;
1386            redo A;
1387          } elsif ($self->{next_input_character} eq 0x003E) { # >
1388            !!!parse-error (type => 'no PUBLIC literal');
1389    
1390            $self->{state} = 'data';
1391            !!!next-input-character;
1392    
1393            delete $self->{current_token}->{correct};
1394            !!!emit ($self->{current_token}); # DOCTYPE
1395    
1396            redo A;
1397          } elsif ($self->{next_input_character} == -1) {
1398            !!!parse-error (type => 'unclosed DOCTYPE');
1399    
1400            $self->{state} = 'data';
1401            ## reconsume
1402    
1403            delete $self->{current_token}->{correct};
1404            !!!emit ($self->{current_token}); # DOCTYPE
1405    
1406            redo A;
1407          } else {
1408            !!!parse-error (type => 'string after PUBLIC');
1409            $self->{state} = 'bogus DOCTYPE';
1410            !!!next-input-character;
1411            redo A;
1412          }
1413        } elsif ($self->{state} eq 'DOCTYPE public identifier (double-quoted)') {
1414          if ($self->{next_input_character} == 0x0022) { # "
1415            $self->{state} = 'after DOCTYPE public identifier';
1416            !!!next-input-character;
1417            redo A;
1418          } elsif ($self->{next_input_character} == -1) {
1419            !!!parse-error (type => 'unclosed PUBLIC literal');
1420    
1421            $self->{state} = 'data';
1422            ## reconsume
1423    
1424            delete $self->{current_token}->{correct};
1425            !!!emit ($self->{current_token}); # DOCTYPE
1426    
1427            redo A;
1428          } else {
1429            $self->{current_token}->{public_identifier} # DOCTYPE
1430                .= chr $self->{next_input_character};
1431            ## Stay in the state
1432            !!!next-input-character;
1433            redo A;
1434          }
1435        } elsif ($self->{state} eq 'DOCTYPE public identifier (single-quoted)') {
1436          if ($self->{next_input_character} == 0x0027) { # '
1437            $self->{state} = 'after DOCTYPE public identifier';
1438            !!!next-input-character;
1439            redo A;
1440          } elsif ($self->{next_input_character} == -1) {
1441            !!!parse-error (type => 'unclosed PUBLIC literal');
1442    
1443            $self->{state} = 'data';
1444            ## reconsume
1445    
1446            delete $self->{current_token}->{correct};
1447            !!!emit ($self->{current_token}); # DOCTYPE
1448    
1449            redo A;
1450          } else {
1451            $self->{current_token}->{public_identifier} # DOCTYPE
1452                .= chr $self->{next_input_character};
1453            ## Stay in the state
1454            !!!next-input-character;
1455            redo A;
1456          }
1457        } elsif ($self->{state} eq 'after DOCTYPE public identifier') {
1458          if ({
1459                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1460                #0x000D => 1, # HT, LF, VT, FF, SP, CR
1461              }->{$self->{next_input_character}}) {
1462            ## Stay in the state
1463            !!!next-input-character;
1464            redo A;
1465          } elsif ($self->{next_input_character} == 0x0022) { # "
1466            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1467            $self->{state} = 'DOCTYPE system identifier (double-quoted)';
1468            !!!next-input-character;
1469            redo A;
1470          } elsif ($self->{next_input_character} == 0x0027) { # '
1471            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1472            $self->{state} = 'DOCTYPE system identifier (single-quoted)';
1473            !!!next-input-character;
1474            redo A;
1475          } elsif ($self->{next_input_character} == 0x003E) { # >
1476            $self->{state} = 'data';
1477            !!!next-input-character;
1478    
1479            !!!emit ($self->{current_token}); # DOCTYPE
1480    
1481            redo A;
1482          } elsif ($self->{next_input_character} == -1) {
1483            !!!parse-error (type => 'unclosed DOCTYPE');
1484    
1485            $self->{state} = 'data';
1486            ## reconsume
1487    
1488            delete $self->{current_token}->{correct};
1489            !!!emit ($self->{current_token}); # DOCTYPE
1490    
1491            redo A;
1492          } else {
1493            !!!parse-error (type => 'string after PUBLIC literal');
1494            $self->{state} = 'bogus DOCTYPE';
1495            !!!next-input-character;
1496            redo A;
1497          }
1498        } elsif ($self->{state} eq 'before DOCTYPE system identifier') {
1499          if ({
1500                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1501                #0x000D => 1, # HT, LF, VT, FF, SP, CR
1502              }->{$self->{next_input_character}}) {
1503            ## Stay in the state
1504            !!!next-input-character;
1505            redo A;
1506          } elsif ($self->{next_input_character} == 0x0022) { # "
1507            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1508            $self->{state} = 'DOCTYPE system identifier (double-quoted)';
1509            !!!next-input-character;
1510            redo A;
1511          } elsif ($self->{next_input_character} == 0x0027) { # '
1512            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1513            $self->{state} = 'DOCTYPE system identifier (single-quoted)';
1514            !!!next-input-character;
1515            redo A;
1516          } elsif ($self->{next_input_character} == 0x003E) { # >
1517            !!!parse-error (type => 'no SYSTEM literal');
1518            $self->{state} = 'data';
1519            !!!next-input-character;
1520    
1521            delete $self->{current_token}->{correct};
1522            !!!emit ($self->{current_token}); # DOCTYPE
1523    
1524            redo A;
1525          } elsif ($self->{next_input_character} == -1) {
1526            !!!parse-error (type => 'unclosed DOCTYPE');
1527    
1528            $self->{state} = 'data';
1529            ## reconsume
1530    
1531            delete $self->{current_token}->{correct};
1532            !!!emit ($self->{current_token}); # DOCTYPE
1533    
1534            redo A;
1535          } else {
1536            !!!parse-error (type => 'string after SYSTEM');
1537            $self->{state} = 'bogus DOCTYPE';
1538            !!!next-input-character;
1539            redo A;
1540          }
1541        } elsif ($self->{state} eq 'DOCTYPE system identifier (double-quoted)') {
1542          if ($self->{next_input_character} == 0x0022) { # "
1543            $self->{state} = 'after DOCTYPE system identifier';
1544            !!!next-input-character;
1545            redo A;
1546          } elsif ($self->{next_input_character} == -1) {
1547            !!!parse-error (type => 'unclosed SYSTEM literal');
1548    
1549            $self->{state} = 'data';
1550            ## reconsume
1551    
1552            delete $self->{current_token}->{correct};
1553            !!!emit ($self->{current_token}); # DOCTYPE
1554    
1555            redo A;
1556          } else {
1557            $self->{current_token}->{system_identifier} # DOCTYPE
1558                .= chr $self->{next_input_character};
1559            ## Stay in the state
1560            !!!next-input-character;
1561            redo A;
1562          }
1563        } elsif ($self->{state} eq 'DOCTYPE system identifier (single-quoted)') {
1564          if ($self->{next_input_character} == 0x0027) { # '
1565            $self->{state} = 'after DOCTYPE system identifier';
1566            !!!next-input-character;
1567            redo A;
1568          } elsif ($self->{next_input_character} == -1) {
1569            !!!parse-error (type => 'unclosed SYSTEM literal');
1570    
1571            $self->{state} = 'data';
1572            ## reconsume
1573    
1574            delete $self->{current_token}->{correct};
1575            !!!emit ($self->{current_token}); # DOCTYPE
1576    
1577            redo A;
1578          } else {
1579            $self->{current_token}->{system_identifier} # DOCTYPE
1580                .= chr $self->{next_input_character};
1581            ## Stay in the state
1582            !!!next-input-character;
1583            redo A;
1584          }
1585        } elsif ($self->{state} eq 'after DOCTYPE system identifier') {
1586          if ({
1587                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1588                #0x000D => 1, # HT, LF, VT, FF, SP, CR
1589              }->{$self->{next_input_character}}) {
1590            ## Stay in the state
1591            !!!next-input-character;
1592            redo A;
1593          } elsif ($self->{next_input_character} == 0x003E) { # >
1594            $self->{state} = 'data';
1595            !!!next-input-character;
1596    
1597            !!!emit ($self->{current_token}); # DOCTYPE
1598    
1599            redo A;
1600          } elsif ($self->{next_input_character} == -1) {
1601            !!!parse-error (type => 'unclosed DOCTYPE');
1602    
1603          $self->{state} = 'data';          $self->{state} = 'data';
1604          ## reconsume          ## reconsume
1605    
1606            delete $self->{current_token}->{correct};
1607          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1608    
1609          redo A;          redo A;
1610        } else {        } else {
1611          !!!parse-error;          !!!parse-error (type => 'string after SYSTEM literal');
         $self->{current_token}->{error} = 1; # DOCTYPE  
1612          $self->{state} = 'bogus DOCTYPE';          $self->{state} = 'bogus DOCTYPE';
1613          !!!next-input-character;          !!!next-input-character;
1614          redo A;          redo A;
# Line 1413  sub _get_next_token ($) { Line 1618  sub _get_next_token ($) {
1618          $self->{state} = 'data';          $self->{state} = 'data';
1619          !!!next-input-character;          !!!next-input-character;
1620    
1621            delete $self->{current_token}->{correct};
1622          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1623    
1624          redo A;          redo A;
1625        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1626          !!!parse-error;          !!!parse-error (type => 'unclosed DOCTYPE');
1627          $self->{state} = 'data';          $self->{state} = 'data';
1628          ## reconsume          ## reconsume
1629    
1630            delete $self->{current_token}->{correct};
1631          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1632    
1633          redo A;          redo A;
1634        } else {        } else {
# Line 1439  sub _get_next_token ($) { Line 1644  sub _get_next_token ($) {
1644    die "$0: _get_next_token: unexpected case";    die "$0: _get_next_token: unexpected case";
1645  } # _get_next_token  } # _get_next_token
1646    
1647  sub _tokenize_attempt_to_consume_an_entity ($) {  sub _tokenize_attempt_to_consume_an_entity ($$) {
1648    my $self = shift;    my ($self, $in_attr) = @_;
1649      
1650    if ($self->{next_input_character} == 0x0023) { # #    if ({
1651           0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,
1652           0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR
1653          }->{$self->{next_input_character}}) {
1654        ## Don't consume
1655        ## No error
1656        return undef;
1657      } elsif ($self->{next_input_character} == 0x0023) { # #
1658      !!!next-input-character;      !!!next-input-character;
     my $num;  
1659      if ($self->{next_input_character} == 0x0078 or # x      if ($self->{next_input_character} == 0x0078 or # x
1660          $self->{next_input_character} == 0x0058) { # X          $self->{next_input_character} == 0x0058) { # X
1661          my $code;
1662        X: {        X: {
1663          my $x_char = $self->{next_input_character};          my $x_char = $self->{next_input_character};
1664          !!!next-input-character;          !!!next-input-character;
1665          if (0x0030 <= $self->{next_input_character} and          if (0x0030 <= $self->{next_input_character} and
1666              $self->{next_input_character} <= 0x0039) { # 0..9              $self->{next_input_character} <= 0x0039) { # 0..9
1667            $num ||= 0;            $code ||= 0;
1668            $num *= 0x10;            $code *= 0x10;
1669            $num += $self->{next_input_character} - 0x0030;            $code += $self->{next_input_character} - 0x0030;
1670            redo X;            redo X;
1671          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_input_character} and
1672                   $self->{next_input_character} <= 0x0066) { # a..f                   $self->{next_input_character} <= 0x0066) { # a..f
1673            ## ISSUE: the spec says U+0078, which is apparently incorrect            $code ||= 0;
1674            $num ||= 0;            $code *= 0x10;
1675            $num *= 0x10;            $code += $self->{next_input_character} - 0x0060 + 9;
           $num += $self->{next_input_character} - 0x0060 + 9;  
1676            redo X;            redo X;
1677          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_input_character} and
1678                   $self->{next_input_character} <= 0x0046) { # A..F                   $self->{next_input_character} <= 0x0046) { # A..F
1679            ## ISSUE: the spec says U+0058, which is apparently incorrect            $code ||= 0;
1680            $num ||= 0;            $code *= 0x10;
1681            $num *= 0x10;            $code += $self->{next_input_character} - 0x0040 + 9;
           $num += $self->{next_input_character} - 0x0040 + 9;  
1682            redo X;            redo X;
1683          } elsif (not defined $num) { # no hexadecimal digit          } elsif (not defined $code) { # no hexadecimal digit
1684            !!!parse-error;            !!!parse-error (type => 'bare hcro');
1685              !!!back-next-input-character ($x_char, $self->{next_input_character});
1686            $self->{next_input_character} = 0x0023; # #            $self->{next_input_character} = 0x0023; # #
           !!!back-next-input-character ($x_char);  
1687            return undef;            return undef;
1688          } elsif ($self->{next_input_character} == 0x003B) { # ;          } elsif ($self->{next_input_character} == 0x003B) { # ;
1689            !!!next-input-character;            !!!next-input-character;
1690          } else {          } else {
1691            !!!parse-error;            !!!parse-error (type => 'no refc');
1692          }          }
1693    
1694          ## TODO: check the definition for |a valid Unicode character|.          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
1695          if ($num > 1114111 or $num == 0) {            !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);
1696            $num = 0xFFFD; # REPLACEMENT CHARACTER            $code = 0xFFFD;
1697            ## ISSUE: Why this is not an error?          } elsif ($code > 0x10FFFF) {
1698              !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);
1699              $code = 0xFFFD;
1700            } elsif ($code == 0x000D) {
1701              !!!parse-error (type => 'CR character reference');
1702              $code = 0x000A;
1703            } elsif (0x80 <= $code and $code <= 0x9F) {
1704              !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);
1705              $code = $c1_entity_char->{$code};
1706          }          }
1707    
1708          return {type => 'character', data => chr $num};          return {type => CHARACTER_TOKEN, data => chr $code};
1709        } # X        } # X
1710      } elsif (0x0030 <= $self->{next_input_character} and      } elsif (0x0030 <= $self->{next_input_character} and
1711               $self->{next_input_character} <= 0x0039) { # 0..9               $self->{next_input_character} <= 0x0039) { # 0..9
# Line 1505  sub _tokenize_attempt_to_consume_an_enti Line 1723  sub _tokenize_attempt_to_consume_an_enti
1723        if ($self->{next_input_character} == 0x003B) { # ;        if ($self->{next_input_character} == 0x003B) { # ;
1724          !!!next-input-character;          !!!next-input-character;
1725        } else {        } else {
1726          !!!parse-error;          !!!parse-error (type => 'no refc');
1727        }        }
1728    
1729        ## TODO: check the definition for |a valid Unicode character|.        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
1730        if ($code > 1114111 or $code == 0) {          !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);
1731          $code = 0xFFFD; # REPLACEMENT CHARACTER          $code = 0xFFFD;
1732          ## ISSUE: Why this is not an error?        } elsif ($code > 0x10FFFF) {
1733            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);
1734            $code = 0xFFFD;
1735          } elsif ($code == 0x000D) {
1736            !!!parse-error (type => 'CR character reference');
1737            $code = 0x000A;
1738          } elsif (0x80 <= $code and $code <= 0x9F) {
1739            !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);
1740            $code = $c1_entity_char->{$code};
1741        }        }
1742                
1743        return {type => 'character', data => chr $code};        return {type => CHARACTER_TOKEN, data => chr $code};
1744      } else {      } else {
1745        !!!parse-error;        !!!parse-error (type => 'bare nero');
1746        !!!back-next-input-character ($self->{next_input_character});        !!!back-next-input-character ($self->{next_input_character});
1747        $self->{next_input_character} = 0x0023; # #        $self->{next_input_character} = 0x0023; # #
1748        return undef;        return undef;
# Line 1529  sub _tokenize_attempt_to_consume_an_enti Line 1755  sub _tokenize_attempt_to_consume_an_enti
1755      !!!next-input-character;      !!!next-input-character;
1756    
1757      my $value = $entity_name;      my $value = $entity_name;
1758      my $match;      my $match = 0;
1759        require Whatpm::_NamedEntityList;
1760        our $EntityChar;
1761    
1762      while (length $entity_name < 10 and      while (length $entity_name < 10 and
1763             ## NOTE: Some number greater than the maximum length of entity name             ## NOTE: Some number greater than the maximum length of entity name
1764             ((0x0041 <= $self->{next_input_character} and             ((0x0041 <= $self->{next_input_character} and # a
1765               $self->{next_input_character} <= 0x005A) or               $self->{next_input_character} <= 0x005A) or # x
1766              (0x0061 <= $self->{next_input_character} and              (0x0061 <= $self->{next_input_character} and # a
1767               $self->{next_input_character} <= 0x007A) or               $self->{next_input_character} <= 0x007A) or # z
1768              (0x0030 <= $self->{next_input_character} and              (0x0030 <= $self->{next_input_character} and # 0
1769               $self->{next_input_character} <= 0x0039))) {               $self->{next_input_character} <= 0x0039) or # 9
1770                $self->{next_input_character} == 0x003B)) { # ;
1771        $entity_name .= chr $self->{next_input_character};        $entity_name .= chr $self->{next_input_character};
1772        if (defined $entity_char->{$entity_name}) {        if (defined $EntityChar->{$entity_name}) {
1773          $value = $entity_char->{$entity_name};          if ($self->{next_input_character} == 0x003B) { # ;
1774          $match = 1;            $value = $EntityChar->{$entity_name};
1775              $match = 1;
1776              !!!next-input-character;
1777              last;
1778            } else {
1779              $value = $EntityChar->{$entity_name};
1780              $match = -1;
1781              !!!next-input-character;
1782            }
1783        } else {        } else {
1784          $value .= chr $self->{next_input_character};          $value .= chr $self->{next_input_character};
1785            $match *= 2;
1786            !!!next-input-character;
1787        }        }
       !!!next-input-character;  
1788      }      }
1789            
1790      if ($match) {      if ($match > 0) {
1791        if ($self->{next_input_character} == 0x003B) { # ;        return {type => CHARACTER_TOKEN, data => $value};
1792          !!!next-input-character;      } elsif ($match < 0) {
1793          !!!parse-error (type => 'no refc');
1794          if ($in_attr and $match < -1) {
1795            return {type => CHARACTER_TOKEN, data => '&'.$entity_name};
1796        } else {        } else {
1797          !!!parse-error;          return {type => CHARACTER_TOKEN, data => $value};
1798        }        }
   
       return {type => 'character', data => $value};  
1799      } else {      } else {
1800        !!!parse-error;        !!!parse-error (type => 'bare ero');
1801        ## NOTE: No characters are consumed in the spec.        ## NOTE: No characters are consumed in the spec.
1802        !!!back-token ({type => 'character', data => $value});        return {type => CHARACTER_TOKEN, data => '&'.$value};
       return undef;  
1803      }      }
1804    } else {    } else {
1805      ## no characters are consumed      ## no characters are consumed
1806      !!!parse-error;      !!!parse-error (type => 'bare ero');
1807      return undef;      return undef;
1808    }    }
1809  } # _tokenize_attempt_to_consume_an_entity  } # _tokenize_attempt_to_consume_an_entity
# Line 1576  sub _initialize_tree_constructor ($) { Line 1814  sub _initialize_tree_constructor ($) {
1814    $self->{document}->strict_error_checking (0);    $self->{document}->strict_error_checking (0);
1815    ## TODO: Turn mutation events off # MUST    ## TODO: Turn mutation events off # MUST
1816    ## TODO: Turn loose Document option (manakai extension) on    ## TODO: Turn loose Document option (manakai extension) on
1817    ## TODO: Mark the Document as an HTML document # MUST    $self->{document}->manakai_is_html (1); # MUST
1818  } # _initialize_tree_constructor  } # _initialize_tree_constructor
1819    
1820  sub _terminate_tree_constructor ($) {  sub _terminate_tree_constructor ($) {
# Line 1587  sub _terminate_tree_constructor ($) { Line 1825  sub _terminate_tree_constructor ($) {
1825    
1826  ## ISSUE: Should append_child (for example) in script executed in tree construction stage fire mutation events?  ## ISSUE: Should append_child (for example) in script executed in tree construction stage fire mutation events?
1827    
1828    { # tree construction stage
1829      my $token;
1830    
1831  sub _construct_tree ($) {  sub _construct_tree ($) {
1832    my ($self) = @_;    my ($self) = @_;
1833    
# Line 1598  sub _construct_tree ($) { Line 1839  sub _construct_tree ($) {
1839    ## characters and insert one Text node whose data is concatenation    ## characters and insert one Text node whose data is concatenation
1840    ## of all those characters. # MUST    ## of all those characters. # MUST
1841        
   my $token;  
1842    !!!next-token;    !!!next-token;
1843    
1844    my $phase = 'initial'; # MUST    $self->{insertion_mode} = BEFORE_HEAD_IM;
1845      undef $self->{form_element};
1846      undef $self->{head_element};
1847      $self->{open_elements} = [];
1848      undef $self->{inner_html_node};
1849    
1850      $self->_tree_construction_initial; # MUST
1851      $self->_tree_construction_root_element;
1852      $self->_tree_construction_main;
1853    } # _construct_tree
1854    
1855    sub _tree_construction_initial ($) {
1856      my $self = shift;
1857      INITIAL: {
1858        if ($token->{type} == DOCTYPE_TOKEN) {
1859          ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"
1860          ## error, switch to a conformance checking mode for another
1861          ## language.
1862          my $doctype_name = $token->{name};
1863          $doctype_name = '' unless defined $doctype_name;
1864          $doctype_name =~ tr/a-z/A-Z/;
1865          if (not defined $token->{name} or # <!DOCTYPE>
1866              defined $token->{public_identifier} or
1867              defined $token->{system_identifier}) {
1868            !!!parse-error (type => 'not HTML5');
1869          } elsif ($doctype_name ne 'HTML') {
1870            ## ISSUE: ASCII case-insensitive? (in fact it does not matter)
1871            !!!parse-error (type => 'not HTML5');
1872          }
1873          
1874          my $doctype = $self->{document}->create_document_type_definition
1875            ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
1876          $doctype->public_id ($token->{public_identifier})
1877              if defined $token->{public_identifier};
1878          $doctype->system_id ($token->{system_identifier})
1879              if defined $token->{system_identifier};
1880          ## NOTE: Other DocumentType attributes are null or empty lists.
1881          ## ISSUE: internalSubset = null??
1882          $self->{document}->append_child ($doctype);
1883          
1884          if (not $token->{correct} or $doctype_name ne 'HTML') {
1885            $self->{document}->manakai_compat_mode ('quirks');
1886          } elsif (defined $token->{public_identifier}) {
1887            my $pubid = $token->{public_identifier};
1888            $pubid =~ tr/a-z/A-z/;
1889            if ({
1890              "+//SILMARIL//DTD HTML PRO V0R11 19970101//EN" => 1,
1891              "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,
1892              "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,
1893              "-//IETF//DTD HTML 2.0 LEVEL 1//EN" => 1,
1894              "-//IETF//DTD HTML 2.0 LEVEL 2//EN" => 1,
1895              "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//EN" => 1,
1896              "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//EN" => 1,
1897              "-//IETF//DTD HTML 2.0 STRICT//EN" => 1,
1898              "-//IETF//DTD HTML 2.0//EN" => 1,
1899              "-//IETF//DTD HTML 2.1E//EN" => 1,
1900              "-//IETF//DTD HTML 3.0//EN" => 1,
1901              "-//IETF//DTD HTML 3.0//EN//" => 1,
1902              "-//IETF//DTD HTML 3.2 FINAL//EN" => 1,
1903              "-//IETF//DTD HTML 3.2//EN" => 1,
1904              "-//IETF//DTD HTML 3//EN" => 1,
1905              "-//IETF//DTD HTML LEVEL 0//EN" => 1,
1906              "-//IETF//DTD HTML LEVEL 0//EN//2.0" => 1,
1907              "-//IETF//DTD HTML LEVEL 1//EN" => 1,
1908              "-//IETF//DTD HTML LEVEL 1//EN//2.0" => 1,
1909              "-//IETF//DTD HTML LEVEL 2//EN" => 1,
1910              "-//IETF//DTD HTML LEVEL 2//EN//2.0" => 1,
1911              "-//IETF//DTD HTML LEVEL 3//EN" => 1,
1912              "-//IETF//DTD HTML LEVEL 3//EN//3.0" => 1,
1913              "-//IETF//DTD HTML STRICT LEVEL 0//EN" => 1,
1914              "-//IETF//DTD HTML STRICT LEVEL 0//EN//2.0" => 1,
1915              "-//IETF//DTD HTML STRICT LEVEL 1//EN" => 1,
1916              "-//IETF//DTD HTML STRICT LEVEL 1//EN//2.0" => 1,
1917              "-//IETF//DTD HTML STRICT LEVEL 2//EN" => 1,
1918              "-//IETF//DTD HTML STRICT LEVEL 2//EN//2.0" => 1,
1919              "-//IETF//DTD HTML STRICT LEVEL 3//EN" => 1,
1920              "-//IETF//DTD HTML STRICT LEVEL 3//EN//3.0" => 1,
1921              "-//IETF//DTD HTML STRICT//EN" => 1,
1922              "-//IETF//DTD HTML STRICT//EN//2.0" => 1,
1923              "-//IETF//DTD HTML STRICT//EN//3.0" => 1,
1924              "-//IETF//DTD HTML//EN" => 1,
1925              "-//IETF//DTD HTML//EN//2.0" => 1,
1926              "-//IETF//DTD HTML//EN//3.0" => 1,
1927              "-//METRIUS//DTD METRIUS PRESENTATIONAL//EN" => 1,
1928              "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//EN" => 1,
1929              "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//EN" => 1,
1930              "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//EN" => 1,
1931              "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//EN" => 1,
1932              "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//EN" => 1,
1933              "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//EN" => 1,
1934              "-//NETSCAPE COMM. CORP.//DTD HTML//EN" => 1,
1935              "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,
1936              "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,
1937              "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,
1938              "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,
1939              "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,
1940              "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,
1941              "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//EN" => 1,
1942              "-//W3C//DTD HTML 3 1995-03-24//EN" => 1,
1943              "-//W3C//DTD HTML 3.2 DRAFT//EN" => 1,
1944              "-//W3C//DTD HTML 3.2 FINAL//EN" => 1,
1945              "-//W3C//DTD HTML 3.2//EN" => 1,
1946              "-//W3C//DTD HTML 3.2S DRAFT//EN" => 1,
1947              "-//W3C//DTD HTML 4.0 FRAMESET//EN" => 1,
1948              "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN" => 1,
1949              "-//W3C//DTD HTML EXPERIMETNAL 19960712//EN" => 1,
1950              "-//W3C//DTD HTML EXPERIMENTAL 970421//EN" => 1,
1951              "-//W3C//DTD W3 HTML//EN" => 1,
1952              "-//W3O//DTD W3 HTML 3.0//EN" => 1,
1953              "-//W3O//DTD W3 HTML 3.0//EN//" => 1,
1954              "-//W3O//DTD W3 HTML STRICT 3.0//EN//" => 1,
1955              "-//WEBTECHS//DTD MOZILLA HTML 2.0//EN" => 1,
1956              "-//WEBTECHS//DTD MOZILLA HTML//EN" => 1,
1957              "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" => 1,
1958              "HTML" => 1,
1959            }->{$pubid}) {
1960              $self->{document}->manakai_compat_mode ('quirks');
1961            } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or
1962                     $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {
1963              if (defined $token->{system_identifier}) {
1964                $self->{document}->manakai_compat_mode ('quirks');
1965              } else {
1966                $self->{document}->manakai_compat_mode ('limited quirks');
1967              }
1968            } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 Frameset//EN" or
1969                     $pubid eq "-//W3C//DTD XHTML 1.0 Transitional//EN") {
1970              $self->{document}->manakai_compat_mode ('limited quirks');
1971            }
1972          }
1973          if (defined $token->{system_identifier}) {
1974            my $sysid = $token->{system_identifier};
1975            $sysid =~ tr/A-Z/a-z/;
1976            if ($sysid eq "http://www.ibm.com/data/dtd/v11/ibmxhtml1-transitional.dtd") {
1977              $self->{document}->manakai_compat_mode ('quirks');
1978            }
1979          }
1980          
1981          ## Go to the root element phase.
1982          !!!next-token;
1983          return;
1984        } elsif ({
1985                  START_TAG_TOKEN, 1,
1986                  END_TAG_TOKEN, 1,
1987                  END_OF_FILE_TOKEN, 1,
1988                 }->{$token->{type}}) {
1989          !!!parse-error (type => 'no DOCTYPE');
1990          $self->{document}->manakai_compat_mode ('quirks');
1991          ## Go to the root element phase
1992          ## reprocess
1993          return;
1994        } elsif ($token->{type} == CHARACTER_TOKEN) {
1995          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
1996            ## Ignore the token
1997    
1998            unless (length $token->{data}) {
1999              ## Stay in the phase
2000              !!!next-token;
2001              redo INITIAL;
2002            }
2003          }
2004    
2005          !!!parse-error (type => 'no DOCTYPE');
2006          $self->{document}->manakai_compat_mode ('quirks');
2007          ## Go to the root element phase
2008          ## reprocess
2009          return;
2010        } elsif ($token->{type} == COMMENT_TOKEN) {
2011          my $comment = $self->{document}->create_comment ($token->{data});
2012          $self->{document}->append_child ($comment);
2013          
2014          ## Stay in the phase.
2015          !!!next-token;
2016          redo INITIAL;
2017        } else {
2018          die "$0: $token->{type}: Unknown token type";
2019        }
2020      } # INITIAL
2021    } # _tree_construction_initial
2022    
2023    sub _tree_construction_root_element ($) {
2024      my $self = shift;
2025      
2026      B: {
2027          if ($token->{type} == DOCTYPE_TOKEN) {
2028            !!!parse-error (type => 'in html:#DOCTYPE');
2029            ## Ignore the token
2030            ## Stay in the phase
2031            !!!next-token;
2032            redo B;
2033          } elsif ($token->{type} == COMMENT_TOKEN) {
2034            my $comment = $self->{document}->create_comment ($token->{data});
2035            $self->{document}->append_child ($comment);
2036            ## Stay in the phase
2037            !!!next-token;
2038            redo B;
2039          } elsif ($token->{type} == CHARACTER_TOKEN) {
2040            if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
2041              ## Ignore the token.
2042    
2043              unless (length $token->{data}) {
2044                ## Stay in the phase
2045                !!!next-token;
2046                redo B;
2047              }
2048            }
2049            #
2050          } elsif ({
2051                    START_TAG_TOKEN, 1,
2052                    END_TAG_TOKEN, 1,
2053                    END_OF_FILE_TOKEN, 1,
2054                   }->{$token->{type}}) {
2055            ## ISSUE: There is an issue in the spec
2056            #
2057          } else {
2058            die "$0: $token->{type}: Unknown token type";
2059          }
2060          my $root_element; !!!create-element ($root_element, 'html');
2061          $self->{document}->append_child ($root_element);
2062          push @{$self->{open_elements}}, [$root_element, 'html'];
2063          ## reprocess
2064          #redo B;
2065          return; ## Go to the main phase.
2066      } # B
2067    } # _tree_construction_root_element
2068    
2069    sub _reset_insertion_mode ($) {
2070      my $self = shift;
2071    
2072        ## Step 1
2073        my $last;
2074        
2075        ## Step 2
2076        my $i = -1;
2077        my $node = $self->{open_elements}->[$i];
2078        
2079        ## Step 3
2080        S3: {
2081          ## ISSUE: Oops! "If node is the first node in the stack of open
2082          ## elements, then set last to true. If the context element of the
2083          ## HTML fragment parsing algorithm is neither a td element nor a
2084          ## th element, then set node to the context element. (fragment case)":
2085          ## The second "if" is in the scope of the first "if"!?
2086          if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
2087            $last = 1;
2088            if (defined $self->{inner_html_node}) {
2089              if ($self->{inner_html_node}->[1] eq 'td' or
2090                  $self->{inner_html_node}->[1] eq 'th') {
2091                #
2092              } else {
2093                $node = $self->{inner_html_node};
2094              }
2095            }
2096          }
2097        
2098          ## Step 4..13
2099          my $new_mode = {
2100                          select => IN_SELECT_IM,
2101                          td => IN_CELL_IM,
2102                          th => IN_CELL_IM,
2103                          tr => IN_ROW_IM,
2104                          tbody => IN_TABLE_BODY_IM,
2105                          thead => IN_TABLE_BODY_IM,
2106                          tfoot => IN_TABLE_BODY_IM,
2107                          caption => IN_CAPTION_IM,
2108                          colgroup => IN_COLUMN_GROUP_IM,
2109                          table => IN_TABLE_IM,
2110                          head => IN_BODY_IM, # not in head!
2111                          body => IN_BODY_IM,
2112                          frameset => IN_FRAMESET_IM,
2113                         }->{$node->[1]};
2114          $self->{insertion_mode} = $new_mode and return if defined $new_mode;
2115          
2116          ## Step 14
2117          if ($node->[1] eq 'html') {
2118            unless (defined $self->{head_element}) {
2119              $self->{insertion_mode} = BEFORE_HEAD_IM;
2120            } else {
2121              $self->{insertion_mode} = AFTER_HEAD_IM;
2122            }
2123            return;
2124          }
2125          
2126          ## Step 15
2127          $self->{insertion_mode} = IN_BODY_IM and return if $last;
2128          
2129          ## Step 16
2130          $i--;
2131          $node = $self->{open_elements}->[$i];
2132          
2133          ## Step 17
2134          redo S3;
2135        } # S3
2136    } # _reset_insertion_mode
2137    
2138    sub _tree_construction_main ($) {
2139      my $self = shift;
2140    
   my $open_elements = [];  
2141    my $active_formatting_elements = [];    my $active_formatting_elements = [];
   my $head_element;  
   my $form_element;  
   my $insertion_mode = 'before head';  
2142    
2143    my $reconstruct_active_formatting_elements = sub { # MUST    my $reconstruct_active_formatting_elements = sub { # MUST
2144      my $insert = shift;      my $insert = shift;
# Line 1621  sub _construct_tree ($) { Line 2152  sub _construct_tree ($) {
2152    
2153      ## Step 2      ## Step 2
2154      return if $entry->[0] eq '#marker';      return if $entry->[0] eq '#marker';
2155      for (@$open_elements) {      for (@{$self->{open_elements}}) {
2156        if ($entry->[0] eq $_->[0]) {        if ($entry->[0] eq $_->[0]) {
2157          return;          return;
2158        }        }
# Line 1640  sub _construct_tree ($) { Line 2171  sub _construct_tree ($) {
2171          #          #
2172        } else {        } else {
2173          my $in_open_elements;          my $in_open_elements;
2174          OE: for (@$open_elements) {          OE: for (@{$self->{open_elements}}) {
2175            if ($entry->[0] eq $_->[0]) {            if ($entry->[0] eq $_->[0]) {
2176              $in_open_elements = 1;              $in_open_elements = 1;
2177              last OE;              last OE;
# Line 1664  sub _construct_tree ($) { Line 2195  sub _construct_tree ($) {
2195            
2196        ## Step 9        ## Step 9
2197        $insert->($clone->[0]);        $insert->($clone->[0]);
2198        push @$open_elements, $clone;        push @{$self->{open_elements}}, $clone;
2199                
2200        ## Step 10        ## Step 10
2201        $active_formatting_elements->[$i] = $open_elements->[-1];        $active_formatting_elements->[$i] = $self->{open_elements}->[-1];
2202    
2203        ## Step 11        ## Step 11
2204        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {
# Line 1689  sub _construct_tree ($) { Line 2220  sub _construct_tree ($) {
2220      }      }
2221    }; # $clear_up_to_marker    }; # $clear_up_to_marker
2222    
2223    my $reset_insertion_mode = sub {    my $parse_rcdata = sub ($$) {
2224        my ($content_model_flag, $insert) = @_;
2225    
2226      ## Step 1      ## Step 1
2227      my $last;      my $start_tag_name = $token->{tag_name};
2228            my $el;
2229        !!!create-element ($el, $start_tag_name, $token->{attributes});
2230    
2231      ## Step 2      ## Step 2
2232      my $i = -1;      $insert->($el); # /context node/->append_child ($el)
2233      my $node = $open_elements->[$i];  
       
2234      ## Step 3      ## Step 3
2235      S3: {      $self->{content_model} = $content_model_flag; # CDATA or RCDATA
2236        $last = 1 if $open_elements->[0]->[0] eq $node->[0];      delete $self->{escape}; # MUST
       ## TODO: the element whose inner_html is set is neither td nor th, then $node = the element  
       
       ## Step 4..13  
       my $new_mode = {  
                       select => 'in select',  
                       td => 'in cell',  
                       th => 'in cell',  
                       tr => 'in row',  
                       tbody => 'in table body',  
                       thead => 'in table head',  
                       tfoot => 'in table foot',  
                       caption => 'in caption',  
                       colgroup => 'in column group',  
                       table => 'in table',  
                       head => 'in body', # not in head!  
                       body => 'in body',  
                       frameset => 'in frameset',  
                      }->{$node->[1]};  
       $insertion_mode = $new_mode and return if defined $new_mode;  
         
       ## Step 14  
       if ($node->[1] eq 'html') {  
         unless (defined $head_element) {  
           $insertion_mode = 'before head';  
         } else {  
           $insertion_mode = 'after head';  
         }  
         return;  
       }  
         
       ## Step 15  
       $insertion_mode = 'in body' and return if $last;  
         
       ## Step 16  
       $i--;  
       $node = $open_elements->[$i];  
         
       ## Step 17  
       redo S3;  
     } # S3  
   }; # $reset_insertion_mode  
2237    
2238    my $style_start_tag = sub {      ## Step 4
     my $style_el; !!!create-element ($style_el, 'style');  
     ## $insertion_mode eq 'in head' and ... (always true)  
     (($insertion_mode eq 'in head' and defined $head_element)  
      ? $head_element : $open_elements->[-1]->[0])  
       ->append_child ($style_el);  
     $self->{content_model_flag} = 'CDATA';  
                 
2239      my $text = '';      my $text = '';
2240      !!!next-token;      !!!next-token;
2241      while ($token->{type} eq 'character') {      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
2242        $text .= $token->{data};        $text .= $token->{data};
2243        !!!next-token;        !!!next-token;
2244      } # stop if non-character token or tokenizer stops tokenising      }
2245    
2246        ## Step 5
2247      if (length $text) {      if (length $text) {
2248        $style_el->manakai_append_text ($text);        my $text = $self->{document}->create_text_node ($text);
2249          $el->append_child ($text);
2250      }      }
2251        
2252      $self->{content_model_flag} = 'PCDATA';      ## Step 6
2253                      $self->{content_model} = PCDATA_CONTENT_MODEL;
2254      if ($token->{type} eq 'end tag' and $token->{tag_name} eq 'style') {  
2255        ## Step 7
2256        if ($token->{type} == END_TAG_TOKEN and $token->{tag_name} eq $start_tag_name) {
2257        ## Ignore the token        ## Ignore the token
2258        } elsif ($content_model_flag == CDATA_CONTENT_MODEL) {
2259          !!!parse-error (type => 'in CDATA:#'.$token->{type});
2260        } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
2261          !!!parse-error (type => 'in RCDATA:#'.$token->{type});
2262      } else {      } else {
2263        !!!parse-error;        die "$0: $content_model_flag in parse_rcdata";
       ## ISSUE: And ignore?  
2264      }      }
2265      !!!next-token;      !!!next-token;
2266    }; # $style_start_tag    }; # $parse_rcdata
2267    
2268    my $script_start_tag = sub {    my $script_start_tag = sub ($) {
2269        my $insert = $_[0];
2270      my $script_el;      my $script_el;
2271      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, 'script', $token->{attributes});
2272      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
2273    
2274      $self->{content_model_flag} = 'CDATA';      $self->{content_model} = CDATA_CONTENT_MODEL;
2275        delete $self->{escape}; # MUST
2276            
2277      my $text = '';      my $text = '';
2278      !!!next-token;      !!!next-token;
2279      while ($token->{type} eq 'character') {      while ($token->{type} == CHARACTER_TOKEN) {
2280        $text .= $token->{data};        $text .= $token->{data};
2281        !!!next-token;        !!!next-token;
2282      } # stop if non-character token or tokenizer stops tokenising      } # stop if non-character token or tokenizer stops tokenising
# Line 1788  sub _construct_tree ($) { Line 2284  sub _construct_tree ($) {
2284        $script_el->manakai_append_text ($text);        $script_el->manakai_append_text ($text);
2285      }      }
2286                                
2287      $self->{content_model_flag} = 'PCDATA';      $self->{content_model} = PCDATA_CONTENT_MODEL;
2288    
2289      if ($token->{type} eq 'end tag' and      if ($token->{type} == END_TAG_TOKEN and
2290          $token->{tag_name} eq 'script') {          $token->{tag_name} eq 'script') {
2291        ## Ignore the token        ## Ignore the token
2292      } else {      } else {
2293        !!!parse-error;        !!!parse-error (type => 'in CDATA:#'.$token->{type});
2294        ## ISSUE: And ignore?        ## ISSUE: And ignore?
2295        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
2296      }      }
2297            
2298      ## TODO: inner_html mode then mark as "already executed" and skip      if (defined $self->{inner_html_node}) {
2299      if (1) {        ## TODO: mark as "already executed"
2300        } else {
2301        ## TODO: $old_insertion_point = current insertion point        ## TODO: $old_insertion_point = current insertion point
2302        ## TODO: insertion point = just before the next input character        ## TODO: insertion point = just before the next input character
2303          
2304        (($insertion_mode eq 'in head' and defined $head_element)        $insert->($script_el);
        ? $head_element : $open_elements->[-1]->[0])->append_child ($script_el);  
2305                
2306        ## TODO: insertion point = $old_insertion_point (might be "undefined")        ## TODO: insertion point = $old_insertion_point (might be "undefined")
2307                
# Line 1832  sub _construct_tree ($) { Line 2328  sub _construct_tree ($) {
2328          }          }
2329        } # AFE        } # AFE
2330        unless (defined $formatting_element) {        unless (defined $formatting_element) {
2331          !!!parse-error;          !!!parse-error (type => 'unmatched end tag:'.$tag_name);
2332          ## Ignore the token          ## Ignore the token
2333          !!!next-token;          !!!next-token;
2334          return;          return;
# Line 1840  sub _construct_tree ($) { Line 2336  sub _construct_tree ($) {
2336        ## has an element in scope        ## has an element in scope
2337        my $in_scope = 1;        my $in_scope = 1;
2338        my $formatting_element_i_in_open;          my $formatting_element_i_in_open;  
2339        INSCOPE: for (reverse 0..$#$open_elements) {        INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
2340          my $node = $open_elements->[$_];          my $node = $self->{open_elements}->[$_];
2341          if ($node->[0] eq $formatting_element->[0]) {          if ($node->[0] eq $formatting_element->[0]) {
2342            if ($in_scope) {            if ($in_scope) {
2343              $formatting_element_i_in_open = $_;              $formatting_element_i_in_open = $_;
2344              last INSCOPE;              last INSCOPE;
2345            } else { # in open elements but not in scope            } else { # in open elements but not in scope
2346              !!!parse-error;              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
2347              ## Ignore the token              ## Ignore the token
2348              !!!next-token;              !!!next-token;
2349              return;              return;
# Line 1860  sub _construct_tree ($) { Line 2356  sub _construct_tree ($) {
2356          }          }
2357        } # INSCOPE        } # INSCOPE
2358        unless (defined $formatting_element_i_in_open) {        unless (defined $formatting_element_i_in_open) {
2359          !!!parse-error;          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
2360          pop @$active_formatting_elements; # $formatting_element          pop @$active_formatting_elements; # $formatting_element
2361          !!!next-token; ## TODO: ok?          !!!next-token; ## TODO: ok?
2362          return;          return;
2363        }        }
2364        if (not $open_elements->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
2365          !!!parse-error;          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
2366        }        }
2367                
2368        ## Step 2        ## Step 2
2369        my $furthest_block;        my $furthest_block;
2370        my $furthest_block_i_in_open;        my $furthest_block_i_in_open;
2371        OE: for (reverse 0..$#$open_elements) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
2372          my $node = $open_elements->[$_];          my $node = $self->{open_elements}->[$_];
2373          if (not $formatting_category->{$node->[1]} and          if (not $formatting_category->{$node->[1]} and
2374              #not $phrasing_category->{$node->[1]} and              #not $phrasing_category->{$node->[1]} and
2375              ($special_category->{$node->[1]} or              ($special_category->{$node->[1]} or
# Line 1887  sub _construct_tree ($) { Line 2383  sub _construct_tree ($) {
2383                
2384        ## Step 3        ## Step 3
2385        unless (defined $furthest_block) { # MUST        unless (defined $furthest_block) { # MUST
2386          splice @$open_elements, $formatting_element_i_in_open;          splice @{$self->{open_elements}}, $formatting_element_i_in_open;
2387          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;
2388          !!!next-token;          !!!next-token;
2389          return;          return;
2390        }        }
2391                
2392        ## Step 4        ## Step 4
2393        my $common_ancestor_node = $open_elements->[$formatting_element_i_in_open - 1];        my $common_ancestor_node = $self->{open_elements}->[$formatting_element_i_in_open - 1];
2394                
2395        ## Step 5        ## Step 5
2396        my $furthest_block_parent = $furthest_block->[0]->parent_node;        my $furthest_block_parent = $furthest_block->[0]->parent_node;
# Line 1914  sub _construct_tree ($) { Line 2410  sub _construct_tree ($) {
2410        S7: {        S7: {
2411          ## Step 1          ## Step 1
2412          $node_i_in_open--;          $node_i_in_open--;
2413          $node = $open_elements->[$node_i_in_open];          $node = $self->{open_elements}->[$node_i_in_open];
2414                    
2415          ## Step 2          ## Step 2
2416          my $node_i_in_active;          my $node_i_in_active;
# Line 1925  sub _construct_tree ($) { Line 2421  sub _construct_tree ($) {
2421                last S7S2;                last S7S2;
2422              }              }
2423            }            }
2424            splice @$open_elements, $node_i_in_open, 1;            splice @{$self->{open_elements}}, $node_i_in_open, 1;
2425            redo S7;            redo S7;
2426          } # S7S2          } # S7S2
2427                    
# Line 1941  sub _construct_tree ($) { Line 2437  sub _construct_tree ($) {
2437          if ($node->[0]->has_child_nodes ()) {          if ($node->[0]->has_child_nodes ()) {
2438            my $clone = [$node->[0]->clone_node (0), $node->[1]];            my $clone = [$node->[0]->clone_node (0), $node->[1]];
2439            $active_formatting_elements->[$node_i_in_active] = $clone;            $active_formatting_elements->[$node_i_in_active] = $clone;
2440            $open_elements->[$node_i_in_open] = $clone;            $self->{open_elements}->[$node_i_in_open] = $clone;
2441            $node = $clone;            $node = $clone;
2442          }          }
2443                    
# Line 1983  sub _construct_tree ($) { Line 2479  sub _construct_tree ($) {
2479                
2480        ## Step 13        ## Step 13
2481        undef $i;        undef $i;
2482        OE: for (reverse 0..$#$open_elements) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
2483          if ($open_elements->[$_]->[0] eq $formatting_element->[0]) {          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {
2484            splice @$open_elements, $_, 1;            splice @{$self->{open_elements}}, $_, 1;
2485            $i-- and last OE if defined $i;            $i-- and last OE if defined $i;
2486          } elsif ($open_elements->[$_]->[0] eq $furthest_block->[0]) {          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {
2487            $i = $_;            $i = $_;
2488          }          }
2489        } # OE        } # OE
2490        splice @$open_elements, $i + 1, 1, $clone;        splice @{$self->{open_elements}}, $i + 1, 1, $clone;
2491                
2492        ## Step 14        ## Step 14
2493        redo FET;        redo FET;
# Line 1999  sub _construct_tree ($) { Line 2495  sub _construct_tree ($) {
2495    }; # $formatting_end_tag    }; # $formatting_end_tag
2496    
2497    my $insert_to_current = sub {    my $insert_to_current = sub {
2498      $open_elements->[-1]->[0]->append_child (shift);      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);
2499    }; # $insert_to_current    }; # $insert_to_current
2500    
2501    my $insert_to_foster = sub {    my $insert_to_foster = sub {
# Line 2007  sub _construct_tree ($) { Line 2503  sub _construct_tree ($) {
2503                         if ({                         if ({
2504                              table => 1, tbody => 1, tfoot => 1,                              table => 1, tbody => 1, tfoot => 1,
2505                              thead => 1, tr => 1,                              thead => 1, tr => 1,
2506                             }->{$open_elements->[-1]->[1]}) {                             }->{$self->{open_elements}->[-1]->[1]}) {
2507                           # MUST                           # MUST
2508                           my $foster_parent_element;                           my $foster_parent_element;
2509                           my $next_sibling;                           my $next_sibling;
2510                           OE: for (reverse 0..$#$open_elements) {                           OE: for (reverse 0..$#{$self->{open_elements}}) {
2511                             if ($open_elements->[$_]->[1] eq 'table') {                             if ($self->{open_elements}->[$_]->[1] eq 'table') {
2512                               my $parent = $open_elements->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
2513                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
2514                                 $foster_parent_element = $parent;                                 $foster_parent_element = $parent;
2515                                 $next_sibling = $open_elements->[$_]->[0];                                 $next_sibling = $self->{open_elements}->[$_]->[0];
2516                               } else {                               } else {
2517                                 $foster_parent_element                                 $foster_parent_element
2518                                   = $open_elements->[$_ - 1]->[0];                                   = $self->{open_elements}->[$_ - 1]->[0];
2519                               }                               }
2520                               last OE;                               last OE;
2521                             }                             }
2522                           } # OE                           } # OE
2523                           $foster_parent_element = $open_elements->[0]->[0]                           $foster_parent_element = $self->{open_elements}->[0]->[0]
2524                             unless defined $foster_parent_element;                             unless defined $foster_parent_element;
2525                           $foster_parent_element->insert_before                           $foster_parent_element->insert_before
2526                             ($child, $next_sibling);                             ($child, $next_sibling);
2527                         } else {                         } else {
2528                           $open_elements->[-1]->[0]->append_child ($child);                           $self->{open_elements}->[-1]->[0]->append_child ($child);
2529                         }                         }
2530    }; # $insert_to_foster    }; # $insert_to_foster
2531    
2532    my $in_body = sub {    my $insert;
     my $insert = shift;  
     if ($token->{type} eq 'start tag') {  
       if ($token->{tag_name} eq 'script') {  
         $script_start_tag->();  
         return;  
       } elsif ($token->{tag_name} eq 'style') {  
         $style_start_tag->();  
         return;  
       } elsif ({  
                 base => 1, link => 1, meta => 1,  
                }->{$token->{tag_name}}) {  
         !!!parse-error ($token->{tag_name}.' in body');  
         ## NOTE: This is an "as if in head" code clone  
         my $el;  
         !!!create-element ($el, $token->{tag_name}, $token->{attributes});  
         if (defined $head_element) {  
           $head_element->append_child ($el);  
         } else {  
           $insert->($el);  
         }  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'title') {  
         !!!parse-error ('title in body');  
         ## NOTE: There is an "as if in head" code clone  
         my $title_el;  
         !!!create-element ($title_el, 'title', $token->{attributes});  
         (defined $head_element ? $head_element : $open_elements->[-1]->[0])  
           ->append_child ($title_el);  
         $self->{content_model_flag} = 'RCDATA';  
           
         my $text = '';  
         !!!next-token;  
         while ($token->{type} eq 'character') {  
           $text .= $token->{data};  
           !!!next-token;  
         }  
         if (length $text) {  
           $title_el->manakai_append_text ($text);  
         }  
           
         $self->{content_model_flag} = 'PCDATA';  
           
         if ($token->{type} eq 'end tag' and  
             $token->{tag_name} eq 'title') {  
           ## Ignore the token  
         } else {  
           !!!parse-error;  
           ## ISSUE: And ignore?  
         }  
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'body') {  
         !!!parse-error;  
                 
         if (@$open_elements == 1 or  
             $open_elements->[1]->[1] ne 'body') {  
           ## Ignore the token  
         } else {  
           my $body_el = $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 @$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 $form_element) {  
           !!!parse-error;  
           ## Ignore the token  
         } else {  
           ## has a p element in scope  
           INSCOPE: for (reverse @$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});  
           $form_element = $open_elements->[-1]->[0];  
           !!!next-token;  
           return;  
         }  
       } elsif ($token->{tag_name} eq 'li') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @$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 = $open_elements->[$i];  
         LI: {  
           ## Step 2  
           if ($node->[1] eq 'li') {  
             splice @$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 = $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 @$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 = $open_elements->[$i];  
         LI: {  
           ## Step 2  
           if ($node->[1] eq 'dt' or $node->[1] eq 'dd') {  
             splice @$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 = $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 @$open_elements) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         $self->{content_model_flag} = 'PLAINTEXT';  
             
         !!!next-token;  
         return;  
       } elsif ({  
                 h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
                }->{$token->{tag_name}}) {  
         ## has a p element in scope  
         INSCOPE: for (reverse 0..$#$open_elements) {  
           my $node = $open_elements->[$_];  
           if ($node->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## has an element in scope  
         my $i;  
         INSCOPE: for (reverse 0..$#$open_elements) {  
           my $node = $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;  
           splice @$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 ('a in 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..$#$open_elements) {  
               if ($open_elements->[$_]->[0] eq $node->[0]) {  
                 splice @$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, $open_elements->[-1];  
   
         !!!next-token;  
         return;  
       } elsif ({  
                 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}}) {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, $open_elements->[-1];  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'button') {  
         ## has a button element in scope  
         INSCOPE: for (reverse 0..$#$open_elements) {  
           my $node = $open_elements->[$_];  
           if ($node->[1] eq 'button') {  
             !!!parse-error;  
             !!!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', ''];  
2533    
2534          !!!next-token;    B: {
2535          return;      if ($token->{type} == DOCTYPE_TOKEN) {
2536        } elsif ($token->{tag_name} eq 'marquee' or        !!!parse-error (type => 'DOCTYPE in the middle');
2537                 $token->{tag_name} eq 'object') {        ## Ignore the token
2538          $reconstruct_active_formatting_elements->($insert_to_current);        ## Stay in the phase
2539                  !!!next-token;
2540          !!!insert-element-t ($token->{tag_name}, $token->{attributes});        redo B;
2541          push @$active_formatting_elements, ['#marker', ''];      } elsif ($token->{type} == END_OF_FILE_TOKEN) {
2542                  if ($self->{insertion_mode} & AFTER_HTML_IMS) {
2543          !!!next-token;          #
         return;  
       } elsif ($token->{tag_name} eq 'xmp') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
           
         $self->{content_model_flag} = 'CDATA';  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'table') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @$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});  
             
         $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;  
           $token->{tag_name} = 'img';  
         }  
           
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         pop @$open_elements;  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'hr') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @$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 @$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 $form_element if defined  
         pop @$open_elements;  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'isindex') {  
         !!!parse-error;  
           
         if (defined $form_element) {  
           ## Ignore the token  
           !!!next-token;  
           return;  
         } else {  
           my $at = $token->{attributes};  
           $at->{name} = {name => 'name', value => 'isindex'};  
           my @tokens = (  
                         {type => 'start tag', tag_name => 'form'},  
                         {type => 'start tag', tag_name => 'hr'},  
                         {type => 'start tag', tag_name => 'p'},  
                         {type => 'start tag', tag_name => 'label'},  
                         {type => 'character',  
                          data => 'This is a searchable index. Insert your search keywords here: '}, # SHOULD  
                         ## TODO: make this configurable  
                         {type => 'start tag', tag_name => 'input', attributes => $at},  
                         #{type => 'character', data => ''}, # SHOULD  
                         {type => 'end tag', tag_name => 'label'},  
                         {type => 'end tag', tag_name => 'p'},  
                         {type => 'start tag', tag_name => 'hr'},  
                         {type => 'end tag', tag_name => 'form'},  
                        );  
           $token = shift @tokens;  
           !!!back-token (@tokens);  
           return;  
         }  
       } elsif ({  
                 textarea => 1,  
                 noembed => 1,  
                 noframes => 1,  
                 noscript => 0, ## TODO: 1 if scripting is enabled  
                }->{$token->{tag_name}}) {  
         my $tag_name = $token->{tag_name};  
         my $el;  
         !!!create-element ($el, $token->{tag_name}, $token->{attributes});  
           
         if ($token->{tag_name} eq 'textarea') {  
           ## TODO: form_element if defined  
           $self->{content_model_flag} = 'RCDATA';  
         } else {  
           $self->{content_model_flag} = 'CDATA';  
         }  
           
         $insert->($el);  
           
         my $text = '';  
         !!!next-token;  
         while ($token->{type} eq 'character') {  
           $text .= $token->{data};  
           !!!next-token;  
         }  
         if (length $text) {  
           $el->manakai_append_text ($text);  
         }  
           
         $self->{content_model_flag} = 'PCDATA';  
           
         if ($token->{type} eq 'end tag' and  
             $token->{tag_name} eq $tag_name) {  
           ## Ignore the token  
         } else {  
           !!!parse-error;  
           ## ISSUE: And ignore?  
         }  
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'select') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
           
         $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;  
         ## Ignore the token  
         !!!next-token;  
         return;  
           
         ## ISSUE: An issue on HTML5 new elements in the spec.  
2544        } else {        } else {
2545          $reconstruct_active_formatting_elements->($insert_to_current);          ## Generate implied end tags
2546                    if ({
2547          !!!insert-element-t ($token->{tag_name}, $token->{attributes});               dd => 1, dt => 1, li => 1, p => 1, td => 1, th => 1, tr => 1,
2548                         tbody => 1, tfoot=> 1, thead => 1,
2549          !!!next-token;              }->{$self->{open_elements}->[-1]->[1]}) {
2550          return;            !!!back-token;
2551        }            $token = {type => END_TAG_TOKEN, tag_name => $self->{open_elements}->[-1]->[1]};
2552      } elsif ($token->{type} eq 'end tag') {            redo B;
       if ($token->{tag_name} eq 'body') {  
         if (@$open_elements > 1 and $open_elements->[1]->[1] eq 'body') {  
           ## ISSUE: There is an issue in the spec.  
           if ($open_elements->[-1]->[1] ne 'body') {  
             !!!parse-error;  
           }  
           $insertion_mode = 'after body';  
           !!!next-token;  
           return;  
         } else {  
           !!!parse-error;  
           ## Ignore the token  
           !!!next-token;  
           return;  
         }  
       } elsif ($token->{tag_name} eq 'html') {  
         if (@$open_elements > 1 and $open_elements->[1]->[1] eq 'body') {  
           ## ISSUE: There is an issue in the spec.  
           if ($open_elements->[-1]->[1] ne 'body') {  
             !!!parse-error;  
           }  
           $insertion_mode = 'after body';  
           ## reprocess  
           return;  
         } else {  
           !!!parse-error;  
           ## 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,  
                 form => 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..$#$open_elements) {  
           my $node = $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,  
                 }->{$open_elements->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => 'end tag',  
                         tag_name => $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 ($open_elements->[-1]->[1] ne $token->{tag_name}) {  
           !!!parse-error;  
2553          }          }
2554                    
2555          splice @$open_elements, $i if defined $i;          if (@{$self->{open_elements}} > 2 or
2556          undef $form_element if $token->{tag_name} eq 'form';              (@{$self->{open_elements}} == 2 and $self->{open_elements}->[1]->[1] ne 'body')) {
2557          $clear_up_to_marker->()            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
2558            if {          } elsif (defined $self->{inner_html_node} and
2559              button => 1, marquee => 1, object => 1,                   @{$self->{open_elements}} > 1 and
2560            }->{$token->{tag_name}};                   $self->{open_elements}->[1]->[1] ne 'body') {
2561          !!!next-token;            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
         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..$#$open_elements) {  
           my $node = $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,  
                 }->{$open_elements->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => 'end tag',  
                         tag_name => $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 ($open_elements->[-1]->[1] ne $token->{tag_name}) {  
           !!!parse-error;  
2562          }          }
           
         splice @$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 ({  
                 caption => 1, col => 1, colgroup => 1, frame => 1,  
                 frameset => 1, head => 1, option => 1, optgroup => 1,  
                 tbody => 1, td => 1, tfoot => 1, th => 1,  
                 thead => 1, tr => 1,  
                 area => 1, basefont => 1, bgsound => 1, br => 1,  
                 embed => 1, hr => 1, iframe => 1, image => 1,  
                 img => 1, input => 1, isindex=> 1, noembed => 1,  
                 noframes => 1, param => 1, select => 1, spacer => 1,  
                 table => 1, textarea => 1, wbr => 1,  
                 noscript => 0, ## TODO: if scripting is enabled  
                }->{$token->{tag_name}}) {  
         !!!parse-error;  
         ## Ignore the token  
         !!!next-token;  
         return;  
           
         ## ISSUE: Issue on HTML5 new elements in spec  
           
       } else {  
         ## Step 1  
         my $node_i = -1;  
         my $node = $open_elements->[$node_i];  
2563    
2564          ## Step 2          ## ISSUE: There is an issue in the spec.
         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,  
                 }->{$open_elements->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => 'end tag',  
                         tag_name => $open_elements->[-1]->[1]}; # MUST  
               return;  
             }  
           
             ## Step 2  
             if ($token->{tag_name} ne $open_elements->[-1]->[1]) {  
               !!!parse-error;  
             }  
               
             ## Step 3  
             splice @$open_elements, $node_i;  
             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;  
               ## Ignore the token  
               !!!next-token;  
               last S2;  
             }  
           }  
             
           ## Step 4  
           $node_i--;  
           $node = $open_elements->[$node_i];  
             
           ## Step 5;  
           redo S2;  
         } # S2  
2565        }        }
     }  
   }; # $in_body  
2566    
2567    B: {        ## Stop parsing
2568      if ($phase eq 'initial') {        last B;
2569        if ($token->{type} eq 'DOCTYPE') {      } elsif ($token->{type} == START_TAG_TOKEN and
2570          if ($token->{error}) {               $token->{tag_name} eq 'html') {
2571            ## ISSUE: Spec currently left this case undefined.        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
2572            !!!parse-error ('bogus DOCTYPE');          ## Turn into the main phase
2573          }          !!!parse-error (type => 'after html:html');
2574          my $doctype = $self->{document}->create_document_type_definition          $self->{insertion_mode} = AFTER_BODY_IM;
2575            ($token->{name});        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
2576          $self->{document}->append_child ($doctype);          ## Turn into the main phase
2577          $phase = 'root element';          !!!parse-error (type => 'after html:html');
2578          !!!next-token;          $self->{insertion_mode} = AFTER_FRAMESET_IM;
2579          redo B;        }
2580        } elsif ({  
2581                  comment => 1,  ## ISSUE: "aa<html>" is not a parse error.
2582                  'start tag' => 1,  ## ISSUE: "<html>" in fragment is not a parse error.
2583                  'end tag' => 1,        unless ($token->{first_start_tag}) {
2584                  'end-of-file' => 1,          !!!parse-error (type => 'not first start tag');
2585                 }->{$token->{type}}) {        }
2586          ## ISSUE: Spec currently left this case undefined.        my $top_el = $self->{open_elements}->[0]->[0];
2587          !!!parse-error ('missing DOCTYPE');        for my $attr_name (keys %{$token->{attributes}}) {
2588          $phase = 'root element';          unless ($top_el->has_attribute_ns (undef, $attr_name)) {
2589          ## reprocess            $top_el->set_attribute_ns
2590          redo B;              (undef, [undef, $attr_name],
2591        } elsif ($token->{type} eq 'character') {               $token->{attributes}->{$attr_name}->{value});
         if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
           $self->{document}->manakai_append_text ($1);  
           ## ISSUE: DOM3 Core does not allow Document > Text  
           unless (length $token->{data}) {  
             ## Stay in the phase  
             !!!next-token;  
             redo B;  
           }  
2592          }          }
         ## ISSUE: Spec currently left this case undefined.  
         !!!parse-error ('missing DOCTYPE');  
         $phase = 'root element';  
         ## reprocess  
         redo B;  
       } else {  
         die "$0: $token->{type}: Unknown token";  
2593        }        }
2594      } elsif ($phase eq 'root element') {        !!!next-token;
2595        if ($token->{type} eq 'DOCTYPE') {        redo B;
2596          !!!parse-error;      } elsif ($token->{type} == COMMENT_TOKEN) {
2597          ## Ignore the token        my $comment = $self->{document}->create_comment ($token->{data});
2598          ## Stay in the phase        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
         !!!next-token;  
         redo B;  
       } elsif ($token->{type} eq 'comment') {  
         my $comment = $self->{document}->create_comment ($token->{data});  
2599          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
2600          ## Stay in the phase        } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {
2601          !!!next-token;          $self->{open_elements}->[0]->[0]->append_child ($comment);
2602          redo B;        } else {
2603        } elsif ($token->{type} eq 'character') {          $self->{open_elements}->[-1]->[0]->append_child ($comment);
2604          }
2605          !!!next-token;
2606          redo B;
2607        } elsif ($self->{insertion_mode} & HEAD_IMS) {
2608          if ($token->{type} == CHARACTER_TOKEN) {
2609          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
2610            $self->{document}->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
           ## ISSUE: DOM3 Core does not allow Document > Text  
2611            unless (length $token->{data}) {            unless (length $token->{data}) {
             ## Stay in the phase  
2612              !!!next-token;              !!!next-token;
2613              redo B;              redo B;
2614            }            }
2615          }          }
         #  
       } elsif ({  
                 'start tag' => 1,  
                 'end tag' => 1,  
                 'end-of-file' => 1,  
                }->{$token->{type}}) {  
         ## ISSUE: There is an issue in the spec  
         #  
       } else {  
         die "$0: $token->{type}: Unknown token";  
       }  
       my $root_element; !!!create-element ($root_element, 'html');  
       $self->{document}->append_child ($root_element);  
       $open_elements = [[$root_element, 'html']];  
       $phase = 'main';  
       ## reprocess  
       redo B;  
     } elsif ($phase eq 'main') {  
       if ($token->{type} eq 'DOCTYPE') {  
         !!!parse-error;  
         ## Ignore the token  
         ## Stay in the phase  
         !!!next-token;  
         redo B;  
       } elsif ($token->{type} eq 'start tag' and  
                $token->{tag_name} eq 'html') {  
         ## TODO: unless it is the first start tag token, parse-error  
         my $top_el = $open_elements->[0]->[0];  
         for my $attr_name (keys %{$token->{attributes}}) {  
           unless ($top_el->has_attribute_ns (undef, $attr_name)) {  
             $top_el->set_attribute_ns  
               (undef, [undef, $attr_name],  
                $token->{attributes}->{$attr_name}->{value});  
           }  
         }  
         !!!next-token;  
         redo B;  
       } elsif ($token->{type} eq 'end-of-file') {  
         ## Generate implied end tags  
         if ({  
              dd => 1, dt => 1, li => 1, p => 1, td => 1, th => 1, tr => 1,  
             }->{$open_elements->[-1]->[1]}) {  
           !!!back-token;  
           $token = {type => 'end tag', tag_name => $open_elements->[-1]->[1]};  
           redo B;  
         }  
           
         if (@$open_elements > 2 or  
             (@$open_elements == 2 and $open_elements->[1]->[1] ne 'body')) {  
           !!!parse-error;  
         } else {  
           ## TODO: inner_html parser and @$open_elements > 1 and $open_elements->[1] ne 'body', then parse-error  
         }  
2616    
2617          ## Stop parsing          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2618          last B;            ## As if <head>
2619              !!!create-element ($self->{head_element}, 'head');
2620              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2621              push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2622    
2623              ## Reprocess in the "in head" insertion mode...
2624              pop @{$self->{open_elements}};
2625    
2626              ## Reprocess in the "after head" insertion mode...
2627            } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2628              ## As if </noscript>
2629              pop @{$self->{open_elements}};
2630              !!!parse-error (type => 'in noscript:#character');
2631              
2632              ## Reprocess in the "in head" insertion mode...
2633              ## As if </head>
2634              pop @{$self->{open_elements}};
2635    
2636          ## ISSUE: There is an issue in the spec.            ## Reprocess in the "after head" insertion mode...
2637        } else {          } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2638          if ($insertion_mode eq 'before head') {            pop @{$self->{open_elements}};
2639            if ($token->{type} eq 'character') {  
2640              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {            ## Reprocess in the "after head" insertion mode...
2641                $open_elements->[-1]->[0]->manakai_append_text ($1);          }
2642                unless (length $token->{data}) {  
2643                  !!!next-token;              ## "after head" insertion mode
2644                  redo B;              ## As if <body>
2645                }              !!!insert-element ('body');
2646              }              $self->{insertion_mode} = IN_BODY_IM;
             ## As if <head>  
             !!!create-element ($head_element, 'head');  
             $open_elements->[-1]->[0]->append_child ($head_element);  
             push @$open_elements, [$head_element, 'head'];  
             $insertion_mode = 'in head';  
2647              ## reprocess              ## reprocess
2648              redo B;              redo B;
2649            } elsif ($token->{type} eq 'comment') {            } elsif ($token->{type} == START_TAG_TOKEN) {
             my $comment = $self->{document}->create_comment ($token->{data});  
             $open_elements->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             my $attr = $token->{tag_name} eq 'head' ? $token->{attributes} : {};  
             !!!create-element ($head_element, 'head', $attr);  
             $open_elements->[-1]->[0]->append_child ($head_element);  
             push @$open_elements, [$head_element, 'head'];  
             $insertion_mode = 'in head';  
2650              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
2651                !!!next-token;                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2652              #} elsif ({                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});
2653              #          base => 1, link => 1, meta => 1,                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2654              #          script => 1, style => 1, title => 1,                  push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];
2655              #         }->{$token->{tag_name}}) {                  $self->{insertion_mode} = IN_HEAD_IM;
2656              #  ## reprocess                  !!!next-token;
2657              } else {                  redo B;
2658                ## reprocess                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
2659              }                  #
2660              redo B;                } else {
2661            } elsif ($token->{type} eq 'end tag') {                  !!!parse-error (type => 'in head:head'); # or in head noscript
2662              if ($token->{tag_name} eq 'html') {                  ## Ignore the token
               ## As if <head>  
               !!!create-element ($head_element, 'head');  
               $open_elements->[-1]->[0]->append_child ($head_element);  
               push @$open_elements, [$head_element, 'head'];  
               $insertion_mode = 'in head';  
               ## reprocess  
               redo B;  
             } else {  
               !!!parse-error;  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
             }  
           } else {  
             die "$0: $token->{type}: Unknown type";  
           }  
         } elsif ($insertion_mode eq 'in head') {  
           if ($token->{type} eq 'character') {  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $open_elements->[-1]->[0]->manakai_append_text ($1);  
               unless (length $token->{data}) {  
2663                  !!!next-token;                  !!!next-token;
2664                  redo B;                  redo B;
2665                }                }
2666                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2667                  ## As if <head>
2668                  !!!create-element ($self->{head_element}, 'head');
2669                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2670                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2671    
2672                  $self->{insertion_mode} = IN_HEAD_IM;
2673                  ## Reprocess in the "in head" insertion mode...
2674              }              }
               
             #  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $open_elements->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'title') {  
               ## NOTE: There is an "as if in head" code clone  
               my $title_el;  
               !!!create-element ($title_el, 'title', $token->{attributes});  
               (defined $head_element ? $head_element : $open_elements->[-1]->[0])  
                 ->append_child ($title_el);  
               $self->{content_model_flag} = 'RCDATA';  
2675    
2676                my $text = '';              if ($token->{tag_name} eq 'base') {
2677                !!!next-token;                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2678                while ($token->{type} eq 'character') {                  ## As if </noscript>
2679                  $text .= $token->{data};                  pop @{$self->{open_elements}};
2680                  !!!next-token;                  !!!parse-error (type => 'in noscript:base');
               }  
               if (length $text) {  
                 $title_el->manakai_append_text ($text);  
               }  
                 
               $self->{content_model_flag} = 'PCDATA';  
2681                                
2682                if ($token->{type} eq 'end tag' and                  $self->{insertion_mode} = IN_HEAD_IM;
2683                    $token->{tag_name} eq 'title') {                  ## Reprocess in the "in head" insertion mode...
                 ## Ignore the token  
               } else {  
                 !!!parse-error;  
                 ## ISSUE: And ignore?  
2684                }                }
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'style') {  
               $style_start_tag->();  
               redo B;  
             } elsif ($token->{tag_name} eq 'script') {  
               $script_start_tag->();  
               redo B;  
             } elsif ({base => 1, link => 1, meta => 1}->{$token->{tag_name}}) {  
               ## NOTE: There are "as if in head" code clones  
               my $el;  
               !!!create-element ($el, $token->{tag_name}, $token->{attributes});  
               (defined $head_element ? $head_element : $open_elements->[-1]->[0])  
                 ->append_child ($el);  
2685    
2686                !!!next-token;                ## NOTE: There is a "as if in head" code clone.
2687                redo B;                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2688              } elsif ($token->{tag_name} eq 'head') {                  !!!parse-error (type => 'after head:'.$token->{tag_name});
2689                !!!parse-error;                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
               ## Ignore the token  
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'head') {  
               if ($open_elements->[-1]->[1] eq 'head') {  
                 pop @$open_elements;  
               } else {  
                 !!!parse-error;  
2690                }                }
2691                $insertion_mode = 'after head';                !!!insert-element ($token->{tag_name}, $token->{attributes});
2692                !!!next-token;                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
2693                redo B;                pop @{$self->{open_elements}}
2694              } elsif ($token->{tag_name} eq 'html') {                    if $self->{insertion_mode} == AFTER_HEAD_IM;
               #  
             } else {  
               !!!parse-error;  
               ## Ignore the token  
2695                !!!next-token;                !!!next-token;
2696                redo B;                redo B;
2697              }              } elsif ($token->{tag_name} eq 'link') {
2698            } else {                ## NOTE: There is a "as if in head" code clone.
2699              #                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2700            }                  !!!parse-error (type => 'after head:'.$token->{tag_name});
2701                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
           if ($open_elements->[-1]->[1] eq 'head') {  
             ## As if </head>  
             pop @$open_elements;  
           }  
           $insertion_mode = 'after head';  
           ## reprocess  
           redo B;  
   
           ## ISSUE: An issue in the spec.  
         } elsif ($insertion_mode eq 'after head') {  
           if ($token->{type} eq 'character') {  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $open_elements->[-1]->[0]->manakai_append_text ($1);  
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
2702                }                }
2703              }                !!!insert-element ($token->{tag_name}, $token->{attributes});
2704                              pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
2705              #                pop @{$self->{open_elements}}
2706            } elsif ($token->{type} eq 'comment') {                    if $self->{insertion_mode} == AFTER_HEAD_IM;
             my $comment = $self->{document}->create_comment ($token->{data});  
             $open_elements->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'body') {  
               !!!insert-element ('body', $token->{attributes});  
               $insertion_mode = 'in body';  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'frameset') {  
               !!!insert-element ('frameset', $token->{attributes});  
               $insertion_mode = 'in frameset';  
2707                !!!next-token;                !!!next-token;
2708                redo B;                redo B;
2709              } elsif ({              } elsif ($token->{tag_name} eq 'meta') {
2710                        base => 1, link => 1, meta => 1,                ## NOTE: There is a "as if in head" code clone.
2711                        script=> 1, style => 1, title => 1,                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2712                       }->{$token->{tag_name}}) {                  !!!parse-error (type => 'after head:'.$token->{tag_name});
2713                !!!parse-error;                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
                $insertion_mode = 'in head';  
               ## reprocess  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
             
           ## As if <body>  
           !!!insert-element ('body');  
           $insertion_mode = 'in body';  
           ## reprocess  
           redo B;  
         } elsif ($insertion_mode eq 'in body') {  
           if ($token->{type} eq 'character') {  
             ## NOTE: There is a code clone of "character in body".  
             $reconstruct_active_formatting_elements->($insert_to_current);  
               
             $open_elements->[-1]->[0]->manakai_append_text ($token->{data});  
   
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             ## NOTE: There is a code clone of "comment in body".  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $open_elements->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } else {  
             $in_body->($insert_to_current);  
             redo B;  
           }  
         } elsif ($insertion_mode eq 'in table') {  
           if ($token->{type} eq 'character') {  
             ## NOTE: There are "character in table" code clones.  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $open_elements->[-1]->[0]->manakai_append_text ($1);  
                 
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
2714                }                }
2715              }                !!!insert-element ($token->{tag_name}, $token->{attributes});
2716                  pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
2717    
2718              ## As if in body, but insert into foster parent element                unless ($self->{confident}) {
2719              ## ISSUE: Spec says that "whenever a node would be inserted                  my $charset;
2720              ## into the current node" while characters might not be                  if ($token->{attributes}->{charset}) { ## TODO: And if supported
2721              ## result in a new Text node.                    $charset = $token->{attributes}->{charset}->{value};
             $reconstruct_active_formatting_elements->($insert_to_foster);  
               
             if ({  
                  table => 1, tbody => 1, tfoot => 1,  
                  thead => 1, tr => 1,  
                 }->{$open_elements->[-1]->[1]}) {  
               # MUST  
               my $foster_parent_element;  
               my $next_sibling;  
               my $prev_sibling;  
               OE: for (reverse 0..$#$open_elements) {  
                 if ($open_elements->[$_]->[1] eq 'table') {  
                   my $parent = $open_elements->[$_]->[0]->parent_node;  
                   if (defined $parent and $parent->node_type == 1) {  
                     $foster_parent_element = $parent;  
                     $next_sibling = $open_elements->[$_]->[0];  
                     $prev_sibling = $next_sibling->previous_sibling;  
                   } else {  
                     $foster_parent_element = $open_elements->[$_ - 1]->[0];  
                     $prev_sibling = $foster_parent_element->last_child;  
                   }  
                   last OE;  
2722                  }                  }
2723                } # OE                  if ($token->{attributes}->{'http-equiv'}) {
2724                $foster_parent_element = $open_elements->[0]->[0] and                    ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.
2725                $prev_sibling = $foster_parent_element->last_child                    if ($token->{attributes}->{'http-equiv'}->{value}
2726                  unless defined $foster_parent_element;                        =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=
2727                if (defined $prev_sibling and                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
2728                    $prev_sibling->node_type == 3) {                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
2729                  $prev_sibling->manakai_append_text ($token->{data});                      $charset = defined $1 ? $1 : defined $2 ? $2 : $3;
2730                } else {                    } ## TODO: And if supported
2731                  $foster_parent_element->insert_before                  }
2732                    ($self->{document}->create_text_node ($token->{data}),                  ## TODO: Change the encoding
                    $next_sibling);  
               }  
             } else {  
               $open_elements->[-1]->[0]->manakai_append_text ($token->{data});  
             }  
               
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $open_elements->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ({  
                  caption => 1,  
                  colgroup => 1,  
                  tbody => 1, tfoot => 1, thead => 1,  
                 }->{$token->{tag_name}}) {  
               ## Clear back to table context  
               while ($open_elements->[-1]->[1] ne 'table' and  
                      $open_elements->[-1]->[1] ne 'html') {  
                 !!!parse-error;  
                 pop @$open_elements;  
2733                }                }
2734    
2735                push @$active_formatting_elements, ['#marker', '']                ## TODO: Extracting |charset| from |meta|.
2736                  if $token->{tag_name} eq 'caption';                pop @{$self->{open_elements}}
2737                      if $self->{insertion_mode} == AFTER_HEAD_IM;
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               $insertion_mode = {  
                                  caption => 'in caption',  
                                  colgroup => 'in column group',  
                                  tbody => 'in table body',  
                                  tfoot => 'in table body',  
                                  thead => 'in table body',  
                                 }->{$token->{tag_name}};  
2738                !!!next-token;                !!!next-token;
2739                redo B;                redo B;
2740              } elsif ({              } elsif ($token->{tag_name} eq 'title') {
2741                        col => 1,                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2742                        td => 1, th => 1, tr => 1,                  ## As if </noscript>
2743                       }->{$token->{tag_name}}) {                  pop @{$self->{open_elements}};
2744                ## Clear back to table context                  !!!parse-error (type => 'in noscript:title');
2745                while ($open_elements->[-1]->[1] ne 'table' and                
2746                       $open_elements->[-1]->[1] ne 'html') {                  $self->{insertion_mode} = IN_HEAD_IM;
2747                  !!!parse-error;                  ## Reprocess in the "in head" insertion mode...
2748                  pop @$open_elements;                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
2749                    !!!parse-error (type => 'after head:'.$token->{tag_name});
2750                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2751                }                }
2752    
2753                !!!insert-element ($token->{tag_name} eq 'col' ? 'colgroup' : 'tbody');                ## NOTE: There is a "as if in head" code clone.
2754                $insertion_mode = $token->{tag_name} eq 'col'                my $parent = defined $self->{head_element} ? $self->{head_element}
2755                  ? 'in column group' : 'in table body';                    : $self->{open_elements}->[-1]->[0];
2756                ## reprocess                $parse_rcdata->(RCDATA_CONTENT_MODEL,
2757                                  sub { $parent->append_child ($_[0]) });
2758                  pop @{$self->{open_elements}}
2759                      if $self->{insertion_mode} == AFTER_HEAD_IM;
2760                redo B;                redo B;
2761              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'style') {
2762                ## NOTE: There are code clones for this "table in table"                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
2763                !!!parse-error;                ## insertion mode IN_HEAD_IM)
2764                  ## NOTE: There is a "as if in head" code clone.
2765                ## As if </table>                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2766                ## have a table element in table scope                  !!!parse-error (type => 'after head:'.$token->{tag_name});
2767                my $i;                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2768                INSCOPE: for (reverse 0..$#$open_elements) {                }
2769                  my $node = $open_elements->[$_];                $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);
2770                  if ($node->[1] eq 'table') {                pop @{$self->{open_elements}}
2771                    $i = $_;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
2772                    last INSCOPE;                redo B;
2773                  } elsif ({              } elsif ($token->{tag_name} eq 'noscript') {
2774                            table => 1, html => 1,                if ($self->{insertion_mode} == IN_HEAD_IM) {
2775                           }->{$node->[1]}) {                  ## NOTE: and scripting is disalbed
2776                    last INSCOPE;                  !!!insert-element ($token->{tag_name}, $token->{attributes});
2777                  }                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error;  
                 ## Ignore tokens </table><table>  
2778                  !!!next-token;                  !!!next-token;
2779                  redo B;                  redo B;
2780                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2781                    !!!parse-error (type => 'in noscript:noscript');
2782                    ## Ignore the token
2783                    !!!next-token;
2784                    redo B;
2785                  } else {
2786                    #
2787                }                }
2788                } elsif ($token->{tag_name} eq 'script') {
2789                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2790                    ## As if </noscript>
2791                    pop @{$self->{open_elements}};
2792                    !!!parse-error (type => 'in noscript:script');
2793                                
2794                ## generate implied end tags                  $self->{insertion_mode} = IN_HEAD_IM;
2795                if ({                  ## Reprocess in the "in head" insertion mode...
2796                     dd => 1, dt => 1, li => 1, p => 1,                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
2797                     td => 1, th => 1, tr => 1,                  !!!parse-error (type => 'after head:'.$token->{tag_name});
2798                    }->{$open_elements->[-1]->[1]}) {                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2799                  !!!back-token; # <table>                }
2800                  $token = {type => 'end tag', tag_name => 'table'};  
2801                  !!!back-token;                ## NOTE: There is a "as if in head" code clone.
2802                  $token = {type => 'end tag',                $script_start_tag->($insert_to_current);
2803                            tag_name => $open_elements->[-1]->[1]}; # MUST                pop @{$self->{open_elements}}
2804                  redo B;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
2805                  redo B;
2806                } elsif ($token->{tag_name} eq 'body' or
2807                         $token->{tag_name} eq 'frameset') {
2808                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2809                    ## As if </noscript>
2810                    pop @{$self->{open_elements}};
2811                    !!!parse-error (type => 'in noscript:'.$token->{tag_name});
2812                    
2813                    ## Reprocess in the "in head" insertion mode...
2814                    ## As if </head>
2815                    pop @{$self->{open_elements}};
2816                    
2817                    ## Reprocess in the "after head" insertion mode...
2818                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2819                    pop @{$self->{open_elements}};
2820                    
2821                    ## Reprocess in the "after head" insertion mode...
2822                }                }
2823    
2824                if ($open_elements->[-1]->[1] ne 'table') {                ## "after head" insertion mode
2825                  !!!parse-error;                !!!insert-element ($token->{tag_name}, $token->{attributes});
2826                  if ($token->{tag_name} eq 'body') {
2827                    $self->{insertion_mode} = IN_BODY_IM;
2828                  } elsif ($token->{tag_name} eq 'frameset') {
2829                    $self->{insertion_mode} = IN_FRAMESET_IM;
2830                  } else {
2831                    die "$0: tag name: $self->{tag_name}";
2832                }                }
2833                  !!!next-token;
               splice @$open_elements, $i;  
   
               $reset_insertion_mode->();                
   
               ## reprocess  
2834                redo B;                redo B;
2835              } else {              } else {
2836                #                #
2837              }              }
2838            } elsif ($token->{type} eq 'end tag') {  
2839              if ($token->{tag_name} eq 'table') {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2840                ## have a table element in table scope                ## As if </noscript>
2841                my $i;                pop @{$self->{open_elements}};
2842                INSCOPE: for (reverse 0..$#$open_elements) {                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});
2843                  my $node = $open_elements->[$_];                
2844                  if ($node->[1] eq $token->{tag_name}) {                ## Reprocess in the "in head" insertion mode...
2845                    $i = $_;                ## As if </head>
2846                    last INSCOPE;                pop @{$self->{open_elements}};
2847                  } elsif ({  
2848                            table => 1, html => 1,                ## Reprocess in the "after head" insertion mode...
2849                           }->{$node->[1]}) {              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2850                    last INSCOPE;                ## As if </head>
2851                  }                pop @{$self->{open_elements}};
2852                } # INSCOPE  
2853                unless (defined $i) {                ## Reprocess in the "after head" insertion mode...
2854                  !!!parse-error;              }
2855                  ## Ignore the token  
2856                ## "after head" insertion mode
2857                ## As if <body>
2858                !!!insert-element ('body');
2859                $self->{insertion_mode} = IN_BODY_IM;
2860                ## reprocess
2861                redo B;
2862              } elsif ($token->{type} == END_TAG_TOKEN) {
2863                if ($token->{tag_name} eq 'head') {
2864                  if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2865                    ## As if <head>
2866                    !!!create-element ($self->{head_element}, 'head');
2867                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2868                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2869    
2870                    ## Reprocess in the "in head" insertion mode...
2871                    pop @{$self->{open_elements}};
2872                    $self->{insertion_mode} = AFTER_HEAD_IM;
2873                    !!!next-token;
2874                    redo B;
2875                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2876                    ## As if </noscript>
2877                    pop @{$self->{open_elements}};
2878                    !!!parse-error (type => 'in noscript:script');
2879                    
2880                    ## Reprocess in the "in head" insertion mode...
2881                    pop @{$self->{open_elements}};
2882                    $self->{insertion_mode} = AFTER_HEAD_IM;
2883                    !!!next-token;
2884                    redo B;
2885                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2886                    pop @{$self->{open_elements}};
2887                    $self->{insertion_mode} = AFTER_HEAD_IM;
2888                  !!!next-token;                  !!!next-token;
2889                  redo B;                  redo B;
2890                  } else {
2891                    #
2892                }                }
2893                              } elsif ($token->{tag_name} eq 'noscript') {
2894                ## generate implied end tags                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2895                if ({                  pop @{$self->{open_elements}};
2896                     dd => 1, dt => 1, li => 1, p => 1,                  $self->{insertion_mode} = IN_HEAD_IM;
2897                     td => 1, th => 1, tr => 1,                  !!!next-token;
2898                    }->{$open_elements->[-1]->[1]}) {                  redo B;
2899                  !!!back-token;                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2900                  $token = {type => 'end tag',                  !!!parse-error (type => 'unmatched end tag:noscript');
2901                            tag_name => $open_elements->[-1]->[1]}; # MUST                  ## Ignore the token ## ISSUE: An issue in the spec.
2902                    !!!next-token;
2903                  redo B;                  redo B;
2904                  } else {
2905                    #
2906                }                }
2907                } elsif ({
2908                if ($open_elements->[-1]->[1] ne 'table') {                        body => 1, html => 1,
2909                  !!!parse-error;                       }->{$token->{tag_name}}) {
2910                  if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2911                    ## As if <head>
2912                    !!!create-element ($self->{head_element}, 'head');
2913                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2914                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2915    
2916                    $self->{insertion_mode} = IN_HEAD_IM;
2917                    ## Reprocess in the "in head" insertion mode...
2918                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2919                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
2920                    ## Ignore the token
2921                    !!!next-token;
2922                    redo B;
2923                }                }
2924                  
2925                  #
2926                } elsif ({
2927                          p => 1, br => 1,
2928                         }->{$token->{tag_name}}) {
2929                  if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2930                    ## As if <head>
2931                    !!!create-element ($self->{head_element}, 'head');
2932                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2933                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2934    
2935                splice @$open_elements, $i;                  $self->{insertion_mode} = IN_HEAD_IM;
2936                    ## Reprocess in the "in head" insertion mode...
2937                  }
2938    
2939                $reset_insertion_mode->();                #
2940                } else {
2941                  if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2942                    #
2943                  } else {
2944                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
2945                    ## Ignore the token
2946                    !!!next-token;
2947                    redo B;
2948                  }
2949                }
2950    
2951                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2952                  ## As if </noscript>
2953                  pop @{$self->{open_elements}};
2954                  !!!parse-error (type => 'in noscript:/'.$token->{tag_name});
2955                  
2956                  ## Reprocess in the "in head" insertion mode...
2957                  ## As if </head>
2958                  pop @{$self->{open_elements}};
2959    
2960                  ## Reprocess in the "after head" insertion mode...
2961                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2962                  ## As if </head>
2963                  pop @{$self->{open_elements}};
2964    
2965                  ## Reprocess in the "after head" insertion mode...
2966                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2967                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
2968                  ## Ignore the token ## ISSUE: An issue in the spec.
2969                !!!next-token;                !!!next-token;
2970                redo B;                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;  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
             } else {  
               #  
2971              }              }
2972    
2973                ## "after head" insertion mode
2974                ## As if <body>
2975                !!!insert-element ('body');
2976                $self->{insertion_mode} = IN_BODY_IM;
2977                ## reprocess
2978                redo B;
2979            } else {            } else {
2980              #              die "$0: $token->{type}: Unknown token type";
2981            }            }
2982    
2983            !!!parse-error;            ## ISSUE: An issue in the spec.
2984            $in_body->($insert_to_foster);      } elsif ($self->{insertion_mode} & BODY_IMS) {
2985            redo B;            if ($token->{type} == CHARACTER_TOKEN) {
2986          } elsif ($insertion_mode eq 'in caption') {              ## NOTE: There is a code clone of "character in body".
           if ($token->{type} eq 'character') {  
             ## NOTE: This is a code clone of "character in body".  
2987              $reconstruct_active_formatting_elements->($insert_to_current);              $reconstruct_active_formatting_elements->($insert_to_current);
2988                            
2989              $open_elements->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
2990    
2991              !!!next-token;              !!!next-token;
2992              redo B;              redo B;
2993            } elsif ($token->{type} eq 'comment') {            } elsif ($token->{type} == START_TAG_TOKEN) {
             ## NOTE: This is a code clone of "comment in body".  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $open_elements->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
2994              if ({              if ({
2995                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
2996                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,
2997                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
2998                !!!parse-error;                if ($self->{insertion_mode} == IN_CELL_IM) {
2999                    ## have an element in table scope
3000                    my $tn;
3001                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3002                      my $node = $self->{open_elements}->[$_];
3003                      if ($node->[1] eq 'td' or $node->[1] eq 'th') {
3004                        $tn = $node->[1];
3005                        last INSCOPE;
3006                      } elsif ({
3007                                table => 1, html => 1,
3008                               }->{$node->[1]}) {
3009                        last INSCOPE;
3010                      }
3011                    } # INSCOPE
3012                      unless (defined $tn) {
3013                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3014                        ## Ignore the token
3015                        !!!next-token;
3016                        redo B;
3017                      }
3018                    
3019                    ## Close the cell
3020                    !!!back-token; # <?>
3021                    $token = {type => END_TAG_TOKEN, tag_name => $tn};
3022                    redo B;
3023                  } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
3024                    !!!parse-error (type => 'not closed:caption');
3025                    
3026                    ## As if </caption>
3027                    ## have a table element in table scope
3028                    my $i;
3029                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3030                      my $node = $self->{open_elements}->[$_];
3031                      if ($node->[1] eq 'caption') {
3032                        $i = $_;
3033                        last INSCOPE;
3034                      } elsif ({
3035                                table => 1, html => 1,
3036                               }->{$node->[1]}) {
3037                        last INSCOPE;
3038                      }
3039                    } # INSCOPE
3040                      unless (defined $i) {
3041                        !!!parse-error (type => 'unmatched end tag:caption');
3042                        ## Ignore the token
3043                        !!!next-token;
3044                        redo B;
3045                      }
3046                    
3047                    ## generate implied end tags
3048                    if ({
3049                         dd => 1, dt => 1, li => 1, p => 1,
3050                         td => 1, th => 1, tr => 1,
3051                         tbody => 1, tfoot=> 1, thead => 1,
3052                        }->{$self->{open_elements}->[-1]->[1]}) {
3053                      !!!back-token; # <?>
3054                      $token = {type => END_TAG_TOKEN, tag_name => 'caption'};
3055                      !!!back-token;
3056                      $token = {type => END_TAG_TOKEN,
3057                                tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3058                      redo B;
3059                    }
3060    
3061                ## As if </caption>                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {
3062                ## have a table element in table scope                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
               my $i;  
               INSCOPE: for (reverse 0..$#$open_elements) {  
                 my $node = $open_elements->[$_];  
                 if ($node->[1] eq 'caption') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
3063                  }                  }
3064                } # INSCOPE                  
3065                unless (defined $i) {                  splice @{$self->{open_elements}}, $i;
3066                  !!!parse-error;                  
3067                    $clear_up_to_marker->();
3068                    
3069                    $self->{insertion_mode} = IN_TABLE_IM;
3070                    
3071                    ## reprocess
3072                    redo B;
3073                  } else {
3074                    #
3075                  }
3076                } else {
3077                  #
3078                }
3079              } elsif ($token->{type} == END_TAG_TOKEN) {
3080                if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {
3081                  if ($self->{insertion_mode} == IN_CELL_IM) {
3082                    ## have an element in table scope
3083                    my $i;
3084                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3085                      my $node = $self->{open_elements}->[$_];
3086                      if ($node->[1] eq $token->{tag_name}) {
3087                        $i = $_;
3088                        last INSCOPE;
3089                      } elsif ({
3090                                table => 1, html => 1,
3091                               }->{$node->[1]}) {
3092                        last INSCOPE;
3093                      }
3094                    } # INSCOPE
3095                      unless (defined $i) {
3096                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3097                        ## Ignore the token
3098                        !!!next-token;
3099                        redo B;
3100                      }
3101                    
3102                    ## generate implied end tags
3103                    if ({
3104                         dd => 1, dt => 1, li => 1, p => 1,
3105                         td => ($token->{tag_name} eq 'th'),
3106                         th => ($token->{tag_name} eq 'td'),
3107                         tr => 1,
3108                         tbody => 1, tfoot=> 1, thead => 1,
3109                        }->{$self->{open_elements}->[-1]->[1]}) {
3110                      !!!back-token;
3111                      $token = {type => END_TAG_TOKEN,
3112                                tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3113                      redo B;
3114                    }
3115                    
3116                    if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
3117                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3118                    }
3119                    
3120                    splice @{$self->{open_elements}}, $i;
3121                    
3122                    $clear_up_to_marker->();
3123                    
3124                    $self->{insertion_mode} = IN_ROW_IM;
3125                    
3126                    !!!next-token;
3127                    redo B;
3128                  } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
3129                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3130                  ## Ignore the token                  ## Ignore the token
3131                  !!!next-token;                  !!!next-token;
3132                  redo B;                  redo B;
3133                  } else {
3134                    #
3135                }                }
3136                              } elsif ($token->{tag_name} eq 'caption') {
3137                ## generate implied end tags                if ($self->{insertion_mode} == IN_CAPTION_IM) {
3138                if ({                  ## have a table element in table scope
3139                     dd => 1, dt => 1, li => 1, p => 1,                  my $i;
3140                     td => 1, th => 1, tr => 1,                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3141                    }->{$open_elements->[-1]->[1]}) {                    my $node = $self->{open_elements}->[$_];
3142                  !!!back-token; # <?>                    if ($node->[1] eq $token->{tag_name}) {
3143                  $token = {type => 'end tag', tag_name => 'caption'};                      $i = $_;
3144                  !!!back-token;                      last INSCOPE;
3145                  $token = {type => 'end tag',                    } elsif ({
3146                            tag_name => $open_elements->[-1]->[1]}; # MUST                              table => 1, html => 1,
3147                               }->{$node->[1]}) {
3148                        last INSCOPE;
3149                      }
3150                    } # INSCOPE
3151                      unless (defined $i) {
3152                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3153                        ## Ignore the token
3154                        !!!next-token;
3155                        redo B;
3156                      }
3157                    
3158                    ## generate implied end tags
3159                    if ({
3160                         dd => 1, dt => 1, li => 1, p => 1,
3161                         td => 1, th => 1, tr => 1,
3162                         tbody => 1, tfoot=> 1, thead => 1,
3163                        }->{$self->{open_elements}->[-1]->[1]}) {
3164                      !!!back-token;
3165                      $token = {type => END_TAG_TOKEN,
3166                                tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3167                      redo B;
3168                    }
3169                    
3170                    if ($self->{open_elements}->[-1]->[1] ne 'caption') {
3171                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3172                    }
3173                    
3174                    splice @{$self->{open_elements}}, $i;
3175                    
3176                    $clear_up_to_marker->();
3177                    
3178                    $self->{insertion_mode} = IN_TABLE_IM;
3179                    
3180                    !!!next-token;
3181                  redo B;                  redo B;
3182                  } elsif ($self->{insertion_mode} == IN_CELL_IM) {
3183                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3184                    ## Ignore the token
3185                    !!!next-token;
3186                    redo B;
3187                  } else {
3188                    #
3189                }                }
3190                } elsif ({
3191                if ($open_elements->[-1]->[1] ne 'caption') {                        table => 1, tbody => 1, tfoot => 1,
3192                  !!!parse-error;                        thead => 1, tr => 1,
3193                }                       }->{$token->{tag_name}} and
3194                         $self->{insertion_mode} == IN_CELL_IM) {
3195                splice @$open_elements, $i;                ## have an element in table scope
   
               $clear_up_to_marker->();  
   
               $insertion_mode = 'in table';  
   
               ## reprocess  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'caption') {  
               ## have a table element in table scope  
3196                my $i;                my $i;
3197                INSCOPE: for (reverse 0..$#$open_elements) {                my $tn;
3198                  my $node = $open_elements->[$_];                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3199                    my $node = $self->{open_elements}->[$_];
3200                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq $token->{tag_name}) {
3201                    $i = $_;                    $i = $_;
3202                    last INSCOPE;                    last INSCOPE;
3203                    } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {
3204                      $tn = $node->[1];
3205                      ## NOTE: There is exactly one |td| or |th| element
3206                      ## in scope in the stack of open elements by definition.
3207                  } elsif ({                  } elsif ({
3208                            table => 1, html => 1,                            table => 1, html => 1,
3209                           }->{$node->[1]}) {                           }->{$node->[1]}) {
# Line 3412  sub _construct_tree ($) { Line 3211  sub _construct_tree ($) {
3211                  }                  }
3212                } # INSCOPE                } # INSCOPE
3213                unless (defined $i) {                unless (defined $i) {
3214                  !!!parse-error;                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3215                  ## Ignore the token                  ## Ignore the token
3216                  !!!next-token;                  !!!next-token;
3217                  redo B;                  redo B;
3218                }                }
                 
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                   }->{$open_elements->[-1]->[1]}) {  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $open_elements->[-1]->[1]}; # MUST  
                 redo B;  
               }  
3219    
3220                if ($open_elements->[-1]->[1] ne 'caption') {                ## Close the cell
3221                  !!!parse-error;                !!!back-token; # </?>
3222                }                $token = {type => END_TAG_TOKEN, tag_name => $tn};
   
               splice @$open_elements, $i;  
   
               $clear_up_to_marker->();  
   
               $insertion_mode = 'in table';  
   
               !!!next-token;  
3223                redo B;                redo B;
3224              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table' and
3225                !!!parse-error;                       $self->{insertion_mode} == IN_CAPTION_IM) {
3226                  !!!parse-error (type => 'not closed:caption');
3227    
3228                ## As if </caption>                ## As if </caption>
3229                ## have a table element in table scope                ## have a table element in table scope
3230                my $i;                my $i;
3231                INSCOPE: for (reverse 0..$#$open_elements) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3232                  my $node = $open_elements->[$_];                  my $node = $self->{open_elements}->[$_];
3233                  if ($node->[1] eq 'caption') {                  if ($node->[1] eq 'caption') {
3234                    $i = $_;                    $i = $_;
3235                    last INSCOPE;                    last INSCOPE;
# Line 3459  sub _construct_tree ($) { Line 3240  sub _construct_tree ($) {
3240                  }                  }
3241                } # INSCOPE                } # INSCOPE
3242                unless (defined $i) {                unless (defined $i) {
3243                  !!!parse-error;                  !!!parse-error (type => 'unmatched end tag:caption');
3244                  ## Ignore the token                  ## Ignore the token
3245                  !!!next-token;                  !!!next-token;
3246                  redo B;                  redo B;
# Line 3469  sub _construct_tree ($) { Line 3250  sub _construct_tree ($) {
3250                if ({                if ({
3251                     dd => 1, dt => 1, li => 1, p => 1,                     dd => 1, dt => 1, li => 1, p => 1,
3252                     td => 1, th => 1, tr => 1,                     td => 1, th => 1, tr => 1,
3253                    }->{$open_elements->[-1]->[1]}) {                     tbody => 1, tfoot=> 1, thead => 1,
3254                      }->{$self->{open_elements}->[-1]->[1]}) {
3255                  !!!back-token; # </table>                  !!!back-token; # </table>
3256                  $token = {type => 'end tag', tag_name => 'caption'};                  $token = {type => END_TAG_TOKEN, tag_name => 'caption'};
3257                  !!!back-token;                  !!!back-token;
3258                  $token = {type => 'end tag',                  $token = {type => END_TAG_TOKEN,
3259                            tag_name => $open_elements->[-1]->[1]}; # MUST                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3260                  redo B;                  redo B;
3261                }                }
3262    
3263                if ($open_elements->[-1]->[1] ne 'caption') {                if ($self->{open_elements}->[-1]->[1] ne 'caption') {
3264                  !!!parse-error;                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3265                }                }
3266    
3267                splice @$open_elements, $i;                splice @{$self->{open_elements}}, $i;
3268    
3269                $clear_up_to_marker->();                $clear_up_to_marker->();
3270    
3271                $insertion_mode = 'in table';                $self->{insertion_mode} = IN_TABLE_IM;
3272    
3273                ## reprocess                ## reprocess
3274                redo B;                redo B;
3275              } elsif ({              } elsif ({
3276                        body => 1, col => 1, colgroup => 1,                        body => 1, col => 1, colgroup => 1, html => 1,
                       html => 1, tbody => 1, td => 1, tfoot => 1,  
                       th => 1, thead => 1, tr => 1,  
3277                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3278                !!!parse-error;                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
3279                ## Ignore the token                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
                 
           $in_body->($insert_to_current);  
           redo B;  
         } elsif ($insertion_mode eq 'in column group') {  
           if ($token->{type} eq 'character') {  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $open_elements->[-1]->[0]->manakai_append_text ($1);  
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
               
             #  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $open_elements->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'col') {  
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               pop @$open_elements;  
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'colgroup') {  
               if ($open_elements->[-1]->[1] eq 'html') {  
                 !!!parse-error;  
3280                  ## Ignore the token                  ## Ignore the token
3281                  !!!next-token;                  !!!next-token;
3282                  redo B;                  redo B;
3283                } else {                } else {
3284                  pop @$open_elements; # colgroup                  #
                 $insertion_mode = 'in table';  
                 !!!next-token;  
                 redo B;              
3285                }                }
3286              } elsif ($token->{tag_name} eq 'col') {              } elsif ({
3287                !!!parse-error;                        tbody => 1, tfoot => 1,
3288                          thead => 1, tr => 1,
3289                         }->{$token->{tag_name}} and
3290                         $self->{insertion_mode} == IN_CAPTION_IM) {
3291                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3292                ## Ignore the token                ## Ignore the token
3293                !!!next-token;                !!!next-token;
3294                redo B;                redo B;
3295              } else {              } else {
3296                #                #
3297              }              }
3298            } else {        } else {
3299              #          die "$0: $token->{type}: Unknown token type";
3300            }        }
3301    
3302            ## As if </colgroup>        $insert = $insert_to_current;
3303            if ($open_elements->[-1]->[1] eq 'html') {        #
3304              !!!parse-error;      } elsif ($self->{insertion_mode} & TABLE_IMS) {
3305              ## Ignore the token            if ($token->{type} == CHARACTER_TOKEN) {
3306              !!!next-token;              ## NOTE: There are "character in table" code clones.
             redo B;  
           } else {  
             pop @$open_elements; # colgroup  
             $insertion_mode = 'in table';  
             ## reprocess  
             redo B;  
           }  
         } elsif ($insertion_mode eq 'in table body') {  
           if ($token->{type} eq 'character') {  
             ## NOTE: This is a "character in table" code clone.  
3307              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
3308                $open_elements->[-1]->[0]->manakai_append_text ($1);                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
3309                                
3310                unless (length $token->{data}) {                unless (length $token->{data}) {
3311                  !!!next-token;                  !!!next-token;
# Line 3581  sub _construct_tree ($) { Line 3313  sub _construct_tree ($) {
3313                }                }
3314              }              }
3315    
3316                !!!parse-error (type => 'in table:#character');
3317    
3318              ## As if in body, but insert into foster parent element              ## As if in body, but insert into foster parent element
3319              ## ISSUE: Spec says that "whenever a node would be inserted              ## ISSUE: Spec says that "whenever a node would be inserted
3320              ## into the current node" while characters might not be              ## into the current node" while characters might not be
3321              ## result in a new Text node.              ## result in a new Text node.
3322              $reconstruct_active_formatting_elements->($insert_to_foster);              $reconstruct_active_formatting_elements->($insert_to_foster);
3323                
3324              if ({              if ({
3325                   table => 1, tbody => 1, tfoot => 1,                   table => 1, tbody => 1, tfoot => 1,
3326                   thead => 1, tr => 1,                   thead => 1, tr => 1,
3327                  }->{$open_elements->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
3328                # MUST                # MUST
3329                my $foster_parent_element;                my $foster_parent_element;
3330                my $next_sibling;                my $next_sibling;
3331                my $prev_sibling;                my $prev_sibling;
3332                OE: for (reverse 0..$#$open_elements) {                OE: for (reverse 0..$#{$self->{open_elements}}) {
3333                  if ($open_elements->[$_]->[1] eq 'table') {                  if ($self->{open_elements}->[$_]->[1] eq 'table') {
3334                    my $parent = $open_elements->[$_]->[0]->parent_node;                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3335                    if (defined $parent and $parent->node_type == 1) {                    if (defined $parent and $parent->node_type == 1) {
3336                      $foster_parent_element = $parent;                      $foster_parent_element = $parent;
3337                      $next_sibling = $open_elements->[$_]->[0];                      $next_sibling = $self->{open_elements}->[$_]->[0];
3338                      $prev_sibling = $next_sibling->previous_sibling;                      $prev_sibling = $next_sibling->previous_sibling;
3339                    } else {                    } else {
3340                      $foster_parent_element = $open_elements->[$_ - 1]->[0];                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];
3341                      $prev_sibling = $foster_parent_element->last_child;                      $prev_sibling = $foster_parent_element->last_child;
3342                    }                    }
3343                    last OE;                    last OE;
3344                  }                  }
3345                } # OE                } # OE
3346                $foster_parent_element = $open_elements->[0]->[0] and                $foster_parent_element = $self->{open_elements}->[0]->[0] and
3347                $prev_sibling = $foster_parent_element->last_child                $prev_sibling = $foster_parent_element->last_child
3348                  unless defined $foster_parent_element;                  unless defined $foster_parent_element;
3349                if (defined $prev_sibling and                if (defined $prev_sibling and
# Line 3621  sub _construct_tree ($) { Line 3355  sub _construct_tree ($) {
3355                     $next_sibling);                     $next_sibling);
3356                }                }
3357              } else {              } else {
3358                $open_elements->[-1]->[0]->manakai_append_text ($token->{data});                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
3359              }              }
3360                            
3361              !!!next-token;              !!!next-token;
3362              redo B;              redo B;
3363            } elsif ($token->{type} eq 'comment') {            } elsif ($token->{type} == START_TAG_TOKEN) {
             ## Copied from 'in table'  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $open_elements->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
3364              if ({              if ({
3365                   tr => 1,                   tr => ($self->{insertion_mode} != IN_ROW_IM),
3366                   th => 1, td => 1,                   th => 1, td => 1,
3367                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
3368                ## Clear back to table body context                if ($self->{insertion_mode} == IN_TABLE_IM) {
3369                while (not {                  ## Clear back to table context
3370                  tbody => 1, tfoot => 1, thead => 1, html => 1,                  while ($self->{open_elements}->[-1]->[1] ne 'table' and
3371                }->{$open_elements->[-1]->[1]}) {                         $self->{open_elements}->[-1]->[1] ne 'html') {
3372                  !!!parse-error;                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3373                  pop @$open_elements;                    pop @{$self->{open_elements}};
               }  
                 
               $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..$#$open_elements) {  
                 my $node = $open_elements->[$_];  
                 if ({  
                      tbody => 1, thead => 1, tfoot => 1,  
                     }->{$node->[1]}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
3374                  }                  }
3375                } # INSCOPE                  
3376                unless (defined $i) {                  !!!insert-element ('tbody');
3377                  !!!parse-error;                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
3378                  ## 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,  
               }->{$open_elements->[-1]->[1]}) {  
                 !!!parse-error;  
                 pop @$open_elements;  
3379                }                }
3380    
3381                ## As if <{current node}>                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
3382                ## have an element in table scope                  unless ($token->{tag_name} eq 'tr') {
3383                ## true by definition                    !!!parse-error (type => 'missing start tag:tr');
   
               ## Clear back to table body context  
               ## nop by definition  
   
               pop @$open_elements;  
               $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;  
   
               ## As if </table>  
               ## have a table element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#$open_elements) {  
                 my $node = $open_elements->[$_];  
                 if ($node->[1] eq 'table') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
3384                  }                  }
3385                } # INSCOPE                  
3386                unless (defined $i) {                  ## Clear back to table body context
3387                  !!!parse-error;                  while (not {
3388                  ## Ignore tokens </table><table>                    tbody => 1, tfoot => 1, thead => 1, html => 1,
3389                  !!!next-token;                  }->{$self->{open_elements}->[-1]->[1]}) {
3390                  redo B;                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3391                }                    pop @{$self->{open_elements}};
                 
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                   }->{$open_elements->[-1]->[1]}) {  
                 !!!back-token; # <table>  
                 $token = {type => 'end tag', tag_name => 'table'};  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $open_elements->[-1]->[1]}; # MUST  
                 redo B;  
               }  
   
               if ($open_elements->[-1]->[1] ne 'table') {  
                 !!!parse-error;  
               }  
   
               splice @$open_elements, $i;  
   
               $reset_insertion_mode->();                
   
               ## reprocess  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ({  
                  tbody => 1, tfoot => 1, thead => 1,  
                 }->{$token->{tag_name}}) {  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#$open_elements) {  
                 my $node = $open_elements->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
3392                  }                  }
3393                } # INSCOPE                  
3394                unless (defined $i) {                  $self->{insertion_mode} = IN_ROW_IM;
3395                  !!!parse-error;                  if ($token->{tag_name} eq 'tr') {
3396                  ## Ignore the token                    !!!insert-element ($token->{tag_name}, $token->{attributes});
3397                  !!!next-token;                    !!!next-token;
3398                  redo B;                    redo B;
3399                }                  } else {
3400                      !!!insert-element ('tr');
3401                ## Clear back to table body context                    ## reprocess in the "in row" insertion mode
               while (not {  
                 tbody => 1, tfoot => 1, thead => 1, html => 1,  
               }->{$open_elements->[-1]->[1]}) {  
                 !!!parse-error;  
                 pop @$open_elements;  
               }  
   
               pop @$open_elements;  
               $insertion_mode = 'in table';  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'table') {  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#$open_elements) {  
                 my $node = $open_elements->[$_];  
                 if ({  
                      tbody => 1, thead => 1, tfoot => 1,  
                     }->{$node->[1]}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
3402                  }                  }
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error;  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Clear back to table body context  
               while (not {  
                 tbody => 1, tfoot => 1, thead => 1, html => 1,  
               }->{$open_elements->[-1]->[1]}) {  
                 !!!parse-error;  
                 pop @$open_elements;  
               }  
   
               ## As if <{current node}>  
               ## have an element in table scope  
               ## true by definition  
   
               ## Clear back to table body context  
               ## nop by definition  
   
               pop @$open_elements;  
               $insertion_mode = 'in table';  
               ## reprocess  
               redo B;  
             } elsif ({  
                       body => 1, caption => 1, col => 1, colgroup => 1,  
                       html => 1, td => 1, th => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               !!!parse-error;  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
             
           ## As if in table  
           !!!parse-error;  
           $in_body->($insert_to_foster);  
           redo B;  
         } elsif ($insertion_mode eq 'in row') {  
           if ($token->{type} eq 'character') {  
             ## NOTE: This is a "character in table" code clone.  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $open_elements->[-1]->[0]->manakai_append_text ($1);  
                 
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
3403                }                }
             }  
3404    
             ## 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,  
                 }->{$open_elements->[-1]->[1]}) {  
               # MUST  
               my $foster_parent_element;  
               my $next_sibling;  
               my $prev_sibling;  
               OE: for (reverse 0..$#$open_elements) {  
                 if ($open_elements->[$_]->[1] eq 'table') {  
                   my $parent = $open_elements->[$_]->[0]->parent_node;  
                   if (defined $parent and $parent->node_type == 1) {  
                     $foster_parent_element = $parent;  
                     $next_sibling = $open_elements->[$_]->[0];  
                     $prev_sibling = $next_sibling->previous_sibling;  
                   } else {  
                     $foster_parent_element = $open_elements->[$_ - 1]->[0];  
                     $prev_sibling = $foster_parent_element->last_child;  
                   }  
                   last OE;  
                 }  
               } # OE  
               $foster_parent_element = $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 {  
               $open_elements->[-1]->[0]->manakai_append_text ($token->{data});  
             }  
               
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             ## Copied from 'in table'  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $open_elements->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'th' or  
                 $token->{tag_name} eq 'td') {  
3405                ## Clear back to table row context                ## Clear back to table row context
3406                while (not {                while (not {
3407                  tr => 1, html => 1,                  tr => 1, html => 1,
3408                }->{$open_elements->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
3409                  !!!parse-error;                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3410                  pop @$open_elements;                  pop @{$self->{open_elements}};
3411                }                }
3412                                
3413                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
3414                $insertion_mode = 'in cell';                $self->{insertion_mode} = IN_CELL_IM;
3415    
3416                push @$active_formatting_elements, ['#marker', ''];                push @$active_formatting_elements, ['#marker', ''];
3417                                
# Line 3930  sub _construct_tree ($) { Line 3419  sub _construct_tree ($) {
3419                redo B;                redo B;
3420              } elsif ({              } elsif ({
3421                        caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
3422                        tbody => 1, tfoot => 1, thead => 1, tr => 1,                        tbody => 1, tfoot => 1, thead => 1,
3423                          tr => 1, # $self->{insertion_mode} == IN_ROW_IM
3424                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3425                ## As if </tr>                if ($self->{insertion_mode} == IN_ROW_IM) {
3426                ## have an element in table scope                  ## As if </tr>
3427                my $i;                  ## have an element in table scope
3428                INSCOPE: for (reverse 0..$#$open_elements) {                  my $i;
3429                  my $node = $open_elements->[$_];                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3430                  if ($node->[1] eq 'tr') {                    my $node = $self->{open_elements}->[$_];
3431                    $i = $_;                    if ($node->[1] eq 'tr') {
3432                    last INSCOPE;                      $i = $_;
3433                  } elsif ({                      last INSCOPE;
3434                            table => 1, html => 1,                    } elsif ({
3435                           }->{$node->[1]}) {                              table => 1, html => 1,
3436                    last INSCOPE;                             }->{$node->[1]}) {
3437                        last INSCOPE;
3438                      }
3439                    } # INSCOPE
3440                    unless (defined $i) {
3441                      !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});
3442                      ## Ignore the token
3443                      !!!next-token;
3444                      redo B;
3445                    }
3446                    
3447                    ## Clear back to table row context
3448                    while (not {
3449                      tr => 1, html => 1,
3450                    }->{$self->{open_elements}->[-1]->[1]}) {
3451                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3452                      pop @{$self->{open_elements}};
3453                    }
3454                    
3455                    pop @{$self->{open_elements}}; # tr
3456                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
3457                    if ($token->{tag_name} eq 'tr') {
3458                      ## reprocess
3459                      redo B;
3460                    } else {
3461                      ## reprocess in the "in table body" insertion mode...
3462                  }                  }
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error;  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
3463                }                }
3464    
3465                ## Clear back to table row context                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
3466                while (not {                  ## have an element in table scope
3467                  tr => 1, html => 1,                  my $i;
3468                }->{$open_elements->[-1]->[1]}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3469                  !!!parse-error;                    my $node = $self->{open_elements}->[$_];
3470                  pop @$open_elements;                    if ({
3471                }                         tbody => 1, thead => 1, tfoot => 1,
3472                          }->{$node->[1]}) {
3473                        $i = $_;
3474                        last INSCOPE;
3475                      } elsif ({
3476                                table => 1, html => 1,
3477                               }->{$node->[1]}) {
3478                        last INSCOPE;
3479                      }
3480                    } # INSCOPE
3481                    unless (defined $i) {
3482                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3483                      ## Ignore the token
3484                      !!!next-token;
3485                      redo B;
3486                    }
3487    
3488                pop @$open_elements; # tr                  ## Clear back to table body context
3489                $insertion_mode = 'in table body';                  while (not {
3490                ## reprocess                    tbody => 1, tfoot => 1, thead => 1, html => 1,
3491                redo B;                  }->{$self->{open_elements}->[-1]->[1]}) {
3492                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3493                      pop @{$self->{open_elements}};
3494                    }
3495                    
3496                    ## As if <{current node}>
3497                    ## have an element in table scope
3498                    ## true by definition
3499                    
3500                    ## Clear back to table body context
3501                    ## nop by definition
3502                    
3503                    pop @{$self->{open_elements}};
3504                    $self->{insertion_mode} = IN_TABLE_IM;
3505                    ## reprocess in "in table" insertion mode...
3506                  }
3507    
3508                  if ($token->{tag_name} eq 'col') {
3509                    ## Clear back to table context
3510                    while ($self->{open_elements}->[-1]->[1] ne 'table' and
3511                           $self->{open_elements}->[-1]->[1] ne 'html') {
3512                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3513                      pop @{$self->{open_elements}};
3514                    }
3515                    
3516                    !!!insert-element ('colgroup');
3517                    $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
3518                    ## reprocess
3519                    redo B;
3520                  } elsif ({
3521                            caption => 1,
3522                            colgroup => 1,
3523                            tbody => 1, tfoot => 1, thead => 1,
3524                           }->{$token->{tag_name}}) {
3525                    ## Clear back to table context
3526                    while ($self->{open_elements}->[-1]->[1] ne 'table' and
3527                           $self->{open_elements}->[-1]->[1] ne 'html') {
3528                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3529                      pop @{$self->{open_elements}};
3530                    }
3531                    
3532                    push @$active_formatting_elements, ['#marker', '']
3533                        if $token->{tag_name} eq 'caption';
3534                    
3535                    !!!insert-element ($token->{tag_name}, $token->{attributes});
3536                    $self->{insertion_mode} = {
3537                                               caption => IN_CAPTION_IM,
3538                                               colgroup => IN_COLUMN_GROUP_IM,
3539                                               tbody => IN_TABLE_BODY_IM,
3540                                               tfoot => IN_TABLE_BODY_IM,
3541                                               thead => IN_TABLE_BODY_IM,
3542                                              }->{$token->{tag_name}};
3543                    !!!next-token;
3544                    redo B;
3545                  } else {
3546                    die "$0: in table: <>: $token->{tag_name}";
3547                  }
3548              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
3549                ## NOTE: This is a code clone of "table in table"                ## NOTE: There are code clones for this "table in table"
3550                !!!parse-error;                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3551    
3552                ## As if </table>                ## As if </table>
3553                ## have a table element in table scope                ## have a table element in table scope
3554                my $i;                my $i;
3555                INSCOPE: for (reverse 0..$#$open_elements) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3556                  my $node = $open_elements->[$_];                  my $node = $self->{open_elements}->[$_];
3557                  if ($node->[1] eq 'table') {                  if ($node->[1] eq 'table') {
3558                    $i = $_;                    $i = $_;
3559                    last INSCOPE;                    last INSCOPE;
# Line 3984  sub _construct_tree ($) { Line 3564  sub _construct_tree ($) {
3564                  }                  }
3565                } # INSCOPE                } # INSCOPE
3566                unless (defined $i) {                unless (defined $i) {
3567                  !!!parse-error;                  !!!parse-error (type => 'unmatched end tag:table');
3568                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
3569                  !!!next-token;                  !!!next-token;
3570                  redo B;                  redo B;
# Line 3994  sub _construct_tree ($) { Line 3574  sub _construct_tree ($) {
3574                if ({                if ({
3575                     dd => 1, dt => 1, li => 1, p => 1,                     dd => 1, dt => 1, li => 1, p => 1,
3576                     td => 1, th => 1, tr => 1,                     td => 1, th => 1, tr => 1,
3577                    }->{$open_elements->[-1]->[1]}) {                     tbody => 1, tfoot=> 1, thead => 1,
3578                      }->{$self->{open_elements}->[-1]->[1]}) {
3579                  !!!back-token; # <table>                  !!!back-token; # <table>
3580                  $token = {type => 'end tag', tag_name => 'table'};                  $token = {type => END_TAG_TOKEN, tag_name => 'table'};
3581                  !!!back-token;                  !!!back-token;
3582                  $token = {type => 'end tag',                  $token = {type => END_TAG_TOKEN,
3583                            tag_name => $open_elements->[-1]->[1]}; # MUST                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3584                  redo B;                  redo B;
3585                }                }
3586    
3587                if ($open_elements->[-1]->[1] ne 'table') {                if ($self->{open_elements}->[-1]->[1] ne 'table') {
3588                  !!!parse-error;                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3589                }                }
3590    
3591                splice @$open_elements, $i;                splice @{$self->{open_elements}}, $i;
3592    
3593                $reset_insertion_mode->();                              $self->_reset_insertion_mode;
3594    
3595                ## reprocess                ## reprocess
3596                redo B;                redo B;
3597              } else {              } else {
3598                #                #
3599              }              }
3600            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
3601              if ($token->{tag_name} eq 'tr') {              if ($token->{tag_name} eq 'tr' and
3602                    $self->{insertion_mode} == IN_ROW_IM) {
3603                ## have an element in table scope                ## have an element in table scope
3604                my $i;                my $i;
3605                INSCOPE: for (reverse 0..$#$open_elements) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3606                  my $node = $open_elements->[$_];                  my $node = $self->{open_elements}->[$_];
3607                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq $token->{tag_name}) {
3608                    $i = $_;                    $i = $_;
3609                    last INSCOPE;                    last INSCOPE;
# Line 4032  sub _construct_tree ($) { Line 3614  sub _construct_tree ($) {
3614                  }                  }
3615                } # INSCOPE                } # INSCOPE
3616                unless (defined $i) {                unless (defined $i) {
3617                  !!!parse-error;                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3618                  ## Ignore the token                  ## Ignore the token
3619                  !!!next-token;                  !!!next-token;
3620                  redo B;                  redo B;
# Line 4041  sub _construct_tree ($) { Line 3623  sub _construct_tree ($) {
3623                ## Clear back to table row context                ## Clear back to table row context
3624                while (not {                while (not {
3625                  tr => 1, html => 1,                  tr => 1, html => 1,
3626                }->{$open_elements->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
3627                  !!!parse-error;                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3628                  pop @$open_elements;                  pop @{$self->{open_elements}};
3629                }                }
3630    
3631                pop @$open_elements; # tr                pop @{$self->{open_elements}}; # tr
3632                $insertion_mode = 'in table body';                $self->{insertion_mode} = IN_TABLE_BODY_IM;
3633                !!!next-token;                !!!next-token;
3634                redo B;                redo B;
3635              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
3636                ## As if </tr>                if ($self->{insertion_mode} == IN_ROW_IM) {
3637                ## have an element in table scope                  ## As if </tr>
3638                    ## have an element in table scope
3639                    my $i;
3640                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3641                      my $node = $self->{open_elements}->[$_];
3642                      if ($node->[1] eq 'tr') {
3643                        $i = $_;
3644                        last INSCOPE;
3645                      } elsif ({
3646                                table => 1, html => 1,
3647                               }->{$node->[1]}) {
3648                        last INSCOPE;
3649                      }
3650                    } # INSCOPE
3651                    unless (defined $i) {
3652                      !!!parse-error (type => 'unmatched end tag:'.$token->{type});
3653                      ## Ignore the token
3654                      !!!next-token;
3655                      redo B;
3656                    }
3657                    
3658                    ## Clear back to table row context
3659                    while (not {
3660                      tr => 1, html => 1,
3661                    }->{$self->{open_elements}->[-1]->[1]}) {
3662                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3663                      pop @{$self->{open_elements}};
3664                    }
3665                    
3666                    pop @{$self->{open_elements}}; # tr
3667                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
3668                    ## reprocess in the "in table body" insertion mode...
3669                  }
3670    
3671                  if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
3672                    ## have an element in table scope
3673                    my $i;
3674                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3675                      my $node = $self->{open_elements}->[$_];
3676                      if ({
3677                           tbody => 1, thead => 1, tfoot => 1,
3678                          }->{$node->[1]}) {
3679                        $i = $_;
3680                        last INSCOPE;
3681                      } elsif ({
3682                                table => 1, html => 1,
3683                               }->{$node->[1]}) {
3684                        last INSCOPE;
3685                      }
3686                    } # INSCOPE
3687                    unless (defined $i) {
3688                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3689                      ## Ignore the token
3690                      !!!next-token;
3691                      redo B;
3692                    }
3693                    
3694                    ## Clear back to table body context
3695                    while (not {
3696                      tbody => 1, tfoot => 1, thead => 1, html => 1,
3697                    }->{$self->{open_elements}->[-1]->[1]}) {
3698                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3699                      pop @{$self->{open_elements}};
3700                    }
3701                    
3702                    ## As if <{current node}>
3703                    ## have an element in table scope
3704                    ## true by definition
3705                    
3706                    ## Clear back to table body context
3707                    ## nop by definition
3708                    
3709                    pop @{$self->{open_elements}};
3710                    $self->{insertion_mode} = IN_TABLE_IM;
3711                    ## reprocess in the "in table" insertion mode...
3712                  }
3713    
3714                  ## have a table element in table scope
3715                my $i;                my $i;
3716                INSCOPE: for (reverse 0..$#$open_elements) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3717                  my $node = $open_elements->[$_];                  my $node = $self->{open_elements}->[$_];
3718                  if ($node->[1] eq 'tr') {                  if ($node->[1] eq $token->{tag_name}) {
3719                    $i = $_;                    $i = $_;
3720                    last INSCOPE;                    last INSCOPE;
3721                  } elsif ({                  } elsif ({
# Line 4066  sub _construct_tree ($) { Line 3725  sub _construct_tree ($) {
3725                  }                  }
3726                } # INSCOPE                } # INSCOPE
3727                unless (defined $i) {                unless (defined $i) {
3728                  !!!parse-error;                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3729                  ## Ignore the token                  ## Ignore the token
3730                  !!!next-token;                  !!!next-token;
3731                  redo B;                  redo B;
3732                }                }
3733    
3734                ## Clear back to table row context                ## generate implied end tags
3735                while (not {                if ({
3736                  tr => 1, html => 1,                     dd => 1, dt => 1, li => 1, p => 1,
3737                }->{$open_elements->[-1]->[1]}) {                     td => 1, th => 1, tr => 1,
3738                  !!!parse-error;                     tbody => 1, tfoot=> 1, thead => 1,
3739                  pop @$open_elements;                    }->{$self->{open_elements}->[-1]->[1]}) {
3740                    !!!back-token;
3741                    $token = {type => END_TAG_TOKEN,
3742                              tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3743                    redo B;
3744                }                }
3745                  
3746                pop @$open_elements; # tr                if ($self->{open_elements}->[-1]->[1] ne 'table') {
3747                $insertion_mode = 'in table body';                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3748                ## reprocess                }
3749                    
3750                  splice @{$self->{open_elements}}, $i;
3751                  
3752                  $self->_reset_insertion_mode;
3753                  
3754                  !!!next-token;
3755                redo B;                redo B;
3756              } elsif ({              } elsif ({
3757                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
3758                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}} and
3759                ## have an element in table scope                       $self->{insertion_mode} & ROW_IMS) {
3760                my $i;                if ($self->{insertion_mode} == IN_ROW_IM) {
3761                INSCOPE: for (reverse 0..$#$open_elements) {                  ## have an element in table scope
3762                  my $node = $open_elements->[$_];                  my $i;
3763                  if ($node->[1] eq $token->{tag_name}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3764                    $i = $_;                    my $node = $self->{open_elements}->[$_];
3765                    last INSCOPE;                    if ($node->[1] eq $token->{tag_name}) {
3766                  } elsif ({                      $i = $_;
3767                            table => 1, html => 1,                      last INSCOPE;
3768                           }->{$node->[1]}) {                    } elsif ({
3769                    last INSCOPE;                              table => 1, html => 1,
3770                               }->{$node->[1]}) {
3771                        last INSCOPE;
3772                      }
3773                    } # INSCOPE
3774                      unless (defined $i) {
3775                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3776                        ## Ignore the token
3777                        !!!next-token;
3778                        redo B;
3779                      }
3780                    
3781                    ## As if </tr>
3782                    ## have an element in table scope
3783                    my $i;
3784                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3785                      my $node = $self->{open_elements}->[$_];
3786                      if ($node->[1] eq 'tr') {
3787                        $i = $_;
3788                        last INSCOPE;
3789                      } elsif ({
3790                                table => 1, html => 1,
3791                               }->{$node->[1]}) {
3792                        last INSCOPE;
3793                      }
3794                    } # INSCOPE
3795                      unless (defined $i) {
3796                        !!!parse-error (type => 'unmatched end tag:tr');
3797                        ## Ignore the token
3798                        !!!next-token;
3799                        redo B;
3800                      }
3801                    
3802                    ## Clear back to table row context
3803                    while (not {
3804                      tr => 1, html => 1,
3805                    }->{$self->{open_elements}->[-1]->[1]}) {
3806                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3807                      pop @{$self->{open_elements}};
3808                  }                  }
3809                } # INSCOPE                  
3810                unless (defined $i) {                  pop @{$self->{open_elements}}; # tr
3811                  !!!parse-error;                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
3812                  ## Ignore the token                  ## reprocess in the "in table body" insertion mode...
                 !!!next-token;  
                 redo B;  
3813                }                }
3814    
               ## As if </tr>  
3815                ## have an element in table scope                ## have an element in table scope
3816                my $i;                my $i;
3817                INSCOPE: for (reverse 0..$#$open_elements) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3818                  my $node = $open_elements->[$_];                  my $node = $self->{open_elements}->[$_];
3819                  if ($node->[1] eq 'tr') {                  if ($node->[1] eq $token->{tag_name}) {
3820                    $i = $_;                    $i = $_;
3821                    last INSCOPE;                    last INSCOPE;
3822                  } elsif ({                  } elsif ({
# Line 4122  sub _construct_tree ($) { Line 3826  sub _construct_tree ($) {
3826                  }                  }
3827                } # INSCOPE                } # INSCOPE
3828                unless (defined $i) {                unless (defined $i) {
3829                  !!!parse-error;                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3830                  ## Ignore the token                  ## Ignore the token
3831                  !!!next-token;                  !!!next-token;
3832                  redo B;                  redo B;
3833                }                }
3834    
3835                ## Clear back to table row context                ## Clear back to table body context
3836                while (not {                while (not {
3837                  tr => 1, html => 1,                  tbody => 1, tfoot => 1, thead => 1, html => 1,
3838                }->{$open_elements->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
3839                  !!!parse-error;                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3840                  pop @$open_elements;                  pop @{$self->{open_elements}};
3841                }                }
3842    
3843                pop @$open_elements; # tr                pop @{$self->{open_elements}};
3844                $insertion_mode = 'in table body';                $self->{insertion_mode} = IN_TABLE_IM;
3845                ## reprocess                !!!next-token;
3846                redo B;                redo B;
3847              } elsif ({              } elsif ({
3848                        body => 1, caption => 1, col => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
3849                        colgroup => 1, html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
3850                          tr => 1, # $self->{insertion_mode} == IN_ROW_IM
3851                          tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
3852                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3853                !!!parse-error;                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3854                ## Ignore the token                ## Ignore the token
3855                !!!next-token;                !!!next-token;
3856                redo B;                redo B;
# Line 4152  sub _construct_tree ($) { Line 3858  sub _construct_tree ($) {
3858                #                #
3859              }              }
3860            } else {            } else {
3861              #              die "$0: $token->{type}: Unknown token type";
3862            }            }
3863    
3864            ## As if in table        !!!parse-error (type => 'in table:'.$token->{tag_name});
           !!!parse-error;  
           $in_body->($insert_to_foster);  
           redo B;  
         } elsif ($insertion_mode eq 'in cell') {  
           if ($token->{type} eq 'character') {  
             ## NOTE: This is a code clone of "character in body".  
             $reconstruct_active_formatting_elements->($insert_to_current);  
               
             $open_elements->[-1]->[0]->manakai_append_text ($token->{data});  
3865    
3866              !!!next-token;        $insert = $insert_to_foster;
3867              redo B;        #
3868            } elsif ($token->{type} eq 'comment') {      } elsif ($self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
3869              ## NOTE: This is a code clone of "comment in body".            if ($token->{type} == CHARACTER_TOKEN) {
3870              my $comment = $self->{document}->create_comment ($token->{data});              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
3871              $open_elements->[-1]->[0]->append_child ($comment);                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
3872              !!!next-token;                unless (length $token->{data}) {
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ({  
                  caption => 1, col => 1, colgroup => 1,  
                  tbody => 1, td => 1, tfoot => 1, th => 1,  
                  thead => 1, tr => 1,  
                 }->{$token->{tag_name}}) {  
               ## have an element in table scope  
               my $tn;  
               INSCOPE: for (reverse 0..$#$open_elements) {  
                 my $node = $open_elements->[$_];  
                 if ($node->[1] eq 'td' or $node->[1] eq 'th') {  
                   $tn = $node->[1];  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $tn) {  
                 !!!parse-error;  
                 ## Ignore the token  
3873                  !!!next-token;                  !!!next-token;
3874                  redo B;                  redo B;
3875                }                }
3876                }
3877                ## Close the cell              
3878                !!!back-token; # <?>              #
3879                $token = {type => 'end tag', tag_name => $tn};            } elsif ($token->{type} == START_TAG_TOKEN) {
3880                if ($token->{tag_name} eq 'col') {
3881                  !!!insert-element ($token->{tag_name}, $token->{attributes});
3882                  pop @{$self->{open_elements}};
3883                  !!!next-token;
3884                redo B;                redo B;
3885              } else {              } else {
3886                #                #
3887              }              }
3888            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
3889              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {              if ($token->{tag_name} eq 'colgroup') {
3890                ## have an element in table scope                if ($self->{open_elements}->[-1]->[1] eq 'html') {
3891                my $i;                  !!!parse-error (type => 'unmatched end tag:colgroup');
               INSCOPE: for (reverse 0..$#$open_elements) {  
                 my $node = $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;  
3892                  ## Ignore the token                  ## Ignore the token
3893                  !!!next-token;                  !!!next-token;
3894                  redo B;                  redo B;
3895                  } else {
3896                    pop @{$self->{open_elements}}; # colgroup
3897                    $self->{insertion_mode} = IN_TABLE_IM;
3898                    !!!next-token;
3899                    redo B;            
3900                }                }
3901                              } elsif ($token->{tag_name} eq 'col') {
3902                ## generate implied end tags                !!!parse-error (type => 'unmatched end tag:col');
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => ($token->{tag_name} eq 'th'),  
                    th => ($token->{tag_name} eq 'td'),  
                    tr => 1,  
                   }->{$open_elements->[-1]->[1]}) {  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $open_elements->[-1]->[1]}; # MUST  
                 redo B;  
               }  
   
               if ($open_elements->[-1]->[1] ne $token->{tag_name}) {  
                 !!!parse-error;  
               }  
   
               splice @$open_elements, $i;  
   
               $clear_up_to_marker->();  
   
               $insertion_mode = 'in row';  
   
               !!!next-token;  
               redo B;  
             } elsif ({  
                       body => 1, caption => 1, col => 1,  
                       colgroup => 1, html => 1,  
                      }->{$token->{tag_name}}) {  
               !!!parse-error;  
3903                ## Ignore the token                ## Ignore the token
3904                !!!next-token;                !!!next-token;
3905                redo B;                redo B;
             } elsif ({  
                       table => 1, tbody => 1, tfoot => 1,  
                       thead => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               ## have an element in table scope  
               my $i;  
               my $tn;  
               INSCOPE: for (reverse 0..$#$open_elements) {  
                 my $node = $open_elements->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {  
                   $tn = $node->[1];  
                   ## NOTE: There is exactly one |td| or |th| element  
                   ## in scope in the stack of open elements by definition.  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error;  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Close the cell  
               !!!back-token; # </?>  
               $token = {type => 'end tag', tag_name => $tn};  
               redo B;  
3906              } else {              } else {
3907                #                #
3908              }              }
3909            } else {            } else {
3910              #              #
3911            }            }
3912              
3913            $in_body->($insert_to_current);            ## As if </colgroup>
3914            redo B;            if ($self->{open_elements}->[-1]->[1] eq 'html') {
3915          } elsif ($insertion_mode eq 'in select') {              !!!parse-error (type => 'unmatched end tag:colgroup');
3916            if ($token->{type} eq 'character') {              ## Ignore the token
             $open_elements->[-1]->[0]->manakai_append_text ($token->{data});  
3917              !!!next-token;              !!!next-token;
3918              redo B;              redo B;
3919            } elsif ($token->{type} eq 'comment') {            } else {
3920              my $comment = $self->{document}->create_comment ($token->{data});              pop @{$self->{open_elements}}; # colgroup
3921              $open_elements->[-1]->[0]->append_child ($comment);              $self->{insertion_mode} = IN_TABLE_IM;
3922                ## reprocess
3923                redo B;
3924              }
3925        } elsif ($self->{insertion_mode} == IN_SELECT_IM) {
3926              if ($token->{type} == CHARACTER_TOKEN) {
3927                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
3928              !!!next-token;              !!!next-token;
3929              redo B;              redo B;
3930            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} == START_TAG_TOKEN) {
3931              if ($token->{tag_name} eq 'option') {              if ($token->{tag_name} eq 'option') {
3932                if ($open_elements->[-1]->[1] eq 'option') {                if ($self->{open_elements}->[-1]->[1] eq 'option') {
3933                  ## As if </option>                  ## As if </option>
3934                  pop @$open_elements;                  pop @{$self->{open_elements}};
3935                }                }
3936    
3937                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
3938                !!!next-token;                !!!next-token;
3939                redo B;                redo B;
3940              } elsif ($token->{tag_name} eq 'optgroup') {              } elsif ($token->{tag_name} eq 'optgroup') {
3941                if ($open_elements->[-1]->[1] eq 'option') {                if ($self->{open_elements}->[-1]->[1] eq 'option') {
3942                  ## As if </option>                  ## As if </option>
3943                  pop @$open_elements;                  pop @{$self->{open_elements}};
3944                }                }
3945    
3946                if ($open_elements->[-1]->[1] eq 'optgroup') {                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {
3947                  ## As if </optgroup>                  ## As if </optgroup>
3948                  pop @$open_elements;                  pop @{$self->{open_elements}};
3949                }                }
3950    
3951                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
3952                !!!next-token;                !!!next-token;
3953                redo B;                redo B;
3954              } elsif ($token->{tag_name} eq 'select') {              } elsif ($token->{tag_name} eq 'select') {
3955                !!!parse-error;                !!!parse-error (type => 'not closed:select');
3956                ## As if </select> instead                ## As if </select> instead
3957                ## have an element in table scope                ## have an element in table scope
3958                my $i;                my $i;
3959                INSCOPE: for (reverse 0..$#$open_elements) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3960                  my $node = $open_elements->[$_];                  my $node = $self->{open_elements}->[$_];
3961                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq $token->{tag_name}) {
3962                    $i = $_;                    $i = $_;
3963                    last INSCOPE;                    last INSCOPE;
# Line 4355  sub _construct_tree ($) { Line 3968  sub _construct_tree ($) {
3968                  }                  }
3969                } # INSCOPE                } # INSCOPE
3970                unless (defined $i) {                unless (defined $i) {
3971                  !!!parse-error;                  !!!parse-error (type => 'unmatched end tag:select');
3972                  ## Ignore the token                  ## Ignore the token
3973                  !!!next-token;                  !!!next-token;
3974                  redo B;                  redo B;
3975                }                }
3976                                
3977                splice @$open_elements, $i;                splice @{$self->{open_elements}}, $i;
3978    
3979                $reset_insertion_mode->();                $self->_reset_insertion_mode;
3980    
3981                !!!next-token;                !!!next-token;
3982                redo B;                redo B;
3983              } else {              } else {
3984                #                #
3985              }              }
3986            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
3987              if ($token->{tag_name} eq 'optgroup') {              if ($token->{tag_name} eq 'optgroup') {
3988                if ($open_elements->[-1]->[1] eq 'option' and                if ($self->{open_elements}->[-1]->[1] eq 'option' and
3989                    $open_elements->[-2]->[1] eq 'optgroup') {                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {
3990                  ## As if </option>                  ## As if </option>
3991                  splice @$open_elements, -2;                  splice @{$self->{open_elements}}, -2;
3992                } elsif ($open_elements->[-1]->[1] eq 'optgroup') {                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {
3993                  pop @$open_elements;                  pop @{$self->{open_elements}};
3994                } else {                } else {
3995                  !!!parse-error;                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3996                  ## Ignore the token                  ## Ignore the token
3997                }                }
3998                !!!next-token;                !!!next-token;
3999                redo B;                redo B;
4000              } elsif ($token->{tag_name} eq 'option') {              } elsif ($token->{tag_name} eq 'option') {
4001                if ($open_elements->[-1]->[1] eq 'option') {                if ($self->{open_elements}->[-1]->[1] eq 'option') {
4002                  pop @$open_elements;                  pop @{$self->{open_elements}};
4003                } else {                } else {
4004                  !!!parse-error;                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4005                  ## Ignore the token                  ## Ignore the token
4006                }                }
4007                !!!next-token;                !!!next-token;
# Line 4396  sub _construct_tree ($) { Line 4009  sub _construct_tree ($) {
4009              } elsif ($token->{tag_name} eq 'select') {              } elsif ($token->{tag_name} eq 'select') {
4010                ## have an element in table scope                ## have an element in table scope
4011                my $i;                my $i;
4012                INSCOPE: for (reverse 0..$#$open_elements) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4013                  my $node = $open_elements->[$_];                  my $node = $self->{open_elements}->[$_];
4014                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq $token->{tag_name}) {
4015                    $i = $_;                    $i = $_;
4016                    last INSCOPE;                    last INSCOPE;
# Line 4408  sub _construct_tree ($) { Line 4021  sub _construct_tree ($) {
4021                  }                  }
4022                } # INSCOPE                } # INSCOPE
4023                unless (defined $i) {                unless (defined $i) {
4024                  !!!parse-error;                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4025                  ## Ignore the token                  ## Ignore the token
4026                  !!!next-token;                  !!!next-token;
4027                  redo B;                  redo B;
4028                }                }
4029                                
4030                splice @$open_elements, $i;                splice @{$self->{open_elements}}, $i;
4031    
4032                $reset_insertion_mode->();                $self->_reset_insertion_mode;
4033    
4034                !!!next-token;                !!!next-token;
4035                redo B;                redo B;
# Line 4424  sub _construct_tree ($) { Line 4037  sub _construct_tree ($) {
4037                        caption => 1, table => 1, tbody => 1,                        caption => 1, table => 1, tbody => 1,
4038                        tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                        tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
4039                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4040                !!!parse-error;                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4041                                
4042                ## have an element in table scope                ## have an element in table scope
4043                my $i;                my $i;
4044                INSCOPE: for (reverse 0..$#$open_elements) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4045                  my $node = $open_elements->[$_];                  my $node = $self->{open_elements}->[$_];
4046                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq $token->{tag_name}) {
4047                    $i = $_;                    $i = $_;
4048                    last INSCOPE;                    last INSCOPE;
# Line 4448  sub _construct_tree ($) { Line 4061  sub _construct_tree ($) {
4061                ## As if </select>                ## As if </select>
4062                ## have an element in table scope                ## have an element in table scope
4063                undef $i;                undef $i;
4064                INSCOPE: for (reverse 0..$#$open_elements) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4065                  my $node = $open_elements->[$_];                  my $node = $self->{open_elements}->[$_];
4066                  if ($node->[1] eq 'select') {                  if ($node->[1] eq 'select') {
4067                    $i = $_;                    $i = $_;
4068                    last INSCOPE;                    last INSCOPE;
# Line 4460  sub _construct_tree ($) { Line 4073  sub _construct_tree ($) {
4073                  }                  }
4074                } # INSCOPE                } # INSCOPE
4075                unless (defined $i) {                unless (defined $i) {
4076                  !!!parse-error;                  !!!parse-error (type => 'unmatched end tag:select');
4077                  ## Ignore the </select> token                  ## Ignore the </select> token
4078                  !!!next-token; ## TODO: ok?                  !!!next-token; ## TODO: ok?
4079                  redo B;                  redo B;
4080                }                }
4081                                
4082                splice @$open_elements, $i;                splice @{$self->{open_elements}}, $i;
4083    
4084                $reset_insertion_mode->();                $self->_reset_insertion_mode;
4085    
4086                ## reprocess                ## reprocess
4087                redo B;                redo B;
# Line 4479  sub _construct_tree ($) { Line 4092  sub _construct_tree ($) {
4092              #              #
4093            }            }
4094    
4095            !!!parse-error;            !!!parse-error (type => 'in select:'.$token->{tag_name});
4096            ## Ignore the token            ## Ignore the token
4097            !!!next-token;            !!!next-token;
4098            redo B;            redo B;
4099          } elsif ($insertion_mode eq 'after body') {      } elsif ($self->{insertion_mode} & BODY_AFTER_IMS) {
4100            if ($token->{type} eq 'character') {        if ($token->{type} == CHARACTER_TOKEN) {
4101              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4102                ## As if in body            my $data = $1;
4103                $reconstruct_active_formatting_elements->($insert_to_current);            ## As if in body
4104              $reconstruct_active_formatting_elements->($insert_to_current);
4105                                
4106                $open_elements->[-1]->[0]->manakai_append_text ($token->{data});            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4107              
4108                unless (length $token->{data}) {            unless (length $token->{data}) {
                 !!!next-token;  
                 redo B;  
               }  
             }  
               
             #  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $open_elements->[0]->[0]->append_child ($comment);  
4109              !!!next-token;              !!!next-token;
4110              redo B;              redo B;
4111            } elsif ($token->{type} eq 'end tag') {            }
4112              if ($token->{tag_name} eq 'html') {          }
4113                ## TODO: if inner_html, parse-error, ignore the token; otherwise,          
4114            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4115              !!!parse-error (type => 'after html:#character');
4116    
4117                $phase = 'trailing end';            ## Reprocess in the "main" phase, "after body" insertion mode...
4118                !!!next-token;          }
4119                redo B;          
4120              } else {          ## "after body" insertion mode
4121                #          !!!parse-error (type => 'after body:#character');
4122              }  
4123            $self->{insertion_mode} = IN_BODY_IM;
4124            ## reprocess
4125            redo B;
4126          } elsif ($token->{type} == START_TAG_TOKEN) {
4127            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4128              !!!parse-error (type => 'after html:'.$token->{tag_name});
4129              
4130              ## Reprocess in the "main" phase, "after body" insertion mode...
4131            }
4132    
4133            ## "after body" insertion mode
4134            !!!parse-error (type => 'after body:'.$token->{tag_name});
4135    
4136            $self->{insertion_mode} = IN_BODY_IM;
4137            ## reprocess
4138            redo B;
4139          } elsif ($token->{type} == END_TAG_TOKEN) {
4140            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4141              !!!parse-error (type => 'after html:/'.$token->{tag_name});
4142              
4143              $self->{insertion_mode} = AFTER_BODY_IM;
4144              ## Reprocess in the "main" phase, "after body" insertion mode...
4145            }
4146    
4147            ## "after body" insertion mode
4148            if ($token->{tag_name} eq 'html') {
4149              if (defined $self->{inner_html_node}) {
4150                !!!parse-error (type => 'unmatched end tag:html');
4151                ## Ignore the token
4152                !!!next-token;
4153                redo B;
4154            } else {            } else {
4155              #              $self->{insertion_mode} = AFTER_HTML_BODY_IM;
4156                !!!next-token;
4157                redo B;
4158            }            }
4159            } else {
4160              !!!parse-error (type => 'after body:/'.$token->{tag_name});
4161    
4162            !!!parse-error ('data after body');            $self->{insertion_mode} = IN_BODY_IM;
           $insertion_mode = 'in body';  
4163            ## reprocess            ## reprocess
4164            redo B;            redo B;
4165          } elsif ($insertion_mode eq 'in frameset') {          }
4166            if ($token->{type} eq 'character') {        } else {
4167              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          die "$0: $token->{type}: Unknown token type";
4168                $open_elements->[-1]->[0]->manakai_append_text ($token->{data});        }
4169        } elsif ($self->{insertion_mode} & FRAME_IMS) {
4170          if ($token->{type} == CHARACTER_TOKEN) {
4171            if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4172              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4173              
4174              unless (length $token->{data}) {
4175                !!!next-token;
4176                redo B;
4177              }
4178            }
4179            
4180            if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
4181              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4182                !!!parse-error (type => 'in frameset:#character');
4183              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
4184                !!!parse-error (type => 'after frameset:#character');
4185              } else { # "after html frameset"
4186                !!!parse-error (type => 'after html:#character');
4187    
4188                $self->{insertion_mode} = AFTER_FRAMESET_IM;
4189                ## Reprocess in the "main" phase, "after frameset"...
4190                !!!parse-error (type => 'after frameset:#character');
4191              }
4192              
4193              ## Ignore the token.
4194              if (length $token->{data}) {
4195                ## reprocess the rest of characters
4196              } else {
4197                !!!next-token;
4198              }
4199              redo B;
4200            }
4201            
4202            die qq[$0: Character "$token->{data}"];
4203          } elsif ($token->{type} == START_TAG_TOKEN) {
4204            if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4205              !!!parse-error (type => 'after html:'.$token->{tag_name});
4206    
4207              $self->{insertion_mode} = AFTER_FRAMESET_IM;
4208              ## Process in the "main" phase, "after frameset" insertion mode...
4209            }
4210    
4211                unless (length $token->{data}) {          if ($token->{tag_name} eq 'frameset' and
4212                  !!!next-token;              $self->{insertion_mode} == IN_FRAMESET_IM) {
4213                  redo B;            !!!insert-element ($token->{tag_name}, $token->{attributes});
4214                }            !!!next-token;
4215              }            redo B;
4216            } elsif ($token->{tag_name} eq 'frame' and
4217                     $self->{insertion_mode} == IN_FRAMESET_IM) {
4218              !!!insert-element ($token->{tag_name}, $token->{attributes});
4219              pop @{$self->{open_elements}};
4220              !!!next-token;
4221              redo B;
4222            } elsif ($token->{tag_name} eq 'noframes') {
4223              ## NOTE: As if in body.
4224              $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);
4225              redo B;
4226            } else {
4227              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4228                !!!parse-error (type => 'in frameset:'.$token->{tag_name});
4229              } else {
4230                !!!parse-error (type => 'after frameset:'.$token->{tag_name});
4231              }
4232              ## Ignore the token
4233              !!!next-token;
4234              redo B;
4235            }
4236          } elsif ($token->{type} == END_TAG_TOKEN) {
4237            if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4238              !!!parse-error (type => 'after html:/'.$token->{tag_name});
4239    
4240              $self->{insertion_mode} = AFTER_FRAMESET_IM;
4241              ## Process in the "main" phase, "after frameset" insertion mode...
4242            }
4243    
4244              #          if ($token->{tag_name} eq 'frameset' and
4245            } elsif ($token->{type} eq 'comment') {              $self->{insertion_mode} == IN_FRAMESET_IM) {
4246              my $comment = $self->{document}->create_comment ($token->{data});            if ($self->{open_elements}->[-1]->[1] eq 'html' and
4247              $open_elements->[-1]->[0]->append_child ($comment);                @{$self->{open_elements}} == 1) {
4248                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4249                ## Ignore the token
4250              !!!next-token;              !!!next-token;
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'frameset') {  
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'frame') {  
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               pop @$open_elements;  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'noframes') {  
               $in_body->($insert_to_current);  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'frameset') {  
               if ($open_elements->[-1]->[1] eq 'html' and  
                   @$open_elements == 1) {  
                 !!!parse-error;  
                 ## Ignore the token  
                 !!!next-token;  
               } else {  
                 pop @$open_elements;  
                 !!!next-token;  
               }  
                 
               ## if not inner_html and  
               if ($open_elements->[-1]->[1] ne 'frameset') {  
                 $insertion_mode = 'after frameset';  
               }  
               redo B;  
             } else {  
               #  
             }  
4251            } else {            } else {
4252              #              pop @{$self->{open_elements}};
4253                !!!next-token;
4254              }
4255    
4256              if (not defined $self->{inner_html_node} and
4257                  $self->{open_elements}->[-1]->[1] ne 'frameset') {
4258                $self->{insertion_mode} = AFTER_FRAMESET_IM;
4259              }
4260              redo B;
4261            } elsif ($token->{tag_name} eq 'html' and
4262                     $self->{insertion_mode} == AFTER_FRAMESET_IM) {
4263              $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
4264              !!!next-token;
4265              redo B;
4266            } else {
4267              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4268                !!!parse-error (type => 'in frameset:/'.$token->{tag_name});
4269              } else {
4270                !!!parse-error (type => 'after frameset:/'.$token->{tag_name});
4271            }            }
             
           !!!parse-error;  
4272            ## Ignore the token            ## Ignore the token
4273            !!!next-token;            !!!next-token;
4274            redo B;            redo B;
4275          } elsif ($insertion_mode eq 'after frameset') {          }
4276            if ($token->{type} eq 'character') {        } else {
4277              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          die "$0: $token->{type}: Unknown token type";
4278                $open_elements->[-1]->[0]->manakai_append_text ($token->{data});        }
4279    
4280                unless (length $token->{data}) {        ## ISSUE: An issue in spec here
4281                  !!!next-token;      } else {
4282                  redo B;        die "$0: $self->{insertion_mode}: Unknown insertion mode";
4283                }      }
             }  
4284    
4285              #      ## "in body" insertion mode
4286            } elsif ($token->{type} eq 'comment') {      if ($token->{type} == START_TAG_TOKEN) {
4287              my $comment = $self->{document}->create_comment ($token->{data});        if ($token->{tag_name} eq 'script') {
4288              $open_elements->[-1]->[0]->append_child ($comment);          ## NOTE: This is an "as if in head" code clone
4289              !!!next-token;          $script_start_tag->($insert);
4290              redo B;          redo B;
4291            } elsif ($token->{type} eq 'start tag') {        } elsif ($token->{tag_name} eq 'style') {
4292              if ($token->{tag_name} eq 'noframes') {          ## NOTE: This is an "as if in head" code clone
4293                $in_body->($insert_to_current);          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
4294                redo B;          redo B;
4295              } else {        } elsif ({
4296                #                  base => 1, link => 1,
4297                   }->{$token->{tag_name}}) {
4298            ## NOTE: This is an "as if in head" code clone, only "-t" differs
4299            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4300            pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4301            !!!next-token;
4302            redo B;
4303          } elsif ($token->{tag_name} eq 'meta') {
4304            ## NOTE: This is an "as if in head" code clone, only "-t" differs
4305            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4306            pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4307    
4308            unless ($self->{confident}) {
4309              my $charset;
4310              if ($token->{attributes}->{charset}) { ## TODO: And if supported
4311                $charset = $token->{attributes}->{charset}->{value};
4312              }
4313              if ($token->{attributes}->{'http-equiv'}) {
4314                ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.
4315                if ($token->{attributes}->{'http-equiv'}->{value}
4316                    =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=
4317                        [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4318                        ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
4319                  $charset = defined $1 ? $1 : defined $2 ? $2 : $3;
4320                } ## TODO: And if supported
4321              }
4322              ## TODO: Change the encoding
4323            }
4324    
4325            !!!next-token;
4326            redo B;
4327          } elsif ($token->{tag_name} eq 'title') {
4328            !!!parse-error (type => 'in body:title');
4329            ## NOTE: This is an "as if in head" code clone
4330            $parse_rcdata->(RCDATA_CONTENT_MODEL, sub {
4331              if (defined $self->{head_element}) {
4332                $self->{head_element}->append_child ($_[0]);
4333              } else {
4334                $insert->($_[0]);
4335              }
4336            });
4337            redo B;
4338          } elsif ($token->{tag_name} eq 'body') {
4339            !!!parse-error (type => 'in body:body');
4340                  
4341            if (@{$self->{open_elements}} == 1 or
4342                $self->{open_elements}->[1]->[1] ne 'body') {
4343              ## Ignore the token
4344            } else {
4345              my $body_el = $self->{open_elements}->[1]->[0];
4346              for my $attr_name (keys %{$token->{attributes}}) {
4347                unless ($body_el->has_attribute_ns (undef, $attr_name)) {
4348                  $body_el->set_attribute_ns
4349                    (undef, [undef, $attr_name],
4350                     $token->{attributes}->{$attr_name}->{value});
4351              }              }
4352            } elsif ($token->{type} eq 'end tag') {            }
4353              if ($token->{tag_name} eq 'html') {          }
4354                $phase = 'trailing end';          !!!next-token;
4355            redo B;
4356          } elsif ({
4357                    address => 1, blockquote => 1, center => 1, dir => 1,
4358                    div => 1, dl => 1, fieldset => 1, listing => 1,
4359                    menu => 1, ol => 1, p => 1, ul => 1,
4360                    pre => 1,
4361                   }->{$token->{tag_name}}) {
4362            ## has a p element in scope
4363            INSCOPE: for (reverse @{$self->{open_elements}}) {
4364              if ($_->[1] eq 'p') {
4365                !!!back-token;
4366                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4367                redo B;
4368              } elsif ({
4369                        table => 1, caption => 1, td => 1, th => 1,
4370                        button => 1, marquee => 1, object => 1, html => 1,
4371                       }->{$_->[1]}) {
4372                last INSCOPE;
4373              }
4374            } # INSCOPE
4375              
4376            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4377            if ($token->{tag_name} eq 'pre') {
4378              !!!next-token;
4379              if ($token->{type} == CHARACTER_TOKEN) {
4380                $token->{data} =~ s/^\x0A//;
4381                unless (length $token->{data}) {
4382                !!!next-token;                !!!next-token;
               redo B;  
             } else {  
               #  
4383              }              }
           } else {  
             #  
4384            }            }
4385                      } else {
4386            !!!parse-error;            !!!next-token;
4387            }
4388            redo B;
4389          } elsif ($token->{tag_name} eq 'form') {
4390            if (defined $self->{form_element}) {
4391              !!!parse-error (type => 'in form:form');
4392            ## Ignore the token            ## Ignore the token
4393            !!!next-token;            !!!next-token;
4394            redo B;            redo B;
   
           ## ISSUE: An issue in spec there  
4395          } else {          } else {
4396            die "$0: $insertion_mode: Unknown insertion mode";            ## has a p element in scope
4397              INSCOPE: for (reverse @{$self->{open_elements}}) {
4398                if ($_->[1] eq 'p') {
4399                  !!!back-token;
4400                  $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4401                  redo B;
4402                } elsif ({
4403                          table => 1, caption => 1, td => 1, th => 1,
4404                          button => 1, marquee => 1, object => 1, html => 1,
4405                         }->{$_->[1]}) {
4406                  last INSCOPE;
4407                }
4408              } # INSCOPE
4409                
4410              !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4411              $self->{form_element} = $self->{open_elements}->[-1]->[0];
4412              !!!next-token;
4413              redo B;
4414          }          }
4415        }        } elsif ($token->{tag_name} eq 'li') {
4416      } elsif ($phase eq 'trailing end') {          ## has a p element in scope
4417        ## states in the main stage is preserved yet # MUST          INSCOPE: for (reverse @{$self->{open_elements}}) {
4418                    if ($_->[1] eq 'p') {
4419        if ($token->{type} eq 'DOCTYPE') {              !!!back-token;
4420          !!!parse-error;              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4421          ## Ignore the token              redo B;
4422              } elsif ({
4423                        table => 1, caption => 1, td => 1, th => 1,
4424                        button => 1, marquee => 1, object => 1, html => 1,
4425                       }->{$_->[1]}) {
4426                last INSCOPE;
4427              }
4428            } # INSCOPE
4429              
4430            ## Step 1
4431            my $i = -1;
4432            my $node = $self->{open_elements}->[$i];
4433            LI: {
4434              ## Step 2
4435              if ($node->[1] eq 'li') {
4436                if ($i != -1) {
4437                  !!!parse-error (type => 'end tag missing:'.
4438                                  $self->{open_elements}->[-1]->[1]);
4439                }
4440                splice @{$self->{open_elements}}, $i;
4441                last LI;
4442              }
4443              
4444              ## Step 3
4445              if (not $formatting_category->{$node->[1]} and
4446                  #not $phrasing_category->{$node->[1]} and
4447                  ($special_category->{$node->[1]} or
4448                   $scoping_category->{$node->[1]}) and
4449                  $node->[1] ne 'address' and $node->[1] ne 'div') {
4450                last LI;
4451              }
4452              
4453              ## Step 4
4454              $i--;
4455              $node = $self->{open_elements}->[$i];
4456              redo LI;
4457            } # LI
4458              
4459            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4460          !!!next-token;          !!!next-token;
4461          redo B;          redo B;
4462        } elsif ($token->{type} eq 'comment') {        } elsif ($token->{tag_name} eq 'dd' or $token->{tag_name} eq 'dt') {
4463          my $comment = $self->{document}->create_comment ($token->{data});          ## has a p element in scope
4464          $self->{document}->append_child ($comment);          INSCOPE: for (reverse @{$self->{open_elements}}) {
4465              if ($_->[1] eq 'p') {
4466                !!!back-token;
4467                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4468                redo B;
4469              } elsif ({
4470                        table => 1, caption => 1, td => 1, th => 1,
4471                        button => 1, marquee => 1, object => 1, html => 1,
4472                       }->{$_->[1]}) {
4473                last INSCOPE;
4474              }
4475            } # INSCOPE
4476              
4477            ## Step 1
4478            my $i = -1;
4479            my $node = $self->{open_elements}->[$i];
4480            LI: {
4481              ## Step 2
4482              if ($node->[1] eq 'dt' or $node->[1] eq 'dd') {
4483                if ($i != -1) {
4484                  !!!parse-error (type => 'end tag missing:'.
4485                                  $self->{open_elements}->[-1]->[1]);
4486                }
4487                splice @{$self->{open_elements}}, $i;
4488                last LI;
4489              }
4490              
4491              ## Step 3
4492              if (not $formatting_category->{$node->[1]} and
4493                  #not $phrasing_category->{$node->[1]} and
4494                  ($special_category->{$node->[1]} or
4495                   $scoping_category->{$node->[1]}) and
4496                  $node->[1] ne 'address' and $node->[1] ne 'div') {
4497                last LI;
4498              }
4499              
4500              ## Step 4
4501              $i--;
4502              $node = $self->{open_elements}->[$i];
4503              redo LI;
4504            } # LI
4505              
4506            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4507          !!!next-token;          !!!next-token;
4508          redo B;          redo B;
4509        } elsif ($token->{type} eq 'character') {        } elsif ($token->{tag_name} eq 'plaintext') {
4510          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          ## has a p element in scope
4511            my $data = $1;          INSCOPE: for (reverse @{$self->{open_elements}}) {
4512            ## As if in the main phase.            if ($_->[1] eq 'p') {
4513            ## NOTE: The insertion mode in the main phase              !!!back-token;
4514            ## just before the phase has been changed to the trailing              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4515            ## end phase is either "after body" or "after frameset".              redo B;
4516            $reconstruct_active_formatting_elements->($insert_to_current)            } elsif ({
4517              if $phase eq 'main';                      table => 1, caption => 1, td => 1, th => 1,
4518                        button => 1, marquee => 1, object => 1, html => 1,
4519                       }->{$_->[1]}) {
4520                last INSCOPE;
4521              }
4522            } # INSCOPE
4523              
4524            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4525              
4526            $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
4527              
4528            !!!next-token;
4529            redo B;
4530          } elsif ({
4531                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
4532                   }->{$token->{tag_name}}) {
4533            ## has a p element in scope
4534            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4535              my $node = $self->{open_elements}->[$_];
4536              if ($node->[1] eq 'p') {
4537                !!!back-token;
4538                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4539                redo B;
4540              } elsif ({
4541                        table => 1, caption => 1, td => 1, th => 1,
4542                        button => 1, marquee => 1, object => 1, html => 1,
4543                       }->{$node->[1]}) {
4544                last INSCOPE;
4545              }
4546            } # INSCOPE
4547              
4548            ## NOTE: See <http://html5.org/tools/web-apps-tracker?from=925&to=926>
4549            ## has an element in scope
4550            #my $i;
4551            #INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4552            #  my $node = $self->{open_elements}->[$_];
4553            #  if ({
4554            #       h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
4555            #      }->{$node->[1]}) {
4556            #    $i = $_;
4557            #    last INSCOPE;
4558            #  } elsif ({
4559            #            table => 1, caption => 1, td => 1, th => 1,
4560            #            button => 1, marquee => 1, object => 1, html => 1,
4561            #           }->{$node->[1]}) {
4562            #    last INSCOPE;
4563            #  }
4564            #} # INSCOPE
4565            #  
4566            #if (defined $i) {
4567            #  !!! parse-error (type => 'in hn:hn');
4568            #  splice @{$self->{open_elements}}, $i;
4569            #}
4570              
4571            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4572              
4573            !!!next-token;
4574            redo B;
4575          } elsif ($token->{tag_name} eq 'a') {
4576            AFE: for my $i (reverse 0..$#$active_formatting_elements) {
4577              my $node = $active_formatting_elements->[$i];
4578              if ($node->[1] eq 'a') {
4579                !!!parse-error (type => 'in a:a');
4580                
4581                !!!back-token;
4582                $token = {type => END_TAG_TOKEN, tag_name => 'a'};
4583                $formatting_end_tag->($token->{tag_name});
4584                
4585                AFE2: for (reverse 0..$#$active_formatting_elements) {
4586                  if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
4587                    splice @$active_formatting_elements, $_, 1;
4588                    last AFE2;
4589                  }
4590                } # AFE2
4591                OE: for (reverse 0..$#{$self->{open_elements}}) {
4592                  if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {
4593                    splice @{$self->{open_elements}}, $_, 1;
4594                    last OE;
4595                  }
4596                } # OE
4597                last AFE;
4598              } elsif ($node->[0] eq '#marker') {
4599                last AFE;
4600              }
4601            } # AFE
4602              
4603            $reconstruct_active_formatting_elements->($insert_to_current);
4604    
4605            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4606            push @$active_formatting_elements, $self->{open_elements}->[-1];
4607    
4608            !!!next-token;
4609            redo B;
4610          } elsif ({
4611                    b => 1, big => 1, em => 1, font => 1, i => 1,
4612                    s => 1, small => 1, strile => 1,
4613                    strong => 1, tt => 1, u => 1,
4614                   }->{$token->{tag_name}}) {
4615            $reconstruct_active_formatting_elements->($insert_to_current);
4616            
4617            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4618            push @$active_formatting_elements, $self->{open_elements}->[-1];
4619            
4620            !!!next-token;
4621            redo B;
4622          } elsif ($token->{tag_name} eq 'nobr') {
4623            $reconstruct_active_formatting_elements->($insert_to_current);
4624    
4625            ## has a |nobr| element in scope
4626            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4627              my $node = $self->{open_elements}->[$_];
4628              if ($node->[1] eq 'nobr') {
4629                !!!parse-error (type => 'not closed:nobr');
4630                !!!back-token;
4631                $token = {type => END_TAG_TOKEN, tag_name => 'nobr'};
4632                redo B;
4633              } elsif ({
4634                        table => 1, caption => 1, td => 1, th => 1,
4635                        button => 1, marquee => 1, object => 1, html => 1,
4636                       }->{$node->[1]}) {
4637                last INSCOPE;
4638              }
4639            } # INSCOPE
4640            
4641            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4642            push @$active_formatting_elements, $self->{open_elements}->[-1];
4643            
4644            !!!next-token;
4645            redo B;
4646          } elsif ($token->{tag_name} eq 'button') {
4647            ## has a button element in scope
4648            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4649              my $node = $self->{open_elements}->[$_];
4650              if ($node->[1] eq 'button') {
4651                !!!parse-error (type => 'in button:button');
4652                !!!back-token;
4653                $token = {type => END_TAG_TOKEN, tag_name => 'button'};
4654                redo B;
4655              } elsif ({
4656                        table => 1, caption => 1, td => 1, th => 1,
4657                        button => 1, marquee => 1, object => 1, html => 1,
4658                       }->{$node->[1]}) {
4659                last INSCOPE;
4660              }
4661            } # INSCOPE
4662                        
4663            $open_elements->[-1]->[0]->manakai_append_text ($data);          $reconstruct_active_formatting_elements->($insert_to_current);
4664              
4665            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4666            push @$active_formatting_elements, ['#marker', ''];
4667    
4668            !!!next-token;
4669            redo B;
4670          } elsif ($token->{tag_name} eq 'marquee' or
4671                   $token->{tag_name} eq 'object') {
4672            $reconstruct_active_formatting_elements->($insert_to_current);
4673            
4674            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4675            push @$active_formatting_elements, ['#marker', ''];
4676            
4677            !!!next-token;
4678            redo B;
4679          } elsif ($token->{tag_name} eq 'xmp') {
4680            $reconstruct_active_formatting_elements->($insert_to_current);
4681            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
4682            redo B;
4683          } elsif ($token->{tag_name} eq 'table') {
4684            ## has a p element in scope
4685            INSCOPE: for (reverse @{$self->{open_elements}}) {
4686              if ($_->[1] eq 'p') {
4687                !!!back-token;
4688                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4689                redo B;
4690              } elsif ({
4691                        table => 1, caption => 1, td => 1, th => 1,
4692                        button => 1, marquee => 1, object => 1, html => 1,
4693                       }->{$_->[1]}) {
4694                last INSCOPE;
4695              }
4696            } # INSCOPE
4697                        
4698            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4699              
4700            $self->{insertion_mode} = IN_TABLE_IM;
4701              
4702            !!!next-token;
4703            redo B;
4704          } elsif ({
4705                    area => 1, basefont => 1, bgsound => 1, br => 1,
4706                    embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
4707                    image => 1,
4708                   }->{$token->{tag_name}}) {
4709            if ($token->{tag_name} eq 'image') {
4710              !!!parse-error (type => 'image');
4711              $token->{tag_name} = 'img';
4712            }
4713    
4714            ## NOTE: There is an "as if <br>" code clone.
4715            $reconstruct_active_formatting_elements->($insert_to_current);
4716            
4717            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4718            pop @{$self->{open_elements}};
4719            
4720            !!!next-token;
4721            redo B;
4722          } elsif ($token->{tag_name} eq 'hr') {
4723            ## has a p element in scope
4724            INSCOPE: for (reverse @{$self->{open_elements}}) {
4725              if ($_->[1] eq 'p') {
4726                !!!back-token;
4727                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4728                redo B;
4729              } elsif ({
4730                        table => 1, caption => 1, td => 1, th => 1,
4731                        button => 1, marquee => 1, object => 1, html => 1,
4732                       }->{$_->[1]}) {
4733                last INSCOPE;
4734              }
4735            } # INSCOPE
4736              
4737            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4738            pop @{$self->{open_elements}};
4739              
4740            !!!next-token;
4741            redo B;
4742          } elsif ($token->{tag_name} eq 'input') {
4743            $reconstruct_active_formatting_elements->($insert_to_current);
4744            
4745            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4746            ## TODO: associate with $self->{form_element} if defined
4747            pop @{$self->{open_elements}};
4748            
4749            !!!next-token;
4750            redo B;
4751          } elsif ($token->{tag_name} eq 'isindex') {
4752            !!!parse-error (type => 'isindex');
4753            
4754            if (defined $self->{form_element}) {
4755              ## Ignore the token
4756              !!!next-token;
4757              redo B;
4758            } else {
4759              my $at = $token->{attributes};
4760              my $form_attrs;
4761              $form_attrs->{action} = $at->{action} if $at->{action};
4762              my $prompt_attr = $at->{prompt};
4763              $at->{name} = {name => 'name', value => 'isindex'};
4764              delete $at->{action};
4765              delete $at->{prompt};
4766              my @tokens = (
4767                            {type => START_TAG_TOKEN, tag_name => 'form',
4768                             attributes => $form_attrs},
4769                            {type => START_TAG_TOKEN, tag_name => 'hr'},
4770                            {type => START_TAG_TOKEN, tag_name => 'p'},
4771                            {type => START_TAG_TOKEN, tag_name => 'label'},
4772                           );
4773              if ($prompt_attr) {
4774                push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value}};
4775              } else {
4776                push @tokens, {type => CHARACTER_TOKEN,
4777                               data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD
4778                ## TODO: make this configurable
4779              }
4780              push @tokens,
4781                            {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at},
4782                            #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
4783                            {type => END_TAG_TOKEN, tag_name => 'label'},
4784                            {type => END_TAG_TOKEN, tag_name => 'p'},
4785                            {type => START_TAG_TOKEN, tag_name => 'hr'},
4786                            {type => END_TAG_TOKEN, tag_name => 'form'};
4787              $token = shift @tokens;
4788              !!!back-token (@tokens);
4789              redo B;
4790            }
4791          } elsif ($token->{tag_name} eq 'textarea') {
4792            my $tag_name = $token->{tag_name};
4793            my $el;
4794            !!!create-element ($el, $token->{tag_name}, $token->{attributes});
4795            
4796            ## TODO: $self->{form_element} if defined
4797            $self->{content_model} = RCDATA_CONTENT_MODEL;
4798            delete $self->{escape}; # MUST
4799            
4800            $insert->($el);
4801            
4802            my $text = '';
4803            !!!next-token;
4804            if ($token->{type} == CHARACTER_TOKEN) {
4805              $token->{data} =~ s/^\x0A//;
4806            unless (length $token->{data}) {            unless (length $token->{data}) {
4807              !!!next-token;              !!!next-token;
             redo B;  
4808            }            }
4809          }          }
4810            while ($token->{type} == CHARACTER_TOKEN) {
4811              $text .= $token->{data};
4812              !!!next-token;
4813            }
4814            if (length $text) {
4815              $el->manakai_append_text ($text);
4816            }
4817            
4818            $self->{content_model} = PCDATA_CONTENT_MODEL;
4819            
4820            if ($token->{type} == END_TAG_TOKEN and
4821                $token->{tag_name} eq $tag_name) {
4822              ## Ignore the token
4823            } else {
4824              !!!parse-error (type => 'in RCDATA:#'.$token->{type});
4825            }
4826            !!!next-token;
4827            redo B;
4828          } elsif ({
4829                    iframe => 1,
4830                    noembed => 1,
4831                    noframes => 1,
4832                    noscript => 0, ## TODO: 1 if scripting is enabled
4833                   }->{$token->{tag_name}}) {
4834            ## NOTE: There are two "as if in body" code clones.
4835            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
4836            redo B;
4837          } elsif ($token->{tag_name} eq 'select') {
4838            $reconstruct_active_formatting_elements->($insert_to_current);
4839            
4840            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4841            
4842            $self->{insertion_mode} = IN_SELECT_IM;
4843            !!!next-token;
4844            redo B;
4845          } elsif ({
4846                    caption => 1, col => 1, colgroup => 1, frame => 1,
4847                    frameset => 1, head => 1, option => 1, optgroup => 1,
4848                    tbody => 1, td => 1, tfoot => 1, th => 1,
4849                    thead => 1, tr => 1,
4850                   }->{$token->{tag_name}}) {
4851            !!!parse-error (type => 'in body:'.$token->{tag_name});
4852            ## Ignore the token
4853            !!!next-token;
4854            redo B;
4855            
4856            ## ISSUE: An issue on HTML5 new elements in the spec.
4857          } else {
4858            $reconstruct_active_formatting_elements->($insert_to_current);
4859            
4860            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4861            
4862            !!!next-token;
4863            redo B;
4864          }
4865        } elsif ($token->{type} == END_TAG_TOKEN) {
4866          if ($token->{tag_name} eq 'body') {
4867            if (@{$self->{open_elements}} > 1 and
4868                $self->{open_elements}->[1]->[1] eq 'body') {
4869              for (@{$self->{open_elements}}) {
4870                unless ({
4871                           dd => 1, dt => 1, li => 1, p => 1, td => 1,
4872                           th => 1, tr => 1, body => 1, html => 1,
4873                         tbody => 1, tfoot => 1, thead => 1,
4874                        }->{$_->[1]}) {
4875                  !!!parse-error (type => 'not closed:'.$_->[1]);
4876                }
4877              }
4878    
4879          !!!parse-error;            $self->{insertion_mode} = AFTER_BODY_IM;
4880          $phase = 'main';            !!!next-token;
4881          ## reprocess            redo B;
4882            } else {
4883              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4884              ## Ignore the token
4885              !!!next-token;
4886              redo B;
4887            }
4888          } elsif ($token->{tag_name} eq 'html') {
4889            if (@{$self->{open_elements}} > 1 and $self->{open_elements}->[1]->[1] eq 'body') {
4890              ## ISSUE: There is an issue in the spec.
4891              if ($self->{open_elements}->[-1]->[1] ne 'body') {
4892                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);
4893              }
4894              $self->{insertion_mode} = AFTER_BODY_IM;
4895              ## reprocess
4896              redo B;
4897            } else {
4898              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4899              ## Ignore the token
4900              !!!next-token;
4901              redo B;
4902            }
4903          } elsif ({
4904                    address => 1, blockquote => 1, center => 1, dir => 1,
4905                    div => 1, dl => 1, fieldset => 1, listing => 1,
4906                    menu => 1, ol => 1, pre => 1, ul => 1,
4907                    p => 1,
4908                    dd => 1, dt => 1, li => 1,
4909                    button => 1, marquee => 1, object => 1,
4910                   }->{$token->{tag_name}}) {
4911            ## has an element in scope
4912            my $i;
4913            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4914              my $node = $self->{open_elements}->[$_];
4915              if ($node->[1] eq $token->{tag_name}) {
4916                ## generate implied end tags
4917                if ({
4918                     dd => ($token->{tag_name} ne 'dd'),
4919                     dt => ($token->{tag_name} ne 'dt'),
4920                     li => ($token->{tag_name} ne 'li'),
4921                     p => ($token->{tag_name} ne 'p'),
4922                     td => 1, th => 1, tr => 1,
4923                     tbody => 1, tfoot=> 1, thead => 1,
4924                    }->{$self->{open_elements}->[-1]->[1]}) {
4925                  !!!back-token;
4926                  $token = {type => END_TAG_TOKEN,
4927                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
4928                  redo B;
4929                }
4930                $i = $_;
4931                last INSCOPE unless $token->{tag_name} eq 'p';
4932              } elsif ({
4933                        table => 1, caption => 1, td => 1, th => 1,
4934                        button => 1, marquee => 1, object => 1, html => 1,
4935                       }->{$node->[1]}) {
4936                last INSCOPE;
4937              }
4938            } # INSCOPE
4939            
4940            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
4941              if (defined $i) {
4942                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4943              } else {
4944                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4945              }
4946            }
4947            
4948            if (defined $i) {
4949              splice @{$self->{open_elements}}, $i;
4950            } elsif ($token->{tag_name} eq 'p') {
4951              ## As if <p>, then reprocess the current token
4952              my $el;
4953              !!!create-element ($el, 'p');
4954              $insert->($el);
4955            }
4956            $clear_up_to_marker->()
4957              if {
4958                button => 1, marquee => 1, object => 1,
4959              }->{$token->{tag_name}};
4960            !!!next-token;
4961          redo B;          redo B;
4962        } elsif ($token->{type} eq 'start tag' or        } elsif ($token->{tag_name} eq 'form') {
4963                 $token->{type} eq 'end tag') {          ## has an element in scope
4964          !!!parse-error;          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4965          $phase = 'main';            my $node = $self->{open_elements}->[$_];
4966          ## reprocess            if ($node->[1] eq $token->{tag_name}) {
4967                ## generate implied end tags
4968                if ({
4969                     dd => 1, dt => 1, li => 1, p => 1,
4970                     td => 1, th => 1, tr => 1,
4971                     tbody => 1, tfoot=> 1, thead => 1,
4972                    }->{$self->{open_elements}->[-1]->[1]}) {
4973                  !!!back-token;
4974                  $token = {type => END_TAG_TOKEN,
4975                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
4976                  redo B;
4977                }
4978                last INSCOPE;
4979              } elsif ({
4980                        table => 1, caption => 1, td => 1, th => 1,
4981                        button => 1, marquee => 1, object => 1, html => 1,
4982                       }->{$node->[1]}) {
4983                last INSCOPE;
4984              }
4985            } # INSCOPE
4986            
4987            if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {
4988              pop @{$self->{open_elements}};
4989            } else {
4990              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4991            }
4992    
4993            undef $self->{form_element};
4994            !!!next-token;
4995            redo B;
4996          } elsif ({
4997                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
4998                   }->{$token->{tag_name}}) {
4999            ## has an element in scope
5000            my $i;
5001            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5002              my $node = $self->{open_elements}->[$_];
5003              if ({
5004                   h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
5005                  }->{$node->[1]}) {
5006                ## generate implied end tags
5007                if ({
5008                     dd => 1, dt => 1, li => 1, p => 1,
5009                     td => 1, th => 1, tr => 1,
5010                     tbody => 1, tfoot=> 1, thead => 1,
5011                    }->{$self->{open_elements}->[-1]->[1]}) {
5012                  !!!back-token;
5013                  $token = {type => END_TAG_TOKEN,
5014                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5015                  redo B;
5016                }
5017                $i = $_;
5018                last INSCOPE;
5019              } elsif ({
5020                        table => 1, caption => 1, td => 1, th => 1,
5021                        button => 1, marquee => 1, object => 1, html => 1,
5022                       }->{$node->[1]}) {
5023                last INSCOPE;
5024              }
5025            } # INSCOPE
5026            
5027            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
5028              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
5029            }
5030            
5031            splice @{$self->{open_elements}}, $i if defined $i;
5032            !!!next-token;
5033          redo B;          redo B;
5034        } elsif ($token->{type} eq 'end-of-file') {        } elsif ({
5035          ## Stop parsing                  a => 1,
5036          last B;                  b => 1, big => 1, em => 1, font => 1, i => 1,
5037                    nobr => 1, s => 1, small => 1, strile => 1,
5038                    strong => 1, tt => 1, u => 1,
5039                   }->{$token->{tag_name}}) {
5040            $formatting_end_tag->($token->{tag_name});
5041            redo B;
5042          } elsif ($token->{tag_name} eq 'br') {
5043            !!!parse-error (type => 'unmatched end tag:br');
5044    
5045            ## As if <br>
5046            $reconstruct_active_formatting_elements->($insert_to_current);
5047            
5048            my $el;
5049            !!!create-element ($el, 'br');
5050            $insert->($el);
5051            
5052            ## Ignore the token.
5053            !!!next-token;
5054            redo B;
5055          } elsif ({
5056                    caption => 1, col => 1, colgroup => 1, frame => 1,
5057                    frameset => 1, head => 1, option => 1, optgroup => 1,
5058                    tbody => 1, td => 1, tfoot => 1, th => 1,
5059                    thead => 1, tr => 1,
5060                    area => 1, basefont => 1, bgsound => 1,
5061                    embed => 1, hr => 1, iframe => 1, image => 1,
5062                    img => 1, input => 1, isindex => 1, noembed => 1,
5063                    noframes => 1, param => 1, select => 1, spacer => 1,
5064                    table => 1, textarea => 1, wbr => 1,
5065                    noscript => 0, ## TODO: if scripting is enabled
5066                   }->{$token->{tag_name}}) {
5067            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5068            ## Ignore the token
5069            !!!next-token;
5070            redo B;
5071            
5072            ## ISSUE: Issue on HTML5 new elements in spec
5073            
5074        } else {        } else {
5075          die "$0: $token->{type}: Unknown token";          ## Step 1
5076            my $node_i = -1;
5077            my $node = $self->{open_elements}->[$node_i];
5078    
5079            ## Step 2
5080            S2: {
5081              if ($node->[1] eq $token->{tag_name}) {
5082                ## Step 1
5083                ## generate implied end tags
5084                if ({
5085                     dd => 1, dt => 1, li => 1, p => 1,
5086                     td => 1, th => 1, tr => 1,
5087                     tbody => 1, tfoot => 1, thead => 1,
5088                    }->{$self->{open_elements}->[-1]->[1]}) {
5089                  !!!back-token;
5090                  $token = {type => END_TAG_TOKEN,
5091                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5092                  redo B;
5093                }
5094            
5095                ## Step 2
5096                if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {
5097                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
5098                }
5099                
5100                ## Step 3
5101                splice @{$self->{open_elements}}, $node_i;
5102    
5103                !!!next-token;
5104                last S2;
5105              } else {
5106                ## Step 3
5107                if (not $formatting_category->{$node->[1]} and
5108                    #not $phrasing_category->{$node->[1]} and
5109                    ($special_category->{$node->[1]} or
5110                     $scoping_category->{$node->[1]})) {
5111                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5112                  ## Ignore the token
5113                  !!!next-token;
5114                  last S2;
5115                }
5116              }
5117              
5118              ## Step 4
5119              $node_i--;
5120              $node = $self->{open_elements}->[$node_i];
5121              
5122              ## Step 5;
5123              redo S2;
5124            } # S2
5125            redo B;
5126        }        }
5127      }      }
5128        redo B;
5129    } # B    } # B
5130    
5131      ## NOTE: The "trailing end" phase in HTML5 is split into
5132      ## two insertion modes: "after html body" and "after html frameset".
5133      ## NOTE: States in the main stage is preserved while
5134      ## the parser stays in the trailing end phase. # MUST
5135    
5136    ## Stop parsing # MUST    ## Stop parsing # MUST
5137        
5138    ## TODO: script stuffs    ## TODO: script stuffs
5139  } # _construct_tree  } # _tree_construct_main
5140    
5141    sub set_inner_html ($$$) {
5142      my $class = shift;
5143      my $node = shift;
5144      my $s = \$_[0];
5145      my $onerror = $_[1];
5146    
5147      my $nt = $node->node_type;
5148      if ($nt == 9) {
5149        # MUST
5150        
5151        ## Step 1 # MUST
5152        ## TODO: If the document has an active parser, ...
5153        ## ISSUE: There is an issue in the spec.
5154        
5155        ## Step 2 # MUST
5156        my @cn = @{$node->child_nodes};
5157        for (@cn) {
5158          $node->remove_child ($_);
5159        }
5160    
5161        ## Step 3, 4, 5 # MUST
5162        $class->parse_string ($$s => $node, $onerror);
5163      } elsif ($nt == 1) {
5164        ## TODO: If non-html element
5165    
5166        ## NOTE: Most of this code is copied from |parse_string|
5167    
5168        ## Step 1 # MUST
5169        my $this_doc = $node->owner_document;
5170        my $doc = $this_doc->implementation->create_document;
5171        $doc->manakai_is_html (1);
5172        my $p = $class->new;
5173        $p->{document} = $doc;
5174    
5175        ## Step 9 # MUST
5176        my $i = 0;
5177        my $line = 1;
5178        my $column = 0;
5179        $p->{set_next_input_character} = sub {
5180          my $self = shift;
5181    
5182          pop @{$self->{prev_input_character}};
5183          unshift @{$self->{prev_input_character}}, $self->{next_input_character};
5184    
5185          $self->{next_input_character} = -1 and return if $i >= length $$s;
5186          $self->{next_input_character} = ord substr $$s, $i++, 1;
5187          $column++;
5188    
5189          if ($self->{next_input_character} == 0x000A) { # LF
5190            $line++;
5191            $column = 0;
5192          } elsif ($self->{next_input_character} == 0x000D) { # CR
5193            $i++ if substr ($$s, $i, 1) eq "\x0A";
5194            $self->{next_input_character} = 0x000A; # LF # MUST
5195            $line++;
5196            $column = 0;
5197          } elsif ($self->{next_input_character} > 0x10FFFF) {
5198            $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
5199          } elsif ($self->{next_input_character} == 0x0000) { # NULL
5200            !!!parse-error (type => 'NULL');
5201            $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
5202          }
5203        };
5204        $p->{prev_input_character} = [-1, -1, -1];
5205        $p->{next_input_character} = -1;
5206        
5207        my $ponerror = $onerror || sub {
5208          my (%opt) = @_;
5209          warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";
5210        };
5211        $p->{parse_error} = sub {
5212          $ponerror->(@_, line => $line, column => $column);
5213        };
5214        
5215        $p->_initialize_tokenizer;
5216        $p->_initialize_tree_constructor;
5217    
5218        ## Step 2
5219        my $node_ln = $node->local_name;
5220        $p->{content_model} = {
5221          title => RCDATA_CONTENT_MODEL,
5222          textarea => RCDATA_CONTENT_MODEL,
5223          style => CDATA_CONTENT_MODEL,
5224          script => CDATA_CONTENT_MODEL,
5225          xmp => CDATA_CONTENT_MODEL,
5226          iframe => CDATA_CONTENT_MODEL,
5227          noembed => CDATA_CONTENT_MODEL,
5228          noframes => CDATA_CONTENT_MODEL,
5229          noscript => CDATA_CONTENT_MODEL,
5230          plaintext => PLAINTEXT_CONTENT_MODEL,
5231        }->{$node_ln};
5232        $p->{content_model} = PCDATA_CONTENT_MODEL
5233            unless defined $p->{content_model};
5234            ## ISSUE: What is "the name of the element"? local name?
5235    
5236        $p->{inner_html_node} = [$node, $node_ln];
5237    
5238        ## Step 4
5239        my $root = $doc->create_element_ns
5240          ('http://www.w3.org/1999/xhtml', [undef, 'html']);
5241    
5242        ## Step 5 # MUST
5243        $doc->append_child ($root);
5244    
5245        ## Step 6 # MUST
5246        push @{$p->{open_elements}}, [$root, 'html'];
5247    
5248        undef $p->{head_element};
5249    
5250        ## Step 7 # MUST
5251        $p->_reset_insertion_mode;
5252    
5253        ## Step 8 # MUST
5254        my $anode = $node;
5255        AN: while (defined $anode) {
5256          if ($anode->node_type == 1) {
5257            my $nsuri = $anode->namespace_uri;
5258            if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {
5259              if ($anode->local_name eq 'form') { ## TODO: case?
5260                $p->{form_element} = $anode;
5261                last AN;
5262              }
5263            }
5264          }
5265          $anode = $anode->parent_node;
5266        } # AN
5267        
5268        ## Step 3 # MUST
5269        ## Step 10 # MUST
5270        {
5271          my $self = $p;
5272          !!!next-token;
5273        }
5274        $p->_tree_construction_main;
5275    
5276        ## Step 11 # MUST
5277        my @cn = @{$node->child_nodes};
5278        for (@cn) {
5279          $node->remove_child ($_);
5280        }
5281        ## ISSUE: mutation events? read-only?
5282    
5283        ## Step 12 # MUST
5284        @cn = @{$root->child_nodes};
5285        for (@cn) {
5286          $this_doc->adopt_node ($_);
5287          $node->append_child ($_);
5288        }
5289        ## ISSUE: mutation events?
5290    
5291        $p->_terminate_tree_constructor;
5292      } else {
5293        die "$0: |set_inner_html| is not defined for node of type $nt";
5294      }
5295    } # set_inner_html
5296    
5297    } # tree construction stage
5298    
5299  sub get_inner_html ($$$) {  sub get_inner_html ($$$) {
5300    my ($class, $node, $on_error) = @_;    my (undef, $node, $on_error) = @_;
5301    
5302    ## Step 1    ## Step 1
5303    my $s = '';    my $s = '';
# Line 4718  sub get_inner_html ($$$) { Line 5331  sub get_inner_html ($$$) {
5331            
5332      my $nt = $child->node_type;      my $nt = $child->node_type;
5333      if ($nt == 1) { # Element      if ($nt == 1) { # Element
5334        my $tag_name = lc $child->tag_name; ## ISSUE: Definition of "lowercase"        my $tag_name = $child->tag_name; ## TODO: manakai_tag_name
5335        $s .= '<' . $tag_name;        $s .= '<' . $tag_name;
5336          ## NOTE: Non-HTML case:
5337        ## ISSUE: Non-html elements        ## <http://permalink.gmane.org/gmane.org.w3c.whatwg.discuss/11191>
5338    
5339        my @attrs = @{$child->attributes}; # sort order MUST be stable        my @attrs = @{$child->attributes}; # sort order MUST be stable
5340        for my $attr (@attrs) { # order is implementation dependent        for my $attr (@attrs) { # order is implementation dependent
5341          my $attr_name = lc $attr->name; ## ISSUE: Definition of "lowercase"          my $attr_name = $attr->name; ## TODO: manakai_name
5342          $s .= ' ' . $attr_name . '="';          $s .= ' ' . $attr_name . '="';
5343          my $attr_value = $attr->value;          my $attr_value = $attr->value;
5344          ## escape          ## escape
# Line 4744  sub get_inner_html ($$$) { Line 5357  sub get_inner_html ($$$) {
5357          spacer => 1, wbr => 1,          spacer => 1, wbr => 1,
5358        }->{$tag_name};        }->{$tag_name};
5359    
5360          $s .= "\x0A" if $tag_name eq 'pre' or $tag_name eq 'textarea';
5361    
5362        if (not $in_cdata and {        if (not $in_cdata and {
5363          style => 1, script => 1, xmp => 1, iframe => 1,          style => 1, script => 1, xmp => 1, iframe => 1,
5364          noembed => 1, noframes => 1, noscript => 1,          noembed => 1, noframes => 1, noscript => 1,
5365            plaintext => 1,
5366        }->{$tag_name}) {        }->{$tag_name}) {
5367          unshift @node, 'cdata-out';          unshift @node, 'cdata-out';
5368          $in_cdata = 1;          $in_cdata = 1;

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24