MarkupGenerator.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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(Error const& object, bool in_promise)
  35. {
  36. StringBuilder output_html;
  37. error_to_html(object, output_html, in_promise);
  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;"sv);
  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<Array const&>(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. return object_to_html(object, output_html, seen_objects);
  65. }
  66. if (value.is_string())
  67. output_html.append(open_style_type(StyleType::String));
  68. else if (value.is_number())
  69. output_html.append(open_style_type(StyleType::Number));
  70. else if (value.is_boolean() || value.is_nullish())
  71. output_html.append(open_style_type(StyleType::KeywordBold));
  72. if (value.is_string())
  73. output_html.append('"');
  74. output_html.append(escape_html_entities(value.to_string_without_side_effects()));
  75. if (value.is_string())
  76. output_html.append('"');
  77. output_html.append("</span>"sv);
  78. }
  79. void MarkupGenerator::array_to_html(Array const& array, StringBuilder& html_output, HashTable<Object*>& seen_objects)
  80. {
  81. html_output.append(wrap_string_in_style("[ ", StyleType::Punctuation));
  82. bool first = true;
  83. for (auto it = array.indexed_properties().begin(false); it != array.indexed_properties().end(); ++it) {
  84. if (!first)
  85. html_output.append(wrap_string_in_style(", ", StyleType::Punctuation));
  86. first = false;
  87. // FIXME: Exception check
  88. value_to_html(array.get(it.index()).release_value(), html_output, seen_objects);
  89. }
  90. html_output.append(wrap_string_in_style(" ]", StyleType::Punctuation));
  91. }
  92. void MarkupGenerator::object_to_html(Object const& object, StringBuilder& html_output, HashTable<Object*>& seen_objects)
  93. {
  94. html_output.append(wrap_string_in_style("{ ", StyleType::Punctuation));
  95. bool first = true;
  96. for (auto& entry : object.indexed_properties()) {
  97. if (!first)
  98. html_output.append(wrap_string_in_style(", ", StyleType::Punctuation));
  99. first = false;
  100. html_output.append(wrap_string_in_style(String::number(entry.index()), StyleType::Number));
  101. html_output.append(wrap_string_in_style(": ", StyleType::Punctuation));
  102. // FIXME: Exception check
  103. value_to_html(object.get(entry.index()).release_value(), html_output, seen_objects);
  104. }
  105. if (!object.indexed_properties().is_empty() && object.shape().property_count())
  106. html_output.append(wrap_string_in_style(", ", StyleType::Punctuation));
  107. size_t index = 0;
  108. for (auto& it : object.shape().property_table_ordered()) {
  109. html_output.append(wrap_string_in_style(String::formatted("\"{}\"", escape_html_entities(it.key.to_display_string())), StyleType::String));
  110. html_output.append(wrap_string_in_style(": ", StyleType::Punctuation));
  111. value_to_html(object.get_direct(it.value.offset), html_output, seen_objects);
  112. if (index != object.shape().property_count() - 1)
  113. html_output.append(wrap_string_in_style(", ", StyleType::Punctuation));
  114. ++index;
  115. }
  116. html_output.append(wrap_string_in_style(" }", StyleType::Punctuation));
  117. }
  118. void MarkupGenerator::function_to_html(Object const& function, StringBuilder& html_output, HashTable<Object*>&)
  119. {
  120. html_output.appendff("[{}]", function.class_name());
  121. }
  122. void MarkupGenerator::date_to_html(Object const& date, StringBuilder& html_output, HashTable<Object*>&)
  123. {
  124. html_output.appendff("Date {}", to_date_string(static_cast<Date const&>(date).date_value()));
  125. }
  126. void MarkupGenerator::trace_to_html(TracebackFrame const& traceback_frame, StringBuilder& html_output)
  127. {
  128. auto function_name = escape_html_entities(traceback_frame.function_name);
  129. auto [line, column, _] = traceback_frame.source_range.start;
  130. auto get_filename_from_path = [&](StringView filename) -> StringView {
  131. auto last_slash_index = filename.find_last('/');
  132. return last_slash_index.has_value() ? filename.substring_view(*last_slash_index + 1) : filename;
  133. };
  134. auto filename = escape_html_entities(get_filename_from_path(traceback_frame.source_range.filename()));
  135. auto trace = String::formatted("at {} ({}:{}:{})", function_name, filename, line, column);
  136. html_output.appendff("&nbsp;&nbsp;{}<br>", trace);
  137. }
  138. void MarkupGenerator::error_to_html(Error const& error, StringBuilder& html_output, bool in_promise)
  139. {
  140. auto& vm = error.vm();
  141. auto name = error.get_without_side_effects(vm.names.name).value_or(js_undefined());
  142. auto message = error.get_without_side_effects(vm.names.message).value_or(js_undefined());
  143. auto name_string = name.to_string_without_side_effects();
  144. auto message_string = message.to_string_without_side_effects();
  145. auto uncaught_message = String::formatted("Uncaught {}[{}]: ", in_promise ? "(in promise) " : "", name_string);
  146. html_output.append(wrap_string_in_style(uncaught_message, StyleType::Invalid));
  147. html_output.appendff("{}<br>", message_string.is_empty() ? "\"\"" : escape_html_entities(message_string));
  148. for (size_t i = 0; i < error.traceback().size() - min(error.traceback().size(), 3); i++) {
  149. auto& traceback_frame = error.traceback().at(i);
  150. trace_to_html(traceback_frame, html_output);
  151. }
  152. }
  153. String MarkupGenerator::style_from_style_type(StyleType type)
  154. {
  155. switch (type) {
  156. case StyleType::Invalid:
  157. return "color: red;";
  158. case StyleType::String:
  159. return "color: -libweb-palette-syntax-string;";
  160. case StyleType::Number:
  161. return "color: -libweb-palette-syntax-number;";
  162. case StyleType::KeywordBold:
  163. return "color: -libweb-palette-syntax-keyword; font-weight: bold;";
  164. case StyleType::Punctuation:
  165. return "color: -libweb-palette-syntax-punctuation;";
  166. case StyleType::Operator:
  167. return "color: -libweb-palette-syntax-operator;";
  168. case StyleType::Keyword:
  169. return "color: -libweb-palette-syntax-keyword;";
  170. case StyleType::ControlKeyword:
  171. return "color: -libweb-palette-syntax-control-keyword;";
  172. case StyleType::Identifier:
  173. return "color: -libweb-palette-syntax-identifier;";
  174. case StyleType::ObjectType:
  175. return "padding: 2px; background-color: #ddf; color: black; font-weight: bold;";
  176. default:
  177. VERIFY_NOT_REACHED();
  178. }
  179. }
  180. MarkupGenerator::StyleType MarkupGenerator::style_type_for_token(Token token)
  181. {
  182. switch (token.category()) {
  183. case TokenCategory::Invalid:
  184. return StyleType::Invalid;
  185. case TokenCategory::Number:
  186. return StyleType::Number;
  187. case TokenCategory::String:
  188. return StyleType::String;
  189. case TokenCategory::Punctuation:
  190. return StyleType::Punctuation;
  191. case TokenCategory::Operator:
  192. return StyleType::Operator;
  193. case TokenCategory::Keyword:
  194. switch (token.type()) {
  195. case TokenType::BoolLiteral:
  196. case TokenType::NullLiteral:
  197. return StyleType::KeywordBold;
  198. default:
  199. return StyleType::Keyword;
  200. }
  201. case TokenCategory::ControlKeyword:
  202. return StyleType::ControlKeyword;
  203. case TokenCategory::Identifier:
  204. return StyleType::Identifier;
  205. default:
  206. dbgln("Unknown style type for token {}", token.name());
  207. VERIFY_NOT_REACHED();
  208. }
  209. }
  210. String MarkupGenerator::open_style_type(StyleType type)
  211. {
  212. return String::formatted("<span style=\"{}\">", style_from_style_type(type));
  213. }
  214. String MarkupGenerator::wrap_string_in_style(String source, StyleType type)
  215. {
  216. return String::formatted("<span style=\"{}\">{}</span>", style_from_style_type(type), escape_html_entities(source));
  217. }
  218. }