blob: 66f12a6ad5ce56a6f0a0dc753c4e4a18501ccc41 [file] [log] [blame]
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001/*
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"
Ben Murdoche69819b2013-07-17 14:56:49 +010030#include "wtf/HashMap.h"
31#include "wtf/Vector.h"
32#include "wtf/text/AtomicStringHash.h"
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010033
34namespace WebCore {
35
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010036class CSSSelector;
37class Document;
38class Element;
Ben Murdochdf957042013-08-06 11:01:27 +010039class ExceptionState;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010040class Node;
41class NodeList;
Ben Murdochfff88842013-07-30 15:20:09 +010042class SimpleNodeList;
43class SpaceSplitString;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010044
45class SelectorDataList {
46public:
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
52private:
53 struct SelectorData {
54 SelectorData(const CSSSelector* selector, bool isFastCheckable) : selector(selector), isFastCheckable(isFastCheckable) { }
55 const CSSSelector* selector;
56 bool isFastCheckable;
57 };
58
Ben Murdochfff88842013-07-30 15:20:09 +010059 bool canUseFastQuery(Node* rootNode) const;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010060 bool selectorMatches(const SelectorData&, Element*, const Node*) const;
Ben Murdochfff88842013-07-30 15:20:09 +010061 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;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010071
72 Vector<SelectorData> m_selectors;
73};
74
75class SelectorQuery {
76 WTF_MAKE_NONCOPYABLE(SelectorQuery);
77 WTF_MAKE_FAST_ALLOCATED;
78public:
79 explicit SelectorQuery(const CSSSelectorList&);
80 bool matches(Element*) const;
81 PassRefPtr<NodeList> queryAll(Node* rootNode) const;
82 PassRefPtr<Element> queryFirst(Node* rootNode) const;
83private:
84 SelectorDataList m_selectors;
85 CSSSelectorList m_selectorList;
86};
87
88class SelectorQueryCache {
89 WTF_MAKE_FAST_ALLOCATED;
90public:
Ben Murdochdf957042013-08-06 11:01:27 +010091 SelectorQuery* add(const AtomicString&, Document*, ExceptionState&);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010092 void invalidate();
93
94private:
95 HashMap<AtomicString, OwnPtr<SelectorQuery> > m_entries;
96};
97
98}
99
100#endif