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/FrameSelection.h" | 5 #include "core/editing/FrameSelection.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include "bindings/core/v8/ExceptionState.h" | 8 #include "bindings/core/v8/ExceptionState.h" |
9 #include "core/dom/Document.h" | 9 #include "core/dom/Document.h" |
10 #include "core/dom/Element.h" | 10 #include "core/dom/Element.h" |
(...skipping 310 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
321 | 321 |
322 Selection().SetSelectedRange( | 322 Selection().SetSelectedRange( |
323 EphemeralRange(Position(text, 0), Position(text, 12)), | 323 EphemeralRange(Position(text, 0), Position(text, 12)), |
324 VP_DEFAULT_AFFINITY, SelectionDirectionalMode::kNonDirectional, 0); | 324 VP_DEFAULT_AFFINITY, SelectionDirectionalMode::kNonDirectional, 0); |
325 | 325 |
326 EXPECT_TRUE(Selection().IsHandleVisible()) | 326 EXPECT_TRUE(Selection().IsHandleVisible()) |
327 << "If handles were present before" | 327 << "If handles were present before" |
328 "selectSetSelectedRange they should be present after it."; | 328 "selectSetSelectedRange they should be present after it."; |
329 } | 329 } |
330 | 330 |
| 331 TEST_F(FrameSelectionTest, CaretInFocusedShadowTree) { |
| 332 SetBodyContent("<input id='field'>"); // <input> hosts a shadow tree. |
| 333 EXPECT_TRUE(Selection().GetSelectionInDOMTree().IsNone()); |
| 334 EXPECT_TRUE(Selection().IsHidden()); |
| 335 |
| 336 Element* const field = GetDocument().GetElementById("field"); |
| 337 field->focus(); |
| 338 EXPECT_TRUE(Selection().GetSelectionInDOMTree().IsCaret()); |
| 339 EXPECT_FALSE(Selection().IsHidden()); |
| 340 |
| 341 field->blur(); // Move focus to document body. |
| 342 EXPECT_TRUE(Selection().GetSelectionInDOMTree().IsCaret()); |
| 343 EXPECT_TRUE(Selection().IsHidden()); // Caret is now hidden. |
| 344 } |
| 345 |
| 346 TEST_F(FrameSelectionTest, CaretInFocusedEditableDiv) { |
| 347 SetBodyContent("<div contenteditable id='ce'>blabla</div>"); |
| 348 EXPECT_TRUE(Selection().GetSelectionInDOMTree().IsNone()); |
| 349 EXPECT_TRUE(Selection().IsHidden()); |
| 350 |
| 351 Element* const ce = GetDocument().GetElementById("ce"); |
| 352 ce->focus(); |
| 353 EXPECT_TRUE(Selection().GetSelectionInDOMTree().IsCaret()); |
| 354 EXPECT_FALSE(Selection().IsHidden()); |
| 355 |
| 356 ce->blur(); // Move focus to document body. |
| 357 EXPECT_TRUE(Selection().GetSelectionInDOMTree().IsCaret()); |
| 358 EXPECT_TRUE(Selection().IsHidden()); // Caret is now hidden. |
| 359 } |
| 360 |
| 361 TEST_F(FrameSelectionTest, SelectionInFocusedEditableDiv) { |
| 362 SetBodyContent("<div contenteditable id='ce'>blabla</div>"); |
| 363 EXPECT_TRUE(Selection().GetSelectionInDOMTree().IsNone()); |
| 364 EXPECT_TRUE(Selection().IsHidden()); |
| 365 |
| 366 Element* const ce = GetDocument().GetElementById("ce"); |
| 367 ce->focus(); |
| 368 EXPECT_TRUE(Selection().GetSelectionInDOMTree().IsCaret()); |
| 369 EXPECT_FALSE(Selection().IsHidden()); |
| 370 |
| 371 Selection().SelectAll(); |
| 372 EXPECT_TRUE(Selection().GetSelectionInDOMTree().IsRange()); |
| 373 EXPECT_FALSE(Selection().IsHidden()); |
| 374 |
| 375 ce->blur(); // Move focus to document body. |
| 376 EXPECT_TRUE(Selection().GetSelectionInDOMTree().IsRange()); |
| 377 EXPECT_FALSE(Selection().IsHidden()); // Range is still visible. |
| 378 } |
| 379 |
| 380 // crbug.com/692898 |
| 381 TEST_F(FrameSelectionTest, FocusingLinkHidesCaretInTextControl) { |
| 382 SetBodyContent("<input id='field'><a href='www' id='alink'>link</a>"); |
| 383 EXPECT_TRUE(Selection().GetSelectionInDOMTree().IsNone()); |
| 384 EXPECT_TRUE(Selection().IsHidden()); |
| 385 |
| 386 Element* const field = GetDocument().GetElementById("field"); |
| 387 field->focus(); |
| 388 EXPECT_FALSE(Selection().IsHidden()); |
| 389 EXPECT_TRUE(Selection().GetSelectionInDOMTree().IsCaret()); |
| 390 |
| 391 Element* const alink = GetDocument().GetElementById("alink"); |
| 392 alink->focus(); |
| 393 EXPECT_TRUE(Selection().GetSelectionInDOMTree().IsCaret()); |
| 394 EXPECT_TRUE(Selection().IsHidden()); |
| 395 } |
| 396 |
| 397 // crbug.com/692898 |
| 398 TEST_F(FrameSelectionTest, FocusingLinkHidesSelectionInTextControl) { |
| 399 SetBodyContent("<input id='field'><a href='www' id='alink'>link</a>"); |
| 400 EXPECT_TRUE(Selection().GetSelectionInDOMTree().IsNone()); |
| 401 EXPECT_TRUE(Selection().IsHidden()); |
| 402 |
| 403 Element* const field = GetDocument().GetElementById("field"); |
| 404 field->focus(); |
| 405 EXPECT_FALSE(Selection().IsHidden()); |
| 406 EXPECT_TRUE(Selection().GetSelectionInDOMTree().IsCaret()); |
| 407 |
| 408 Selection().SelectAll(); |
| 409 EXPECT_TRUE(Selection().GetSelectionInDOMTree().IsRange()); |
| 410 EXPECT_FALSE(Selection().IsHidden()); |
| 411 |
| 412 Element* const alink = GetDocument().GetElementById("alink"); |
| 413 alink->focus(); |
| 414 EXPECT_TRUE(Selection().GetSelectionInDOMTree().IsRange()); |
| 415 EXPECT_TRUE(Selection().IsHidden()); |
| 416 } |
| 417 |
| 418 // crbug.com/713051 |
| 419 TEST_F(FrameSelectionTest, FocusedNonEditableParent) { |
| 420 SetBodyContent("<div tabindex='-1' id='parent'><input id='field'></div>"); |
| 421 EXPECT_TRUE(Selection().GetSelectionInDOMTree().IsNone()); |
| 422 EXPECT_TRUE(Selection().IsHidden()); |
| 423 |
| 424 Element* const field = GetDocument().GetElementById("field"); |
| 425 field->focus(); |
| 426 EXPECT_FALSE(Selection().IsHidden()); |
| 427 EXPECT_TRUE(Selection().GetSelectionInDOMTree().IsCaret()); |
| 428 |
| 429 // Here the selection belongs to <input>'s shadow tree and that tree has a |
| 430 // non-editable parent that is is focused. |
| 431 Element* const parent = GetDocument().GetElementById("parent"); |
| 432 parent->focus(); |
| 433 EXPECT_TRUE(Selection().GetSelectionInDOMTree().IsCaret()); |
| 434 EXPECT_TRUE(Selection().IsHidden()); // Focus is outside editing boundary. |
| 435 } |
| 436 |
| 437 // crbug.com/707143 |
| 438 TEST_F(FrameSelectionTest, RangeContainsFocus) { |
| 439 SetBodyContent( |
| 440 "<div id='outer'>" |
| 441 " <div>" |
| 442 " <span id='start'>start</span>" |
| 443 " </div>" |
| 444 " <a href='www' id='alink'>link</a>" |
| 445 " <div>line 1</div>" |
| 446 " <div id='middle'>line 2</div>" |
| 447 " <div>line 3</div>" |
| 448 " <div>line 4</div>" |
| 449 " <span id='end'>end</span>" |
| 450 " <div></div>" |
| 451 "</div>"); |
| 452 EXPECT_TRUE(Selection().GetSelectionInDOMTree().IsNone()); |
| 453 EXPECT_TRUE(Selection().IsHidden()); |
| 454 |
| 455 Element* const start = GetDocument().GetElementById("start"); |
| 456 Element* const end = GetDocument().GetElementById("end"); |
| 457 Selection().SetSelection( |
| 458 SelectionInDOMTree::Builder() |
| 459 .SetBaseAndExtent(Position(start, 0), Position(end, 0)) |
| 460 .Build()); |
| 461 EXPECT_FALSE(Selection().IsHidden()); |
| 462 EXPECT_TRUE(Selection().GetSelectionInDOMTree().IsRange()); |
| 463 |
| 464 Element* const alink = GetDocument().GetElementById("alink"); |
| 465 alink->focus(); |
| 466 EXPECT_FALSE(Selection().IsHidden()); // Range still visible. |
| 467 EXPECT_TRUE(Selection().GetSelectionInDOMTree().IsRange()); |
| 468 } |
| 469 |
| 470 // crbug.com/707143 |
| 471 TEST_F(FrameSelectionTest, RangeOutsideFocus) { |
| 472 SetBodyContent( |
| 473 "<a href='www' id='alink'>link</a>" |
| 474 "<div id='outer'>" |
| 475 " <div>" |
| 476 " <span id='start'>start</span>" |
| 477 " </div>" |
| 478 " <div>line 1</div>" |
| 479 " <div id='middle'>line 2</div>" |
| 480 " <div>line 3</div>" |
| 481 " <div>line 4</div>" |
| 482 " <span id='end'>end</span>" |
| 483 " <div></div>" |
| 484 "</div>"); |
| 485 EXPECT_TRUE(Selection().GetSelectionInDOMTree().IsNone()); |
| 486 EXPECT_TRUE(Selection().IsHidden()); |
| 487 |
| 488 Element* const start = GetDocument().GetElementById("start"); |
| 489 Element* const end = GetDocument().GetElementById("end"); |
| 490 Selection().SetSelection( |
| 491 SelectionInDOMTree::Builder() |
| 492 .SetBaseAndExtent(Position(start, 0), Position(end, 0)) |
| 493 .Build()); |
| 494 EXPECT_FALSE(Selection().IsHidden()); |
| 495 EXPECT_TRUE(Selection().GetSelectionInDOMTree().IsRange()); |
| 496 |
| 497 Element* const alink = GetDocument().GetElementById("alink"); |
| 498 alink->focus(); |
| 499 EXPECT_FALSE(Selection().IsHidden()); // Range still visible. |
| 500 EXPECT_TRUE(Selection().GetSelectionInDOMTree().IsRange()); |
| 501 } |
| 502 |
331 } // namespace blink | 503 } // namespace blink |
OLD | NEW |