All files / src/reactivity url.js

80.67% Statements 238/295
100% Branches 28/28
51.16% Functions 22/43
80.41% Lines 234/291

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 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 2922x 2x 2x 2x 2x 2x 2x 2x 2x 2x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 2x 2x 2x   2x 2x 4x 4x 4x 3x 3x 4x 4x 1x 1x 1x 4x 4x 10x 10x 10x 10x 4x 4x         4x 4x     4x 4x 2x 2x 2x 4x 4x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 4x 4x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 4x 4x     4x 4x       4x 4x 10x 10x 4x 4x 3x 3x 3x 4x 4x     4x 4x       4x 4x     4x 4x       4x 4x 15x 15x 15x 4x 4x 3x 3x 3x 4x 4x     4x 4x       4x 4x           4x 4x 22x 22x 4x 4x     4x 4x     4x 2x 2x 5x 5x 5x 14x 14x 5x 5x 5x 5x 2x 2x 2x   2x 2x 5x 5x 5x 5x 5x 5x 10x 6x 6x 10x 10x 10x 10x 10x 10x 10x 5x 5x 5x 5x 5x 5x 5x 2x 2x 2x 5x 5x 5x 5x 5x 5x 5x 1x 1x 1x 5x 5x 5x 5x 5x 5x 16x 16x 16x 5x 5x 5x 5x 5x 5x       5x 5x 5x 5x 5x 5x 5x 4x 4x 4x 5x 5x       5x 5x 5x 5x 5x 5x 5x 1x 1x 1x 5x 5x       5x 5x 28x 28x 28x 5x 5x       5x 5x       5x 5x     5x 5x       5x  
import { DEV } from 'esm-env';
import { INSPECT_SYMBOL } from '../internal/client/constants.js';
import { source, set } from '../internal/client/reactivity/sources.js';
import { get } from '../internal/client/runtime.js';
 
const REPLACE = Symbol();
var inited_url = false;
var inited_search_params = false;
 
export class ReactiveURL 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);
	#searchParams = new ReactiveURLSearchParams();
 
	/**
	 * @param {string | URL} url
	 * @param {string | URL} [base]
	 */
	constructor(url, base) {
		url = new URL(url, base);
		super(url);
		this.#searchParams[REPLACE](url.searchParams);
 
		if (DEV && !inited_url) {
			inited_url = true;
			// @ts-ignore
			ReactiveURL.prototype[INSPECT_SYMBOL] = function () {
				this.href;
			};
		}
	}
 
	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);
		this.#searchParams.toString();
		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);
		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() {
		const search = this.#searchParams?.toString();
		return search ? `?${search}` : '';
	}
 
	set search(value) {
		super.search = value;
		this.#searchParams[REPLACE](new URLSearchParams(value.replace(/^\?/, '')));
	}
 
	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;
	}
}
 
export class ReactiveURLSearchParams extends URLSearchParams {
	#version = source(0);
 
	#increment_version() {
		set(this.#version, this.#version.v + 1);
	}
 
	constructor() {
		super();
		if (DEV && !inited_search_params) {
			inited_search_params = true;
			// @ts-ignore
			ReactiveURLSearchParams.prototype[INSPECT_SYMBOL] = function () {
				get(this.#version);
			};
		}
	}
 
	/**
	 * @param {URLSearchParams} params
	 */
	[REPLACE](params) {
		for (const key of [...super.keys()]) {
			super.delete(key);
		}
 
		for (const [key, value] of params) {
			super.append(key, value);
		}
 
		this.#increment_version();
	}
 
	/**
	 * @param {string} name
	 * @param {string} value
	 * @returns {void}
	 */
	append(name, value) {
		this.#increment_version();
		return super.append(name, value);
	}
 
	/**
	 * @param {string} name
	 * @param {string=} value
	 * @returns {void}
	 */
	delete(name, value) {
		this.#increment_version();
		return super.delete(name, value);
	}
 
	/**
	 * @param {string} name
	 * @returns {string|null}
	 */
	get(name) {
		get(this.#version);
		return super.get(name);
	}
 
	/**
	 * @param {string} name
	 * @returns {string[]}
	 */
	getAll(name) {
		get(this.#version);
		return super.getAll(name);
	}
 
	/**
	 * @param {string} name
	 * @param {string=} value
	 * @returns {boolean}
	 */
	has(name, value) {
		get(this.#version);
		return super.has(name, value);
	}
 
	keys() {
		get(this.#version);
		return super.keys();
	}
 
	/**
	 * @param {string} name
	 * @param {string} value
	 * @returns {void}
	 */
	set(name, value) {
		this.#increment_version();
		return super.set(name, value);
	}
 
	sort() {
		this.#increment_version();
		return super.sort();
	}
 
	toString() {
		get(this.#version);
		return super.toString();
	}
 
	values() {
		get(this.#version);
		return super.values();
	}
 
	entries() {
		get(this.#version);
		return super.entries();
	}
 
	[Symbol.iterator]() {
		return this.entries();
	}
 
	get size() {
		get(this.#version);
		return super.size;
	}
}