MarkupGenerator.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. * Copyright (c) 2020, Hunter Salyer <thefalsehonesty@gmail.com>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/HashTable.h>
  27. #include <AK/StringBuilder.h>
  28. #include <LibJS/Lexer.h>
  29. #include <LibJS/MarkupGenerator.h>
  30. #include <LibJS/Runtime/Array.h>
  31. #include <LibJS/Runtime/Date.h>
  32. #include <LibJS/Runtime/Error.h>
  33. #include <LibJS/Runtime/Object.h>
  34. namespace JS {
  35. String MarkupGenerator::html_from_source(const StringView& source)
  36. {
  37. StringBuilder builder;
  38. auto lexer = Lexer(source);
  39. for (auto token = lexer.next(); token.type() != TokenType::Eof; token = lexer.next()) {
  40. builder.append(token.trivia());
  41. builder.append(wrap_string_in_style(token.value(), style_type_for_token(token)));
  42. }
  43. return builder.to_string();
  44. }
  45. String MarkupGenerator::html_from_value(Value value)
  46. {
  47. StringBuilder output_html;
  48. value_to_html(value, output_html);
  49. return output_html.to_string();
  50. }
  51. void MarkupGenerator::value_to_html(Value value, StringBuilder& output_html, HashTable<Object*> seen_objects)
  52. {
  53. if (value.is_empty()) {
  54. output_html.append("&lt;empty&gt;");
  55. return;
  56. }
  57. if (value.is_object()) {
  58. if (seen_objects.contains(&value.as_object())) {
  59. // FIXME: Maybe we should only do this for circular references,
  60. // not for all reoccurring objects.
  61. output_html.appendff("&lt;already printed Object {:p}&gt;", &value.as_object());
  62. return;
  63. }
  64. seen_objects.set(&value.as_object());
  65. }
  66. if (value.is_array())
  67. return array_to_html(static_cast<const Array&>(value.as_object()), output_html, seen_objects);
  68. if (value.is_object()) {
  69. auto& object = value.as_object();
  70. if (object.is_function())
  71. return function_to_html(object, output_html, seen_objects);
  72. if (object.is_date())
  73. return date_to_html(object, output_html, seen_objects);
  74. if (object.is_error())
  75. return error_to_html(object, output_html, seen_objects);
  76. return object_to_html(object, output_html, seen_objects);
  77. }
  78. if (value.is_string())
  79. output_html.append(open_style_type(StyleType::String));
  80. else if (value.is_number())
  81. output_html.append(open_style_type(StyleType::Number));
  82. else if (value.is_boolean() || value.is_nullish())
  83. output_html.append(open_style_type(StyleType::KeywordBold));
  84. if (value.is_string())
  85. output_html.append('"');
  86. output_html.append(escape_html_entities(value.to_string_without_side_effects()));
  87. if (value.is_string())
  88. output_html.append('"');
  89. output_html.append("</span>");
  90. }
  91. void MarkupGenerator::array_to_html(const Array& array, StringBuilder& html_output, HashTable<Object*>& seen_objects)
  92. {
  93. html_output.append(wrap_string_in_style("[ ", StyleType::Punctuation));
  94. bool first = true;
  95. for (auto it = array.indexed_properties().begin(false); it != array.indexed_properties().end(); ++it) {
  96. if (!first)
  97. html_output.append(wrap_string_in_style(", ", StyleType::Punctuation));
  98. first = false;
  99. // FIXME: Exception check
  100. value_to_html(it.value_and_attributes(const_cast<Array*>(&array)).value, html_output, seen_objects);
  101. }
  102. html_output.append(wrap_string_in_style(" ]", StyleType::Punctuation));
  103. }
  104. void MarkupGenerator::object_to_html(const Object& object, StringBuilder& html_output, HashTable<Object*>& seen_objects)
  105. {
  106. html_output.append(wrap_string_in_style("{ ", StyleType::Punctuation));
  107. bool first = true;
  108. for (auto& entry : object.indexed_properties()) {
  109. if (!first)
  110. html_output.append(wrap_string_in_style(", ", StyleType::Punctuation));
  111. first = false;
  112. html_output.append(wrap_string_in_style(String::number(entry.index()), StyleType::Number));
  113. html_output.append(wrap_string_in_style(": ", StyleType::Punctuation));
  114. // FIXME: Exception check
  115. value_to_html(entry.value_and_attributes(const_cast<Object*>(&object)).value, html_output, seen_objects);
  116. }
  117. if (!object.indexed_properties().is_empty() && object.shape().property_count())
  118. html_output.append(wrap_string_in_style(", ", StyleType::Punctuation));
  119. size_t index = 0;
  120. for (auto& it : object.shape().property_table_ordered()) {
  121. html_output.append(wrap_string_in_style(String::formatted("\"{}\"", escape_html_entities(it.key.to_display_string())), StyleType::String));
  122. html_output.append(wrap_string_in_style(": ", StyleType::Punctuation));
  123. value_to_html(object.get_direct(it.value.offset), html_output, seen_objects);
  124. if (index != object.shape().property_count() - 1)
  125. html_output.append(wrap_string_in_style(", ", StyleType::Punctuation));
  126. ++index;
  127. }
  128. html_output.append(wrap_string_in_style(" }", StyleType::Punctuation));
  129. }
  130. void MarkupGenerator::function_to_html(const Object& function, StringBuilder& html_output, HashTable<Object*>&)
  131. {
  132. html_output.appendff("[{}]", function.class_name());
  133. }
  134. void MarkupGenerator::date_to_html(const Object& date, StringBuilder& html_output, HashTable<Object*>&)
  135. {
  136. html_output.appendff("Date {}", static_cast<const JS::Date&>(date).string());
  137. }
  138. void MarkupGenerator::error_to_html(const Object& object, StringBuilder& html_output, HashTable<Object*>&)
  139. {
  140. auto& error = static_cast<const Error&>(object);
  141. html_output.append(wrap_string_in_style(String::formatted("[{}]", error.name()), StyleType::Invalid));
  142. if (!error.message().is_empty()) {
  143. html_output.appendff(": {}", escape_html_entities(error.message()));
  144. }
  145. }
  146. String MarkupGenerator::style_from_style_type(StyleType type)
  147. {
  148. switch (type) {
  149. case StyleType::Invalid:
  150. return "color: red;";
  151. case StyleType::String:
  152. return "color: -libweb-palette-syntax-string;";
  153. case StyleType::Number:
  154. return "color: -libweb-palette-syntax-number;";
  155. case StyleType::KeywordBold:
  156. return "color: -libweb-palette-syntax-keyword; font-weight: bold;";
  157. case StyleType::Punctuation:
  158. return "color: -libweb-palette-syntax-punctuation;";
  159. case StyleType::Operator:
  160. return "color: -libweb-palette-syntax-operator;";
  161. case StyleType::Keyword:
  162. return "color: -libweb-palette-syntax-keyword;";
  163. case StyleType::ControlKeyword:
  164. return "color: -libweb-palette-syntax-control-keyword;";
  165. case StyleType::Identifier:
  166. return "color: -libweb-palette-syntax-identifier;";
  167. default:
  168. ASSERT_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. ASSERT_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. }