Skip to content

Commit

Permalink
LibWeb: Implement URL.canParse(url, base)
Browse files Browse the repository at this point in the history
This patch adds an implementation of the URL.canParse(url, base) static
function :^)
  • Loading branch information
networkException authored and linusg committed Apr 11, 2023
1 parent 0e552f1 commit b1e43c9
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
14 changes: 14 additions & 0 deletions Userland/Libraries/LibWeb/URL/URL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> 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<String> URL::href() const
{
auto& vm = realm().vm();
Expand Down
3 changes: 3 additions & 0 deletions Userland/Libraries/LibWeb/URL/URL.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/*
* Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
* Copyright (c) 2021, the SerenityOS developers.
* Copyright (c) 2023, networkException <networkexception@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
Expand All @@ -23,6 +24,8 @@ class URL : public Bindings::PlatformObject {

virtual ~URL() override;

static bool can_parse(JS::VM&, String const& url, Optional<String> const& base = {});

WebIDL::ExceptionOr<String> href() const;
WebIDL::ExceptionOr<void> set_href(String const&);

Expand Down
2 changes: 2 additions & 0 deletions Userland/Libraries/LibWeb/URL/URL.idl
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit b1e43c9

Please sign in to comment.