85d9742ee9a29512874fa2f9307f8eb140a22a79
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / css / SiblingTraversalStrategies.h
1 /*
2  * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3  *           (C) 2004-2005 Allan Sandfeld Jensen (kde@carewolf.com)
4  * Copyright (C) 2006, 2007 Nicholas Shanks (webkit@nickshanks.com)
5  * Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011 Apple Inc. All rights reserved.
6  * Copyright (C) 2007 Alexey Proskuryakov <ap@webkit.org>
7  * Copyright (C) 2007, 2008 Eric Seidel <eric@webkit.org>
8  * Copyright (C) 2008, 2009 Torch Mobile Inc. All rights reserved. (http://www.torchmobile.com/)
9  * Copyright (c) 2011, Code Aurora Forum. All rights reserved.
10  * Copyright (C) Research In Motion Limited 2011. All rights reserved.
11  * Copyright (C) 2012, Google, Inc. All rights reserved.
12  *
13  * This library is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU Library General Public
15  * License as published by the Free Software Foundation; either
16  * version 2 of the License, or (at your option) any later version.
17  *
18  * This library is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21  * Library General Public License for more details.
22  *
23  * You should have received a copy of the GNU Library General Public License
24  * along with this library; see the file COPYING.LIB.  If not, write to
25  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26  * Boston, MA 02110-1301, USA.
27  */
28
29 #ifndef SiblingTraversalStrategies_h
30 #define SiblingTraversalStrategies_h
31
32 #include "core/dom/Element.h"
33 #include "core/rendering/style/RenderStyle.h"
34 #include "wtf/UnusedParam.h"
35
36 namespace WebCore {
37
38 struct DOMSiblingTraversalStrategy {
39     bool isFirstChild(Element*) const;
40     bool isLastChild(Element*) const;
41     bool isFirstOfType(Element*, const QualifiedName&) const;
42     bool isLastOfType(Element*, const QualifiedName&) const;
43
44     int countElementsBefore(Element*) const;
45     int countElementsAfter(Element*) const;
46     int countElementsOfTypeBefore(Element*, const QualifiedName&) const;
47     int countElementsOfTypeAfter(Element*, const QualifiedName&) const;
48 };
49
50 inline bool DOMSiblingTraversalStrategy::isFirstChild(Element* element) const
51 {
52     return !element->previousElementSibling();
53 }
54
55 inline bool DOMSiblingTraversalStrategy::isLastChild(Element* element) const
56 {
57     return !element->nextElementSibling();
58 }
59
60 inline bool DOMSiblingTraversalStrategy::isFirstOfType(Element* element, const QualifiedName& type) const
61 {
62     for (const Element* sibling = element->previousElementSibling(); sibling; sibling = sibling->previousElementSibling()) {
63         if (sibling->hasTagName(type))
64             return false;
65     }
66     return true;
67 }
68
69 inline bool DOMSiblingTraversalStrategy::isLastOfType(Element* element, const QualifiedName& type) const
70 {
71     for (const Element* sibling = element->nextElementSibling(); sibling; sibling = sibling->nextElementSibling()) {
72         if (sibling->hasTagName(type))
73             return false;
74     }
75     return true;
76 }
77
78 inline int DOMSiblingTraversalStrategy::countElementsBefore(Element* element) const
79 {
80     int count = 0;
81     for (const Element* sibling = element->previousElementSibling(); sibling; sibling = sibling->previousElementSibling())
82         count++;
83
84     return count;
85 }
86
87 inline int DOMSiblingTraversalStrategy::countElementsOfTypeBefore(Element* element, const QualifiedName& type) const
88 {
89     int count = 0;
90     for (const Element* sibling = element->previousElementSibling(); sibling; sibling = sibling->previousElementSibling()) {
91         if (sibling->hasTagName(type))
92             ++count;
93     }
94
95     return count;
96 }
97
98 inline int DOMSiblingTraversalStrategy::countElementsAfter(Element* element) const
99 {
100     int count = 0;
101     for (const Element* sibling = element->nextElementSibling(); sibling; sibling = sibling->nextElementSibling())
102         ++count;
103
104     return count;
105 }
106
107 inline int DOMSiblingTraversalStrategy::countElementsOfTypeAfter(Element* element, const QualifiedName& type) const
108 {
109     int count = 0;
110     for (const Element* sibling = element->nextElementSibling(); sibling; sibling = sibling->nextElementSibling()) {
111         if (sibling->hasTagName(type))
112             ++count;
113     }
114
115     return count;
116 }
117
118 struct ShadowDOMSiblingTraversalStrategy {
119     ShadowDOMSiblingTraversalStrategy(const Vector<Node*, 32>& siblings, int nth)
120         : m_siblings(siblings)
121         , m_nth(nth)
122     {
123     }
124
125     bool isFirstChild(Element*) const;
126     bool isLastChild(Element*) const;
127     bool isFirstOfType(Element*, const QualifiedName&) const;
128     bool isLastOfType(Element*, const QualifiedName&) const;
129
130     int countElementsBefore(Element*) const;
131     int countElementsAfter(Element*) const;
132     int countElementsOfTypeBefore(Element*, const QualifiedName&) const;
133     int countElementsOfTypeAfter(Element*, const QualifiedName&) const;
134
135 private:
136     const Vector<Node*, 32>& m_siblings;
137     int m_nth;
138 };
139
140 inline bool ShadowDOMSiblingTraversalStrategy::isFirstChild(Element* element) const
141 {
142     UNUSED_PARAM(element);
143
144     ASSERT(element == toElement(m_siblings[m_nth]));
145
146     for (int i = m_nth - 1; i >= 0; --i) {
147         if (m_siblings[i]->isElementNode())
148             return false;
149     }
150
151     return true;
152 }
153
154 inline bool ShadowDOMSiblingTraversalStrategy::isLastChild(Element* element) const
155 {
156     UNUSED_PARAM(element);
157
158     ASSERT(element == toElement(m_siblings[m_nth]));
159
160     for (size_t i = m_nth + 1; i < m_siblings.size(); ++i) {
161         if (m_siblings[i]->isElementNode())
162             return false;
163     }
164
165     return true;
166 }
167
168 inline bool ShadowDOMSiblingTraversalStrategy::isFirstOfType(Element* element, const QualifiedName& type) const
169 {
170     UNUSED_PARAM(element);
171
172     ASSERT(element == toElement(m_siblings[m_nth]));
173
174     for (int i = m_nth - 1; i >= 0; --i) {
175         if (m_siblings[i]->hasTagName(type))
176             return false;
177     }
178
179     return true;
180 }
181
182 inline bool ShadowDOMSiblingTraversalStrategy::isLastOfType(Element* element, const QualifiedName& type) const
183 {
184     UNUSED_PARAM(element);
185
186     ASSERT(element == toElement(m_siblings[m_nth]));
187
188     for (size_t i = m_nth + 1; i < m_siblings.size(); ++i) {
189         if (m_siblings[i]->hasTagName(type))
190             return false;
191     }
192
193     return true;
194 }
195
196 inline int ShadowDOMSiblingTraversalStrategy::countElementsBefore(Element* element) const
197 {
198     UNUSED_PARAM(element);
199
200     ASSERT(element == toElement(m_siblings[m_nth]));
201
202     int count = 0;
203     for (int i = m_nth - 1; i >= 0; --i) {
204         if (m_siblings[i]->isElementNode())
205             ++count;
206     }
207
208     return count;
209 }
210
211 inline int ShadowDOMSiblingTraversalStrategy::countElementsAfter(Element* element) const
212 {
213     UNUSED_PARAM(element);
214
215     ASSERT(element == toElement(m_siblings[m_nth]));
216
217     int count = 0;
218     for (size_t i = m_nth + 1; i < m_siblings.size(); ++i) {
219         if (m_siblings[i]->isElementNode())
220             return ++count;
221     }
222
223     return count;
224 }
225
226 inline int ShadowDOMSiblingTraversalStrategy::countElementsOfTypeBefore(Element* element, const QualifiedName& type) const
227 {
228     UNUSED_PARAM(element);
229
230     ASSERT(element == toElement(m_siblings[m_nth]));
231
232     int count = 0;
233     for (int i = m_nth - 1; i >= 0; --i) {
234         if (m_siblings[i]->hasTagName(type))
235             ++count;
236     }
237
238     return count;
239 }
240
241 inline int ShadowDOMSiblingTraversalStrategy::countElementsOfTypeAfter(Element* element, const QualifiedName& type) const
242 {
243     UNUSED_PARAM(element);
244
245     ASSERT(element == toElement(m_siblings[m_nth]));
246
247     int count = 0;
248     for (size_t i = m_nth + 1; i < m_siblings.size(); ++i) {
249         if (m_siblings[i]->hasTagName(type))
250             return ++count;
251     }
252
253     return count;
254 }
255
256 }
257
258 #endif