1/*
2 * Copyright (C) 2005 Frerich Raabe <raabe@kde.org>
3 * Copyright (C) 2006, 2009 Apple Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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.
25 */
26
27#pragma once
28
29#include <wtf/Vector.h>
30#include <wtf/text/AtomicString.h>
31
32namespace WebCore {
33
34class Node;
35
36namespace XPath {
37
38class Expression;
39class NodeSet;
40
41class Step {
42 WTF_MAKE_FAST_ALLOCATED;
43public:
44 enum Axis {
45 AncestorAxis, AncestorOrSelfAxis, AttributeAxis,
46 ChildAxis, DescendantAxis, DescendantOrSelfAxis,
47 FollowingAxis, FollowingSiblingAxis, NamespaceAxis,
48 ParentAxis, PrecedingAxis, PrecedingSiblingAxis,
49 SelfAxis
50 };
51
52 class NodeTest {
53 WTF_MAKE_FAST_ALLOCATED;
54 public:
55 enum Kind { TextNodeTest, CommentNodeTest, ProcessingInstructionNodeTest, AnyNodeTest, NameTest };
56
57 explicit NodeTest(Kind kind) : m_kind(kind) { }
58 NodeTest(Kind kind, const AtomicString& data) : m_kind(kind), m_data(data) { }
59 NodeTest(Kind kind, const AtomicString& data, const AtomicString& namespaceURI) : m_kind(kind), m_data(data), m_namespaceURI(namespaceURI) { }
60
61 private:
62 friend class Step;
63 friend void optimizeStepPair(Step&, Step&, bool&);
64 friend bool nodeMatchesBasicTest(Node&, Axis, const NodeTest&);
65 friend bool nodeMatches(Node&, Axis, const NodeTest&);
66
67 Kind m_kind;
68 AtomicString m_data;
69 AtomicString m_namespaceURI;
70 Vector<std::unique_ptr<Expression>> m_mergedPredicates;
71 };
72
73 Step(Axis, NodeTest);
74 Step(Axis, NodeTest, Vector<std::unique_ptr<Expression>>);
75 ~Step();
76
77 void optimize();
78
79 void evaluate(Node& context, NodeSet&) const;
80
81 Axis axis() const { return m_axis; }
82
83private:
84 friend void optimizeStepPair(Step&, Step&, bool&);
85
86 bool predicatesAreContextListInsensitive() const;
87
88 void parseNodeTest(const String&);
89 void nodesInAxis(Node& context, NodeSet&) const;
90
91 Axis m_axis;
92 NodeTest m_nodeTest;
93 Vector<std::unique_ptr<Expression>> m_predicates;
94};
95
96void optimizeStepPair(Step&, Step&, bool& dropSecondStep);
97
98} // namespace XPath
99} // namespace WebCore
100