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

Unified Diff: Source/core/dom/ElementTraversal.h

Issue 201293002: Add Traversal<*Element>::firstAncestor() API (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Add firstAncestorOrSelf() Created 6 years, 9 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
« no previous file with comments | « Source/core/accessibility/AXNodeObject.cpp ('k') | Source/core/editing/FrameSelection.cpp » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: Source/core/dom/ElementTraversal.h
diff --git a/Source/core/dom/ElementTraversal.h b/Source/core/dom/ElementTraversal.h
index 6705f04efcd20b8bf281258b366a92797c59998a..e6d7e924ed65168990537f355f2588727990de9f 100644
--- a/Source/core/dom/ElementTraversal.h
+++ b/Source/core/dom/ElementTraversal.h
@@ -40,6 +40,11 @@ public:
static ElementType* lastChild(const ContainerNode& current) { return lastChildTemplate(current); }
static ElementType* lastChild(const Node& current) { return lastChildTemplate(current); }
+ // First ElementType ancestor of the node.
+ static ElementType* firstAncestor(const Node& current);
+ static ElementType* firstAncestorOrSelf(Node& current) { return firstAncestorOrSelfTemplate(current); }
+ static ElementType* firstAncestorOrSelf(Element& current) { return firstAncestorOrSelfTemplate(current); }
+
// First or last ElementType descendant of the node.
// For Elements firstWithin() is always the same as firstChild().
static ElementType* firstWithin(const ContainerNode& current) { return firstWithinTemplate(current); }
@@ -81,6 +86,8 @@ private:
template <class NodeType>
static ElementType* lastChildTemplate(NodeType&);
template <class NodeType>
+ static ElementType* firstAncestorOrSelfTemplate(NodeType&);
+ template <class NodeType>
static ElementType* firstWithinTemplate(NodeType&);
template <class NodeType>
static ElementType* lastWithinTemplate(NodeType&);
@@ -167,6 +174,24 @@ inline ElementType* Traversal<ElementType>::firstChildTemplate(NodeType& current
}
template <class ElementType>
+inline ElementType* Traversal<ElementType>::firstAncestor(const Node& current)
+{
+ ContainerNode* ancestor = current.parentNode();
+ while (ancestor && !isElementOfType<const ElementType>(*ancestor))
+ ancestor = ancestor->parentNode();
+ return toElement<ElementType>(ancestor);
+}
+
+template <class ElementType>
+template <class NodeType>
+inline ElementType* Traversal<ElementType>::firstAncestorOrSelfTemplate(NodeType& current)
+{
+ if (isElementOfType<const ElementType>(current))
+ return &toElement<ElementType>(current);
+ return firstAncestor(current);
+}
+
+template <class ElementType>
template <class NodeType>
inline ElementType* Traversal<ElementType>::lastChildTemplate(NodeType& current)
{
« no previous file with comments | « Source/core/accessibility/AXNodeObject.cpp ('k') | Source/core/editing/FrameSelection.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698