Turn avoidIntersectionWithNode into Editor member functions to encapsulate delete...
[WebKit-https.git] / Source / WebCore / editing / EditCommand.cpp
1 /*
2  * Copyright (C) 2005, 2006, 2007 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 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 "EditCommand.h"
28
29 #include "CompositeEditCommand.h"
30 #include "Document.h"
31 #include "Editor.h"
32 #include "Element.h"
33 #include "EventNames.h"
34 #include "Frame.h"
35 #include "FrameSelection.h"
36 #include "NodeTraversal.h"
37 #include "VisiblePosition.h"
38 #include "htmlediting.h"
39
40 namespace WebCore {
41
42 EditCommand::EditCommand(Document* document)
43     : m_document(document)
44     , m_parent(0)
45 {
46     ASSERT(m_document);
47     ASSERT(m_document->frame());
48     setStartingSelection(m_document->frame()->editor()->avoidIntersectionWithDeleteButtonController(m_document->frame()->selection()->selection()));
49     setEndingSelection(m_startingSelection);
50 }
51
52 EditCommand::EditCommand(Document* document, const VisibleSelection& startingSelection, const VisibleSelection& endingSelection)
53     : m_document(document)
54     , m_parent(0)
55 {
56     ASSERT(m_document);
57     ASSERT(m_document->frame());
58     setStartingSelection(startingSelection);
59     setEndingSelection(endingSelection);
60 }
61
62 EditCommand::~EditCommand()
63 {
64 }
65
66 EditAction EditCommand::editingAction() const
67 {
68     return EditActionUnspecified;
69 }
70
71 static inline EditCommandComposition* compositionIfPossible(EditCommand* command)
72 {
73     if (!command->isCompositeEditCommand())
74         return 0;
75     return toCompositeEditCommand(command)->composition();
76 }
77
78 void EditCommand::setStartingSelection(const VisibleSelection& s)
79 {
80     for (EditCommand* cmd = this; ; cmd = cmd->m_parent) {
81         if (EditCommandComposition* composition = compositionIfPossible(cmd)) {
82             ASSERT(cmd->isTopLevelCommand());
83             composition->setStartingSelection(s);
84         }
85         cmd->m_startingSelection = s;
86         if (!cmd->m_parent || cmd->m_parent->isFirstCommand(cmd))
87             break;
88     }
89 }
90
91 void EditCommand::setEndingSelection(const VisibleSelection &s)
92 {
93     for (EditCommand* cmd = this; cmd; cmd = cmd->m_parent) {
94         if (EditCommandComposition* composition = compositionIfPossible(cmd)) {
95             ASSERT(cmd->isTopLevelCommand());
96             composition->setEndingSelection(s);
97         }
98         cmd->m_endingSelection = s;
99     }
100 }
101
102 void EditCommand::setParent(CompositeEditCommand* parent)
103 {
104     ASSERT((parent && !m_parent) || (!parent && m_parent));
105     ASSERT(!parent || !isCompositeEditCommand() || !toCompositeEditCommand(this)->composition());
106     m_parent = parent;
107     if (parent) {
108         m_startingSelection = parent->m_endingSelection;
109         m_endingSelection = parent->m_endingSelection;
110     }
111 }
112
113 void SimpleEditCommand::doReapply()
114 {
115     doApply();
116 }
117
118 #ifndef NDEBUG
119 void SimpleEditCommand::addNodeAndDescendants(Node* startNode, HashSet<Node*>& nodes)
120 {
121     for (Node* node = startNode; node; node = NodeTraversal::next(node, startNode))
122         nodes.add(node);
123 }
124 #endif
125
126 } // namespace WebCore