OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "core/css/ActiveStyleSheets.h" | 5 #include "core/css/ActiveStyleSheets.h" |
6 | 6 |
7 #include "core/css/CSSStyleSheet.h" | 7 #include "core/css/CSSStyleSheet.h" |
8 #include "core/css/RuleSet.h" | 8 #include "core/css/RuleSet.h" |
9 | 9 |
10 namespace blink { | 10 namespace blink { |
11 | 11 |
12 ActiveSheetsChange compareActiveStyleSheets( | 12 ActiveSheetsChange compareActiveStyleSheets( |
13 const ActiveStyleSheetVector& oldStyleSheets, | 13 const ActiveStyleSheetVector& oldStyleSheets, |
14 const ActiveStyleSheetVector& newStyleSheets, | 14 const ActiveStyleSheetVector& newStyleSheets, |
15 HeapVector<Member<RuleSet>>& changedRuleSets) { | 15 HeapVector<Member<RuleSet>>& changedRuleSets) { |
16 unsigned newStyleSheetCount = newStyleSheets.size(); | 16 unsigned newStyleSheetCount = newStyleSheets.size(); |
17 unsigned oldStyleSheetCount = oldStyleSheets.size(); | 17 unsigned oldStyleSheetCount = oldStyleSheets.size(); |
18 | 18 |
19 unsigned minCount = std::min(newStyleSheetCount, oldStyleSheetCount); | 19 unsigned minCount = std::min(newStyleSheetCount, oldStyleSheetCount); |
20 unsigned index = 0; | 20 unsigned index = 0; |
21 | 21 |
22 // Walk the common prefix of stylesheets. If the stylesheet rules were | 22 // Walk the common prefix of stylesheets. If the stylesheet rules were |
23 // modified since last time, add them to the list of changed rulesets. | 23 // modified since last time, add them to the list of changed rulesets. |
24 for (; index < minCount && | 24 for (; index < minCount && |
25 newStyleSheets[index].first == oldStyleSheets[index].first; | 25 newStyleSheets[index].first == oldStyleSheets[index].first; |
26 index++) { | 26 index++) { |
27 if (newStyleSheets[index].second == oldStyleSheets[index].second) | 27 if (newStyleSheets[index].second == oldStyleSheets[index].second) |
28 continue; | 28 continue; |
29 | 29 |
30 changedRuleSets.append(newStyleSheets[index].second); | 30 if (newStyleSheets[index].second) |
31 changedRuleSets.append(oldStyleSheets[index].second); | 31 changedRuleSets.append(newStyleSheets[index].second); |
| 32 if (oldStyleSheets[index].second) |
| 33 changedRuleSets.append(oldStyleSheets[index].second); |
32 } | 34 } |
33 | 35 |
34 if (index == oldStyleSheetCount) { | 36 if (index == oldStyleSheetCount) { |
35 if (index == newStyleSheetCount) | 37 if (index == newStyleSheetCount) |
36 return changedRuleSets.size() ? ActiveSheetsChanged | 38 return changedRuleSets.size() ? ActiveSheetsChanged |
37 : NoActiveSheetsChanged; | 39 : NoActiveSheetsChanged; |
38 | 40 |
39 // Sheets added at the end. | 41 // Sheets added at the end. |
40 for (; index < newStyleSheetCount; index++) | 42 for (; index < newStyleSheetCount; index++) { |
41 changedRuleSets.append(newStyleSheets[index].second); | 43 if (newStyleSheets[index].second) |
42 return ActiveSheetsAppended; | 44 changedRuleSets.append(newStyleSheets[index].second); |
| 45 } |
| 46 return changedRuleSets.size() ? ActiveSheetsAppended |
| 47 : NoActiveSheetsChanged; |
43 } | 48 } |
44 | 49 |
45 if (index == newStyleSheetCount) { | 50 if (index == newStyleSheetCount) { |
46 // Sheets removed from the end. | 51 // Sheets removed from the end. |
47 for (; index < oldStyleSheetCount; index++) | 52 for (; index < oldStyleSheetCount; index++) { |
48 changedRuleSets.append(oldStyleSheets[index].second); | 53 if (oldStyleSheets[index].second) |
49 return ActiveSheetsChanged; | 54 changedRuleSets.append(oldStyleSheets[index].second); |
| 55 } |
| 56 return changedRuleSets.size() ? ActiveSheetsChanged : NoActiveSheetsChanged; |
50 } | 57 } |
51 | 58 |
52 DCHECK(index < oldStyleSheetCount && index < newStyleSheetCount); | 59 DCHECK(index < oldStyleSheetCount && index < newStyleSheetCount); |
53 | 60 |
54 // Both the new and old active stylesheet vectors have stylesheets following | 61 // Both the new and old active stylesheet vectors have stylesheets following |
55 // the common prefix. Figure out which were added or removed by sorting the | 62 // the common prefix. Figure out which were added or removed by sorting the |
56 // merged vector of old and new sheets. | 63 // merged vector of old and new sheets. |
57 | 64 |
58 ActiveStyleSheetVector mergedSorted; | 65 ActiveStyleSheetVector mergedSorted; |
59 mergedSorted.reserveCapacity(oldStyleSheetCount + newStyleSheetCount - | 66 mergedSorted.reserveCapacity(oldStyleSheetCount + newStyleSheetCount - |
60 2 * index); | 67 2 * index); |
61 mergedSorted.appendRange(oldStyleSheets.begin() + index, | 68 mergedSorted.appendRange(oldStyleSheets.begin() + index, |
62 oldStyleSheets.end()); | 69 oldStyleSheets.end()); |
63 mergedSorted.appendRange(newStyleSheets.begin() + index, | 70 mergedSorted.appendRange(newStyleSheets.begin() + index, |
64 newStyleSheets.end()); | 71 newStyleSheets.end()); |
65 | 72 |
66 std::sort(mergedSorted.begin(), mergedSorted.end()); | 73 std::sort(mergedSorted.begin(), mergedSorted.end()); |
67 | 74 |
68 auto mergedIterator = mergedSorted.begin(); | 75 auto mergedIterator = mergedSorted.begin(); |
69 while (mergedIterator != mergedSorted.end()) { | 76 while (mergedIterator != mergedSorted.end()) { |
70 const auto& sheet1 = *mergedIterator++; | 77 const auto& sheet1 = *mergedIterator++; |
71 if (mergedIterator == mergedSorted.end() || | 78 if (mergedIterator == mergedSorted.end() || |
72 (*mergedIterator).first != sheet1.first) { | 79 (*mergedIterator).first != sheet1.first) { |
73 // Sheet either removed or inserted. | 80 // Sheet either removed or inserted. |
74 changedRuleSets.append(sheet1.second); | 81 if (sheet1.second) |
| 82 changedRuleSets.append(sheet1.second); |
75 continue; | 83 continue; |
76 } | 84 } |
77 | 85 |
78 // Sheet present in both old and new. | 86 // Sheet present in both old and new. |
79 const auto& sheet2 = *mergedIterator++; | 87 const auto& sheet2 = *mergedIterator++; |
80 | 88 |
81 if (sheet1.second == sheet2.second) | 89 if (sheet1.second == sheet2.second) |
82 continue; | 90 continue; |
83 | 91 |
84 // Active rules for the given stylesheet changed. | 92 // Active rules for the given stylesheet changed. |
85 // DOM, CSSOM, or media query changes. | 93 // DOM, CSSOM, or media query changes. |
86 changedRuleSets.append(sheet1.second); | 94 if (sheet1.second) |
87 changedRuleSets.append(sheet2.second); | 95 changedRuleSets.append(sheet1.second); |
| 96 if (sheet2.second) |
| 97 changedRuleSets.append(sheet2.second); |
88 } | 98 } |
89 return ActiveSheetsChanged; | 99 return changedRuleSets.size() ? ActiveSheetsChanged : NoActiveSheetsChanged; |
90 } | 100 } |
91 | 101 |
92 } // namespace blink | 102 } // namespace blink |
OLD | NEW |