mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 1 | /* |
mjs@apple.com | 9204733 | 2014-03-15 04:08:27 +0000 | [diff] [blame] | 2 | * Copyright (C) 2005 Apple Inc. All rights reserved. |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 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 | * |
mjs@apple.com | 9204733 | 2014-03-15 04:08:27 +0000 | [diff] [blame] | 13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 14 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
mjs@apple.com | 9204733 | 2014-03-15 04:08:27 +0000 | [diff] [blame] | 16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 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 | |
mjs | b64c50a | 2005-10-03 21:13:12 +0000 | [diff] [blame] | 26 | #include "config.h" |
darin | a68e043 | 2006-02-14 21:40:54 +0000 | [diff] [blame] | 27 | #include "DeleteSelectionCommand.h" |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 28 | |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 29 | #include "Document.h" |
jpu@apple.com | fd2014c | 2011-05-12 00:54:08 +0000 | [diff] [blame] | 30 | #include "DocumentMarkerController.h" |
commit-queue@webkit.org | 02ea7a2 | 2017-03-03 06:35:25 +0000 | [diff] [blame] | 31 | #include "Editing.h" |
justing | 5745f95 | 2006-11-11 01:56:24 +0000 | [diff] [blame] | 32 | #include "Editor.h" |
justing | f90e28d | 2007-07-30 18:25:19 +0000 | [diff] [blame] | 33 | #include "EditorClient.h" |
zalan@apple.com | 5386267 | 2017-02-10 22:36:12 +0000 | [diff] [blame] | 34 | #include "ElementIterator.h" |
mjs | 810e976 | 2006-01-09 21:39:14 +0000 | [diff] [blame] | 35 | #include "Frame.h" |
darin@apple.com | d385be4 | 2016-05-14 20:09:50 +0000 | [diff] [blame] | 36 | #include "HTMLBRElement.h" |
cdumez@apple.com | a3bc43f | 2014-09-30 19:59:49 +0000 | [diff] [blame] | 37 | #include "HTMLLinkElement.h" |
darin | 98fa8b8 | 2006-03-20 08:03:57 +0000 | [diff] [blame] | 38 | #include "HTMLNames.h" |
cdumez@apple.com | a3bc43f | 2014-09-30 19:59:49 +0000 | [diff] [blame] | 39 | #include "HTMLStyleElement.h" |
kangil.han@samsung.com | b44a681 | 2013-07-08 06:52:41 +0000 | [diff] [blame] | 40 | #include "HTMLTableElement.h" |
antti@apple.com | 5d47b58 | 2012-12-11 00:13:29 +0000 | [diff] [blame] | 41 | #include "NodeTraversal.h" |
hyatt@apple.com | d885df7 | 2009-01-22 02:31:52 +0000 | [diff] [blame] | 42 | #include "RenderTableCell.h" |
antti@apple.com | e9c9016 | 2013-10-13 16:00:10 +0000 | [diff] [blame] | 43 | #include "RenderText.h" |
cdumez@apple.com | 426611c | 2014-10-20 05:17:06 +0000 | [diff] [blame] | 44 | #include "RenderedDocumentMarker.h" |
darin | 42563ac5 | 2007-01-22 17:28:57 +0000 | [diff] [blame] | 45 | #include "Text.h" |
tkent@chromium.org | 8c35c12 | 2013-03-06 13:00:14 +0000 | [diff] [blame] | 46 | #include "VisibleUnits.h" |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 47 | |
darin | bbe6466 | 2006-01-16 17:52:23 +0000 | [diff] [blame] | 48 | namespace WebCore { |
darin | edbc5e4 | 2005-08-25 23:13:58 +0000 | [diff] [blame] | 49 | |
darin | bbe6466 | 2006-01-16 17:52:23 +0000 | [diff] [blame] | 50 | using namespace HTMLNames; |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 51 | |
justing | 94b6f9f | 2007-10-31 01:27:54 +0000 | [diff] [blame] | 52 | static bool isTableRow(const Node* node) |
justing | 187d292 | 2007-07-17 21:05:21 +0000 | [diff] [blame] | 53 | { |
| 54 | return node && node->hasTagName(trTag); |
| 55 | } |
| 56 | |
justing | 13e16aa | 2007-07-17 21:55:05 +0000 | [diff] [blame] | 57 | static bool isTableCellEmpty(Node* cell) |
justing | 187d292 | 2007-07-17 21:05:21 +0000 | [diff] [blame] | 58 | { |
| 59 | ASSERT(isTableCell(cell)); |
leviw@chromium.org | 1f95362 | 2011-03-15 19:36:51 +0000 | [diff] [blame] | 60 | return VisiblePosition(firstPositionInNode(cell)) == VisiblePosition(lastPositionInNode(cell)); |
justing | 187d292 | 2007-07-17 21:05:21 +0000 | [diff] [blame] | 61 | } |
| 62 | |
justing | 13e16aa | 2007-07-17 21:55:05 +0000 | [diff] [blame] | 63 | static bool isTableRowEmpty(Node* row) |
justing | 187d292 | 2007-07-17 21:05:21 +0000 | [diff] [blame] | 64 | { |
| 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.org | 2de6e05 | 2015-04-26 22:17:11 +0000 | [diff] [blame] | 75 | DeleteSelectionCommand::DeleteSelectionCommand(Document& document, bool smartDelete, bool mergeBlocksAfterDelete, bool replace, bool expandForSpecialElements, bool sanitizeMarkup, EditAction editingAction) |
| 76 | : CompositeEditCommand(document, editingAction) |
jpu@apple.com | fd2014c | 2011-05-12 00:54:08 +0000 | [diff] [blame] | 77 | , 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.com | d14c2a8 | 2012-05-07 23:30:08 +0000 | [diff] [blame] | 85 | , m_sanitizeMarkup(sanitizeMarkup) |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 86 | { |
| 87 | } |
| 88 | |
commit-queue@webkit.org | 2de6e05 | 2015-04-26 22:17:11 +0000 | [diff] [blame] | 89 | DeleteSelectionCommand::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.com | fd2014c | 2011-05-12 00:54:08 +0000 | [diff] [blame] | 91 | , 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.com | d14c2a8 | 2012-05-07 23:30:08 +0000 | [diff] [blame] | 99 | , m_sanitizeMarkup(sanitizeMarkup) |
jpu@apple.com | fd2014c | 2011-05-12 00:54:08 +0000 | [diff] [blame] | 100 | , m_selectionToDelete(selection) |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 101 | { |
| 102 | } |
| 103 | |
justing | 113aaf3 | 2007-01-25 01:00:36 +0000 | [diff] [blame] | 104 | void DeleteSelectionCommand::initializeStartEnd(Position& start, Position& end) |
darin | 3538406 | 2006-08-24 22:07:45 +0000 | [diff] [blame] | 105 | { |
darin@apple.com | d385be4 | 2016-05-14 20:09:50 +0000 | [diff] [blame] | 106 | HTMLElement* startSpecialContainer = nullptr; |
| 107 | HTMLElement* endSpecialContainer = nullptr; |
harrison | dec5e09 | 2006-01-25 21:05:02 +0000 | [diff] [blame] | 108 | |
justing | 113aaf3 | 2007-01-25 01:00:36 +0000 | [diff] [blame] | 109 | start = m_selectionToDelete.start(); |
| 110 | end = m_selectionToDelete.end(); |
justing | e6f80c1 | 2006-06-09 04:57:51 +0000 | [diff] [blame] | 111 | |
justing | 2112d43 | 2006-11-28 21:25:28 +0000 | [diff] [blame] | 112 | // 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, |
lweintraub | 635ec2a | 2006-07-13 18:28:28 +0000 | [diff] [blame] | 113 | // but in these cases, we want to delete it, so manually expand the selection |
rniwa@webkit.org | 62b1697 | 2011-02-21 09:28:48 +0000 | [diff] [blame] | 114 | if (start.deprecatedNode()->hasTagName(hrTag)) |
| 115 | start = positionBeforeNode(start.deprecatedNode()); |
| 116 | else if (end.deprecatedNode()->hasTagName(hrTag)) |
| 117 | end = positionAfterNode(end.deprecatedNode()); |
lweintraub | 635ec2a | 2006-07-13 18:28:28 +0000 | [diff] [blame] | 118 | |
dbates@webkit.org | ef42d38 | 2010-01-25 19:20:06 +0000 | [diff] [blame] | 119 | // FIXME: This is only used so that moveParagraphs can avoid the bugs in special element expansion. |
justing | 113aaf3 | 2007-01-25 01:00:36 +0000 | [diff] [blame] | 120 | if (!m_expandForSpecialElements) |
| 121 | return; |
| 122 | |
justin.garcia@apple.com | 6412551 | 2008-02-20 17:48:44 +0000 | [diff] [blame] | 123 | while (1) { |
hs85.jeong@samsung.com | 135c82f | 2015-10-28 17:09:56 +0000 | [diff] [blame] | 124 | startSpecialContainer = nullptr; |
| 125 | endSpecialContainer = nullptr; |
justing | e6f80c1 | 2006-06-09 04:57:51 +0000 | [diff] [blame] | 126 | |
justing | 47d836d | 2007-06-08 00:02:06 +0000 | [diff] [blame] | 127 | Position s = positionBeforeContainingSpecialElement(start, &startSpecialContainer); |
| 128 | Position e = positionAfterContainingSpecialElement(end, &endSpecialContainer); |
justing | e6f80c1 | 2006-06-09 04:57:51 +0000 | [diff] [blame] | 129 | |
justing | 47d836d | 2007-06-08 00:02:06 +0000 | [diff] [blame] | 130 | if (!startSpecialContainer && !endSpecialContainer) |
justing | e6f80c1 | 2006-06-09 04:57:51 +0000 | [diff] [blame] | 131 | break; |
commit-queue@webkit.org | c8932f8 | 2013-10-11 06:18:44 +0000 | [diff] [blame] | 132 | |
| 133 | m_mergeBlocksAfterDelete = false; |
| 134 | |
justin.garcia@apple.com | 6412551 | 2008-02-20 17:48:44 +0000 | [diff] [blame] | 135 | if (VisiblePosition(start) != m_selectionToDelete.visibleStart() || VisiblePosition(end) != m_selectionToDelete.visibleEnd()) |
| 136 | break; |
eric@webkit.org | 05d6e93 | 2009-06-02 21:31:08 +0000 | [diff] [blame] | 137 | |
eric@webkit.org | f74ae5c | 2009-09-09 23:25:47 +0000 | [diff] [blame] | 138 | // If we're going to expand to include the startSpecialContainer, it must be fully selected. |
| 139 | if (startSpecialContainer && !endSpecialContainer && comparePositions(positionInParentAfterNode(startSpecialContainer), end) > -1) |
justing | 47d836d | 2007-06-08 00:02:06 +0000 | [diff] [blame] | 140 | break; |
| 141 | |
eric@webkit.org | 05d6e93 | 2009-06-02 21:31:08 +0000 | [diff] [blame] | 142 | // If we're going to expand to include the endSpecialContainer, it must be fully selected. |
eric@webkit.org | f74ae5c | 2009-09-09 23:25:47 +0000 | [diff] [blame] | 143 | if (endSpecialContainer && !startSpecialContainer && comparePositions(start, positionInParentBeforeNode(endSpecialContainer)) > -1) |
justing | 47d836d | 2007-06-08 00:02:06 +0000 | [diff] [blame] | 144 | break; |
eric@webkit.org | f74ae5c | 2009-09-09 23:25:47 +0000 | [diff] [blame] | 145 | |
justing | 47d836d | 2007-06-08 00:02:06 +0000 | [diff] [blame] | 146 | if (startSpecialContainer && startSpecialContainer->isDescendantOf(endSpecialContainer)) |
justing | 2bdb03e | 2006-11-30 02:54:19 +0000 | [diff] [blame] | 147 | // 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; |
justing | 47d836d | 2007-06-08 00:02:06 +0000 | [diff] [blame] | 150 | else if (endSpecialContainer && endSpecialContainer->isDescendantOf(startSpecialContainer)) |
justing | 2bdb03e | 2006-11-30 02:54:19 +0000 | [diff] [blame] | 151 | // 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 | } |
harrison | dec5e09 | 2006-01-25 21:05:02 +0000 | [diff] [blame] | 158 | } |
harrison | dec5e09 | 2006-01-25 21:05:02 +0000 | [diff] [blame] | 159 | } |
| 160 | |
ojan@chromium.org | 078aa80 | 2010-03-10 00:17:11 +0000 | [diff] [blame] | 161 | void DeleteSelectionCommand::setStartingSelectionOnSmartDelete(const Position& start, const Position& end) |
ojan@chromium.org | 77dae9d | 2010-03-09 23:41:38 +0000 | [diff] [blame] | 162 | { |
| 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.org | eccfd3f | 2011-08-16 18:39:52 +0000 | [diff] [blame] | 172 | setStartingSelection(VisibleSelection(newBase, newExtent, startingSelection().isDirectional())); |
ojan@chromium.org | 77dae9d | 2010-03-09 23:41:38 +0000 | [diff] [blame] | 173 | } |
| 174 | |
megan_gardner@apple.com | 01af70af | 2019-03-21 17:03:17 +0000 | [diff] [blame] | 175 | bool DeleteSelectionCommand::shouldSmartDeleteParagraphSpacers() |
| 176 | { |
| 177 | return document().editingBehavior().shouldSmartInsertDeleteParagraphs(); |
| 178 | } |
| 179 | |
| 180 | void 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.com | 7cd854e | 2017-08-24 04:12:55 +0000 | [diff] [blame] | 215 | bool DeleteSelectionCommand::initializePositionData() |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 216 | { |
justing | 113aaf3 | 2007-01-25 01:00:36 +0000 | [diff] [blame] | 217 | Position start, end; |
| 218 | initializeStartEnd(start, end); |
| 219 | |
jiewen_tan@apple.com | 8a64a4a | 2015-11-05 00:46:34 +0000 | [diff] [blame] | 220 | 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.com | 7cd854e | 2017-08-24 04:12:55 +0000 | [diff] [blame] | 225 | if (start.isNull() || end.isNull()) |
| 226 | return false; |
| 227 | |
justing | 113aaf3 | 2007-01-25 01:00:36 +0000 | [diff] [blame] | 228 | m_upstreamStart = start.upstream(); |
| 229 | m_downstreamStart = start.downstream(); |
| 230 | m_upstreamEnd = end.upstream(); |
| 231 | m_downstreamEnd = end.downstream(); |
harrison | dec5e09 | 2006-01-25 21:05:02 +0000 | [diff] [blame] | 232 | |
justing | 06a653c | 2007-04-04 20:49:52 +0000 | [diff] [blame] | 233 | m_startRoot = editableRootForPosition(start); |
| 234 | m_endRoot = editableRootForPosition(end); |
| 235 | |
justin.garcia@apple.com | deed90d | 2007-12-13 21:32:12 +0000 | [diff] [blame] | 236 | m_startTableRow = enclosingNodeOfType(start, &isTableRow); |
| 237 | m_endTableRow = enclosingNodeOfType(end, &isTableRow); |
justing | 187d292 | 2007-07-17 21:05:21 +0000 | [diff] [blame] | 238 | |
justing | b770150 | 2007-03-29 22:34:28 +0000 | [diff] [blame] | 239 | // Don't move content out of a table cell. |
justin.garcia@apple.com | ab87ef8 | 2008-05-29 20:37:53 +0000 | [diff] [blame] | 240 | // 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.org | 45ecbaf | 2011-03-11 00:52:53 +0000 | [diff] [blame] | 242 | Node* startCell = enclosingNodeOfType(m_upstreamStart, &isTableCell, CanCrossEditingBoundary); |
| 243 | Node* endCell = enclosingNodeOfType(m_downstreamEnd, &isTableCell, CanCrossEditingBoundary); |
justing | 8f16cee | 2006-06-22 00:20:29 +0000 | [diff] [blame] | 244 | // FIXME: This isn't right. A borderless table with two rows and a single column would appear as two paragraphs. |
justing | b770150 | 2007-03-29 22:34:28 +0000 | [diff] [blame] | 245 | if (endCell && endCell != startCell) |
justing | 8f16cee | 2006-06-22 00:20:29 +0000 | [diff] [blame] | 246 | m_mergeBlocksAfterDelete = false; |
| 247 | |
justing | 11ec52e | 2006-06-07 00:09:37 +0000 | [diff] [blame] | 248 | // Usually the start and the end of the selection to delete are pulled together as a result of the deletion. |
justing | 93798f1 | 2006-06-08 04:04:38 +0000 | [diff] [blame] | 249 | // 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. |
justing | 11ec52e | 2006-06-07 00:09:37 +0000 | [diff] [blame] | 251 | 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.com | 7e95c7b | 2008-11-12 20:08:06 +0000 | [diff] [blame] | 256 | |
| 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.com | e2bc16b | 2009-04-28 22:17:29 +0000 | [diff] [blame] | 261 | // 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.com | 7e95c7b | 2008-11-12 20:08:06 +0000 | [diff] [blame] | 266 | m_mergeBlocksAfterDelete = false; |
| 267 | m_pruneStartBlockIfNecessary = true; |
| 268 | } |
justing | 8f16cee | 2006-06-22 00:20:29 +0000 | [diff] [blame] | 269 | |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 270 | // Handle leading and trailing whitespace, as well as smart delete adjustments to the selection |
darin | 9aa45a4 | 2006-01-15 23:32:02 +0000 | [diff] [blame] | 271 | m_leadingWhitespace = m_upstreamStart.leadingWhitespacePosition(m_selectionToDelete.affinity()); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 272 | 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 |
darin | 9aa45a4 | 2006-01-15 23:32:02 +0000 | [diff] [blame] | 277 | Position pos = VisiblePosition(m_upstreamStart, m_selectionToDelete.affinity()).deepEquivalent(); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 278 | 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 |
darin | 9aa45a4 | 2006-01-15 23:32:02 +0000 | [diff] [blame] | 283 | bool hasLeadingWhitespaceBeforeAdjustment = m_upstreamStart.leadingWhitespacePosition(m_selectionToDelete.affinity(), true).isNotNull(); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 284 | if (!skipSmartDelete && hasLeadingWhitespaceBeforeAdjustment) { |
harrison | dec5e09 | 2006-01-25 21:05:02 +0000 | [diff] [blame] | 285 | VisiblePosition visiblePos = VisiblePosition(m_upstreamStart, VP_DEFAULT_AFFINITY).previous(); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 286 | 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.org | 77dae9d | 2010-03-09 23:41:38 +0000 | [diff] [blame] | 292 | |
| 293 | setStartingSelectionOnSmartDelete(m_upstreamStart, m_upstreamEnd); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 294 | } |
| 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. |
harrison | dec5e09 | 2006-01-25 21:05:02 +0000 | [diff] [blame] | 301 | pos = VisiblePosition(m_downstreamEnd, VP_DEFAULT_AFFINITY).next().deepEquivalent(); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 302 | m_upstreamEnd = pos.upstream(); |
| 303 | m_downstreamEnd = pos.downstream(); |
| 304 | m_trailingWhitespace = m_downstreamEnd.trailingWhitespacePosition(VP_DEFAULT_AFFINITY); |
ojan@chromium.org | 77dae9d | 2010-03-09 23:41:38 +0000 | [diff] [blame] | 305 | |
| 306 | setStartingSelectionOnSmartDelete(m_downstreamStart, m_downstreamEnd); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 307 | } |
megan_gardner@apple.com | 01af70af | 2019-03-21 17:03:17 +0000 | [diff] [blame] | 308 | |
| 309 | if (shouldSmartDeleteParagraphSpacers()) |
| 310 | smartDeleteParagraphSpacers(); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 311 | } |
| 312 | |
commit-queue@webkit.org | c56f5de | 2011-01-19 09:32:09 +0000 | [diff] [blame] | 313 | // We must pass call parentAnchoredEquivalent on the positions since some editing positions |
justin.garcia@apple.com | e1da273 | 2008-06-25 21:22:12 +0000 | [diff] [blame] | 314 | // that appear inside their nodes aren't really inside them. [hr, 0] is one example. |
commit-queue@webkit.org | c56f5de | 2011-01-19 09:32:09 +0000 | [diff] [blame] | 315 | // FIXME: parentAnchoredEquivalent should eventually be moved into enclosing element getters |
justin.garcia@apple.com | e1da273 | 2008-06-25 21:22:12 +0000 | [diff] [blame] | 316 | // 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.org | 45ecbaf | 2011-03-11 00:52:53 +0000 | [diff] [blame] | 319 | m_startBlock = enclosingNodeOfType(m_downstreamStart.parentAnchoredEquivalent(), &isBlock, CanCrossEditingBoundary); |
| 320 | m_endBlock = enclosingNodeOfType(m_upstreamEnd.parentAnchoredEquivalent(), &isBlock, CanCrossEditingBoundary); |
wenson_hsieh@apple.com | 7cd854e | 2017-08-24 04:12:55 +0000 | [diff] [blame] | 321 | |
| 322 | return true; |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | void DeleteSelectionCommand::saveTypingStyleState() |
| 326 | { |
justin.garcia@apple.com | 6412551 | 2008-02-20 17:48:44 +0000 | [diff] [blame] | 327 | // 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.com | 305f685 | 2013-09-10 07:27:21 +0000 | [diff] [blame] | 334 | // 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.com | 6412551 | 2008-02-20 17:48:44 +0000 | [diff] [blame] | 338 | return; |
a.bah@samsung.com | 305f685 | 2013-09-10 07:27:21 +0000 | [diff] [blame] | 339 | } |
rniwa@webkit.org | 1de555e | 2009-08-07 21:36:24 +0000 | [diff] [blame] | 340 | |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 341 | // Figure out the typing style in effect before the delete is done. |
commit-queue@webkit.org | ecadd60 | 2013-11-07 03:49:36 +0000 | [diff] [blame] | 342 | m_typingStyle = EditingStyle::create(m_selectionToDelete.start(), EditingStyle::EditingPropertiesInEffect); |
rniwa@webkit.org | b70a577 | 2011-09-22 19:25:54 +0000 | [diff] [blame] | 343 | m_typingStyle->removeStyleAddedByNode(enclosingAnchorElement(m_selectionToDelete.start())); |
rniwa@webkit.org | 1de555e | 2009-08-07 21:36:24 +0000 | [diff] [blame] | 344 | |
justing | 503ccf1 | 2005-07-29 22:50:47 +0000 | [diff] [blame] | 345 | // 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.org | 24bd1d3 | 2011-03-17 00:03:22 +0000 | [diff] [blame] | 347 | if (enclosingNodeOfType(m_selectionToDelete.start(), isMailBlockquote)) |
rniwa@webkit.org | 7e06f4a | 2010-11-06 21:19:59 +0000 | [diff] [blame] | 348 | m_deleteIntoBlockquoteStyle = EditingStyle::create(m_selectionToDelete.end()); |
rniwa@webkit.org | 1de555e | 2009-08-07 21:36:24 +0000 | [diff] [blame] | 349 | else |
cdumez@apple.com | d839ea1 | 2015-07-04 19:42:18 +0000 | [diff] [blame] | 350 | m_deleteIntoBlockquoteStyle = nullptr; |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | bool DeleteSelectionCommand::handleSpecialCaseBRDelete() |
| 354 | { |
rniwa@webkit.org | 3810eff | 2012-06-27 00:00:52 +0000 | [diff] [blame] | 355 | 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 | |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 363 | // Check for special-case where the selection contains only a BR on a line by itself after another BR. |
rniwa@webkit.org | 3810eff | 2012-06-27 00:00:52 +0000 | [diff] [blame] | 364 | bool upstreamStartIsBR = nodeAfterUpstreamStart->hasTagName(brTag); |
| 365 | bool downstreamStartIsBR = nodeAfterDownstreamStart->hasTagName(brTag); |
enrica@apple.com | d2ba815 | 2013-07-09 02:48:19 +0000 | [diff] [blame] | 366 | // 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 | |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 370 | if (isBROnLineByItself) { |
cdumez@apple.com | 2fecce7 | 2017-05-05 05:09:56 +0000 | [diff] [blame] | 371 | removeNode(*nodeAfterDownstreamStart); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 372 | return true; |
| 373 | } |
| 374 | |
rniwa@webkit.org | 3810eff | 2012-06-27 00:00:52 +0000 | [diff] [blame] | 375 | // 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.com | b024701 | 2013-08-23 11:38:26 +0000 | [diff] [blame] | 377 | if (upstreamStartIsBR && downstreamStartIsBR |
| 378 | && !(isStartOfBlock(positionBeforeNode(nodeAfterUpstreamStart)) && isEndOfBlock(positionAfterNode(nodeAfterDownstreamStart))) |
| 379 | && (!nodeAfterUpstreamEnd || nodeAfterUpstreamEnd->hasTagName(brTag) || nodeAfterUpstreamEnd->previousSibling() != nodeAfterUpstreamStart)) { |
mitz@apple.com | fc292c3 | 2009-05-03 04:09:41 +0000 | [diff] [blame] | 380 | m_startsAtEmptyLine = true; |
harrison | 581c07b | 2006-11-06 18:08:52 +0000 | [diff] [blame] | 381 | m_endingPosition = m_downstreamEnd; |
| 382 | } |
| 383 | |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 384 | return false; |
| 385 | } |
| 386 | |
kalman@chromium.org | 4cb651d | 2011-03-31 07:38:10 +0000 | [diff] [blame] | 387 | static Position firstEditablePositionInNode(Node* node) |
| 388 | { |
| 389 | ASSERT(node); |
| 390 | Node* next = node; |
antti@apple.com | f02be1e | 2013-12-21 18:51:04 +0000 | [diff] [blame] | 391 | while (next && !next->hasEditableStyle()) |
cdumez@apple.com | b7f4850 | 2015-01-26 22:36:36 +0000 | [diff] [blame] | 392 | next = NodeTraversal::next(*next, node); |
kalman@chromium.org | 4cb651d | 2011-03-31 07:38:10 +0000 | [diff] [blame] | 393 | return next ? firstPositionInOrBeforeNode(next) : Position(); |
| 394 | } |
| 395 | |
zalan@apple.com | 5386267 | 2017-02-10 22:36:12 +0000 | [diff] [blame] | 396 | void 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 | |
| 407 | void 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.com | 2fecce7 | 2017-05-05 05:09:56 +0000 | [diff] [blame] | 419 | CompositeEditCommand::removeNode(node, shouldAssumeContentIsAlwaysEditable); |
zalan@apple.com | 5386267 | 2017-02-10 22:36:12 +0000 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | static inline bool shouldRemoveContentOnly(const Node& node) |
| 423 | { |
| 424 | return isTableStructureNode(&node) || node.isRootEditableElement(); |
| 425 | } |
| 426 | |
cdumez@apple.com | 2fecce7 | 2017-05-05 05:09:56 +0000 | [diff] [blame] | 427 | void DeleteSelectionCommand::removeNode(Node& node, ShouldAssumeContentIsAlwaysEditable shouldAssumeContentIsAlwaysEditable) |
justing | 11ec52e | 2006-06-07 00:09:37 +0000 | [diff] [blame] | 428 | { |
cdumez@apple.com | 2fecce7 | 2017-05-05 05:09:56 +0000 | [diff] [blame] | 429 | Ref<Node> protectedNode = node; |
| 430 | if (m_startRoot != m_endRoot && !(node.isDescendantOf(m_startRoot.get()) && node.isDescendantOf(m_endRoot.get()))) { |
justing | 06a653c | 2007-04-04 20:49:52 +0000 | [diff] [blame] | 431 | // If a node is not in both the start and end editable roots, remove it only if its inside an editable region. |
cdumez@apple.com | 2fecce7 | 2017-05-05 05:09:56 +0000 | [diff] [blame] | 432 | if (!node.parentNode()->hasEditableStyle()) { |
justing | 06a653c | 2007-04-04 20:49:52 +0000 | [diff] [blame] | 433 | // Don't remove non-editable atomic nodes. |
cdumez@apple.com | 2fecce7 | 2017-05-05 05:09:56 +0000 | [diff] [blame] | 434 | if (!node.firstChild()) |
justing | 06a653c | 2007-04-04 20:49:52 +0000 | [diff] [blame] | 435 | return; |
| 436 | // Search this non-editable region for editable regions to empty. |
cdumez@apple.com | 2fecce7 | 2017-05-05 05:09:56 +0000 | [diff] [blame] | 437 | RefPtr<Node> child = node.firstChild(); |
justing | 06a653c | 2007-04-04 20:49:52 +0000 | [diff] [blame] | 438 | while (child) { |
| 439 | RefPtr<Node> nextChild = child->nextSibling(); |
cdumez@apple.com | 2fecce7 | 2017-05-05 05:09:56 +0000 | [diff] [blame] | 440 | removeNode(*child, shouldAssumeContentIsAlwaysEditable); |
justing | 06a653c | 2007-04-04 20:49:52 +0000 | [diff] [blame] | 441 | // Bail if nextChild is no longer node's child. |
cdumez@apple.com | 2fecce7 | 2017-05-05 05:09:56 +0000 | [diff] [blame] | 442 | if (nextChild && nextChild->parentNode() != &node) |
justing | 06a653c | 2007-04-04 20:49:52 +0000 | [diff] [blame] | 443 | 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.com | 2fecce7 | 2017-05-05 05:09:56 +0000 | [diff] [blame] | 452 | if (shouldRemoveContentOnly(node)) { |
justing | 11ec52e | 2006-06-07 00:09:37 +0000 | [diff] [blame] | 453 | // Do not remove an element of table structure; remove its contents. |
| 454 | // Likewise for the root editable element. |
cdumez@apple.com | 2fecce7 | 2017-05-05 05:09:56 +0000 | [diff] [blame] | 455 | auto* child = NodeTraversal::next(node, &node); |
justing | 11ec52e | 2006-06-07 00:09:37 +0000 | [diff] [blame] | 456 | while (child) { |
zalan@apple.com | 5386267 | 2017-02-10 22:36:12 +0000 | [diff] [blame] | 457 | if (shouldRemoveContentOnly(*child)) { |
cdumez@apple.com | 2fecce7 | 2017-05-05 05:09:56 +0000 | [diff] [blame] | 458 | child = NodeTraversal::next(*child, &node); |
zalan@apple.com | 5386267 | 2017-02-10 22:36:12 +0000 | [diff] [blame] | 459 | continue; |
| 460 | } |
| 461 | auto* remove = child; |
cdumez@apple.com | 2fecce7 | 2017-05-05 05:09:56 +0000 | [diff] [blame] | 462 | child = NodeTraversal::nextSkippingChildren(*child, &node); |
zalan@apple.com | 5386267 | 2017-02-10 22:36:12 +0000 | [diff] [blame] | 463 | removeNodeUpdatingStates(*remove, shouldAssumeContentIsAlwaysEditable); |
justing | 11ec52e | 2006-06-07 00:09:37 +0000 | [diff] [blame] | 464 | } |
| 465 | |
cdumez@apple.com | 2fecce7 | 2017-05-05 05:09:56 +0000 | [diff] [blame] | 466 | ASSERT(is<Element>(node)); |
| 467 | auto& element = downcast<Element>(node); |
akling@apple.com | d455eb6 | 2013-09-01 05:30:31 +0000 | [diff] [blame] | 468 | document().updateLayoutIgnorePendingStylesheets(); |
zalan@apple.com | 5386267 | 2017-02-10 22:36:12 +0000 | [diff] [blame] | 469 | // 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.org | 4cb651d | 2011-03-31 07:38:10 +0000 | [diff] [blame] | 475 | } |
zalan@apple.com | 5386267 | 2017-02-10 22:36:12 +0000 | [diff] [blame] | 476 | insertBlockPlaceholderForTableCellIfNeeded(element); |
justing | 11ec52e | 2006-06-07 00:09:37 +0000 | [diff] [blame] | 477 | return; |
| 478 | } |
cdumez@apple.com | 2fecce7 | 2017-05-05 05:09:56 +0000 | [diff] [blame] | 479 | removeNodeUpdatingStates(node, shouldAssumeContentIsAlwaysEditable); |
justing | f81641c | 2006-06-14 23:23:50 +0000 | [diff] [blame] | 480 | } |
| 481 | |
andersca@apple.com | d8dda09 | 2009-01-23 19:52:19 +0000 | [diff] [blame] | 482 | static void updatePositionForTextRemoval(Node* node, int offset, int count, Position& position) |
justing | f81641c | 2006-06-14 23:23:50 +0000 | [diff] [blame] | 483 | { |
rniwa@webkit.org | 186f5bb | 2011-01-18 21:33:34 +0000 | [diff] [blame] | 484 | 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); |
justing | 11ec52e | 2006-06-07 00:09:37 +0000 | [diff] [blame] | 491 | } |
| 492 | |
cdumez@apple.com | ecdce03 | 2017-05-03 18:41:47 +0000 | [diff] [blame] | 493 | void DeleteSelectionCommand::deleteTextFromNode(Text& node, unsigned offset, unsigned count) |
justing | 11ec52e | 2006-06-07 00:09:37 +0000 | [diff] [blame] | 494 | { |
justing | f81641c | 2006-06-14 23:23:50 +0000 | [diff] [blame] | 495 | // FIXME: Update the endpoints of the range being deleted. |
cdumez@apple.com | ecdce03 | 2017-05-03 18:41:47 +0000 | [diff] [blame] | 496 | 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); |
justing | 11ec52e | 2006-06-07 00:09:37 +0000 | [diff] [blame] | 500 | |
| 501 | CompositeEditCommand::deleteTextFromNode(node, offset, count); |
| 502 | } |
| 503 | |
commit-queue@webkit.org | 72e6348 | 2012-08-18 00:31:00 +0000 | [diff] [blame] | 504 | void DeleteSelectionCommand::makeStylingElementsDirectChildrenOfEditableRootToPreventStyleLoss() |
| 505 | { |
| 506 | RefPtr<Range> range = m_selectionToDelete.toNormalizedRange(); |
bfulgham@apple.com | d80d75f | 2017-10-12 03:01:19 +0000 | [diff] [blame] | 507 | RefPtr<Node> node = range ? range->firstNode() : nullptr; |
commit-queue@webkit.org | 72e6348 | 2012-08-18 00:31:00 +0000 | [diff] [blame] | 508 | while (node && node != range->pastLastNode()) { |
cdumez@apple.com | b7f4850 | 2015-01-26 22:36:36 +0000 | [diff] [blame] | 509 | RefPtr<Node> nextNode = NodeTraversal::next(*node); |
rniwa@webkit.org | bda54a0 | 2016-07-18 00:39:37 +0000 | [diff] [blame] | 510 | if ((is<HTMLStyleElement>(*node) && !downcast<HTMLStyleElement>(*node).hasAttributeWithoutSynchronization(scopedAttr)) || is<HTMLLinkElement>(*node)) { |
cdumez@apple.com | b7f4850 | 2015-01-26 22:36:36 +0000 | [diff] [blame] | 511 | nextNode = NodeTraversal::nextSkippingChildren(*node); |
commit-queue@webkit.org | 72e6348 | 2012-08-18 00:31:00 +0000 | [diff] [blame] | 512 | RefPtr<ContainerNode> rootEditableElement = node->rootEditableElement(); |
rniwa@webkit.org | d14c7ce | 2013-05-21 03:18:22 +0000 | [diff] [blame] | 513 | if (rootEditableElement) { |
cdumez@apple.com | 2fecce7 | 2017-05-05 05:09:56 +0000 | [diff] [blame] | 514 | removeNode(*node); |
cdumez@apple.com | aac2e34 | 2017-05-02 17:41:46 +0000 | [diff] [blame] | 515 | appendNode(*node, *rootEditableElement); |
rniwa@webkit.org | d14c7ce | 2013-05-21 03:18:22 +0000 | [diff] [blame] | 516 | } |
commit-queue@webkit.org | 72e6348 | 2012-08-18 00:31:00 +0000 | [diff] [blame] | 517 | } |
| 518 | node = nextNode; |
| 519 | } |
| 520 | } |
| 521 | |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 522 | void DeleteSelectionCommand::handleGeneralDelete() |
| 523 | { |
rniwa@webkit.org | 23ae78c | 2011-10-13 20:58:42 +0000 | [diff] [blame] | 524 | if (m_upstreamStart.isNull()) |
| 525 | return; |
| 526 | |
eric@webkit.org | 4e32ebc | 2009-04-30 01:09:57 +0000 | [diff] [blame] | 527 | int startOffset = m_upstreamStart.deprecatedEditingOffset(); |
rniwa@webkit.org | 62b1697 | 2011-02-21 09:28:48 +0000 | [diff] [blame] | 528 | Node* startNode = m_upstreamStart.deprecatedNode(); |
commit-queue@webkit.org | 72e6348 | 2012-08-18 00:31:00 +0000 | [diff] [blame] | 529 | |
| 530 | makeStylingElementsDirectChildrenOfEditableRootToPreventStyleLoss(); |
rniwa@webkit.org | 23ae78c | 2011-10-13 20:58:42 +0000 | [diff] [blame] | 531 | |
lweintraub | 635ec2a | 2006-07-13 18:28:28 +0000 | [diff] [blame] | 532 | // Never remove the start block unless it's a table, in which case we won't merge content in. |
darin@apple.com | bc63aba | 2016-05-15 22:08:52 +0000 | [diff] [blame] | 533 | if (startNode == m_startBlock && !startOffset && canHaveChildrenForEditing(*startNode) && !is<HTMLTableElement>(*startNode)) { |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 534 | startOffset = 0; |
cdumez@apple.com | b7f4850 | 2015-01-26 22:36:36 +0000 | [diff] [blame] | 535 | startNode = NodeTraversal::next(*startNode); |
rniwa@webkit.org | 4df07a2 | 2012-01-31 20:41:05 +0000 | [diff] [blame] | 536 | if (!startNode) |
| 537 | return; |
justing | e6f80c1 | 2006-06-09 04:57:51 +0000 | [diff] [blame] | 538 | } |
| 539 | |
darin@apple.com | d385be4 | 2016-05-14 20:09:50 +0000 | [diff] [blame] | 540 | int startNodeCaretMaxOffset = caretMaxOffset(*startNode); |
| 541 | if (startOffset >= startNodeCaretMaxOffset && is<Text>(*startNode)) { |
cdumez@apple.com | a38df4d | 2014-09-29 17:20:18 +0000 | [diff] [blame] | 542 | Text& text = downcast<Text>(*startNode); |
darin@apple.com | d385be4 | 2016-05-14 20:09:50 +0000 | [diff] [blame] | 543 | if (text.length() > static_cast<unsigned>(startNodeCaretMaxOffset)) |
cdumez@apple.com | ecdce03 | 2017-05-03 18:41:47 +0000 | [diff] [blame] | 544 | deleteTextFromNode(text, startNodeCaretMaxOffset, text.length() - startNodeCaretMaxOffset); |
justing | e6f80c1 | 2006-06-09 04:57:51 +0000 | [diff] [blame] | 545 | } |
| 546 | |
darin@apple.com | d385be4 | 2016-05-14 20:09:50 +0000 | [diff] [blame] | 547 | if (startOffset >= lastOffsetForEditing(*startNode)) { |
cdumez@apple.com | b7f4850 | 2015-01-26 22:36:36 +0000 | [diff] [blame] | 548 | startNode = NodeTraversal::nextSkippingChildren(*startNode); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 549 | startOffset = 0; |
| 550 | } |
| 551 | |
| 552 | // Done adjusting the start. See if we're all done. |
justing | 11ec52e | 2006-06-07 00:09:37 +0000 | [diff] [blame] | 553 | if (!startNode) |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 554 | return; |
| 555 | |
rniwa@webkit.org | 62b1697 | 2011-02-21 09:28:48 +0000 | [diff] [blame] | 556 | if (startNode == m_downstreamEnd.deprecatedNode()) { |
commit-queue@webkit.org | 2aa8d03 | 2010-09-22 20:43:26 +0000 | [diff] [blame] | 557 | if (m_downstreamEnd.deprecatedEditingOffset() - startOffset > 0) { |
cdumez@apple.com | a9c60c9 | 2014-10-02 19:39:41 +0000 | [diff] [blame] | 558 | if (is<Text>(*startNode)) { |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 559 | // in a text node that needs to be trimmed |
cdumez@apple.com | a38df4d | 2014-09-29 17:20:18 +0000 | [diff] [blame] | 560 | Text& text = downcast<Text>(*startNode); |
cdumez@apple.com | ecdce03 | 2017-05-03 18:41:47 +0000 | [diff] [blame] | 561 | deleteTextFromNode(text, startOffset, m_downstreamEnd.deprecatedEditingOffset() - startOffset); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 562 | } else { |
cdumez@apple.com | ecdce03 | 2017-05-03 18:41:47 +0000 | [diff] [blame] | 563 | removeChildrenInRange(*startNode, startOffset, m_downstreamEnd.deprecatedEditingOffset()); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 564 | m_endingPosition = m_upstreamStart; |
| 565 | } |
| 566 | } |
commit-queue@webkit.org | 2aa8d03 | 2010-09-22 20:43:26 +0000 | [diff] [blame] | 567 | |
| 568 | // The selection to delete is all in one node. |
| 569 | if (!startNode->renderer() || (!startOffset && m_downstreamEnd.atLastEditingPositionForNode())) |
cdumez@apple.com | 2fecce7 | 2017-05-05 05:09:56 +0000 | [diff] [blame] | 570 | removeNode(*startNode); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 571 | } |
| 572 | else { |
rniwa@webkit.org | 62b1697 | 2011-02-21 09:28:48 +0000 | [diff] [blame] | 573 | bool startNodeWasDescendantOfEndNode = m_upstreamStart.deprecatedNode()->isDescendantOf(m_downstreamEnd.deprecatedNode()); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 574 | // The selection to delete spans more than one node. |
justing | d197d6b | 2007-08-25 01:20:33 +0000 | [diff] [blame] | 575 | RefPtr<Node> node(startNode); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 576 | |
| 577 | if (startOffset > 0) { |
cdumez@apple.com | a9c60c9 | 2014-10-02 19:39:41 +0000 | [diff] [blame] | 578 | if (is<Text>(*startNode)) { |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 579 | // in a text node that needs to be trimmed |
cdumez@apple.com | a38df4d | 2014-09-29 17:20:18 +0000 | [diff] [blame] | 580 | Text& text = downcast<Text>(*node); |
cdumez@apple.com | ecdce03 | 2017-05-03 18:41:47 +0000 | [diff] [blame] | 581 | deleteTextFromNode(text, startOffset, text.length() - startOffset); |
cdumez@apple.com | b7f4850 | 2015-01-26 22:36:36 +0000 | [diff] [blame] | 582 | node = NodeTraversal::next(*node); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 583 | } else { |
cdumez@apple.com | d997517 | 2014-09-17 02:00:40 +0000 | [diff] [blame] | 584 | node = startNode->traverseToChildAt(startOffset); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 585 | } |
cdumez@apple.com | a9c60c9 | 2014-10-02 19:39:41 +0000 | [diff] [blame] | 586 | } else if (startNode == m_upstreamEnd.deprecatedNode() && is<Text>(*startNode)) { |
cdumez@apple.com | a38df4d | 2014-09-29 17:20:18 +0000 | [diff] [blame] | 587 | Text& text = downcast<Text>(*m_upstreamEnd.deprecatedNode()); |
cdumez@apple.com | ecdce03 | 2017-05-03 18:41:47 +0000 | [diff] [blame] | 588 | deleteTextFromNode(text, 0, m_upstreamEnd.deprecatedEditingOffset()); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 589 | } |
| 590 | |
| 591 | // handle deleting all nodes that are completely selected |
rniwa@webkit.org | 62b1697 | 2011-02-21 09:28:48 +0000 | [diff] [blame] | 592 | while (node && node != m_downstreamEnd.deprecatedNode()) { |
rniwa@webkit.org | 186f5bb | 2011-01-18 21:33:34 +0000 | [diff] [blame] | 593 | if (comparePositions(firstPositionInOrBeforeNode(node.get()), m_downstreamEnd) >= 0) { |
antti@apple.com | b498fe8 | 2012-12-12 03:12:23 +0000 | [diff] [blame] | 594 | // NodeTraversal::nextSkippingChildren just blew past the end position, so stop deleting |
cdumez@apple.com | b7f4850 | 2015-01-26 22:36:36 +0000 | [diff] [blame] | 595 | node = nullptr; |
cdumez@apple.com | ca12b50 | 2016-11-17 06:24:13 +0000 | [diff] [blame] | 596 | } else if (!m_downstreamEnd.deprecatedNode()->isDescendantOf(*node)) { |
cdumez@apple.com | b7f4850 | 2015-01-26 22:36:36 +0000 | [diff] [blame] | 597 | RefPtr<Node> nextNode = NodeTraversal::nextSkippingChildren(*node); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 598 | // if we just removed a node from the end container, update end position so the |
| 599 | // check above will work |
cdumez@apple.com | 2cbdf51 | 2014-11-11 00:02:57 +0000 | [diff] [blame] | 600 | updatePositionForNodeRemoval(m_downstreamEnd, *node); |
cdumez@apple.com | 2fecce7 | 2017-05-05 05:09:56 +0000 | [diff] [blame] | 601 | removeNode(*node); |
justing | 2475432 | 2007-07-09 20:27:05 +0000 | [diff] [blame] | 602 | node = nextNode.get(); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 603 | } else { |
justing | 11ec52e | 2006-06-07 00:09:37 +0000 | [diff] [blame] | 604 | Node* n = node->lastDescendant(); |
darin@apple.com | d385be4 | 2016-05-14 20:09:50 +0000 | [diff] [blame] | 605 | if (m_downstreamEnd.deprecatedNode() == n && m_downstreamEnd.deprecatedEditingOffset() >= caretMaxOffset(*n)) { |
cdumez@apple.com | 2fecce7 | 2017-05-05 05:09:56 +0000 | [diff] [blame] | 606 | removeNode(*node); |
cdumez@apple.com | b7f4850 | 2015-01-26 22:36:36 +0000 | [diff] [blame] | 607 | node = nullptr; |
justing | 9556126 | 2006-06-21 21:22:50 +0000 | [diff] [blame] | 608 | } else |
cdumez@apple.com | b7f4850 | 2015-01-26 22:36:36 +0000 | [diff] [blame] | 609 | node = NodeTraversal::next(*node); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 610 | } |
| 611 | } |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 612 | |
rniwa@webkit.org | 1484cab | 2019-06-22 04:04:23 +0000 | [diff] [blame] | 613 | 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.com | bc63aba | 2016-05-15 22:08:52 +0000 | [diff] [blame] | 616 | if (m_downstreamEnd.atLastEditingPositionForNode() && !canHaveChildrenForEditing(*m_downstreamEnd.deprecatedNode())) { |
justing | 395a18e | 2007-06-26 06:52:15 +0000 | [diff] [blame] | 617 | // The node itself is fully selected, not just its contents. Delete it. |
cdumez@apple.com | 2fecce7 | 2017-05-05 05:09:56 +0000 | [diff] [blame] | 618 | removeNode(*m_downstreamEnd.deprecatedNode()); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 619 | } else { |
cdumez@apple.com | a9c60c9 | 2014-10-02 19:39:41 +0000 | [diff] [blame] | 620 | if (is<Text>(*m_downstreamEnd.deprecatedNode())) { |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 621 | // in a text node that needs to be trimmed |
cdumez@apple.com | a38df4d | 2014-09-29 17:20:18 +0000 | [diff] [blame] | 622 | Text& text = downcast<Text>(*m_downstreamEnd.deprecatedNode()); |
eric@webkit.org | 4e32ebc | 2009-04-30 01:09:57 +0000 | [diff] [blame] | 623 | if (m_downstreamEnd.deprecatedEditingOffset() > 0) { |
cdumez@apple.com | ecdce03 | 2017-05-03 18:41:47 +0000 | [diff] [blame] | 624 | deleteTextFromNode(text, 0, m_downstreamEnd.deprecatedEditingOffset()); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 625 | } |
rniwa@webkit.org | 62b1697 | 2011-02-21 09:28:48 +0000 | [diff] [blame] | 626 | // 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() |
justing | d197d6b | 2007-08-25 01:20:33 +0000 | [diff] [blame] | 628 | // 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.com | 85cc262 | 2017-02-02 21:29:15 +0000 | [diff] [blame] | 632 | } else if (!(startNodeWasDescendantOfEndNode && !m_upstreamStart.anchorNode()->isConnected())) { |
cdumez@apple.com | 3751070 | 2014-09-16 18:28:57 +0000 | [diff] [blame] | 633 | unsigned offset = 0; |
rniwa@webkit.org | 62b1697 | 2011-02-21 09:28:48 +0000 | [diff] [blame] | 634 | if (m_upstreamStart.deprecatedNode()->isDescendantOf(m_downstreamEnd.deprecatedNode())) { |
| 635 | Node* n = m_upstreamStart.deprecatedNode(); |
| 636 | while (n && n->parentNode() != m_downstreamEnd.deprecatedNode()) |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 637 | n = n->parentNode(); |
| 638 | if (n) |
cdumez@apple.com | 3751070 | 2014-09-16 18:28:57 +0000 | [diff] [blame] | 639 | offset = n->computeNodeIndex() + 1; |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 640 | } |
cdumez@apple.com | ecdce03 | 2017-05-03 18:41:47 +0000 | [diff] [blame] | 641 | removeChildrenInRange(*m_downstreamEnd.deprecatedNode(), offset, m_downstreamEnd.deprecatedEditingOffset()); |
rniwa@webkit.org | 0f81260 | 2011-06-24 17:45:00 +0000 | [diff] [blame] | 642 | m_downstreamEnd = createLegacyEditingPosition(m_downstreamEnd.deprecatedNode(), offset); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 643 | } |
| 644 | } |
| 645 | } |
| 646 | } |
| 647 | } |
| 648 | |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 649 | void DeleteSelectionCommand::fixupWhitespace() |
| 650 | { |
akling@apple.com | d455eb6 | 2013-09-01 05:30:31 +0000 | [diff] [blame] | 651 | document().updateLayoutIgnorePendingStylesheets(); |
justing | f81641c | 2006-06-14 23:23:50 +0000 | [diff] [blame] | 652 | // FIXME: isRenderedCharacter should be removed, and we should use VisiblePosition::characterAfter and VisiblePosition::characterBefore |
cdumez@apple.com | a9c60c9 | 2014-10-02 19:39:41 +0000 | [diff] [blame] | 653 | if (m_leadingWhitespace.isNotNull() && !m_leadingWhitespace.isRenderedCharacter() && is<Text>(*m_leadingWhitespace.deprecatedNode())) { |
cdumez@apple.com | a38df4d | 2014-09-29 17:20:18 +0000 | [diff] [blame] | 654 | Text& textNode = downcast<Text>(*m_leadingWhitespace.deprecatedNode()); |
| 655 | ASSERT(!textNode.renderer() || textNode.renderer()->style().collapseWhiteSpace()); |
cdumez@apple.com | ecdce03 | 2017-05-03 18:41:47 +0000 | [diff] [blame] | 656 | replaceTextInNodePreservingMarkers(textNode, m_leadingWhitespace.deprecatedEditingOffset(), 1, nonBreakingSpaceString()); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 657 | } |
cdumez@apple.com | a9c60c9 | 2014-10-02 19:39:41 +0000 | [diff] [blame] | 658 | if (m_trailingWhitespace.isNotNull() && !m_trailingWhitespace.isRenderedCharacter() && is<Text>(*m_trailingWhitespace.deprecatedNode())) { |
cdumez@apple.com | a38df4d | 2014-09-29 17:20:18 +0000 | [diff] [blame] | 659 | Text& textNode = downcast<Text>(*m_trailingWhitespace.deprecatedNode()); |
| 660 | ASSERT(!textNode.renderer() || textNode.renderer()->style().collapseWhiteSpace()); |
cdumez@apple.com | ecdce03 | 2017-05-03 18:41:47 +0000 | [diff] [blame] | 661 | replaceTextInNodePreservingMarkers(textNode, m_trailingWhitespace.deprecatedEditingOffset(), 1, nonBreakingSpaceString()); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 662 | } |
| 663 | } |
| 664 | |
justing | f81641c | 2006-06-14 23:23:50 +0000 | [diff] [blame] | 665 | // 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. |
justing | 217ded8 | 2006-04-04 20:52:10 +0000 | [diff] [blame] | 667 | void DeleteSelectionCommand::mergeParagraphs() |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 668 | { |
justin.garcia@apple.com | 7e95c7b | 2008-11-12 20:08:06 +0000 | [diff] [blame] | 669 | if (!m_mergeBlocksAfterDelete) { |
| 670 | if (m_pruneStartBlockIfNecessary) { |
justin.garcia@apple.com | 7e95c7b | 2008-11-12 20:08:06 +0000 | [diff] [blame] | 671 | // We aren't going to merge into the start block, so remove it if it's empty. |
cdumez@apple.com | 2fecce7 | 2017-05-05 05:09:56 +0000 | [diff] [blame] | 672 | prune(m_startBlock.get()); |
justin.garcia@apple.com | 7e95c7b | 2008-11-12 20:08:06 +0000 | [diff] [blame] | 673 | // 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 | } |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 677 | return; |
justin.garcia@apple.com | 7e95c7b | 2008-11-12 20:08:06 +0000 | [diff] [blame] | 678 | } |
| 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); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 682 | |
justing | 217ded8 | 2006-04-04 20:52:10 +0000 | [diff] [blame] | 683 | // FIXME: Deletion should adjust selection endpoints as it removes nodes so that we never get into this state (4099839). |
rniwa@webkit.org | 1484cab | 2019-06-22 04:04:23 +0000 | [diff] [blame] | 684 | if (m_downstreamEnd.isNull() || m_upstreamStart.isNull() || !m_downstreamEnd.anchorNode()->isConnected() || !m_upstreamStart.anchorNode()->isConnected()) |
justing | 217ded8 | 2006-04-04 20:52:10 +0000 | [diff] [blame] | 685 | return; |
cdumez@apple.com | 85cc262 | 2017-02-02 21:29:15 +0000 | [diff] [blame] | 686 | |
justing | ced6772 | 2006-05-01 21:43:03 +0000 | [diff] [blame] | 687 | // FIXME: The deletion algorithm shouldn't let this happen. |
eric@webkit.org | 05d6e93 | 2009-06-02 21:31:08 +0000 | [diff] [blame] | 688 | if (comparePositions(m_upstreamStart, m_downstreamEnd) > 0) |
justing | ced6772 | 2006-05-01 21:43:03 +0000 | [diff] [blame] | 689 | return; |
| 690 | |
justin.garcia@apple.com | 68510a5 | 2009-04-23 00:54:42 +0000 | [diff] [blame] | 691 | // There's nothing to merge. |
| 692 | if (m_upstreamStart == m_downstreamEnd) |
justing | ced6772 | 2006-05-01 21:43:03 +0000 | [diff] [blame] | 693 | return; |
| 694 | |
justing | 217ded8 | 2006-04-04 20:52:10 +0000 | [diff] [blame] | 695 | VisiblePosition startOfParagraphToMove(m_downstreamEnd); |
| 696 | VisiblePosition mergeDestination(m_upstreamStart); |
| 697 | |
justin.garcia@apple.com | 60777c9 | 2007-11-14 22:33:07 +0000 | [diff] [blame] | 698 | // 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.org | b3d3204 | 2012-05-10 16:44:26 +0000 | [diff] [blame] | 700 | Element* endBlock = enclosingBlock(m_downstreamEnd.deprecatedNode()); |
cdumez@apple.com | 2fecce7 | 2017-05-05 05:09:56 +0000 | [diff] [blame] | 701 | if (!endBlock) |
| 702 | return; |
| 703 | |
| 704 | if (!endBlock->contains(startOfParagraphToMove.deepEquivalent().deprecatedNode()) || !startOfParagraphToMove.deepEquivalent().deprecatedNode()) { |
| 705 | removeNode(*endBlock); |
justin.garcia@apple.com | 60777c9 | 2007-11-14 22:33:07 +0000 | [diff] [blame] | 706 | return; |
| 707 | } |
| 708 | |
justing | ced6772 | 2006-05-01 21:43:03 +0000 | [diff] [blame] | 709 | // We need to merge into m_upstreamStart's block, but it's been emptied out and collapsed by deletion. |
leviw@chromium.org | 688f641 | 2011-03-14 21:19:29 +0000 | [diff] [blame] | 710 | if (!mergeDestination.deepEquivalent().deprecatedNode() || !mergeDestination.deepEquivalent().deprecatedNode()->isDescendantOf(enclosingBlock(m_upstreamStart.containerNode())) || m_startsAtEmptyLine) { |
cdumez@apple.com | aac2e34 | 2017-05-02 17:41:46 +0000 | [diff] [blame] | 711 | insertNodeAt(HTMLBRElement::create(document()), m_upstreamStart); |
justing | ced6772 | 2006-05-01 21:43:03 +0000 | [diff] [blame] | 712 | mergeDestination = VisiblePosition(m_upstreamStart); |
| 713 | } |
harrison | dec5e09 | 2006-01-25 21:05:02 +0000 | [diff] [blame] | 714 | |
justing | ced6772 | 2006-05-01 21:43:03 +0000 | [diff] [blame] | 715 | if (mergeDestination == startOfParagraphToMove) |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 716 | return; |
justing | 217ded8 | 2006-04-04 20:52:10 +0000 | [diff] [blame] | 717 | |
commit-queue@webkit.org | 10809db | 2013-10-24 19:18:30 +0000 | [diff] [blame] | 718 | VisiblePosition endOfParagraphToMove = endOfParagraph(startOfParagraphToMove, CanSkipOverEditingBoundary); |
justing | 217ded8 | 2006-04-04 20:52:10 +0000 | [diff] [blame] | 719 | |
darin | 2b0ff52 | 2006-05-03 19:32:21 +0000 | [diff] [blame] | 720 | if (mergeDestination == endOfParagraphToMove) |
| 721 | return; |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 722 | |
justing | e809f93 | 2006-06-23 22:36:12 +0000 | [diff] [blame] | 723 | // The rule for merging into an empty block is: only do so if its farther to the right. |
| 724 | // FIXME: Consider RTL. |
mitz@apple.com | fc292c3 | 2009-05-03 04:09:41 +0000 | [diff] [blame] | 725 | if (!m_startsAtEmptyLine && isStartOfParagraph(mergeDestination) && startOfParagraphToMove.absoluteCaretBounds().x() > mergeDestination.absoluteCaretBounds().x()) { |
rniwa@webkit.org | 62b1697 | 2011-02-21 09:28:48 +0000 | [diff] [blame] | 726 | if (mergeDestination.deepEquivalent().downstream().deprecatedNode()->hasTagName(brTag)) { |
cdumez@apple.com | 2fecce7 | 2017-05-05 05:09:56 +0000 | [diff] [blame] | 727 | removeNodeAndPruneAncestors(*mergeDestination.deepEquivalent().downstream().deprecatedNode()); |
enrica@apple.com | 25b91e3 | 2010-01-11 18:04:58 +0000 | [diff] [blame] | 728 | m_endingPosition = startOfParagraphToMove.deepEquivalent(); |
| 729 | return; |
| 730 | } |
justing | e809f93 | 2006-06-23 22:36:12 +0000 | [diff] [blame] | 731 | } |
| 732 | |
justin.garcia@apple.com | 619526a | 2009-11-05 05:25:24 +0000 | [diff] [blame] | 733 | // 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.org | 62b1697 | 2011-02-21 09:28:48 +0000 | [diff] [blame] | 736 | if (isRenderedAsNonInlineTableImageOrHR(startOfParagraphToMove.deepEquivalent().deprecatedNode()) && !isStartOfParagraph(mergeDestination)) { |
justin.garcia@apple.com | 619526a | 2009-11-05 05:25:24 +0000 | [diff] [blame] | 737 | m_endingPosition = m_upstreamStart; |
| 738 | return; |
| 739 | } |
| 740 | |
yusukesuzuki@slowstart.org | ae5a8bd | 2018-12-22 06:37:39 +0000 | [diff] [blame] | 741 | 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())) |
justing | f90e28d | 2007-07-30 18:25:19 +0000 | [diff] [blame] | 744 | return; |
| 745 | |
justin.garcia@apple.com | 60777c9 | 2007-11-14 22:33:07 +0000 | [diff] [blame] | 746 | // 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.org | d34b748 | 2010-08-12 02:05:11 +0000 | [diff] [blame] | 749 | bool paragraphToMergeIsEmpty = (startOfParagraphToMove == endOfParagraphToMove); |
| 750 | moveParagraph(startOfParagraphToMove, endOfParagraphToMove, mergeDestination, false, !paragraphToMergeIsEmpty); |
justin.garcia@apple.com | 60777c9 | 2007-11-14 22:33:07 +0000 | [diff] [blame] | 751 | m_needPlaceholder = needPlaceholder; |
justing | 93798f1 | 2006-06-08 04:04:38 +0000 | [diff] [blame] | 752 | // The endingPosition was likely clobbered by the move, so recompute it (moveParagraph selects the moved paragraph). |
justing | 11ec52e | 2006-06-07 00:09:37 +0000 | [diff] [blame] | 753 | m_endingPosition = endingSelection().start(); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 754 | } |
| 755 | |
justing | 187d292 | 2007-07-17 21:05:21 +0000 | [diff] [blame] | 756 | void DeleteSelectionCommand::removePreviouslySelectedEmptyTableRows() |
| 757 | { |
cdumez@apple.com | 85cc262 | 2017-02-02 21:29:15 +0000 | [diff] [blame] | 758 | if (m_endTableRow && m_endTableRow->isConnected() && m_endTableRow != m_startTableRow) { |
justin.garcia@apple.com | 99a0554 | 2007-12-13 06:56:28 +0000 | [diff] [blame] | 759 | Node* row = m_endTableRow->previousSibling(); |
| 760 | while (row && row != m_startTableRow) { |
justing | 187d292 | 2007-07-17 21:05:21 +0000 | [diff] [blame] | 761 | 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.com | 2fecce7 | 2017-05-05 05:09:56 +0000 | [diff] [blame] | 765 | CompositeEditCommand::removeNode(*row); |
justing | 187d292 | 2007-07-17 21:05:21 +0000 | [diff] [blame] | 766 | row = previousRow.get(); |
| 767 | } |
| 768 | } |
| 769 | |
justing | 7d4a434 | 2007-10-13 00:40:48 +0000 | [diff] [blame] | 770 | // Remove empty rows after the start row. |
cdumez@apple.com | 85cc262 | 2017-02-02 21:29:15 +0000 | [diff] [blame] | 771 | if (m_startTableRow && m_startTableRow->isConnected() && m_startTableRow != m_endTableRow) { |
justing | 187d292 | 2007-07-17 21:05:21 +0000 | [diff] [blame] | 772 | Node* row = m_startTableRow->nextSibling(); |
justin.garcia@apple.com | 99a0554 | 2007-12-13 06:56:28 +0000 | [diff] [blame] | 773 | while (row && row != m_endTableRow) { |
justing | 187d292 | 2007-07-17 21:05:21 +0000 | [diff] [blame] | 774 | RefPtr<Node> nextRow = row->nextSibling(); |
| 775 | if (isTableRowEmpty(row)) |
cdumez@apple.com | 2fecce7 | 2017-05-05 05:09:56 +0000 | [diff] [blame] | 776 | CompositeEditCommand::removeNode(*row); |
justing | 187d292 | 2007-07-17 21:05:21 +0000 | [diff] [blame] | 777 | row = nextRow.get(); |
| 778 | } |
| 779 | } |
cdumez@apple.com | 85cc262 | 2017-02-02 21:29:15 +0000 | [diff] [blame] | 780 | |
| 781 | if (m_endTableRow && m_endTableRow->isConnected() && m_endTableRow != m_startTableRow) { |
justin.garcia@apple.com | 99a0554 | 2007-12-13 06:56:28 +0000 | [diff] [blame] | 782 | if (isTableRowEmpty(m_endTableRow.get())) { |
| 783 | // Don't remove m_endTableRow if it's where we're putting the ending selection. |
cdumez@apple.com | ca12b50 | 2016-11-17 06:24:13 +0000 | [diff] [blame] | 784 | if (!m_endingPosition.deprecatedNode()->isDescendantOf(*m_endTableRow)) { |
justin.garcia@apple.com | 99a0554 | 2007-12-13 06:56:28 +0000 | [diff] [blame] | 785 | // 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.com | 2fecce7 | 2017-05-05 05:09:56 +0000 | [diff] [blame] | 788 | CompositeEditCommand::removeNode(*m_endTableRow); |
justin.garcia@apple.com | 99a0554 | 2007-12-13 06:56:28 +0000 | [diff] [blame] | 789 | } |
| 790 | } |
cdumez@apple.com | 85cc262 | 2017-02-02 21:29:15 +0000 | [diff] [blame] | 791 | } |
justing | 187d292 | 2007-07-17 21:05:21 +0000 | [diff] [blame] | 792 | } |
| 793 | |
justin.garcia@apple.com | d1eac9a | 2008-06-20 00:24:13 +0000 | [diff] [blame] | 794 | void DeleteSelectionCommand::calculateTypingStyleAfterDelete() |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 795 | { |
justin.garcia@apple.com | 6412551 | 2008-02-20 17:48:44 +0000 | [diff] [blame] | 796 | if (!m_typingStyle) |
| 797 | return; |
| 798 | |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 799 | // Compute the difference between the style before the delete and the style now |
mjs | 810e976 | 2006-01-09 21:39:14 +0000 | [diff] [blame] | 800 | // after the delete has been done. Set this style on the frame, so other editing |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 801 | // commands being composed with this one will work, and also cache it on the command, |
mjs | 810e976 | 2006-01-09 21:39:14 +0000 | [diff] [blame] | 802 | // so the Frame::appliedEditing can set it after the whole composite command |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 803 | // has completed. |
justing | 503ccf1 | 2005-07-29 22:50:47 +0000 | [diff] [blame] | 804 | |
eseidel | ef50898 | 2006-01-03 09:19:17 +0000 | [diff] [blame] | 805 | // If we deleted into a blockquote, but are now no longer in a blockquote, use the alternate typing style |
rniwa@webkit.org | 24bd1d3 | 2011-03-17 00:03:22 +0000 | [diff] [blame] | 806 | if (m_deleteIntoBlockquoteStyle && !enclosingNodeOfType(m_endingPosition, isMailBlockquote, CanCrossEditingBoundary)) |
eseidel | ef50898 | 2006-01-03 09:19:17 +0000 | [diff] [blame] | 807 | m_typingStyle = m_deleteIntoBlockquoteStyle; |
cdumez@apple.com | d839ea1 | 2015-07-04 19:42:18 +0000 | [diff] [blame] | 808 | m_deleteIntoBlockquoteStyle = nullptr; |
rniwa@webkit.org | 1de555e | 2009-08-07 21:36:24 +0000 | [diff] [blame] | 809 | |
rniwa@webkit.org | 7e06f4a | 2010-11-06 21:19:59 +0000 | [diff] [blame] | 810 | m_typingStyle->prepareToApplyAt(m_endingPosition); |
| 811 | if (m_typingStyle->isEmpty()) |
cdumez@apple.com | d839ea1 | 2015-07-04 19:42:18 +0000 | [diff] [blame] | 812 | m_typingStyle = nullptr; |
justin.garcia@apple.com | d1eac9a | 2008-06-20 00:24:13 +0000 | [diff] [blame] | 813 | // 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.com | 09e70aa | 2017-04-27 16:42:13 +0000 | [diff] [blame] | 817 | frame().selection().setTypingStyle(m_typingStyle.copyRef()); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 818 | } |
| 819 | |
| 820 | void DeleteSelectionCommand::clearTransientState() |
| 821 | { |
eric@webkit.org | 87ea95c | 2009-02-09 21:43:24 +0000 | [diff] [blame] | 822 | m_selectionToDelete = VisibleSelection(); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 823 | 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(); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 830 | } |
jpu@apple.com | fd2014c | 2011-05-12 00:54:08 +0000 | [diff] [blame] | 831 | |
| 832 | String 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.org | 3d7a0f4 | 2011-05-12 14:36:29 +0000 | [diff] [blame] | 841 | VisiblePosition nextPosition = startOfSelection.next(); |
| 842 | if (nextPosition.isNull()) |
| 843 | return String(); |
| 844 | |
cdumez@apple.com | 8f91d5b | 2017-05-01 18:08:07 +0000 | [diff] [blame] | 845 | auto rangeOfFirstCharacter = Range::create(document(), startOfSelection.deepEquivalent(), nextPosition.deepEquivalent()); |
| 846 | for (auto* marker : document().markers().markersInRange(rangeOfFirstCharacter, DocumentMarker::Autocorrected)) { |
morrita@google.com | c07dd58 | 2011-05-27 07:56:25 +0000 | [diff] [blame] | 847 | int startOffset = marker->startOffset(); |
jpu@apple.com | fd2014c | 2011-05-12 00:54:08 +0000 | [diff] [blame] | 848 | if (startOffset == startOfSelection.deepEquivalent().offsetInContainerNode()) |
morrita@google.com | c07dd58 | 2011-05-27 07:56:25 +0000 | [diff] [blame] | 849 | return marker->description(); |
jpu@apple.com | fd2014c | 2011-05-12 00:54:08 +0000 | [diff] [blame] | 850 | } |
| 851 | return String(); |
| 852 | } |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 853 | |
enrica@apple.com | 7abf3f9 | 2011-12-01 01:16:13 +0000 | [diff] [blame] | 854 | // This method removes div elements with no attributes that have only one child or no children at all. |
| 855 | void DeleteSelectionCommand::removeRedundantBlocks() |
| 856 | { |
enrica@apple.com | d5b2e4d | 2011-12-02 18:57:09 +0000 | [diff] [blame] | 857 | Node* node = m_endingPosition.containerNode(); |
enrica@apple.com | 7abf3f9 | 2011-12-01 01:16:13 +0000 | [diff] [blame] | 858 | Node* rootNode = node->rootEditableElement(); |
| 859 | |
| 860 | while (node != rootNode) { |
enrica@apple.com | 4853733 | 2011-12-15 00:32:27 +0000 | [diff] [blame] | 861 | if (isRemovableBlock(node)) { |
enrica@apple.com | 7abf3f9 | 2011-12-01 01:16:13 +0000 | [diff] [blame] | 862 | if (node == m_endingPosition.anchorNode()) |
cdumez@apple.com | 2cbdf51 | 2014-11-11 00:02:57 +0000 | [diff] [blame] | 863 | updatePositionForNodeRemovalPreservingChildren(m_endingPosition, *node); |
enrica@apple.com | 7abf3f9 | 2011-12-01 01:16:13 +0000 | [diff] [blame] | 864 | |
cdumez@apple.com | 2fecce7 | 2017-05-05 05:09:56 +0000 | [diff] [blame] | 865 | CompositeEditCommand::removeNodePreservingChildren(*node); |
enrica@apple.com | 7abf3f9 | 2011-12-01 01:16:13 +0000 | [diff] [blame] | 866 | node = m_endingPosition.anchorNode(); |
| 867 | } else |
enrica@apple.com | 4853733 | 2011-12-15 00:32:27 +0000 | [diff] [blame] | 868 | node = node->parentNode(); |
enrica@apple.com | 7abf3f9 | 2011-12-01 01:16:13 +0000 | [diff] [blame] | 869 | } |
| 870 | } |
| 871 | |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 872 | void 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.org | d39a1e7 | 2010-08-25 19:19:26 +0000 | [diff] [blame] | 878 | |
| 879 | if (!m_selectionToDelete.isNonOrphanedRange()) |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 880 | return; |
| 881 | |
jpu@apple.com | fd2014c | 2011-05-12 00:54:08 +0000 | [diff] [blame] | 882 | String originalString = originalStringForAutocorrectionAtBeginningOfSelection(); |
| 883 | |
adele | b2f2b66 | 2006-07-15 17:22:50 +0000 | [diff] [blame] | 884 | // 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.org | 4f6c51f | 2011-06-30 09:14:17 +0000 | [diff] [blame] | 886 | Element* textControl = enclosingTextFormControl(m_selectionToDelete.start()); |
| 887 | if (textControl && textControl->focused()) |
akling@apple.com | 6f18f6d | 2013-09-01 14:57:23 +0000 | [diff] [blame] | 888 | frame().editor().textWillBeDeletedInTextField(textControl); |
adele | b2f2b66 | 2006-07-15 17:22:50 +0000 | [diff] [blame] | 889 | } |
adele | 174364d | 2006-04-13 01:50:04 +0000 | [diff] [blame] | 890 | |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 891 | // save this to later make the selection with |
darin | 9aa45a4 | 2006-01-15 23:32:02 +0000 | [diff] [blame] | 892 | EAffinity affinity = m_selectionToDelete.affinity(); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 893 | |
justing | 11ec52e | 2006-06-07 00:09:37 +0000 | [diff] [blame] | 894 | Position downstreamEnd = m_selectionToDelete.end().downstream(); |
commit-queue@webkit.org | f362872 | 2010-11-28 02:54:43 +0000 | [diff] [blame] | 895 | m_needPlaceholder = isStartOfParagraph(m_selectionToDelete.visibleStart(), CanCrossEditingBoundary) |
| 896 | && isEndOfParagraph(m_selectionToDelete.visibleEnd(), CanCrossEditingBoundary) |
commit-queue@webkit.org | 1d836ce | 2010-10-29 21:47:01 +0000 | [diff] [blame] | 897 | && !lineBreakExistsAtVisiblePosition(m_selectionToDelete.visibleEnd()); |
justing | 552eeaf | 2007-04-20 01:53:22 +0000 | [diff] [blame] | 898 | 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.com | ca12b50 | 2016-11-17 06:24:13 +0000 | [diff] [blame] | 902 | if (auto* table = isLastPositionBeforeTable(m_selectionToDelete.visibleStart())) { |
| 903 | if (m_selectionToDelete.end().deprecatedNode()->isDescendantOf(*table)) |
justing | 552eeaf | 2007-04-20 01:53:22 +0000 | [diff] [blame] | 904 | m_needPlaceholder = false; |
cdumez@apple.com | ca12b50 | 2016-11-17 06:24:13 +0000 | [diff] [blame] | 905 | } |
justing | 552eeaf | 2007-04-20 01:53:22 +0000 | [diff] [blame] | 906 | } |
| 907 | |
justing | 11ec52e | 2006-06-07 00:09:37 +0000 | [diff] [blame] | 908 | |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 909 | // set up our state |
wenson_hsieh@apple.com | 7cd854e | 2017-08-24 04:12:55 +0000 | [diff] [blame] | 910 | if (!initializePositionData()) |
| 911 | return; |
harrison | dff01cd | 2005-07-18 23:21:19 +0000 | [diff] [blame] | 912 | |
hyatt | 0c05e10 | 2006-04-14 08:15:00 +0000 | [diff] [blame] | 913 | // Delete any text that may hinder our ability to fixup whitespace after the delete |
harrison | dff01cd | 2005-07-18 23:21:19 +0000 | [diff] [blame] | 914 | deleteInsignificantTextDownstream(m_trailingWhitespace); |
| 915 | |
| 916 | saveTypingStyleState(); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 917 | |
harrison | e4b84c5 | 2005-07-18 18:14:59 +0000 | [diff] [blame] | 918 | // 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.com | d1eac9a | 2008-06-20 00:24:13 +0000 | [diff] [blame] | 921 | calculateTypingStyleAfterDelete(); |
rniwa@webkit.org | eccfd3f | 2011-08-16 18:39:52 +0000 | [diff] [blame] | 922 | setEndingSelection(VisibleSelection(m_endingPosition, affinity, endingSelection().isDirectional())); |
harrison | e4b84c5 | 2005-07-18 18:14:59 +0000 | [diff] [blame] | 923 | clearTransientState(); |
| 924 | rebalanceWhitespace(); |
| 925 | return; |
| 926 | } |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 927 | |
harrison | e4b84c5 | 2005-07-18 18:14:59 +0000 | [diff] [blame] | 928 | handleGeneralDelete(); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 929 | |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 930 | fixupWhitespace(); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 931 | |
justing | f81641c | 2006-06-14 23:23:50 +0000 | [diff] [blame] | 932 | mergeParagraphs(); |
| 933 | |
justing | 187d292 | 2007-07-17 21:05:21 +0000 | [diff] [blame] | 934 | removePreviouslySelectedEmptyTableRows(); |
| 935 | |
darin@apple.com | d385be4 | 2016-05-14 20:09:50 +0000 | [diff] [blame] | 936 | if (m_needPlaceholder) { |
enrica@apple.com | d14c2a8 | 2012-05-07 23:30:08 +0000 | [diff] [blame] | 937 | if (m_sanitizeMarkup) |
| 938 | removeRedundantBlocks(); |
darin@apple.com | d385be4 | 2016-05-14 20:09:50 +0000 | [diff] [blame] | 939 | insertNodeAt(HTMLBRElement::create(document()), m_endingPosition); |
enrica@apple.com | 7abf3f9 | 2011-12-01 01:16:13 +0000 | [diff] [blame] | 940 | } |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 941 | |
enrica@apple.com | 2a79a80 | 2014-09-17 00:48:01 +0000 | [diff] [blame] | 942 | bool shouldRebalaceWhiteSpace = true; |
| 943 | if (!frame().editor().behavior().shouldRebalanceWhiteSpacesInSecureField()) { |
| 944 | Node* node = m_endingPosition.deprecatedNode(); |
cdumez@apple.com | a9c60c9 | 2014-10-02 19:39:41 +0000 | [diff] [blame] | 945 | if (is<Text>(node)) { |
cdumez@apple.com | a38df4d | 2014-09-29 17:20:18 +0000 | [diff] [blame] | 946 | Text& textNode = downcast<Text>(*node); |
rniwa@webkit.org | 5c5bdba | 2016-07-21 19:41:47 +0000 | [diff] [blame] | 947 | if (textNode.length() && textNode.renderer()) |
commit-queue@webkit.org | 1a4e667 | 2018-05-21 16:55:45 +0000 | [diff] [blame] | 948 | shouldRebalaceWhiteSpace = textNode.renderer()->style().textSecurity() == TextSecurity::None; |
enrica@apple.com | 2a79a80 | 2014-09-17 00:48:01 +0000 | [diff] [blame] | 949 | } |
dbates@webkit.org | f435ee1 | 2014-01-10 17:06:52 +0000 | [diff] [blame] | 950 | } |
enrica@apple.com | 2a79a80 | 2014-09-17 00:48:01 +0000 | [diff] [blame] | 951 | if (shouldRebalaceWhiteSpace) |
dbates@webkit.org | f435ee1 | 2014-01-10 17:06:52 +0000 | [diff] [blame] | 952 | rebalanceWhitespaceAt(m_endingPosition); |
justing | 92cd9eb | 2006-12-06 21:01:30 +0000 | [diff] [blame] | 953 | |
justin.garcia@apple.com | d1eac9a | 2008-06-20 00:24:13 +0000 | [diff] [blame] | 954 | calculateTypingStyleAfterDelete(); |
jpu@apple.com | fd2014c | 2011-05-12 00:54:08 +0000 | [diff] [blame] | 955 | |
akling@apple.com | 6f18f6d | 2013-09-01 14:57:23 +0000 | [diff] [blame] | 956 | if (!originalString.isEmpty()) |
| 957 | frame().editor().deletedAutocorrectionAtPosition(m_endingPosition, originalString); |
jpu@apple.com | fd2014c | 2011-05-12 00:54:08 +0000 | [diff] [blame] | 958 | |
rniwa@webkit.org | eccfd3f | 2011-08-16 18:39:52 +0000 | [diff] [blame] | 959 | setEndingSelection(VisibleSelection(m_endingPosition, affinity, endingSelection().isDirectional())); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 960 | clearTransientState(); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 961 | } |
| 962 | |
justin.garcia@apple.com | d1eac9a | 2008-06-20 00:24:13 +0000 | [diff] [blame] | 963 | // 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. |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 966 | bool DeleteSelectionCommand::preservesTypingStyle() const |
| 967 | { |
justin.garcia@apple.com | d1eac9a | 2008-06-20 00:24:13 +0000 | [diff] [blame] | 968 | return m_typingStyle; |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 969 | } |
| 970 | |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 971 | } // namespace WebCore |