lib/HTML/Object.pm

filter: Statementstmt 77.2% 332 / 430 Branchbran 47.7% 85 / 178 Conditioncond 36.0% 54 / 150 MC/DCmcdc 12.6% 10 / 79 Subroutinesub 87.5% 49 / 56 Podpod 100% 30 / 30 totaltotal 60.6% 560 / 923 CC 143 SCAR 41.5
CC
143
Cov
62%
parse_url
420.0
__ANON__
182.0
set_dom
30.0
CRAP 63.4
« blib/lib/HTML/Object/DOM/NodeIteratorShared.pm ↑ Summary lib/HTML/Object/DOM/Element.pm »
1 ##----------------------------------------------------------------------------
2 ## HTML Object - ~/lib//mnt/src/perl/HTML-Object/lib/HTML/Object.pm
3 ## Version v0.6.2
4 ## Copyright(c) 2026 DEGUEST Pte. Ltd.
5 ## Author: Jacques Deguest <jack@deguest.jp>
6 ## Created 2021/04/20
7 ## Modified 2026/06/27
8 ## All rights reserved.
9 ##
10 ##
11 ## This program is free software; you can redistribute it and/or modify it
12 ## under the same terms as Perl itself.
13 ##----------------------------------------------------------------------------
14 package HTML::Object;
15 BEGIN
16 {
17 36 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 36 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 36 ▶ use warnings::register;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 36 ▶ use parent qw( Module::Generic );
Subroutine BEGIN: called CC=1 SCAR=0.0
21 36 ▶ use vars qw( $DICT $LINK_ELEMENTS $FATAL_ERROR $GLOBAL_DOM $VERSION );
Subroutine BEGIN: called CC=1 SCAR=0.0
22 36 ▶ use curry;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 36 ▶ use Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
24 36 ▶ use Filter::Util::Call;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 36 ▶ use HTML::Object::Closing;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 36 ▶ use HTML::Object::Comment;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 36 ▶ use HTML::Object::Declaration;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 36 ▶ use HTML::Object::Document;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 36 ▶ use HTML::Object::Element;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 36 ▶ use HTML::Object::Space;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 36 ▶ use HTML::Object::Text;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 36 ▶ use HTML::Parser;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 36 ▶ use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 36 ▶ use Module::Generic::File qw( file );
Subroutine BEGIN: called CC=1 SCAR=0.0
35 36 ▶ use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
36 36 ▶ our $VERSION = 'v0.6.2';
Subroutine BEGIN: called CC=1 SCAR=0.0
37 36 our $DICT = {};
38 36 our $LINK_ELEMENTS = {};
39 36 our $FATAL_ERROR = 0;
40 };
41  
42 36 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 36 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
44  
45 {
46 36 my $me = file( __FILE__ );
47 36 my $path = $me->parent;
48 36 my $dict_json = 'html_tags_dict.json';
49 36 my $tags_repo = $path->child( $dict_json );
50 36 ▶ if( $tags_repo->exists )
Branch: 1 branch
BranchTrueFalseTotal
if ($tags_repo->exists) { } 36 0 36
51 {
52 # try-catch
53 36 local $@;
54 eval
55 36 {
56 36 ▶ my $json = $tags_repo->load_utf8 ||
Condition50%1/2
$tags_repo->load_utf8 || die(qq[Unable to open html tags json dictionary "$tags_repo": ], $tags_repo->error)
l!l
360
MC/DC0%0/1
$tags_repo->load_utf8 || die(qq[Unable to open html tags json dictionary "$tags_repo": ], $tags_repo->error)
$tags_repo->load_utf8
Truth table50%1/2
A
A= $tags_repo->load_utf8
Aresult
11
00
57 die( "Unable to open html tags json dictionary \"$tags_repo\": ", $tags_repo->error );
58 36 my $j = JSON->new->relaxed;
59 36 my $hash = $j->decode( $json );
60 36 ▶ die( "No html tags found inside dictionary file \"$tags_repo\"\n" ) if( !scalar( keys( %{$hash->{dict}} ) ) );
Branch: 1 branch
BranchTrueFalseTotal
unless scalar keys %{$$hash{"dict"};} 0 36 36
61 36 $DICT = $hash->{dict};
62 36 for( keys( %$DICT ) )
63 {
64 7416 ▶ if( exists( $DICT->{ $_ }->{link_in} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $HTML::Object::DICT->{$_}{"link_in"}) 936 6480 7416
65 {
66 936 $LINK_ELEMENTS->{ $_ } = $DICT->{ $_ }->{link_in};
67 }
68 }
69 };
70 36 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 36 36
71 {
72 0 die( "Fatal error occurred while trying to load html tags json dictionary \"$tags_repo\": $@\n" );
73 }
74 }
75 else
76 {
77 0 die( "Missing core file \"$dict_json\"\n" );
78 }
79 }
80  
81 sub import
82 {
83 55 ▶ my $class = shift( @_ );
Subroutine import: called CC=20 SCAR=34.0
84 55 my $hash = {};
85 55 ▶ for( my $i = 0; $i < scalar( @_ ); $i++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @_) 17 55 72
86 {
87 17 ▶ if( $_[$i] eq 'debug' ||
Branch: 1 branch
BranchTrueFalseTotal
if ($_[$i] eq "debug" or $_[$i] eq "debug_code" or $_[$i] eq "debug_file" or $_[$i] eq "fatal_error" or $_[$i] eq "global_dom" or $_[$i] eq "try_catch" or $_[$i] eq "xquery") 6 4 10
Condition55%10/18
$_[$i] eq "debug" or $_[$i] eq "debug_code" or $_[$i] eq "debug_file" or $_[$i] eq "fatal_error" or $_[$i] eq "global_dom" or $_[$i] eq "try_catch" or $_[$i] eq "xquery"
l!l&&r!l&&!r
764
$_[$i] eq "debug" or $_[$i] eq "debug_code" or $_[$i] eq "debug_file" or $_[$i] eq "fatal_error" or $_[$i] eq "global_dom" or $_[$i] eq "try_catch"
l!l&&r!l&&!r
7010
$_[$i] eq "debug" or $_[$i] eq "debug_code" or $_[$i] eq "debug_file" or $_[$i] eq "fatal_error" or $_[$i] eq "global_dom"
l!l&&r!l&&!r
0710
$_[$i] eq "debug" or $_[$i] eq "debug_code" or $_[$i] eq "debug_file" or $_[$i] eq "fatal_error"
l!l&&r!l&&!r
0017
$_[$i] eq "debug" or $_[$i] eq "debug_code" or $_[$i] eq "debug_file"
l!l&&r!l&&!r
0017
$_[$i] eq "debug" or $_[$i] eq "debug_code"
l!l&&r!l&&!r
0017
MC/DC28%2/7
$_[$i] eq "debug" or $_[$i] eq "debug_code" or $_[$i] eq "debug_file" or $_[$i] eq "fatal_error" or $_[$i] eq "global_dom" or $_[$i] eq "try_catch" or $_[$i] eq "xquery"
$_[$i] eq "debug" $_[$i] eq "debug_code" $_[$i] eq "debug_file" $_[$i] eq "fatal_error" $_[$i] eq "global_dom" $_[$i] eq "try_catch" $_[$i] eq "xquery"
Truth table37%3/8
A or B or C or D or E or F or G
A= $_[$i] eq "debug"B= $_[$i] eq "debug_code"C= $_[$i] eq "debug_file"D= $_[$i] eq "fatal_error"E= $_[$i] eq "global_dom"F= $_[$i] eq "try_catch"G= $_[$i] eq "xquery"
ABCDEFGresult
1XXXXXX1
01XXXXX1
001XXXX1
0001XXX1
00001XX1
000001X1
00000011
00000000
88 $_[$i] eq 'debug_code' ||
89 $_[$i] eq 'debug_file' ||
90 $_[$i] eq 'fatal_error' ||
91 $_[$i] eq 'global_dom' ||
92 $_[$i] eq 'try_catch' ||
93 $_[$i] eq 'xquery' )
94 {
95 13 $hash->{ $_[$i] } = $_[$i+1];
96 13 CORE::splice( @_, $i, 2 );
97 13 $i--;
98 }
99 }
100  
101 {
102 55 local $Exporter::ExportLevel = 1;
103 55 Exporter::import( $class, @_ );
104 }
105 55 ▶ $hash->{debug} = 0 if( !CORE::exists( $hash->{debug} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $hash->{"debug"} 55 0 55
106 55 ▶ $hash->{global_dom} = 0 if( !CORE::exists( $hash->{global_dom} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $hash->{"global_dom"} 48 7 55
107 55 ▶ $hash->{debug_code} = 0 if( !CORE::exists( $hash->{debug_code} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $hash->{"debug_code"} 55 0 55
108 55 ▶ $hash->{fatal_error} = 0 if( !CORE::exists( $hash->{fatal_error} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $hash->{"fatal_error"} 55 0 55
109 55 ▶ $hash->{try_catch} = 0 if( !CORE::exists( $hash->{try_catch} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $hash->{"try_catch"} 55 0 55
110 55 ▶ if( $hash->{fatal_error} )
Branch: 1 branch
BranchTrueFalseTotal
if ($hash->{"fatal_error"}) 0 55 55
111 {
112 0 $FATAL_ERROR = 1;
113 }
114  
115 55 ▶ if( $hash->{try_catch} )
Branch: 1 branch
BranchTrueFalseTotal
if ($hash->{"try_catch"}) 0 55 55
116 {
117 0 require Nice::Try;
118 0 Nice::Try->export_to_level( 1, @_ );
119 }
120  
121 55 ▶ if( $hash->{global_dom} || $hash->{xquery} )
Branch: 1 branch
BranchTrueFalseTotal
if ($hash->{"global_dom"} or $hash->{"xquery"}) 7 48 55
Condition66%2/3
$hash->{"global_dom"} or $hash->{"xquery"}
l!l&&r!l&&!r
7048
MC/DC50%1/2
$hash->{"global_dom"} or $hash->{"xquery"}
$hash->{"global_dom"} $hash->{"xquery"}
Truth table66%2/3
A or B
A= $hash->{"global_dom"}B= $hash->{"xquery"}
ABresult
1X1
011
000
122 {
123 7 ▶ Filter::Util::Call::filter_add( bless( $hash => ( ref( $class ) || $class ) ) );
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
070
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
124 7 require HTML::Object::XQuery;
125 7 HTML::Object::XQuery->export_to_level( 1, @_ );
126 7 ▶ if( $hash->{global_dom} )
Branch: 1 branch
BranchTrueFalseTotal
if ($hash->{"global_dom"}) 7 0 7
127 {
128 # Same as Firefox, Chrome or Safari do: default dom for blank page
129 7 our $GLOBAL_DOM = __PACKAGE__->new( debug => $hash->{debug} )->parse( <<EOT );
130 <html><head></head><body></body></html>
131 EOT
132 }
133 }
134 }
135  
136 sub filter
137 {
138 7 ▶ my( $self ) = @_ ;
Subroutine filter: called CC=8 SCAR=24.2
Pod documented
139 7 my( $status, $last_line );
140 7 my $line = 0;
141 7 my $code = '';
142 7 ▶ if( !$self->{global_dom} )
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"global_dom"}) 0 7 7
143 {
144 0 Filter::Util::Call::filter_del();
145 0 $status = 1;
146 0 return( $status );
147 }
148 7 ▶ while( $status = Filter::Util::Call::filter_read() )
Branch: 1 branch
BranchTrueFalseTotal
if ($status = &Filter::Util::Call::filter_read()) 1045 7 1052
149 {
150 1045 ▶ return( $status ) if( $status < 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $status < 0 0 1045 1045
151 1045 $line++;
152 1045 ▶ if( /^__(?:DATA|END)__/ )
Branch: 1 branch
BranchTrueFalseTotal
if (/^__(?:DATA|END)__/) 0 1045 1045
153 {
154 0 last;
155 }
156  
157 s{
158 (?<!\\)\$\(
159 }
160 {
161 36 "xq("
162 1045 }gexs;
163 1045 ▶ if( $self->{xquery} )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"xquery"}) 975 70 1045
164 {
165 975 s{
166 (?<!\\)\$\.([a-zA-Z]+)
167 }
168 {
169 69 "xQuery->$1"
170 }gexs;
171 }
172 }
173 7 ▶ if( $self->{debug_file} )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"debug_file"}) 0 7 7
174 {
175 0 ▶ if( open( my $fh, ">$self->{debug_file}" ) )
Branch: 1 branch
BranchTrueFalseTotal
if (open my $fh, qq[>$self->{"debug_file"}]) 0 0 0
176 {
177 0 binmode( $fh, ':utf8' );
178 0 print( $fh $_ );
179 0 close( $fh );
180 }
181 }
182 7 return( $line );
183 }
184  
185 sub init
186 {
187 99 ▶ my $self = shift( @_ );
Subroutine init: called CC=3 SCAR=11.1
Pod documented
188 99 $self->{_init_strict_use_sub} = 1;
189 99 ▶ $self->{_exception_class} = 'HTML::Object::Exception' unless( CORE::exists( $self->{_exception_class} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"_exception_class"} 99 0 99
190 99 ▶ $self->SUPER::init( @_ ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->SUPER::init(@_) or return $self->pass_error 0 99 99
191 99 my $p = HTML::Parser->new(
192 api_version => 3,
193 start_h => [ $self->curry::add_start, 'self, tagname, attr, attrseq, text, column, line, offset, offset_end'],
194 end_h => [ $self->curry::add_end, 'self, tagname, attr, attrseq, text, column, line, offset, offset_end' ],
195 marked_sections => 1,
196 comment_h => [ $self->curry::add_comment, 'self, text, column, line, offset, offset_end'],
197 declaration_h => [ $self->curry::add_declaration, 'self, text, column, line, offset, offset_end'],
198 default_h => [ $self->curry::add_default, 'self, tagname, attr, attrseq, text, column, line, offset, offset_end'],
199 text_h => [ $self->curry::add_text, 'self, text, column, line, offset, offset_end'],
200 # This is not activated, because as per the documentation, this will call an 'end tag' caller, and this could imply <br></br> for other unknown tags, whereas with <br /> we know for sure this is an empty tag
201 # empty_element_tags => 1,
202 unbroken_text => 1,
203 );
204 99 $self->{document} = '';
205 99 $self->{current_parent} = '';
206 99 $self->{_parser} = $p;
207 99 $self->{_elems} = [];
208 99 return( $self );
209 }
210  
211 sub add_comment
212 {
213 8 ▶ my $self = shift( @_ );
Subroutine add_comment: called CC=2 SCAR=6.9
Pod documented
214 8 my @args = @_;
215 8 my $opts = {};
216 8 my @p = qw( p raw col line offset offset_end );
217 8 @$opts{ @p } = @args;
218 8 my $parent = $self->current_parent;
219 8 my $val = $opts->{raw};
220 8 $val =~ s,^\<\!\-\-|\-\-\>$,,gs;
221 my $e = $self->new_comment({
222 column => $opts->{col},
223 line => $opts->{line},
224 offset => $opts->{offset},
225 original => $opts->{raw},
226 8 ▶ parent => $parent,
Condition50%1/2
$self->new_comment({'column', $opts->{'col'}, 'line', $opts->{'line'}, 'offset', $opts->{'offset'}, 'original', $opts->{'raw'}, 'parent', $parent, 'value', $val, 'debug', $self->debug}) || (return)
l!l
80
MC/DC0%0/1
$self->new_comment({'column', $opts->{'col'}, 'line', $opts->{'line'}, 'offset', $opts->{'offset'}, 'original', $opts->{'raw'}, 'parent', $parent, 'value', $val, 'debug', $self->debug}) || (return)
$self->new_comment({'column', $opts->{'col'}, 'line', $opts->{'line'}, 'offset', $opts->{'offset'}, 'original', $opts->{'raw'}, 'parent', $parent, 'value', $val, 'debug', $self->debug})
Truth table50%1/2
A
A= $self->new_comment({'column', $opts->{'col'}, 'line', $opts->{'line'}, 'offset', $opts->{'offset'}, 'original', $opts->{'raw'}, 'parent', $parent, 'value', $val, 'debug', $self->debug})
Aresult
11
00
227 value => $val,
228 debug => $self->debug,
229 }) || return;
230 8 $parent->children->push( $e );
231 8 return( $e );
232 }
233  
234 sub add_declaration
235 {
236 29 ▶ my $self = shift( @_ );
Subroutine add_declaration: called CC=2 SCAR=6.9
Pod documented
237 29 my @args = @_;
238 29 my $opts = {};
239 29 my @p = qw( p raw col line offset offset_end );
240 29 @$opts{ @p } = @args;
241 29 my $parent = $self->current_parent;
242 29 ▶ return if( !$self->_is_a( $parent => 'HTML::Object::DOM::Document' ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_a($parent, "HTML::Object::DOM::Document") 1 28 29
243 my $e = $self->new_declaration({
244 column => $opts->{col},
245 line => $opts->{line},
246 offset => $opts->{offset},
247 original => $opts->{raw},
248 28 parent => $parent,
249 debug => $self->debug,
250 });
251 # $parent->children->push( $e );
252 28 $self->document->declaration( $e );
253 28 $parent->children->push( $e );
254 28 return( $e );
255 }
256  
257 sub add_default
258 {
259 146 ▶ my $self = shift( @_ );
Subroutine add_default: called CC=7 SCAR=33.0
Pod documented
260 146 my @args = @_;
261 146 my $opts = {};
262 146 my @p = qw( p tag attr seq raw col line offset offset_end );
263 146 @$opts{ @p } = @args;
264 146 ▶ return if( !CORE::length( $opts->{raw} ) && !defined( $opts->{tag} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless length $opts->{"raw"} or defined $opts->{"tag"} 146 0 146
Condition33%1/3
length $opts->{"raw"} or defined $opts->{"tag"}
l!l&&r!l&&!r
00146
MC/DC0%0/2
length $opts->{"raw"} or defined $opts->{"tag"}
length $opts->{"raw"} defined $opts->{"tag"}
Truth table33%1/3
A or B
A= length $opts->{"raw"}B= defined $opts->{"tag"}
ABresult
1X1
011
000
265 # Unknown tag, so we check if there is a "/>" to determine if this is an empty (void) tag or not
266 0 my $attr = $opts->{attr};
267 0 my $def = {};
268 0 ▶ $def->{is_empty} = exists( $attr->{'/'} ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
exists $attr->{'/'} ? : 0 0 0
269 0 my $parent = $self->current_parent;
270 0 ▶ if( !length( $opts->{tag} ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (length $opts->{"tag"}) 0 0 0
271 {
272 0 my @args_keys = qw( p raw col line offset offset_end );
273 0 return( $self->add_text( @$opts{ @args_keys } ) );
274 }
275 # Check the current parent and see if we need to close it.
276 # If this new tag is a non-empty tag (i.e. non-void) and the current parent has not been closed,
277 # implicitly close it now, by setting that tag's parent as the current parent
278 # This is what Mozilla does:
279 # Ref: <https://bugzilla.mozilla.org/show_bug.cgi?id=820926>
280 # NOTE This needs to be done in post processing not during initial parsing, because at this point in the process we have not yet seen the closing tag, and we might see it later, so making guesses here is ill-advised.
281 # if( !$parent->is_closed &&
282 # !$def->{is_empty} &&
283 # $parent &&
284 # !$parent->isa( 'HTML::Object::Document' ) &&
285 # $parent->tag ne 'html' )
286 # {
287 # $parent = $parent->parent;
288 # }
289 my $e = $self->new_element({
290 attributes => $opts->{attr},
291 attributes_sequence => $opts->{seq},
292 column => $opts->{col},
293 is_empty => $def->{is_empty},
294 line => $opts->{line},
295 offset => $opts->{offset},
296 original => $opts->{raw},
297 parent => $parent,
298 tag => $opts->{tag},
299 0 ▶ debug => $self->debug,
Condition0%0/2
$self->new_element({'attributes', $opts->{'attr'}, 'attributes_sequence', $opts->{'seq'}, 'column', $opts->{'col'}, 'is_empty', $def->{'is_empty'}, 'line', $opts->{'line'}, 'offset', $opts->{'offset'}, 'original', $opts->{'raw'}, 'parent', $parent, 'tag', $opts->{'tag'}, 'debug', $self->debug}) || (return)
l!l
00
MC/DC0%0/1
$self->new_element({'attributes', $opts->{'attr'}, 'attributes_sequence', $opts->{'seq'}, 'column', $opts->{'col'}, 'is_empty', $def->{'is_empty'}, 'line', $opts->{'line'}, 'offset', $opts->{'offset'}, 'original', $opts->{'raw'}, 'parent', $parent, 'tag', $opts->{'tag'}, 'debug', $self->debug}) || (return)
$self->new_element({'attributes', $opts->{'attr'}, 'attributes_sequence', $opts->{'seq'}, 'column', $opts->{'col'}, 'is_empty', $def->{'is_empty'}, 'line', $opts->{'line'}, 'offset', $opts->{'offset'}, 'original', $opts->{'raw'}, 'parent', $parent, 'tag', $opts->{'tag'}, 'debug', $self->debug})
Truth table0%0/2
A
A= $self->new_element({'attributes', $opts->{'attr'}, 'attributes_sequence', $opts->{'seq'}, 'column', $opts->{'col'}, 'is_empty', $def->{'is_empty'}, 'line', $opts->{'line'}, 'offset', $opts->{'offset'}, 'original', $opts->{'raw'}, 'parent', $parent, 'tag', $opts->{'tag'}, 'debug', $self->debug})
Aresult
11
00
300 }) || return;
301 0 $parent->children->push( $e );
302 0 ▶ if( !$def->{is_empty} )
Branch: 1 branch
BranchTrueFalseTotal
unless ($def->{"is_empty"}) 0 0 0
303 {
304 0 $self->current_parent( $e );
305 }
306 0 return( $e );
307 }
308  
309 sub add_end
310 {
311 317 ▶ my $self = shift( @_ );
Subroutine add_end: called CC=4 SCAR=14.6
Pod documented
312 317 my @args = @_;
313 317 my $opts = {};
314 317 my @p = qw( p tag attr seq raw col line offset offset_end );
315 317 @$opts{ @p } = @args;
316 317 my $me = $self->current_parent;
317 317 my $parent = $me->parent;
318 317 ▶ if( $opts->{tag} ne $me->tag )
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'tag'} ne $me->tag) { } 0 317 317
319 {
320 0 ▶ warn( "Oops, something is wrong in the parsing. I was expecting a closing tag for \"", $me->tag, "\" that started at line \"", $me->line, "\" but instead found a closing tag for \"$opts->{tag}\" at line \"$opts->{line}\" and column \"$opts->{col}\": $opts->{raw}\n" ) if( $self->_warnings_is_enabled );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_warnings_is_enabled 0 0 0
321 }
322 else
323 {
324 my $e = $self->new_closing({
325 attributes => $opts->{attr},
326 attributes_sequence => $opts->{seq},
327 column => $opts->{col},
328 line => $opts->{line},
329 offset => $opts->{offset},
330 original => $opts->{raw},
331 tag => $opts->{tag},
332 317 ▶ debug => $self->debug,
Condition50%1/2
$self->new_closing({'attributes', $opts->{'attr'}, 'attributes_sequence', $opts->{'seq'}, 'column', $opts->{'col'}, 'line', $opts->{'line'}, 'offset', $opts->{'offset'}, 'original', $opts->{'raw'}, 'tag', $opts->{'tag'}, 'debug', $self->debug}) || (return)
l!l
3170
MC/DC0%0/1
$self->new_closing({'attributes', $opts->{'attr'}, 'attributes_sequence', $opts->{'seq'}, 'column', $opts->{'col'}, 'line', $opts->{'line'}, 'offset', $opts->{'offset'}, 'original', $opts->{'raw'}, 'tag', $opts->{'tag'}, 'debug', $self->debug}) || (return)
$self->new_closing({'attributes', $opts->{'attr'}, 'attributes_sequence', $opts->{'seq'}, 'column', $opts->{'col'}, 'line', $opts->{'line'}, 'offset', $opts->{'offset'}, 'original', $opts->{'raw'}, 'tag', $opts->{'tag'}, 'debug', $self->debug})
Truth table50%1/2
A
A= $self->new_closing({'attributes', $opts->{'attr'}, 'attributes_sequence', $opts->{'seq'}, 'column', $opts->{'col'}, 'line', $opts->{'line'}, 'offset', $opts->{'offset'}, 'original', $opts->{'raw'}, 'tag', $opts->{'tag'}, 'debug', $self->debug})
Aresult
11
00
333 }) || return;
334 317 $me->is_closed(1);
335 317 $me->close_tag( $e );
336 # $parent->children->push( $e );
337 317 $self->current_parent( $parent );
338 }
339 }
340  
341 sub add_space
342 {
343 504 ▶ my $self = shift( @_ );
Subroutine add_space: called CC=2 SCAR=7.0
Pod documented
344 504 my $opts = $self->_get_args_as_hash( @_ );
345 504 my $parent = $self->current_parent;
346 504 ▶ my $e = $self->new_space( $opts ) || return( $self->pass_error );
Condition50%1/2
$self->new_space($opts) || (return $self->pass_error)
l!l
5040
MC/DC0%0/1
$self->new_space($opts) || (return $self->pass_error)
$self->new_space($opts)
Truth table50%1/2
A
A= $self->new_space($opts)
Aresult
11
00
347 504 $parent->children->push( $e );
348 504 return( $e );
349 }
350  
351 sub add_start
352 {
353 441 ▶ my $self = shift( @_ );
Subroutine add_start: called CC=10 SCAR=23.7
Pod documented
354 441 my @args = @_;
355 441 my $opts = {};
356 441 my @p = qw( p tag attr seq raw col line offset offset_end );
357 441 @$opts{ @p } = @args;
358 441 my $parent = $self->current_parent;
359 441 ▶ if( $opts->{tag} =~ s,/,, )
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"tag"} =~ s[/][]) 1 440 441
360 {
361 1 $opts->{attr}->{'/'} = '/';
362 }
363 441 my $def = $self->get_definition( $opts->{tag} );
364 # Make some easy guess
365 441 ▶ if( !scalar( keys( %$def ) ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar keys %$def) 0 441 441
366 {
367 0 ▶ $def->{is_empty} = 1 if( CORE::exists( $opts->{attr}->{'/'} ) );
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts->{"attr"}{"/"} 0 0 0
368 # "Return HTMLUnknownElement"
369 # <https://html.spec.whatwg.org/multipage/dom.html#htmlunknownelement>
370 0 $def->{class} = 'HTML::Object::DOM::Unknown';
371 }
372 441 ▶ $def->{is_empty} = 0 unless( CORE::exists( $def->{is_empty} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $def->{"is_empty"} 0 441 441
373 # Check the current parent and see if we need to close it.
374 # If this new tag is a non-empty tag (i.e. non-void) and the current parent has not been closed,
375 # implicitly close it now, by setting that tag's parent as the current parent
376 # This is what Mozilla does:
377 # Ref: <https://bugzilla.mozilla.org/show_bug.cgi?id=820926>
378 # NOTE This needs to be done in post processing not during initial parsing, because at this point in the process we have not yet seen the closing tag, and we might see it later, so making guesses here is ill-advised.
379 # if( !$parent->is_closed &&
380 # !$def->{is_empty} &&
381 # $parent &&
382 # !$parent->isa( 'HTML::Object::Document' ) &&
383 # $parent->tag ne 'html' )
384 # {
385 # $parent = $parent->parent;
386 # }
387 441 ▶ $def->{class} //= '';
Condition100%2/2
$def->{"class"} //= ""
l!l
40041
MC/DC100%1/1
$def->{"class"} //= ""
$def->{"class"}
Truth table100%2/2
A
A= $def->{"class"}
Aresult
11
00
388 441 my $e;
389 my $params =
390 {
391 attributes => $opts->{attr},
392 attributes_sequence => $opts->{seq},
393 column => $opts->{col},
394 is_empty => $def->{is_empty},
395 line => $opts->{line},
396 offset => $opts->{offset},
397 original => $opts->{raw},
398 parent => $parent,
399 tag => $opts->{tag},
400 # and
401 441 debug => $self->debug,
402 };
403  
404 # If this tag is handled by a special class, instantiate the object by this class
405 441 ▶ if( $def->{class} )
Branch: 1 branch
BranchTrueFalseTotal
if ($def->{'class'}) { } 378 63 441
406 {
407 378 ▶ $e = $self->new_special( $def->{class} => $params ) || return;
Condition50%1/2
$self->new_special($def->{'class'}, $params) || (return)
l!l
3780
MC/DC0%0/1
$self->new_special($def->{'class'}, $params) || (return)
$self->new_special($def->{'class'}, $params)
Truth table50%1/2
A
A= $self->new_special($def->{'class'}, $params)
Aresult
11
00
408 }
409 else
410 {
411 63 ▶ $e = $self->new_element( $params ) || return;
Condition50%1/2
$self->new_element($params) || (return)
l!l
630
MC/DC0%0/1
$self->new_element($params) || (return)
$self->new_element($params)
Truth table50%1/2
A
A= $self->new_element($params)
Aresult
11
00
412 }
413 441 $parent->children->push( $e );
414 # If this element is an element that, by nature, can contain other elements we mark it as the last element seen so it can be used as a parent. When we close it, we switch the parent to its parent .
415 441 ▶ if( !$def->{is_empty} )
Branch: 1 branch
BranchTrueFalseTotal
unless ($def->{"is_empty"}) 323 118 441
416 {
417 323 $self->current_parent( $e );
418 }
419 441 return( $e );
420 }
421  
422 sub add_text
423 {
424 644 ▶ my $self = shift( @_ );
Subroutine add_text: called CC=5 SCAR=16.3
Pod documented
425 644 my @args = @_;
426 644 my $opts = {};
427 644 my @p = qw( p raw col line offset offset_end );
428 644 @$opts{ @p } = @args;
429 644 ▶ my $parent = $self->current_parent ||
Condition50%1/2
$self->current_parent || (return $self->error('You must create a document first using the new_document() method first before adding text.'))
l!l
6440
MC/DC0%0/1
$self->current_parent || (return $self->error('You must create a document first using the new_document() method first before adding text.'))
$self->current_parent
Truth table50%1/2
A
A= $self->current_parent
Aresult
11
00
430 return( $self->error( "You must create a document first using the new_document() method first before adding text." ) );
431 644 my $e;
432 # Text can be either some space or letters, digits (non-space characters)
433 # HTML::Parser does not make the difference, but we do
434 644 ▶ if( $opts->{raw} =~ /^[[:blank:]\h\v]*$/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'raw'} =~ /^[[:blank:]\h\v]*$/) { } 504 140 644
435 {
436 $e = $self->add_space(
437 original => $opts->{raw},
438 column => $opts->{col},
439 line => $opts->{line},
440 offset => $opts->{offset},
441 parent => $parent,
442 value => $opts->{raw},
443 504 ▶ debug => $self->debug,
Condition50%1/2
$self->add_space('original', $opts->{'raw'}, 'column', $opts->{'col'}, 'line', $opts->{'line'}, 'offset', $opts->{'offset'}, 'parent', $parent, 'value', $opts->{'raw'}, 'debug', $self->debug) || (return)
l!l
5040
MC/DC0%0/1
$self->add_space('original', $opts->{'raw'}, 'column', $opts->{'col'}, 'line', $opts->{'line'}, 'offset', $opts->{'offset'}, 'parent', $parent, 'value', $opts->{'raw'}, 'debug', $self->debug) || (return)
$self->add_space('original', $opts->{'raw'}, 'column', $opts->{'col'}, 'line', $opts->{'line'}, 'offset', $opts->{'offset'}, 'parent', $parent, 'value', $opts->{'raw'}, 'debug', $self->debug)
Truth table50%1/2
A
A= $self->add_space('original', $opts->{'raw'}, 'column', $opts->{'col'}, 'line', $opts->{'line'}, 'offset', $opts->{'offset'}, 'parent', $parent, 'value', $opts->{'raw'}, 'debug', $self->debug)
Aresult
11
00
444 # No 'value' set on purpose, because if none, then 'original' will be used by
445 # as_string
446 ) || return;
447 }
448 else
449 {
450 $e = $self->new_text({
451 column => $opts->{col},
452 line => $opts->{line},
453 offset => $opts->{offset},
454 original => $opts->{raw},
455 parent => $parent,
456 value => $opts->{raw},
457 140 ▶ debug => $self->debug,
Condition50%1/2
$self->new_text({'column', $opts->{'col'}, 'line', $opts->{'line'}, 'offset', $opts->{'offset'}, 'original', $opts->{'raw'}, 'parent', $parent, 'value', $opts->{'raw'}, 'debug', $self->debug}) || (return)
l!l
1400
MC/DC0%0/1
$self->new_text({'column', $opts->{'col'}, 'line', $opts->{'line'}, 'offset', $opts->{'offset'}, 'original', $opts->{'raw'}, 'parent', $parent, 'value', $opts->{'raw'}, 'debug', $self->debug}) || (return)
$self->new_text({'column', $opts->{'col'}, 'line', $opts->{'line'}, 'offset', $opts->{'offset'}, 'original', $opts->{'raw'}, 'parent', $parent, 'value', $opts->{'raw'}, 'debug', $self->debug})
Truth table50%1/2
A
A= $self->new_text({'column', $opts->{'col'}, 'line', $opts->{'line'}, 'offset', $opts->{'offset'}, 'original', $opts->{'raw'}, 'parent', $parent, 'value', $opts->{'raw'}, 'debug', $self->debug})
Aresult
11
00
458 }) || return;
459 140 $parent->children->push( $e );
460 }
461 644 return( $e );
462 }
463  
464 254 ▶sub current_parent { return( shift->_set_get_object_without_init( 'current_parent', 'HTML::Object::Element', @_ ) ); }
Subroutine current_parent: called CC=1 SCAR=0.0
Pod documented
465  
466 0 ▶sub dictionary { return( $DICT ); }
Subroutine dictionary: not called CC=1 SCAR=6.9
Pod documented
467  
468 15 ▶sub document { return( shift->_set_get_object( 'document', 'HTML::Object::Document', @_ ) ); }
Subroutine document: called CC=1 SCAR=0.0
Pod documented
469  
470 sub get_definition
471 {
472 462 ▶ my $self = shift( @_ );
Subroutine get_definition: called CC=3 SCAR=11.2
Pod documented
473 462 my $tag = shift( @_ );
474 462 ▶ return( $self->error( "No tag was provided to get its definition." ) ) if( !length( $tag ) );
Branch: 1 branch
BranchTrueFalseTotal
unless length $tag 0 462 462
475 # Just to be sure
476 462 $tag = lc( $tag );
477 462 ▶ return( {} ) if( !exists( $DICT->{ $tag } ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $HTML::Object::DICT->{$tag} 0 462 462
478 462 return( $DICT->{ $tag } );
479 }
480  
481 sub new_closing
482 {
483 34 ▶ my $self = shift( @_ );
Subroutine new_closing: called CC=2 SCAR=7.1
Pod documented
484 34 ▶ my $e = HTML::Object::Closing->new( @_ ) ||
Condition50%1/2
'HTML::Object::Closing'->new(@_) || (return $self->pass_error('HTML::Object::Closing'->error))
l!l
340
MC/DC0%0/1
'HTML::Object::Closing'->new(@_) || (return $self->pass_error('HTML::Object::Closing'->error))
'HTML::Object::Closing'->new(@_)
Truth table50%1/2
A
A= 'HTML::Object::Closing'->new(@_)
Aresult
11
00
485 return( $self->pass_error( HTML::Object::Closing->error ) );
486 34 return( $e );
487 }
488  
489 sub new_comment
490 {
491 1 ▶ my $self = shift( @_ );
Subroutine new_comment: called CC=2 SCAR=7.0
Pod documented
492 1 ▶ my $e = HTML::Object::Comment->new( @_ ) ||
Condition50%1/2
'HTML::Object::Comment'->new(@_) || (return $self->pass_error('HTML::Object::Comment'->error))
l!l
10
MC/DC0%0/1
'HTML::Object::Comment'->new(@_) || (return $self->pass_error('HTML::Object::Comment'->error))
'HTML::Object::Comment'->new(@_)
Truth table50%1/2
A
A= 'HTML::Object::Comment'->new(@_)
Aresult
11
00
493 return( $self->pass_error( HTML::Object::Comment->error ) );
494 1 $e->debug( $self->debug );
495 1 return( $e );
496 }
497  
498 sub new_declaration
499 {
500 0 ▶ my $self = shift( @_ );
Subroutine new_declaration: not called CC=2 SCAR=17.9
Pod documented
501 0 ▶ my $e = HTML::Object::Declaration->new( @_ ) ||
Condition0%0/2
'HTML::Object::Declaration'->new(@_) || (return $self->pass_error('HTML::Object::Declaration'->error))
l!l
00
MC/DC0%0/1
'HTML::Object::Declaration'->new(@_) || (return $self->pass_error('HTML::Object::Declaration'->error))
'HTML::Object::Declaration'->new(@_)
Truth table0%0/2
A
A= 'HTML::Object::Declaration'->new(@_)
Aresult
11
00
502 return( $self->pass_error( HTML::Object::Declaration->error ) );
503 0 $e->debug( $self->debug );
504 0 return( $e );
505 }
506  
507 sub new_document
508 {
509 8 ▶ my $self = shift( @_ );
Subroutine new_document: called CC=2 SCAR=7.0
Pod documented
510 8 ▶ my $e = HTML::Object::Document->new( @_ ) ||
Condition50%1/2
'HTML::Object::Document'->new(@_) || (return $self->pass_error('HTML::Object::Document'->error))
l!l
80
MC/DC0%0/1
'HTML::Object::Document'->new(@_) || (return $self->pass_error('HTML::Object::Document'->error))
'HTML::Object::Document'->new(@_)
Truth table50%1/2
A
A= 'HTML::Object::Document'->new(@_)
Aresult
11
00
511 return( $self->pass_error( HTML::Object::Document->error ) );
512 8 $e->debug( $self->debug );
513 8 return( $e );
514 }
515  
516 sub new_element
517 {
518 50 ▶ my $self = shift( @_ );
Subroutine new_element: called CC=2 SCAR=7.0
Pod documented
519 50 ▶ my $e = HTML::Object::Element->new( @_ ) ||
Condition50%1/2
'HTML::Object::Element'->new(@_) || (return $self->pass_error('HTML::Object::Element'->error))
l!l
500
MC/DC0%0/1
'HTML::Object::Element'->new(@_) || (return $self->pass_error('HTML::Object::Element'->error))
'HTML::Object::Element'->new(@_)
Truth table50%1/2
A
A= 'HTML::Object::Element'->new(@_)
Aresult
11
00
520 return( $self->pass_error( HTML::Object::Element->error ) );
521 50 $e->debug( $self->debug );
522 50 return( $e );
523 }
524  
525 sub new_space
526 {
527 42 ▶ my $self = shift( @_ );
Subroutine new_space: called CC=2 SCAR=7.1
Pod documented
528 42 ▶ my $e = HTML::Object::Space->new( @_ ) ||
Condition50%1/2
'HTML::Object::Space'->new(@_) || (return $self->pass_error('HTML::Object::Space'->error))
l!l
420
MC/DC0%0/1
'HTML::Object::Space'->new(@_) || (return $self->pass_error('HTML::Object::Space'->error))
'HTML::Object::Space'->new(@_)
Truth table50%1/2
A
A= 'HTML::Object::Space'->new(@_)
Aresult
11
00
529 return( $self->pass_error( HTML::Object::Space->error ) );
530 42 return( $e );
531 }
532  
533 sub new_special
534 {
535 392 ▶ my $self = shift( @_ );
Subroutine new_special: called CC=3 SCAR=11.2
Pod documented
536 392 my $class = shift( @_ );
537 392 ▶ $self->_load_class( $class ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->_load_class($class) or return $self->pass_error 0 392 392
538 392 ▶ my $e = $class->new( @_ ) || return( $self->pass_error( $class->error ) );
Condition50%1/2
$class->new(@_) || (return $self->pass_error($class->error))
l!l
3920
MC/DC0%0/1
$class->new(@_) || (return $self->pass_error($class->error))
$class->new(@_)
Truth table50%1/2
A
A= $class->new(@_)
Aresult
11
00
539 392 $e->debug( $self->debug );
540 392 return( $e );
541 }
542  
543 sub new_text
544 {
545 8 ▶ my $self = shift( @_ );
Subroutine new_text: called CC=2 SCAR=7.1
Pod documented
546 8 ▶ my $e = HTML::Object::Text->new( @_ ) ||
Condition50%1/2
'HTML::Object::Text'->new(@_) || (return $self->pass_error('HTML::Object::Text'->error))
l!l
80
MC/DC0%0/1
'HTML::Object::Text'->new(@_) || (return $self->pass_error('HTML::Object::Text'->error))
'HTML::Object::Text'->new(@_)
Truth table50%1/2
A
A= 'HTML::Object::Text'->new(@_)
Aresult
11
00
547 return( $self->pass_error( HTML::Object::Text->error ) );
548 8 return( $e );
549 }
550  
551 sub parse
552 {
553 22 ▶ my $self = shift( @_ );
Subroutine parse: called CC=6 SCAR=19.4
Pod documented
554 22 my $this = shift( @_ );
555 22 my $opts = $self->_get_args_as_hash( @_ );
556 22 ▶ if( ref( $this ) eq 'CODE' || ref( $this ) eq 'GLOB' || "$this" =~ /<\w+/ || CORE::length( "$this" ) > 1024 )
Branch: 2 branches
BranchTrueFalseTotal
if (ref $this eq 'CODE' or ref $this eq 'GLOB' or "$this" =~ /<\w+/ or length "$this" > 1024) { } 21 1 22
elsif (ref $this) { } 0 1 1
Condition55%5/9
ref $this eq "CODE" or ref $this eq "GLOB" or "$this" =~ /<\w+/ or length "$this" > 1024
l!l&&r!l&&!r
2101
ref $this eq "CODE" or ref $this eq "GLOB" or "$this" =~ /<\w+/
l!l&&r!l&&!r
0211
ref $this eq "CODE" or ref $this eq "GLOB"
l!l&&r!l&&!r
0022
MC/DC25%1/4
ref $this eq "CODE" or ref $this eq "GLOB" or "$this" =~ /<\w+/ or length "$this" > 1024
ref $this eq "CODE" ref $this eq "GLOB" "$this" =~ /<\w+/ length "$this" > 1024
Truth table40%2/5
A or B or C or D
A= ref $this eq "CODE"B= ref $this eq "GLOB"C= "$this" =~ /<\w+/D= length "$this" > 1024
ABCDresult
1XXX1
01XX1
001X1
00011
00000
557 {
558 21 return( $self->parse_data( $this, $opts ) );
559 }
560 elsif( ref( $this ) )
561 {
562 0 return( $self->error( "I was provided a reference (", overload::StrVal( $this ), ") to parse html data, but I do not know what to do with it." ) );
563 }
564 else
565 {
566 1 return( $self->parse_file( $this, $opts ) );
567 }
568 }
569  
570 sub parse_data
571 {
572 69 ▶ my $self = shift( @_ );
Subroutine parse_data: called CC=8 SCAR=21.2
Pod documented
573 69 my $html = shift( @_ );
574 69 my $opts = $self->_get_args_as_hash( @_ );
575 69 $html = "$html";
576 # try-catch
577 69 local $@;
578 eval
579 69 {
580 69 ▶ if( $opts->{utf8} )
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"utf8"}) 0 69 69
581 {
582 0 $html = Encode::decode( 'utf8', $html, Encode::FB_CROAK );
583 }
584 };
585 69 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 69 69
586 {
587 0 return( $self->error( "Error found while utf8 decoding ", length( $html ), " bytes of html data provided: $@" ) );
588 }
589  
590 69 my $e;
591 69 ▶ if( length( $self->{current_parent} ) && $self->_is_object( $self->{current_parent} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (length $self->{'current_parent'} and $self->_is_object($self->{'current_parent'})) { } 0 69 69
Condition33%1/3
length $self->{"current_parent"} and $self->_is_object($self->{"current_parent"})
!ll&&!rl&&r
6900
MC/DC0%0/2
length $self->{"current_parent"} and $self->_is_object($self->{"current_parent"})
length $self->{"current_parent"} $self->_is_object($self->{"current_parent"})
Truth table33%1/3
A and B
A= length $self->{"current_parent"}B= $self->_is_object($self->{"current_parent"})
ABresult
0X0
100
111
592 {
593 0 $e = $self->current_parent;
594 }
595 else
596 {
597 69 $e = $self->new_document( debug => $self->debug );
598 69 $self->document( $e );
599 69 $self->current_parent( $e );
600 69 ▶ if( $self->isa( 'HTML::Object::DOM' ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->isa("HTML::Object::DOM")) 62 7 69
601 {
602 62 ▶ if( my $code = $self->onload )
Branch: 1 branch
BranchTrueFalseTotal
if (my $code = $self->onload) 3 59 62
603 {
604 3 $e->onload( $code );
605 }
606 62 ▶ if( my $code = $self->onreadystatechange )
Branch: 1 branch
BranchTrueFalseTotal
if (my $code = $self->onreadystatechange) 1 61 62
607 {
608 1 $e->onreadystatechange( $code );
609 }
610 }
611 }
612 69 my $doc = $self->document;
613 69 my $p = $self->parser;
614 69 $self->_set_state( 'loading' => $doc );
615 69 $p->parse( $html );
616 69 $self->_set_state( 'interactive' => $doc );
617 69 $self->post_process( $e );
618 69 $self->_set_state( 'complete' => $doc );
619 69 $p->eof;
620 69 return( $e );
621 }
622  
623 sub parse_file
624 {
625 4 ▶ my $self = shift( @_ );
Subroutine parse_file: called CC=10 SCAR=24.8
Pod documented
626 4 ▶ my $file = shift( @_ ) || return( $self->error( "No file to parse was provided." ) );
Condition50%1/2
shift @_ || (return $self->error('No file to parse was provided.'))
l!l
40
MC/DC0%0/1
shift @_ || (return $self->error('No file to parse was provided.'))
shift @_
Truth table50%1/2
A
A= shift @_
Aresult
11
00
627 4 my $opts = $self->_get_args_as_hash( @_ );
628 # On, by default, if not provided
629 4 ▶ $opts->{utf8} //= 1;
Condition50%1/2
$opts->{"utf8"} //= 1
l!l
04
MC/DC0%0/1
$opts->{"utf8"} //= 1
$opts->{"utf8"}
Truth table50%1/2
A
A= $opts->{"utf8"}
Aresult
11
00
630 4 my $f = $self->new_file( $file );
631 4 ▶ if( !$f->exists )
Branch: 2 branches
BranchTrueFalseTotal
if (not $f->exists) { } 0 4 4
elsif ($f->is_empty) { } 0 4 4
632 {
633 0 return( $self->error( "File to parse \"$file\" does not exist." ) );
634 }
635 elsif( $f->is_empty )
636 {
637 0 return( $self->error( "File to parse \"$file\" is empty." ) );
638 }
639 4 my $params = {};
640 4 ▶ $params->{binmode} = 'utf8' if( $opts->{utf8} );
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"utf8"} 4 0 4
641 4 ▶ my $io = $f->open( '<', $params ) ||
Condition50%1/2
$f->open('<', $params) || (return $self->error(qq[Unable to open file to parse "$file": ], $f->error))
l!l
40
MC/DC0%0/1
$f->open('<', $params) || (return $self->error(qq[Unable to open file to parse "$file": ], $f->error))
$f->open('<', $params)
Truth table50%1/2
A
A= $f->open('<', $params)
Aresult
11
00
642 return( $self->error( "Unable to open file to parse \"$file\": ", $f->error ) );
643 4 my $e = $self->new_document( _last_modified => $f->mtime );
644 4 $self->document( $e );
645 4 ▶ if( $self->isa( 'HTML::Object::DOM' ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->isa("HTML::Object::DOM")) 3 1 4
646 {
647 3 ▶ if( my $code = $self->onload )
Branch: 1 branch
BranchTrueFalseTotal
if (my $code = $self->onload) 0 3 3
648 {
649 0 $e->onload( $code );
650 }
651 3 ▶ if( my $code = $self->onreadystatechange )
Branch: 1 branch
BranchTrueFalseTotal
if (my $code = $self->onreadystatechange) 0 3 3
652 {
653 0 $e->onreadystatechange( $code );
654 }
655 }
656 4 $self->current_parent( $e );
657 4 $self->_set_state( 'loading' => $e );
658 4 my $p = $self->parser;
659 4 $p->parse_file( $io );
660 4 $io->close;
661 4 $self->_set_state( 'interactive' => $e );
662 4 $self->post_process( $e );
663 4 $self->_set_state( 'complete' => $e );
664 4 return( $e );
665 }
666  
667 sub parse_url
668 {
669 0 ▶ my $self = shift( @_ );
Subroutine parse_url: not called CC=20 SCAR=60.4
Pod documented
670 0 my $uri;
671 0 ▶ if( ( scalar( @_ ) == 1 && ref( $_[0] ) ne 'HASH' ) ||
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ == 1 and ref $_[0] ne "HASH" or scalar @_ > 1 and @_ % 2 || scalar @_ == 2 && ref $_[1] eq "HASH") 0 0 0
Condition0%0/15
scalar @_ == 1 and ref $_[0] ne "HASH" or scalar @_ > 1 and @_ % 2 || scalar @_ == 2 && ref $_[1] eq "HASH"
l!l&&r!l&&!r
000
scalar @_ == 1 and ref $_[0] ne "HASH"
!ll&&!rl&&r
000
scalar @_ > 1 and @_ % 2 || scalar @_ == 2 && ref $_[1] eq "HASH"
!ll&&!rl&&r
000
@_ % 2 or scalar @_ == 2 and ref $_[1] eq "HASH"
l!l&&r!l&&!r
000
scalar @_ == 2 and ref $_[1] eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/6
scalar @_ == 1 and ref $_[0] ne "HASH" or scalar @_ > 1 and @_ % 2 || scalar @_ == 2 && ref $_[1] eq "HASH"
scalar @_ == 1 ref $_[0] ne "HASH" scalar @_ > 1 @_ % 2 scalar @_ == 2 ref $_[1] eq "HASH"
Truth table0%0/11
A and B or C and D or E and F
A= scalar @_ == 1B= ref $_[0] ne "HASH"C= scalar @_ > 1D= @_ % 2E= scalar @_ == 2F= ref $_[1] eq "HASH"
ABCDEFresult
11XXXX1
0X11XX1
0X10111
1011XX1
1010111
0X0XXX0
0X100X0
0X10100
100XXX0
10100X0
1010100
672 ( scalar( @_ ) > 1 &&
673 (
674 ( @_ % 2 ) ||
675 ( scalar( @_ ) == 2 && ref( $_[1] ) eq 'HASH' )
676 )
677 ) )
678 {
679 0 $uri = shift( @_ );
680 }
681 0 my $opts = $self->_get_args_as_hash( @_ );
682 0 ▶ $uri = CORE::delete( $opts->{uri} ) if( defined( $opts->{uri} ) && CORE::length( $opts->{uri} ) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts->{"uri"} and length $opts->{"uri"} 0 0 0
Condition0%0/3
defined $opts->{"uri"} and length $opts->{"uri"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $opts->{"uri"} and length $opts->{"uri"}
defined $opts->{"uri"} length $opts->{"uri"}
Truth table0%0/3
A and B
A= defined $opts->{"uri"}B= length $opts->{"uri"}
ABresult
0X0
100
111
683 0 ▶ if( !$self->_load_class( 'LWP::UserAgent', { version => '6.49' } ) )
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_load_class("LWP::UserAgent", {"version", "6.49"})) 0 0 0
684 {
685 0 return( $self->error( "LWP::UserAgent version 6.49 or higher is required to use load()" ) );
686 }
687 0 ▶ if( !$self->_load_class( 'URI', { version => '1.74' } ) )
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_load_class("URI", {"version", "1.74"})) 0 0 0
688 {
689 0 return( $self->error( "URI version 1.74 or higher is required to use load()" ) );
690 }
691 0 ▶ $opts->{timeout} //= 10;
Condition0%0/2
$opts->{"timeout"} //= 10
l!l
00
MC/DC0%0/1
$opts->{"timeout"} //= 10
$opts->{"timeout"}
Truth table0%0/2
A
A= $opts->{"timeout"}
Aresult
11
00
692 # try-catch
693 0 local $@;
694 eval
695 0 {
696 0 $uri = URI->new( "$uri" );
697 };
698 0 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
699 {
700 0 return( $self->error( "Bad url provided \"$uri\": $@" ) );
701 }
702  
703 0 my $content;
704 my $resp;
705 # try-catch
706 eval
707 0 {
708 my $ua = LWP::UserAgent->new(
709 agent => "HTML::Object/$VERSION",
710 timeout => $opts->{timeout},
711 0 );
712 0 ▶ $resp = $ua->get( $uri, ( CORE::exists( $opts->{headers} ) && defined( $opts->{headers} ) && ref( $opts->{headers} ) eq 'HASH' && scalar( keys( %{$opts->{headers}} ) ) ) ? %{$opts->{headers}} : () );
Branch: 1 branch
BranchTrueFalseTotal
exists $opts->{'headers'} && defined $opts->{'headers'} && ref $opts->{'headers'} eq 'HASH' && scalar keys %{$$opts{'headers'};} ? : 0 0 0
Condition0%0/8
exists $opts->{"headers"} and defined $opts->{"headers"} and ref $opts->{"headers"} eq "HASH" and scalar keys %{$$opts{"headers"};}
!ll
00
exists $opts->{"headers"} and defined $opts->{"headers"} and ref $opts->{"headers"} eq "HASH"
!ll&&!rl&&r
000
exists $opts->{"headers"} and defined $opts->{"headers"}
!ll&&!rl&&r
000
MC/DC0%0/3
exists $opts->{"headers"} and defined $opts->{"headers"} and ref $opts->{"headers"} eq "HASH" and scalar keys %{$$opts{"headers"};}
exists $opts->{"headers"} defined $opts->{"headers"} ref $opts->{"headers"} eq "HASH"
Truth table0%0/4
A and B and C
A= exists $opts->{"headers"}B= defined $opts->{"headers"}C= ref $opts->{"headers"} eq "HASH"
ABCresult
0XX0
10X0
1100
1111
713 0 $content = $resp->decoded_content;
714 };
715 0 ▶ if( $resp->header( 'Client-Warning' ) || !$resp->is_success )
Branch: 1 branch
BranchTrueFalseTotal
if ($resp->header("Client-Warning") or not $resp->is_success) 0 0 0
Condition0%0/3
$resp->header("Client-Warning") or not $resp->is_success
l!l&&r!l&&!r
000
MC/DC0%0/2
$resp->header("Client-Warning") or not $resp->is_success
$resp->header("Client-Warning") not $resp->is_success
Truth table0%0/3
A or not(B)
A= $resp->header("Client-Warning")B= not $resp->is_success
ABresult
1X1
011
000
716 {
717 0 return( $self->error({
718 code => $resp->code,
719 message => $resp->message,
720 }) );
721 }
722 0 $self->response( $resp );
723 0 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
724 {
725 0 return( $self->error( "Error making a GET request to $uri: $@" ) );
726 }
727 0 my $doc = $self->parse_data( $content );
728 0 $doc->uri( $uri );
729 0 return( $doc );
730 }
731  
732 73 ▶sub parser { return( shift->_set_get_object_without_init( '_parser', 'HTML::Parser', @_ ) ); }
Subroutine parser: called CC=1 SCAR=0.0
Pod documented
733  
734 sub post_process
735 {
736 390 ▶ my $self = shift( @_ );
Subroutine post_process: called CC=3 SCAR=11.6
Pod documented
737 390 my $elem = shift( @_ );
738 390 ▶ return if( !$self->_is_object( $elem ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_object($elem) 0 390 390
739 390 ▶ return if( !$elem->isa( 'HTML::Object::Element' ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $elem->isa("HTML::Object::Element") 0 390 390
740 # Crawl through the tree and look for unclosed tags
741 $elem->children->foreach(sub
742 {
743 1083 ▶ my $e = shift( @_ );
Subroutine __ANON__: called CC=11 SCAR=26.6
744 1083 ▶ return(1) if( $e->isa( 'HTML::Object::Closing' ) || $e->tag->substr( 0, 1 ) eq '_' );
Branch: 1 branch
BranchTrueFalseTotal
if $e->isa("HTML::Object::Closing") or $e->tag->substr(0, 1) eq "_" 642 441 1083
Condition66%2/3
$e->isa("HTML::Object::Closing") or $e->tag->substr(0, 1) eq "_"
l!l&&r!l&&!r
0642441
MC/DC50%1/2
$e->isa("HTML::Object::Closing") or $e->tag->substr(0, 1) eq "_"
$e->isa("HTML::Object::Closing") $e->tag->substr(0, 1) eq "_"
Truth table66%2/3
A or B
A= $e->isa("HTML::Object::Closing")B= $e->tag->substr(0, 1) eq "_"
ABresult
1X1
011
000
745 441 ▶ if( $e->is_empty && $e->children->length )
Branch: 3 branches
BranchTrueFalseTotal
if ($e->is_empty and $e->children->length) { } 0 441 441
elsif ($e->is_empty and not $e->attributes->exists('/')) { } 8 433 441
elsif (not $e->is_empty || $e->is_closed) { } 0 433 433
Condition66%2/3
$e->is_empty and $e->children->length
!ll&&!rl&&r
3171240
MC/DC0%0/2
$e->is_empty and $e->children->length
$e->is_empty $e->children->length
Truth table66%2/3
A and B
A= $e->is_emptyB= $e->children->length
ABresult
0X0
100
111
746 {
747 # It should be empty, but has some children
748 }
749 ▶ elsif( $e->is_empty && !$e->attributes->exists( '/' ) )
Condition100%3/3
$e->is_empty and not $e->attributes->exists("/")
!ll&&!rl&&r
3171168
MC/DC100%2/2
$e->is_empty and not $e->attributes->exists("/")
$e->is_empty not $e->attributes->exists("/")
Truth table100%3/3
A and not(B)
A= $e->is_emptyB= not $e->attributes->exists("/")
ABresult
0X0
100
111
750 {
751 # Is empty, but does not end with />
752 }
753 ▶ elsif( !$e->is_empty && !$e->is_closed )
Condition66%2/3
$e->is_empty || $e->is_closed
l!l&&r!l&&!r
1163170
MC/DC0%0/2
$e->is_empty || $e->is_closed
$e->is_empty $e->is_closed
Truth table66%2/3
A || B
A= $e->is_emptyB= $e->is_closed
ABresult
1X1
011
000
754 {
755 0 my $def = $self->get_definition( $e->tag );
756 0 ▶ if( !$def->{is_empty} )
Branch: 1 branch
BranchTrueFalseTotal
if (not $def->{'is_empty'}) { } 0 0 0
757 {
758 # Is an enclosing tag, but it has not been closed
759 }
760 else
761 {
762 # Is an empty (void) tag, but it did not end with /
763 }
764 }
765 441 ▶ $self->post_process( $e ) if( !$e->is_empty );
Branch: 1 branch
BranchTrueFalseTotal
if not $e->is_empty 317 124 441
766 390 });
767 390 return( $self );
768 }
769  
770 0 ▶sub response { return( shift->_set_get_object_without_init( 'response', 'HTTP::Response', @_ ) ); }
Subroutine response: not called CC=1 SCAR=6.9
Pod documented
771  
772 sub sanity_check
773 {
774 0 ▶ my $self = shift( @_ );
Subroutine sanity_check: not called CC=3 SCAR=24.8
Pod documented
775 0 my $elem = shift( @_ );
776 0 ▶ return if( !$self->_is_object( $elem ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_object($elem) 0 0 0
777 0 ▶ return if( !$elem->isa( 'HTML::Object::Element' ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $elem->isa("HTML::Object::Element") 0 0 0
778 # Crawl through the tree and look for unclosed tags
779 $elem->children->foreach(sub
780 {
781 0 ▶ my $e = shift( @_ );
Subroutine __ANON__: not called CC=13 SCAR=52.0
782 0 ▶ return(1) if( $e->isa( 'HTML::Object::Closing' ) || $e->tag->substr( 0, 1 ) eq '_' );
Branch: 1 branch
BranchTrueFalseTotal
if $e->isa("HTML::Object::Closing") or $e->tag->substr(0, 1) eq "_" 0 0 0
Condition0%0/3
$e->isa("HTML::Object::Closing") or $e->tag->substr(0, 1) eq "_"
l!l&&r!l&&!r
000
MC/DC0%0/2
$e->isa("HTML::Object::Closing") or $e->tag->substr(0, 1) eq "_"
$e->isa("HTML::Object::Closing") $e->tag->substr(0, 1) eq "_"
Truth table0%0/3
A or B
A= $e->isa("HTML::Object::Closing")B= $e->tag->substr(0, 1) eq "_"
ABresult
1X1
011
000
783 0 ▶ if( $e->is_empty && $e->children->length )
Branch: 4 branches
BranchTrueFalseTotal
if ($e->is_empty and $e->children->length) { } 0 0 0
elsif ($e->is_empty and not $e->attributes->exists('/')) { } 0 0 0
elsif (not $e->is_empty and $e->attributes->exists('/')) { } 0 0 0
elsif (not $e->is_empty || $e->is_closed) { } 0 0 0
Condition0%0/3
$e->is_empty and $e->children->length
!ll&&!rl&&r
000
MC/DC0%0/2
$e->is_empty and $e->children->length
$e->is_empty $e->children->length
Truth table0%0/3
A and B
A= $e->is_emptyB= $e->children->length
ABresult
0X0
100
111
784 {
785 0 printf( STDOUT "Tag \"%s\" should be empty (void), but it has %d children.\n", $e->tag, $e->children->length );
786 }
787 ▶ elsif( $e->is_empty && !$e->attributes->exists( '/' ) )
Condition0%0/3
$e->is_empty and not $e->attributes->exists("/")
!ll&&!rl&&r
000
MC/DC0%0/2
$e->is_empty and not $e->attributes->exists("/")
$e->is_empty not $e->attributes->exists("/")
Truth table0%0/3
A and not(B)
A= $e->is_emptyB= not $e->attributes->exists("/")
ABresult
0X0
100
111
788 {
789 0 printf( STDOUT "Tag \"%s\" at line %d at row %d is an empty (void) tag, but it did not end with />\n", $e->tag, $e->line, $e->column );
790 }
791 ▶ elsif( !$e->is_empty && $e->attributes->exists( '/' ) )
Condition0%0/3
not $e->is_empty and $e->attributes->exists("/")
!ll&&!rl&&r
000
MC/DC0%0/2
not $e->is_empty and $e->attributes->exists("/")
not $e->is_empty $e->attributes->exists("/")
Truth table0%0/3
not(A) and B
A= not $e->is_emptyB= $e->attributes->exists("/")
ABresult
0X0
100
111
792 {
793 0 printf( STDOUT "Tag \"%s\" at line %d at row %d is marked as non-empty (non-void), but it ends with />\n", $e->tag, $e->line, $e->column );
794 }
795 ▶ elsif( !$e->is_empty && !$e->is_closed )
Condition0%0/3
$e->is_empty || $e->is_closed
l!l&&r!l&&!r
000
MC/DC0%0/2
$e->is_empty || $e->is_closed
$e->is_empty $e->is_closed
Truth table0%0/3
A || B
A= $e->is_emptyB= $e->is_closed
ABresult
1X1
011
000
796 {
797 0 my $def = $self->get_definition( $e->tag );
798 0 ▶ if( !$def->{is_empty} )
Branch: 1 branch
BranchTrueFalseTotal
if (not $def->{'is_empty'}) { } 0 0 0
799 {
800 0 printf( STDOUT "Tag \"%s\" at line %d at row %d is an enclosing tag, but it has not been closed.\n", $e->tag, $e->line, $e->column );
801 }
802 else
803 {
804 0 printf( STDOUT "Tag \"%s\" at line %d at row %d is an empty (void) tag, but it did not end with />\n", $e->tag, $e->line, $e->column );
805 }
806 }
807 0 ▶ $self->sanity_check( $e ) if( !$e->is_empty );
Branch: 1 branch
BranchTrueFalseTotal
if not $e->is_empty 0 0 0
808 0 });
809 0 return( $self );
810 }
811  
812 sub set_dom
813 {
814 0 ▶ my( $this, $html ) = @_;
Subroutine set_dom: not called CC=5 SCAR=34.0
Pod documented
815 0 ▶ if( defined( $html ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $html) 0 0 0
816 {
817 0 ▶ if( Scalar::Util::blessed( $html ) && $html->isa( 'HTML::Object::Document' ) )
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::blessed($html) and $html->isa('HTML::Object::Document')) { } 0 0 0
elsif (length $html) { } 0 0 0
Condition0%0/3
builtin::blessed($html) and $html->isa("HTML::Object::Document")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($html) and $html->isa("HTML::Object::Document")
builtin::blessed($html) $html->isa("HTML::Object::Document")
Truth table0%0/3
A and B
A= builtin::blessed($html)B= $html->isa("HTML::Object::Document")
ABresult
0X0
100
111
818 {
819 0 $GLOBAL_DOM = $html;
820 }
821 elsif( CORE::length( $html ) )
822 {
823 0 $GLOBAL_DOM = $this->new->parse( $html );
824 }
825 }
826 0 return( $this );
827 }
828  
829 sub _set_state
830 {
831 219 ▶ my $self = shift( @_ );
Subroutine _set_state: called CC=9 SCAR=22.3
832 219 my( $state, $elem ) = @_;
833 # This feature is only applicable for HTML::Object::DOM
834 219 ▶ return( $self ) unless( $self->isa( 'HTML::Object::DOM' ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->isa("HTML::Object::DOM") 24 195 219
835 # ... and only for documents
836 195 ▶ return if( !defined( $elem ) || !$self->_is_a( $elem => 'HTML::Object::DOM::Document' ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $elem and $self->_is_a($elem, "HTML::Object::DOM::Document") 0 195 195
Condition33%1/3
defined $elem and $self->_is_a($elem, "HTML::Object::DOM::Document")
!ll&&!rl&&r
00195
MC/DC0%0/2
defined $elem and $self->_is_a($elem, "HTML::Object::DOM::Document")
defined $elem $self->_is_a($elem, "HTML::Object::DOM::Document")
Truth table33%1/3
A and B
A= defined $elemB= $self->_is_a($elem, "HTML::Object::DOM::Document")
ABresult
0X0
100
111
837 195 $elem->readyState( $state );
838 195 require HTML::Object::Event;
839 195 my $event = HTML::Object::Event->new( 'readystate',
840 bubbles => 0,
841 cancelable => 0,
842 detail => { 'state' => $state, document => $elem },
843 target => $elem,
844 );
845 # $elem->dispatchEvent( $event );
846 195 ▶ if( my $eh = $elem->onreadystatechange )
Branch: 1 branch
BranchTrueFalseTotal
if (my $eh = $elem->onreadystatechange) 3 192 195
847 {
848 3 local $_ = $elem;
849 3 my $code = $eh->code;
850 3 ▶ warn( "Value for event handler '$code' is not a code reference.\n" ) if( ref( $code ) ne 'CODE' );
Branch: 1 branch
BranchTrueFalseTotal
if ref $code ne "CODE" 0 3 3
851 3 ▶ $code->( $event ) if( ref( $code ) eq 'CODE' );
Branch: 1 branch
BranchTrueFalseTotal
if ref $code eq "CODE" 3 0 3
852 }
853 195 ▶ if( $state eq 'complete' && ( my $code = $elem->onload ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($state eq "complete" and my $code = $elem->onload) 3 192 195
Condition100%3/3
$state eq "complete" and my $code = $elem->onload
!ll&&!rl&&r
130623
MC/DC100%2/2
$state eq "complete" and my $code = $elem->onload
$state eq "complete" my $code = $elem->onload
Truth table100%3/3
A and B
A= $state eq "complete"B= my $code = $elem->onload
ABresult
0X0
100
111
854 {
855 3 local $_ = $elem;
856 3 $code->( $event );
857 }
858 195 return( $self );
859 }
860  
861 36 1;
862 # NOTE: POD
863 __END__
« blib/lib/HTML/Object/DOM/NodeIteratorShared.pm ↑ Summary lib/HTML/Object/DOM/Element.pm »