Dump.cpp 39 KB

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