From 2827374edca89dc44a13ca8b42dcbfe0410ca602 Mon Sep 17 00:00:00 2001 From: Shannon Booth Date: Sat, 22 Feb 2025 21:02:32 +1300 Subject: [PATCH] LibWebView: Handle missing URL for view source Instead of potentially passing through an invalid URL. --- Libraries/LibWebView/InspectorClient.cpp | 5 +++-- Libraries/LibWebView/SourceHighlighter.cpp | 10 +++++++--- Libraries/LibWebView/SourceHighlighter.h | 4 ++-- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/Libraries/LibWebView/InspectorClient.cpp b/Libraries/LibWebView/InspectorClient.cpp index 2895a8fa3c2..1eb52698ee7 100644 --- a/Libraries/LibWebView/InspectorClient.cpp +++ b/Libraries/LibWebView/InspectorClient.cpp @@ -15,6 +15,7 @@ #include #include #include +#include #include #include #include @@ -97,8 +98,8 @@ InspectorClient::InspectorClient(ViewImplementation& content_web_view, ViewImple m_inspector_web_view.run_javascript(MUST(builder.to_string())); }; - m_content_web_view.on_received_style_sheet_source = [this](Web::CSS::StyleSheetIdentifier const& identifier, auto const& base_url, String const& source) { - auto html = highlight_source(identifier.url.value_or({}), base_url, source, Syntax::Language::CSS, HighlightOutputMode::SourceOnly); + m_content_web_view.on_received_style_sheet_source = [this](Web::CSS::StyleSheetIdentifier const& identifier, URL::URL const& base_url, String const& source) { + auto html = highlight_source(URL::Parser::basic_parse(identifier.url.value_or({})), base_url, source, Syntax::Language::CSS, HighlightOutputMode::SourceOnly); auto script = MUST(String::formatted("inspector.setStyleSheetSource({}, \"{}\");", style_sheet_identifier_to_json(identifier), MUST(encode_base64(html.bytes())))); diff --git a/Libraries/LibWebView/SourceHighlighter.cpp b/Libraries/LibWebView/SourceHighlighter.cpp index 570ec4d4330..1f24dc7c226 100644 --- a/Libraries/LibWebView/SourceHighlighter.cpp +++ b/Libraries/LibWebView/SourceHighlighter.cpp @@ -147,7 +147,7 @@ void SourceHighlighterClient::highlighter_did_set_folding_regions(Vector const& url, URL::URL const& base_url, String const& source, Syntax::Language language, HighlightOutputMode mode) { SourceHighlighterClient highlighter_client { source, language }; return highlighter_client.to_html_string(url, base_url, mode); @@ -268,7 +268,7 @@ StringView SourceHighlighterClient::class_for_token(u64 token_type) const } } -String SourceHighlighterClient::to_html_string(URL::URL const& url, URL::URL const& base_url, HighlightOutputMode mode) const +String SourceHighlighterClient::to_html_string(Optional const& url, URL::URL const& base_url, HighlightOutputMode mode) const { StringBuilder builder; @@ -302,7 +302,11 @@ String SourceHighlighterClient::to_html_string(URL::URL const& url, URL::URL con )~~~"sv); - builder.appendff("View Source - {}", escape_html_entities(url.serialize_for_display())); + if (url.has_value()) + builder.appendff("View Source - {}", escape_html_entities(url->serialize_for_display())); + else + builder.append("View Source"sv); + builder.appendff("", HTML_HIGHLIGHTER_STYLE); builder.append(R"~~~( diff --git a/Libraries/LibWebView/SourceHighlighter.h b/Libraries/LibWebView/SourceHighlighter.h index 6069c87b906..603913cec8b 100644 --- a/Libraries/LibWebView/SourceHighlighter.h +++ b/Libraries/LibWebView/SourceHighlighter.h @@ -52,7 +52,7 @@ public: SourceHighlighterClient(String const& source, Syntax::Language); virtual ~SourceHighlighterClient() = default; - String to_html_string(URL::URL const& url, URL::URL const& base_url, HighlightOutputMode) const; + String to_html_string(Optional const&, URL::URL const& base_url, HighlightOutputMode) const; private: // ^ Syntax::HighlighterClient @@ -75,7 +75,7 @@ private: OwnPtr m_highlighter; }; -String highlight_source(URL::URL const& url, URL::URL const& base_url, String const& source, Syntax::Language, HighlightOutputMode); +String highlight_source(Optional const&, URL::URL const& base_url, String const& source, Syntax::Language, HighlightOutputMode); constexpr inline StringView HTML_HIGHLIGHTER_STYLE = R"~~~( @media (prefers-color-scheme: dark) {