It is wrong to assume that path can be parsed as a URI-reference.
path-abempty cannot match the URI grammar rule so ends up matching relative-ref
In relative-part, a string of the form //.* can only match the production rule "//" authority path-abempty. This rule expect a non-empty authority which lead to the rejection of "//". This also means that any first segment of a path-abempty would be interpreted as an authority
Considering that the type URLPath represents paths, it seems wrong that its fromString function considers its argument as URI-reference-first.
Proposed solution
Rename fromString to fromURLString
Reimplement fromString to only accept strings of the form path-abempty
The second point can be achieved by the following implementation[^1] which reuses existings URL's parsing.
Context
Here's the grammar specification for URLs (rfc3986):
URL
'spathname
getterThe algorithm used to generate
pathname
ensures that apathname
can only match eitherpath-abempty
orpath-empty
(path-empty
)URL
's constructorThe
URL
constructor expect its arguments to be URLs and parses them according to theURI-reference
grammar rule.Issue
It is wrong to assume that
path
can be parsed as aURI-reference
.path-abempty
cannot match theURI
grammar rule so ends up matchingrelative-ref
relative-part
, a string of the form//.*
can only match the production rule"//" authority path-abempty
. This rule expect a non-empty authority which lead to the rejection of"//"
. This also means that any first segment of apath-abempty
would be interpreted as an authorityConsidering that the type
URLPath
represents paths, it seems wrong that itsfromString
function considers its argument asURI-reference
-first.Proposed solution
fromString
tofromURLString
fromString
to only accept strings of the formpath-abempty
The second point can be achieved by the following implementation[^1] which reuses existings URL's parsing.
[^1]: code by @mlegenhausen