| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "config.h" | 5 #include "config.h" |
| 6 #include "core/editing/SurroundingText.h" | 6 #include "core/editing/SurroundingText.h" |
| 7 | 7 |
| 8 #include "core/dom/Position.h" | 8 #include "core/dom/Position.h" |
| 9 #include "core/dom/Range.h" | 9 #include "core/dom/Range.h" |
| 10 #include "core/dom/Text.h" | 10 #include "core/dom/Text.h" |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 51 | 51 |
| 52 TEST_F(SurroundingTextTest, BasicCaretSelection) | 52 TEST_F(SurroundingTextTest, BasicCaretSelection) |
| 53 { | 53 { |
| 54 setHTML(String("<p id='selection'>foo bar</p>")); | 54 setHTML(String("<p id='selection'>foo bar</p>")); |
| 55 | 55 |
| 56 { | 56 { |
| 57 VisibleSelection selection = select(0); | 57 VisibleSelection selection = select(0); |
| 58 SurroundingText surroundingText(selection.start(), 1); | 58 SurroundingText surroundingText(selection.start(), 1); |
| 59 | 59 |
| 60 EXPECT_EQ("f", surroundingText.content()); | 60 EXPECT_EQ("f", surroundingText.content()); |
| 61 EXPECT_EQ(0u, surroundingText.positionOffsetInContent()); | 61 EXPECT_EQ(0u, surroundingText.startOffsetInContent()); |
| 62 EXPECT_EQ(0u, surroundingText.endOffsetInContent()); |
| 62 } | 63 } |
| 63 | 64 |
| 64 { | 65 { |
| 65 VisibleSelection selection = select(0); | 66 VisibleSelection selection = select(0); |
| 66 SurroundingText surroundingText(selection.start(), 5); | 67 SurroundingText surroundingText(selection.start(), 5); |
| 67 | 68 |
| 68 // maxlength/2 is used on the left and right. | 69 // maxlength/2 is used on the left and right. |
| 69 EXPECT_EQ("foo", surroundingText.content().simplifyWhiteSpace()); | 70 EXPECT_EQ("foo", surroundingText.content().simplifyWhiteSpace()); |
| 70 EXPECT_EQ(1u, surroundingText.positionOffsetInContent()); | 71 EXPECT_EQ(1u, surroundingText.startOffsetInContent()); |
| 72 EXPECT_EQ(1u, surroundingText.endOffsetInContent()); |
| 71 } | 73 } |
| 72 | 74 |
| 73 { | 75 { |
| 74 VisibleSelection selection = select(0); | 76 VisibleSelection selection = select(0); |
| 75 SurroundingText surroundingText(selection.start(), 42); | 77 SurroundingText surroundingText(selection.start(), 42); |
| 76 | 78 |
| 77 EXPECT_EQ("foo bar", surroundingText.content().simplifyWhiteSpace()); | 79 EXPECT_EQ("foo bar", surroundingText.content().simplifyWhiteSpace()); |
| 78 EXPECT_EQ(1u, surroundingText.positionOffsetInContent()); | 80 EXPECT_EQ(1u, surroundingText.startOffsetInContent()); |
| 79 } | 81 EXPECT_EQ(1u, surroundingText.endOffsetInContent()); } |
| 80 | 82 |
| 81 { | 83 { |
| 82 // FIXME: if the selection is at the end of the text, SurroundingText | 84 // FIXME: if the selection is at the end of the text, SurroundingText |
| 83 // will return nothing. | 85 // will return nothing. |
| 84 VisibleSelection selection = select(7); | 86 VisibleSelection selection = select(7); |
| 85 SurroundingText surroundingText(selection.start(), 42); | 87 SurroundingText surroundingText(selection.start(), 42); |
| 86 | 88 |
| 87 EXPECT_EQ(0u, surroundingText.content().length()); | 89 EXPECT_TRUE(surroundingText.content().isEmpty()); |
| 88 EXPECT_EQ(0u, surroundingText.positionOffsetInContent()); | 90 EXPECT_EQ(0u, surroundingText.startOffsetInContent()); |
| 89 } | 91 EXPECT_EQ(0u, surroundingText.endOffsetInContent()); } |
| 90 | 92 |
| 91 { | 93 { |
| 92 VisibleSelection selection = select(6); | 94 VisibleSelection selection = select(6); |
| 93 SurroundingText surroundingText(selection.start(), 2); | 95 SurroundingText surroundingText(selection.start(), 2); |
| 94 | 96 |
| 95 EXPECT_EQ("ar", surroundingText.content()); | 97 EXPECT_EQ("ar", surroundingText.content()); |
| 96 EXPECT_EQ(1u, surroundingText.positionOffsetInContent()); | 98 EXPECT_EQ(1u, surroundingText.startOffsetInContent()); |
| 97 } | 99 EXPECT_EQ(1u, surroundingText.endOffsetInContent()); } |
| 98 | 100 |
| 99 { | 101 { |
| 100 VisibleSelection selection = select(6); | 102 VisibleSelection selection = select(6); |
| 101 SurroundingText surroundingText(selection.start(), 42); | 103 SurroundingText surroundingText(selection.start(), 42); |
| 102 | 104 |
| 103 EXPECT_EQ("foo bar", surroundingText.content().simplifyWhiteSpace()); | 105 EXPECT_EQ("foo bar", surroundingText.content().simplifyWhiteSpace()); |
| 104 EXPECT_EQ(7u, surroundingText.positionOffsetInContent()); | 106 EXPECT_EQ(7u, surroundingText.startOffsetInContent()); |
| 107 EXPECT_EQ(7u, surroundingText.endOffsetInContent()); |
| 105 } | 108 } |
| 106 } | 109 } |
| 107 | 110 |
| 111 TEST_F(SurroundingTextTest, BasicRangeSelection) |
| 112 { |
| 113 setHTML(String("<p id='selection'>Lorem ipsum dolor sit amet</p>")); |
| 114 |
| 115 { |
| 116 VisibleSelection selection = select(0, 5); |
| 117 SurroundingText surroundingText(selection.start(), selection.end(), 1); |
| 118 |
| 119 EXPECT_EQ("Lorem ", surroundingText.content()); |
| 120 EXPECT_EQ(0u, surroundingText.startOffsetInContent()); |
| 121 EXPECT_EQ(5u, surroundingText.endOffsetInContent()); |
| 122 } |
| 123 |
| 124 { |
| 125 VisibleSelection selection = select(0, 5); |
| 126 SurroundingText surroundingText(selection.start(), selection.end(), 5); |
| 127 |
| 128 EXPECT_EQ("Lorem ip", surroundingText.content().simplifyWhiteSpace()); |
| 129 EXPECT_EQ(1u, surroundingText.startOffsetInContent()); |
| 130 EXPECT_EQ(6u, surroundingText.endOffsetInContent()); |
| 131 } |
| 132 |
| 133 { |
| 134 VisibleSelection selection = select(0, 5); |
| 135 SurroundingText surroundingText(selection.start(), selection.end(), 42); |
| 136 |
| 137 EXPECT_EQ("Lorem ipsum dolor sit amet", surroundingText.content().simpli
fyWhiteSpace()); |
| 138 EXPECT_EQ(1u, surroundingText.startOffsetInContent()); |
| 139 EXPECT_EQ(6u, surroundingText.endOffsetInContent()); |
| 140 } |
| 141 |
| 142 { |
| 143 VisibleSelection selection = select(6, 11); |
| 144 SurroundingText surroundingText(selection.start(), selection.end(), 2); |
| 145 |
| 146 EXPECT_EQ(" ipsum ", surroundingText.content()); |
| 147 EXPECT_EQ(1u, surroundingText.startOffsetInContent()); |
| 148 EXPECT_EQ(6u, surroundingText.endOffsetInContent()); |
| 149 } |
| 150 |
| 151 { |
| 152 VisibleSelection selection = select(6, 11); |
| 153 SurroundingText surroundingText(selection.start(), selection.end(), 42); |
| 154 |
| 155 EXPECT_EQ("Lorem ipsum dolor sit amet", surroundingText.content().simpli
fyWhiteSpace()); |
| 156 EXPECT_EQ(7u, surroundingText.startOffsetInContent()); |
| 157 EXPECT_EQ(12u, surroundingText.endOffsetInContent()); |
| 158 } |
| 159 } |
| 160 |
| 108 TEST_F(SurroundingTextTest, TreeCaretSelection) | 161 TEST_F(SurroundingTextTest, TreeCaretSelection) |
| 109 { | 162 { |
| 110 setHTML(String("<div>This is outside of <p id='selection'>foo bar</p> the se
lected node</div>")); | 163 setHTML(String("<div>This is outside of <p id='selection'>foo bar</p> the se
lected node</div>")); |
| 111 | 164 |
| 112 { | 165 { |
| 113 VisibleSelection selection = select(0); | 166 VisibleSelection selection = select(0); |
| 114 SurroundingText surroundingText(selection.start(), 1); | 167 SurroundingText surroundingText(selection.start(), 1); |
| 115 | 168 |
| 116 EXPECT_EQ("f", surroundingText.content()); | 169 EXPECT_EQ("f", surroundingText.content()); |
| 117 EXPECT_EQ(0u, surroundingText.positionOffsetInContent()); | 170 EXPECT_EQ(0u, surroundingText.startOffsetInContent()); |
| 171 EXPECT_EQ(0u, surroundingText.endOffsetInContent()); |
| 118 } | 172 } |
| 119 | 173 |
| 120 { | 174 { |
| 121 VisibleSelection selection = select(0); | 175 VisibleSelection selection = select(0); |
| 122 SurroundingText surroundingText(selection.start(), 5); | 176 SurroundingText surroundingText(selection.start(), 5); |
| 123 | 177 |
| 124 EXPECT_EQ("foo", surroundingText.content().simplifyWhiteSpace()); | 178 EXPECT_EQ("foo", surroundingText.content().simplifyWhiteSpace()); |
| 125 EXPECT_EQ(1u, surroundingText.positionOffsetInContent()); | 179 EXPECT_EQ(1u, surroundingText.startOffsetInContent()); |
| 180 EXPECT_EQ(1u, surroundingText.endOffsetInContent()); |
| 126 } | 181 } |
| 127 | 182 |
| 128 { | 183 { |
| 129 VisibleSelection selection = select(0); | 184 VisibleSelection selection = select(0); |
| 130 SurroundingText surroundingText(selection.start(), 1337); | 185 SurroundingText surroundingText(selection.start(), 1337); |
| 131 | 186 |
| 132 EXPECT_EQ("This is outside of foo bar the selected node", surroundingTex
t.content().simplifyWhiteSpace()); | 187 EXPECT_EQ("This is outside of foo bar the selected node", surroundingTex
t.content().simplifyWhiteSpace()); |
| 133 EXPECT_EQ(20u, surroundingText.positionOffsetInContent()); | 188 EXPECT_EQ(20u, surroundingText.startOffsetInContent()); |
| 189 EXPECT_EQ(20u, surroundingText.endOffsetInContent()); |
| 134 } | 190 } |
| 135 | 191 |
| 136 { | 192 { |
| 137 VisibleSelection selection = select(6); | 193 VisibleSelection selection = select(6); |
| 138 SurroundingText surroundingText(selection.start(), 2); | 194 SurroundingText surroundingText(selection.start(), 2); |
| 139 | 195 |
| 140 EXPECT_EQ("ar", surroundingText.content()); | 196 EXPECT_EQ("ar", surroundingText.content()); |
| 141 EXPECT_EQ(1u, surroundingText.positionOffsetInContent()); | 197 EXPECT_EQ(1u, surroundingText.startOffsetInContent()); |
| 198 EXPECT_EQ(1u, surroundingText.endOffsetInContent()); |
| 142 } | 199 } |
| 143 | 200 |
| 144 { | 201 { |
| 145 VisibleSelection selection = select(6); | 202 VisibleSelection selection = select(6); |
| 146 SurroundingText surroundingText(selection.start(), 1337); | 203 SurroundingText surroundingText(selection.start(), 1337); |
| 147 | 204 |
| 148 EXPECT_EQ("This is outside of foo bar the selected node", surroundingTex
t.content().simplifyWhiteSpace()); | 205 EXPECT_EQ("This is outside of foo bar the selected node", surroundingTex
t.content().simplifyWhiteSpace()); |
| 149 EXPECT_EQ(26u, surroundingText.positionOffsetInContent()); | 206 EXPECT_EQ(26u, surroundingText.startOffsetInContent()); |
| 207 EXPECT_EQ(26u, surroundingText.endOffsetInContent()); |
| 150 } | 208 } |
| 151 } | 209 } |
| 152 | 210 |
| 211 TEST_F(SurroundingTextTest, TreeRangeSelection) |
| 212 { |
| 213 setHTML(String("<div>This is outside of <p id='selection'>foo bar</p> the se
lected node</div>")); |
| 214 |
| 215 { |
| 216 VisibleSelection selection = select(0, 1); |
| 217 SurroundingText surroundingText(selection.start(), selection.end(), 1); |
| 218 |
| 219 EXPECT_EQ("fo", surroundingText.content().simplifyWhiteSpace()); |
| 220 EXPECT_EQ(0u, surroundingText.startOffsetInContent()); |
| 221 EXPECT_EQ(1u, surroundingText.endOffsetInContent()); |
| 222 } |
| 223 |
| 224 { |
| 225 VisibleSelection selection = select(0, 3); |
| 226 SurroundingText surroundingText(selection.start(), selection.end(), 12); |
| 227 |
| 228 EXPECT_EQ("e of foo bar", surroundingText.content().simplifyWhiteSpace()
); |
| 229 EXPECT_EQ(5u, surroundingText.startOffsetInContent()); |
| 230 EXPECT_EQ(8u, surroundingText.endOffsetInContent()); |
| 231 } |
| 232 |
| 233 { |
| 234 VisibleSelection selection = select(0, 3); |
| 235 SurroundingText surroundingText(selection.start(), selection.end(), 1337
); |
| 236 |
| 237 EXPECT_EQ("This is outside of foo bar the selected node", surroundingTex
t.content().simplifyWhiteSpace()); |
| 238 EXPECT_EQ(20u, surroundingText.startOffsetInContent()); |
| 239 EXPECT_EQ(23u, surroundingText.endOffsetInContent()); |
| 240 } |
| 241 |
| 242 { |
| 243 VisibleSelection selection = select(4, 7); |
| 244 SurroundingText surroundingText(selection.start(), selection.end(), 12); |
| 245 |
| 246 EXPECT_EQ("foo bar the se", surroundingText.content().simplifyWhiteSpace
()); |
| 247 EXPECT_EQ(5u, surroundingText.startOffsetInContent()); |
| 248 EXPECT_EQ(8u, surroundingText.endOffsetInContent()); |
| 249 } |
| 250 |
| 251 { |
| 252 VisibleSelection selection = select(0, 7); |
| 253 SurroundingText surroundingText(selection.start(), selection.end(), 1337
); |
| 254 |
| 255 EXPECT_EQ("This is outside of foo bar the selected node", surroundingTex
t.content().simplifyWhiteSpace()); |
| 256 EXPECT_EQ(20u, surroundingText.startOffsetInContent()); |
| 257 EXPECT_EQ(27u, surroundingText.endOffsetInContent()); |
| 258 } |
| 259 } |
| 260 |
| 153 } // anonymous namespace | 261 } // anonymous namespace |
| OLD | NEW |