This source file includes following definitions.
- m_usesRemUnits
- addStyleSheetCandidateNode
- removeStyleSheetCandidateNode
- compareStyleSheets
- activeLoadingStyleSheetLoaded
- findFontFaceRulesFromStyleSheetContents
- analyzeStyleSheetChange
- clearMediaQueryRuleSetStyleSheets
- resetAllRuleSetsInTreeScope
- styleSheetsUseRemUnits
- updateUsesRemUnits
#include "config.h"
#include "core/dom/TreeScopeStyleSheetCollection.h"
#include "core/css/CSSStyleSheet.h"
#include "core/css/StyleRuleImport.h"
#include "core/css/StyleSheetContents.h"
#include "core/css/invalidation/StyleSheetInvalidationAnalysis.h"
#include "core/css/resolver/StyleResolver.h"
#include "core/dom/Element.h"
#include "core/dom/StyleEngine.h"
#include "core/html/HTMLStyleElement.h"
namespace WebCore {
TreeScopeStyleSheetCollection::TreeScopeStyleSheetCollection(TreeScope& treeScope)
: m_treeScope(treeScope)
, m_hadActiveLoadingStylesheet(false)
, m_usesRemUnits(false)
{
}
void TreeScopeStyleSheetCollection::addStyleSheetCandidateNode(Node* node, bool createdByParser)
{
if (!node->inDocument())
return;
if (createdByParser && document().body())
m_styleSheetCandidateNodes.parserAdd(node);
else
m_styleSheetCandidateNodes.add(node);
if (!isHTMLStyleElement(*node))
return;
ContainerNode* scopingNode = toHTMLStyleElement(*node).scopingNode();
if (!isTreeScopeRoot(scopingNode))
m_scopingNodesForStyleScoped.add(scopingNode);
}
void TreeScopeStyleSheetCollection::removeStyleSheetCandidateNode(Node* node, ContainerNode* scopingNode)
{
m_styleSheetCandidateNodes.remove(node);
if (!isTreeScopeRoot(scopingNode))
m_scopingNodesForStyleScoped.remove(scopingNode);
}
TreeScopeStyleSheetCollection::StyleResolverUpdateType TreeScopeStyleSheetCollection::compareStyleSheets(const WillBeHeapVector<RefPtrWillBeMember<CSSStyleSheet> >& oldStyleSheets, const WillBeHeapVector<RefPtrWillBeMember<CSSStyleSheet> >& newStylesheets, WillBeHeapVector<RawPtrWillBeMember<StyleSheetContents> >& addedSheets)
{
unsigned newStyleSheetCount = newStylesheets.size();
unsigned oldStyleSheetCount = oldStyleSheets.size();
ASSERT(newStyleSheetCount >= oldStyleSheetCount);
if (!newStyleSheetCount)
return Reconstruct;
unsigned newIndex = 0;
for (unsigned oldIndex = 0; oldIndex < oldStyleSheetCount; ++oldIndex) {
while (oldStyleSheets[oldIndex] != newStylesheets[newIndex]) {
addedSheets.append(newStylesheets[newIndex]->contents());
if (++newIndex == newStyleSheetCount)
return Reconstruct;
}
if (++newIndex == newStyleSheetCount)
return Reconstruct;
}
bool hasInsertions = !addedSheets.isEmpty();
while (newIndex < newStyleSheetCount) {
addedSheets.append(newStylesheets[newIndex]->contents());
++newIndex;
}
return hasInsertions ? Reset : Additive;
}
bool TreeScopeStyleSheetCollection::activeLoadingStyleSheetLoaded(const WillBeHeapVector<RefPtrWillBeMember<CSSStyleSheet> >& newStyleSheets)
{
bool hasActiveLoadingStylesheet = false;
unsigned newStylesheetCount = newStyleSheets.size();
for (unsigned i = 0; i < newStylesheetCount; ++i) {
if (newStyleSheets[i]->isLoading())
hasActiveLoadingStylesheet = true;
}
if (m_hadActiveLoadingStylesheet && !hasActiveLoadingStylesheet) {
m_hadActiveLoadingStylesheet = false;
return true;
}
m_hadActiveLoadingStylesheet = hasActiveLoadingStylesheet;
return false;
}
static bool findFontFaceRulesFromStyleSheetContents(const WillBeHeapVector<RawPtrWillBeMember<StyleSheetContents> >& sheets, WillBeHeapVector<RawPtrWillBeMember<const StyleRuleFontFace> >& fontFaceRules)
{
bool hasFontFaceRule = false;
for (unsigned i = 0; i < sheets.size(); ++i) {
ASSERT(sheets[i]);
if (sheets[i]->hasFontFaceRule()) {
sheets[i]->findFontFaceRules(fontFaceRules);
hasFontFaceRule = true;
}
}
return hasFontFaceRule;
}
void TreeScopeStyleSheetCollection::analyzeStyleSheetChange(StyleResolverUpdateMode updateMode, const StyleSheetCollection& newCollection, StyleSheetChange& change)
{
if (activeLoadingStyleSheetLoaded(newCollection.activeAuthorStyleSheets()))
return;
if (updateMode != AnalyzedStyleUpdate)
return;
WillBeHeapVector<RawPtrWillBeMember<StyleSheetContents> > addedSheets;
if (m_activeAuthorStyleSheets.size() <= newCollection.activeAuthorStyleSheets().size()) {
change.styleResolverUpdateType = compareStyleSheets(m_activeAuthorStyleSheets, newCollection.activeAuthorStyleSheets(), addedSheets);
} else {
StyleResolverUpdateType updateType = compareStyleSheets(newCollection.activeAuthorStyleSheets(), m_activeAuthorStyleSheets, addedSheets);
if (updateType != Additive) {
change.styleResolverUpdateType = updateType;
} else {
change.styleResolverUpdateType = Reset;
if (findFontFaceRulesFromStyleSheetContents(addedSheets, change.fontFaceRulesToRemove))
return;
}
}
if (document().hasPendingForcedStyleRecalc())
return;
if (!document().body() || document().hasNodesWithPlaceholderStyle())
return;
StyleSheetInvalidationAnalysis invalidationAnalysis(addedSheets);
if (invalidationAnalysis.dirtiesAllStyle())
return;
invalidationAnalysis.invalidateStyle(document());
change.requiresFullStyleRecalc = false;
return;
}
void TreeScopeStyleSheetCollection::clearMediaQueryRuleSetStyleSheets()
{
for (size_t i = 0; i < m_activeAuthorStyleSheets.size(); ++i) {
StyleSheetContents* contents = m_activeAuthorStyleSheets[i]->contents();
if (contents->hasMediaQueries())
contents->clearRuleSet();
}
}
void TreeScopeStyleSheetCollection::resetAllRuleSetsInTreeScope(StyleResolver* styleResolver)
{
if (DocumentOrderedList* styleScopedScopingNodes = scopingNodesForStyleScoped()) {
for (DocumentOrderedList::iterator it = styleScopedScopingNodes->begin(); it != styleScopedScopingNodes->end(); ++it)
styleResolver->resetAuthorStyle(toContainerNode(*it));
}
if (ListHashSet<Node*, 4>* removedNodes = scopingNodesRemoved()) {
for (ListHashSet<Node*, 4>::iterator it = removedNodes->begin(); it != removedNodes->end(); ++it)
styleResolver->resetAuthorStyle(toContainerNode(*it));
}
styleResolver->resetAuthorStyle(toContainerNode(&m_treeScope.rootNode()));
}
static bool styleSheetsUseRemUnits(const WillBeHeapVector<RefPtrWillBeMember<CSSStyleSheet> >& sheets)
{
for (unsigned i = 0; i < sheets.size(); ++i) {
if (sheets[i]->contents()->usesRemUnits())
return true;
}
return false;
}
void TreeScopeStyleSheetCollection::updateUsesRemUnits()
{
m_usesRemUnits = styleSheetsUseRemUnits(m_activeAuthorStyleSheets);
}
}