#include "config.h"
#include "RuleData.h"
#include "CSSFontSelector.h"
#include "CSSKeyframesRule.h"
#include "CSSSelector.h"
#include "CSSSelectorList.h"
#include "HTMLNames.h"
#include "MediaQueryEvaluator.h"
#include "SecurityOrigin.h"
#include "SelectorChecker.h"
#include "SelectorFilter.h"
#include "StyleResolver.h"
#include "StyleRule.h"
#include "StyleRuleImport.h"
#include "StyleSheetContents.h"
#if ENABLE(VIDEO)
#include "TextTrackCue.h"
#endif
namespace WebCore {
namespace Style {
using namespace HTMLNames;
struct SameSizeAsRuleData {
void* a;
unsigned b;
unsigned c;
unsigned d[4];
};
COMPILE_ASSERT(sizeof(RuleData) == sizeof(SameSizeAsRuleData), RuleData_should_stay_small);
static inline MatchBasedOnRuleHash computeMatchBasedOnRuleHash(const CSSSelector& selector)
{
if (selector.tagHistory())
return MatchBasedOnRuleHash::None;
if (selector.match() == CSSSelector::Tag) {
const QualifiedName& tagQualifiedName = selector.tagQName();
const AtomString& selectorNamespace = tagQualifiedName.namespaceURI();
if (selectorNamespace == starAtom() || selectorNamespace == xhtmlNamespaceURI) {
if (tagQualifiedName == anyQName())
return MatchBasedOnRuleHash::Universal;
return MatchBasedOnRuleHash::ClassC;
}
return MatchBasedOnRuleHash::None;
}
if (SelectorChecker::isCommonPseudoClassSelector(&selector))
return MatchBasedOnRuleHash::ClassB;
if (selector.match() == CSSSelector::Id)
return MatchBasedOnRuleHash::ClassA;
if (selector.match() == CSSSelector::Class)
return MatchBasedOnRuleHash::ClassB;
return MatchBasedOnRuleHash::None;
}
static bool selectorCanMatchPseudoElement(const CSSSelector& rootSelector)
{
const CSSSelector* selector = &rootSelector;
do {
if (selector->matchesPseudoElement())
return true;
if (const CSSSelectorList* selectorList = selector->selectorList()) {
for (const CSSSelector* subSelector = selectorList->first(); subSelector; subSelector = CSSSelectorList::next(subSelector)) {
if (selectorCanMatchPseudoElement(*subSelector))
return true;
}
}
selector = selector->tagHistory();
} while (selector);
return false;
}
static inline bool isCommonAttributeSelectorAttribute(const QualifiedName& attribute)
{
return attribute == typeAttr || attribute == readonlyAttr;
}
static bool computeContainsUncommonAttributeSelector(const CSSSelector& rootSelector, bool matchesRightmostElement = true)
{
const CSSSelector* selector = &rootSelector;
do {
if (selector->isAttributeSelector()) {
if (!isCommonAttributeSelectorAttribute(selector->attribute()) || !matchesRightmostElement)
return true;
}
if (const CSSSelectorList* selectorList = selector->selectorList()) {
for (const CSSSelector* subSelector = selectorList->first(); subSelector; subSelector = CSSSelectorList::next(subSelector)) {
if (computeContainsUncommonAttributeSelector(*subSelector, matchesRightmostElement))
return true;
}
}
if (selector->relation() != CSSSelector::Subselector)
matchesRightmostElement = false;
selector = selector->tagHistory();
} while (selector);
return false;
}
static inline PropertyAllowlistType determinePropertyAllowlistType(const CSSSelector* selector)
{
for (const CSSSelector* component = selector; component; component = component->tagHistory()) {
#if ENABLE(VIDEO)
if (component->match() == CSSSelector::PseudoElement && (component->pseudoElementType() == CSSSelector::PseudoElementCue || component->value() == TextTrackCue::cueShadowPseudoId()))
return PropertyAllowlistCue;
#endif
if (component->match() == CSSSelector::PseudoElement && component->pseudoElementType() == CSSSelector::PseudoElementMarker)
return PropertyAllowlistMarker;
if (const auto* selectorList = selector->selectorList()) {
for (const auto* subSelector = selectorList->first(); subSelector; subSelector = CSSSelectorList::next(subSelector)) {
auto allowlistType = determinePropertyAllowlistType(subSelector);
if (allowlistType != PropertyAllowlistNone)
return allowlistType;
}
}
}
return PropertyAllowlistNone;
}
RuleData::RuleData(const StyleRule& styleRule, unsigned selectorIndex, unsigned selectorListIndex, unsigned position)
: m_styleRule(&styleRule)
, m_selectorIndex(selectorIndex)
, m_selectorListIndex(selectorListIndex)
, m_position(position)
, m_matchBasedOnRuleHash(static_cast<unsigned>(computeMatchBasedOnRuleHash(*selector())))
, m_canMatchPseudoElement(selectorCanMatchPseudoElement(*selector()))
, m_containsUncommonAttributeSelector(computeContainsUncommonAttributeSelector(*selector()))
, m_linkMatchType(SelectorChecker::determineLinkMatchType(selector()))
, m_propertyAllowlistType(determinePropertyAllowlistType(selector()))
, m_isEnabled(true)
, m_descendantSelectorIdentifierHashes(SelectorFilter::collectHashes(*selector()))
{
ASSERT(m_position == position);
ASSERT(m_selectorIndex == selectorIndex);
}
} }