Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1036)

Unified Diff: Source/core/html/HTMLCollection.cpp

Issue 229213002: Make HTMLCollection / NodeList backward traversal consistent with forward one (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: Source/core/html/HTMLCollection.cpp
diff --git a/Source/core/html/HTMLCollection.cpp b/Source/core/html/HTMLCollection.cpp
index 8a81afed942ae52b576c675e400d6847f908be28..b66fa9d26d5ea5003523c4402deb4ef231e56878 100644
--- a/Source/core/html/HTMLCollection.cpp
+++ b/Source/core/html/HTMLCollection.cpp
@@ -276,11 +276,6 @@ template <> inline bool isMatchingElement(const HTMLTagCollection& collection, c
return collection.elementMatches(element);
}
-Element* HTMLCollection::itemBefore(const Element* previous) const
-{
- return LiveNodeListBase::itemBefore(*this, previous);
-}
-
Element* HTMLCollection::virtualItemAfter(Element*) const
{
ASSERT_NOT_REACHED();
@@ -314,6 +309,14 @@ inline Element* firstMatchingChildElement(const HTMLCollection& nodeList)
return element;
}
+inline Element* lastMatchingChildElement(const HTMLCollection& nodeList)
+{
+ Element* element = ElementTraversal::lastChild(nodeList.rootNode());
+ while (element && !isMatchingElement(nodeList, *element))
+ element = ElementTraversal::previousSibling(*element);
+ return element;
+}
+
inline Element* nextMatchingChildElement(const HTMLCollection& nodeList, Element& current)
{
Element* next = &current;
@@ -323,6 +326,15 @@ inline Element* nextMatchingChildElement(const HTMLCollection& nodeList, Element
return next;
}
+inline Element* previousMatchingChildElement(const HTMLCollection& nodeList, Element& current)
+{
+ Element* previous = &current;
+ do {
+ previous = ElementTraversal::previousSibling(*previous);
+ } while (previous && !isMatchingElement(nodeList, *previous));
+ return previous;
+}
+
Element* HTMLCollection::traverseToFirstElement() const
{
switch (type()) {
@@ -339,6 +351,14 @@ Element* HTMLCollection::traverseToFirstElement() const
}
}
+Element* HTMLCollection::traverseToLastElement() const
+{
+ ASSERT(canTraverseBackward());
+ if (shouldOnlyIncludeDirectChildren())
+ return lastMatchingChildElement(*this);
+ return lastMatchingElement(*this);
+}
+
inline Element* HTMLCollection::traverseNextElement(Element& previous) const
{
if (overridesItemAfter())
@@ -377,6 +397,21 @@ Element* HTMLCollection::traverseForwardToOffset(unsigned offset, Element& curre
}
}
+Element* HTMLCollection::traverseBackwardToOffset(unsigned offset, Element& currentElement, unsigned& currentOffset) const
+{
+ ASSERT(currentOffset > offset);
+ ASSERT(canTraverseBackward());
+ if (shouldOnlyIncludeDirectChildren()) {
Inactive 2014/04/09 23:08:58 Now we do this check once and then traverse to the
+ Element* previous = &currentElement;
+ while ((previous = previousMatchingChildElement(*this, *previous))) {
+ if (--currentOffset == offset)
+ return previous;
+ }
+ return 0;
+ }
+ return traverseMatchingElementsBackwardToOffset(*this, offset, currentElement, currentOffset);
+}
+
Element* HTMLCollection::namedItem(const AtomicString& name) const
{
// http://msdn.microsoft.com/workshop/author/dhtml/reference/methods/nameditem.asp
« Source/core/html/CollectionIndexCache.h ('K') | « Source/core/html/HTMLCollection.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698