Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / dom / LiveNodeListBase.h
index ecc1730..1724e97 100644 (file)
@@ -63,19 +63,13 @@ public:
     ContainerNode& rootNode() const;
 
     void didMoveToDocument(Document& oldDocument, Document& newDocument);
-    ALWAYS_INLINE bool hasIdNameCache() const { return !isLiveNodeListType(type()); }
     ALWAYS_INLINE bool isRootedAtDocument() const { return m_rootType == NodeListIsRootedAtDocument; }
     ALWAYS_INLINE NodeListInvalidationType invalidationType() const { return static_cast<NodeListInvalidationType>(m_invalidationType); }
     ALWAYS_INLINE CollectionType type() const { return static_cast<CollectionType>(m_collectionType); }
     ContainerNode& ownerNode() const { return *m_ownerNode; }
-    ALWAYS_INLINE void invalidateCache(const QualifiedName* attrName) const
-    {
-        if (!attrName || shouldInvalidateTypeOnAttributeChange(invalidationType(), *attrName))
-            invalidateCache();
-        else if (hasIdNameCache() && (*attrName == HTMLNames::idAttr || *attrName == HTMLNames::nameAttr))
-            invalidateIdNameCacheMaps();
-    }
+
     virtual void invalidateCache(Document* oldDocument = 0) const = 0;
+    void invalidateCacheForAttribute(const QualifiedName*) const;
 
     static bool shouldInvalidateTypeOnAttributeChange(NodeListInvalidationType, const QualifiedName&);
 
@@ -84,23 +78,20 @@ protected:
 
     ALWAYS_INLINE NodeListRootType rootType() const { return static_cast<NodeListRootType>(m_rootType); }
 
-    template <typename Collection>
-    static Element* itemBefore(const Collection&, const Element* previousItem);
     template <class NodeListType>
-    static Element* firstMatchingElement(const NodeListType&, const ContainerNode&);
+    static Element* firstMatchingElement(const NodeListType&);
+    template <class NodeListType>
+    static Element* lastMatchingElement(const NodeListType&);
+    template <class NodeListType>
+    static Element* nextMatchingElement(const NodeListType&, Element& current);
     template <class NodeListType>
-    static Element* nextMatchingElement(const NodeListType&, Element& current, const ContainerNode& root);
+    static Element* previousMatchingElement(const NodeListType&, Element& current);
     template <class NodeListType>
-    static Element* traverseMatchingElementsForwardToOffset(const NodeListType&, unsigned offset, Element& currentElement, unsigned& currentOffset, const ContainerNode& root);
+    static Element* traverseMatchingElementsForwardToOffset(const NodeListType&, unsigned offset, Element& currentElement, unsigned& currentOffset);
+    template <class NodeListType>
+    static Element* traverseMatchingElementsBackwardToOffset(const NodeListType&, unsigned offset, Element& currentElement, unsigned& currentOffset);
 
 private:
-    void invalidateIdNameCacheMaps() const;
-    template <typename Collection>
-    static Element* iterateForPreviousNode(const Collection&, Node* current);
-    static Node* previousNode(const ContainerNode&, const Node& previous, bool onlyIncludeDirectChildren);
-    static Node* lastDescendant(const ContainerNode&);
-    static Node* lastNode(const ContainerNode&, bool onlyIncludeDirectChildren);
-
     RefPtr<ContainerNode> m_ownerNode; // Cannot be null.
     const unsigned m_rootType : 1;
     const unsigned m_invalidationType : 4;
@@ -131,51 +122,20 @@ ALWAYS_INLINE bool LiveNodeListBase::shouldInvalidateTypeOnAttributeChange(NodeL
     return false;
 }
 
-inline Node* LiveNodeListBase::previousNode(const ContainerNode& base, const Node& previous, bool onlyIncludeDirectChildren)
-{
-    return onlyIncludeDirectChildren ? previous.previousSibling() : NodeTraversal::previous(previous, &base);
-}
-
-inline Node* LiveNodeListBase::lastDescendant(const ContainerNode& node)
+template <typename NodeListType>
+Element* LiveNodeListBase::lastMatchingElement(const NodeListType& nodeList)
 {
-    Node* descendant = node.lastChild();
-    for (Node* current = descendant; current; current = current->lastChild())
-        descendant = current;
-    return descendant;
-}
-
-inline Node* LiveNodeListBase::lastNode(const ContainerNode& rootNode, bool onlyIncludeDirectChildren)
-{
-    return onlyIncludeDirectChildren ? rootNode.lastChild() : lastDescendant(rootNode);
-}
-
-template <typename Collection>
-Element* LiveNodeListBase::iterateForPreviousNode(const Collection& collection, Node* current)
-{
-    bool onlyIncludeDirectChildren = collection.shouldOnlyIncludeDirectChildren();
-    ContainerNode& rootNode = collection.rootNode();
-    for (; current; current = previousNode(rootNode, *current, onlyIncludeDirectChildren)) {
-        if (current->isElementNode() && isMatchingElement(collection, toElement(*current)))
-            return toElement(current);
-    }
-    return 0;
-}
-
-template <typename Collection>
-Element* LiveNodeListBase::itemBefore(const Collection& collection, const Element* previous)
-{
-    Node* current;
-    if (LIKELY(!!previous)) // Without this LIKELY, length() and item() can be 10% slower.
-        current = previousNode(collection.rootNode(), *previous, collection.shouldOnlyIncludeDirectChildren());
-    else
-        current = lastNode(collection.rootNode(), collection.shouldOnlyIncludeDirectChildren());
-
-    return iterateForPreviousNode(collection, current);
+    ContainerNode& root = nodeList.rootNode();
+    Element* element = ElementTraversal::lastWithin(root);
+    while (element && !isMatchingElement(nodeList, *element))
+        element = ElementTraversal::previous(*element, &root);
+    return element;
 }
 
 template <class NodeListType>
-Element* LiveNodeListBase::firstMatchingElement(const NodeListType& nodeList, const ContainerNode& root)
+Element* LiveNodeListBase::firstMatchingElement(const NodeListType& nodeList)
 {
+    ContainerNode& root = nodeList.rootNode();
     Element* element = ElementTraversal::firstWithin(root);
     while (element && !isMatchingElement(nodeList, *element))
         element = ElementTraversal::next(*element, &root);
@@ -183,8 +143,9 @@ Element* LiveNodeListBase::firstMatchingElement(const NodeListType& nodeList, co
 }
 
 template <class NodeListType>
-Element* LiveNodeListBase::nextMatchingElement(const NodeListType& nodeList, Element& current, const ContainerNode& root)
+Element* LiveNodeListBase::nextMatchingElement(const NodeListType& nodeList, Element& current)
 {
+    ContainerNode& root = nodeList.rootNode();
     Element* next = &current;
     do {
         next = ElementTraversal::next(*next, &root);
@@ -193,17 +154,40 @@ Element* LiveNodeListBase::nextMatchingElement(const NodeListType& nodeList, Ele
 }
 
 template <class NodeListType>
-Element* LiveNodeListBase::traverseMatchingElementsForwardToOffset(const NodeListType& nodeList, unsigned offset, Element& currentElement, unsigned& currentOffset, const ContainerNode& root)
+Element* LiveNodeListBase::previousMatchingElement(const NodeListType& nodeList, Element& current)
+{
+    ContainerNode& root = nodeList.rootNode();
+    Element* previous = &current;
+    do {
+        previous = ElementTraversal::previous(*previous, &root);
+    } while (previous && !isMatchingElement(nodeList, *previous));
+    return previous;
+}
+
+template <class NodeListType>
+Element* LiveNodeListBase::traverseMatchingElementsForwardToOffset(const NodeListType& nodeList, unsigned offset, Element& currentElement, unsigned& currentOffset)
 {
     ASSERT(currentOffset < offset);
     Element* next = &currentElement;
-    while ((next = nextMatchingElement(nodeList, *next, root))) {
+    while ((next = nextMatchingElement(nodeList, *next))) {
         if (++currentOffset == offset)
             return next;
     }
     return 0;
 }
 
+template <class NodeListType>
+Element* LiveNodeListBase::traverseMatchingElementsBackwardToOffset(const NodeListType& nodeList, unsigned offset, Element& currentElement, unsigned& currentOffset)
+{
+    ASSERT(currentOffset > offset);
+    Element* previous = &currentElement;
+    while ((previous = previousMatchingElement(nodeList, *previous))) {
+        if (--currentOffset == offset)
+            return previous;
+    }
+    return 0;
+}
+
 } // namespace WebCore
 
 #endif // LiveNodeListBase_h