| 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) | 47 Position InsertTextCommand::positionInsideTextNode(const Position& p) |
| 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); | 52 insertNodeAtTabSpanPosition(textNode.get(), pos); |
| 53 return firstPositionInNode(textNode.get()); | 53 return firstPositionInNode(textNode.get()); |
| 54 } | 54 } |
| 55 | 55 |
| 56 // Prepare for text input by looking at the specified position. | 56 // Prepare for text input by looking at the specified position. |
| 57 // It may be necessary to insert a text node to receive characters. | 57 // It may be necessary to insert a text node to receive characters. |
| 58 if (!pos.computeContainerNode()->isTextNode()) { | 58 if (!pos.computeContainerNode()->isTextNode()) { |
| 59 RefPtrWillBeRawPtr<Text> textNode = document().createEditingTextNode("")
; | 59 RawPtr<Text> textNode = document().createEditingTextNode(""); |
| 60 insertNodeAt(textNode.get(), pos); | 60 insertNodeAt(textNode.get(), pos); |
| 61 return firstPositionInNode(textNode.get()); | 61 return firstPositionInNode(textNode.get()); |
| 62 } | 62 } |
| 63 | 63 |
| 64 return pos; | 64 return pos; |
| 65 } | 65 } |
| 66 | 66 |
| 67 void InsertTextCommand::setEndingSelectionWithoutValidation(const Position& star
tPosition, const Position& endPosition) | 67 void InsertTextCommand::setEndingSelectionWithoutValidation(const Position& star
tPosition, const Position& endPosition) |
| 68 { | 68 { |
| 69 // We could have inserted a part of composed character sequence, | 69 // We could have inserted a part of composed character sequence, |
| (...skipping 25 matching lines...) Expand all Loading... |
| 95 setEndingSelection(VisibleSelection(endingSelection().visibleEnd(), endi
ngSelection().isDirectional())); | 95 setEndingSelection(VisibleSelection(endingSelection().visibleEnd(), endi
ngSelection().isDirectional())); |
| 96 | 96 |
| 97 return true; | 97 return true; |
| 98 } | 98 } |
| 99 | 99 |
| 100 bool InsertTextCommand::performOverwrite(const String& text, bool selectInserted
Text) | 100 bool InsertTextCommand::performOverwrite(const String& text, bool selectInserted
Text) |
| 101 { | 101 { |
| 102 Position start = endingSelection().start(); | 102 Position start = endingSelection().start(); |
| 103 if (start.isNull() || !start.isOffsetInAnchor() || !start.computeContainerNo
de()->isTextNode()) | 103 if (start.isNull() || !start.isOffsetInAnchor() || !start.computeContainerNo
de()->isTextNode()) |
| 104 return false; | 104 return false; |
| 105 RefPtrWillBeRawPtr<Text> textNode = toText(start.computeContainerNode()); | 105 RawPtr<Text> textNode = toText(start.computeContainerNode()); |
| 106 if (!textNode) | 106 if (!textNode) |
| 107 return false; | 107 return false; |
| 108 | 108 |
| 109 unsigned count = std::min(text.length(), textNode->length() - start.offsetIn
ContainerNode()); | 109 unsigned count = std::min(text.length(), textNode->length() - start.offsetIn
ContainerNode()); |
| 110 if (!count) | 110 if (!count) |
| 111 return false; | 111 return false; |
| 112 | 112 |
| 113 replaceTextInNode(textNode, start.offsetInContainerNode(), count, text); | 113 replaceTextInNode(textNode, start.offsetInContainerNode(), count, text); |
| 114 | 114 |
| 115 Position endPosition = Position(textNode.release(), start.offsetInContainerN
ode() + text.length()); | 115 Position endPosition = Position(textNode.release(), start.offsetInContainerN
ode() + text.length()); |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 191 if (placeholder.isNotNull()) | 191 if (placeholder.isNotNull()) |
| 192 removePlaceholderAt(placeholder); | 192 removePlaceholderAt(placeholder); |
| 193 } else { | 193 } else { |
| 194 // Make sure the document is set up to receive m_text | 194 // Make sure the document is set up to receive m_text |
| 195 startPosition = positionInsideTextNode(startPosition); | 195 startPosition = positionInsideTextNode(startPosition); |
| 196 ASSERT(startPosition.isOffsetInAnchor()); | 196 ASSERT(startPosition.isOffsetInAnchor()); |
| 197 ASSERT(startPosition.computeContainerNode()); | 197 ASSERT(startPosition.computeContainerNode()); |
| 198 ASSERT(startPosition.computeContainerNode()->isTextNode()); | 198 ASSERT(startPosition.computeContainerNode()->isTextNode()); |
| 199 if (placeholder.isNotNull()) | 199 if (placeholder.isNotNull()) |
| 200 removePlaceholderAt(placeholder); | 200 removePlaceholderAt(placeholder); |
| 201 RefPtrWillBeRawPtr<Text> textNode = toText(startPosition.computeContaine
rNode()); | 201 RawPtr<Text> textNode = toText(startPosition.computeContainerNode()); |
| 202 const unsigned offset = startPosition.offsetInContainerNode(); | 202 const unsigned offset = startPosition.offsetInContainerNode(); |
| 203 | 203 |
| 204 insertTextIntoNode(textNode, offset, m_text); | 204 insertTextIntoNode(textNode, offset, m_text); |
| 205 endPosition = Position(textNode, offset + m_text.length()); | 205 endPosition = Position(textNode, offset + m_text.length()); |
| 206 | 206 |
| 207 if (m_rebalanceType == RebalanceLeadingAndTrailingWhitespaces) { | 207 if (m_rebalanceType == RebalanceLeadingAndTrailingWhitespaces) { |
| 208 // The insertion may require adjusting adjacent whitespace, if it is
present. | 208 // The insertion may require adjusting adjacent whitespace, if it is
present. |
| 209 rebalanceWhitespaceAt(endPosition); | 209 rebalanceWhitespaceAt(endPosition); |
| 210 // Rebalancing on both sides isn't necessary if we've inserted only
spaces. | 210 // Rebalancing on both sides isn't necessary if we've inserted only
spaces. |
| 211 if (!shouldRebalanceLeadingWhitespaceFor(m_text)) | 211 if (!shouldRebalanceLeadingWhitespaceFor(m_text)) |
| 212 rebalanceWhitespaceAt(startPosition); | 212 rebalanceWhitespaceAt(startPosition); |
| 213 } else { | 213 } else { |
| 214 ASSERT(m_rebalanceType == RebalanceAllWhitespaces); | 214 ASSERT(m_rebalanceType == RebalanceAllWhitespaces); |
| 215 if (canRebalance(startPosition) && canRebalance(endPosition)) | 215 if (canRebalance(startPosition) && canRebalance(endPosition)) |
| 216 rebalanceWhitespaceOnTextSubstring(textNode, startPosition.offse
tInContainerNode(), endPosition.offsetInContainerNode()); | 216 rebalanceWhitespaceOnTextSubstring(textNode, startPosition.offse
tInContainerNode(), endPosition.offsetInContainerNode()); |
| 217 } | 217 } |
| 218 } | 218 } |
| 219 | 219 |
| 220 setEndingSelectionWithoutValidation(startPosition, endPosition); | 220 setEndingSelectionWithoutValidation(startPosition, endPosition); |
| 221 | 221 |
| 222 // Handle the case where there is a typing style. | 222 // Handle the case where there is a typing style. |
| 223 if (RefPtrWillBeRawPtr<EditingStyle> typingStyle = document().frame()->selec
tion().typingStyle()) { | 223 if (RawPtr<EditingStyle> typingStyle = document().frame()->selection().typin
gStyle()) { |
| 224 typingStyle->prepareToApplyAt(endPosition, EditingStyle::PreserveWriting
Direction); | 224 typingStyle->prepareToApplyAt(endPosition, EditingStyle::PreserveWriting
Direction); |
| 225 if (!typingStyle->isEmpty()) | 225 if (!typingStyle->isEmpty()) |
| 226 applyStyle(typingStyle.get()); | 226 applyStyle(typingStyle.get()); |
| 227 } | 227 } |
| 228 | 228 |
| 229 if (!m_selectInsertedText) | 229 if (!m_selectInsertedText) |
| 230 setEndingSelection(VisibleSelection(endingSelection().end(), endingSelec
tion().affinity(), endingSelection().isDirectional())); | 230 setEndingSelection(VisibleSelection(endingSelection().end(), endingSelec
tion().affinity(), endingSelection().isDirectional())); |
| 231 } | 231 } |
| 232 | 232 |
| 233 Position InsertTextCommand::insertTab(const Position& pos) | 233 Position InsertTextCommand::insertTab(const Position& pos) |
| 234 { | 234 { |
| 235 Position insertPos = createVisiblePosition(pos).deepEquivalent(); | 235 Position insertPos = createVisiblePosition(pos).deepEquivalent(); |
| 236 if (insertPos.isNull()) | 236 if (insertPos.isNull()) |
| 237 return pos; | 237 return pos; |
| 238 | 238 |
| 239 Node* node = insertPos.computeContainerNode(); | 239 Node* node = insertPos.computeContainerNode(); |
| 240 unsigned offset = node->isTextNode() ? insertPos.offsetInContainerNode() : 0
; | 240 unsigned offset = node->isTextNode() ? insertPos.offsetInContainerNode() : 0
; |
| 241 | 241 |
| 242 // keep tabs coalesced in tab span | 242 // keep tabs coalesced in tab span |
| 243 if (isTabHTMLSpanElementTextNode(node)) { | 243 if (isTabHTMLSpanElementTextNode(node)) { |
| 244 RefPtrWillBeRawPtr<Text> textNode = toText(node); | 244 RawPtr<Text> textNode = toText(node); |
| 245 insertTextIntoNode(textNode, offset, "\t"); | 245 insertTextIntoNode(textNode, offset, "\t"); |
| 246 return Position(textNode.release(), offset + 1); | 246 return Position(textNode.release(), offset + 1); |
| 247 } | 247 } |
| 248 | 248 |
| 249 // create new tab span | 249 // create new tab span |
| 250 RefPtrWillBeRawPtr<HTMLSpanElement> spanElement = createTabSpanElement(docum
ent()); | 250 RawPtr<HTMLSpanElement> spanElement = createTabSpanElement(document()); |
| 251 | 251 |
| 252 // place it | 252 // place it |
| 253 if (!node->isTextNode()) { | 253 if (!node->isTextNode()) { |
| 254 insertNodeAt(spanElement.get(), insertPos); | 254 insertNodeAt(spanElement.get(), insertPos); |
| 255 } else { | 255 } else { |
| 256 RefPtrWillBeRawPtr<Text> textNode = toText(node); | 256 RawPtr<Text> textNode = toText(node); |
| 257 if (offset >= textNode->length()) { | 257 if (offset >= textNode->length()) { |
| 258 insertNodeAfter(spanElement, textNode.release()); | 258 insertNodeAfter(spanElement, textNode.release()); |
| 259 } else { | 259 } else { |
| 260 // split node to make room for the span | 260 // split node to make room for the span |
| 261 // NOTE: splitTextNode uses textNode for the | 261 // NOTE: splitTextNode uses textNode for the |
| 262 // second node in the split, so we need to | 262 // second node in the split, so we need to |
| 263 // insert the span before it. | 263 // insert the span before it. |
| 264 if (offset > 0) | 264 if (offset > 0) |
| 265 splitTextNode(textNode, offset); | 265 splitTextNode(textNode, offset); |
| 266 insertNodeBefore(spanElement, textNode.release()); | 266 insertNodeBefore(spanElement, textNode.release()); |
| 267 } | 267 } |
| 268 } | 268 } |
| 269 | 269 |
| 270 // return the position following the new tab | 270 // return the position following the new tab |
| 271 return lastPositionInNode(spanElement.get()); | 271 return lastPositionInNode(spanElement.get()); |
| 272 } | 272 } |
| 273 | 273 |
| 274 } // namespace blink | 274 } // namespace blink |
| OLD | NEW |