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

Contents of /markup/html/whatpm/Whatpm/XML/Parser.pm.src

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.8 - (hide annotations) (download) (as text)
Tue Oct 14 14:57:52 2008 UTC (16 years, 9 months ago) by wakaba
Branch: MAIN
Changes since 1.7: +2 -2 lines
File MIME type: application/x-wais-source
++ whatpm/t/xml/ChangeLog	14 Oct 2008 14:56:52 -0000
	* cdata-1.dat: Tests on CDATA section outside of the root element
	added.

2008-10-14  Wakaba  <wakaba@suika.fam.cx>

++ whatpm/Whatpm/HTML/ChangeLog	14 Oct 2008 14:57:28 -0000
	* Tokenizer.pm.src: Parse error if CDATA section is not closed or
	is placed outside of the root element.

2008-10-14  Wakaba  <wakaba@suika.fam.cx>

1 wakaba 1.1 package Whatpm::XML::Parser;
2     use strict;
3    
4     push our @ISA, 'Whatpm::HTML';
5     use Whatpm::HTML::Tokenizer qw/:token/;
6    
7     sub parse_char_string ($$$;$$) {
8     #my ($self, $s, $doc, $onerror, $get_wrapper) = @_;
9     my $self = shift;
10     my $s = ref $_[0] ? $_[0] : \($_[0]);
11     require Whatpm::Charset::DecodeHandle;
12     my $input = Whatpm::Charset::DecodeHandle::CharString->new ($s);
13     return $self->parse_char_stream ($input, @_[1..$#_]);
14     } # parse_char_string
15    
16     sub parse_char_stream ($$$;$$) {
17     my $self = ref $_[0] ? shift : shift->new;
18     my $input = $_[0];
19     $self->{document} = $_[1];
20     @{$self->{document}->child_nodes} = ();
21    
22     ## NOTE: |set_inner_html| copies most of this method's code
23    
24     $self->{confident} = 1 unless exists $self->{confident};
25     $self->{document}->input_encoding ($self->{input_encoding})
26     if defined $self->{input_encoding};
27     ## TODO: |{input_encoding}| is needless?
28    
29     $self->{line_prev} = $self->{line} = 1;
30     $self->{column_prev} = -1;
31     $self->{column} = 0;
32     $self->{set_nc} = sub {
33     my $self = shift;
34    
35     my $char = '';
36     if (defined $self->{next_nc}) {
37     $char = $self->{next_nc};
38     delete $self->{next_nc};
39     $self->{nc} = ord $char;
40     } else {
41     $self->{char_buffer} = '';
42     $self->{char_buffer_pos} = 0;
43    
44     my $count = $input->manakai_read_until
45     ($self->{char_buffer}, qr/[^\x00\x0A\x0D]/, $self->{char_buffer_pos});
46     if ($count) {
47     $self->{line_prev} = $self->{line};
48     $self->{column_prev} = $self->{column};
49     $self->{column}++;
50     $self->{nc}
51     = ord substr ($self->{char_buffer}, $self->{char_buffer_pos}++, 1);
52     return;
53     }
54    
55     if ($input->read ($char, 1)) {
56     $self->{nc} = ord $char;
57     } else {
58     $self->{nc} = -1;
59     return;
60     }
61     }
62    
63     ($self->{line_prev}, $self->{column_prev})
64     = ($self->{line}, $self->{column});
65     $self->{column}++;
66    
67     if ($self->{nc} == 0x000A) { # LF
68     !!!cp ('j1');
69     $self->{line}++;
70     $self->{column} = 0;
71     } elsif ($self->{nc} == 0x000D) { # CR
72     !!!cp ('j2');
73     ## TODO: support for abort/streaming
74     my $next = '';
75     if ($input->read ($next, 1) and $next ne "\x0A") {
76     $self->{next_nc} = $next;
77     }
78     $self->{nc} = 0x000A; # LF # MUST
79     $self->{line}++;
80     $self->{column} = 0;
81     } elsif ($self->{nc} == 0x0000) { # NULL
82     !!!cp ('j4');
83     !!!parse-error (type => 'NULL');
84     $self->{nc} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
85     }
86     };
87    
88     $self->{read_until} = sub {
89     #my ($scalar, $specials_range, $offset) = @_;
90     return 0 if defined $self->{next_nc};
91    
92     my $pattern = qr/[^$_[1]\x00\x0A\x0D]/;
93     my $offset = $_[2] || 0;
94    
95     if ($self->{char_buffer_pos} < length $self->{char_buffer}) {
96     pos ($self->{char_buffer}) = $self->{char_buffer_pos};
97     if ($self->{char_buffer} =~ /\G(?>$pattern)+/) {
98     substr ($_[0], $offset)
99     = substr ($self->{char_buffer}, $-[0], $+[0] - $-[0]);
100     my $count = $+[0] - $-[0];
101     if ($count) {
102     $self->{column} += $count;
103     $self->{char_buffer_pos} += $count;
104     $self->{line_prev} = $self->{line};
105     $self->{column_prev} = $self->{column} - 1;
106     $self->{nc} = -1;
107     }
108     return $count;
109     } else {
110     return 0;
111     }
112     } else {
113     my $count = $input->manakai_read_until ($_[0], $pattern, $_[2]);
114     if ($count) {
115     $self->{column} += $count;
116     $self->{line_prev} = $self->{line};
117     $self->{column_prev} = $self->{column} - 1;
118     $self->{nc} = -1;
119     }
120     return $count;
121     }
122     }; # $self->{read_until}
123    
124     my $onerror = $_[2] || sub {
125     my (%opt) = @_;
126     my $line = $opt{token} ? $opt{token}->{line} : $opt{line};
127     my $column = $opt{token} ? $opt{token}->{column} : $opt{column};
128     warn "Parse error ($opt{type}) at line $line column $column\n";
129     };
130     $self->{parse_error} = sub {
131     $onerror->(line => $self->{line}, column => $self->{column}, @_);
132     };
133    
134     my $char_onerror = sub {
135     my (undef, $type, %opt) = @_;
136     !!!parse-error (layer => 'encode',
137     line => $self->{line}, column => $self->{column} + 1,
138     %opt, type => $type);
139     }; # $char_onerror
140    
141     if ($_[3]) {
142     $input = $_[3]->($input);
143     $input->onerror ($char_onerror);
144     } else {
145     $input->onerror ($char_onerror) unless defined $input->onerror;
146     }
147    
148     $self->_initialize_tokenizer;
149     $self->_initialize_tree_constructor;
150     $self->_construct_tree;
151     $self->_terminate_tree_constructor;
152    
153     delete $self->{parse_error}; # remove loop
154    
155     return $self->{document};
156     } # parse_char_stream
157    
158     sub new ($) {
159     my $class = shift;
160     my $self = bless {
161     level => {must => 'm',
162     should => 's',
163     warn => 'w',
164     info => 'i',
165     uncertain => 'u'},
166     }, $class;
167     $self->{set_nc} = sub {
168     $self->{nc} = -1;
169     };
170     $self->{parse_error} = sub {
171     #
172     };
173     $self->{change_encoding} = sub {
174     # if ($_[0] is a supported encoding) {
175     # run "change the encoding" algorithm;
176     # throw Whatpm::HTML::RestartParser (charset => $new_encoding);
177     # }
178     };
179     $self->{application_cache_selection} = sub {
180     #
181     };
182 wakaba 1.2
183     $self->{is_xml} = 1;
184    
185 wakaba 1.1 return $self;
186     } # new
187    
188     sub _initialize_tree_constructor ($) {
189     my $self = shift;
190     ## NOTE: $self->{document} MUST be specified before this method is called
191     $self->{document}->strict_error_checking (0);
192     ## TODO: Turn mutation events off # MUST
193     $self->{document}->dom_config
194     ->{'http://suika.fam.cx/www/2006/dom-config/strict-document-children'}
195     = 0;
196     $self->{document}->manakai_is_html (0);
197     $self->{document}->set_user_data (manakai_source_line => 1);
198     $self->{document}->set_user_data (manakai_source_column => 1);
199     } # _initialize_tree_constructor
200    
201     sub _terminate_tree_constructor ($) {
202     my $self = shift;
203     $self->{document}->strict_error_checking (1);
204     $self->{document}->dom_config
205     ->{'http://suika.fam.cx/www/2006/dom-config/strict-document-children'}
206     = 1;
207     ## TODO: Turn mutation events on
208     } # _terminate_tree_constructor
209    
210     ## Tree construction stage
211    
212    
213     ## NOTE: Differences from the XML5 draft are marked as "XML5:".
214    
215     ## XML5: No namespace support.
216    
217     ## XML5: XML5 has "empty tag token". In this implementation, it is
218 wakaba 1.3 ## represented as a start tag token with $self->{self_closing} flag
219 wakaba 1.1 ## set to true.
220    
221     ## XML5: XML5 has "short end tag token". In this implementation, it
222     ## is represented as an end tag token with $token->{tag_name} flag set
223     ## to an empty string.
224    
225     ## XML5: Start, main, end phases. In this implementation, they are
226     ## represented by insertion modes.
227    
228     ## Insertion modes
229     sub INITIAL_IM () { 0 }
230     sub BEFORE_ROOT_ELEMENT_IM () { 1 }
231     sub IN_ELEMENT_IM () { 2 }
232     sub AFTER_ROOT_ELEMENT_IM () { 3 }
233    
234     {
235     my $token; ## TODO: change to $self->{t}
236    
237     sub _construct_tree ($) {
238     my ($self) = @_;
239    
240     delete $self->{tainted};
241     $self->{open_elements} = [];
242     $self->{insertion_mode} = INITIAL_IM;
243 wakaba 1.8
244     !!!next-token;
245 wakaba 1.1
246     while (1) {
247     if ($self->{insertion_mode} == IN_ELEMENT_IM) {
248     $self->_tree_in_element;
249     } elsif ($self->{insertion_mode} == AFTER_ROOT_ELEMENT_IM) {
250     $self->_tree_after_root_element;
251     } elsif ($self->{insertion_mode} == BEFORE_ROOT_ELEMENT_IM) {
252     $self->_tree_before_root_element;
253     } elsif ($self->{insertion_mode} == INITIAL_IM) {
254     $self->_tree_initial;
255     } else {
256     die "$0: Unknown XML insertion mode: $self->{insertion_mode}";
257     }
258    
259     last if $token->{type} == ABORT_TOKEN;
260     }
261     } # _construct_tree
262    
263     sub _tree_initial ($) {
264     my $self = shift;
265    
266     B: while (1) {
267     if ($token->{type} == DOCTYPE_TOKEN) {
268     ## XML5: No "DOCTYPE" token.
269    
270     my $doctype = $self->{document}->create_document_type_definition
271     (defined $token->{name} ? $token->{name} : '');
272    
273     ## NOTE: Default value for both |public_id| and |system_id| attributes
274     ## are empty strings, so that we don't set any value in missing cases.
275     $doctype->public_id ($token->{public_identifier})
276     if defined $token->{public_identifier};
277     $doctype->system_id ($token->{system_identifier})
278     if defined $token->{system_identifier};
279    
280     ## TODO: internal_subset
281    
282     $self->{document}->append_child ($doctype);
283    
284     $self->{insertion_mode} = BEFORE_ROOT_ELEMENT_IM;
285     !!!next-token;
286     return;
287     } elsif ($token->{type} == START_TAG_TOKEN or
288     $token->{type} == END_OF_FILE_TOKEN) {
289     $self->{insertion_mode} = BEFORE_ROOT_ELEMENT_IM;
290     ## Reprocess.
291     return;
292     } elsif ($token->{type} == COMMENT_TOKEN) {
293     my $comment = $self->{document}->create_comment ($token->{data});
294     $self->{document}->append_child ($comment);
295    
296     ## Stay in the mode.
297     !!!next-token;
298     next B;
299     } elsif ($token->{type} == PI_TOKEN) {
300     my $pi = $self->{document}->create_processing_instruction
301     ($token->{target}, $token->{data});
302     $self->{document}->append_child ($pi);
303    
304     ## Stay in the mode.
305     !!!next-token;
306     next B;
307     } elsif ($token->{type} == CHARACTER_TOKEN) {
308     if (not $self->{tainted} and
309     $token->{data} =~ s/^([\x09\x0A\x0C\x20]+)//) {
310     #
311     }
312    
313     if (length $token->{data}) {
314     ## XML5: Ignore the token.
315    
316     unless ($self->{tainted}) {
317     !!!parse-error (type => 'text outside of root element',
318     token => $token);
319     $self->{tainted} = 1;
320     }
321    
322     $self->{document}->manakai_append_text ($token->{data});
323     }
324    
325     ## Stay in the mode.
326     !!!next-token;
327     next B;
328     } elsif ($token->{type} == END_TAG_TOKEN) {
329     !!!parse-error (type => 'unmatched end tag',
330     text => $token->{tag_name},
331     token => $token);
332     ## Ignore the token.
333    
334     ## Stay in the mode.
335     !!!next-token;
336     next B;
337     } elsif ($token->{type} == ABORT_TOKEN) {
338     return;
339     } else {
340     die "$0: XML parser initial: Unknown token type $token->{type}";
341     }
342     } # B
343     } # _tree_initial
344    
345     sub _tree_before_root_element ($) {
346     my $self = shift;
347    
348     B: while (1) {
349     if ($token->{type} == START_TAG_TOKEN) {
350 wakaba 1.6 my $nsmap = {
351     xml => q<http://www.w3.org/XML/1998/namespace>,
352     xmlns => q<http://www.w3.org/2000/xmlns/>,
353     };
354    
355     for (keys %{$token->{attributes}}) {
356     if (/^xmlns:./s) {
357     my $prefix = substr $_, 6;
358     my $value = $token->{attributes}->{$_}->{value};
359     if ($prefix eq 'xml' or $prefix eq 'xmlns' or
360     $value eq q<http://www.w3.org/XML/1998/namespace> or
361     $value eq q<http://www.w3.org/2000/xmlns/>) {
362     ## NOTE: Error should be detected at the DOM layer.
363     #
364     } elsif (length $value) {
365     $nsmap->{$prefix} = $value;
366     } else {
367     delete $nsmap->{$prefix};
368     ## TODO: Error unless XML1.1
369     }
370     } elsif ($_ eq 'xmlns') {
371     my $value = $token->{attributes}->{$_}->{value};
372     if ($value eq q<http://www.w3.org/XML/1998/namespace> or
373     $value eq q<http://www.w3.org/2000/xmlns/>) {
374     ## NOTE: Error should be detected at the DOM layer.
375     #
376     } elsif (length $value) {
377     $nsmap->{''} = $value;
378     } else {
379     delete $nsmap->{''};
380     }
381     }
382     }
383    
384     my $ns;
385 wakaba 1.3 my ($prefix, $ln) = split /:/, $token->{tag_name}, 2;
386 wakaba 1.6
387     if (defined $ln) { # prefixed
388     if (defined $nsmap->{$prefix}) {
389     $ns = $nsmap->{$prefix};
390     } else {
391     ## NOTE: Error should be detected at the DOM layer.
392     ($prefix, $ln) = (undef, $token->{tag_name});
393     }
394     } else {
395     ($prefix, $ln) = (undef, $prefix);
396     $ns = $nsmap->{''};
397     }
398    
399 wakaba 1.3 my $el = $self->{document}->create_element_ns ($ns, [$prefix, $ln]);
400 wakaba 1.4 $el->set_user_data (manakai_source_line => $token->{line});
401     $el->set_user_data (manakai_source_column => $token->{column});
402    
403 wakaba 1.6 my $has_attr;
404     for my $attr_name (sort {$a cmp $b} keys %{$token->{attributes}}) {
405     my $ns;
406 wakaba 1.4 my ($p, $l) = split /:/, $attr_name, 2;
407 wakaba 1.6
408     if ($attr_name eq 'xmlns:xmlns') {
409     ($p, $l) = (undef, $attr_name);
410     } elsif (defined $l) { # prefixed
411     if (defined $nsmap->{$p}) {
412     $ns = $nsmap->{$p};
413     } else {
414     ## NOTE: Error should be detected at the DOM-layer.
415     ($p, $l) = (undef, $attr_name);
416     }
417     } else {
418     if ($p eq 'xmlns') {
419     $ns = $nsmap->{xmlns};
420     }
421     ($p, $l) = (undef, $p);
422     }
423    
424     if ($has_attr->{defined $ns ? $ns : ''}->{$l}) {
425     ## NOTE: Attributes are sorted as Unicode characters (not
426     ## code units) of their names, for stable output.
427    
428     ## TODO: Should be sorted by source order?
429     !!!parse-error (type => 'duplicate ns attr',
430     token => $token,
431     value => $attr_name);
432     next;
433     } else {
434     $has_attr->{defined $ns ? $ns : ''}->{$l} = 1;
435     }
436    
437 wakaba 1.4 my $attr_t = $token->{attributes}->{$attr_name};
438     my $attr = $self->{document}->create_attribute_ns ($ns, [$p, $l]);
439     $attr->value ($attr_t->{value});
440     $attr->set_user_data (manakai_source_line => $attr_t->{line});
441     $attr->set_user_data (manakai_source_column => $attr_t->{column});
442     $el->set_attribute_node_ns ($attr);
443     }
444    
445 wakaba 1.1 $self->{document}->append_child ($el);
446    
447 wakaba 1.3 if ($self->{self_closing}) {
448 wakaba 1.1 !!!ack ('ack');
449     $self->{insertion_mode} = AFTER_ROOT_ELEMENT_IM;
450     } else {
451 wakaba 1.6 push @{$self->{open_elements}}, [$el, $token->{tag_name}, $nsmap];
452 wakaba 1.1 $self->{insertion_mode} = IN_ELEMENT_IM;
453     }
454    
455     #delete $self->{tainted};
456    
457     !!!next-token;
458     return;
459     } elsif ($token->{type} == COMMENT_TOKEN) {
460     my $comment = $self->{document}->create_comment ($token->{data});
461     $self->{document}->append_child ($comment);
462    
463     ## Stay in the mode.
464     !!!next-token;
465     next B;
466     } elsif ($token->{type} == PI_TOKEN) {
467     my $pi = $self->{document}->create_processing_instruction
468     ($token->{target}, $token->{data});
469     $self->{document}->append_child ($pi);
470    
471     ## Stay in the mode.
472     !!!next-token;
473     next B;
474     } elsif ($token->{type} == CHARACTER_TOKEN) {
475     if (not $self->{tainted} and
476     $token->{data} =~ s/^([\x09\x0A\x0C\x20]+)//) {
477     #
478     }
479    
480     if (length $token->{data}) {
481     ## XML5: Ignore the token.
482    
483     unless ($self->{tainted}) {
484     !!!parse-error (type => 'text outside of root element',
485     token => $token);
486     $self->{tainted} = 1;
487     }
488    
489     $self->{document}->manakai_append_text ($token->{data});
490     }
491    
492     ## Stay in the mode.
493     !!!next-token;
494     next B;
495     } elsif ($token->{type} == END_OF_FILE_TOKEN) {
496     !!!parse-error (type => 'no root element',
497     token => $token);
498    
499     $self->{insertion_mode} = AFTER_ROOT_ELEMENT_IM;
500     ## Reprocess.
501     return;
502     } elsif ($token->{type} == END_TAG_TOKEN) {
503     !!!parse-error (type => 'unmatched end tag',
504     text => $token->{tag_name},
505     token => $token);
506     ## Ignore the token.
507    
508     ## Stay in the mode.
509     !!!next-token;
510     next B;
511     } elsif ($token->{type} == DOCTYPE_TOKEN) {
512     !!!parse-error (type => 'in html:#doctype',
513     token => $token);
514     ## Ignore the token.
515    
516     ## Stay in the mode.
517     !!!next-token;
518     next B;
519     } elsif ($token->{type} == ABORT_TOKEN) {
520     return;
521     } else {
522     die "$0: XML parser initial: Unknown token type $token->{type}";
523     }
524     } # B
525     } # _tree_before_root_element
526    
527     sub _tree_in_element ($) {
528     my $self = shift;
529    
530     B: while (1) {
531     if ($token->{type} == CHARACTER_TOKEN) {
532     $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
533    
534     ## Stay in the mode.
535     !!!next-token;
536     next B;
537     } elsif ($token->{type} == START_TAG_TOKEN) {
538 wakaba 1.7 my $nsmap = {%{$self->{open_elements}->[-1]->[2]}};
539    
540     for (keys %{$token->{attributes}}) {
541     if (/^xmlns:./s) {
542     my $prefix = substr $_, 6;
543     my $value = $token->{attributes}->{$_}->{value};
544     if ($prefix eq 'xml' or $prefix eq 'xmlns' or
545     $value eq q<http://www.w3.org/XML/1998/namespace> or
546     $value eq q<http://www.w3.org/2000/xmlns/>) {
547     ## NOTE: Error should be detected at the DOM layer.
548     #
549     } elsif (length $value) {
550     $nsmap->{$prefix} = $value;
551     } else {
552     delete $nsmap->{$prefix};
553     ## TODO: Error unless XML1.1
554     }
555     } elsif ($_ eq 'xmlns') {
556     my $value = $token->{attributes}->{$_}->{value};
557     if ($value eq q<http://www.w3.org/XML/1998/namespace> or
558     $value eq q<http://www.w3.org/2000/xmlns/>) {
559     ## NOTE: Error should be detected at the DOM layer.
560     #
561     } elsif (length $value) {
562     $nsmap->{''} = $value;
563     } else {
564     delete $nsmap->{''};
565     }
566     }
567     }
568    
569     my $ns;
570 wakaba 1.3 my ($prefix, $ln) = split /:/, $token->{tag_name}, 2;
571 wakaba 1.7
572     if (defined $ln) { # prefixed
573     if (defined $nsmap->{$prefix}) {
574     $ns = $nsmap->{$prefix};
575     } else {
576     ## NOTE: Error should be detected at the DOM layer.
577     ($prefix, $ln) = (undef, $token->{tag_name});
578     }
579     } else {
580     ($prefix, $ln) = (undef, $prefix);
581     $ns = $nsmap->{''};
582     }
583    
584 wakaba 1.3 my $el = $self->{document}->create_element_ns ($ns, [$prefix, $ln]);
585 wakaba 1.4 $el->set_user_data (manakai_source_line => $token->{line});
586     $el->set_user_data (manakai_source_column => $token->{column});
587    
588 wakaba 1.7 my $has_attr;
589     for my $attr_name (sort {$a cmp $b} keys %{$token->{attributes}}) {
590     my $ns;
591 wakaba 1.4 my ($p, $l) = split /:/, $attr_name, 2;
592 wakaba 1.7
593     if ($attr_name eq 'xmlns:xmlns') {
594     ($p, $l) = (undef, $attr_name);
595     } elsif (defined $l) { # prefixed
596     if (defined $nsmap->{$p}) {
597     $ns = $nsmap->{$p};
598     } else {
599     ## NOTE: Error should be detected at the DOM-layer.
600     ($p, $l) = (undef, $attr_name);
601     }
602     } else {
603     if ($p eq 'xmlns') {
604     $ns = $nsmap->{xmlns};
605     }
606     ($p, $l) = (undef, $p);
607     }
608    
609     if ($has_attr->{defined $ns ? $ns : ''}->{$l}) {
610     ## NOTE: Attributes are sorted as Unicode characters (not
611     ## code units) of their names, for stable output.
612    
613     ## TODO: Should be sorted by source order?
614     !!!parse-error (type => 'duplicate ns attr',
615     token => $token,
616     value => $attr_name);
617     next;
618     } else {
619     $has_attr->{defined $ns ? $ns : ''}->{$l} = 1;
620     }
621    
622 wakaba 1.4 my $attr_t = $token->{attributes}->{$attr_name};
623     my $attr = $self->{document}->create_attribute_ns ($ns, [$p, $l]);
624     $attr->value ($attr_t->{value});
625     $attr->set_user_data (manakai_source_line => $attr_t->{line});
626     $attr->set_user_data (manakai_source_column => $attr_t->{column});
627     $el->set_attribute_node_ns ($attr);
628     }
629    
630 wakaba 1.1 $self->{open_elements}->[-1]->[0]->append_child ($el);
631    
632 wakaba 1.3 if ($self->{self_closing}) {
633 wakaba 1.1 !!!ack ('ack');
634     } else {
635 wakaba 1.7 push @{$self->{open_elements}}, [$el, $token->{tag_name}, $nsmap];
636 wakaba 1.1 }
637    
638     ## Stay in the mode.
639     !!!next-token;
640     next B;
641     } elsif ($token->{type} == END_TAG_TOKEN) {
642 wakaba 1.2 if ($token->{tag_name} eq '') {
643 wakaba 1.1 ## Short end tag token.
644     pop @{$self->{open_elements}};
645     } elsif ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {
646     pop @{$self->{open_elements}};
647     } else {
648     !!!parse-error (type => 'unmatched end tag',
649     text => $token->{tag_name},
650     token => $token);
651    
652     ## Has an element in scope
653 wakaba 1.2 INSCOPE: for my $i (reverse 0..$#{$self->{open_elements}}) {
654 wakaba 1.1 if ($self->{open_elements}->[$i]->[1] eq $token->{tag_name}) {
655     splice @{$self->{open_elements}}, $i;
656     last INSCOPE;
657     }
658     } # INSCOPE
659     }
660    
661     unless (@{$self->{open_elements}}) {
662     $self->{insertion_mode} = AFTER_ROOT_ELEMENT_IM;
663     !!!next-token;
664     return;
665     } else {
666     ## Stay in the state.
667     !!!next-token;
668     redo B;
669     }
670     } elsif ($token->{type} == COMMENT_TOKEN) {
671     my $comment = $self->{document}->create_comment ($token->{data});
672     $self->{open_elements}->[-1]->[0]->append_child ($comment);
673    
674     ## Stay in the mode.
675     !!!next-token;
676     next B;
677     } elsif ($token->{type} == PI_TOKEN) {
678     my $pi = $self->{document}->create_processing_instruction
679     ($token->{target}, $token->{data});
680     $self->{open_elements}->[-1]->[0]->append_child ($pi);
681    
682     ## Stay in the mode.
683     !!!next-token;
684     next B;
685     } elsif ($token->{type} == END_OF_FILE_TOKEN) {
686     !!!parse-error (type => 'in body:#eof',
687     token => $token);
688    
689     $self->{insertion_mode} = AFTER_ROOT_ELEMENT_IM;
690     !!!next-token;
691     return;
692     } elsif ($token->{type} == DOCTYPE_TOKEN) {
693     !!!parse-error (type => 'in html:#doctype',
694     token => $token);
695     ## Ignore the token.
696    
697     ## Stay in the mode.
698     !!!next-token;
699     next B;
700     } elsif ($token->{type} == ABORT_TOKEN) {
701     return;
702     } else {
703     die "$0: XML parser initial: Unknown token type $token->{type}";
704     }
705     } # B
706     } # _tree_in_element
707    
708     sub _tree_after_root_element ($) {
709     my $self = shift;
710    
711     B: while (1) {
712     if ($token->{type} == START_TAG_TOKEN) {
713     !!!parse-error (type => 'second root element',
714     token => $token);
715    
716     ## XML5: Ignore the token.
717 wakaba 1.4
718 wakaba 1.5 $self->{insertion_mode} = BEFORE_ROOT_ELEMENT_IM;
719     ## Reprocess.
720 wakaba 1.1 return;
721     } elsif ($token->{type} == COMMENT_TOKEN) {
722     my $comment = $self->{document}->create_comment ($token->{data});
723     $self->{document}->append_child ($comment);
724    
725     ## Stay in the mode.
726     !!!next-token;
727     next B;
728     } elsif ($token->{type} == PI_TOKEN) {
729     my $pi = $self->{document}->create_processing_instruction
730     ($token->{target}, $token->{data});
731     $self->{document}->append_child ($pi);
732    
733     ## Stay in the mode.
734     !!!next-token;
735     next B;
736     } elsif ($token->{type} == CHARACTER_TOKEN) {
737     if (not $self->{tainted} and
738     $token->{data} =~ s/^([\x09\x0A\x0C\x20]+)//) {
739     #
740     }
741    
742     if (length $token->{data}) {
743     ## XML5: Ignore the token.
744    
745     unless ($self->{tainted}) {
746     !!!parse-error (type => 'text outside of root element',
747     token => $token);
748     $self->{tainted} = 1;
749     }
750    
751     $self->{document}->manakai_append_text ($token->{data});
752     }
753    
754     ## Stay in the mode.
755     !!!next-token;
756     next B;
757     } elsif ($token->{type} == END_OF_FILE_TOKEN) {
758     ## Stop parsing.
759    
760     ## TODO: implement "stop parsing".
761    
762     $token = {type => ABORT_TOKEN};
763     return;
764     } elsif ($token->{type} == END_TAG_TOKEN) {
765     !!!parse-error (type => 'unmatched end tag',
766     text => $token->{tag_name},
767     token => $token);
768     ## Ignore the token.
769    
770     ## Stay in the mode.
771     !!!next-token;
772     next B;
773     } elsif ($token->{type} == DOCTYPE_TOKEN) {
774     !!!parse-error (type => 'in html:#doctype',
775     token => $token);
776     ## Ignore the token.
777    
778     ## Stay in the mode.
779     !!!next-token;
780     next B;
781     } elsif ($token->{type} == ABORT_TOKEN) {
782     return;
783     } else {
784     die "$0: XML parser initial: Unknown token type $token->{type}";
785     }
786     } # B
787     } # _tree_after_root_element
788    
789     }
790    
791     1;

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24