| 1 | "use strict";
|
|---|
| 2 |
|
|---|
| 3 | const conversions = require("webidl-conversions");
|
|---|
| 4 | const utils = require("./utils.js");
|
|---|
| 5 |
|
|---|
| 6 | const implSymbol = utils.implSymbol;
|
|---|
| 7 | const ctorRegistrySymbol = utils.ctorRegistrySymbol;
|
|---|
| 8 |
|
|---|
| 9 | const interfaceName = "URL";
|
|---|
| 10 |
|
|---|
| 11 | exports.is = value => {
|
|---|
| 12 | return utils.isObject(value) && utils.hasOwn(value, implSymbol) && value[implSymbol] instanceof Impl.implementation;
|
|---|
| 13 | };
|
|---|
| 14 | exports.isImpl = value => {
|
|---|
| 15 | return utils.isObject(value) && value instanceof Impl.implementation;
|
|---|
| 16 | };
|
|---|
| 17 | exports.convert = (value, { context = "The provided value" } = {}) => {
|
|---|
| 18 | if (exports.is(value)) {
|
|---|
| 19 | return utils.implForWrapper(value);
|
|---|
| 20 | }
|
|---|
| 21 | throw new TypeError(`${context} is not of type 'URL'.`);
|
|---|
| 22 | };
|
|---|
| 23 |
|
|---|
| 24 | function makeWrapper(globalObject) {
|
|---|
| 25 | if (globalObject[ctorRegistrySymbol] === undefined) {
|
|---|
| 26 | throw new Error("Internal error: invalid global object");
|
|---|
| 27 | }
|
|---|
| 28 |
|
|---|
| 29 | const ctor = globalObject[ctorRegistrySymbol]["URL"];
|
|---|
| 30 | if (ctor === undefined) {
|
|---|
| 31 | throw new Error("Internal error: constructor URL is not installed on the passed global object");
|
|---|
| 32 | }
|
|---|
| 33 |
|
|---|
| 34 | return Object.create(ctor.prototype);
|
|---|
| 35 | }
|
|---|
| 36 |
|
|---|
| 37 | exports.create = (globalObject, constructorArgs, privateData) => {
|
|---|
| 38 | const wrapper = makeWrapper(globalObject);
|
|---|
| 39 | return exports.setup(wrapper, globalObject, constructorArgs, privateData);
|
|---|
| 40 | };
|
|---|
| 41 |
|
|---|
| 42 | exports.createImpl = (globalObject, constructorArgs, privateData) => {
|
|---|
| 43 | const wrapper = exports.create(globalObject, constructorArgs, privateData);
|
|---|
| 44 | return utils.implForWrapper(wrapper);
|
|---|
| 45 | };
|
|---|
| 46 |
|
|---|
| 47 | exports._internalSetup = (wrapper, globalObject) => {};
|
|---|
| 48 |
|
|---|
| 49 | exports.setup = (wrapper, globalObject, constructorArgs = [], privateData = {}) => {
|
|---|
| 50 | privateData.wrapper = wrapper;
|
|---|
| 51 |
|
|---|
| 52 | exports._internalSetup(wrapper, globalObject);
|
|---|
| 53 | Object.defineProperty(wrapper, implSymbol, {
|
|---|
| 54 | value: new Impl.implementation(globalObject, constructorArgs, privateData),
|
|---|
| 55 | configurable: true
|
|---|
| 56 | });
|
|---|
| 57 |
|
|---|
| 58 | wrapper[implSymbol][utils.wrapperSymbol] = wrapper;
|
|---|
| 59 | if (Impl.init) {
|
|---|
| 60 | Impl.init(wrapper[implSymbol]);
|
|---|
| 61 | }
|
|---|
| 62 | return wrapper;
|
|---|
| 63 | };
|
|---|
| 64 |
|
|---|
| 65 | exports.new = globalObject => {
|
|---|
| 66 | const wrapper = makeWrapper(globalObject);
|
|---|
| 67 |
|
|---|
| 68 | exports._internalSetup(wrapper, globalObject);
|
|---|
| 69 | Object.defineProperty(wrapper, implSymbol, {
|
|---|
| 70 | value: Object.create(Impl.implementation.prototype),
|
|---|
| 71 | configurable: true
|
|---|
| 72 | });
|
|---|
| 73 |
|
|---|
| 74 | wrapper[implSymbol][utils.wrapperSymbol] = wrapper;
|
|---|
| 75 | if (Impl.init) {
|
|---|
| 76 | Impl.init(wrapper[implSymbol]);
|
|---|
| 77 | }
|
|---|
| 78 | return wrapper[implSymbol];
|
|---|
| 79 | };
|
|---|
| 80 |
|
|---|
| 81 | const exposed = new Set(["Window", "Worker"]);
|
|---|
| 82 |
|
|---|
| 83 | exports.install = (globalObject, globalNames) => {
|
|---|
| 84 | if (!globalNames.some(globalName => exposed.has(globalName))) {
|
|---|
| 85 | return;
|
|---|
| 86 | }
|
|---|
| 87 | class URL {
|
|---|
| 88 | constructor(url) {
|
|---|
| 89 | if (arguments.length < 1) {
|
|---|
| 90 | throw new TypeError(
|
|---|
| 91 | "Failed to construct 'URL': 1 argument required, but only " + arguments.length + " present."
|
|---|
| 92 | );
|
|---|
| 93 | }
|
|---|
| 94 | const args = [];
|
|---|
| 95 | {
|
|---|
| 96 | let curArg = arguments[0];
|
|---|
| 97 | curArg = conversions["USVString"](curArg, { context: "Failed to construct 'URL': parameter 1" });
|
|---|
| 98 | args.push(curArg);
|
|---|
| 99 | }
|
|---|
| 100 | {
|
|---|
| 101 | let curArg = arguments[1];
|
|---|
| 102 | if (curArg !== undefined) {
|
|---|
| 103 | curArg = conversions["USVString"](curArg, { context: "Failed to construct 'URL': parameter 2" });
|
|---|
| 104 | }
|
|---|
| 105 | args.push(curArg);
|
|---|
| 106 | }
|
|---|
| 107 | return exports.setup(Object.create(new.target.prototype), globalObject, args);
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 | toJSON() {
|
|---|
| 111 | const esValue = this !== null && this !== undefined ? this : globalObject;
|
|---|
| 112 | if (!exports.is(esValue)) {
|
|---|
| 113 | throw new TypeError("'toJSON' called on an object that is not a valid instance of URL.");
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | return esValue[implSymbol].toJSON();
|
|---|
| 117 | }
|
|---|
| 118 |
|
|---|
| 119 | get href() {
|
|---|
| 120 | const esValue = this !== null && this !== undefined ? this : globalObject;
|
|---|
| 121 |
|
|---|
| 122 | if (!exports.is(esValue)) {
|
|---|
| 123 | throw new TypeError("'get href' called on an object that is not a valid instance of URL.");
|
|---|
| 124 | }
|
|---|
| 125 |
|
|---|
| 126 | return esValue[implSymbol]["href"];
|
|---|
| 127 | }
|
|---|
| 128 |
|
|---|
| 129 | set href(V) {
|
|---|
| 130 | const esValue = this !== null && this !== undefined ? this : globalObject;
|
|---|
| 131 |
|
|---|
| 132 | if (!exports.is(esValue)) {
|
|---|
| 133 | throw new TypeError("'set href' called on an object that is not a valid instance of URL.");
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| 136 | V = conversions["USVString"](V, { context: "Failed to set the 'href' property on 'URL': The provided value" });
|
|---|
| 137 |
|
|---|
| 138 | esValue[implSymbol]["href"] = V;
|
|---|
| 139 | }
|
|---|
| 140 |
|
|---|
| 141 | toString() {
|
|---|
| 142 | const esValue = this;
|
|---|
| 143 | if (!exports.is(esValue)) {
|
|---|
| 144 | throw new TypeError("'toString' called on an object that is not a valid instance of URL.");
|
|---|
| 145 | }
|
|---|
| 146 |
|
|---|
| 147 | return esValue[implSymbol]["href"];
|
|---|
| 148 | }
|
|---|
| 149 |
|
|---|
| 150 | get origin() {
|
|---|
| 151 | const esValue = this !== null && this !== undefined ? this : globalObject;
|
|---|
| 152 |
|
|---|
| 153 | if (!exports.is(esValue)) {
|
|---|
| 154 | throw new TypeError("'get origin' called on an object that is not a valid instance of URL.");
|
|---|
| 155 | }
|
|---|
| 156 |
|
|---|
| 157 | return esValue[implSymbol]["origin"];
|
|---|
| 158 | }
|
|---|
| 159 |
|
|---|
| 160 | get protocol() {
|
|---|
| 161 | const esValue = this !== null && this !== undefined ? this : globalObject;
|
|---|
| 162 |
|
|---|
| 163 | if (!exports.is(esValue)) {
|
|---|
| 164 | throw new TypeError("'get protocol' called on an object that is not a valid instance of URL.");
|
|---|
| 165 | }
|
|---|
| 166 |
|
|---|
| 167 | return esValue[implSymbol]["protocol"];
|
|---|
| 168 | }
|
|---|
| 169 |
|
|---|
| 170 | set protocol(V) {
|
|---|
| 171 | const esValue = this !== null && this !== undefined ? this : globalObject;
|
|---|
| 172 |
|
|---|
| 173 | if (!exports.is(esValue)) {
|
|---|
| 174 | throw new TypeError("'set protocol' called on an object that is not a valid instance of URL.");
|
|---|
| 175 | }
|
|---|
| 176 |
|
|---|
| 177 | V = conversions["USVString"](V, {
|
|---|
| 178 | context: "Failed to set the 'protocol' property on 'URL': The provided value"
|
|---|
| 179 | });
|
|---|
| 180 |
|
|---|
| 181 | esValue[implSymbol]["protocol"] = V;
|
|---|
| 182 | }
|
|---|
| 183 |
|
|---|
| 184 | get username() {
|
|---|
| 185 | const esValue = this !== null && this !== undefined ? this : globalObject;
|
|---|
| 186 |
|
|---|
| 187 | if (!exports.is(esValue)) {
|
|---|
| 188 | throw new TypeError("'get username' called on an object that is not a valid instance of URL.");
|
|---|
| 189 | }
|
|---|
| 190 |
|
|---|
| 191 | return esValue[implSymbol]["username"];
|
|---|
| 192 | }
|
|---|
| 193 |
|
|---|
| 194 | set username(V) {
|
|---|
| 195 | const esValue = this !== null && this !== undefined ? this : globalObject;
|
|---|
| 196 |
|
|---|
| 197 | if (!exports.is(esValue)) {
|
|---|
| 198 | throw new TypeError("'set username' called on an object that is not a valid instance of URL.");
|
|---|
| 199 | }
|
|---|
| 200 |
|
|---|
| 201 | V = conversions["USVString"](V, {
|
|---|
| 202 | context: "Failed to set the 'username' property on 'URL': The provided value"
|
|---|
| 203 | });
|
|---|
| 204 |
|
|---|
| 205 | esValue[implSymbol]["username"] = V;
|
|---|
| 206 | }
|
|---|
| 207 |
|
|---|
| 208 | get password() {
|
|---|
| 209 | const esValue = this !== null && this !== undefined ? this : globalObject;
|
|---|
| 210 |
|
|---|
| 211 | if (!exports.is(esValue)) {
|
|---|
| 212 | throw new TypeError("'get password' called on an object that is not a valid instance of URL.");
|
|---|
| 213 | }
|
|---|
| 214 |
|
|---|
| 215 | return esValue[implSymbol]["password"];
|
|---|
| 216 | }
|
|---|
| 217 |
|
|---|
| 218 | set password(V) {
|
|---|
| 219 | const esValue = this !== null && this !== undefined ? this : globalObject;
|
|---|
| 220 |
|
|---|
| 221 | if (!exports.is(esValue)) {
|
|---|
| 222 | throw new TypeError("'set password' called on an object that is not a valid instance of URL.");
|
|---|
| 223 | }
|
|---|
| 224 |
|
|---|
| 225 | V = conversions["USVString"](V, {
|
|---|
| 226 | context: "Failed to set the 'password' property on 'URL': The provided value"
|
|---|
| 227 | });
|
|---|
| 228 |
|
|---|
| 229 | esValue[implSymbol]["password"] = V;
|
|---|
| 230 | }
|
|---|
| 231 |
|
|---|
| 232 | get host() {
|
|---|
| 233 | const esValue = this !== null && this !== undefined ? this : globalObject;
|
|---|
| 234 |
|
|---|
| 235 | if (!exports.is(esValue)) {
|
|---|
| 236 | throw new TypeError("'get host' called on an object that is not a valid instance of URL.");
|
|---|
| 237 | }
|
|---|
| 238 |
|
|---|
| 239 | return esValue[implSymbol]["host"];
|
|---|
| 240 | }
|
|---|
| 241 |
|
|---|
| 242 | set host(V) {
|
|---|
| 243 | const esValue = this !== null && this !== undefined ? this : globalObject;
|
|---|
| 244 |
|
|---|
| 245 | if (!exports.is(esValue)) {
|
|---|
| 246 | throw new TypeError("'set host' called on an object that is not a valid instance of URL.");
|
|---|
| 247 | }
|
|---|
| 248 |
|
|---|
| 249 | V = conversions["USVString"](V, { context: "Failed to set the 'host' property on 'URL': The provided value" });
|
|---|
| 250 |
|
|---|
| 251 | esValue[implSymbol]["host"] = V;
|
|---|
| 252 | }
|
|---|
| 253 |
|
|---|
| 254 | get hostname() {
|
|---|
| 255 | const esValue = this !== null && this !== undefined ? this : globalObject;
|
|---|
| 256 |
|
|---|
| 257 | if (!exports.is(esValue)) {
|
|---|
| 258 | throw new TypeError("'get hostname' called on an object that is not a valid instance of URL.");
|
|---|
| 259 | }
|
|---|
| 260 |
|
|---|
| 261 | return esValue[implSymbol]["hostname"];
|
|---|
| 262 | }
|
|---|
| 263 |
|
|---|
| 264 | set hostname(V) {
|
|---|
| 265 | const esValue = this !== null && this !== undefined ? this : globalObject;
|
|---|
| 266 |
|
|---|
| 267 | if (!exports.is(esValue)) {
|
|---|
| 268 | throw new TypeError("'set hostname' called on an object that is not a valid instance of URL.");
|
|---|
| 269 | }
|
|---|
| 270 |
|
|---|
| 271 | V = conversions["USVString"](V, {
|
|---|
| 272 | context: "Failed to set the 'hostname' property on 'URL': The provided value"
|
|---|
| 273 | });
|
|---|
| 274 |
|
|---|
| 275 | esValue[implSymbol]["hostname"] = V;
|
|---|
| 276 | }
|
|---|
| 277 |
|
|---|
| 278 | get port() {
|
|---|
| 279 | const esValue = this !== null && this !== undefined ? this : globalObject;
|
|---|
| 280 |
|
|---|
| 281 | if (!exports.is(esValue)) {
|
|---|
| 282 | throw new TypeError("'get port' called on an object that is not a valid instance of URL.");
|
|---|
| 283 | }
|
|---|
| 284 |
|
|---|
| 285 | return esValue[implSymbol]["port"];
|
|---|
| 286 | }
|
|---|
| 287 |
|
|---|
| 288 | set port(V) {
|
|---|
| 289 | const esValue = this !== null && this !== undefined ? this : globalObject;
|
|---|
| 290 |
|
|---|
| 291 | if (!exports.is(esValue)) {
|
|---|
| 292 | throw new TypeError("'set port' called on an object that is not a valid instance of URL.");
|
|---|
| 293 | }
|
|---|
| 294 |
|
|---|
| 295 | V = conversions["USVString"](V, { context: "Failed to set the 'port' property on 'URL': The provided value" });
|
|---|
| 296 |
|
|---|
| 297 | esValue[implSymbol]["port"] = V;
|
|---|
| 298 | }
|
|---|
| 299 |
|
|---|
| 300 | get pathname() {
|
|---|
| 301 | const esValue = this !== null && this !== undefined ? this : globalObject;
|
|---|
| 302 |
|
|---|
| 303 | if (!exports.is(esValue)) {
|
|---|
| 304 | throw new TypeError("'get pathname' called on an object that is not a valid instance of URL.");
|
|---|
| 305 | }
|
|---|
| 306 |
|
|---|
| 307 | return esValue[implSymbol]["pathname"];
|
|---|
| 308 | }
|
|---|
| 309 |
|
|---|
| 310 | set pathname(V) {
|
|---|
| 311 | const esValue = this !== null && this !== undefined ? this : globalObject;
|
|---|
| 312 |
|
|---|
| 313 | if (!exports.is(esValue)) {
|
|---|
| 314 | throw new TypeError("'set pathname' called on an object that is not a valid instance of URL.");
|
|---|
| 315 | }
|
|---|
| 316 |
|
|---|
| 317 | V = conversions["USVString"](V, {
|
|---|
| 318 | context: "Failed to set the 'pathname' property on 'URL': The provided value"
|
|---|
| 319 | });
|
|---|
| 320 |
|
|---|
| 321 | esValue[implSymbol]["pathname"] = V;
|
|---|
| 322 | }
|
|---|
| 323 |
|
|---|
| 324 | get search() {
|
|---|
| 325 | const esValue = this !== null && this !== undefined ? this : globalObject;
|
|---|
| 326 |
|
|---|
| 327 | if (!exports.is(esValue)) {
|
|---|
| 328 | throw new TypeError("'get search' called on an object that is not a valid instance of URL.");
|
|---|
| 329 | }
|
|---|
| 330 |
|
|---|
| 331 | return esValue[implSymbol]["search"];
|
|---|
| 332 | }
|
|---|
| 333 |
|
|---|
| 334 | set search(V) {
|
|---|
| 335 | const esValue = this !== null && this !== undefined ? this : globalObject;
|
|---|
| 336 |
|
|---|
| 337 | if (!exports.is(esValue)) {
|
|---|
| 338 | throw new TypeError("'set search' called on an object that is not a valid instance of URL.");
|
|---|
| 339 | }
|
|---|
| 340 |
|
|---|
| 341 | V = conversions["USVString"](V, { context: "Failed to set the 'search' property on 'URL': The provided value" });
|
|---|
| 342 |
|
|---|
| 343 | esValue[implSymbol]["search"] = V;
|
|---|
| 344 | }
|
|---|
| 345 |
|
|---|
| 346 | get searchParams() {
|
|---|
| 347 | const esValue = this !== null && this !== undefined ? this : globalObject;
|
|---|
| 348 |
|
|---|
| 349 | if (!exports.is(esValue)) {
|
|---|
| 350 | throw new TypeError("'get searchParams' called on an object that is not a valid instance of URL.");
|
|---|
| 351 | }
|
|---|
| 352 |
|
|---|
| 353 | return utils.getSameObject(this, "searchParams", () => {
|
|---|
| 354 | return utils.tryWrapperForImpl(esValue[implSymbol]["searchParams"]);
|
|---|
| 355 | });
|
|---|
| 356 | }
|
|---|
| 357 |
|
|---|
| 358 | get hash() {
|
|---|
| 359 | const esValue = this !== null && this !== undefined ? this : globalObject;
|
|---|
| 360 |
|
|---|
| 361 | if (!exports.is(esValue)) {
|
|---|
| 362 | throw new TypeError("'get hash' called on an object that is not a valid instance of URL.");
|
|---|
| 363 | }
|
|---|
| 364 |
|
|---|
| 365 | return esValue[implSymbol]["hash"];
|
|---|
| 366 | }
|
|---|
| 367 |
|
|---|
| 368 | set hash(V) {
|
|---|
| 369 | const esValue = this !== null && this !== undefined ? this : globalObject;
|
|---|
| 370 |
|
|---|
| 371 | if (!exports.is(esValue)) {
|
|---|
| 372 | throw new TypeError("'set hash' called on an object that is not a valid instance of URL.");
|
|---|
| 373 | }
|
|---|
| 374 |
|
|---|
| 375 | V = conversions["USVString"](V, { context: "Failed to set the 'hash' property on 'URL': The provided value" });
|
|---|
| 376 |
|
|---|
| 377 | esValue[implSymbol]["hash"] = V;
|
|---|
| 378 | }
|
|---|
| 379 | }
|
|---|
| 380 | Object.defineProperties(URL.prototype, {
|
|---|
| 381 | toJSON: { enumerable: true },
|
|---|
| 382 | href: { enumerable: true },
|
|---|
| 383 | toString: { enumerable: true },
|
|---|
| 384 | origin: { enumerable: true },
|
|---|
| 385 | protocol: { enumerable: true },
|
|---|
| 386 | username: { enumerable: true },
|
|---|
| 387 | password: { enumerable: true },
|
|---|
| 388 | host: { enumerable: true },
|
|---|
| 389 | hostname: { enumerable: true },
|
|---|
| 390 | port: { enumerable: true },
|
|---|
| 391 | pathname: { enumerable: true },
|
|---|
| 392 | search: { enumerable: true },
|
|---|
| 393 | searchParams: { enumerable: true },
|
|---|
| 394 | hash: { enumerable: true },
|
|---|
| 395 | [Symbol.toStringTag]: { value: "URL", configurable: true }
|
|---|
| 396 | });
|
|---|
| 397 | if (globalObject[ctorRegistrySymbol] === undefined) {
|
|---|
| 398 | globalObject[ctorRegistrySymbol] = Object.create(null);
|
|---|
| 399 | }
|
|---|
| 400 | globalObject[ctorRegistrySymbol][interfaceName] = URL;
|
|---|
| 401 |
|
|---|
| 402 | Object.defineProperty(globalObject, interfaceName, {
|
|---|
| 403 | configurable: true,
|
|---|
| 404 | writable: true,
|
|---|
| 405 | value: URL
|
|---|
| 406 | });
|
|---|
| 407 |
|
|---|
| 408 | if (globalNames.includes("Window")) {
|
|---|
| 409 | Object.defineProperty(globalObject, "webkitURL", {
|
|---|
| 410 | configurable: true,
|
|---|
| 411 | writable: true,
|
|---|
| 412 | value: URL
|
|---|
| 413 | });
|
|---|
| 414 | }
|
|---|
| 415 | };
|
|---|
| 416 |
|
|---|
| 417 | const Impl = require("./URL-impl.js");
|
|---|