| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2005 Apple Computer, Inc. All rights reserved. | 2 * Copyright (C) 2005 Apple Computer, Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
| 6 * are met: | 6 * are met: |
| 7 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
| 8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
| 9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
| 10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
| (...skipping 30 matching lines...) Expand all Loading... |
| 41 , m_text(text) | 41 , m_text(text) |
| 42 , m_selectInsertedText(selectInsertedText) | 42 , m_selectInsertedText(selectInsertedText) |
| 43 , m_rebalanceType(rebalanceType) | 43 , m_rebalanceType(rebalanceType) |
| 44 { | 44 { |
| 45 } | 45 } |
| 46 | 46 |
| 47 Position InsertTextCommand::positionInsideTextNode(const Position& p, EditingSta
te* editingState) | 47 Position InsertTextCommand::positionInsideTextNode(const Position& p, EditingSta
te* editingState) |
| 48 { | 48 { |
| 49 Position pos = p; | 49 Position pos = p; |
| 50 if (isTabHTMLSpanElementTextNode(pos.anchorNode())) { | 50 if (isTabHTMLSpanElementTextNode(pos.anchorNode())) { |
| 51 RefPtrWillBeRawPtr<Text> textNode = document().createEditingTextNode("")
; | 51 RawPtr<Text> textNode = document().createEditingTextNode(""); |
| 52 insertNodeAtTabSpanPosition(textNode.get(), pos, editingState); | 52 insertNodeAtTabSpanPosition(textNode.get(), pos, editingState); |
| 53 if (editingState->isAborted()) | 53 if (editingState->isAborted()) |
| 54 return Position(); | 54 return Position(); |
| 55 return firstPositionInNode(textNode.get()); | 55 return firstPositionInNode(textNode.get()); |
| 56 } | 56 } |
| 57 | 57 |
| 58 // Prepare for text input by looking at the specified position. | 58 // Prepare for text input by looking at the specified position. |
| 59 // It may be necessary to insert a text node to receive characters. | 59 // It may be necessary to insert a text node to receive characters. |
| 60 if (!pos.computeContainerNode()->isTextNode()) { | 60 if (!pos.computeContainerNode()->isTextNode()) { |
| 61 RefPtrWillBeRawPtr<Text> textNode = document().createEditingTextNode("")
; | 61 RawPtr<Text> textNode = document().createEditingTextNode(""); |
| 62 insertNodeAt(textNode.get(), pos, editingState); | 62 insertNodeAt(textNode.get(), pos, editingState); |
| 63 if (editingState->isAborted()) | 63 if (editingState->isAborted()) |
| 64 return Position(); | 64 return Position(); |
| 65 return firstPositionInNode(textNode.get()); | 65 return firstPositionInNode(textNode.get()); |
| 66 } | 66 } |
| 67 | 67 |
| 68 return pos; | 68 return pos; |
| 69 } | 69 } |
| 70 | 70 |
| 71 void InsertTextCommand::setEndingSelectionWithoutValidation(const Position& star
tPosition, const Position& endPosition) | 71 void InsertTextCommand::setEndingSelectionWithoutValidation(const Position& star
tPosition, const Position& endPosition) |
| (...skipping 27 matching lines...) Expand all Loading... |
| 99 setEndingSelection(VisibleSelection(endingSelection().visibleEnd(), endi
ngSelection().isDirectional())); | 99 setEndingSelection(VisibleSelection(endingSelection().visibleEnd(), endi
ngSelection().isDirectional())); |
| 100 | 100 |
| 101 return true; | 101 return true; |
| 102 } | 102 } |
| 103 | 103 |
| 104 bool InsertTextCommand::performOverwrite(const String& text, bool selectInserted
Text) | 104 bool InsertTextCommand::performOverwrite(const String& text, bool selectInserted
Text) |
| 105 { | 105 { |
| 106 Position start = endingSelection().start(); | 106 Position start = endingSelection().start(); |
| 107 if (start.isNull() || !start.isOffsetInAnchor() || !start.computeContainerNo
de()->isTextNode()) | 107 if (start.isNull() || !start.isOffsetInAnchor() || !start.computeContainerNo
de()->isTextNode()) |
| 108 return false; | 108 return false; |
| 109 RefPtrWillBeRawPtr<Text> textNode = toText(start.computeContainerNode()); | 109 RawPtr<Text> textNode = toText(start.computeContainerNode()); |
| 110 if (!textNode) | 110 if (!textNode) |
| 111 return false; | 111 return false; |
| 112 | 112 |
| 113 unsigned count = std::min(text.length(), textNode->length() - start.offsetIn
ContainerNode()); | 113 unsigned count = std::min(text.length(), textNode->length() - start.offsetIn
ContainerNode()); |
| 114 if (!count) | 114 if (!count) |
| 115 return false; | 115 return false; |
| 116 | 116 |
| 117 replaceTextInNode(textNode, start.offsetInContainerNode(), count, text); | 117 replaceTextInNode(textNode, start.offsetInContainerNode(), count, text); |
| 118 | 118 |
| 119 Position endPosition = Position(textNode.release(), start.offsetInContainerN
ode() + text.length()); | 119 Position endPosition = Position(textNode.release(), start.offsetInContainerN
ode() + text.length()); |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 203 } else { | 203 } else { |
| 204 // Make sure the document is set up to receive m_text | 204 // Make sure the document is set up to receive m_text |
| 205 startPosition = positionInsideTextNode(startPosition, editingState); | 205 startPosition = positionInsideTextNode(startPosition, editingState); |
| 206 if (editingState->isAborted()) | 206 if (editingState->isAborted()) |
| 207 return; | 207 return; |
| 208 ASSERT(startPosition.isOffsetInAnchor()); | 208 ASSERT(startPosition.isOffsetInAnchor()); |
| 209 ASSERT(startPosition.computeContainerNode()); | 209 ASSERT(startPosition.computeContainerNode()); |
| 210 ASSERT(startPosition.computeContainerNode()->isTextNode()); | 210 ASSERT(startPosition.computeContainerNode()->isTextNode()); |
| 211 if (placeholder.isNotNull()) | 211 if (placeholder.isNotNull()) |
| 212 removePlaceholderAt(placeholder); | 212 removePlaceholderAt(placeholder); |
| 213 RefPtrWillBeRawPtr<Text> textNode = toText(startPosition.computeContaine
rNode()); | 213 RawPtr<Text> textNode = toText(startPosition.computeContainerNode()); |
| 214 const unsigned offset = startPosition.offsetInContainerNode(); | 214 const unsigned offset = startPosition.offsetInContainerNode(); |
| 215 | 215 |
| 216 insertTextIntoNode(textNode, offset, m_text); | 216 insertTextIntoNode(textNode, offset, m_text); |
| 217 endPosition = Position(textNode, offset + m_text.length()); | 217 endPosition = Position(textNode, offset + m_text.length()); |
| 218 | 218 |
| 219 if (m_rebalanceType == RebalanceLeadingAndTrailingWhitespaces) { | 219 if (m_rebalanceType == RebalanceLeadingAndTrailingWhitespaces) { |
| 220 // The insertion may require adjusting adjacent whitespace, if it is
present. | 220 // The insertion may require adjusting adjacent whitespace, if it is
present. |
| 221 rebalanceWhitespaceAt(endPosition); | 221 rebalanceWhitespaceAt(endPosition); |
| 222 // Rebalancing on both sides isn't necessary if we've inserted only
spaces. | 222 // Rebalancing on both sides isn't necessary if we've inserted only
spaces. |
| 223 if (!shouldRebalanceLeadingWhitespaceFor(m_text)) | 223 if (!shouldRebalanceLeadingWhitespaceFor(m_text)) |
| 224 rebalanceWhitespaceAt(startPosition); | 224 rebalanceWhitespaceAt(startPosition); |
| 225 } else { | 225 } else { |
| 226 ASSERT(m_rebalanceType == RebalanceAllWhitespaces); | 226 ASSERT(m_rebalanceType == RebalanceAllWhitespaces); |
| 227 if (canRebalance(startPosition) && canRebalance(endPosition)) | 227 if (canRebalance(startPosition) && canRebalance(endPosition)) |
| 228 rebalanceWhitespaceOnTextSubstring(textNode, startPosition.offse
tInContainerNode(), endPosition.offsetInContainerNode()); | 228 rebalanceWhitespaceOnTextSubstring(textNode, startPosition.offse
tInContainerNode(), endPosition.offsetInContainerNode()); |
| 229 } | 229 } |
| 230 } | 230 } |
| 231 | 231 |
| 232 setEndingSelectionWithoutValidation(startPosition, endPosition); | 232 setEndingSelectionWithoutValidation(startPosition, endPosition); |
| 233 | 233 |
| 234 // Handle the case where there is a typing style. | 234 // Handle the case where there is a typing style. |
| 235 if (RefPtrWillBeRawPtr<EditingStyle> typingStyle = document().frame()->selec
tion().typingStyle()) { | 235 if (RawPtr<EditingStyle> typingStyle = document().frame()->selection().typin
gStyle()) { |
| 236 typingStyle->prepareToApplyAt(endPosition, EditingStyle::PreserveWriting
Direction); | 236 typingStyle->prepareToApplyAt(endPosition, EditingStyle::PreserveWriting
Direction); |
| 237 if (!typingStyle->isEmpty()) { | 237 if (!typingStyle->isEmpty()) { |
| 238 applyStyle(typingStyle.get(), editingState); | 238 applyStyle(typingStyle.get(), editingState); |
| 239 if (editingState->isAborted()) | 239 if (editingState->isAborted()) |
| 240 return; | 240 return; |
| 241 } | 241 } |
| 242 } | 242 } |
| 243 | 243 |
| 244 if (!m_selectInsertedText) | 244 if (!m_selectInsertedText) |
| 245 setEndingSelection(VisibleSelection(endingSelection().end(), endingSelec
tion().affinity(), endingSelection().isDirectional())); | 245 setEndingSelection(VisibleSelection(endingSelection().end(), endingSelec
tion().affinity(), endingSelection().isDirectional())); |
| 246 } | 246 } |
| 247 | 247 |
| 248 Position InsertTextCommand::insertTab(const Position& pos, EditingState* editing
State) | 248 Position InsertTextCommand::insertTab(const Position& pos, EditingState* editing
State) |
| 249 { | 249 { |
| 250 Position insertPos = createVisiblePosition(pos).deepEquivalent(); | 250 Position insertPos = createVisiblePosition(pos).deepEquivalent(); |
| 251 if (insertPos.isNull()) | 251 if (insertPos.isNull()) |
| 252 return pos; | 252 return pos; |
| 253 | 253 |
| 254 Node* node = insertPos.computeContainerNode(); | 254 Node* node = insertPos.computeContainerNode(); |
| 255 unsigned offset = node->isTextNode() ? insertPos.offsetInContainerNode() : 0
; | 255 unsigned offset = node->isTextNode() ? insertPos.offsetInContainerNode() : 0
; |
| 256 | 256 |
| 257 // keep tabs coalesced in tab span | 257 // keep tabs coalesced in tab span |
| 258 if (isTabHTMLSpanElementTextNode(node)) { | 258 if (isTabHTMLSpanElementTextNode(node)) { |
| 259 RefPtrWillBeRawPtr<Text> textNode = toText(node); | 259 RawPtr<Text> textNode = toText(node); |
| 260 insertTextIntoNode(textNode, offset, "\t"); | 260 insertTextIntoNode(textNode, offset, "\t"); |
| 261 return Position(textNode.release(), offset + 1); | 261 return Position(textNode.release(), offset + 1); |
| 262 } | 262 } |
| 263 | 263 |
| 264 // create new tab span | 264 // create new tab span |
| 265 RefPtrWillBeRawPtr<HTMLSpanElement> spanElement = createTabSpanElement(docum
ent()); | 265 RawPtr<HTMLSpanElement> spanElement = createTabSpanElement(document()); |
| 266 | 266 |
| 267 // place it | 267 // place it |
| 268 if (!node->isTextNode()) { | 268 if (!node->isTextNode()) { |
| 269 insertNodeAt(spanElement.get(), insertPos, editingState); | 269 insertNodeAt(spanElement.get(), insertPos, editingState); |
| 270 } else { | 270 } else { |
| 271 RefPtrWillBeRawPtr<Text> textNode = toText(node); | 271 RawPtr<Text> textNode = toText(node); |
| 272 if (offset >= textNode->length()) { | 272 if (offset >= textNode->length()) { |
| 273 insertNodeAfter(spanElement, textNode.release(), editingState); | 273 insertNodeAfter(spanElement, textNode.release(), editingState); |
| 274 } else { | 274 } else { |
| 275 // split node to make room for the span | 275 // split node to make room for the span |
| 276 // NOTE: splitTextNode uses textNode for the | 276 // NOTE: splitTextNode uses textNode for the |
| 277 // second node in the split, so we need to | 277 // second node in the split, so we need to |
| 278 // insert the span before it. | 278 // insert the span before it. |
| 279 if (offset > 0) | 279 if (offset > 0) |
| 280 splitTextNode(textNode, offset); | 280 splitTextNode(textNode, offset); |
| 281 insertNodeBefore(spanElement, textNode.release(), editingState); | 281 insertNodeBefore(spanElement, textNode.release(), editingState); |
| 282 } | 282 } |
| 283 } | 283 } |
| 284 if (editingState->isAborted()) | 284 if (editingState->isAborted()) |
| 285 return Position(); | 285 return Position(); |
| 286 | 286 |
| 287 // return the position following the new tab | 287 // return the position following the new tab |
| 288 return lastPositionInNode(spanElement.get()); | 288 return lastPositionInNode(spanElement.get()); |
| 289 } | 289 } |
| 290 | 290 |
| 291 } // namespace blink | 291 } // namespace blink |
| OLD | NEW |