Commit 186f5bb6 authored by rniwa@webkit.org's avatar rniwa@webkit.org
Browse files

2011-01-18 Ryosuke Niwa <rniwa@webkit.org>

        Reviewed by Eric Seidel.

        Stop instantiating legacy editing positions in DeleteSelectionCommand, IndentOudentCommand,
        InsertLineBreakCommand, InsertListCOmmand.cpp, InsertParagraphSeparatorCommand, and htmlediting.cpp
        https://bugs.webkit.org/show_bug.cgi?id=52644

        This is a cleanup to stop instantiating legacy editing positions in the following tests.
        firstDeepEditingPositionForNode and lastDeepEditingPositionForNode in htmlediting.h are not updated
        because doing so requires significant code changes. They are tracked on the bug 52642.

        * editing/DeleteSelectionCommand.cpp:
        (WebCore::DeleteSelectionCommand::initializeStartEnd):
        (WebCore::updatePositionForNodeRemoval):
        (WebCore::DeleteSelectionCommand::removeNode):
        (WebCore::updatePositionForTextRemoval):
        (WebCore::DeleteSelectionCommand::handleGeneralDelete):
        * editing/IndentOutdentCommand.cpp:
        (WebCore::IndentOutdentCommand::outdentParagraph):
        * editing/InsertLineBreakCommand.cpp:
        (WebCore::InsertLineBreakCommand::doApply):
        * editing/InsertListCommand.cpp:
        (WebCore::InsertListCommand::doApplyForSingleParagraph):
        (WebCore::InsertListCommand::unlistifyParagraph):
        (WebCore::InsertListCommand::listifyParagraph):
        * editing/InsertParagraphSeparatorCommand.cpp:
        (WebCore::InsertParagraphSeparatorCommand::doApply):
        * editing/htmlediting.cpp:
        (WebCore::enclosingBlock):
        (WebCore::enclosingList):
        (WebCore::enclosingListChild):
        (WebCore::indexForVisiblePosition):
        (WebCore::isNodeVisiblyContainedWithin):
        (WebCore::avoidIntersectionWithNode):


git-svn-id: http://svn.webkit.org/repository/webkit/trunk@76057 268f45cc-cd09-0410-ab3c-d52691b4dbfc
parent 019aefe5
2011-01-18 Ryosuke Niwa <rniwa@webkit.org>
Reviewed by Eric Seidel.
Stop instantiating legacy editing positions in DeleteSelectionCommand, IndentOudentCommand,
InsertLineBreakCommand, InsertListCOmmand.cpp, InsertParagraphSeparatorCommand, and htmlediting.cpp
https://bugs.webkit.org/show_bug.cgi?id=52644
This is a cleanup to stop instantiating legacy editing positions in the following tests.
firstDeepEditingPositionForNode and lastDeepEditingPositionForNode in htmlediting.h are not updated
because doing so requires significant code changes. They are tracked on the bug 52642.
* editing/DeleteSelectionCommand.cpp:
(WebCore::DeleteSelectionCommand::initializeStartEnd):
(WebCore::updatePositionForNodeRemoval):
(WebCore::DeleteSelectionCommand::removeNode):
(WebCore::updatePositionForTextRemoval):
(WebCore::DeleteSelectionCommand::handleGeneralDelete):
* editing/IndentOutdentCommand.cpp:
(WebCore::IndentOutdentCommand::outdentParagraph):
* editing/InsertLineBreakCommand.cpp:
(WebCore::InsertLineBreakCommand::doApply):
* editing/InsertListCommand.cpp:
(WebCore::InsertListCommand::doApplyForSingleParagraph):
(WebCore::InsertListCommand::unlistifyParagraph):
(WebCore::InsertListCommand::listifyParagraph):
* editing/InsertParagraphSeparatorCommand.cpp:
(WebCore::InsertParagraphSeparatorCommand::doApply):
* editing/htmlediting.cpp:
(WebCore::enclosingBlock):
(WebCore::enclosingList):
(WebCore::enclosingListChild):
(WebCore::indexForVisiblePosition):
(WebCore::isNodeVisiblyContainedWithin):
(WebCore::avoidIntersectionWithNode):
2011-01-18 Benjamin Kalman <kalman@chromium.org>
 
Reviewed by Ryosuke Niwa.
......@@ -111,9 +111,9 @@ void DeleteSelectionCommand::initializeStartEnd(Position& start, Position& end)
// For HRs, we'll get a position at (HR,1) when hitting delete from the beginning of the previous line, or (HR,0) when forward deleting,
// but in these cases, we want to delete it, so manually expand the selection
if (start.node()->hasTagName(hrTag))
start = Position(start.node(), 0);
start = positionBeforeNode(start.node());
else if (end.node()->hasTagName(hrTag))
end = Position(end.node(), 1);
end = positionAfterNode(end.node());
// FIXME: This is only used so that moveParagraphs can avoid the bugs in special element expansion.
if (!m_expandForSpecialElements)
......@@ -315,10 +315,22 @@ static void updatePositionForNodeRemoval(Node* node, Position& position)
{
if (position.isNull())
return;
if (node->parentNode() == position.node() && node->nodeIndex() < (unsigned)position.deprecatedEditingOffset())
position = Position(position.node(), position.deprecatedEditingOffset() - 1);
if (position.node() == node || position.node()->isDescendantOf(node))
position = positionInParentBeforeNode(node);
switch (position.anchorType()) {
case Position::PositionIsOffsetInAnchor:
if (position.containerNode() == node->parentNode() && static_cast<unsigned>(position.offsetInContainerNode()) > node->nodeIndex())
position.moveToOffset(position.offsetInContainerNode() - 1);
else if (node->contains(position.containerNode()))
position = positionInParentBeforeNode(node);
break;
case Position::PositionIsAfterAnchor:
if (node->contains(position.anchorNode()))
position = positionInParentAfterNode(node);
break;
case Position::PositionIsBeforeAnchor:
if (node->contains(position.anchorNode()))
position = positionInParentBeforeNode(node);
break;
}
}
void DeleteSelectionCommand::removeNode(PassRefPtr<Node> node)
......@@ -362,7 +374,7 @@ void DeleteSelectionCommand::removeNode(PassRefPtr<Node> node)
updateLayout();
RenderObject *r = node->renderer();
if (r && r->isTableCell() && toRenderTableCell(r)->contentHeight() <= 0)
insertBlockPlaceholder(Position(node, 0));
insertBlockPlaceholder(firstPositionInNode(node.get()));
return;
}
......@@ -381,12 +393,13 @@ void DeleteSelectionCommand::removeNode(PassRefPtr<Node> node)
static void updatePositionForTextRemoval(Node* node, int offset, int count, Position& position)
{
if (position.node() == node) {
if (position.deprecatedEditingOffset() > offset + count)
position = Position(position.node(), position.deprecatedEditingOffset() - count);
else if (position.deprecatedEditingOffset() > offset)
position = Position(position.node(), offset);
}
if (position.anchorType() != Position::PositionIsOffsetInAnchor || position.containerNode() != node)
return;
if (position.offsetInContainerNode() > offset + count)
position.moveToOffset(position.offsetInContainerNode() - count);
else if (position.offsetInContainerNode() > offset)
position.moveToOffset(offset);
}
void DeleteSelectionCommand::deleteTextFromNode(PassRefPtr<Text> node, unsigned offset, unsigned count)
......@@ -463,7 +476,7 @@ void DeleteSelectionCommand::handleGeneralDelete()
// handle deleting all nodes that are completely selected
while (node && node != m_downstreamEnd.node()) {
if (comparePositions(Position(node.get(), 0), m_downstreamEnd) >= 0) {
if (comparePositions(firstPositionInOrBeforeNode(node.get()), m_downstreamEnd) >= 0) {
// traverseNextSibling just blew past the end position, so stop deleting
node = 0;
} else if (!m_downstreamEnd.node()->isDescendantOf(node.get())) {
......@@ -471,8 +484,9 @@ void DeleteSelectionCommand::handleGeneralDelete()
// if we just removed a node from the end container, update end position so the
// check above will work
if (node->parentNode() == m_downstreamEnd.node()) {
ASSERT(m_downstreamEnd.deprecatedEditingOffset());
ASSERT(node->nodeIndex() < (unsigned)m_downstreamEnd.deprecatedEditingOffset());
m_downstreamEnd = Position(m_downstreamEnd.node(), m_downstreamEnd.deprecatedEditingOffset() - 1);
m_downstreamEnd.moveToOffset(m_downstreamEnd.deprecatedEditingOffset() - 1);
}
removeNode(node.get());
node = nextNode.get();
......@@ -513,7 +527,7 @@ void DeleteSelectionCommand::handleGeneralDelete()
offset = n->nodeIndex() + 1;
}
removeChildrenInRange(m_downstreamEnd.node(), offset, m_downstreamEnd.deprecatedEditingOffset());
m_downstreamEnd = Position(m_downstreamEnd.node(), offset);
m_downstreamEnd.moveToOffset(offset);
}
}
}
......
......@@ -134,11 +134,11 @@ void IndentOutdentCommand::outdentParagraph()
}
// The selection is inside a blockquote i.e. enclosingNode is a blockquote
VisiblePosition positionInEnclosingBlock = VisiblePosition(Position(enclosingNode, 0));
VisiblePosition positionInEnclosingBlock = VisiblePosition(firstPositionInNode(enclosingNode));
// If the blockquote is inline, the start of the enclosing block coincides with
// positionInEnclosingBlock.
VisiblePosition startOfEnclosingBlock = (enclosingNode->renderer() && enclosingNode->renderer()->isInline()) ? positionInEnclosingBlock : startOfBlock(positionInEnclosingBlock);
VisiblePosition lastPositionInEnclosingBlock = VisiblePosition(Position(enclosingNode, enclosingNode->childNodeCount()));
VisiblePosition lastPositionInEnclosingBlock = VisiblePosition(lastPositionInNode(enclosingNode));
VisiblePosition endOfEnclosingBlock = endOfBlock(lastPositionInEnclosingBlock);
if (visibleStartOfParagraph == startOfEnclosingBlock &&
visibleEndOfParagraph == endOfEnclosingBlock) {
......@@ -176,7 +176,7 @@ void IndentOutdentCommand::outdentParagraph()
}
RefPtr<Node> placeholder = createBreakElement(document());
insertNodeBefore(placeholder, splitBlockquoteNode);
moveParagraph(startOfParagraph(visibleStartOfParagraph), endOfParagraph(visibleEndOfParagraph), VisiblePosition(Position(placeholder.get(), 0)), true);
moveParagraph(startOfParagraph(visibleStartOfParagraph), endOfParagraph(visibleEndOfParagraph), positionBeforeNode(placeholder.get()), true);
}
// FIXME: We should merge this function with ApplyBlockElementCommand::formatSelection
......
......@@ -121,13 +121,13 @@ void InsertLineBreakCommand::doApply()
if (needExtraLineBreak)
insertNodeBefore(nodeToInsert->cloneNode(false), nodeToInsert);
VisiblePosition endingPosition(Position(nodeToInsert.get(), 0));
VisiblePosition endingPosition(positionBeforeNode(nodeToInsert.get()));
setEndingSelection(VisibleSelection(endingPosition));
} else if (pos.deprecatedEditingOffset() <= caretMinOffset(pos.node())) {
insertNodeAt(nodeToInsert.get(), pos);
// Insert an extra br or '\n' if the just inserted one collapsed.
if (!isStartOfParagraph(VisiblePosition(Position(nodeToInsert.get(), 0))))
if (!isStartOfParagraph(positionBeforeNode(nodeToInsert.get())))
insertNodeBefore(nodeToInsert->cloneNode(false).get(), nodeToInsert.get());
setEndingSelection(VisibleSelection(positionInParentAfterNode(nodeToInsert.get()), DOWNSTREAM));
......@@ -141,7 +141,7 @@ void InsertLineBreakCommand::doApply()
Text* textNode = static_cast<Text*>(pos.node());
splitTextNode(textNode, pos.deprecatedEditingOffset());
insertNodeBefore(nodeToInsert, textNode);
Position endingPosition = Position(textNode, 0);
Position endingPosition = firstPositionInNode(textNode);
// Handle whitespace that occurs after the split
updateLayout();
......@@ -156,7 +156,7 @@ void InsertLineBreakCommand::doApply()
else {
RefPtr<Text> nbspNode = document()->createTextNode(nonBreakingSpaceString());
insertNodeAt(nbspNode.get(), positionBeforeTextNode);
endingPosition = Position(nbspNode.get(), 0);
endingPosition = firstPositionInNode(nbspNode.get());
}
}
......
......@@ -217,7 +217,7 @@ void InsertListCommand::doApplyForSingleParagraph(bool forceCreateList, const Qu
RefPtr<HTMLElement> newList = createHTMLElement(document(), listTag);
insertNodeBefore(newList, listNode);
Node* firstChildInList = enclosingListChild(VisiblePosition(Position(listNode, 0)).deepEquivalent().node(), listNode.get());
Node* firstChildInList = enclosingListChild(VisiblePosition(firstPositionInNode(listNode.get())).deepEquivalent().node(), listNode.get());
Node* outerBlock = firstChildInList->isBlockFlow() ? firstChildInList : listNode.get();
moveParagraphWithClones(firstPositionInNode(listNode.get()), lastPositionInNode(listNode.get()), newList.get(), outerBlock);
......@@ -302,7 +302,7 @@ void InsertListCommand::unlistifyParagraph(const VisiblePosition& originalStart,
} else
insertNodeAfter(nodeToInsert, listNode);
VisiblePosition insertionPoint = VisiblePosition(Position(placeholder.get(), 0));
VisiblePosition insertionPoint = VisiblePosition(positionBeforeNode(placeholder.get()));
moveParagraphs(start, end, insertionPoint, true);
}
......@@ -345,7 +345,7 @@ PassRefPtr<HTMLElement> InsertListCommand::listifyParagraph(const VisiblePositio
if (previousList)
appendNode(listItemElement, previousList);
else if (nextList)
insertNodeAt(listItemElement, Position(nextList, 0));
insertNodeAt(listItemElement, positionBeforeNode(nextList));
else {
// Create the list.
listElement = createHTMLElement(document(), listTag);
......@@ -356,7 +356,7 @@ PassRefPtr<HTMLElement> InsertListCommand::listifyParagraph(const VisiblePositio
// by a br or a '\n', will invalidate start and end. Insert
// a placeholder and then recompute start and end.
RefPtr<Node> placeholder = insertBlockPlaceholder(start.deepEquivalent());
start = VisiblePosition(Position(placeholder.get(), 0));
start = positionBeforeNode(placeholder.get());
end = start;
}
......@@ -379,7 +379,7 @@ PassRefPtr<HTMLElement> InsertListCommand::listifyParagraph(const VisiblePositio
start = startOfParagraph(originalStart);
}
moveParagraph(start, end, VisiblePosition(Position(placeholder.get(), 0)), true);
moveParagraph(start, end, positionBeforeNode(placeholder.get()), true);
if (listElement)
return mergeWithNeighboringLists(listElement);
......
......@@ -242,7 +242,7 @@ void InsertParagraphSeparatorCommand::doApply()
appendBlockPlaceholder(parent);
setEndingSelection(VisibleSelection(Position(parent.get(), 0), DOWNSTREAM));
setEndingSelection(VisibleSelection(firstPositionInNode(parent.get()), DOWNSTREAM));
return;
}
......@@ -378,7 +378,8 @@ void InsertParagraphSeparatorCommand::doApply()
// Handle whitespace that occurs after the split
if (splitText) {
updateLayout();
insertionPosition = Position(insertionPosition.node(), 0);
if (insertionPosition.anchorType() == Position::PositionIsOffsetInAnchor)
insertionPosition.moveToOffset(0);
if (!insertionPosition.isRenderedCharacter()) {
// Clear out all whitespace and insert one non-breaking space
ASSERT(!insertionPosition.node()->renderer() || insertionPosition.node()->renderer()->style()->collapseWhiteSpace());
......@@ -388,7 +389,7 @@ void InsertParagraphSeparatorCommand::doApply()
}
}
setEndingSelection(VisibleSelection(Position(blockToInsert.get(), 0), DOWNSTREAM));
setEndingSelection(VisibleSelection(firstPositionInNode(blockToInsert.get()), DOWNSTREAM));
applyStyleAfterInsertion(startBlock);
}
......
......@@ -329,7 +329,7 @@ bool isBlock(const Node* node)
// knowing about these kinds of special cases.
Node* enclosingBlock(Node* node)
{
return static_cast<Element*>(enclosingNodeOfType(Position(node, 0), isBlock));
return static_cast<Element*>(enclosingNodeOfType(firstPositionInOrBeforeNode(node), isBlock));
}
// Internally editing uses "invalid" positions for historical reasons. For
......@@ -704,7 +704,7 @@ HTMLElement* enclosingList(Node* node)
if (!node)
return 0;
Node* root = highestEditableRoot(Position(node, 0));
Node* root = highestEditableRoot(firstPositionInOrBeforeNode(node));
for (ContainerNode* n = node->parentNode(); n; n = n->parentNode()) {
if (n->hasTagName(ulTag) || n->hasTagName(olTag))
......@@ -722,7 +722,7 @@ HTMLElement* enclosingListChild(Node *node)
return 0;
// Check for a list item element, or for a node whose parent is a list element. Such a node
// will appear visually as a list item (but without a list marker)
Node* root = highestEditableRoot(Position(node, 0));
Node* root = highestEditableRoot(firstPositionInOrBeforeNode(node));
// FIXME: This function is inappropriately named if it starts with node instead of node->parentNode()
for (Node* n = node; n && n->parentNode(); n = n->parentNode()) {
......@@ -1081,7 +1081,8 @@ int indexForVisiblePosition(const VisiblePosition& visiblePosition)
if (visiblePosition.isNull())
return 0;
Position p(visiblePosition.deepEquivalent());
RefPtr<Range> range = Range::create(p.node()->document(), Position(p.node()->document(), 0), rangeCompliantEquivalent(p));
RefPtr<Range> range = Range::create(p.node()->document(), firstPositionInNode(p.anchorNode()->document()->documentElement()),
rangeCompliantEquivalent(p));
return TextIterator::rangeLength(range.get(), true);
}
......@@ -1104,11 +1105,11 @@ bool isNodeVisiblyContainedWithin(Node* node, const Range* selectedRange)
return true;
bool startIsVisuallySame = visiblePositionBeforeNode(node) == selectedRange->startPosition();
if (startIsVisuallySame && comparePositions(Position(node->parentNode(), node->nodeIndex()+1), selectedRange->endPosition()) < 0)
if (startIsVisuallySame && comparePositions(positionInParentAfterNode(node), selectedRange->endPosition()) < 0)
return true;
bool endIsVisuallySame = visiblePositionAfterNode(node) == selectedRange->endPosition();
if (endIsVisuallySame && comparePositions(selectedRange->startPosition(), Position(node->parentNode(), node->nodeIndex())) < 0)
if (endIsVisuallySame && comparePositions(selectedRange->startPosition(), positionInParentBeforeNode(node)) < 0)
return true;
return startIsVisuallySame && endIsVisuallySame;
......@@ -1157,23 +1158,23 @@ VisibleSelection avoidIntersectionWithNode(const VisibleSelection& selection, No
{
if (selection.isNone())
return VisibleSelection(selection);
VisibleSelection updatedSelection(selection);
Node* base = selection.base().node();
Node* extent = selection.extent().node();
ASSERT(base);
ASSERT(extent);
if (base == node || base->isDescendantOf(node)) {
ASSERT(node->parentNode());
updatedSelection.setBase(Position(node->parentNode(), node->nodeIndex()));
updatedSelection.setBase(positionInParentBeforeNode(node));
}
if (extent == node || extent->isDescendantOf(node)) {
ASSERT(node->parentNode());
updatedSelection.setExtent(Position(node->parentNode(), node->nodeIndex()));
updatedSelection.setExtent(positionInParentBeforeNode(node));
}
return updatedSelection;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment