1 |
package Whatpm::HTML::Dumper; |
2 |
use strict; |
3 |
our $VERSION=do{my @r=(q$Revision: 1.3 $=~/\d+/g);sprintf "%d."."%02d" x $#r,@r}; |
4 |
|
5 |
require Exporter; |
6 |
push our @ISA, 'Exporter'; |
7 |
|
8 |
our @EXPORT = qw(dumptree); |
9 |
|
10 |
sub dumptree ($) { |
11 |
my $node = shift; |
12 |
my $r = ''; |
13 |
|
14 |
my $ns_id = { |
15 |
q<http://www.w3.org/1999/xhtml> => 'html', |
16 |
q<http://www.w3.org/2000/svg> => 'svg', |
17 |
q<http://www.w3.org/1998/Math/MathML> => 'math', |
18 |
q<http://www.w3.org/1999/xlink> => 'xlink', |
19 |
q<http://www.w3.org/XML/1998/namespace> => 'xml', |
20 |
q<http://www.w3.org/2000/xmlns/> => 'xmlns', |
21 |
}; |
22 |
|
23 |
my @node = map { [$_, ''] } @{$node->child_nodes}; |
24 |
while (@node) { |
25 |
my $child = shift @node; |
26 |
my $nt = $child->[0]->node_type; |
27 |
if ($nt == $child->[0]->ELEMENT_NODE) { |
28 |
my $ns = $child->[0]->namespace_uri; |
29 |
unless (defined $ns) { |
30 |
$ns = '{} '; |
31 |
} elsif ($ns eq q<http://www.w3.org/1999/xhtml>) { |
32 |
$ns = ''; |
33 |
} elsif ($ns_id->{$ns}) { |
34 |
$ns = $ns_id->{$ns} . ' '; |
35 |
} else { |
36 |
$ns = '{' . $ns . '} '; |
37 |
} |
38 |
$r .= $child->[1] . '<' . $ns . $child->[0]->manakai_local_name . ">\x0A"; |
39 |
|
40 |
for my $attr (sort {$a->[0] cmp $b->[0]} map { [do { |
41 |
my $ns = $_->namespace_uri; |
42 |
unless (defined $ns) { |
43 |
$ns = ''; |
44 |
} elsif ($ns_id->{$ns}) { |
45 |
$ns = $ns_id->{$ns} . ' '; |
46 |
} else { |
47 |
$ns = '{' . $ns . '} '; |
48 |
} |
49 |
$ns . $_->manakai_local_name; |
50 |
}, $_->value] } |
51 |
@{$child->[0]->attributes}) { |
52 |
$r .= $child->[1] . ' ' . $attr->[0] . '="'; ## ISSUE: case? |
53 |
$r .= $attr->[1] . '"' . "\x0A"; |
54 |
} |
55 |
|
56 |
unshift @node, |
57 |
map { [$_, $child->[1] . ' '] } @{$child->[0]->child_nodes}; |
58 |
} elsif ($nt == $child->[0]->TEXT_NODE) { |
59 |
$r .= $child->[1] . '"' . $child->[0]->data . '"' . "\x0A"; |
60 |
} elsif ($nt == $child->[0]->COMMENT_NODE) { |
61 |
$r .= $child->[1] . '<!-- ' . $child->[0]->data . " -->\x0A"; |
62 |
} elsif ($nt == $child->[0]->DOCUMENT_TYPE_NODE) { |
63 |
$r .= $child->[1] . '<!DOCTYPE ' . $child->[0]->name; |
64 |
my $pubid = $child->[0]->public_id; |
65 |
my $sysid = $child->[0]->system_id; |
66 |
if (length $pubid or length $sysid) { |
67 |
$r .= ' "' . $pubid . '"'; |
68 |
$r .= ' "' . $sysid . '"'; |
69 |
} |
70 |
$r .= ">\x0A"; |
71 |
unshift @node, |
72 |
map { [$_, $child->[1] . ' '] } |
73 |
sort { $a->node_name cmp $b->node_name } |
74 |
values %{$child->[0]->element_types}; |
75 |
unshift @node, |
76 |
map { [$_, $child->[1] . ' '] } |
77 |
sort { $a->node_name cmp $b->node_name } |
78 |
values %{$child->[0]->entities}; |
79 |
unshift @node, |
80 |
map { [$_, $child->[1] . ' '] } |
81 |
sort { $a->node_name cmp $b->node_name } |
82 |
values %{$child->[0]->notations}; |
83 |
unshift @node, |
84 |
map { [$_, $child->[1] . ' '] } @{$child->[0]->child_nodes}; |
85 |
} elsif ($nt == $child->[0]->PROCESSING_INSTRUCTION_NODE) { |
86 |
$r .= $child->[1] . '<?' . $child->[0]->target . ' '; |
87 |
$r .= $child->[0]->data . "?>\x0A"; |
88 |
} elsif ($nt == $child->[0]->ENTITY_NODE) { |
89 |
$r .= $child->[1] . '<!ENTITY ' . $child->[0]->node_name . ' "'; |
90 |
$r .= $child->[0]->public_id if defined $child->[0]->public_id; |
91 |
$r .= '" "'; |
92 |
$r .= $child->[0]->system_id if defined $child->[0]->system_id; |
93 |
$r .= '" '; |
94 |
$r .= $child->[0]->notation_name if defined $child->[0]->notation_name; |
95 |
$r .= ">\x0A"; |
96 |
unshift @node, |
97 |
map { [$_, $child->[1] . ' '] } @{$child->[0]->child_nodes}; |
98 |
} elsif ($nt == $child->[0]->NOTATION_NODE) { |
99 |
$r .= $child->[1] . '<!NOTATION ' . $child->[0]->node_name . ' "'; |
100 |
$r .= $child->[0]->public_id if defined $child->[0]->public_id; |
101 |
$r .= '" "'; |
102 |
$r .= $child->[0]->system_id if defined $child->[0]->system_id; |
103 |
$r .= qq[">\x0A]; |
104 |
} elsif ($nt == $child->[0]->ELEMENT_TYPE_DEFINITION_NODE) { |
105 |
$r .= $child->[1] . '<!ELEMENT ' . $child->[0]->node_name . ' '; |
106 |
$r .= $child->[0]->content_model_text; |
107 |
$r .= ">\x0A"; |
108 |
unshift @node, |
109 |
map { [$_, $child->[1] . ' '] } |
110 |
sort { $a->node_name cmp $b->node_name } |
111 |
values %{$child->[0]->attribute_definitions}; |
112 |
} elsif ($nt == $child->[0]->ATTRIBUTE_DEFINITION_NODE) { |
113 |
$r .= $child->[1] . $child->[0]->node_name . ' '; |
114 |
$r .= [ |
115 |
0, 'CDATA', 'ID', 'IDREF', 'IDREFS', 'ENTITY', 'ENTITIES', |
116 |
'NMTOKEN', 'NMTOKENS', 'NOTATION', 'ENUMERATION', 11, |
117 |
]->[$child->[0]->declared_type] || $child->[0]->declared_type; |
118 |
$r .= ' (' . join ('|', @{$child->[0]->allowed_tokens}) . ') '; |
119 |
$r .= [ |
120 |
0, 'FIXED', 'REQUIRED', 'IMPLIED', 'EXPLICIT', |
121 |
]->[$child->[0]->default_type] || $child->[0]->default_type; |
122 |
$r .= ' "' . $child->[0]->text_content . '"'; |
123 |
$r .= "\x0A"; |
124 |
} else { |
125 |
$r .= $child->[1] . $child->[0]->node_type . "\x0A"; # error |
126 |
} |
127 |
} |
128 |
|
129 |
return $r; |
130 |
} # dumptree |
131 |
|
132 |
## NOTE: Based on <http://wiki.whatwg.org/wiki/Parser_tests>. |
133 |
## TDOO: Document |
134 |
|
135 |
1; |
136 |
## $Date: 2008/10/17 07:14:29 $ |