Dump.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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/BlockBox.h>
  40. #include <LibWeb/Layout/Node.h>
  41. #include <LibWeb/Layout/TextNode.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 Layout::Node& layout_node, bool show_box_model, bool show_specified_style)
  80. {
  81. StringBuilder builder;
  82. dump_tree(builder, layout_node, show_box_model, show_specified_style, true);
  83. dbgprintf("%s", builder.to_string().characters());
  84. }
  85. void dump_tree(StringBuilder& builder, const Layout::Node& layout_node, bool show_box_model, bool show_specified_style, bool interactive)
  86. {
  87. static size_t indent = 0;
  88. for (size_t i = 0; i < indent; ++i)
  89. builder.append(" ");
  90. FlyString tag_name;
  91. if (layout_node.is_anonymous())
  92. tag_name = "(anonymous)";
  93. else if (is<DOM::Text>(layout_node.dom_node()))
  94. tag_name = "#text";
  95. else if (is<DOM::Document>(layout_node.dom_node()))
  96. tag_name = "#document";
  97. else if (is<DOM::Element>(layout_node.dom_node()))
  98. tag_name = downcast<DOM::Element>(*layout_node.dom_node()).local_name();
  99. else
  100. tag_name = "???";
  101. String identifier = "";
  102. if (layout_node.dom_node() && is<DOM::Element>(*layout_node.dom_node())) {
  103. auto& element = downcast<DOM::Element>(*layout_node.dom_node());
  104. StringBuilder builder;
  105. auto id = element.attribute(HTML::AttributeNames::id);
  106. if (!id.is_empty()) {
  107. builder.append('#');
  108. builder.append(id);
  109. }
  110. for (auto& class_name : element.class_names()) {
  111. builder.append('.');
  112. builder.append(class_name);
  113. }
  114. identifier = builder.to_string();
  115. }
  116. const char* nonbox_color_on = "";
  117. const char* box_color_on = "";
  118. const char* positioned_color_on = "";
  119. const char* floating_color_on = "";
  120. const char* inline_block_color_on = "";
  121. const char* line_box_color_on = "";
  122. const char* fragment_color_on = "";
  123. const char* color_off = "";
  124. if (interactive) {
  125. nonbox_color_on = "\033[33m";
  126. box_color_on = "\033[34m";
  127. positioned_color_on = "\033[31;1m";
  128. floating_color_on = "\033[32;1m";
  129. inline_block_color_on = "\033[36;1m";
  130. line_box_color_on = "\033[34;1m";
  131. fragment_color_on = "\033[35;1m";
  132. color_off = "\033[0m";
  133. }
  134. if (!is<Layout::Box>(layout_node)) {
  135. builder.appendff("{}{}{} <{}{}{}>",
  136. nonbox_color_on,
  137. layout_node.class_name().substring_view(13),
  138. color_off,
  139. tag_name,
  140. nonbox_color_on,
  141. identifier,
  142. color_off);
  143. if (interactive)
  144. builder.appendff(" @{:p}", &layout_node);
  145. builder.append("\n");
  146. } else {
  147. auto& box = downcast<Layout::Box>(layout_node);
  148. builder.appendff("{}{}{} <{}{}{}{}> ",
  149. box_color_on,
  150. box.class_name().substring_view(13),
  151. color_off,
  152. box_color_on,
  153. tag_name,
  154. color_off,
  155. identifier.characters());
  156. if (interactive)
  157. builder.appendff("@{:p} ", &layout_node);
  158. builder.appendff("at ({},{}) size {}x{}",
  159. (int)box.absolute_x(),
  160. (int)box.absolute_y(),
  161. (int)box.width(),
  162. (int)box.height());
  163. if (box.is_positioned())
  164. builder.appendff(" {}positioned{}", positioned_color_on, color_off);
  165. if (box.is_floating())
  166. builder.appendff(" {}floating{}", floating_color_on, color_off);
  167. if (box.is_inline_block())
  168. builder.appendff(" {}inline-block{}", inline_block_color_on, color_off);
  169. if (show_box_model) {
  170. // Dump the horizontal box properties
  171. builder.appendf(" [%g+%g+%g %g %g+%g+%g]",
  172. box.box_model().margin.left,
  173. box.box_model().border.left,
  174. box.box_model().padding.left,
  175. box.width(),
  176. box.box_model().padding.right,
  177. box.box_model().border.right,
  178. box.box_model().margin.right);
  179. // And the vertical box properties
  180. builder.appendf(" [%g+%g+%g %g %g+%g+%g]",
  181. box.box_model().margin.top,
  182. box.box_model().border.top,
  183. box.box_model().padding.top,
  184. box.height(),
  185. box.box_model().padding.bottom,
  186. box.box_model().border.bottom,
  187. box.box_model().margin.bottom);
  188. }
  189. builder.append("\n");
  190. }
  191. if (is<Layout::BlockBox>(layout_node) && static_cast<const Layout::BlockBox&>(layout_node).children_are_inline()) {
  192. auto& block = static_cast<const Layout::BlockBox&>(layout_node);
  193. for (size_t line_box_index = 0; line_box_index < block.line_boxes().size(); ++line_box_index) {
  194. auto& line_box = block.line_boxes()[line_box_index];
  195. for (size_t i = 0; i < indent; ++i)
  196. builder.append(" ");
  197. builder.appendff(" {}line {}{} width: {}\n",
  198. line_box_color_on,
  199. line_box_index,
  200. color_off,
  201. (int)line_box.width());
  202. for (size_t fragment_index = 0; fragment_index < line_box.fragments().size(); ++fragment_index) {
  203. auto& fragment = line_box.fragments()[fragment_index];
  204. for (size_t i = 0; i < indent; ++i)
  205. builder.append(" ");
  206. builder.appendff(" {}frag {}{} from {} ",
  207. fragment_color_on,
  208. fragment_index,
  209. color_off,
  210. fragment.layout_node().class_name());
  211. if (interactive)
  212. builder.appendff("@{:p}, ", &fragment.layout_node());
  213. builder.appendff("start: {}, length: {}, rect: {}\n",
  214. fragment.start(),
  215. fragment.length(),
  216. enclosing_int_rect(fragment.absolute_rect()).to_string());
  217. if (is<Layout::TextNode>(fragment.layout_node())) {
  218. for (size_t i = 0; i < indent; ++i)
  219. builder.append(" ");
  220. auto& layout_text = static_cast<const Layout::TextNode&>(fragment.layout_node());
  221. auto fragment_text = layout_text.text_for_rendering().substring(fragment.start(), fragment.length());
  222. builder.appendff(" \"{}\"\n", fragment_text);
  223. }
  224. }
  225. }
  226. }
  227. if (show_specified_style && layout_node.dom_node() && layout_node.dom_node()->is_element() && downcast<DOM::Element>(layout_node.dom_node())->specified_css_values()) {
  228. struct NameAndValue {
  229. String name;
  230. String value;
  231. };
  232. Vector<NameAndValue> properties;
  233. downcast<DOM::Element>(*layout_node.dom_node()).specified_css_values()->for_each_property([&](auto property_id, auto& value) {
  234. properties.append({ CSS::string_from_property_id(property_id), value.to_string() });
  235. });
  236. quick_sort(properties, [](auto& a, auto& b) { return a.name < b.name; });
  237. for (auto& property : properties) {
  238. for (size_t i = 0; i < indent; ++i)
  239. builder.append(" ");
  240. builder.appendf(" (%s: %s)\n", property.name.characters(), property.value.characters());
  241. }
  242. }
  243. ++indent;
  244. layout_node.for_each_child([&](auto& child) {
  245. dump_tree(builder, child, show_box_model, show_specified_style, interactive);
  246. });
  247. --indent;
  248. }
  249. void dump_selector(const CSS::Selector& selector)
  250. {
  251. dbgprintf(" CSS::Selector:\n");
  252. for (auto& complex_selector : selector.complex_selectors()) {
  253. dbgprintf(" ");
  254. const char* relation_description = "";
  255. switch (complex_selector.relation) {
  256. case CSS::Selector::ComplexSelector::Relation::None:
  257. relation_description = "None";
  258. break;
  259. case CSS::Selector::ComplexSelector::Relation::ImmediateChild:
  260. relation_description = "ImmediateChild";
  261. break;
  262. case CSS::Selector::ComplexSelector::Relation::Descendant:
  263. relation_description = "Descendant";
  264. break;
  265. case CSS::Selector::ComplexSelector::Relation::AdjacentSibling:
  266. relation_description = "AdjacentSibling";
  267. break;
  268. case CSS::Selector::ComplexSelector::Relation::GeneralSibling:
  269. relation_description = "GeneralSibling";
  270. break;
  271. }
  272. if (*relation_description)
  273. dbgprintf("{%s} ", relation_description);
  274. for (size_t i = 0; i < complex_selector.compound_selector.size(); ++i) {
  275. auto& simple_selector = complex_selector.compound_selector[i];
  276. const char* type_description = "Unknown";
  277. switch (simple_selector.type) {
  278. case CSS::Selector::SimpleSelector::Type::Invalid:
  279. type_description = "Invalid";
  280. break;
  281. case CSS::Selector::SimpleSelector::Type::Universal:
  282. type_description = "Universal";
  283. break;
  284. case CSS::Selector::SimpleSelector::Type::Id:
  285. type_description = "Id";
  286. break;
  287. case CSS::Selector::SimpleSelector::Type::Class:
  288. type_description = "Class";
  289. break;
  290. case CSS::Selector::SimpleSelector::Type::TagName:
  291. type_description = "TagName";
  292. break;
  293. }
  294. const char* attribute_match_type_description = "";
  295. switch (simple_selector.attribute_match_type) {
  296. case CSS::Selector::SimpleSelector::AttributeMatchType::None:
  297. break;
  298. case CSS::Selector::SimpleSelector::AttributeMatchType::HasAttribute:
  299. attribute_match_type_description = "HasAttribute";
  300. break;
  301. case CSS::Selector::SimpleSelector::AttributeMatchType::ExactValueMatch:
  302. attribute_match_type_description = "ExactValueMatch";
  303. break;
  304. case CSS::Selector::SimpleSelector::AttributeMatchType::Contains:
  305. attribute_match_type_description = "Contains";
  306. break;
  307. }
  308. const char* pseudo_class_description = "";
  309. switch (simple_selector.pseudo_class) {
  310. case CSS::Selector::SimpleSelector::PseudoClass::Link:
  311. pseudo_class_description = "Link";
  312. break;
  313. case CSS::Selector::SimpleSelector::PseudoClass::Visited:
  314. pseudo_class_description = "Visited";
  315. break;
  316. case CSS::Selector::SimpleSelector::PseudoClass::None:
  317. pseudo_class_description = "None";
  318. break;
  319. case CSS::Selector::SimpleSelector::PseudoClass::Root:
  320. pseudo_class_description = "Root";
  321. break;
  322. case CSS::Selector::SimpleSelector::PseudoClass::Focus:
  323. pseudo_class_description = "Focus";
  324. break;
  325. case CSS::Selector::SimpleSelector::PseudoClass::Empty:
  326. pseudo_class_description = "Empty";
  327. break;
  328. case CSS::Selector::SimpleSelector::PseudoClass::Hover:
  329. pseudo_class_description = "Hover";
  330. break;
  331. case CSS::Selector::SimpleSelector::PseudoClass::LastChild:
  332. pseudo_class_description = "LastChild";
  333. break;
  334. case CSS::Selector::SimpleSelector::PseudoClass::FirstChild:
  335. pseudo_class_description = "FirstChild";
  336. break;
  337. case CSS::Selector::SimpleSelector::PseudoClass::OnlyChild:
  338. pseudo_class_description = "OnlyChild";
  339. break;
  340. }
  341. dbgprintf("%s:%s", type_description, simple_selector.value.characters());
  342. if (simple_selector.pseudo_class != CSS::Selector::SimpleSelector::PseudoClass::None)
  343. dbgprintf(" pseudo_class=%s", pseudo_class_description);
  344. if (simple_selector.attribute_match_type != CSS::Selector::SimpleSelector::AttributeMatchType::None) {
  345. dbgprintf(" [%s, name='%s', value='%s']", attribute_match_type_description, simple_selector.attribute_name.characters(), simple_selector.attribute_value.characters());
  346. }
  347. if (i != complex_selector.compound_selector.size() - 1)
  348. dbgprintf(", ");
  349. }
  350. dbgprintf("\n");
  351. }
  352. }
  353. void dump_rule(const CSS::StyleRule& rule)
  354. {
  355. dbgprintf("Rule:\n");
  356. for (auto& selector : rule.selectors()) {
  357. dump_selector(selector);
  358. }
  359. dbgprintf(" Declarations:\n");
  360. for (auto& property : rule.declaration().properties()) {
  361. dbgprintf(" %s: '%s'\n", CSS::string_from_property_id(property.property_id), property.value->to_string().characters());
  362. }
  363. }
  364. void dump_sheet(const CSS::StyleSheet& sheet)
  365. {
  366. dbgprintf("StyleSheet{%p}: %zu rule(s)\n", &sheet, sheet.rules().size());
  367. for (auto& rule : sheet.rules()) {
  368. dump_rule(rule);
  369. }
  370. }
  371. }