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.
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.
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.
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.
29 #ifndef SiblingTraversalStrategies_h
30 #define SiblingTraversalStrategies_h
32 #include "core/dom/Element.h"
33 #include "core/rendering/style/RenderStyle.h"
34 #include "wtf/UnusedParam.h"
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;
44 int countElementsBefore(Element*) const;
45 int countElementsAfter(Element*) const;
46 int countElementsOfTypeBefore(Element*, const QualifiedName&) const;
47 int countElementsOfTypeAfter(Element*, const QualifiedName&) const;
50 inline bool DOMSiblingTraversalStrategy::isFirstChild(Element* element) const
52 return !element->previousElementSibling();
55 inline bool DOMSiblingTraversalStrategy::isLastChild(Element* element) const
57 return !element->nextElementSibling();
60 inline bool DOMSiblingTraversalStrategy::isFirstOfType(Element* element, const QualifiedName& type) const
62 for (const Element* sibling = element->previousElementSibling(); sibling; sibling = sibling->previousElementSibling()) {
63 if (sibling->hasTagName(type))
69 inline bool DOMSiblingTraversalStrategy::isLastOfType(Element* element, const QualifiedName& type) const
71 for (const Element* sibling = element->nextElementSibling(); sibling; sibling = sibling->nextElementSibling()) {
72 if (sibling->hasTagName(type))
78 inline int DOMSiblingTraversalStrategy::countElementsBefore(Element* element) const
81 for (const Element* sibling = element->previousElementSibling(); sibling; sibling = sibling->previousElementSibling())
87 inline int DOMSiblingTraversalStrategy::countElementsOfTypeBefore(Element* element, const QualifiedName& type) const
90 for (const Element* sibling = element->previousElementSibling(); sibling; sibling = sibling->previousElementSibling()) {
91 if (sibling->hasTagName(type))
98 inline int DOMSiblingTraversalStrategy::countElementsAfter(Element* element) const
101 for (const Element* sibling = element->nextElementSibling(); sibling; sibling = sibling->nextElementSibling())
107 inline int DOMSiblingTraversalStrategy::countElementsOfTypeAfter(Element* element, const QualifiedName& type) const
110 for (const Element* sibling = element->nextElementSibling(); sibling; sibling = sibling->nextElementSibling()) {
111 if (sibling->hasTagName(type))
118 struct ShadowDOMSiblingTraversalStrategy {
119 ShadowDOMSiblingTraversalStrategy(const Vector<Node*, 32>& siblings, int nth)
120 : m_siblings(siblings)
125 bool isFirstChild(Element*) const;
126 bool isLastChild(Element*) const;
127 bool isFirstOfType(Element*, const QualifiedName&) const;
128 bool isLastOfType(Element*, const QualifiedName&) const;
130 int countElementsBefore(Element*) const;
131 int countElementsAfter(Element*) const;
132 int countElementsOfTypeBefore(Element*, const QualifiedName&) const;
133 int countElementsOfTypeAfter(Element*, const QualifiedName&) const;
136 const Vector<Node*, 32>& m_siblings;
140 inline bool ShadowDOMSiblingTraversalStrategy::isFirstChild(Element* element) const
142 UNUSED_PARAM(element);
144 ASSERT(element == toElement(m_siblings[m_nth]));
146 for (int i = m_nth - 1; i >= 0; --i) {
147 if (m_siblings[i]->isElementNode())
154 inline bool ShadowDOMSiblingTraversalStrategy::isLastChild(Element* element) const
156 UNUSED_PARAM(element);
158 ASSERT(element == toElement(m_siblings[m_nth]));
160 for (size_t i = m_nth + 1; i < m_siblings.size(); ++i) {
161 if (m_siblings[i]->isElementNode())
168 inline bool ShadowDOMSiblingTraversalStrategy::isFirstOfType(Element* element, const QualifiedName& type) const
170 UNUSED_PARAM(element);
172 ASSERT(element == toElement(m_siblings[m_nth]));
174 for (int i = m_nth - 1; i >= 0; --i) {
175 if (m_siblings[i]->hasTagName(type))
182 inline bool ShadowDOMSiblingTraversalStrategy::isLastOfType(Element* element, const QualifiedName& type) const
184 UNUSED_PARAM(element);
186 ASSERT(element == toElement(m_siblings[m_nth]));
188 for (size_t i = m_nth + 1; i < m_siblings.size(); ++i) {
189 if (m_siblings[i]->hasTagName(type))
196 inline int ShadowDOMSiblingTraversalStrategy::countElementsBefore(Element* element) const
198 UNUSED_PARAM(element);
200 ASSERT(element == toElement(m_siblings[m_nth]));
203 for (int i = m_nth - 1; i >= 0; --i) {
204 if (m_siblings[i]->isElementNode())
211 inline int ShadowDOMSiblingTraversalStrategy::countElementsAfter(Element* element) const
213 UNUSED_PARAM(element);
215 ASSERT(element == toElement(m_siblings[m_nth]));
218 for (size_t i = m_nth + 1; i < m_siblings.size(); ++i) {
219 if (m_siblings[i]->isElementNode())
226 inline int ShadowDOMSiblingTraversalStrategy::countElementsOfTypeBefore(Element* element, const QualifiedName& type) const
228 UNUSED_PARAM(element);
230 ASSERT(element == toElement(m_siblings[m_nth]));
233 for (int i = m_nth - 1; i >= 0; --i) {
234 if (m_siblings[i]->hasTagName(type))
241 inline int ShadowDOMSiblingTraversalStrategy::countElementsOfTypeAfter(Element* element, const QualifiedName& type) const
243 UNUSED_PARAM(element);
245 ASSERT(element == toElement(m_siblings[m_nth]));
248 for (size_t i = m_nth + 1; i < m_siblings.size(); ++i) {
249 if (m_siblings[i]->hasTagName(type))