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

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

Issue 137433008: Have HTMLCollection::item() return an Element as per specification (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Update WebNodeCollection API Created 6 years, 11 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 8112c6ac4518127171854807fafbc36e8ef7a8bd..686f0b8db4f67da3a9c31a04f143032e578c3603 100644
--- a/Source/core/html/HTMLCollection.cpp
+++ b/Source/core/html/HTMLCollection.cpp
@@ -291,34 +291,40 @@ static Node* lastNode(const Node& rootNode, bool onlyIncludeDirectChildren)
return onlyIncludeDirectChildren ? rootNode.lastChild() : lastDescendant(rootNode);
}
-ALWAYS_INLINE Node* LiveNodeListBase::iterateForPreviousNode(Node* current) const
+template <typename Collection>
+ALWAYS_INLINE Element* LiveNodeListBase::iterateForPreviousNode(const Collection& collection, Node* current)
{
- bool onlyIncludeDirectChildren = shouldOnlyIncludeDirectChildren();
- CollectionType collectionType = type();
- Node& rootNode = this->rootNode();
+ bool onlyIncludeDirectChildren = collection.shouldOnlyIncludeDirectChildren();
+ Node& rootNode = collection.rootNode();
for (; current; current = previousNode(rootNode, *current, onlyIncludeDirectChildren)) {
- if (isLiveNodeListType(collectionType)) {
- if (current->isElementNode() && isMatchingElement(static_cast<const LiveNodeList&>(*this), toElement(*current)))
- return toElement(current);
- } else {
- if (current->isElementNode() && isMatchingElement(static_cast<const HTMLCollection&>(*this), toElement(*current)))
- return toElement(current);
- }
+ if (current->isElementNode() && isMatchingElement(collection, toElement(*current)))
+ return toElement(current);
}
return 0;
}
-Node* LiveNodeListBase::itemBefore(const Node* previous) const
+template <typename Collection>
+Element* LiveNodeListBase::itemBefore(const Collection& collection, const Node* previous)
{
Node* current;
if (LIKELY(!!previous)) // Without this LIKELY, length() and item() can be 10% slower.
- current = previousNode(rootNode(), *previous, shouldOnlyIncludeDirectChildren());
+ current = previousNode(collection.rootNode(), *previous, collection.shouldOnlyIncludeDirectChildren());
else
- current = lastNode(rootNode(), shouldOnlyIncludeDirectChildren());
+ current = lastNode(collection.rootNode(), collection.shouldOnlyIncludeDirectChildren());
+
+ return iterateForPreviousNode(collection, current);
+}
+Node* LiveNodeList::itemBefore(const Node* previous) const
+{
if (type() == ChildNodeListType)
- return current;
- return iterateForPreviousNode(current);
+ return LIKELY(!!previous) ? previous->previousSibling() : rootNode().lastChild();
+ return LiveNodeListBase::itemBefore(*this, previous);
+}
+
+Element* HTMLCollection::itemBefore(const Node* previous) const
+{
+ return LiveNodeListBase::itemBefore(*this, previous);
}
template <class NodeListType>
@@ -457,11 +463,11 @@ inline Element* HTMLCollection::traverseNextElement(Element& previous, const Con
return nextMatchingElement(*this, previous, root);
}
-Element* HTMLCollection::traverseForwardToOffset(unsigned offset, Node& currentElement, unsigned& currentOffset, const ContainerNode& root) const
+Element* HTMLCollection::traverseForwardToOffset(unsigned offset, Element& currentElement, unsigned& currentOffset, const ContainerNode& root) const
{
ASSERT(currentOffset < offset);
if (overridesItemAfter()) {
- Element* next = &toElement(currentElement);
+ Element* next = &currentElement;
while ((next = virtualItemAfter(next))) {
if (++currentOffset == offset)
return next;
@@ -469,14 +475,14 @@ Element* HTMLCollection::traverseForwardToOffset(unsigned offset, Node& currentE
return 0;
}
if (shouldOnlyIncludeDirectChildren()) {
- Element* next = &toElement(currentElement);
+ Element* next = &currentElement;
while ((next = nextMatchingChildElement(*this, *next, root))) {
if (++currentOffset == offset)
return next;
}
return 0;
}
- return traverseMatchingElementsForwardToOffset(*this, offset, toElement(currentElement), currentOffset, root);
+ return traverseMatchingElementsForwardToOffset(*this, offset, currentElement, currentOffset, root);
}
Element* HTMLCollection::namedItem(const AtomicString& name) const

Powered by Google App Engine
This is Rietveld 408576698