LibHTML: Flesh out the code to dump a StyleSheet object graph.

This commit is contained in:
Andreas Kling 2019-06-25 06:31:47 +02:00
parent 048705e1c2
commit 6469d7f043
Notes: sideshowbarker 2024-07-19 13:29:22 +09:00
3 changed files with 42 additions and 1 deletions

View file

@ -13,6 +13,16 @@ public:
~StyleRule();
const Vector<Selector>& selectors() const { return m_selectors; }
const Vector<NonnullRefPtr<StyleDeclaration>>& declarations() const { return m_declarations; }
template<typename C>
void for_each_declaration(C callback) const
{
for (auto& declaration : m_declarations)
callback(*declaration);
}
private:
StyleRule(Vector<Selector>&&, Vector<NonnullRefPtr<StyleDeclaration>>&&);

View file

@ -20,6 +20,8 @@ public:
Type type() const { return m_type; }
virtual String to_string() const = 0;
protected:
explicit StyleValue(Type);
@ -36,7 +38,7 @@ public:
{
}
String to_string() const { return m_string; }
String to_string() const override { return m_string; }
private:
String m_string;

View file

@ -70,4 +70,33 @@ void dump_tree(const LayoutNode& layout_node)
void dump_sheet(const StyleSheet& sheet)
{
printf("StyleSheet{%p}: %d rule(s)\n", &sheet, sheet.rules().size());
for (auto& rule : sheet.rules()) {
printf("Rule:\n");
for (auto& selector : rule->selectors()) {
printf(" 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;
}
printf(" %s:%s", type_description, component.value.characters());
}
}
printf(" Declarations:\n");
rule->for_each_declaration([](auto& declaration) {
printf(" '%s': '%s'\n", declaration.property_name().characters(), declaration.value().to_string().characters());
});
}
}