DeleteSelectionCommand.cpp [plain text]
#include "config.h"
#include "DeleteSelectionCommand.h"
#include "Document.h"
#include "DocumentFragment.h"
#include "Element.h"
#include "Frame.h"
#include "Logging.h"
#include "CSSComputedStyleDeclaration.h"
#include "htmlediting.h"
#include "HTMLInputElement.h"
#include "HTMLNames.h"
#include "markup.h"
#include "ReplaceSelectionCommand.h"
#include "TextIterator.h"
#include "visible_units.h"
namespace WebCore {
using namespace HTMLNames;
DeleteSelectionCommand::DeleteSelectionCommand(Document *document, bool smartDelete, bool mergeBlocksAfterDelete, bool replace)
: CompositeEditCommand(document),
m_hasSelectionToDelete(false),
m_smartDelete(smartDelete),
m_mergeBlocksAfterDelete(mergeBlocksAfterDelete),
m_replace(replace),
m_startBlock(0),
m_endBlock(0),
m_typingStyle(0),
m_deleteIntoBlockquoteStyle(0)
{
}
DeleteSelectionCommand::DeleteSelectionCommand(Document *document, const Selection &selection, bool smartDelete, bool mergeBlocksAfterDelete, bool replace)
: CompositeEditCommand(document),
m_hasSelectionToDelete(true),
m_smartDelete(smartDelete),
m_mergeBlocksAfterDelete(mergeBlocksAfterDelete),
m_replace(replace),
m_selectionToDelete(selection),
m_startBlock(0),
m_endBlock(0),
m_typingStyle(0),
m_deleteIntoBlockquoteStyle(0)
{
}
void DeleteSelectionCommand::initializeStartEnd()
{
Node* startSpecialContainer = 0;
Node* endSpecialContainer = 0;
Position start = m_selectionToDelete.start();
Position end = m_selectionToDelete.end();
if (start.node()->hasTagName(hrTag))
start = Position(start.node(), 0);
else if (end.node()->hasTagName(hrTag))
end = Position(end.node(), 1);
while (1) {
startSpecialContainer = 0;
endSpecialContainer = 0;
Position s = positionOutsideContainingSpecialElement(start, &startSpecialContainer);
Position e = positionOutsideContainingSpecialElement(end, &endSpecialContainer);
if (!startSpecialContainer || !endSpecialContainer)
break;
start = s;
end = e;
}
m_upstreamStart = start.upstream();
m_downstreamStart = start.downstream();
m_upstreamEnd = end.upstream();
m_downstreamEnd = end.downstream();
}
void DeleteSelectionCommand::initializePositionData()
{
initializeStartEnd();
Node* startCell = enclosingTableCell(m_upstreamStart.node());
Node* endCell = enclosingTableCell(m_downstreamEnd.node());
if ((startCell || endCell) && endCell != startCell)
m_mergeBlocksAfterDelete = false;
VisiblePosition visibleEnd(m_downstreamEnd);
if (m_mergeBlocksAfterDelete && !isEndOfParagraph(visibleEnd))
m_endingPosition = m_downstreamEnd;
else
m_endingPosition = m_downstreamStart;
m_leadingWhitespace = m_upstreamStart.leadingWhitespacePosition(m_selectionToDelete.affinity());
m_trailingWhitespace = m_downstreamEnd.trailingWhitespacePosition(VP_DEFAULT_AFFINITY);
if (m_smartDelete) {
Position pos = VisiblePosition(m_upstreamStart, m_selectionToDelete.affinity()).deepEquivalent();
bool skipSmartDelete = pos.trailingWhitespacePosition(VP_DEFAULT_AFFINITY, true).isNotNull();
if (!skipSmartDelete)
skipSmartDelete = m_downstreamEnd.leadingWhitespacePosition(VP_DEFAULT_AFFINITY, true).isNotNull();
bool hasLeadingWhitespaceBeforeAdjustment = m_upstreamStart.leadingWhitespacePosition(m_selectionToDelete.affinity(), true).isNotNull();
if (!skipSmartDelete && hasLeadingWhitespaceBeforeAdjustment) {
VisiblePosition visiblePos = VisiblePosition(m_upstreamStart, VP_DEFAULT_AFFINITY).previous();
pos = visiblePos.deepEquivalent();
m_upstreamStart = pos.upstream();
m_downstreamStart = pos.downstream();
m_leadingWhitespace = m_upstreamStart.leadingWhitespacePosition(visiblePos.affinity());
}
if (!skipSmartDelete && !hasLeadingWhitespaceBeforeAdjustment && m_downstreamEnd.trailingWhitespacePosition(VP_DEFAULT_AFFINITY, true).isNotNull()) {
pos = VisiblePosition(m_downstreamEnd, VP_DEFAULT_AFFINITY).next().deepEquivalent();
m_upstreamEnd = pos.upstream();
m_downstreamEnd = pos.downstream();
m_trailingWhitespace = m_downstreamEnd.trailingWhitespacePosition(VP_DEFAULT_AFFINITY);
}
}
m_startBlock = m_downstreamStart.node()->enclosingBlockFlowOrTableElement();
m_endBlock = m_upstreamEnd.node()->enclosingBlockFlowOrTableElement();
}
void DeleteSelectionCommand::saveTypingStyleState()
{
RefPtr<CSSComputedStyleDeclaration> computedStyle = positionBeforeTabSpan(m_selectionToDelete.start()).computedStyle();
m_typingStyle = computedStyle->copyInheritableProperties();
if (nearestMailBlockquote(m_selectionToDelete.start().node())) {
computedStyle = m_selectionToDelete.end().computedStyle();
m_deleteIntoBlockquoteStyle = computedStyle->copyInheritableProperties();
} else
m_deleteIntoBlockquoteStyle = 0;
}
bool DeleteSelectionCommand::handleSpecialCaseBRDelete()
{
bool upstreamStartIsBR = m_upstreamStart.node()->hasTagName(brTag);
bool downstreamStartIsBR = m_downstreamStart.node()->hasTagName(brTag);
bool isBROnLineByItself = upstreamStartIsBR && downstreamStartIsBR && m_downstreamStart.node() == m_upstreamEnd.node();
if (isBROnLineByItself) {
removeNode(m_downstreamStart.node());
m_mergeBlocksAfterDelete = false;
return true;
}
if (upstreamStartIsBR && downstreamStartIsBR)
m_mergeBlocksAfterDelete = false;
return false;
}
static void updatePositionForNodeRemoval(Node* node, Position& position)
{
if (position.isNull())
return;
if (node->parent() == position.node() && node->nodeIndex() < (unsigned)position.offset())
position = Position(position.node(), position.offset() - 1);
if (position.node() == node || position.node()->isAncestor(node))
position = positionBeforeNode(node);
}
void DeleteSelectionCommand::removeNode(Node *node)
{
if (isTableStructureNode(node) || node == node->rootEditableElement()) {
Node *child = node->firstChild();
while (child) {
Node *remove = child;
child = child->nextSibling();
removeNode(remove);
}
updateLayout();
RenderObject *r = node->renderer();
if (r && r->isTableCell() && r->contentHeight() <= 0)
insertBlockPlaceholder(Position(node,0));
return;
}
if (node == m_startBlock && !isEndOfBlock(VisiblePosition(m_startBlock.get(), 0, DOWNSTREAM).previous()))
m_needPlaceholder = true;
else if (node == m_endBlock && !isStartOfBlock(VisiblePosition(m_endBlock.get(), maxDeepOffset(m_endBlock.get()), DOWNSTREAM).next()))
m_needPlaceholder = true;
updatePositionForNodeRemoval(node, m_endingPosition);
updatePositionForNodeRemoval(node, m_leadingWhitespace);
updatePositionForNodeRemoval(node, m_trailingWhitespace);
CompositeEditCommand::removeNode(node);
}
void updatePositionForTextRemoval(Node* node, int offset, int count, Position& position)
{
if (position.node() == node) {
if (position.offset() > offset + count)
position = Position(position.node(), position.offset() - count);
else if (position.offset() > offset)
position = Position(position.node(), offset);
}
}
void DeleteSelectionCommand::deleteTextFromNode(Text *node, int offset, int count)
{
updatePositionForTextRemoval(node, offset, count, m_endingPosition);
updatePositionForTextRemoval(node, offset, count, m_leadingWhitespace);
updatePositionForTextRemoval(node, offset, count, m_trailingWhitespace);
CompositeEditCommand::deleteTextFromNode(node, offset, count);
}
void DeleteSelectionCommand::handleGeneralDelete()
{
int startOffset = m_upstreamStart.offset();
Node* startNode = m_upstreamStart.node();
if (startNode == m_startBlock && startOffset == 0 && canHaveChildrenForEditing(startNode) && !startNode->hasTagName(tableTag)) {
startOffset = 0;
startNode = startNode->traverseNextNode();
}
if (startOffset >= startNode->caretMaxOffset() && startNode->isTextNode()) {
Text *text = static_cast<Text *>(startNode);
if (text->length() > (unsigned)startNode->caretMaxOffset())
deleteTextFromNode(text, startNode->caretMaxOffset(), text->length() - startNode->caretMaxOffset());
}
if (startOffset >= maxDeepOffset(startNode)) {
startNode = startNode->traverseNextSibling();
startOffset = 0;
}
if (!startNode)
return;
if (startNode == m_downstreamEnd.node()) {
if (!startNode->renderer() ||
(startOffset == 0 && m_downstreamEnd.offset() >= maxDeepOffset(startNode))) {
removeNode(startNode);
} else if (m_downstreamEnd.offset() - startOffset > 0) {
if (startNode->isTextNode()) {
Text *text = static_cast<Text *>(startNode);
deleteTextFromNode(text, startOffset, m_downstreamEnd.offset() - startOffset);
} else {
removeChildrenInRange(startNode, startOffset, m_downstreamEnd.offset());
m_endingPosition = m_upstreamStart;
}
}
}
else {
Node *node = startNode;
if (startOffset > 0) {
if (startNode->isTextNode()) {
Text *text = static_cast<Text *>(node);
deleteTextFromNode(text, startOffset, text->length() - startOffset);
node = node->traverseNextNode();
} else {
node = startNode->childNode(startOffset);
}
}
while (node && node != m_downstreamEnd.node()) {
if (Range::compareBoundaryPoints(Position(node, 0), m_downstreamEnd) >= 0) {
node = 0;
} else if (!m_downstreamEnd.node()->isAncestor(node)) {
Node *nextNode = node->traverseNextSibling();
if (node->parentNode() == m_downstreamEnd.node()) {
ASSERT(node->nodeIndex() < (unsigned)m_downstreamEnd.offset());
m_downstreamEnd = Position(m_downstreamEnd.node(), m_downstreamEnd.offset() - 1);
}
removeNode(node);
node = nextNode;
} else {
Node* n = node->lastDescendant();
if (m_downstreamEnd.node() == n && m_downstreamEnd.offset() >= n->caretMaxOffset()) {
removeNode(node);
node = 0;
} else
node = node->traverseNextNode();
}
}
if (m_downstreamEnd.node() != startNode && !m_upstreamStart.node()->isAncestor(m_downstreamEnd.node()) && m_downstreamEnd.node()->inDocument() && m_downstreamEnd.offset() >= m_downstreamEnd.node()->caretMinOffset()) {
if (m_downstreamEnd.offset() >= maxDeepOffset(m_downstreamEnd.node())) {
if (!m_upstreamStart.node()->inDocument() ||
m_upstreamStart.node() == m_downstreamEnd.node() ||
m_upstreamStart.node()->isAncestor(m_downstreamEnd.node())) {
m_upstreamStart = Position(m_downstreamEnd.node()->parentNode(), m_downstreamEnd.node()->nodeIndex());
}
removeNode(m_downstreamEnd.node());
} else {
if (m_downstreamEnd.node()->isTextNode()) {
Text *text = static_cast<Text *>(m_downstreamEnd.node());
if (m_downstreamEnd.offset() > 0) {
deleteTextFromNode(text, 0, m_downstreamEnd.offset());
m_downstreamEnd = Position(text, 0);
}
} else {
int offset = 0;
if (m_upstreamStart.node()->isAncestor(m_downstreamEnd.node())) {
Node *n = m_upstreamStart.node();
while (n && n->parentNode() != m_downstreamEnd.node())
n = n->parentNode();
if (n)
offset = n->nodeIndex() + 1;
}
removeChildrenInRange(m_downstreamEnd.node(), offset, m_downstreamEnd.offset());
m_downstreamEnd = Position(m_downstreamEnd.node(), offset);
}
}
}
}
}
void DeleteSelectionCommand::fixupWhitespace()
{
updateLayout();
if (m_leadingWhitespace.isNotNull() && !m_leadingWhitespace.isRenderedCharacter()) {
Text* textNode = static_cast<Text*>(m_leadingWhitespace.node());
ASSERT(!textNode->renderer() || textNode->renderer()->style()->collapseWhiteSpace());
replaceTextInNode(textNode, m_leadingWhitespace.offset(), 1, nonBreakingSpaceString());
}
if (m_trailingWhitespace.isNotNull() && !m_trailingWhitespace.isRenderedCharacter()) {
Text* textNode = static_cast<Text*>(m_trailingWhitespace.node());
ASSERT(!textNode->renderer() ||textNode->renderer()->style()->collapseWhiteSpace());
replaceTextInNode(textNode, m_trailingWhitespace.offset(), 1, nonBreakingSpaceString());
}
}
void DeleteSelectionCommand::mergeParagraphs()
{
if (!m_mergeBlocksAfterDelete)
return;
if (!m_downstreamEnd.node()->inDocument() || !m_upstreamStart.node()->inDocument())
return;
if (Range::compareBoundaryPoints(m_upstreamStart, m_downstreamEnd) > 0)
return;
if (m_endBlock == m_startBlock)
return;
VisiblePosition startOfParagraphToMove(m_downstreamEnd);
VisiblePosition mergeDestination(m_upstreamStart);
if (!mergeDestination.deepEquivalent().node()->isAncestor(m_upstreamStart.node()->enclosingBlockFlowElement())) {
insertNodeAt(createBreakElement(document()).get(), m_upstreamStart.node(), m_upstreamStart.offset());
mergeDestination = VisiblePosition(m_upstreamStart);
}
if (mergeDestination == startOfParagraphToMove)
return;
VisiblePosition endOfParagraphToMove = endOfParagraph(startOfParagraphToMove);
if (mergeDestination == endOfParagraphToMove)
return;
if (isStartOfParagraph(mergeDestination) &&
startOfParagraphToMove.deepEquivalent().node()->renderer()->caretRect(startOfParagraphToMove.deepEquivalent().offset()).location().x() >
mergeDestination.deepEquivalent().node()->renderer()->caretRect(startOfParagraphToMove.deepEquivalent().offset()).location().x()) {
ASSERT(mergeDestination.deepEquivalent().downstream().node()->hasTagName(brTag));
removeNodeAndPruneAncestors(mergeDestination.deepEquivalent().downstream().node());
m_endingPosition = startOfParagraphToMove.deepEquivalent();
return;
}
moveParagraph(startOfParagraphToMove, endOfParagraphToMove, mergeDestination);
m_endingPosition = endingSelection().start();
}
void DeleteSelectionCommand::calculateTypingStyleAfterDelete(Node *insertedPlaceholder)
{
if (m_deleteIntoBlockquoteStyle && !nearestMailBlockquote(m_endingPosition.node()))
m_typingStyle = m_deleteIntoBlockquoteStyle;
m_deleteIntoBlockquoteStyle = 0;
RefPtr<CSSComputedStyleDeclaration> endingStyle = new CSSComputedStyleDeclaration(m_endingPosition.node());
endingStyle->diff(m_typingStyle.get());
if (!m_typingStyle->length())
m_typingStyle = 0;
if (insertedPlaceholder && m_typingStyle) {
setEndingSelection(Selection(Position(insertedPlaceholder, 0), DOWNSTREAM));
applyStyle(m_typingStyle.get(), EditActionUnspecified);
m_typingStyle = 0;
}
document()->frame()->setTypingStyle(m_typingStyle.get());
setTypingStyle(m_typingStyle.get());
}
void DeleteSelectionCommand::clearTransientState()
{
m_selectionToDelete = Selection();
m_upstreamStart.clear();
m_downstreamStart.clear();
m_upstreamEnd.clear();
m_downstreamEnd.clear();
m_endingPosition.clear();
m_leadingWhitespace.clear();
m_trailingWhitespace.clear();
}
void DeleteSelectionCommand::doApply()
{
if (!m_hasSelectionToDelete)
m_selectionToDelete = endingSelection();
if (!m_selectionToDelete.isRange())
return;
if (!m_replace) {
Node* startNode = m_selectionToDelete.start().node();
Node* ancestorNode = startNode ? startNode->shadowAncestorNode() : 0;
if (ancestorNode && ancestorNode->hasTagName(inputTag) && static_cast<HTMLInputElement*>(ancestorNode)->isNonWidgetTextField())
document()->frame()->textWillBeDeletedInTextField(static_cast<Element*>(ancestorNode));
}
EAffinity affinity = m_selectionToDelete.affinity();
Position downstreamEnd = m_selectionToDelete.end().downstream();
m_needPlaceholder = isStartOfParagraph(m_selectionToDelete.visibleStart()) &&
isEndOfParagraph(m_selectionToDelete.visibleEnd()) &&
!(downstreamEnd.node()->hasTagName(brTag) && downstreamEnd.offset() == 0);
initializePositionData();
if (!m_startBlock || !m_endBlock) {
clearTransientState();
return;
}
deleteInsignificantTextDownstream(m_trailingWhitespace);
saveTypingStyleState();
if (handleSpecialCaseBRDelete()) {
calculateTypingStyleAfterDelete(false);
setEndingSelection(Selection(m_endingPosition, affinity));
clearTransientState();
rebalanceWhitespace();
return;
}
handleGeneralDelete();
fixupWhitespace();
RefPtr<Node> placeholder = m_needPlaceholder ? createBreakElement(document()) : 0;
mergeParagraphs();
if (placeholder)
insertNodeAt(placeholder.get(), m_endingPosition.node(), m_endingPosition.offset());
calculateTypingStyleAfterDelete(placeholder.get());
rebalanceWhitespaceAt(m_endingPosition);
setEndingSelection(Selection(m_endingPosition, affinity));
clearTransientState();
}
EditAction DeleteSelectionCommand::editingAction() const
{
return EditActionCut;
}
bool DeleteSelectionCommand::preservesTypingStyle() const
{
return true;
}
}