/[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.58 by wakaba, Tue Sep 4 11:19:07 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 DATA_STATE () { 0 }
163    sub ENTITY_DATA_STATE () { 1 }
164    sub TAG_OPEN_STATE () { 2 }
165    sub CLOSE_TAG_OPEN_STATE () { 3 }
166    sub TAG_NAME_STATE () { 4 }
167    sub BEFORE_ATTRIBUTE_NAME_STATE () { 5 }
168    sub ATTRIBUTE_NAME_STATE () { 6 }
169    sub AFTER_ATTRIBUTE_NAME_STATE () { 7 }
170    sub BEFORE_ATTRIBUTE_VALUE_STATE () { 8 }
171    sub ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE () { 9 }
172    sub ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE () { 10 }
173    sub ATTRIBUTE_VALUE_UNQUOTED_STATE () { 11 }
174    sub ENTITY_IN_ATTRIBUTE_VALUE_STATE () { 12 }
175    sub MARKUP_DECLARATION_OPEN_STATE () { 13 }
176    sub COMMENT_START_STATE () { 14 }
177    sub COMMENT_START_DASH_STATE () { 15 }
178    sub COMMENT_STATE () { 16 }
179    sub COMMENT_END_STATE () { 17 }
180    sub COMMENT_END_DASH_STATE () { 18 }
181    sub BOGUS_COMMENT_STATE () { 19 }
182    sub DOCTYPE_STATE () { 20 }
183    sub BEFORE_DOCTYPE_NAME_STATE () { 21 }
184    sub DOCTYPE_NAME_STATE () { 22 }
185    sub AFTER_DOCTYPE_NAME_STATE () { 23 }
186    sub BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE () { 24 }
187    sub DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE () { 25 }
188    sub DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE () { 26 }
189    sub AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE () { 27 }
190    sub BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 28 }
191    sub DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE () { 29 }
192    sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE () { 30 }
193    sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }
194    sub BOGUS_DOCTYPE_STATE () { 32 }
195    
196    sub DOCTYPE_TOKEN () { 1 }
197    sub COMMENT_TOKEN () { 2 }
198    sub START_TAG_TOKEN () { 3 }
199    sub END_TAG_TOKEN () { 4 }
200    sub END_OF_FILE_TOKEN () { 5 }
201    sub CHARACTER_TOKEN () { 6 }
202    
203    sub AFTER_HTML_IMS () { 0b100 }
204    sub HEAD_IMS ()       { 0b1000 }
205    sub BODY_IMS ()       { 0b10000 }
206    sub BODY_TABLE_IMS () { 0b100000 }
207    sub TABLE_IMS ()      { 0b1000000 }
208    sub ROW_IMS ()        { 0b10000000 }
209    sub BODY_AFTER_IMS () { 0b100000000 }
210    sub FRAME_IMS ()      { 0b1000000000 }
211    
212    sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }
213    sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }
214    sub IN_HEAD_IM () { HEAD_IMS | 0b00 }
215    sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }
216    sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }
217    sub BEFORE_HEAD_IM () { HEAD_IMS | 0b11 }
218    sub IN_BODY_IM () { BODY_IMS }
219    sub IN_CELL_IM () { BODY_IMS | BODY_TABLE_IMS | 0b01 }
220    sub IN_CAPTION_IM () { BODY_IMS | BODY_TABLE_IMS | 0b10 }
221    sub IN_ROW_IM () { TABLE_IMS | ROW_IMS | 0b01 }
222    sub IN_TABLE_BODY_IM () { TABLE_IMS | ROW_IMS | 0b10 }
223    sub IN_TABLE_IM () { TABLE_IMS }
224    sub AFTER_BODY_IM () { BODY_AFTER_IMS }
225    sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }
226    sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }
227    sub IN_SELECT_IM () { 0b01 }
228    sub IN_COLUMN_GROUP_IM () { 0b10 }
229    
230  ## Implementations MUST act as if state machine in the spec  ## Implementations MUST act as if state machine in the spec
231    
232  sub _initialize_tokenizer ($) {  sub _initialize_tokenizer ($) {
233    my $self = shift;    my $self = shift;
234    $self->{state} = 'data'; # MUST    $self->{state} = DATA_STATE; # MUST
235    $self->{content_model_flag} = 'PCDATA'; # be    $self->{content_model} = PCDATA_CONTENT_MODEL; # be
236    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE
237    undef $self->{current_attribute};    undef $self->{current_attribute};
238    undef $self->{last_emitted_start_tag_name};    undef $self->{last_emitted_start_tag_name};
# Line 371  sub _initialize_tokenizer ($) { Line 241  sub _initialize_tokenizer ($) {
241    # $self->{next_input_character}    # $self->{next_input_character}
242    !!!next-input-character;    !!!next-input-character;
243    $self->{token} = [];    $self->{token} = [];
244      # $self->{escape}
245  } # _initialize_tokenizer  } # _initialize_tokenizer
246    
247  ## A token has:  ## A token has:
248  ##   ->{type} eq 'DOCTYPE', 'start tag', 'end tag', 'comment',  ##   ->{type} == DOCTYPE_TOKEN, START_TAG_TOKEN, END_TAG_TOKEN, COMMENT_TOKEN,
249  ##       'character', or 'end-of-file'  ##       CHARACTER_TOKEN, or END_OF_FILE_TOKEN
250  ##   ->{name} (DOCTYPE, start tag (tagname), end tag (tagname))  ##   ->{name} (DOCTYPE_TOKEN)
251      ## ISSUE: the spec need s/tagname/tag name/  ##   ->{tag_name} (START_TAG_TOKEN, END_TAG_TOKEN)
252  ##   ->{error} == 1 or 0 (DOCTYPE)  ##   ->{public_identifier} (DOCTYPE_TOKEN)
253  ##   ->{attributes} isa HASH (start tag, end tag)  ##   ->{system_identifier} (DOCTYPE_TOKEN)
254  ##   ->{data} (comment, character)  ##   ->{correct} == 1 or 0 (DOCTYPE_TOKEN)
255    ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)
256  ## Macros  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
 ##   Macros MUST be preceded by three EXCLAMATION MARKs.  
 ##   emit ($token)  
 ##     Emits the specified token.  
257    
258  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
259    
# Line 402  sub _get_next_token ($) { Line 270  sub _get_next_token ($) {
270    }    }
271    
272    A: {    A: {
273      if ($self->{state} eq 'data') {      if ($self->{state} == DATA_STATE) {
274        if ($self->{next_input_character} == 0x0026) { # &        if ($self->{next_input_character} == 0x0026) { # &
275          if ($self->{content_model_flag} eq 'PCDATA' or          if ($self->{content_model} & CM_ENTITY) { # PCDATA | RCDATA
276              $self->{content_model_flag} eq 'RCDATA') {            $self->{state} = ENTITY_DATA_STATE;
           $self->{state} = 'entity data';  
277            !!!next-input-character;            !!!next-input-character;
278            redo A;            redo A;
279          } else {          } else {
280            #            #
281          }          }
282          } elsif ($self->{next_input_character} == 0x002D) { # -
283            if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
284              unless ($self->{escape}) {
285                if ($self->{prev_input_character}->[0] == 0x002D and # -
286                    $self->{prev_input_character}->[1] == 0x0021 and # !
287                    $self->{prev_input_character}->[2] == 0x003C) { # <
288                  $self->{escape} = 1;
289                }
290              }
291            }
292            
293            #
294        } elsif ($self->{next_input_character} == 0x003C) { # <        } elsif ($self->{next_input_character} == 0x003C) { # <
295          if ($self->{content_model_flag} ne 'PLAINTEXT') {          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA
296            $self->{state} = 'tag open';              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA
297                 not $self->{escape})) {
298              $self->{state} = TAG_OPEN_STATE;
299            !!!next-input-character;            !!!next-input-character;
300            redo A;            redo A;
301          } else {          } else {
302            #            #
303          }          }
304          } elsif ($self->{next_input_character} == 0x003E) { # >
305            if ($self->{escape} and
306                ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA
307              if ($self->{prev_input_character}->[0] == 0x002D and # -
308                  $self->{prev_input_character}->[1] == 0x002D) { # -
309                delete $self->{escape};
310              }
311            }
312            
313            #
314        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
315          !!!emit ({type => 'end-of-file'});          !!!emit ({type => END_OF_FILE_TOKEN});
316          last A; ## TODO: ok?          last A; ## TODO: ok?
317        }        }
318        # Anything else        # Anything else
319        my $token = {type => 'character',        my $token = {type => CHARACTER_TOKEN,
320                     data => chr $self->{next_input_character}};                     data => chr $self->{next_input_character}};
321        ## Stay in the data state        ## Stay in the data state
322        !!!next-input-character;        !!!next-input-character;
# Line 433  sub _get_next_token ($) { Line 324  sub _get_next_token ($) {
324        !!!emit ($token);        !!!emit ($token);
325    
326        redo A;        redo A;
327      } elsif ($self->{state} eq 'entity data') {      } elsif ($self->{state} == ENTITY_DATA_STATE) {
328        ## (cannot happen in CDATA state)        ## (cannot happen in CDATA state)
329                
330        my $token = $self->_tokenize_attempt_to_consume_an_entity;        my $token = $self->_tokenize_attempt_to_consume_an_entity (0);
331    
332        $self->{state} = 'data';        $self->{state} = DATA_STATE;
333        # next-input-character is already done        # next-input-character is already done
334    
335        unless (defined $token) {        unless (defined $token) {
336          !!!emit ({type => 'character', data => '&'});          !!!emit ({type => CHARACTER_TOKEN, data => '&'});
337        } else {        } else {
338          !!!emit ($token);          !!!emit ($token);
339        }        }
340    
341        redo A;        redo A;
342      } elsif ($self->{state} eq 'tag open') {      } elsif ($self->{state} == TAG_OPEN_STATE) {
343        if ($self->{content_model_flag} eq 'RCDATA' or        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
           $self->{content_model_flag} eq 'CDATA') {  
344          if ($self->{next_input_character} == 0x002F) { # /          if ($self->{next_input_character} == 0x002F) { # /
345            !!!next-input-character;            !!!next-input-character;
346            $self->{state} = 'close tag open';            $self->{state} = CLOSE_TAG_OPEN_STATE;
347            redo A;            redo A;
348          } else {          } else {
349            ## reconsume            ## reconsume
350            $self->{state} = 'data';            $self->{state} = DATA_STATE;
351    
352            !!!emit ({type => 'character', data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<'});
353    
354            redo A;            redo A;
355          }          }
356        } elsif ($self->{content_model_flag} eq 'PCDATA') {        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA
357          if ($self->{next_input_character} == 0x0021) { # !          if ($self->{next_input_character} == 0x0021) { # !
358            $self->{state} = 'markup declaration open';            $self->{state} = MARKUP_DECLARATION_OPEN_STATE;
359            !!!next-input-character;            !!!next-input-character;
360            redo A;            redo A;
361          } elsif ($self->{next_input_character} == 0x002F) { # /          } elsif ($self->{next_input_character} == 0x002F) { # /
362            $self->{state} = 'close tag open';            $self->{state} = CLOSE_TAG_OPEN_STATE;
363            !!!next-input-character;            !!!next-input-character;
364            redo A;            redo A;
365          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_input_character} and
366                   $self->{next_input_character} <= 0x005A) { # A..Z                   $self->{next_input_character} <= 0x005A) { # A..Z
367            $self->{current_token}            $self->{current_token}
368              = {type => 'start tag',              = {type => START_TAG_TOKEN,
369                 tag_name => chr ($self->{next_input_character} + 0x0020)};                 tag_name => chr ($self->{next_input_character} + 0x0020)};
370            $self->{state} = 'tag name';            $self->{state} = TAG_NAME_STATE;
371            !!!next-input-character;            !!!next-input-character;
372            redo A;            redo A;
373          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_input_character} and
374                   $self->{next_input_character} <= 0x007A) { # a..z                   $self->{next_input_character} <= 0x007A) { # a..z
375            $self->{current_token} = {type => 'start tag',            $self->{current_token} = {type => START_TAG_TOKEN,
376                              tag_name => chr ($self->{next_input_character})};                              tag_name => chr ($self->{next_input_character})};
377            $self->{state} = 'tag name';            $self->{state} = TAG_NAME_STATE;
378            !!!next-input-character;            !!!next-input-character;
379            redo A;            redo A;
380          } elsif ($self->{next_input_character} == 0x003E) { # >          } elsif ($self->{next_input_character} == 0x003E) { # >
381            !!!parse-error;            !!!parse-error (type => 'empty start tag');
382            $self->{state} = 'data';            $self->{state} = DATA_STATE;
383            !!!next-input-character;            !!!next-input-character;
384    
385            !!!emit ({type => 'character', data => '<>'});            !!!emit ({type => CHARACTER_TOKEN, data => '<>'});
386    
387            redo A;            redo A;
388          } elsif ($self->{next_input_character} == 0x003F) { # ?          } elsif ($self->{next_input_character} == 0x003F) { # ?
389            !!!parse-error;            !!!parse-error (type => 'pio');
390            $self->{state} = 'bogus comment';            $self->{state} = BOGUS_COMMENT_STATE;
391            ## $self->{next_input_character} is intentionally left as is            ## $self->{next_input_character} is intentionally left as is
392            redo A;            redo A;
393          } else {          } else {
394            !!!parse-error;            !!!parse-error (type => 'bare stago');
395            $self->{state} = 'data';            $self->{state} = DATA_STATE;
396            ## reconsume            ## reconsume
397    
398            !!!emit ({type => 'character', data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<'});
399    
400            redo A;            redo A;
401          }          }
402        } else {        } else {
403          die "$0: $self->{content_model_flag}: Unknown content model flag";          die "$0: $self->{content_model} in tag open";
404        }        }
405      } elsif ($self->{state} eq 'close tag open') {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
406        if ($self->{content_model_flag} eq 'RCDATA' or        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
407            $self->{content_model_flag} eq 'CDATA') {          if (defined $self->{last_emitted_start_tag_name}) {
408          my @next_char;            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>
409          TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {            my @next_char;
410              TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {
411                push @next_char, $self->{next_input_character};
412                my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);
413                my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;
414                if ($self->{next_input_character} == $c or $self->{next_input_character} == $C) {
415                  !!!next-input-character;
416                  next TAGNAME;
417                } else {
418                  $self->{next_input_character} = shift @next_char; # reconsume
419                  !!!back-next-input-character (@next_char);
420                  $self->{state} = DATA_STATE;
421    
422                  !!!emit ({type => CHARACTER_TOKEN, data => '</'});
423      
424                  redo A;
425                }
426              }
427            push @next_char, $self->{next_input_character};            push @next_char, $self->{next_input_character};
428            my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);        
429            my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;            unless ($self->{next_input_character} == 0x0009 or # HT
430            if ($self->{next_input_character} == $c or $self->{next_input_character} == $C) {                    $self->{next_input_character} == 0x000A or # LF
431              !!!next-input-character;                    $self->{next_input_character} == 0x000B or # VT
432              next TAGNAME;                    $self->{next_input_character} == 0x000C or # FF
433            } else {                    $self->{next_input_character} == 0x0020 or # SP
434              !!!parse-error;                    $self->{next_input_character} == 0x003E or # >
435                      $self->{next_input_character} == 0x002F or # /
436                      $self->{next_input_character} == -1) {
437              $self->{next_input_character} = shift @next_char; # reconsume              $self->{next_input_character} = shift @next_char; # reconsume
438              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
439              $self->{state} = 'data';              $self->{state} = DATA_STATE;
440                !!!emit ({type => CHARACTER_TOKEN, data => '</'});
             !!!emit ({type => 'character', data => '</'});  
   
441              redo A;              redo A;
442              } else {
443                $self->{next_input_character} = shift @next_char;
444                !!!back-next-input-character (@next_char);
445                # and consume...
446            }            }
         }  
         push @next_char, $self->{next_input_character};  
       
         unless ($self->{next_input_character} == 0x0009 or # HT  
                 $self->{next_input_character} == 0x000A or # LF  
                 $self->{next_input_character} == 0x000B or # VT  
                 $self->{next_input_character} == 0x000C or # FF  
                 $self->{next_input_character} == 0x0020 or # SP  
                 $self->{next_input_character} == 0x003E or # >  
                 $self->{next_input_character} == 0x002F or # /  
                 $self->{next_input_character} == 0x003C or # <  
                 $self->{next_input_character} == -1) {  
           !!!parse-error;  
           $self->{next_input_character} = shift @next_char; # reconsume  
           !!!back-next-input-character (@next_char);  
           $self->{state} = 'data';  
   
           !!!emit ({type => 'character', data => '</'});  
   
           redo A;  
447          } else {          } else {
448            $self->{next_input_character} = shift @next_char;            ## No start tag token has ever been emitted
449            !!!back-next-input-character (@next_char);            # next-input-character is already done
450            # and consume...            $self->{state} = DATA_STATE;
451              !!!emit ({type => CHARACTER_TOKEN, data => '</'});
452              redo A;
453          }          }
454        }        }
455                
456        if (0x0041 <= $self->{next_input_character} and        if (0x0041 <= $self->{next_input_character} and
457            $self->{next_input_character} <= 0x005A) { # A..Z            $self->{next_input_character} <= 0x005A) { # A..Z
458          $self->{current_token} = {type => 'end tag',          $self->{current_token} = {type => END_TAG_TOKEN,
459                            tag_name => chr ($self->{next_input_character} + 0x0020)};                            tag_name => chr ($self->{next_input_character} + 0x0020)};
460          $self->{state} = 'tag name';          $self->{state} = TAG_NAME_STATE;
461          !!!next-input-character;          !!!next-input-character;
462          redo A;          redo A;
463        } elsif (0x0061 <= $self->{next_input_character} and        } elsif (0x0061 <= $self->{next_input_character} and
464                 $self->{next_input_character} <= 0x007A) { # a..z                 $self->{next_input_character} <= 0x007A) { # a..z
465          $self->{current_token} = {type => 'end tag',          $self->{current_token} = {type => END_TAG_TOKEN,
466                            tag_name => chr ($self->{next_input_character})};                            tag_name => chr ($self->{next_input_character})};
467          $self->{state} = 'tag name';          $self->{state} = TAG_NAME_STATE;
468          !!!next-input-character;          !!!next-input-character;
469          redo A;          redo A;
470        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
471          !!!parse-error;          !!!parse-error (type => 'empty end tag');
472          $self->{state} = 'data';          $self->{state} = DATA_STATE;
473          !!!next-input-character;          !!!next-input-character;
474          redo A;          redo A;
475        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
476          !!!parse-error;          !!!parse-error (type => 'bare etago');
477          $self->{state} = 'data';          $self->{state} = DATA_STATE;
478          # reconsume          # reconsume
479    
480          !!!emit ({type => 'character', data => '</'});          !!!emit ({type => CHARACTER_TOKEN, data => '</'});
481    
482          redo A;          redo A;
483        } else {        } else {
484          !!!parse-error;          !!!parse-error (type => 'bogus end tag');
485          $self->{state} = 'bogus comment';          $self->{state} = BOGUS_COMMENT_STATE;
486          ## $self->{next_input_character} is intentionally left as is          ## $self->{next_input_character} is intentionally left as is
487          redo A;          redo A;
488        }        }
489      } elsif ($self->{state} eq 'tag name') {      } elsif ($self->{state} == TAG_NAME_STATE) {
490        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
491            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
492            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
493            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
494            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
495          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
496          !!!next-input-character;          !!!next-input-character;
497          redo A;          redo A;
498        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
499          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
500              $self->{current_token}->{first_start_tag}
501                  = not defined $self->{last_emitted_start_tag_name};
502            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
503          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
504            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
505            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
506              !!!parse-error;              !!!parse-error (type => 'end tag attribute');
507            }            }
508          } else {          } else {
509            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
510          }          }
511          $self->{state} = 'data';          $self->{state} = DATA_STATE;
512          !!!next-input-character;          !!!next-input-character;
513    
514          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
515    
516          redo A;          redo A;
517        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_input_character} and
# Line 627  sub _get_next_token ($) { Line 521  sub _get_next_token ($) {
521          ## Stay in this state          ## Stay in this state
522          !!!next-input-character;          !!!next-input-character;
523          redo A;          redo A;
524        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_input_character} == -1) {
525                 $self->{next_input_character} == -1) {          !!!parse-error (type => 'unclosed tag');
526          !!!parse-error;          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
527          if ($self->{current_token}->{type} eq 'start tag') {            $self->{current_token}->{first_start_tag}
528                  = not defined $self->{last_emitted_start_tag_name};
529            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
530          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
531            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
532            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
533              !!!parse-error;              !!!parse-error (type => 'end tag attribute');
534            }            }
535          } else {          } else {
536            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
537          }          }
538          $self->{state} = 'data';          $self->{state} = DATA_STATE;
539          # reconsume          # reconsume
540    
541          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
542    
543          redo A;          redo A;
544        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_input_character} == 0x002F) { # /
545          !!!next-input-character;          !!!next-input-character;
546          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_input_character} == 0x003E and # >
547              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
548              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
549            # permitted slash            # permitted slash
550            #            #
551          } else {          } else {
552            !!!parse-error;            !!!parse-error (type => 'nestc');
553          }          }
554          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
555          # next-input-character is already done          # next-input-character is already done
556          redo A;          redo A;
557        } else {        } else {
# Line 667  sub _get_next_token ($) { Line 561  sub _get_next_token ($) {
561          !!!next-input-character;          !!!next-input-character;
562          redo A;          redo A;
563        }        }
564      } elsif ($self->{state} eq 'before attribute name') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_NAME_STATE) {
565        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
566            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
567            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
# Line 677  sub _get_next_token ($) { Line 571  sub _get_next_token ($) {
571          !!!next-input-character;          !!!next-input-character;
572          redo A;          redo A;
573        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
574          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
575              $self->{current_token}->{first_start_tag}
576                  = not defined $self->{last_emitted_start_tag_name};
577            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
578          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
579            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
580            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
581              !!!parse-error;              !!!parse-error (type => 'end tag attribute');
582            }            }
583          } else {          } else {
584            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
585          }          }
586          $self->{state} = 'data';          $self->{state} = DATA_STATE;
587          !!!next-input-character;          !!!next-input-character;
588    
589          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
590    
591          redo A;          redo A;
592        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_input_character} and
593                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_input_character} <= 0x005A) { # A..Z
594          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),
595                                value => ''};                                value => ''};
596          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
597          !!!next-input-character;          !!!next-input-character;
598          redo A;          redo A;
599        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_input_character} == 0x002F) { # /
600          !!!next-input-character;          !!!next-input-character;
601          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_input_character} == 0x003E and # >
602              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
603              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
604            # permitted slash            # permitted slash
605            #            #
606          } else {          } else {
607            !!!parse-error;            !!!parse-error (type => 'nestc');
608          }          }
609          ## Stay in the state          ## Stay in the state
610          # next-input-character is already done          # next-input-character is already done
611          redo A;          redo A;
612        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_input_character} == -1) {
613                 $self->{next_input_character} == -1) {          !!!parse-error (type => 'unclosed tag');
614          !!!parse-error;          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
615          if ($self->{current_token}->{type} eq 'start tag') {            $self->{current_token}->{first_start_tag}
616                  = not defined $self->{last_emitted_start_tag_name};
617            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
618          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
619            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
620            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
621              !!!parse-error;              !!!parse-error (type => 'end tag attribute');
622            }            }
623          } else {          } else {
624            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
625          }          }
626          $self->{state} = 'data';          $self->{state} = DATA_STATE;
627          # reconsume          # reconsume
628    
629          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
630    
631          redo A;          redo A;
632        } else {        } else {
633          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          $self->{current_attribute} = {name => chr ($self->{next_input_character}),
634                                value => ''};                                value => ''};
635          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
636          !!!next-input-character;          !!!next-input-character;
637          redo A;          redo A;
638        }        }
639      } elsif ($self->{state} eq 'attribute name') {      } elsif ($self->{state} == ATTRIBUTE_NAME_STATE) {
640        my $before_leave = sub {        my $before_leave = sub {
641          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
642              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
643            !!!parse-error;            !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name});
644            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
645          } else {          } else {
646            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}
# Line 759  sub _get_next_token ($) { Line 654  sub _get_next_token ($) {
654            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
655            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
656          $before_leave->();          $before_leave->();
657          $self->{state} = 'after attribute name';          $self->{state} = AFTER_ATTRIBUTE_NAME_STATE;
658          !!!next-input-character;          !!!next-input-character;
659          redo A;          redo A;
660        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_input_character} == 0x003D) { # =
661          $before_leave->();          $before_leave->();
662          $self->{state} = 'before attribute value';          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
663          !!!next-input-character;          !!!next-input-character;
664          redo A;          redo A;
665        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
666          $before_leave->();          $before_leave->();
667          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
668              $self->{current_token}->{first_start_tag}
669                  = not defined $self->{last_emitted_start_tag_name};
670            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
671          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
672            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
673            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
674              !!!parse-error;              !!!parse-error (type => 'end tag attribute');
675            }            }
676          } else {          } else {
677            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
678          }          }
679          $self->{state} = 'data';          $self->{state} = DATA_STATE;
680          !!!next-input-character;          !!!next-input-character;
681    
682          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
683    
684          redo A;          redo A;
685        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_input_character} and
# Line 796  sub _get_next_token ($) { Line 692  sub _get_next_token ($) {
692          $before_leave->();          $before_leave->();
693          !!!next-input-character;          !!!next-input-character;
694          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_input_character} == 0x003E and # >
695              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
696              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
697            # permitted slash            # permitted slash
698            #            #
699          } else {          } else {
700            !!!parse-error;            !!!parse-error (type => 'nestc');
701          }          }
702          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
703          # next-input-character is already done          # next-input-character is already done
704          redo A;          redo A;
705        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_input_character} == -1) {
706                 $self->{next_input_character} == -1) {          !!!parse-error (type => 'unclosed tag');
         !!!parse-error;  
707          $before_leave->();          $before_leave->();
708          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
709              $self->{current_token}->{first_start_tag}
710                  = not defined $self->{last_emitted_start_tag_name};
711            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
712          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
713            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
714            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
715              !!!parse-error;              !!!parse-error (type => 'end tag attribute');
716            }            }
717          } else {          } else {
718            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
719          }          }
720          $self->{state} = 'data';          $self->{state} = DATA_STATE;
721          # reconsume          # reconsume
722    
723          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
724    
725          redo A;          redo A;
726        } else {        } else {
# Line 833  sub _get_next_token ($) { Line 729  sub _get_next_token ($) {
729          !!!next-input-character;          !!!next-input-character;
730          redo A;          redo A;
731        }        }
732      } elsif ($self->{state} eq 'after attribute name') {      } elsif ($self->{state} == AFTER_ATTRIBUTE_NAME_STATE) {
733        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
734            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
735            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
# Line 843  sub _get_next_token ($) { Line 739  sub _get_next_token ($) {
739          !!!next-input-character;          !!!next-input-character;
740          redo A;          redo A;
741        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_input_character} == 0x003D) { # =
742          $self->{state} = 'before attribute value';          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
743          !!!next-input-character;          !!!next-input-character;
744          redo A;          redo A;
745        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
746          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
747              $self->{current_token}->{first_start_tag}
748                  = not defined $self->{last_emitted_start_tag_name};
749            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
750          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
751            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
752            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
753              !!!parse-error;              !!!parse-error (type => 'end tag attribute');
754            }            }
755          } else {          } else {
756            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
757          }          }
758          $self->{state} = 'data';          $self->{state} = DATA_STATE;
759          !!!next-input-character;          !!!next-input-character;
760    
761          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
762    
763          redo A;          redo A;
764        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_input_character} and
765                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_input_character} <= 0x005A) { # A..Z
766          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),
767                                value => ''};                                value => ''};
768          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
769          !!!next-input-character;          !!!next-input-character;
770          redo A;          redo A;
771        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_input_character} == 0x002F) { # /
772          !!!next-input-character;          !!!next-input-character;
773          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_input_character} == 0x003E and # >
774              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
775              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
776            # permitted slash            # permitted slash
777            #            #
778          } else {          } else {
779            !!!parse-error;            !!!parse-error (type => 'nestc');
780              ## TODO: Different error type for <aa / bb> than <aa/>
781          }          }
782          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
783          # next-input-character is already done          # next-input-character is already done
784          redo A;          redo A;
785        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_input_character} == -1) {
786                 $self->{next_input_character} == -1) {          !!!parse-error (type => 'unclosed tag');
787          !!!parse-error;          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
788          if ($self->{current_token}->{type} eq 'start tag') {            $self->{current_token}->{first_start_tag}
789                  = not defined $self->{last_emitted_start_tag_name};
790            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
791          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
792            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
793            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
794              !!!parse-error;              !!!parse-error (type => 'end tag attribute');
795            }            }
796          } else {          } else {
797            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
798          }          }
799          $self->{state} = 'data';          $self->{state} = DATA_STATE;
800          # reconsume          # reconsume
801    
802          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
803    
804          redo A;          redo A;
805        } else {        } else {
806          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          $self->{current_attribute} = {name => chr ($self->{next_input_character}),
807                                value => ''};                                value => ''};
808          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
809          !!!next-input-character;          !!!next-input-character;
810          redo A;                  redo A;        
811        }        }
812      } elsif ($self->{state} eq 'before attribute value') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_VALUE_STATE) {
813        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
814            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
815            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
# Line 921  sub _get_next_token ($) { Line 819  sub _get_next_token ($) {
819          !!!next-input-character;          !!!next-input-character;
820          redo A;          redo A;
821        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_input_character} == 0x0022) { # "
822          $self->{state} = 'attribute value (double-quoted)';          $self->{state} = ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE;
823          !!!next-input-character;          !!!next-input-character;
824          redo A;          redo A;
825        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_input_character} == 0x0026) { # &
826          $self->{state} = 'attribute value (unquoted)';          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
827          ## reconsume          ## reconsume
828          redo A;          redo A;
829        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_input_character} == 0x0027) { # '
830          $self->{state} = 'attribute value (single-quoted)';          $self->{state} = ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE;
831          !!!next-input-character;          !!!next-input-character;
832          redo A;          redo A;
833        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
834          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
835              $self->{current_token}->{first_start_tag}
836                  = not defined $self->{last_emitted_start_tag_name};
837            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
838          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
839            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
840            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
841              !!!parse-error;              !!!parse-error (type => 'end tag attribute');
842            }            }
843          } else {          } else {
844            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
845          }          }
846          $self->{state} = 'data';          $self->{state} = DATA_STATE;
847          !!!next-input-character;          !!!next-input-character;
848    
849          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
850    
851          redo A;          redo A;
852        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_input_character} == -1) {
853                 $self->{next_input_character} == -1) {          !!!parse-error (type => 'unclosed tag');
854          !!!parse-error;          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
855          if ($self->{current_token}->{type} eq 'start tag') {            $self->{current_token}->{first_start_tag}
856                  = not defined $self->{last_emitted_start_tag_name};
857            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
858          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
859            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
860            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
861              !!!parse-error;              !!!parse-error (type => 'end tag attribute');
862            }            }
863          } else {          } else {
864            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
865          }          }
866          $self->{state} = 'data';          $self->{state} = DATA_STATE;
867          ## reconsume          ## reconsume
868    
869          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
870    
871          redo A;          redo A;
872        } else {        } else {
873          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});
874          $self->{state} = 'attribute value (unquoted)';          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
875          !!!next-input-character;          !!!next-input-character;
876          redo A;          redo A;
877        }        }
878      } elsif ($self->{state} eq 'attribute value (double-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE) {
879        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_input_character} == 0x0022) { # "
880          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
881          !!!next-input-character;          !!!next-input-character;
882          redo A;          redo A;
883        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_input_character} == 0x0026) { # &
884          $self->{last_attribute_value_state} = 'attribute value (double-quoted)';          $self->{last_attribute_value_state} = $self->{state};
885          $self->{state} = 'entity in attribute value';          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
886          !!!next-input-character;          !!!next-input-character;
887          redo A;          redo A;
888        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
889          !!!parse-error;          !!!parse-error (type => 'unclosed attribute value');
890          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
891              $self->{current_token}->{first_start_tag}
892                  = not defined $self->{last_emitted_start_tag_name};
893            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
894          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
895            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
896            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
897              !!!parse-error;              !!!parse-error (type => 'end tag attribute');
898            }            }
899          } else {          } else {
900            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
901          }          }
902          $self->{state} = 'data';          $self->{state} = DATA_STATE;
903          ## reconsume          ## reconsume
904    
905          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
906    
907          redo A;          redo A;
908        } else {        } else {
# Line 1011  sub _get_next_token ($) { Line 911  sub _get_next_token ($) {
911          !!!next-input-character;          !!!next-input-character;
912          redo A;          redo A;
913        }        }
914      } elsif ($self->{state} eq 'attribute value (single-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE) {
915        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_input_character} == 0x0027) { # '
916          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
917          !!!next-input-character;          !!!next-input-character;
918          redo A;          redo A;
919        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_input_character} == 0x0026) { # &
920          $self->{last_attribute_value_state} = 'attribute value (single-quoted)';          $self->{last_attribute_value_state} = $self->{state};
921          $self->{state} = 'entity in attribute value';          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
922          !!!next-input-character;          !!!next-input-character;
923          redo A;          redo A;
924        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
925          !!!parse-error;          !!!parse-error (type => 'unclosed attribute value');
926          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
927              $self->{current_token}->{first_start_tag}
928                  = not defined $self->{last_emitted_start_tag_name};
929            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
930          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
931            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
932            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
933              !!!parse-error;              !!!parse-error (type => 'end tag attribute');
934            }            }
935          } else {          } else {
936            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
937          }          }
938          $self->{state} = 'data';          $self->{state} = DATA_STATE;
939          ## reconsume          ## reconsume
940    
941          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
942    
943          redo A;          redo A;
944        } else {        } else {
# Line 1046  sub _get_next_token ($) { Line 947  sub _get_next_token ($) {
947          !!!next-input-character;          !!!next-input-character;
948          redo A;          redo A;
949        }        }
950      } elsif ($self->{state} eq 'attribute value (unquoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_UNQUOTED_STATE) {
951        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
952            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
953            $self->{next_input_character} == 0x000B or # HT            $self->{next_input_character} == 0x000B or # HT
954            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
955            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
956          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
957          !!!next-input-character;          !!!next-input-character;
958          redo A;          redo A;
959        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_input_character} == 0x0026) { # &
960          $self->{last_attribute_value_state} = 'attribute value (unquoted)';          $self->{last_attribute_value_state} = $self->{state};
961          $self->{state} = 'entity in attribute value';          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
962          !!!next-input-character;          !!!next-input-character;
963          redo A;          redo A;
964        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
965          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
966              $self->{current_token}->{first_start_tag}
967                  = not defined $self->{last_emitted_start_tag_name};
968            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
969          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
970            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
971            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
972              !!!parse-error;              !!!parse-error (type => 'end tag attribute');
973            }            }
974          } else {          } else {
975            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
976          }          }
977          $self->{state} = 'data';          $self->{state} = DATA_STATE;
978          !!!next-input-character;          !!!next-input-character;
979    
980          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
981    
982          redo A;          redo A;
983        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_input_character} == -1) {
984                 $self->{next_input_character} == -1) {          !!!parse-error (type => 'unclosed tag');
985          !!!parse-error;          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
986          if ($self->{current_token}->{type} eq 'start tag') {            $self->{current_token}->{first_start_tag}
987                  = not defined $self->{last_emitted_start_tag_name};
988            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
989          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
990            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
991            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
992              !!!parse-error;              !!!parse-error (type => 'end tag attribute');
993            }            }
994          } else {          } else {
995            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
996          }          }
997          $self->{state} = 'data';          $self->{state} = DATA_STATE;
998          ## reconsume          ## reconsume
999    
1000          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
1001    
1002          redo A;          redo A;
1003        } else {        } else {
# Line 1104  sub _get_next_token ($) { Line 1006  sub _get_next_token ($) {
1006          !!!next-input-character;          !!!next-input-character;
1007          redo A;          redo A;
1008        }        }
1009      } elsif ($self->{state} eq 'entity in attribute value') {      } elsif ($self->{state} == ENTITY_IN_ATTRIBUTE_VALUE_STATE) {
1010        my $token = $self->_tokenize_attempt_to_consume_an_entity;        my $token = $self->_tokenize_attempt_to_consume_an_entity (1);
1011    
1012        unless (defined $token) {        unless (defined $token) {
1013          $self->{current_attribute}->{value} .= '&';          $self->{current_attribute}->{value} .= '&';
# Line 1117  sub _get_next_token ($) { Line 1019  sub _get_next_token ($) {
1019        $self->{state} = $self->{last_attribute_value_state};        $self->{state} = $self->{last_attribute_value_state};
1020        # next-input-character is already done        # next-input-character is already done
1021        redo A;        redo A;
1022      } elsif ($self->{state} eq 'bogus comment') {      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
1023        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1024                
1025        my $token = {type => 'comment', data => ''};        my $token = {type => COMMENT_TOKEN, data => ''};
1026    
1027        BC: {        BC: {
1028          if ($self->{next_input_character} == 0x003E) { # >          if ($self->{next_input_character} == 0x003E) { # >
1029            $self->{state} = 'data';            $self->{state} = DATA_STATE;
1030            !!!next-input-character;            !!!next-input-character;
1031    
1032            !!!emit ($token);            !!!emit ($token);
1033    
1034            redo A;            redo A;
1035          } elsif ($self->{next_input_character} == -1) {          } elsif ($self->{next_input_character} == -1) {
1036            $self->{state} = 'data';            $self->{state} = DATA_STATE;
1037            ## reconsume            ## reconsume
1038    
1039            !!!emit ($token);            !!!emit ($token);
# Line 1143  sub _get_next_token ($) { Line 1045  sub _get_next_token ($) {
1045            redo BC;            redo BC;
1046          }          }
1047        } # BC        } # BC
1048      } elsif ($self->{state} eq 'markup declaration open') {      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
1049        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1050    
1051        my @next_char;        my @next_char;
# Line 1153  sub _get_next_token ($) { Line 1055  sub _get_next_token ($) {
1055          !!!next-input-character;          !!!next-input-character;
1056          push @next_char, $self->{next_input_character};          push @next_char, $self->{next_input_character};
1057          if ($self->{next_input_character} == 0x002D) { # -          if ($self->{next_input_character} == 0x002D) { # -
1058            $self->{current_token} = {type => 'comment', data => ''};            $self->{current_token} = {type => COMMENT_TOKEN, data => ''};
1059            $self->{state} = 'comment';            $self->{state} = COMMENT_START_STATE;
1060            !!!next-input-character;            !!!next-input-character;
1061            redo A;            redo A;
1062          }          }
# Line 1185  sub _get_next_token ($) { Line 1087  sub _get_next_token ($) {
1087                    if ($self->{next_input_character} == 0x0045 or # E                    if ($self->{next_input_character} == 0x0045 or # E
1088                        $self->{next_input_character} == 0x0065) { # e                        $self->{next_input_character} == 0x0065) { # e
1089                      ## ISSUE: What a stupid code this is!                      ## ISSUE: What a stupid code this is!
1090                      $self->{state} = 'DOCTYPE';                      $self->{state} = DOCTYPE_STATE;
1091                      !!!next-input-character;                      !!!next-input-character;
1092                      redo A;                      redo A;
1093                    }                    }
# Line 1196  sub _get_next_token ($) { Line 1098  sub _get_next_token ($) {
1098          }          }
1099        }        }
1100    
1101        !!!parse-error;        !!!parse-error (type => 'bogus comment');
1102        $self->{next_input_character} = shift @next_char;        $self->{next_input_character} = shift @next_char;
1103        !!!back-next-input-character (@next_char);        !!!back-next-input-character (@next_char);
1104        $self->{state} = 'bogus comment';        $self->{state} = BOGUS_COMMENT_STATE;
1105        redo A;        redo A;
1106                
1107        ## ISSUE: typos in spec: chacacters, is is a parse error        ## ISSUE: typos in spec: chacacters, is is a parse error
1108        ## 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?
1109      } elsif ($self->{state} eq 'comment') {      } elsif ($self->{state} == COMMENT_START_STATE) {
1110          if ($self->{next_input_character} == 0x002D) { # -
1111            $self->{state} = COMMENT_START_DASH_STATE;
1112            !!!next-input-character;
1113            redo A;
1114          } elsif ($self->{next_input_character} == 0x003E) { # >
1115            !!!parse-error (type => 'bogus comment');
1116            $self->{state} = DATA_STATE;
1117            !!!next-input-character;
1118    
1119            !!!emit ($self->{current_token}); # comment
1120    
1121            redo A;
1122          } elsif ($self->{next_input_character} == -1) {
1123            !!!parse-error (type => 'unclosed comment');
1124            $self->{state} = DATA_STATE;
1125            ## reconsume
1126    
1127            !!!emit ($self->{current_token}); # comment
1128    
1129            redo A;
1130          } else {
1131            $self->{current_token}->{data} # comment
1132                .= chr ($self->{next_input_character});
1133            $self->{state} = COMMENT_STATE;
1134            !!!next-input-character;
1135            redo A;
1136          }
1137        } elsif ($self->{state} == COMMENT_START_DASH_STATE) {
1138        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_input_character} == 0x002D) { # -
1139          $self->{state} = 'comment dash';          $self->{state} = COMMENT_END_STATE;
1140          !!!next-input-character;          !!!next-input-character;
1141          redo A;          redo A;
1142          } elsif ($self->{next_input_character} == 0x003E) { # >
1143            !!!parse-error (type => 'bogus comment');
1144            $self->{state} = DATA_STATE;
1145            !!!next-input-character;
1146    
1147            !!!emit ($self->{current_token}); # comment
1148    
1149            redo A;
1150        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1151          !!!parse-error;          !!!parse-error (type => 'unclosed comment');
1152          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1153            ## reconsume
1154    
1155            !!!emit ($self->{current_token}); # comment
1156    
1157            redo A;
1158          } else {
1159            $self->{current_token}->{data} # comment
1160                .= '-' . chr ($self->{next_input_character});
1161            $self->{state} = COMMENT_STATE;
1162            !!!next-input-character;
1163            redo A;
1164          }
1165        } elsif ($self->{state} == COMMENT_STATE) {
1166          if ($self->{next_input_character} == 0x002D) { # -
1167            $self->{state} = COMMENT_END_DASH_STATE;
1168            !!!next-input-character;
1169            redo A;
1170          } elsif ($self->{next_input_character} == -1) {
1171            !!!parse-error (type => 'unclosed comment');
1172            $self->{state} = DATA_STATE;
1173          ## reconsume          ## reconsume
1174    
1175          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
1176    
1177          redo A;          redo A;
1178        } else {        } else {
# Line 1224  sub _get_next_token ($) { Line 1181  sub _get_next_token ($) {
1181          !!!next-input-character;          !!!next-input-character;
1182          redo A;          redo A;
1183        }        }
1184      } elsif ($self->{state} eq 'comment dash') {      } elsif ($self->{state} == COMMENT_END_DASH_STATE) {
1185        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_input_character} == 0x002D) { # -
1186          $self->{state} = 'comment end';          $self->{state} = COMMENT_END_STATE;
1187          !!!next-input-character;          !!!next-input-character;
1188          redo A;          redo A;
1189        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1190          !!!parse-error;          !!!parse-error (type => 'unclosed comment');
1191          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1192          ## reconsume          ## reconsume
1193    
1194          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
1195    
1196          redo A;          redo A;
1197        } else {        } else {
1198          $self->{current_token}->{data} .= '-' . chr ($self->{next_input_character}); # comment          $self->{current_token}->{data} .= '-' . chr ($self->{next_input_character}); # comment
1199          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
1200          !!!next-input-character;          !!!next-input-character;
1201          redo A;          redo A;
1202        }        }
1203      } elsif ($self->{state} eq 'comment end') {      } elsif ($self->{state} == COMMENT_END_STATE) {
1204        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_input_character} == 0x003E) { # >
1205          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1206          !!!next-input-character;          !!!next-input-character;
1207    
1208          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
1209    
1210          redo A;          redo A;
1211        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_input_character} == 0x002D) { # -
1212          !!!parse-error;          !!!parse-error (type => 'dash in comment');
1213          $self->{current_token}->{data} .= '-'; # comment          $self->{current_token}->{data} .= '-'; # comment
1214          ## Stay in the state          ## Stay in the state
1215          !!!next-input-character;          !!!next-input-character;
1216          redo A;          redo A;
1217        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1218          !!!parse-error;          !!!parse-error (type => 'unclosed comment');
1219          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1220          ## reconsume          ## reconsume
1221    
1222          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
1223    
1224          redo A;          redo A;
1225        } else {        } else {
1226          !!!parse-error;          !!!parse-error (type => 'dash in comment');
1227          $self->{current_token}->{data} .= '--' . chr ($self->{next_input_character}); # comment          $self->{current_token}->{data} .= '--' . chr ($self->{next_input_character}); # comment
1228          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
1229          !!!next-input-character;          !!!next-input-character;
1230          redo A;          redo A;
1231        }        }
1232      } elsif ($self->{state} eq 'DOCTYPE') {      } elsif ($self->{state} == DOCTYPE_STATE) {
1233        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1234            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1235            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
1236            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
1237            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
1238          $self->{state} = 'before DOCTYPE name';          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
1239          !!!next-input-character;          !!!next-input-character;
1240          redo A;          redo A;
1241        } else {        } else {
1242          !!!parse-error;          !!!parse-error (type => 'no space before DOCTYPE name');
1243          $self->{state} = 'before DOCTYPE name';          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
1244          ## reconsume          ## reconsume
1245          redo A;          redo A;
1246        }        }
1247      } elsif ($self->{state} eq 'before DOCTYPE name') {      } elsif ($self->{state} == BEFORE_DOCTYPE_NAME_STATE) {
1248        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1249            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1250            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
# Line 1299  sub _get_next_token ($) { Line 1253  sub _get_next_token ($) {
1253          ## Stay in the state          ## Stay in the state
1254          !!!next-input-character;          !!!next-input-character;
1255          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;  
1256        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1257          !!!parse-error;          !!!parse-error (type => 'no DOCTYPE name');
1258          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1259          !!!next-input-character;          !!!next-input-character;
1260    
1261          !!!emit ({type => 'DOCTYPE', name => '', error => 1});          !!!emit ({type => DOCTYPE_TOKEN}); # incorrect
1262    
1263          redo A;          redo A;
1264        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1265          !!!parse-error;          !!!parse-error (type => 'no DOCTYPE name');
1266          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1267          ## reconsume          ## reconsume
1268    
1269          !!!emit ({type => 'DOCTYPE', name => '', error => 1});          !!!emit ({type => DOCTYPE_TOKEN}); # incorrect
1270    
1271          redo A;          redo A;
1272        } else {        } else {
1273          $self->{current_token} = {type => 'DOCTYPE',          $self->{current_token}
1274                            name => chr ($self->{next_input_character}),              = {type => DOCTYPE_TOKEN,
1275                            error => 1};                 name => chr ($self->{next_input_character}),
1276          $self->{state} = 'DOCTYPE name';                 correct => 1};
1277    ## ISSUE: "Set the token's name name to the" in the spec
1278            $self->{state} = DOCTYPE_NAME_STATE;
1279          !!!next-input-character;          !!!next-input-character;
1280          redo A;          redo A;
1281        }        }
1282      } elsif ($self->{state} eq 'DOCTYPE name') {      } elsif ($self->{state} == DOCTYPE_NAME_STATE) {
1283    ## ISSUE: Redundant "First," in the spec.
1284        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1285            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1286            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
1287            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
1288            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
1289          $self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML'); # DOCTYPE          $self->{state} = AFTER_DOCTYPE_NAME_STATE;
         $self->{state} = 'after DOCTYPE name';  
1290          !!!next-input-character;          !!!next-input-character;
1291          redo A;          redo A;
1292        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1293          $self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML'); # DOCTYPE          $self->{state} = DATA_STATE;
         $self->{state} = 'data';  
1294          !!!next-input-character;          !!!next-input-character;
1295    
1296          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1297    
1298          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;  
1299        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1300          !!!parse-error;          !!!parse-error (type => 'unclosed DOCTYPE');
1301          $self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML'); # DOCTYPE          $self->{state} = DATA_STATE;
         $self->{state} = 'data';  
1302          ## reconsume          ## reconsume
1303    
1304          !!!emit ($self->{current_token});          delete $self->{current_token}->{correct};
1305          undef $self->{current_token};          !!!emit ($self->{current_token}); # DOCTYPE
1306    
1307          redo A;          redo A;
1308        } else {        } else {
1309          $self->{current_token}->{name}          $self->{current_token}->{name}
1310            .= chr ($self->{next_input_character}); # DOCTYPE            .= chr ($self->{next_input_character}); # DOCTYPE
         #$self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML');  
1311          ## Stay in the state          ## Stay in the state
1312          !!!next-input-character;          !!!next-input-character;
1313          redo A;          redo A;
1314        }        }
1315      } elsif ($self->{state} eq 'after DOCTYPE name') {      } elsif ($self->{state} == AFTER_DOCTYPE_NAME_STATE) {
1316        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1317            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1318            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
# Line 1385  sub _get_next_token ($) { Line 1322  sub _get_next_token ($) {
1322          !!!next-input-character;          !!!next-input-character;
1323          redo A;          redo A;
1324        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1325          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1326            !!!next-input-character;
1327    
1328            !!!emit ($self->{current_token}); # DOCTYPE
1329    
1330            redo A;
1331          } elsif ($self->{next_input_character} == -1) {
1332            !!!parse-error (type => 'unclosed DOCTYPE');
1333            $self->{state} = DATA_STATE;
1334            ## reconsume
1335    
1336            delete $self->{current_token}->{correct};
1337            !!!emit ($self->{current_token}); # DOCTYPE
1338    
1339            redo A;
1340          } elsif ($self->{next_input_character} == 0x0050 or # P
1341                   $self->{next_input_character} == 0x0070) { # p
1342            !!!next-input-character;
1343            if ($self->{next_input_character} == 0x0055 or # U
1344                $self->{next_input_character} == 0x0075) { # u
1345              !!!next-input-character;
1346              if ($self->{next_input_character} == 0x0042 or # B
1347                  $self->{next_input_character} == 0x0062) { # b
1348                !!!next-input-character;
1349                if ($self->{next_input_character} == 0x004C or # L
1350                    $self->{next_input_character} == 0x006C) { # l
1351                  !!!next-input-character;
1352                  if ($self->{next_input_character} == 0x0049 or # I
1353                      $self->{next_input_character} == 0x0069) { # i
1354                    !!!next-input-character;
1355                    if ($self->{next_input_character} == 0x0043 or # C
1356                        $self->{next_input_character} == 0x0063) { # c
1357                      $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1358                      !!!next-input-character;
1359                      redo A;
1360                    }
1361                  }
1362                }
1363              }
1364            }
1365    
1366            #
1367          } elsif ($self->{next_input_character} == 0x0053 or # S
1368                   $self->{next_input_character} == 0x0073) { # s
1369            !!!next-input-character;
1370            if ($self->{next_input_character} == 0x0059 or # Y
1371                $self->{next_input_character} == 0x0079) { # y
1372              !!!next-input-character;
1373              if ($self->{next_input_character} == 0x0053 or # S
1374                  $self->{next_input_character} == 0x0073) { # s
1375                !!!next-input-character;
1376                if ($self->{next_input_character} == 0x0054 or # T
1377                    $self->{next_input_character} == 0x0074) { # t
1378                  !!!next-input-character;
1379                  if ($self->{next_input_character} == 0x0045 or # E
1380                      $self->{next_input_character} == 0x0065) { # e
1381                    !!!next-input-character;
1382                    if ($self->{next_input_character} == 0x004D or # M
1383                        $self->{next_input_character} == 0x006D) { # m
1384                      $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
1385                      !!!next-input-character;
1386                      redo A;
1387                    }
1388                  }
1389                }
1390              }
1391            }
1392    
1393            #
1394          } else {
1395            !!!next-input-character;
1396            #
1397          }
1398    
1399          !!!parse-error (type => 'string after DOCTYPE name');
1400          $self->{state} = BOGUS_DOCTYPE_STATE;
1401          # next-input-character is already done
1402          redo A;
1403        } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
1404          if ({
1405                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1406                #0x000D => 1, # HT, LF, VT, FF, SP, CR
1407              }->{$self->{next_input_character}}) {
1408            ## Stay in the state
1409            !!!next-input-character;
1410            redo A;
1411          } elsif ($self->{next_input_character} eq 0x0022) { # "
1412            $self->{current_token}->{public_identifier} = ''; # DOCTYPE
1413            $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;
1414            !!!next-input-character;
1415            redo A;
1416          } elsif ($self->{next_input_character} eq 0x0027) { # '
1417            $self->{current_token}->{public_identifier} = ''; # DOCTYPE
1418            $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;
1419            !!!next-input-character;
1420            redo A;
1421          } elsif ($self->{next_input_character} eq 0x003E) { # >
1422            !!!parse-error (type => 'no PUBLIC literal');
1423    
1424            $self->{state} = DATA_STATE;
1425            !!!next-input-character;
1426    
1427            delete $self->{current_token}->{correct};
1428            !!!emit ($self->{current_token}); # DOCTYPE
1429    
1430            redo A;
1431          } elsif ($self->{next_input_character} == -1) {
1432            !!!parse-error (type => 'unclosed DOCTYPE');
1433    
1434            $self->{state} = DATA_STATE;
1435            ## reconsume
1436    
1437            delete $self->{current_token}->{correct};
1438            !!!emit ($self->{current_token}); # DOCTYPE
1439    
1440            redo A;
1441          } else {
1442            !!!parse-error (type => 'string after PUBLIC');
1443            $self->{state} = BOGUS_DOCTYPE_STATE;
1444            !!!next-input-character;
1445            redo A;
1446          }
1447        } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {
1448          if ($self->{next_input_character} == 0x0022) { # "
1449            $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1450            !!!next-input-character;
1451            redo A;
1452          } elsif ($self->{next_input_character} == -1) {
1453            !!!parse-error (type => 'unclosed PUBLIC literal');
1454    
1455            $self->{state} = DATA_STATE;
1456            ## reconsume
1457    
1458            delete $self->{current_token}->{correct};
1459            !!!emit ($self->{current_token}); # DOCTYPE
1460    
1461            redo A;
1462          } else {
1463            $self->{current_token}->{public_identifier} # DOCTYPE
1464                .= chr $self->{next_input_character};
1465            ## Stay in the state
1466            !!!next-input-character;
1467            redo A;
1468          }
1469        } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {
1470          if ($self->{next_input_character} == 0x0027) { # '
1471            $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1472            !!!next-input-character;
1473            redo A;
1474          } elsif ($self->{next_input_character} == -1) {
1475            !!!parse-error (type => 'unclosed PUBLIC literal');
1476    
1477            $self->{state} = DATA_STATE;
1478            ## reconsume
1479    
1480            delete $self->{current_token}->{correct};
1481            !!!emit ($self->{current_token}); # DOCTYPE
1482    
1483            redo A;
1484          } else {
1485            $self->{current_token}->{public_identifier} # DOCTYPE
1486                .= chr $self->{next_input_character};
1487            ## Stay in the state
1488            !!!next-input-character;
1489            redo A;
1490          }
1491        } elsif ($self->{state} == AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
1492          if ({
1493                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1494                #0x000D => 1, # HT, LF, VT, FF, SP, CR
1495              }->{$self->{next_input_character}}) {
1496            ## Stay in the state
1497            !!!next-input-character;
1498            redo A;
1499          } elsif ($self->{next_input_character} == 0x0022) { # "
1500            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1501            $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
1502            !!!next-input-character;
1503            redo A;
1504          } elsif ($self->{next_input_character} == 0x0027) { # '
1505            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1506            $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
1507            !!!next-input-character;
1508            redo A;
1509          } elsif ($self->{next_input_character} == 0x003E) { # >
1510            $self->{state} = DATA_STATE;
1511          !!!next-input-character;          !!!next-input-character;
1512    
1513          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1514    
1515          redo A;          redo A;
1516        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1517          !!!parse-error;          !!!parse-error (type => 'unclosed DOCTYPE');
1518          $self->{state} = 'data';  
1519            $self->{state} = DATA_STATE;
1520          ## reconsume          ## reconsume
1521    
1522            delete $self->{current_token}->{correct};
1523          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1524    
1525          redo A;          redo A;
1526        } else {        } else {
1527          !!!parse-error;          !!!parse-error (type => 'string after PUBLIC literal');
1528          $self->{current_token}->{error} = 1; # DOCTYPE          $self->{state} = BOGUS_DOCTYPE_STATE;
         $self->{state} = 'bogus DOCTYPE';  
1529          !!!next-input-character;          !!!next-input-character;
1530          redo A;          redo A;
1531        }        }
1532      } elsif ($self->{state} eq 'bogus DOCTYPE') {      } elsif ($self->{state} == BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
1533          if ({
1534                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1535                #0x000D => 1, # HT, LF, VT, FF, SP, CR
1536              }->{$self->{next_input_character}}) {
1537            ## Stay in the state
1538            !!!next-input-character;
1539            redo A;
1540          } elsif ($self->{next_input_character} == 0x0022) { # "
1541            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1542            $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
1543            !!!next-input-character;
1544            redo A;
1545          } elsif ($self->{next_input_character} == 0x0027) { # '
1546            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1547            $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
1548            !!!next-input-character;
1549            redo A;
1550          } elsif ($self->{next_input_character} == 0x003E) { # >
1551            !!!parse-error (type => 'no SYSTEM literal');
1552            $self->{state} = DATA_STATE;
1553            !!!next-input-character;
1554    
1555            delete $self->{current_token}->{correct};
1556            !!!emit ($self->{current_token}); # DOCTYPE
1557    
1558            redo A;
1559          } elsif ($self->{next_input_character} == -1) {
1560            !!!parse-error (type => 'unclosed DOCTYPE');
1561    
1562            $self->{state} = DATA_STATE;
1563            ## reconsume
1564    
1565            delete $self->{current_token}->{correct};
1566            !!!emit ($self->{current_token}); # DOCTYPE
1567    
1568            redo A;
1569          } else {
1570            !!!parse-error (type => 'string after SYSTEM');
1571            $self->{state} = BOGUS_DOCTYPE_STATE;
1572            !!!next-input-character;
1573            redo A;
1574          }
1575        } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {
1576          if ($self->{next_input_character} == 0x0022) { # "
1577            $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
1578            !!!next-input-character;
1579            redo A;
1580          } elsif ($self->{next_input_character} == -1) {
1581            !!!parse-error (type => 'unclosed SYSTEM literal');
1582    
1583            $self->{state} = DATA_STATE;
1584            ## reconsume
1585    
1586            delete $self->{current_token}->{correct};
1587            !!!emit ($self->{current_token}); # DOCTYPE
1588    
1589            redo A;
1590          } else {
1591            $self->{current_token}->{system_identifier} # DOCTYPE
1592                .= chr $self->{next_input_character};
1593            ## Stay in the state
1594            !!!next-input-character;
1595            redo A;
1596          }
1597        } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {
1598          if ($self->{next_input_character} == 0x0027) { # '
1599            $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
1600            !!!next-input-character;
1601            redo A;
1602          } elsif ($self->{next_input_character} == -1) {
1603            !!!parse-error (type => 'unclosed SYSTEM literal');
1604    
1605            $self->{state} = DATA_STATE;
1606            ## reconsume
1607    
1608            delete $self->{current_token}->{correct};
1609            !!!emit ($self->{current_token}); # DOCTYPE
1610    
1611            redo A;
1612          } else {
1613            $self->{current_token}->{system_identifier} # DOCTYPE
1614                .= chr $self->{next_input_character};
1615            ## Stay in the state
1616            !!!next-input-character;
1617            redo A;
1618          }
1619        } elsif ($self->{state} == AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
1620          if ({
1621                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1622                #0x000D => 1, # HT, LF, VT, FF, SP, CR
1623              }->{$self->{next_input_character}}) {
1624            ## Stay in the state
1625            !!!next-input-character;
1626            redo A;
1627          } elsif ($self->{next_input_character} == 0x003E) { # >
1628            $self->{state} = DATA_STATE;
1629            !!!next-input-character;
1630    
1631            !!!emit ($self->{current_token}); # DOCTYPE
1632    
1633            redo A;
1634          } elsif ($self->{next_input_character} == -1) {
1635            !!!parse-error (type => 'unclosed DOCTYPE');
1636    
1637            $self->{state} = DATA_STATE;
1638            ## reconsume
1639    
1640            delete $self->{current_token}->{correct};
1641            !!!emit ($self->{current_token}); # DOCTYPE
1642    
1643            redo A;
1644          } else {
1645            !!!parse-error (type => 'string after SYSTEM literal');
1646            $self->{state} = BOGUS_DOCTYPE_STATE;
1647            !!!next-input-character;
1648            redo A;
1649          }
1650        } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {
1651        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_input_character} == 0x003E) { # >
1652          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1653          !!!next-input-character;          !!!next-input-character;
1654    
1655            delete $self->{current_token}->{correct};
1656          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1657    
1658          redo A;          redo A;
1659        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1660          !!!parse-error;          !!!parse-error (type => 'unclosed DOCTYPE');
1661          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1662          ## reconsume          ## reconsume
1663    
1664            delete $self->{current_token}->{correct};
1665          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1666    
1667          redo A;          redo A;
1668        } else {        } else {
# Line 1439  sub _get_next_token ($) { Line 1678  sub _get_next_token ($) {
1678    die "$0: _get_next_token: unexpected case";    die "$0: _get_next_token: unexpected case";
1679  } # _get_next_token  } # _get_next_token
1680    
1681  sub _tokenize_attempt_to_consume_an_entity ($) {  sub _tokenize_attempt_to_consume_an_entity ($$) {
1682    my $self = shift;    my ($self, $in_attr) = @_;
1683      
1684    if ($self->{next_input_character} == 0x0023) { # #    if ({
1685           0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,
1686           0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR
1687          }->{$self->{next_input_character}}) {
1688        ## Don't consume
1689        ## No error
1690        return undef;
1691      } elsif ($self->{next_input_character} == 0x0023) { # #
1692      !!!next-input-character;      !!!next-input-character;
     my $num;  
1693      if ($self->{next_input_character} == 0x0078 or # x      if ($self->{next_input_character} == 0x0078 or # x
1694          $self->{next_input_character} == 0x0058) { # X          $self->{next_input_character} == 0x0058) { # X
1695          my $code;
1696        X: {        X: {
1697          my $x_char = $self->{next_input_character};          my $x_char = $self->{next_input_character};
1698          !!!next-input-character;          !!!next-input-character;
1699          if (0x0030 <= $self->{next_input_character} and          if (0x0030 <= $self->{next_input_character} and
1700              $self->{next_input_character} <= 0x0039) { # 0..9              $self->{next_input_character} <= 0x0039) { # 0..9
1701            $num ||= 0;            $code ||= 0;
1702            $num *= 0x10;            $code *= 0x10;
1703            $num += $self->{next_input_character} - 0x0030;            $code += $self->{next_input_character} - 0x0030;
1704            redo X;            redo X;
1705          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_input_character} and
1706                   $self->{next_input_character} <= 0x0066) { # a..f                   $self->{next_input_character} <= 0x0066) { # a..f
1707            ## ISSUE: the spec says U+0078, which is apparently incorrect            $code ||= 0;
1708            $num ||= 0;            $code *= 0x10;
1709            $num *= 0x10;            $code += $self->{next_input_character} - 0x0060 + 9;
           $num += $self->{next_input_character} - 0x0060 + 9;  
1710            redo X;            redo X;
1711          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_input_character} and
1712                   $self->{next_input_character} <= 0x0046) { # A..F                   $self->{next_input_character} <= 0x0046) { # A..F
1713            ## ISSUE: the spec says U+0058, which is apparently incorrect            $code ||= 0;
1714            $num ||= 0;            $code *= 0x10;
1715            $num *= 0x10;            $code += $self->{next_input_character} - 0x0040 + 9;
           $num += $self->{next_input_character} - 0x0040 + 9;  
1716            redo X;            redo X;
1717          } elsif (not defined $num) { # no hexadecimal digit          } elsif (not defined $code) { # no hexadecimal digit
1718            !!!parse-error;            !!!parse-error (type => 'bare hcro');
1719              !!!back-next-input-character ($x_char, $self->{next_input_character});
1720            $self->{next_input_character} = 0x0023; # #            $self->{next_input_character} = 0x0023; # #
           !!!back-next-input-character ($x_char);  
1721            return undef;            return undef;
1722          } elsif ($self->{next_input_character} == 0x003B) { # ;          } elsif ($self->{next_input_character} == 0x003B) { # ;
1723            !!!next-input-character;            !!!next-input-character;
1724          } else {          } else {
1725            !!!parse-error;            !!!parse-error (type => 'no refc');
1726          }          }
1727    
1728          ## TODO: check the definition for |a valid Unicode character|.          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
1729          if ($num > 1114111 or $num == 0) {            !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);
1730            $num = 0xFFFD; # REPLACEMENT CHARACTER            $code = 0xFFFD;
1731            ## ISSUE: Why this is not an error?          } elsif ($code > 0x10FFFF) {
1732              !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);
1733              $code = 0xFFFD;
1734            } elsif ($code == 0x000D) {
1735              !!!parse-error (type => 'CR character reference');
1736              $code = 0x000A;
1737            } elsif (0x80 <= $code and $code <= 0x9F) {
1738              !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);
1739              $code = $c1_entity_char->{$code};
1740          }          }
1741    
1742          return {type => 'character', data => chr $num};          return {type => CHARACTER_TOKEN, data => chr $code};
1743        } # X        } # X
1744      } elsif (0x0030 <= $self->{next_input_character} and      } elsif (0x0030 <= $self->{next_input_character} and
1745               $self->{next_input_character} <= 0x0039) { # 0..9               $self->{next_input_character} <= 0x0039) { # 0..9
# Line 1505  sub _tokenize_attempt_to_consume_an_enti Line 1757  sub _tokenize_attempt_to_consume_an_enti
1757        if ($self->{next_input_character} == 0x003B) { # ;        if ($self->{next_input_character} == 0x003B) { # ;
1758          !!!next-input-character;          !!!next-input-character;
1759        } else {        } else {
1760          !!!parse-error;          !!!parse-error (type => 'no refc');
1761        }        }
1762    
1763        ## TODO: check the definition for |a valid Unicode character|.        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
1764        if ($code > 1114111 or $code == 0) {          !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);
1765          $code = 0xFFFD; # REPLACEMENT CHARACTER          $code = 0xFFFD;
1766          ## ISSUE: Why this is not an error?        } elsif ($code > 0x10FFFF) {
1767            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);
1768            $code = 0xFFFD;
1769          } elsif ($code == 0x000D) {
1770            !!!parse-error (type => 'CR character reference');
1771            $code = 0x000A;
1772          } elsif (0x80 <= $code and $code <= 0x9F) {
1773            !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);
1774            $code = $c1_entity_char->{$code};
1775        }        }
1776                
1777        return {type => 'character', data => chr $code};        return {type => CHARACTER_TOKEN, data => chr $code};
1778      } else {      } else {
1779        !!!parse-error;        !!!parse-error (type => 'bare nero');
1780        !!!back-next-input-character ($self->{next_input_character});        !!!back-next-input-character ($self->{next_input_character});
1781        $self->{next_input_character} = 0x0023; # #        $self->{next_input_character} = 0x0023; # #
1782        return undef;        return undef;
# Line 1529  sub _tokenize_attempt_to_consume_an_enti Line 1789  sub _tokenize_attempt_to_consume_an_enti
1789      !!!next-input-character;      !!!next-input-character;
1790    
1791      my $value = $entity_name;      my $value = $entity_name;
1792      my $match;      my $match = 0;
1793        require Whatpm::_NamedEntityList;
1794        our $EntityChar;
1795    
1796      while (length $entity_name < 10 and      while (length $entity_name < 10 and
1797             ## NOTE: Some number greater than the maximum length of entity name             ## NOTE: Some number greater than the maximum length of entity name
1798             ((0x0041 <= $self->{next_input_character} and             ((0x0041 <= $self->{next_input_character} and # a
1799               $self->{next_input_character} <= 0x005A) or               $self->{next_input_character} <= 0x005A) or # x
1800              (0x0061 <= $self->{next_input_character} and              (0x0061 <= $self->{next_input_character} and # a
1801               $self->{next_input_character} <= 0x007A) or               $self->{next_input_character} <= 0x007A) or # z
1802              (0x0030 <= $self->{next_input_character} and              (0x0030 <= $self->{next_input_character} and # 0
1803               $self->{next_input_character} <= 0x0039))) {               $self->{next_input_character} <= 0x0039) or # 9
1804                $self->{next_input_character} == 0x003B)) { # ;
1805        $entity_name .= chr $self->{next_input_character};        $entity_name .= chr $self->{next_input_character};
1806        if (defined $entity_char->{$entity_name}) {        if (defined $EntityChar->{$entity_name}) {
1807          $value = $entity_char->{$entity_name};          if ($self->{next_input_character} == 0x003B) { # ;
1808          $match = 1;            $value = $EntityChar->{$entity_name};
1809              $match = 1;
1810              !!!next-input-character;
1811              last;
1812            } else {
1813              $value = $EntityChar->{$entity_name};
1814              $match = -1;
1815              !!!next-input-character;
1816            }
1817        } else {        } else {
1818          $value .= chr $self->{next_input_character};          $value .= chr $self->{next_input_character};
1819            $match *= 2;
1820            !!!next-input-character;
1821        }        }
       !!!next-input-character;  
1822      }      }
1823            
1824      if ($match) {      if ($match > 0) {
1825        if ($self->{next_input_character} == 0x003B) { # ;        return {type => CHARACTER_TOKEN, data => $value};
1826          !!!next-input-character;      } elsif ($match < 0) {
1827          !!!parse-error (type => 'no refc');
1828          if ($in_attr and $match < -1) {
1829            return {type => CHARACTER_TOKEN, data => '&'.$entity_name};
1830        } else {        } else {
1831          !!!parse-error;          return {type => CHARACTER_TOKEN, data => $value};
1832        }        }
   
       return {type => 'character', data => $value};  
1833      } else {      } else {
1834        !!!parse-error;        !!!parse-error (type => 'bare ero');
1835        ## NOTE: No characters are consumed in the spec.        ## NOTE: No characters are consumed in the spec.
1836        !!!back-token ({type => 'character', data => $value});        return {type => CHARACTER_TOKEN, data => '&'.$value};
       return undef;  
1837      }      }
1838    } else {    } else {
1839      ## no characters are consumed      ## no characters are consumed
1840      !!!parse-error;      !!!parse-error (type => 'bare ero');
1841      return undef;      return undef;
1842    }    }
1843  } # _tokenize_attempt_to_consume_an_entity  } # _tokenize_attempt_to_consume_an_entity
# Line 1576  sub _initialize_tree_constructor ($) { Line 1848  sub _initialize_tree_constructor ($) {
1848    $self->{document}->strict_error_checking (0);    $self->{document}->strict_error_checking (0);
1849    ## TODO: Turn mutation events off # MUST    ## TODO: Turn mutation events off # MUST
1850    ## TODO: Turn loose Document option (manakai extension) on    ## TODO: Turn loose Document option (manakai extension) on
1851    ## TODO: Mark the Document as an HTML document # MUST    $self->{document}->manakai_is_html (1); # MUST
1852  } # _initialize_tree_constructor  } # _initialize_tree_constructor
1853    
1854  sub _terminate_tree_constructor ($) {  sub _terminate_tree_constructor ($) {
# Line 1587  sub _terminate_tree_constructor ($) { Line 1859  sub _terminate_tree_constructor ($) {
1859    
1860  ## 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?
1861    
1862    { # tree construction stage
1863      my $token;
1864    
1865  sub _construct_tree ($) {  sub _construct_tree ($) {
1866    my ($self) = @_;    my ($self) = @_;
1867    
# Line 1598  sub _construct_tree ($) { Line 1873  sub _construct_tree ($) {
1873    ## characters and insert one Text node whose data is concatenation    ## characters and insert one Text node whose data is concatenation
1874    ## of all those characters. # MUST    ## of all those characters. # MUST
1875        
   my $token;  
1876    !!!next-token;    !!!next-token;
1877    
1878    my $phase = 'initial'; # MUST    $self->{insertion_mode} = BEFORE_HEAD_IM;
1879      undef $self->{form_element};
1880      undef $self->{head_element};
1881      $self->{open_elements} = [];
1882      undef $self->{inner_html_node};
1883    
1884      $self->_tree_construction_initial; # MUST
1885      $self->_tree_construction_root_element;
1886      $self->_tree_construction_main;
1887    } # _construct_tree
1888    
1889    sub _tree_construction_initial ($) {
1890      my $self = shift;
1891      INITIAL: {
1892        if ($token->{type} == DOCTYPE_TOKEN) {
1893          ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"
1894          ## error, switch to a conformance checking mode for another
1895          ## language.
1896          my $doctype_name = $token->{name};
1897          $doctype_name = '' unless defined $doctype_name;
1898          $doctype_name =~ tr/a-z/A-Z/;
1899          if (not defined $token->{name} or # <!DOCTYPE>
1900              defined $token->{public_identifier} or
1901              defined $token->{system_identifier}) {
1902            !!!parse-error (type => 'not HTML5');
1903          } elsif ($doctype_name ne 'HTML') {
1904            ## ISSUE: ASCII case-insensitive? (in fact it does not matter)
1905            !!!parse-error (type => 'not HTML5');
1906          }
1907          
1908          my $doctype = $self->{document}->create_document_type_definition
1909            ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
1910          $doctype->public_id ($token->{public_identifier})
1911              if defined $token->{public_identifier};
1912          $doctype->system_id ($token->{system_identifier})
1913              if defined $token->{system_identifier};
1914          ## NOTE: Other DocumentType attributes are null or empty lists.
1915          ## ISSUE: internalSubset = null??
1916          $self->{document}->append_child ($doctype);
1917          
1918          if (not $token->{correct} or $doctype_name ne 'HTML') {
1919            $self->{document}->manakai_compat_mode ('quirks');
1920          } elsif (defined $token->{public_identifier}) {
1921            my $pubid = $token->{public_identifier};
1922            $pubid =~ tr/a-z/A-z/;
1923            if ({
1924              "+//SILMARIL//DTD HTML PRO V0R11 19970101//EN" => 1,
1925              "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,
1926              "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,
1927              "-//IETF//DTD HTML 2.0 LEVEL 1//EN" => 1,
1928              "-//IETF//DTD HTML 2.0 LEVEL 2//EN" => 1,
1929              "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//EN" => 1,
1930              "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//EN" => 1,
1931              "-//IETF//DTD HTML 2.0 STRICT//EN" => 1,
1932              "-//IETF//DTD HTML 2.0//EN" => 1,
1933              "-//IETF//DTD HTML 2.1E//EN" => 1,
1934              "-//IETF//DTD HTML 3.0//EN" => 1,
1935              "-//IETF//DTD HTML 3.0//EN//" => 1,
1936              "-//IETF//DTD HTML 3.2 FINAL//EN" => 1,
1937              "-//IETF//DTD HTML 3.2//EN" => 1,
1938              "-//IETF//DTD HTML 3//EN" => 1,
1939              "-//IETF//DTD HTML LEVEL 0//EN" => 1,
1940              "-//IETF//DTD HTML LEVEL 0//EN//2.0" => 1,
1941              "-//IETF//DTD HTML LEVEL 1//EN" => 1,
1942              "-//IETF//DTD HTML LEVEL 1//EN//2.0" => 1,
1943              "-//IETF//DTD HTML LEVEL 2//EN" => 1,
1944              "-//IETF//DTD HTML LEVEL 2//EN//2.0" => 1,
1945              "-//IETF//DTD HTML LEVEL 3//EN" => 1,
1946              "-//IETF//DTD HTML LEVEL 3//EN//3.0" => 1,
1947              "-//IETF//DTD HTML STRICT LEVEL 0//EN" => 1,
1948              "-//IETF//DTD HTML STRICT LEVEL 0//EN//2.0" => 1,
1949              "-//IETF//DTD HTML STRICT LEVEL 1//EN" => 1,
1950              "-//IETF//DTD HTML STRICT LEVEL 1//EN//2.0" => 1,
1951              "-//IETF//DTD HTML STRICT LEVEL 2//EN" => 1,
1952              "-//IETF//DTD HTML STRICT LEVEL 2//EN//2.0" => 1,
1953              "-//IETF//DTD HTML STRICT LEVEL 3//EN" => 1,
1954              "-//IETF//DTD HTML STRICT LEVEL 3//EN//3.0" => 1,
1955              "-//IETF//DTD HTML STRICT//EN" => 1,
1956              "-//IETF//DTD HTML STRICT//EN//2.0" => 1,
1957              "-//IETF//DTD HTML STRICT//EN//3.0" => 1,
1958              "-//IETF//DTD HTML//EN" => 1,
1959              "-//IETF//DTD HTML//EN//2.0" => 1,
1960              "-//IETF//DTD HTML//EN//3.0" => 1,
1961              "-//METRIUS//DTD METRIUS PRESENTATIONAL//EN" => 1,
1962              "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//EN" => 1,
1963              "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//EN" => 1,
1964              "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//EN" => 1,
1965              "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//EN" => 1,
1966              "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//EN" => 1,
1967              "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//EN" => 1,
1968              "-//NETSCAPE COMM. CORP.//DTD HTML//EN" => 1,
1969              "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,
1970              "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,
1971              "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,
1972              "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,
1973              "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,
1974              "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,
1975              "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//EN" => 1,
1976              "-//W3C//DTD HTML 3 1995-03-24//EN" => 1,
1977              "-//W3C//DTD HTML 3.2 DRAFT//EN" => 1,
1978              "-//W3C//DTD HTML 3.2 FINAL//EN" => 1,
1979              "-//W3C//DTD HTML 3.2//EN" => 1,
1980              "-//W3C//DTD HTML 3.2S DRAFT//EN" => 1,
1981              "-//W3C//DTD HTML 4.0 FRAMESET//EN" => 1,
1982              "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN" => 1,
1983              "-//W3C//DTD HTML EXPERIMETNAL 19960712//EN" => 1,
1984              "-//W3C//DTD HTML EXPERIMENTAL 970421//EN" => 1,
1985              "-//W3C//DTD W3 HTML//EN" => 1,
1986              "-//W3O//DTD W3 HTML 3.0//EN" => 1,
1987              "-//W3O//DTD W3 HTML 3.0//EN//" => 1,
1988              "-//W3O//DTD W3 HTML STRICT 3.0//EN//" => 1,
1989              "-//WEBTECHS//DTD MOZILLA HTML 2.0//EN" => 1,
1990              "-//WEBTECHS//DTD MOZILLA HTML//EN" => 1,
1991              "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" => 1,
1992              "HTML" => 1,
1993            }->{$pubid}) {
1994              $self->{document}->manakai_compat_mode ('quirks');
1995            } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or
1996                     $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {
1997              if (defined $token->{system_identifier}) {
1998                $self->{document}->manakai_compat_mode ('quirks');
1999              } else {
2000                $self->{document}->manakai_compat_mode ('limited quirks');
2001              }
2002            } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 Frameset//EN" or
2003                     $pubid eq "-//W3C//DTD XHTML 1.0 Transitional//EN") {
2004              $self->{document}->manakai_compat_mode ('limited quirks');
2005            }
2006          }
2007          if (defined $token->{system_identifier}) {
2008            my $sysid = $token->{system_identifier};
2009            $sysid =~ tr/A-Z/a-z/;
2010            if ($sysid eq "http://www.ibm.com/data/dtd/v11/ibmxhtml1-transitional.dtd") {
2011              $self->{document}->manakai_compat_mode ('quirks');
2012            }
2013          }
2014          
2015          ## Go to the root element phase.
2016          !!!next-token;
2017          return;
2018        } elsif ({
2019                  START_TAG_TOKEN, 1,
2020                  END_TAG_TOKEN, 1,
2021                  END_OF_FILE_TOKEN, 1,
2022                 }->{$token->{type}}) {
2023          !!!parse-error (type => 'no DOCTYPE');
2024          $self->{document}->manakai_compat_mode ('quirks');
2025          ## Go to the root element phase
2026          ## reprocess
2027          return;
2028        } elsif ($token->{type} == CHARACTER_TOKEN) {
2029          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
2030            ## Ignore the token
2031    
2032            unless (length $token->{data}) {
2033              ## Stay in the phase
2034              !!!next-token;
2035              redo INITIAL;
2036            }
2037          }
2038    
2039          !!!parse-error (type => 'no DOCTYPE');
2040          $self->{document}->manakai_compat_mode ('quirks');
2041          ## Go to the root element phase
2042          ## reprocess
2043          return;
2044        } elsif ($token->{type} == COMMENT_TOKEN) {
2045          my $comment = $self->{document}->create_comment ($token->{data});
2046          $self->{document}->append_child ($comment);
2047          
2048          ## Stay in the phase.
2049          !!!next-token;
2050          redo INITIAL;
2051        } else {
2052          die "$0: $token->{type}: Unknown token type";
2053        }
2054      } # INITIAL
2055    } # _tree_construction_initial
2056    
2057    sub _tree_construction_root_element ($) {
2058      my $self = shift;
2059      
2060      B: {
2061          if ($token->{type} == DOCTYPE_TOKEN) {
2062            !!!parse-error (type => 'in html:#DOCTYPE');
2063            ## Ignore the token
2064            ## Stay in the phase
2065            !!!next-token;
2066            redo B;
2067          } elsif ($token->{type} == COMMENT_TOKEN) {
2068            my $comment = $self->{document}->create_comment ($token->{data});
2069            $self->{document}->append_child ($comment);
2070            ## Stay in the phase
2071            !!!next-token;
2072            redo B;
2073          } elsif ($token->{type} == CHARACTER_TOKEN) {
2074            if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
2075              ## Ignore the token.
2076    
2077              unless (length $token->{data}) {
2078                ## Stay in the phase
2079                !!!next-token;
2080                redo B;
2081              }
2082            }
2083            #
2084          } elsif ({
2085                    START_TAG_TOKEN, 1,
2086                    END_TAG_TOKEN, 1,
2087                    END_OF_FILE_TOKEN, 1,
2088                   }->{$token->{type}}) {
2089            ## ISSUE: There is an issue in the spec
2090            #
2091          } else {
2092            die "$0: $token->{type}: Unknown token type";
2093          }
2094          my $root_element; !!!create-element ($root_element, 'html');
2095          $self->{document}->append_child ($root_element);
2096          push @{$self->{open_elements}}, [$root_element, 'html'];
2097          ## reprocess
2098          #redo B;
2099          return; ## Go to the main phase.
2100      } # B
2101    } # _tree_construction_root_element
2102    
2103    sub _reset_insertion_mode ($) {
2104      my $self = shift;
2105    
2106        ## Step 1
2107        my $last;
2108        
2109        ## Step 2
2110        my $i = -1;
2111        my $node = $self->{open_elements}->[$i];
2112        
2113        ## Step 3
2114        S3: {
2115          ## ISSUE: Oops! "If node is the first node in the stack of open
2116          ## elements, then set last to true. If the context element of the
2117          ## HTML fragment parsing algorithm is neither a td element nor a
2118          ## th element, then set node to the context element. (fragment case)":
2119          ## The second "if" is in the scope of the first "if"!?
2120          if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
2121            $last = 1;
2122            if (defined $self->{inner_html_node}) {
2123              if ($self->{inner_html_node}->[1] eq 'td' or
2124                  $self->{inner_html_node}->[1] eq 'th') {
2125                #
2126              } else {
2127                $node = $self->{inner_html_node};
2128              }
2129            }
2130          }
2131        
2132          ## Step 4..13
2133          my $new_mode = {
2134                          select => IN_SELECT_IM,
2135                          td => IN_CELL_IM,
2136                          th => IN_CELL_IM,
2137                          tr => IN_ROW_IM,
2138                          tbody => IN_TABLE_BODY_IM,
2139                          thead => IN_TABLE_BODY_IM,
2140                          tfoot => IN_TABLE_BODY_IM,
2141                          caption => IN_CAPTION_IM,
2142                          colgroup => IN_COLUMN_GROUP_IM,
2143                          table => IN_TABLE_IM,
2144                          head => IN_BODY_IM, # not in head!
2145                          body => IN_BODY_IM,
2146                          frameset => IN_FRAMESET_IM,
2147                         }->{$node->[1]};
2148          $self->{insertion_mode} = $new_mode and return if defined $new_mode;
2149          
2150          ## Step 14
2151          if ($node->[1] eq 'html') {
2152            unless (defined $self->{head_element}) {
2153              $self->{insertion_mode} = BEFORE_HEAD_IM;
2154            } else {
2155              $self->{insertion_mode} = AFTER_HEAD_IM;
2156            }
2157            return;
2158          }
2159          
2160          ## Step 15
2161          $self->{insertion_mode} = IN_BODY_IM and return if $last;
2162          
2163          ## Step 16
2164          $i--;
2165          $node = $self->{open_elements}->[$i];
2166          
2167          ## Step 17
2168          redo S3;
2169        } # S3
2170    } # _reset_insertion_mode
2171    
2172    sub _tree_construction_main ($) {
2173      my $self = shift;
2174    
   my $open_elements = [];  
2175    my $active_formatting_elements = [];    my $active_formatting_elements = [];
   my $head_element;  
   my $form_element;  
   my $insertion_mode = 'before head';  
2176    
2177    my $reconstruct_active_formatting_elements = sub { # MUST    my $reconstruct_active_formatting_elements = sub { # MUST
2178      my $insert = shift;      my $insert = shift;
# Line 1621  sub _construct_tree ($) { Line 2186  sub _construct_tree ($) {
2186    
2187      ## Step 2      ## Step 2
2188      return if $entry->[0] eq '#marker';      return if $entry->[0] eq '#marker';
2189      for (@$open_elements) {      for (@{$self->{open_elements}}) {
2190        if ($entry->[0] eq $_->[0]) {        if ($entry->[0] eq $_->[0]) {
2191          return;          return;
2192        }        }
# Line 1640  sub _construct_tree ($) { Line 2205  sub _construct_tree ($) {
2205          #          #
2206        } else {        } else {
2207          my $in_open_elements;          my $in_open_elements;
2208          OE: for (@$open_elements) {          OE: for (@{$self->{open_elements}}) {
2209            if ($entry->[0] eq $_->[0]) {            if ($entry->[0] eq $_->[0]) {
2210              $in_open_elements = 1;              $in_open_elements = 1;
2211              last OE;              last OE;
# Line 1664  sub _construct_tree ($) { Line 2229  sub _construct_tree ($) {
2229            
2230        ## Step 9        ## Step 9
2231        $insert->($clone->[0]);        $insert->($clone->[0]);
2232        push @$open_elements, $clone;        push @{$self->{open_elements}}, $clone;
2233                
2234        ## Step 10        ## Step 10
2235        $active_formatting_elements->[$i] = $open_elements->[-1];        $active_formatting_elements->[$i] = $self->{open_elements}->[-1];
2236    
2237        ## Step 11        ## Step 11
2238        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {
# Line 1689  sub _construct_tree ($) { Line 2254  sub _construct_tree ($) {
2254      }      }
2255    }; # $clear_up_to_marker    }; # $clear_up_to_marker
2256    
2257    my $reset_insertion_mode = sub {    my $parse_rcdata = sub ($$) {
2258        my ($content_model_flag, $insert) = @_;
2259    
2260      ## Step 1      ## Step 1
2261      my $last;      my $start_tag_name = $token->{tag_name};
2262            my $el;
2263        !!!create-element ($el, $start_tag_name, $token->{attributes});
2264    
2265      ## Step 2      ## Step 2
2266      my $i = -1;      $insert->($el); # /context node/->append_child ($el)
2267      my $node = $open_elements->[$i];  
       
2268      ## Step 3      ## Step 3
2269      S3: {      $self->{content_model} = $content_model_flag; # CDATA or RCDATA
2270        $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  
2271    
2272    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';  
                 
2273      my $text = '';      my $text = '';
2274      !!!next-token;      !!!next-token;
2275      while ($token->{type} eq 'character') {      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
2276        $text .= $token->{data};        $text .= $token->{data};
2277        !!!next-token;        !!!next-token;
2278      } # stop if non-character token or tokenizer stops tokenising      }
2279    
2280        ## Step 5
2281      if (length $text) {      if (length $text) {
2282        $style_el->manakai_append_text ($text);        my $text = $self->{document}->create_text_node ($text);
2283          $el->append_child ($text);
2284      }      }
2285        
2286      $self->{content_model_flag} = 'PCDATA';      ## Step 6
2287                      $self->{content_model} = PCDATA_CONTENT_MODEL;
2288      if ($token->{type} eq 'end tag' and $token->{tag_name} eq 'style') {  
2289        ## Step 7
2290        if ($token->{type} == END_TAG_TOKEN and $token->{tag_name} eq $start_tag_name) {
2291        ## Ignore the token        ## Ignore the token
2292        } elsif ($content_model_flag == CDATA_CONTENT_MODEL) {
2293          !!!parse-error (type => 'in CDATA:#'.$token->{type});
2294        } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
2295          !!!parse-error (type => 'in RCDATA:#'.$token->{type});
2296      } else {      } else {
2297        !!!parse-error;        die "$0: $content_model_flag in parse_rcdata";
       ## ISSUE: And ignore?  
2298      }      }
2299      !!!next-token;      !!!next-token;
2300    }; # $style_start_tag    }; # $parse_rcdata
2301    
2302    my $script_start_tag = sub {    my $script_start_tag = sub ($) {
2303        my $insert = $_[0];
2304      my $script_el;      my $script_el;
2305      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, 'script', $token->{attributes});
2306      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
2307    
2308      $self->{content_model_flag} = 'CDATA';      $self->{content_model} = CDATA_CONTENT_MODEL;
2309        delete $self->{escape}; # MUST
2310            
2311      my $text = '';      my $text = '';
2312      !!!next-token;      !!!next-token;
2313      while ($token->{type} eq 'character') {      while ($token->{type} == CHARACTER_TOKEN) {
2314        $text .= $token->{data};        $text .= $token->{data};
2315        !!!next-token;        !!!next-token;
2316      } # stop if non-character token or tokenizer stops tokenising      } # stop if non-character token or tokenizer stops tokenising
# Line 1788  sub _construct_tree ($) { Line 2318  sub _construct_tree ($) {
2318        $script_el->manakai_append_text ($text);        $script_el->manakai_append_text ($text);
2319      }      }
2320                                
2321      $self->{content_model_flag} = 'PCDATA';      $self->{content_model} = PCDATA_CONTENT_MODEL;
2322    
2323      if ($token->{type} eq 'end tag' and      if ($token->{type} == END_TAG_TOKEN and
2324          $token->{tag_name} eq 'script') {          $token->{tag_name} eq 'script') {
2325        ## Ignore the token        ## Ignore the token
2326      } else {      } else {
2327        !!!parse-error;        !!!parse-error (type => 'in CDATA:#'.$token->{type});
2328        ## ISSUE: And ignore?        ## ISSUE: And ignore?
2329        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
2330      }      }
2331            
2332      ## TODO: inner_html mode then mark as "already executed" and skip      if (defined $self->{inner_html_node}) {
2333      if (1) {        ## TODO: mark as "already executed"
2334        } else {
2335        ## TODO: $old_insertion_point = current insertion point        ## TODO: $old_insertion_point = current insertion point
2336        ## TODO: insertion point = just before the next input character        ## TODO: insertion point = just before the next input character
2337          
2338        (($insertion_mode eq 'in head' and defined $head_element)        $insert->($script_el);
        ? $head_element : $open_elements->[-1]->[0])->append_child ($script_el);  
2339                
2340        ## TODO: insertion point = $old_insertion_point (might be "undefined")        ## TODO: insertion point = $old_insertion_point (might be "undefined")
2341                
# Line 1832  sub _construct_tree ($) { Line 2362  sub _construct_tree ($) {
2362          }          }
2363        } # AFE        } # AFE
2364        unless (defined $formatting_element) {        unless (defined $formatting_element) {
2365          !!!parse-error;          !!!parse-error (type => 'unmatched end tag:'.$tag_name);
2366          ## Ignore the token          ## Ignore the token
2367          !!!next-token;          !!!next-token;
2368          return;          return;
# Line 1840  sub _construct_tree ($) { Line 2370  sub _construct_tree ($) {
2370        ## has an element in scope        ## has an element in scope
2371        my $in_scope = 1;        my $in_scope = 1;
2372        my $formatting_element_i_in_open;          my $formatting_element_i_in_open;  
2373        INSCOPE: for (reverse 0..$#$open_elements) {        INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
2374          my $node = $open_elements->[$_];          my $node = $self->{open_elements}->[$_];
2375          if ($node->[0] eq $formatting_element->[0]) {          if ($node->[0] eq $formatting_element->[0]) {
2376            if ($in_scope) {            if ($in_scope) {
2377              $formatting_element_i_in_open = $_;              $formatting_element_i_in_open = $_;
2378              last INSCOPE;              last INSCOPE;
2379            } else { # in open elements but not in scope            } else { # in open elements but not in scope
2380              !!!parse-error;              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
2381              ## Ignore the token              ## Ignore the token
2382              !!!next-token;              !!!next-token;
2383              return;              return;
# Line 1860  sub _construct_tree ($) { Line 2390  sub _construct_tree ($) {
2390          }          }
2391        } # INSCOPE        } # INSCOPE
2392        unless (defined $formatting_element_i_in_open) {        unless (defined $formatting_element_i_in_open) {
2393          !!!parse-error;          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
2394          pop @$active_formatting_elements; # $formatting_element          pop @$active_formatting_elements; # $formatting_element
2395          !!!next-token; ## TODO: ok?          !!!next-token; ## TODO: ok?
2396          return;          return;
2397        }        }
2398        if (not $open_elements->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
2399          !!!parse-error;          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
2400        }        }
2401                
2402        ## Step 2        ## Step 2
2403        my $furthest_block;        my $furthest_block;
2404        my $furthest_block_i_in_open;        my $furthest_block_i_in_open;
2405        OE: for (reverse 0..$#$open_elements) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
2406          my $node = $open_elements->[$_];          my $node = $self->{open_elements}->[$_];
2407          if (not $formatting_category->{$node->[1]} and          if (not $formatting_category->{$node->[1]} and
2408              #not $phrasing_category->{$node->[1]} and              #not $phrasing_category->{$node->[1]} and
2409              ($special_category->{$node->[1]} or              ($special_category->{$node->[1]} or
# Line 1887  sub _construct_tree ($) { Line 2417  sub _construct_tree ($) {
2417                
2418        ## Step 3        ## Step 3
2419        unless (defined $furthest_block) { # MUST        unless (defined $furthest_block) { # MUST
2420          splice @$open_elements, $formatting_element_i_in_open;          splice @{$self->{open_elements}}, $formatting_element_i_in_open;
2421          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;
2422          !!!next-token;          !!!next-token;
2423          return;          return;
2424        }        }
2425                
2426        ## Step 4        ## Step 4
2427        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];
2428                
2429        ## Step 5        ## Step 5
2430        my $furthest_block_parent = $furthest_block->[0]->parent_node;        my $furthest_block_parent = $furthest_block->[0]->parent_node;
# Line 1914  sub _construct_tree ($) { Line 2444  sub _construct_tree ($) {
2444        S7: {        S7: {
2445          ## Step 1          ## Step 1
2446          $node_i_in_open--;          $node_i_in_open--;
2447          $node = $open_elements->[$node_i_in_open];          $node = $self->{open_elements}->[$node_i_in_open];
2448                    
2449          ## Step 2          ## Step 2
2450          my $node_i_in_active;          my $node_i_in_active;
# Line 1925  sub _construct_tree ($) { Line 2455  sub _construct_tree ($) {
2455                last S7S2;                last S7S2;
2456              }              }
2457            }            }
2458            splice @$open_elements, $node_i_in_open, 1;            splice @{$self->{open_elements}}, $node_i_in_open, 1;
2459            redo S7;            redo S7;
2460          } # S7S2          } # S7S2
2461                    
# Line 1941  sub _construct_tree ($) { Line 2471  sub _construct_tree ($) {
2471          if ($node->[0]->has_child_nodes ()) {          if ($node->[0]->has_child_nodes ()) {
2472            my $clone = [$node->[0]->clone_node (0), $node->[1]];            my $clone = [$node->[0]->clone_node (0), $node->[1]];
2473            $active_formatting_elements->[$node_i_in_active] = $clone;            $active_formatting_elements->[$node_i_in_active] = $clone;
2474            $open_elements->[$node_i_in_open] = $clone;            $self->{open_elements}->[$node_i_in_open] = $clone;
2475            $node = $clone;            $node = $clone;
2476          }          }
2477                    
# Line 1983  sub _construct_tree ($) { Line 2513  sub _construct_tree ($) {
2513                
2514        ## Step 13        ## Step 13
2515        undef $i;        undef $i;
2516        OE: for (reverse 0..$#$open_elements) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
2517          if ($open_elements->[$_]->[0] eq $formatting_element->[0]) {          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {
2518            splice @$open_elements, $_, 1;            splice @{$self->{open_elements}}, $_, 1;
2519            $i-- and last OE if defined $i;            $i-- and last OE if defined $i;
2520          } elsif ($open_elements->[$_]->[0] eq $furthest_block->[0]) {          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {
2521            $i = $_;            $i = $_;
2522          }          }
2523        } # OE        } # OE
2524        splice @$open_elements, $i + 1, 1, $clone;        splice @{$self->{open_elements}}, $i + 1, 1, $clone;
2525                
2526        ## Step 14        ## Step 14
2527        redo FET;        redo FET;
# Line 1999  sub _construct_tree ($) { Line 2529  sub _construct_tree ($) {
2529    }; # $formatting_end_tag    }; # $formatting_end_tag
2530    
2531    my $insert_to_current = sub {    my $insert_to_current = sub {
2532      $open_elements->[-1]->[0]->append_child (shift);      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);
2533    }; # $insert_to_current    }; # $insert_to_current
2534    
2535    my $insert_to_foster = sub {    my $insert_to_foster = sub {
# Line 2007  sub _construct_tree ($) { Line 2537  sub _construct_tree ($) {
2537                         if ({                         if ({
2538                              table => 1, tbody => 1, tfoot => 1,                              table => 1, tbody => 1, tfoot => 1,
2539                              thead => 1, tr => 1,                              thead => 1, tr => 1,
2540                             }->{$open_elements->[-1]->[1]}) {                             }->{$self->{open_elements}->[-1]->[1]}) {
2541                           # MUST                           # MUST
2542                           my $foster_parent_element;                           my $foster_parent_element;
2543                           my $next_sibling;                           my $next_sibling;
2544                           OE: for (reverse 0..$#$open_elements) {                           OE: for (reverse 0..$#{$self->{open_elements}}) {
2545                             if ($open_elements->[$_]->[1] eq 'table') {                             if ($self->{open_elements}->[$_]->[1] eq 'table') {
2546                               my $parent = $open_elements->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
2547                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
2548                                 $foster_parent_element = $parent;                                 $foster_parent_element = $parent;
2549                                 $next_sibling = $open_elements->[$_]->[0];                                 $next_sibling = $self->{open_elements}->[$_]->[0];
2550                               } else {                               } else {
2551                                 $foster_parent_element                                 $foster_parent_element
2552                                   = $open_elements->[$_ - 1]->[0];                                   = $self->{open_elements}->[$_ - 1]->[0];
2553                               }                               }
2554                               last OE;                               last OE;
2555                             }                             }
2556                           } # OE                           } # OE
2557                           $foster_parent_element = $open_elements->[0]->[0]                           $foster_parent_element = $self->{open_elements}->[0]->[0]
2558                             unless defined $foster_parent_element;                             unless defined $foster_parent_element;
2559                           $foster_parent_element->insert_before                           $foster_parent_element->insert_before
2560                             ($child, $next_sibling);                             ($child, $next_sibling);
2561                         } else {                         } else {
2562                           $open_elements->[-1]->[0]->append_child ($child);                           $self->{open_elements}->[-1]->[0]->append_child ($child);
2563                         }                         }
2564    }; # $insert_to_foster    }; # $insert_to_foster
2565    
2566    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];  
2567    
2568          !!!next-token;    B: {
2569          return;      if ($token->{type} == DOCTYPE_TOKEN) {
2570        } elsif ({        !!!parse-error (type => 'DOCTYPE in the middle');
2571                  b => 1, big => 1, em => 1, font => 1, i => 1,        ## Ignore the token
2572                  nobr => 1, s => 1, small => 1, strile => 1,        ## Stay in the phase
2573                  strong => 1, tt => 1, u => 1,        !!!next-token;
2574                 }->{$token->{tag_name}}) {        redo B;
2575          $reconstruct_active_formatting_elements->($insert_to_current);      } elsif ($token->{type} == END_OF_FILE_TOKEN) {
2576                  if ($self->{insertion_mode} & AFTER_HTML_IMS) {
2577          !!!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', ''];  
   
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'marquee' or  
                $token->{tag_name} eq 'object') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, ['#marker', ''];  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'xmp') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
           
         $self->{content_model_flag} = 'CDATA';  
           
         !!!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.  
2578        } else {        } else {
2579          $reconstruct_active_formatting_elements->($insert_to_current);          ## Generate implied end tags
2580                    if ({
2581          !!!insert-element-t ($token->{tag_name}, $token->{attributes});               dd => 1, dt => 1, li => 1, p => 1, td => 1, th => 1, tr => 1,
2582                         tbody => 1, tfoot=> 1, thead => 1,
2583          !!!next-token;              }->{$self->{open_elements}->[-1]->[1]}) {
2584          return;            !!!back-token;
2585        }            $token = {type => END_TAG_TOKEN, tag_name => $self->{open_elements}->[-1]->[1]};
2586      } 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;  
2587          }          }
2588                    
2589          splice @$open_elements, $i if defined $i;          if (@{$self->{open_elements}} > 2 or
2590          undef $form_element if $token->{tag_name} eq 'form';              (@{$self->{open_elements}} == 2 and $self->{open_elements}->[1]->[1] ne 'body')) {
2591          $clear_up_to_marker->()            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
2592            if {          } elsif (defined $self->{inner_html_node} and
2593              button => 1, marquee => 1, object => 1,                   @{$self->{open_elements}} > 1 and
2594            }->{$token->{tag_name}};                   $self->{open_elements}->[1]->[1] ne 'body') {
2595          !!!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;  
2596          }          }
           
         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];  
2597    
2598          ## 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  
2599        }        }
     }  
   }; # $in_body  
2600    
2601    B: {        ## Stop parsing
2602      if ($phase eq 'initial') {        last B;
2603        if ($token->{type} eq 'DOCTYPE') {      } elsif ($token->{type} == START_TAG_TOKEN and
2604          if ($token->{error}) {               $token->{tag_name} eq 'html') {
2605            ## ISSUE: Spec currently left this case undefined.        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
2606            !!!parse-error ('bogus DOCTYPE');          ## Turn into the main phase
2607          }          !!!parse-error (type => 'after html:html');
2608          my $doctype = $self->{document}->create_document_type_definition          $self->{insertion_mode} = AFTER_BODY_IM;
2609            ($token->{name});        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
2610          $self->{document}->append_child ($doctype);          ## Turn into the main phase
2611          $phase = 'root element';          !!!parse-error (type => 'after html:html');
2612          !!!next-token;          $self->{insertion_mode} = AFTER_FRAMESET_IM;
2613          redo B;        }
2614        } elsif ({  
2615                  comment => 1,  ## ISSUE: "aa<html>" is not a parse error.
2616                  'start tag' => 1,  ## ISSUE: "<html>" in fragment is not a parse error.
2617                  'end tag' => 1,        unless ($token->{first_start_tag}) {
2618                  'end-of-file' => 1,          !!!parse-error (type => 'not first start tag');
2619                 }->{$token->{type}}) {        }
2620          ## ISSUE: Spec currently left this case undefined.        my $top_el = $self->{open_elements}->[0]->[0];
2621          !!!parse-error ('missing DOCTYPE');        for my $attr_name (keys %{$token->{attributes}}) {
2622          $phase = 'root element';          unless ($top_el->has_attribute_ns (undef, $attr_name)) {
2623          ## reprocess            $top_el->set_attribute_ns
2624          redo B;              (undef, [undef, $attr_name],
2625        } 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;  
           }  
2626          }          }
         ## ISSUE: Spec currently left this case undefined.  
         !!!parse-error ('missing DOCTYPE');  
         $phase = 'root element';  
         ## reprocess  
         redo B;  
       } else {  
         die "$0: $token->{type}: Unknown token";  
2627        }        }
2628      } elsif ($phase eq 'root element') {        !!!next-token;
2629        if ($token->{type} eq 'DOCTYPE') {        redo B;
2630          !!!parse-error;      } elsif ($token->{type} == COMMENT_TOKEN) {
2631          ## Ignore the token        my $comment = $self->{document}->create_comment ($token->{data});
2632          ## 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});  
2633          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
2634          ## Stay in the phase        } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {
2635          !!!next-token;          $self->{open_elements}->[0]->[0]->append_child ($comment);
2636          redo B;        } else {
2637        } elsif ($token->{type} eq 'character') {          $self->{open_elements}->[-1]->[0]->append_child ($comment);
2638          }
2639          !!!next-token;
2640          redo B;
2641        } elsif ($self->{insertion_mode} & HEAD_IMS) {
2642          if ($token->{type} == CHARACTER_TOKEN) {
2643          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
2644            $self->{document}->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
           ## ISSUE: DOM3 Core does not allow Document > Text  
2645            unless (length $token->{data}) {            unless (length $token->{data}) {
             ## Stay in the phase  
2646              !!!next-token;              !!!next-token;
2647              redo B;              redo B;
2648            }            }
2649          }          }
         #  
       } 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  
         }  
2650    
2651          ## Stop parsing          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2652          last B;            ## As if <head>
2653              !!!create-element ($self->{head_element}, 'head');
2654              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2655              push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2656    
2657              ## Reprocess in the "in head" insertion mode...
2658              pop @{$self->{open_elements}};
2659    
2660              ## Reprocess in the "after head" insertion mode...
2661            } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2662              ## As if </noscript>
2663              pop @{$self->{open_elements}};
2664              !!!parse-error (type => 'in noscript:#character');
2665              
2666              ## Reprocess in the "in head" insertion mode...
2667              ## As if </head>
2668              pop @{$self->{open_elements}};
2669    
2670              ## Reprocess in the "after head" insertion mode...
2671            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2672              pop @{$self->{open_elements}};
2673    
2674          ## ISSUE: There is an issue in the spec.            ## Reprocess in the "after head" insertion mode...
2675        } else {          }
2676          if ($insertion_mode eq 'before head') {  
2677            if ($token->{type} eq 'character') {              ## "after head" insertion mode
2678              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              ## As if <body>
2679                $open_elements->[-1]->[0]->manakai_append_text ($1);              !!!insert-element ('body');
2680                unless (length $token->{data}) {              $self->{insertion_mode} = IN_BODY_IM;
                 !!!next-token;  
                 redo B;  
               }  
             }  
             ## 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';  
2681              ## reprocess              ## reprocess
2682              redo B;              redo B;
2683            } 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';  
2684              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
2685                !!!next-token;                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2686              #} elsif ({                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});
2687              #          base => 1, link => 1, meta => 1,                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2688              #          script => 1, style => 1, title => 1,                  push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];
2689              #         }->{$token->{tag_name}}) {                  $self->{insertion_mode} = IN_HEAD_IM;
2690              #  ## reprocess                  !!!next-token;
2691              } else {                  redo B;
2692                ## reprocess                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
2693              }                  #
2694              redo B;                } else {
2695            } elsif ($token->{type} eq 'end tag') {                  !!!parse-error (type => 'in head:head'); # or in head noscript
2696              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}) {  
2697                  !!!next-token;                  !!!next-token;
2698                  redo B;                  redo B;
2699                }                }
2700                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2701                  ## As if <head>
2702                  !!!create-element ($self->{head_element}, 'head');
2703                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2704                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2705    
2706                  $self->{insertion_mode} = IN_HEAD_IM;
2707                  ## Reprocess in the "in head" insertion mode...
2708              }              }
               
             #  
           } 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';  
2709    
2710                my $text = '';              if ($token->{tag_name} eq 'base') {
2711                !!!next-token;                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2712                while ($token->{type} eq 'character') {                  ## As if </noscript>
2713                  $text .= $token->{data};                  pop @{$self->{open_elements}};
2714                  !!!next-token;                  !!!parse-error (type => 'in noscript:base');
               }  
               if (length $text) {  
                 $title_el->manakai_append_text ($text);  
               }  
                 
               $self->{content_model_flag} = 'PCDATA';  
2715                                
2716                if ($token->{type} eq 'end tag' and                  $self->{insertion_mode} = IN_HEAD_IM;
2717                    $token->{tag_name} eq 'title') {                  ## Reprocess in the "in head" insertion mode...
                 ## Ignore the token  
               } else {  
                 !!!parse-error;  
                 ## ISSUE: And ignore?  
2718                }                }
               !!!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);  
2719    
2720                !!!next-token;                ## NOTE: There is a "as if in head" code clone.
2721                redo B;                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2722              } elsif ($token->{tag_name} eq 'head') {                  !!!parse-error (type => 'after head:'.$token->{tag_name});
2723                !!!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;  
2724                }                }
2725                $insertion_mode = 'after head';                !!!insert-element ($token->{tag_name}, $token->{attributes});
2726                !!!next-token;                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
2727                redo B;                pop @{$self->{open_elements}}
2728              } elsif ($token->{tag_name} eq 'html') {                    if $self->{insertion_mode} == AFTER_HEAD_IM;
               #  
             } else {  
               !!!parse-error;  
               ## Ignore the token  
2729                !!!next-token;                !!!next-token;
2730                redo B;                redo B;
2731              }              } elsif ($token->{tag_name} eq 'link') {
2732            } else {                ## NOTE: There is a "as if in head" code clone.
2733              #                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2734            }                  !!!parse-error (type => 'after head:'.$token->{tag_name});
2735                    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;  
2736                }                }
2737              }                !!!insert-element ($token->{tag_name}, $token->{attributes});
2738                              pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
2739              #                pop @{$self->{open_elements}}
2740            } 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';  
2741                !!!next-token;                !!!next-token;
2742                redo B;                redo B;
2743              } elsif ({              } elsif ($token->{tag_name} eq 'meta') {
2744                        base => 1, link => 1, meta => 1,                ## NOTE: There is a "as if in head" code clone.
2745                        script=> 1, style => 1, title => 1,                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2746                       }->{$token->{tag_name}}) {                  !!!parse-error (type => 'after head:'.$token->{tag_name});
2747                !!!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;  
2748                }                }
2749              }                !!!insert-element ($token->{tag_name}, $token->{attributes});
2750                  pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
2751    
2752              ## As if in body, but insert into foster parent element                unless ($self->{confident}) {
2753              ## ISSUE: Spec says that "whenever a node would be inserted                  my $charset;
2754              ## into the current node" while characters might not be                  if ($token->{attributes}->{charset}) { ## TODO: And if supported
2755              ## 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;  
2756                  }                  }
2757                } # OE                  if ($token->{attributes}->{'http-equiv'}) {
2758                $foster_parent_element = $open_elements->[0]->[0] and                    ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.
2759                $prev_sibling = $foster_parent_element->last_child                    if ($token->{attributes}->{'http-equiv'}->{value}
2760                  unless defined $foster_parent_element;                        =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=
2761                if (defined $prev_sibling and                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
2762                    $prev_sibling->node_type == 3) {                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
2763                  $prev_sibling->manakai_append_text ($token->{data});                      $charset = defined $1 ? $1 : defined $2 ? $2 : $3;
2764                } else {                    } ## TODO: And if supported
2765                  $foster_parent_element->insert_before                  }
2766                    ($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;  
2767                }                }
2768    
2769                push @$active_formatting_elements, ['#marker', '']                ## TODO: Extracting |charset| from |meta|.
2770                  if $token->{tag_name} eq 'caption';                pop @{$self->{open_elements}}
2771                      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}};  
2772                !!!next-token;                !!!next-token;
2773                redo B;                redo B;
2774              } elsif ({              } elsif ($token->{tag_name} eq 'title') {
2775                        col => 1,                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2776                        td => 1, th => 1, tr => 1,                  ## As if </noscript>
2777                       }->{$token->{tag_name}}) {                  pop @{$self->{open_elements}};
2778                ## Clear back to table context                  !!!parse-error (type => 'in noscript:title');
2779                while ($open_elements->[-1]->[1] ne 'table' and                
2780                       $open_elements->[-1]->[1] ne 'html') {                  $self->{insertion_mode} = IN_HEAD_IM;
2781                  !!!parse-error;                  ## Reprocess in the "in head" insertion mode...
2782                  pop @$open_elements;                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
2783                    !!!parse-error (type => 'after head:'.$token->{tag_name});
2784                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2785                }                }
2786    
2787                !!!insert-element ($token->{tag_name} eq 'col' ? 'colgroup' : 'tbody');                ## NOTE: There is a "as if in head" code clone.
2788                $insertion_mode = $token->{tag_name} eq 'col'                my $parent = defined $self->{head_element} ? $self->{head_element}
2789                  ? 'in column group' : 'in table body';                    : $self->{open_elements}->[-1]->[0];
2790                ## reprocess                $parse_rcdata->(RCDATA_CONTENT_MODEL,
2791                                  sub { $parent->append_child ($_[0]) });
2792                  pop @{$self->{open_elements}}
2793                      if $self->{insertion_mode} == AFTER_HEAD_IM;
2794                redo B;                redo B;
2795              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'style') {
2796                ## NOTE: There are code clones for this "table in table"                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
2797                !!!parse-error;                ## insertion mode IN_HEAD_IM)
2798                  ## NOTE: There is a "as if in head" code clone.
2799                ## As if </table>                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2800                ## have a table element in table scope                  !!!parse-error (type => 'after head:'.$token->{tag_name});
2801                my $i;                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2802                INSCOPE: for (reverse 0..$#$open_elements) {                }
2803                  my $node = $open_elements->[$_];                $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);
2804                  if ($node->[1] eq 'table') {                pop @{$self->{open_elements}}
2805                    $i = $_;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
2806                    last INSCOPE;                redo B;
2807                  } elsif ({              } elsif ($token->{tag_name} eq 'noscript') {
2808                            table => 1, html => 1,                if ($self->{insertion_mode} == IN_HEAD_IM) {
2809                           }->{$node->[1]}) {                  ## NOTE: and scripting is disalbed
2810                    last INSCOPE;                  !!!insert-element ($token->{tag_name}, $token->{attributes});
2811                  }                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
2812                } # INSCOPE                  !!!next-token;
2813                unless (defined $i) {                  redo B;
2814                  !!!parse-error;                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2815                  ## Ignore tokens </table><table>                  !!!parse-error (type => 'in noscript:noscript');
2816                    ## Ignore the token
2817                  !!!next-token;                  !!!next-token;
2818                  redo B;                  redo B;
2819                  } else {
2820                    #
2821                }                }
2822                } elsif ($token->{tag_name} eq 'script') {
2823                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2824                    ## As if </noscript>
2825                    pop @{$self->{open_elements}};
2826                    !!!parse-error (type => 'in noscript:script');
2827                                
2828                ## generate implied end tags                  $self->{insertion_mode} = IN_HEAD_IM;
2829                if ({                  ## Reprocess in the "in head" insertion mode...
2830                     dd => 1, dt => 1, li => 1, p => 1,                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
2831                     td => 1, th => 1, tr => 1,                  !!!parse-error (type => 'after head:'.$token->{tag_name});
2832                    }->{$open_elements->[-1]->[1]}) {                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2833                  !!!back-token; # <table>                }
2834                  $token = {type => 'end tag', tag_name => 'table'};  
2835                  !!!back-token;                ## NOTE: There is a "as if in head" code clone.
2836                  $token = {type => 'end tag',                $script_start_tag->($insert_to_current);
2837                            tag_name => $open_elements->[-1]->[1]}; # MUST                pop @{$self->{open_elements}}
2838                  redo B;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
2839                  redo B;
2840                } elsif ($token->{tag_name} eq 'body' or
2841                         $token->{tag_name} eq 'frameset') {
2842                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2843                    ## As if </noscript>
2844                    pop @{$self->{open_elements}};
2845                    !!!parse-error (type => 'in noscript:'.$token->{tag_name});
2846                    
2847                    ## Reprocess in the "in head" insertion mode...
2848                    ## As if </head>
2849                    pop @{$self->{open_elements}};
2850                    
2851                    ## Reprocess in the "after head" insertion mode...
2852                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2853                    pop @{$self->{open_elements}};
2854                    
2855                    ## Reprocess in the "after head" insertion mode...
2856                }                }
2857    
2858                if ($open_elements->[-1]->[1] ne 'table') {                ## "after head" insertion mode
2859                  !!!parse-error;                !!!insert-element ($token->{tag_name}, $token->{attributes});
2860                  if ($token->{tag_name} eq 'body') {
2861                    $self->{insertion_mode} = IN_BODY_IM;
2862                  } elsif ($token->{tag_name} eq 'frameset') {
2863                    $self->{insertion_mode} = IN_FRAMESET_IM;
2864                  } else {
2865                    die "$0: tag name: $self->{tag_name}";
2866                }                }
2867                  !!!next-token;
               splice @$open_elements, $i;  
   
               $reset_insertion_mode->();                
   
               ## reprocess  
2868                redo B;                redo B;
2869              } else {              } else {
2870                #                #
2871              }              }
2872            } elsif ($token->{type} eq 'end tag') {  
2873              if ($token->{tag_name} eq 'table') {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2874                ## have a table element in table scope                ## As if </noscript>
2875                my $i;                pop @{$self->{open_elements}};
2876                INSCOPE: for (reverse 0..$#$open_elements) {                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});
2877                  my $node = $open_elements->[$_];                
2878                  if ($node->[1] eq $token->{tag_name}) {                ## Reprocess in the "in head" insertion mode...
2879                    $i = $_;                ## As if </head>
2880                    last INSCOPE;                pop @{$self->{open_elements}};
2881                  } elsif ({  
2882                            table => 1, html => 1,                ## Reprocess in the "after head" insertion mode...
2883                           }->{$node->[1]}) {              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2884                    last INSCOPE;                ## As if </head>
2885                  }                pop @{$self->{open_elements}};
2886                } # INSCOPE  
2887                unless (defined $i) {                ## Reprocess in the "after head" insertion mode...
2888                  !!!parse-error;              }
2889                  ## Ignore the token  
2890                ## "after head" insertion mode
2891                ## As if <body>
2892                !!!insert-element ('body');
2893                $self->{insertion_mode} = IN_BODY_IM;
2894                ## reprocess
2895                redo B;
2896              } elsif ($token->{type} == END_TAG_TOKEN) {
2897                if ($token->{tag_name} eq 'head') {
2898                  if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2899                    ## As if <head>
2900                    !!!create-element ($self->{head_element}, 'head');
2901                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2902                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2903    
2904                    ## Reprocess in the "in head" insertion mode...
2905                    pop @{$self->{open_elements}};
2906                    $self->{insertion_mode} = AFTER_HEAD_IM;
2907                    !!!next-token;
2908                    redo B;
2909                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2910                    ## As if </noscript>
2911                    pop @{$self->{open_elements}};
2912                    !!!parse-error (type => 'in noscript:script');
2913                    
2914                    ## Reprocess in the "in head" insertion mode...
2915                    pop @{$self->{open_elements}};
2916                    $self->{insertion_mode} = AFTER_HEAD_IM;
2917                    !!!next-token;
2918                    redo B;
2919                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2920                    pop @{$self->{open_elements}};
2921                    $self->{insertion_mode} = AFTER_HEAD_IM;
2922                  !!!next-token;                  !!!next-token;
2923                  redo B;                  redo B;
2924                  } else {
2925                    #
2926                }                }
2927                              } elsif ($token->{tag_name} eq 'noscript') {
2928                ## generate implied end tags                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2929                if ({                  pop @{$self->{open_elements}};
2930                     dd => 1, dt => 1, li => 1, p => 1,                  $self->{insertion_mode} = IN_HEAD_IM;
2931                     td => 1, th => 1, tr => 1,                  !!!next-token;
2932                    }->{$open_elements->[-1]->[1]}) {                  redo B;
2933                  !!!back-token;                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2934                  $token = {type => 'end tag',                  !!!parse-error (type => 'unmatched end tag:noscript');
2935                            tag_name => $open_elements->[-1]->[1]}; # MUST                  ## Ignore the token ## ISSUE: An issue in the spec.
2936                    !!!next-token;
2937                  redo B;                  redo B;
2938                  } else {
2939                    #
2940                }                }
2941                } elsif ({
2942                if ($open_elements->[-1]->[1] ne 'table') {                        body => 1, html => 1,
2943                  !!!parse-error;                       }->{$token->{tag_name}}) {
2944                  if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2945                    ## As if <head>
2946                    !!!create-element ($self->{head_element}, 'head');
2947                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2948                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2949    
2950                    $self->{insertion_mode} = IN_HEAD_IM;
2951                    ## Reprocess in the "in head" insertion mode...
2952                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2953                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
2954                    ## Ignore the token
2955                    !!!next-token;
2956                    redo B;
2957                }                }
2958                  
2959                  #
2960                } elsif ({
2961                          p => 1, br => 1,
2962                         }->{$token->{tag_name}}) {
2963                  if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2964                    ## As if <head>
2965                    !!!create-element ($self->{head_element}, 'head');
2966                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2967                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2968    
2969                splice @$open_elements, $i;                  $self->{insertion_mode} = IN_HEAD_IM;
2970                    ## Reprocess in the "in head" insertion mode...
2971                  }
2972    
2973                $reset_insertion_mode->();                #
2974                } else {
2975                  if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2976                    #
2977                  } else {
2978                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
2979                    ## Ignore the token
2980                    !!!next-token;
2981                    redo B;
2982                  }
2983                }
2984    
2985                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2986                  ## As if </noscript>
2987                  pop @{$self->{open_elements}};
2988                  !!!parse-error (type => 'in noscript:/'.$token->{tag_name});
2989                  
2990                  ## Reprocess in the "in head" insertion mode...
2991                  ## As if </head>
2992                  pop @{$self->{open_elements}};
2993    
2994                  ## Reprocess in the "after head" insertion mode...
2995                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2996                  ## As if </head>
2997                  pop @{$self->{open_elements}};
2998    
2999                  ## Reprocess in the "after head" insertion mode...
3000                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3001                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3002                  ## Ignore the token ## ISSUE: An issue in the spec.
3003                !!!next-token;                !!!next-token;
3004                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 {  
               #  
3005              }              }
3006    
3007                ## "after head" insertion mode
3008                ## As if <body>
3009                !!!insert-element ('body');
3010                $self->{insertion_mode} = IN_BODY_IM;
3011                ## reprocess
3012                redo B;
3013            } else {            } else {
3014              #              die "$0: $token->{type}: Unknown token type";
3015            }            }
3016    
3017            !!!parse-error;            ## ISSUE: An issue in the spec.
3018            $in_body->($insert_to_foster);      } elsif ($self->{insertion_mode} & BODY_IMS) {
3019            redo B;            if ($token->{type} == CHARACTER_TOKEN) {
3020          } 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".  
3021              $reconstruct_active_formatting_elements->($insert_to_current);              $reconstruct_active_formatting_elements->($insert_to_current);
3022                            
3023              $open_elements->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
3024    
3025              !!!next-token;              !!!next-token;
3026              redo B;              redo B;
3027            } 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') {  
3028              if ({              if ({
3029                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
3030                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,
3031                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
3032                !!!parse-error;                if ($self->{insertion_mode} == IN_CELL_IM) {
3033                    ## have an element in table scope
3034                    my $tn;
3035                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3036                      my $node = $self->{open_elements}->[$_];
3037                      if ($node->[1] eq 'td' or $node->[1] eq 'th') {
3038                        $tn = $node->[1];
3039                        last INSCOPE;
3040                      } elsif ({
3041                                table => 1, html => 1,
3042                               }->{$node->[1]}) {
3043                        last INSCOPE;
3044                      }
3045                    } # INSCOPE
3046                      unless (defined $tn) {
3047                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3048                        ## Ignore the token
3049                        !!!next-token;
3050                        redo B;
3051                      }
3052                    
3053                    ## Close the cell
3054                    !!!back-token; # <?>
3055                    $token = {type => END_TAG_TOKEN, tag_name => $tn};
3056                    redo B;
3057                  } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
3058                    !!!parse-error (type => 'not closed:caption');
3059                    
3060                    ## As if </caption>
3061                    ## have a table element in table scope
3062                    my $i;
3063                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3064                      my $node = $self->{open_elements}->[$_];
3065                      if ($node->[1] eq 'caption') {
3066                        $i = $_;
3067                        last INSCOPE;
3068                      } elsif ({
3069                                table => 1, html => 1,
3070                               }->{$node->[1]}) {
3071                        last INSCOPE;
3072                      }
3073                    } # INSCOPE
3074                      unless (defined $i) {
3075                        !!!parse-error (type => 'unmatched end tag:caption');
3076                        ## Ignore the token
3077                        !!!next-token;
3078                        redo B;
3079                      }
3080                    
3081                    ## generate implied end tags
3082                    if ({
3083                         dd => 1, dt => 1, li => 1, p => 1,
3084                         td => 1, th => 1, tr => 1,
3085                         tbody => 1, tfoot=> 1, thead => 1,
3086                        }->{$self->{open_elements}->[-1]->[1]}) {
3087                      !!!back-token; # <?>
3088                      $token = {type => END_TAG_TOKEN, tag_name => 'caption'};
3089                      !!!back-token;
3090                      $token = {type => END_TAG_TOKEN,
3091                                tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3092                      redo B;
3093                    }
3094    
3095                ## As if </caption>                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {
3096                ## 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;  
3097                  }                  }
3098                } # INSCOPE                  
3099                unless (defined $i) {                  splice @{$self->{open_elements}}, $i;
3100                  !!!parse-error;                  
3101                    $clear_up_to_marker->();
3102                    
3103                    $self->{insertion_mode} = IN_TABLE_IM;
3104                    
3105                    ## reprocess
3106                    redo B;
3107                  } else {
3108                    #
3109                  }
3110                } else {
3111                  #
3112                }
3113              } elsif ($token->{type} == END_TAG_TOKEN) {
3114                if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {
3115                  if ($self->{insertion_mode} == IN_CELL_IM) {
3116                    ## have an element in table scope
3117                    my $i;
3118                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3119                      my $node = $self->{open_elements}->[$_];
3120                      if ($node->[1] eq $token->{tag_name}) {
3121                        $i = $_;
3122                        last INSCOPE;
3123                      } elsif ({
3124                                table => 1, html => 1,
3125                               }->{$node->[1]}) {
3126                        last INSCOPE;
3127                      }
3128                    } # INSCOPE
3129                      unless (defined $i) {
3130                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3131                        ## Ignore the token
3132                        !!!next-token;
3133                        redo B;
3134                      }
3135                    
3136                    ## generate implied end tags
3137                    if ({
3138                         dd => 1, dt => 1, li => 1, p => 1,
3139                         td => ($token->{tag_name} eq 'th'),
3140                         th => ($token->{tag_name} eq 'td'),
3141                         tr => 1,
3142                         tbody => 1, tfoot=> 1, thead => 1,
3143                        }->{$self->{open_elements}->[-1]->[1]}) {
3144                      !!!back-token;
3145                      $token = {type => END_TAG_TOKEN,
3146                                tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3147                      redo B;
3148                    }
3149                    
3150                    if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
3151                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3152                    }
3153                    
3154                    splice @{$self->{open_elements}}, $i;
3155                    
3156                    $clear_up_to_marker->();
3157                    
3158                    $self->{insertion_mode} = IN_ROW_IM;
3159                    
3160                    !!!next-token;
3161                    redo B;
3162                  } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
3163                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3164                  ## Ignore the token                  ## Ignore the token
3165                  !!!next-token;                  !!!next-token;
3166                  redo B;                  redo B;
3167                  } else {
3168                    #
3169                }                }
3170                              } elsif ($token->{tag_name} eq 'caption') {
3171                ## generate implied end tags                if ($self->{insertion_mode} == IN_CAPTION_IM) {
3172                if ({                  ## have a table element in table scope
3173                     dd => 1, dt => 1, li => 1, p => 1,                  my $i;
3174                     td => 1, th => 1, tr => 1,                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3175                    }->{$open_elements->[-1]->[1]}) {                    my $node = $self->{open_elements}->[$_];
3176                  !!!back-token; # <?>                    if ($node->[1] eq $token->{tag_name}) {
3177                  $token = {type => 'end tag', tag_name => 'caption'};                      $i = $_;
3178                  !!!back-token;                      last INSCOPE;
3179                  $token = {type => 'end tag',                    } elsif ({
3180                            tag_name => $open_elements->[-1]->[1]}; # MUST                              table => 1, html => 1,
3181                               }->{$node->[1]}) {
3182                        last INSCOPE;
3183                      }
3184                    } # INSCOPE
3185                      unless (defined $i) {
3186                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3187                        ## Ignore the token
3188                        !!!next-token;
3189                        redo B;
3190                      }
3191                    
3192                    ## generate implied end tags
3193                    if ({
3194                         dd => 1, dt => 1, li => 1, p => 1,
3195                         td => 1, th => 1, tr => 1,
3196                         tbody => 1, tfoot=> 1, thead => 1,
3197                        }->{$self->{open_elements}->[-1]->[1]}) {
3198                      !!!back-token;
3199                      $token = {type => END_TAG_TOKEN,
3200                                tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3201                      redo B;
3202                    }
3203                    
3204                    if ($self->{open_elements}->[-1]->[1] ne 'caption') {
3205                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3206                    }
3207                    
3208                    splice @{$self->{open_elements}}, $i;
3209                    
3210                    $clear_up_to_marker->();
3211                    
3212                    $self->{insertion_mode} = IN_TABLE_IM;
3213                    
3214                    !!!next-token;
3215                  redo B;                  redo B;
3216                  } elsif ($self->{insertion_mode} == IN_CELL_IM) {
3217                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3218                    ## Ignore the token
3219                    !!!next-token;
3220                    redo B;
3221                  } else {
3222                    #
3223                }                }
3224                } elsif ({
3225                if ($open_elements->[-1]->[1] ne 'caption') {                        table => 1, tbody => 1, tfoot => 1,
3226                  !!!parse-error;                        thead => 1, tr => 1,
3227                }                       }->{$token->{tag_name}} and
3228                         $self->{insertion_mode} == IN_CELL_IM) {
3229                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  
3230                my $i;                my $i;
3231                INSCOPE: for (reverse 0..$#$open_elements) {                my $tn;
3232                  my $node = $open_elements->[$_];                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3233                    my $node = $self->{open_elements}->[$_];
3234                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq $token->{tag_name}) {
3235                    $i = $_;                    $i = $_;
3236                    last INSCOPE;                    last INSCOPE;
3237                    } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {
3238                      $tn = $node->[1];
3239                      ## NOTE: There is exactly one |td| or |th| element
3240                      ## in scope in the stack of open elements by definition.
3241                  } elsif ({                  } elsif ({
3242                            table => 1, html => 1,                            table => 1, html => 1,
3243                           }->{$node->[1]}) {                           }->{$node->[1]}) {
# Line 3412  sub _construct_tree ($) { Line 3245  sub _construct_tree ($) {
3245                  }                  }
3246                } # INSCOPE                } # INSCOPE
3247                unless (defined $i) {                unless (defined $i) {
3248                  !!!parse-error;                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3249                  ## Ignore the token                  ## Ignore the token
3250                  !!!next-token;                  !!!next-token;
3251                  redo B;                  redo B;
3252                }                }
                 
               ## 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;  
               }  
3253    
3254                if ($open_elements->[-1]->[1] ne 'caption') {                ## Close the cell
3255                  !!!parse-error;                !!!back-token; # </?>
3256                }                $token = {type => END_TAG_TOKEN, tag_name => $tn};
   
               splice @$open_elements, $i;  
   
               $clear_up_to_marker->();  
   
               $insertion_mode = 'in table';  
   
               !!!next-token;  
3257                redo B;                redo B;
3258              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table' and
3259                !!!parse-error;                       $self->{insertion_mode} == IN_CAPTION_IM) {
3260                  !!!parse-error (type => 'not closed:caption');
3261    
3262                ## As if </caption>                ## As if </caption>
3263                ## have a table element in table scope                ## have a table element in table scope
3264                my $i;                my $i;
3265                INSCOPE: for (reverse 0..$#$open_elements) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3266                  my $node = $open_elements->[$_];                  my $node = $self->{open_elements}->[$_];
3267                  if ($node->[1] eq 'caption') {                  if ($node->[1] eq 'caption') {
3268                    $i = $_;                    $i = $_;
3269                    last INSCOPE;                    last INSCOPE;
# Line 3459  sub _construct_tree ($) { Line 3274  sub _construct_tree ($) {
3274                  }                  }
3275                } # INSCOPE                } # INSCOPE
3276                unless (defined $i) {                unless (defined $i) {
3277                  !!!parse-error;                  !!!parse-error (type => 'unmatched end tag:caption');
3278                  ## Ignore the token                  ## Ignore the token
3279                  !!!next-token;                  !!!next-token;
3280                  redo B;                  redo B;
# Line 3469  sub _construct_tree ($) { Line 3284  sub _construct_tree ($) {
3284                if ({                if ({
3285                     dd => 1, dt => 1, li => 1, p => 1,                     dd => 1, dt => 1, li => 1, p => 1,
3286                     td => 1, th => 1, tr => 1,                     td => 1, th => 1, tr => 1,
3287                    }->{$open_elements->[-1]->[1]}) {                     tbody => 1, tfoot=> 1, thead => 1,
3288                      }->{$self->{open_elements}->[-1]->[1]}) {
3289                  !!!back-token; # </table>                  !!!back-token; # </table>
3290                  $token = {type => 'end tag', tag_name => 'caption'};                  $token = {type => END_TAG_TOKEN, tag_name => 'caption'};
3291                  !!!back-token;                  !!!back-token;
3292                  $token = {type => 'end tag',                  $token = {type => END_TAG_TOKEN,
3293                            tag_name => $open_elements->[-1]->[1]}; # MUST                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3294                  redo B;                  redo B;
3295                }                }
3296    
3297                if ($open_elements->[-1]->[1] ne 'caption') {                if ($self->{open_elements}->[-1]->[1] ne 'caption') {
3298                  !!!parse-error;                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3299                }                }
3300    
3301                splice @$open_elements, $i;                splice @{$self->{open_elements}}, $i;
3302    
3303                $clear_up_to_marker->();                $clear_up_to_marker->();
3304    
3305                $insertion_mode = 'in table';                $self->{insertion_mode} = IN_TABLE_IM;
3306    
3307                ## reprocess                ## reprocess
3308                redo B;                redo B;
3309              } elsif ({              } elsif ({
3310                        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,  
3311                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3312                !!!parse-error;                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
3313                ## 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;  
3314                  ## Ignore the token                  ## Ignore the token
3315                  !!!next-token;                  !!!next-token;
3316                  redo B;                  redo B;
3317                } else {                } else {
3318                  pop @$open_elements; # colgroup                  #
                 $insertion_mode = 'in table';  
                 !!!next-token;  
                 redo B;              
3319                }                }
3320              } elsif ($token->{tag_name} eq 'col') {              } elsif ({
3321                !!!parse-error;                        tbody => 1, tfoot => 1,
3322                          thead => 1, tr => 1,
3323                         }->{$token->{tag_name}} and
3324                         $self->{insertion_mode} == IN_CAPTION_IM) {
3325                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3326                ## Ignore the token                ## Ignore the token
3327                !!!next-token;                !!!next-token;
3328                redo B;                redo B;
3329              } else {              } else {
3330                #                #
3331              }              }
3332            } else {        } else {
3333              #          die "$0: $token->{type}: Unknown token type";
3334            }        }
3335    
3336            ## As if </colgroup>        $insert = $insert_to_current;
3337            if ($open_elements->[-1]->[1] eq 'html') {        #
3338              !!!parse-error;      } elsif ($self->{insertion_mode} & TABLE_IMS) {
3339              ## Ignore the token        if ($token->{type} == CHARACTER_TOKEN) {
             !!!next-token;  
             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.  
3340              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
3341                $open_elements->[-1]->[0]->manakai_append_text ($1);                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
3342                                
3343                unless (length $token->{data}) {                unless (length $token->{data}) {
3344                  !!!next-token;                  !!!next-token;
# Line 3581  sub _construct_tree ($) { Line 3346  sub _construct_tree ($) {
3346                }                }
3347              }              }
3348    
3349                !!!parse-error (type => 'in table:#character');
3350    
3351              ## As if in body, but insert into foster parent element              ## As if in body, but insert into foster parent element
3352              ## ISSUE: Spec says that "whenever a node would be inserted              ## ISSUE: Spec says that "whenever a node would be inserted
3353              ## into the current node" while characters might not be              ## into the current node" while characters might not be
3354              ## result in a new Text node.              ## result in a new Text node.
3355              $reconstruct_active_formatting_elements->($insert_to_foster);              $reconstruct_active_formatting_elements->($insert_to_foster);
3356                
3357              if ({              if ({
3358                   table => 1, tbody => 1, tfoot => 1,                   table => 1, tbody => 1, tfoot => 1,
3359                   thead => 1, tr => 1,                   thead => 1, tr => 1,
3360                  }->{$open_elements->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
3361                # MUST                # MUST
3362                my $foster_parent_element;                my $foster_parent_element;
3363                my $next_sibling;                my $next_sibling;
3364                my $prev_sibling;                my $prev_sibling;
3365                OE: for (reverse 0..$#$open_elements) {                OE: for (reverse 0..$#{$self->{open_elements}}) {
3366                  if ($open_elements->[$_]->[1] eq 'table') {                  if ($self->{open_elements}->[$_]->[1] eq 'table') {
3367                    my $parent = $open_elements->[$_]->[0]->parent_node;                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3368                    if (defined $parent and $parent->node_type == 1) {                    if (defined $parent and $parent->node_type == 1) {
3369                      $foster_parent_element = $parent;                      $foster_parent_element = $parent;
3370                      $next_sibling = $open_elements->[$_]->[0];                      $next_sibling = $self->{open_elements}->[$_]->[0];
3371                      $prev_sibling = $next_sibling->previous_sibling;                      $prev_sibling = $next_sibling->previous_sibling;
3372                    } else {                    } else {
3373                      $foster_parent_element = $open_elements->[$_ - 1]->[0];                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];
3374                      $prev_sibling = $foster_parent_element->last_child;                      $prev_sibling = $foster_parent_element->last_child;
3375                    }                    }
3376                    last OE;                    last OE;
3377                  }                  }
3378                } # OE                } # OE
3379                $foster_parent_element = $open_elements->[0]->[0] and                $foster_parent_element = $self->{open_elements}->[0]->[0] and
3380                $prev_sibling = $foster_parent_element->last_child                $prev_sibling = $foster_parent_element->last_child
3381                  unless defined $foster_parent_element;                  unless defined $foster_parent_element;
3382                if (defined $prev_sibling and                if (defined $prev_sibling and
# Line 3621  sub _construct_tree ($) { Line 3388  sub _construct_tree ($) {
3388                     $next_sibling);                     $next_sibling);
3389                }                }
3390              } else {              } else {
3391                $open_elements->[-1]->[0]->manakai_append_text ($token->{data});                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
3392              }              }
3393                            
3394              !!!next-token;              !!!next-token;
3395              redo B;              redo B;
3396            } 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') {  
3397              if ({              if ({
3398                   tr => 1,                   tr => ($self->{insertion_mode} != IN_ROW_IM),
3399                   th => 1, td => 1,                   th => 1, td => 1,
3400                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
3401                ## Clear back to table body context                if ($self->{insertion_mode} == IN_TABLE_IM) {
3402                while (not {                  ## Clear back to table context
3403                  tbody => 1, tfoot => 1, thead => 1, html => 1,                  while ($self->{open_elements}->[-1]->[1] ne 'table' and
3404                }->{$open_elements->[-1]->[1]}) {                         $self->{open_elements}->[-1]->[1] ne 'html') {
3405                  !!!parse-error;                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3406                  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;  
3407                  }                  }
3408                } # INSCOPE                  
3409                unless (defined $i) {                  !!!insert-element ('tbody');
3410                  !!!parse-error;                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
3411                  ## Ignore the token                  ## reprocess in the "in table body" insertion mode...
                 !!!next-token;  
                 redo B;  
3412                }                }
3413    
3414                ## Clear back to table body context                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
3415                while (not {                  unless ($token->{tag_name} eq 'tr') {
3416                  tbody => 1, tfoot => 1, thead => 1, html => 1,                    !!!parse-error (type => 'missing start tag:tr');
               }->{$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 ($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;  
3417                  }                  }
3418                } # INSCOPE                  
3419                unless (defined $i) {                  ## Clear back to table body context
3420                  !!!parse-error;                  while (not {
3421                  ## Ignore tokens </table><table>                    tbody => 1, tfoot => 1, thead => 1, html => 1,
3422                  !!!next-token;                  }->{$self->{open_elements}->[-1]->[1]}) {
3423                  redo B;                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3424                }                    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;  
3425                  }                  }
3426                } # INSCOPE                  
3427                unless (defined $i) {                  $self->{insertion_mode} = IN_ROW_IM;
3428                  !!!parse-error;                  if ($token->{tag_name} eq 'tr') {
3429                  ## Ignore the token                    !!!insert-element ($token->{tag_name}, $token->{attributes});
3430                  !!!next-token;                    !!!next-token;
3431                  redo B;                    redo B;
3432                }                  } else {
3433                      !!!insert-element ('tr');
3434                ## 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;  
3435                  }                  }
               } # 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;  
3436                }                }
             }  
3437    
             ## 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') {  
3438                ## Clear back to table row context                ## Clear back to table row context
3439                while (not {                while (not {
3440                  tr => 1, html => 1,                  tr => 1, html => 1,
3441                }->{$open_elements->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
3442                  !!!parse-error;                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3443                  pop @$open_elements;                  pop @{$self->{open_elements}};
3444                }                }
3445                                
3446                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
3447                $insertion_mode = 'in cell';                $self->{insertion_mode} = IN_CELL_IM;
3448    
3449                push @$active_formatting_elements, ['#marker', ''];                push @$active_formatting_elements, ['#marker', ''];
3450                                
# Line 3930  sub _construct_tree ($) { Line 3452  sub _construct_tree ($) {
3452                redo B;                redo B;
3453              } elsif ({              } elsif ({
3454                        caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
3455                        tbody => 1, tfoot => 1, thead => 1, tr => 1,                        tbody => 1, tfoot => 1, thead => 1,
3456                          tr => 1, # $self->{insertion_mode} == IN_ROW_IM
3457                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3458                ## As if </tr>                if ($self->{insertion_mode} == IN_ROW_IM) {
3459                ## have an element in table scope                  ## As if </tr>
3460                my $i;                  ## have an element in table scope
3461                INSCOPE: for (reverse 0..$#$open_elements) {                  my $i;
3462                  my $node = $open_elements->[$_];                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3463                  if ($node->[1] eq 'tr') {                    my $node = $self->{open_elements}->[$_];
3464                    $i = $_;                    if ($node->[1] eq 'tr') {
3465                    last INSCOPE;                      $i = $_;
3466                  } elsif ({                      last INSCOPE;
3467                            table => 1, html => 1,                    } elsif ({
3468                           }->{$node->[1]}) {                              table => 1, html => 1,
3469                    last INSCOPE;                             }->{$node->[1]}) {
3470                        last INSCOPE;
3471                      }
3472                    } # INSCOPE
3473                    unless (defined $i) {
3474                      !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});
3475                      ## Ignore the token
3476                      !!!next-token;
3477                      redo B;
3478                    }
3479                    
3480                    ## Clear back to table row context
3481                    while (not {
3482                      tr => 1, html => 1,
3483                    }->{$self->{open_elements}->[-1]->[1]}) {
3484                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3485                      pop @{$self->{open_elements}};
3486                    }
3487                    
3488                    pop @{$self->{open_elements}}; # tr
3489                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
3490                    if ($token->{tag_name} eq 'tr') {
3491                      ## reprocess
3492                      redo B;
3493                    } else {
3494                      ## reprocess in the "in table body" insertion mode...
3495                  }                  }
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error;  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
3496                }                }
3497    
3498                ## Clear back to table row context                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
3499                while (not {                  ## have an element in table scope
3500                  tr => 1, html => 1,                  my $i;
3501                }->{$open_elements->[-1]->[1]}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3502                  !!!parse-error;                    my $node = $self->{open_elements}->[$_];
3503                  pop @$open_elements;                    if ({
3504                }                         tbody => 1, thead => 1, tfoot => 1,
3505                          }->{$node->[1]}) {
3506                        $i = $_;
3507                        last INSCOPE;
3508                      } elsif ({
3509                                table => 1, html => 1,
3510                               }->{$node->[1]}) {
3511                        last INSCOPE;
3512                      }
3513                    } # INSCOPE
3514                    unless (defined $i) {
3515                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3516                      ## Ignore the token
3517                      !!!next-token;
3518                      redo B;
3519                    }
3520    
3521                pop @$open_elements; # tr                  ## Clear back to table body context
3522                $insertion_mode = 'in table body';                  while (not {
3523                ## reprocess                    tbody => 1, tfoot => 1, thead => 1, html => 1,
3524                redo B;                  }->{$self->{open_elements}->[-1]->[1]}) {
3525                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3526                      pop @{$self->{open_elements}};
3527                    }
3528                    
3529                    ## As if <{current node}>
3530                    ## have an element in table scope
3531                    ## true by definition
3532                    
3533                    ## Clear back to table body context
3534                    ## nop by definition
3535                    
3536                    pop @{$self->{open_elements}};
3537                    $self->{insertion_mode} = IN_TABLE_IM;
3538                    ## reprocess in "in table" insertion mode...
3539                  }
3540    
3541                  if ($token->{tag_name} eq 'col') {
3542                    ## Clear back to table context
3543                    while ($self->{open_elements}->[-1]->[1] ne 'table' and
3544                           $self->{open_elements}->[-1]->[1] ne 'html') {
3545                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3546                      pop @{$self->{open_elements}};
3547                    }
3548                    
3549                    !!!insert-element ('colgroup');
3550                    $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
3551                    ## reprocess
3552                    redo B;
3553                  } elsif ({
3554                            caption => 1,
3555                            colgroup => 1,
3556                            tbody => 1, tfoot => 1, thead => 1,
3557                           }->{$token->{tag_name}}) {
3558                    ## Clear back to table context
3559                    while ($self->{open_elements}->[-1]->[1] ne 'table' and
3560                           $self->{open_elements}->[-1]->[1] ne 'html') {
3561                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3562                      pop @{$self->{open_elements}};
3563                    }
3564                    
3565                    push @$active_formatting_elements, ['#marker', '']
3566                        if $token->{tag_name} eq 'caption';
3567                    
3568                    !!!insert-element ($token->{tag_name}, $token->{attributes});
3569                    $self->{insertion_mode} = {
3570                                               caption => IN_CAPTION_IM,
3571                                               colgroup => IN_COLUMN_GROUP_IM,
3572                                               tbody => IN_TABLE_BODY_IM,
3573                                               tfoot => IN_TABLE_BODY_IM,
3574                                               thead => IN_TABLE_BODY_IM,
3575                                              }->{$token->{tag_name}};
3576                    !!!next-token;
3577                    redo B;
3578                  } else {
3579                    die "$0: in table: <>: $token->{tag_name}";
3580                  }
3581              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
3582                ## NOTE: This is a code clone of "table in table"                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
               !!!parse-error;  
3583    
3584                ## As if </table>                ## As if </table>
3585                ## have a table element in table scope                ## have a table element in table scope
3586                my $i;                my $i;
3587                INSCOPE: for (reverse 0..$#$open_elements) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3588                  my $node = $open_elements->[$_];                  my $node = $self->{open_elements}->[$_];
3589                  if ($node->[1] eq 'table') {                  if ($node->[1] eq 'table') {
3590                    $i = $_;                    $i = $_;
3591                    last INSCOPE;                    last INSCOPE;
# Line 3984  sub _construct_tree ($) { Line 3596  sub _construct_tree ($) {
3596                  }                  }
3597                } # INSCOPE                } # INSCOPE
3598                unless (defined $i) {                unless (defined $i) {
3599                  !!!parse-error;                  !!!parse-error (type => 'unmatched end tag:table');
3600                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
3601                  !!!next-token;                  !!!next-token;
3602                  redo B;                  redo B;
# Line 3994  sub _construct_tree ($) { Line 3606  sub _construct_tree ($) {
3606                if ({                if ({
3607                     dd => 1, dt => 1, li => 1, p => 1,                     dd => 1, dt => 1, li => 1, p => 1,
3608                     td => 1, th => 1, tr => 1,                     td => 1, th => 1, tr => 1,
3609                    }->{$open_elements->[-1]->[1]}) {                     tbody => 1, tfoot=> 1, thead => 1,
3610                      }->{$self->{open_elements}->[-1]->[1]}) {
3611                  !!!back-token; # <table>                  !!!back-token; # <table>
3612                  $token = {type => 'end tag', tag_name => 'table'};                  $token = {type => END_TAG_TOKEN, tag_name => 'table'};
3613                  !!!back-token;                  !!!back-token;
3614                  $token = {type => 'end tag',                  $token = {type => END_TAG_TOKEN,
3615                            tag_name => $open_elements->[-1]->[1]}; # MUST                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3616                  redo B;                  redo B;
3617                }                }
3618    
3619                if ($open_elements->[-1]->[1] ne 'table') {                if ($self->{open_elements}->[-1]->[1] ne 'table') {
3620                  !!!parse-error;                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3621                }                }
3622    
3623                splice @$open_elements, $i;                splice @{$self->{open_elements}}, $i;
3624    
3625                $reset_insertion_mode->();                              $self->_reset_insertion_mode;
3626    
3627                ## reprocess                ## reprocess
3628                redo B;                redo B;
3629              } else {          } else {
3630                #            !!!parse-error (type => 'in table:'.$token->{tag_name});
3631              }  
3632            } elsif ($token->{type} eq 'end tag') {            $insert = $insert_to_foster;
3633              if ($token->{tag_name} eq 'tr') {            #
3634            }
3635          } elsif ($token->{type} == END_TAG_TOKEN) {
3636                if ($token->{tag_name} eq 'tr' and
3637                    $self->{insertion_mode} == IN_ROW_IM) {
3638                ## have an element in table scope                ## have an element in table scope
3639                my $i;                my $i;
3640                INSCOPE: for (reverse 0..$#$open_elements) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3641                  my $node = $open_elements->[$_];                  my $node = $self->{open_elements}->[$_];
3642                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq $token->{tag_name}) {
3643                    $i = $_;                    $i = $_;
3644                    last INSCOPE;                    last INSCOPE;
# Line 4032  sub _construct_tree ($) { Line 3649  sub _construct_tree ($) {
3649                  }                  }
3650                } # INSCOPE                } # INSCOPE
3651                unless (defined $i) {                unless (defined $i) {
3652                  !!!parse-error;                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3653                  ## Ignore the token                  ## Ignore the token
3654                  !!!next-token;                  !!!next-token;
3655                  redo B;                  redo B;
# Line 4041  sub _construct_tree ($) { Line 3658  sub _construct_tree ($) {
3658                ## Clear back to table row context                ## Clear back to table row context
3659                while (not {                while (not {
3660                  tr => 1, html => 1,                  tr => 1, html => 1,
3661                }->{$open_elements->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
3662                  !!!parse-error;                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3663                  pop @$open_elements;                  pop @{$self->{open_elements}};
3664                }                }
3665    
3666                pop @$open_elements; # tr                pop @{$self->{open_elements}}; # tr
3667                $insertion_mode = 'in table body';                $self->{insertion_mode} = IN_TABLE_BODY_IM;
3668                !!!next-token;                !!!next-token;
3669                redo B;                redo B;
3670              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
3671                ## As if </tr>                if ($self->{insertion_mode} == IN_ROW_IM) {
3672                ## have an element in table scope                  ## As if </tr>
3673                    ## have an element in table scope
3674                    my $i;
3675                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3676                      my $node = $self->{open_elements}->[$_];
3677                      if ($node->[1] eq 'tr') {
3678                        $i = $_;
3679                        last INSCOPE;
3680                      } elsif ({
3681                                table => 1, html => 1,
3682                               }->{$node->[1]}) {
3683                        last INSCOPE;
3684                      }
3685                    } # INSCOPE
3686                    unless (defined $i) {
3687                      !!!parse-error (type => 'unmatched end tag:'.$token->{type});
3688                      ## Ignore the token
3689                      !!!next-token;
3690                      redo B;
3691                    }
3692                    
3693                    ## Clear back to table row context
3694                    while (not {
3695                      tr => 1, html => 1,
3696                    }->{$self->{open_elements}->[-1]->[1]}) {
3697                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3698                      pop @{$self->{open_elements}};
3699                    }
3700                    
3701                    pop @{$self->{open_elements}}; # tr
3702                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
3703                    ## reprocess in the "in table body" insertion mode...
3704                  }
3705    
3706                  if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
3707                    ## have an element in table scope
3708                    my $i;
3709                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3710                      my $node = $self->{open_elements}->[$_];
3711                      if ({
3712                           tbody => 1, thead => 1, tfoot => 1,
3713                          }->{$node->[1]}) {
3714                        $i = $_;
3715                        last INSCOPE;
3716                      } elsif ({
3717                                table => 1, html => 1,
3718                               }->{$node->[1]}) {
3719                        last INSCOPE;
3720                      }
3721                    } # INSCOPE
3722                    unless (defined $i) {
3723                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3724                      ## Ignore the token
3725                      !!!next-token;
3726                      redo B;
3727                    }
3728                    
3729                    ## Clear back to table body context
3730                    while (not {
3731                      tbody => 1, tfoot => 1, thead => 1, html => 1,
3732                    }->{$self->{open_elements}->[-1]->[1]}) {
3733                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3734                      pop @{$self->{open_elements}};
3735                    }
3736                    
3737                    ## As if <{current node}>
3738                    ## have an element in table scope
3739                    ## true by definition
3740                    
3741                    ## Clear back to table body context
3742                    ## nop by definition
3743                    
3744                    pop @{$self->{open_elements}};
3745                    $self->{insertion_mode} = IN_TABLE_IM;
3746                    ## reprocess in the "in table" insertion mode...
3747                  }
3748    
3749                  ## have a table element in table scope
3750                my $i;                my $i;
3751                INSCOPE: for (reverse 0..$#$open_elements) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3752                  my $node = $open_elements->[$_];                  my $node = $self->{open_elements}->[$_];
3753                  if ($node->[1] eq 'tr') {                  if ($node->[1] eq $token->{tag_name}) {
3754                    $i = $_;                    $i = $_;
3755                    last INSCOPE;                    last INSCOPE;
3756                  } elsif ({                  } elsif ({
# Line 4066  sub _construct_tree ($) { Line 3760  sub _construct_tree ($) {
3760                  }                  }
3761                } # INSCOPE                } # INSCOPE
3762                unless (defined $i) {                unless (defined $i) {
3763                  !!!parse-error;                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3764                  ## Ignore the token                  ## Ignore the token
3765                  !!!next-token;                  !!!next-token;
3766                  redo B;                  redo B;
3767                }                }
3768    
3769                ## Clear back to table row context                ## generate implied end tags
3770                while (not {                if ({
3771                  tr => 1, html => 1,                     dd => 1, dt => 1, li => 1, p => 1,
3772                }->{$open_elements->[-1]->[1]}) {                     td => 1, th => 1, tr => 1,
3773                  !!!parse-error;                     tbody => 1, tfoot=> 1, thead => 1,
3774                  pop @$open_elements;                    }->{$self->{open_elements}->[-1]->[1]}) {
3775                    !!!back-token;
3776                    $token = {type => END_TAG_TOKEN,
3777                              tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3778                    redo B;
3779                }                }
3780                  
3781                pop @$open_elements; # tr                if ($self->{open_elements}->[-1]->[1] ne 'table') {
3782                $insertion_mode = 'in table body';                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3783                ## reprocess                }
3784                    
3785                  splice @{$self->{open_elements}}, $i;
3786                  
3787                  $self->_reset_insertion_mode;
3788                  
3789                  !!!next-token;
3790                redo B;                redo B;
3791              } elsif ({              } elsif ({
3792                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
3793                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}} and
3794                ## have an element in table scope                       $self->{insertion_mode} & ROW_IMS) {
3795                my $i;                if ($self->{insertion_mode} == IN_ROW_IM) {
3796                INSCOPE: for (reverse 0..$#$open_elements) {                  ## have an element in table scope
3797                  my $node = $open_elements->[$_];                  my $i;
3798                  if ($node->[1] eq $token->{tag_name}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3799                    $i = $_;                    my $node = $self->{open_elements}->[$_];
3800                    last INSCOPE;                    if ($node->[1] eq $token->{tag_name}) {
3801                  } elsif ({                      $i = $_;
3802                            table => 1, html => 1,                      last INSCOPE;
3803                           }->{$node->[1]}) {                    } elsif ({
3804                    last INSCOPE;                              table => 1, html => 1,
3805                               }->{$node->[1]}) {
3806                        last INSCOPE;
3807                      }
3808                    } # INSCOPE
3809                      unless (defined $i) {
3810                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3811                        ## Ignore the token
3812                        !!!next-token;
3813                        redo B;
3814                      }
3815                    
3816                    ## As if </tr>
3817                    ## have an element in table scope
3818                    my $i;
3819                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3820                      my $node = $self->{open_elements}->[$_];
3821                      if ($node->[1] eq 'tr') {
3822                        $i = $_;
3823                        last INSCOPE;
3824                      } elsif ({
3825                                table => 1, html => 1,
3826                               }->{$node->[1]}) {
3827                        last INSCOPE;
3828                      }
3829                    } # INSCOPE
3830                      unless (defined $i) {
3831                        !!!parse-error (type => 'unmatched end tag:tr');
3832                        ## Ignore the token
3833                        !!!next-token;
3834                        redo B;
3835                      }
3836                    
3837                    ## Clear back to table row context
3838                    while (not {
3839                      tr => 1, html => 1,
3840                    }->{$self->{open_elements}->[-1]->[1]}) {
3841                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3842                      pop @{$self->{open_elements}};
3843                  }                  }
3844                } # INSCOPE                  
3845                unless (defined $i) {                  pop @{$self->{open_elements}}; # tr
3846                  !!!parse-error;                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
3847                  ## Ignore the token                  ## reprocess in the "in table body" insertion mode...
                 !!!next-token;  
                 redo B;  
3848                }                }
3849    
               ## As if </tr>  
3850                ## have an element in table scope                ## have an element in table scope
3851                my $i;                my $i;
3852                INSCOPE: for (reverse 0..$#$open_elements) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3853                  my $node = $open_elements->[$_];                  my $node = $self->{open_elements}->[$_];
3854                  if ($node->[1] eq 'tr') {                  if ($node->[1] eq $token->{tag_name}) {
3855                    $i = $_;                    $i = $_;
3856                    last INSCOPE;                    last INSCOPE;
3857                  } elsif ({                  } elsif ({
# Line 4122  sub _construct_tree ($) { Line 3861  sub _construct_tree ($) {
3861                  }                  }
3862                } # INSCOPE                } # INSCOPE
3863                unless (defined $i) {                unless (defined $i) {
3864                  !!!parse-error;                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3865                  ## Ignore the token                  ## Ignore the token
3866                  !!!next-token;                  !!!next-token;
3867                  redo B;                  redo B;
3868                }                }
3869    
3870                ## Clear back to table row context                ## Clear back to table body context
3871                while (not {                while (not {
3872                  tr => 1, html => 1,                  tbody => 1, tfoot => 1, thead => 1, html => 1,
3873                }->{$open_elements->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
3874                  !!!parse-error;                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3875                  pop @$open_elements;                  pop @{$self->{open_elements}};
3876                }                }
3877    
3878                pop @$open_elements; # tr                pop @{$self->{open_elements}};
3879                $insertion_mode = 'in table body';                $self->{insertion_mode} = IN_TABLE_IM;
3880                ## reprocess                !!!next-token;
3881                redo B;                redo B;
3882              } elsif ({              } elsif ({
3883                        body => 1, caption => 1, col => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
3884                        colgroup => 1, html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
3885                          tr => 1, # $self->{insertion_mode} == IN_ROW_IM
3886                          tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
3887                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3888                !!!parse-error;                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3889                ## Ignore the token                ## Ignore the token
3890                !!!next-token;                !!!next-token;
3891                redo B;                redo B;
3892              } else {          } else {
3893                #            !!!parse-error (type => 'in table:/'.$token->{tag_name});
             }  
           } else {  
             #  
           }  
   
           ## As if in table  
           !!!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});  
3894    
3895              !!!next-token;            $insert = $insert_to_foster;
3896              redo B;            #
3897            } elsif ($token->{type} eq 'comment') {          }
3898              ## NOTE: This is a code clone of "comment in body".        } else {
3899              my $comment = $self->{document}->create_comment ($token->{data});          die "$0: $token->{type}: Unknown token type";
3900              $open_elements->[-1]->[0]->append_child ($comment);        }
3901              !!!next-token;      } elsif ($self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
3902              redo B;            if ($token->{type} == CHARACTER_TOKEN) {
3903            } elsif ($token->{type} eq 'start tag') {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
3904              if ({                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
3905                   caption => 1, col => 1, colgroup => 1,                unless (length $token->{data}) {
                  tbody => 1, td => 1, tfoot => 1, th => 1,  
                  thead => 1, tr => 1,  
                 }->{$token->{tag_name}}) {  
               ## have an element in table scope  
               my $tn;  
               INSCOPE: for (reverse 0..$#$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  
3906                  !!!next-token;                  !!!next-token;
3907                  redo B;                  redo B;
3908                }                }
3909                }
3910                ## Close the cell              
3911                !!!back-token; # <?>              #
3912                $token = {type => 'end tag', tag_name => $tn};            } elsif ($token->{type} == START_TAG_TOKEN) {
3913                if ($token->{tag_name} eq 'col') {
3914                  !!!insert-element ($token->{tag_name}, $token->{attributes});
3915                  pop @{$self->{open_elements}};
3916                  !!!next-token;
3917                redo B;                redo B;
3918              } else {              } else {
3919                #                #
3920              }              }
3921            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
3922              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {              if ($token->{tag_name} eq 'colgroup') {
3923                ## have an element in table scope                if ($self->{open_elements}->[-1]->[1] eq 'html') {
3924                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;  
3925                  ## Ignore the token                  ## Ignore the token
3926                  !!!next-token;                  !!!next-token;
3927                  redo B;                  redo B;
3928                  } else {
3929                    pop @{$self->{open_elements}}; # colgroup
3930                    $self->{insertion_mode} = IN_TABLE_IM;
3931                    !!!next-token;
3932                    redo B;            
3933                }                }
3934                              } elsif ($token->{tag_name} eq 'col') {
3935                ## 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;  
3936                ## Ignore the token                ## Ignore the token
3937                !!!next-token;                !!!next-token;
3938                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;  
3939              } else {              } else {
3940                #                #
3941              }              }
3942            } else {            } else {
3943              #              #
3944            }            }
3945              
3946            $in_body->($insert_to_current);            ## As if </colgroup>
3947            redo B;            if ($self->{open_elements}->[-1]->[1] eq 'html') {
3948          } elsif ($insertion_mode eq 'in select') {              !!!parse-error (type => 'unmatched end tag:colgroup');
3949            if ($token->{type} eq 'character') {              ## Ignore the token
             $open_elements->[-1]->[0]->manakai_append_text ($token->{data});  
3950              !!!next-token;              !!!next-token;
3951              redo B;              redo B;
3952            } elsif ($token->{type} eq 'comment') {            } else {
3953              my $comment = $self->{document}->create_comment ($token->{data});              pop @{$self->{open_elements}}; # colgroup
3954              $open_elements->[-1]->[0]->append_child ($comment);              $self->{insertion_mode} = IN_TABLE_IM;
3955              !!!next-token;              ## reprocess
3956              redo B;              redo B;
3957            } elsif ($token->{type} eq 'start tag') {            }
3958        } elsif ($self->{insertion_mode} == IN_SELECT_IM) {
3959          if ($token->{type} == CHARACTER_TOKEN) {
3960            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
3961            !!!next-token;
3962            redo B;
3963          } elsif ($token->{type} == START_TAG_TOKEN) {
3964              if ($token->{tag_name} eq 'option') {              if ($token->{tag_name} eq 'option') {
3965                if ($open_elements->[-1]->[1] eq 'option') {                if ($self->{open_elements}->[-1]->[1] eq 'option') {
3966                  ## As if </option>                  ## As if </option>
3967                  pop @$open_elements;                  pop @{$self->{open_elements}};
3968                }                }
3969    
3970                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
3971                !!!next-token;                !!!next-token;
3972                redo B;                redo B;
3973              } elsif ($token->{tag_name} eq 'optgroup') {              } elsif ($token->{tag_name} eq 'optgroup') {
3974                if ($open_elements->[-1]->[1] eq 'option') {                if ($self->{open_elements}->[-1]->[1] eq 'option') {
3975                  ## As if </option>                  ## As if </option>
3976                  pop @$open_elements;                  pop @{$self->{open_elements}};
3977                }                }
3978    
3979                if ($open_elements->[-1]->[1] eq 'optgroup') {                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {
3980                  ## As if </optgroup>                  ## As if </optgroup>
3981                  pop @$open_elements;                  pop @{$self->{open_elements}};
3982                }                }
3983    
3984                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
3985                !!!next-token;                !!!next-token;
3986                redo B;                redo B;
3987              } elsif ($token->{tag_name} eq 'select') {              } elsif ($token->{tag_name} eq 'select') {
3988                !!!parse-error;                !!!parse-error (type => 'not closed:select');
3989                ## As if </select> instead                ## As if </select> instead
3990                ## have an element in table scope                ## have an element in table scope
3991                my $i;                my $i;
3992                INSCOPE: for (reverse 0..$#$open_elements) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3993                  my $node = $open_elements->[$_];                  my $node = $self->{open_elements}->[$_];
3994                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq $token->{tag_name}) {
3995                    $i = $_;                    $i = $_;
3996                    last INSCOPE;                    last INSCOPE;
# Line 4355  sub _construct_tree ($) { Line 4001  sub _construct_tree ($) {
4001                  }                  }
4002                } # INSCOPE                } # INSCOPE
4003                unless (defined $i) {                unless (defined $i) {
4004                  !!!parse-error;                  !!!parse-error (type => 'unmatched end tag:select');
4005                  ## Ignore the token                  ## Ignore the token
4006                  !!!next-token;                  !!!next-token;
4007                  redo B;                  redo B;
4008                }                }
4009                                
4010                splice @$open_elements, $i;                splice @{$self->{open_elements}}, $i;
4011    
4012                $reset_insertion_mode->();                $self->_reset_insertion_mode;
4013    
4014                !!!next-token;                !!!next-token;
4015                redo B;                redo B;
4016              } else {          } else {
4017                #            !!!parse-error (type => 'in select:'.$token->{tag_name});
4018              }            ## Ignore the token
4019            } elsif ($token->{type} eq 'end tag') {            !!!next-token;
4020              redo B;
4021            }
4022          } elsif ($token->{type} == END_TAG_TOKEN) {
4023              if ($token->{tag_name} eq 'optgroup') {              if ($token->{tag_name} eq 'optgroup') {
4024                if ($open_elements->[-1]->[1] eq 'option' and                if ($self->{open_elements}->[-1]->[1] eq 'option' and
4025                    $open_elements->[-2]->[1] eq 'optgroup') {                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {
4026                  ## As if </option>                  ## As if </option>
4027                  splice @$open_elements, -2;                  splice @{$self->{open_elements}}, -2;
4028                } elsif ($open_elements->[-1]->[1] eq 'optgroup') {                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {
4029                  pop @$open_elements;                  pop @{$self->{open_elements}};
4030                } else {                } else {
4031                  !!!parse-error;                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4032                  ## Ignore the token                  ## Ignore the token
4033                }                }
4034                !!!next-token;                !!!next-token;
4035                redo B;                redo B;
4036              } elsif ($token->{tag_name} eq 'option') {              } elsif ($token->{tag_name} eq 'option') {
4037                if ($open_elements->[-1]->[1] eq 'option') {                if ($self->{open_elements}->[-1]->[1] eq 'option') {
4038                  pop @$open_elements;                  pop @{$self->{open_elements}};
4039                } else {                } else {
4040                  !!!parse-error;                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4041                  ## Ignore the token                  ## Ignore the token
4042                }                }
4043                !!!next-token;                !!!next-token;
# Line 4396  sub _construct_tree ($) { Line 4045  sub _construct_tree ($) {
4045              } elsif ($token->{tag_name} eq 'select') {              } elsif ($token->{tag_name} eq 'select') {
4046                ## have an element in table scope                ## have an element in table scope
4047                my $i;                my $i;
4048                INSCOPE: for (reverse 0..$#$open_elements) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4049                  my $node = $open_elements->[$_];                  my $node = $self->{open_elements}->[$_];
4050                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq $token->{tag_name}) {
4051                    $i = $_;                    $i = $_;
4052                    last INSCOPE;                    last INSCOPE;
# Line 4408  sub _construct_tree ($) { Line 4057  sub _construct_tree ($) {
4057                  }                  }
4058                } # INSCOPE                } # INSCOPE
4059                unless (defined $i) {                unless (defined $i) {
4060                  !!!parse-error;                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4061                  ## Ignore the token                  ## Ignore the token
4062                  !!!next-token;                  !!!next-token;
4063                  redo B;                  redo B;
4064                }                }
4065                                
4066                splice @$open_elements, $i;                splice @{$self->{open_elements}}, $i;
4067    
4068                $reset_insertion_mode->();                $self->_reset_insertion_mode;
4069    
4070                !!!next-token;                !!!next-token;
4071                redo B;                redo B;
# Line 4424  sub _construct_tree ($) { Line 4073  sub _construct_tree ($) {
4073                        caption => 1, table => 1, tbody => 1,                        caption => 1, table => 1, tbody => 1,
4074                        tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                        tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
4075                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4076                !!!parse-error;                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4077                                
4078                ## have an element in table scope                ## have an element in table scope
4079                my $i;                my $i;
4080                INSCOPE: for (reverse 0..$#$open_elements) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4081                  my $node = $open_elements->[$_];                  my $node = $self->{open_elements}->[$_];
4082                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq $token->{tag_name}) {
4083                    $i = $_;                    $i = $_;
4084                    last INSCOPE;                    last INSCOPE;
# Line 4448  sub _construct_tree ($) { Line 4097  sub _construct_tree ($) {
4097                ## As if </select>                ## As if </select>
4098                ## have an element in table scope                ## have an element in table scope
4099                undef $i;                undef $i;
4100                INSCOPE: for (reverse 0..$#$open_elements) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4101                  my $node = $open_elements->[$_];                  my $node = $self->{open_elements}->[$_];
4102                  if ($node->[1] eq 'select') {                  if ($node->[1] eq 'select') {
4103                    $i = $_;                    $i = $_;
4104                    last INSCOPE;                    last INSCOPE;
# Line 4460  sub _construct_tree ($) { Line 4109  sub _construct_tree ($) {
4109                  }                  }
4110                } # INSCOPE                } # INSCOPE
4111                unless (defined $i) {                unless (defined $i) {
4112                  !!!parse-error;                  !!!parse-error (type => 'unmatched end tag:select');
4113                  ## Ignore the </select> token                  ## Ignore the </select> token
4114                  !!!next-token; ## TODO: ok?                  !!!next-token; ## TODO: ok?
4115                  redo B;                  redo B;
4116                }                }
4117                                
4118                splice @$open_elements, $i;                splice @{$self->{open_elements}}, $i;
4119    
4120                $reset_insertion_mode->();                $self->_reset_insertion_mode;
4121    
4122                ## reprocess                ## reprocess
4123                redo B;                redo B;
4124              } else {          } else {
4125                #            !!!parse-error (type => 'in select:/'.$token->{tag_name});
             }  
           } else {  
             #  
           }  
   
           !!!parse-error;  
4126            ## Ignore the token            ## Ignore the token
4127            !!!next-token;            !!!next-token;
4128            redo B;            redo B;
4129          } elsif ($insertion_mode eq 'after body') {          }
4130            if ($token->{type} eq 'character') {        } else {
4131              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          die "$0: $token->{type}: Unknown token type";
4132                ## As if in body        }
4133                $reconstruct_active_formatting_elements->($insert_to_current);      } elsif ($self->{insertion_mode} & BODY_AFTER_IMS) {
4134          if ($token->{type} == CHARACTER_TOKEN) {
4135            if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4136              my $data = $1;
4137              ## As if in body
4138              $reconstruct_active_formatting_elements->($insert_to_current);
4139                                
4140                $open_elements->[-1]->[0]->manakai_append_text ($token->{data});            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4141              
4142                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);  
4143              !!!next-token;              !!!next-token;
4144              redo B;              redo B;
4145            } elsif ($token->{type} eq 'end tag') {            }
4146              if ($token->{tag_name} eq 'html') {          }
4147                ## TODO: if inner_html, parse-error, ignore the token; otherwise,          
4148            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4149              !!!parse-error (type => 'after html:#character');
4150    
4151                $phase = 'trailing end';            ## Reprocess in the "main" phase, "after body" insertion mode...
4152                !!!next-token;          }
4153                redo B;          
4154              } else {          ## "after body" insertion mode
4155                #          !!!parse-error (type => 'after body:#character');
4156              }  
4157            $self->{insertion_mode} = IN_BODY_IM;
4158            ## reprocess
4159            redo B;
4160          } elsif ($token->{type} == START_TAG_TOKEN) {
4161            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4162              !!!parse-error (type => 'after html:'.$token->{tag_name});
4163              
4164              ## Reprocess in the "main" phase, "after body" insertion mode...
4165            }
4166    
4167            ## "after body" insertion mode
4168            !!!parse-error (type => 'after body:'.$token->{tag_name});
4169    
4170            $self->{insertion_mode} = IN_BODY_IM;
4171            ## reprocess
4172            redo B;
4173          } elsif ($token->{type} == END_TAG_TOKEN) {
4174            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4175              !!!parse-error (type => 'after html:/'.$token->{tag_name});
4176              
4177              $self->{insertion_mode} = AFTER_BODY_IM;
4178              ## Reprocess in the "main" phase, "after body" insertion mode...
4179            }
4180    
4181            ## "after body" insertion mode
4182            if ($token->{tag_name} eq 'html') {
4183              if (defined $self->{inner_html_node}) {
4184                !!!parse-error (type => 'unmatched end tag:html');
4185                ## Ignore the token
4186                !!!next-token;
4187                redo B;
4188            } else {            } else {
4189              #              $self->{insertion_mode} = AFTER_HTML_BODY_IM;
4190                !!!next-token;
4191                redo B;
4192            }            }
4193            } else {
4194              !!!parse-error (type => 'after body:/'.$token->{tag_name});
4195    
4196            !!!parse-error ('data after body');            $self->{insertion_mode} = IN_BODY_IM;
           $insertion_mode = 'in body';  
4197            ## reprocess            ## reprocess
4198            redo B;            redo B;
4199          } elsif ($insertion_mode eq 'in frameset') {          }
4200            if ($token->{type} eq 'character') {        } else {
4201              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          die "$0: $token->{type}: Unknown token type";
4202                $open_elements->[-1]->[0]->manakai_append_text ($token->{data});        }
4203        } elsif ($self->{insertion_mode} & FRAME_IMS) {
4204          if ($token->{type} == CHARACTER_TOKEN) {
4205            if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4206              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4207              
4208              unless (length $token->{data}) {
4209                !!!next-token;
4210                redo B;
4211              }
4212            }
4213            
4214            if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
4215              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4216                !!!parse-error (type => 'in frameset:#character');
4217              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
4218                !!!parse-error (type => 'after frameset:#character');
4219              } else { # "after html frameset"
4220                !!!parse-error (type => 'after html:#character');
4221    
4222                $self->{insertion_mode} = AFTER_FRAMESET_IM;
4223                ## Reprocess in the "main" phase, "after frameset"...
4224                !!!parse-error (type => 'after frameset:#character');
4225              }
4226              
4227              ## Ignore the token.
4228              if (length $token->{data}) {
4229                ## reprocess the rest of characters
4230              } else {
4231                !!!next-token;
4232              }
4233              redo B;
4234            }
4235            
4236            die qq[$0: Character "$token->{data}"];
4237          } elsif ($token->{type} == START_TAG_TOKEN) {
4238            if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4239              !!!parse-error (type => 'after html:'.$token->{tag_name});
4240    
4241              $self->{insertion_mode} = AFTER_FRAMESET_IM;
4242              ## Process in the "main" phase, "after frameset" insertion mode...
4243            }
4244    
4245                unless (length $token->{data}) {          if ($token->{tag_name} eq 'frameset' and
4246                  !!!next-token;              $self->{insertion_mode} == IN_FRAMESET_IM) {
4247                  redo B;            !!!insert-element ($token->{tag_name}, $token->{attributes});
4248                }            !!!next-token;
4249              }            redo B;
4250            } elsif ($token->{tag_name} eq 'frame' and
4251                     $self->{insertion_mode} == IN_FRAMESET_IM) {
4252              !!!insert-element ($token->{tag_name}, $token->{attributes});
4253              pop @{$self->{open_elements}};
4254              !!!next-token;
4255              redo B;
4256            } elsif ($token->{tag_name} eq 'noframes') {
4257              ## NOTE: As if in body.
4258              $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);
4259              redo B;
4260            } else {
4261              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4262                !!!parse-error (type => 'in frameset:'.$token->{tag_name});
4263              } else {
4264                !!!parse-error (type => 'after frameset:'.$token->{tag_name});
4265              }
4266              ## Ignore the token
4267              !!!next-token;
4268              redo B;
4269            }
4270          } elsif ($token->{type} == END_TAG_TOKEN) {
4271            if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4272              !!!parse-error (type => 'after html:/'.$token->{tag_name});
4273    
4274              $self->{insertion_mode} = AFTER_FRAMESET_IM;
4275              ## Process in the "main" phase, "after frameset" insertion mode...
4276            }
4277    
4278              #          if ($token->{tag_name} eq 'frameset' and
4279            } elsif ($token->{type} eq 'comment') {              $self->{insertion_mode} == IN_FRAMESET_IM) {
4280              my $comment = $self->{document}->create_comment ($token->{data});            if ($self->{open_elements}->[-1]->[1] eq 'html' and
4281              $open_elements->[-1]->[0]->append_child ($comment);                @{$self->{open_elements}} == 1) {
4282                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4283                ## Ignore the token
4284              !!!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 {  
               #  
             }  
4285            } else {            } else {
4286              #              pop @{$self->{open_elements}};
4287                !!!next-token;
4288              }
4289    
4290              if (not defined $self->{inner_html_node} and
4291                  $self->{open_elements}->[-1]->[1] ne 'frameset') {
4292                $self->{insertion_mode} = AFTER_FRAMESET_IM;
4293              }
4294              redo B;
4295            } elsif ($token->{tag_name} eq 'html' and
4296                     $self->{insertion_mode} == AFTER_FRAMESET_IM) {
4297              $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
4298              !!!next-token;
4299              redo B;
4300            } else {
4301              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4302                !!!parse-error (type => 'in frameset:/'.$token->{tag_name});
4303              } else {
4304                !!!parse-error (type => 'after frameset:/'.$token->{tag_name});
4305            }            }
             
           !!!parse-error;  
4306            ## Ignore the token            ## Ignore the token
4307            !!!next-token;            !!!next-token;
4308            redo B;            redo B;
4309          } elsif ($insertion_mode eq 'after frameset') {          }
4310            if ($token->{type} eq 'character') {        } else {
4311              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          die "$0: $token->{type}: Unknown token type";
4312                $open_elements->[-1]->[0]->manakai_append_text ($token->{data});        }
4313    
4314                unless (length $token->{data}) {        ## ISSUE: An issue in spec here
4315                  !!!next-token;      } else {
4316                  redo B;        die "$0: $self->{insertion_mode}: Unknown insertion mode";
4317                }      }
             }  
4318    
4319              #      ## "in body" insertion mode
4320            } elsif ($token->{type} eq 'comment') {      if ($token->{type} == START_TAG_TOKEN) {
4321              my $comment = $self->{document}->create_comment ($token->{data});        if ($token->{tag_name} eq 'script') {
4322              $open_elements->[-1]->[0]->append_child ($comment);          ## NOTE: This is an "as if in head" code clone
4323              !!!next-token;          $script_start_tag->($insert);
4324              redo B;          redo B;
4325            } elsif ($token->{type} eq 'start tag') {        } elsif ($token->{tag_name} eq 'style') {
4326              if ($token->{tag_name} eq 'noframes') {          ## NOTE: This is an "as if in head" code clone
4327                $in_body->($insert_to_current);          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
4328                redo B;          redo B;
4329              } else {        } elsif ({
4330                #                  base => 1, link => 1,
4331                   }->{$token->{tag_name}}) {
4332            ## NOTE: This is an "as if in head" code clone, only "-t" differs
4333            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4334            pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4335            !!!next-token;
4336            redo B;
4337          } elsif ($token->{tag_name} eq 'meta') {
4338            ## NOTE: This is an "as if in head" code clone, only "-t" differs
4339            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4340            pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4341    
4342            unless ($self->{confident}) {
4343              my $charset;
4344              if ($token->{attributes}->{charset}) { ## TODO: And if supported
4345                $charset = $token->{attributes}->{charset}->{value};
4346              }
4347              if ($token->{attributes}->{'http-equiv'}) {
4348                ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.
4349                if ($token->{attributes}->{'http-equiv'}->{value}
4350                    =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=
4351                        [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4352                        ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
4353                  $charset = defined $1 ? $1 : defined $2 ? $2 : $3;
4354                } ## TODO: And if supported
4355              }
4356              ## TODO: Change the encoding
4357            }
4358    
4359            !!!next-token;
4360            redo B;
4361          } elsif ($token->{tag_name} eq 'title') {
4362            !!!parse-error (type => 'in body:title');
4363            ## NOTE: This is an "as if in head" code clone
4364            $parse_rcdata->(RCDATA_CONTENT_MODEL, sub {
4365              if (defined $self->{head_element}) {
4366                $self->{head_element}->append_child ($_[0]);
4367              } else {
4368                $insert->($_[0]);
4369              }
4370            });
4371            redo B;
4372          } elsif ($token->{tag_name} eq 'body') {
4373            !!!parse-error (type => 'in body:body');
4374                  
4375            if (@{$self->{open_elements}} == 1 or
4376                $self->{open_elements}->[1]->[1] ne 'body') {
4377              ## Ignore the token
4378            } else {
4379              my $body_el = $self->{open_elements}->[1]->[0];
4380              for my $attr_name (keys %{$token->{attributes}}) {
4381                unless ($body_el->has_attribute_ns (undef, $attr_name)) {
4382                  $body_el->set_attribute_ns
4383                    (undef, [undef, $attr_name],
4384                     $token->{attributes}->{$attr_name}->{value});
4385              }              }
4386            } elsif ($token->{type} eq 'end tag') {            }
4387              if ($token->{tag_name} eq 'html') {          }
4388                $phase = 'trailing end';          !!!next-token;
4389            redo B;
4390          } elsif ({
4391                    address => 1, blockquote => 1, center => 1, dir => 1,
4392                    div => 1, dl => 1, fieldset => 1, listing => 1,
4393                    menu => 1, ol => 1, p => 1, ul => 1,
4394                    pre => 1,
4395                   }->{$token->{tag_name}}) {
4396            ## 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            if ($token->{tag_name} eq 'pre') {
4412              !!!next-token;
4413              if ($token->{type} == CHARACTER_TOKEN) {
4414                $token->{data} =~ s/^\x0A//;
4415                unless (length $token->{data}) {
4416                !!!next-token;                !!!next-token;
               redo B;  
             } else {  
               #  
4417              }              }
           } else {  
             #  
4418            }            }
4419                      } else {
4420            !!!parse-error;            !!!next-token;
4421            }
4422            redo B;
4423          } elsif ($token->{tag_name} eq 'form') {
4424            if (defined $self->{form_element}) {
4425              !!!parse-error (type => 'in form:form');
4426            ## Ignore the token            ## Ignore the token
4427            !!!next-token;            !!!next-token;
4428            redo B;            redo B;
   
           ## ISSUE: An issue in spec there  
4429          } else {          } else {
4430            die "$0: $insertion_mode: Unknown insertion mode";            ## has a p element in scope
4431              INSCOPE: for (reverse @{$self->{open_elements}}) {
4432                if ($_->[1] eq 'p') {
4433                  !!!back-token;
4434                  $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4435                  redo B;
4436                } elsif ({
4437                          table => 1, caption => 1, td => 1, th => 1,
4438                          button => 1, marquee => 1, object => 1, html => 1,
4439                         }->{$_->[1]}) {
4440                  last INSCOPE;
4441                }
4442              } # INSCOPE
4443                
4444              !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4445              $self->{form_element} = $self->{open_elements}->[-1]->[0];
4446              !!!next-token;
4447              redo B;
4448          }          }
4449        }        } elsif ($token->{tag_name} eq 'li') {
4450      } elsif ($phase eq 'trailing end') {          ## has a p element in scope
4451        ## states in the main stage is preserved yet # MUST          INSCOPE: for (reverse @{$self->{open_elements}}) {
4452                    if ($_->[1] eq 'p') {
4453        if ($token->{type} eq 'DOCTYPE') {              !!!back-token;
4454          !!!parse-error;              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4455          ## Ignore the token              redo B;
4456              } elsif ({
4457                        table => 1, caption => 1, td => 1, th => 1,
4458                        button => 1, marquee => 1, object => 1, html => 1,
4459                       }->{$_->[1]}) {
4460                last INSCOPE;
4461              }
4462            } # INSCOPE
4463              
4464            ## Step 1
4465            my $i = -1;
4466            my $node = $self->{open_elements}->[$i];
4467            LI: {
4468              ## Step 2
4469              if ($node->[1] eq 'li') {
4470                if ($i != -1) {
4471                  !!!parse-error (type => 'end tag missing:'.
4472                                  $self->{open_elements}->[-1]->[1]);
4473                }
4474                splice @{$self->{open_elements}}, $i;
4475                last LI;
4476              }
4477              
4478              ## Step 3
4479              if (not $formatting_category->{$node->[1]} and
4480                  #not $phrasing_category->{$node->[1]} and
4481                  ($special_category->{$node->[1]} or
4482                   $scoping_category->{$node->[1]}) and
4483                  $node->[1] ne 'address' and $node->[1] ne 'div') {
4484                last LI;
4485              }
4486              
4487              ## Step 4
4488              $i--;
4489              $node = $self->{open_elements}->[$i];
4490              redo LI;
4491            } # LI
4492              
4493            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4494          !!!next-token;          !!!next-token;
4495          redo B;          redo B;
4496        } elsif ($token->{type} eq 'comment') {        } elsif ($token->{tag_name} eq 'dd' or $token->{tag_name} eq 'dt') {
4497          my $comment = $self->{document}->create_comment ($token->{data});          ## has a p element in scope
4498          $self->{document}->append_child ($comment);          INSCOPE: for (reverse @{$self->{open_elements}}) {
4499              if ($_->[1] eq 'p') {
4500                !!!back-token;
4501                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4502                redo B;
4503              } elsif ({
4504                        table => 1, caption => 1, td => 1, th => 1,
4505                        button => 1, marquee => 1, object => 1, html => 1,
4506                       }->{$_->[1]}) {
4507                last INSCOPE;
4508              }
4509            } # INSCOPE
4510              
4511            ## Step 1
4512            my $i = -1;
4513            my $node = $self->{open_elements}->[$i];
4514            LI: {
4515              ## Step 2
4516              if ($node->[1] eq 'dt' or $node->[1] eq 'dd') {
4517                if ($i != -1) {
4518                  !!!parse-error (type => 'end tag missing:'.
4519                                  $self->{open_elements}->[-1]->[1]);
4520                }
4521                splice @{$self->{open_elements}}, $i;
4522                last LI;
4523              }
4524              
4525              ## Step 3
4526              if (not $formatting_category->{$node->[1]} and
4527                  #not $phrasing_category->{$node->[1]} and
4528                  ($special_category->{$node->[1]} or
4529                   $scoping_category->{$node->[1]}) and
4530                  $node->[1] ne 'address' and $node->[1] ne 'div') {
4531                last LI;
4532              }
4533              
4534              ## Step 4
4535              $i--;
4536              $node = $self->{open_elements}->[$i];
4537              redo LI;
4538            } # LI
4539              
4540            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4541          !!!next-token;          !!!next-token;
4542          redo B;          redo B;
4543        } elsif ($token->{type} eq 'character') {        } elsif ($token->{tag_name} eq 'plaintext') {
4544          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          ## has a p element in scope
4545            my $data = $1;          INSCOPE: for (reverse @{$self->{open_elements}}) {
4546            ## As if in the main phase.            if ($_->[1] eq 'p') {
4547            ## NOTE: The insertion mode in the main phase              !!!back-token;
4548            ## just before the phase has been changed to the trailing              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4549            ## end phase is either "after body" or "after frameset".              redo B;
4550            $reconstruct_active_formatting_elements->($insert_to_current)            } elsif ({
4551              if $phase eq 'main';                      table => 1, caption => 1, td => 1, th => 1,
4552                        button => 1, marquee => 1, object => 1, html => 1,
4553                       }->{$_->[1]}) {
4554                last INSCOPE;
4555              }
4556            } # INSCOPE
4557              
4558            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4559              
4560            $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
4561              
4562            !!!next-token;
4563            redo B;
4564          } elsif ({
4565                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
4566                   }->{$token->{tag_name}}) {
4567            ## has a p element in scope
4568            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4569              my $node = $self->{open_elements}->[$_];
4570              if ($node->[1] eq 'p') {
4571                !!!back-token;
4572                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4573                redo B;
4574              } elsif ({
4575                        table => 1, caption => 1, td => 1, th => 1,
4576                        button => 1, marquee => 1, object => 1, html => 1,
4577                       }->{$node->[1]}) {
4578                last INSCOPE;
4579              }
4580            } # INSCOPE
4581              
4582            ## NOTE: See <http://html5.org/tools/web-apps-tracker?from=925&to=926>
4583            ## has an element in scope
4584            #my $i;
4585            #INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4586            #  my $node = $self->{open_elements}->[$_];
4587            #  if ({
4588            #       h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
4589            #      }->{$node->[1]}) {
4590            #    $i = $_;
4591            #    last INSCOPE;
4592            #  } elsif ({
4593            #            table => 1, caption => 1, td => 1, th => 1,
4594            #            button => 1, marquee => 1, object => 1, html => 1,
4595            #           }->{$node->[1]}) {
4596            #    last INSCOPE;
4597            #  }
4598            #} # INSCOPE
4599            #  
4600            #if (defined $i) {
4601            #  !!! parse-error (type => 'in hn:hn');
4602            #  splice @{$self->{open_elements}}, $i;
4603            #}
4604              
4605            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4606              
4607            !!!next-token;
4608            redo B;
4609          } elsif ($token->{tag_name} eq 'a') {
4610            AFE: for my $i (reverse 0..$#$active_formatting_elements) {
4611              my $node = $active_formatting_elements->[$i];
4612              if ($node->[1] eq 'a') {
4613                !!!parse-error (type => 'in a:a');
4614                
4615                !!!back-token;
4616                $token = {type => END_TAG_TOKEN, tag_name => 'a'};
4617                $formatting_end_tag->($token->{tag_name});
4618                
4619                AFE2: for (reverse 0..$#$active_formatting_elements) {
4620                  if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
4621                    splice @$active_formatting_elements, $_, 1;
4622                    last AFE2;
4623                  }
4624                } # AFE2
4625                OE: for (reverse 0..$#{$self->{open_elements}}) {
4626                  if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {
4627                    splice @{$self->{open_elements}}, $_, 1;
4628                    last OE;
4629                  }
4630                } # OE
4631                last AFE;
4632              } elsif ($node->[0] eq '#marker') {
4633                last AFE;
4634              }
4635            } # AFE
4636              
4637            $reconstruct_active_formatting_elements->($insert_to_current);
4638    
4639            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4640            push @$active_formatting_elements, $self->{open_elements}->[-1];
4641    
4642            !!!next-token;
4643            redo B;
4644          } elsif ({
4645                    b => 1, big => 1, em => 1, font => 1, i => 1,
4646                    s => 1, small => 1, strile => 1,
4647                    strong => 1, tt => 1, u => 1,
4648                   }->{$token->{tag_name}}) {
4649            $reconstruct_active_formatting_elements->($insert_to_current);
4650            
4651            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4652            push @$active_formatting_elements, $self->{open_elements}->[-1];
4653            
4654            !!!next-token;
4655            redo B;
4656          } elsif ($token->{tag_name} eq 'nobr') {
4657            $reconstruct_active_formatting_elements->($insert_to_current);
4658    
4659            ## has a |nobr| element in scope
4660            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4661              my $node = $self->{open_elements}->[$_];
4662              if ($node->[1] eq 'nobr') {
4663                !!!parse-error (type => 'in nobr:nobr');
4664                !!!back-token;
4665                $token = {type => END_TAG_TOKEN, tag_name => 'nobr'};
4666                redo B;
4667              } elsif ({
4668                        table => 1, caption => 1, td => 1, th => 1,
4669                        button => 1, marquee => 1, object => 1, html => 1,
4670                       }->{$node->[1]}) {
4671                last INSCOPE;
4672              }
4673            } # INSCOPE
4674            
4675            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4676            push @$active_formatting_elements, $self->{open_elements}->[-1];
4677            
4678            !!!next-token;
4679            redo B;
4680          } elsif ($token->{tag_name} eq 'button') {
4681            ## has a button element in scope
4682            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4683              my $node = $self->{open_elements}->[$_];
4684              if ($node->[1] eq 'button') {
4685                !!!parse-error (type => 'in button:button');
4686                !!!back-token;
4687                $token = {type => END_TAG_TOKEN, tag_name => 'button'};
4688                redo B;
4689              } elsif ({
4690                        table => 1, caption => 1, td => 1, th => 1,
4691                        button => 1, marquee => 1, object => 1, html => 1,
4692                       }->{$node->[1]}) {
4693                last INSCOPE;
4694              }
4695            } # INSCOPE
4696              
4697            $reconstruct_active_formatting_elements->($insert_to_current);
4698              
4699            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4700            push @$active_formatting_elements, ['#marker', ''];
4701    
4702            !!!next-token;
4703            redo B;
4704          } elsif ($token->{tag_name} eq 'marquee' or
4705                   $token->{tag_name} eq 'object') {
4706            $reconstruct_active_formatting_elements->($insert_to_current);
4707            
4708            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4709            push @$active_formatting_elements, ['#marker', ''];
4710            
4711            !!!next-token;
4712            redo B;
4713          } elsif ($token->{tag_name} eq 'xmp') {
4714            $reconstruct_active_formatting_elements->($insert_to_current);
4715            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
4716            redo B;
4717          } elsif ($token->{tag_name} eq 'table') {
4718            ## has a p element in scope
4719            INSCOPE: for (reverse @{$self->{open_elements}}) {
4720              if ($_->[1] eq 'p') {
4721                !!!back-token;
4722                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4723                redo B;
4724              } elsif ({
4725                        table => 1, caption => 1, td => 1, th => 1,
4726                        button => 1, marquee => 1, object => 1, html => 1,
4727                       }->{$_->[1]}) {
4728                last INSCOPE;
4729              }
4730            } # INSCOPE
4731                        
4732            $open_elements->[-1]->[0]->manakai_append_text ($data);          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4733              
4734            $self->{insertion_mode} = IN_TABLE_IM;
4735                        
4736            !!!next-token;
4737            redo B;
4738          } elsif ({
4739                    area => 1, basefont => 1, bgsound => 1, br => 1,
4740                    embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
4741                    image => 1,
4742                   }->{$token->{tag_name}}) {
4743            if ($token->{tag_name} eq 'image') {
4744              !!!parse-error (type => 'image');
4745              $token->{tag_name} = 'img';
4746            }
4747    
4748            ## NOTE: There is an "as if <br>" code clone.
4749            $reconstruct_active_formatting_elements->($insert_to_current);
4750            
4751            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4752            pop @{$self->{open_elements}};
4753            
4754            !!!next-token;
4755            redo B;
4756          } elsif ($token->{tag_name} eq 'hr') {
4757            ## has a p element in scope
4758            INSCOPE: for (reverse @{$self->{open_elements}}) {
4759              if ($_->[1] eq 'p') {
4760                !!!back-token;
4761                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4762                redo B;
4763              } elsif ({
4764                        table => 1, caption => 1, td => 1, th => 1,
4765                        button => 1, marquee => 1, object => 1, html => 1,
4766                       }->{$_->[1]}) {
4767                last INSCOPE;
4768              }
4769            } # INSCOPE
4770              
4771            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4772            pop @{$self->{open_elements}};
4773              
4774            !!!next-token;
4775            redo B;
4776          } elsif ($token->{tag_name} eq 'input') {
4777            $reconstruct_active_formatting_elements->($insert_to_current);
4778            
4779            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4780            ## TODO: associate with $self->{form_element} if defined
4781            pop @{$self->{open_elements}};
4782            
4783            !!!next-token;
4784            redo B;
4785          } elsif ($token->{tag_name} eq 'isindex') {
4786            !!!parse-error (type => 'isindex');
4787            
4788            if (defined $self->{form_element}) {
4789              ## Ignore the token
4790              !!!next-token;
4791              redo B;
4792            } else {
4793              my $at = $token->{attributes};
4794              my $form_attrs;
4795              $form_attrs->{action} = $at->{action} if $at->{action};
4796              my $prompt_attr = $at->{prompt};
4797              $at->{name} = {name => 'name', value => 'isindex'};
4798              delete $at->{action};
4799              delete $at->{prompt};
4800              my @tokens = (
4801                            {type => START_TAG_TOKEN, tag_name => 'form',
4802                             attributes => $form_attrs},
4803                            {type => START_TAG_TOKEN, tag_name => 'hr'},
4804                            {type => START_TAG_TOKEN, tag_name => 'p'},
4805                            {type => START_TAG_TOKEN, tag_name => 'label'},
4806                           );
4807              if ($prompt_attr) {
4808                push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value}};
4809              } else {
4810                push @tokens, {type => CHARACTER_TOKEN,
4811                               data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD
4812                ## TODO: make this configurable
4813              }
4814              push @tokens,
4815                            {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at},
4816                            #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
4817                            {type => END_TAG_TOKEN, tag_name => 'label'},
4818                            {type => END_TAG_TOKEN, tag_name => 'p'},
4819                            {type => START_TAG_TOKEN, tag_name => 'hr'},
4820                            {type => END_TAG_TOKEN, tag_name => 'form'};
4821              $token = shift @tokens;
4822              !!!back-token (@tokens);
4823              redo B;
4824            }
4825          } elsif ($token->{tag_name} eq 'textarea') {
4826            my $tag_name = $token->{tag_name};
4827            my $el;
4828            !!!create-element ($el, $token->{tag_name}, $token->{attributes});
4829            
4830            ## TODO: $self->{form_element} if defined
4831            $self->{content_model} = RCDATA_CONTENT_MODEL;
4832            delete $self->{escape}; # MUST
4833            
4834            $insert->($el);
4835            
4836            my $text = '';
4837            !!!next-token;
4838            if ($token->{type} == CHARACTER_TOKEN) {
4839              $token->{data} =~ s/^\x0A//;
4840            unless (length $token->{data}) {            unless (length $token->{data}) {
4841              !!!next-token;              !!!next-token;
             redo B;  
4842            }            }
4843          }          }
4844            while ($token->{type} == CHARACTER_TOKEN) {
4845              $text .= $token->{data};
4846              !!!next-token;
4847            }
4848            if (length $text) {
4849              $el->manakai_append_text ($text);
4850            }
4851            
4852            $self->{content_model} = PCDATA_CONTENT_MODEL;
4853            
4854            if ($token->{type} == END_TAG_TOKEN and
4855                $token->{tag_name} eq $tag_name) {
4856              ## Ignore the token
4857            } else {
4858              !!!parse-error (type => 'in RCDATA:#'.$token->{type});
4859            }
4860            !!!next-token;
4861            redo B;
4862          } elsif ({
4863                    iframe => 1,
4864                    noembed => 1,
4865                    noframes => 1,
4866                    noscript => 0, ## TODO: 1 if scripting is enabled
4867                   }->{$token->{tag_name}}) {
4868            ## NOTE: There is an "as if in body" code clone.
4869            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
4870            redo B;
4871          } elsif ($token->{tag_name} eq 'select') {
4872            $reconstruct_active_formatting_elements->($insert_to_current);
4873            
4874            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4875            
4876            $self->{insertion_mode} = IN_SELECT_IM;
4877            !!!next-token;
4878            redo B;
4879          } elsif ({
4880                    caption => 1, col => 1, colgroup => 1, frame => 1,
4881                    frameset => 1, head => 1, option => 1, optgroup => 1,
4882                    tbody => 1, td => 1, tfoot => 1, th => 1,
4883                    thead => 1, tr => 1,
4884                   }->{$token->{tag_name}}) {
4885            !!!parse-error (type => 'in body:'.$token->{tag_name});
4886            ## Ignore the token
4887            !!!next-token;
4888            redo B;
4889            
4890            ## ISSUE: An issue on HTML5 new elements in the spec.
4891          } else {
4892            $reconstruct_active_formatting_elements->($insert_to_current);
4893            
4894            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4895            
4896            !!!next-token;
4897            redo B;
4898          }
4899        } elsif ($token->{type} == END_TAG_TOKEN) {
4900          if ($token->{tag_name} eq 'body') {
4901            if (@{$self->{open_elements}} > 1 and
4902                $self->{open_elements}->[1]->[1] eq 'body') {
4903              for (@{$self->{open_elements}}) {
4904                unless ({
4905                           dd => 1, dt => 1, li => 1, p => 1, td => 1,
4906                           th => 1, tr => 1, body => 1, html => 1,
4907                         tbody => 1, tfoot => 1, thead => 1,
4908                        }->{$_->[1]}) {
4909                  !!!parse-error (type => 'not closed:'.$_->[1]);
4910                }
4911              }
4912    
4913          !!!parse-error;            $self->{insertion_mode} = AFTER_BODY_IM;
4914          $phase = 'main';            !!!next-token;
4915          ## reprocess            redo B;
4916            } else {
4917              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4918              ## Ignore the token
4919              !!!next-token;
4920              redo B;
4921            }
4922          } elsif ($token->{tag_name} eq 'html') {
4923            if (@{$self->{open_elements}} > 1 and $self->{open_elements}->[1]->[1] eq 'body') {
4924              ## ISSUE: There is an issue in the spec.
4925              if ($self->{open_elements}->[-1]->[1] ne 'body') {
4926                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);
4927              }
4928              $self->{insertion_mode} = AFTER_BODY_IM;
4929              ## reprocess
4930              redo B;
4931            } else {
4932              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4933              ## Ignore the token
4934              !!!next-token;
4935              redo B;
4936            }
4937          } elsif ({
4938                    address => 1, blockquote => 1, center => 1, dir => 1,
4939                    div => 1, dl => 1, fieldset => 1, listing => 1,
4940                    menu => 1, ol => 1, pre => 1, ul => 1,
4941                    p => 1,
4942                    dd => 1, dt => 1, li => 1,
4943                    button => 1, marquee => 1, object => 1,
4944                   }->{$token->{tag_name}}) {
4945            ## has an element in scope
4946            my $i;
4947            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4948              my $node = $self->{open_elements}->[$_];
4949              if ($node->[1] eq $token->{tag_name}) {
4950                ## generate implied end tags
4951                if ({
4952                     dd => ($token->{tag_name} ne 'dd'),
4953                     dt => ($token->{tag_name} ne 'dt'),
4954                     li => ($token->{tag_name} ne 'li'),
4955                     p => ($token->{tag_name} ne 'p'),
4956                     td => 1, th => 1, tr => 1,
4957                     tbody => 1, tfoot=> 1, thead => 1,
4958                    }->{$self->{open_elements}->[-1]->[1]}) {
4959                  !!!back-token;
4960                  $token = {type => END_TAG_TOKEN,
4961                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
4962                  redo B;
4963                }
4964                $i = $_;
4965                last INSCOPE unless $token->{tag_name} eq 'p';
4966              } elsif ({
4967                        table => 1, caption => 1, td => 1, th => 1,
4968                        button => 1, marquee => 1, object => 1, html => 1,
4969                       }->{$node->[1]}) {
4970                last INSCOPE;
4971              }
4972            } # INSCOPE
4973            
4974            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
4975              if (defined $i) {
4976                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4977              } else {
4978                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4979              }
4980            }
4981            
4982            if (defined $i) {
4983              splice @{$self->{open_elements}}, $i;
4984            } elsif ($token->{tag_name} eq 'p') {
4985              ## As if <p>, then reprocess the current token
4986              my $el;
4987              !!!create-element ($el, 'p');
4988              $insert->($el);
4989            }
4990            $clear_up_to_marker->()
4991              if {
4992                button => 1, marquee => 1, object => 1,
4993              }->{$token->{tag_name}};
4994            !!!next-token;
4995          redo B;          redo B;
4996        } elsif ($token->{type} eq 'start tag' or        } elsif ($token->{tag_name} eq 'form') {
4997                 $token->{type} eq 'end tag') {          ## has an element in scope
4998          !!!parse-error;          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4999          $phase = 'main';            my $node = $self->{open_elements}->[$_];
5000          ## reprocess            if ($node->[1] eq $token->{tag_name}) {
5001                ## generate implied end tags
5002                if ({
5003                     dd => 1, dt => 1, li => 1, p => 1,
5004                     td => 1, th => 1, tr => 1,
5005                     tbody => 1, tfoot=> 1, thead => 1,
5006                    }->{$self->{open_elements}->[-1]->[1]}) {
5007                  !!!back-token;
5008                  $token = {type => END_TAG_TOKEN,
5009                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5010                  redo B;
5011                }
5012                last INSCOPE;
5013              } elsif ({
5014                        table => 1, caption => 1, td => 1, th => 1,
5015                        button => 1, marquee => 1, object => 1, html => 1,
5016                       }->{$node->[1]}) {
5017                last INSCOPE;
5018              }
5019            } # INSCOPE
5020            
5021            if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {
5022              pop @{$self->{open_elements}};
5023            } else {
5024              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5025            }
5026    
5027            undef $self->{form_element};
5028            !!!next-token;
5029            redo B;
5030          } elsif ({
5031                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
5032                   }->{$token->{tag_name}}) {
5033            ## has an element in scope
5034            my $i;
5035            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5036              my $node = $self->{open_elements}->[$_];
5037              if ({
5038                   h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
5039                  }->{$node->[1]}) {
5040                ## generate implied end tags
5041                if ({
5042                     dd => 1, dt => 1, li => 1, p => 1,
5043                     td => 1, th => 1, tr => 1,
5044                     tbody => 1, tfoot=> 1, thead => 1,
5045                    }->{$self->{open_elements}->[-1]->[1]}) {
5046                  !!!back-token;
5047                  $token = {type => END_TAG_TOKEN,
5048                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5049                  redo B;
5050                }
5051                $i = $_;
5052                last INSCOPE;
5053              } elsif ({
5054                        table => 1, caption => 1, td => 1, th => 1,
5055                        button => 1, marquee => 1, object => 1, html => 1,
5056                       }->{$node->[1]}) {
5057                last INSCOPE;
5058              }
5059            } # INSCOPE
5060            
5061            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
5062              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5063            }
5064            
5065            splice @{$self->{open_elements}}, $i if defined $i;
5066            !!!next-token;
5067            redo B;
5068          } elsif ({
5069                    a => 1,
5070                    b => 1, big => 1, em => 1, font => 1, i => 1,
5071                    nobr => 1, s => 1, small => 1, strile => 1,
5072                    strong => 1, tt => 1, u => 1,
5073                   }->{$token->{tag_name}}) {
5074            $formatting_end_tag->($token->{tag_name});
5075            redo B;
5076          } elsif ($token->{tag_name} eq 'br') {
5077            !!!parse-error (type => 'unmatched end tag:br');
5078    
5079            ## As if <br>
5080            $reconstruct_active_formatting_elements->($insert_to_current);
5081            
5082            my $el;
5083            !!!create-element ($el, 'br');
5084            $insert->($el);
5085            
5086            ## Ignore the token.
5087            !!!next-token;
5088            redo B;
5089          } elsif ({
5090                    caption => 1, col => 1, colgroup => 1, frame => 1,
5091                    frameset => 1, head => 1, option => 1, optgroup => 1,
5092                    tbody => 1, td => 1, tfoot => 1, th => 1,
5093                    thead => 1, tr => 1,
5094                    area => 1, basefont => 1, bgsound => 1,
5095                    embed => 1, hr => 1, iframe => 1, image => 1,
5096                    img => 1, input => 1, isindex => 1, noembed => 1,
5097                    noframes => 1, param => 1, select => 1, spacer => 1,
5098                    table => 1, textarea => 1, wbr => 1,
5099                    noscript => 0, ## TODO: if scripting is enabled
5100                   }->{$token->{tag_name}}) {
5101            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5102            ## Ignore the token
5103            !!!next-token;
5104          redo B;          redo B;
5105        } elsif ($token->{type} eq 'end-of-file') {          
5106          ## Stop parsing          ## ISSUE: Issue on HTML5 new elements in spec
5107          last B;          
5108        } else {        } else {
5109          die "$0: $token->{type}: Unknown token";          ## Step 1
5110            my $node_i = -1;
5111            my $node = $self->{open_elements}->[$node_i];
5112    
5113            ## Step 2
5114            S2: {
5115              if ($node->[1] eq $token->{tag_name}) {
5116                ## Step 1
5117                ## generate implied end tags
5118                if ({
5119                     dd => 1, dt => 1, li => 1, p => 1,
5120                     td => 1, th => 1, tr => 1,
5121                     tbody => 1, tfoot => 1, thead => 1,
5122                    }->{$self->{open_elements}->[-1]->[1]}) {
5123                  !!!back-token;
5124                  $token = {type => END_TAG_TOKEN,
5125                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5126                  redo B;
5127                }
5128            
5129                ## Step 2
5130                if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {
5131                  ## NOTE: <x><y></x>
5132                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
5133                }
5134                
5135                ## Step 3
5136                splice @{$self->{open_elements}}, $node_i;
5137    
5138                !!!next-token;
5139                last S2;
5140              } else {
5141                ## Step 3
5142                if (not $formatting_category->{$node->[1]} and
5143                    #not $phrasing_category->{$node->[1]} and
5144                    ($special_category->{$node->[1]} or
5145                     $scoping_category->{$node->[1]})) {
5146                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5147                  ## Ignore the token
5148                  !!!next-token;
5149                  last S2;
5150                }
5151              }
5152              
5153              ## Step 4
5154              $node_i--;
5155              $node = $self->{open_elements}->[$node_i];
5156              
5157              ## Step 5;
5158              redo S2;
5159            } # S2
5160            redo B;
5161        }        }
5162      }      }
5163        redo B;
5164    } # B    } # B
5165    
5166      ## NOTE: The "trailing end" phase in HTML5 is split into
5167      ## two insertion modes: "after html body" and "after html frameset".
5168      ## NOTE: States in the main stage is preserved while
5169      ## the parser stays in the trailing end phase. # MUST
5170    
5171    ## Stop parsing # MUST    ## Stop parsing # MUST
5172        
5173    ## TODO: script stuffs    ## TODO: script stuffs
5174  } # _construct_tree  } # _tree_construct_main
5175    
5176    sub set_inner_html ($$$) {
5177      my $class = shift;
5178      my $node = shift;
5179      my $s = \$_[0];
5180      my $onerror = $_[1];
5181    
5182      my $nt = $node->node_type;
5183      if ($nt == 9) {
5184        # MUST
5185        
5186        ## Step 1 # MUST
5187        ## TODO: If the document has an active parser, ...
5188        ## ISSUE: There is an issue in the spec.
5189        
5190        ## Step 2 # MUST
5191        my @cn = @{$node->child_nodes};
5192        for (@cn) {
5193          $node->remove_child ($_);
5194        }
5195    
5196        ## Step 3, 4, 5 # MUST
5197        $class->parse_string ($$s => $node, $onerror);
5198      } elsif ($nt == 1) {
5199        ## TODO: If non-html element
5200    
5201        ## NOTE: Most of this code is copied from |parse_string|
5202    
5203        ## Step 1 # MUST
5204        my $this_doc = $node->owner_document;
5205        my $doc = $this_doc->implementation->create_document;
5206        $doc->manakai_is_html (1);
5207        my $p = $class->new;
5208        $p->{document} = $doc;
5209    
5210        ## Step 9 # MUST
5211        my $i = 0;
5212        my $line = 1;
5213        my $column = 0;
5214        $p->{set_next_input_character} = sub {
5215          my $self = shift;
5216    
5217          pop @{$self->{prev_input_character}};
5218          unshift @{$self->{prev_input_character}}, $self->{next_input_character};
5219    
5220          $self->{next_input_character} = -1 and return if $i >= length $$s;
5221          $self->{next_input_character} = ord substr $$s, $i++, 1;
5222          $column++;
5223    
5224          if ($self->{next_input_character} == 0x000A) { # LF
5225            $line++;
5226            $column = 0;
5227          } elsif ($self->{next_input_character} == 0x000D) { # CR
5228            $i++ if substr ($$s, $i, 1) eq "\x0A";
5229            $self->{next_input_character} = 0x000A; # LF # MUST
5230            $line++;
5231            $column = 0;
5232          } elsif ($self->{next_input_character} > 0x10FFFF) {
5233            $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
5234          } elsif ($self->{next_input_character} == 0x0000) { # NULL
5235            !!!parse-error (type => 'NULL');
5236            $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
5237          }
5238        };
5239        $p->{prev_input_character} = [-1, -1, -1];
5240        $p->{next_input_character} = -1;
5241        
5242        my $ponerror = $onerror || sub {
5243          my (%opt) = @_;
5244          warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";
5245        };
5246        $p->{parse_error} = sub {
5247          $ponerror->(@_, line => $line, column => $column);
5248        };
5249        
5250        $p->_initialize_tokenizer;
5251        $p->_initialize_tree_constructor;
5252    
5253        ## Step 2
5254        my $node_ln = $node->local_name;
5255        $p->{content_model} = {
5256          title => RCDATA_CONTENT_MODEL,
5257          textarea => RCDATA_CONTENT_MODEL,
5258          style => CDATA_CONTENT_MODEL,
5259          script => CDATA_CONTENT_MODEL,
5260          xmp => CDATA_CONTENT_MODEL,
5261          iframe => CDATA_CONTENT_MODEL,
5262          noembed => CDATA_CONTENT_MODEL,
5263          noframes => CDATA_CONTENT_MODEL,
5264          noscript => CDATA_CONTENT_MODEL,
5265          plaintext => PLAINTEXT_CONTENT_MODEL,
5266        }->{$node_ln};
5267        $p->{content_model} = PCDATA_CONTENT_MODEL
5268            unless defined $p->{content_model};
5269            ## ISSUE: What is "the name of the element"? local name?
5270    
5271        $p->{inner_html_node} = [$node, $node_ln];
5272    
5273        ## Step 4
5274        my $root = $doc->create_element_ns
5275          ('http://www.w3.org/1999/xhtml', [undef, 'html']);
5276    
5277        ## Step 5 # MUST
5278        $doc->append_child ($root);
5279    
5280        ## Step 6 # MUST
5281        push @{$p->{open_elements}}, [$root, 'html'];
5282    
5283        undef $p->{head_element};
5284    
5285        ## Step 7 # MUST
5286        $p->_reset_insertion_mode;
5287    
5288        ## Step 8 # MUST
5289        my $anode = $node;
5290        AN: while (defined $anode) {
5291          if ($anode->node_type == 1) {
5292            my $nsuri = $anode->namespace_uri;
5293            if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {
5294              if ($anode->local_name eq 'form') { ## TODO: case?
5295                $p->{form_element} = $anode;
5296                last AN;
5297              }
5298            }
5299          }
5300          $anode = $anode->parent_node;
5301        } # AN
5302        
5303        ## Step 3 # MUST
5304        ## Step 10 # MUST
5305        {
5306          my $self = $p;
5307          !!!next-token;
5308        }
5309        $p->_tree_construction_main;
5310    
5311        ## Step 11 # MUST
5312        my @cn = @{$node->child_nodes};
5313        for (@cn) {
5314          $node->remove_child ($_);
5315        }
5316        ## ISSUE: mutation events? read-only?
5317    
5318        ## Step 12 # MUST
5319        @cn = @{$root->child_nodes};
5320        for (@cn) {
5321          $this_doc->adopt_node ($_);
5322          $node->append_child ($_);
5323        }
5324        ## ISSUE: mutation events?
5325    
5326        $p->_terminate_tree_constructor;
5327      } else {
5328        die "$0: |set_inner_html| is not defined for node of type $nt";
5329      }
5330    } # set_inner_html
5331    
5332    } # tree construction stage
5333    
5334  sub get_inner_html ($$$) {  sub get_inner_html ($$$) {
5335    my ($class, $node, $on_error) = @_;    my (undef, $node, $on_error) = @_;
5336    
5337    ## Step 1    ## Step 1
5338    my $s = '';    my $s = '';
# Line 4718  sub get_inner_html ($$$) { Line 5366  sub get_inner_html ($$$) {
5366            
5367      my $nt = $child->node_type;      my $nt = $child->node_type;
5368      if ($nt == 1) { # Element      if ($nt == 1) { # Element
5369        my $tag_name = lc $child->tag_name; ## ISSUE: Definition of "lowercase"        my $tag_name = $child->tag_name; ## TODO: manakai_tag_name
5370        $s .= '<' . $tag_name;        $s .= '<' . $tag_name;
5371          ## NOTE: Non-HTML case:
5372        ## ISSUE: Non-html elements        ## <http://permalink.gmane.org/gmane.org.w3c.whatwg.discuss/11191>
5373    
5374        my @attrs = @{$child->attributes}; # sort order MUST be stable        my @attrs = @{$child->attributes}; # sort order MUST be stable
5375        for my $attr (@attrs) { # order is implementation dependent        for my $attr (@attrs) { # order is implementation dependent
5376          my $attr_name = lc $attr->name; ## ISSUE: Definition of "lowercase"          my $attr_name = $attr->name; ## TODO: manakai_name
5377          $s .= ' ' . $attr_name . '="';          $s .= ' ' . $attr_name . '="';
5378          my $attr_value = $attr->value;          my $attr_value = $attr->value;
5379          ## escape          ## escape
# Line 4744  sub get_inner_html ($$$) { Line 5392  sub get_inner_html ($$$) {
5392          spacer => 1, wbr => 1,          spacer => 1, wbr => 1,
5393        }->{$tag_name};        }->{$tag_name};
5394    
5395          $s .= "\x0A" if $tag_name eq 'pre' or $tag_name eq 'textarea';
5396    
5397        if (not $in_cdata and {        if (not $in_cdata and {
5398          style => 1, script => 1, xmp => 1, iframe => 1,          style => 1, script => 1, xmp => 1, iframe => 1,
5399          noembed => 1, noframes => 1, noscript => 1,          noembed => 1, noframes => 1, noscript => 1,
5400            plaintext => 1,
5401        }->{$tag_name}) {        }->{$tag_name}) {
5402          unshift @node, 'cdata-out';          unshift @node, 'cdata-out';
5403          $in_cdata = 1;          $in_cdata = 1;

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24