File: SharedStyleFinder.cpp

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (406 lines) | stat: -rw-r--r-- 14,272 bytes parent folder | download
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
/*
 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
 *           (C) 2004-2005 Allan Sandfeld Jensen (kde@carewolf.com)
 * Copyright (C) 2006, 2007 Nicholas Shanks (webkit@nickshanks.com)
 * Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 Apple Inc.
 * All rights reserved.
 * Copyright (C) 2007 Alexey Proskuryakov <ap@webkit.org>
 * Copyright (C) 2007, 2008 Eric Seidel <eric@webkit.org>
 * Copyright (C) 2008, 2009 Torch Mobile Inc. All rights reserved.
 * (http://www.torchmobile.com/)
 * Copyright (c) 2011, Code Aurora Forum. All rights reserved.
 * Copyright (C) Research In Motion Limited 2011. All rights reserved.
 * Copyright (C) 2013 Google Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include "core/css/resolver/SharedStyleFinder.h"

#include "core/HTMLNames.h"
#include "core/XMLNames.h"
#include "core/css/resolver/StyleResolver.h"
#include "core/css/resolver/StyleResolverStats.h"
#include "core/dom/ContainerNode.h"
#include "core/dom/Document.h"
#include "core/dom/ElementTraversal.h"
#include "core/dom/Node.h"
#include "core/dom/NodeComputedStyle.h"
#include "core/dom/QualifiedName.h"
#include "core/dom/SpaceSplitString.h"
#include "core/dom/StyleEngine.h"
#include "core/dom/shadow/ElementShadow.h"
#include "core/dom/shadow/InsertionPoint.h"
#include "core/html/HTMLElement.h"
#include "core/html/HTMLInputElement.h"
#include "core/html/HTMLOptGroupElement.h"
#include "core/html/HTMLOptionElement.h"
#include "core/style/ComputedStyle.h"
#include "core/svg/SVGElement.h"
#include "platform/instrumentation/tracing/TraceEvent.h"
#include "wtf/HashSet.h"
#include "wtf/text/AtomicString.h"

namespace blink {

using namespace HTMLNames;

bool SharedStyleFinder::canShareStyleWithControl(Element& candidate) const {
  if (!isHTMLInputElement(candidate) || !isHTMLInputElement(element()))
    return false;

  HTMLInputElement& candidateInput = toHTMLInputElement(candidate);
  HTMLInputElement& thisInput = toHTMLInputElement(element());

  if (candidateInput.isAutofilled() != thisInput.isAutofilled())
    return false;
  if (candidateInput.shouldAppearChecked() != thisInput.shouldAppearChecked())
    return false;
  if (candidateInput.shouldAppearIndeterminate() !=
      thisInput.shouldAppearIndeterminate())
    return false;
  if (candidateInput.isRequired() != thisInput.isRequired())
    return false;

  if (candidate.isDisabledFormControl() != element().isDisabledFormControl())
    return false;

  if (candidate.matchesDefaultPseudoClass() !=
      element().matchesDefaultPseudoClass())
    return false;

  if (document().containsValidityStyleRules()) {
    bool willValidate = candidate.willValidate();

    if (willValidate != element().willValidate())
      return false;

    if (willValidate &&
        (candidate.isValidElement() != element().isValidElement()))
      return false;

    if (candidate.isInRange() != element().isInRange())
      return false;

    if (candidate.isOutOfRange() != element().isOutOfRange())
      return false;
  }

  if (candidateInput.isPlaceholderVisible() != thisInput.isPlaceholderVisible())
    return false;

  return true;
}

bool SharedStyleFinder::classNamesAffectedByRules(
    const SpaceSplitString& classNames) const {
  unsigned count = classNames.size();
  for (unsigned i = 0; i < count; ++i) {
    if (m_features.hasSelectorForClass(classNames[i]))
      return true;
  }
  return false;
}

static inline const AtomicString& typeAttributeValue(const Element& element) {
  // type is animatable in SVG so we need to go down the slow path here.
  return element.isSVGElement() ? element.getAttribute(typeAttr)
                                : element.fastGetAttribute(typeAttr);
}

bool SharedStyleFinder::sharingCandidateHasIdenticalStyleAffectingAttributes(
    Element& candidate) const {
  if (element().sharesSameElementData(candidate))
    return true;
  if (element().fastGetAttribute(XMLNames::langAttr) !=
      candidate.fastGetAttribute(XMLNames::langAttr))
    return false;
  if (element().fastGetAttribute(langAttr) !=
      candidate.fastGetAttribute(langAttr))
    return false;

  // These two checks must be here since RuleSet has a special case to allow
  // style sharing between elements with type and readonly attributes whereas
  // other attribute selectors prevent sharing.
  if (typeAttributeValue(element()) != typeAttributeValue(candidate))
    return false;
  if (element().fastGetAttribute(readonlyAttr) !=
      candidate.fastGetAttribute(readonlyAttr))
    return false;

  if (!m_elementAffectedByClassRules) {
    if (candidate.hasClass() &&
        classNamesAffectedByRules(candidate.classNames()))
      return false;
  } else if (candidate.hasClass()) {
    // SVG elements require a (slow!) getAttribute comparision because "class"
    // is an animatable attribute for SVG.
    if (element().isSVGElement()) {
      if (element().getAttribute(classAttr) !=
          candidate.getAttribute(classAttr))
        return false;
    } else if (element().classNames() != candidate.classNames()) {
      return false;
    }
  } else {
    return false;
  }

  if (element().presentationAttributeStyle() !=
      candidate.presentationAttributeStyle())
    return false;

  // FIXME: Consider removing this, it's unlikely we'll have so many progress
  // elements that sharing the style makes sense. Instead we should just not
  // support style sharing for them.
  if (isHTMLProgressElement(element())) {
    if (element().shouldAppearIndeterminate() !=
        candidate.shouldAppearIndeterminate())
      return false;
  }

  if (isHTMLOptGroupElement(element()) || isHTMLOptionElement(element())) {
    if (element().isDisabledFormControl() != candidate.isDisabledFormControl())
      return false;
    if (isHTMLOptionElement(element()) &&
        toHTMLOptionElement(element()).selected() !=
            toHTMLOptionElement(candidate).selected())
      return false;
  }

  return true;
}

bool SharedStyleFinder::sharingCandidateCanShareHostStyles(
    Element& candidate) const {
  const ElementShadow* elementShadow = element().shadow();
  const ElementShadow* candidateShadow = candidate.shadow();

  if (!elementShadow && !candidateShadow)
    return true;

  if (static_cast<bool>(elementShadow) != static_cast<bool>(candidateShadow))
    return false;

  return elementShadow->hasSameStyles(*candidateShadow);
}

bool SharedStyleFinder::sharingCandidateAssignedToSameSlot(
    Element& candidate) const {
  HTMLSlotElement* elementSlot = element().assignedSlot();
  HTMLSlotElement* candidateSlot = candidate.assignedSlot();
  if (!elementSlot && !candidateSlot)
    return true;
  return elementSlot == candidateSlot;
}

bool SharedStyleFinder::sharingCandidateDistributedToSameInsertionPoint(
    Element& candidate) const {
  HeapVector<Member<InsertionPoint>, 8> insertionPoints,
      candidateInsertionPoints;
  collectDestinationInsertionPoints(element(), insertionPoints);
  collectDestinationInsertionPoints(candidate, candidateInsertionPoints);
  if (insertionPoints.size() != candidateInsertionPoints.size())
    return false;
  for (size_t i = 0; i < insertionPoints.size(); ++i) {
    if (insertionPoints[i] != candidateInsertionPoints[i])
      return false;
  }
  return true;
}

DISABLE_CFI_PERF
bool SharedStyleFinder::canShareStyleWithElement(Element& candidate) const {
  if (element() == candidate)
    return false;
  Element* parent = candidate.parentOrShadowHostElement();
  const ComputedStyle* style = candidate.computedStyle();
  if (!style)
    return false;
  if (!style->isSharable())
    return false;
  if (!parent)
    return false;
  if (element().parentOrShadowHostElement()->computedStyle() !=
      parent->computedStyle())
    return false;
  if (candidate.tagQName() != element().tagQName())
    return false;
  if (candidate.inlineStyle())
    return false;
  if (candidate.needsStyleRecalc())
    return false;
  if (candidate.isSVGElement() &&
      toSVGElement(candidate).animatedSMILStyleProperties())
    return false;
  if (candidate.isLink() != element().isLink())
    return false;
  if (candidate.shadowPseudoId() != element().shadowPseudoId())
    return false;
  if (!sharingCandidateHasIdenticalStyleAffectingAttributes(candidate))
    return false;
  if (candidate.additionalPresentationAttributeStyle() !=
      element().additionalPresentationAttributeStyle())
    return false;
  if (candidate.hasID() &&
      m_features.hasSelectorForId(candidate.idForStyleResolution()))
    return false;
  if (!sharingCandidateCanShareHostStyles(candidate))
    return false;
  // For Shadow DOM V1
  if (!sharingCandidateAssignedToSameSlot(candidate))
    return false;
  // For Shadow DOM V0
  if (!sharingCandidateDistributedToSameInsertionPoint(candidate))
    return false;
  if (candidate.isInTopLayer() != element().isInTopLayer())
    return false;

  bool isControl = candidate.isFormControlElement();
  ASSERT(isControl == element().isFormControlElement());
  if (isControl && !canShareStyleWithControl(candidate))
    return false;

  if (isHTMLOptionElement(candidate) && isHTMLOptionElement(element()) &&
      (toHTMLOptionElement(candidate).selected() !=
           toHTMLOptionElement(element()).selected() ||
       toHTMLOptionElement(candidate).spatialNavigationFocused() !=
           toHTMLOptionElement(element()).spatialNavigationFocused()))
    return false;

  // FIXME: This line is surprisingly hot, we may wish to inline
  // hasDirectionAuto into StyleResolver.
  if (candidate.isHTMLElement() && toHTMLElement(candidate).hasDirectionAuto())
    return false;

  if (candidate.isLink() && m_context.elementLinkState() != style->insideLink())
    return false;

  if (candidate.isUnresolvedV0CustomElement() !=
      element().isUnresolvedV0CustomElement())
    return false;

  if (element().parentOrShadowHostElement() != parent) {
    if (!parent->isStyledElement())
      return false;
    if (parent->inlineStyle())
      return false;
    if (parent->isSVGElement() &&
        toSVGElement(parent)->animatedSMILStyleProperties())
      return false;
    if (parent->hasID() &&
        m_features.hasSelectorForId(parent->idForStyleResolution()))
      return false;
    if (!parent->childrenSupportStyleSharing())
      return false;
  }

  ShadowRoot* root1 = element().containingShadowRoot();
  ShadowRoot* root2 = candidate.containingShadowRoot();
  if (root1 && root2 && root1->type() != root2->type())
    return false;

  if (document().containsValidityStyleRules()) {
    if (candidate.isValidElement() != element().isValidElement())
      return false;
  }

  return true;
}

bool SharedStyleFinder::documentContainsValidCandidate() const {
  for (Element& element :
       ElementTraversal::startsAt(document().documentElement())) {
    if (element.supportsStyleSharing() && canShareStyleWithElement(element))
      return true;
  }
  return false;
}

inline Element* SharedStyleFinder::findElementForStyleSharing() const {
  StyleSharingList& styleSharingList = m_styleResolver->styleSharingList();
  for (StyleSharingList::iterator it = styleSharingList.begin();
       it != styleSharingList.end(); ++it) {
    Element& candidate = **it;
    if (!canShareStyleWithElement(candidate))
      continue;
    if (it != styleSharingList.begin()) {
      // Move the element to the front of the LRU
      styleSharingList.remove(it);
      styleSharingList.prepend(&candidate);
    }
    return &candidate;
  }
  m_styleResolver->addToStyleSharingList(element());
  return nullptr;
}

bool SharedStyleFinder::matchesRuleSet(RuleSet* ruleSet) {
  if (!ruleSet)
    return false;
  ElementRuleCollector collector(m_context, m_styleResolver->selectorFilter());
  return collector.hasAnyMatchingRules(ruleSet);
}

ComputedStyle* SharedStyleFinder::findSharedStyle() {
  INCREMENT_STYLE_STATS_COUNTER(document().styleEngine(), sharedStyleLookups,
                                1);

  if (!element().supportsStyleSharing())
    return nullptr;

  // Cache whether context.element() is affected by any known class selectors.
  m_elementAffectedByClassRules =
      element().hasClass() && classNamesAffectedByRules(element().classNames());

  Element* shareElement = findElementForStyleSharing();

  if (!shareElement) {
    if (document().styleEngine().stats() &&
        document().styleEngine().stats()->allCountersEnabled() &&
        documentContainsValidCandidate())
      INCREMENT_STYLE_STATS_COUNTER(document().styleEngine(), sharedStyleMissed,
                                    1);
    return nullptr;
  }

  INCREMENT_STYLE_STATS_COUNTER(document().styleEngine(), sharedStyleFound, 1);

  if (matchesRuleSet(m_siblingRuleSet)) {
    INCREMENT_STYLE_STATS_COUNTER(document().styleEngine(),
                                  sharedStyleRejectedBySiblingRules, 1);
    return nullptr;
  }

  if (matchesRuleSet(m_uncommonAttributeRuleSet)) {
    INCREMENT_STYLE_STATS_COUNTER(document().styleEngine(),
                                  sharedStyleRejectedByUncommonAttributeRules,
                                  1);
    return nullptr;
  }

  // Tracking child index requires unique style for each node. This may get set
  // by the sibling rule match above.
  if (!element().parentElementOrShadowRoot()->childrenSupportStyleSharing()) {
    INCREMENT_STYLE_STATS_COUNTER(document().styleEngine(),
                                  sharedStyleRejectedByParent, 1);
    return nullptr;
  }

  return shareElement->mutableComputedStyle();
}

}  // namespace blink