From 98666b012d1d48679c16692402817fffa3a69561 Mon Sep 17 00:00:00 2001 From: Shannon Booth Date: Sun, 6 Aug 2023 16:32:44 +1200 Subject: [PATCH] AK: Remove URL::ApplyPercentEncoding Everywhere only ever expects percent encoding to occur, so let's just remove this flag altogether. At the same time, replace some DeprecatedString with StringView. --- AK/URL.cpp | 49 ++++++++------------- AK/URL.h | 16 +++---- Userland/Libraries/LibWeb/DOM/Document.cpp | 2 +- Userland/Libraries/LibWeb/HTML/Location.cpp | 2 +- Userland/Libraries/LibWeb/URL/URL.cpp | 4 +- Userland/Libraries/LibWeb/URL/URL.h | 2 +- 6 files changed, 30 insertions(+), 45 deletions(-) diff --git a/AK/URL.cpp b/AK/URL.cpp index d41746f6fe..6ba190facf 100644 --- a/AK/URL.cpp +++ b/AK/URL.cpp @@ -86,19 +86,19 @@ void URL::set_scheme(DeprecatedString scheme) m_valid = compute_validity(); } -void URL::set_username(DeprecatedString username, ApplyPercentEncoding apply_percent_encoding) +// https://url.spec.whatwg.org/#set-the-username +void URL::set_username(StringView username) { - if (apply_percent_encoding == ApplyPercentEncoding::Yes) - username = deprecated_string_percent_encode(username, PercentEncodeSet::Userinfo); - m_username = move(username); + // To set the username given a url and username, set url’s username to the result of running UTF-8 percent-encode on username using the userinfo percent-encode set. + m_username = deprecated_string_percent_encode(username, PercentEncodeSet::Userinfo); m_valid = compute_validity(); } -void URL::set_password(DeprecatedString password, ApplyPercentEncoding apply_percent_encoding) +// https://url.spec.whatwg.org/#set-the-password +void URL::set_password(StringView password) { - if (apply_percent_encoding == ApplyPercentEncoding::Yes) - password = deprecated_string_percent_encode(password, PercentEncodeSet::Userinfo); - m_password = move(password); + // To set the password given a url and password, set url’s password to the result of running UTF-8 percent-encode on password using the userinfo percent-encode set. + m_password = deprecated_string_percent_encode(password, PercentEncodeSet::Userinfo); m_valid = compute_validity(); } @@ -124,39 +124,28 @@ void URL::set_port(Optional port) m_valid = compute_validity(); } -void URL::set_paths(Vector paths, ApplyPercentEncoding apply_percent_encoding) +void URL::set_paths(Vector const& paths) { - if (apply_percent_encoding == ApplyPercentEncoding::Yes) { - Vector encoded_paths; - encoded_paths.ensure_capacity(paths.size()); - for (auto& segment : paths) - encoded_paths.unchecked_append(deprecated_string_percent_encode(segment, PercentEncodeSet::Path)); - m_paths = move(encoded_paths); - } else { - m_paths = move(paths); - } + m_paths.clear_with_capacity(); + m_paths.ensure_capacity(paths.size()); + for (auto const& segment : paths) + m_paths.unchecked_append(deprecated_string_percent_encode(segment, PercentEncodeSet::Path)); m_valid = compute_validity(); } -void URL::append_path(DeprecatedString path, ApplyPercentEncoding apply_percent_encoding) +void URL::append_path(StringView path) { - if (apply_percent_encoding == ApplyPercentEncoding::Yes) - path = deprecated_string_percent_encode(path, PercentEncodeSet::Path); - m_paths.append(path); + m_paths.append(deprecated_string_percent_encode(path, PercentEncodeSet::Path)); } -void URL::set_query(DeprecatedString query, ApplyPercentEncoding apply_percent_encoding) +void URL::set_query(StringView query) { - if (apply_percent_encoding == ApplyPercentEncoding::Yes) - query = deprecated_string_percent_encode(query, is_special() ? PercentEncodeSet::SpecialQuery : PercentEncodeSet::Query); - m_query = move(query); + m_query = deprecated_string_percent_encode(query, is_special() ? PercentEncodeSet::SpecialQuery : PercentEncodeSet::Query); } -void URL::set_fragment(DeprecatedString fragment, ApplyPercentEncoding apply_percent_encoding) +void URL::set_fragment(StringView fragment) { - if (apply_percent_encoding == ApplyPercentEncoding::Yes) - fragment = deprecated_string_percent_encode(fragment, PercentEncodeSet::Fragment); - m_fragment = move(fragment); + m_fragment = deprecated_string_percent_encode(fragment, PercentEncodeSet::Fragment); } // https://url.spec.whatwg.org/#cannot-have-a-username-password-port diff --git a/AK/URL.h b/AK/URL.h index bcda75b7d4..b2e0c4ce5f 100644 --- a/AK/URL.h +++ b/AK/URL.h @@ -95,20 +95,16 @@ public: bool includes_credentials() const { return !m_username.is_empty() || !m_password.is_empty(); } bool is_special() const { return is_special_scheme(m_scheme); } - enum class ApplyPercentEncoding { - Yes, - No - }; void set_scheme(DeprecatedString); - void set_username(DeprecatedString, ApplyPercentEncoding = ApplyPercentEncoding::Yes); - void set_password(DeprecatedString, ApplyPercentEncoding = ApplyPercentEncoding::Yes); + void set_username(StringView); + void set_password(StringView); void set_host(Host); void set_port(Optional); - void set_paths(Vector, ApplyPercentEncoding = ApplyPercentEncoding::Yes); - void set_query(DeprecatedString, ApplyPercentEncoding = ApplyPercentEncoding::Yes); - void set_fragment(DeprecatedString fragment, ApplyPercentEncoding = ApplyPercentEncoding::Yes); + void set_paths(Vector const&); + void set_query(StringView); + void set_fragment(StringView fragment); void set_cannot_be_a_base_url(bool value) { m_cannot_be_a_base_url = value; } - void append_path(DeprecatedString, ApplyPercentEncoding = ApplyPercentEncoding::Yes); + void append_path(StringView); void append_slash() { // NOTE: To indicate that we want to end the path with a slash, we have to append an empty path segment. diff --git a/Userland/Libraries/LibWeb/DOM/Document.cpp b/Userland/Libraries/LibWeb/DOM/Document.cpp index a6cef56816..9bca97c566 100644 --- a/Userland/Libraries/LibWeb/DOM/Document.cpp +++ b/Userland/Libraries/LibWeb/DOM/Document.cpp @@ -506,7 +506,7 @@ WebIDL::ExceptionOr Document::open(DeprecatedString const&, Deprecate auto new_url = entry_document.url(); // 2. If entryDocument is not document, then set newURL's fragment to null. if (&entry_document != this) - new_url.set_fragment(""); + new_url.set_fragment({}); // FIXME: 3. Run the URL and history update steps with document and newURL. } diff --git a/Userland/Libraries/LibWeb/HTML/Location.cpp b/Userland/Libraries/LibWeb/HTML/Location.cpp index c4727456d4..71c5f11bc8 100644 --- a/Userland/Libraries/LibWeb/HTML/Location.cpp +++ b/Userland/Libraries/LibWeb/HTML/Location.cpp @@ -309,7 +309,7 @@ WebIDL::ExceptionOr Location::set_hash(String const& value) auto input = value.bytes_as_string_view().trim("#"sv, TrimMode::Left); // 5. Set copyURL's fragment to the empty string. - copy_url.set_fragment(""); + copy_url.set_fragment(""sv); // 6. Basic URL parse input, with copyURL as url and fragment state as state override. auto result_url = URLParser::basic_parse(input, {}, copy_url, URLParser::State::Fragment); diff --git a/Userland/Libraries/LibWeb/URL/URL.cpp b/Userland/Libraries/LibWeb/URL/URL.cpp index 207d404721..1daad7d438 100644 --- a/Userland/Libraries/LibWeb/URL/URL.cpp +++ b/Userland/Libraries/LibWeb/URL/URL.cpp @@ -237,7 +237,7 @@ void URL::set_username(String const& username) return; // 2. Set the username given this’s URL and the given value. - m_url.set_username(username.to_deprecated_string(), AK::URL::ApplyPercentEncoding::Yes); + m_url.set_username(username); } // https://url.spec.whatwg.org/#dom-url-password @@ -257,7 +257,7 @@ void URL::set_password(String const& password) return; // 2. Set the password given this’s URL and the given value. - m_url.set_password(password.to_deprecated_string(), AK::URL::ApplyPercentEncoding::Yes); + m_url.set_password(password); } // https://url.spec.whatwg.org/#dom-url-host diff --git a/Userland/Libraries/LibWeb/URL/URL.h b/Userland/Libraries/LibWeb/URL/URL.h index da2e105a84..1cba47b8da 100644 --- a/Userland/Libraries/LibWeb/URL/URL.h +++ b/Userland/Libraries/LibWeb/URL/URL.h @@ -65,7 +65,7 @@ public: WebIDL::ExceptionOr to_json() const; - void set_query(Badge, String query) { m_url.set_query(query.to_deprecated_string(), AK::URL::ApplyPercentEncoding::Yes); } + void set_query(Badge, StringView query) { m_url.set_query(query); } private: URL(JS::Realm&, AK::URL, JS::NonnullGCPtr query);