Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clarify that Location.assign and Location.replace also accept URLs #36432

Merged
merged 5 commits into from
Oct 26, 2024

Conversation

silverwind
Copy link
Contributor

@silverwind silverwind commented Oct 22, 2024

Description

Location.assign and Location.replace also accept URLs

Motivation

This works in all browsers I tested (Firefox, Chrome, Safari, PaleMoon), so MDN was incorrect.

Additional details

TypeScript has it correct already:

https://github.com/microsoft/TypeScript/blob/e6ef279403d86440600c866d53839a3e695220d3/src/lib/dom.generated.d.ts#L15107
https://github.com/microsoft/TypeScript/blob/e6ef279403d86440600c866d53839a3e695220d3/src/lib/dom.generated.d.ts#L15119

@silverwind silverwind requested a review from a team as a code owner October 22, 2024 00:10
@silverwind silverwind requested review from sideshowbarker and removed request for a team October 22, 2024 00:10
@github-actions github-actions bot added Content:WebAPI Web API docs size/xs [PR only] 0-5 LoC changed labels Oct 22, 2024
Copy link
Contributor

github-actions bot commented Oct 22, 2024

Preview URLs

(comment last updated: 2024-10-26 18:33:22)

@Josh-Cena
Copy link
Member

There's nothing special about passing URLs to these APIs. They merely coerce everything to strings. Check the spec: https://html.spec.whatwg.org/multipage/nav-history-apis.html#the-location-interface undefined assign(USVString url);

The reason TypeScript needs to declare the type like this is because it doesn't have a type that means "all objects with sensible toString behavior" and presumably someone complained that they can't pass a URL directly without converting it to string first. But as far as the IDL and the underlying implementation is concerned, URL is not a "blessed type" as an input.

@Josh-Cena Josh-Cena closed this Oct 22, 2024
@silverwind
Copy link
Contributor Author

Saying it accepts "a string" is still incorrect, thought. Maybe it should say "a string, or any object with a .toString() method"?

@Josh-Cena Josh-Cena reopened this Oct 22, 2024
@Josh-Cena
Copy link
Member

I think we have precedents of doing similar stuff elsewhere, so you may want to search for one, but the idea makes sense to me.

@sideshowbarker
Copy link
Collaborator

I think we have precedents of doing similar stuff elsewhere, so you may want to search for one, but the idea makes sense to me.

Yeah, for one relevant precedent, see the wording about the url param in the https://developer.mozilla.org/en-US/docs/Web/API/URL/URL#url section of the “URL: URL() constructor” article:

A string or any other object with a stringifier that represents an absolute URL or a relative reference to a base URL.

And in that same article, see the note at https://developer.mozilla.org/en-US/docs/Web/API/URL/URL#sect2:

The url and base arguments will each be stringified from whatever value you pass, such as an HTMLAnchorElement or HTMLAreaElement element, just like with other Web APIs that accept a string. In particular, you can use an existing URL object for either argument, and it will be stringified from the object's href property.

@silverwind
Copy link
Contributor Author

I think we have precedents of doing similar stuff elsewhere, so you may want to search for one, but the idea makes sense to me.

Yeah, for one relevant precedent, see the wording about the url param in the https://developer.mozilla.org/en-US/docs/Web/API/URL/URL#url section of the “URL: URL() constructor” article:

A string or any other object with a stringifier that represents an absolute URL or a relative reference to a base URL.

And in that same article, see the note at https://developer.mozilla.org/en-US/docs/Web/API/URL/URL#sect2:

The url and base arguments will each be stringified from whatever value you pass, such as an HTMLAnchorElement or HTMLAreaElement element, just like with other Web APIs that accept a string. In particular, you can use an existing URL object for either argument, and it will be stringified from the object's href property.

I agree and have changed to use the form "'A string or any other object with a stringifier'" of that wording now.

silverwind and others added 2 commits October 23, 2024 11:01
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
files/en-us/web/api/location/assign/index.md Outdated Show resolved Hide resolved
files/en-us/web/api/location/replace/index.md Outdated Show resolved Hide resolved
@Josh-Cena Josh-Cena merged commit 2e40017 into mdn:main Oct 26, 2024
8 checks passed
@silverwind silverwind deleted the url-loc branch October 28, 2024 19:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Content:WebAPI Web API docs size/xs [PR only] 0-5 LoC changed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants