-
Notifications
You must be signed in to change notification settings - Fork 30.5k
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
Two URL parser fixes #24495
Two URL parser fixes #24495
Changes from 3 commits
9fed0e0
6cf1769
8be84ac
19634c7
61114b1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,7 +42,7 @@ const { | |
domainToUnicode: _domainToUnicode, | ||
encodeAuth, | ||
toUSVString: _toUSVString, | ||
parse: _parse, | ||
parse, | ||
setURLConstructor, | ||
URL_FLAGS_CANNOT_BE_BASE, | ||
URL_FLAGS_HAS_FRAGMENT, | ||
|
@@ -242,21 +242,6 @@ function onParseError(flags, input) { | |
throw error; | ||
} | ||
|
||
// Reused by URL constructor and URL#href setter. | ||
function parse(url, input, base) { | ||
const base_context = base ? base[context] : undefined; | ||
// In the URL#href setter | ||
if (!url[context]) { | ||
Object.defineProperty(url, context, { | ||
enumerable: false, | ||
configurable: false, | ||
value: new URLContext() | ||
}); | ||
} | ||
_parse(input.trim(), -1, base_context, undefined, | ||
onParseComplete.bind(url), onParseError); | ||
} | ||
|
||
function onParseProtocolComplete(flags, protocol, username, password, | ||
host, port, path, query, fragment) { | ||
const ctx = this[context]; | ||
|
@@ -325,10 +310,13 @@ class URL { | |
constructor(input, base) { | ||
// toUSVString is not needed. | ||
input = `${input}`; | ||
let base_context; | ||
if (base !== undefined) { | ||
base = new URL(base); | ||
base_context = new URL(base)[context]; | ||
} | ||
parse(this, input, base); | ||
this[context] = new URLContext(); | ||
parse(input, -1, base_context, undefined, onParseComplete.bind(this), | ||
onParseError); | ||
} | ||
|
||
get [special]() { | ||
|
@@ -451,7 +439,8 @@ Object.defineProperties(URL.prototype, { | |
set(input) { | ||
// toUSVString is not needed. | ||
input = `${input}`; | ||
parse(this, input); | ||
parse(input, -1, undefined, undefined, onParseComplete.bind(this), | ||
onParseError); | ||
} | ||
}, | ||
origin: { // readonly | ||
|
@@ -497,7 +486,7 @@ Object.defineProperties(URL.prototype, { | |
(ctx.host === '' || ctx.host === null)) { | ||
return; | ||
} | ||
_parse(scheme, kSchemeStart, null, ctx, | ||
parse(scheme, kSchemeStart, null, ctx, | ||
onParseProtocolComplete.bind(this)); | ||
} | ||
}, | ||
|
@@ -561,7 +550,7 @@ Object.defineProperties(URL.prototype, { | |
// Cannot set the host if cannot-be-base is set | ||
return; | ||
} | ||
_parse(host, kHost, null, ctx, onParseHostComplete.bind(this)); | ||
parse(host, kHost, null, ctx, onParseHostComplete.bind(this)); | ||
} | ||
}, | ||
hostname: { | ||
|
@@ -578,7 +567,7 @@ Object.defineProperties(URL.prototype, { | |
// Cannot set the host if cannot-be-base is set | ||
return; | ||
} | ||
_parse(host, kHostname, null, ctx, onParseHostnameComplete.bind(this)); | ||
parse(host, kHostname, null, ctx, onParseHostnameComplete.bind(this)); | ||
} | ||
}, | ||
port: { | ||
|
@@ -598,7 +587,7 @@ Object.defineProperties(URL.prototype, { | |
ctx.port = null; | ||
return; | ||
} | ||
_parse(port, kPort, null, ctx, onParsePortComplete.bind(this)); | ||
parse(port, kPort, null, ctx, onParsePortComplete.bind(this)); | ||
} | ||
}, | ||
pathname: { | ||
|
@@ -617,7 +606,7 @@ Object.defineProperties(URL.prototype, { | |
path = `${path}`; | ||
if (this[cannotBeBase]) | ||
return; | ||
_parse(path, kPathStart, null, this[context], | ||
parse(path, kPathStart, null, this[context], | ||
onParsePathComplete.bind(this)); | ||
} | ||
}, | ||
|
@@ -641,7 +630,7 @@ Object.defineProperties(URL.prototype, { | |
ctx.query = ''; | ||
ctx.flags |= URL_FLAGS_HAS_QUERY; | ||
if (search) { | ||
_parse(search, kQuery, null, ctx, onParseSearchComplete.bind(this)); | ||
parse(search, kQuery, null, ctx, onParseSearchComplete.bind(this)); | ||
} | ||
} | ||
initSearchParams(this[searchParams], search); | ||
|
@@ -675,7 +664,7 @@ Object.defineProperties(URL.prototype, { | |
if (hash[0] === '#') hash = hash.slice(1); | ||
ctx.fragment = ''; | ||
ctx.flags |= URL_FLAGS_HAS_FRAGMENT; | ||
_parse(hash, kFragment, null, ctx, onParseHashComplete.bind(this)); | ||
parse(hash, kFragment, null, ctx, onParseHashComplete.bind(this)); | ||
} | ||
}, | ||
toJSON: { | ||
|
@@ -1444,11 +1433,7 @@ function toPathIfFileURL(fileURLOrPath) { | |
} | ||
|
||
function NativeURL(ctx) { | ||
Object.defineProperty(this, context, { | ||
enumerable: false, | ||
configurable: false, | ||
value: ctx | ||
}); | ||
this[context] = ctx; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am wondering if we can just remove this class and inline the calls const url = new NativeURL(ctx);
url[searchParams] = new URLSearchParams();
url[searchParams][context] = url; with const url = Object.create(URL.prototype);
url[context] = ctx;
const params = new URLSearchParams();
params[context] = url;
url[searchParams] = params; (Not sure about the performance implications, though, but it seems cleaner if we reduce one more class that looks like URL-related since this is only used by the C++ classes, and there are already many URL classes so it's a bit hard to find which one is the one you are looking for) |
||
} | ||
NativeURL.prototype = URL.prototype; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
'use strict'; | ||
|
||
// Tests below are not from WPT. | ||
const common = require('../common'); | ||
const assert = require('assert'); | ||
|
||
const ref = new URL('http://example.com/path'); | ||
const url = new URL('http://example.com/path'); | ||
common.expectsError(() => { | ||
url.href = ''; | ||
}, { | ||
type: TypeError | ||
}); | ||
|
||
assert.deepStrictEqual(url, ref); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be camelCase, for consistency?