lib/HTML/Object/DOM/Element/Anchor.pm

filter: Statementstmt 100% 41 / 41 Branchbran 83.3% 5 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 14 / 14 Podpod 100% 4 / 4 totaltotal 98.4% 64 / 65 CC 4 SCAR 3.9
CC
4
Cov
98%
init
3.0
__ANON__
2.0
BEGIN
1.0
CRAP 1.5
« lib/HTML/Object/Exception.pm ↑ Summary lib/HTML/Object/Text.pm »
1 ##----------------------------------------------------------------------------
2 ## HTML Object - ~/lib/HTML/Object/DOM/Element/Anchor.pm
3 ## Version v0.2.0
4 ## Copyright(c) 2021 DEGUEST Pte. Ltd.
5 ## Author: Jacques Deguest <jack@deguest.jp>
6 ## Created 2021/12/21
7 ## Modified 2022/09/18
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::Anchor;
15 BEGIN
16 {
17 7 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 7 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 7 ▶ warnings::register_categories( 'HTML::Object' );
Subroutine BEGIN: called CC=1 SCAR=0.0
20 7 ▶ use parent qw( HTML::Object::DOM::Element );
Subroutine BEGIN: called CC=1 SCAR=0.0
21 7 ▶ use vars qw( $VERSION );
Subroutine BEGIN: called CC=1 SCAR=0.0
22 7 ▶ use HTML::Object;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 7 ▶ use HTML::Object::DOM::Element::Shared qw( :anchor );
Subroutine BEGIN: called CC=1 SCAR=0.0
24 7 our $VERSION = 'v0.2.0';
25 };
26  
27 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 sub init
31 {
32 6 ▶ my $self = shift( @_ );
Subroutine init: called CC=3 SCAR=11.0
Pod documented
33 6 $self->{_init_strict_use_sub} = 1;
34 6 ▶ $self->SUPER::init( @_ ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->SUPER::init(@_) or return $self->pass_error 0 6 6
35 6 ▶ $self->{tag} = 'a' if( !$self->tag );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->tag 1 5 6
36 6 $self->{uri} = '';
37 $self->_set_get_internal_attribute_callback( rel => sub
38 {
39 3 ▶ my( $this, $val ) = @_;
Subroutine __ANON__: called CC=2 SCAR=6.9
40 3 my $list;
41 3 ▶ return if( !( $list = $this->{_rel_list} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $list = $this->{"_rel_list"} 1 2 3
42 # $list->debug( $self->debug );
43 2 $list->update( $val );
44 6 });
45 6 return( $self );
46 }
47  
48 # Note: property download inherited
49  
50 # Note: property hash inherited
51  
52 # Note: property host inherited
53  
54 # Note: property hostname inherited
55  
56 # Note: property href inherited
57  
58 # Note: property hreflang inherited
59  
60 # Note: read-only property origin inherited
61  
62 # Note: property password inherited
63  
64 # Note: property pathname inherited
65  
66 # Note: property port inherited
67  
68 # Note: property protocol inherited
69  
70 # Note: property referrerPolicy inherited
71  
72 # Note: property rel inherited
73  
74 # Note: property read-only relList inherited. Similar to HTML::Object::DOM::Element->classList
75  
76 # Note: property search inherited
77  
78 # Note: tabIndex is inherited from HTML::Object::DOM::Element
79  
80 # Note: property target inherited
81  
82 # Note: alias for textContent in HTML::Object::DOM::Node
83 # Note: property
84 2 ▶sub text : lvalue { return( shift->textContent( @_ ) ); }
Subroutine text: called CC=1 SCAR=0.0
Pod documented
85  
86 1 ▶sub toString { return( shift->attr( 'href' ) ); }
Subroutine toString: called CC=1 SCAR=0.0
Pod documented
87  
88 # Note: property
89 2 ▶sub type : lvalue { return( shift->_set_get_property( 'type', @_ ) ); }
Subroutine type: called CC=1 SCAR=0.0
Pod documented
90  
91 # Note: property username inherited
92  
93 7 1;
94 # NOTE: POD
95 __END__
96  
97 =encoding utf-8
98  
99 =head1 NAME
100  
101 HTML::Object::DOM::Element::Anchor - HTML Object DOM Link Class
102  
103 =head1 SYNOPSIS
104  
105 use HTML::Object::DOM::Element::Anchor;
106 my $link = HTML::Object::DOM::Element::Anchor->new ||
107 die( HTML::Object::DOM::Element::Anchor->error, "\n" );
108  
109 =head1 VERSION
110  
111 v0.2.0
112  
113 =head1 DESCRIPTION
114  
115 This implements an HTML link element. It inherits from L<HTML::Object::DOM::Element>
116  
117 =head1 INHERITANCE
118  
119 +-----------------------+ +---------------------------+ +-------------------------+ +----------------------------+ +------------------------------------+
120 | HTML::Object::Element | --> | HTML::Object::EventTarget | --> | HTML::Object::DOM::Node | --> | HTML::Object::DOM::Element | --> | HTML::Object::DOM::Element::Anchor |
121 +-----------------------+ +---------------------------+ +-------------------------+ +----------------------------+ +------------------------------------+
122  
123 =head1 PROPERTIES
124  
125 This class inherits properties from its parent, L<HTML::Object::DOM::Element>.
126  
127 =head2 download
128  
129 This indicates that the linked resource is intended to be downloaded rather than displayed in the browser. The value represent the proposed name of the file.
130  
131 Note that you can put whatever you want here, but it does not mean the web browser will accept it and let alone act upon it. Research the reliability of this attribute first before relying on it.
132  
133 Example:
134  
135 <a id="myAnchor" href="/some/where#nice">Nice spot</a>
136  
137 my $anchor = $doc->getElementById("myAnchor");
138 $anchor->download = 'my_file.txt';
139 # link is now:
140 # <a id="myAnchor" href="/some/where#nice" download="nice_file.txt">Nice spot</a>
141  
142 See L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLAnchorElement/download>
143  
144 =head2 hash
145  
146 Is a string representing the fragment identifier, including the leading hash mark ('#'), if any, in the referenced URL.
147  
148 Example:
149  
150 <a id="myAnchor" href="/some/where#nice">Examples</a>
151  
152 my $anchor = $doc->getElementById("myAnchor");
153 $anchor->hash; # returns '#nice'
154  
155 See L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLAnchorElement/hash>
156  
157 =head2 host
158  
159 Is a string representing the hostname and port (if it's not the default port) in the referenced URL.
160  
161 Example:
162  
163 my $anchor = $document->createElement("a");
164  
165 $anchor->href = "https://example.org/some/where"
166 $anchor->host = "example.org"
167  
168 $anchor->href = "https://example.org:443/some/where"
169 $anchor->host = "example.org"
170 # The port number is not included because 443 is the scheme's default port
171  
172 $anchor->href = "https://example.org:4097/some/where"
173 $anchor->host = "example.org:4097"
174  
175 See L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLAnchorElement/host>
176  
177 =head2 hostname
178  
179 Is a string representing the hostname in the referenced URL.
180  
181 Example:
182  
183 # An <a id="myAnchor" href="https://example.org/some/where"> element is in the document
184 my $anchor = $doc->getElementById("myAnchor");
185 $anchor->hostname; # returns 'example.org'
186  
187 See L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLAnchorElement/hostname>
188  
189 =head2 href
190  
191 Is a string that is the result of parsing the href HTML attribute relative to the document, containing a valid URL of a linked resource.
192  
193 # An <a id="myAnchor" href="https://example.org/some/where"> element is in the document
194 my $anchor = $doc->getElementById("myAnchor");
195 $anchor->href; # returns 'https://example.org/some/where'
196  
197 See L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLAnchorElement/href>
198  
199 =head2 hreflang
200  
201 Is a string that reflects the hreflang HTML attribute, indicating the language of the linked resource.
202  
203 Example:
204  
205 <a href="https://example.org/ja-jp/some/where" hreflang="ja">マニュアル</a>
206 var lang = document.getElementById("myAnchor").hreflang; # ja
207  
208 See L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLAnchorElement/hreflang>
209  
210 =head2 origin
211  
212 Read-only.
213  
214 Returns a string containing the origin of the URL, that is its scheme, its domain and its port.
215  
216 Example:
217  
218 # An <a id="myAnchor" href="https://example.org/some/where"> element is in the document
219 my $anchor = $doc->getElementById("myAnchor");
220 $anchor->origin; # returns 'https://example.org'
221  
222 See L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLAnchorElement/origin>
223  
224 =head2 password
225  
226 Is a string containing the password specified before the domain name.
227  
228 Example:
229  
230 # An <a id="myAnchor" href="https://anonymous:flabada@example.org/some/where"> is in the document
231 my $anchor = $doc->getElementByID("myAnchor");
232 $anchor->password; # returns 'flabada'
233  
234 See L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLAnchorElement/password>
235  
236 =head2 pathname
237  
238 Is a string containing an initial '/' followed by the path of the URL, not including the query string or fragment.
239  
240 # An <a id="myAnchor" href="https://example.org/some/where"> element is in the document
241 my $anchor = $doc->getElementById("myAnchor");
242 $anchor->pathname; # returns '/some/where'
243  
244 See L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLAnchorElement/pathname>
245  
246 =head2 port
247  
248 Is a string representing the port component, if any, of the referenced URL.
249  
250 # An <a id="myAnchor" href="https://example.org:443/some/where"> element is in the document
251 my $anchor = $doc->getElementByID("myAnchor");
252 $anchor->port; # returns '443'
253  
254 See L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLAnchorElement/port>
255  
256 =head2 protocol
257  
258 Is a string representing the protocol component, including trailing colon (':'), of the referenced URL.
259  
260 # An <a id="myAnchor" href="https://example.org/some/where"> element is in the document
261 my $anchor = $doc->getElementById("myAnchor");
262 $anchor->protocol; # returns 'https:'
263  
264 See L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLAnchorElement/protocol>
265  
266 =head2 referrerPolicy
267  
268 Is a string that reflects the referrerpolicy HTML attribute indicating which referrer to use.
269  
270 A proper value is one of the following:
271  
272 =over 4
273  
274 =item no-referrer
275  
276 The Referer header will be omitted entirely. No referrer information is sent along with requests.
277  
278 =item no-referrer-when-downgrade
279  
280 The URL is sent as a referrer when the protocol security level stays the same (e.g.HTTP→HTTP, HTTPS→HTTPS), but is not sent to a less secure destination (e.g. HTTPS→HTTP).
281  
282 =item origin
283  
284 Only send the origin of the document as the referrer in all cases. The document C<https://example.com/page.html> will send the referrer C<https://example.com/>.
285  
286 =item origin-when-cross-origin
287  
288 Send a full URL when performing a same-origin request, but only send the origin of the document for other cases.
289  
290 =item same-origin
291  
292 A referrer will be sent for same-site origins, but cross-origin requests will contain no referrer information.
293  
294 =item strict-origin
295  
296 Only send the origin of the document as the referrer when the protocol security level stays the same (e.g. HTTPS→HTTPS), but do not send it to a less secure destination (e.g. HTTPS→HTTP).
297  
298 =item strict-origin-when-cross-origin (default)
299  
300 This is the user agent's default behavior if no policy is specified. Send a full URL when performing a same-origin request, only send the origin when the protocol security level stays the same (e.g. HTTPS→HTTPS), and send no header to a less secure destination (e.g. HTTPS→HTTP).
301  
302 =item unsafe-url
303  
304 Send a full URL when performing a same-origin or cross-origin request. This policy will leak origins and paths from TLS-protected resources to insecure origins. Carefully consider the impact of this setting.
305  
306 =back
307  
308 Example:
309  
310 my $elt = $doc->createElement("a");
311 my $linkText = $doc->createTextNode("My link");
312 $elt->appendChild( $linkText );
313 $elt->href = "https://example.org/ja-jp/";
314 $elt->referrerPolicy = "no-referrer";
315  
316 my $div = $doc->getElementById("divAround");
317 $div->appendChild( $elt ); # When clicked, the link will not send a referrer header.
318  
319 See L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLAnchorElement/referrerPolicy>
320  
321 =head2 rel
322  
323 Is a string that reflects the rel HTML attribute, specifying the relationship of the target object to the linked object.
324  
325 You can set whatever value you want, but standard values are:
326  
327 =over 4
328  
329 =item L<alternate|https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/rel#attr-alternate>
330  
331 =item L<author|https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/rel#attr-author>
332  
333 =item L<bookmark|https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/rel#attr-bookmark>
334  
335 =item L<help|https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/rel#attr-help>
336  
337 =item L<license|https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/rel#attr-license>
338  
339 =item L<manifest|https://developer.mozilla.org/en-US/docs/Web/HTML/Link_types/manifest>
340  
341 =item L<next|https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/rel#attr-next>
342  
343 =item L<prev|https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/rel#attr-prev>
344  
345 =item L<search|https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/rel#attr-search>
346  
347 =item L<tag|https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/rel#attr-tag>
348  
349 =back
350  
351 Example:
352  
353 my $anchors = $doc->getElementsByTagName("a");
354 my $length = $anchors->length;
355 for( my $i = 0; $i < $length; $i++ )
356 {
357 say( "Rel: " + anchors->[$i]->rel );
358 }
359  
360 could print:
361  
362 Re;: noopener noreferrer
363  
364 The C<noopener> value prevents the newly opened page from controlling the page that delivered the traffic. This is because, in the case of C<target> attribute set to C<_blank>, for example, the newly opened page can access the current window object with the C<window.opener> property. This may also allow the new page to redirect the current page to a malicious URL. This makes the link behave as if window.opener were null and target="_parent" were set.
365  
366 The C<noreferrer> value prevents the web browser from sending the referrer information to the target site. C<noreferrer> attribute has the same effect as C<noopener> and is well supported, but often both are used for when C<noopener> is not supported.
367  
368 To mitigate this issue, you could do:
369  
370 # Replace 'example.org' by whatever your safe domain name is
371 $doc->getElementsByTagName( 'a' )->foreach(sub
372 {
373 if( $_->href->host ne 'example.org' &&
374 $_->hasAttribute( 'target' ) &&
375 $_->getAttribute( 'target' ) eq '_blank' )
376 {
377 $_->relList->add( qw( noopener noreferrer ) );
378 }
379 });
380  
381 See L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLAnchorElement/rel>, L<Mozilla documentation on rel|https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/rel>, L<StackOverflow discussion|https://stackoverflow.com/questions/57628890/why-people-use-rel-noopener-noreferrer-instead-of-just-rel-noreferrer>
382  
383 L<W3C specification|https://html.spec.whatwg.org/multipage/links.html#link-type-noreferrer>
384  
385 =head2 relList
386  
387 Read-only.
388  
389 Returns a L<TokenList|HTML::Object::TokenList> that reflects the rel HTML attribute, as a list of link types indicating the relationship between the resource represented by the <a> element and the current document.
390  
391 The property itself is read-only, meaning you can not substitute the L<TokenList|HTML::Object::TokenList> with another one, but its contents can still be changed.
392  
393 Example:
394  
395 my $anchors = $doc->getElementsByTagName("a");
396 my $length = $anchors->length;
397 for( my $i = 0; $i < $length; $i++ )
398 {
399 my $list = $anchors->[$i]->relList;
400 my $listLength = $list->length;
401 say( "New anchor node found with", $listLength, "link types in relList." );
402 for( my $j = 0; $j < $listLength; $j++ )
403 {
404 say( $list->[$j] );
405 }
406 }
407  
408 See L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLAnchorElement/relList>
409  
410 =head2 search
411  
412 Is a string representing the search element, including leading question mark ('?'), if any, of the referenced URL.
413  
414 Example:
415  
416 # An <a id="myAnchor" href="/some/where?q=123"> element is in the document
417 my $anchor = $doc->getElementById("myAnchor");
418 $anchor->search; # returns '?q=123'
419  
420 See L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLAnchorElement/search>
421  
422 =head2 tabIndex
423  
424 Is a long containing the position of the element in the tabbing navigation order for the current document.
425  
426 See L<HTML::Object::DOM::Element/tabIndex>
427  
428 See L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLAnchorElement/tabIndex>
429  
430 =head2 target
431  
432 Is a string that reflects the target HTML attribute, indicating where to display the linked resource.
433  
434 Example:
435  
436 $doc->getElementById("myAnchor")->target = "_blank";
437  
438 See L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLAnchorElement/target>
439  
440 =head2 text
441  
442 Is a string being a synonym for the L<HTML::Object::DOM::Node/textContent> property.
443  
444 See L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLAnchorElement/text>
445  
446 =head2 type
447  
448 Is a string that reflects the type HTML attribute, indicating the MIME type of the linked resource.
449  
450 See L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLAnchorElement/type>
451  
452 =head2 username
453  
454 Is a string containing the username specified before the domain name.
455  
456 Example:
457  
458 # An <a id="myAnchor" href="https://anonymous:flabada@example.org/some/where"> element is in the document
459 my $anchor = $doc->getElementByID("myAnchor");
460 $anchor->username; # returns 'anonymous'
461  
462 See L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLAnchorElement/username>
463  
464 =head1 METHODS
465  
466 Inherits methods from its parent L<HTML::Object::DOM::Element>
467  
468 =head2 toString
469  
470 Returns a string containing the whole URL. It is a synonym for HTMLAnchorElement.href, though it can't be used to modify the value.
471  
472 Example:
473  
474 # An <a id="myAnchor" href="https://example.org/some/where"> element is in the document
475 my $anchor = $doc->getElementById("myAnchor");
476 $anchor->toString(); # returns 'https://example.org/some/where'
477  
478 See L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLAnchorElement/toString>
479  
480 =head1 AUTHOR
481  
482 Jacques Deguest E<lt>F<jack@deguest.jp>E<gt>
483  
484 =head1 SEE ALSO
485  
486 L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLAnchorElement>, L<Mozilla documentation on anchor element|https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a>
487  
488 =head1 COPYRIGHT & LICENSE
489  
490 Copyright(c) 2021 DEGUEST Pte. Ltd.
491  
492 All rights reserved
493  
494 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
495  
496 =cut
« lib/HTML/Object/Exception.pm ↑ Summary lib/HTML/Object/Text.pm »