StyleScopeResolver.h [plain text]
#ifndef StyleScopeResolver_h
#define StyleScopeResolver_h
#include <wtf/Assertions.h>
#include <wtf/Forward.h>
#include <wtf/HashMap.h>
#include <wtf/OwnPtr.h>
#include <wtf/Vector.h>
namespace WebCore {
class ContainerNode;
class CSSStyleSheet;
class Element;
class RuleSet;
class ShadowRoot;
class StyleRuleHost;
struct RuleFeatureSet;
#if ENABLE(STYLE_SCOPED) || ENABLE(SHADOW_DOM)
class StyleScopeResolver {
public:
typedef HashMap<const ContainerNode*, OwnPtr<RuleSet> > ScopedRuleSetMap;
struct StackFrame {
StackFrame() : m_scope(0), m_authorStyleBoundsIndex(0), m_ruleSet(0) { }
StackFrame(const ContainerNode* scope, int authorStyleBoundsIndex, RuleSet* ruleSet)
: m_scope(scope), m_authorStyleBoundsIndex(authorStyleBoundsIndex), m_ruleSet(ruleSet)
{ }
const ContainerNode* m_scope;
int m_authorStyleBoundsIndex;
RuleSet* m_ruleSet;
};
StyleScopeResolver();
~StyleScopeResolver();
static const ContainerNode* scopeFor(const CSSStyleSheet*);
void push(const ContainerNode* scope, const ContainerNode* scopeParent);
void pop(const ContainerNode* scope);
bool hasScopedStyles() const { return !m_authorStyles.isEmpty(); }
RuleSet* ensureRuleSetFor(const ContainerNode* scope);
bool ensureStackConsistency(ContainerNode*);
unsigned stackSize() const { return m_stack.size(); }
const StackFrame& stackFrameAt(unsigned index) const { return m_stack.at(index); }
bool matchesStyleBounds(const StackFrame& frame) const { return frame.m_authorStyleBoundsIndex == m_stackParentBoundsIndex; }
void collectFeaturesTo(RuleFeatureSet&);
void addHostRule(StyleRuleHost*, bool hasDocumentSecurityOrigin, const ContainerNode* scope);
bool styleSharingCandidateMatchesHostRules(const Element*);
void matchHostRules(const Element*, Vector<RuleSet*>& matchedRules);
private:
RuleSet* ruleSetFor(const ContainerNode* scope) const;
void setupStack(const ContainerNode*);
bool stackIsConsistent(const ContainerNode* parent) const { return parent && parent == m_stackParent; }
RuleSet* ensureAtHostRuleSetFor(const ShadowRoot*);
RuleSet* atHostRuleSetFor(const ShadowRoot*) const;
ScopedRuleSetMap m_authorStyles;
Vector<StackFrame> m_stack;
const ContainerNode* m_stackParent;
int m_stackParentBoundsIndex;
ScopedRuleSetMap m_atHostRules;
};
inline bool StyleScopeResolver::ensureStackConsistency(ContainerNode* parent)
{
if (!stackIsConsistent(parent))
setupStack(parent);
return !m_stack.isEmpty();
}
#else
class StyleScopeResolver {
public:
static const ContainerNode* scopeFor(const CSSStyleSheet*) { return 0; }
void push(const ContainerNode*, const ContainerNode*) { }
void pop(const ContainerNode*) { }
void collectFeaturesTo(RuleFeatureSet&) { }
RuleSet* ensureRuleSetFor(const ContainerNode*) { return 0; }
};
#endif // ENABLE(STYLE_SCOPED)
}
#endif // StyleScopeResolver_h