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(PassRefPtr<Text> text, int offset)
: SimpleEditCommand(text->document())
, m_text2(text)
, m_offset(offset)
{
ASSERT(m_text2);
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;
String prefixText = m_text2->substringData(0, m_offset, IGNORE_EXCEPTION);
if (prefixText.isEmpty())
return;
m_text1 = Text::create(document(), prefixText);
ASSERT(m_text1);
document().markers().copyMarkers(m_text2.get(), 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, ASSERT_NO_EXCEPTION);
document().markers().copyMarkers(m_text1.get(), 0, prefixText.length(), m_text2.get(), 0);
m_text1->remove(ASSERT_NO_EXCEPTION);
}
void SplitTextNodeCommand::doReapply()
{
if (!m_text1 || !m_text2)
return;
ContainerNode* parent = m_text2->parentNode();
if (!parent || !parent->hasEditableStyle())
return;
insertText1AndTrimText2();
}
void SplitTextNodeCommand::insertText1AndTrimText2()
{
ExceptionCode ec = 0;
m_text2->parentNode()->insertBefore(m_text1.get(), m_text2.get(), ec);
if (ec)
return;
m_text2->deleteData(0, m_offset, ec);
}
#ifndef NDEBUG
void SplitTextNodeCommand::getNodesInCommand(HashSet<Node*>& nodes)
{
addNodeAndDescendants(m_text1.get(), nodes);
addNodeAndDescendants(m_text2.get(), nodes);
}
#endif
}