138 lines
4.5 KiB
C++
138 lines
4.5 KiB
C++
#include <LibHTML/CSS/StyleSheet.h>
|
|
#include <LibHTML/DOM/Document.h>
|
|
#include <LibHTML/DOM/Element.h>
|
|
#include <LibHTML/DOM/Text.h>
|
|
#include <LibHTML/Dump.h>
|
|
#include <LibHTML/Layout/LayoutNode.h>
|
|
#include <LibHTML/Layout/LayoutText.h>
|
|
#include <stdio.h>
|
|
|
|
void dump_tree(const Node& node)
|
|
{
|
|
static int indent = 0;
|
|
for (int i = 0; i < indent; ++i)
|
|
dbgprintf(" ");
|
|
if (node.is_document()) {
|
|
dbgprintf("*Document*\n");
|
|
} else if (node.is_element()) {
|
|
dbgprintf("<%s", static_cast<const Element&>(node).tag_name().characters());
|
|
static_cast<const Element&>(node).for_each_attribute([](auto& name, auto& value) {
|
|
dbgprintf(" %s=%s", name.characters(), value.characters());
|
|
});
|
|
dbgprintf(">\n");
|
|
} else if (node.is_text()) {
|
|
dbgprintf("\"%s\"\n", static_cast<const Text&>(node).data().characters());
|
|
}
|
|
++indent;
|
|
if (node.is_parent_node()) {
|
|
static_cast<const ParentNode&>(node).for_each_child([](auto& child) {
|
|
dump_tree(child);
|
|
});
|
|
}
|
|
--indent;
|
|
}
|
|
|
|
void dump_tree(const LayoutNode& layout_node)
|
|
{
|
|
static int indent = 0;
|
|
for (int i = 0; i < indent; ++i)
|
|
dbgprintf(" ");
|
|
|
|
String tag_name;
|
|
if (layout_node.is_anonymous())
|
|
tag_name = "(anonymous)";
|
|
else if (layout_node.node()->is_text())
|
|
tag_name = "#text";
|
|
else if (layout_node.node()->is_document())
|
|
tag_name = "#document";
|
|
else if (layout_node.node()->is_element())
|
|
tag_name = static_cast<const Element&>(*layout_node.node()).tag_name();
|
|
else
|
|
tag_name = "???";
|
|
|
|
dbgprintf("%s {%s} at (%d,%d) size %dx%d",
|
|
layout_node.class_name(),
|
|
tag_name.characters(),
|
|
layout_node.rect().x(),
|
|
layout_node.rect().y(),
|
|
layout_node.rect().width(),
|
|
layout_node.rect().height());
|
|
|
|
// Dump the horizontal box properties
|
|
dbgprintf(" [%d+%d+%d %d %d+%d+%d]",
|
|
layout_node.style().margin().left.to_px(),
|
|
layout_node.style().border().left.to_px(),
|
|
layout_node.style().padding().left.to_px(),
|
|
layout_node.rect().width(),
|
|
layout_node.style().padding().right.to_px(),
|
|
layout_node.style().border().right.to_px(),
|
|
layout_node.style().margin().right.to_px());
|
|
|
|
// And the vertical box properties
|
|
dbgprintf(" [%d+%d+%d %d %d+%d+%d]",
|
|
layout_node.style().margin().top.to_px(),
|
|
layout_node.style().border().top.to_px(),
|
|
layout_node.style().padding().top.to_px(),
|
|
layout_node.rect().height(),
|
|
layout_node.style().padding().bottom.to_px(),
|
|
layout_node.style().border().bottom.to_px(),
|
|
layout_node.style().margin().bottom.to_px());
|
|
|
|
if (layout_node.is_text()) {
|
|
const LayoutText& layout_text = static_cast<const LayoutText&>(layout_node);
|
|
dbgprintf(" \"%s\", %d runs", layout_text.text().characters(), layout_text.runs().size());
|
|
}
|
|
|
|
dbgprintf("\n");
|
|
|
|
layout_node.style_properties().for_each_property([&](auto& key, auto& value) {
|
|
for (int i = 0; i < indent; ++i)
|
|
dbgprintf(" ");
|
|
dbgprintf(" (%s: %s)\n", key.characters(), value.to_string().characters());
|
|
});
|
|
|
|
++indent;
|
|
layout_node.for_each_child([](auto& child) {
|
|
dump_tree(child);
|
|
});
|
|
--indent;
|
|
}
|
|
|
|
void dump_rule(const StyleRule& rule)
|
|
{
|
|
dbgprintf("Rule:\n");
|
|
for (auto& selector : rule.selectors()) {
|
|
dbgprintf(" Selector:\n");
|
|
for (auto& component : selector.components()) {
|
|
const char* type_description = "Unknown";
|
|
switch (component.type) {
|
|
case Selector::Component::Type::Invalid:
|
|
type_description = "Invalid";
|
|
break;
|
|
case Selector::Component::Type::Id:
|
|
type_description = "Id";
|
|
break;
|
|
case Selector::Component::Type::Class:
|
|
type_description = "Class";
|
|
break;
|
|
case Selector::Component::Type::TagName:
|
|
type_description = "TagName";
|
|
break;
|
|
}
|
|
dbgprintf(" %s:%s\n", type_description, component.value.characters());
|
|
}
|
|
}
|
|
dbgprintf(" Declarations:\n");
|
|
for (auto& property : rule.declaration().properties()) {
|
|
dbgprintf(" '%s': '%s'\n", property.name.characters(), property.value->to_string().characters());
|
|
}
|
|
}
|
|
|
|
void dump_sheet(const StyleSheet& sheet)
|
|
{
|
|
dbgprintf("StyleSheet{%p}: %d rule(s)\n", &sheet, sheet.rules().size());
|
|
|
|
for (auto& rule : sheet.rules()) {
|
|
dump_rule(rule);
|
|
}
|
|
}
|