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.
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Library General Public
14 * License as published by the Free Software Foundation; either
15 * version 2 of the License, or (at your option) any later version.
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public License
23 * along with this library; see the file COPYING.LIB. If not, write to
24 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 * Boston, MA 02110-1301, USA.
28 #ifndef SelectorChecker_h
29 #define SelectorChecker_h
31 #include "core/css/CSSSelector.h"
32 #include "core/dom/Element.h"
33 #include "platform/scroll/ScrollTypes.h"
40 class RenderScrollbar;
43 class SelectorChecker {
44 WTF_MAKE_NONCOPYABLE(SelectorChecker);
46 enum Match { SelectorMatches, SelectorFailsLocally, SelectorFailsAllSiblings, SelectorFailsCompletely };
47 enum VisitedMatchType { VisitedMatchDisabled, VisitedMatchEnabled };
48 enum Mode { ResolvingStyle = 0, CollectingStyleRules, CollectingCSSRules, QueryingRules, SharingRules };
49 explicit SelectorChecker(Document&, Mode);
51 struct SelectorCheckingContext {
54 // Initial selector constructor
55 SelectorCheckingContext(const CSSSelector& selector, Element* element, VisitedMatchType visitedMatchType)
58 , previousElement(nullptr)
60 , visitedMatchType(visitedMatchType)
64 , scrollbarPart(NoPart)
65 , isSubSelector(false)
66 , hasScrollbarPseudo(false)
67 , hasSelectionPseudo(false)
69 , scopeContainsLastMatchedElement(false)
70 , treatShadowHostAsNormalScope(false)
74 const CSSSelector* selector;
75 RawPtrWillBeMember<Element> element;
76 RawPtrWillBeMember<Element> previousElement;
77 RawPtrWillBeMember<const ContainerNode> scope;
78 VisitedMatchType visitedMatchType;
80 RenderStyle* elementStyle;
81 RawPtrWillBeMember<RenderScrollbar> scrollbar;
82 ScrollbarPart scrollbarPart;
83 unsigned isSubSelector : 1;
84 unsigned hasScrollbarPseudo : 1;
85 unsigned hasSelectionPseudo : 1;
86 unsigned isUARule : 1;
87 unsigned scopeContainsLastMatchedElement : 1;
88 unsigned treatShadowHostAsNormalScope : 1;
93 : dynamicPseudo(NOPSEUDO)
96 PseudoId dynamicPseudo;
100 template<typename SiblingTraversalStrategy>
101 Match match(const SelectorCheckingContext&, const SiblingTraversalStrategy&, MatchResult* = 0) const;
103 template<typename SiblingTraversalStrategy>
104 bool checkOne(const SelectorCheckingContext&, const SiblingTraversalStrategy&, unsigned* specificity = 0) const;
106 bool strictParsing() const { return m_strictParsing; }
108 Mode mode() const { return m_mode; }
110 static bool tagMatches(const Element&, const QualifiedName&);
111 static bool isCommonPseudoClassSelector(const CSSSelector&);
112 static bool matchesFocusPseudoClass(const Element&);
113 static bool matchesSpatialNavigationFocusPseudoClass(const Element&);
114 static bool matchesListBoxPseudoClass(const Element&);
115 static bool checkExactAttribute(const Element&, const QualifiedName& selectorAttributeName, const StringImpl* value);
117 enum LinkMatchMask { MatchLink = 1, MatchVisited = 2, MatchAll = MatchLink | MatchVisited };
118 static unsigned determineLinkMatchType(const CSSSelector&);
120 static bool isHostInItsShadowTree(const Element&, const ContainerNode* scope);
123 template<typename SiblingTraversalStrategy>
124 Match matchForSubSelector(const SelectorCheckingContext&, const SiblingTraversalStrategy&, MatchResult*) const;
125 template<typename SiblingTraversalStrategy>
126 Match matchForRelation(const SelectorCheckingContext&, const SiblingTraversalStrategy&, MatchResult*) const;
127 template<typename SiblingTraversalStrategy>
128 Match matchForShadowDistributed(const Element*, const SiblingTraversalStrategy&, SelectorCheckingContext& nextContext, MatchResult* = 0) const;
129 template<typename SiblingTraversalStrategy>
130 Match matchForPseudoShadow(const ContainerNode*, const SelectorCheckingContext&, const SiblingTraversalStrategy&, MatchResult*) const;
131 template<typename SiblingTraversalStrategy>
132 bool checkPseudoClass(const SelectorCheckingContext&, const SiblingTraversalStrategy&, unsigned* specificity) const;
133 template<typename SiblingTraversalStrategy>
134 bool checkPseudoElement(const SelectorCheckingContext&, const SiblingTraversalStrategy&) const;
136 bool checkScrollbarPseudoClass(const SelectorCheckingContext&, Document*, const CSSSelector&) const;
137 template<typename SiblingTraversalStrategy>
138 bool checkPseudoHost(const SelectorCheckingContext&, const SiblingTraversalStrategy&, unsigned*) const;
140 static bool isFrameFocused(const Element&);
142 bool m_strictParsing;
146 inline bool SelectorChecker::isCommonPseudoClassSelector(const CSSSelector& selector)
148 if (selector.match() != CSSSelector::PseudoClass)
150 CSSSelector::PseudoType pseudoType = selector.pseudoType();
151 return pseudoType == CSSSelector::PseudoLink
152 || pseudoType == CSSSelector::PseudoAnyLink
153 || pseudoType == CSSSelector::PseudoVisited
154 || pseudoType == CSSSelector::PseudoFocus;
157 inline bool SelectorChecker::tagMatches(const Element& element, const QualifiedName& tagQName)
159 if (tagQName == anyQName())
161 const AtomicString& localName = tagQName.localName();
162 if (localName != starAtom && localName != element.localName())
164 const AtomicString& namespaceURI = tagQName.namespaceURI();
165 return namespaceURI == starAtom || namespaceURI == element.namespaceURI();
168 inline bool SelectorChecker::checkExactAttribute(const Element& element, const QualifiedName& selectorAttributeName, const StringImpl* value)
170 for (const auto& attribute : element.attributesWithoutUpdate()) {
171 if (attribute.matches(selectorAttributeName) && (!value || attribute.value().impl() == value))
177 inline bool SelectorChecker::isHostInItsShadowTree(const Element& element, const ContainerNode* scope)
179 return scope && scope->isInShadowTree() && scope->shadowHost() == element;