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" |
darin@apple.com | 2e2bfd7 | 2020-07-29 16:36:40 +0000 | [diff] [blame] | 42 | #include "Range.h" |
hyatt@apple.com | d885df7 | 2009-01-22 02:31:52 +0000 | [diff] [blame] | 43 | #include "RenderTableCell.h" |
antti@apple.com | e9c9016 | 2013-10-13 16:00:10 +0000 | [diff] [blame] | 44 | #include "RenderText.h" |
cdumez@apple.com | 426611c | 2014-10-20 05:17:06 +0000 | [diff] [blame] | 45 | #include "RenderedDocumentMarker.h" |
rniwa@webkit.org | 098af8c | 2021-04-25 05:59:59 +0000 | [diff] [blame] | 46 | #include "ScriptDisallowedScope.h" |
darin | 42563ac5 | 2007-01-22 17:28:57 +0000 | [diff] [blame] | 47 | #include "Text.h" |
tkent@chromium.org | 8c35c12 | 2013-03-06 13:00:14 +0000 | [diff] [blame] | 48 | #include "VisibleUnits.h" |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 49 | |
darin | bbe6466 | 2006-01-16 17:52:23 +0000 | [diff] [blame] | 50 | namespace WebCore { |
darin | edbc5e4 | 2005-08-25 23:13:58 +0000 | [diff] [blame] | 51 | |
darin | bbe6466 | 2006-01-16 17:52:23 +0000 | [diff] [blame] | 52 | using namespace HTMLNames; |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 53 | |
justing | 94b6f9f | 2007-10-31 01:27:54 +0000 | [diff] [blame] | 54 | static bool isTableRow(const Node* node) |
justing | 187d292 | 2007-07-17 21:05:21 +0000 | [diff] [blame] | 55 | { |
| 56 | return node && node->hasTagName(trTag); |
| 57 | } |
| 58 | |
justing | 13e16aa | 2007-07-17 21:55:05 +0000 | [diff] [blame] | 59 | static bool isTableCellEmpty(Node* cell) |
justing | 187d292 | 2007-07-17 21:05:21 +0000 | [diff] [blame] | 60 | { |
| 61 | ASSERT(isTableCell(cell)); |
leviw@chromium.org | 1f95362 | 2011-03-15 19:36:51 +0000 | [diff] [blame] | 62 | return VisiblePosition(firstPositionInNode(cell)) == VisiblePosition(lastPositionInNode(cell)); |
justing | 187d292 | 2007-07-17 21:05:21 +0000 | [diff] [blame] | 63 | } |
| 64 | |
justing | 13e16aa | 2007-07-17 21:55:05 +0000 | [diff] [blame] | 65 | static bool isTableRowEmpty(Node* row) |
justing | 187d292 | 2007-07-17 21:05:21 +0000 | [diff] [blame] | 66 | { |
| 67 | if (!isTableRow(row)) |
| 68 | return false; |
rniwa@webkit.org | 098af8c | 2021-04-25 05:59:59 +0000 | [diff] [blame] | 69 | |
cdumez@apple.com | e1840be | 2021-09-20 21:45:40 +0000 | [diff] [blame] | 70 | for (RefPtr child = row->firstChild(); child; child = child->nextSibling()) { |
rniwa@webkit.org | 098af8c | 2021-04-25 05:59:59 +0000 | [diff] [blame] | 71 | if (isTableCell(child.get()) && !isTableCellEmpty(child.get())) |
justing | 187d292 | 2007-07-17 21:05:21 +0000 | [diff] [blame] | 72 | return false; |
rniwa@webkit.org | 098af8c | 2021-04-25 05:59:59 +0000 | [diff] [blame] | 73 | } |
| 74 | |
justing | 187d292 | 2007-07-17 21:05:21 +0000 | [diff] [blame] | 75 | return true; |
| 76 | } |
| 77 | |
rniwa@webkit.org | 098af8c | 2021-04-25 05:59:59 +0000 | [diff] [blame] | 78 | static bool isSpecialHTMLElement(const Node& node) |
| 79 | { |
| 80 | ScriptDisallowedScope scriptDisallowedScope; |
| 81 | |
| 82 | if (!is<HTMLElement>(node)) |
| 83 | return false; |
| 84 | |
| 85 | if (downcast<HTMLElement>(node).isLink()) |
| 86 | return true; |
| 87 | |
| 88 | auto* renderer = downcast<HTMLElement>(node).renderer(); |
| 89 | if (!renderer) |
| 90 | return false; |
| 91 | |
| 92 | if (renderer->style().display() == DisplayType::Table || renderer->style().display() == DisplayType::InlineTable) |
| 93 | return true; |
| 94 | |
| 95 | if (renderer->style().isFloating()) |
| 96 | return true; |
| 97 | |
| 98 | if (renderer->style().position() != PositionType::Static) |
| 99 | return true; |
| 100 | |
| 101 | return false; |
| 102 | } |
| 103 | |
| 104 | static RefPtr<HTMLElement> firstInSpecialElement(const Position& position) |
| 105 | { |
cdumez@apple.com | e1840be | 2021-09-20 21:45:40 +0000 | [diff] [blame] | 106 | RefPtr rootEditableElement { position.rootEditableElement() }; |
| 107 | for (RefPtr node = position.deprecatedNode(); node && node->rootEditableElement() == rootEditableElement; node = node->parentNode()) { |
rniwa@webkit.org | 098af8c | 2021-04-25 05:59:59 +0000 | [diff] [blame] | 108 | if (!isSpecialHTMLElement(*node)) |
| 109 | continue; |
| 110 | VisiblePosition visiblePosition = position; |
| 111 | VisiblePosition firstInElement = firstPositionInOrBeforeNode(node.get()); |
| 112 | if ((isRenderedTable(node.get()) && visiblePosition == firstInElement.next()) || visiblePosition == firstInElement) { |
| 113 | RELEASE_ASSERT(is<HTMLElement>(node)); |
cdumez@apple.com | 0db4766 | 2022-04-08 18:41:53 +0000 | [diff] [blame] | 114 | return static_pointer_cast<HTMLElement>(WTFMove(node)); |
rniwa@webkit.org | 098af8c | 2021-04-25 05:59:59 +0000 | [diff] [blame] | 115 | } |
| 116 | } |
| 117 | return nullptr; |
| 118 | } |
| 119 | |
| 120 | static RefPtr<HTMLElement> lastInSpecialElement(const Position& position) |
| 121 | { |
cdumez@apple.com | e1840be | 2021-09-20 21:45:40 +0000 | [diff] [blame] | 122 | RefPtr rootEditableElement { position.rootEditableElement() }; |
| 123 | for (RefPtr node = position.deprecatedNode(); node && node->rootEditableElement() == rootEditableElement; node = node->parentNode()) { |
rniwa@webkit.org | 098af8c | 2021-04-25 05:59:59 +0000 | [diff] [blame] | 124 | if (!isSpecialHTMLElement(*node)) |
| 125 | continue; |
| 126 | VisiblePosition visiblePosition = position; |
| 127 | VisiblePosition lastInElement = lastPositionInOrAfterNode(node.get()); |
| 128 | if ((isRenderedTable(node.get()) && visiblePosition == lastInElement.previous()) || visiblePosition == lastInElement) { |
| 129 | RELEASE_ASSERT(is<HTMLElement>(node)); |
cdumez@apple.com | 0db4766 | 2022-04-08 18:41:53 +0000 | [diff] [blame] | 130 | return static_pointer_cast<HTMLElement>(WTFMove(node)); |
rniwa@webkit.org | 098af8c | 2021-04-25 05:59:59 +0000 | [diff] [blame] | 131 | } |
| 132 | } |
| 133 | return nullptr; |
| 134 | } |
| 135 | |
| 136 | static std::pair<Position, RefPtr<HTMLElement>> positionBeforeContainingSpecialElement(const Position& position) |
| 137 | { |
| 138 | auto element = firstInSpecialElement(position); |
| 139 | if (!element) |
| 140 | return { position, nullptr }; |
| 141 | auto result = positionInParentBeforeNode(element.get()); |
| 142 | if (result.isNull() || result.containerNode()->rootEditableElement() != position.containerNode()->rootEditableElement()) |
| 143 | return { position, nullptr }; |
| 144 | return { result, WTFMove(element) }; |
| 145 | } |
| 146 | |
| 147 | static std::pair<Position, RefPtr<HTMLElement>> positionAfterContainingSpecialElement(const Position& position) |
| 148 | { |
| 149 | auto element = lastInSpecialElement(position); |
| 150 | if (!element) |
| 151 | return { position, nullptr }; |
| 152 | auto result = positionInParentAfterNode(element.get()); |
| 153 | if (result.isNull() || result.deprecatedNode()->rootEditableElement() != position.containerNode()->rootEditableElement()) |
| 154 | return { position, nullptr }; |
| 155 | return { result, WTFMove(element) }; |
| 156 | } |
| 157 | |
commit-queue@webkit.org | 2de6e05 | 2015-04-26 22:17:11 +0000 | [diff] [blame] | 158 | DeleteSelectionCommand::DeleteSelectionCommand(Document& document, bool smartDelete, bool mergeBlocksAfterDelete, bool replace, bool expandForSpecialElements, bool sanitizeMarkup, EditAction editingAction) |
| 159 | : CompositeEditCommand(document, editingAction) |
jpu@apple.com | fd2014c | 2011-05-12 00:54:08 +0000 | [diff] [blame] | 160 | , m_hasSelectionToDelete(false) |
| 161 | , m_smartDelete(smartDelete) |
| 162 | , m_mergeBlocksAfterDelete(mergeBlocksAfterDelete) |
| 163 | , m_needPlaceholder(false) |
| 164 | , m_replace(replace) |
| 165 | , m_expandForSpecialElements(expandForSpecialElements) |
| 166 | , m_pruneStartBlockIfNecessary(false) |
| 167 | , m_startsAtEmptyLine(false) |
enrica@apple.com | d14c2a8 | 2012-05-07 23:30:08 +0000 | [diff] [blame] | 168 | , m_sanitizeMarkup(sanitizeMarkup) |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 169 | { |
| 170 | } |
| 171 | |
commit-queue@webkit.org | 2de6e05 | 2015-04-26 22:17:11 +0000 | [diff] [blame] | 172 | DeleteSelectionCommand::DeleteSelectionCommand(const VisibleSelection& selection, bool smartDelete, bool mergeBlocksAfterDelete, bool replace, bool expandForSpecialElements, bool sanitizeMarkup, EditAction editingAction) |
| 173 | : CompositeEditCommand(selection.start().anchorNode()->document(), editingAction) |
jpu@apple.com | fd2014c | 2011-05-12 00:54:08 +0000 | [diff] [blame] | 174 | , m_hasSelectionToDelete(true) |
| 175 | , m_smartDelete(smartDelete) |
| 176 | , m_mergeBlocksAfterDelete(mergeBlocksAfterDelete) |
| 177 | , m_needPlaceholder(false) |
| 178 | , m_replace(replace) |
| 179 | , m_expandForSpecialElements(expandForSpecialElements) |
| 180 | , m_pruneStartBlockIfNecessary(false) |
| 181 | , m_startsAtEmptyLine(false) |
enrica@apple.com | d14c2a8 | 2012-05-07 23:30:08 +0000 | [diff] [blame] | 182 | , m_sanitizeMarkup(sanitizeMarkup) |
jpu@apple.com | fd2014c | 2011-05-12 00:54:08 +0000 | [diff] [blame] | 183 | , m_selectionToDelete(selection) |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 184 | { |
| 185 | } |
| 186 | |
justing | 113aaf3 | 2007-01-25 01:00:36 +0000 | [diff] [blame] | 187 | void DeleteSelectionCommand::initializeStartEnd(Position& start, Position& end) |
rniwa@webkit.org | 098af8c | 2021-04-25 05:59:59 +0000 | [diff] [blame] | 188 | { |
justing | 113aaf3 | 2007-01-25 01:00:36 +0000 | [diff] [blame] | 189 | start = m_selectionToDelete.start(); |
| 190 | end = m_selectionToDelete.end(); |
justing | e6f80c1 | 2006-06-09 04:57:51 +0000 | [diff] [blame] | 191 | |
justing | 2112d43 | 2006-11-28 21:25:28 +0000 | [diff] [blame] | 192 | // 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] | 193 | // 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] | 194 | if (start.deprecatedNode()->hasTagName(hrTag)) |
| 195 | start = positionBeforeNode(start.deprecatedNode()); |
| 196 | else if (end.deprecatedNode()->hasTagName(hrTag)) |
| 197 | end = positionAfterNode(end.deprecatedNode()); |
lweintraub | 635ec2a | 2006-07-13 18:28:28 +0000 | [diff] [blame] | 198 | |
dbates@webkit.org | ef42d38 | 2010-01-25 19:20:06 +0000 | [diff] [blame] | 199 | // 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] | 200 | if (!m_expandForSpecialElements) |
| 201 | return; |
| 202 | |
justin.garcia@apple.com | 6412551 | 2008-02-20 17:48:44 +0000 | [diff] [blame] | 203 | while (1) { |
rniwa@webkit.org | 098af8c | 2021-04-25 05:59:59 +0000 | [diff] [blame] | 204 | auto [startBeforeSpecialElement, startSpecialContainer] = positionBeforeContainingSpecialElement(start); |
| 205 | auto [endAfterSpecialElement, endSpecialContainer] = positionAfterContainingSpecialElement(end); |
| 206 | |
justing | 47d836d | 2007-06-08 00:02:06 +0000 | [diff] [blame] | 207 | if (!startSpecialContainer && !endSpecialContainer) |
justing | e6f80c1 | 2006-06-09 04:57:51 +0000 | [diff] [blame] | 208 | break; |
commit-queue@webkit.org | c8932f8 | 2013-10-11 06:18:44 +0000 | [diff] [blame] | 209 | |
| 210 | m_mergeBlocksAfterDelete = false; |
| 211 | |
justin.garcia@apple.com | 6412551 | 2008-02-20 17:48:44 +0000 | [diff] [blame] | 212 | if (VisiblePosition(start) != m_selectionToDelete.visibleStart() || VisiblePosition(end) != m_selectionToDelete.visibleEnd()) |
| 213 | break; |
eric@webkit.org | 05d6e93 | 2009-06-02 21:31:08 +0000 | [diff] [blame] | 214 | |
eric@webkit.org | f74ae5c | 2009-09-09 23:25:47 +0000 | [diff] [blame] | 215 | // If we're going to expand to include the startSpecialContainer, it must be fully selected. |
rniwa@webkit.org | 098af8c | 2021-04-25 05:59:59 +0000 | [diff] [blame] | 216 | if (startSpecialContainer && !endSpecialContainer && positionInParentAfterNode(startSpecialContainer.get()) >= end) |
justing | 47d836d | 2007-06-08 00:02:06 +0000 | [diff] [blame] | 217 | break; |
| 218 | |
eric@webkit.org | 05d6e93 | 2009-06-02 21:31:08 +0000 | [diff] [blame] | 219 | // If we're going to expand to include the endSpecialContainer, it must be fully selected. |
rniwa@webkit.org | 098af8c | 2021-04-25 05:59:59 +0000 | [diff] [blame] | 220 | if (endSpecialContainer && !startSpecialContainer && start >= positionInParentBeforeNode(endSpecialContainer.get())) |
justing | 47d836d | 2007-06-08 00:02:06 +0000 | [diff] [blame] | 221 | break; |
eric@webkit.org | f74ae5c | 2009-09-09 23:25:47 +0000 | [diff] [blame] | 222 | |
rniwa@webkit.org | 098af8c | 2021-04-25 05:59:59 +0000 | [diff] [blame] | 223 | if (startSpecialContainer && startSpecialContainer->isDescendantOf(endSpecialContainer.get())) { |
justing | 2bdb03e | 2006-11-30 02:54:19 +0000 | [diff] [blame] | 224 | // Don't adjust the end yet, it is the end of a special element that contains the start |
| 225 | // special element (which may or may not be fully selected). |
rniwa@webkit.org | 098af8c | 2021-04-25 05:59:59 +0000 | [diff] [blame] | 226 | start = startBeforeSpecialElement; |
| 227 | } else if (endSpecialContainer && endSpecialContainer->isDescendantOf(startSpecialContainer.get())) { |
justing | 2bdb03e | 2006-11-30 02:54:19 +0000 | [diff] [blame] | 228 | // Don't adjust the start yet, it is the start of a special element that contains the end |
| 229 | // special element (which may or may not be fully selected). |
rniwa@webkit.org | 098af8c | 2021-04-25 05:59:59 +0000 | [diff] [blame] | 230 | end = endAfterSpecialElement; |
| 231 | } else { |
| 232 | start = startBeforeSpecialElement; |
| 233 | end = endAfterSpecialElement; |
justing | 2bdb03e | 2006-11-30 02:54:19 +0000 | [diff] [blame] | 234 | } |
harrison | dec5e09 | 2006-01-25 21:05:02 +0000 | [diff] [blame] | 235 | } |
harrison | dec5e09 | 2006-01-25 21:05:02 +0000 | [diff] [blame] | 236 | } |
| 237 | |
ojan@chromium.org | 078aa80 | 2010-03-10 00:17:11 +0000 | [diff] [blame] | 238 | void DeleteSelectionCommand::setStartingSelectionOnSmartDelete(const Position& start, const Position& end) |
ojan@chromium.org | 77dae9d | 2010-03-09 23:41:38 +0000 | [diff] [blame] | 239 | { |
| 240 | VisiblePosition newBase; |
| 241 | VisiblePosition newExtent; |
| 242 | if (startingSelection().isBaseFirst()) { |
| 243 | newBase = start; |
| 244 | newExtent = end; |
| 245 | } else { |
| 246 | newBase = end; |
| 247 | newExtent = start; |
| 248 | } |
rniwa@webkit.org | eccfd3f | 2011-08-16 18:39:52 +0000 | [diff] [blame] | 249 | setStartingSelection(VisibleSelection(newBase, newExtent, startingSelection().isDirectional())); |
ojan@chromium.org | 77dae9d | 2010-03-09 23:41:38 +0000 | [diff] [blame] | 250 | } |
| 251 | |
megan_gardner@apple.com | 01af70af | 2019-03-21 17:03:17 +0000 | [diff] [blame] | 252 | bool DeleteSelectionCommand::shouldSmartDeleteParagraphSpacers() |
| 253 | { |
| 254 | return document().editingBehavior().shouldSmartInsertDeleteParagraphs(); |
| 255 | } |
| 256 | |
| 257 | void DeleteSelectionCommand::smartDeleteParagraphSpacers() |
| 258 | { |
| 259 | VisiblePosition visibleStart { m_upstreamStart }; |
| 260 | VisiblePosition visibleEnd { m_downstreamEnd }; |
| 261 | bool selectionEndsInParagraphSeperator = isEndOfParagraph(visibleEnd); |
| 262 | bool selectionEndIsEndOfContent = endOfEditableContent(visibleEnd) == visibleEnd; |
| 263 | bool startAndEndInSameUnsplittableElement = unsplittableElementForPosition(visibleStart.deepEquivalent()) == unsplittableElementForPosition(visibleEnd.deepEquivalent()); |
| 264 | visibleStart = visibleStart.previous(CannotCrossEditingBoundary); |
| 265 | visibleEnd = visibleEnd.next(CannotCrossEditingBoundary); |
megan_gardner@apple.com | ffce9df | 2020-06-25 20:57:59 +0000 | [diff] [blame] | 266 | bool previousPositionIsStartOfContent = startOfEditableContent(visibleStart) == visibleStart; |
megan_gardner@apple.com | 01af70af | 2019-03-21 17:03:17 +0000 | [diff] [blame] | 267 | bool previousPositionIsBlankParagraph = isBlankParagraph(visibleStart); |
megan_gardner@apple.com | 35d1189 | 2020-06-25 22:25:39 +0000 | [diff] [blame] | 268 | bool endPositionIsBlankParagraph = isBlankParagraph(visibleEnd); |
| 269 | bool hasBlankParagraphAfterEndOrIsEndOfContent = !selectionEndIsEndOfContent && (endPositionIsBlankParagraph || selectionEndsInParagraphSeperator); |
megan_gardner@apple.com | 01af70af | 2019-03-21 17:03:17 +0000 | [diff] [blame] | 270 | if (startAndEndInSameUnsplittableElement && previousPositionIsBlankParagraph && hasBlankParagraphAfterEndOrIsEndOfContent) { |
| 271 | m_needPlaceholder = false; |
| 272 | Position position; |
megan_gardner@apple.com | 35d1189 | 2020-06-25 22:25:39 +0000 | [diff] [blame] | 273 | if (endPositionIsBlankParagraph) |
megan_gardner@apple.com | 01af70af | 2019-03-21 17:03:17 +0000 | [diff] [blame] | 274 | position = startOfNextParagraph(startOfNextParagraph(m_downstreamEnd)).deepEquivalent(); |
| 275 | else |
| 276 | position = VisiblePosition(m_downstreamEnd).next().deepEquivalent(); |
| 277 | m_upstreamEnd = position.upstream(); |
| 278 | m_downstreamEnd = position.downstream(); |
darin@apple.com | d1d65f9 | 2020-09-02 21:01:59 +0000 | [diff] [blame] | 279 | m_trailingWhitespace = m_downstreamEnd.trailingWhitespacePosition(VisiblePosition::defaultAffinity); |
megan_gardner@apple.com | 01af70af | 2019-03-21 17:03:17 +0000 | [diff] [blame] | 280 | setStartingSelectionOnSmartDelete(m_upstreamStart, m_downstreamEnd); |
| 281 | } |
| 282 | if (startAndEndInSameUnsplittableElement && selectionEndIsEndOfContent && previousPositionIsBlankParagraph && selectionEndsInParagraphSeperator) { |
| 283 | m_needPlaceholder = false; |
megan_gardner@apple.com | ffce9df | 2020-06-25 20:57:59 +0000 | [diff] [blame] | 284 | VisiblePosition endOfParagraphBeforeStart; |
| 285 | if (previousPositionIsStartOfContent) |
| 286 | endOfParagraphBeforeStart = endOfParagraph(VisiblePosition { m_upstreamStart }.previous()); |
| 287 | else |
| 288 | endOfParagraphBeforeStart = endOfParagraph(VisiblePosition { m_upstreamStart }.previous().previous()); |
| 289 | auto position = endOfParagraphBeforeStart.deepEquivalent(); |
megan_gardner@apple.com | 01af70af | 2019-03-21 17:03:17 +0000 | [diff] [blame] | 290 | m_upstreamStart = position.upstream(); |
| 291 | m_downstreamStart = position.downstream(); |
darin@apple.com | c8d8b55 | 2020-09-03 21:38:50 +0000 | [diff] [blame] | 292 | m_leadingWhitespace = m_upstreamStart.leadingWhitespacePosition(Affinity::Downstream); |
megan_gardner@apple.com | 01af70af | 2019-03-21 17:03:17 +0000 | [diff] [blame] | 293 | setStartingSelectionOnSmartDelete(m_upstreamStart, m_upstreamEnd); |
| 294 | } |
| 295 | } |
| 296 | |
wenson_hsieh@apple.com | 7cd854e | 2017-08-24 04:12:55 +0000 | [diff] [blame] | 297 | bool DeleteSelectionCommand::initializePositionData() |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 298 | { |
justing | 113aaf3 | 2007-01-25 01:00:36 +0000 | [diff] [blame] | 299 | Position start, end; |
| 300 | initializeStartEnd(start, end); |
| 301 | |
jiewen_tan@apple.com | 8a64a4a | 2015-11-05 00:46:34 +0000 | [diff] [blame] | 302 | if (!isEditablePosition(start, ContentIsEditable)) |
| 303 | start = firstEditablePositionAfterPositionInRoot(start, highestEditableRoot(start)); |
| 304 | if (!isEditablePosition(end, ContentIsEditable)) |
| 305 | end = lastEditablePositionBeforePositionInRoot(end, highestEditableRoot(start)); |
| 306 | |
wenson_hsieh@apple.com | 7cd854e | 2017-08-24 04:12:55 +0000 | [diff] [blame] | 307 | if (start.isNull() || end.isNull()) |
| 308 | return false; |
| 309 | |
justing | 113aaf3 | 2007-01-25 01:00:36 +0000 | [diff] [blame] | 310 | m_upstreamStart = start.upstream(); |
| 311 | m_downstreamStart = start.downstream(); |
| 312 | m_upstreamEnd = end.upstream(); |
| 313 | m_downstreamEnd = end.downstream(); |
harrison | dec5e09 | 2006-01-25 21:05:02 +0000 | [diff] [blame] | 314 | |
justing | 06a653c | 2007-04-04 20:49:52 +0000 | [diff] [blame] | 315 | m_startRoot = editableRootForPosition(start); |
| 316 | m_endRoot = editableRootForPosition(end); |
| 317 | |
justin.garcia@apple.com | deed90d | 2007-12-13 21:32:12 +0000 | [diff] [blame] | 318 | m_startTableRow = enclosingNodeOfType(start, &isTableRow); |
| 319 | m_endTableRow = enclosingNodeOfType(end, &isTableRow); |
justing | 187d292 | 2007-07-17 21:05:21 +0000 | [diff] [blame] | 320 | |
justing | b770150 | 2007-03-29 22:34:28 +0000 | [diff] [blame] | 321 | // Don't move content out of a table cell. |
justin.garcia@apple.com | ab87ef8 | 2008-05-29 20:37:53 +0000 | [diff] [blame] | 322 | // If the cell is non-editable, enclosingNodeOfType won't return it by default, so |
| 323 | // tell that function that we don't care if it returns non-editable nodes. |
cdumez@apple.com | e1840be | 2021-09-20 21:45:40 +0000 | [diff] [blame] | 324 | RefPtr startCell { enclosingNodeOfType(m_upstreamStart, &isTableCell, CanCrossEditingBoundary) }; |
| 325 | RefPtr endCell { enclosingNodeOfType(m_downstreamEnd, &isTableCell, CanCrossEditingBoundary) }; |
justing | 8f16cee | 2006-06-22 00:20:29 +0000 | [diff] [blame] | 326 | // 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] | 327 | if (endCell && endCell != startCell) |
justing | 8f16cee | 2006-06-22 00:20:29 +0000 | [diff] [blame] | 328 | m_mergeBlocksAfterDelete = false; |
| 329 | |
justing | 11ec52e | 2006-06-07 00:09:37 +0000 | [diff] [blame] | 330 | // 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] | 331 | // Sometimes they aren't (like when no merge is requested), so we must choose one position to hold the caret |
| 332 | // and receive the placeholder after deletion. |
justing | 11ec52e | 2006-06-07 00:09:37 +0000 | [diff] [blame] | 333 | VisiblePosition visibleEnd(m_downstreamEnd); |
| 334 | if (m_mergeBlocksAfterDelete && !isEndOfParagraph(visibleEnd)) |
| 335 | m_endingPosition = m_downstreamEnd; |
| 336 | else |
| 337 | m_endingPosition = m_downstreamStart; |
justin.garcia@apple.com | 7e95c7b | 2008-11-12 20:08:06 +0000 | [diff] [blame] | 338 | |
| 339 | // We don't want to merge into a block if it will mean changing the quote level of content after deleting |
| 340 | // selections that contain a whole number paragraphs plus a line break, since it is unclear to most users |
| 341 | // that such a selection actually ends at the start of the next paragraph. This matches TextEdit behavior |
| 342 | // for indented paragraphs. |
adele@apple.com | e2bc16b | 2009-04-28 22:17:29 +0000 | [diff] [blame] | 343 | // Only apply this rule if the endingSelection is a range selection. If it is a caret, then other operations have created |
| 344 | // 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. |
| 345 | if (numEnclosingMailBlockquotes(start) != numEnclosingMailBlockquotes(end) |
| 346 | && isStartOfParagraph(visibleEnd) && isStartOfParagraph(VisiblePosition(start)) |
| 347 | && endingSelection().isRange()) { |
justin.garcia@apple.com | 7e95c7b | 2008-11-12 20:08:06 +0000 | [diff] [blame] | 348 | m_mergeBlocksAfterDelete = false; |
| 349 | m_pruneStartBlockIfNecessary = true; |
| 350 | } |
justing | 8f16cee | 2006-06-22 00:20:29 +0000 | [diff] [blame] | 351 | |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 352 | // 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] | 353 | m_leadingWhitespace = m_upstreamStart.leadingWhitespacePosition(m_selectionToDelete.affinity()); |
darin@apple.com | d1d65f9 | 2020-09-02 21:01:59 +0000 | [diff] [blame] | 354 | m_trailingWhitespace = m_downstreamEnd.trailingWhitespacePosition(VisiblePosition::defaultAffinity); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 355 | |
| 356 | if (m_smartDelete) { |
| 357 | |
| 358 | // 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] | 359 | Position pos = VisiblePosition(m_upstreamStart, m_selectionToDelete.affinity()).deepEquivalent(); |
darin@apple.com | d1d65f9 | 2020-09-02 21:01:59 +0000 | [diff] [blame] | 360 | bool skipSmartDelete = pos.trailingWhitespacePosition(VisiblePosition::defaultAffinity, true).isNotNull(); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 361 | if (!skipSmartDelete) |
darin@apple.com | d1d65f9 | 2020-09-02 21:01:59 +0000 | [diff] [blame] | 362 | skipSmartDelete = m_downstreamEnd.leadingWhitespacePosition(VisiblePosition::defaultAffinity, true).isNotNull(); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 363 | |
| 364 | // extend selection upstream if there is whitespace there |
darin | 9aa45a4 | 2006-01-15 23:32:02 +0000 | [diff] [blame] | 365 | bool hasLeadingWhitespaceBeforeAdjustment = m_upstreamStart.leadingWhitespacePosition(m_selectionToDelete.affinity(), true).isNotNull(); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 366 | if (!skipSmartDelete && hasLeadingWhitespaceBeforeAdjustment) { |
darin@apple.com | d1d65f9 | 2020-09-02 21:01:59 +0000 | [diff] [blame] | 367 | auto visiblePos = VisiblePosition(m_upstreamStart).previous(); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 368 | pos = visiblePos.deepEquivalent(); |
| 369 | // Expand out one character upstream for smart delete and recalculate |
| 370 | // positions based on this change. |
| 371 | m_upstreamStart = pos.upstream(); |
| 372 | m_downstreamStart = pos.downstream(); |
| 373 | m_leadingWhitespace = m_upstreamStart.leadingWhitespacePosition(visiblePos.affinity()); |
ojan@chromium.org | 77dae9d | 2010-03-09 23:41:38 +0000 | [diff] [blame] | 374 | |
| 375 | setStartingSelectionOnSmartDelete(m_upstreamStart, m_upstreamEnd); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | // trailing whitespace is only considered for smart delete if there is no leading |
| 379 | // whitespace, as in the case where you double-click the first word of a paragraph. |
darin@apple.com | d1d65f9 | 2020-09-02 21:01:59 +0000 | [diff] [blame] | 380 | if (!skipSmartDelete && !hasLeadingWhitespaceBeforeAdjustment && m_downstreamEnd.trailingWhitespacePosition(VisiblePosition::defaultAffinity, true).isNotNull()) { |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 381 | // Expand out one character downstream for smart delete and recalculate |
| 382 | // positions based on this change. |
darin@apple.com | d1d65f9 | 2020-09-02 21:01:59 +0000 | [diff] [blame] | 383 | pos = VisiblePosition(m_downstreamEnd).next().deepEquivalent(); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 384 | m_upstreamEnd = pos.upstream(); |
| 385 | m_downstreamEnd = pos.downstream(); |
darin@apple.com | d1d65f9 | 2020-09-02 21:01:59 +0000 | [diff] [blame] | 386 | m_trailingWhitespace = m_downstreamEnd.trailingWhitespacePosition(VisiblePosition::defaultAffinity); |
ojan@chromium.org | 77dae9d | 2010-03-09 23:41:38 +0000 | [diff] [blame] | 387 | |
| 388 | setStartingSelectionOnSmartDelete(m_downstreamStart, m_downstreamEnd); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 389 | } |
megan_gardner@apple.com | 01af70af | 2019-03-21 17:03:17 +0000 | [diff] [blame] | 390 | |
| 391 | if (shouldSmartDeleteParagraphSpacers()) |
| 392 | smartDeleteParagraphSpacers(); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 393 | } |
| 394 | |
commit-queue@webkit.org | c56f5de | 2011-01-19 09:32:09 +0000 | [diff] [blame] | 395 | // 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] | 396 | // 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] | 397 | // FIXME: parentAnchoredEquivalent should eventually be moved into enclosing element getters |
justin.garcia@apple.com | e1da273 | 2008-06-25 21:22:12 +0000 | [diff] [blame] | 398 | // like the one below, since editing functions should obviously accept editing positions. |
| 399 | // FIXME: Passing false to enclosingNodeOfType tells it that it's OK to return a non-editable |
| 400 | // 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] | 401 | m_startBlock = enclosingNodeOfType(m_downstreamStart.parentAnchoredEquivalent(), &isBlock, CanCrossEditingBoundary); |
| 402 | m_endBlock = enclosingNodeOfType(m_upstreamEnd.parentAnchoredEquivalent(), &isBlock, CanCrossEditingBoundary); |
wenson_hsieh@apple.com | 7cd854e | 2017-08-24 04:12:55 +0000 | [diff] [blame] | 403 | |
| 404 | return true; |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | void DeleteSelectionCommand::saveTypingStyleState() |
| 408 | { |
justin.garcia@apple.com | 6412551 | 2008-02-20 17:48:44 +0000 | [diff] [blame] | 409 | // A common case is deleting characters that are all from the same text node. In |
| 410 | // that case, the style at the start of the selection before deletion will be the |
| 411 | // same as the style at the start of the selection after deletion (since those |
| 412 | // two positions will be identical). Therefore there is no need to save the |
| 413 | // typing style at the start of the selection, nor is there a reason to |
| 414 | // compute the style at the start of the selection after deletion (see the |
| 415 | // early return in calculateTypingStyleAfterDelete). |
a.bah@samsung.com | 305f685 | 2013-09-10 07:27:21 +0000 | [diff] [blame] | 416 | // However, if typing style was previously set from another text node at the previous |
| 417 | // position (now deleted), we need to clear that style as well. |
| 418 | if (m_upstreamStart.deprecatedNode() == m_downstreamEnd.deprecatedNode() && m_upstreamStart.deprecatedNode()->isTextNode()) { |
shihchieh_lee@apple.com | 501b37c | 2020-04-28 16:36:38 +0000 | [diff] [blame] | 419 | document().selection().clearTypingStyle(); |
justin.garcia@apple.com | 6412551 | 2008-02-20 17:48:44 +0000 | [diff] [blame] | 420 | return; |
a.bah@samsung.com | 305f685 | 2013-09-10 07:27:21 +0000 | [diff] [blame] | 421 | } |
rniwa@webkit.org | 1de555e | 2009-08-07 21:36:24 +0000 | [diff] [blame] | 422 | |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 423 | // 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] | 424 | m_typingStyle = EditingStyle::create(m_selectionToDelete.start(), EditingStyle::EditingPropertiesInEffect); |
rniwa@webkit.org | b70a577 | 2011-09-22 19:25:54 +0000 | [diff] [blame] | 425 | m_typingStyle->removeStyleAddedByNode(enclosingAnchorElement(m_selectionToDelete.start())); |
rniwa@webkit.org | 1de555e | 2009-08-07 21:36:24 +0000 | [diff] [blame] | 426 | |
justing | 503ccf1 | 2005-07-29 22:50:47 +0000 | [diff] [blame] | 427 | // If we're deleting into a Mail blockquote, save the style at end() instead of start() |
| 428 | // 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] | 429 | if (enclosingNodeOfType(m_selectionToDelete.start(), isMailBlockquote)) |
rniwa@webkit.org | 7e06f4a | 2010-11-06 21:19:59 +0000 | [diff] [blame] | 430 | m_deleteIntoBlockquoteStyle = EditingStyle::create(m_selectionToDelete.end()); |
rniwa@webkit.org | 1de555e | 2009-08-07 21:36:24 +0000 | [diff] [blame] | 431 | else |
cdumez@apple.com | d839ea1 | 2015-07-04 19:42:18 +0000 | [diff] [blame] | 432 | m_deleteIntoBlockquoteStyle = nullptr; |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | bool DeleteSelectionCommand::handleSpecialCaseBRDelete() |
| 436 | { |
cdumez@apple.com | e1840be | 2021-09-20 21:45:40 +0000 | [diff] [blame] | 437 | RefPtr nodeAfterUpstreamStart { m_upstreamStart.computeNodeAfterPosition() }; |
| 438 | RefPtr nodeAfterDownstreamStart { m_downstreamStart.computeNodeAfterPosition() }; |
rniwa@webkit.org | 3810eff | 2012-06-27 00:00:52 +0000 | [diff] [blame] | 439 | // Upstream end will appear before BR due to canonicalization |
cdumez@apple.com | e1840be | 2021-09-20 21:45:40 +0000 | [diff] [blame] | 440 | RefPtr nodeAfterUpstreamEnd { m_upstreamEnd.computeNodeAfterPosition() }; |
rniwa@webkit.org | 3810eff | 2012-06-27 00:00:52 +0000 | [diff] [blame] | 441 | |
| 442 | if (!nodeAfterUpstreamStart || !nodeAfterDownstreamStart) |
| 443 | return false; |
| 444 | |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 445 | // 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] | 446 | bool upstreamStartIsBR = nodeAfterUpstreamStart->hasTagName(brTag); |
| 447 | bool downstreamStartIsBR = nodeAfterDownstreamStart->hasTagName(brTag); |
enrica@apple.com | d2ba815 | 2013-07-09 02:48:19 +0000 | [diff] [blame] | 448 | // We should consider that the BR is on a line by itself also when we have <br><br>. This test should be true only |
| 449 | // when the two elements are siblings and should be false in a case like <div><br></div><br>. |
| 450 | bool isBROnLineByItself = upstreamStartIsBR && downstreamStartIsBR && ((nodeAfterDownstreamStart == nodeAfterUpstreamEnd) || (nodeAfterUpstreamEnd && nodeAfterUpstreamEnd->hasTagName(brTag) && nodeAfterUpstreamStart->nextSibling() == nodeAfterUpstreamEnd)); |
| 451 | |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 452 | if (isBROnLineByItself) { |
cdumez@apple.com | 2fecce7 | 2017-05-05 05:09:56 +0000 | [diff] [blame] | 453 | removeNode(*nodeAfterDownstreamStart); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 454 | return true; |
| 455 | } |
| 456 | |
rniwa@webkit.org | 3810eff | 2012-06-27 00:00:52 +0000 | [diff] [blame] | 457 | // FIXME: This code doesn't belong in here. |
| 458 | // 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] | 459 | if (upstreamStartIsBR && downstreamStartIsBR |
rniwa@webkit.org | 098af8c | 2021-04-25 05:59:59 +0000 | [diff] [blame] | 460 | && !(isStartOfBlock(positionBeforeNode(nodeAfterUpstreamStart.get())) && isEndOfBlock(positionAfterNode(nodeAfterDownstreamStart.get()))) |
a.bah@samsung.com | b024701 | 2013-08-23 11:38:26 +0000 | [diff] [blame] | 461 | && (!nodeAfterUpstreamEnd || nodeAfterUpstreamEnd->hasTagName(brTag) || nodeAfterUpstreamEnd->previousSibling() != nodeAfterUpstreamStart)) { |
mitz@apple.com | fc292c3 | 2009-05-03 04:09:41 +0000 | [diff] [blame] | 462 | m_startsAtEmptyLine = true; |
harrison | 581c07b | 2006-11-06 18:08:52 +0000 | [diff] [blame] | 463 | m_endingPosition = m_downstreamEnd; |
| 464 | } |
| 465 | |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 466 | return false; |
| 467 | } |
| 468 | |
kalman@chromium.org | 4cb651d | 2011-03-31 07:38:10 +0000 | [diff] [blame] | 469 | static Position firstEditablePositionInNode(Node* node) |
| 470 | { |
| 471 | ASSERT(node); |
cdumez@apple.com | e1840be | 2021-09-20 21:45:40 +0000 | [diff] [blame] | 472 | RefPtr next { node }; |
antti@apple.com | f02be1e | 2013-12-21 18:51:04 +0000 | [diff] [blame] | 473 | while (next && !next->hasEditableStyle()) |
cdumez@apple.com | b7f4850 | 2015-01-26 22:36:36 +0000 | [diff] [blame] | 474 | next = NodeTraversal::next(*next, node); |
rniwa@webkit.org | 098af8c | 2021-04-25 05:59:59 +0000 | [diff] [blame] | 475 | return next ? firstPositionInOrBeforeNode(next.get()) : Position(); |
kalman@chromium.org | 4cb651d | 2011-03-31 07:38:10 +0000 | [diff] [blame] | 476 | } |
| 477 | |
zalan@apple.com | 5386267 | 2017-02-10 22:36:12 +0000 | [diff] [blame] | 478 | void DeleteSelectionCommand::insertBlockPlaceholderForTableCellIfNeeded(Element& element) |
| 479 | { |
| 480 | // Make sure empty cell has some height. |
rniwa@webkit.org | 098af8c | 2021-04-25 05:59:59 +0000 | [diff] [blame] | 481 | { |
| 482 | ScriptDisallowedScope scriptDisallowedScope; |
| 483 | auto* renderer = element.renderer(); |
| 484 | if (!is<RenderTableCell>(renderer)) |
| 485 | return; |
| 486 | if (downcast<RenderTableCell>(*renderer).contentHeight() > 0) |
| 487 | return; |
| 488 | } |
zalan@apple.com | 5386267 | 2017-02-10 22:36:12 +0000 | [diff] [blame] | 489 | insertBlockPlaceholder(firstEditablePositionInNode(&element)); |
| 490 | } |
| 491 | |
| 492 | void DeleteSelectionCommand::removeNodeUpdatingStates(Node& node, ShouldAssumeContentIsAlwaysEditable shouldAssumeContentIsAlwaysEditable) |
| 493 | { |
commit-queue@webkit.org | a58a7fb | 2021-09-07 08:11:28 +0000 | [diff] [blame] | 494 | if (&node == m_startBlock) { |
| 495 | auto prev = VisiblePosition(firstPositionInNode(m_startBlock.get())).previous(); |
| 496 | if (!prev.isNull() && !isEndOfBlock(prev)) |
| 497 | m_needPlaceholder = true; |
| 498 | } else if (&node == m_endBlock) { |
| 499 | auto next = VisiblePosition(lastPositionInNode(m_endBlock.get())).next(); |
| 500 | if (!next.isNull() && !isStartOfBlock(next)) |
| 501 | m_needPlaceholder = true; |
| 502 | } |
zalan@apple.com | 5386267 | 2017-02-10 22:36:12 +0000 | [diff] [blame] | 503 | |
| 504 | // FIXME: Update the endpoints of the range being deleted. |
| 505 | updatePositionForNodeRemoval(m_endingPosition, node); |
| 506 | updatePositionForNodeRemoval(m_leadingWhitespace, node); |
| 507 | updatePositionForNodeRemoval(m_trailingWhitespace, node); |
| 508 | |
cdumez@apple.com | 2fecce7 | 2017-05-05 05:09:56 +0000 | [diff] [blame] | 509 | CompositeEditCommand::removeNode(node, shouldAssumeContentIsAlwaysEditable); |
zalan@apple.com | 5386267 | 2017-02-10 22:36:12 +0000 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | static inline bool shouldRemoveContentOnly(const Node& node) |
| 513 | { |
| 514 | return isTableStructureNode(&node) || node.isRootEditableElement(); |
| 515 | } |
| 516 | |
cdumez@apple.com | 2fecce7 | 2017-05-05 05:09:56 +0000 | [diff] [blame] | 517 | void DeleteSelectionCommand::removeNode(Node& node, ShouldAssumeContentIsAlwaysEditable shouldAssumeContentIsAlwaysEditable) |
justing | 11ec52e | 2006-06-07 00:09:37 +0000 | [diff] [blame] | 518 | { |
commit-queue@webkit.org | 7290dd9 | 2021-05-07 21:54:02 +0000 | [diff] [blame] | 519 | if (!node.parentNode()) |
| 520 | return; |
| 521 | |
cdumez@apple.com | 2fecce7 | 2017-05-05 05:09:56 +0000 | [diff] [blame] | 522 | Ref<Node> protectedNode = node; |
| 523 | 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] | 524 | // 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] | 525 | if (!node.parentNode()->hasEditableStyle()) { |
justing | 06a653c | 2007-04-04 20:49:52 +0000 | [diff] [blame] | 526 | // Don't remove non-editable atomic nodes. |
cdumez@apple.com | 2fecce7 | 2017-05-05 05:09:56 +0000 | [diff] [blame] | 527 | if (!node.firstChild()) |
justing | 06a653c | 2007-04-04 20:49:52 +0000 | [diff] [blame] | 528 | return; |
| 529 | // Search this non-editable region for editable regions to empty. |
cdumez@apple.com | e1840be | 2021-09-20 21:45:40 +0000 | [diff] [blame] | 530 | RefPtr child { node.firstChild() }; |
justing | 06a653c | 2007-04-04 20:49:52 +0000 | [diff] [blame] | 531 | while (child) { |
cdumez@apple.com | e1840be | 2021-09-20 21:45:40 +0000 | [diff] [blame] | 532 | RefPtr nextChild { child->nextSibling() }; |
cdumez@apple.com | 2fecce7 | 2017-05-05 05:09:56 +0000 | [diff] [blame] | 533 | removeNode(*child, shouldAssumeContentIsAlwaysEditable); |
justing | 06a653c | 2007-04-04 20:49:52 +0000 | [diff] [blame] | 534 | // Bail if nextChild is no longer node's child. |
cdumez@apple.com | 2fecce7 | 2017-05-05 05:09:56 +0000 | [diff] [blame] | 535 | if (nextChild && nextChild->parentNode() != &node) |
justing | 06a653c | 2007-04-04 20:49:52 +0000 | [diff] [blame] | 536 | return; |
rniwa@webkit.org | 098af8c | 2021-04-25 05:59:59 +0000 | [diff] [blame] | 537 | child = WTFMove(nextChild); |
justing | 06a653c | 2007-04-04 20:49:52 +0000 | [diff] [blame] | 538 | } |
| 539 | |
| 540 | // Don't remove editable regions that are inside non-editable ones, just clear them. |
| 541 | return; |
| 542 | } |
| 543 | } |
| 544 | |
cdumez@apple.com | 2fecce7 | 2017-05-05 05:09:56 +0000 | [diff] [blame] | 545 | if (shouldRemoveContentOnly(node)) { |
justing | 11ec52e | 2006-06-07 00:09:37 +0000 | [diff] [blame] | 546 | // Do not remove an element of table structure; remove its contents. |
| 547 | // Likewise for the root editable element. |
cdumez@apple.com | e1840be | 2021-09-20 21:45:40 +0000 | [diff] [blame] | 548 | RefPtr child { NodeTraversal::next(node, &node) }; |
justing | 11ec52e | 2006-06-07 00:09:37 +0000 | [diff] [blame] | 549 | while (child) { |
zalan@apple.com | 5386267 | 2017-02-10 22:36:12 +0000 | [diff] [blame] | 550 | if (shouldRemoveContentOnly(*child)) { |
cdumez@apple.com | 2fecce7 | 2017-05-05 05:09:56 +0000 | [diff] [blame] | 551 | child = NodeTraversal::next(*child, &node); |
zalan@apple.com | 5386267 | 2017-02-10 22:36:12 +0000 | [diff] [blame] | 552 | continue; |
| 553 | } |
cdumez@apple.com | e1840be | 2021-09-20 21:45:40 +0000 | [diff] [blame] | 554 | RefPtr nextChild { NodeTraversal::nextSkippingChildren(*child, &node) }; |
rniwa@webkit.org | 098af8c | 2021-04-25 05:59:59 +0000 | [diff] [blame] | 555 | removeNodeUpdatingStates(*child, shouldAssumeContentIsAlwaysEditable); |
| 556 | child = WTFMove(nextChild); |
justing | 11ec52e | 2006-06-07 00:09:37 +0000 | [diff] [blame] | 557 | } |
| 558 | |
cdumez@apple.com | 2fecce7 | 2017-05-05 05:09:56 +0000 | [diff] [blame] | 559 | ASSERT(is<Element>(node)); |
| 560 | auto& element = downcast<Element>(node); |
akling@apple.com | d455eb6 | 2013-09-01 05:30:31 +0000 | [diff] [blame] | 561 | document().updateLayoutIgnorePendingStylesheets(); |
zalan@apple.com | 5386267 | 2017-02-10 22:36:12 +0000 | [diff] [blame] | 562 | // Check if we need to insert a placeholder for descendant table cells. |
cdumez@apple.com | e1840be | 2021-09-20 21:45:40 +0000 | [diff] [blame] | 563 | RefPtr descendant { ElementTraversal::next(element, &element) }; |
zalan@apple.com | 5386267 | 2017-02-10 22:36:12 +0000 | [diff] [blame] | 564 | while (descendant) { |
cdumez@apple.com | e1840be | 2021-09-20 21:45:40 +0000 | [diff] [blame] | 565 | RefPtr nextDescendant { ElementTraversal::next(*descendant, &element) }; |
rniwa@webkit.org | 098af8c | 2021-04-25 05:59:59 +0000 | [diff] [blame] | 566 | insertBlockPlaceholderForTableCellIfNeeded(*descendant); |
| 567 | descendant = WTFMove(nextDescendant); |
kalman@chromium.org | 4cb651d | 2011-03-31 07:38:10 +0000 | [diff] [blame] | 568 | } |
zalan@apple.com | 5386267 | 2017-02-10 22:36:12 +0000 | [diff] [blame] | 569 | insertBlockPlaceholderForTableCellIfNeeded(element); |
justing | 11ec52e | 2006-06-07 00:09:37 +0000 | [diff] [blame] | 570 | return; |
| 571 | } |
cdumez@apple.com | 2fecce7 | 2017-05-05 05:09:56 +0000 | [diff] [blame] | 572 | removeNodeUpdatingStates(node, shouldAssumeContentIsAlwaysEditable); |
justing | f81641c | 2006-06-14 23:23:50 +0000 | [diff] [blame] | 573 | } |
| 574 | |
andersca@apple.com | d8dda09 | 2009-01-23 19:52:19 +0000 | [diff] [blame] | 575 | static void updatePositionForTextRemoval(Node* node, int offset, int count, Position& position) |
justing | f81641c | 2006-06-14 23:23:50 +0000 | [diff] [blame] | 576 | { |
rniwa@webkit.org | 186f5bb | 2011-01-18 21:33:34 +0000 | [diff] [blame] | 577 | if (position.anchorType() != Position::PositionIsOffsetInAnchor || position.containerNode() != node) |
| 578 | return; |
| 579 | |
| 580 | if (position.offsetInContainerNode() > offset + count) |
| 581 | position.moveToOffset(position.offsetInContainerNode() - count); |
| 582 | else if (position.offsetInContainerNode() > offset) |
| 583 | position.moveToOffset(offset); |
justing | 11ec52e | 2006-06-07 00:09:37 +0000 | [diff] [blame] | 584 | } |
| 585 | |
cdumez@apple.com | ecdce03 | 2017-05-03 18:41:47 +0000 | [diff] [blame] | 586 | void DeleteSelectionCommand::deleteTextFromNode(Text& node, unsigned offset, unsigned count) |
justing | 11ec52e | 2006-06-07 00:09:37 +0000 | [diff] [blame] | 587 | { |
justing | f81641c | 2006-06-14 23:23:50 +0000 | [diff] [blame] | 588 | // FIXME: Update the endpoints of the range being deleted. |
cdumez@apple.com | ecdce03 | 2017-05-03 18:41:47 +0000 | [diff] [blame] | 589 | updatePositionForTextRemoval(&node, offset, count, m_endingPosition); |
| 590 | updatePositionForTextRemoval(&node, offset, count, m_leadingWhitespace); |
| 591 | updatePositionForTextRemoval(&node, offset, count, m_trailingWhitespace); |
| 592 | updatePositionForTextRemoval(&node, offset, count, m_downstreamEnd); |
justing | 11ec52e | 2006-06-07 00:09:37 +0000 | [diff] [blame] | 593 | |
| 594 | CompositeEditCommand::deleteTextFromNode(node, offset, count); |
| 595 | } |
| 596 | |
commit-queue@webkit.org | 72e6348 | 2012-08-18 00:31:00 +0000 | [diff] [blame] | 597 | void DeleteSelectionCommand::makeStylingElementsDirectChildrenOfEditableRootToPreventStyleLoss() |
| 598 | { |
darin@apple.com | ea2c7f8 | 2020-04-26 15:33:03 +0000 | [diff] [blame] | 599 | auto range = m_selectionToDelete.toNormalizedRange(); |
| 600 | if (!range) |
| 601 | return; |
| 602 | auto nodes = intersectingNodes(*range).begin(); |
| 603 | while (nodes) { |
cdumez@apple.com | 6674f14 | 2021-09-22 02:55:27 +0000 | [diff] [blame] | 604 | Ref node = *nodes; |
darin@apple.com | ea2c7f8 | 2020-04-26 15:33:03 +0000 | [diff] [blame] | 605 | auto shouldMove = is<HTMLLinkElement>(node) |
| 606 | || (is<HTMLStyleElement>(node) && !downcast<HTMLStyleElement>(node.get()).hasAttributeWithoutSynchronization(scopedAttr)); |
| 607 | if (!shouldMove) |
| 608 | nodes.advance(); |
| 609 | else { |
| 610 | nodes.advanceSkippingChildren(); |
cdumez@apple.com | e1840be | 2021-09-20 21:45:40 +0000 | [diff] [blame] | 611 | if (RefPtr rootEditableElement = node->rootEditableElement()) { |
darin@apple.com | ea2c7f8 | 2020-04-26 15:33:03 +0000 | [diff] [blame] | 612 | removeNode(node.get()); |
| 613 | appendNode(node.get(), *rootEditableElement); |
rniwa@webkit.org | d14c7ce | 2013-05-21 03:18:22 +0000 | [diff] [blame] | 614 | } |
commit-queue@webkit.org | 72e6348 | 2012-08-18 00:31:00 +0000 | [diff] [blame] | 615 | } |
commit-queue@webkit.org | 72e6348 | 2012-08-18 00:31:00 +0000 | [diff] [blame] | 616 | } |
| 617 | } |
| 618 | |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 619 | void DeleteSelectionCommand::handleGeneralDelete() |
| 620 | { |
rniwa@webkit.org | 23ae78c | 2011-10-13 20:58:42 +0000 | [diff] [blame] | 621 | if (m_upstreamStart.isNull()) |
| 622 | return; |
| 623 | |
eric@webkit.org | 4e32ebc | 2009-04-30 01:09:57 +0000 | [diff] [blame] | 624 | int startOffset = m_upstreamStart.deprecatedEditingOffset(); |
cdumez@apple.com | e1840be | 2021-09-20 21:45:40 +0000 | [diff] [blame] | 625 | RefPtr startNode { m_upstreamStart.deprecatedNode() }; |
rniwa@webkit.org | 098af8c | 2021-04-25 05:59:59 +0000 | [diff] [blame] | 626 | |
commit-queue@webkit.org | 72e6348 | 2012-08-18 00:31:00 +0000 | [diff] [blame] | 627 | makeStylingElementsDirectChildrenOfEditableRootToPreventStyleLoss(); |
rniwa@webkit.org | 23ae78c | 2011-10-13 20:58:42 +0000 | [diff] [blame] | 628 | |
lweintraub | 635ec2a | 2006-07-13 18:28:28 +0000 | [diff] [blame] | 629 | // 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] | 630 | if (startNode == m_startBlock && !startOffset && canHaveChildrenForEditing(*startNode) && !is<HTMLTableElement>(*startNode)) { |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 631 | startOffset = 0; |
cdumez@apple.com | b7f4850 | 2015-01-26 22:36:36 +0000 | [diff] [blame] | 632 | startNode = NodeTraversal::next(*startNode); |
rniwa@webkit.org | 4df07a2 | 2012-01-31 20:41:05 +0000 | [diff] [blame] | 633 | if (!startNode) |
| 634 | return; |
justing | e6f80c1 | 2006-06-09 04:57:51 +0000 | [diff] [blame] | 635 | } |
| 636 | |
darin@apple.com | d385be4 | 2016-05-14 20:09:50 +0000 | [diff] [blame] | 637 | int startNodeCaretMaxOffset = caretMaxOffset(*startNode); |
| 638 | if (startOffset >= startNodeCaretMaxOffset && is<Text>(*startNode)) { |
cdumez@apple.com | a38df4d | 2014-09-29 17:20:18 +0000 | [diff] [blame] | 639 | Text& text = downcast<Text>(*startNode); |
darin@apple.com | d385be4 | 2016-05-14 20:09:50 +0000 | [diff] [blame] | 640 | if (text.length() > static_cast<unsigned>(startNodeCaretMaxOffset)) |
cdumez@apple.com | ecdce03 | 2017-05-03 18:41:47 +0000 | [diff] [blame] | 641 | deleteTextFromNode(text, startNodeCaretMaxOffset, text.length() - startNodeCaretMaxOffset); |
justing | e6f80c1 | 2006-06-09 04:57:51 +0000 | [diff] [blame] | 642 | } |
| 643 | |
darin@apple.com | d385be4 | 2016-05-14 20:09:50 +0000 | [diff] [blame] | 644 | if (startOffset >= lastOffsetForEditing(*startNode)) { |
cdumez@apple.com | b7f4850 | 2015-01-26 22:36:36 +0000 | [diff] [blame] | 645 | startNode = NodeTraversal::nextSkippingChildren(*startNode); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 646 | startOffset = 0; |
| 647 | } |
| 648 | |
| 649 | // Done adjusting the start. See if we're all done. |
justing | 11ec52e | 2006-06-07 00:09:37 +0000 | [diff] [blame] | 650 | if (!startNode) |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 651 | return; |
| 652 | |
rniwa@webkit.org | 62b1697 | 2011-02-21 09:28:48 +0000 | [diff] [blame] | 653 | if (startNode == m_downstreamEnd.deprecatedNode()) { |
commit-queue@webkit.org | 2aa8d03 | 2010-09-22 20:43:26 +0000 | [diff] [blame] | 654 | if (m_downstreamEnd.deprecatedEditingOffset() - startOffset > 0) { |
cdumez@apple.com | a9c60c9 | 2014-10-02 19:39:41 +0000 | [diff] [blame] | 655 | if (is<Text>(*startNode)) { |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 656 | // in a text node that needs to be trimmed |
cdumez@apple.com | a38df4d | 2014-09-29 17:20:18 +0000 | [diff] [blame] | 657 | Text& text = downcast<Text>(*startNode); |
cdumez@apple.com | ecdce03 | 2017-05-03 18:41:47 +0000 | [diff] [blame] | 658 | deleteTextFromNode(text, startOffset, m_downstreamEnd.deprecatedEditingOffset() - startOffset); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 659 | } else { |
cdumez@apple.com | ecdce03 | 2017-05-03 18:41:47 +0000 | [diff] [blame] | 660 | removeChildrenInRange(*startNode, startOffset, m_downstreamEnd.deprecatedEditingOffset()); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 661 | m_endingPosition = m_upstreamStart; |
| 662 | } |
| 663 | } |
commit-queue@webkit.org | 2aa8d03 | 2010-09-22 20:43:26 +0000 | [diff] [blame] | 664 | |
| 665 | // The selection to delete is all in one node. |
| 666 | if (!startNode->renderer() || (!startOffset && m_downstreamEnd.atLastEditingPositionForNode())) |
cdumez@apple.com | 2fecce7 | 2017-05-05 05:09:56 +0000 | [diff] [blame] | 667 | removeNode(*startNode); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 668 | } |
| 669 | else { |
rniwa@webkit.org | 62b1697 | 2011-02-21 09:28:48 +0000 | [diff] [blame] | 670 | bool startNodeWasDescendantOfEndNode = m_upstreamStart.deprecatedNode()->isDescendantOf(m_downstreamEnd.deprecatedNode()); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 671 | // The selection to delete spans more than one node. |
rniwa@webkit.org | 098af8c | 2021-04-25 05:59:59 +0000 | [diff] [blame] | 672 | auto node = startNode.copyRef(); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 673 | |
| 674 | if (startOffset > 0) { |
rniwa@webkit.org | 098af8c | 2021-04-25 05:59:59 +0000 | [diff] [blame] | 675 | if (is<Text>(*node)) { |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 676 | // in a text node that needs to be trimmed |
rniwa@webkit.org | 098af8c | 2021-04-25 05:59:59 +0000 | [diff] [blame] | 677 | Text& text = downcast<Text>(*startNode); |
cdumez@apple.com | ecdce03 | 2017-05-03 18:41:47 +0000 | [diff] [blame] | 678 | deleteTextFromNode(text, startOffset, text.length() - startOffset); |
rniwa@webkit.org | 098af8c | 2021-04-25 05:59:59 +0000 | [diff] [blame] | 679 | node = NodeTraversal::next(*startNode); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 680 | } else { |
cdumez@apple.com | d997517 | 2014-09-17 02:00:40 +0000 | [diff] [blame] | 681 | node = startNode->traverseToChildAt(startOffset); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 682 | } |
cdumez@apple.com | a9c60c9 | 2014-10-02 19:39:41 +0000 | [diff] [blame] | 683 | } else if (startNode == m_upstreamEnd.deprecatedNode() && is<Text>(*startNode)) { |
rniwa@webkit.org | 098af8c | 2021-04-25 05:59:59 +0000 | [diff] [blame] | 684 | Text& text = downcast<Text>(*startNode); |
cdumez@apple.com | ecdce03 | 2017-05-03 18:41:47 +0000 | [diff] [blame] | 685 | deleteTextFromNode(text, 0, m_upstreamEnd.deprecatedEditingOffset()); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 686 | } |
| 687 | |
| 688 | // handle deleting all nodes that are completely selected |
rniwa@webkit.org | fa66f47 | 2021-03-04 01:48:53 +0000 | [diff] [blame] | 689 | while (node && node != m_downstreamEnd.deprecatedNode() && !m_downstreamEnd.isNull() && !m_downstreamEnd.isOrphan()) { |
darin@apple.com | d1d65f9 | 2020-09-02 21:01:59 +0000 | [diff] [blame] | 690 | if (firstPositionInOrBeforeNode(node.get()) >= m_downstreamEnd) { |
antti@apple.com | b498fe8 | 2012-12-12 03:12:23 +0000 | [diff] [blame] | 691 | // NodeTraversal::nextSkippingChildren just blew past the end position, so stop deleting |
cdumez@apple.com | b7f4850 | 2015-01-26 22:36:36 +0000 | [diff] [blame] | 692 | node = nullptr; |
cdumez@apple.com | ca12b50 | 2016-11-17 06:24:13 +0000 | [diff] [blame] | 693 | } else if (!m_downstreamEnd.deprecatedNode()->isDescendantOf(*node)) { |
cdumez@apple.com | b7f4850 | 2015-01-26 22:36:36 +0000 | [diff] [blame] | 694 | RefPtr<Node> nextNode = NodeTraversal::nextSkippingChildren(*node); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 695 | // if we just removed a node from the end container, update end position so the |
| 696 | // check above will work |
cdumez@apple.com | 2cbdf51 | 2014-11-11 00:02:57 +0000 | [diff] [blame] | 697 | updatePositionForNodeRemoval(m_downstreamEnd, *node); |
cdumez@apple.com | 2fecce7 | 2017-05-05 05:09:56 +0000 | [diff] [blame] | 698 | removeNode(*node); |
justing | 2475432 | 2007-07-09 20:27:05 +0000 | [diff] [blame] | 699 | node = nextNode.get(); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 700 | } else { |
cdumez@apple.com | e1840be | 2021-09-20 21:45:40 +0000 | [diff] [blame] | 701 | RefPtr lastDescendant { node->lastDescendant() }; |
rniwa@webkit.org | 098af8c | 2021-04-25 05:59:59 +0000 | [diff] [blame] | 702 | if (m_downstreamEnd.deprecatedNode() == lastDescendant && m_downstreamEnd.deprecatedEditingOffset() >= caretMaxOffset(*lastDescendant)) { |
cdumez@apple.com | 2fecce7 | 2017-05-05 05:09:56 +0000 | [diff] [blame] | 703 | removeNode(*node); |
cdumez@apple.com | b7f4850 | 2015-01-26 22:36:36 +0000 | [diff] [blame] | 704 | node = nullptr; |
justing | 9556126 | 2006-06-21 21:22:50 +0000 | [diff] [blame] | 705 | } else |
cdumez@apple.com | b7f4850 | 2015-01-26 22:36:36 +0000 | [diff] [blame] | 706 | node = NodeTraversal::next(*node); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 707 | } |
| 708 | } |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 709 | |
rniwa@webkit.org | 1484cab | 2019-06-22 04:04:23 +0000 | [diff] [blame] | 710 | if (!m_downstreamEnd.isNull() && !m_downstreamEnd.isOrphan() && m_downstreamEnd.deprecatedNode() != startNode |
| 711 | && !m_upstreamStart.deprecatedNode()->isDescendantOf(m_downstreamEnd.deprecatedNode()) |
| 712 | && m_downstreamEnd.deprecatedEditingOffset() >= caretMinOffset(*m_downstreamEnd.deprecatedNode())) { |
darin@apple.com | bc63aba | 2016-05-15 22:08:52 +0000 | [diff] [blame] | 713 | if (m_downstreamEnd.atLastEditingPositionForNode() && !canHaveChildrenForEditing(*m_downstreamEnd.deprecatedNode())) { |
justing | 395a18e | 2007-06-26 06:52:15 +0000 | [diff] [blame] | 714 | // 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] | 715 | removeNode(*m_downstreamEnd.deprecatedNode()); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 716 | } else { |
cdumez@apple.com | a9c60c9 | 2014-10-02 19:39:41 +0000 | [diff] [blame] | 717 | if (is<Text>(*m_downstreamEnd.deprecatedNode())) { |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 718 | // in a text node that needs to be trimmed |
cdumez@apple.com | a38df4d | 2014-09-29 17:20:18 +0000 | [diff] [blame] | 719 | Text& text = downcast<Text>(*m_downstreamEnd.deprecatedNode()); |
rniwa@webkit.org | 098af8c | 2021-04-25 05:59:59 +0000 | [diff] [blame] | 720 | if (m_downstreamEnd.deprecatedEditingOffset() > 0) |
cdumez@apple.com | ecdce03 | 2017-05-03 18:41:47 +0000 | [diff] [blame] | 721 | deleteTextFromNode(text, 0, m_downstreamEnd.deprecatedEditingOffset()); |
rniwa@webkit.org | 62b1697 | 2011-02-21 09:28:48 +0000 | [diff] [blame] | 722 | // Remove children of m_downstreamEnd.deprecatedNode() that come after m_upstreamStart. |
| 723 | // 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] | 724 | // and m_upstreamStart has been removed from the document, because then we don't |
| 725 | // know how many children to remove. |
| 726 | // FIXME: Make m_upstreamStart a position we update as we remove content, then we can |
| 727 | // always know which children to remove. |
cdumez@apple.com | 85cc262 | 2017-02-02 21:29:15 +0000 | [diff] [blame] | 728 | } else if (!(startNodeWasDescendantOfEndNode && !m_upstreamStart.anchorNode()->isConnected())) { |
cdumez@apple.com | 3751070 | 2014-09-16 18:28:57 +0000 | [diff] [blame] | 729 | unsigned offset = 0; |
rniwa@webkit.org | 62b1697 | 2011-02-21 09:28:48 +0000 | [diff] [blame] | 730 | if (m_upstreamStart.deprecatedNode()->isDescendantOf(m_downstreamEnd.deprecatedNode())) { |
cdumez@apple.com | e1840be | 2021-09-20 21:45:40 +0000 | [diff] [blame] | 731 | RefPtr n { m_upstreamStart.deprecatedNode() }; |
rniwa@webkit.org | 62b1697 | 2011-02-21 09:28:48 +0000 | [diff] [blame] | 732 | while (n && n->parentNode() != m_downstreamEnd.deprecatedNode()) |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 733 | n = n->parentNode(); |
| 734 | if (n) |
cdumez@apple.com | 3751070 | 2014-09-16 18:28:57 +0000 | [diff] [blame] | 735 | offset = n->computeNodeIndex() + 1; |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 736 | } |
cdumez@apple.com | ecdce03 | 2017-05-03 18:41:47 +0000 | [diff] [blame] | 737 | removeChildrenInRange(*m_downstreamEnd.deprecatedNode(), offset, m_downstreamEnd.deprecatedEditingOffset()); |
darin@apple.com | e48d273 | 2020-09-21 20:28:24 +0000 | [diff] [blame] | 738 | m_downstreamEnd = makeDeprecatedLegacyPosition(m_downstreamEnd.deprecatedNode(), offset); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 739 | } |
| 740 | } |
| 741 | } |
| 742 | } |
| 743 | } |
| 744 | |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 745 | void DeleteSelectionCommand::fixupWhitespace() |
| 746 | { |
akling@apple.com | d455eb6 | 2013-09-01 05:30:31 +0000 | [diff] [blame] | 747 | document().updateLayoutIgnorePendingStylesheets(); |
justing | f81641c | 2006-06-14 23:23:50 +0000 | [diff] [blame] | 748 | // 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] | 749 | 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] | 750 | Text& textNode = downcast<Text>(*m_leadingWhitespace.deprecatedNode()); |
| 751 | ASSERT(!textNode.renderer() || textNode.renderer()->style().collapseWhiteSpace()); |
cdumez@apple.com | ecdce03 | 2017-05-03 18:41:47 +0000 | [diff] [blame] | 752 | replaceTextInNodePreservingMarkers(textNode, m_leadingWhitespace.deprecatedEditingOffset(), 1, nonBreakingSpaceString()); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 753 | } |
cdumez@apple.com | a9c60c9 | 2014-10-02 19:39:41 +0000 | [diff] [blame] | 754 | 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] | 755 | Text& textNode = downcast<Text>(*m_trailingWhitespace.deprecatedNode()); |
| 756 | ASSERT(!textNode.renderer() || textNode.renderer()->style().collapseWhiteSpace()); |
cdumez@apple.com | ecdce03 | 2017-05-03 18:41:47 +0000 | [diff] [blame] | 757 | replaceTextInNodePreservingMarkers(textNode, m_trailingWhitespace.deprecatedEditingOffset(), 1, nonBreakingSpaceString()); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 758 | } |
| 759 | } |
| 760 | |
justing | f81641c | 2006-06-14 23:23:50 +0000 | [diff] [blame] | 761 | // If a selection starts in one block and ends in another, we have to merge to bring content before the |
| 762 | // start together with content after the end. |
justing | 217ded8 | 2006-04-04 20:52:10 +0000 | [diff] [blame] | 763 | void DeleteSelectionCommand::mergeParagraphs() |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 764 | { |
justin.garcia@apple.com | 7e95c7b | 2008-11-12 20:08:06 +0000 | [diff] [blame] | 765 | if (!m_mergeBlocksAfterDelete) { |
| 766 | if (m_pruneStartBlockIfNecessary) { |
justin.garcia@apple.com | 7e95c7b | 2008-11-12 20:08:06 +0000 | [diff] [blame] | 767 | // 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] | 768 | prune(m_startBlock.get()); |
justin.garcia@apple.com | 7e95c7b | 2008-11-12 20:08:06 +0000 | [diff] [blame] | 769 | // Removing the start block during a deletion is usually an indication that we need |
| 770 | // a placeholder, but not in this case. |
| 771 | m_needPlaceholder = false; |
| 772 | } |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 773 | return; |
justin.garcia@apple.com | 7e95c7b | 2008-11-12 20:08:06 +0000 | [diff] [blame] | 774 | } |
| 775 | |
| 776 | // It shouldn't have been asked to both try and merge content into the start block and prune it. |
| 777 | ASSERT(!m_pruneStartBlockIfNecessary); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 778 | |
justing | 217ded8 | 2006-04-04 20:52:10 +0000 | [diff] [blame] | 779 | // FIXME: Deletion should adjust selection endpoints as it removes nodes so that we never get into this state (4099839). |
darin@apple.com | d1d65f9 | 2020-09-02 21:01:59 +0000 | [diff] [blame] | 780 | if (m_downstreamEnd.isNull() || m_upstreamStart.isNull() || m_downstreamEnd.isOrphan() || m_upstreamStart.isOrphan()) |
| 781 | return; |
cdumez@apple.com | 85cc262 | 2017-02-02 21:29:15 +0000 | [diff] [blame] | 782 | |
darin@apple.com | d1d65f9 | 2020-09-02 21:01:59 +0000 | [diff] [blame] | 783 | if (m_upstreamStart >= m_downstreamEnd) |
justing | ced6772 | 2006-05-01 21:43:03 +0000 | [diff] [blame] | 784 | return; |
darin@apple.com | d1d65f9 | 2020-09-02 21:01:59 +0000 | [diff] [blame] | 785 | |
justing | 217ded8 | 2006-04-04 20:52:10 +0000 | [diff] [blame] | 786 | VisiblePosition startOfParagraphToMove(m_downstreamEnd); |
| 787 | VisiblePosition mergeDestination(m_upstreamStart); |
| 788 | |
justin.garcia@apple.com | 60777c9 | 2007-11-14 22:33:07 +0000 | [diff] [blame] | 789 | // m_downstreamEnd's block has been emptied out by deletion. There is no content inside of it to |
| 790 | // move, so just remove it. |
cdumez@apple.com | e1840be | 2021-09-20 21:45:40 +0000 | [diff] [blame] | 791 | RefPtr endBlock { enclosingBlock(m_downstreamEnd.deprecatedNode()) }; |
cdumez@apple.com | 2fecce7 | 2017-05-05 05:09:56 +0000 | [diff] [blame] | 792 | if (!endBlock) |
| 793 | return; |
| 794 | |
| 795 | if (!endBlock->contains(startOfParagraphToMove.deepEquivalent().deprecatedNode()) || !startOfParagraphToMove.deepEquivalent().deprecatedNode()) { |
| 796 | removeNode(*endBlock); |
justin.garcia@apple.com | 60777c9 | 2007-11-14 22:33:07 +0000 | [diff] [blame] | 797 | return; |
| 798 | } |
| 799 | |
justing | ced6772 | 2006-05-01 21:43:03 +0000 | [diff] [blame] | 800 | // 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] | 801 | 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] | 802 | insertNodeAt(HTMLBRElement::create(document()), m_upstreamStart); |
justing | ced6772 | 2006-05-01 21:43:03 +0000 | [diff] [blame] | 803 | mergeDestination = VisiblePosition(m_upstreamStart); |
| 804 | } |
harrison | dec5e09 | 2006-01-25 21:05:02 +0000 | [diff] [blame] | 805 | |
justing | ced6772 | 2006-05-01 21:43:03 +0000 | [diff] [blame] | 806 | if (mergeDestination == startOfParagraphToMove) |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 807 | return; |
justing | 217ded8 | 2006-04-04 20:52:10 +0000 | [diff] [blame] | 808 | |
commit-queue@webkit.org | 10809db | 2013-10-24 19:18:30 +0000 | [diff] [blame] | 809 | VisiblePosition endOfParagraphToMove = endOfParagraph(startOfParagraphToMove, CanSkipOverEditingBoundary); |
justing | 217ded8 | 2006-04-04 20:52:10 +0000 | [diff] [blame] | 810 | |
darin | 2b0ff52 | 2006-05-03 19:32:21 +0000 | [diff] [blame] | 811 | if (mergeDestination == endOfParagraphToMove) |
| 812 | return; |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 813 | |
justing | e809f93 | 2006-06-23 22:36:12 +0000 | [diff] [blame] | 814 | // The rule for merging into an empty block is: only do so if its farther to the right. |
| 815 | // FIXME: Consider RTL. |
mitz@apple.com | fc292c3 | 2009-05-03 04:09:41 +0000 | [diff] [blame] | 816 | if (!m_startsAtEmptyLine && isStartOfParagraph(mergeDestination) && startOfParagraphToMove.absoluteCaretBounds().x() > mergeDestination.absoluteCaretBounds().x()) { |
rniwa@webkit.org | 62b1697 | 2011-02-21 09:28:48 +0000 | [diff] [blame] | 817 | if (mergeDestination.deepEquivalent().downstream().deprecatedNode()->hasTagName(brTag)) { |
cdumez@apple.com | e1840be | 2021-09-20 21:45:40 +0000 | [diff] [blame] | 818 | RefPtr nodeToRemove { mergeDestination.deepEquivalent().downstream().deprecatedNode() }; |
rniwa@webkit.org | 098af8c | 2021-04-25 05:59:59 +0000 | [diff] [blame] | 819 | removeNodeAndPruneAncestors(*nodeToRemove); |
enrica@apple.com | 25b91e3 | 2010-01-11 18:04:58 +0000 | [diff] [blame] | 820 | m_endingPosition = startOfParagraphToMove.deepEquivalent(); |
| 821 | return; |
| 822 | } |
justing | e809f93 | 2006-06-23 22:36:12 +0000 | [diff] [blame] | 823 | } |
| 824 | |
justin.garcia@apple.com | 619526a | 2009-11-05 05:25:24 +0000 | [diff] [blame] | 825 | // Block images, tables and horizontal rules cannot be made inline with content at mergeDestination. If there is |
| 826 | // any (!isStartOfParagraph(mergeDestination)), don't merge, just move the caret to just before the selection we deleted. |
| 827 | // See https://bugs.webkit.org/show_bug.cgi?id=25439 |
rniwa@webkit.org | 62b1697 | 2011-02-21 09:28:48 +0000 | [diff] [blame] | 828 | if (isRenderedAsNonInlineTableImageOrHR(startOfParagraphToMove.deepEquivalent().deprecatedNode()) && !isStartOfParagraph(mergeDestination)) { |
justin.garcia@apple.com | 619526a | 2009-11-05 05:25:24 +0000 | [diff] [blame] | 829 | m_endingPosition = m_upstreamStart; |
| 830 | return; |
| 831 | } |
| 832 | |
darin@apple.com | 5d3decc | 2020-08-01 15:50:36 +0000 | [diff] [blame] | 833 | auto range = makeSimpleRange(startOfParagraphToMove, endOfParagraphToMove); |
| 834 | if (!range) |
| 835 | return; |
| 836 | auto rangeToBeReplaced = makeSimpleRange(mergeDestination); |
| 837 | if (!rangeToBeReplaced) |
| 838 | return; |
| 839 | if (!document().editor().client()->shouldMoveRangeAfterDelete(*range, *rangeToBeReplaced)) |
justing | f90e28d | 2007-07-30 18:25:19 +0000 | [diff] [blame] | 840 | return; |
darin@apple.com | ea4947c | 2020-07-22 04:45:40 +0000 | [diff] [blame] | 841 | |
justin.garcia@apple.com | 60777c9 | 2007-11-14 22:33:07 +0000 | [diff] [blame] | 842 | // moveParagraphs will insert placeholders if it removes blocks that would require their use, don't let block |
| 843 | // removals that it does cause the insertion of *another* placeholder. |
| 844 | bool needPlaceholder = m_needPlaceholder; |
rniwa@webkit.org | d34b748 | 2010-08-12 02:05:11 +0000 | [diff] [blame] | 845 | bool paragraphToMergeIsEmpty = (startOfParagraphToMove == endOfParagraphToMove); |
| 846 | moveParagraph(startOfParagraphToMove, endOfParagraphToMove, mergeDestination, false, !paragraphToMergeIsEmpty); |
justin.garcia@apple.com | 60777c9 | 2007-11-14 22:33:07 +0000 | [diff] [blame] | 847 | m_needPlaceholder = needPlaceholder; |
justing | 93798f1 | 2006-06-08 04:04:38 +0000 | [diff] [blame] | 848 | // The endingPosition was likely clobbered by the move, so recompute it (moveParagraph selects the moved paragraph). |
shihchieh_lee@apple.com | 8788590 | 2020-05-14 00:21:50 +0000 | [diff] [blame] | 849 | |
| 850 | // FIXME (Bug 211793): endingSelection() becomes disconnected in moveParagraph |
commit-queue@webkit.org | bb107d2 | 2020-11-17 16:29:25 +0000 | [diff] [blame] | 851 | if (auto* anchorNode = endingSelection().start().anchorNode(); anchorNode && anchorNode->isConnected()) |
shihchieh_lee@apple.com | 8788590 | 2020-05-14 00:21:50 +0000 | [diff] [blame] | 852 | m_endingPosition = endingSelection().start(); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 853 | } |
| 854 | |
justing | 187d292 | 2007-07-17 21:05:21 +0000 | [diff] [blame] | 855 | void DeleteSelectionCommand::removePreviouslySelectedEmptyTableRows() |
| 856 | { |
commit-queue@webkit.org | 3390232 | 2021-05-07 08:08:40 +0000 | [diff] [blame] | 857 | // DeleteSelectionCommand::removeNode does not remove rows but only empties them in preparation for this function. |
| 858 | // Instead, DeleteSelectionCommand::removeNodeUpdatingStates is used below, which calls a raw CompositeEditCommand::removeNode and adjusts selection. |
cdumez@apple.com | 85cc262 | 2017-02-02 21:29:15 +0000 | [diff] [blame] | 859 | if (m_endTableRow && m_endTableRow->isConnected() && m_endTableRow != m_startTableRow) { |
cdumez@apple.com | e1840be | 2021-09-20 21:45:40 +0000 | [diff] [blame] | 860 | RefPtr row { m_endTableRow->previousSibling() }; |
justin.garcia@apple.com | 99a0554 | 2007-12-13 06:56:28 +0000 | [diff] [blame] | 861 | while (row && row != m_startTableRow) { |
cdumez@apple.com | e1840be | 2021-09-20 21:45:40 +0000 | [diff] [blame] | 862 | RefPtr previousRow { row->previousSibling() }; |
commit-queue@webkit.org | 3390232 | 2021-05-07 08:08:40 +0000 | [diff] [blame] | 863 | if (isTableRowEmpty(row.get())) |
| 864 | removeNodeUpdatingStates(*row, DoNotAssumeContentIsAlwaysEditable); |
rniwa@webkit.org | 098af8c | 2021-04-25 05:59:59 +0000 | [diff] [blame] | 865 | row = WTFMove(previousRow); |
justing | 187d292 | 2007-07-17 21:05:21 +0000 | [diff] [blame] | 866 | } |
| 867 | } |
| 868 | |
justing | 7d4a434 | 2007-10-13 00:40:48 +0000 | [diff] [blame] | 869 | // Remove empty rows after the start row. |
cdumez@apple.com | 85cc262 | 2017-02-02 21:29:15 +0000 | [diff] [blame] | 870 | if (m_startTableRow && m_startTableRow->isConnected() && m_startTableRow != m_endTableRow) { |
cdumez@apple.com | e1840be | 2021-09-20 21:45:40 +0000 | [diff] [blame] | 871 | RefPtr row { m_startTableRow->nextSibling() }; |
justin.garcia@apple.com | 99a0554 | 2007-12-13 06:56:28 +0000 | [diff] [blame] | 872 | while (row && row != m_endTableRow) { |
cdumez@apple.com | e1840be | 2021-09-20 21:45:40 +0000 | [diff] [blame] | 873 | RefPtr nextRow { row->nextSibling() }; |
rniwa@webkit.org | 098af8c | 2021-04-25 05:59:59 +0000 | [diff] [blame] | 874 | if (isTableRowEmpty(row.get())) |
commit-queue@webkit.org | 3390232 | 2021-05-07 08:08:40 +0000 | [diff] [blame] | 875 | removeNodeUpdatingStates(*row, DoNotAssumeContentIsAlwaysEditable); |
rniwa@webkit.org | 098af8c | 2021-04-25 05:59:59 +0000 | [diff] [blame] | 876 | row = WTFMove(nextRow); |
justing | 187d292 | 2007-07-17 21:05:21 +0000 | [diff] [blame] | 877 | } |
| 878 | } |
cdumez@apple.com | 85cc262 | 2017-02-02 21:29:15 +0000 | [diff] [blame] | 879 | |
| 880 | if (m_endTableRow && m_endTableRow->isConnected() && m_endTableRow != m_startTableRow) { |
justin.garcia@apple.com | 99a0554 | 2007-12-13 06:56:28 +0000 | [diff] [blame] | 881 | if (isTableRowEmpty(m_endTableRow.get())) { |
| 882 | // 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] | 883 | if (!m_endingPosition.deprecatedNode()->isDescendantOf(*m_endTableRow)) { |
justin.garcia@apple.com | 99a0554 | 2007-12-13 06:56:28 +0000 | [diff] [blame] | 884 | // FIXME: We probably shouldn't remove m_endTableRow unless it's fully selected, even if it is empty. |
| 885 | // We'll need to start adjusting the selection endpoints during deletion to know whether or not m_endTableRow |
| 886 | // was fully selected here. |
svillar@igalia.com | 9a8754f | 2021-02-12 11:29:32 +0000 | [diff] [blame] | 887 | removeNodeUpdatingStates(*m_endTableRow, DoNotAssumeContentIsAlwaysEditable); |
justin.garcia@apple.com | 99a0554 | 2007-12-13 06:56:28 +0000 | [diff] [blame] | 888 | } |
| 889 | } |
cdumez@apple.com | 85cc262 | 2017-02-02 21:29:15 +0000 | [diff] [blame] | 890 | } |
justing | 187d292 | 2007-07-17 21:05:21 +0000 | [diff] [blame] | 891 | } |
| 892 | |
justin.garcia@apple.com | d1eac9a | 2008-06-20 00:24:13 +0000 | [diff] [blame] | 893 | void DeleteSelectionCommand::calculateTypingStyleAfterDelete() |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 894 | { |
justin.garcia@apple.com | 6412551 | 2008-02-20 17:48:44 +0000 | [diff] [blame] | 895 | if (!m_typingStyle) |
| 896 | return; |
| 897 | |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 898 | // Compute the difference between the style before the delete and the style now |
mjs | 810e976 | 2006-01-09 21:39:14 +0000 | [diff] [blame] | 899 | // 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] | 900 | // 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] | 901 | // so the Frame::appliedEditing can set it after the whole composite command |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 902 | // has completed. |
justing | 503ccf1 | 2005-07-29 22:50:47 +0000 | [diff] [blame] | 903 | |
eseidel | ef50898 | 2006-01-03 09:19:17 +0000 | [diff] [blame] | 904 | // 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] | 905 | if (m_deleteIntoBlockquoteStyle && !enclosingNodeOfType(m_endingPosition, isMailBlockquote, CanCrossEditingBoundary)) |
eseidel | ef50898 | 2006-01-03 09:19:17 +0000 | [diff] [blame] | 906 | m_typingStyle = m_deleteIntoBlockquoteStyle; |
cdumez@apple.com | d839ea1 | 2015-07-04 19:42:18 +0000 | [diff] [blame] | 907 | m_deleteIntoBlockquoteStyle = nullptr; |
rniwa@webkit.org | 1de555e | 2009-08-07 21:36:24 +0000 | [diff] [blame] | 908 | |
rniwa@webkit.org | 7e06f4a | 2010-11-06 21:19:59 +0000 | [diff] [blame] | 909 | m_typingStyle->prepareToApplyAt(m_endingPosition); |
| 910 | if (m_typingStyle->isEmpty()) |
cdumez@apple.com | d839ea1 | 2015-07-04 19:42:18 +0000 | [diff] [blame] | 911 | m_typingStyle = nullptr; |
justin.garcia@apple.com | d1eac9a | 2008-06-20 00:24:13 +0000 | [diff] [blame] | 912 | // This is where we've deleted all traces of a style but not a whole paragraph (that's handled above). |
| 913 | // In this case if we start typing, the new characters should have the same style as the just deleted ones, |
| 914 | // but, if we change the selection, come back and start typing that style should be lost. Also see |
| 915 | // preserveTypingStyle() below. |
shihchieh_lee@apple.com | 501b37c | 2020-04-28 16:36:38 +0000 | [diff] [blame] | 916 | document().selection().setTypingStyle(m_typingStyle.copyRef()); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 917 | } |
| 918 | |
| 919 | void DeleteSelectionCommand::clearTransientState() |
| 920 | { |
eric@webkit.org | 87ea95c | 2009-02-09 21:43:24 +0000 | [diff] [blame] | 921 | m_selectionToDelete = VisibleSelection(); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 922 | m_upstreamStart.clear(); |
| 923 | m_downstreamStart.clear(); |
| 924 | m_upstreamEnd.clear(); |
| 925 | m_downstreamEnd.clear(); |
| 926 | m_endingPosition.clear(); |
| 927 | m_leadingWhitespace.clear(); |
| 928 | m_trailingWhitespace.clear(); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 929 | } |
jpu@apple.com | fd2014c | 2011-05-12 00:54:08 +0000 | [diff] [blame] | 930 | |
| 931 | String DeleteSelectionCommand::originalStringForAutocorrectionAtBeginningOfSelection() |
| 932 | { |
| 933 | if (!m_selectionToDelete.isRange()) |
| 934 | return String(); |
| 935 | |
| 936 | VisiblePosition startOfSelection = m_selectionToDelete.start(); |
| 937 | if (!isStartOfWord(startOfSelection)) |
| 938 | return String(); |
| 939 | |
darin@apple.com | 5d3decc | 2020-08-01 15:50:36 +0000 | [diff] [blame] | 940 | auto rangeOfFirstCharacter = makeSimpleRange(startOfSelection, startOfSelection.next()); |
| 941 | if (!rangeOfFirstCharacter) |
philn@webkit.org | 3d7a0f4 | 2011-05-12 14:36:29 +0000 | [diff] [blame] | 942 | return String(); |
| 943 | |
rniwa@webkit.org | 098af8c | 2021-04-25 05:59:59 +0000 | [diff] [blame] | 944 | ScriptDisallowedScope scriptDisallowedScope; |
darin@apple.com | 5d3decc | 2020-08-01 15:50:36 +0000 | [diff] [blame] | 945 | for (auto* marker : document().markers().markersInRange(*rangeOfFirstCharacter, DocumentMarker::Autocorrected)) { |
morrita@google.com | c07dd58 | 2011-05-27 07:56:25 +0000 | [diff] [blame] | 946 | int startOffset = marker->startOffset(); |
jpu@apple.com | fd2014c | 2011-05-12 00:54:08 +0000 | [diff] [blame] | 947 | if (startOffset == startOfSelection.deepEquivalent().offsetInContainerNode()) |
morrita@google.com | c07dd58 | 2011-05-27 07:56:25 +0000 | [diff] [blame] | 948 | return marker->description(); |
jpu@apple.com | fd2014c | 2011-05-12 00:54:08 +0000 | [diff] [blame] | 949 | } |
| 950 | return String(); |
| 951 | } |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 952 | |
enrica@apple.com | 7abf3f9 | 2011-12-01 01:16:13 +0000 | [diff] [blame] | 953 | // This method removes div elements with no attributes that have only one child or no children at all. |
| 954 | void DeleteSelectionCommand::removeRedundantBlocks() |
| 955 | { |
cdumez@apple.com | e1840be | 2021-09-20 21:45:40 +0000 | [diff] [blame] | 956 | RefPtr node { m_endingPosition.containerNode() }; |
Hironori.Fujii@sony.com | 9fa1f10 | 2021-04-20 20:21:16 +0000 | [diff] [blame] | 957 | if (!node) |
| 958 | return; |
cdumez@apple.com | e1840be | 2021-09-20 21:45:40 +0000 | [diff] [blame] | 959 | RefPtr rootNode { node->rootEditableElement() }; |
enrica@apple.com | 7abf3f9 | 2011-12-01 01:16:13 +0000 | [diff] [blame] | 960 | |
commit-queue@webkit.org | 2ed8d90 | 2021-04-17 00:57:21 +0000 | [diff] [blame] | 961 | while (node && node != rootNode) { |
| 962 | if (isRemovableBlock(node.get())) { |
enrica@apple.com | 7abf3f9 | 2011-12-01 01:16:13 +0000 | [diff] [blame] | 963 | if (node == m_endingPosition.anchorNode()) |
cdumez@apple.com | 2cbdf51 | 2014-11-11 00:02:57 +0000 | [diff] [blame] | 964 | updatePositionForNodeRemovalPreservingChildren(m_endingPosition, *node); |
enrica@apple.com | 7abf3f9 | 2011-12-01 01:16:13 +0000 | [diff] [blame] | 965 | |
cdumez@apple.com | 2fecce7 | 2017-05-05 05:09:56 +0000 | [diff] [blame] | 966 | CompositeEditCommand::removeNodePreservingChildren(*node); |
enrica@apple.com | 7abf3f9 | 2011-12-01 01:16:13 +0000 | [diff] [blame] | 967 | node = m_endingPosition.anchorNode(); |
| 968 | } else |
enrica@apple.com | 4853733 | 2011-12-15 00:32:27 +0000 | [diff] [blame] | 969 | node = node->parentNode(); |
enrica@apple.com | 7abf3f9 | 2011-12-01 01:16:13 +0000 | [diff] [blame] | 970 | } |
| 971 | } |
| 972 | |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 973 | void DeleteSelectionCommand::doApply() |
| 974 | { |
| 975 | // If selection has not been set to a custom selection when the command was created, |
| 976 | // use the current ending selection. |
| 977 | if (!m_hasSelectionToDelete) |
| 978 | m_selectionToDelete = endingSelection(); |
rniwa@webkit.org | d39a1e7 | 2010-08-25 19:19:26 +0000 | [diff] [blame] | 979 | |
| 980 | if (!m_selectionToDelete.isNonOrphanedRange()) |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 981 | return; |
| 982 | |
jpu@apple.com | fd2014c | 2011-05-12 00:54:08 +0000 | [diff] [blame] | 983 | String originalString = originalStringForAutocorrectionAtBeginningOfSelection(); |
| 984 | |
adele | b2f2b66 | 2006-07-15 17:22:50 +0000 | [diff] [blame] | 985 | // 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. |
| 986 | if (!m_replace) { |
cdumez@apple.com | e1840be | 2021-09-20 21:45:40 +0000 | [diff] [blame] | 987 | if (RefPtr textControl = enclosingTextFormControl(m_selectionToDelete.start()); textControl && textControl->focused()) |
wenson_hsieh@apple.com | 880f350 | 2022-05-19 03:50:47 +0000 | [diff] [blame] | 988 | document().editor().textWillBeDeletedInTextField(*textControl); |
adele | b2f2b66 | 2006-07-15 17:22:50 +0000 | [diff] [blame] | 989 | } |
adele | 174364d | 2006-04-13 01:50:04 +0000 | [diff] [blame] | 990 | |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 991 | // save this to later make the selection with |
darin@apple.com | c8d8b55 | 2020-09-03 21:38:50 +0000 | [diff] [blame] | 992 | auto affinity = m_selectionToDelete.affinity(); |
| 993 | |
justing | 11ec52e | 2006-06-07 00:09:37 +0000 | [diff] [blame] | 994 | Position downstreamEnd = m_selectionToDelete.end().downstream(); |
commit-queue@webkit.org | f362872 | 2010-11-28 02:54:43 +0000 | [diff] [blame] | 995 | m_needPlaceholder = isStartOfParagraph(m_selectionToDelete.visibleStart(), CanCrossEditingBoundary) |
| 996 | && isEndOfParagraph(m_selectionToDelete.visibleEnd(), CanCrossEditingBoundary) |
commit-queue@webkit.org | 1d836ce | 2010-10-29 21:47:01 +0000 | [diff] [blame] | 997 | && !lineBreakExistsAtVisiblePosition(m_selectionToDelete.visibleEnd()); |
justing | 552eeaf | 2007-04-20 01:53:22 +0000 | [diff] [blame] | 998 | if (m_needPlaceholder) { |
| 999 | // Don't need a placeholder when deleting a selection that starts just before a table |
| 1000 | // and ends inside it (we do need placeholders to hold open empty cells, but that's |
| 1001 | // handled elsewhere). |
cdumez@apple.com | e1840be | 2021-09-20 21:45:40 +0000 | [diff] [blame] | 1002 | if (RefPtr table = isLastPositionBeforeTable(m_selectionToDelete.visibleStart())) { |
cdumez@apple.com | ca12b50 | 2016-11-17 06:24:13 +0000 | [diff] [blame] | 1003 | if (m_selectionToDelete.end().deprecatedNode()->isDescendantOf(*table)) |
justing | 552eeaf | 2007-04-20 01:53:22 +0000 | [diff] [blame] | 1004 | m_needPlaceholder = false; |
cdumez@apple.com | ca12b50 | 2016-11-17 06:24:13 +0000 | [diff] [blame] | 1005 | } |
justing | 552eeaf | 2007-04-20 01:53:22 +0000 | [diff] [blame] | 1006 | } |
| 1007 | |
justing | 11ec52e | 2006-06-07 00:09:37 +0000 | [diff] [blame] | 1008 | |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 1009 | // set up our state |
wenson_hsieh@apple.com | 7cd854e | 2017-08-24 04:12:55 +0000 | [diff] [blame] | 1010 | if (!initializePositionData()) |
| 1011 | return; |
harrison | dff01cd | 2005-07-18 23:21:19 +0000 | [diff] [blame] | 1012 | |
hyatt | 0c05e10 | 2006-04-14 08:15:00 +0000 | [diff] [blame] | 1013 | // 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] | 1014 | deleteInsignificantTextDownstream(m_trailingWhitespace); |
| 1015 | |
| 1016 | saveTypingStyleState(); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 1017 | |
harrison | e4b84c5 | 2005-07-18 18:14:59 +0000 | [diff] [blame] | 1018 | // deleting just a BR is handled specially, at least because we do not |
| 1019 | // want to replace it with a placeholder BR! |
| 1020 | if (handleSpecialCaseBRDelete()) { |
justin.garcia@apple.com | d1eac9a | 2008-06-20 00:24:13 +0000 | [diff] [blame] | 1021 | calculateTypingStyleAfterDelete(); |
rniwa@webkit.org | eccfd3f | 2011-08-16 18:39:52 +0000 | [diff] [blame] | 1022 | setEndingSelection(VisibleSelection(m_endingPosition, affinity, endingSelection().isDirectional())); |
harrison | e4b84c5 | 2005-07-18 18:14:59 +0000 | [diff] [blame] | 1023 | clearTransientState(); |
| 1024 | rebalanceWhitespace(); |
| 1025 | return; |
| 1026 | } |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 1027 | |
harrison | e4b84c5 | 2005-07-18 18:14:59 +0000 | [diff] [blame] | 1028 | handleGeneralDelete(); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 1029 | |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 1030 | fixupWhitespace(); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 1031 | |
justing | f81641c | 2006-06-14 23:23:50 +0000 | [diff] [blame] | 1032 | mergeParagraphs(); |
| 1033 | |
justing | 187d292 | 2007-07-17 21:05:21 +0000 | [diff] [blame] | 1034 | removePreviouslySelectedEmptyTableRows(); |
| 1035 | |
darin@apple.com | d385be4 | 2016-05-14 20:09:50 +0000 | [diff] [blame] | 1036 | if (m_needPlaceholder) { |
enrica@apple.com | d14c2a8 | 2012-05-07 23:30:08 +0000 | [diff] [blame] | 1037 | if (m_sanitizeMarkup) |
| 1038 | removeRedundantBlocks(); |
shihchieh_lee@apple.com | 14ba3da | 2020-06-05 03:37:14 +0000 | [diff] [blame] | 1039 | |
| 1040 | // FIXME (Bug 212723): m_endingPosition becomes disconnected in moveParagraph() |
| 1041 | // because it is ancestor of the deleted element and gets pruned in removeNodeAndPruneAncestors(). |
| 1042 | // Either removeNodeAndPruneAncestors() should not remove ending position or we should find |
| 1043 | // a different ending position. |
| 1044 | if (!m_endingPosition.containerNode() || !m_endingPosition.containerNode()->isConnected()) |
| 1045 | return; |
darin@apple.com | d385be4 | 2016-05-14 20:09:50 +0000 | [diff] [blame] | 1046 | insertNodeAt(HTMLBRElement::create(document()), m_endingPosition); |
enrica@apple.com | 7abf3f9 | 2011-12-01 01:16:13 +0000 | [diff] [blame] | 1047 | } |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 1048 | |
enrica@apple.com | 2a79a80 | 2014-09-17 00:48:01 +0000 | [diff] [blame] | 1049 | bool shouldRebalaceWhiteSpace = true; |
shihchieh_lee@apple.com | 501b37c | 2020-04-28 16:36:38 +0000 | [diff] [blame] | 1050 | if (!document().editor().behavior().shouldRebalanceWhiteSpacesInSecureField()) { |
cdumez@apple.com | e1840be | 2021-09-20 21:45:40 +0000 | [diff] [blame] | 1051 | if (RefPtr endNode = m_endingPosition.deprecatedNode(); is<Text>(endNode)) { |
rniwa@webkit.org | 098af8c | 2021-04-25 05:59:59 +0000 | [diff] [blame] | 1052 | auto& textNode = downcast<Text>(*endNode); |
| 1053 | ScriptDisallowedScope scriptDisallowedScope; |
rniwa@webkit.org | 5c5bdba | 2016-07-21 19:41:47 +0000 | [diff] [blame] | 1054 | if (textNode.length() && textNode.renderer()) |
commit-queue@webkit.org | 1a4e667 | 2018-05-21 16:55:45 +0000 | [diff] [blame] | 1055 | shouldRebalaceWhiteSpace = textNode.renderer()->style().textSecurity() == TextSecurity::None; |
enrica@apple.com | 2a79a80 | 2014-09-17 00:48:01 +0000 | [diff] [blame] | 1056 | } |
dbates@webkit.org | f435ee1 | 2014-01-10 17:06:52 +0000 | [diff] [blame] | 1057 | } |
enrica@apple.com | 2a79a80 | 2014-09-17 00:48:01 +0000 | [diff] [blame] | 1058 | if (shouldRebalaceWhiteSpace) |
dbates@webkit.org | f435ee1 | 2014-01-10 17:06:52 +0000 | [diff] [blame] | 1059 | rebalanceWhitespaceAt(m_endingPosition); |
justing | 92cd9eb | 2006-12-06 21:01:30 +0000 | [diff] [blame] | 1060 | |
justin.garcia@apple.com | d1eac9a | 2008-06-20 00:24:13 +0000 | [diff] [blame] | 1061 | calculateTypingStyleAfterDelete(); |
jpu@apple.com | fd2014c | 2011-05-12 00:54:08 +0000 | [diff] [blame] | 1062 | |
akling@apple.com | 6f18f6d | 2013-09-01 14:57:23 +0000 | [diff] [blame] | 1063 | if (!originalString.isEmpty()) |
shihchieh_lee@apple.com | 501b37c | 2020-04-28 16:36:38 +0000 | [diff] [blame] | 1064 | document().editor().deletedAutocorrectionAtPosition(m_endingPosition, originalString); |
jpu@apple.com | fd2014c | 2011-05-12 00:54:08 +0000 | [diff] [blame] | 1065 | |
rniwa@webkit.org | eccfd3f | 2011-08-16 18:39:52 +0000 | [diff] [blame] | 1066 | setEndingSelection(VisibleSelection(m_endingPosition, affinity, endingSelection().isDirectional())); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 1067 | clearTransientState(); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 1068 | } |
| 1069 | |
justin.garcia@apple.com | d1eac9a | 2008-06-20 00:24:13 +0000 | [diff] [blame] | 1070 | // Normally deletion doesn't preserve the typing style that was present before it. For example, |
| 1071 | // type a character, Bold, then delete the character and start typing. The Bold typing style shouldn't |
| 1072 | // stick around. Deletion should preserve a typing style that *it* sets, however. |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 1073 | bool DeleteSelectionCommand::preservesTypingStyle() const |
| 1074 | { |
justin.garcia@apple.com | d1eac9a | 2008-06-20 00:24:13 +0000 | [diff] [blame] | 1075 | return m_typingStyle; |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 1076 | } |
| 1077 | |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 1078 | } // namespace WebCore |