LibWebView+UI: Highlight CSS in the style sheet inspector

This commit is contained in:
Sam Atkins 2024-09-25 11:46:31 +01:00 committed by Sam Atkins
parent af23f3890b
commit f0dd0c5107
Notes: github-actions[bot] 2024-09-30 07:54:15 +00:00
5 changed files with 23 additions and 15 deletions

View File

@ -1049,7 +1049,7 @@ static void copy_data_to_clipboard(StringView data, NSPasteboardType pasteboard_
if (self == nil) {
return;
}
auto html = WebView::highlight_source(url, source);
auto html = WebView::highlight_source(MUST(url.to_string()), source, Syntax::Language::HTML, WebView::HighlightOutputMode::FullDocument);
[self.observer onCreateNewTab:html
url:url

View File

@ -322,7 +322,7 @@ Tab::Tab(BrowserWindow* window, RefPtr<WebView::WebContentClient> parent_client,
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(url, source);
auto html = WebView::highlight_source(MUST(url.to_string()), source, Syntax::Language::HTML, WebView::HighlightOutputMode::FullDocument);
m_window->new_tab_from_content(html, Web::HTML::ActivateTab::Yes);
};

View File

@ -129,11 +129,10 @@ InspectorClient::InspectorClient(ViewImplementation& content_web_view, ViewImple
};
m_content_web_view.on_received_style_sheet_source = [this](Web::CSS::StyleSheetIdentifier const& identifier, String const& source) {
// TODO: Highlight it
auto escaped_source = escape_html_entities(source.bytes()).replace("\t"sv, " "sv, ReplaceMode::All);
auto html = highlight_source(identifier.url.value_or({}), source, Syntax::Language::CSS, HighlightOutputMode::SourceOnly);
auto script = MUST(String::formatted("inspector.setStyleSheetSource({}, \"{}\");",
style_sheet_identifier_to_json(identifier),
MUST(encode_base64(escaped_source.bytes()))));
MUST(encode_base64(html.bytes()))));
m_inspector_web_view.run_javascript(script);
};

View File

@ -113,10 +113,10 @@ void SourceHighlighterClient::highlighter_did_set_folding_regions(Vector<Syntax:
document().set_folding_regions(move(folding_regions));
}
String highlight_source(URL::URL const& url, StringView source)
String highlight_source(String const& url, StringView source, Syntax::Language language, HighlightOutputMode mode)
{
SourceHighlighterClient highlighter_client { source, Syntax::Language::HTML };
return highlighter_client.to_html_string(url);
SourceHighlighterClient highlighter_client { source, language };
return highlighter_client.to_html_string(url, mode);
}
StringView SourceHighlighterClient::class_for_token(u64 token_type) const
@ -259,7 +259,8 @@ String SourceHighlighterClient::to_html_string(String const& url, HighlightOutpu
builder.append("</span>"sv);
};
builder.append(R"~~~(
if (mode == HighlightOutputMode::FullDocument) {
builder.append(R"~~~(
<!DOCTYPE html>
<html>
<head>
@ -269,8 +270,9 @@ String SourceHighlighterClient::to_html_string(String const& url, HighlightOutpu
builder.appendff("<style type=\"text/css\">{}</style>", HTML_HIGHLIGHTER_STYLE);
builder.append(R"~~~(
</head>
<body>
<pre class="html">)~~~"sv);
<body>)~~~"sv);
}
builder.append("<pre class=\"html\">"sv);
size_t span_index = 0;
for (size_t line_index = 0; line_index < document().line_count(); ++line_index) {
@ -337,11 +339,13 @@ String SourceHighlighterClient::to_html_string(String const& url, HighlightOutpu
builder.append("</div>"sv);
}
builder.append(R"~~~(
</pre>
builder.append("</pre>"sv);
if (mode == HighlightOutputMode::FullDocument) {
builder.append(R"~~~(
</body>
</html>
)~~~"sv);
}
return builder.to_string_without_validation();
}

View File

@ -15,6 +15,11 @@
namespace WebView {
enum class HighlightOutputMode {
FullDocument, // Include HTML header, title, style sheet, etc
SourceOnly, // Just the highlighted source
};
class SourceDocument final : public Syntax::Document {
public:
static NonnullRefPtr<SourceDocument> create(StringView source)
@ -45,7 +50,7 @@ public:
SourceHighlighterClient(StringView source, Syntax::Language);
virtual ~SourceHighlighterClient() = default;
String to_html_string(URL::URL const&) const;
String to_html_string(String const&, HighlightOutputMode) const;
private:
// ^ Syntax::HighlighterClient
@ -68,7 +73,7 @@ private:
OwnPtr<Syntax::Highlighter> m_highlighter;
};
String highlight_source(URL::URL const&, StringView);
String highlight_source(String const&, StringView, Syntax::Language, HighlightOutputMode);
constexpr inline StringView HTML_HIGHLIGHTER_STYLE = R"~~~(
@media (prefers-color-scheme: dark) {