2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 * (C) 2004-2005 Allan Sandfeld Jensen (kde@carewolf.com)
4 * Copyright (C) 2006, 2007 Nicholas Shanks (webkit@nickshanks.com)
5 * Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 Apple Inc. All rights reserved.
6 * Copyright (C) 2007 Alexey Proskuryakov <ap@webkit.org>
7 * Copyright (C) 2007, 2008 Eric Seidel <eric@webkit.org>
8 * Copyright (C) 2008, 2009 Torch Mobile Inc. All rights reserved. (http://www.torchmobile.com/)
9 * Copyright (c) 2011, Code Aurora Forum. All rights reserved.
10 * Copyright (C) Research In Motion Limited 2011. All rights reserved.
11 * Copyright (C) 2013 Google Inc. All rights reserved.
13 * This library is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU Library General Public
15 * License as published by the Free Software Foundation; either
16 * version 2 of the License, or (at your option) any later version.
18 * This library is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * Library General Public License for more details.
23 * You should have received a copy of the GNU Library General Public License
24 * along with this library; see the file COPYING.LIB. If not, write to
25 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26 * Boston, MA 02110-1301, USA.
30 #include "core/css/resolver/SharedStyleFinder.h"
32 #include "core/HTMLNames.h"
33 #include "core/XMLNames.h"
34 #include "core/css/resolver/StyleResolver.h"
35 #include "core/css/resolver/StyleResolverStats.h"
36 #include "core/dom/ContainerNode.h"
37 #include "core/dom/Document.h"
38 #include "core/dom/ElementTraversal.h"
39 #include "core/dom/FullscreenElementStack.h"
40 #include "core/dom/Node.h"
41 #include "core/dom/NodeRenderStyle.h"
42 #include "core/dom/QualifiedName.h"
43 #include "core/dom/SpaceSplitString.h"
44 #include "core/dom/shadow/ElementShadow.h"
45 #include "core/dom/shadow/InsertionPoint.h"
46 #include "core/html/HTMLElement.h"
47 #include "core/html/HTMLInputElement.h"
48 #include "core/html/HTMLOptGroupElement.h"
49 #include "core/html/HTMLOptionElement.h"
50 #include "core/rendering/style/RenderStyle.h"
51 #include "core/svg/SVGElement.h"
52 #include "wtf/HashSet.h"
53 #include "wtf/text/AtomicString.h"
57 using namespace HTMLNames;
59 bool SharedStyleFinder::canShareStyleWithControl(Element& candidate) const
61 if (!isHTMLInputElement(candidate) || !isHTMLInputElement(element()))
64 HTMLInputElement& candidateInput = toHTMLInputElement(candidate);
65 HTMLInputElement& thisInput = toHTMLInputElement(element());
67 if (candidateInput.isAutofilled() != thisInput.isAutofilled())
69 if (candidateInput.shouldAppearChecked() != thisInput.shouldAppearChecked())
71 if (candidateInput.shouldAppearIndeterminate() != thisInput.shouldAppearIndeterminate())
73 if (candidateInput.isRequired() != thisInput.isRequired())
76 if (candidate.isDisabledFormControl() != element().isDisabledFormControl())
79 if (candidate.isDefaultButtonForForm() != element().isDefaultButtonForForm())
82 if (document().containsValidityStyleRules()) {
83 bool willValidate = candidate.willValidate();
85 if (willValidate != element().willValidate())
88 if (willValidate && (candidate.isValidFormControlElement() != element().isValidFormControlElement()))
91 if (candidate.isInRange() != element().isInRange())
94 if (candidate.isOutOfRange() != element().isOutOfRange())
101 bool SharedStyleFinder::classNamesAffectedByRules(const SpaceSplitString& classNames) const
103 unsigned count = classNames.size();
104 for (unsigned i = 0; i < count; ++i) {
105 if (m_features.hasSelectorForClass(classNames[i]))
111 static inline const AtomicString& typeAttributeValue(const Element& element)
113 // type is animatable in SVG so we need to go down the slow path here.
114 return element.isSVGElement() ? element.getAttribute(typeAttr) : element.fastGetAttribute(typeAttr);
117 bool SharedStyleFinder::sharingCandidateHasIdenticalStyleAffectingAttributes(Element& candidate) const
119 if (element().sharesSameElementData(candidate))
121 if (element().fastGetAttribute(XMLNames::langAttr) != candidate.fastGetAttribute(XMLNames::langAttr))
123 if (element().fastGetAttribute(langAttr) != candidate.fastGetAttribute(langAttr))
126 // These two checks must be here since RuleSet has a special case to allow style sharing between elements
127 // with type and readonly attributes whereas other attribute selectors prevent sharing.
128 if (typeAttributeValue(element()) != typeAttributeValue(candidate))
130 if (element().fastGetAttribute(readonlyAttr) != candidate.fastGetAttribute(readonlyAttr))
133 if (!m_elementAffectedByClassRules) {
134 if (candidate.hasClass() && classNamesAffectedByRules(candidate.classNames()))
136 } else if (candidate.hasClass()) {
137 // SVG elements require a (slow!) getAttribute comparision because "class" is an animatable attribute for SVG.
138 if (element().isSVGElement()) {
139 if (element().getAttribute(classAttr) != candidate.getAttribute(classAttr))
141 } else if (element().classNames() != candidate.classNames()) {
148 if (element().presentationAttributeStyle() != candidate.presentationAttributeStyle())
151 // FIXME: Consider removing this, it's unlikely we'll have so many progress elements
152 // that sharing the style makes sense. Instead we should just not support style sharing
154 if (isHTMLProgressElement(element())) {
155 if (element().shouldAppearIndeterminate() != candidate.shouldAppearIndeterminate())
159 if (isHTMLOptGroupElement(element()) || isHTMLOptionElement(element())) {
160 if (element().isDisabledFormControl() != candidate.isDisabledFormControl())
162 if (isHTMLOptionElement(element()) && toHTMLOptionElement(element()).selected() != toHTMLOptionElement(candidate).selected())
169 bool SharedStyleFinder::sharingCandidateCanShareHostStyles(Element& candidate) const
171 const ElementShadow* elementShadow = element().shadow();
172 const ElementShadow* candidateShadow = candidate.shadow();
174 if (!elementShadow && !candidateShadow)
177 if (static_cast<bool>(elementShadow) != static_cast<bool>(candidateShadow))
180 return elementShadow->hasSameStyles(candidateShadow);
183 bool SharedStyleFinder::sharingCandidateDistributedToSameInsertionPoint(Element& candidate) const
185 WillBeHeapVector<RawPtrWillBeMember<InsertionPoint>, 8> insertionPoints, candidateInsertionPoints;
186 collectDestinationInsertionPoints(element(), insertionPoints);
187 collectDestinationInsertionPoints(candidate, candidateInsertionPoints);
188 if (insertionPoints.size() != candidateInsertionPoints.size())
190 for (size_t i = 0; i < insertionPoints.size(); ++i) {
191 if (insertionPoints[i] != candidateInsertionPoints[i])
197 bool SharedStyleFinder::canShareStyleWithElement(Element& candidate) const
199 if (element() == candidate)
201 Element* parent = candidate.parentOrShadowHostElement();
202 RenderStyle* style = candidate.renderStyle();
205 if (!style->isSharable())
209 if (element().parentOrShadowHostElement()->renderStyle() != parent->renderStyle())
211 if (candidate.tagQName() != element().tagQName())
213 if (candidate.inlineStyle())
215 if (candidate.needsStyleRecalc())
217 if (candidate.isSVGElement() && toSVGElement(candidate).animatedSMILStyleProperties())
219 if (candidate.isLink() != element().isLink())
221 if (candidate.shadowPseudoId() != element().shadowPseudoId())
223 if (!sharingCandidateHasIdenticalStyleAffectingAttributes(candidate))
225 if (candidate.additionalPresentationAttributeStyle() != element().additionalPresentationAttributeStyle())
227 if (candidate.hasID() && m_features.hasSelectorForId(candidate.idForStyleResolution()))
229 if (!sharingCandidateCanShareHostStyles(candidate))
231 if (!sharingCandidateDistributedToSameInsertionPoint(candidate))
233 if (candidate.isInTopLayer() != element().isInTopLayer())
236 bool isControl = candidate.isFormControlElement();
237 ASSERT(isControl == element().isFormControlElement());
238 if (isControl && !canShareStyleWithControl(candidate))
241 if (isHTMLOptionElement(candidate) && isHTMLOptionElement(element())
242 && (toHTMLOptionElement(candidate).selected() != toHTMLOptionElement(element()).selected()
243 || toHTMLOptionElement(candidate).spatialNavigationFocused() != toHTMLOptionElement(element()).spatialNavigationFocused()))
246 // FIXME: This line is surprisingly hot, we may wish to inline hasDirectionAuto into StyleResolver.
247 if (candidate.isHTMLElement() && toHTMLElement(candidate).hasDirectionAuto())
250 if (candidate.isLink() && m_context.elementLinkState() != style->insideLink())
253 if (candidate.isUnresolvedCustomElement() != element().isUnresolvedCustomElement())
256 if (element().parentOrShadowHostElement() != parent) {
257 if (!parent->isStyledElement())
259 if (parent->inlineStyle())
261 if (parent->isSVGElement() && toSVGElement(parent)->animatedSMILStyleProperties())
263 if (parent->hasID() && m_features.hasSelectorForId(parent->idForStyleResolution()))
265 if (!parent->childrenSupportStyleSharing())
272 bool SharedStyleFinder::documentContainsValidCandidate() const
274 for (Element* element = document().documentElement(); element; element = ElementTraversal::next(*element)) {
275 if (element->supportsStyleSharing() && canShareStyleWithElement(*element))
281 inline Element* SharedStyleFinder::findElementForStyleSharing() const
283 StyleSharingList& styleSharingList = m_styleResolver.styleSharingList();
284 for (StyleSharingList::iterator it = styleSharingList.begin(); it != styleSharingList.end(); ++it) {
285 Element& candidate = **it;
286 if (!canShareStyleWithElement(candidate))
288 if (it != styleSharingList.begin()) {
289 // Move the element to the front of the LRU
290 styleSharingList.remove(it);
291 styleSharingList.prepend(&candidate);
295 m_styleResolver.addToStyleSharingList(element());
299 bool SharedStyleFinder::matchesRuleSet(RuleSet* ruleSet)
303 ElementRuleCollector collector(m_context, m_styleResolver.selectorFilter());
304 return collector.hasAnyMatchingRules(ruleSet);
307 RenderStyle* SharedStyleFinder::findSharedStyle()
309 INCREMENT_STYLE_STATS_COUNTER(m_styleResolver, sharedStyleLookups);
311 if (!element().supportsStyleSharing())
314 // Cache whether context.element() is affected by any known class selectors.
315 m_elementAffectedByClassRules = element().hasClass() && classNamesAffectedByRules(element().classNames());
317 Element* shareElement = findElementForStyleSharing();
320 if (m_styleResolver.stats() && m_styleResolver.stats()->printMissedCandidateCount && documentContainsValidCandidate())
321 INCREMENT_STYLE_STATS_COUNTER(m_styleResolver, sharedStyleMissed);
325 INCREMENT_STYLE_STATS_COUNTER(m_styleResolver, sharedStyleFound);
327 if (matchesRuleSet(m_siblingRuleSet)) {
328 INCREMENT_STYLE_STATS_COUNTER(m_styleResolver, sharedStyleRejectedBySiblingRules);
332 if (matchesRuleSet(m_uncommonAttributeRuleSet)) {
333 INCREMENT_STYLE_STATS_COUNTER(m_styleResolver, sharedStyleRejectedByUncommonAttributeRules);
337 // Tracking child index requires unique style for each node. This may get set by the sibling rule match above.
338 if (!element().parentElementOrShadowRoot()->childrenSupportStyleSharing()) {
339 INCREMENT_STYLE_STATS_COUNTER(m_styleResolver, sharedStyleRejectedByParent);
343 return shareElement->renderStyle();