mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-08-05 23:59:49 +00:00
LibURL: Port create_with_file_scheme to Optional
Removing one of the main remaining users of URL valid state.
This commit is contained in:
parent
2072eee83d
commit
00bbb2105b
Notes:
github-actions[bot]
2025-04-19 11:20:14 +00:00
Author: https://github.com/shannonbooth
Commit: 00bbb2105b
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/4401
Reviewed-by: https://github.com/trflynn89 ✅
5 changed files with 18 additions and 16 deletions
|
@ -163,7 +163,7 @@ Optional<u16> default_port_for_scheme(StringView scheme)
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
URL create_with_file_scheme(ByteString const& path, ByteString const& fragment, ByteString const& hostname)
|
Optional<URL> create_with_file_scheme(ByteString const& path, ByteString const& fragment, ByteString const& hostname)
|
||||||
{
|
{
|
||||||
LexicalPath lexical_path(path);
|
LexicalPath lexical_path(path);
|
||||||
if (!lexical_path.is_absolute())
|
if (!lexical_path.is_absolute())
|
||||||
|
@ -180,11 +180,10 @@ URL create_with_file_scheme(ByteString const& path, ByteString const& fragment,
|
||||||
url_builder.append(fragment);
|
url_builder.append(fragment);
|
||||||
}
|
}
|
||||||
|
|
||||||
auto url = Parser::basic_parse(url_builder.string_view());
|
return Parser::basic_parse(url_builder.string_view());
|
||||||
return url.has_value() ? url.release_value() : URL {};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
URL create_with_url_or_path(ByteString const& url_or_path)
|
Optional<URL> create_with_url_or_path(ByteString const& url_or_path)
|
||||||
{
|
{
|
||||||
auto url = Parser::basic_parse(url_or_path);
|
auto url = Parser::basic_parse(url_or_path);
|
||||||
if (url.has_value())
|
if (url.has_value())
|
||||||
|
|
|
@ -203,8 +203,8 @@ private:
|
||||||
AK::CopyOnWrite<Data> m_data;
|
AK::CopyOnWrite<Data> m_data;
|
||||||
};
|
};
|
||||||
|
|
||||||
URL create_with_url_or_path(ByteString const&);
|
Optional<URL> create_with_url_or_path(ByteString const&);
|
||||||
URL create_with_file_scheme(ByteString const& path, ByteString const& fragment = {}, ByteString const& hostname = {});
|
Optional<URL> create_with_file_scheme(ByteString const& path, ByteString const& fragment = {}, ByteString const& hostname = {});
|
||||||
URL create_with_data(StringView mime_type, StringView payload, bool is_base64 = false);
|
URL create_with_data(StringView mime_type, StringView payload, bool is_base64 = false);
|
||||||
|
|
||||||
bool is_public_suffix(StringView host);
|
bool is_public_suffix(StringView host);
|
||||||
|
|
|
@ -35,10 +35,10 @@ Optional<URL::URL> sanitize_url(StringView location, Optional<SearchEngine> cons
|
||||||
|
|
||||||
auto url = URL::create_with_url_or_path(location);
|
auto url = URL::create_with_url_or_path(location);
|
||||||
|
|
||||||
if (!url.is_valid()) {
|
if (!url.has_value()) {
|
||||||
url = URL::create_with_url_or_path(ByteString::formatted("https://{}", location));
|
url = URL::create_with_url_or_path(ByteString::formatted("https://{}", location));
|
||||||
|
|
||||||
if (!url.is_valid())
|
if (!url.has_value())
|
||||||
return search_url_or_error();
|
return search_url_or_error();
|
||||||
|
|
||||||
https_scheme_was_guessed = true;
|
https_scheme_was_guessed = true;
|
||||||
|
@ -140,9 +140,10 @@ static URLParts break_web_url_into_parts(URL::URL const& url, StringView url_str
|
||||||
|
|
||||||
Optional<URLParts> break_url_into_parts(StringView url_string)
|
Optional<URLParts> break_url_into_parts(StringView url_string)
|
||||||
{
|
{
|
||||||
auto url = URL::create_with_url_or_path(url_string);
|
auto maybe_url = URL::create_with_url_or_path(url_string);
|
||||||
if (!url.is_valid())
|
if (!maybe_url.has_value())
|
||||||
return {};
|
return {};
|
||||||
|
auto const& url = maybe_url.value();
|
||||||
|
|
||||||
auto const& scheme = url.scheme();
|
auto const& scheme = url.scheme();
|
||||||
auto scheme_length = scheme.bytes_as_string_view().length();
|
auto scheme_length = scheme.bytes_as_string_view().length();
|
||||||
|
|
|
@ -270,8 +270,9 @@ TEST_CASE(equality)
|
||||||
|
|
||||||
TEST_CASE(create_with_file_scheme)
|
TEST_CASE(create_with_file_scheme)
|
||||||
{
|
{
|
||||||
auto url = URL::create_with_file_scheme("/home/anon/README.md");
|
auto maybe_url = URL::create_with_file_scheme("/home/anon/README.md");
|
||||||
EXPECT(url.is_valid());
|
EXPECT(maybe_url.has_value());
|
||||||
|
auto url = maybe_url.release_value();
|
||||||
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.path_segment_count(), 3u);
|
EXPECT_EQ(url.path_segment_count(), 3u);
|
||||||
|
@ -282,8 +283,9 @@ TEST_CASE(create_with_file_scheme)
|
||||||
EXPECT(!url.query().has_value());
|
EXPECT(!url.query().has_value());
|
||||||
EXPECT(!url.fragment().has_value());
|
EXPECT(!url.fragment().has_value());
|
||||||
|
|
||||||
url = URL::create_with_file_scheme("/home/anon/");
|
maybe_url = URL::create_with_file_scheme("/home/anon/");
|
||||||
EXPECT(url.is_valid());
|
EXPECT(maybe_url.has_value());
|
||||||
|
url = maybe_url.release_value();
|
||||||
EXPECT_EQ(url.path_segment_count(), 3u);
|
EXPECT_EQ(url.path_segment_count(), 3u);
|
||||||
EXPECT_EQ(url.path_segment_at_index(0), "home");
|
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(1), "anon");
|
||||||
|
|
|
@ -119,8 +119,8 @@ Web::DragEvent ns_event_to_drag_event(Web::DragEvent::Type type, id<NSDraggingIn
|
||||||
Vector<URL::URL> urls;
|
Vector<URL::URL> urls;
|
||||||
|
|
||||||
for_each_file([&](ByteString const& file_path) {
|
for_each_file([&](ByteString const& file_path) {
|
||||||
if (auto url = URL::create_with_url_or_path(file_path); url.is_valid())
|
if (auto url = URL::create_with_url_or_path(file_path); url.has_value())
|
||||||
urls.append(move(url));
|
urls.append(url.release_value());
|
||||||
});
|
});
|
||||||
|
|
||||||
browser_data = make<DragData>(move(urls));
|
browser_data = make<DragData>(move(urls));
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue