1/*
2 * Copyright (C) 2005, 2006 Apple 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 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 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 "MoveSelectionCommand.h"
28
29#include "DeleteSelectionCommand.h"
30#include "DocumentFragment.h"
31#include "ReplaceSelectionCommand.h"
32
33namespace WebCore {
34
35MoveSelectionCommand::MoveSelectionCommand(Ref<DocumentFragment>&& fragment, const Position& position, bool smartInsert, bool smartDelete)
36 : CompositeEditCommand(position.anchorNode()->document())
37 , m_fragment(WTFMove(fragment))
38 , m_position(position)
39 , m_smartInsert(smartInsert)
40 , m_smartDelete(smartDelete)
41{
42}
43
44void MoveSelectionCommand::doApply()
45{
46 ASSERT(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 deleted.
53 Position selectionEnd = endingSelection().end();
54 if (pos.anchorType() == Position::PositionIsOffsetInAnchor && selectionEnd.anchorType() == Position::PositionIsOffsetInAnchor
55 && selectionEnd.containerNode() == pos.containerNode() && selectionEnd.offsetInContainerNode() < pos.offsetInContainerNode()) {
56 pos.moveToOffset(pos.offsetInContainerNode() - selectionEnd.offsetInContainerNode());
57
58 Position selectionStart = endingSelection().start();
59 if (selectionStart.anchorType() == Position::PositionIsOffsetInAnchor && selectionStart.containerNode() == pos.containerNode())
60 pos.moveToOffset(pos.offsetInContainerNode() + selectionStart.offsetInContainerNode());
61 }
62
63 {
64 auto deleteSelection = DeleteSelectionCommand::create(document(), m_smartDelete, true, false, true, true, EditAction::DeleteByDrag);
65 deleteSelection->setParent(this);
66 deleteSelection->apply();
67 m_commands.append(WTFMove(deleteSelection));
68 }
69
70 // If the node for the destination has been removed as a result of the deletion,
71 // set the destination to the ending point after the deletion.
72 // Fixes: <rdar://problem/3910425> REGRESSION (Mail): Crash in ReplaceSelectionCommand;
73 // selection is empty, leading to null deref
74 if (!pos.anchorNode()->isConnected())
75 pos = endingSelection().start();
76
77 cleanupAfterDeletion(pos);
78
79 setEndingSelection(VisibleSelection(pos, endingSelection().affinity(), endingSelection().isDirectional()));
80 setStartingSelection(endingSelection());
81 if (!pos.anchorNode()->isConnected()) {
82 // Document was modified out from under us.
83 return;
84 }
85 OptionSet<ReplaceSelectionCommand::CommandOption> options { ReplaceSelectionCommand::SelectReplacement, ReplaceSelectionCommand::PreventNesting };
86 if (m_smartInsert)
87 options.add(ReplaceSelectionCommand::SmartReplace);
88
89 {
90 auto replaceSelection = ReplaceSelectionCommand::create(document(), WTFMove(m_fragment), options, EditAction::InsertFromDrop);
91 replaceSelection->setParent(this);
92 replaceSelection->apply();
93 m_commands.append(WTFMove(replaceSelection));
94 }
95}
96
97EditAction MoveSelectionCommand::editingAction() const
98{
99 return EditAction::DeleteByDrag;
100}
101
102} // namespace WebCore
103