Dump.cpp 14 KB

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