2 * Copyright (C) 2005 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
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.
27 #include "InsertTextCommand.h"
29 #include "CharacterNames.h"
30 #include "CSSMutableStyleDeclaration.h"
31 #include "CSSComputedStyleDeclaration.h"
34 #include "EditingText.h"
38 #include "HTMLInterchange.h"
39 #include "htmlediting.h"
40 #include "TextIterator.h"
41 #include "TypingCommand.h"
42 #include "visible_units.h"
46 InsertTextCommand::InsertTextCommand(Document
*document
)
47 : CompositeEditCommand(document
), m_charactersAdded(0)
51 void InsertTextCommand::doApply()
55 Position
InsertTextCommand::prepareForTextInsertion(const Position
& p
)
58 // If an anchor was removed and the selection hasn't changed, we restore it.
59 RefPtr
<Node
> anchor
= document()->frame()->editor()->removedAnchor();
61 insertNodeAt(anchor
.get(), pos
);
62 document()->frame()->editor()->setRemovedAnchor(0);
63 pos
= Position(anchor
.get(), 0);
65 // Prepare for text input by looking at the specified position.
66 // It may be necessary to insert a text node to receive characters.
67 if (!pos
.node()->isTextNode()) {
68 RefPtr
<Node
> textNode
= document()->createEditingTextNode("");
69 insertNodeAt(textNode
.get(), pos
);
70 return Position(textNode
.get(), 0);
73 if (isTabSpanTextNode(pos
.node())) {
74 RefPtr
<Node
> textNode
= document()->createEditingTextNode("");
75 insertNodeAtTabSpanPosition(textNode
.get(), pos
);
76 return Position(textNode
.get(), 0);
82 // This avoids the expense of a full fledged delete operation, and avoids a layout that typically results
84 bool InsertTextCommand::performTrivialReplace(const String
& text
, bool selectInsertedText
)
86 if (!endingSelection().isRange())
89 if (text
.contains('\t') || text
.contains(' ') || text
.contains('\n'))
92 Position start
= endingSelection().start();
93 Position end
= endingSelection().end();
95 if (start
.node() != end
.node() || !start
.node()->isTextNode() || isTabSpanTextNode(start
.node()))
98 replaceTextInNode(static_cast<Text
*>(start
.node()), start
.offset(), end
.offset() - start
.offset(), text
);
100 Position
endPosition(start
.node(), start
.offset() + text
.length());
102 // We could have inserted a part of composed character sequence,
103 // so we are basically treating ending selection as a range to avoid validation.
104 // <http://bugs.webkit.org/show_bug.cgi?id=15781>
105 Selection forcedEndingSelection
;
106 forcedEndingSelection
.setWithoutValidation(start
, endPosition
);
107 setEndingSelection(forcedEndingSelection
);
109 if (!selectInsertedText
)
110 setEndingSelection(Selection(endingSelection().visibleEnd()));
115 void InsertTextCommand::input(const String
& originalText
, bool selectInsertedText
)
117 String text
= originalText
;
119 ASSERT(text
.find('\n') == -1);
121 if (endingSelection().isNone())
124 if (RenderObject
* renderer
= endingSelection().start().node()->renderer())
125 if (renderer
->style()->collapseWhiteSpace())
126 // Turn all spaces into non breaking spaces, to make sure that they are treated
127 // literally, and aren't collapsed after insertion. They will be rebalanced
128 // (turned into a sequence of regular and non breaking spaces) below.
129 text
.replace(' ', noBreakSpace
);
131 // Delete the current selection.
132 // FIXME: This delete operation blows away the typing style.
133 if (endingSelection().isRange()) {
134 if (performTrivialReplace(text
, selectInsertedText
))
136 deleteSelection(false, true, true, false);
139 // Insert the character at the leftmost candidate.
140 Position startPosition
= endingSelection().start().upstream();
141 // It is possible for the node that contains startPosition to contain only unrendered whitespace,
142 // and so deleteInsignificantText could remove it. Save the position before the node in case that happens.
143 Position
positionBeforeStartNode(positionBeforeNode(startPosition
.node()));
144 deleteInsignificantText(startPosition
.upstream(), startPosition
.downstream());
145 if (!startPosition
.node()->inDocument())
146 startPosition
= positionBeforeStartNode
;
147 if (!startPosition
.isCandidate())
148 startPosition
= startPosition
.downstream();
150 // FIXME: This typing around anchor behavior doesn't exactly match TextEdit. In TextEdit,
151 // you won't be placed inside a link when typing after it if you've just placed the caret
152 // there with the mouse.
153 startPosition
= positionAvoidingSpecialElementBoundary(startPosition
, false);
155 Position endPosition
;
158 endPosition
= insertTab(startPosition
);
159 startPosition
= endPosition
.previous();
160 removePlaceholderAt(VisiblePosition(startPosition
));
161 m_charactersAdded
+= 1;
163 // Make sure the document is set up to receive text
164 startPosition
= prepareForTextInsertion(startPosition
);
165 removePlaceholderAt(VisiblePosition(startPosition
));
166 Text
*textNode
= static_cast<Text
*>(startPosition
.node());
167 int offset
= startPosition
.offset();
169 insertTextIntoNode(textNode
, offset
, text
);
170 endPosition
= Position(textNode
, offset
+ text
.length());
172 // The insertion may require adjusting adjacent whitespace, if it is present.
173 rebalanceWhitespaceAt(endPosition
);
174 // Rebalancing on both sides isn't necessary if we've inserted a space.
175 if (originalText
!= " ")
176 rebalanceWhitespaceAt(startPosition
);
178 m_charactersAdded
+= text
.length();
181 // We could have inserted a part of composed character sequence,
182 // so we are basically treating ending selection as a range to avoid validation.
183 // <http://bugs.webkit.org/show_bug.cgi?id=15781>
184 Selection forcedEndingSelection
;
185 forcedEndingSelection
.setWithoutValidation(startPosition
, endPosition
);
186 setEndingSelection(forcedEndingSelection
);
188 // Handle the case where there is a typing style.
189 CSSMutableStyleDeclaration
* typingStyle
= document()->frame()->typingStyle();
190 RefPtr
<CSSComputedStyleDeclaration
> endingStyle
= endPosition
.computedStyle();
191 endingStyle
->diff(typingStyle
);
192 if (typingStyle
&& typingStyle
->length() > 0)
193 applyStyle(typingStyle
);
195 if (!selectInsertedText
)
196 setEndingSelection(Selection(endingSelection().end(), endingSelection().affinity()));
199 Position
InsertTextCommand::insertTab(const Position
& pos
)
201 Position insertPos
= VisiblePosition(pos
, DOWNSTREAM
).deepEquivalent();
203 Node
*node
= insertPos
.node();
204 unsigned int offset
= insertPos
.offset();
206 // keep tabs coalesced in tab span
207 if (isTabSpanTextNode(node
)) {
208 insertTextIntoNode(static_cast<Text
*>(node
), offset
, "\t");
209 return Position(node
, offset
+ 1);
212 // create new tab span
213 RefPtr
<Element
> spanNode
= createTabSpanElement(document());
216 if (!node
->isTextNode()) {
217 insertNodeAt(spanNode
.get(), insertPos
);
219 Text
*textNode
= static_cast<Text
*>(node
);
220 if (offset
>= textNode
->length()) {
221 insertNodeAfter(spanNode
.get(), textNode
);
223 // split node to make room for the span
224 // NOTE: splitTextNode uses textNode for the
225 // second node in the split, so we need to
226 // insert the span before it.
228 splitTextNode(textNode
, offset
);
229 insertNodeBefore(spanNode
.get(), textNode
);
233 // return the position following the new tab
234 return Position(spanNode
->lastChild(), caretMaxOffset(spanNode
->lastChild()));
237 bool InsertTextCommand::isInsertTextCommand() const