blob: cbebe541198da51dab6a2c438e27dd6621caab52 [file] [log] [blame]
mjsa0fe4042005-05-13 08:37:15 +00001/*
2 * Copyright (C) 2005 Apple Computer, Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
mjsb64c50a2005-10-03 21:13:12 +000026#include "config.h"
darina68e0432006-02-14 21:40:54 +000027#include "DeleteSelectionCommand.h"
mjsa0fe4042005-05-13 08:37:15 +000028
darinb9481ed2006-03-20 02:57:59 +000029#include "Document.h"
darin36d11362006-04-11 16:30:21 +000030#include "DocumentFragment.h"
commit-queue@webkit.orgf3628722010-11-28 02:54:43 +000031#include "EditingBoundary.h"
justing5745f952006-11-11 01:56:24 +000032#include "Editor.h"
justingf90e28d2007-07-30 18:25:19 +000033#include "EditorClient.h"
eseidel40eb1b92006-03-25 22:20:36 +000034#include "Element.h"
mjs810e9762006-01-09 21:39:14 +000035#include "Frame.h"
darinbbe64662006-01-16 17:52:23 +000036#include "htmlediting.h"
adele174364d2006-04-13 01:50:04 +000037#include "HTMLInputElement.h"
darin98fa8b82006-03-20 08:03:57 +000038#include "HTMLNames.h"
hyatt@apple.comd885df72009-01-22 02:31:52 +000039#include "RenderTableCell.h"
darin42563ac52007-01-22 17:28:57 +000040#include "Text.h"
mjsa0fe4042005-05-13 08:37:15 +000041#include "visible_units.h"
mjsa0fe4042005-05-13 08:37:15 +000042
darinbbe64662006-01-16 17:52:23 +000043namespace WebCore {
darinedbc5e42005-08-25 23:13:58 +000044
darinbbe64662006-01-16 17:52:23 +000045using namespace HTMLNames;
mjsa0fe4042005-05-13 08:37:15 +000046
justing94b6f9f2007-10-31 01:27:54 +000047static bool isTableRow(const Node* node)
justing187d2922007-07-17 21:05:21 +000048{
49 return node && node->hasTagName(trTag);
50}
51
justing13e16aa2007-07-17 21:55:05 +000052static bool isTableCellEmpty(Node* cell)
justing187d2922007-07-17 21:05:21 +000053{
54 ASSERT(isTableCell(cell));
leviw@chromium.org1f953622011-03-15 19:36:51 +000055 return VisiblePosition(firstPositionInNode(cell)) == VisiblePosition(lastPositionInNode(cell));
justing187d2922007-07-17 21:05:21 +000056}
57
justing13e16aa2007-07-17 21:55:05 +000058static bool isTableRowEmpty(Node* row)
justing187d2922007-07-17 21:05:21 +000059{
60 if (!isTableRow(row))
61 return false;
62
63 for (Node* child = row->firstChild(); child; child = child->nextSibling())
64 if (isTableCell(child) && !isTableCellEmpty(child))
65 return false;
66
67 return true;
68}
69
justing113aaf32007-01-25 01:00:36 +000070DeleteSelectionCommand::DeleteSelectionCommand(Document *document, bool smartDelete, bool mergeBlocksAfterDelete, bool replace, bool expandForSpecialElements)
mjsa0fe4042005-05-13 08:37:15 +000071 : CompositeEditCommand(document),
72 m_hasSelectionToDelete(false),
73 m_smartDelete(smartDelete),
74 m_mergeBlocksAfterDelete(mergeBlocksAfterDelete),
commit-queue@webkit.org49ecbf72011-03-15 10:47:29 +000075 m_needPlaceholder(false),
adeleb2f2b662006-07-15 17:22:50 +000076 m_replace(replace),
justing113aaf32007-01-25 01:00:36 +000077 m_expandForSpecialElements(expandForSpecialElements),
justin.garcia@apple.com7e95c7b2008-11-12 20:08:06 +000078 m_pruneStartBlockIfNecessary(false),
mitz@apple.comfc292c32009-05-03 04:09:41 +000079 m_startsAtEmptyLine(false),
mjsa0fe4042005-05-13 08:37:15 +000080 m_startBlock(0),
81 m_endBlock(0),
justing503ccf12005-07-29 22:50:47 +000082 m_typingStyle(0),
83 m_deleteIntoBlockquoteStyle(0)
mjsa0fe4042005-05-13 08:37:15 +000084{
85}
86
eric@webkit.org87ea95c2009-02-09 21:43:24 +000087DeleteSelectionCommand::DeleteSelectionCommand(const VisibleSelection& selection, bool smartDelete, bool mergeBlocksAfterDelete, bool replace, bool expandForSpecialElements)
rniwa@webkit.org62b16972011-02-21 09:28:48 +000088 : CompositeEditCommand(selection.start().anchorNode()->document()),
mjsa0fe4042005-05-13 08:37:15 +000089 m_hasSelectionToDelete(true),
90 m_smartDelete(smartDelete),
91 m_mergeBlocksAfterDelete(mergeBlocksAfterDelete),
commit-queue@webkit.org49ecbf72011-03-15 10:47:29 +000092 m_needPlaceholder(false),
adeleb2f2b662006-07-15 17:22:50 +000093 m_replace(replace),
justing113aaf32007-01-25 01:00:36 +000094 m_expandForSpecialElements(expandForSpecialElements),
justin.garcia@apple.com7e95c7b2008-11-12 20:08:06 +000095 m_pruneStartBlockIfNecessary(false),
mitz@apple.comfc292c32009-05-03 04:09:41 +000096 m_startsAtEmptyLine(false),
mjsa0fe4042005-05-13 08:37:15 +000097 m_selectionToDelete(selection),
98 m_startBlock(0),
99 m_endBlock(0),
justing503ccf12005-07-29 22:50:47 +0000100 m_typingStyle(0),
101 m_deleteIntoBlockquoteStyle(0)
mjsa0fe4042005-05-13 08:37:15 +0000102{
103}
104
justing113aaf32007-01-25 01:00:36 +0000105void DeleteSelectionCommand::initializeStartEnd(Position& start, Position& end)
darin35384062006-08-24 22:07:45 +0000106{
darin2b0ff522006-05-03 19:32:21 +0000107 Node* startSpecialContainer = 0;
108 Node* endSpecialContainer = 0;
harrisondec5e092006-01-25 21:05:02 +0000109
justing113aaf32007-01-25 01:00:36 +0000110 start = m_selectionToDelete.start();
111 end = m_selectionToDelete.end();
justinge6f80c12006-06-09 04:57:51 +0000112
justing2112d432006-11-28 21:25:28 +0000113 // 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,
lweintraub635ec2a2006-07-13 18:28:28 +0000114 // but in these cases, we want to delete it, so manually expand the selection
rniwa@webkit.org62b16972011-02-21 09:28:48 +0000115 if (start.deprecatedNode()->hasTagName(hrTag))
116 start = positionBeforeNode(start.deprecatedNode());
117 else if (end.deprecatedNode()->hasTagName(hrTag))
118 end = positionAfterNode(end.deprecatedNode());
lweintraub635ec2a2006-07-13 18:28:28 +0000119
dbates@webkit.orgef42d382010-01-25 19:20:06 +0000120 // FIXME: This is only used so that moveParagraphs can avoid the bugs in special element expansion.
justing113aaf32007-01-25 01:00:36 +0000121 if (!m_expandForSpecialElements)
122 return;
123
justin.garcia@apple.com64125512008-02-20 17:48:44 +0000124 while (1) {
justinge6f80c12006-06-09 04:57:51 +0000125 startSpecialContainer = 0;
126 endSpecialContainer = 0;
127
justing47d836d2007-06-08 00:02:06 +0000128 Position s = positionBeforeContainingSpecialElement(start, &startSpecialContainer);
129 Position e = positionAfterContainingSpecialElement(end, &endSpecialContainer);
justinge6f80c12006-06-09 04:57:51 +0000130
justing47d836d2007-06-08 00:02:06 +0000131 if (!startSpecialContainer && !endSpecialContainer)
justinge6f80c12006-06-09 04:57:51 +0000132 break;
justin.garcia@apple.com64125512008-02-20 17:48:44 +0000133
134 if (VisiblePosition(start) != m_selectionToDelete.visibleStart() || VisiblePosition(end) != m_selectionToDelete.visibleEnd())
135 break;
eric@webkit.org05d6e932009-06-02 21:31:08 +0000136
eric@webkit.orgf74ae5c2009-09-09 23:25:47 +0000137 // If we're going to expand to include the startSpecialContainer, it must be fully selected.
138 if (startSpecialContainer && !endSpecialContainer && comparePositions(positionInParentAfterNode(startSpecialContainer), end) > -1)
justing47d836d2007-06-08 00:02:06 +0000139 break;
140
eric@webkit.org05d6e932009-06-02 21:31:08 +0000141 // If we're going to expand to include the endSpecialContainer, it must be fully selected.
eric@webkit.orgf74ae5c2009-09-09 23:25:47 +0000142 if (endSpecialContainer && !startSpecialContainer && comparePositions(start, positionInParentBeforeNode(endSpecialContainer)) > -1)
justing47d836d2007-06-08 00:02:06 +0000143 break;
eric@webkit.orgf74ae5c2009-09-09 23:25:47 +0000144
justing47d836d2007-06-08 00:02:06 +0000145 if (startSpecialContainer && startSpecialContainer->isDescendantOf(endSpecialContainer))
justing2bdb03e2006-11-30 02:54:19 +0000146 // Don't adjust the end yet, it is the end of a special element that contains the start
147 // special element (which may or may not be fully selected).
148 start = s;
justing47d836d2007-06-08 00:02:06 +0000149 else if (endSpecialContainer && endSpecialContainer->isDescendantOf(startSpecialContainer))
justing2bdb03e2006-11-30 02:54:19 +0000150 // Don't adjust the start yet, it is the start of a special element that contains the end
151 // special element (which may or may not be fully selected).
152 end = e;
153 else {
154 start = s;
155 end = e;
156 }
harrisondec5e092006-01-25 21:05:02 +0000157 }
harrisondec5e092006-01-25 21:05:02 +0000158}
159
ojan@chromium.org078aa802010-03-10 00:17:11 +0000160void DeleteSelectionCommand::setStartingSelectionOnSmartDelete(const Position& start, const Position& end)
ojan@chromium.org77dae9d2010-03-09 23:41:38 +0000161{
162 VisiblePosition newBase;
163 VisiblePosition newExtent;
164 if (startingSelection().isBaseFirst()) {
165 newBase = start;
166 newExtent = end;
167 } else {
168 newBase = end;
169 newExtent = start;
170 }
171 setStartingSelection(VisibleSelection(newBase, newExtent));
172}
173
mjsa0fe4042005-05-13 08:37:15 +0000174void DeleteSelectionCommand::initializePositionData()
175{
justing113aaf32007-01-25 01:00:36 +0000176 Position start, end;
177 initializeStartEnd(start, end);
178
179 m_upstreamStart = start.upstream();
180 m_downstreamStart = start.downstream();
181 m_upstreamEnd = end.upstream();
182 m_downstreamEnd = end.downstream();
harrisondec5e092006-01-25 21:05:02 +0000183
justing06a653c2007-04-04 20:49:52 +0000184 m_startRoot = editableRootForPosition(start);
185 m_endRoot = editableRootForPosition(end);
186
justin.garcia@apple.comdeed90d2007-12-13 21:32:12 +0000187 m_startTableRow = enclosingNodeOfType(start, &isTableRow);
188 m_endTableRow = enclosingNodeOfType(end, &isTableRow);
justing187d2922007-07-17 21:05:21 +0000189
justingb7701502007-03-29 22:34:28 +0000190 // Don't move content out of a table cell.
justin.garcia@apple.comab87ef82008-05-29 20:37:53 +0000191 // If the cell is non-editable, enclosingNodeOfType won't return it by default, so
192 // tell that function that we don't care if it returns non-editable nodes.
rniwa@webkit.org45ecbaf2011-03-11 00:52:53 +0000193 Node* startCell = enclosingNodeOfType(m_upstreamStart, &isTableCell, CanCrossEditingBoundary);
194 Node* endCell = enclosingNodeOfType(m_downstreamEnd, &isTableCell, CanCrossEditingBoundary);
justing8f16cee2006-06-22 00:20:29 +0000195 // FIXME: This isn't right. A borderless table with two rows and a single column would appear as two paragraphs.
justingb7701502007-03-29 22:34:28 +0000196 if (endCell && endCell != startCell)
justing8f16cee2006-06-22 00:20:29 +0000197 m_mergeBlocksAfterDelete = false;
198
justing11ec52e2006-06-07 00:09:37 +0000199 // Usually the start and the end of the selection to delete are pulled together as a result of the deletion.
justing93798f12006-06-08 04:04:38 +0000200 // Sometimes they aren't (like when no merge is requested), so we must choose one position to hold the caret
201 // and receive the placeholder after deletion.
justing11ec52e2006-06-07 00:09:37 +0000202 VisiblePosition visibleEnd(m_downstreamEnd);
203 if (m_mergeBlocksAfterDelete && !isEndOfParagraph(visibleEnd))
204 m_endingPosition = m_downstreamEnd;
205 else
206 m_endingPosition = m_downstreamStart;
justin.garcia@apple.com7e95c7b2008-11-12 20:08:06 +0000207
208 // We don't want to merge into a block if it will mean changing the quote level of content after deleting
209 // selections that contain a whole number paragraphs plus a line break, since it is unclear to most users
210 // that such a selection actually ends at the start of the next paragraph. This matches TextEdit behavior
211 // for indented paragraphs.
adele@apple.come2bc16b2009-04-28 22:17:29 +0000212 // Only apply this rule if the endingSelection is a range selection. If it is a caret, then other operations have created
213 // the selection we're deleting (like the process of creating a selection to delete during a backspace), and the user isn't in the situation described above.
214 if (numEnclosingMailBlockquotes(start) != numEnclosingMailBlockquotes(end)
215 && isStartOfParagraph(visibleEnd) && isStartOfParagraph(VisiblePosition(start))
216 && endingSelection().isRange()) {
justin.garcia@apple.com7e95c7b2008-11-12 20:08:06 +0000217 m_mergeBlocksAfterDelete = false;
218 m_pruneStartBlockIfNecessary = true;
219 }
justing8f16cee2006-06-22 00:20:29 +0000220
mjsa0fe4042005-05-13 08:37:15 +0000221 // Handle leading and trailing whitespace, as well as smart delete adjustments to the selection
darin9aa45a42006-01-15 23:32:02 +0000222 m_leadingWhitespace = m_upstreamStart.leadingWhitespacePosition(m_selectionToDelete.affinity());
mjsa0fe4042005-05-13 08:37:15 +0000223 m_trailingWhitespace = m_downstreamEnd.trailingWhitespacePosition(VP_DEFAULT_AFFINITY);
224
225 if (m_smartDelete) {
226
227 // skip smart delete if the selection to delete already starts or ends with whitespace
darin9aa45a42006-01-15 23:32:02 +0000228 Position pos = VisiblePosition(m_upstreamStart, m_selectionToDelete.affinity()).deepEquivalent();
mjsa0fe4042005-05-13 08:37:15 +0000229 bool skipSmartDelete = pos.trailingWhitespacePosition(VP_DEFAULT_AFFINITY, true).isNotNull();
230 if (!skipSmartDelete)
231 skipSmartDelete = m_downstreamEnd.leadingWhitespacePosition(VP_DEFAULT_AFFINITY, true).isNotNull();
232
233 // extend selection upstream if there is whitespace there
darin9aa45a42006-01-15 23:32:02 +0000234 bool hasLeadingWhitespaceBeforeAdjustment = m_upstreamStart.leadingWhitespacePosition(m_selectionToDelete.affinity(), true).isNotNull();
mjsa0fe4042005-05-13 08:37:15 +0000235 if (!skipSmartDelete && hasLeadingWhitespaceBeforeAdjustment) {
harrisondec5e092006-01-25 21:05:02 +0000236 VisiblePosition visiblePos = VisiblePosition(m_upstreamStart, VP_DEFAULT_AFFINITY).previous();
mjsa0fe4042005-05-13 08:37:15 +0000237 pos = visiblePos.deepEquivalent();
238 // Expand out one character upstream for smart delete and recalculate
239 // positions based on this change.
240 m_upstreamStart = pos.upstream();
241 m_downstreamStart = pos.downstream();
242 m_leadingWhitespace = m_upstreamStart.leadingWhitespacePosition(visiblePos.affinity());
ojan@chromium.org77dae9d2010-03-09 23:41:38 +0000243
244 setStartingSelectionOnSmartDelete(m_upstreamStart, m_upstreamEnd);
mjsa0fe4042005-05-13 08:37:15 +0000245 }
246
247 // trailing whitespace is only considered for smart delete if there is no leading
248 // whitespace, as in the case where you double-click the first word of a paragraph.
249 if (!skipSmartDelete && !hasLeadingWhitespaceBeforeAdjustment && m_downstreamEnd.trailingWhitespacePosition(VP_DEFAULT_AFFINITY, true).isNotNull()) {
250 // Expand out one character downstream for smart delete and recalculate
251 // positions based on this change.
harrisondec5e092006-01-25 21:05:02 +0000252 pos = VisiblePosition(m_downstreamEnd, VP_DEFAULT_AFFINITY).next().deepEquivalent();
mjsa0fe4042005-05-13 08:37:15 +0000253 m_upstreamEnd = pos.upstream();
254 m_downstreamEnd = pos.downstream();
255 m_trailingWhitespace = m_downstreamEnd.trailingWhitespacePosition(VP_DEFAULT_AFFINITY);
ojan@chromium.org77dae9d2010-03-09 23:41:38 +0000256
257 setStartingSelectionOnSmartDelete(m_downstreamStart, m_downstreamEnd);
mjsa0fe4042005-05-13 08:37:15 +0000258 }
259 }
260
commit-queue@webkit.orgc56f5de2011-01-19 09:32:09 +0000261 // We must pass call parentAnchoredEquivalent on the positions since some editing positions
justin.garcia@apple.come1da2732008-06-25 21:22:12 +0000262 // that appear inside their nodes aren't really inside them. [hr, 0] is one example.
commit-queue@webkit.orgc56f5de2011-01-19 09:32:09 +0000263 // FIXME: parentAnchoredEquivalent should eventually be moved into enclosing element getters
justin.garcia@apple.come1da2732008-06-25 21:22:12 +0000264 // like the one below, since editing functions should obviously accept editing positions.
265 // FIXME: Passing false to enclosingNodeOfType tells it that it's OK to return a non-editable
266 // node. This was done to match existing behavior, but it seems wrong.
rniwa@webkit.org45ecbaf2011-03-11 00:52:53 +0000267 m_startBlock = enclosingNodeOfType(m_downstreamStart.parentAnchoredEquivalent(), &isBlock, CanCrossEditingBoundary);
268 m_endBlock = enclosingNodeOfType(m_upstreamEnd.parentAnchoredEquivalent(), &isBlock, CanCrossEditingBoundary);
mjsa0fe4042005-05-13 08:37:15 +0000269}
270
271void DeleteSelectionCommand::saveTypingStyleState()
272{
justin.garcia@apple.com64125512008-02-20 17:48:44 +0000273 // A common case is deleting characters that are all from the same text node. In
274 // that case, the style at the start of the selection before deletion will be the
275 // same as the style at the start of the selection after deletion (since those
276 // two positions will be identical). Therefore there is no need to save the
277 // typing style at the start of the selection, nor is there a reason to
278 // compute the style at the start of the selection after deletion (see the
279 // early return in calculateTypingStyleAfterDelete).
rniwa@webkit.org62b16972011-02-21 09:28:48 +0000280 if (m_upstreamStart.deprecatedNode() == m_downstreamEnd.deprecatedNode() && m_upstreamStart.deprecatedNode()->isTextNode())
justin.garcia@apple.com64125512008-02-20 17:48:44 +0000281 return;
rniwa@webkit.org1de555e2009-08-07 21:36:24 +0000282
mjsa0fe4042005-05-13 08:37:15 +0000283 // Figure out the typing style in effect before the delete is done.
rniwa@webkit.org7e06f4a2010-11-06 21:19:59 +0000284 m_typingStyle = EditingStyle::create(positionBeforeTabSpan(m_selectionToDelete.start()));
rniwa@webkit.org0d96cf62010-11-08 20:15:38 +0000285 m_typingStyle->removeStyleAddedByNode(enclosingAnchorElement(m_selectionToDelete.start()));
rniwa@webkit.org1de555e2009-08-07 21:36:24 +0000286
justing503ccf12005-07-29 22:50:47 +0000287 // If we're deleting into a Mail blockquote, save the style at end() instead of start()
288 // We'll use this later in computeTypingStyleAfterDelete if we end up outside of a Mail blockquote
rniwa@webkit.org24bd1d32011-03-17 00:03:22 +0000289 if (enclosingNodeOfType(m_selectionToDelete.start(), isMailBlockquote))
rniwa@webkit.org7e06f4a2010-11-06 21:19:59 +0000290 m_deleteIntoBlockquoteStyle = EditingStyle::create(m_selectionToDelete.end());
rniwa@webkit.org1de555e2009-08-07 21:36:24 +0000291 else
justing503ccf12005-07-29 22:50:47 +0000292 m_deleteIntoBlockquoteStyle = 0;
mjsa0fe4042005-05-13 08:37:15 +0000293}
294
295bool DeleteSelectionCommand::handleSpecialCaseBRDelete()
296{
297 // Check for special-case where the selection contains only a BR on a line by itself after another BR.
rniwa@webkit.org62b16972011-02-21 09:28:48 +0000298 bool upstreamStartIsBR = m_upstreamStart.deprecatedNode()->hasTagName(brTag);
299 bool downstreamStartIsBR = m_downstreamStart.deprecatedNode()->hasTagName(brTag);
300 bool isBROnLineByItself = upstreamStartIsBR && downstreamStartIsBR && m_downstreamStart.deprecatedNode() == m_upstreamEnd.deprecatedNode();
mjsa0fe4042005-05-13 08:37:15 +0000301 if (isBROnLineByItself) {
rniwa@webkit.org62b16972011-02-21 09:28:48 +0000302 removeNode(m_downstreamStart.deprecatedNode());
mjsa0fe4042005-05-13 08:37:15 +0000303 return true;
304 }
305
306 // Not a special-case delete per se, but we can detect that the merging of content between blocks
307 // should not be done.
harrison581c07b2006-11-06 18:08:52 +0000308 if (upstreamStartIsBR && downstreamStartIsBR) {
mitz@apple.comfc292c32009-05-03 04:09:41 +0000309 m_startsAtEmptyLine = true;
harrison581c07b2006-11-06 18:08:52 +0000310 m_endingPosition = m_downstreamEnd;
311 }
312
mjsa0fe4042005-05-13 08:37:15 +0000313 return false;
314}
315
justingf81641c2006-06-14 23:23:50 +0000316static void updatePositionForNodeRemoval(Node* node, Position& position)
317{
318 if (position.isNull())
319 return;
rniwa@webkit.org186f5bb2011-01-18 21:33:34 +0000320 switch (position.anchorType()) {
321 case Position::PositionIsOffsetInAnchor:
322 if (position.containerNode() == node->parentNode() && static_cast<unsigned>(position.offsetInContainerNode()) > node->nodeIndex())
323 position.moveToOffset(position.offsetInContainerNode() - 1);
324 else if (node->contains(position.containerNode()))
325 position = positionInParentBeforeNode(node);
326 break;
327 case Position::PositionIsAfterAnchor:
328 if (node->contains(position.anchorNode()))
329 position = positionInParentAfterNode(node);
330 break;
331 case Position::PositionIsBeforeAnchor:
332 if (node->contains(position.anchorNode()))
333 position = positionInParentBeforeNode(node);
334 break;
335 }
justingf81641c2006-06-14 23:23:50 +0000336}
337
darin@apple.come95b53d2008-12-23 21:42:46 +0000338void DeleteSelectionCommand::removeNode(PassRefPtr<Node> node)
justing11ec52e2006-06-07 00:09:37 +0000339{
justing06a653c2007-04-04 20:49:52 +0000340 if (!node)
341 return;
342
343 if (m_startRoot != m_endRoot && !(node->isDescendantOf(m_startRoot.get()) && node->isDescendantOf(m_endRoot.get()))) {
344 // If a node is not in both the start and end editable roots, remove it only if its inside an editable region.
commit-queue@webkit.org595681f2011-03-25 16:21:30 +0000345 if (!node->parentNode()->rendererIsEditable()) {
justing06a653c2007-04-04 20:49:52 +0000346 // Don't remove non-editable atomic nodes.
347 if (!node->firstChild())
348 return;
349 // Search this non-editable region for editable regions to empty.
350 RefPtr<Node> child = node->firstChild();
351 while (child) {
352 RefPtr<Node> nextChild = child->nextSibling();
353 removeNode(child.get());
354 // Bail if nextChild is no longer node's child.
355 if (nextChild && nextChild->parentNode() != node)
356 return;
357 child = nextChild;
358 }
359
360 // Don't remove editable regions that are inside non-editable ones, just clear them.
361 return;
362 }
363 }
364
darin@apple.come95b53d2008-12-23 21:42:46 +0000365 if (isTableStructureNode(node.get()) || node == node->rootEditableElement()) {
justing11ec52e2006-06-07 00:09:37 +0000366 // Do not remove an element of table structure; remove its contents.
367 // Likewise for the root editable element.
darin@apple.come95b53d2008-12-23 21:42:46 +0000368 Node* child = node->firstChild();
justing11ec52e2006-06-07 00:09:37 +0000369 while (child) {
darin@apple.come95b53d2008-12-23 21:42:46 +0000370 Node* remove = child;
justing11ec52e2006-06-07 00:09:37 +0000371 child = child->nextSibling();
justingf81641c2006-06-14 23:23:50 +0000372 removeNode(remove);
justing11ec52e2006-06-07 00:09:37 +0000373 }
374
375 // make sure empty cell has some height
376 updateLayout();
377 RenderObject *r = node->renderer();
darin@apple.com445c34f2009-08-01 00:40:58 +0000378 if (r && r->isTableCell() && toRenderTableCell(r)->contentHeight() <= 0)
rniwa@webkit.org186f5bb2011-01-18 21:33:34 +0000379 insertBlockPlaceholder(firstPositionInNode(node.get()));
justing11ec52e2006-06-07 00:09:37 +0000380 return;
381 }
382
leviw@chromium.org1f953622011-03-15 19:36:51 +0000383 if (node == m_startBlock && !isEndOfBlock(VisiblePosition(firstPositionInNode(m_startBlock.get())).previous()))
lweintraub635ec2a2006-07-13 18:28:28 +0000384 m_needPlaceholder = true;
leviw@chromium.org1f953622011-03-15 19:36:51 +0000385 else if (node == m_endBlock && !isStartOfBlock(VisiblePosition(lastPositionInNode(m_startBlock.get())).next()))
lweintraub635ec2a2006-07-13 18:28:28 +0000386 m_needPlaceholder = true;
387
justingf81641c2006-06-14 23:23:50 +0000388 // FIXME: Update the endpoints of the range being deleted.
darin@apple.come95b53d2008-12-23 21:42:46 +0000389 updatePositionForNodeRemoval(node.get(), m_endingPosition);
390 updatePositionForNodeRemoval(node.get(), m_leadingWhitespace);
391 updatePositionForNodeRemoval(node.get(), m_trailingWhitespace);
justing11ec52e2006-06-07 00:09:37 +0000392
justingf81641c2006-06-14 23:23:50 +0000393 CompositeEditCommand::removeNode(node);
394}
395
andersca@apple.comd8dda092009-01-23 19:52:19 +0000396static void updatePositionForTextRemoval(Node* node, int offset, int count, Position& position)
justingf81641c2006-06-14 23:23:50 +0000397{
rniwa@webkit.org186f5bb2011-01-18 21:33:34 +0000398 if (position.anchorType() != Position::PositionIsOffsetInAnchor || position.containerNode() != node)
399 return;
400
401 if (position.offsetInContainerNode() > offset + count)
402 position.moveToOffset(position.offsetInContainerNode() - count);
403 else if (position.offsetInContainerNode() > offset)
404 position.moveToOffset(offset);
justing11ec52e2006-06-07 00:09:37 +0000405}
406
darin@apple.come95b53d2008-12-23 21:42:46 +0000407void DeleteSelectionCommand::deleteTextFromNode(PassRefPtr<Text> node, unsigned offset, unsigned count)
justing11ec52e2006-06-07 00:09:37 +0000408{
justingf81641c2006-06-14 23:23:50 +0000409 // FIXME: Update the endpoints of the range being deleted.
darin@apple.come95b53d2008-12-23 21:42:46 +0000410 updatePositionForTextRemoval(node.get(), offset, count, m_endingPosition);
411 updatePositionForTextRemoval(node.get(), offset, count, m_leadingWhitespace);
412 updatePositionForTextRemoval(node.get(), offset, count, m_trailingWhitespace);
justin.garcia@apple.com68510a52009-04-23 00:54:42 +0000413 updatePositionForTextRemoval(node.get(), offset, count, m_downstreamEnd);
justing11ec52e2006-06-07 00:09:37 +0000414
415 CompositeEditCommand::deleteTextFromNode(node, offset, count);
416}
417
mjsa0fe4042005-05-13 08:37:15 +0000418void DeleteSelectionCommand::handleGeneralDelete()
419{
eric@webkit.org4e32ebc2009-04-30 01:09:57 +0000420 int startOffset = m_upstreamStart.deprecatedEditingOffset();
rniwa@webkit.org62b16972011-02-21 09:28:48 +0000421 Node* startNode = m_upstreamStart.deprecatedNode();
lweintraub635ec2a2006-07-13 18:28:28 +0000422
423 // Never remove the start block unless it's a table, in which case we won't merge content in.
424 if (startNode == m_startBlock && startOffset == 0 && canHaveChildrenForEditing(startNode) && !startNode->hasTagName(tableTag)) {
mjsa0fe4042005-05-13 08:37:15 +0000425 startOffset = 0;
justing11ec52e2006-06-07 00:09:37 +0000426 startNode = startNode->traverseNextNode();
justinge6f80c12006-06-09 04:57:51 +0000427 }
428
ap@webkit.orgff51b522007-11-11 20:28:51 +0000429 if (startOffset >= caretMaxOffset(startNode) && startNode->isTextNode()) {
justinge6f80c12006-06-09 04:57:51 +0000430 Text *text = static_cast<Text *>(startNode);
ap@webkit.orgff51b522007-11-11 20:28:51 +0000431 if (text->length() > (unsigned)caretMaxOffset(startNode))
432 deleteTextFromNode(text, caretMaxOffset(startNode), text->length() - caretMaxOffset(startNode));
justinge6f80c12006-06-09 04:57:51 +0000433 }
434
eric@webkit.orgf3a18812009-03-20 18:45:15 +0000435 if (startOffset >= lastOffsetForEditing(startNode)) {
justinge6f80c12006-06-09 04:57:51 +0000436 startNode = startNode->traverseNextSibling();
mjsa0fe4042005-05-13 08:37:15 +0000437 startOffset = 0;
438 }
439
440 // Done adjusting the start. See if we're all done.
justing11ec52e2006-06-07 00:09:37 +0000441 if (!startNode)
mjsa0fe4042005-05-13 08:37:15 +0000442 return;
443
rniwa@webkit.org62b16972011-02-21 09:28:48 +0000444 if (startNode == m_downstreamEnd.deprecatedNode()) {
commit-queue@webkit.org2aa8d032010-09-22 20:43:26 +0000445 if (m_downstreamEnd.deprecatedEditingOffset() - startOffset > 0) {
justing11ec52e2006-06-07 00:09:37 +0000446 if (startNode->isTextNode()) {
mjsa0fe4042005-05-13 08:37:15 +0000447 // in a text node that needs to be trimmed
eric@webkit.orgf3a18812009-03-20 18:45:15 +0000448 Text* text = static_cast<Text*>(startNode);
eric@webkit.org4e32ebc2009-04-30 01:09:57 +0000449 deleteTextFromNode(text, startOffset, m_downstreamEnd.deprecatedEditingOffset() - startOffset);
mjsa0fe4042005-05-13 08:37:15 +0000450 } else {
eric@webkit.org4e32ebc2009-04-30 01:09:57 +0000451 removeChildrenInRange(startNode, startOffset, m_downstreamEnd.deprecatedEditingOffset());
mjsa0fe4042005-05-13 08:37:15 +0000452 m_endingPosition = m_upstreamStart;
453 }
454 }
commit-queue@webkit.org2aa8d032010-09-22 20:43:26 +0000455
456 // The selection to delete is all in one node.
457 if (!startNode->renderer() || (!startOffset && m_downstreamEnd.atLastEditingPositionForNode()))
458 removeNode(startNode);
mjsa0fe4042005-05-13 08:37:15 +0000459 }
460 else {
rniwa@webkit.org62b16972011-02-21 09:28:48 +0000461 bool startNodeWasDescendantOfEndNode = m_upstreamStart.deprecatedNode()->isDescendantOf(m_downstreamEnd.deprecatedNode());
mjsa0fe4042005-05-13 08:37:15 +0000462 // The selection to delete spans more than one node.
justingd197d6b2007-08-25 01:20:33 +0000463 RefPtr<Node> node(startNode);
mjsa0fe4042005-05-13 08:37:15 +0000464
465 if (startOffset > 0) {
justing11ec52e2006-06-07 00:09:37 +0000466 if (startNode->isTextNode()) {
mjsa0fe4042005-05-13 08:37:15 +0000467 // in a text node that needs to be trimmed
justing4855c4f2007-08-23 18:11:04 +0000468 Text *text = static_cast<Text *>(node.get());
mjsa0fe4042005-05-13 08:37:15 +0000469 deleteTextFromNode(text, startOffset, text->length() - startOffset);
470 node = node->traverseNextNode();
471 } else {
justing11ec52e2006-06-07 00:09:37 +0000472 node = startNode->childNode(startOffset);
mjsa0fe4042005-05-13 08:37:15 +0000473 }
rniwa@webkit.org62b16972011-02-21 09:28:48 +0000474 } else if (startNode == m_upstreamEnd.deprecatedNode() && startNode->isTextNode()) {
475 Text* text = static_cast<Text*>(m_upstreamEnd.deprecatedNode());
commit-queue@webkit.org2aa8d032010-09-22 20:43:26 +0000476 deleteTextFromNode(text, 0, m_upstreamEnd.deprecatedEditingOffset());
mjsa0fe4042005-05-13 08:37:15 +0000477 }
478
479 // handle deleting all nodes that are completely selected
rniwa@webkit.org62b16972011-02-21 09:28:48 +0000480 while (node && node != m_downstreamEnd.deprecatedNode()) {
rniwa@webkit.org186f5bb2011-01-18 21:33:34 +0000481 if (comparePositions(firstPositionInOrBeforeNode(node.get()), m_downstreamEnd) >= 0) {
mjsa0fe4042005-05-13 08:37:15 +0000482 // traverseNextSibling just blew past the end position, so stop deleting
483 node = 0;
rniwa@webkit.org62b16972011-02-21 09:28:48 +0000484 } else if (!m_downstreamEnd.deprecatedNode()->isDescendantOf(node.get())) {
justing24754322007-07-09 20:27:05 +0000485 RefPtr<Node> nextNode = node->traverseNextSibling();
mjsa0fe4042005-05-13 08:37:15 +0000486 // if we just removed a node from the end container, update end position so the
487 // check above will work
rniwa@webkit.org62b16972011-02-21 09:28:48 +0000488 if (node->parentNode() == m_downstreamEnd.deprecatedNode()) {
rniwa@webkit.org186f5bb2011-01-18 21:33:34 +0000489 ASSERT(m_downstreamEnd.deprecatedEditingOffset());
eric@webkit.org4e32ebc2009-04-30 01:09:57 +0000490 ASSERT(node->nodeIndex() < (unsigned)m_downstreamEnd.deprecatedEditingOffset());
rniwa@webkit.org186f5bb2011-01-18 21:33:34 +0000491 m_downstreamEnd.moveToOffset(m_downstreamEnd.deprecatedEditingOffset() - 1);
mjsa0fe4042005-05-13 08:37:15 +0000492 }
justing4855c4f2007-08-23 18:11:04 +0000493 removeNode(node.get());
justing24754322007-07-09 20:27:05 +0000494 node = nextNode.get();
mjsa0fe4042005-05-13 08:37:15 +0000495 } else {
justing11ec52e2006-06-07 00:09:37 +0000496 Node* n = node->lastDescendant();
rniwa@webkit.org62b16972011-02-21 09:28:48 +0000497 if (m_downstreamEnd.deprecatedNode() == n && m_downstreamEnd.deprecatedEditingOffset() >= caretMaxOffset(n)) {
justing4855c4f2007-08-23 18:11:04 +0000498 removeNode(node.get());
mjsa0fe4042005-05-13 08:37:15 +0000499 node = 0;
justing95561262006-06-21 21:22:50 +0000500 } else
mjsa0fe4042005-05-13 08:37:15 +0000501 node = node->traverseNextNode();
mjsa0fe4042005-05-13 08:37:15 +0000502 }
503 }
mjsa0fe4042005-05-13 08:37:15 +0000504
rniwa@webkit.org62b16972011-02-21 09:28:48 +0000505 if (m_downstreamEnd.deprecatedNode() != startNode && !m_upstreamStart.deprecatedNode()->isDescendantOf(m_downstreamEnd.deprecatedNode()) && m_downstreamEnd.anchorNode()->inDocument() && m_downstreamEnd.deprecatedEditingOffset() >= caretMinOffset(m_downstreamEnd.deprecatedNode())) {
506 if (m_downstreamEnd.atLastEditingPositionForNode() && !canHaveChildrenForEditing(m_downstreamEnd.deprecatedNode())) {
justing395a18e2007-06-26 06:52:15 +0000507 // The node itself is fully selected, not just its contents. Delete it.
rniwa@webkit.org62b16972011-02-21 09:28:48 +0000508 removeNode(m_downstreamEnd.deprecatedNode());
mjsa0fe4042005-05-13 08:37:15 +0000509 } else {
rniwa@webkit.org62b16972011-02-21 09:28:48 +0000510 if (m_downstreamEnd.deprecatedNode()->isTextNode()) {
mjsa0fe4042005-05-13 08:37:15 +0000511 // in a text node that needs to be trimmed
rniwa@webkit.org62b16972011-02-21 09:28:48 +0000512 Text* text = static_cast<Text*>(m_downstreamEnd.deprecatedNode());
eric@webkit.org4e32ebc2009-04-30 01:09:57 +0000513 if (m_downstreamEnd.deprecatedEditingOffset() > 0) {
514 deleteTextFromNode(text, 0, m_downstreamEnd.deprecatedEditingOffset());
mjsa0fe4042005-05-13 08:37:15 +0000515 }
rniwa@webkit.org62b16972011-02-21 09:28:48 +0000516 // Remove children of m_downstreamEnd.deprecatedNode() that come after m_upstreamStart.
517 // Don't try to remove children if m_upstreamStart was inside m_downstreamEnd.deprecatedNode()
justingd197d6b2007-08-25 01:20:33 +0000518 // and m_upstreamStart has been removed from the document, because then we don't
519 // know how many children to remove.
520 // FIXME: Make m_upstreamStart a position we update as we remove content, then we can
521 // always know which children to remove.
rniwa@webkit.org62b16972011-02-21 09:28:48 +0000522 } else if (!(startNodeWasDescendantOfEndNode && !m_upstreamStart.anchorNode()->inDocument())) {
mjsa0fe4042005-05-13 08:37:15 +0000523 int offset = 0;
rniwa@webkit.org62b16972011-02-21 09:28:48 +0000524 if (m_upstreamStart.deprecatedNode()->isDescendantOf(m_downstreamEnd.deprecatedNode())) {
525 Node* n = m_upstreamStart.deprecatedNode();
526 while (n && n->parentNode() != m_downstreamEnd.deprecatedNode())
mjsa0fe4042005-05-13 08:37:15 +0000527 n = n->parentNode();
528 if (n)
529 offset = n->nodeIndex() + 1;
530 }
rniwa@webkit.org62b16972011-02-21 09:28:48 +0000531 removeChildrenInRange(m_downstreamEnd.deprecatedNode(), offset, m_downstreamEnd.deprecatedEditingOffset());
rniwa@webkit.org186f5bb2011-01-18 21:33:34 +0000532 m_downstreamEnd.moveToOffset(offset);
mjsa0fe4042005-05-13 08:37:15 +0000533 }
534 }
535 }
536 }
537}
538
mjsa0fe4042005-05-13 08:37:15 +0000539void DeleteSelectionCommand::fixupWhitespace()
540{
darin26f5b362005-12-22 04:11:39 +0000541 updateLayout();
justingf81641c2006-06-14 23:23:50 +0000542 // FIXME: isRenderedCharacter should be removed, and we should use VisiblePosition::characterAfter and VisiblePosition::characterBefore
rniwa@webkit.org62b16972011-02-21 09:28:48 +0000543 if (m_leadingWhitespace.isNotNull() && !m_leadingWhitespace.isRenderedCharacter() && m_leadingWhitespace.deprecatedNode()->isTextNode()) {
544 Text* textNode = static_cast<Text*>(m_leadingWhitespace.deprecatedNode());
darin38ae5342006-07-25 04:39:25 +0000545 ASSERT(!textNode->renderer() || textNode->renderer()->style()->collapseWhiteSpace());
eric@webkit.org4e32ebc2009-04-30 01:09:57 +0000546 replaceTextInNode(textNode, m_leadingWhitespace.deprecatedEditingOffset(), 1, nonBreakingSpaceString());
mjsa0fe4042005-05-13 08:37:15 +0000547 }
rniwa@webkit.org62b16972011-02-21 09:28:48 +0000548 if (m_trailingWhitespace.isNotNull() && !m_trailingWhitespace.isRenderedCharacter() && m_trailingWhitespace.deprecatedNode()->isTextNode()) {
549 Text* textNode = static_cast<Text*>(m_trailingWhitespace.deprecatedNode());
darin38ae5342006-07-25 04:39:25 +0000550 ASSERT(!textNode->renderer() ||textNode->renderer()->style()->collapseWhiteSpace());
eric@webkit.org4e32ebc2009-04-30 01:09:57 +0000551 replaceTextInNode(textNode, m_trailingWhitespace.deprecatedEditingOffset(), 1, nonBreakingSpaceString());
mjsa0fe4042005-05-13 08:37:15 +0000552 }
553}
554
justingf81641c2006-06-14 23:23:50 +0000555// If a selection starts in one block and ends in another, we have to merge to bring content before the
556// start together with content after the end.
justing217ded82006-04-04 20:52:10 +0000557void DeleteSelectionCommand::mergeParagraphs()
mjsa0fe4042005-05-13 08:37:15 +0000558{
justin.garcia@apple.com7e95c7b2008-11-12 20:08:06 +0000559 if (!m_mergeBlocksAfterDelete) {
560 if (m_pruneStartBlockIfNecessary) {
justin.garcia@apple.com7e95c7b2008-11-12 20:08:06 +0000561 // We aren't going to merge into the start block, so remove it if it's empty.
mitz@apple.comafdb80f2009-05-06 08:02:27 +0000562 prune(m_startBlock);
justin.garcia@apple.com7e95c7b2008-11-12 20:08:06 +0000563 // Removing the start block during a deletion is usually an indication that we need
564 // a placeholder, but not in this case.
565 m_needPlaceholder = false;
566 }
mjsa0fe4042005-05-13 08:37:15 +0000567 return;
justin.garcia@apple.com7e95c7b2008-11-12 20:08:06 +0000568 }
569
570 // It shouldn't have been asked to both try and merge content into the start block and prune it.
571 ASSERT(!m_pruneStartBlockIfNecessary);
mjsa0fe4042005-05-13 08:37:15 +0000572
justing217ded82006-04-04 20:52:10 +0000573 // FIXME: Deletion should adjust selection endpoints as it removes nodes so that we never get into this state (4099839).
rniwa@webkit.org62b16972011-02-21 09:28:48 +0000574 if (!m_downstreamEnd.anchorNode()->inDocument() || !m_upstreamStart.anchorNode()->inDocument())
justing217ded82006-04-04 20:52:10 +0000575 return;
576
justingced67722006-05-01 21:43:03 +0000577 // FIXME: The deletion algorithm shouldn't let this happen.
eric@webkit.org05d6e932009-06-02 21:31:08 +0000578 if (comparePositions(m_upstreamStart, m_downstreamEnd) > 0)
justingced67722006-05-01 21:43:03 +0000579 return;
580
justin.garcia@apple.com68510a52009-04-23 00:54:42 +0000581 // There's nothing to merge.
582 if (m_upstreamStart == m_downstreamEnd)
justingced67722006-05-01 21:43:03 +0000583 return;
584
justing217ded82006-04-04 20:52:10 +0000585 VisiblePosition startOfParagraphToMove(m_downstreamEnd);
586 VisiblePosition mergeDestination(m_upstreamStart);
587
justin.garcia@apple.com60777c92007-11-14 22:33:07 +0000588 // m_downstreamEnd's block has been emptied out by deletion. There is no content inside of it to
589 // move, so just remove it.
rniwa@webkit.org62b16972011-02-21 09:28:48 +0000590 Element* endBlock = static_cast<Element*>(enclosingBlock(m_downstreamEnd.deprecatedNode()));
591 if (!startOfParagraphToMove.deepEquivalent().deprecatedNode() || !endBlock->contains(startOfParagraphToMove.deepEquivalent().deprecatedNode())) {
592 removeNode(enclosingBlock(m_downstreamEnd.deprecatedNode()));
justin.garcia@apple.com60777c92007-11-14 22:33:07 +0000593 return;
594 }
595
justingced67722006-05-01 21:43:03 +0000596 // We need to merge into m_upstreamStart's block, but it's been emptied out and collapsed by deletion.
leviw@chromium.org688f6412011-03-14 21:19:29 +0000597 if (!mergeDestination.deepEquivalent().deprecatedNode() || !mergeDestination.deepEquivalent().deprecatedNode()->isDescendantOf(enclosingBlock(m_upstreamStart.containerNode())) || m_startsAtEmptyLine) {
justing5097f5d52007-04-20 00:08:50 +0000598 insertNodeAt(createBreakElement(document()).get(), m_upstreamStart);
justingced67722006-05-01 21:43:03 +0000599 mergeDestination = VisiblePosition(m_upstreamStart);
600 }
harrisondec5e092006-01-25 21:05:02 +0000601
justingced67722006-05-01 21:43:03 +0000602 if (mergeDestination == startOfParagraphToMove)
mjsa0fe4042005-05-13 08:37:15 +0000603 return;
justing217ded82006-04-04 20:52:10 +0000604
605 VisiblePosition endOfParagraphToMove = endOfParagraph(startOfParagraphToMove);
606
darin2b0ff522006-05-03 19:32:21 +0000607 if (mergeDestination == endOfParagraphToMove)
608 return;
mjsa0fe4042005-05-13 08:37:15 +0000609
justinge809f932006-06-23 22:36:12 +0000610 // The rule for merging into an empty block is: only do so if its farther to the right.
611 // FIXME: Consider RTL.
mitz@apple.comfc292c32009-05-03 04:09:41 +0000612 if (!m_startsAtEmptyLine && isStartOfParagraph(mergeDestination) && startOfParagraphToMove.absoluteCaretBounds().x() > mergeDestination.absoluteCaretBounds().x()) {
rniwa@webkit.org62b16972011-02-21 09:28:48 +0000613 if (mergeDestination.deepEquivalent().downstream().deprecatedNode()->hasTagName(brTag)) {
614 removeNodeAndPruneAncestors(mergeDestination.deepEquivalent().downstream().deprecatedNode());
enrica@apple.com25b91e32010-01-11 18:04:58 +0000615 m_endingPosition = startOfParagraphToMove.deepEquivalent();
616 return;
617 }
justinge809f932006-06-23 22:36:12 +0000618 }
619
justin.garcia@apple.com619526a2009-11-05 05:25:24 +0000620 // Block images, tables and horizontal rules cannot be made inline with content at mergeDestination. If there is
621 // any (!isStartOfParagraph(mergeDestination)), don't merge, just move the caret to just before the selection we deleted.
622 // See https://bugs.webkit.org/show_bug.cgi?id=25439
rniwa@webkit.org62b16972011-02-21 09:28:48 +0000623 if (isRenderedAsNonInlineTableImageOrHR(startOfParagraphToMove.deepEquivalent().deprecatedNode()) && !isStartOfParagraph(mergeDestination)) {
justin.garcia@apple.com619526a2009-11-05 05:25:24 +0000624 m_endingPosition = m_upstreamStart;
625 return;
626 }
627
commit-queue@webkit.orgc56f5de2011-01-19 09:32:09 +0000628 RefPtr<Range> range = Range::create(document(), startOfParagraphToMove.deepEquivalent().parentAnchoredEquivalent(), endOfParagraphToMove.deepEquivalent().parentAnchoredEquivalent());
629 RefPtr<Range> rangeToBeReplaced = Range::create(document(), mergeDestination.deepEquivalent().parentAnchoredEquivalent(), mergeDestination.deepEquivalent().parentAnchoredEquivalent());
justingf90e28d2007-07-30 18:25:19 +0000630 if (!document()->frame()->editor()->client()->shouldMoveRangeAfterDelete(range.get(), rangeToBeReplaced.get()))
631 return;
632
justin.garcia@apple.com60777c92007-11-14 22:33:07 +0000633 // moveParagraphs will insert placeholders if it removes blocks that would require their use, don't let block
634 // removals that it does cause the insertion of *another* placeholder.
635 bool needPlaceholder = m_needPlaceholder;
rniwa@webkit.orgd34b7482010-08-12 02:05:11 +0000636 bool paragraphToMergeIsEmpty = (startOfParagraphToMove == endOfParagraphToMove);
637 moveParagraph(startOfParagraphToMove, endOfParagraphToMove, mergeDestination, false, !paragraphToMergeIsEmpty);
justin.garcia@apple.com60777c92007-11-14 22:33:07 +0000638 m_needPlaceholder = needPlaceholder;
justing93798f12006-06-08 04:04:38 +0000639 // The endingPosition was likely clobbered by the move, so recompute it (moveParagraph selects the moved paragraph).
justing11ec52e2006-06-07 00:09:37 +0000640 m_endingPosition = endingSelection().start();
mjsa0fe4042005-05-13 08:37:15 +0000641}
642
justing187d2922007-07-17 21:05:21 +0000643void DeleteSelectionCommand::removePreviouslySelectedEmptyTableRows()
644{
justin.garcia@apple.com99a05542007-12-13 06:56:28 +0000645 if (m_endTableRow && m_endTableRow->inDocument() && m_endTableRow != m_startTableRow) {
646 Node* row = m_endTableRow->previousSibling();
647 while (row && row != m_startTableRow) {
justing187d2922007-07-17 21:05:21 +0000648 RefPtr<Node> previousRow = row->previousSibling();
649 if (isTableRowEmpty(row))
650 // Use a raw removeNode, instead of DeleteSelectionCommand's, because
651 // that won't remove rows, it only empties them in preparation for this function.
652 CompositeEditCommand::removeNode(row);
653 row = previousRow.get();
654 }
655 }
656
justing7d4a4342007-10-13 00:40:48 +0000657 // Remove empty rows after the start row.
658 if (m_startTableRow && m_startTableRow->inDocument() && m_startTableRow != m_endTableRow) {
justing187d2922007-07-17 21:05:21 +0000659 Node* row = m_startTableRow->nextSibling();
justin.garcia@apple.com99a05542007-12-13 06:56:28 +0000660 while (row && row != m_endTableRow) {
justing187d2922007-07-17 21:05:21 +0000661 RefPtr<Node> nextRow = row->nextSibling();
662 if (isTableRowEmpty(row))
663 CompositeEditCommand::removeNode(row);
664 row = nextRow.get();
665 }
666 }
justin.garcia@apple.com99a05542007-12-13 06:56:28 +0000667
668 if (m_endTableRow && m_endTableRow->inDocument() && m_endTableRow != m_startTableRow)
669 if (isTableRowEmpty(m_endTableRow.get())) {
670 // Don't remove m_endTableRow if it's where we're putting the ending selection.
rniwa@webkit.org62b16972011-02-21 09:28:48 +0000671 if (!m_endingPosition.deprecatedNode()->isDescendantOf(m_endTableRow.get())) {
justin.garcia@apple.com99a05542007-12-13 06:56:28 +0000672 // FIXME: We probably shouldn't remove m_endTableRow unless it's fully selected, even if it is empty.
673 // We'll need to start adjusting the selection endpoints during deletion to know whether or not m_endTableRow
674 // was fully selected here.
675 CompositeEditCommand::removeNode(m_endTableRow.get());
676 }
677 }
justing187d2922007-07-17 21:05:21 +0000678}
679
justin.garcia@apple.comd1eac9a2008-06-20 00:24:13 +0000680void DeleteSelectionCommand::calculateTypingStyleAfterDelete()
mjsa0fe4042005-05-13 08:37:15 +0000681{
justin.garcia@apple.com64125512008-02-20 17:48:44 +0000682 if (!m_typingStyle)
683 return;
684
mjsa0fe4042005-05-13 08:37:15 +0000685 // Compute the difference between the style before the delete and the style now
mjs810e9762006-01-09 21:39:14 +0000686 // after the delete has been done. Set this style on the frame, so other editing
mjsa0fe4042005-05-13 08:37:15 +0000687 // commands being composed with this one will work, and also cache it on the command,
mjs810e9762006-01-09 21:39:14 +0000688 // so the Frame::appliedEditing can set it after the whole composite command
mjsa0fe4042005-05-13 08:37:15 +0000689 // has completed.
justing503ccf12005-07-29 22:50:47 +0000690
eseidelef508982006-01-03 09:19:17 +0000691 // If we deleted into a blockquote, but are now no longer in a blockquote, use the alternate typing style
rniwa@webkit.org24bd1d32011-03-17 00:03:22 +0000692 if (m_deleteIntoBlockquoteStyle && !enclosingNodeOfType(m_endingPosition, isMailBlockquote, CanCrossEditingBoundary))
eseidelef508982006-01-03 09:19:17 +0000693 m_typingStyle = m_deleteIntoBlockquoteStyle;
694 m_deleteIntoBlockquoteStyle = 0;
rniwa@webkit.org1de555e2009-08-07 21:36:24 +0000695
rniwa@webkit.org7e06f4a2010-11-06 21:19:59 +0000696 m_typingStyle->prepareToApplyAt(m_endingPosition);
697 if (m_typingStyle->isEmpty())
mjsa0fe4042005-05-13 08:37:15 +0000698 m_typingStyle = 0;
justin.garcia@apple.comd1eac9a2008-06-20 00:24:13 +0000699 VisiblePosition visibleEnd(m_endingPosition);
700 if (m_typingStyle &&
701 isStartOfParagraph(visibleEnd) &&
702 isEndOfParagraph(visibleEnd) &&
justin.garcia@apple.comd5c8f432009-04-14 18:48:16 +0000703 lineBreakExistsAtVisiblePosition(visibleEnd)) {
justin.garcia@apple.comd1eac9a2008-06-20 00:24:13 +0000704 // Apply style to the placeholder that is now holding open the empty paragraph.
705 // This makes sure that the paragraph has the right height, and that the paragraph
706 // takes on the right style and retains it even if you move the selection away and
707 // then move it back (which will clear typing style).
mjsa0fe4042005-05-13 08:37:15 +0000708
justin.garcia@apple.comd1eac9a2008-06-20 00:24:13 +0000709 setEndingSelection(visibleEnd);
rniwa@webkit.org5359dff2010-12-07 01:00:23 +0000710 applyStyle(m_typingStyle.get(), EditActionUnspecified);
justin.garcia@apple.comd1eac9a2008-06-20 00:24:13 +0000711 // applyStyle can destroy the placeholder that was at m_endingPosition if it needs to
712 // move it, but it will set an endingSelection() at [movedPlaceholder, 0] if it does so.
713 m_endingPosition = endingSelection().start();
mjsa0fe4042005-05-13 08:37:15 +0000714 m_typingStyle = 0;
715 }
justin.garcia@apple.comd1eac9a2008-06-20 00:24:13 +0000716 // This is where we've deleted all traces of a style but not a whole paragraph (that's handled above).
717 // In this case if we start typing, the new characters should have the same style as the just deleted ones,
718 // but, if we change the selection, come back and start typing that style should be lost. Also see
719 // preserveTypingStyle() below.
darin@apple.comb17ec2f2010-09-10 22:06:47 +0000720 document()->frame()->selection()->setTypingStyle(m_typingStyle);
mjsa0fe4042005-05-13 08:37:15 +0000721}
722
723void DeleteSelectionCommand::clearTransientState()
724{
eric@webkit.org87ea95c2009-02-09 21:43:24 +0000725 m_selectionToDelete = VisibleSelection();
mjsa0fe4042005-05-13 08:37:15 +0000726 m_upstreamStart.clear();
727 m_downstreamStart.clear();
728 m_upstreamEnd.clear();
729 m_downstreamEnd.clear();
730 m_endingPosition.clear();
731 m_leadingWhitespace.clear();
732 m_trailingWhitespace.clear();
mjsa0fe4042005-05-13 08:37:15 +0000733}
734
735void DeleteSelectionCommand::doApply()
736{
737 // If selection has not been set to a custom selection when the command was created,
738 // use the current ending selection.
739 if (!m_hasSelectionToDelete)
740 m_selectionToDelete = endingSelection();
rniwa@webkit.orgd39a1e72010-08-25 19:19:26 +0000741
742 if (!m_selectionToDelete.isNonOrphanedRange())
mjsa0fe4042005-05-13 08:37:15 +0000743 return;
744
adeleb2f2b662006-07-15 17:22:50 +0000745 // If the deletion is occurring in a text field, and we're not deleting to replace the selection, then let the frame call across the bridge to notify the form delegate.
746 if (!m_replace) {
rniwa@webkit.org62b16972011-02-21 09:28:48 +0000747 Node* startNode = m_selectionToDelete.start().deprecatedNode();
adeleb2f2b662006-07-15 17:22:50 +0000748 Node* ancestorNode = startNode ? startNode->shadowAncestorNode() : 0;
darin3f6601d2007-02-17 01:15:48 +0000749 if (ancestorNode && ancestorNode->hasTagName(inputTag)
750 && static_cast<HTMLInputElement*>(ancestorNode)->isTextField()
751 && ancestorNode->focused())
darin@apple.combecacd82010-09-09 23:08:54 +0000752 document()->frame()->editor()->textWillBeDeletedInTextField(static_cast<Element*>(ancestorNode));
adeleb2f2b662006-07-15 17:22:50 +0000753 }
adele174364d2006-04-13 01:50:04 +0000754
mjsa0fe4042005-05-13 08:37:15 +0000755 // save this to later make the selection with
darin9aa45a42006-01-15 23:32:02 +0000756 EAffinity affinity = m_selectionToDelete.affinity();
mjsa0fe4042005-05-13 08:37:15 +0000757
justing11ec52e2006-06-07 00:09:37 +0000758 Position downstreamEnd = m_selectionToDelete.end().downstream();
commit-queue@webkit.orgf3628722010-11-28 02:54:43 +0000759 m_needPlaceholder = isStartOfParagraph(m_selectionToDelete.visibleStart(), CanCrossEditingBoundary)
760 && isEndOfParagraph(m_selectionToDelete.visibleEnd(), CanCrossEditingBoundary)
commit-queue@webkit.org1d836ce2010-10-29 21:47:01 +0000761 && !lineBreakExistsAtVisiblePosition(m_selectionToDelete.visibleEnd());
justing552eeaf2007-04-20 01:53:22 +0000762 if (m_needPlaceholder) {
763 // Don't need a placeholder when deleting a selection that starts just before a table
764 // and ends inside it (we do need placeholders to hold open empty cells, but that's
765 // handled elsewhere).
766 if (Node* table = isLastPositionBeforeTable(m_selectionToDelete.visibleStart()))
rniwa@webkit.org62b16972011-02-21 09:28:48 +0000767 if (m_selectionToDelete.end().deprecatedNode()->isDescendantOf(table))
justing552eeaf2007-04-20 01:53:22 +0000768 m_needPlaceholder = false;
769 }
770
justing11ec52e2006-06-07 00:09:37 +0000771
mjsa0fe4042005-05-13 08:37:15 +0000772 // set up our state
773 initializePositionData();
harrisondff01cd2005-07-18 23:21:19 +0000774
hyatt0c05e102006-04-14 08:15:00 +0000775 // Delete any text that may hinder our ability to fixup whitespace after the delete
harrisondff01cd2005-07-18 23:21:19 +0000776 deleteInsignificantTextDownstream(m_trailingWhitespace);
777
778 saveTypingStyleState();
mjsa0fe4042005-05-13 08:37:15 +0000779
harrisone4b84c52005-07-18 18:14:59 +0000780 // deleting just a BR is handled specially, at least because we do not
781 // want to replace it with a placeholder BR!
782 if (handleSpecialCaseBRDelete()) {
justin.garcia@apple.comd1eac9a2008-06-20 00:24:13 +0000783 calculateTypingStyleAfterDelete();
eric@webkit.org87ea95c2009-02-09 21:43:24 +0000784 setEndingSelection(VisibleSelection(m_endingPosition, affinity));
harrisone4b84c52005-07-18 18:14:59 +0000785 clearTransientState();
786 rebalanceWhitespace();
787 return;
788 }
mjsa0fe4042005-05-13 08:37:15 +0000789
harrisone4b84c52005-07-18 18:14:59 +0000790 handleGeneralDelete();
mjsa0fe4042005-05-13 08:37:15 +0000791
mjsa0fe4042005-05-13 08:37:15 +0000792 fixupWhitespace();
mjsa0fe4042005-05-13 08:37:15 +0000793
justingf81641c2006-06-14 23:23:50 +0000794 mergeParagraphs();
795
justing187d2922007-07-17 21:05:21 +0000796 removePreviouslySelectedEmptyTableRows();
797
justin.garcia@apple.com60777c92007-11-14 22:33:07 +0000798 RefPtr<Node> placeholder = m_needPlaceholder ? createBreakElement(document()).get() : 0;
799
justing11ec52e2006-06-07 00:09:37 +0000800 if (placeholder)
justing5097f5d52007-04-20 00:08:50 +0000801 insertNodeAt(placeholder.get(), m_endingPosition);
mjsa0fe4042005-05-13 08:37:15 +0000802
justing92cd9eb2006-12-06 21:01:30 +0000803 rebalanceWhitespaceAt(m_endingPosition);
804
justin.garcia@apple.comd1eac9a2008-06-20 00:24:13 +0000805 calculateTypingStyleAfterDelete();
justing11ec52e2006-06-07 00:09:37 +0000806
eric@webkit.org87ea95c2009-02-09 21:43:24 +0000807 setEndingSelection(VisibleSelection(m_endingPosition, affinity));
mjsa0fe4042005-05-13 08:37:15 +0000808 clearTransientState();
mjsa0fe4042005-05-13 08:37:15 +0000809}
810
811EditAction DeleteSelectionCommand::editingAction() const
812{
813 // Note that DeleteSelectionCommand is also used when the user presses the Delete key,
814 // but in that case there's a TypingCommand that supplies the editingAction(), so
815 // the Undo menu correctly shows "Undo Typing"
816 return EditActionCut;
817}
818
justin.garcia@apple.comd1eac9a2008-06-20 00:24:13 +0000819// Normally deletion doesn't preserve the typing style that was present before it. For example,
820// type a character, Bold, then delete the character and start typing. The Bold typing style shouldn't
821// stick around. Deletion should preserve a typing style that *it* sets, however.
mjsa0fe4042005-05-13 08:37:15 +0000822bool DeleteSelectionCommand::preservesTypingStyle() const
823{
justin.garcia@apple.comd1eac9a2008-06-20 00:24:13 +0000824 return m_typingStyle;
mjsa0fe4042005-05-13 08:37:15 +0000825}
826
darinb9481ed2006-03-20 02:57:59 +0000827} // namespace WebCore