| 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__ | ||
| 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 |