/[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.11 - (show annotations) (download) (as text)
Wed Oct 15 08:05:47 2008 UTC (17 years, 8 months ago) by wakaba
Branch: MAIN
Changes since 1.10: +12 -15 lines
File MIME type: application/x-wais-source
++ whatpm/t/ChangeLog	15 Oct 2008 08:04:32 -0000
	* XML-Parser.t: "xml/ns-elements-1.dat" added.

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

++ whatpm/t/xml/ChangeLog	15 Oct 2008 08:05:44 -0000
	* ns-elements-1.dat: New test data file.

	* ns-attrs-1.dat: New test data added.

	* elements-1.dat: New test data file.

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

++ whatpm/Whatpm/HTML/ChangeLog	15 Oct 2008 08:03:32 -0000
	* Tokenizer.pm.src: XML tag name start charcter support for start
	tags.

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

++ whatpm/Whatpm/XML/ChangeLog	15 Oct 2008 08:04:01 -0000
	* Parser.pm.src: Bug fixes for the handling of ":" in the element
	type names and attribute names.

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

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
183 $self->{is_xml} = 1;
184
185 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 ## represented as a start tag token with $self->{self_closing} flag
219 ## 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
244 !!!next-token;
245
246 ## XML5: No support for the XML declaration
247 if ($token->{type} == PI_TOKEN and
248 $token->{target} eq 'xml' and
249 $token->{data} =~ /\Aversion[\x09\x0A\x20]*=[\x09\x0A\x20]*
250 (?>"([^"]*)"|'([^']*)')
251 (?:[\x09\x0A\x20]+
252 encoding[\x09\x0A\x20]*=[\x09\x0A\x20]*
253 (?>"([^"]*)"|'([^']*)')[\x09\x0A\x20]*)?
254 (?:[\x09\x0A\x20]+
255 standalone[\x09\x0A\x20]*=[\x09\x0A\x20]*
256 (?>"(yes|no)"|'(yes|no)'))?
257 [\x09\x0A\x20]*\z/x) {
258 $self->{document}->xml_version (defined $1 ? $1 : $2);
259 $self->{document}->xml_encoding (defined $3 ? $3 : $4); # possibly undef
260 $self->{document}->xml_standalone (($5 || $6 || 'no') ne 'no');
261
262 !!!next-token;
263 } else {
264 $self->{document}->xml_version ('1.0');
265 $self->{document}->xml_encoding (undef);
266 $self->{document}->xml_standalone (0);
267 }
268
269 while (1) {
270 if ($self->{insertion_mode} == IN_ELEMENT_IM) {
271 $self->_tree_in_element;
272 } elsif ($self->{insertion_mode} == AFTER_ROOT_ELEMENT_IM) {
273 $self->_tree_after_root_element;
274 } elsif ($self->{insertion_mode} == BEFORE_ROOT_ELEMENT_IM) {
275 $self->_tree_before_root_element;
276 } elsif ($self->{insertion_mode} == INITIAL_IM) {
277 $self->_tree_initial;
278 } else {
279 die "$0: Unknown XML insertion mode: $self->{insertion_mode}";
280 }
281
282 last if $token->{type} == ABORT_TOKEN;
283 }
284 } # _construct_tree
285
286 sub _tree_initial ($) {
287 my $self = shift;
288
289 B: while (1) {
290 if ($token->{type} == DOCTYPE_TOKEN) {
291 ## XML5: No "DOCTYPE" token.
292
293 my $doctype = $self->{document}->create_document_type_definition
294 (defined $token->{name} ? $token->{name} : '');
295
296 ## NOTE: Default value for both |public_id| and |system_id| attributes
297 ## are empty strings, so that we don't set any value in missing cases.
298 $doctype->public_id ($token->{public_identifier})
299 if defined $token->{public_identifier};
300 $doctype->system_id ($token->{system_identifier})
301 if defined $token->{system_identifier};
302
303 ## TODO: internal_subset
304
305 $self->{document}->append_child ($doctype);
306
307 $self->{insertion_mode} = BEFORE_ROOT_ELEMENT_IM;
308 !!!next-token;
309 return;
310 } elsif ($token->{type} == START_TAG_TOKEN or
311 $token->{type} == END_OF_FILE_TOKEN) {
312 $self->{insertion_mode} = BEFORE_ROOT_ELEMENT_IM;
313 ## Reprocess.
314 return;
315 } elsif ($token->{type} == COMMENT_TOKEN) {
316 my $comment = $self->{document}->create_comment ($token->{data});
317 $self->{document}->append_child ($comment);
318
319 ## Stay in the mode.
320 !!!next-token;
321 next B;
322 } elsif ($token->{type} == PI_TOKEN) {
323 my $pi = $self->{document}->create_processing_instruction
324 ($token->{target}, $token->{data});
325 $self->{document}->append_child ($pi);
326
327 ## Stay in the mode.
328 !!!next-token;
329 next B;
330 } elsif ($token->{type} == CHARACTER_TOKEN) {
331 if (not $self->{tainted} and
332 not $token->{has_reference} and
333 $token->{data} =~ s/^([\x09\x0A\x0C\x20]+)//) {
334 #
335 }
336
337 if (length $token->{data}) {
338 ## XML5: Ignore the token.
339
340 unless ($self->{tainted}) {
341 !!!parse-error (type => 'text outside of root element',
342 token => $token);
343 $self->{tainted} = 1;
344 }
345
346 $self->{document}->manakai_append_text ($token->{data});
347 }
348
349 ## Stay in the mode.
350 !!!next-token;
351 next B;
352 } elsif ($token->{type} == END_TAG_TOKEN) {
353 !!!parse-error (type => 'unmatched end tag',
354 text => $token->{tag_name},
355 token => $token);
356 ## Ignore the token.
357
358 ## Stay in the mode.
359 !!!next-token;
360 next B;
361 } elsif ($token->{type} == ABORT_TOKEN) {
362 return;
363 } else {
364 die "$0: XML parser initial: Unknown token type $token->{type}";
365 }
366 } # B
367 } # _tree_initial
368
369 sub _tree_before_root_element ($) {
370 my $self = shift;
371
372 B: while (1) {
373 if ($token->{type} == START_TAG_TOKEN) {
374 my $nsmap = {
375 xml => q<http://www.w3.org/XML/1998/namespace>,
376 xmlns => q<http://www.w3.org/2000/xmlns/>,
377 };
378
379 for (keys %{$token->{attributes}}) {
380 if (/^xmlns:./s) {
381 my $prefix = substr $_, 6;
382 my $value = $token->{attributes}->{$_}->{value};
383 if ($prefix eq 'xml' or $prefix eq 'xmlns' or
384 $value eq q<http://www.w3.org/XML/1998/namespace> or
385 $value eq q<http://www.w3.org/2000/xmlns/>) {
386 ## NOTE: Error should be detected at the DOM layer.
387 #
388 } elsif (length $value) {
389 $nsmap->{$prefix} = $value;
390 } else {
391 delete $nsmap->{$prefix};
392 }
393 } elsif ($_ eq 'xmlns') {
394 my $value = $token->{attributes}->{$_}->{value};
395 if ($value eq q<http://www.w3.org/XML/1998/namespace> or
396 $value eq q<http://www.w3.org/2000/xmlns/>) {
397 ## NOTE: Error should be detected at the DOM layer.
398 #
399 } elsif (length $value) {
400 $nsmap->{''} = $value;
401 } else {
402 delete $nsmap->{''};
403 }
404 }
405 }
406
407 my $ns;
408 my ($prefix, $ln) = split /:/, $token->{tag_name}, 2;
409
410 if (defined $ln and $prefix ne '' and $ln ne '') { # prefixed
411 if (defined $nsmap->{$prefix}) {
412 $ns = $nsmap->{$prefix};
413 } else {
414 ($prefix, $ln) = (undef, $token->{tag_name});
415 }
416 } else {
417 $ns = $nsmap->{''} if $prefix ne '' and not defined $ln;
418 ($prefix, $ln) = (undef, $token->{tag_name});
419 }
420
421 my $el = $self->{document}->create_element_ns ($ns, [$prefix, $ln]);
422 $el->set_user_data (manakai_source_line => $token->{line});
423 $el->set_user_data (manakai_source_column => $token->{column});
424
425 my $has_attr;
426 for my $attr_name (sort {$a cmp $b} keys %{$token->{attributes}}) {
427 my $ns;
428 my ($p, $l) = split /:/, $attr_name, 2;
429
430 if ($attr_name eq 'xmlns:xmlns') {
431 ($p, $l) = (undef, $attr_name);
432 } elsif (defined $l and $p ne '' and $l ne '') { # prefixed
433 if (defined $nsmap->{$p}) {
434 $ns = $nsmap->{$p};
435 } else {
436 ## NOTE: Error should be detected at the DOM-layer.
437 ($p, $l) = (undef, $attr_name);
438 }
439 } else {
440 if ($attr_name eq 'xmlns') {
441 $ns = $nsmap->{xmlns};
442 }
443 ($p, $l) = (undef, $attr_name);
444 }
445
446 if ($has_attr->{defined $ns ? $ns : ''}->{$l}) {
447 ## NOTE: Attributes are sorted as Unicode characters (not
448 ## code units) of their names, for stable output.
449
450 ## TODO: Should be sorted by source order?
451 !!!parse-error (type => 'duplicate ns attr',
452 token => $token,
453 value => $attr_name);
454 next;
455 } else {
456 $has_attr->{defined $ns ? $ns : ''}->{$l} = 1;
457 }
458
459 my $attr_t = $token->{attributes}->{$attr_name};
460 my $attr = $self->{document}->create_attribute_ns ($ns, [$p, $l]);
461 $attr->value ($attr_t->{value});
462 $attr->set_user_data (manakai_source_line => $attr_t->{line});
463 $attr->set_user_data (manakai_source_column => $attr_t->{column});
464 $el->set_attribute_node_ns ($attr);
465 }
466
467 $self->{document}->append_child ($el);
468
469 if ($self->{self_closing}) {
470 !!!ack ('ack');
471 $self->{insertion_mode} = AFTER_ROOT_ELEMENT_IM;
472 } else {
473 push @{$self->{open_elements}}, [$el, $token->{tag_name}, $nsmap];
474 $self->{insertion_mode} = IN_ELEMENT_IM;
475 }
476
477 #delete $self->{tainted};
478
479 !!!next-token;
480 return;
481 } elsif ($token->{type} == COMMENT_TOKEN) {
482 my $comment = $self->{document}->create_comment ($token->{data});
483 $self->{document}->append_child ($comment);
484
485 ## Stay in the mode.
486 !!!next-token;
487 next B;
488 } elsif ($token->{type} == PI_TOKEN) {
489 my $pi = $self->{document}->create_processing_instruction
490 ($token->{target}, $token->{data});
491 $self->{document}->append_child ($pi);
492
493 ## Stay in the mode.
494 !!!next-token;
495 next B;
496 } elsif ($token->{type} == CHARACTER_TOKEN) {
497 if (not $self->{tainted} and
498 not $token->{has_reference} and
499 $token->{data} =~ s/^([\x09\x0A\x0C\x20]+)//) {
500 #
501 }
502
503 if (length $token->{data}) {
504 ## XML5: Ignore the token.
505
506 unless ($self->{tainted}) {
507 !!!parse-error (type => 'text outside of root element',
508 token => $token);
509 $self->{tainted} = 1;
510 }
511
512 $self->{document}->manakai_append_text ($token->{data});
513 }
514
515 ## Stay in the mode.
516 !!!next-token;
517 next B;
518 } elsif ($token->{type} == END_OF_FILE_TOKEN) {
519 !!!parse-error (type => 'no root element',
520 token => $token);
521
522 $self->{insertion_mode} = AFTER_ROOT_ELEMENT_IM;
523 ## Reprocess.
524 return;
525 } elsif ($token->{type} == END_TAG_TOKEN) {
526 !!!parse-error (type => 'unmatched end tag',
527 text => $token->{tag_name},
528 token => $token);
529 ## Ignore the token.
530
531 ## Stay in the mode.
532 !!!next-token;
533 next B;
534 } elsif ($token->{type} == DOCTYPE_TOKEN) {
535 !!!parse-error (type => 'in html:#doctype',
536 token => $token);
537 ## Ignore the token.
538
539 ## Stay in the mode.
540 !!!next-token;
541 next B;
542 } elsif ($token->{type} == ABORT_TOKEN) {
543 return;
544 } else {
545 die "$0: XML parser initial: Unknown token type $token->{type}";
546 }
547 } # B
548 } # _tree_before_root_element
549
550 sub _tree_in_element ($) {
551 my $self = shift;
552
553 B: while (1) {
554 if ($token->{type} == CHARACTER_TOKEN) {
555 $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
556
557 ## Stay in the mode.
558 !!!next-token;
559 next B;
560 } elsif ($token->{type} == START_TAG_TOKEN) {
561 my $nsmap = {%{$self->{open_elements}->[-1]->[2]}};
562
563 for (keys %{$token->{attributes}}) {
564 if (/^xmlns:./s) {
565 my $prefix = substr $_, 6;
566 my $value = $token->{attributes}->{$_}->{value};
567 if ($prefix eq 'xml' or $prefix eq 'xmlns' or
568 $value eq q<http://www.w3.org/XML/1998/namespace> or
569 $value eq q<http://www.w3.org/2000/xmlns/>) {
570 ## NOTE: Error should be detected at the DOM layer.
571 #
572 } elsif (length $value) {
573 $nsmap->{$prefix} = $value;
574 } else {
575 delete $nsmap->{$prefix};
576 }
577 } elsif ($_ eq 'xmlns') {
578 my $value = $token->{attributes}->{$_}->{value};
579 if ($value eq q<http://www.w3.org/XML/1998/namespace> or
580 $value eq q<http://www.w3.org/2000/xmlns/>) {
581 ## NOTE: Error should be detected at the DOM layer.
582 #
583 } elsif (length $value) {
584 $nsmap->{''} = $value;
585 } else {
586 delete $nsmap->{''};
587 }
588 }
589 }
590
591 my $ns;
592 my ($prefix, $ln) = split /:/, $token->{tag_name}, 2;
593
594 if (defined $ln and $prefix ne '' and $ln ne '') { # prefixed
595 if (defined $nsmap->{$prefix}) {
596 $ns = $nsmap->{$prefix};
597 } else {
598 ## NOTE: Error should be detected at the DOM layer.
599 ($prefix, $ln) = (undef, $token->{tag_name});
600 }
601 } else {
602 $ns = $nsmap->{''} if $prefix ne '' and not defined $ln;
603 ($prefix, $ln) = (undef, $token->{tag_name});
604 }
605
606 my $el = $self->{document}->create_element_ns ($ns, [$prefix, $ln]);
607 $el->set_user_data (manakai_source_line => $token->{line});
608 $el->set_user_data (manakai_source_column => $token->{column});
609
610 my $has_attr;
611 for my $attr_name (sort {$a cmp $b} keys %{$token->{attributes}}) {
612 my $ns;
613 my ($p, $l) = split /:/, $attr_name, 2;
614
615 if ($attr_name eq 'xmlns:xmlns') {
616 ($p, $l) = (undef, $attr_name);
617 } elsif (defined $l and $p ne '' and $l ne '') { # prefixed
618 if (defined $nsmap->{$p}) {
619 $ns = $nsmap->{$p};
620 } else {
621 ## NOTE: Error should be detected at the DOM-layer.
622 ($p, $l) = (undef, $attr_name);
623 }
624 } else {
625 if ($attr_name eq 'xmlns') {
626 $ns = $nsmap->{xmlns};
627 }
628 ($p, $l) = (undef, $attr_name);
629 }
630
631 if ($has_attr->{defined $ns ? $ns : ''}->{$l}) {
632 ## NOTE: Attributes are sorted as Unicode characters (not
633 ## code units) of their names, for stable output.
634
635 ## TODO: Should be sorted by source order?
636 !!!parse-error (type => 'duplicate ns attr',
637 token => $token,
638 value => $attr_name);
639 next;
640 } else {
641 $has_attr->{defined $ns ? $ns : ''}->{$l} = 1;
642 }
643
644 my $attr_t = $token->{attributes}->{$attr_name};
645 my $attr = $self->{document}->create_attribute_ns ($ns, [$p, $l]);
646 $attr->value ($attr_t->{value});
647 $attr->set_user_data (manakai_source_line => $attr_t->{line});
648 $attr->set_user_data (manakai_source_column => $attr_t->{column});
649 $el->set_attribute_node_ns ($attr);
650 }
651
652 $self->{open_elements}->[-1]->[0]->append_child ($el);
653
654 if ($self->{self_closing}) {
655 !!!ack ('ack');
656 } else {
657 push @{$self->{open_elements}}, [$el, $token->{tag_name}, $nsmap];
658 }
659
660 ## Stay in the mode.
661 !!!next-token;
662 next B;
663 } elsif ($token->{type} == END_TAG_TOKEN) {
664 if ($token->{tag_name} eq '') {
665 ## Short end tag token.
666 pop @{$self->{open_elements}};
667 } elsif ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {
668 pop @{$self->{open_elements}};
669 } else {
670 !!!parse-error (type => 'unmatched end tag',
671 text => $token->{tag_name},
672 token => $token);
673
674 ## Has an element in scope
675 INSCOPE: for my $i (reverse 0..$#{$self->{open_elements}}) {
676 if ($self->{open_elements}->[$i]->[1] eq $token->{tag_name}) {
677 splice @{$self->{open_elements}}, $i;
678 last INSCOPE;
679 }
680 } # INSCOPE
681 }
682
683 unless (@{$self->{open_elements}}) {
684 $self->{insertion_mode} = AFTER_ROOT_ELEMENT_IM;
685 !!!next-token;
686 return;
687 } else {
688 ## Stay in the state.
689 !!!next-token;
690 redo B;
691 }
692 } elsif ($token->{type} == COMMENT_TOKEN) {
693 my $comment = $self->{document}->create_comment ($token->{data});
694 $self->{open_elements}->[-1]->[0]->append_child ($comment);
695
696 ## Stay in the mode.
697 !!!next-token;
698 next B;
699 } elsif ($token->{type} == PI_TOKEN) {
700 my $pi = $self->{document}->create_processing_instruction
701 ($token->{target}, $token->{data});
702 $self->{open_elements}->[-1]->[0]->append_child ($pi);
703
704 ## Stay in the mode.
705 !!!next-token;
706 next B;
707 } elsif ($token->{type} == END_OF_FILE_TOKEN) {
708 !!!parse-error (type => 'in body:#eof',
709 token => $token);
710
711 $self->{insertion_mode} = AFTER_ROOT_ELEMENT_IM;
712 !!!next-token;
713 return;
714 } elsif ($token->{type} == DOCTYPE_TOKEN) {
715 !!!parse-error (type => 'in html:#doctype',
716 token => $token);
717 ## Ignore the token.
718
719 ## Stay in the mode.
720 !!!next-token;
721 next B;
722 } elsif ($token->{type} == ABORT_TOKEN) {
723 return;
724 } else {
725 die "$0: XML parser initial: Unknown token type $token->{type}";
726 }
727 } # B
728 } # _tree_in_element
729
730 sub _tree_after_root_element ($) {
731 my $self = shift;
732
733 B: while (1) {
734 if ($token->{type} == START_TAG_TOKEN) {
735 !!!parse-error (type => 'second root element',
736 token => $token);
737
738 ## XML5: Ignore the token.
739
740 $self->{insertion_mode} = BEFORE_ROOT_ELEMENT_IM;
741 ## Reprocess.
742 return;
743 } elsif ($token->{type} == COMMENT_TOKEN) {
744 my $comment = $self->{document}->create_comment ($token->{data});
745 $self->{document}->append_child ($comment);
746
747 ## Stay in the mode.
748 !!!next-token;
749 next B;
750 } elsif ($token->{type} == PI_TOKEN) {
751 my $pi = $self->{document}->create_processing_instruction
752 ($token->{target}, $token->{data});
753 $self->{document}->append_child ($pi);
754
755 ## Stay in the mode.
756 !!!next-token;
757 next B;
758 } elsif ($token->{type} == CHARACTER_TOKEN) {
759 if (not $self->{tainted} and
760 not $token->{has_reference} and
761 $token->{data} =~ s/^([\x09\x0A\x0C\x20]+)//) {
762 #
763 }
764
765 if (length $token->{data}) {
766 ## XML5: Ignore the token.
767
768 unless ($self->{tainted}) {
769 !!!parse-error (type => 'text outside of root element',
770 token => $token);
771 $self->{tainted} = 1;
772 }
773
774 $self->{document}->manakai_append_text ($token->{data});
775 }
776
777 ## Stay in the mode.
778 !!!next-token;
779 next B;
780 } elsif ($token->{type} == END_OF_FILE_TOKEN) {
781 ## Stop parsing.
782
783 ## TODO: implement "stop parsing".
784
785 $token = {type => ABORT_TOKEN};
786 return;
787 } elsif ($token->{type} == END_TAG_TOKEN) {
788 !!!parse-error (type => 'unmatched end tag',
789 text => $token->{tag_name},
790 token => $token);
791 ## Ignore the token.
792
793 ## Stay in the mode.
794 !!!next-token;
795 next B;
796 } elsif ($token->{type} == DOCTYPE_TOKEN) {
797 !!!parse-error (type => 'in html:#doctype',
798 token => $token);
799 ## Ignore the token.
800
801 ## Stay in the mode.
802 !!!next-token;
803 next B;
804 } elsif ($token->{type} == ABORT_TOKEN) {
805 return;
806 } else {
807 die "$0: XML parser initial: Unknown token type $token->{type}";
808 }
809 } # B
810 } # _tree_after_root_element
811
812 }
813
814 1;

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24