blib/lib/HTML/Display/Common.pm

filter: Statementstmt 100% 73 / 73 Branchbran 92.8% 26 / 28 Conditioncond 84.6% 11 / 13 MC/DCmcdc 75.0% 6 / 8 Subroutinesub 86.6% 13 / 15 Podpod 100% 2 / 2 totaltotal 94.2% 131 / 139 CC 20 SCAR 23.5
CC
20
Cov
96%
display
20.0
BEGIN
1.0
BEGIN
1.0
CRAP 10.5
↑ Summary blib/lib/HTML/Display.pm »
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 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 7 ▶use HTML::TokeParser;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 7 ▶use URI::URL;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 7 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 8 $VERSION='0.40';
14 8 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 =head2 __PACKAGE__-E<gt>new %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 ▶=for example
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: not called CC=1 SCAR=0.0
23 no warnings 'redefine';
24 *HTML::Display::WhizBang::display_html = sub {};
25  
26 =for example begin
27  
28 2 ▶ package HTML::Display::WhizBang;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: not called CC=1 SCAR=0.0
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 ▶ package main;
Subroutine BEGIN: called CC=1 SCAR=0.0
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 10 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
52 11 my ($class) = shift;
53 #croak "Odd number" if @_ % 2;
54 11 my $self = { @_ };
55 11 bless $self,$class;
56 11 $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 ▶=for example
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
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 = "<html><body><h1>Hello world!</h1></body></html>";
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_,"<html><body><h1>Hello world!</h1></body></html>","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<location> parameter comes in very handy :
96  
97 1 ▶=for example
Subroutine BEGIN: called CC=1 SCAR=0.0
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 = '<html><body><img src="/images/hp0.gif"></body></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 '<html><head><base href="http://www.google.com/" /></head><body><img src="/images/hp0.gif"></body></html>',
119 "HTML gets output");
120 $main::_STDOUT_ = "";
121 $browser->display( html => $html, location => 'http://www.google.com' );
122 is( $main::_STDOUT_,
123 '<html><head><base href="http://www.google.com/" /></head><body><img src="/images/hp0.gif"></body></html>',
124 "HTML gets output");
125  
126 =cut
127  
128 21 ▶sub display {
Subroutine display: called CC=20 SCAR=30.0
Pod documented
129 22 my ($self) = shift;
130 22 my %args;
131 22 ▶ if (scalar @_ == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ == 1) { } 2 17 19
132 5 %args = ( html => $_[0] );
133 } else {
134 20 %args = @_;
135 };
136  
137 22 ▶ if ($args{file}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"file"}) 0 19 19
138 2 my $filename = delete $args{file};
139 2 local $/;
140 2 local *FILE;
141 2 ▶ open FILE, "<", $filename
Branch: 1 branch
BranchTrueFalseTotal
open FILE, "<", $filename or croak("Couldn't read $filename") 0 1 1
142 or croak "Couldn't read $filename";
143 2 $args{html} = <FILE>;
144 };
145  
146 $args{base} = delete $args{location}
147 21 ▶ if (! exists $args{base} and exists $args{location});
Branch: 1 branch
BranchTrueFalseTotal
if not exists $args{"base"} and exists $args{"location"} 14 6 20
Condition100%3/3
not exists $args{"base"} and exists $args{"location"}
!ll&&!rl&&r
1414
MC/DC100%2/2
not exists $args{"base"} and exists $args{"location"}
not exists $args{"base"} exists $args{"location"}
Truth table100%3/3
not(A) and B
A= not exists $args{"base"}B= exists $args{"location"}
ABresult
0X0
100
111
148  
149 21 my $new_html;
150 21 ▶ if (exists $args{base}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{'base'}) { } 15 5 20
151 # trim to directory create BASE HREF
152 # We are carefull to not trim if we just have http://domain.com
153 17 my $location = URI::URL->new( $args{base} );
154 17 my $path = $location->path;
155 17 $path =~ s%(?<!/)/[^/]*$%/%;
156 16 $location = sprintf "%s://%s%s", $location->scheme, $location->authority , $path;
157  
158 16 require HTML::TokeParser::Simple;
159 16 ▶ my $p = HTML::TokeParser::Simple->new(\$args{html}) || die 'could not create HTML::TokeParser::Simple object';
Condition50%1/2
'HTML::TokeParser::Simple'->new(\$args{'html'}) || die('could not create HTML::TokeParser::Simple object')
l!l
150
MC/DC0%0/1
'HTML::TokeParser::Simple'->new(\$args{'html'}) || die('could not create HTML::TokeParser::Simple object')
'HTML::TokeParser::Simple'->new(\$args{'html'})
Truth table50%1/2
A
A= 'HTML::TokeParser::Simple'->new(\$args{'html'})
Aresult
11
00
160 16 my ($has_head,$has_base);
161 17 ▶ while (my $token = $p->get_token) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $token = $p->get_token) 71 12 83
162 73 ▶ if ( $token->is_start_tag('head') ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($token->is_start_tag('head')) { } 11 60 71
elsif ($token->is_start_tag('base')) { } 4 56 60
163 13 $has_head++;
164 } elsif ( $token->is_start_tag('base')) {
165 6 $has_base++;
166 5 last;
167 };
168 };
169  
170 # restart parsing
171 16 ▶ $p = HTML::TokeParser::Simple->new(\$args{html}) || die 'could not create HTML::TokeParser::Simple object';
Condition50%1/2
'HTML::TokeParser::Simple'->new(\$args{'html'}) || die('could not create HTML::TokeParser::Simple object')
l!l
150
MC/DC0%0/1
'HTML::TokeParser::Simple'->new(\$args{'html'}) || die('could not create HTML::TokeParser::Simple object')
'HTML::TokeParser::Simple'->new(\$args{'html'})
Truth table50%1/2
A
A= 'HTML::TokeParser::Simple'->new(\$args{'html'})
Aresult
11
00
172 16 ▶ while (my $token = $p->get_token) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $token = $p->get_token) 75 12 87
173 76 ▶ if ( $token->is_start_tag('html') and not $has_head) {
Branch: 3 branches
BranchTrueFalseTotal
if ($token->is_start_tag('html') and not $has_head) { } 3 72 75
elsif ($token->is_start_tag('head') and not $has_base) { } 7 65 72
elsif ($token->is_start_tag('base')) { } 4 61 65
Condition100%3/3
$token->is_start_tag("html") and not $has_head
!ll&&!rl&&r
61113
MC/DC100%2/2
$token->is_start_tag("html") and not $has_head
$token->is_start_tag("html") not $has_head
Truth table100%3/3
A and not(B)
A= $token->is_start_tag("html")B= not $has_head
ABresult
0X0
100
111
174 4 $new_html .= $token->as_is . qq{<head><base href="$location" /></head>};
175 ▶ } elsif ( $token->is_start_tag('head') and not $has_base) {
Condition100%3/3
$token->is_start_tag("head") and not $has_base
!ll&&!rl&&r
6147
MC/DC100%2/2
$token->is_start_tag("head") and not $has_base
$token->is_start_tag("head") not $has_base
Truth table100%3/3
A and not(B)
A= $token->is_start_tag("head")B= not $has_base
ABresult
0X0
100
111
176 # handle an empty <head /> :
177 8 ▶ if ($token->as_is =~ m!^<\s*head\s*/>$!i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token->as_is =~ m[^<\s*head\s*/>$]i) { } 1 6 7
178 2 $new_html .= qq{<head><base href="$location" /></head>}
179 } else {
180 7 $new_html .= $token->as_is . qq{<base href="$location" />};
181 };
182 } elsif ( $token->is_start_tag('base') ) {
183 # If they already have a <base href>, give up
184 5 ▶ if ($token->return_attr->{href}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token->return_attr->{'href'}) { } 3 1 4
185 4 $new_html = $args{html};
186 4 last;
187 } else {
188 2 $token->set_attr('href',$location);
189 2 $new_html .= $token->as_is;
190 };
191 } else {
192 62 $new_html .= $token->as_is;
193 }
194 };
195 } else {
196 5 $new_html = $args{html};
197 };
198  
199 20 $self->display_html($new_html);
200 };
201  
202 =head1 AUTHOR
203  
204 Copyright (c) 2004-2013 Max Maischein C<< <corion@cpan.org> >>
205  
206 =head1 LICENSE
207  
208 This module is released under the same terms as Perl itself.
209  
210 =cut
211  
212  
213 8 1;
↑ Summary blib/lib/HTML/Display.pm »