| 1 |
wakaba |
1.1 |
=head1 NAME |
| 2 |
|
|
|
| 3 |
wakaba |
1.2 |
Whatpm::NanoDOM - A Non-Conforming Implementation of DOM Subset |
| 4 |
wakaba |
1.1 |
|
| 5 |
|
|
=head1 DESCRIPTION |
| 6 |
|
|
|
| 7 |
wakaba |
1.2 |
The C<Whatpm::NanoDOM> module contains a non-conforming implementation |
| 8 |
wakaba |
1.1 |
of a subset of DOM. It is the intention that this module is |
| 9 |
wakaba |
1.2 |
used only for the purpose of testing the C<Whatpm::HTML> module. |
| 10 |
wakaba |
1.1 |
|
| 11 |
|
|
See source code if you would like to know what it does. |
| 12 |
|
|
|
| 13 |
|
|
=cut |
| 14 |
|
|
|
| 15 |
wakaba |
1.2 |
package Whatpm::NanoDOM; |
| 16 |
wakaba |
1.1 |
use strict; |
| 17 |
wakaba |
1.28 |
our $VERSION=do{my @r=(q$Revision: 1.27 $=~/\d+/g);sprintf "%d."."%02d" x $#r,@r}; |
| 18 |
wakaba |
1.1 |
|
| 19 |
wakaba |
1.3 |
require Scalar::Util; |
| 20 |
|
|
|
| 21 |
|
|
package Whatpm::NanoDOM::DOMImplementation; |
| 22 |
|
|
|
| 23 |
|
|
sub create_document ($) { |
| 24 |
|
|
return Whatpm::NanoDOM::Document->new; |
| 25 |
|
|
} # create_document |
| 26 |
|
|
|
| 27 |
wakaba |
1.2 |
package Whatpm::NanoDOM::Node; |
| 28 |
wakaba |
1.1 |
|
| 29 |
|
|
sub new ($) { |
| 30 |
|
|
my $class = shift; |
| 31 |
|
|
my $self = bless {}, $class; |
| 32 |
|
|
return $self; |
| 33 |
|
|
} # new |
| 34 |
|
|
|
| 35 |
|
|
sub parent_node ($) { |
| 36 |
|
|
return shift->{parent_node}; |
| 37 |
|
|
} # parent_node |
| 38 |
|
|
|
| 39 |
wakaba |
1.4 |
sub manakai_parent_element ($) { |
| 40 |
|
|
my $self = shift; |
| 41 |
|
|
my $parent = $self->{parent_node}; |
| 42 |
|
|
while (defined $parent) { |
| 43 |
|
|
if ($parent->node_type == 1) { |
| 44 |
|
|
return $parent; |
| 45 |
|
|
} else { |
| 46 |
|
|
$parent = $parent->{parent_node}; |
| 47 |
|
|
} |
| 48 |
|
|
} |
| 49 |
|
|
return undef; |
| 50 |
|
|
} # manakai_parent_element |
| 51 |
|
|
|
| 52 |
wakaba |
1.1 |
sub child_nodes ($) { |
| 53 |
wakaba |
1.7 |
return shift->{child_nodes} || []; |
| 54 |
wakaba |
1.1 |
} # child_nodes |
| 55 |
|
|
|
| 56 |
wakaba |
1.23 |
sub node_name ($) { return $_[0]->{node_name} } |
| 57 |
|
|
|
| 58 |
wakaba |
1.1 |
## NOTE: Only applied to Elements and Documents |
| 59 |
|
|
sub append_child ($$) { |
| 60 |
|
|
my ($self, $new_child) = @_; |
| 61 |
|
|
if (defined $new_child->{parent_node}) { |
| 62 |
|
|
my $parent_list = $new_child->{parent_node}->{child_nodes}; |
| 63 |
|
|
for (0..$#$parent_list) { |
| 64 |
|
|
if ($parent_list->[$_] eq $new_child) { |
| 65 |
|
|
splice @$parent_list, $_, 1; |
| 66 |
|
|
} |
| 67 |
|
|
} |
| 68 |
|
|
} |
| 69 |
|
|
push @{$self->{child_nodes}}, $new_child; |
| 70 |
wakaba |
1.3 |
$new_child->{parent_node} = $self; |
| 71 |
|
|
Scalar::Util::weaken ($new_child->{parent_node}); |
| 72 |
wakaba |
1.1 |
return $new_child; |
| 73 |
|
|
} # append_child |
| 74 |
|
|
|
| 75 |
|
|
## NOTE: Only applied to Elements and Documents |
| 76 |
|
|
sub insert_before ($$;$) { |
| 77 |
|
|
my ($self, $new_child, $ref_child) = @_; |
| 78 |
|
|
if (defined $new_child->{parent_node}) { |
| 79 |
|
|
my $parent_list = $new_child->{parent_node}->{child_nodes}; |
| 80 |
|
|
for (0..$#$parent_list) { |
| 81 |
|
|
if ($parent_list->[$_] eq $new_child) { |
| 82 |
|
|
splice @$parent_list, $_, 1; |
| 83 |
|
|
} |
| 84 |
|
|
} |
| 85 |
|
|
} |
| 86 |
|
|
my $i = @{$self->{child_nodes}}; |
| 87 |
|
|
if (defined $ref_child) { |
| 88 |
|
|
for (0..$#{$self->{child_nodes}}) { |
| 89 |
|
|
if ($self->{child_nodes}->[$_] eq $ref_child) { |
| 90 |
|
|
$i = $_; |
| 91 |
|
|
last; |
| 92 |
|
|
} |
| 93 |
|
|
} |
| 94 |
|
|
} |
| 95 |
|
|
splice @{$self->{child_nodes}}, $i, 0, $new_child; |
| 96 |
wakaba |
1.5 |
$new_child->{parent_node} = $self; |
| 97 |
|
|
Scalar::Util::weaken ($new_child->{parent_node}); |
| 98 |
wakaba |
1.1 |
return $new_child; |
| 99 |
|
|
} # insert_before |
| 100 |
|
|
|
| 101 |
|
|
## NOTE: Only applied to Elements and Documents |
| 102 |
|
|
sub remove_child ($$) { |
| 103 |
|
|
my ($self, $old_child) = @_; |
| 104 |
|
|
my $parent_list = $self->{child_nodes}; |
| 105 |
|
|
for (0..$#$parent_list) { |
| 106 |
|
|
if ($parent_list->[$_] eq $old_child) { |
| 107 |
|
|
splice @$parent_list, $_, 1; |
| 108 |
|
|
} |
| 109 |
|
|
} |
| 110 |
|
|
delete $old_child->{parent_node}; |
| 111 |
|
|
return $old_child; |
| 112 |
|
|
} # remove_child |
| 113 |
|
|
|
| 114 |
|
|
## NOTE: Only applied to Elements and Documents |
| 115 |
|
|
sub has_child_nodes ($) { |
| 116 |
|
|
return @{shift->{child_nodes}} > 0; |
| 117 |
|
|
} # has_child_nodes |
| 118 |
|
|
|
| 119 |
|
|
## NOTE: Only applied to Elements and Documents |
| 120 |
wakaba |
1.8 |
sub first_child ($) { |
| 121 |
|
|
my $self = shift; |
| 122 |
|
|
return $self->{child_nodes}->[0]; |
| 123 |
|
|
} # first_child |
| 124 |
|
|
|
| 125 |
|
|
## NOTE: Only applied to Elements and Documents |
| 126 |
wakaba |
1.1 |
sub last_child ($) { |
| 127 |
|
|
my $self = shift; |
| 128 |
|
|
return @{$self->{child_nodes}} ? $self->{child_nodes}->[-1] : undef; |
| 129 |
|
|
} # last_child |
| 130 |
|
|
|
| 131 |
|
|
## NOTE: Only applied to Elements and Documents |
| 132 |
|
|
sub previous_sibling ($) { |
| 133 |
|
|
my $self = shift; |
| 134 |
|
|
my $parent = $self->{parent_node}; |
| 135 |
|
|
return undef unless defined $parent; |
| 136 |
|
|
my $r; |
| 137 |
|
|
for (@{$parent->{child_nodes}}) { |
| 138 |
|
|
if ($_ eq $self) { |
| 139 |
|
|
return $r; |
| 140 |
|
|
} else { |
| 141 |
|
|
$r = $_; |
| 142 |
|
|
} |
| 143 |
|
|
} |
| 144 |
|
|
return undef; |
| 145 |
|
|
} # previous_sibling |
| 146 |
|
|
|
| 147 |
wakaba |
1.6 |
sub prefix ($;$) { |
| 148 |
|
|
my $self = shift; |
| 149 |
|
|
if (@_) { |
| 150 |
|
|
$self->{prefix} = shift; |
| 151 |
|
|
} |
| 152 |
|
|
return $self->{prefix}; |
| 153 |
|
|
} # prefix |
| 154 |
|
|
|
| 155 |
wakaba |
1.24 |
sub text_content ($;$) { |
| 156 |
|
|
my $self = shift; |
| 157 |
|
|
if (@_) { |
| 158 |
|
|
@{$self->{child_nodes}} = (); ## NOTE: parent_node not unset. |
| 159 |
|
|
$self->append_child (Whatpm::NanoDOM::Text->new ($_[0])); |
| 160 |
|
|
return unless wantarray; |
| 161 |
|
|
} |
| 162 |
|
|
my $r = ''; |
| 163 |
|
|
for my $child (@{$self->child_nodes}) { |
| 164 |
|
|
if ($child->can ('data')) { |
| 165 |
|
|
$r .= $child->data; |
| 166 |
|
|
} else { |
| 167 |
|
|
$r .= $child->text_content; |
| 168 |
|
|
} |
| 169 |
|
|
} |
| 170 |
|
|
return $r; |
| 171 |
|
|
} # text_content |
| 172 |
|
|
|
| 173 |
wakaba |
1.18 |
sub get_user_data ($$) { |
| 174 |
|
|
return $_[0]->{$_[1]}; |
| 175 |
|
|
} # get_user_data |
| 176 |
|
|
|
| 177 |
|
|
sub set_user_data ($$;$$) { |
| 178 |
|
|
$_[0]->{$_[1]} = $_[2]; |
| 179 |
|
|
} # set_user_data |
| 180 |
|
|
|
| 181 |
wakaba |
1.1 |
sub ELEMENT_NODE () { 1 } |
| 182 |
|
|
sub ATTRIBUTE_NODE () { 2 } |
| 183 |
|
|
sub TEXT_NODE () { 3 } |
| 184 |
|
|
sub CDATA_SECTION_NODE () { 4 } |
| 185 |
|
|
sub ENTITY_REFERENCE_NODE () { 5 } |
| 186 |
|
|
sub ENTITY_NODE () { 6 } |
| 187 |
|
|
sub PROCESSING_INSTRUCTION_NODE () { 7 } |
| 188 |
|
|
sub COMMENT_NODE () { 8 } |
| 189 |
|
|
sub DOCUMENT_NODE () { 9 } |
| 190 |
|
|
sub DOCUMENT_TYPE_NODE () { 10 } |
| 191 |
|
|
sub DOCUMENT_FRAGMENT_NODE () { 11 } |
| 192 |
|
|
sub NOTATION_NODE () { 12 } |
| 193 |
wakaba |
1.23 |
sub ELEMENT_TYPE_DEFINITION_NODE () { 81001 } |
| 194 |
|
|
sub ATTRIBUTE_DEFINITION_NODE () { 81002 } |
| 195 |
wakaba |
1.1 |
|
| 196 |
wakaba |
1.2 |
package Whatpm::NanoDOM::Document; |
| 197 |
|
|
push our @ISA, 'Whatpm::NanoDOM::Node'; |
| 198 |
wakaba |
1.1 |
|
| 199 |
|
|
sub new ($) { |
| 200 |
|
|
my $self = shift->SUPER::new; |
| 201 |
|
|
$self->{child_nodes} = []; |
| 202 |
|
|
return $self; |
| 203 |
|
|
} # new |
| 204 |
|
|
|
| 205 |
|
|
## A manakai extension |
| 206 |
|
|
sub manakai_append_text ($$) { |
| 207 |
|
|
my $self = shift; |
| 208 |
|
|
if (@{$self->{child_nodes}} and |
| 209 |
|
|
$self->{child_nodes}->[-1]->node_type == 3) { |
| 210 |
|
|
$self->{child_nodes}->[-1]->manakai_append_text (shift); |
| 211 |
|
|
} else { |
| 212 |
|
|
my $text = $self->create_text_node (shift); |
| 213 |
|
|
$self->append_child ($text); |
| 214 |
|
|
} |
| 215 |
|
|
} # manakai_append_text |
| 216 |
|
|
|
| 217 |
|
|
sub node_type () { 9 } |
| 218 |
|
|
|
| 219 |
|
|
sub strict_error_checking { |
| 220 |
|
|
return 0; |
| 221 |
|
|
} # strict_error_checking |
| 222 |
|
|
|
| 223 |
|
|
sub create_text_node ($$) { |
| 224 |
|
|
shift; |
| 225 |
wakaba |
1.2 |
return Whatpm::NanoDOM::Text->new (shift); |
| 226 |
wakaba |
1.1 |
} # create_text_node |
| 227 |
|
|
|
| 228 |
|
|
sub create_comment ($$) { |
| 229 |
|
|
shift; |
| 230 |
wakaba |
1.2 |
return Whatpm::NanoDOM::Comment->new (shift); |
| 231 |
wakaba |
1.1 |
} # create_comment |
| 232 |
|
|
|
| 233 |
|
|
## The second parameter only supports manakai extended way |
| 234 |
|
|
## to specify qualified name - "[$prefix, $local_name]" |
| 235 |
wakaba |
1.20 |
sub create_attribute_ns ($$$) { |
| 236 |
|
|
my ($self, $nsuri, $qn) = @_; |
| 237 |
|
|
return Whatpm::NanoDOM::Attr->new (undef, $nsuri, $qn->[0], $qn->[1], ''); |
| 238 |
|
|
|
| 239 |
|
|
## NOTE: Created attribute node should be set to an element node |
| 240 |
|
|
## as far as possible. |onwer_document| of the attribute node, for |
| 241 |
|
|
## example, depends on the definedness of the |owner_element| attribute. |
| 242 |
|
|
} # create_attribute_ns |
| 243 |
|
|
|
| 244 |
|
|
## The second parameter only supports manakai extended way |
| 245 |
|
|
## to specify qualified name - "[$prefix, $local_name]" |
| 246 |
wakaba |
1.1 |
sub create_element_ns ($$$) { |
| 247 |
|
|
my ($self, $nsuri, $qn) = @_; |
| 248 |
wakaba |
1.3 |
return Whatpm::NanoDOM::Element->new ($self, $nsuri, $qn->[0], $qn->[1]); |
| 249 |
wakaba |
1.1 |
} # create_element_ns |
| 250 |
|
|
|
| 251 |
|
|
## A manakai extension |
| 252 |
|
|
sub create_document_type_definition ($$) { |
| 253 |
|
|
shift; |
| 254 |
wakaba |
1.2 |
return Whatpm::NanoDOM::DocumentType->new (shift); |
| 255 |
wakaba |
1.1 |
} # create_document_type_definition |
| 256 |
|
|
|
| 257 |
wakaba |
1.23 |
## A manakai extension. |
| 258 |
|
|
sub create_element_type_definition ($$) { |
| 259 |
|
|
shift; |
| 260 |
|
|
return Whatpm::NanoDOM::ElementTypeDefinition->new (shift); |
| 261 |
|
|
} # create_element_type_definition |
| 262 |
|
|
|
| 263 |
|
|
## A manakai extension. |
| 264 |
|
|
sub create_general_entity ($$) { |
| 265 |
|
|
shift; |
| 266 |
|
|
return Whatpm::NanoDOM::Entity->new (shift); |
| 267 |
|
|
} # create_general_entity |
| 268 |
|
|
|
| 269 |
|
|
## A manakai extension. |
| 270 |
|
|
sub create_notation ($$) { |
| 271 |
|
|
shift; |
| 272 |
|
|
return Whatpm::NanoDOM::Notation->new (shift); |
| 273 |
|
|
} # create_notation |
| 274 |
|
|
|
| 275 |
|
|
## A manakai extension. |
| 276 |
|
|
sub create_attribute_definition ($$) { |
| 277 |
|
|
shift; |
| 278 |
|
|
return Whatpm::NanoDOM::AttributeDefinition->new (shift); |
| 279 |
|
|
} # create_attribute_definition |
| 280 |
|
|
|
| 281 |
wakaba |
1.22 |
sub create_processing_instruction ($$$) { |
| 282 |
|
|
return Whatpm::NanoDOM::ProcessingInstruction->new (@_); |
| 283 |
|
|
} # creat_processing_instruction |
| 284 |
|
|
|
| 285 |
wakaba |
1.3 |
sub implementation ($) { |
| 286 |
|
|
return 'Whatpm::NanoDOM::DOMImplementation'; |
| 287 |
|
|
} # implementation |
| 288 |
|
|
|
| 289 |
wakaba |
1.4 |
sub document_element ($) { |
| 290 |
|
|
my $self = shift; |
| 291 |
|
|
for (@{$self->child_nodes}) { |
| 292 |
|
|
if ($_->node_type == 1) { |
| 293 |
|
|
return $_; |
| 294 |
|
|
} |
| 295 |
|
|
} |
| 296 |
|
|
return undef; |
| 297 |
|
|
} # document_element |
| 298 |
|
|
|
| 299 |
wakaba |
1.21 |
sub dom_config ($) { |
| 300 |
|
|
return {}; |
| 301 |
|
|
} # dom_config |
| 302 |
|
|
|
| 303 |
wakaba |
1.12 |
sub adopt_node ($$) { |
| 304 |
wakaba |
1.11 |
my @node = ($_[1]); |
| 305 |
|
|
while (@node) { |
| 306 |
|
|
my $node = shift @node; |
| 307 |
|
|
$node->{owner_document} = $_[0]; |
| 308 |
|
|
Scalar::Util::weaken ($node->{owner_document}); |
| 309 |
|
|
push @node, @{$node->child_nodes}; |
| 310 |
|
|
push @node, @{$node->attributes or []} if $node->can ('attributes'); |
| 311 |
|
|
} |
| 312 |
|
|
return $_[1]; |
| 313 |
|
|
} # adopt_node |
| 314 |
|
|
|
| 315 |
wakaba |
1.12 |
sub manakai_is_html ($;$) { |
| 316 |
|
|
if (@_ > 1) { |
| 317 |
|
|
if ($_[1]) { |
| 318 |
|
|
$_[0]->{manakai_is_html} = 1; |
| 319 |
|
|
} else { |
| 320 |
wakaba |
1.13 |
delete $_[0]->{manakai_is_html}; |
| 321 |
wakaba |
1.12 |
delete $_[0]->{manakai_compat_mode}; |
| 322 |
|
|
} |
| 323 |
|
|
} |
| 324 |
|
|
return $_[0]->{manakai_is_html}; |
| 325 |
|
|
} # manakai_is_html |
| 326 |
|
|
|
| 327 |
|
|
sub compat_mode ($) { |
| 328 |
|
|
if ($_[0]->{manakai_is_html}) { |
| 329 |
|
|
if ($_[0]->{manakai_compat_mode} eq 'quirks') { |
| 330 |
|
|
return 'BackCompat'; |
| 331 |
|
|
} |
| 332 |
|
|
} |
| 333 |
|
|
return 'CSS1Compat'; |
| 334 |
|
|
} # compat_mode |
| 335 |
|
|
|
| 336 |
|
|
sub manakai_compat_mode ($;$) { |
| 337 |
|
|
if ($_[0]->{manakai_is_html}) { |
| 338 |
|
|
if (@_ > 1 and defined $_[1] and |
| 339 |
|
|
{'no quirks' => 1, 'limited quirks' => 1, 'quirks' => 1}->{$_[1]}) { |
| 340 |
|
|
$_[0]->{manakai_compat_mode} = $_[1]; |
| 341 |
|
|
} |
| 342 |
|
|
return $_[0]->{manakai_compat_mode} || 'no quirks'; |
| 343 |
|
|
} else { |
| 344 |
|
|
return 'no quirks'; |
| 345 |
|
|
} |
| 346 |
|
|
} # manakai_compat_mode |
| 347 |
|
|
|
| 348 |
wakaba |
1.19 |
sub manakai_head ($) { |
| 349 |
|
|
my $html = $_[0]->manakai_html; |
| 350 |
|
|
return undef unless defined $html; |
| 351 |
|
|
for my $el (@{$html->child_nodes}) { |
| 352 |
|
|
next unless $el->node_type == 1; # ELEMENT_NODE |
| 353 |
|
|
my $nsuri = $el->namespace_uri; |
| 354 |
|
|
next unless defined $nsuri; |
| 355 |
|
|
next unless $nsuri eq q<http://www.w3.org/1999/xhtml>; |
| 356 |
|
|
next unless $el->manakai_local_name eq 'head'; |
| 357 |
|
|
return $el; |
| 358 |
|
|
} |
| 359 |
|
|
return undef; |
| 360 |
|
|
} # manakai_head |
| 361 |
|
|
|
| 362 |
|
|
sub manakai_html ($) { |
| 363 |
|
|
my $de = $_[0]->document_element; |
| 364 |
|
|
my $nsuri = $de->namespace_uri; |
| 365 |
|
|
if (defined $nsuri and $nsuri eq q<http://www.w3.org/1999/xhtml> and |
| 366 |
|
|
$de->manakai_local_name eq 'html') { |
| 367 |
|
|
return $de; |
| 368 |
|
|
} else { |
| 369 |
|
|
return undef; |
| 370 |
|
|
} |
| 371 |
|
|
} # manakai_html |
| 372 |
|
|
|
| 373 |
wakaba |
1.28 |
## NOTE: Manakai extension. |
| 374 |
|
|
sub all_declarations_processed ($;$) { |
| 375 |
|
|
$_[0]->{all_declarations_processed} = $_[1] if @_ > 1; |
| 376 |
|
|
return $_[0]->{all_declarations_processed}; |
| 377 |
|
|
} # all_declarations_processed |
| 378 |
|
|
|
| 379 |
wakaba |
1.17 |
sub input_encoding ($;$) { |
| 380 |
|
|
$_[0]->{input_encoding} = $_[1] if @_ > 1; |
| 381 |
|
|
return $_[0]->{input_encoding}; |
| 382 |
|
|
} |
| 383 |
|
|
|
| 384 |
|
|
sub manakai_charset ($;$) { |
| 385 |
|
|
$_[0]->{manakai_charset} = $_[1] if @_ > 1; |
| 386 |
|
|
return $_[0]->{manakai_charset}; |
| 387 |
|
|
} |
| 388 |
|
|
|
| 389 |
|
|
sub manakai_has_bom ($;$) { |
| 390 |
|
|
$_[0]->{manakai_has_bom} = $_[1] if @_ > 1; |
| 391 |
|
|
return $_[0]->{manakai_has_bom}; |
| 392 |
|
|
} |
| 393 |
|
|
|
| 394 |
wakaba |
1.22 |
sub xml_version ($;$) { |
| 395 |
|
|
$_[0]->{xml_version} = $_[1] if @_ > 1; |
| 396 |
|
|
return $_[0]->{xml_version}; |
| 397 |
|
|
} |
| 398 |
|
|
|
| 399 |
|
|
sub xml_encoding ($;$) { |
| 400 |
|
|
$_[0]->{xml_encoding} = $_[1] if @_ > 1; |
| 401 |
|
|
return $_[0]->{xml_encoding}; |
| 402 |
|
|
} |
| 403 |
|
|
|
| 404 |
|
|
sub xml_standalone ($;$) { |
| 405 |
|
|
$_[0]->{xml_standalone} = $_[1] if @_ > 1; |
| 406 |
|
|
return $_[0]->{xml_standalone}; |
| 407 |
|
|
} |
| 408 |
|
|
|
| 409 |
wakaba |
1.2 |
package Whatpm::NanoDOM::Element; |
| 410 |
|
|
push our @ISA, 'Whatpm::NanoDOM::Node'; |
| 411 |
wakaba |
1.1 |
|
| 412 |
wakaba |
1.3 |
sub new ($$$$$) { |
| 413 |
wakaba |
1.1 |
my $self = shift->SUPER::new; |
| 414 |
wakaba |
1.3 |
$self->{owner_document} = shift; |
| 415 |
|
|
Scalar::Util::weaken ($self->{owner_document}); |
| 416 |
wakaba |
1.1 |
$self->{namespace_uri} = shift; |
| 417 |
|
|
$self->{prefix} = shift; |
| 418 |
|
|
$self->{local_name} = shift; |
| 419 |
|
|
$self->{attributes} = {}; |
| 420 |
|
|
$self->{child_nodes} = []; |
| 421 |
|
|
return $self; |
| 422 |
|
|
} # new |
| 423 |
|
|
|
| 424 |
wakaba |
1.3 |
sub owner_document ($) { |
| 425 |
|
|
return shift->{owner_document}; |
| 426 |
|
|
} # owner_document |
| 427 |
|
|
|
| 428 |
wakaba |
1.1 |
sub clone_node ($$) { |
| 429 |
|
|
my ($self, $deep) = @_; ## NOTE: Deep cloning is not supported |
| 430 |
|
|
my $clone = bless { |
| 431 |
|
|
namespace_uri => $self->{namespace_uri}, |
| 432 |
|
|
prefix => $self->{prefix}, |
| 433 |
|
|
local_name => $self->{local_name}, |
| 434 |
|
|
child_nodes => [], |
| 435 |
|
|
}, ref $self; |
| 436 |
|
|
for my $ns (keys %{$self->{attributes}}) { |
| 437 |
|
|
for my $ln (keys %{$self->{attributes}->{$ns}}) { |
| 438 |
|
|
my $attr = $self->{attributes}->{$ns}->{$ln}; |
| 439 |
|
|
$clone->{attributes}->{$ns}->{$ln} = bless { |
| 440 |
|
|
namespace_uri => $attr->{namespace_uri}, |
| 441 |
|
|
prefix => $attr->{prefix}, |
| 442 |
|
|
local_name => $attr->{local_name}, |
| 443 |
|
|
value => $attr->{value}, |
| 444 |
|
|
}, ref $self->{attributes}->{$ns}->{$ln}; |
| 445 |
|
|
} |
| 446 |
|
|
} |
| 447 |
|
|
return $clone; |
| 448 |
|
|
} # clone |
| 449 |
|
|
|
| 450 |
|
|
## A manakai extension |
| 451 |
|
|
sub manakai_append_text ($$) { |
| 452 |
|
|
my $self = shift; |
| 453 |
|
|
if (@{$self->{child_nodes}} and |
| 454 |
|
|
$self->{child_nodes}->[-1]->node_type == 3) { |
| 455 |
|
|
$self->{child_nodes}->[-1]->manakai_append_text (shift); |
| 456 |
|
|
} else { |
| 457 |
wakaba |
1.2 |
my $text = Whatpm::NanoDOM::Text->new (shift); |
| 458 |
wakaba |
1.1 |
$self->append_child ($text); |
| 459 |
|
|
} |
| 460 |
|
|
} # manakai_append_text |
| 461 |
|
|
|
| 462 |
|
|
sub attributes ($) { |
| 463 |
|
|
my $self = shift; |
| 464 |
|
|
my $r = []; |
| 465 |
|
|
## Order MUST be stable |
| 466 |
|
|
for my $ns (sort {$a cmp $b} keys %{$self->{attributes}}) { |
| 467 |
|
|
for my $ln (sort {$a cmp $b} keys %{$self->{attributes}->{$ns}}) { |
| 468 |
|
|
push @$r, $self->{attributes}->{$ns}->{$ln} |
| 469 |
|
|
if defined $self->{attributes}->{$ns}->{$ln}; |
| 470 |
|
|
} |
| 471 |
|
|
} |
| 472 |
|
|
return $r; |
| 473 |
|
|
} # attributes |
| 474 |
|
|
|
| 475 |
wakaba |
1.3 |
sub local_name ($) { # TODO: HTML5 case |
| 476 |
|
|
return shift->{local_name}; |
| 477 |
|
|
} # local_name |
| 478 |
|
|
|
| 479 |
wakaba |
1.4 |
sub manakai_local_name ($) { |
| 480 |
|
|
return shift->{local_name}; # no case fixing for HTML5 |
| 481 |
|
|
} # manakai_local_name |
| 482 |
|
|
|
| 483 |
wakaba |
1.3 |
sub namespace_uri ($) { |
| 484 |
|
|
return shift->{namespace_uri}; |
| 485 |
|
|
} # namespace_uri |
| 486 |
|
|
|
| 487 |
wakaba |
1.4 |
sub manakai_element_type_match ($$$) { |
| 488 |
|
|
my ($self, $nsuri, $ln) = @_; |
| 489 |
|
|
if (defined $nsuri) { |
| 490 |
|
|
if (defined $self->{namespace_uri} and $nsuri eq $self->{namespace_uri}) { |
| 491 |
|
|
return ($ln eq $self->{local_name}); |
| 492 |
|
|
} else { |
| 493 |
|
|
return 0; |
| 494 |
|
|
} |
| 495 |
|
|
} else { |
| 496 |
|
|
if (not defined $self->{namespace_uri}) { |
| 497 |
|
|
return ($ln eq $self->{local_name}); |
| 498 |
|
|
} else { |
| 499 |
|
|
return 0; |
| 500 |
|
|
} |
| 501 |
|
|
} |
| 502 |
|
|
} # manakai_element_type_match |
| 503 |
|
|
|
| 504 |
wakaba |
1.1 |
sub node_type { 1 } |
| 505 |
|
|
|
| 506 |
|
|
## TODO: HTML5 capitalization |
| 507 |
|
|
sub tag_name ($) { |
| 508 |
|
|
my $self = shift; |
| 509 |
|
|
if (defined $self->{prefix}) { |
| 510 |
|
|
return $self->{prefix} . ':' . $self->{local_name}; |
| 511 |
|
|
} else { |
| 512 |
|
|
return $self->{local_name}; |
| 513 |
|
|
} |
| 514 |
|
|
} # tag_name |
| 515 |
|
|
|
| 516 |
wakaba |
1.8 |
sub get_attribute_ns ($$$) { |
| 517 |
|
|
my ($self, $nsuri, $ln) = @_; |
| 518 |
|
|
$nsuri = '' unless defined $nsuri; |
| 519 |
|
|
return defined $self->{attributes}->{$nsuri}->{$ln} |
| 520 |
|
|
? $self->{attributes}->{$nsuri}->{$ln}->value : undef; |
| 521 |
|
|
} # get_attribute_ns |
| 522 |
|
|
|
| 523 |
wakaba |
1.9 |
sub get_attribute_node_ns ($$$) { |
| 524 |
|
|
my ($self, $nsuri, $ln) = @_; |
| 525 |
|
|
$nsuri = '' unless defined $nsuri; |
| 526 |
|
|
return $self->{attributes}->{$nsuri}->{$ln}; |
| 527 |
|
|
} # get_attribute_node_ns |
| 528 |
|
|
|
| 529 |
wakaba |
1.1 |
sub has_attribute_ns ($$$) { |
| 530 |
|
|
my ($self, $nsuri, $ln) = @_; |
| 531 |
wakaba |
1.8 |
$nsuri = '' unless defined $nsuri; |
| 532 |
wakaba |
1.1 |
return defined $self->{attributes}->{$nsuri}->{$ln}; |
| 533 |
|
|
} # has_attribute_ns |
| 534 |
|
|
|
| 535 |
|
|
## The second parameter only supports manakai extended way |
| 536 |
|
|
## to specify qualified name - "[$prefix, $local_name]" |
| 537 |
|
|
sub set_attribute_ns ($$$$) { |
| 538 |
|
|
my ($self, $nsuri, $qn, $value) = @_; |
| 539 |
|
|
$self->{attributes}->{$nsuri}->{$qn->[1]} |
| 540 |
wakaba |
1.5 |
= Whatpm::NanoDOM::Attr->new ($self, $nsuri, $qn->[0], $qn->[1], $value); |
| 541 |
wakaba |
1.1 |
} # set_attribute_ns |
| 542 |
|
|
|
| 543 |
wakaba |
1.20 |
sub set_attribute_node_ns ($$) { |
| 544 |
|
|
my $self = shift; |
| 545 |
|
|
my $attr = shift; |
| 546 |
|
|
$self->{attributes}->{$attr->namespace_uri}->{$attr->manakai_local_name} |
| 547 |
|
|
= $attr; |
| 548 |
|
|
$attr->{owner_element} = $self; |
| 549 |
|
|
Scalar::Util::weaken ($attr->{owner_element}); |
| 550 |
|
|
} # set_attribute_node_ns |
| 551 |
|
|
|
| 552 |
wakaba |
1.2 |
package Whatpm::NanoDOM::Attr; |
| 553 |
|
|
push our @ISA, 'Whatpm::NanoDOM::Node'; |
| 554 |
wakaba |
1.1 |
|
| 555 |
wakaba |
1.5 |
sub new ($$$$$$) { |
| 556 |
wakaba |
1.1 |
my $self = shift->SUPER::new; |
| 557 |
wakaba |
1.5 |
$self->{owner_element} = shift; |
| 558 |
|
|
Scalar::Util::weaken ($self->{owner_element}); |
| 559 |
wakaba |
1.1 |
$self->{namespace_uri} = shift; |
| 560 |
|
|
$self->{prefix} = shift; |
| 561 |
|
|
$self->{local_name} = shift; |
| 562 |
|
|
$self->{value} = shift; |
| 563 |
wakaba |
1.28 |
$self->{specified} = 1; |
| 564 |
wakaba |
1.1 |
return $self; |
| 565 |
|
|
} # new |
| 566 |
|
|
|
| 567 |
wakaba |
1.5 |
sub namespace_uri ($) { |
| 568 |
|
|
return shift->{namespace_uri}; |
| 569 |
|
|
} # namespace_uri |
| 570 |
|
|
|
| 571 |
|
|
sub manakai_local_name ($) { |
| 572 |
|
|
return shift->{local_name}; |
| 573 |
|
|
} # manakai_local_name |
| 574 |
|
|
|
| 575 |
wakaba |
1.1 |
sub node_type { 2 } |
| 576 |
|
|
|
| 577 |
wakaba |
1.14 |
sub owner_document ($) { |
| 578 |
|
|
return shift->owner_element->owner_document; |
| 579 |
|
|
} # owner_document |
| 580 |
|
|
|
| 581 |
wakaba |
1.1 |
## TODO: HTML5 case stuff? |
| 582 |
|
|
sub name ($) { |
| 583 |
|
|
my $self = shift; |
| 584 |
|
|
if (defined $self->{prefix}) { |
| 585 |
|
|
return $self->{prefix} . ':' . $self->{local_name}; |
| 586 |
|
|
} else { |
| 587 |
|
|
return $self->{local_name}; |
| 588 |
|
|
} |
| 589 |
|
|
} # name |
| 590 |
|
|
|
| 591 |
wakaba |
1.20 |
sub value ($;$) { |
| 592 |
|
|
if (@_ > 1) { |
| 593 |
|
|
$_[0]->{value} = $_[1]; |
| 594 |
|
|
} |
| 595 |
wakaba |
1.1 |
return shift->{value}; |
| 596 |
|
|
} # value |
| 597 |
|
|
|
| 598 |
wakaba |
1.5 |
sub owner_element ($) { |
| 599 |
|
|
return shift->{owner_element}; |
| 600 |
|
|
} # owner_element |
| 601 |
|
|
|
| 602 |
wakaba |
1.28 |
sub specified ($;$) { |
| 603 |
|
|
$_[0]->{specified} = $_[1] if @_ > 1; |
| 604 |
|
|
return $_[0]->{specified} || 0; |
| 605 |
|
|
} |
| 606 |
|
|
|
| 607 |
|
|
sub manakai_attribute_type ($;$) { |
| 608 |
|
|
$_[0]->{manakai_attribute_type} = $_[1] if @_ > 1; |
| 609 |
|
|
return $_[0]->{manakai_attribute_type} || 0; |
| 610 |
|
|
} |
| 611 |
|
|
|
| 612 |
wakaba |
1.2 |
package Whatpm::NanoDOM::CharacterData; |
| 613 |
|
|
push our @ISA, 'Whatpm::NanoDOM::Node'; |
| 614 |
wakaba |
1.1 |
|
| 615 |
|
|
sub new ($$) { |
| 616 |
|
|
my $self = shift->SUPER::new; |
| 617 |
|
|
$self->{data} = shift; |
| 618 |
|
|
return $self; |
| 619 |
|
|
} # new |
| 620 |
|
|
|
| 621 |
|
|
## A manakai extension |
| 622 |
|
|
sub manakai_append_text ($$) { |
| 623 |
|
|
my ($self, $s) = @_; |
| 624 |
|
|
$self->{data} .= $s; |
| 625 |
|
|
} # manakai_append_text |
| 626 |
|
|
|
| 627 |
|
|
sub data ($) { |
| 628 |
|
|
return shift->{data}; |
| 629 |
|
|
} # data |
| 630 |
|
|
|
| 631 |
wakaba |
1.2 |
package Whatpm::NanoDOM::Text; |
| 632 |
|
|
push our @ISA, 'Whatpm::NanoDOM::CharacterData'; |
| 633 |
wakaba |
1.1 |
|
| 634 |
|
|
sub node_type () { 3 } |
| 635 |
|
|
|
| 636 |
wakaba |
1.2 |
package Whatpm::NanoDOM::Comment; |
| 637 |
|
|
push our @ISA, 'Whatpm::NanoDOM::CharacterData'; |
| 638 |
wakaba |
1.1 |
|
| 639 |
|
|
sub node_type () { 8 } |
| 640 |
|
|
|
| 641 |
wakaba |
1.2 |
package Whatpm::NanoDOM::DocumentType; |
| 642 |
|
|
push our @ISA, 'Whatpm::NanoDOM::Node'; |
| 643 |
wakaba |
1.1 |
|
| 644 |
|
|
sub new ($$) { |
| 645 |
|
|
my $self = shift->SUPER::new; |
| 646 |
|
|
$self->{name} = shift; |
| 647 |
wakaba |
1.23 |
$self->{element_types} = {}; |
| 648 |
|
|
$self->{entities} = {}; |
| 649 |
|
|
$self->{notations} = {}; |
| 650 |
|
|
$self->{child_nodes} = []; |
| 651 |
wakaba |
1.1 |
return $self; |
| 652 |
|
|
} # new |
| 653 |
|
|
|
| 654 |
|
|
sub node_type () { 10 } |
| 655 |
|
|
|
| 656 |
|
|
sub name ($) { |
| 657 |
|
|
return shift->{name}; |
| 658 |
|
|
} # name |
| 659 |
|
|
|
| 660 |
wakaba |
1.16 |
sub public_id ($;$) { |
| 661 |
|
|
$_[0]->{public_id} = $_[1] if @_ > 1; |
| 662 |
|
|
return $_[0]->{public_id}; |
| 663 |
|
|
} # public_id |
| 664 |
|
|
|
| 665 |
|
|
sub system_id ($;$) { |
| 666 |
|
|
$_[0]->{system_id} = $_[1] if @_ > 1; |
| 667 |
|
|
return $_[0]->{system_id}; |
| 668 |
|
|
} # system_id |
| 669 |
|
|
|
| 670 |
wakaba |
1.23 |
sub element_types ($) { |
| 671 |
|
|
return $_[0]->{element_types}; |
| 672 |
|
|
} # element_types |
| 673 |
|
|
|
| 674 |
|
|
sub entities ($) { |
| 675 |
|
|
return $_[0]->{entities}; |
| 676 |
|
|
} # entities |
| 677 |
|
|
|
| 678 |
|
|
sub notations ($) { |
| 679 |
|
|
return $_[0]->{notations}; |
| 680 |
|
|
} # notations |
| 681 |
|
|
|
| 682 |
|
|
sub get_element_type_definition_node ($$) { |
| 683 |
|
|
return $_[0]->{element_types}->{$_[1]}; |
| 684 |
|
|
} # get_element_type_definition_node |
| 685 |
|
|
|
| 686 |
|
|
sub set_element_type_definition_node ($$) { |
| 687 |
|
|
$_[0]->{element_types}->{$_[1]->node_name} = $_[1]; |
| 688 |
|
|
} # set_element_type_definition_node |
| 689 |
|
|
|
| 690 |
|
|
sub get_general_entity_node ($$) { |
| 691 |
|
|
return $_[0]->{entities}->{$_[1]}; |
| 692 |
|
|
} # get_general_entity_node |
| 693 |
|
|
|
| 694 |
|
|
sub set_general_entity_node ($$) { |
| 695 |
|
|
$_[0]->{entities}->{$_[1]->node_name} = $_[1]; |
| 696 |
|
|
} # set_general_entity_node |
| 697 |
|
|
|
| 698 |
|
|
sub get_notation_node ($$) { |
| 699 |
|
|
return $_[0]->{notations}->{$_[1]}; |
| 700 |
|
|
} # get_notation_node |
| 701 |
|
|
|
| 702 |
|
|
sub set_notation_node ($$) { |
| 703 |
|
|
$_[0]->{notations}->{$_[1]->node_name} = $_[1]; |
| 704 |
|
|
} # set_notation_node |
| 705 |
|
|
|
| 706 |
wakaba |
1.22 |
package Whatpm::NanoDOM::ProcessingInstruction; |
| 707 |
|
|
push our @ISA, 'Whatpm::NanoDOM::Node'; |
| 708 |
|
|
|
| 709 |
|
|
sub new ($$$$) { |
| 710 |
|
|
my $self = shift->SUPER::new; |
| 711 |
|
|
shift; |
| 712 |
|
|
# $self->{owner_document} = shift; |
| 713 |
|
|
# Scalar::Util::weaken ($self->{owner_document}); |
| 714 |
|
|
$self->{target} = shift; |
| 715 |
|
|
$self->{data} = shift; |
| 716 |
|
|
return $self; |
| 717 |
|
|
} # new |
| 718 |
|
|
|
| 719 |
|
|
sub node_type () { 7 } |
| 720 |
|
|
|
| 721 |
|
|
sub target ($) { |
| 722 |
|
|
return $_[0]->{target}; |
| 723 |
|
|
} # target |
| 724 |
|
|
|
| 725 |
|
|
sub data ($;$) { |
| 726 |
|
|
$_[0]->{data} = $_[1] if @_ > 1; |
| 727 |
|
|
return $_[0]->{data}; |
| 728 |
|
|
} # data |
| 729 |
|
|
|
| 730 |
wakaba |
1.23 |
package Whatpm::NanoDOM::Entity; |
| 731 |
|
|
push our @ISA, 'Whatpm::NanoDOM::Node'; |
| 732 |
|
|
|
| 733 |
|
|
sub new ($$) { |
| 734 |
|
|
my $self = shift->SUPER::new; |
| 735 |
|
|
$self->{node_name} = shift; |
| 736 |
wakaba |
1.27 |
$self->{child_nodes} = []; |
| 737 |
wakaba |
1.23 |
return $self; |
| 738 |
|
|
} # new |
| 739 |
|
|
|
| 740 |
|
|
sub node_type () { 6 } |
| 741 |
|
|
|
| 742 |
wakaba |
1.25 |
sub public_id ($;$) { |
| 743 |
|
|
$_[0]->{public_id} = $_[1] if @_ > 1; |
| 744 |
|
|
return $_[0]->{public_id}; |
| 745 |
|
|
} # public_id |
| 746 |
|
|
|
| 747 |
|
|
sub system_id ($;$) { |
| 748 |
|
|
$_[0]->{system_id} = $_[1] if @_ > 1; |
| 749 |
|
|
return $_[0]->{system_id}; |
| 750 |
|
|
} # system_id |
| 751 |
|
|
|
| 752 |
wakaba |
1.26 |
sub notation_name ($;$) { |
| 753 |
|
|
$_[0]->{notation_name} = $_[1] if @_ > 1; |
| 754 |
|
|
return $_[0]->{notation_name}; |
| 755 |
|
|
} # notation_name |
| 756 |
|
|
|
| 757 |
wakaba |
1.23 |
package Whatpm::NanoDOM::Notation; |
| 758 |
|
|
push our @ISA, 'Whatpm::NanoDOM::Node'; |
| 759 |
|
|
|
| 760 |
|
|
sub new ($$) { |
| 761 |
|
|
my $self = shift->SUPER::new; |
| 762 |
|
|
$self->{node_name} = shift; |
| 763 |
|
|
return $self; |
| 764 |
|
|
} # new |
| 765 |
|
|
|
| 766 |
|
|
sub node_type () { 12 } |
| 767 |
|
|
|
| 768 |
wakaba |
1.25 |
sub public_id ($;$) { |
| 769 |
|
|
$_[0]->{public_id} = $_[1] if @_ > 1; |
| 770 |
|
|
return $_[0]->{public_id}; |
| 771 |
|
|
} # public_id |
| 772 |
|
|
|
| 773 |
|
|
sub system_id ($;$) { |
| 774 |
|
|
$_[0]->{system_id} = $_[1] if @_ > 1; |
| 775 |
|
|
return $_[0]->{system_id}; |
| 776 |
|
|
} # system_id |
| 777 |
|
|
|
| 778 |
wakaba |
1.23 |
package Whatpm::NanoDOM::ElementTypeDefinition; |
| 779 |
|
|
push our @ISA, 'Whatpm::NanoDOM::Node'; |
| 780 |
|
|
|
| 781 |
|
|
sub new ($$) { |
| 782 |
|
|
my $self = shift->SUPER::new; |
| 783 |
|
|
$self->{node_name} = shift; |
| 784 |
|
|
$self->{content_model} = ''; |
| 785 |
|
|
$self->{attribute_definitions} = {}; |
| 786 |
|
|
return $self; |
| 787 |
|
|
} # new |
| 788 |
|
|
|
| 789 |
|
|
sub node_type () { 81001 } |
| 790 |
|
|
|
| 791 |
|
|
sub content_model_text ($;$) { |
| 792 |
|
|
$_[0]->{content_model} = $_[1] if @_ > 1; |
| 793 |
|
|
return $_[0]->{content_model}; |
| 794 |
|
|
} # content_model_text |
| 795 |
|
|
|
| 796 |
|
|
sub attribute_definitions ($) { return $_[0]->{attribute_definitions} } |
| 797 |
|
|
|
| 798 |
|
|
sub get_attribute_definition_node ($$) { |
| 799 |
|
|
return $_[0]->{attribute_definitions}->{$_[1]}; |
| 800 |
|
|
} # get_attribute_definition_node |
| 801 |
|
|
|
| 802 |
|
|
sub set_attribute_definition_node ($$) { |
| 803 |
|
|
$_[0]->{attribute_definitions}->{$_[1]->node_name} = $_[1]; |
| 804 |
|
|
} # set_attribute_definition_node |
| 805 |
|
|
|
| 806 |
|
|
package Whatpm::NanoDOM::AttributeDefinition; |
| 807 |
|
|
push our @ISA, 'Whatpm::NanoDOM::Node'; |
| 808 |
|
|
|
| 809 |
|
|
sub new ($$) { |
| 810 |
|
|
my $self = shift->SUPER::new; |
| 811 |
|
|
$self->{node_name} = shift; |
| 812 |
wakaba |
1.24 |
$self->{allowed_tokens} = []; |
| 813 |
wakaba |
1.23 |
return $self; |
| 814 |
|
|
} # new |
| 815 |
|
|
|
| 816 |
|
|
sub node_type () { 81002 } |
| 817 |
|
|
|
| 818 |
wakaba |
1.24 |
sub allowed_tokens ($) { return $_[0]->{allowed_tokens} } |
| 819 |
|
|
|
| 820 |
|
|
sub default_type ($;$) { |
| 821 |
|
|
$_[0]->{default_type} = $_[1] if @_ > 1; |
| 822 |
|
|
return $_[0]->{default_type} || 0; |
| 823 |
|
|
} # default_type |
| 824 |
|
|
|
| 825 |
|
|
sub declared_type ($;$) { |
| 826 |
|
|
$_[0]->{declared_type} = $_[1] if @_ > 1; |
| 827 |
|
|
return $_[0]->{declared_type} || 0; |
| 828 |
|
|
} # declared_type |
| 829 |
|
|
|
| 830 |
wakaba |
1.1 |
=head1 SEE ALSO |
| 831 |
|
|
|
| 832 |
wakaba |
1.22 |
L<Whatpm::HTML|Whatpm::HTML> |
| 833 |
|
|
|
| 834 |
|
|
L<Whatpm::XML::Parser|Whatpm::XML::Parser> |
| 835 |
|
|
|
| 836 |
|
|
L<Whatpm::ContentChecker|Whatpm::ContentChecker> |
| 837 |
wakaba |
1.1 |
|
| 838 |
|
|
=head1 AUTHOR |
| 839 |
|
|
|
| 840 |
|
|
Wakaba <w@suika.fam.cx>. |
| 841 |
|
|
|
| 842 |
|
|
=head1 LICENSE |
| 843 |
|
|
|
| 844 |
wakaba |
1.22 |
Copyright 2007-2008 Wakaba <w@suika.fam.cx> |
| 845 |
wakaba |
1.1 |
|
| 846 |
|
|
This library is free software; you can redistribute it |
| 847 |
|
|
and/or modify it under the same terms as Perl itself. |
| 848 |
|
|
|
| 849 |
|
|
=cut |
| 850 |
|
|
|
| 851 |
|
|
1; |
| 852 |
wakaba |
1.28 |
# $Date: 2008/10/19 07:19:00 $ |