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.
This commit is contained in:
FalseHonesty 2020-05-25 11:46:10 -04:00 committed by Andreas Kling
parent 941b028ca3
commit d2b493b74e
Notes: sideshowbarker 2024-07-19 06:08:12 +09:00
4 changed files with 54 additions and 100 deletions

View file

@ -32,8 +32,6 @@
#include <LibJS/Interpreter.h>
#include <LibJS/MarkupGenerator.h>
#include <LibJS/Parser.h>
#include <LibJS/Runtime/Array.h>
#include <LibJS/Runtime/Date.h>
#include <LibJS/Runtime/Error.h>
#include <LibWeb/DOM/DocumentType.h>
#include <LibWeb/DOM/ElementFactory.h>
@ -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<JS::Interpreter> 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("&gt; ");
html.append("</span>");
// FIXME: Support output source highlighting
html.append(source);
html.append(JS::MarkupGenerator::html_from_source(source));
print_html(html.string_view());
}

View file

@ -45,8 +45,6 @@ public:
private:
ConsoleWidget();
String create_document_style();
RefPtr<GUI::TextBox> m_console_input;
RefPtr<Web::HtmlView> m_console_output_view;
RefPtr<Web::Element> m_console_output_container;

View file

@ -1,28 +1,28 @@
/*
* Copyright (c) 2020, Hunter Salyer <thefalsehonesty@gmail.com>
* 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 <thefalsehonesty@gmail.com>
* 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 <AK/HashTable.h>
#include <AK/StringBuilder.h>
@ -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:

View file

@ -1,28 +1,28 @@
/*
* Copyright (c) 2020, Hunter Salyer <thefalsehonesty@gmail.com>
* 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 <thefalsehonesty@gmail.com>
* 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 <AK/HashTable.h>
#include <AK/String.h>
@ -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: