OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "core/css/ActiveStyleSheets.h" |
| 6 |
| 7 #include "core/css/CSSStyleSheet.h" |
| 8 #include "core/css/RuleSet.h" |
| 9 #include "core/css/resolver/ScopedStyleResolver.h" |
| 10 #include "core/dom/ContainerNode.h" |
| 11 #include "core/dom/StyleChangeReason.h" |
| 12 #include "core/dom/StyleEngine.h" |
| 13 #include "core/dom/TreeScope.h" |
| 14 #include "core/dom/shadow/ShadowRoot.h" |
| 15 #include "core/html/imports/HTMLImportsController.h" |
| 16 |
| 17 namespace blink { |
| 18 |
| 19 ActiveSheetsChange compareActiveStyleSheets( |
| 20 const ActiveStyleSheetVector& oldStyleSheets, |
| 21 const ActiveStyleSheetVector& newStyleSheets, |
| 22 HeapVector<Member<RuleSet>>& changedRuleSets) |
| 23 { |
| 24 unsigned newStyleSheetCount = newStyleSheets.size(); |
| 25 unsigned oldStyleSheetCount = oldStyleSheets.size(); |
| 26 |
| 27 unsigned minCount = std::min(newStyleSheetCount, oldStyleSheetCount); |
| 28 unsigned index = 0; |
| 29 |
| 30 // Walk the common prefix of stylesheets. If the stylesheet rules were |
| 31 // modified since last time, add them to the list of changed rulesets. |
| 32 for (; index < minCount && newStyleSheets[index].first == oldStyleSheets[ind
ex].first; index++) { |
| 33 if (newStyleSheets[index].second == oldStyleSheets[index].second) |
| 34 continue; |
| 35 |
| 36 changedRuleSets.append(newStyleSheets[index].second); |
| 37 changedRuleSets.append(oldStyleSheets[index].second); |
| 38 } |
| 39 |
| 40 if (index == oldStyleSheetCount) { |
| 41 if (index == newStyleSheetCount) |
| 42 return changedRuleSets.size() ? ActiveSheetsChanged : NoActiveSheets
Changed; |
| 43 |
| 44 // Sheets added at the end. |
| 45 for (; index < newStyleSheetCount; index++) |
| 46 changedRuleSets.append(newStyleSheets[index].second); |
| 47 return ActiveSheetsAppended; |
| 48 } |
| 49 |
| 50 if (index == newStyleSheetCount) { |
| 51 // Sheets removed from the end. |
| 52 for (; index < oldStyleSheetCount; index++) |
| 53 changedRuleSets.append(oldStyleSheets[index].second); |
| 54 return ActiveSheetsChanged; |
| 55 } |
| 56 |
| 57 DCHECK(index < oldStyleSheetCount && index < newStyleSheetCount); |
| 58 |
| 59 // Both the new and old active stylesheet vectors have stylesheets following |
| 60 // the common prefix. Figure out which were added or removed by sorting the |
| 61 // merged vector of old and new sheets. |
| 62 |
| 63 ActiveStyleSheetVector mergedSorted; |
| 64 mergedSorted.reserveCapacity(oldStyleSheetCount + newStyleSheetCount - 2 * i
ndex); |
| 65 mergedSorted.appendRange(oldStyleSheets.begin() + index, oldStyleSheets.end(
)); |
| 66 mergedSorted.appendRange(newStyleSheets.begin() + index, newStyleSheets.end(
)); |
| 67 |
| 68 std::sort(mergedSorted.begin(), mergedSorted.end()); |
| 69 |
| 70 auto mergedIterator = mergedSorted.begin(); |
| 71 while (mergedIterator != mergedSorted.end()) { |
| 72 |
| 73 const auto& sheet1 = *mergedIterator++; |
| 74 if (mergedIterator == mergedSorted.end() || (*mergedIterator).first != s
heet1.first) { |
| 75 // Sheet either removed or inserted. |
| 76 changedRuleSets.append(sheet1.second); |
| 77 continue; |
| 78 } |
| 79 |
| 80 // Sheet present in both old and new. |
| 81 const auto& sheet2 = *mergedIterator++; |
| 82 |
| 83 if (sheet1.second == sheet2.second) { |
| 84 // TODO(rune@opera.com): HTML Imports keep document and stylesheets |
| 85 // when removed from the parent document. The sorting does not detec
t |
| 86 // such issues. |
| 87 if (Document* owner = sheet1.first->ownerDocument()) { |
| 88 if (owner->importsController() && owner->importsController()->ma
ster() != owner) |
| 89 changedRuleSets.append(sheet1.second); |
| 90 } |
| 91 continue; |
| 92 } |
| 93 |
| 94 // Active rules for the given stylesheet changed. |
| 95 // DOM, CSSOM, or media query changes. |
| 96 changedRuleSets.append(sheet1.second); |
| 97 changedRuleSets.append(sheet2.second); |
| 98 } |
| 99 return ActiveSheetsChanged; |
| 100 } |
| 101 |
| 102 namespace { |
| 103 |
| 104 enum RuleSetFlags { |
| 105 FontFaceRules = 1 << 0, |
| 106 KeyframesRules = 1 << 1, |
| 107 ShadowBoundaryCrossingRules = 1 << 2, |
| 108 UniversalRules = 1 << 3 |
| 109 }; |
| 110 |
| 111 unsigned getRuleFlags(const HeapVector<Member<RuleSet>> ruleSets) |
| 112 { |
| 113 unsigned flags = 0; |
| 114 for (auto& ruleSet : ruleSets) { |
| 115 ruleSet->compactRulesIfNeeded(); |
| 116 if (!ruleSet->keyframesRules().isEmpty()) |
| 117 flags |= KeyframesRules; |
| 118 if (!ruleSet->fontFaceRules().isEmpty()) |
| 119 flags |= FontFaceRules; |
| 120 if (ruleSet->deepCombinatorOrShadowPseudoRules().size() || ruleSet->cont
entPseudoElementRules().size() || ruleSet->slottedPseudoElementRules().size()) |
| 121 flags |= ShadowBoundaryCrossingRules; |
| 122 if (ruleSet->features().needsFullRecalcForRuleSetInvalidation()) |
| 123 flags |= UniversalRules; |
| 124 } |
| 125 return flags; |
| 126 } |
| 127 |
| 128 ContainerNode& invalidationRootForTreeScope(const TreeScope& treeScope) |
| 129 { |
| 130 if (treeScope.rootNode().isDocumentNode()) |
| 131 return treeScope.rootNode(); |
| 132 return toShadowRoot(treeScope.rootNode()).host(); |
| 133 } |
| 134 |
| 135 } // namespace |
| 136 |
| 137 void applyRuleSetChanges(StyleEngine& engine, TreeScope& treeScope, |
| 138 const ActiveStyleSheetVector& oldStyleSheets, |
| 139 const ActiveStyleSheetVector& newStyleSheets) |
| 140 { |
| 141 HeapVector<Member<RuleSet>> changedRuleSets; |
| 142 |
| 143 ActiveSheetsChange change = compareActiveStyleSheets(oldStyleSheets, newStyl
eSheets, changedRuleSets); |
| 144 if (change == NoActiveSheetsChanged) |
| 145 return; |
| 146 |
| 147 engine.setNeedsGlobalRuleSetUpdate(); |
| 148 |
| 149 unsigned changedRuleFlags = getRuleFlags(changedRuleSets); |
| 150 bool fontsChanged = treeScope.rootNode().isDocumentNode() && (changedRuleFla
gs & FontFaceRules); |
| 151 |
| 152 if (change == ActiveSheetsChanged) { |
| 153 |
| 154 if (fontsChanged) |
| 155 engine.clearFontCache(); |
| 156 |
| 157 if (newStyleSheets.isEmpty()) { |
| 158 treeScope.clearScopedStyleResolver(); |
| 159 } else { |
| 160 if (treeScope.scopedStyleResolver()) |
| 161 treeScope.scopedStyleResolver()->resetAuthorStyle(); |
| 162 treeScope.ensureScopedStyleResolver().appendActiveStyleSheets(0, new
StyleSheets); |
| 163 } |
| 164 |
| 165 } else { |
| 166 treeScope.ensureScopedStyleResolver().appendActiveStyleSheets(oldStyleSh
eets.size(), newStyleSheets); |
| 167 } |
| 168 |
| 169 if (treeScope.document().hasPendingForcedStyleRecalc()) |
| 170 return; |
| 171 |
| 172 if (!treeScope.document().body() || treeScope.document().hasNodesWithPlaceho
lderStyle()) { |
| 173 treeScope.document().setNeedsStyleRecalc(SubtreeStyleChange, StyleChange
ReasonForTracing::create(StyleChangeReason::CleanupPlaceholderStyles)); |
| 174 return; |
| 175 } |
| 176 |
| 177 if (fontsChanged || (changedRuleFlags & (KeyframesRules | ShadowBoundaryCros
singRules | UniversalRules))) { |
| 178 invalidationRootForTreeScope(treeScope).setNeedsStyleRecalc(SubtreeStyle
Change, StyleChangeReasonForTracing::create(StyleChangeReason::ActiveStylesheets
Update)); |
| 179 return; |
| 180 } |
| 181 |
| 182 engine.scheduleInvalidationsForRuleSets(treeScope, changedRuleSets); |
| 183 } |
| 184 |
| 185 } // namespace blink |
OLD | NEW |