Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(141)

Side by Side Diff: third_party/WebKit/Source/core/dom/SelectorQuery.cpp

Issue 2769783002: Centralize more querySelector logic behind QuerySelectorCache. (Closed)
Patch Set: Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2011, 2013 Apple Inc. All rights reserved. 2 * Copyright (C) 2011, 2013 Apple Inc. All rights reserved.
3 * Copyright (C) 2014 Samsung Electronics. All rights reserved. 3 * Copyright (C) 2014 Samsung Electronics. All rights reserved.
4 * 4 *
5 * Redistribution and use in source and binary forms, with or without 5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions 6 * modification, are permitted provided that the following conditions
7 * are met: 7 * are met:
8 * 8 *
9 * 1. Redistributions of source code must retain the above copyright 9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer. 10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright 11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the 12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution. 13 * documentation and/or other materials provided with the distribution.
14 * 14 *
15 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY 15 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
16 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 16 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY 18 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
19 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 19 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 20 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 21 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */ 25 */
26 26
27 #include "core/dom/SelectorQuery.h" 27 #include "core/dom/SelectorQuery.h"
28 28
29 #include <memory>
29 #include "bindings/core/v8/ExceptionState.h" 30 #include "bindings/core/v8/ExceptionState.h"
30 #include "core/HTMLNames.h" 31 #include "core/HTMLNames.h"
31 #include "core/css/SelectorChecker.h" 32 #include "core/css/SelectorChecker.h"
32 #include "core/css/parser/CSSParser.h" 33 #include "core/css/parser/CSSParser.h"
33 #include "core/dom/Document.h" 34 #include "core/dom/Document.h"
34 #include "core/dom/ElementTraversal.h" 35 #include "core/dom/ElementTraversal.h"
35 #include "core/dom/ExceptionCode.h" 36 #include "core/dom/ExceptionCode.h"
36 #include "core/dom/Node.h" 37 #include "core/dom/Node.h"
38 #include "core/dom/NthIndexCache.h"
37 #include "core/dom/StaticNodeList.h" 39 #include "core/dom/StaticNodeList.h"
38 #include "core/dom/shadow/ElementShadow.h" 40 #include "core/dom/shadow/ElementShadow.h"
39 #include "core/dom/shadow/ShadowRoot.h" 41 #include "core/dom/shadow/ShadowRoot.h"
40 #include "wtf/PtrUtil.h" 42 #include "wtf/PtrUtil.h"
41 #include <memory>
42 43
43 namespace blink { 44 namespace blink {
44 45
45 using namespace HTMLNames; 46 using namespace HTMLNames;
46 47
47 struct SingleElementSelectorQueryTrait { 48 struct SingleElementSelectorQueryTrait {
48 typedef Element* OutputType; 49 typedef Element* OutputType;
49 static const bool shouldOnlyMatchFirstElement = true; 50 static const bool shouldOnlyMatchFirstElement = true;
50 ALWAYS_INLINE static void appendElement(OutputType& output, 51 ALWAYS_INLINE static void appendElement(OutputType& output,
51 Element& element) { 52 Element& element) {
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
139 currentElement = currentElement->parentElement()) { 140 currentElement = currentElement->parentElement()) {
140 for (const auto& selector : m_selectors) { 141 for (const auto& selector : m_selectors) {
141 if (selectorMatches(*selector, *currentElement, targetElement)) 142 if (selectorMatches(*selector, *currentElement, targetElement))
142 return currentElement; 143 return currentElement;
143 } 144 }
144 } 145 }
145 return nullptr; 146 return nullptr;
146 } 147 }
147 148
148 StaticElementList* SelectorQuery::queryAll(ContainerNode& rootNode) const { 149 StaticElementList* SelectorQuery::queryAll(ContainerNode& rootNode) const {
150 NthIndexCache nthIndexCache(rootNode.document());
149 HeapVector<Member<Element>> result; 151 HeapVector<Member<Element>> result;
150 execute<AllElementsSelectorQueryTrait>(rootNode, result); 152 execute<AllElementsSelectorQueryTrait>(rootNode, result);
151 return StaticElementList::adopt(result); 153 return StaticElementList::adopt(result);
152 } 154 }
153 155
154 Element* SelectorQuery::queryFirst(ContainerNode& rootNode) const { 156 Element* SelectorQuery::queryFirst(ContainerNode& rootNode) const {
157 NthIndexCache nthIndexCache(rootNode.document());
155 Element* matchedElement = nullptr; 158 Element* matchedElement = nullptr;
156 execute<SingleElementSelectorQueryTrait>(rootNode, matchedElement); 159 execute<SingleElementSelectorQueryTrait>(rootNode, matchedElement);
157 return matchedElement; 160 return matchedElement;
158 } 161 }
159 162
160 template <typename SelectorQueryTrait> 163 template <typename SelectorQueryTrait>
161 static void collectElementsByClassName( 164 static void collectElementsByClassName(
162 ContainerNode& rootNode, 165 ContainerNode& rootNode,
163 const AtomicString& className, 166 const AtomicString& className,
164 typename SelectorQueryTrait::OutputType& output) { 167 typename SelectorQueryTrait::OutputType& output) {
(...skipping 425 matching lines...) Expand 10 before | Expand all | Expand 10 after
590 m_selectors.uncheckedAppend(selector); 593 m_selectors.uncheckedAppend(selector);
591 m_usesDeepCombinatorOrShadowPseudo |= 594 m_usesDeepCombinatorOrShadowPseudo |=
592 selector->hasDeepCombinatorOrShadowPseudo(); 595 selector->hasDeepCombinatorOrShadowPseudo();
593 m_needsUpdatedDistribution |= selector->needsUpdatedDistribution(); 596 m_needsUpdatedDistribution |= selector->needsUpdatedDistribution();
594 } 597 }
595 } 598 }
596 599
597 SelectorQuery* SelectorQueryCache::add(const AtomicString& selectors, 600 SelectorQuery* SelectorQueryCache::add(const AtomicString& selectors,
598 const Document& document, 601 const Document& document,
599 ExceptionState& exceptionState) { 602 ExceptionState& exceptionState) {
603 if (selectors.isEmpty()) {
604 exceptionState.throwDOMException(SyntaxError,
605 "The provided selector is empty.");
606 return nullptr;
607 }
608
600 HashMap<AtomicString, std::unique_ptr<SelectorQuery>>::iterator it = 609 HashMap<AtomicString, std::unique_ptr<SelectorQuery>>::iterator it =
601 m_entries.find(selectors); 610 m_entries.find(selectors);
602 if (it != m_entries.end()) 611 if (it != m_entries.end())
603 return it->value.get(); 612 return it->value.get();
604 613
605 CSSSelectorList selectorList = CSSParser::parseSelector( 614 CSSSelectorList selectorList = CSSParser::parseSelector(
606 CSSParserContext::create(document, KURL(), emptyString, 615 CSSParserContext::create(document, KURL(), emptyString,
607 CSSParserContext::StaticProfile), 616 CSSParserContext::StaticProfile),
608 nullptr, selectors); 617 nullptr, selectors);
609 618
(...skipping 10 matching lines...) Expand all
620 return m_entries 629 return m_entries
621 .insert(selectors, SelectorQuery::adopt(std::move(selectorList))) 630 .insert(selectors, SelectorQuery::adopt(std::move(selectorList)))
622 .storedValue->value.get(); 631 .storedValue->value.get();
623 } 632 }
624 633
625 void SelectorQueryCache::invalidate() { 634 void SelectorQueryCache::invalidate() {
626 m_entries.clear(); 635 m_entries.clear();
627 } 636 }
628 637
629 } // namespace blink 638 } // namespace blink
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/core/dom/Element.cpp ('k') | third_party/WebKit/Source/core/inspector/InspectorCSSAgent.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698