google / closure-library

Google's common JavaScript library
code.google.com/closure/library/
Apache License 2.0
4.86k stars 1.04k forks source link

URIs with empty fragments are not preserved #1209

Open quoll opened 9 months ago

quoll commented 9 months ago

When the fragment portion of a URI is an empty string, then this is not distinguished from an undefined fragment.

Consequently, an empty fragment (a common construct in Semantic Web applications) is not preserved. For instance, the following returns true:

new goog.Uri("http://ex.test/path#").toString() == new goog.Uri("http://ex.test/path").toString()

This goes against RFC 3986, 5.3 Component Recomposition, which states:

Note that we are careful to preserve the distinction between a component that is undefined, meaning that its separator was not present in the reference, and a component that is empty, meaning that the separator was present and was immediately followed by the next component separator or the end of the reference.

In general, it would be simple to record a null or empty string on line 154, but the compatibility note points out that this may not work in IE.

trevorade commented 8 months ago

It doesn't appear to be possible to construct a native URL ending in '#' without setting the initial string to have one.

Setting URL.hash to the empty string results in no ending #.

In addition, new URL('http://example.com/test#').hash === new URL('http://example.com/test').hash returns true.

Using native APIs (barring toString), is there a way to distinguish window.location with and without the fragment token?