summaryrefslogtreecommitdiff
path: root/Userland
diff options
context:
space:
mode:
authorSam Atkins <atkinssj@serenityos.org>2021-10-08 17:02:47 +0100
committerAndreas Kling <kling@serenityos.org>2021-10-08 23:02:57 +0200
commit439d978ea52318bad67ff9097fa4551f1f14ece5 (patch)
tree3689c79a05562bbd6cb515fb1ea4a14936de3e1b /Userland
parentdf08b25b3f49a3c9243ae7ec8d54192bcf4cf646 (diff)
downloadserenity-439d978ea52318bad67ff9097fa4551f1f14ece5.zip
LibWeb: Make style-rule iteration aware of CSSMediaRule
The logic is handled by `CSSGroupingRule` and `CSSConditionRule`, so `CSSMediaRule` only has to report if its condition matches. Right now, that condition is always false because we do not evaluate the media query.
Diffstat (limited to 'Userland')
-rw-r--r--Userland/Libraries/LibWeb/CSS/CSSConditionRule.cpp14
-rw-r--r--Userland/Libraries/LibWeb/CSS/CSSConditionRule.h4
-rw-r--r--Userland/Libraries/LibWeb/CSS/CSSGroupingRule.cpp10
-rw-r--r--Userland/Libraries/LibWeb/CSS/CSSGroupingRule.h4
-rw-r--r--Userland/Libraries/LibWeb/CSS/CSSMediaRule.h2
-rw-r--r--Userland/Libraries/LibWeb/CSS/CSSRuleList.cpp18
6 files changed, 49 insertions, 3 deletions
diff --git a/Userland/Libraries/LibWeb/CSS/CSSConditionRule.cpp b/Userland/Libraries/LibWeb/CSS/CSSConditionRule.cpp
index eb2a3716cb..35b16b5dd7 100644
--- a/Userland/Libraries/LibWeb/CSS/CSSConditionRule.cpp
+++ b/Userland/Libraries/LibWeb/CSS/CSSConditionRule.cpp
@@ -17,4 +17,18 @@ CSSConditionRule::~CSSConditionRule()
{
}
+void CSSConditionRule::for_each_effective_style_rule(Function<void(CSSStyleRule const&)> const& callback) const
+{
+ if (condition_matches())
+ CSSGroupingRule::for_each_effective_style_rule(callback);
+}
+
+bool CSSConditionRule::for_first_not_loaded_import_rule(Function<void(CSSImportRule&)> const& callback)
+{
+ if (condition_matches())
+ return CSSGroupingRule::for_first_not_loaded_import_rule(callback);
+
+ return false;
+}
+
}
diff --git a/Userland/Libraries/LibWeb/CSS/CSSConditionRule.h b/Userland/Libraries/LibWeb/CSS/CSSConditionRule.h
index 7fb5b55caa..adca7a46cb 100644
--- a/Userland/Libraries/LibWeb/CSS/CSSConditionRule.h
+++ b/Userland/Libraries/LibWeb/CSS/CSSConditionRule.h
@@ -21,6 +21,10 @@ public:
virtual String condition_text() const = 0;
virtual void set_condition_text(String) = 0;
+ virtual bool condition_matches() const = 0;
+
+ virtual void for_each_effective_style_rule(Function<void(CSSStyleRule const&)> const& callback) const override;
+ virtual bool for_first_not_loaded_import_rule(Function<void(CSSImportRule&)> const& callback) override;
protected:
explicit CSSConditionRule(NonnullRefPtrVector<CSSRule>&&);
diff --git a/Userland/Libraries/LibWeb/CSS/CSSGroupingRule.cpp b/Userland/Libraries/LibWeb/CSS/CSSGroupingRule.cpp
index 56f30ba847..fb771b6c35 100644
--- a/Userland/Libraries/LibWeb/CSS/CSSGroupingRule.cpp
+++ b/Userland/Libraries/LibWeb/CSS/CSSGroupingRule.cpp
@@ -36,4 +36,14 @@ String CSSGroupingRule::serialized() const
TODO();
}
+void CSSGroupingRule::for_each_effective_style_rule(Function<void(CSSStyleRule const&)> const& callback) const
+{
+ m_rules->for_each_effective_style_rule(callback);
+}
+
+bool CSSGroupingRule::for_first_not_loaded_import_rule(Function<void(CSSImportRule&)> const& callback)
+{
+ return m_rules->for_first_not_loaded_import_rule(callback);
+}
+
}
diff --git a/Userland/Libraries/LibWeb/CSS/CSSGroupingRule.h b/Userland/Libraries/LibWeb/CSS/CSSGroupingRule.h
index d8c2398caf..cee8386bfe 100644
--- a/Userland/Libraries/LibWeb/CSS/CSSGroupingRule.h
+++ b/Userland/Libraries/LibWeb/CSS/CSSGroupingRule.h
@@ -6,6 +6,7 @@
#pragma once
+#include <AK/Function.h>
#include <AK/NonnullRefPtr.h>
#include <LibWeb/CSS/CSSRule.h>
#include <LibWeb/CSS/CSSRuleList.h>
@@ -24,6 +25,9 @@ public:
size_t insert_rule(StringView const& rule, size_t index = 0);
void delete_rule(size_t index);
+ virtual void for_each_effective_style_rule(Function<void(CSSStyleRule const&)> const& callback) const;
+ virtual bool for_first_not_loaded_import_rule(Function<void(CSSImportRule&)> const& callback);
+
virtual String serialized() const;
protected:
diff --git a/Userland/Libraries/LibWeb/CSS/CSSMediaRule.h b/Userland/Libraries/LibWeb/CSS/CSSMediaRule.h
index b856e9a03d..4c02dfc08e 100644
--- a/Userland/Libraries/LibWeb/CSS/CSSMediaRule.h
+++ b/Userland/Libraries/LibWeb/CSS/CSSMediaRule.h
@@ -30,6 +30,8 @@ public:
virtual String condition_text() const override;
virtual void set_condition_text(String) override;
+ // FIXME: We need to evaluate() the query before matches() will work!
+ virtual bool condition_matches() const override { return m_media->matches(); }
NonnullRefPtr<MediaList> const& media() const { return m_media; }
diff --git a/Userland/Libraries/LibWeb/CSS/CSSRuleList.cpp b/Userland/Libraries/LibWeb/CSS/CSSRuleList.cpp
index ebb810806f..92f02249d5 100644
--- a/Userland/Libraries/LibWeb/CSS/CSSRuleList.cpp
+++ b/Userland/Libraries/LibWeb/CSS/CSSRuleList.cpp
@@ -6,6 +6,7 @@
#include <AK/TypeCasts.h>
#include <LibWeb/CSS/CSSImportRule.h>
+#include <LibWeb/CSS/CSSMediaRule.h>
#include <LibWeb/CSS/CSSRuleList.h>
#include <LibWeb/DOM/ExceptionOr.h>
@@ -79,12 +80,21 @@ DOM::ExceptionOr<void> CSSRuleList::remove_a_css_rule(u32 index)
void CSSRuleList::for_each_effective_style_rule(Function<void(CSSStyleRule const&)> const& callback) const
{
for (auto& rule : m_rules) {
- if (rule.type() == CSSRule::Type::Style) {
+ switch (rule.type()) {
+ case CSSRule::Type::Style:
callback(verify_cast<CSSStyleRule>(rule));
- } else if (rule.type() == CSSRule::Type::Import) {
- const auto& import_rule = verify_cast<CSSImportRule>(rule);
+ break;
+ case CSSRule::Type::Import: {
+ auto const& import_rule = verify_cast<CSSImportRule>(rule);
if (import_rule.has_import_result())
import_rule.loaded_style_sheet()->for_each_effective_style_rule(callback);
+ break;
+ }
+ case CSSRule::Type::Media:
+ verify_cast<CSSMediaRule>(rule).for_each_effective_style_rule(callback);
+ break;
+ case CSSRule::Type::__Count:
+ VERIFY_NOT_REACHED();
}
}
}
@@ -102,6 +112,8 @@ bool CSSRuleList::for_first_not_loaded_import_rule(Function<void(CSSImportRule&)
if (import_rule.loaded_style_sheet()->for_first_not_loaded_import_rule(callback)) {
return true;
}
+ } else if (rule.type() == CSSRule::Type::Media) {
+ return verify_cast<CSSMediaRule>(rule).for_first_not_loaded_import_rule(callback);
}
}