-
Notifications
You must be signed in to change notification settings - Fork 139
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
Differentiate from zero-sized fragment and no fragment in url #779
Comments
We cannot change the existing API, but I'm somewhat supportive of adding API surface for this as it is indeed hidden information. For Having said that, is there evidence on Stack Overflow or in popular JS libraries that this is a shortcoming people have to work around? |
I found the problem while looking at how the url fragment is supported across languages while working at another standard, so I cannot tell you how widespread this need is within JS, I guess we'll have to make a note and signal the pitfall. What is surprising me even more is that you do not get what you set. let url = new URL("scheme://host/path/");
console.log(url.hash);
url.hash = "#";
console.log(url.toString()); // -> scheme://host/path/#
console.log(url.hash); // -> ''
url.hash = "#a";
console.log(url.toString()); // -> scheme://host/path/#a
console.log(url.hash); // -> '#a' |
I agree that this part of the JS URL API is awkward. To give another data point: in my library WebURL, which implements the WHATWG standard in Swift, I made this change ("not present" is communicated as
I appreciate that the JS API cannot be changed at this point, though. |
Host has this problem too.
There is this classic post according to which query and fragment have been in use fairly consistently to refer to the search without the So one option is to fix search and hash and make them available as query and fragment instead. The search and hash getters / setters can then be marked as legacy or deprecated (but not removed). |
I've run into this problem myself, in multiple projects and libraries, in both Node & browsers. Right now I'm building developer tools, where URLs are taken as string input, parsed, and manipulated by component, and preserving the raw formatting where possible is useful. Not being able to differentiate between Of course this state does exist within the URL parser (the URL's internal query and fragment states in the spec do store empty & null differently) but it's just not currently exposed the same way in Totally understand that changing the existing API is impractical. Either of the options proposed here so far would work well in scenarios like mine:
The latter is definitely more convenient as a user ( |
and
if fed to the
URL
do not distinguish between the two: URL.hash returns''
and to make it even stranger passing
.hash = '#'
producesscheme://host:port/#
but calling.hash
returns''
nonetheless.would be nicer if
.hash
returnsundefined
/null
if it is unset or"#"
if the trailing hash is present.The text was updated successfully, but these errors were encountered: