// FIXME: We shouldn't need to take selectionForInsertion. It should be identical to SelectionController's current selection. void TypingCommand::insertText(Document* document, const String& text, const VisibleSelection& selectionForInsertion, bool selectInsertedText, TextCompositionType compositionType) { #if REMOVE_MARKERS_UPON_EDITING if (!text.isEmpty()) document->frame()->editor()->removeSpellAndCorrectionMarkersFromWordsToBeEdited(isSpaceOrNewline(text.characters()[0])); #endif ASSERT(document); RefPtr<Frame> frame = document->frame(); ASSERT(frame); VisibleSelection currentSelection = frame->selection()->selection(); bool changeSelection = currentSelection != selectionForInsertion; String newText = text; Node* startNode = selectionForInsertion.start().node(); if (startNode && startNode->rootEditableElement() && compositionType != TextCompositionUpdate) { // Send BeforeTextInsertedEvent. The event handler will update text if necessary. ExceptionCode ec = 0; RefPtr<BeforeTextInsertedEvent> evt = BeforeTextInsertedEvent::create(text); startNode->rootEditableElement()->dispatchEvent(evt, ec); newText = evt->text(); } if (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 should change EditCommand // to deal with custom selections in a general way that can be used by all of the commands. RefPtr<EditCommand> lastEditCommand = frame->editor()->lastEditCommand(); if (isOpenForMoreTypingCommand(lastEditCommand.get())) { TypingCommand* lastTypingCommand = static_cast<TypingCommand*>(lastEditCommand.get()); if (lastTypingCommand->endingSelection() != selectionForInsertion) { lastTypingCommand->setStartingSelection(selectionForInsertion); lastTypingCommand->setEndingSelection(selectionForInsertion); } lastTypingCommand->setCompositionType(compositionType); lastTypingCommand->insertText(newText, selectInsertedText); return; } RefPtr<TypingCommand> cmd = TypingCommand::create(document, InsertText, newText, selectInsertedText, compositionType); if (changeSelection) { cmd->setStartingSelection(selectionForInsertion); cmd->setEndingSelection(selectionForInsertion); } applyCommand(cmd); if (changeSelection) { cmd->setEndingSelection(currentSelection); frame->selection()->setSelection(currentSelection); } }
// FIXME: We shouldn't need to take selectionForInsertion. It should be identical to SelectionController's current selection. void TypingCommand::insertText(Document* document, const String& text, const VisibleSelection& selectionForInsertion, Options options, TextCompositionType compositionType) { ASSERT(document); RefPtr<Frame> frame = document->frame(); ASSERT(frame); VisibleSelection currentSelection = frame->selection()->selection(); bool changeSelection = currentSelection != selectionForInsertion; String newText = text; Node* startNode = selectionForInsertion.start().deprecatedNode(); if (startNode && startNode->rootEditableElement() && compositionType != TextCompositionUpdate) { // Send BeforeTextInsertedEvent. The event handler will update text if necessary. ExceptionCode ec = 0; RefPtr<BeforeTextInsertedEvent> evt = BeforeTextInsertedEvent::create(text); startNode->rootEditableElement()->dispatchEvent(evt, ec); newText = evt->text(); } if (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 should change EditCommand // to deal with custom selections in a general way that can be used by all of the commands. RefPtr<EditCommand> lastEditCommand = frame->editor()->lastEditCommand(); if (isOpenForMoreTypingCommand(lastEditCommand.get())) { TypingCommand* lastTypingCommand = static_cast<TypingCommand*>(lastEditCommand.get()); if (lastTypingCommand->endingSelection() != selectionForInsertion) { lastTypingCommand->setStartingSelection(selectionForInsertion); lastTypingCommand->setEndingSelection(selectionForInsertion); } lastTypingCommand->setCompositionType(compositionType); lastTypingCommand->setShouldRetainAutocorrectionIndicator(options & RetainAutocorrectionIndicator); lastTypingCommand->setShouldPreventSpellChecking(options & PreventSpellChecking); lastTypingCommand->insertText(newText, options & SelectInsertedText); return; } RefPtr<TypingCommand> cmd = TypingCommand::create(document, InsertText, newText, options, compositionType); if (changeSelection) { cmd->setStartingSelection(selectionForInsertion); cmd->setEndingSelection(selectionForInsertion); } applyCommand(cmd); if (changeSelection) { cmd->setEndingSelection(currentSelection); frame->selection()->setSelection(currentSelection); } }