InsertTextCommand.cpp [plain text]
#include "config.h"
#include "InsertTextCommand.h"
#include "Document.h"
#include "Element.h"
#include "Editor.h"
#include "Frame.h"
#include "HTMLInterchange.h"
#include "Text.h"
#include "VisibleUnits.h"
#include "htmlediting.h"
namespace WebCore {
InsertTextCommand::InsertTextCommand(Document& document, const String& text, bool selectInsertedText, RebalanceType rebalanceType, EditAction editingAction)
: CompositeEditCommand(document, editingAction)
, m_text(text)
, m_selectInsertedText(selectInsertedText)
, m_rebalanceType(rebalanceType)
{
}
InsertTextCommand::InsertTextCommand(Document& document, const String& text, PassRefPtr<TextInsertionMarkerSupplier> markerSupplier, EditAction editingAction)
: CompositeEditCommand(document, editingAction)
, m_text(text)
, m_selectInsertedText(false)
, m_rebalanceType(RebalanceLeadingAndTrailingWhitespaces)
, m_markerSupplier(markerSupplier)
{
}
Position InsertTextCommand::positionInsideTextNode(const Position& p)
{
Position pos = p;
if (isTabSpanTextNode(pos.anchorNode())) {
RefPtr<Node> textNode = document().createEditingTextNode("");
insertNodeAtTabSpanPosition(textNode.get(), pos);
return firstPositionInNode(textNode.get());
}
if (!pos.containerNode()->isTextNode()) {
RefPtr<Node> textNode = document().createEditingTextNode("");
insertNodeAt(textNode.get(), pos);
return firstPositionInNode(textNode.get());
}
return pos;
}
void InsertTextCommand::setEndingSelectionWithoutValidation(const Position& startPosition, const Position& endPosition)
{
VisibleSelection forcedEndingSelection;
forcedEndingSelection.setWithoutValidation(startPosition, endPosition);
forcedEndingSelection.setIsDirectional(endingSelection().isDirectional());
setEndingSelection(forcedEndingSelection);
}
bool InsertTextCommand::performTrivialReplace(const String& text, bool selectInsertedText)
{
if (!endingSelection().isRange())
return false;
if (text.contains('\t') || text.contains(' ') || text.contains('\n'))
return false;
Position start = endingSelection().start();
Position endPosition = replaceSelectedTextInNode(text);
if (endPosition.isNull())
return false;
setEndingSelectionWithoutValidation(start, endPosition);
if (!selectInsertedText)
setEndingSelection(VisibleSelection(endingSelection().visibleEnd(), endingSelection().isDirectional()));
return true;
}
bool InsertTextCommand::performOverwrite(const String& text, bool selectInsertedText)
{
Position start = endingSelection().start();
RefPtr<Text> textNode = start.containerText();
if (!textNode)
return false;
unsigned count = std::min(text.length(), textNode->length() - start.offsetInContainerNode());
if (!count)
return false;
replaceTextInNode(textNode, start.offsetInContainerNode(), count, text);
Position endPosition = Position(textNode.release(), start.offsetInContainerNode() + text.length());
setEndingSelectionWithoutValidation(start, endPosition);
if (!selectInsertedText)
setEndingSelection(VisibleSelection(endingSelection().visibleEnd(), endingSelection().isDirectional()));
return true;
}
void InsertTextCommand::doApply()
{
ASSERT(m_text.find('\n') == notFound);
if (!endingSelection().isNonOrphanedCaretOrRange())
return;
if (endingSelection().isRange()) {
if (performTrivialReplace(m_text, m_selectInsertedText))
return;
deleteSelection(false, true, true, false, false);
if (endingSelection().isNone())
return;
} else if (frame().editor().isOverwriteModeEnabled()) {
if (performOverwrite(m_text, m_selectInsertedText))
return;
}
Position startPosition(endingSelection().start());
Position placeholder;
Position downstream(startPosition.downstream());
if (lineBreakExistsAtPosition(downstream)) {
VisiblePosition caret(startPosition);
if (isEndOfBlock(caret) && isStartOfParagraph(caret))
placeholder = downstream;
}
startPosition = startPosition.upstream();
Position positionBeforeStartNode(positionInParentBeforeNode(startPosition.containerNode()));
deleteInsignificantText(startPosition.upstream(), startPosition.downstream());
if (!startPosition.anchorNode()->inDocument())
startPosition = positionBeforeStartNode;
if (!startPosition.isCandidate())
startPosition = startPosition.downstream();
startPosition = positionAvoidingSpecialElementBoundary(startPosition);
Position endPosition;
if (m_text == "\t") {
endPosition = insertTab(startPosition);
startPosition = endPosition.previous();
if (placeholder.isNotNull())
removePlaceholderAt(placeholder);
} else {
startPosition = positionInsideTextNode(startPosition);
ASSERT(startPosition.anchorType() == Position::PositionIsOffsetInAnchor);
ASSERT(startPosition.containerNode());
ASSERT(startPosition.containerNode()->isTextNode());
if (placeholder.isNotNull())
removePlaceholderAt(placeholder);
RefPtr<Text> textNode = startPosition.containerText();
const unsigned offset = startPosition.offsetInContainerNode();
insertTextIntoNode(textNode, offset, m_text);
endPosition = Position(textNode, offset + m_text.length());
if (m_markerSupplier)
m_markerSupplier->addMarkersToTextNode(textNode.get(), offset, m_text);
if (m_rebalanceType == RebalanceLeadingAndTrailingWhitespaces) {
rebalanceWhitespaceAt(endPosition);
if (!shouldRebalanceLeadingWhitespaceFor(m_text))
rebalanceWhitespaceAt(startPosition);
} else {
ASSERT(m_rebalanceType == RebalanceAllWhitespaces);
if (canRebalance(startPosition) && canRebalance(endPosition))
rebalanceWhitespaceOnTextSubstring(textNode, startPosition.offsetInContainerNode(), endPosition.offsetInContainerNode());
}
}
setEndingSelectionWithoutValidation(startPosition, endPosition);
if (RefPtr<EditingStyle> typingStyle = frame().selection().typingStyle()) {
typingStyle->prepareToApplyAt(endPosition, EditingStyle::PreserveWritingDirection);
if (!typingStyle->isEmpty())
applyStyle(typingStyle.get());
}
if (!m_selectInsertedText)
setEndingSelection(VisibleSelection(endingSelection().end(), endingSelection().affinity(), endingSelection().isDirectional()));
}
Position InsertTextCommand::insertTab(const Position& pos)
{
Position insertPos = VisiblePosition(pos, DOWNSTREAM).deepEquivalent();
Node* node = insertPos.containerNode();
unsigned int offset = node->isTextNode() ? insertPos.offsetInContainerNode() : 0;
if (isTabSpanTextNode(node)) {
RefPtr<Text> textNode = downcast<Text>(node);
insertTextIntoNode(textNode, offset, "\t");
return Position(textNode.release(), offset + 1);
}
RefPtr<Element> spanNode = createTabSpanElement(document());
if (!is<Text>(*node)) {
insertNodeAt(spanNode.get(), insertPos);
} else {
RefPtr<Text> textNode = downcast<Text>(node);
if (offset >= textNode->length())
insertNodeAfter(spanNode, textNode.release());
else {
if (offset > 0)
splitTextNode(textNode, offset);
insertNodeBefore(spanNode, textNode.release());
}
}
return lastPositionInNode(spanNode.get());
}
}