Dump.cpp 34 KB

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