Start using the new :not() and :matches() in the Web Inspector
[WebKit-https.git] / Source / WebCore / css / SelectorChecker.cpp
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, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014 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) 2014 Yusuke Suzuki <utatane.tea@gmail.com>
12  *
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.
17  *
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.
22  *
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.
27  */
28
29 #include "config.h"
30 #include "SelectorChecker.h"
31
32 #include "CSSSelector.h"
33 #include "CSSSelectorList.h"
34 #include "Document.h"
35 #include "ElementTraversal.h"
36 #include "Frame.h"
37 #include "FrameSelection.h"
38 #include "HTMLAnchorElement.h"
39 #include "HTMLDocument.h"
40 #include "HTMLFrameElementBase.h"
41 #include "HTMLInputElement.h"
42 #include "HTMLNames.h"
43 #include "HTMLOptGroupElement.h"
44 #include "HTMLOptionElement.h"
45 #include "HTMLParserIdioms.h"
46 #include "HTMLProgressElement.h"
47 #include "HTMLStyleElement.h"
48 #include "InsertionPoint.h"
49 #include "InspectorInstrumentation.h"
50 #include "NodeRenderStyle.h"
51 #include "Page.h"
52 #include "RenderElement.h"
53 #include "RenderStyle.h"
54 #include "SelectorCheckerTestFunctions.h"
55 #include "ShadowRoot.h"
56 #include "StyledElement.h"
57 #include "Text.h"
58
59 namespace WebCore {
60
61 using namespace HTMLNames;
62
63 enum class VisitedMatchType : unsigned char {
64     Disabled, Enabled
65 };
66
67 struct SelectorChecker::CheckingContextWithStatus : public SelectorChecker::CheckingContext {
68     CheckingContextWithStatus(const SelectorChecker::CheckingContext& checkingContext, const CSSSelector* selector, Element* element)
69         : SelectorChecker::CheckingContext(checkingContext)
70         , selector(selector)
71         , element(element)
72         , visitedMatchType(resolvingMode == SelectorChecker::Mode::QueryingRules ? VisitedMatchType::Disabled : VisitedMatchType::Enabled)
73         , firstSelectorOfTheFragment(selector)
74         , inFunctionalPseudoClass(false)
75         , pseudoElementEffective(true)
76         , hasScrollbarPseudo(false)
77         , hasSelectionPseudo(false)
78     { }
79
80     const CSSSelector* selector;
81     Element* element;
82     VisitedMatchType visitedMatchType;
83     const CSSSelector* firstSelectorOfTheFragment;
84     bool inFunctionalPseudoClass;
85     bool pseudoElementEffective;
86     bool hasScrollbarPseudo;
87     bool hasSelectionPseudo;
88 };
89
90 static inline bool isFirstChildElement(const Element* element)
91 {
92     return !ElementTraversal::previousSibling(element);
93 }
94
95 static inline bool isLastChildElement(const Element* element)
96 {
97     return !ElementTraversal::nextSibling(element);
98 }
99
100 static inline bool isFirstOfType(Element* element, const QualifiedName& type, bool isResolvingStyle)
101 {
102     for (Element* sibling = ElementTraversal::previousSibling(element); sibling; sibling = ElementTraversal::previousSibling(sibling)) {
103         if (isResolvingStyle)
104             sibling->setAffectsNextSiblingElementStyle();
105         if (sibling->hasTagName(type))
106             return false;
107     }
108     return true;
109 }
110
111 static inline bool isLastOfType(const Element* element, const QualifiedName& type)
112 {
113     for (const Element* sibling = ElementTraversal::nextSibling(element); sibling; sibling = ElementTraversal::nextSibling(sibling)) {
114         if (sibling->hasTagName(type))
115             return false;
116     }
117     return true;
118 }
119
120 static inline int countElementsBefore(Element* element, bool isResolvingStyle)
121 {
122     int count = 0;
123     for (Element* sibling = ElementTraversal::previousSibling(element); sibling; sibling = ElementTraversal::previousSibling(sibling)) {
124         if (isResolvingStyle)
125             sibling->setAffectsNextSiblingElementStyle();
126
127         unsigned index = sibling->childIndex();
128         if (index) {
129             count += index;
130             break;
131         }
132         count++;
133     }
134     return count;
135 }
136
137 static inline int countElementsOfTypeBefore(Element* element, const QualifiedName& type, bool isResolvingStyle)
138 {
139     int count = 0;
140     for (Element* sibling = ElementTraversal::previousSibling(element); sibling; sibling = ElementTraversal::previousSibling(sibling)) {
141         if (isResolvingStyle)
142             sibling->setAffectsNextSiblingElementStyle();
143
144         if (sibling->hasTagName(type))
145             ++count;
146     }
147     return count;
148 }
149
150 static inline int countElementsAfter(const Element* element)
151 {
152     int count = 0;
153     for (const Element* sibling = ElementTraversal::nextSibling(element); sibling; sibling = ElementTraversal::nextSibling(sibling))
154         ++count;
155     return count;
156 }
157
158 static inline int countElementsOfTypeAfter(const Element* element, const QualifiedName& type)
159 {
160     int count = 0;
161     for (const Element* sibling = ElementTraversal::nextSibling(element); sibling; sibling = ElementTraversal::nextSibling(sibling)) {
162         if (sibling->hasTagName(type))
163             ++count;
164     }
165     return count;
166 }
167
168 SelectorChecker::SelectorChecker(Document& document)
169     : m_strictParsing(!document.inQuirksMode())
170     , m_documentIsHTML(document.isHTMLDocument())
171 {
172 }
173
174 bool SelectorChecker::match(const CSSSelector* selector, Element* element, const CheckingContext& providedContext, unsigned& specificity) const
175 {
176     specificity = 0;
177
178     CheckingContextWithStatus context(providedContext, selector, element);
179     PseudoIdSet pseudoIdSet;
180     MatchResult result = matchRecursively(context, pseudoIdSet, specificity);
181     if (result.match != Match::SelectorMatches)
182         return false;
183     if (context.pseudoId != NOPSEUDO && !pseudoIdSet.has(context.pseudoId))
184         return false;
185
186     if (context.pseudoId == NOPSEUDO && pseudoIdSet) {
187         PseudoIdSet publicPseudoIdSet = pseudoIdSet & PseudoIdSet::fromMask(PUBLIC_PSEUDOID_MASK);
188         if (context.resolvingMode == Mode::ResolvingStyle && publicPseudoIdSet)
189             context.elementStyle->setHasPseudoStyles(publicPseudoIdSet);
190
191         // When ignoring virtual pseudo elements, the context's pseudo should also be NOPSEUDO but that does
192         // not cause a failure.
193         return context.resolvingMode == Mode::CollectingRulesIgnoringVirtualPseudoElements || result.matchType == MatchType::Element;
194     }
195     return true;
196 }
197
198 inline static bool hasScrollbarPseudoElement(const PseudoIdSet& dynamicPseudoIdSet)
199 {
200     PseudoIdSet scrollbarIdSet = { SCROLLBAR, SCROLLBAR_THUMB, SCROLLBAR_BUTTON, SCROLLBAR_TRACK, SCROLLBAR_TRACK_PIECE, SCROLLBAR_CORNER };
201     if (dynamicPseudoIdSet & scrollbarIdSet)
202         return true;
203
204     // RESIZER does not always have a scrollbar but it is a scrollbar-like pseudo element
205     // because it can have more than one pseudo element.
206     return dynamicPseudoIdSet.has(RESIZER);
207 }
208
209 static SelectorChecker::CheckingContextWithStatus checkingContextForParent(const SelectorChecker::CheckingContextWithStatus& context)
210 {
211     SelectorChecker::CheckingContextWithStatus updatedContext(context);
212     // Disable :visited matching when we see the first link.
213     if (context.element->isLink())
214         updatedContext.visitedMatchType = VisitedMatchType::Disabled;
215     updatedContext.element = context.element->parentElement();
216     return updatedContext;
217 }
218
219 // Recursive check of selectors and combinators
220 // It can return 4 different values:
221 // * SelectorMatches          - the selector matches the element e
222 // * SelectorFailsLocally     - the selector fails for the element e
223 // * SelectorFailsAllSiblings - the selector fails for e and any sibling of e
224 // * SelectorFailsCompletely  - the selector fails for e and any sibling or ancestor of e
225 SelectorChecker::MatchResult SelectorChecker::matchRecursively(const CheckingContextWithStatus& context, PseudoIdSet& dynamicPseudoIdSet, unsigned& specificity) const
226 {
227     MatchType matchType = MatchType::Element;
228
229     // The first selector has to match.
230     if (!checkOne(context, dynamicPseudoIdSet, matchType, specificity))
231         return MatchResult::fails(Match::SelectorFailsLocally);
232
233     if (context.selector->match() == CSSSelector::PseudoElement) {
234         if (context.selector->isCustomPseudoElement()) {
235             // In functional pseudo class, custom pseudo elements are always disabled.
236             // FIXME: We should accept custom pseudo elements inside :matches().
237             if (context.inFunctionalPseudoClass)
238                 return MatchResult::fails(Match::SelectorFailsCompletely);
239             if (ShadowRoot* root = context.element->containingShadowRoot()) {
240                 if (context.element->shadowPseudoId() != context.selector->value())
241                     return MatchResult::fails(Match::SelectorFailsLocally);
242
243                 if (context.selector->pseudoElementType() == CSSSelector::PseudoElementWebKitCustom && root->type() != ShadowRoot::UserAgentShadowRoot)
244                     return MatchResult::fails(Match::SelectorFailsLocally);
245             } else
246                 return MatchResult::fails(Match::SelectorFailsLocally);
247         } else {
248             if (!context.pseudoElementEffective)
249                 return MatchResult::fails(Match::SelectorFailsCompletely);
250
251             if (context.resolvingMode == Mode::QueryingRules)
252                 return MatchResult::fails(Match::SelectorFailsCompletely);
253
254             PseudoId pseudoId = CSSSelector::pseudoId(context.selector->pseudoElementType());
255             if (pseudoId != NOPSEUDO)
256                 dynamicPseudoIdSet.add(pseudoId);
257             matchType = MatchType::VirtualPseudoElementOnly;
258         }
259     }
260
261     // The rest of the selectors has to match
262     CSSSelector::Relation relation = context.selector->relation();
263
264     // Prepare next selector
265     const CSSSelector* historySelector = context.selector->tagHistory();
266     if (!historySelector)
267         return MatchResult::matches(matchType);
268
269     CheckingContextWithStatus nextContext(context);
270     nextContext.selector = historySelector;
271
272     if (relation != CSSSelector::SubSelector) {
273         // Bail-out if this selector is irrelevant for the pseudoId
274         if (context.pseudoId != NOPSEUDO && !dynamicPseudoIdSet.has(context.pseudoId))
275             return MatchResult::fails(Match::SelectorFailsCompletely);
276
277         // Disable :visited matching when we try to match anything else than an ancestors.
278         if (relation != CSSSelector::Descendant && relation != CSSSelector::Child)
279             nextContext.visitedMatchType = VisitedMatchType::Disabled;
280
281         nextContext.pseudoId = NOPSEUDO;
282         // Virtual pseudo element is only effective in the rightmost fragment.
283         nextContext.pseudoElementEffective = false;
284     }
285
286     switch (relation) {
287     case CSSSelector::Descendant:
288         nextContext = checkingContextForParent(nextContext);
289         nextContext.firstSelectorOfTheFragment = nextContext.selector;
290         nextContext.elementStyle = nullptr;
291         for (; nextContext.element; nextContext = checkingContextForParent(nextContext)) {
292             PseudoIdSet ignoreDynamicPseudo;
293             unsigned descendantsSpecificity = 0;
294             MatchResult result = matchRecursively(nextContext, ignoreDynamicPseudo, descendantsSpecificity);
295             ASSERT(!nextContext.pseudoElementEffective && !ignoreDynamicPseudo);
296
297             if (result.match == Match::SelectorMatches)
298                 specificity = CSSSelector::addSpecificities(specificity, descendantsSpecificity);
299
300             if (result.match == Match::SelectorMatches || result.match == Match::SelectorFailsCompletely)
301                 return MatchResult::updateWithMatchType(result, matchType);
302         }
303         return MatchResult::fails(Match::SelectorFailsCompletely);
304
305     case CSSSelector::Child:
306         {
307             nextContext = checkingContextForParent(nextContext);
308             if (!nextContext.element)
309                 return MatchResult::fails(Match::SelectorFailsCompletely);
310             nextContext.firstSelectorOfTheFragment = nextContext.selector;
311             nextContext.elementStyle = nullptr;
312             PseudoIdSet ignoreDynamicPseudo;
313             unsigned childSpecificity = 0;
314             MatchResult result = matchRecursively(nextContext, ignoreDynamicPseudo, childSpecificity);
315             ASSERT(!nextContext.pseudoElementEffective && !ignoreDynamicPseudo);
316
317             if (result.match == Match::SelectorMatches)
318                 specificity = CSSSelector::addSpecificities(specificity, childSpecificity);
319
320             if (result.match == Match::SelectorMatches || result.match == Match::SelectorFailsCompletely)
321                 return MatchResult::updateWithMatchType(result, matchType);
322             return MatchResult::fails(Match::SelectorFailsAllSiblings);
323         }
324
325     case CSSSelector::DirectAdjacent:
326         {
327             if (context.resolvingMode == Mode::ResolvingStyle)
328                 context.element->setStyleIsAffectedByPreviousSibling();
329
330             Element* previousElement = context.element->previousElementSibling();
331             if (!previousElement)
332                 return MatchResult::fails(Match::SelectorFailsAllSiblings);
333             if (context.resolvingMode == Mode::ResolvingStyle)
334                 previousElement->setAffectsNextSiblingElementStyle();
335             nextContext.element = previousElement;
336             nextContext.firstSelectorOfTheFragment = nextContext.selector;
337             nextContext.elementStyle = nullptr;
338             PseudoIdSet ignoreDynamicPseudo;
339             unsigned adjacentSpecificity = 0;
340             MatchResult result = matchRecursively(nextContext, ignoreDynamicPseudo, adjacentSpecificity);
341             ASSERT(!nextContext.pseudoElementEffective && !ignoreDynamicPseudo);
342
343             if (result.match == Match::SelectorMatches)
344                 specificity = CSSSelector::addSpecificities(specificity, adjacentSpecificity);
345
346             return MatchResult::updateWithMatchType(result, matchType);
347         }
348     case CSSSelector::IndirectAdjacent:
349         if (context.resolvingMode == Mode::ResolvingStyle)
350             context.element->setStyleIsAffectedByPreviousSibling();
351         nextContext.element = context.element->previousElementSibling();
352         nextContext.firstSelectorOfTheFragment = nextContext.selector;
353         nextContext.elementStyle = nullptr;
354         for (; nextContext.element; nextContext.element = nextContext.element->previousElementSibling()) {
355             if (context.resolvingMode == Mode::ResolvingStyle)
356                 context.element->setAffectsNextSiblingElementStyle();
357
358             PseudoIdSet ignoreDynamicPseudo;
359             unsigned indirectAdjacentSpecificity = 0;
360             MatchResult result = matchRecursively(nextContext, ignoreDynamicPseudo, indirectAdjacentSpecificity);
361             ASSERT(!nextContext.pseudoElementEffective && !ignoreDynamicPseudo);
362
363             if (result.match == Match::SelectorMatches)
364                 specificity = CSSSelector::addSpecificities(specificity, indirectAdjacentSpecificity);
365
366             if (result.match == Match::SelectorMatches || result.match == Match::SelectorFailsAllSiblings || result.match == Match::SelectorFailsCompletely)
367                 return MatchResult::updateWithMatchType(result, matchType);
368         };
369         return MatchResult::fails(Match::SelectorFailsAllSiblings);
370
371     case CSSSelector::SubSelector:
372         {
373             // a selector is invalid if something follows a pseudo-element
374             // We make an exception for scrollbar pseudo elements and allow a set of pseudo classes (but nothing else)
375             // to follow the pseudo elements.
376             nextContext.hasScrollbarPseudo = hasScrollbarPseudoElement(dynamicPseudoIdSet);
377             nextContext.hasSelectionPseudo = dynamicPseudoIdSet.has(SELECTION);
378             if ((context.elementStyle || context.resolvingMode == Mode::CollectingRules) && dynamicPseudoIdSet
379                 && !nextContext.hasSelectionPseudo
380                 && !(nextContext.hasScrollbarPseudo && nextContext.selector->match() == CSSSelector::PseudoClass))
381                 return MatchResult::fails(Match::SelectorFailsCompletely);
382
383             unsigned subselectorSpecificity = 0;
384             MatchResult result = matchRecursively(nextContext, dynamicPseudoIdSet, subselectorSpecificity);
385
386             if (result.match == Match::SelectorMatches)
387                 specificity = CSSSelector::addSpecificities(specificity, subselectorSpecificity);
388
389             return MatchResult::updateWithMatchType(result, matchType);
390         }
391     case CSSSelector::ShadowDescendant:
392         {
393             Element* shadowHostNode = context.element->shadowHost();
394             if (!shadowHostNode)
395                 return MatchResult::fails(Match::SelectorFailsCompletely);
396             nextContext.element = shadowHostNode;
397             nextContext.firstSelectorOfTheFragment = nextContext.selector;
398             nextContext.elementStyle = nullptr;
399             PseudoIdSet ignoreDynamicPseudo;
400             unsigned shadowDescendantSpecificity = 0;
401             MatchResult result = matchRecursively(nextContext, ignoreDynamicPseudo, shadowDescendantSpecificity);
402
403             if (result.match == Match::SelectorMatches)
404                 specificity = CSSSelector::addSpecificities(specificity, shadowDescendantSpecificity);
405
406             return MatchResult::updateWithMatchType(result, matchType);
407         }
408     }
409
410     ASSERT_NOT_REACHED();
411     return MatchResult::fails(Match::SelectorFailsCompletely);
412 }
413
414 static bool attributeValueMatches(const Attribute& attribute, CSSSelector::Match match, const AtomicString& selectorValue, bool caseSensitive)
415 {
416     const AtomicString& value = attribute.value();
417     ASSERT(!value.isNull());
418
419     switch (match) {
420     case CSSSelector::Set:
421         break;
422     case CSSSelector::Exact:
423         if (caseSensitive ? selectorValue != value : !equalIgnoringCase(selectorValue, value))
424             return false;
425         break;
426     case CSSSelector::List:
427         {
428             // Ignore empty selectors or selectors containing spaces.
429             if (selectorValue.isEmpty() || selectorValue.find(isHTMLSpace<UChar>) != notFound)
430                 return false;
431
432             unsigned startSearchAt = 0;
433             while (true) {
434                 size_t foundPos = value.find(selectorValue, startSearchAt, caseSensitive);
435                 if (foundPos == notFound)
436                     return false;
437                 if (!foundPos || isHTMLSpace(value[foundPos - 1])) {
438                     unsigned endStr = foundPos + selectorValue.length();
439                     if (endStr == value.length() || isHTMLSpace(value[endStr]))
440                         break; // We found a match.
441                 }
442
443                 // No match. Keep looking.
444                 startSearchAt = foundPos + 1;
445             }
446             break;
447         }
448     case CSSSelector::Contain:
449         if (!value.contains(selectorValue, caseSensitive) || selectorValue.isEmpty())
450             return false;
451         break;
452     case CSSSelector::Begin:
453         if (!value.startsWith(selectorValue, caseSensitive) || selectorValue.isEmpty())
454             return false;
455         break;
456     case CSSSelector::End:
457         if (!value.endsWith(selectorValue, caseSensitive) || selectorValue.isEmpty())
458             return false;
459         break;
460     case CSSSelector::Hyphen:
461         if (value.length() < selectorValue.length())
462             return false;
463         if (!value.startsWith(selectorValue, caseSensitive))
464             return false;
465         // It they start the same, check for exact match or following '-':
466         if (value.length() != selectorValue.length() && value[selectorValue.length()] != '-')
467             return false;
468         break;
469     default:
470         ASSERT_NOT_REACHED();
471         return false;
472     }
473
474     return true;
475 }
476
477 static bool anyAttributeMatches(Element* element, const CSSSelector* selector, const QualifiedName& selectorAttr, bool caseSensitive)
478 {
479     ASSERT(element->hasAttributesWithoutUpdate());
480     for (const Attribute& attribute : element->attributesIterator()) {
481         if (!attribute.matches(selectorAttr.prefix(), element->isHTMLElement() ? selector->attributeCanonicalLocalName() : selectorAttr.localName(), selectorAttr.namespaceURI()))
482             continue;
483
484         if (attributeValueMatches(attribute, selector->match(), selector->value(), caseSensitive))
485             return true;
486     }
487
488     return false;
489 }
490
491 static bool canMatchHoverOrActiveInQuirksMode(const SelectorChecker::CheckingContextWithStatus& context)
492 {
493     // For quirks mode, follow this: http://quirks.spec.whatwg.org/#the-:active-and-:hover-quirk
494     // In quirks mode, a compound selector 'selector' that matches the following conditions must not match elements that would not also match the ':any-link' selector.
495     //
496     //    selector uses the ':active' or ':hover' pseudo-classes.
497     //    selector does not use a type selector.
498     //    selector does not use an attribute selector.
499     //    selector does not use an ID selector.
500     //    selector does not use a class selector.
501     //    selector does not use a pseudo-class selector other than ':active' and ':hover'.
502     //    selector does not use a pseudo-element selector.
503     //    selector is not part of an argument to a functional pseudo-class or pseudo-element.
504     if (context.inFunctionalPseudoClass)
505         return true;
506
507     for (const CSSSelector* selector = context.firstSelectorOfTheFragment; selector; selector = selector->tagHistory()) {
508         switch (selector->match()) {
509         case CSSSelector::Tag:
510             if (selector->tagQName() != anyQName())
511                 return true;
512             break;
513         case CSSSelector::PseudoClass: {
514             CSSSelector::PseudoClassType pseudoClassType = selector->pseudoClassType();
515             if (pseudoClassType != CSSSelector::PseudoClassHover && pseudoClassType != CSSSelector::PseudoClassActive)
516                 return true;
517             break;
518         }
519         case CSSSelector::Id:
520         case CSSSelector::Class:
521         case CSSSelector::Exact:
522         case CSSSelector::Set:
523         case CSSSelector::List:
524         case CSSSelector::Hyphen:
525         case CSSSelector::Contain:
526         case CSSSelector::Begin:
527         case CSSSelector::End:
528         case CSSSelector::PagePseudoClass:
529         case CSSSelector::PseudoElement:
530             return true;
531         case CSSSelector::Unknown:
532             ASSERT_NOT_REACHED();
533             break;
534         }
535
536         CSSSelector::Relation relation = selector->relation();
537         if (relation == CSSSelector::ShadowDescendant)
538             return true;
539
540         if (relation != CSSSelector::SubSelector)
541             return false;
542     }
543     return false;
544 }
545
546 bool SelectorChecker::checkOne(const CheckingContextWithStatus& context, PseudoIdSet& dynamicPseudoIdSet, MatchType& matchType, unsigned& specificity) const
547 {
548     Element* const & element = context.element;
549     const CSSSelector* const & selector = context.selector;
550     ASSERT(element);
551     ASSERT(selector);
552
553     specificity = CSSSelector::addSpecificities(specificity, selector->simpleSelectorSpecificity());
554
555     if (selector->match() == CSSSelector::Tag)
556         return SelectorChecker::tagMatches(element, selector->tagQName());
557
558     if (selector->match() == CSSSelector::Class)
559         return element->hasClass() && element->classNames().contains(selector->value());
560
561     if (selector->match() == CSSSelector::Id)
562         return element->hasID() && element->idForStyleResolution() == selector->value();
563
564     if (selector->isAttributeSelector()) {
565         if (!element->hasAttributes())
566             return false;
567
568         const QualifiedName& attr = selector->attribute();
569         bool caseSensitive = !(m_documentIsHTML && element->isHTMLElement()) || HTMLDocument::isCaseSensitiveAttribute(attr);
570
571         return anyAttributeMatches(element, selector, attr, caseSensitive);
572     }
573
574     if (selector->match() == CSSSelector::PseudoClass) {
575         // Handle :not up front.
576         if (selector->pseudoClassType() == CSSSelector::PseudoClassNot) {
577             const CSSSelectorList* selectorList = selector->selectorList();
578
579             for (const CSSSelector* subselector = selectorList->first(); subselector; subselector = CSSSelectorList::next(subselector)) {
580                 CheckingContextWithStatus subcontext(context);
581                 subcontext.inFunctionalPseudoClass = true;
582                 subcontext.pseudoElementEffective = false;
583                 subcontext.selector = subselector;
584                 subcontext.firstSelectorOfTheFragment = selectorList->first();
585                 PseudoIdSet ignoreDynamicPseudo;
586
587                 unsigned ignoredSpecificity;
588                 if (matchRecursively(subcontext, ignoreDynamicPseudo, ignoredSpecificity).match == Match::SelectorMatches) {
589                     ASSERT(!ignoreDynamicPseudo);
590                     return false;
591                 }
592             }
593             return true;
594         } else if (context.hasScrollbarPseudo) {
595             // CSS scrollbars match a specific subset of pseudo classes, and they have specialized rules for each
596             // (since there are no elements involved except with window-inactive).
597             return checkScrollbarPseudoClass(context, selector);
598         }
599
600         // Normal element pseudo class checking.
601         switch (selector->pseudoClassType()) {
602             // Pseudo classes:
603         case CSSSelector::PseudoClassNot:
604             break; // Already handled up above.
605         case CSSSelector::PseudoClassEmpty:
606             {
607                 bool result = true;
608                 for (Node* node = element->firstChild(); node; node = node->nextSibling()) {
609                     if (is<Element>(*node)) {
610                         result = false;
611                         break;
612                     }
613                     if (is<Text>(*node)) {
614                         Text& textNode = downcast<Text>(*node);
615                         if (!textNode.data().isEmpty()) {
616                             result = false;
617                             break;
618                         }
619                     }
620                 }
621                 if (context.resolvingMode == Mode::ResolvingStyle) {
622                     element->setStyleAffectedByEmpty();
623                     if (context.elementStyle)
624                         context.elementStyle->setEmptyState(result);
625                 }
626                 return result;
627             }
628         case CSSSelector::PseudoClassFirstChild:
629             // first-child matches the first child that is an element
630             if (Element* parentElement = element->parentElement()) {
631                 bool result = isFirstChildElement(element);
632                 if (context.resolvingMode == Mode::ResolvingStyle) {
633                     RenderStyle* childStyle = context.elementStyle ? context.elementStyle : element->renderStyle();
634                     parentElement->setChildrenAffectedByFirstChildRules();
635                     if (result && childStyle)
636                         childStyle->setFirstChildState();
637                 }
638                 return result;
639             }
640             break;
641         case CSSSelector::PseudoClassFirstOfType:
642             // first-of-type matches the first element of its type
643             if (element->parentElement()) {
644                 if (context.resolvingMode == Mode::ResolvingStyle)
645                     element->setStyleIsAffectedByPreviousSibling();
646
647                 return isFirstOfType(element, element->tagQName(), context.resolvingMode == Mode::ResolvingStyle);
648             }
649             break;
650         case CSSSelector::PseudoClassLastChild:
651             // last-child matches the last child that is an element
652             if (Element* parentElement = element->parentElement()) {
653                 bool result = parentElement->isFinishedParsingChildren() && isLastChildElement(element);
654                 if (context.resolvingMode == Mode::ResolvingStyle) {
655                     RenderStyle* childStyle = context.elementStyle ? context.elementStyle : element->renderStyle();
656                     parentElement->setChildrenAffectedByLastChildRules();
657                     if (result && childStyle)
658                         childStyle->setLastChildState();
659                 }
660                 return result;
661             }
662             break;
663         case CSSSelector::PseudoClassLastOfType:
664             // last-of-type matches the last element of its type
665             if (Element* parentElement = element->parentElement()) {
666                 if (context.resolvingMode == Mode::ResolvingStyle)
667                     parentElement->setChildrenAffectedByBackwardPositionalRules();
668                 if (!parentElement->isFinishedParsingChildren())
669                     return false;
670                 return isLastOfType(element, element->tagQName());
671             }
672             break;
673         case CSSSelector::PseudoClassOnlyChild:
674             if (Element* parentElement = element->parentElement()) {
675                 bool firstChild = isFirstChildElement(element);
676                 bool onlyChild = firstChild && parentElement->isFinishedParsingChildren() && isLastChildElement(element);
677                 if (context.resolvingMode == Mode::ResolvingStyle) {
678                     RenderStyle* childStyle = context.elementStyle ? context.elementStyle : element->renderStyle();
679                     parentElement->setChildrenAffectedByFirstChildRules();
680                     parentElement->setChildrenAffectedByLastChildRules();
681                     if (firstChild && childStyle)
682                         childStyle->setFirstChildState();
683                     if (onlyChild && childStyle)
684                         childStyle->setLastChildState();
685                 }
686                 return onlyChild;
687             }
688             break;
689         case CSSSelector::PseudoClassOnlyOfType:
690             // FIXME: This selector is very slow.
691             if (Element* parentElement = element->parentElement()) {
692                 if (context.resolvingMode == Mode::ResolvingStyle) {
693                     element->setStyleIsAffectedByPreviousSibling();
694                     parentElement->setChildrenAffectedByBackwardPositionalRules();
695                 }
696                 if (!parentElement->isFinishedParsingChildren())
697                     return false;
698                 return isFirstOfType(element, element->tagQName(), context.resolvingMode == Mode::ResolvingStyle) && isLastOfType(element, element->tagQName());
699             }
700             break;
701         case CSSSelector::PseudoClassMatches:
702             {
703                 bool hasMatchedAnything = false;
704                 unsigned maxSpecificity = 0;
705
706                 MatchType localMatchType = MatchType::VirtualPseudoElementOnly;
707                 for (const CSSSelector* subselector = selector->selectorList()->first(); subselector; subselector = CSSSelectorList::next(subselector)) {
708                     CheckingContextWithStatus subcontext(context);
709                     subcontext.inFunctionalPseudoClass = true;
710                     subcontext.pseudoElementEffective = context.pseudoElementEffective;
711                     subcontext.selector = subselector;
712                     subcontext.firstSelectorOfTheFragment = subselector;
713                     PseudoIdSet localDynamicPseudoIdSet;
714                     unsigned localSpecificity = 0;
715                     MatchResult result = matchRecursively(subcontext, localDynamicPseudoIdSet, localSpecificity);
716                     if (result.match == Match::SelectorMatches) {
717                         maxSpecificity = std::max(maxSpecificity, localSpecificity);
718
719                         if (result.matchType == MatchType::Element)
720                             localMatchType = MatchType::Element;
721
722                         dynamicPseudoIdSet.merge(localDynamicPseudoIdSet);
723                         hasMatchedAnything = true;
724                     }
725                 }
726                 if (hasMatchedAnything) {
727                     matchType = localMatchType;
728                     specificity = CSSSelector::addSpecificities(specificity, maxSpecificity);
729                 }
730                 return hasMatchedAnything;
731             }
732 #if ENABLE(CSS_SELECTORS_LEVEL4)
733         case CSSSelector::PseudoClassPlaceholderShown:
734             if (is<HTMLTextFormControlElement>(*element)) {
735                 if (context.resolvingMode == Mode::ResolvingStyle) {
736                     if (RenderStyle* style = context.elementStyle ? context.elementStyle : element->renderStyle())
737                         style->setUnique();
738                 }
739                 return downcast<HTMLTextFormControlElement>(*element).isPlaceholderVisible();
740             }
741             return false;
742 #endif
743         case CSSSelector::PseudoClassNthChild:
744             if (!selector->parseNth())
745                 break;
746             if (element->parentElement()) {
747 #if ENABLE(CSS_SELECTORS_LEVEL4)
748                 if (const CSSSelectorList* selectorList = selector->selectorList()) {
749                     if (!matchSelectorList(context, *element, *selectorList))
750                         return false;
751                 }
752 #endif
753
754                 if (context.resolvingMode == Mode::ResolvingStyle)
755                     element->setStyleIsAffectedByPreviousSibling();
756
757                 int count = 1;
758 #if ENABLE(CSS_SELECTORS_LEVEL4)
759                 if (const CSSSelectorList* selectorList = selector->selectorList()) {
760                     for (Element* sibling = ElementTraversal::previousSibling(element); sibling; sibling = ElementTraversal::previousSibling(sibling)) {
761                         if (context.resolvingMode == Mode::ResolvingStyle)
762                             sibling->setAffectsNextSiblingElementStyle();
763
764                         if (matchSelectorList(context, *sibling, *selectorList))
765                             ++count;
766                     }
767                 } else
768 #endif
769                 {
770                     count += countElementsBefore(element, context.resolvingMode == Mode::ResolvingStyle);
771                     if (context.resolvingMode == Mode::ResolvingStyle)
772                         element->setChildIndex(count);
773                 }
774
775                 if (selector->matchNth(count))
776                     return true;
777             }
778             break;
779         case CSSSelector::PseudoClassNthOfType:
780             if (!selector->parseNth())
781                 break;
782
783             if (element->parentElement()) {
784                 if (context.resolvingMode == Mode::ResolvingStyle)
785                     element->setStyleIsAffectedByPreviousSibling();
786
787                 int count = 1 + countElementsOfTypeBefore(element, element->tagQName(), context.resolvingMode == Mode::ResolvingStyle);
788                 if (selector->matchNth(count))
789                     return true;
790             }
791             break;
792         case CSSSelector::PseudoClassNthLastChild:
793             if (!selector->parseNth())
794                 break;
795             if (Element* parentElement = element->parentElement()) {
796                 if (!parentElement->isFinishedParsingChildren())
797                     return false;
798
799 #if ENABLE(CSS_SELECTORS_LEVEL4)
800                 if (const CSSSelectorList* selectorList = selector->selectorList()) {
801                     if (!matchSelectorList(context, *element, *selectorList))
802                         return false;
803
804                     if (context.resolvingMode == Mode::ResolvingStyle) {
805                         parentElement->setChildrenAffectedByPropertyBasedBackwardPositionalRules();
806                         parentElement->setChildrenAffectedByBackwardPositionalRules();
807                     }
808                 } else
809 #endif
810                 if (context.resolvingMode == Mode::ResolvingStyle)
811                     parentElement->setChildrenAffectedByBackwardPositionalRules();
812
813                 int count = 1;
814 #if ENABLE(CSS_SELECTORS_LEVEL4)
815                 if (const CSSSelectorList* selectorList = selector->selectorList()) {
816                     for (Element* sibling = ElementTraversal::nextSibling(element); sibling; sibling = ElementTraversal::nextSibling(sibling)) {
817                         if (matchSelectorList(context, *sibling, *selectorList))
818                             ++count;
819                     }
820                 } else
821 #endif
822                 {
823                     count += countElementsAfter(element);
824                 }
825
826                 if (selector->matchNth(count))
827                     return true;
828             }
829             break;
830         case CSSSelector::PseudoClassNthLastOfType:
831             if (!selector->parseNth())
832                 break;
833             if (Element* parentElement = element->parentElement()) {
834                 if (context.resolvingMode == Mode::ResolvingStyle)
835                     parentElement->setChildrenAffectedByBackwardPositionalRules();
836                 if (!parentElement->isFinishedParsingChildren())
837                     return false;
838
839                 int count = 1 + countElementsOfTypeAfter(element, element->tagQName());
840                 if (selector->matchNth(count))
841                     return true;
842             }
843             break;
844         case CSSSelector::PseudoClassTarget:
845             if (element == element->document().cssTarget())
846                 return true;
847             break;
848         case CSSSelector::PseudoClassAny:
849             {
850                 CheckingContextWithStatus subContext(context);
851                 subContext.inFunctionalPseudoClass = true;
852                 subContext.pseudoElementEffective = false;
853                 for (subContext.selector = selector->selectorList()->first(); subContext.selector; subContext.selector = CSSSelectorList::next(subContext.selector)) {
854                     subContext.firstSelectorOfTheFragment = subContext.selector;
855                     PseudoIdSet ignoreDynamicPseudo;
856                     unsigned ingoredSpecificity = 0;
857                     if (matchRecursively(subContext, ignoreDynamicPseudo, ingoredSpecificity).match == Match::SelectorMatches)
858                         return true;
859                 }
860             }
861             break;
862         case CSSSelector::PseudoClassAutofill:
863             return isAutofilled(element);
864 #if ENABLE(CSS_SELECTORS_LEVEL4)
865         case CSSSelector::PseudoClassAnyLink:
866 #endif
867         case CSSSelector::PseudoClassAnyLinkDeprecated:
868         case CSSSelector::PseudoClassLink:
869             // :visited and :link matches are separated later when applying the style. Here both classes match all links...
870             return element->isLink();
871         case CSSSelector::PseudoClassVisited:
872             // ...except if :visited matching is disabled for ancestor/sibling matching.
873             // Inside functional pseudo class except for :not, :visited never matches.
874             if (context.inFunctionalPseudoClass)
875                 return false;
876             return element->isLink() && context.visitedMatchType == VisitedMatchType::Enabled;
877         case CSSSelector::PseudoClassDrag:
878             if (context.resolvingMode == Mode::ResolvingStyle) {
879                 if (context.elementStyle)
880                     context.elementStyle->setAffectedByDrag();
881                 else
882                     element->setChildrenAffectedByDrag();
883             }
884             if (element->renderer() && element->renderer()->isDragging())
885                 return true;
886             break;
887         case CSSSelector::PseudoClassFocus:
888             return matchesFocusPseudoClass(element);
889         case CSSSelector::PseudoClassHover:
890             if (m_strictParsing || element->isLink() || canMatchHoverOrActiveInQuirksMode(context)) {
891                 if (context.resolvingMode == Mode::ResolvingStyle) {
892                     if (context.elementStyle)
893                         context.elementStyle->setAffectedByHover();
894                     else
895                         element->setChildrenAffectedByHover();
896                 }
897                 if (element->hovered() || InspectorInstrumentation::forcePseudoState(element, CSSSelector::PseudoClassHover))
898                     return true;
899             }
900             break;
901         case CSSSelector::PseudoClassActive:
902             if (m_strictParsing || element->isLink() || canMatchHoverOrActiveInQuirksMode(context)) {
903                 if (context.resolvingMode == Mode::ResolvingStyle) {
904                     if (context.elementStyle)
905                         context.elementStyle->setAffectedByActive();
906                     else
907                         element->setChildrenAffectedByActive();
908                 }
909                 if (element->active() || InspectorInstrumentation::forcePseudoState(element, CSSSelector::PseudoClassActive))
910                     return true;
911             }
912             break;
913         case CSSSelector::PseudoClassEnabled:
914             return isEnabled(element);
915         case CSSSelector::PseudoClassFullPageMedia:
916             return isMediaDocument(element);
917         case CSSSelector::PseudoClassDefault:
918             return isDefaultButtonForForm(element);
919         case CSSSelector::PseudoClassDisabled:
920             return isDisabled(element);
921         case CSSSelector::PseudoClassReadOnly:
922             return matchesReadOnlyPseudoClass(element);
923         case CSSSelector::PseudoClassReadWrite:
924             return matchesReadWritePseudoClass(element);
925         case CSSSelector::PseudoClassOptional:
926             return isOptionalFormControl(element);
927         case CSSSelector::PseudoClassRequired:
928             return isRequiredFormControl(element);
929         case CSSSelector::PseudoClassValid:
930             return isValid(element);
931         case CSSSelector::PseudoClassInvalid:
932             return isInvalid(element);
933         case CSSSelector::PseudoClassChecked:
934             return isChecked(element);
935         case CSSSelector::PseudoClassIndeterminate:
936             return shouldAppearIndeterminate(element);
937         case CSSSelector::PseudoClassRoot:
938             if (element == element->document().documentElement())
939                 return true;
940             break;
941         case CSSSelector::PseudoClassLang:
942             {
943 #if ENABLE(CSS_SELECTORS_LEVEL4)
944                 ASSERT(selector->argumentList() && !selector->argumentList()->isEmpty());
945                 return matchesLangPseudoClass(element, *selector->argumentList());
946 #else
947                 const AtomicString& argument = selector->argument();      
948                 if (argument.isNull())
949                     return false;
950                 return matchesLangPseudoClassDeprecated(element, argument.impl());
951 #endif                          
952             }
953 #if ENABLE(FULLSCREEN_API)
954         case CSSSelector::PseudoClassFullScreen:
955             return matchesFullScreenPseudoClass(element);
956         case CSSSelector::PseudoClassAnimatingFullScreenTransition:
957             return matchesFullScreenAnimatingFullScreenTransitionPseudoClass(element);
958         case CSSSelector::PseudoClassFullScreenAncestor:
959             return matchesFullScreenAncestorPseudoClass(element);
960         case CSSSelector::PseudoClassFullScreenDocument:
961             return matchesFullScreenDocumentPseudoClass(element);
962 #endif
963         case CSSSelector::PseudoClassInRange:
964             return isInRange(element);
965         case CSSSelector::PseudoClassOutOfRange:
966             return isOutOfRange(element);
967 #if ENABLE(VIDEO_TRACK)
968         case CSSSelector::PseudoClassFuture:
969             return matchesFutureCuePseudoClass(element);
970         case CSSSelector::PseudoClassPast:
971             return matchesPastCuePseudoClass(element);
972 #endif
973
974         case CSSSelector::PseudoClassScope:
975             {
976                 const Node* contextualReferenceNode = !context.scope ? element->document().documentElement() : context.scope;
977                 if (element == contextualReferenceNode)
978                     return true;
979                 break;
980             }
981
982         case CSSSelector::PseudoClassWindowInactive:
983             return isWindowInactive(element);
984
985         case CSSSelector::PseudoClassHorizontal:
986         case CSSSelector::PseudoClassVertical:
987         case CSSSelector::PseudoClassDecrement:
988         case CSSSelector::PseudoClassIncrement:
989         case CSSSelector::PseudoClassStart:
990         case CSSSelector::PseudoClassEnd:
991         case CSSSelector::PseudoClassDoubleButton:
992         case CSSSelector::PseudoClassSingleButton:
993         case CSSSelector::PseudoClassNoButton:
994         case CSSSelector::PseudoClassCornerPresent:
995             return false;
996
997 #if ENABLE(CSS_SELECTORS_LEVEL4)
998         // FIXME: Implement :role() selector.
999         case CSSSelector::PseudoClassRole:
1000             return false;
1001 #endif
1002
1003         case CSSSelector::PseudoClassUnknown:
1004             ASSERT_NOT_REACHED();
1005             break;
1006         }
1007         return false;
1008     }
1009 #if ENABLE(VIDEO_TRACK)
1010     if (selector->match() == CSSSelector::PseudoElement && selector->pseudoElementType() == CSSSelector::PseudoElementCue) {
1011         CheckingContextWithStatus subContext(context);
1012
1013         const CSSSelector* const & selector = context.selector;
1014         for (subContext.selector = selector->selectorList()->first(); subContext.selector; subContext.selector = CSSSelectorList::next(subContext.selector)) {
1015             subContext.firstSelectorOfTheFragment = subContext.selector;
1016             subContext.inFunctionalPseudoClass = true;
1017             subContext.pseudoElementEffective = false;
1018             PseudoIdSet ignoredDynamicPseudo;
1019             unsigned ignoredSpecificity = 0;
1020             if (matchRecursively(subContext, ignoredDynamicPseudo, ignoredSpecificity).match == Match::SelectorMatches)
1021                 return true;
1022         }
1023         return false;
1024     }
1025 #endif
1026     // ### add the rest of the checks...
1027     return true;
1028 }
1029
1030 bool SelectorChecker::matchSelectorList(const CheckingContextWithStatus& baseContext, Element& element, const CSSSelectorList& selectorList) const
1031 {
1032     for (const CSSSelector* subselector = selectorList.first(); subselector; subselector = CSSSelectorList::next(subselector)) {
1033         CheckingContextWithStatus subcontext(baseContext);
1034         subcontext.element = &element;
1035         subcontext.selector = subselector;
1036         subcontext.inFunctionalPseudoClass = true;
1037         subcontext.pseudoElementEffective = false;
1038         subcontext.firstSelectorOfTheFragment = subselector;
1039         PseudoIdSet ignoreDynamicPseudo;
1040         unsigned localSpecificity = 0;
1041         if (matchRecursively(subcontext, ignoreDynamicPseudo, localSpecificity).match == Match::SelectorMatches) {
1042             ASSERT(!ignoreDynamicPseudo);
1043             return true;
1044         }
1045     }
1046     return false;
1047 }
1048
1049 bool SelectorChecker::checkScrollbarPseudoClass(const CheckingContextWithStatus& context, const CSSSelector* selector) const
1050 {
1051     ASSERT(selector->match() == CSSSelector::PseudoClass);
1052
1053     switch (selector->pseudoClassType()) {
1054     case CSSSelector::PseudoClassWindowInactive:
1055         return isWindowInactive(context.element);
1056     case CSSSelector::PseudoClassEnabled:
1057         return scrollbarMatchesEnabledPseudoClass(context);
1058     case CSSSelector::PseudoClassDisabled:
1059         return scrollbarMatchesDisabledPseudoClass(context);
1060     case CSSSelector::PseudoClassHover:
1061         return scrollbarMatchesHoverPseudoClass(context);
1062     case CSSSelector::PseudoClassActive:
1063         return scrollbarMatchesActivePseudoClass(context);
1064     case CSSSelector::PseudoClassHorizontal:
1065         return scrollbarMatchesHorizontalPseudoClass(context);
1066     case CSSSelector::PseudoClassVertical:
1067         return scrollbarMatchesVerticalPseudoClass(context);
1068     case CSSSelector::PseudoClassDecrement:
1069         return scrollbarMatchesDecrementPseudoClass(context);
1070     case CSSSelector::PseudoClassIncrement:
1071         return scrollbarMatchesIncrementPseudoClass(context);
1072     case CSSSelector::PseudoClassStart:
1073         return scrollbarMatchesStartPseudoClass(context);
1074     case CSSSelector::PseudoClassEnd:
1075         return scrollbarMatchesEndPseudoClass(context);
1076     case CSSSelector::PseudoClassDoubleButton:
1077         return scrollbarMatchesDoubleButtonPseudoClass(context);
1078     case CSSSelector::PseudoClassSingleButton:
1079         return scrollbarMatchesSingleButtonPseudoClass(context);
1080     case CSSSelector::PseudoClassNoButton:
1081         return scrollbarMatchesNoButtonPseudoClass(context);
1082     case CSSSelector::PseudoClassCornerPresent:
1083         return scrollbarMatchesCornerPresentPseudoClass(context);
1084     default:
1085         return false;
1086     }
1087 }
1088
1089 unsigned SelectorChecker::determineLinkMatchType(const CSSSelector* selector)
1090 {
1091     unsigned linkMatchType = MatchAll;
1092
1093     // Statically determine if this selector will match a link in visited, unvisited or any state, or never.
1094     // :visited never matches other elements than the innermost link element.
1095     for (; selector; selector = selector->tagHistory()) {
1096         if (selector->match() == CSSSelector::PseudoClass) {
1097             switch (selector->pseudoClassType()) {
1098             case CSSSelector::PseudoClassLink:
1099                 linkMatchType &= ~SelectorChecker::MatchVisited;
1100                 break;
1101             case CSSSelector::PseudoClassVisited:
1102                 linkMatchType &= ~SelectorChecker::MatchLink;
1103                 break;
1104             default:
1105                 // We don't support :link and :visited inside :-webkit-any.
1106                 break;
1107             }
1108         }
1109         CSSSelector::Relation relation = selector->relation();
1110         if (relation == CSSSelector::SubSelector)
1111             continue;
1112         if (relation != CSSSelector::Descendant && relation != CSSSelector::Child)
1113             return linkMatchType;
1114         if (linkMatchType != MatchAll)
1115             return linkMatchType;
1116     }
1117     return linkMatchType;
1118 }
1119
1120 static bool isFrameFocused(const Element* element)
1121 {
1122     return element->document().frame() && element->document().frame()->selection().isFocusedAndActive();
1123 }
1124
1125 bool SelectorChecker::matchesFocusPseudoClass(const Element* element)
1126 {
1127     if (InspectorInstrumentation::forcePseudoState(const_cast<Element*>(element), CSSSelector::PseudoClassFocus))
1128         return true;
1129     return element->focused() && isFrameFocused(element);
1130 }
1131
1132 }