/usr/share/perl5/XML/Element.pm is in libxml-treebuilder-perl 5.4-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 | require 5;
package XML::Element;
use warnings;
use strict;
use HTML::Tagset ();
use HTML::Element 4.1 ();
use Carp;
use vars qw(@ISA $VERSION);
$VERSION = '5.4';
@ISA = ('HTML::Element');
# Init:
my %emptyElement = ();
foreach my $e (%HTML::Tagset::emptyElement) {
$emptyElement{$e} = 1
if substr( $e, 0, 1 ) eq '~' and $HTML::Tagset::emptyElement{$e};
}
my $in_cdata = 0;
my $nillio = [];
#--------------------------------------------------------------------------
#Some basic overrides:
sub _empty_element_map { \%emptyElement }
*_fold_case = \&HTML::Element::_fold_case_NOT;
*starttag = \&starttag_XML;
*endtag = \&endtag_XML;
*encoded_content = \$HTML::Element::encoded_content;
*_xml_escape = \&HTML::Element::_xml_escape;
# TODO: override id with something that looks for xml:id too/instead?
#--------------------------------------------------------------------------
#TODO: test and document this:
# with no tagname set, assumes ALL all-whitespace nodes are ignorable!
sub delete_ignorable_whitespace {
my $under_hash = $_[1];
my (@to_do) = ( $_[0] );
if ( $under_hash and ref($under_hash) eq 'ARRAY' ) {
$under_hash = { map { ; $_ => 1 } @$under_hash };
}
my $all = !$under_hash;
my ( $i, $this, $children );
while (@to_do) {
$this = shift @to_do;
$children = $this->content || next;
if ( ( $all or $under_hash->{ $this->tag } )
and @$children )
{
for ( $i = $#$children; $i >= 0; --$i ) {
# work backwards thru the list
next if ref $children->[$i];
if ( $children->[$i] =~ m<^\s*$>s ) { # all WS
splice @$children, $i, 1; # delete it.
}
}
}
unshift @to_do, grep ref($_), @$children; # recurse
}
return;
}
## copied from HTML::Element to support CDATDA
sub starttag_XML {
my ($self) = @_;
# and a third parameter to signal emptiness?
my $name = $self->{'_tag'};
return $self->{'text'} if $name eq '~literal';
return '<!' . $self->{'text'} . '>' if $name eq '~declaration';
return "<?" . $self->{'text'} . "?>" if $name eq '~pi';
if ( $name eq '~comment' ) {
if ( ref( $self->{'text'} || '' ) eq 'ARRAY' ) {
# Does this ever get used? And is this right?
$name = join( ' ', @{ $self->{'text'} } );
}
else {
$name = $self->{'text'};
}
$name =~ s/--/--/g; # can't have double --'s in XML comments
return "<!-- $name -->";
}
if ( $name eq '~cdata' ) {
$in_cdata = 1;
return "<![CDATA[";
}
my $tag = "<$name";
my $val;
for ( sort keys %$self ) { # predictable ordering
next if !length $_ or m/^_/s or $_ eq '/';
# Hm -- what to do if val is undef?
# I suppose that shouldn't ever happen.
next if !defined( $val = $self->{$_} ); # or ref $val;
_xml_escape($val);
$tag .= qq{ $_="$val"};
}
@_ == 3 ? "$tag />" : "$tag>";
}
## copied from HTML::Element to support CDATDA
sub endtag_XML {
my ($self) = @_;
# and a third parameter to signal emptiness?
my $name = $self->{'_tag'};
if ( $name eq '~cdata' ) {
$in_cdata = 0;
return "]]>";
}
"</$_[0]->{'_tag'}>";
}
## copied from HTML::Element to support CDATDA
sub as_XML {
my ($self) = @_;
#my $indent_on = defined($indent) && length($indent);
my @xml = ();
my $empty_element_map = $self->_empty_element_map;
my ( $tag, $node, $start ); # per-iteration scratch
$self->traverse(
sub {
( $node, $start ) = @_;
if ( ref $node ) { # it's an element
$tag = $node->{'_tag'};
if ($start) { # on the way in
foreach my $attr ( $node->all_attr_names() ) {
croak("$tag has an invalid attribute name '$attr'")
unless ( $attr eq '/'
|| $self->_valid_name($attr) );
}
if ( $empty_element_map->{$tag}
and !@{ $node->{'_content'} || $nillio } )
{
push( @xml, $node->starttag_XML( undef, 1 ) );
}
else {
push( @xml, $node->starttag_XML(undef) );
}
}
else { # on the way out
unless ( $empty_element_map->{$tag}
and !@{ $node->{'_content'} || $nillio } )
{
push( @xml, $node->endtag_XML() );
} # otherwise it will have been an <... /> tag.
}
}
else { # it's just text
_xml_escape($node) unless ($in_cdata);
push( @xml, $node );
}
1; # keep traversing
}
);
join( '', @xml, "\n" );
}
#--------------------------------------------------------------------------
1;
__END__
=head1 NAME
XML::Element - XML elements with the same interface as HTML::Element
=head1 SYNOPSIS
[See HTML::Element]
=head1 METHODS AND ATTRIBUTES
=head2 delete_ignorable_whitespace
TODO: test and document this:
with no tagname set, assumes ALL all-whitespace nodes are ignorable!
=head2 endtag
Redirects to endtag_XML
=head2 starttag
Redirects to starttag_XML
=head2 as_XML
$s = $doc->as_XML()
Returns a string representing in XML the element and its descendants.
=head2 starttag_XML
$start = $doc->starttag_XML();
Returns a string representing the complete start tag for the element.
Except for CDATA.
=head2 endtag_XML
$end = $doc->endtag_XML();
Returns a string representing the complete end tag for this element.
I.e., "</", tag name, and ">". Except for CDATA.
=head1 DESCRIPTION
This is just a subclass of HTML::Element. It works basically the same
as HTML::Element, except that tagnames and attribute names aren't
forced to lowercase, as they are in HTML::Element.
L<HTML::Element> describes everything you can do with this class.
=head1 CAVEATS
Has currently no handling of namespaces.
=head1 SEE ALSO
L<XML::TreeBuilder> for a class that actually builds XML::Element
structures.
L<HTML::Element> for all documentation.
L<XML::DOM> and L<XML::Twig> for other XML document tree interfaces.
L<XML::Generator> for more fun.
=head1 COPYRIGHT AND DISCLAIMERS
Copyright (c) 2000,2004 Sean M. Burke. All rights reserved.
Copyright (c) 2010,2011,2013 Jeff Fearn. All rights reserved.
This library is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.
This program is distributed in the hope that it will be useful, but
without any warranty; without even the implied warranty of
merchantability or fitness for a particular purpose.
=head1 AUTHOR
Current Author:
Jeff Fearn E<lt>jfearn@cpan.orgE<gt>.
Former Authors:
Sean M. Burke, E<lt>sburke@cpan.orgE<gt>
=cut
|