StyleInvalidationAnalysis.cpp [plain text]
#include "config.h"
#include "StyleInvalidationAnalysis.h"
#include "CSSSelectorList.h"
#include "Document.h"
#include "ElementIterator.h"
#include "ElementRuleCollector.h"
#include "SelectorFilter.h"
#include "StyleRuleImport.h"
#include "StyleSheetContents.h"
namespace WebCore {
static bool shouldDirtyAllStyle(const Vector<RefPtr<StyleRuleBase>>& rules)
{
for (auto& rule : rules) {
if (rule->isMediaRule()) {
if (shouldDirtyAllStyle(static_cast<StyleRuleMedia&>(*rule).childRules()))
return true;
continue;
}
if (!rule->isStyleRule())
return true;
}
return false;
}
static bool shouldDirtyAllStyle(const StyleSheetContents& sheet)
{
for (auto& import : sheet.importRules()) {
if (!import->styleSheet())
continue;
if (shouldDirtyAllStyle(*import->styleSheet()))
return true;
}
if (shouldDirtyAllStyle(sheet.childRules()))
return true;
return false;
}
static bool shouldDirtyAllStyle(const Vector<StyleSheetContents*>& sheets)
{
for (auto& sheet : sheets) {
if (shouldDirtyAllStyle(*sheet))
return true;
}
return false;
}
StyleInvalidationAnalysis::StyleInvalidationAnalysis(const Vector<StyleSheetContents*>& sheets, const MediaQueryEvaluator& mediaQueryEvaluator)
: m_dirtiesAllStyle(shouldDirtyAllStyle(sheets))
{
if (m_dirtiesAllStyle)
return;
m_ruleSets.resetAuthorStyle();
for (auto& sheet : sheets)
m_ruleSets.authorStyle()->addRulesFromSheet(sheet, mediaQueryEvaluator);
if (m_ruleSets.authorStyle()->hasShadowPseudoElementRules())
m_dirtiesAllStyle = true;
}
static void invalidateStyleRecursively(Element& element, SelectorFilter& filter, const DocumentRuleSets& ruleSets)
{
if (element.styleChangeType() > InlineStyleChange)
return;
if (element.styleChangeType() == NoStyleChange) {
ElementRuleCollector ruleCollector(element, nullptr, ruleSets, filter);
ruleCollector.setMode(SelectorChecker::Mode::StyleInvalidation);
ruleCollector.matchAuthorRules(false);
if (ruleCollector.hasMatchedRules())
element.setNeedsStyleRecalc(InlineStyleChange);
}
auto children = childrenOfType<Element>(element);
if (!children.first())
return;
filter.pushParent(&element);
for (auto& child : children)
invalidateStyleRecursively(child, filter, ruleSets);
filter.popParent();
}
void StyleInvalidationAnalysis::invalidateStyle(Document& document)
{
ASSERT(!m_dirtiesAllStyle);
if (!m_ruleSets.authorStyle())
return;
Element* documentElement = document.documentElement();
if (!documentElement)
return;
SelectorFilter filter;
filter.setupParentStack(documentElement);
invalidateStyleRecursively(*documentElement, filter, m_ruleSets);
}
}