1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 13:37:45 +00:00

LibJS+WebContent+js: Bring console.trace() to spec

The spec very kindly defines `Printer` as accepting
"Implementation-specific representations of printable things such as a
stack trace or group." for the `args`. We make use of that here by
passing the `Trace` itself to `Printer`, instead of having to produce a
representation of the stack trace in advance and then pass that to
`Printer`. That both avoids the hassle of tracking whether the data has
been html-encoded or not, and means clients don't have to implement the
whole `trace()` algorithm, but only the code needed to output the trace.
This commit is contained in:
Sam Atkins 2021-12-10 13:48:38 +00:00 committed by Andreas Kling
parent ce694490f3
commit ff5e07d718
5 changed files with 68 additions and 50 deletions

View file

@ -120,24 +120,25 @@ void WebContentConsoleClient::clear()
clear_output();
}
JS::Value WebContentConsoleClient::trace()
{
StringBuilder html;
html.append(escape_html_entities(vm().join_arguments()));
auto trace = get_trace();
for (auto& function_name : trace) {
if (function_name.is_empty())
function_name = "<anonymous>";
html.appendff(" -> {}<br>", function_name);
}
print_html(html.string_view());
return JS::js_undefined();
}
// 2.3. Printer(logLevel, args[, options]), https://console.spec.whatwg.org/#printer
JS::ThrowCompletionOr<JS::Value> WebContentConsoleClient::printer(JS::Console::LogLevel log_level, Vector<JS::Value>& arguments)
JS::ThrowCompletionOr<JS::Value> WebContentConsoleClient::printer(JS::Console::LogLevel log_level, Variant<Vector<JS::Value>, JS::Console::Trace> arguments)
{
auto output = String::join(" ", arguments);
if (log_level == JS::Console::LogLevel::Trace) {
auto trace = arguments.get<JS::Console::Trace>();
StringBuilder html;
if (!trace.label.is_empty())
html.appendff("<span class='title'>{}</span><br>", escape_html_entities(trace.label));
html.append("<span class='trace'>");
for (auto& function_name : trace.stack)
html.appendff("-> {}<br>", escape_html_entities(function_name));
html.append("</span>");
print_html(html.string_view());
return JS::js_undefined();
}
auto output = String::join(" ", arguments.get<Vector<JS::Value>>());
m_console.output_debug_message(log_level, output);
StringBuilder html;