whatwg-url 0.6.2 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,30 @@
1
+ {
2
+ "version": "0.2.0",
3
+ "configurations": [
4
+ {
5
+ "name": "Launch",
6
+ "type": "node",
7
+ "request": "launch",
8
+ "program": "test.js",
9
+ "stopOnEntry": false,
10
+ "args": [],
11
+ "cwd": ".",
12
+ "runtimeExecutable": null,
13
+ "runtimeArgs": [
14
+ "--nolazy"
15
+ ],
16
+ "env": {
17
+ "NODE_ENV": "development"
18
+ },
19
+ "externalConsole": false,
20
+ "sourceMaps": false,
21
+ "outDir": null
22
+ },
23
+ {
24
+ "name": "Attach",
25
+ "type": "node",
26
+ "request": "attach",
27
+ "port": 5858
28
+ }
29
+ ]
30
+ }
package/README.md CHANGED
@@ -1,7 +1,64 @@
1
1
  # whatwg-url
2
2
 
3
- whatwg-url is a full implementation of the [WHATWG URL](https://url.spec.whatwg.org/) specification.
3
+ whatwg-url is a full implementation of the WHATWG [URL Standard](https://url.spec.whatwg.org/). It can be used standalone, but it also exposes a lot of the internal algorithms that are useful for integrating a URL parser into a project like [jsdom](https://github.com/tmpvar/jsdom).
4
4
 
5
- ## Current State
5
+ ## Current Status
6
6
 
7
- whatwg-url is currently up to date with the URL spec up to commit [1eab2abb38](https://github.com/whatwg/url/tree/1eab2abb3806158fc7a550ac5b7deb2d6fff5602).
7
+ whatwg-url is currently up to date with the URL spec up to commit [a9197f](a9197f7714e6b125f1f760ca1aa661530261773c).
8
+
9
+ ## API
10
+
11
+ ### The `URL` Constructor
12
+
13
+ The main API is the [`URL`](https://url.spec.whatwg.org/#url) export, which follows the spec's behavior in all ways (including e.g. `USVString` conversion). Most consumers of this library will want to use this.
14
+
15
+ ### Low-level URL Standard API
16
+
17
+ The following methods are exported for use by places like jsdom that need to implement things like [`HTMLHyperlinkElementUtils`](https://html.spec.whatwg.org/#htmlhyperlinkelementutils). They operate on or return an "internal URL" or ["URL record"](https://url.spec.whatwg.org/#concept-url) type.
18
+
19
+ - [URL parser](https://url.spec.whatwg.org/#concept-url-parser): `parseURL(input, { baseURL, encodingOverride })`
20
+ - [Basic URL parser](https://url.spec.whatwg.org/#concept-basic-url-parser): `basicURLParse(input, { baseURL, encodingOverride, url, stateOverride })`
21
+ - [URL serializer](https://url.spec.whatwg.org/#concept-url-serializer): `serializeURL(urlRecord, excludeFragment)`
22
+ - [Host serializer](https://url.spec.whatwg.org/#concept-host-serializer): `serializeHost(hostFromURLRecord)`
23
+ - [Serialize an integer](https://url.spec.whatwg.org/#serialize-an-integer): `serializeInteger(number)`
24
+ - [Origin](https://url.spec.whatwg.org/#concept-url-origin) [Unicode serializer](https://html.spec.whatwg.org/multipage/browsers.html#unicode-serialisation-of-an-origin): `serializeURLToUnicodeOrigin(urlRecord)`
25
+ - [Set the username](https://url.spec.whatwg.org/#set-the-username): `setTheUsername(urlRecord, usernameString)`
26
+ - [Set the password](https://url.spec.whatwg.org/#set-the-password): `setThePassword(urlRecord, passwordString)`.
27
+
28
+ The `stateOverride` parameter is one of the following strings:
29
+
30
+ - [`"scheme start"`](https://url.spec.whatwg.org/#scheme-start-state)
31
+ - [`"scheme"`](https://url.spec.whatwg.org/#scheme-state)
32
+ - [`"no scheme"`](https://url.spec.whatwg.org/#no-scheme-state)
33
+ - [`"special relative or authority"`](https://url.spec.whatwg.org/#special-relative-or-authority-state)
34
+ - [`"path or authority"`](https://url.spec.whatwg.org/#path-or-authority-state)
35
+ - [`"relative"`](https://url.spec.whatwg.org/#relative-state)
36
+ - [`"relative slash"`](https://url.spec.whatwg.org/#relative-slash-state)
37
+ - [`"special authority slashes"`](https://url.spec.whatwg.org/#special-authority-slashes-state)
38
+ - [`"special authority ignore slashes"`](https://url.spec.whatwg.org/#special-authority-ignore-slashes-state)
39
+ - [`"authority"`](https://url.spec.whatwg.org/#authority-state)
40
+ - [`"host"`](https://url.spec.whatwg.org/#host-state)
41
+ - [`"hostname"`](https://url.spec.whatwg.org/#hostname-state)
42
+ - [`"port"`](https://url.spec.whatwg.org/#port-state)
43
+ - [`"file"`](https://url.spec.whatwg.org/#file-state)
44
+ - [`"file slash"`](https://url.spec.whatwg.org/#file-slash-state)
45
+ - [`"file host"`](https://url.spec.whatwg.org/#file-host-state)
46
+ - [`"path start"`](https://url.spec.whatwg.org/#path-start-state)
47
+ - [`"path"`](https://url.spec.whatwg.org/#path-state)
48
+ - [`"non-relative path"`](https://url.spec.whatwg.org/#non-relative-path-state)
49
+ - [`"query"`](https://url.spec.whatwg.org/#query-state)
50
+ - [`"fragment"`](https://url.spec.whatwg.org/#fragment-state)
51
+
52
+ The URL record type has the following API:
53
+
54
+ - [`scheme`](https://url.spec.whatwg.org/#concept-url-scheme)
55
+ - [`username`](https://url.spec.whatwg.org/#concept-url-username)
56
+ - [`password`](https://url.spec.whatwg.org/#concept-url-password)
57
+ - [`host`](https://url.spec.whatwg.org/#concept-url-host)
58
+ - [`port`](https://url.spec.whatwg.org/#concept-url-port)
59
+ - [`path`](https://url.spec.whatwg.org/#concept-url-path) (as an array)
60
+ - [`query`](https://url.spec.whatwg.org/#concept-url-query)
61
+ - [`fragment`](https://url.spec.whatwg.org/#concept-url-fragment)
62
+ - [`nonRelative`](https://url.spec.whatwg.org/#non-relative-flag) (as a boolean)
63
+
64
+ These properties should be treated with care, as in general changing them will cause the URL record to be in an inconsistent state until the appropriate invocation of `basicURLParse` is used to fix it up. You can see examples of this in the URL Standard, where there are many step sequences like "4. Set context object’s url’s fragment to the empty string. 5. Basic URL parse _input_ with context object’s url as _url_ and fragment state as _state override_." In between those two steps, a URL record is in an unusable state.
@@ -0,0 +1,206 @@
1
+ "use strict";
2
+ const usm = require("./url-state-machine");
3
+
4
+ exports.implementation = class URLImpl {
5
+ constructor(constructorArgs) {
6
+ const url = constructorArgs[0];
7
+ const base = constructorArgs[1];
8
+
9
+ let parsedBase = null;
10
+ if (base !== undefined) {
11
+ parsedBase = usm.basicURLParse(base);
12
+ if (parsedBase.failure) {
13
+ throw new TypeError("Invalid base URL");
14
+ }
15
+ }
16
+
17
+ const parsedURL = usm.basicURLParse(url, { baseURL: parsedBase });
18
+ if (parsedURL.failure) {
19
+ throw new TypeError("Invalid URL");
20
+ }
21
+
22
+ this._url = parsedURL;
23
+
24
+ // TODO: query stuff
25
+ }
26
+
27
+ static domainToASCII(domain) {
28
+ const asciiDomain = usm.parseHost(domain);
29
+ if (typeof asciiDomain !== "string") {
30
+ return "";
31
+ }
32
+ return asciiDomain;
33
+ }
34
+
35
+ static domainToUnicode(domain) {
36
+ const unicodeDomain = usm.parseHost(domain, true);
37
+ if (typeof unicodeDomain !== "string") {
38
+ return "";
39
+ }
40
+ return unicodeDomain;
41
+ }
42
+
43
+ get href() {
44
+ return usm.serializeURL(this._url);
45
+ }
46
+
47
+ set href(v) {
48
+ this._url = usm.basicURLParse(v);
49
+ }
50
+
51
+ get origin() {
52
+ return usm.serializeURLToUnicodeOrigin(this._url);
53
+ }
54
+
55
+ get protocol() {
56
+ return this._url.scheme + ":";
57
+ }
58
+
59
+ set protocol(v) {
60
+ usm.basicURLParse(v + ":", { url: this._url, stateOverride: "scheme start" });
61
+ }
62
+
63
+ get username() {
64
+ return this._url.username;
65
+ }
66
+
67
+ set username(v) {
68
+ if (this._url.host === null || this._url.nonRelative) {
69
+ return;
70
+ }
71
+
72
+ usm.setTheUsername(this._url, v);
73
+ }
74
+
75
+ get password() {
76
+ if (this._url.password === null) {
77
+ return "";
78
+ }
79
+
80
+ return this._url.password;
81
+ }
82
+
83
+ set password(v) {
84
+ if (this._url.host === null || this._url.nonRelative) {
85
+ return;
86
+ }
87
+
88
+ usm.setThePassword(this._url, v);
89
+ }
90
+
91
+ get host() {
92
+ const url = this._url;
93
+
94
+ if (url.host === null) {
95
+ return "";
96
+ }
97
+
98
+ if (url.port === null) {
99
+ return usm.serializeHost(url.host);
100
+ }
101
+
102
+ return usm.serializeHost(url.host) + ":" + usm.serializeInteger(url.port);
103
+ }
104
+
105
+ set host(v) {
106
+ if (this._url.nonRelative) {
107
+ return;
108
+ }
109
+
110
+ usm.basicURLParse(v, { url: this._url, stateOverride: "host" });
111
+ }
112
+
113
+ get hostname() {
114
+ if (this._url.host === null) {
115
+ return "";
116
+ }
117
+
118
+ return usm.serializeHost(this._url.host);
119
+ }
120
+
121
+ set hostname(v) {
122
+ if (this._url.nonRelative) {
123
+ return;
124
+ }
125
+
126
+ usm.basicURLParse(v, { url: this._url, stateOverride: "hostname" });
127
+ }
128
+
129
+ get port() {
130
+ if (this._url.port === null) {
131
+ return "";
132
+ }
133
+
134
+ return usm.serializeInteger(this._url.port);
135
+ }
136
+
137
+ set port(v) {
138
+ if (this._url.host === null || this._url.nonRelative || this._url.scheme === "file") {
139
+ return;
140
+ }
141
+
142
+ usm.basicURLParse(v, { url: this._url, stateOverride: "port" });
143
+ }
144
+
145
+ get pathname() {
146
+ if (this._url.nonRelative) {
147
+ return this._url.path[0];
148
+ }
149
+
150
+ return "/" + this._url.path.join("/");
151
+ }
152
+
153
+ set pathname(v) {
154
+ if (this._url.nonRelative) {
155
+ return;
156
+ }
157
+
158
+ this._url.path = [];
159
+ usm.basicURLParse(v, { url: this._url, stateOverride: "path start" });
160
+ }
161
+
162
+ get search() {
163
+ if (this._url.query === null || this._url.query === "") {
164
+ return "";
165
+ }
166
+
167
+ return "?" + this._url.query;
168
+ }
169
+
170
+ set search(v) {
171
+ // TODO: query stuff
172
+
173
+ const url = this._url;
174
+
175
+ if (v === "") {
176
+ url.query = null;
177
+ }
178
+
179
+ const input = v[0] === "?" ? v.substring(1) : v;
180
+ url.query = "";
181
+ usm.basicURLParse(input, { url, stateOverride: "query" });
182
+ }
183
+
184
+ get hash() {
185
+ if (this._url.fragment === null || this._url.fragment === "") {
186
+ return "";
187
+ }
188
+
189
+ return "#" + this._url.fragment;
190
+ }
191
+
192
+ set hash(v) {
193
+ if (this._url.scheme === "javascript") {
194
+ return;
195
+ }
196
+
197
+ if (v === "") {
198
+ this._url.fragment = null;
199
+ return;
200
+ }
201
+
202
+ const input = v[0] === "#" ? v.substring(1) : v;
203
+ this._url.fragment = "";
204
+ usm.basicURLParse(input, { url: this._url, stateOverride: "fragment" });
205
+ }
206
+ };
package/lib/URL.js ADDED
@@ -0,0 +1,209 @@
1
+ "use strict";
2
+
3
+ const conversions = require("webidl-conversions");
4
+ const utils = require("./utils.js");
5
+ const Impl = require(".//URL-impl.js");
6
+
7
+ const impl = utils.implSymbol;
8
+
9
+ function URL(url) {
10
+ if (!this || this[impl] || !(this instanceof URL)) {
11
+ throw new TypeError("Failed to construct 'URL': Please use the 'new' operator, this DOM object constructor cannot be called as a function.");
12
+ }
13
+ if (arguments.length < 1) {
14
+ throw new TypeError("Failed to construct 'URL': 1 argument required, but only " + arguments.length + " present.");
15
+ }
16
+ const args = [];
17
+ for (let i = 0; i < arguments.length && i < 2; ++i) {
18
+ args[i] = arguments[i];
19
+ }
20
+ args[0] = conversions["USVString"](args[0]);
21
+ if (args[1] !== undefined) {
22
+ args[1] = conversions["USVString"](args[1]);
23
+ }
24
+
25
+ module.exports.setup(this, args);
26
+ }
27
+
28
+ URL.domainToASCII = function domainToASCII(domain) {
29
+ if (arguments.length < 1) {
30
+ throw new TypeError("Failed to execute 'domainToASCII' on 'URL': 1 argument required, but only " + arguments.length + " present.");
31
+ }
32
+ const args = [];
33
+ for (let i = 0; i < arguments.length && i < 1; ++i) {
34
+ args[i] = arguments[i];
35
+ }
36
+ args[0] = conversions["USVString"](args[0]);
37
+ return Impl.domainToASCII.apply(Impl, args);
38
+ };
39
+
40
+ URL.domainToUnicode = function domainToUnicode(domain) {
41
+ if (arguments.length < 1) {
42
+ throw new TypeError("Failed to execute 'domainToUnicode' on 'URL': 1 argument required, but only " + arguments.length + " present.");
43
+ }
44
+ const args = [];
45
+ for (let i = 0; i < arguments.length && i < 1; ++i) {
46
+ args[i] = arguments[i];
47
+ }
48
+ args[0] = conversions["USVString"](args[0]);
49
+ return Impl.domainToUnicode.apply(Impl, args);
50
+ };
51
+ Object.defineProperty(URL.prototype, "href", {
52
+ get() {
53
+ return this[impl].href;
54
+ },
55
+ set(V) {
56
+ V = conversions["USVString"](V);
57
+ this[impl].href = V;
58
+ },
59
+ enumerable: true,
60
+ configurable: true
61
+ });
62
+
63
+ URL.prototype.toString = function () {
64
+ if (!this || !module.exports.is(this)) {
65
+ throw new TypeError("Illegal invocation");
66
+ }
67
+ return this.href;
68
+ };
69
+
70
+ Object.defineProperty(URL.prototype, "origin", {
71
+ get() {
72
+ return this[impl].origin;
73
+ },
74
+ enumerable: true,
75
+ configurable: true
76
+ });
77
+
78
+ Object.defineProperty(URL.prototype, "protocol", {
79
+ get() {
80
+ return this[impl].protocol;
81
+ },
82
+ set(V) {
83
+ V = conversions["USVString"](V);
84
+ this[impl].protocol = V;
85
+ },
86
+ enumerable: true,
87
+ configurable: true
88
+ });
89
+
90
+ Object.defineProperty(URL.prototype, "username", {
91
+ get() {
92
+ return this[impl].username;
93
+ },
94
+ set(V) {
95
+ V = conversions["USVString"](V);
96
+ this[impl].username = V;
97
+ },
98
+ enumerable: true,
99
+ configurable: true
100
+ });
101
+
102
+ Object.defineProperty(URL.prototype, "password", {
103
+ get() {
104
+ return this[impl].password;
105
+ },
106
+ set(V) {
107
+ V = conversions["USVString"](V);
108
+ this[impl].password = V;
109
+ },
110
+ enumerable: true,
111
+ configurable: true
112
+ });
113
+
114
+ Object.defineProperty(URL.prototype, "host", {
115
+ get() {
116
+ return this[impl].host;
117
+ },
118
+ set(V) {
119
+ V = conversions["USVString"](V);
120
+ this[impl].host = V;
121
+ },
122
+ enumerable: true,
123
+ configurable: true
124
+ });
125
+
126
+ Object.defineProperty(URL.prototype, "hostname", {
127
+ get() {
128
+ return this[impl].hostname;
129
+ },
130
+ set(V) {
131
+ V = conversions["USVString"](V);
132
+ this[impl].hostname = V;
133
+ },
134
+ enumerable: true,
135
+ configurable: true
136
+ });
137
+
138
+ Object.defineProperty(URL.prototype, "port", {
139
+ get() {
140
+ return this[impl].port;
141
+ },
142
+ set(V) {
143
+ V = conversions["USVString"](V);
144
+ this[impl].port = V;
145
+ },
146
+ enumerable: true,
147
+ configurable: true
148
+ });
149
+
150
+ Object.defineProperty(URL.prototype, "pathname", {
151
+ get() {
152
+ return this[impl].pathname;
153
+ },
154
+ set(V) {
155
+ V = conversions["USVString"](V);
156
+ this[impl].pathname = V;
157
+ },
158
+ enumerable: true,
159
+ configurable: true
160
+ });
161
+
162
+ Object.defineProperty(URL.prototype, "search", {
163
+ get() {
164
+ return this[impl].search;
165
+ },
166
+ set(V) {
167
+ V = conversions["USVString"](V);
168
+ this[impl].search = V;
169
+ },
170
+ enumerable: true,
171
+ configurable: true
172
+ });
173
+
174
+ Object.defineProperty(URL.prototype, "hash", {
175
+ get() {
176
+ return this[impl].hash;
177
+ },
178
+ set(V) {
179
+ V = conversions["USVString"](V);
180
+ this[impl].hash = V;
181
+ },
182
+ enumerable: true,
183
+ configurable: true
184
+ });
185
+
186
+
187
+ module.exports = {
188
+ is(obj) {
189
+ return !!obj && obj[impl] instanceof Impl.implementation;
190
+ },
191
+ create(constructorArgs, privateData) {
192
+ let obj = Object.create(URL.prototype);
193
+ this.setup(obj, constructorArgs, privateData);
194
+ return obj;
195
+ },
196
+ setup(obj, constructorArgs, privateData) {
197
+ if (!privateData) privateData = {};
198
+ privateData.wrapper = obj;
199
+
200
+ obj[impl] = new Impl.implementation(constructorArgs, privateData);
201
+ obj[impl][utils.wrapperSymbol] = obj;
202
+ },
203
+ interface: URL,
204
+ expose: {
205
+ Window: { URL: URL },
206
+ Worker: { URL: URL }
207
+ }
208
+ };
209
+
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+
3
+ exports.URL = require("./URL").interface;
4
+ exports.serializeURL = require("./url-state-machine").serializeURL;
5
+ exports.serializeURLToUnicodeOrigin = require("./url-state-machine").serializeURLToUnicodeOrigin;
6
+ exports.basicURLParse = require("./url-state-machine").basicURLParse;
7
+ exports.setTheUsername = require("./url-state-machine").setTheUsername;
8
+ exports.setThePassword = require("./url-state-machine").setThePassword;
9
+ exports.serializeHost = require("./url-state-machine").serializeHost;
10
+ exports.serializeInteger = require("./url-state-machine").serializeInteger;
11
+ exports.parseURL = require("./url-state-machine").parseURL;