NAME

HTML::Object::DOM::Element::Anchor - HTML Object DOM Link Class

SYNOPSIS

use HTML::Object::DOM::Element::Anchor;
my $link = HTML::Object::DOM::Element::Anchor->new || 
    die( HTML::Object::DOM::Element::Anchor->error, "\n" );

VERSION

v0.2.0

DESCRIPTION

This implements an HTML link element. It inherits from HTML::Object::DOM::Element

INHERITANCE

+-----------------------+     +---------------------------+     +-------------------------+     +----------------------------+     +------------------------------------+
| HTML::Object::Element | --> | HTML::Object::EventTarget | --> | HTML::Object::DOM::Node | --> | HTML::Object::DOM::Element | --> | HTML::Object::DOM::Element::Anchor |
+-----------------------+     +---------------------------+     +-------------------------+     +----------------------------+     +------------------------------------+

PROPERTIES

This class inherits properties from its parent, HTML::Object::DOM::Element.

download

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.

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.

Example:

<a id="myAnchor" href="/some/where#nice">Nice spot</a>

my $anchor = $doc->getElementById("myAnchor");
$anchor->download = 'my_file.txt';
# link is now:
# <a id="myAnchor" href="/some/where#nice" download="nice_file.txt">Nice spot</a>

See Mozilla documentation

hash

Is a string representing the fragment identifier, including the leading hash mark ('#'), if any, in the referenced URL.

Example:

<a id="myAnchor" href="/some/where#nice">Examples</a>

my $anchor = $doc->getElementById("myAnchor");
$anchor->hash; # returns '#nice'

See Mozilla documentation

host

Is a string representing the hostname and port (if it's not the default port) in the referenced URL.

Example:

my $anchor = $document->createElement("a");

$anchor->href = "https://example.org/some/where"
$anchor->host = "example.org"

$anchor->href = "https://example.org:443/some/where"
$anchor->host = "example.org"
# The port number is not included because 443 is the scheme's default port

$anchor->href = "https://example.org:4097/some/where"
$anchor->host = "example.org:4097"

See Mozilla documentation

hostname

Is a string representing the hostname in the referenced URL.

Example:

# An <a id="myAnchor" href="https://example.org/some/where"> element is in the document
my $anchor = $doc->getElementById("myAnchor");
$anchor->hostname; # returns 'example.org'

See Mozilla documentation

href

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.

# An <a id="myAnchor" href="https://example.org/some/where"> element is in the document
my $anchor = $doc->getElementById("myAnchor");
$anchor->href; # returns 'https://example.org/some/where'

See Mozilla documentation

hreflang

Is a string that reflects the hreflang HTML attribute, indicating the language of the linked resource.

Example:

<a href="https://example.org/ja-jp/some/where" hreflang="ja">マニュアル</a>
var lang = document.getElementById("myAnchor").hreflang; # ja

See Mozilla documentation

origin

Read-only.

Returns a string containing the origin of the URL, that is its scheme, its domain and its port.

Example:

# An <a id="myAnchor" href="https://example.org/some/where"> element is in the document
my $anchor = $doc->getElementById("myAnchor");
$anchor->origin; # returns 'https://example.org'

See Mozilla documentation

password

Is a string containing the password specified before the domain name.

Example:

# An <a id="myAnchor" href="https://anonymous:flabada@example.org/some/where"> is in the document
my $anchor = $doc->getElementByID("myAnchor");
$anchor->password; # returns 'flabada'

See Mozilla documentation

pathname

Is a string containing an initial '/' followed by the path of the URL, not including the query string or fragment.

# An <a id="myAnchor" href="https://example.org/some/where"> element is in the document
my $anchor = $doc->getElementById("myAnchor");
$anchor->pathname; # returns '/some/where'

See Mozilla documentation

port

Is a string representing the port component, if any, of the referenced URL.

# An <a id="myAnchor" href="https://example.org:443/some/where"> element is in the document
my $anchor = $doc->getElementByID("myAnchor");
$anchor->port; # returns '443'

See Mozilla documentation

protocol

Is a string representing the protocol component, including trailing colon (':'), of the referenced URL.

# An <a id="myAnchor" href="https://example.org/some/where"> element is in the document
my $anchor = $doc->getElementById("myAnchor");
$anchor->protocol; # returns 'https:'

See Mozilla documentation

referrerPolicy

Is a string that reflects the referrerpolicy HTML attribute indicating which referrer to use.

A proper value is one of the following:

no-referrer

The Referer header will be omitted entirely. No referrer information is sent along with requests.

no-referrer-when-downgrade

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).

origin

Only send the origin of the document as the referrer in all cases. The document https://example.com/page.html will send the referrer https://example.com/.

origin-when-cross-origin

Send a full URL when performing a same-origin request, but only send the origin of the document for other cases.

same-origin

A referrer will be sent for same-site origins, but cross-origin requests will contain no referrer information.

strict-origin

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).

strict-origin-when-cross-origin (default)

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).

unsafe-url

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.

Example:

my $elt = $doc->createElement("a");
my $linkText = $doc->createTextNode("My link");
$elt->appendChild( $linkText );
$elt->href = "https://example.org/ja-jp/";
$elt->referrerPolicy = "no-referrer";

my $div = $doc->getElementById("divAround");
$div->appendChild( $elt ); # When clicked, the link will not send a referrer header.

See Mozilla documentation

rel

Is a string that reflects the rel HTML attribute, specifying the relationship of the target object to the linked object.

You can set whatever value you want, but standard values are:

alternate
author
bookmark
help
license
manifest
next
prev
tag

Example:

my $anchors = $doc->getElementsByTagName("a");
my $length = $anchors->length;
for( my $i = 0; $i < $length; $i++ )
{
    say( "Rel: " + anchors->[$i]->rel );
}

could print:

Re;: noopener noreferrer

The noopener value prevents the newly opened page from controlling the page that delivered the traffic. This is because, in the case of target attribute set to _blank, for example, the newly opened page can access the current window object with the 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.

The noreferrer value prevents the web browser from sending the referrer information to the target site. noreferrer attribute has the same effect as noopener and is well supported, but often both are used for when noopener is not supported.

To mitigate this issue, you could do:

# Replace 'example.org' by whatever your safe domain name is
$doc->getElementsByTagName( 'a' )->foreach(sub
{
    if( $_->href->host ne 'example.org' && 
        $_->hasAttribute( 'target' ) && 
        $_->getAttribute( 'target' ) eq '_blank' )
    {
        $_->relList->add( qw( noopener noreferrer ) );
    }
});

See Mozilla documentation, Mozilla documentation on rel, StackOverflow discussion

W3C specification

relList

Read-only.

Returns a 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.

The property itself is read-only, meaning you can not substitute the TokenList with another one, but its contents can still be changed.

Example:

my $anchors = $doc->getElementsByTagName("a");
my $length = $anchors->length;
for( my $i = 0; $i < $length; $i++ )
{
    my $list = $anchors->[$i]->relList;
    my $listLength = $list->length;
    say( "New anchor node found with", $listLength, "link types in relList." );
    for( my $j = 0; $j < $listLength; $j++ )
    {
        say( $list->[$j] );
    }
}

See Mozilla documentation

search

Is a string representing the search element, including leading question mark ('?'), if any, of the referenced URL.

Example:

# An <a id="myAnchor" href="/some/where?q=123"> element is in the document
my $anchor = $doc->getElementById("myAnchor");
$anchor->search; # returns '?q=123'

See Mozilla documentation

tabIndex

Is a long containing the position of the element in the tabbing navigation order for the current document.

See "tabIndex" in HTML::Object::DOM::Element

See Mozilla documentation

target

Is a string that reflects the target HTML attribute, indicating where to display the linked resource.

Example:

$doc->getElementById("myAnchor")->target = "_blank";

See Mozilla documentation

text

Is a string being a synonym for the "textContent" in HTML::Object::DOM::Node property.

See Mozilla documentation

type

Is a string that reflects the type HTML attribute, indicating the MIME type of the linked resource.

See Mozilla documentation

username

Is a string containing the username specified before the domain name.

Example:

# An <a id="myAnchor" href="https://anonymous:flabada@example.org/some/where"> element is in the document
my $anchor = $doc->getElementByID("myAnchor");
$anchor->username; # returns 'anonymous'

See Mozilla documentation

METHODS

Inherits methods from its parent HTML::Object::DOM::Element

toString

Returns a string containing the whole URL. It is a synonym for HTMLAnchorElement.href, though it can't be used to modify the value.

Example:

# An <a id="myAnchor" href="https://example.org/some/where"> element is in the document
my $anchor = $doc->getElementById("myAnchor");
$anchor->toString(); # returns 'https://example.org/some/where'

See Mozilla documentation

AUTHOR

Jacques Deguest <jack@deguest.jp>

SEE ALSO

Mozilla documentation, Mozilla documentation on anchor element

COPYRIGHT & LICENSE

Copyright(c) 2021 DEGUEST Pte. Ltd.

All rights reserved

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.