-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This PR adds polyfils for `URL.parse` and `URL.canParse`.
- Loading branch information
Showing
8 changed files
with
220 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
"URL" in self && "canParse" in self.URL; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = "*" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
"URL" in self && "parse" in self.URL; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
}); | ||
}); | ||
}); | ||
}); |