StyleScopeResolver.cpp [plain text]
#include "config.h"
#include "StyleScopeResolver.h"
#if ENABLE(STYLE_SCOPED) || ENABLE(SHADOW_DOM)
#include "CSSStyleRule.h"
#include "CSSStyleSheet.h"
#include "ContentDistributor.h"
#include "ContextFeatures.h"
#include "ElementShadow.h"
#include "HTMLNames.h"
#include "HTMLStyleElement.h"
#include "RuleFeature.h"
#include "RuleSet.h"
#include "ShadowRoot.h"
namespace WebCore {
StyleScopeResolver::StyleScopeResolver()
: m_stackParent(0)
, m_stackParentBoundsIndex(0)
{
}
StyleScopeResolver::~StyleScopeResolver()
{
}
const ContainerNode* StyleScopeResolver::scopeFor(const CSSStyleSheet* sheet)
{
ASSERT(sheet);
Document* document = sheet->ownerDocument();
if (!document)
return 0;
Node* ownerNode = sheet->ownerNode();
if (!ownerNode || !ownerNode->isHTMLElement() || !ownerNode->hasTagName(HTMLNames::styleTag))
return 0;
HTMLStyleElement* styleElement = static_cast<HTMLStyleElement*>(ownerNode);
if (!styleElement->scoped())
return styleElement->isInShadowTree() ? styleElement->containingShadowRoot() : 0;
ContainerNode* parent = styleElement->parentNode();
if (!parent)
return 0;
return (parent->isElementNode() || parent->isShadowRoot()) ? parent : 0;
}
inline RuleSet* StyleScopeResolver::ruleSetFor(const ContainerNode* scope) const
{
if (!scope->hasScopedHTMLStyleChild())
return 0;
ScopedRuleSetMap::const_iterator it = m_authorStyles.find(scope);
return it != m_authorStyles.end() ? it->value.get() : 0;
}
RuleSet* StyleScopeResolver::ensureRuleSetFor(const ContainerNode* scope)
{
ScopedRuleSetMap::AddResult addResult = m_authorStyles.add(scope, nullptr);
if (addResult.isNewEntry)
addResult.iterator->value = RuleSet::create();
return addResult.iterator->value.get();
}
void StyleScopeResolver::setupStack(const ContainerNode* parent)
{
ASSERT(!m_authorStyles.isEmpty());
m_stack.shrink(0);
int authorStyleBoundsIndex = 0;
for (const ContainerNode* scope = parent; scope; scope = scope->parentOrShadowHostNode()) {
RuleSet* ruleSet = ruleSetFor(scope);
if (ruleSet)
m_stack.append(StackFrame(scope, authorStyleBoundsIndex, ruleSet));
if (scope->isShadowRoot() && !toShadowRoot(scope)->applyAuthorStyles())
--authorStyleBoundsIndex;
}
m_stack.reverse();
m_stackParent = parent;
m_stackParentBoundsIndex = 0;
}
void StyleScopeResolver::push(const ContainerNode* scope, const ContainerNode* scopeParent)
{
if (m_authorStyles.isEmpty()) {
ASSERT(!m_stackParent);
ASSERT(m_stack.isEmpty());
return;
}
if (!stackIsConsistent(scopeParent)) {
setupStack(scope);
return;
}
if (scope->isShadowRoot() && !toShadowRoot(scope)->applyAuthorStyles())
++m_stackParentBoundsIndex;
RuleSet* ruleSet = ruleSetFor(scope);
if (ruleSet)
m_stack.append(StackFrame(scope, m_stackParentBoundsIndex, ruleSet));
m_stackParent = scope;
}
void StyleScopeResolver::pop(const ContainerNode* scope)
{
if (stackIsConsistent(scope)) {
if (!m_stack.isEmpty() && m_stack.last().m_scope == scope)
m_stack.removeLast();
if (scope->isShadowRoot() && !toShadowRoot(scope)->applyAuthorStyles())
--m_stackParentBoundsIndex;
m_stackParent = scope->parentOrShadowHostNode();
}
}
void StyleScopeResolver::collectFeaturesTo(RuleFeatureSet& features)
{
for (ScopedRuleSetMap::iterator it = m_authorStyles.begin(); it != m_authorStyles.end(); ++it)
features.add(it->value->features());
#if ENABLE(SHADOW_DOM)
for (ScopedRuleSetMap::iterator it = m_atHostRules.begin(); it != m_atHostRules.end(); ++it)
features.add(it->value->features());
#endif
}
inline RuleSet* StyleScopeResolver::ensureAtHostRuleSetFor(const ShadowRoot* shadowRoot)
{
ScopedRuleSetMap::AddResult addResult = m_atHostRules.add(shadowRoot, nullptr);
if (addResult.isNewEntry)
addResult.iterator->value = RuleSet::create();
return addResult.iterator->value.get();
}
inline RuleSet* StyleScopeResolver::atHostRuleSetFor(const ShadowRoot* shadowRoot) const
{
ScopedRuleSetMap::const_iterator it = m_atHostRules.find(shadowRoot);
return it != m_atHostRules.end() ? it->value.get() : 0;
}
#if ENABLE(SHADOW_DOM)
void StyleScopeResolver::addHostRule(StyleRuleHost* hostRule, bool hasDocumentSecurityOrigin, const ContainerNode* scope)
{
if (!scope || !scope->isInShadowTree())
return;
ShadowRoot* shadowRoot = scope->containingShadowRoot();
if (!shadowRoot || !shadowRoot->host())
return;
RuleSet* rule = ensureAtHostRuleSetFor(shadowRoot);
const Vector<RefPtr<StyleRuleBase> >& childRules = hostRule->childRules();
AddRuleFlags addRuleFlags = hasDocumentSecurityOrigin ? RuleHasDocumentSecurityOrigin : RuleHasNoSpecialState;
addRuleFlags = static_cast<AddRuleFlags>(addRuleFlags | RuleCanUseFastCheckSelector);
for (unsigned i = 0; i < childRules.size(); ++i) {
StyleRuleBase* hostStylingRule = childRules[i].get();
if (hostStylingRule->isStyleRule())
rule->addStyleRule(static_cast<StyleRule*>(hostStylingRule), addRuleFlags);
}
}
#endif
bool StyleScopeResolver::styleSharingCandidateMatchesHostRules(const Element* element)
{
if (m_atHostRules.isEmpty())
return false;
ElementShadow* shadow = element->shadow();
if (!shadow)
return false;
if (ShadowRoot* shadowRoot = shadow->shadowRoot()) {
if (atHostRuleSetFor(shadowRoot))
return true;
}
return false;
}
void StyleScopeResolver::matchHostRules(const Element* element, Vector<RuleSet*>& matchedRules)
{
if (m_atHostRules.isEmpty())
return;
ElementShadow* shadow = element->shadow();
if (!shadow)
return;
if (ShadowRoot* shadowRoot = shadow->shadowRoot()) {
if (RuleSet* ruleSet = atHostRuleSetFor(shadowRoot))
matchedRules.append(ruleSet);
}
}
}
#endif // ENABLE(STYLE_SCOPED)