Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions src/url.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -776,10 +776,16 @@ bool url::set_port(const std::string_view input) {
if (cannot_have_credentials_or_port()) {
return false;
}

if (input.empty()) {
port = std::nullopt;
return true;
}

std::string trimmed(input);
helpers::remove_ascii_tab_or_newline(trimmed);

if (trimmed.empty()) {
port = std::nullopt;
return true;
}

Expand All @@ -788,9 +794,15 @@ bool url::set_port(const std::string_view input) {
return false;
}

// Find the first non-digit character to determine the length of digits
auto first_non_digit =
std::ranges::find_if_not(trimmed, ada::unicode::is_ascii_digit);
std::string_view digits_to_parse =
std::string_view(trimmed.data(), first_non_digit - trimmed.begin());

// Revert changes if parse_port fails.
std::optional<uint16_t> previous_port = port;
parse_port(trimmed);
parse_port(digits_to_parse);
if (is_valid) {
return true;
}
Expand Down
16 changes: 14 additions & 2 deletions src/url_aggregator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -280,10 +280,16 @@ bool url_aggregator::set_port(const std::string_view input) {
if (cannot_have_credentials_or_port()) {
return false;
}

if (input.empty()) {
clear_port();
return true;
}

std::string trimmed(input);
helpers::remove_ascii_tab_or_newline(trimmed);

if (trimmed.empty()) {
clear_port();
return true;
}

Expand All @@ -292,9 +298,15 @@ bool url_aggregator::set_port(const std::string_view input) {
return false;
}

// Find the first non-digit character to determine the length of digits
auto first_non_digit =
std::ranges::find_if_not(trimmed, ada::unicode::is_ascii_digit);
std::string_view digits_to_parse =
std::string_view(trimmed.data(), first_non_digit - trimmed.begin());

// Revert changes if parse_port fails.
uint32_t previous_port = components.port;
parse_port(trimmed);
parse_port(digits_to_parse);
if (is_valid) {
return true;
}
Expand Down
14 changes: 14 additions & 0 deletions tests/wpt/ada_extra_setters_tests.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,20 @@
"expected": {
"port": "4"
}
},
{
"href": "https://domain.com:3000",
"new_value": "\n\t80\n\t80\n\t",
"expected": {
"port": "8080"
}
},
{
"href": "https://domain.com:3000",
"new_value": "\n\n\t\t",
"expected": {
"port": "3000"
}
}
],
"hash": [
Expand Down
Loading