Dump.cpp 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866
  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. void dump_selector(StringBuilder& builder, CSS::Selector const& selector)
  361. {
  362. builder.append(" CSS::Selector:\n"sv);
  363. for (auto& relative_selector : selector.compound_selectors()) {
  364. builder.append(" "sv);
  365. char const* relation_description = "";
  366. switch (relative_selector.combinator) {
  367. case CSS::Selector::Combinator::None:
  368. relation_description = "None";
  369. break;
  370. case CSS::Selector::Combinator::ImmediateChild:
  371. relation_description = "ImmediateChild";
  372. break;
  373. case CSS::Selector::Combinator::Descendant:
  374. relation_description = "Descendant";
  375. break;
  376. case CSS::Selector::Combinator::NextSibling:
  377. relation_description = "AdjacentSibling";
  378. break;
  379. case CSS::Selector::Combinator::SubsequentSibling:
  380. relation_description = "GeneralSibling";
  381. break;
  382. case CSS::Selector::Combinator::Column:
  383. relation_description = "Column";
  384. break;
  385. }
  386. if (*relation_description)
  387. builder.appendff("{{{}}} ", relation_description);
  388. for (size_t i = 0; i < relative_selector.simple_selectors.size(); ++i) {
  389. auto& simple_selector = relative_selector.simple_selectors[i];
  390. char const* type_description = "Unknown";
  391. switch (simple_selector.type) {
  392. case CSS::Selector::SimpleSelector::Type::Universal:
  393. type_description = "Universal";
  394. break;
  395. case CSS::Selector::SimpleSelector::Type::Id:
  396. type_description = "Id";
  397. break;
  398. case CSS::Selector::SimpleSelector::Type::Class:
  399. type_description = "Class";
  400. break;
  401. case CSS::Selector::SimpleSelector::Type::TagName:
  402. type_description = "TagName";
  403. break;
  404. case CSS::Selector::SimpleSelector::Type::Attribute:
  405. type_description = "Attribute";
  406. break;
  407. case CSS::Selector::SimpleSelector::Type::PseudoClass:
  408. type_description = "PseudoClass";
  409. break;
  410. case CSS::Selector::SimpleSelector::Type::PseudoElement:
  411. type_description = "PseudoElement";
  412. break;
  413. }
  414. builder.appendff("{}:", type_description);
  415. // FIXME: This is goofy
  416. if (simple_selector.value.has<CSS::Selector::SimpleSelector::Name>())
  417. builder.append(simple_selector.name());
  418. if (simple_selector.type == CSS::Selector::SimpleSelector::Type::PseudoClass) {
  419. auto const& pseudo_class = simple_selector.pseudo_class();
  420. char const* pseudo_class_description = "";
  421. switch (pseudo_class.type) {
  422. case CSS::Selector::SimpleSelector::PseudoClass::Type::Link:
  423. pseudo_class_description = "Link";
  424. break;
  425. case CSS::Selector::SimpleSelector::PseudoClass::Type::Visited:
  426. pseudo_class_description = "Visited";
  427. break;
  428. case CSS::Selector::SimpleSelector::PseudoClass::Type::Active:
  429. pseudo_class_description = "Active";
  430. break;
  431. case CSS::Selector::SimpleSelector::PseudoClass::Type::Root:
  432. pseudo_class_description = "Root";
  433. break;
  434. case CSS::Selector::SimpleSelector::PseudoClass::Type::Host:
  435. pseudo_class_description = "Host";
  436. break;
  437. case CSS::Selector::SimpleSelector::PseudoClass::Type::FirstOfType:
  438. pseudo_class_description = "FirstOfType";
  439. break;
  440. case CSS::Selector::SimpleSelector::PseudoClass::Type::LastOfType:
  441. pseudo_class_description = "LastOfType";
  442. break;
  443. case CSS::Selector::SimpleSelector::PseudoClass::Type::OnlyOfType:
  444. pseudo_class_description = "OnlyOfType";
  445. break;
  446. case CSS::Selector::SimpleSelector::PseudoClass::Type::NthOfType:
  447. pseudo_class_description = "NthOfType";
  448. break;
  449. case CSS::Selector::SimpleSelector::PseudoClass::Type::NthLastOfType:
  450. pseudo_class_description = "NthLastOfType";
  451. break;
  452. case CSS::Selector::SimpleSelector::PseudoClass::Type::NthChild:
  453. pseudo_class_description = "NthChild";
  454. break;
  455. case CSS::Selector::SimpleSelector::PseudoClass::Type::NthLastChild:
  456. pseudo_class_description = "NthLastChild";
  457. break;
  458. case CSS::Selector::SimpleSelector::PseudoClass::Type::Focus:
  459. pseudo_class_description = "Focus";
  460. break;
  461. case CSS::Selector::SimpleSelector::PseudoClass::Type::FocusWithin:
  462. pseudo_class_description = "FocusWithin";
  463. break;
  464. case CSS::Selector::SimpleSelector::PseudoClass::Type::Empty:
  465. pseudo_class_description = "Empty";
  466. break;
  467. case CSS::Selector::SimpleSelector::PseudoClass::Type::Hover:
  468. pseudo_class_description = "Hover";
  469. break;
  470. case CSS::Selector::SimpleSelector::PseudoClass::Type::LastChild:
  471. pseudo_class_description = "LastChild";
  472. break;
  473. case CSS::Selector::SimpleSelector::PseudoClass::Type::FirstChild:
  474. pseudo_class_description = "FirstChild";
  475. break;
  476. case CSS::Selector::SimpleSelector::PseudoClass::Type::OnlyChild:
  477. pseudo_class_description = "OnlyChild";
  478. break;
  479. case CSS::Selector::SimpleSelector::PseudoClass::Type::Disabled:
  480. pseudo_class_description = "Disabled";
  481. break;
  482. case CSS::Selector::SimpleSelector::PseudoClass::Type::Enabled:
  483. pseudo_class_description = "Enabled";
  484. break;
  485. case CSS::Selector::SimpleSelector::PseudoClass::Type::Checked:
  486. pseudo_class_description = "Checked";
  487. break;
  488. case CSS::Selector::SimpleSelector::PseudoClass::Type::Indeterminate:
  489. pseudo_class_description = "Indeterminate";
  490. break;
  491. case CSS::Selector::SimpleSelector::PseudoClass::Type::Not:
  492. pseudo_class_description = "Not";
  493. break;
  494. case CSS::Selector::SimpleSelector::PseudoClass::Type::Is:
  495. pseudo_class_description = "Is";
  496. break;
  497. case CSS::Selector::SimpleSelector::PseudoClass::Type::Where:
  498. pseudo_class_description = "Where";
  499. break;
  500. case CSS::Selector::SimpleSelector::PseudoClass::Type::Lang:
  501. pseudo_class_description = "Lang";
  502. break;
  503. case CSS::Selector::SimpleSelector::PseudoClass::Type::Scope:
  504. pseudo_class_description = "Scope";
  505. break;
  506. case CSS::Selector::SimpleSelector::PseudoClass::Type::Defined:
  507. pseudo_class_description = "Defined";
  508. break;
  509. case CSS::Selector::SimpleSelector::PseudoClass::Type::Playing:
  510. pseudo_class_description = "Playing";
  511. break;
  512. case CSS::Selector::SimpleSelector::PseudoClass::Type::Paused:
  513. pseudo_class_description = "Paused";
  514. break;
  515. case CSS::Selector::SimpleSelector::PseudoClass::Type::Seeking:
  516. pseudo_class_description = "Seeking";
  517. break;
  518. case CSS::Selector::SimpleSelector::PseudoClass::Type::Muted:
  519. pseudo_class_description = "Muted";
  520. break;
  521. case CSS::Selector::SimpleSelector::PseudoClass::Type::VolumeLocked:
  522. pseudo_class_description = "VolumeLocked";
  523. break;
  524. }
  525. builder.appendff(" pseudo_class={}", pseudo_class_description);
  526. if (pseudo_class.type == CSS::Selector::SimpleSelector::PseudoClass::Type::Lang) {
  527. builder.append('(');
  528. builder.join(',', pseudo_class.languages);
  529. builder.append(')');
  530. } else if (pseudo_class.type == CSS::Selector::SimpleSelector::PseudoClass::Type::Not
  531. || pseudo_class.type == CSS::Selector::SimpleSelector::PseudoClass::Type::Is
  532. || pseudo_class.type == CSS::Selector::SimpleSelector::PseudoClass::Type::Where) {
  533. builder.append("(["sv);
  534. for (auto& selector : pseudo_class.argument_selector_list)
  535. dump_selector(builder, selector);
  536. builder.append("])"sv);
  537. } else if ((pseudo_class.type == CSS::Selector::SimpleSelector::PseudoClass::Type::NthChild)
  538. || (pseudo_class.type == CSS::Selector::SimpleSelector::PseudoClass::Type::NthLastChild)
  539. || (pseudo_class.type == CSS::Selector::SimpleSelector::PseudoClass::Type::NthOfType)
  540. || (pseudo_class.type == CSS::Selector::SimpleSelector::PseudoClass::Type::NthLastOfType)) {
  541. builder.appendff("(step={}, offset={}", pseudo_class.nth_child_pattern.step_size, pseudo_class.nth_child_pattern.offset);
  542. if (!pseudo_class.argument_selector_list.is_empty()) {
  543. builder.append(", selectors=["sv);
  544. for (auto const& child_selector : pseudo_class.argument_selector_list)
  545. dump_selector(builder, child_selector);
  546. builder.append("]"sv);
  547. }
  548. builder.append(")"sv);
  549. }
  550. }
  551. if (simple_selector.type == CSS::Selector::SimpleSelector::Type::PseudoElement) {
  552. char const* pseudo_element_description = "";
  553. switch (simple_selector.pseudo_element()) {
  554. case CSS::Selector::PseudoElement::Before:
  555. pseudo_element_description = "before";
  556. break;
  557. case CSS::Selector::PseudoElement::After:
  558. pseudo_element_description = "after";
  559. break;
  560. case CSS::Selector::PseudoElement::FirstLine:
  561. pseudo_element_description = "first-line";
  562. break;
  563. case CSS::Selector::PseudoElement::FirstLetter:
  564. pseudo_element_description = "first-letter";
  565. break;
  566. case CSS::Selector::PseudoElement::Marker:
  567. pseudo_element_description = "marker";
  568. break;
  569. case CSS::Selector::PseudoElement::ProgressBar:
  570. pseudo_element_description = "-webkit-progress-bar";
  571. break;
  572. case CSS::Selector::PseudoElement::ProgressValue:
  573. pseudo_element_description = "-webkit-progress-value";
  574. break;
  575. case CSS::Selector::PseudoElement::Placeholder:
  576. pseudo_element_description = "placeholder";
  577. break;
  578. case CSS::Selector::PseudoElement::Selection:
  579. pseudo_element_description = "selection";
  580. break;
  581. case CSS::Selector::PseudoElement::PseudoElementCount:
  582. VERIFY_NOT_REACHED();
  583. break;
  584. }
  585. builder.appendff(" pseudo_element={}", pseudo_element_description);
  586. }
  587. if (simple_selector.type == CSS::Selector::SimpleSelector::Type::Attribute) {
  588. auto const& attribute = simple_selector.attribute();
  589. char const* attribute_match_type_description = "";
  590. switch (attribute.match_type) {
  591. case CSS::Selector::SimpleSelector::Attribute::MatchType::HasAttribute:
  592. attribute_match_type_description = "HasAttribute";
  593. break;
  594. case CSS::Selector::SimpleSelector::Attribute::MatchType::ExactValueMatch:
  595. attribute_match_type_description = "ExactValueMatch";
  596. break;
  597. case CSS::Selector::SimpleSelector::Attribute::MatchType::ContainsWord:
  598. attribute_match_type_description = "ContainsWord";
  599. break;
  600. case CSS::Selector::SimpleSelector::Attribute::MatchType::ContainsString:
  601. attribute_match_type_description = "ContainsString";
  602. break;
  603. case CSS::Selector::SimpleSelector::Attribute::MatchType::StartsWithSegment:
  604. attribute_match_type_description = "StartsWithSegment";
  605. break;
  606. case CSS::Selector::SimpleSelector::Attribute::MatchType::StartsWithString:
  607. attribute_match_type_description = "StartsWithString";
  608. break;
  609. case CSS::Selector::SimpleSelector::Attribute::MatchType::EndsWithString:
  610. attribute_match_type_description = "EndsWithString";
  611. break;
  612. }
  613. builder.appendff(" [{}, name='{}', value='{}']", attribute_match_type_description, attribute.name, attribute.value);
  614. }
  615. if (i != relative_selector.simple_selectors.size() - 1)
  616. builder.append(", "sv);
  617. }
  618. builder.append("\n"sv);
  619. }
  620. }
  621. ErrorOr<void> dump_rule(CSS::CSSRule const& rule)
  622. {
  623. StringBuilder builder;
  624. TRY(dump_rule(builder, rule));
  625. dbgln("{}", builder.string_view());
  626. return {};
  627. }
  628. ErrorOr<void> dump_rule(StringBuilder& builder, CSS::CSSRule const& rule, int indent_levels)
  629. {
  630. indent(builder, indent_levels);
  631. builder.appendff("{}:\n", rule.class_name());
  632. switch (rule.type()) {
  633. case CSS::CSSRule::Type::FontFace:
  634. dump_font_face_rule(builder, verify_cast<CSS::CSSFontFaceRule const>(rule), indent_levels);
  635. break;
  636. case CSS::CSSRule::Type::Import:
  637. dump_import_rule(builder, verify_cast<CSS::CSSImportRule const>(rule), indent_levels);
  638. break;
  639. case CSS::CSSRule::Type::Media:
  640. TRY(dump_media_rule(builder, verify_cast<CSS::CSSMediaRule const>(rule), indent_levels));
  641. break;
  642. case CSS::CSSRule::Type::Style:
  643. TRY(dump_style_rule(builder, verify_cast<CSS::CSSStyleRule const>(rule), indent_levels));
  644. break;
  645. case CSS::CSSRule::Type::Supports:
  646. TRY(dump_supports_rule(builder, verify_cast<CSS::CSSSupportsRule const>(rule), indent_levels));
  647. break;
  648. case CSS::CSSRule::Type::Keyframe:
  649. case CSS::CSSRule::Type::Keyframes:
  650. break;
  651. case CSS::CSSRule::Type::Namespace:
  652. TRY(dump_namespace_rule(builder, verify_cast<CSS::CSSNamespaceRule const>(rule), indent_levels));
  653. break;
  654. }
  655. return {};
  656. }
  657. void dump_font_face_rule(StringBuilder& builder, CSS::CSSFontFaceRule const& rule, int indent_levels)
  658. {
  659. auto& font_face = rule.font_face();
  660. indent(builder, indent_levels + 1);
  661. builder.appendff("font-family: {}\n", font_face.font_family());
  662. if (font_face.weight().has_value()) {
  663. indent(builder, indent_levels + 1);
  664. builder.appendff("weight: {}\n", font_face.weight().value());
  665. }
  666. if (font_face.slope().has_value()) {
  667. indent(builder, indent_levels + 1);
  668. builder.appendff("slope: {}\n", font_face.slope().value());
  669. }
  670. indent(builder, indent_levels + 1);
  671. builder.append("sources:\n"sv);
  672. for (auto const& source : font_face.sources()) {
  673. indent(builder, indent_levels + 2);
  674. builder.appendff("url={}, format={}\n", source.url, source.format.value_or("???"_short_string));
  675. }
  676. indent(builder, indent_levels + 1);
  677. builder.append("unicode-ranges:\n"sv);
  678. for (auto const& unicode_range : font_face.unicode_ranges()) {
  679. indent(builder, indent_levels + 2);
  680. builder.appendff("{}\n", unicode_range.to_string().release_value_but_fixme_should_propagate_errors());
  681. }
  682. }
  683. void dump_import_rule(StringBuilder& builder, CSS::CSSImportRule const& rule, int indent_levels)
  684. {
  685. indent(builder, indent_levels);
  686. builder.appendff(" Document URL: {}\n", rule.url());
  687. }
  688. ErrorOr<void> dump_media_rule(StringBuilder& builder, CSS::CSSMediaRule const& media, int indent_levels)
  689. {
  690. indent(builder, indent_levels);
  691. builder.appendff(" Media: {}\n Rules ({}):\n", media.condition_text(), media.css_rules().length());
  692. for (auto& rule : media.css_rules())
  693. TRY(dump_rule(builder, rule, indent_levels + 1));
  694. return {};
  695. }
  696. ErrorOr<void> dump_supports_rule(StringBuilder& builder, CSS::CSSSupportsRule const& supports, int indent_levels)
  697. {
  698. indent(builder, indent_levels);
  699. builder.appendff(" Supports: {}\n Rules ({}):\n", supports.condition_text(), supports.css_rules().length());
  700. for (auto& rule : supports.css_rules())
  701. TRY(dump_rule(builder, rule, indent_levels + 1));
  702. return {};
  703. }
  704. ErrorOr<void> dump_style_rule(StringBuilder& builder, CSS::CSSStyleRule const& rule, int indent_levels)
  705. {
  706. for (auto& selector : rule.selectors()) {
  707. dump_selector(builder, selector);
  708. }
  709. indent(builder, indent_levels);
  710. builder.append(" Declarations:\n"sv);
  711. auto& style_declaration = verify_cast<CSS::PropertyOwningCSSStyleDeclaration>(rule.declaration());
  712. for (auto& property : style_declaration.properties()) {
  713. indent(builder, indent_levels);
  714. builder.appendff(" {}: '{}'", CSS::string_from_property_id(property.property_id), TRY(property.value->to_string()));
  715. if (property.important == CSS::Important::Yes)
  716. builder.append(" \033[31;1m!important\033[0m"sv);
  717. builder.append('\n');
  718. }
  719. for (auto& property : style_declaration.custom_properties()) {
  720. indent(builder, indent_levels);
  721. builder.appendff(" {}: '{}'", property.key, TRY(property.value.value->to_string()));
  722. if (property.value.important == CSS::Important::Yes)
  723. builder.append(" \033[31;1m!important\033[0m"sv);
  724. builder.append('\n');
  725. }
  726. return {};
  727. }
  728. ErrorOr<void> dump_sheet(CSS::StyleSheet const& sheet)
  729. {
  730. StringBuilder builder;
  731. TRY(dump_sheet(builder, sheet));
  732. dbgln("{}", builder.string_view());
  733. return {};
  734. }
  735. ErrorOr<void> dump_sheet(StringBuilder& builder, CSS::StyleSheet const& sheet)
  736. {
  737. auto& css_stylesheet = verify_cast<CSS::CSSStyleSheet>(sheet);
  738. builder.appendff("CSSStyleSheet{{{}}}: {} rule(s)\n", &sheet, css_stylesheet.rules().length());
  739. for (auto& rule : css_stylesheet.rules())
  740. TRY(dump_rule(builder, rule));
  741. return {};
  742. }
  743. void dump_tree(Painting::Paintable const& paintable)
  744. {
  745. StringBuilder builder;
  746. dump_tree(builder, paintable, true);
  747. dbgln("{}", builder.string_view());
  748. }
  749. void dump_tree(StringBuilder& builder, Painting::Paintable const& paintable, bool colorize, int indent)
  750. {
  751. for (int i = 0; i < indent; ++i)
  752. builder.append(" "sv);
  753. StringView paintable_with_lines_color_on = ""sv;
  754. StringView paintable_box_color_on = ""sv;
  755. StringView text_paintable_color_on = ""sv;
  756. StringView paintable_color_on = ""sv;
  757. StringView color_off = ""sv;
  758. if (colorize) {
  759. paintable_with_lines_color_on = "\033[34m"sv;
  760. paintable_box_color_on = "\033[33m"sv;
  761. text_paintable_color_on = "\033[35m"sv;
  762. paintable_color_on = "\033[32m"sv;
  763. color_off = "\033[0m"sv;
  764. }
  765. if (is<Painting::PaintableWithLines>(paintable))
  766. builder.append(paintable_with_lines_color_on);
  767. else if (is<Painting::PaintableBox>(paintable))
  768. builder.append(paintable_box_color_on);
  769. else if (is<Painting::TextPaintable>(paintable))
  770. builder.append(text_paintable_color_on);
  771. else
  772. builder.append(paintable_color_on);
  773. builder.appendff("{}{} ({})", paintable.class_name(), color_off, paintable.layout_node().debug_description());
  774. if (paintable.layout_node().is_box()) {
  775. auto const& paintable_box = static_cast<Painting::PaintableBox const&>(paintable);
  776. builder.appendff(" {}", paintable_box.absolute_border_box_rect());
  777. if (paintable_box.has_scrollable_overflow()) {
  778. builder.appendff(" overflow: {}", paintable_box.scrollable_overflow_rect());
  779. }
  780. }
  781. builder.append("\n"sv);
  782. for (auto const* child = paintable.first_child(); child; child = child->next_sibling()) {
  783. dump_tree(builder, *child, colorize, indent + 1);
  784. }
  785. }
  786. ErrorOr<void> dump_namespace_rule(StringBuilder& builder, CSS::CSSNamespaceRule const& namespace_, int indent_levels)
  787. {
  788. indent(builder, indent_levels);
  789. TRY(builder.try_appendff(" Namespace: {}\n", namespace_.namespace_uri()));
  790. if (!namespace_.prefix().is_null() && !namespace_.prefix().is_empty())
  791. TRY(builder.try_appendff(" Prefix: {}\n", namespace_.prefix()));
  792. return {};
  793. }
  794. }