From 00bbb2105b3d510ec828294f636dfaea9b852ad3 Mon Sep 17 00:00:00 2001 From: Shannon Booth Date: Sat, 19 Apr 2025 16:43:17 +1200 Subject: [PATCH] LibURL: Port create_with_file_scheme to Optional Removing one of the main remaining users of URL valid state. --- Libraries/LibURL/URL.cpp | 7 +++---- Libraries/LibURL/URL.h | 4 ++-- Libraries/LibWebView/URL.cpp | 9 +++++---- Tests/LibURL/TestURL.cpp | 10 ++++++---- UI/AppKit/Interface/Event.mm | 4 ++-- 5 files changed, 18 insertions(+), 16 deletions(-) diff --git a/Libraries/LibURL/URL.cpp b/Libraries/LibURL/URL.cpp index 8a087cdaaf4..d49a58491ce 100644 --- a/Libraries/LibURL/URL.cpp +++ b/Libraries/LibURL/URL.cpp @@ -163,7 +163,7 @@ Optional default_port_for_scheme(StringView scheme) return {}; } -URL create_with_file_scheme(ByteString const& path, ByteString const& fragment, ByteString const& hostname) +Optional 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 create_with_url_or_path(ByteString const& url_or_path) { auto url = Parser::basic_parse(url_or_path); if (url.has_value()) diff --git a/Libraries/LibURL/URL.h b/Libraries/LibURL/URL.h index 88ec9f965a8..40ae8637ad7 100644 --- a/Libraries/LibURL/URL.h +++ b/Libraries/LibURL/URL.h @@ -203,8 +203,8 @@ private: AK::CopyOnWrite 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 create_with_url_or_path(ByteString const&); +Optional 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); diff --git a/Libraries/LibWebView/URL.cpp b/Libraries/LibWebView/URL.cpp index 39e314a8f0c..1d203d87556 100644 --- a/Libraries/LibWebView/URL.cpp +++ b/Libraries/LibWebView/URL.cpp @@ -35,10 +35,10 @@ Optional sanitize_url(StringView location, Optional 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 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(); diff --git a/Tests/LibURL/TestURL.cpp b/Tests/LibURL/TestURL.cpp index c0178c15098..81d1279c3ef 100644 --- a/Tests/LibURL/TestURL.cpp +++ b/Tests/LibURL/TestURL.cpp @@ -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"); diff --git a/UI/AppKit/Interface/Event.mm b/UI/AppKit/Interface/Event.mm index 92d2b0769f5..2c2b3bb820c 100644 --- a/UI/AppKit/Interface/Event.mm +++ b/UI/AppKit/Interface/Event.mm @@ -119,8 +119,8 @@ Web::DragEvent ns_event_to_drag_event(Web::DragEvent::Type type, id 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(move(urls));