Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(133)

Unified Diff: third_party/WebKit/Source/core/editing/commands/TypingCommand.cpp

Issue 2530843003: Introduce InsertIncrementalTextCommand to respect existing style for composition (Closed)
Patch Set: A little change Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: third_party/WebKit/Source/core/editing/commands/TypingCommand.cpp
diff --git a/third_party/WebKit/Source/core/editing/commands/TypingCommand.cpp b/third_party/WebKit/Source/core/editing/commands/TypingCommand.cpp
index 68aee201cbbe8b3e2226b5d4b3e667d7bc59ff60..15b0e695e1f70f8a7c76d3f6e8f57b3ea836f695 100644
--- a/third_party/WebKit/Source/core/editing/commands/TypingCommand.cpp
+++ b/third_party/WebKit/Source/core/editing/commands/TypingCommand.cpp
@@ -31,15 +31,18 @@
#include "core/dom/ElementTraversal.h"
#include "core/editing/EditingUtilities.h"
#include "core/editing/Editor.h"
+#include "core/editing/PlainTextRange.h"
#include "core/editing/SelectionModifier.h"
#include "core/editing/VisiblePosition.h"
#include "core/editing/VisibleUnits.h"
#include "core/editing/commands/BreakBlockquoteCommand.h"
+#include "core/editing/commands/InsertIncrementalTextCommand.h"
#include "core/editing/commands/InsertLineBreakCommand.h"
#include "core/editing/commands/InsertParagraphSeparatorCommand.h"
#include "core/editing/commands/InsertTextCommand.h"
#include "core/editing/spellcheck/SpellChecker.h"
#include "core/events/BeforeTextInsertedEvent.h"
+#include "core/events/TextEvent.h"
#include "core/frame/LocalFrame.h"
#include "core/html/HTMLBRElement.h"
#include "core/layout/LayoutObject.h"
@@ -47,16 +50,17 @@
namespace blink {
using namespace HTMLNames;
+bool TypingCommand::m_isIncrementalInsertion = false;
TypingCommand::TypingCommand(Document& document,
ETypingCommand commandType,
- const String& textToInsert,
+ const String& text,
Options options,
TextGranularity granularity,
TextCompositionType compositionType)
: CompositeEditCommand(document),
m_commandType(commandType),
- m_textToInsert(textToInsert),
+ m_text(text),
m_openForMoreTyping(true),
m_selectInsertedText(options & SelectInsertedText),
m_smartDelete(options & SmartDelete),
@@ -143,8 +147,8 @@ void TypingCommand::forwardDeleteKeyPressed(Document& document,
}
String TypingCommand::textDataForInputEvent() const {
- if (m_commands.isEmpty())
- return m_textToInsert;
+ if (m_commands.isEmpty() || isIncrementalInsertion())
+ return m_text;
return m_commands.back()->textDataForInputEvent();
}
@@ -162,11 +166,7 @@ void TypingCommand::updateSelectionIfDifferentFromCurrentSelection(
static String dispatchBeforeTextInsertedEvent(
const String& text,
- const VisibleSelection& selectionForInsertion,
- bool insertionIsForUpdatingComposition) {
- if (insertionIsForUpdatingComposition)
- return text;
-
+ const VisibleSelection& selectionForInsertion) {
String newText = text;
if (Node* startNode = selectionForInsertion.start().computeContainerNode()) {
if (rootEditableElement(*startNode)) {
@@ -180,10 +180,24 @@ static String dispatchBeforeTextInsertedEvent(
return newText;
}
+static DispatchEventResult dispatchTextInputEvent(LocalFrame* frame,
+ const String& text) {
+ if (Element* target = frame->document()->focusedElement()) {
+ // Send TextInputEvent. Unlike BeforeTextInsertedEvent, there is no need to
+ // update text for TextInputEvent as it doesn't have the API to modify text.
+ TextEvent* event = TextEvent::create(frame->domWindow(), text,
+ TextEventInputIncrementalInsertion);
+ event->setUnderlyingEvent(nullptr);
+ return target->dispatchEvent(event);
+ }
+ return DispatchEventResult::CanceledBeforeDispatch;
+}
+
void TypingCommand::insertText(Document& document,
const String& text,
Options options,
- TextCompositionType composition) {
+ TextCompositionType composition,
+ bool isIncrementalInsertion) {
LocalFrame* frame = document.frame();
DCHECK(frame);
@@ -191,6 +205,7 @@ void TypingCommand::insertText(Document& document,
document.frame()->spellChecker().updateMarkersForWordsAffectedByEditing(
isSpaceOrNewline(text[0]));
+ m_isIncrementalInsertion = isIncrementalInsertion;
insertText(document, text, frame->selection().selection(), options,
composition);
}
@@ -207,8 +222,23 @@ void TypingCommand::insertText(Document& document,
VisibleSelection currentSelection = frame->selection().selection();
- String newText = dispatchBeforeTextInsertedEvent(
- text, selectionForInsertion, compositionType == TextCompositionUpdate);
+ String newText = text;
+ if (compositionType != TextCompositionUpdate)
+ newText = dispatchBeforeTextInsertedEvent(text, selectionForInsertion);
+
+ if (compositionType == TextCompositionConfirm) {
+ if (dispatchTextInputEvent(frame, newText) !=
+ DispatchEventResult::NotCanceled)
+ return;
+ }
+
+ // TODO(xiaochengh): The use of updateStyleAndLayoutIgnorePendingStylesheets
+ // needs to be audited. see http://crbug.com/590369 for more details.
+ document.updateStyleAndLayoutIgnorePendingStylesheets();
+
+ // Do nothing if no need to delete and insert.
+ if (selectionForInsertion.isCaret() && newText.isEmpty())
+ return;
// Set the starting and ending selection appropriately if we are using a
// selection that is different from the current selection. In the future, we
@@ -333,7 +363,7 @@ void TypingCommand::doApply(EditingState* editingState) {
insertParagraphSeparatorInQuotedContent(editingState);
return;
case InsertText:
- insertText(m_textToInsert, m_selectInsertedText, editingState);
+ insertText(m_text, m_selectInsertedText, editingState);
return;
}
@@ -419,23 +449,36 @@ void TypingCommand::insertText(const String& text,
return;
}
- if (text.length() > offset)
+ if (text.length() > offset) {
insertTextRunWithoutNewlines(text.substring(offset, text.length() - offset),
selectInsertedText, editingState);
+ }
}
void TypingCommand::insertTextRunWithoutNewlines(const String& text,
bool selectInsertedText,
EditingState* editingState) {
- InsertTextCommand* command = InsertTextCommand::create(
- document(), text, selectInsertedText,
- m_compositionType == TextCompositionNone
- ? InsertTextCommand::RebalanceLeadingAndTrailingWhitespaces
- : InsertTextCommand::RebalanceAllWhitespaces);
+ CompositeEditCommand* command;
+ if (isIncrementalInsertion()) {
+ command = InsertIncrementalTextCommand::create(
+ document(), text, selectInsertedText,
+ m_compositionType == TextCompositionNone
+ ? InsertIncrementalTextCommand::
+ RebalanceLeadingAndTrailingWhitespaces
+ : InsertIncrementalTextCommand::RebalanceAllWhitespaces);
+ } else {
+ command = InsertTextCommand::create(
+ document(), text, selectInsertedText,
+ m_compositionType == TextCompositionNone
+ ? InsertTextCommand::RebalanceLeadingAndTrailingWhitespaces
+ : InsertTextCommand::RebalanceAllWhitespaces);
+ }
applyCommandToComposite(command, endingSelection(), editingState);
if (editingState->isAborted())
return;
+
+ m_text = text;
typingAddedToOpenCommand(InsertText);
}

Powered by Google App Engine
This is Rietveld 408576698