Dump.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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 id = to<Element>(*layout_node.node()).attribute(HTML::AttributeNames::id);
  91. if (!id.is_empty()) {
  92. StringBuilder builder;
  93. builder.append('#');
  94. builder.append(id);
  95. identifier = builder.to_string();
  96. }
  97. }
  98. if (!layout_node.is_box()) {
  99. dbgprintf("%s {\033[33m%s\033[0m%s}\n", layout_node.class_name(), tag_name.characters(), identifier.characters());
  100. } else {
  101. auto& layout_box = to<LayoutBox>(layout_node);
  102. dbgprintf("%s {\033[34m%s\033[0m%s} at (%g,%g) size %gx%g",
  103. layout_box.class_name(),
  104. tag_name.characters(),
  105. identifier.characters(),
  106. layout_box.absolute_x(),
  107. layout_box.absolute_y(),
  108. layout_box.width(),
  109. layout_box.height());
  110. // Dump the horizontal box properties
  111. dbgprintf(" [%g+%g+%g %g %g+%g+%g]",
  112. layout_box.box_model().margin().left.to_px(layout_box),
  113. layout_box.box_model().border().left.to_px(layout_box),
  114. layout_box.box_model().padding().left.to_px(layout_box),
  115. layout_box.width(),
  116. layout_box.box_model().padding().right.to_px(layout_box),
  117. layout_box.box_model().border().right.to_px(layout_box),
  118. layout_box.box_model().margin().right.to_px(layout_box));
  119. // And the vertical box properties
  120. dbgprintf(" [%g+%g+%g %g %g+%g+%g]",
  121. layout_box.box_model().margin().top.to_px(layout_box),
  122. layout_box.box_model().border().top.to_px(layout_box),
  123. layout_box.box_model().padding().top.to_px(layout_box),
  124. layout_box.height(),
  125. layout_box.box_model().padding().bottom.to_px(layout_box),
  126. layout_box.box_model().border().bottom.to_px(layout_box),
  127. layout_box.box_model().margin().bottom.to_px(layout_box));
  128. dbgprintf("\n");
  129. }
  130. if (layout_node.is_block() && static_cast<const LayoutBlock&>(layout_node).children_are_inline()) {
  131. auto& block = static_cast<const LayoutBlock&>(layout_node);
  132. for (size_t i = 0; i < indent; ++i)
  133. dbgprintf(" ");
  134. dbgprintf(" Line boxes (%d):\n", block.line_boxes().size());
  135. for (size_t line_box_index = 0; line_box_index < block.line_boxes().size(); ++line_box_index) {
  136. auto& line_box = block.line_boxes()[line_box_index];
  137. for (size_t i = 0; i < indent; ++i)
  138. dbgprintf(" ");
  139. dbgprintf(" [%d] width: %g\n", line_box_index, line_box.width());
  140. for (size_t fragment_index = 0; fragment_index < line_box.fragments().size(); ++fragment_index) {
  141. auto& fragment = line_box.fragments()[fragment_index];
  142. for (size_t i = 0; i < indent; ++i)
  143. dbgprintf(" ");
  144. dbgprintf(" [%d] layout_node: %s{%p}, start: %d, length: %d, rect: %s\n",
  145. fragment_index,
  146. fragment.layout_node().class_name(),
  147. &fragment.layout_node(),
  148. fragment.start(),
  149. fragment.length(),
  150. fragment.absolute_rect().to_string().characters());
  151. if (fragment.layout_node().is_text()) {
  152. for (size_t i = 0; i < indent; ++i)
  153. dbgprintf(" ");
  154. auto& layout_text = static_cast<const LayoutText&>(fragment.layout_node());
  155. auto fragment_text = layout_text.text_for_rendering().substring(fragment.start(), fragment.length());
  156. dbgprintf(" text: \"%s\"\n", fragment_text.characters());
  157. }
  158. }
  159. }
  160. }
  161. layout_node.style().for_each_property([&](auto property_id, auto& value) {
  162. for (size_t i = 0; i < indent; ++i)
  163. dbgprintf(" ");
  164. dbgprintf(" (%s: %s)\n", CSS::string_from_property_id(property_id), value.to_string().characters());
  165. });
  166. ++indent;
  167. layout_node.for_each_child([](auto& child) {
  168. dump_tree(child);
  169. });
  170. --indent;
  171. }
  172. void dump_selector(const Selector& selector)
  173. {
  174. dbgprintf(" Selector:\n");
  175. for (auto& complex_selector : selector.complex_selectors()) {
  176. dbgprintf(" ");
  177. const char* relation_description = "";
  178. switch (complex_selector.relation) {
  179. case Selector::ComplexSelector::Relation::None:
  180. relation_description = "None";
  181. break;
  182. case Selector::ComplexSelector::Relation::ImmediateChild:
  183. relation_description = "ImmediateChild";
  184. break;
  185. case Selector::ComplexSelector::Relation::Descendant:
  186. relation_description = "Descendant";
  187. break;
  188. case Selector::ComplexSelector::Relation::AdjacentSibling:
  189. relation_description = "AdjacentSibling";
  190. break;
  191. case Selector::ComplexSelector::Relation::GeneralSibling:
  192. relation_description = "GeneralSibling";
  193. break;
  194. }
  195. if (*relation_description)
  196. dbgprintf("{%s} ", relation_description);
  197. for (size_t i = 0; i < complex_selector.compound_selector.size(); ++i) {
  198. auto& simple_selector = complex_selector.compound_selector[i];
  199. const char* type_description = "Unknown";
  200. switch (simple_selector.type) {
  201. case Selector::SimpleSelector::Type::Invalid:
  202. type_description = "Invalid";
  203. break;
  204. case Selector::SimpleSelector::Type::Universal:
  205. type_description = "Universal";
  206. break;
  207. case Selector::SimpleSelector::Type::Id:
  208. type_description = "Id";
  209. break;
  210. case Selector::SimpleSelector::Type::Class:
  211. type_description = "Class";
  212. break;
  213. case Selector::SimpleSelector::Type::TagName:
  214. type_description = "TagName";
  215. break;
  216. }
  217. const char* attribute_match_type_description = "";
  218. switch (simple_selector.attribute_match_type) {
  219. case Selector::SimpleSelector::AttributeMatchType::None:
  220. break;
  221. case Selector::SimpleSelector::AttributeMatchType::HasAttribute:
  222. attribute_match_type_description = "HasAttribute";
  223. break;
  224. case Selector::SimpleSelector::AttributeMatchType::ExactValueMatch:
  225. attribute_match_type_description = "ExactValueMatch";
  226. break;
  227. case Selector::SimpleSelector::AttributeMatchType::Contains:
  228. attribute_match_type_description = "Contains";
  229. break;
  230. }
  231. dbgprintf("%s:%s", type_description, simple_selector.value.characters());
  232. if (simple_selector.attribute_match_type != Selector::SimpleSelector::AttributeMatchType::None) {
  233. dbgprintf(" [%s, name='%s', value='%s']", attribute_match_type_description, simple_selector.attribute_name.characters(), simple_selector.attribute_value.characters());
  234. }
  235. if (i != complex_selector.compound_selector.size() - 1)
  236. dbgprintf(", ");
  237. }
  238. dbgprintf("\n");
  239. }
  240. }
  241. void dump_rule(const StyleRule& rule)
  242. {
  243. dbgprintf("Rule:\n");
  244. for (auto& selector : rule.selectors()) {
  245. dump_selector(selector);
  246. }
  247. dbgprintf(" Declarations:\n");
  248. for (auto& property : rule.declaration().properties()) {
  249. dbgprintf(" %s: '%s'\n", CSS::string_from_property_id(property.property_id), property.value->to_string().characters());
  250. }
  251. }
  252. void dump_sheet(const StyleSheet& sheet)
  253. {
  254. dbgprintf("StyleSheet{%p}: %d rule(s)\n", &sheet, sheet.rules().size());
  255. for (auto& rule : sheet.rules()) {
  256. dump_rule(rule);
  257. }
  258. }
  259. }