LibURL: Port create_with_file_scheme to Optional

Removing one of the main remaining users of URL valid state.
This commit is contained in:
Shannon Booth 2025-04-19 16:43:17 +12:00 committed by Tim Flynn
parent 2072eee83d
commit 00bbb2105b
Notes: github-actions[bot] 2025-04-19 11:20:14 +00:00
5 changed files with 18 additions and 16 deletions

View file

@ -163,7 +163,7 @@ Optional<u16> default_port_for_scheme(StringView scheme)
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);
if (!lexical_path.is_absolute())
@ -180,11 +180,10 @@ URL create_with_file_scheme(ByteString const& path, ByteString const& fragment,
url_builder.append(fragment);
}
auto url = Parser::basic_parse(url_builder.string_view());
return url.has_value() ? url.release_value() : URL {};
return Parser::basic_parse(url_builder.string_view());
}
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);
if (url.has_value())

View file

@ -203,8 +203,8 @@ private:
AK::CopyOnWrite<Data> m_data;
};
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_url_or_path(ByteString const&);
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);
bool is_public_suffix(StringView host);

View file

@ -35,10 +35,10 @@ Optional<URL::URL> sanitize_url(StringView location, Optional<SearchEngine> cons
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));
if (!url.is_valid())
if (!url.has_value())
return search_url_or_error();
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)
{
auto url = URL::create_with_url_or_path(url_string);
if (!url.is_valid())
auto maybe_url = URL::create_with_url_or_path(url_string);
if (!maybe_url.has_value())
return {};
auto const& url = maybe_url.value();
auto const& scheme = url.scheme();
auto scheme_length = scheme.bytes_as_string_view().length();

View file

@ -270,8 +270,9 @@ TEST_CASE(equality)
TEST_CASE(create_with_file_scheme)
{
auto url = URL::create_with_file_scheme("/home/anon/README.md");
EXPECT(url.is_valid());
auto maybe_url = URL::create_with_file_scheme("/home/anon/README.md");
EXPECT(maybe_url.has_value());
auto url = maybe_url.release_value();
EXPECT_EQ(url.scheme(), "file");
EXPECT_EQ(url.port_or_default(), 0);
EXPECT_EQ(url.path_segment_count(), 3u);
@ -282,8 +283,9 @@ TEST_CASE(create_with_file_scheme)
EXPECT(!url.query().has_value());
EXPECT(!url.fragment().has_value());
url = URL::create_with_file_scheme("/home/anon/");
EXPECT(url.is_valid());
maybe_url = URL::create_with_file_scheme("/home/anon/");
EXPECT(maybe_url.has_value());
url = maybe_url.release_value();
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");

View file

@ -119,8 +119,8 @@ Web::DragEvent ns_event_to_drag_event(Web::DragEvent::Type type, id<NSDraggingIn
Vector<URL::URL> urls;
for_each_file([&](ByteString const& file_path) {
if (auto url = URL::create_with_url_or_path(file_path); url.is_valid())
urls.append(move(url));
if (auto url = URL::create_with_url_or_path(file_path); url.has_value())
urls.append(url.release_value());
});
browser_data = make<DragData>(move(urls));