#include "config.h"
#include "TypingCommand.h"
#include "BeforeTextInsertedEvent.h"
#include "BreakBlockquoteCommand.h"
#include "DeleteSelectionCommand.h"
#include "Document.h"
#include "Editor.h"
#include "Element.h"
#include "Frame.h"
#include "InsertLineBreakCommand.h"
#include "InsertParagraphSeparatorCommand.h"
#include "InsertTextCommand.h"
#include "SelectionController.h"
#include "VisiblePosition.h"
#include "htmlediting.h"
#include "visible_units.h"
namespace WebCore {
TypingCommand::TypingCommand(Document *document, ETypingCommand commandType, const String &textToInsert, bool selectInsertedText, TextGranularity granularity, bool killRing)
: CompositeEditCommand(document),
m_commandType(commandType),
m_textToInsert(textToInsert),
m_openForMoreTyping(true),
m_selectInsertedText(selectInsertedText),
m_smartDelete(false),
m_granularity(granularity),
m_killRing(killRing),
m_openedByBackwardDelete(false)
{
}
void TypingCommand::deleteSelection(Document* document, bool smartDelete)
{
ASSERT(document);
Frame* frame = document->frame();
ASSERT(frame);
if (!frame->selection()->isRange())
return;
EditCommand* lastEditCommand = frame->editor()->lastEditCommand();
if (isOpenForMoreTypingCommand(lastEditCommand)) {
static_cast<TypingCommand*>(lastEditCommand)->deleteSelection(smartDelete);
return;
}
RefPtr<TypingCommand> typingCommand = TypingCommand::create(document, DeleteSelection, "", false);
typingCommand->setSmartDelete(smartDelete);
typingCommand->apply();
}
void TypingCommand::deleteKeyPressed(Document *document, bool smartDelete, TextGranularity granularity, bool killRing)
{
ASSERT(document);
Frame *frame = document->frame();
ASSERT(frame);
EditCommand* lastEditCommand = frame->editor()->lastEditCommand();
if (isOpenForMoreTypingCommand(lastEditCommand)) {
static_cast<TypingCommand*>(lastEditCommand)->deleteKeyPressed(granularity, killRing);
return;
}
RefPtr<TypingCommand> typingCommand = TypingCommand::create(document, DeleteKey, "", false, granularity, killRing);
typingCommand->setSmartDelete(smartDelete);
typingCommand->apply();
}
void TypingCommand::forwardDeleteKeyPressed(Document *document, bool smartDelete, TextGranularity granularity, bool killRing)
{
ASSERT(document);
Frame *frame = document->frame();
ASSERT(frame);
EditCommand* lastEditCommand = frame->editor()->lastEditCommand();
if (isOpenForMoreTypingCommand(lastEditCommand)) {
static_cast<TypingCommand*>(lastEditCommand)->forwardDeleteKeyPressed(granularity, killRing);
return;
}
RefPtr<TypingCommand> typingCommand = TypingCommand::create(document, ForwardDeleteKey, "", false, granularity, killRing);
typingCommand->setSmartDelete(smartDelete);
typingCommand->apply();
}
void TypingCommand::insertText(Document* document, const String& text, bool selectInsertedText, bool insertedTextIsComposition)
{
ASSERT(document);
Frame* frame = document->frame();
ASSERT(frame);
insertText(document, text, frame->selection()->selection(), selectInsertedText, insertedTextIsComposition);
}
void TypingCommand::insertText(Document* document, const String& text, const Selection& selectionForInsertion, bool selectInsertedText, bool insertedTextIsComposition)
{
ASSERT(document);
RefPtr<Frame> frame = document->frame();
ASSERT(frame);
Selection currentSelection = frame->selection()->selection();
bool changeSelection = currentSelection != selectionForInsertion;
String newText = text;
Node* startNode = selectionForInsertion.start().node();
if (startNode && startNode->rootEditableElement() && !insertedTextIsComposition) {
ExceptionCode ec = 0;
RefPtr<BeforeTextInsertedEvent> evt = BeforeTextInsertedEvent::create(text);
startNode->rootEditableElement()->dispatchEvent(evt, ec);
newText = evt->text();
}
if (newText.isEmpty())
return;
RefPtr<EditCommand> lastEditCommand = frame->editor()->lastEditCommand();
if (isOpenForMoreTypingCommand(lastEditCommand.get())) {
TypingCommand* lastTypingCommand = static_cast<TypingCommand*>(lastEditCommand.get());
if (changeSelection) {
lastTypingCommand->setStartingSelection(selectionForInsertion);
lastTypingCommand->setEndingSelection(selectionForInsertion);
}
lastTypingCommand->insertText(newText, selectInsertedText);
if (changeSelection) {
lastTypingCommand->setEndingSelection(currentSelection);
frame->selection()->setSelection(currentSelection);
}
return;
}
RefPtr<TypingCommand> cmd = TypingCommand::create(document, InsertText, newText, selectInsertedText);
if (changeSelection) {
cmd->setStartingSelection(selectionForInsertion);
cmd->setEndingSelection(selectionForInsertion);
}
applyCommand(cmd);
if (changeSelection) {
cmd->setEndingSelection(currentSelection);
frame->selection()->setSelection(currentSelection);
}
}
void TypingCommand::insertLineBreak(Document *document)
{
ASSERT(document);
Frame *frame = document->frame();
ASSERT(frame);
EditCommand* lastEditCommand = frame->editor()->lastEditCommand();
if (isOpenForMoreTypingCommand(lastEditCommand)) {
static_cast<TypingCommand*>(lastEditCommand)->insertLineBreak();
return;
}
applyCommand(TypingCommand::create(document, InsertLineBreak));
}
void TypingCommand::insertParagraphSeparatorInQuotedContent(Document *document)
{
ASSERT(document);
Frame *frame = document->frame();
ASSERT(frame);
EditCommand* lastEditCommand = frame->editor()->lastEditCommand();
if (isOpenForMoreTypingCommand(lastEditCommand)) {
static_cast<TypingCommand*>(lastEditCommand)->insertParagraphSeparatorInQuotedContent();
return;
}
applyCommand(TypingCommand::create(document, InsertParagraphSeparatorInQuotedContent));
}
void TypingCommand::insertParagraphSeparator(Document *document)
{
ASSERT(document);
Frame *frame = document->frame();
ASSERT(frame);
EditCommand* lastEditCommand = frame->editor()->lastEditCommand();
if (isOpenForMoreTypingCommand(lastEditCommand)) {
static_cast<TypingCommand*>(lastEditCommand)->insertParagraphSeparator();
return;
}
applyCommand(TypingCommand::create(document, InsertParagraphSeparator));
}
bool TypingCommand::isOpenForMoreTypingCommand(const EditCommand* cmd)
{
return cmd && cmd->isTypingCommand() && static_cast<const TypingCommand*>(cmd)->isOpenForMoreTyping();
}
void TypingCommand::closeTyping(EditCommand* cmd)
{
if (isOpenForMoreTypingCommand(cmd))
static_cast<TypingCommand*>(cmd)->closeTyping();
}
void TypingCommand::doApply()
{
if (endingSelection().isNone())
return;
if (m_commandType == DeleteKey)
if (m_commands.isEmpty())
m_openedByBackwardDelete = true;
switch (m_commandType) {
case DeleteSelection:
deleteSelection(m_smartDelete);
return;
case DeleteKey:
deleteKeyPressed(m_granularity, m_killRing);
return;
case ForwardDeleteKey:
forwardDeleteKeyPressed(m_granularity, m_killRing);
return;
case InsertLineBreak:
insertLineBreak();
return;
case InsertParagraphSeparator:
insertParagraphSeparator();
return;
case InsertParagraphSeparatorInQuotedContent:
insertParagraphSeparatorInQuotedContent();
return;
case InsertText:
insertText(m_textToInsert, m_selectInsertedText);
return;
}
ASSERT_NOT_REACHED();
}
EditAction TypingCommand::editingAction() const
{
return EditActionTyping;
}
void TypingCommand::markMisspellingsAfterTyping()
{
}
void TypingCommand::typingAddedToOpenCommand()
{
markMisspellingsAfterTyping();
document()->frame()->editor()->appliedEditing(this);
}
void TypingCommand::insertText(const String &text, bool selectInsertedText)
{
int offset = 0;
int newline;
while ((newline = text.find('\n', offset)) != -1) {
if (newline != offset)
insertTextRunWithoutNewlines(text.substring(offset, newline - offset), false);
insertParagraphSeparator();
offset = newline + 1;
}
if (offset == 0)
insertTextRunWithoutNewlines(text, selectInsertedText);
else {
int length = text.length();
if (length != offset) {
insertTextRunWithoutNewlines(text.substring(offset, length - offset), selectInsertedText);
}
}
}
void TypingCommand::insertTextRunWithoutNewlines(const String &text, bool selectInsertedText)
{
RefPtr<InsertTextCommand> command;
if (!document()->frame()->typingStyle() && !m_commands.isEmpty()) {
EditCommand* lastCommand = m_commands.last().get();
if (lastCommand->isInsertTextCommand())
command = static_cast<InsertTextCommand*>(lastCommand);
}
if (!command) {
command = InsertTextCommand::create(document());
applyCommandToComposite(command);
}
command->input(text, selectInsertedText);
typingAddedToOpenCommand();
}
void TypingCommand::insertLineBreak()
{
applyCommandToComposite(InsertLineBreakCommand::create(document()));
typingAddedToOpenCommand();
}
void TypingCommand::insertParagraphSeparator()
{
applyCommandToComposite(InsertParagraphSeparatorCommand::create(document()));
typingAddedToOpenCommand();
}
void TypingCommand::insertParagraphSeparatorInQuotedContent()
{
applyCommandToComposite(BreakBlockquoteCommand::create(document()));
typingAddedToOpenCommand();
}
void TypingCommand::deleteKeyPressed(TextGranularity granularity, bool killRing)
{
Selection selectionToDelete;
Selection selectionAfterUndo;
switch (endingSelection().state()) {
case Selection::RANGE:
selectionToDelete = endingSelection();
selectionAfterUndo = selectionToDelete;
break;
case Selection::CARET: {
if (breakOutOfEmptyMailBlockquotedParagraph()) {
typingAddedToOpenCommand();
return;
}
m_smartDelete = false;
SelectionController selection;
selection.setSelection(endingSelection());
selection.modify(SelectionController::EXTEND, SelectionController::BACKWARD, granularity);
if (killRing && selection.isCaret() && granularity != CharacterGranularity)
selection.modify(SelectionController::EXTEND, SelectionController::BACKWARD, CharacterGranularity);
if (endingSelection().visibleStart().previous(true).isNull()) {
if (breakOutOfEmptyListItem()) {
typingAddedToOpenCommand();
return;
}
}
VisiblePosition visibleStart(endingSelection().visibleStart());
if (isStartOfParagraph(visibleStart) && isFirstPositionAfterTable(visibleStart.previous(true))) {
if (isLastPositionBeforeTable(visibleStart))
return;
selection.modify(SelectionController::EXTEND, SelectionController::BACKWARD, granularity);
} else if (Node* table = isFirstPositionAfterTable(visibleStart)) {
setEndingSelection(Selection(Position(table, 0), endingSelection().start(), DOWNSTREAM));
typingAddedToOpenCommand();
return;
}
selectionToDelete = selection.selection();
if (granularity == CharacterGranularity && selectionToDelete.end().offset() - selectionToDelete.start().offset() > 1) {
if (selectionToDelete.end().offset() - selectionToDelete.end().previous(UsingComposedCharacters).offset() > 1)
selectionToDelete.setWithoutValidation(selectionToDelete.end(), selectionToDelete.end().previous(NotUsingComposedCharacters));
}
if (!startingSelection().isRange() || selectionToDelete.base() != startingSelection().start())
selectionAfterUndo = selectionToDelete;
else
selectionAfterUndo.setWithoutValidation(startingSelection().end(), selectionToDelete.extent());
break;
}
case Selection::NONE:
ASSERT_NOT_REACHED();
break;
}
if (selectionToDelete.isNone()) {
setEndingSelection(document()->frame()->selection()->selection());
closeTyping(this);
return;
}
if (selectionToDelete.isCaret() || !document()->frame()->shouldDeleteSelection(selectionToDelete))
return;
if (killRing)
document()->frame()->editor()->addToKillRing(selectionToDelete.toRange().get(), false);
if (m_openedByBackwardDelete)
setStartingSelection(selectionAfterUndo);
CompositeEditCommand::deleteSelection(selectionToDelete, m_smartDelete);
setSmartDelete(false);
typingAddedToOpenCommand();
}
void TypingCommand::forwardDeleteKeyPressed(TextGranularity granularity, bool killRing)
{
Selection selectionToDelete;
Selection selectionAfterUndo;
switch (endingSelection().state()) {
case Selection::RANGE:
selectionToDelete = endingSelection();
selectionAfterUndo = selectionToDelete;
break;
case Selection::CARET: {
m_smartDelete = false;
SelectionController selection;
selection.setSelection(endingSelection());
selection.modify(SelectionController::EXTEND, SelectionController::FORWARD, granularity);
if (killRing && selection.isCaret() && granularity != CharacterGranularity)
selection.modify(SelectionController::EXTEND, SelectionController::FORWARD, CharacterGranularity);
Position downstreamEnd = endingSelection().end().downstream();
VisiblePosition visibleEnd = endingSelection().visibleEnd();
if (visibleEnd == endOfParagraph(visibleEnd))
downstreamEnd = visibleEnd.next(true).deepEquivalent().downstream();
if (downstreamEnd.node() && downstreamEnd.node()->renderer() && downstreamEnd.node()->renderer()->isTable() && downstreamEnd.offset() == 0) {
setEndingSelection(Selection(endingSelection().end(), Position(downstreamEnd.node(), maxDeepOffset(downstreamEnd.node())), DOWNSTREAM));
typingAddedToOpenCommand();
return;
}
if (granularity == ParagraphBoundary && selection.selection().isCaret() && isEndOfParagraph(selection.selection().visibleEnd()))
selection.modify(SelectionController::EXTEND, SelectionController::FORWARD, CharacterGranularity);
selectionToDelete = selection.selection();
if (!startingSelection().isRange() || selectionToDelete.base() != startingSelection().start())
selectionAfterUndo = selectionToDelete;
else {
Position extent = startingSelection().end();
if (extent.node() != selectionToDelete.end().node())
extent = selectionToDelete.extent();
else {
int extraCharacters;
if (selectionToDelete.start().node() == selectionToDelete.end().node())
extraCharacters = selectionToDelete.end().offset() - selectionToDelete.start().offset();
else
extraCharacters = selectionToDelete.end().offset();
extent = Position(extent.node(), extent.offset() + extraCharacters);
}
selectionAfterUndo.setWithoutValidation(startingSelection().start(), extent);
}
break;
}
case Selection::NONE:
ASSERT_NOT_REACHED();
break;
}
if (selectionToDelete.isNone()) {
setEndingSelection(document()->frame()->selection()->selection());
closeTyping(this);
return;
}
if (selectionToDelete.isCaret() || !document()->frame()->shouldDeleteSelection(selectionToDelete))
return;
if (killRing)
document()->frame()->editor()->addToKillRing(selectionToDelete.toRange().get(), false);
setStartingSelection(selectionAfterUndo);
CompositeEditCommand::deleteSelection(selectionToDelete, m_smartDelete);
setSmartDelete(false);
typingAddedToOpenCommand();
}
void TypingCommand::deleteSelection(bool smartDelete)
{
CompositeEditCommand::deleteSelection(smartDelete);
typingAddedToOpenCommand();
}
void TypingCommand::setEndingSelectionOnLastInsertCommand(const Selection& selection)
{
if (!m_commands.isEmpty()) {
EditCommand* lastCommand = m_commands.last().get();
if (lastCommand->isInsertTextCommand())
lastCommand->setEndingSelection(selection);
}
}
bool TypingCommand::preservesTypingStyle() const
{
switch (m_commandType) {
case DeleteSelection:
case DeleteKey:
case ForwardDeleteKey:
case InsertParagraphSeparator:
case InsertLineBreak:
return true;
case InsertParagraphSeparatorInQuotedContent:
case InsertText:
return false;
}
ASSERT_NOT_REACHED();
return false;
}
bool TypingCommand::isTypingCommand() const
{
return true;
}
}