OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "core/editing/EphemeralRange.h" | 5 #include "core/editing/EphemeralRange.h" |
6 | 6 |
7 #include "core/dom/Range.h" | 7 #include "core/dom/Range.h" |
8 #include "core/editing/EditingTestBase.h" | 8 #include "core/editing/EditingTestBase.h" |
9 #include <sstream> | 9 #include <sstream> |
10 | 10 |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
44 | 44 |
45 Range* EphemeralRangeTest::getBodyRange() const { | 45 Range* EphemeralRangeTest::getBodyRange() const { |
46 Range* range = Range::create(document()); | 46 Range* range = Range::create(document()); |
47 range->selectNode(document().body()); | 47 range->selectNode(document().body()); |
48 return range; | 48 return range; |
49 } | 49 } |
50 | 50 |
51 // Tests that |EphemeralRange::nodes()| will traverse the whole range exactly as | 51 // Tests that |EphemeralRange::nodes()| will traverse the whole range exactly as |
52 // |for (Node* n = firstNode(); n != pastLastNode(); n = Traversal::next(*n))| | 52 // |for (Node* n = firstNode(); n != pastLastNode(); n = Traversal::next(*n))| |
53 // does. | 53 // does. |
54 TEST_F(EphemeralRangeTest, rangeTraversal) { | 54 TEST_F(EphemeralRangeTest, rangeTraversalDOM) { |
55 const char* bodyContent = | 55 const char* bodyContent = |
56 "<p id='host'><b id='one'></b><b id='two'>22</b></p>"; | 56 "<p id='host'>" |
57 "<b id='zero'>0</b>" | |
58 "<b id='one'>1</b>" | |
59 "<b id='two'>22</b>" | |
60 "<span id='three'>" | |
Xiaocheng
2017/03/10 01:27:15
Could you add a closing tag to this <span>, and ma
tanvir
2017/03/10 10:14:29
Done.
| |
61 "</p>"; | |
57 setBodyContent(bodyContent); | 62 setBodyContent(bodyContent); |
58 | 63 |
59 const std::string expectedNodes( | 64 const std::string expectedNodes( |
60 "[BODY][P id=\"host\"][B id=\"one\"][B id=\"two\"][#text \"22\"]"); | 65 "[BODY][P id=\"host\"][B id=\"zero\"][#text \"0\"][B id=\"one\"][#text " |
66 "\"1\"][B id=\"two\"][#text \"22\"][SPAN id=\"three\"]"); | |
61 | 67 |
62 // Check two ways to traverse. | 68 // Check two ways to traverse. |
63 EXPECT_EQ(expectedNodes, traverseRange<>(getBodyRange())); | 69 EXPECT_EQ(expectedNodes, traverseRange<>(getBodyRange())); |
64 EXPECT_EQ(traverseRange<>(getBodyRange()), | 70 EXPECT_EQ(traverseRange<>(getBodyRange()), |
65 traverseRange(EphemeralRange(getBodyRange()))); | 71 traverseRange(EphemeralRange(getBodyRange()))); |
66 | 72 |
67 // The same with FlatTree traversing. | |
68 EXPECT_EQ(expectedNodes, traverseRange<FlatTreeTraversal>(getBodyRange())); | 73 EXPECT_EQ(expectedNodes, traverseRange<FlatTreeTraversal>(getBodyRange())); |
69 EXPECT_EQ(traverseRange<FlatTreeTraversal>(getBodyRange()), | 74 EXPECT_EQ(traverseRange<FlatTreeTraversal>(getBodyRange()), |
70 traverseRange(EphemeralRangeInFlatTree(getBodyRange()))); | 75 traverseRange(EphemeralRangeInFlatTree(getBodyRange()))); |
71 } | 76 } |
72 | 77 |
73 // Tests that |inRange| helper will traverse the whole range with shadow DOM. | 78 // Tests that |inRange| helper will traverse the whole range with shadow DOM. |
74 TEST_F(EphemeralRangeTest, rangeShadowTraversal) { | 79 TEST_F(EphemeralRangeTest, rangeShadowTraversal) { |
75 const char* bodyContent = | 80 const char* bodyContent = |
76 "<p id='host'><b id='one'></b><input type='text' value='some'></p>"; | 81 "<b id='zero'>0</b>" |
82 "<p id='host'>" | |
83 "<b id='one'>1</b>" | |
84 "<b id='two'>22</b>" | |
Xiaocheng
2017/03/10 01:27:15
Let's add more node under #host and verify that it
tanvir
2017/03/10 10:14:29
Done.
| |
85 "</p>" | |
86 "<b id='three'>333</b>"; | |
87 const char* shadowContent = | |
88 "<p id='four'>4444</p>" | |
89 "<content select=#two></content>" | |
90 "<content select=#one></content>" | |
91 "<span id='five'>" | |
Xiaocheng
2017/03/10 01:27:15
ditto.
| |
92 "<p id='six'>666666</p>"; | |
77 setBodyContent(bodyContent); | 93 setBodyContent(bodyContent); |
94 setShadowContent(shadowContent, "host"); | |
78 | 95 |
79 EXPECT_EQ("[BODY][P id=\"host\"][B id=\"one\"][INPUT]", | 96 const std::string expectedNodes( |
80 traverseRange<>(getBodyRange())); | 97 "[BODY][B id=\"zero\"][#text \"0\"][P id=\"host\"][P id=\"four\"][#text " |
81 EXPECT_EQ(traverseRange<>(getBodyRange()), | 98 "\"4444\"][B id=\"two\"][#text \"22\"][B id=\"one\"][#text \"1\"][SPAN " |
82 traverseRange(EphemeralRange(getBodyRange()))); | 99 "id=\"five\"][P id=\"six\"][#text \"666666\"][B id=\"three\"][#text " |
83 | 100 "\"333\"]"); |
84 // In this case FlatTree traverse should differs from DOM tree traverse. | 101 EXPECT_EQ(expectedNodes, traverseRange<FlatTreeTraversal>(getBodyRange())); |
85 EXPECT_EQ( | |
86 "[BODY][P id=\"host\"][B id=\"one\"][INPUT][DIV id=\"inner-editor\" " | |
87 "(editable)][#text \"some\"]", | |
88 traverseRange<FlatTreeTraversal>(getBodyRange())); | |
89 EXPECT_EQ(traverseRange<FlatTreeTraversal>(getBodyRange()), | 102 EXPECT_EQ(traverseRange<FlatTreeTraversal>(getBodyRange()), |
90 traverseRange(EphemeralRangeInFlatTree(getBodyRange()))); | 103 traverseRange(EphemeralRangeInFlatTree(getBodyRange()))); |
91 } | 104 } |
92 | 105 |
93 // Limit a range and check that it will be traversed correctly. | 106 // Limit a range and check that it will be traversed correctly. |
94 TEST_F(EphemeralRangeTest, rangeTraversalLimited) { | 107 TEST_F(EphemeralRangeTest, rangeTraversalLimitedDOM) { |
95 const char* bodyContent = | 108 const char* bodyContent = |
96 "<p id='host'><b id='one'></b><input type='text' value='some'><span " | 109 "<p id='host'>" |
97 "id='two'></p>"; | 110 "<b id='zero'>0</b>" |
111 "<b id='one'>1</b>" | |
112 "<b id='two'>22</b>" | |
113 "<span id='three'>" | |
Xiaocheng
2017/03/10 01:27:16
ditto.
tanvir
2017/03/10 10:14:29
Done.
| |
114 "</p>"; | |
98 setBodyContent(bodyContent); | 115 setBodyContent(bodyContent); |
99 | 116 |
100 // Get a limited range from <body> to <b> nodes. | |
101 Range* untilB = getBodyRange(); | 117 Range* untilB = getBodyRange(); |
102 untilB->setEnd(document().getElementById("one"), 0, | 118 untilB->setEnd(document().getElementById("one"), 0, |
103 IGNORE_EXCEPTION_FOR_TESTING); | 119 IGNORE_EXCEPTION_FOR_TESTING); |
104 EXPECT_EQ("[BODY][P id=\"host\"][B id=\"one\"]", traverseRange<>(untilB)); | 120 EXPECT_EQ("[BODY][P id=\"host\"][B id=\"zero\"][#text \"0\"][B id=\"one\"]", |
105 | 121 traverseRange<>(untilB)); |
106 EXPECT_EQ(traverseRange<>(untilB), traverseRange(EphemeralRange(untilB))); | 122 EXPECT_EQ(traverseRange<>(untilB), traverseRange(EphemeralRange(untilB))); |
107 | 123 |
108 EXPECT_EQ("[BODY][P id=\"host\"][B id=\"one\"]", | |
109 traverseRange<FlatTreeTraversal>(untilB)); | |
110 EXPECT_EQ(traverseRange<FlatTreeTraversal>(untilB), | |
111 traverseRange(EphemeralRangeInFlatTree(untilB))); | |
112 | |
113 // Get a limited range from <b> to <span> nodes. | |
114 Range* fromBToSpan = getBodyRange(); | 124 Range* fromBToSpan = getBodyRange(); |
115 fromBToSpan->setStart(document().getElementById("one"), 0, | 125 fromBToSpan->setStart(document().getElementById("one"), 0, |
116 IGNORE_EXCEPTION_FOR_TESTING); | 126 IGNORE_EXCEPTION_FOR_TESTING); |
117 fromBToSpan->setEnd(document().getElementById("two"), 0, | 127 fromBToSpan->setEnd(document().getElementById("three"), 0, |
118 IGNORE_EXCEPTION_FOR_TESTING); | 128 IGNORE_EXCEPTION_FOR_TESTING); |
119 | 129 EXPECT_EQ("[#text \"1\"][B id=\"two\"][#text \"22\"][SPAN id=\"three\"]", |
120 EXPECT_EQ("[B id=\"one\"][INPUT][SPAN id=\"two\"]", | |
121 traverseRange<>(fromBToSpan)); | 130 traverseRange<>(fromBToSpan)); |
122 EXPECT_EQ(traverseRange<>(fromBToSpan), | 131 EXPECT_EQ(traverseRange<>(fromBToSpan), |
123 traverseRange(EphemeralRange(fromBToSpan))); | 132 traverseRange(EphemeralRange(fromBToSpan))); |
133 } | |
124 | 134 |
125 EXPECT_EQ( | 135 TEST_F(EphemeralRangeTest, rangeTraversalLimitedFlatTree) { |
126 "[B id=\"one\"][INPUT][DIV id=\"inner-editor\" (editable)][#text " | 136 const char* bodyContent = |
127 "\"some\"][SPAN id=\"two\"]", | 137 "<b id='zero'>0</b>" |
128 traverseRange<FlatTreeTraversal>(fromBToSpan)); | 138 "<p id='host'>" |
129 EXPECT_EQ(traverseRange<FlatTreeTraversal>(fromBToSpan), | 139 "<b id='one'>1</b>" |
130 traverseRange(EphemeralRangeInFlatTree(fromBToSpan))); | 140 "<b id='two'>22</b>" |
141 "</p>" | |
142 "<b id='three'>333</b>"; | |
143 const char* shadowContent = | |
144 "<p id='four'>4444</p>" | |
145 "<content select=#two></content>" | |
146 "<content select=#one></content>" | |
147 "<span id='five'>" | |
Xiaocheng
2017/03/10 01:27:15
ditto.
tanvir
2017/03/10 10:14:29
Done.
| |
148 "<p id='six'>666666</p>"; | |
149 setBodyContent(bodyContent); | |
150 ShadowRoot* shadowRoot = setShadowContent(shadowContent, "host"); | |
151 | |
152 const PositionInFlatTree startPosition(document().getElementById("one"), 0); | |
153 const PositionInFlatTree limitPosition(shadowRoot->getElementById("five"), 0); | |
154 const PositionInFlatTree endPosition(shadowRoot->getElementById("six"), 0); | |
155 const EphemeralRangeInFlatTree fromBToSpan(startPosition, limitPosition); | |
156 EXPECT_EQ("[#text \"1\"][SPAN id=\"five\"]", traverseRange(fromBToSpan)); | |
157 | |
158 const EphemeralRangeInFlatTree fromSpanToEnd(limitPosition, endPosition); | |
159 EXPECT_EQ("[P id=\"six\"]", traverseRange(fromSpanToEnd)); | |
131 } | 160 } |
132 | 161 |
133 TEST_F(EphemeralRangeTest, traversalEmptyRanges) { | 162 TEST_F(EphemeralRangeTest, traversalEmptyRanges) { |
134 const char* bodyContent = "<p id='host'><b id='one'></b></p>"; | 163 const char* bodyContent = |
164 "<p id='host'>" | |
165 "<b id='one'></b>" | |
166 "</p>"; | |
135 setBodyContent(bodyContent); | 167 setBodyContent(bodyContent); |
136 | 168 |
137 // Expect no iterations in loop for an empty EphemeralRange. | 169 // Expect no iterations in loop for an empty EphemeralRange. |
138 EXPECT_EQ(std::string(), traverseRange(EphemeralRange())); | 170 EXPECT_EQ(std::string(), traverseRange(EphemeralRange())); |
139 | 171 |
140 auto iterable = EphemeralRange().nodes(); | 172 auto iterable = EphemeralRange().nodes(); |
141 // Tree iterators have only |operator !=| ATM. | 173 // Tree iterators have only |operator !=| ATM. |
142 EXPECT_FALSE(iterable.begin() != iterable.end()); | 174 EXPECT_FALSE(iterable.begin() != iterable.end()); |
143 | 175 |
144 const EphemeralRange singlePositionRange(getBodyRange()->startPosition()); | 176 const EphemeralRange singlePositionRange(getBodyRange()->startPosition()); |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
179 setBodyContent(bodyContent); | 211 setBodyContent(bodyContent); |
180 ShadowRoot* shadowRoot = setShadowContent(shadowContent, "host"); | 212 ShadowRoot* shadowRoot = setShadowContent(shadowContent, "host"); |
181 | 213 |
182 const PositionInFlatTree startPosition(document().getElementById("one"), 0); | 214 const PositionInFlatTree startPosition(document().getElementById("one"), 0); |
183 const PositionInFlatTree endPosition(shadowRoot->getElementById("five"), 0); | 215 const PositionInFlatTree endPosition(shadowRoot->getElementById("five"), 0); |
184 const EphemeralRangeInFlatTree range(startPosition, endPosition); | 216 const EphemeralRangeInFlatTree range(startPosition, endPosition); |
185 EXPECT_EQ(document().getElementById("host"), range.commonAncestorContainer()); | 217 EXPECT_EQ(document().getElementById("host"), range.commonAncestorContainer()); |
186 } | 218 } |
187 | 219 |
188 } // namespace blink | 220 } // namespace blink |
OLD | NEW |