| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright (C) 2005 Apple Computer, 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 (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. | |
| 24 */ | |
| 25 | |
| 26 #include "config.h" | |
| 27 #include "core/editing/InsertTextCommand.h" | |
| 28 | |
| 29 #include "core/dom/Document.h" | |
| 30 #include "core/dom/Element.h" | |
| 31 #include "core/dom/Text.h" | |
| 32 #include "core/editing/EditingUtilities.h" | |
| 33 #include "core/editing/Editor.h" | |
| 34 #include "core/editing/VisibleUnits.h" | |
| 35 #include "core/frame/LocalFrame.h" | |
| 36 #include "core/html/HTMLSpanElement.h" | |
| 37 | |
| 38 namespace blink { | |
| 39 | |
| 40 InsertTextCommand::InsertTextCommand(Document& document, const String& text, boo
l selectInsertedText, RebalanceType rebalanceType) | |
| 41 : CompositeEditCommand(document) | |
| 42 , m_text(text) | |
| 43 , m_selectInsertedText(selectInsertedText) | |
| 44 , m_rebalanceType(rebalanceType) | |
| 45 { | |
| 46 } | |
| 47 | |
| 48 Position InsertTextCommand::positionInsideTextNode(const Position& p) | |
| 49 { | |
| 50 Position pos = p; | |
| 51 if (isTabHTMLSpanElementTextNode(pos.anchorNode())) { | |
| 52 RefPtrWillBeRawPtr<Text> textNode = document().createEditingTextNode("")
; | |
| 53 insertNodeAtTabSpanPosition(textNode.get(), pos); | |
| 54 return firstPositionInNode(textNode.get()); | |
| 55 } | |
| 56 | |
| 57 // Prepare for text input by looking at the specified position. | |
| 58 // It may be necessary to insert a text node to receive characters. | |
| 59 if (!pos.computeContainerNode()->isTextNode()) { | |
| 60 RefPtrWillBeRawPtr<Text> textNode = document().createEditingTextNode("")
; | |
| 61 insertNodeAt(textNode.get(), pos); | |
| 62 return firstPositionInNode(textNode.get()); | |
| 63 } | |
| 64 | |
| 65 return pos; | |
| 66 } | |
| 67 | |
| 68 void InsertTextCommand::setEndingSelectionWithoutValidation(const Position& star
tPosition, const Position& endPosition) | |
| 69 { | |
| 70 // We could have inserted a part of composed character sequence, | |
| 71 // so we are basically treating ending selection as a range to avoid validat
ion. | |
| 72 // <http://bugs.webkit.org/show_bug.cgi?id=15781> | |
| 73 VisibleSelection forcedEndingSelection; | |
| 74 forcedEndingSelection.setWithoutValidation(startPosition, endPosition); | |
| 75 forcedEndingSelection.setIsDirectional(endingSelection().isDirectional()); | |
| 76 setEndingSelection(forcedEndingSelection); | |
| 77 } | |
| 78 | |
| 79 // This avoids the expense of a full fledged delete operation, and avoids a layo
ut that typically results | |
| 80 // from text removal. | |
| 81 bool InsertTextCommand::performTrivialReplace(const String& text, bool selectIns
ertedText) | |
| 82 { | |
| 83 if (!endingSelection().isRange()) | |
| 84 return false; | |
| 85 | |
| 86 if (text.contains('\t') || text.contains(' ') || text.contains('\n')) | |
| 87 return false; | |
| 88 | |
| 89 Position start = endingSelection().start(); | |
| 90 Position endPosition = replaceSelectedTextInNode(text); | |
| 91 if (endPosition.isNull()) | |
| 92 return false; | |
| 93 | |
| 94 setEndingSelectionWithoutValidation(start, endPosition); | |
| 95 if (!selectInsertedText) | |
| 96 setEndingSelection(VisibleSelection(endingSelection().visibleEnd(), endi
ngSelection().isDirectional())); | |
| 97 | |
| 98 return true; | |
| 99 } | |
| 100 | |
| 101 bool InsertTextCommand::performOverwrite(const String& text, bool selectInserted
Text) | |
| 102 { | |
| 103 Position start = endingSelection().start(); | |
| 104 if (start.isNull() || !start.isOffsetInAnchor() || !start.computeContainerNo
de()->isTextNode()) | |
| 105 return false; | |
| 106 RefPtrWillBeRawPtr<Text> textNode = toText(start.computeContainerNode()); | |
| 107 if (!textNode) | |
| 108 return false; | |
| 109 | |
| 110 unsigned count = std::min(text.length(), textNode->length() - start.offsetIn
ContainerNode()); | |
| 111 if (!count) | |
| 112 return false; | |
| 113 | |
| 114 replaceTextInNode(textNode, start.offsetInContainerNode(), count, text); | |
| 115 | |
| 116 Position endPosition = Position(textNode.release(), start.offsetInContainerN
ode() + text.length()); | |
| 117 setEndingSelectionWithoutValidation(start, endPosition); | |
| 118 if (!selectInsertedText) | |
| 119 setEndingSelection(VisibleSelection(endingSelection().visibleEnd(), endi
ngSelection().isDirectional())); | |
| 120 | |
| 121 return true; | |
| 122 } | |
| 123 | |
| 124 void InsertTextCommand::doApply() | |
| 125 { | |
| 126 ASSERT(m_text.find('\n') == kNotFound); | |
| 127 | |
| 128 if (!endingSelection().isNonOrphanedCaretOrRange()) | |
| 129 return; | |
| 130 | |
| 131 // Delete the current selection. | |
| 132 // FIXME: This delete operation blows away the typing style. | |
| 133 if (endingSelection().isRange()) { | |
| 134 if (performTrivialReplace(m_text, m_selectInsertedText)) | |
| 135 return; | |
| 136 bool endOfSelectionWasAtStartOfBlock = isStartOfBlock(endingSelection().
visibleEnd()); | |
| 137 deleteSelection(false, true, false, false); | |
| 138 // deleteSelection eventually makes a new endingSelection out of a Posit
ion. If that Position doesn't have | |
| 139 // a layoutObject (e.g. it is on a <frameset> in the DOM), the VisibleSe
lection cannot be canonicalized to | |
| 140 // anything other than NoSelection. The rest of this function requires a
real endingSelection, so bail out. | |
| 141 if (endingSelection().isNone()) | |
| 142 return; | |
| 143 if (endOfSelectionWasAtStartOfBlock) { | |
| 144 if (EditingStyle* typingStyle = document().frame()->selection().typi
ngStyle()) | |
| 145 typingStyle->removeBlockProperties(); | |
| 146 } | |
| 147 } else if (document().frame()->editor().isOverwriteModeEnabled()) { | |
| 148 if (performOverwrite(m_text, m_selectInsertedText)) | |
| 149 return; | |
| 150 } | |
| 151 | |
| 152 Position startPosition(endingSelection().start()); | |
| 153 | |
| 154 Position placeholder; | |
| 155 // We want to remove preserved newlines and brs that will collapse (and thus
become unnecessary) when content | |
| 156 // is inserted just before them. | |
| 157 // FIXME: We shouldn't really have to do this, but removing placeholders is
a workaround for 9661. | |
| 158 // If the caret is just before a placeholder, downstream will normalize the
caret to it. | |
| 159 Position downstream(startPosition.downstream()); | |
| 160 if (lineBreakExistsAtPosition(downstream)) { | |
| 161 // FIXME: This doesn't handle placeholders at the end of anonymous block
s. | |
| 162 VisiblePosition caret(startPosition); | |
| 163 if (isEndOfBlock(caret) && isStartOfParagraph(caret)) | |
| 164 placeholder = downstream; | |
| 165 // Don't remove the placeholder yet, otherwise the block we're inserting
into would collapse before | |
| 166 // we get a chance to insert into it. We check for a placeholder now, t
hough, because doing so requires | |
| 167 // the creation of a VisiblePosition, and if we did that post-insertion
it would force a layout. | |
| 168 } | |
| 169 | |
| 170 // Insert the character at the leftmost candidate. | |
| 171 startPosition = startPosition.upstream(); | |
| 172 | |
| 173 // It is possible for the node that contains startPosition to contain only u
nrendered whitespace, | |
| 174 // and so deleteInsignificantText could remove it. Save the position before
the node in case that happens. | |
| 175 ASSERT(startPosition.computeContainerNode()); | |
| 176 Position positionBeforeStartNode(positionInParentBeforeNode(*startPosition.c
omputeContainerNode())); | |
| 177 deleteInsignificantText(startPosition, startPosition.downstream()); | |
| 178 if (!startPosition.inDocument()) | |
| 179 startPosition = positionBeforeStartNode; | |
| 180 if (!startPosition.isCandidate()) | |
| 181 startPosition = startPosition.downstream(); | |
| 182 | |
| 183 startPosition = positionAvoidingSpecialElementBoundary(startPosition); | |
| 184 | |
| 185 Position endPosition; | |
| 186 | |
| 187 if (m_text == "\t") { | |
| 188 endPosition = insertTab(startPosition); | |
| 189 startPosition = endPosition.previous(); | |
| 190 if (placeholder.isNotNull()) | |
| 191 removePlaceholderAt(placeholder); | |
| 192 } else { | |
| 193 // Make sure the document is set up to receive m_text | |
| 194 startPosition = positionInsideTextNode(startPosition); | |
| 195 ASSERT(startPosition.isOffsetInAnchor()); | |
| 196 ASSERT(startPosition.computeContainerNode()); | |
| 197 ASSERT(startPosition.computeContainerNode()->isTextNode()); | |
| 198 if (placeholder.isNotNull()) | |
| 199 removePlaceholderAt(placeholder); | |
| 200 RefPtrWillBeRawPtr<Text> textNode = toText(startPosition.computeContaine
rNode()); | |
| 201 const unsigned offset = startPosition.offsetInContainerNode(); | |
| 202 | |
| 203 insertTextIntoNode(textNode, offset, m_text); | |
| 204 endPosition = Position(textNode, offset + m_text.length()); | |
| 205 | |
| 206 if (m_rebalanceType == RebalanceLeadingAndTrailingWhitespaces) { | |
| 207 // The insertion may require adjusting adjacent whitespace, if it is
present. | |
| 208 rebalanceWhitespaceAt(endPosition); | |
| 209 // Rebalancing on both sides isn't necessary if we've inserted only
spaces. | |
| 210 if (!shouldRebalanceLeadingWhitespaceFor(m_text)) | |
| 211 rebalanceWhitespaceAt(startPosition); | |
| 212 } else { | |
| 213 ASSERT(m_rebalanceType == RebalanceAllWhitespaces); | |
| 214 if (canRebalance(startPosition) && canRebalance(endPosition)) | |
| 215 rebalanceWhitespaceOnTextSubstring(textNode, startPosition.offse
tInContainerNode(), endPosition.offsetInContainerNode()); | |
| 216 } | |
| 217 } | |
| 218 | |
| 219 setEndingSelectionWithoutValidation(startPosition, endPosition); | |
| 220 | |
| 221 // Handle the case where there is a typing style. | |
| 222 if (RefPtrWillBeRawPtr<EditingStyle> typingStyle = document().frame()->selec
tion().typingStyle()) { | |
| 223 typingStyle->prepareToApplyAt(endPosition, EditingStyle::PreserveWriting
Direction); | |
| 224 if (!typingStyle->isEmpty()) | |
| 225 applyStyle(typingStyle.get()); | |
| 226 } | |
| 227 | |
| 228 if (!m_selectInsertedText) | |
| 229 setEndingSelection(VisibleSelection(endingSelection().end(), endingSelec
tion().affinity(), endingSelection().isDirectional())); | |
| 230 } | |
| 231 | |
| 232 Position InsertTextCommand::insertTab(const Position& pos) | |
| 233 { | |
| 234 Position insertPos = VisiblePosition(pos, DOWNSTREAM).deepEquivalent(); | |
| 235 if (insertPos.isNull()) | |
| 236 return pos; | |
| 237 | |
| 238 Node* node = insertPos.computeContainerNode(); | |
| 239 unsigned offset = node->isTextNode() ? insertPos.offsetInContainerNode() : 0
; | |
| 240 | |
| 241 // keep tabs coalesced in tab span | |
| 242 if (isTabHTMLSpanElementTextNode(node)) { | |
| 243 RefPtrWillBeRawPtr<Text> textNode = toText(node); | |
| 244 insertTextIntoNode(textNode, offset, "\t"); | |
| 245 return Position(textNode.release(), offset + 1); | |
| 246 } | |
| 247 | |
| 248 // create new tab span | |
| 249 RefPtrWillBeRawPtr<HTMLSpanElement> spanElement = createTabSpanElement(docum
ent()); | |
| 250 | |
| 251 // place it | |
| 252 if (!node->isTextNode()) { | |
| 253 insertNodeAt(spanElement.get(), insertPos); | |
| 254 } else { | |
| 255 RefPtrWillBeRawPtr<Text> textNode = toText(node); | |
| 256 if (offset >= textNode->length()) | |
| 257 insertNodeAfter(spanElement, textNode.release()); | |
| 258 else { | |
| 259 // split node to make room for the span | |
| 260 // NOTE: splitTextNode uses textNode for the | |
| 261 // second node in the split, so we need to | |
| 262 // insert the span before it. | |
| 263 if (offset > 0) | |
| 264 splitTextNode(textNode, offset); | |
| 265 insertNodeBefore(spanElement, textNode.release()); | |
| 266 } | |
| 267 } | |
| 268 | |
| 269 // return the position following the new tab | |
| 270 return lastPositionInNode(spanElement.get()); | |
| 271 } | |
| 272 | |
| 273 } | |
| OLD | NEW |