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

url: extend URLSearchParams constructor #11060

Closed
wants to merge 8 commits into from

Conversation

TimothyGu
Copy link
Member

@TimothyGu TimothyGu commented Jan 28, 2017

In addition to URLSearchParams and USVString, support record<USVString, USVString> and sequence<sequence<USVString>> also.

Fixes: #10635
Refs: whatwg/url#175
Refs: web-platform-tests/wpt#4523

CI: https://ci.nodejs.org/job/node-test-pull-request/6089/

Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • tests and/or benchmarks are included
  • documentation is changed or added
  • commit message follows commit guidelines
Affected core subsystem(s)

url

@nodejs-github-bot nodejs-github-bot added dont-land-on-v4.x tools Issues and PRs related to the tools directory. whatwg-url Issues and PRs related to the WHATWG URL implementation. labels Jan 28, 2017
@addaleax addaleax added the semver-minor PRs that contain new features and should be released in the next minor version. label Jan 29, 2017
doc/api/url.md Outdated
getSingleQueryPair(2)
]);
console.log(params.toString());
// Prints 'user=abc&query=first&query=second'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The generator examples are good but I would guess that most people probably won’t be interested in that… how about wrapping it in a <details> tag? Does that work?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@addaleax, if that is the case I'd rather just remove it. I agree the second generator example is a bit obscure, and it is in fact only compatible with URLSearchParams, not with Map (referenced earlier). Though the first example does seem useful to me.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’m fine with any of those options… maybe somebody else has a stronger opinion but otherwise just go with whatever you prefer.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I kinda can't get the merit of the second generator example at first glance. Maybe we can leave that later with a better use case as an example?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@joyeecheung, fair.

Copy link
Member

@joyeecheung joyeecheung left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work! Left a few comments, please take a look.

if (init instanceof URLSearchParams) {
const childParams = init[searchParams];
this[searchParams] = childParams.slice();
if (init === null || init === undefined) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we have init = '' as default then init can't be undefined?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The user can do new URLSearchParams(undefined). The default is there to echo the = "" in the spec, though now I agree it's a bit pointless.

const childParams = init[searchParams];
this[searchParams] = childParams.slice();
if (init === null || init === undefined) {
// record<USVString, USVString>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment can be removed I believe.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

@@ -16,23 +16,24 @@ assert.strictEqual(params + '', 'a=b');
params = new URLSearchParams(params);
assert.strictEqual(params + '', 'a=b');

// URLSearchParams constructor, empty.
// URLSearchParams constructor, no arguments
params = new URLSearchParams();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have new URLSearchParams(null) test?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not yet, nor do we have a new URLSearchParams(undefined) test. Both are added.

doc/api/url.md Outdated
getSingleQueryPair(2)
]);
console.log(params.toString());
// Prints 'user=abc&query=first&query=second'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I kinda can't get the merit of the second generator example at first glance. Maybe we can leave that later with a better use case as an example?

const params = new URLSearchParams(val.input);
let i = 0;
for (const param of params) {
assert.deepStrictEqual(param, val.output[i],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about assert.deepStrictEqual(Array.from(params), val.output, ...)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea. Fixed also.

Copy link
Member

@jasnell jasnell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Woohoo! happy to see this. Left a couple of comments but otherwise LGTM

doc/api/url.md Outdated
@@ -525,7 +525,8 @@ value returned is equivalent to that of `url.href`.
### Class: URLSearchParams

The `URLSearchParams` object provides read and write access to the query of a
`URL`.
`URL`. It can also be used standalone with one of the four following
constructors.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a fan of starting a sentence with "It can..."... perhaps something like:

The object can also be used standalone...

doc/api/url.md Outdated
Instantiate a new `URLSearchParams` object with a query hash map. The key and
value of each property of `obj` are always coerced to strings.

Warning: Unlike [`querystring`][] module, duplicate keys in the form of array
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/^Warning:/^*Note*:/

@TimothyGu
Copy link
Member Author

@jasnell, done, PTAL. I also added some more examples for interoperation of URL and URLSearchParams in 25a60ad. Please take a look at that as well.

/cc @nodejs/url

@jasnell
Copy link
Member

jasnell commented Jan 31, 2017

@TimothyGu
Copy link
Member Author

Landed in cc48f21, 29f7587, and c505b81.

@TimothyGu TimothyGu closed this Feb 1, 2017
@TimothyGu TimothyGu deleted the urlsearchparams-ctor branch February 1, 2017 07:53
TimothyGu added a commit that referenced this pull request Feb 1, 2017
PR-URL: #11060
Fixes: #10635
Ref: whatwg/url#175
Ref: web-platform-tests/wpt#4523
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
TimothyGu added a commit that referenced this pull request Feb 1, 2017
PR-URL: #11060
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
TimothyGu added a commit that referenced this pull request Feb 1, 2017
PR-URL: #11060
Ref: whatwg/url#175
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
italoacasas pushed a commit to italoacasas/node that referenced this pull request Feb 2, 2017
PR-URL: nodejs#11060
Fixes: nodejs#10635
Ref: whatwg/url#175
Ref: web-platform-tests/wpt#4523
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
italoacasas pushed a commit to italoacasas/node that referenced this pull request Feb 2, 2017
PR-URL: nodejs#11060
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
italoacasas pushed a commit to italoacasas/node that referenced this pull request Feb 2, 2017
PR-URL: nodejs#11060
Ref: whatwg/url#175
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
@jasnell jasnell mentioned this pull request Apr 4, 2017
@TimothyGu
Copy link
Member Author

TimothyGu commented Apr 19, 2017

Added backport-requested for v7.x since it doesn't look like the spec is gonna change any time soon (re #11185 (comment)).

EDIT: it was backported in #12507, pending a merge there.

Copy link

@leylarezayiii leylarezayiii left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
semver-minor PRs that contain new features and should be released in the next minor version. tools Issues and PRs related to the tools directory. whatwg-url Issues and PRs related to the WHATWG URL implementation.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants