2 * Copyright (C) 1999-2003 Lars Knoll (knoll@kde.org)
3 * 1999 Waldo Bastian (bastian@kde.org)
4 * Copyright (C) 2004, 2006, 2007, 2008, 2009, 2010, 2013 Apple Inc. All rights reserved.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB. If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
25 #include "core/dom/QualifiedName.h"
26 #include "core/rendering/style/RenderStyleConstants.h"
27 #include "wtf/OwnPtr.h"
28 #include "wtf/PassOwnPtr.h"
31 class CSSSelectorList;
33 // This class represents a selector for a StyleRule.
35 // CSS selector representation is somewhat complicated and subtle. A representative list of selectors is
36 // in CSSSelectorTest; run it in a debug build to see useful debugging output.
38 // ** tagHistory() and relation():
40 // Selectors are represented as a linked list of simple selectors (defined more or less according to
41 // http://www.w3.org/TR/css3-selectors/#simple-selectors-dfn). The tagHistory() method returns the next
42 // simple selector in the list. The relation() method returns the relationship of the current simple selector to
43 // the one in tagHistory(). For example, the CSS selector .a.b #c is represented as:
45 // selectorText(): .a.b #c
46 // --> (relation == Descendant)
47 // selectorText(): .a.b
48 // --> (relation == SubSelector)
51 // Note that currently a bare selector such as ".a" has a relation() of Descendant. This is a bug - instead the relation should be
54 // The order of tagHistory() varies depending on the situation.
55 // * Relations using combinators (http://www.w3.org/TR/css3-selectors/#combinators), such as descendant, sibling, etc., are parsed
56 // right-to-left (in the example above, this is why .c is earlier in the tagHistory() chain than .a.b).
57 // * SubSelector relations are parsed left-to-right in most cases (such as the .a.b example above); a counter-example is the
58 // ::content pseudo-element. Most (all?) other pseudo elements and pseudo classes are parsed left-to-right.
59 // * ShadowPseudo relations are parsed right-to-left. Example: summary::-webkit-details-marker is parsed as:
60 // selectorText(): summary::-webkit-details-marker
61 // --> (relation == ShadowPseudo)
62 // selectorText(): summary
66 // The match of the current simple selector tells us the type of selector, such as class, id, tagname, or pseudo-class.
67 // Inline comments in the Match enum give examples of when each type would occur.
69 // ** value(), attribute():
71 // value() tells you the value of the simple selector. For example, for class selectors, value() will tell you the class string,
72 // and for id selectors it will tell you the id(). See below for the special case of attribute selectors.
74 // ** Attribute selectors.
76 // Attribute selectors return the attribute name in the attribute() method. The value() method returns the value matched against
77 // in case of selectors like [attr="value"].
79 // ** isCustomPseudoElement():
81 // It appears this is used only for pseudo elements that appear in user-agent shadow DOM. They are not exposed to author-created
85 WTF_MAKE_FAST_ALLOCATED;
88 CSSSelector(const CSSSelector&);
89 explicit CSSSelector(const QualifiedName&, bool tagIsForNamespaceRule = false);
94 * Re-create selector text from selector's data
96 String selectorText(const String& = "") const;
98 // checks if the 2 selectors (including sub selectors) agree.
99 bool operator==(const CSSSelector&) const;
101 // tag == -1 means apply to all elements (Selector = *)
103 // http://www.w3.org/TR/css3-selectors/#specificity
104 // We use 256 as the base of the specificity number system.
105 unsigned specificity() const;
107 /* how the attribute value has to match.... Default is Exact */
112 Class, // example: .class
113 PseudoClass, // Example: :nth-child(2)
114 PseudoElement, // Example: ::first-line
115 PagePseudoClass, // ??
116 Exact, // Example: E[foo="bar"]
117 Set, // Example: E[foo]
118 Hyphen, // Example: E[foo|="bar"]
119 List, // Example: E[foo~="bar"]
120 Contain, // css3: E[foo*="bar"]
121 Begin, // css3: E[foo^="bar"]
122 End, // css3: E[foo$="bar"]
123 FirstAttributeSelectorMatch = Exact,
127 Descendant = 0, // "Space" combinator
128 Child, // > combinator
129 DirectAdjacent, // + combinator
130 IndirectAdjacent, // ~ combinator
131 SubSelector, // "No space" combinator
132 ShadowPseudo, // Special case of shadow DOM pseudo elements / shadow pseudo element
133 ShadowDeep // /deep/ combinator
184 PseudoScrollbarButton,
185 PseudoScrollbarCorner,
186 PseudoScrollbarForward,
187 PseudoScrollbarThumb,
188 PseudoScrollbarTrack,
189 PseudoScrollbarTrackPiece,
190 PseudoWindowInactive,
206 PseudoFullScreenDocument,
207 PseudoFullScreenAncestor,
210 PseudoUserAgentCustomElement,
211 PseudoWebKitCustomElement,
220 PseudoSpatialNavigationFocus,
225 TopLeftCornerMarginBox,
229 TopRightCornerMarginBox,
230 BottomLeftCornerMarginBox,
232 BottomCenterMarginBox,
233 BottomRightMarginBox,
234 BottomRightCornerMarginBox,
239 RightMiddleMarginBox,
240 RightBottomMarginBox,
243 enum AttributeMatchType {
248 PseudoType pseudoType() const
250 if (m_pseudoType == PseudoNotParsed)
252 return static_cast<PseudoType>(m_pseudoType);
255 static PseudoType parsePseudoType(const AtomicString&, bool hasArguments);
256 static PseudoId pseudoId(PseudoType);
258 // Selectors are kept in an array by CSSSelectorList. The next component of the selector is
259 // the next item in the array.
260 const CSSSelector* tagHistory() const { return m_isLastInTagHistory ? 0 : const_cast<CSSSelector*>(this + 1); }
262 const QualifiedName& tagQName() const;
263 const AtomicString& value() const;
265 // WARNING: Use of QualifiedName by attribute() is a lie.
266 // attribute() will return a QualifiedName with prefix and namespaceURI
267 // set to starAtom to mean "matches any namespace". Be very careful
268 // how you use the returned QualifiedName.
269 // http://www.w3.org/TR/css3-selectors/#attrnmsp
270 const QualifiedName& attribute() const;
271 AttributeMatchType attributeMatchType() const;
272 // Returns the argument of a parameterized selector. For example, nth-child(2) would have an argument of 2.
273 const AtomicString& argument() const { return m_hasRareData ? m_data.m_rareData->m_argument : nullAtom; }
274 const CSSSelectorList* selectorList() const { return m_hasRareData ? m_data.m_rareData->m_selectorList.get() : 0; }
278 void show(int indent) const;
281 void setValue(const AtomicString&);
282 void setAttribute(const QualifiedName&, AttributeMatchType);
283 void setArgument(const AtomicString&);
284 void setSelectorList(PassOwnPtr<CSSSelectorList>);
285 void setMatchUserAgentOnly();
287 bool parseNth() const;
288 bool matchNth(int count) const;
290 bool matchesPseudoElement() const;
291 bool isCustomPseudoElement() const;
292 bool isDirectAdjacentSelector() const { return m_relation == DirectAdjacent; }
293 bool isSiblingSelector() const;
294 bool isAttributeSelector() const;
295 bool isContentPseudoElement() const;
296 bool isShadowPseudoElement() const;
297 bool isHostPseudoClass() const;
299 // FIXME: selectors with no tagHistory() get a relation() of Descendant (and sometimes even SubSelector). It should instead be
301 Relation relation() const { return static_cast<Relation>(m_relation); }
302 void setRelation(Relation relation)
304 m_relation = relation;
305 ASSERT(static_cast<Relation>(m_relation) == relation); // using a bitfield.
308 Match match() const { return static_cast<Match>(m_match); }
309 void setMatch(Match match)
312 ASSERT(static_cast<Match>(m_match) == match); // using a bitfield.
315 bool isLastInSelectorList() const { return m_isLastInSelectorList; }
316 void setLastInSelectorList() { m_isLastInSelectorList = true; }
317 bool isLastInTagHistory() const { return m_isLastInTagHistory; }
318 void setNotLastInTagHistory() { m_isLastInTagHistory = false; }
320 // http://dev.w3.org/csswg/selectors4/#compound
321 bool isCompound() const;
323 bool isForPage() const { return m_isForPage; }
324 void setForPage() { m_isForPage = true; }
326 bool relationIsAffectedByPseudoContent() const { return m_relationIsAffectedByPseudoContent; }
327 void setRelationIsAffectedByPseudoContent() { m_relationIsAffectedByPseudoContent = true; }
330 unsigned m_relation : 3; // enum Relation
331 mutable unsigned m_match : 4; // enum Match
332 mutable unsigned m_pseudoType : 8; // PseudoType
333 mutable unsigned m_parsedNth : 1; // Used for :nth-*
334 unsigned m_isLastInSelectorList : 1;
335 unsigned m_isLastInTagHistory : 1;
336 unsigned m_hasRareData : 1;
337 unsigned m_isForPage : 1;
338 unsigned m_tagIsForNamespaceRule : 1;
339 unsigned m_relationIsAffectedByPseudoContent : 1;
341 unsigned specificityForOneSelector() const;
342 unsigned specificityForPage() const;
343 void extractPseudoType() const;
346 CSSSelector& operator=(const CSSSelector&);
348 struct RareData : public RefCounted<RareData> {
349 static PassRefPtr<RareData> create(const AtomicString& value) { return adoptRef(new RareData(value)); }
353 bool matchNth(int count);
354 int nthAValue() const { return m_bits.m_nth.m_a; }
355 void setNthAValue(int nthA) { m_bits.m_nth.m_a = nthA; }
356 int nthBValue() const { return m_bits.m_nth.m_b; }
357 void setNthBValue(int nthB) { m_bits.m_nth.m_b = nthB; }
359 AtomicString m_value;
362 int m_a; // Used for :nth-*
363 int m_b; // Used for :nth-*
365 AttributeMatchType m_attributeMatchType; // used for attribute selector (with value)
367 QualifiedName m_attribute; // used for attribute selector
368 AtomicString m_argument; // Used for :contains, :lang, :nth-*
369 OwnPtr<CSSSelectorList> m_selectorList; // Used for :-webkit-any and :not
372 RareData(const AtomicString& value);
374 void createRareData();
377 DataUnion() : m_value(0) { }
379 QualifiedName::QualifiedNameImpl* m_tagQName;
380 RareData* m_rareData;
384 inline const QualifiedName& CSSSelector::attribute() const
386 ASSERT(isAttributeSelector());
387 ASSERT(m_hasRareData);
388 return m_data.m_rareData->m_attribute;
391 inline CSSSelector::AttributeMatchType CSSSelector::attributeMatchType() const
393 ASSERT(isAttributeSelector());
394 ASSERT(m_hasRareData);
395 return m_data.m_rareData->m_bits.m_attributeMatchType;
398 inline bool CSSSelector::matchesPseudoElement() const
400 if (m_pseudoType == PseudoUnknown)
402 return m_match == PseudoElement;
405 inline bool CSSSelector::isCustomPseudoElement() const
407 return m_match == PseudoElement && (m_pseudoType == PseudoUserAgentCustomElement || m_pseudoType == PseudoWebKitCustomElement);
410 inline bool CSSSelector::isHostPseudoClass() const
412 return m_match == PseudoClass && (m_pseudoType == PseudoHost || m_pseudoType == PseudoHostContext);
415 inline bool CSSSelector::isSiblingSelector() const
417 PseudoType type = pseudoType();
418 return m_relation == DirectAdjacent
419 || m_relation == IndirectAdjacent
420 || type == PseudoEmpty
421 || type == PseudoFirstChild
422 || type == PseudoFirstOfType
423 || type == PseudoLastChild
424 || type == PseudoLastOfType
425 || type == PseudoOnlyChild
426 || type == PseudoOnlyOfType
427 || type == PseudoNthChild
428 || type == PseudoNthOfType
429 || type == PseudoNthLastChild
430 || type == PseudoNthLastOfType;
433 inline bool CSSSelector::isAttributeSelector() const
435 return m_match >= FirstAttributeSelectorMatch;
438 inline bool CSSSelector::isContentPseudoElement() const
440 return m_match == PseudoElement && pseudoType() == PseudoContent;
443 inline bool CSSSelector::isShadowPseudoElement() const
445 return m_match == PseudoElement && pseudoType() == PseudoShadow;
448 inline void CSSSelector::setValue(const AtomicString& value)
450 ASSERT(m_match != Tag);
451 ASSERT(m_pseudoType == PseudoNotParsed);
452 // Need to do ref counting manually for the union.
454 m_data.m_rareData->m_value = value;
458 m_data.m_value->deref();
459 m_data.m_value = value.impl();
460 m_data.m_value->ref();
463 inline CSSSelector::CSSSelector()
464 : m_relation(Descendant)
466 , m_pseudoType(PseudoNotParsed)
468 , m_isLastInSelectorList(false)
469 , m_isLastInTagHistory(true)
470 , m_hasRareData(false)
472 , m_tagIsForNamespaceRule(false)
473 , m_relationIsAffectedByPseudoContent(false)
477 inline CSSSelector::CSSSelector(const QualifiedName& tagQName, bool tagIsForNamespaceRule)
478 : m_relation(Descendant)
480 , m_pseudoType(PseudoNotParsed)
482 , m_isLastInSelectorList(false)
483 , m_isLastInTagHistory(true)
484 , m_hasRareData(false)
486 , m_tagIsForNamespaceRule(tagIsForNamespaceRule)
487 , m_relationIsAffectedByPseudoContent(false)
489 m_data.m_tagQName = tagQName.impl();
490 m_data.m_tagQName->ref();
493 inline CSSSelector::CSSSelector(const CSSSelector& o)
494 : m_relation(o.m_relation)
496 , m_pseudoType(o.m_pseudoType)
497 , m_parsedNth(o.m_parsedNth)
498 , m_isLastInSelectorList(o.m_isLastInSelectorList)
499 , m_isLastInTagHistory(o.m_isLastInTagHistory)
500 , m_hasRareData(o.m_hasRareData)
501 , m_isForPage(o.m_isForPage)
502 , m_tagIsForNamespaceRule(o.m_tagIsForNamespaceRule)
503 , m_relationIsAffectedByPseudoContent(o.m_relationIsAffectedByPseudoContent)
505 if (o.m_match == Tag) {
506 m_data.m_tagQName = o.m_data.m_tagQName;
507 m_data.m_tagQName->ref();
508 } else if (o.m_hasRareData) {
509 m_data.m_rareData = o.m_data.m_rareData;
510 m_data.m_rareData->ref();
511 } else if (o.m_data.m_value) {
512 m_data.m_value = o.m_data.m_value;
513 m_data.m_value->ref();
517 inline CSSSelector::~CSSSelector()
520 m_data.m_tagQName->deref();
521 else if (m_hasRareData)
522 m_data.m_rareData->deref();
523 else if (m_data.m_value)
524 m_data.m_value->deref();
527 inline const QualifiedName& CSSSelector::tagQName() const
529 ASSERT(m_match == Tag);
530 return *reinterpret_cast<const QualifiedName*>(&m_data.m_tagQName);
533 inline const AtomicString& CSSSelector::value() const
535 ASSERT(m_match != Tag);
537 return m_data.m_rareData->m_value;
538 // AtomicString is really just a StringImpl* so the cast below is safe.
539 // FIXME: Perhaps call sites could be changed to accept StringImpl?
540 return *reinterpret_cast<const AtomicString*>(&m_data.m_value);
545 #endif // CSSSelector_h