diff --git a/Userland/Libraries/LibWeb/URL/URL.cpp b/Userland/Libraries/LibWeb/URL/URL.cpp index 14ff13894df64e..8771b9534faf20 100644 --- a/Userland/Libraries/LibWeb/URL/URL.cpp +++ b/Userland/Libraries/LibWeb/URL/URL.cpp @@ -96,6 +96,20 @@ void URL::visit_edges(Cell::Visitor& visitor) visitor.visit(m_query.ptr()); } +// https://url.spec.whatwg.org/#dom-url-canparse +bool URL::can_parse(JS::VM&, String const& url, Optional const& base) +{ + // 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 false. + if (!parsed_url.has_value()) + return false; + + // 3. Return true. + return true; +} + WebIDL::ExceptionOr URL::href() const { auto& vm = realm().vm(); diff --git a/Userland/Libraries/LibWeb/URL/URL.h b/Userland/Libraries/LibWeb/URL/URL.h index 449d55ff86f563..f723308eb25a9d 100644 --- a/Userland/Libraries/LibWeb/URL/URL.h +++ b/Userland/Libraries/LibWeb/URL/URL.h @@ -1,6 +1,7 @@ /* * Copyright (c) 2021, Idan Horowitz * Copyright (c) 2021, the SerenityOS developers. + * Copyright (c) 2023, networkException * * SPDX-License-Identifier: BSD-2-Clause */ @@ -23,6 +24,8 @@ class URL : public Bindings::PlatformObject { virtual ~URL() override; + static bool can_parse(JS::VM&, String const& url, Optional const& base = {}); + WebIDL::ExceptionOr href() const; WebIDL::ExceptionOr set_href(String const&); diff --git a/Userland/Libraries/LibWeb/URL/URL.idl b/Userland/Libraries/LibWeb/URL/URL.idl index d149a998e93876..702e551c1aeccc 100644 --- a/Userland/Libraries/LibWeb/URL/URL.idl +++ b/Userland/Libraries/LibWeb/URL/URL.idl @@ -5,6 +5,8 @@ interface URL { constructor(USVString url, optional USVString base); + static boolean canParse(USVString url, optional USVString base); + stringifier attribute USVString href; readonly attribute USVString origin; attribute USVString protocol;