1 | /* |
2 | * Copyright (C) 2016 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 | * 1. Redistributions of source code must retain the above copyright |
8 | * notice, this list of conditions and the following disclaimer. |
9 | * 2. Redistributions in binary form must reproduce the above copyright |
10 | * notice, this list of conditions and the following disclaimer in the |
11 | * documentation and/or other materials provided with the distribution. |
12 | * |
13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' |
14 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
15 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
17 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
18 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
19 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
20 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
21 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
22 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
23 | * THE POSSIBILITY OF SUCH DAMAGE. |
24 | */ |
25 | |
26 | #pragma once |
27 | |
28 | #include "RenderIterator.h" |
29 | |
30 | namespace WebCore { |
31 | |
32 | template <typename T> |
33 | class RenderDescendantIterator : public RenderIterator<T> { |
34 | public: |
35 | RenderDescendantIterator(const RenderElement& root); |
36 | RenderDescendantIterator(const RenderElement& root, T* current); |
37 | RenderDescendantIterator& operator++(); |
38 | }; |
39 | |
40 | template <typename T> |
41 | class RenderDescendantConstIterator : public RenderConstIterator<T> { |
42 | public: |
43 | RenderDescendantConstIterator(const RenderElement& root); |
44 | RenderDescendantConstIterator(const RenderElement& root, const T* current); |
45 | RenderDescendantConstIterator& operator++(); |
46 | }; |
47 | |
48 | template <typename T> |
49 | class RenderDescendantIteratorAdapter { |
50 | public: |
51 | RenderDescendantIteratorAdapter(RenderElement& root); |
52 | RenderDescendantIterator<T> begin(); |
53 | RenderDescendantIterator<T> end(); |
54 | RenderDescendantIterator<T> at(T&); |
55 | |
56 | private: |
57 | RenderElement& m_root; |
58 | }; |
59 | |
60 | template <typename T> |
61 | class RenderDescendantConstIteratorAdapter { |
62 | public: |
63 | RenderDescendantConstIteratorAdapter(const RenderElement& root); |
64 | RenderDescendantConstIterator<T> begin() const; |
65 | RenderDescendantConstIterator<T> end() const; |
66 | RenderDescendantConstIterator<T> at(const T&) const; |
67 | |
68 | private: |
69 | const RenderElement& m_root; |
70 | }; |
71 | |
72 | template <typename T> RenderDescendantIteratorAdapter<T> descendantsOfType(RenderElement&); |
73 | template <typename T> RenderDescendantConstIteratorAdapter<T> descendantsOfType(const RenderElement&); |
74 | |
75 | // RenderDescendantIterator |
76 | |
77 | template <typename T> |
78 | inline RenderDescendantIterator<T>::RenderDescendantIterator(const RenderElement& root) |
79 | : RenderIterator<T>(&root) |
80 | { |
81 | } |
82 | |
83 | template <typename T> |
84 | inline RenderDescendantIterator<T>::RenderDescendantIterator(const RenderElement& root, T* current) |
85 | : RenderIterator<T>(&root, current) |
86 | { |
87 | } |
88 | |
89 | template <typename T> |
90 | inline RenderDescendantIterator<T>& RenderDescendantIterator<T>::operator++() |
91 | { |
92 | return static_cast<RenderDescendantIterator<T>&>(RenderIterator<T>::traverseNext()); |
93 | } |
94 | |
95 | // RenderDescendantConstIterator |
96 | |
97 | template <typename T> |
98 | inline RenderDescendantConstIterator<T>::RenderDescendantConstIterator(const RenderElement& root) |
99 | : RenderConstIterator<T>(&root) |
100 | { |
101 | } |
102 | |
103 | template <typename T> |
104 | inline RenderDescendantConstIterator<T>::RenderDescendantConstIterator(const RenderElement& root, const T* current) |
105 | : RenderConstIterator<T>(&root, current) |
106 | { |
107 | } |
108 | |
109 | template <typename T> |
110 | inline RenderDescendantConstIterator<T>& RenderDescendantConstIterator<T>::operator++() |
111 | { |
112 | return static_cast<RenderDescendantConstIterator<T>&>(RenderConstIterator<T>::traverseNext()); |
113 | } |
114 | |
115 | // RenderDescendantIteratorAdapter |
116 | |
117 | template <typename T> |
118 | inline RenderDescendantIteratorAdapter<T>::RenderDescendantIteratorAdapter(RenderElement& root) |
119 | : m_root(root) |
120 | { |
121 | } |
122 | |
123 | template <typename T> |
124 | inline RenderDescendantIterator<T> RenderDescendantIteratorAdapter<T>::begin() |
125 | { |
126 | return RenderDescendantIterator<T>(m_root, RenderTraversal::firstWithin<T>(m_root)); |
127 | } |
128 | |
129 | template <typename T> |
130 | inline RenderDescendantIterator<T> RenderDescendantIteratorAdapter<T>::end() |
131 | { |
132 | return RenderDescendantIterator<T>(m_root); |
133 | } |
134 | |
135 | template <typename T> |
136 | inline RenderDescendantIterator<T> RenderDescendantIteratorAdapter<T>::at(T& current) |
137 | { |
138 | return RenderDescendantIterator<T>(m_root, ¤t); |
139 | } |
140 | |
141 | // RenderDescendantConstIteratorAdapter |
142 | |
143 | template <typename T> |
144 | inline RenderDescendantConstIteratorAdapter<T>::RenderDescendantConstIteratorAdapter(const RenderElement& root) |
145 | : m_root(root) |
146 | { |
147 | } |
148 | |
149 | template <typename T> |
150 | inline RenderDescendantConstIterator<T> RenderDescendantConstIteratorAdapter<T>::begin() const |
151 | { |
152 | return RenderDescendantConstIterator<T>(m_root, RenderTraversal::firstWithin<T>(m_root)); |
153 | } |
154 | |
155 | template <typename T> |
156 | inline RenderDescendantConstIterator<T> RenderDescendantConstIteratorAdapter<T>::end() const |
157 | { |
158 | return RenderDescendantConstIterator<T>(m_root); |
159 | } |
160 | |
161 | template <typename T> |
162 | inline RenderDescendantConstIterator<T> RenderDescendantConstIteratorAdapter<T>::at(const T& current) const |
163 | { |
164 | return RenderDescendantConstIterator<T>(m_root, ¤t); |
165 | } |
166 | |
167 | // Standalone functions |
168 | |
169 | template <typename T> |
170 | inline RenderDescendantIteratorAdapter<T> descendantsOfType(RenderElement& root) |
171 | { |
172 | return RenderDescendantIteratorAdapter<T>(root); |
173 | } |
174 | |
175 | template <typename T> |
176 | inline RenderDescendantConstIteratorAdapter<T> descendantsOfType(const RenderElement& root) |
177 | { |
178 | return RenderDescendantConstIteratorAdapter<T>(root); |
179 | } |
180 | |
181 | } // namespace WebCore |
182 | |