Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright (C) 2006, 2007, 2008, 2011 Apple Inc. All rights reserved. | 2 * Copyright (C) 2006, 2007, 2008, 2011 Apple Inc. All rights reserved. |
| 3 * Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies) | 3 * Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies) |
| 4 * | 4 * |
| 5 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
| 6 * modification, are permitted provided that the following conditions | 6 * modification, are permitted provided that the following conditions |
| 7 * are met: | 7 * are met: |
| 8 * 1. Redistributions of source code must retain the above copyright | 8 * 1. Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
| 10 * 2. Redistributions in binary form must reproduce the above copyright | 10 * 2. Redistributions in binary form must reproduce the above copyright |
| (...skipping 339 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 350 if (!selection.isNone() && !m_compositionRange->collapsed()) { | 350 if (!selection.isNone() && !m_compositionRange->collapsed()) { |
| 351 if (selection.start().compareTo(m_compositionRange->startPosition()) >= 0 && | 351 if (selection.start().compareTo(m_compositionRange->startPosition()) >= 0 && |
| 352 selection.end().compareTo(m_compositionRange->endPosition()) <= 0) | 352 selection.end().compareTo(m_compositionRange->endPosition()) <= 0) |
| 353 return; | 353 return; |
| 354 } | 354 } |
| 355 | 355 |
| 356 cancelComposition(); | 356 cancelComposition(); |
| 357 frame().chromeClient().didCancelCompositionOnSelectionChange(); | 357 frame().chromeClient().didCancelCompositionOnSelectionChange(); |
| 358 } | 358 } |
| 359 | 359 |
| 360 static size_t computeCommonPrefixLength(const String& str1, | |
| 361 const String& str2) { | |
| 362 const size_t maxCommonPrefixLength = std::min(str1.length(), str2.length()); | |
| 363 for (size_t index = 0; index < maxCommonPrefixLength; ++index) { | |
| 364 if (str1[index] != str2[index]) | |
| 365 return index; | |
| 366 } | |
| 367 return maxCommonPrefixLength; | |
| 368 } | |
| 369 | |
| 370 static size_t computeCommonSuffixLength(const String& str1, | |
| 371 const String& str2) { | |
| 372 const size_t length1 = str1.length(); | |
| 373 const size_t length2 = str2.length(); | |
| 374 const size_t maxCommonSuffixLength = std::min(length1, length2); | |
| 375 for (size_t index = 0; index < maxCommonSuffixLength; ++index) { | |
| 376 if (str1[length1 - index - 1] != str2[length2 - index - 1]) | |
| 377 return index; | |
| 378 } | |
| 379 return maxCommonSuffixLength; | |
| 380 } | |
| 381 | |
| 382 static size_t computeCommonGraphemeClusterPrefixLengthForSetComposition( | |
| 383 const String& oldText, | |
| 384 const String& newText, | |
| 385 const Element* rootEditableElement) { | |
| 386 const size_t commonPrefixLength = computeCommonPrefixLength(oldText, newText); | |
| 387 | |
| 388 // For grapheme cluster, we should adjust it for grapheme boundary. | |
| 389 const EphemeralRange& range = | |
| 390 PlainTextRange(0, commonPrefixLength).createRange(*rootEditableElement); | |
| 391 if (range.isNull()) | |
| 392 return 0; | |
| 393 const Position& position = range.endPosition(); | |
| 394 const Position& adjustedPosition = previousPositionOf( | |
| 395 nextPositionOf(position, PositionMoveType::GraphemeCluster), | |
| 396 PositionMoveType::GraphemeCluster); | |
| 397 DCHECK_EQ(position.anchorNode(), adjustedPosition.anchorNode()); | |
| 398 DCHECK_GE(position.computeOffsetInContainerNode(), | |
| 399 adjustedPosition.computeOffsetInContainerNode()); | |
| 400 const int diff = position.computeOffsetInContainerNode() - | |
| 401 adjustedPosition.computeOffsetInContainerNode(); | |
| 402 return commonPrefixLength - static_cast<size_t>(diff); | |
| 403 } | |
| 404 | |
| 405 static size_t computeCommonGraphemeClusterSuffixLengthForSetComposition( | |
| 406 const String& oldText, | |
| 407 const String& newText, | |
| 408 const Element* rootEditableElement) { | |
| 409 const size_t commonSuffixLength = computeCommonSuffixLength(oldText, newText); | |
| 410 | |
| 411 // For grapheme cluster, we should adjust it for grapheme boundary. | |
| 412 const EphemeralRange& range = | |
| 413 PlainTextRange(0, oldText.length() - commonSuffixLength) | |
| 414 .createRange(*rootEditableElement); | |
| 415 if (range.isNull()) | |
| 416 return 0; | |
| 417 const Position& position = range.endPosition(); | |
| 418 const Position& adjustedPosition = nextPositionOf( | |
| 419 previousPositionOf(position, PositionMoveType::GraphemeCluster), | |
| 420 PositionMoveType::GraphemeCluster); | |
| 421 DCHECK_EQ(position.anchorNode(), adjustedPosition.anchorNode()); | |
| 422 DCHECK_GE(adjustedPosition.computeOffsetInContainerNode(), | |
| 423 position.computeOffsetInContainerNode()); | |
| 424 const int diff = adjustedPosition.computeOffsetInContainerNode() - | |
| 425 position.computeOffsetInContainerNode(); | |
| 426 return commonSuffixLength - static_cast<size_t>(diff); | |
| 427 } | |
| 428 | |
| 429 void InputMethodController::setCompositionWithIncrementalText( | |
| 430 const String& text, | |
| 431 const Vector<CompositionUnderline>& underlines, | |
| 432 int selectionStart, | |
| 433 int selectionEnd) { | |
| 434 Element* editable = frame().selection().rootEditableElement(); | |
|
yosin_UTC9
2016/10/12 08:12:52
Just in case, please add
DCHECK_GE(selectionStart,
yabinh
2016/10/12 08:18:40
Done.
yabinh
2016/10/12 09:14:08
I just found that selectionStart and selectionEnd
| |
| 435 if (!editable) | |
| 436 return; | |
| 437 | |
| 438 String composing = composingText(); | |
| 439 const size_t commonPrefixLength = | |
| 440 computeCommonGraphemeClusterPrefixLengthForSetComposition(composing, text, | |
| 441 editable); | |
| 442 | |
| 443 // We should ignore common prefix when finding common suffix. | |
| 444 const size_t commonSuffixLength = | |
| 445 computeCommonGraphemeClusterSuffixLengthForSetComposition( | |
| 446 composing.right(composing.length() - commonPrefixLength), | |
| 447 text.right(text.length() - commonPrefixLength), editable); | |
| 448 | |
| 449 const bool inserting = | |
| 450 text.length() > commonPrefixLength + commonSuffixLength; | |
| 451 const bool deleting = | |
| 452 composing.length() > commonPrefixLength + commonSuffixLength; | |
| 453 | |
| 454 if (inserting || deleting) { | |
| 455 // Select the text to be deleted. | |
| 456 const size_t compositionStart = | |
| 457 PlainTextRange::create(*editable, compositionEphemeralRange()).start(); | |
| 458 const size_t deletionStart = compositionStart + commonPrefixLength; | |
| 459 const size_t deletionEnd = | |
| 460 compositionStart + composing.length() - commonSuffixLength; | |
| 461 const EphemeralRange& deletionRange = | |
| 462 PlainTextRange(deletionStart, deletionEnd).createRange(*editable); | |
| 463 VisibleSelection selection; | |
| 464 selection.setWithoutValidation(deletionRange.startPosition(), | |
| 465 deletionRange.endPosition()); | |
| 466 Document* const currentDocument = frame().document(); | |
| 467 frame().selection().setSelection(selection, 0); | |
| 468 clear(); | |
| 469 | |
| 470 // FrameSeleciton::setSelection() can change document associate to |frame|. | |
| 471 if (currentDocument != frame().document()) | |
| 472 return; | |
| 473 if (!currentDocument->focusedElement()) | |
| 474 return; | |
| 475 | |
| 476 // Insert the incremental text. | |
| 477 const size_t insertionLength = | |
| 478 text.length() - commonPrefixLength - commonSuffixLength; | |
| 479 const String& insertingText = | |
| 480 text.substring(commonPrefixLength, insertionLength); | |
| 481 insertTextDuringCompositionWithEvents(frame(), insertingText, | |
| 482 TypingCommand::PreventSpellChecking, | |
| 483 TypingCommand::TextCompositionUpdate); | |
| 484 | |
| 485 // Event handlers might destroy document. | |
| 486 if (currentDocument != frame().document()) | |
| 487 return; | |
| 488 | |
| 489 // TODO(yosin): The use of updateStyleAndLayoutIgnorePendingStylesheets | |
| 490 // needs to be audited. see http://crbug.com/590369 for more details. | |
| 491 frame().document()->updateStyleAndLayoutIgnorePendingStylesheets(); | |
| 492 | |
| 493 // Now recreate the composition starting at its original start, and | |
| 494 // apply the specified final selection offsets. | |
| 495 setCompositionFromExistingText(underlines, compositionStart, | |
| 496 compositionStart + text.length()); | |
| 497 } | |
| 498 | |
| 499 selectComposition(); | |
| 500 | |
| 501 // TODO(xiaochengh): The use of updateStyleAndLayoutIgnorePendingStylesheets | |
| 502 // needs to be audited. see http://crbug.com/590369 for more details. | |
| 503 frame().document()->updateStyleAndLayoutIgnorePendingStylesheets(); | |
| 504 | |
| 505 const PlainTextRange& selectedRange = createSelectionRangeForSetComposition( | |
| 506 selectionStart, selectionEnd, text.length()); | |
| 507 // We shouldn't close typing in the middle of setComposition. | |
| 508 setEditableSelectionOffsets(selectedRange, NotUserTriggered); | |
| 509 m_isDirty = true; | |
| 510 } | |
| 511 | |
| 360 void InputMethodController::setComposition( | 512 void InputMethodController::setComposition( |
| 361 const String& text, | 513 const String& text, |
| 362 const Vector<CompositionUnderline>& underlines, | 514 const Vector<CompositionUnderline>& underlines, |
| 363 int selectionStart, | 515 int selectionStart, |
| 364 int selectionEnd) { | 516 int selectionEnd) { |
| 365 Editor::RevealSelectionScope revealSelectionScope(&editor()); | 517 Editor::RevealSelectionScope revealSelectionScope(&editor()); |
| 366 | 518 |
| 367 // Updates styles before setting selection for composition to prevent | 519 // Updates styles before setting selection for composition to prevent |
| 368 // inserting the previous composition text into text nodes oddly. | 520 // inserting the previous composition text into text nodes oddly. |
| 369 // See https://bugs.webkit.org/show_bug.cgi?id=46868 | 521 // See https://bugs.webkit.org/show_bug.cgi?id=46868 |
| 370 frame().document()->updateStyleAndLayoutTree(); | 522 frame().document()->updateStyleAndLayoutTree(); |
| 371 | 523 |
| 524 // When the IME only wants to change a few characters at the end of the | |
| 525 // composition, only touch those characters in order to preserve rich text | |
| 526 // substructure. | |
| 527 if (hasComposition() && text.length()) { | |
| 528 return setCompositionWithIncrementalText(text, underlines, selectionStart, | |
| 529 selectionEnd); | |
| 530 } | |
| 531 | |
| 372 selectComposition(); | 532 selectComposition(); |
| 373 | 533 |
| 374 if (frame().selection().isNone()) | 534 if (frame().selection().isNone()) |
| 375 return; | 535 return; |
| 376 | 536 |
| 377 Element* target = frame().document()->focusedElement(); | 537 Element* target = frame().document()->focusedElement(); |
| 378 if (!target) | 538 if (!target) |
| 379 return; | 539 return; |
| 380 | 540 |
| 381 // TODO(xiaochengh): The use of updateStyleAndLayoutIgnorePendingStylesheets | 541 // TODO(xiaochengh): The use of updateStyleAndLayoutIgnorePendingStylesheets |
| 382 // needs to be audited. see http://crbug.com/590369 for more details. | 542 // needs to be audited. see http://crbug.com/590369 for more details. |
| 383 frame().document()->updateStyleAndLayoutIgnorePendingStylesheets(); | 543 frame().document()->updateStyleAndLayoutIgnorePendingStylesheets(); |
| 384 | 544 |
| 385 int selectionOffsetsStart = static_cast<int>(getSelectionOffsets().start()); | 545 PlainTextRange selectedRange = createSelectionRangeForSetComposition( |
| 386 int start = selectionOffsetsStart + selectionStart; | 546 selectionStart, selectionEnd, text.length()); |
| 387 int end = selectionOffsetsStart + selectionEnd; | |
| 388 PlainTextRange selectedRange = | |
| 389 createRangeForSelection(start, end, text.length()); | |
| 390 | 547 |
| 391 // Dispatch an appropriate composition event to the focused node. | 548 // Dispatch an appropriate composition event to the focused node. |
| 392 // We check the composition status and choose an appropriate composition event | 549 // We check the composition status and choose an appropriate composition event |
| 393 // since this function is used for three purposes: | 550 // since this function is used for three purposes: |
| 394 // 1. Starting a new composition. | 551 // 1. Starting a new composition. |
| 395 // Send a compositionstart and a compositionupdate event when this function | 552 // Send a compositionstart and a compositionupdate event when this function |
| 396 // creates a new composition node, i.e. !hasComposition() && | 553 // creates a new composition node, i.e. !hasComposition() && |
| 397 // !text.isEmpty(). | 554 // !text.isEmpty(). |
| 398 // Sending a compositionupdate event at this time ensures that at least one | 555 // Sending a compositionupdate event at this time ensures that at least one |
| 399 // compositionupdate event is dispatched. | 556 // compositionupdate event is dispatched. |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 496 EphemeralRange ephemeralLineRange = EphemeralRange( | 653 EphemeralRange ephemeralLineRange = EphemeralRange( |
| 497 Position(baseNode, underlineStart), Position(baseNode, underlineEnd)); | 654 Position(baseNode, underlineStart), Position(baseNode, underlineEnd)); |
| 498 if (ephemeralLineRange.isNull()) | 655 if (ephemeralLineRange.isNull()) |
| 499 continue; | 656 continue; |
| 500 frame().document()->markers().addCompositionMarker( | 657 frame().document()->markers().addCompositionMarker( |
| 501 ephemeralLineRange.startPosition(), ephemeralLineRange.endPosition(), | 658 ephemeralLineRange.startPosition(), ephemeralLineRange.endPosition(), |
| 502 underline.color(), underline.thick(), underline.backgroundColor()); | 659 underline.color(), underline.thick(), underline.backgroundColor()); |
| 503 } | 660 } |
| 504 } | 661 } |
| 505 | 662 |
| 663 PlainTextRange InputMethodController::createSelectionRangeForSetComposition( | |
| 664 int selectionStart, | |
| 665 int selectionEnd, | |
| 666 size_t textLength) const { | |
| 667 const int selectionOffsetsStart = | |
| 668 static_cast<int>(getSelectionOffsets().start()); | |
| 669 const int start = selectionOffsetsStart + selectionStart; | |
| 670 const int end = selectionOffsetsStart + selectionEnd; | |
| 671 return createRangeForSelection(start, end, textLength); | |
| 672 } | |
| 673 | |
| 506 void InputMethodController::setCompositionFromExistingText( | 674 void InputMethodController::setCompositionFromExistingText( |
| 507 const Vector<CompositionUnderline>& underlines, | 675 const Vector<CompositionUnderline>& underlines, |
| 508 unsigned compositionStart, | 676 unsigned compositionStart, |
| 509 unsigned compositionEnd) { | 677 unsigned compositionEnd) { |
| 510 Element* editable = frame().selection().rootEditableElement(); | 678 Element* editable = frame().selection().rootEditableElement(); |
| 511 if (!editable) | 679 if (!editable) |
| 512 return; | 680 return; |
| 513 | 681 |
| 514 DCHECK(!editable->document().needsLayoutTreeUpdate()); | 682 DCHECK(!editable->document().needsLayoutTreeUpdate()); |
| 515 | 683 |
| (...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 684 new RangeVector(1, m_frame->selection().firstRange())); | 852 new RangeVector(1, m_frame->selection().firstRange())); |
| 685 TypingCommand::deleteSelection(*frame().document()); | 853 TypingCommand::deleteSelection(*frame().document()); |
| 686 } | 854 } |
| 687 | 855 |
| 688 DEFINE_TRACE(InputMethodController) { | 856 DEFINE_TRACE(InputMethodController) { |
| 689 visitor->trace(m_frame); | 857 visitor->trace(m_frame); |
| 690 visitor->trace(m_compositionRange); | 858 visitor->trace(m_compositionRange); |
| 691 } | 859 } |
| 692 | 860 |
| 693 } // namespace blink | 861 } // namespace blink |
| OLD | NEW |