| 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 | 17 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 17 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 17 | ▶ | warnings::register_categories( 'HTML::Object' ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | 17 | ▶ | use parent qw( HTML::Object::DOM::Element ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | 17 | ▶ | use vars qw( $VERSION ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | 17 | ▶ | use Wanted; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | 17 | our $VERSION = 'v0.3.0'; | |||||||||||||||||||||||||||
| 24 | }; | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 17 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | 17 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | sub init | ||||||||||||||||||||||||||||
| 30 | { | ||||||||||||||||||||||||||||
| 31 | 27 | ▶ | my $self = shift( @_ ); | ||||||||||||||||||||||||||
|
Subroutine
init: called CC=3 SCAR=11.2
Pod
documented
| |||||||||||||||||||||||||||||
| 32 | 27 | $self->{_init_strict_use_sub} = 1; | |||||||||||||||||||||||||||
| 33 | 27 | ▶ | $self->SUPER::init( @_ ) || return( $self->pass_error ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 34 | 27 | ▶ | $self->{tag} = 'title' if( !CORE::length( "$self->{tag}" ) ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | # We set this boolean to true to indicate we have not yet looked into the text inside the <title></title> | ||||||||||||||||||||||||||||
| 36 | 27 | $self->{_initial_text} = 1; | |||||||||||||||||||||||||||
| 37 | 27 | return( $self ); | |||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | # Note: property text | ||||||||||||||||||||||||||||
| 41 | sub text : lvalue { return( shift->_set_get_callback({ | ||||||||||||||||||||||||||||
| 42 | get => sub | ||||||||||||||||||||||||||||
| 43 | { | ||||||||||||||||||||||||||||
| 44 | 4 | ▶ | my $self = shift( @_ ); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=6 SCAR=18.0
| |||||||||||||||||||||||||||||
| 45 | 4 | ▶ | if( !$self->{_title_text} || $self->_is_reset ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not $self->{"_title_text"} or $self->_is_reset
MC/DC50%1/2
not $self->{"_title_text"} or $self->_is_reset
not $self->{"_title_text"}
$self->_is_reset
Truth table66%2/3
not(A) or B
A= not $self->{"_title_text"}B= $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 | 2 | ▶ | if( $self->{_initial_text} ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | { | ||||||||||||||||||||||||||||
| 51 | 2 | my $children = $self->children; | |||||||||||||||||||||||||||
| 52 | 2 | my $val; | |||||||||||||||||||||||||||
| 53 | 2 | $val = $self->as_text; | |||||||||||||||||||||||||||
| 54 | 2 | ▶ | if( $self->looks_like_it_has_html( "$val" ) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | { | ||||||||||||||||||||||||||||
| 56 | 1 | my $p = $self->new_parser; | |||||||||||||||||||||||||||
| 57 | 1 | my $doc = $p->parse_data( $val ); | |||||||||||||||||||||||||||
| 58 | 1 | my $kids = $doc->children; | |||||||||||||||||||||||||||
| 59 | 1 | $_->parent( $self ) for( @$kids ); | |||||||||||||||||||||||||||
| 60 | 1 | $children->set( $kids ); | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | else | ||||||||||||||||||||||||||||
| 63 | { | ||||||||||||||||||||||||||||
| 64 | # Title text does not look like it contains HTML. | ||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | 2 | CORE::delete( $self->{_initial_text} ); | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 2 | 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 | 4 | ▶ | if( $self->_is_a( $_ => 'HTML::Object::DOM::Text' ) || | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
Branch: 1 branch
Condition66%2/3
$self->_is_a($_, "HTML::Object::DOM::Text") or $self->_is_a($_, "HTML::Object::DOM::Space")
MC/DC50%1/2
$self->_is_a($_, "HTML::Object::DOM::Text") or $self->_is_a($_, "HTML::Object::DOM::Space")
$self->_is_a($_, "HTML::Object::DOM::Text")
$self->_is_a($_, "HTML::Object::DOM::Space")
Truth table66%2/3
A or B
A= $self->_is_a($_, "HTML::Object::DOM::Text")B= $self->_is_a($_, "HTML::Object::DOM::Space")
| |||||||||||||||||||||||||||||
| 75 | $self->_is_a( $_ => 'HTML::Object::DOM::Space' ) ) | ||||||||||||||||||||||||||||
| 76 | { | ||||||||||||||||||||||||||||
| 77 | 3 | my $v = $_->value; | |||||||||||||||||||||||||||
| 78 | 3 | $result->push( "$v" ); | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | 2 | }); | |||||||||||||||||||||||||||
| 81 | 2 | $self->{_title_text} = $result->join( '' )->scalar; | |||||||||||||||||||||||||||
| 82 | 2 | $self->_remove_reset; | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | 4 | my $text = $self->{_title_text}; | |||||||||||||||||||||||||||
| 85 | 4 | return( $text ); | |||||||||||||||||||||||||||
| 86 | }, | ||||||||||||||||||||||||||||
| 87 | set => sub | ||||||||||||||||||||||||||||
| 88 | { | ||||||||||||||||||||||||||||
| 89 | 1 | ▶ | my $self = shift( @_ ); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=7 SCAR=22.3
| |||||||||||||||||||||||||||||
| 90 | 1 | my $arg = shift( @_ ); | |||||||||||||||||||||||||||
| 91 | 1 | my $nodes = $self->_get_from_list_of_elements_or_html( $arg ); | |||||||||||||||||||||||||||
| 92 | # for( my $i = 0; $i < scalar( @$nodes ); $i++ ) | ||||||||||||||||||||||||||||
| 93 | # { | ||||||||||||||||||||||||||||
| 94 | # } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 1 | ▶ | if( !defined( $nodes ) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | { | ||||||||||||||||||||||||||||
| 98 | 0 | return( $self->pass_error ); | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | 1 | my $ok = 1; | |||||||||||||||||||||||||||
| 101 | 1 | for( @$nodes ) | |||||||||||||||||||||||||||
| 102 | { | ||||||||||||||||||||||||||||
| 103 | 2 | ▶ | if( !$self->_is_a( $_ => 'HTML::Object::DOM::Text' ) && | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->_is_a($_, "HTML::Object::DOM::Text") or $self->_is_a($_, "HTML::Object::DOM::Space")
MC/DC50%1/2
$self->_is_a($_, "HTML::Object::DOM::Text") or $self->_is_a($_, "HTML::Object::DOM::Space")
$self->_is_a($_, "HTML::Object::DOM::Text")
$self->_is_a($_, "HTML::Object::DOM::Space")
Truth table66%2/3
A or B
A= $self->_is_a($_, "HTML::Object::DOM::Text")B= $self->_is_a($_, "HTML::Object::DOM::Space")
| |||||||||||||||||||||||||||||
| 104 | !$self->_is_a( $_ => 'HTML::Object::DOM::Space' ) ) | ||||||||||||||||||||||||||||
| 105 | { | ||||||||||||||||||||||||||||
| 106 | 1 | $ok = 0, last; | |||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | 1 | ▶ | if( !$ok ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | { | ||||||||||||||||||||||||||||
| 111 | 1 | 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 | 0 | $_->parent( $self ) for( @$nodes ); | |||||||||||||||||||||||||||
| 114 | 0 | my $children = $self->children; | |||||||||||||||||||||||||||
| 115 | 0 | $children->set( $nodes ); | |||||||||||||||||||||||||||
| 116 | 0 | $self->reset(1); | |||||||||||||||||||||||||||
| 117 | 0 | return(1); | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | 5 | ▶ | }, @_ ) ); } | ||||||||||||||||||||||||||
|
Subroutine
text: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 17 | 1; | |||||||||||||||||||||||||||
| 122 | # NOTE: POD | ||||||||||||||||||||||||||||
| 123 | __END__ | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | =encoding utf-8 | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | =head1 NAME | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | HTML::Object::DOM::Element::Title - HTML Object DOM Title Class | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | use HTML::Object::DOM::Element::Title; | ||||||||||||||||||||||||||||
| 134 | my $title = HTML::Object::DOM::Element::Title->new || | ||||||||||||||||||||||||||||
| 135 | die( HTML::Object::DOM::Element::Title->error, "\n" ); | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | =head1 VERSION | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | v0.3.0 | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | This interface contains the title for a document. This element inherits all of the properties and methods of the L<HTML::Object::DOM::Element> interface. | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | =head1 INHERITANCE | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | +-----------------------+ +---------------------------+ +-------------------------+ +----------------------------+ +-----------------------------------+ | ||||||||||||||||||||||||||||
| 148 | | HTML::Object::Element | --> | HTML::Object::EventTarget | --> | HTML::Object::DOM::Node | --> | HTML::Object::DOM::Element | --> | HTML::Object::DOM::Element::Title | | ||||||||||||||||||||||||||||
| 149 | +-----------------------+ +---------------------------+ +-------------------------+ +----------------------------+ +-----------------------------------+ | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | =head1 PROPERTIES | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | Inherits properties from its parent L<HTML::Object::DOM::Element> | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | =head2 text | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | Is a string representing the text of the document's title, and only the text part. For example, consider this: | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | <!doctype html> | ||||||||||||||||||||||||||||
| 160 | <html> | ||||||||||||||||||||||||||||
| 161 | <head> | ||||||||||||||||||||||||||||
| 162 | <title>Hello world! <span class="highlight">Isn't this wonderful</span> really?</title> | ||||||||||||||||||||||||||||
| 163 | </head> | ||||||||||||||||||||||||||||
| 164 | <body></body> | ||||||||||||||||||||||||||||
| 165 | </html> | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | my $title = $doc->getElementsByTagName( 'title' )->[0]; | ||||||||||||||||||||||||||||
| 168 | say $title->text; | ||||||||||||||||||||||||||||
| 169 | # Hello world! really? | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | As you can see, the tag C<span> and its content was skipped. | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | Also, do not confuse: | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | $doc->title; | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | with: | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | $doc->getElementsByTagName( 'title' )->[0]; | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | The former is just a setter/getter method to set or get the inner text value of the document title, while the latter is the L<HTML::Object::DOM::Element::Title> object. So you cannot write: | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | $doc->title->text = "Hello world!"; | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLTitleElement/text> | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | =head1 METHODS | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | Inherits methods from its parent L<HTML::Object::DOM::Element> | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | Jacques Deguest E<lt>F<jack@deguest.jp>E<gt> | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLTitleElement>, L<Mozilla documentation on title element|https://developer.mozilla.org/en-US/docs/Web/HTML/Element/title> | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | =head1 COPYRIGHT & LICENSE | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | Copyright(c) 2022 DEGUEST Pte. Ltd. | ||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | All rights reserved | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | =cut | ||||||||||||||||||||||||||||