From d2b493b74ea78a71b0d7140edcf876ee28b8bf07 Mon Sep 17 00:00:00 2001 From: FalseHonesty Date: Mon, 25 May 2020 11:46:10 -0400 Subject: [PATCH] Browser: Add output styles to JS source printed in the console This patch uses the new JS::MarkupGenerator to stylize all of the source code and runtime values printed in the console's output panel. This also does away with the Console's global style sheet, as all styling is handled by the MarkupGenerator and the System Palette. --- Applications/Browser/ConsoleWidget.cpp | 47 +--------------------- Applications/Browser/ConsoleWidget.h | 2 - Libraries/LibJS/MarkupGenerator.cpp | 55 +++++++++++++------------- Libraries/LibJS/MarkupGenerator.h | 50 +++++++++++------------ 4 files changed, 54 insertions(+), 100 deletions(-) diff --git a/Applications/Browser/ConsoleWidget.cpp b/Applications/Browser/ConsoleWidget.cpp index a5ae9e61aa..6d1852ffdb 100644 --- a/Applications/Browser/ConsoleWidget.cpp +++ b/Applications/Browser/ConsoleWidget.cpp @@ -32,8 +32,6 @@ #include #include #include -#include -#include #include #include #include @@ -54,9 +52,6 @@ ConsoleWidget::ConsoleWidget() base_document->append_child(html_element); auto head_element = create_element(base_document, "head"); html_element->append_child(head_element); - auto style_element = create_element(base_document, "style"); - style_element->append_child(adopt(*new Web::Text(base_document, create_document_style()))); - head_element->append_child(style_element); auto body_element = create_element(base_document, "body"); html_element->append_child(body_element); m_console_output_container = body_element; @@ -121,45 +116,6 @@ void ConsoleWidget::set_interpreter(WeakPtr interpreter) clear_output(); } -String ConsoleWidget::create_document_style() -{ - StringBuilder style; - auto palette = this->palette(); - - auto add_class_and_color = [&](const StringView& class_name, Color color, bool bold = false) { - style.append("."); - style.append(class_name); - style.append(" { color: "); - style.append(color.to_string_without_alpha()); - style.append(";"); - - if (bold) { - style.append("font-weight: bold;"); - } - - style.append(" } "); - }; - - add_class_and_color("js-string", palette.syntax_string()); - add_class_and_color("js-number", palette.syntax_number()); - add_class_and_color("js-boolean", palette.syntax_keyword(), true); - add_class_and_color("js-null", palette.syntax_keyword(), true); - add_class_and_color("js-undefined", palette.syntax_keyword(), true); - add_class_and_color("js-array-open", palette.syntax_punctuation()); - add_class_and_color("js-array-close", palette.syntax_punctuation()); - add_class_and_color("js-array-element-separator", palette.syntax_punctuation()); - add_class_and_color("js-object-open", palette.syntax_punctuation()); - add_class_and_color("js-object-close", palette.syntax_punctuation()); - add_class_and_color("js-object-element-separator", palette.syntax_punctuation()); - add_class_and_color("js-object-element-index", palette.syntax_number()); - add_class_and_color("js-object-element-key", palette.syntax_string()); - - // FIXME: Add to palette? - add_class_and_color("js-error-name", Color::Red, true); - - return style.to_string(); -} - void ConsoleWidget::print_source_line(const StringView& source) { StringBuilder html; @@ -167,8 +123,7 @@ void ConsoleWidget::print_source_line(const StringView& source) html.append("> "); html.append(""); - // FIXME: Support output source highlighting - html.append(source); + html.append(JS::MarkupGenerator::html_from_source(source)); print_html(html.string_view()); } diff --git a/Applications/Browser/ConsoleWidget.h b/Applications/Browser/ConsoleWidget.h index 50516ed5e6..7c463a5051 100644 --- a/Applications/Browser/ConsoleWidget.h +++ b/Applications/Browser/ConsoleWidget.h @@ -45,8 +45,6 @@ public: private: ConsoleWidget(); - String create_document_style(); - RefPtr m_console_input; RefPtr m_console_output_view; RefPtr m_console_output_container; diff --git a/Libraries/LibJS/MarkupGenerator.cpp b/Libraries/LibJS/MarkupGenerator.cpp index 7724d7d1a0..2b0e583611 100644 --- a/Libraries/LibJS/MarkupGenerator.cpp +++ b/Libraries/LibJS/MarkupGenerator.cpp @@ -1,28 +1,28 @@ /* - * Copyright (c) 2020, Hunter Salyer - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, this - * list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ + * Copyright (c) 2020, Hunter Salyer + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ #include #include @@ -35,12 +35,12 @@ namespace JS { -String MarkupGenerator::html_from_source(String source) +String MarkupGenerator::html_from_source(const StringView& source) { - auto lexer = Lexer(source); StringBuilder builder; size_t source_cursor = 0; + auto lexer = Lexer(source); for (auto token = lexer.next(); token.type() != TokenType::Eof; token = lexer.next()) { auto length = token.value().length(); auto start = token.line_column(); @@ -52,7 +52,7 @@ String MarkupGenerator::html_from_source(String source) } builder.append(wrap_string_in_style(token.value(), style_type_for_token(token))); - source_cursor = length; + source_cursor = start + length; } if (source_cursor < source.length()) @@ -224,6 +224,7 @@ MarkupGenerator::StyleType MarkupGenerator::style_type_for_token(Token token) case TokenType::ParenClose: case TokenType::ParenOpen: case TokenType::Semicolon: + case TokenType::Colon: case TokenType::Period: return StyleType::Punctuation; case TokenType::Ampersand: diff --git a/Libraries/LibJS/MarkupGenerator.h b/Libraries/LibJS/MarkupGenerator.h index 94909d50ea..0d1dadead4 100644 --- a/Libraries/LibJS/MarkupGenerator.h +++ b/Libraries/LibJS/MarkupGenerator.h @@ -1,28 +1,28 @@ /* - * Copyright (c) 2020, Hunter Salyer - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, this - * list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ + * Copyright (c) 2020, Hunter Salyer + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ #include #include @@ -32,7 +32,7 @@ namespace JS { class MarkupGenerator { public: - static String html_from_source(String); + static String html_from_source(const StringView&); static String html_from_value(Value); private: