Dump.cpp 26 KB

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