Dump.cpp 31 KB

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