| 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 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 374 VisibleSelectionInFlatTree selectionInFlatTree; | 374 VisibleSelectionInFlatTree selectionInFlatTree; |
| 375 | 375 |
| 376 // Beginning of a word. | 376 // Beginning of a word. |
| 377 { | 377 { |
| 378 setSelection(selection, 0); | 378 setSelection(selection, 0); |
| 379 setSelection(selectionInFlatTree, 0); | 379 setSelection(selectionInFlatTree, 0); |
| 380 selection = expandUsingGranularity(selection, WordGranularity); | 380 selection = expandUsingGranularity(selection, WordGranularity); |
| 381 selectionInFlatTree = | 381 selectionInFlatTree = |
| 382 expandUsingGranularity(selectionInFlatTree, WordGranularity); | 382 expandUsingGranularity(selectionInFlatTree, WordGranularity); |
| 383 | 383 |
| 384 Range* range = firstRangeOf(selection); | 384 Range* range = createRange(firstEphemeralRangeOf(selection)); |
| 385 EXPECT_EQ(0u, range->startOffset()); | 385 EXPECT_EQ(0u, range->startOffset()); |
| 386 EXPECT_EQ(5u, range->endOffset()); | 386 EXPECT_EQ(5u, range->endOffset()); |
| 387 EXPECT_EQ("Lorem", range->text()); | 387 EXPECT_EQ("Lorem", range->text()); |
| 388 testFlatTreePositionsToEqualToDOMTreePositions(selection, | 388 testFlatTreePositionsToEqualToDOMTreePositions(selection, |
| 389 selectionInFlatTree); | 389 selectionInFlatTree); |
| 390 } | 390 } |
| 391 | 391 |
| 392 // Middle of a word. | 392 // Middle of a word. |
| 393 { | 393 { |
| 394 setSelection(selection, 8); | 394 setSelection(selection, 8); |
| 395 setSelection(selectionInFlatTree, 8); | 395 setSelection(selectionInFlatTree, 8); |
| 396 selection = expandUsingGranularity(selection, WordGranularity); | 396 selection = expandUsingGranularity(selection, WordGranularity); |
| 397 selectionInFlatTree = | 397 selectionInFlatTree = |
| 398 expandUsingGranularity(selectionInFlatTree, WordGranularity); | 398 expandUsingGranularity(selectionInFlatTree, WordGranularity); |
| 399 | 399 |
| 400 Range* range = firstRangeOf(selection); | 400 Range* range = createRange(firstEphemeralRangeOf(selection)); |
| 401 EXPECT_EQ(6u, range->startOffset()); | 401 EXPECT_EQ(6u, range->startOffset()); |
| 402 EXPECT_EQ(11u, range->endOffset()); | 402 EXPECT_EQ(11u, range->endOffset()); |
| 403 EXPECT_EQ("ipsum", range->text()); | 403 EXPECT_EQ("ipsum", range->text()); |
| 404 testFlatTreePositionsToEqualToDOMTreePositions(selection, | 404 testFlatTreePositionsToEqualToDOMTreePositions(selection, |
| 405 selectionInFlatTree); | 405 selectionInFlatTree); |
| 406 } | 406 } |
| 407 | 407 |
| 408 // End of a word. | 408 // End of a word. |
| 409 // FIXME: that sounds buggy, we might want to select the word _before_ instead | 409 // FIXME: that sounds buggy, we might want to select the word _before_ instead |
| 410 // of the space... | 410 // of the space... |
| 411 { | 411 { |
| 412 setSelection(selection, 5); | 412 setSelection(selection, 5); |
| 413 setSelection(selectionInFlatTree, 5); | 413 setSelection(selectionInFlatTree, 5); |
| 414 selection = expandUsingGranularity(selection, WordGranularity); | 414 selection = expandUsingGranularity(selection, WordGranularity); |
| 415 selectionInFlatTree = | 415 selectionInFlatTree = |
| 416 expandUsingGranularity(selectionInFlatTree, WordGranularity); | 416 expandUsingGranularity(selectionInFlatTree, WordGranularity); |
| 417 | 417 |
| 418 Range* range = firstRangeOf(selection); | 418 Range* range = createRange(firstEphemeralRangeOf(selection)); |
| 419 EXPECT_EQ(5u, range->startOffset()); | 419 EXPECT_EQ(5u, range->startOffset()); |
| 420 EXPECT_EQ(6u, range->endOffset()); | 420 EXPECT_EQ(6u, range->endOffset()); |
| 421 EXPECT_EQ(" ", range->text()); | 421 EXPECT_EQ(" ", range->text()); |
| 422 testFlatTreePositionsToEqualToDOMTreePositions(selection, | 422 testFlatTreePositionsToEqualToDOMTreePositions(selection, |
| 423 selectionInFlatTree); | 423 selectionInFlatTree); |
| 424 } | 424 } |
| 425 | 425 |
| 426 // Before comma. | 426 // Before comma. |
| 427 // FIXME: that sounds buggy, we might want to select the word _before_ instead | 427 // FIXME: that sounds buggy, we might want to select the word _before_ instead |
| 428 // of the comma. | 428 // of the comma. |
| 429 { | 429 { |
| 430 setSelection(selection, 26); | 430 setSelection(selection, 26); |
| 431 setSelection(selectionInFlatTree, 26); | 431 setSelection(selectionInFlatTree, 26); |
| 432 selection = expandUsingGranularity(selection, WordGranularity); | 432 selection = expandUsingGranularity(selection, WordGranularity); |
| 433 selectionInFlatTree = | 433 selectionInFlatTree = |
| 434 expandUsingGranularity(selectionInFlatTree, WordGranularity); | 434 expandUsingGranularity(selectionInFlatTree, WordGranularity); |
| 435 | 435 |
| 436 Range* range = firstRangeOf(selection); | 436 Range* range = createRange(firstEphemeralRangeOf(selection)); |
| 437 EXPECT_EQ(26u, range->startOffset()); | 437 EXPECT_EQ(26u, range->startOffset()); |
| 438 EXPECT_EQ(27u, range->endOffset()); | 438 EXPECT_EQ(27u, range->endOffset()); |
| 439 EXPECT_EQ(",", range->text()); | 439 EXPECT_EQ(",", range->text()); |
| 440 testFlatTreePositionsToEqualToDOMTreePositions(selection, | 440 testFlatTreePositionsToEqualToDOMTreePositions(selection, |
| 441 selectionInFlatTree); | 441 selectionInFlatTree); |
| 442 } | 442 } |
| 443 | 443 |
| 444 // After comma. | 444 // After comma. |
| 445 { | 445 { |
| 446 setSelection(selection, 27); | 446 setSelection(selection, 27); |
| 447 setSelection(selectionInFlatTree, 27); | 447 setSelection(selectionInFlatTree, 27); |
| 448 selection = expandUsingGranularity(selection, WordGranularity); | 448 selection = expandUsingGranularity(selection, WordGranularity); |
| 449 selectionInFlatTree = | 449 selectionInFlatTree = |
| 450 expandUsingGranularity(selectionInFlatTree, WordGranularity); | 450 expandUsingGranularity(selectionInFlatTree, WordGranularity); |
| 451 | 451 |
| 452 Range* range = firstRangeOf(selection); | 452 Range* range = createRange(firstEphemeralRangeOf(selection)); |
| 453 EXPECT_EQ(27u, range->startOffset()); | 453 EXPECT_EQ(27u, range->startOffset()); |
| 454 EXPECT_EQ(28u, range->endOffset()); | 454 EXPECT_EQ(28u, range->endOffset()); |
| 455 EXPECT_EQ(" ", range->text()); | 455 EXPECT_EQ(" ", range->text()); |
| 456 testFlatTreePositionsToEqualToDOMTreePositions(selection, | 456 testFlatTreePositionsToEqualToDOMTreePositions(selection, |
| 457 selectionInFlatTree); | 457 selectionInFlatTree); |
| 458 } | 458 } |
| 459 | 459 |
| 460 // When selecting part of a word. | 460 // When selecting part of a word. |
| 461 { | 461 { |
| 462 setSelection(selection, 0, 1); | 462 setSelection(selection, 0, 1); |
| 463 setSelection(selectionInFlatTree, 0, 1); | 463 setSelection(selectionInFlatTree, 0, 1); |
| 464 selection = expandUsingGranularity(selection, WordGranularity); | 464 selection = expandUsingGranularity(selection, WordGranularity); |
| 465 selectionInFlatTree = | 465 selectionInFlatTree = |
| 466 expandUsingGranularity(selectionInFlatTree, WordGranularity); | 466 expandUsingGranularity(selectionInFlatTree, WordGranularity); |
| 467 | 467 |
| 468 Range* range = firstRangeOf(selection); | 468 Range* range = createRange(firstEphemeralRangeOf(selection)); |
| 469 EXPECT_EQ(0u, range->startOffset()); | 469 EXPECT_EQ(0u, range->startOffset()); |
| 470 EXPECT_EQ(5u, range->endOffset()); | 470 EXPECT_EQ(5u, range->endOffset()); |
| 471 EXPECT_EQ("Lorem", range->text()); | 471 EXPECT_EQ("Lorem", range->text()); |
| 472 testFlatTreePositionsToEqualToDOMTreePositions(selection, | 472 testFlatTreePositionsToEqualToDOMTreePositions(selection, |
| 473 selectionInFlatTree); | 473 selectionInFlatTree); |
| 474 } | 474 } |
| 475 | 475 |
| 476 // When selecting part of two words. | 476 // When selecting part of two words. |
| 477 { | 477 { |
| 478 setSelection(selection, 2, 8); | 478 setSelection(selection, 2, 8); |
| 479 setSelection(selectionInFlatTree, 2, 8); | 479 setSelection(selectionInFlatTree, 2, 8); |
| 480 selection = expandUsingGranularity(selection, WordGranularity); | 480 selection = expandUsingGranularity(selection, WordGranularity); |
| 481 selectionInFlatTree = | 481 selectionInFlatTree = |
| 482 expandUsingGranularity(selectionInFlatTree, WordGranularity); | 482 expandUsingGranularity(selectionInFlatTree, WordGranularity); |
| 483 | 483 |
| 484 Range* range = firstRangeOf(selection); | 484 Range* range = createRange(firstEphemeralRangeOf(selection)); |
| 485 EXPECT_EQ(0u, range->startOffset()); | 485 EXPECT_EQ(0u, range->startOffset()); |
| 486 EXPECT_EQ(11u, range->endOffset()); | 486 EXPECT_EQ(11u, range->endOffset()); |
| 487 EXPECT_EQ("Lorem ipsum", range->text()); | 487 EXPECT_EQ("Lorem ipsum", range->text()); |
| 488 testFlatTreePositionsToEqualToDOMTreePositions(selection, | 488 testFlatTreePositionsToEqualToDOMTreePositions(selection, |
| 489 selectionInFlatTree); | 489 selectionInFlatTree); |
| 490 } | 490 } |
| 491 } | 491 } |
| 492 | 492 |
| 493 // This is for crbug.com/627783, simulating restoring selection | 493 // This is for crbug.com/627783, simulating restoring selection |
| 494 // in undo stack. | 494 // in undo stack. |
| (...skipping 13 matching lines...) Expand all Loading... |
| 508 Element* host = document().getElementById("host"); | 508 Element* host = document().getElementById("host"); |
| 509 host->appendChild(sample); | 509 host->appendChild(sample); |
| 510 document().updateStyleAndLayout(); | 510 document().updateStyleAndLayout(); |
| 511 | 511 |
| 512 // Simulates to restore selection from undo stack. | 512 // Simulates to restore selection from undo stack. |
| 513 selection = createVisibleSelection(selection.asSelection()); | 513 selection = createVisibleSelection(selection.asSelection()); |
| 514 EXPECT_EQ(Position(sample->firstChild(), 0), selection.start()); | 514 EXPECT_EQ(Position(sample->firstChild(), 0), selection.start()); |
| 515 } | 515 } |
| 516 | 516 |
| 517 } // namespace blink | 517 } // namespace blink |
| OLD | NEW |