mjs | 84e724c | 2005-05-24 07:21:47 +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 "BreakBlockquoteCommand.h" |
mjs | 84e724c | 2005-05-24 07:21:47 +0000 | [diff] [blame] | 28 | |
darin@apple.com | 930f438 | 2009-03-29 17:15:41 +0000 | [diff] [blame] | 29 | #include "HTMLElement.h" |
darin | 98fa8b8 | 2006-03-20 08:03:57 +0000 | [diff] [blame] | 30 | #include "HTMLNames.h" |
antti@apple.com | 5d47b58 | 2012-12-11 00:13:29 +0000 | [diff] [blame] | 31 | #include "NodeTraversal.h" |
darin@apple.com | 930f438 | 2009-03-29 17:15:41 +0000 | [diff] [blame] | 32 | #include "RenderListItem.h" |
eseidel | 40eb1b9 | 2006-03-25 22:20:36 +0000 | [diff] [blame] | 33 | #include "Text.h" |
eseidel | 40eb1b9 | 2006-03-25 22:20:36 +0000 | [diff] [blame] | 34 | #include "htmlediting.h" |
| 35 | |
darin | 4e051a0 | 2006-01-23 17:07:29 +0000 | [diff] [blame] | 36 | namespace WebCore { |
darin | edbc5e4 | 2005-08-25 23:13:58 +0000 | [diff] [blame] | 37 | |
darin | 4e051a0 | 2006-01-23 17:07:29 +0000 | [diff] [blame] | 38 | using namespace HTMLNames; |
mjs | 84e724c | 2005-05-24 07:21:47 +0000 | [diff] [blame] | 39 | |
akling@apple.com | d455eb6 | 2013-09-01 05:30:31 +0000 | [diff] [blame] | 40 | BreakBlockquoteCommand::BreakBlockquoteCommand(Document& document) |
harrison | a34f524 | 2005-11-03 02:20:15 +0000 | [diff] [blame] | 41 | : CompositeEditCommand(document) |
mjs | 84e724c | 2005-05-24 07:21:47 +0000 | [diff] [blame] | 42 | { |
| 43 | } |
| 44 | |
mjs | 84e724c | 2005-05-24 07:21:47 +0000 | [diff] [blame] | 45 | void BreakBlockquoteCommand::doApply() |
| 46 | { |
justin.garcia@apple.com | 8c6832c | 2008-10-30 23:46:31 +0000 | [diff] [blame] | 47 | if (endingSelection().isNone()) |
mjs | 84e724c | 2005-05-24 07:21:47 +0000 | [diff] [blame] | 48 | return; |
| 49 | |
| 50 | // Delete the current selection. |
justin.garcia@apple.com | 8c6832c | 2008-10-30 23:46:31 +0000 | [diff] [blame] | 51 | if (endingSelection().isRange()) |
mjs | 84e724c | 2005-05-24 07:21:47 +0000 | [diff] [blame] | 52 | deleteSelection(false, false); |
justin.garcia@apple.com | 8c6832c | 2008-10-30 23:46:31 +0000 | [diff] [blame] | 53 | |
eric@webkit.org | 5fbc25c | 2009-09-29 23:10:01 +0000 | [diff] [blame] | 54 | // This is a scenario that should never happen, but we want to |
| 55 | // make sure we don't dereference a null pointer below. |
| 56 | |
| 57 | ASSERT(!endingSelection().isNone()); |
| 58 | |
| 59 | if (endingSelection().isNone()) |
| 60 | return; |
| 61 | |
justin.garcia@apple.com | 8c6832c | 2008-10-30 23:46:31 +0000 | [diff] [blame] | 62 | VisiblePosition visiblePos = endingSelection().visibleStart(); |
eric@webkit.org | 5fbc25c | 2009-09-29 23:10:01 +0000 | [diff] [blame] | 63 | |
justin.garcia@apple.com | 8c6832c | 2008-10-30 23:46:31 +0000 | [diff] [blame] | 64 | // pos is a position equivalent to the caret. We use downstream() so that pos will |
| 65 | // be in the first node that we need to move (there are a few exceptions to this, see below). |
| 66 | Position pos = endingSelection().start().downstream(); |
mjs | 84e724c | 2005-05-24 07:21:47 +0000 | [diff] [blame] | 67 | |
| 68 | // Find the top-most blockquote from the start. |
rniwa@webkit.org | 24bd1d3 | 2011-03-17 00:03:22 +0000 | [diff] [blame] | 69 | Node* topBlockquote = highestEnclosingNodeOfType(pos, isMailBlockquote); |
| 70 | if (!topBlockquote || !topBlockquote->parentNode() || !topBlockquote->isElementNode()) |
mjs | 84e724c | 2005-05-24 07:21:47 +0000 | [diff] [blame] | 71 | return; |
| 72 | |
weinig@apple.com | 8d80b86 | 2013-09-23 20:24:47 +0000 | [diff] [blame] | 73 | RefPtr<Element> breakNode = createBreakElement(document()); |
adele@apple.com | dbe21c6 | 2009-04-13 15:12:41 +0000 | [diff] [blame] | 74 | |
kmccullough@apple.com | c326351 | 2009-06-23 19:12:32 +0000 | [diff] [blame] | 75 | bool isLastVisPosInNode = isLastVisiblePositionInNode(visiblePos, topBlockquote); |
| 76 | |
adele@apple.com | dbe21c6 | 2009-04-13 15:12:41 +0000 | [diff] [blame] | 77 | // If the position is at the beginning of the top quoted content, we don't need to break the quote. |
kmccullough@apple.com | c326351 | 2009-06-23 19:12:32 +0000 | [diff] [blame] | 78 | // Instead, insert the break before the blockquote, unless the position is as the end of the the quoted content. |
| 79 | if (isFirstVisiblePositionInNode(visiblePos, topBlockquote) && !isLastVisPosInNode) { |
adele@apple.com | dbe21c6 | 2009-04-13 15:12:41 +0000 | [diff] [blame] | 80 | insertNodeBefore(breakNode.get(), topBlockquote); |
rniwa@webkit.org | eccfd3f | 2011-08-16 18:39:52 +0000 | [diff] [blame] | 81 | setEndingSelection(VisibleSelection(positionBeforeNode(breakNode.get()), DOWNSTREAM, endingSelection().isDirectional())); |
adele@apple.com | dbe21c6 | 2009-04-13 15:12:41 +0000 | [diff] [blame] | 82 | rebalanceWhitespace(); |
| 83 | return; |
| 84 | } |
| 85 | |
| 86 | // Insert a break after the top blockquote. |
darin | 7191dd1 | 2006-02-10 00:09:17 +0000 | [diff] [blame] | 87 | insertNodeAfter(breakNode.get(), topBlockquote); |
kmccullough@apple.com | c326351 | 2009-06-23 19:12:32 +0000 | [diff] [blame] | 88 | |
adele@apple.com | dbe21c6 | 2009-04-13 15:12:41 +0000 | [diff] [blame] | 89 | // If we're inserting the break at the end of the quoted content, we don't need to break the quote. |
kmccullough@apple.com | c326351 | 2009-06-23 19:12:32 +0000 | [diff] [blame] | 90 | if (isLastVisPosInNode) { |
rniwa@webkit.org | eccfd3f | 2011-08-16 18:39:52 +0000 | [diff] [blame] | 91 | setEndingSelection(VisibleSelection(positionBeforeNode(breakNode.get()), DOWNSTREAM, endingSelection().isDirectional())); |
adele@apple.com | 44641760 | 2009-04-14 17:40:28 +0000 | [diff] [blame] | 92 | rebalanceWhitespace(); |
justin.garcia@apple.com | 8cba38d | 2008-10-29 21:43:47 +0000 | [diff] [blame] | 93 | return; |
mjs | 84e724c | 2005-05-24 07:21:47 +0000 | [diff] [blame] | 94 | } |
justin.garcia@apple.com | 484cb6b | 2008-10-31 00:11:48 +0000 | [diff] [blame] | 95 | |
| 96 | // Don't move a line break just after the caret. Doing so would create an extra, empty paragraph |
| 97 | // in the new blockquote. |
justin.garcia@apple.com | d5c8f43 | 2009-04-14 18:48:16 +0000 | [diff] [blame] | 98 | if (lineBreakExistsAtVisiblePosition(visiblePos)) |
justin.garcia@apple.com | 484cb6b | 2008-10-31 00:11:48 +0000 | [diff] [blame] | 99 | pos = pos.next(); |
justin.garcia@apple.com | 8cba38d | 2008-10-29 21:43:47 +0000 | [diff] [blame] | 100 | |
adele@apple.com | dbe21c6 | 2009-04-13 15:12:41 +0000 | [diff] [blame] | 101 | // Adjust the position so we don't split at the beginning of a quote. |
rniwa@webkit.org | 24bd1d3 | 2011-03-17 00:03:22 +0000 | [diff] [blame] | 102 | while (isFirstVisiblePositionInNode(VisiblePosition(pos), enclosingNodeOfType(pos, isMailBlockquote))) |
adele@apple.com | dbe21c6 | 2009-04-13 15:12:41 +0000 | [diff] [blame] | 103 | pos = pos.previous(); |
| 104 | |
| 105 | // startNode is the first node that we need to move to the new blockquote. |
rniwa@webkit.org | 62b1697 | 2011-02-21 09:28:48 +0000 | [diff] [blame] | 106 | Node* startNode = pos.deprecatedNode(); |
adele@apple.com | dbe21c6 | 2009-04-13 15:12:41 +0000 | [diff] [blame] | 107 | |
justin.garcia@apple.com | 8cba38d | 2008-10-29 21:43:47 +0000 | [diff] [blame] | 108 | // Split at pos if in the middle of a text node. |
| 109 | if (startNode->isTextNode()) { |
commit-queue@webkit.org | 9b335e4 | 2012-02-12 11:27:56 +0000 | [diff] [blame] | 110 | Text* textNode = toText(startNode); |
eric@webkit.org | 4e32ebc | 2009-04-30 01:09:57 +0000 | [diff] [blame] | 111 | if ((unsigned)pos.deprecatedEditingOffset() >= textNode->length()) { |
antti@apple.com | 5d47b58 | 2012-12-11 00:13:29 +0000 | [diff] [blame] | 112 | startNode = NodeTraversal::next(startNode); |
justin.garcia@apple.com | 74f5c37 | 2008-10-30 20:06:24 +0000 | [diff] [blame] | 113 | ASSERT(startNode); |
eric@webkit.org | 4e32ebc | 2009-04-30 01:09:57 +0000 | [diff] [blame] | 114 | } else if (pos.deprecatedEditingOffset() > 0) |
| 115 | splitTextNode(textNode, pos.deprecatedEditingOffset()); |
| 116 | } else if (pos.deprecatedEditingOffset() > 0) { |
mitz@apple.com | ffea9c7 | 2009-05-07 03:38:25 +0000 | [diff] [blame] | 117 | Node* childAtOffset = startNode->childNode(pos.deprecatedEditingOffset()); |
antti@apple.com | 5d47b58 | 2012-12-11 00:13:29 +0000 | [diff] [blame] | 118 | startNode = childAtOffset ? childAtOffset : NodeTraversal::next(startNode); |
justin.garcia@apple.com | 74f5c37 | 2008-10-30 20:06:24 +0000 | [diff] [blame] | 119 | ASSERT(startNode); |
justin.garcia@apple.com | 8cba38d | 2008-10-29 21:43:47 +0000 | [diff] [blame] | 120 | } |
| 121 | |
justin.garcia@apple.com | 74f5c37 | 2008-10-30 20:06:24 +0000 | [diff] [blame] | 122 | // If there's nothing inside topBlockquote to move, we're finished. |
| 123 | if (!startNode->isDescendantOf(topBlockquote)) { |
rniwa@webkit.org | eccfd3f | 2011-08-16 18:39:52 +0000 | [diff] [blame] | 124 | setEndingSelection(VisibleSelection(VisiblePosition(firstPositionInOrBeforeNode(startNode)), endingSelection().isDirectional())); |
justin.garcia@apple.com | 74f5c37 | 2008-10-30 20:06:24 +0000 | [diff] [blame] | 125 | return; |
justin.garcia@apple.com | 8cba38d | 2008-10-29 21:43:47 +0000 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | // Build up list of ancestors in between the start node and the top blockquote. |
andersca@apple.com | c3523f8 | 2013-10-18 23:41:24 +0000 | [diff] [blame] | 129 | Vector<RefPtr<Element>> ancestors; |
darin@apple.com | e95b53d | 2008-12-23 21:42:46 +0000 | [diff] [blame] | 130 | for (Element* node = startNode->parentElement(); node && node != topBlockquote; node = node->parentElement()) |
justin.garcia@apple.com | 8cba38d | 2008-10-29 21:43:47 +0000 | [diff] [blame] | 131 | ancestors.append(node); |
| 132 | |
| 133 | // Insert a clone of the top blockquote after the break. |
inferno@chromium.org | b9af1c1 | 2013-03-12 00:50:46 +0000 | [diff] [blame] | 134 | RefPtr<Element> clonedBlockquote = toElement(topBlockquote)->cloneElementWithoutChildren(); |
justin.garcia@apple.com | 8cba38d | 2008-10-29 21:43:47 +0000 | [diff] [blame] | 135 | insertNodeAfter(clonedBlockquote.get(), breakNode.get()); |
| 136 | |
| 137 | // Clone startNode's ancestors into the cloned blockquote. |
| 138 | // On exiting this loop, clonedAncestor is the lowest ancestor |
| 139 | // that was cloned (i.e. the clone of either ancestors.last() |
| 140 | // or clonedBlockquote if ancestors is empty). |
darin@apple.com | e95b53d | 2008-12-23 21:42:46 +0000 | [diff] [blame] | 141 | RefPtr<Element> clonedAncestor = clonedBlockquote; |
justin.garcia@apple.com | 8cba38d | 2008-10-29 21:43:47 +0000 | [diff] [blame] | 142 | for (size_t i = ancestors.size(); i != 0; --i) { |
jchaffraix@webkit.org | ba7c01d | 2009-03-12 12:42:09 +0000 | [diff] [blame] | 143 | RefPtr<Element> clonedChild = ancestors[i - 1]->cloneElementWithoutChildren(); |
justin.garcia@apple.com | 8cba38d | 2008-10-29 21:43:47 +0000 | [diff] [blame] | 144 | // Preserve list item numbering in cloned lists. |
| 145 | if (clonedChild->isElementNode() && clonedChild->hasTagName(olTag)) { |
rniwa@webkit.org | b6a8d0c | 2012-06-08 22:35:35 +0000 | [diff] [blame] | 146 | Node* listChildNode = i > 1 ? ancestors[i - 2].get() : startNode; |
justin.garcia@apple.com | 8cba38d | 2008-10-29 21:43:47 +0000 | [diff] [blame] | 147 | // The first child of the cloned list might not be a list item element, |
| 148 | // find the first one so that we know where to start numbering. |
| 149 | while (listChildNode && !listChildNode->hasTagName(liTag)) |
| 150 | listChildNode = listChildNode->nextSibling(); |
leviw@chromium.org | 6d74acc | 2011-03-30 10:46:13 +0000 | [diff] [blame] | 151 | if (listChildNode && listChildNode->renderer() && listChildNode->renderer()->isListItem()) |
darin@apple.com | 73f3fc0 | 2013-10-05 18:22:39 +0000 | [diff] [blame] | 152 | setNodeAttribute(clonedChild, startAttr, AtomicString::number(toRenderListItem(listChildNode->renderer())->value())); |
justin.garcia@apple.com | 8cba38d | 2008-10-29 21:43:47 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | appendNode(clonedChild.get(), clonedAncestor.get()); |
| 156 | clonedAncestor = clonedChild; |
| 157 | } |
rniwa@webkit.org | b6a8d0c | 2012-06-08 22:35:35 +0000 | [diff] [blame] | 158 | |
| 159 | moveRemainingSiblingsToNewParent(startNode, 0, clonedAncestor); |
justin.garcia@apple.com | 8cba38d | 2008-10-29 21:43:47 +0000 | [diff] [blame] | 160 | |
justin.garcia@apple.com | 8cba38d | 2008-10-29 21:43:47 +0000 | [diff] [blame] | 161 | if (!ancestors.isEmpty()) { |
justin.garcia@apple.com | 8cba38d | 2008-10-29 21:43:47 +0000 | [diff] [blame] | 162 | // Split the tree up the ancestor chain until the topBlockquote |
| 163 | // Throughout this loop, clonedParent is the clone of ancestor's parent. |
| 164 | // This is so we can clone ancestor's siblings and place the clones |
| 165 | // into the clone corresponding to the ancestor's parent. |
rniwa@webkit.org | b6a8d0c | 2012-06-08 22:35:35 +0000 | [diff] [blame] | 166 | RefPtr<Element> ancestor; |
| 167 | RefPtr<Element> clonedParent; |
darin@apple.com | e95b53d | 2008-12-23 21:42:46 +0000 | [diff] [blame] | 168 | for (ancestor = ancestors.first(), clonedParent = clonedAncestor->parentElement(); |
justin.garcia@apple.com | 8cba38d | 2008-10-29 21:43:47 +0000 | [diff] [blame] | 169 | ancestor && ancestor != topBlockquote; |
rniwa@webkit.org | b6a8d0c | 2012-06-08 22:35:35 +0000 | [diff] [blame] | 170 | ancestor = ancestor->parentElement(), clonedParent = clonedParent->parentElement()) |
| 171 | moveRemainingSiblingsToNewParent(ancestor->nextSibling(), 0, clonedParent); |
| 172 | |
adele@apple.com | 44641760 | 2009-04-14 17:40:28 +0000 | [diff] [blame] | 173 | // If the startNode's original parent is now empty, remove it |
rniwa@webkit.org | b6a8d0c | 2012-06-08 22:35:35 +0000 | [diff] [blame] | 174 | Node* originalParent = ancestors.first().get(); |
adele@apple.com | 44641760 | 2009-04-14 17:40:28 +0000 | [diff] [blame] | 175 | if (!originalParent->hasChildNodes()) |
| 176 | removeNode(originalParent); |
justin.garcia@apple.com | 8cba38d | 2008-10-29 21:43:47 +0000 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | // Make sure the cloned block quote renders. |
| 180 | addBlockPlaceholderIfNeeded(clonedBlockquote.get()); |
mjs | 84e724c | 2005-05-24 07:21:47 +0000 | [diff] [blame] | 181 | |
| 182 | // Put the selection right before the break. |
rniwa@webkit.org | eccfd3f | 2011-08-16 18:39:52 +0000 | [diff] [blame] | 183 | setEndingSelection(VisibleSelection(positionBeforeNode(breakNode.get()), DOWNSTREAM, endingSelection().isDirectional())); |
mjs | 84e724c | 2005-05-24 07:21:47 +0000 | [diff] [blame] | 184 | rebalanceWhitespace(); |
| 185 | } |
| 186 | |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 187 | } // namespace WebCore |