1/*
2 * Copyright (C) 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 * 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. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#pragma once
27
28#include "RenderIterator.h"
29
30namespace WebCore {
31
32template <typename T>
33class RenderChildIterator : public RenderIterator<T> {
34public:
35 RenderChildIterator(const RenderElement& parent);
36 RenderChildIterator(const RenderElement& parent, T* current);
37 RenderChildIterator& operator++();
38};
39
40template <typename T>
41class RenderChildConstIterator : public RenderConstIterator<T> {
42public:
43 RenderChildConstIterator(const RenderElement& parent);
44 RenderChildConstIterator(const RenderElement& parent, const T* current);
45 RenderChildConstIterator& operator++();
46};
47
48template <typename T>
49class RenderChildIteratorAdapter {
50public:
51 RenderChildIteratorAdapter(RenderElement& parent);
52 RenderChildIterator<T> begin();
53 RenderChildIterator<T> end();
54 T* first();
55 T* last();
56
57private:
58 RenderElement& m_parent;
59};
60
61template <typename T>
62class RenderChildConstIteratorAdapter {
63public:
64 RenderChildConstIteratorAdapter(const RenderElement& parent);
65 RenderChildConstIterator<T> begin() const;
66 RenderChildConstIterator<T> end() const;
67 const T* first() const;
68 const T* last() const;
69
70private:
71 const RenderElement& m_parent;
72};
73
74template <typename T> RenderChildIteratorAdapter<T> childrenOfType(RenderElement&);
75template <typename T> RenderChildConstIteratorAdapter<T> childrenOfType(const RenderElement&);
76
77// RenderChildIterator
78
79template <typename T>
80inline RenderChildIterator<T>::RenderChildIterator(const RenderElement& parent)
81 : RenderIterator<T>(&parent)
82{
83}
84
85template <typename T>
86inline RenderChildIterator<T>::RenderChildIterator(const RenderElement& parent, T* current)
87 : RenderIterator<T>(&parent, current)
88{
89}
90
91template <typename T>
92inline RenderChildIterator<T>& RenderChildIterator<T>::operator++()
93{
94 return static_cast<RenderChildIterator<T>&>(RenderIterator<T>::traverseNextSibling());
95}
96
97// RenderChildConstIterator
98
99template <typename T>
100inline RenderChildConstIterator<T>::RenderChildConstIterator(const RenderElement& parent)
101 : RenderConstIterator<T>(&parent)
102{
103}
104
105template <typename T>
106inline RenderChildConstIterator<T>::RenderChildConstIterator(const RenderElement& parent, const T* current)
107 : RenderConstIterator<T>(&parent, current)
108{
109}
110
111template <typename T>
112inline RenderChildConstIterator<T>& RenderChildConstIterator<T>::operator++()
113{
114 return static_cast<RenderChildConstIterator<T>&>(RenderConstIterator<T>::traverseNextSibling());
115}
116
117// RenderChildIteratorAdapter
118
119template <typename T>
120inline RenderChildIteratorAdapter<T>::RenderChildIteratorAdapter(RenderElement& parent)
121 : m_parent(parent)
122{
123}
124
125template <typename T>
126inline RenderChildIterator<T> RenderChildIteratorAdapter<T>::begin()
127{
128 return RenderChildIterator<T>(m_parent, RenderTraversal::firstChild<T>(m_parent));
129}
130
131template <typename T>
132inline RenderChildIterator<T> RenderChildIteratorAdapter<T>::end()
133{
134 return RenderChildIterator<T>(m_parent);
135}
136
137template <typename T>
138inline T* RenderChildIteratorAdapter<T>::first()
139{
140 return RenderTraversal::firstChild<T>(m_parent);
141}
142
143template <typename T>
144inline T* RenderChildIteratorAdapter<T>::last()
145{
146 return RenderTraversal::lastChild<T>(m_parent);
147}
148
149// RenderChildConstIteratorAdapter
150
151template <typename T>
152inline RenderChildConstIteratorAdapter<T>::RenderChildConstIteratorAdapter(const RenderElement& parent)
153 : m_parent(parent)
154{
155}
156
157template <typename T>
158inline RenderChildConstIterator<T> RenderChildConstIteratorAdapter<T>::begin() const
159{
160 return RenderChildConstIterator<T>(m_parent, RenderTraversal::firstChild<T>(m_parent));
161}
162
163template <typename T>
164inline RenderChildConstIterator<T> RenderChildConstIteratorAdapter<T>::end() const
165{
166 return RenderChildConstIterator<T>(m_parent);
167}
168
169template <typename T>
170inline const T* RenderChildConstIteratorAdapter<T>::first() const
171{
172 return RenderTraversal::firstChild<T>(m_parent);
173}
174
175template <typename T>
176inline const T* RenderChildConstIteratorAdapter<T>::last() const
177{
178 return RenderTraversal::lastChild<T>(m_parent);
179}
180
181// Standalone functions
182
183template <typename T>
184inline RenderChildIteratorAdapter<T> childrenOfType(RenderElement& parent)
185{
186 return RenderChildIteratorAdapter<T>(parent);
187}
188
189template <typename T>
190inline RenderChildConstIteratorAdapter<T> childrenOfType(const RenderElement& parent)
191{
192 return RenderChildConstIteratorAdapter<T>(parent);
193}
194
195} // namespace WebCore
196