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

doc: add basic documentation for WHATWG URL API #10620

Closed
wants to merge 2 commits into from

Conversation

jasnell
Copy link
Member

@jasnell jasnell commented Jan 4, 2017

Add documentation for the WHATWG URL API

Checklist
  • documentation is changed or added
  • commit message follows commit guidelines
Affected core subsystem(s)

doc,url

@nodejs-github-bot nodejs-github-bot added doc Issues and PRs related to the documentations. url Issues and PRs related to the legacy built-in url module. lts-watch-v6.x labels Jan 4, 2017
@@ -250,7 +250,308 @@ properties of URL objects:
For example, the ASCII space character (`' '`) is encoded as `%20`. The ASCII
forward slash (`/`) character is encoded as `%3C`.

## The WHATWG URL API

The `url` module provides an experimental implementation of the
Copy link
Contributor

Choose a reason for hiding this comment

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

define experimental, perhaps by including another > Stability: ... stanza here.

console.log(myURL.href);
// Prints http://example.org/foo

myURL.href = 'https://example.com/bar'
Copy link
Contributor

Choose a reason for hiding this comment

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

What does this actually do? Does it cause all the other properties to be re-evaluated as if the myURL had been constructuted from this new URL string?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes

```js
const myURL = new URL('http://example.org/foo/bar?baz');
console.log(myURL.origin);
// Prints http://exmaple.org
Copy link
Contributor

Choose a reason for hiding this comment

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

"example"


#### url.origin

Gets the read-only Unicode serialization of the URL's origin.
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is it so specific about Unicode seialization here, and not in the other string properties? This one is different?

Copy link
Member

Choose a reason for hiding this comment

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

That one is specificly pointed out in the spec https://url.spec.whatwg.org/#dom-url-origin

The origin attribute’s getter must return the Unicode serialization of context object’s url’s origin. [HTML]

It returns the Unicode rather than the ASCII serialization for compatibility with HTML’s MessageEvent feature. [HTML]


#### url.port

Gets and sets the port portion of the URL.
Copy link
Contributor

Choose a reason for hiding this comment

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

if delete url.port, does it become "http://example.org"?

Copy link
Member

Choose a reason for hiding this comment

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

It has no effect.

Copy link
Member

Choose a reason for hiding this comment

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

To be more precise: it has no effect in sloppy mode and throws in strict mode. I think it deserves a note somewhere that to remove writable properties, one must set an empty value.

Copy link
Member Author

@jasnell jasnell Jan 5, 2017

Choose a reason for hiding this comment

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

hmm.. it does not appear to throw in strict mode... always appears to return true but have no effect.

'use strict';
const URL = require('url').URL;
const u = new URL('http://example.org:123/abc');
console.log(delete u.port);
console.log(u.href);

Copy link
Member

Choose a reason for hiding this comment

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

That's because port is not a property on u, but rather u.[[Prototype]] (i.e. URL.prototype), so delete u.port will not affect the object.

However, since URL.prototype.port is configurable, you can actually delete URL.prototype.port in both strict and sloppy modes.

Copy link
Member

Choose a reason for hiding this comment

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

I see. Sorry for the misunderstanding


#### url.protocol

Gets and sets the protocol portion of the URL.
Copy link
Contributor

Choose a reason for hiding this comment

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

what if you delete it? actually, for all the properties, what happens if they are deleted?

also, what happens if you assign an invalid value? does it throw, like it does in construction?

Copy link
Member

Choose a reason for hiding this comment

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

Could add a hint about all the attributes of properties https://heycam.github.io/webidl/#es-attributes at the top or somewhere (all enumerable: true and configurable: false for URL properties, writable depends on read-only-ness).

Copy link
Member Author

Choose a reason for hiding this comment

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

Potentially but we do not typically go into that level of detail for most Node.js properties and the original spec is already pretty well defined.


#### url.search

Gets and sets the serialized querystring portion of the URL.
Copy link
Contributor

Choose a reason for hiding this comment

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

serialized, as opposed to ...?

Copy link
Member Author

Choose a reason for hiding this comment

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

As opposed to searchParams

#### url.searchParams

Gets a `URLSearchParams` object representing the querystring parameters of the
URL.
Copy link
Contributor

Choose a reason for hiding this comment

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

should that be linked to the def'n of URLSearchParams?

exported by the object, they are not considered to be part of the "public"
API of the object.

*Note*: The origin object and the `require('url').originFor()` API are not part
Copy link
Contributor

Choose a reason for hiding this comment

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

Why are they here, then, are they going to be deprecated and removed?

Copy link
Contributor

Choose a reason for hiding this comment

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

same question for the APIs below

Copy link
Member Author

Choose a reason for hiding this comment

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

No, these are added as part of the new experimental implementation. They are just not part of the standard

@jasnell
Copy link
Member Author

jasnell commented Jan 5, 2017

Updated to address nits


Using the `delete` keyword (e.g. `delete myURL.hash`) has no effect.

Invalid URL characters included in the value assigned to the `hash` property
Copy link
Member

Choose a reason for hiding this comment

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

Maybe worth pointing out that the additional escaped characters in url.parse (e.g. ") are no longer escaped (not necessarily here because that's common to a lot of properties).

// Prints http://example.org/foo#baz
```

Using the `delete` keyword (e.g. `delete myURL.hash`) has no effect.
Copy link
Member

Choose a reason for hiding this comment

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

The delete statement would return true even though it has no effect. Could use a hint?

// Prints http://example.org/foo

myURL.href = 'https://example.com/bar'
// Prints https://example.com/bar
Copy link
Member

Choose a reason for hiding this comment

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

Might be worth adding

myURL.href = 'http://你好你好';
// Prints http://xn--6qqa088eba/
// Notice the slash added at the end and the ASCII serialization

```js
const myURL = new URL('http://example.org/foo/bar?baz');
console.log(myURL.origin);
// Prints http://example.org
Copy link
Member

Choose a reason for hiding this comment

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

Might be worth adding

const idnURL = new URL('http://你好你好');
console.log(idnURL.origin);
// Prints http://你好你好


```js
const myURL = new URL('http://example.org:8888');
console.log(myURL.port);
Copy link
Member

Choose a reason for hiding this comment

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

Worth pointing out it returns a string, not a number. Also setting it to the protocol's default port would result in a '' returned, that's different from what url.parse would do.


Using the `delete` keyword (e.g. `delete myURL.port`) has no effect.

Invalid URL port values assigned to the `port` property are ignored.
Copy link
Member

Choose a reason for hiding this comment

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

Valid range is [0, 65535].

```js
const myURL = new URL('http://example.org/abc?123');
console.log(myURL.search);
// Prints 123
Copy link
Member

Choose a reason for hiding this comment

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

Has a question mark at the beginning, ?123


#### url.toString([unicode])

* `unicode` {Boolean} `true` to serialize Unicode characters in the URL
Copy link
Member

Choose a reason for hiding this comment

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

Could use a tip about it returning the same result as #href

@joyeecheung
Copy link
Member

Overall I think we should document the behaviors different from url.parse for people who want to migrate to this new API. That doesn't have to happen all at once in this PR though.

@jasnell
Copy link
Member Author

jasnell commented Jan 5, 2017

@joyeecheung ... definitely think that would be helpful but perhaps a guide would be better for that? In the meantime, I've address some of your comments! PTAL!


```js
const myURL = new URL('/foo', 'https://example.org/');
// https://example.org/foo
Copy link
Member

Choose a reason for hiding this comment

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

Any reason this comment is indented (and others below)?

Copy link
Member Author

Choose a reason for hiding this comment

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

Just a style I generally prefer (and have used in other docs that show the expected output

Each item of the iterator is a JavaScript Array. The first item of the Array
is the `name`, the second item of the Array is the `value`.

### require('url').originFor(url)
Copy link
Member

Choose a reason for hiding this comment

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

Not very sure about these, if they are not part of the public API, maybe we should avoid documenting them?

Copy link
Member Author

Choose a reason for hiding this comment

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

They are not part of the WHATWG specification. They will be part of the Node.js API, however.


#### url.port

Gets and sets the port portion of the URL as a String.
Copy link
Member

Choose a reason for hiding this comment

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

Gets as a string, sets as either a string or a number.

@joyeecheung
Copy link
Member

@jasnell Yeah a guide would definitely be more suitable for this. I've submitted another review, if resolved then LGTM.

@jasnell
Copy link
Member Author

jasnell commented Jan 5, 2017

I've added some specific details about the pct-encoding in the new implementation

Remove any existing name-value pairs whose name is `name` and append a new
name-value pair.

#### urlSearchParams[Symbol.iterator]()
Copy link
Member

Choose a reason for hiding this comment

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

Perhaps the @@iterator notation used in ES spec would be more concise? Or is this notation already used in the documentation?

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't believe there is much (or any) precedence for documenting @@iterator in our current docs given that few (if any) of our core APIs make use of it.


Returns an ES6 Iterator over each of the name-value pairs in the query string.
Each item of the iterator is a JavaScript Array. The first item of the Array
is the `name`, the second item of the Array is the `value`.
Copy link
Member

Choose a reason for hiding this comment

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

There are also keys, values, and entries functions.


* `name` {String}
* `value` {String}
* Returns `undefined`
Copy link
Member

Choose a reason for hiding this comment

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

I don't think "Returns undefined" needs to be explicitly stated…

#### urlSearchParams.get(name)

* `name` {String}
* Returns a string or `undefined`
Copy link
Member

Choose a reason for hiding this comment

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

Actually, it returns null when the param cannot be found.

#### urlSearchParams.getAll(name)

* `name` {String}
* Returns a JavaScript Array
Copy link
Member

@TimothyGu TimothyGu Jan 5, 2017

Choose a reason for hiding this comment

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

Other places of the documentation seem to prefer

* Returns: {Array}


#### urlSearchParams.entries()

* Returns: ES6 Iterator
Copy link
Member

Choose a reason for hiding this comment

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

While at it, why don't you add an MDN link for the Iterator type to tools/doc/type-parser.js ? Then, you will be able to use {Iterator} with automatic linking.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, good idea


Returns an ES6 Iterator over each of the name-value pairs in the query string.
Each item of the iterator is a JavaScript Array. The first item of the Array
is the `name`, the second item of the Array is the `value`.
Copy link
Member

Choose a reason for hiding this comment

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

I'd explicitly mention that by definition, urlSearchParams[@@iterator] === urlSearchParams.entries. You can completely replace this description with the aliasing information (which has the benefit of not going out of sync), or make that info an additional paragraph – either is fine with me.

@joyeecheung joyeecheung added the whatwg-url Issues and PRs related to the WHATWG URL implementation. label Jan 5, 2017
@jasnell
Copy link
Member Author

jasnell commented Jan 17, 2017

Updated!

@jasnell
Copy link
Member Author

jasnell commented Jan 17, 2017

Will land this tomorrow if there are further objections or comments.

@joyeecheung
Copy link
Member

This haven't moved for a while. Pinging @Fishrock123 , are you OK with landing this now?

@jasnell
Copy link
Member Author

jasnell commented Jan 23, 2017

Yeah, I intentionally left it open just in case. I'm planning on landing it today.

jasnell added a commit that referenced this pull request Jan 23, 2017
PR-URL: #10620
Reviewed-By: Sam Roberts <[email protected]>
Reviewed-By: Joyee Cheung <[email protected]>
Reviewed-By: Michal Zasso <[email protected]>
Reviewed-By: Timothy Gu <[email protected]>
jasnell added a commit that referenced this pull request Jan 23, 2017
PR-URL: #10620
Reviewed-By: Sam Roberts <[email protected]>
Reviewed-By: Joyee Cheung <[email protected]>
Reviewed-By: Michal Zasso <[email protected]>
Reviewed-By: Timothy Gu <[email protected]>
@jasnell
Copy link
Member Author

jasnell commented Jan 23, 2017

Landed in 0c712b6 and 4757ddc

@jasnell jasnell closed this Jan 23, 2017
italoacasas pushed a commit to italoacasas/node that referenced this pull request Jan 25, 2017
PR-URL: nodejs#10620
Reviewed-By: Sam Roberts <[email protected]>
Reviewed-By: Joyee Cheung <[email protected]>
Reviewed-By: Michal Zasso <[email protected]>
Reviewed-By: Timothy Gu <[email protected]>
italoacasas pushed a commit to italoacasas/node that referenced this pull request Jan 25, 2017
PR-URL: nodejs#10620
Reviewed-By: Sam Roberts <[email protected]>
Reviewed-By: Joyee Cheung <[email protected]>
Reviewed-By: Michal Zasso <[email protected]>
Reviewed-By: Timothy Gu <[email protected]>
italoacasas pushed a commit to italoacasas/node that referenced this pull request Jan 27, 2017
PR-URL: nodejs#10620
Reviewed-By: Sam Roberts <[email protected]>
Reviewed-By: Joyee Cheung <[email protected]>
Reviewed-By: Michal Zasso <[email protected]>
Reviewed-By: Timothy Gu <[email protected]>
italoacasas pushed a commit to italoacasas/node that referenced this pull request Jan 27, 2017
PR-URL: nodejs#10620
Reviewed-By: Sam Roberts <[email protected]>
Reviewed-By: Joyee Cheung <[email protected]>
Reviewed-By: Michal Zasso <[email protected]>
Reviewed-By: Timothy Gu <[email protected]>
@italoacasas italoacasas mentioned this pull request Jan 29, 2017
italoacasas pushed a commit to italoacasas/node that referenced this pull request Jan 30, 2017
PR-URL: nodejs#10620
Reviewed-By: Sam Roberts <[email protected]>
Reviewed-By: Joyee Cheung <[email protected]>
Reviewed-By: Michal Zasso <[email protected]>
Reviewed-By: Timothy Gu <[email protected]>
italoacasas pushed a commit to italoacasas/node that referenced this pull request Jan 30, 2017
PR-URL: nodejs#10620
Reviewed-By: Sam Roberts <[email protected]>
Reviewed-By: Joyee Cheung <[email protected]>
Reviewed-By: Michal Zasso <[email protected]>
Reviewed-By: Timothy Gu <[email protected]>
italoacasas pushed a commit to italoacasas/node that referenced this pull request Jan 30, 2017
PR-URL: nodejs#10620
Reviewed-By: Sam Roberts <[email protected]>
Reviewed-By: Joyee Cheung <[email protected]>
Reviewed-By: Michal Zasso <[email protected]>
Reviewed-By: Timothy Gu <[email protected]>
italoacasas pushed a commit to italoacasas/node that referenced this pull request Jan 30, 2017
PR-URL: nodejs#10620
Reviewed-By: Sam Roberts <[email protected]>
Reviewed-By: Joyee Cheung <[email protected]>
Reviewed-By: Michal Zasso <[email protected]>
Reviewed-By: Timothy Gu <[email protected]>
evanlucas added a commit that referenced this pull request Jan 31, 2017
Notable changes:

* crypto:
  * ability to select cert store at runtime (Adam Majer) #8334
  * Use system CAs instead of using bundled ones (Adam Majer) #8334
* deps:
  * upgrade npm to 4.1.2 (Kat Marchán) #11020
  * upgrade openssl sources to 1.0.2k (Shigeki Ohtsu) #11021
* doc: add basic documentation for WHATWG URL API (James M Snell) #10620
* process: add NODE_NO_WARNINGS environment variable (cjihrig) #10842
* url: allow use of URL with http.request and https.request (James M Snell) #10638

PR-URL: #11062
evanlucas added a commit that referenced this pull request Feb 1, 2017
Notable changes:

* crypto:
  * ability to select cert store at runtime (Adam Majer) #8334
  * Use system CAs instead of using bundled ones (Adam Majer) #8334
* deps:
  * upgrade npm to 4.1.2 (Kat Marchán) #11020
  * upgrade openssl sources to 1.0.2k (Shigeki Ohtsu) #11021
* doc: add basic documentation for WHATWG URL API (James M Snell) #10620
* process: add NODE_NO_WARNINGS environment variable (cjihrig) #10842
* url: allow use of URL with http.request and https.request (James M Snell) #10638

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

Successfully merging this pull request may close these issues.

10 participants