tizen beta release
[profile/ivi/webkit-efl.git] / Source / WebCore / editing / IndentOutdentCommand.cpp
1 /*
2  * Copyright (C) 2006, 2008 Apple 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 (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. 
24  */
25
26 #include "config.h"
27 #include "IndentOutdentCommand.h"
28
29 #include "Document.h"
30 #include "HTMLElement.h"
31 #include "HTMLNames.h"
32 #include "InsertLineBreakCommand.h"
33 #include "InsertListCommand.h"
34 #include "Range.h"
35 #include "RenderObject.h"
36 #include "SplitElementCommand.h"
37 #include "Text.h"
38 #include "TextIterator.h"
39 #include "htmlediting.h"
40 #include "visible_units.h"
41 #include <wtf/StdLibExtras.h>
42
43 namespace WebCore {
44
45 using namespace HTMLNames;
46
47 static bool isListOrIndentBlockquote(const Node* node)
48 {
49     return node && (node->hasTagName(ulTag) || node->hasTagName(olTag) || node->hasTagName(blockquoteTag));
50 }
51
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)
56 {
57 }
58
59 bool IndentOutdentCommand::tryIndentingAsListItem(const Position& start, const Position& end)
60 {
61     // If our selection is not inside a list, bail out.
62     Node* lastNodeInSelectedParagraph = start.deprecatedNode();
63     RefPtr<Element> listNode = enclosingList(lastNodeInSelectedParagraph);
64     if (!listNode)
65         return false;
66
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));
69
70     // FIXME: we need to deal with the case where there is no li (malformed HTML)
71     if (!selectedListItem->hasTagName(liTag))
72         return false;
73     
74     // FIXME: previousElementSibling does not ignore non-rendered content like <span></span>.  Should we?
75     Element* previousList = selectedListItem->previousElementSibling();
76     Element* nextList = selectedListItem->nextElementSibling();
77
78     RefPtr<Element> newList = document()->createElement(listNode->tagQName(), false);
79     insertNodeBefore(newList, selectedListItem);
80
81     moveParagraphWithClones(start, end, newList.get(), selectedListItem);
82
83     if (canMergeLists(previousList, newList.get()))
84         mergeIdenticalElements(previousList, newList);
85     if (canMergeLists(newList.get(), nextList))
86         mergeIdenticalElements(newList, nextList);
87
88     return true;
89 }
90
91 void IndentOutdentCommand::indentIntoBlockquote(const Position& start, const Position& end, RefPtr<Element>& targetBlockquote)
92 {
93     Node* enclosingCell = enclosingNodeOfType(start, &isTableCell);
94     Node* nodeToSplitTo;
95     if (enclosingCell)
96         nodeToSplitTo = enclosingCell;
97     else if (enclosingList(start.containerNode()))
98         nodeToSplitTo = enclosingBlock(start.containerNode());
99     else
100         nodeToSplitTo = editableRootForPosition(start);
101
102     if (!nodeToSplitTo)
103         return;
104
105     RefPtr<Node> nodeAfterStart = start.computeNodeAfterPosition();
106     RefPtr<Node> outerBlock = (start.containerNode() == nodeToSplitTo) ? start.containerNode() : splitTreeToNode(start.containerNode(), nodeToSplitTo);
107
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);
115         else
116             insertNodeBefore(targetBlockquote, outerBlock);
117         startOfContents = positionAfterNode(targetBlockquote.get());
118     }
119
120     moveParagraphWithClones(startOfContents, end, targetBlockquote.get(), outerBlock.get());
121 }
122
123 void IndentOutdentCommand::outdentParagraph()
124 {
125     VisiblePosition visibleStartOfParagraph = startOfParagraph(endingSelection().visibleStart());
126     VisiblePosition visibleEndOfParagraph = endOfParagraph(visibleStartOfParagraph);
127
128     Node* enclosingNode = enclosingNodeOfType(visibleStartOfParagraph.deepEquivalent(), &isListOrIndentBlockquote);
129     if (!enclosingNode || !enclosingNode->parentNode()->rendererIsEditable()) // We can't outdent if there is no place to go!
130         return;
131
132     // Use InsertListCommand to remove the selection from the list
133     if (enclosingNode->hasTagName(olTag)) {
134         applyCommandToComposite(InsertListCommand::create(document(), InsertListCommand::OrderedList));
135         return;        
136     }
137     if (enclosingNode->hasTagName(ulTag)) {
138         applyCommandToComposite(InsertListCommand::create(document(), InsertListCommand::UnorderedList));
139         return;
140     }
141     
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
156         if (splitPoint) {
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);
162             }
163         }
164         
165         updateLayout();
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());
172
173         return;
174     }
175     Node* enclosingBlockFlow = enclosingBlock(visibleStartOfParagraph.deepEquivalent().deprecatedNode());
176     RefPtr<Node> splitBlockquoteNode = enclosingNode;
177     if (enclosingBlockFlow != enclosingNode)
178         splitBlockquoteNode = splitTreeToNode(enclosingBlockFlow, enclosingNode, true);
179     else {
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());
183     }
184     RefPtr<Node> placeholder = createBreakElement(document());
185     insertNodeBefore(placeholder, splitBlockquoteNode);
186     moveParagraph(startOfParagraph(visibleStartOfParagraph), endOfParagraph(visibleEndOfParagraph), positionBeforeNode(placeholder.get()), true);
187 }
188
189 // FIXME: We should merge this function with ApplyBlockElementCommand::formatSelection
190 void IndentOutdentCommand::outdentRegion(const VisiblePosition& startOfSelection, const VisiblePosition& endOfSelection)
191 {
192     VisiblePosition endOfLastParagraph = endOfParagraph(endOfSelection);
193
194     if (endOfParagraph(startOfSelection) == endOfLastParagraph) {
195         outdentParagraph();
196         return;
197     }
198     
199     Position originalSelectionEnd = endingSelection().end();
200     VisiblePosition endOfCurrentParagraph = endOfParagraph(startOfSelection);
201     VisiblePosition endAfterSelection = endOfParagraph(endOfParagraph(endOfSelection).next());
202
203     while (endOfCurrentParagraph != endAfterSelection) {
204         VisiblePosition endOfNextParagraph = endOfParagraph(endOfCurrentParagraph.next());
205         if (endOfCurrentParagraph == endOfLastParagraph)
206             setEndingSelection(VisibleSelection(originalSelectionEnd, DOWNSTREAM));
207         else
208             setEndingSelection(endOfCurrentParagraph);
209         
210         outdentParagraph();
211         
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())
216             break;
217             
218         if (endOfNextParagraph.isNotNull() && !endOfNextParagraph.deepEquivalent().anchorNode()->inDocument()) {
219             endOfCurrentParagraph = endingSelection().end();
220             endOfNextParagraph = endOfParagraph(endOfCurrentParagraph.next());
221         }
222         endOfCurrentParagraph = endOfNextParagraph;
223     }
224 }
225
226 void IndentOutdentCommand::formatSelection(const VisiblePosition& startOfSelection, const VisiblePosition& endOfSelection)
227 {
228     if (m_typeOfAction == Indent)
229         ApplyBlockElementCommand::formatSelection(startOfSelection, endOfSelection);
230     else
231         outdentRegion(startOfSelection, endOfSelection);
232 }
233
234 void IndentOutdentCommand::formatRange(const Position& start, const Position& end, const Position&, RefPtr<Element>& blockquoteForNextIndent)
235 {
236     if (tryIndentingAsListItem(start, end))
237         blockquoteForNextIndent = 0;
238     else
239         indentIntoBlockquote(start, end, blockquoteForNextIndent);
240 }
241
242 }