This source file includes following definitions.
- m_rebalanceType
- positionInsideTextNode
- setEndingSelectionWithoutValidation
- performTrivialReplace
- performOverwrite
- doApply
- insertTab
#include "config.h"
#include "core/editing/InsertTextCommand.h"
#include "core/dom/Document.h"
#include "core/dom/Element.h"
#include "core/dom/Text.h"
#include "core/editing/Editor.h"
#include "core/editing/VisibleUnits.h"
#include "core/editing/htmlediting.h"
#include "core/frame/LocalFrame.h"
namespace WebCore {
InsertTextCommand::InsertTextCommand(Document& document, const String& text, bool selectInsertedText, RebalanceType rebalanceType)
: CompositeEditCommand(document)
, m_text(text)
, m_selectInsertedText(selectInsertedText)
, m_rebalanceType(rebalanceType)
{
}
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') == kNotFound);
if (!endingSelection().isNonOrphanedCaretOrRange())
return;
if (endingSelection().isRange()) {
if (performTrivialReplace(m_text, m_selectInsertedText))
return;
bool endOfSelectionWasAtStartOfBlock = isStartOfBlock(endingSelection().visibleEnd());
deleteSelection(false, true, false, false);
if (endingSelection().isNone())
return;
if (endOfSelectionWasAtStartOfBlock) {
if (EditingStyle* typingStyle = document().frame()->selection().typingStyle())
typingStyle->removeBlockProperties();
}
} else if (document().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();
ASSERT(startPosition.containerNode());
Position positionBeforeStartNode(positionInParentBeforeNode(*startPosition.containerNode()));
deleteInsignificantText(startPosition, startPosition.downstream());
if (!startPosition.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_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 = document().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 offset = node->isTextNode() ? insertPos.offsetInContainerNode() : 0;
if (isTabSpanTextNode(node)) {
RefPtr<Text> textNode = toText(node);
insertTextIntoNode(textNode, offset, "\t");
return Position(textNode.release(), offset + 1);
}
RefPtr<Element> spanNode = createTabSpanElement(document());
if (!node->isTextNode()) {
insertNodeAt(spanNode.get(), insertPos);
} else {
RefPtr<Text> textNode = toText(node);
if (offset >= textNode->length())
insertNodeAfter(spanNode, textNode.release());
else {
if (offset > 0)
splitTextNode(textNode, offset);
insertNodeBefore(spanNode, textNode.release());
}
}
return lastPositionInNode(spanNode.get());
}
}