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