Browse Source

LibWeb: Make dumping of `@supports` more useful

The string representation doesn't tell you what the internals look like,
which is what you want when dumping.
Sam Atkins 9 months ago
parent
commit
398e112c8c

+ 2 - 0
Userland/Libraries/LibWeb/CSS/CSSSupportsRule.h

@@ -27,6 +27,8 @@ public:
     String condition_text() const override;
     virtual bool condition_matches() const override { return m_supports->matches(); }
 
+    Supports const& supports() const { return m_supports; }
+
 private:
     CSSSupportsRule(JS::Realm&, NonnullRefPtr<Supports>&&, CSSRuleList&);
 

+ 58 - 0
Userland/Libraries/LibWeb/CSS/Supports.cpp

@@ -10,6 +10,12 @@
 
 namespace Web::CSS {
 
+static void indent(StringBuilder& builder, int levels)
+{
+    for (int i = 0; i < levels; i++)
+        builder.append("  "sv);
+}
+
 Supports::Supports(JS::Realm& realm, NonnullOwnPtr<Condition>&& condition)
     : m_condition(move(condition))
 {
@@ -115,4 +121,56 @@ String Supports::to_string() const
     return m_condition->to_string();
 }
 
+void Supports::Declaration::dump(StringBuilder& builder, int indent_levels) const
+{
+    indent(builder, indent_levels);
+    builder.appendff("Declaration: {}\n", declaration);
+}
+
+void Supports::Selector::dump(StringBuilder& builder, int indent_levels) const
+{
+    indent(builder, indent_levels);
+    builder.appendff("Selector: {}\n", selector);
+}
+
+void Supports::Feature::dump(StringBuilder& builder, int indent_levels) const
+{
+    value.visit([&](auto& it) { it.dump(builder, indent_levels); });
+}
+
+void Supports::InParens::dump(StringBuilder& builder, int indent_levels) const
+{
+    value.visit(
+        [&](NonnullOwnPtr<Condition> const& condition) { condition->dump(builder, indent_levels); },
+        [&](Supports::Feature const& it) { it.dump(builder, indent_levels); },
+        [&](GeneralEnclosed const& it) {
+            indent(builder, indent_levels);
+            builder.appendff("GeneralEnclosed: {}\n", it.to_string());
+        });
+}
+
+void Supports::Condition::dump(StringBuilder& builder, int indent_levels) const
+{
+    indent(builder, indent_levels);
+    StringView type_name = [](Type type) {
+        switch (type) {
+        case Type::And:
+            return "AND"sv;
+        case Type::Or:
+            return "OR"sv;
+        case Type::Not:
+            return "NOT"sv;
+        }
+        VERIFY_NOT_REACHED();
+    }(type);
+    builder.appendff("Condition: {}\n", type_name);
+    for (auto const& child : children)
+        child.dump(builder, indent_levels + 1);
+}
+
+void Supports::dump(StringBuilder& builder, int indent_levels) const
+{
+    m_condition->dump(builder, indent_levels);
+}
+
 }

+ 7 - 0
Userland/Libraries/LibWeb/CSS/Supports.h

@@ -24,18 +24,21 @@ public:
         String declaration;
         [[nodiscard]] bool evaluate(JS::Realm&) const;
         String to_string() const;
+        void dump(StringBuilder&, int indent_levels = 0) const;
     };
 
     struct Selector {
         String selector;
         [[nodiscard]] bool evaluate(JS::Realm&) const;
         String to_string() const;
+        void dump(StringBuilder&, int indent_levels = 0) const;
     };
 
     struct Feature {
         Variant<Declaration, Selector> value;
         [[nodiscard]] bool evaluate(JS::Realm&) const;
         String to_string() const;
+        void dump(StringBuilder&, int indent_levels = 0) const;
     };
 
     struct Condition;
@@ -44,6 +47,7 @@ public:
 
         [[nodiscard]] bool evaluate(JS::Realm&) const;
         String to_string() const;
+        void dump(StringBuilder&, int indent_levels = 0) const;
     };
 
     struct Condition {
@@ -57,6 +61,7 @@ public:
 
         [[nodiscard]] bool evaluate(JS::Realm&) const;
         String to_string() const;
+        void dump(StringBuilder&, int indent_levels = 0) const;
     };
 
     static NonnullRefPtr<Supports> create(JS::Realm& realm, NonnullOwnPtr<Condition>&& condition)
@@ -67,6 +72,8 @@ public:
     bool matches() const { return m_matches; }
     String to_string() const;
 
+    void dump(StringBuilder&, int indent_levels = 0) const;
+
 private:
     Supports(JS::Realm&, NonnullOwnPtr<Condition>&&);
 

+ 2 - 1
Userland/Libraries/LibWeb/Dump.cpp

@@ -778,7 +778,8 @@ void dump_media_rule(StringBuilder& builder, CSS::CSSMediaRule const& media, int
 void dump_supports_rule(StringBuilder& builder, CSS::CSSSupportsRule const& supports, int indent_levels)
 {
     indent(builder, indent_levels);
-    builder.appendff("  Supports: {}\n", supports.condition_text());
+    builder.append("  Supports:\n"sv);
+    supports.supports().dump(builder, indent_levels + 2);
     indent(builder, indent_levels);
     builder.appendff("  Rules ({}):\n", supports.css_rules().length());