InspectorClient.cpp 28 KB

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