| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org) | 2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org) |
| 3 * (C) 2004-2005 Allan Sandfeld Jensen (kde@carewolf.com) | 3 * (C) 2004-2005 Allan Sandfeld Jensen (kde@carewolf.com) |
| 4 * Copyright (C) 2006, 2007 Nicholas Shanks (webkit@nickshanks.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. | 5 * Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011 Apple Inc. All rights
reserved. |
| 6 * Copyright (C) 2007 Alexey Proskuryakov <ap@webkit.org> | 6 * Copyright (C) 2007 Alexey Proskuryakov <ap@webkit.org> |
| 7 * Copyright (C) 2007, 2008 Eric Seidel <eric@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.t
orchmobile.com/) | 8 * Copyright (C) 2008, 2009 Torch Mobile Inc. All rights reserved. (http://www.t
orchmobile.com/) |
| 9 * Copyright (c) 2011, Code Aurora Forum. All rights reserved. | 9 * Copyright (c) 2011, Code Aurora Forum. All rights reserved. |
| 10 * Copyright (C) Research In Motion Limited 2011. All rights reserved. | 10 * Copyright (C) Research In Motion Limited 2011. All rights reserved. |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 * Boston, MA 02110-1301, USA. | 26 * Boston, MA 02110-1301, USA. |
| 27 */ | 27 */ |
| 28 | 28 |
| 29 #ifndef SiblingTraversalStrategies_h | 29 #ifndef SiblingTraversalStrategies_h |
| 30 #define SiblingTraversalStrategies_h | 30 #define SiblingTraversalStrategies_h |
| 31 | 31 |
| 32 #include "core/dom/Document.h" | 32 #include "core/dom/Document.h" |
| 33 #include "core/dom/Element.h" | 33 #include "core/dom/Element.h" |
| 34 #include "core/dom/ElementTraversal.h" | 34 #include "core/dom/ElementTraversal.h" |
| 35 #include "core/dom/NthIndexCache.h" | 35 #include "core/dom/NthIndexCache.h" |
| 36 #include "core/style/ComputedStyle.h" | |
| 37 | 36 |
| 38 namespace blink { | 37 namespace blink { |
| 39 | 38 |
| 39 // TODO(esprehn): Merge this into ElementTraversal. |
| 40 class DOMSiblingTraversalStrategy { | 40 class DOMSiblingTraversalStrategy { |
| 41 public: | 41 public: |
| 42 bool isFirstChild(Element&) const; | 42 static bool isFirstChild(Element&); |
| 43 bool isLastChild(Element&) const; | 43 static bool isLastChild(Element&); |
| 44 bool isFirstOfType(Element&, const QualifiedName&) const; | 44 static bool isFirstOfType(Element&, const QualifiedName&); |
| 45 bool isLastOfType(Element&, const QualifiedName&) const; | 45 static bool isLastOfType(Element&, const QualifiedName&); |
| 46 | 46 |
| 47 int countElementsBefore(Element&) const; | 47 static int countElementsBefore(Element&); |
| 48 int countElementsAfter(Element&) const; | 48 static int countElementsAfter(Element&); |
| 49 int countElementsOfTypeBefore(Element&, const QualifiedName&) const; | 49 static int countElementsOfTypeBefore(Element&, const QualifiedName&); |
| 50 int countElementsOfTypeAfter(Element&, const QualifiedName&) const; | 50 static int countElementsOfTypeAfter(Element&, const QualifiedName&); |
| 51 | 51 |
| 52 private: | 52 private: |
| 53 class HasTagName { | 53 class HasTagName { |
| 54 public: | 54 public: |
| 55 explicit HasTagName(const QualifiedName& tagName) : m_tagName(tagName) {
} | 55 explicit HasTagName(const QualifiedName& tagName) : m_tagName(tagName) {
} |
| 56 bool operator() (const Element& element) const { return element.hasTagNa
me(m_tagName); } | 56 bool operator() (const Element& element) const { return element.hasTagNa
me(m_tagName); } |
| 57 private: | 57 private: |
| 58 const QualifiedName& m_tagName; | 58 const QualifiedName& m_tagName; |
| 59 }; | 59 }; |
| 60 }; | 60 }; |
| 61 | 61 |
| 62 inline bool DOMSiblingTraversalStrategy::isFirstChild(Element& element) const | 62 inline bool DOMSiblingTraversalStrategy::isFirstChild(Element& element) |
| 63 { | 63 { |
| 64 return !ElementTraversal::previousSibling(element); | 64 return !ElementTraversal::previousSibling(element); |
| 65 } | 65 } |
| 66 | 66 |
| 67 inline bool DOMSiblingTraversalStrategy::isLastChild(Element& element) const | 67 inline bool DOMSiblingTraversalStrategy::isLastChild(Element& element) |
| 68 { | 68 { |
| 69 return !ElementTraversal::nextSibling(element); | 69 return !ElementTraversal::nextSibling(element); |
| 70 } | 70 } |
| 71 | 71 |
| 72 inline bool DOMSiblingTraversalStrategy::isFirstOfType(Element& element, const Q
ualifiedName& type) const | 72 inline bool DOMSiblingTraversalStrategy::isFirstOfType(Element& element, const Q
ualifiedName& type) |
| 73 { | 73 { |
| 74 return !ElementTraversal::previousSibling(element, HasTagName(type)); | 74 return !ElementTraversal::previousSibling(element, HasTagName(type)); |
| 75 } | 75 } |
| 76 | 76 |
| 77 inline bool DOMSiblingTraversalStrategy::isLastOfType(Element& element, const Qu
alifiedName& type) const | 77 inline bool DOMSiblingTraversalStrategy::isLastOfType(Element& element, const Qu
alifiedName& type) |
| 78 { | 78 { |
| 79 return !ElementTraversal::nextSibling(element, HasTagName(type)); | 79 return !ElementTraversal::nextSibling(element, HasTagName(type)); |
| 80 } | 80 } |
| 81 | 81 |
| 82 inline int DOMSiblingTraversalStrategy::countElementsBefore(Element& element) co
nst | 82 inline int DOMSiblingTraversalStrategy::countElementsBefore(Element& element) |
| 83 { | 83 { |
| 84 if (NthIndexCache* nthIndexCache = element.document().nthIndexCache()) | 84 if (NthIndexCache* nthIndexCache = element.document().nthIndexCache()) |
| 85 return nthIndexCache->nthChildIndex(element) - 1; | 85 return nthIndexCache->nthChildIndex(element) - 1; |
| 86 | 86 |
| 87 int count = 0; | 87 int count = 0; |
| 88 for (const Element* sibling = ElementTraversal::previousSibling(element); si
bling; sibling = ElementTraversal::previousSibling(*sibling)) | 88 for (const Element* sibling = ElementTraversal::previousSibling(element); si
bling; sibling = ElementTraversal::previousSibling(*sibling)) |
| 89 count++; | 89 count++; |
| 90 | 90 |
| 91 return count; | 91 return count; |
| 92 } | 92 } |
| 93 | 93 |
| 94 inline int DOMSiblingTraversalStrategy::countElementsOfTypeBefore(Element& eleme
nt, const QualifiedName& type) const | 94 inline int DOMSiblingTraversalStrategy::countElementsOfTypeBefore(Element& eleme
nt, const QualifiedName& type) |
| 95 { | 95 { |
| 96 int count = 0; | 96 int count = 0; |
| 97 for (const Element* sibling = ElementTraversal::previousSibling(element, Has
TagName(type)); sibling; sibling = ElementTraversal::previousSibling(*sibling, H
asTagName(type))) | 97 for (const Element* sibling = ElementTraversal::previousSibling(element, Has
TagName(type)); sibling; sibling = ElementTraversal::previousSibling(*sibling, H
asTagName(type))) |
| 98 ++count; | 98 ++count; |
| 99 return count; | 99 return count; |
| 100 } | 100 } |
| 101 | 101 |
| 102 inline int DOMSiblingTraversalStrategy::countElementsAfter(Element& element) con
st | 102 inline int DOMSiblingTraversalStrategy::countElementsAfter(Element& element) |
| 103 { | 103 { |
| 104 if (NthIndexCache* nthIndexCache = element.document().nthIndexCache()) | 104 if (NthIndexCache* nthIndexCache = element.document().nthIndexCache()) |
| 105 return nthIndexCache->nthLastChildIndex(element) - 1; | 105 return nthIndexCache->nthLastChildIndex(element) - 1; |
| 106 | 106 |
| 107 int count = 0; | 107 int count = 0; |
| 108 for (const Element* sibling = ElementTraversal::nextSibling(element); siblin
g; sibling = ElementTraversal::nextSibling(*sibling)) | 108 for (const Element* sibling = ElementTraversal::nextSibling(element); siblin
g; sibling = ElementTraversal::nextSibling(*sibling)) |
| 109 ++count; | 109 ++count; |
| 110 return count; | 110 return count; |
| 111 } | 111 } |
| 112 | 112 |
| 113 inline int DOMSiblingTraversalStrategy::countElementsOfTypeAfter(Element& elemen
t, const QualifiedName& type) const | 113 inline int DOMSiblingTraversalStrategy::countElementsOfTypeAfter(Element& elemen
t, const QualifiedName& type) |
| 114 { | 114 { |
| 115 int count = 0; | 115 int count = 0; |
| 116 for (const Element* sibling = ElementTraversal::nextSibling(element, HasTagN
ame(type)); sibling; sibling = ElementTraversal::nextSibling(*sibling, HasTagNam
e(type))) | 116 for (const Element* sibling = ElementTraversal::nextSibling(element, HasTagN
ame(type)); sibling; sibling = ElementTraversal::nextSibling(*sibling, HasTagNam
e(type))) |
| 117 ++count; | 117 ++count; |
| 118 return count; | 118 return count; |
| 119 } | 119 } |
| 120 | 120 |
| 121 class ShadowDOMSiblingTraversalStrategy final { | |
| 122 STACK_ALLOCATED(); | |
| 123 public: | |
| 124 ShadowDOMSiblingTraversalStrategy(const WillBeHeapVector<RawPtrWillBeMember<
Node>, 32>& siblings, int nth) | |
| 125 : m_siblings(siblings) | |
| 126 , m_nth(nth) | |
| 127 { | |
| 128 } | |
| 129 | |
| 130 bool isFirstChild(Element&) const; | |
| 131 bool isLastChild(Element&) const; | |
| 132 bool isFirstOfType(Element&, const QualifiedName&) const; | |
| 133 bool isLastOfType(Element&, const QualifiedName&) const; | |
| 134 | |
| 135 int countElementsBefore(Element&) const; | |
| 136 int countElementsAfter(Element&) const; | |
| 137 int countElementsOfTypeBefore(Element&, const QualifiedName&) const; | |
| 138 int countElementsOfTypeAfter(Element&, const QualifiedName&) const; | |
| 139 | |
| 140 private: | |
| 141 const WillBeHeapVector<RawPtrWillBeMember<Node>, 32>& m_siblings; | |
| 142 int m_nth; | |
| 143 }; | |
| 144 | |
| 145 inline bool ShadowDOMSiblingTraversalStrategy::isFirstChild(Element& element) co
nst | |
| 146 { | |
| 147 ASSERT(element == toElement(m_siblings[m_nth])); | |
| 148 | |
| 149 for (int i = m_nth - 1; i >= 0; --i) { | |
| 150 if (m_siblings[i]->isElementNode()) | |
| 151 return false; | |
| 152 } | |
| 153 | |
| 154 return true; | |
| 155 } | |
| 156 | |
| 157 inline bool ShadowDOMSiblingTraversalStrategy::isLastChild(Element& element) con
st | |
| 158 { | |
| 159 ASSERT(element == toElement(m_siblings[m_nth])); | |
| 160 | |
| 161 for (size_t i = m_nth + 1; i < m_siblings.size(); ++i) { | |
| 162 if (m_siblings[i]->isElementNode()) | |
| 163 return false; | |
| 164 } | |
| 165 | |
| 166 return true; | |
| 167 } | |
| 168 | |
| 169 inline bool ShadowDOMSiblingTraversalStrategy::isFirstOfType(Element& element, c
onst QualifiedName& type) const | |
| 170 { | |
| 171 ASSERT(element == toElement(m_siblings[m_nth])); | |
| 172 | |
| 173 for (int i = m_nth - 1; i >= 0; --i) { | |
| 174 if (m_siblings[i]->isElementNode() && toElement(m_siblings[i])->hasTagNa
me(type)) | |
| 175 return false; | |
| 176 } | |
| 177 | |
| 178 return true; | |
| 179 } | |
| 180 | |
| 181 inline bool ShadowDOMSiblingTraversalStrategy::isLastOfType(Element& element, co
nst QualifiedName& type) const | |
| 182 { | |
| 183 ASSERT(element == toElement(m_siblings[m_nth])); | |
| 184 | |
| 185 for (size_t i = m_nth + 1; i < m_siblings.size(); ++i) { | |
| 186 if (m_siblings[i]->isElementNode() && toElement(m_siblings[i])->hasTagNa
me(type)) | |
| 187 return false; | |
| 188 } | |
| 189 | |
| 190 return true; | |
| 191 } | |
| 192 | |
| 193 inline int ShadowDOMSiblingTraversalStrategy::countElementsBefore(Element& eleme
nt) const | |
| 194 { | |
| 195 ASSERT(element == toElement(m_siblings[m_nth])); | |
| 196 | |
| 197 int count = 0; | |
| 198 for (int i = m_nth - 1; i >= 0; --i) { | |
| 199 if (m_siblings[i]->isElementNode()) | |
| 200 ++count; | |
| 201 } | |
| 202 | |
| 203 return count; | |
| 204 } | |
| 205 | |
| 206 inline int ShadowDOMSiblingTraversalStrategy::countElementsAfter(Element& elemen
t) const | |
| 207 { | |
| 208 ASSERT(element == toElement(m_siblings[m_nth])); | |
| 209 | |
| 210 int count = 0; | |
| 211 for (size_t i = m_nth + 1; i < m_siblings.size(); ++i) { | |
| 212 if (m_siblings[i]->isElementNode()) | |
| 213 return ++count; | |
| 214 } | |
| 215 | |
| 216 return count; | |
| 217 } | |
| 218 | |
| 219 inline int ShadowDOMSiblingTraversalStrategy::countElementsOfTypeBefore(Element&
element, const QualifiedName& type) const | |
| 220 { | |
| 221 ASSERT(element == toElement(m_siblings[m_nth])); | |
| 222 | |
| 223 int count = 0; | |
| 224 for (int i = m_nth - 1; i >= 0; --i) { | |
| 225 if (m_siblings[i]->isElementNode() && toElement(m_siblings[i])->hasTagNa
me(type)) | |
| 226 ++count; | |
| 227 } | |
| 228 | |
| 229 return count; | |
| 230 } | |
| 231 | |
| 232 inline int ShadowDOMSiblingTraversalStrategy::countElementsOfTypeAfter(Element&
element, const QualifiedName& type) const | |
| 233 { | |
| 234 ASSERT(element == toElement(m_siblings[m_nth])); | |
| 235 | |
| 236 int count = 0; | |
| 237 for (size_t i = m_nth + 1; i < m_siblings.size(); ++i) { | |
| 238 if (m_siblings[i]->isElementNode() && toElement(m_siblings[i])->hasTagNa
me(type)) | |
| 239 return ++count; | |
| 240 } | |
| 241 | |
| 242 return count; | |
| 243 } | |
| 244 | |
| 245 } | 121 } |
| 246 | 122 |
| 247 #endif | 123 #endif |
| OLD | NEW |