Dump.cpp 34 KB

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