Skip to content

Commit

Permalink
url: ensure ? is present when relying on URLSearchParams
Browse files Browse the repository at this point in the history
  • Loading branch information
MattIPv4 committed Jan 19, 2024
1 parent aadda98 commit 2d6f0e5
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions lib/internal/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -794,13 +794,19 @@ class URL {
}

#getContextSearch() {
if (!this.#context.hasSearch) { return ''; }
if (!this.#context.hasSearch) return '';
let endsAt = this.#context.href.length;
if (this.#context.hasHash) { endsAt = this.#context.hash_start; }
if (endsAt - this.#context.search_start <= 1) { return ''; }
if (this.#context.hasHash) endsAt = this.#context.hash_start;
if (endsAt - this.#context.search_start <= 1) return '';
return StringPrototypeSlice(this.#context.href, this.#context.search_start, endsAt);
}

#getParamsSearch() {
if (!this.#searchParams) return '';
if (!this.#searchParams.size) return '';
return `?${this.#searchParams.toString()}`;
}

#updateContext(href) {
this.#context.href = href;

Expand Down Expand Up @@ -839,7 +845,7 @@ class URL {
// URLSearchParams is used on demand, and stores the search if it's used.
if (this.#searchParams) {
return StringPrototypeSlice(this.#context.href, 0, this.#context.hash_start - 1) +
this.#searchParams.toString() +
this.#getParamsSearch() +
StringPrototypeSlice(this.#context.href, this.#context.hash_start);
}
return this.#context.href;
Expand Down Expand Up @@ -992,7 +998,7 @@ class URL {

get search() {
// URLSearchParams is used on demand, and stores the search if it's used.
if (this.#searchParams) return this.#searchParams.toString();
if (this.#searchParams) return this.#getParamsSearch();
return this.#getContextSearch();
}

Expand Down

0 comments on commit 2d6f0e5

Please sign in to comment.