1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
6 #include "core/editing/Position.h"
8 #include "core/editing/EditingTestBase.h"
12 class PositionTest
: public EditingTestBase
{
15 TEST_F(PositionTest
, NodeAsRangeLastNodeNull
)
17 EXPECT_EQ(nullptr, Position().nodeAsRangeLastNode());
18 EXPECT_EQ(nullptr, PositionInComposedTree().nodeAsRangeLastNode());
21 TEST_F(PositionTest
, NodeAsRangeLastNode
)
23 const char* bodyContent
= "<p id='p1'>11</p><p id='p2'></p><p id='p3'>33</p>";
24 setBodyContent(bodyContent
);
25 Node
* p1
= document().getElementById("p1");
26 Node
* p2
= document().getElementById("p2");
27 Node
* p3
= document().getElementById("p3");
28 Node
* body
= EditingStrategy::parent(*p1
);
29 Node
* t1
= EditingStrategy::firstChild(*p1
);
30 Node
* t3
= EditingStrategy::firstChild(*p3
);
32 EXPECT_EQ(body
, Position::inParentBeforeNode(*p1
).nodeAsRangeLastNode());
33 EXPECT_EQ(t1
, Position::inParentBeforeNode(*p2
).nodeAsRangeLastNode());
34 EXPECT_EQ(p2
, Position::inParentBeforeNode(*p3
).nodeAsRangeLastNode());
35 EXPECT_EQ(t1
, Position::inParentAfterNode(*p1
).nodeAsRangeLastNode());
36 EXPECT_EQ(p2
, Position::inParentAfterNode(*p2
).nodeAsRangeLastNode());
37 EXPECT_EQ(t3
, Position::inParentAfterNode(*p3
).nodeAsRangeLastNode());
38 EXPECT_EQ(t3
, Position::afterNode(p3
).nodeAsRangeLastNode());
40 EXPECT_EQ(body
, PositionInComposedTree::inParentBeforeNode(*p1
).nodeAsRangeLastNode());
41 EXPECT_EQ(t1
, PositionInComposedTree::inParentBeforeNode(*p2
).nodeAsRangeLastNode());
42 EXPECT_EQ(p2
, PositionInComposedTree::inParentBeforeNode(*p3
).nodeAsRangeLastNode());
43 EXPECT_EQ(t1
, PositionInComposedTree::inParentAfterNode(*p1
).nodeAsRangeLastNode());
44 EXPECT_EQ(p2
, PositionInComposedTree::inParentAfterNode(*p2
).nodeAsRangeLastNode());
45 EXPECT_EQ(t3
, PositionInComposedTree::inParentAfterNode(*p3
).nodeAsRangeLastNode());
46 EXPECT_EQ(t3
, PositionInComposedTree::afterNode(p3
).nodeAsRangeLastNode());
49 TEST_F(PositionTest
, NodeAsRangeLastNodeShadow
)
51 const char* bodyContent
= "<p id='host'>00<b id='one'>11</b><b id='two'>22</b>33</p>";
52 const char* shadowContent
= "<a id='a'><content select=#two></content><content select=#one></content></a>";
53 setBodyContent(bodyContent
);
54 RefPtrWillBeRawPtr
<ShadowRoot
> shadowRoot
= setShadowContent(shadowContent
, "host");
56 Node
* host
= document().getElementById("host");
57 Node
* n1
= document().getElementById("one");
58 Node
* n2
= document().getElementById("two");
59 Node
* t0
= EditingStrategy::firstChild(*host
);
60 Node
* t1
= EditingStrategy::firstChild(*n1
);
61 Node
* t2
= EditingStrategy::firstChild(*n2
);
62 Node
* t3
= EditingStrategy::lastChild(*host
);
63 Node
* a
= shadowRoot
->getElementById("a");
65 EXPECT_EQ(t0
, Position::inParentBeforeNode(*n1
).nodeAsRangeLastNode());
66 EXPECT_EQ(t1
, Position::inParentBeforeNode(*n2
).nodeAsRangeLastNode());
67 EXPECT_EQ(t1
, Position::inParentAfterNode(*n1
).nodeAsRangeLastNode());
68 EXPECT_EQ(t2
, Position::inParentAfterNode(*n2
).nodeAsRangeLastNode());
69 EXPECT_EQ(t3
, Position::afterNode(host
).nodeAsRangeLastNode());
71 EXPECT_EQ(t2
, PositionInComposedTree::inParentBeforeNode(*n1
).nodeAsRangeLastNode());
72 EXPECT_EQ(a
, PositionInComposedTree::inParentBeforeNode(*n2
).nodeAsRangeLastNode());
73 EXPECT_EQ(t1
, PositionInComposedTree::inParentAfterNode(*n1
).nodeAsRangeLastNode());
74 EXPECT_EQ(t2
, PositionInComposedTree::inParentAfterNode(*n2
).nodeAsRangeLastNode());
75 EXPECT_EQ(t1
, PositionInComposedTree::afterNode(host
).nodeAsRangeLastNode());
78 TEST_F(PositionTest
, ToPositionInComposedTreeWithActiveInsertionPoint
)
80 const char* bodyContent
= "<p id='host'>00<b id='one'>11</b>22</p>";
81 const char* shadowContent
= "<a id='a'><content select=#one id='content'></content><content></content></a>";
82 setBodyContent(bodyContent
);
83 RefPtrWillBeRawPtr
<ShadowRoot
> shadowRoot
= setShadowContent(shadowContent
, "host");
84 RefPtrWillBeRawPtr
<Element
> anchor
= shadowRoot
->getElementById("a");
86 EXPECT_EQ(PositionInComposedTree(anchor
.get(), 0), toPositionInComposedTree(Position(anchor
.get(), 0)));
87 EXPECT_EQ(PositionInComposedTree(anchor
.get(), 1), toPositionInComposedTree(Position(anchor
.get(), 1)));
88 EXPECT_EQ(PositionInComposedTree(anchor
, PositionAnchorType::AfterChildren
), toPositionInComposedTree(Position(anchor
.get(), 2)));
91 TEST_F(PositionTest
, ToPositionInComposedTreeWithInactiveInsertionPoint
)
93 const char* bodyContent
= "<p id='p'><content></content></p>";
94 setBodyContent(bodyContent
);
95 RefPtrWillBeRawPtr
<Element
> anchor
= document().getElementById("p");
97 EXPECT_EQ(PositionInComposedTree(anchor
.get(), 0), toPositionInComposedTree(Position(anchor
.get(), 0)));
98 EXPECT_EQ(PositionInComposedTree(anchor
, PositionAnchorType::AfterChildren
), toPositionInComposedTree(Position(anchor
.get(), 1)));
101 TEST_F(PositionTest
, ToPositionInComposedTreeWithShadowRoot
)
103 const char* bodyContent
= "<p id='host'>00<b id='one'>11</b>22</p>";
104 const char* shadowContent
= "<a><content select=#one></content></a>";
105 setBodyContent(bodyContent
);
106 RefPtrWillBeRawPtr
<ShadowRoot
> shadowRoot
= setShadowContent(shadowContent
, "host");
107 RefPtrWillBeRawPtr
<Element
> host
= document().getElementById("host");
109 EXPECT_EQ(PositionInComposedTree(host
.get(), 0), toPositionInComposedTree(Position(shadowRoot
.get(), 0)));
110 EXPECT_EQ(PositionInComposedTree(host
.get(), PositionAnchorType::AfterChildren
), toPositionInComposedTree(Position(shadowRoot
.get(), 1)));
113 TEST_F(PositionTest
, ToPositionInComposedTreeWithShadowRootContainingSingleContent
)
115 const char* bodyContent
= "<p id='host'>00<b id='one'>11</b>22</p>";
116 const char* shadowContent
= "<content select=#one></content>";
117 setBodyContent(bodyContent
);
118 RefPtrWillBeRawPtr
<ShadowRoot
> shadowRoot
= setShadowContent(shadowContent
, "host");
119 RefPtrWillBeRawPtr
<Element
> host
= document().getElementById("host");
121 EXPECT_EQ(PositionInComposedTree(host
.get(), 0), toPositionInComposedTree(Position(shadowRoot
.get(), 0)));
122 EXPECT_EQ(PositionInComposedTree(host
.get(), PositionAnchorType::AfterChildren
), toPositionInComposedTree(Position(shadowRoot
.get(), 1)));
125 TEST_F(PositionTest
, ToPositionInComposedTreeWithEmptyShadowRoot
)
127 const char* bodyContent
= "<p id='host'>00<b id='one'>11</b>22</p>";
128 const char* shadowContent
= "";
129 setBodyContent(bodyContent
);
130 RefPtrWillBeRawPtr
<ShadowRoot
> shadowRoot
= setShadowContent(shadowContent
, "host");
131 RefPtrWillBeRawPtr
<Element
> host
= document().getElementById("host");
133 EXPECT_EQ(PositionInComposedTree(host
, PositionAnchorType::AfterChildren
), toPositionInComposedTree(Position(shadowRoot
.get(), 0)));