| 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 "core/editing/VisibleSelection.h" | 5 #include "core/editing/VisibleSelection.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 "core/editing/SelectionAdjuster.h" | 9 #include "core/editing/SelectionAdjuster.h" |
| 10 | 10 |
| (...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 201 VisibleSelection selection; | 201 VisibleSelection selection; |
| 202 VisibleSelectionInFlatTree selectionInFlatTree; | 202 VisibleSelectionInFlatTree selectionInFlatTree; |
| 203 setSelection(selection, 0); | 203 setSelection(selection, 0); |
| 204 setSelection(selectionInFlatTree, 0); | 204 setSelection(selectionInFlatTree, 0); |
| 205 | 205 |
| 206 EXPECT_FALSE(selection.isNone()); | 206 EXPECT_FALSE(selection.isNone()); |
| 207 EXPECT_FALSE(selectionInFlatTree.isNone()); | 207 EXPECT_FALSE(selectionInFlatTree.isNone()); |
| 208 EXPECT_TRUE(selection.isCaret()); | 208 EXPECT_TRUE(selection.isCaret()); |
| 209 EXPECT_TRUE(selectionInFlatTree.isCaret()); | 209 EXPECT_TRUE(selectionInFlatTree.isCaret()); |
| 210 | 210 |
| 211 Range* range = firstRangeOf(selection); | 211 Range* range = createRange(firstEphemeralRangeOf(selection)); |
| 212 EXPECT_EQ(0u, range->startOffset()); | 212 EXPECT_EQ(0u, range->startOffset()); |
| 213 EXPECT_EQ(0u, range->endOffset()); | 213 EXPECT_EQ(0u, range->endOffset()); |
| 214 EXPECT_EQ("", range->text()); | 214 EXPECT_EQ("", range->text()); |
| 215 testFlatTreePositionsToEqualToDOMTreePositions(selection, | 215 testFlatTreePositionsToEqualToDOMTreePositions(selection, |
| 216 selectionInFlatTree); | 216 selectionInFlatTree); |
| 217 | 217 |
| 218 const VisibleSelection noSelection = | 218 const VisibleSelection noSelection = |
| 219 createVisibleSelection(SelectionInDOMTree::Builder().build()); | 219 createVisibleSelection(SelectionInDOMTree::Builder().build()); |
| 220 EXPECT_EQ(NoSelection, noSelection.getSelectionType()); | 220 EXPECT_EQ(NoSelection, noSelection.getSelectionType()); |
| 221 } | 221 } |
| (...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 343 VisibleSelectionInFlatTree selectionInFlatTree; | 343 VisibleSelectionInFlatTree selectionInFlatTree; |
| 344 | 344 |
| 345 // Beginning of a word. | 345 // Beginning of a word. |
| 346 { | 346 { |
| 347 setSelection(selection, 0); | 347 setSelection(selection, 0); |
| 348 setSelection(selectionInFlatTree, 0); | 348 setSelection(selectionInFlatTree, 0); |
| 349 selection = expandUsingGranularity(selection, WordGranularity); | 349 selection = expandUsingGranularity(selection, WordGranularity); |
| 350 selectionInFlatTree = | 350 selectionInFlatTree = |
| 351 expandUsingGranularity(selectionInFlatTree, WordGranularity); | 351 expandUsingGranularity(selectionInFlatTree, WordGranularity); |
| 352 | 352 |
| 353 Range* range = firstRangeOf(selection); | 353 Range* range = createRange(firstEphemeralRangeOf(selection)); |
| 354 EXPECT_EQ(0u, range->startOffset()); | 354 EXPECT_EQ(0u, range->startOffset()); |
| 355 EXPECT_EQ(5u, range->endOffset()); | 355 EXPECT_EQ(5u, range->endOffset()); |
| 356 EXPECT_EQ("Lorem", range->text()); | 356 EXPECT_EQ("Lorem", range->text()); |
| 357 testFlatTreePositionsToEqualToDOMTreePositions(selection, | 357 testFlatTreePositionsToEqualToDOMTreePositions(selection, |
| 358 selectionInFlatTree); | 358 selectionInFlatTree); |
| 359 } | 359 } |
| 360 | 360 |
| 361 // Middle of a word. | 361 // Middle of a word. |
| 362 { | 362 { |
| 363 setSelection(selection, 8); | 363 setSelection(selection, 8); |
| 364 setSelection(selectionInFlatTree, 8); | 364 setSelection(selectionInFlatTree, 8); |
| 365 selection = expandUsingGranularity(selection, WordGranularity); | 365 selection = expandUsingGranularity(selection, WordGranularity); |
| 366 selectionInFlatTree = | 366 selectionInFlatTree = |
| 367 expandUsingGranularity(selectionInFlatTree, WordGranularity); | 367 expandUsingGranularity(selectionInFlatTree, WordGranularity); |
| 368 | 368 |
| 369 Range* range = firstRangeOf(selection); | 369 Range* range = createRange(firstEphemeralRangeOf(selection)); |
| 370 EXPECT_EQ(6u, range->startOffset()); | 370 EXPECT_EQ(6u, range->startOffset()); |
| 371 EXPECT_EQ(11u, range->endOffset()); | 371 EXPECT_EQ(11u, range->endOffset()); |
| 372 EXPECT_EQ("ipsum", range->text()); | 372 EXPECT_EQ("ipsum", range->text()); |
| 373 testFlatTreePositionsToEqualToDOMTreePositions(selection, | 373 testFlatTreePositionsToEqualToDOMTreePositions(selection, |
| 374 selectionInFlatTree); | 374 selectionInFlatTree); |
| 375 } | 375 } |
| 376 | 376 |
| 377 // End of a word. | 377 // End of a word. |
| 378 // FIXME: that sounds buggy, we might want to select the word _before_ instead | 378 // FIXME: that sounds buggy, we might want to select the word _before_ instead |
| 379 // of the space... | 379 // of the space... |
| 380 { | 380 { |
| 381 setSelection(selection, 5); | 381 setSelection(selection, 5); |
| 382 setSelection(selectionInFlatTree, 5); | 382 setSelection(selectionInFlatTree, 5); |
| 383 selection = expandUsingGranularity(selection, WordGranularity); | 383 selection = expandUsingGranularity(selection, WordGranularity); |
| 384 selectionInFlatTree = | 384 selectionInFlatTree = |
| 385 expandUsingGranularity(selectionInFlatTree, WordGranularity); | 385 expandUsingGranularity(selectionInFlatTree, WordGranularity); |
| 386 | 386 |
| 387 Range* range = firstRangeOf(selection); | 387 Range* range = createRange(firstEphemeralRangeOf(selection)); |
| 388 EXPECT_EQ(5u, range->startOffset()); | 388 EXPECT_EQ(5u, range->startOffset()); |
| 389 EXPECT_EQ(6u, range->endOffset()); | 389 EXPECT_EQ(6u, range->endOffset()); |
| 390 EXPECT_EQ(" ", range->text()); | 390 EXPECT_EQ(" ", range->text()); |
| 391 testFlatTreePositionsToEqualToDOMTreePositions(selection, | 391 testFlatTreePositionsToEqualToDOMTreePositions(selection, |
| 392 selectionInFlatTree); | 392 selectionInFlatTree); |
| 393 } | 393 } |
| 394 | 394 |
| 395 // Before comma. | 395 // Before comma. |
| 396 // FIXME: that sounds buggy, we might want to select the word _before_ instead | 396 // FIXME: that sounds buggy, we might want to select the word _before_ instead |
| 397 // of the comma. | 397 // of the comma. |
| 398 { | 398 { |
| 399 setSelection(selection, 26); | 399 setSelection(selection, 26); |
| 400 setSelection(selectionInFlatTree, 26); | 400 setSelection(selectionInFlatTree, 26); |
| 401 selection = expandUsingGranularity(selection, WordGranularity); | 401 selection = expandUsingGranularity(selection, WordGranularity); |
| 402 selectionInFlatTree = | 402 selectionInFlatTree = |
| 403 expandUsingGranularity(selectionInFlatTree, WordGranularity); | 403 expandUsingGranularity(selectionInFlatTree, WordGranularity); |
| 404 | 404 |
| 405 Range* range = firstRangeOf(selection); | 405 Range* range = createRange(firstEphemeralRangeOf(selection)); |
| 406 EXPECT_EQ(26u, range->startOffset()); | 406 EXPECT_EQ(26u, range->startOffset()); |
| 407 EXPECT_EQ(27u, range->endOffset()); | 407 EXPECT_EQ(27u, range->endOffset()); |
| 408 EXPECT_EQ(",", range->text()); | 408 EXPECT_EQ(",", range->text()); |
| 409 testFlatTreePositionsToEqualToDOMTreePositions(selection, | 409 testFlatTreePositionsToEqualToDOMTreePositions(selection, |
| 410 selectionInFlatTree); | 410 selectionInFlatTree); |
| 411 } | 411 } |
| 412 | 412 |
| 413 // After comma. | 413 // After comma. |
| 414 { | 414 { |
| 415 setSelection(selection, 27); | 415 setSelection(selection, 27); |
| 416 setSelection(selectionInFlatTree, 27); | 416 setSelection(selectionInFlatTree, 27); |
| 417 selection = expandUsingGranularity(selection, WordGranularity); | 417 selection = expandUsingGranularity(selection, WordGranularity); |
| 418 selectionInFlatTree = | 418 selectionInFlatTree = |
| 419 expandUsingGranularity(selectionInFlatTree, WordGranularity); | 419 expandUsingGranularity(selectionInFlatTree, WordGranularity); |
| 420 | 420 |
| 421 Range* range = firstRangeOf(selection); | 421 Range* range = createRange(firstEphemeralRangeOf(selection)); |
| 422 EXPECT_EQ(27u, range->startOffset()); | 422 EXPECT_EQ(27u, range->startOffset()); |
| 423 EXPECT_EQ(28u, range->endOffset()); | 423 EXPECT_EQ(28u, range->endOffset()); |
| 424 EXPECT_EQ(" ", range->text()); | 424 EXPECT_EQ(" ", range->text()); |
| 425 testFlatTreePositionsToEqualToDOMTreePositions(selection, | 425 testFlatTreePositionsToEqualToDOMTreePositions(selection, |
| 426 selectionInFlatTree); | 426 selectionInFlatTree); |
| 427 } | 427 } |
| 428 | 428 |
| 429 // When selecting part of a word. | 429 // When selecting part of a word. |
| 430 { | 430 { |
| 431 setSelection(selection, 0, 1); | 431 setSelection(selection, 0, 1); |
| 432 setSelection(selectionInFlatTree, 0, 1); | 432 setSelection(selectionInFlatTree, 0, 1); |
| 433 selection = expandUsingGranularity(selection, WordGranularity); | 433 selection = expandUsingGranularity(selection, WordGranularity); |
| 434 selectionInFlatTree = | 434 selectionInFlatTree = |
| 435 expandUsingGranularity(selectionInFlatTree, WordGranularity); | 435 expandUsingGranularity(selectionInFlatTree, WordGranularity); |
| 436 | 436 |
| 437 Range* range = firstRangeOf(selection); | 437 Range* range = createRange(firstEphemeralRangeOf(selection)); |
| 438 EXPECT_EQ(0u, range->startOffset()); | 438 EXPECT_EQ(0u, range->startOffset()); |
| 439 EXPECT_EQ(5u, range->endOffset()); | 439 EXPECT_EQ(5u, range->endOffset()); |
| 440 EXPECT_EQ("Lorem", range->text()); | 440 EXPECT_EQ("Lorem", range->text()); |
| 441 testFlatTreePositionsToEqualToDOMTreePositions(selection, | 441 testFlatTreePositionsToEqualToDOMTreePositions(selection, |
| 442 selectionInFlatTree); | 442 selectionInFlatTree); |
| 443 } | 443 } |
| 444 | 444 |
| 445 // When selecting part of two words. | 445 // When selecting part of two words. |
| 446 { | 446 { |
| 447 setSelection(selection, 2, 8); | 447 setSelection(selection, 2, 8); |
| 448 setSelection(selectionInFlatTree, 2, 8); | 448 setSelection(selectionInFlatTree, 2, 8); |
| 449 selection = expandUsingGranularity(selection, WordGranularity); | 449 selection = expandUsingGranularity(selection, WordGranularity); |
| 450 selectionInFlatTree = | 450 selectionInFlatTree = |
| 451 expandUsingGranularity(selectionInFlatTree, WordGranularity); | 451 expandUsingGranularity(selectionInFlatTree, WordGranularity); |
| 452 | 452 |
| 453 Range* range = firstRangeOf(selection); | 453 Range* range = createRange(firstEphemeralRangeOf(selection)); |
| 454 EXPECT_EQ(0u, range->startOffset()); | 454 EXPECT_EQ(0u, range->startOffset()); |
| 455 EXPECT_EQ(11u, range->endOffset()); | 455 EXPECT_EQ(11u, range->endOffset()); |
| 456 EXPECT_EQ("Lorem ipsum", range->text()); | 456 EXPECT_EQ("Lorem ipsum", range->text()); |
| 457 testFlatTreePositionsToEqualToDOMTreePositions(selection, | 457 testFlatTreePositionsToEqualToDOMTreePositions(selection, |
| 458 selectionInFlatTree); | 458 selectionInFlatTree); |
| 459 } | 459 } |
| 460 } | 460 } |
| 461 | 461 |
| 462 // This is for crbug.com/627783, simulating restoring selection | 462 // This is for crbug.com/627783, simulating restoring selection |
| 463 // in undo stack. | 463 // in undo stack. |
| (...skipping 13 matching lines...) Expand all Loading... |
| 477 Element* host = document().getElementById("host"); | 477 Element* host = document().getElementById("host"); |
| 478 host->appendChild(sample); | 478 host->appendChild(sample); |
| 479 document().updateStyleAndLayout(); | 479 document().updateStyleAndLayout(); |
| 480 | 480 |
| 481 // Simulates to restore selection from undo stack. | 481 // Simulates to restore selection from undo stack. |
| 482 selection = createVisibleSelection(selection.asSelection()); | 482 selection = createVisibleSelection(selection.asSelection()); |
| 483 EXPECT_EQ(Position(sample->firstChild(), 0), selection.start()); | 483 EXPECT_EQ(Position(sample->firstChild(), 0), selection.start()); |
| 484 } | 484 } |
| 485 | 485 |
| 486 } // namespace blink | 486 } // namespace blink |
| OLD | NEW |