summaryrefslogtreecommitdiff
path: root/node_modules/swup/src/helpers/Location.ts
blob: aca9d6b6a05a1306fc90cac408b0339fe55b8f5c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/**
 * A helper for creating a Location from either an element
 * or a URL object/string
 *
 */
export class Location extends URL {
	constructor(url: URL | string, base: string = document.baseURI) {
		super(url.toString(), base);
		// Fix Safari bug with extending native classes
		Object.setPrototypeOf(this, Location.prototype);
	}

	/**
	 * The full local path including query params.
	 */
	get url(): string {
		return this.pathname + this.search;
	}

	/**
	 * Instantiate a Location from an element's href attribute
	 * @param el
	 * @returns new Location instance
	 */
	static fromElement(el: Element): Location {
		const href = el.getAttribute('href') || el.getAttribute('xlink:href') || '';
		return new Location(href);
	}

	/**
	 * Instantiate a Location from a URL object or string
	 * @param url
	 * @returns new Location instance
	 */
	static fromUrl(url: URL | string): Location {
		return new Location(url);
	}
}