Implement URL::parse() (#32819)

* Implement URL::parse()

Signed-off-by: Shane Handley <shanehandley@fastmail.com>

* fix: Make handling more idiomatic

Signed-off-by: Shane Handley <shanehandley@fastmail.com>

---------

Signed-off-by: Shane Handley <shanehandley@fastmail.com>
This commit is contained in:
shanehandley 2024-07-22 18:48:16 +10:00 committed by GitHub
parent a007baa4cf
commit 45eabad169
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 25 additions and 129 deletions

View file

@ -104,7 +104,7 @@ impl URL {
};
// Skip the steps below.
// Instead of construcing a new `URLSearchParams` object here, construct it
// Instead of constructing a new `URLSearchParams` object here, construct it
// on-demand inside `URL::SearchParams`.
//
// Step 3. Let query be parsedURLs query.
@ -133,6 +133,28 @@ impl URL {
ServoUrl::parse_with_base(parsed_base.as_ref(), &url.0).is_ok()
}
/// <https://url.spec.whatwg.org/#dom-url-parse>
pub fn Parse(
global: &GlobalScope,
url: USVString,
base: Option<USVString>,
) -> Option<DomRoot<URL>> {
// Step 1: Let parsedURL be the result of running the API URL parser on url with base,
// if given.
let parsed_base = base.and_then(|base| ServoUrl::parse(base.0.as_str()).ok());
let parsed_url = ServoUrl::parse_with_base(parsed_base.as_ref(), &url.0);
// Step 2: If parsedURL is failure, then return null.
// Step 3: Let url be a new URL object.
// Step 4: Initialize url with parsedURL.
// Step 5: Return url.
// These steps are all handled while mapping the Result to an Option<ServoUrl>.
// Regarding initialization, the same condition should apply here as stated in the comments
// in Self::Constructor above - construct it on-demand inside `URL::SearchParams`.
Some(URL::new(global, None, parsed_url.ok()?))
}
/// <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
@ -183,6 +205,7 @@ impl URL {
}
}
#[allow(non_snake_case)]
impl URLMethods for URL {
/// <https://url.spec.whatwg.org/#dom-url-hash>
fn Hash(&self) -> USVString {

View file

@ -8,6 +8,7 @@
interface URL {
[Throws] constructor(USVString url, optional USVString base);
static URL? parse(USVString url, optional USVString base);
static boolean canParse(USVString url, optional USVString base);
[SetterThrows]

View file

@ -1,14 +0,0 @@
[idlharness.any.worker.html]
[URL interface: operation parse(USVString, optional USVString)]
expected: FAIL
[URL interface: calling parse(USVString, optional USVString) on new URL("http://foo") with too few arguments must throw TypeError]
expected: FAIL
[idlharness.any.html]
[URL interface: operation parse(USVString, optional USVString)]
expected: FAIL
[URL interface: calling parse(USVString, optional USVString) on new URL("http://foo") with too few arguments must throw TypeError]
expected: FAIL

View file

@ -1,50 +0,0 @@
[url-statics-parse.any.worker.html]
[URL.parse(undefined, undefined)]
expected: FAIL
[URL.parse(aaa:b, undefined)]
expected: FAIL
[URL.parse(undefined, aaa:b)]
expected: FAIL
[URL.parse(aaa:/b, undefined)]
expected: FAIL
[URL.parse(undefined, aaa:/b)]
expected: FAIL
[URL.parse(https://test:test, undefined)]
expected: FAIL
[URL.parse(a, https://b/)]
expected: FAIL
[URL.parse() should return a unique object]
expected: FAIL
[url-statics-parse.any.html]
[URL.parse(undefined, undefined)]
expected: FAIL
[URL.parse(aaa:b, undefined)]
expected: FAIL
[URL.parse(undefined, aaa:b)]
expected: FAIL
[URL.parse(aaa:/b, undefined)]
expected: FAIL
[URL.parse(undefined, aaa:/b)]
expected: FAIL
[URL.parse(https://test:test, undefined)]
expected: FAIL
[URL.parse(a, https://b/)]
expected: FAIL
[URL.parse() should return a unique object]
expected: FAIL

View file

@ -1,14 +0,0 @@
[idlharness.any.worker.html]
[URL interface: operation parse(USVString, optional USVString)]
expected: FAIL
[URL interface: calling parse(USVString, optional USVString) on new URL("http://foo") with too few arguments must throw TypeError]
expected: FAIL
[idlharness.any.html]
[URL interface: operation parse(USVString, optional USVString)]
expected: FAIL
[URL interface: calling parse(USVString, optional USVString) on new URL("http://foo") with too few arguments must throw TypeError]
expected: FAIL

View file

@ -1,50 +0,0 @@
[url-statics-parse.any.worker.html]
[URL.parse(undefined, undefined)]
expected: FAIL
[URL.parse(aaa:b, undefined)]
expected: FAIL
[URL.parse(undefined, aaa:b)]
expected: FAIL
[URL.parse(aaa:/b, undefined)]
expected: FAIL
[URL.parse(undefined, aaa:/b)]
expected: FAIL
[URL.parse(https://test:test, undefined)]
expected: FAIL
[URL.parse(a, https://b/)]
expected: FAIL
[URL.parse() should return a unique object]
expected: FAIL
[url-statics-parse.any.html]
[URL.parse(undefined, undefined)]
expected: FAIL
[URL.parse(aaa:b, undefined)]
expected: FAIL
[URL.parse(undefined, aaa:b)]
expected: FAIL
[URL.parse(aaa:/b, undefined)]
expected: FAIL
[URL.parse(undefined, aaa:/b)]
expected: FAIL
[URL.parse(https://test:test, undefined)]
expected: FAIL
[URL.parse(a, https://b/)]
expected: FAIL
[URL.parse() should return a unique object]
expected: FAIL