1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
sub new { # constructor! my $class = shift; $class = ref($class) || $class; # Initialize HTML::Element part my $self = $class->element_class->new('html'); { # A hack for certain strange versions of Parser: my $other_self = HTML::Parser->new(); %$self = ( %$self, %$other_self ); # copy fields # Yes, multiple inheritance is messy. Kids, don't try this at home. bless $other_self, "HTML::TreeBuilder::_hideyhole"; # whack it out of the HTML::Parser class, to avoid the destructor }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#!/usr/bin/perl use lib "/testscript/modules"; use TemplateTreeBuilder; my $rootelement = TemplateTreeBuilder->new(); #Ich dachte durch diese Zeile würde es gehen. Aber Pustekuchen: Aber: Es gibt keine Fehlermeldung ?!? $rootelement->case_sensitive(1); #nicht standart html Tags werden mit in den Baum aufgenommen. $rootelement->ignore_unknown(0); #& in source stays as it is: no decode to & $rootelement->no_expand_entities(1); $rootelement->parse_file($ARGV[0]);
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
#!/usr/bin/perl package TemplateTreeBuilder; use lib "/testproject/modules"; use MyElement; use HTML::TreeBuilder; #inherit from TreeBuilder @ISA = qw(HTML::TreeBuilder); #use parent qw/HTML::TreeBuilder/; #constructor sub new { my $class = shift; $class = ref($class) || $class; #invoke base constructor my $self = $class->SUPER::new(@_); $self->{_element_class}='MyElement'; bless($self, $class); return $self; } return 1;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
#!/usr/bin/perl package MyElement; use HTML::Element; #inherit from Element @ISA = qw(HTML::Element); *HTML::Element::_fold_case = \&HTML::Element::_fold_case_NOT; #overwrite the var from base class #constructor sub new { my $class = shift; $class = ref($class) || $class; #invoke base constructor my $self = $class->SUPER::new(@_); bless($self, $class); return $self; } return 1;
QuoteIch öffne solche Verlinkungen grundsätzlich nicht....
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
#!/usr/bin/perl
package MyElement;
use HTML::Element;
#inherit from Element
@ISA = qw(HTML::Element);
sub dummy{
#HTML::Element::_fold_case_NOT;
@a = HTML::Element::_fold_case_NOT(@_);
return @a;
}
*HTML::Element::_fold_case = \&HTML::Element::_fold_case_NOT;
#*_fold_case = \&dummy;
#constructor
sub new {
my $class = shift;
$class = ref($class) || $class;
#invoke base constructor
my $self = $class->SUPER::new(@_);
bless($self, $class);
return $self;
}
sub starttag {
my ( $self, $entities ) = @_;
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?
return
"<!"
. join( ' ', map( "--$_--", @{ $self->{'text'} } ) ) . ">";
}
else {
return "<!--" . $self->{'text'} . "-->";
}
}
my $tag = "<$name";
my $val;
for ( sort keys %$self ) { # predictable ordering
next if !length $_ or m/^_/s or $_ eq '/';
$val = $self->{$_};
next if !defined $val; # or ref $val;
if ($_ eq $val && # if attribute is boolean, for this element
exists( $HTML::Element::boolean_attr{$name} )
&& (ref( $HTML::Element::boolean_attr{$name} )
? $HTML::Element::boolean_attr{$name}{$_}
: $HTML::Element::boolean_attr{$name} eq $_
)
)
{
$tag .= " $_";
}
else { # non-boolean attribute
if ( ref $val eq 'HTML::Element'
and $val->{_tag} eq '~literal' )
{
$val = $val->{text};
}
else {
HTML::Entities::encode_entities( $val, $entities )
unless (
defined($entities) && !length($entities)
|| $encoded_content
);
}
$val = qq{"$val"};
$tag .= qq{ $_\E=$val};
}
} # for keys
if ( scalar $self->content_list == 0
&& $self->_empty_element_map->{ $self->tag } )
{
return $tag . " />";
}
else {
return $tag . ">";
}
}
sub endtag {
"</$_[0]->{'_tag'}>";
}
return 1;