MarkupGenerator.cpp 8.6 KB

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