MarkupGenerator.cpp 8.7 KB

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