Dump.cpp 32 KB

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