| Index: third_party/WebKit/Source/core/css/ActiveStyleSheets.cpp
|
| diff --git a/third_party/WebKit/Source/core/css/ActiveStyleSheets.cpp b/third_party/WebKit/Source/core/css/ActiveStyleSheets.cpp
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..ce5951c051d1cde8b2de9ba5f9d107119973f121
|
| --- /dev/null
|
| +++ b/third_party/WebKit/Source/core/css/ActiveStyleSheets.cpp
|
| @@ -0,0 +1,185 @@
|
| +// Copyright 2016 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "core/css/ActiveStyleSheets.h"
|
| +
|
| +#include "core/css/CSSStyleSheet.h"
|
| +#include "core/css/RuleSet.h"
|
| +#include "core/css/resolver/ScopedStyleResolver.h"
|
| +#include "core/dom/ContainerNode.h"
|
| +#include "core/dom/StyleChangeReason.h"
|
| +#include "core/dom/StyleEngine.h"
|
| +#include "core/dom/TreeScope.h"
|
| +#include "core/dom/shadow/ShadowRoot.h"
|
| +#include "core/html/imports/HTMLImportsController.h"
|
| +
|
| +namespace blink {
|
| +
|
| +ActiveSheetsChange compareActiveStyleSheets(
|
| + const ActiveStyleSheetVector& oldStyleSheets,
|
| + const ActiveStyleSheetVector& newStyleSheets,
|
| + HeapVector<Member<RuleSet>>& changedRuleSets)
|
| +{
|
| + unsigned newStyleSheetCount = newStyleSheets.size();
|
| + unsigned oldStyleSheetCount = oldStyleSheets.size();
|
| +
|
| + unsigned minCount = std::min(newStyleSheetCount, oldStyleSheetCount);
|
| + unsigned index = 0;
|
| +
|
| + // Walk the common prefix of stylesheets. If the stylesheet rules were
|
| + // modified since last time, add them to the list of changed rulesets.
|
| + for (; index < minCount && newStyleSheets[index].first == oldStyleSheets[index].first; index++) {
|
| + if (newStyleSheets[index].second == oldStyleSheets[index].second)
|
| + continue;
|
| +
|
| + changedRuleSets.append(newStyleSheets[index].second);
|
| + changedRuleSets.append(oldStyleSheets[index].second);
|
| + }
|
| +
|
| + if (index == oldStyleSheetCount) {
|
| + if (index == newStyleSheetCount)
|
| + return changedRuleSets.size() ? ActiveSheetsChanged : NoActiveSheetsChanged;
|
| +
|
| + // Sheets added at the end.
|
| + for (; index < newStyleSheetCount; index++)
|
| + changedRuleSets.append(newStyleSheets[index].second);
|
| + return ActiveSheetsAppended;
|
| + }
|
| +
|
| + if (index == newStyleSheetCount) {
|
| + // Sheets removed from the end.
|
| + for (; index < oldStyleSheetCount; index++)
|
| + changedRuleSets.append(oldStyleSheets[index].second);
|
| + return ActiveSheetsChanged;
|
| + }
|
| +
|
| + DCHECK(index < oldStyleSheetCount && index < newStyleSheetCount);
|
| +
|
| + // Both the new and old active stylesheet vectors have stylesheets following
|
| + // the common prefix. Figure out which were added or removed by sorting the
|
| + // merged vector of old and new sheets.
|
| +
|
| + ActiveStyleSheetVector mergedSorted;
|
| + mergedSorted.reserveCapacity(oldStyleSheetCount + newStyleSheetCount - 2 * index);
|
| + mergedSorted.appendRange(oldStyleSheets.begin() + index, oldStyleSheets.end());
|
| + mergedSorted.appendRange(newStyleSheets.begin() + index, newStyleSheets.end());
|
| +
|
| + std::sort(mergedSorted.begin(), mergedSorted.end());
|
| +
|
| + auto mergedIterator = mergedSorted.begin();
|
| + while (mergedIterator != mergedSorted.end()) {
|
| +
|
| + const auto& sheet1 = *mergedIterator++;
|
| + if (mergedIterator == mergedSorted.end() || (*mergedIterator).first != sheet1.first) {
|
| + // Sheet either removed or inserted.
|
| + changedRuleSets.append(sheet1.second);
|
| + continue;
|
| + }
|
| +
|
| + // Sheet present in both old and new.
|
| + const auto& sheet2 = *mergedIterator++;
|
| +
|
| + if (sheet1.second == sheet2.second) {
|
| + // TODO(rune@opera.com): HTML Imports keep document and stylesheets
|
| + // when removed from the parent document. The sorting does not detect
|
| + // such issues.
|
| + if (Document* owner = sheet1.first->ownerDocument()) {
|
| + if (owner->importsController() && owner->importsController()->master() != owner)
|
| + changedRuleSets.append(sheet1.second);
|
| + }
|
| + continue;
|
| + }
|
| +
|
| + // Active rules for the given stylesheet changed.
|
| + // DOM, CSSOM, or media query changes.
|
| + changedRuleSets.append(sheet1.second);
|
| + changedRuleSets.append(sheet2.second);
|
| + }
|
| + return ActiveSheetsChanged;
|
| +}
|
| +
|
| +namespace {
|
| +
|
| +enum RuleSetFlags {
|
| + FontFaceRules = 1 << 0,
|
| + KeyframesRules = 1 << 1,
|
| + ShadowBoundaryCrossingRules = 1 << 2,
|
| + UniversalRules = 1 << 3
|
| +};
|
| +
|
| +unsigned getRuleFlags(const HeapVector<Member<RuleSet>> ruleSets)
|
| +{
|
| + unsigned flags = 0;
|
| + for (auto& ruleSet : ruleSets) {
|
| + ruleSet->compactRulesIfNeeded();
|
| + if (!ruleSet->keyframesRules().isEmpty())
|
| + flags |= KeyframesRules;
|
| + if (!ruleSet->fontFaceRules().isEmpty())
|
| + flags |= FontFaceRules;
|
| + if (ruleSet->deepCombinatorOrShadowPseudoRules().size() || ruleSet->contentPseudoElementRules().size() || ruleSet->slottedPseudoElementRules().size())
|
| + flags |= ShadowBoundaryCrossingRules;
|
| + if (ruleSet->features().needsFullRecalcForRuleSetInvalidation())
|
| + flags |= UniversalRules;
|
| + }
|
| + return flags;
|
| +}
|
| +
|
| +ContainerNode& invalidationRootForTreeScope(const TreeScope& treeScope)
|
| +{
|
| + if (treeScope.rootNode().isDocumentNode())
|
| + return treeScope.rootNode();
|
| + return toShadowRoot(treeScope.rootNode()).host();
|
| +}
|
| +
|
| +} // namespace
|
| +
|
| +void applyRuleSetChanges(StyleEngine& engine, TreeScope& treeScope,
|
| + const ActiveStyleSheetVector& oldStyleSheets,
|
| + const ActiveStyleSheetVector& newStyleSheets)
|
| +{
|
| + HeapVector<Member<RuleSet>> changedRuleSets;
|
| +
|
| + ActiveSheetsChange change = compareActiveStyleSheets(oldStyleSheets, newStyleSheets, changedRuleSets);
|
| + if (change == NoActiveSheetsChanged)
|
| + return;
|
| +
|
| + engine.setNeedsGlobalRuleSetUpdate();
|
| +
|
| + unsigned changedRuleFlags = getRuleFlags(changedRuleSets);
|
| + bool fontsChanged = treeScope.rootNode().isDocumentNode() && (changedRuleFlags & FontFaceRules);
|
| +
|
| + if (change == ActiveSheetsChanged) {
|
| +
|
| + if (fontsChanged)
|
| + engine.clearFontCache();
|
| +
|
| + if (newStyleSheets.isEmpty()) {
|
| + treeScope.clearScopedStyleResolver();
|
| + } else {
|
| + if (treeScope.scopedStyleResolver())
|
| + treeScope.scopedStyleResolver()->resetAuthorStyle();
|
| + treeScope.ensureScopedStyleResolver().appendActiveStyleSheets(0, newStyleSheets);
|
| + }
|
| +
|
| + } else {
|
| + treeScope.ensureScopedStyleResolver().appendActiveStyleSheets(oldStyleSheets.size(), newStyleSheets);
|
| + }
|
| +
|
| + if (treeScope.document().hasPendingForcedStyleRecalc())
|
| + return;
|
| +
|
| + if (!treeScope.document().body() || treeScope.document().hasNodesWithPlaceholderStyle()) {
|
| + treeScope.document().setNeedsStyleRecalc(SubtreeStyleChange, StyleChangeReasonForTracing::create(StyleChangeReason::CleanupPlaceholderStyles));
|
| + return;
|
| + }
|
| +
|
| + if (fontsChanged || (changedRuleFlags & (KeyframesRules | ShadowBoundaryCrossingRules | UniversalRules))) {
|
| + invalidationRootForTreeScope(treeScope).setNeedsStyleRecalc(SubtreeStyleChange, StyleChangeReasonForTracing::create(StyleChangeReason::ActiveStylesheetsUpdate));
|
| + return;
|
| + }
|
| +
|
| + engine.scheduleInvalidationsForRuleSets(treeScope, changedRuleSets);
|
| +}
|
| +
|
| +} // namespace blink
|
|
|