Skip to content

Should URL constructors prepend ? when initializing query objects? #248

Closed
@joyeecheung

Description

@joyeecheung

This question comes from the Node.js implementation, refs: nodejs/node#11093

The URL constructor specifies:

Set result's query object to a new
{{URLSearchParams}} object using query, and then set that query object's
url object to result

And the URLSearchParams constructor specifies:

If init is given, is a string, and starts with "?", remove the first
code point from init.

But referencing the getter spec of url.search:

Return "?", followed by context object's url's query.

one can infer that the query of an url should not have the leading ?, this means:

  1. The URL constructor would pass an query with leading ? stripped to new URLSearchParams
  2. The new URLSearchParams would strip a leading ? again, in any, but the WPT tests expect ??a=b&c=d to be serialized as %3Fa=b&c=d

AFAICT the URL constructor should prepend a ? here, if we don't introduce a special way to notify the URLSearchParams constructor not to strip the leading ?:

diff --git a/url.bs b/url.bs
index 7988aa2..f1a9c9d 100644
--- a/url.bs
+++ b/url.bs
@@ -2648,7 +2648,8 @@ when invoked, must run these steps:
  <li><p>Set <var>result</var>'s <a for=URL>url</a> to <var>parsedURL</var>.

  <li><p>Set <var>result</var>'s <a for=URL>query object</a> to a <a for=URLSearchParams>new</a>
- {{URLSearchParams}} object using <var>query</var>, and then set that <a for=URL>query object</a>'s
+ {{URLSearchParams}} object using <var>query</var> prepended with the
+ leading <code>?</code>, and then set that <a for=URL>query object</a>'s
  <a for=URLSearchParams>url object</a> to <var>result</var>.

  <li><p>Return <var>result</var>.

Also the definition of url.query in 4.1. URL representation can be a little bit more explicit about the leading ?. I can put up a PR if this is confirmed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions