/[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.10 by wakaba, Sat Jun 23 03:30:06 2007 UTC revision 1.28 by wakaba, Mon Jun 25 00:14:40 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  my $permitted_slash_tag_name = {  my $permitted_slash_tag_name = {
11    base => 1,    base => 1,
# Line 18  my $permitted_slash_tag_name = { Line 21  my $permitted_slash_tag_name = {
21    input => 1,    input => 1,
22  };  };
23    
 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  
   
24  my $c1_entity_char = {  my $c1_entity_char = {
25    0x80 => 0x20AC,    0x80 => 0x20AC,
26    0x81 => 0xFFFD,    0x81 => 0xFFFD,
# Line 349  sub parse_string ($$$;$) { Line 90  sub parse_string ($$$;$) {
90    my $column = 0;    my $column = 0;
91    $self->{set_next_input_character} = sub {    $self->{set_next_input_character} = sub {
92      my $self = shift;      my $self = shift;
93    
94        pop @{$self->{prev_input_character}};
95        unshift @{$self->{prev_input_character}}, $self->{next_input_character};
96    
97      $self->{next_input_character} = -1 and return if $i >= length $$s;      $self->{next_input_character} = -1 and return if $i >= length $$s;
98      $self->{next_input_character} = ord substr $$s, $i++, 1;      $self->{next_input_character} = ord substr $$s, $i++, 1;
99      $column++;      $column++;
# Line 357  sub parse_string ($$$;$) { Line 102  sub parse_string ($$$;$) {
102        $line++;        $line++;
103        $column = 0;        $column = 0;
104      } elsif ($self->{next_input_character} == 0x000D) { # CR      } elsif ($self->{next_input_character} == 0x000D) { # CR
105        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;  
         }  
       }  
106        $self->{next_input_character} = 0x000A; # LF # MUST        $self->{next_input_character} = 0x000A; # LF # MUST
107        $line++;        $line++;
108        $column = 0;        $column = 0;
# Line 374  sub parse_string ($$$;$) { Line 110  sub parse_string ($$$;$) {
110        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
111      } elsif ($self->{next_input_character} == 0x0000) { # NULL      } elsif ($self->{next_input_character} == 0x0000) { # NULL
112        !!!parse-error (type => 'NULL');        !!!parse-error (type => 'NULL');
 ## TODO: test  
113        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
114      }      }
115    };    };
116      $self->{prev_input_character} = [-1, -1, -1];
117      $self->{next_input_character} = -1;
118    
119    my $onerror = $_[2] || sub {    my $onerror = $_[2] || sub {
120      my (%opt) = @_;      my (%opt) = @_;
# Line 421  sub _initialize_tokenizer ($) { Line 158  sub _initialize_tokenizer ($) {
158    # $self->{next_input_character}    # $self->{next_input_character}
159    !!!next-input-character;    !!!next-input-character;
160    $self->{token} = [];    $self->{token} = [];
161      # $self->{escape}
162  } # _initialize_tokenizer  } # _initialize_tokenizer
163    
164  ## A token has:  ## A token has:
165  ##   ->{type} eq 'DOCTYPE', 'start tag', 'end tag', 'comment',  ##   ->{type} eq 'DOCTYPE', 'start tag', 'end tag', 'comment',
166  ##       'character', or 'end-of-file'  ##       'character', or 'end-of-file'
167  ##   ->{name} (DOCTYPE, start tag (tagname), end tag (tagname))  ##   ->{name} (DOCTYPE, start tag (tag name), end tag (tag name))
168      ## ISSUE: the spec need s/tagname/tag name/  ##   ->{public_identifier} (DOCTYPE)
169  ##   ->{error} == 1 or 0 (DOCTYPE)  ##   ->{system_identifier} (DOCTYPE)
170    ##   ->{correct} == 1 or 0 (DOCTYPE)
171  ##   ->{attributes} isa HASH (start tag, end tag)  ##   ->{attributes} isa HASH (start tag, end tag)
172  ##   ->{data} (comment, character)  ##   ->{data} (comment, character)
173    
 ## Macros  
 ##   Macros MUST be preceded by three EXCLAMATION MARKs.  
 ##   emit ($token)  
 ##     Emits the specified token.  
   
174  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
175    
176  ## 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 445  sub _initialize_tokenizer ($) { Line 179  sub _initialize_tokenizer ($) {
179  ## has completed loading.  If one has, then it MUST be executed  ## has completed loading.  If one has, then it MUST be executed
180  ## and removed from the list.  ## and removed from the list.
181    
 ## ISSUE: <http://html5.org/tools/web-apps-tracker?from=874&to=876>  
   
182  sub _get_next_token ($) {  sub _get_next_token ($) {
183    my $self = shift;    my $self = shift;
184    if (@{$self->{token}}) {    if (@{$self->{token}}) {
# Line 464  sub _get_next_token ($) { Line 196  sub _get_next_token ($) {
196          } else {          } else {
197            #            #
198          }          }
199          } elsif ($self->{next_input_character} == 0x002D) { # -
200            if ($self->{content_model_flag} eq 'RCDATA' or
201                $self->{content_model_flag} eq 'CDATA') {
202              unless ($self->{escape}) {
203                if ($self->{prev_input_character}->[0] == 0x002D and # -
204                    $self->{prev_input_character}->[1] == 0x0021 and # !
205                    $self->{prev_input_character}->[2] == 0x003C) { # <
206                  $self->{escape} = 1;
207                }
208              }
209            }
210            
211            #
212        } elsif ($self->{next_input_character} == 0x003C) { # <        } elsif ($self->{next_input_character} == 0x003C) { # <
213          if ($self->{content_model_flag} ne 'PLAINTEXT') {          if ($self->{content_model_flag} eq 'PCDATA' or
214                (($self->{content_model_flag} eq 'CDATA' or
215                  $self->{content_model_flag} eq 'RCDATA') and
216                 not $self->{escape})) {
217            $self->{state} = 'tag open';            $self->{state} = 'tag open';
218            !!!next-input-character;            !!!next-input-character;
219            redo A;            redo A;
220          } else {          } else {
221            #            #
222          }          }
223          } elsif ($self->{next_input_character} == 0x003E) { # >
224            if ($self->{escape} and
225                ($self->{content_model_flag} eq 'RCDATA' or
226                 $self->{content_model_flag} eq 'CDATA')) {
227              if ($self->{prev_input_character}->[0] == 0x002D and # -
228                  $self->{prev_input_character}->[1] == 0x002D) { # -
229                delete $self->{escape};
230              }
231            }
232            
233            #
234        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
235          !!!emit ({type => 'end-of-file'});          !!!emit ({type => 'end-of-file'});
236          last A; ## TODO: ok?          last A; ## TODO: ok?
# Line 488  sub _get_next_token ($) { Line 247  sub _get_next_token ($) {
247      } elsif ($self->{state} eq 'entity data') {      } elsif ($self->{state} eq 'entity data') {
248        ## (cannot happen in CDATA state)        ## (cannot happen in CDATA state)
249                
250        my $token = $self->_tokenize_attempt_to_consume_an_entity;        my $token = $self->_tokenize_attempt_to_consume_an_entity (0);
251    
252        $self->{state} = 'data';        $self->{state} = 'data';
253        # next-input-character is already done        # next-input-character is already done
# Line 567  sub _get_next_token ($) { Line 326  sub _get_next_token ($) {
326      } elsif ($self->{state} eq 'close tag open') {      } elsif ($self->{state} eq 'close tag open') {
327        if ($self->{content_model_flag} eq 'RCDATA' or        if ($self->{content_model_flag} eq 'RCDATA' or
328            $self->{content_model_flag} eq 'CDATA') {            $self->{content_model_flag} eq 'CDATA') {
329          my @next_char;          if (defined $self->{last_emitted_start_tag_name}) {
330          TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {            my @next_char;
331              TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {
332                push @next_char, $self->{next_input_character};
333                my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);
334                my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;
335                if ($self->{next_input_character} == $c or $self->{next_input_character} == $C) {
336                  !!!next-input-character;
337                  next TAGNAME;
338                } else {
339                  $self->{next_input_character} = shift @next_char; # reconsume
340                  !!!back-next-input-character (@next_char);
341                  $self->{state} = 'data';
342    
343                  !!!emit ({type => 'character', data => '</'});
344      
345                  redo A;
346                }
347              }
348            push @next_char, $self->{next_input_character};            push @next_char, $self->{next_input_character};
349            my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);        
350            my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;            unless ($self->{next_input_character} == 0x0009 or # HT
351            if ($self->{next_input_character} == $c or $self->{next_input_character} == $C) {                    $self->{next_input_character} == 0x000A or # LF
352              !!!next-input-character;                    $self->{next_input_character} == 0x000B or # VT
353              next TAGNAME;                    $self->{next_input_character} == 0x000C or # FF
354            } else {                    $self->{next_input_character} == 0x0020 or # SP
355              !!!parse-error (type => 'unmatched end tag');                    $self->{next_input_character} == 0x003E or # >
356                      $self->{next_input_character} == 0x002F or # /
357                      $self->{next_input_character} == -1) {
358              $self->{next_input_character} = shift @next_char; # reconsume              $self->{next_input_character} = shift @next_char; # reconsume
359              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
360              $self->{state} = 'data';              $self->{state} = 'data';
   
361              !!!emit ({type => 'character', data => '</'});              !!!emit ({type => 'character', data => '</'});
   
362              redo A;              redo A;
363              } else {
364                $self->{next_input_character} = shift @next_char;
365                !!!back-next-input-character (@next_char);
366                # and consume...
367            }            }
368          }          } else {
369          push @next_char, $self->{next_input_character};            ## No start tag token has ever been emitted
370                  # 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);  
371            $self->{state} = 'data';            $self->{state} = 'data';
   
372            !!!emit ({type => 'character', data => '</'});            !!!emit ({type => 'character', data => '</'});
   
373            redo A;            redo A;
         } else {  
           $self->{next_input_character} = shift @next_char;  
           !!!back-next-input-character (@next_char);  
           # and consume...  
374          }          }
375        }        }
376                
# Line 656  sub _get_next_token ($) { Line 418  sub _get_next_token ($) {
418          redo A;          redo A;
419        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
420          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
421              $self->{current_token}->{first_start_tag}
422                  = not defined $self->{last_emitted_start_tag_name};
423            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
424          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
425            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model_flag} = 'PCDATA'; # MUST
# Line 669  sub _get_next_token ($) { Line 433  sub _get_next_token ($) {
433          !!!next-input-character;          !!!next-input-character;
434    
435          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
436    
437          redo A;          redo A;
438        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_input_character} and
# Line 679  sub _get_next_token ($) { Line 442  sub _get_next_token ($) {
442          ## Stay in this state          ## Stay in this state
443          !!!next-input-character;          !!!next-input-character;
444          redo A;          redo A;
445        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_input_character} == -1) {
                $self->{next_input_character} == -1) {  
446          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
447          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
448              $self->{current_token}->{first_start_tag}
449                  = not defined $self->{last_emitted_start_tag_name};
450            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
451          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
452            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model_flag} = 'PCDATA'; # MUST
# Line 696  sub _get_next_token ($) { Line 460  sub _get_next_token ($) {
460          # reconsume          # reconsume
461    
462          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
463    
464          redo A;          redo A;
465        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_input_character} == 0x002F) { # /
# Line 730  sub _get_next_token ($) { Line 493  sub _get_next_token ($) {
493          redo A;          redo A;
494        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
495          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
496              $self->{current_token}->{first_start_tag}
497                  = not defined $self->{last_emitted_start_tag_name};
498            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
499          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
500            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model_flag} = 'PCDATA'; # MUST
# Line 743  sub _get_next_token ($) { Line 508  sub _get_next_token ($) {
508          !!!next-input-character;          !!!next-input-character;
509    
510          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
511    
512          redo A;          redo A;
513        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_input_character} and
# Line 766  sub _get_next_token ($) { Line 530  sub _get_next_token ($) {
530          ## Stay in the state          ## Stay in the state
531          # next-input-character is already done          # next-input-character is already done
532          redo A;          redo A;
533        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_input_character} == -1) {
                $self->{next_input_character} == -1) {  
534          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
535          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
536              $self->{current_token}->{first_start_tag}
537                  = not defined $self->{last_emitted_start_tag_name};
538            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
539          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
540            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model_flag} = 'PCDATA'; # MUST
# Line 783  sub _get_next_token ($) { Line 548  sub _get_next_token ($) {
548          # reconsume          # reconsume
549    
550          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
551    
552          redo A;          redo A;
553        } else {        } else {
# Line 822  sub _get_next_token ($) { Line 586  sub _get_next_token ($) {
586        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
587          $before_leave->();          $before_leave->();
588          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
589              $self->{current_token}->{first_start_tag}
590                  = not defined $self->{last_emitted_start_tag_name};
591            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
592          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
593            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model_flag} = 'PCDATA'; # MUST
# Line 835  sub _get_next_token ($) { Line 601  sub _get_next_token ($) {
601          !!!next-input-character;          !!!next-input-character;
602    
603          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
604    
605          redo A;          redo A;
606        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_input_character} and
# Line 858  sub _get_next_token ($) { Line 623  sub _get_next_token ($) {
623          $self->{state} = 'before attribute name';          $self->{state} = 'before attribute name';
624          # next-input-character is already done          # next-input-character is already done
625          redo A;          redo A;
626        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_input_character} == -1) {
                $self->{next_input_character} == -1) {  
627          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
628          $before_leave->();          $before_leave->();
629          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
630              $self->{current_token}->{first_start_tag}
631                  = not defined $self->{last_emitted_start_tag_name};
632            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
633          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
634            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model_flag} = 'PCDATA'; # MUST
# Line 876  sub _get_next_token ($) { Line 642  sub _get_next_token ($) {
642          # reconsume          # reconsume
643    
644          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
645    
646          redo A;          redo A;
647        } else {        } else {
# Line 900  sub _get_next_token ($) { Line 665  sub _get_next_token ($) {
665          redo A;          redo A;
666        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
667          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
668              $self->{current_token}->{first_start_tag}
669                  = not defined $self->{last_emitted_start_tag_name};
670            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
671          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
672            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model_flag} = 'PCDATA'; # MUST
# Line 913  sub _get_next_token ($) { Line 680  sub _get_next_token ($) {
680          !!!next-input-character;          !!!next-input-character;
681    
682          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
683    
684          redo A;          redo A;
685        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_input_character} and
# Line 936  sub _get_next_token ($) { Line 702  sub _get_next_token ($) {
702          $self->{state} = 'before attribute name';          $self->{state} = 'before attribute name';
703          # next-input-character is already done          # next-input-character is already done
704          redo A;          redo A;
705        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_input_character} == -1) {
                $self->{next_input_character} == -1) {  
706          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
707          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
708              $self->{current_token}->{first_start_tag}
709                  = not defined $self->{last_emitted_start_tag_name};
710            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
711          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
712            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model_flag} = 'PCDATA'; # MUST
# Line 953  sub _get_next_token ($) { Line 720  sub _get_next_token ($) {
720          # reconsume          # reconsume
721    
722          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
723    
724          redo A;          redo A;
725        } else {        } else {
# Line 986  sub _get_next_token ($) { Line 752  sub _get_next_token ($) {
752          redo A;          redo A;
753        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
754          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
755              $self->{current_token}->{first_start_tag}
756                  = not defined $self->{last_emitted_start_tag_name};
757            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
758          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
759            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model_flag} = 'PCDATA'; # MUST
# Line 999  sub _get_next_token ($) { Line 767  sub _get_next_token ($) {
767          !!!next-input-character;          !!!next-input-character;
768    
769          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
770    
771          redo A;          redo A;
772        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_input_character} == -1) {
                $self->{next_input_character} == -1) {  
773          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
774          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
775              $self->{current_token}->{first_start_tag}
776                  = not defined $self->{last_emitted_start_tag_name};
777            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
778          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
779            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model_flag} = 'PCDATA'; # MUST
# Line 1019  sub _get_next_token ($) { Line 787  sub _get_next_token ($) {
787          ## reconsume          ## reconsume
788    
789          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
790    
791          redo A;          redo A;
792        } else {        } else {
# Line 1041  sub _get_next_token ($) { Line 808  sub _get_next_token ($) {
808        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
809          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
810          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
811              $self->{current_token}->{first_start_tag}
812                  = not defined $self->{last_emitted_start_tag_name};
813            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
814          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
815            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model_flag} = 'PCDATA'; # MUST
# Line 1054  sub _get_next_token ($) { Line 823  sub _get_next_token ($) {
823          ## reconsume          ## reconsume
824    
825          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
826    
827          redo A;          redo A;
828        } else {        } else {
# Line 1076  sub _get_next_token ($) { Line 844  sub _get_next_token ($) {
844        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
845          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
846          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
847              $self->{current_token}->{first_start_tag}
848                  = not defined $self->{last_emitted_start_tag_name};
849            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
850          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
851            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model_flag} = 'PCDATA'; # MUST
# Line 1089  sub _get_next_token ($) { Line 859  sub _get_next_token ($) {
859          ## reconsume          ## reconsume
860    
861          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
862    
863          redo A;          redo A;
864        } else {        } else {
# Line 1114  sub _get_next_token ($) { Line 883  sub _get_next_token ($) {
883          redo A;          redo A;
884        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
885          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
886              $self->{current_token}->{first_start_tag}
887                  = not defined $self->{last_emitted_start_tag_name};
888            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
889          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
890            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model_flag} = 'PCDATA'; # MUST
# Line 1127  sub _get_next_token ($) { Line 898  sub _get_next_token ($) {
898          !!!next-input-character;          !!!next-input-character;
899    
900          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
901    
902          redo A;          redo A;
903        } elsif ($self->{next_input_character} == 0x003C or # <        } elsif ($self->{next_input_character} == -1) {
                $self->{next_input_character} == -1) {  
904          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
905          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
906              $self->{current_token}->{first_start_tag}
907                  = not defined $self->{last_emitted_start_tag_name};
908            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
909          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
910            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model_flag} = 'PCDATA'; # MUST
# Line 1147  sub _get_next_token ($) { Line 918  sub _get_next_token ($) {
918          ## reconsume          ## reconsume
919    
920          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
921    
922          redo A;          redo A;
923        } else {        } else {
# Line 1157  sub _get_next_token ($) { Line 927  sub _get_next_token ($) {
927          redo A;          redo A;
928        }        }
929      } elsif ($self->{state} eq 'entity in attribute value') {      } elsif ($self->{state} eq 'entity in attribute value') {
930        my $token = $self->_tokenize_attempt_to_consume_an_entity;        my $token = $self->_tokenize_attempt_to_consume_an_entity (1);
931    
932        unless (defined $token) {        unless (defined $token) {
933          $self->{current_attribute}->{value} .= '&';          $self->{current_attribute}->{value} .= '&';
# Line 1206  sub _get_next_token ($) { Line 976  sub _get_next_token ($) {
976          push @next_char, $self->{next_input_character};          push @next_char, $self->{next_input_character};
977          if ($self->{next_input_character} == 0x002D) { # -          if ($self->{next_input_character} == 0x002D) { # -
978            $self->{current_token} = {type => 'comment', data => ''};            $self->{current_token} = {type => 'comment', data => ''};
979            $self->{state} = 'comment';            $self->{state} = 'comment start';
980            !!!next-input-character;            !!!next-input-character;
981            redo A;            redo A;
982          }          }
# Line 1256  sub _get_next_token ($) { Line 1026  sub _get_next_token ($) {
1026                
1027        ## ISSUE: typos in spec: chacacters, is is a parse error        ## ISSUE: typos in spec: chacacters, is is a parse error
1028        ## 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?
1029        } elsif ($self->{state} eq 'comment start') {
1030          if ($self->{next_input_character} == 0x002D) { # -
1031            $self->{state} = 'comment start dash';
1032            !!!next-input-character;
1033            redo A;
1034          } elsif ($self->{next_input_character} == 0x003E) { # >
1035            !!!parse-error (type => 'bogus comment');
1036            $self->{state} = 'data';
1037            !!!next-input-character;
1038    
1039            !!!emit ($self->{current_token}); # comment
1040    
1041            redo A;
1042          } elsif ($self->{next_input_character} == -1) {
1043            !!!parse-error (type => 'unclosed comment');
1044            $self->{state} = 'data';
1045            ## reconsume
1046    
1047            !!!emit ($self->{current_token}); # comment
1048    
1049            redo A;
1050          } else {
1051            $self->{current_token}->{data} # comment
1052                .= chr ($self->{next_input_character});
1053            $self->{state} = 'comment';
1054            !!!next-input-character;
1055            redo A;
1056          }
1057        } elsif ($self->{state} eq 'comment start dash') {
1058          if ($self->{next_input_character} == 0x002D) { # -
1059            $self->{state} = 'comment end';
1060            !!!next-input-character;
1061            redo A;
1062          } elsif ($self->{next_input_character} == 0x003E) { # >
1063            !!!parse-error (type => 'bogus comment');
1064            $self->{state} = 'data';
1065            !!!next-input-character;
1066    
1067            !!!emit ($self->{current_token}); # comment
1068    
1069            redo A;
1070          } elsif ($self->{next_input_character} == -1) {
1071            !!!parse-error (type => 'unclosed comment');
1072            $self->{state} = 'data';
1073            ## reconsume
1074    
1075            !!!emit ($self->{current_token}); # comment
1076    
1077            redo A;
1078          } else {
1079            $self->{current_token}->{data} # comment
1080                .= chr ($self->{next_input_character});
1081            $self->{state} = 'comment';
1082            !!!next-input-character;
1083            redo A;
1084          }
1085      } elsif ($self->{state} eq 'comment') {      } elsif ($self->{state} eq 'comment') {
1086        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_input_character} == 0x002D) { # -
1087          $self->{state} = 'comment dash';          $self->{state} = 'comment end dash';
1088          !!!next-input-character;          !!!next-input-character;
1089          redo A;          redo A;
1090        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
# Line 1267  sub _get_next_token ($) { Line 1093  sub _get_next_token ($) {
1093          ## reconsume          ## reconsume
1094    
1095          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
1096    
1097          redo A;          redo A;
1098        } else {        } else {
# Line 1276  sub _get_next_token ($) { Line 1101  sub _get_next_token ($) {
1101          !!!next-input-character;          !!!next-input-character;
1102          redo A;          redo A;
1103        }        }
1104      } elsif ($self->{state} eq 'comment dash') {      } elsif ($self->{state} eq 'comment end dash') {
1105        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_input_character} == 0x002D) { # -
1106          $self->{state} = 'comment end';          $self->{state} = 'comment end';
1107          !!!next-input-character;          !!!next-input-character;
# Line 1287  sub _get_next_token ($) { Line 1112  sub _get_next_token ($) {
1112          ## reconsume          ## reconsume
1113    
1114          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
1115    
1116          redo A;          redo A;
1117        } else {        } else {
# Line 1302  sub _get_next_token ($) { Line 1126  sub _get_next_token ($) {
1126          !!!next-input-character;          !!!next-input-character;
1127    
1128          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
1129    
1130          redo A;          redo A;
1131        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_input_character} == 0x002D) { # -
# Line 1317  sub _get_next_token ($) { Line 1140  sub _get_next_token ($) {
1140          ## reconsume          ## reconsume
1141    
1142          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
1143    
1144          redo A;          redo A;
1145        } else {        } else {
# Line 1351  sub _get_next_token ($) { Line 1173  sub _get_next_token ($) {
1173          ## Stay in the state          ## Stay in the state
1174          !!!next-input-character;          !!!next-input-character;
1175          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;  
1176        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1177          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
1178          $self->{state} = 'data';          $self->{state} = 'data';
1179          !!!next-input-character;          !!!next-input-character;
1180    
1181          !!!emit ({type => 'DOCTYPE', name => '', error => 1});          !!!emit ({type => 'DOCTYPE'}); # incorrect
1182    
1183          redo A;          redo A;
1184        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
# Line 1373  sub _get_next_token ($) { Line 1186  sub _get_next_token ($) {
1186          $self->{state} = 'data';          $self->{state} = 'data';
1187          ## reconsume          ## reconsume
1188    
1189          !!!emit ({type => 'DOCTYPE', name => '', error => 1});          !!!emit ({type => 'DOCTYPE'}); # incorrect
1190    
1191          redo A;          redo A;
1192        } else {        } else {
1193          $self->{current_token} = {type => 'DOCTYPE',          $self->{current_token}
1194                            name => chr ($self->{next_input_character}),              = {type => 'DOCTYPE',
1195                            error => 1};                 name => chr ($self->{next_input_character}),
1196                   correct => 1};
1197  ## ISSUE: "Set the token's name name to the" in the spec  ## ISSUE: "Set the token's name name to the" in the spec
1198          $self->{state} = 'DOCTYPE name';          $self->{state} = 'DOCTYPE name';
1199          !!!next-input-character;          !!!next-input-character;
1200          redo A;          redo A;
1201        }        }
1202      } elsif ($self->{state} eq 'DOCTYPE name') {      } elsif ($self->{state} eq 'DOCTYPE name') {
1203    ## ISSUE: Redundant "First," in the spec.
1204        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1205            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1206            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
1207            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
1208            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
         $self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML'); # DOCTYPE  
1209          $self->{state} = 'after DOCTYPE name';          $self->{state} = 'after DOCTYPE name';
1210          !!!next-input-character;          !!!next-input-character;
1211          redo A;          redo A;
1212        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
         $self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML'); # DOCTYPE  
1213          $self->{state} = 'data';          $self->{state} = 'data';
1214          !!!next-input-character;          !!!next-input-character;
1215    
1216          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1217    
1218          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;  
1219        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1220          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
         $self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML'); # DOCTYPE  
1221          $self->{state} = 'data';          $self->{state} = 'data';
1222          ## reconsume          ## reconsume
1223    
1224          !!!emit ($self->{current_token});          delete $self->{current_token}->{correct};
1225          undef $self->{current_token};          !!!emit ($self->{current_token}); # DOCTYPE
1226    
1227          redo A;          redo A;
1228        } else {        } else {
1229          $self->{current_token}->{name}          $self->{current_token}->{name}
1230            .= chr ($self->{next_input_character}); # DOCTYPE            .= chr ($self->{next_input_character}); # DOCTYPE
         #$self->{current_token}->{error} = ($self->{current_token}->{name} ne 'HTML');  
1231          ## Stay in the state          ## Stay in the state
1232          !!!next-input-character;          !!!next-input-character;
1233          redo A;          redo A;
# Line 1443  sub _get_next_token ($) { Line 1246  sub _get_next_token ($) {
1246          !!!next-input-character;          !!!next-input-character;
1247    
1248          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1249    
1250          redo A;          redo A;
1251        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
# Line 1451  sub _get_next_token ($) { Line 1253  sub _get_next_token ($) {
1253          $self->{state} = 'data';          $self->{state} = 'data';
1254          ## reconsume          ## reconsume
1255    
1256            delete $self->{current_token}->{correct};
1257            !!!emit ($self->{current_token}); # DOCTYPE
1258    
1259            redo A;
1260          } elsif ($self->{next_input_character} == 0x0050 or # P
1261                   $self->{next_input_character} == 0x0070) { # p
1262            !!!next-input-character;
1263            if ($self->{next_input_character} == 0x0055 or # U
1264                $self->{next_input_character} == 0x0075) { # u
1265              !!!next-input-character;
1266              if ($self->{next_input_character} == 0x0042 or # B
1267                  $self->{next_input_character} == 0x0062) { # b
1268                !!!next-input-character;
1269                if ($self->{next_input_character} == 0x004C or # L
1270                    $self->{next_input_character} == 0x006C) { # l
1271                  !!!next-input-character;
1272                  if ($self->{next_input_character} == 0x0049 or # I
1273                      $self->{next_input_character} == 0x0069) { # i
1274                    !!!next-input-character;
1275                    if ($self->{next_input_character} == 0x0043 or # C
1276                        $self->{next_input_character} == 0x0063) { # c
1277                      $self->{state} = 'before DOCTYPE public identifier';
1278                      !!!next-input-character;
1279                      redo A;
1280                    }
1281                  }
1282                }
1283              }
1284            }
1285    
1286            #
1287          } elsif ($self->{next_input_character} == 0x0053 or # S
1288                   $self->{next_input_character} == 0x0073) { # s
1289            !!!next-input-character;
1290            if ($self->{next_input_character} == 0x0059 or # Y
1291                $self->{next_input_character} == 0x0079) { # y
1292              !!!next-input-character;
1293              if ($self->{next_input_character} == 0x0053 or # S
1294                  $self->{next_input_character} == 0x0073) { # s
1295                !!!next-input-character;
1296                if ($self->{next_input_character} == 0x0054 or # T
1297                    $self->{next_input_character} == 0x0074) { # t
1298                  !!!next-input-character;
1299                  if ($self->{next_input_character} == 0x0045 or # E
1300                      $self->{next_input_character} == 0x0065) { # e
1301                    !!!next-input-character;
1302                    if ($self->{next_input_character} == 0x004D or # M
1303                        $self->{next_input_character} == 0x006D) { # m
1304                      $self->{state} = 'before DOCTYPE system identifier';
1305                      !!!next-input-character;
1306                      redo A;
1307                    }
1308                  }
1309                }
1310              }
1311            }
1312    
1313            #
1314          } else {
1315            !!!next-input-character;
1316            #
1317          }
1318    
1319          !!!parse-error (type => 'string after DOCTYPE name');
1320          $self->{state} = 'bogus DOCTYPE';
1321          # next-input-character is already done
1322          redo A;
1323        } elsif ($self->{state} eq 'before DOCTYPE public identifier') {
1324          if ({
1325                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1326                #0x000D => 1, # HT, LF, VT, FF, SP, CR
1327              }->{$self->{next_input_character}}) {
1328            ## Stay in the state
1329            !!!next-input-character;
1330            redo A;
1331          } elsif ($self->{next_input_character} eq 0x0022) { # "
1332            $self->{current_token}->{public_identifier} = ''; # DOCTYPE
1333            $self->{state} = 'DOCTYPE public identifier (double-quoted)';
1334            !!!next-input-character;
1335            redo A;
1336          } elsif ($self->{next_input_character} eq 0x0027) { # '
1337            $self->{current_token}->{public_identifier} = ''; # DOCTYPE
1338            $self->{state} = 'DOCTYPE public identifier (single-quoted)';
1339            !!!next-input-character;
1340            redo A;
1341          } elsif ($self->{next_input_character} eq 0x003E) { # >
1342            !!!parse-error (type => 'no PUBLIC literal');
1343    
1344            $self->{state} = 'data';
1345            !!!next-input-character;
1346    
1347            delete $self->{current_token}->{correct};
1348            !!!emit ($self->{current_token}); # DOCTYPE
1349    
1350            redo A;
1351          } elsif ($self->{next_input_character} == -1) {
1352            !!!parse-error (type => 'unclosed DOCTYPE');
1353    
1354            $self->{state} = 'data';
1355            ## reconsume
1356    
1357            delete $self->{current_token}->{correct};
1358            !!!emit ($self->{current_token}); # DOCTYPE
1359    
1360            redo A;
1361          } else {
1362            !!!parse-error (type => 'string after PUBLIC');
1363            $self->{state} = 'bogus DOCTYPE';
1364            !!!next-input-character;
1365            redo A;
1366          }
1367        } elsif ($self->{state} eq 'DOCTYPE public identifier (double-quoted)') {
1368          if ($self->{next_input_character} == 0x0022) { # "
1369            $self->{state} = 'after DOCTYPE public identifier';
1370            !!!next-input-character;
1371            redo A;
1372          } elsif ($self->{next_input_character} == -1) {
1373            !!!parse-error (type => 'unclosed PUBLIC literal');
1374    
1375            $self->{state} = 'data';
1376            ## reconsume
1377    
1378            delete $self->{current_token}->{correct};
1379            !!!emit ($self->{current_token}); # DOCTYPE
1380    
1381            redo A;
1382          } else {
1383            $self->{current_token}->{public_identifier} # DOCTYPE
1384                .= chr $self->{next_input_character};
1385            ## Stay in the state
1386            !!!next-input-character;
1387            redo A;
1388          }
1389        } elsif ($self->{state} eq 'DOCTYPE public identifier (single-quoted)') {
1390          if ($self->{next_input_character} == 0x0027) { # '
1391            $self->{state} = 'after DOCTYPE public identifier';
1392            !!!next-input-character;
1393            redo A;
1394          } elsif ($self->{next_input_character} == -1) {
1395            !!!parse-error (type => 'unclosed PUBLIC literal');
1396    
1397            $self->{state} = 'data';
1398            ## reconsume
1399    
1400            delete $self->{current_token}->{correct};
1401            !!!emit ($self->{current_token}); # DOCTYPE
1402    
1403            redo A;
1404          } else {
1405            $self->{current_token}->{public_identifier} # DOCTYPE
1406                .= chr $self->{next_input_character};
1407            ## Stay in the state
1408            !!!next-input-character;
1409            redo A;
1410          }
1411        } elsif ($self->{state} eq 'after DOCTYPE public identifier') {
1412          if ({
1413                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1414                #0x000D => 1, # HT, LF, VT, FF, SP, CR
1415              }->{$self->{next_input_character}}) {
1416            ## Stay in the state
1417            !!!next-input-character;
1418            redo A;
1419          } elsif ($self->{next_input_character} == 0x0022) { # "
1420            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1421            $self->{state} = 'DOCTYPE system identifier (double-quoted)';
1422            !!!next-input-character;
1423            redo A;
1424          } elsif ($self->{next_input_character} == 0x0027) { # '
1425            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1426            $self->{state} = 'DOCTYPE system identifier (single-quoted)';
1427            !!!next-input-character;
1428            redo A;
1429          } elsif ($self->{next_input_character} == 0x003E) { # >
1430            $self->{state} = 'data';
1431            !!!next-input-character;
1432    
1433            !!!emit ($self->{current_token}); # DOCTYPE
1434    
1435            redo A;
1436          } elsif ($self->{next_input_character} == -1) {
1437            !!!parse-error (type => 'unclosed DOCTYPE');
1438    
1439            $self->{state} = 'data';
1440            ## reconsume
1441    
1442            delete $self->{current_token}->{correct};
1443          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1444    
1445          redo A;          redo A;
1446        } else {        } else {
1447          !!!parse-error (type => 'string after DOCTYPE name');          !!!parse-error (type => 'string after PUBLIC literal');
1448          $self->{current_token}->{error} = 1; # DOCTYPE          $self->{state} = 'bogus DOCTYPE';
1449            !!!next-input-character;
1450            redo A;
1451          }
1452        } elsif ($self->{state} eq 'before DOCTYPE system identifier') {
1453          if ({
1454                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1455                #0x000D => 1, # HT, LF, VT, FF, SP, CR
1456              }->{$self->{next_input_character}}) {
1457            ## Stay in the state
1458            !!!next-input-character;
1459            redo A;
1460          } elsif ($self->{next_input_character} == 0x0022) { # "
1461            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1462            $self->{state} = 'DOCTYPE system identifier (double-quoted)';
1463            !!!next-input-character;
1464            redo A;
1465          } elsif ($self->{next_input_character} == 0x0027) { # '
1466            $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1467            $self->{state} = 'DOCTYPE system identifier (single-quoted)';
1468            !!!next-input-character;
1469            redo A;
1470          } elsif ($self->{next_input_character} == 0x003E) { # >
1471            !!!parse-error (type => 'no SYSTEM literal');
1472            $self->{state} = 'data';
1473            !!!next-input-character;
1474    
1475            delete $self->{current_token}->{correct};
1476            !!!emit ($self->{current_token}); # DOCTYPE
1477    
1478            redo A;
1479          } elsif ($self->{next_input_character} == -1) {
1480            !!!parse-error (type => 'unclosed DOCTYPE');
1481    
1482            $self->{state} = 'data';
1483            ## reconsume
1484    
1485            delete $self->{current_token}->{correct};
1486            !!!emit ($self->{current_token}); # DOCTYPE
1487    
1488            redo A;
1489          } else {
1490            !!!parse-error (type => 'string after PUBLIC literal');
1491            $self->{state} = 'bogus DOCTYPE';
1492            !!!next-input-character;
1493            redo A;
1494          }
1495        } elsif ($self->{state} eq 'DOCTYPE system identifier (double-quoted)') {
1496          if ($self->{next_input_character} == 0x0022) { # "
1497            $self->{state} = 'after DOCTYPE system identifier';
1498            !!!next-input-character;
1499            redo A;
1500          } elsif ($self->{next_input_character} == -1) {
1501            !!!parse-error (type => 'unclosed SYSTEM literal');
1502    
1503            $self->{state} = 'data';
1504            ## reconsume
1505    
1506            delete $self->{current_token}->{correct};
1507            !!!emit ($self->{current_token}); # DOCTYPE
1508    
1509            redo A;
1510          } else {
1511            $self->{current_token}->{system_identifier} # DOCTYPE
1512                .= chr $self->{next_input_character};
1513            ## Stay in the state
1514            !!!next-input-character;
1515            redo A;
1516          }
1517        } elsif ($self->{state} eq 'DOCTYPE system identifier (single-quoted)') {
1518          if ($self->{next_input_character} == 0x0027) { # '
1519            $self->{state} = 'after DOCTYPE system identifier';
1520            !!!next-input-character;
1521            redo A;
1522          } elsif ($self->{next_input_character} == -1) {
1523            !!!parse-error (type => 'unclosed SYSTEM literal');
1524    
1525            $self->{state} = 'data';
1526            ## reconsume
1527    
1528            delete $self->{current_token}->{correct};
1529            !!!emit ($self->{current_token}); # DOCTYPE
1530    
1531            redo A;
1532          } else {
1533            $self->{current_token}->{system_identifier} # DOCTYPE
1534                .= chr $self->{next_input_character};
1535            ## Stay in the state
1536            !!!next-input-character;
1537            redo A;
1538          }
1539        } elsif ($self->{state} eq 'after DOCTYPE system identifier') {
1540          if ({
1541                0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1542                #0x000D => 1, # HT, LF, VT, FF, SP, CR
1543              }->{$self->{next_input_character}}) {
1544            ## Stay in the state
1545            !!!next-input-character;
1546            redo A;
1547          } elsif ($self->{next_input_character} == 0x003E) { # >
1548            $self->{state} = 'data';
1549            !!!next-input-character;
1550    
1551            !!!emit ($self->{current_token}); # DOCTYPE
1552    
1553            redo A;
1554          } elsif ($self->{next_input_character} == -1) {
1555            !!!parse-error (type => 'unclosed DOCTYPE');
1556    
1557            $self->{state} = 'data';
1558            ## reconsume
1559    
1560            delete $self->{current_token}->{correct};
1561            !!!emit ($self->{current_token}); # DOCTYPE
1562    
1563            redo A;
1564          } else {
1565            !!!parse-error (type => 'string after SYSTEM literal');
1566          $self->{state} = 'bogus DOCTYPE';          $self->{state} = 'bogus DOCTYPE';
1567          !!!next-input-character;          !!!next-input-character;
1568          redo A;          redo A;
# Line 1467  sub _get_next_token ($) { Line 1572  sub _get_next_token ($) {
1572          $self->{state} = 'data';          $self->{state} = 'data';
1573          !!!next-input-character;          !!!next-input-character;
1574    
1575            delete $self->{current_token}->{correct};
1576          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1577    
1578          redo A;          redo A;
1579        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
# Line 1476  sub _get_next_token ($) { Line 1581  sub _get_next_token ($) {
1581          $self->{state} = 'data';          $self->{state} = 'data';
1582          ## reconsume          ## reconsume
1583    
1584            delete $self->{current_token}->{correct};
1585          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1586    
1587          redo A;          redo A;
1588        } else {        } else {
# Line 1493  sub _get_next_token ($) { Line 1598  sub _get_next_token ($) {
1598    die "$0: _get_next_token: unexpected case";    die "$0: _get_next_token: unexpected case";
1599  } # _get_next_token  } # _get_next_token
1600    
1601  sub _tokenize_attempt_to_consume_an_entity ($) {  sub _tokenize_attempt_to_consume_an_entity ($$) {
1602    my $self = shift;    my ($self, $in_attr) = @_;
1603      
1604    if ($self->{next_input_character} == 0x0023) { # #    if ({
1605           0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,
1606           0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR
1607          }->{$self->{next_input_character}}) {
1608        ## Don't consume
1609        ## No error
1610        return undef;
1611      } elsif ($self->{next_input_character} == 0x0023) { # #
1612      !!!next-input-character;      !!!next-input-character;
1613      if ($self->{next_input_character} == 0x0078 or # x      if ($self->{next_input_character} == 0x0078 or # x
1614          $self->{next_input_character} == 0x0058) { # X          $self->{next_input_character} == 0x0058) { # X
1615        my $num;        my $code;
1616        X: {        X: {
1617          my $x_char = $self->{next_input_character};          my $x_char = $self->{next_input_character};
1618          !!!next-input-character;          !!!next-input-character;
1619          if (0x0030 <= $self->{next_input_character} and          if (0x0030 <= $self->{next_input_character} and
1620              $self->{next_input_character} <= 0x0039) { # 0..9              $self->{next_input_character} <= 0x0039) { # 0..9
1621            $num ||= 0;            $code ||= 0;
1622            $num *= 0x10;            $code *= 0x10;
1623            $num += $self->{next_input_character} - 0x0030;            $code += $self->{next_input_character} - 0x0030;
1624            redo X;            redo X;
1625          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_input_character} and
1626                   $self->{next_input_character} <= 0x0066) { # a..f                   $self->{next_input_character} <= 0x0066) { # a..f
1627            ## ISSUE: the spec says U+0078, which is apparently incorrect            $code ||= 0;
1628            $num ||= 0;            $code *= 0x10;
1629            $num *= 0x10;            $code += $self->{next_input_character} - 0x0060 + 9;
           $num += $self->{next_input_character} - 0x0060 + 9;  
1630            redo X;            redo X;
1631          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_input_character} and
1632                   $self->{next_input_character} <= 0x0046) { # A..F                   $self->{next_input_character} <= 0x0046) { # A..F
1633            ## ISSUE: the spec says U+0058, which is apparently incorrect            $code ||= 0;
1634            $num ||= 0;            $code *= 0x10;
1635            $num *= 0x10;            $code += $self->{next_input_character} - 0x0040 + 9;
           $num += $self->{next_input_character} - 0x0040 + 9;  
1636            redo X;            redo X;
1637          } elsif (not defined $num) { # no hexadecimal digit          } elsif (not defined $code) { # no hexadecimal digit
1638            !!!parse-error (type => 'bare hcro');            !!!parse-error (type => 'bare hcro');
1639            $self->{next_input_character} = 0x0023; # #            $self->{next_input_character} = 0x0023; # #
1640            !!!back-next-input-character ($x_char);            !!!back-next-input-character ($x_char);
# Line 1535  sub _tokenize_attempt_to_consume_an_enti Line 1645  sub _tokenize_attempt_to_consume_an_enti
1645            !!!parse-error (type => 'no refc');            !!!parse-error (type => 'no refc');
1646          }          }
1647    
1648          ## TODO: check the definition for |a valid Unicode character|.          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
1649          ## <http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2006-December/thread.html#8189>            !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);
1650          if ($num > 1114111 or $num == 0) {            $code = 0xFFFD;
1651            $num = 0xFFFD; # REPLACEMENT CHARACTER          } elsif ($code > 0x10FFFF) {
1652            ## ISSUE: Why this is not an error?            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);
1653          } elsif (0x80 <= $num and $num <= 0x9F) {            $code = 0xFFFD;
1654            !!!parse-error (type => sprintf 'c1 entity:U+%04X', $num);          } elsif ($code == 0x000D) {
1655            $num = $c1_entity_char->{$num};            !!!parse-error (type => 'CR character reference');
1656              $code = 0x000A;
1657            } elsif (0x80 <= $code and $code <= 0x9F) {
1658              !!!parse-error (type => sprintf 'c1 entity:U+%04X', $code);
1659              $code = $c1_entity_char->{$code};
1660          }          }
1661    
1662          return {type => 'character', data => chr $num};          return {type => 'character', data => chr $code};
1663        } # X        } # X
1664      } elsif (0x0030 <= $self->{next_input_character} and      } elsif (0x0030 <= $self->{next_input_character} and
1665               $self->{next_input_character} <= 0x0039) { # 0..9               $self->{next_input_character} <= 0x0039) { # 0..9
# Line 1566  sub _tokenize_attempt_to_consume_an_enti Line 1680  sub _tokenize_attempt_to_consume_an_enti
1680          !!!parse-error (type => 'no refc');          !!!parse-error (type => 'no refc');
1681        }        }
1682    
1683        ## TODO: check the definition for |a valid Unicode character|.        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
1684        if ($code > 1114111 or $code == 0) {          !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);
1685          $code = 0xFFFD; # REPLACEMENT CHARACTER          $code = 0xFFFD;
1686          ## ISSUE: Why this is not an error?        } elsif ($code > 0x10FFFF) {
1687            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);
1688            $code = 0xFFFD;
1689          } elsif ($code == 0x000D) {
1690            !!!parse-error (type => 'CR character reference');
1691            $code = 0x000A;
1692        } elsif (0x80 <= $code and $code <= 0x9F) {        } elsif (0x80 <= $code and $code <= 0x9F) {
1693          !!!parse-error (type => sprintf 'c1 entity:U+%04X', $code);          !!!parse-error (type => sprintf 'c1 entity:U+%04X', $code);
1694          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
# Line 1591  sub _tokenize_attempt_to_consume_an_enti Line 1710  sub _tokenize_attempt_to_consume_an_enti
1710    
1711      my $value = $entity_name;      my $value = $entity_name;
1712      my $match;      my $match;
1713        require Whatpm::_NamedEntityList;
1714        our $EntityChar;
1715    
1716      while (length $entity_name < 10 and      while (length $entity_name < 10 and
1717             ## NOTE: Some number greater than the maximum length of entity name             ## NOTE: Some number greater than the maximum length of entity name
1718             ((0x0041 <= $self->{next_input_character} and             ((0x0041 <= $self->{next_input_character} and # a
1719               $self->{next_input_character} <= 0x005A) or               $self->{next_input_character} <= 0x005A) or # x
1720              (0x0061 <= $self->{next_input_character} and              (0x0061 <= $self->{next_input_character} and # a
1721               $self->{next_input_character} <= 0x007A) or               $self->{next_input_character} <= 0x007A) or # z
1722              (0x0030 <= $self->{next_input_character} and              (0x0030 <= $self->{next_input_character} and # 0
1723               $self->{next_input_character} <= 0x0039))) {               $self->{next_input_character} <= 0x0039) or # 9
1724                $self->{next_input_character} == 0x003B)) { # ;
1725        $entity_name .= chr $self->{next_input_character};        $entity_name .= chr $self->{next_input_character};
1726        if (defined $entity_char->{$entity_name}) {        if (defined $EntityChar->{$entity_name}) {
1727          $value = $entity_char->{$entity_name};          if ($self->{next_input_character} == 0x003B) { # ;
1728          $match = 1;            $value = $EntityChar->{$entity_name};
1729              $match = 1;
1730              !!!next-input-character;
1731              last;
1732            } elsif (not $in_attr) {
1733              $value = $EntityChar->{$entity_name};
1734              $match = -1;
1735            } else {
1736              $value .= chr $self->{next_input_character};
1737            }
1738        } else {        } else {
1739          $value .= chr $self->{next_input_character};          $value .= chr $self->{next_input_character};
1740        }        }
1741        !!!next-input-character;        !!!next-input-character;
1742      }      }
1743            
1744      if ($match) {      if ($match > 0) {
1745        if ($self->{next_input_character} == 0x003B) { # ;        return {type => 'character', data => $value};
1746          !!!next-input-character;      } elsif ($match < 0) {
1747        } else {        !!!parse-error (type => 'refc');
         !!!parse-error (type => 'refc');  
       }  
   
1748        return {type => 'character', data => $value};        return {type => 'character', data => $value};
1749      } else {      } else {
1750        !!!parse-error (type => 'bare ero');        !!!parse-error (type => 'bare ero');
1751        ## NOTE: No characters are consumed in the spec.        ## NOTE: No characters are consumed in the spec.
1752        !!!back-token ({type => 'character', data => $value});        return {type => 'character', data => '&'.$value};
       return undef;  
1753      }      }
1754    } else {    } else {
1755      ## no characters are consumed      ## no characters are consumed
# Line 1637  sub _initialize_tree_constructor ($) { Line 1764  sub _initialize_tree_constructor ($) {
1764    $self->{document}->strict_error_checking (0);    $self->{document}->strict_error_checking (0);
1765    ## TODO: Turn mutation events off # MUST    ## TODO: Turn mutation events off # MUST
1766    ## TODO: Turn loose Document option (manakai extension) on    ## TODO: Turn loose Document option (manakai extension) on
1767    ## TODO: Mark the Document as an HTML document # MUST    $self->{document}->manakai_is_html (1); # MUST
1768  } # _initialize_tree_constructor  } # _initialize_tree_constructor
1769    
1770  sub _terminate_tree_constructor ($) {  sub _terminate_tree_constructor ($) {
# Line 1677  sub _construct_tree ($) { Line 1804  sub _construct_tree ($) {
1804    
1805  sub _tree_construction_initial ($) {  sub _tree_construction_initial ($) {
1806    my $self = shift;    my $self = shift;
1807    B: {    INITIAL: {
1808        if ($token->{type} eq 'DOCTYPE') {      if ($token->{type} eq 'DOCTYPE') {
1809          if ($token->{error}) {        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"
1810            ## ISSUE: Spec currently left this case undefined.        ## error, switch to a conformance checking mode for another
1811            !!!parse-error (type => 'bogus DOCTYPE');        ## language.
1812          }        my $doctype_name = $token->{name};
1813          my $doctype = $self->{document}->create_document_type_definition        $doctype_name = '' unless defined $doctype_name;
1814            ($token->{name});        $doctype_name =~ tr/a-z/A-Z/;
1815          $self->{document}->append_child ($doctype);        if (not defined $token->{name} or # <!DOCTYPE>
1816          #$phase = 'root element';            defined $token->{public_identifier} or
1817          !!!next-token;            defined $token->{system_identifier}) {
1818          #redo B;          !!!parse-error (type => 'not HTML5');
1819          return;        } elsif ($doctype_name ne 'HTML') {
1820        } elsif ({          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)
1821                  comment => 1,          !!!parse-error (type => 'not HTML5');
1822                  'start tag' => 1,        }
1823                  'end tag' => 1,        
1824                  'end-of-file' => 1,        my $doctype = $self->{document}->create_document_type_definition
1825                 }->{$token->{type}}) {          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
1826          ## ISSUE: Spec currently left this case undefined.        $doctype->public_id ($token->{public_identifier})
1827          !!!parse-error (type => 'missing DOCTYPE');            if defined $token->{public_identifier};
1828          #$phase = 'root element';        $doctype->system_id ($token->{system_identifier})
1829          ## reprocess            if defined $token->{system_identifier};
1830          #redo B;        ## NOTE: Other DocumentType attributes are null or empty lists.
1831          return;        ## ISSUE: internalSubset = null??
1832        } elsif ($token->{type} eq 'character') {        $self->{document}->append_child ($doctype);
1833          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {        
1834            $self->{document}->manakai_append_text ($1);        if (not $token->{correct} or $doctype_name ne 'HTML') {
1835            ## ISSUE: DOM3 Core does not allow Document > Text          $self->{document}->manakai_compat_mode ('quirks');
1836            unless (length $token->{data}) {        } elsif (defined $token->{public_identifier}) {
1837              ## Stay in the phase          my $pubid = $token->{public_identifier};
1838              !!!next-token;          $pubid =~ tr/a-z/A-z/;
1839              redo B;          if ({
1840              "+//SILMARIL//DTD HTML PRO V0R11 19970101//EN" => 1,
1841              "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,
1842              "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,
1843              "-//IETF//DTD HTML 2.0 LEVEL 1//EN" => 1,
1844              "-//IETF//DTD HTML 2.0 LEVEL 2//EN" => 1,
1845              "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//EN" => 1,
1846              "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//EN" => 1,
1847              "-//IETF//DTD HTML 2.0 STRICT//EN" => 1,
1848              "-//IETF//DTD HTML 2.0//EN" => 1,
1849              "-//IETF//DTD HTML 2.1E//EN" => 1,
1850              "-//IETF//DTD HTML 3.0//EN" => 1,
1851              "-//IETF//DTD HTML 3.0//EN//" => 1,
1852              "-//IETF//DTD HTML 3.2 FINAL//EN" => 1,
1853              "-//IETF//DTD HTML 3.2//EN" => 1,
1854              "-//IETF//DTD HTML 3//EN" => 1,
1855              "-//IETF//DTD HTML LEVEL 0//EN" => 1,
1856              "-//IETF//DTD HTML LEVEL 0//EN//2.0" => 1,
1857              "-//IETF//DTD HTML LEVEL 1//EN" => 1,
1858              "-//IETF//DTD HTML LEVEL 1//EN//2.0" => 1,
1859              "-//IETF//DTD HTML LEVEL 2//EN" => 1,
1860              "-//IETF//DTD HTML LEVEL 2//EN//2.0" => 1,
1861              "-//IETF//DTD HTML LEVEL 3//EN" => 1,
1862              "-//IETF//DTD HTML LEVEL 3//EN//3.0" => 1,
1863              "-//IETF//DTD HTML STRICT LEVEL 0//EN" => 1,
1864              "-//IETF//DTD HTML STRICT LEVEL 0//EN//2.0" => 1,
1865              "-//IETF//DTD HTML STRICT LEVEL 1//EN" => 1,
1866              "-//IETF//DTD HTML STRICT LEVEL 1//EN//2.0" => 1,
1867              "-//IETF//DTD HTML STRICT LEVEL 2//EN" => 1,
1868              "-//IETF//DTD HTML STRICT LEVEL 2//EN//2.0" => 1,
1869              "-//IETF//DTD HTML STRICT LEVEL 3//EN" => 1,
1870              "-//IETF//DTD HTML STRICT LEVEL 3//EN//3.0" => 1,
1871              "-//IETF//DTD HTML STRICT//EN" => 1,
1872              "-//IETF//DTD HTML STRICT//EN//2.0" => 1,
1873              "-//IETF//DTD HTML STRICT//EN//3.0" => 1,
1874              "-//IETF//DTD HTML//EN" => 1,
1875              "-//IETF//DTD HTML//EN//2.0" => 1,
1876              "-//IETF//DTD HTML//EN//3.0" => 1,
1877              "-//METRIUS//DTD METRIUS PRESENTATIONAL//EN" => 1,
1878              "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//EN" => 1,
1879              "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//EN" => 1,
1880              "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//EN" => 1,
1881              "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//EN" => 1,
1882              "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//EN" => 1,
1883              "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//EN" => 1,
1884              "-//NETSCAPE COMM. CORP.//DTD HTML//EN" => 1,
1885              "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,
1886              "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,
1887              "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,
1888              "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,
1889              "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,
1890              "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,
1891              "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//EN" => 1,
1892              "-//W3C//DTD HTML 3 1995-03-24//EN" => 1,
1893              "-//W3C//DTD HTML 3.2 DRAFT//EN" => 1,
1894              "-//W3C//DTD HTML 3.2 FINAL//EN" => 1,
1895              "-//W3C//DTD HTML 3.2//EN" => 1,
1896              "-//W3C//DTD HTML 3.2S DRAFT//EN" => 1,
1897              "-//W3C//DTD HTML 4.0 FRAMESET//EN" => 1,
1898              "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN" => 1,
1899              "-//W3C//DTD HTML EXPERIMETNAL 19960712//EN" => 1,
1900              "-//W3C//DTD HTML EXPERIMENTAL 970421//EN" => 1,
1901              "-//W3C//DTD W3 HTML//EN" => 1,
1902              "-//W3O//DTD W3 HTML 3.0//EN" => 1,
1903              "-//W3O//DTD W3 HTML 3.0//EN//" => 1,
1904              "-//W3O//DTD W3 HTML STRICT 3.0//EN//" => 1,
1905              "-//WEBTECHS//DTD MOZILLA HTML 2.0//EN" => 1,
1906              "-//WEBTECHS//DTD MOZILLA HTML//EN" => 1,
1907              "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" => 1,
1908              "HTML" => 1,
1909            }->{$pubid}) {
1910              $self->{document}->manakai_compat_mode ('quirks');
1911            } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or
1912                     $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {
1913              if (defined $token->{system_identifier}) {
1914                $self->{document}->manakai_compat_mode ('quirks');
1915              } else {
1916                $self->{document}->manakai_compat_mode ('limited quirks');
1917            }            }
1918            } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 Frameset//EN" or
1919                     $pubid eq "-//W3C//DTD XHTML 1.0 Transitional//EN") {
1920              $self->{document}->manakai_compat_mode ('limited quirks');
1921            }
1922          }
1923          if (defined $token->{system_identifier}) {
1924            my $sysid = $token->{system_identifier};
1925            $sysid =~ tr/A-Z/a-z/;
1926            if ($sysid eq "http://www.ibm.com/data/dtd/v11/ibmxhtml1-transitional.dtd") {
1927              $self->{document}->manakai_compat_mode ('quirks');
1928            }
1929          }
1930          
1931          ## Go to the root element phase.
1932          !!!next-token;
1933          return;
1934        } elsif ({
1935                  'start tag' => 1,
1936                  'end tag' => 1,
1937                  'end-of-file' => 1,
1938                 }->{$token->{type}}) {
1939          !!!parse-error (type => 'no DOCTYPE');
1940          $self->{document}->manakai_compat_mode ('quirks');
1941          ## Go to the root element phase
1942          ## reprocess
1943          return;
1944        } elsif ($token->{type} eq 'character') {
1945          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
1946            ## Ignore the token
1947    
1948            unless (length $token->{data}) {
1949              ## Stay in the phase
1950              !!!next-token;
1951              redo INITIAL;
1952          }          }
         ## 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";  
1953        }        }
1954      } # B  
1955          !!!parse-error (type => 'no DOCTYPE');
1956          $self->{document}->manakai_compat_mode ('quirks');
1957          ## Go to the root element phase
1958          ## reprocess
1959          return;
1960        } elsif ($token->{type} eq 'comment') {
1961          my $comment = $self->{document}->create_comment ($token->{data});
1962          $self->{document}->append_child ($comment);
1963          
1964          ## Stay in the phase.
1965          !!!next-token;
1966          redo INITIAL;
1967        } else {
1968          die "$0: $token->{type}: Unknown token";
1969        }
1970      } # INITIAL
1971  } # _tree_construction_initial  } # _tree_construction_initial
1972    
1973  sub _tree_construction_root_element ($) {  sub _tree_construction_root_element ($) {
# Line 1741  sub _tree_construction_root_element ($) Line 1987  sub _tree_construction_root_element ($)
1987          !!!next-token;          !!!next-token;
1988          redo B;          redo B;
1989        } elsif ($token->{type} eq 'character') {        } elsif ($token->{type} eq 'character') {
1990          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
1991            $self->{document}->manakai_append_text ($1);            ## Ignore the token.
1992            ## ISSUE: DOM3 Core does not allow Document > Text  
1993            unless (length $token->{data}) {            unless (length $token->{data}) {
1994              ## Stay in the phase              ## Stay in the phase
1995              !!!next-token;              !!!next-token;
# Line 1920  sub _tree_construction_main ($) { Line 2166  sub _tree_construction_main ($) {
2166      }      }
2167    }; # $clear_up_to_marker    }; # $clear_up_to_marker
2168    
2169    my $style_start_tag = sub {    my $parse_rcdata = sub ($$) {
2170      my $style_el; !!!create-element ($style_el, 'style', $token->{attributes});      my ($content_model_flag, $insert) = @_;
2171      ## $self->{insertion_mode} eq 'in head' and ... (always true)  
2172      (($self->{insertion_mode} eq 'in head' and defined $self->{head_element})      ## Step 1
2173       ? $self->{head_element} : $self->{open_elements}->[-1]->[0])      my $start_tag_name = $token->{tag_name};
2174        ->append_child ($style_el);      my $el;
2175      $self->{content_model_flag} = 'CDATA';      !!!create-element ($el, $start_tag_name, $token->{attributes});
2176                  
2177        ## Step 2
2178        $insert->($el); # /context node/->append_child ($el)
2179    
2180        ## Step 3
2181        $self->{content_model_flag} = $content_model_flag; # CDATA or RCDATA
2182        delete $self->{escape}; # MUST
2183    
2184        ## Step 4
2185      my $text = '';      my $text = '';
2186      !!!next-token;      !!!next-token;
2187      while ($token->{type} eq 'character') {      while ($token->{type} eq 'character') { # or until stop tokenizing
2188        $text .= $token->{data};        $text .= $token->{data};
2189        !!!next-token;        !!!next-token;
2190      } # stop if non-character token or tokenizer stops tokenising      }
2191    
2192        ## Step 5
2193      if (length $text) {      if (length $text) {
2194        $style_el->manakai_append_text ($text);        my $text = $self->{document}->create_text_node ($text);
2195          $el->append_child ($text);
2196      }      }
2197        
2198        ## Step 6
2199      $self->{content_model_flag} = 'PCDATA';      $self->{content_model_flag} = 'PCDATA';
2200                  
2201      if ($token->{type} eq 'end tag' and $token->{tag_name} eq 'style') {      ## Step 7
2202        if ($token->{type} eq 'end tag' and $token->{tag_name} eq $start_tag_name) {
2203        ## Ignore the token        ## Ignore the token
2204      } else {      } else {
2205        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!parse-error (type => 'in '.$content_model_flag.':#'.$token->{type});
       ## ISSUE: And ignore?  
2206      }      }
2207      !!!next-token;      !!!next-token;
2208    }; # $style_start_tag    }; # $parse_rcdata
2209    
2210    my $script_start_tag = sub {    my $script_start_tag = sub ($) {
2211        my $insert = $_[0];
2212      my $script_el;      my $script_el;
2213      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, 'script', $token->{attributes});
2214      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
2215    
2216      $self->{content_model_flag} = 'CDATA';      $self->{content_model_flag} = 'CDATA';
2217        delete $self->{escape}; # MUST
2218            
2219      my $text = '';      my $text = '';
2220      !!!next-token;      !!!next-token;
# Line 1982  sub _tree_construction_main ($) { Line 2242  sub _tree_construction_main ($) {
2242      } else {      } else {
2243        ## TODO: $old_insertion_point = current insertion point        ## TODO: $old_insertion_point = current insertion point
2244        ## TODO: insertion point = just before the next input character        ## TODO: insertion point = just before the next input character
2245          
2246        (($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);  
2247                
2248        ## TODO: insertion point = $old_insertion_point (might be "undefined")        ## TODO: insertion point = $old_insertion_point (might be "undefined")
2249                
# Line 2178  sub _tree_construction_main ($) { Line 2437  sub _tree_construction_main ($) {
2437    }; # $formatting_end_tag    }; # $formatting_end_tag
2438    
2439    my $insert_to_current = sub {    my $insert_to_current = sub {
2440      $self->{open_elements}->[-1]->[0]->append_child (shift);      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);
2441    }; # $insert_to_current    }; # $insert_to_current
2442    
2443    my $insert_to_foster = sub {    my $insert_to_foster = sub {
# Line 2216  sub _tree_construction_main ($) { Line 2475  sub _tree_construction_main ($) {
2475      my $insert = shift;      my $insert = shift;
2476      if ($token->{type} eq 'start tag') {      if ($token->{type} eq 'start tag') {
2477        if ($token->{tag_name} eq 'script') {        if ($token->{tag_name} eq 'script') {
2478          $script_start_tag->();          ## NOTE: This is an "as if in head" code clone
2479            $script_start_tag->($insert);
2480          return;          return;
2481        } elsif ($token->{tag_name} eq 'style') {        } elsif ($token->{tag_name} eq 'style') {
2482          $style_start_tag->();          ## NOTE: This is an "as if in head" code clone
2483            $parse_rcdata->('CDATA', $insert);
2484          return;          return;
2485        } elsif ({        } elsif ({
2486                  base => 1, link => 1, meta => 1,                  base => 1, link => 1, meta => 1,
2487                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
2488          !!!parse-error (type => 'in body:'.$token->{tag_name});          ## NOTE: This is an "as if in head" code clone, only "-t" differs
2489          ## NOTE: This is an "as if in head" code clone          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
2490          my $el;          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
         !!!create-element ($el, $token->{tag_name}, $token->{attributes});  
         if (defined $self->{head_element}) {  
           $self->{head_element}->append_child ($el);  
         } else {  
           $insert->($el);  
         }  
           
2491          !!!next-token;          !!!next-token;
2492            ## TODO: Extracting |charset| from |meta|.
2493          return;          return;
2494        } elsif ($token->{tag_name} eq 'title') {        } elsif ($token->{tag_name} eq 'title') {
2495          !!!parse-error (type => 'in body:title');          !!!parse-error (type => 'in body:title');
2496          ## NOTE: There is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
2497          my $title_el;          $parse_rcdata->('RCDATA', $insert);
         !!!create-element ($title_el, 'title', $token->{attributes});  
         (defined $self->{head_element} ? $self->{head_element} : $self->{open_elements}->[-1]->[0])  
           ->append_child ($title_el);  
         $self->{content_model_flag} = 'RCDATA';  
           
         my $text = '';  
         !!!next-token;  
         while ($token->{type} eq 'character') {  
           $text .= $token->{data};  
           !!!next-token;  
         }  
         if (length $text) {  
           $title_el->manakai_append_text ($text);  
         }  
           
         $self->{content_model_flag} = 'PCDATA';  
           
         if ($token->{type} eq 'end tag' and  
             $token->{tag_name} eq 'title') {  
           ## Ignore the token  
         } else {  
           !!!parse-error (type => 'in RCDATA:#'.$token->{type});  
           ## ISSUE: And ignore?  
         }  
         !!!next-token;  
2498          return;          return;
2499        } elsif ($token->{tag_name} eq 'body') {        } elsif ($token->{tag_name} eq 'body') {
2500          !!!parse-error (type => 'in body:body');          !!!parse-error (type => 'in body:body');
# Line 2367  sub _tree_construction_main ($) { Line 2597  sub _tree_construction_main ($) {
2597              if ($i != -1) {              if ($i != -1) {
2598                !!!parse-error (type => 'end tag missing:'.                !!!parse-error (type => 'end tag missing:'.
2599                                $self->{open_elements}->[-1]->[1]);                                $self->{open_elements}->[-1]->[1]);
               ## TODO: test  
2600              }              }
2601              splice @{$self->{open_elements}}, $i;              splice @{$self->{open_elements}}, $i;
2602              last LI;              last LI;
# Line 2415  sub _tree_construction_main ($) { Line 2644  sub _tree_construction_main ($) {
2644              if ($i != -1) {              if ($i != -1) {
2645                !!!parse-error (type => 'end tag missing:'.                !!!parse-error (type => 'end tag missing:'.
2646                                $self->{open_elements}->[-1]->[1]);                                $self->{open_elements}->[-1]->[1]);
               ## TODO: test  
2647              }              }
2648              splice @{$self->{open_elements}}, $i;              splice @{$self->{open_elements}}, $i;
2649              last LI;              last LI;
# Line 2478  sub _tree_construction_main ($) { Line 2706  sub _tree_construction_main ($) {
2706            }            }
2707          } # INSCOPE          } # INSCOPE
2708                        
2709            ## NOTE: See <http://html5.org/tools/web-apps-tracker?from=925&to=926>
2710          ## has an element in scope          ## has an element in scope
2711          my $i;          #my $i;
2712          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          #INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
2713            my $node = $self->{open_elements}->[$_];          #  my $node = $self->{open_elements}->[$_];
2714            if ({          #  if ({
2715                 h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,          #       h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
2716                }->{$node->[1]}) {          #      }->{$node->[1]}) {
2717              $i = $_;          #    $i = $_;
2718              last INSCOPE;          #    last INSCOPE;
2719            } elsif ({          #  } elsif ({
2720                      table => 1, caption => 1, td => 1, th => 1,          #            table => 1, caption => 1, td => 1, th => 1,
2721                      button => 1, marquee => 1, object => 1, html => 1,          #            button => 1, marquee => 1, object => 1, html => 1,
2722                     }->{$node->[1]}) {          #           }->{$node->[1]}) {
2723              last INSCOPE;          #    last INSCOPE;
2724            }          #  }
2725          } # INSCOPE          #} # INSCOPE
2726                      #  
2727          if (defined $i) {          #if (defined $i) {
2728            !!!parse-error (type => 'in hn:hn');          #  !!! parse-error (type => 'in hn:hn');
2729            splice @{$self->{open_elements}}, $i;          #  splice @{$self->{open_elements}}, $i;
2730          }          #}
2731                        
2732          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
2733                        
# Line 2541  sub _tree_construction_main ($) { Line 2770  sub _tree_construction_main ($) {
2770          return;          return;
2771        } elsif ({        } elsif ({
2772                  b => 1, big => 1, em => 1, font => 1, i => 1,                  b => 1, big => 1, em => 1, font => 1, i => 1,
2773                  nobr => 1, s => 1, small => 1, strile => 1,                  s => 1, small => 1, strile => 1,
2774                  strong => 1, tt => 1, u => 1,                  strong => 1, tt => 1, u => 1,
2775                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
2776          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
# Line 2551  sub _tree_construction_main ($) { Line 2780  sub _tree_construction_main ($) {
2780                    
2781          !!!next-token;          !!!next-token;
2782          return;          return;
2783          } elsif ($token->{tag_name} eq 'nobr') {
2784            $reconstruct_active_formatting_elements->($insert_to_current);
2785    
2786            ## has a |nobr| element in scope
2787            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
2788              my $node = $self->{open_elements}->[$_];
2789              if ($node->[1] eq 'nobr') {
2790                !!!back-token;
2791                $token = {type => 'end tag', tag_name => 'nobr'};
2792                return;
2793              } elsif ({
2794                        table => 1, caption => 1, td => 1, th => 1,
2795                        button => 1, marquee => 1, object => 1, html => 1,
2796                       }->{$node->[1]}) {
2797                last INSCOPE;
2798              }
2799            } # INSCOPE
2800            
2801            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
2802            push @$active_formatting_elements, $self->{open_elements}->[-1];
2803            
2804            !!!next-token;
2805            return;
2806        } elsif ($token->{tag_name} eq 'button') {        } elsif ($token->{tag_name} eq 'button') {
2807          ## has a button element in scope          ## has a button element in scope
2808          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 2586  sub _tree_construction_main ($) { Line 2838  sub _tree_construction_main ($) {
2838          return;          return;
2839        } elsif ($token->{tag_name} eq 'xmp') {        } elsif ($token->{tag_name} eq 'xmp') {
2840          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
2841                    $parse_rcdata->('CDATA', $insert);
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
           
         $self->{content_model_flag} = 'CDATA';  
           
         !!!next-token;  
2842          return;          return;
2843        } elsif ($token->{tag_name} eq 'table') {        } elsif ($token->{tag_name} eq 'table') {
2844          ## has a p element in scope          ## has a p element in scope
# Line 2669  sub _tree_construction_main ($) { Line 2916  sub _tree_construction_main ($) {
2916            return;            return;
2917          } else {          } else {
2918            my $at = $token->{attributes};            my $at = $token->{attributes};
2919              my $form_attrs;
2920              $form_attrs->{action} = $at->{action} if $at->{action};
2921              my $prompt_attr = $at->{prompt};
2922            $at->{name} = {name => 'name', value => 'isindex'};            $at->{name} = {name => 'name', value => 'isindex'};
2923              delete $at->{action};
2924              delete $at->{prompt};
2925            my @tokens = (            my @tokens = (
2926                          {type => 'start tag', tag_name => 'form'},                          {type => 'start tag', tag_name => 'form',
2927                             attributes => $form_attrs},
2928                          {type => 'start tag', tag_name => 'hr'},                          {type => 'start tag', tag_name => 'hr'},
2929                          {type => 'start tag', tag_name => 'p'},                          {type => 'start tag', tag_name => 'p'},
2930                          {type => 'start tag', tag_name => 'label'},                          {type => 'start tag', tag_name => 'label'},
2931                          {type => 'character',                         );
2932                           data => 'This is a searchable index. Insert your search keywords here: '}, # SHOULD            if ($prompt_attr) {
2933                          ## TODO: make this configurable              push @tokens, {type => 'character', data => $prompt_attr->{value}};
2934              } else {
2935                push @tokens, {type => 'character',
2936                               data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD
2937                ## TODO: make this configurable
2938              }
2939              push @tokens,
2940                          {type => 'start tag', tag_name => 'input', attributes => $at},                          {type => 'start tag', tag_name => 'input', attributes => $at},
2941                          #{type => 'character', data => ''}, # SHOULD                          #{type => 'character', data => ''}, # SHOULD
2942                          {type => 'end tag', tag_name => 'label'},                          {type => 'end tag', tag_name => 'label'},
2943                          {type => 'end tag', tag_name => 'p'},                          {type => 'end tag', tag_name => 'p'},
2944                          {type => 'start tag', tag_name => 'hr'},                          {type => 'start tag', tag_name => 'hr'},
2945                          {type => 'end tag', tag_name => 'form'},                          {type => 'end tag', tag_name => 'form'};
                        );  
2946            $token = shift @tokens;            $token = shift @tokens;
2947            !!!back-token (@tokens);            !!!back-token (@tokens);
2948            return;            return;
2949          }          }
2950        } 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}}) {  
2951          my $tag_name = $token->{tag_name};          my $tag_name = $token->{tag_name};
2952          my $el;          my $el;
2953          !!!create-element ($el, $token->{tag_name}, $token->{attributes});          !!!create-element ($el, $token->{tag_name}, $token->{attributes});
2954                    
2955          if ($token->{tag_name} eq 'textarea') {          ## TODO: $self->{form_element} if defined
2956            ## TODO: $self->{form_element} if defined          $self->{content_model_flag} = 'RCDATA';
2957            $self->{content_model_flag} = 'RCDATA';          delete $self->{escape}; # MUST
         } else {  
           $self->{content_model_flag} = 'CDATA';  
         }  
2958                    
2959          $insert->($el);          $insert->($el);
2960                    
2961          my $text = '';          my $text = '';
2962          if ($token->{tag_name} eq 'textarea') {          !!!next-token;
2963            !!!next-token;          if ($token->{type} eq 'character') {
2964            if ($token->{type} eq 'character') {            $token->{data} =~ s/^\x0A//;
2965              $token->{data} =~ s/^\x0A//;            unless (length $token->{data}) {
2966              unless (length $token->{data}) {              !!!next-token;
               !!!next-token;  
             }  
2967            }            }
         } else {  
           !!!next-token;  
2968          }          }
2969          while ($token->{type} eq 'character') {          while ($token->{type} eq 'character') {
2970            $text .= $token->{data};            $text .= $token->{data};
# Line 2735  sub _tree_construction_main ($) { Line 2980  sub _tree_construction_main ($) {
2980              $token->{tag_name} eq $tag_name) {              $token->{tag_name} eq $tag_name) {
2981            ## Ignore the token            ## Ignore the token
2982          } else {          } else {
2983            if ($token->{tag_name} eq 'textarea') {            !!!parse-error (type => 'in RCDATA:#'.$token->{type});
             !!!parse-error (type => 'in RCDATA:#'.$token->{type});  
           } else {  
             !!!parse-error (type => 'in CDATA:#'.$token->{type});  
           }  
           ## ISSUE: And ignore?  
2984          }          }
2985          !!!next-token;          !!!next-token;
2986          return;          return;
2987          } elsif ({
2988                    iframe => 1,
2989                    noembed => 1,
2990                    noframes => 1,
2991                    noscript => 0, ## TODO: 1 if scripting is enabled
2992                   }->{$token->{tag_name}}) {
2993            $parse_rcdata->('CDATA', $insert);
2994            return;
2995        } elsif ($token->{tag_name} eq 'select') {        } elsif ($token->{tag_name} eq 'select') {
2996          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
2997                    
# Line 2774  sub _tree_construction_main ($) { Line 3022  sub _tree_construction_main ($) {
3022        }        }
3023      } elsif ($token->{type} eq 'end tag') {      } elsif ($token->{type} eq 'end tag') {
3024        if ($token->{tag_name} eq 'body') {        if ($token->{tag_name} eq 'body') {
3025          if (@{$self->{open_elements}} > 1 and $self->{open_elements}->[1]->[1] eq 'body') {          if (@{$self->{open_elements}} > 1 and
3026            ## ISSUE: There is an issue in the spec.              $self->{open_elements}->[1]->[1] eq 'body') {
3027            if ($self->{open_elements}->[-1]->[1] ne 'body') {            for (@{$self->{open_elements}}) {
3028              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);              unless ({
3029                           dd => 1, dt => 1, li => 1, p => 1, td => 1,
3030                           th => 1, tr => 1, body => 1, html => 1,
3031                        }->{$_->[1]}) {
3032                  !!!parse-error (type => 'not closed:'.$_->[1]);
3033                }
3034            }            }
3035    
3036            $self->{insertion_mode} = 'after body';            $self->{insertion_mode} = 'after body';
3037            !!!next-token;            !!!next-token;
3038            return;            return;
# Line 2807  sub _tree_construction_main ($) { Line 3061  sub _tree_construction_main ($) {
3061                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
3062                  div => 1, dl => 1, fieldset => 1, listing => 1,                  div => 1, dl => 1, fieldset => 1, listing => 1,
3063                  menu => 1, ol => 1, pre => 1, ul => 1,                  menu => 1, ol => 1, pre => 1, ul => 1,
                 form => 1,  
3064                  p => 1,                  p => 1,
3065                  dd => 1, dt => 1, li => 1,                  dd => 1, dt => 1, li => 1,
3066                  button => 1, marquee => 1, object => 1,                  button => 1, marquee => 1, object => 1,
# Line 2845  sub _tree_construction_main ($) { Line 3098  sub _tree_construction_main ($) {
3098          }          }
3099                    
3100          splice @{$self->{open_elements}}, $i if defined $i;          splice @{$self->{open_elements}}, $i if defined $i;
         undef $self->{form_element} if $token->{tag_name} eq 'form';  
3101          $clear_up_to_marker->()          $clear_up_to_marker->()
3102            if {            if {
3103              button => 1, marquee => 1, object => 1,              button => 1, marquee => 1, object => 1,
3104            }->{$token->{tag_name}};            }->{$token->{tag_name}};
3105          !!!next-token;          !!!next-token;
3106          return;          return;
3107          } elsif ($token->{tag_name} eq 'form') {
3108            ## has an element in scope
3109            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3110              my $node = $self->{open_elements}->[$_];
3111              if ($node->[1] eq $token->{tag_name}) {
3112                ## generate implied end tags
3113                if ({
3114                     dd => 1, dt => 1, li => 1, p => 1,
3115                     td => 1, th => 1, tr => 1,
3116                    }->{$self->{open_elements}->[-1]->[1]}) {
3117                  !!!back-token;
3118                  $token = {type => 'end tag',
3119                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3120                  return;
3121                }
3122                last INSCOPE;
3123              } elsif ({
3124                        table => 1, caption => 1, td => 1, th => 1,
3125                        button => 1, marquee => 1, object => 1, html => 1,
3126                       }->{$node->[1]}) {
3127                last INSCOPE;
3128              }
3129            } # INSCOPE
3130            
3131            if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {
3132              pop @{$self->{open_elements}};
3133            } else {
3134              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3135            }
3136    
3137            undef $self->{form_element};
3138            !!!next-token;
3139            return;
3140        } elsif ({        } elsif ({
3141                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
3142                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
# Line 2953  sub _tree_construction_main ($) { Line 3238  sub _tree_construction_main ($) {
3238                  #not $phrasing_category->{$node->[1]} and                  #not $phrasing_category->{$node->[1]} and
3239                  ($special_category->{$node->[1]} or                  ($special_category->{$node->[1]} or
3240                   $scoping_category->{$node->[1]})) {                   $scoping_category->{$node->[1]})) {
3241                !!!parse-error (type => 'not closed:'.$node->[1]);                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3242                ## Ignore the token                ## Ignore the token
3243                !!!next-token;                !!!next-token;
3244                last S2;                last S2;
# Line 2982  sub _tree_construction_main ($) { Line 3267  sub _tree_construction_main ($) {
3267          redo B;          redo B;
3268        } elsif ($token->{type} eq 'start tag' and        } elsif ($token->{type} eq 'start tag' and
3269                 $token->{tag_name} eq 'html') {                 $token->{tag_name} eq 'html') {
3270          ## TODO: unless it is the first start tag token, parse-error  ## ISSUE: "aa<html>" is not a parse error.
3271    ## ISSUE: "<html>" in fragment is not a parse error.
3272            unless ($token->{first_start_tag}) {
3273              !!!parse-error (type => 'not first start tag');
3274            }
3275          my $top_el = $self->{open_elements}->[0]->[0];          my $top_el = $self->{open_elements}->[0]->[0];
3276          for my $attr_name (keys %{$token->{attributes}}) {          for my $attr_name (keys %{$token->{attributes}}) {
3277            unless ($top_el->has_attribute_ns (undef, $attr_name)) {            unless ($top_el->has_attribute_ns (undef, $attr_name)) {
# Line 3056  sub _tree_construction_main ($) { Line 3345  sub _tree_construction_main ($) {
3345              }              }
3346              redo B;              redo B;
3347            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} eq 'end tag') {
3348              if ($token->{tag_name} eq 'html') {              if ({head => 1, body => 1, html => 1}->{$token->{tag_name}}) {
3349                ## As if <head>                ## As if <head>
3350                !!!create-element ($self->{head_element}, 'head');                !!!create-element ($self->{head_element}, 'head');
3351                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
# Line 3066  sub _tree_construction_main ($) { Line 3355  sub _tree_construction_main ($) {
3355                redo B;                redo B;
3356              } else {              } else {
3357                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3358                ## Ignore the token                ## Ignore the token ## ISSUE: An issue in the spec.
3359                !!!next-token;                !!!next-token;
3360                redo B;                redo B;
3361              }              }
3362            } else {            } else {
3363              die "$0: $token->{type}: Unknown type";              die "$0: $token->{type}: Unknown type";
3364            }            }
3365          } elsif ($self->{insertion_mode} eq 'in head') {          } elsif ($self->{insertion_mode} eq 'in head' or
3366                     $self->{insertion_mode} eq 'in head noscript' or
3367                     $self->{insertion_mode} eq 'after head') {
3368            if ($token->{type} eq 'character') {            if ($token->{type} eq 'character') {
3369              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
3370                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
# Line 3090  sub _tree_construction_main ($) { Line 3381  sub _tree_construction_main ($) {
3381              !!!next-token;              !!!next-token;
3382              redo B;              redo B;
3383            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} eq 'start tag') {
3384              if ($token->{tag_name} eq 'title') {              if ({base => ($self->{insertion_mode} eq 'in head' or
3385                ## NOTE: There is an "as if in head" code clone                            $self->{insertion_mode} eq 'after head'),
3386                my $title_el;                   link => 1, meta => 1}->{$token->{tag_name}}) {
3387                !!!create-element ($title_el, 'title', $token->{attributes});                ## NOTE: There is a "as if in head" code clone.
3388                (defined $self->{head_element} ? $self->{head_element} : $self->{open_elements}->[-1]->[0])                if ($self->{insertion_mode} eq 'after head') {
3389                  ->append_child ($title_el);                  !!!parse-error (type => 'after head:'.$token->{tag_name});
3390                $self->{content_model_flag} = 'RCDATA';                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3391                  }
3392                my $text = '';                !!!insert-element ($token->{tag_name}, $token->{attributes});
3393                  pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
3394                  ## TODO: Extracting |charset| from |meta|.
3395                  pop @{$self->{open_elements}}
3396                      if $self->{insertion_mode} eq 'after head';
3397                !!!next-token;                !!!next-token;
3398                while ($token->{type} eq 'character') {                redo B;
3399                  $text .= $token->{data};              } elsif ($token->{tag_name} eq 'title' and
3400                         $self->{insertion_mode} eq 'in head') {
3401                  ## NOTE: There is a "as if in head" code clone.
3402                  if ($self->{insertion_mode} eq 'after head') {
3403                    !!!parse-error (type => 'after head:'.$token->{tag_name});
3404                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3405                  }
3406                  $parse_rcdata->('RCDATA', $insert_to_current);
3407                  pop @{$self->{open_elements}}
3408                      if $self->{insertion_mode} eq 'after head';
3409                  redo B;
3410                } elsif ($token->{tag_name} eq 'style') {
3411                  ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
3412                  ## insertion mode 'in head')
3413                  ## NOTE: There is a "as if in head" code clone.
3414                  if ($self->{insertion_mode} eq 'after head') {
3415                    !!!parse-error (type => 'after head:'.$token->{tag_name});
3416                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3417                  }
3418                  $parse_rcdata->('CDATA', $insert_to_current);
3419                  pop @{$self->{open_elements}}
3420                      if $self->{insertion_mode} eq 'after head';
3421                  redo B;
3422                } elsif ($token->{tag_name} eq 'noscript') {
3423                  if ($self->{insertion_mode} eq 'in head') {
3424                    ## NOTE: and scripting is disalbed
3425                    !!!insert-element ($token->{tag_name}, $token->{attributes});
3426                    $self->{insertion_mode} = 'in head noscript';
3427                  !!!next-token;                  !!!next-token;
3428                }                  redo B;
3429                if (length $text) {                } elsif ($self->{insertion_mode} eq 'in head noscript') {
3430                  $title_el->manakai_append_text ($text);                  !!!parse-error (type => 'noscript in noscript');
               }  
                 
               $self->{content_model_flag} = 'PCDATA';  
                 
               if ($token->{type} eq 'end tag' and  
                   $token->{tag_name} eq 'title') {  
3431                  ## Ignore the token                  ## Ignore the token
3432                    redo B;
3433                } else {                } else {
3434                  !!!parse-error (type => 'in RCDATA:#'.$token->{type});                  #
                 ## ISSUE: And ignore?  
3435                }                }
3436                } elsif ($token->{tag_name} eq 'head' and
3437                         $self->{insertion_mode} ne 'after head') {
3438                  !!!parse-error (type => 'in head:head'); # or in head noscript
3439                  ## Ignore the token
3440                !!!next-token;                !!!next-token;
3441                redo B;                redo B;
3442              } elsif ($token->{tag_name} eq 'style') {              } elsif ($self->{insertion_mode} ne 'in head noscript' and
3443                $style_start_tag->();                       $token->{tag_name} eq 'script') {
3444                  if ($self->{insertion_mode} eq 'after head') {
3445                    !!!parse-error (type => 'after head:'.$token->{tag_name});
3446                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3447                  }
3448                  ## NOTE: There is a "as if in head" code clone.
3449                  $script_start_tag->($insert_to_current);
3450                  pop @{$self->{open_elements}}
3451                      if $self->{insertion_mode} eq 'after head';
3452                redo B;                redo B;
3453              } elsif ($token->{tag_name} eq 'script') {              } elsif ($self->{insertion_mode} eq 'after head' and
3454                $script_start_tag->();                       $token->{tag_name} eq 'body') {
3455                redo B;                !!!insert-element ('body', $token->{attributes});
3456              } elsif ({base => 1, link => 1, meta => 1}->{$token->{tag_name}}) {                $self->{insertion_mode} = 'in body';
               ## NOTE: There are "as if in head" code clones  
               my $el;  
               !!!create-element ($el, $token->{tag_name}, $token->{attributes});  
               (defined $self->{head_element} ? $self->{head_element} : $self->{open_elements}->[-1]->[0])  
                 ->append_child ($el);  
   
3457                !!!next-token;                !!!next-token;
3458                redo B;                redo B;
3459              } elsif ($token->{tag_name} eq 'head') {              } elsif ($self->{insertion_mode} eq 'after head' and
3460                !!!parse-error (type => 'in head:head');                       $token->{tag_name} eq 'frameset') {
3461                ## Ignore the token                !!!insert-element ('frameset', $token->{attributes});
3462                  $self->{insertion_mode} = 'in frameset';
3463                !!!next-token;                !!!next-token;
3464                redo B;                redo B;
3465              } else {              } else {
3466                #                #
3467              }              }
3468            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} eq 'end tag') {
3469              if ($token->{tag_name} eq 'head') {              if ($self->{insertion_mode} eq 'in head' and
3470                if ($self->{open_elements}->[-1]->[1] eq 'head') {                  $token->{tag_name} eq 'head') {
3471                  pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
               } else {  
                 !!!parse-error (type => 'unmatched end tag:head');  
               }  
3472                $self->{insertion_mode} = 'after head';                $self->{insertion_mode} = 'after head';
3473                !!!next-token;                !!!next-token;
3474                redo B;                redo B;
3475              } elsif ($token->{tag_name} eq 'html') {              } elsif ($self->{insertion_mode} eq 'in head noscript' and
3476                    $token->{tag_name} eq 'noscript') {
3477                  pop @{$self->{open_elements}};
3478                  $self->{insertion_mode} = 'in head';
3479                  !!!next-token;
3480                  redo B;
3481                } elsif ($self->{insertion_mode} eq 'in head' and
3482                         ($token->{tag_name} eq 'body' or
3483                          $token->{tag_name} eq 'html')) {
3484                #                #
3485              } else {              } elsif ($self->{insertion_mode} ne 'after head') {
3486                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3487                ## Ignore the token                ## Ignore the token
3488                !!!next-token;                !!!next-token;
3489                redo B;                redo B;
3490                } else {
3491                  #
3492              }              }
3493            } else {            } else {
3494              #              #
3495            }            }
3496    
3497            if ($self->{open_elements}->[-1]->[1] eq 'head') {            ## As if </head> or </noscript> or <body>
3498              ## As if </head>            if ($self->{insertion_mode} eq 'in head') {
3499                pop @{$self->{open_elements}};
3500                $self->{insertion_mode} = 'after head';
3501              } elsif ($self->{insertion_mode} eq 'in head noscript') {
3502              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
3503                !!!parse-error (type => 'in noscript:'.(defined $token->{tag_name} ? ($token->{type} eq 'end tag' ? '/' : '') . $token->{tag_name} : '#' . $token->{type}));
3504                $self->{insertion_mode} = 'in head';
3505              } else { # 'after head'
3506                !!!insert-element ('body');
3507                $self->{insertion_mode} = 'in body';
3508            }            }
           $self->{insertion_mode} = 'after head';  
3509            ## reprocess            ## reprocess
3510            redo B;            redo B;
3511    
3512            ## ISSUE: An issue in the spec.            ## ISSUE: An issue in the spec.
         } elsif ($self->{insertion_mode} eq 'after head') {  
           if ($token->{type} eq 'character') {  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
               
             #  
           } 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;  
3513          } elsif ($self->{insertion_mode} eq 'in body') {          } elsif ($self->{insertion_mode} eq 'in body') {
3514            if ($token->{type} eq 'character') {            if ($token->{type} eq 'character') {
3515              ## NOTE: There is a code clone of "character in body".              ## NOTE: There is a code clone of "character in body".
# Line 4868  sub set_inner_html ($$$) { Line 5158  sub set_inner_html ($$$) {
5158      ## NOTE: Most of this code is copied from |parse_string|      ## NOTE: Most of this code is copied from |parse_string|
5159    
5160      ## Step 1 # MUST      ## Step 1 # MUST
5161      my $doc = $node->owner_document->implementation->create_document;      my $this_doc = $node->owner_document;
5162      ## TODO: Mark as HTML document      my $doc = $this_doc->implementation->create_document;
5163        $doc->manakai_is_html (1);
5164      my $p = $class->new;      my $p = $class->new;
5165      $p->{document} = $doc;      $p->{document} = $doc;
5166    
# Line 4879  sub set_inner_html ($$$) { Line 5170  sub set_inner_html ($$$) {
5170      my $column = 0;      my $column = 0;
5171      $p->{set_next_input_character} = sub {      $p->{set_next_input_character} = sub {
5172        my $self = shift;        my $self = shift;
5173    
5174          pop @{$self->{prev_input_character}};
5175          unshift @{$self->{prev_input_character}}, $self->{next_input_character};
5176    
5177        $self->{next_input_character} = -1 and return if $i >= length $$s;        $self->{next_input_character} = -1 and return if $i >= length $$s;
5178        $self->{next_input_character} = ord substr $$s, $i++, 1;        $self->{next_input_character} = ord substr $$s, $i++, 1;
5179        $column++;        $column++;
# Line 4887  sub set_inner_html ($$$) { Line 5182  sub set_inner_html ($$$) {
5182          $line++;          $line++;
5183          $column = 0;          $column = 0;
5184        } elsif ($self->{next_input_character} == 0x000D) { # CR        } elsif ($self->{next_input_character} == 0x000D) { # CR
5185          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;  
           }  
         }  
5186          $self->{next_input_character} = 0x000A; # LF # MUST          $self->{next_input_character} = 0x000A; # LF # MUST
5187          $line++;          $line++;
5188          $column = 0;          $column = 0;
5189        } elsif ($self->{next_input_character} > 0x10FFFF) {        } elsif ($self->{next_input_character} > 0x10FFFF) {
5190          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
5191        } elsif ($self->{next_input_character} == 0x0000) { # NULL        } elsif ($self->{next_input_character} == 0x0000) { # NULL
5192            !!!parse-error (type => 'NULL');
5193          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
5194        }        }
5195      };      };
5196        $p->{prev_input_character} = [-1, -1, -1];
5197        $p->{next_input_character} = -1;
5198            
5199      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
5200        my (%opt) = @_;        my (%opt) = @_;
# Line 4984  sub set_inner_html ($$$) { Line 5273  sub set_inner_html ($$$) {
5273      ## Step 12 # MUST      ## Step 12 # MUST
5274      @cn = @{$root->child_nodes};      @cn = @{$root->child_nodes};
5275      for (@cn) {      for (@cn) {
5276          $this_doc->adopt_node ($_);
5277        $node->append_child ($_);        $node->append_child ($_);
5278      }      }
5279      ## ISSUE: adopt_node? mutation events?      ## ISSUE: mutation events?
5280    
5281      $p->_terminate_tree_constructor;      $p->_terminate_tree_constructor;
5282    } else {    } else {
# Line 5031  sub get_inner_html ($$$) { Line 5321  sub get_inner_html ($$$) {
5321            
5322      my $nt = $child->node_type;      my $nt = $child->node_type;
5323      if ($nt == 1) { # Element      if ($nt == 1) { # Element
5324        my $tag_name = lc $child->tag_name; ## ISSUE: Definition of "lowercase"        my $tag_name = $child->tag_name; ## TODO: manakai_tag_name
5325        $s .= '<' . $tag_name;        $s .= '<' . $tag_name;
5326          ## NOTE: Non-HTML case:
5327        ## ISSUE: Non-html elements        ## <http://permalink.gmane.org/gmane.org.w3c.whatwg.discuss/11191>
5328    
5329        my @attrs = @{$child->attributes}; # sort order MUST be stable        my @attrs = @{$child->attributes}; # sort order MUST be stable
5330        for my $attr (@attrs) { # order is implementation dependent        for my $attr (@attrs) { # order is implementation dependent
5331          my $attr_name = lc $attr->name; ## ISSUE: Definition of "lowercase"          my $attr_name = $attr->name; ## TODO: manakai_name
5332          $s .= ' ' . $attr_name . '="';          $s .= ' ' . $attr_name . '="';
5333          my $attr_value = $attr->value;          my $attr_value = $attr->value;
5334          ## escape          ## escape
# Line 5057  sub get_inner_html ($$$) { Line 5347  sub get_inner_html ($$$) {
5347          spacer => 1, wbr => 1,          spacer => 1, wbr => 1,
5348        }->{$tag_name};        }->{$tag_name};
5349    
5350          $s .= "\x0A" if $tag_name eq 'pre' or $tag_name eq 'textarea';
5351    
5352        if (not $in_cdata and {        if (not $in_cdata and {
5353          style => 1, script => 1, xmp => 1, iframe => 1,          style => 1, script => 1, xmp => 1, iframe => 1,
5354          noembed => 1, noframes => 1, noscript => 1,          noembed => 1, noframes => 1, noscript => 1,
5355            plaintext => 1,
5356        }->{$tag_name}) {        }->{$tag_name}) {
5357          unshift @node, 'cdata-out';          unshift @node, 'cdata-out';
5358          $in_cdata = 1;          $in_cdata = 1;

Legend:
Removed from v.1.10  
changed lines
  Added in v.1.28

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24