1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-06-01 08:28:11 +00:00

LibWeb: Split CSS::StyleSheet into StyleSheet and CSSStyleSheet

This is a little convoluted but matches the CSSOM specification.
This commit is contained in:
Andreas Kling 2021-03-07 16:14:04 +01:00
parent 0af4762662
commit fefb05f6f3
17 changed files with 162 additions and 82 deletions

View file

@ -27,62 +27,16 @@
#pragma once
#include <AK/NonnullRefPtrVector.h>
#include <AK/TypeCasts.h>
#include <LibWeb/CSS/CSSImportRule.h>
#include <LibWeb/CSS/CSSRule.h>
#include <LibWeb/Loader/Resource.h>
#include <AK/RefCounted.h>
namespace Web::CSS {
class StyleSheet : public RefCounted<StyleSheet> {
public:
static NonnullRefPtr<StyleSheet> create(NonnullRefPtrVector<CSSRule>&& rules)
{
return adopt(*new StyleSheet(move(rules)));
}
virtual ~StyleSheet() = default;
~StyleSheet();
const NonnullRefPtrVector<CSSRule>& rules() const { return m_rules; }
NonnullRefPtrVector<CSSRule>& rules() { return m_rules; }
template<typename Callback>
void for_each_effective_style_rule(Callback callback) const
{
for (auto& rule : m_rules)
if (rule.type() == CSSRule::Type::Style) {
callback(downcast<CSSStyleRule>(rule));
} else if (rule.type() == CSSRule::Type::Import) {
const CSSImportRule& import_rule = downcast<CSSImportRule>(rule);
if (import_rule.has_import_result())
import_rule.loaded_style_sheet()->for_each_effective_style_rule(callback);
}
}
template<typename Callback>
bool for_first_not_loaded_import_rule(Callback callback)
{
for (auto& rule : m_rules)
if (rule.type() == CSSRule::Type::Import) {
CSSImportRule& import_rule = downcast<CSSImportRule>(rule);
if (!import_rule.has_import_result()) {
callback(import_rule);
return true;
}
if (import_rule.loaded_style_sheet()->for_first_not_loaded_import_rule(callback)) {
return true;
}
}
return false;
}
private:
explicit StyleSheet(NonnullRefPtrVector<CSSRule>&&);
NonnullRefPtrVector<CSSRule> m_rules;
protected:
StyleSheet() = default;
};
}