2021-09-13 00:33:23 +03:00
|
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
|
|
|
|
|
* Copyright (c) 2021, the SerenityOS developers.
|
2023-04-11 03:14:16 +02:00
|
|
|
|
* Copyright (c) 2023, networkException <networkexception@serenityos.org>
|
2021-09-13 00:33:23 +03:00
|
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
*/
|
|
|
|
|
|
2022-10-13 18:26:35 +02:00
|
|
|
|
#include <AK/IPv4Address.h>
|
|
|
|
|
#include <AK/IPv6Address.h>
|
2021-09-14 00:21:29 +03:00
|
|
|
|
#include <AK/URLParser.h>
|
LibWeb: Remove unecessary dependence on Window from assorted classes
These classes only needed Window to get at its realm. Pass a realm
directly to construct Crypto, Encoding, HRT, IntersectionObserver,
NavigationTiming, Page, RequestIdleCallback, Selection, Streams, URL,
and XML classes.
2022-09-25 18:11:21 -06:00
|
|
|
|
#include <LibWeb/Bindings/Intrinsics.h>
|
2021-09-13 00:33:23 +03:00
|
|
|
|
#include <LibWeb/URL/URL.h>
|
|
|
|
|
|
|
|
|
|
namespace Web::URL {
|
2021-09-14 00:10:22 +03:00
|
|
|
|
|
2023-02-14 20:01:04 +01:00
|
|
|
|
WebIDL::ExceptionOr<JS::NonnullGCPtr<URL>> URL::create(JS::Realm& realm, AK::URL url, JS::NonnullGCPtr<URLSearchParams> query)
|
2022-09-04 14:04:42 +02:00
|
|
|
|
{
|
2023-02-14 20:01:04 +01:00
|
|
|
|
return MUST_OR_THROW_OOM(realm.heap().allocate<URL>(realm, realm, move(url), move(query)));
|
2022-09-04 14:04:42 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-11 03:14:16 +02:00
|
|
|
|
// https://url.spec.whatwg.org/#api-url-parser
|
|
|
|
|
static Optional<AK::URL> parse_api_url(String const& url, Optional<String> const& base)
|
2021-09-14 00:10:22 +03:00
|
|
|
|
{
|
2023-04-11 03:14:16 +02:00
|
|
|
|
// FIXME: We somewhat awkwardly have two failure states encapsulated in the return type (and convert between them in the steps),
|
|
|
|
|
// ideally we'd get rid of URL's valid flag
|
2023-03-01 20:10:01 +01:00
|
|
|
|
|
2021-09-14 00:10:22 +03:00
|
|
|
|
// 1. Let parsedBase be null.
|
|
|
|
|
Optional<AK::URL> parsed_base;
|
2023-04-11 03:14:16 +02:00
|
|
|
|
|
|
|
|
|
// 2. If base is non-null:
|
2023-03-01 20:10:01 +01:00
|
|
|
|
if (base.has_value()) {
|
2023-04-11 03:14:16 +02:00
|
|
|
|
// 1. Set parsedBase to the result of running the basic URL parser on base.
|
|
|
|
|
auto parsed_base_url = URLParser::parse(*base);
|
|
|
|
|
|
|
|
|
|
// 2. If parsedBase is failure, then return failure.
|
|
|
|
|
if (!parsed_base_url.is_valid())
|
|
|
|
|
return {};
|
|
|
|
|
|
|
|
|
|
parsed_base = parsed_base_url;
|
2021-09-14 00:10:22 +03:00
|
|
|
|
}
|
2023-04-11 03:14:16 +02:00
|
|
|
|
|
|
|
|
|
// 3. Return the result of running the basic URL parser on url with parsedBase.
|
|
|
|
|
auto parsed = URLParser::parse(url, parsed_base);
|
|
|
|
|
return parsed.is_valid() ? parsed : Optional<AK::URL> {};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// https://url.spec.whatwg.org/#dom-url-url
|
|
|
|
|
WebIDL::ExceptionOr<JS::NonnullGCPtr<URL>> URL::construct_impl(JS::Realm& realm, String const& url, Optional<String> const& base)
|
|
|
|
|
{
|
|
|
|
|
auto& vm = realm.vm();
|
|
|
|
|
|
|
|
|
|
// 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 throw a TypeError.
|
|
|
|
|
if (!parsed_url.has_value())
|
2022-10-27 23:18:16 +01:00
|
|
|
|
return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Invalid URL"sv };
|
2023-04-11 03:14:16 +02:00
|
|
|
|
|
|
|
|
|
// 3. Let query be parsedURL’s query, if that is non-null, and the empty string otherwise.
|
|
|
|
|
auto query = parsed_url->query().is_null() ? String {} : TRY_OR_THROW_OOM(vm, String::from_deprecated_string(parsed_url->query()));
|
|
|
|
|
|
|
|
|
|
// 4. Set this’s URL to parsedURL.
|
|
|
|
|
// 5. Set this’s query object to a new URLSearchParams object.
|
LibWeb: Remove unecessary dependence on Window from assorted classes
These classes only needed Window to get at its realm. Pass a realm
directly to construct Crypto, Encoding, HRT, IntersectionObserver,
NavigationTiming, Page, RequestIdleCallback, Selection, Streams, URL,
and XML classes.
2022-09-25 18:11:21 -06:00
|
|
|
|
auto query_object = MUST(URLSearchParams::construct_impl(realm, query));
|
2023-04-11 03:14:16 +02:00
|
|
|
|
|
|
|
|
|
// 6. Initialize this’s query object with query.
|
|
|
|
|
auto result_url = TRY(URL::create(realm, parsed_url.release_value(), move(query_object)));
|
|
|
|
|
|
|
|
|
|
// 7. Set this’s query object’s URL object to this.
|
2021-09-14 00:10:22 +03:00
|
|
|
|
result_url->m_query->m_url = result_url;
|
|
|
|
|
|
|
|
|
|
return result_url;
|
|
|
|
|
}
|
|
|
|
|
|
LibWeb: Remove unecessary dependence on Window from assorted classes
These classes only needed Window to get at its realm. Pass a realm
directly to construct Crypto, Encoding, HRT, IntersectionObserver,
NavigationTiming, Page, RequestIdleCallback, Selection, Streams, URL,
and XML classes.
2022-09-25 18:11:21 -06:00
|
|
|
|
URL::URL(JS::Realm& realm, AK::URL url, JS::NonnullGCPtr<URLSearchParams> query)
|
|
|
|
|
: PlatformObject(realm)
|
2022-09-04 14:04:42 +02:00
|
|
|
|
, m_url(move(url))
|
|
|
|
|
, m_query(move(query))
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
URL::~URL() = default;
|
|
|
|
|
|
2023-01-28 12:33:35 -05:00
|
|
|
|
JS::ThrowCompletionOr<void> URL::initialize(JS::Realm& realm)
|
2023-01-10 06:28:20 -05:00
|
|
|
|
{
|
2023-01-28 12:33:35 -05:00
|
|
|
|
MUST_OR_THROW_OOM(Base::initialize(realm));
|
2023-01-10 06:28:20 -05:00
|
|
|
|
set_prototype(&Bindings::ensure_web_prototype<Bindings::URLPrototype>(realm, "URL"));
|
2023-01-28 12:33:35 -05:00
|
|
|
|
|
|
|
|
|
return {};
|
2023-01-10 06:28:20 -05:00
|
|
|
|
}
|
|
|
|
|
|
2022-09-04 14:04:42 +02:00
|
|
|
|
void URL::visit_edges(Cell::Visitor& visitor)
|
|
|
|
|
{
|
|
|
|
|
Base::visit_edges(visitor);
|
|
|
|
|
visitor.visit(m_query.ptr());
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-11 14:57:34 +02:00
|
|
|
|
// 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;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-12 23:20:27 +02:00
|
|
|
|
// https://url.spec.whatwg.org/#dom-url-href
|
2023-03-01 20:10:01 +01:00
|
|
|
|
WebIDL::ExceptionOr<String> URL::href() const
|
2021-09-14 00:13:32 +03:00
|
|
|
|
{
|
2023-03-01 20:10:01 +01:00
|
|
|
|
auto& vm = realm().vm();
|
|
|
|
|
|
2023-04-12 23:20:27 +02:00
|
|
|
|
// The href getter steps and the toJSON() method steps are to return the serialization of this’s URL.
|
2023-03-01 20:10:01 +01:00
|
|
|
|
return TRY_OR_THROW_OOM(vm, String::from_deprecated_string(m_url.serialize()));
|
2021-09-14 00:13:32 +03:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-12 23:20:27 +02:00
|
|
|
|
// https://url.spec.whatwg.org/#dom-url-tojson
|
2023-03-01 20:10:01 +01:00
|
|
|
|
WebIDL::ExceptionOr<String> URL::to_json() const
|
2021-09-14 00:13:32 +03:00
|
|
|
|
{
|
2023-03-01 20:10:01 +01:00
|
|
|
|
auto& vm = realm().vm();
|
|
|
|
|
|
2023-04-12 23:20:27 +02:00
|
|
|
|
// The href getter steps and the toJSON() method steps are to return the serialization of this’s URL.
|
2023-03-01 20:10:01 +01:00
|
|
|
|
return TRY_OR_THROW_OOM(vm, String::from_deprecated_string(m_url.serialize()));
|
2021-09-14 00:13:32 +03:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-12 23:20:27 +02:00
|
|
|
|
// https://url.spec.whatwg.org/#ref-for-dom-url-href②
|
2023-03-01 20:10:01 +01:00
|
|
|
|
WebIDL::ExceptionOr<void> URL::set_href(String const& href)
|
2021-09-14 00:13:32 +03:00
|
|
|
|
{
|
2023-03-01 20:10:01 +01:00
|
|
|
|
auto& vm = realm().vm();
|
|
|
|
|
|
2021-09-14 00:13:32 +03:00
|
|
|
|
// 1. Let parsedURL be the result of running the basic URL parser on the given value.
|
|
|
|
|
AK::URL parsed_url = href;
|
2023-04-12 23:20:27 +02:00
|
|
|
|
|
2021-09-14 00:13:32 +03:00
|
|
|
|
// 2. If parsedURL is failure, then throw a TypeError.
|
|
|
|
|
if (!parsed_url.is_valid())
|
2022-10-27 23:18:16 +01:00
|
|
|
|
return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Invalid URL"sv };
|
2023-04-12 23:20:27 +02:00
|
|
|
|
|
2021-09-14 00:13:32 +03:00
|
|
|
|
// 3. Set this’s URL to parsedURL.
|
|
|
|
|
m_url = move(parsed_url);
|
2023-04-12 23:20:27 +02:00
|
|
|
|
|
2021-09-14 00:13:32 +03:00
|
|
|
|
// 4. Empty this’s query object’s list.
|
|
|
|
|
m_query->m_list.clear();
|
2023-04-12 23:20:27 +02:00
|
|
|
|
|
2021-09-14 00:13:32 +03:00
|
|
|
|
// 5. Let query be this’s URL’s query.
|
|
|
|
|
auto& query = m_url.query();
|
2023-04-12 23:20:27 +02:00
|
|
|
|
|
2021-09-14 00:13:32 +03:00
|
|
|
|
// 6. If query is non-null, then set this’s query object’s list to the result of parsing query.
|
|
|
|
|
if (!query.is_null())
|
2023-03-01 20:10:01 +01:00
|
|
|
|
m_query->m_list = TRY_OR_THROW_OOM(vm, url_decode(query));
|
2021-09-14 00:13:32 +03:00
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-12 23:20:27 +02:00
|
|
|
|
// https://url.spec.whatwg.org/#dom-url-origin
|
2023-03-01 20:10:01 +01:00
|
|
|
|
WebIDL::ExceptionOr<String> URL::origin() const
|
2021-09-14 00:18:25 +03:00
|
|
|
|
{
|
2023-03-01 20:10:01 +01:00
|
|
|
|
auto& vm = realm().vm();
|
|
|
|
|
|
2023-04-12 23:20:27 +02:00
|
|
|
|
// The origin getter steps are to return the serialization of this’s URL’s origin. [HTML]
|
2023-03-01 20:10:01 +01:00
|
|
|
|
return TRY_OR_THROW_OOM(vm, String::from_deprecated_string(m_url.serialize_origin()));
|
2021-09-14 00:18:25 +03:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-12 23:20:27 +02:00
|
|
|
|
// https://url.spec.whatwg.org/#dom-url-protocol
|
2023-03-01 20:10:01 +01:00
|
|
|
|
WebIDL::ExceptionOr<String> URL::protocol() const
|
2021-09-14 00:21:51 +03:00
|
|
|
|
{
|
2023-03-01 20:10:01 +01:00
|
|
|
|
auto& vm = realm().vm();
|
|
|
|
|
|
2023-04-12 23:20:27 +02:00
|
|
|
|
// The protocol getter steps are to return this’s URL’s scheme, followed by U+003A (:).
|
2023-03-01 20:10:01 +01:00
|
|
|
|
return TRY_OR_THROW_OOM(vm, String::formatted("{}:", m_url.scheme()));
|
2021-09-14 00:21:51 +03:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-12 23:20:27 +02:00
|
|
|
|
// https://url.spec.whatwg.org/#ref-for-dom-url-protocol%E2%91%A0
|
2023-03-01 20:10:01 +01:00
|
|
|
|
WebIDL::ExceptionOr<void> URL::set_protocol(String const& protocol)
|
2021-09-14 00:21:51 +03:00
|
|
|
|
{
|
2023-03-01 20:10:01 +01:00
|
|
|
|
auto& vm = realm().vm();
|
|
|
|
|
|
2023-04-12 23:20:27 +02:00
|
|
|
|
// The protocol setter steps are to basic URL parse the given value, followed by U+003A (:), with this’s URL as
|
|
|
|
|
// url and scheme start state as state override.
|
2023-04-11 14:53:40 +02:00
|
|
|
|
auto result_url = URLParser::parse(TRY_OR_THROW_OOM(vm, String::formatted("{}:", protocol)), {}, m_url, URLParser::State::SchemeStart);
|
2021-09-14 00:21:51 +03:00
|
|
|
|
if (result_url.is_valid())
|
|
|
|
|
m_url = move(result_url);
|
2023-03-01 20:10:01 +01:00
|
|
|
|
return {};
|
2021-09-14 00:21:51 +03:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-12 23:20:27 +02:00
|
|
|
|
// https://url.spec.whatwg.org/#dom-url-username
|
2023-03-01 20:10:01 +01:00
|
|
|
|
WebIDL::ExceptionOr<String> URL::username() const
|
2021-09-14 00:18:25 +03:00
|
|
|
|
{
|
2023-03-01 20:10:01 +01:00
|
|
|
|
auto& vm = realm().vm();
|
|
|
|
|
|
2023-04-12 23:20:27 +02:00
|
|
|
|
// The username getter steps are to return this’s URL’s username.
|
2023-03-01 20:10:01 +01:00
|
|
|
|
return TRY_OR_THROW_OOM(vm, String::from_deprecated_string(m_url.username()));
|
2021-09-14 00:18:25 +03:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-12 23:20:27 +02:00
|
|
|
|
// https://url.spec.whatwg.org/#ref-for-dom-url-username%E2%91%A0
|
2023-03-01 20:10:01 +01:00
|
|
|
|
void URL::set_username(String const& username)
|
2021-09-14 00:18:25 +03:00
|
|
|
|
{
|
|
|
|
|
// 1. If this’s URL cannot have a username/password/port, then return.
|
|
|
|
|
if (m_url.cannot_have_a_username_or_password_or_port())
|
|
|
|
|
return;
|
2023-04-12 23:20:27 +02:00
|
|
|
|
|
2021-09-14 00:18:25 +03:00
|
|
|
|
// 2. Set the username given this’s URL and the given value.
|
|
|
|
|
m_url.set_username(AK::URL::percent_encode(username, AK::URL::PercentEncodeSet::Userinfo));
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-12 23:20:27 +02:00
|
|
|
|
// https://url.spec.whatwg.org/#dom-url-password
|
2023-03-01 20:10:01 +01:00
|
|
|
|
WebIDL::ExceptionOr<String> URL::password() const
|
2021-09-14 00:18:25 +03:00
|
|
|
|
{
|
2023-03-01 20:10:01 +01:00
|
|
|
|
auto& vm = realm().vm();
|
|
|
|
|
|
2023-04-12 23:20:27 +02:00
|
|
|
|
// The password getter steps are to return this’s URL’s password.
|
2023-03-01 20:10:01 +01:00
|
|
|
|
return TRY_OR_THROW_OOM(vm, String::from_deprecated_string(m_url.password()));
|
2021-09-14 00:18:25 +03:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-12 23:20:27 +02:00
|
|
|
|
// https://url.spec.whatwg.org/#ref-for-dom-url-password%E2%91%A0
|
2023-03-01 20:10:01 +01:00
|
|
|
|
void URL::set_password(String const& password)
|
2021-09-14 00:18:25 +03:00
|
|
|
|
{
|
|
|
|
|
// 1. If this’s URL cannot have a username/password/port, then return.
|
|
|
|
|
if (m_url.cannot_have_a_username_or_password_or_port())
|
|
|
|
|
return;
|
2023-04-12 23:20:27 +02:00
|
|
|
|
|
2021-09-14 00:18:25 +03:00
|
|
|
|
// 2. Set the password given this’s URL and the given value.
|
|
|
|
|
m_url.set_password(AK::URL::percent_encode(password, AK::URL::PercentEncodeSet::Userinfo));
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-12 23:20:27 +02:00
|
|
|
|
// https://url.spec.whatwg.org/#dom-url-host
|
2023-03-01 20:10:01 +01:00
|
|
|
|
WebIDL::ExceptionOr<String> URL::host() const
|
2021-09-14 00:21:29 +03:00
|
|
|
|
{
|
2023-03-01 20:10:01 +01:00
|
|
|
|
auto& vm = realm().vm();
|
|
|
|
|
|
2021-09-14 00:21:29 +03:00
|
|
|
|
// 1. Let url be this’s URL.
|
|
|
|
|
auto& url = m_url;
|
2023-04-12 23:20:27 +02:00
|
|
|
|
|
2021-09-14 00:21:29 +03:00
|
|
|
|
// 2. If url’s host is null, then return the empty string.
|
|
|
|
|
if (url.host().is_null())
|
2023-03-01 20:10:01 +01:00
|
|
|
|
return String {};
|
2023-04-12 23:20:27 +02:00
|
|
|
|
|
2021-09-14 00:21:29 +03:00
|
|
|
|
// 3. If url’s port is null, return url’s host, serialized.
|
|
|
|
|
if (!url.port().has_value())
|
2023-03-01 20:10:01 +01:00
|
|
|
|
return TRY_OR_THROW_OOM(vm, String::from_deprecated_string(url.host()));
|
2023-04-12 23:20:27 +02:00
|
|
|
|
|
2021-09-14 00:21:29 +03:00
|
|
|
|
// 4. Return url’s host, serialized, followed by U+003A (:) and url’s port, serialized.
|
2023-03-01 20:10:01 +01:00
|
|
|
|
return TRY_OR_THROW_OOM(vm, String::formatted("{}:{}", url.host(), *url.port()));
|
2021-09-14 00:21:29 +03:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-12 23:20:27 +02:00
|
|
|
|
// https://url.spec.whatwg.org/#dom-url-hostref-for-dom-url-host%E2%91%A0
|
2023-03-01 20:10:01 +01:00
|
|
|
|
void URL::set_host(String const& host)
|
2021-09-14 00:21:29 +03:00
|
|
|
|
{
|
|
|
|
|
// 1. If this’s URL’s cannot-be-a-base-URL is true, then return.
|
|
|
|
|
if (m_url.cannot_be_a_base_url())
|
|
|
|
|
return;
|
2023-04-12 23:20:27 +02:00
|
|
|
|
|
2021-09-14 00:21:29 +03:00
|
|
|
|
// 2. Basic URL parse the given value with this’s URL as url and host state as state override.
|
2023-04-11 14:53:40 +02:00
|
|
|
|
auto result_url = URLParser::parse(host, {}, m_url, URLParser::State::Host);
|
2021-09-14 00:21:29 +03:00
|
|
|
|
if (result_url.is_valid())
|
|
|
|
|
m_url = move(result_url);
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-12 23:20:27 +02:00
|
|
|
|
// https://url.spec.whatwg.org/#dom-url-hostname
|
2023-03-01 20:10:01 +01:00
|
|
|
|
WebIDL::ExceptionOr<String> URL::hostname() const
|
2021-09-14 00:21:29 +03:00
|
|
|
|
{
|
2023-03-01 20:10:01 +01:00
|
|
|
|
auto& vm = realm().vm();
|
|
|
|
|
|
2021-09-14 00:21:29 +03:00
|
|
|
|
// 1. If this’s URL’s host is null, then return the empty string.
|
|
|
|
|
if (m_url.host().is_null())
|
2023-03-01 20:10:01 +01:00
|
|
|
|
return String {};
|
2023-04-12 23:20:27 +02:00
|
|
|
|
|
2021-09-14 00:21:29 +03:00
|
|
|
|
// 2. Return this’s URL’s host, serialized.
|
2023-03-01 20:10:01 +01:00
|
|
|
|
return TRY_OR_THROW_OOM(vm, String::from_deprecated_string(m_url.host()));
|
2021-09-14 00:21:29 +03:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-12 23:20:27 +02:00
|
|
|
|
// https://url.spec.whatwg.org/#ref-for-dom-url-hostname①
|
2023-03-01 20:10:01 +01:00
|
|
|
|
void URL::set_hostname(String const& hostname)
|
2021-09-14 00:21:29 +03:00
|
|
|
|
{
|
|
|
|
|
// 1. If this’s URL’s cannot-be-a-base-URL is true, then return.
|
|
|
|
|
if (m_url.cannot_be_a_base_url())
|
|
|
|
|
return;
|
2023-04-12 23:20:27 +02:00
|
|
|
|
|
2021-09-14 00:21:29 +03:00
|
|
|
|
// 2. Basic URL parse the given value with this’s URL as url and hostname state as state override.
|
2023-04-11 14:53:40 +02:00
|
|
|
|
auto result_url = URLParser::parse(hostname, {}, m_url, URLParser::State::Hostname);
|
2021-09-14 00:21:29 +03:00
|
|
|
|
if (result_url.is_valid())
|
|
|
|
|
m_url = move(result_url);
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-12 23:20:27 +02:00
|
|
|
|
// https://url.spec.whatwg.org/#ref-for-dom-url-hostname①
|
2023-03-01 20:10:01 +01:00
|
|
|
|
WebIDL::ExceptionOr<String> URL::port() const
|
2021-09-14 00:21:29 +03:00
|
|
|
|
{
|
2023-03-01 20:10:01 +01:00
|
|
|
|
auto& vm = realm().vm();
|
|
|
|
|
|
2021-09-14 00:21:29 +03:00
|
|
|
|
// 1. If this’s URL’s port is null, then return the empty string.
|
|
|
|
|
if (!m_url.port().has_value())
|
2023-03-01 20:10:01 +01:00
|
|
|
|
return String {};
|
2021-09-14 00:21:29 +03:00
|
|
|
|
|
|
|
|
|
// 2. Return this’s URL’s port, serialized.
|
2023-03-01 20:10:01 +01:00
|
|
|
|
return TRY_OR_THROW_OOM(vm, String::formatted("{}", *m_url.port()));
|
2021-09-14 00:21:29 +03:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-12 23:20:27 +02:00
|
|
|
|
// https://url.spec.whatwg.org/#ref-for-dom-url-port%E2%91%A0
|
2023-03-01 20:10:01 +01:00
|
|
|
|
void URL::set_port(String const& port)
|
2021-09-14 00:21:29 +03:00
|
|
|
|
{
|
|
|
|
|
// 1. If this’s URL cannot have a username/password/port, then return.
|
|
|
|
|
if (m_url.cannot_have_a_username_or_password_or_port())
|
|
|
|
|
return;
|
2021-10-03 20:18:34 +02:00
|
|
|
|
|
2021-09-14 00:21:29 +03:00
|
|
|
|
// 2. If the given value is the empty string, then set this’s URL’s port to null.
|
2021-10-03 20:18:34 +02:00
|
|
|
|
if (port.is_empty()) {
|
2021-09-14 00:21:29 +03:00
|
|
|
|
m_url.set_port({});
|
2021-10-03 20:18:34 +02:00
|
|
|
|
}
|
2021-09-14 00:21:29 +03:00
|
|
|
|
// 3. Otherwise, basic URL parse the given value with this’s URL as url and port state as state override.
|
2023-04-12 23:20:27 +02:00
|
|
|
|
else {
|
|
|
|
|
auto result_url = URLParser::parse(port, {}, m_url, URLParser::State::Port);
|
|
|
|
|
if (result_url.is_valid())
|
|
|
|
|
m_url = move(result_url);
|
|
|
|
|
}
|
2021-09-14 00:21:29 +03:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-12 23:20:27 +02:00
|
|
|
|
// https://url.spec.whatwg.org/#dom-url-pathname
|
2023-03-01 20:10:01 +01:00
|
|
|
|
WebIDL::ExceptionOr<String> URL::pathname() const
|
2021-09-14 00:21:51 +03:00
|
|
|
|
{
|
2023-03-01 20:10:01 +01:00
|
|
|
|
auto& vm = realm().vm();
|
|
|
|
|
|
2023-04-12 23:20:27 +02:00
|
|
|
|
// The pathname getter steps are to return the result of URL path serializing this’s URL.
|
2023-03-01 20:10:01 +01:00
|
|
|
|
return TRY_OR_THROW_OOM(vm, String::from_deprecated_string(m_url.path()));
|
2021-09-14 00:21:51 +03:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-12 23:20:27 +02:00
|
|
|
|
// https://url.spec.whatwg.org/#ref-for-dom-url-pathname%E2%91%A0
|
2023-03-01 20:10:01 +01:00
|
|
|
|
void URL::set_pathname(String const& pathname)
|
2021-09-14 00:21:51 +03:00
|
|
|
|
{
|
2023-04-12 23:20:27 +02:00
|
|
|
|
// FIXME: These steps no longer match the speci.
|
2021-09-14 00:21:51 +03:00
|
|
|
|
// 1. If this’s URL’s cannot-be-a-base-URL is true, then return.
|
|
|
|
|
if (m_url.cannot_be_a_base_url())
|
|
|
|
|
return;
|
2023-04-12 23:20:27 +02:00
|
|
|
|
|
2021-09-14 00:21:51 +03:00
|
|
|
|
// 2. Empty this’s URL’s path.
|
|
|
|
|
auto url = m_url; // We copy the URL here to follow other browser's behaviour of reverting the path change if the parse failed.
|
|
|
|
|
url.set_paths({});
|
2023-04-12 23:20:27 +02:00
|
|
|
|
|
2021-09-14 00:21:51 +03:00
|
|
|
|
// 3. Basic URL parse the given value with this’s URL as url and path start state as state override.
|
2023-04-11 14:53:40 +02:00
|
|
|
|
auto result_url = URLParser::parse(pathname, {}, move(url), URLParser::State::PathStart);
|
2021-09-14 00:21:51 +03:00
|
|
|
|
if (result_url.is_valid())
|
|
|
|
|
m_url = move(result_url);
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-12 23:20:27 +02:00
|
|
|
|
// https://url.spec.whatwg.org/#dom-url-search
|
2023-03-01 20:10:01 +01:00
|
|
|
|
WebIDL::ExceptionOr<String> URL::search() const
|
2021-09-14 00:21:51 +03:00
|
|
|
|
{
|
2023-03-01 20:10:01 +01:00
|
|
|
|
auto& vm = realm().vm();
|
|
|
|
|
|
2021-09-14 00:21:51 +03:00
|
|
|
|
// 1. If this’s URL’s query is either null or the empty string, then return the empty string.
|
|
|
|
|
if (m_url.query().is_null() || m_url.query().is_empty())
|
2023-03-01 20:10:01 +01:00
|
|
|
|
return String {};
|
2023-04-12 23:20:27 +02:00
|
|
|
|
|
2021-09-14 00:21:51 +03:00
|
|
|
|
// 2. Return U+003F (?), followed by this’s URL’s query.
|
2023-03-01 20:10:01 +01:00
|
|
|
|
return TRY_OR_THROW_OOM(vm, String::formatted("?{}", m_url.query()));
|
2021-09-14 00:21:51 +03:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-12 23:20:27 +02:00
|
|
|
|
// https://url.spec.whatwg.org/#ref-for-dom-url-search%E2%91%A0
|
2023-03-01 20:10:01 +01:00
|
|
|
|
WebIDL::ExceptionOr<void> URL::set_search(String const& search)
|
2021-09-14 00:21:51 +03:00
|
|
|
|
{
|
2023-03-01 20:10:01 +01:00
|
|
|
|
auto& vm = realm().vm();
|
|
|
|
|
|
2021-09-14 00:21:51 +03:00
|
|
|
|
// 1. Let url be this’s URL.
|
|
|
|
|
auto& url = m_url;
|
2023-04-12 23:20:27 +02:00
|
|
|
|
|
|
|
|
|
// 2. If the given value is the empty string:
|
2021-09-14 00:21:51 +03:00
|
|
|
|
if (search.is_empty()) {
|
2023-04-12 23:20:27 +02:00
|
|
|
|
// 1. Set url’s query to null.
|
2021-09-14 00:21:51 +03:00
|
|
|
|
url.set_query({});
|
2023-04-12 23:20:27 +02:00
|
|
|
|
|
|
|
|
|
// 2. Empty this’s query object’s list.
|
2021-09-14 00:21:51 +03:00
|
|
|
|
m_query->m_list.clear();
|
2023-04-12 23:20:27 +02:00
|
|
|
|
|
|
|
|
|
// FIXME: 3. Potentially strip trailing spaces from an opaque path with this.
|
|
|
|
|
|
|
|
|
|
// 4. Return.
|
2023-03-01 20:10:01 +01:00
|
|
|
|
return {};
|
2021-09-14 00:21:51 +03:00
|
|
|
|
}
|
2023-04-12 23:20:27 +02:00
|
|
|
|
|
|
|
|
|
// 3. Let input be the given value with a single leading U+003F (?) removed, if any.
|
2023-03-01 20:10:01 +01:00
|
|
|
|
auto search_as_string_view = search.bytes_as_string_view();
|
|
|
|
|
auto input = search_as_string_view.substring_view(search_as_string_view.starts_with('?'));
|
2023-04-12 23:20:27 +02:00
|
|
|
|
|
|
|
|
|
// 4. Set url’s query to the empty string.
|
2021-09-14 00:21:51 +03:00
|
|
|
|
auto url_copy = url; // We copy the URL here to follow other browser's behaviour of reverting the search change if the parse failed.
|
2022-12-04 18:02:33 +00:00
|
|
|
|
url_copy.set_query(DeprecatedString::empty());
|
2023-04-12 23:20:27 +02:00
|
|
|
|
|
|
|
|
|
// 5. Basic URL parse input with url as url and query state as state override.
|
2023-04-11 14:53:40 +02:00
|
|
|
|
auto result_url = URLParser::parse(input, {}, move(url_copy), URLParser::State::Query);
|
2021-09-14 00:21:51 +03:00
|
|
|
|
if (result_url.is_valid()) {
|
|
|
|
|
m_url = move(result_url);
|
2023-04-12 23:20:27 +02:00
|
|
|
|
|
|
|
|
|
// 6. Set this’s query object’s list to the result of parsing input.
|
2023-03-01 20:10:01 +01:00
|
|
|
|
m_query->m_list = TRY_OR_THROW_OOM(vm, url_decode(input));
|
2021-09-14 00:21:51 +03:00
|
|
|
|
}
|
2023-03-01 20:10:01 +01:00
|
|
|
|
|
|
|
|
|
return {};
|
2021-09-14 00:21:51 +03:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-12 23:20:27 +02:00
|
|
|
|
// https://url.spec.whatwg.org/#dom-url-searchparams
|
2023-04-12 23:20:27 +02:00
|
|
|
|
JS::NonnullGCPtr<URLSearchParams const> URL::search_params() const
|
2021-09-14 00:15:41 +03:00
|
|
|
|
{
|
2023-04-12 23:20:27 +02:00
|
|
|
|
// The searchParams getter steps are to return this’s query object.
|
2021-09-14 00:15:41 +03:00
|
|
|
|
return m_query;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-12 23:20:27 +02:00
|
|
|
|
// https://url.spec.whatwg.org/#dom-url-hash
|
2023-03-01 20:10:01 +01:00
|
|
|
|
WebIDL::ExceptionOr<String> URL::hash() const
|
2021-09-14 00:21:51 +03:00
|
|
|
|
{
|
2023-03-01 20:10:01 +01:00
|
|
|
|
auto& vm = realm().vm();
|
|
|
|
|
|
2021-09-14 00:21:51 +03:00
|
|
|
|
// 1. If this’s URL’s fragment is either null or the empty string, then return the empty string.
|
|
|
|
|
if (m_url.fragment().is_null() || m_url.fragment().is_empty())
|
2023-03-01 20:10:01 +01:00
|
|
|
|
return String {};
|
2023-04-12 23:20:27 +02:00
|
|
|
|
|
2021-09-14 00:21:51 +03:00
|
|
|
|
// 2. Return U+0023 (#), followed by this’s URL’s fragment.
|
2023-03-01 20:10:01 +01:00
|
|
|
|
return TRY_OR_THROW_OOM(vm, String::formatted("#{}", m_url.fragment()));
|
2021-09-14 00:21:51 +03:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-12 23:20:27 +02:00
|
|
|
|
// https://url.spec.whatwg.org/#ref-for-dom-url-hash%E2%91%A0
|
2023-03-01 20:10:01 +01:00
|
|
|
|
void URL::set_hash(String const& hash)
|
2021-09-14 00:21:51 +03:00
|
|
|
|
{
|
2023-04-12 23:20:27 +02:00
|
|
|
|
// 1. If the given value is the empty string:
|
2021-09-14 00:21:51 +03:00
|
|
|
|
if (hash.is_empty()) {
|
2023-04-12 23:20:27 +02:00
|
|
|
|
// 1. Set this’s URL’s fragment to null.
|
2021-09-14 00:21:51 +03:00
|
|
|
|
m_url.set_fragment({});
|
2023-04-12 23:20:27 +02:00
|
|
|
|
|
|
|
|
|
// FIXME: 2. Potentially strip trailing spaces from an opaque path with this.
|
|
|
|
|
|
|
|
|
|
// 3. Return.
|
2021-09-14 00:21:51 +03:00
|
|
|
|
return;
|
|
|
|
|
}
|
2023-04-12 23:20:27 +02:00
|
|
|
|
|
2021-09-14 00:21:51 +03:00
|
|
|
|
// 2. Let input be the given value with a single leading U+0023 (#) removed, if any.
|
2023-03-01 20:10:01 +01:00
|
|
|
|
auto hash_as_string_view = hash.bytes_as_string_view();
|
|
|
|
|
auto input = hash_as_string_view.substring_view(hash_as_string_view.starts_with('#'));
|
2023-04-12 23:20:27 +02:00
|
|
|
|
|
2021-09-14 00:21:51 +03:00
|
|
|
|
// 3. Set this’s URL’s fragment to the empty string.
|
|
|
|
|
auto url = m_url; // We copy the URL here to follow other browser's behaviour of reverting the hash change if the parse failed.
|
2022-12-04 18:02:33 +00:00
|
|
|
|
url.set_fragment(DeprecatedString::empty());
|
2023-04-12 23:20:27 +02:00
|
|
|
|
|
2021-09-14 00:21:51 +03:00
|
|
|
|
// 4. Basic URL parse input with this’s URL as url and fragment state as state override.
|
2023-04-11 14:53:40 +02:00
|
|
|
|
auto result_url = URLParser::parse(input, {}, move(url), URLParser::State::Fragment);
|
2021-09-14 00:21:51 +03:00
|
|
|
|
if (result_url.is_valid())
|
|
|
|
|
m_url = move(result_url);
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-13 18:25:00 +02:00
|
|
|
|
// https://url.spec.whatwg.org/#concept-url-origin
|
|
|
|
|
HTML::Origin url_origin(AK::URL const& url)
|
|
|
|
|
{
|
|
|
|
|
// FIXME: We should probably have an extended version of AK::URL for LibWeb instead of standalone functions like this.
|
|
|
|
|
|
|
|
|
|
// The origin of a URL url is the origin returned by running these steps, switching on url’s scheme:
|
2023-04-12 23:20:27 +02:00
|
|
|
|
// -> "blob"
|
2022-10-13 18:25:00 +02:00
|
|
|
|
if (url.scheme() == "blob"sv) {
|
|
|
|
|
// FIXME: Support 'blob://' URLs
|
|
|
|
|
return HTML::Origin {};
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-12 23:20:27 +02:00
|
|
|
|
// -> "ftp"
|
|
|
|
|
// -> "http"
|
|
|
|
|
// -> "https"
|
|
|
|
|
// -> "ws"
|
|
|
|
|
// -> "wss"
|
2022-10-13 18:25:00 +02:00
|
|
|
|
if (url.scheme().is_one_of("ftp"sv, "http"sv, "https"sv, "ws"sv, "wss"sv)) {
|
|
|
|
|
// Return the tuple origin (url’s scheme, url’s host, url’s port, null).
|
|
|
|
|
return HTML::Origin(url.scheme(), url.host(), url.port().value_or(0));
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-12 23:20:27 +02:00
|
|
|
|
// -> "file"
|
2022-10-13 18:25:00 +02:00
|
|
|
|
if (url.scheme() == "file"sv) {
|
|
|
|
|
// Unfortunate as it is, this is left as an exercise to the reader. When in doubt, return a new opaque origin.
|
|
|
|
|
// Note: We must return an origin with the `file://' protocol for `file://' iframes to work from `file://' pages.
|
2022-12-04 18:02:33 +00:00
|
|
|
|
return HTML::Origin(url.scheme(), DeprecatedString(), 0);
|
2022-10-13 18:25:00 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-12 23:20:27 +02:00
|
|
|
|
// -> Otherwise
|
2022-10-13 18:25:00 +02:00
|
|
|
|
// Return a new opaque origin.
|
|
|
|
|
return HTML::Origin {};
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-13 18:26:35 +02:00
|
|
|
|
// https://url.spec.whatwg.org/#concept-domain
|
|
|
|
|
bool host_is_domain(StringView host)
|
|
|
|
|
{
|
|
|
|
|
// A domain is a non-empty ASCII string that identifies a realm within a network.
|
|
|
|
|
return !host.is_empty()
|
|
|
|
|
&& !IPv4Address::from_string(host).has_value()
|
|
|
|
|
&& !IPv6Address::from_string(host).has_value();
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-13 00:33:23 +03:00
|
|
|
|
}
|