Skip to content

Commit

Permalink
LibWebView: Generate hyperlinks for attributes that represent links
Browse files Browse the repository at this point in the history
On the view-source page, generate anchor tags for any 'href' or 'src'
attribute value we come across. This handles both when the attribute
contains an absolute URL and a URL relative to the page.

This requires sending the document's base URL over IPC to resolve
relative URLs.

(cherry picked from commit 1aab7b51ea9c27a6ffff8df0c8bcbec87680865c)
  • Loading branch information
trflynn89 authored and nico committed Nov 16, 2024
1 parent d53ba7a commit 82ea153
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 26 deletions.
4 changes: 2 additions & 2 deletions Ladybird/AppKit/UI/LadybirdWebView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -1063,12 +1063,12 @@ - (void)setWebViewCallbacks
return Ladybird::ns_rect_to_gfx_rect([[self window] frame]);
};

m_web_view_bridge->on_received_source = [weak_self](auto const& url, auto const& source) {
m_web_view_bridge->on_received_source = [weak_self](auto const& url, auto const& base_url, auto const& source) {
LadybirdWebView* self = weak_self;
if (self == nil) {
return;
}
auto html = WebView::highlight_source(MUST(url.to_string()), source, Syntax::Language::HTML, WebView::HighlightOutputMode::FullDocument);
auto html = WebView::highlight_source(url, base_url, source, Syntax::Language::HTML, WebView::HighlightOutputMode::FullDocument);

[self.observer onCreateNewTab:html
url:url
Expand Down
4 changes: 2 additions & 2 deletions Ladybird/Qt/Tab.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -321,8 +321,8 @@ Tab::Tab(BrowserWindow* window, WebContentOptions const& web_content_options, St

QObject::connect(focus_location_editor_action, &QAction::triggered, this, &Tab::focus_location_editor);

view().on_received_source = [this](auto const& url, auto const& source) {
auto html = WebView::highlight_source(MUST(url.to_string()), source, Syntax::Language::HTML, WebView::HighlightOutputMode::FullDocument);
view().on_received_source = [this](auto const& url, auto const& base_url, auto const& source) {
auto html = WebView::highlight_source(url, base_url, source, Syntax::Language::HTML, WebView::HighlightOutputMode::FullDocument);
m_window->new_tab_from_content(html, Web::HTML::ActivateTab::Yes);
};

Expand Down
4 changes: 2 additions & 2 deletions Userland/Libraries/LibWebView/InspectorClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ InspectorClient::InspectorClient(ViewImplementation& content_web_view, ViewImple
m_inspector_web_view.run_javascript(builder.string_view());
};

m_content_web_view.on_received_style_sheet_source = [this](Web::CSS::StyleSheetIdentifier const& identifier, String const& source) {
auto html = highlight_source(identifier.url.value_or({}), source, Syntax::Language::CSS, HighlightOutputMode::SourceOnly);
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);
auto script = MUST(String::formatted("inspector.setStyleSheetSource({}, \"{}\");",
style_sheet_identifier_to_json(identifier),
MUST(encode_base64(html.bytes()))));
Expand Down
41 changes: 37 additions & 4 deletions Userland/Libraries/LibWebView/SourceHighlighter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <LibURL/URL.h>
#include <LibWeb/CSS/Parser/Token.h>
#include <LibWeb/CSS/SyntaxHighlighter/SyntaxHighlighter.h>
#include <LibWeb/DOMURL/DOMURL.h>
#include <LibWeb/HTML/SyntaxHighlighter/SyntaxHighlighter.h>
#include <LibWebView/SourceHighlighter.h>

Expand Down Expand Up @@ -113,10 +114,10 @@ void SourceHighlighterClient::highlighter_did_set_folding_regions(Vector<Syntax:
document().set_folding_regions(move(folding_regions));
}

String highlight_source(String const& url, StringView source, Syntax::Language language, HighlightOutputMode mode)
String highlight_source(URL::URL const& url, URL::URL const& base_url, StringView source, Syntax::Language language, HighlightOutputMode mode)
{
SourceHighlighterClient highlighter_client { source, language };
return highlighter_client.to_html_string(url, mode);
return highlighter_client.to_html_string(url, base_url, mode);
}

StringView SourceHighlighterClient::class_for_token(u64 token_type) const
Expand Down Expand Up @@ -232,7 +233,7 @@ StringView SourceHighlighterClient::class_for_token(u64 token_type) const
}
}

String SourceHighlighterClient::to_html_string(String const& url, HighlightOutputMode mode) const
String SourceHighlighterClient::to_html_string(URL::URL const& url, URL::URL const& base_url, HighlightOutputMode mode) const
{
StringBuilder builder;

Expand Down Expand Up @@ -266,14 +267,30 @@ String SourceHighlighterClient::to_html_string(String const& url, HighlightOutpu
<head>
<meta name="color-scheme" content="dark light">)~~~"sv);

builder.appendff("<title>View Source - {}</title>", escape_html_entities(url));
builder.appendff("<title>View Source - {}</title>", escape_html_entities(url.serialize_for_display()));
builder.appendff("<style type=\"text/css\">{}</style>", HTML_HIGHLIGHTER_STYLE);
builder.append(R"~~~(
</head>
<body>)~~~"sv);
}
builder.append("<pre class=\"html\">"sv);

static constexpr auto href = to_array<u32>({ 'h', 'r', 'e', 'f' });
static constexpr auto src = to_array<u32>({ 's', 'r', 'c' });
bool linkify_attribute = false;

auto resolve_url_for_attribute = [&](Utf32View const& attribute_value) -> Optional<URL::URL> {
if (!linkify_attribute)
return {};

auto attribute_url = MUST(String::formatted("{}", attribute_value));
auto attribute_url_without_quotes = attribute_url.bytes_as_string_view().trim("\""sv);

if (auto resolved = Web::DOMURL::parse(attribute_url_without_quotes, base_url); resolved.is_valid())
return resolved;
return {};
};

size_t span_index = 0;
for (size_t line_index = 0; line_index < document().line_count(); ++line_index) {
auto& line = document().line(line_index);
Expand All @@ -286,11 +303,27 @@ String SourceHighlighterClient::to_html_string(String const& url, HighlightOutpu
size_t length = end - start;
if (length == 0)
return;

auto text = line_view.substring_view(start, length);

if (span.has_value()) {
bool append_anchor_close = false;

if (span->data == to_underlying(Web::HTML::AugmentedTokenKind::AttributeName)) {
linkify_attribute = text == Utf32View { href } || text == Utf32View { src };
} else if (span->data == to_underlying(Web::HTML::AugmentedTokenKind::AttributeValue)) {
if (auto href = resolve_url_for_attribute(text); href.has_value()) {
builder.appendff("<a href=\"{}\">", *href);
append_anchor_close = true;
}
}

start_token(span->data);
append_escaped(text);
end_token();

if (append_anchor_close)
builder.append("</a>"sv);
} else {
append_escaped(text);
}
Expand Down
6 changes: 4 additions & 2 deletions Userland/Libraries/LibWebView/SourceHighlighter.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@

#pragma once

#include <AK/OwnPtr.h>
#include <AK/String.h>
#include <AK/StringView.h>
#include <LibSyntax/Document.h>
#include <LibSyntax/HighlighterClient.h>
#include <LibSyntax/Language.h>
#include <LibURL/Forward.h>

namespace WebView {

Expand Down Expand Up @@ -50,7 +52,7 @@ class SourceHighlighterClient final : public Syntax::HighlighterClient {
SourceHighlighterClient(StringView source, Syntax::Language);
virtual ~SourceHighlighterClient() = default;

String to_html_string(String const&, HighlightOutputMode) const;
String to_html_string(URL::URL const& url, URL::URL const& base_url, HighlightOutputMode) const;

private:
// ^ Syntax::HighlighterClient
Expand All @@ -73,7 +75,7 @@ class SourceHighlighterClient final : public Syntax::HighlighterClient {
OwnPtr<Syntax::Highlighter> m_highlighter;
};

String highlight_source(String const&, StringView, Syntax::Language, HighlightOutputMode);
String highlight_source(URL::URL const& url, URL::URL const& base_url, StringView, Syntax::Language, HighlightOutputMode);

constexpr inline StringView HTML_HIGHLIGHTER_STYLE = R"~~~(
@media (prefers-color-scheme: dark) {
Expand Down
4 changes: 2 additions & 2 deletions Userland/Libraries/LibWebView/ViewImplementation.h
Original file line number Diff line number Diff line change
Expand Up @@ -180,13 +180,13 @@ class ViewImplementation {
Function<void(String const& message)> on_request_set_prompt_text;
Function<void()> on_request_accept_dialog;
Function<void()> on_request_dismiss_dialog;
Function<void(URL::URL const&, ByteString const&)> on_received_source;
Function<void(URL::URL const&, URL::URL const&, String const&)> on_received_source;
Function<void(ByteString const&)> on_received_dom_tree;
Function<void(Optional<DOMNodeProperties>)> on_received_dom_node_properties;
Function<void(ByteString const&)> on_received_accessibility_tree;
Function<void(Vector<Web::CSS::StyleSheetIdentifier>)> on_received_style_sheet_list;
Function<void(Web::CSS::StyleSheetIdentifier const&)> on_inspector_requested_style_sheet_source;
Function<void(Web::CSS::StyleSheetIdentifier const&, String const&)> on_received_style_sheet_source;
Function<void(Web::CSS::StyleSheetIdentifier const&, URL::URL const&, String const&)> on_received_style_sheet_source;
Function<void(i32 node_id)> on_received_hovered_node_id;
Function<void(Optional<i32> const& node_id)> on_finshed_editing_dom_node;
Function<void(String const&)> on_received_dom_node_html;
Expand Down
8 changes: 4 additions & 4 deletions Userland/Libraries/LibWebView/WebContentClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -253,11 +253,11 @@ void WebContentClient::did_request_media_context_menu(u64 page_id, Gfx::IntPoint
}
}

void WebContentClient::did_get_source(u64 page_id, URL::URL const& url, ByteString const& source)
void WebContentClient::did_get_source(u64 page_id, URL::URL const& url, URL::URL const& base_url, String const& source)
{
if (auto view = view_for_page_id(page_id); view.has_value()) {
if (view->on_received_source)
view->on_received_source(url, source);
view->on_received_source(url, base_url, source);
}
}

Expand Down Expand Up @@ -714,11 +714,11 @@ void WebContentClient::inspector_did_request_style_sheet_source(u64 page_id, Web
}
}

void WebContentClient::did_request_style_sheet_source(u64 page_id, Web::CSS::StyleSheetIdentifier const& identifier, String const& source)
void WebContentClient::did_get_style_sheet_source(u64 page_id, Web::CSS::StyleSheetIdentifier const& identifier, URL::URL const& base_url, String const& source)
{
if (auto view = view_for_page_id(page_id); view.has_value()) {
if (view->on_received_style_sheet_source)
view->on_received_style_sheet_source(identifier, source);
view->on_received_style_sheet_source(identifier, base_url, source);
}
}

Expand Down
4 changes: 2 additions & 2 deletions Userland/Libraries/LibWebView/WebContentClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class WebContentClient final
virtual void did_request_link_context_menu(u64 page_id, Gfx::IntPoint, URL::URL const&, ByteString const&, unsigned) override;
virtual void did_request_image_context_menu(u64 page_id, Gfx::IntPoint, URL::URL const&, ByteString const&, unsigned, Gfx::ShareableBitmap const&) override;
virtual void did_request_media_context_menu(u64 page_id, Gfx::IntPoint, ByteString const&, unsigned, Web::Page::MediaContextMenu const&) override;
virtual void did_get_source(u64 page_id, URL::URL const&, ByteString const&) override;
virtual void did_get_source(u64 page_id, URL::URL const&, URL::URL const&, String const&) override;
virtual void did_inspect_dom_tree(u64 page_id, ByteString const&) override;
virtual void did_inspect_dom_node(u64 page_id, bool has_style, ByteString const& computed_style, ByteString const& resolved_style, ByteString const& custom_properties, ByteString const& node_box_sizing, ByteString const& aria_properties_state, ByteString const& fonts) override;
virtual void did_inspect_accessibility_tree(u64 page_id, ByteString const&) override;
Expand Down Expand Up @@ -117,7 +117,7 @@ class WebContentClient final
virtual Messages::WebContentClient::RequestWorkerAgentResponse request_worker_agent(u64 page_id) override;
virtual void inspector_did_list_style_sheets(u64 page_id, Vector<Web::CSS::StyleSheetIdentifier> const& stylesheets) override;
virtual void inspector_did_request_style_sheet_source(u64 page_id, Web::CSS::StyleSheetIdentifier const& identifier) override;
virtual void did_request_style_sheet_source(u64 page_id, Web::CSS::StyleSheetIdentifier const& identifier, String const& source) override;
virtual void did_get_style_sheet_source(u64 page_id, Web::CSS::StyleSheetIdentifier const& identifier, URL::URL const&, String const& source) override;

Optional<ViewImplementation&> view_for_page_id(u64, SourceLocation = SourceLocation::current());

Expand Down
7 changes: 3 additions & 4 deletions Userland/Services/WebContent/ConnectionFromClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ void ConnectionFromClient::get_source(u64 page_id)
{
if (auto page = this->page(page_id); page.has_value()) {
if (auto* doc = page->page().top_level_browsing_context().active_document())
async_did_get_source(page_id, doc->url(), doc->source().to_byte_string());
async_did_get_source(page_id, doc->url(), doc->base_url(), doc->source());
}
}

Expand Down Expand Up @@ -673,9 +673,8 @@ void ConnectionFromClient::request_style_sheet_source(u64 page_id, Web::CSS::Sty
return;

if (auto* document = page->page().top_level_browsing_context().active_document()) {
auto stylesheet = document->get_style_sheet_source(identifier);
if (stylesheet.has_value())
async_did_request_style_sheet_source(page_id, identifier, stylesheet.value());
if (auto stylesheet = document->get_style_sheet_source(identifier); stylesheet.has_value())
async_did_get_style_sheet_source(page_id, identifier, document->base_url(), stylesheet.value());
}
}

Expand Down
4 changes: 2 additions & 2 deletions Userland/Services/WebContent/WebContentClient.ipc
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ endpoint WebContentClient
did_request_set_prompt_text(u64 page_id, String message) =|
did_request_accept_dialog(u64 page_id) =|
did_request_dismiss_dialog(u64 page_id) =|
did_get_source(u64 page_id, URL::URL url, ByteString source) =|
did_get_source(u64 page_id, URL::URL url, URL::URL base_url, String source) =|

did_inspect_dom_tree(u64 page_id, ByteString dom_tree) =|
did_inspect_dom_node(u64 page_id, bool has_style, ByteString computed_style, ByteString resolved_style, ByteString custom_properties, ByteString node_box_sizing, ByteString aria_properties_state, ByteString fonts) =|
Expand All @@ -61,7 +61,7 @@ endpoint WebContentClient

inspector_did_list_style_sheets(u64 page_id, Vector<Web::CSS::StyleSheetIdentifier> style_sheets) =|
inspector_did_request_style_sheet_source(u64 page_id, Web::CSS::StyleSheetIdentifier identifier) =|
did_request_style_sheet_source(u64 page_id, Web::CSS::StyleSheetIdentifier identifier, String source) =|
did_get_style_sheet_source(u64 page_id, Web::CSS::StyleSheetIdentifier identifier, URL::URL base_url, String source) =|

did_take_screenshot(u64 page_id, Gfx::ShareableBitmap screenshot) =|

Expand Down

0 comments on commit 82ea153

Please sign in to comment.