| 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()); |
| 81 EXPECT_EQ(1u, surroundingText.endOffsetInContent()); |
| 79 } | 82 } |
| 80 | 83 |
| 81 { | 84 { |
| 82 // FIXME: if the selection is at the end of the text, SurroundingText | 85 // FIXME: if the selection is at the end of the text, SurroundingText |
| 83 // will return nothing. | 86 // will return nothing. |
| 84 VisibleSelection selection = select(7); | 87 VisibleSelection selection = select(7); |
| 85 SurroundingText surroundingText(selection.start(), 42); | 88 SurroundingText surroundingText(selection.start(), 42); |
| 86 | 89 |
| 87 EXPECT_EQ(0u, surroundingText.content().length()); | 90 EXPECT_EQ(0u, surroundingText.content().length()); |
| 88 EXPECT_EQ(0u, surroundingText.positionOffsetInContent()); | 91 EXPECT_EQ(0u, surroundingText.startOffsetInContent()); |
| 92 EXPECT_EQ(0u, surroundingText.endOffsetInContent()); |
| 89 } | 93 } |
| 90 | 94 |
| 91 { | 95 { |
| 92 VisibleSelection selection = select(6); | 96 VisibleSelection selection = select(6); |
| 93 SurroundingText surroundingText(selection.start(), 2); | 97 SurroundingText surroundingText(selection.start(), 2); |
| 94 | 98 |
| 95 EXPECT_EQ("ar", surroundingText.content()); | 99 EXPECT_EQ("ar", surroundingText.content()); |
| 96 EXPECT_EQ(1u, surroundingText.positionOffsetInContent()); | 100 EXPECT_EQ(1u, surroundingText.startOffsetInContent()); |
| 101 EXPECT_EQ(1u, surroundingText.endOffsetInContent()); |
| 97 } | 102 } |
| 98 | 103 |
| 99 { | 104 { |
| 100 VisibleSelection selection = select(6); | 105 VisibleSelection selection = select(6); |
| 101 SurroundingText surroundingText(selection.start(), 42); | 106 SurroundingText surroundingText(selection.start(), 42); |
| 102 | 107 |
| 103 EXPECT_EQ("foo bar", surroundingText.content().simplifyWhiteSpace()); | 108 EXPECT_EQ("foo bar", surroundingText.content().simplifyWhiteSpace()); |
| 104 EXPECT_EQ(7u, surroundingText.positionOffsetInContent()); | 109 EXPECT_EQ(7u, surroundingText.startOffsetInContent()); |
| 110 EXPECT_EQ(7u, surroundingText.endOffsetInContent()); |
| 105 } | 111 } |
| 106 } | 112 } |
| 107 | 113 |
| 114 TEST_F(SurroundingTextTest, BasicRangeSelection) |
| 115 { |
| 116 setHTML(String("<p id='selection'>Lorem ipsum dolor sit amet</p>")); |
| 117 |
| 118 { |
| 119 VisibleSelection selection = select(0, 5); |
| 120 SurroundingText surroundingText(*selection.firstRange(), 1); |
| 121 |
| 122 EXPECT_EQ("Lorem ", surroundingText.content()); |
| 123 EXPECT_EQ(0u, surroundingText.startOffsetInContent()); |
| 124 EXPECT_EQ(5u, surroundingText.endOffsetInContent()); |
| 125 } |
| 126 |
| 127 { |
| 128 VisibleSelection selection = select(0, 5); |
| 129 SurroundingText surroundingText(*selection.firstRange(), 5); |
| 130 |
| 131 EXPECT_EQ("Lorem ip", surroundingText.content().simplifyWhiteSpace()); |
| 132 EXPECT_EQ(1u, surroundingText.startOffsetInContent()); |
| 133 EXPECT_EQ(6u, surroundingText.endOffsetInContent()); |
| 134 } |
| 135 |
| 136 { |
| 137 VisibleSelection selection = select(0, 5); |
| 138 SurroundingText surroundingText(*selection.firstRange(), 42); |
| 139 |
| 140 EXPECT_EQ("Lorem ipsum dolor sit amet", surroundingText.content().simpli
fyWhiteSpace()); |
| 141 EXPECT_EQ(1u, surroundingText.startOffsetInContent()); |
| 142 EXPECT_EQ(6u, surroundingText.endOffsetInContent()); |
| 143 } |
| 144 |
| 145 { |
| 146 VisibleSelection selection = select(6, 11); |
| 147 SurroundingText surroundingText(*selection.firstRange(), 2); |
| 148 |
| 149 EXPECT_EQ(" ipsum ", surroundingText.content()); |
| 150 EXPECT_EQ(1u, surroundingText.startOffsetInContent()); |
| 151 EXPECT_EQ(6u, surroundingText.endOffsetInContent()); |
| 152 } |
| 153 |
| 154 { |
| 155 VisibleSelection selection = select(6, 11); |
| 156 SurroundingText surroundingText(*selection.firstRange(), 42); |
| 157 |
| 158 EXPECT_EQ("Lorem ipsum dolor sit amet", surroundingText.content().simpli
fyWhiteSpace()); |
| 159 EXPECT_EQ(7u, surroundingText.startOffsetInContent()); |
| 160 EXPECT_EQ(12u, surroundingText.endOffsetInContent()); |
| 161 } |
| 162 } |
| 163 |
| 108 TEST_F(SurroundingTextTest, TreeCaretSelection) | 164 TEST_F(SurroundingTextTest, TreeCaretSelection) |
| 109 { | 165 { |
| 110 setHTML(String("<div>This is outside of <p id='selection'>foo bar</p> the se
lected node</div>")); | 166 setHTML(String("<div>This is outside of <p id='selection'>foo bar</p> the se
lected node</div>")); |
| 111 | 167 |
| 112 { | 168 { |
| 113 VisibleSelection selection = select(0); | 169 VisibleSelection selection = select(0); |
| 114 SurroundingText surroundingText(selection.start(), 1); | 170 SurroundingText surroundingText(selection.start(), 1); |
| 115 | 171 |
| 116 EXPECT_EQ("f", surroundingText.content()); | 172 EXPECT_EQ("f", surroundingText.content()); |
| 117 EXPECT_EQ(0u, surroundingText.positionOffsetInContent()); | 173 EXPECT_EQ(0u, surroundingText.startOffsetInContent()); |
| 174 EXPECT_EQ(0u, surroundingText.endOffsetInContent()); |
| 118 } | 175 } |
| 119 | 176 |
| 120 { | 177 { |
| 121 VisibleSelection selection = select(0); | 178 VisibleSelection selection = select(0); |
| 122 SurroundingText surroundingText(selection.start(), 5); | 179 SurroundingText surroundingText(selection.start(), 5); |
| 123 | 180 |
| 124 EXPECT_EQ("foo", surroundingText.content().simplifyWhiteSpace()); | 181 EXPECT_EQ("foo", surroundingText.content().simplifyWhiteSpace()); |
| 125 EXPECT_EQ(1u, surroundingText.positionOffsetInContent()); | 182 EXPECT_EQ(1u, surroundingText.startOffsetInContent()); |
| 183 EXPECT_EQ(1u, surroundingText.endOffsetInContent()); |
| 126 } | 184 } |
| 127 | 185 |
| 128 { | 186 { |
| 129 VisibleSelection selection = select(0); | 187 VisibleSelection selection = select(0); |
| 130 SurroundingText surroundingText(selection.start(), 1337); | 188 SurroundingText surroundingText(selection.start(), 1337); |
| 131 | 189 |
| 132 EXPECT_EQ("This is outside of foo bar the selected node", surroundingTex
t.content().simplifyWhiteSpace()); | 190 EXPECT_EQ("This is outside of foo bar the selected node", surroundingTex
t.content().simplifyWhiteSpace()); |
| 133 EXPECT_EQ(20u, surroundingText.positionOffsetInContent()); | 191 EXPECT_EQ(20u, surroundingText.startOffsetInContent()); |
| 192 EXPECT_EQ(20u, surroundingText.endOffsetInContent()); |
| 134 } | 193 } |
| 135 | 194 |
| 136 { | 195 { |
| 137 VisibleSelection selection = select(6); | 196 VisibleSelection selection = select(6); |
| 138 SurroundingText surroundingText(selection.start(), 2); | 197 SurroundingText surroundingText(selection.start(), 2); |
| 139 | 198 |
| 140 EXPECT_EQ("ar", surroundingText.content()); | 199 EXPECT_EQ("ar", surroundingText.content()); |
| 141 EXPECT_EQ(1u, surroundingText.positionOffsetInContent()); | 200 EXPECT_EQ(1u, surroundingText.startOffsetInContent()); |
| 201 EXPECT_EQ(1u, surroundingText.endOffsetInContent()); |
| 142 } | 202 } |
| 143 | 203 |
| 144 { | 204 { |
| 145 VisibleSelection selection = select(6); | 205 VisibleSelection selection = select(6); |
| 146 SurroundingText surroundingText(selection.start(), 1337); | 206 SurroundingText surroundingText(selection.start(), 1337); |
| 147 | 207 |
| 148 EXPECT_EQ("This is outside of foo bar the selected node", surroundingTex
t.content().simplifyWhiteSpace()); | 208 EXPECT_EQ("This is outside of foo bar the selected node", surroundingTex
t.content().simplifyWhiteSpace()); |
| 149 EXPECT_EQ(26u, surroundingText.positionOffsetInContent()); | 209 EXPECT_EQ(26u, surroundingText.startOffsetInContent()); |
| 210 EXPECT_EQ(26u, surroundingText.endOffsetInContent()); |
| 150 } | 211 } |
| 151 } | 212 } |
| 152 | 213 |
| 214 TEST_F(SurroundingTextTest, TreeRangeSelection) |
| 215 { |
| 216 setHTML(String("<div>This is outside of <p id='selection'>foo bar</p> the se
lected node</div>")); |
| 217 |
| 218 { |
| 219 VisibleSelection selection = select(0, 1); |
| 220 SurroundingText surroundingText(*selection.firstRange(), 1); |
| 221 |
| 222 EXPECT_EQ("fo", surroundingText.content().simplifyWhiteSpace()); |
| 223 EXPECT_EQ(0u, surroundingText.startOffsetInContent()); |
| 224 EXPECT_EQ(1u, surroundingText.endOffsetInContent()); |
| 225 } |
| 226 |
| 227 { |
| 228 VisibleSelection selection = select(0, 3); |
| 229 SurroundingText surroundingText(*selection.firstRange(), 12); |
| 230 |
| 231 EXPECT_EQ("e of foo bar", surroundingText.content().simplifyWhiteSpace()
); |
| 232 EXPECT_EQ(5u, surroundingText.startOffsetInContent()); |
| 233 EXPECT_EQ(8u, surroundingText.endOffsetInContent()); |
| 234 } |
| 235 |
| 236 { |
| 237 VisibleSelection selection = select(0, 3); |
| 238 SurroundingText surroundingText(*selection.firstRange(), 1337); |
| 239 |
| 240 EXPECT_EQ("This is outside of foo bar the selected node", surroundingTex
t.content().simplifyWhiteSpace()); |
| 241 EXPECT_EQ(20u, surroundingText.startOffsetInContent()); |
| 242 EXPECT_EQ(23u, surroundingText.endOffsetInContent()); |
| 243 } |
| 244 |
| 245 { |
| 246 VisibleSelection selection = select(4, 7); |
| 247 SurroundingText surroundingText(*selection.firstRange(), 12); |
| 248 |
| 249 EXPECT_EQ("foo bar the se", surroundingText.content().simplifyWhiteSpace
()); |
| 250 EXPECT_EQ(5u, surroundingText.startOffsetInContent()); |
| 251 EXPECT_EQ(8u, surroundingText.endOffsetInContent()); |
| 252 } |
| 253 |
| 254 { |
| 255 VisibleSelection selection = select(0, 7); |
| 256 SurroundingText surroundingText(*selection.firstRange(), 1337); |
| 257 |
| 258 EXPECT_EQ("This is outside of foo bar the selected node", surroundingTex
t.content().simplifyWhiteSpace()); |
| 259 EXPECT_EQ(20u, surroundingText.startOffsetInContent()); |
| 260 EXPECT_EQ(27u, surroundingText.endOffsetInContent()); |
| 261 } |
| 262 } |
| 263 |
| 153 } // anonymous namespace | 264 } // anonymous namespace |
| OLD | NEW |