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/EditingUtilities.h"
8 #include "core/editing/EditingTestBase.h"
12 class EditingUtilitiesTest
: public EditingTestBase
{
15 TEST_F(EditingUtilitiesTest
, directionOfEnclosingBlock
)
17 const char* bodyContent
= "<p id='host'><b id='one'></b><b id='two'>22</b></p>";
18 const char* shadowContent
= "<content select=#two></content><p dir=rtl><content select=#one></content><p>";
19 setBodyContent(bodyContent
);
20 setShadowContent(shadowContent
, "host");
21 updateLayoutAndStyleForPainting();
22 Node
* one
= document().getElementById("one");
24 EXPECT_EQ(LTR
, directionOfEnclosingBlock(Position(one
, 0)));
25 EXPECT_EQ(RTL
, directionOfEnclosingBlock(PositionInComposedTree(one
, 0)));
28 TEST_F(EditingUtilitiesTest
, firstEditablePositionAfterPositionInRoot
)
30 const char* bodyContent
= "<p id='host' contenteditable><b id='one'>1</b><b id='two'>22</b></p>";
31 const char* shadowContent
= "<content select=#two></content><content select=#one></content><b id='three'>333</b>";
32 setBodyContent(bodyContent
);
33 RefPtrWillBeRawPtr
<ShadowRoot
> shadowRoot
= setShadowContent(shadowContent
, "host");
34 updateLayoutAndStyleForPainting();
35 Element
* host
= document().getElementById("host");
36 Node
* one
= document().getElementById("one");
37 Node
* two
= document().getElementById("two");
38 Node
* three
= shadowRoot
->getElementById("three");
40 EXPECT_EQ(Position(one
, 0), firstEditablePositionAfterPositionInRoot(Position(one
, 0), host
));
41 EXPECT_EQ(Position(one
->firstChild(), 0), firstEditableVisiblePositionAfterPositionInRoot(Position(one
, 0), host
).deepEquivalent());
43 EXPECT_EQ(PositionInComposedTree(one
, 0), firstEditablePositionAfterPositionInRoot(PositionInComposedTree(one
, 0), host
));
44 EXPECT_EQ(PositionInComposedTree(two
->firstChild(), 2), firstEditableVisiblePositionAfterPositionInRoot(PositionInComposedTree(one
, 0), host
).deepEquivalent());
46 EXPECT_EQ(Position::firstPositionInNode(host
), firstEditablePositionAfterPositionInRoot(Position(three
, 0), host
));
47 EXPECT_EQ(Position(one
->firstChild(), 0), firstEditableVisiblePositionAfterPositionInRoot(Position(three
, 0), host
).deepEquivalent());
48 EXPECT_EQ(PositionInComposedTree::afterNode(host
), firstEditablePositionAfterPositionInRoot(PositionInComposedTree(three
, 0), host
));
49 EXPECT_EQ(PositionInComposedTree::lastPositionInNode(host
), firstEditableVisiblePositionAfterPositionInRoot(PositionInComposedTree(three
, 0), host
).deepEquivalent());
52 TEST_F(EditingUtilitiesTest
, enclosingBlock
)
54 const char* bodyContent
= "<p id='host'><b id='one'>11</b></p>";
55 const char* shadowContent
= "<content select=#two></content><div id='three'><content select=#one></content></div>";
56 setBodyContent(bodyContent
);
57 RefPtrWillBeRawPtr
<ShadowRoot
> shadowRoot
= setShadowContent(shadowContent
, "host");
58 updateLayoutAndStyleForPainting();
59 Node
* host
= document().getElementById("host");
60 Node
* one
= document().getElementById("one");
61 Node
* three
= shadowRoot
->getElementById("three");
63 EXPECT_EQ(host
, enclosingBlock(Position(one
, 0), CannotCrossEditingBoundary
));
64 EXPECT_EQ(three
, enclosingBlock(PositionInComposedTree(one
, 0), CannotCrossEditingBoundary
));
67 TEST_F(EditingUtilitiesTest
, enclosingNodeOfType
)
69 const char* bodyContent
= "<p id='host'><b id='one'>11</b></p>";
70 const char* shadowContent
= "<content select=#two></content><div id='three'><content select=#one></div></content>";
71 setBodyContent(bodyContent
);
72 RefPtrWillBeRawPtr
<ShadowRoot
> shadowRoot
= setShadowContent(shadowContent
, "host");
73 updateLayoutAndStyleForPainting();
74 Node
* host
= document().getElementById("host");
75 Node
* one
= document().getElementById("one");
76 Node
* three
= shadowRoot
->getElementById("three");
78 EXPECT_EQ(host
, enclosingNodeOfType(Position(one
, 0), isEnclosingBlock
));
79 EXPECT_EQ(three
, enclosingNodeOfType(PositionInComposedTree(one
, 0), isEnclosingBlock
));
82 TEST_F(EditingUtilitiesTest
, isFirstPositionAfterTable
)
84 const char* bodyContent
= "<div contenteditable id=host><table id=table><tr><td>1</td></tr></table><b id=two>22</b></div>";
85 const char* shadowContent
= "<content select=#two></content><content select=#table></content>";
86 setBodyContent(bodyContent
);
87 setShadowContent(shadowContent
, "host");
88 updateLayoutAndStyleForPainting();
89 Node
* host
= document().getElementById("host");
90 Node
* table
= document().getElementById("table");
92 EXPECT_EQ(table
, isFirstPositionAfterTable(createVisiblePosition(Position::afterNode(table
))));
93 EXPECT_EQ(table
, isFirstPositionAfterTable(createVisiblePosition(PositionInComposedTree::afterNode(table
))));
95 EXPECT_EQ(table
, isFirstPositionAfterTable(createVisiblePosition(Position::lastPositionInNode(table
))));
96 EXPECT_EQ(table
, isFirstPositionAfterTable(createVisiblePosition(PositionInComposedTree::lastPositionInNode(table
))));
98 EXPECT_EQ(nullptr, isFirstPositionAfterTable(createVisiblePosition(Position(host
, 2))));
99 EXPECT_EQ(table
, isFirstPositionAfterTable(createVisiblePosition(PositionInComposedTree(host
, 2))));
101 EXPECT_EQ(nullptr, isFirstPositionAfterTable(createVisiblePosition(Position::afterNode(host
))));
102 EXPECT_EQ(nullptr, isFirstPositionAfterTable(createVisiblePosition(PositionInComposedTree::afterNode(host
))));
104 EXPECT_EQ(nullptr, isFirstPositionAfterTable(createVisiblePosition(Position::lastPositionInNode(host
))));
105 EXPECT_EQ(table
, isFirstPositionAfterTable(createVisiblePosition(PositionInComposedTree::lastPositionInNode(host
))));
108 TEST_F(EditingUtilitiesTest
, lastEditablePositionBeforePositionInRoot
)
110 const char* bodyContent
= "<p id='host' contenteditable><b id='one'>1</b><b id='two'>22</b></p>";
111 const char* shadowContent
= "<content select=#two></content><content select=#one></content><b id='three'>333</b>";
112 setBodyContent(bodyContent
);
113 RefPtrWillBeRawPtr
<ShadowRoot
> shadowRoot
= setShadowContent(shadowContent
, "host");
114 updateLayoutAndStyleForPainting();
115 Element
* host
= document().getElementById("host");
116 Node
* one
= document().getElementById("one");
117 Node
* two
= document().getElementById("two");
118 Node
* three
= shadowRoot
->getElementById("three");
120 EXPECT_EQ(Position(one
, 0), lastEditablePositionBeforePositionInRoot(Position(one
, 0), host
));
121 EXPECT_EQ(Position(one
->firstChild(), 0), lastEditableVisiblePositionBeforePositionInRoot(Position(one
, 0), host
).deepEquivalent());
123 EXPECT_EQ(PositionInComposedTree(one
, 0), lastEditablePositionBeforePositionInRoot(PositionInComposedTree(one
, 0), host
));
124 EXPECT_EQ(PositionInComposedTree(two
->firstChild(), 2), lastEditableVisiblePositionBeforePositionInRoot(PositionInComposedTree(one
, 0), host
).deepEquivalent());
126 EXPECT_EQ(Position::firstPositionInNode(host
), lastEditablePositionBeforePositionInRoot(Position(three
, 0), host
));
127 EXPECT_EQ(Position(one
->firstChild(), 0), lastEditableVisiblePositionBeforePositionInRoot(Position(three
, 0), host
).deepEquivalent());
128 EXPECT_EQ(PositionInComposedTree::firstPositionInNode(host
), lastEditablePositionBeforePositionInRoot(PositionInComposedTree(three
, 0), host
));
129 EXPECT_EQ(PositionInComposedTree(two
->firstChild(), 0), lastEditableVisiblePositionBeforePositionInRoot(PositionInComposedTree(three
, 0), host
).deepEquivalent());
132 TEST_F(EditingUtilitiesTest
, NextNodeIndex
)
134 const char* bodyContent
= "<p id='host'>00<b id='one'>11</b><b id='two'>22</b>33</p>";
135 const char* shadowContent
= "<content select=#two></content><content select=#one></content>";
136 setBodyContent(bodyContent
);
137 setShadowContent(shadowContent
, "host");
138 Node
* host
= document().getElementById("host");
139 Node
* two
= document().getElementById("two");
141 EXPECT_EQ(Position(host
, 3), nextPositionOf(Position(two
, 2), PositionMoveType::CodePoint
));
142 EXPECT_EQ(PositionInComposedTree(host
, 1), nextPositionOf(PositionInComposedTree(two
, 2), PositionMoveType::CodePoint
));
145 TEST_F(EditingUtilitiesTest
, NextVisuallyDistinctCandidate
)
147 const char* bodyContent
= "<p id='host'>00<b id='one'>11</b><b id='two'>22</b><b id='three'>33</b></p>";
148 const char* shadowContent
= "<content select=#two></content><content select=#one></content><content select=#three></content>";
149 setBodyContent(bodyContent
);
150 setShadowContent(shadowContent
, "host");
151 updateLayoutAndStyleForPainting();
152 Node
* one
= document().getElementById("one");
153 Node
* two
= document().getElementById("two");
154 Node
* three
= document().getElementById("three");
156 EXPECT_EQ(Position(two
->firstChild(), 1), nextVisuallyDistinctCandidate(Position(one
, 1)));
157 EXPECT_EQ(PositionInComposedTree(three
->firstChild(), 1), nextVisuallyDistinctCandidate(PositionInComposedTree(one
, 1)));