Skip to content

Commit b1e43c9

Browse files
networkExceptionlinusg
authored andcommitted
LibWeb: Implement URL.canParse(url, base)
This patch adds an implementation of the URL.canParse(url, base) static function :^)
1 parent 0e552f1 commit b1e43c9

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

Userland/Libraries/LibWeb/URL/URL.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,20 @@ void URL::visit_edges(Cell::Visitor& visitor)
9696
visitor.visit(m_query.ptr());
9797
}
9898

99+
// https://url.spec.whatwg.org/#dom-url-canparse
100+
bool URL::can_parse(JS::VM&, String const& url, Optional<String> const& base)
101+
{
102+
// 1. Let parsedURL be the result of running the API URL parser on url with base, if given.
103+
auto parsed_url = parse_api_url(url, base);
104+
105+
// 2. If parsedURL is failure, then return false.
106+
if (!parsed_url.has_value())
107+
return false;
108+
109+
// 3. Return true.
110+
return true;
111+
}
112+
99113
WebIDL::ExceptionOr<String> URL::href() const
100114
{
101115
auto& vm = realm().vm();

Userland/Libraries/LibWeb/URL/URL.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/*
22
* Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
33
* Copyright (c) 2021, the SerenityOS developers.
4+
* Copyright (c) 2023, networkException <networkexception@serenityos.org>
45
*
56
* SPDX-License-Identifier: BSD-2-Clause
67
*/
@@ -23,6 +24,8 @@ class URL : public Bindings::PlatformObject {
2324

2425
virtual ~URL() override;
2526

27+
static bool can_parse(JS::VM&, String const& url, Optional<String> const& base = {});
28+
2629
WebIDL::ExceptionOr<String> href() const;
2730
WebIDL::ExceptionOr<void> set_href(String const&);
2831

Userland/Libraries/LibWeb/URL/URL.idl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
interface URL {
66
constructor(USVString url, optional USVString base);
77

8+
static boolean canParse(USVString url, optional USVString base);
9+
810
stringifier attribute USVString href;
911
readonly attribute USVString origin;
1012
attribute USVString protocol;

0 commit comments

Comments
 (0)