All files / src/reactivity url.js

73.33% Statements 121/165
100% Branches 11/11
40.74% Functions 11/27
73.33% Lines 121/165

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 1661x 1x 1x 1x 1x 1x 1x 1x 12x 12x 12x 1x 1x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 3x 3x 4x 4x 1x 1x 1x 4x 4x         4x 4x         4x 4x     4x 4x       4x 4x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 4x 4x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x     4x 4x       4x 4x     4x 4x 1x 1x 1x 4x 4x     4x 4x       4x 4x     4x 4x       4x 4x 12x 12x 4x 4x 7x 7x 7x 7x 4x 4x     4x 4x       4x 4x           4x 4x 13x 13x 4x 4x     4x 4x     4x  
import { source, set } from '../internal/client/reactivity/sources.js';
import { get } from '../internal/client/runtime.js';
import { REPLACE, SvelteURLSearchParams } from './url-search-params.js';
 
/** @type {SvelteURL | null} */
let current_url = null;
 
export function get_current_url() {
	// ideally we'd just export `current_url` directly, but it seems Vitest doesn't respect live bindings
	return current_url;
}
 
export class SvelteURL extends URL {
	#protocol = source(super.protocol);
	#username = source(super.username);
	#password = source(super.password);
	#hostname = source(super.hostname);
	#port = source(super.port);
	#pathname = source(super.pathname);
	#hash = source(super.hash);
	#search = source(super.search);
	#searchParams;
 
	/**
	 * @param {string | URL} url
	 * @param {string | URL} [base]
	 */
	constructor(url, base) {
		url = new URL(url, base);
		super(url);
 
		current_url = this;
		this.#searchParams = new SvelteURLSearchParams(url.searchParams);
		current_url = null;
	}
 
	get hash() {
		return get(this.#hash);
	}
 
	set hash(value) {
		super.hash = value;
		set(this.#hash, super.hash);
	}
 
	get host() {
		get(this.#hostname);
		get(this.#port);
		return super.host;
	}
 
	set host(value) {
		super.host = value;
		set(this.#hostname, super.hostname);
		set(this.#port, super.port);
	}
 
	get hostname() {
		return get(this.#hostname);
	}
 
	set hostname(value) {
		super.hostname = value;
		set(this.#hostname, super.hostname);
	}
 
	get href() {
		get(this.#protocol);
		get(this.#username);
		get(this.#password);
		get(this.#hostname);
		get(this.#port);
		get(this.#pathname);
		get(this.#hash);
		get(this.#search);
		return super.href;
	}
 
	set href(value) {
		super.href = value;
		set(this.#protocol, super.protocol);
		set(this.#username, super.username);
		set(this.#password, super.password);
		set(this.#hostname, super.hostname);
		set(this.#port, super.port);
		set(this.#pathname, super.pathname);
		set(this.#hash, super.hash);
		set(this.#search, super.search);
		this.#searchParams[REPLACE](super.searchParams);
	}
 
	get password() {
		return get(this.#password);
	}
 
	set password(value) {
		super.password = value;
		set(this.#password, super.password);
	}
 
	get pathname() {
		return get(this.#pathname);
	}
 
	set pathname(value) {
		super.pathname = value;
		set(this.#pathname, super.pathname);
	}
 
	get port() {
		return get(this.#port);
	}
 
	set port(value) {
		super.port = value;
		set(this.#port, super.port);
	}
 
	get protocol() {
		return get(this.#protocol);
	}
 
	set protocol(value) {
		super.protocol = value;
		set(this.#protocol, super.protocol);
	}
 
	get search() {
		return get(this.#search);
	}
 
	set search(value) {
		super.search = value;
		set(this.#search, value);
		this.#searchParams[REPLACE](super.searchParams);
	}
 
	get username() {
		return get(this.#username);
	}
 
	set username(value) {
		super.username = value;
		set(this.#username, super.username);
	}
 
	get origin() {
		get(this.#protocol);
		get(this.#hostname);
		get(this.#port);
		return super.origin;
	}
 
	get searchParams() {
		return this.#searchParams;
	}
 
	toString() {
		return this.href;
	}
 
	toJSON() {
		return this.href;
	}
}