Commit 9f05e9b3 authored by Yoshifumi Inoue's avatar Yoshifumi Inoue Committed by Commit Bot

Introduce FlatTreeTraversal:: AncestorsOf()

This patch introduces |FlatTreeTraversal::AncestorsOf()| as |NodeTraversal|.

This patch is a preparation of the patch[1].

Note: The patch[2] introduces |FlatTreeTraversal::InclusiveAncestorsOf()|.

[1] http://crrev.com/c/737981 Introduce TextOffsetMapping to simplify
word/paragraph selection
[2] http://crrev.com/c/742770 Introduce FlatTreeTraversal::InclusiveAncestorsOf

Bug: 778507
Change-Id: I5f76f00c06a4e02b5981600049af11853fa2f60b
Reviewed-on: https://chromium-review.googlesource.com/923443Reviewed-by: default avatarHayato Ito <hayato@chromium.org>
Commit-Queue: Yoshifumi Inoue <yosin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#537237}
parent f44f9ef6
...@@ -127,11 +127,12 @@ class CORE_EXPORT FlatTreeTraversal { ...@@ -127,11 +127,12 @@ class CORE_EXPORT FlatTreeTraversal {
// Flat tree range helper functions for range based for statement. // Flat tree range helper functions for range based for statement.
// TODO(dom-team): We should have following functions to match with // TODO(dom-team): We should have following functions to match with
// |NodeTraversal|: // |NodeTraversal|:
// - AncestorsOf()
// - DescendantsOf() // - DescendantsOf()
// - InclusiveDescendantsOf() // - InclusiveDescendantsOf()
// - StartsAt() // - StartsAt()
// - StartsAfter() // - StartsAfter()
static TraversalRange<TraversalAncestorsIterator<FlatTreeTraversal>>
AncestorsOf(const Node&);
static TraversalRange<TraversalChildrenIterator<FlatTreeTraversal>> static TraversalRange<TraversalChildrenIterator<FlatTreeTraversal>>
ChildrenOf(const Node&); ChildrenOf(const Node&);
...@@ -341,6 +342,11 @@ inline Node* FlatTreeTraversal::TraverseLastChild(const Node& node) { ...@@ -341,6 +342,11 @@ inline Node* FlatTreeTraversal::TraverseLastChild(const Node& node) {
} }
// TraverseRange<T> implementations // TraverseRange<T> implementations
inline TraversalRange<TraversalAncestorsIterator<FlatTreeTraversal>>
FlatTreeTraversal::AncestorsOf(const Node& node) {
return TraversalRange<TraversalAncestorsIterator<FlatTreeTraversal>>(&node);
}
inline TraversalRange<TraversalChildrenIterator<FlatTreeTraversal>> inline TraversalRange<TraversalChildrenIterator<FlatTreeTraversal>>
FlatTreeTraversal::ChildrenOf(const Node& parent) { FlatTreeTraversal::ChildrenOf(const Node& parent) {
return TraversalRange<TraversalChildrenIterator<FlatTreeTraversal>>(&parent); return TraversalRange<TraversalChildrenIterator<FlatTreeTraversal>>(&parent);
......
...@@ -320,6 +320,23 @@ TEST_F(FlatTreeTraversalTest, nextSkippingChildren) { ...@@ -320,6 +320,23 @@ TEST_F(FlatTreeTraversalTest, nextSkippingChildren) {
EXPECT_EQ(*m1, FlatTreeTraversal::PreviousSkippingChildren(*m2)); EXPECT_EQ(*m1, FlatTreeTraversal::PreviousSkippingChildren(*m2));
} }
TEST_F(FlatTreeTraversalTest, AncestorsOf) {
SetupDocumentTree("<div><div><div id=sample></div></div></div>");
Element* const sample = GetDocument().getElementById("sample");
HeapVector<Member<Node>> expected_nodes;
for (Node* parent = FlatTreeTraversal::Parent(*sample); parent;
parent = FlatTreeTraversal::Parent(*parent)) {
expected_nodes.push_back(parent);
}
HeapVector<Member<Node>> actual_nodes;
for (Node& ancestor : FlatTreeTraversal::AncestorsOf(*sample))
actual_nodes.push_back(&ancestor);
EXPECT_EQ(expected_nodes, actual_nodes);
}
TEST_F(FlatTreeTraversalTest, InclusiveAncestorsOf) { TEST_F(FlatTreeTraversalTest, InclusiveAncestorsOf) {
SetupDocumentTree("<div><div><div id=sample></div></div></div>"); SetupDocumentTree("<div><div><div id=sample></div></div></div>");
Element* const sample = GetDocument().getElementById("sample"); Element* const sample = GetDocument().getElementById("sample");
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment