Dump.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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/Utf8View.h>
  27. #include <LibWeb/CSS/PropertyID.h>
  28. #include <LibWeb/CSS/StyleSheet.h>
  29. #include <LibWeb/DOM/Comment.h>
  30. #include <LibWeb/DOM/Document.h>
  31. #include <LibWeb/DOM/DocumentFragment.h>
  32. #include <LibWeb/DOM/DocumentType.h>
  33. #include <LibWeb/DOM/Element.h>
  34. #include <LibWeb/DOM/Text.h>
  35. #include <LibWeb/Dump.h>
  36. #include <LibWeb/Layout/LayoutBlock.h>
  37. #include <LibWeb/Layout/LayoutNode.h>
  38. #include <LibWeb/Layout/LayoutText.h>
  39. #include <stdio.h>
  40. namespace Web {
  41. void dump_tree(const Node& node)
  42. {
  43. static int indent = 0;
  44. for (int i = 0; i < indent; ++i)
  45. dbgprintf(" ");
  46. if (is<Document>(node)) {
  47. dbgprintf("*Document*\n");
  48. } else if (is<Element>(node)) {
  49. dbgprintf("<%s", to<Element>(node).tag_name().characters());
  50. to<Element>(node).for_each_attribute([](auto& name, auto& value) {
  51. dbgprintf(" %s=%s", name.characters(), value.characters());
  52. });
  53. dbgprintf(">\n");
  54. } else if (is<Text>(node)) {
  55. dbgprintf("\"%s\"\n", static_cast<const Text&>(node).data().characters());
  56. } else if (is<DocumentType>(node)) {
  57. dbgprintf("<!DOCTYPE html>\n");
  58. } else if (is<Comment>(node)) {
  59. dbgprintf("<!--%s-->\n", to<Comment>(node).data().characters());
  60. } else if (is<DocumentFragment>(node)) {
  61. dbgprintf("#document-fragment\n");
  62. }
  63. ++indent;
  64. if (is<ParentNode>(node)) {
  65. static_cast<const ParentNode&>(node).for_each_child([](auto& child) {
  66. dump_tree(child);
  67. });
  68. }
  69. --indent;
  70. }
  71. void dump_tree(const LayoutNode& layout_node)
  72. {
  73. static size_t indent = 0;
  74. for (size_t i = 0; i < indent; ++i)
  75. dbgprintf(" ");
  76. FlyString tag_name;
  77. if (layout_node.is_anonymous())
  78. tag_name = "(anonymous)";
  79. else if (is<Text>(layout_node.node()))
  80. tag_name = "#text";
  81. else if (is<Document>(layout_node.node()))
  82. tag_name = "#document";
  83. else if (is<Element>(layout_node.node()))
  84. tag_name = to<Element>(*layout_node.node()).tag_name();
  85. else
  86. tag_name = "???";
  87. if (!layout_node.is_box()) {
  88. dbgprintf("%s {%s}\n", layout_node.class_name(), tag_name.characters());
  89. } else {
  90. auto& layout_box = to<LayoutBox>(layout_node);
  91. dbgprintf("%s {%s} at (%g,%g) size %gx%g",
  92. layout_box.class_name(),
  93. tag_name.characters(),
  94. layout_box.x(),
  95. layout_box.y(),
  96. layout_box.width(),
  97. layout_box.height());
  98. // Dump the horizontal box properties
  99. dbgprintf(" [%g+%g+%g %g %g+%g+%g]",
  100. layout_box.box_model().margin().left.to_px(),
  101. layout_box.box_model().border().left.to_px(),
  102. layout_box.box_model().padding().left.to_px(),
  103. layout_box.width(),
  104. layout_box.box_model().padding().right.to_px(),
  105. layout_box.box_model().border().right.to_px(),
  106. layout_box.box_model().margin().right.to_px());
  107. // And the vertical box properties
  108. dbgprintf(" [%g+%g+%g %g %g+%g+%g]",
  109. layout_box.box_model().margin().top.to_px(),
  110. layout_box.box_model().border().top.to_px(),
  111. layout_box.box_model().padding().top.to_px(),
  112. layout_box.height(),
  113. layout_box.box_model().padding().bottom.to_px(),
  114. layout_box.box_model().border().bottom.to_px(),
  115. layout_box.box_model().margin().bottom.to_px());
  116. dbgprintf("\n");
  117. }
  118. if (layout_node.is_block() && static_cast<const LayoutBlock&>(layout_node).children_are_inline()) {
  119. auto& block = static_cast<const LayoutBlock&>(layout_node);
  120. for (size_t i = 0; i < indent; ++i)
  121. dbgprintf(" ");
  122. dbgprintf(" Line boxes (%d):\n", block.line_boxes().size());
  123. for (size_t line_box_index = 0; line_box_index < block.line_boxes().size(); ++line_box_index) {
  124. auto& line_box = block.line_boxes()[line_box_index];
  125. for (size_t i = 0; i < indent; ++i)
  126. dbgprintf(" ");
  127. dbgprintf(" [%d] width: %g\n", line_box_index, line_box.width());
  128. for (size_t fragment_index = 0; fragment_index < line_box.fragments().size(); ++fragment_index) {
  129. auto& fragment = line_box.fragments()[fragment_index];
  130. for (size_t i = 0; i < indent; ++i)
  131. dbgprintf(" ");
  132. dbgprintf(" [%d] layout_node: %s{%p}, start: %d, length: %d, rect: %s\n",
  133. fragment_index,
  134. fragment.layout_node().class_name(),
  135. &fragment.layout_node(),
  136. fragment.start(),
  137. fragment.length(),
  138. fragment.rect().to_string().characters());
  139. if (fragment.layout_node().is_text()) {
  140. for (size_t i = 0; i < indent; ++i)
  141. dbgprintf(" ");
  142. auto& layout_text = static_cast<const LayoutText&>(fragment.layout_node());
  143. auto fragment_text = layout_text.text_for_rendering().substring(fragment.start(), fragment.length());
  144. dbgprintf(" text: \"%s\"\n", fragment_text.characters());
  145. }
  146. }
  147. }
  148. }
  149. layout_node.style().for_each_property([&](auto property_id, auto& value) {
  150. for (size_t i = 0; i < indent; ++i)
  151. dbgprintf(" ");
  152. dbgprintf(" (%s: %s)\n", CSS::string_from_property_id(property_id), value.to_string().characters());
  153. });
  154. ++indent;
  155. layout_node.for_each_child([](auto& child) {
  156. dump_tree(child);
  157. });
  158. --indent;
  159. }
  160. void dump_selector(const Selector& selector)
  161. {
  162. dbgprintf(" Selector:\n");
  163. for (auto& complex_selector : selector.complex_selectors()) {
  164. dbgprintf(" ");
  165. const char* relation_description = "";
  166. switch (complex_selector.relation) {
  167. case Selector::ComplexSelector::Relation::None:
  168. break;
  169. case Selector::ComplexSelector::Relation::ImmediateChild:
  170. relation_description = "ImmediateChild";
  171. break;
  172. case Selector::ComplexSelector::Relation::Descendant:
  173. relation_description = "Descendant";
  174. break;
  175. case Selector::ComplexSelector::Relation::AdjacentSibling:
  176. relation_description = "AdjacentSibling";
  177. break;
  178. case Selector::ComplexSelector::Relation::GeneralSibling:
  179. relation_description = "GeneralSibling";
  180. break;
  181. }
  182. if (*relation_description)
  183. dbgprintf("{%s} ", relation_description);
  184. for (size_t i = 0; i < complex_selector.compound_selector.size(); ++i) {
  185. auto& simple_selector = complex_selector.compound_selector[i];
  186. const char* type_description = "Unknown";
  187. switch (simple_selector.type) {
  188. case Selector::SimpleSelector::Type::Invalid:
  189. type_description = "Invalid";
  190. break;
  191. case Selector::SimpleSelector::Type::Universal:
  192. type_description = "Universal";
  193. break;
  194. case Selector::SimpleSelector::Type::Id:
  195. type_description = "Id";
  196. break;
  197. case Selector::SimpleSelector::Type::Class:
  198. type_description = "Class";
  199. break;
  200. case Selector::SimpleSelector::Type::TagName:
  201. type_description = "TagName";
  202. break;
  203. }
  204. const char* attribute_match_type_description = "";
  205. switch (simple_selector.attribute_match_type) {
  206. case Selector::SimpleSelector::AttributeMatchType::None:
  207. break;
  208. case Selector::SimpleSelector::AttributeMatchType::HasAttribute:
  209. attribute_match_type_description = "HasAttribute";
  210. break;
  211. case Selector::SimpleSelector::AttributeMatchType::ExactValueMatch:
  212. attribute_match_type_description = "ExactValueMatch";
  213. break;
  214. }
  215. dbgprintf("%s:%s", type_description, simple_selector.value.characters());
  216. if (simple_selector.attribute_match_type != Selector::SimpleSelector::AttributeMatchType::None) {
  217. dbgprintf(" [%s, name='%s', value='%s']", attribute_match_type_description, simple_selector.attribute_name.characters(), simple_selector.attribute_value.characters());
  218. }
  219. if (i != complex_selector.compound_selector.size() - 1)
  220. dbgprintf(", ");
  221. }
  222. dbgprintf("\n");
  223. }
  224. }
  225. void dump_rule(const StyleRule& rule)
  226. {
  227. dbgprintf("Rule:\n");
  228. for (auto& selector : rule.selectors()) {
  229. dump_selector(selector);
  230. }
  231. dbgprintf(" Declarations:\n");
  232. for (auto& property : rule.declaration().properties()) {
  233. dbgprintf(" %s: '%s'\n", CSS::string_from_property_id(property.property_id), property.value->to_string().characters());
  234. }
  235. }
  236. void dump_sheet(const StyleSheet& sheet)
  237. {
  238. dbgprintf("StyleSheet{%p}: %d rule(s)\n", &sheet, sheet.rules().size());
  239. for (auto& rule : sheet.rules()) {
  240. dump_rule(rule);
  241. }
  242. }
  243. }