/
StyleSharingResolver.cpp
365 lines (313 loc) · 14.1 KB
/
StyleSharingResolver.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/*
* Copyright (C) 2016 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "StyleSharingResolver.h"
#include "ElementRuleCollector.h"
#include "FullscreenManager.h"
#include "HTMLInputElement.h"
#include "HTMLNames.h"
#include "NodeRenderStyle.h"
#include "RenderStyle.h"
#include "SVGElement.h"
#include "ShadowRoot.h"
#include "StyleResolver.h"
#include "StyleScope.h"
#include "StyleScopeRuleSets.h"
#include "StyleUpdate.h"
#include "StyledElement.h"
#include "VisitedLinkState.h"
#include "WebVTTElement.h"
#include "XMLNames.h"
namespace WebCore {
namespace Style {
static const unsigned cStyleSearchThreshold = 10;
struct SharingResolver::Context {
const Update& update;
const StyledElement& element;
bool elementAffectedByClassRules;
InsideLink elementLinkState;
};
SharingResolver::SharingResolver(const Document& document, const ScopeRuleSets& ruleSets, const SelectorFilter& selectorFilter)
: m_document(document)
, m_ruleSets(ruleSets)
, m_selectorFilter(selectorFilter)
{
}
static inline bool parentElementPreventsSharing(const Element& parentElement)
{
return parentElement.hasFlagsSetDuringStylingOfChildren();
}
static inline bool elementHasDirectionAuto(const Element& element)
{
return is<HTMLElement>(element) && downcast<HTMLElement>(element).hasDirectionAuto();
}
std::unique_ptr<RenderStyle> SharingResolver::resolve(const Styleable& searchStyleable, const Update& update)
{
if (!is<StyledElement>(searchStyleable.element))
return nullptr;
auto& element = downcast<StyledElement>(searchStyleable.element);
if (!element.parentElement())
return nullptr;
auto& parentElement = *element.parentElement();
if (parentElement.shadowRoot())
return nullptr;
if (!update.elementStyle(parentElement))
return nullptr;
// If the element has inline style it is probably unique.
if (element.inlineStyle())
return nullptr;
if (element.isSVGElement() && downcast<SVGElement>(element).animatedSMILStyleProperties())
return nullptr;
// Ids stop style sharing if they show up in the stylesheets.
auto& id = element.idForStyleResolution();
if (!id.isNull() && m_ruleSets.features().idsInRules.contains(id))
return nullptr;
if (parentElementPreventsSharing(parentElement))
return nullptr;
if (&element == m_document.cssTarget())
return nullptr;
if (elementHasDirectionAuto(element))
return nullptr;
if (element.shadowRoot() && !element.shadowRoot()->styleScope().resolver().ruleSets().authorStyle().hostPseudoClassRules().isEmpty())
return nullptr;
if (auto* keyframeEffectStack = searchStyleable.keyframeEffectStack()) {
if (keyframeEffectStack->hasEffectWithImplicitKeyframes())
return nullptr;
}
Context context {
update,
element,
element.hasClass() && classNamesAffectedByRules(element.classNames()),
m_document.visitedLinkState().determineLinkState(element)
};
// Check previous siblings and their cousins.
unsigned count = 0;
StyledElement* shareElement = nullptr;
Node* cousinList = element.previousSibling();
while (cousinList) {
shareElement = findSibling(context, cousinList, count);
if (shareElement)
break;
if (count >= cStyleSearchThreshold)
break;
cousinList = locateCousinList(cousinList->parentElement());
}
// If we have exhausted all our budget or our cousins.
if (!shareElement)
return nullptr;
// Can't share if sibling rules apply. This is checked at the end as it should rarely fail.
if (styleSharingCandidateMatchesRuleSet(element, m_ruleSets.sibling()))
return nullptr;
// Can't share if attribute rules apply.
if (styleSharingCandidateMatchesRuleSet(element, m_ruleSets.uncommonAttribute()))
return nullptr;
// Tracking child index requires unique style for each node. This may get set by the sibling rule match above.
if (parentElementPreventsSharing(parentElement))
return nullptr;
m_elementsSharingStyle.add(&element, shareElement);
return RenderStyle::clonePtr(*update.elementStyle(*shareElement));
}
StyledElement* SharingResolver::findSibling(const Context& context, Node* node, unsigned& count) const
{
for (; node; node = node->previousSibling()) {
if (!is<StyledElement>(*node))
continue;
if (canShareStyleWithElement(context, downcast<StyledElement>(*node)))
break;
if (count++ >= cStyleSearchThreshold)
return nullptr;
}
return downcast<StyledElement>(node);
}
Node* SharingResolver::locateCousinList(const Element* parent) const
{
for (unsigned count = 0; count < cStyleSearchThreshold; ++count) {
auto* elementSharingParentStyle = m_elementsSharingStyle.get(parent);
if (!elementSharingParentStyle)
return nullptr;
if (!parentElementPreventsSharing(*elementSharingParentStyle)) {
if (auto* cousin = elementSharingParentStyle->lastChild())
return cousin;
}
parent = elementSharingParentStyle;
}
return nullptr;
}
static bool canShareStyleWithControl(const HTMLFormControlElement& element, const HTMLFormControlElement& formElement)
{
if (!is<HTMLInputElement>(formElement) || !is<HTMLInputElement>(element))
return false;
auto& thisInputElement = downcast<HTMLInputElement>(formElement);
auto& otherInputElement = downcast<HTMLInputElement>(element);
if (thisInputElement.isAutoFilled() != otherInputElement.isAutoFilled())
return false;
if (thisInputElement.shouldAppearChecked() != otherInputElement.shouldAppearChecked())
return false;
if (thisInputElement.isRequired() != otherInputElement.isRequired())
return false;
if (formElement.isDisabledFormControl() != element.isDisabledFormControl())
return false;
if (formElement.isInRange() != element.isInRange())
return false;
if (formElement.isOutOfRange() != element.isOutOfRange())
return false;
return true;
}
bool SharingResolver::canShareStyleWithElement(const Context& context, const StyledElement& candidateElement) const
{
auto& element = context.element;
auto* style = context.update.elementStyle(candidateElement);
if (!style)
return false;
if (style->unique())
return false;
if (style->hasUniquePseudoStyle())
return false;
if (candidateElement.tagQName() != element.tagQName())
return false;
if (candidateElement.inlineStyle())
return false;
if (candidateElement.needsStyleRecalc())
return false;
if (candidateElement.isSVGElement() && downcast<SVGElement>(candidateElement).animatedSMILStyleProperties())
return false;
if (candidateElement.isLink() != element.isLink())
return false;
if (candidateElement.hovered() != element.hovered())
return false;
if (candidateElement.active() != element.active())
return false;
if (candidateElement.focused() != element.focused())
return false;
if (candidateElement.hasFocusWithin() != element.hasFocusWithin())
return false;
if (candidateElement.isBeingDragged() != element.isBeingDragged())
return false;
if (candidateElement.shadowPseudoId() != element.shadowPseudoId())
return false;
if (element.isInShadowTree() && candidateElement.partNames() != element.partNames())
return false;
if (&candidateElement == m_document.cssTarget())
return false;
if (!sharingCandidateHasIdenticalStyleAffectingAttributes(context, candidateElement))
return false;
if (const_cast<StyledElement&>(candidateElement).additionalPresentationAttributeStyle() != const_cast<StyledElement&>(element).additionalPresentationAttributeStyle())
return false;
if (candidateElement.affectsNextSiblingElementStyle() || candidateElement.styleIsAffectedByPreviousSibling())
return false;
auto& candidateElementId = candidateElement.idForStyleResolution();
if (!candidateElementId.isNull() && m_ruleSets.features().idsInRules.contains(candidateElementId))
return false;
bool isControl = is<HTMLFormControlElement>(candidateElement);
if (isControl != is<HTMLFormControlElement>(element))
return false;
if (isControl && !canShareStyleWithControl(downcast<HTMLFormControlElement>(element), downcast<HTMLFormControlElement>(candidateElement)))
return false;
if (style->transitions() || style->animations())
return false;
// Turn off style sharing for elements that can gain layers for reasons outside of the style system.
// See comments in RenderObject::setStyle().
if (candidateElement.hasTagName(HTMLNames::iframeTag) || candidateElement.hasTagName(HTMLNames::frameTag))
return false;
if (candidateElement.hasTagName(HTMLNames::embedTag) || candidateElement.hasTagName(HTMLNames::objectTag) || candidateElement.hasTagName(HTMLNames::appletTag) || candidateElement.hasTagName(HTMLNames::canvasTag))
return false;
if (elementHasDirectionAuto(candidateElement))
return false;
if (candidateElement.isLink() && context.elementLinkState != style->insideLink())
return false;
if (candidateElement.elementData() != element.elementData()) {
if (candidateElement.attributeWithoutSynchronization(HTMLNames::readonlyAttr) != element.attributeWithoutSynchronization(HTMLNames::readonlyAttr))
return false;
if (candidateElement.isSVGElement()) {
if (candidateElement.getAttribute(HTMLNames::typeAttr) != element.getAttribute(HTMLNames::typeAttr))
return false;
} else {
if (candidateElement.attributeWithoutSynchronization(HTMLNames::typeAttr) != element.attributeWithoutSynchronization(HTMLNames::typeAttr))
return false;
}
}
if (candidateElement.matchesValidPseudoClass() != element.matchesValidPseudoClass())
return false;
if (element.matchesInvalidPseudoClass() != element.matchesValidPseudoClass())
return false;
if (candidateElement.matchesIndeterminatePseudoClass() != element.matchesIndeterminatePseudoClass())
return false;
if (candidateElement.matchesDefaultPseudoClass() != element.matchesDefaultPseudoClass())
return false;
if (candidateElement.shadowRoot() && !candidateElement.shadowRoot()->styleScope().resolver().ruleSets().authorStyle().hostPseudoClassRules().isEmpty())
return false;
#if ENABLE(WHEEL_EVENT_REGIONS)
if (candidateElement.hasEventListeners() || element.hasEventListeners())
return false;
#endif
#if ENABLE(FULLSCREEN_API)
if (&candidateElement == m_document.fullscreenManager().currentFullscreenElement() || &element == m_document.fullscreenManager().currentFullscreenElement())
return false;
#endif
return true;
}
bool SharingResolver::styleSharingCandidateMatchesRuleSet(const StyledElement& element, const RuleSet* ruleSet) const
{
if (!ruleSet)
return false;
ElementRuleCollector collector(const_cast<StyledElement&>(element), m_ruleSets, &m_selectorFilter);
return collector.hasAnyMatchingRules(ruleSet);
}
bool SharingResolver::sharingCandidateHasIdenticalStyleAffectingAttributes(const Context& context, const StyledElement& sharingCandidate) const
{
auto& element = context.element;
if (element.elementData() == sharingCandidate.elementData())
return true;
if (element.attributeWithoutSynchronization(XMLNames::langAttr) != sharingCandidate.attributeWithoutSynchronization(XMLNames::langAttr))
return false;
if (element.attributeWithoutSynchronization(HTMLNames::langAttr) != sharingCandidate.attributeWithoutSynchronization(HTMLNames::langAttr))
return false;
if (context.elementAffectedByClassRules) {
if (!sharingCandidate.hasClass())
return false;
// SVG elements require a (slow!) getAttribute comparision because "class" is an animatable attribute for SVG.
if (element.isSVGElement()) {
if (element.getAttribute(HTMLNames::classAttr) != sharingCandidate.getAttribute(HTMLNames::classAttr))
return false;
} else {
if (element.classNames() != sharingCandidate.classNames())
return false;
}
} else if (sharingCandidate.hasClass() && classNamesAffectedByRules(sharingCandidate.classNames()))
return false;
if (const_cast<StyledElement&>(element).presentationAttributeStyle() != const_cast<StyledElement&>(sharingCandidate).presentationAttributeStyle())
return false;
return true;
}
bool SharingResolver::classNamesAffectedByRules(const SpaceSplitString& classNames) const
{
for (unsigned i = 0; i < classNames.size(); ++i) {
if (m_ruleSets.features().classRules.contains(classNames[i]))
return true;
}
return false;
}
}
}