File Coverage

blib/lib/CGI/Test/Page/HTML.pm
Criterion Covered Total %
statement 39 52 75.0
branch 1 8 12.5
condition 3 6 50.0
subroutine 10 12 83.3
pod 4 5 80.0
total 57 83 68.6


line stmt bran cond sub pod time code
1             package CGI::Test::Page::HTML;
2 14     14   89 use strict;
  14         20  
  14         688  
3 14     14   86 use warnings;
  14         38  
  14         819  
4             ####################################################################
5             # $Id: HTML.pm 411 2011-09-26 11:19:30Z nohuhu@nohuhu.org $
6             # $Name: cgi-test_0-104_t1 $
7             ####################################################################
8             #
9             # Copyright (c) 2001, Raphael Manfredi
10             #
11             # You may redistribute only under the terms of the Artistic License,
12             # as specified in the README file that comes with the distribution.
13              
14             require CGI::Test::Page::Real;
15 14     14   73 use base qw(CGI::Test::Page::Real);
  14         34  
  14         6868  
16              
17             #
18             # ->new
19             #
20             # Creation routine
21             #
22             sub new
23             {
24 16     16 0 54 my $this = bless {}, shift;
25 16         292 $this->_init(@_);
26 16         46 return $this;
27             }
28              
29             #
30             # Attribute access
31             #
32              
33             sub tree
34             {
35 16     16 1 28 my $this = shift;
36 16   33     80 return $this->{tree} || $this->_build_tree();
37             }
38              
39             sub forms
40             {
41 16     16 1 86 my $this = shift;
42 16   66     94 return $this->{forms} || $this->_xtract_forms();
43             }
44              
45             sub form_count
46             {
47 2     2 1 8 my $this = shift;
48 2 50       22 $this->_xtract_forms() unless exists $this->{form_count};
49 2         18 return $this->{form_count};
50             }
51              
52             #
53             # ->_build_tree
54             #
55             # Parse HTML content from `raw_content' into an HTML tree.
56             # Only called the first time an access to `tree' is requested.
57             #
58             # Returns constructed tree object.
59             #
60             sub _build_tree
61             {
62 16     16   22 my $this = shift;
63              
64 16         11596 require HTML::TreeBuilder;
65              
66 16         382243 my $tree = HTML::TreeBuilder->new();
67 16         4119 $tree->ignore_unknown(0); # Keep everything, even unknown tags
68 16         148 $tree->store_comments(1); # Useful things may hide in "comments"
69 16         108 $tree->store_declarations(1); # Store everything that we may test
70 16         100 $tree->store_pis(1); # Idem
71 16         98 $tree->warn(1); # We want to know if there's a problem
72              
73 16         281 $tree->parse($this->raw_content);
74 16         149351 $tree->eof;
75              
76 16         7135 return $this->{tree} = $tree;
77             }
78              
79             #
80             # _xtract_forms
81             #
82             # Extract tags out of the tree, and for each form, build a
83             # CGI::Test::Form object that represents it.
84             # Only called the first time an access to `forms' is requested.
85             #
86             # Side effect: updates the `forms' and `form_count' attributes.
87             #
88             # Returns list ref of objects, in the order they were found.
89             #
90             sub _xtract_forms
91             {
92 16     16   28 my $this = shift;
93 16         48 my $tree = $this->tree;
94              
95 16         8048 require CGI::Test::Form;
96              
97             #
98             # The CGI::Test::Form objects we're about to create will refer back to
99             # us, because they are conceptually part of this page. Besides, their
100             # HTML tree is a direct reference into our own tree.
101             #
102              
103 16     915   302 my @forms = $tree->look_down(sub {$_[ 0 ]->tag eq "form"});
  915         7235  
104 16         239 @forms = map {CGI::Test::Form->new($_, $this)} @forms;
  16         99  
105              
106 16         44 $this->{form_count} = scalar @forms;
107 16         139 return $this->{forms} = \@forms;
108             }
109              
110             #
111             # ->delete
112             #
113             # Break circular references
114             #
115             sub delete
116             {
117 0     0 1   my $this = shift;
118              
119             #
120             # The following attributes are "lazy", i.e. calculated on demand.
121             # Therefore, take precautions before de-referencing them.
122             #
123              
124 0 0         $this->{tree} = $this->{tree}->delete if ref $this->{tree};
125 0 0         if (ref $this->{forms})
126             {
127 0           foreach my $form (@{$this->{forms}})
  0            
128             {
129 0           $form->delete;
130             }
131 0           delete $this->{forms};
132             }
133              
134 0           $this->SUPER::delete;
135 0           return;
136             }
137              
138             #
139             # (DESTROY)
140             #
141             # Dispose of HTML tree properly
142             #
143             sub DESTROY
144             {
145 0     0     my $this = shift;
146 0 0         return unless ref $this->{tree};
147 0           $this->{tree} = $this->{tree}->delete;
148 0           return;
149             }
150              
151             1;
152              
153             =head1 NAME
154              
155             CGI::Test::Page::HTML - A HTML page reply
156              
157             =head1 SYNOPSIS
158              
159             # Inherits from CGI::Test::Page::Real
160              
161             =head1 DESCRIPTION
162              
163             This class represents an HTTP reply containing C data.
164             When testing CGI scripts, this is usually what one gets back.
165              
166             =head1 INTERFACE
167              
168             The interface is the same as the one described in L,
169             with the following addition:
170              
171             =over 4
172              
173             =item C
174              
175             Returns the root of the HTML tree of the page content, as an
176             HTML::Element node.
177              
178             =back
179              
180             =head1 AUTHORS
181              
182             The original author is Raphael Manfredi.
183              
184             Steven Hilton was long time maintainer of this module.
185              
186             Current maintainer is Alexander Tokarev Ftokarev@cpan.orgE>.
187              
188             =head1 SEE ALSO
189              
190             CGI::Test::Page::Real(3), HTML::Element(3).
191              
192             =cut
193