lib/HTML/Object.pm

filter: Statementstmt 77.1% 328 / 425 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.5% 556 / 918 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/HTML/Object.pm
3 ## Version v0.6.1
4 ## Copyright(c) 2026 DEGUEST Pte. Ltd.
5 ## Author: Jacques Deguest <jack@deguest.jp>
6 ## Created 2021/04/20
7 ## Modified 2026/03/29
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 33 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 33 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 33 ▶ use warnings::register;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 33 ▶ use parent qw( Module::Generic );
Subroutine BEGIN: called CC=1 SCAR=0.0
21 33 ▶ use vars qw( $DICT $LINK_ELEMENTS $FATAL_ERROR $GLOBAL_DOM $VERSION );
Subroutine BEGIN: called CC=1 SCAR=0.0
22 33 ▶ use curry;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 33 ▶ use Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
24 33 ▶ use Filter::Util::Call;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 33 ▶ use HTML::Object::Closing;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 33 ▶ use HTML::Object::Comment;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 33 ▶ use HTML::Object::Declaration;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 33 ▶ use HTML::Object::Document;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 33 ▶ use HTML::Object::Element;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 33 ▶ use HTML::Object::Space;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 33 ▶ use HTML::Object::Text;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 33 ▶ use HTML::Parser;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 33 ▶ use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 33 ▶ use Module::Generic::File qw( file );
Subroutine BEGIN: called CC=1 SCAR=0.0
35 33 ▶ use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
36 33 ▶ our $VERSION = 'v0.6.1';
Subroutine BEGIN: called CC=1 SCAR=0.0
37 33 our $DICT = {};
38 33 our $LINK_ELEMENTS = {};
39 33 our $FATAL_ERROR = 0;
40 };
41  
42 33 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 33 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
44  
45 {
46 33 my $me = file( __FILE__ );
47 33 my $path = $me->parent;
48 33 my $dict_json = 'html_tags_dict.json';
49 33 my $tags_repo = $path->child( $dict_json );
50 33 ▶ if( $tags_repo->exists )
Branch: 1 branch
BranchTrueFalseTotal
if ($tags_repo->exists) { } 33 0 33
51 {
52 # try-catch
53 33 local $@;
54 eval
55 33 {
56 33 ▶ 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, "\n")
l!l
330
MC/DC0%0/1
$tags_repo->load_utf8 || die(qq[Unable to open html tags json dictionary "$tags_repo": ], $tags_repo->error, "\n")
$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, "\n" );
58 33 my $j = JSON->new->relaxed;
59 33 my $hash = $j->decode( $json );
60 33 ▶ 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 33 33
61 33 $DICT = $hash->{dict};
62 33 for( keys( %$DICT ) )
63 {
64 6798 ▶ if( exists( $DICT->{ $_ }->{link_in} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $HTML::Object::DICT->{$_}{"link_in"}) 858 5940 6798
65 {
66 858 $LINK_ELEMENTS->{ $_ } = $DICT->{ $_ }->{link_in};
67 }
68 }
69 };
70 33 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 33 33
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 50 ▶ my $class = shift( @_ );
Subroutine import: called CC=20 SCAR=34.0
84 50 my $hash = {};
85 50 ▶ for( my $i = 0; $i < scalar( @_ ); $i++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @_) 15 50 65
86 {
87 15 ▶ 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") 5 4 9
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
654
$_[$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
609
$_[$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
069
$_[$i] eq "debug" or $_[$i] eq "debug_code" or $_[$i] eq "debug_file" or $_[$i] eq "fatal_error"
l!l&&r!l&&!r
0015
$_[$i] eq "debug" or $_[$i] eq "debug_code" or $_[$i] eq "debug_file"
l!l&&r!l&&!r
0015
$_[$i] eq "debug" or $_[$i] eq "debug_code"
l!l&&r!l&&!r
0015
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 11 $hash->{ $_[$i] } = $_[$i+1];
96 11 CORE::splice( @_, $i, 2 );
97 11 $i--;
98 }
99 }
100
101 {
102 50 local $Exporter::ExportLevel = 1;
103 50 Exporter::import( $class, @_ );
104 }
105 50 ▶ $hash->{debug} = 0 if( !CORE::exists( $hash->{debug} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $hash->{"debug"} 50 0 50
106 50 ▶ $hash->{global_dom} = 0 if( !CORE::exists( $hash->{global_dom} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $hash->{"global_dom"} 44 6 50
107 50 ▶ $hash->{debug_code} = 0 if( !CORE::exists( $hash->{debug_code} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $hash->{"debug_code"} 50 0 50
108 50 ▶ $hash->{fatal_error} = 0 if( !CORE::exists( $hash->{fatal_error} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $hash->{"fatal_error"} 50 0 50
109 50 ▶ $hash->{try_catch} = 0 if( !CORE::exists( $hash->{try_catch} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $hash->{"try_catch"} 50 0 50
110 50 ▶ if( $hash->{fatal_error} )
Branch: 1 branch
BranchTrueFalseTotal
if ($hash->{"fatal_error"}) 0 50 50
111 {
112 0 $FATAL_ERROR = 1;
113 }
114
115 50 ▶ if( $hash->{try_catch} )
Branch: 1 branch
BranchTrueFalseTotal
if ($hash->{"try_catch"}) 0 50 50
116 {
117 0 require Nice::Try;
118 0 Nice::Try->export_to_level( 1, @_ );
119 }
120
121 50 ▶ if( $hash->{global_dom} || $hash->{xquery} )
Branch: 1 branch
BranchTrueFalseTotal
if ($hash->{"global_dom"} or $hash->{"xquery"}) 6 44 50
Condition66%2/3
$hash->{"global_dom"} or $hash->{"xquery"}
l!l&&r!l&&!r
6044
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 6 ▶ Filter::Util::Call::filter_add( bless( $hash => ( ref( $class ) || $class ) ) );
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
060
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 6 require HTML::Object::XQuery;
125 6 HTML::Object::XQuery->export_to_level( 1, @_ );
126 6 ▶ if( $hash->{global_dom} )
Branch: 1 branch
BranchTrueFalseTotal
if ($hash->{"global_dom"}) 6 0 6
127 {
128 # Same as Firefox, Chrome or Safari do: default dom for blank page
129 6 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 6 ▶ my( $self ) = @_ ;
Subroutine filter: called CC=8 SCAR=24.2
Pod documented
139 6 my( $status, $last_line );
140 6 my $line = 0;
141 6 my $code = '';
142 6 ▶ if( !$self->{global_dom} )
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"global_dom"}) 0 6 6
143 {
144 0 Filter::Util::Call::filter_del();
145 0 $status = 1;
146 0 return( $status );
147 }
148 6 ▶ while( $status = Filter::Util::Call::filter_read() )
Branch: 1 branch
BranchTrueFalseTotal
if ($status = &Filter::Util::Call::filter_read()) 1035 6 1041
149 {
150 1035 ▶ return( $status ) if( $status < 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $status < 0 0 1035 1035
151 1035 $line++;
152 1035 ▶ if( /^__(?:DATA|END)__/ )
Branch: 1 branch
BranchTrueFalseTotal
if (/^__(?:DATA|END)__/) 0 1035 1035
153 {
154 0 last;
155 }
156
157 s{
158 (?<!\\)\$\(
159 }
160 {
161 36 "xq("
162 1035 }gexs;
163 1035 ▶ if( $self->{xquery} )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"xquery"}) 965 70 1035
164 {
165 965 s{
166 (?<!\\)\$\.([a-zA-Z]+)
167 }
168 {
169 69 "xQuery->$1"
170 }gexs;
171 }
172 }
173 6 ▶ if( $self->{debug_file} )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"debug_file"}) 0 6 6
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 6 return( $line );
183 }
184  
185 sub init
186 {
187 97 ▶ my $self = shift( @_ );
Subroutine init: called CC=3 SCAR=11.1
Pod documented
188 97 $self->{_init_strict_use_sub} = 1;
189 97 ▶ $self->{_exception_class} = 'HTML::Object::Exception' unless( CORE::exists( $self->{_exception_class} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"_exception_class"} 97 0 97
190 97 ▶ $self->SUPER::init( @_ ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->SUPER::init(@_) or return $self->pass_error 0 97 97
191 97 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 97 $self->{document} = '';
205 97 $self->{current_parent} = '';
206 97 $self->{_parser} = $p;
207 97 $self->{_elems} = [];
208 97 return( $self );
209 }
210  
211 sub add_comment
212 {
213 7 ▶ my $self = shift( @_ );
Subroutine add_comment: called CC=2 SCAR=6.9
Pod documented
214 7 my @args = @_;
215 7 my $opts = {};
216 7 my @p = qw( p raw col line offset offset_end );
217 7 @$opts{ @p } = @args;
218 7 my $parent = $self->current_parent;
219 7 my $val = $opts->{raw};
220 7 $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 7 ▶ 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
70
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 7 $parent->children->push( $e );
231 7 return( $e );
232 }
233  
234 sub add_declaration
235 {
236 28 ▶ my $self = shift( @_ );
Subroutine add_declaration: called CC=2 SCAR=6.9
Pod documented
237 28 my @args = @_;
238 28 my $opts = {};
239 28 my @p = qw( p raw col line offset offset_end );
240 28 @$opts{ @p } = @args;
241 28 my $parent = $self->current_parent;
242 28 ▶ return if( !$self->_is_a( $parent => 'HTML::Object::DOM::Document' ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_a($parent, "HTML::Object::DOM::Document") 1 27 28
243 my $e = $self->new_declaration({
244 column => $opts->{col},
245 line => $opts->{line},
246 offset => $opts->{offset},
247 original => $opts->{raw},
248 27 parent => $parent,
249 debug => $self->debug,
250 });
251 # $parent->children->push( $e );
252 27 $self->document->declaration( $e );
253 27 $parent->children->push( $e );
254 27 return( $e );
255 }
256  
257 sub add_default
258 {
259 142 ▶ my $self = shift( @_ );
Subroutine add_default: called CC=7 SCAR=33.0
Pod documented
260 142 my @args = @_;
261 142 my $opts = {};
262 142 my @p = qw( p tag attr seq raw col line offset offset_end );
263 142 @$opts{ @p } = @args;
264 142 ▶ return if( !CORE::length( $opts->{raw} ) && !defined( $opts->{tag} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless length $opts->{"raw"} or defined $opts->{"tag"} 142 0 142
Condition33%1/3
length $opts->{"raw"} or defined $opts->{"tag"}
l!l&&r!l&&!r
00142
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 302 ▶ my $self = shift( @_ );
Subroutine add_end: called CC=4 SCAR=14.6
Pod documented
312 302 my @args = @_;
313 302 my $opts = {};
314 302 my @p = qw( p tag attr seq raw col line offset offset_end );
315 302 @$opts{ @p } = @args;
316 302 my $me = $self->current_parent;
317 302 my $parent = $me->parent;
318 302 ▶ if( $opts->{tag} ne $me->tag )
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'tag'} ne $me->tag) { } 0 302 302
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 302 ▶ 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
3020
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 302 $me->is_closed(1);
335 302 $me->close_tag( $e );
336 # $parent->children->push( $e );
337 302 $self->current_parent( $parent );
338 }
339 }
340  
341 sub add_space
342 {
343 481 ▶ my $self = shift( @_ );
Subroutine add_space: called CC=2 SCAR=7.0
Pod documented
344 481 my $opts = $self->_get_args_as_hash( @_ );
345 481 my $parent = $self->current_parent;
346 481 ▶ my $e = $self->new_space( $opts ) || return( $self->pass_error );
Condition50%1/2
$self->new_space($opts) || (return $self->pass_error)
l!l
4810
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 481 $parent->children->push( $e );
348 481 return( $e );
349 }
350  
351 sub add_start
352 {
353 421 ▶ my $self = shift( @_ );
Subroutine add_start: called CC=10 SCAR=23.7
Pod documented
354 421 my @args = @_;
355 421 my $opts = {};
356 421 my @p = qw( p tag attr seq raw col line offset offset_end );
357 421 @$opts{ @p } = @args;
358 421 my $parent = $self->current_parent;
359 421 ▶ if( $opts->{tag} =~ s,/,, )
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"tag"} =~ s[/][]) 1 420 421
360 {
361 1 $opts->{attr}->{'/'} = '/';
362 }
363 421 my $def = $self->get_definition( $opts->{tag} );
364 # Make some easy guess
365 421 ▶ if( !scalar( keys( %$def ) ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar keys %$def) 0 421 421
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 421 ▶ $def->{is_empty} = 0 unless( CORE::exists( $def->{is_empty} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $def->{"is_empty"} 0 421 421
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 421 ▶ $def->{class} //= '';
Condition100%2/2
$def->{"class"} //= ""
l!l
38536
MC/DC100%1/1
$def->{"class"} //= ""
$def->{"class"}
Truth table100%2/2
A
A= $def->{"class"}
Aresult
11
00
388 421 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 421 debug => $self->debug,
402 };
403
404 # If this tag is handled by a special class, instantiate the object by this class
405 421 ▶ if( $def->{class} )
Branch: 1 branch
BranchTrueFalseTotal
if ($def->{'class'}) { } 363 58 421
406 {
407 363 ▶ $e = $self->new_special( $def->{class} => $params ) || return;
Condition50%1/2
$self->new_special($def->{'class'}, $params) || (return)
l!l
3630
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 58 ▶ $e = $self->new_element( $params ) || return;
Condition50%1/2
$self->new_element($params) || (return)
l!l
580
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 421 $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 421 ▶ if( !$def->{is_empty} )
Branch: 1 branch
BranchTrueFalseTotal
unless ($def->{"is_empty"}) 308 113 421
416 {
417 308 $self->current_parent( $e );
418 }
419 421 return( $e );
420 }
421  
422 sub add_text
423 {
424 616 ▶ my $self = shift( @_ );
Subroutine add_text: called CC=5 SCAR=16.3
Pod documented
425 616 my @args = @_;
426 616 my $opts = {};
427 616 my @p = qw( p raw col line offset offset_end );
428 616 @$opts{ @p } = @args;
429 616 ▶ 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
6160
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 616 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 616 ▶ if( $opts->{raw} =~ /^[[:blank:]\h\v]*$/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'raw'} =~ /^[[:blank:]\h\v]*$/) { } 481 135 616
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 481 ▶ 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
4810
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 135 ▶ 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
1350
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 135 $parent->children->push( $e );
460 }
461 616 return( $e );
462 }
463  
464 239 ▶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 13 ▶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 442 ▶ my $self = shift( @_ );
Subroutine get_definition: called CC=3 SCAR=11.2
Pod documented
473 442 my $tag = shift( @_ );
474 442 ▶ return( $self->error( "No tag was provided to get its definition." ) ) if( !length( $tag ) );
Branch: 1 branch
BranchTrueFalseTotal
unless length $tag 0 442 442
475 # Just to be sure
476 442 $tag = lc( $tag );
477 442 ▶ return( {} ) if( !exists( $DICT->{ $tag } ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $HTML::Object::DICT->{$tag} 0 442 442
478 442 return( $DICT->{ $tag } );
479 }
480  
481 sub new_closing
482 {
483 31 ▶ my $self = shift( @_ );
Subroutine new_closing: called CC=2 SCAR=7.1
Pod documented
484 31 ▶ my $e = HTML::Object::Closing->new( @_ ) ||
Condition50%1/2
'HTML::Object::Closing'->new(@_) || (return $self->pass_error('HTML::Object::Closing'->error))
l!l
310
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 31 return( $e );
487 }
488  
489 sub new_comment
490 {
491 1 ▶ my $self = shift( @_ );
Subroutine new_comment: called CC=2 SCAR=7.1
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 return( $e );
495 }
496  
497 sub new_declaration
498 {
499 0 ▶ my $self = shift( @_ );
Subroutine new_declaration: not called CC=2 SCAR=17.9
Pod documented
500 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
501 return( $self->pass_error( HTML::Object::Declaration->error ) );
502 0 return( $e );
503 }
504  
505 sub new_document
506 {
507 7 ▶ my $self = shift( @_ );
Subroutine new_document: called CC=2 SCAR=7.1
Pod documented
508 7 ▶ my $e = HTML::Object::Document->new( @_ ) ||
Condition50%1/2
'HTML::Object::Document'->new(@_) || (return $self->pass_error('HTML::Object::Document'->error))
l!l
70
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
509 return( $self->pass_error( HTML::Object::Document->error ) );
510 7 return( $e );
511 }
512  
513 sub new_element
514 {
515 47 ▶ my $self = shift( @_ );
Subroutine new_element: called CC=2 SCAR=7.1
Pod documented
516 47 ▶ my $e = HTML::Object::Element->new( @_ ) ||
Condition50%1/2
'HTML::Object::Element'->new(@_) || (return $self->pass_error('HTML::Object::Element'->error))
l!l
470
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
517 return( $self->pass_error( HTML::Object::Element->error ) );
518 47 return( $e );
519 }
520  
521 sub new_space
522 {
523 41 ▶ my $self = shift( @_ );
Subroutine new_space: called CC=2 SCAR=7.1
Pod documented
524 41 ▶ my $e = HTML::Object::Space->new( @_ ) ||
Condition50%1/2
'HTML::Object::Space'->new(@_) || (return $self->pass_error('HTML::Object::Space'->error))
l!l
410
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
525 return( $self->pass_error( HTML::Object::Space->error ) );
526 41 return( $e );
527 }
528  
529 sub new_special
530 {
531 377 ▶ my $self = shift( @_ );
Subroutine new_special: called CC=3 SCAR=11.3
Pod documented
532 377 my $class = shift( @_ );
533 377 ▶ $self->_load_class( $class ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->_load_class($class) or return $self->pass_error 0 377 377
534 377 ▶ my $e = $class->new( @_ ) || return( $self->pass_error( $class->error ) );
Condition50%1/2
$class->new(@_) || (return $self->pass_error($class->error))
l!l
3770
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
535 377 return( $e );
536 }
537  
538 sub new_text
539 {
540 8 ▶ my $self = shift( @_ );
Subroutine new_text: called CC=2 SCAR=7.1
Pod documented
541 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
542 return( $self->pass_error( HTML::Object::Text->error ) );
543 8 return( $e );
544 }
545  
546 sub parse
547 {
548 20 ▶ my $self = shift( @_ );
Subroutine parse: called CC=6 SCAR=19.4
Pod documented
549 20 my $this = shift( @_ );
550 20 my $opts = $self->_get_args_as_hash( @_ );
551 20 ▶ 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) { } 19 1 20
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
1901
ref $this eq "CODE" or ref $this eq "GLOB" or "$this" =~ /<\w+/
l!l&&r!l&&!r
0191
ref $this eq "CODE" or ref $this eq "GLOB"
l!l&&r!l&&!r
0020
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
552 {
553 19 return( $self->parse_data( $this, $opts ) );
554 }
555 elsif( ref( $this ) )
556 {
557 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." ) );
558 }
559 else
560 {
561 1 return( $self->parse_file( $this, $opts ) );
562 }
563 }
564  
565 sub parse_data
566 {
567 67 ▶ my $self = shift( @_ );
Subroutine parse_data: called CC=8 SCAR=21.2
Pod documented
568 67 my $html = shift( @_ );
569 67 my $opts = $self->_get_args_as_hash( @_ );
570 67 $html = "$html";
571 # try-catch
572 67 local $@;
573 eval
574 67 {
575 67 ▶ if( $opts->{utf8} )
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"utf8"}) 0 67 67
576 {
577 0 $html = Encode::decode( 'utf8', $html, Encode::FB_CROAK );
578 }
579 };
580 67 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 67 67
581 {
582 0 return( $self->error( "Error found while utf8 decoding ", length( $html ), " bytes of html data provided: $@" ) );
583 }
584
585 67 my $e;
586 67 ▶ 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 67 67
Condition33%1/3
length $self->{"current_parent"} and $self->_is_object($self->{"current_parent"})
!ll&&!rl&&r
6700
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
587 {
588 0 $e = $self->current_parent;
589 }
590 else
591 {
592 67 $e = $self->new_document( debug => $self->debug );
593 67 $self->document( $e );
594 67 $self->current_parent( $e );
595 67 ▶ if( $self->isa( 'HTML::Object::DOM' ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->isa("HTML::Object::DOM")) 61 6 67
596 {
597 61 ▶ if( my $code = $self->onload )
Branch: 1 branch
BranchTrueFalseTotal
if (my $code = $self->onload) 2 59 61
598 {
599 2 $e->onload( $code );
600 }
601 61 ▶ if( my $code = $self->onreadystatechange )
Branch: 1 branch
BranchTrueFalseTotal
if (my $code = $self->onreadystatechange) 1 60 61
602 {
603 1 $e->onreadystatechange( $code );
604 }
605 }
606 }
607 67 my $doc = $self->document;
608 67 my $p = $self->parser;
609 67 $self->_set_state( 'loading' => $doc );
610 67 $p->parse( $html );
611 67 $self->_set_state( 'interactive' => $doc );
612 67 $self->post_process( $e );
613 67 $self->_set_state( 'complete' => $doc );
614 67 $p->eof;
615 67 return( $e );
616 }
617  
618 sub parse_file
619 {
620 4 ▶ my $self = shift( @_ );
Subroutine parse_file: called CC=10 SCAR=24.8
Pod documented
621 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
622 4 my $opts = $self->_get_args_as_hash( @_ );
623 # On, by default, if not provided
624 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
625 4 my $f = $self->new_file( $file );
626 4 ▶ if( !$f->exists )
Branch: 2 branches
BranchTrueFalseTotal
if (not $f->exists) { } 0 4 4
elsif ($f->is_empty) { } 0 4 4
627 {
628 0 return( $self->error( "File to parse \"$file\" does not exist." ) );
629 }
630 elsif( $f->is_empty )
631 {
632 0 return( $self->error( "File to parse \"$file\" is empty." ) );
633 }
634 4 my $params = {};
635 4 ▶ $params->{binmode} = 'utf8' if( $opts->{utf8} );
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"utf8"} 4 0 4
636 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
637 return( $self->error( "Unable to open file to parse \"$file\": ", $f->error ) );
638 4 my $e = $self->new_document( _last_modified => $f->mtime );
639 4 $self->document( $e );
640 4 ▶ if( $self->isa( 'HTML::Object::DOM' ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->isa("HTML::Object::DOM")) 3 1 4
641 {
642 3 ▶ if( my $code = $self->onload )
Branch: 1 branch
BranchTrueFalseTotal
if (my $code = $self->onload) 0 3 3
643 {
644 0 $e->onload( $code );
645 }
646 3 ▶ if( my $code = $self->onreadystatechange )
Branch: 1 branch
BranchTrueFalseTotal
if (my $code = $self->onreadystatechange) 0 3 3
647 {
648 0 $e->onreadystatechange( $code );
649 }
650 }
651 4 $self->current_parent( $e );
652 4 $self->_set_state( 'loading' => $e );
653 4 my $p = $self->parser;
654 4 $p->parse_file( $io );
655 4 $io->close;
656 4 $self->_set_state( 'interactive' => $e );
657 4 $self->post_process( $e );
658 4 $self->_set_state( 'complete' => $e );
659 4 return( $e );
660 }
661  
662 sub parse_url
663 {
664 0 ▶ my $self = shift( @_ );
Subroutine parse_url: not called CC=20 SCAR=60.4
Pod documented
665 0 my $uri;
666 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
667 ( scalar( @_ ) > 1 &&
668 (
669 ( @_ % 2 ) ||
670 ( scalar( @_ ) == 2 && ref( $_[1] ) eq 'HASH' )
671 )
672 ) )
673 {
674 0 $uri = shift( @_ );
675 }
676 0 my $opts = $self->_get_args_as_hash( @_ );
677 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
678 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
679 {
680 0 return( $self->error( "LWP::UserAgent version 6.49 or higher is required to use load()" ) );
681 }
682 0 ▶ if( !$self->_load_class( 'URI', { version => '1.74' } ) )
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_load_class("URI", {"version", "1.74"})) 0 0 0
683 {
684 0 return( $self->error( "URI version 1.74 or higher is required to use load()" ) );
685 }
686 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
687 # try-catch
688 0 local $@;
689 eval
690 0 {
691 0 $uri = URI->new( "$uri" );
692 };
693 0 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
694 {
695 0 return( $self->error( "Bad url provided \"$uri\": $@" ) );
696 }
697
698 0 my $content;
699 my $resp;
700 # try-catch
701 eval
702 0 {
703 my $ua = LWP::UserAgent->new(
704 agent => "HTML::Object/$VERSION",
705 timeout => $opts->{timeout},
706 0 );
707 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
708 0 $content = $resp->decoded_content;
709 };
710 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
711 {
712 0 return( $self->error({
713 code => $resp->code,
714 message => $resp->message,
715 }) );
716 }
717 0 $self->response( $resp );
718 0 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
719 {
720 0 return( $self->error( "Error making a GET request to $uri: $@" ) );
721 }
722 0 my $doc = $self->parse_data( $content );
723 0 $doc->uri( $uri );
724 0 return( $doc );
725 }
726  
727 71 ▶sub parser { return( shift->_set_get_object_without_init( '_parser', 'HTML::Parser', @_ ) ); }
Subroutine parser: called CC=1 SCAR=0.0
Pod documented
728  
729 sub post_process
730 {
731 373 ▶ my $self = shift( @_ );
Subroutine post_process: called CC=3 SCAR=11.6
Pod documented
732 373 my $elem = shift( @_ );
733 373 ▶ return if( !$self->_is_object( $elem ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_object($elem) 0 373 373
734 373 ▶ return if( !$elem->isa( 'HTML::Object::Element' ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $elem->isa("HTML::Object::Element") 0 373 373
735 # Crawl through the tree and look for unclosed tags
736 $elem->children->foreach(sub
737 {
738 1035 ▶ my $e = shift( @_ );
Subroutine __ANON__: called CC=11 SCAR=26.6
739 1035 ▶ 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 "_" 614 421 1035
Condition66%2/3
$e->isa("HTML::Object::Closing") or $e->tag->substr(0, 1) eq "_"
l!l&&r!l&&!r
0614421
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
740 421 ▶ if( $e->is_empty && $e->children->length )
Branch: 3 branches
BranchTrueFalseTotal
if ($e->is_empty and $e->children->length) { } 0 421 421
elsif ($e->is_empty and not $e->attributes->exists('/')) { } 8 413 421
elsif (not $e->is_empty || $e->is_closed) { } 0 413 413
Condition66%2/3
$e->is_empty and $e->children->length
!ll&&!rl&&r
3021190
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
741 {
742 # It should be empty, but has some children
743 }
744 ▶ elsif( $e->is_empty && !$e->attributes->exists( '/' ) )
Condition100%3/3
$e->is_empty and not $e->attributes->exists("/")
!ll&&!rl&&r
3021118
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
745 {
746 # Is empty, but does not end with />
747 }
748 ▶ elsif( !$e->is_empty && !$e->is_closed )
Condition66%2/3
$e->is_empty || $e->is_closed
l!l&&r!l&&!r
1113020
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
749 {
750 0 my $def = $self->get_definition( $e->tag );
751 0 ▶ if( !$def->{is_empty} )
Branch: 1 branch
BranchTrueFalseTotal
if (not $def->{'is_empty'}) { } 0 0 0
752 {
753 # Is an enclosing tag, but it has not been closed
754 }
755 else
756 {
757 # Is an empty (void) tag, but it did not end with /
758 }
759 }
760 421 ▶ $self->post_process( $e ) if( !$e->is_empty );
Branch: 1 branch
BranchTrueFalseTotal
if not $e->is_empty 302 119 421
761 373 });
762 373 return( $self );
763 }
764  
765 0 ▶sub response { return( shift->_set_get_object_without_init( 'response', 'HTTP::Response', @_ ) ); }
Subroutine response: not called CC=1 SCAR=6.9
Pod documented
766  
767 sub sanity_check
768 {
769 0 ▶ my $self = shift( @_ );
Subroutine sanity_check: not called CC=3 SCAR=24.8
Pod documented
770 0 my $elem = shift( @_ );
771 0 ▶ return if( !$self->_is_object( $elem ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_object($elem) 0 0 0
772 0 ▶ return if( !$elem->isa( 'HTML::Object::Element' ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $elem->isa("HTML::Object::Element") 0 0 0
773 # Crawl through the tree and look for unclosed tags
774 $elem->children->foreach(sub
775 {
776 0 ▶ my $e = shift( @_ );
Subroutine __ANON__: not called CC=13 SCAR=52.0
777 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
778 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
779 {
780 0 printf( STDOUT "Tag \"%s\" should be empty (void), but it has %d children.\n", $e->tag, $e->children->length );
781 }
782 ▶ 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
783 {
784 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 );
785 }
786 ▶ 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
787 {
788 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 );
789 }
790 ▶ 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
791 {
792 0 my $def = $self->get_definition( $e->tag );
793 0 ▶ if( !$def->{is_empty} )
Branch: 1 branch
BranchTrueFalseTotal
if (not $def->{'is_empty'}) { } 0 0 0
794 {
795 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 );
796 }
797 else
798 {
799 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 );
800 }
801 }
802 0 ▶ $self->sanity_check( $e ) if( !$e->is_empty );
Branch: 1 branch
BranchTrueFalseTotal
if not $e->is_empty 0 0 0
803 0 });
804 0 return( $self );
805 }
806  
807 sub set_dom
808 {
809 0 ▶ my( $this, $html ) = @_;
Subroutine set_dom: not called CC=5 SCAR=34.0
Pod documented
810 0 ▶ if( defined( $html ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $html) 0 0 0
811 {
812 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
813 {
814 0 $GLOBAL_DOM = $html;
815 }
816 elsif( CORE::length( $html ) )
817 {
818 0 $GLOBAL_DOM = $this->new->parse( $html );
819 }
820 }
821 0 return( $this );
822 }
823  
824 sub _set_state
825 {
826 213 ▶ my $self = shift( @_ );
Subroutine _set_state: called CC=9 SCAR=22.3
827 213 my( $state, $elem ) = @_;
828 # This feature is only applicable for HTML::Object::DOM
829 213 ▶ return( $self ) unless( $self->isa( 'HTML::Object::DOM' ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->isa("HTML::Object::DOM") 21 192 213
830 # ... and only for documents
831 192 ▶ 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 192 192
Condition33%1/3
defined $elem and $self->_is_a($elem, "HTML::Object::DOM::Document")
!ll&&!rl&&r
00192
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
832 192 $elem->readyState( $state );
833 192 require HTML::Object::Event;
834 192 my $event = HTML::Object::Event->new( 'readystate',
835 bubbles => 0,
836 cancelable => 0,
837 detail => { 'state' => $state, document => $elem },
838 target => $elem,
839 );
840 # $elem->dispatchEvent( $event );
841 192 ▶ if( my $eh = $elem->onreadystatechange )
Branch: 1 branch
BranchTrueFalseTotal
if (my $eh = $elem->onreadystatechange) 3 189 192
842 {
843 3 local $_ = $elem;
844 3 my $code = $eh->code;
845 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
846 3 ▶ $code->( $event ) if( ref( $code ) eq 'CODE' );
Branch: 1 branch
BranchTrueFalseTotal
if ref $code eq "CODE" 3 0 3
847 }
848 192 ▶ if( $state eq 'complete' && ( my $code = $elem->onload ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($state eq "complete" and my $code = $elem->onload) 2 190 192
Condition100%3/3
$state eq "complete" and my $code = $elem->onload
!ll&&!rl&&r
128622
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
849 {
850 2 local $_ = $elem;
851 2 $code->( $event );
852 }
853 192 return( $self );
854 }
855  
856 33 1;
857 # NOTE: POD
858 __END__
859  
860 =encoding utf-8
861  
862 =head1 NAME
863  
864 HTML::Object - HTML Parser, Modifier and Query Interface
865  
866 =head1 SYNOPSIS
867  
868 use HTML::Object;
869 my $p = HTML::Object->new( debug => 5 );
870 my $doc = $p->parse( $file, { utf8 => 1 } ) || die( $p->error, "\n" );
871 print $doc->as_string;
872  
873 or, using the HTML DOM implementation same as the Web API:
874  
875 use HTML::Object::DOM global_dom => 1;
876 # then you can also use HTML::Object::XQuery for jQuery like DOM manipulation
877 my $p = HTML::Object::DOM->new;
878 my $doc = $p->parse_data( $some_html ) || die( $p->error, "\n" );
879 $('div.inner')->after( "<p>Test</p>" );
880  
881 You can also enable access to L<xQuery> class functions with the C<$.> prefix, like jQuery, by passing the C<xquery> option:
882  
883 use HTML::Object::DOM xquery => 1;
884 # or
885 # use HTML::Object::DOM global_dom => 1, xquery => 1;
886 $.each( $array_ref, sub
887 {
888 my( $index, $value ) = @_;
889 # Do something
890 });
891  
892 # returns an HTML::Object::DOM::Collection
893 my $divs = $doc->getElementsByTagName( 'div' );
894 my $new = $doc->createElement( 'div' );
895 $new->setAttribute( id => 'newDiv' );
896 $divs->[0]->parent->replaceChild( $new, $divs->[0] );
897 # etc.
898  
899 To enable fatal error and also implement try-catch (using L<Nice::Try>) :
900  
901 use HTML::Object fatal_error => 1, try_catch => 1;
902  
903 =head1 VERSION
904  
905 v0.6.1
906  
907 =head1 DESCRIPTION
908  
909 This module is yet another HTML parser, manipulation and query interface, but probably the most comprehensive one. It uses the C parser from L<HTML::Parser> and has the unique particularity that it does not try to decode the entire html document tree only to re-encode it when printing out its data as string like so many other html parsers out there do. Instead, it modifies only the parts required. The rest is returned exactly as it was found in the HTML. This is faster and safer.
910  
911 This module contains 144 modules to closely implement the HTML standard as documented on L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTML_DOM_API>.
912  
913 It uses an external json data dictionary file of html tags (C<html_tags_dict.json>).
914  
915 There are 3 ways to manipulate and query the html data:
916  
917 =over 4
918  
919 =item 1. L<HTML::Object::Element>
920  
921 This is lightweight and simple
922  
923 =item 2. L<HTML::Object::DOM>
924  
925 This is an alternative HTML parser also based on L<HTML::Parser>, and that implements fully the Web API with DOM (Data Object Model), so you can query the HTML with perl equivalent to JavaScript methods of the Web API. It has been designed to be strictly identical to the Web API.
926  
927 =item 3. L<HTML::Object::XQuery>
928  
929 This interface provides a jQuery like API and requires the use of L<HTML::Object::DOM>. However, this is not designed to be a perl implementation of JavaScript, but rather a perl implementation of DOM manipulation methods found in jQuery.
930  
931 =back
932  
933 Note that this interface does not enforce HTML standard. It is up to you the developer to decide what value to use and where the HTML elements should go in the HTML tree and what to do with it.
934  
935 =head1 METHODS
936  
937 =head2 new
938  
939 Instantiate a new L<HTML::Object> object.
940  
941 You need to instantiate a new object prior to parse any new document. It cannot be re-used to parse another document, or if you really wanted to, you would first need to unset L</document> and unset L</current_parent>:
942  
943 $p->document( undef );
944 $p->current_parent( undef );
945  
946 But, it is just as fast to do:
947  
948 $p = HTML::Object->new;
949  
950 =head2 add_comment
951  
952 This is a parser method called that will add a comment to the stack of html elements.
953  
954 =head2 add_declaration
955  
956 This is a parser method called that will add a declaration to the stack of html elements.
957  
958 =head2 add_default
959  
960 This is a parser method called that will add a default html tag to the stack of html elements.
961  
962 =head2 add_end
963  
964 This is a parser method called that will add a closing html tag to the stack of html elements.
965  
966 =head2 add_space
967  
968 This is a parser method called that will add a space to the stack of html elements.
969  
970 =head2 add_start
971  
972 This is a parser method called that will add a starting html tag to the stack of html elements.
973  
974 =head2 add_text
975  
976 This is a parser method called that will add a text to the stack of html elements.
977  
978 =head2 current_parent
979  
980 Sets or gets the current parent, which must be an L<HTML::Object::Element> object or an inheriting class.
981  
982 =head2 dictionary
983  
984 Returns an hash reference containing the HTML tags dictionary. Its structure is:
985  
986 =over 4
987  
988 =item * dict
989  
990 This property reflects an hash containing all the known tags. Each tag has the following possible properties:
991  
992 =over 8
993  
994 =item * description
995  
996 String
997  
998 =item * is_deprecated
999  
1000 Boolean value
1001  
1002 =item * is_empty
1003  
1004 Boolean value
1005  
1006 =item * is_inline
1007  
1008 Boolean value
1009  
1010 =item * is_svg
1011  
1012 Boolean value that describes whether this is a tag dedicated to svg.
1013  
1014 =item * link_in
1015  
1016 Array reference of HTML attributes containing links
1017  
1018 =item * ref
1019  
1020 The reference URL to the online web documentation for this tag.
1021  
1022 =back
1023  
1024 =item * meta
1025  
1026 This property holds an hash reference containing the following meta information:
1027  
1028 =over 8
1029  
1030 =item * author
1031  
1032 String
1033  
1034 =item * updated
1035  
1036 ISO 8601 datetime
1037  
1038 =item * version
1039  
1040 Version number
1041  
1042 =back
1043  
1044 =back
1045  
1046 =head2 document
1047  
1048 Sets or gets the document L<HTML::Object::Document> object.
1049  
1050 =head2 get_definition
1051  
1052 Get the hash definition for a given tag (case does not matter).
1053  
1054 The tags definition is taken from the external file C<html_tags_dict.json> that is provided with this package.
1055  
1056 =head2 new_closing
1057  
1058 Creates and returns a new closing html element L<HTML::Object::Closing>, passing it any arguments provided.
1059  
1060 =head2 new_comment
1061  
1062 Creates and returns a new closing html element L<HTML::Object::Comment>, passing it any arguments provided.
1063  
1064 =head2 new_declaration
1065  
1066 Creates and returns a new closing html element L<HTML::Object::Declaration>, passing it any arguments provided.
1067  
1068 =head2 new_document
1069  
1070 Creates and returns a new closing html element L<HTML::Object::Document>, passing it any arguments provided.
1071  
1072 =head2 new_element
1073  
1074 Creates and returns a new closing html element L<HTML::Object::Element>, passing it any arguments provided.
1075  
1076 =head2 new_space
1077  
1078 Creates and returns a new closing html element L<HTML::Object::Space>, passing it any arguments provided.
1079  
1080 =head2 new_special
1081  
1082 Provided with an HTML tag class name and hash or hash reference of options and this will load that class and instantiate an object passing it the options provided. It returns the object thus Instantiated.
1083  
1084 This is used to instantiate object for special class to handle certain HTML tag, such as C<a>
1085  
1086 =head2 new_text
1087  
1088 Creates and returns a new closing html element L<HTML::Object::Text>, passing it any arguments provided.
1089  
1090 =head2 parse
1091  
1092 Provided with some C<data> (see below), and some options as hash or hash reference and this will parse it and return a new L<HTML::Object::Document> object.
1093  
1094 Possible accepted data are:
1095  
1096 =over 4
1097  
1098 =item I<code>
1099  
1100 L</parse_data> will be called with it.
1101  
1102 =item I<glob>
1103  
1104 L</parse_data> will be called with it.
1105  
1106 =item I<string>
1107  
1108 L</parse_file> will be called with it.
1109  
1110 =back
1111  
1112 Other reference will return an error.
1113  
1114 =head2 parse_data
1115  
1116 Provided with some C<data> and some options as hash or hash reference and this will parse the given data and return a L<HTML::Object::Document> object.
1117  
1118 If the option I<utf8> is provided, the C<data> received will be converted to utf8 using L<Encode/decode>. If an error occurs decoding the data into utf8, the error will be set as an L<Module::Generic::Exception> object and undef will be returned.
1119  
1120 =head2 parse_file
1121  
1122 Provided with a file path and some options as hash or hash reference and this will parse the file.
1123  
1124 If the option I<utf8> is provided, the file will be opened with L<perlfunc/binmode> set to C<utf8>
1125  
1126 It returns a new L<HTML::Object::Document>
1127  
1128 =head2 parse_url
1129  
1130 Provided with an URI supported by L<LWP::UserAgent> and this will issue a GET query and parse the resulting HTML data, and return a new L<HTML::Object::Document> or L<HTML::Object::DOM::Document> depending on which interface you use (either L<HTML::Object> or L<HTML::Object::DOM>.
1131  
1132 If an error occurred, this will set an L<error|Module::Generic/error> and return C<undef>.
1133  
1134 You can get the L<response|HTTP::Response> object with L</response>
1135  
1136 =head2 parser
1137  
1138 Sets or gets a L<HTML::Parser> object.
1139  
1140 =head2 post_process
1141  
1142 Provided with an L<HTML::Object::Element> and this will post process its parsing.
1143  
1144 =head2 response
1145  
1146 Get the latest L<HTTP::Response> object from the HTTP query made using L</parse_url>
1147  
1148 =head2 sanity_check
1149  
1150 Provided with an L<HTML::Object::Element> and this will perform some sanity checks and report the result on C<STDOUT>.
1151  
1152 =head2 set_dom
1153  
1154 Provided with a L<HTML::Object::Document> object and this sets the global variable C<$GLOBAL_DOM>. This is particularly useful when using L<HTML::Object::XQuery> to do things like:
1155  
1156 my $collection = $('div');
1157  
1158 =head1 CREDITS
1159  
1160 Throughout the documentation of this distribution, a lot of descriptions, references and examples have been borrowed from Mozilla. I have also contributed to improving their documentation by fixing bugs and typos on their site.
1161  
1162 =head1 AUTHOR
1163  
1164 Jacques Deguest E<lt>F<jack@deguest.jp>E<gt>
1165  
1166 =head1 SEE ALSO
1167  
1168 L<HTML::Object::DOM>, L<HTML::Object::Element>, L<HTML::Object::XQuery>
1169  
1170 =head1 COPYRIGHT & LICENSE
1171  
1172 Copyright (c) 2021 DEGUEST Pte. Ltd.
1173  
1174 You can use, copy, modify and redistribute this package and associated files under the same terms as Perl itself.
1175  
1176 =cut
« blib/lib/HTML/Object/DOM/NodeIteratorShared.pm ↑ Summary lib/HTML/Object/DOM/Element.pm »