blob: 9e4ba29bebf1c39dbdeee670abbe7ee152386176 [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
darin@apple.com9a925fa2009-05-04 18:00:34 +000029#include "CSSMutableStyleDeclaration.h"
darinb9481ed2006-03-20 02:57:59 +000030#include "Document.h"
darin36d11362006-04-11 16:30:21 +000031#include "DocumentFragment.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"
eseidel18f16d82006-02-28 00:19:49 +000036#include "Logging.h"
darinb9481ed2006-03-20 02:57:59 +000037#include "CSSComputedStyleDeclaration.h"
darinbbe64662006-01-16 17:52:23 +000038#include "htmlediting.h"
adele174364d2006-04-13 01:50:04 +000039#include "HTMLInputElement.h"
darin98fa8b82006-03-20 08:03:57 +000040#include "HTMLNames.h"
justing217ded82006-04-04 20:52:10 +000041#include "markup.h"
hyatt@apple.comd885df72009-01-22 02:31:52 +000042#include "RenderTableCell.h"
justing217ded82006-04-04 20:52:10 +000043#include "ReplaceSelectionCommand.h"
darin42563ac52007-01-22 17:28:57 +000044#include "Text.h"
darinb9481ed2006-03-20 02:57:59 +000045#include "TextIterator.h"
mjsa0fe4042005-05-13 08:37:15 +000046#include "visible_units.h"
rniwa@webkit.org1de555e2009-08-07 21:36:24 +000047#include "ApplyStyleCommand.h"
mjsa0fe4042005-05-13 08:37:15 +000048
darinbbe64662006-01-16 17:52:23 +000049namespace WebCore {
darinedbc5e42005-08-25 23:13:58 +000050
darinbbe64662006-01-16 17:52:23 +000051using namespace HTMLNames;
mjsa0fe4042005-05-13 08:37:15 +000052
justing94b6f9f2007-10-31 01:27:54 +000053static bool isTableRow(const Node* node)
justing187d2922007-07-17 21:05:21 +000054{
55 return node && node->hasTagName(trTag);
56}
57
justing13e16aa2007-07-17 21:55:05 +000058static bool isTableCellEmpty(Node* cell)
justing187d2922007-07-17 21:05:21 +000059{
60 ASSERT(isTableCell(cell));
eric@webkit.orgf3a18812009-03-20 18:45:15 +000061 return VisiblePosition(firstDeepEditingPositionForNode(cell)) == VisiblePosition(lastDeepEditingPositionForNode(cell));
justing187d2922007-07-17 21:05:21 +000062}
63
justing13e16aa2007-07-17 21:55:05 +000064static bool isTableRowEmpty(Node* row)
justing187d2922007-07-17 21:05:21 +000065{
66 if (!isTableRow(row))
67 return false;
68
69 for (Node* child = row->firstChild(); child; child = child->nextSibling())
70 if (isTableCell(child) && !isTableCellEmpty(child))
71 return false;
72
73 return true;
74}
75
justing113aaf32007-01-25 01:00:36 +000076DeleteSelectionCommand::DeleteSelectionCommand(Document *document, bool smartDelete, bool mergeBlocksAfterDelete, bool replace, bool expandForSpecialElements)
mjsa0fe4042005-05-13 08:37:15 +000077 : CompositeEditCommand(document),
78 m_hasSelectionToDelete(false),
79 m_smartDelete(smartDelete),
80 m_mergeBlocksAfterDelete(mergeBlocksAfterDelete),
adeleb2f2b662006-07-15 17:22:50 +000081 m_replace(replace),
justing113aaf32007-01-25 01:00:36 +000082 m_expandForSpecialElements(expandForSpecialElements),
justin.garcia@apple.com7e95c7b2008-11-12 20:08:06 +000083 m_pruneStartBlockIfNecessary(false),
mitz@apple.comfc292c32009-05-03 04:09:41 +000084 m_startsAtEmptyLine(false),
mjsa0fe4042005-05-13 08:37:15 +000085 m_startBlock(0),
86 m_endBlock(0),
justing503ccf12005-07-29 22:50:47 +000087 m_typingStyle(0),
88 m_deleteIntoBlockquoteStyle(0)
mjsa0fe4042005-05-13 08:37:15 +000089{
90}
91
eric@webkit.org87ea95c2009-02-09 21:43:24 +000092DeleteSelectionCommand::DeleteSelectionCommand(const VisibleSelection& selection, bool smartDelete, bool mergeBlocksAfterDelete, bool replace, bool expandForSpecialElements)
darin35384062006-08-24 22:07:45 +000093 : CompositeEditCommand(selection.start().node()->document()),
mjsa0fe4042005-05-13 08:37:15 +000094 m_hasSelectionToDelete(true),
95 m_smartDelete(smartDelete),
96 m_mergeBlocksAfterDelete(mergeBlocksAfterDelete),
adeleb2f2b662006-07-15 17:22:50 +000097 m_replace(replace),
justing113aaf32007-01-25 01:00:36 +000098 m_expandForSpecialElements(expandForSpecialElements),
justin.garcia@apple.com7e95c7b2008-11-12 20:08:06 +000099 m_pruneStartBlockIfNecessary(false),
mitz@apple.comfc292c32009-05-03 04:09:41 +0000100 m_startsAtEmptyLine(false),
mjsa0fe4042005-05-13 08:37:15 +0000101 m_selectionToDelete(selection),
102 m_startBlock(0),
103 m_endBlock(0),
justing503ccf12005-07-29 22:50:47 +0000104 m_typingStyle(0),
105 m_deleteIntoBlockquoteStyle(0)
mjsa0fe4042005-05-13 08:37:15 +0000106{
107}
108
justing113aaf32007-01-25 01:00:36 +0000109void DeleteSelectionCommand::initializeStartEnd(Position& start, Position& end)
darin35384062006-08-24 22:07:45 +0000110{
darin2b0ff522006-05-03 19:32:21 +0000111 Node* startSpecialContainer = 0;
112 Node* endSpecialContainer = 0;
harrisondec5e092006-01-25 21:05:02 +0000113
justing113aaf32007-01-25 01:00:36 +0000114 start = m_selectionToDelete.start();
115 end = m_selectionToDelete.end();
justinge6f80c12006-06-09 04:57:51 +0000116
justing2112d432006-11-28 21:25:28 +0000117 // 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 +0000118 // but in these cases, we want to delete it, so manually expand the selection
119 if (start.node()->hasTagName(hrTag))
120 start = Position(start.node(), 0);
121 else if (end.node()->hasTagName(hrTag))
122 end = Position(end.node(), 1);
123
justing113aaf32007-01-25 01:00:36 +0000124 // FIXME: This is only used so that moveParagraphs can avoid the bugs in special element expanion.
125 if (!m_expandForSpecialElements)
126 return;
127
justin.garcia@apple.com64125512008-02-20 17:48:44 +0000128 while (1) {
justinge6f80c12006-06-09 04:57:51 +0000129 startSpecialContainer = 0;
130 endSpecialContainer = 0;
131
justing47d836d2007-06-08 00:02:06 +0000132 Position s = positionBeforeContainingSpecialElement(start, &startSpecialContainer);
133 Position e = positionAfterContainingSpecialElement(end, &endSpecialContainer);
justinge6f80c12006-06-09 04:57:51 +0000134
justing47d836d2007-06-08 00:02:06 +0000135 if (!startSpecialContainer && !endSpecialContainer)
justinge6f80c12006-06-09 04:57:51 +0000136 break;
justin.garcia@apple.com64125512008-02-20 17:48:44 +0000137
138 if (VisiblePosition(start) != m_selectionToDelete.visibleStart() || VisiblePosition(end) != m_selectionToDelete.visibleEnd())
139 break;
eric@webkit.org05d6e932009-06-02 21:31:08 +0000140
eric@webkit.orgf74ae5c2009-09-09 23:25:47 +0000141 // If we're going to expand to include the startSpecialContainer, it must be fully selected.
142 if (startSpecialContainer && !endSpecialContainer && comparePositions(positionInParentAfterNode(startSpecialContainer), end) > -1)
justing47d836d2007-06-08 00:02:06 +0000143 break;
144
eric@webkit.org05d6e932009-06-02 21:31:08 +0000145 // If we're going to expand to include the endSpecialContainer, it must be fully selected.
eric@webkit.orgf74ae5c2009-09-09 23:25:47 +0000146 if (endSpecialContainer && !startSpecialContainer && comparePositions(start, positionInParentBeforeNode(endSpecialContainer)) > -1)
justing47d836d2007-06-08 00:02:06 +0000147 break;
eric@webkit.orgf74ae5c2009-09-09 23:25:47 +0000148
justing47d836d2007-06-08 00:02:06 +0000149 if (startSpecialContainer && startSpecialContainer->isDescendantOf(endSpecialContainer))
justing2bdb03e2006-11-30 02:54:19 +0000150 // Don't adjust the end yet, it is the end of a special element that contains the start
151 // special element (which may or may not be fully selected).
152 start = s;
justing47d836d2007-06-08 00:02:06 +0000153 else if (endSpecialContainer && endSpecialContainer->isDescendantOf(startSpecialContainer))
justing2bdb03e2006-11-30 02:54:19 +0000154 // Don't adjust the start yet, it is the start of a special element that contains the end
155 // special element (which may or may not be fully selected).
156 end = e;
157 else {
158 start = s;
159 end = e;
160 }
harrisondec5e092006-01-25 21:05:02 +0000161 }
harrisondec5e092006-01-25 21:05:02 +0000162}
163
mjsa0fe4042005-05-13 08:37:15 +0000164void DeleteSelectionCommand::initializePositionData()
165{
justing113aaf32007-01-25 01:00:36 +0000166 Position start, end;
167 initializeStartEnd(start, end);
168
169 m_upstreamStart = start.upstream();
170 m_downstreamStart = start.downstream();
171 m_upstreamEnd = end.upstream();
172 m_downstreamEnd = end.downstream();
harrisondec5e092006-01-25 21:05:02 +0000173
justing06a653c2007-04-04 20:49:52 +0000174 m_startRoot = editableRootForPosition(start);
175 m_endRoot = editableRootForPosition(end);
176
justin.garcia@apple.comdeed90d2007-12-13 21:32:12 +0000177 m_startTableRow = enclosingNodeOfType(start, &isTableRow);
178 m_endTableRow = enclosingNodeOfType(end, &isTableRow);
justing187d2922007-07-17 21:05:21 +0000179
justingb7701502007-03-29 22:34:28 +0000180 // Don't move content out of a table cell.
justin.garcia@apple.comab87ef82008-05-29 20:37:53 +0000181 // If the cell is non-editable, enclosingNodeOfType won't return it by default, so
182 // tell that function that we don't care if it returns non-editable nodes.
183 Node* startCell = enclosingNodeOfType(m_upstreamStart, &isTableCell, false);
184 Node* endCell = enclosingNodeOfType(m_downstreamEnd, &isTableCell, false);
justing8f16cee2006-06-22 00:20:29 +0000185 // 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 +0000186 if (endCell && endCell != startCell)
justing8f16cee2006-06-22 00:20:29 +0000187 m_mergeBlocksAfterDelete = false;
188
justing11ec52e2006-06-07 00:09:37 +0000189 // 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 +0000190 // Sometimes they aren't (like when no merge is requested), so we must choose one position to hold the caret
191 // and receive the placeholder after deletion.
justing11ec52e2006-06-07 00:09:37 +0000192 VisiblePosition visibleEnd(m_downstreamEnd);
193 if (m_mergeBlocksAfterDelete && !isEndOfParagraph(visibleEnd))
194 m_endingPosition = m_downstreamEnd;
195 else
196 m_endingPosition = m_downstreamStart;
justin.garcia@apple.com7e95c7b2008-11-12 20:08:06 +0000197
198 // We don't want to merge into a block if it will mean changing the quote level of content after deleting
199 // selections that contain a whole number paragraphs plus a line break, since it is unclear to most users
200 // that such a selection actually ends at the start of the next paragraph. This matches TextEdit behavior
201 // for indented paragraphs.
adele@apple.come2bc16b2009-04-28 22:17:29 +0000202 // Only apply this rule if the endingSelection is a range selection. If it is a caret, then other operations have created
203 // 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.
204 if (numEnclosingMailBlockquotes(start) != numEnclosingMailBlockquotes(end)
205 && isStartOfParagraph(visibleEnd) && isStartOfParagraph(VisiblePosition(start))
206 && endingSelection().isRange()) {
justin.garcia@apple.com7e95c7b2008-11-12 20:08:06 +0000207 m_mergeBlocksAfterDelete = false;
208 m_pruneStartBlockIfNecessary = true;
209 }
justing8f16cee2006-06-22 00:20:29 +0000210
mjsa0fe4042005-05-13 08:37:15 +0000211 // Handle leading and trailing whitespace, as well as smart delete adjustments to the selection
darin9aa45a42006-01-15 23:32:02 +0000212 m_leadingWhitespace = m_upstreamStart.leadingWhitespacePosition(m_selectionToDelete.affinity());
mjsa0fe4042005-05-13 08:37:15 +0000213 m_trailingWhitespace = m_downstreamEnd.trailingWhitespacePosition(VP_DEFAULT_AFFINITY);
214
215 if (m_smartDelete) {
216
217 // skip smart delete if the selection to delete already starts or ends with whitespace
darin9aa45a42006-01-15 23:32:02 +0000218 Position pos = VisiblePosition(m_upstreamStart, m_selectionToDelete.affinity()).deepEquivalent();
mjsa0fe4042005-05-13 08:37:15 +0000219 bool skipSmartDelete = pos.trailingWhitespacePosition(VP_DEFAULT_AFFINITY, true).isNotNull();
220 if (!skipSmartDelete)
221 skipSmartDelete = m_downstreamEnd.leadingWhitespacePosition(VP_DEFAULT_AFFINITY, true).isNotNull();
222
223 // extend selection upstream if there is whitespace there
darin9aa45a42006-01-15 23:32:02 +0000224 bool hasLeadingWhitespaceBeforeAdjustment = m_upstreamStart.leadingWhitespacePosition(m_selectionToDelete.affinity(), true).isNotNull();
mjsa0fe4042005-05-13 08:37:15 +0000225 if (!skipSmartDelete && hasLeadingWhitespaceBeforeAdjustment) {
harrisondec5e092006-01-25 21:05:02 +0000226 VisiblePosition visiblePos = VisiblePosition(m_upstreamStart, VP_DEFAULT_AFFINITY).previous();
mjsa0fe4042005-05-13 08:37:15 +0000227 pos = visiblePos.deepEquivalent();
228 // Expand out one character upstream for smart delete and recalculate
229 // positions based on this change.
230 m_upstreamStart = pos.upstream();
231 m_downstreamStart = pos.downstream();
232 m_leadingWhitespace = m_upstreamStart.leadingWhitespacePosition(visiblePos.affinity());
233 }
234
235 // trailing whitespace is only considered for smart delete if there is no leading
236 // whitespace, as in the case where you double-click the first word of a paragraph.
237 if (!skipSmartDelete && !hasLeadingWhitespaceBeforeAdjustment && m_downstreamEnd.trailingWhitespacePosition(VP_DEFAULT_AFFINITY, true).isNotNull()) {
238 // Expand out one character downstream for smart delete and recalculate
239 // positions based on this change.
harrisondec5e092006-01-25 21:05:02 +0000240 pos = VisiblePosition(m_downstreamEnd, VP_DEFAULT_AFFINITY).next().deepEquivalent();
mjsa0fe4042005-05-13 08:37:15 +0000241 m_upstreamEnd = pos.upstream();
242 m_downstreamEnd = pos.downstream();
243 m_trailingWhitespace = m_downstreamEnd.trailingWhitespacePosition(VP_DEFAULT_AFFINITY);
244 }
245 }
246
justin.garcia@apple.come1da2732008-06-25 21:22:12 +0000247 // We must pass the positions through rangeCompliantEquivalent, since some editing positions
248 // that appear inside their nodes aren't really inside them. [hr, 0] is one example.
249 // FIXME: rangeComplaintEquivalent should eventually be moved into enclosing element getters
250 // like the one below, since editing functions should obviously accept editing positions.
251 // FIXME: Passing false to enclosingNodeOfType tells it that it's OK to return a non-editable
252 // node. This was done to match existing behavior, but it seems wrong.
253 m_startBlock = enclosingNodeOfType(rangeCompliantEquivalent(m_downstreamStart), &isBlock, false);
254 m_endBlock = enclosingNodeOfType(rangeCompliantEquivalent(m_upstreamEnd), &isBlock, false);
mjsa0fe4042005-05-13 08:37:15 +0000255}
256
justin.garcia@apple.come2482302008-11-22 01:50:42 +0000257static void removeEnclosingAnchorStyle(CSSMutableStyleDeclaration* style, const Position& position)
258{
259 Node* enclosingAnchor = enclosingAnchorElement(position);
260 if (!enclosingAnchor || !enclosingAnchor->parentNode())
261 return;
rniwa@webkit.org1de555e2009-08-07 21:36:24 +0000262
263 removeStylesAddedByNode(style, enclosingAnchor);
justin.garcia@apple.come2482302008-11-22 01:50:42 +0000264}
265
mjsa0fe4042005-05-13 08:37:15 +0000266void DeleteSelectionCommand::saveTypingStyleState()
267{
justin.garcia@apple.com64125512008-02-20 17:48:44 +0000268 // A common case is deleting characters that are all from the same text node. In
269 // that case, the style at the start of the selection before deletion will be the
270 // same as the style at the start of the selection after deletion (since those
271 // two positions will be identical). Therefore there is no need to save the
272 // typing style at the start of the selection, nor is there a reason to
273 // compute the style at the start of the selection after deletion (see the
274 // early return in calculateTypingStyleAfterDelete).
275 if (m_upstreamStart.node() == m_downstreamEnd.node() && m_upstreamStart.node()->isTextNode())
276 return;
rniwa@webkit.org1de555e2009-08-07 21:36:24 +0000277
mjsa0fe4042005-05-13 08:37:15 +0000278 // Figure out the typing style in effect before the delete is done.
rniwa@webkit.org1de555e2009-08-07 21:36:24 +0000279 m_typingStyle = editingStyleAtPosition(positionBeforeTabSpan(m_selectionToDelete.start()));
280
justin.garcia@apple.come2482302008-11-22 01:50:42 +0000281 removeEnclosingAnchorStyle(m_typingStyle.get(), m_selectionToDelete.start());
rniwa@webkit.org1de555e2009-08-07 21:36:24 +0000282
justing503ccf12005-07-29 22:50:47 +0000283 // If we're deleting into a Mail blockquote, save the style at end() instead of start()
284 // We'll use this later in computeTypingStyleAfterDelete if we end up outside of a Mail blockquote
rniwa@webkit.org1de555e2009-08-07 21:36:24 +0000285 if (nearestMailBlockquote(m_selectionToDelete.start().node()))
286 m_deleteIntoBlockquoteStyle = editingStyleAtPosition(m_selectionToDelete.end());
287 else
justing503ccf12005-07-29 22:50:47 +0000288 m_deleteIntoBlockquoteStyle = 0;
mjsa0fe4042005-05-13 08:37:15 +0000289}
290
291bool DeleteSelectionCommand::handleSpecialCaseBRDelete()
292{
293 // Check for special-case where the selection contains only a BR on a line by itself after another BR.
justing11ec52e2006-06-07 00:09:37 +0000294 bool upstreamStartIsBR = m_upstreamStart.node()->hasTagName(brTag);
mjs76582fb2005-07-30 02:33:26 +0000295 bool downstreamStartIsBR = m_downstreamStart.node()->hasTagName(brTag);
mjsa0fe4042005-05-13 08:37:15 +0000296 bool isBROnLineByItself = upstreamStartIsBR && downstreamStartIsBR && m_downstreamStart.node() == m_upstreamEnd.node();
297 if (isBROnLineByItself) {
298 removeNode(m_downstreamStart.node());
mjsa0fe4042005-05-13 08:37:15 +0000299 return true;
300 }
301
302 // Not a special-case delete per se, but we can detect that the merging of content between blocks
303 // should not be done.
harrison581c07b2006-11-06 18:08:52 +0000304 if (upstreamStartIsBR && downstreamStartIsBR) {
mitz@apple.comfc292c32009-05-03 04:09:41 +0000305 m_startsAtEmptyLine = true;
harrison581c07b2006-11-06 18:08:52 +0000306 m_endingPosition = m_downstreamEnd;
307 }
308
mjsa0fe4042005-05-13 08:37:15 +0000309 return false;
310}
311
justingf81641c2006-06-14 23:23:50 +0000312static void updatePositionForNodeRemoval(Node* node, Position& position)
313{
314 if (position.isNull())
315 return;
eric@webkit.org4e32ebc2009-04-30 01:09:57 +0000316 if (node->parent() == position.node() && node->nodeIndex() < (unsigned)position.deprecatedEditingOffset())
317 position = Position(position.node(), position.deprecatedEditingOffset() - 1);
justing5b9c5a02006-09-23 00:14:55 +0000318 if (position.node() == node || position.node()->isDescendantOf(node))
eric@webkit.orgf74ae5c2009-09-09 23:25:47 +0000319 position = positionInParentBeforeNode(node);
justingf81641c2006-06-14 23:23:50 +0000320}
321
darin@apple.come95b53d2008-12-23 21:42:46 +0000322void DeleteSelectionCommand::removeNode(PassRefPtr<Node> node)
justing11ec52e2006-06-07 00:09:37 +0000323{
justing06a653c2007-04-04 20:49:52 +0000324 if (!node)
325 return;
326
327 if (m_startRoot != m_endRoot && !(node->isDescendantOf(m_startRoot.get()) && node->isDescendantOf(m_endRoot.get()))) {
328 // If a node is not in both the start and end editable roots, remove it only if its inside an editable region.
329 if (!node->parentNode()->isContentEditable()) {
330 // Don't remove non-editable atomic nodes.
331 if (!node->firstChild())
332 return;
333 // Search this non-editable region for editable regions to empty.
334 RefPtr<Node> child = node->firstChild();
335 while (child) {
336 RefPtr<Node> nextChild = child->nextSibling();
337 removeNode(child.get());
338 // Bail if nextChild is no longer node's child.
339 if (nextChild && nextChild->parentNode() != node)
340 return;
341 child = nextChild;
342 }
343
344 // Don't remove editable regions that are inside non-editable ones, just clear them.
345 return;
346 }
347 }
348
darin@apple.come95b53d2008-12-23 21:42:46 +0000349 if (isTableStructureNode(node.get()) || node == node->rootEditableElement()) {
justing11ec52e2006-06-07 00:09:37 +0000350 // Do not remove an element of table structure; remove its contents.
351 // Likewise for the root editable element.
darin@apple.come95b53d2008-12-23 21:42:46 +0000352 Node* child = node->firstChild();
justing11ec52e2006-06-07 00:09:37 +0000353 while (child) {
darin@apple.come95b53d2008-12-23 21:42:46 +0000354 Node* remove = child;
justing11ec52e2006-06-07 00:09:37 +0000355 child = child->nextSibling();
justingf81641c2006-06-14 23:23:50 +0000356 removeNode(remove);
justing11ec52e2006-06-07 00:09:37 +0000357 }
358
359 // make sure empty cell has some height
360 updateLayout();
361 RenderObject *r = node->renderer();
darin@apple.com445c34f2009-08-01 00:40:58 +0000362 if (r && r->isTableCell() && toRenderTableCell(r)->contentHeight() <= 0)
ddkilzer@apple.com9c4dacc2009-07-11 05:36:29 +0000363 insertBlockPlaceholder(Position(node, 0));
justing11ec52e2006-06-07 00:09:37 +0000364 return;
365 }
366
eric@webkit.orgf3a18812009-03-20 18:45:15 +0000367 if (node == m_startBlock && !isEndOfBlock(VisiblePosition(firstDeepEditingPositionForNode(m_startBlock.get())).previous()))
lweintraub635ec2a2006-07-13 18:28:28 +0000368 m_needPlaceholder = true;
eric@webkit.orgf3a18812009-03-20 18:45:15 +0000369 else if (node == m_endBlock && !isStartOfBlock(VisiblePosition(lastDeepEditingPositionForNode(m_startBlock.get())).next()))
lweintraub635ec2a2006-07-13 18:28:28 +0000370 m_needPlaceholder = true;
371
justingf81641c2006-06-14 23:23:50 +0000372 // FIXME: Update the endpoints of the range being deleted.
darin@apple.come95b53d2008-12-23 21:42:46 +0000373 updatePositionForNodeRemoval(node.get(), m_endingPosition);
374 updatePositionForNodeRemoval(node.get(), m_leadingWhitespace);
375 updatePositionForNodeRemoval(node.get(), m_trailingWhitespace);
justing11ec52e2006-06-07 00:09:37 +0000376
justingf81641c2006-06-14 23:23:50 +0000377 CompositeEditCommand::removeNode(node);
378}
379
andersca@apple.comd8dda092009-01-23 19:52:19 +0000380static void updatePositionForTextRemoval(Node* node, int offset, int count, Position& position)
justingf81641c2006-06-14 23:23:50 +0000381{
382 if (position.node() == node) {
eric@webkit.org4e32ebc2009-04-30 01:09:57 +0000383 if (position.deprecatedEditingOffset() > offset + count)
384 position = Position(position.node(), position.deprecatedEditingOffset() - count);
385 else if (position.deprecatedEditingOffset() > offset)
justingf81641c2006-06-14 23:23:50 +0000386 position = Position(position.node(), offset);
387 }
justing11ec52e2006-06-07 00:09:37 +0000388}
389
darin@apple.come95b53d2008-12-23 21:42:46 +0000390void DeleteSelectionCommand::deleteTextFromNode(PassRefPtr<Text> node, unsigned offset, unsigned count)
justing11ec52e2006-06-07 00:09:37 +0000391{
justingf81641c2006-06-14 23:23:50 +0000392 // FIXME: Update the endpoints of the range being deleted.
darin@apple.come95b53d2008-12-23 21:42:46 +0000393 updatePositionForTextRemoval(node.get(), offset, count, m_endingPosition);
394 updatePositionForTextRemoval(node.get(), offset, count, m_leadingWhitespace);
395 updatePositionForTextRemoval(node.get(), offset, count, m_trailingWhitespace);
justin.garcia@apple.com68510a52009-04-23 00:54:42 +0000396 updatePositionForTextRemoval(node.get(), offset, count, m_downstreamEnd);
justing11ec52e2006-06-07 00:09:37 +0000397
398 CompositeEditCommand::deleteTextFromNode(node, offset, count);
399}
400
mjsa0fe4042005-05-13 08:37:15 +0000401void DeleteSelectionCommand::handleGeneralDelete()
402{
eric@webkit.org4e32ebc2009-04-30 01:09:57 +0000403 int startOffset = m_upstreamStart.deprecatedEditingOffset();
justing11ec52e2006-06-07 00:09:37 +0000404 Node* startNode = m_upstreamStart.node();
lweintraub635ec2a2006-07-13 18:28:28 +0000405
406 // Never remove the start block unless it's a table, in which case we won't merge content in.
407 if (startNode == m_startBlock && startOffset == 0 && canHaveChildrenForEditing(startNode) && !startNode->hasTagName(tableTag)) {
mjsa0fe4042005-05-13 08:37:15 +0000408 startOffset = 0;
justing11ec52e2006-06-07 00:09:37 +0000409 startNode = startNode->traverseNextNode();
justinge6f80c12006-06-09 04:57:51 +0000410 }
411
ap@webkit.orgff51b522007-11-11 20:28:51 +0000412 if (startOffset >= caretMaxOffset(startNode) && startNode->isTextNode()) {
justinge6f80c12006-06-09 04:57:51 +0000413 Text *text = static_cast<Text *>(startNode);
ap@webkit.orgff51b522007-11-11 20:28:51 +0000414 if (text->length() > (unsigned)caretMaxOffset(startNode))
415 deleteTextFromNode(text, caretMaxOffset(startNode), text->length() - caretMaxOffset(startNode));
justinge6f80c12006-06-09 04:57:51 +0000416 }
417
eric@webkit.orgf3a18812009-03-20 18:45:15 +0000418 if (startOffset >= lastOffsetForEditing(startNode)) {
justinge6f80c12006-06-09 04:57:51 +0000419 startNode = startNode->traverseNextSibling();
mjsa0fe4042005-05-13 08:37:15 +0000420 startOffset = 0;
421 }
422
423 // Done adjusting the start. See if we're all done.
justing11ec52e2006-06-07 00:09:37 +0000424 if (!startNode)
mjsa0fe4042005-05-13 08:37:15 +0000425 return;
426
justing11ec52e2006-06-07 00:09:37 +0000427 if (startNode == m_downstreamEnd.node()) {
mjsa0fe4042005-05-13 08:37:15 +0000428 // The selection to delete is all in one node.
eric@webkit.orgf3a18812009-03-20 18:45:15 +0000429 if (!startNode->renderer() || (startOffset == 0 && m_downstreamEnd.atLastEditingPositionForNode())) {
mjsa0fe4042005-05-13 08:37:15 +0000430 // just delete
justingf81641c2006-06-14 23:23:50 +0000431 removeNode(startNode);
eric@webkit.org4e32ebc2009-04-30 01:09:57 +0000432 } else if (m_downstreamEnd.deprecatedEditingOffset() - startOffset > 0) {
justing11ec52e2006-06-07 00:09:37 +0000433 if (startNode->isTextNode()) {
mjsa0fe4042005-05-13 08:37:15 +0000434 // in a text node that needs to be trimmed
eric@webkit.orgf3a18812009-03-20 18:45:15 +0000435 Text* text = static_cast<Text*>(startNode);
eric@webkit.org4e32ebc2009-04-30 01:09:57 +0000436 deleteTextFromNode(text, startOffset, m_downstreamEnd.deprecatedEditingOffset() - startOffset);
mjsa0fe4042005-05-13 08:37:15 +0000437 } else {
eric@webkit.org4e32ebc2009-04-30 01:09:57 +0000438 removeChildrenInRange(startNode, startOffset, m_downstreamEnd.deprecatedEditingOffset());
mjsa0fe4042005-05-13 08:37:15 +0000439 m_endingPosition = m_upstreamStart;
440 }
441 }
442 }
443 else {
justingd197d6b2007-08-25 01:20:33 +0000444 bool startNodeWasDescendantOfEndNode = m_upstreamStart.node()->isDescendantOf(m_downstreamEnd.node());
mjsa0fe4042005-05-13 08:37:15 +0000445 // The selection to delete spans more than one node.
justingd197d6b2007-08-25 01:20:33 +0000446 RefPtr<Node> node(startNode);
mjsa0fe4042005-05-13 08:37:15 +0000447
448 if (startOffset > 0) {
justing11ec52e2006-06-07 00:09:37 +0000449 if (startNode->isTextNode()) {
mjsa0fe4042005-05-13 08:37:15 +0000450 // in a text node that needs to be trimmed
justing4855c4f2007-08-23 18:11:04 +0000451 Text *text = static_cast<Text *>(node.get());
mjsa0fe4042005-05-13 08:37:15 +0000452 deleteTextFromNode(text, startOffset, text->length() - startOffset);
453 node = node->traverseNextNode();
454 } else {
justing11ec52e2006-06-07 00:09:37 +0000455 node = startNode->childNode(startOffset);
mjsa0fe4042005-05-13 08:37:15 +0000456 }
457 }
458
459 // handle deleting all nodes that are completely selected
460 while (node && node != m_downstreamEnd.node()) {
eric@webkit.org05d6e932009-06-02 21:31:08 +0000461 if (comparePositions(Position(node.get(), 0), m_downstreamEnd) >= 0) {
mjsa0fe4042005-05-13 08:37:15 +0000462 // traverseNextSibling just blew past the end position, so stop deleting
463 node = 0;
justing4855c4f2007-08-23 18:11:04 +0000464 } else if (!m_downstreamEnd.node()->isDescendantOf(node.get())) {
justing24754322007-07-09 20:27:05 +0000465 RefPtr<Node> nextNode = node->traverseNextSibling();
mjsa0fe4042005-05-13 08:37:15 +0000466 // if we just removed a node from the end container, update end position so the
467 // check above will work
468 if (node->parentNode() == m_downstreamEnd.node()) {
eric@webkit.org4e32ebc2009-04-30 01:09:57 +0000469 ASSERT(node->nodeIndex() < (unsigned)m_downstreamEnd.deprecatedEditingOffset());
470 m_downstreamEnd = Position(m_downstreamEnd.node(), m_downstreamEnd.deprecatedEditingOffset() - 1);
mjsa0fe4042005-05-13 08:37:15 +0000471 }
justing4855c4f2007-08-23 18:11:04 +0000472 removeNode(node.get());
justing24754322007-07-09 20:27:05 +0000473 node = nextNode.get();
mjsa0fe4042005-05-13 08:37:15 +0000474 } else {
justing11ec52e2006-06-07 00:09:37 +0000475 Node* n = node->lastDescendant();
eric@webkit.org4e32ebc2009-04-30 01:09:57 +0000476 if (m_downstreamEnd.node() == n && m_downstreamEnd.deprecatedEditingOffset() >= caretMaxOffset(n)) {
justing4855c4f2007-08-23 18:11:04 +0000477 removeNode(node.get());
mjsa0fe4042005-05-13 08:37:15 +0000478 node = 0;
justing95561262006-06-21 21:22:50 +0000479 } else
mjsa0fe4042005-05-13 08:37:15 +0000480 node = node->traverseNextNode();
mjsa0fe4042005-05-13 08:37:15 +0000481 }
482 }
mjsa0fe4042005-05-13 08:37:15 +0000483
eric@webkit.org4e32ebc2009-04-30 01:09:57 +0000484 if (m_downstreamEnd.node() != startNode && !m_upstreamStart.node()->isDescendantOf(m_downstreamEnd.node()) && m_downstreamEnd.node()->inDocument() && m_downstreamEnd.deprecatedEditingOffset() >= caretMinOffset(m_downstreamEnd.node())) {
eric@webkit.orgf3a18812009-03-20 18:45:15 +0000485 if (m_downstreamEnd.atLastEditingPositionForNode() && !canHaveChildrenForEditing(m_downstreamEnd.node())) {
justing395a18e2007-06-26 06:52:15 +0000486 // The node itself is fully selected, not just its contents. Delete it.
justingf81641c2006-06-14 23:23:50 +0000487 removeNode(m_downstreamEnd.node());
mjsa0fe4042005-05-13 08:37:15 +0000488 } else {
489 if (m_downstreamEnd.node()->isTextNode()) {
490 // in a text node that needs to be trimmed
darinb9481ed2006-03-20 02:57:59 +0000491 Text *text = static_cast<Text *>(m_downstreamEnd.node());
eric@webkit.org4e32ebc2009-04-30 01:09:57 +0000492 if (m_downstreamEnd.deprecatedEditingOffset() > 0) {
493 deleteTextFromNode(text, 0, m_downstreamEnd.deprecatedEditingOffset());
mjsa0fe4042005-05-13 08:37:15 +0000494 }
justingd197d6b2007-08-25 01:20:33 +0000495 // Remove children of m_downstreamEnd.node() that come after m_upstreamStart.
496 // Don't try to remove children if m_upstreamStart was inside m_downstreamEnd.node()
497 // and m_upstreamStart has been removed from the document, because then we don't
498 // know how many children to remove.
499 // FIXME: Make m_upstreamStart a position we update as we remove content, then we can
500 // always know which children to remove.
501 } else if (!(startNodeWasDescendantOfEndNode && !m_upstreamStart.node()->inDocument())) {
mjsa0fe4042005-05-13 08:37:15 +0000502 int offset = 0;
justing5b9c5a02006-09-23 00:14:55 +0000503 if (m_upstreamStart.node()->isDescendantOf(m_downstreamEnd.node())) {
darinb9481ed2006-03-20 02:57:59 +0000504 Node *n = m_upstreamStart.node();
mjsa0fe4042005-05-13 08:37:15 +0000505 while (n && n->parentNode() != m_downstreamEnd.node())
506 n = n->parentNode();
507 if (n)
508 offset = n->nodeIndex() + 1;
509 }
eric@webkit.org4e32ebc2009-04-30 01:09:57 +0000510 removeChildrenInRange(m_downstreamEnd.node(), offset, m_downstreamEnd.deprecatedEditingOffset());
mjsa0fe4042005-05-13 08:37:15 +0000511 m_downstreamEnd = Position(m_downstreamEnd.node(), offset);
512 }
513 }
514 }
515 }
516}
517
mjsa0fe4042005-05-13 08:37:15 +0000518void DeleteSelectionCommand::fixupWhitespace()
519{
darin26f5b362005-12-22 04:11:39 +0000520 updateLayout();
justingf81641c2006-06-14 23:23:50 +0000521 // FIXME: isRenderedCharacter should be removed, and we should use VisiblePosition::characterAfter and VisiblePosition::characterBefore
522 if (m_leadingWhitespace.isNotNull() && !m_leadingWhitespace.isRenderedCharacter()) {
darin38ae5342006-07-25 04:39:25 +0000523 Text* textNode = static_cast<Text*>(m_leadingWhitespace.node());
524 ASSERT(!textNode->renderer() || textNode->renderer()->style()->collapseWhiteSpace());
eric@webkit.org4e32ebc2009-04-30 01:09:57 +0000525 replaceTextInNode(textNode, m_leadingWhitespace.deprecatedEditingOffset(), 1, nonBreakingSpaceString());
mjsa0fe4042005-05-13 08:37:15 +0000526 }
justingf81641c2006-06-14 23:23:50 +0000527 if (m_trailingWhitespace.isNotNull() && !m_trailingWhitespace.isRenderedCharacter()) {
darin38ae5342006-07-25 04:39:25 +0000528 Text* textNode = static_cast<Text*>(m_trailingWhitespace.node());
529 ASSERT(!textNode->renderer() ||textNode->renderer()->style()->collapseWhiteSpace());
eric@webkit.org4e32ebc2009-04-30 01:09:57 +0000530 replaceTextInNode(textNode, m_trailingWhitespace.deprecatedEditingOffset(), 1, nonBreakingSpaceString());
mjsa0fe4042005-05-13 08:37:15 +0000531 }
532}
533
justingf81641c2006-06-14 23:23:50 +0000534// If a selection starts in one block and ends in another, we have to merge to bring content before the
535// start together with content after the end.
justing217ded82006-04-04 20:52:10 +0000536void DeleteSelectionCommand::mergeParagraphs()
mjsa0fe4042005-05-13 08:37:15 +0000537{
justin.garcia@apple.com7e95c7b2008-11-12 20:08:06 +0000538 if (!m_mergeBlocksAfterDelete) {
539 if (m_pruneStartBlockIfNecessary) {
justin.garcia@apple.com7e95c7b2008-11-12 20:08:06 +0000540 // 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 +0000541 prune(m_startBlock);
justin.garcia@apple.com7e95c7b2008-11-12 20:08:06 +0000542 // Removing the start block during a deletion is usually an indication that we need
543 // a placeholder, but not in this case.
544 m_needPlaceholder = false;
545 }
mjsa0fe4042005-05-13 08:37:15 +0000546 return;
justin.garcia@apple.com7e95c7b2008-11-12 20:08:06 +0000547 }
548
549 // It shouldn't have been asked to both try and merge content into the start block and prune it.
550 ASSERT(!m_pruneStartBlockIfNecessary);
mjsa0fe4042005-05-13 08:37:15 +0000551
justing217ded82006-04-04 20:52:10 +0000552 // FIXME: Deletion should adjust selection endpoints as it removes nodes so that we never get into this state (4099839).
553 if (!m_downstreamEnd.node()->inDocument() || !m_upstreamStart.node()->inDocument())
554 return;
555
justingced67722006-05-01 21:43:03 +0000556 // FIXME: The deletion algorithm shouldn't let this happen.
eric@webkit.org05d6e932009-06-02 21:31:08 +0000557 if (comparePositions(m_upstreamStart, m_downstreamEnd) > 0)
justingced67722006-05-01 21:43:03 +0000558 return;
559
justin.garcia@apple.com68510a52009-04-23 00:54:42 +0000560 // There's nothing to merge.
561 if (m_upstreamStart == m_downstreamEnd)
justingced67722006-05-01 21:43:03 +0000562 return;
563
justing217ded82006-04-04 20:52:10 +0000564 VisiblePosition startOfParagraphToMove(m_downstreamEnd);
565 VisiblePosition mergeDestination(m_upstreamStart);
566
justin.garcia@apple.com60777c92007-11-14 22:33:07 +0000567 // m_downstreamEnd's block has been emptied out by deletion. There is no content inside of it to
568 // move, so just remove it.
569 Element* endBlock = static_cast<Element*>(enclosingBlock(m_downstreamEnd.node()));
570 if (!startOfParagraphToMove.deepEquivalent().node() || !endBlock->contains(startOfParagraphToMove.deepEquivalent().node())) {
571 removeNode(enclosingBlock(m_downstreamEnd.node()));
572 return;
573 }
574
justingced67722006-05-01 21:43:03 +0000575 // We need to merge into m_upstreamStart's block, but it's been emptied out and collapsed by deletion.
mitz@apple.comfc292c32009-05-03 04:09:41 +0000576 if (!mergeDestination.deepEquivalent().node() || !mergeDestination.deepEquivalent().node()->isDescendantOf(m_upstreamStart.node()->enclosingBlockFlowElement()) || m_startsAtEmptyLine) {
justing5097f5d52007-04-20 00:08:50 +0000577 insertNodeAt(createBreakElement(document()).get(), m_upstreamStart);
justingced67722006-05-01 21:43:03 +0000578 mergeDestination = VisiblePosition(m_upstreamStart);
579 }
harrisondec5e092006-01-25 21:05:02 +0000580
justingced67722006-05-01 21:43:03 +0000581 if (mergeDestination == startOfParagraphToMove)
mjsa0fe4042005-05-13 08:37:15 +0000582 return;
justing217ded82006-04-04 20:52:10 +0000583
584 VisiblePosition endOfParagraphToMove = endOfParagraph(startOfParagraphToMove);
585
darin2b0ff522006-05-03 19:32:21 +0000586 if (mergeDestination == endOfParagraphToMove)
587 return;
mjsa0fe4042005-05-13 08:37:15 +0000588
justinge809f932006-06-23 22:36:12 +0000589 // The rule for merging into an empty block is: only do so if its farther to the right.
590 // FIXME: Consider RTL.
mitz@apple.comfc292c32009-05-03 04:09:41 +0000591 if (!m_startsAtEmptyLine && isStartOfParagraph(mergeDestination) && startOfParagraphToMove.absoluteCaretBounds().x() > mergeDestination.absoluteCaretBounds().x()) {
justinge809f932006-06-23 22:36:12 +0000592 ASSERT(mergeDestination.deepEquivalent().downstream().node()->hasTagName(brTag));
593 removeNodeAndPruneAncestors(mergeDestination.deepEquivalent().downstream().node());
594 m_endingPosition = startOfParagraphToMove.deepEquivalent();
595 return;
596 }
597
justin.garcia@apple.com619526a2009-11-05 05:25:24 +0000598 // Block images, tables and horizontal rules cannot be made inline with content at mergeDestination. If there is
599 // any (!isStartOfParagraph(mergeDestination)), don't merge, just move the caret to just before the selection we deleted.
600 // See https://bugs.webkit.org/show_bug.cgi?id=25439
601 if (isRenderedAsNonInlineTableImageOrHR(startOfParagraphToMove.deepEquivalent().node()) && !isStartOfParagraph(mergeDestination)) {
602 m_endingPosition = m_upstreamStart;
603 return;
604 }
605
darin@apple.comb7bf13a2008-03-11 23:43:12 +0000606 RefPtr<Range> range = Range::create(document(), rangeCompliantEquivalent(startOfParagraphToMove.deepEquivalent()), rangeCompliantEquivalent(endOfParagraphToMove.deepEquivalent()));
607 RefPtr<Range> rangeToBeReplaced = Range::create(document(), rangeCompliantEquivalent(mergeDestination.deepEquivalent()), rangeCompliantEquivalent(mergeDestination.deepEquivalent()));
justingf90e28d2007-07-30 18:25:19 +0000608 if (!document()->frame()->editor()->client()->shouldMoveRangeAfterDelete(range.get(), rangeToBeReplaced.get()))
609 return;
610
justin.garcia@apple.com60777c92007-11-14 22:33:07 +0000611 // moveParagraphs will insert placeholders if it removes blocks that would require their use, don't let block
612 // removals that it does cause the insertion of *another* placeholder.
613 bool needPlaceholder = m_needPlaceholder;
justing11ec52e2006-06-07 00:09:37 +0000614 moveParagraph(startOfParagraphToMove, endOfParagraphToMove, mergeDestination);
justin.garcia@apple.com60777c92007-11-14 22:33:07 +0000615 m_needPlaceholder = needPlaceholder;
justing93798f12006-06-08 04:04:38 +0000616 // The endingPosition was likely clobbered by the move, so recompute it (moveParagraph selects the moved paragraph).
justing11ec52e2006-06-07 00:09:37 +0000617 m_endingPosition = endingSelection().start();
mjsa0fe4042005-05-13 08:37:15 +0000618}
619
justing187d2922007-07-17 21:05:21 +0000620void DeleteSelectionCommand::removePreviouslySelectedEmptyTableRows()
621{
justin.garcia@apple.com99a05542007-12-13 06:56:28 +0000622 if (m_endTableRow && m_endTableRow->inDocument() && m_endTableRow != m_startTableRow) {
623 Node* row = m_endTableRow->previousSibling();
624 while (row && row != m_startTableRow) {
justing187d2922007-07-17 21:05:21 +0000625 RefPtr<Node> previousRow = row->previousSibling();
626 if (isTableRowEmpty(row))
627 // Use a raw removeNode, instead of DeleteSelectionCommand's, because
628 // that won't remove rows, it only empties them in preparation for this function.
629 CompositeEditCommand::removeNode(row);
630 row = previousRow.get();
631 }
632 }
633
justing7d4a4342007-10-13 00:40:48 +0000634 // Remove empty rows after the start row.
635 if (m_startTableRow && m_startTableRow->inDocument() && m_startTableRow != m_endTableRow) {
justing187d2922007-07-17 21:05:21 +0000636 Node* row = m_startTableRow->nextSibling();
justin.garcia@apple.com99a05542007-12-13 06:56:28 +0000637 while (row && row != m_endTableRow) {
justing187d2922007-07-17 21:05:21 +0000638 RefPtr<Node> nextRow = row->nextSibling();
639 if (isTableRowEmpty(row))
640 CompositeEditCommand::removeNode(row);
641 row = nextRow.get();
642 }
643 }
justin.garcia@apple.com99a05542007-12-13 06:56:28 +0000644
645 if (m_endTableRow && m_endTableRow->inDocument() && m_endTableRow != m_startTableRow)
646 if (isTableRowEmpty(m_endTableRow.get())) {
647 // Don't remove m_endTableRow if it's where we're putting the ending selection.
648 if (!m_endingPosition.node()->isDescendantOf(m_endTableRow.get())) {
649 // FIXME: We probably shouldn't remove m_endTableRow unless it's fully selected, even if it is empty.
650 // We'll need to start adjusting the selection endpoints during deletion to know whether or not m_endTableRow
651 // was fully selected here.
652 CompositeEditCommand::removeNode(m_endTableRow.get());
653 }
654 }
justing187d2922007-07-17 21:05:21 +0000655}
656
justin.garcia@apple.comd1eac9a2008-06-20 00:24:13 +0000657void DeleteSelectionCommand::calculateTypingStyleAfterDelete()
mjsa0fe4042005-05-13 08:37:15 +0000658{
justin.garcia@apple.com64125512008-02-20 17:48:44 +0000659 if (!m_typingStyle)
660 return;
661
mjsa0fe4042005-05-13 08:37:15 +0000662 // Compute the difference between the style before the delete and the style now
mjs810e9762006-01-09 21:39:14 +0000663 // after the delete has been done. Set this style on the frame, so other editing
mjsa0fe4042005-05-13 08:37:15 +0000664 // commands being composed with this one will work, and also cache it on the command,
mjs810e9762006-01-09 21:39:14 +0000665 // so the Frame::appliedEditing can set it after the whole composite command
mjsa0fe4042005-05-13 08:37:15 +0000666 // has completed.
justing503ccf12005-07-29 22:50:47 +0000667
eseidelef508982006-01-03 09:19:17 +0000668 // If we deleted into a blockquote, but are now no longer in a blockquote, use the alternate typing style
669 if (m_deleteIntoBlockquoteStyle && !nearestMailBlockquote(m_endingPosition.node()))
670 m_typingStyle = m_deleteIntoBlockquoteStyle;
671 m_deleteIntoBlockquoteStyle = 0;
rniwa@webkit.org1de555e2009-08-07 21:36:24 +0000672
673 prepareEditingStyleToApplyAt(m_typingStyle.get(), m_endingPosition);
eseidelef508982006-01-03 09:19:17 +0000674 if (!m_typingStyle->length())
mjsa0fe4042005-05-13 08:37:15 +0000675 m_typingStyle = 0;
justin.garcia@apple.comd1eac9a2008-06-20 00:24:13 +0000676 VisiblePosition visibleEnd(m_endingPosition);
677 if (m_typingStyle &&
678 isStartOfParagraph(visibleEnd) &&
679 isEndOfParagraph(visibleEnd) &&
justin.garcia@apple.comd5c8f432009-04-14 18:48:16 +0000680 lineBreakExistsAtVisiblePosition(visibleEnd)) {
justin.garcia@apple.comd1eac9a2008-06-20 00:24:13 +0000681 // Apply style to the placeholder that is now holding open the empty paragraph.
682 // This makes sure that the paragraph has the right height, and that the paragraph
683 // takes on the right style and retains it even if you move the selection away and
684 // then move it back (which will clear typing style).
mjsa0fe4042005-05-13 08:37:15 +0000685
justin.garcia@apple.comd1eac9a2008-06-20 00:24:13 +0000686 setEndingSelection(visibleEnd);
eseidelef508982006-01-03 09:19:17 +0000687 applyStyle(m_typingStyle.get(), EditActionUnspecified);
justin.garcia@apple.comd1eac9a2008-06-20 00:24:13 +0000688 // applyStyle can destroy the placeholder that was at m_endingPosition if it needs to
689 // move it, but it will set an endingSelection() at [movedPlaceholder, 0] if it does so.
690 m_endingPosition = endingSelection().start();
mjsa0fe4042005-05-13 08:37:15 +0000691 m_typingStyle = 0;
692 }
justin.garcia@apple.comd1eac9a2008-06-20 00:24:13 +0000693 // This is where we've deleted all traces of a style but not a whole paragraph (that's handled above).
694 // In this case if we start typing, the new characters should have the same style as the just deleted ones,
695 // but, if we change the selection, come back and start typing that style should be lost. Also see
696 // preserveTypingStyle() below.
mjs810e9762006-01-09 21:39:14 +0000697 document()->frame()->setTypingStyle(m_typingStyle.get());
mjsa0fe4042005-05-13 08:37:15 +0000698}
699
700void DeleteSelectionCommand::clearTransientState()
701{
eric@webkit.org87ea95c2009-02-09 21:43:24 +0000702 m_selectionToDelete = VisibleSelection();
mjsa0fe4042005-05-13 08:37:15 +0000703 m_upstreamStart.clear();
704 m_downstreamStart.clear();
705 m_upstreamEnd.clear();
706 m_downstreamEnd.clear();
707 m_endingPosition.clear();
708 m_leadingWhitespace.clear();
709 m_trailingWhitespace.clear();
mjsa0fe4042005-05-13 08:37:15 +0000710}
711
712void DeleteSelectionCommand::doApply()
713{
714 // If selection has not been set to a custom selection when the command was created,
715 // use the current ending selection.
716 if (!m_hasSelectionToDelete)
717 m_selectionToDelete = endingSelection();
justing54a6cff2006-07-21 23:38:26 +0000718
mjsa0fe4042005-05-13 08:37:15 +0000719 if (!m_selectionToDelete.isRange())
720 return;
721
adeleb2f2b662006-07-15 17:22:50 +0000722 // 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.
723 if (!m_replace) {
724 Node* startNode = m_selectionToDelete.start().node();
725 Node* ancestorNode = startNode ? startNode->shadowAncestorNode() : 0;
darin3f6601d2007-02-17 01:15:48 +0000726 if (ancestorNode && ancestorNode->hasTagName(inputTag)
727 && static_cast<HTMLInputElement*>(ancestorNode)->isTextField()
728 && ancestorNode->focused())
adeleb2f2b662006-07-15 17:22:50 +0000729 document()->frame()->textWillBeDeletedInTextField(static_cast<Element*>(ancestorNode));
730 }
adele174364d2006-04-13 01:50:04 +0000731
mjsa0fe4042005-05-13 08:37:15 +0000732 // save this to later make the selection with
darin9aa45a42006-01-15 23:32:02 +0000733 EAffinity affinity = m_selectionToDelete.affinity();
mjsa0fe4042005-05-13 08:37:15 +0000734
justing11ec52e2006-06-07 00:09:37 +0000735 Position downstreamEnd = m_selectionToDelete.end().downstream();
lweintraub635ec2a2006-07-13 18:28:28 +0000736 m_needPlaceholder = isStartOfParagraph(m_selectionToDelete.visibleStart()) &&
737 isEndOfParagraph(m_selectionToDelete.visibleEnd()) &&
justin.garcia@apple.comd5c8f432009-04-14 18:48:16 +0000738 !lineBreakExistsAtVisiblePosition(m_selectionToDelete.visibleEnd());
justing552eeaf2007-04-20 01:53:22 +0000739 if (m_needPlaceholder) {
740 // Don't need a placeholder when deleting a selection that starts just before a table
741 // and ends inside it (we do need placeholders to hold open empty cells, but that's
742 // handled elsewhere).
743 if (Node* table = isLastPositionBeforeTable(m_selectionToDelete.visibleStart()))
744 if (m_selectionToDelete.end().node()->isDescendantOf(table))
745 m_needPlaceholder = false;
746 }
747
justing11ec52e2006-06-07 00:09:37 +0000748
mjsa0fe4042005-05-13 08:37:15 +0000749 // set up our state
750 initializePositionData();
harrisondff01cd2005-07-18 23:21:19 +0000751
hyatt0c05e102006-04-14 08:15:00 +0000752 // Delete any text that may hinder our ability to fixup whitespace after the delete
harrisondff01cd2005-07-18 23:21:19 +0000753 deleteInsignificantTextDownstream(m_trailingWhitespace);
754
755 saveTypingStyleState();
mjsa0fe4042005-05-13 08:37:15 +0000756
harrisone4b84c52005-07-18 18:14:59 +0000757 // deleting just a BR is handled specially, at least because we do not
758 // want to replace it with a placeholder BR!
759 if (handleSpecialCaseBRDelete()) {
justin.garcia@apple.comd1eac9a2008-06-20 00:24:13 +0000760 calculateTypingStyleAfterDelete();
eric@webkit.org87ea95c2009-02-09 21:43:24 +0000761 setEndingSelection(VisibleSelection(m_endingPosition, affinity));
harrisone4b84c52005-07-18 18:14:59 +0000762 clearTransientState();
763 rebalanceWhitespace();
764 return;
765 }
mjsa0fe4042005-05-13 08:37:15 +0000766
harrisone4b84c52005-07-18 18:14:59 +0000767 handleGeneralDelete();
mjsa0fe4042005-05-13 08:37:15 +0000768
mjsa0fe4042005-05-13 08:37:15 +0000769 fixupWhitespace();
mjsa0fe4042005-05-13 08:37:15 +0000770
justingf81641c2006-06-14 23:23:50 +0000771 mergeParagraphs();
772
justing187d2922007-07-17 21:05:21 +0000773 removePreviouslySelectedEmptyTableRows();
774
justin.garcia@apple.com60777c92007-11-14 22:33:07 +0000775 RefPtr<Node> placeholder = m_needPlaceholder ? createBreakElement(document()).get() : 0;
776
justing11ec52e2006-06-07 00:09:37 +0000777 if (placeholder)
justing5097f5d52007-04-20 00:08:50 +0000778 insertNodeAt(placeholder.get(), m_endingPosition);
mjsa0fe4042005-05-13 08:37:15 +0000779
justing92cd9eb2006-12-06 21:01:30 +0000780 rebalanceWhitespaceAt(m_endingPosition);
781
justin.garcia@apple.comd1eac9a2008-06-20 00:24:13 +0000782 calculateTypingStyleAfterDelete();
justing11ec52e2006-06-07 00:09:37 +0000783
eric@webkit.org87ea95c2009-02-09 21:43:24 +0000784 setEndingSelection(VisibleSelection(m_endingPosition, affinity));
mjsa0fe4042005-05-13 08:37:15 +0000785 clearTransientState();
mjsa0fe4042005-05-13 08:37:15 +0000786}
787
788EditAction DeleteSelectionCommand::editingAction() const
789{
790 // Note that DeleteSelectionCommand is also used when the user presses the Delete key,
791 // but in that case there's a TypingCommand that supplies the editingAction(), so
792 // the Undo menu correctly shows "Undo Typing"
793 return EditActionCut;
794}
795
justin.garcia@apple.comd1eac9a2008-06-20 00:24:13 +0000796// Normally deletion doesn't preserve the typing style that was present before it. For example,
797// type a character, Bold, then delete the character and start typing. The Bold typing style shouldn't
798// stick around. Deletion should preserve a typing style that *it* sets, however.
mjsa0fe4042005-05-13 08:37:15 +0000799bool DeleteSelectionCommand::preservesTypingStyle() const
800{
justin.garcia@apple.comd1eac9a2008-06-20 00:24:13 +0000801 return m_typingStyle;
mjsa0fe4042005-05-13 08:37:15 +0000802}
803
darinb9481ed2006-03-20 02:57:59 +0000804} // namespace WebCore