Commit 1d58bc40 authored by Ryan Landay's avatar Ryan Landay Committed by Commit Bot

Make InputMethodController::SetComposition() set composition range from UndoStep

TypingCommand::InsertText(const String&, bool, EditingState*) is sort of messed
up right now because it calls AdjustSelectionAfterIncrementalInsertion(), which
calls FrameSelection::SetSelection(), which we want to hoist out of here (see
discussion at https://goo.gl/GFgLF3).

This CL removes AdjustSelectionAfterIncrementalInsertion's call to
SetSelection() and replaces it with some logic in
InputMethodController::SetComposition() to pull the ending selection out of the
most recent UndoStep. This sets us up to avoid an unnecessary selectionchange
event. Currently, SetComposition() does the following:

1. Selects the composition range.
2. Replaces the selection with the new text (requesting that the new text be
   left selected).
3. Converts the selection back into a composition range.

This CL sets us up to avoid actually selecting the range in step 2; instead, we
can make TypingCommand::InsertText() set it as the ending selection, and
InputMethodController can retrieve it from the UndoStep.

Bug: 790801
Change-Id: I9111af255f61c34f5bdaf27b94a5e8a659750860
Reviewed-on: https://chromium-review.googlesource.com/807151Reviewed-by: default avatarXiaocheng Hu <xiaochengh@chromium.org>
Reviewed-by: default avataryosin (OOO Dec 11 to Jan 8) <yosin@chromium.org>
Commit-Queue: Ryan Landay <rlanday@chromium.org>
Cr-Commit-Position: refs/heads/master@{#521984}
parent 99dbd3ea
......@@ -338,14 +338,7 @@ void TypingCommand::AdjustSelectionAfterIncrementalInsertion(
CompositionType() == kTextCompositionUpdate ? selection_start : end;
const SelectionInDOMTree& selection =
CreateSelection(start, end, EndingSelection().IsDirectional(), element);
if (selection == frame->Selection()
.ComputeVisibleSelectionInDOMTreeDeprecated()
.AsSelection())
return;
SetEndingSelection(SelectionForUndoStep::From(selection));
frame->Selection().SetSelection(selection);
}
// FIXME: We shouldn't need to take selectionForInsertion. It should be
......
......@@ -37,6 +37,7 @@
#include "core/editing/SelectionTemplate.h"
#include "core/editing/SetSelectionOptions.h"
#include "core/editing/commands/TypingCommand.h"
#include "core/editing/commands/UndoStack.h"
#include "core/editing/markers/DocumentMarkerController.h"
#include "core/editing/markers/SuggestionMarkerProperties.h"
#include "core/editing/spellcheck/SpellChecker.h"
......@@ -732,15 +733,26 @@ void InputMethodController::SetComposition(
// needs to be audited. see http://crbug.com/590369 for more details.
GetDocument().UpdateStyleAndLayoutIgnorePendingStylesheets();
// The undo stack could become empty if a JavaScript event handler calls
// execCommand('undo') to pop elements off the stack. Or, the top element of
// the stack could end up not corresponding to the TypingCommand. Make sure we
// don't crash in these cases (it's unclear what the composition range should
// be set to in these cases, so we don't worry too much about that).
SelectionInDOMTree selection;
if (GetEditor().GetUndoStack().CanUndo()) {
const UndoStep* undo_step = *GetEditor().GetUndoStack().UndoSteps().begin();
const SelectionForUndoStep& undo_selection = undo_step->EndingSelection();
if (undo_selection.IsValidFor(GetDocument()))
selection = undo_selection.AsSelection();
}
// Find out what node has the composition now.
Position base = MostForwardCaretPosition(
GetFrame().Selection().ComputeVisibleSelectionInDOMTree().Base());
const Position base = MostForwardCaretPosition(selection.Base());
Node* base_node = base.AnchorNode();
if (!base_node || !base_node->IsTextNode())
return;
Position extent =
GetFrame().Selection().ComputeVisibleSelectionInDOMTree().Extent();
const Position extent = selection.Extent();
Node* extent_node = extent.AnchorNode();
unsigned extent_offset = extent.ComputeOffsetInContainerNode();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment