MarkupGenerator.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. * Copyright (c) 2020, Hunter Salyer <thefalsehonesty@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/HashTable.h>
  7. #include <AK/StringBuilder.h>
  8. #include <AK/TypeCasts.h>
  9. #include <LibJS/Lexer.h>
  10. #include <LibJS/MarkupGenerator.h>
  11. #include <LibJS/Runtime/Array.h>
  12. #include <LibJS/Runtime/Date.h>
  13. #include <LibJS/Runtime/DatePrototype.h>
  14. #include <LibJS/Runtime/Error.h>
  15. #include <LibJS/Runtime/Object.h>
  16. #include <LibJS/Runtime/VM.h>
  17. namespace JS {
  18. String MarkupGenerator::html_from_source(StringView source)
  19. {
  20. StringBuilder builder;
  21. auto lexer = Lexer(source);
  22. for (auto token = lexer.next(); token.type() != TokenType::Eof; token = lexer.next()) {
  23. builder.append(token.trivia());
  24. builder.append(wrap_string_in_style(token.value(), style_type_for_token(token)));
  25. }
  26. return builder.to_string();
  27. }
  28. String MarkupGenerator::html_from_value(Value value)
  29. {
  30. StringBuilder output_html;
  31. value_to_html(value, output_html);
  32. return output_html.to_string();
  33. }
  34. String MarkupGenerator::html_from_error(Object& object)
  35. {
  36. StringBuilder output_html;
  37. HashTable<Object*> seen_objects;
  38. error_to_html(object, output_html, seen_objects);
  39. return output_html.to_string();
  40. }
  41. void MarkupGenerator::value_to_html(Value value, StringBuilder& output_html, HashTable<Object*> seen_objects)
  42. {
  43. if (value.is_empty()) {
  44. output_html.append("&lt;empty&gt;");
  45. return;
  46. }
  47. if (value.is_object()) {
  48. if (seen_objects.contains(&value.as_object())) {
  49. // FIXME: Maybe we should only do this for circular references,
  50. // not for all reoccurring objects.
  51. output_html.appendff("&lt;already printed Object {:p}&gt;", &value.as_object());
  52. return;
  53. }
  54. seen_objects.set(&value.as_object());
  55. }
  56. if (value.is_object()) {
  57. auto& object = value.as_object();
  58. if (is<Array>(object))
  59. return array_to_html(static_cast<const Array&>(object), output_html, seen_objects);
  60. output_html.append(wrap_string_in_style(object.class_name(), StyleType::ObjectType));
  61. if (object.is_function())
  62. return function_to_html(object, output_html, seen_objects);
  63. if (is<Date>(object))
  64. return date_to_html(object, output_html, seen_objects);
  65. if (is<Error>(object))
  66. return error_to_html(object, output_html, seen_objects);
  67. return object_to_html(object, output_html, seen_objects);
  68. }
  69. if (value.is_string())
  70. output_html.append(open_style_type(StyleType::String));
  71. else if (value.is_number())
  72. output_html.append(open_style_type(StyleType::Number));
  73. else if (value.is_boolean() || value.is_nullish())
  74. output_html.append(open_style_type(StyleType::KeywordBold));
  75. if (value.is_string())
  76. output_html.append('"');
  77. output_html.append(escape_html_entities(value.to_string_without_side_effects()));
  78. if (value.is_string())
  79. output_html.append('"');
  80. output_html.append("</span>");
  81. }
  82. void MarkupGenerator::array_to_html(const Array& array, StringBuilder& html_output, HashTable<Object*>& seen_objects)
  83. {
  84. html_output.append(wrap_string_in_style("[ ", StyleType::Punctuation));
  85. bool first = true;
  86. for (auto it = array.indexed_properties().begin(false); it != array.indexed_properties().end(); ++it) {
  87. if (!first)
  88. html_output.append(wrap_string_in_style(", ", StyleType::Punctuation));
  89. first = false;
  90. // FIXME: Exception check
  91. value_to_html(array.get(it.index()).release_value(), html_output, seen_objects);
  92. }
  93. html_output.append(wrap_string_in_style(" ]", StyleType::Punctuation));
  94. }
  95. void MarkupGenerator::object_to_html(const Object& object, StringBuilder& html_output, HashTable<Object*>& seen_objects)
  96. {
  97. html_output.append(wrap_string_in_style("{ ", StyleType::Punctuation));
  98. bool first = true;
  99. for (auto& entry : object.indexed_properties()) {
  100. if (!first)
  101. html_output.append(wrap_string_in_style(", ", StyleType::Punctuation));
  102. first = false;
  103. html_output.append(wrap_string_in_style(String::number(entry.index()), StyleType::Number));
  104. html_output.append(wrap_string_in_style(": ", StyleType::Punctuation));
  105. // FIXME: Exception check
  106. value_to_html(object.get(entry.index()).release_value(), html_output, seen_objects);
  107. }
  108. if (!object.indexed_properties().is_empty() && object.shape().property_count())
  109. html_output.append(wrap_string_in_style(", ", StyleType::Punctuation));
  110. size_t index = 0;
  111. for (auto& it : object.shape().property_table_ordered()) {
  112. html_output.append(wrap_string_in_style(String::formatted("\"{}\"", escape_html_entities(it.key.to_display_string())), StyleType::String));
  113. html_output.append(wrap_string_in_style(": ", StyleType::Punctuation));
  114. value_to_html(object.get_direct(it.value.offset), html_output, seen_objects);
  115. if (index != object.shape().property_count() - 1)
  116. html_output.append(wrap_string_in_style(", ", StyleType::Punctuation));
  117. ++index;
  118. }
  119. html_output.append(wrap_string_in_style(" }", StyleType::Punctuation));
  120. }
  121. void MarkupGenerator::function_to_html(const Object& function, StringBuilder& html_output, HashTable<Object*>&)
  122. {
  123. html_output.appendff("[{}]", function.class_name());
  124. }
  125. void MarkupGenerator::date_to_html(const Object& date, StringBuilder& html_output, HashTable<Object*>&)
  126. {
  127. html_output.appendff("Date {}", JS::to_date_string(static_cast<JS::Date const&>(date).date_value()));
  128. }
  129. void MarkupGenerator::error_to_html(const Object& object, StringBuilder& html_output, HashTable<Object*>&)
  130. {
  131. auto& vm = object.vm();
  132. auto name = object.get_without_side_effects(vm.names.name).value_or(JS::js_undefined());
  133. auto message = object.get_without_side_effects(vm.names.message).value_or(JS::js_undefined());
  134. if (name.is_accessor() || message.is_accessor()) {
  135. html_output.append(wrap_string_in_style(JS::Value(&object).to_string_without_side_effects(), StyleType::Invalid));
  136. } else {
  137. auto name_string = name.to_string_without_side_effects();
  138. auto message_string = message.to_string_without_side_effects();
  139. html_output.append(wrap_string_in_style(String::formatted("[{}]", name_string), StyleType::Invalid));
  140. if (!message_string.is_empty())
  141. html_output.appendff(": {}", escape_html_entities(message_string));
  142. }
  143. }
  144. String MarkupGenerator::style_from_style_type(StyleType type)
  145. {
  146. switch (type) {
  147. case StyleType::Invalid:
  148. return "color: red;";
  149. case StyleType::String:
  150. return "color: -libweb-palette-syntax-string;";
  151. case StyleType::Number:
  152. return "color: -libweb-palette-syntax-number;";
  153. case StyleType::KeywordBold:
  154. return "color: -libweb-palette-syntax-keyword; font-weight: bold;";
  155. case StyleType::Punctuation:
  156. return "color: -libweb-palette-syntax-punctuation;";
  157. case StyleType::Operator:
  158. return "color: -libweb-palette-syntax-operator;";
  159. case StyleType::Keyword:
  160. return "color: -libweb-palette-syntax-keyword;";
  161. case StyleType::ControlKeyword:
  162. return "color: -libweb-palette-syntax-control-keyword;";
  163. case StyleType::Identifier:
  164. return "color: -libweb-palette-syntax-identifier;";
  165. case StyleType::ObjectType:
  166. return "padding: 2px; background-color: #ddf; color: black; font-weight: bold;";
  167. default:
  168. VERIFY_NOT_REACHED();
  169. }
  170. }
  171. MarkupGenerator::StyleType MarkupGenerator::style_type_for_token(Token token)
  172. {
  173. switch (token.category()) {
  174. case TokenCategory::Invalid:
  175. return StyleType::Invalid;
  176. case TokenCategory::Number:
  177. return StyleType::Number;
  178. case TokenCategory::String:
  179. return StyleType::String;
  180. case TokenCategory::Punctuation:
  181. return StyleType::Punctuation;
  182. case TokenCategory::Operator:
  183. return StyleType::Operator;
  184. case TokenCategory::Keyword:
  185. switch (token.type()) {
  186. case TokenType::BoolLiteral:
  187. case TokenType::NullLiteral:
  188. return StyleType::KeywordBold;
  189. default:
  190. return StyleType::Keyword;
  191. }
  192. case TokenCategory::ControlKeyword:
  193. return StyleType::ControlKeyword;
  194. case TokenCategory::Identifier:
  195. return StyleType::Identifier;
  196. default:
  197. dbgln("Unknown style type for token {}", token.name());
  198. VERIFY_NOT_REACHED();
  199. }
  200. }
  201. String MarkupGenerator::open_style_type(StyleType type)
  202. {
  203. return String::formatted("<span style=\"{}\">", style_from_style_type(type));
  204. }
  205. String MarkupGenerator::wrap_string_in_style(String source, StyleType type)
  206. {
  207. return String::formatted("<span style=\"{}\">{}</span>", style_from_style_type(type), escape_html_entities(source));
  208. }
  209. }