Dump.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681
  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)) {
  62. if (auto* shadow_root = static_cast<DOM::Element const&>(node).shadow_root_internal()) {
  63. dump_tree(builder, *shadow_root);
  64. }
  65. }
  66. if (is<DOM::ParentNode>(node)) {
  67. if (!is<HTML::HTMLTemplateElement>(node)) {
  68. static_cast<DOM::ParentNode const&>(node).for_each_child([&](auto& child) {
  69. dump_tree(builder, child);
  70. });
  71. } else {
  72. auto& template_element = verify_cast<HTML::HTMLTemplateElement>(node);
  73. dump_tree(builder, template_element.content());
  74. }
  75. }
  76. --indent;
  77. }
  78. void dump_tree(Layout::Node const& layout_node, bool show_box_model, bool show_specified_style)
  79. {
  80. StringBuilder builder;
  81. dump_tree(builder, layout_node, show_box_model, show_specified_style, true);
  82. dbgln("{}", builder.string_view());
  83. }
  84. void dump_tree(StringBuilder& builder, Layout::Node const& layout_node, bool show_box_model, bool show_specified_style, bool interactive)
  85. {
  86. static size_t indent = 0;
  87. for (size_t i = 0; i < indent; ++i)
  88. builder.append(" "sv);
  89. DeprecatedFlyString tag_name;
  90. if (layout_node.is_anonymous())
  91. tag_name = "(anonymous)";
  92. else if (is<DOM::Element>(layout_node.dom_node()))
  93. tag_name = verify_cast<DOM::Element>(*layout_node.dom_node()).local_name();
  94. else
  95. tag_name = layout_node.dom_node()->node_name();
  96. DeprecatedString identifier = "";
  97. if (layout_node.dom_node() && is<DOM::Element>(*layout_node.dom_node())) {
  98. auto& element = verify_cast<DOM::Element>(*layout_node.dom_node());
  99. StringBuilder builder;
  100. auto id = element.attribute(HTML::AttributeNames::id);
  101. if (!id.is_empty()) {
  102. builder.append('#');
  103. builder.append(id);
  104. }
  105. for (auto& class_name : element.class_names()) {
  106. builder.append('.');
  107. builder.append(class_name);
  108. }
  109. identifier = builder.to_deprecated_string();
  110. }
  111. StringView nonbox_color_on = ""sv;
  112. StringView box_color_on = ""sv;
  113. StringView svg_box_color_on = ""sv;
  114. StringView positioned_color_on = ""sv;
  115. StringView floating_color_on = ""sv;
  116. StringView inline_color_on = ""sv;
  117. StringView line_box_color_on = ""sv;
  118. StringView fragment_color_on = ""sv;
  119. StringView flex_color_on = ""sv;
  120. StringView color_off = ""sv;
  121. if (interactive) {
  122. nonbox_color_on = "\033[33m"sv;
  123. box_color_on = "\033[34m"sv;
  124. svg_box_color_on = "\033[31m"sv;
  125. positioned_color_on = "\033[31;1m"sv;
  126. floating_color_on = "\033[32;1m"sv;
  127. inline_color_on = "\033[36;1m"sv;
  128. line_box_color_on = "\033[34;1m"sv;
  129. fragment_color_on = "\033[35;1m"sv;
  130. flex_color_on = "\033[34;1m"sv;
  131. color_off = "\033[0m"sv;
  132. }
  133. if (!is<Layout::Box>(layout_node)) {
  134. builder.appendff("{}{}{} <{}{}{}{}>",
  135. nonbox_color_on,
  136. layout_node.class_name(),
  137. color_off,
  138. tag_name,
  139. nonbox_color_on,
  140. identifier,
  141. color_off);
  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(),
  149. color_off,
  150. color_on,
  151. tag_name,
  152. color_off,
  153. identifier.characters());
  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_color_on, color_off);
  167. if (box.is_inline_table())
  168. builder.appendff(" {}inline-table{}", inline_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. builder.appendff("start: {}, length: {}, rect: {}\n",
  244. fragment.start(),
  245. fragment.length(),
  246. fragment.absolute_rect());
  247. if (is<Layout::TextNode>(fragment.layout_node())) {
  248. for (size_t i = 0; i < indent; ++i)
  249. builder.append(" "sv);
  250. auto& layout_text = static_cast<Layout::TextNode const&>(fragment.layout_node());
  251. auto fragment_text = layout_text.text_for_rendering().substring(fragment.start(), fragment.length());
  252. builder.appendff(" \"{}\"\n", fragment_text);
  253. }
  254. }
  255. }
  256. }
  257. 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()) {
  258. struct NameAndValue {
  259. DeprecatedString name;
  260. DeprecatedString value;
  261. };
  262. Vector<NameAndValue> properties;
  263. verify_cast<DOM::Element>(*layout_node.dom_node()).computed_css_values()->for_each_property([&](auto property_id, auto& value) {
  264. properties.append({ CSS::string_from_property_id(property_id), value.to_string().release_value_but_fixme_should_propagate_errors().to_deprecated_string() });
  265. });
  266. quick_sort(properties, [](auto& a, auto& b) { return a.name < b.name; });
  267. for (auto& property : properties) {
  268. for (size_t i = 0; i < indent; ++i)
  269. builder.append(" "sv);
  270. builder.appendff(" ({}: {})\n", property.name, property.value);
  271. }
  272. }
  273. ++indent;
  274. layout_node.for_each_child([&](auto& child) {
  275. dump_tree(builder, child, show_box_model, show_specified_style, interactive);
  276. });
  277. --indent;
  278. }
  279. void dump_selector(CSS::Selector const& selector)
  280. {
  281. StringBuilder builder;
  282. dump_selector(builder, selector);
  283. dbgln("{}", builder.string_view());
  284. }
  285. void dump_selector(StringBuilder& builder, CSS::Selector const& selector)
  286. {
  287. builder.append(" CSS::Selector:\n"sv);
  288. for (auto& relative_selector : selector.compound_selectors()) {
  289. builder.append(" "sv);
  290. char const* relation_description = "";
  291. switch (relative_selector.combinator) {
  292. case CSS::Selector::Combinator::None:
  293. relation_description = "None";
  294. break;
  295. case CSS::Selector::Combinator::ImmediateChild:
  296. relation_description = "ImmediateChild";
  297. break;
  298. case CSS::Selector::Combinator::Descendant:
  299. relation_description = "Descendant";
  300. break;
  301. case CSS::Selector::Combinator::NextSibling:
  302. relation_description = "AdjacentSibling";
  303. break;
  304. case CSS::Selector::Combinator::SubsequentSibling:
  305. relation_description = "GeneralSibling";
  306. break;
  307. case CSS::Selector::Combinator::Column:
  308. relation_description = "Column";
  309. break;
  310. }
  311. if (*relation_description)
  312. builder.appendff("{{{}}} ", relation_description);
  313. for (size_t i = 0; i < relative_selector.simple_selectors.size(); ++i) {
  314. auto& simple_selector = relative_selector.simple_selectors[i];
  315. char const* type_description = "Unknown";
  316. switch (simple_selector.type) {
  317. case CSS::Selector::SimpleSelector::Type::Universal:
  318. type_description = "Universal";
  319. break;
  320. case CSS::Selector::SimpleSelector::Type::Id:
  321. type_description = "Id";
  322. break;
  323. case CSS::Selector::SimpleSelector::Type::Class:
  324. type_description = "Class";
  325. break;
  326. case CSS::Selector::SimpleSelector::Type::TagName:
  327. type_description = "TagName";
  328. break;
  329. case CSS::Selector::SimpleSelector::Type::Attribute:
  330. type_description = "Attribute";
  331. break;
  332. case CSS::Selector::SimpleSelector::Type::PseudoClass:
  333. type_description = "PseudoClass";
  334. break;
  335. case CSS::Selector::SimpleSelector::Type::PseudoElement:
  336. type_description = "PseudoElement";
  337. break;
  338. }
  339. builder.appendff("{}:", type_description);
  340. // FIXME: This is goofy
  341. if (simple_selector.value.has<CSS::Selector::SimpleSelector::Name>())
  342. builder.append(simple_selector.name());
  343. if (simple_selector.type == CSS::Selector::SimpleSelector::Type::PseudoClass) {
  344. auto const& pseudo_class = simple_selector.pseudo_class();
  345. char const* pseudo_class_description = "";
  346. switch (pseudo_class.type) {
  347. case CSS::Selector::SimpleSelector::PseudoClass::Type::Link:
  348. pseudo_class_description = "Link";
  349. break;
  350. case CSS::Selector::SimpleSelector::PseudoClass::Type::Visited:
  351. pseudo_class_description = "Visited";
  352. break;
  353. case CSS::Selector::SimpleSelector::PseudoClass::Type::Active:
  354. pseudo_class_description = "Active";
  355. break;
  356. case CSS::Selector::SimpleSelector::PseudoClass::Type::Root:
  357. pseudo_class_description = "Root";
  358. break;
  359. case CSS::Selector::SimpleSelector::PseudoClass::Type::FirstOfType:
  360. pseudo_class_description = "FirstOfType";
  361. break;
  362. case CSS::Selector::SimpleSelector::PseudoClass::Type::LastOfType:
  363. pseudo_class_description = "LastOfType";
  364. break;
  365. case CSS::Selector::SimpleSelector::PseudoClass::Type::OnlyOfType:
  366. pseudo_class_description = "OnlyOfType";
  367. break;
  368. case CSS::Selector::SimpleSelector::PseudoClass::Type::NthOfType:
  369. pseudo_class_description = "NthOfType";
  370. break;
  371. case CSS::Selector::SimpleSelector::PseudoClass::Type::NthLastOfType:
  372. pseudo_class_description = "NthLastOfType";
  373. break;
  374. case CSS::Selector::SimpleSelector::PseudoClass::Type::NthChild:
  375. pseudo_class_description = "NthChild";
  376. break;
  377. case CSS::Selector::SimpleSelector::PseudoClass::Type::NthLastChild:
  378. pseudo_class_description = "NthLastChild";
  379. break;
  380. case CSS::Selector::SimpleSelector::PseudoClass::Type::Focus:
  381. pseudo_class_description = "Focus";
  382. break;
  383. case CSS::Selector::SimpleSelector::PseudoClass::Type::FocusWithin:
  384. pseudo_class_description = "FocusWithin";
  385. break;
  386. case CSS::Selector::SimpleSelector::PseudoClass::Type::Empty:
  387. pseudo_class_description = "Empty";
  388. break;
  389. case CSS::Selector::SimpleSelector::PseudoClass::Type::Hover:
  390. pseudo_class_description = "Hover";
  391. break;
  392. case CSS::Selector::SimpleSelector::PseudoClass::Type::LastChild:
  393. pseudo_class_description = "LastChild";
  394. break;
  395. case CSS::Selector::SimpleSelector::PseudoClass::Type::FirstChild:
  396. pseudo_class_description = "FirstChild";
  397. break;
  398. case CSS::Selector::SimpleSelector::PseudoClass::Type::OnlyChild:
  399. pseudo_class_description = "OnlyChild";
  400. break;
  401. case CSS::Selector::SimpleSelector::PseudoClass::Type::Disabled:
  402. pseudo_class_description = "Disabled";
  403. break;
  404. case CSS::Selector::SimpleSelector::PseudoClass::Type::Enabled:
  405. pseudo_class_description = "Enabled";
  406. break;
  407. case CSS::Selector::SimpleSelector::PseudoClass::Type::Checked:
  408. pseudo_class_description = "Checked";
  409. break;
  410. case CSS::Selector::SimpleSelector::PseudoClass::Type::Not:
  411. pseudo_class_description = "Not";
  412. break;
  413. case CSS::Selector::SimpleSelector::PseudoClass::Type::Is:
  414. pseudo_class_description = "Is";
  415. break;
  416. case CSS::Selector::SimpleSelector::PseudoClass::Type::Where:
  417. pseudo_class_description = "Where";
  418. break;
  419. case CSS::Selector::SimpleSelector::PseudoClass::Type::Lang:
  420. pseudo_class_description = "Lang";
  421. break;
  422. }
  423. builder.appendff(" pseudo_class={}", pseudo_class_description);
  424. if (pseudo_class.type == CSS::Selector::SimpleSelector::PseudoClass::Type::Lang) {
  425. builder.append('(');
  426. builder.join(',', pseudo_class.languages);
  427. builder.append(')');
  428. } else if (pseudo_class.type == CSS::Selector::SimpleSelector::PseudoClass::Type::Not
  429. || pseudo_class.type == CSS::Selector::SimpleSelector::PseudoClass::Type::Is
  430. || pseudo_class.type == CSS::Selector::SimpleSelector::PseudoClass::Type::Where) {
  431. builder.append("(["sv);
  432. for (auto& selector : pseudo_class.argument_selector_list)
  433. dump_selector(builder, selector);
  434. builder.append("])"sv);
  435. } else if ((pseudo_class.type == CSS::Selector::SimpleSelector::PseudoClass::Type::NthChild)
  436. || (pseudo_class.type == CSS::Selector::SimpleSelector::PseudoClass::Type::NthLastChild)
  437. || (pseudo_class.type == CSS::Selector::SimpleSelector::PseudoClass::Type::NthOfType)
  438. || (pseudo_class.type == CSS::Selector::SimpleSelector::PseudoClass::Type::NthLastOfType)) {
  439. builder.appendff("(step={}, offset={}", pseudo_class.nth_child_pattern.step_size, pseudo_class.nth_child_pattern.offset);
  440. if (!pseudo_class.argument_selector_list.is_empty()) {
  441. builder.append(", selectors=["sv);
  442. for (auto const& child_selector : pseudo_class.argument_selector_list)
  443. dump_selector(builder, child_selector);
  444. builder.append("]"sv);
  445. }
  446. builder.append(")"sv);
  447. }
  448. }
  449. if (simple_selector.type == CSS::Selector::SimpleSelector::Type::PseudoElement) {
  450. char const* pseudo_element_description = "";
  451. switch (simple_selector.pseudo_element()) {
  452. case CSS::Selector::PseudoElement::Before:
  453. pseudo_element_description = "before";
  454. break;
  455. case CSS::Selector::PseudoElement::After:
  456. pseudo_element_description = "after";
  457. break;
  458. case CSS::Selector::PseudoElement::FirstLine:
  459. pseudo_element_description = "first-line";
  460. break;
  461. case CSS::Selector::PseudoElement::FirstLetter:
  462. pseudo_element_description = "first-letter";
  463. break;
  464. case CSS::Selector::PseudoElement::Marker:
  465. pseudo_element_description = "marker";
  466. break;
  467. case CSS::Selector::PseudoElement::ProgressBar:
  468. pseudo_element_description = "-webkit-progress-bar";
  469. break;
  470. case CSS::Selector::PseudoElement::ProgressValue:
  471. pseudo_element_description = "-webkit-progress-value";
  472. break;
  473. case CSS::Selector::PseudoElement::Placeholder:
  474. pseudo_element_description = "placeholder";
  475. break;
  476. case CSS::Selector::PseudoElement::PseudoElementCount:
  477. VERIFY_NOT_REACHED();
  478. break;
  479. }
  480. builder.appendff(" pseudo_element={}", pseudo_element_description);
  481. }
  482. if (simple_selector.type == CSS::Selector::SimpleSelector::Type::Attribute) {
  483. auto const& attribute = simple_selector.attribute();
  484. char const* attribute_match_type_description = "";
  485. switch (attribute.match_type) {
  486. case CSS::Selector::SimpleSelector::Attribute::MatchType::HasAttribute:
  487. attribute_match_type_description = "HasAttribute";
  488. break;
  489. case CSS::Selector::SimpleSelector::Attribute::MatchType::ExactValueMatch:
  490. attribute_match_type_description = "ExactValueMatch";
  491. break;
  492. case CSS::Selector::SimpleSelector::Attribute::MatchType::ContainsWord:
  493. attribute_match_type_description = "ContainsWord";
  494. break;
  495. case CSS::Selector::SimpleSelector::Attribute::MatchType::ContainsString:
  496. attribute_match_type_description = "ContainsString";
  497. break;
  498. case CSS::Selector::SimpleSelector::Attribute::MatchType::StartsWithSegment:
  499. attribute_match_type_description = "StartsWithSegment";
  500. break;
  501. case CSS::Selector::SimpleSelector::Attribute::MatchType::StartsWithString:
  502. attribute_match_type_description = "StartsWithString";
  503. break;
  504. case CSS::Selector::SimpleSelector::Attribute::MatchType::EndsWithString:
  505. attribute_match_type_description = "EndsWithString";
  506. break;
  507. }
  508. builder.appendff(" [{}, name='{}', value='{}']", attribute_match_type_description, attribute.name, attribute.value);
  509. }
  510. if (i != relative_selector.simple_selectors.size() - 1)
  511. builder.append(", "sv);
  512. }
  513. builder.append("\n"sv);
  514. }
  515. }
  516. ErrorOr<void> dump_rule(CSS::CSSRule const& rule)
  517. {
  518. StringBuilder builder;
  519. TRY(dump_rule(builder, rule));
  520. dbgln("{}", builder.string_view());
  521. return {};
  522. }
  523. ErrorOr<void> dump_rule(StringBuilder& builder, CSS::CSSRule const& rule, int indent_levels)
  524. {
  525. indent(builder, indent_levels);
  526. builder.appendff("{}:\n", rule.class_name());
  527. switch (rule.type()) {
  528. case CSS::CSSRule::Type::FontFace:
  529. dump_font_face_rule(builder, verify_cast<CSS::CSSFontFaceRule const>(rule), indent_levels);
  530. break;
  531. case CSS::CSSRule::Type::Import:
  532. dump_import_rule(builder, verify_cast<CSS::CSSImportRule const>(rule), indent_levels);
  533. break;
  534. case CSS::CSSRule::Type::Media:
  535. TRY(dump_media_rule(builder, verify_cast<CSS::CSSMediaRule const>(rule), indent_levels));
  536. break;
  537. case CSS::CSSRule::Type::Style:
  538. TRY(dump_style_rule(builder, verify_cast<CSS::CSSStyleRule const>(rule), indent_levels));
  539. break;
  540. case CSS::CSSRule::Type::Supports:
  541. TRY(dump_supports_rule(builder, verify_cast<CSS::CSSSupportsRule const>(rule), indent_levels));
  542. break;
  543. }
  544. return {};
  545. }
  546. void dump_font_face_rule(StringBuilder& builder, CSS::CSSFontFaceRule const& rule, int indent_levels)
  547. {
  548. auto& font_face = rule.font_face();
  549. indent(builder, indent_levels + 1);
  550. builder.appendff("font-family: {}\n", font_face.font_family());
  551. indent(builder, indent_levels + 1);
  552. builder.append("sources:\n"sv);
  553. for (auto const& source : font_face.sources()) {
  554. indent(builder, indent_levels + 2);
  555. builder.appendff("url={}, format={}\n", source.url, source.format.value_or("???"_short_string));
  556. }
  557. indent(builder, indent_levels + 1);
  558. builder.append("unicode-ranges:\n"sv);
  559. for (auto const& unicode_range : font_face.unicode_ranges()) {
  560. indent(builder, indent_levels + 2);
  561. builder.appendff("{}\n", unicode_range.to_string().release_value_but_fixme_should_propagate_errors());
  562. }
  563. }
  564. void dump_import_rule(StringBuilder& builder, CSS::CSSImportRule const& rule, int indent_levels)
  565. {
  566. indent(builder, indent_levels);
  567. builder.appendff(" Document URL: {}\n", rule.url());
  568. }
  569. ErrorOr<void> dump_media_rule(StringBuilder& builder, CSS::CSSMediaRule const& media, int indent_levels)
  570. {
  571. indent(builder, indent_levels);
  572. builder.appendff(" Media: {}\n Rules ({}):\n", media.condition_text(), media.css_rules().length());
  573. for (auto& rule : media.css_rules())
  574. TRY(dump_rule(builder, rule, indent_levels + 1));
  575. return {};
  576. }
  577. ErrorOr<void> dump_supports_rule(StringBuilder& builder, CSS::CSSSupportsRule const& supports, int indent_levels)
  578. {
  579. indent(builder, indent_levels);
  580. builder.appendff(" Supports: {}\n Rules ({}):\n", supports.condition_text(), supports.css_rules().length());
  581. for (auto& rule : supports.css_rules())
  582. TRY(dump_rule(builder, rule, indent_levels + 1));
  583. return {};
  584. }
  585. ErrorOr<void> dump_style_rule(StringBuilder& builder, CSS::CSSStyleRule const& rule, int indent_levels)
  586. {
  587. for (auto& selector : rule.selectors()) {
  588. dump_selector(builder, selector);
  589. }
  590. indent(builder, indent_levels);
  591. builder.append(" Declarations:\n"sv);
  592. auto& style_declaration = verify_cast<CSS::PropertyOwningCSSStyleDeclaration>(rule.declaration());
  593. for (auto& property : style_declaration.properties()) {
  594. indent(builder, indent_levels);
  595. builder.appendff(" {}: '{}'", CSS::string_from_property_id(property.property_id), TRY(property.value->to_string()));
  596. if (property.important == CSS::Important::Yes)
  597. builder.append(" \033[31;1m!important\033[0m"sv);
  598. builder.append('\n');
  599. }
  600. for (auto& property : style_declaration.custom_properties()) {
  601. indent(builder, indent_levels);
  602. builder.appendff(" {}: '{}'", property.key, TRY(property.value.value->to_string()));
  603. if (property.value.important == CSS::Important::Yes)
  604. builder.append(" \033[31;1m!important\033[0m"sv);
  605. builder.append('\n');
  606. }
  607. return {};
  608. }
  609. ErrorOr<void> dump_sheet(CSS::StyleSheet const& sheet)
  610. {
  611. StringBuilder builder;
  612. TRY(dump_sheet(builder, sheet));
  613. dbgln("{}", builder.string_view());
  614. return {};
  615. }
  616. ErrorOr<void> dump_sheet(StringBuilder& builder, CSS::StyleSheet const& sheet)
  617. {
  618. auto& css_stylesheet = verify_cast<CSS::CSSStyleSheet>(sheet);
  619. builder.appendff("CSSStyleSheet{{{}}}: {} rule(s)\n", &sheet, css_stylesheet.rules().length());
  620. for (auto& rule : css_stylesheet.rules())
  621. TRY(dump_rule(builder, rule));
  622. return {};
  623. }
  624. }