From acec34351ff7ce6169e4362b1e7749c2cd80567e Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Wed, 5 Oct 2022 12:24:08 +0100 Subject: [PATCH] Ladybird/ConsoleClient: Implement console message styling with `%c` This matches the changes made to Serenity in: https://github.com/SerenityOS/serenity/commit/7a2da4cabf5627941cf1fc6eb7c5fd068fcc75f9 --- Ladybird/ConsoleClient.cpp | 19 +++++++++++-------- Ladybird/ConsoleClient.h | 10 +++++++++- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/Ladybird/ConsoleClient.cpp b/Ladybird/ConsoleClient.cpp index e41cac1ca7c..fe5d8620de5 100644 --- a/Ladybird/ConsoleClient.cpp +++ b/Ladybird/ConsoleClient.cpp @@ -1,7 +1,7 @@ /* * Copyright (c) 2021, Brandon Scott * Copyright (c) 2020, Hunter Salyer - * Copyright (c) 2021, Sam Atkins + * Copyright (c) 2021-2022, Sam Atkins * Copyright (c) 2022, Andreas Kling * * SPDX-License-Identifier: BSD-2-Clause @@ -144,6 +144,9 @@ void ConsoleClient::clear() // 2.3. Printer(logLevel, args[, options]), https://console.spec.whatwg.org/#printer JS::ThrowCompletionOr ConsoleClient::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; @@ -161,7 +164,7 @@ JS::ThrowCompletionOr ConsoleClient::printer(JS::Console::LogLevel lo 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(); } @@ -171,23 +174,23 @@ JS::ThrowCompletionOr ConsoleClient::printer(JS::Console::LogLevel lo 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/Ladybird/ConsoleClient.h b/Ladybird/ConsoleClient.h index 30e91a5af1f..1ffd3864092 100644 --- a/Ladybird/ConsoleClient.h +++ b/Ladybird/ConsoleClient.h @@ -1,7 +1,7 @@ /* * Copyright (c) 2022, Brandon Scott * Copyright (c) 2020, Hunter Salyer - * Copyright (c) 2021, Sam Atkins + * Copyright (c) 2021-2022, Sam Atkins * Copyright (c) 2022, Andreas Kling * * SPDX-License-Identifier: BSD-2-Clause @@ -32,6 +32,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(';'); + } + SimpleWebView& m_view; WeakPtr m_interpreter; @@ -55,6 +61,8 @@ private: }; Vector m_message_log; + StringBuilder m_current_message_style; + WeakPtr m_realm; };