AK: Strip leading/trailing C0-control-or-space in URLs correctly

We have to stop scanning once we hit a non-strippable character.
Add some tests to cover this.
This commit is contained in:
Andreas Kling 2021-06-01 13:11:14 +02:00
parent 468bb54677
commit c0d1a75881
Notes: sideshowbarker 2024-07-18 17:03:05 +09:00
2 changed files with 27 additions and 2 deletions

View file

@ -196,12 +196,16 @@ URL URLParser::parse(Badge<URL>, const StringView& raw_input, URL const* base_ur
if (raw_input[i] <= 0x20) {
++start_index;
has_validation_error = true;
} else {
break;
}
}
for (size_t i = 0; i < raw_input.length(); ++i) {
if (raw_input[raw_input.length() - 1 - i] <= 0x20) {
for (ssize_t i = raw_input.length() - 1; i >= 0; --i) {
if (raw_input[i] <= 0x20) {
--end_index;
has_validation_error = true;
} else {
break;
}
}
if (has_validation_error)

View file

@ -307,3 +307,24 @@ TEST_CASE(complete_url)
EXPECT(base_url.complete_url("../index.html#fragment").equals(base_url));
}
TEST_CASE(leading_whitespace)
{
URL url { " https://foo.com/" };
EXPECT(url.is_valid());
EXPECT_EQ(url.to_string(), "https://foo.com/");
}
TEST_CASE(trailing_whitespace)
{
URL url { "https://foo.com/ " };
EXPECT(url.is_valid());
EXPECT_EQ(url.to_string(), "https://foo.com/");
}
TEST_CASE(leading_and_trailing_whitespace)
{
URL url { " https://foo.com/ " };
EXPECT(url.is_valid());
EXPECT_EQ(url.to_string(), "https://foo.com/");
}