Dump.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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 DOM::Node& node)
  44. {
  45. static int indent = 0;
  46. for (int i = 0; i < indent; ++i)
  47. dbgprintf(" ");
  48. if (is<DOM::Document>(node)) {
  49. dbgprintf("*Document*\n");
  50. } else if (is<DOM::Element>(node)) {
  51. dbgprintf("<%s", downcast<DOM::Element>(node).local_name().characters());
  52. downcast<DOM::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<DOM::Text>(node)) {
  57. dbgprintf("\"%s\"\n", downcast<DOM::Text>(node).data().characters());
  58. } else if (is<DOM::DocumentType>(node)) {
  59. dbgprintf("<!DOCTYPE html>\n");
  60. } else if (is<DOM::Comment>(node)) {
  61. dbgprintf("<!--%s-->\n", downcast<DOM::Comment>(node).data().characters());
  62. } else if (is<DOM::DocumentFragment>(node)) {
  63. dbgprintf("#document-fragment\n");
  64. }
  65. ++indent;
  66. if (is<DOM::ParentNode>(node)) {
  67. static_cast<const DOM::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<DOM::Text>(layout_node.node()))
  82. tag_name = "#text";
  83. else if (is<DOM::Document>(layout_node.node()))
  84. tag_name = "#document";
  85. else if (is<DOM::Element>(layout_node.node()))
  86. tag_name = downcast<DOM::Element>(*layout_node.node()).local_name();
  87. else
  88. tag_name = "???";
  89. String identifier = "";
  90. if (layout_node.node() && is<DOM::Element>(*layout_node.node())) {
  91. auto& element = downcast<DOM::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 = downcast<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. for (size_t i = 0; i < indent; ++i)
  139. dbgprintf(" ");
  140. dbgprintf(" Line boxes (%d):\n", block.line_boxes().size());
  141. for (size_t line_box_index = 0; line_box_index < block.line_boxes().size(); ++line_box_index) {
  142. auto& line_box = block.line_boxes()[line_box_index];
  143. for (size_t i = 0; i < indent; ++i)
  144. dbgprintf(" ");
  145. dbgprintf(" [%d] width: %g\n", line_box_index, line_box.width());
  146. for (size_t fragment_index = 0; fragment_index < line_box.fragments().size(); ++fragment_index) {
  147. auto& fragment = line_box.fragments()[fragment_index];
  148. for (size_t i = 0; i < indent; ++i)
  149. dbgprintf(" ");
  150. dbgprintf(" [%d] layout_node: %s{%p}, start: %d, length: %d, rect: %s\n",
  151. fragment_index,
  152. fragment.layout_node().class_name(),
  153. &fragment.layout_node(),
  154. fragment.start(),
  155. fragment.length(),
  156. fragment.absolute_rect().to_string().characters());
  157. if (fragment.layout_node().is_text()) {
  158. for (size_t i = 0; i < indent; ++i)
  159. dbgprintf(" ");
  160. auto& layout_text = static_cast<const LayoutText&>(fragment.layout_node());
  161. auto fragment_text = layout_text.text_for_rendering().substring(fragment.start(), fragment.length());
  162. dbgprintf(" text: \"%s\"\n", fragment_text.characters());
  163. }
  164. }
  165. }
  166. }
  167. struct NameAndValue {
  168. String name;
  169. String value;
  170. };
  171. Vector<NameAndValue> properties;
  172. layout_node.specified_style().for_each_property([&](auto property_id, auto& value) {
  173. properties.append({ CSS::string_from_property_id(property_id), value.to_string() });
  174. });
  175. quick_sort(properties, [](auto& a, auto& b) { return a.name < b.name; });
  176. for (auto& property : properties) {
  177. for (size_t i = 0; i < indent; ++i)
  178. dbgprintf(" ");
  179. dbgprintf(" (%s: %s)\n", property.name.characters(), property.value.characters());
  180. }
  181. ++indent;
  182. layout_node.for_each_child([](auto& child) {
  183. dump_tree(child);
  184. });
  185. --indent;
  186. }
  187. void dump_selector(const CSS::Selector& selector)
  188. {
  189. dbgprintf(" CSS::Selector:\n");
  190. for (auto& complex_selector : selector.complex_selectors()) {
  191. dbgprintf(" ");
  192. const char* relation_description = "";
  193. switch (complex_selector.relation) {
  194. case CSS::Selector::ComplexSelector::Relation::None:
  195. relation_description = "None";
  196. break;
  197. case CSS::Selector::ComplexSelector::Relation::ImmediateChild:
  198. relation_description = "ImmediateChild";
  199. break;
  200. case CSS::Selector::ComplexSelector::Relation::Descendant:
  201. relation_description = "Descendant";
  202. break;
  203. case CSS::Selector::ComplexSelector::Relation::AdjacentSibling:
  204. relation_description = "AdjacentSibling";
  205. break;
  206. case CSS::Selector::ComplexSelector::Relation::GeneralSibling:
  207. relation_description = "GeneralSibling";
  208. break;
  209. }
  210. if (*relation_description)
  211. dbgprintf("{%s} ", relation_description);
  212. for (size_t i = 0; i < complex_selector.compound_selector.size(); ++i) {
  213. auto& simple_selector = complex_selector.compound_selector[i];
  214. const char* type_description = "Unknown";
  215. switch (simple_selector.type) {
  216. case CSS::Selector::SimpleSelector::Type::Invalid:
  217. type_description = "Invalid";
  218. break;
  219. case CSS::Selector::SimpleSelector::Type::Universal:
  220. type_description = "Universal";
  221. break;
  222. case CSS::Selector::SimpleSelector::Type::Id:
  223. type_description = "Id";
  224. break;
  225. case CSS::Selector::SimpleSelector::Type::Class:
  226. type_description = "Class";
  227. break;
  228. case CSS::Selector::SimpleSelector::Type::TagName:
  229. type_description = "TagName";
  230. break;
  231. }
  232. const char* attribute_match_type_description = "";
  233. switch (simple_selector.attribute_match_type) {
  234. case CSS::Selector::SimpleSelector::AttributeMatchType::None:
  235. break;
  236. case CSS::Selector::SimpleSelector::AttributeMatchType::HasAttribute:
  237. attribute_match_type_description = "HasAttribute";
  238. break;
  239. case CSS::Selector::SimpleSelector::AttributeMatchType::ExactValueMatch:
  240. attribute_match_type_description = "ExactValueMatch";
  241. break;
  242. case CSS::Selector::SimpleSelector::AttributeMatchType::Contains:
  243. attribute_match_type_description = "Contains";
  244. break;
  245. }
  246. const char* pseudo_class_description = "";
  247. switch (simple_selector.pseudo_class) {
  248. case CSS::Selector::SimpleSelector::PseudoClass::Link:
  249. pseudo_class_description = "Link";
  250. break;
  251. case CSS::Selector::SimpleSelector::PseudoClass::Visited:
  252. pseudo_class_description = "Visited";
  253. break;
  254. case CSS::Selector::SimpleSelector::PseudoClass::None:
  255. pseudo_class_description = "None";
  256. break;
  257. case CSS::Selector::SimpleSelector::PseudoClass::Root:
  258. pseudo_class_description = "Root";
  259. break;
  260. case CSS::Selector::SimpleSelector::PseudoClass::Focus:
  261. pseudo_class_description = "Focus";
  262. break;
  263. case CSS::Selector::SimpleSelector::PseudoClass::Empty:
  264. pseudo_class_description = "Empty";
  265. break;
  266. case CSS::Selector::SimpleSelector::PseudoClass::Hover:
  267. pseudo_class_description = "Hover";
  268. break;
  269. case CSS::Selector::SimpleSelector::PseudoClass::LastChild:
  270. pseudo_class_description = "LastChild";
  271. break;
  272. case CSS::Selector::SimpleSelector::PseudoClass::FirstChild:
  273. pseudo_class_description = "FirstChild";
  274. break;
  275. case CSS::Selector::SimpleSelector::PseudoClass::OnlyChild:
  276. pseudo_class_description = "OnlyChild";
  277. break;
  278. }
  279. dbgprintf("%s:%s", type_description, simple_selector.value.characters());
  280. if (simple_selector.pseudo_class != CSS::Selector::SimpleSelector::PseudoClass::None)
  281. dbgprintf(" pseudo_class=%s", pseudo_class_description);
  282. if (simple_selector.attribute_match_type != CSS::Selector::SimpleSelector::AttributeMatchType::None) {
  283. dbgprintf(" [%s, name='%s', value='%s']", attribute_match_type_description, simple_selector.attribute_name.characters(), simple_selector.attribute_value.characters());
  284. }
  285. if (i != complex_selector.compound_selector.size() - 1)
  286. dbgprintf(", ");
  287. }
  288. dbgprintf("\n");
  289. }
  290. }
  291. void dump_rule(const CSS::StyleRule& rule)
  292. {
  293. dbgprintf("Rule:\n");
  294. for (auto& selector : rule.selectors()) {
  295. dump_selector(selector);
  296. }
  297. dbgprintf(" Declarations:\n");
  298. for (auto& property : rule.declaration().properties()) {
  299. dbgprintf(" %s: '%s'\n", CSS::string_from_property_id(property.property_id), property.value->to_string().characters());
  300. }
  301. }
  302. void dump_sheet(const CSS::StyleSheet& sheet)
  303. {
  304. dbgprintf("StyleSheet{%p}: %d rule(s)\n", &sheet, sheet.rules().size());
  305. for (auto& rule : sheet.rules()) {
  306. dump_rule(rule);
  307. }
  308. }
  309. }