1/*
2 * Copyright (C) 2006, 2008 Apple Inc. All rights reserved.
3 * Copyright (C) 2010 Google Inc. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
16 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
17 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
18 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
20 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include "config.h"
28#include "ApplyBlockElementCommand.h"
29
30#include "HTMLElement.h"
31#include "HTMLNames.h"
32#include "RenderElement.h"
33#include "RenderStyle.h"
34#include "Text.h"
35#include "VisibleUnits.h"
36#include "htmlediting.h"
37
38namespace WebCore {
39
40using namespace HTMLNames;
41
42ApplyBlockElementCommand::ApplyBlockElementCommand(Document& document, const QualifiedName& tagName, const AtomicString& inlineStyle)
43    : CompositeEditCommand(document)
44    , m_tagName(tagName)
45    , m_inlineStyle(inlineStyle)
46{
47}
48
49ApplyBlockElementCommand::ApplyBlockElementCommand(Document& document, const QualifiedName& tagName)
50    : CompositeEditCommand(document)
51    , m_tagName(tagName)
52{
53}
54
55void ApplyBlockElementCommand::doApply()
56{
57    if (!endingSelection().rootEditableElement())
58        return;
59
60    VisiblePosition visibleEnd = endingSelection().visibleEnd();
61    VisiblePosition visibleStart = endingSelection().visibleStart();
62    if (visibleStart.isNull() || visibleStart.isOrphan() || visibleEnd.isNull() || visibleEnd.isOrphan())
63        return;
64
65    // When a selection ends at the start of a paragraph, we rarely paint
66    // the selection gap before that paragraph, because there often is no gap.
67    // In a case like this, it's not obvious to the user that the selection
68    // ends "inside" that paragraph, so it would be confusing if Indent/Outdent
69    // operated on that paragraph.
70    // FIXME: We paint the gap before some paragraphs that are indented with left
71    // margin/padding, but not others.  We should make the gap painting more consistent and
72    // then use a left margin/padding rule here.
73    if (visibleEnd != visibleStart && isStartOfParagraph(visibleEnd))
74        setEndingSelection(VisibleSelection(visibleStart, visibleEnd.previous(CannotCrossEditingBoundary), endingSelection().isDirectional()));
75
76    VisibleSelection selection = selectionForParagraphIteration(endingSelection());
77    VisiblePosition startOfSelection = selection.visibleStart();
78    VisiblePosition endOfSelection = selection.visibleEnd();
79    ASSERT(!startOfSelection.isNull());
80    ASSERT(!endOfSelection.isNull());
81    RefPtr<ContainerNode> startScope;
82    int startIndex = indexForVisiblePosition(startOfSelection, startScope);
83    RefPtr<ContainerNode> endScope;
84    int endIndex = indexForVisiblePosition(endOfSelection, endScope);
85
86    formatSelection(startOfSelection, endOfSelection);
87
88    document().updateLayoutIgnorePendingStylesheets();
89
90    ASSERT(startScope == endScope);
91    ASSERT(startIndex >= 0);
92    ASSERT(startIndex <= endIndex);
93    if (startScope == endScope && startIndex >= 0 && startIndex <= endIndex) {
94        VisiblePosition start(visiblePositionForIndex(startIndex, startScope.get()));
95        VisiblePosition end(visiblePositionForIndex(endIndex, endScope.get()));
96        if (start.isNotNull() && end.isNotNull())
97            setEndingSelection(VisibleSelection(start, end, endingSelection().isDirectional()));
98    }
99}
100
101void ApplyBlockElementCommand::formatSelection(const VisiblePosition& startOfSelection, const VisiblePosition& endOfSelection)
102{
103    // Special case empty unsplittable elements because there's nothing to split
104    // and there's nothing to move.
105    Position start = startOfSelection.deepEquivalent().downstream();
106    if (isAtUnsplittableElement(start)) {
107        RefPtr<Element> blockquote = createBlockElement();
108        insertNodeAt(blockquote, start);
109        RefPtr<Element> placeholder = createBreakElement(document());
110        appendNode(placeholder, blockquote);
111        setEndingSelection(VisibleSelection(positionBeforeNode(placeholder.get()), DOWNSTREAM, endingSelection().isDirectional()));
112        return;
113    }
114
115    RefPtr<Element> blockquoteForNextIndent;
116    VisiblePosition endOfCurrentParagraph = endOfParagraph(startOfSelection);
117    VisiblePosition endAfterSelection = endOfParagraph(endOfParagraph(endOfSelection).next());
118    m_endOfLastParagraph = endOfParagraph(endOfSelection).deepEquivalent();
119
120    bool atEnd = false;
121    Position end;
122    while (endOfCurrentParagraph != endAfterSelection && !atEnd) {
123        if (endOfCurrentParagraph.deepEquivalent() == m_endOfLastParagraph)
124            atEnd = true;
125
126        rangeForParagraphSplittingTextNodesIfNeeded(endOfCurrentParagraph, start, end);
127        endOfCurrentParagraph = end;
128
129        // FIXME: endOfParagraph can errornously return a position at the beginning of a block element
130        // when the position passed into endOfParagraph is at the beginning of a block.
131        // Work around this bug here because too much of the existing code depends on the current behavior of endOfParagraph.
132        if (start == end && startOfBlock(start) != endOfBlock(start) && !isEndOfBlock(end) && start == startOfParagraph(endOfBlock(start))) {
133            endOfCurrentParagraph = endOfBlock(end);
134            end = endOfCurrentParagraph.deepEquivalent();
135        }
136
137        Position afterEnd = end.next();
138        Node* enclosingCell = enclosingNodeOfType(start, &isTableCell);
139        VisiblePosition endOfNextParagraph = endOfNextParagrahSplittingTextNodesIfNeeded(endOfCurrentParagraph, start, end);
140
141        formatRange(start, end, m_endOfLastParagraph, blockquoteForNextIndent);
142
143        // Don't put the next paragraph in the blockquote we just created for this paragraph unless
144        // the next paragraph is in the same cell.
145        if (enclosingCell && enclosingCell != enclosingNodeOfType(endOfNextParagraph.deepEquivalent(), &isTableCell))
146            blockquoteForNextIndent = 0;
147
148        // indentIntoBlockquote could move more than one paragraph if the paragraph
149        // is in a list item or a table. As a result, endAfterSelection could refer to a position
150        // no longer in the document.
151        if (endAfterSelection.isNotNull() && !endAfterSelection.deepEquivalent().anchorNode()->inDocument())
152            break;
153        // Sanity check: Make sure our moveParagraph calls didn't remove endOfNextParagraph.deepEquivalent().deprecatedNode()
154        // If somehow we did, return to prevent crashes.
155        if (endOfNextParagraph.isNotNull() && !endOfNextParagraph.deepEquivalent().anchorNode()->inDocument()) {
156            ASSERT_NOT_REACHED();
157            return;
158        }
159        endOfCurrentParagraph = endOfNextParagraph;
160    }
161}
162
163static bool isNewLineAtPosition(const Position& position)
164{
165    Node* textNode = position.containerNode();
166    int offset = position.offsetInContainerNode();
167    if (!textNode || !textNode->isTextNode() || offset < 0 || offset >= textNode->maxCharacterOffset())
168        return false;
169
170    ExceptionCode ec = 0;
171    String textAtPosition = toText(textNode)->substringData(offset, 1, ec);
172    if (ec)
173        return false;
174
175    return textAtPosition[0] == '\n';
176}
177
178RenderStyle* ApplyBlockElementCommand::renderStyleOfEnclosingTextNode(const Position& position)
179{
180    if (position.anchorType() != Position::PositionIsOffsetInAnchor
181        || !position.containerNode()
182        || !position.containerNode()->isTextNode())
183        return 0;
184
185    document().updateStyleIfNeeded();
186
187    RenderObject* renderer = position.containerNode()->renderer();
188    if (!renderer)
189        return 0;
190
191    return &renderer->style();
192}
193
194void ApplyBlockElementCommand::rangeForParagraphSplittingTextNodesIfNeeded(const VisiblePosition& endOfCurrentParagraph, Position& start, Position& end)
195{
196    start = startOfParagraph(endOfCurrentParagraph).deepEquivalent();
197    end = endOfCurrentParagraph.deepEquivalent();
198
199    bool isStartAndEndOnSameNode = false;
200    if (RenderStyle* startStyle = renderStyleOfEnclosingTextNode(start)) {
201        isStartAndEndOnSameNode = renderStyleOfEnclosingTextNode(end) && start.containerNode() == end.containerNode();
202        bool isStartAndEndOfLastParagraphOnSameNode = renderStyleOfEnclosingTextNode(m_endOfLastParagraph) && start.containerNode() == m_endOfLastParagraph.containerNode();
203
204        // Avoid obtanining the start of next paragraph for start
205        if (startStyle->preserveNewline() && isNewLineAtPosition(start) && !isNewLineAtPosition(start.previous()) && start.offsetInContainerNode() > 0)
206            start = startOfParagraph(end.previous()).deepEquivalent();
207
208        // If start is in the middle of a text node, split.
209        if (!startStyle->collapseWhiteSpace() && start.offsetInContainerNode() > 0) {
210            int startOffset = start.offsetInContainerNode();
211            Text* startText = start.containerText();
212            splitTextNode(startText, startOffset);
213            start = firstPositionInNode(startText);
214            if (isStartAndEndOnSameNode) {
215                ASSERT(end.offsetInContainerNode() >= startOffset);
216                end = Position(startText, end.offsetInContainerNode() - startOffset);
217            }
218            if (isStartAndEndOfLastParagraphOnSameNode) {
219                ASSERT(m_endOfLastParagraph.offsetInContainerNode() >= startOffset);
220                m_endOfLastParagraph = Position(startText, m_endOfLastParagraph.offsetInContainerNode() - startOffset);
221            }
222        }
223    }
224
225    if (RenderStyle* endStyle = renderStyleOfEnclosingTextNode(end)) {
226        bool isEndAndEndOfLastParagraphOnSameNode = renderStyleOfEnclosingTextNode(m_endOfLastParagraph) && end.deprecatedNode() == m_endOfLastParagraph.deprecatedNode();
227        // Include \n at the end of line if we're at an empty paragraph
228        if (endStyle->preserveNewline() && start == end && end.offsetInContainerNode() < end.containerNode()->maxCharacterOffset()) {
229            int endOffset = end.offsetInContainerNode();
230            if (!isNewLineAtPosition(end.previous()) && isNewLineAtPosition(end))
231                end = Position(end.containerText(), endOffset + 1);
232            if (isEndAndEndOfLastParagraphOnSameNode && end.offsetInContainerNode() >= m_endOfLastParagraph.offsetInContainerNode())
233                m_endOfLastParagraph = end;
234        }
235
236        // If end is in the middle of a text node, split.
237        if (!endStyle->collapseWhiteSpace() && end.offsetInContainerNode() && end.offsetInContainerNode() < end.containerNode()->maxCharacterOffset()) {
238            RefPtr<Text> endContainer = end.containerText();
239            splitTextNode(endContainer, end.offsetInContainerNode());
240            if (isStartAndEndOnSameNode)
241                start = firstPositionInOrBeforeNode(endContainer->previousSibling());
242            if (isEndAndEndOfLastParagraphOnSameNode) {
243                if (m_endOfLastParagraph.offsetInContainerNode() == end.offsetInContainerNode())
244                    m_endOfLastParagraph = lastPositionInOrAfterNode(endContainer->previousSibling());
245                else
246                    m_endOfLastParagraph = Position(endContainer, m_endOfLastParagraph.offsetInContainerNode() - end.offsetInContainerNode());
247            }
248            end = lastPositionInNode(endContainer->previousSibling());
249        }
250    }
251}
252
253VisiblePosition ApplyBlockElementCommand::endOfNextParagrahSplittingTextNodesIfNeeded(VisiblePosition& endOfCurrentParagraph, Position& start, Position& end)
254{
255    VisiblePosition endOfNextParagraph = endOfParagraph(endOfCurrentParagraph.next());
256    Position position = endOfNextParagraph.deepEquivalent();
257    RenderStyle* style = renderStyleOfEnclosingTextNode(position);
258    if (!style)
259        return endOfNextParagraph;
260
261    RefPtr<Text> text = position.containerText();
262    if (!style->preserveNewline() || !position.offsetInContainerNode() || !isNewLineAtPosition(firstPositionInNode(text.get())))
263        return endOfNextParagraph;
264
265    // \n at the beginning of the text node immediately following the current paragraph is trimmed by moveParagraphWithClones.
266    // If endOfNextParagraph was pointing at this same text node, endOfNextParagraph will be shifted by one paragraph.
267    // Avoid this by splitting "\n"
268    splitTextNode(text, 1);
269
270    if (text == start.containerNode() && text->previousSibling() && text->previousSibling()->isTextNode()) {
271        ASSERT(start.offsetInContainerNode() < position.offsetInContainerNode());
272        start = Position(toText(text->previousSibling()), start.offsetInContainerNode());
273    }
274    if (text == end.containerNode() && text->previousSibling() && text->previousSibling()->isTextNode()) {
275        ASSERT(end.offsetInContainerNode() < position.offsetInContainerNode());
276        end = Position(toText(text->previousSibling()), end.offsetInContainerNode());
277    }
278    if (text == m_endOfLastParagraph.containerNode()) {
279        if (m_endOfLastParagraph.offsetInContainerNode() < position.offsetInContainerNode()) {
280            // We can only fix endOfLastParagraph if the previous node was still text and hasn't been modified by script.
281            if (text->previousSibling()->isTextNode()
282                && static_cast<unsigned>(m_endOfLastParagraph.offsetInContainerNode()) <= toText(text->previousSibling())->length())
283                m_endOfLastParagraph = Position(toText(text->previousSibling()), m_endOfLastParagraph.offsetInContainerNode());
284        } else
285            m_endOfLastParagraph = Position(text.get(), m_endOfLastParagraph.offsetInContainerNode() - 1);
286    }
287
288    return Position(text.get(), position.offsetInContainerNode() - 1);
289}
290
291PassRefPtr<Element> ApplyBlockElementCommand::createBlockElement()
292{
293    RefPtr<Element> element = createHTMLElement(document(), m_tagName);
294    if (m_inlineStyle.length())
295        element->setAttribute(styleAttr, m_inlineStyle);
296    return element.release();
297}
298
299}
300