| 1 | /* |
| 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-2016 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 | * |
| 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. |
| 16 | * |
| 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. |
| 21 | * |
| 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. |
| 26 | */ |
| 27 | |
| 28 | #pragma once |
| 29 | |
| 30 | #include "CSSSelector.h" |
| 31 | #include "Element.h" |
| 32 | #include "StyleRelations.h" |
| 33 | |
| 34 | namespace WebCore { |
| 35 | |
| 36 | class CSSSelector; |
| 37 | class Element; |
| 38 | class RenderScrollbar; |
| 39 | class RenderStyle; |
| 40 | |
| 41 | class SelectorChecker { |
| 42 | WTF_MAKE_NONCOPYABLE(SelectorChecker); |
| 43 | enum class Match { SelectorMatches, SelectorFailsLocally, SelectorFailsAllSiblings, SelectorFailsCompletely }; |
| 44 | |
| 45 | enum class MatchType { VirtualPseudoElementOnly, Element }; |
| 46 | |
| 47 | struct MatchResult { |
| 48 | Match match; |
| 49 | MatchType matchType; |
| 50 | |
| 51 | static MatchResult matches(MatchType matchType) |
| 52 | { |
| 53 | return { Match::SelectorMatches, matchType }; |
| 54 | } |
| 55 | |
| 56 | static MatchResult updateWithMatchType(MatchResult result, MatchType matchType) |
| 57 | { |
| 58 | if (matchType == MatchType::VirtualPseudoElementOnly) |
| 59 | result.matchType = MatchType::VirtualPseudoElementOnly; |
| 60 | return result; |
| 61 | } |
| 62 | |
| 63 | static MatchResult fails(Match match) |
| 64 | { |
| 65 | return { match, MatchType::Element }; |
| 66 | } |
| 67 | }; |
| 68 | |
| 69 | public: |
| 70 | enum class Mode : unsigned char { |
| 71 | ResolvingStyle = 0, CollectingRules, CollectingRulesIgnoringVirtualPseudoElements, QueryingRules |
| 72 | }; |
| 73 | |
| 74 | SelectorChecker(Document&); |
| 75 | |
| 76 | struct CheckingContext { |
| 77 | CheckingContext(SelectorChecker::Mode resolvingMode) |
| 78 | : resolvingMode(resolvingMode) |
| 79 | { } |
| 80 | |
| 81 | const SelectorChecker::Mode resolvingMode; |
| 82 | PseudoId pseudoId { PseudoId::None }; |
| 83 | RenderScrollbar* scrollbar { nullptr }; |
| 84 | ScrollbarPart scrollbarPart { NoPart }; |
| 85 | const ContainerNode* scope { nullptr }; |
| 86 | bool isMatchingHostPseudoClass { false }; |
| 87 | |
| 88 | // FIXME: It would be nicer to have a separate object for return values. This requires some more work in the selector compiler. |
| 89 | Style::Relations styleRelations; |
| 90 | PseudoIdSet pseudoIDSet; |
| 91 | }; |
| 92 | |
| 93 | bool match(const CSSSelector&, const Element&, CheckingContext&, unsigned& specificity) const; |
| 94 | |
| 95 | bool matchHostPseudoClass(const CSSSelector&, const Element&, CheckingContext&, unsigned& specificity) const; |
| 96 | |
| 97 | static bool isCommonPseudoClassSelector(const CSSSelector*); |
| 98 | static bool matchesFocusPseudoClass(const Element&); |
| 99 | static bool attributeSelectorMatches(const Element&, const QualifiedName&, const AtomicString& attributeValue, const CSSSelector&); |
| 100 | |
| 101 | enum LinkMatchMask { MatchDefault = 0, MatchLink = 1, MatchVisited = 2, MatchAll = MatchLink | MatchVisited }; |
| 102 | static unsigned determineLinkMatchType(const CSSSelector*); |
| 103 | |
| 104 | struct LocalContext; |
| 105 | |
| 106 | private: |
| 107 | MatchResult matchRecursively(CheckingContext&, const LocalContext&, PseudoIdSet&, unsigned& specificity) const; |
| 108 | bool checkOne(CheckingContext&, const LocalContext&, PseudoIdSet&, MatchType&, unsigned& specificity) const; |
| 109 | bool matchSelectorList(CheckingContext&, const LocalContext&, const Element&, const CSSSelectorList&, unsigned& specificity) const; |
| 110 | |
| 111 | bool checkScrollbarPseudoClass(const CheckingContext&, const Element&, const CSSSelector&) const; |
| 112 | |
| 113 | bool m_strictParsing; |
| 114 | bool m_documentIsHTML; |
| 115 | }; |
| 116 | |
| 117 | inline bool SelectorChecker::isCommonPseudoClassSelector(const CSSSelector* selector) |
| 118 | { |
| 119 | if (selector->match() != CSSSelector::PseudoClass) |
| 120 | return false; |
| 121 | CSSSelector::PseudoClassType pseudoType = selector->pseudoClassType(); |
| 122 | return pseudoType == CSSSelector::PseudoClassLink |
| 123 | || pseudoType == CSSSelector::PseudoClassAnyLink |
| 124 | || pseudoType == CSSSelector::PseudoClassAnyLinkDeprecated |
| 125 | || pseudoType == CSSSelector::PseudoClassVisited |
| 126 | || pseudoType == CSSSelector::PseudoClassFocus; |
| 127 | } |
| 128 | |
| 129 | } // namespace WebCore |
| 130 | |