| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright (C) 2005, 2006 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 "core/editing/commands/MoveSelectionCommand.h" | |
| 27 | |
| 28 #include "core/dom/DocumentFragment.h" | |
| 29 #include "core/editing/commands/ReplaceSelectionCommand.h" | |
| 30 | |
| 31 namespace blink { | |
| 32 | |
| 33 MoveSelectionCommand::MoveSelectionCommand(DocumentFragment* fragment, | |
| 34 const Position& position, | |
| 35 bool smartInsert, | |
| 36 bool smartDelete) | |
| 37 : CompositeEditCommand(*position.document()), | |
| 38 m_fragment(fragment), | |
| 39 m_position(position), | |
| 40 m_smartInsert(smartInsert), | |
| 41 m_smartDelete(smartDelete) { | |
| 42 DCHECK(m_fragment); | |
| 43 } | |
| 44 | |
| 45 void MoveSelectionCommand::doApply(EditingState* editingState) { | |
| 46 DCHECK(endingSelection().isNonOrphanedRange()); | |
| 47 | |
| 48 Position pos = m_position; | |
| 49 if (pos.isNull()) | |
| 50 return; | |
| 51 | |
| 52 // Update the position otherwise it may become invalid after the selection is | |
| 53 // deleted. | |
| 54 Position selectionEnd = endingSelection().end(); | |
| 55 if (pos.isOffsetInAnchor() && selectionEnd.isOffsetInAnchor() && | |
| 56 selectionEnd.computeContainerNode() == pos.computeContainerNode() && | |
| 57 selectionEnd.offsetInContainerNode() < pos.offsetInContainerNode()) { | |
| 58 pos = Position( | |
| 59 pos.computeContainerNode(), | |
| 60 pos.offsetInContainerNode() - selectionEnd.offsetInContainerNode()); | |
| 61 | |
| 62 Position selectionStart = endingSelection().start(); | |
| 63 if (selectionStart.isOffsetInAnchor() && | |
| 64 selectionStart.computeContainerNode() == pos.computeContainerNode()) | |
| 65 pos = Position( | |
| 66 pos.computeContainerNode(), | |
| 67 pos.offsetInContainerNode() + selectionStart.offsetInContainerNode()); | |
| 68 } | |
| 69 | |
| 70 deleteSelection(editingState, m_smartDelete); | |
| 71 if (editingState->isAborted()) | |
| 72 return; | |
| 73 | |
| 74 // If the node for the destination has been removed as a result of the | |
| 75 // deletion, set the destination to the ending point after the deletion. | |
| 76 // Fixes: <rdar://problem/3910425> REGRESSION (Mail): Crash in | |
| 77 // ReplaceSelectionCommand; selection is empty, leading to null deref | |
| 78 if (!pos.isConnected()) | |
| 79 pos = endingSelection().start(); | |
| 80 | |
| 81 cleanupAfterDeletion(editingState, createVisiblePositionDeprecated(pos)); | |
| 82 if (editingState->isAborted()) | |
| 83 return; | |
| 84 | |
| 85 setEndingSelection(createVisibleSelectionDeprecated( | |
| 86 pos, endingSelection().affinity(), endingSelection().isDirectional())); | |
| 87 if (!pos.isConnected()) { | |
| 88 // Document was modified out from under us. | |
| 89 return; | |
| 90 } | |
| 91 ReplaceSelectionCommand::CommandOptions options = | |
| 92 ReplaceSelectionCommand::SelectReplacement | | |
| 93 ReplaceSelectionCommand::PreventNesting; | |
| 94 if (m_smartInsert) | |
| 95 options |= ReplaceSelectionCommand::SmartReplace; | |
| 96 applyCommandToComposite( | |
| 97 ReplaceSelectionCommand::create(document(), m_fragment, options), | |
| 98 editingState); | |
| 99 } | |
| 100 | |
| 101 InputEvent::InputType MoveSelectionCommand::inputType() const { | |
| 102 return InputEvent::InputType::None; | |
| 103 } | |
| 104 | |
| 105 DEFINE_TRACE(MoveSelectionCommand) { | |
| 106 visitor->trace(m_fragment); | |
| 107 visitor->trace(m_position); | |
| 108 CompositeEditCommand::trace(visitor); | |
| 109 } | |
| 110 | |
| 111 } // namespace blink | |
| OLD | NEW |