Dump.cpp 22 KB

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