From b1e43c9ea1a2dde97a2e0fdd02895337bcc55a7e Mon Sep 17 00:00:00 2001 From: networkException Date: Tue, 11 Apr 2023 14:57:34 +0200 Subject: [PATCH] LibWeb: Implement URL.canParse(url, base) This patch adds an implementation of the URL.canParse(url, base) static function :^) --- Userland/Libraries/LibWeb/URL/URL.cpp | 14 ++++++++++++++ Userland/Libraries/LibWeb/URL/URL.h | 3 +++ Userland/Libraries/LibWeb/URL/URL.idl | 2 ++ 3 files changed, 19 insertions(+) diff --git a/Userland/Libraries/LibWeb/URL/URL.cpp b/Userland/Libraries/LibWeb/URL/URL.cpp index 14ff13894d..8771b9534f 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 449d55ff86..f723308eb2 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 @@ public: 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 d149a998e9..702e551c1a 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;