123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232 |
- /*
- * Copyright (c) 2020, Hunter Salyer <thefalsehonesty@gmail.com>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
- #include <AK/HashTable.h>
- #include <AK/StringBuilder.h>
- #include <AK/TypeCasts.h>
- #include <LibJS/Lexer.h>
- #include <LibJS/MarkupGenerator.h>
- #include <LibJS/Runtime/Array.h>
- #include <LibJS/Runtime/Date.h>
- #include <LibJS/Runtime/Error.h>
- #include <LibJS/Runtime/Object.h>
- #include <LibJS/Runtime/VM.h>
- namespace JS {
- String MarkupGenerator::html_from_source(StringView source)
- {
- StringBuilder builder;
- auto lexer = Lexer(source);
- for (auto token = lexer.next(); token.type() != TokenType::Eof; token = lexer.next()) {
- builder.append(token.trivia());
- builder.append(wrap_string_in_style(token.value(), style_type_for_token(token)));
- }
- return builder.to_string();
- }
- String MarkupGenerator::html_from_value(Value value)
- {
- StringBuilder output_html;
- value_to_html(value, output_html);
- return output_html.to_string();
- }
- String MarkupGenerator::html_from_error(Object& object)
- {
- StringBuilder output_html;
- HashTable<Object*> seen_objects;
- error_to_html(object, output_html, seen_objects);
- return output_html.to_string();
- }
- void MarkupGenerator::value_to_html(Value value, StringBuilder& output_html, HashTable<Object*> seen_objects)
- {
- if (value.is_empty()) {
- output_html.append("<empty>");
- return;
- }
- if (value.is_object()) {
- if (seen_objects.contains(&value.as_object())) {
- // FIXME: Maybe we should only do this for circular references,
- // not for all reoccurring objects.
- output_html.appendff("<already printed Object {:p}>", &value.as_object());
- return;
- }
- seen_objects.set(&value.as_object());
- }
- if (value.is_object()) {
- auto& object = value.as_object();
- if (is<Array>(object))
- return array_to_html(static_cast<const Array&>(object), output_html, seen_objects);
- output_html.append(wrap_string_in_style(object.class_name(), StyleType::ObjectType));
- if (object.is_function())
- return function_to_html(object, output_html, seen_objects);
- if (is<Date>(object))
- return date_to_html(object, output_html, seen_objects);
- if (is<Error>(object))
- return error_to_html(object, output_html, seen_objects);
- return object_to_html(object, output_html, seen_objects);
- }
- if (value.is_string())
- output_html.append(open_style_type(StyleType::String));
- else if (value.is_number())
- output_html.append(open_style_type(StyleType::Number));
- else if (value.is_boolean() || value.is_nullish())
- output_html.append(open_style_type(StyleType::KeywordBold));
- if (value.is_string())
- output_html.append('"');
- output_html.append(escape_html_entities(value.to_string_without_side_effects()));
- if (value.is_string())
- output_html.append('"');
- output_html.append("</span>");
- }
- void MarkupGenerator::array_to_html(const Array& array, StringBuilder& html_output, HashTable<Object*>& seen_objects)
- {
- html_output.append(wrap_string_in_style("[ ", StyleType::Punctuation));
- bool first = true;
- for (auto it = array.indexed_properties().begin(false); it != array.indexed_properties().end(); ++it) {
- if (!first)
- html_output.append(wrap_string_in_style(", ", StyleType::Punctuation));
- first = false;
- // FIXME: Exception check
- value_to_html(array.get(it.index()).release_value(), html_output, seen_objects);
- }
- html_output.append(wrap_string_in_style(" ]", StyleType::Punctuation));
- }
- void MarkupGenerator::object_to_html(const Object& object, StringBuilder& html_output, HashTable<Object*>& seen_objects)
- {
- html_output.append(wrap_string_in_style("{ ", StyleType::Punctuation));
- bool first = true;
- for (auto& entry : object.indexed_properties()) {
- if (!first)
- html_output.append(wrap_string_in_style(", ", StyleType::Punctuation));
- first = false;
- html_output.append(wrap_string_in_style(String::number(entry.index()), StyleType::Number));
- html_output.append(wrap_string_in_style(": ", StyleType::Punctuation));
- // FIXME: Exception check
- value_to_html(object.get(entry.index()).release_value(), html_output, seen_objects);
- }
- if (!object.indexed_properties().is_empty() && object.shape().property_count())
- html_output.append(wrap_string_in_style(", ", StyleType::Punctuation));
- size_t index = 0;
- for (auto& it : object.shape().property_table_ordered()) {
- html_output.append(wrap_string_in_style(String::formatted("\"{}\"", escape_html_entities(it.key.to_display_string())), StyleType::String));
- html_output.append(wrap_string_in_style(": ", StyleType::Punctuation));
- value_to_html(object.get_direct(it.value.offset), html_output, seen_objects);
- if (index != object.shape().property_count() - 1)
- html_output.append(wrap_string_in_style(", ", StyleType::Punctuation));
- ++index;
- }
- html_output.append(wrap_string_in_style(" }", StyleType::Punctuation));
- }
- void MarkupGenerator::function_to_html(const Object& function, StringBuilder& html_output, HashTable<Object*>&)
- {
- html_output.appendff("[{}]", function.class_name());
- }
- void MarkupGenerator::date_to_html(const Object& date, StringBuilder& html_output, HashTable<Object*>&)
- {
- html_output.appendff("Date {}", static_cast<const JS::Date&>(date).string());
- }
- void MarkupGenerator::error_to_html(const Object& object, StringBuilder& html_output, HashTable<Object*>&)
- {
- auto& vm = object.vm();
- auto name = object.get_without_side_effects(vm.names.name).value_or(JS::js_undefined());
- auto message = object.get_without_side_effects(vm.names.message).value_or(JS::js_undefined());
- if (name.is_accessor() || message.is_accessor()) {
- html_output.append(wrap_string_in_style(JS::Value(&object).to_string_without_side_effects(), StyleType::Invalid));
- } else {
- auto name_string = name.to_string_without_side_effects();
- auto message_string = message.to_string_without_side_effects();
- html_output.append(wrap_string_in_style(String::formatted("[{}]", name_string), StyleType::Invalid));
- if (!message_string.is_empty())
- html_output.appendff(": {}", escape_html_entities(message_string));
- }
- }
- String MarkupGenerator::style_from_style_type(StyleType type)
- {
- switch (type) {
- case StyleType::Invalid:
- return "color: red;";
- case StyleType::String:
- return "color: -libweb-palette-syntax-string;";
- case StyleType::Number:
- return "color: -libweb-palette-syntax-number;";
- case StyleType::KeywordBold:
- return "color: -libweb-palette-syntax-keyword; font-weight: bold;";
- case StyleType::Punctuation:
- return "color: -libweb-palette-syntax-punctuation;";
- case StyleType::Operator:
- return "color: -libweb-palette-syntax-operator;";
- case StyleType::Keyword:
- return "color: -libweb-palette-syntax-keyword;";
- case StyleType::ControlKeyword:
- return "color: -libweb-palette-syntax-control-keyword;";
- case StyleType::Identifier:
- return "color: -libweb-palette-syntax-identifier;";
- case StyleType::ObjectType:
- return "padding: 2px; background-color: #ddf; color: black; font-weight: bold;";
- default:
- VERIFY_NOT_REACHED();
- }
- }
- MarkupGenerator::StyleType MarkupGenerator::style_type_for_token(Token token)
- {
- switch (token.category()) {
- case TokenCategory::Invalid:
- return StyleType::Invalid;
- case TokenCategory::Number:
- return StyleType::Number;
- case TokenCategory::String:
- return StyleType::String;
- case TokenCategory::Punctuation:
- return StyleType::Punctuation;
- case TokenCategory::Operator:
- return StyleType::Operator;
- case TokenCategory::Keyword:
- switch (token.type()) {
- case TokenType::BoolLiteral:
- case TokenType::NullLiteral:
- return StyleType::KeywordBold;
- default:
- return StyleType::Keyword;
- }
- case TokenCategory::ControlKeyword:
- return StyleType::ControlKeyword;
- case TokenCategory::Identifier:
- return StyleType::Identifier;
- default:
- dbgln("Unknown style type for token {}", token.name());
- VERIFY_NOT_REACHED();
- }
- }
- String MarkupGenerator::open_style_type(StyleType type)
- {
- return String::formatted("<span style=\"{}\">", style_from_style_type(type));
- }
- String MarkupGenerator::wrap_string_in_style(String source, StyleType type)
- {
- return String::formatted("<span style=\"{}\">{}</span>", style_from_style_type(type), escape_html_entities(source));
- }
- }
|