1 /* 2 * Copyright (C) 2011 Apple Inc. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY 15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 17 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY 18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 20 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 21 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 */ 25 26 #ifndef SelectorQuery_h 27 #define SelectorQuery_h 28 29 #include "core/css/CSSSelectorList.h" 30 #include "wtf/HashMap.h" 31 #include "wtf/Vector.h" 32 #include "wtf/text/AtomicStringHash.h" 33 34 namespace WebCore { 35 36 class CSSSelector; 37 class Document; 38 class Element; 39 class ExceptionState; 40 class Node; 41 class NodeList; 42 class SimpleNodeList; 43 class SpaceSplitString; 44 45 class SelectorDataList { 46 public: 47 void initialize(const CSSSelectorList&); 48 bool matches(Element&) const; 49 PassRefPtr<NodeList> queryAll(Node& rootNode) const; 50 PassRefPtr<Element> queryFirst(Node& rootNode) const; 51 52 private: 53 struct SelectorData { 54 SelectorData(const CSSSelector* selector, bool isFastCheckable) : selector(selector), isFastCheckable(isFastCheckable) { } 55 const CSSSelector* selector; 56 bool isFastCheckable; 57 }; 58 59 bool canUseFastQuery(const Node& rootNode) const; 60 bool selectorMatches(const SelectorData&, Element&, const Node&) const; 61 void collectElementsByClassName(Node& rootNode, const AtomicString& className, Vector<RefPtr<Node> >&) const; 62 Element* findElementByClassName(Node& rootNode, const AtomicString& className) const; 63 void collectElementsByTagName(Node& rootNode, const QualifiedName& tagName, Vector<RefPtr<Node> >&) const; 64 Element* findElementByTagName(Node& rootNode, const QualifiedName& tagName) const; 65 PassOwnPtr<SimpleNodeList> findTraverseRoots(Node& rootNode, bool& matchTraverseRoots) const; 66 void executeSlowQueryAll(Node& rootNode, Vector<RefPtr<Node> >& matchedElements) const; 67 void executeQueryAll(Node& rootNode, Vector<RefPtr<Node> >& matchedElements) const; 68 Node* findTraverseRoot(Node& rootNode, bool& matchTraverseRoot) const; 69 Element* executeSlowQueryFirst(Node& rootNode) const; 70 Element* executeQueryFirst(Node& rootNode) const; 71 72 Vector<SelectorData> m_selectors; 73 }; 74 75 class SelectorQuery { 76 WTF_MAKE_NONCOPYABLE(SelectorQuery); 77 WTF_MAKE_FAST_ALLOCATED; 78 public: 79 explicit SelectorQuery(const CSSSelectorList&); 80 bool matches(Element&) const; 81 PassRefPtr<NodeList> queryAll(Node& rootNode) const; 82 PassRefPtr<Element> queryFirst(Node& rootNode) const; 83 private: 84 SelectorDataList m_selectors; 85 CSSSelectorList m_selectorList; 86 }; 87 88 class SelectorQueryCache { 89 WTF_MAKE_FAST_ALLOCATED; 90 public: 91 SelectorQuery* add(const AtomicString&, const Document&, ExceptionState&); 92 void invalidate(); 93 94 private: 95 HashMap<AtomicString, OwnPtr<SelectorQuery> > m_entries; 96 }; 97 98 } 99 100 #endif 101