Dump.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, the SerenityOS developers.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form must reproduce the above copyright notice,
  13. * this list of conditions and the following disclaimer in the documentation
  14. * and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  20. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  22. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  23. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  24. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include <AK/QuickSort.h>
  28. #include <AK/StringBuilder.h>
  29. #include <AK/Utf8View.h>
  30. #include <LibWeb/CSS/CSSImportRule.h>
  31. #include <LibWeb/CSS/CSSRule.h>
  32. #include <LibWeb/CSS/CSSStyleRule.h>
  33. #include <LibWeb/CSS/CSSStyleSheet.h>
  34. #include <LibWeb/CSS/PropertyID.h>
  35. #include <LibWeb/DOM/Comment.h>
  36. #include <LibWeb/DOM/Document.h>
  37. #include <LibWeb/DOM/Element.h>
  38. #include <LibWeb/DOM/ShadowRoot.h>
  39. #include <LibWeb/DOM/Text.h>
  40. #include <LibWeb/Dump.h>
  41. #include <LibWeb/HTML/HTMLTemplateElement.h>
  42. #include <LibWeb/Layout/BlockBox.h>
  43. #include <LibWeb/Layout/Node.h>
  44. #include <LibWeb/Layout/TextNode.h>
  45. #include <stdio.h>
  46. namespace Web {
  47. void dump_tree(const DOM::Node& node)
  48. {
  49. StringBuilder builder;
  50. dump_tree(builder, node);
  51. dbgln("{}", builder.string_view());
  52. }
  53. void dump_tree(StringBuilder& builder, const DOM::Node& node)
  54. {
  55. static int indent = 0;
  56. for (int i = 0; i < indent; ++i)
  57. builder.append(" ");
  58. if (is<DOM::Element>(node)) {
  59. builder.appendff("<{}", downcast<DOM::Element>(node).local_name());
  60. downcast<DOM::Element>(node).for_each_attribute([&](auto& name, auto& value) {
  61. builder.appendff(" {}={}", name, value);
  62. });
  63. builder.append(">\n");
  64. } else if (is<DOM::Text>(node)) {
  65. builder.appendff("\"{}\"\n", downcast<DOM::Text>(node).data());
  66. } else {
  67. builder.appendff("{}\n", node.node_name());
  68. }
  69. ++indent;
  70. if (is<DOM::Element>(node) && downcast<DOM::Element>(node).shadow_root()) {
  71. dump_tree(*downcast<DOM::Element>(node).shadow_root());
  72. }
  73. if (is<DOM::ParentNode>(node)) {
  74. if (!is<HTML::HTMLTemplateElement>(node)) {
  75. static_cast<const DOM::ParentNode&>(node).for_each_child([](auto& child) {
  76. dump_tree(child);
  77. });
  78. } else {
  79. auto& template_element = downcast<HTML::HTMLTemplateElement>(node);
  80. dump_tree(template_element.content());
  81. }
  82. }
  83. --indent;
  84. }
  85. void dump_tree(const Layout::Node& layout_node, bool show_box_model, bool show_specified_style)
  86. {
  87. StringBuilder builder;
  88. dump_tree(builder, layout_node, show_box_model, show_specified_style, true);
  89. dbgln("{}", builder.string_view());
  90. }
  91. void dump_tree(StringBuilder& builder, const Layout::Node& layout_node, bool show_box_model, bool show_specified_style, bool interactive)
  92. {
  93. static size_t indent = 0;
  94. for (size_t i = 0; i < indent; ++i)
  95. builder.append(" ");
  96. FlyString tag_name;
  97. if (layout_node.is_anonymous())
  98. tag_name = "(anonymous)";
  99. else if (is<DOM::Element>(layout_node.dom_node()))
  100. tag_name = downcast<DOM::Element>(*layout_node.dom_node()).local_name();
  101. else
  102. tag_name = layout_node.dom_node()->node_name();
  103. String identifier = "";
  104. if (layout_node.dom_node() && is<DOM::Element>(*layout_node.dom_node())) {
  105. auto& element = downcast<DOM::Element>(*layout_node.dom_node());
  106. StringBuilder builder;
  107. auto id = element.attribute(HTML::AttributeNames::id);
  108. if (!id.is_empty()) {
  109. builder.append('#');
  110. builder.append(id);
  111. }
  112. for (auto& class_name : element.class_names()) {
  113. builder.append('.');
  114. builder.append(class_name);
  115. }
  116. identifier = builder.to_string();
  117. }
  118. const char* nonbox_color_on = "";
  119. const char* box_color_on = "";
  120. const char* positioned_color_on = "";
  121. const char* floating_color_on = "";
  122. const char* inline_block_color_on = "";
  123. const char* line_box_color_on = "";
  124. const char* fragment_color_on = "";
  125. const char* flex_color_on = "";
  126. const char* color_off = "";
  127. if (interactive) {
  128. nonbox_color_on = "\033[33m";
  129. box_color_on = "\033[34m";
  130. positioned_color_on = "\033[31;1m";
  131. floating_color_on = "\033[32;1m";
  132. inline_block_color_on = "\033[36;1m";
  133. line_box_color_on = "\033[34;1m";
  134. fragment_color_on = "\033[35;1m";
  135. flex_color_on = "\033[34;1m";
  136. color_off = "\033[0m";
  137. }
  138. if (!is<Layout::Box>(layout_node)) {
  139. builder.appendff("{}{}{} <{}{}{}{}>",
  140. nonbox_color_on,
  141. layout_node.class_name().substring_view(13),
  142. color_off,
  143. tag_name,
  144. nonbox_color_on,
  145. identifier,
  146. color_off);
  147. if (interactive)
  148. builder.appendff(" @{:p}", &layout_node);
  149. builder.append("\n");
  150. } else {
  151. auto& box = downcast<Layout::Box>(layout_node);
  152. builder.appendff("{}{}{} <{}{}{}{}> ",
  153. box_color_on,
  154. box.class_name().substring_view(13),
  155. color_off,
  156. box_color_on,
  157. tag_name,
  158. color_off,
  159. identifier.characters());
  160. if (interactive)
  161. builder.appendff("@{:p} ", &layout_node);
  162. builder.appendff("at ({},{}) size {}x{}",
  163. (int)box.absolute_x(),
  164. (int)box.absolute_y(),
  165. (int)box.width(),
  166. (int)box.height());
  167. if (box.is_positioned())
  168. builder.appendff(" {}positioned{}", positioned_color_on, color_off);
  169. if (box.is_floating())
  170. builder.appendff(" {}floating{}", floating_color_on, color_off);
  171. if (box.is_inline_block())
  172. builder.appendff(" {}inline-block{}", inline_block_color_on, color_off);
  173. if (box.computed_values().display() == CSS::Display::Flex)
  174. builder.appendff(" {}flex{}", flex_color_on, color_off);
  175. if (show_box_model) {
  176. // Dump the horizontal box properties
  177. builder.appendff(" [{}+{}+{} {} {}+{}+{}]",
  178. box.box_model().margin.left,
  179. box.box_model().border.left,
  180. box.box_model().padding.left,
  181. box.width(),
  182. box.box_model().padding.right,
  183. box.box_model().border.right,
  184. box.box_model().margin.right);
  185. // And the vertical box properties
  186. builder.appendff(" [{}+{}+{} {} {}+{}+{}]",
  187. box.box_model().margin.top,
  188. box.box_model().border.top,
  189. box.box_model().padding.top,
  190. box.height(),
  191. box.box_model().padding.bottom,
  192. box.box_model().border.bottom,
  193. box.box_model().margin.bottom);
  194. }
  195. builder.append("\n");
  196. }
  197. if (is<Layout::BlockBox>(layout_node) && static_cast<const Layout::BlockBox&>(layout_node).children_are_inline()) {
  198. auto& block = static_cast<const Layout::BlockBox&>(layout_node);
  199. for (size_t line_box_index = 0; line_box_index < block.line_boxes().size(); ++line_box_index) {
  200. auto& line_box = block.line_boxes()[line_box_index];
  201. for (size_t i = 0; i < indent; ++i)
  202. builder.append(" ");
  203. builder.appendff(" {}line {}{} width: {}\n",
  204. line_box_color_on,
  205. line_box_index,
  206. color_off,
  207. (int)line_box.width());
  208. for (size_t fragment_index = 0; fragment_index < line_box.fragments().size(); ++fragment_index) {
  209. auto& fragment = line_box.fragments()[fragment_index];
  210. for (size_t i = 0; i < indent; ++i)
  211. builder.append(" ");
  212. builder.appendff(" {}frag {}{} from {} ",
  213. fragment_color_on,
  214. fragment_index,
  215. color_off,
  216. fragment.layout_node().class_name());
  217. if (interactive)
  218. builder.appendff("@{:p}, ", &fragment.layout_node());
  219. builder.appendff("start: {}, length: {}, rect: {}\n",
  220. fragment.start(),
  221. fragment.length(),
  222. enclosing_int_rect(fragment.absolute_rect()).to_string());
  223. if (is<Layout::TextNode>(fragment.layout_node())) {
  224. for (size_t i = 0; i < indent; ++i)
  225. builder.append(" ");
  226. auto& layout_text = static_cast<const Layout::TextNode&>(fragment.layout_node());
  227. auto fragment_text = layout_text.text_for_rendering().substring(fragment.start(), fragment.length());
  228. builder.appendff(" \"{}\"\n", fragment_text);
  229. }
  230. }
  231. }
  232. }
  233. if (show_specified_style && layout_node.dom_node() && layout_node.dom_node()->is_element() && downcast<DOM::Element>(layout_node.dom_node())->specified_css_values()) {
  234. struct NameAndValue {
  235. String name;
  236. String value;
  237. };
  238. Vector<NameAndValue> properties;
  239. downcast<DOM::Element>(*layout_node.dom_node()).specified_css_values()->for_each_property([&](auto property_id, auto& value) {
  240. properties.append({ CSS::string_from_property_id(property_id), value.to_string() });
  241. });
  242. quick_sort(properties, [](auto& a, auto& b) { return a.name < b.name; });
  243. for (auto& property : properties) {
  244. for (size_t i = 0; i < indent; ++i)
  245. builder.append(" ");
  246. builder.appendf(" (%s: %s)\n", property.name.characters(), property.value.characters());
  247. }
  248. }
  249. ++indent;
  250. layout_node.for_each_child([&](auto& child) {
  251. dump_tree(builder, child, show_box_model, show_specified_style, interactive);
  252. });
  253. --indent;
  254. }
  255. void dump_selector(const CSS::Selector& selector)
  256. {
  257. StringBuilder builder;
  258. dump_selector(builder, selector);
  259. dbgln("{}", builder.string_view());
  260. }
  261. void dump_selector(StringBuilder& builder, const CSS::Selector& selector)
  262. {
  263. builder.append(" CSS::Selector:\n");
  264. for (auto& complex_selector : selector.complex_selectors()) {
  265. builder.append(" ");
  266. const char* relation_description = "";
  267. switch (complex_selector.relation) {
  268. case CSS::Selector::ComplexSelector::Relation::None:
  269. relation_description = "None";
  270. break;
  271. case CSS::Selector::ComplexSelector::Relation::ImmediateChild:
  272. relation_description = "ImmediateChild";
  273. break;
  274. case CSS::Selector::ComplexSelector::Relation::Descendant:
  275. relation_description = "Descendant";
  276. break;
  277. case CSS::Selector::ComplexSelector::Relation::AdjacentSibling:
  278. relation_description = "AdjacentSibling";
  279. break;
  280. case CSS::Selector::ComplexSelector::Relation::GeneralSibling:
  281. relation_description = "GeneralSibling";
  282. break;
  283. }
  284. if (*relation_description)
  285. builder.appendff("{{{}}} ", relation_description);
  286. for (size_t i = 0; i < complex_selector.compound_selector.size(); ++i) {
  287. auto& simple_selector = complex_selector.compound_selector[i];
  288. const char* type_description = "Unknown";
  289. switch (simple_selector.type) {
  290. case CSS::Selector::SimpleSelector::Type::Invalid:
  291. type_description = "Invalid";
  292. break;
  293. case CSS::Selector::SimpleSelector::Type::Universal:
  294. type_description = "Universal";
  295. break;
  296. case CSS::Selector::SimpleSelector::Type::Id:
  297. type_description = "Id";
  298. break;
  299. case CSS::Selector::SimpleSelector::Type::Class:
  300. type_description = "Class";
  301. break;
  302. case CSS::Selector::SimpleSelector::Type::TagName:
  303. type_description = "TagName";
  304. break;
  305. }
  306. const char* attribute_match_type_description = "";
  307. switch (simple_selector.attribute_match_type) {
  308. case CSS::Selector::SimpleSelector::AttributeMatchType::None:
  309. break;
  310. case CSS::Selector::SimpleSelector::AttributeMatchType::HasAttribute:
  311. attribute_match_type_description = "HasAttribute";
  312. break;
  313. case CSS::Selector::SimpleSelector::AttributeMatchType::ExactValueMatch:
  314. attribute_match_type_description = "ExactValueMatch";
  315. break;
  316. case CSS::Selector::SimpleSelector::AttributeMatchType::Contains:
  317. attribute_match_type_description = "Contains";
  318. break;
  319. }
  320. const char* pseudo_class_description = "";
  321. switch (simple_selector.pseudo_class) {
  322. case CSS::Selector::SimpleSelector::PseudoClass::Link:
  323. pseudo_class_description = "Link";
  324. break;
  325. case CSS::Selector::SimpleSelector::PseudoClass::Visited:
  326. pseudo_class_description = "Visited";
  327. break;
  328. case CSS::Selector::SimpleSelector::PseudoClass::None:
  329. pseudo_class_description = "None";
  330. break;
  331. case CSS::Selector::SimpleSelector::PseudoClass::Root:
  332. pseudo_class_description = "Root";
  333. break;
  334. case CSS::Selector::SimpleSelector::PseudoClass::FirstOfType:
  335. pseudo_class_description = "FirstOfType";
  336. break;
  337. case CSS::Selector::SimpleSelector::PseudoClass::LastOfType:
  338. pseudo_class_description = "LastOfType";
  339. break;
  340. case CSS::Selector::SimpleSelector::PseudoClass::Focus:
  341. pseudo_class_description = "Focus";
  342. break;
  343. case CSS::Selector::SimpleSelector::PseudoClass::Empty:
  344. pseudo_class_description = "Empty";
  345. break;
  346. case CSS::Selector::SimpleSelector::PseudoClass::Hover:
  347. pseudo_class_description = "Hover";
  348. break;
  349. case CSS::Selector::SimpleSelector::PseudoClass::LastChild:
  350. pseudo_class_description = "LastChild";
  351. break;
  352. case CSS::Selector::SimpleSelector::PseudoClass::FirstChild:
  353. pseudo_class_description = "FirstChild";
  354. break;
  355. case CSS::Selector::SimpleSelector::PseudoClass::OnlyChild:
  356. pseudo_class_description = "OnlyChild";
  357. break;
  358. }
  359. builder.appendff("{}:{}", type_description, simple_selector.value);
  360. if (simple_selector.pseudo_class != CSS::Selector::SimpleSelector::PseudoClass::None)
  361. builder.appendff(" pseudo_class={}", pseudo_class_description);
  362. if (simple_selector.attribute_match_type != CSS::Selector::SimpleSelector::AttributeMatchType::None) {
  363. builder.appendff(" [{}, name='{}', value='{}']", attribute_match_type_description, simple_selector.attribute_name, simple_selector.attribute_value);
  364. }
  365. if (i != complex_selector.compound_selector.size() - 1)
  366. builder.append(", ");
  367. }
  368. builder.append("\n");
  369. }
  370. }
  371. void dump_rule(const CSS::CSSRule& rule)
  372. {
  373. StringBuilder builder;
  374. dump_rule(builder, rule);
  375. dbgln("{}", builder.string_view());
  376. }
  377. void dump_rule(StringBuilder& builder, const CSS::CSSRule& rule)
  378. {
  379. builder.appendff("{}:\n", rule.class_name());
  380. switch (rule.type()) {
  381. case CSS::CSSRule::Type::Style:
  382. dump_style_rule(builder, downcast<const CSS::CSSStyleRule>(rule));
  383. break;
  384. case CSS::CSSRule::Type::Import:
  385. dump_import_rule(builder, downcast<const CSS::CSSImportRule>(rule));
  386. break;
  387. default:
  388. VERIFY_NOT_REACHED();
  389. }
  390. }
  391. void dump_import_rule(StringBuilder& builder, const CSS::CSSImportRule& rule)
  392. {
  393. builder.appendff(" Document URL: {}\n", rule.url());
  394. }
  395. void dump_style_rule(StringBuilder& builder, const CSS::CSSStyleRule& rule)
  396. {
  397. for (auto& selector : rule.selectors()) {
  398. dump_selector(builder, selector);
  399. }
  400. builder.append(" Declarations:\n");
  401. for (auto& property : rule.declaration().properties()) {
  402. builder.appendff(" {}: '{}'\n", CSS::string_from_property_id(property.property_id), property.value->to_string());
  403. }
  404. }
  405. void dump_sheet(const CSS::StyleSheet& sheet)
  406. {
  407. StringBuilder builder;
  408. dump_sheet(builder, sheet);
  409. dbgln("{}", builder.string_view());
  410. }
  411. void dump_sheet(StringBuilder& builder, const CSS::StyleSheet& sheet)
  412. {
  413. VERIFY(is<CSS::CSSStyleSheet>(sheet));
  414. builder.appendff("CSSStyleSheet{{{}}}: {} rule(s)\n", &sheet, static_cast<const CSS::CSSStyleSheet&>(sheet).rules().size());
  415. for (auto& rule : static_cast<const CSS::CSSStyleSheet&>(sheet).rules()) {
  416. dump_rule(builder, rule);
  417. }
  418. }
  419. }