Dump.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/QuickSort.h>
  27. #include <AK/StringBuilder.h>
  28. #include <AK/Utf8View.h>
  29. #include <LibWeb/CSS/PropertyID.h>
  30. #include <LibWeb/CSS/StyleSheet.h>
  31. #include <LibWeb/DOM/Comment.h>
  32. #include <LibWeb/DOM/Document.h>
  33. #include <LibWeb/DOM/Element.h>
  34. #include <LibWeb/DOM/ShadowRoot.h>
  35. #include <LibWeb/DOM/Text.h>
  36. #include <LibWeb/Dump.h>
  37. #include <LibWeb/HTML/HTMLTemplateElement.h>
  38. #include <LibWeb/Layout/BlockBox.h>
  39. #include <LibWeb/Layout/Node.h>
  40. #include <LibWeb/Layout/TextNode.h>
  41. #include <stdio.h>
  42. namespace Web {
  43. void dump_tree(const DOM::Node& node)
  44. {
  45. StringBuilder builder;
  46. dump_tree(builder, node);
  47. dbgln("{}", builder.string_view());
  48. }
  49. void dump_tree(StringBuilder& builder, const DOM::Node& node)
  50. {
  51. static int indent = 0;
  52. for (int i = 0; i < indent; ++i)
  53. builder.append(" ");
  54. if (is<DOM::Element>(node)) {
  55. builder.appendff("<{}", downcast<DOM::Element>(node).local_name());
  56. downcast<DOM::Element>(node).for_each_attribute([&](auto& name, auto& value) {
  57. builder.appendff(" {}={}", name, value);
  58. });
  59. builder.append(">\n");
  60. } else if (is<DOM::Text>(node)) {
  61. builder.appendff("\"{}\"\n", downcast<DOM::Text>(node).data());
  62. } else {
  63. builder.appendff("{}\n", node.node_name());
  64. }
  65. ++indent;
  66. if (is<DOM::Element>(node) && downcast<DOM::Element>(node).shadow_root()) {
  67. dump_tree(*downcast<DOM::Element>(node).shadow_root());
  68. }
  69. if (is<DOM::ParentNode>(node)) {
  70. if (!is<HTML::HTMLTemplateElement>(node)) {
  71. static_cast<const DOM::ParentNode&>(node).for_each_child([](auto& child) {
  72. dump_tree(child);
  73. });
  74. } else {
  75. auto& template_element = downcast<HTML::HTMLTemplateElement>(node);
  76. dump_tree(template_element.content());
  77. }
  78. }
  79. --indent;
  80. }
  81. void dump_tree(const Layout::Node& layout_node, bool show_box_model, bool show_specified_style)
  82. {
  83. StringBuilder builder;
  84. dump_tree(builder, layout_node, show_box_model, show_specified_style, true);
  85. dbgln("{}", builder.string_view());
  86. }
  87. void dump_tree(StringBuilder& builder, const Layout::Node& layout_node, bool show_box_model, bool show_specified_style, bool interactive)
  88. {
  89. static size_t indent = 0;
  90. for (size_t i = 0; i < indent; ++i)
  91. builder.append(" ");
  92. FlyString tag_name;
  93. if (layout_node.is_anonymous())
  94. tag_name = "(anonymous)";
  95. else if (is<DOM::Element>(layout_node.dom_node()))
  96. tag_name = downcast<DOM::Element>(*layout_node.dom_node()).local_name();
  97. else
  98. tag_name = layout_node.dom_node()->node_name();
  99. String identifier = "";
  100. if (layout_node.dom_node() && is<DOM::Element>(*layout_node.dom_node())) {
  101. auto& element = downcast<DOM::Element>(*layout_node.dom_node());
  102. StringBuilder builder;
  103. auto id = element.attribute(HTML::AttributeNames::id);
  104. if (!id.is_empty()) {
  105. builder.append('#');
  106. builder.append(id);
  107. }
  108. for (auto& class_name : element.class_names()) {
  109. builder.append('.');
  110. builder.append(class_name);
  111. }
  112. identifier = builder.to_string();
  113. }
  114. const char* nonbox_color_on = "";
  115. const char* box_color_on = "";
  116. const char* positioned_color_on = "";
  117. const char* floating_color_on = "";
  118. const char* inline_block_color_on = "";
  119. const char* line_box_color_on = "";
  120. const char* fragment_color_on = "";
  121. const char* flex_color_on = "";
  122. const char* color_off = "";
  123. if (interactive) {
  124. nonbox_color_on = "\033[33m";
  125. box_color_on = "\033[34m";
  126. positioned_color_on = "\033[31;1m";
  127. floating_color_on = "\033[32;1m";
  128. inline_block_color_on = "\033[36;1m";
  129. line_box_color_on = "\033[34;1m";
  130. fragment_color_on = "\033[35;1m";
  131. flex_color_on = "\033[34;1m";
  132. color_off = "\033[0m";
  133. }
  134. if (!is<Layout::Box>(layout_node)) {
  135. builder.appendff("{}{}{} <{}{}{}{}>",
  136. nonbox_color_on,
  137. layout_node.class_name().substring_view(13),
  138. color_off,
  139. tag_name,
  140. nonbox_color_on,
  141. identifier,
  142. color_off);
  143. if (interactive)
  144. builder.appendff(" @{:p}", &layout_node);
  145. builder.append("\n");
  146. } else {
  147. auto& box = downcast<Layout::Box>(layout_node);
  148. builder.appendff("{}{}{} <{}{}{}{}> ",
  149. box_color_on,
  150. box.class_name().substring_view(13),
  151. color_off,
  152. box_color_on,
  153. tag_name,
  154. color_off,
  155. identifier.characters());
  156. if (interactive)
  157. builder.appendff("@{:p} ", &layout_node);
  158. builder.appendff("at ({},{}) size {}x{}",
  159. (int)box.absolute_x(),
  160. (int)box.absolute_y(),
  161. (int)box.width(),
  162. (int)box.height());
  163. if (box.is_positioned())
  164. builder.appendff(" {}positioned{}", positioned_color_on, color_off);
  165. if (box.is_floating())
  166. builder.appendff(" {}floating{}", floating_color_on, color_off);
  167. if (box.is_inline_block())
  168. builder.appendff(" {}inline-block{}", inline_block_color_on, color_off);
  169. if (box.computed_values().display() == CSS::Display::Flex)
  170. builder.appendff(" {}flex{}", flex_color_on, color_off);
  171. if (show_box_model) {
  172. // Dump the horizontal box properties
  173. builder.appendf(" [%g+%g+%g %g %g+%g+%g]",
  174. box.box_model().margin.left,
  175. box.box_model().border.left,
  176. box.box_model().padding.left,
  177. box.width(),
  178. box.box_model().padding.right,
  179. box.box_model().border.right,
  180. box.box_model().margin.right);
  181. // And the vertical box properties
  182. builder.appendf(" [%g+%g+%g %g %g+%g+%g]",
  183. box.box_model().margin.top,
  184. box.box_model().border.top,
  185. box.box_model().padding.top,
  186. box.height(),
  187. box.box_model().padding.bottom,
  188. box.box_model().border.bottom,
  189. box.box_model().margin.bottom);
  190. }
  191. builder.append("\n");
  192. }
  193. if (is<Layout::BlockBox>(layout_node) && static_cast<const Layout::BlockBox&>(layout_node).children_are_inline()) {
  194. auto& block = static_cast<const Layout::BlockBox&>(layout_node);
  195. for (size_t line_box_index = 0; line_box_index < block.line_boxes().size(); ++line_box_index) {
  196. auto& line_box = block.line_boxes()[line_box_index];
  197. for (size_t i = 0; i < indent; ++i)
  198. builder.append(" ");
  199. builder.appendff(" {}line {}{} width: {}\n",
  200. line_box_color_on,
  201. line_box_index,
  202. color_off,
  203. (int)line_box.width());
  204. for (size_t fragment_index = 0; fragment_index < line_box.fragments().size(); ++fragment_index) {
  205. auto& fragment = line_box.fragments()[fragment_index];
  206. for (size_t i = 0; i < indent; ++i)
  207. builder.append(" ");
  208. builder.appendff(" {}frag {}{} from {} ",
  209. fragment_color_on,
  210. fragment_index,
  211. color_off,
  212. fragment.layout_node().class_name());
  213. if (interactive)
  214. builder.appendff("@{:p}, ", &fragment.layout_node());
  215. builder.appendff("start: {}, length: {}, rect: {}\n",
  216. fragment.start(),
  217. fragment.length(),
  218. enclosing_int_rect(fragment.absolute_rect()).to_string());
  219. if (is<Layout::TextNode>(fragment.layout_node())) {
  220. for (size_t i = 0; i < indent; ++i)
  221. builder.append(" ");
  222. auto& layout_text = static_cast<const Layout::TextNode&>(fragment.layout_node());
  223. auto fragment_text = layout_text.text_for_rendering().substring(fragment.start(), fragment.length());
  224. builder.appendff(" \"{}\"\n", fragment_text);
  225. }
  226. }
  227. }
  228. }
  229. if (show_specified_style && layout_node.dom_node() && layout_node.dom_node()->is_element() && downcast<DOM::Element>(layout_node.dom_node())->specified_css_values()) {
  230. struct NameAndValue {
  231. String name;
  232. String value;
  233. };
  234. Vector<NameAndValue> properties;
  235. downcast<DOM::Element>(*layout_node.dom_node()).specified_css_values()->for_each_property([&](auto property_id, auto& value) {
  236. properties.append({ CSS::string_from_property_id(property_id), value.to_string() });
  237. });
  238. quick_sort(properties, [](auto& a, auto& b) { return a.name < b.name; });
  239. for (auto& property : properties) {
  240. for (size_t i = 0; i < indent; ++i)
  241. builder.append(" ");
  242. builder.appendf(" (%s: %s)\n", property.name.characters(), property.value.characters());
  243. }
  244. }
  245. ++indent;
  246. layout_node.for_each_child([&](auto& child) {
  247. dump_tree(builder, child, show_box_model, show_specified_style, interactive);
  248. });
  249. --indent;
  250. }
  251. void dump_selector(const CSS::Selector& selector)
  252. {
  253. StringBuilder builder;
  254. dump_selector(builder, selector);
  255. dbgln("{}", builder.string_view());
  256. }
  257. void dump_selector(StringBuilder& builder, const CSS::Selector& selector)
  258. {
  259. builder.append(" CSS::Selector:\n");
  260. for (auto& complex_selector : selector.complex_selectors()) {
  261. builder.append(" ");
  262. const char* relation_description = "";
  263. switch (complex_selector.relation) {
  264. case CSS::Selector::ComplexSelector::Relation::None:
  265. relation_description = "None";
  266. break;
  267. case CSS::Selector::ComplexSelector::Relation::ImmediateChild:
  268. relation_description = "ImmediateChild";
  269. break;
  270. case CSS::Selector::ComplexSelector::Relation::Descendant:
  271. relation_description = "Descendant";
  272. break;
  273. case CSS::Selector::ComplexSelector::Relation::AdjacentSibling:
  274. relation_description = "AdjacentSibling";
  275. break;
  276. case CSS::Selector::ComplexSelector::Relation::GeneralSibling:
  277. relation_description = "GeneralSibling";
  278. break;
  279. }
  280. if (*relation_description)
  281. builder.appendff("{{{}}} ", relation_description);
  282. for (size_t i = 0; i < complex_selector.compound_selector.size(); ++i) {
  283. auto& simple_selector = complex_selector.compound_selector[i];
  284. const char* type_description = "Unknown";
  285. switch (simple_selector.type) {
  286. case CSS::Selector::SimpleSelector::Type::Invalid:
  287. type_description = "Invalid";
  288. break;
  289. case CSS::Selector::SimpleSelector::Type::Universal:
  290. type_description = "Universal";
  291. break;
  292. case CSS::Selector::SimpleSelector::Type::Id:
  293. type_description = "Id";
  294. break;
  295. case CSS::Selector::SimpleSelector::Type::Class:
  296. type_description = "Class";
  297. break;
  298. case CSS::Selector::SimpleSelector::Type::TagName:
  299. type_description = "TagName";
  300. break;
  301. }
  302. const char* attribute_match_type_description = "";
  303. switch (simple_selector.attribute_match_type) {
  304. case CSS::Selector::SimpleSelector::AttributeMatchType::None:
  305. break;
  306. case CSS::Selector::SimpleSelector::AttributeMatchType::HasAttribute:
  307. attribute_match_type_description = "HasAttribute";
  308. break;
  309. case CSS::Selector::SimpleSelector::AttributeMatchType::ExactValueMatch:
  310. attribute_match_type_description = "ExactValueMatch";
  311. break;
  312. case CSS::Selector::SimpleSelector::AttributeMatchType::Contains:
  313. attribute_match_type_description = "Contains";
  314. break;
  315. }
  316. const char* pseudo_class_description = "";
  317. switch (simple_selector.pseudo_class) {
  318. case CSS::Selector::SimpleSelector::PseudoClass::Link:
  319. pseudo_class_description = "Link";
  320. break;
  321. case CSS::Selector::SimpleSelector::PseudoClass::Visited:
  322. pseudo_class_description = "Visited";
  323. break;
  324. case CSS::Selector::SimpleSelector::PseudoClass::None:
  325. pseudo_class_description = "None";
  326. break;
  327. case CSS::Selector::SimpleSelector::PseudoClass::Root:
  328. pseudo_class_description = "Root";
  329. break;
  330. case CSS::Selector::SimpleSelector::PseudoClass::Focus:
  331. pseudo_class_description = "Focus";
  332. break;
  333. case CSS::Selector::SimpleSelector::PseudoClass::Empty:
  334. pseudo_class_description = "Empty";
  335. break;
  336. case CSS::Selector::SimpleSelector::PseudoClass::Hover:
  337. pseudo_class_description = "Hover";
  338. break;
  339. case CSS::Selector::SimpleSelector::PseudoClass::LastChild:
  340. pseudo_class_description = "LastChild";
  341. break;
  342. case CSS::Selector::SimpleSelector::PseudoClass::FirstChild:
  343. pseudo_class_description = "FirstChild";
  344. break;
  345. case CSS::Selector::SimpleSelector::PseudoClass::OnlyChild:
  346. pseudo_class_description = "OnlyChild";
  347. break;
  348. }
  349. builder.appendff("{}:{}", type_description, simple_selector.value);
  350. if (simple_selector.pseudo_class != CSS::Selector::SimpleSelector::PseudoClass::None)
  351. builder.appendff(" pseudo_class={}", pseudo_class_description);
  352. if (simple_selector.attribute_match_type != CSS::Selector::SimpleSelector::AttributeMatchType::None) {
  353. builder.appendff(" [{}, name='{}', value='{}']", attribute_match_type_description, simple_selector.attribute_name, simple_selector.attribute_value);
  354. }
  355. if (i != complex_selector.compound_selector.size() - 1)
  356. builder.append(", ");
  357. }
  358. builder.append("\n");
  359. }
  360. }
  361. void dump_rule(const CSS::StyleRule& rule)
  362. {
  363. StringBuilder builder;
  364. dump_rule(builder, rule);
  365. dbgln("{}", builder.string_view());
  366. }
  367. void dump_rule(StringBuilder& builder, const CSS::StyleRule& rule)
  368. {
  369. builder.append("Rule:\n");
  370. for (auto& selector : rule.selectors()) {
  371. dump_selector(builder, selector);
  372. }
  373. builder.append(" Declarations:\n");
  374. for (auto& property : rule.declaration().properties()) {
  375. builder.appendff(" {}: '{}'\n", CSS::string_from_property_id(property.property_id), property.value->to_string());
  376. }
  377. }
  378. void dump_sheet(const CSS::StyleSheet& sheet)
  379. {
  380. StringBuilder builder;
  381. dump_sheet(builder, sheet);
  382. dbgln("{}", builder.string_view());
  383. }
  384. void dump_sheet(StringBuilder& builder, const CSS::StyleSheet& sheet)
  385. {
  386. builder.appendff("StyleSheet{{{}}}: {} rule(s)", &sheet, sheet.rules().size());
  387. for (auto& rule : sheet.rules()) {
  388. dump_rule(builder, rule);
  389. }
  390. }
  391. }