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