ReplaceRangeWithTextCommand.cpp [plain text]
#include "config.h"
#include "ReplaceRangeWithTextCommand.h"
#include "AlternativeTextController.h"
#include "DataTransfer.h"
#include "Document.h"
#include "DocumentFragment.h"
#include "Editor.h"
#include "Frame.h"
#include "ReplaceSelectionCommand.h"
#include "SetSelectionCommand.h"
#include "StaticRange.h"
#include "TextIterator.h"
#include "markup.h"
namespace WebCore {
ReplaceRangeWithTextCommand::ReplaceRangeWithTextCommand(RefPtr<Range> rangeToBeReplaced, const String& text)
: CompositeEditCommand(rangeToBeReplaced->startContainer().document(), EditActionInsertReplacement)
, m_rangeToBeReplaced(rangeToBeReplaced)
, m_text(text)
{
}
bool ReplaceRangeWithTextCommand::willApplyCommand()
{
m_textFragment = createFragmentFromText(*m_rangeToBeReplaced, m_text);
return CompositeEditCommand::willApplyCommand();
}
void ReplaceRangeWithTextCommand::doApply()
{
VisibleSelection selection = *m_rangeToBeReplaced;
if (!m_rangeToBeReplaced)
return;
if (!frame().selection().shouldChangeSelection(selection))
return;
String previousText = plainText(m_rangeToBeReplaced.get());
if (!previousText.length())
return;
applyCommandToComposite(SetSelectionCommand::create(selection, FrameSelection::defaultSetSelectionOptions()));
applyCommandToComposite(ReplaceSelectionCommand::create(document(), WTFMove(m_textFragment), ReplaceSelectionCommand::MatchStyle, EditActionPaste));
}
String ReplaceRangeWithTextCommand::inputEventData() const
{
if (isEditingTextAreaOrTextInput())
return m_text;
return CompositeEditCommand::inputEventData();
}
RefPtr<DataTransfer> ReplaceRangeWithTextCommand::inputEventDataTransfer() const
{
if (!isEditingTextAreaOrTextInput())
return DataTransfer::createForInputEvent(m_text, createMarkup(*m_textFragment));
return CompositeEditCommand::inputEventDataTransfer();
}
Vector<RefPtr<StaticRange>> ReplaceRangeWithTextCommand::targetRanges() const
{
RefPtr<StaticRange> range = StaticRange::createFromRange(*m_rangeToBeReplaced);
return { 1, range };
}
}