1/* 2 * Copyright (C) 2011, 2013 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 "CSSSelectorList.h" 30#include "NodeList.h" 31#include <wtf/HashMap.h> 32#include <wtf/PassRefPtr.h> 33#include <wtf/Vector.h> 34#include <wtf/text/AtomicStringHash.h> 35 36namespace WebCore { 37 38typedef int ExceptionCode; 39 40class CSSSelector; 41class Document; 42class Element; 43class Node; 44class NodeList; 45 46class SelectorDataList { 47public: 48 void initialize(const CSSSelectorList&); 49 bool matches(Element*) const; 50 PassRefPtr<NodeList> queryAll(Node* rootNode) const; 51 PassRefPtr<Element> queryFirst(Node* rootNode) const; 52 53private: 54 struct SelectorData { 55 SelectorData(const CSSSelector* selector, bool isFastCheckable) : selector(selector), isFastCheckable(isFastCheckable) { } 56 const CSSSelector* selector; 57 bool isFastCheckable; 58 }; 59 60 bool selectorMatches(const SelectorData&, Element*, const Node*) const; 61 template <bool firstMatchOnly> 62 void execute(Node* rootNode, Vector<RefPtr<Node> >&) const; 63 64 template <bool firstMatchOnly> void executeFastPathForIdSelector(const Node* rootNode, const SelectorData&, const CSSSelector* idSelector, Vector<RefPtr<Node> >&) const; 65 template <bool firstMatchOnly> void executeSingleTagNameSelectorData(const Node* rootNode, const SelectorData&, Vector<RefPtr<Node> >&) const; 66 template <bool firstMatchOnly> void executeSingleClassNameSelectorData(const Node* rootNode, const SelectorData&, Vector<RefPtr<Node> >&) const; 67 template <bool firstMatchOnly> void executeSingleSelectorData(const Node* rootNode, const SelectorData&, Vector<RefPtr<Node> >&) const; 68 template <bool firstMatchOnly> void executeSingleMultiSelectorData(const Node* rootNode, Vector<RefPtr<Node> >&) const; 69 70 Vector<SelectorData> m_selectors; 71}; 72 73class SelectorQuery { 74 WTF_MAKE_NONCOPYABLE(SelectorQuery); 75 WTF_MAKE_FAST_ALLOCATED; 76public: 77 explicit SelectorQuery(const CSSSelectorList&); 78 bool matches(Element*) const; 79 PassRefPtr<NodeList> queryAll(Node* rootNode) const; 80 PassRefPtr<Element> queryFirst(Node* rootNode) const; 81private: 82 SelectorDataList m_selectors; 83 CSSSelectorList m_selectorList; 84}; 85 86class SelectorQueryCache { 87 WTF_MAKE_FAST_ALLOCATED; 88public: 89 SelectorQuery* add(const AtomicString&, Document*, ExceptionCode&); 90 void invalidate(); 91 92private: 93 HashMap<AtomicString, OwnPtr<SelectorQuery> > m_entries; 94}; 95 96inline bool SelectorQuery::matches(Element* element) const 97{ 98 return m_selectors.matches(element); 99} 100 101inline PassRefPtr<NodeList> SelectorQuery::queryAll(Node* rootNode) const 102{ 103 return m_selectors.queryAll(rootNode); 104} 105 106inline PassRefPtr<Element> SelectorQuery::queryFirst(Node* rootNode) const 107{ 108 return m_selectors.queryFirst(rootNode); 109} 110 111} 112 113#endif 114