Skip to content

Commit

Permalink
Adds URL.parse and URL.canParse (#54)
Browse files Browse the repository at this point in the history
This PR adds polyfils for `URL.parse` and `URL.canParse`.
  • Loading branch information
mhassan1 authored Jul 24, 2024
1 parent ad74160 commit e5d510b
Show file tree
Hide file tree
Showing 8 changed files with 220 additions and 0 deletions.
24 changes: 24 additions & 0 deletions polyfills/URL/canParse/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
dependencies = [
"_ESAbstract.ToString",
"URL",
]
license = "MIT"
docs = "https://developer.mozilla.org/en-US/docs/Web/API/URL/canParse_static"
spec = "https://url.spec.whatwg.org/#ref-for-dom-url-canparse"

[browsers]
android = "*"
bb = "*"
chrome = "<120"
edge = "*"
edge_mob = "*"
firefox = "<115"
firefox_mob = "<115"
ie = "*"
ie_mob = "*"
opera = "<106"
op_mob = "<80"
op_mini = "*"
safari = "<17.0"
ios_saf = "<17.0"
samsung_mob = "<25.0"
1 change: 1 addition & 0 deletions polyfills/URL/canParse/detect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"URL" in self && "canParse" in self.URL;
26 changes: 26 additions & 0 deletions polyfills/URL/canParse/polyfill.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/* global ToString */
(function (global) {
"use strict";
global.URL.canParse = function canParse(url /* , base */) {
if (arguments.length < 1) {
throw new TypeError("Not enough arguments");
}
var urlString = ToString(url);
if (arguments.length < 2 || arguments[1] === undefined) {
try {
new URL(urlString);
return true;
} catch (ignore) {
return false;
}
} else {
var base = ToString(arguments[1]);
try {
new URL(urlString, base);
return true;
} catch (ignore) {
return false;
}
}
};
})(self);
56 changes: 56 additions & 0 deletions polyfills/URL/canParse/polyfill.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
it("is a function", function () {
proclaim.isFunction(URL.canParse);
});

it("has correct arity", function () {
proclaim.arity(URL.canParse, 1);
});

it("has correct name", function () {
proclaim.hasName(URL.canParse, "canParse");
});

it("is enumerable", function () {
proclaim.isEnumerable(URL, "canParse");
});

describe("canParse", function () {
it("returns true if it can parse", function () {
proclaim.isTrue(URL.canParse("http://hello/there"));
proclaim.isTrue(URL.canParse("/there", "http://hello"));
});

it("returns false if it can't parse", function () {
proclaim.isFalse(URL.canParse("/there"));
});

it("throws for too few arguments", function () {
proclaim.throws(function () {
URL.canParse();
});
});

it("throws for a stringifier that throws", function () {
proclaim.throws(function () {
URL.canParse({
toString: function () {
throw new Error();
}
});
});
proclaim.throws(function () {
URL.canParse("http://hello", {
toString: function () {
throw new Error();
}
});
});
proclaim.throws(function () {
URL.canParse("/there", {
toString: function () {
throw new Error();
}
});
});
});
});
24 changes: 24 additions & 0 deletions polyfills/URL/parse/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
dependencies = [
"_ESAbstract.ToString",
"URL",
]
license = "MIT"
docs = "https://developer.mozilla.org/en-US/docs/Web/API/URL/parse_static"
spec = "https://url.spec.whatwg.org/#ref-for-dom-url-parse"

[browsers]
android = "*"
bb = "*"
chrome = "<126"
edge = "*"
edge_mob = "*"
firefox = "<126"
firefox_mob = "<126"
ie = "*"
ie_mob = "*"
opera = "*"
op_mob = "*"
op_mini = "*"
safari = "*"
ios_saf = "*"
samsung_mob = "*"
1 change: 1 addition & 0 deletions polyfills/URL/parse/detect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"URL" in self && "parse" in self.URL;
24 changes: 24 additions & 0 deletions polyfills/URL/parse/polyfill.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/* global ToString */
(function (global) {
"use strict";
global.URL.parse = function parse(url /* , base */) {
if (arguments.length < 1) {
throw new TypeError("Not enough arguments");
}
var urlString = ToString(url);
if (arguments.length < 2 || arguments[1] === undefined) {
try {
return new URL(urlString);
} catch (ignore) {
return null;
}
} else {
var base = ToString(arguments[1]);
try {
return new URL(urlString, base);
} catch (ignore) {
return null;
}
}
};
})(self);
64 changes: 64 additions & 0 deletions polyfills/URL/parse/polyfill.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
it("is a function", function () {
proclaim.isFunction(URL.parse);
});

it("has correct arity", function () {
proclaim.arity(URL.parse, 1);
});

it("has correct name", function () {
proclaim.hasName(URL.parse, "parse");
});

it("is enumerable", function () {
proclaim.isEnumerable(URL, "parse");
});

describe("parse", function () {
it("returns parsed url if it can parse", function () {
proclaim.instanceOf(URL.parse("http://hello/there"), URL);
proclaim.equal(
URL.parse("http://hello/there").href,
new URL("http://hello/there").href
);
proclaim.instanceOf(URL.parse("/there", "http://hello"), URL);
proclaim.equal(
URL.parse("/there", "http://hello").href,
new URL("/there", "http://hello").href
);
});

it("returns null if it can't parse", function () {
proclaim.isNull(URL.parse("/there"));
});

it("throws for too few arguments", function () {
proclaim.throws(function () {
URL.parse();
});
});

it("throws for a stringifier that throws", function () {
proclaim.throws(function () {
URL.parse({
toString: function () {
throw new Error();
}
});
});
proclaim.throws(function () {
URL.parse("http://hello", {
toString: function () {
throw new Error();
}
});
});
proclaim.throws(function () {
URL.parse("/there", {
toString: function () {
throw new Error();
}
});
});
});
});

0 comments on commit e5d510b

Please sign in to comment.