line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package HTML::Display::Common; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
=head1 NAME |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
HTML::Display::Common - routines common to all HTML::Display subclasses |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
=cut |
8
|
|
|
|
|
|
|
|
9
|
8
|
|
|
7
|
|
48751
|
use strict; |
|
7
|
|
|
|
|
16
|
|
|
7
|
|
|
|
|
252
|
|
10
|
7
|
|
|
7
|
|
6128
|
use HTML::TokeParser; |
|
7
|
|
|
|
|
151106
|
|
|
7
|
|
|
|
|
275
|
|
11
|
7
|
|
|
7
|
|
6560
|
use URI::URL; |
|
8
|
|
|
|
|
93389
|
|
|
7
|
|
|
|
|
409
|
|
12
|
7
|
|
|
7
|
|
79
|
use vars qw($VERSION); |
|
8
|
|
|
|
|
25
|
|
|
7
|
|
|
|
|
517
|
|
13
|
|
|
|
|
|
|
$VERSION='0.40'; |
14
|
8
|
|
|
7
|
|
52
|
use Carp qw( croak ); |
|
8
|
|
|
|
|
21
|
|
|
8
|
|
|
|
|
5896
|
|
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
=head2 __PACKAGE__-Enew %ARGS |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
Creates a new object as a blessed hash. The passed arguments are stored within |
19
|
|
|
|
|
|
|
the hash. If you need to do other things in your constructor, remember to call |
20
|
|
|
|
|
|
|
this constructor as well : |
21
|
|
|
|
|
|
|
|
22
|
2
|
|
|
3
|
|
33711
|
=for example |
|
2
|
|
|
0
|
|
5
|
|
|
2
|
|
|
|
|
107
|
|
|
1
|
|
|
|
|
412
|
|
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
2
|
|
23
|
|
|
|
|
|
|
no warnings 'redefine'; |
24
|
|
|
|
|
|
|
*HTML::Display::WhizBang::display_html = sub {}; |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
=for example begin |
27
|
|
|
|
|
|
|
|
28
|
2
|
|
|
2
|
|
1015
|
package HTML::Display::WhizBang; |
|
2
|
|
|
1
|
|
532
|
|
|
2
|
|
|
|
|
10
|
|
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
100
|
|
29
|
|
|
|
|
|
|
use parent 'HTML::Display::Common'; |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
sub new { |
32
|
|
|
|
|
|
|
my ($class) = shift; |
33
|
|
|
|
|
|
|
my %args = @_; |
34
|
|
|
|
|
|
|
my $self = $class->SUPER::new(%args); |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
# do stuff |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
$self; |
39
|
|
|
|
|
|
|
}; |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
=for example end |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
=for example_testing |
44
|
1
|
|
|
1
|
|
5
|
package main; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
4
|
|
45
|
|
|
|
|
|
|
use HTML::Display; |
46
|
|
|
|
|
|
|
my $browser = HTML::Display->new( class => "HTML::Display::WhizBang"); |
47
|
|
|
|
|
|
|
isa_ok($browser,"HTML::Display::Common"); |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
=cut |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
sub new { |
52
|
11
|
|
|
10
|
1
|
820
|
my ($class) = shift; |
53
|
|
|
|
|
|
|
#croak "Odd number" if @_ % 2; |
54
|
11
|
|
|
|
|
30
|
my $self = { @_ }; |
55
|
11
|
|
|
|
|
278
|
bless $self,$class; |
56
|
11
|
|
|
|
|
46
|
$self; |
57
|
|
|
|
|
|
|
}; |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
=head2 $display->display %ARGS |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
This is the routine used to display the HTML to the user. It takes the |
62
|
|
|
|
|
|
|
following parameters : |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
html => SCALAR containing the HTML |
65
|
|
|
|
|
|
|
file => SCALAR containing the filename of the file to be displayed |
66
|
|
|
|
|
|
|
base => optional base url for the HTML, so that relative links still work |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
location (synonymous to base) |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
=head3 Basic usage : |
71
|
|
|
|
|
|
|
|
72
|
1
|
|
|
1
|
|
5
|
=for example |
|
1
|
|
|
0
|
|
19
|
|
|
1
|
|
|
|
|
750
|
|
73
|
|
|
|
|
|
|
no warnings 'redefine'; |
74
|
|
|
|
|
|
|
*HTML::Display::new = sub { |
75
|
|
|
|
|
|
|
my $class = shift; |
76
|
|
|
|
|
|
|
require HTML::Display::Dump; |
77
|
|
|
|
|
|
|
return HTML::Display::Dump->new(@_); |
78
|
|
|
|
|
|
|
}; |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
=for example begin |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
my $html = "Hello world!"; |
83
|
|
|
|
|
|
|
my $browser = HTML::Display->new(); |
84
|
|
|
|
|
|
|
$browser->display( html => $html ); |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
=for example end |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
=for example_testing |
89
|
|
|
|
|
|
|
isa_ok($browser, "HTML::Display::Dump","The browser"); |
90
|
|
|
|
|
|
|
is( $main::_STDOUT_,"Hello world!","HTML gets output"); |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
=head3 Location parameter : |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
If you fetch a page from a remote site but still want to display |
95
|
|
|
|
|
|
|
it to the user, the C parameter comes in very handy : |
96
|
|
|
|
|
|
|
|
97
|
1
|
|
|
1
|
|
414
|
=for example |
|
1
|
|
|
|
|
6
|
|
|
1
|
|
|
|
|
13
|
|
98
|
|
|
|
|
|
|
no warnings 'redefine'; |
99
|
|
|
|
|
|
|
*HTML::Display::new = sub { |
100
|
|
|
|
|
|
|
my $class = shift; |
101
|
|
|
|
|
|
|
require HTML::Display::Dump; |
102
|
|
|
|
|
|
|
return HTML::Display::Dump->new(@_); |
103
|
|
|
|
|
|
|
}; |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
=for example begin |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
my $html = ''; |
108
|
|
|
|
|
|
|
my $browser = HTML::Display->new(); |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
# This will display part of the Google logo |
111
|
|
|
|
|
|
|
$browser->display( html => $html, base => 'http://www.google.com' ); |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
=for example end |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
=for example_testing |
116
|
|
|
|
|
|
|
isa_ok($browser, "HTML::Display::Dump","The browser"); |
117
|
|
|
|
|
|
|
is( $main::_STDOUT_, |
118
|
|
|
|
|
|
|
'', |
119
|
|
|
|
|
|
|
"HTML gets output"); |
120
|
|
|
|
|
|
|
$main::_STDOUT_ = ""; |
121
|
|
|
|
|
|
|
$browser->display( html => $html, location => 'http://www.google.com' ); |
122
|
|
|
|
|
|
|
is( $main::_STDOUT_, |
123
|
|
|
|
|
|
|
'', |
124
|
|
|
|
|
|
|
"HTML gets output"); |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
=cut |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
sub display { |
129
|
21
|
|
|
21
|
1
|
7112
|
my ($self) = shift; |
130
|
21
|
|
|
|
|
466
|
my %args; |
131
|
21
|
100
|
|
|
|
828
|
if (scalar @_ == 1) { |
132
|
4
|
|
|
|
|
16
|
%args = ( html => $_[0] ); |
133
|
|
|
|
|
|
|
} else { |
134
|
19
|
|
|
|
|
2653
|
%args = @_; |
135
|
|
|
|
|
|
|
}; |
136
|
|
|
|
|
|
|
|
137
|
21
|
50
|
|
|
|
203
|
if ($args{file}) { |
138
|
2
|
|
|
|
|
54
|
my $filename = delete $args{file}; |
139
|
2
|
|
|
|
|
113
|
local $/; |
140
|
2
|
|
|
|
|
434
|
local *FILE; |
141
|
2
|
0
|
|
|
|
3
|
open FILE, "<", $filename |
142
|
|
|
|
|
|
|
or croak "Couldn't read $filename"; |
143
|
2
|
|
|
|
|
14
|
$args{html} = ; |
144
|
|
|
|
|
|
|
}; |
145
|
|
|
|
|
|
|
|
146
|
21
|
100
|
100
|
|
|
158
|
$args{base} = delete $args{location} |
147
|
|
|
|
|
|
|
if (! exists $args{base} and exists $args{location}); |
148
|
|
|
|
|
|
|
|
149
|
21
|
|
|
|
|
33
|
my $new_html; |
150
|
21
|
100
|
|
|
|
891
|
if (exists $args{base}) { |
151
|
|
|
|
|
|
|
# trim to directory create BASE HREF |
152
|
|
|
|
|
|
|
# We are carefull to not trim if we just have http://domain.com |
153
|
17
|
|
|
|
|
86
|
my $location = URI::URL->new( $args{base} ); |
154
|
17
|
|
|
|
|
32120
|
my $path = $location->path; |
155
|
17
|
|
|
|
|
697
|
$path =~ s%(?
|
156
|
17
|
|
|
|
|
92
|
$location = sprintf "%s://%s%s", $location->scheme, $location->authority , $path; |
157
|
|
|
|
|
|
|
|
158
|
17
|
|
|
|
|
3696
|
require HTML::TokeParser::Simple; |
159
|
17
|
|
50
|
|
|
37905
|
my $p = HTML::TokeParser::Simple->new(\$args{html}) || die 'could not create HTML::TokeParser::Simple object'; |
160
|
17
|
|
|
|
|
2059
|
my ($has_head,$has_base); |
161
|
16
|
|
|
|
|
59
|
while (my $token = $p->get_token) { |
162
|
72
|
100
|
|
|
|
3779
|
if ( $token->is_start_tag('head') ) { |
|
|
100
|
|
|
|
|
|
163
|
12
|
|
|
|
|
554
|
$has_head++; |
164
|
|
|
|
|
|
|
} elsif ( $token->is_start_tag('base')) { |
165
|
5
|
|
|
|
|
444
|
$has_base++; |
166
|
5
|
|
|
|
|
17
|
last; |
167
|
|
|
|
|
|
|
}; |
168
|
|
|
|
|
|
|
}; |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
# restart parsing |
171
|
16
|
|
50
|
|
|
343
|
$p = HTML::TokeParser::Simple->new(\$args{html}) || die 'could not create HTML::TokeParser::Simple object'; |
172
|
16
|
|
|
|
|
1751
|
while (my $token = $p->get_token) { |
173
|
76
|
100
|
100
|
|
|
3150
|
if ( $token->is_start_tag('html') and not $has_head) { |
|
|
100
|
100
|
|
|
|
|
|
|
100
|
|
|
|
|
|
174
|
4
|
|
|
|
|
54
|
$new_html .= $token->as_is . qq{}; |
175
|
|
|
|
|
|
|
} elsif ( $token->is_start_tag('head') and not $has_base) { |
176
|
|
|
|
|
|
|
# handle an empty | :
177
|
8
|
100
|
|
|
|
165
|
if ($token->as_is =~ m!^<\s*head\s*/>$!i) { |
178
|
2
|
|
|
|
|
20
|
$new_html .= qq{} |
179
|
|
|
|
|
|
|
} else { |
180
|
7
|
|
|
|
|
40
|
$new_html .= $token->as_is . qq{}; |
181
|
|
|
|
|
|
|
}; |
182
|
|
|
|
|
|
|
} elsif ( $token->is_start_tag('base') ) { |
183
|
|
|
|
|
|
|
# If they already have a , give up |
184
|
5
|
100
|
|
|
|
132
|
if ($token->return_attr->{href}) { |
185
|
4
|
|
|
|
|
47
|
$new_html = $args{html}; |
186
|
4
|
|
|
|
|
78
|
last; |
187
|
|
|
|
|
|
|
} else { |
188
|
2
|
|
|
|
|
32
|
$token->set_attr('href',$location); |
189
|
2
|
|
|
|
|
593
|
$new_html .= $token->as_is; |
190
|
|
|
|
|
|
|
}; |
191
|
|
|
|
|
|
|
} else { |
192
|
62
|
|
|
|
|
1402
|
$new_html .= $token->as_is; |
193
|
|
|
|
|
|
|
} |
194
|
|
|
|
|
|
|
}; |
195
|
|
|
|
|
|
|
} else { |
196
|
5
|
|
|
|
|
38
|
$new_html = $args{html}; |
197
|
|
|
|
|
|
|
}; |
198
|
|
|
|
|
|
|
|
199
|
20
|
|
|
|
|
430
|
$self->display_html($new_html); |
200
|
|
|
|
|
|
|
}; |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
=head1 AUTHOR |
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
Copyright (c) 2004-2013 Max Maischein C<< >> |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
=head1 LICENSE |
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
This module is released under the same terms as Perl itself. |
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
=cut |
211
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
1; |