mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 11:48:10 +00: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
6 changed files with 28 additions and 21 deletions
|
@ -45,6 +45,12 @@ DeprecatedString URL::password(ApplyPercentDecoding apply_percent_decoding) cons
|
||||||
return apply_percent_decoding == ApplyPercentDecoding::Yes ? percent_decode(m_password) : m_password;
|
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
|
DeprecatedString URL::basename(ApplyPercentDecoding apply_percent_decoding) const
|
||||||
{
|
{
|
||||||
if (!m_valid)
|
if (!m_valid)
|
||||||
|
|
2
AK/URL.h
2
AK/URL.h
|
@ -65,6 +65,8 @@ public:
|
||||||
DeprecatedString query(ApplyPercentDecoding = ApplyPercentDecoding::No) const;
|
DeprecatedString query(ApplyPercentDecoding = ApplyPercentDecoding::No) const;
|
||||||
DeprecatedString fragment(ApplyPercentDecoding = ApplyPercentDecoding::Yes) const;
|
DeprecatedString fragment(ApplyPercentDecoding = ApplyPercentDecoding::Yes) const;
|
||||||
Optional<u16> port() const { return m_port; }
|
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)); }
|
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; }
|
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 = {};
|
url->m_query = {};
|
||||||
auto substring_from_pointer = input.substring_view(iterator - input.begin()).as_string();
|
auto substring_from_pointer = input.substring_view(iterator - input.begin()).as_string();
|
||||||
if (!starts_with_windows_drive_letter(substring_from_pointer)) {
|
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);
|
url->m_paths.remove(url->m_paths.size() - 1);
|
||||||
} else {
|
} else {
|
||||||
report_validation_error();
|
report_validation_error();
|
||||||
|
|
|
@ -203,8 +203,8 @@ TEST_CASE(mailto_url)
|
||||||
EXPECT_EQ(url.scheme(), "mailto");
|
EXPECT_EQ(url.scheme(), "mailto");
|
||||||
EXPECT(url.host().is_null());
|
EXPECT(url.host().is_null());
|
||||||
EXPECT_EQ(url.port_or_default(), 0);
|
EXPECT_EQ(url.port_or_default(), 0);
|
||||||
EXPECT_EQ(url.paths().size(), 1u);
|
EXPECT_EQ(url.path_segment_count(), 1u);
|
||||||
EXPECT_EQ(url.paths()[0], "mail@example.com");
|
EXPECT_EQ(url.path_segment_at_index(0), "mail@example.com");
|
||||||
EXPECT(url.query().is_null());
|
EXPECT(url.query().is_null());
|
||||||
EXPECT(url.fragment().is_null());
|
EXPECT(url.fragment().is_null());
|
||||||
EXPECT_EQ(url.serialize(), "mailto:mail@example.com");
|
EXPECT_EQ(url.serialize(), "mailto:mail@example.com");
|
||||||
|
@ -329,20 +329,20 @@ TEST_CASE(create_with_file_scheme)
|
||||||
EXPECT(url.is_valid());
|
EXPECT(url.is_valid());
|
||||||
EXPECT_EQ(url.scheme(), "file");
|
EXPECT_EQ(url.scheme(), "file");
|
||||||
EXPECT_EQ(url.port_or_default(), 0);
|
EXPECT_EQ(url.port_or_default(), 0);
|
||||||
EXPECT_EQ(url.paths().size(), 3u);
|
EXPECT_EQ(url.path_segment_count(), 3u);
|
||||||
EXPECT_EQ(url.paths()[0], "home");
|
EXPECT_EQ(url.path_segment_at_index(0), "home");
|
||||||
EXPECT_EQ(url.paths()[1], "anon");
|
EXPECT_EQ(url.path_segment_at_index(1), "anon");
|
||||||
EXPECT_EQ(url.paths()[2], "README.md");
|
EXPECT_EQ(url.path_segment_at_index(2), "README.md");
|
||||||
EXPECT_EQ(url.serialize_path(), "/home/anon/README.md");
|
EXPECT_EQ(url.serialize_path(), "/home/anon/README.md");
|
||||||
EXPECT(url.query().is_null());
|
EXPECT(url.query().is_null());
|
||||||
EXPECT(url.fragment().is_null());
|
EXPECT(url.fragment().is_null());
|
||||||
|
|
||||||
url = URL::create_with_file_scheme("/home/anon/");
|
url = URL::create_with_file_scheme("/home/anon/");
|
||||||
EXPECT(url.is_valid());
|
EXPECT(url.is_valid());
|
||||||
EXPECT_EQ(url.paths().size(), 3u);
|
EXPECT_EQ(url.path_segment_count(), 3u);
|
||||||
EXPECT_EQ(url.paths()[0], "home");
|
EXPECT_EQ(url.path_segment_at_index(0), "home");
|
||||||
EXPECT_EQ(url.paths()[1], "anon");
|
EXPECT_EQ(url.path_segment_at_index(1), "anon");
|
||||||
EXPECT_EQ(url.paths()[2], "");
|
EXPECT_EQ(url.path_segment_at_index(2), "");
|
||||||
EXPECT_EQ(url.serialize_path(), "/home/anon/");
|
EXPECT_EQ(url.serialize_path(), "/home/anon/");
|
||||||
|
|
||||||
url = URL("file:///home/anon/"sv);
|
url = URL("file:///home/anon/"sv);
|
||||||
|
@ -399,9 +399,9 @@ TEST_CASE(complete_file_url_with_base)
|
||||||
URL url { "file:///home/index.html" };
|
URL url { "file:///home/index.html" };
|
||||||
EXPECT(url.is_valid());
|
EXPECT(url.is_valid());
|
||||||
EXPECT_EQ(url.serialize_path(), "/home/index.html");
|
EXPECT_EQ(url.serialize_path(), "/home/index.html");
|
||||||
EXPECT_EQ(url.paths().size(), 2u);
|
EXPECT_EQ(url.path_segment_count(), 2u);
|
||||||
EXPECT_EQ(url.paths()[0], "home");
|
EXPECT_EQ(url.path_segment_at_index(0), "home");
|
||||||
EXPECT_EQ(url.paths()[1], "index.html");
|
EXPECT_EQ(url.path_segment_at_index(1), "index.html");
|
||||||
|
|
||||||
auto sub_url = url.complete_url("js/app.js"sv);
|
auto sub_url = url.complete_url("js/app.js"sv);
|
||||||
EXPECT(sub_url.is_valid());
|
EXPECT(sub_url.is_valid());
|
||||||
|
|
|
@ -226,7 +226,7 @@ void URLProvider::query(DeprecatedString const& query, Function<void(Vector<Nonn
|
||||||
url.set_scheme("http");
|
url.set_scheme("http");
|
||||||
if (url.host().is_empty())
|
if (url.host().is_empty())
|
||||||
url.set_host(query);
|
url.set_host(query);
|
||||||
if (url.paths().is_empty())
|
if (url.path_segment_count() == 0)
|
||||||
url.set_paths({ "" });
|
url.set_paths({ "" });
|
||||||
|
|
||||||
if (!url.is_valid())
|
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")
|
if (url.host() != "man")
|
||||||
return Error::from_string_view("Bad help operation"sv);
|
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);
|
return Error::from_string_view("Bad help page URL"sv);
|
||||||
|
|
||||||
auto paths = url.paths();
|
auto const section = url.path_segment_at_index(0);
|
||||||
auto const section = paths.take_first();
|
|
||||||
auto maybe_section_number = section.to_uint();
|
auto maybe_section_number = section.to_uint();
|
||||||
if (!maybe_section_number.has_value())
|
if (!maybe_section_number.has_value())
|
||||||
return Error::from_string_view("Bad section number"sv);
|
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];
|
NonnullRefPtr<Node const> current_node = sections[section_number - 1];
|
||||||
|
|
||||||
while (!paths.is_empty()) {
|
for (size_t i = 1; i < url.path_segment_count(); i++) {
|
||||||
auto next_path_segment = TRY(String::from_deprecated_string(paths.take_first()));
|
|
||||||
auto children = TRY(current_node->children());
|
auto children = TRY(current_node->children());
|
||||||
for (auto const& child : 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;
|
current_node = child;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return current_node;
|
return current_node;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue