SplitTextNodeCommand.cpp [plain text]
#include "config.h"
#include "SplitTextNodeCommand.h"
#include "Document.h"
#include "DocumentMarkerController.h"
#include "Text.h"
#include <wtf/Assertions.h>
namespace WebCore {
SplitTextNodeCommand::SplitTextNodeCommand(Ref<Text>&& text, int offset)
: SimpleEditCommand(text->document())
, m_text2(WTFMove(text))
, m_offset(offset)
{
ASSERT(m_text2->length() > 0);
ASSERT(m_offset > 0);
ASSERT(m_offset < m_text2->length());
}
void SplitTextNodeCommand::doApply()
{
ContainerNode* parent = m_text2->parentNode();
if (!parent || !parent->hasEditableStyle())
return;
auto result = m_text2->substringData(0, m_offset);
if (result.hasException())
return;
auto prefixText = result.releaseReturnValue();
if (prefixText.isEmpty())
return;
m_text1 = Text::create(document(), WTFMove(prefixText));
ASSERT(m_text1);
document().markers().copyMarkers(m_text2.ptr(), 0, m_offset, m_text1.get(), 0);
insertText1AndTrimText2();
}
void SplitTextNodeCommand::doUnapply()
{
if (!m_text1 || !m_text1->hasEditableStyle())
return;
ASSERT(&m_text1->document() == &document());
String prefixText = m_text1->data();
m_text2->insertData(0, prefixText);
document().markers().copyMarkers(m_text1.get(), 0, prefixText.length(), m_text2.ptr(), 0);
m_text1->remove();
}
void SplitTextNodeCommand::doReapply()
{
if (!m_text1)
return;
ContainerNode* parent = m_text2->parentNode();
if (!parent || !parent->hasEditableStyle())
return;
insertText1AndTrimText2();
}
void SplitTextNodeCommand::insertText1AndTrimText2()
{
if (m_text2->parentNode()->insertBefore(*m_text1, m_text2.ptr()).hasException())
return;
m_text2->deleteData(0, m_offset);
}
#ifndef NDEBUG
void SplitTextNodeCommand::getNodesInCommand(HashSet<Node*>& nodes)
{
addNodeAndDescendants(m_text1.get(), nodes);
addNodeAndDescendants(m_text2.ptr(), nodes);
}
#endif
}