MarkupGenerator.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. namespace JS {
  15. String MarkupGenerator::html_from_source(const StringView& source)
  16. {
  17. StringBuilder builder;
  18. auto lexer = Lexer(source);
  19. for (auto token = lexer.next(); token.type() != TokenType::Eof; token = lexer.next()) {
  20. builder.append(token.trivia());
  21. builder.append(wrap_string_in_style(token.value(), style_type_for_token(token)));
  22. }
  23. return builder.to_string();
  24. }
  25. String MarkupGenerator::html_from_value(Value value)
  26. {
  27. StringBuilder output_html;
  28. value_to_html(value, output_html);
  29. return output_html.to_string();
  30. }
  31. String MarkupGenerator::html_from_error(Object& object)
  32. {
  33. StringBuilder output_html;
  34. HashTable<Object*> seen_objects;
  35. error_to_html(object, output_html, seen_objects);
  36. return output_html.to_string();
  37. }
  38. void MarkupGenerator::value_to_html(Value value, StringBuilder& output_html, HashTable<Object*> seen_objects)
  39. {
  40. if (value.is_empty()) {
  41. output_html.append("&lt;empty&gt;");
  42. return;
  43. }
  44. if (value.is_object()) {
  45. if (seen_objects.contains(&value.as_object())) {
  46. // FIXME: Maybe we should only do this for circular references,
  47. // not for all reoccurring objects.
  48. output_html.appendff("&lt;already printed Object {:p}&gt;", &value.as_object());
  49. return;
  50. }
  51. seen_objects.set(&value.as_object());
  52. }
  53. if (value.is_object()) {
  54. auto& object = value.as_object();
  55. if (object.is_array())
  56. return array_to_html(static_cast<const Array&>(object), output_html, seen_objects);
  57. output_html.append(wrap_string_in_style(object.class_name(), StyleType::ObjectType));
  58. if (object.is_function())
  59. return function_to_html(object, output_html, seen_objects);
  60. if (is<Date>(object))
  61. return date_to_html(object, output_html, seen_objects);
  62. if (is<Error>(object))
  63. return error_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>");
  78. }
  79. void MarkupGenerator::array_to_html(const Array& 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(it.value_and_attributes(const_cast<Array*>(&array)).value, html_output, seen_objects);
  89. }
  90. html_output.append(wrap_string_in_style(" ]", StyleType::Punctuation));
  91. }
  92. void MarkupGenerator::object_to_html(const Object& 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(entry.value_and_attributes(const_cast<Object*>(&object)).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(const Object& function, StringBuilder& html_output, HashTable<Object*>&)
  119. {
  120. html_output.appendff("[{}]", function.class_name());
  121. }
  122. void MarkupGenerator::date_to_html(const Object& date, StringBuilder& html_output, HashTable<Object*>&)
  123. {
  124. html_output.appendff("Date {}", static_cast<const JS::Date&>(date).string());
  125. }
  126. void MarkupGenerator::error_to_html(const Object& object, StringBuilder& html_output, HashTable<Object*>&)
  127. {
  128. auto name = object.get_without_side_effects("name").value_or(JS::js_undefined());
  129. auto message = object.get_without_side_effects("message").value_or(JS::js_undefined());
  130. if (name.is_accessor() || name.is_native_property() || message.is_accessor() || message.is_native_property()) {
  131. html_output.append(wrap_string_in_style(JS::Value(&object).to_string_without_side_effects(), StyleType::Invalid));
  132. } else {
  133. auto name_string = name.to_string_without_side_effects();
  134. auto message_string = message.to_string_without_side_effects();
  135. html_output.append(wrap_string_in_style(String::formatted("[{}]", name_string), StyleType::Invalid));
  136. if (!message_string.is_empty())
  137. html_output.appendff(": {}", escape_html_entities(message_string));
  138. }
  139. }
  140. String MarkupGenerator::style_from_style_type(StyleType type)
  141. {
  142. switch (type) {
  143. case StyleType::Invalid:
  144. return "color: red;";
  145. case StyleType::String:
  146. return "color: -libweb-palette-syntax-string;";
  147. case StyleType::Number:
  148. return "color: -libweb-palette-syntax-number;";
  149. case StyleType::KeywordBold:
  150. return "color: -libweb-palette-syntax-keyword; font-weight: bold;";
  151. case StyleType::Punctuation:
  152. return "color: -libweb-palette-syntax-punctuation;";
  153. case StyleType::Operator:
  154. return "color: -libweb-palette-syntax-operator;";
  155. case StyleType::Keyword:
  156. return "color: -libweb-palette-syntax-keyword;";
  157. case StyleType::ControlKeyword:
  158. return "color: -libweb-palette-syntax-control-keyword;";
  159. case StyleType::Identifier:
  160. return "color: -libweb-palette-syntax-identifier;";
  161. case StyleType::ObjectType:
  162. return "padding: 2px; background-color: #ddf; color: black; font-weight: bold;";
  163. default:
  164. VERIFY_NOT_REACHED();
  165. }
  166. }
  167. MarkupGenerator::StyleType MarkupGenerator::style_type_for_token(Token token)
  168. {
  169. switch (token.category()) {
  170. case TokenCategory::Invalid:
  171. return StyleType::Invalid;
  172. case TokenCategory::Number:
  173. return StyleType::Number;
  174. case TokenCategory::String:
  175. return StyleType::String;
  176. case TokenCategory::Punctuation:
  177. return StyleType::Punctuation;
  178. case TokenCategory::Operator:
  179. return StyleType::Operator;
  180. case TokenCategory::Keyword:
  181. switch (token.type()) {
  182. case TokenType::BoolLiteral:
  183. case TokenType::NullLiteral:
  184. return StyleType::KeywordBold;
  185. default:
  186. return StyleType::Keyword;
  187. }
  188. case TokenCategory::ControlKeyword:
  189. return StyleType::ControlKeyword;
  190. case TokenCategory::Identifier:
  191. return StyleType::Identifier;
  192. default:
  193. dbgln("Unknown style type for token {}", token.name());
  194. VERIFY_NOT_REACHED();
  195. }
  196. }
  197. String MarkupGenerator::open_style_type(StyleType type)
  198. {
  199. return String::formatted("<span style=\"{}\">", style_from_style_type(type));
  200. }
  201. String MarkupGenerator::wrap_string_in_style(String source, StyleType type)
  202. {
  203. return String::formatted("<span style=\"{}\">{}</span>", style_from_style_type(type), escape_html_entities(source));
  204. }
  205. }