Dump.cpp 28 KB

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