Dump.cpp 39 KB

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