Dump.cpp 21 KB

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