1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-22 15:06:54 -05:00

Revert "Use the same parse processing at contractor of URL with setters (#1549)"

Right now every instance of URL which has a basePath passed will share
the same instance of parts, so a change to one of them will change them
all.

https://github.com/denoland/deno/pull/1549#issuecomment-455896081

This reverts commit 9e1f5ccb8b.
This commit is contained in:
Ryan Dahl 2019-01-20 21:31:13 -05:00
parent 106fe1f762
commit 7eb74ba0d6
2 changed files with 17 additions and 57 deletions

View file

@ -7,7 +7,7 @@ interface URLParts {
password: string; password: string;
hostname: string; hostname: string;
port: string; port: string;
pathname: string; path: string;
query: string; query: string;
hash: string; hash: string;
} }
@ -15,7 +15,7 @@ interface URLParts {
const patterns = { const patterns = {
protocol: "(?:([^:/?#]+):)", protocol: "(?:([^:/?#]+):)",
authority: "(?://([^/?#]*))", authority: "(?://([^/?#]*))",
pathname: "([^?#]*)", path: "([^?#]*)",
query: "(\\?[^#]*)", query: "(\\?[^#]*)",
hash: "(#.*)", hash: "(#.*)",
@ -25,7 +25,7 @@ const patterns = {
}; };
const urlRegExp = new RegExp( const urlRegExp = new RegExp(
`^${patterns.protocol}?${patterns.authority}?${patterns.pathname}${ `^${patterns.protocol}?${patterns.authority}?${patterns.path}${
patterns.query patterns.query
}?${patterns.hash}?` }?${patterns.hash}?`
); );
@ -40,17 +40,6 @@ const searchParamsMethods: Array<keyof urlSearchParams.URLSearchParams> = [
"set" "set"
]; ];
const initializedURLParts = {
protocol: "",
username: "",
password: "",
hostname: "",
port: "",
pathname: "",
query: "",
hash: ""
};
function parse(url: string): URLParts | undefined { function parse(url: string): URLParts | undefined {
const urlMatch = urlRegExp.exec(url); const urlMatch = urlRegExp.exec(url);
if (urlMatch) { if (urlMatch) {
@ -65,7 +54,7 @@ function parse(url: string): URLParts | undefined {
password: authorityMatch[2] || "", password: authorityMatch[2] || "",
hostname: authorityMatch[3] || "", hostname: authorityMatch[3] || "",
port: authorityMatch[4] || "", port: authorityMatch[4] || "",
pathname: urlMatch[3] || "", path: urlMatch[3] || "",
query: urlMatch[4] || "", query: urlMatch[4] || "",
hash: urlMatch[5] || "" hash: urlMatch[5] || ""
}; };
@ -166,7 +155,7 @@ export class URL {
} }
get pathname(): string { get pathname(): string {
return this._parts.pathname ? this._parts.pathname : "/"; return this._parts.path ? this._parts.path : "/";
} }
set pathname(value: string) { set pathname(value: string) {
@ -174,8 +163,8 @@ export class URL {
if (!value || value.charAt(0) !== "/") { if (!value || value.charAt(0) !== "/") {
value = `/${value}`; value = `/${value}`;
} }
// pathnames can contain % unescaped // paths can contain % unescaped
this._parts.pathname = escape(value).replace(/%25/g, "%"); this._parts.path = escape(value).replace(/%25/g, "%");
} }
get port(): string { get port(): string {
@ -229,15 +218,6 @@ export class URL {
return this._searchParams; return this._searchParams;
} }
get query(): string {
return this._parts.query;
}
set query(value: string) {
value = String(value);
this._parts.query = value;
}
constructor(url: string, base?: string | URL) { constructor(url: string, base?: string | URL) {
let baseParts: URLParts | undefined; let baseParts: URLParts | undefined;
if (base) { if (base) {
@ -254,24 +234,17 @@ export class URL {
if (urlParts.protocol) { if (urlParts.protocol) {
this._parts = urlParts; this._parts = urlParts;
this.protocol = urlParts.protocol;
this.username = urlParts.username;
this.password = urlParts.password;
this.hostname = urlParts.hostname;
this.port = urlParts.port;
this.pathname = urlParts.pathname;
this.query = urlParts.query;
this.hash = urlParts.hash;
} else if (baseParts) { } else if (baseParts) {
this._parts = initializedURLParts; this._parts = {
this.protocol = baseParts.protocol; protocol: baseParts.protocol,
this.username = baseParts.username; username: baseParts.username,
this.password = baseParts.password; password: baseParts.password,
this.hostname = baseParts.hostname; hostname: baseParts.hostname,
this.port = baseParts.port; port: baseParts.port,
this.pathname = urlParts.pathname || baseParts.pathname; path: urlParts.path || baseParts.path,
this.query = urlParts.query || baseParts.query; query: urlParts.query || baseParts.query,
this.hash = urlParts.hash; hash: urlParts.hash
};
} else { } else {
throw new TypeError("URL requires a base URL."); throw new TypeError("URL requires a base URL.");
} }

View file

@ -31,19 +31,6 @@ test(function urlParsing() {
); );
}); });
test(function constractorParsing() {
const url = new URL("http://どめいん.com/ぱす?きー=ばりゅー#はっしゅ");
const { host, pathname, search, hash } = url;
url.host = "どめいん.com";
url.pathname = "/ぱす";
url.search = "?きー=ばりゅー";
url.hash = "#はっしゅ";
assertEqual(host, url.host);
assertEqual(pathname, url.pathname);
assertEqual(search, url.search);
assertEqual(hash, url.hash);
});
test(function urlModifications() { test(function urlModifications() {
const url = new URL( const url = new URL(
"https://foo:bar@baz.qat:8000/qux/quux?foo=bar&baz=12#qat" "https://foo:bar@baz.qat:8000/qux/quux?foo=bar&baz=12#qat"