Skip to content

Commit

Permalink
LibWeb/URL: Add strip_trailing_spaces_from_an_opaque_path()
Browse files Browse the repository at this point in the history
Also remove 2 FIXMEs by including this function.
  • Loading branch information
kemzeb committed Sep 11, 2023
1 parent e738015 commit 80d5e61
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pathname => '/well '
pathname => '/well'
pathname => '/search '
pathname => '/search'
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<script src="../include.js"></script>
<script>
test(() => {
let url = new URL("https://serenityos.org/well #hello");
println(`pathname => '${url.pathname}'`);
url.hash = "";
println(`pathname => '${url.pathname}'`);

url = new URL("https://serenityos.org/search ?q=friends");
println(`pathname => '${url.pathname}'`);
url.search = "";
println(`pathname => '${url.pathname}'`);
});
</script>
28 changes: 26 additions & 2 deletions Userland/Libraries/LibWeb/URL/URL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,8 @@ WebIDL::ExceptionOr<void> URL::set_search(String const& search)
// 2. Empty this’s query object’s list.
m_query->m_list.clear();

// FIXME: 3. Potentially strip trailing spaces from an opaque path with this.
// 3. Potentially strip trailing spaces from an opaque path with this.
strip_trailing_spaces_from_an_opaque_path(*this);

// 4. Return.
return {};
Expand Down Expand Up @@ -457,7 +458,8 @@ void URL::set_hash(String const& hash)
// 1. Set this’s URL’s fragment to null.
m_url.set_fragment({});

// FIXME: 2. Potentially strip trailing spaces from an opaque path with this.
// 2. Potentially strip trailing spaces from an opaque path with this.
strip_trailing_spaces_from_an_opaque_path(*this);

// 3. Return.
return;
Expand Down Expand Up @@ -535,6 +537,28 @@ bool host_is_domain(AK::URL::Host const& host)
return host.has<String>() && host.get<String>() != String {};
}

// https://url.spec.whatwg.org/#potentially-strip-trailing-spaces-from-an-opaque-path
void strip_trailing_spaces_from_an_opaque_path(URL& url)
{
// 1. If url’s URL does not have an opaque path, then return.
if (!url.is_opaque_path())
return;

// 2. If url’s URL’s fragment is non-null, then return.
if (url.fragment().has_value())
return;

// 3. If url’s URL’s query is non-null, then return.
if (url.query().has_value())
return;

// 4. Remove all trailing U+0020 SPACE code points from url’s URL’s path.
// NOTE: At index 0 since the has_opaque_path() check tells us that the URL only has one path segment.
auto opaque_path = MUST(url.path_segment_at_index(0));
auto trimmed_path = MUST(opaque_path.trim(" "sv, TrimMode::Right));
url.set_pathname(trimmed_path);
}

// https://url.spec.whatwg.org/#concept-url-parser
AK::URL parse(StringView input, Optional<AK::URL> const& base_url)
{
Expand Down
10 changes: 10 additions & 0 deletions Userland/Libraries/LibWeb/URL/URL.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ class URL : public Bindings::PlatformObject {
WebIDL::ExceptionOr<String> pathname() const;
void set_pathname(String const&);

Optional<String> const& fragment() const { return m_url.fragment(); }

ErrorOr<String> path_segment_at_index(size_t index) const { return String::from_deprecated_string(m_url.path_segment_at_index(index)); }

bool is_opaque_path() const { return m_url.is_opaque_path(); }

WebIDL::ExceptionOr<String> search() const;
WebIDL::ExceptionOr<void> set_search(String const&);

Expand All @@ -65,6 +71,7 @@ class URL : public Bindings::PlatformObject {

WebIDL::ExceptionOr<String> to_json() const;

Optional<String> const& query() const { return m_url.query(); }
void set_query(Badge<URLSearchParams>, Optional<String> query) { m_url.set_query(move(query)); }

private:
Expand All @@ -80,6 +87,9 @@ class URL : public Bindings::PlatformObject {
HTML::Origin url_origin(AK::URL const&);
bool host_is_domain(AK::URL::Host const&);

// https://url.spec.whatwg.org/#potentially-strip-trailing-spaces-from-an-opaque-path
void strip_trailing_spaces_from_an_opaque_path(URL& url);

// https://url.spec.whatwg.org/#concept-url-parser
AK::URL parse(StringView input, Optional<AK::URL> const& base_url = {});

Expand Down

0 comments on commit 80d5e61

Please sign in to comment.