Dump.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. /*
  2. * Copyright (c) 2018-2021, 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/CSSImportRule.h>
  11. #include <LibWeb/CSS/CSSRule.h>
  12. #include <LibWeb/CSS/CSSStyleRule.h>
  13. #include <LibWeb/CSS/CSSStyleSheet.h>
  14. #include <LibWeb/CSS/PropertyID.h>
  15. #include <LibWeb/DOM/Comment.h>
  16. #include <LibWeb/DOM/Document.h>
  17. #include <LibWeb/DOM/Element.h>
  18. #include <LibWeb/DOM/ShadowRoot.h>
  19. #include <LibWeb/DOM/Text.h>
  20. #include <LibWeb/Dump.h>
  21. #include <LibWeb/HTML/HTMLTemplateElement.h>
  22. #include <LibWeb/Layout/BlockBox.h>
  23. #include <LibWeb/Layout/Node.h>
  24. #include <LibWeb/Layout/TextNode.h>
  25. #include <stdio.h>
  26. namespace Web {
  27. void dump_tree(const DOM::Node& node)
  28. {
  29. StringBuilder builder;
  30. dump_tree(builder, node);
  31. dbgln("{}", builder.string_view());
  32. }
  33. void dump_tree(StringBuilder& builder, const DOM::Node& node)
  34. {
  35. static int indent = 0;
  36. for (int i = 0; i < indent; ++i)
  37. builder.append(" ");
  38. if (is<DOM::Element>(node)) {
  39. builder.appendff("<{}", downcast<DOM::Element>(node).local_name());
  40. downcast<DOM::Element>(node).for_each_attribute([&](auto& name, auto& value) {
  41. builder.appendff(" {}={}", name, value);
  42. });
  43. builder.append(">\n");
  44. } else if (is<DOM::Text>(node)) {
  45. builder.appendff("\"{}\"\n", downcast<DOM::Text>(node).data());
  46. } else {
  47. builder.appendff("{}\n", node.node_name());
  48. }
  49. ++indent;
  50. if (is<DOM::Element>(node) && downcast<DOM::Element>(node).shadow_root()) {
  51. dump_tree(*downcast<DOM::Element>(node).shadow_root());
  52. }
  53. if (is<DOM::ParentNode>(node)) {
  54. if (!is<HTML::HTMLTemplateElement>(node)) {
  55. static_cast<const DOM::ParentNode&>(node).for_each_child([](auto& child) {
  56. dump_tree(child);
  57. });
  58. } else {
  59. auto& template_element = downcast<HTML::HTMLTemplateElement>(node);
  60. dump_tree(template_element.content());
  61. }
  62. }
  63. --indent;
  64. }
  65. void dump_tree(const Layout::Node& layout_node, bool show_box_model, bool show_specified_style)
  66. {
  67. StringBuilder builder;
  68. dump_tree(builder, layout_node, show_box_model, show_specified_style, true);
  69. dbgln("{}", builder.string_view());
  70. }
  71. void dump_tree(StringBuilder& builder, const Layout::Node& layout_node, bool show_box_model, bool show_specified_style, bool interactive)
  72. {
  73. static size_t indent = 0;
  74. for (size_t i = 0; i < indent; ++i)
  75. builder.append(" ");
  76. FlyString tag_name;
  77. if (layout_node.is_anonymous())
  78. tag_name = "(anonymous)";
  79. else if (is<DOM::Element>(layout_node.dom_node()))
  80. tag_name = downcast<DOM::Element>(*layout_node.dom_node()).local_name();
  81. else
  82. tag_name = layout_node.dom_node()->node_name();
  83. String identifier = "";
  84. if (layout_node.dom_node() && is<DOM::Element>(*layout_node.dom_node())) {
  85. auto& element = downcast<DOM::Element>(*layout_node.dom_node());
  86. StringBuilder builder;
  87. auto id = element.attribute(HTML::AttributeNames::id);
  88. if (!id.is_empty()) {
  89. builder.append('#');
  90. builder.append(id);
  91. }
  92. for (auto& class_name : element.class_names()) {
  93. builder.append('.');
  94. builder.append(class_name);
  95. }
  96. identifier = builder.to_string();
  97. }
  98. const char* nonbox_color_on = "";
  99. const char* box_color_on = "";
  100. const char* positioned_color_on = "";
  101. const char* floating_color_on = "";
  102. const char* inline_block_color_on = "";
  103. const char* line_box_color_on = "";
  104. const char* fragment_color_on = "";
  105. const char* flex_color_on = "";
  106. const char* color_off = "";
  107. if (interactive) {
  108. nonbox_color_on = "\033[33m";
  109. box_color_on = "\033[34m";
  110. positioned_color_on = "\033[31;1m";
  111. floating_color_on = "\033[32;1m";
  112. inline_block_color_on = "\033[36;1m";
  113. line_box_color_on = "\033[34;1m";
  114. fragment_color_on = "\033[35;1m";
  115. flex_color_on = "\033[34;1m";
  116. color_off = "\033[0m";
  117. }
  118. if (!is<Layout::Box>(layout_node)) {
  119. builder.appendff("{}{}{} <{}{}{}{}>",
  120. nonbox_color_on,
  121. layout_node.class_name().substring_view(13),
  122. color_off,
  123. tag_name,
  124. nonbox_color_on,
  125. identifier,
  126. color_off);
  127. if (interactive)
  128. builder.appendff(" @{:p}", &layout_node);
  129. builder.append("\n");
  130. } else {
  131. auto& box = downcast<Layout::Box>(layout_node);
  132. builder.appendff("{}{}{} <{}{}{}{}> ",
  133. box_color_on,
  134. box.class_name().substring_view(13),
  135. color_off,
  136. box_color_on,
  137. tag_name,
  138. color_off,
  139. identifier.characters());
  140. if (interactive)
  141. builder.appendff("@{:p} ", &layout_node);
  142. builder.appendff("at ({},{}) size {}x{}",
  143. (int)box.absolute_x(),
  144. (int)box.absolute_y(),
  145. (int)box.width(),
  146. (int)box.height());
  147. if (box.is_positioned())
  148. builder.appendff(" {}positioned{}", positioned_color_on, color_off);
  149. if (box.is_floating())
  150. builder.appendff(" {}floating{}", floating_color_on, color_off);
  151. if (box.is_inline_block())
  152. builder.appendff(" {}inline-block{}", inline_block_color_on, color_off);
  153. if (box.computed_values().display() == CSS::Display::Flex)
  154. builder.appendff(" {}flex{}", flex_color_on, color_off);
  155. if (show_box_model) {
  156. // Dump the horizontal box properties
  157. builder.appendff(" [{}+{}+{} {} {}+{}+{}]",
  158. box.box_model().margin.left,
  159. box.box_model().border.left,
  160. box.box_model().padding.left,
  161. box.width(),
  162. box.box_model().padding.right,
  163. box.box_model().border.right,
  164. box.box_model().margin.right);
  165. // And the vertical box properties
  166. builder.appendff(" [{}+{}+{} {} {}+{}+{}]",
  167. box.box_model().margin.top,
  168. box.box_model().border.top,
  169. box.box_model().padding.top,
  170. box.height(),
  171. box.box_model().padding.bottom,
  172. box.box_model().border.bottom,
  173. box.box_model().margin.bottom);
  174. }
  175. builder.append("\n");
  176. }
  177. if (is<Layout::BlockBox>(layout_node) && static_cast<const Layout::BlockBox&>(layout_node).children_are_inline()) {
  178. auto& block = static_cast<const Layout::BlockBox&>(layout_node);
  179. for (size_t line_box_index = 0; line_box_index < block.line_boxes().size(); ++line_box_index) {
  180. auto& line_box = block.line_boxes()[line_box_index];
  181. for (size_t i = 0; i < indent; ++i)
  182. builder.append(" ");
  183. builder.appendff(" {}line {}{} width: {}\n",
  184. line_box_color_on,
  185. line_box_index,
  186. color_off,
  187. (int)line_box.width());
  188. for (size_t fragment_index = 0; fragment_index < line_box.fragments().size(); ++fragment_index) {
  189. auto& fragment = line_box.fragments()[fragment_index];
  190. for (size_t i = 0; i < indent; ++i)
  191. builder.append(" ");
  192. builder.appendff(" {}frag {}{} from {} ",
  193. fragment_color_on,
  194. fragment_index,
  195. color_off,
  196. fragment.layout_node().class_name());
  197. if (interactive)
  198. builder.appendff("@{:p}, ", &fragment.layout_node());
  199. builder.appendff("start: {}, length: {}, rect: {}\n",
  200. fragment.start(),
  201. fragment.length(),
  202. enclosing_int_rect(fragment.absolute_rect()).to_string());
  203. if (is<Layout::TextNode>(fragment.layout_node())) {
  204. for (size_t i = 0; i < indent; ++i)
  205. builder.append(" ");
  206. auto& layout_text = static_cast<const Layout::TextNode&>(fragment.layout_node());
  207. auto fragment_text = layout_text.text_for_rendering().substring(fragment.start(), fragment.length());
  208. builder.appendff(" \"{}\"\n", fragment_text);
  209. }
  210. }
  211. }
  212. }
  213. if (show_specified_style && layout_node.dom_node() && layout_node.dom_node()->is_element() && downcast<DOM::Element>(layout_node.dom_node())->specified_css_values()) {
  214. struct NameAndValue {
  215. String name;
  216. String value;
  217. };
  218. Vector<NameAndValue> properties;
  219. downcast<DOM::Element>(*layout_node.dom_node()).specified_css_values()->for_each_property([&](auto property_id, auto& value) {
  220. properties.append({ CSS::string_from_property_id(property_id), value.to_string() });
  221. });
  222. quick_sort(properties, [](auto& a, auto& b) { return a.name < b.name; });
  223. for (auto& property : properties) {
  224. for (size_t i = 0; i < indent; ++i)
  225. builder.append(" ");
  226. builder.appendf(" (%s: %s)\n", property.name.characters(), property.value.characters());
  227. }
  228. }
  229. ++indent;
  230. layout_node.for_each_child([&](auto& child) {
  231. dump_tree(builder, child, show_box_model, show_specified_style, interactive);
  232. });
  233. --indent;
  234. }
  235. void dump_selector(const CSS::Selector& selector)
  236. {
  237. StringBuilder builder;
  238. dump_selector(builder, selector);
  239. dbgln("{}", builder.string_view());
  240. }
  241. void dump_selector(StringBuilder& builder, const CSS::Selector& selector)
  242. {
  243. builder.append(" CSS::Selector:\n");
  244. for (auto& complex_selector : selector.complex_selectors()) {
  245. builder.append(" ");
  246. const char* relation_description = "";
  247. switch (complex_selector.relation) {
  248. case CSS::Selector::ComplexSelector::Relation::None:
  249. relation_description = "None";
  250. break;
  251. case CSS::Selector::ComplexSelector::Relation::ImmediateChild:
  252. relation_description = "ImmediateChild";
  253. break;
  254. case CSS::Selector::ComplexSelector::Relation::Descendant:
  255. relation_description = "Descendant";
  256. break;
  257. case CSS::Selector::ComplexSelector::Relation::AdjacentSibling:
  258. relation_description = "AdjacentSibling";
  259. break;
  260. case CSS::Selector::ComplexSelector::Relation::GeneralSibling:
  261. relation_description = "GeneralSibling";
  262. break;
  263. case CSS::Selector::ComplexSelector::Relation::Column:
  264. relation_description = "Column";
  265. break;
  266. }
  267. if (*relation_description)
  268. builder.appendff("{{{}}} ", relation_description);
  269. for (size_t i = 0; i < complex_selector.compound_selector.size(); ++i) {
  270. auto& simple_selector = complex_selector.compound_selector[i];
  271. const char* type_description = "Unknown";
  272. switch (simple_selector.type) {
  273. case CSS::Selector::SimpleSelector::Type::Invalid:
  274. type_description = "Invalid";
  275. break;
  276. case CSS::Selector::SimpleSelector::Type::Universal:
  277. type_description = "Universal";
  278. break;
  279. case CSS::Selector::SimpleSelector::Type::Id:
  280. type_description = "Id";
  281. break;
  282. case CSS::Selector::SimpleSelector::Type::Class:
  283. type_description = "Class";
  284. break;
  285. case CSS::Selector::SimpleSelector::Type::TagName:
  286. type_description = "TagName";
  287. break;
  288. }
  289. const char* attribute_match_type_description = "";
  290. switch (simple_selector.attribute_match_type) {
  291. case CSS::Selector::SimpleSelector::AttributeMatchType::None:
  292. break;
  293. case CSS::Selector::SimpleSelector::AttributeMatchType::HasAttribute:
  294. attribute_match_type_description = "HasAttribute";
  295. break;
  296. case CSS::Selector::SimpleSelector::AttributeMatchType::ExactValueMatch:
  297. attribute_match_type_description = "ExactValueMatch";
  298. break;
  299. case CSS::Selector::SimpleSelector::AttributeMatchType::Contains:
  300. attribute_match_type_description = "Contains";
  301. break;
  302. case CSS::Selector::SimpleSelector::AttributeMatchType::StartsWith:
  303. attribute_match_type_description = "StartsWith";
  304. break;
  305. }
  306. const char* pseudo_class_description = "";
  307. switch (simple_selector.pseudo_class) {
  308. case CSS::Selector::SimpleSelector::PseudoClass::Link:
  309. pseudo_class_description = "Link";
  310. break;
  311. case CSS::Selector::SimpleSelector::PseudoClass::Visited:
  312. pseudo_class_description = "Visited";
  313. break;
  314. case CSS::Selector::SimpleSelector::PseudoClass::None:
  315. pseudo_class_description = "None";
  316. break;
  317. case CSS::Selector::SimpleSelector::PseudoClass::Root:
  318. pseudo_class_description = "Root";
  319. break;
  320. case CSS::Selector::SimpleSelector::PseudoClass::FirstOfType:
  321. pseudo_class_description = "FirstOfType";
  322. break;
  323. case CSS::Selector::SimpleSelector::PseudoClass::LastOfType:
  324. pseudo_class_description = "LastOfType";
  325. break;
  326. case CSS::Selector::SimpleSelector::PseudoClass::Focus:
  327. pseudo_class_description = "Focus";
  328. break;
  329. case CSS::Selector::SimpleSelector::PseudoClass::Empty:
  330. pseudo_class_description = "Empty";
  331. break;
  332. case CSS::Selector::SimpleSelector::PseudoClass::Hover:
  333. pseudo_class_description = "Hover";
  334. break;
  335. case CSS::Selector::SimpleSelector::PseudoClass::LastChild:
  336. pseudo_class_description = "LastChild";
  337. break;
  338. case CSS::Selector::SimpleSelector::PseudoClass::FirstChild:
  339. pseudo_class_description = "FirstChild";
  340. break;
  341. case CSS::Selector::SimpleSelector::PseudoClass::OnlyChild:
  342. pseudo_class_description = "OnlyChild";
  343. break;
  344. }
  345. builder.appendff("{}:{}", type_description, simple_selector.value);
  346. if (simple_selector.pseudo_class != CSS::Selector::SimpleSelector::PseudoClass::None)
  347. builder.appendff(" pseudo_class={}", pseudo_class_description);
  348. if (simple_selector.attribute_match_type != CSS::Selector::SimpleSelector::AttributeMatchType::None) {
  349. builder.appendff(" [{}, name='{}', value='{}']", attribute_match_type_description, simple_selector.attribute_name, simple_selector.attribute_value);
  350. }
  351. if (i != complex_selector.compound_selector.size() - 1)
  352. builder.append(", ");
  353. }
  354. builder.append("\n");
  355. }
  356. }
  357. void dump_rule(const CSS::CSSRule& rule)
  358. {
  359. StringBuilder builder;
  360. dump_rule(builder, rule);
  361. dbgln("{}", builder.string_view());
  362. }
  363. void dump_rule(StringBuilder& builder, const CSS::CSSRule& rule)
  364. {
  365. builder.appendff("{}:\n", rule.class_name());
  366. switch (rule.type()) {
  367. case CSS::CSSRule::Type::Style:
  368. dump_style_rule(builder, downcast<const CSS::CSSStyleRule>(rule));
  369. break;
  370. case CSS::CSSRule::Type::Import:
  371. dump_import_rule(builder, downcast<const CSS::CSSImportRule>(rule));
  372. break;
  373. default:
  374. VERIFY_NOT_REACHED();
  375. }
  376. }
  377. void dump_import_rule(StringBuilder& builder, const CSS::CSSImportRule& rule)
  378. {
  379. builder.appendff(" Document URL: {}\n", rule.url());
  380. }
  381. void dump_style_rule(StringBuilder& builder, const CSS::CSSStyleRule& rule)
  382. {
  383. for (auto& selector : rule.selectors()) {
  384. dump_selector(builder, selector);
  385. }
  386. builder.append(" Declarations:\n");
  387. for (auto& property : rule.declaration().properties()) {
  388. builder.appendff(" {}: '{}'\n", CSS::string_from_property_id(property.property_id), property.value->to_string());
  389. }
  390. }
  391. void dump_sheet(const CSS::StyleSheet& sheet)
  392. {
  393. StringBuilder builder;
  394. dump_sheet(builder, sheet);
  395. dbgln("{}", builder.string_view());
  396. }
  397. void dump_sheet(StringBuilder& builder, const CSS::StyleSheet& sheet)
  398. {
  399. VERIFY(is<CSS::CSSStyleSheet>(sheet));
  400. builder.appendff("CSSStyleSheet{{{}}}: {} rule(s)\n", &sheet, static_cast<const CSS::CSSStyleSheet&>(sheet).rules().size());
  401. for (auto& rule : static_cast<const CSS::CSSStyleSheet&>(sheet).rules()) {
  402. dump_rule(builder, rule);
  403. }
  404. }
  405. }