Skip to content

Commit 40719fc

Browse files
committed
fix: potentially strip trailing spaces from opaque path
1 parent adc93ac commit 40719fc

File tree

3 files changed

+14
-13
lines changed

3 files changed

+14
-13
lines changed

include/ada/helpers.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ namespace ada::helpers {
7373
*/
7474
ada_really_inline void trim_c0_whitespace(std::string_view& input) noexcept;
7575

76+
/**
77+
* @see https://url.spec.whatwg.org/#potentially-strip-trailing-spaces-from-an-opaque-path
78+
*/
79+
ada_really_inline void strip_trailing_spaces_from_opaque_path(ada::url& url) noexcept;
80+
7681
} // namespace ada::helpers
7782

7883
#endif // ADA_HELPERS_H

src/helpers.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,13 @@ namespace ada::helpers {
239239
} while (true);
240240
}
241241
}
242+
243+
ada_really_inline void strip_trailing_spaces_from_opaque_path(ada::url& url) noexcept {
244+
if (!url.has_opaque_path) return;
245+
if (url.fragment.has_value()) return;
246+
if (url.query.has_value()) return;
247+
while (!url.path.empty() && url.path.back() == ' ') { url.path.resize(url.path.size()-1); }
248+
}
242249
} // namespace ada::helpers
243250

244251
namespace ada {

src/url-setters.cpp

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ namespace ada {
3636
void url::set_hash(const std::string_view input) {
3737
if (input.empty()) {
3838
fragment = std::nullopt;
39-
// TODO: Potentially strip trailing spaces from an opaque path with this.
39+
helpers::strip_trailing_spaces_from_opaque_path(*this);
4040
return;
4141
}
4242

@@ -50,14 +50,7 @@ namespace ada {
5050
void url::set_search(const std::string_view input) {
5151
if (input.empty()) {
5252
query = std::nullopt;
53-
54-
// Potentially strip trailing spaces from an opaque path
55-
if (!has_opaque_path || fragment.has_value()) return;
56-
57-
size_t non_whitespace_location = path.find_first_of(' ');
58-
if (non_whitespace_location != std::string_view::npos) {
59-
path = path.substr(0, non_whitespace_location);
60-
}
53+
helpers::strip_trailing_spaces_from_opaque_path(*this);
6154
return;
6255
}
6356

@@ -70,10 +63,6 @@ namespace ada {
7063
ada::character_sets::QUERY_PERCENT_ENCODE;
7164

7265
query = ada::unicode::percent_encode(std::string_view(new_value), query_percent_encode_set);
73-
74-
// Set this’s query object’s list to the result of parsing input.
75-
// @todo Implement this if/when we have URLSearchParams.
76-
return ;
7766
}
7867

7968
bool url::set_pathname(const std::string_view input) {

0 commit comments

Comments
 (0)