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&);
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;
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);
}
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 = ¤t;
do {
next = ElementTraversal::next(*next, &root);
}
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 = ¤t;
+ 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 = ¤tElement;
- 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 = ¤tElement;
+ while ((previous = previousMatchingElement(nodeList, *previous))) {
+ if (--currentOffset == offset)
+ return previous;
+ }
+ return 0;
+}
+
} // namespace WebCore
#endif // LiveNodeListBase_h