diff --git a/Tests/LibWeb/Text/expected/URL/url.txt b/Tests/LibWeb/Text/expected/URL/url.txt index 1129f807508..ecb93109d14 100644 --- a/Tests/LibWeb/Text/expected/URL/url.txt +++ b/Tests/LibWeb/Text/expected/URL/url.txt @@ -78,3 +78,84 @@ port => '' pathname => '/d:/' search => '' hash => '' +========================================= +URL.parse('ftp://serenityos.org:21', undefined) +protocol => 'ftp:' +username => '' +password => '' +host => 'serenityos.org' +hostname => 'serenityos.org' +port => '' +pathname => '/' +search => '' +hash => '' +URL.parse('http://[0:1:0:1:0:1:0:1]', undefined) +protocol => 'http:' +username => '' +password => '' +host => '[0:1:0:1:0:1:0:1]' +hostname => '[0:1:0:1:0:1:0:1]' +port => '' +pathname => '/' +search => '' +hash => '' +URL.parse('http://[1:0:1:0:1:0:1:0]', undefined) +protocol => 'http:' +username => '' +password => '' +host => '[1:0:1:0:1:0:1:0]' +hostname => '[1:0:1:0:1:0:1:0]' +port => '' +pathname => '/' +search => '' +hash => '' +URL.parse('http://[1:1:0:0:1:0:0:0]/', undefined) +protocol => 'http:' +username => '' +password => '' +host => '[1:1:0:0:1::]' +hostname => '[1:1:0:0:1::]' +port => '' +pathname => '/' +search => '' +hash => '' +URL.parse('unknown://serenityos.org:0', undefined) +protocol => 'unknown:' +username => '' +password => '' +host => 'serenityos.org:0' +hostname => 'serenityos.org' +port => '0' +pathname => '' +search => '' +hash => '' +URL.parse('http://serenityos.org/cat?dog#meow"woof', undefined) +protocol => 'http:' +username => '' +password => '' +host => 'serenityos.org' +hostname => 'serenityos.org' +port => '' +pathname => '/cat' +search => '?dog' +hash => '#meow%22woof' +URL.parse('/hello', 'file://friends/') +protocol => 'file:' +username => '' +password => '' +host => 'friends' +hostname => 'friends' +port => '' +pathname => '/hello' +search => '' +hash => '' +URL.parse('//d:/..', 'file:///C:/a/b') +protocol => 'file:' +username => '' +password => '' +host => '' +hostname => '' +port => '' +pathname => '/d:/' +search => '' +hash => '' diff --git a/Tests/LibWeb/Text/input/URL/url.html b/Tests/LibWeb/Text/input/URL/url.html index 3994ab61c2d..e6099a7274f 100644 --- a/Tests/LibWeb/Text/input/URL/url.html +++ b/Tests/LibWeb/Text/input/URL/url.html @@ -1,13 +1,7 @@ diff --git a/Userland/Libraries/LibWeb/DOMURL/DOMURL.cpp b/Userland/Libraries/LibWeb/DOMURL/DOMURL.cpp index 5d4049e4d05..65e2ce93a9f 100644 --- a/Userland/Libraries/LibWeb/DOMURL/DOMURL.cpp +++ b/Userland/Libraries/LibWeb/DOMURL/DOMURL.cpp @@ -70,6 +70,24 @@ JS::NonnullGCPtr DOMURL::initialize_a_url(JS::Realm& realm, URL::URL con return result_url; } +// https://url.spec.whatwg.org/#dom-url-parse +JS::GCPtr DOMURL::parse_for_bindings(JS::VM& vm, String const& url, Optional const& base) +{ + auto& realm = *vm.current_realm(); + + // 1. Let parsedURL be the result of running the API URL parser on url with base, if given. + auto parsed_url = parse_api_url(url, base); + + // 2. If parsedURL is failure, then return null. + if (!parsed_url.has_value()) + return nullptr; + + // 3. Let url be a new URL object. + // 4. Initialize url with parsedURL. + // 5. Return url. + return initialize_a_url(realm, parsed_url.value()); +} + // https://url.spec.whatwg.org/#dom-url-url WebIDL::ExceptionOr> DOMURL::construct_impl(JS::Realm& realm, String const& url, Optional const& base) { diff --git a/Userland/Libraries/LibWeb/DOMURL/DOMURL.h b/Userland/Libraries/LibWeb/DOMURL/DOMURL.h index 671ed0aba36..a122b936c9b 100644 --- a/Userland/Libraries/LibWeb/DOMURL/DOMURL.h +++ b/Userland/Libraries/LibWeb/DOMURL/DOMURL.h @@ -29,6 +29,7 @@ public: static WebIDL::ExceptionOr create_object_url(JS::VM&, JS::NonnullGCPtr object); static WebIDL::ExceptionOr revoke_object_url(JS::VM&, StringView url); + static JS::GCPtr parse_for_bindings(JS::VM&, String const& url, Optional const& base = {}); static bool can_parse(JS::VM&, String const& url, Optional const& base = {}); WebIDL::ExceptionOr href() const; diff --git a/Userland/Libraries/LibWeb/DOMURL/DOMURL.idl b/Userland/Libraries/LibWeb/DOMURL/DOMURL.idl index 6ef49fe3f60..cc468050b43 100644 --- a/Userland/Libraries/LibWeb/DOMURL/DOMURL.idl +++ b/Userland/Libraries/LibWeb/DOMURL/DOMURL.idl @@ -6,6 +6,7 @@ interface URL { constructor(USVString url, optional USVString base); + [ImplementedAs=parse_for_bindings] static DOMURL? parse(USVString url, optional USVString base); static boolean canParse(USVString url, optional USVString base); stringifier attribute USVString href;