Skip to content

Commit

Permalink
Revert "Use the same parse processing at contractor of URL with sette…
Browse files Browse the repository at this point in the history
…rs (denoland#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.

denoland#1549 (comment)

This reverts commit 9e1f5cc.
  • Loading branch information
ry committed Jan 21, 2019
1 parent 106fe1f commit 7eb74ba
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 57 deletions.
61 changes: 17 additions & 44 deletions js/url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ interface URLParts {
password: string;
hostname: string;
port: string;
pathname: string;
path: string;
query: string;
hash: string;
}

const patterns = {
protocol: "(?:([^:/?#]+):)",
authority: "(?:https://([^/?#]*))",
pathname: "([^?#]*)",
path: "([^?#]*)",
query: "(\\?[^#]*)",
hash: "(#.*)",

Expand All @@ -25,7 +25,7 @@ const patterns = {
};

const urlRegExp = new RegExp(
`^${patterns.protocol}?${patterns.authority}?${patterns.pathname}${
`^${patterns.protocol}?${patterns.authority}?${patterns.path}${
patterns.query
}?${patterns.hash}?`
);
Expand All @@ -40,17 +40,6 @@ const searchParamsMethods: Array<keyof urlSearchParams.URLSearchParams> = [
"set"
];

const initializedURLParts = {
protocol: "",
username: "",
password: "",
hostname: "",
port: "",
pathname: "",
query: "",
hash: ""
};

function parse(url: string): URLParts | undefined {
const urlMatch = urlRegExp.exec(url);
if (urlMatch) {
Expand All @@ -65,7 +54,7 @@ function parse(url: string): URLParts | undefined {
password: authorityMatch[2] || "",
hostname: authorityMatch[3] || "",
port: authorityMatch[4] || "",
pathname: urlMatch[3] || "",
path: urlMatch[3] || "",
query: urlMatch[4] || "",
hash: urlMatch[5] || ""
};
Expand Down Expand Up @@ -166,16 +155,16 @@ export class URL {
}

get pathname(): string {
return this._parts.pathname ? this._parts.pathname : "/";
return this._parts.path ? this._parts.path : "/";
}

set pathname(value: string) {
value = unescape(String(value));
if (!value || value.charAt(0) !== "/") {
value = `/${value}`;
}
// pathnames can contain % unescaped
this._parts.pathname = escape(value).replace(/%25/g, "%");
// paths can contain % unescaped
this._parts.path = escape(value).replace(/%25/g, "%");
}

get port(): string {
Expand Down Expand Up @@ -229,15 +218,6 @@ export class URL {
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) {
let baseParts: URLParts | undefined;
if (base) {
Expand All @@ -254,24 +234,17 @@ export class URL {

if (urlParts.protocol) {
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) {
this._parts = initializedURLParts;
this.protocol = baseParts.protocol;
this.username = baseParts.username;
this.password = baseParts.password;
this.hostname = baseParts.hostname;
this.port = baseParts.port;
this.pathname = urlParts.pathname || baseParts.pathname;
this.query = urlParts.query || baseParts.query;
this.hash = urlParts.hash;
this._parts = {
protocol: baseParts.protocol,
username: baseParts.username,
password: baseParts.password,
hostname: baseParts.hostname,
port: baseParts.port,
path: urlParts.path || baseParts.path,
query: urlParts.query || baseParts.query,
hash: urlParts.hash
};
} else {
throw new TypeError("URL requires a base URL.");
}
Expand Down
13 changes: 0 additions & 13 deletions js/url_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,6 @@ test(function urlParsing() {
);
});

test(function constractorParsing() {
const url = new URL("http:https://どめいん.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() {
const url = new URL(
"https://foo:[email protected]:8000/qux/quux?foo=bar&baz=12#qat"
Expand Down

0 comments on commit 7eb74ba

Please sign in to comment.