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/dom/ElementTraversal.h"
34 #include "core/rendering/style/RenderStyle.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 !ElementTraversal::previousSibling(element);
55 inline bool DOMSiblingTraversalStrategy::isLastChild(Element& element) const
57 return !ElementTraversal::nextSibling(element);
60 inline bool DOMSiblingTraversalStrategy::isFirstOfType(Element& element, const QualifiedName& type) const
62 for (const Element* sibling = ElementTraversal::previousSibling(element); sibling; sibling = ElementTraversal::previousSibling(*sibling)) {
63 if (sibling->hasTagName(type))
69 inline bool DOMSiblingTraversalStrategy::isLastOfType(Element& element, const QualifiedName& type) const
71 for (const Element* sibling = ElementTraversal::nextSibling(element); sibling; sibling = ElementTraversal::nextSibling(*sibling)) {
72 if (sibling->hasTagName(type))
78 inline int DOMSiblingTraversalStrategy::countElementsBefore(Element& element) const
81 for (const Element* sibling = ElementTraversal::previousSibling(element); sibling; sibling = ElementTraversal::previousSibling(*sibling))
87 inline int DOMSiblingTraversalStrategy::countElementsOfTypeBefore(Element& element, const QualifiedName& type) const
90 for (const Element* sibling = ElementTraversal::previousSibling(element); sibling; sibling = ElementTraversal::previousSibling(*sibling)) {
91 if (sibling->hasTagName(type))
98 inline int DOMSiblingTraversalStrategy::countElementsAfter(Element& element) const
101 for (const Element* sibling = ElementTraversal::nextSibling(element); sibling; sibling = ElementTraversal::nextSibling(*sibling))
107 inline int DOMSiblingTraversalStrategy::countElementsOfTypeAfter(Element& element, const QualifiedName& type) const
110 for (const Element* sibling = ElementTraversal::nextSibling(element); sibling; sibling = ElementTraversal::nextSibling(*sibling)) {
111 if (sibling->hasTagName(type))
118 class ShadowDOMSiblingTraversalStrategy FINAL {
121 ShadowDOMSiblingTraversalStrategy(const WillBeHeapVector<RawPtrWillBeMember<Node>, 32>& siblings, int nth)
122 : m_siblings(siblings)
127 bool isFirstChild(Element&) const;
128 bool isLastChild(Element&) const;
129 bool isFirstOfType(Element&, const QualifiedName&) const;
130 bool isLastOfType(Element&, const QualifiedName&) const;
132 int countElementsBefore(Element&) const;
133 int countElementsAfter(Element&) const;
134 int countElementsOfTypeBefore(Element&, const QualifiedName&) const;
135 int countElementsOfTypeAfter(Element&, const QualifiedName&) const;
138 const WillBeHeapVector<RawPtrWillBeMember<Node>, 32>& m_siblings;
142 inline bool ShadowDOMSiblingTraversalStrategy::isFirstChild(Element& element) const
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 ASSERT(element == toElement(m_siblings[m_nth]));
158 for (size_t i = m_nth + 1; i < m_siblings.size(); ++i) {
159 if (m_siblings[i]->isElementNode())
166 inline bool ShadowDOMSiblingTraversalStrategy::isFirstOfType(Element& element, const QualifiedName& type) const
168 ASSERT(element == toElement(m_siblings[m_nth]));
170 for (int i = m_nth - 1; i >= 0; --i) {
171 if (m_siblings[i]->hasTagName(type))
178 inline bool ShadowDOMSiblingTraversalStrategy::isLastOfType(Element& element, const QualifiedName& type) const
180 ASSERT(element == toElement(m_siblings[m_nth]));
182 for (size_t i = m_nth + 1; i < m_siblings.size(); ++i) {
183 if (m_siblings[i]->hasTagName(type))
190 inline int ShadowDOMSiblingTraversalStrategy::countElementsBefore(Element& element) const
192 ASSERT(element == toElement(m_siblings[m_nth]));
195 for (int i = m_nth - 1; i >= 0; --i) {
196 if (m_siblings[i]->isElementNode())
203 inline int ShadowDOMSiblingTraversalStrategy::countElementsAfter(Element& element) const
205 ASSERT(element == toElement(m_siblings[m_nth]));
208 for (size_t i = m_nth + 1; i < m_siblings.size(); ++i) {
209 if (m_siblings[i]->isElementNode())
216 inline int ShadowDOMSiblingTraversalStrategy::countElementsOfTypeBefore(Element& element, const QualifiedName& type) const
218 ASSERT(element == toElement(m_siblings[m_nth]));
221 for (int i = m_nth - 1; i >= 0; --i) {
222 if (m_siblings[i]->hasTagName(type))
229 inline int ShadowDOMSiblingTraversalStrategy::countElementsOfTypeAfter(Element& element, const QualifiedName& type) const
231 ASSERT(element == toElement(m_siblings[m_nth]));
234 for (size_t i = m_nth + 1; i < m_siblings.size(); ++i) {
235 if (m_siblings[i]->hasTagName(type))