| 1 | ##---------------------------------------------------------------------------- | ||
| 2 | ## HTML Object - ~/lib/HTML/Object/DOM/Element/Title.pm | ||
| 3 | ## Version v0.3.0 | ||
| 4 | ## Copyright(c) 2022 DEGUEST Pte. Ltd. | ||
| 5 | ## Author: Jacques Deguest <jack@deguest.jp> | ||
| 6 | ## Created 2022/01/09 | ||
| 7 | ## Modified 2025/10/16 | ||
| 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::DOM::Element::Title; | ||
| 15 | BEGIN | ||
| 16 | { | ||
| 17 | use strict; | ||
| 18 | use warnings; | ||
| 19 | warnings::register_categories( 'HTML::Object' ); | ||
| 20 | use parent qw( HTML::Object::DOM::Element ); | ||
| 21 | use vars qw( $VERSION ); | ||
| 22 | use Wanted; | ||
| 23 | our $VERSION = 'v0.3.0'; | ||
| 24 | }; | ||
| 25 | |||
| 26 | use strict; | ||
| 27 | use warnings; | ||
| 28 | |||
| 29 | sub init | ||
| 30 | { | ||
| 31 | my $self = shift( @_ ); | ||
| 32 | $self->{_init_strict_use_sub} = 1; | ||
| 33 | $self->SUPER::init( @_ ) || return( $self->pass_error ); | ||
| 34 | $self->{tag} = 'title' if( !CORE::length( "$self->{tag}" ) ); | ||
| 35 | # We set this boolean to true to indicate we have not yet looked into the text inside the <title></title> | ||
| 36 | $self->{_initial_text} = 1; | ||
| 37 | return( $self ); | ||
| 38 | } | ||
| 39 | |||
| 40 | # Note: property text | ||
| 41 | sub text : lvalue { return( shift->_set_get_callback({ | ||
| 42 | get => sub | ||
| 43 | { | ||
| 44 | my $self = shift( @_ ); | ||
| 45 | if( !$self->{_title_text} || $self->_is_reset ) | ||
| 46 | { | ||
| 47 | # We set this boolean to true to indicate we have not yet looked into the text inside the <title></title> | ||
| 48 | # The HTML::Parser sets the title value to anything within <title></title> no matter if there are any tag embedded, so we need to parse it further, but only once, hence this boolean value | ||
| 49 | if( $self->{_initial_text} ) | ||
| 50 | { | ||
| 51 | my $children = $self->children; | ||
| 52 | my $val; | ||
| 53 | $val = $self->as_text; | ||
| 54 | if( $self->looks_like_it_has_html( "$val" ) ) | ||
| 55 | { | ||
| 56 | my $p = $self->new_parser; | ||
| 57 | my $doc = $p->parse_data( $val ); | ||
| 58 | my $kids = $doc->children; | ||
| 59 | $_->parent( $self ) for( @$kids ); | ||
| 60 | $children->set( $kids ); | ||
| 61 | } | ||
| 62 | else | ||
| 63 | { | ||
| 64 | # Title text does not look like it contains HTML. | ||
| 65 | } | ||
| 66 | CORE::delete( $self->{_initial_text} ); | ||
| 67 | } | ||
| 68 | |||
| 69 | my $result = $self->new_array; | ||
| 70 | # We purposively skip anything that is neither a space nor a text. | ||
| 71 | # This is what web browser do, notwithstanding any tag that may exist in the <title> tag | ||
| 72 | $self->children->foreach(sub | ||
| 73 | { | ||
| 74 | if( $self->_is_a( $_ => 'HTML::Object::DOM::Text' ) || | ||
| 75 | $self->_is_a( $_ => 'HTML::Object::DOM::Space' ) ) | ||
| 76 | { | ||
| 77 | my $v = $_->value; | ||
| 78 | $result->push( "$v" ); | ||
| 79 | } | ||
| 80 | }); | ||
| 81 | $self->{_title_text} = $result->join( '' )->scalar; | ||
| 82 | $self->_remove_reset; | ||
| 83 | } | ||
| 84 | my $text = $self->{_title_text}; | ||
| 85 | return( $text ); | ||
| 86 | }, | ||
| 87 | set => sub | ||
| 88 | { | ||
| 89 | my $self = shift( @_ ); | ||
| 90 | my $arg = shift( @_ ); | ||
| 91 | my $nodes = $self->_get_from_list_of_elements_or_html( $arg ); | ||
| 92 | # for( my $i = 0; $i < scalar( @$nodes ); $i++ ) | ||
| 93 | # { | ||
| 94 | # } | ||
| 95 | |||
| 96 | if( !defined( $nodes ) ) | ||
| 97 | { | ||
| 98 | return( $self->pass_error ); | ||
| 99 | } | ||
| 100 | my $ok = 1; | ||
| 101 | for( @$nodes ) | ||
| 102 | { | ||
| 103 | if( !$self->_is_a( $_ => 'HTML::Object::DOM::Text' ) && | ||
| 104 | !$self->_is_a( $_ => 'HTML::Object::DOM::Space' ) ) | ||
| 105 | { | ||
| 106 | $ok = 0, last; | ||
| 107 | } | ||
| 108 | } | ||
| 109 | if( !$ok ) | ||
| 110 | { | ||
| 111 | return( $self->error( 'Values provided for title text contains data other tan text or space. You can provide text, space including HTML::Object::DOM::Text and HTML::Object::DOM::Space objects' ) ); | ||
| 112 | } | ||
| 113 | $_->parent( $self ) for( @$nodes ); | ||
| 114 | my $children = $self->children; | ||
| 115 | $children->set( $nodes ); | ||
| 116 | $self->reset(1); | ||
| 117 | return(1); | ||
| 118 | } | ||
| 119 | }, @_ ) ); } | ||
| 120 | |||
| 121 | 1; | ||
| 122 | # NOTE: POD | ||
| 123 | __END__ |