GeezerVison is showing github.com — url: reuse existing context in href setter · nodejs/node@d06ea3e
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/internal/url.js +17-21Lines changed: 17 additions & 21 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -43,7 +43,7 @@ const { | 43 | 43 | domainToUnicode: _domainToUnicode, | 44 | 44 | encodeAuth, | 45 | 45 | toUSVString: _toUSVString, | 46 | | - parse: _parse, | | 46 | + parse, | 47 | 47 | setURLConstructor, | 48 | 48 | URL_FLAGS_CANNOT_BE_BASE, | 49 | 49 | URL_FLAGS_HAS_FRAGMENT, | @@ -243,14 +243,6 @@ function onParseError(flags, input) { | 243 | 243 | throw error; | 244 | 244 | } | 245 | 245 |
| 246 | | -// Reused by URL constructor and URL#href setter. | 247 | | -function parse(url, input, base) { | 248 | | - const base_context = base ? base[context] : undefined; | 249 | | - url[context] = new URLContext(); | 250 | | - _parse(input.trim(), -1, base_context, undefined, | 251 | | - onParseComplete.bind(url), onParseError); | 252 | | -} | 253 | | - | 254 | 246 | function onParseProtocolComplete(flags, protocol, username, password, | 255 | 247 | host, port, path, query, fragment) { | 256 | 248 | const ctx = this[context]; | @@ -319,10 +311,13 @@ class URL { | 319 | 311 | constructor(input, base) { | 320 | 312 | // toUSVString is not needed. | 321 | 313 | input = `${input}`; | | 314 | + let base_context; | 322 | 315 | if (base !== undefined) { | 323 | | - base = new URL(base); | | 316 | + base_context = new URL(base)[context]; | 324 | 317 | } | 325 | | - parse(this, input, base); | | 318 | + this[context] = new URLContext(); | | 319 | + parse(input, -1, base_context, undefined, onParseComplete.bind(this), | | 320 | + onParseError); | 326 | 321 | } | 327 | 322 |
| 328 | 323 | get [special]() { | @@ -445,7 +440,8 @@ Object.defineProperties(URL.prototype, { | 445 | 440 | set(input) { | 446 | 441 | // toUSVString is not needed. | 447 | 442 | input = `${input}`; | 448 | | - parse(this, input); | | 443 | + parse(input, -1, undefined, undefined, onParseComplete.bind(this), | | 444 | + onParseError); | 449 | 445 | } | 450 | 446 | }, | 451 | 447 | origin: { // readonly | @@ -491,8 +487,8 @@ Object.defineProperties(URL.prototype, { | 491 | 487 | (ctx.host === '' || ctx.host === null)) { | 492 | 488 | return; | 493 | 489 | } | 494 | | - _parse(scheme, kSchemeStart, null, ctx, | 495 | | - onParseProtocolComplete.bind(this)); | | 490 | + parse(scheme, kSchemeStart, null, ctx, | | 491 | + onParseProtocolComplete.bind(this)); | 496 | 492 | } | 497 | 493 | }, | 498 | 494 | username: { | @@ -555,7 +551,7 @@ Object.defineProperties(URL.prototype, { | 555 | 551 | // Cannot set the host if cannot-be-base is set | 556 | 552 | return; | 557 | 553 | } | 558 | | - _parse(host, kHost, null, ctx, onParseHostComplete.bind(this)); | | 554 | + parse(host, kHost, null, ctx, onParseHostComplete.bind(this)); | 559 | 555 | } | 560 | 556 | }, | 561 | 557 | hostname: { | @@ -572,7 +568,7 @@ Object.defineProperties(URL.prototype, { | 572 | 568 | // Cannot set the host if cannot-be-base is set | 573 | 569 | return; | 574 | 570 | } | 575 | | - _parse(host, kHostname, null, ctx, onParseHostnameComplete.bind(this)); | | 571 | + parse(host, kHostname, null, ctx, onParseHostnameComplete.bind(this)); | 576 | 572 | } | 577 | 573 | }, | 578 | 574 | port: { | @@ -592,7 +588,7 @@ Object.defineProperties(URL.prototype, { | 592 | 588 | ctx.port = null; | 593 | 589 | return; | 594 | 590 | } | 595 | | - _parse(port, kPort, null, ctx, onParsePortComplete.bind(this)); | | 591 | + parse(port, kPort, null, ctx, onParsePortComplete.bind(this)); | 596 | 592 | } | 597 | 593 | }, | 598 | 594 | pathname: { | @@ -611,8 +607,8 @@ Object.defineProperties(URL.prototype, { | 611 | 607 | path = `${path}`; | 612 | 608 | if (this[cannotBeBase]) | 613 | 609 | return; | 614 | | - _parse(path, kPathStart, null, this[context], | 615 | | - onParsePathComplete.bind(this)); | | 610 | + parse(path, kPathStart, null, this[context], | | 611 | + onParsePathComplete.bind(this)); | 616 | 612 | } | 617 | 613 | }, | 618 | 614 | search: { | @@ -635,7 +631,7 @@ Object.defineProperties(URL.prototype, { | 635 | 631 | ctx.query = ''; | 636 | 632 | ctx.flags |= URL_FLAGS_HAS_QUERY; | 637 | 633 | if (search) { | 638 | | - _parse(search, kQuery, null, ctx, onParseSearchComplete.bind(this)); | | 634 | + parse(search, kQuery, null, ctx, onParseSearchComplete.bind(this)); | 639 | 635 | } | 640 | 636 | } | 641 | 637 | initSearchParams(this[searchParams], search); | @@ -669,7 +665,7 @@ Object.defineProperties(URL.prototype, { | 669 | 665 | if (hash[0] === '#') hash = hash.slice(1); | 670 | 666 | ctx.fragment = ''; | 671 | 667 | ctx.flags |= URL_FLAGS_HAS_FRAGMENT; | 672 | | - _parse(hash, kFragment, null, ctx, onParseHashComplete.bind(this)); | | 668 | + parse(hash, kFragment, null, ctx, onParseHashComplete.bind(this)); | 673 | 669 | } | 674 | 670 | }, | 675 | 671 | toJSON: { | |
Collapse file Copy file name to clipboardExpand all lines: src/node_url.cc +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1375,6 +1375,7 @@ void URL::Parse(const char* input, | 1375 | 1375 | else | 1376 | 1376 | break; | 1377 | 1377 | } | | 1378 | + input = p; | 1378 | 1379 | len = end - p; | 1379 | 1380 | } | 1380 | 1381 |
| |
Collapse file Copy file name to clipboard +15Lines changed: 15 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +'use strict'; | | 2 | + | | 3 | +// Tests below are not from WPT. | | 4 | +const common = require('../common'); | | 5 | +const assert = require('assert'); | | 6 | + | | 7 | +const ref = new URL('http://example.com/path'); | | 8 | +const url = new URL('http://example.com/path'); | | 9 | +common.expectsError(() => { | | 10 | + url.href = ''; | | 11 | +}, { | | 12 | + type: TypeError | | 13 | +}); | | 14 | + | | 15 | +assert.deepStrictEqual(url, ref); |
|