Dump.cpp 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873
  1. /*
  2. * Copyright (c) 2018-2023, Andreas Kling <andreas@ladybird.org>
  3. * Copyright (c) 2021, the SerenityOS developers.
  4. * Copyright (c) 2021-2024, Sam Atkins <sam@ladybird.org>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include <AK/QuickSort.h>
  9. #include <AK/StringBuilder.h>
  10. #include <AK/Utf8View.h>
  11. #include <LibWeb/CSS/CSSFontFaceRule.h>
  12. #include <LibWeb/CSS/CSSImportRule.h>
  13. #include <LibWeb/CSS/CSSLayerBlockRule.h>
  14. #include <LibWeb/CSS/CSSLayerStatementRule.h>
  15. #include <LibWeb/CSS/CSSMediaRule.h>
  16. #include <LibWeb/CSS/CSSRule.h>
  17. #include <LibWeb/CSS/CSSStyleRule.h>
  18. #include <LibWeb/CSS/CSSStyleSheet.h>
  19. #include <LibWeb/CSS/CSSSupportsRule.h>
  20. #include <LibWeb/CSS/PropertyID.h>
  21. #include <LibWeb/CSS/PseudoClass.h>
  22. #include <LibWeb/DOM/Comment.h>
  23. #include <LibWeb/DOM/Document.h>
  24. #include <LibWeb/DOM/Element.h>
  25. #include <LibWeb/DOM/ShadowRoot.h>
  26. #include <LibWeb/DOM/Text.h>
  27. #include <LibWeb/Dump.h>
  28. #include <LibWeb/HTML/DocumentState.h>
  29. #include <LibWeb/HTML/HTMLImageElement.h>
  30. #include <LibWeb/HTML/HTMLTemplateElement.h>
  31. #include <LibWeb/HTML/ImageRequest.h>
  32. #include <LibWeb/HTML/TraversableNavigable.h>
  33. #include <LibWeb/Layout/BlockContainer.h>
  34. #include <LibWeb/Layout/FormattingContext.h>
  35. #include <LibWeb/Layout/FrameBox.h>
  36. #include <LibWeb/Layout/InlineNode.h>
  37. #include <LibWeb/Layout/Node.h>
  38. #include <LibWeb/Layout/SVGBox.h>
  39. #include <LibWeb/Layout/TextNode.h>
  40. #include <LibWeb/Layout/Viewport.h>
  41. #include <LibWeb/Painting/InlinePaintable.h>
  42. #include <LibWeb/Painting/PaintableBox.h>
  43. #include <LibWeb/Painting/TextPaintable.h>
  44. #include <LibWeb/SVG/SVGDecodedImageData.h>
  45. #include <stdio.h>
  46. namespace Web {
  47. static void indent(StringBuilder& builder, int levels)
  48. {
  49. for (int i = 0; i < levels; i++)
  50. builder.append(" "sv);
  51. }
  52. static void dump_session_history_entry(StringBuilder& builder, HTML::SessionHistoryEntry const& session_history_entry, int indent_levels)
  53. {
  54. indent(builder, indent_levels);
  55. auto const& document = session_history_entry.document();
  56. builder.appendff("step=({}) url=({}) is-active=({})\n", session_history_entry.step().get<int>(), session_history_entry.url(), document && document->is_active());
  57. for (auto const& nested_history : session_history_entry.document_state()->nested_histories()) {
  58. for (auto const& nested_she : nested_history.entries) {
  59. dump_session_history_entry(builder, *nested_she, indent_levels + 1);
  60. }
  61. }
  62. }
  63. void dump_tree(HTML::TraversableNavigable& traversable)
  64. {
  65. StringBuilder builder;
  66. for (auto const& she : traversable.session_history_entries()) {
  67. dump_session_history_entry(builder, *she, 0);
  68. }
  69. dbgln("{}", builder.string_view());
  70. }
  71. void dump_tree(DOM::Node const& node)
  72. {
  73. StringBuilder builder;
  74. dump_tree(builder, node);
  75. dbgln("{}", builder.string_view());
  76. }
  77. void dump_tree(StringBuilder& builder, DOM::Node const& node)
  78. {
  79. static int indent = 0;
  80. for (int i = 0; i < indent; ++i)
  81. builder.append(" "sv);
  82. if (is<DOM::Element>(node)) {
  83. builder.appendff("<{}", verify_cast<DOM::Element>(node).local_name());
  84. verify_cast<DOM::Element>(node).for_each_attribute([&](auto& name, auto& value) {
  85. builder.appendff(" {}={}", name, value);
  86. });
  87. builder.append(">\n"sv);
  88. auto& element = verify_cast<DOM::Element>(node);
  89. if (element.use_pseudo_element().has_value()) {
  90. for (int i = 0; i < indent; ++i)
  91. builder.append(" "sv);
  92. builder.appendff(" (pseudo-element: {})\n", CSS::Selector::PseudoElement::name(element.use_pseudo_element().value()));
  93. }
  94. } else if (is<DOM::Text>(node)) {
  95. builder.appendff("\"{}\"\n", verify_cast<DOM::Text>(node).data());
  96. } else {
  97. builder.appendff("{}\n", node.node_name());
  98. }
  99. ++indent;
  100. if (is<DOM::Element>(node)) {
  101. if (auto shadow_root = static_cast<DOM::Element const&>(node).shadow_root()) {
  102. dump_tree(builder, *shadow_root);
  103. }
  104. }
  105. if (is<HTML::HTMLImageElement>(node)) {
  106. if (auto image_data = static_cast<HTML::HTMLImageElement const&>(node).current_request().image_data()) {
  107. if (is<SVG::SVGDecodedImageData>(*image_data)) {
  108. ++indent;
  109. for (int i = 0; i < indent; ++i)
  110. builder.append(" "sv);
  111. builder.append("(SVG-as-image isolated context)\n"sv);
  112. auto& svg_data = verify_cast<SVG::SVGDecodedImageData>(*image_data);
  113. dump_tree(builder, svg_data.svg_document());
  114. --indent;
  115. }
  116. }
  117. }
  118. if (is<DOM::ParentNode>(node)) {
  119. if (!is<HTML::HTMLTemplateElement>(node)) {
  120. static_cast<DOM::ParentNode const&>(node).for_each_child([&](auto& child) {
  121. dump_tree(builder, child);
  122. return IterationDecision::Continue;
  123. });
  124. } else {
  125. auto& template_element = verify_cast<HTML::HTMLTemplateElement>(node);
  126. dump_tree(builder, template_element.content());
  127. }
  128. }
  129. --indent;
  130. }
  131. void dump_tree(Layout::Node const& layout_node, bool show_box_model, bool show_specified_style)
  132. {
  133. StringBuilder builder;
  134. dump_tree(builder, layout_node, show_box_model, show_specified_style, true);
  135. dbgln("{}", builder.string_view());
  136. }
  137. void dump_tree(StringBuilder& builder, Layout::Node const& layout_node, bool show_box_model, bool show_specified_style, bool interactive)
  138. {
  139. static size_t indent = 0;
  140. for (size_t i = 0; i < indent; ++i)
  141. builder.append(" "sv);
  142. FlyString tag_name;
  143. if (layout_node.is_anonymous())
  144. tag_name = "(anonymous)"_fly_string;
  145. else if (is<DOM::Element>(layout_node.dom_node()))
  146. tag_name = verify_cast<DOM::Element>(*layout_node.dom_node()).local_name();
  147. else
  148. tag_name = layout_node.dom_node()->node_name();
  149. String identifier;
  150. if (layout_node.dom_node() && is<DOM::Element>(*layout_node.dom_node())) {
  151. auto& element = verify_cast<DOM::Element>(*layout_node.dom_node());
  152. StringBuilder builder;
  153. if (element.id().has_value() && !element.id()->is_empty()) {
  154. builder.append('#');
  155. builder.append(*element.id());
  156. }
  157. for (auto& class_name : element.class_names()) {
  158. builder.append('.');
  159. builder.append(class_name);
  160. }
  161. identifier = MUST(builder.to_string());
  162. }
  163. StringView nonbox_color_on = ""sv;
  164. StringView box_color_on = ""sv;
  165. StringView svg_box_color_on = ""sv;
  166. StringView positioned_color_on = ""sv;
  167. StringView floating_color_on = ""sv;
  168. StringView inline_color_on = ""sv;
  169. StringView line_box_color_on = ""sv;
  170. StringView fragment_color_on = ""sv;
  171. StringView flex_color_on = ""sv;
  172. StringView table_color_on = ""sv;
  173. StringView formatting_context_color_on = ""sv;
  174. StringView color_off = ""sv;
  175. if (interactive) {
  176. nonbox_color_on = "\033[33m"sv;
  177. box_color_on = "\033[34m"sv;
  178. svg_box_color_on = "\033[31m"sv;
  179. positioned_color_on = "\033[31;1m"sv;
  180. floating_color_on = "\033[32;1m"sv;
  181. inline_color_on = "\033[36;1m"sv;
  182. line_box_color_on = "\033[34;1m"sv;
  183. fragment_color_on = "\033[35;1m"sv;
  184. flex_color_on = "\033[34;1m"sv;
  185. table_color_on = "\033[91;1m"sv;
  186. formatting_context_color_on = "\033[37;1m"sv;
  187. color_off = "\033[0m"sv;
  188. }
  189. if (!is<Layout::Box>(layout_node)) {
  190. builder.appendff("{}{}{} <{}{}{}{}>",
  191. nonbox_color_on,
  192. layout_node.class_name(),
  193. color_off,
  194. tag_name,
  195. nonbox_color_on,
  196. identifier,
  197. color_off);
  198. builder.append("\n"sv);
  199. } else {
  200. auto& box = verify_cast<Layout::Box>(layout_node);
  201. StringView color_on = is<Layout::SVGBox>(box) ? svg_box_color_on : box_color_on;
  202. builder.appendff("{}{}{} <{}{}{}{}> ",
  203. color_on,
  204. box.class_name(),
  205. color_off,
  206. color_on,
  207. tag_name,
  208. color_off,
  209. identifier);
  210. if (auto const* paintable_box = box.paintable_box()) {
  211. builder.appendff("at ({},{}) content-size {}x{}",
  212. paintable_box->absolute_x(),
  213. paintable_box->absolute_y(),
  214. paintable_box->content_width(),
  215. paintable_box->content_height());
  216. } else {
  217. builder.appendff("(not painted)");
  218. }
  219. if (box.is_positioned())
  220. builder.appendff(" {}positioned{}", positioned_color_on, color_off);
  221. if (box.is_floating())
  222. builder.appendff(" {}floating{}", floating_color_on, color_off);
  223. if (box.is_inline_block())
  224. builder.appendff(" {}inline-block{}", inline_color_on, color_off);
  225. if (box.is_inline_table())
  226. builder.appendff(" {}inline-table{}", inline_color_on, color_off);
  227. if (box.display().is_flex_inside()) {
  228. StringView direction;
  229. switch (box.computed_values().flex_direction()) {
  230. case CSS::FlexDirection::Column:
  231. direction = "column"sv;
  232. break;
  233. case CSS::FlexDirection::ColumnReverse:
  234. direction = "column-reverse"sv;
  235. break;
  236. case CSS::FlexDirection::Row:
  237. direction = "row"sv;
  238. break;
  239. case CSS::FlexDirection::RowReverse:
  240. direction = "row-reverse"sv;
  241. break;
  242. }
  243. builder.appendff(" {}flex-container({}){}", flex_color_on, direction, color_off);
  244. }
  245. if (box.is_flex_item())
  246. builder.appendff(" {}flex-item{}", flex_color_on, color_off);
  247. if (box.display().is_table_inside())
  248. builder.appendff(" {}table-box{}", table_color_on, color_off);
  249. if (box.display().is_table_row_group())
  250. builder.appendff(" {}table-row-group{}", table_color_on, color_off);
  251. if (box.display().is_table_column_group())
  252. builder.appendff(" {}table-column-group{}", table_color_on, color_off);
  253. if (box.display().is_table_header_group())
  254. builder.appendff(" {}table-header-group{}", table_color_on, color_off);
  255. if (box.display().is_table_footer_group())
  256. builder.appendff(" {}table-footer-group{}", table_color_on, color_off);
  257. if (box.display().is_table_row())
  258. builder.appendff(" {}table-row{}", table_color_on, color_off);
  259. if (box.display().is_table_cell())
  260. builder.appendff(" {}table-cell{}", table_color_on, color_off);
  261. if (show_box_model) {
  262. // Dump the horizontal box properties
  263. builder.appendff(" [{}+{}+{} {} {}+{}+{}]",
  264. box.box_model().margin.left,
  265. box.box_model().border.left,
  266. box.box_model().padding.left,
  267. box.paintable_box() ? box.paintable_box()->content_width() : 0,
  268. box.box_model().padding.right,
  269. box.box_model().border.right,
  270. box.box_model().margin.right);
  271. // And the vertical box properties
  272. builder.appendff(" [{}+{}+{} {} {}+{}+{}]",
  273. box.box_model().margin.top,
  274. box.box_model().border.top,
  275. box.box_model().padding.top,
  276. box.paintable_box() ? box.paintable_box()->content_height() : 0,
  277. box.box_model().padding.bottom,
  278. box.box_model().border.bottom,
  279. box.box_model().margin.bottom);
  280. }
  281. if (auto formatting_context_type = Layout::FormattingContext::formatting_context_type_created_by_box(box); formatting_context_type.has_value()) {
  282. switch (formatting_context_type.value()) {
  283. case Layout::FormattingContext::Type::Block:
  284. builder.appendff(" [{}BFC{}]", formatting_context_color_on, color_off);
  285. break;
  286. case Layout::FormattingContext::Type::Flex:
  287. builder.appendff(" [{}FFC{}]", formatting_context_color_on, color_off);
  288. break;
  289. case Layout::FormattingContext::Type::Grid:
  290. builder.appendff(" [{}GFC{}]", formatting_context_color_on, color_off);
  291. break;
  292. case Layout::FormattingContext::Type::Table:
  293. builder.appendff(" [{}TFC{}]", formatting_context_color_on, color_off);
  294. break;
  295. case Layout::FormattingContext::Type::SVG:
  296. builder.appendff(" [{}SVG{}]", formatting_context_color_on, color_off);
  297. break;
  298. default:
  299. break;
  300. }
  301. }
  302. builder.appendff(" children: {}", box.children_are_inline() ? "inline" : "not-inline");
  303. if (is<Layout::FrameBox>(box)) {
  304. auto const& frame_box = static_cast<Layout::FrameBox const&>(box);
  305. if (auto* nested_browsing_context = frame_box.dom_node().nested_browsing_context()) {
  306. if (auto* document = nested_browsing_context->active_document()) {
  307. builder.appendff(" (url: {})", document->url());
  308. }
  309. }
  310. }
  311. builder.append("\n"sv);
  312. }
  313. if (layout_node.dom_node() && is<HTML::HTMLImageElement>(*layout_node.dom_node())) {
  314. if (auto image_data = static_cast<HTML::HTMLImageElement const&>(*layout_node.dom_node()).current_request().image_data()) {
  315. if (is<SVG::SVGDecodedImageData>(*image_data)) {
  316. auto& svg_data = verify_cast<SVG::SVGDecodedImageData>(*image_data);
  317. if (svg_data.svg_document().layout_node()) {
  318. ++indent;
  319. for (size_t i = 0; i < indent; ++i)
  320. builder.append(" "sv);
  321. builder.append("(SVG-as-image isolated context)\n"sv);
  322. dump_tree(builder, *svg_data.svg_document().layout_node(), show_box_model, show_specified_style, interactive);
  323. --indent;
  324. }
  325. }
  326. }
  327. }
  328. auto dump_fragment = [&](auto& fragment, size_t fragment_index) {
  329. for (size_t i = 0; i < indent; ++i)
  330. builder.append(" "sv);
  331. builder.appendff(" {}frag {}{} from {} ",
  332. fragment_color_on,
  333. fragment_index,
  334. color_off,
  335. fragment.layout_node().class_name());
  336. builder.appendff("start: {}, length: {}, rect: {} baseline: {}\n",
  337. fragment.start(),
  338. fragment.length(),
  339. fragment.absolute_rect(),
  340. fragment.baseline());
  341. if (is<Layout::TextNode>(fragment.layout_node())) {
  342. for (size_t i = 0; i < indent; ++i)
  343. builder.append(" "sv);
  344. auto const& layout_text = static_cast<Layout::TextNode const&>(fragment.layout_node());
  345. auto fragment_text = MUST(layout_text.text_for_rendering().substring_from_byte_offset(fragment.start(), fragment.length()));
  346. builder.appendff(" \"{}\"\n", fragment_text);
  347. }
  348. };
  349. if (is<Layout::BlockContainer>(layout_node) && static_cast<Layout::BlockContainer const&>(layout_node).children_are_inline()) {
  350. auto& block = static_cast<Layout::BlockContainer const&>(layout_node);
  351. for (size_t fragment_index = 0; block.paintable_with_lines() && fragment_index < block.paintable_with_lines()->fragments().size(); ++fragment_index) {
  352. auto const& fragment = block.paintable_with_lines()->fragments()[fragment_index];
  353. dump_fragment(fragment, fragment_index);
  354. }
  355. }
  356. if (is<Layout::InlineNode>(layout_node) && layout_node.paintable()) {
  357. auto const& inline_node = static_cast<Layout::InlineNode const&>(layout_node);
  358. auto const& inline_paintable = static_cast<Painting::InlinePaintable const&>(*inline_node.paintable());
  359. auto const& fragments = inline_paintable.fragments();
  360. for (size_t fragment_index = 0; fragment_index < fragments.size(); ++fragment_index) {
  361. auto const& fragment = fragments[fragment_index];
  362. dump_fragment(fragment, fragment_index);
  363. }
  364. }
  365. 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()) {
  366. struct NameAndValue {
  367. FlyString name;
  368. String value;
  369. };
  370. Vector<NameAndValue> properties;
  371. verify_cast<DOM::Element>(*layout_node.dom_node()).computed_css_values()->for_each_property([&](auto property_id, auto& value) {
  372. properties.append({ CSS::string_from_property_id(property_id), value.to_string() });
  373. });
  374. quick_sort(properties, [](auto& a, auto& b) { return a.name < b.name; });
  375. for (auto& property : properties) {
  376. for (size_t i = 0; i < indent; ++i)
  377. builder.append(" "sv);
  378. builder.appendff(" ({}: {})\n", property.name, property.value);
  379. }
  380. }
  381. ++indent;
  382. layout_node.for_each_child([&](auto& child) {
  383. dump_tree(builder, child, show_box_model, show_specified_style, interactive);
  384. return IterationDecision::Continue;
  385. });
  386. --indent;
  387. }
  388. void dump_selector(CSS::Selector const& selector)
  389. {
  390. StringBuilder builder;
  391. dump_selector(builder, selector);
  392. dbgln("{}", builder.string_view());
  393. }
  394. static void dump_qualified_name(StringBuilder& builder, CSS::Selector::SimpleSelector::QualifiedName const& qualified_name)
  395. {
  396. StringView namespace_type;
  397. switch (qualified_name.namespace_type) {
  398. case CSS::Selector::SimpleSelector::QualifiedName::NamespaceType::Default:
  399. namespace_type = "Default"sv;
  400. break;
  401. case CSS::Selector::SimpleSelector::QualifiedName::NamespaceType::None:
  402. namespace_type = "None"sv;
  403. break;
  404. case CSS::Selector::SimpleSelector::QualifiedName::NamespaceType::Any:
  405. namespace_type = "Any"sv;
  406. break;
  407. case CSS::Selector::SimpleSelector::QualifiedName::NamespaceType::Named:
  408. namespace_type = "Named"sv;
  409. break;
  410. }
  411. builder.appendff("NamespaceType={}, Namespace='{}', Name='{}'", namespace_type, qualified_name.namespace_, qualified_name.name.name);
  412. }
  413. void dump_selector(StringBuilder& builder, CSS::Selector const& selector)
  414. {
  415. builder.append(" CSS::Selector:\n"sv);
  416. for (auto& relative_selector : selector.compound_selectors()) {
  417. builder.append(" "sv);
  418. char const* relation_description = "";
  419. switch (relative_selector.combinator) {
  420. case CSS::Selector::Combinator::None:
  421. relation_description = "None";
  422. break;
  423. case CSS::Selector::Combinator::ImmediateChild:
  424. relation_description = "ImmediateChild";
  425. break;
  426. case CSS::Selector::Combinator::Descendant:
  427. relation_description = "Descendant";
  428. break;
  429. case CSS::Selector::Combinator::NextSibling:
  430. relation_description = "AdjacentSibling";
  431. break;
  432. case CSS::Selector::Combinator::SubsequentSibling:
  433. relation_description = "GeneralSibling";
  434. break;
  435. case CSS::Selector::Combinator::Column:
  436. relation_description = "Column";
  437. break;
  438. }
  439. if (*relation_description)
  440. builder.appendff("{{{}}} ", relation_description);
  441. for (size_t i = 0; i < relative_selector.simple_selectors.size(); ++i) {
  442. auto& simple_selector = relative_selector.simple_selectors[i];
  443. char const* type_description = "Unknown";
  444. switch (simple_selector.type) {
  445. case CSS::Selector::SimpleSelector::Type::Universal:
  446. type_description = "Universal";
  447. break;
  448. case CSS::Selector::SimpleSelector::Type::Id:
  449. type_description = "Id";
  450. break;
  451. case CSS::Selector::SimpleSelector::Type::Class:
  452. type_description = "Class";
  453. break;
  454. case CSS::Selector::SimpleSelector::Type::TagName:
  455. type_description = "TagName";
  456. break;
  457. case CSS::Selector::SimpleSelector::Type::Attribute:
  458. type_description = "Attribute";
  459. break;
  460. case CSS::Selector::SimpleSelector::Type::PseudoClass:
  461. type_description = "PseudoClassSelector";
  462. break;
  463. case CSS::Selector::SimpleSelector::Type::PseudoElement:
  464. type_description = "PseudoElement";
  465. break;
  466. }
  467. builder.appendff("{}:", type_description);
  468. // FIXME: This is goofy
  469. if (simple_selector.value.has<CSS::Selector::SimpleSelector::Name>()) {
  470. builder.append(simple_selector.name());
  471. } else if (simple_selector.value.has<CSS::Selector::SimpleSelector::QualifiedName>()) {
  472. dump_qualified_name(builder, simple_selector.qualified_name());
  473. }
  474. if (simple_selector.type == CSS::Selector::SimpleSelector::Type::PseudoClass) {
  475. auto const& pseudo_class = simple_selector.pseudo_class();
  476. builder.appendff(" pseudo_class={}", CSS::pseudo_class_name(pseudo_class.type));
  477. auto pseudo_class_metadata = CSS::pseudo_class_metadata(pseudo_class.type);
  478. switch (pseudo_class_metadata.parameter_type) {
  479. case CSS::PseudoClassMetadata::ParameterType::None:
  480. break;
  481. case CSS::PseudoClassMetadata::ParameterType::ANPlusB:
  482. case CSS::PseudoClassMetadata::ParameterType::ANPlusBOf: {
  483. builder.appendff("(step={}, offset={}", pseudo_class.nth_child_pattern.step_size, pseudo_class.nth_child_pattern.offset);
  484. if (!pseudo_class.argument_selector_list.is_empty()) {
  485. builder.append(", selectors=["sv);
  486. for (auto const& child_selector : pseudo_class.argument_selector_list)
  487. dump_selector(builder, child_selector);
  488. builder.append("]"sv);
  489. }
  490. builder.append(")"sv);
  491. break;
  492. }
  493. case CSS::PseudoClassMetadata::ParameterType::CompoundSelector:
  494. case CSS::PseudoClassMetadata::ParameterType::ForgivingSelectorList:
  495. case CSS::PseudoClassMetadata::ParameterType::ForgivingRelativeSelectorList:
  496. case CSS::PseudoClassMetadata::ParameterType::SelectorList: {
  497. builder.append("(["sv);
  498. for (auto& selector : pseudo_class.argument_selector_list)
  499. dump_selector(builder, selector);
  500. builder.append("])"sv);
  501. break;
  502. }
  503. case CSS::PseudoClassMetadata::ParameterType::Ident:
  504. builder.appendff("(keyword={})", string_from_keyword(pseudo_class.keyword.value()));
  505. break;
  506. case CSS::PseudoClassMetadata::ParameterType::LanguageRanges: {
  507. builder.append('(');
  508. builder.join(',', pseudo_class.languages);
  509. builder.append(')');
  510. break;
  511. }
  512. }
  513. }
  514. if (simple_selector.type == CSS::Selector::SimpleSelector::Type::PseudoElement) {
  515. builder.appendff(" pseudo_element={}", simple_selector.pseudo_element().name());
  516. }
  517. if (simple_selector.type == CSS::Selector::SimpleSelector::Type::Attribute) {
  518. auto const& attribute = simple_selector.attribute();
  519. char const* attribute_match_type_description = "";
  520. switch (attribute.match_type) {
  521. case CSS::Selector::SimpleSelector::Attribute::MatchType::HasAttribute:
  522. attribute_match_type_description = "HasAttribute";
  523. break;
  524. case CSS::Selector::SimpleSelector::Attribute::MatchType::ExactValueMatch:
  525. attribute_match_type_description = "ExactValueMatch";
  526. break;
  527. case CSS::Selector::SimpleSelector::Attribute::MatchType::ContainsWord:
  528. attribute_match_type_description = "ContainsWord";
  529. break;
  530. case CSS::Selector::SimpleSelector::Attribute::MatchType::ContainsString:
  531. attribute_match_type_description = "ContainsString";
  532. break;
  533. case CSS::Selector::SimpleSelector::Attribute::MatchType::StartsWithSegment:
  534. attribute_match_type_description = "StartsWithSegment";
  535. break;
  536. case CSS::Selector::SimpleSelector::Attribute::MatchType::StartsWithString:
  537. attribute_match_type_description = "StartsWithString";
  538. break;
  539. case CSS::Selector::SimpleSelector::Attribute::MatchType::EndsWithString:
  540. attribute_match_type_description = "EndsWithString";
  541. break;
  542. }
  543. builder.appendff(" [{}, ", attribute_match_type_description);
  544. dump_qualified_name(builder, attribute.qualified_name);
  545. builder.appendff(", value='{}']", attribute.value);
  546. }
  547. if (i != relative_selector.simple_selectors.size() - 1)
  548. builder.append(", "sv);
  549. }
  550. builder.append("\n"sv);
  551. }
  552. }
  553. void dump_rule(CSS::CSSRule const& rule)
  554. {
  555. StringBuilder builder;
  556. dump_rule(builder, rule);
  557. dbgln("{}", builder.string_view());
  558. }
  559. void dump_rule(StringBuilder& builder, CSS::CSSRule const& rule, int indent_levels)
  560. {
  561. indent(builder, indent_levels);
  562. builder.appendff("{}:\n", rule.class_name());
  563. switch (rule.type()) {
  564. case CSS::CSSRule::Type::FontFace:
  565. dump_font_face_rule(builder, verify_cast<CSS::CSSFontFaceRule const>(rule), indent_levels);
  566. break;
  567. case CSS::CSSRule::Type::Import:
  568. dump_import_rule(builder, verify_cast<CSS::CSSImportRule const>(rule), indent_levels);
  569. break;
  570. case CSS::CSSRule::Type::Keyframe:
  571. case CSS::CSSRule::Type::Keyframes:
  572. // TODO: Dump them!
  573. break;
  574. case CSS::CSSRule::Type::LayerBlock:
  575. dump_layer_block_rule(builder, verify_cast<CSS::CSSLayerBlockRule const>(rule), indent_levels);
  576. break;
  577. case CSS::CSSRule::Type::LayerStatement:
  578. dump_layer_statement_rule(builder, verify_cast<CSS::CSSLayerStatementRule const>(rule), indent_levels);
  579. break;
  580. case CSS::CSSRule::Type::Media:
  581. dump_media_rule(builder, verify_cast<CSS::CSSMediaRule const>(rule), indent_levels);
  582. break;
  583. case CSS::CSSRule::Type::Namespace:
  584. dump_namespace_rule(builder, verify_cast<CSS::CSSNamespaceRule const>(rule), indent_levels);
  585. break;
  586. case CSS::CSSRule::Type::Style:
  587. dump_style_rule(builder, verify_cast<CSS::CSSStyleRule const>(rule), indent_levels);
  588. break;
  589. case CSS::CSSRule::Type::Supports:
  590. dump_supports_rule(builder, verify_cast<CSS::CSSSupportsRule const>(rule), indent_levels);
  591. break;
  592. }
  593. }
  594. void dump_font_face_rule(StringBuilder& builder, CSS::CSSFontFaceRule const& rule, int indent_levels)
  595. {
  596. auto& font_face = rule.font_face();
  597. indent(builder, indent_levels + 1);
  598. builder.appendff("font-family: {}\n", font_face.font_family());
  599. if (font_face.weight().has_value()) {
  600. indent(builder, indent_levels + 1);
  601. builder.appendff("weight: {}\n", font_face.weight().value());
  602. }
  603. if (font_face.slope().has_value()) {
  604. indent(builder, indent_levels + 1);
  605. builder.appendff("slope: {}\n", font_face.slope().value());
  606. }
  607. if (font_face.width().has_value()) {
  608. indent(builder, indent_levels + 1);
  609. builder.appendff("width: {}\n", font_face.width().value());
  610. }
  611. indent(builder, indent_levels + 1);
  612. builder.append("sources:\n"sv);
  613. for (auto const& source : font_face.sources()) {
  614. indent(builder, indent_levels + 2);
  615. if (source.local_or_url.has<URL::URL>())
  616. builder.appendff("url={}, format={}\n", source.local_or_url.get<URL::URL>(), source.format.value_or("???"_string));
  617. else
  618. builder.appendff("local={}\n", source.local_or_url.get<AK::String>());
  619. }
  620. indent(builder, indent_levels + 1);
  621. builder.append("unicode-ranges:\n"sv);
  622. for (auto const& unicode_range : font_face.unicode_ranges()) {
  623. indent(builder, indent_levels + 2);
  624. builder.appendff("{}\n", unicode_range.to_string());
  625. }
  626. if (font_face.ascent_override().has_value()) {
  627. indent(builder, indent_levels + 1);
  628. builder.appendff("ascent-override: {}\n", font_face.ascent_override().value());
  629. }
  630. if (font_face.descent_override().has_value()) {
  631. indent(builder, indent_levels + 1);
  632. builder.appendff("descent-override: {}\n", font_face.descent_override().value());
  633. }
  634. if (font_face.line_gap_override().has_value()) {
  635. indent(builder, indent_levels + 1);
  636. builder.appendff("line-gap-override: {}\n", font_face.line_gap_override().value());
  637. }
  638. indent(builder, indent_levels + 1);
  639. builder.appendff("display: {}\n", CSS::to_string(font_face.font_display()));
  640. if (font_face.font_named_instance().has_value()) {
  641. indent(builder, indent_levels + 1);
  642. builder.appendff("named-instance: {}\n", font_face.font_named_instance().value());
  643. }
  644. if (font_face.font_language_override().has_value()) {
  645. indent(builder, indent_levels + 1);
  646. builder.appendff("language-override: {}\n", font_face.font_language_override().value());
  647. }
  648. if (font_face.font_feature_settings().has_value()) {
  649. indent(builder, indent_levels + 1);
  650. builder.append("feature-settings:"sv);
  651. auto const& entries = font_face.font_feature_settings().value();
  652. for (auto const& [name, value] : entries) {
  653. builder.appendff(" {}={}", name, value);
  654. }
  655. builder.append("\n"sv);
  656. }
  657. if (font_face.font_variation_settings().has_value()) {
  658. indent(builder, indent_levels + 1);
  659. builder.append("variation-settings:"sv);
  660. auto const& entries = font_face.font_variation_settings().value();
  661. for (auto const& [name, value] : entries) {
  662. builder.appendff(" {}={}", name, value);
  663. }
  664. builder.append("\n"sv);
  665. }
  666. }
  667. void dump_import_rule(StringBuilder& builder, CSS::CSSImportRule const& rule, int indent_levels)
  668. {
  669. indent(builder, indent_levels);
  670. builder.appendff(" Document URL: {}\n", rule.url());
  671. }
  672. void dump_layer_block_rule(StringBuilder& builder, CSS::CSSLayerBlockRule const& layer_block, int indent_levels)
  673. {
  674. indent(builder, indent_levels);
  675. builder.appendff(" Layer Block: `{}`\n Rules ({}):\n", layer_block.internal_name(), layer_block.css_rules().length());
  676. for (auto& rule : layer_block.css_rules())
  677. dump_rule(builder, rule, indent_levels + 1);
  678. }
  679. void dump_layer_statement_rule(StringBuilder& builder, CSS::CSSLayerStatementRule const& layer_statement, int indent_levels)
  680. {
  681. indent(builder, indent_levels);
  682. builder.append(" Layer Statement: "sv);
  683. builder.join(", "sv, layer_statement.name_list());
  684. }
  685. void dump_media_rule(StringBuilder& builder, CSS::CSSMediaRule const& media, int indent_levels)
  686. {
  687. indent(builder, indent_levels);
  688. builder.appendff(" Media: {}\n Rules ({}):\n", media.condition_text(), media.css_rules().length());
  689. for (auto& rule : media.css_rules())
  690. dump_rule(builder, rule, indent_levels + 1);
  691. }
  692. void dump_supports_rule(StringBuilder& builder, CSS::CSSSupportsRule const& supports, int indent_levels)
  693. {
  694. indent(builder, indent_levels);
  695. builder.appendff(" Supports: {}\n Rules ({}):\n", supports.condition_text(), supports.css_rules().length());
  696. for (auto& rule : supports.css_rules())
  697. dump_rule(builder, rule, indent_levels + 1);
  698. }
  699. void dump_style_rule(StringBuilder& builder, CSS::CSSStyleRule const& rule, int indent_levels)
  700. {
  701. for (auto& selector : rule.selectors()) {
  702. dump_selector(builder, selector);
  703. }
  704. indent(builder, indent_levels);
  705. builder.appendff(" Declarations ({}):\n", rule.declaration().length());
  706. for (auto& property : rule.declaration().properties()) {
  707. indent(builder, indent_levels);
  708. builder.appendff(" {}: '{}'", CSS::string_from_property_id(property.property_id), property.value->to_string());
  709. if (property.important == CSS::Important::Yes)
  710. builder.append(" \033[31;1m!important\033[0m"sv);
  711. builder.append('\n');
  712. }
  713. for (auto& property : rule.declaration().custom_properties()) {
  714. indent(builder, indent_levels);
  715. builder.appendff(" {}: '{}'", property.key, property.value.value->to_string());
  716. if (property.value.important == CSS::Important::Yes)
  717. builder.append(" \033[31;1m!important\033[0m"sv);
  718. builder.append('\n');
  719. }
  720. }
  721. void dump_sheet(CSS::StyleSheet const& sheet)
  722. {
  723. StringBuilder builder;
  724. dump_sheet(builder, sheet);
  725. dbgln("{}", builder.string_view());
  726. }
  727. void dump_sheet(StringBuilder& builder, CSS::StyleSheet const& sheet)
  728. {
  729. auto& css_stylesheet = verify_cast<CSS::CSSStyleSheet>(sheet);
  730. builder.appendff("CSSStyleSheet{{{}}}: {} rule(s)\n", &sheet, css_stylesheet.rules().length());
  731. for (auto& rule : css_stylesheet.rules())
  732. dump_rule(builder, rule);
  733. }
  734. void dump_tree(Painting::Paintable const& paintable)
  735. {
  736. StringBuilder builder;
  737. dump_tree(builder, paintable, true);
  738. dbgln("{}", builder.string_view());
  739. }
  740. void dump_tree(StringBuilder& builder, Painting::Paintable const& paintable, bool colorize, int indent)
  741. {
  742. for (int i = 0; i < indent; ++i)
  743. builder.append(" "sv);
  744. StringView paintable_with_lines_color_on = ""sv;
  745. StringView paintable_box_color_on = ""sv;
  746. StringView text_paintable_color_on = ""sv;
  747. StringView paintable_color_on = ""sv;
  748. StringView color_off = ""sv;
  749. if (colorize) {
  750. paintable_with_lines_color_on = "\033[34m"sv;
  751. paintable_box_color_on = "\033[33m"sv;
  752. text_paintable_color_on = "\033[35m"sv;
  753. paintable_color_on = "\033[32m"sv;
  754. color_off = "\033[0m"sv;
  755. }
  756. if (is<Painting::PaintableWithLines>(paintable))
  757. builder.append(paintable_with_lines_color_on);
  758. else if (is<Painting::PaintableBox>(paintable))
  759. builder.append(paintable_box_color_on);
  760. else if (is<Painting::TextPaintable>(paintable))
  761. builder.append(text_paintable_color_on);
  762. else
  763. builder.append(paintable_color_on);
  764. builder.appendff("{}{} ({})", paintable.class_name(), color_off, paintable.layout_node().debug_description());
  765. if (paintable.layout_node().is_box()) {
  766. auto const& paintable_box = static_cast<Painting::PaintableBox const&>(paintable);
  767. builder.appendff(" {}", paintable_box.absolute_border_box_rect());
  768. if (paintable_box.has_scrollable_overflow()) {
  769. builder.appendff(" overflow: {}", paintable_box.scrollable_overflow_rect());
  770. }
  771. if (!paintable_box.scroll_offset().is_zero()) {
  772. builder.appendff(" scroll-offset: {}", paintable_box.scroll_offset());
  773. }
  774. }
  775. builder.append("\n"sv);
  776. for (auto const* child = paintable.first_child(); child; child = child->next_sibling()) {
  777. dump_tree(builder, *child, colorize, indent + 1);
  778. }
  779. }
  780. void dump_namespace_rule(StringBuilder& builder, CSS::CSSNamespaceRule const& namespace_, int indent_levels)
  781. {
  782. indent(builder, indent_levels);
  783. builder.appendff(" Namespace: {}\n", namespace_.namespace_uri());
  784. if (!namespace_.prefix().is_empty())
  785. builder.appendff(" Prefix: {}\n", namespace_.prefix());
  786. }
  787. }