happy-dom
Version:
Happy DOM is a JavaScript implementation of a web browser without its graphical user interface. It includes many web standards from WHATWG DOM and HTML.
522 lines • 11.7 kB
JavaScript
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
var _a, _b;
Object.defineProperty(exports, "__esModule", { value: true });
const HTMLElement_js_1 = __importDefault(require("../html-element/HTMLElement.cjs"));
const PropertySymbol = __importStar(require("../../PropertySymbol.cjs"));
const DOMTokenList_js_1 = __importDefault(require("../../dom-token-list/DOMTokenList.cjs"));
const URL_js_1 = __importDefault(require("../../url/URL.cjs"));
const HTMLAnchorElementNamedNodeMap_js_1 = __importDefault(require("./HTMLAnchorElementNamedNodeMap.cjs"));
const EventPhaseEnum_js_1 = __importDefault(require("../../event/EventPhaseEnum.cjs"));
const MouseEvent_js_1 = __importDefault(require("../../event/events/MouseEvent.cjs"));
/**
* HTML Anchor Element.
*
* Reference:
* https://developer.mozilla.org/en-US/docs/Web/API/HTMLAnchorElement.
*/
class HTMLAnchorElement extends HTMLElement_js_1.default {
constructor() {
super(...arguments);
this[_a] = new HTMLAnchorElementNamedNodeMap_js_1.default(this);
this[_b] = null;
}
/**
* Returns download.
*
* @returns download.
*/
get download() {
return this.getAttribute('download') || '';
}
/**
* Sets download.
*
* @param download Download.
*/
set download(download) {
this.setAttribute('download', download);
}
/**
* Returns hash.
*
* @returns Hash.
*/
get hash() {
const href = this.getAttribute('href');
if (href.startsWith('#')) {
return href;
}
let url;
try {
url = new URL_js_1.default(this.href);
}
catch (e) {
return '';
}
return url.hash;
}
/**
* Sets hash.
*
* @param hash Hash.
*/
set hash(hash) {
let url;
try {
url = new URL_js_1.default(this.href);
}
catch (e) {
return;
}
url.hash = hash;
this.href = url.href;
}
/**
* Returns href.
*
* @returns Href.
*/
get href() {
if (!this.hasAttribute('href')) {
return '';
}
try {
return new URL_js_1.default(this.getAttribute('href'), this[PropertySymbol.ownerDocument].location.href)
.href;
}
catch (e) {
return this.getAttribute('href');
}
}
/**
* Sets href.
*
* @param href Href.
*/
set href(href) {
this.setAttribute('href', href);
}
/**
* Returns hreflang.
*
* @returns Hreflang.
*/
get hreflang() {
return this.getAttribute('hreflang') || '';
}
/**
* Sets hreflang.
*
* @param hreflang Hreflang.
*/
set hreflang(hreflang) {
this.setAttribute('hreflang', hreflang);
}
/**
* Returns the hyperlink's URL's origin.
*
* @returns Origin.
*/
get origin() {
try {
return new URL_js_1.default(this.href).origin;
}
catch (e) {
return '';
}
}
/**
* Returns ping.
*
* @returns Ping.
*/
get ping() {
return this.getAttribute('ping') || '';
}
/**
* Sets ping.
*
* @param ping Ping.
*/
set ping(ping) {
this.setAttribute('ping', ping);
}
/**
* Returns protocol.
*
* @returns Protocol.
*/
get protocol() {
try {
return new URL_js_1.default(this.href).protocol;
}
catch (e) {
return '';
}
}
/**
* Sets protocol.
*
* @param protocol Protocol.
*/
set protocol(protocol) {
let url;
try {
url = new URL_js_1.default(this.href);
}
catch (e) {
return;
}
url.protocol = protocol;
this.href = url.href;
}
/**
* Returns username.
*
* @returns Username.
*/
get username() {
try {
return new URL_js_1.default(this.href).username;
}
catch (e) {
return '';
}
}
/**
* Sets username.
*
* @param username Username.
*/
set username(username) {
let url;
try {
url = new URL_js_1.default(this.href);
}
catch (e) {
return;
}
url.username = username;
this.href = url.href;
}
/**
* Returns password.
*
* @returns Password.
*/
get password() {
try {
return new URL_js_1.default(this.href).password;
}
catch (e) {
return '';
}
}
/**
* Sets password.
*
* @param password Password.
*/
set password(password) {
let url;
try {
url = new URL_js_1.default(this.href);
}
catch (e) {
return;
}
url.password = password;
this.href = url.href;
}
/**
* Returns pathname.
*
* @returns Pathname.
*/
get pathname() {
try {
return new URL_js_1.default(this.href).pathname;
}
catch (e) {
return '';
}
}
/**
* Sets pathname.
*
* @param pathname Pathname.
*/
set pathname(pathname) {
let url;
try {
url = new URL_js_1.default(this.href);
}
catch (e) {
return;
}
url.pathname = pathname;
this.href = url.href;
}
/**
* Returns port.
*
* @returns Port.
*/
get port() {
try {
return new URL_js_1.default(this.href).port;
}
catch (e) {
return '';
}
}
/**
* Sets port.
*
* @param port Port.
*/
set port(port) {
let url;
try {
url = new URL_js_1.default(this.href);
}
catch (e) {
return;
}
url.port = port;
this.href = url.href;
}
/**
* Returns host.
*
* @returns Host.
*/
get host() {
try {
return new URL_js_1.default(this.href).host;
}
catch (e) {
return '';
}
}
/**
* Sets host.
*
* @param host Host.
*/
set host(host) {
let url;
try {
url = new URL_js_1.default(this.href);
}
catch (e) {
return;
}
url.host = host;
this.href = url.href;
}
/**
* Returns hostname.
*
* @returns Hostname.
*/
get hostname() {
try {
return new URL_js_1.default(this.href).hostname;
}
catch (e) {
return '';
}
}
/**
* Sets hostname.
*
* @param hostname Hostname.
*/
set hostname(hostname) {
let url;
try {
url = new URL_js_1.default(this.href);
}
catch (e) {
return;
}
url.hostname = hostname;
this.href = url.href;
}
/**
* Returns referrerPolicy.
*
* @returns Referrer Policy.
*/
get referrerPolicy() {
return this.getAttribute('referrerPolicy') || '';
}
/**
* Sets referrerPolicy.
*
* @param referrerPolicy Referrer Policy.
*/
set referrerPolicy(referrerPolicy) {
this.setAttribute('referrerPolicy', referrerPolicy);
}
/**
* Returns rel.
*
* @returns Rel.
*/
get rel() {
return this.getAttribute('rel') || '';
}
/**
* Sets rel.
*
* @param rel Rel.
*/
set rel(rel) {
this.setAttribute('rel', rel);
}
/**
* Returns rel list.
*
* @returns Rel list.
*/
get relList() {
if (!this[PropertySymbol.relList]) {
this[PropertySymbol.relList] = new DOMTokenList_js_1.default(this, 'rel');
}
return this[PropertySymbol.relList];
}
/**
* Returns search.
*
* @returns Search.
*/
get search() {
try {
return new URL_js_1.default(this.href).search;
}
catch (e) {
return '';
}
}
/**
* Sets search.
*
* @param search Search.
*/
set search(search) {
let url;
try {
url = new URL_js_1.default(this.href);
}
catch (e) {
return;
}
url.search = search;
this.href = url.href;
}
/**
* Returns target.
*
* @returns target.
*/
get target() {
return this.getAttribute('target') || '';
}
/**
* Sets target.
*
* @param target Target.
*/
set target(target) {
this.setAttribute('target', target);
}
/**
* Returns text.
*
* @returns text.
*/
get text() {
return this.textContent;
}
/**
* Sets text.
*
* @param text Text.
*/
set text(text) {
this.textContent = text;
}
/**
* Returns type.
*
* @returns Type.
*/
get type() {
return this.getAttribute('type') || '';
}
/**
* Sets type.
*
* @param type Type.
*/
set type(type) {
this.setAttribute('type', type);
}
/**
* @override
*/
toString() {
return this.href;
}
/**
* @override
*/
dispatchEvent(event) {
const returnValue = super.dispatchEvent(event);
if (event.type === 'click' &&
event instanceof MouseEvent_js_1.default &&
(event.eventPhase === EventPhaseEnum_js_1.default.atTarget ||
event.eventPhase === EventPhaseEnum_js_1.default.bubbling) &&
!event.defaultPrevented) {
const href = this.href;
if (href) {
this[PropertySymbol.ownerDocument][PropertySymbol.ownerWindow].open(href, this.target || '_self');
if (this[PropertySymbol.ownerDocument][PropertySymbol.ownerWindow].closed) {
event.stopImmediatePropagation();
}
}
}
return returnValue;
}
}
_a = PropertySymbol.attributes, _b = PropertySymbol.relList;
exports.default = HTMLAnchorElement;
//# sourceMappingURL=HTMLAnchorElement.cjs.map