From 7a2da4cabf5627941cf1fc6eb7c5fd068fcc75f9 Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Tue, 20 Sep 2022 18:23:04 +0100 Subject: Browser+WebContent+LibJS: Support `%c` specifiers in Console.log() ...and the other Console methods. This lets you apply styling to a log message or any other text that passes through the Console `Formatter` operation. We store the CSS on the ConsoleClient instead of passing it along with the rest of the message, since I couldn't figure out a nice way of doing that, as Formatter has to return JS::Values. This way isn't nice, and has a risk of forgetting to clear the style and having it apply to subsequent messages, but it works. This is only supported in the Browser for now. REPL support would require parsing the CSS and figuring out the relevant ANSI codes. We also don't filter this styling at all, so you can `position: absolute` and `transform: translate(...)` all you want, which is less than ideal. --- .../Services/WebContent/WebContentConsoleClient.cpp | 19 +++++++++++-------- .../Services/WebContent/WebContentConsoleClient.h | 8 ++++++++ 2 files changed, 19 insertions(+), 8 deletions(-) (limited to 'Userland/Services/WebContent') diff --git a/Userland/Services/WebContent/WebContentConsoleClient.cpp b/Userland/Services/WebContent/WebContentConsoleClient.cpp index 2724b38f2e..6b2d96a0fa 100644 --- a/Userland/Services/WebContent/WebContentConsoleClient.cpp +++ b/Userland/Services/WebContent/WebContentConsoleClient.cpp @@ -143,11 +143,14 @@ void WebContentConsoleClient::clear() // 2.3. Printer(logLevel, args[, options]), https://console.spec.whatwg.org/#printer JS::ThrowCompletionOr WebContentConsoleClient::printer(JS::Console::LogLevel log_level, PrinterArguments arguments) { + auto styling = escape_html_entities(m_current_message_style.string_view()); + m_current_message_style.clear(); + if (log_level == JS::Console::LogLevel::Trace) { auto trace = arguments.get(); StringBuilder html; if (!trace.label.is_empty()) - html.appendff("{}
", escape_html_entities(trace.label)); + html.appendff("{}
", styling, escape_html_entities(trace.label)); html.append(""sv); for (auto& function_name : trace.stack) @@ -160,7 +163,7 @@ JS::ThrowCompletionOr WebContentConsoleClient::printer(JS::Console::L if (log_level == JS::Console::LogLevel::Group || log_level == JS::Console::LogLevel::GroupCollapsed) { auto group = arguments.get(); - begin_group(group.label, log_level == JS::Console::LogLevel::Group); + begin_group(String::formatted("{}", styling, escape_html_entities(group.label)), log_level == JS::Console::LogLevel::Group); return JS::js_undefined(); } @@ -170,23 +173,23 @@ JS::ThrowCompletionOr WebContentConsoleClient::printer(JS::Console::L StringBuilder html; switch (log_level) { case JS::Console::LogLevel::Debug: - html.append("(d) "sv); + html.appendff("(d) "sv, styling); break; case JS::Console::LogLevel::Error: - html.append("(e) "sv); + html.appendff("(e) "sv, styling); break; case JS::Console::LogLevel::Info: - html.append("(i) "sv); + html.appendff("(i) "sv, styling); break; case JS::Console::LogLevel::Log: - html.append(" "sv); + html.appendff(" "sv, styling); break; case JS::Console::LogLevel::Warn: case JS::Console::LogLevel::CountReset: - html.append("(w) "sv); + html.appendff("(w) "sv, styling); break; default: - html.append(""sv); + html.appendff(""sv, styling); break; } diff --git a/Userland/Services/WebContent/WebContentConsoleClient.h b/Userland/Services/WebContent/WebContentConsoleClient.h index a8dfc28a16..c234bb779d 100644 --- a/Userland/Services/WebContent/WebContentConsoleClient.h +++ b/Userland/Services/WebContent/WebContentConsoleClient.h @@ -27,6 +27,12 @@ private: virtual void clear() override; virtual JS::ThrowCompletionOr printer(JS::Console::LogLevel log_level, PrinterArguments) override; + virtual void add_css_style_to_current_message(StringView style) override + { + m_current_message_style.append(style); + m_current_message_style.append(';'); + } + ConnectionFromClient& m_client; WeakPtr m_realm; JS::Handle m_console_global_object; @@ -48,6 +54,8 @@ private: String data; }; Vector m_message_log; + + StringBuilder m_current_message_style; }; } -- cgit v1.2.3