MarkupGenerator.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. size_t source_cursor = 0;
  39. auto lexer = Lexer(source);
  40. for (auto token = lexer.next(); token.type() != TokenType::Eof; token = lexer.next()) {
  41. auto length = token.value().length();
  42. auto start = token.line_column() - 1;
  43. if (start > source_cursor) {
  44. builder.append(source.substring_view(source_cursor, start - source_cursor));
  45. }
  46. builder.append(wrap_string_in_style(token.value(), style_type_for_token(token)));
  47. source_cursor = start + length;
  48. }
  49. if (source_cursor < source.length())
  50. builder.append(source.substring_view(source_cursor, source.length() - source_cursor));
  51. return builder.to_string();
  52. }
  53. String MarkupGenerator::html_from_value(Value value)
  54. {
  55. StringBuilder output_html;
  56. value_to_html(value, output_html);
  57. return output_html.to_string();
  58. }
  59. void MarkupGenerator::value_to_html(Value value, StringBuilder& output_html, HashTable<Object*> seen_objects)
  60. {
  61. if (value.is_empty()) {
  62. output_html.append("&lt;empty&gt;");
  63. return;
  64. }
  65. if (value.is_object()) {
  66. if (seen_objects.contains(&value.as_object())) {
  67. // FIXME: Maybe we should only do this for circular references,
  68. // not for all reoccurring objects.
  69. output_html.appendf("&lt;already printed Object %p&gt;", &value.as_object());
  70. return;
  71. }
  72. seen_objects.set(&value.as_object());
  73. }
  74. if (value.is_array())
  75. return array_to_html(static_cast<const Array&>(value.as_object()), output_html, seen_objects);
  76. if (value.is_object()) {
  77. auto& object = value.as_object();
  78. if (object.is_function())
  79. return function_to_html(object, output_html, seen_objects);
  80. if (object.is_date())
  81. return date_to_html(object, output_html, seen_objects);
  82. if (object.is_error())
  83. return error_to_html(object, output_html, seen_objects);
  84. return object_to_html(object, output_html, seen_objects);
  85. }
  86. if (value.is_string())
  87. output_html.append(open_style_type(StyleType::String));
  88. else if (value.is_number())
  89. output_html.append(open_style_type(StyleType::Number));
  90. else if (value.is_boolean() || value.is_null() || value.is_undefined())
  91. output_html.append(open_style_type(StyleType::KeywordBold));
  92. if (value.is_string())
  93. output_html.append('"');
  94. output_html.append(value.to_string_without_side_effects());
  95. if (value.is_string())
  96. output_html.append('"');
  97. output_html.append("</span>");
  98. }
  99. void MarkupGenerator::array_to_html(const Array& array, StringBuilder& html_output, HashTable<Object*>& seen_objects)
  100. {
  101. html_output.append(wrap_string_in_style("[ ", StyleType::Punctuation));
  102. bool first = true;
  103. for (auto it = array.indexed_properties().begin(false); it != array.indexed_properties().end(); ++it) {
  104. if (!first)
  105. html_output.append(wrap_string_in_style(", ", StyleType::Punctuation));
  106. first = false;
  107. // FIXME: Exception check
  108. value_to_html(it.value_and_attributes(const_cast<Array*>(&array)).value, html_output, seen_objects);
  109. }
  110. html_output.append(wrap_string_in_style(" ]", StyleType::Punctuation));
  111. }
  112. void MarkupGenerator::object_to_html(const Object& object, StringBuilder& html_output, HashTable<Object*>& seen_objects)
  113. {
  114. html_output.append(wrap_string_in_style("{ ", StyleType::Punctuation));
  115. bool first = true;
  116. for (auto& entry : object.indexed_properties()) {
  117. if (!first)
  118. html_output.append(wrap_string_in_style(", ", StyleType::Punctuation));
  119. first = false;
  120. html_output.append(wrap_string_in_style(String::number(entry.index()), StyleType::Number));
  121. html_output.append(wrap_string_in_style(": ", StyleType::Punctuation));
  122. // FIXME: Exception check
  123. value_to_html(entry.value_and_attributes(const_cast<Object*>(&object)).value, html_output, seen_objects);
  124. }
  125. if (!object.indexed_properties().is_empty() && object.shape().property_count())
  126. html_output.append(wrap_string_in_style(", ", StyleType::Punctuation));
  127. size_t index = 0;
  128. for (auto& it : object.shape().property_table_ordered()) {
  129. html_output.append(wrap_string_in_style(String::format("\"%s\"", it.key.characters()), StyleType::String));
  130. html_output.append(wrap_string_in_style(": ", StyleType::Punctuation));
  131. value_to_html(object.get_direct(it.value.offset), html_output, seen_objects);
  132. if (index != object.shape().property_count() - 1)
  133. html_output.append(wrap_string_in_style(", ", StyleType::Punctuation));
  134. ++index;
  135. }
  136. html_output.append(wrap_string_in_style(" }", StyleType::Punctuation));
  137. }
  138. void MarkupGenerator::function_to_html(const Object& function, StringBuilder& html_output, HashTable<Object*>&)
  139. {
  140. html_output.appendf("[%s]", function.class_name());
  141. }
  142. void MarkupGenerator::date_to_html(const Object& date, StringBuilder& html_output, HashTable<Object*>&)
  143. {
  144. html_output.appendf("Date %s", static_cast<const JS::Date&>(date).string().characters());
  145. }
  146. void MarkupGenerator::error_to_html(const Object& object, StringBuilder& html_output, HashTable<Object*>&)
  147. {
  148. auto& error = static_cast<const Error&>(object);
  149. html_output.append(wrap_string_in_style(String::format("[%s]", error.name().characters()), StyleType::Invalid));
  150. if (!error.message().is_empty()) {
  151. html_output.appendf(": %s", error.message().characters());
  152. }
  153. }
  154. String MarkupGenerator::style_from_style_type(StyleType type)
  155. {
  156. switch (type) {
  157. case StyleType::Invalid:
  158. return "color: red;";
  159. case StyleType::String:
  160. return "color: -libweb-palette-syntax-string;";
  161. case StyleType::Number:
  162. return "color: -libweb-palette-syntax-number;";
  163. case StyleType::KeywordBold:
  164. return "color: -libweb-palette-syntax-keyword; font-weight: bold;";
  165. case StyleType::Punctuation:
  166. return "color: -libweb-palette-syntax-punctuation;";
  167. case StyleType::Operator:
  168. return "color: -libweb-palette-syntax-operator;";
  169. case StyleType::Keyword:
  170. return "color: -libweb-palette-syntax-keyword;";
  171. case StyleType::ControlKeyword:
  172. return "color: -libweb-palette-syntax-control-keyword;";
  173. case StyleType::Identifier:
  174. return "color: -libweb-palette-syntax-identifier;";
  175. default:
  176. ASSERT_NOT_REACHED();
  177. }
  178. }
  179. MarkupGenerator::StyleType MarkupGenerator::style_type_for_token(Token token)
  180. {
  181. switch (token.type()) {
  182. case TokenType::Enum:
  183. case TokenType::Eof:
  184. case TokenType::Implements:
  185. case TokenType::Invalid:
  186. case TokenType::Package:
  187. case TokenType::Private:
  188. case TokenType::Protected:
  189. case TokenType::Public:
  190. case TokenType::Static:
  191. case TokenType::UnterminatedTemplateLiteral:
  192. return StyleType::Invalid;
  193. case TokenType::NumericLiteral:
  194. case TokenType::BigIntLiteral:
  195. return StyleType::Number;
  196. case TokenType::StringLiteral:
  197. case TokenType::TemplateLiteralStart:
  198. case TokenType::TemplateLiteralEnd:
  199. case TokenType::TemplateLiteralString:
  200. case TokenType::RegexLiteral:
  201. case TokenType::RegexFlags:
  202. case TokenType::UnterminatedStringLiteral:
  203. return StyleType::String;
  204. case TokenType::BracketClose:
  205. case TokenType::BracketOpen:
  206. case TokenType::Comma:
  207. case TokenType::CurlyClose:
  208. case TokenType::CurlyOpen:
  209. case TokenType::ParenClose:
  210. case TokenType::ParenOpen:
  211. case TokenType::Semicolon:
  212. case TokenType::Colon:
  213. case TokenType::Period:
  214. return StyleType::Punctuation;
  215. case TokenType::Ampersand:
  216. case TokenType::AmpersandEquals:
  217. case TokenType::Arrow:
  218. case TokenType::Asterisk:
  219. case TokenType::AsteriskEquals:
  220. case TokenType::Caret:
  221. case TokenType::CaretEquals:
  222. case TokenType::DoubleAmpersand:
  223. case TokenType::DoubleAsterisk:
  224. case TokenType::DoubleAsteriskEquals:
  225. case TokenType::DoublePipe:
  226. case TokenType::DoubleQuestionMark:
  227. case TokenType::Equals:
  228. case TokenType::EqualsEquals:
  229. case TokenType::EqualsEqualsEquals:
  230. case TokenType::ExclamationMark:
  231. case TokenType::ExclamationMarkEquals:
  232. case TokenType::ExclamationMarkEqualsEquals:
  233. case TokenType::GreaterThan:
  234. case TokenType::GreaterThanEquals:
  235. case TokenType::LessThan:
  236. case TokenType::LessThanEquals:
  237. case TokenType::Minus:
  238. case TokenType::MinusEquals:
  239. case TokenType::MinusMinus:
  240. case TokenType::Percent:
  241. case TokenType::PercentEquals:
  242. case TokenType::Pipe:
  243. case TokenType::PipeEquals:
  244. case TokenType::Plus:
  245. case TokenType::PlusEquals:
  246. case TokenType::PlusPlus:
  247. case TokenType::QuestionMark:
  248. case TokenType::QuestionMarkPeriod:
  249. case TokenType::ShiftLeft:
  250. case TokenType::ShiftLeftEquals:
  251. case TokenType::ShiftRight:
  252. case TokenType::ShiftRightEquals:
  253. case TokenType::Slash:
  254. case TokenType::SlashEquals:
  255. case TokenType::Tilde:
  256. case TokenType::TripleDot:
  257. case TokenType::UnsignedShiftRight:
  258. case TokenType::UnsignedShiftRightEquals:
  259. return StyleType::Operator;
  260. case TokenType::BoolLiteral:
  261. case TokenType::NullLiteral:
  262. return StyleType::KeywordBold;
  263. case TokenType::Async:
  264. case TokenType::Class:
  265. case TokenType::Const:
  266. case TokenType::Debugger:
  267. case TokenType::Delete:
  268. case TokenType::Export:
  269. case TokenType::Extends:
  270. case TokenType::Function:
  271. case TokenType::Import:
  272. case TokenType::In:
  273. case TokenType::Instanceof:
  274. case TokenType::Interface:
  275. case TokenType::Let:
  276. case TokenType::New:
  277. case TokenType::Super:
  278. case TokenType::TemplateLiteralExprStart:
  279. case TokenType::TemplateLiteralExprEnd:
  280. case TokenType::This:
  281. case TokenType::Throw:
  282. case TokenType::Typeof:
  283. case TokenType::Var:
  284. case TokenType::Void:
  285. return StyleType::Keyword;
  286. case TokenType::Await:
  287. case TokenType::Break:
  288. case TokenType::Case:
  289. case TokenType::Catch:
  290. case TokenType::Continue:
  291. case TokenType::Default:
  292. case TokenType::Do:
  293. case TokenType::Else:
  294. case TokenType::Finally:
  295. case TokenType::For:
  296. case TokenType::If:
  297. case TokenType::Return:
  298. case TokenType::Switch:
  299. case TokenType::Try:
  300. case TokenType::While:
  301. case TokenType::With:
  302. case TokenType::Yield:
  303. return StyleType::ControlKeyword;
  304. case TokenType::Identifier:
  305. return StyleType::Identifier;
  306. default:
  307. dbg() << "Unknown style type for token" << token.name();
  308. ASSERT_NOT_REACHED();
  309. }
  310. }
  311. String MarkupGenerator::open_style_type(StyleType type)
  312. {
  313. return String::format("<span style=\"%s\">", style_from_style_type(type).characters());
  314. }
  315. String MarkupGenerator::wrap_string_in_style(String source, StyleType type)
  316. {
  317. return String::format("<span style=\"%s\">%s</span>", style_from_style_type(type).characters(), source.characters());
  318. }
  319. }