/[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.7 by wakaba, Wed May 30 12:24:50 2007 UTC revision 1.46 by wakaba, Sat Jul 21 08:52:59 2007 UTC
# Line 2  package Whatpm::HTML; Line 2  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    
 my $entity_char = {  
   AElig => "\x{00C6}",  
   Aacute => "\x{00C1}",  
   Acirc => "\x{00C2}",  
   Agrave => "\x{00C0}",  
   Alpha => "\x{0391}",  
   Aring => "\x{00C5}",  
   Atilde => "\x{00C3}",  
   Auml => "\x{00C4}",  
   Beta => "\x{0392}",  
   Ccedil => "\x{00C7}",  
   Chi => "\x{03A7}",  
   Dagger => "\x{2021}",  
   Delta => "\x{0394}",  
   ETH => "\x{00D0}",  
   Eacute => "\x{00C9}",  
   Ecirc => "\x{00CA}",  
   Egrave => "\x{00C8}",  
   Epsilon => "\x{0395}",  
   Eta => "\x{0397}",  
   Euml => "\x{00CB}",  
   Gamma => "\x{0393}",  
   Iacute => "\x{00CD}",  
   Icirc => "\x{00CE}",  
   Igrave => "\x{00CC}",  
   Iota => "\x{0399}",  
   Iuml => "\x{00CF}",  
   Kappa => "\x{039A}",  
   Lambda => "\x{039B}",  
   Mu => "\x{039C}",  
   Ntilde => "\x{00D1}",  
   Nu => "\x{039D}",  
   OElig => "\x{0152}",  
   Oacute => "\x{00D3}",  
   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}",  
 }; # $entity_char  
   
 ## <http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2006-December/thread.html#8562>  
30  my $c1_entity_char = {  my $c1_entity_char = {
31       128, 8364,    0x80 => 0x20AC,
32       129, 65533,    0x81 => 0xFFFD,
33       130, 8218,    0x82 => 0x201A,
34       131, 402,    0x83 => 0x0192,
35       132, 8222,    0x84 => 0x201E,
36       133, 8230,    0x85 => 0x2026,
37       134, 8224,    0x86 => 0x2020,
38       135, 8225,    0x87 => 0x2021,
39       136, 710,    0x88 => 0x02C6,
40       137, 8240,    0x89 => 0x2030,
41       138, 352,    0x8A => 0x0160,
42       139, 8249,    0x8B => 0x2039,
43       140, 338,    0x8C => 0x0152,
44       141, 65533,    0x8D => 0xFFFD,
45       142, 381,    0x8E => 0x017D,
46       143, 65533,    0x8F => 0xFFFD,
47       144, 65533,    0x90 => 0xFFFD,
48       145, 8216,    0x91 => 0x2018,
49       146, 8217,    0x92 => 0x2019,
50       147, 8220,    0x93 => 0x201C,
51       148, 8221,    0x94 => 0x201D,
52       149, 8226,    0x95 => 0x2022,
53       150, 8211,    0x96 => 0x2013,
54       151, 8212,    0x97 => 0x2014,
55       152, 732,    0x98 => 0x02DC,
56       153, 8482,    0x99 => 0x2122,
57       154, 353,    0x9A => 0x0161,
58       155, 8250,    0x9B => 0x203A,
59       156, 339,    0x9C => 0x0153,
60       157, 65533,    0x9D => 0xFFFD,
61       158, 382,    0x9E => 0x017E,
62       159, 376,    0x9F => 0x0178,
63  }; # $c1_entity_char  }; # $c1_entity_char
64    
65  my $special_category = {  my $special_category = {
# Line 350  sub parse_string ($$$;$) { Line 96  sub parse_string ($$$;$) {
96    my $column = 0;    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++;      $column++;
# Line 358  sub parse_string ($$$;$) { Line 108  sub parse_string ($$$;$) {
108        $line++;        $line++;
109        $column = 0;        $column = 0;
110      } elsif ($self->{next_input_character} == 0x000D) { # CR      } elsif ($self->{next_input_character} == 0x000D) { # CR
111        if ($i >= length $$s) {        $i++ if substr ($$s, $i, 1) eq "\x0A";
         #  
       } else {  
         my $next_char = ord substr $$s, $i++, 1;  
         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++;        $line++;
114        $column = 0;        $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    my $onerror = $_[2] || sub {    my $onerror = $_[2] || sub {
126      my (%opt) = @_;      my (%opt) = @_;
# Line 406  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  ## Implementations MUST act as if state machine in the spec  ## Implementations MUST act as if state machine in the spec
163    
164  sub _initialize_tokenizer ($) {  sub _initialize_tokenizer ($) {
165    my $self = shift;    my $self = shift;
166    $self->{state} = 'data'; # MUST    $self->{state} = 'data'; # MUST
167    $self->{content_model_flag} = 'PCDATA'; # be    $self->{content_model} = PCDATA_CONTENT_MODEL; # be
168    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE
169    undef $self->{current_attribute};    undef $self->{current_attribute};
170    undef $self->{last_emitted_start_tag_name};    undef $self->{last_emitted_start_tag_name};
# Line 420  sub _initialize_tokenizer ($) { Line 173  sub _initialize_tokenizer ($) {
173    # $self->{next_input_character}    # $self->{next_input_character}
174    !!!next-input-character;    !!!next-input-character;
175    $self->{token} = [];    $self->{token} = [];
176      # $self->{escape}
177  } # _initialize_tokenizer  } # _initialize_tokenizer
178    
179  ## A token has:  ## A token has:
180  ##   ->{type} eq 'DOCTYPE', 'start tag', 'end tag', 'comment',  ##   ->{type} eq 'DOCTYPE', 'start tag', 'end tag', 'comment',
181  ##       'character', or 'end-of-file'  ##       'character', or 'end-of-file'
182  ##   ->{name} (DOCTYPE, start tag (tagname), end tag (tagname))  ##   ->{name} (DOCTYPE, start tag (tag name), end tag (tag name))
183      ## ISSUE: the spec need s/tagname/tag name/  ##   ->{public_identifier} (DOCTYPE)
184  ##   ->{error} == 1 or 0 (DOCTYPE)  ##   ->{system_identifier} (DOCTYPE)
185    ##   ->{correct} == 1 or 0 (DOCTYPE)
186  ##   ->{attributes} isa HASH (start tag, end tag)  ##   ->{attributes} isa HASH (start tag, end tag)
187  ##   ->{data} (comment, character)  ##   ->{data} (comment, character)
188    
 ## Macros  
 ##   Macros MUST be preceded by three EXCLAMATION MARKs.  
 ##   emit ($token)  
 ##     Emits the specified token.  
   
189  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
190    
191  ## Before each step, UA MAY check to see if either one of the scripts in  ## Before each step, UA MAY check to see if either one of the scripts in
# Line 453  sub _get_next_token ($) { Line 203  sub _get_next_token ($) {
203    A: {    A: {
204      if ($self->{state} eq 'data') {      if ($self->{state} eq 'data') {
205        if ($self->{next_input_character} == 0x0026) { # &        if ($self->{next_input_character} == 0x0026) { # &
206          if ($self->{content_model_flag} eq 'PCDATA' or          if ($self->{content_model} & CM_ENTITY) { # PCDATA | RCDATA
             $self->{content_model_flag} eq 'RCDATA') {  
207            $self->{state} = 'entity data';            $self->{state} = 'entity data';
208            !!!next-input-character;            !!!next-input-character;
209            redo A;            redo A;
210          } else {          } else {
211            #            #
212          }          }
213          } elsif ($self->{next_input_character} == 0x002D) { # -
214            if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
215              unless ($self->{escape}) {
216                if ($self->{prev_input_character}->[0] == 0x002D and # -
217                    $self->{prev_input_character}->[1] == 0x0021 and # !
218                    $self->{prev_input_character}->[2] == 0x003C) { # <
219                  $self->{escape} = 1;
220                }
221              }
222            }
223            
224            #
225        } elsif ($self->{next_input_character} == 0x003C) { # <        } elsif ($self->{next_input_character} == 0x003C) { # <
226          if ($self->{content_model_flag} ne 'PLAINTEXT') {          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA
227                (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA
228                 not $self->{escape})) {
229            $self->{state} = 'tag open';            $self->{state} = 'tag open';
230            !!!next-input-character;            !!!next-input-character;
231            redo A;            redo A;
232          } else {          } else {
233            #            #
234          }          }
235          } elsif ($self->{next_input_character} == 0x003E) { # >
236            if ($self->{escape} and
237                ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA
238              if ($self->{prev_input_character}->[0] == 0x002D and # -
239                  $self->{prev_input_character}->[1] == 0x002D) { # -
240                delete $self->{escape};
241              }
242            }
243            
244            #
245        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
246          !!!emit ({type => 'end-of-file'});          !!!emit ({type => 'end-of-file'});
247          last A; ## TODO: ok?          last A; ## TODO: ok?
# Line 485  sub _get_next_token ($) { Line 258  sub _get_next_token ($) {
258      } elsif ($self->{state} eq 'entity data') {      } elsif ($self->{state} eq 'entity data') {
259        ## (cannot happen in CDATA state)        ## (cannot happen in CDATA state)
260                
261        my $token = $self->_tokenize_attempt_to_consume_an_entity;        my $token = $self->_tokenize_attempt_to_consume_an_entity (0);
262    
263        $self->{state} = 'data';        $self->{state} = 'data';
264        # next-input-character is already done        # next-input-character is already done
# Line 498  sub _get_next_token ($) { Line 271  sub _get_next_token ($) {
271    
272        redo A;        redo A;
273      } elsif ($self->{state} eq 'tag open') {      } elsif ($self->{state} eq 'tag open') {
274        if ($self->{content_model_flag} eq 'RCDATA' or        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
           $self->{content_model_flag} eq 'CDATA') {  
275          if ($self->{next_input_character} == 0x002F) { # /          if ($self->{next_input_character} == 0x002F) { # /
276            !!!next-input-character;            !!!next-input-character;
277            $self->{state} = 'close tag open';            $self->{state} = 'close tag open';
# Line 512  sub _get_next_token ($) { Line 284  sub _get_next_token ($) {
284    
285            redo A;            redo A;
286          }          }
287        } elsif ($self->{content_model_flag} eq 'PCDATA') {        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA
288          if ($self->{next_input_character} == 0x0021) { # !          if ($self->{next_input_character} == 0x0021) { # !
289            $self->{state} = 'markup declaration open';            $self->{state} = 'markup declaration open';
290            !!!next-input-character;            !!!next-input-character;
# Line 559  sub _get_next_token ($) { Line 331  sub _get_next_token ($) {
331            redo A;            redo A;
332          }          }
333        } else {        } else {
334          die "$0: $self->{content_model_flag}: Unknown content model flag";          die "$0: $self->{content_model} in tag open";
335        }        }
336      } elsif ($self->{state} eq 'close tag open') {      } elsif ($self->{state} eq 'close tag open') {
337        if ($self->{content_model_flag} eq 'RCDATA' or        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
338            $self->{content_model_flag} eq 'CDATA') {          if (defined $self->{last_emitted_start_tag_name}) {
339          my @next_char;            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>
340          TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {            my @next_char;
341              TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {
342                push @next_char, $self->{next_input_character};
343                my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);
344                my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;
345                if ($self->{next_input_character} == $c or $self->{next_input_character} == $C) {
346                  !!!next-input-character;
347                  next TAGNAME;
348                } else {
349                  $self->{next_input_character} = shift @next_char; # reconsume
350                  !!!back-next-input-character (@next_char);
351                  $self->{state} = 'data';
352    
353                  !!!emit ({type => 'character', data => '</'});
354      
355                  redo A;
356                }
357              }
358            push @next_char, $self->{next_input_character};            push @next_char, $self->{next_input_character};
359            my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);        
360            my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;            unless ($self->{next_input_character} == 0x0009 or # HT
361            if ($self->{next_input_character} == $c or $self->{next_input_character} == $C) {                    $self->{next_input_character} == 0x000A or # LF
362              !!!next-input-character;                    $self->{next_input_character} == 0x000B or # VT
363              next TAGNAME;                    $self->{next_input_character} == 0x000C or # FF
364            } else {                    $self->{next_input_character} == 0x0020 or # SP
365              !!!parse-error (type => 'unmatched end tag');                    $self->{next_input_character} == 0x003E or # >
366                      $self->{next_input_character} == 0x002F or # /
367                      $self->{next_input_character} == -1) {
368              $self->{next_input_character} = shift @next_char; # reconsume              $self->{next_input_character} = shift @next_char; # reconsume
369              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
370              $self->{state} = 'data';              $self->{state} = 'data';
   
371              !!!emit ({type => 'character', data => '</'});              !!!emit ({type => 'character', data => '</'});
   
372              redo A;              redo A;
373              } else {
374                $self->{next_input_character} = shift @next_char;
375                !!!back-next-input-character (@next_char);
376                # and consume...
377            }            }
378          }          } else {
379          push @next_char, $self->{next_input_character};            ## No start tag token has ever been emitted
380                  # next-input-character is already done
         unless ($self->{next_input_character} == 0x0009 or # HT  
                 $self->{next_input_character} == 0x000A or # LF  
                 $self->{next_input_character} == 0x000B or # VT  
                 $self->{next_input_character} == 0x000C or # FF  
                 $self->{next_input_character} == 0x0020 or # SP  
                 $self->{next_input_character} == 0x003E or # >  
                 $self->{next_input_character} == 0x002F or # /  
                 $self->{next_input_character} == 0x003C or # <  
                 $self->{next_input_character} == -1) {  
           !!!parse-error (type => 'unmatched end tag');  
           $self->{next_input_character} = shift @next_char; # reconsume  
           !!!back-next-input-character (@next_char);  
381            $self->{state} = 'data';            $self->{state} = 'data';
   
382            !!!emit ({type => 'character', data => '</'});            !!!emit ({type => 'character', data => '</'});
   
383            redo A;            redo A;
         } else {  
           $self->{next_input_character} = shift @next_char;  
           !!!back-next-input-character (@next_char);  
           # and consume...  
384          }          }
385        }        }
386                
# Line 653  sub _get_next_token ($) { Line 428  sub _get_next_token ($) {
428          redo A;          redo A;
429        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
430          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
431              $self->{current_token}->{first_start_tag}
432                  = not defined $self->{last_emitted_start_tag_name};
433            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
434          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
435            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
436            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
437              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
438            }            }
# Line 666  sub _get_next_token ($) { Line 443  sub _get_next_token ($) {
443          !!!next-input-character;          !!!next-input-character;
444    
445          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
446    
447          redo A;          redo A;
448        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_input_character} and
# Line 676  sub _get_next_token ($) { Line 452  sub _get_next_token ($) {
452          ## Stay in this state          ## Stay in this state
453          !!!next-input-character;          !!!next-input-character;
454          redo A;          redo A;
455        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_input_character} == -1) {
                $self->{next_input_character} == -1) {  
456          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
457          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
458              $self->{current_token}->{first_start_tag}
459                  = not defined $self->{last_emitted_start_tag_name};
460            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
461          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
462            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
463            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
464              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
465            }            }
# Line 693  sub _get_next_token ($) { Line 470  sub _get_next_token ($) {
470          # reconsume          # reconsume
471    
472          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
473    
474          redo A;          redo A;
475        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_input_character} == 0x002F) { # /
# Line 727  sub _get_next_token ($) { Line 503  sub _get_next_token ($) {
503          redo A;          redo A;
504        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
505          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
506              $self->{current_token}->{first_start_tag}
507                  = not defined $self->{last_emitted_start_tag_name};
508            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
509          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
510            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
511            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
512              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
513            }            }
# Line 740  sub _get_next_token ($) { Line 518  sub _get_next_token ($) {
518          !!!next-input-character;          !!!next-input-character;
519    
520          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
521    
522          redo A;          redo A;
523        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_input_character} and
# Line 763  sub _get_next_token ($) { Line 540  sub _get_next_token ($) {
540          ## Stay in the state          ## Stay in the state
541          # next-input-character is already done          # next-input-character is already done
542          redo A;          redo A;
543        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_input_character} == -1) {
                $self->{next_input_character} == -1) {  
544          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
545          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
546              $self->{current_token}->{first_start_tag}
547                  = not defined $self->{last_emitted_start_tag_name};
548            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
549          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
550            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
551            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
552              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
553            }            }
# Line 780  sub _get_next_token ($) { Line 558  sub _get_next_token ($) {
558          # reconsume          # reconsume
559    
560          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
561    
562          redo A;          redo A;
563        } else {        } else {
# Line 794  sub _get_next_token ($) { Line 571  sub _get_next_token ($) {
571        my $before_leave = sub {        my $before_leave = sub {
572          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
573              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
574            !!!parse-error (type => 'dupulicate attribute');            !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name});
575            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
576          } else {          } else {
577            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}
# Line 819  sub _get_next_token ($) { Line 596  sub _get_next_token ($) {
596        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
597          $before_leave->();          $before_leave->();
598          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
599              $self->{current_token}->{first_start_tag}
600                  = not defined $self->{last_emitted_start_tag_name};
601            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
602          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
603            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
604            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
605              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
606            }            }
# Line 832  sub _get_next_token ($) { Line 611  sub _get_next_token ($) {
611          !!!next-input-character;          !!!next-input-character;
612    
613          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
614    
615          redo A;          redo A;
616        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_input_character} and
# Line 855  sub _get_next_token ($) { Line 633  sub _get_next_token ($) {
633          $self->{state} = 'before attribute name';          $self->{state} = 'before attribute name';
634          # next-input-character is already done          # next-input-character is already done
635          redo A;          redo A;
636        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_input_character} == -1) {
                $self->{next_input_character} == -1) {  
637          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
638          $before_leave->();          $before_leave->();
639          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
640              $self->{current_token}->{first_start_tag}
641                  = not defined $self->{last_emitted_start_tag_name};
642            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
643          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
644            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
645            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
646              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
647            }            }
# Line 873  sub _get_next_token ($) { Line 652  sub _get_next_token ($) {
652          # reconsume          # reconsume
653    
654          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
655    
656          redo A;          redo A;
657        } else {        } else {
# Line 897  sub _get_next_token ($) { Line 675  sub _get_next_token ($) {
675          redo A;          redo A;
676        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
677          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
678              $self->{current_token}->{first_start_tag}
679                  = not defined $self->{last_emitted_start_tag_name};
680            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
681          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
682            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
683            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
684              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
685            }            }
# Line 910  sub _get_next_token ($) { Line 690  sub _get_next_token ($) {
690          !!!next-input-character;          !!!next-input-character;
691    
692          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
693    
694          redo A;          redo A;
695        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_input_character} and
# Line 929  sub _get_next_token ($) { Line 708  sub _get_next_token ($) {
708            #            #
709          } else {          } else {
710            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
711              ## TODO: Different error type for <aa / bb> than <aa/>
712          }          }
713          $self->{state} = 'before attribute name';          $self->{state} = 'before attribute name';
714          # next-input-character is already done          # next-input-character is already done
715          redo A;          redo A;
716        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_input_character} == -1) {
                $self->{next_input_character} == -1) {  
717          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
718          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
719              $self->{current_token}->{first_start_tag}
720                  = not defined $self->{last_emitted_start_tag_name};
721            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
722          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
723            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
724            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
725              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
726            }            }
# Line 950  sub _get_next_token ($) { Line 731  sub _get_next_token ($) {
731          # reconsume          # reconsume
732    
733          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
734    
735          redo A;          redo A;
736        } else {        } else {
# Line 983  sub _get_next_token ($) { Line 763  sub _get_next_token ($) {
763          redo A;          redo A;
764        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
765          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
766              $self->{current_token}->{first_start_tag}
767                  = not defined $self->{last_emitted_start_tag_name};
768            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
769          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
770            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
771            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
772              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
773            }            }
# Line 996  sub _get_next_token ($) { Line 778  sub _get_next_token ($) {
778          !!!next-input-character;          !!!next-input-character;
779    
780          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
781    
782          redo A;          redo A;
783        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_input_character} == -1) {
                $self->{next_input_character} == -1) {  
784          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
785          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
786              $self->{current_token}->{first_start_tag}
787                  = not defined $self->{last_emitted_start_tag_name};
788            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
789          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
790            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
791            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
792              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
793            }            }
# Line 1016  sub _get_next_token ($) { Line 798  sub _get_next_token ($) {
798          ## reconsume          ## reconsume
799    
800          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
801    
802          redo A;          redo A;
803        } else {        } else {
# Line 1038  sub _get_next_token ($) { Line 819  sub _get_next_token ($) {
819        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
820          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
821          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
822              $self->{current_token}->{first_start_tag}
823                  = not defined $self->{last_emitted_start_tag_name};
824            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
825          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
826            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
827            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
828              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
829            }            }
# Line 1051  sub _get_next_token ($) { Line 834  sub _get_next_token ($) {
834          ## reconsume          ## reconsume
835    
836          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
837    
838          redo A;          redo A;
839        } else {        } else {
# Line 1073  sub _get_next_token ($) { Line 855  sub _get_next_token ($) {
855        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
856          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
857          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
858              $self->{current_token}->{first_start_tag}
859                  = not defined $self->{last_emitted_start_tag_name};
860            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
861          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
862            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
863            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
864              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
865            }            }
# Line 1086  sub _get_next_token ($) { Line 870  sub _get_next_token ($) {
870          ## reconsume          ## reconsume
871    
872          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
873    
874          redo A;          redo A;
875        } else {        } else {
# Line 1111  sub _get_next_token ($) { Line 894  sub _get_next_token ($) {
894          redo A;          redo A;
895        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
896          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
897              $self->{current_token}->{first_start_tag}
898                  = not defined $self->{last_emitted_start_tag_name};
899            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
900          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
901            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
902            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
903              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
904            }            }
# Line 1124  sub _get_next_token ($) { Line 909  sub _get_next_token ($) {
909          !!!next-input-character;          !!!next-input-character;
910    
911          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
912    
913          redo A;          redo A;
914        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_input_character} == -1) {
                $self->{next_input_character} == -1) {  
915          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
916          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
917              $self->{current_token}->{first_start_tag}
918                  = not defined $self->{last_emitted_start_tag_name};
919            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
920          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
921            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
922            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
923              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
924            }            }
# Line 1144  sub _get_next_token ($) { Line 929  sub _get_next_token ($) {
929          ## reconsume          ## reconsume
930    
931          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
932    
933          redo A;          redo A;
934        } else {        } else {
# Line 1154  sub _get_next_token ($) { Line 938  sub _get_next_token ($) {
938          redo A;          redo A;
939        }        }
940      } elsif ($self->{state} eq 'entity in attribute value') {      } elsif ($self->{state} eq 'entity in attribute value') {
941        my $token = $self->_tokenize_attempt_to_consume_an_entity;        my $token = $self->_tokenize_attempt_to_consume_an_entity (1);
942    
943        unless (defined $token) {        unless (defined $token) {
944          $self->{current_attribute}->{value} .= '&';          $self->{current_attribute}->{value} .= '&';
# Line 1203  sub _get_next_token ($) { Line 987  sub _get_next_token ($) {
987          push @next_char, $self->{next_input_character};          push @next_char, $self->{next_input_character};
988          if ($self->{next_input_character} == 0x002D) { # -          if ($self->{next_input_character} == 0x002D) { # -
989            $self->{current_token} = {type => 'comment', data => ''};            $self->{current_token} = {type => 'comment', data => ''};
990            $self->{state} = 'comment';            $self->{state} = 'comment start';
991            !!!next-input-character;            !!!next-input-character;
992            redo A;            redo A;
993          }          }
# Line 1245  sub _get_next_token ($) { Line 1029  sub _get_next_token ($) {
1029          }          }
1030        }        }
1031    
1032        !!!parse-error (type => 'bogus comment open');        !!!parse-error (type => 'bogus comment');
1033        $self->{next_input_character} = shift @next_char;        $self->{next_input_character} = shift @next_char;
1034        !!!back-next-input-character (@next_char);        !!!back-next-input-character (@next_char);
1035        $self->{state} = 'bogus comment';        $self->{state} = 'bogus comment';
# Line 1253  sub _get_next_token ($) { Line 1037  sub _get_next_token ($) {
1037                
1038        ## ISSUE: typos in spec: chacacters, is is a parse error        ## ISSUE: typos in spec: chacacters, is is a parse error
1039        ## 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?
1040        } elsif ($self->{state} eq 'comment start') {
1041          if ($self->{next_input_character} == 0x002D) { # -
1042            $self->{state} = 'comment start dash';
1043            !!!next-input-character;
1044            redo A;
1045          } elsif ($self->{next_input_character} == 0x003E) { # >
1046            !!!parse-error (type => 'bogus comment');
1047            $self->{state} = 'data';
1048            !!!next-input-character;
1049    
1050            !!!emit ($self->{current_token}); # comment
1051    
1052            redo A;
1053          } elsif ($self->{next_input_character} == -1) {
1054            !!!parse-error (type => 'unclosed comment');
1055            $self->{state} = 'data';
1056            ## reconsume
1057    
1058            !!!emit ($self->{current_token}); # comment
1059    
1060            redo A;
1061          } else {
1062            $self->{current_token}->{data} # comment
1063                .= chr ($self->{next_input_character});
1064            $self->{state} = 'comment';
1065            !!!next-input-character;
1066            redo A;
1067          }
1068        } elsif ($self->{state} eq 'comment start dash') {
1069          if ($self->{next_input_character} == 0x002D) { # -
1070            $self->{state} = 'comment end';
1071            !!!next-input-character;
1072            redo A;
1073          } elsif ($self->{next_input_character} == 0x003E) { # >
1074            !!!parse-error (type => 'bogus comment');
1075            $self->{state} = 'data';
1076            !!!next-input-character;
1077    
1078            !!!emit ($self->{current_token}); # comment
1079    
1080            redo A;
1081          } elsif ($self->{next_input_character} == -1) {
1082            !!!parse-error (type => 'unclosed comment');
1083            $self->{state} = 'data';
1084            ## reconsume
1085    
1086            !!!emit ($self->{current_token}); # comment
1087    
1088            redo A;
1089          } else {
1090            $self->{current_token}->{data} # comment
1091                .= '-' . chr ($self->{next_input_character});
1092            $self->{state} = 'comment';
1093            !!!next-input-character;
1094            redo A;
1095          }
1096      } elsif ($self->{state} eq 'comment') {      } elsif ($self->{state} eq 'comment') {
1097        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_input_character} == 0x002D) { # -
1098          $self->{state} = 'comment dash';          $self->{state} = 'comment end dash';
1099          !!!next-input-character;          !!!next-input-character;
1100          redo A;          redo A;
1101        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
# Line 1264  sub _get_next_token ($) { Line 1104  sub _get_next_token ($) {
1104          ## reconsume          ## reconsume
1105    
1106          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
1107    
1108          redo A;          redo A;
1109        } else {        } else {
# Line 1273  sub _get_next_token ($) { Line 1112  sub _get_next_token ($) {
1112          !!!next-input-character;          !!!next-input-character;
1113          redo A;          redo A;
1114        }        }
1115      } elsif ($self->{state} eq 'comment dash') {      } elsif ($self->{state} eq 'comment end dash') {
1116        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_input_character} == 0x002D) { # -
1117          $self->{state} = 'comment end';          $self->{state} = 'comment end';
1118          !!!next-input-character;          !!!next-input-character;
# Line 1284  sub _get_next_token ($) { Line 1123  sub _get_next_token ($) {
1123          ## reconsume          ## reconsume
1124    
1125          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
1126    
1127          redo A;          redo A;
1128        } else {        } else {
# Line 1299  sub _get_next_token ($) { Line 1137  sub _get_next_token ($) {
1137          !!!next-input-character;          !!!next-input-character;
1138    
1139          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
1140    
1141          redo A;          redo A;
1142        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_input_character} == 0x002D) { # -
# Line 1314  sub _get_next_token ($) { Line 1151  sub _get_next_token ($) {
1151          ## reconsume          ## reconsume
1152    
1153          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
1154    
1155          redo A;          redo A;
1156        } else {        } else {
# Line 1348  sub _get_next_token ($) { Line 1184  sub _get_next_token ($) {
1184          ## Stay in the state          ## Stay in the state
1185          !!!next-input-character;          !!!next-input-character;
1186          redo A;          redo A;
       } elsif (0x0061 <= $self->{next_input_character} and  
                $self->{next_input_character} <= 0x007A) { # a..z  
 ## ISSUE: "Set the token's name name to the" in the spec  
         $self->{current_token} = {type => 'DOCTYPE',  
                           name => chr ($self->{next_input_character} - 0x0020),  
                           error => 1};  
         $self->{state} = 'DOCTYPE name';  
         !!!next-input-character;  
         redo A;  
1187        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1188          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
1189          $self->{state} = 'data';          $self->{state} = 'data';
1190          !!!next-input-character;          !!!next-input-character;
1191    
1192          !!!emit ({type => 'DOCTYPE', name => '', error => 1});          !!!emit ({type => 'DOCTYPE'}); # incorrect
1193    
1194          redo A;          redo A;
1195        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
# Line 1370  sub _get_next_token ($) { Line 1197  sub _get_next_token ($) {
1197          $self->{state} = 'data';          $self->{state} = 'data';
1198          ## reconsume          ## reconsume
1199    
1200          !!!emit ({type => 'DOCTYPE', name => '', error => 1});          !!!emit ({type => 'DOCTYPE'}); # incorrect
1201    
1202          redo A;          redo A;
1203        } else {        } else {
1204          $self->{current_token} = {type => 'DOCTYPE',          $self->{current_token}
1205                            name => chr ($self->{next_input_character}),              = {type => 'DOCTYPE',
1206                            error => 1};                 name => chr ($self->{next_input_character}),
1207                   correct => 1};
1208  ## ISSUE: "Set the token's name name to the" in the spec  ## ISSUE: "Set the token's name name to the" in the spec
1209          $self->{state} = 'DOCTYPE name';          $self->{state} = 'DOCTYPE name';
1210          !!!next-input-character;          !!!next-input-character;
1211          redo A;          redo A;
1212        }        }
1213      } elsif ($self->{state} eq 'DOCTYPE name') {      } elsif ($self->{state} eq 'DOCTYPE name') {
1214    ## ISSUE: Redundant "First," in the spec.
1215        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1216            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1217            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
1218            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
1219            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
         $self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML'); # DOCTYPE  
1220          $self->{state} = 'after DOCTYPE name';          $self->{state} = 'after DOCTYPE name';
1221          !!!next-input-character;          !!!next-input-character;
1222          redo A;          redo A;
1223        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
         $self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML'); # DOCTYPE  
1224          $self->{state} = 'data';          $self->{state} = 'data';
1225          !!!next-input-character;          !!!next-input-character;
1226    
1227          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1228    
1229          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;  
1230        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1231          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
         $self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML'); # DOCTYPE  
1232          $self->{state} = 'data';          $self->{state} = 'data';
1233          ## reconsume          ## reconsume
1234    
1235          !!!emit ($self->{current_token});          delete $self->{current_token}->{correct};
1236          undef $self->{current_token};          !!!emit ($self->{current_token}); # DOCTYPE
1237    
1238          redo A;          redo A;
1239        } else {        } else {
1240          $self->{current_token}->{name}          $self->{current_token}->{name}
1241            .= chr ($self->{next_input_character}); # DOCTYPE            .= chr ($self->{next_input_character}); # DOCTYPE
         #$self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML');  
1242          ## Stay in the state          ## Stay in the state
1243          !!!next-input-character;          !!!next-input-character;
1244          redo A;          redo A;
# Line 1440  sub _get_next_token ($) { Line 1257  sub _get_next_token ($) {
1257          !!!next-input-character;          !!!next-input-character;
1258    
1259          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1260    
1261          redo A;          redo A;
1262        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
# Line 1448  sub _get_next_token ($) { Line 1264  sub _get_next_token ($) {
1264          $self->{state} = 'data';          $self->{state} = 'data';
1265          ## reconsume          ## reconsume
1266    
1267            delete $self->{current_token}->{correct};
1268            !!!emit ($self->{current_token}); # DOCTYPE
1269    
1270            redo A;
1271          } elsif ($self->{next_input_character} == 0x0050 or # P
1272                   $self->{next_input_character} == 0x0070) { # p
1273            !!!next-input-character;
1274            if ($self->{next_input_character} == 0x0055 or # U
1275                $self->{next_input_character} == 0x0075) { # u
1276              !!!next-input-character;
1277              if ($self->{next_input_character} == 0x0042 or # B
1278                  $self->{next_input_character} == 0x0062) { # b
1279                !!!next-input-character;
1280                if ($self->{next_input_character} == 0x004C or # L
1281                    $self->{next_input_character} == 0x006C) { # l
1282                  !!!next-input-character;
1283                  if ($self->{next_input_character} == 0x0049 or # I
1284                      $self->{next_input_character} == 0x0069) { # i
1285                    !!!next-input-character;
1286                    if ($self->{next_input_character} == 0x0043 or # C
1287                        $self->{next_input_character} == 0x0063) { # c
1288                      $self->{state} = 'before DOCTYPE public identifier';
1289                      !!!next-input-character;
1290                      redo A;
1291                    }
1292                  }
1293                }
1294              }
1295            }
1296    
1297            #
1298          } elsif ($self->{next_input_character} == 0x0053 or # S
1299                   $self->{next_input_character} == 0x0073) { # s
1300            !!!next-input-character;
1301            if ($self->{next_input_character} == 0x0059 or # Y
1302                $self->{next_input_character} == 0x0079) { # y
1303              !!!next-input-character;
1304              if ($self->{next_input_character} == 0x0053 or # S
1305                  $self->{next_input_character} == 0x0073) { # s
1306                !!!next-input-character;
1307                if ($self->{next_input_character} == 0x0054 or # T
1308                    $self->{next_input_character} == 0x0074) { # t
1309                  !!!next-input-character;
1310                  if ($self->{next_input_character} == 0x0045 or # E
1311                      $self->{next_input_character} == 0x0065) { # e
1312                    !!!next-input-character;
1313                    if ($self->{next_input_character} == 0x004D or # M
1314                        $self->{next_input_character} == 0x006D) { # m
1315                      $self->{state} = 'before DOCTYPE system identifier';
1316                      !!!next-input-character;
1317                      redo A;
1318                    }
1319                  }
1320                }
1321              }
1322            }
1323    
1324            #
1325          } else {
1326            !!!next-input-character;
1327            #
1328          }
1329    
1330          !!!parse-error (type => 'string after DOCTYPE name');
1331          $self->{state} = 'bogus DOCTYPE';
1332          # next-input-character is already done
1333          redo A;
1334        } elsif ($self->{state} eq 'before DOCTYPE public identifier') {
1335          if ({
1336                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1337                #0x000D => 1, # HT, LF, VT, FF, SP, CR
1338              }->{$self->{next_input_character}}) {
1339            ## Stay in the state
1340            !!!next-input-character;
1341            redo A;
1342          } elsif ($self->{next_input_character} eq 0x0022) { # "
1343            $self->{current_token}->{public_identifier} = ''; # DOCTYPE
1344            $self->{state} = 'DOCTYPE public identifier (double-quoted)';
1345            !!!next-input-character;
1346            redo A;
1347          } elsif ($self->{next_input_character} eq 0x0027) { # '
1348            $self->{current_token}->{public_identifier} = ''; # DOCTYPE
1349            $self->{state} = 'DOCTYPE public identifier (single-quoted)';
1350            !!!next-input-character;
1351            redo A;
1352          } elsif ($self->{next_input_character} eq 0x003E) { # >
1353            !!!parse-error (type => 'no PUBLIC literal');
1354    
1355            $self->{state} = 'data';
1356            !!!next-input-character;
1357    
1358            delete $self->{current_token}->{correct};
1359            !!!emit ($self->{current_token}); # DOCTYPE
1360    
1361            redo A;
1362          } elsif ($self->{next_input_character} == -1) {
1363            !!!parse-error (type => 'unclosed DOCTYPE');
1364    
1365            $self->{state} = 'data';
1366            ## reconsume
1367    
1368            delete $self->{current_token}->{correct};
1369            !!!emit ($self->{current_token}); # DOCTYPE
1370    
1371            redo A;
1372          } else {
1373            !!!parse-error (type => 'string after PUBLIC');
1374            $self->{state} = 'bogus DOCTYPE';
1375            !!!next-input-character;
1376            redo A;
1377          }
1378        } elsif ($self->{state} eq 'DOCTYPE public identifier (double-quoted)') {
1379          if ($self->{next_input_character} == 0x0022) { # "
1380            $self->{state} = 'after DOCTYPE public identifier';
1381            !!!next-input-character;
1382            redo A;
1383          } elsif ($self->{next_input_character} == -1) {
1384            !!!parse-error (type => 'unclosed PUBLIC literal');
1385    
1386            $self->{state} = 'data';
1387            ## reconsume
1388    
1389            delete $self->{current_token}->{correct};
1390            !!!emit ($self->{current_token}); # DOCTYPE
1391    
1392            redo A;
1393          } else {
1394            $self->{current_token}->{public_identifier} # DOCTYPE
1395                .= chr $self->{next_input_character};
1396            ## Stay in the state
1397            !!!next-input-character;
1398            redo A;
1399          }
1400        } elsif ($self->{state} eq 'DOCTYPE public identifier (single-quoted)') {
1401          if ($self->{next_input_character} == 0x0027) { # '
1402            $self->{state} = 'after DOCTYPE public identifier';
1403            !!!next-input-character;
1404            redo A;
1405          } elsif ($self->{next_input_character} == -1) {
1406            !!!parse-error (type => 'unclosed PUBLIC literal');
1407    
1408            $self->{state} = 'data';
1409            ## reconsume
1410    
1411            delete $self->{current_token}->{correct};
1412            !!!emit ($self->{current_token}); # DOCTYPE
1413    
1414            redo A;
1415          } else {
1416            $self->{current_token}->{public_identifier} # DOCTYPE
1417                .= chr $self->{next_input_character};
1418            ## Stay in the state
1419            !!!next-input-character;
1420            redo A;
1421          }
1422        } elsif ($self->{state} eq 'after DOCTYPE public identifier') {
1423          if ({
1424                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1425                #0x000D => 1, # HT, LF, VT, FF, SP, CR
1426              }->{$self->{next_input_character}}) {
1427            ## Stay in the state
1428            !!!next-input-character;
1429            redo A;
1430          } elsif ($self->{next_input_character} == 0x0022) { # "
1431            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1432            $self->{state} = 'DOCTYPE system identifier (double-quoted)';
1433            !!!next-input-character;
1434            redo A;
1435          } elsif ($self->{next_input_character} == 0x0027) { # '
1436            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1437            $self->{state} = 'DOCTYPE system identifier (single-quoted)';
1438            !!!next-input-character;
1439            redo A;
1440          } elsif ($self->{next_input_character} == 0x003E) { # >
1441            $self->{state} = 'data';
1442            !!!next-input-character;
1443    
1444            !!!emit ($self->{current_token}); # DOCTYPE
1445    
1446            redo A;
1447          } elsif ($self->{next_input_character} == -1) {
1448            !!!parse-error (type => 'unclosed DOCTYPE');
1449    
1450            $self->{state} = 'data';
1451            ## reconsume
1452    
1453            delete $self->{current_token}->{correct};
1454            !!!emit ($self->{current_token}); # DOCTYPE
1455    
1456            redo A;
1457          } else {
1458            !!!parse-error (type => 'string after PUBLIC literal');
1459            $self->{state} = 'bogus DOCTYPE';
1460            !!!next-input-character;
1461            redo A;
1462          }
1463        } elsif ($self->{state} eq 'before DOCTYPE system identifier') {
1464          if ({
1465                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1466                #0x000D => 1, # HT, LF, VT, FF, SP, CR
1467              }->{$self->{next_input_character}}) {
1468            ## Stay in the state
1469            !!!next-input-character;
1470            redo A;
1471          } elsif ($self->{next_input_character} == 0x0022) { # "
1472            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1473            $self->{state} = 'DOCTYPE system identifier (double-quoted)';
1474            !!!next-input-character;
1475            redo A;
1476          } elsif ($self->{next_input_character} == 0x0027) { # '
1477            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1478            $self->{state} = 'DOCTYPE system identifier (single-quoted)';
1479            !!!next-input-character;
1480            redo A;
1481          } elsif ($self->{next_input_character} == 0x003E) { # >
1482            !!!parse-error (type => 'no SYSTEM literal');
1483            $self->{state} = 'data';
1484            !!!next-input-character;
1485    
1486            delete $self->{current_token}->{correct};
1487            !!!emit ($self->{current_token}); # DOCTYPE
1488    
1489            redo A;
1490          } elsif ($self->{next_input_character} == -1) {
1491            !!!parse-error (type => 'unclosed DOCTYPE');
1492    
1493            $self->{state} = 'data';
1494            ## reconsume
1495    
1496            delete $self->{current_token}->{correct};
1497          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1498    
1499          redo A;          redo A;
1500        } else {        } else {
1501          !!!parse-error (type => 'string after DOCTYPE name');          !!!parse-error (type => 'string after SYSTEM');
1502          $self->{current_token}->{error} = 1; # DOCTYPE          $self->{state} = 'bogus DOCTYPE';
1503            !!!next-input-character;
1504            redo A;
1505          }
1506        } elsif ($self->{state} eq 'DOCTYPE system identifier (double-quoted)') {
1507          if ($self->{next_input_character} == 0x0022) { # "
1508            $self->{state} = 'after DOCTYPE system identifier';
1509            !!!next-input-character;
1510            redo A;
1511          } elsif ($self->{next_input_character} == -1) {
1512            !!!parse-error (type => 'unclosed SYSTEM literal');
1513    
1514            $self->{state} = 'data';
1515            ## reconsume
1516    
1517            delete $self->{current_token}->{correct};
1518            !!!emit ($self->{current_token}); # DOCTYPE
1519    
1520            redo A;
1521          } else {
1522            $self->{current_token}->{system_identifier} # DOCTYPE
1523                .= chr $self->{next_input_character};
1524            ## Stay in the state
1525            !!!next-input-character;
1526            redo A;
1527          }
1528        } elsif ($self->{state} eq 'DOCTYPE system identifier (single-quoted)') {
1529          if ($self->{next_input_character} == 0x0027) { # '
1530            $self->{state} = 'after DOCTYPE system identifier';
1531            !!!next-input-character;
1532            redo A;
1533          } elsif ($self->{next_input_character} == -1) {
1534            !!!parse-error (type => 'unclosed SYSTEM literal');
1535    
1536            $self->{state} = 'data';
1537            ## reconsume
1538    
1539            delete $self->{current_token}->{correct};
1540            !!!emit ($self->{current_token}); # DOCTYPE
1541    
1542            redo A;
1543          } else {
1544            $self->{current_token}->{system_identifier} # DOCTYPE
1545                .= chr $self->{next_input_character};
1546            ## Stay in the state
1547            !!!next-input-character;
1548            redo A;
1549          }
1550        } elsif ($self->{state} eq 'after DOCTYPE system identifier') {
1551          if ({
1552                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1553                #0x000D => 1, # HT, LF, VT, FF, SP, CR
1554              }->{$self->{next_input_character}}) {
1555            ## Stay in the state
1556            !!!next-input-character;
1557            redo A;
1558          } elsif ($self->{next_input_character} == 0x003E) { # >
1559            $self->{state} = 'data';
1560            !!!next-input-character;
1561    
1562            !!!emit ($self->{current_token}); # DOCTYPE
1563    
1564            redo A;
1565          } elsif ($self->{next_input_character} == -1) {
1566            !!!parse-error (type => 'unclosed DOCTYPE');
1567    
1568            $self->{state} = 'data';
1569            ## reconsume
1570    
1571            delete $self->{current_token}->{correct};
1572            !!!emit ($self->{current_token}); # DOCTYPE
1573    
1574            redo A;
1575          } else {
1576            !!!parse-error (type => 'string after SYSTEM literal');
1577          $self->{state} = 'bogus DOCTYPE';          $self->{state} = 'bogus DOCTYPE';
1578          !!!next-input-character;          !!!next-input-character;
1579          redo A;          redo A;
# Line 1464  sub _get_next_token ($) { Line 1583  sub _get_next_token ($) {
1583          $self->{state} = 'data';          $self->{state} = 'data';
1584          !!!next-input-character;          !!!next-input-character;
1585    
1586            delete $self->{current_token}->{correct};
1587          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1588    
1589          redo A;          redo A;
1590        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
# Line 1473  sub _get_next_token ($) { Line 1592  sub _get_next_token ($) {
1592          $self->{state} = 'data';          $self->{state} = 'data';
1593          ## reconsume          ## reconsume
1594    
1595            delete $self->{current_token}->{correct};
1596          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1597    
1598          redo A;          redo A;
1599        } else {        } else {
# Line 1490  sub _get_next_token ($) { Line 1609  sub _get_next_token ($) {
1609    die "$0: _get_next_token: unexpected case";    die "$0: _get_next_token: unexpected case";
1610  } # _get_next_token  } # _get_next_token
1611    
1612  sub _tokenize_attempt_to_consume_an_entity ($) {  sub _tokenize_attempt_to_consume_an_entity ($$) {
1613    my $self = shift;    my ($self, $in_attr) = @_;
1614      
1615    if ($self->{next_input_character} == 0x0023) { # #    if ({
1616           0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,
1617           0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR
1618          }->{$self->{next_input_character}}) {
1619        ## Don't consume
1620        ## No error
1621        return undef;
1622      } elsif ($self->{next_input_character} == 0x0023) { # #
1623      !!!next-input-character;      !!!next-input-character;
1624      if ($self->{next_input_character} == 0x0078 or # x      if ($self->{next_input_character} == 0x0078 or # x
1625          $self->{next_input_character} == 0x0058) { # X          $self->{next_input_character} == 0x0058) { # X
1626        my $num;        my $code;
1627        X: {        X: {
1628          my $x_char = $self->{next_input_character};          my $x_char = $self->{next_input_character};
1629          !!!next-input-character;          !!!next-input-character;
1630          if (0x0030 <= $self->{next_input_character} and          if (0x0030 <= $self->{next_input_character} and
1631              $self->{next_input_character} <= 0x0039) { # 0..9              $self->{next_input_character} <= 0x0039) { # 0..9
1632            $num ||= 0;            $code ||= 0;
1633            $num *= 0x10;            $code *= 0x10;
1634            $num += $self->{next_input_character} - 0x0030;            $code += $self->{next_input_character} - 0x0030;
1635            redo X;            redo X;
1636          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_input_character} and
1637                   $self->{next_input_character} <= 0x0066) { # a..f                   $self->{next_input_character} <= 0x0066) { # a..f
1638            ## ISSUE: the spec says U+0078, which is apparently incorrect            $code ||= 0;
1639            $num ||= 0;            $code *= 0x10;
1640            $num *= 0x10;            $code += $self->{next_input_character} - 0x0060 + 9;
           $num += $self->{next_input_character} - 0x0060 + 9;  
1641            redo X;            redo X;
1642          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_input_character} and
1643                   $self->{next_input_character} <= 0x0046) { # A..F                   $self->{next_input_character} <= 0x0046) { # A..F
1644            ## ISSUE: the spec says U+0058, which is apparently incorrect            $code ||= 0;
1645            $num ||= 0;            $code *= 0x10;
1646            $num *= 0x10;            $code += $self->{next_input_character} - 0x0040 + 9;
           $num += $self->{next_input_character} - 0x0040 + 9;  
1647            redo X;            redo X;
1648          } elsif (not defined $num) { # no hexadecimal digit          } elsif (not defined $code) { # no hexadecimal digit
1649            !!!parse-error (type => 'bare hcro');            !!!parse-error (type => 'bare hcro');
1650              !!!back-next-input-character ($x_char, $self->{next_input_character});
1651            $self->{next_input_character} = 0x0023; # #            $self->{next_input_character} = 0x0023; # #
           !!!back-next-input-character ($x_char);  
1652            return undef;            return undef;
1653          } elsif ($self->{next_input_character} == 0x003B) { # ;          } elsif ($self->{next_input_character} == 0x003B) { # ;
1654            !!!next-input-character;            !!!next-input-character;
# Line 1532  sub _tokenize_attempt_to_consume_an_enti Line 1656  sub _tokenize_attempt_to_consume_an_enti
1656            !!!parse-error (type => 'no refc');            !!!parse-error (type => 'no refc');
1657          }          }
1658    
1659          ## TODO: check the definition for |a valid Unicode character|.          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
1660          ## <http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2006-December/thread.html#8189>            !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);
1661          if ($num > 1114111 or $num == 0) {            $code = 0xFFFD;
1662            $num = 0xFFFD; # REPLACEMENT CHARACTER          } elsif ($code > 0x10FFFF) {
1663            ## ISSUE: Why this is not an error?            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);
1664          } elsif (0x80 <= $num and $num <= 0x9F) {            $code = 0xFFFD;
1665            ## NOTE: <http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2006-December/thread.html#8562>          } elsif ($code == 0x000D) {
1666            ## ISSUE: Not in the spec yet; parse error?            !!!parse-error (type => 'CR character reference');
1667            $num = $c1_entity_char->{$num};            $code = 0x000A;
1668            } elsif (0x80 <= $code and $code <= 0x9F) {
1669              !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);
1670              $code = $c1_entity_char->{$code};
1671          }          }
1672    
1673          return {type => 'character', data => chr $num};          return {type => 'character', data => chr $code};
1674        } # X        } # X
1675      } elsif (0x0030 <= $self->{next_input_character} and      } elsif (0x0030 <= $self->{next_input_character} and
1676               $self->{next_input_character} <= 0x0039) { # 0..9               $self->{next_input_character} <= 0x0039) { # 0..9
# Line 1564  sub _tokenize_attempt_to_consume_an_enti Line 1691  sub _tokenize_attempt_to_consume_an_enti
1691          !!!parse-error (type => 'no refc');          !!!parse-error (type => 'no refc');
1692        }        }
1693    
1694        ## TODO: check the definition for |a valid Unicode character|.        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
1695        if ($code > 1114111 or $code == 0) {          !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);
1696          $code = 0xFFFD; # REPLACEMENT CHARACTER          $code = 0xFFFD;
1697          ## ISSUE: Why this is not an error?        } elsif ($code > 0x10FFFF) {
1698            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);
1699            $code = 0xFFFD;
1700          } elsif ($code == 0x000D) {
1701            !!!parse-error (type => 'CR character reference');
1702            $code = 0x000A;
1703        } elsif (0x80 <= $code and $code <= 0x9F) {        } elsif (0x80 <= $code and $code <= 0x9F) {
1704          ## NOTE: <http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2006-December/thread.html#8562>          !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);
         ## ISSUE: Not in the spec yet; parse error?  
1705          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
1706        }        }
1707                
# Line 1589  sub _tokenize_attempt_to_consume_an_enti Line 1720  sub _tokenize_attempt_to_consume_an_enti
1720      !!!next-input-character;      !!!next-input-character;
1721    
1722      my $value = $entity_name;      my $value = $entity_name;
1723      my $match;      my $match = 0;
1724        require Whatpm::_NamedEntityList;
1725        our $EntityChar;
1726    
1727      while (length $entity_name < 10 and      while (length $entity_name < 10 and
1728             ## NOTE: Some number greater than the maximum length of entity name             ## NOTE: Some number greater than the maximum length of entity name
1729             ((0x0041 <= $self->{next_input_character} and             ((0x0041 <= $self->{next_input_character} and # a
1730               $self->{next_input_character} <= 0x005A) or               $self->{next_input_character} <= 0x005A) or # x
1731              (0x0061 <= $self->{next_input_character} and              (0x0061 <= $self->{next_input_character} and # a
1732               $self->{next_input_character} <= 0x007A) or               $self->{next_input_character} <= 0x007A) or # z
1733              (0x0030 <= $self->{next_input_character} and              (0x0030 <= $self->{next_input_character} and # 0
1734               $self->{next_input_character} <= 0x0039))) {               $self->{next_input_character} <= 0x0039) or # 9
1735                $self->{next_input_character} == 0x003B)) { # ;
1736        $entity_name .= chr $self->{next_input_character};        $entity_name .= chr $self->{next_input_character};
1737        if (defined $entity_char->{$entity_name}) {        if (defined $EntityChar->{$entity_name}) {
1738          $value = $entity_char->{$entity_name};          if ($self->{next_input_character} == 0x003B) { # ;
1739          $match = 1;            $value = $EntityChar->{$entity_name};
1740              $match = 1;
1741              !!!next-input-character;
1742              last;
1743            } else {
1744              $value = $EntityChar->{$entity_name};
1745              $match = -1;
1746              !!!next-input-character;
1747            }
1748        } else {        } else {
1749          $value .= chr $self->{next_input_character};          $value .= chr $self->{next_input_character};
1750            $match *= 2;
1751            !!!next-input-character;
1752        }        }
       !!!next-input-character;  
1753      }      }
1754            
1755      if ($match) {      if ($match > 0) {
1756        if ($self->{next_input_character} == 0x003B) { # ;        return {type => 'character', data => $value};
1757          !!!next-input-character;      } elsif ($match < 0) {
1758          !!!parse-error (type => 'no refc');
1759          if ($in_attr and $match < -1) {
1760            return {type => 'character', data => '&'.$entity_name};
1761        } else {        } else {
1762          !!!parse-error (type => 'refc');          return {type => 'character', data => $value};
1763        }        }
   
       return {type => 'character', data => $value};  
1764      } else {      } else {
1765        !!!parse-error (type => 'bare ero');        !!!parse-error (type => 'bare ero');
1766        ## NOTE: No characters are consumed in the spec.        ## NOTE: No characters are consumed in the spec.
1767        !!!back-token ({type => 'character', data => $value});        return {type => 'character', data => '&'.$value};
       return undef;  
1768      }      }
1769    } else {    } else {
1770      ## no characters are consumed      ## no characters are consumed
# Line 1636  sub _initialize_tree_constructor ($) { Line 1779  sub _initialize_tree_constructor ($) {
1779    $self->{document}->strict_error_checking (0);    $self->{document}->strict_error_checking (0);
1780    ## TODO: Turn mutation events off # MUST    ## TODO: Turn mutation events off # MUST
1781    ## TODO: Turn loose Document option (manakai extension) on    ## TODO: Turn loose Document option (manakai extension) on
1782    ## TODO: Mark the Document as an HTML document # MUST    $self->{document}->manakai_is_html (1); # MUST
1783  } # _initialize_tree_constructor  } # _initialize_tree_constructor
1784    
1785  sub _terminate_tree_constructor ($) {  sub _terminate_tree_constructor ($) {
# Line 1676  sub _construct_tree ($) { Line 1819  sub _construct_tree ($) {
1819    
1820  sub _tree_construction_initial ($) {  sub _tree_construction_initial ($) {
1821    my $self = shift;    my $self = shift;
1822    B: {    INITIAL: {
1823        if ($token->{type} eq 'DOCTYPE') {      if ($token->{type} eq 'DOCTYPE') {
1824          if ($token->{error}) {        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"
1825            ## ISSUE: Spec currently left this case undefined.        ## error, switch to a conformance checking mode for another
1826            !!!parse-error (type => 'bogus DOCTYPE');        ## language.
1827          }        my $doctype_name = $token->{name};
1828          my $doctype = $self->{document}->create_document_type_definition        $doctype_name = '' unless defined $doctype_name;
1829            ($token->{name});        $doctype_name =~ tr/a-z/A-Z/;
1830          $self->{document}->append_child ($doctype);        if (not defined $token->{name} or # <!DOCTYPE>
1831          #$phase = 'root element';            defined $token->{public_identifier} or
1832          !!!next-token;            defined $token->{system_identifier}) {
1833          #redo B;          !!!parse-error (type => 'not HTML5');
1834          return;        } elsif ($doctype_name ne 'HTML') {
1835        } elsif ({          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)
1836                  comment => 1,          !!!parse-error (type => 'not HTML5');
1837                  'start tag' => 1,        }
1838                  'end tag' => 1,        
1839                  'end-of-file' => 1,        my $doctype = $self->{document}->create_document_type_definition
1840                 }->{$token->{type}}) {          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
1841          ## ISSUE: Spec currently left this case undefined.        $doctype->public_id ($token->{public_identifier})
1842          !!!parse-error (type => 'missing DOCTYPE');            if defined $token->{public_identifier};
1843          #$phase = 'root element';        $doctype->system_id ($token->{system_identifier})
1844          ## reprocess            if defined $token->{system_identifier};
1845          #redo B;        ## NOTE: Other DocumentType attributes are null or empty lists.
1846          return;        ## ISSUE: internalSubset = null??
1847        } elsif ($token->{type} eq 'character') {        $self->{document}->append_child ($doctype);
1848          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {        
1849            $self->{document}->manakai_append_text ($1);        if (not $token->{correct} or $doctype_name ne 'HTML') {
1850            ## ISSUE: DOM3 Core does not allow Document > Text          $self->{document}->manakai_compat_mode ('quirks');
1851            unless (length $token->{data}) {        } elsif (defined $token->{public_identifier}) {
1852              ## Stay in the phase          my $pubid = $token->{public_identifier};
1853              !!!next-token;          $pubid =~ tr/a-z/A-z/;
1854              redo B;          if ({
1855              "+//SILMARIL//DTD HTML PRO V0R11 19970101//EN" => 1,
1856              "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,
1857              "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,
1858              "-//IETF//DTD HTML 2.0 LEVEL 1//EN" => 1,
1859              "-//IETF//DTD HTML 2.0 LEVEL 2//EN" => 1,
1860              "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//EN" => 1,
1861              "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//EN" => 1,
1862              "-//IETF//DTD HTML 2.0 STRICT//EN" => 1,
1863              "-//IETF//DTD HTML 2.0//EN" => 1,
1864              "-//IETF//DTD HTML 2.1E//EN" => 1,
1865              "-//IETF//DTD HTML 3.0//EN" => 1,
1866              "-//IETF//DTD HTML 3.0//EN//" => 1,
1867              "-//IETF//DTD HTML 3.2 FINAL//EN" => 1,
1868              "-//IETF//DTD HTML 3.2//EN" => 1,
1869              "-//IETF//DTD HTML 3//EN" => 1,
1870              "-//IETF//DTD HTML LEVEL 0//EN" => 1,
1871              "-//IETF//DTD HTML LEVEL 0//EN//2.0" => 1,
1872              "-//IETF//DTD HTML LEVEL 1//EN" => 1,
1873              "-//IETF//DTD HTML LEVEL 1//EN//2.0" => 1,
1874              "-//IETF//DTD HTML LEVEL 2//EN" => 1,
1875              "-//IETF//DTD HTML LEVEL 2//EN//2.0" => 1,
1876              "-//IETF//DTD HTML LEVEL 3//EN" => 1,
1877              "-//IETF//DTD HTML LEVEL 3//EN//3.0" => 1,
1878              "-//IETF//DTD HTML STRICT LEVEL 0//EN" => 1,
1879              "-//IETF//DTD HTML STRICT LEVEL 0//EN//2.0" => 1,
1880              "-//IETF//DTD HTML STRICT LEVEL 1//EN" => 1,
1881              "-//IETF//DTD HTML STRICT LEVEL 1//EN//2.0" => 1,
1882              "-//IETF//DTD HTML STRICT LEVEL 2//EN" => 1,
1883              "-//IETF//DTD HTML STRICT LEVEL 2//EN//2.0" => 1,
1884              "-//IETF//DTD HTML STRICT LEVEL 3//EN" => 1,
1885              "-//IETF//DTD HTML STRICT LEVEL 3//EN//3.0" => 1,
1886              "-//IETF//DTD HTML STRICT//EN" => 1,
1887              "-//IETF//DTD HTML STRICT//EN//2.0" => 1,
1888              "-//IETF//DTD HTML STRICT//EN//3.0" => 1,
1889              "-//IETF//DTD HTML//EN" => 1,
1890              "-//IETF//DTD HTML//EN//2.0" => 1,
1891              "-//IETF//DTD HTML//EN//3.0" => 1,
1892              "-//METRIUS//DTD METRIUS PRESENTATIONAL//EN" => 1,
1893              "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//EN" => 1,
1894              "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//EN" => 1,
1895              "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//EN" => 1,
1896              "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//EN" => 1,
1897              "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//EN" => 1,
1898              "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//EN" => 1,
1899              "-//NETSCAPE COMM. CORP.//DTD HTML//EN" => 1,
1900              "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,
1901              "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,
1902              "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,
1903              "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,
1904              "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,
1905              "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,
1906              "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//EN" => 1,
1907              "-//W3C//DTD HTML 3 1995-03-24//EN" => 1,
1908              "-//W3C//DTD HTML 3.2 DRAFT//EN" => 1,
1909              "-//W3C//DTD HTML 3.2 FINAL//EN" => 1,
1910              "-//W3C//DTD HTML 3.2//EN" => 1,
1911              "-//W3C//DTD HTML 3.2S DRAFT//EN" => 1,
1912              "-//W3C//DTD HTML 4.0 FRAMESET//EN" => 1,
1913              "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN" => 1,
1914              "-//W3C//DTD HTML EXPERIMETNAL 19960712//EN" => 1,
1915              "-//W3C//DTD HTML EXPERIMENTAL 970421//EN" => 1,
1916              "-//W3C//DTD W3 HTML//EN" => 1,
1917              "-//W3O//DTD W3 HTML 3.0//EN" => 1,
1918              "-//W3O//DTD W3 HTML 3.0//EN//" => 1,
1919              "-//W3O//DTD W3 HTML STRICT 3.0//EN//" => 1,
1920              "-//WEBTECHS//DTD MOZILLA HTML 2.0//EN" => 1,
1921              "-//WEBTECHS//DTD MOZILLA HTML//EN" => 1,
1922              "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" => 1,
1923              "HTML" => 1,
1924            }->{$pubid}) {
1925              $self->{document}->manakai_compat_mode ('quirks');
1926            } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or
1927                     $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {
1928              if (defined $token->{system_identifier}) {
1929                $self->{document}->manakai_compat_mode ('quirks');
1930              } else {
1931                $self->{document}->manakai_compat_mode ('limited quirks');
1932            }            }
1933            } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 Frameset//EN" or
1934                     $pubid eq "-//W3C//DTD XHTML 1.0 Transitional//EN") {
1935              $self->{document}->manakai_compat_mode ('limited quirks');
1936            }
1937          }
1938          if (defined $token->{system_identifier}) {
1939            my $sysid = $token->{system_identifier};
1940            $sysid =~ tr/A-Z/a-z/;
1941            if ($sysid eq "http://www.ibm.com/data/dtd/v11/ibmxhtml1-transitional.dtd") {
1942              $self->{document}->manakai_compat_mode ('quirks');
1943          }          }
         ## ISSUE: Spec currently left this case undefined.  
         !!!parse-error (type => 'missing DOCTYPE');  
         #$phase = 'root element';  
         ## reprocess  
         #redo B;  
         return;  
       } else {  
         die "$0: $token->{type}: Unknown token";  
1944        }        }
1945      } # B        
1946          ## Go to the root element phase.
1947          !!!next-token;
1948          return;
1949        } elsif ({
1950                  'start tag' => 1,
1951                  'end tag' => 1,
1952                  'end-of-file' => 1,
1953                 }->{$token->{type}}) {
1954          !!!parse-error (type => 'no DOCTYPE');
1955          $self->{document}->manakai_compat_mode ('quirks');
1956          ## Go to the root element phase
1957          ## reprocess
1958          return;
1959        } elsif ($token->{type} eq 'character') {
1960          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
1961            ## Ignore the token
1962    
1963            unless (length $token->{data}) {
1964              ## Stay in the phase
1965              !!!next-token;
1966              redo INITIAL;
1967            }
1968          }
1969    
1970          !!!parse-error (type => 'no DOCTYPE');
1971          $self->{document}->manakai_compat_mode ('quirks');
1972          ## Go to the root element phase
1973          ## reprocess
1974          return;
1975        } elsif ($token->{type} eq 'comment') {
1976          my $comment = $self->{document}->create_comment ($token->{data});
1977          $self->{document}->append_child ($comment);
1978          
1979          ## Stay in the phase.
1980          !!!next-token;
1981          redo INITIAL;
1982        } else {
1983          die "$0: $token->{type}: Unknown token";
1984        }
1985      } # INITIAL
1986  } # _tree_construction_initial  } # _tree_construction_initial
1987    
1988  sub _tree_construction_root_element ($) {  sub _tree_construction_root_element ($) {
# Line 1740  sub _tree_construction_root_element ($) Line 2002  sub _tree_construction_root_element ($)
2002          !!!next-token;          !!!next-token;
2003          redo B;          redo B;
2004        } elsif ($token->{type} eq 'character') {        } elsif ($token->{type} eq 'character') {
2005          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
2006            $self->{document}->manakai_append_text ($1);            ## Ignore the token.
2007            ## ISSUE: DOM3 Core does not allow Document > Text  
2008            unless (length $token->{data}) {            unless (length $token->{data}) {
2009              ## Stay in the phase              ## Stay in the phase
2010              !!!next-token;              !!!next-token;
# Line 1763  sub _tree_construction_root_element ($) Line 2025  sub _tree_construction_root_element ($)
2025        my $root_element; !!!create-element ($root_element, 'html');        my $root_element; !!!create-element ($root_element, 'html');
2026        $self->{document}->append_child ($root_element);        $self->{document}->append_child ($root_element);
2027        push @{$self->{open_elements}}, [$root_element, 'html'];        push @{$self->{open_elements}}, [$root_element, 'html'];
       #$phase = 'main';  
2028        ## reprocess        ## reprocess
2029        #redo B;        #redo B;
2030        return;        return; ## Go to the main phase.
2031    } # B    } # B
2032  } # _tree_construction_root_element  } # _tree_construction_root_element
2033    
# Line 1782  sub _reset_insertion_mode ($) { Line 2043  sub _reset_insertion_mode ($) {
2043            
2044      ## Step 3      ## Step 3
2045      S3: {      S3: {
2046        $last = 1 if $self->{open_elements}->[0]->[0] eq $node->[0];        ## ISSUE: Oops! "If node is the first node in the stack of open
2047        if (defined $self->{inner_html_node}) {        ## elements, then set last to true. If the context element of the
2048          if ($self->{inner_html_node}->[1] eq 'td' or        ## HTML fragment parsing algorithm is neither a td element nor a
2049              $self->{inner_html_node}->[1] eq 'th') {        ## th element, then set node to the context element. (fragment case)":
2050            #        ## The second "if" is in the scope of the first "if"!?
2051          } else {        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
2052            $node = $self->{inner_html_node};          $last = 1;
2053            if (defined $self->{inner_html_node}) {
2054              if ($self->{inner_html_node}->[1] eq 'td' or
2055                  $self->{inner_html_node}->[1] eq 'th') {
2056                #
2057              } else {
2058                $node = $self->{inner_html_node};
2059              }
2060          }          }
2061        }        }
2062            
# Line 1799  sub _reset_insertion_mode ($) { Line 2067  sub _reset_insertion_mode ($) {
2067                        th => 'in cell',                        th => 'in cell',
2068                        tr => 'in row',                        tr => 'in row',
2069                        tbody => 'in table body',                        tbody => 'in table body',
2070                        thead => 'in table head',                        thead => 'in table body',
2071                        tfoot => 'in table foot',                        tfoot => 'in table body',
2072                        caption => 'in caption',                        caption => 'in caption',
2073                        colgroup => 'in column group',                        colgroup => 'in column group',
2074                        table => 'in table',                        table => 'in table',
# Line 1835  sub _reset_insertion_mode ($) { Line 2103  sub _reset_insertion_mode ($) {
2103  sub _tree_construction_main ($) {  sub _tree_construction_main ($) {
2104    my $self = shift;    my $self = shift;
2105    
2106    my $phase = 'main';    my $previous_insertion_mode;
2107    
2108    my $active_formatting_elements = [];    my $active_formatting_elements = [];
2109    
# Line 1919  sub _tree_construction_main ($) { Line 2187  sub _tree_construction_main ($) {
2187      }      }
2188    }; # $clear_up_to_marker    }; # $clear_up_to_marker
2189    
2190    my $style_start_tag = sub {    my $parse_rcdata = sub ($$) {
2191      my $style_el; !!!create-element ($style_el, 'style', $token->{attributes});      my ($content_model_flag, $insert) = @_;
2192      ## $self->{insertion_mode} eq 'in head' and ... (always true)  
2193      (($self->{insertion_mode} eq 'in head' and defined $self->{head_element})      ## Step 1
2194       ? $self->{head_element} : $self->{open_elements}->[-1]->[0])      my $start_tag_name = $token->{tag_name};
2195        ->append_child ($style_el);      my $el;
2196      $self->{content_model_flag} = 'CDATA';      !!!create-element ($el, $start_tag_name, $token->{attributes});
2197                  
2198        ## Step 2
2199        $insert->($el); # /context node/->append_child ($el)
2200    
2201        ## Step 3
2202        $self->{content_model} = $content_model_flag; # CDATA or RCDATA
2203        delete $self->{escape}; # MUST
2204    
2205        ## Step 4
2206      my $text = '';      my $text = '';
2207      !!!next-token;      !!!next-token;
2208      while ($token->{type} eq 'character') {      while ($token->{type} eq 'character') { # or until stop tokenizing
2209        $text .= $token->{data};        $text .= $token->{data};
2210        !!!next-token;        !!!next-token;
2211      } # stop if non-character token or tokenizer stops tokenising      }
2212    
2213        ## Step 5
2214      if (length $text) {      if (length $text) {
2215        $style_el->manakai_append_text ($text);        my $text = $self->{document}->create_text_node ($text);
2216          $el->append_child ($text);
2217      }      }
2218        
2219      $self->{content_model_flag} = 'PCDATA';      ## Step 6
2220                      $self->{content_model} = PCDATA_CONTENT_MODEL;
2221      if ($token->{type} eq 'end tag' and $token->{tag_name} eq 'style') {  
2222        ## Step 7
2223        if ($token->{type} eq 'end tag' and $token->{tag_name} eq $start_tag_name) {
2224        ## Ignore the token        ## Ignore the token
2225      } else {      } elsif ($content_model_flag == CDATA_CONTENT_MODEL) {
2226        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!parse-error (type => 'in CDATA:#'.$token->{type});
2227        ## ISSUE: And ignore?      } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
2228          !!!parse-error (type => 'in RCDATA:#'.$token->{type});
2229        } else {
2230          die "$0: $content_model_flag in parse_rcdata";
2231      }      }
2232      !!!next-token;      !!!next-token;
2233    }; # $style_start_tag    }; # $parse_rcdata
2234    
2235    my $script_start_tag = sub {    my $script_start_tag = sub ($) {
2236        my $insert = $_[0];
2237      my $script_el;      my $script_el;
2238      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, 'script', $token->{attributes});
2239      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
2240    
2241      $self->{content_model_flag} = 'CDATA';      $self->{content_model} = CDATA_CONTENT_MODEL;
2242        delete $self->{escape}; # MUST
2243            
2244      my $text = '';      my $text = '';
2245      !!!next-token;      !!!next-token;
# Line 1965  sub _tree_construction_main ($) { Line 2251  sub _tree_construction_main ($) {
2251        $script_el->manakai_append_text ($text);        $script_el->manakai_append_text ($text);
2252      }      }
2253                                
2254      $self->{content_model_flag} = 'PCDATA';      $self->{content_model} = PCDATA_CONTENT_MODEL;
2255    
2256      if ($token->{type} eq 'end tag' and      if ($token->{type} eq 'end tag' and
2257          $token->{tag_name} eq 'script') {          $token->{tag_name} eq 'script') {
# Line 1981  sub _tree_construction_main ($) { Line 2267  sub _tree_construction_main ($) {
2267      } else {      } else {
2268        ## TODO: $old_insertion_point = current insertion point        ## TODO: $old_insertion_point = current insertion point
2269        ## TODO: insertion point = just before the next input character        ## TODO: insertion point = just before the next input character
2270          
2271        (($self->{insertion_mode} eq 'in head' and defined $self->{head_element})        $insert->($script_el);
        ? $self->{head_element} : $self->{open_elements}->[-1]->[0])->append_child ($script_el);  
2272                
2273        ## TODO: insertion point = $old_insertion_point (might be "undefined")        ## TODO: insertion point = $old_insertion_point (might be "undefined")
2274                
# Line 2177  sub _tree_construction_main ($) { Line 2462  sub _tree_construction_main ($) {
2462    }; # $formatting_end_tag    }; # $formatting_end_tag
2463    
2464    my $insert_to_current = sub {    my $insert_to_current = sub {
2465      $self->{open_elements}->[-1]->[0]->append_child (shift);      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);
2466    }; # $insert_to_current    }; # $insert_to_current
2467    
2468    my $insert_to_foster = sub {    my $insert_to_foster = sub {
# Line 2215  sub _tree_construction_main ($) { Line 2500  sub _tree_construction_main ($) {
2500      my $insert = shift;      my $insert = shift;
2501      if ($token->{type} eq 'start tag') {      if ($token->{type} eq 'start tag') {
2502        if ($token->{tag_name} eq 'script') {        if ($token->{tag_name} eq 'script') {
2503          $script_start_tag->();          ## NOTE: This is an "as if in head" code clone
2504            $script_start_tag->($insert);
2505          return;          return;
2506        } elsif ($token->{tag_name} eq 'style') {        } elsif ($token->{tag_name} eq 'style') {
2507          $style_start_tag->();          ## NOTE: This is an "as if in head" code clone
2508            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
2509          return;          return;
2510        } elsif ({        } elsif ({
2511                  base => 1, link => 1, meta => 1,                  base => 1, link => 1,
2512                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
2513          !!!parse-error (type => 'in body:'.$token->{tag_name});          ## NOTE: This is an "as if in head" code clone, only "-t" differs
2514          ## NOTE: This is an "as if in head" code clone          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
2515          my $el;          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
2516          !!!create-element ($el, $token->{tag_name}, $token->{attributes});          !!!next-token;
2517          if (defined $self->{head_element}) {          return;
2518            $self->{head_element}->append_child ($el);        } elsif ($token->{tag_name} eq 'meta') {
2519          } else {          ## NOTE: This is an "as if in head" code clone, only "-t" differs
2520            $insert->($el);          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
2521            pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
2522    
2523            unless ($self->{confident}) {
2524              my $charset;
2525              if ($token->{attributes}->{charset}) { ## TODO: And if supported
2526                $charset = $token->{attributes}->{charset}->{value};
2527              }
2528              if ($token->{attributes}->{'http-equiv'}) {
2529                ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.
2530                if ($token->{attributes}->{'http-equiv'}->{value}
2531                    =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=
2532                        [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
2533                        ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
2534                  $charset = defined $1 ? $1 : defined $2 ? $2 : $3;
2535                } ## TODO: And if supported
2536              }
2537              ## TODO: Change the encoding
2538          }          }
2539            
2540          !!!next-token;          !!!next-token;
2541          return;          return;
2542        } elsif ($token->{tag_name} eq 'title') {        } elsif ($token->{tag_name} eq 'title') {
2543          !!!parse-error (type => 'in body:title');          !!!parse-error (type => 'in body:title');
2544          ## NOTE: There is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
2545          my $title_el;          $parse_rcdata->(RCDATA_CONTENT_MODEL, sub {
2546          !!!create-element ($title_el, 'title', $token->{attributes});            if (defined $self->{head_element}) {
2547          (defined $self->{head_element} ? $self->{head_element} : $self->{open_elements}->[-1]->[0])              $self->{head_element}->append_child ($_[0]);
2548            ->append_child ($title_el);            } else {
2549          $self->{content_model_flag} = 'RCDATA';              $insert->($_[0]);
2550                      }
2551          my $text = '';          });
         !!!next-token;  
         while ($token->{type} eq 'character') {  
           $text .= $token->{data};  
           !!!next-token;  
         }  
         if (length $text) {  
           $title_el->manakai_append_text ($text);  
         }  
           
         $self->{content_model_flag} = 'PCDATA';  
           
         if ($token->{type} eq 'end tag' and  
             $token->{tag_name} eq 'title') {  
           ## Ignore the token  
         } else {  
           !!!parse-error (type => 'in RCDATA:#'.$token->{type});  
           ## ISSUE: And ignore?  
         }  
         !!!next-token;  
2552          return;          return;
2553        } elsif ($token->{tag_name} eq 'body') {        } elsif ($token->{tag_name} eq 'body') {
2554          !!!parse-error (type => 'in body:body');          !!!parse-error (type => 'in body:body');
# Line 2363  sub _tree_construction_main ($) { Line 2648  sub _tree_construction_main ($) {
2648          LI: {          LI: {
2649            ## Step 2            ## Step 2
2650            if ($node->[1] eq 'li') {            if ($node->[1] eq 'li') {
2651                if ($i != -1) {
2652                  !!!parse-error (type => 'end tag missing:'.
2653                                  $self->{open_elements}->[-1]->[1]);
2654                }
2655              splice @{$self->{open_elements}}, $i;              splice @{$self->{open_elements}}, $i;
2656              last LI;              last LI;
2657            }            }
# Line 2406  sub _tree_construction_main ($) { Line 2695  sub _tree_construction_main ($) {
2695          LI: {          LI: {
2696            ## Step 2            ## Step 2
2697            if ($node->[1] eq 'dt' or $node->[1] eq 'dd') {            if ($node->[1] eq 'dt' or $node->[1] eq 'dd') {
2698                if ($i != -1) {
2699                  !!!parse-error (type => 'end tag missing:'.
2700                                  $self->{open_elements}->[-1]->[1]);
2701                }
2702              splice @{$self->{open_elements}}, $i;              splice @{$self->{open_elements}}, $i;
2703              last LI;              last LI;
2704            }            }
# Line 2445  sub _tree_construction_main ($) { Line 2738  sub _tree_construction_main ($) {
2738                        
2739          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
2740                        
2741          $self->{content_model_flag} = 'PLAINTEXT';          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
2742                        
2743          !!!next-token;          !!!next-token;
2744          return;          return;
# Line 2467  sub _tree_construction_main ($) { Line 2760  sub _tree_construction_main ($) {
2760            }            }
2761          } # INSCOPE          } # INSCOPE
2762                        
2763            ## NOTE: See <http://html5.org/tools/web-apps-tracker?from=925&to=926>
2764          ## has an element in scope          ## has an element in scope
2765          my $i;          #my $i;
2766          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          #INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
2767            my $node = $self->{open_elements}->[$_];          #  my $node = $self->{open_elements}->[$_];
2768            if ({          #  if ({
2769                 h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,          #       h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
2770                }->{$node->[1]}) {          #      }->{$node->[1]}) {
2771              $i = $_;          #    $i = $_;
2772              last INSCOPE;          #    last INSCOPE;
2773            } elsif ({          #  } elsif ({
2774                      table => 1, caption => 1, td => 1, th => 1,          #            table => 1, caption => 1, td => 1, th => 1,
2775                      button => 1, marquee => 1, object => 1, html => 1,          #            button => 1, marquee => 1, object => 1, html => 1,
2776                     }->{$node->[1]}) {          #           }->{$node->[1]}) {
2777              last INSCOPE;          #    last INSCOPE;
2778            }          #  }
2779          } # INSCOPE          #} # INSCOPE
2780                      #  
2781          if (defined $i) {          #if (defined $i) {
2782            !!!parse-error (type => 'in hn:hn');          #  !!! parse-error (type => 'in hn:hn');
2783            splice @{$self->{open_elements}}, $i;          #  splice @{$self->{open_elements}}, $i;
2784          }          #}
2785                        
2786          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
2787                        
# Line 2530  sub _tree_construction_main ($) { Line 2824  sub _tree_construction_main ($) {
2824          return;          return;
2825        } elsif ({        } elsif ({
2826                  b => 1, big => 1, em => 1, font => 1, i => 1,                  b => 1, big => 1, em => 1, font => 1, i => 1,
2827                  nobr => 1, s => 1, small => 1, strile => 1,                  s => 1, small => 1, strile => 1,
2828                  strong => 1, tt => 1, u => 1,                  strong => 1, tt => 1, u => 1,
2829                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
2830          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
# Line 2540  sub _tree_construction_main ($) { Line 2834  sub _tree_construction_main ($) {
2834                    
2835          !!!next-token;          !!!next-token;
2836          return;          return;
2837          } elsif ($token->{tag_name} eq 'nobr') {
2838            $reconstruct_active_formatting_elements->($insert_to_current);
2839    
2840            ## has a |nobr| element in scope
2841            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
2842              my $node = $self->{open_elements}->[$_];
2843              if ($node->[1] eq 'nobr') {
2844                !!!parse-error (type => 'not closed:nobr');
2845                !!!back-token;
2846                $token = {type => 'end tag', tag_name => 'nobr'};
2847                return;
2848              } elsif ({
2849                        table => 1, caption => 1, td => 1, th => 1,
2850                        button => 1, marquee => 1, object => 1, html => 1,
2851                       }->{$node->[1]}) {
2852                last INSCOPE;
2853              }
2854            } # INSCOPE
2855            
2856            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
2857            push @$active_formatting_elements, $self->{open_elements}->[-1];
2858            
2859            !!!next-token;
2860            return;
2861        } elsif ($token->{tag_name} eq 'button') {        } elsif ($token->{tag_name} eq 'button') {
2862          ## has a button element in scope          ## has a button element in scope
2863          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 2575  sub _tree_construction_main ($) { Line 2893  sub _tree_construction_main ($) {
2893          return;          return;
2894        } elsif ($token->{tag_name} eq 'xmp') {        } elsif ($token->{tag_name} eq 'xmp') {
2895          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
2896                    $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
           
         $self->{content_model_flag} = 'CDATA';  
           
         !!!next-token;  
2897          return;          return;
2898        } elsif ($token->{tag_name} eq 'table') {        } elsif ($token->{tag_name} eq 'table') {
2899          ## has a p element in scope          ## has a p element in scope
# Line 2612  sub _tree_construction_main ($) { Line 2925  sub _tree_construction_main ($) {
2925            !!!parse-error (type => 'image');            !!!parse-error (type => 'image');
2926            $token->{tag_name} = 'img';            $token->{tag_name} = 'img';
2927          }          }
2928            
2929            ## NOTE: There is an "as if <br>" code clone.
2930          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
2931                    
2932          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
# Line 2658  sub _tree_construction_main ($) { Line 2972  sub _tree_construction_main ($) {
2972            return;            return;
2973          } else {          } else {
2974            my $at = $token->{attributes};            my $at = $token->{attributes};
2975              my $form_attrs;
2976              $form_attrs->{action} = $at->{action} if $at->{action};
2977              my $prompt_attr = $at->{prompt};
2978            $at->{name} = {name => 'name', value => 'isindex'};            $at->{name} = {name => 'name', value => 'isindex'};
2979              delete $at->{action};
2980              delete $at->{prompt};
2981            my @tokens = (            my @tokens = (
2982                          {type => 'start tag', tag_name => 'form'},                          {type => 'start tag', tag_name => 'form',
2983                             attributes => $form_attrs},
2984                          {type => 'start tag', tag_name => 'hr'},                          {type => 'start tag', tag_name => 'hr'},
2985                          {type => 'start tag', tag_name => 'p'},                          {type => 'start tag', tag_name => 'p'},
2986                          {type => 'start tag', tag_name => 'label'},                          {type => 'start tag', tag_name => 'label'},
2987                          {type => 'character',                         );
2988                           data => 'This is a searchable index. Insert your search keywords here: '}, # SHOULD            if ($prompt_attr) {
2989                          ## TODO: make this configurable              push @tokens, {type => 'character', data => $prompt_attr->{value}};
2990              } else {
2991                push @tokens, {type => 'character',
2992                               data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD
2993                ## TODO: make this configurable
2994              }
2995              push @tokens,
2996                          {type => 'start tag', tag_name => 'input', attributes => $at},                          {type => 'start tag', tag_name => 'input', attributes => $at},
2997                          #{type => 'character', data => ''}, # SHOULD                          #{type => 'character', data => ''}, # SHOULD
2998                          {type => 'end tag', tag_name => 'label'},                          {type => 'end tag', tag_name => 'label'},
2999                          {type => 'end tag', tag_name => 'p'},                          {type => 'end tag', tag_name => 'p'},
3000                          {type => 'start tag', tag_name => 'hr'},                          {type => 'start tag', tag_name => 'hr'},
3001                          {type => 'end tag', tag_name => 'form'},                          {type => 'end tag', tag_name => 'form'};
                        );  
3002            $token = shift @tokens;            $token = shift @tokens;
3003            !!!back-token (@tokens);            !!!back-token (@tokens);
3004            return;            return;
3005          }          }
3006        } elsif ({        } elsif ($token->{tag_name} eq 'textarea') {
                 textarea => 1,  
                 iframe => 1,  
                 noembed => 1,  
                 noframes => 1,  
                 noscript => 0, ## TODO: 1 if scripting is enabled  
                }->{$token->{tag_name}}) {  
3007          my $tag_name = $token->{tag_name};          my $tag_name = $token->{tag_name};
3008          my $el;          my $el;
3009          !!!create-element ($el, $token->{tag_name}, $token->{attributes});          !!!create-element ($el, $token->{tag_name}, $token->{attributes});
3010                    
3011          if ($token->{tag_name} eq 'textarea') {          ## TODO: $self->{form_element} if defined
3012            ## TODO: $self->{form_element} if defined          $self->{content_model} = RCDATA_CONTENT_MODEL;
3013            $self->{content_model_flag} = 'RCDATA';          delete $self->{escape}; # MUST
         } else {  
           $self->{content_model_flag} = 'CDATA';  
         }  
3014                    
3015          $insert->($el);          $insert->($el);
3016                    
3017          my $text = '';          my $text = '';
3018          !!!next-token;          !!!next-token;
3019            if ($token->{type} eq 'character') {
3020              $token->{data} =~ s/^\x0A//;
3021              unless (length $token->{data}) {
3022                !!!next-token;
3023              }
3024            }
3025          while ($token->{type} eq 'character') {          while ($token->{type} eq 'character') {
3026            $text .= $token->{data};            $text .= $token->{data};
3027            !!!next-token;            !!!next-token;
# Line 2708  sub _tree_construction_main ($) { Line 3030  sub _tree_construction_main ($) {
3030            $el->manakai_append_text ($text);            $el->manakai_append_text ($text);
3031          }          }
3032                    
3033          $self->{content_model_flag} = 'PCDATA';          $self->{content_model} = PCDATA_CONTENT_MODEL;
3034                    
3035          if ($token->{type} eq 'end tag' and          if ($token->{type} eq 'end tag' and
3036              $token->{tag_name} eq $tag_name) {              $token->{tag_name} eq $tag_name) {
3037            ## Ignore the token            ## Ignore the token
3038          } else {          } else {
3039            if ($token->{tag_name} eq 'textarea') {            !!!parse-error (type => 'in RCDATA:#'.$token->{type});
             !!!parse-error (type => 'in CDATA:#'.$token->{type});  
           } else {  
             !!!parse-error (type => 'in RCDATA:#'.$token->{type});  
           }  
           ## ISSUE: And ignore?  
3040          }          }
3041          !!!next-token;          !!!next-token;
3042          return;          return;
3043          } elsif ({
3044                    iframe => 1,
3045                    noembed => 1,
3046                    noframes => 1,
3047                    noscript => 0, ## TODO: 1 if scripting is enabled
3048                   }->{$token->{tag_name}}) {
3049            ## NOTE: There are two "as if in body" code clones.
3050            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
3051            return;
3052        } elsif ($token->{tag_name} eq 'select') {        } elsif ($token->{tag_name} eq 'select') {
3053          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
3054                    
# Line 2753  sub _tree_construction_main ($) { Line 3079  sub _tree_construction_main ($) {
3079        }        }
3080      } elsif ($token->{type} eq 'end tag') {      } elsif ($token->{type} eq 'end tag') {
3081        if ($token->{tag_name} eq 'body') {        if ($token->{tag_name} eq 'body') {
3082          if (@{$self->{open_elements}} > 1 and $self->{open_elements}->[1]->[1] eq 'body') {          if (@{$self->{open_elements}} > 1 and
3083            ## ISSUE: There is an issue in the spec.              $self->{open_elements}->[1]->[1] eq 'body') {
3084            if ($self->{open_elements}->[-1]->[1] ne 'body') {            for (@{$self->{open_elements}}) {
3085              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);              unless ({
3086                           dd => 1, dt => 1, li => 1, p => 1, td => 1,
3087                           th => 1, tr => 1, body => 1, html => 1,
3088                         tbody => 1, tfoot => 1, thead => 1,
3089                        }->{$_->[1]}) {
3090                  !!!parse-error (type => 'not closed:'.$_->[1]);
3091                }
3092            }            }
3093    
3094            $self->{insertion_mode} = 'after body';            $self->{insertion_mode} = 'after body';
3095            !!!next-token;            !!!next-token;
3096            return;            return;
# Line 2786  sub _tree_construction_main ($) { Line 3119  sub _tree_construction_main ($) {
3119                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
3120                  div => 1, dl => 1, fieldset => 1, listing => 1,                  div => 1, dl => 1, fieldset => 1, listing => 1,
3121                  menu => 1, ol => 1, pre => 1, ul => 1,                  menu => 1, ol => 1, pre => 1, ul => 1,
                 form => 1,  
3122                  p => 1,                  p => 1,
3123                  dd => 1, dt => 1, li => 1,                  dd => 1, dt => 1, li => 1,
3124                  button => 1, marquee => 1, object => 1,                  button => 1, marquee => 1, object => 1,
# Line 2803  sub _tree_construction_main ($) { Line 3135  sub _tree_construction_main ($) {
3135                   li => ($token->{tag_name} ne 'li'),                   li => ($token->{tag_name} ne 'li'),
3136                   p => ($token->{tag_name} ne 'p'),                   p => ($token->{tag_name} ne 'p'),
3137                   td => 1, th => 1, tr => 1,                   td => 1, th => 1, tr => 1,
3138                     tbody => 1, tfoot=> 1, thead => 1,
3139                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
3140                !!!back-token;                !!!back-token;
3141                $token = {type => 'end tag',                $token = {type => 'end tag',
# Line 2820  sub _tree_construction_main ($) { Line 3153  sub _tree_construction_main ($) {
3153          } # INSCOPE          } # INSCOPE
3154                    
3155          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
3156            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            if (defined $i) {
3157                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3158              } else {
3159                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3160              }
3161          }          }
3162                    
3163          splice @{$self->{open_elements}}, $i if defined $i;          if (defined $i) {
3164          undef $self->{form_element} if $token->{tag_name} eq 'form';            splice @{$self->{open_elements}}, $i;
3165            } elsif ($token->{tag_name} eq 'p') {
3166              ## As if <p>, then reprocess the current token
3167              my $el;
3168              !!!create-element ($el, 'p');
3169              $insert->($el);
3170            }
3171          $clear_up_to_marker->()          $clear_up_to_marker->()
3172            if {            if {
3173              button => 1, marquee => 1, object => 1,              button => 1, marquee => 1, object => 1,
3174            }->{$token->{tag_name}};            }->{$token->{tag_name}};
3175          !!!next-token;          !!!next-token;
3176          return;          return;
3177          } elsif ($token->{tag_name} eq 'form') {
3178            ## has an element in scope
3179            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3180              my $node = $self->{open_elements}->[$_];
3181              if ($node->[1] eq $token->{tag_name}) {
3182                ## generate implied end tags
3183                if ({
3184                     dd => 1, dt => 1, li => 1, p => 1,
3185                     td => 1, th => 1, tr => 1,
3186                     tbody => 1, tfoot=> 1, thead => 1,
3187                    }->{$self->{open_elements}->[-1]->[1]}) {
3188                  !!!back-token;
3189                  $token = {type => 'end tag',
3190                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3191                  return;
3192                }
3193                last INSCOPE;
3194              } elsif ({
3195                        table => 1, caption => 1, td => 1, th => 1,
3196                        button => 1, marquee => 1, object => 1, html => 1,
3197                       }->{$node->[1]}) {
3198                last INSCOPE;
3199              }
3200            } # INSCOPE
3201            
3202            if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {
3203              pop @{$self->{open_elements}};
3204            } else {
3205              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3206            }
3207    
3208            undef $self->{form_element};
3209            !!!next-token;
3210            return;
3211        } elsif ({        } elsif ({
3212                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
3213                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
# Line 2845  sub _tree_construction_main ($) { Line 3222  sub _tree_construction_main ($) {
3222              if ({              if ({
3223                   dd => 1, dt => 1, li => 1, p => 1,                   dd => 1, dt => 1, li => 1, p => 1,
3224                   td => 1, th => 1, tr => 1,                   td => 1, th => 1, tr => 1,
3225                     tbody => 1, tfoot=> 1, thead => 1,
3226                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
3227                !!!back-token;                !!!back-token;
3228                $token = {type => 'end tag',                $token = {type => 'end tag',
# Line 2876  sub _tree_construction_main ($) { Line 3254  sub _tree_construction_main ($) {
3254                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
3255          $formatting_end_tag->($token->{tag_name});          $formatting_end_tag->($token->{tag_name});
3256          return;          return;
3257          } elsif ($token->{tag_name} eq 'br') {
3258            !!!parse-error (type => 'unmatched end tag:br');
3259    
3260            ## As if <br>
3261            $reconstruct_active_formatting_elements->($insert_to_current);
3262            
3263            my $el;
3264            !!!create-element ($el, 'br');
3265            $insert->($el);
3266            
3267            ## Ignore the token.
3268            !!!next-token;
3269            return;
3270        } elsif ({        } elsif ({
3271                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
3272                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
3273                  tbody => 1, td => 1, tfoot => 1, th => 1,                  tbody => 1, td => 1, tfoot => 1, th => 1,
3274                  thead => 1, tr => 1,                  thead => 1, tr => 1,
3275                  area => 1, basefont => 1, bgsound => 1, br => 1,                  area => 1, basefont => 1, bgsound => 1,
3276                  embed => 1, hr => 1, iframe => 1, image => 1,                  embed => 1, hr => 1, iframe => 1, image => 1,
3277                  img => 1, input => 1, isindex => 1, noembed => 1,                  img => 1, input => 1, isindex => 1, noembed => 1,
3278                  noframes => 1, param => 1, select => 1, spacer => 1,                  noframes => 1, param => 1, select => 1, spacer => 1,
# Line 2908  sub _tree_construction_main ($) { Line 3299  sub _tree_construction_main ($) {
3299              if ({              if ({
3300                   dd => 1, dt => 1, li => 1, p => 1,                   dd => 1, dt => 1, li => 1, p => 1,
3301                   td => 1, th => 1, tr => 1,                   td => 1, th => 1, tr => 1,
3302                     tbody => 1, tfoot=> 1, thead => 1,
3303                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
3304                !!!back-token;                !!!back-token;
3305                $token = {type => 'end tag',                $token = {type => 'end tag',
# Line 2931  sub _tree_construction_main ($) { Line 3323  sub _tree_construction_main ($) {
3323                  #not $phrasing_category->{$node->[1]} and                  #not $phrasing_category->{$node->[1]} and
3324                  ($special_category->{$node->[1]} or                  ($special_category->{$node->[1]} or
3325                   $scoping_category->{$node->[1]})) {                   $scoping_category->{$node->[1]})) {
3326                !!!parse-error (type => 'not closed:'.$node->[1]);                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3327                ## Ignore the token                ## Ignore the token
3328                !!!next-token;                !!!next-token;
3329                last S2;                last S2;
# Line 2951  sub _tree_construction_main ($) { Line 3343  sub _tree_construction_main ($) {
3343    }; # $in_body    }; # $in_body
3344    
3345    B: {    B: {
3346      if ($phase eq 'main') {      if ($token->{type} eq 'DOCTYPE') {
3347        if ($token->{type} eq 'DOCTYPE') {        !!!parse-error (type => 'DOCTYPE in the middle');
3348          !!!parse-error (type => 'in html:#DOCTYPE');        ## Ignore the token
3349          ## Ignore the token        ## Stay in the phase
3350          ## Stay in the phase        !!!next-token;
3351          !!!next-token;        redo B;
3352          redo B;      } elsif ($token->{type} eq 'end-of-file') {
3353        } elsif ($token->{type} eq 'start tag' and        if ($token->{insertion_mode} ne 'trailing end') {
                $token->{tag_name} eq 'html') {  
         ## TODO: unless it is the first start tag token, parse-error  
         my $top_el = $self->{open_elements}->[0]->[0];  
         for my $attr_name (keys %{$token->{attributes}}) {  
           unless ($top_el->has_attribute_ns (undef, $attr_name)) {  
             $top_el->set_attribute_ns  
               (undef, [undef, $attr_name],  
                $token->{attributes}->{$attr_name}->{value});  
           }  
         }  
         !!!next-token;  
         redo B;  
       } elsif ($token->{type} eq 'end-of-file') {  
3354          ## Generate implied end tags          ## Generate implied end tags
3355          if ({          if ({
3356               dd => 1, dt => 1, li => 1, p => 1, td => 1, th => 1, tr => 1,               dd => 1, dt => 1, li => 1, p => 1, td => 1, th => 1, tr => 1,
3357                 tbody => 1, tfoot=> 1, thead => 1,
3358              }->{$self->{open_elements}->[-1]->[1]}) {              }->{$self->{open_elements}->[-1]->[1]}) {
3359            !!!back-token;            !!!back-token;
3360            $token = {type => 'end tag', tag_name => $self->{open_elements}->[-1]->[1]};            $token = {type => 'end tag', tag_name => $self->{open_elements}->[-1]->[1]};
# Line 2990  sub _tree_construction_main ($) { Line 3370  sub _tree_construction_main ($) {
3370            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3371          }          }
3372    
         ## Stop parsing  
         last B;  
   
3373          ## ISSUE: There is an issue in the spec.          ## ISSUE: There is an issue in the spec.
3374          }
3375    
3376          ## Stop parsing
3377          last B;
3378        } elsif ($token->{type} eq 'start tag' and
3379                 $token->{tag_name} eq 'html') {
3380          if ($self->{insertion_mode} eq 'trailing end') {
3381            ## Turn into the main phase
3382            !!!parse-error (type => 'after html:html');
3383            $self->{insertion_mode} = $previous_insertion_mode;
3384          }
3385    
3386    ## ISSUE: "aa<html>" is not a parse error.
3387    ## ISSUE: "<html>" in fragment is not a parse error.
3388          unless ($token->{first_start_tag}) {
3389            !!!parse-error (type => 'not first start tag');
3390          }
3391          my $top_el = $self->{open_elements}->[0]->[0];
3392          for my $attr_name (keys %{$token->{attributes}}) {
3393            unless ($top_el->has_attribute_ns (undef, $attr_name)) {
3394              $top_el->set_attribute_ns
3395                (undef, [undef, $attr_name],
3396                 $token->{attributes}->{$attr_name}->{value});
3397            }
3398          }
3399          !!!next-token;
3400          redo B;
3401        } elsif ($token->{type} eq 'comment') {
3402          my $comment = $self->{document}->create_comment ($token->{data});
3403          if ($self->{insertion_mode} eq 'trailing end') {
3404            $self->{document}->append_child ($comment);
3405          } elsif ($self->{insertion_mode} eq 'after body') {
3406            $self->{open_elements}->[0]->[0]->append_child ($comment);
3407        } else {        } else {
3408          if ($self->{insertion_mode} eq 'before head') {          $self->{open_elements}->[-1]->[0]->append_child ($comment);
3409          }
3410          !!!next-token;
3411          redo B;
3412        } elsif ($self->{insertion_mode} eq 'before head') {
3413            if ($token->{type} eq 'character') {            if ($token->{type} eq 'character') {
3414              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
3415                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
# Line 3011  sub _tree_construction_main ($) { Line 3425  sub _tree_construction_main ($) {
3425              $self->{insertion_mode} = 'in head';              $self->{insertion_mode} = 'in head';
3426              ## reprocess              ## reprocess
3427              redo B;              redo B;
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
3428            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} eq 'start tag') {
3429              my $attr = $token->{tag_name} eq 'head' ? $token->{attributes} : {};              my $attr = $token->{tag_name} eq 'head' ? $token->{attributes} : {};
3430              !!!create-element ($self->{head_element}, 'head', $attr);              !!!create-element ($self->{head_element}, 'head', $attr);
# Line 3034  sub _tree_construction_main ($) { Line 3443  sub _tree_construction_main ($) {
3443              }              }
3444              redo B;              redo B;
3445            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} eq 'end tag') {
3446              if ($token->{tag_name} eq 'html') {              if ({
3447                     head => 1, body => 1, html => 1,
3448                     p => 1, br => 1,
3449                    }->{$token->{tag_name}}) {
3450                ## As if <head>                ## As if <head>
3451                !!!create-element ($self->{head_element}, 'head');                !!!create-element ($self->{head_element}, 'head');
3452                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
# Line 3044  sub _tree_construction_main ($) { Line 3456  sub _tree_construction_main ($) {
3456                redo B;                redo B;
3457              } else {              } else {
3458                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3459                ## Ignore the token                ## Ignore the token ## ISSUE: An issue in the spec.
3460                !!!next-token;                !!!next-token;
3461                redo B;                redo B;
3462              }              }
3463            } else {            } else {
3464              die "$0: $token->{type}: Unknown type";              die "$0: $token->{type}: Unknown type";
3465            }            }
3466          } elsif ($self->{insertion_mode} eq 'in head') {          } elsif ($self->{insertion_mode} eq 'in head' or
3467                     $self->{insertion_mode} eq 'in head noscript' or
3468                     $self->{insertion_mode} eq 'after head') {
3469            if ($token->{type} eq 'character') {            if ($token->{type} eq 'character') {
3470              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
3471                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
# Line 3062  sub _tree_construction_main ($) { Line 3476  sub _tree_construction_main ($) {
3476              }              }
3477                            
3478              #              #
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
3479            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} eq 'start tag') {
3480              if ($token->{tag_name} eq 'title') {              if ({base => ($self->{insertion_mode} eq 'in head' or
3481                ## NOTE: There is an "as if in head" code clone                            $self->{insertion_mode} eq 'after head'),
3482                my $title_el;                   link => 1}->{$token->{tag_name}}) {
3483                !!!create-element ($title_el, 'title', $token->{attributes});                ## NOTE: There is a "as if in head" code clone.
3484                (defined $self->{head_element} ? $self->{head_element} : $self->{open_elements}->[-1]->[0])                if ($self->{insertion_mode} eq 'after head') {
3485                  ->append_child ($title_el);                  !!!parse-error (type => 'after head:'.$token->{tag_name});
3486                $self->{content_model_flag} = 'RCDATA';                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3487                  }
3488                my $text = '';                !!!insert-element ($token->{tag_name}, $token->{attributes});
3489                  pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
3490                  pop @{$self->{open_elements}}
3491                      if $self->{insertion_mode} eq 'after head';
3492                !!!next-token;                !!!next-token;
3493                while ($token->{type} eq 'character') {                redo B;
3494                  $text .= $token->{data};              } elsif ($token->{tag_name} eq 'meta') {
3495                  !!!next-token;                ## NOTE: There is a "as if in head" code clone.
3496                  if ($self->{insertion_mode} eq 'after head') {
3497                    !!!parse-error (type => 'after head:'.$token->{tag_name});
3498                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3499                }                }
3500                if (length $text) {                !!!insert-element ($token->{tag_name}, $token->{attributes});
3501                  $title_el->manakai_append_text ($text);                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
3502    
3503                  unless ($self->{confident}) {
3504                    my $charset;
3505                    if ($token->{attributes}->{charset}) { ## TODO: And if supported
3506                      $charset = $token->{attributes}->{charset}->{value};
3507                    }
3508                    if ($token->{attributes}->{'http-equiv'}) {
3509                      ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.
3510                      if ($token->{attributes}->{'http-equiv'}->{value}
3511                          =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=
3512                              [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
3513                              ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
3514                        $charset = defined $1 ? $1 : defined $2 ? $2 : $3;
3515                      } ## TODO: And if supported
3516                    }
3517                    ## TODO: Change the encoding
3518                }                }
3519                  
3520                $self->{content_model_flag} = 'PCDATA';                ## TODO: Extracting |charset| from |meta|.
3521                                pop @{$self->{open_elements}}
3522                if ($token->{type} eq 'end tag' and                    if $self->{insertion_mode} eq 'after head';
3523                    $token->{tag_name} eq 'title') {                !!!next-token;
3524                  redo B;
3525                } elsif ($token->{tag_name} eq 'title' and
3526                         $self->{insertion_mode} eq 'in head') {
3527                  ## NOTE: There is a "as if in head" code clone.
3528                  if ($self->{insertion_mode} eq 'after head') {
3529                    !!!parse-error (type => 'after head:'.$token->{tag_name});
3530                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3531                  }
3532                  my $parent = defined $self->{head_element} ? $self->{head_element}
3533                      : $self->{open_elements}->[-1]->[0];
3534                  $parse_rcdata->(RCDATA_CONTENT_MODEL,
3535                                  sub { $parent->append_child ($_[0]) });
3536                  pop @{$self->{open_elements}}
3537                      if $self->{insertion_mode} eq 'after head';
3538                  redo B;
3539                } elsif ($token->{tag_name} eq 'style') {
3540                  ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
3541                  ## insertion mode 'in head')
3542                  ## NOTE: There is a "as if in head" code clone.
3543                  if ($self->{insertion_mode} eq 'after head') {
3544                    !!!parse-error (type => 'after head:'.$token->{tag_name});
3545                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3546                  }
3547                  $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);
3548                  pop @{$self->{open_elements}}
3549                      if $self->{insertion_mode} eq 'after head';
3550                  redo B;
3551                } elsif ($token->{tag_name} eq 'noscript') {
3552                  if ($self->{insertion_mode} eq 'in head') {
3553                    ## NOTE: and scripting is disalbed
3554                    !!!insert-element ($token->{tag_name}, $token->{attributes});
3555                    $self->{insertion_mode} = 'in head noscript';
3556                    !!!next-token;
3557                    redo B;
3558                  } elsif ($self->{insertion_mode} eq 'in head noscript') {
3559                    !!!parse-error (type => 'in noscript:noscript');
3560                  ## Ignore the token                  ## Ignore the token
3561                    !!!next-token;
3562                    redo B;
3563                } else {                } else {
3564                  !!!parse-error (type => 'in RCDATA:#'.$token->{type});                  #
                 ## ISSUE: And ignore?  
3565                }                }
3566                } elsif ($token->{tag_name} eq 'head' and
3567                         $self->{insertion_mode} ne 'after head') {
3568                  !!!parse-error (type => 'in head:head'); # or in head noscript
3569                  ## Ignore the token
3570                !!!next-token;                !!!next-token;
3571                redo B;                redo B;
3572              } elsif ($token->{tag_name} eq 'style') {              } elsif ($self->{insertion_mode} ne 'in head noscript' and
3573                $style_start_tag->();                       $token->{tag_name} eq 'script') {
3574                redo B;                if ($self->{insertion_mode} eq 'after head') {
3575              } elsif ($token->{tag_name} eq 'script') {                  !!!parse-error (type => 'after head:'.$token->{tag_name});
3576                $script_start_tag->();                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3577                  }
3578                  ## NOTE: There is a "as if in head" code clone.
3579                  $script_start_tag->($insert_to_current);
3580                  pop @{$self->{open_elements}}
3581                      if $self->{insertion_mode} eq 'after head';
3582                redo B;                redo B;
3583              } elsif ({base => 1, link => 1, meta => 1}->{$token->{tag_name}}) {              } elsif ($self->{insertion_mode} eq 'after head' and
3584                ## NOTE: There are "as if in head" code clones                       $token->{tag_name} eq 'body') {
3585                my $el;                !!!insert-element ('body', $token->{attributes});
3586                !!!create-element ($el, $token->{tag_name}, $token->{attributes});                $self->{insertion_mode} = 'in body';
               (defined $self->{head_element} ? $self->{head_element} : $self->{open_elements}->[-1]->[0])  
                 ->append_child ($el);  
   
3587                !!!next-token;                !!!next-token;
3588                redo B;                redo B;
3589              } elsif ($token->{tag_name} eq 'head') {              } elsif ($self->{insertion_mode} eq 'after head' and
3590                !!!parse-error (type => 'in head:head');                       $token->{tag_name} eq 'frameset') {
3591                ## Ignore the token                !!!insert-element ('frameset', $token->{attributes});
3592                  $self->{insertion_mode} = 'in frameset';
3593                !!!next-token;                !!!next-token;
3594                redo B;                redo B;
3595              } else {              } else {
3596                #                #
3597              }              }
3598            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} eq 'end tag') {
3599              if ($token->{tag_name} eq 'head') {              if ($self->{insertion_mode} eq 'in head' and
3600                if ($self->{open_elements}->[-1]->[1] eq 'head') {                  $token->{tag_name} eq 'head') {
3601                  pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
               } else {  
                 !!!parse-error (type => 'unmatched end tag:head');  
               }  
3602                $self->{insertion_mode} = 'after head';                $self->{insertion_mode} = 'after head';
3603                !!!next-token;                !!!next-token;
3604                redo B;                redo B;
3605              } elsif ($token->{tag_name} eq 'html') {              } elsif ($self->{insertion_mode} eq 'in head noscript' and
3606                    $token->{tag_name} eq 'noscript') {
3607                  pop @{$self->{open_elements}};
3608                  $self->{insertion_mode} = 'in head';
3609                  !!!next-token;
3610                  redo B;
3611                } elsif ($self->{insertion_mode} eq 'in head' and
3612                         {
3613                          body => 1, html => 1,
3614                          p => 1, br => 1,
3615                         }->{$token->{tag_name}}) {
3616                #                #
3617              } else {              } elsif ($self->{insertion_mode} eq 'in head noscript' and
3618                         {
3619                          p => 1, br => 1,
3620                         }->{$token->{tag_name}}) {
3621                  #
3622                } elsif ($self->{insertion_mode} ne 'after head') {
3623                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3624                ## Ignore the token                ## Ignore the token
3625                !!!next-token;                !!!next-token;
3626                redo B;                redo B;
3627                } else {
3628                  #
3629              }              }
3630            } else {            } else {
3631              #              #
3632            }            }
3633    
3634            if ($self->{open_elements}->[-1]->[1] eq 'head') {            ## As if </head> or </noscript> or <body>
3635              ## As if </head>            if ($self->{insertion_mode} eq 'in head') {
3636              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
3637                $self->{insertion_mode} = 'after head';
3638              } elsif ($self->{insertion_mode} eq 'in head noscript') {
3639                pop @{$self->{open_elements}};
3640                !!!parse-error (type => 'in noscript:'.(defined $token->{tag_name} ? ($token->{type} eq 'end tag' ? '/' : '') . $token->{tag_name} : '#' . $token->{type}));
3641                $self->{insertion_mode} = 'in head';
3642              } else { # 'after head'
3643                !!!insert-element ('body');
3644                $self->{insertion_mode} = 'in body';
3645            }            }
           $self->{insertion_mode} = 'after head';  
3646            ## reprocess            ## reprocess
3647            redo B;            redo B;
3648    
3649            ## ISSUE: An issue in the spec.            ## ISSUE: An issue in the spec.
3650          } elsif ($self->{insertion_mode} eq 'after head') {          } elsif ($self->{insertion_mode} eq 'in body' or
3651            if ($token->{type} eq 'character') {                   $self->{insertion_mode} eq 'in cell' or
3652              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {                   $self->{insertion_mode} eq 'in caption') {
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
               
             #  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'body') {  
               !!!insert-element ('body', $token->{attributes});  
               $self->{insertion_mode} = 'in body';  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'frameset') {  
               !!!insert-element ('frameset', $token->{attributes});  
               $self->{insertion_mode} = 'in frameset';  
               !!!next-token;  
               redo B;  
             } elsif ({  
                       base => 1, link => 1, meta => 1,  
                       script => 1, style => 1, title => 1,  
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'after head:'.$token->{tag_name});  
               $self->{insertion_mode} = 'in head';  
               ## reprocess  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
             
           ## As if <body>  
           !!!insert-element ('body');  
           $self->{insertion_mode} = 'in body';  
           ## reprocess  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'in body') {  
3653            if ($token->{type} eq 'character') {            if ($token->{type} eq 'character') {
3654              ## NOTE: There is a code clone of "character in body".              ## NOTE: There is a code clone of "character in body".
3655              $reconstruct_active_formatting_elements->($insert_to_current);              $reconstruct_active_formatting_elements->($insert_to_current);
# Line 3207  sub _tree_construction_main ($) { Line 3658  sub _tree_construction_main ($) {
3658    
3659              !!!next-token;              !!!next-token;
3660              redo B;              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});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } else {  
             $in_body->($insert_to_current);  
             redo B;  
           }  
         } elsif ($self->{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]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
                 
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
   
             !!!parse-error (type => 'in table:#character');  
   
             ## As if in body, but insert into foster parent element  
             ## ISSUE: Spec says that "whenever a node would be inserted  
             ## into the current node" while characters might not be  
             ## result in a new Text node.  
             $reconstruct_active_formatting_elements->($insert_to_foster);  
               
             if ({  
                  table => 1, tbody => 1, tfoot => 1,  
                  thead => 1, tr => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               # MUST  
               my $foster_parent_element;  
               my $next_sibling;  
               my $prev_sibling;  
               OE: for (reverse 0..$#{$self->{open_elements}}) {  
                 if ($self->{open_elements}->[$_]->[1] eq 'table') {  
                   my $parent = $self->{open_elements}->[$_]->[0]->parent_node;  
                   if (defined $parent and $parent->node_type == 1) {  
                     $foster_parent_element = $parent;  
                     $next_sibling = $self->{open_elements}->[$_]->[0];  
                     $prev_sibling = $next_sibling->previous_sibling;  
                   } else {  
                     $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];  
                     $prev_sibling = $foster_parent_element->last_child;  
                   }  
                   last OE;  
                 }  
               } # OE  
               $foster_parent_element = $self->{open_elements}->[0]->[0] and  
               $prev_sibling = $foster_parent_element->last_child  
                 unless defined $foster_parent_element;  
               if (defined $prev_sibling and  
                   $prev_sibling->node_type == 3) {  
                 $prev_sibling->manakai_append_text ($token->{data});  
               } else {  
                 $foster_parent_element->insert_before  
                   ($self->{document}->create_text_node ($token->{data}),  
                    $next_sibling);  
               }  
             } else {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
             }  
               
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
3661            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} eq 'start tag') {
3662              if ({              if ({
3663                   caption => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
3664                   colgroup => 1,                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,
                  tbody => 1, tfoot => 1, thead => 1,  
3665                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
3666                ## Clear back to table context                if ($self->{insertion_mode} eq 'in cell') {
3667                while ($self->{open_elements}->[-1]->[1] ne 'table' and                  ## have an element in table scope
3668                       $self->{open_elements}->[-1]->[1] ne 'html') {                  my $tn;
3669                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3670                  pop @{$self->{open_elements}};                    my $node = $self->{open_elements}->[$_];
3671                }                    if ($node->[1] eq 'td' or $node->[1] eq 'th') {
3672                        $tn = $node->[1];
3673                push @$active_formatting_elements, ['#marker', '']                      last INSCOPE;
3674                  if $token->{tag_name} eq 'caption';                    } elsif ({
3675                                table => 1, html => 1,
3676                !!!insert-element ($token->{tag_name}, $token->{attributes});                             }->{$node->[1]}) {
3677                $self->{insertion_mode} = {                      last INSCOPE;
3678                                   caption => 'in caption',                    }
3679                                   colgroup => 'in column group',                  } # INSCOPE
3680                                   tbody => 'in table body',                    unless (defined $tn) {
3681                                   tfoot => 'in table body',                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3682                                   thead => 'in table body',                      ## Ignore the token
3683                                  }->{$token->{tag_name}};                      !!!next-token;
3684                !!!next-token;                      redo B;
3685                redo B;                    }
3686              } elsif ({                  
3687                        col => 1,                  ## Close the cell
3688                        td => 1, th => 1, tr => 1,                  !!!back-token; # <?>
3689                       }->{$token->{tag_name}}) {                  $token = {type => 'end tag', tag_name => $tn};
3690                ## Clear back to table context                  redo B;
3691                while ($self->{open_elements}->[-1]->[1] ne 'table' and                } elsif ($self->{insertion_mode} eq 'in caption') {
3692                       $self->{open_elements}->[-1]->[1] ne 'html') {                  !!!parse-error (type => 'not closed:caption');
3693                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  
3694                  pop @{$self->{open_elements}};                  ## As if </caption>
3695                }                  ## have a table element in table scope
3696                    my $i;
3697                !!!insert-element ($token->{tag_name} eq 'col' ? 'colgroup' : 'tbody');                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3698                $self->{insertion_mode} = $token->{tag_name} eq 'col'                    my $node = $self->{open_elements}->[$_];
3699                  ? 'in column group' : 'in table body';                    if ($node->[1] eq 'caption') {
3700                ## reprocess                      $i = $_;
3701                redo B;                      last INSCOPE;
3702              } elsif ($token->{tag_name} eq 'table') {                    } elsif ({
3703                ## NOTE: There are code clones for this "table in table"                              table => 1, html => 1,
3704                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                             }->{$node->[1]}) {
3705                        last INSCOPE;
3706                      }
3707                    } # INSCOPE
3708                      unless (defined $i) {
3709                        !!!parse-error (type => 'unmatched end tag:caption');
3710                        ## Ignore the token
3711                        !!!next-token;
3712                        redo B;
3713                      }
3714                    
3715                    ## generate implied end tags
3716                    if ({
3717                         dd => 1, dt => 1, li => 1, p => 1,
3718                         td => 1, th => 1, tr => 1,
3719                         tbody => 1, tfoot=> 1, thead => 1,
3720                        }->{$self->{open_elements}->[-1]->[1]}) {
3721                      !!!back-token; # <?>
3722                      $token = {type => 'end tag', tag_name => 'caption'};
3723                      !!!back-token;
3724                      $token = {type => 'end tag',
3725                                tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3726                      redo B;
3727                    }
3728    
3729                ## As if </table>                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {
3730                ## have a table element in table scope                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'table') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
3731                  }                  }
3732                } # INSCOPE                  
3733                unless (defined $i) {                  splice @{$self->{open_elements}}, $i;
3734                  !!!parse-error (type => 'unmatched end tag:table');                  
3735                  ## Ignore tokens </table><table>                  $clear_up_to_marker->();
3736                  !!!next-token;                  
3737                  redo B;                  $self->{insertion_mode} = 'in table';
3738                }                  
3739                                  ## reprocess
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token; # <table>  
                 $token = {type => 'end tag', tag_name => 'table'};  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
3740                  redo B;                  redo B;
3741                  } else {
3742                    #
3743                }                }
   
               if ($self->{open_elements}->[-1]->[1] ne 'table') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               }  
   
               splice @{$self->{open_elements}}, $i;  
   
               $self->_reset_insertion_mode;  
   
               ## reprocess  
               redo B;  
3744              } else {              } else {
3745                #                #
3746              }              }
3747            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} eq 'end tag') {
3748              if ($token->{tag_name} eq 'table') {              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {
3749                ## have a table element in table scope                if ($self->{insertion_mode} eq 'in cell') {
3750                my $i;                  ## have an element in table scope
3751                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  my $i;
3752                  my $node = $self->{open_elements}->[$_];                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3753                  if ($node->[1] eq $token->{tag_name}) {                    my $node = $self->{open_elements}->[$_];
3754                    $i = $_;                    if ($node->[1] eq $token->{tag_name}) {
3755                    last INSCOPE;                      $i = $_;
3756                  } elsif ({                      last INSCOPE;
3757                            table => 1, html => 1,                    } elsif ({
3758                           }->{$node->[1]}) {                              table => 1, html => 1,
3759                    last INSCOPE;                             }->{$node->[1]}) {
3760                        last INSCOPE;
3761                      }
3762                    } # INSCOPE
3763                      unless (defined $i) {
3764                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3765                        ## Ignore the token
3766                        !!!next-token;
3767                        redo B;
3768                      }
3769                    
3770                    ## generate implied end tags
3771                    if ({
3772                         dd => 1, dt => 1, li => 1, p => 1,
3773                         td => ($token->{tag_name} eq 'th'),
3774                         th => ($token->{tag_name} eq 'td'),
3775                         tr => 1,
3776                         tbody => 1, tfoot=> 1, thead => 1,
3777                        }->{$self->{open_elements}->[-1]->[1]}) {
3778                      !!!back-token;
3779                      $token = {type => 'end tag',
3780                                tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3781                      redo B;
3782                  }                  }
3783                } # INSCOPE                  
3784                unless (defined $i) {                  if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
3785                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3786                    }
3787                    
3788                    splice @{$self->{open_elements}}, $i;
3789                    
3790                    $clear_up_to_marker->();
3791                    
3792                    $self->{insertion_mode} = 'in row';
3793                    
3794                    !!!next-token;
3795                    redo B;
3796                  } elsif ($self->{insertion_mode} eq 'in caption') {
3797                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3798                  ## Ignore the token                  ## Ignore the token
3799                  !!!next-token;                  !!!next-token;
3800                  redo B;                  redo B;
3801                  } else {
3802                    #
3803                }                }
3804                              } elsif ($token->{tag_name} eq 'caption') {
3805                ## generate implied end tags                if ($self->{insertion_mode} eq 'in caption') {
3806                if ({                  ## have a table element in table scope
3807                     dd => 1, dt => 1, li => 1, p => 1,                  my $i;
3808                     td => 1, th => 1, tr => 1,                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3809                    }->{$self->{open_elements}->[-1]->[1]}) {                    my $node = $self->{open_elements}->[$_];
3810                  !!!back-token;                    if ($node->[1] eq $token->{tag_name}) {
3811                  $token = {type => 'end tag',                      $i = $_;
3812                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                      last INSCOPE;
3813                  redo B;                    } elsif ({
3814                }                              table => 1, html => 1,
3815                               }->{$node->[1]}) {
3816                if ($self->{open_elements}->[-1]->[1] ne 'table') {                      last INSCOPE;
3817                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    }
3818                }                  } # INSCOPE
3819                      unless (defined $i) {
3820                splice @{$self->{open_elements}}, $i;                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3821                        ## Ignore the token
3822                $self->_reset_insertion_mode;                      !!!next-token;
3823                        redo B;
3824                !!!next-token;                    }
3825                redo B;                  
3826              } elsif ({                  ## generate implied end tags
3827                        body => 1, caption => 1, col => 1, colgroup => 1,                  if ({
3828                        html => 1, tbody => 1, td => 1, tfoot => 1, th => 1,                       dd => 1, dt => 1, li => 1, p => 1,
3829                        thead => 1, tr => 1,                       td => 1, th => 1, tr => 1,
3830                       }->{$token->{tag_name}}) {                       tbody => 1, tfoot=> 1, thead => 1,
3831                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      }->{$self->{open_elements}->[-1]->[1]}) {
3832                ## Ignore the token                    !!!back-token;
3833                !!!next-token;                    $token = {type => 'end tag',
3834                redo B;                              tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3835              } else {                    redo B;
               #  
             }  
           } else {  
             #  
           }  
   
           !!!parse-error (type => 'in table:'.$token->{tag_name});  
           $in_body->($insert_to_foster);  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'in caption') {  
           if ($token->{type} eq 'character') {  
             ## NOTE: This is a code clone of "character in body".  
             $reconstruct_active_formatting_elements->($insert_to_current);  
               
             $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
   
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             ## NOTE: This is a code clone of "comment in body".  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ({  
                  caption => 1, col => 1, colgroup => 1, tbody => 1,  
                  td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,  
                 }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'not closed:caption');  
   
               ## As if </caption>  
               ## have a table element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'caption') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
3836                  }                  }
3837                } # INSCOPE                  
3838                unless (defined $i) {                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {
3839                  !!!parse-error (type => 'unmatched end tag:caption');                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3840                  ## Ignore the token                  }
3841                    
3842                    splice @{$self->{open_elements}}, $i;
3843                    
3844                    $clear_up_to_marker->();
3845                    
3846                    $self->{insertion_mode} = 'in table';
3847                    
3848                  !!!next-token;                  !!!next-token;
3849                  redo B;                  redo B;
3850                }                } elsif ($self->{insertion_mode} eq 'in cell') {
3851                                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3852                ## generate implied end tags                  ## Ignore the token
3853                if ({                  !!!next-token;
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token; # <?>  
                 $token = {type => 'end tag', tag_name => 'caption'};  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
3854                  redo B;                  redo B;
3855                  } else {
3856                    #
3857                }                }
3858                } elsif ({
3859                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                        table => 1, tbody => 1, tfoot => 1,
3860                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                        thead => 1, tr => 1,
3861                }                       }->{$token->{tag_name}} and
3862                         $self->{insertion_mode} eq 'in cell') {
3863                splice @{$self->{open_elements}}, $i;                ## have an element in table scope
   
               $clear_up_to_marker->();  
   
               $self->{insertion_mode} = 'in table';  
   
               ## reprocess  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'caption') {  
               ## have a table element in table scope  
3864                my $i;                my $i;
3865                  my $tn;
3866                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3867                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
3868                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq $token->{tag_name}) {
3869                    $i = $_;                    $i = $_;
3870                    last INSCOPE;                    last INSCOPE;
3871                    } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {
3872                      $tn = $node->[1];
3873                      ## NOTE: There is exactly one |td| or |th| element
3874                      ## in scope in the stack of open elements by definition.
3875                  } elsif ({                  } elsif ({
3876                            table => 1, html => 1,                            table => 1, html => 1,
3877                           }->{$node->[1]}) {                           }->{$node->[1]}) {
# Line 3528  sub _tree_construction_main ($) { Line 3884  sub _tree_construction_main ($) {
3884                  !!!next-token;                  !!!next-token;
3885                  redo B;                  redo B;
3886                }                }
                 
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
   
               if ($self->{open_elements}->[-1]->[1] ne 'caption') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               }  
   
               splice @{$self->{open_elements}}, $i;  
3887    
3888                $clear_up_to_marker->();                ## Close the cell
3889                  !!!back-token; # </?>
3890                $self->{insertion_mode} = 'in table';                $token = {type => 'end tag', tag_name => $tn};
   
               !!!next-token;  
3891                redo B;                redo B;
3892              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table' and
3893                         $self->{insertion_mode} eq 'in caption') {
3894                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed:caption');
3895    
3896                ## As if </caption>                ## As if </caption>
# Line 3580  sub _tree_construction_main ($) { Line 3918  sub _tree_construction_main ($) {
3918                if ({                if ({
3919                     dd => 1, dt => 1, li => 1, p => 1,                     dd => 1, dt => 1, li => 1, p => 1,
3920                     td => 1, th => 1, tr => 1,                     td => 1, th => 1, tr => 1,
3921                       tbody => 1, tfoot=> 1, thead => 1,
3922                    }->{$self->{open_elements}->[-1]->[1]}) {                    }->{$self->{open_elements}->[-1]->[1]}) {
3923                  !!!back-token; # </table>                  !!!back-token; # </table>
3924                  $token = {type => 'end tag', tag_name => 'caption'};                  $token = {type => 'end tag', tag_name => 'caption'};
# Line 3602  sub _tree_construction_main ($) { Line 3941  sub _tree_construction_main ($) {
3941                ## reprocess                ## reprocess
3942                redo B;                redo B;
3943              } elsif ({              } elsif ({
3944                        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,  
3945                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3946                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                if ($self->{insertion_mode} eq 'in cell' or
3947                ## Ignore the token                    $self->{insertion_mode} eq 'in caption') {
3948                redo B;                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
             } else {  
               #  
             }  
           } else {  
             #  
           }  
                 
           $in_body->($insert_to_current);  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'in column group') {  
           if ($token->{type} eq 'character') {  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
               
             #  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'col') {  
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               pop @{$self->{open_elements}};  
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'colgroup') {  
               if ($self->{open_elements}->[-1]->[1] eq 'html') {  
                 !!!parse-error (type => 'unmatched end tag:colgroup');  
3949                  ## Ignore the token                  ## Ignore the token
3950                  !!!next-token;                  !!!next-token;
3951                  redo B;                  redo B;
3952                } else {                } else {
3953                  pop @{$self->{open_elements}}; # colgroup                  #
                 $self->{insertion_mode} = 'in table';  
                 !!!next-token;  
                 redo B;              
3954                }                }
3955              } elsif ($token->{tag_name} eq 'col') {              } elsif ({
3956                !!!parse-error (type => 'unmatched end tag:col');                        tbody => 1, tfoot => 1,
3957                          thead => 1, tr => 1,
3958                         }->{$token->{tag_name}} and
3959                         $self->{insertion_mode} eq 'in caption') {
3960                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3961                ## Ignore the token                ## Ignore the token
3962                !!!next-token;                !!!next-token;
3963                redo B;                redo B;
3964              } else {              } else {
3965                #                #
3966              }              }
3967            } else {            } else {
3968              #              #
3969            }            }
3970              
3971            ## As if </colgroup>            $in_body->($insert_to_current);
3972            if ($self->{open_elements}->[-1]->[1] eq 'html') {            redo B;
3973              !!!parse-error (type => 'unmatched end tag:colgroup');          } elsif ($self->{insertion_mode} eq 'in table body' or
3974              ## Ignore the token                   $self->{insertion_mode} eq 'in table') {
             !!!next-token;  
             redo B;  
           } else {  
             pop @{$self->{open_elements}}; # colgroup  
             $self->{insertion_mode} = 'in table';  
             ## reprocess  
             redo B;  
           }  
         } elsif ($self->{insertion_mode} eq 'in table body') {  
3975            if ($token->{type} eq 'character') {            if ($token->{type} eq 'character') {
3976              ## NOTE: This is a "character in table" code clone.              ## NOTE: There are "character in table" code clones.
3977              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
3978                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
3979                                
# Line 3699  sub _tree_construction_main ($) { Line 3990  sub _tree_construction_main ($) {
3990              ## into the current node" while characters might not be              ## into the current node" while characters might not be
3991              ## result in a new Text node.              ## result in a new Text node.
3992              $reconstruct_active_formatting_elements->($insert_to_foster);              $reconstruct_active_formatting_elements->($insert_to_foster);
3993                
3994              if ({              if ({
3995                   table => 1, tbody => 1, tfoot => 1,                   table => 1, tbody => 1, tfoot => 1,
3996                   thead => 1, tr => 1,                   thead => 1, tr => 1,
# Line 3739  sub _tree_construction_main ($) { Line 4030  sub _tree_construction_main ($) {
4030                            
4031              !!!next-token;              !!!next-token;
4032              redo B;              redo B;
           } elsif ($token->{type} eq 'comment') {  
             ## Copied from 'in table'  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
4033            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} eq 'start tag') {
4034              if ({              if ({
4035                   tr => 1,                   tr => 1,
4036                   th => 1, td => 1,                   th => 1, td => 1,
4037                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
4038                  if ($self->{insertion_mode} eq 'in table') {
4039                    ## Clear back to table context
4040                    while ($self->{open_elements}->[-1]->[1] ne 'table' and
4041                           $self->{open_elements}->[-1]->[1] ne 'html') {
4042                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4043                      pop @{$self->{open_elements}};
4044                    }
4045                    
4046                    !!!insert-element ('tbody');
4047                    $self->{insertion_mode} = 'in table body';
4048                    ## reprocess in the "in table body" insertion mode...
4049                  }
4050    
4051                unless ($token->{tag_name} eq 'tr') {                unless ($token->{tag_name} eq 'tr') {
4052                  !!!parse-error (type => 'missing start tag:tr');                  !!!parse-error (type => 'missing start tag:tr');
4053                }                }
# Line 3774  sub _tree_construction_main ($) { Line 4072  sub _tree_construction_main ($) {
4072              } elsif ({              } elsif ({
4073                        caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
4074                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
4075                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}} and
4076                         $self->{insertion_mode} eq 'in table body') {
4077                ## have an element in table scope                ## have an element in table scope
4078                my $i;                my $i;
4079                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 3816  sub _tree_construction_main ($) { Line 4115  sub _tree_construction_main ($) {
4115                $self->{insertion_mode} = 'in table';                $self->{insertion_mode} = 'in table';
4116                ## reprocess                ## reprocess
4117                redo B;                redo B;
4118                } elsif ($token->{tag_name} eq 'col' and
4119                         $self->{insertion_mode} eq 'in table') {
4120                  ## Clear back to table context
4121                  while ($self->{open_elements}->[-1]->[1] ne 'table' and
4122                         $self->{open_elements}->[-1]->[1] ne 'html') {
4123                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4124                    pop @{$self->{open_elements}};
4125                  }
4126    
4127                  !!!insert-element ('colgroup');
4128                  $self->{insertion_mode} = 'in column group';
4129                  ## reprocess
4130                  redo B;
4131                } elsif ({
4132                          caption => 1,
4133                          colgroup => 1,
4134                          tbody => 1, tfoot => 1, thead => 1,
4135                         }->{$token->{tag_name}} and
4136                        $self->{insertion_mode} eq 'in table') {
4137                  ## Clear back to table context
4138                  while ($self->{open_elements}->[-1]->[1] ne 'table' and
4139                         $self->{open_elements}->[-1]->[1] ne 'html') {
4140                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4141                    pop @{$self->{open_elements}};
4142                  }
4143    
4144                  push @$active_formatting_elements, ['#marker', '']
4145                    if $token->{tag_name} eq 'caption';
4146    
4147                  !!!insert-element ($token->{tag_name}, $token->{attributes});
4148                  $self->{insertion_mode} = {
4149                                     caption => 'in caption',
4150                                     colgroup => 'in column group',
4151                                     tbody => 'in table body',
4152                                     tfoot => 'in table body',
4153                                     thead => 'in table body',
4154                                    }->{$token->{tag_name}};
4155                  !!!next-token;
4156                  redo B;
4157              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
4158                ## NOTE: This is a code clone of "table in table"                ## NOTE: There are code clones for this "table in table"
4159                !!!parse-error (type => 'not closed:table');                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4160    
4161                ## As if </table>                ## As if </table>
4162                ## have a table element in table scope                ## have a table element in table scope
# Line 3845  sub _tree_construction_main ($) { Line 4183  sub _tree_construction_main ($) {
4183                if ({                if ({
4184                     dd => 1, dt => 1, li => 1, p => 1,                     dd => 1, dt => 1, li => 1, p => 1,
4185                     td => 1, th => 1, tr => 1,                     td => 1, th => 1, tr => 1,
4186                       tbody => 1, tfoot=> 1, thead => 1,
4187                    }->{$self->{open_elements}->[-1]->[1]}) {                    }->{$self->{open_elements}->[-1]->[1]}) {
4188                  !!!back-token; # <table>                  !!!back-token; # <table>
4189                  $token = {type => 'end tag', tag_name => 'table'};                  $token = {type => 'end tag', tag_name => 'table'};
# Line 3860  sub _tree_construction_main ($) { Line 4199  sub _tree_construction_main ($) {
4199    
4200                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
4201    
4202                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
4203    
4204                ## reprocess                ## reprocess
4205                redo B;                redo B;
# Line 3868  sub _tree_construction_main ($) { Line 4207  sub _tree_construction_main ($) {
4207                #                #
4208              }              }
4209            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} eq 'end tag') {
4210              if ({              if ($token->{tag_name} eq 'table') {
4211                   tbody => 1, tfoot => 1, thead => 1,                if ($self->{insertion_mode} eq 'in table body') {
4212                  }->{$token->{tag_name}}) {                  ## have an element in table scope
4213                ## have an element in table scope                  my $i;
4214                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4215                      my $node = $self->{open_elements}->[$_];
4216                      if ({
4217                           tbody => 1, thead => 1, tfoot => 1,
4218                          }->{$node->[1]}) {
4219                        $i = $_;
4220                        last INSCOPE;
4221                      } elsif ({
4222                                table => 1, html => 1,
4223                               }->{$node->[1]}) {
4224                        last INSCOPE;
4225                      }
4226                    } # INSCOPE
4227                    unless (defined $i) {
4228                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4229                      ## Ignore the token
4230                      !!!next-token;
4231                      redo B;
4232                    }
4233                    
4234                    ## Clear back to table body context
4235                    while (not {
4236                      tbody => 1, tfoot => 1, thead => 1, html => 1,
4237                    }->{$self->{open_elements}->[-1]->[1]}) {
4238                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4239                      pop @{$self->{open_elements}};
4240                    }
4241                    
4242                    ## As if <{current node}>
4243                    ## have an element in table scope
4244                    ## true by definition
4245                    
4246                    ## Clear back to table body context
4247                    ## nop by definition
4248                    
4249                    pop @{$self->{open_elements}};
4250                    $self->{insertion_mode} = 'in table';
4251                    ## reprocess in the "in table" insertion mode...
4252                  }
4253    
4254                  ## have a table element in table scope
4255                my $i;                my $i;
4256                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4257                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
# Line 3891  sub _tree_construction_main ($) { Line 4271  sub _tree_construction_main ($) {
4271                  redo B;                  redo B;
4272                }                }
4273    
4274                ## Clear back to table body context                ## generate implied end tags
4275                while (not {                if ({
4276                  tbody => 1, tfoot => 1, thead => 1, html => 1,                     dd => 1, dt => 1, li => 1, p => 1,
4277                }->{$self->{open_elements}->[-1]->[1]}) {                     td => 1, th => 1, tr => 1,
4278                       tbody => 1, tfoot=> 1, thead => 1,
4279                      }->{$self->{open_elements}->[-1]->[1]}) {
4280                    !!!back-token;
4281                    $token = {type => 'end tag',
4282                              tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
4283                    redo B;
4284                  }
4285                  
4286                  if ($self->{open_elements}->[-1]->[1] ne 'table') {
4287                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
                 pop @{$self->{open_elements}};  
4288                }                }
4289                    
4290                pop @{$self->{open_elements}};                splice @{$self->{open_elements}}, $i;
4291                $self->{insertion_mode} = 'in table';                
4292                  $self->_reset_insertion_mode;
4293                  
4294                !!!next-token;                !!!next-token;
4295                redo B;                redo B;
4296              } elsif ($token->{tag_name} eq 'table') {              } elsif ({
4297                          tbody => 1, tfoot => 1, thead => 1,
4298                         }->{$token->{tag_name}} and
4299                         $self->{insertion_mode} eq 'in table body') {
4300                ## have an element in table scope                ## have an element in table scope
4301                my $i;                my $i;
4302                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4303                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4304                  if ({                  if ($node->[1] eq $token->{tag_name}) {
                      tbody => 1, thead => 1, tfoot => 1,  
                     }->{$node->[1]}) {  
4305                    $i = $_;                    $i = $_;
4306                    last INSCOPE;                    last INSCOPE;
4307                  } elsif ({                  } elsif ({
# Line 3934  sub _tree_construction_main ($) { Line 4325  sub _tree_construction_main ($) {
4325                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4326                }                }
4327    
               ## As if <{current node}>  
               ## have an element in table scope  
               ## true by definition  
   
               ## Clear back to table body context  
               ## nop by definition  
   
4328                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4329                $self->{insertion_mode} = 'in table';                $self->{insertion_mode} = 'in table';
4330                ## reprocess                !!!next-token;
4331                redo B;                redo B;
4332              } elsif ({              } elsif ({
4333                        body => 1, caption => 1, col => 1, colgroup => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
4334                        html => 1, td => 1, th => 1, tr => 1,                        html => 1, td => 1, th => 1, tr => 1,
4335                          tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} eq 'in table'
4336                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4337                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4338                ## Ignore the token                ## Ignore the token
# Line 3957  sub _tree_construction_main ($) { Line 4342  sub _tree_construction_main ($) {
4342                #                #
4343              }              }
4344            } else {            } else {
4345              #              die "$0: $token->{type}: Unknown token type";
4346            }            }
4347              
           ## As if in table  
4348            !!!parse-error (type => 'in table:'.$token->{tag_name});            !!!parse-error (type => 'in table:'.$token->{tag_name});
4349            $in_body->($insert_to_foster);            $in_body->($insert_to_foster);
4350            redo B;            redo B;
4351            } elsif ($self->{insertion_mode} eq 'in column group') {
4352              if ($token->{type} eq 'character') {
4353                if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4354                  $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4355                  unless (length $token->{data}) {
4356                    !!!next-token;
4357                    redo B;
4358                  }
4359                }
4360                
4361                #
4362              } elsif ($token->{type} eq 'start tag') {
4363                if ($token->{tag_name} eq 'col') {
4364                  !!!insert-element ($token->{tag_name}, $token->{attributes});
4365                  pop @{$self->{open_elements}};
4366                  !!!next-token;
4367                  redo B;
4368                } else {
4369                  #
4370                }
4371              } elsif ($token->{type} eq 'end tag') {
4372                if ($token->{tag_name} eq 'colgroup') {
4373                  if ($self->{open_elements}->[-1]->[1] eq 'html') {
4374                    !!!parse-error (type => 'unmatched end tag:colgroup');
4375                    ## Ignore the token
4376                    !!!next-token;
4377                    redo B;
4378                  } else {
4379                    pop @{$self->{open_elements}}; # colgroup
4380                    $self->{insertion_mode} = 'in table';
4381                    !!!next-token;
4382                    redo B;            
4383                  }
4384                } elsif ($token->{tag_name} eq 'col') {
4385                  !!!parse-error (type => 'unmatched end tag:col');
4386                  ## Ignore the token
4387                  !!!next-token;
4388                  redo B;
4389                } else {
4390                  #
4391                }
4392              } else {
4393                #
4394              }
4395    
4396              ## As if </colgroup>
4397              if ($self->{open_elements}->[-1]->[1] eq 'html') {
4398                !!!parse-error (type => 'unmatched end tag:colgroup');
4399                ## Ignore the token
4400                !!!next-token;
4401                redo B;
4402              } else {
4403                pop @{$self->{open_elements}}; # colgroup
4404                $self->{insertion_mode} = 'in table';
4405                ## reprocess
4406                redo B;
4407              }
4408          } elsif ($self->{insertion_mode} eq 'in row') {          } elsif ($self->{insertion_mode} eq 'in row') {
4409            if ($token->{type} eq 'character') {            if ($token->{type} eq 'character') {
4410              ## NOTE: This is a "character in table" code clone.              ## NOTE: This is a "character in table" code clone.
# Line 4023  sub _tree_construction_main ($) { Line 4464  sub _tree_construction_main ($) {
4464                            
4465              !!!next-token;              !!!next-token;
4466              redo B;              redo B;
           } elsif ($token->{type} eq 'comment') {  
             ## Copied from 'in table'  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
4467            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} eq 'start tag') {
4468              if ($token->{tag_name} eq 'th' or              if ($token->{tag_name} eq 'th' or
4469                  $token->{tag_name} eq 'td') {                  $token->{tag_name} eq 'td') {
# Line 4113  sub _tree_construction_main ($) { Line 4548  sub _tree_construction_main ($) {
4548                if ({                if ({
4549                     dd => 1, dt => 1, li => 1, p => 1,                     dd => 1, dt => 1, li => 1, p => 1,
4550                     td => 1, th => 1, tr => 1,                     td => 1, th => 1, tr => 1,
4551                       tbody => 1, tfoot=> 1, thead => 1,
4552                    }->{$self->{open_elements}->[-1]->[1]}) {                    }->{$self->{open_elements}->[-1]->[1]}) {
4553                  !!!back-token; # <table>                  !!!back-token; # <table>
4554                  $token = {type => 'end tag', tag_name => 'table'};                  $token = {type => 'end tag', tag_name => 'table'};
# Line 4278  sub _tree_construction_main ($) { Line 4714  sub _tree_construction_main ($) {
4714            !!!parse-error (type => 'in table:'.$token->{tag_name});            !!!parse-error (type => 'in table:'.$token->{tag_name});
4715            $in_body->($insert_to_foster);            $in_body->($insert_to_foster);
4716            redo B;            redo B;
         } elsif ($self->{insertion_mode} eq 'in cell') {  
           if ($token->{type} eq 'character') {  
             ## NOTE: This is a code clone of "character in body".  
             $reconstruct_active_formatting_elements->($insert_to_current);  
               
             $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
   
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             ## NOTE: This is a code clone of "comment in body".  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ({  
                  caption => 1, col => 1, colgroup => 1,  
                  tbody => 1, td => 1, tfoot => 1, th => 1,  
                  thead => 1, tr => 1,  
                 }->{$token->{tag_name}}) {  
               ## have an element in table scope  
               my $tn;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'td' or $node->[1] eq 'th') {  
                   $tn = $node->[1];  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $tn) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Close the cell  
               !!!back-token; # <?>  
               $token = {type => 'end tag', tag_name => $tn};  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
                 
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => ($token->{tag_name} eq 'th'),  
                    th => ($token->{tag_name} eq 'td'),  
                    tr => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
   
               if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               }  
   
               splice @{$self->{open_elements}}, $i;  
   
               $clear_up_to_marker->();  
   
               $self->{insertion_mode} = 'in row';  
   
               !!!next-token;  
               redo B;  
             } elsif ({  
                       body => 1, caption => 1, col => 1,  
                       colgroup => 1, html => 1,  
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
             } elsif ({  
                       table => 1, tbody => 1, tfoot => 1,  
                       thead => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               ## have an element in table scope  
               my $i;  
               my $tn;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {  
                   $tn = $node->[1];  
                   ## NOTE: There is exactly one |td| or |th| element  
                   ## in scope in the stack of open elements by definition.  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Close the cell  
               !!!back-token; # </?>  
               $token = {type => 'end tag', tag_name => $tn};  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
             
           $in_body->($insert_to_current);  
           redo B;  
4717          } elsif ($self->{insertion_mode} eq 'in select') {          } elsif ($self->{insertion_mode} eq 'in select') {
4718            if ($token->{type} eq 'character') {            if ($token->{type} eq 'character') {
4719              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4720              !!!next-token;              !!!next-token;
4721              redo B;              redo B;
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
4722            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} eq 'start tag') {
4723              if ($token->{tag_name} eq 'option') {              if ($token->{tag_name} eq 'option') {
4724                if ($self->{open_elements}->[-1]->[1] eq 'option') {                if ($self->{open_elements}->[-1]->[1] eq 'option') {
# Line 4605  sub _tree_construction_main ($) { Line 4891  sub _tree_construction_main ($) {
4891          } elsif ($self->{insertion_mode} eq 'after body') {          } elsif ($self->{insertion_mode} eq 'after body') {
4892            if ($token->{type} eq 'character') {            if ($token->{type} eq 'character') {
4893              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4894                  my $data = $1;
4895                ## As if in body                ## As if in body
4896                $reconstruct_active_formatting_elements->($insert_to_current);                $reconstruct_active_formatting_elements->($insert_to_current);
4897                                
4898                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4899    
4900                unless (length $token->{data}) {                unless (length $token->{data}) {
4901                  !!!next-token;                  !!!next-token;
# Line 4617  sub _tree_construction_main ($) { Line 4904  sub _tree_construction_main ($) {
4904              }              }
4905                            
4906              #              #
4907              !!!parse-error (type => 'after body:#'.$token->{type});              !!!parse-error (type => 'after body:#character');
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[0]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
4908            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} eq 'start tag') {
4909              !!!parse-error (type => 'after body:'.$token->{tag_name});              !!!parse-error (type => 'after body:'.$token->{tag_name});
4910              #              #
# Line 4634  sub _tree_construction_main ($) { Line 4916  sub _tree_construction_main ($) {
4916                  !!!next-token;                  !!!next-token;
4917                  redo B;                  redo B;
4918                } else {                } else {
4919                  $phase = 'trailing end';                  $previous_insertion_mode = $self->{insertion_mode};
4920                    $self->{insertion_mode} = 'trailing end';
4921                  !!!next-token;                  !!!next-token;
4922                  redo B;                  redo B;
4923                }                }
# Line 4642  sub _tree_construction_main ($) { Line 4925  sub _tree_construction_main ($) {
4925                !!!parse-error (type => 'after body:/'.$token->{tag_name});                !!!parse-error (type => 'after body:/'.$token->{tag_name});
4926              }              }
4927            } else {            } else {
4928              !!!parse-error (type => 'after body:#'.$token->{type});              die "$0: $token->{type}: Unknown token type";
4929            }            }
4930    
4931            $self->{insertion_mode} = 'in body';            $self->{insertion_mode} = 'in body';
4932            ## reprocess            ## reprocess
4933            redo B;            redo B;
4934          } elsif ($self->{insertion_mode} eq 'in frameset') {      } elsif ($self->{insertion_mode} eq 'in frameset') {
4935            if ($token->{type} eq 'character') {        if ($token->{type} eq 'character') {
4936              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4937                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
   
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
4938    
4939              #            unless (length $token->{data}) {
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
4940              !!!next-token;              !!!next-token;
4941              redo B;              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 @{$self->{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 ($self->{open_elements}->[-1]->[1] eq 'html' and  
                   @{$self->{open_elements}} == 1) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
               } else {  
                 pop @{$self->{open_elements}};  
                 !!!next-token;  
               }  
                 
               ## if not inner_html and  
               if ($self->{open_elements}->[-1]->[1] ne 'frameset') {  
                 $self->{insertion_mode} = 'after frameset';  
               }  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
4942            }            }
4943                      }
4944            if (defined $token->{tag_name}) {  
4945              !!!parse-error (type => 'in frameset:'.$token->{tag_name});          !!!parse-error (type => 'in frameset:#character');
4946            ## Ignore the token
4947            !!!next-token;
4948            redo B;
4949          } elsif ($token->{type} eq 'start tag') {
4950            if ($token->{tag_name} eq 'frameset') {
4951              !!!insert-element ($token->{tag_name}, $token->{attributes});
4952              !!!next-token;
4953              redo B;
4954            } elsif ($token->{tag_name} eq 'frame') {
4955              !!!insert-element ($token->{tag_name}, $token->{attributes});
4956              pop @{$self->{open_elements}};
4957              !!!next-token;
4958              redo B;
4959            } elsif ($token->{tag_name} eq 'noframes') {
4960              ## NOTE: As if in body.
4961              $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);
4962              redo B;
4963            } else {
4964              !!!parse-error (type => 'in frameset:'.$token->{tag_name});
4965              ## Ignore the token
4966              !!!next-token;
4967              redo B;
4968            }
4969          } elsif ($token->{type} eq 'end tag') {
4970            if ($token->{tag_name} eq 'frameset') {
4971              if ($self->{open_elements}->[-1]->[1] eq 'html' and
4972                  @{$self->{open_elements}} == 1) {
4973                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4974                ## Ignore the token
4975                !!!next-token;
4976            } else {            } else {
4977              !!!parse-error (type => 'in frameset:#'.$token->{type});              pop @{$self->{open_elements}};
4978                !!!next-token;
4979              }
4980    
4981              if (not defined $self->{inner_html_node} and
4982                  $self->{open_elements}->[-1]->[1] ne 'frameset') {
4983                $self->{insertion_mode} = 'after frameset';
4984            }            }
4985              redo B;
4986            } else {
4987              !!!parse-error (type => 'in frameset:/'.$token->{tag_name});
4988            ## Ignore the token            ## Ignore the token
4989            !!!next-token;            !!!next-token;
4990            redo B;            redo B;
4991          } elsif ($self->{insertion_mode} eq 'after frameset') {          }
4992            if ($token->{type} eq 'character') {        } else {
4993            die "$0: $token->{type}: Unknown token type";
4994          }
4995        } elsif ($self->{insertion_mode} eq 'after frameset') {
4996          if ($token->{type} eq 'character') {
4997              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4998                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4999    
5000                unless (length $token->{data}) {                unless (length $token->{data}) {
5001                  !!!next-token;                  !!!next-token;
# Line 4724  sub _tree_construction_main ($) { Line 5003  sub _tree_construction_main ($) {
5003                }                }
5004              }              }
5005    
5006              #              if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
5007            } elsif ($token->{type} eq 'comment') {                !!!parse-error (type => 'after frameset:#character');
5008              my $comment = $self->{document}->create_comment ($token->{data});  
5009              $self->{open_elements}->[-1]->[0]->append_child ($comment);                ## Ignore the token.
5010              !!!next-token;                if (length $token->{data}) {
5011              redo B;                  ## reprocess the rest of characters
5012            } elsif ($token->{type} eq 'start tag') {                } else {
5013              if ($token->{tag_name} eq 'noframes') {                  !!!next-token;
5014                $in_body->($insert_to_current);                }
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'html') {  
               $phase = 'trailing end';  
               !!!next-token;  
5015                redo B;                redo B;
             } else {  
               #  
5016              }              }
5017            } else {  
5018              #          die qq[$0: Character "$token->{data}"];
5019            }        } elsif ($token->{type} eq 'start tag') {
5020                      if ($token->{tag_name} eq 'noframes') {
5021            if (defined $token->{tag_name}) {            ## NOTE: As if in body.
5022              !!!parse-error (type => 'after frameset:'.$token->{tag_name});            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);
5023            } else {            redo B;
5024              !!!parse-error (type => 'after frameset:#'.$token->{type});          } else {
5025            }            !!!parse-error (type => 'after frameset:'.$token->{tag_name});
5026            ## Ignore the token            ## Ignore the token
5027            !!!next-token;            !!!next-token;
5028            redo B;            redo B;
5029            }
5030            ## ISSUE: An issue in spec there        } elsif ($token->{type} eq 'end tag') {
5031            if ($token->{tag_name} eq 'html') {
5032              $previous_insertion_mode = $self->{insertion_mode};
5033              $self->{insertion_mode} = 'trailing end';
5034              !!!next-token;
5035              redo B;
5036          } else {          } else {
5037            die "$0: $self->{insertion_mode}: Unknown insertion mode";            !!!parse-error (type => 'after frameset:/'.$token->{tag_name});
5038              ## Ignore the token
5039              !!!next-token;
5040              redo B;
5041          }          }
5042          } else {
5043            die "$0: $token->{type}: Unknown token type";
5044        }        }
5045      } elsif ($phase eq 'trailing end') {  
5046          ## ISSUE: An issue in spec here
5047        } elsif ($self->{insertion_mode} eq 'trailing end') {
5048        ## states in the main stage is preserved yet # MUST        ## states in the main stage is preserved yet # MUST
5049                
5050        if ($token->{type} eq 'DOCTYPE') {        if ($token->{type} eq 'character') {
         !!!parse-error (type => 'after html:#DOCTYPE');  
         ## Ignore the token  
         !!!next-token;  
         redo B;  
       } elsif ($token->{type} eq 'comment') {  
         my $comment = $self->{document}->create_comment ($token->{data});  
         $self->{document}->append_child ($comment);  
         !!!next-token;  
         redo B;  
       } elsif ($token->{type} eq 'character') {  
5051          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5052            my $data = $1;            my $data = $1;
5053            ## As if in the main phase.            ## As if in the main phase.
5054            ## NOTE: The insertion mode in the main phase            ## NOTE: The insertion mode in the main phase
5055            ## just before the phase has been changed to the trailing            ## just before the phase has been changed to the trailing
5056            ## end phase is either "after body" or "after frameset".            ## end phase is either "after body" or "after frameset".
5057            $reconstruct_active_formatting_elements->($insert_to_current)            $reconstruct_active_formatting_elements->($insert_to_current);
             if $phase eq 'main';  
5058                        
5059            $self->{open_elements}->[-1]->[0]->manakai_append_text ($data);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($data);
5060                        
# Line 4795  sub _tree_construction_main ($) { Line 5065  sub _tree_construction_main ($) {
5065          }          }
5066    
5067          !!!parse-error (type => 'after html:#character');          !!!parse-error (type => 'after html:#character');
5068          $phase = 'main';          $self->{insertion_mode} = $previous_insertion_mode;
5069          ## reprocess          ## reprocess
5070          redo B;          redo B;
5071        } elsif ($token->{type} eq 'start tag' or        } elsif ($token->{type} eq 'start tag') {
                $token->{type} eq 'end tag') {  
5072          !!!parse-error (type => 'after html:'.$token->{tag_name});          !!!parse-error (type => 'after html:'.$token->{tag_name});
5073          $phase = 'main';          $self->{insertion_mode} = $previous_insertion_mode;
5074            ## reprocess
5075            redo B;
5076          } elsif ($token->{type} eq 'end tag') {
5077            !!!parse-error (type => 'after html:/'.$token->{tag_name});
5078            $self->{insertion_mode} = $previous_insertion_mode;
5079          ## reprocess          ## reprocess
5080          redo B;          redo B;
       } elsif ($token->{type} eq 'end-of-file') {  
         ## Stop parsing  
         last B;  
5081        } else {        } else {
5082          die "$0: $token->{type}: Unknown token";          die "$0: $token->{type}: Unknown token";
5083        }        }
5084        } else {
5085          die "$0: $self->{insertion_mode}: Unknown insertion mode";
5086      }      }
5087    } # B    } # B
5088    
# Line 4846  sub set_inner_html ($$$) { Line 5119  sub set_inner_html ($$$) {
5119      ## NOTE: Most of this code is copied from |parse_string|      ## NOTE: Most of this code is copied from |parse_string|
5120    
5121      ## Step 1 # MUST      ## Step 1 # MUST
5122      my $doc = $node->owner_document->implementation->create_document;      my $this_doc = $node->owner_document;
5123      ## TODO: Mark as HTML document      my $doc = $this_doc->implementation->create_document;
5124        $doc->manakai_is_html (1);
5125      my $p = $class->new;      my $p = $class->new;
5126      $p->{document} = $doc;      $p->{document} = $doc;
5127    
# Line 4857  sub set_inner_html ($$$) { Line 5131  sub set_inner_html ($$$) {
5131      my $column = 0;      my $column = 0;
5132      $p->{set_next_input_character} = sub {      $p->{set_next_input_character} = sub {
5133        my $self = shift;        my $self = shift;
5134    
5135          pop @{$self->{prev_input_character}};
5136          unshift @{$self->{prev_input_character}}, $self->{next_input_character};
5137    
5138        $self->{next_input_character} = -1 and return if $i >= length $$s;        $self->{next_input_character} = -1 and return if $i >= length $$s;
5139        $self->{next_input_character} = ord substr $$s, $i++, 1;        $self->{next_input_character} = ord substr $$s, $i++, 1;
5140        $column++;        $column++;
# Line 4865  sub set_inner_html ($$$) { Line 5143  sub set_inner_html ($$$) {
5143          $line++;          $line++;
5144          $column = 0;          $column = 0;
5145        } elsif ($self->{next_input_character} == 0x000D) { # CR        } elsif ($self->{next_input_character} == 0x000D) { # CR
5146          if ($i >= length $$s) {          $i++ if substr ($$s, $i, 1) eq "\x0A";
           #  
         } else {  
           my $next_char = ord substr $$s, $i++, 1;  
           if ($next_char == 0x000A) { # LF  
             #  
           } else {  
             push @{$self->{char}}, $next_char;  
           }  
         }  
5147          $self->{next_input_character} = 0x000A; # LF # MUST          $self->{next_input_character} = 0x000A; # LF # MUST
5148          $line++;          $line++;
5149          $column = 0;          $column = 0;
5150        } elsif ($self->{next_input_character} > 0x10FFFF) {        } elsif ($self->{next_input_character} > 0x10FFFF) {
5151          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
5152        } elsif ($self->{next_input_character} == 0x0000) { # NULL        } elsif ($self->{next_input_character} == 0x0000) { # NULL
5153            !!!parse-error (type => 'NULL');
5154          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
5155        }        }
5156      };      };
5157        $p->{prev_input_character} = [-1, -1, -1];
5158        $p->{next_input_character} = -1;
5159            
5160      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
5161        my (%opt) = @_;        my (%opt) = @_;
# Line 4898  sub set_inner_html ($$$) { Line 5170  sub set_inner_html ($$$) {
5170    
5171      ## Step 2      ## Step 2
5172      my $node_ln = $node->local_name;      my $node_ln = $node->local_name;
5173      $p->{content_model_flag} = {      $p->{content_model} = {
5174        title => 'RCDATA',        title => RCDATA_CONTENT_MODEL,
5175        textarea => 'RCDATA',        textarea => RCDATA_CONTENT_MODEL,
5176        style => 'CDATA',        style => CDATA_CONTENT_MODEL,
5177        script => 'CDATA',        script => CDATA_CONTENT_MODEL,
5178        xmp => 'CDATA',        xmp => CDATA_CONTENT_MODEL,
5179        iframe => 'CDATA',        iframe => CDATA_CONTENT_MODEL,
5180        noembed => 'CDATA',        noembed => CDATA_CONTENT_MODEL,
5181        noframes => 'CDATA',        noframes => CDATA_CONTENT_MODEL,
5182        noscript => 'CDATA',        noscript => CDATA_CONTENT_MODEL,
5183        plaintext => 'PLAINTEXT',        plaintext => PLAINTEXT_CONTENT_MODEL,
5184      }->{$node_ln} || 'PCDATA';      }->{$node_ln};
5185         ## ISSUE: What is "the name of the element"? local name?      $p->{content_model} = PCDATA_CONTENT_MODEL
5186            unless defined $p->{content_model};
5187            ## ISSUE: What is "the name of the element"? local name?
5188    
5189      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $node_ln];
5190    
# Line 4962  sub set_inner_html ($$$) { Line 5236  sub set_inner_html ($$$) {
5236      ## Step 12 # MUST      ## Step 12 # MUST
5237      @cn = @{$root->child_nodes};      @cn = @{$root->child_nodes};
5238      for (@cn) {      for (@cn) {
5239          $this_doc->adopt_node ($_);
5240        $node->append_child ($_);        $node->append_child ($_);
5241      }      }
5242      ## ISSUE: adopt_node? mutation events?      ## ISSUE: mutation events?
5243    
5244      $p->_terminate_tree_constructor;      $p->_terminate_tree_constructor;
5245    } else {    } else {
# Line 5009  sub get_inner_html ($$$) { Line 5284  sub get_inner_html ($$$) {
5284            
5285      my $nt = $child->node_type;      my $nt = $child->node_type;
5286      if ($nt == 1) { # Element      if ($nt == 1) { # Element
5287        my $tag_name = lc $child->tag_name; ## ISSUE: Definition of "lowercase"        my $tag_name = $child->tag_name; ## TODO: manakai_tag_name
5288        $s .= '<' . $tag_name;        $s .= '<' . $tag_name;
5289          ## NOTE: Non-HTML case:
5290        ## ISSUE: Non-html elements        ## <http://permalink.gmane.org/gmane.org.w3c.whatwg.discuss/11191>
5291    
5292        my @attrs = @{$child->attributes}; # sort order MUST be stable        my @attrs = @{$child->attributes}; # sort order MUST be stable
5293        for my $attr (@attrs) { # order is implementation dependent        for my $attr (@attrs) { # order is implementation dependent
5294          my $attr_name = lc $attr->name; ## ISSUE: Definition of "lowercase"          my $attr_name = $attr->name; ## TODO: manakai_name
5295          $s .= ' ' . $attr_name . '="';          $s .= ' ' . $attr_name . '="';
5296          my $attr_value = $attr->value;          my $attr_value = $attr->value;
5297          ## escape          ## escape
# Line 5035  sub get_inner_html ($$$) { Line 5310  sub get_inner_html ($$$) {
5310          spacer => 1, wbr => 1,          spacer => 1, wbr => 1,
5311        }->{$tag_name};        }->{$tag_name};
5312    
5313          $s .= "\x0A" if $tag_name eq 'pre' or $tag_name eq 'textarea';
5314    
5315        if (not $in_cdata and {        if (not $in_cdata and {
5316          style => 1, script => 1, xmp => 1, iframe => 1,          style => 1, script => 1, xmp => 1, iframe => 1,
5317          noembed => 1, noframes => 1, noscript => 1,          noembed => 1, noframes => 1, noscript => 1,
5318            plaintext => 1,
5319        }->{$tag_name}) {        }->{$tag_name}) {
5320          unshift @node, 'cdata-out';          unshift @node, 'cdata-out';
5321          $in_cdata = 1;          $in_cdata = 1;

Legend:
Removed from v.1.7  
changed lines
  Added in v.1.46

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24