#pragma once
#include <wtf/RefPtr.h>
#include <wtf/Vector.h>
namespace WebCore {
class CSSRule;
class CSSStyleSheet;
class CSSRuleList {
WTF_MAKE_NONCOPYABLE(CSSRuleList); WTF_MAKE_FAST_ALLOCATED;
public:
virtual ~CSSRuleList();
virtual void ref() = 0;
virtual void deref() = 0;
virtual unsigned length() const = 0;
virtual CSSRule* item(unsigned index) const = 0;
virtual CSSStyleSheet* styleSheet() const = 0;
protected:
CSSRuleList();
};
class StaticCSSRuleList final : public CSSRuleList {
public:
static Ref<StaticCSSRuleList> create() { return adoptRef(*new StaticCSSRuleList); }
void ref() final { ++m_refCount; }
void deref() final;
Vector<RefPtr<CSSRule>>& rules() { return m_rules; }
CSSStyleSheet* styleSheet() const final { return nullptr; }
private:
StaticCSSRuleList();
~StaticCSSRuleList();
unsigned length() const final { return m_rules.size(); }
CSSRule* item(unsigned index) const final { return index < m_rules.size() ? m_rules[index].get() : nullptr; }
Vector<RefPtr<CSSRule>> m_rules;
unsigned m_refCount;
};
template <class Rule>
class LiveCSSRuleList final : public CSSRuleList {
public:
LiveCSSRuleList(Rule& rule)
: m_rule(rule)
{
}
void ref() final { m_rule.ref(); }
void deref() final { m_rule.deref(); }
private:
unsigned length() const final { return m_rule.length(); }
CSSRule* item(unsigned index) const final { return m_rule.item(index); }
CSSStyleSheet* styleSheet() const final { return m_rule.parentStyleSheet(); }
Rule& m_rule;
};
}