Explorar o código

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

Andreas Kling %!s(int64=6) %!d(string=hai) anos
pai
achega
6469d7f043
Modificáronse 3 ficheiros con 42 adicións e 1 borrados
  1. 10 0
      LibHTML/CSS/StyleRule.h
  2. 3 1
      LibHTML/CSS/StyleValue.h
  3. 29 0
      LibHTML/Dump.cpp

+ 10 - 0
LibHTML/CSS/StyleRule.h

@@ -13,6 +13,16 @@ public:
 
 
     ~StyleRule();
     ~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:
 private:
     StyleRule(Vector<Selector>&&, Vector<NonnullRefPtr<StyleDeclaration>>&&);
     StyleRule(Vector<Selector>&&, Vector<NonnullRefPtr<StyleDeclaration>>&&);
 
 

+ 3 - 1
LibHTML/CSS/StyleValue.h

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

+ 29 - 0
LibHTML/Dump.cpp

@@ -70,4 +70,33 @@ void dump_tree(const LayoutNode& layout_node)
 void dump_sheet(const StyleSheet& sheet)
 void dump_sheet(const StyleSheet& sheet)
 {
 {
     printf("StyleSheet{%p}: %d rule(s)\n", &sheet, sheet.rules().size());
     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());
+        });
+    }
 }
 }