deno.land / x / deno@v1.28.2 / ext / web / 12_location.js
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license."use strict";
/// <reference path="../../core/internal.d.ts" />
((window) => { const { URL } = window.__bootstrap.url; const { DOMException } = window.__bootstrap.domException; const { Error, ObjectDefineProperties, Symbol, SymbolFor, SymbolToStringTag, TypeError, WeakMap, WeakMapPrototypeGet, WeakMapPrototypeSet, } = window.__bootstrap.primordials;
const locationConstructorKey = Symbol("locationConstuctorKey");
// The differences between the definitions of `Location` and `WorkerLocation` // are because of the `LegacyUnforgeable` attribute only specified upon // `Location`'s properties. See: // - https://html.spec.whatwg.org/multipage/history.html#the-location-interface // - https://heycam.github.io/webidl/#LegacyUnforgeable class Location { constructor(href = null, key = null) { if (key != locationConstructorKey) { throw new TypeError("Illegal constructor."); } const url = new URL(href); url.username = ""; url.password = ""; ObjectDefineProperties(this, { hash: { get() { return url.hash; }, set() { throw new DOMException( `Cannot set "location.hash".`, "NotSupportedError", ); }, enumerable: true, }, host: { get() { return url.host; }, set() { throw new DOMException( `Cannot set "location.host".`, "NotSupportedError", ); }, enumerable: true, }, hostname: { get() { return url.hostname; }, set() { throw new DOMException( `Cannot set "location.hostname".`, "NotSupportedError", ); }, enumerable: true, }, href: { get() { return url.href; }, set() { throw new DOMException( `Cannot set "location.href".`, "NotSupportedError", ); }, enumerable: true, }, origin: { get() { return url.origin; }, enumerable: true, }, pathname: { get() { return url.pathname; }, set() { throw new DOMException( `Cannot set "location.pathname".`, "NotSupportedError", ); }, enumerable: true, }, port: { get() { return url.port; }, set() { throw new DOMException( `Cannot set "location.port".`, "NotSupportedError", ); }, enumerable: true, }, protocol: { get() { return url.protocol; }, set() { throw new DOMException( `Cannot set "location.protocol".`, "NotSupportedError", ); }, enumerable: true, }, search: { get() { return url.search; }, set() { throw new DOMException( `Cannot set "location.search".`, "NotSupportedError", ); }, enumerable: true, }, ancestorOrigins: { get() { // TODO(nayeemrmn): Replace with a `DOMStringList` instance. return { length: 0, item: () => null, contains: () => false, }; }, enumerable: true, }, assign: { value: function assign() { throw new DOMException( `Cannot call "location.assign()".`, "NotSupportedError", ); }, enumerable: true, }, reload: { value: function reload() { throw new DOMException( `Cannot call "location.reload()".`, "NotSupportedError", ); }, enumerable: true, }, replace: { value: function replace() { throw new DOMException( `Cannot call "location.replace()".`, "NotSupportedError", ); }, enumerable: true, }, toString: { value: function toString() { return url.href; }, enumerable: true, }, [SymbolFor("Deno.privateCustomInspect")]: { value: function (inspect) { const object = { hash: this.hash, host: this.host, hostname: this.hostname, href: this.href, origin: this.origin, pathname: this.pathname, port: this.port, protocol: this.protocol, search: this.search, }; return `${this.constructor.name} ${inspect(object)}`; }, }, }); } }
ObjectDefineProperties(Location.prototype, { [SymbolToStringTag]: { value: "Location", configurable: true, }, });
const workerLocationUrls = new WeakMap();
class WorkerLocation { constructor(href = null, key = null) { if (key != locationConstructorKey) { throw new TypeError("Illegal constructor."); } const url = new URL(href); url.username = ""; url.password = ""; WeakMapPrototypeSet(workerLocationUrls, this, url); } }
ObjectDefineProperties(WorkerLocation.prototype, { hash: { get() { const url = WeakMapPrototypeGet(workerLocationUrls, this); if (url == null) { throw new TypeError("Illegal invocation."); } return url.hash; }, configurable: true, enumerable: true, }, host: { get() { const url = WeakMapPrototypeGet(workerLocationUrls, this); if (url == null) { throw new TypeError("Illegal invocation."); } return url.host; }, configurable: true, enumerable: true, }, hostname: { get() { const url = WeakMapPrototypeGet(workerLocationUrls, this); if (url == null) { throw new TypeError("Illegal invocation."); } return url.hostname; }, configurable: true, enumerable: true, }, href: { get() { const url = WeakMapPrototypeGet(workerLocationUrls, this); if (url == null) { throw new TypeError("Illegal invocation."); } return url.href; }, configurable: true, enumerable: true, }, origin: { get() { const url = WeakMapPrototypeGet(workerLocationUrls, this); if (url == null) { throw new TypeError("Illegal invocation."); } return url.origin; }, configurable: true, enumerable: true, }, pathname: { get() { const url = WeakMapPrototypeGet(workerLocationUrls, this); if (url == null) { throw new TypeError("Illegal invocation."); } return url.pathname; }, configurable: true, enumerable: true, }, port: { get() { const url = WeakMapPrototypeGet(workerLocationUrls, this); if (url == null) { throw new TypeError("Illegal invocation."); } return url.port; }, configurable: true, enumerable: true, }, protocol: { get() { const url = WeakMapPrototypeGet(workerLocationUrls, this); if (url == null) { throw new TypeError("Illegal invocation."); } return url.protocol; }, configurable: true, enumerable: true, }, search: { get() { const url = WeakMapPrototypeGet(workerLocationUrls, this); if (url == null) { throw new TypeError("Illegal invocation."); } return url.search; }, configurable: true, enumerable: true, }, toString: { value: function toString() { const url = WeakMapPrototypeGet(workerLocationUrls, this); if (url == null) { throw new TypeError("Illegal invocation."); } return url.href; }, configurable: true, enumerable: true, writable: true, }, [SymbolToStringTag]: { value: "WorkerLocation", configurable: true, }, [SymbolFor("Deno.privateCustomInspect")]: { value: function (inspect) { const object = { hash: this.hash, host: this.host, hostname: this.hostname, href: this.href, origin: this.origin, pathname: this.pathname, port: this.port, protocol: this.protocol, search: this.search, }; return `${this.constructor.name} ${inspect(object)}`; }, }, });
let location = undefined; let workerLocation = undefined;
function setLocationHref(href) { location = new Location(href, locationConstructorKey); workerLocation = new WorkerLocation(href, locationConstructorKey); }
window.__bootstrap.location = { locationConstructorDescriptor: { value: Location, configurable: true, writable: true, }, workerLocationConstructorDescriptor: { value: WorkerLocation, configurable: true, writable: true, }, locationDescriptor: { get() { return location; }, set() { throw new DOMException(`Cannot set "location".`, "NotSupportedError"); }, enumerable: true, }, workerLocationDescriptor: { get() { if (workerLocation == null) { throw new Error( `Assertion: "globalThis.location" must be defined in a worker.`, ); } return workerLocation; }, configurable: true, enumerable: true, }, setLocationHref, getLocationHref() { return location?.href; }, };})(this);
Version Info