summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIdan Horowitz <idan.horowitz@gmail.com>2021-09-14 00:21:51 +0300
committerAndreas Kling <kling@serenityos.org>2021-09-14 00:14:45 +0200
commited5128d759289249b5502f395566376d326859ed (patch)
treea58b6c26094bff8400cb6b4f86dfb07831891b96
parent7f9818bcbc5390adb2f4df8a44e4f5ae806ca75b (diff)
downloadserenity-ed5128d759289249b5502f395566376d326859ed.zip
LibWeb: Add the URL::{protocol, pathname, search, hash} attributes
-rw-r--r--Userland/Libraries/LibWeb/URL/URL.cpp96
-rw-r--r--Userland/Libraries/LibWeb/URL/URL.h12
-rw-r--r--Userland/Libraries/LibWeb/URL/URL.idl8
3 files changed, 112 insertions, 4 deletions
diff --git a/Userland/Libraries/LibWeb/URL/URL.cpp b/Userland/Libraries/LibWeb/URL/URL.cpp
index 40e6bdbec4..814d1a78f4 100644
--- a/Userland/Libraries/LibWeb/URL/URL.cpp
+++ b/Userland/Libraries/LibWeb/URL/URL.cpp
@@ -82,6 +82,20 @@ String URL::origin() const
return m_url.serialize_origin();
}
+String URL::protocol() const
+{
+ // return this’s URL’s scheme, followed by U+003A (:).
+ return String::formatted("{}:", m_url.scheme());
+}
+
+void URL::set_protocol(String const& protocol)
+{
+ // basic URL parse the given value, followed by U+003A (:), with this’s URL as url and scheme start state as state override.
+ auto result_url = URLParser::parse(String::formatted("{}:", protocol), nullptr, m_url, URLParser::State::SchemeStart);
+ if (result_url.is_valid())
+ m_url = move(result_url);
+}
+
String URL::username() const
{
// return this’s URL’s username.
@@ -181,9 +195,91 @@ void URL::set_port(String const& port)
m_url = move(result_url);
}
+String URL::pathname() const
+{
+ // 1. If this’s URL’s cannot-be-a-base-URL is true, then return this’s URL’s path[0].
+ // 2. If this’s URL’s path is empty, then return the empty string.
+ // 3. Return U+002F (/), followed by the strings in this’s URL’s path (including empty strings), if any, separated from each other by U+002F (/).
+ return m_url.path();
+}
+
+void URL::set_pathname(String const& pathname)
+{
+ // 1. If this’s URL’s cannot-be-a-base-URL is true, then return.
+ if (m_url.cannot_be_a_base_url())
+ return;
+ // 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({});
+ // 3. Basic URL parse the given value with this’s URL as url and path start state as state override.
+ auto result_url = URLParser::parse(pathname, nullptr, move(url), URLParser::State::PathStart);
+ if (result_url.is_valid())
+ m_url = move(result_url);
+}
+
+String URL::search() const
+{
+ // 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())
+ return String::empty();
+ // 2. Return U+003F (?), followed by this’s URL’s query.
+ return String::formatted("?{}", m_url.query());
+}
+
+void URL::set_search(String const& search)
+{
+ // 1. Let url be this’s URL.
+ auto& url = m_url;
+ // If the given value is the empty string, set url’s query to null, empty this’s query object’s list, and then return.
+ if (search.is_empty()) {
+ url.set_query({});
+ m_query->m_list.clear();
+ return;
+ }
+ // 2. Let input be the given value with a single leading U+003F (?) removed, if any.
+ auto input = search.substring_view(search.starts_with('?'));
+ // 3. Set url’s query to the empty string.
+ auto url_copy = url; // We copy the URL here to follow other browser's behaviour of reverting the search change if the parse failed.
+ url_copy.set_query(String::empty());
+ // 4. Basic URL parse input with url as url and query state as state override.
+ auto result_url = URLParser::parse(input, nullptr, move(url_copy), URLParser::State::Query);
+ if (result_url.is_valid()) {
+ m_url = move(result_url);
+ // 5. Set this’s query object’s list to the result of parsing input.
+ m_query->m_list = url_decode(input);
+ }
+}
+
URLSearchParams const* URL::search_params() const
{
return m_query;
}
+String URL::hash() const
+{
+ // 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())
+ return String::empty();
+ // 2. Return U+0023 (#), followed by this’s URL’s fragment.
+ return String::formatted("#{}", m_url.fragment());
+}
+
+void URL::set_hash(String const& hash)
+{
+ // 1. If the given value is the empty string, then set this’s URL’s fragment to null and return.
+ if (hash.is_empty()) {
+ m_url.set_fragment({});
+ return;
+ }
+ // 2. Let input be the given value with a single leading U+0023 (#) removed, if any.
+ auto input = hash.substring_view(hash.starts_with('#'));
+ // 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.
+ url.set_fragment(String::empty());
+ // 4. Basic URL parse input with this’s URL as url and fragment state as state override.
+ auto result_url = URLParser::parse(input, nullptr, move(url), URLParser::State::Fragment);
+ if (result_url.is_valid())
+ m_url = move(result_url);
+}
+
}
diff --git a/Userland/Libraries/LibWeb/URL/URL.h b/Userland/Libraries/LibWeb/URL/URL.h
index cb6df8ae33..0f17dc1c62 100644
--- a/Userland/Libraries/LibWeb/URL/URL.h
+++ b/Userland/Libraries/LibWeb/URL/URL.h
@@ -33,6 +33,9 @@ public:
String origin() const;
+ String protocol() const;
+ void set_protocol(String const&);
+
String username() const;
void set_username(String const&);
@@ -48,8 +51,17 @@ public:
String port() const;
void set_port(String const&);
+ String pathname() const;
+ void set_pathname(String const&);
+
+ String search() const;
+ void set_search(String const&);
+
URLSearchParams const* search_params() const;
+ String hash() const;
+ void set_hash(String const&);
+
String to_json() const;
void set_query(Badge<URLSearchParams>, String query) { m_url.set_query(move(query)); }
diff --git a/Userland/Libraries/LibWeb/URL/URL.idl b/Userland/Libraries/LibWeb/URL/URL.idl
index fc668ebd3d..1ad7f2ba18 100644
--- a/Userland/Libraries/LibWeb/URL/URL.idl
+++ b/Userland/Libraries/LibWeb/URL/URL.idl
@@ -3,16 +3,16 @@ interface URL {
stringifier attribute USVString href;
readonly attribute USVString origin;
- // TODO: attribute USVString protocol;
+ attribute USVString protocol;
attribute USVString username;
attribute USVString password;
attribute USVString host;
attribute USVString hostname;
attribute USVString port;
- // TODO: attribute USVString pathname;
- // TODO: attribute USVString search;
+ attribute USVString pathname;
+ attribute USVString search;
[SameObject] readonly attribute URLSearchParams searchParams;
- // TODO: attribute USVString hash;
+ attribute USVString hash;
USVString toJSON();
};