summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibWeb
diff options
context:
space:
mode:
authorSam Atkins <atkinssj@serenityos.org>2022-04-22 15:18:47 +0100
committerAndreas Kling <kling@serenityos.org>2022-05-11 20:16:10 +0200
commitdfba0cb2d9a1319e37cb0575ec32fbe179ddb741 (patch)
tree6f790ed1e6ad8a190bf4a2b4b7c1e66d78ede72b /Userland/Libraries/LibWeb
parent1cec8e473f9ca6c13847ebfaed9bc0f4e2e4aecd (diff)
downloadserenity-dfba0cb2d9a1319e37cb0575ec32fbe179ddb741.zip
LibWeb: Implement `@supports` serialization
Diffstat (limited to 'Userland/Libraries/LibWeb')
-rw-r--r--Userland/Libraries/LibWeb/CSS/CSSSupportsRule.cpp3
-rw-r--r--Userland/Libraries/LibWeb/CSS/Supports.cpp40
-rw-r--r--Userland/Libraries/LibWeb/CSS/Supports.h14
3 files changed, 55 insertions, 2 deletions
diff --git a/Userland/Libraries/LibWeb/CSS/CSSSupportsRule.cpp b/Userland/Libraries/LibWeb/CSS/CSSSupportsRule.cpp
index 89e9b48d01..f38759415f 100644
--- a/Userland/Libraries/LibWeb/CSS/CSSSupportsRule.cpp
+++ b/Userland/Libraries/LibWeb/CSS/CSSSupportsRule.cpp
@@ -17,8 +17,7 @@ CSSSupportsRule::CSSSupportsRule(NonnullRefPtr<Supports>&& supports, NonnullRefP
String CSSSupportsRule::condition_text() const
{
- // FIXME: Serializing supports rules!
- return "<supports-condition>";
+ return m_supports->to_string();
}
void CSSSupportsRule::set_condition_text(String text)
diff --git a/Userland/Libraries/LibWeb/CSS/Supports.cpp b/Userland/Libraries/LibWeb/CSS/Supports.cpp
index 0cb947ec19..eb1a7ea6e6 100644
--- a/Userland/Libraries/LibWeb/CSS/Supports.cpp
+++ b/Userland/Libraries/LibWeb/CSS/Supports.cpp
@@ -73,4 +73,44 @@ bool Supports::Feature::evaluate() const
});
}
+String Supports::Declaration::to_string() const
+{
+ return String::formatted("({})", declaration);
+}
+
+String Supports::Selector::to_string() const
+{
+ return String::formatted("selector({})", selector);
+}
+
+String Supports::Feature::to_string() const
+{
+ return value.visit([](auto& it) { return it.to_string(); });
+}
+
+String Supports::InParens::to_string() const
+{
+ return value.visit(
+ [](NonnullOwnPtr<Condition> const& condition) -> String { return String::formatted("({})", condition->to_string()); },
+ [](auto& it) -> String { return it.to_string(); });
+}
+
+String Supports::Condition::to_string() const
+{
+ switch (type) {
+ case Type::Not:
+ return String::formatted("not {}", children.first().to_string());
+ case Type::And:
+ return String::join(" and ", children);
+ case Type::Or:
+ return String::join(" or ", children);
+ }
+ VERIFY_NOT_REACHED();
+}
+
+String Supports::to_string() const
+{
+ return m_condition->to_string();
+}
+
}
diff --git a/Userland/Libraries/LibWeb/CSS/Supports.h b/Userland/Libraries/LibWeb/CSS/Supports.h
index 51a7a72d96..f963290322 100644
--- a/Userland/Libraries/LibWeb/CSS/Supports.h
+++ b/Userland/Libraries/LibWeb/CSS/Supports.h
@@ -24,16 +24,19 @@ public:
struct Declaration {
String declaration;
bool evaluate() const;
+ String to_string() const;
};
struct Selector {
String selector;
bool evaluate() const;
+ String to_string() const;
};
struct Feature {
Variant<Declaration, Selector> value;
bool evaluate() const;
+ String to_string() const;
};
struct Condition;
@@ -41,6 +44,7 @@ public:
Variant<NonnullOwnPtr<Condition>, Feature, GeneralEnclosed> value;
bool evaluate() const;
+ String to_string() const;
};
struct Condition {
@@ -53,6 +57,7 @@ public:
Vector<InParens> children;
bool evaluate() const;
+ String to_string() const;
};
static NonnullRefPtr<Supports> create(NonnullOwnPtr<Condition>&& condition)
@@ -61,6 +66,7 @@ public:
}
bool matches() const { return m_matches; }
+ String to_string() const;
private:
Supports(NonnullOwnPtr<Condition>&&);
@@ -70,3 +76,11 @@ private:
};
}
+
+template<>
+struct AK::Formatter<Web::CSS::Supports::InParens> : AK::Formatter<StringView> {
+ ErrorOr<void> format(FormatBuilder& builder, Web::CSS::Supports::InParens const& in_parens)
+ {
+ return Formatter<StringView>::format(builder, in_parens.to_string());
+ }
+};