2008-11-04 Anders Carlsson <andersca@apple.com>
[webkit/qt.git] / WebCore / editing / InsertLineBreakCommand.cpp
blobd6b4f56888dc8e2236cd5e2ffb30c62f4a9839ad
1 /*
2 * Copyright (C) 2005, 2006 Apple Computer, 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
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.
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.
26 #include "config.h"
27 #include "InsertLineBreakCommand.h"
29 #include "CSSMutableStyleDeclaration.h"
30 #include "Document.h"
31 #include "Element.h"
32 #include "Frame.h"
33 #include "Text.h"
34 #include "VisiblePosition.h"
35 #include "Range.h"
36 #include "htmlediting.h"
37 #include "HTMLNames.h"
38 #include "visible_units.h"
40 namespace WebCore {
42 using namespace HTMLNames;
44 InsertLineBreakCommand::InsertLineBreakCommand(Document* document)
45 : CompositeEditCommand(document)
49 bool InsertLineBreakCommand::preservesTypingStyle() const
51 return true;
54 void InsertLineBreakCommand::insertNodeAfterPosition(Node *node, const Position &pos)
56 // Insert the BR after the caret position. In the case the
57 // position is a block, do an append. We don't want to insert
58 // the BR *after* the block.
59 Node *cb = pos.node()->enclosingBlockFlowElement();
60 if (cb == pos.node())
61 appendNode(node, cb);
62 else
63 insertNodeAfter(node, pos.node());
66 void InsertLineBreakCommand::insertNodeBeforePosition(Node *node, const Position &pos)
68 // Insert the BR after the caret position. In the case the
69 // position is a block, do an append. We don't want to insert
70 // the BR *before* the block.
71 Node *cb = pos.node()->enclosingBlockFlowElement();
72 if (cb == pos.node())
73 appendNode(node, cb);
74 else
75 insertNodeBefore(node, pos.node());
78 // Whether we should insert a break element or a '\n'.
79 bool InsertLineBreakCommand::shouldUseBreakElement(const Position& insertionPos)
81 // An editing position like [input, 0] actually refers to the position before
82 // the input element, and in that case we need to check the input element's
83 // parent's renderer.
84 Position p(rangeCompliantEquivalent(insertionPos));
85 return p.node()->renderer() && !p.node()->renderer()->style()->preserveNewline();
88 void InsertLineBreakCommand::doApply()
90 deleteSelection();
91 Selection selection = endingSelection();
92 if (selection.isNone())
93 return;
95 VisiblePosition caret(selection.visibleStart());
96 Position pos(caret.deepEquivalent());
98 pos = positionAvoidingSpecialElementBoundary(pos);
100 pos = positionOutsideTabSpan(pos);
102 RefPtr<Node> nodeToInsert;
103 if (shouldUseBreakElement(pos))
104 nodeToInsert = createBreakElement(document());
105 else
106 nodeToInsert = document()->createTextNode("\n");
108 // FIXME: Need to merge text nodes when inserting just after or before text.
110 if (isEndOfParagraph(caret) && !lineBreakExistsAtPosition(caret)) {
111 bool needExtraLineBreak = !pos.node()->hasTagName(hrTag) && !pos.node()->hasTagName(tableTag);
113 insertNodeAt(nodeToInsert.get(), pos);
115 if (needExtraLineBreak)
116 insertNodeBefore(nodeToInsert->cloneNode(false).get(), nodeToInsert.get());
118 VisiblePosition endingPosition(Position(nodeToInsert.get(), 0));
119 setEndingSelection(Selection(endingPosition));
120 } else if (pos.offset() <= caretMinOffset(pos.node())) {
121 insertNodeAt(nodeToInsert.get(), pos);
123 // Insert an extra br or '\n' if the just inserted one collapsed.
124 if (!isStartOfParagraph(VisiblePosition(Position(nodeToInsert.get(), 0))))
125 insertNodeBefore(nodeToInsert->cloneNode(false).get(), nodeToInsert.get());
127 setEndingSelection(Selection(positionAfterNode(nodeToInsert.get()), DOWNSTREAM));
128 } else if (pos.offset() >= caretMaxOffset(pos.node())) {
129 insertNodeAt(nodeToInsert.get(), pos);
130 setEndingSelection(Selection(positionAfterNode(nodeToInsert.get()), DOWNSTREAM));
131 } else {
132 // Split a text node
133 ASSERT(pos.node()->isTextNode());
135 // Do the split
136 ExceptionCode ec = 0;
137 Text *textNode = static_cast<Text *>(pos.node());
138 RefPtr<Text> textBeforeNode = document()->createTextNode(textNode->substringData(0, selection.start().offset(), ec));
139 deleteTextFromNode(textNode, 0, pos.offset());
140 insertNodeBefore(textBeforeNode.get(), textNode);
141 insertNodeBefore(nodeToInsert.get(), textNode);
142 Position endingPosition = Position(textNode, 0);
144 // Handle whitespace that occurs after the split
145 updateLayout();
146 if (!endingPosition.isRenderedCharacter()) {
147 Position positionBeforeTextNode(positionBeforeNode(textNode));
148 // Clear out all whitespace and insert one non-breaking space
149 deleteInsignificantTextDownstream(endingPosition);
150 ASSERT(!textNode->renderer() || textNode->renderer()->style()->collapseWhiteSpace());
151 // Deleting insignificant whitespace will remove textNode if it contains nothing but insignificant whitespace.
152 if (textNode->inDocument())
153 insertTextIntoNode(textNode, 0, nonBreakingSpaceString());
154 else {
155 RefPtr<Text> nbspNode = document()->createTextNode(nonBreakingSpaceString());
156 insertNodeAt(nbspNode.get(), positionBeforeTextNode);
157 endingPosition = Position(nbspNode.get(), 0);
161 setEndingSelection(Selection(endingPosition, DOWNSTREAM));
164 // Handle the case where there is a typing style.
166 CSSMutableStyleDeclaration* typingStyle = document()->frame()->typingStyle();
168 if (typingStyle && typingStyle->length() > 0) {
169 // Apply the typing style to the inserted line break, so that if the selection
170 // leaves and then comes back, new input will have the right style.
171 // FIXME: We shouldn't always apply the typing style to the line break here,
172 // see <rdar://problem/5794462>.
173 applyStyle(typingStyle, Position(nodeToInsert.get(), 0),
174 Position(nodeToInsert.get(), maxDeepOffset(nodeToInsert.get())));
175 // Even though this applyStyle operates on a Range, it still sets an endingSelection().
176 // It tries to set a Selection around the content it operated on. So, that Selection
177 // will either (a) select the line break we inserted, or it will (b) be a caret just
178 // before the line break (if the line break is at the end of a block it isn't selectable).
179 // So, this next call sets the endingSelection() to a caret just after the line break
180 // that we inserted, or just before it if it's at the end of a block.
181 setEndingSelection(endingSelection().visibleEnd());
184 rebalanceWhitespace();