Dump.cpp 13 KB

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