mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-09-11 13:36:13 +03:00
AK+Everywhere: Replace URL::paths() with path_segment_at_index()
This allows accessing and looping over the path segments in a URL without necessarily allocating a new vector if you want them percent decoded too (which path_segment_at_index() has an option for).
This commit is contained in:
parent
35612c6a7f
commit
5db1eb9961
Notes:
sideshowbarker
2024-07-16 21:23:42 +09:00
Author: https://github.com/MacDue Commit: https://github.com/SerenityOS/serenity/commit/5db1eb9961 Pull-request: https://github.com/SerenityOS/serenity/pull/18341 Reviewed-by: https://github.com/awesomekling
@ -45,6 +45,12 @@ DeprecatedString URL::password(ApplyPercentDecoding apply_percent_decoding) cons
|
||||
return apply_percent_decoding == ApplyPercentDecoding::Yes ? percent_decode(m_password) : m_password;
|
||||
}
|
||||
|
||||
DeprecatedString URL::path_segment_at_index(size_t index, ApplyPercentDecoding apply_percent_decoding) const
|
||||
{
|
||||
VERIFY(index < path_segment_count());
|
||||
return apply_percent_decoding == ApplyPercentDecoding::Yes ? percent_decode(m_paths[index]) : m_paths[index];
|
||||
}
|
||||
|
||||
DeprecatedString URL::basename(ApplyPercentDecoding apply_percent_decoding) const
|
||||
{
|
||||
if (!m_valid)
|
||||
|
2
AK/URL.h
2
AK/URL.h
@ -65,6 +65,8 @@ public:
|
||||
DeprecatedString query(ApplyPercentDecoding = ApplyPercentDecoding::No) const;
|
||||
DeprecatedString fragment(ApplyPercentDecoding = ApplyPercentDecoding::Yes) const;
|
||||
Optional<u16> port() const { return m_port; }
|
||||
DeprecatedString path_segment_at_index(size_t index, ApplyPercentDecoding = ApplyPercentDecoding::Yes) const;
|
||||
size_t path_segment_count() const { return m_paths.size(); }
|
||||
|
||||
u16 port_or_default() const { return m_port.value_or(default_port_for_scheme(m_scheme)); }
|
||||
bool cannot_be_a_base_url() const { return m_cannot_be_a_base_url; }
|
||||
|
@ -536,7 +536,7 @@ URL URLParser::parse(StringView raw_input, Optional<URL> const& base_url, Option
|
||||
url->m_query = {};
|
||||
auto substring_from_pointer = input.substring_view(iterator - input.begin()).as_string();
|
||||
if (!starts_with_windows_drive_letter(substring_from_pointer)) {
|
||||
if (!url->paths().is_empty() && !(url->scheme() == "file" && url->paths().size() == 1 && is_normalized_windows_drive_letter(url->paths()[0])))
|
||||
if (!url->m_paths.is_empty() && !(url->scheme() == "file" && url->m_paths.size() == 1 && is_normalized_windows_drive_letter(url->m_paths[0])))
|
||||
url->m_paths.remove(url->m_paths.size() - 1);
|
||||
} else {
|
||||
report_validation_error();
|
||||
|
@ -203,8 +203,8 @@ TEST_CASE(mailto_url)
|
||||
EXPECT_EQ(url.scheme(), "mailto");
|
||||
EXPECT(url.host().is_null());
|
||||
EXPECT_EQ(url.port_or_default(), 0);
|
||||
EXPECT_EQ(url.paths().size(), 1u);
|
||||
EXPECT_EQ(url.paths()[0], "mail@example.com");
|
||||
EXPECT_EQ(url.path_segment_count(), 1u);
|
||||
EXPECT_EQ(url.path_segment_at_index(0), "mail@example.com");
|
||||
EXPECT(url.query().is_null());
|
||||
EXPECT(url.fragment().is_null());
|
||||
EXPECT_EQ(url.serialize(), "mailto:mail@example.com");
|
||||
@ -329,20 +329,20 @@ TEST_CASE(create_with_file_scheme)
|
||||
EXPECT(url.is_valid());
|
||||
EXPECT_EQ(url.scheme(), "file");
|
||||
EXPECT_EQ(url.port_or_default(), 0);
|
||||
EXPECT_EQ(url.paths().size(), 3u);
|
||||
EXPECT_EQ(url.paths()[0], "home");
|
||||
EXPECT_EQ(url.paths()[1], "anon");
|
||||
EXPECT_EQ(url.paths()[2], "README.md");
|
||||
EXPECT_EQ(url.path_segment_count(), 3u);
|
||||
EXPECT_EQ(url.path_segment_at_index(0), "home");
|
||||
EXPECT_EQ(url.path_segment_at_index(1), "anon");
|
||||
EXPECT_EQ(url.path_segment_at_index(2), "README.md");
|
||||
EXPECT_EQ(url.serialize_path(), "/home/anon/README.md");
|
||||
EXPECT(url.query().is_null());
|
||||
EXPECT(url.fragment().is_null());
|
||||
|
||||
url = URL::create_with_file_scheme("/home/anon/");
|
||||
EXPECT(url.is_valid());
|
||||
EXPECT_EQ(url.paths().size(), 3u);
|
||||
EXPECT_EQ(url.paths()[0], "home");
|
||||
EXPECT_EQ(url.paths()[1], "anon");
|
||||
EXPECT_EQ(url.paths()[2], "");
|
||||
EXPECT_EQ(url.path_segment_count(), 3u);
|
||||
EXPECT_EQ(url.path_segment_at_index(0), "home");
|
||||
EXPECT_EQ(url.path_segment_at_index(1), "anon");
|
||||
EXPECT_EQ(url.path_segment_at_index(2), "");
|
||||
EXPECT_EQ(url.serialize_path(), "/home/anon/");
|
||||
|
||||
url = URL("file:///home/anon/"sv);
|
||||
@ -399,9 +399,9 @@ TEST_CASE(complete_file_url_with_base)
|
||||
URL url { "file:///home/index.html" };
|
||||
EXPECT(url.is_valid());
|
||||
EXPECT_EQ(url.serialize_path(), "/home/index.html");
|
||||
EXPECT_EQ(url.paths().size(), 2u);
|
||||
EXPECT_EQ(url.paths()[0], "home");
|
||||
EXPECT_EQ(url.paths()[1], "index.html");
|
||||
EXPECT_EQ(url.path_segment_count(), 2u);
|
||||
EXPECT_EQ(url.path_segment_at_index(0), "home");
|
||||
EXPECT_EQ(url.path_segment_at_index(1), "index.html");
|
||||
|
||||
auto sub_url = url.complete_url("js/app.js"sv);
|
||||
EXPECT(sub_url.is_valid());
|
||||
|
@ -226,7 +226,7 @@ void URLProvider::query(DeprecatedString const& query, Function<void(Vector<Nonn
|
||||
url.set_scheme("http");
|
||||
if (url.host().is_empty())
|
||||
url.set_host(query);
|
||||
if (url.paths().is_empty())
|
||||
if (url.path_segment_count() == 0)
|
||||
url.set_paths({ "" });
|
||||
|
||||
if (!url.is_valid())
|
||||
|
@ -70,11 +70,10 @@ ErrorOr<NonnullRefPtr<Node const>> Node::try_find_from_help_url(URL const& url)
|
||||
{
|
||||
if (url.host() != "man")
|
||||
return Error::from_string_view("Bad help operation"sv);
|
||||
if (url.paths().size() < 2)
|
||||
if (url.path_segment_count() < 2)
|
||||
return Error::from_string_view("Bad help page URL"sv);
|
||||
|
||||
auto paths = url.paths();
|
||||
auto const section = paths.take_first();
|
||||
auto const section = url.path_segment_at_index(0);
|
||||
auto maybe_section_number = section.to_uint();
|
||||
if (!maybe_section_number.has_value())
|
||||
return Error::from_string_view("Bad section number"sv);
|
||||
@ -84,16 +83,16 @@ ErrorOr<NonnullRefPtr<Node const>> Node::try_find_from_help_url(URL const& url)
|
||||
|
||||
NonnullRefPtr<Node const> current_node = sections[section_number - 1];
|
||||
|
||||
while (!paths.is_empty()) {
|
||||
auto next_path_segment = TRY(String::from_deprecated_string(paths.take_first()));
|
||||
for (size_t i = 1; i < url.path_segment_count(); i++) {
|
||||
auto children = TRY(current_node->children());
|
||||
for (auto const& child : children) {
|
||||
if (TRY(child->name()) == next_path_segment) {
|
||||
if (TRY(child->name()) == url.path_segment_at_index(i).view()) {
|
||||
current_node = child;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return current_node;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user