Dump.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /*
  2. * Copyright (c) 2018-2020, 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/DocumentFragment.h>
  34. #include <LibWeb/DOM/DocumentType.h>
  35. #include <LibWeb/DOM/Element.h>
  36. #include <LibWeb/DOM/Text.h>
  37. #include <LibWeb/Dump.h>
  38. #include <LibWeb/HTML/HTMLTemplateElement.h>
  39. #include <LibWeb/Layout/BlockBox.h>
  40. #include <LibWeb/Layout/Node.h>
  41. #include <LibWeb/Layout/TextNode.h>
  42. #include <stdio.h>
  43. namespace Web {
  44. void dump_tree(const DOM::Node& node)
  45. {
  46. static int indent = 0;
  47. for (int i = 0; i < indent; ++i)
  48. dbgprintf(" ");
  49. if (is<DOM::Document>(node)) {
  50. dbgprintf("*Document*\n");
  51. } else if (is<DOM::Element>(node)) {
  52. dbgprintf("<%s", downcast<DOM::Element>(node).local_name().characters());
  53. downcast<DOM::Element>(node).for_each_attribute([](auto& name, auto& value) {
  54. dbgprintf(" %s=%s", name.characters(), value.characters());
  55. });
  56. dbgprintf(">\n");
  57. } else if (is<DOM::Text>(node)) {
  58. dbgprintf("\"%s\"\n", downcast<DOM::Text>(node).data().characters());
  59. } else if (is<DOM::DocumentType>(node)) {
  60. dbgprintf("<!DOCTYPE html>\n");
  61. } else if (is<DOM::Comment>(node)) {
  62. dbgprintf("<!--%s-->\n", downcast<DOM::Comment>(node).data().characters());
  63. } else if (is<DOM::DocumentFragment>(node)) {
  64. dbgprintf("#document-fragment\n");
  65. }
  66. ++indent;
  67. if (is<DOM::ParentNode>(node)) {
  68. if (!is<HTML::HTMLTemplateElement>(node)) {
  69. static_cast<const DOM::ParentNode&>(node).for_each_child([](auto& child) {
  70. dump_tree(child);
  71. });
  72. } else {
  73. auto& template_element = downcast<HTML::HTMLTemplateElement>(node);
  74. dump_tree(template_element.content());
  75. }
  76. }
  77. --indent;
  78. }
  79. void dump_tree(const Layout::Node& layout_node, bool show_box_model, bool show_specified_style)
  80. {
  81. static size_t indent = 0;
  82. for (size_t i = 0; i < indent; ++i)
  83. dbgprintf(" ");
  84. FlyString tag_name;
  85. if (layout_node.is_anonymous())
  86. tag_name = "(anonymous)";
  87. else if (is<DOM::Text>(layout_node.dom_node()))
  88. tag_name = "#text";
  89. else if (is<DOM::Document>(layout_node.dom_node()))
  90. tag_name = "#document";
  91. else if (is<DOM::Element>(layout_node.dom_node()))
  92. tag_name = downcast<DOM::Element>(*layout_node.dom_node()).local_name();
  93. else
  94. tag_name = "???";
  95. String identifier = "";
  96. if (layout_node.dom_node() && is<DOM::Element>(*layout_node.dom_node())) {
  97. auto& element = downcast<DOM::Element>(*layout_node.dom_node());
  98. StringBuilder builder;
  99. auto id = element.attribute(HTML::AttributeNames::id);
  100. if (!id.is_empty()) {
  101. builder.append('#');
  102. builder.append(id);
  103. }
  104. for (auto& class_name : element.class_names()) {
  105. builder.append('.');
  106. builder.append(class_name);
  107. }
  108. identifier = builder.to_string();
  109. }
  110. if (!layout_node.is_box()) {
  111. dbgprintf("\033[33m%s\033[0m {\033[33m%s\033[0m%s} @%p\n", layout_node.class_name(), tag_name.characters(), identifier.characters(), &layout_node);
  112. } else {
  113. auto& box = downcast<Layout::Box>(layout_node);
  114. dbgprintf("\033[34m%s\033[0m {\033[34m%s\033[0m%s} @%p at (%d,%d) size %dx%d",
  115. box.class_name(),
  116. tag_name.characters(),
  117. identifier.characters(),
  118. &layout_node,
  119. (int)box.absolute_x(),
  120. (int)box.absolute_y(),
  121. (int)box.width(),
  122. (int)box.height());
  123. if (box.is_positioned())
  124. dbgprintf(" \033[31;1mpositioned\033[0m");
  125. if (box.is_floating())
  126. dbgprintf(" \033[32;1mfloating\033[0m");
  127. if (show_box_model) {
  128. // Dump the horizontal box properties
  129. dbgprintf(" [%g+%g+%g %g %g+%g+%g]",
  130. box.box_model().margin.left.to_px(box),
  131. box.box_model().border.left.to_px(box),
  132. box.box_model().padding.left.to_px(box),
  133. box.width(),
  134. box.box_model().padding.right.to_px(box),
  135. box.box_model().border.right.to_px(box),
  136. box.box_model().margin.right.to_px(box));
  137. // And the vertical box properties
  138. dbgprintf(" [%g+%g+%g %g %g+%g+%g]",
  139. box.box_model().margin.top.to_px(box),
  140. box.box_model().border.top.to_px(box),
  141. box.box_model().padding.top.to_px(box),
  142. box.height(),
  143. box.box_model().padding.bottom.to_px(box),
  144. box.box_model().border.bottom.to_px(box),
  145. box.box_model().margin.bottom.to_px(box));
  146. }
  147. dbgprintf("\n");
  148. }
  149. if (layout_node.is_block() && static_cast<const Layout::BlockBox&>(layout_node).children_are_inline()) {
  150. auto& block = static_cast<const Layout::BlockBox&>(layout_node);
  151. for (size_t line_box_index = 0; line_box_index < block.line_boxes().size(); ++line_box_index) {
  152. auto& line_box = block.line_boxes()[line_box_index];
  153. for (size_t i = 0; i < indent; ++i)
  154. dbgprintf(" ");
  155. dbgprintf(" \033[34;1mline %d\033[0m width: %d\n", line_box_index, (int)line_box.width());
  156. for (size_t fragment_index = 0; fragment_index < line_box.fragments().size(); ++fragment_index) {
  157. auto& fragment = line_box.fragments()[fragment_index];
  158. for (size_t i = 0; i < indent; ++i)
  159. dbgprintf(" ");
  160. dbgprintf(" \033[35;1mfrag %d\033[0m from %s @%p, start: %d, length: %d, rect: %s\n",
  161. fragment_index,
  162. fragment.layout_node().class_name(),
  163. &fragment.layout_node(),
  164. fragment.start(),
  165. fragment.length(),
  166. enclosing_int_rect(fragment.absolute_rect()).to_string().characters());
  167. if (fragment.layout_node().is_text()) {
  168. for (size_t i = 0; i < indent; ++i)
  169. dbgprintf(" ");
  170. auto& layout_text = static_cast<const Layout::TextNode&>(fragment.layout_node());
  171. auto fragment_text = layout_text.text_for_rendering().substring(fragment.start(), fragment.length());
  172. dbgprintf(" \"%s\"\n", fragment_text.characters());
  173. }
  174. }
  175. }
  176. }
  177. if (show_specified_style) {
  178. struct NameAndValue {
  179. String name;
  180. String value;
  181. };
  182. Vector<NameAndValue> properties;
  183. layout_node.specified_style().for_each_property([&](auto property_id, auto& value) {
  184. properties.append({ CSS::string_from_property_id(property_id), value.to_string() });
  185. });
  186. quick_sort(properties, [](auto& a, auto& b) { return a.name < b.name; });
  187. for (auto& property : properties) {
  188. for (size_t i = 0; i < indent; ++i)
  189. dbgprintf(" ");
  190. dbgprintf(" (%s: %s)\n", property.name.characters(), property.value.characters());
  191. }
  192. }
  193. ++indent;
  194. layout_node.for_each_child([](auto& child) {
  195. dump_tree(child);
  196. });
  197. --indent;
  198. }
  199. void dump_selector(const CSS::Selector& selector)
  200. {
  201. dbgprintf(" CSS::Selector:\n");
  202. for (auto& complex_selector : selector.complex_selectors()) {
  203. dbgprintf(" ");
  204. const char* relation_description = "";
  205. switch (complex_selector.relation) {
  206. case CSS::Selector::ComplexSelector::Relation::None:
  207. relation_description = "None";
  208. break;
  209. case CSS::Selector::ComplexSelector::Relation::ImmediateChild:
  210. relation_description = "ImmediateChild";
  211. break;
  212. case CSS::Selector::ComplexSelector::Relation::Descendant:
  213. relation_description = "Descendant";
  214. break;
  215. case CSS::Selector::ComplexSelector::Relation::AdjacentSibling:
  216. relation_description = "AdjacentSibling";
  217. break;
  218. case CSS::Selector::ComplexSelector::Relation::GeneralSibling:
  219. relation_description = "GeneralSibling";
  220. break;
  221. }
  222. if (*relation_description)
  223. dbgprintf("{%s} ", relation_description);
  224. for (size_t i = 0; i < complex_selector.compound_selector.size(); ++i) {
  225. auto& simple_selector = complex_selector.compound_selector[i];
  226. const char* type_description = "Unknown";
  227. switch (simple_selector.type) {
  228. case CSS::Selector::SimpleSelector::Type::Invalid:
  229. type_description = "Invalid";
  230. break;
  231. case CSS::Selector::SimpleSelector::Type::Universal:
  232. type_description = "Universal";
  233. break;
  234. case CSS::Selector::SimpleSelector::Type::Id:
  235. type_description = "Id";
  236. break;
  237. case CSS::Selector::SimpleSelector::Type::Class:
  238. type_description = "Class";
  239. break;
  240. case CSS::Selector::SimpleSelector::Type::TagName:
  241. type_description = "TagName";
  242. break;
  243. }
  244. const char* attribute_match_type_description = "";
  245. switch (simple_selector.attribute_match_type) {
  246. case CSS::Selector::SimpleSelector::AttributeMatchType::None:
  247. break;
  248. case CSS::Selector::SimpleSelector::AttributeMatchType::HasAttribute:
  249. attribute_match_type_description = "HasAttribute";
  250. break;
  251. case CSS::Selector::SimpleSelector::AttributeMatchType::ExactValueMatch:
  252. attribute_match_type_description = "ExactValueMatch";
  253. break;
  254. case CSS::Selector::SimpleSelector::AttributeMatchType::Contains:
  255. attribute_match_type_description = "Contains";
  256. break;
  257. }
  258. const char* pseudo_class_description = "";
  259. switch (simple_selector.pseudo_class) {
  260. case CSS::Selector::SimpleSelector::PseudoClass::Link:
  261. pseudo_class_description = "Link";
  262. break;
  263. case CSS::Selector::SimpleSelector::PseudoClass::Visited:
  264. pseudo_class_description = "Visited";
  265. break;
  266. case CSS::Selector::SimpleSelector::PseudoClass::None:
  267. pseudo_class_description = "None";
  268. break;
  269. case CSS::Selector::SimpleSelector::PseudoClass::Root:
  270. pseudo_class_description = "Root";
  271. break;
  272. case CSS::Selector::SimpleSelector::PseudoClass::Focus:
  273. pseudo_class_description = "Focus";
  274. break;
  275. case CSS::Selector::SimpleSelector::PseudoClass::Empty:
  276. pseudo_class_description = "Empty";
  277. break;
  278. case CSS::Selector::SimpleSelector::PseudoClass::Hover:
  279. pseudo_class_description = "Hover";
  280. break;
  281. case CSS::Selector::SimpleSelector::PseudoClass::LastChild:
  282. pseudo_class_description = "LastChild";
  283. break;
  284. case CSS::Selector::SimpleSelector::PseudoClass::FirstChild:
  285. pseudo_class_description = "FirstChild";
  286. break;
  287. case CSS::Selector::SimpleSelector::PseudoClass::OnlyChild:
  288. pseudo_class_description = "OnlyChild";
  289. break;
  290. }
  291. dbgprintf("%s:%s", type_description, simple_selector.value.characters());
  292. if (simple_selector.pseudo_class != CSS::Selector::SimpleSelector::PseudoClass::None)
  293. dbgprintf(" pseudo_class=%s", pseudo_class_description);
  294. if (simple_selector.attribute_match_type != CSS::Selector::SimpleSelector::AttributeMatchType::None) {
  295. dbgprintf(" [%s, name='%s', value='%s']", attribute_match_type_description, simple_selector.attribute_name.characters(), simple_selector.attribute_value.characters());
  296. }
  297. if (i != complex_selector.compound_selector.size() - 1)
  298. dbgprintf(", ");
  299. }
  300. dbgprintf("\n");
  301. }
  302. }
  303. void dump_rule(const CSS::StyleRule& rule)
  304. {
  305. dbgprintf("Rule:\n");
  306. for (auto& selector : rule.selectors()) {
  307. dump_selector(selector);
  308. }
  309. dbgprintf(" Declarations:\n");
  310. for (auto& property : rule.declaration().properties()) {
  311. dbgprintf(" %s: '%s'\n", CSS::string_from_property_id(property.property_id), property.value->to_string().characters());
  312. }
  313. }
  314. void dump_sheet(const CSS::StyleSheet& sheet)
  315. {
  316. dbgprintf("StyleSheet{%p}: %d rule(s)\n", &sheet, sheet.rules().size());
  317. for (auto& rule : sheet.rules()) {
  318. dump_rule(rule);
  319. }
  320. }
  321. }