TextInsertionBaseCommand.cpp [plain text]
#include "config.h"
#include "TextInsertionBaseCommand.h"
#include "BeforeTextInsertedEvent.h"
#include "Document.h"
#include "Element.h"
#include "Frame.h"
#include "FrameSelection.h"
#include "Node.h"
namespace WebCore {
TextInsertionBaseCommand::TextInsertionBaseCommand(Document& document, EditAction editingAction)
: CompositeEditCommand(document, editingAction)
{
}
void TextInsertionBaseCommand::applyTextInsertionCommand(Frame* frame, TextInsertionBaseCommand& command, const VisibleSelection& selectionForInsertion, const VisibleSelection& endingSelection)
{
bool changeSelection = selectionForInsertion != endingSelection;
if (changeSelection) {
command.setStartingSelection(selectionForInsertion);
command.setEndingSelection(selectionForInsertion);
}
applyCommand(&command);
if (changeSelection) {
command.setEndingSelection(endingSelection);
frame->selection().setSelection(endingSelection);
}
}
String dispatchBeforeTextInsertedEvent(const String& text, const VisibleSelection& selectionForInsertion, bool insertionIsForUpdatingComposition)
{
if (insertionIsForUpdatingComposition)
return text;
String newText = text;
if (Node* startNode = selectionForInsertion.start().containerNode()) {
if (startNode->rootEditableElement()) {
Ref<BeforeTextInsertedEvent> event = BeforeTextInsertedEvent::create(text);
startNode->rootEditableElement()->dispatchEvent(event);
newText = event->text();
}
}
return newText;
}
bool canAppendNewLineFeedToSelection(const VisibleSelection& selection)
{
Node* node = selection.rootEditableElement();
if (!node)
return false;
Ref<BeforeTextInsertedEvent> event = BeforeTextInsertedEvent::create(String("\n"));
node->dispatchEvent(event);
return event->text().length();
}
}