InspectorClient.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  1. /*
  2. * Copyright (c) 2023-2024, Tim Flynn <trflynn89@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Base64.h>
  7. #include <AK/JsonArray.h>
  8. #include <AK/JsonObject.h>
  9. #include <AK/LexicalPath.h>
  10. #include <AK/SourceGenerator.h>
  11. #include <AK/StringBuilder.h>
  12. #include <LibCore/Directory.h>
  13. #include <LibCore/File.h>
  14. #include <LibCore/Resource.h>
  15. #include <LibJS/MarkupGenerator.h>
  16. #include <LibWeb/Infra/Strings.h>
  17. #include <LibWebView/Application.h>
  18. #include <LibWebView/InspectorClient.h>
  19. #include <LibWebView/SourceHighlighter.h>
  20. namespace WebView {
  21. static constexpr auto INSPECTOR_HTML = "resource://ladybird/inspector.html"sv;
  22. static constexpr auto INSPECTOR_CSS = "resource://ladybird/inspector.css"sv;
  23. static constexpr auto INSPECTOR_JS = "resource://ladybird/inspector.js"sv;
  24. static ErrorOr<JsonValue> parse_json_tree(StringView json)
  25. {
  26. auto parsed_tree = TRY(JsonValue::from_string(json));
  27. if (!parsed_tree.is_object())
  28. return Error::from_string_literal("Expected tree to be a JSON object");
  29. return parsed_tree;
  30. }
  31. static String style_sheet_identifier_to_json(Web::CSS::StyleSheetIdentifier const& identifier)
  32. {
  33. return MUST(String::formatted("{{ type: '{}', domNodeId: {}, url: '{}' }}"sv,
  34. Web::CSS::style_sheet_identifier_type_to_string(identifier.type),
  35. identifier.dom_element_unique_id.map([](auto& it) { return MUST(String::number(it)); }).value_or("undefined"_string),
  36. identifier.url.value_or("undefined"_string)));
  37. }
  38. InspectorClient::InspectorClient(ViewImplementation& content_web_view, ViewImplementation& inspector_web_view)
  39. : m_content_web_view(content_web_view)
  40. , m_inspector_web_view(inspector_web_view)
  41. {
  42. m_content_web_view.on_received_dom_tree = [this](auto const& dom_tree) {
  43. auto result = parse_json_tree(dom_tree);
  44. if (result.is_error()) {
  45. dbgln("Failed to load DOM tree: {}", result.error());
  46. return;
  47. }
  48. auto dom_tree_html = generate_dom_tree(result.value().as_object());
  49. auto dom_tree_base64 = MUST(encode_base64(dom_tree_html.bytes()));
  50. auto script = MUST(String::formatted("inspector.loadDOMTree(\"{}\");", dom_tree_base64));
  51. m_inspector_web_view.run_javascript(script);
  52. m_dom_tree_loaded = true;
  53. if (m_pending_selection.has_value())
  54. select_node(m_pending_selection.release_value());
  55. else
  56. select_default_node();
  57. };
  58. m_content_web_view.on_received_dom_node_properties = [this](auto const& inspected_node_properties) {
  59. StringBuilder builder;
  60. // FIXME: Support box model metrics and ARIA properties.
  61. auto generate_property_script = [&](auto const& computed_style, auto const& resolved_style, auto const& custom_properties, auto const& fonts) {
  62. builder.append("inspector.createPropertyTables(\""sv);
  63. builder.append_escaped_for_json(computed_style);
  64. builder.append("\", \""sv);
  65. builder.append_escaped_for_json(resolved_style);
  66. builder.append("\", \""sv);
  67. builder.append_escaped_for_json(custom_properties);
  68. builder.append("\");"sv);
  69. builder.append("inspector.createFontList(\""sv);
  70. builder.append_escaped_for_json(fonts);
  71. builder.append("\");"sv);
  72. };
  73. if (inspected_node_properties.has_value()) {
  74. generate_property_script(
  75. inspected_node_properties->computed_style_json,
  76. inspected_node_properties->resolved_style_json,
  77. inspected_node_properties->custom_properties_json,
  78. inspected_node_properties->fonts_json);
  79. } else {
  80. generate_property_script("{}"sv, "{}"sv, "{}"sv, "{}"sv);
  81. }
  82. m_inspector_web_view.run_javascript(builder.string_view());
  83. };
  84. m_content_web_view.on_received_accessibility_tree = [this](auto const& accessibility_tree) {
  85. auto result = parse_json_tree(accessibility_tree);
  86. if (result.is_error()) {
  87. dbgln("Failed to load accessibility tree: {}", result.error());
  88. return;
  89. }
  90. auto accessibility_tree_html = generate_accessibility_tree(result.value().as_object());
  91. auto accessibility_tree_base64 = MUST(encode_base64(accessibility_tree_html.bytes()));
  92. auto script = MUST(String::formatted("inspector.loadAccessibilityTree(\"{}\");", accessibility_tree_base64));
  93. m_inspector_web_view.run_javascript(script);
  94. };
  95. m_content_web_view.on_received_hovered_node_id = [this](auto node_id) {
  96. select_node(node_id);
  97. };
  98. m_content_web_view.on_received_style_sheet_list = [this](auto const& style_sheets) {
  99. StringBuilder builder;
  100. builder.append("inspector.setStyleSheets(["sv);
  101. for (auto& style_sheet : style_sheets) {
  102. builder.appendff("{}, "sv, style_sheet_identifier_to_json(style_sheet));
  103. }
  104. builder.append("]);"sv);
  105. m_inspector_web_view.run_javascript(builder.string_view());
  106. };
  107. m_content_web_view.on_received_style_sheet_source = [this](Web::CSS::StyleSheetIdentifier const& identifier, String const& source) {
  108. // TODO: Highlight it
  109. auto escaped_source = escape_html_entities(source.bytes()).replace("\t"sv, " "sv, ReplaceMode::All);
  110. auto script = MUST(String::formatted("inspector.setStyleSheetSource({}, \"{}\");",
  111. style_sheet_identifier_to_json(identifier),
  112. MUST(encode_base64(escaped_source.bytes()))));
  113. m_inspector_web_view.run_javascript(script);
  114. };
  115. m_content_web_view.on_finshed_editing_dom_node = [this](auto const& node_id) {
  116. m_pending_selection = node_id;
  117. m_dom_tree_loaded = false;
  118. m_dom_node_attributes.clear();
  119. inspect();
  120. };
  121. m_content_web_view.on_received_dom_node_html = [this](auto const& html) {
  122. if (m_content_web_view.on_insert_clipboard_entry)
  123. m_content_web_view.on_insert_clipboard_entry(html, "unspecified"_string, "text/plain"_string);
  124. };
  125. m_content_web_view.on_received_console_message = [this](auto message_index) {
  126. handle_console_message(message_index);
  127. };
  128. m_content_web_view.on_received_console_messages = [this](auto start_index, auto const& message_types, auto const& messages) {
  129. handle_console_messages(start_index, message_types, messages);
  130. };
  131. m_inspector_web_view.enable_inspector_prototype();
  132. m_inspector_web_view.use_native_user_style_sheet();
  133. m_inspector_web_view.on_inspector_loaded = [this]() {
  134. m_inspector_loaded = true;
  135. inspect();
  136. m_content_web_view.js_console_request_messages(0);
  137. };
  138. m_inspector_web_view.on_inspector_requested_dom_tree_context_menu = [this](auto node_id, auto position, auto const& type, auto const& tag, auto const& attribute_index) {
  139. Optional<Attribute> attribute;
  140. if (attribute_index.has_value())
  141. attribute = m_dom_node_attributes.get(node_id)->at(*attribute_index);
  142. m_context_menu_data = ContextMenuData { node_id, tag, attribute };
  143. if (type.is_one_of("text"sv, "comment"sv)) {
  144. if (on_requested_dom_node_text_context_menu)
  145. on_requested_dom_node_text_context_menu(position);
  146. } else if (type == "tag"sv) {
  147. VERIFY(tag.has_value());
  148. if (on_requested_dom_node_tag_context_menu)
  149. on_requested_dom_node_tag_context_menu(position, *tag);
  150. } else if (type == "attribute"sv) {
  151. VERIFY(tag.has_value());
  152. VERIFY(attribute.has_value());
  153. if (on_requested_dom_node_attribute_context_menu)
  154. on_requested_dom_node_attribute_context_menu(position, *tag, *attribute);
  155. }
  156. };
  157. m_inspector_web_view.on_inspector_selected_dom_node = [this](auto node_id, auto const& pseudo_element) {
  158. m_content_web_view.inspect_dom_node(node_id, pseudo_element);
  159. };
  160. m_inspector_web_view.on_inspector_set_dom_node_text = [this](auto node_id, auto const& text) {
  161. m_content_web_view.set_dom_node_text(node_id, text);
  162. };
  163. m_inspector_web_view.on_inspector_set_dom_node_tag = [this](auto node_id, auto const& tag) {
  164. m_content_web_view.set_dom_node_tag(node_id, tag);
  165. };
  166. m_inspector_web_view.on_inspector_added_dom_node_attributes = [this](auto node_id, auto const& attributes) {
  167. m_content_web_view.add_dom_node_attributes(node_id, attributes);
  168. };
  169. m_inspector_web_view.on_inspector_replaced_dom_node_attribute = [this](auto node_id, u32 attribute_index, auto const& replacement_attributes) {
  170. auto const& attribute = m_dom_node_attributes.get(node_id)->at(attribute_index);
  171. m_content_web_view.replace_dom_node_attribute(node_id, attribute.name, replacement_attributes);
  172. };
  173. m_inspector_web_view.on_inspector_requested_style_sheet_source = [this](auto const& identifier) {
  174. m_content_web_view.request_style_sheet_source(identifier);
  175. };
  176. m_inspector_web_view.on_inspector_executed_console_script = [this](auto const& script) {
  177. append_console_source(script);
  178. m_content_web_view.js_console_input(script.to_byte_string());
  179. };
  180. m_inspector_web_view.on_inspector_exported_inspector_html = [this](String const& html) {
  181. auto maybe_inspector_path = Application::the().path_for_downloaded_file("inspector"sv);
  182. if (maybe_inspector_path.is_error()) {
  183. append_console_warning(MUST(String::formatted("Unable to select a download location: {}", maybe_inspector_path.error())));
  184. return;
  185. }
  186. auto inspector_path = maybe_inspector_path.release_value();
  187. if (auto result = Core::Directory::create(inspector_path.string(), Core::Directory::CreateDirectories::Yes); result.is_error()) {
  188. append_console_warning(MUST(String::formatted("Unable to create {}: {}", inspector_path, result.error())));
  189. return;
  190. }
  191. auto export_file = [&](auto name, auto const& contents) {
  192. auto path = inspector_path.append(name);
  193. auto file = Core::File::open(path.string(), Core::File::OpenMode::Write);
  194. if (file.is_error()) {
  195. append_console_warning(MUST(String::formatted("Unable to open {}: {}", path, file.error())));
  196. return false;
  197. }
  198. if (auto result = file.value()->write_until_depleted(contents); result.is_error()) {
  199. append_console_warning(MUST(String::formatted("Unable to save {}: {}", path, result.error())));
  200. return false;
  201. }
  202. return true;
  203. };
  204. auto inspector_css = MUST(Core::Resource::load_from_uri(INSPECTOR_CSS));
  205. auto inspector_js = MUST(Core::Resource::load_from_uri(INSPECTOR_JS));
  206. auto inspector_html = MUST(html.replace(INSPECTOR_CSS, "inspector.css"sv, ReplaceMode::All));
  207. inspector_html = MUST(inspector_html.replace(INSPECTOR_JS, "inspector.js"sv, ReplaceMode::All));
  208. if (!export_file("inspector.html"sv, inspector_html))
  209. return;
  210. if (!export_file("inspector.css"sv, inspector_css->data()))
  211. return;
  212. if (!export_file("inspector.js"sv, inspector_js->data()))
  213. return;
  214. append_console_message(MUST(String::formatted("Exported Inspector files to {}", inspector_path)));
  215. };
  216. load_inspector();
  217. }
  218. InspectorClient::~InspectorClient()
  219. {
  220. m_content_web_view.on_finshed_editing_dom_node = nullptr;
  221. m_content_web_view.on_received_accessibility_tree = nullptr;
  222. m_content_web_view.on_received_console_message = nullptr;
  223. m_content_web_view.on_received_console_messages = nullptr;
  224. m_content_web_view.on_received_dom_node_html = nullptr;
  225. m_content_web_view.on_received_dom_node_properties = nullptr;
  226. m_content_web_view.on_received_dom_tree = nullptr;
  227. m_content_web_view.on_received_hovered_node_id = nullptr;
  228. m_content_web_view.on_received_style_sheet_list = nullptr;
  229. m_content_web_view.on_inspector_requested_style_sheet_source = nullptr;
  230. }
  231. void InspectorClient::inspect()
  232. {
  233. if (!m_inspector_loaded)
  234. return;
  235. m_content_web_view.inspect_dom_tree();
  236. m_content_web_view.inspect_accessibility_tree();
  237. m_content_web_view.list_style_sheets();
  238. }
  239. void InspectorClient::reset()
  240. {
  241. static constexpr auto script = "inspector.reset();"sv;
  242. m_inspector_web_view.run_javascript(script);
  243. m_body_node_id.clear();
  244. m_pending_selection.clear();
  245. m_dom_tree_loaded = false;
  246. m_dom_node_attributes.clear();
  247. m_highest_notified_message_index = -1;
  248. m_highest_received_message_index = -1;
  249. m_waiting_for_messages = false;
  250. }
  251. void InspectorClient::select_hovered_node()
  252. {
  253. m_content_web_view.get_hovered_node_id();
  254. }
  255. void InspectorClient::select_default_node()
  256. {
  257. if (m_body_node_id.has_value())
  258. select_node(*m_body_node_id);
  259. }
  260. void InspectorClient::clear_selection()
  261. {
  262. m_content_web_view.clear_inspected_dom_node();
  263. static constexpr auto script = "inspector.clearInspectedDOMNode();"sv;
  264. m_inspector_web_view.run_javascript(script);
  265. }
  266. void InspectorClient::select_node(i32 node_id)
  267. {
  268. if (!m_dom_tree_loaded) {
  269. m_pending_selection = node_id;
  270. return;
  271. }
  272. auto script = MUST(String::formatted("inspector.inspectDOMNodeID({});", node_id));
  273. m_inspector_web_view.run_javascript(script);
  274. }
  275. void InspectorClient::context_menu_edit_dom_node()
  276. {
  277. VERIFY(m_context_menu_data.has_value());
  278. auto script = MUST(String::formatted("inspector.editDOMNodeID({});", m_context_menu_data->dom_node_id));
  279. m_inspector_web_view.run_javascript(script);
  280. m_context_menu_data.clear();
  281. }
  282. void InspectorClient::context_menu_copy_dom_node()
  283. {
  284. VERIFY(m_context_menu_data.has_value());
  285. m_content_web_view.get_dom_node_html(m_context_menu_data->dom_node_id);
  286. m_context_menu_data.clear();
  287. }
  288. void InspectorClient::context_menu_screenshot_dom_node()
  289. {
  290. VERIFY(m_context_menu_data.has_value());
  291. m_content_web_view.take_dom_node_screenshot(m_context_menu_data->dom_node_id)
  292. ->when_resolved([this](auto const& path) {
  293. append_console_message(MUST(String::formatted("Screenshot saved to: {}", path)));
  294. })
  295. .when_rejected([this](auto const& error) {
  296. append_console_warning(MUST(String::formatted("Warning: {}", error)));
  297. });
  298. m_context_menu_data.clear();
  299. }
  300. void InspectorClient::context_menu_create_child_element()
  301. {
  302. VERIFY(m_context_menu_data.has_value());
  303. m_content_web_view.create_child_element(m_context_menu_data->dom_node_id);
  304. m_context_menu_data.clear();
  305. }
  306. void InspectorClient::context_menu_create_child_text_node()
  307. {
  308. VERIFY(m_context_menu_data.has_value());
  309. m_content_web_view.create_child_text_node(m_context_menu_data->dom_node_id);
  310. m_context_menu_data.clear();
  311. }
  312. void InspectorClient::context_menu_clone_dom_node()
  313. {
  314. VERIFY(m_context_menu_data.has_value());
  315. m_content_web_view.clone_dom_node(m_context_menu_data->dom_node_id);
  316. m_context_menu_data.clear();
  317. }
  318. void InspectorClient::context_menu_remove_dom_node()
  319. {
  320. VERIFY(m_context_menu_data.has_value());
  321. m_content_web_view.remove_dom_node(m_context_menu_data->dom_node_id);
  322. m_context_menu_data.clear();
  323. }
  324. void InspectorClient::context_menu_add_dom_node_attribute()
  325. {
  326. VERIFY(m_context_menu_data.has_value());
  327. auto script = MUST(String::formatted("inspector.addAttributeToDOMNodeID({});", m_context_menu_data->dom_node_id));
  328. m_inspector_web_view.run_javascript(script);
  329. m_context_menu_data.clear();
  330. }
  331. void InspectorClient::context_menu_remove_dom_node_attribute()
  332. {
  333. VERIFY(m_context_menu_data.has_value());
  334. VERIFY(m_context_menu_data->attribute.has_value());
  335. m_content_web_view.replace_dom_node_attribute(m_context_menu_data->dom_node_id, m_context_menu_data->attribute->name, {});
  336. m_context_menu_data.clear();
  337. }
  338. void InspectorClient::context_menu_copy_dom_node_attribute_value()
  339. {
  340. VERIFY(m_context_menu_data.has_value());
  341. VERIFY(m_context_menu_data->attribute.has_value());
  342. if (m_content_web_view.on_insert_clipboard_entry)
  343. m_content_web_view.on_insert_clipboard_entry(m_context_menu_data->attribute->value, "unspecified"_string, "text/plain"_string);
  344. m_context_menu_data.clear();
  345. }
  346. void InspectorClient::load_inspector()
  347. {
  348. auto inspector_html = MUST(Core::Resource::load_from_uri(INSPECTOR_HTML));
  349. auto generate_property_table = [&](auto name) {
  350. return MUST(String::formatted(R"~~~(
  351. <div id="{0}" class="tab-content">
  352. <table class="property-table">
  353. <thead>
  354. <tr>
  355. <th>Name</th>
  356. <th>Value</th>
  357. </tr>
  358. </thead>
  359. <tbody id="{0}-table">
  360. </tbody>
  361. </table>
  362. </div>
  363. )~~~",
  364. name));
  365. };
  366. StringBuilder builder;
  367. SourceGenerator generator { builder };
  368. generator.set("INSPECTOR_CSS"sv, INSPECTOR_CSS);
  369. generator.set("INSPECTOR_JS"sv, INSPECTOR_JS);
  370. generator.set("INSPECTOR_STYLE"sv, HTML_HIGHLIGHTER_STYLE);
  371. generator.set("COMPUTED_STYLE"sv, generate_property_table("computed-style"sv));
  372. generator.set("RESOVLED_STYLE"sv, generate_property_table("resolved-style"sv));
  373. generator.set("CUSTOM_PROPERTIES"sv, generate_property_table("custom-properties"sv));
  374. generator.append(inspector_html->data());
  375. m_inspector_web_view.load_html(generator.as_string_view());
  376. }
  377. template<typename Generator>
  378. static void generate_tree(StringBuilder& builder, JsonObject const& node, Generator&& generator)
  379. {
  380. if (auto children = node.get_array("children"sv); children.has_value() && !children->is_empty()) {
  381. auto name = node.get_byte_string("name"sv).value_or({});
  382. builder.append("<details>"sv);
  383. builder.append("<summary>"sv);
  384. generator(node);
  385. builder.append("</summary>"sv);
  386. children->for_each([&](auto const& child) {
  387. builder.append("<div>"sv);
  388. generate_tree(builder, child.as_object(), generator);
  389. builder.append("</div>"sv);
  390. });
  391. builder.append("</details>"sv);
  392. } else {
  393. generator(node);
  394. }
  395. }
  396. String InspectorClient::generate_dom_tree(JsonObject const& dom_tree)
  397. {
  398. StringBuilder builder;
  399. generate_tree(builder, dom_tree, [&](JsonObject const& node) {
  400. auto type = node.get_byte_string("type"sv).value_or("unknown"sv);
  401. auto name = node.get_byte_string("name"sv).value_or({});
  402. StringBuilder data_attributes;
  403. auto append_data_attribute = [&](auto name, auto value) {
  404. if (!data_attributes.is_empty())
  405. data_attributes.append(' ');
  406. data_attributes.appendff("data-{}=\"{}\"", name, value);
  407. };
  408. i32 node_id = 0;
  409. if (auto pseudo_element = node.get_integer<i32>("pseudo-element"sv); pseudo_element.has_value()) {
  410. node_id = node.get_integer<i32>("parent-id"sv).value();
  411. append_data_attribute("pseudo-element"sv, *pseudo_element);
  412. } else {
  413. node_id = node.get_integer<i32>("id"sv).value();
  414. }
  415. append_data_attribute("id"sv, node_id);
  416. if (type == "text"sv) {
  417. auto deprecated_text = node.get_byte_string("text"sv).release_value();
  418. deprecated_text = escape_html_entities(deprecated_text);
  419. auto text = MUST(Web::Infra::strip_and_collapse_whitespace(deprecated_text));
  420. builder.appendff("<span data-node-type=\"text\" class=\"hoverable editable\" {}>", data_attributes.string_view());
  421. if (text.is_empty())
  422. builder.appendff("<span class=\"internal\">{}</span>", name);
  423. else
  424. builder.append(text);
  425. builder.append("</span>"sv);
  426. return;
  427. }
  428. if (type == "comment"sv) {
  429. auto comment = node.get_byte_string("data"sv).release_value();
  430. comment = escape_html_entities(comment);
  431. builder.appendff("<span class=\"hoverable comment\" {}>", data_attributes.string_view());
  432. builder.append("<span>&lt;!--</span>"sv);
  433. builder.appendff("<span data-node-type=\"comment\" class=\"editable\">{}</span>", comment);
  434. builder.append("<span>--&gt;</span>"sv);
  435. builder.append("</span>"sv);
  436. return;
  437. }
  438. if (type == "shadow-root"sv) {
  439. auto mode = node.get_byte_string("mode"sv).release_value();
  440. builder.appendff("<span class=\"hoverable internal\" {}>", data_attributes.string_view());
  441. builder.appendff("{} ({})", name, mode);
  442. builder.append("</span>"sv);
  443. return;
  444. }
  445. if (type != "element"sv) {
  446. builder.appendff("<span class=\"hoverable internal\" {}>", data_attributes.string_view());
  447. builder.appendff(name);
  448. builder.append("</span>"sv);
  449. return;
  450. }
  451. if (name.equals_ignoring_ascii_case("BODY"sv))
  452. m_body_node_id = node_id;
  453. auto tag = name.to_lowercase();
  454. builder.appendff("<span class=\"hoverable\" {}>", data_attributes.string_view());
  455. builder.append("<span>&lt;</span>"sv);
  456. builder.appendff("<span data-node-type=\"tag\" data-tag=\"{0}\" class=\"editable tag\">{0}</span>", tag);
  457. if (auto attributes = node.get_object("attributes"sv); attributes.has_value()) {
  458. attributes->for_each_member([&](auto const& name, auto const& value) {
  459. auto& dom_node_attributes = m_dom_node_attributes.ensure(node_id);
  460. auto value_string = value.as_string();
  461. builder.append("&nbsp;"sv);
  462. builder.appendff("<span data-node-type=\"attribute\" data-tag=\"{}\" data-attribute-index={} class=\"editable\">", tag, dom_node_attributes.size());
  463. builder.appendff("<span class=\"attribute-name\">{}</span>", escape_html_entities(name));
  464. builder.append('=');
  465. builder.appendff("<span class=\"attribute-value\">\"{}\"</span>", escape_html_entities(value_string));
  466. builder.append("</span>"sv);
  467. dom_node_attributes.empend(MUST(String::from_byte_string(name)), MUST(String::from_byte_string(value_string)));
  468. });
  469. }
  470. builder.append("<span>&gt;</span>"sv);
  471. builder.append("</span>"sv);
  472. });
  473. return MUST(builder.to_string());
  474. }
  475. String InspectorClient::generate_accessibility_tree(JsonObject const& accessibility_tree)
  476. {
  477. StringBuilder builder;
  478. generate_tree(builder, accessibility_tree, [&](JsonObject const& node) {
  479. auto type = node.get_byte_string("type"sv).value_or("unknown"sv);
  480. auto role = node.get_byte_string("role"sv).value_or({});
  481. if (type == "text"sv) {
  482. auto text = node.get_byte_string("text"sv).release_value();
  483. text = escape_html_entities(text);
  484. builder.appendff("<span class=\"hoverable\">");
  485. builder.append(MUST(Web::Infra::strip_and_collapse_whitespace(text)));
  486. builder.append("</span>"sv);
  487. return;
  488. }
  489. if (type != "element"sv) {
  490. builder.appendff("<span class=\"hoverable internal\">");
  491. builder.appendff(role.to_lowercase());
  492. builder.append("</span>"sv);
  493. return;
  494. }
  495. auto name = node.get_byte_string("name"sv).value_or({});
  496. auto description = node.get_byte_string("description"sv).value_or({});
  497. builder.appendff("<span class=\"hoverable\">");
  498. builder.append(role.to_lowercase());
  499. builder.appendff(" name: \"{}\", description: \"{}\"", name, description);
  500. builder.append("</span>"sv);
  501. });
  502. return MUST(builder.to_string());
  503. }
  504. void InspectorClient::request_console_messages()
  505. {
  506. VERIFY(!m_waiting_for_messages);
  507. m_content_web_view.js_console_request_messages(m_highest_received_message_index + 1);
  508. m_waiting_for_messages = true;
  509. }
  510. void InspectorClient::handle_console_message(i32 message_index)
  511. {
  512. if (message_index <= m_highest_received_message_index) {
  513. dbgln("Notified about console message we already have");
  514. return;
  515. }
  516. if (message_index <= m_highest_notified_message_index) {
  517. dbgln("Notified about console message we're already aware of");
  518. return;
  519. }
  520. m_highest_notified_message_index = message_index;
  521. if (!m_waiting_for_messages)
  522. request_console_messages();
  523. }
  524. void InspectorClient::handle_console_messages(i32 start_index, ReadonlySpan<ByteString> message_types, ReadonlySpan<ByteString> messages)
  525. {
  526. auto end_index = start_index + static_cast<i32>(message_types.size()) - 1;
  527. if (end_index <= m_highest_received_message_index) {
  528. dbgln("Received old console messages");
  529. return;
  530. }
  531. for (size_t i = 0; i < message_types.size(); ++i) {
  532. auto const& type = message_types[i];
  533. auto const& message = messages[i];
  534. if (type == "html"sv)
  535. append_console_output(message);
  536. else if (type == "clear"sv)
  537. clear_console_output();
  538. else if (type == "group"sv)
  539. begin_console_group(message, true);
  540. else if (type == "groupCollapsed"sv)
  541. begin_console_group(message, false);
  542. else if (type == "groupEnd"sv)
  543. end_console_group();
  544. else
  545. VERIFY_NOT_REACHED();
  546. }
  547. m_highest_received_message_index = end_index;
  548. m_waiting_for_messages = false;
  549. if (m_highest_received_message_index < m_highest_notified_message_index)
  550. request_console_messages();
  551. }
  552. void InspectorClient::append_console_source(StringView source)
  553. {
  554. StringBuilder builder;
  555. builder.append("<span class=\"console-prompt\">&gt;&nbsp;</span>"sv);
  556. builder.append(MUST(JS::MarkupGenerator::html_from_source(source)));
  557. append_console_output(builder.string_view());
  558. }
  559. void InspectorClient::append_console_message(StringView message)
  560. {
  561. StringBuilder builder;
  562. builder.append("<span class=\"console-prompt\">#&nbsp;</span>"sv);
  563. builder.appendff("<span class=\"console-message\">{}</span>", message);
  564. append_console_output(builder.string_view());
  565. }
  566. void InspectorClient::append_console_warning(StringView warning)
  567. {
  568. StringBuilder builder;
  569. builder.append("<span class=\"console-prompt\">#&nbsp;</span>"sv);
  570. builder.appendff("<span class=\"console-warning\">{}</span>", warning);
  571. append_console_output(builder.string_view());
  572. }
  573. void InspectorClient::append_console_output(StringView html)
  574. {
  575. auto html_base64 = MUST(encode_base64(html.bytes()));
  576. auto script = MUST(String::formatted("inspector.appendConsoleOutput(\"{}\");", html_base64));
  577. m_inspector_web_view.run_javascript(script);
  578. }
  579. void InspectorClient::clear_console_output()
  580. {
  581. static constexpr auto script = "inspector.clearConsoleOutput();"sv;
  582. m_inspector_web_view.run_javascript(script);
  583. }
  584. void InspectorClient::begin_console_group(StringView label, bool start_expanded)
  585. {
  586. auto label_base64 = MUST(encode_base64(label.bytes()));
  587. auto script = MUST(String::formatted("inspector.beginConsoleGroup(\"{}\", {});", label_base64, start_expanded));
  588. m_inspector_web_view.run_javascript(script);
  589. }
  590. void InspectorClient::end_console_group()
  591. {
  592. static constexpr auto script = "inspector.endConsoleGroup();"sv;
  593. m_inspector_web_view.run_javascript(script);
  594. }
  595. }