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
30namespace WebCore {
31
32template <typename T>
33class RenderDescendantIterator : public RenderIterator<T> {
34public:
35 RenderDescendantIterator(const RenderElement& root);
36 RenderDescendantIterator(const RenderElement& root, T* current);
37 RenderDescendantIterator& operator++();
38};
39
40template <typename T>
41class RenderDescendantConstIterator : public RenderConstIterator<T> {
42public:
43 RenderDescendantConstIterator(const RenderElement& root);
44 RenderDescendantConstIterator(const RenderElement& root, const T* current);
45 RenderDescendantConstIterator& operator++();
46};
47
48template <typename T>
49class RenderDescendantIteratorAdapter {
50public:
51 RenderDescendantIteratorAdapter(RenderElement& root);
52 RenderDescendantIterator<T> begin();
53 RenderDescendantIterator<T> end();
54 RenderDescendantIterator<T> at(T&);
55
56private:
57 RenderElement& m_root;
58};
59
60template <typename T>
61class RenderDescendantConstIteratorAdapter {
62public:
63 RenderDescendantConstIteratorAdapter(const RenderElement& root);
64 RenderDescendantConstIterator<T> begin() const;
65 RenderDescendantConstIterator<T> end() const;
66 RenderDescendantConstIterator<T> at(const T&) const;
67
68private:
69 const RenderElement& m_root;
70};
71
72template <typename T> RenderDescendantIteratorAdapter<T> descendantsOfType(RenderElement&);
73template <typename T> RenderDescendantConstIteratorAdapter<T> descendantsOfType(const RenderElement&);
74
75// RenderDescendantIterator
76
77template <typename T>
78inline RenderDescendantIterator<T>::RenderDescendantIterator(const RenderElement& root)
79 : RenderIterator<T>(&root)
80{
81}
82
83template <typename T>
84inline RenderDescendantIterator<T>::RenderDescendantIterator(const RenderElement& root, T* current)
85 : RenderIterator<T>(&root, current)
86{
87}
88
89template <typename T>
90inline RenderDescendantIterator<T>& RenderDescendantIterator<T>::operator++()
91{
92 return static_cast<RenderDescendantIterator<T>&>(RenderIterator<T>::traverseNext());
93}
94
95// RenderDescendantConstIterator
96
97template <typename T>
98inline RenderDescendantConstIterator<T>::RenderDescendantConstIterator(const RenderElement& root)
99 : RenderConstIterator<T>(&root)
100{
101}
102
103template <typename T>
104inline RenderDescendantConstIterator<T>::RenderDescendantConstIterator(const RenderElement& root, const T* current)
105 : RenderConstIterator<T>(&root, current)
106{
107}
108
109template <typename T>
110inline RenderDescendantConstIterator<T>& RenderDescendantConstIterator<T>::operator++()
111{
112 return static_cast<RenderDescendantConstIterator<T>&>(RenderConstIterator<T>::traverseNext());
113}
114
115// RenderDescendantIteratorAdapter
116
117template <typename T>
118inline RenderDescendantIteratorAdapter<T>::RenderDescendantIteratorAdapter(RenderElement& root)
119 : m_root(root)
120{
121}
122
123template <typename T>
124inline RenderDescendantIterator<T> RenderDescendantIteratorAdapter<T>::begin()
125{
126 return RenderDescendantIterator<T>(m_root, RenderTraversal::firstWithin<T>(m_root));
127}
128
129template <typename T>
130inline RenderDescendantIterator<T> RenderDescendantIteratorAdapter<T>::end()
131{
132 return RenderDescendantIterator<T>(m_root);
133}
134
135template <typename T>
136inline RenderDescendantIterator<T> RenderDescendantIteratorAdapter<T>::at(T& current)
137{
138 return RenderDescendantIterator<T>(m_root, &current);
139}
140
141// RenderDescendantConstIteratorAdapter
142
143template <typename T>
144inline RenderDescendantConstIteratorAdapter<T>::RenderDescendantConstIteratorAdapter(const RenderElement& root)
145 : m_root(root)
146{
147}
148
149template <typename T>
150inline RenderDescendantConstIterator<T> RenderDescendantConstIteratorAdapter<T>::begin() const
151{
152 return RenderDescendantConstIterator<T>(m_root, RenderTraversal::firstWithin<T>(m_root));
153}
154
155template <typename T>
156inline RenderDescendantConstIterator<T> RenderDescendantConstIteratorAdapter<T>::end() const
157{
158 return RenderDescendantConstIterator<T>(m_root);
159}
160
161template <typename T>
162inline RenderDescendantConstIterator<T> RenderDescendantConstIteratorAdapter<T>::at(const T& current) const
163{
164 return RenderDescendantConstIterator<T>(m_root, &current);
165}
166
167// Standalone functions
168
169template <typename T>
170inline RenderDescendantIteratorAdapter<T> descendantsOfType(RenderElement& root)
171{
172 return RenderDescendantIteratorAdapter<T>(root);
173}
174
175template <typename T>
176inline RenderDescendantConstIteratorAdapter<T> descendantsOfType(const RenderElement& root)
177{
178 return RenderDescendantConstIteratorAdapter<T>(root);
179}
180
181} // namespace WebCore
182