Dump.cpp 27 KB

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