mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2005 Apple Computer, Inc. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * 1. Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * 2. Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
| 12 | * |
| 13 | * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY |
| 14 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR |
| 17 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 18 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 20 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 21 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 23 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | */ |
| 25 | |
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@apple.com | 9a925fa | 2009-05-04 18:00:34 +0000 | [diff] [blame] | 29 | #include "CSSMutableStyleDeclaration.h" |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 30 | #include "Document.h" |
darin | 36d1136 | 2006-04-11 16:30:21 +0000 | [diff] [blame] | 31 | #include "DocumentFragment.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" |
eseidel | 40eb1b9 | 2006-03-25 22:20:36 +0000 | [diff] [blame] | 34 | #include "Element.h" |
mjs | 810e976 | 2006-01-09 21:39:14 +0000 | [diff] [blame] | 35 | #include "Frame.h" |
eseidel | 18f16d8 | 2006-02-28 00:19:49 +0000 | [diff] [blame] | 36 | #include "Logging.h" |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 37 | #include "CSSComputedStyleDeclaration.h" |
darin | bbe6466 | 2006-01-16 17:52:23 +0000 | [diff] [blame] | 38 | #include "htmlediting.h" |
adele | 174364d | 2006-04-13 01:50:04 +0000 | [diff] [blame] | 39 | #include "HTMLInputElement.h" |
darin | 98fa8b8 | 2006-03-20 08:03:57 +0000 | [diff] [blame] | 40 | #include "HTMLNames.h" |
justing | 217ded8 | 2006-04-04 20:52:10 +0000 | [diff] [blame] | 41 | #include "markup.h" |
hyatt@apple.com | d885df7 | 2009-01-22 02:31:52 +0000 | [diff] [blame] | 42 | #include "RenderTableCell.h" |
justing | 217ded8 | 2006-04-04 20:52:10 +0000 | [diff] [blame] | 43 | #include "ReplaceSelectionCommand.h" |
darin | 42563ac5 | 2007-01-22 17:28:57 +0000 | [diff] [blame] | 44 | #include "Text.h" |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 45 | #include "TextIterator.h" |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 46 | #include "visible_units.h" |
rniwa@webkit.org | 1de555e | 2009-08-07 21:36:24 +0000 | [diff] [blame] | 47 | #include "ApplyStyleCommand.h" |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 48 | |
darin | bbe6466 | 2006-01-16 17:52:23 +0000 | [diff] [blame] | 49 | namespace WebCore { |
darin | edbc5e4 | 2005-08-25 23:13:58 +0000 | [diff] [blame] | 50 | |
darin | bbe6466 | 2006-01-16 17:52:23 +0000 | [diff] [blame] | 51 | using namespace HTMLNames; |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 52 | |
justing | 94b6f9f | 2007-10-31 01:27:54 +0000 | [diff] [blame] | 53 | static bool isTableRow(const Node* node) |
justing | 187d292 | 2007-07-17 21:05:21 +0000 | [diff] [blame] | 54 | { |
| 55 | return node && node->hasTagName(trTag); |
| 56 | } |
| 57 | |
justing | 13e16aa | 2007-07-17 21:55:05 +0000 | [diff] [blame] | 58 | static bool isTableCellEmpty(Node* cell) |
justing | 187d292 | 2007-07-17 21:05:21 +0000 | [diff] [blame] | 59 | { |
| 60 | ASSERT(isTableCell(cell)); |
eric@webkit.org | f3a1881 | 2009-03-20 18:45:15 +0000 | [diff] [blame] | 61 | return VisiblePosition(firstDeepEditingPositionForNode(cell)) == VisiblePosition(lastDeepEditingPositionForNode(cell)); |
justing | 187d292 | 2007-07-17 21:05:21 +0000 | [diff] [blame] | 62 | } |
| 63 | |
justing | 13e16aa | 2007-07-17 21:55:05 +0000 | [diff] [blame] | 64 | static bool isTableRowEmpty(Node* row) |
justing | 187d292 | 2007-07-17 21:05:21 +0000 | [diff] [blame] | 65 | { |
| 66 | if (!isTableRow(row)) |
| 67 | return false; |
| 68 | |
| 69 | for (Node* child = row->firstChild(); child; child = child->nextSibling()) |
| 70 | if (isTableCell(child) && !isTableCellEmpty(child)) |
| 71 | return false; |
| 72 | |
| 73 | return true; |
| 74 | } |
| 75 | |
justing | 113aaf3 | 2007-01-25 01:00:36 +0000 | [diff] [blame] | 76 | DeleteSelectionCommand::DeleteSelectionCommand(Document *document, bool smartDelete, bool mergeBlocksAfterDelete, bool replace, bool expandForSpecialElements) |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 77 | : CompositeEditCommand(document), |
| 78 | m_hasSelectionToDelete(false), |
| 79 | m_smartDelete(smartDelete), |
| 80 | m_mergeBlocksAfterDelete(mergeBlocksAfterDelete), |
adele | b2f2b66 | 2006-07-15 17:22:50 +0000 | [diff] [blame] | 81 | m_replace(replace), |
justing | 113aaf3 | 2007-01-25 01:00:36 +0000 | [diff] [blame] | 82 | m_expandForSpecialElements(expandForSpecialElements), |
justin.garcia@apple.com | 7e95c7b | 2008-11-12 20:08:06 +0000 | [diff] [blame] | 83 | m_pruneStartBlockIfNecessary(false), |
mitz@apple.com | fc292c3 | 2009-05-03 04:09:41 +0000 | [diff] [blame] | 84 | m_startsAtEmptyLine(false), |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 85 | m_startBlock(0), |
| 86 | m_endBlock(0), |
justing | 503ccf1 | 2005-07-29 22:50:47 +0000 | [diff] [blame] | 87 | m_typingStyle(0), |
| 88 | m_deleteIntoBlockquoteStyle(0) |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 89 | { |
| 90 | } |
| 91 | |
eric@webkit.org | 87ea95c | 2009-02-09 21:43:24 +0000 | [diff] [blame] | 92 | DeleteSelectionCommand::DeleteSelectionCommand(const VisibleSelection& selection, bool smartDelete, bool mergeBlocksAfterDelete, bool replace, bool expandForSpecialElements) |
darin | 3538406 | 2006-08-24 22:07:45 +0000 | [diff] [blame] | 93 | : CompositeEditCommand(selection.start().node()->document()), |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 94 | m_hasSelectionToDelete(true), |
| 95 | m_smartDelete(smartDelete), |
| 96 | m_mergeBlocksAfterDelete(mergeBlocksAfterDelete), |
adele | b2f2b66 | 2006-07-15 17:22:50 +0000 | [diff] [blame] | 97 | m_replace(replace), |
justing | 113aaf3 | 2007-01-25 01:00:36 +0000 | [diff] [blame] | 98 | m_expandForSpecialElements(expandForSpecialElements), |
justin.garcia@apple.com | 7e95c7b | 2008-11-12 20:08:06 +0000 | [diff] [blame] | 99 | m_pruneStartBlockIfNecessary(false), |
mitz@apple.com | fc292c3 | 2009-05-03 04:09:41 +0000 | [diff] [blame] | 100 | m_startsAtEmptyLine(false), |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 101 | m_selectionToDelete(selection), |
| 102 | m_startBlock(0), |
| 103 | m_endBlock(0), |
justing | 503ccf1 | 2005-07-29 22:50:47 +0000 | [diff] [blame] | 104 | m_typingStyle(0), |
| 105 | m_deleteIntoBlockquoteStyle(0) |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 106 | { |
| 107 | } |
| 108 | |
justing | 113aaf3 | 2007-01-25 01:00:36 +0000 | [diff] [blame] | 109 | void DeleteSelectionCommand::initializeStartEnd(Position& start, Position& end) |
darin | 3538406 | 2006-08-24 22:07:45 +0000 | [diff] [blame] | 110 | { |
darin | 2b0ff52 | 2006-05-03 19:32:21 +0000 | [diff] [blame] | 111 | Node* startSpecialContainer = 0; |
| 112 | Node* endSpecialContainer = 0; |
harrison | dec5e09 | 2006-01-25 21:05:02 +0000 | [diff] [blame] | 113 | |
justing | 113aaf3 | 2007-01-25 01:00:36 +0000 | [diff] [blame] | 114 | start = m_selectionToDelete.start(); |
| 115 | end = m_selectionToDelete.end(); |
justing | e6f80c1 | 2006-06-09 04:57:51 +0000 | [diff] [blame] | 116 | |
justing | 2112d43 | 2006-11-28 21:25:28 +0000 | [diff] [blame] | 117 | // 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] | 118 | // but in these cases, we want to delete it, so manually expand the selection |
| 119 | if (start.node()->hasTagName(hrTag)) |
| 120 | start = Position(start.node(), 0); |
| 121 | else if (end.node()->hasTagName(hrTag)) |
| 122 | end = Position(end.node(), 1); |
| 123 | |
dbates@webkit.org | ef42d38 | 2010-01-25 19:20:06 +0000 | [diff] [blame] | 124 | // 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] | 125 | if (!m_expandForSpecialElements) |
| 126 | return; |
| 127 | |
justin.garcia@apple.com | 6412551 | 2008-02-20 17:48:44 +0000 | [diff] [blame] | 128 | while (1) { |
justing | e6f80c1 | 2006-06-09 04:57:51 +0000 | [diff] [blame] | 129 | startSpecialContainer = 0; |
| 130 | endSpecialContainer = 0; |
| 131 | |
justing | 47d836d | 2007-06-08 00:02:06 +0000 | [diff] [blame] | 132 | Position s = positionBeforeContainingSpecialElement(start, &startSpecialContainer); |
| 133 | Position e = positionAfterContainingSpecialElement(end, &endSpecialContainer); |
justing | e6f80c1 | 2006-06-09 04:57:51 +0000 | [diff] [blame] | 134 | |
justing | 47d836d | 2007-06-08 00:02:06 +0000 | [diff] [blame] | 135 | if (!startSpecialContainer && !endSpecialContainer) |
justing | e6f80c1 | 2006-06-09 04:57:51 +0000 | [diff] [blame] | 136 | break; |
justin.garcia@apple.com | 6412551 | 2008-02-20 17:48:44 +0000 | [diff] [blame] | 137 | |
| 138 | if (VisiblePosition(start) != m_selectionToDelete.visibleStart() || VisiblePosition(end) != m_selectionToDelete.visibleEnd()) |
| 139 | break; |
eric@webkit.org | 05d6e93 | 2009-06-02 21:31:08 +0000 | [diff] [blame] | 140 | |
eric@webkit.org | f74ae5c | 2009-09-09 23:25:47 +0000 | [diff] [blame] | 141 | // If we're going to expand to include the startSpecialContainer, it must be fully selected. |
| 142 | if (startSpecialContainer && !endSpecialContainer && comparePositions(positionInParentAfterNode(startSpecialContainer), end) > -1) |
justing | 47d836d | 2007-06-08 00:02:06 +0000 | [diff] [blame] | 143 | break; |
| 144 | |
eric@webkit.org | 05d6e93 | 2009-06-02 21:31:08 +0000 | [diff] [blame] | 145 | // If we're going to expand to include the endSpecialContainer, it must be fully selected. |
eric@webkit.org | f74ae5c | 2009-09-09 23:25:47 +0000 | [diff] [blame] | 146 | if (endSpecialContainer && !startSpecialContainer && comparePositions(start, positionInParentBeforeNode(endSpecialContainer)) > -1) |
justing | 47d836d | 2007-06-08 00:02:06 +0000 | [diff] [blame] | 147 | break; |
eric@webkit.org | f74ae5c | 2009-09-09 23:25:47 +0000 | [diff] [blame] | 148 | |
justing | 47d836d | 2007-06-08 00:02:06 +0000 | [diff] [blame] | 149 | if (startSpecialContainer && startSpecialContainer->isDescendantOf(endSpecialContainer)) |
justing | 2bdb03e | 2006-11-30 02:54:19 +0000 | [diff] [blame] | 150 | // Don't adjust the end yet, it is the end of a special element that contains the start |
| 151 | // special element (which may or may not be fully selected). |
| 152 | start = s; |
justing | 47d836d | 2007-06-08 00:02:06 +0000 | [diff] [blame] | 153 | else if (endSpecialContainer && endSpecialContainer->isDescendantOf(startSpecialContainer)) |
justing | 2bdb03e | 2006-11-30 02:54:19 +0000 | [diff] [blame] | 154 | // Don't adjust the start yet, it is the start of a special element that contains the end |
| 155 | // special element (which may or may not be fully selected). |
| 156 | end = e; |
| 157 | else { |
| 158 | start = s; |
| 159 | end = e; |
| 160 | } |
harrison | dec5e09 | 2006-01-25 21:05:02 +0000 | [diff] [blame] | 161 | } |
harrison | dec5e09 | 2006-01-25 21:05:02 +0000 | [diff] [blame] | 162 | } |
| 163 | |
ojan@chromium.org | 078aa80 | 2010-03-10 00:17:11 +0000 | [diff] [blame] | 164 | void DeleteSelectionCommand::setStartingSelectionOnSmartDelete(const Position& start, const Position& end) |
ojan@chromium.org | 77dae9d | 2010-03-09 23:41:38 +0000 | [diff] [blame] | 165 | { |
| 166 | VisiblePosition newBase; |
| 167 | VisiblePosition newExtent; |
| 168 | if (startingSelection().isBaseFirst()) { |
| 169 | newBase = start; |
| 170 | newExtent = end; |
| 171 | } else { |
| 172 | newBase = end; |
| 173 | newExtent = start; |
| 174 | } |
| 175 | setStartingSelection(VisibleSelection(newBase, newExtent)); |
| 176 | } |
| 177 | |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 178 | void DeleteSelectionCommand::initializePositionData() |
| 179 | { |
justing | 113aaf3 | 2007-01-25 01:00:36 +0000 | [diff] [blame] | 180 | Position start, end; |
| 181 | initializeStartEnd(start, end); |
| 182 | |
| 183 | m_upstreamStart = start.upstream(); |
| 184 | m_downstreamStart = start.downstream(); |
| 185 | m_upstreamEnd = end.upstream(); |
| 186 | m_downstreamEnd = end.downstream(); |
harrison | dec5e09 | 2006-01-25 21:05:02 +0000 | [diff] [blame] | 187 | |
justing | 06a653c | 2007-04-04 20:49:52 +0000 | [diff] [blame] | 188 | m_startRoot = editableRootForPosition(start); |
| 189 | m_endRoot = editableRootForPosition(end); |
| 190 | |
justin.garcia@apple.com | deed90d | 2007-12-13 21:32:12 +0000 | [diff] [blame] | 191 | m_startTableRow = enclosingNodeOfType(start, &isTableRow); |
| 192 | m_endTableRow = enclosingNodeOfType(end, &isTableRow); |
justing | 187d292 | 2007-07-17 21:05:21 +0000 | [diff] [blame] | 193 | |
justing | b770150 | 2007-03-29 22:34:28 +0000 | [diff] [blame] | 194 | // Don't move content out of a table cell. |
justin.garcia@apple.com | ab87ef8 | 2008-05-29 20:37:53 +0000 | [diff] [blame] | 195 | // If the cell is non-editable, enclosingNodeOfType won't return it by default, so |
| 196 | // tell that function that we don't care if it returns non-editable nodes. |
| 197 | Node* startCell = enclosingNodeOfType(m_upstreamStart, &isTableCell, false); |
| 198 | Node* endCell = enclosingNodeOfType(m_downstreamEnd, &isTableCell, false); |
justing | 8f16cee | 2006-06-22 00:20:29 +0000 | [diff] [blame] | 199 | // 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] | 200 | if (endCell && endCell != startCell) |
justing | 8f16cee | 2006-06-22 00:20:29 +0000 | [diff] [blame] | 201 | m_mergeBlocksAfterDelete = false; |
| 202 | |
justing | 11ec52e | 2006-06-07 00:09:37 +0000 | [diff] [blame] | 203 | // 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] | 204 | // Sometimes they aren't (like when no merge is requested), so we must choose one position to hold the caret |
| 205 | // and receive the placeholder after deletion. |
justing | 11ec52e | 2006-06-07 00:09:37 +0000 | [diff] [blame] | 206 | VisiblePosition visibleEnd(m_downstreamEnd); |
| 207 | if (m_mergeBlocksAfterDelete && !isEndOfParagraph(visibleEnd)) |
| 208 | m_endingPosition = m_downstreamEnd; |
| 209 | else |
| 210 | m_endingPosition = m_downstreamStart; |
justin.garcia@apple.com | 7e95c7b | 2008-11-12 20:08:06 +0000 | [diff] [blame] | 211 | |
| 212 | // We don't want to merge into a block if it will mean changing the quote level of content after deleting |
| 213 | // selections that contain a whole number paragraphs plus a line break, since it is unclear to most users |
| 214 | // that such a selection actually ends at the start of the next paragraph. This matches TextEdit behavior |
| 215 | // for indented paragraphs. |
adele@apple.com | e2bc16b | 2009-04-28 22:17:29 +0000 | [diff] [blame] | 216 | // Only apply this rule if the endingSelection is a range selection. If it is a caret, then other operations have created |
| 217 | // 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. |
| 218 | if (numEnclosingMailBlockquotes(start) != numEnclosingMailBlockquotes(end) |
| 219 | && isStartOfParagraph(visibleEnd) && isStartOfParagraph(VisiblePosition(start)) |
| 220 | && endingSelection().isRange()) { |
justin.garcia@apple.com | 7e95c7b | 2008-11-12 20:08:06 +0000 | [diff] [blame] | 221 | m_mergeBlocksAfterDelete = false; |
| 222 | m_pruneStartBlockIfNecessary = true; |
| 223 | } |
justing | 8f16cee | 2006-06-22 00:20:29 +0000 | [diff] [blame] | 224 | |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 225 | // 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] | 226 | m_leadingWhitespace = m_upstreamStart.leadingWhitespacePosition(m_selectionToDelete.affinity()); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 227 | m_trailingWhitespace = m_downstreamEnd.trailingWhitespacePosition(VP_DEFAULT_AFFINITY); |
| 228 | |
| 229 | if (m_smartDelete) { |
| 230 | |
| 231 | // 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] | 232 | Position pos = VisiblePosition(m_upstreamStart, m_selectionToDelete.affinity()).deepEquivalent(); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 233 | bool skipSmartDelete = pos.trailingWhitespacePosition(VP_DEFAULT_AFFINITY, true).isNotNull(); |
| 234 | if (!skipSmartDelete) |
| 235 | skipSmartDelete = m_downstreamEnd.leadingWhitespacePosition(VP_DEFAULT_AFFINITY, true).isNotNull(); |
| 236 | |
| 237 | // extend selection upstream if there is whitespace there |
darin | 9aa45a4 | 2006-01-15 23:32:02 +0000 | [diff] [blame] | 238 | bool hasLeadingWhitespaceBeforeAdjustment = m_upstreamStart.leadingWhitespacePosition(m_selectionToDelete.affinity(), true).isNotNull(); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 239 | if (!skipSmartDelete && hasLeadingWhitespaceBeforeAdjustment) { |
harrison | dec5e09 | 2006-01-25 21:05:02 +0000 | [diff] [blame] | 240 | VisiblePosition visiblePos = VisiblePosition(m_upstreamStart, VP_DEFAULT_AFFINITY).previous(); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 241 | pos = visiblePos.deepEquivalent(); |
| 242 | // Expand out one character upstream for smart delete and recalculate |
| 243 | // positions based on this change. |
| 244 | m_upstreamStart = pos.upstream(); |
| 245 | m_downstreamStart = pos.downstream(); |
| 246 | m_leadingWhitespace = m_upstreamStart.leadingWhitespacePosition(visiblePos.affinity()); |
ojan@chromium.org | 77dae9d | 2010-03-09 23:41:38 +0000 | [diff] [blame] | 247 | |
| 248 | setStartingSelectionOnSmartDelete(m_upstreamStart, m_upstreamEnd); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | // trailing whitespace is only considered for smart delete if there is no leading |
| 252 | // whitespace, as in the case where you double-click the first word of a paragraph. |
| 253 | if (!skipSmartDelete && !hasLeadingWhitespaceBeforeAdjustment && m_downstreamEnd.trailingWhitespacePosition(VP_DEFAULT_AFFINITY, true).isNotNull()) { |
| 254 | // Expand out one character downstream for smart delete and recalculate |
| 255 | // positions based on this change. |
harrison | dec5e09 | 2006-01-25 21:05:02 +0000 | [diff] [blame] | 256 | pos = VisiblePosition(m_downstreamEnd, VP_DEFAULT_AFFINITY).next().deepEquivalent(); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 257 | m_upstreamEnd = pos.upstream(); |
| 258 | m_downstreamEnd = pos.downstream(); |
| 259 | m_trailingWhitespace = m_downstreamEnd.trailingWhitespacePosition(VP_DEFAULT_AFFINITY); |
ojan@chromium.org | 77dae9d | 2010-03-09 23:41:38 +0000 | [diff] [blame] | 260 | |
| 261 | setStartingSelectionOnSmartDelete(m_downstreamStart, m_downstreamEnd); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 262 | } |
| 263 | } |
| 264 | |
justin.garcia@apple.com | e1da273 | 2008-06-25 21:22:12 +0000 | [diff] [blame] | 265 | // We must pass the positions through rangeCompliantEquivalent, since some editing positions |
| 266 | // that appear inside their nodes aren't really inside them. [hr, 0] is one example. |
| 267 | // FIXME: rangeComplaintEquivalent should eventually be moved into enclosing element getters |
| 268 | // like the one below, since editing functions should obviously accept editing positions. |
| 269 | // FIXME: Passing false to enclosingNodeOfType tells it that it's OK to return a non-editable |
| 270 | // node. This was done to match existing behavior, but it seems wrong. |
| 271 | m_startBlock = enclosingNodeOfType(rangeCompliantEquivalent(m_downstreamStart), &isBlock, false); |
| 272 | m_endBlock = enclosingNodeOfType(rangeCompliantEquivalent(m_upstreamEnd), &isBlock, false); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 273 | } |
| 274 | |
justin.garcia@apple.com | e248230 | 2008-11-22 01:50:42 +0000 | [diff] [blame] | 275 | static void removeEnclosingAnchorStyle(CSSMutableStyleDeclaration* style, const Position& position) |
| 276 | { |
| 277 | Node* enclosingAnchor = enclosingAnchorElement(position); |
| 278 | if (!enclosingAnchor || !enclosingAnchor->parentNode()) |
| 279 | return; |
rniwa@webkit.org | 1de555e | 2009-08-07 21:36:24 +0000 | [diff] [blame] | 280 | |
| 281 | removeStylesAddedByNode(style, enclosingAnchor); |
justin.garcia@apple.com | e248230 | 2008-11-22 01:50:42 +0000 | [diff] [blame] | 282 | } |
| 283 | |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 284 | void DeleteSelectionCommand::saveTypingStyleState() |
| 285 | { |
justin.garcia@apple.com | 6412551 | 2008-02-20 17:48:44 +0000 | [diff] [blame] | 286 | // A common case is deleting characters that are all from the same text node. In |
| 287 | // that case, the style at the start of the selection before deletion will be the |
| 288 | // same as the style at the start of the selection after deletion (since those |
| 289 | // two positions will be identical). Therefore there is no need to save the |
| 290 | // typing style at the start of the selection, nor is there a reason to |
| 291 | // compute the style at the start of the selection after deletion (see the |
| 292 | // early return in calculateTypingStyleAfterDelete). |
| 293 | if (m_upstreamStart.node() == m_downstreamEnd.node() && m_upstreamStart.node()->isTextNode()) |
| 294 | return; |
rniwa@webkit.org | 1de555e | 2009-08-07 21:36:24 +0000 | [diff] [blame] | 295 | |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 296 | // Figure out the typing style in effect before the delete is done. |
rniwa@webkit.org | 7e06f4a | 2010-11-06 21:19:59 +0000 | [diff] [blame^] | 297 | m_typingStyle = EditingStyle::create(positionBeforeTabSpan(m_selectionToDelete.start())); |
rniwa@webkit.org | 1de555e | 2009-08-07 21:36:24 +0000 | [diff] [blame] | 298 | |
rniwa@webkit.org | 7e06f4a | 2010-11-06 21:19:59 +0000 | [diff] [blame^] | 299 | removeEnclosingAnchorStyle(m_typingStyle->style(), m_selectionToDelete.start()); |
rniwa@webkit.org | 1de555e | 2009-08-07 21:36:24 +0000 | [diff] [blame] | 300 | |
justing | 503ccf1 | 2005-07-29 22:50:47 +0000 | [diff] [blame] | 301 | // If we're deleting into a Mail blockquote, save the style at end() instead of start() |
| 302 | // We'll use this later in computeTypingStyleAfterDelete if we end up outside of a Mail blockquote |
rniwa@webkit.org | 1de555e | 2009-08-07 21:36:24 +0000 | [diff] [blame] | 303 | if (nearestMailBlockquote(m_selectionToDelete.start().node())) |
rniwa@webkit.org | 7e06f4a | 2010-11-06 21:19:59 +0000 | [diff] [blame^] | 304 | m_deleteIntoBlockquoteStyle = EditingStyle::create(m_selectionToDelete.end()); |
rniwa@webkit.org | 1de555e | 2009-08-07 21:36:24 +0000 | [diff] [blame] | 305 | else |
justing | 503ccf1 | 2005-07-29 22:50:47 +0000 | [diff] [blame] | 306 | m_deleteIntoBlockquoteStyle = 0; |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | bool DeleteSelectionCommand::handleSpecialCaseBRDelete() |
| 310 | { |
| 311 | // Check for special-case where the selection contains only a BR on a line by itself after another BR. |
justing | 11ec52e | 2006-06-07 00:09:37 +0000 | [diff] [blame] | 312 | bool upstreamStartIsBR = m_upstreamStart.node()->hasTagName(brTag); |
mjs | 76582fb | 2005-07-30 02:33:26 +0000 | [diff] [blame] | 313 | bool downstreamStartIsBR = m_downstreamStart.node()->hasTagName(brTag); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 314 | bool isBROnLineByItself = upstreamStartIsBR && downstreamStartIsBR && m_downstreamStart.node() == m_upstreamEnd.node(); |
| 315 | if (isBROnLineByItself) { |
| 316 | removeNode(m_downstreamStart.node()); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 317 | return true; |
| 318 | } |
| 319 | |
| 320 | // Not a special-case delete per se, but we can detect that the merging of content between blocks |
| 321 | // should not be done. |
harrison | 581c07b | 2006-11-06 18:08:52 +0000 | [diff] [blame] | 322 | if (upstreamStartIsBR && downstreamStartIsBR) { |
mitz@apple.com | fc292c3 | 2009-05-03 04:09:41 +0000 | [diff] [blame] | 323 | m_startsAtEmptyLine = true; |
harrison | 581c07b | 2006-11-06 18:08:52 +0000 | [diff] [blame] | 324 | m_endingPosition = m_downstreamEnd; |
| 325 | } |
| 326 | |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 327 | return false; |
| 328 | } |
| 329 | |
justing | f81641c | 2006-06-14 23:23:50 +0000 | [diff] [blame] | 330 | static void updatePositionForNodeRemoval(Node* node, Position& position) |
| 331 | { |
| 332 | if (position.isNull()) |
| 333 | return; |
eric@webkit.org | 4e32ebc | 2009-04-30 01:09:57 +0000 | [diff] [blame] | 334 | if (node->parent() == position.node() && node->nodeIndex() < (unsigned)position.deprecatedEditingOffset()) |
| 335 | position = Position(position.node(), position.deprecatedEditingOffset() - 1); |
justing | 5b9c5a0 | 2006-09-23 00:14:55 +0000 | [diff] [blame] | 336 | if (position.node() == node || position.node()->isDescendantOf(node)) |
eric@webkit.org | f74ae5c | 2009-09-09 23:25:47 +0000 | [diff] [blame] | 337 | position = positionInParentBeforeNode(node); |
justing | f81641c | 2006-06-14 23:23:50 +0000 | [diff] [blame] | 338 | } |
| 339 | |
darin@apple.com | e95b53d | 2008-12-23 21:42:46 +0000 | [diff] [blame] | 340 | void DeleteSelectionCommand::removeNode(PassRefPtr<Node> node) |
justing | 11ec52e | 2006-06-07 00:09:37 +0000 | [diff] [blame] | 341 | { |
justing | 06a653c | 2007-04-04 20:49:52 +0000 | [diff] [blame] | 342 | if (!node) |
| 343 | return; |
| 344 | |
| 345 | if (m_startRoot != m_endRoot && !(node->isDescendantOf(m_startRoot.get()) && node->isDescendantOf(m_endRoot.get()))) { |
| 346 | // If a node is not in both the start and end editable roots, remove it only if its inside an editable region. |
| 347 | if (!node->parentNode()->isContentEditable()) { |
| 348 | // Don't remove non-editable atomic nodes. |
| 349 | if (!node->firstChild()) |
| 350 | return; |
| 351 | // Search this non-editable region for editable regions to empty. |
| 352 | RefPtr<Node> child = node->firstChild(); |
| 353 | while (child) { |
| 354 | RefPtr<Node> nextChild = child->nextSibling(); |
| 355 | removeNode(child.get()); |
| 356 | // Bail if nextChild is no longer node's child. |
| 357 | if (nextChild && nextChild->parentNode() != node) |
| 358 | return; |
| 359 | child = nextChild; |
| 360 | } |
| 361 | |
| 362 | // Don't remove editable regions that are inside non-editable ones, just clear them. |
| 363 | return; |
| 364 | } |
| 365 | } |
| 366 | |
darin@apple.com | e95b53d | 2008-12-23 21:42:46 +0000 | [diff] [blame] | 367 | if (isTableStructureNode(node.get()) || node == node->rootEditableElement()) { |
justing | 11ec52e | 2006-06-07 00:09:37 +0000 | [diff] [blame] | 368 | // Do not remove an element of table structure; remove its contents. |
| 369 | // Likewise for the root editable element. |
darin@apple.com | e95b53d | 2008-12-23 21:42:46 +0000 | [diff] [blame] | 370 | Node* child = node->firstChild(); |
justing | 11ec52e | 2006-06-07 00:09:37 +0000 | [diff] [blame] | 371 | while (child) { |
darin@apple.com | e95b53d | 2008-12-23 21:42:46 +0000 | [diff] [blame] | 372 | Node* remove = child; |
justing | 11ec52e | 2006-06-07 00:09:37 +0000 | [diff] [blame] | 373 | child = child->nextSibling(); |
justing | f81641c | 2006-06-14 23:23:50 +0000 | [diff] [blame] | 374 | removeNode(remove); |
justing | 11ec52e | 2006-06-07 00:09:37 +0000 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | // make sure empty cell has some height |
| 378 | updateLayout(); |
| 379 | RenderObject *r = node->renderer(); |
darin@apple.com | 445c34f | 2009-08-01 00:40:58 +0000 | [diff] [blame] | 380 | if (r && r->isTableCell() && toRenderTableCell(r)->contentHeight() <= 0) |
ddkilzer@apple.com | 9c4dacc | 2009-07-11 05:36:29 +0000 | [diff] [blame] | 381 | insertBlockPlaceholder(Position(node, 0)); |
justing | 11ec52e | 2006-06-07 00:09:37 +0000 | [diff] [blame] | 382 | return; |
| 383 | } |
| 384 | |
eric@webkit.org | f3a1881 | 2009-03-20 18:45:15 +0000 | [diff] [blame] | 385 | if (node == m_startBlock && !isEndOfBlock(VisiblePosition(firstDeepEditingPositionForNode(m_startBlock.get())).previous())) |
lweintraub | 635ec2a | 2006-07-13 18:28:28 +0000 | [diff] [blame] | 386 | m_needPlaceholder = true; |
eric@webkit.org | f3a1881 | 2009-03-20 18:45:15 +0000 | [diff] [blame] | 387 | else if (node == m_endBlock && !isStartOfBlock(VisiblePosition(lastDeepEditingPositionForNode(m_startBlock.get())).next())) |
lweintraub | 635ec2a | 2006-07-13 18:28:28 +0000 | [diff] [blame] | 388 | m_needPlaceholder = true; |
| 389 | |
justing | f81641c | 2006-06-14 23:23:50 +0000 | [diff] [blame] | 390 | // FIXME: Update the endpoints of the range being deleted. |
darin@apple.com | e95b53d | 2008-12-23 21:42:46 +0000 | [diff] [blame] | 391 | updatePositionForNodeRemoval(node.get(), m_endingPosition); |
| 392 | updatePositionForNodeRemoval(node.get(), m_leadingWhitespace); |
| 393 | updatePositionForNodeRemoval(node.get(), m_trailingWhitespace); |
justing | 11ec52e | 2006-06-07 00:09:37 +0000 | [diff] [blame] | 394 | |
justing | f81641c | 2006-06-14 23:23:50 +0000 | [diff] [blame] | 395 | CompositeEditCommand::removeNode(node); |
| 396 | } |
| 397 | |
andersca@apple.com | d8dda09 | 2009-01-23 19:52:19 +0000 | [diff] [blame] | 398 | static void updatePositionForTextRemoval(Node* node, int offset, int count, Position& position) |
justing | f81641c | 2006-06-14 23:23:50 +0000 | [diff] [blame] | 399 | { |
| 400 | if (position.node() == node) { |
eric@webkit.org | 4e32ebc | 2009-04-30 01:09:57 +0000 | [diff] [blame] | 401 | if (position.deprecatedEditingOffset() > offset + count) |
| 402 | position = Position(position.node(), position.deprecatedEditingOffset() - count); |
| 403 | else if (position.deprecatedEditingOffset() > offset) |
justing | f81641c | 2006-06-14 23:23:50 +0000 | [diff] [blame] | 404 | position = Position(position.node(), offset); |
| 405 | } |
justing | 11ec52e | 2006-06-07 00:09:37 +0000 | [diff] [blame] | 406 | } |
| 407 | |
darin@apple.com | e95b53d | 2008-12-23 21:42:46 +0000 | [diff] [blame] | 408 | void DeleteSelectionCommand::deleteTextFromNode(PassRefPtr<Text> node, unsigned offset, unsigned count) |
justing | 11ec52e | 2006-06-07 00:09:37 +0000 | [diff] [blame] | 409 | { |
justing | f81641c | 2006-06-14 23:23:50 +0000 | [diff] [blame] | 410 | // FIXME: Update the endpoints of the range being deleted. |
darin@apple.com | e95b53d | 2008-12-23 21:42:46 +0000 | [diff] [blame] | 411 | updatePositionForTextRemoval(node.get(), offset, count, m_endingPosition); |
| 412 | updatePositionForTextRemoval(node.get(), offset, count, m_leadingWhitespace); |
| 413 | updatePositionForTextRemoval(node.get(), offset, count, m_trailingWhitespace); |
justin.garcia@apple.com | 68510a5 | 2009-04-23 00:54:42 +0000 | [diff] [blame] | 414 | updatePositionForTextRemoval(node.get(), offset, count, m_downstreamEnd); |
justing | 11ec52e | 2006-06-07 00:09:37 +0000 | [diff] [blame] | 415 | |
| 416 | CompositeEditCommand::deleteTextFromNode(node, offset, count); |
| 417 | } |
| 418 | |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 419 | void DeleteSelectionCommand::handleGeneralDelete() |
| 420 | { |
eric@webkit.org | 4e32ebc | 2009-04-30 01:09:57 +0000 | [diff] [blame] | 421 | int startOffset = m_upstreamStart.deprecatedEditingOffset(); |
justing | 11ec52e | 2006-06-07 00:09:37 +0000 | [diff] [blame] | 422 | Node* startNode = m_upstreamStart.node(); |
lweintraub | 635ec2a | 2006-07-13 18:28:28 +0000 | [diff] [blame] | 423 | |
| 424 | // Never remove the start block unless it's a table, in which case we won't merge content in. |
| 425 | if (startNode == m_startBlock && startOffset == 0 && canHaveChildrenForEditing(startNode) && !startNode->hasTagName(tableTag)) { |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 426 | startOffset = 0; |
justing | 11ec52e | 2006-06-07 00:09:37 +0000 | [diff] [blame] | 427 | startNode = startNode->traverseNextNode(); |
justing | e6f80c1 | 2006-06-09 04:57:51 +0000 | [diff] [blame] | 428 | } |
| 429 | |
ap@webkit.org | ff51b52 | 2007-11-11 20:28:51 +0000 | [diff] [blame] | 430 | if (startOffset >= caretMaxOffset(startNode) && startNode->isTextNode()) { |
justing | e6f80c1 | 2006-06-09 04:57:51 +0000 | [diff] [blame] | 431 | Text *text = static_cast<Text *>(startNode); |
ap@webkit.org | ff51b52 | 2007-11-11 20:28:51 +0000 | [diff] [blame] | 432 | if (text->length() > (unsigned)caretMaxOffset(startNode)) |
| 433 | deleteTextFromNode(text, caretMaxOffset(startNode), text->length() - caretMaxOffset(startNode)); |
justing | e6f80c1 | 2006-06-09 04:57:51 +0000 | [diff] [blame] | 434 | } |
| 435 | |
eric@webkit.org | f3a1881 | 2009-03-20 18:45:15 +0000 | [diff] [blame] | 436 | if (startOffset >= lastOffsetForEditing(startNode)) { |
justing | e6f80c1 | 2006-06-09 04:57:51 +0000 | [diff] [blame] | 437 | startNode = startNode->traverseNextSibling(); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 438 | startOffset = 0; |
| 439 | } |
| 440 | |
| 441 | // Done adjusting the start. See if we're all done. |
justing | 11ec52e | 2006-06-07 00:09:37 +0000 | [diff] [blame] | 442 | if (!startNode) |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 443 | return; |
| 444 | |
justing | 11ec52e | 2006-06-07 00:09:37 +0000 | [diff] [blame] | 445 | if (startNode == m_downstreamEnd.node()) { |
commit-queue@webkit.org | 2aa8d03 | 2010-09-22 20:43:26 +0000 | [diff] [blame] | 446 | if (m_downstreamEnd.deprecatedEditingOffset() - startOffset > 0) { |
justing | 11ec52e | 2006-06-07 00:09:37 +0000 | [diff] [blame] | 447 | if (startNode->isTextNode()) { |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 448 | // in a text node that needs to be trimmed |
eric@webkit.org | f3a1881 | 2009-03-20 18:45:15 +0000 | [diff] [blame] | 449 | Text* text = static_cast<Text*>(startNode); |
eric@webkit.org | 4e32ebc | 2009-04-30 01:09:57 +0000 | [diff] [blame] | 450 | deleteTextFromNode(text, startOffset, m_downstreamEnd.deprecatedEditingOffset() - startOffset); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 451 | } else { |
eric@webkit.org | 4e32ebc | 2009-04-30 01:09:57 +0000 | [diff] [blame] | 452 | removeChildrenInRange(startNode, startOffset, m_downstreamEnd.deprecatedEditingOffset()); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 453 | m_endingPosition = m_upstreamStart; |
| 454 | } |
| 455 | } |
commit-queue@webkit.org | 2aa8d03 | 2010-09-22 20:43:26 +0000 | [diff] [blame] | 456 | |
| 457 | // The selection to delete is all in one node. |
| 458 | if (!startNode->renderer() || (!startOffset && m_downstreamEnd.atLastEditingPositionForNode())) |
| 459 | removeNode(startNode); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 460 | } |
| 461 | else { |
justing | d197d6b | 2007-08-25 01:20:33 +0000 | [diff] [blame] | 462 | bool startNodeWasDescendantOfEndNode = m_upstreamStart.node()->isDescendantOf(m_downstreamEnd.node()); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 463 | // The selection to delete spans more than one node. |
justing | d197d6b | 2007-08-25 01:20:33 +0000 | [diff] [blame] | 464 | RefPtr<Node> node(startNode); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 465 | |
| 466 | if (startOffset > 0) { |
justing | 11ec52e | 2006-06-07 00:09:37 +0000 | [diff] [blame] | 467 | if (startNode->isTextNode()) { |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 468 | // in a text node that needs to be trimmed |
justing | 4855c4f | 2007-08-23 18:11:04 +0000 | [diff] [blame] | 469 | Text *text = static_cast<Text *>(node.get()); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 470 | deleteTextFromNode(text, startOffset, text->length() - startOffset); |
| 471 | node = node->traverseNextNode(); |
| 472 | } else { |
justing | 11ec52e | 2006-06-07 00:09:37 +0000 | [diff] [blame] | 473 | node = startNode->childNode(startOffset); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 474 | } |
commit-queue@webkit.org | 2aa8d03 | 2010-09-22 20:43:26 +0000 | [diff] [blame] | 475 | } else if (startNode == m_upstreamEnd.node() && startNode->isTextNode()) { |
| 476 | Text* text = static_cast<Text*>(m_upstreamEnd.node()); |
| 477 | deleteTextFromNode(text, 0, m_upstreamEnd.deprecatedEditingOffset()); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | // handle deleting all nodes that are completely selected |
| 481 | while (node && node != m_downstreamEnd.node()) { |
eric@webkit.org | 05d6e93 | 2009-06-02 21:31:08 +0000 | [diff] [blame] | 482 | if (comparePositions(Position(node.get(), 0), m_downstreamEnd) >= 0) { |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 483 | // traverseNextSibling just blew past the end position, so stop deleting |
| 484 | node = 0; |
justing | 4855c4f | 2007-08-23 18:11:04 +0000 | [diff] [blame] | 485 | } else if (!m_downstreamEnd.node()->isDescendantOf(node.get())) { |
justing | 2475432 | 2007-07-09 20:27:05 +0000 | [diff] [blame] | 486 | RefPtr<Node> nextNode = node->traverseNextSibling(); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 487 | // if we just removed a node from the end container, update end position so the |
| 488 | // check above will work |
| 489 | if (node->parentNode() == m_downstreamEnd.node()) { |
eric@webkit.org | 4e32ebc | 2009-04-30 01:09:57 +0000 | [diff] [blame] | 490 | ASSERT(node->nodeIndex() < (unsigned)m_downstreamEnd.deprecatedEditingOffset()); |
| 491 | m_downstreamEnd = Position(m_downstreamEnd.node(), m_downstreamEnd.deprecatedEditingOffset() - 1); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 492 | } |
justing | 4855c4f | 2007-08-23 18:11:04 +0000 | [diff] [blame] | 493 | removeNode(node.get()); |
justing | 2475432 | 2007-07-09 20:27:05 +0000 | [diff] [blame] | 494 | node = nextNode.get(); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 495 | } else { |
justing | 11ec52e | 2006-06-07 00:09:37 +0000 | [diff] [blame] | 496 | Node* n = node->lastDescendant(); |
eric@webkit.org | 4e32ebc | 2009-04-30 01:09:57 +0000 | [diff] [blame] | 497 | if (m_downstreamEnd.node() == n && m_downstreamEnd.deprecatedEditingOffset() >= caretMaxOffset(n)) { |
justing | 4855c4f | 2007-08-23 18:11:04 +0000 | [diff] [blame] | 498 | removeNode(node.get()); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 499 | node = 0; |
justing | 9556126 | 2006-06-21 21:22:50 +0000 | [diff] [blame] | 500 | } else |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 501 | node = node->traverseNextNode(); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 502 | } |
| 503 | } |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 504 | |
eric@webkit.org | 4e32ebc | 2009-04-30 01:09:57 +0000 | [diff] [blame] | 505 | if (m_downstreamEnd.node() != startNode && !m_upstreamStart.node()->isDescendantOf(m_downstreamEnd.node()) && m_downstreamEnd.node()->inDocument() && m_downstreamEnd.deprecatedEditingOffset() >= caretMinOffset(m_downstreamEnd.node())) { |
eric@webkit.org | f3a1881 | 2009-03-20 18:45:15 +0000 | [diff] [blame] | 506 | if (m_downstreamEnd.atLastEditingPositionForNode() && !canHaveChildrenForEditing(m_downstreamEnd.node())) { |
justing | 395a18e | 2007-06-26 06:52:15 +0000 | [diff] [blame] | 507 | // The node itself is fully selected, not just its contents. Delete it. |
justing | f81641c | 2006-06-14 23:23:50 +0000 | [diff] [blame] | 508 | removeNode(m_downstreamEnd.node()); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 509 | } else { |
| 510 | if (m_downstreamEnd.node()->isTextNode()) { |
| 511 | // in a text node that needs to be trimmed |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 512 | Text *text = static_cast<Text *>(m_downstreamEnd.node()); |
eric@webkit.org | 4e32ebc | 2009-04-30 01:09:57 +0000 | [diff] [blame] | 513 | if (m_downstreamEnd.deprecatedEditingOffset() > 0) { |
| 514 | deleteTextFromNode(text, 0, m_downstreamEnd.deprecatedEditingOffset()); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 515 | } |
justing | d197d6b | 2007-08-25 01:20:33 +0000 | [diff] [blame] | 516 | // Remove children of m_downstreamEnd.node() that come after m_upstreamStart. |
| 517 | // Don't try to remove children if m_upstreamStart was inside m_downstreamEnd.node() |
| 518 | // and m_upstreamStart has been removed from the document, because then we don't |
| 519 | // know how many children to remove. |
| 520 | // FIXME: Make m_upstreamStart a position we update as we remove content, then we can |
| 521 | // always know which children to remove. |
| 522 | } else if (!(startNodeWasDescendantOfEndNode && !m_upstreamStart.node()->inDocument())) { |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 523 | int offset = 0; |
justing | 5b9c5a0 | 2006-09-23 00:14:55 +0000 | [diff] [blame] | 524 | if (m_upstreamStart.node()->isDescendantOf(m_downstreamEnd.node())) { |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 525 | Node *n = m_upstreamStart.node(); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 526 | while (n && n->parentNode() != m_downstreamEnd.node()) |
| 527 | n = n->parentNode(); |
| 528 | if (n) |
| 529 | offset = n->nodeIndex() + 1; |
| 530 | } |
eric@webkit.org | 4e32ebc | 2009-04-30 01:09:57 +0000 | [diff] [blame] | 531 | removeChildrenInRange(m_downstreamEnd.node(), offset, m_downstreamEnd.deprecatedEditingOffset()); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 532 | m_downstreamEnd = Position(m_downstreamEnd.node(), offset); |
| 533 | } |
| 534 | } |
| 535 | } |
| 536 | } |
| 537 | } |
| 538 | |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 539 | void DeleteSelectionCommand::fixupWhitespace() |
| 540 | { |
darin | 26f5b36 | 2005-12-22 04:11:39 +0000 | [diff] [blame] | 541 | updateLayout(); |
justing | f81641c | 2006-06-14 23:23:50 +0000 | [diff] [blame] | 542 | // FIXME: isRenderedCharacter should be removed, and we should use VisiblePosition::characterAfter and VisiblePosition::characterBefore |
inferno@chromium.org | 776ab18 | 2010-07-20 21:11:52 +0000 | [diff] [blame] | 543 | if (m_leadingWhitespace.isNotNull() && !m_leadingWhitespace.isRenderedCharacter() && m_leadingWhitespace.node()->isTextNode()) { |
darin | 38ae534 | 2006-07-25 04:39:25 +0000 | [diff] [blame] | 544 | Text* textNode = static_cast<Text*>(m_leadingWhitespace.node()); |
| 545 | ASSERT(!textNode->renderer() || textNode->renderer()->style()->collapseWhiteSpace()); |
eric@webkit.org | 4e32ebc | 2009-04-30 01:09:57 +0000 | [diff] [blame] | 546 | replaceTextInNode(textNode, m_leadingWhitespace.deprecatedEditingOffset(), 1, nonBreakingSpaceString()); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 547 | } |
inferno@chromium.org | 776ab18 | 2010-07-20 21:11:52 +0000 | [diff] [blame] | 548 | if (m_trailingWhitespace.isNotNull() && !m_trailingWhitespace.isRenderedCharacter() && m_trailingWhitespace.node()->isTextNode()) { |
darin | 38ae534 | 2006-07-25 04:39:25 +0000 | [diff] [blame] | 549 | Text* textNode = static_cast<Text*>(m_trailingWhitespace.node()); |
| 550 | ASSERT(!textNode->renderer() ||textNode->renderer()->style()->collapseWhiteSpace()); |
eric@webkit.org | 4e32ebc | 2009-04-30 01:09:57 +0000 | [diff] [blame] | 551 | replaceTextInNode(textNode, m_trailingWhitespace.deprecatedEditingOffset(), 1, nonBreakingSpaceString()); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 552 | } |
| 553 | } |
| 554 | |
justing | f81641c | 2006-06-14 23:23:50 +0000 | [diff] [blame] | 555 | // If a selection starts in one block and ends in another, we have to merge to bring content before the |
| 556 | // start together with content after the end. |
justing | 217ded8 | 2006-04-04 20:52:10 +0000 | [diff] [blame] | 557 | void DeleteSelectionCommand::mergeParagraphs() |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 558 | { |
justin.garcia@apple.com | 7e95c7b | 2008-11-12 20:08:06 +0000 | [diff] [blame] | 559 | if (!m_mergeBlocksAfterDelete) { |
| 560 | if (m_pruneStartBlockIfNecessary) { |
justin.garcia@apple.com | 7e95c7b | 2008-11-12 20:08:06 +0000 | [diff] [blame] | 561 | // We aren't going to merge into the start block, so remove it if it's empty. |
mitz@apple.com | afdb80f | 2009-05-06 08:02:27 +0000 | [diff] [blame] | 562 | prune(m_startBlock); |
justin.garcia@apple.com | 7e95c7b | 2008-11-12 20:08:06 +0000 | [diff] [blame] | 563 | // Removing the start block during a deletion is usually an indication that we need |
| 564 | // a placeholder, but not in this case. |
| 565 | m_needPlaceholder = false; |
| 566 | } |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 567 | return; |
justin.garcia@apple.com | 7e95c7b | 2008-11-12 20:08:06 +0000 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | // It shouldn't have been asked to both try and merge content into the start block and prune it. |
| 571 | ASSERT(!m_pruneStartBlockIfNecessary); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 572 | |
justing | 217ded8 | 2006-04-04 20:52:10 +0000 | [diff] [blame] | 573 | // FIXME: Deletion should adjust selection endpoints as it removes nodes so that we never get into this state (4099839). |
| 574 | if (!m_downstreamEnd.node()->inDocument() || !m_upstreamStart.node()->inDocument()) |
| 575 | return; |
| 576 | |
justing | ced6772 | 2006-05-01 21:43:03 +0000 | [diff] [blame] | 577 | // FIXME: The deletion algorithm shouldn't let this happen. |
eric@webkit.org | 05d6e93 | 2009-06-02 21:31:08 +0000 | [diff] [blame] | 578 | if (comparePositions(m_upstreamStart, m_downstreamEnd) > 0) |
justing | ced6772 | 2006-05-01 21:43:03 +0000 | [diff] [blame] | 579 | return; |
| 580 | |
justin.garcia@apple.com | 68510a5 | 2009-04-23 00:54:42 +0000 | [diff] [blame] | 581 | // There's nothing to merge. |
| 582 | if (m_upstreamStart == m_downstreamEnd) |
justing | ced6772 | 2006-05-01 21:43:03 +0000 | [diff] [blame] | 583 | return; |
| 584 | |
justing | 217ded8 | 2006-04-04 20:52:10 +0000 | [diff] [blame] | 585 | VisiblePosition startOfParagraphToMove(m_downstreamEnd); |
| 586 | VisiblePosition mergeDestination(m_upstreamStart); |
| 587 | |
justin.garcia@apple.com | 60777c9 | 2007-11-14 22:33:07 +0000 | [diff] [blame] | 588 | // m_downstreamEnd's block has been emptied out by deletion. There is no content inside of it to |
| 589 | // move, so just remove it. |
| 590 | Element* endBlock = static_cast<Element*>(enclosingBlock(m_downstreamEnd.node())); |
| 591 | if (!startOfParagraphToMove.deepEquivalent().node() || !endBlock->contains(startOfParagraphToMove.deepEquivalent().node())) { |
| 592 | removeNode(enclosingBlock(m_downstreamEnd.node())); |
| 593 | return; |
| 594 | } |
| 595 | |
justing | ced6772 | 2006-05-01 21:43:03 +0000 | [diff] [blame] | 596 | // We need to merge into m_upstreamStart's block, but it's been emptied out and collapsed by deletion. |
mitz@apple.com | fc292c3 | 2009-05-03 04:09:41 +0000 | [diff] [blame] | 597 | if (!mergeDestination.deepEquivalent().node() || !mergeDestination.deepEquivalent().node()->isDescendantOf(m_upstreamStart.node()->enclosingBlockFlowElement()) || m_startsAtEmptyLine) { |
justing | 5097f5d5 | 2007-04-20 00:08:50 +0000 | [diff] [blame] | 598 | insertNodeAt(createBreakElement(document()).get(), m_upstreamStart); |
justing | ced6772 | 2006-05-01 21:43:03 +0000 | [diff] [blame] | 599 | mergeDestination = VisiblePosition(m_upstreamStart); |
| 600 | } |
harrison | dec5e09 | 2006-01-25 21:05:02 +0000 | [diff] [blame] | 601 | |
justing | ced6772 | 2006-05-01 21:43:03 +0000 | [diff] [blame] | 602 | if (mergeDestination == startOfParagraphToMove) |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 603 | return; |
justing | 217ded8 | 2006-04-04 20:52:10 +0000 | [diff] [blame] | 604 | |
| 605 | VisiblePosition endOfParagraphToMove = endOfParagraph(startOfParagraphToMove); |
| 606 | |
darin | 2b0ff52 | 2006-05-03 19:32:21 +0000 | [diff] [blame] | 607 | if (mergeDestination == endOfParagraphToMove) |
| 608 | return; |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 609 | |
justing | e809f93 | 2006-06-23 22:36:12 +0000 | [diff] [blame] | 610 | // The rule for merging into an empty block is: only do so if its farther to the right. |
| 611 | // FIXME: Consider RTL. |
mitz@apple.com | fc292c3 | 2009-05-03 04:09:41 +0000 | [diff] [blame] | 612 | if (!m_startsAtEmptyLine && isStartOfParagraph(mergeDestination) && startOfParagraphToMove.absoluteCaretBounds().x() > mergeDestination.absoluteCaretBounds().x()) { |
enrica@apple.com | 25b91e3 | 2010-01-11 18:04:58 +0000 | [diff] [blame] | 613 | if (mergeDestination.deepEquivalent().downstream().node()->hasTagName(brTag)) { |
| 614 | removeNodeAndPruneAncestors(mergeDestination.deepEquivalent().downstream().node()); |
| 615 | m_endingPosition = startOfParagraphToMove.deepEquivalent(); |
| 616 | return; |
| 617 | } |
justing | e809f93 | 2006-06-23 22:36:12 +0000 | [diff] [blame] | 618 | } |
| 619 | |
justin.garcia@apple.com | 619526a | 2009-11-05 05:25:24 +0000 | [diff] [blame] | 620 | // Block images, tables and horizontal rules cannot be made inline with content at mergeDestination. If there is |
| 621 | // any (!isStartOfParagraph(mergeDestination)), don't merge, just move the caret to just before the selection we deleted. |
| 622 | // See https://bugs.webkit.org/show_bug.cgi?id=25439 |
| 623 | if (isRenderedAsNonInlineTableImageOrHR(startOfParagraphToMove.deepEquivalent().node()) && !isStartOfParagraph(mergeDestination)) { |
| 624 | m_endingPosition = m_upstreamStart; |
| 625 | return; |
| 626 | } |
| 627 | |
darin@apple.com | b7bf13a | 2008-03-11 23:43:12 +0000 | [diff] [blame] | 628 | RefPtr<Range> range = Range::create(document(), rangeCompliantEquivalent(startOfParagraphToMove.deepEquivalent()), rangeCompliantEquivalent(endOfParagraphToMove.deepEquivalent())); |
| 629 | RefPtr<Range> rangeToBeReplaced = Range::create(document(), rangeCompliantEquivalent(mergeDestination.deepEquivalent()), rangeCompliantEquivalent(mergeDestination.deepEquivalent())); |
justing | f90e28d | 2007-07-30 18:25:19 +0000 | [diff] [blame] | 630 | if (!document()->frame()->editor()->client()->shouldMoveRangeAfterDelete(range.get(), rangeToBeReplaced.get())) |
| 631 | return; |
| 632 | |
justin.garcia@apple.com | 60777c9 | 2007-11-14 22:33:07 +0000 | [diff] [blame] | 633 | // moveParagraphs will insert placeholders if it removes blocks that would require their use, don't let block |
| 634 | // removals that it does cause the insertion of *another* placeholder. |
| 635 | bool needPlaceholder = m_needPlaceholder; |
rniwa@webkit.org | d34b748 | 2010-08-12 02:05:11 +0000 | [diff] [blame] | 636 | bool paragraphToMergeIsEmpty = (startOfParagraphToMove == endOfParagraphToMove); |
| 637 | moveParagraph(startOfParagraphToMove, endOfParagraphToMove, mergeDestination, false, !paragraphToMergeIsEmpty); |
justin.garcia@apple.com | 60777c9 | 2007-11-14 22:33:07 +0000 | [diff] [blame] | 638 | m_needPlaceholder = needPlaceholder; |
justing | 93798f1 | 2006-06-08 04:04:38 +0000 | [diff] [blame] | 639 | // The endingPosition was likely clobbered by the move, so recompute it (moveParagraph selects the moved paragraph). |
justing | 11ec52e | 2006-06-07 00:09:37 +0000 | [diff] [blame] | 640 | m_endingPosition = endingSelection().start(); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 641 | } |
| 642 | |
justing | 187d292 | 2007-07-17 21:05:21 +0000 | [diff] [blame] | 643 | void DeleteSelectionCommand::removePreviouslySelectedEmptyTableRows() |
| 644 | { |
justin.garcia@apple.com | 99a0554 | 2007-12-13 06:56:28 +0000 | [diff] [blame] | 645 | if (m_endTableRow && m_endTableRow->inDocument() && m_endTableRow != m_startTableRow) { |
| 646 | Node* row = m_endTableRow->previousSibling(); |
| 647 | while (row && row != m_startTableRow) { |
justing | 187d292 | 2007-07-17 21:05:21 +0000 | [diff] [blame] | 648 | RefPtr<Node> previousRow = row->previousSibling(); |
| 649 | if (isTableRowEmpty(row)) |
| 650 | // Use a raw removeNode, instead of DeleteSelectionCommand's, because |
| 651 | // that won't remove rows, it only empties them in preparation for this function. |
| 652 | CompositeEditCommand::removeNode(row); |
| 653 | row = previousRow.get(); |
| 654 | } |
| 655 | } |
| 656 | |
justing | 7d4a434 | 2007-10-13 00:40:48 +0000 | [diff] [blame] | 657 | // Remove empty rows after the start row. |
| 658 | if (m_startTableRow && m_startTableRow->inDocument() && m_startTableRow != m_endTableRow) { |
justing | 187d292 | 2007-07-17 21:05:21 +0000 | [diff] [blame] | 659 | Node* row = m_startTableRow->nextSibling(); |
justin.garcia@apple.com | 99a0554 | 2007-12-13 06:56:28 +0000 | [diff] [blame] | 660 | while (row && row != m_endTableRow) { |
justing | 187d292 | 2007-07-17 21:05:21 +0000 | [diff] [blame] | 661 | RefPtr<Node> nextRow = row->nextSibling(); |
| 662 | if (isTableRowEmpty(row)) |
| 663 | CompositeEditCommand::removeNode(row); |
| 664 | row = nextRow.get(); |
| 665 | } |
| 666 | } |
justin.garcia@apple.com | 99a0554 | 2007-12-13 06:56:28 +0000 | [diff] [blame] | 667 | |
| 668 | if (m_endTableRow && m_endTableRow->inDocument() && m_endTableRow != m_startTableRow) |
| 669 | if (isTableRowEmpty(m_endTableRow.get())) { |
| 670 | // Don't remove m_endTableRow if it's where we're putting the ending selection. |
| 671 | if (!m_endingPosition.node()->isDescendantOf(m_endTableRow.get())) { |
| 672 | // FIXME: We probably shouldn't remove m_endTableRow unless it's fully selected, even if it is empty. |
| 673 | // We'll need to start adjusting the selection endpoints during deletion to know whether or not m_endTableRow |
| 674 | // was fully selected here. |
| 675 | CompositeEditCommand::removeNode(m_endTableRow.get()); |
| 676 | } |
| 677 | } |
justing | 187d292 | 2007-07-17 21:05:21 +0000 | [diff] [blame] | 678 | } |
| 679 | |
justin.garcia@apple.com | d1eac9a | 2008-06-20 00:24:13 +0000 | [diff] [blame] | 680 | void DeleteSelectionCommand::calculateTypingStyleAfterDelete() |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 681 | { |
justin.garcia@apple.com | 6412551 | 2008-02-20 17:48:44 +0000 | [diff] [blame] | 682 | if (!m_typingStyle) |
| 683 | return; |
| 684 | |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 685 | // Compute the difference between the style before the delete and the style now |
mjs | 810e976 | 2006-01-09 21:39:14 +0000 | [diff] [blame] | 686 | // 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] | 687 | // 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] | 688 | // so the Frame::appliedEditing can set it after the whole composite command |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 689 | // has completed. |
justing | 503ccf1 | 2005-07-29 22:50:47 +0000 | [diff] [blame] | 690 | |
eseidel | ef50898 | 2006-01-03 09:19:17 +0000 | [diff] [blame] | 691 | // If we deleted into a blockquote, but are now no longer in a blockquote, use the alternate typing style |
| 692 | if (m_deleteIntoBlockquoteStyle && !nearestMailBlockquote(m_endingPosition.node())) |
| 693 | m_typingStyle = m_deleteIntoBlockquoteStyle; |
| 694 | m_deleteIntoBlockquoteStyle = 0; |
rniwa@webkit.org | 1de555e | 2009-08-07 21:36:24 +0000 | [diff] [blame] | 695 | |
rniwa@webkit.org | 7e06f4a | 2010-11-06 21:19:59 +0000 | [diff] [blame^] | 696 | m_typingStyle->prepareToApplyAt(m_endingPosition); |
| 697 | if (m_typingStyle->isEmpty()) |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 698 | m_typingStyle = 0; |
justin.garcia@apple.com | d1eac9a | 2008-06-20 00:24:13 +0000 | [diff] [blame] | 699 | VisiblePosition visibleEnd(m_endingPosition); |
| 700 | if (m_typingStyle && |
| 701 | isStartOfParagraph(visibleEnd) && |
| 702 | isEndOfParagraph(visibleEnd) && |
justin.garcia@apple.com | d5c8f43 | 2009-04-14 18:48:16 +0000 | [diff] [blame] | 703 | lineBreakExistsAtVisiblePosition(visibleEnd)) { |
justin.garcia@apple.com | d1eac9a | 2008-06-20 00:24:13 +0000 | [diff] [blame] | 704 | // Apply style to the placeholder that is now holding open the empty paragraph. |
| 705 | // This makes sure that the paragraph has the right height, and that the paragraph |
| 706 | // takes on the right style and retains it even if you move the selection away and |
| 707 | // then move it back (which will clear typing style). |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 708 | |
justin.garcia@apple.com | d1eac9a | 2008-06-20 00:24:13 +0000 | [diff] [blame] | 709 | setEndingSelection(visibleEnd); |
rniwa@webkit.org | 7e06f4a | 2010-11-06 21:19:59 +0000 | [diff] [blame^] | 710 | applyStyle(m_typingStyle->style(), EditActionUnspecified); |
justin.garcia@apple.com | d1eac9a | 2008-06-20 00:24:13 +0000 | [diff] [blame] | 711 | // applyStyle can destroy the placeholder that was at m_endingPosition if it needs to |
| 712 | // move it, but it will set an endingSelection() at [movedPlaceholder, 0] if it does so. |
| 713 | m_endingPosition = endingSelection().start(); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 714 | m_typingStyle = 0; |
| 715 | } |
justin.garcia@apple.com | d1eac9a | 2008-06-20 00:24:13 +0000 | [diff] [blame] | 716 | // This is where we've deleted all traces of a style but not a whole paragraph (that's handled above). |
| 717 | // In this case if we start typing, the new characters should have the same style as the just deleted ones, |
| 718 | // but, if we change the selection, come back and start typing that style should be lost. Also see |
| 719 | // preserveTypingStyle() below. |
darin@apple.com | b17ec2f | 2010-09-10 22:06:47 +0000 | [diff] [blame] | 720 | document()->frame()->selection()->setTypingStyle(m_typingStyle); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 721 | } |
| 722 | |
| 723 | void DeleteSelectionCommand::clearTransientState() |
| 724 | { |
eric@webkit.org | 87ea95c | 2009-02-09 21:43:24 +0000 | [diff] [blame] | 725 | m_selectionToDelete = VisibleSelection(); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 726 | m_upstreamStart.clear(); |
| 727 | m_downstreamStart.clear(); |
| 728 | m_upstreamEnd.clear(); |
| 729 | m_downstreamEnd.clear(); |
| 730 | m_endingPosition.clear(); |
| 731 | m_leadingWhitespace.clear(); |
| 732 | m_trailingWhitespace.clear(); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 733 | } |
| 734 | |
| 735 | void DeleteSelectionCommand::doApply() |
| 736 | { |
| 737 | // If selection has not been set to a custom selection when the command was created, |
| 738 | // use the current ending selection. |
| 739 | if (!m_hasSelectionToDelete) |
| 740 | m_selectionToDelete = endingSelection(); |
rniwa@webkit.org | d39a1e7 | 2010-08-25 19:19:26 +0000 | [diff] [blame] | 741 | |
| 742 | if (!m_selectionToDelete.isNonOrphanedRange()) |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 743 | return; |
| 744 | |
adele | b2f2b66 | 2006-07-15 17:22:50 +0000 | [diff] [blame] | 745 | // If the deletion is occurring in a text field, and we're not deleting to replace the selection, then let the frame call across the bridge to notify the form delegate. |
| 746 | if (!m_replace) { |
| 747 | Node* startNode = m_selectionToDelete.start().node(); |
| 748 | Node* ancestorNode = startNode ? startNode->shadowAncestorNode() : 0; |
darin | 3f6601d | 2007-02-17 01:15:48 +0000 | [diff] [blame] | 749 | if (ancestorNode && ancestorNode->hasTagName(inputTag) |
| 750 | && static_cast<HTMLInputElement*>(ancestorNode)->isTextField() |
| 751 | && ancestorNode->focused()) |
darin@apple.com | becacd8 | 2010-09-09 23:08:54 +0000 | [diff] [blame] | 752 | document()->frame()->editor()->textWillBeDeletedInTextField(static_cast<Element*>(ancestorNode)); |
adele | b2f2b66 | 2006-07-15 17:22:50 +0000 | [diff] [blame] | 753 | } |
adele | 174364d | 2006-04-13 01:50:04 +0000 | [diff] [blame] | 754 | |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 755 | // save this to later make the selection with |
darin | 9aa45a4 | 2006-01-15 23:32:02 +0000 | [diff] [blame] | 756 | EAffinity affinity = m_selectionToDelete.affinity(); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 757 | |
justing | 11ec52e | 2006-06-07 00:09:37 +0000 | [diff] [blame] | 758 | Position downstreamEnd = m_selectionToDelete.end().downstream(); |
commit-queue@webkit.org | 1d836ce | 2010-10-29 21:47:01 +0000 | [diff] [blame] | 759 | m_needPlaceholder = isStartOfParagraph(m_selectionToDelete.visibleStart(), Position::CanCrossEditingBoundary) |
| 760 | && isEndOfParagraph(m_selectionToDelete.visibleEnd(), Position::CanCrossEditingBoundary) |
| 761 | && !lineBreakExistsAtVisiblePosition(m_selectionToDelete.visibleEnd()); |
justing | 552eeaf | 2007-04-20 01:53:22 +0000 | [diff] [blame] | 762 | if (m_needPlaceholder) { |
| 763 | // Don't need a placeholder when deleting a selection that starts just before a table |
| 764 | // and ends inside it (we do need placeholders to hold open empty cells, but that's |
| 765 | // handled elsewhere). |
| 766 | if (Node* table = isLastPositionBeforeTable(m_selectionToDelete.visibleStart())) |
| 767 | if (m_selectionToDelete.end().node()->isDescendantOf(table)) |
| 768 | m_needPlaceholder = false; |
| 769 | } |
| 770 | |
justing | 11ec52e | 2006-06-07 00:09:37 +0000 | [diff] [blame] | 771 | |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 772 | // set up our state |
| 773 | initializePositionData(); |
harrison | dff01cd | 2005-07-18 23:21:19 +0000 | [diff] [blame] | 774 | |
hyatt | 0c05e10 | 2006-04-14 08:15:00 +0000 | [diff] [blame] | 775 | // 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] | 776 | deleteInsignificantTextDownstream(m_trailingWhitespace); |
| 777 | |
| 778 | saveTypingStyleState(); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 779 | |
harrison | e4b84c5 | 2005-07-18 18:14:59 +0000 | [diff] [blame] | 780 | // deleting just a BR is handled specially, at least because we do not |
| 781 | // want to replace it with a placeholder BR! |
| 782 | if (handleSpecialCaseBRDelete()) { |
justin.garcia@apple.com | d1eac9a | 2008-06-20 00:24:13 +0000 | [diff] [blame] | 783 | calculateTypingStyleAfterDelete(); |
eric@webkit.org | 87ea95c | 2009-02-09 21:43:24 +0000 | [diff] [blame] | 784 | setEndingSelection(VisibleSelection(m_endingPosition, affinity)); |
harrison | e4b84c5 | 2005-07-18 18:14:59 +0000 | [diff] [blame] | 785 | clearTransientState(); |
| 786 | rebalanceWhitespace(); |
| 787 | return; |
| 788 | } |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 789 | |
harrison | e4b84c5 | 2005-07-18 18:14:59 +0000 | [diff] [blame] | 790 | handleGeneralDelete(); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 791 | |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 792 | fixupWhitespace(); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 793 | |
justing | f81641c | 2006-06-14 23:23:50 +0000 | [diff] [blame] | 794 | mergeParagraphs(); |
| 795 | |
justing | 187d292 | 2007-07-17 21:05:21 +0000 | [diff] [blame] | 796 | removePreviouslySelectedEmptyTableRows(); |
| 797 | |
justin.garcia@apple.com | 60777c9 | 2007-11-14 22:33:07 +0000 | [diff] [blame] | 798 | RefPtr<Node> placeholder = m_needPlaceholder ? createBreakElement(document()).get() : 0; |
| 799 | |
justing | 11ec52e | 2006-06-07 00:09:37 +0000 | [diff] [blame] | 800 | if (placeholder) |
justing | 5097f5d5 | 2007-04-20 00:08:50 +0000 | [diff] [blame] | 801 | insertNodeAt(placeholder.get(), m_endingPosition); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 802 | |
justing | 92cd9eb | 2006-12-06 21:01:30 +0000 | [diff] [blame] | 803 | rebalanceWhitespaceAt(m_endingPosition); |
| 804 | |
justin.garcia@apple.com | d1eac9a | 2008-06-20 00:24:13 +0000 | [diff] [blame] | 805 | calculateTypingStyleAfterDelete(); |
justing | 11ec52e | 2006-06-07 00:09:37 +0000 | [diff] [blame] | 806 | |
eric@webkit.org | 87ea95c | 2009-02-09 21:43:24 +0000 | [diff] [blame] | 807 | setEndingSelection(VisibleSelection(m_endingPosition, affinity)); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 808 | clearTransientState(); |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 809 | } |
| 810 | |
| 811 | EditAction DeleteSelectionCommand::editingAction() const |
| 812 | { |
| 813 | // Note that DeleteSelectionCommand is also used when the user presses the Delete key, |
| 814 | // but in that case there's a TypingCommand that supplies the editingAction(), so |
| 815 | // the Undo menu correctly shows "Undo Typing" |
| 816 | return EditActionCut; |
| 817 | } |
| 818 | |
justin.garcia@apple.com | d1eac9a | 2008-06-20 00:24:13 +0000 | [diff] [blame] | 819 | // Normally deletion doesn't preserve the typing style that was present before it. For example, |
| 820 | // type a character, Bold, then delete the character and start typing. The Bold typing style shouldn't |
| 821 | // stick around. Deletion should preserve a typing style that *it* sets, however. |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 822 | bool DeleteSelectionCommand::preservesTypingStyle() const |
| 823 | { |
justin.garcia@apple.com | d1eac9a | 2008-06-20 00:24:13 +0000 | [diff] [blame] | 824 | return m_typingStyle; |
mjs | a0fe404 | 2005-05-13 08:37:15 +0000 | [diff] [blame] | 825 | } |
| 826 | |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 827 | } // namespace WebCore |