Dump.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, the SerenityOS developers.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/QuickSort.h>
  8. #include <AK/StringBuilder.h>
  9. #include <AK/Utf8View.h>
  10. #include <LibWeb/CSS/CSSImportRule.h>
  11. #include <LibWeb/CSS/CSSRule.h>
  12. #include <LibWeb/CSS/CSSStyleRule.h>
  13. #include <LibWeb/CSS/CSSStyleSheet.h>
  14. #include <LibWeb/CSS/PropertyID.h>
  15. #include <LibWeb/DOM/Comment.h>
  16. #include <LibWeb/DOM/Document.h>
  17. #include <LibWeb/DOM/Element.h>
  18. #include <LibWeb/DOM/ShadowRoot.h>
  19. #include <LibWeb/DOM/Text.h>
  20. #include <LibWeb/Dump.h>
  21. #include <LibWeb/HTML/HTMLTemplateElement.h>
  22. #include <LibWeb/Layout/BlockBox.h>
  23. #include <LibWeb/Layout/Node.h>
  24. #include <LibWeb/Layout/SVGBox.h>
  25. #include <LibWeb/Layout/TextNode.h>
  26. #include <stdio.h>
  27. namespace Web {
  28. void dump_tree(DOM::Node const& node)
  29. {
  30. StringBuilder builder;
  31. dump_tree(builder, node);
  32. dbgln("{}", builder.string_view());
  33. }
  34. void dump_tree(StringBuilder& builder, DOM::Node const& node)
  35. {
  36. static int indent = 0;
  37. for (int i = 0; i < indent; ++i)
  38. builder.append(" ");
  39. if (is<DOM::Element>(node)) {
  40. builder.appendff("<{}", verify_cast<DOM::Element>(node).local_name());
  41. verify_cast<DOM::Element>(node).for_each_attribute([&](auto& name, auto& value) {
  42. builder.appendff(" {}={}", name, value);
  43. });
  44. builder.append(">\n");
  45. } else if (is<DOM::Text>(node)) {
  46. builder.appendff("\"{}\"\n", verify_cast<DOM::Text>(node).data());
  47. } else {
  48. builder.appendff("{}\n", node.node_name());
  49. }
  50. ++indent;
  51. if (is<DOM::Element>(node) && verify_cast<DOM::Element>(node).shadow_root()) {
  52. dump_tree(builder, *verify_cast<DOM::Element>(node).shadow_root());
  53. }
  54. if (is<DOM::ParentNode>(node)) {
  55. if (!is<HTML::HTMLTemplateElement>(node)) {
  56. static_cast<DOM::ParentNode const&>(node).for_each_child([&](auto& child) {
  57. dump_tree(builder, child);
  58. });
  59. } else {
  60. auto& template_element = verify_cast<HTML::HTMLTemplateElement>(node);
  61. dump_tree(builder, template_element.content());
  62. }
  63. }
  64. --indent;
  65. }
  66. void dump_tree(Layout::Node const& layout_node, bool show_box_model, bool show_specified_style)
  67. {
  68. StringBuilder builder;
  69. dump_tree(builder, layout_node, show_box_model, show_specified_style, true);
  70. dbgln("{}", builder.string_view());
  71. }
  72. void dump_tree(StringBuilder& builder, Layout::Node const& layout_node, bool show_box_model, bool show_specified_style, bool interactive)
  73. {
  74. static size_t indent = 0;
  75. for (size_t i = 0; i < indent; ++i)
  76. builder.append(" ");
  77. FlyString tag_name;
  78. if (layout_node.is_anonymous())
  79. tag_name = "(anonymous)";
  80. else if (is<DOM::Element>(layout_node.dom_node()))
  81. tag_name = verify_cast<DOM::Element>(*layout_node.dom_node()).local_name();
  82. else
  83. tag_name = layout_node.dom_node()->node_name();
  84. String identifier = "";
  85. if (layout_node.dom_node() && is<DOM::Element>(*layout_node.dom_node())) {
  86. auto& element = verify_cast<DOM::Element>(*layout_node.dom_node());
  87. StringBuilder builder;
  88. auto id = element.attribute(HTML::AttributeNames::id);
  89. if (!id.is_empty()) {
  90. builder.append('#');
  91. builder.append(id);
  92. }
  93. for (auto& class_name : element.class_names()) {
  94. builder.append('.');
  95. builder.append(class_name);
  96. }
  97. identifier = builder.to_string();
  98. }
  99. char const* nonbox_color_on = "";
  100. char const* box_color_on = "";
  101. char const* svg_box_color_on = "";
  102. char const* positioned_color_on = "";
  103. char const* floating_color_on = "";
  104. char const* inline_block_color_on = "";
  105. char const* line_box_color_on = "";
  106. char const* fragment_color_on = "";
  107. char const* flex_color_on = "";
  108. char const* color_off = "";
  109. if (interactive) {
  110. nonbox_color_on = "\033[33m";
  111. box_color_on = "\033[34m";
  112. svg_box_color_on = "\033[31m";
  113. positioned_color_on = "\033[31;1m";
  114. floating_color_on = "\033[32;1m";
  115. inline_block_color_on = "\033[36;1m";
  116. line_box_color_on = "\033[34;1m";
  117. fragment_color_on = "\033[35;1m";
  118. flex_color_on = "\033[34;1m";
  119. color_off = "\033[0m";
  120. }
  121. if (!is<Layout::Box>(layout_node)) {
  122. builder.appendff("{}{}{} <{}{}{}{}>",
  123. nonbox_color_on,
  124. layout_node.class_name().substring_view(13),
  125. color_off,
  126. tag_name,
  127. nonbox_color_on,
  128. identifier,
  129. color_off);
  130. if (interactive)
  131. builder.appendff(" @{:p}", &layout_node);
  132. builder.append("\n");
  133. } else {
  134. auto& box = verify_cast<Layout::Box>(layout_node);
  135. StringView color_on = is<Layout::SVGBox>(box) ? svg_box_color_on : box_color_on;
  136. builder.appendff("{}{}{} <{}{}{}{}> ",
  137. color_on,
  138. box.class_name().substring_view(13),
  139. color_off,
  140. color_on,
  141. tag_name,
  142. color_off,
  143. identifier.characters());
  144. if (interactive)
  145. builder.appendff("@{:p} ", &layout_node);
  146. builder.appendff("at ({},{}) size {}x{}",
  147. box.absolute_x(),
  148. box.absolute_y(),
  149. box.width(),
  150. box.height());
  151. if (box.is_positioned())
  152. builder.appendff(" {}positioned{}", positioned_color_on, color_off);
  153. if (box.is_floating())
  154. builder.appendff(" {}floating{}", floating_color_on, color_off);
  155. if (box.is_inline_block())
  156. builder.appendff(" {}inline-block{}", inline_block_color_on, color_off);
  157. if (box.computed_values().display() == CSS::Display::Flex)
  158. builder.appendff(" {}flex-container{}", flex_color_on, color_off);
  159. if (box.is_flex_item())
  160. builder.appendff(" {}flex-item{}", flex_color_on, color_off);
  161. if (show_box_model) {
  162. // Dump the horizontal box properties
  163. builder.appendff(" [{}+{}+{} {} {}+{}+{}]",
  164. box.box_model().margin.left,
  165. box.box_model().border.left,
  166. box.box_model().padding.left,
  167. box.width(),
  168. box.box_model().padding.right,
  169. box.box_model().border.right,
  170. box.box_model().margin.right);
  171. // And the vertical box properties
  172. builder.appendff(" [{}+{}+{} {} {}+{}+{}]",
  173. box.box_model().margin.top,
  174. box.box_model().border.top,
  175. box.box_model().padding.top,
  176. box.height(),
  177. box.box_model().padding.bottom,
  178. box.box_model().border.bottom,
  179. box.box_model().margin.bottom);
  180. }
  181. builder.append("\n");
  182. }
  183. if (is<Layout::BlockBox>(layout_node) && static_cast<Layout::BlockBox const&>(layout_node).children_are_inline()) {
  184. auto& block = static_cast<Layout::BlockBox const&>(layout_node);
  185. for (size_t line_box_index = 0; line_box_index < block.line_boxes().size(); ++line_box_index) {
  186. auto& line_box = block.line_boxes()[line_box_index];
  187. for (size_t i = 0; i < indent; ++i)
  188. builder.append(" ");
  189. builder.appendff(" {}line {}{} width: {}\n",
  190. line_box_color_on,
  191. line_box_index,
  192. color_off,
  193. (int)line_box.width());
  194. for (size_t fragment_index = 0; fragment_index < line_box.fragments().size(); ++fragment_index) {
  195. auto& fragment = line_box.fragments()[fragment_index];
  196. for (size_t i = 0; i < indent; ++i)
  197. builder.append(" ");
  198. builder.appendff(" {}frag {}{} from {} ",
  199. fragment_color_on,
  200. fragment_index,
  201. color_off,
  202. fragment.layout_node().class_name());
  203. if (interactive)
  204. builder.appendff("@{:p}, ", &fragment.layout_node());
  205. builder.appendff("start: {}, length: {}, rect: {}\n",
  206. fragment.start(),
  207. fragment.length(),
  208. fragment.absolute_rect().to_string());
  209. if (is<Layout::TextNode>(fragment.layout_node())) {
  210. for (size_t i = 0; i < indent; ++i)
  211. builder.append(" ");
  212. auto& layout_text = static_cast<Layout::TextNode const&>(fragment.layout_node());
  213. auto fragment_text = layout_text.text_for_rendering().substring(fragment.start(), fragment.length());
  214. builder.appendff(" \"{}\"\n", fragment_text);
  215. }
  216. }
  217. }
  218. }
  219. if (show_specified_style && layout_node.dom_node() && layout_node.dom_node()->is_element() && verify_cast<DOM::Element>(layout_node.dom_node())->specified_css_values()) {
  220. struct NameAndValue {
  221. String name;
  222. String value;
  223. };
  224. Vector<NameAndValue> properties;
  225. verify_cast<DOM::Element>(*layout_node.dom_node()).specified_css_values()->for_each_property([&](auto property_id, auto& value) {
  226. properties.append({ CSS::string_from_property_id(property_id), value.to_string() });
  227. });
  228. quick_sort(properties, [](auto& a, auto& b) { return a.name < b.name; });
  229. for (auto& property : properties) {
  230. for (size_t i = 0; i < indent; ++i)
  231. builder.append(" ");
  232. builder.appendff(" ({}: {})\n", property.name, property.value);
  233. }
  234. }
  235. ++indent;
  236. layout_node.for_each_child([&](auto& child) {
  237. dump_tree(builder, child, show_box_model, show_specified_style, interactive);
  238. });
  239. --indent;
  240. }
  241. void dump_selector(CSS::Selector const& selector)
  242. {
  243. StringBuilder builder;
  244. dump_selector(builder, selector);
  245. dbgln("{}", builder.string_view());
  246. }
  247. void dump_selector(StringBuilder& builder, CSS::Selector const& selector)
  248. {
  249. builder.append(" CSS::Selector:\n");
  250. for (auto& relative_selector : selector.compound_selectors()) {
  251. builder.append(" ");
  252. char const* relation_description = "";
  253. switch (relative_selector.combinator) {
  254. case CSS::Selector::Combinator::None:
  255. relation_description = "None";
  256. break;
  257. case CSS::Selector::Combinator::ImmediateChild:
  258. relation_description = "ImmediateChild";
  259. break;
  260. case CSS::Selector::Combinator::Descendant:
  261. relation_description = "Descendant";
  262. break;
  263. case CSS::Selector::Combinator::NextSibling:
  264. relation_description = "AdjacentSibling";
  265. break;
  266. case CSS::Selector::Combinator::SubsequentSibling:
  267. relation_description = "GeneralSibling";
  268. break;
  269. case CSS::Selector::Combinator::Column:
  270. relation_description = "Column";
  271. break;
  272. }
  273. if (*relation_description)
  274. builder.appendff("{{{}}} ", relation_description);
  275. for (size_t i = 0; i < relative_selector.simple_selectors.size(); ++i) {
  276. auto& simple_selector = relative_selector.simple_selectors[i];
  277. char const* type_description = "Unknown";
  278. switch (simple_selector.type) {
  279. case CSS::Selector::SimpleSelector::Type::Invalid:
  280. type_description = "Invalid";
  281. break;
  282. case CSS::Selector::SimpleSelector::Type::Universal:
  283. type_description = "Universal";
  284. break;
  285. case CSS::Selector::SimpleSelector::Type::Id:
  286. type_description = "Id";
  287. break;
  288. case CSS::Selector::SimpleSelector::Type::Class:
  289. type_description = "Class";
  290. break;
  291. case CSS::Selector::SimpleSelector::Type::TagName:
  292. type_description = "TagName";
  293. break;
  294. case CSS::Selector::SimpleSelector::Type::Attribute:
  295. type_description = "Attribute";
  296. break;
  297. case CSS::Selector::SimpleSelector::Type::PseudoClass:
  298. type_description = "PseudoClass";
  299. break;
  300. case CSS::Selector::SimpleSelector::Type::PseudoElement:
  301. type_description = "PseudoElement";
  302. break;
  303. }
  304. builder.appendff("{}:{}", type_description, simple_selector.value);
  305. if (simple_selector.type == CSS::Selector::SimpleSelector::Type::PseudoClass) {
  306. auto const& pseudo_class = simple_selector.pseudo_class;
  307. char const* pseudo_class_description = "";
  308. switch (pseudo_class.type) {
  309. case CSS::Selector::SimpleSelector::PseudoClass::Type::Link:
  310. pseudo_class_description = "Link";
  311. break;
  312. case CSS::Selector::SimpleSelector::PseudoClass::Type::Visited:
  313. pseudo_class_description = "Visited";
  314. break;
  315. case CSS::Selector::SimpleSelector::PseudoClass::Type::Active:
  316. pseudo_class_description = "Active";
  317. break;
  318. case CSS::Selector::SimpleSelector::PseudoClass::Type::None:
  319. pseudo_class_description = "None";
  320. break;
  321. case CSS::Selector::SimpleSelector::PseudoClass::Type::Root:
  322. pseudo_class_description = "Root";
  323. break;
  324. case CSS::Selector::SimpleSelector::PseudoClass::Type::FirstOfType:
  325. pseudo_class_description = "FirstOfType";
  326. break;
  327. case CSS::Selector::SimpleSelector::PseudoClass::Type::LastOfType:
  328. pseudo_class_description = "LastOfType";
  329. break;
  330. case CSS::Selector::SimpleSelector::PseudoClass::Type::NthChild:
  331. pseudo_class_description = "NthChild";
  332. break;
  333. case CSS::Selector::SimpleSelector::PseudoClass::Type::NthLastChild:
  334. pseudo_class_description = "NthLastChild";
  335. break;
  336. case CSS::Selector::SimpleSelector::PseudoClass::Type::Focus:
  337. pseudo_class_description = "Focus";
  338. break;
  339. case CSS::Selector::SimpleSelector::PseudoClass::Type::Empty:
  340. pseudo_class_description = "Empty";
  341. break;
  342. case CSS::Selector::SimpleSelector::PseudoClass::Type::Hover:
  343. pseudo_class_description = "Hover";
  344. break;
  345. case CSS::Selector::SimpleSelector::PseudoClass::Type::LastChild:
  346. pseudo_class_description = "LastChild";
  347. break;
  348. case CSS::Selector::SimpleSelector::PseudoClass::Type::FirstChild:
  349. pseudo_class_description = "FirstChild";
  350. break;
  351. case CSS::Selector::SimpleSelector::PseudoClass::Type::OnlyChild:
  352. pseudo_class_description = "OnlyChild";
  353. break;
  354. case CSS::Selector::SimpleSelector::PseudoClass::Type::Disabled:
  355. pseudo_class_description = "Disabled";
  356. break;
  357. case CSS::Selector::SimpleSelector::PseudoClass::Type::Enabled:
  358. pseudo_class_description = "Enabled";
  359. break;
  360. case CSS::Selector::SimpleSelector::PseudoClass::Type::Checked:
  361. pseudo_class_description = "Checked";
  362. break;
  363. case CSS::Selector::SimpleSelector::PseudoClass::Type::Not:
  364. pseudo_class_description = "Not";
  365. break;
  366. }
  367. builder.appendff(" pseudo_class={}", pseudo_class_description);
  368. if (pseudo_class.type == CSS::Selector::SimpleSelector::PseudoClass::Type::Not) {
  369. builder.append("([");
  370. for (auto& selector : pseudo_class.not_selector)
  371. dump_selector(builder, selector);
  372. builder.append("])");
  373. } else if ((pseudo_class.type == CSS::Selector::SimpleSelector::PseudoClass::Type::NthChild)
  374. || (pseudo_class.type == CSS::Selector::SimpleSelector::PseudoClass::Type::NthLastChild)) {
  375. builder.appendff("(step={}, offset={})", pseudo_class.nth_child_pattern.step_size, pseudo_class.nth_child_pattern.offset);
  376. }
  377. }
  378. if (simple_selector.type == CSS::Selector::SimpleSelector::Type::PseudoElement) {
  379. char const* pseudo_element_description = "";
  380. switch (simple_selector.pseudo_element) {
  381. case CSS::Selector::SimpleSelector::PseudoElement::None:
  382. pseudo_element_description = "NONE";
  383. break;
  384. case CSS::Selector::SimpleSelector::PseudoElement::Before:
  385. pseudo_element_description = "before";
  386. break;
  387. case CSS::Selector::SimpleSelector::PseudoElement::After:
  388. pseudo_element_description = "after";
  389. break;
  390. case CSS::Selector::SimpleSelector::PseudoElement::FirstLine:
  391. pseudo_element_description = "first-line";
  392. break;
  393. case CSS::Selector::SimpleSelector::PseudoElement::FirstLetter:
  394. pseudo_element_description = "first-letter";
  395. break;
  396. }
  397. builder.appendff(" pseudo_element={}", pseudo_element_description);
  398. }
  399. if (simple_selector.type == CSS::Selector::SimpleSelector::Type::Attribute) {
  400. char const* attribute_match_type_description = "";
  401. switch (simple_selector.attribute.match_type) {
  402. case CSS::Selector::SimpleSelector::Attribute::MatchType::None:
  403. attribute_match_type_description = "NONE";
  404. break;
  405. case CSS::Selector::SimpleSelector::Attribute::MatchType::HasAttribute:
  406. attribute_match_type_description = "HasAttribute";
  407. break;
  408. case CSS::Selector::SimpleSelector::Attribute::MatchType::ExactValueMatch:
  409. attribute_match_type_description = "ExactValueMatch";
  410. break;
  411. case CSS::Selector::SimpleSelector::Attribute::MatchType::ContainsWord:
  412. attribute_match_type_description = "ContainsWord";
  413. break;
  414. case CSS::Selector::SimpleSelector::Attribute::MatchType::ContainsString:
  415. attribute_match_type_description = "ContainsString";
  416. break;
  417. case CSS::Selector::SimpleSelector::Attribute::MatchType::StartsWithSegment:
  418. attribute_match_type_description = "StartsWithSegment";
  419. break;
  420. case CSS::Selector::SimpleSelector::Attribute::MatchType::StartsWithString:
  421. attribute_match_type_description = "StartsWithString";
  422. break;
  423. case CSS::Selector::SimpleSelector::Attribute::MatchType::EndsWithString:
  424. attribute_match_type_description = "EndsWithString";
  425. break;
  426. }
  427. builder.appendff(" [{}, name='{}', value='{}']", attribute_match_type_description, simple_selector.attribute.name, simple_selector.attribute.value);
  428. }
  429. if (i != relative_selector.simple_selectors.size() - 1)
  430. builder.append(", ");
  431. }
  432. builder.append("\n");
  433. }
  434. }
  435. void dump_rule(CSS::CSSRule const& rule)
  436. {
  437. StringBuilder builder;
  438. dump_rule(builder, rule);
  439. dbgln("{}", builder.string_view());
  440. }
  441. void dump_rule(StringBuilder& builder, CSS::CSSRule const& rule)
  442. {
  443. builder.appendff("{}:\n", rule.class_name());
  444. switch (rule.type()) {
  445. case CSS::CSSRule::Type::Style:
  446. dump_style_rule(builder, verify_cast<CSS::CSSStyleRule const>(rule));
  447. break;
  448. case CSS::CSSRule::Type::Import:
  449. dump_import_rule(builder, verify_cast<CSS::CSSImportRule const>(rule));
  450. break;
  451. default:
  452. VERIFY_NOT_REACHED();
  453. }
  454. }
  455. void dump_import_rule(StringBuilder& builder, CSS::CSSImportRule const& rule)
  456. {
  457. builder.appendff(" Document URL: {}\n", rule.url());
  458. }
  459. void dump_style_rule(StringBuilder& builder, CSS::CSSStyleRule const& rule)
  460. {
  461. for (auto& selector : rule.selectors()) {
  462. dump_selector(builder, selector);
  463. }
  464. builder.append(" Declarations:\n");
  465. for (auto& property : verify_cast<CSS::PropertyOwningCSSStyleDeclaration>(rule.declaration()).properties()) {
  466. builder.appendff(" {}: '{}'", CSS::string_from_property_id(property.property_id), property.value->to_string());
  467. if (property.important)
  468. builder.append(" \033[31;1m!important\033[0m");
  469. builder.append('\n');
  470. }
  471. }
  472. void dump_sheet(CSS::StyleSheet const& sheet)
  473. {
  474. StringBuilder builder;
  475. dump_sheet(builder, sheet);
  476. dbgln("{}", builder.string_view());
  477. }
  478. void dump_sheet(StringBuilder& builder, CSS::StyleSheet const& sheet)
  479. {
  480. auto& css_stylesheet = verify_cast<CSS::CSSStyleSheet>(sheet);
  481. builder.appendff("CSSStyleSheet{{{}}}: {} rule(s)\n", &sheet, css_stylesheet.rules().length());
  482. for (auto& rule : css_stylesheet.rules()) {
  483. dump_rule(builder, rule);
  484. }
  485. }
  486. }