IndentOutdentCommand.cpp 11 KB
Newer Older
lweintraub's avatar
lweintraub committed
1
/*
darin@apple.com's avatar
darin@apple.com committed
2
 * Copyright (C) 2006, 2008 Apple Inc. All rights reserved.
lweintraub's avatar
lweintraub committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (IndentOutdentCommandINCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 */

#include "config.h"
#include "IndentOutdentCommand.h"
28

lweintraub's avatar
lweintraub committed
29
#include "Document.h"
30 31
#include "Element.h"
#include "HTMLBlockquoteElement.h"
lweintraub's avatar
lweintraub committed
32 33
#include "HTMLNames.h"
#include "InsertLineBreakCommand.h"
34
#include "InsertListCommand.h"
lweintraub's avatar
lweintraub committed
35
#include "Range.h"
36
#include "RenderObject.h"
lweintraub's avatar
lweintraub committed
37
#include "SplitElementCommand.h"
38
#include "Text.h"
justing's avatar
justing committed
39
#include "TextIterator.h"
40
#include "htmlediting.h"
lweintraub's avatar
lweintraub committed
41
#include "visible_units.h"
42
#include <wtf/StdLibExtras.h>
lweintraub's avatar
lweintraub committed
43 44 45 46 47

namespace WebCore {

using namespace HTMLNames;

justing's avatar
justing committed
48
static bool isListOrIndentBlockquote(const Node* node)
justing's avatar
justing committed
49
{
jparent@chromium.org's avatar
jparent@chromium.org committed
50
    return node && (node->hasTagName(ulTag) || node->hasTagName(olTag) || node->hasTagName(blockquoteTag));
justing's avatar
justing committed
51 52 53
}

IndentOutdentCommand::IndentOutdentCommand(Document* document, EIndentType typeOfAction, int marginInPixels)
54 55 56
    : ApplyBlockElementCommand(document, blockquoteTag, "webkit-indent-blockquote", "margin: 0 0 0 40px; border: none; padding: 0px;")
    , m_typeOfAction(typeOfAction)
    , m_marginInPixels(marginInPixels)
57 58
{
}
justing's avatar
justing committed
59

60
bool IndentOutdentCommand::tryIndentingAsListItem(const Position& start, const Position& end)
bfulgham@webkit.org's avatar
bfulgham@webkit.org committed
61 62
{
    // If our selection is not inside a list, bail out.
63
    Node* lastNodeInSelectedParagraph = start.deprecatedNode();
bfulgham@webkit.org's avatar
bfulgham@webkit.org committed
64 65 66 67
    RefPtr<Element> listNode = enclosingList(lastNodeInSelectedParagraph);
    if (!listNode)
        return false;

68
    // 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.
69 70
    Element* selectedListItem = static_cast<Element*>(enclosingBlock(lastNodeInSelectedParagraph));

rniwa@webkit.org's avatar
rniwa@webkit.org committed
71 72 73
    // FIXME: we need to deal with the case where there is no li (malformed HTML)
    if (!selectedListItem->hasTagName(liTag))
        return false;
74
    
bfulgham@webkit.org's avatar
bfulgham@webkit.org committed
75 76 77 78 79 80
    // FIXME: previousElementSibling does not ignore non-rendered content like <span></span>.  Should we?
    Element* previousList = selectedListItem->previousElementSibling();
    Element* nextList = selectedListItem->nextElementSibling();

    RefPtr<Element> newList = document()->createElement(listNode->tagQName(), false);
    insertNodeBefore(newList, selectedListItem);
81

82
    moveParagraphWithClones(start, end, newList.get(), selectedListItem);
bfulgham@webkit.org's avatar
bfulgham@webkit.org committed
83 84 85 86 87 88 89 90

    if (canMergeLists(previousList, newList.get()))
        mergeIdenticalElements(previousList, newList);
    if (canMergeLists(newList.get(), nextList))
        mergeIdenticalElements(newList, nextList);

    return true;
}
91 92

void IndentOutdentCommand::indentIntoBlockquote(const Position& start, const Position& end, RefPtr<Element>& targetBlockquote)
bfulgham@webkit.org's avatar
bfulgham@webkit.org committed
93
{
94
    Node* enclosingCell = enclosingNodeOfType(start, &isTableCell);
95 96 97
    Node* nodeToSplitTo;
    if (enclosingCell)
        nodeToSplitTo = enclosingCell;
98 99
    else if (enclosingList(start.deprecatedNode()))
        nodeToSplitTo = enclosingBlock(start.deprecatedNode());
100 101 102
    else
        nodeToSplitTo = editableRootForPosition(start);

103 104 105
    if (!nodeToSplitTo)
        return;

106
    RefPtr<Node> outerBlock = (start.deprecatedNode() == nodeToSplitTo) ? start.deprecatedNode() : splitTreeToNode(start.deprecatedNode(), nodeToSplitTo);
107

bfulgham@webkit.org's avatar
bfulgham@webkit.org committed
108
    if (!targetBlockquote) {
109
        // Create a new blockquote and insert it as a child of the root editable element. We accomplish
bfulgham@webkit.org's avatar
bfulgham@webkit.org committed
110
        // this by splitting all parents of the current paragraph up to that point.
111
        targetBlockquote = createBlockElement();
112
        insertNodeBefore(targetBlockquote, outerBlock);
bfulgham@webkit.org's avatar
bfulgham@webkit.org committed
113 114
    }

115
    moveParagraphWithClones(start, end, targetBlockquote.get(), outerBlock.get());
116 117
}

lweintraub's avatar
lweintraub committed
118 119 120 121 122
void IndentOutdentCommand::outdentParagraph()
{
    VisiblePosition visibleStartOfParagraph = startOfParagraph(endingSelection().visibleStart());
    VisiblePosition visibleEndOfParagraph = endOfParagraph(visibleStartOfParagraph);

justin.garcia@apple.com's avatar
justin.garcia@apple.com committed
123
    Node* enclosingNode = enclosingNodeOfType(visibleStartOfParagraph.deepEquivalent(), &isListOrIndentBlockquote);
124
    if (!enclosingNode || !enclosingNode->parentNode()->rendererIsEditable()) // We can't outdent if there is no place to go!
lweintraub's avatar
lweintraub committed
125 126
        return;

justing's avatar
justing committed
127
    // Use InsertListCommand to remove the selection from the list
lweintraub's avatar
lweintraub committed
128
    if (enclosingNode->hasTagName(olTag)) {
justin.garcia@apple.com's avatar
justin.garcia@apple.com committed
129
        applyCommandToComposite(InsertListCommand::create(document(), InsertListCommand::OrderedList));
justing's avatar
justing committed
130
        return;        
darin@apple.com's avatar
darin@apple.com committed
131 132
    }
    if (enclosingNode->hasTagName(ulTag)) {
justin.garcia@apple.com's avatar
justin.garcia@apple.com committed
133
        applyCommandToComposite(InsertListCommand::create(document(), InsertListCommand::UnorderedList));
lweintraub's avatar
lweintraub committed
134 135
        return;
    }
justing's avatar
justing committed
136
    
jparent@chromium.org's avatar
jparent@chromium.org committed
137
    // The selection is inside a blockquote i.e. enclosingNode is a blockquote
138
    VisiblePosition positionInEnclosingBlock = VisiblePosition(firstPositionInNode(enclosingNode));
139 140 141
    // If the blockquote is inline, the start of the enclosing block coincides with
    // positionInEnclosingBlock.
    VisiblePosition startOfEnclosingBlock = (enclosingNode->renderer() && enclosingNode->renderer()->isInline()) ? positionInEnclosingBlock : startOfBlock(positionInEnclosingBlock);
142
    VisiblePosition lastPositionInEnclosingBlock = VisiblePosition(lastPositionInNode(enclosingNode));
kmccullough@apple.com's avatar
kmccullough@apple.com committed
143
    VisiblePosition endOfEnclosingBlock = endOfBlock(lastPositionInEnclosingBlock);
lweintraub's avatar
lweintraub committed
144 145 146
    if (visibleStartOfParagraph == startOfEnclosingBlock &&
        visibleEndOfParagraph == endOfEnclosingBlock) {
        // The blockquote doesn't contain anything outside the paragraph, so it can be totally removed.
kmccullough@apple.com's avatar
kmccullough@apple.com committed
147
        Node* splitPoint = enclosingNode->nextSibling();
lweintraub's avatar
lweintraub committed
148
        removeNodePreservingChildren(enclosingNode);
kmccullough@apple.com's avatar
kmccullough@apple.com committed
149 150 151
        // outdentRegion() assumes it is operating on the first paragraph of an enclosing blockquote, but if there are multiply nested blockquotes and we've
        // just removed one, then this assumption isn't true. By splitting the next containing blockquote after this node, we keep this assumption true
        if (splitPoint) {
152
            if (ContainerNode* splitPointParent = splitPoint->parentNode()) {
jparent@chromium.org's avatar
jparent@chromium.org committed
153 154
                if (splitPointParent->hasTagName(blockquoteTag)
                    && !splitPoint->hasTagName(blockquoteTag)
155
                    && splitPointParent->parentNode()->rendererIsEditable()) // We can't outdent if there is no place to go!
kmccullough@apple.com's avatar
kmccullough@apple.com committed
156 157 158 159
                    splitElement(static_cast<Element*>(splitPointParent), splitPoint);
            }
        }
        
bdakin's avatar
bdakin committed
160
        updateLayout();
justing's avatar
justing committed
161 162
        visibleStartOfParagraph = VisiblePosition(visibleStartOfParagraph.deepEquivalent());
        visibleEndOfParagraph = VisiblePosition(visibleEndOfParagraph.deepEquivalent());
justing's avatar
justing committed
163
        if (visibleStartOfParagraph.isNotNull() && !isStartOfParagraph(visibleStartOfParagraph))
164
            insertNodeAt(createBreakElement(document()), visibleStartOfParagraph.deepEquivalent());
justing's avatar
justing committed
165
        if (visibleEndOfParagraph.isNotNull() && !isEndOfParagraph(visibleEndOfParagraph))
166
            insertNodeAt(createBreakElement(document()), visibleEndOfParagraph.deepEquivalent());
kmccullough@apple.com's avatar
kmccullough@apple.com committed
167

lweintraub's avatar
lweintraub committed
168 169
        return;
    }
170
    Node* enclosingBlockFlow = enclosingBlock(visibleStartOfParagraph.deepEquivalent().deprecatedNode());
darin@apple.com's avatar
darin@apple.com committed
171
    RefPtr<Node> splitBlockquoteNode = enclosingNode;
lweintraub's avatar
lweintraub committed
172
    if (enclosingBlockFlow != enclosingNode)
jparent@chromium.org's avatar
jparent@chromium.org committed
173 174 175
        splitBlockquoteNode = splitTreeToNode(enclosingBlockFlow, enclosingNode, true);
    else {
        // We split the blockquote at where we start outdenting.
176
        splitElement(static_cast<Element*>(enclosingNode), visibleStartOfParagraph.deepEquivalent().deprecatedNode());
jparent@chromium.org's avatar
jparent@chromium.org committed
177
    }
lweintraub's avatar
lweintraub committed
178
    RefPtr<Node> placeholder = createBreakElement(document());
179
    insertNodeBefore(placeholder, splitBlockquoteNode);
180
    moveParagraph(startOfParagraph(visibleStartOfParagraph), endOfParagraph(visibleEndOfParagraph), positionBeforeNode(placeholder.get()), true);
lweintraub's avatar
lweintraub committed
181 182
}

183
// FIXME: We should merge this function with ApplyBlockElementCommand::formatSelection
184
void IndentOutdentCommand::outdentRegion(const VisiblePosition& startOfSelection, const VisiblePosition& endOfSelection)
lweintraub's avatar
lweintraub committed
185 186 187 188 189 190 191
{
    VisiblePosition endOfLastParagraph = endOfParagraph(endOfSelection);

    if (endOfParagraph(startOfSelection) == endOfLastParagraph) {
        outdentParagraph();
        return;
    }
192
    
lweintraub's avatar
lweintraub committed
193
    Position originalSelectionEnd = endingSelection().end();
194
    VisiblePosition endOfCurrentParagraph = endOfParagraph(startOfSelection);
lweintraub's avatar
lweintraub committed
195
    VisiblePosition endAfterSelection = endOfParagraph(endOfParagraph(endOfSelection).next());
196

lweintraub's avatar
lweintraub committed
197 198 199
    while (endOfCurrentParagraph != endAfterSelection) {
        VisiblePosition endOfNextParagraph = endOfParagraph(endOfCurrentParagraph.next());
        if (endOfCurrentParagraph == endOfLastParagraph)
200
            setEndingSelection(VisibleSelection(originalSelectionEnd, DOWNSTREAM));
lweintraub's avatar
lweintraub committed
201 202
        else
            setEndingSelection(endOfCurrentParagraph);
203
        
lweintraub's avatar
lweintraub committed
204
        outdentParagraph();
205 206 207 208
        
        // outdentParagraph could move more than one paragraph if the paragraph
        // is in a list item. As a result, endAfterSelection and endOfNextParagraph
        // could refer to positions no longer in the document.
209
        if (endAfterSelection.isNotNull() && !endAfterSelection.deepEquivalent().anchorNode()->inDocument())
210 211
            break;
            
212
        if (endOfNextParagraph.isNotNull() && !endOfNextParagraph.deepEquivalent().anchorNode()->inDocument()) {
213 214 215
            endOfCurrentParagraph = endingSelection().end();
            endOfNextParagraph = endOfParagraph(endOfCurrentParagraph.next());
        }
lweintraub's avatar
lweintraub committed
216 217 218 219
        endOfCurrentParagraph = endOfNextParagraph;
    }
}

220
void IndentOutdentCommand::formatSelection(const VisiblePosition& startOfSelection, const VisiblePosition& endOfSelection)
lweintraub's avatar
lweintraub committed
221 222
{
    if (m_typeOfAction == Indent)
223
        ApplyBlockElementCommand::formatSelection(startOfSelection, endOfSelection);
lweintraub's avatar
lweintraub committed
224
    else
225
        outdentRegion(startOfSelection, endOfSelection);
226
}
227

228
void IndentOutdentCommand::formatRange(const Position& start, const Position& end, const Position&, RefPtr<Element>& blockquoteForNextIndent)
229
{
230
    if (tryIndentingAsListItem(start, end))
231 232
        blockquoteForNextIndent = 0;
    else
233
        indentIntoBlockquote(start, end, blockquoteForNextIndent);
lweintraub's avatar
lweintraub committed
234 235 236
}

}