Dump.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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/LayoutBlock.h>
  40. #include <LibWeb/Layout/LayoutNode.h>
  41. #include <LibWeb/Layout/LayoutText.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 LayoutNode& layout_node)
  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.node()))
  88. tag_name = "#text";
  89. else if (is<DOM::Document>(layout_node.node()))
  90. tag_name = "#document";
  91. else if (is<DOM::Element>(layout_node.node()))
  92. tag_name = downcast<DOM::Element>(*layout_node.node()).local_name();
  93. else
  94. tag_name = "???";
  95. String identifier = "";
  96. if (layout_node.node() && is<DOM::Element>(*layout_node.node())) {
  97. auto& element = downcast<DOM::Element>(*layout_node.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("%s {\033[33m%s\033[0m%s}\n", layout_node.class_name(), tag_name.characters(), identifier.characters());
  112. } else {
  113. auto& layout_box = downcast<LayoutBox>(layout_node);
  114. dbgprintf("%s {\033[34m%s\033[0m%s} at (%g,%g) size %gx%g",
  115. layout_box.class_name(),
  116. tag_name.characters(),
  117. identifier.characters(),
  118. layout_box.absolute_x(),
  119. layout_box.absolute_y(),
  120. layout_box.width(),
  121. layout_box.height());
  122. // Dump the horizontal box properties
  123. dbgprintf(" [%g+%g+%g %g %g+%g+%g]",
  124. layout_box.box_model().margin.left.to_px(layout_box),
  125. layout_box.box_model().border.left.to_px(layout_box),
  126. layout_box.box_model().padding.left.to_px(layout_box),
  127. layout_box.width(),
  128. layout_box.box_model().padding.right.to_px(layout_box),
  129. layout_box.box_model().border.right.to_px(layout_box),
  130. layout_box.box_model().margin.right.to_px(layout_box));
  131. // And the vertical box properties
  132. dbgprintf(" [%g+%g+%g %g %g+%g+%g]",
  133. layout_box.box_model().margin.top.to_px(layout_box),
  134. layout_box.box_model().border.top.to_px(layout_box),
  135. layout_box.box_model().padding.top.to_px(layout_box),
  136. layout_box.height(),
  137. layout_box.box_model().padding.bottom.to_px(layout_box),
  138. layout_box.box_model().border.bottom.to_px(layout_box),
  139. layout_box.box_model().margin.bottom.to_px(layout_box));
  140. dbgprintf("\n");
  141. }
  142. if (layout_node.is_block() && static_cast<const LayoutBlock&>(layout_node).children_are_inline()) {
  143. auto& block = static_cast<const LayoutBlock&>(layout_node);
  144. for (size_t i = 0; i < indent; ++i)
  145. dbgprintf(" ");
  146. dbgprintf(" Line boxes (%d):\n", block.line_boxes().size());
  147. for (size_t line_box_index = 0; line_box_index < block.line_boxes().size(); ++line_box_index) {
  148. auto& line_box = block.line_boxes()[line_box_index];
  149. for (size_t i = 0; i < indent; ++i)
  150. dbgprintf(" ");
  151. dbgprintf(" [%d] width: %g\n", line_box_index, line_box.width());
  152. for (size_t fragment_index = 0; fragment_index < line_box.fragments().size(); ++fragment_index) {
  153. auto& fragment = line_box.fragments()[fragment_index];
  154. for (size_t i = 0; i < indent; ++i)
  155. dbgprintf(" ");
  156. dbgprintf(" [%d] layout_node: %s{%p}, start: %d, length: %d, rect: %s\n",
  157. fragment_index,
  158. fragment.layout_node().class_name(),
  159. &fragment.layout_node(),
  160. fragment.start(),
  161. fragment.length(),
  162. fragment.absolute_rect().to_string().characters());
  163. if (fragment.layout_node().is_text()) {
  164. for (size_t i = 0; i < indent; ++i)
  165. dbgprintf(" ");
  166. auto& layout_text = static_cast<const LayoutText&>(fragment.layout_node());
  167. auto fragment_text = layout_text.text_for_rendering().substring(fragment.start(), fragment.length());
  168. dbgprintf(" text: \"%s\"\n", fragment_text.characters());
  169. }
  170. }
  171. }
  172. }
  173. struct NameAndValue {
  174. String name;
  175. String value;
  176. };
  177. Vector<NameAndValue> properties;
  178. layout_node.specified_style().for_each_property([&](auto property_id, auto& value) {
  179. properties.append({ CSS::string_from_property_id(property_id), value.to_string() });
  180. });
  181. quick_sort(properties, [](auto& a, auto& b) { return a.name < b.name; });
  182. for (auto& property : properties) {
  183. for (size_t i = 0; i < indent; ++i)
  184. dbgprintf(" ");
  185. dbgprintf(" (%s: %s)\n", property.name.characters(), property.value.characters());
  186. }
  187. ++indent;
  188. layout_node.for_each_child([](auto& child) {
  189. dump_tree(child);
  190. });
  191. --indent;
  192. }
  193. void dump_selector(const CSS::Selector& selector)
  194. {
  195. dbgprintf(" CSS::Selector:\n");
  196. for (auto& complex_selector : selector.complex_selectors()) {
  197. dbgprintf(" ");
  198. const char* relation_description = "";
  199. switch (complex_selector.relation) {
  200. case CSS::Selector::ComplexSelector::Relation::None:
  201. relation_description = "None";
  202. break;
  203. case CSS::Selector::ComplexSelector::Relation::ImmediateChild:
  204. relation_description = "ImmediateChild";
  205. break;
  206. case CSS::Selector::ComplexSelector::Relation::Descendant:
  207. relation_description = "Descendant";
  208. break;
  209. case CSS::Selector::ComplexSelector::Relation::AdjacentSibling:
  210. relation_description = "AdjacentSibling";
  211. break;
  212. case CSS::Selector::ComplexSelector::Relation::GeneralSibling:
  213. relation_description = "GeneralSibling";
  214. break;
  215. }
  216. if (*relation_description)
  217. dbgprintf("{%s} ", relation_description);
  218. for (size_t i = 0; i < complex_selector.compound_selector.size(); ++i) {
  219. auto& simple_selector = complex_selector.compound_selector[i];
  220. const char* type_description = "Unknown";
  221. switch (simple_selector.type) {
  222. case CSS::Selector::SimpleSelector::Type::Invalid:
  223. type_description = "Invalid";
  224. break;
  225. case CSS::Selector::SimpleSelector::Type::Universal:
  226. type_description = "Universal";
  227. break;
  228. case CSS::Selector::SimpleSelector::Type::Id:
  229. type_description = "Id";
  230. break;
  231. case CSS::Selector::SimpleSelector::Type::Class:
  232. type_description = "Class";
  233. break;
  234. case CSS::Selector::SimpleSelector::Type::TagName:
  235. type_description = "TagName";
  236. break;
  237. }
  238. const char* attribute_match_type_description = "";
  239. switch (simple_selector.attribute_match_type) {
  240. case CSS::Selector::SimpleSelector::AttributeMatchType::None:
  241. break;
  242. case CSS::Selector::SimpleSelector::AttributeMatchType::HasAttribute:
  243. attribute_match_type_description = "HasAttribute";
  244. break;
  245. case CSS::Selector::SimpleSelector::AttributeMatchType::ExactValueMatch:
  246. attribute_match_type_description = "ExactValueMatch";
  247. break;
  248. case CSS::Selector::SimpleSelector::AttributeMatchType::Contains:
  249. attribute_match_type_description = "Contains";
  250. break;
  251. }
  252. const char* pseudo_class_description = "";
  253. switch (simple_selector.pseudo_class) {
  254. case CSS::Selector::SimpleSelector::PseudoClass::Link:
  255. pseudo_class_description = "Link";
  256. break;
  257. case CSS::Selector::SimpleSelector::PseudoClass::Visited:
  258. pseudo_class_description = "Visited";
  259. break;
  260. case CSS::Selector::SimpleSelector::PseudoClass::None:
  261. pseudo_class_description = "None";
  262. break;
  263. case CSS::Selector::SimpleSelector::PseudoClass::Root:
  264. pseudo_class_description = "Root";
  265. break;
  266. case CSS::Selector::SimpleSelector::PseudoClass::Focus:
  267. pseudo_class_description = "Focus";
  268. break;
  269. case CSS::Selector::SimpleSelector::PseudoClass::Empty:
  270. pseudo_class_description = "Empty";
  271. break;
  272. case CSS::Selector::SimpleSelector::PseudoClass::Hover:
  273. pseudo_class_description = "Hover";
  274. break;
  275. case CSS::Selector::SimpleSelector::PseudoClass::LastChild:
  276. pseudo_class_description = "LastChild";
  277. break;
  278. case CSS::Selector::SimpleSelector::PseudoClass::FirstChild:
  279. pseudo_class_description = "FirstChild";
  280. break;
  281. case CSS::Selector::SimpleSelector::PseudoClass::OnlyChild:
  282. pseudo_class_description = "OnlyChild";
  283. break;
  284. }
  285. dbgprintf("%s:%s", type_description, simple_selector.value.characters());
  286. if (simple_selector.pseudo_class != CSS::Selector::SimpleSelector::PseudoClass::None)
  287. dbgprintf(" pseudo_class=%s", pseudo_class_description);
  288. if (simple_selector.attribute_match_type != CSS::Selector::SimpleSelector::AttributeMatchType::None) {
  289. dbgprintf(" [%s, name='%s', value='%s']", attribute_match_type_description, simple_selector.attribute_name.characters(), simple_selector.attribute_value.characters());
  290. }
  291. if (i != complex_selector.compound_selector.size() - 1)
  292. dbgprintf(", ");
  293. }
  294. dbgprintf("\n");
  295. }
  296. }
  297. void dump_rule(const CSS::StyleRule& rule)
  298. {
  299. dbgprintf("Rule:\n");
  300. for (auto& selector : rule.selectors()) {
  301. dump_selector(selector);
  302. }
  303. dbgprintf(" Declarations:\n");
  304. for (auto& property : rule.declaration().properties()) {
  305. dbgprintf(" %s: '%s'\n", CSS::string_from_property_id(property.property_id), property.value->to_string().characters());
  306. }
  307. }
  308. void dump_sheet(const CSS::StyleSheet& sheet)
  309. {
  310. dbgprintf("StyleSheet{%p}: %d rule(s)\n", &sheet, sheet.rules().size());
  311. for (auto& rule : sheet.rules()) {
  312. dump_rule(rule);
  313. }
  314. }
  315. }