blob: 68d150612b0cee3c39711635c85b17d2de388ab2 [file] [log] [blame]
mjsa0fe4042005-05-13 08:37:15 +00001/*
mjs@apple.com92047332014-03-15 04:08:27 +00002 * Copyright (C) 2005 Apple Inc. All rights reserved.
mjsa0fe4042005-05-13 08:37:15 +00003 *
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 *
mjs@apple.com92047332014-03-15 04:08:27 +000013 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
mjsa0fe4042005-05-13 08:37:15 +000014 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
mjs@apple.com92047332014-03-15 04:08:27 +000016 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
mjsa0fe4042005-05-13 08:37:15 +000017 * 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"
jpu@apple.comfd2014c2011-05-12 00:54:08 +000030#include "DocumentMarkerController.h"
commit-queue@webkit.org02ea7a22017-03-03 06:35:25 +000031#include "Editing.h"
justing5745f952006-11-11 01:56:24 +000032#include "Editor.h"
justingf90e28d2007-07-30 18:25:19 +000033#include "EditorClient.h"
zalan@apple.com53862672017-02-10 22:36:12 +000034#include "ElementIterator.h"
mjs810e9762006-01-09 21:39:14 +000035#include "Frame.h"
darin@apple.comd385be42016-05-14 20:09:50 +000036#include "HTMLBRElement.h"
cdumez@apple.coma3bc43f2014-09-30 19:59:49 +000037#include "HTMLLinkElement.h"
darin98fa8b82006-03-20 08:03:57 +000038#include "HTMLNames.h"
cdumez@apple.coma3bc43f2014-09-30 19:59:49 +000039#include "HTMLStyleElement.h"
kangil.han@samsung.comb44a6812013-07-08 06:52:41 +000040#include "HTMLTableElement.h"
antti@apple.com5d47b582012-12-11 00:13:29 +000041#include "NodeTraversal.h"
hyatt@apple.comd885df72009-01-22 02:31:52 +000042#include "RenderTableCell.h"
antti@apple.come9c90162013-10-13 16:00:10 +000043#include "RenderText.h"
cdumez@apple.com426611c2014-10-20 05:17:06 +000044#include "RenderedDocumentMarker.h"
darin42563ac52007-01-22 17:28:57 +000045#include "Text.h"
tkent@chromium.org8c35c122013-03-06 13:00:14 +000046#include "VisibleUnits.h"
mjsa0fe4042005-05-13 08:37:15 +000047
darinbbe64662006-01-16 17:52:23 +000048namespace WebCore {
darinedbc5e42005-08-25 23:13:58 +000049
darinbbe64662006-01-16 17:52:23 +000050using namespace HTMLNames;
mjsa0fe4042005-05-13 08:37:15 +000051
justing94b6f9f2007-10-31 01:27:54 +000052static bool isTableRow(const Node* node)
justing187d2922007-07-17 21:05:21 +000053{
54 return node && node->hasTagName(trTag);
55}
56
justing13e16aa2007-07-17 21:55:05 +000057static bool isTableCellEmpty(Node* cell)
justing187d2922007-07-17 21:05:21 +000058{
59 ASSERT(isTableCell(cell));
leviw@chromium.org1f953622011-03-15 19:36:51 +000060 return VisiblePosition(firstPositionInNode(cell)) == VisiblePosition(lastPositionInNode(cell));
justing187d2922007-07-17 21:05:21 +000061}
62
justing13e16aa2007-07-17 21:55:05 +000063static bool isTableRowEmpty(Node* row)
justing187d2922007-07-17 21:05:21 +000064{
65 if (!isTableRow(row))
66 return false;
67
68 for (Node* child = row->firstChild(); child; child = child->nextSibling())
69 if (isTableCell(child) && !isTableCellEmpty(child))
70 return false;
71
72 return true;
73}
74
commit-queue@webkit.org2de6e052015-04-26 22:17:11 +000075DeleteSelectionCommand::DeleteSelectionCommand(Document& document, bool smartDelete, bool mergeBlocksAfterDelete, bool replace, bool expandForSpecialElements, bool sanitizeMarkup, EditAction editingAction)
76 : CompositeEditCommand(document, editingAction)
jpu@apple.comfd2014c2011-05-12 00:54:08 +000077 , m_hasSelectionToDelete(false)
78 , m_smartDelete(smartDelete)
79 , m_mergeBlocksAfterDelete(mergeBlocksAfterDelete)
80 , m_needPlaceholder(false)
81 , m_replace(replace)
82 , m_expandForSpecialElements(expandForSpecialElements)
83 , m_pruneStartBlockIfNecessary(false)
84 , m_startsAtEmptyLine(false)
enrica@apple.comd14c2a82012-05-07 23:30:08 +000085 , m_sanitizeMarkup(sanitizeMarkup)
mjsa0fe4042005-05-13 08:37:15 +000086{
87}
88
commit-queue@webkit.org2de6e052015-04-26 22:17:11 +000089DeleteSelectionCommand::DeleteSelectionCommand(const VisibleSelection& selection, bool smartDelete, bool mergeBlocksAfterDelete, bool replace, bool expandForSpecialElements, bool sanitizeMarkup, EditAction editingAction)
90 : CompositeEditCommand(selection.start().anchorNode()->document(), editingAction)
jpu@apple.comfd2014c2011-05-12 00:54:08 +000091 , m_hasSelectionToDelete(true)
92 , m_smartDelete(smartDelete)
93 , m_mergeBlocksAfterDelete(mergeBlocksAfterDelete)
94 , m_needPlaceholder(false)
95 , m_replace(replace)
96 , m_expandForSpecialElements(expandForSpecialElements)
97 , m_pruneStartBlockIfNecessary(false)
98 , m_startsAtEmptyLine(false)
enrica@apple.comd14c2a82012-05-07 23:30:08 +000099 , m_sanitizeMarkup(sanitizeMarkup)
jpu@apple.comfd2014c2011-05-12 00:54:08 +0000100 , m_selectionToDelete(selection)
mjsa0fe4042005-05-13 08:37:15 +0000101{
102}
103
justing113aaf32007-01-25 01:00:36 +0000104void DeleteSelectionCommand::initializeStartEnd(Position& start, Position& end)
darin35384062006-08-24 22:07:45 +0000105{
darin@apple.comd385be42016-05-14 20:09:50 +0000106 HTMLElement* startSpecialContainer = nullptr;
107 HTMLElement* endSpecialContainer = nullptr;
harrisondec5e092006-01-25 21:05:02 +0000108
justing113aaf32007-01-25 01:00:36 +0000109 start = m_selectionToDelete.start();
110 end = m_selectionToDelete.end();
justinge6f80c12006-06-09 04:57:51 +0000111
justing2112d432006-11-28 21:25:28 +0000112 // 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 +0000113 // but in these cases, we want to delete it, so manually expand the selection
rniwa@webkit.org62b16972011-02-21 09:28:48 +0000114 if (start.deprecatedNode()->hasTagName(hrTag))
115 start = positionBeforeNode(start.deprecatedNode());
116 else if (end.deprecatedNode()->hasTagName(hrTag))
117 end = positionAfterNode(end.deprecatedNode());
lweintraub635ec2a2006-07-13 18:28:28 +0000118
dbates@webkit.orgef42d382010-01-25 19:20:06 +0000119 // FIXME: This is only used so that moveParagraphs can avoid the bugs in special element expansion.
justing113aaf32007-01-25 01:00:36 +0000120 if (!m_expandForSpecialElements)
121 return;
122
justin.garcia@apple.com64125512008-02-20 17:48:44 +0000123 while (1) {
hs85.jeong@samsung.com135c82f2015-10-28 17:09:56 +0000124 startSpecialContainer = nullptr;
125 endSpecialContainer = nullptr;
justinge6f80c12006-06-09 04:57:51 +0000126
justing47d836d2007-06-08 00:02:06 +0000127 Position s = positionBeforeContainingSpecialElement(start, &startSpecialContainer);
128 Position e = positionAfterContainingSpecialElement(end, &endSpecialContainer);
justinge6f80c12006-06-09 04:57:51 +0000129
justing47d836d2007-06-08 00:02:06 +0000130 if (!startSpecialContainer && !endSpecialContainer)
justinge6f80c12006-06-09 04:57:51 +0000131 break;
commit-queue@webkit.orgc8932f82013-10-11 06:18:44 +0000132
133 m_mergeBlocksAfterDelete = false;
134
justin.garcia@apple.com64125512008-02-20 17:48:44 +0000135 if (VisiblePosition(start) != m_selectionToDelete.visibleStart() || VisiblePosition(end) != m_selectionToDelete.visibleEnd())
136 break;
eric@webkit.org05d6e932009-06-02 21:31:08 +0000137
eric@webkit.orgf74ae5c2009-09-09 23:25:47 +0000138 // If we're going to expand to include the startSpecialContainer, it must be fully selected.
139 if (startSpecialContainer && !endSpecialContainer && comparePositions(positionInParentAfterNode(startSpecialContainer), end) > -1)
justing47d836d2007-06-08 00:02:06 +0000140 break;
141
eric@webkit.org05d6e932009-06-02 21:31:08 +0000142 // If we're going to expand to include the endSpecialContainer, it must be fully selected.
eric@webkit.orgf74ae5c2009-09-09 23:25:47 +0000143 if (endSpecialContainer && !startSpecialContainer && comparePositions(start, positionInParentBeforeNode(endSpecialContainer)) > -1)
justing47d836d2007-06-08 00:02:06 +0000144 break;
eric@webkit.orgf74ae5c2009-09-09 23:25:47 +0000145
justing47d836d2007-06-08 00:02:06 +0000146 if (startSpecialContainer && startSpecialContainer->isDescendantOf(endSpecialContainer))
justing2bdb03e2006-11-30 02:54:19 +0000147 // Don't adjust the end yet, it is the end of a special element that contains the start
148 // special element (which may or may not be fully selected).
149 start = s;
justing47d836d2007-06-08 00:02:06 +0000150 else if (endSpecialContainer && endSpecialContainer->isDescendantOf(startSpecialContainer))
justing2bdb03e2006-11-30 02:54:19 +0000151 // Don't adjust the start yet, it is the start of a special element that contains the end
152 // special element (which may or may not be fully selected).
153 end = e;
154 else {
155 start = s;
156 end = e;
157 }
harrisondec5e092006-01-25 21:05:02 +0000158 }
harrisondec5e092006-01-25 21:05:02 +0000159}
160
ojan@chromium.org078aa802010-03-10 00:17:11 +0000161void DeleteSelectionCommand::setStartingSelectionOnSmartDelete(const Position& start, const Position& end)
ojan@chromium.org77dae9d2010-03-09 23:41:38 +0000162{
163 VisiblePosition newBase;
164 VisiblePosition newExtent;
165 if (startingSelection().isBaseFirst()) {
166 newBase = start;
167 newExtent = end;
168 } else {
169 newBase = end;
170 newExtent = start;
171 }
rniwa@webkit.orgeccfd3f2011-08-16 18:39:52 +0000172 setStartingSelection(VisibleSelection(newBase, newExtent, startingSelection().isDirectional()));
ojan@chromium.org77dae9d2010-03-09 23:41:38 +0000173}
174
megan_gardner@apple.com01af70af2019-03-21 17:03:17 +0000175bool DeleteSelectionCommand::shouldSmartDeleteParagraphSpacers()
176{
177 return document().editingBehavior().shouldSmartInsertDeleteParagraphs();
178}
179
180void DeleteSelectionCommand::smartDeleteParagraphSpacers()
181{
182 VisiblePosition visibleStart { m_upstreamStart };
183 VisiblePosition visibleEnd { m_downstreamEnd };
184 bool selectionEndsInParagraphSeperator = isEndOfParagraph(visibleEnd);
185 bool selectionEndIsEndOfContent = endOfEditableContent(visibleEnd) == visibleEnd;
186 bool startAndEndInSameUnsplittableElement = unsplittableElementForPosition(visibleStart.deepEquivalent()) == unsplittableElementForPosition(visibleEnd.deepEquivalent());
187 visibleStart = visibleStart.previous(CannotCrossEditingBoundary);
188 visibleEnd = visibleEnd.next(CannotCrossEditingBoundary);
189 bool previousPositionIsBlankParagraph = isBlankParagraph(visibleStart);
190 bool endPositonIsBlankParagraph = isBlankParagraph(visibleEnd);
191 bool hasBlankParagraphAfterEndOrIsEndOfContent = !selectionEndIsEndOfContent && (endPositonIsBlankParagraph || selectionEndsInParagraphSeperator);
192 if (startAndEndInSameUnsplittableElement && previousPositionIsBlankParagraph && hasBlankParagraphAfterEndOrIsEndOfContent) {
193 m_needPlaceholder = false;
194 Position position;
195 if (endPositonIsBlankParagraph)
196 position = startOfNextParagraph(startOfNextParagraph(m_downstreamEnd)).deepEquivalent();
197 else
198 position = VisiblePosition(m_downstreamEnd).next().deepEquivalent();
199 m_upstreamEnd = position.upstream();
200 m_downstreamEnd = position.downstream();
201 m_trailingWhitespace = m_downstreamEnd.trailingWhitespacePosition(VP_DEFAULT_AFFINITY);
202 setStartingSelectionOnSmartDelete(m_upstreamStart, m_downstreamEnd);
203 }
204 if (startAndEndInSameUnsplittableElement && selectionEndIsEndOfContent && previousPositionIsBlankParagraph && selectionEndsInParagraphSeperator) {
205 m_needPlaceholder = false;
206 VisiblePosition endOfParagraphBeforeStart = endOfParagraph(VisiblePosition { m_upstreamStart }.previous().previous());
207 Position position = endOfParagraphBeforeStart.deepEquivalent();
208 m_upstreamStart = position.upstream();
209 m_downstreamStart = position.downstream();
210 m_leadingWhitespace = m_upstreamStart.leadingWhitespacePosition(DOWNSTREAM);
211 setStartingSelectionOnSmartDelete(m_upstreamStart, m_upstreamEnd);
212 }
213}
214
wenson_hsieh@apple.com7cd854e2017-08-24 04:12:55 +0000215bool DeleteSelectionCommand::initializePositionData()
mjsa0fe4042005-05-13 08:37:15 +0000216{
justing113aaf32007-01-25 01:00:36 +0000217 Position start, end;
218 initializeStartEnd(start, end);
219
jiewen_tan@apple.com8a64a4a2015-11-05 00:46:34 +0000220 if (!isEditablePosition(start, ContentIsEditable))
221 start = firstEditablePositionAfterPositionInRoot(start, highestEditableRoot(start));
222 if (!isEditablePosition(end, ContentIsEditable))
223 end = lastEditablePositionBeforePositionInRoot(end, highestEditableRoot(start));
224
wenson_hsieh@apple.com7cd854e2017-08-24 04:12:55 +0000225 if (start.isNull() || end.isNull())
226 return false;
227
justing113aaf32007-01-25 01:00:36 +0000228 m_upstreamStart = start.upstream();
229 m_downstreamStart = start.downstream();
230 m_upstreamEnd = end.upstream();
231 m_downstreamEnd = end.downstream();
harrisondec5e092006-01-25 21:05:02 +0000232
justing06a653c2007-04-04 20:49:52 +0000233 m_startRoot = editableRootForPosition(start);
234 m_endRoot = editableRootForPosition(end);
235
justin.garcia@apple.comdeed90d2007-12-13 21:32:12 +0000236 m_startTableRow = enclosingNodeOfType(start, &isTableRow);
237 m_endTableRow = enclosingNodeOfType(end, &isTableRow);
justing187d2922007-07-17 21:05:21 +0000238
justingb7701502007-03-29 22:34:28 +0000239 // Don't move content out of a table cell.
justin.garcia@apple.comab87ef82008-05-29 20:37:53 +0000240 // If the cell is non-editable, enclosingNodeOfType won't return it by default, so
241 // tell that function that we don't care if it returns non-editable nodes.
rniwa@webkit.org45ecbaf2011-03-11 00:52:53 +0000242 Node* startCell = enclosingNodeOfType(m_upstreamStart, &isTableCell, CanCrossEditingBoundary);
243 Node* endCell = enclosingNodeOfType(m_downstreamEnd, &isTableCell, CanCrossEditingBoundary);
justing8f16cee2006-06-22 00:20:29 +0000244 // 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 +0000245 if (endCell && endCell != startCell)
justing8f16cee2006-06-22 00:20:29 +0000246 m_mergeBlocksAfterDelete = false;
247
justing11ec52e2006-06-07 00:09:37 +0000248 // 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 +0000249 // Sometimes they aren't (like when no merge is requested), so we must choose one position to hold the caret
250 // and receive the placeholder after deletion.
justing11ec52e2006-06-07 00:09:37 +0000251 VisiblePosition visibleEnd(m_downstreamEnd);
252 if (m_mergeBlocksAfterDelete && !isEndOfParagraph(visibleEnd))
253 m_endingPosition = m_downstreamEnd;
254 else
255 m_endingPosition = m_downstreamStart;
justin.garcia@apple.com7e95c7b2008-11-12 20:08:06 +0000256
257 // We don't want to merge into a block if it will mean changing the quote level of content after deleting
258 // selections that contain a whole number paragraphs plus a line break, since it is unclear to most users
259 // that such a selection actually ends at the start of the next paragraph. This matches TextEdit behavior
260 // for indented paragraphs.
adele@apple.come2bc16b2009-04-28 22:17:29 +0000261 // Only apply this rule if the endingSelection is a range selection. If it is a caret, then other operations have created
262 // 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.
263 if (numEnclosingMailBlockquotes(start) != numEnclosingMailBlockquotes(end)
264 && isStartOfParagraph(visibleEnd) && isStartOfParagraph(VisiblePosition(start))
265 && endingSelection().isRange()) {
justin.garcia@apple.com7e95c7b2008-11-12 20:08:06 +0000266 m_mergeBlocksAfterDelete = false;
267 m_pruneStartBlockIfNecessary = true;
268 }
justing8f16cee2006-06-22 00:20:29 +0000269
mjsa0fe4042005-05-13 08:37:15 +0000270 // Handle leading and trailing whitespace, as well as smart delete adjustments to the selection
darin9aa45a42006-01-15 23:32:02 +0000271 m_leadingWhitespace = m_upstreamStart.leadingWhitespacePosition(m_selectionToDelete.affinity());
mjsa0fe4042005-05-13 08:37:15 +0000272 m_trailingWhitespace = m_downstreamEnd.trailingWhitespacePosition(VP_DEFAULT_AFFINITY);
273
274 if (m_smartDelete) {
275
276 // skip smart delete if the selection to delete already starts or ends with whitespace
darin9aa45a42006-01-15 23:32:02 +0000277 Position pos = VisiblePosition(m_upstreamStart, m_selectionToDelete.affinity()).deepEquivalent();
mjsa0fe4042005-05-13 08:37:15 +0000278 bool skipSmartDelete = pos.trailingWhitespacePosition(VP_DEFAULT_AFFINITY, true).isNotNull();
279 if (!skipSmartDelete)
280 skipSmartDelete = m_downstreamEnd.leadingWhitespacePosition(VP_DEFAULT_AFFINITY, true).isNotNull();
281
282 // extend selection upstream if there is whitespace there
darin9aa45a42006-01-15 23:32:02 +0000283 bool hasLeadingWhitespaceBeforeAdjustment = m_upstreamStart.leadingWhitespacePosition(m_selectionToDelete.affinity(), true).isNotNull();
mjsa0fe4042005-05-13 08:37:15 +0000284 if (!skipSmartDelete && hasLeadingWhitespaceBeforeAdjustment) {
harrisondec5e092006-01-25 21:05:02 +0000285 VisiblePosition visiblePos = VisiblePosition(m_upstreamStart, VP_DEFAULT_AFFINITY).previous();
mjsa0fe4042005-05-13 08:37:15 +0000286 pos = visiblePos.deepEquivalent();
287 // Expand out one character upstream for smart delete and recalculate
288 // positions based on this change.
289 m_upstreamStart = pos.upstream();
290 m_downstreamStart = pos.downstream();
291 m_leadingWhitespace = m_upstreamStart.leadingWhitespacePosition(visiblePos.affinity());
ojan@chromium.org77dae9d2010-03-09 23:41:38 +0000292
293 setStartingSelectionOnSmartDelete(m_upstreamStart, m_upstreamEnd);
mjsa0fe4042005-05-13 08:37:15 +0000294 }
295
296 // trailing whitespace is only considered for smart delete if there is no leading
297 // whitespace, as in the case where you double-click the first word of a paragraph.
298 if (!skipSmartDelete && !hasLeadingWhitespaceBeforeAdjustment && m_downstreamEnd.trailingWhitespacePosition(VP_DEFAULT_AFFINITY, true).isNotNull()) {
299 // Expand out one character downstream for smart delete and recalculate
300 // positions based on this change.
harrisondec5e092006-01-25 21:05:02 +0000301 pos = VisiblePosition(m_downstreamEnd, VP_DEFAULT_AFFINITY).next().deepEquivalent();
mjsa0fe4042005-05-13 08:37:15 +0000302 m_upstreamEnd = pos.upstream();
303 m_downstreamEnd = pos.downstream();
304 m_trailingWhitespace = m_downstreamEnd.trailingWhitespacePosition(VP_DEFAULT_AFFINITY);
ojan@chromium.org77dae9d2010-03-09 23:41:38 +0000305
306 setStartingSelectionOnSmartDelete(m_downstreamStart, m_downstreamEnd);
mjsa0fe4042005-05-13 08:37:15 +0000307 }
megan_gardner@apple.com01af70af2019-03-21 17:03:17 +0000308
309 if (shouldSmartDeleteParagraphSpacers())
310 smartDeleteParagraphSpacers();
mjsa0fe4042005-05-13 08:37:15 +0000311 }
312
commit-queue@webkit.orgc56f5de2011-01-19 09:32:09 +0000313 // We must pass call parentAnchoredEquivalent on the positions since some editing positions
justin.garcia@apple.come1da2732008-06-25 21:22:12 +0000314 // that appear inside their nodes aren't really inside them. [hr, 0] is one example.
commit-queue@webkit.orgc56f5de2011-01-19 09:32:09 +0000315 // FIXME: parentAnchoredEquivalent should eventually be moved into enclosing element getters
justin.garcia@apple.come1da2732008-06-25 21:22:12 +0000316 // like the one below, since editing functions should obviously accept editing positions.
317 // FIXME: Passing false to enclosingNodeOfType tells it that it's OK to return a non-editable
318 // node. This was done to match existing behavior, but it seems wrong.
rniwa@webkit.org45ecbaf2011-03-11 00:52:53 +0000319 m_startBlock = enclosingNodeOfType(m_downstreamStart.parentAnchoredEquivalent(), &isBlock, CanCrossEditingBoundary);
320 m_endBlock = enclosingNodeOfType(m_upstreamEnd.parentAnchoredEquivalent(), &isBlock, CanCrossEditingBoundary);
wenson_hsieh@apple.com7cd854e2017-08-24 04:12:55 +0000321
322 return true;
mjsa0fe4042005-05-13 08:37:15 +0000323}
324
325void DeleteSelectionCommand::saveTypingStyleState()
326{
justin.garcia@apple.com64125512008-02-20 17:48:44 +0000327 // A common case is deleting characters that are all from the same text node. In
328 // that case, the style at the start of the selection before deletion will be the
329 // same as the style at the start of the selection after deletion (since those
330 // two positions will be identical). Therefore there is no need to save the
331 // typing style at the start of the selection, nor is there a reason to
332 // compute the style at the start of the selection after deletion (see the
333 // early return in calculateTypingStyleAfterDelete).
a.bah@samsung.com305f6852013-09-10 07:27:21 +0000334 // However, if typing style was previously set from another text node at the previous
335 // position (now deleted), we need to clear that style as well.
336 if (m_upstreamStart.deprecatedNode() == m_downstreamEnd.deprecatedNode() && m_upstreamStart.deprecatedNode()->isTextNode()) {
337 frame().selection().clearTypingStyle();
justin.garcia@apple.com64125512008-02-20 17:48:44 +0000338 return;
a.bah@samsung.com305f6852013-09-10 07:27:21 +0000339 }
rniwa@webkit.org1de555e2009-08-07 21:36:24 +0000340
mjsa0fe4042005-05-13 08:37:15 +0000341 // Figure out the typing style in effect before the delete is done.
commit-queue@webkit.orgecadd602013-11-07 03:49:36 +0000342 m_typingStyle = EditingStyle::create(m_selectionToDelete.start(), EditingStyle::EditingPropertiesInEffect);
rniwa@webkit.orgb70a5772011-09-22 19:25:54 +0000343 m_typingStyle->removeStyleAddedByNode(enclosingAnchorElement(m_selectionToDelete.start()));
rniwa@webkit.org1de555e2009-08-07 21:36:24 +0000344
justing503ccf12005-07-29 22:50:47 +0000345 // If we're deleting into a Mail blockquote, save the style at end() instead of start()
346 // We'll use this later in computeTypingStyleAfterDelete if we end up outside of a Mail blockquote
rniwa@webkit.org24bd1d32011-03-17 00:03:22 +0000347 if (enclosingNodeOfType(m_selectionToDelete.start(), isMailBlockquote))
rniwa@webkit.org7e06f4a2010-11-06 21:19:59 +0000348 m_deleteIntoBlockquoteStyle = EditingStyle::create(m_selectionToDelete.end());
rniwa@webkit.org1de555e2009-08-07 21:36:24 +0000349 else
cdumez@apple.comd839ea12015-07-04 19:42:18 +0000350 m_deleteIntoBlockquoteStyle = nullptr;
mjsa0fe4042005-05-13 08:37:15 +0000351}
352
353bool DeleteSelectionCommand::handleSpecialCaseBRDelete()
354{
rniwa@webkit.org3810eff2012-06-27 00:00:52 +0000355 Node* nodeAfterUpstreamStart = m_upstreamStart.computeNodeAfterPosition();
356 Node* nodeAfterDownstreamStart = m_downstreamStart.computeNodeAfterPosition();
357 // Upstream end will appear before BR due to canonicalization
358 Node* nodeAfterUpstreamEnd = m_upstreamEnd.computeNodeAfterPosition();
359
360 if (!nodeAfterUpstreamStart || !nodeAfterDownstreamStart)
361 return false;
362
mjsa0fe4042005-05-13 08:37:15 +0000363 // Check for special-case where the selection contains only a BR on a line by itself after another BR.
rniwa@webkit.org3810eff2012-06-27 00:00:52 +0000364 bool upstreamStartIsBR = nodeAfterUpstreamStart->hasTagName(brTag);
365 bool downstreamStartIsBR = nodeAfterDownstreamStart->hasTagName(brTag);
enrica@apple.comd2ba8152013-07-09 02:48:19 +0000366 // We should consider that the BR is on a line by itself also when we have <br><br>. This test should be true only
367 // when the two elements are siblings and should be false in a case like <div><br></div><br>.
368 bool isBROnLineByItself = upstreamStartIsBR && downstreamStartIsBR && ((nodeAfterDownstreamStart == nodeAfterUpstreamEnd) || (nodeAfterUpstreamEnd && nodeAfterUpstreamEnd->hasTagName(brTag) && nodeAfterUpstreamStart->nextSibling() == nodeAfterUpstreamEnd));
369
mjsa0fe4042005-05-13 08:37:15 +0000370 if (isBROnLineByItself) {
cdumez@apple.com2fecce72017-05-05 05:09:56 +0000371 removeNode(*nodeAfterDownstreamStart);
mjsa0fe4042005-05-13 08:37:15 +0000372 return true;
373 }
374
rniwa@webkit.org3810eff2012-06-27 00:00:52 +0000375 // FIXME: This code doesn't belong in here.
376 // We detect the case where the start is an empty line consisting of BR not wrapped in a block element.
a.bah@samsung.comb0247012013-08-23 11:38:26 +0000377 if (upstreamStartIsBR && downstreamStartIsBR
378 && !(isStartOfBlock(positionBeforeNode(nodeAfterUpstreamStart)) && isEndOfBlock(positionAfterNode(nodeAfterDownstreamStart)))
379 && (!nodeAfterUpstreamEnd || nodeAfterUpstreamEnd->hasTagName(brTag) || nodeAfterUpstreamEnd->previousSibling() != nodeAfterUpstreamStart)) {
mitz@apple.comfc292c32009-05-03 04:09:41 +0000380 m_startsAtEmptyLine = true;
harrison581c07b2006-11-06 18:08:52 +0000381 m_endingPosition = m_downstreamEnd;
382 }
383
mjsa0fe4042005-05-13 08:37:15 +0000384 return false;
385}
386
kalman@chromium.org4cb651d2011-03-31 07:38:10 +0000387static Position firstEditablePositionInNode(Node* node)
388{
389 ASSERT(node);
390 Node* next = node;
antti@apple.comf02be1e2013-12-21 18:51:04 +0000391 while (next && !next->hasEditableStyle())
cdumez@apple.comb7f48502015-01-26 22:36:36 +0000392 next = NodeTraversal::next(*next, node);
kalman@chromium.org4cb651d2011-03-31 07:38:10 +0000393 return next ? firstPositionInOrBeforeNode(next) : Position();
394}
395
zalan@apple.com53862672017-02-10 22:36:12 +0000396void DeleteSelectionCommand::insertBlockPlaceholderForTableCellIfNeeded(Element& element)
397{
398 // Make sure empty cell has some height.
399 auto* renderer = element.renderer();
400 if (!is<RenderTableCell>(renderer))
401 return;
402 if (downcast<RenderTableCell>(*renderer).contentHeight() > 0)
403 return;
404 insertBlockPlaceholder(firstEditablePositionInNode(&element));
405}
406
407void DeleteSelectionCommand::removeNodeUpdatingStates(Node& node, ShouldAssumeContentIsAlwaysEditable shouldAssumeContentIsAlwaysEditable)
408{
409 if (&node == m_startBlock && !isEndOfBlock(VisiblePosition(firstPositionInNode(m_startBlock.get())).previous()))
410 m_needPlaceholder = true;
411 else if (&node == m_endBlock && !isStartOfBlock(VisiblePosition(lastPositionInNode(m_startBlock.get())).next()))
412 m_needPlaceholder = true;
413
414 // FIXME: Update the endpoints of the range being deleted.
415 updatePositionForNodeRemoval(m_endingPosition, node);
416 updatePositionForNodeRemoval(m_leadingWhitespace, node);
417 updatePositionForNodeRemoval(m_trailingWhitespace, node);
418
cdumez@apple.com2fecce72017-05-05 05:09:56 +0000419 CompositeEditCommand::removeNode(node, shouldAssumeContentIsAlwaysEditable);
zalan@apple.com53862672017-02-10 22:36:12 +0000420}
421
422static inline bool shouldRemoveContentOnly(const Node& node)
423{
424 return isTableStructureNode(&node) || node.isRootEditableElement();
425}
426
cdumez@apple.com2fecce72017-05-05 05:09:56 +0000427void DeleteSelectionCommand::removeNode(Node& node, ShouldAssumeContentIsAlwaysEditable shouldAssumeContentIsAlwaysEditable)
justing11ec52e2006-06-07 00:09:37 +0000428{
cdumez@apple.com2fecce72017-05-05 05:09:56 +0000429 Ref<Node> protectedNode = node;
430 if (m_startRoot != m_endRoot && !(node.isDescendantOf(m_startRoot.get()) && node.isDescendantOf(m_endRoot.get()))) {
justing06a653c2007-04-04 20:49:52 +0000431 // If a node is not in both the start and end editable roots, remove it only if its inside an editable region.
cdumez@apple.com2fecce72017-05-05 05:09:56 +0000432 if (!node.parentNode()->hasEditableStyle()) {
justing06a653c2007-04-04 20:49:52 +0000433 // Don't remove non-editable atomic nodes.
cdumez@apple.com2fecce72017-05-05 05:09:56 +0000434 if (!node.firstChild())
justing06a653c2007-04-04 20:49:52 +0000435 return;
436 // Search this non-editable region for editable regions to empty.
cdumez@apple.com2fecce72017-05-05 05:09:56 +0000437 RefPtr<Node> child = node.firstChild();
justing06a653c2007-04-04 20:49:52 +0000438 while (child) {
439 RefPtr<Node> nextChild = child->nextSibling();
cdumez@apple.com2fecce72017-05-05 05:09:56 +0000440 removeNode(*child, shouldAssumeContentIsAlwaysEditable);
justing06a653c2007-04-04 20:49:52 +0000441 // Bail if nextChild is no longer node's child.
cdumez@apple.com2fecce72017-05-05 05:09:56 +0000442 if (nextChild && nextChild->parentNode() != &node)
justing06a653c2007-04-04 20:49:52 +0000443 return;
444 child = nextChild;
445 }
446
447 // Don't remove editable regions that are inside non-editable ones, just clear them.
448 return;
449 }
450 }
451
cdumez@apple.com2fecce72017-05-05 05:09:56 +0000452 if (shouldRemoveContentOnly(node)) {
justing11ec52e2006-06-07 00:09:37 +0000453 // Do not remove an element of table structure; remove its contents.
454 // Likewise for the root editable element.
cdumez@apple.com2fecce72017-05-05 05:09:56 +0000455 auto* child = NodeTraversal::next(node, &node);
justing11ec52e2006-06-07 00:09:37 +0000456 while (child) {
zalan@apple.com53862672017-02-10 22:36:12 +0000457 if (shouldRemoveContentOnly(*child)) {
cdumez@apple.com2fecce72017-05-05 05:09:56 +0000458 child = NodeTraversal::next(*child, &node);
zalan@apple.com53862672017-02-10 22:36:12 +0000459 continue;
460 }
461 auto* remove = child;
cdumez@apple.com2fecce72017-05-05 05:09:56 +0000462 child = NodeTraversal::nextSkippingChildren(*child, &node);
zalan@apple.com53862672017-02-10 22:36:12 +0000463 removeNodeUpdatingStates(*remove, shouldAssumeContentIsAlwaysEditable);
justing11ec52e2006-06-07 00:09:37 +0000464 }
465
cdumez@apple.com2fecce72017-05-05 05:09:56 +0000466 ASSERT(is<Element>(node));
467 auto& element = downcast<Element>(node);
akling@apple.comd455eb62013-09-01 05:30:31 +0000468 document().updateLayoutIgnorePendingStylesheets();
zalan@apple.com53862672017-02-10 22:36:12 +0000469 // Check if we need to insert a placeholder for descendant table cells.
470 auto* descendant = ElementTraversal::next(element, &element);
471 while (descendant) {
472 auto* placeholderCandidate = descendant;
473 descendant = ElementTraversal::next(*descendant, &element);
474 insertBlockPlaceholderForTableCellIfNeeded(*placeholderCandidate);
kalman@chromium.org4cb651d2011-03-31 07:38:10 +0000475 }
zalan@apple.com53862672017-02-10 22:36:12 +0000476 insertBlockPlaceholderForTableCellIfNeeded(element);
justing11ec52e2006-06-07 00:09:37 +0000477 return;
478 }
cdumez@apple.com2fecce72017-05-05 05:09:56 +0000479 removeNodeUpdatingStates(node, shouldAssumeContentIsAlwaysEditable);
justingf81641c2006-06-14 23:23:50 +0000480}
481
andersca@apple.comd8dda092009-01-23 19:52:19 +0000482static void updatePositionForTextRemoval(Node* node, int offset, int count, Position& position)
justingf81641c2006-06-14 23:23:50 +0000483{
rniwa@webkit.org186f5bb2011-01-18 21:33:34 +0000484 if (position.anchorType() != Position::PositionIsOffsetInAnchor || position.containerNode() != node)
485 return;
486
487 if (position.offsetInContainerNode() > offset + count)
488 position.moveToOffset(position.offsetInContainerNode() - count);
489 else if (position.offsetInContainerNode() > offset)
490 position.moveToOffset(offset);
justing11ec52e2006-06-07 00:09:37 +0000491}
492
cdumez@apple.comecdce032017-05-03 18:41:47 +0000493void DeleteSelectionCommand::deleteTextFromNode(Text& node, unsigned offset, unsigned count)
justing11ec52e2006-06-07 00:09:37 +0000494{
justingf81641c2006-06-14 23:23:50 +0000495 // FIXME: Update the endpoints of the range being deleted.
cdumez@apple.comecdce032017-05-03 18:41:47 +0000496 updatePositionForTextRemoval(&node, offset, count, m_endingPosition);
497 updatePositionForTextRemoval(&node, offset, count, m_leadingWhitespace);
498 updatePositionForTextRemoval(&node, offset, count, m_trailingWhitespace);
499 updatePositionForTextRemoval(&node, offset, count, m_downstreamEnd);
justing11ec52e2006-06-07 00:09:37 +0000500
501 CompositeEditCommand::deleteTextFromNode(node, offset, count);
502}
503
commit-queue@webkit.org72e63482012-08-18 00:31:00 +0000504void DeleteSelectionCommand::makeStylingElementsDirectChildrenOfEditableRootToPreventStyleLoss()
505{
506 RefPtr<Range> range = m_selectionToDelete.toNormalizedRange();
bfulgham@apple.comd80d75f2017-10-12 03:01:19 +0000507 RefPtr<Node> node = range ? range->firstNode() : nullptr;
commit-queue@webkit.org72e63482012-08-18 00:31:00 +0000508 while (node && node != range->pastLastNode()) {
cdumez@apple.comb7f48502015-01-26 22:36:36 +0000509 RefPtr<Node> nextNode = NodeTraversal::next(*node);
rniwa@webkit.orgbda54a02016-07-18 00:39:37 +0000510 if ((is<HTMLStyleElement>(*node) && !downcast<HTMLStyleElement>(*node).hasAttributeWithoutSynchronization(scopedAttr)) || is<HTMLLinkElement>(*node)) {
cdumez@apple.comb7f48502015-01-26 22:36:36 +0000511 nextNode = NodeTraversal::nextSkippingChildren(*node);
commit-queue@webkit.org72e63482012-08-18 00:31:00 +0000512 RefPtr<ContainerNode> rootEditableElement = node->rootEditableElement();
rniwa@webkit.orgd14c7ce2013-05-21 03:18:22 +0000513 if (rootEditableElement) {
cdumez@apple.com2fecce72017-05-05 05:09:56 +0000514 removeNode(*node);
cdumez@apple.comaac2e342017-05-02 17:41:46 +0000515 appendNode(*node, *rootEditableElement);
rniwa@webkit.orgd14c7ce2013-05-21 03:18:22 +0000516 }
commit-queue@webkit.org72e63482012-08-18 00:31:00 +0000517 }
518 node = nextNode;
519 }
520}
521
mjsa0fe4042005-05-13 08:37:15 +0000522void DeleteSelectionCommand::handleGeneralDelete()
523{
rniwa@webkit.org23ae78c2011-10-13 20:58:42 +0000524 if (m_upstreamStart.isNull())
525 return;
526
eric@webkit.org4e32ebc2009-04-30 01:09:57 +0000527 int startOffset = m_upstreamStart.deprecatedEditingOffset();
rniwa@webkit.org62b16972011-02-21 09:28:48 +0000528 Node* startNode = m_upstreamStart.deprecatedNode();
commit-queue@webkit.org72e63482012-08-18 00:31:00 +0000529
530 makeStylingElementsDirectChildrenOfEditableRootToPreventStyleLoss();
rniwa@webkit.org23ae78c2011-10-13 20:58:42 +0000531
lweintraub635ec2a2006-07-13 18:28:28 +0000532 // Never remove the start block unless it's a table, in which case we won't merge content in.
darin@apple.combc63aba2016-05-15 22:08:52 +0000533 if (startNode == m_startBlock && !startOffset && canHaveChildrenForEditing(*startNode) && !is<HTMLTableElement>(*startNode)) {
mjsa0fe4042005-05-13 08:37:15 +0000534 startOffset = 0;
cdumez@apple.comb7f48502015-01-26 22:36:36 +0000535 startNode = NodeTraversal::next(*startNode);
rniwa@webkit.org4df07a22012-01-31 20:41:05 +0000536 if (!startNode)
537 return;
justinge6f80c12006-06-09 04:57:51 +0000538 }
539
darin@apple.comd385be42016-05-14 20:09:50 +0000540 int startNodeCaretMaxOffset = caretMaxOffset(*startNode);
541 if (startOffset >= startNodeCaretMaxOffset && is<Text>(*startNode)) {
cdumez@apple.coma38df4d2014-09-29 17:20:18 +0000542 Text& text = downcast<Text>(*startNode);
darin@apple.comd385be42016-05-14 20:09:50 +0000543 if (text.length() > static_cast<unsigned>(startNodeCaretMaxOffset))
cdumez@apple.comecdce032017-05-03 18:41:47 +0000544 deleteTextFromNode(text, startNodeCaretMaxOffset, text.length() - startNodeCaretMaxOffset);
justinge6f80c12006-06-09 04:57:51 +0000545 }
546
darin@apple.comd385be42016-05-14 20:09:50 +0000547 if (startOffset >= lastOffsetForEditing(*startNode)) {
cdumez@apple.comb7f48502015-01-26 22:36:36 +0000548 startNode = NodeTraversal::nextSkippingChildren(*startNode);
mjsa0fe4042005-05-13 08:37:15 +0000549 startOffset = 0;
550 }
551
552 // Done adjusting the start. See if we're all done.
justing11ec52e2006-06-07 00:09:37 +0000553 if (!startNode)
mjsa0fe4042005-05-13 08:37:15 +0000554 return;
555
rniwa@webkit.org62b16972011-02-21 09:28:48 +0000556 if (startNode == m_downstreamEnd.deprecatedNode()) {
commit-queue@webkit.org2aa8d032010-09-22 20:43:26 +0000557 if (m_downstreamEnd.deprecatedEditingOffset() - startOffset > 0) {
cdumez@apple.coma9c60c92014-10-02 19:39:41 +0000558 if (is<Text>(*startNode)) {
mjsa0fe4042005-05-13 08:37:15 +0000559 // in a text node that needs to be trimmed
cdumez@apple.coma38df4d2014-09-29 17:20:18 +0000560 Text& text = downcast<Text>(*startNode);
cdumez@apple.comecdce032017-05-03 18:41:47 +0000561 deleteTextFromNode(text, startOffset, m_downstreamEnd.deprecatedEditingOffset() - startOffset);
mjsa0fe4042005-05-13 08:37:15 +0000562 } else {
cdumez@apple.comecdce032017-05-03 18:41:47 +0000563 removeChildrenInRange(*startNode, startOffset, m_downstreamEnd.deprecatedEditingOffset());
mjsa0fe4042005-05-13 08:37:15 +0000564 m_endingPosition = m_upstreamStart;
565 }
566 }
commit-queue@webkit.org2aa8d032010-09-22 20:43:26 +0000567
568 // The selection to delete is all in one node.
569 if (!startNode->renderer() || (!startOffset && m_downstreamEnd.atLastEditingPositionForNode()))
cdumez@apple.com2fecce72017-05-05 05:09:56 +0000570 removeNode(*startNode);
mjsa0fe4042005-05-13 08:37:15 +0000571 }
572 else {
rniwa@webkit.org62b16972011-02-21 09:28:48 +0000573 bool startNodeWasDescendantOfEndNode = m_upstreamStart.deprecatedNode()->isDescendantOf(m_downstreamEnd.deprecatedNode());
mjsa0fe4042005-05-13 08:37:15 +0000574 // The selection to delete spans more than one node.
justingd197d6b2007-08-25 01:20:33 +0000575 RefPtr<Node> node(startNode);
mjsa0fe4042005-05-13 08:37:15 +0000576
577 if (startOffset > 0) {
cdumez@apple.coma9c60c92014-10-02 19:39:41 +0000578 if (is<Text>(*startNode)) {
mjsa0fe4042005-05-13 08:37:15 +0000579 // in a text node that needs to be trimmed
cdumez@apple.coma38df4d2014-09-29 17:20:18 +0000580 Text& text = downcast<Text>(*node);
cdumez@apple.comecdce032017-05-03 18:41:47 +0000581 deleteTextFromNode(text, startOffset, text.length() - startOffset);
cdumez@apple.comb7f48502015-01-26 22:36:36 +0000582 node = NodeTraversal::next(*node);
mjsa0fe4042005-05-13 08:37:15 +0000583 } else {
cdumez@apple.comd9975172014-09-17 02:00:40 +0000584 node = startNode->traverseToChildAt(startOffset);
mjsa0fe4042005-05-13 08:37:15 +0000585 }
cdumez@apple.coma9c60c92014-10-02 19:39:41 +0000586 } else if (startNode == m_upstreamEnd.deprecatedNode() && is<Text>(*startNode)) {
cdumez@apple.coma38df4d2014-09-29 17:20:18 +0000587 Text& text = downcast<Text>(*m_upstreamEnd.deprecatedNode());
cdumez@apple.comecdce032017-05-03 18:41:47 +0000588 deleteTextFromNode(text, 0, m_upstreamEnd.deprecatedEditingOffset());
mjsa0fe4042005-05-13 08:37:15 +0000589 }
590
591 // handle deleting all nodes that are completely selected
rniwa@webkit.org62b16972011-02-21 09:28:48 +0000592 while (node && node != m_downstreamEnd.deprecatedNode()) {
rniwa@webkit.org186f5bb2011-01-18 21:33:34 +0000593 if (comparePositions(firstPositionInOrBeforeNode(node.get()), m_downstreamEnd) >= 0) {
antti@apple.comb498fe82012-12-12 03:12:23 +0000594 // NodeTraversal::nextSkippingChildren just blew past the end position, so stop deleting
cdumez@apple.comb7f48502015-01-26 22:36:36 +0000595 node = nullptr;
cdumez@apple.comca12b502016-11-17 06:24:13 +0000596 } else if (!m_downstreamEnd.deprecatedNode()->isDescendantOf(*node)) {
cdumez@apple.comb7f48502015-01-26 22:36:36 +0000597 RefPtr<Node> nextNode = NodeTraversal::nextSkippingChildren(*node);
mjsa0fe4042005-05-13 08:37:15 +0000598 // if we just removed a node from the end container, update end position so the
599 // check above will work
cdumez@apple.com2cbdf512014-11-11 00:02:57 +0000600 updatePositionForNodeRemoval(m_downstreamEnd, *node);
cdumez@apple.com2fecce72017-05-05 05:09:56 +0000601 removeNode(*node);
justing24754322007-07-09 20:27:05 +0000602 node = nextNode.get();
mjsa0fe4042005-05-13 08:37:15 +0000603 } else {
justing11ec52e2006-06-07 00:09:37 +0000604 Node* n = node->lastDescendant();
darin@apple.comd385be42016-05-14 20:09:50 +0000605 if (m_downstreamEnd.deprecatedNode() == n && m_downstreamEnd.deprecatedEditingOffset() >= caretMaxOffset(*n)) {
cdumez@apple.com2fecce72017-05-05 05:09:56 +0000606 removeNode(*node);
cdumez@apple.comb7f48502015-01-26 22:36:36 +0000607 node = nullptr;
justing95561262006-06-21 21:22:50 +0000608 } else
cdumez@apple.comb7f48502015-01-26 22:36:36 +0000609 node = NodeTraversal::next(*node);
mjsa0fe4042005-05-13 08:37:15 +0000610 }
611 }
mjsa0fe4042005-05-13 08:37:15 +0000612
rniwa@webkit.org1484cab2019-06-22 04:04:23 +0000613 if (!m_downstreamEnd.isNull() && !m_downstreamEnd.isOrphan() && m_downstreamEnd.deprecatedNode() != startNode
614 && !m_upstreamStart.deprecatedNode()->isDescendantOf(m_downstreamEnd.deprecatedNode())
615 && m_downstreamEnd.deprecatedEditingOffset() >= caretMinOffset(*m_downstreamEnd.deprecatedNode())) {
darin@apple.combc63aba2016-05-15 22:08:52 +0000616 if (m_downstreamEnd.atLastEditingPositionForNode() && !canHaveChildrenForEditing(*m_downstreamEnd.deprecatedNode())) {
justing395a18e2007-06-26 06:52:15 +0000617 // The node itself is fully selected, not just its contents. Delete it.
cdumez@apple.com2fecce72017-05-05 05:09:56 +0000618 removeNode(*m_downstreamEnd.deprecatedNode());
mjsa0fe4042005-05-13 08:37:15 +0000619 } else {
cdumez@apple.coma9c60c92014-10-02 19:39:41 +0000620 if (is<Text>(*m_downstreamEnd.deprecatedNode())) {
mjsa0fe4042005-05-13 08:37:15 +0000621 // in a text node that needs to be trimmed
cdumez@apple.coma38df4d2014-09-29 17:20:18 +0000622 Text& text = downcast<Text>(*m_downstreamEnd.deprecatedNode());
eric@webkit.org4e32ebc2009-04-30 01:09:57 +0000623 if (m_downstreamEnd.deprecatedEditingOffset() > 0) {
cdumez@apple.comecdce032017-05-03 18:41:47 +0000624 deleteTextFromNode(text, 0, m_downstreamEnd.deprecatedEditingOffset());
mjsa0fe4042005-05-13 08:37:15 +0000625 }
rniwa@webkit.org62b16972011-02-21 09:28:48 +0000626 // Remove children of m_downstreamEnd.deprecatedNode() that come after m_upstreamStart.
627 // Don't try to remove children if m_upstreamStart was inside m_downstreamEnd.deprecatedNode()
justingd197d6b2007-08-25 01:20:33 +0000628 // and m_upstreamStart has been removed from the document, because then we don't
629 // know how many children to remove.
630 // FIXME: Make m_upstreamStart a position we update as we remove content, then we can
631 // always know which children to remove.
cdumez@apple.com85cc2622017-02-02 21:29:15 +0000632 } else if (!(startNodeWasDescendantOfEndNode && !m_upstreamStart.anchorNode()->isConnected())) {
cdumez@apple.com37510702014-09-16 18:28:57 +0000633 unsigned offset = 0;
rniwa@webkit.org62b16972011-02-21 09:28:48 +0000634 if (m_upstreamStart.deprecatedNode()->isDescendantOf(m_downstreamEnd.deprecatedNode())) {
635 Node* n = m_upstreamStart.deprecatedNode();
636 while (n && n->parentNode() != m_downstreamEnd.deprecatedNode())
mjsa0fe4042005-05-13 08:37:15 +0000637 n = n->parentNode();
638 if (n)
cdumez@apple.com37510702014-09-16 18:28:57 +0000639 offset = n->computeNodeIndex() + 1;
mjsa0fe4042005-05-13 08:37:15 +0000640 }
cdumez@apple.comecdce032017-05-03 18:41:47 +0000641 removeChildrenInRange(*m_downstreamEnd.deprecatedNode(), offset, m_downstreamEnd.deprecatedEditingOffset());
rniwa@webkit.org0f812602011-06-24 17:45:00 +0000642 m_downstreamEnd = createLegacyEditingPosition(m_downstreamEnd.deprecatedNode(), offset);
mjsa0fe4042005-05-13 08:37:15 +0000643 }
644 }
645 }
646 }
647}
648
mjsa0fe4042005-05-13 08:37:15 +0000649void DeleteSelectionCommand::fixupWhitespace()
650{
akling@apple.comd455eb62013-09-01 05:30:31 +0000651 document().updateLayoutIgnorePendingStylesheets();
justingf81641c2006-06-14 23:23:50 +0000652 // FIXME: isRenderedCharacter should be removed, and we should use VisiblePosition::characterAfter and VisiblePosition::characterBefore
cdumez@apple.coma9c60c92014-10-02 19:39:41 +0000653 if (m_leadingWhitespace.isNotNull() && !m_leadingWhitespace.isRenderedCharacter() && is<Text>(*m_leadingWhitespace.deprecatedNode())) {
cdumez@apple.coma38df4d2014-09-29 17:20:18 +0000654 Text& textNode = downcast<Text>(*m_leadingWhitespace.deprecatedNode());
655 ASSERT(!textNode.renderer() || textNode.renderer()->style().collapseWhiteSpace());
cdumez@apple.comecdce032017-05-03 18:41:47 +0000656 replaceTextInNodePreservingMarkers(textNode, m_leadingWhitespace.deprecatedEditingOffset(), 1, nonBreakingSpaceString());
mjsa0fe4042005-05-13 08:37:15 +0000657 }
cdumez@apple.coma9c60c92014-10-02 19:39:41 +0000658 if (m_trailingWhitespace.isNotNull() && !m_trailingWhitespace.isRenderedCharacter() && is<Text>(*m_trailingWhitespace.deprecatedNode())) {
cdumez@apple.coma38df4d2014-09-29 17:20:18 +0000659 Text& textNode = downcast<Text>(*m_trailingWhitespace.deprecatedNode());
660 ASSERT(!textNode.renderer() || textNode.renderer()->style().collapseWhiteSpace());
cdumez@apple.comecdce032017-05-03 18:41:47 +0000661 replaceTextInNodePreservingMarkers(textNode, m_trailingWhitespace.deprecatedEditingOffset(), 1, nonBreakingSpaceString());
mjsa0fe4042005-05-13 08:37:15 +0000662 }
663}
664
justingf81641c2006-06-14 23:23:50 +0000665// If a selection starts in one block and ends in another, we have to merge to bring content before the
666// start together with content after the end.
justing217ded82006-04-04 20:52:10 +0000667void DeleteSelectionCommand::mergeParagraphs()
mjsa0fe4042005-05-13 08:37:15 +0000668{
justin.garcia@apple.com7e95c7b2008-11-12 20:08:06 +0000669 if (!m_mergeBlocksAfterDelete) {
670 if (m_pruneStartBlockIfNecessary) {
justin.garcia@apple.com7e95c7b2008-11-12 20:08:06 +0000671 // We aren't going to merge into the start block, so remove it if it's empty.
cdumez@apple.com2fecce72017-05-05 05:09:56 +0000672 prune(m_startBlock.get());
justin.garcia@apple.com7e95c7b2008-11-12 20:08:06 +0000673 // Removing the start block during a deletion is usually an indication that we need
674 // a placeholder, but not in this case.
675 m_needPlaceholder = false;
676 }
mjsa0fe4042005-05-13 08:37:15 +0000677 return;
justin.garcia@apple.com7e95c7b2008-11-12 20:08:06 +0000678 }
679
680 // It shouldn't have been asked to both try and merge content into the start block and prune it.
681 ASSERT(!m_pruneStartBlockIfNecessary);
mjsa0fe4042005-05-13 08:37:15 +0000682
justing217ded82006-04-04 20:52:10 +0000683 // FIXME: Deletion should adjust selection endpoints as it removes nodes so that we never get into this state (4099839).
rniwa@webkit.org1484cab2019-06-22 04:04:23 +0000684 if (m_downstreamEnd.isNull() || m_upstreamStart.isNull() || !m_downstreamEnd.anchorNode()->isConnected() || !m_upstreamStart.anchorNode()->isConnected())
justing217ded82006-04-04 20:52:10 +0000685 return;
cdumez@apple.com85cc2622017-02-02 21:29:15 +0000686
justingced67722006-05-01 21:43:03 +0000687 // FIXME: The deletion algorithm shouldn't let this happen.
eric@webkit.org05d6e932009-06-02 21:31:08 +0000688 if (comparePositions(m_upstreamStart, m_downstreamEnd) > 0)
justingced67722006-05-01 21:43:03 +0000689 return;
690
justin.garcia@apple.com68510a52009-04-23 00:54:42 +0000691 // There's nothing to merge.
692 if (m_upstreamStart == m_downstreamEnd)
justingced67722006-05-01 21:43:03 +0000693 return;
694
justing217ded82006-04-04 20:52:10 +0000695 VisiblePosition startOfParagraphToMove(m_downstreamEnd);
696 VisiblePosition mergeDestination(m_upstreamStart);
697
justin.garcia@apple.com60777c92007-11-14 22:33:07 +0000698 // m_downstreamEnd's block has been emptied out by deletion. There is no content inside of it to
699 // move, so just remove it.
inferno@chromium.orgb3d32042012-05-10 16:44:26 +0000700 Element* endBlock = enclosingBlock(m_downstreamEnd.deprecatedNode());
cdumez@apple.com2fecce72017-05-05 05:09:56 +0000701 if (!endBlock)
702 return;
703
704 if (!endBlock->contains(startOfParagraphToMove.deepEquivalent().deprecatedNode()) || !startOfParagraphToMove.deepEquivalent().deprecatedNode()) {
705 removeNode(*endBlock);
justin.garcia@apple.com60777c92007-11-14 22:33:07 +0000706 return;
707 }
708
justingced67722006-05-01 21:43:03 +0000709 // 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 +0000710 if (!mergeDestination.deepEquivalent().deprecatedNode() || !mergeDestination.deepEquivalent().deprecatedNode()->isDescendantOf(enclosingBlock(m_upstreamStart.containerNode())) || m_startsAtEmptyLine) {
cdumez@apple.comaac2e342017-05-02 17:41:46 +0000711 insertNodeAt(HTMLBRElement::create(document()), m_upstreamStart);
justingced67722006-05-01 21:43:03 +0000712 mergeDestination = VisiblePosition(m_upstreamStart);
713 }
harrisondec5e092006-01-25 21:05:02 +0000714
justingced67722006-05-01 21:43:03 +0000715 if (mergeDestination == startOfParagraphToMove)
mjsa0fe4042005-05-13 08:37:15 +0000716 return;
justing217ded82006-04-04 20:52:10 +0000717
commit-queue@webkit.org10809db2013-10-24 19:18:30 +0000718 VisiblePosition endOfParagraphToMove = endOfParagraph(startOfParagraphToMove, CanSkipOverEditingBoundary);
justing217ded82006-04-04 20:52:10 +0000719
darin2b0ff522006-05-03 19:32:21 +0000720 if (mergeDestination == endOfParagraphToMove)
721 return;
mjsa0fe4042005-05-13 08:37:15 +0000722
justinge809f932006-06-23 22:36:12 +0000723 // The rule for merging into an empty block is: only do so if its farther to the right.
724 // FIXME: Consider RTL.
mitz@apple.comfc292c32009-05-03 04:09:41 +0000725 if (!m_startsAtEmptyLine && isStartOfParagraph(mergeDestination) && startOfParagraphToMove.absoluteCaretBounds().x() > mergeDestination.absoluteCaretBounds().x()) {
rniwa@webkit.org62b16972011-02-21 09:28:48 +0000726 if (mergeDestination.deepEquivalent().downstream().deprecatedNode()->hasTagName(brTag)) {
cdumez@apple.com2fecce72017-05-05 05:09:56 +0000727 removeNodeAndPruneAncestors(*mergeDestination.deepEquivalent().downstream().deprecatedNode());
enrica@apple.com25b91e32010-01-11 18:04:58 +0000728 m_endingPosition = startOfParagraphToMove.deepEquivalent();
729 return;
730 }
justinge809f932006-06-23 22:36:12 +0000731 }
732
justin.garcia@apple.com619526a2009-11-05 05:25:24 +0000733 // Block images, tables and horizontal rules cannot be made inline with content at mergeDestination. If there is
734 // any (!isStartOfParagraph(mergeDestination)), don't merge, just move the caret to just before the selection we deleted.
735 // See https://bugs.webkit.org/show_bug.cgi?id=25439
rniwa@webkit.org62b16972011-02-21 09:28:48 +0000736 if (isRenderedAsNonInlineTableImageOrHR(startOfParagraphToMove.deepEquivalent().deprecatedNode()) && !isStartOfParagraph(mergeDestination)) {
justin.garcia@apple.com619526a2009-11-05 05:25:24 +0000737 m_endingPosition = m_upstreamStart;
738 return;
739 }
740
yusukesuzuki@slowstart.orgae5a8bd2018-12-22 06:37:39 +0000741 auto range = Range::create(document(), startOfParagraphToMove.deepEquivalent().parentAnchoredEquivalent(), endOfParagraphToMove.deepEquivalent().parentAnchoredEquivalent());
742 auto rangeToBeReplaced = Range::create(document(), mergeDestination.deepEquivalent().parentAnchoredEquivalent(), mergeDestination.deepEquivalent().parentAnchoredEquivalent());
743 if (!frame().editor().client()->shouldMoveRangeAfterDelete(range.ptr(), rangeToBeReplaced.ptr()))
justingf90e28d2007-07-30 18:25:19 +0000744 return;
745
justin.garcia@apple.com60777c92007-11-14 22:33:07 +0000746 // moveParagraphs will insert placeholders if it removes blocks that would require their use, don't let block
747 // removals that it does cause the insertion of *another* placeholder.
748 bool needPlaceholder = m_needPlaceholder;
rniwa@webkit.orgd34b7482010-08-12 02:05:11 +0000749 bool paragraphToMergeIsEmpty = (startOfParagraphToMove == endOfParagraphToMove);
750 moveParagraph(startOfParagraphToMove, endOfParagraphToMove, mergeDestination, false, !paragraphToMergeIsEmpty);
justin.garcia@apple.com60777c92007-11-14 22:33:07 +0000751 m_needPlaceholder = needPlaceholder;
justing93798f12006-06-08 04:04:38 +0000752 // The endingPosition was likely clobbered by the move, so recompute it (moveParagraph selects the moved paragraph).
justing11ec52e2006-06-07 00:09:37 +0000753 m_endingPosition = endingSelection().start();
mjsa0fe4042005-05-13 08:37:15 +0000754}
755
justing187d2922007-07-17 21:05:21 +0000756void DeleteSelectionCommand::removePreviouslySelectedEmptyTableRows()
757{
cdumez@apple.com85cc2622017-02-02 21:29:15 +0000758 if (m_endTableRow && m_endTableRow->isConnected() && m_endTableRow != m_startTableRow) {
justin.garcia@apple.com99a05542007-12-13 06:56:28 +0000759 Node* row = m_endTableRow->previousSibling();
760 while (row && row != m_startTableRow) {
justing187d2922007-07-17 21:05:21 +0000761 RefPtr<Node> previousRow = row->previousSibling();
762 if (isTableRowEmpty(row))
763 // Use a raw removeNode, instead of DeleteSelectionCommand's, because
764 // that won't remove rows, it only empties them in preparation for this function.
cdumez@apple.com2fecce72017-05-05 05:09:56 +0000765 CompositeEditCommand::removeNode(*row);
justing187d2922007-07-17 21:05:21 +0000766 row = previousRow.get();
767 }
768 }
769
justing7d4a4342007-10-13 00:40:48 +0000770 // Remove empty rows after the start row.
cdumez@apple.com85cc2622017-02-02 21:29:15 +0000771 if (m_startTableRow && m_startTableRow->isConnected() && m_startTableRow != m_endTableRow) {
justing187d2922007-07-17 21:05:21 +0000772 Node* row = m_startTableRow->nextSibling();
justin.garcia@apple.com99a05542007-12-13 06:56:28 +0000773 while (row && row != m_endTableRow) {
justing187d2922007-07-17 21:05:21 +0000774 RefPtr<Node> nextRow = row->nextSibling();
775 if (isTableRowEmpty(row))
cdumez@apple.com2fecce72017-05-05 05:09:56 +0000776 CompositeEditCommand::removeNode(*row);
justing187d2922007-07-17 21:05:21 +0000777 row = nextRow.get();
778 }
779 }
cdumez@apple.com85cc2622017-02-02 21:29:15 +0000780
781 if (m_endTableRow && m_endTableRow->isConnected() && m_endTableRow != m_startTableRow) {
justin.garcia@apple.com99a05542007-12-13 06:56:28 +0000782 if (isTableRowEmpty(m_endTableRow.get())) {
783 // Don't remove m_endTableRow if it's where we're putting the ending selection.
cdumez@apple.comca12b502016-11-17 06:24:13 +0000784 if (!m_endingPosition.deprecatedNode()->isDescendantOf(*m_endTableRow)) {
justin.garcia@apple.com99a05542007-12-13 06:56:28 +0000785 // FIXME: We probably shouldn't remove m_endTableRow unless it's fully selected, even if it is empty.
786 // We'll need to start adjusting the selection endpoints during deletion to know whether or not m_endTableRow
787 // was fully selected here.
cdumez@apple.com2fecce72017-05-05 05:09:56 +0000788 CompositeEditCommand::removeNode(*m_endTableRow);
justin.garcia@apple.com99a05542007-12-13 06:56:28 +0000789 }
790 }
cdumez@apple.com85cc2622017-02-02 21:29:15 +0000791 }
justing187d2922007-07-17 21:05:21 +0000792}
793
justin.garcia@apple.comd1eac9a2008-06-20 00:24:13 +0000794void DeleteSelectionCommand::calculateTypingStyleAfterDelete()
mjsa0fe4042005-05-13 08:37:15 +0000795{
justin.garcia@apple.com64125512008-02-20 17:48:44 +0000796 if (!m_typingStyle)
797 return;
798
mjsa0fe4042005-05-13 08:37:15 +0000799 // Compute the difference between the style before the delete and the style now
mjs810e9762006-01-09 21:39:14 +0000800 // after the delete has been done. Set this style on the frame, so other editing
mjsa0fe4042005-05-13 08:37:15 +0000801 // commands being composed with this one will work, and also cache it on the command,
mjs810e9762006-01-09 21:39:14 +0000802 // so the Frame::appliedEditing can set it after the whole composite command
mjsa0fe4042005-05-13 08:37:15 +0000803 // has completed.
justing503ccf12005-07-29 22:50:47 +0000804
eseidelef508982006-01-03 09:19:17 +0000805 // 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 +0000806 if (m_deleteIntoBlockquoteStyle && !enclosingNodeOfType(m_endingPosition, isMailBlockquote, CanCrossEditingBoundary))
eseidelef508982006-01-03 09:19:17 +0000807 m_typingStyle = m_deleteIntoBlockquoteStyle;
cdumez@apple.comd839ea12015-07-04 19:42:18 +0000808 m_deleteIntoBlockquoteStyle = nullptr;
rniwa@webkit.org1de555e2009-08-07 21:36:24 +0000809
rniwa@webkit.org7e06f4a2010-11-06 21:19:59 +0000810 m_typingStyle->prepareToApplyAt(m_endingPosition);
811 if (m_typingStyle->isEmpty())
cdumez@apple.comd839ea12015-07-04 19:42:18 +0000812 m_typingStyle = nullptr;
justin.garcia@apple.comd1eac9a2008-06-20 00:24:13 +0000813 // This is where we've deleted all traces of a style but not a whole paragraph (that's handled above).
814 // In this case if we start typing, the new characters should have the same style as the just deleted ones,
815 // but, if we change the selection, come back and start typing that style should be lost. Also see
816 // preserveTypingStyle() below.
achristensen@apple.com09e70aa2017-04-27 16:42:13 +0000817 frame().selection().setTypingStyle(m_typingStyle.copyRef());
mjsa0fe4042005-05-13 08:37:15 +0000818}
819
820void DeleteSelectionCommand::clearTransientState()
821{
eric@webkit.org87ea95c2009-02-09 21:43:24 +0000822 m_selectionToDelete = VisibleSelection();
mjsa0fe4042005-05-13 08:37:15 +0000823 m_upstreamStart.clear();
824 m_downstreamStart.clear();
825 m_upstreamEnd.clear();
826 m_downstreamEnd.clear();
827 m_endingPosition.clear();
828 m_leadingWhitespace.clear();
829 m_trailingWhitespace.clear();
mjsa0fe4042005-05-13 08:37:15 +0000830}
jpu@apple.comfd2014c2011-05-12 00:54:08 +0000831
832String DeleteSelectionCommand::originalStringForAutocorrectionAtBeginningOfSelection()
833{
834 if (!m_selectionToDelete.isRange())
835 return String();
836
837 VisiblePosition startOfSelection = m_selectionToDelete.start();
838 if (!isStartOfWord(startOfSelection))
839 return String();
840
philn@webkit.org3d7a0f42011-05-12 14:36:29 +0000841 VisiblePosition nextPosition = startOfSelection.next();
842 if (nextPosition.isNull())
843 return String();
844
cdumez@apple.com8f91d5b2017-05-01 18:08:07 +0000845 auto rangeOfFirstCharacter = Range::create(document(), startOfSelection.deepEquivalent(), nextPosition.deepEquivalent());
846 for (auto* marker : document().markers().markersInRange(rangeOfFirstCharacter, DocumentMarker::Autocorrected)) {
morrita@google.comc07dd582011-05-27 07:56:25 +0000847 int startOffset = marker->startOffset();
jpu@apple.comfd2014c2011-05-12 00:54:08 +0000848 if (startOffset == startOfSelection.deepEquivalent().offsetInContainerNode())
morrita@google.comc07dd582011-05-27 07:56:25 +0000849 return marker->description();
jpu@apple.comfd2014c2011-05-12 00:54:08 +0000850 }
851 return String();
852}
mjsa0fe4042005-05-13 08:37:15 +0000853
enrica@apple.com7abf3f92011-12-01 01:16:13 +0000854// This method removes div elements with no attributes that have only one child or no children at all.
855void DeleteSelectionCommand::removeRedundantBlocks()
856{
enrica@apple.comd5b2e4d2011-12-02 18:57:09 +0000857 Node* node = m_endingPosition.containerNode();
enrica@apple.com7abf3f92011-12-01 01:16:13 +0000858 Node* rootNode = node->rootEditableElement();
859
860 while (node != rootNode) {
enrica@apple.com48537332011-12-15 00:32:27 +0000861 if (isRemovableBlock(node)) {
enrica@apple.com7abf3f92011-12-01 01:16:13 +0000862 if (node == m_endingPosition.anchorNode())
cdumez@apple.com2cbdf512014-11-11 00:02:57 +0000863 updatePositionForNodeRemovalPreservingChildren(m_endingPosition, *node);
enrica@apple.com7abf3f92011-12-01 01:16:13 +0000864
cdumez@apple.com2fecce72017-05-05 05:09:56 +0000865 CompositeEditCommand::removeNodePreservingChildren(*node);
enrica@apple.com7abf3f92011-12-01 01:16:13 +0000866 node = m_endingPosition.anchorNode();
867 } else
enrica@apple.com48537332011-12-15 00:32:27 +0000868 node = node->parentNode();
enrica@apple.com7abf3f92011-12-01 01:16:13 +0000869 }
870}
871
mjsa0fe4042005-05-13 08:37:15 +0000872void DeleteSelectionCommand::doApply()
873{
874 // If selection has not been set to a custom selection when the command was created,
875 // use the current ending selection.
876 if (!m_hasSelectionToDelete)
877 m_selectionToDelete = endingSelection();
rniwa@webkit.orgd39a1e72010-08-25 19:19:26 +0000878
879 if (!m_selectionToDelete.isNonOrphanedRange())
mjsa0fe4042005-05-13 08:37:15 +0000880 return;
881
jpu@apple.comfd2014c2011-05-12 00:54:08 +0000882 String originalString = originalStringForAutocorrectionAtBeginningOfSelection();
883
adeleb2f2b662006-07-15 17:22:50 +0000884 // 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.
885 if (!m_replace) {
rniwa@webkit.org4f6c51f2011-06-30 09:14:17 +0000886 Element* textControl = enclosingTextFormControl(m_selectionToDelete.start());
887 if (textControl && textControl->focused())
akling@apple.com6f18f6d2013-09-01 14:57:23 +0000888 frame().editor().textWillBeDeletedInTextField(textControl);
adeleb2f2b662006-07-15 17:22:50 +0000889 }
adele174364d2006-04-13 01:50:04 +0000890
mjsa0fe4042005-05-13 08:37:15 +0000891 // save this to later make the selection with
darin9aa45a42006-01-15 23:32:02 +0000892 EAffinity affinity = m_selectionToDelete.affinity();
mjsa0fe4042005-05-13 08:37:15 +0000893
justing11ec52e2006-06-07 00:09:37 +0000894 Position downstreamEnd = m_selectionToDelete.end().downstream();
commit-queue@webkit.orgf3628722010-11-28 02:54:43 +0000895 m_needPlaceholder = isStartOfParagraph(m_selectionToDelete.visibleStart(), CanCrossEditingBoundary)
896 && isEndOfParagraph(m_selectionToDelete.visibleEnd(), CanCrossEditingBoundary)
commit-queue@webkit.org1d836ce2010-10-29 21:47:01 +0000897 && !lineBreakExistsAtVisiblePosition(m_selectionToDelete.visibleEnd());
justing552eeaf2007-04-20 01:53:22 +0000898 if (m_needPlaceholder) {
899 // Don't need a placeholder when deleting a selection that starts just before a table
900 // and ends inside it (we do need placeholders to hold open empty cells, but that's
901 // handled elsewhere).
cdumez@apple.comca12b502016-11-17 06:24:13 +0000902 if (auto* table = isLastPositionBeforeTable(m_selectionToDelete.visibleStart())) {
903 if (m_selectionToDelete.end().deprecatedNode()->isDescendantOf(*table))
justing552eeaf2007-04-20 01:53:22 +0000904 m_needPlaceholder = false;
cdumez@apple.comca12b502016-11-17 06:24:13 +0000905 }
justing552eeaf2007-04-20 01:53:22 +0000906 }
907
justing11ec52e2006-06-07 00:09:37 +0000908
mjsa0fe4042005-05-13 08:37:15 +0000909 // set up our state
wenson_hsieh@apple.com7cd854e2017-08-24 04:12:55 +0000910 if (!initializePositionData())
911 return;
harrisondff01cd2005-07-18 23:21:19 +0000912
hyatt0c05e102006-04-14 08:15:00 +0000913 // Delete any text that may hinder our ability to fixup whitespace after the delete
harrisondff01cd2005-07-18 23:21:19 +0000914 deleteInsignificantTextDownstream(m_trailingWhitespace);
915
916 saveTypingStyleState();
mjsa0fe4042005-05-13 08:37:15 +0000917
harrisone4b84c52005-07-18 18:14:59 +0000918 // deleting just a BR is handled specially, at least because we do not
919 // want to replace it with a placeholder BR!
920 if (handleSpecialCaseBRDelete()) {
justin.garcia@apple.comd1eac9a2008-06-20 00:24:13 +0000921 calculateTypingStyleAfterDelete();
rniwa@webkit.orgeccfd3f2011-08-16 18:39:52 +0000922 setEndingSelection(VisibleSelection(m_endingPosition, affinity, endingSelection().isDirectional()));
harrisone4b84c52005-07-18 18:14:59 +0000923 clearTransientState();
924 rebalanceWhitespace();
925 return;
926 }
mjsa0fe4042005-05-13 08:37:15 +0000927
harrisone4b84c52005-07-18 18:14:59 +0000928 handleGeneralDelete();
mjsa0fe4042005-05-13 08:37:15 +0000929
mjsa0fe4042005-05-13 08:37:15 +0000930 fixupWhitespace();
mjsa0fe4042005-05-13 08:37:15 +0000931
justingf81641c2006-06-14 23:23:50 +0000932 mergeParagraphs();
933
justing187d2922007-07-17 21:05:21 +0000934 removePreviouslySelectedEmptyTableRows();
935
darin@apple.comd385be42016-05-14 20:09:50 +0000936 if (m_needPlaceholder) {
enrica@apple.comd14c2a82012-05-07 23:30:08 +0000937 if (m_sanitizeMarkup)
938 removeRedundantBlocks();
darin@apple.comd385be42016-05-14 20:09:50 +0000939 insertNodeAt(HTMLBRElement::create(document()), m_endingPosition);
enrica@apple.com7abf3f92011-12-01 01:16:13 +0000940 }
mjsa0fe4042005-05-13 08:37:15 +0000941
enrica@apple.com2a79a802014-09-17 00:48:01 +0000942 bool shouldRebalaceWhiteSpace = true;
943 if (!frame().editor().behavior().shouldRebalanceWhiteSpacesInSecureField()) {
944 Node* node = m_endingPosition.deprecatedNode();
cdumez@apple.coma9c60c92014-10-02 19:39:41 +0000945 if (is<Text>(node)) {
cdumez@apple.coma38df4d2014-09-29 17:20:18 +0000946 Text& textNode = downcast<Text>(*node);
rniwa@webkit.org5c5bdba2016-07-21 19:41:47 +0000947 if (textNode.length() && textNode.renderer())
commit-queue@webkit.org1a4e6672018-05-21 16:55:45 +0000948 shouldRebalaceWhiteSpace = textNode.renderer()->style().textSecurity() == TextSecurity::None;
enrica@apple.com2a79a802014-09-17 00:48:01 +0000949 }
dbates@webkit.orgf435ee12014-01-10 17:06:52 +0000950 }
enrica@apple.com2a79a802014-09-17 00:48:01 +0000951 if (shouldRebalaceWhiteSpace)
dbates@webkit.orgf435ee12014-01-10 17:06:52 +0000952 rebalanceWhitespaceAt(m_endingPosition);
justing92cd9eb2006-12-06 21:01:30 +0000953
justin.garcia@apple.comd1eac9a2008-06-20 00:24:13 +0000954 calculateTypingStyleAfterDelete();
jpu@apple.comfd2014c2011-05-12 00:54:08 +0000955
akling@apple.com6f18f6d2013-09-01 14:57:23 +0000956 if (!originalString.isEmpty())
957 frame().editor().deletedAutocorrectionAtPosition(m_endingPosition, originalString);
jpu@apple.comfd2014c2011-05-12 00:54:08 +0000958
rniwa@webkit.orgeccfd3f2011-08-16 18:39:52 +0000959 setEndingSelection(VisibleSelection(m_endingPosition, affinity, endingSelection().isDirectional()));
mjsa0fe4042005-05-13 08:37:15 +0000960 clearTransientState();
mjsa0fe4042005-05-13 08:37:15 +0000961}
962
justin.garcia@apple.comd1eac9a2008-06-20 00:24:13 +0000963// Normally deletion doesn't preserve the typing style that was present before it. For example,
964// type a character, Bold, then delete the character and start typing. The Bold typing style shouldn't
965// stick around. Deletion should preserve a typing style that *it* sets, however.
mjsa0fe4042005-05-13 08:37:15 +0000966bool DeleteSelectionCommand::preservesTypingStyle() const
967{
justin.garcia@apple.comd1eac9a2008-06-20 00:24:13 +0000968 return m_typingStyle;
mjsa0fe4042005-05-13 08:37:15 +0000969}
970
darinb9481ed2006-03-20 02:57:59 +0000971} // namespace WebCore