LibWeb/URL: Add strip_trailing_spaces_from_an_opaque_path()

Also remove 2 FIXMEs by including this function.
This commit is contained in:
Kemal Zebari 2023-09-10 16:50:29 -07:00 committed by Andrew Kaster
parent ce549eb92a
commit 824c54acaf
4 changed files with 58 additions and 2 deletions

View file

@ -0,0 +1,4 @@
pathname => 'foobar '
pathname => 'foobar'
pathname => 'baz '
pathname => 'baz'

View file

@ -0,0 +1,14 @@
<script src="../include.js"></script>
<script>
test(() => {
let url = new URL("scheme:foobar ?well=hello");
println(`pathname => '${url.pathname}'`);
url.search = "";
println(`pathname => '${url.pathname}'`);
url = new URL("scheme:baz #friends");
println(`pathname => '${url.pathname}'`);
url.hash = "";
println(`pathname => '${url.pathname}'`);
});
</script>

View file

@ -403,7 +403,8 @@ WebIDL::ExceptionOr<void> URL::set_search(String const& search)
// 2. Empty thiss query objects 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 {};
@ -457,7 +458,8 @@ void URL::set_hash(String const& hash)
// 1. Set thiss URLs 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;
@ -535,6 +537,29 @@ 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 urls URL does not have an opaque path, then return.
// FIXME: Reimplement this step once we modernize the URL implementation to meet the spec.
if (!url.cannot_be_a_base_url())
return;
// 2. If urls URLs fragment is non-null, then return.
if (url.fragment().has_value())
return;
// 3. If urls URLs query is non-null, then return.
if (url.query().has_value())
return;
// 4. Remove all trailing U+0020 SPACE code points from urls URLs path.
// NOTE: At index 0 since the first step tells us that the URL only has one path segment.
auto opaque_path = url.path_segment_at_index(0);
auto trimmed_path = opaque_path.trim(" "sv, TrimMode::Right);
url.set_paths({ trimmed_path });
}
// https://url.spec.whatwg.org/#concept-url-parser
AK::URL parse(StringView input, Optional<AK::URL> const& base_url)
{

View file

@ -55,6 +55,15 @@ public:
WebIDL::ExceptionOr<String> pathname() const;
void set_pathname(String const&);
Optional<String> const& fragment() const { return m_url.fragment(); }
DeprecatedString path_segment_at_index(size_t index) const { return m_url.path_segment_at_index(index); }
void set_paths(Vector<DeprecatedString> const& paths) { return m_url.set_paths(paths); }
// FIXME: Reimplement this to meet the definition in https://url.spec.whatwg.org/#url-opaque-path once we modernize URL to meet the spec.
bool cannot_be_a_base_url() const { return m_url.cannot_be_a_base_url(); }
WebIDL::ExceptionOr<String> search() const;
WebIDL::ExceptionOr<void> set_search(String const&);
@ -65,6 +74,7 @@ public:
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:
@ -80,6 +90,9 @@ private:
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 = {});