| 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 | use strict; | ||
| 18 | use warnings; | ||
| 19 | use warnings::register; | ||
| 20 | use parent qw( Module::Generic ); | ||
| 21 | use vars qw( $DICT $LINK_ELEMENTS $FATAL_ERROR $GLOBAL_DOM $VERSION ); | ||
| 22 | use curry; | ||
| 23 | use Encode (); | ||
| 24 | use Filter::Util::Call; | ||
| 25 | use HTML::Object::Closing; | ||
| 26 | use HTML::Object::Comment; | ||
| 27 | use HTML::Object::Declaration; | ||
| 28 | use HTML::Object::Document; | ||
| 29 | use HTML::Object::Element; | ||
| 30 | use HTML::Object::Space; | ||
| 31 | use HTML::Object::Text; | ||
| 32 | use HTML::Parser; | ||
| 33 | use JSON; | ||
| 34 | use Module::Generic::File qw( file ); | ||
| 35 | use Scalar::Util (); | ||
| 36 | our $VERSION = 'v0.6.2'; | ||
| 37 | our $DICT = {}; | ||
| 38 | our $LINK_ELEMENTS = {}; | ||
| 39 | our $FATAL_ERROR = 0; | ||
| 40 | }; | ||
| 41 | |||
| 42 | use strict; | ||
| 43 | use warnings; | ||
| 44 | |||
| 45 | { | ||
| 46 | my $me = file( __FILE__ ); | ||
| 47 | my $path = $me->parent; | ||
| 48 | my $dict_json = 'html_tags_dict.json'; | ||
| 49 | my $tags_repo = $path->child( $dict_json ); | ||
| 50 | if( $tags_repo->exists ) | ||
| 51 | { | ||
| 52 | # try-catch | ||
| 53 | local $@; | ||
| 54 | eval | ||
| 55 | { | ||
| 56 | my $json = $tags_repo->load_utf8 || | ||
| 57 | die( "Unable to open html tags json dictionary \"$tags_repo\": ", $tags_repo->error ); | ||
| 58 | my $j = JSON->new->relaxed; | ||
| 59 | my $hash = $j->decode( $json ); | ||
| 60 | die( "No html tags found inside dictionary file \"$tags_repo\"\n" ) if( !scalar( keys( %{$hash->{dict}} ) ) ); | ||
| 61 | $DICT = $hash->{dict}; | ||
| 62 | for( keys( %$DICT ) ) | ||
| 63 | { | ||
| 64 | if( exists( $DICT->{ $_ }->{link_in} ) ) | ||
| 65 | { | ||
| 66 | $LINK_ELEMENTS->{ $_ } = $DICT->{ $_ }->{link_in}; | ||
| 67 | } | ||
| 68 | } | ||
| 69 | }; | ||
| 70 | if( $@ ) | ||
| 71 | { | ||
| 72 | die( "Fatal error occurred while trying to load html tags json dictionary \"$tags_repo\": $@\n" ); | ||
| 73 | } | ||
| 74 | } | ||
| 75 | else | ||
| 76 | { | ||
| 77 | die( "Missing core file \"$dict_json\"\n" ); | ||
| 78 | } | ||
| 79 | } | ||
| 80 | |||
| 81 | sub import | ||
| 82 | { | ||
| 83 | my $class = shift( @_ ); | ||
| 84 | my $hash = {}; | ||
| 85 | for( my $i = 0; $i < scalar( @_ ); $i++ ) | ||
| 86 | { | ||
| 87 | if( $_[$i] eq 'debug' || | ||
| 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 | $hash->{ $_[$i] } = $_[$i+1]; | ||
| 96 | CORE::splice( @_, $i, 2 ); | ||
| 97 | $i--; | ||
| 98 | } | ||
| 99 | } | ||
| 100 | |||
| 101 | { | ||
| 102 | local $Exporter::ExportLevel = 1; | ||
| 103 | Exporter::import( $class, @_ ); | ||
| 104 | } | ||
| 105 | $hash->{debug} = 0 if( !CORE::exists( $hash->{debug} ) ); | ||
| 106 | $hash->{global_dom} = 0 if( !CORE::exists( $hash->{global_dom} ) ); | ||
| 107 | $hash->{debug_code} = 0 if( !CORE::exists( $hash->{debug_code} ) ); | ||
| 108 | $hash->{fatal_error} = 0 if( !CORE::exists( $hash->{fatal_error} ) ); | ||
| 109 | $hash->{try_catch} = 0 if( !CORE::exists( $hash->{try_catch} ) ); | ||
| 110 | if( $hash->{fatal_error} ) | ||
| 111 | { | ||
| 112 | $FATAL_ERROR = 1; | ||
| 113 | } | ||
| 114 | |||
| 115 | if( $hash->{try_catch} ) | ||
| 116 | { | ||
| 117 | require Nice::Try; | ||
| 118 | Nice::Try->export_to_level( 1, @_ ); | ||
| 119 | } | ||
| 120 | |||
| 121 | if( $hash->{global_dom} || $hash->{xquery} ) | ||
| 122 | { | ||
| 123 | Filter::Util::Call::filter_add( bless( $hash => ( ref( $class ) || $class ) ) ); | ||
| 124 | require HTML::Object::XQuery; | ||
| 125 | HTML::Object::XQuery->export_to_level( 1, @_ ); | ||
| 126 | if( $hash->{global_dom} ) | ||
| 127 | { | ||
| 128 | # Same as Firefox, Chrome or Safari do: default dom for blank page | ||
| 129 | 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 | my( $self ) = @_ ; | ||
| 139 | my( $status, $last_line ); | ||
| 140 | my $line = 0; | ||
| 141 | my $code = ''; | ||
| 142 | if( !$self->{global_dom} ) | ||
| 143 | { | ||
| 144 | Filter::Util::Call::filter_del(); | ||
| 145 | $status = 1; | ||
| 146 | return( $status ); | ||
| 147 | } | ||
| 148 | while( $status = Filter::Util::Call::filter_read() ) | ||
| 149 | { | ||
| 150 | return( $status ) if( $status < 0 ); | ||
| 151 | $line++; | ||
| 152 | if( /^__(?:DATA|END)__/ ) | ||
| 153 | { | ||
| 154 | last; | ||
| 155 | } | ||
| 156 | |||
| 157 | s{ | ||
| 158 | (?<!\\)\$\( | ||
| 159 | } | ||
| 160 | { | ||
| 161 | "xq(" | ||
| 162 | }gexs; | ||
| 163 | if( $self->{xquery} ) | ||
| 164 | { | ||
| 165 | s{ | ||
| 166 | (?<!\\)\$\.([a-zA-Z]+) | ||
| 167 | } | ||
| 168 | { | ||
| 169 | "xQuery->$1" | ||
| 170 | }gexs; | ||
| 171 | } | ||
| 172 | } | ||
| 173 | if( $self->{debug_file} ) | ||
| 174 | { | ||
| 175 | if( open( my $fh, ">$self->{debug_file}" ) ) | ||
| 176 | { | ||
| 177 | binmode( $fh, ':utf8' ); | ||
| 178 | print( $fh $_ ); | ||
| 179 | close( $fh ); | ||
| 180 | } | ||
| 181 | } | ||
| 182 | return( $line ); | ||
| 183 | } | ||
| 184 | |||
| 185 | sub init | ||
| 186 | { | ||
| 187 | my $self = shift( @_ ); | ||
| 188 | $self->{_init_strict_use_sub} = 1; | ||
| 189 | $self->{_exception_class} = 'HTML::Object::Exception' unless( CORE::exists( $self->{_exception_class} ) ); | ||
| 190 | $self->SUPER::init( @_ ) || return( $self->pass_error ); | ||
| 191 | 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 | $self->{document} = ''; | ||
| 205 | $self->{current_parent} = ''; | ||
| 206 | $self->{_parser} = $p; | ||
| 207 | $self->{_elems} = []; | ||
| 208 | return( $self ); | ||
| 209 | } | ||
| 210 | |||
| 211 | sub add_comment | ||
| 212 | { | ||
| 213 | my $self = shift( @_ ); | ||
| 214 | my @args = @_; | ||
| 215 | my $opts = {}; | ||
| 216 | my @p = qw( p raw col line offset offset_end ); | ||
| 217 | @$opts{ @p } = @args; | ||
| 218 | my $parent = $self->current_parent; | ||
| 219 | my $val = $opts->{raw}; | ||
| 220 | $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 | parent => $parent, | ||
| 227 | value => $val, | ||
| 228 | debug => $self->debug, | ||
| 229 | }) || return; | ||
| 230 | $parent->children->push( $e ); | ||
| 231 | return( $e ); | ||
| 232 | } | ||
| 233 | |||
| 234 | sub add_declaration | ||
| 235 | { | ||
| 236 | my $self = shift( @_ ); | ||
| 237 | my @args = @_; | ||
| 238 | my $opts = {}; | ||
| 239 | my @p = qw( p raw col line offset offset_end ); | ||
| 240 | @$opts{ @p } = @args; | ||
| 241 | my $parent = $self->current_parent; | ||
| 242 | return if( !$self->_is_a( $parent => 'HTML::Object::DOM::Document' ) ); | ||
| 243 | my $e = $self->new_declaration({ | ||
| 244 | column => $opts->{col}, | ||
| 245 | line => $opts->{line}, | ||
| 246 | offset => $opts->{offset}, | ||
| 247 | original => $opts->{raw}, | ||
| 248 | parent => $parent, | ||
| 249 | debug => $self->debug, | ||
| 250 | }); | ||
| 251 | # $parent->children->push( $e ); | ||
| 252 | $self->document->declaration( $e ); | ||
| 253 | $parent->children->push( $e ); | ||
| 254 | return( $e ); | ||
| 255 | } | ||
| 256 | |||
| 257 | sub add_default | ||
| 258 | { | ||
| 259 | my $self = shift( @_ ); | ||
| 260 | my @args = @_; | ||
| 261 | my $opts = {}; | ||
| 262 | my @p = qw( p tag attr seq raw col line offset offset_end ); | ||
| 263 | @$opts{ @p } = @args; | ||
| 264 | return if( !CORE::length( $opts->{raw} ) && !defined( $opts->{tag} ) ); | ||
| 265 | # Unknown tag, so we check if there is a "/>" to determine if this is an empty (void) tag or not | ||
| 266 | my $attr = $opts->{attr}; | ||
| 267 | my $def = {}; | ||
| 268 | $def->{is_empty} = exists( $attr->{'/'} ) ? 1 : 0; | ||
| 269 | my $parent = $self->current_parent; | ||
| 270 | if( !length( $opts->{tag} ) ) | ||
| 271 | { | ||
| 272 | my @args_keys = qw( p raw col line offset offset_end ); | ||
| 273 | 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 | debug => $self->debug, | ||
| 300 | }) || return; | ||
| 301 | $parent->children->push( $e ); | ||
| 302 | if( !$def->{is_empty} ) | ||
| 303 | { | ||
| 304 | $self->current_parent( $e ); | ||
| 305 | } | ||
| 306 | return( $e ); | ||
| 307 | } | ||
| 308 | |||
| 309 | sub add_end | ||
| 310 | { | ||
| 311 | my $self = shift( @_ ); | ||
| 312 | my @args = @_; | ||
| 313 | my $opts = {}; | ||
| 314 | my @p = qw( p tag attr seq raw col line offset offset_end ); | ||
| 315 | @$opts{ @p } = @args; | ||
| 316 | my $me = $self->current_parent; | ||
| 317 | my $parent = $me->parent; | ||
| 318 | if( $opts->{tag} ne $me->tag ) | ||
| 319 | { | ||
| 320 | 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 ); | ||
| 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 | debug => $self->debug, | ||
| 333 | }) || return; | ||
| 334 | $me->is_closed(1); | ||
| 335 | $me->close_tag( $e ); | ||
| 336 | # $parent->children->push( $e ); | ||
| 337 | $self->current_parent( $parent ); | ||
| 338 | } | ||
| 339 | } | ||
| 340 | |||
| 341 | sub add_space | ||
| 342 | { | ||
| 343 | my $self = shift( @_ ); | ||
| 344 | my $opts = $self->_get_args_as_hash( @_ ); | ||
| 345 | my $parent = $self->current_parent; | ||
| 346 | my $e = $self->new_space( $opts ) || return( $self->pass_error ); | ||
| 347 | $parent->children->push( $e ); | ||
| 348 | return( $e ); | ||
| 349 | } | ||
| 350 | |||
| 351 | sub add_start | ||
| 352 | { | ||
| 353 | my $self = shift( @_ ); | ||
| 354 | my @args = @_; | ||
| 355 | my $opts = {}; | ||
| 356 | my @p = qw( p tag attr seq raw col line offset offset_end ); | ||
| 357 | @$opts{ @p } = @args; | ||
| 358 | my $parent = $self->current_parent; | ||
| 359 | if( $opts->{tag} =~ s,/,, ) | ||
| 360 | { | ||
| 361 | $opts->{attr}->{'/'} = '/'; | ||
| 362 | } | ||
| 363 | my $def = $self->get_definition( $opts->{tag} ); | ||
| 364 | # Make some easy guess | ||
| 365 | if( !scalar( keys( %$def ) ) ) | ||
| 366 | { | ||
| 367 | $def->{is_empty} = 1 if( CORE::exists( $opts->{attr}->{'/'} ) ); | ||
| 368 | # "Return HTMLUnknownElement" | ||
| 369 | # <https://html.spec.whatwg.org/multipage/dom.html#htmlunknownelement> | ||
| 370 | $def->{class} = 'HTML::Object::DOM::Unknown'; | ||
| 371 | } | ||
| 372 | $def->{is_empty} = 0 unless( CORE::exists( $def->{is_empty} ) ); | ||
| 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 | $def->{class} //= ''; | ||
| 388 | 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 | debug => $self->debug, | ||
| 402 | }; | ||
| 403 | |||
| 404 | # If this tag is handled by a special class, instantiate the object by this class | ||
| 405 | if( $def->{class} ) | ||
| 406 | { | ||
| 407 | $e = $self->new_special( $def->{class} => $params ) || return; | ||
| 408 | } | ||
| 409 | else | ||
| 410 | { | ||
| 411 | $e = $self->new_element( $params ) || return; | ||
| 412 | } | ||
| 413 | $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 | if( !$def->{is_empty} ) | ||
| 416 | { | ||
| 417 | $self->current_parent( $e ); | ||
| 418 | } | ||
| 419 | return( $e ); | ||
| 420 | } | ||
| 421 | |||
| 422 | sub add_text | ||
| 423 | { | ||
| 424 | my $self = shift( @_ ); | ||
| 425 | my @args = @_; | ||
| 426 | my $opts = {}; | ||
| 427 | my @p = qw( p raw col line offset offset_end ); | ||
| 428 | @$opts{ @p } = @args; | ||
| 429 | my $parent = $self->current_parent || | ||
| 430 | return( $self->error( "You must create a document first using the new_document() method first before adding text." ) ); | ||
| 431 | 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 | if( $opts->{raw} =~ /^[[:blank:]\h\v]*$/ ) | ||
| 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 | debug => $self->debug, | ||
| 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 | debug => $self->debug, | ||
| 458 | }) || return; | ||
| 459 | $parent->children->push( $e ); | ||
| 460 | } | ||
| 461 | return( $e ); | ||
| 462 | } | ||
| 463 | |||
| 464 | sub current_parent { return( shift->_set_get_object_without_init( 'current_parent', 'HTML::Object::Element', @_ ) ); } | ||
| 465 | |||
| 466 | sub dictionary { return( $DICT ); } | ||
| 467 | |||
| 468 | sub document { return( shift->_set_get_object( 'document', 'HTML::Object::Document', @_ ) ); } | ||
| 469 | |||
| 470 | sub get_definition | ||
| 471 | { | ||
| 472 | my $self = shift( @_ ); | ||
| 473 | my $tag = shift( @_ ); | ||
| 474 | return( $self->error( "No tag was provided to get its definition." ) ) if( !length( $tag ) ); | ||
| 475 | # Just to be sure | ||
| 476 | $tag = lc( $tag ); | ||
| 477 | return( {} ) if( !exists( $DICT->{ $tag } ) ); | ||
| 478 | return( $DICT->{ $tag } ); | ||
| 479 | } | ||
| 480 | |||
| 481 | sub new_closing | ||
| 482 | { | ||
| 483 | my $self = shift( @_ ); | ||
| 484 | my $e = HTML::Object::Closing->new( @_ ) || | ||
| 485 | return( $self->pass_error( HTML::Object::Closing->error ) ); | ||
| 486 | return( $e ); | ||
| 487 | } | ||
| 488 | |||
| 489 | sub new_comment | ||
| 490 | { | ||
| 491 | my $self = shift( @_ ); | ||
| 492 | my $e = HTML::Object::Comment->new( @_ ) || | ||
| 493 | return( $self->pass_error( HTML::Object::Comment->error ) ); | ||
| 494 | $e->debug( $self->debug ); | ||
| 495 | return( $e ); | ||
| 496 | } | ||
| 497 | |||
| 498 | sub new_declaration | ||
| 499 | { | ||
| 500 | my $self = shift( @_ ); | ||
| 501 | my $e = HTML::Object::Declaration->new( @_ ) || | ||
| 502 | return( $self->pass_error( HTML::Object::Declaration->error ) ); | ||
| 503 | $e->debug( $self->debug ); | ||
| 504 | return( $e ); | ||
| 505 | } | ||
| 506 | |||
| 507 | sub new_document | ||
| 508 | { | ||
| 509 | my $self = shift( @_ ); | ||
| 510 | my $e = HTML::Object::Document->new( @_ ) || | ||
| 511 | return( $self->pass_error( HTML::Object::Document->error ) ); | ||
| 512 | $e->debug( $self->debug ); | ||
| 513 | return( $e ); | ||
| 514 | } | ||
| 515 | |||
| 516 | sub new_element | ||
| 517 | { | ||
| 518 | my $self = shift( @_ ); | ||
| 519 | my $e = HTML::Object::Element->new( @_ ) || | ||
| 520 | return( $self->pass_error( HTML::Object::Element->error ) ); | ||
| 521 | $e->debug( $self->debug ); | ||
| 522 | return( $e ); | ||
| 523 | } | ||
| 524 | |||
| 525 | sub new_space | ||
| 526 | { | ||
| 527 | my $self = shift( @_ ); | ||
| 528 | my $e = HTML::Object::Space->new( @_ ) || | ||
| 529 | return( $self->pass_error( HTML::Object::Space->error ) ); | ||
| 530 | return( $e ); | ||
| 531 | } | ||
| 532 | |||
| 533 | sub new_special | ||
| 534 | { | ||
| 535 | my $self = shift( @_ ); | ||
| 536 | my $class = shift( @_ ); | ||
| 537 | $self->_load_class( $class ) || return( $self->pass_error ); | ||
| 538 | my $e = $class->new( @_ ) || return( $self->pass_error( $class->error ) ); | ||
| 539 | $e->debug( $self->debug ); | ||
| 540 | return( $e ); | ||
| 541 | } | ||
| 542 | |||
| 543 | sub new_text | ||
| 544 | { | ||
| 545 | my $self = shift( @_ ); | ||
| 546 | my $e = HTML::Object::Text->new( @_ ) || | ||
| 547 | return( $self->pass_error( HTML::Object::Text->error ) ); | ||
| 548 | return( $e ); | ||
| 549 | } | ||
| 550 | |||
| 551 | sub parse | ||
| 552 | { | ||
| 553 | my $self = shift( @_ ); | ||
| 554 | my $this = shift( @_ ); | ||
| 555 | my $opts = $self->_get_args_as_hash( @_ ); | ||
| 556 | if( ref( $this ) eq 'CODE' || ref( $this ) eq 'GLOB' || "$this" =~ /<\w+/ || CORE::length( "$this" ) > 1024 ) | ||
| 557 | { | ||
| 558 | return( $self->parse_data( $this, $opts ) ); | ||
| 559 | } | ||
| 560 | elsif( ref( $this ) ) | ||
| 561 | { | ||
| 562 | 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 | return( $self->parse_file( $this, $opts ) ); | ||
| 567 | } | ||
| 568 | } | ||
| 569 | |||
| 570 | sub parse_data | ||
| 571 | { | ||
| 572 | my $self = shift( @_ ); | ||
| 573 | my $html = shift( @_ ); | ||
| 574 | my $opts = $self->_get_args_as_hash( @_ ); | ||
| 575 | $html = "$html"; | ||
| 576 | # try-catch | ||
| 577 | local $@; | ||
| 578 | eval | ||
| 579 | { | ||
| 580 | if( $opts->{utf8} ) | ||
| 581 | { | ||
| 582 | $html = Encode::decode( 'utf8', $html, Encode::FB_CROAK ); | ||
| 583 | } | ||
| 584 | }; | ||
| 585 | if( $@ ) | ||
| 586 | { | ||
| 587 | return( $self->error( "Error found while utf8 decoding ", length( $html ), " bytes of html data provided: $@" ) ); | ||
| 588 | } | ||
| 589 | |||
| 590 | my $e; | ||
| 591 | if( length( $self->{current_parent} ) && $self->_is_object( $self->{current_parent} ) ) | ||
| 592 | { | ||
| 593 | $e = $self->current_parent; | ||
| 594 | } | ||
| 595 | else | ||
| 596 | { | ||
| 597 | $e = $self->new_document( debug => $self->debug ); | ||
| 598 | $self->document( $e ); | ||
| 599 | $self->current_parent( $e ); | ||
| 600 | if( $self->isa( 'HTML::Object::DOM' ) ) | ||
| 601 | { | ||
| 602 | if( my $code = $self->onload ) | ||
| 603 | { | ||
| 604 | $e->onload( $code ); | ||
| 605 | } | ||
| 606 | if( my $code = $self->onreadystatechange ) | ||
| 607 | { | ||
| 608 | $e->onreadystatechange( $code ); | ||
| 609 | } | ||
| 610 | } | ||
| 611 | } | ||
| 612 | my $doc = $self->document; | ||
| 613 | my $p = $self->parser; | ||
| 614 | $self->_set_state( 'loading' => $doc ); | ||
| 615 | $p->parse( $html ); | ||
| 616 | $self->_set_state( 'interactive' => $doc ); | ||
| 617 | $self->post_process( $e ); | ||
| 618 | $self->_set_state( 'complete' => $doc ); | ||
| 619 | $p->eof; | ||
| 620 | return( $e ); | ||
| 621 | } | ||
| 622 | |||
| 623 | sub parse_file | ||
| 624 | { | ||
| 625 | my $self = shift( @_ ); | ||
| 626 | my $file = shift( @_ ) || return( $self->error( "No file to parse was provided." ) ); | ||
| 627 | my $opts = $self->_get_args_as_hash( @_ ); | ||
| 628 | # On, by default, if not provided | ||
| 629 | $opts->{utf8} //= 1; | ||
| 630 | my $f = $self->new_file( $file ); | ||
| 631 | if( !$f->exists ) | ||
| 632 | { | ||
| 633 | return( $self->error( "File to parse \"$file\" does not exist." ) ); | ||
| 634 | } | ||
| 635 | elsif( $f->is_empty ) | ||
| 636 | { | ||
| 637 | return( $self->error( "File to parse \"$file\" is empty." ) ); | ||
| 638 | } | ||
| 639 | my $params = {}; | ||
| 640 | $params->{binmode} = 'utf8' if( $opts->{utf8} ); | ||
| 641 | my $io = $f->open( '<', $params ) || | ||
| 642 | return( $self->error( "Unable to open file to parse \"$file\": ", $f->error ) ); | ||
| 643 | my $e = $self->new_document( _last_modified => $f->mtime ); | ||
| 644 | $self->document( $e ); | ||
| 645 | if( $self->isa( 'HTML::Object::DOM' ) ) | ||
| 646 | { | ||
| 647 | if( my $code = $self->onload ) | ||
| 648 | { | ||
| 649 | $e->onload( $code ); | ||
| 650 | } | ||
| 651 | if( my $code = $self->onreadystatechange ) | ||
| 652 | { | ||
| 653 | $e->onreadystatechange( $code ); | ||
| 654 | } | ||
| 655 | } | ||
| 656 | $self->current_parent( $e ); | ||
| 657 | $self->_set_state( 'loading' => $e ); | ||
| 658 | my $p = $self->parser; | ||
| 659 | $p->parse_file( $io ); | ||
| 660 | $io->close; | ||
| 661 | $self->_set_state( 'interactive' => $e ); | ||
| 662 | $self->post_process( $e ); | ||
| 663 | $self->_set_state( 'complete' => $e ); | ||
| 664 | return( $e ); | ||
| 665 | } | ||
| 666 | |||
| 667 | sub parse_url | ||
| 668 | { | ||
| 669 | my $self = shift( @_ ); | ||
| 670 | my $uri; | ||
| 671 | if( ( scalar( @_ ) == 1 && ref( $_[0] ) ne 'HASH' ) || | ||
| 672 | ( scalar( @_ ) > 1 && | ||
| 673 | ( | ||
| 674 | ( @_ % 2 ) || | ||
| 675 | ( scalar( @_ ) == 2 && ref( $_[1] ) eq 'HASH' ) | ||
| 676 | ) | ||
| 677 | ) ) | ||
| 678 | { | ||
| 679 | $uri = shift( @_ ); | ||
| 680 | } | ||
| 681 | my $opts = $self->_get_args_as_hash( @_ ); | ||
| 682 | $uri = CORE::delete( $opts->{uri} ) if( defined( $opts->{uri} ) && CORE::length( $opts->{uri} ) ); | ||
| 683 | if( !$self->_load_class( 'LWP::UserAgent', { version => '6.49' } ) ) | ||
| 684 | { | ||
| 685 | return( $self->error( "LWP::UserAgent version 6.49 or higher is required to use load()" ) ); | ||
| 686 | } | ||
| 687 | if( !$self->_load_class( 'URI', { version => '1.74' } ) ) | ||
| 688 | { | ||
| 689 | return( $self->error( "URI version 1.74 or higher is required to use load()" ) ); | ||
| 690 | } | ||
| 691 | $opts->{timeout} //= 10; | ||
| 692 | # try-catch | ||
| 693 | local $@; | ||
| 694 | eval | ||
| 695 | { | ||
| 696 | $uri = URI->new( "$uri" ); | ||
| 697 | }; | ||
| 698 | if( $@ ) | ||
| 699 | { | ||
| 700 | return( $self->error( "Bad url provided \"$uri\": $@" ) ); | ||
| 701 | } | ||
| 702 | |||
| 703 | my $content; | ||
| 704 | my $resp; | ||
| 705 | # try-catch | ||
| 706 | eval | ||
| 707 | { | ||
| 708 | my $ua = LWP::UserAgent->new( | ||
| 709 | agent => "HTML::Object/$VERSION", | ||
| 710 | timeout => $opts->{timeout}, | ||
| 711 | ); | ||
| 712 | $resp = $ua->get( $uri, ( CORE::exists( $opts->{headers} ) && defined( $opts->{headers} ) && ref( $opts->{headers} ) eq 'HASH' && scalar( keys( %{$opts->{headers}} ) ) ) ? %{$opts->{headers}} : () ); | ||
| 713 | $content = $resp->decoded_content; | ||
| 714 | }; | ||
| 715 | if( $resp->header( 'Client-Warning' ) || !$resp->is_success ) | ||
| 716 | { | ||
| 717 | return( $self->error({ | ||
| 718 | code => $resp->code, | ||
| 719 | message => $resp->message, | ||
| 720 | }) ); | ||
| 721 | } | ||
| 722 | $self->response( $resp ); | ||
| 723 | if( $@ ) | ||
| 724 | { | ||
| 725 | return( $self->error( "Error making a GET request to $uri: $@" ) ); | ||
| 726 | } | ||
| 727 | my $doc = $self->parse_data( $content ); | ||
| 728 | $doc->uri( $uri ); | ||
| 729 | return( $doc ); | ||
| 730 | } | ||
| 731 | |||
| 732 | sub parser { return( shift->_set_get_object_without_init( '_parser', 'HTML::Parser', @_ ) ); } | ||
| 733 | |||
| 734 | sub post_process | ||
| 735 | { | ||
| 736 | my $self = shift( @_ ); | ||
| 737 | my $elem = shift( @_ ); | ||
| 738 | return if( !$self->_is_object( $elem ) ); | ||
| 739 | return if( !$elem->isa( 'HTML::Object::Element' ) ); | ||
| 740 | # Crawl through the tree and look for unclosed tags | ||
| 741 | $elem->children->foreach(sub | ||
| 742 | { | ||
| 743 | my $e = shift( @_ ); | ||
| 744 | return(1) if( $e->isa( 'HTML::Object::Closing' ) || $e->tag->substr( 0, 1 ) eq '_' ); | ||
| 745 | if( $e->is_empty && $e->children->length ) | ||
| 746 | { | ||
| 747 | # It should be empty, but has some children | ||
| 748 | } | ||
| 749 | elsif( $e->is_empty && !$e->attributes->exists( '/' ) ) | ||
| 750 | { | ||
| 751 | # Is empty, but does not end with /> | ||
| 752 | } | ||
| 753 | elsif( !$e->is_empty && !$e->is_closed ) | ||
| 754 | { | ||
| 755 | my $def = $self->get_definition( $e->tag ); | ||
| 756 | if( !$def->{is_empty} ) | ||
| 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 | $self->post_process( $e ) if( !$e->is_empty ); | ||
| 766 | }); | ||
| 767 | return( $self ); | ||
| 768 | } | ||
| 769 | |||
| 770 | sub response { return( shift->_set_get_object_without_init( 'response', 'HTTP::Response', @_ ) ); } | ||
| 771 | |||
| 772 | sub sanity_check | ||
| 773 | { | ||
| 774 | my $self = shift( @_ ); | ||
| 775 | my $elem = shift( @_ ); | ||
| 776 | return if( !$self->_is_object( $elem ) ); | ||
| 777 | return if( !$elem->isa( 'HTML::Object::Element' ) ); | ||
| 778 | # Crawl through the tree and look for unclosed tags | ||
| 779 | $elem->children->foreach(sub | ||
| 780 | { | ||
| 781 | my $e = shift( @_ ); | ||
| 782 | return(1) if( $e->isa( 'HTML::Object::Closing' ) || $e->tag->substr( 0, 1 ) eq '_' ); | ||
| 783 | if( $e->is_empty && $e->children->length ) | ||
| 784 | { | ||
| 785 | 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( '/' ) ) | ||
| 788 | { | ||
| 789 | 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( '/' ) ) | ||
| 792 | { | ||
| 793 | 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 ) | ||
| 796 | { | ||
| 797 | my $def = $self->get_definition( $e->tag ); | ||
| 798 | if( !$def->{is_empty} ) | ||
| 799 | { | ||
| 800 | 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 | 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 | $self->sanity_check( $e ) if( !$e->is_empty ); | ||
| 808 | }); | ||
| 809 | return( $self ); | ||
| 810 | } | ||
| 811 | |||
| 812 | sub set_dom | ||
| 813 | { | ||
| 814 | my( $this, $html ) = @_; | ||
| 815 | if( defined( $html ) ) | ||
| 816 | { | ||
| 817 | if( Scalar::Util::blessed( $html ) && $html->isa( 'HTML::Object::Document' ) ) | ||
| 818 | { | ||
| 819 | $GLOBAL_DOM = $html; | ||
| 820 | } | ||
| 821 | elsif( CORE::length( $html ) ) | ||
| 822 | { | ||
| 823 | $GLOBAL_DOM = $this->new->parse( $html ); | ||
| 824 | } | ||
| 825 | } | ||
| 826 | return( $this ); | ||
| 827 | } | ||
| 828 | |||
| 829 | sub _set_state | ||
| 830 | { | ||
| 831 | my $self = shift( @_ ); | ||
| 832 | my( $state, $elem ) = @_; | ||
| 833 | # This feature is only applicable for HTML::Object::DOM | ||
| 834 | return( $self ) unless( $self->isa( 'HTML::Object::DOM' ) ); | ||
| 835 | # ... and only for documents | ||
| 836 | return if( !defined( $elem ) || !$self->_is_a( $elem => 'HTML::Object::DOM::Document' ) ); | ||
| 837 | $elem->readyState( $state ); | ||
| 838 | require HTML::Object::Event; | ||
| 839 | 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 | if( my $eh = $elem->onreadystatechange ) | ||
| 847 | { | ||
| 848 | local $_ = $elem; | ||
| 849 | my $code = $eh->code; | ||
| 850 | warn( "Value for event handler '$code' is not a code reference.\n" ) if( ref( $code ) ne 'CODE' ); | ||
| 851 | $code->( $event ) if( ref( $code ) eq 'CODE' ); | ||
| 852 | } | ||
| 853 | if( $state eq 'complete' && ( my $code = $elem->onload ) ) | ||
| 854 | { | ||
| 855 | local $_ = $elem; | ||
| 856 | $code->( $event ); | ||
| 857 | } | ||
| 858 | return( $self ); | ||
| 859 | } | ||
| 860 | |||
| 861 | 1; | ||
| 862 | # NOTE: POD | ||
| 863 | __END__ |