2 * Copyright (C) 2006, 2008 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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 (IndentOutdentCommandINCLUDING, 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.
27 #include "IndentOutdentCommand.h"
30 #include "HTMLElement.h"
31 #include "HTMLNames.h"
32 #include "InsertLineBreakCommand.h"
33 #include "InsertListCommand.h"
35 #include "RenderObject.h"
36 #include "SplitElementCommand.h"
38 #include "TextIterator.h"
39 #include "htmlediting.h"
40 #include "visible_units.h"
41 #include <wtf/StdLibExtras.h>
45 using namespace HTMLNames;
47 static bool isListOrIndentBlockquote(const Node* node)
49 return node && (node->hasTagName(ulTag) || node->hasTagName(olTag) || node->hasTagName(blockquoteTag));
52 IndentOutdentCommand::IndentOutdentCommand(Document* document, EIndentType typeOfAction, int marginInPixels)
53 : ApplyBlockElementCommand(document, blockquoteTag, "margin: 0 0 0 40px; border: none; padding: 0px;")
54 , m_typeOfAction(typeOfAction)
55 , m_marginInPixels(marginInPixels)
59 bool IndentOutdentCommand::tryIndentingAsListItem(const Position& start, const Position& end)
61 // If our selection is not inside a list, bail out.
62 Node* lastNodeInSelectedParagraph = start.deprecatedNode();
63 RefPtr<Element> listNode = enclosingList(lastNodeInSelectedParagraph);
67 // Find the block that we want to indent. If it's not a list item (e.g., a div inside a list item), we bail out.
68 Element* selectedListItem = static_cast<Element*>(enclosingBlock(lastNodeInSelectedParagraph));
70 // FIXME: we need to deal with the case where there is no li (malformed HTML)
71 if (!selectedListItem->hasTagName(liTag))
74 // FIXME: previousElementSibling does not ignore non-rendered content like <span></span>. Should we?
75 Element* previousList = selectedListItem->previousElementSibling();
76 Element* nextList = selectedListItem->nextElementSibling();
78 RefPtr<Element> newList = document()->createElement(listNode->tagQName(), false);
79 insertNodeBefore(newList, selectedListItem);
81 moveParagraphWithClones(start, end, newList.get(), selectedListItem);
83 if (canMergeLists(previousList, newList.get()))
84 mergeIdenticalElements(previousList, newList);
85 if (canMergeLists(newList.get(), nextList))
86 mergeIdenticalElements(newList, nextList);
91 void IndentOutdentCommand::indentIntoBlockquote(const Position& start, const Position& end, RefPtr<Element>& targetBlockquote)
93 Node* enclosingCell = enclosingNodeOfType(start, &isTableCell);
96 nodeToSplitTo = enclosingCell;
97 else if (enclosingList(start.containerNode()))
98 nodeToSplitTo = enclosingBlock(start.containerNode());
100 nodeToSplitTo = editableRootForPosition(start);
105 RefPtr<Node> nodeAfterStart = start.computeNodeAfterPosition();
106 RefPtr<Node> outerBlock = (start.containerNode() == nodeToSplitTo) ? start.containerNode() : splitTreeToNode(start.containerNode(), nodeToSplitTo);
108 VisiblePosition startOfContents = start;
109 if (!targetBlockquote) {
110 // Create a new blockquote and insert it as a child of the root editable element. We accomplish
111 // this by splitting all parents of the current paragraph up to that point.
112 targetBlockquote = createBlockElement();
113 if (outerBlock == start.containerNode())
114 insertNodeAt(targetBlockquote, start);
116 insertNodeBefore(targetBlockquote, outerBlock);
117 startOfContents = positionAfterNode(targetBlockquote.get());
120 moveParagraphWithClones(startOfContents, end, targetBlockquote.get(), outerBlock.get());
123 void IndentOutdentCommand::outdentParagraph()
125 VisiblePosition visibleStartOfParagraph = startOfParagraph(endingSelection().visibleStart());
126 VisiblePosition visibleEndOfParagraph = endOfParagraph(visibleStartOfParagraph);
128 Node* enclosingNode = enclosingNodeOfType(visibleStartOfParagraph.deepEquivalent(), &isListOrIndentBlockquote);
129 if (!enclosingNode || !enclosingNode->parentNode()->rendererIsEditable()) // We can't outdent if there is no place to go!
132 // Use InsertListCommand to remove the selection from the list
133 if (enclosingNode->hasTagName(olTag)) {
134 applyCommandToComposite(InsertListCommand::create(document(), InsertListCommand::OrderedList));
137 if (enclosingNode->hasTagName(ulTag)) {
138 applyCommandToComposite(InsertListCommand::create(document(), InsertListCommand::UnorderedList));
142 // The selection is inside a blockquote i.e. enclosingNode is a blockquote
143 VisiblePosition positionInEnclosingBlock = VisiblePosition(firstPositionInNode(enclosingNode));
144 // If the blockquote is inline, the start of the enclosing block coincides with
145 // positionInEnclosingBlock.
146 VisiblePosition startOfEnclosingBlock = (enclosingNode->renderer() && enclosingNode->renderer()->isInline()) ? positionInEnclosingBlock : startOfBlock(positionInEnclosingBlock);
147 VisiblePosition lastPositionInEnclosingBlock = VisiblePosition(lastPositionInNode(enclosingNode));
148 VisiblePosition endOfEnclosingBlock = endOfBlock(lastPositionInEnclosingBlock);
149 if (visibleStartOfParagraph == startOfEnclosingBlock &&
150 visibleEndOfParagraph == endOfEnclosingBlock) {
151 // The blockquote doesn't contain anything outside the paragraph, so it can be totally removed.
152 Node* splitPoint = enclosingNode->nextSibling();
153 removeNodePreservingChildren(enclosingNode);
154 // outdentRegion() assumes it is operating on the first paragraph of an enclosing blockquote, but if there are multiply nested blockquotes and we've
155 // just removed one, then this assumption isn't true. By splitting the next containing blockquote after this node, we keep this assumption true
157 if (ContainerNode* splitPointParent = splitPoint->parentNode()) {
158 if (splitPointParent->hasTagName(blockquoteTag)
159 && !splitPoint->hasTagName(blockquoteTag)
160 && splitPointParent->parentNode()->rendererIsEditable()) // We can't outdent if there is no place to go!
161 splitElement(static_cast<Element*>(splitPointParent), splitPoint);
166 visibleStartOfParagraph = VisiblePosition(visibleStartOfParagraph.deepEquivalent());
167 visibleEndOfParagraph = VisiblePosition(visibleEndOfParagraph.deepEquivalent());
168 if (visibleStartOfParagraph.isNotNull() && !isStartOfParagraph(visibleStartOfParagraph))
169 insertNodeAt(createBreakElement(document()), visibleStartOfParagraph.deepEquivalent());
170 if (visibleEndOfParagraph.isNotNull() && !isEndOfParagraph(visibleEndOfParagraph))
171 insertNodeAt(createBreakElement(document()), visibleEndOfParagraph.deepEquivalent());
175 Node* enclosingBlockFlow = enclosingBlock(visibleStartOfParagraph.deepEquivalent().deprecatedNode());
176 RefPtr<Node> splitBlockquoteNode = enclosingNode;
177 if (enclosingBlockFlow != enclosingNode)
178 splitBlockquoteNode = splitTreeToNode(enclosingBlockFlow, enclosingNode, true);
180 // We split the blockquote at where we start outdenting.
181 Node* highestInlineNode = highestEnclosingNodeOfType(visibleStartOfParagraph.deepEquivalent(), isInline, CannotCrossEditingBoundary, enclosingBlockFlow);
182 splitElement(static_cast<Element*>(enclosingNode), (highestInlineNode) ? highestInlineNode : visibleStartOfParagraph.deepEquivalent().deprecatedNode());
184 RefPtr<Node> placeholder = createBreakElement(document());
185 insertNodeBefore(placeholder, splitBlockquoteNode);
186 moveParagraph(startOfParagraph(visibleStartOfParagraph), endOfParagraph(visibleEndOfParagraph), positionBeforeNode(placeholder.get()), true);
189 // FIXME: We should merge this function with ApplyBlockElementCommand::formatSelection
190 void IndentOutdentCommand::outdentRegion(const VisiblePosition& startOfSelection, const VisiblePosition& endOfSelection)
192 VisiblePosition endOfLastParagraph = endOfParagraph(endOfSelection);
194 if (endOfParagraph(startOfSelection) == endOfLastParagraph) {
199 Position originalSelectionEnd = endingSelection().end();
200 VisiblePosition endOfCurrentParagraph = endOfParagraph(startOfSelection);
201 VisiblePosition endAfterSelection = endOfParagraph(endOfParagraph(endOfSelection).next());
203 while (endOfCurrentParagraph != endAfterSelection) {
204 VisiblePosition endOfNextParagraph = endOfParagraph(endOfCurrentParagraph.next());
205 if (endOfCurrentParagraph == endOfLastParagraph)
206 setEndingSelection(VisibleSelection(originalSelectionEnd, DOWNSTREAM));
208 setEndingSelection(endOfCurrentParagraph);
212 // outdentParagraph could move more than one paragraph if the paragraph
213 // is in a list item. As a result, endAfterSelection and endOfNextParagraph
214 // could refer to positions no longer in the document.
215 if (endAfterSelection.isNotNull() && !endAfterSelection.deepEquivalent().anchorNode()->inDocument())
218 if (endOfNextParagraph.isNotNull() && !endOfNextParagraph.deepEquivalent().anchorNode()->inDocument()) {
219 endOfCurrentParagraph = endingSelection().end();
220 endOfNextParagraph = endOfParagraph(endOfCurrentParagraph.next());
222 endOfCurrentParagraph = endOfNextParagraph;
226 void IndentOutdentCommand::formatSelection(const VisiblePosition& startOfSelection, const VisiblePosition& endOfSelection)
228 if (m_typeOfAction == Indent)
229 ApplyBlockElementCommand::formatSelection(startOfSelection, endOfSelection);
231 outdentRegion(startOfSelection, endOfSelection);
234 void IndentOutdentCommand::formatRange(const Position& start, const Position& end, const Position&, RefPtr<Element>& blockquoteForNextIndent)
236 if (tryIndentingAsListItem(start, end))
237 blockquoteForNextIndent = 0;
239 indentIntoBlockquote(start, end, blockquoteForNextIndent);