Dump.cpp 33 KB

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