diff --git a/components/script/dom/url.rs b/components/script/dom/url.rs index ab235816f4c..c1533bec159 100644 --- a/components/script/dom/url.rs +++ b/components/script/dom/url.rs @@ -111,6 +111,27 @@ impl URL { Ok(result) } + // https://url.spec.whatwg.org/#dom-url-canparse + pub fn CanParse(_global: &GlobalScope, url: USVString, base: Option) -> bool { + // Step 1. + let parsed_base = match base { + None => None, + Some(base) => match ServoUrl::parse(&base.0) { + Ok(base) => Some(base), + Err(_) => { + // Step 2.1 + return false; + }, + }, + }; + match ServoUrl::parse_with_base(parsed_base.as_ref(), &url.0) { + // Step 3 + Ok(_) => true, + // Step 2.2 + Err(_) => false, + } + } + // https://w3c.github.io/FileAPI/#dfn-createObjectURL pub fn CreateObjectURL(global: &GlobalScope, blob: &Blob) -> DOMString { // XXX: Second field is an unicode-serialized Origin, it is a temporary workaround diff --git a/components/script/dom/webidls/URL.webidl b/components/script/dom/webidls/URL.webidl index 7059fe43ea5..2870cd53940 100644 --- a/components/script/dom/webidls/URL.webidl +++ b/components/script/dom/webidls/URL.webidl @@ -7,6 +7,9 @@ LegacyWindowAlias=webkitURL] interface URL { [Throws] constructor(USVString url, optional USVString base); + + static boolean canParse(USVString url, optional USVString base); + [SetterThrows] stringifier attribute USVString href; readonly attribute USVString origin; diff --git a/tests/wpt/metadata/url/url-statics-canparse.any.js.ini b/tests/wpt/metadata/url/url-statics-canparse.any.js.ini deleted file mode 100644 index 08c038b74e8..00000000000 --- a/tests/wpt/metadata/url/url-statics-canparse.any.js.ini +++ /dev/null @@ -1,44 +0,0 @@ -[url-statics-canparse.any.html] - [URL.canParse(undefined, undefined)] - expected: FAIL - - [URL.canParse(a:b, undefined)] - expected: FAIL - - [URL.canParse(undefined, a:b)] - expected: FAIL - - [URL.canParse(a:/b, undefined)] - expected: FAIL - - [URL.canParse(undefined, a:/b)] - expected: FAIL - - [URL.canParse(https://test:test, undefined)] - expected: FAIL - - [URL.canParse(a, https://b/)] - expected: FAIL - - -[url-statics-canparse.any.worker.html] - [URL.canParse(undefined, undefined)] - expected: FAIL - - [URL.canParse(a:b, undefined)] - expected: FAIL - - [URL.canParse(undefined, a:b)] - expected: FAIL - - [URL.canParse(a:/b, undefined)] - expected: FAIL - - [URL.canParse(undefined, a:/b)] - expected: FAIL - - [URL.canParse(https://test:test, undefined)] - expected: FAIL - - [URL.canParse(a, https://b/)] - expected: FAIL