Use "= default" to denote default constructor or destructor
[WebKit-https.git] / Source / WebCore / editing / EditCommand.cpp
1 /*
2  * Copyright (C) 2005, 2006, 2007, 2013 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 "EditCommand.h"
28
29 #include "AXObjectCache.h"
30 #include "CompositeEditCommand.h"
31 #include "Document.h"
32 #include "Editing.h"
33 #include "Editor.h"
34 #include "Element.h"
35 #include "Frame.h"
36 #include "HTMLInputElement.h"
37 #include "HTMLTextAreaElement.h"
38 #include "NodeTraversal.h"
39
40 namespace WebCore {
41
42 String inputTypeNameForEditingAction(EditAction action)
43 {
44     switch (action) {
45     case EditActionJustify:
46         return ASCIILiteral("formatJustifyFull");
47     case EditActionAlignLeft:
48         return ASCIILiteral("formatJustifyLeft");
49     case EditActionAlignRight:
50         return ASCIILiteral("formatJustifyRight");
51     case EditActionCenter:
52         return ASCIILiteral("formatJustifyCenter");
53     case EditActionSubscript:
54         return ASCIILiteral("formatSubscript");
55     case EditActionSuperscript:
56         return ASCIILiteral("formatSuperscript");
57     case EditActionUnderline:
58         return ASCIILiteral("formatUnderline");
59     case EditActionSetColor:
60         return ASCIILiteral("formatFontColor");
61     case EditActionDeleteByDrag:
62         return ASCIILiteral("deleteByDrag");
63     case EditActionCut:
64         return ASCIILiteral("deleteByCut");
65     case EditActionBold:
66         return ASCIILiteral("formatBold");
67     case EditActionItalics:
68         return ASCIILiteral("formatItalic");
69     case EditActionPaste:
70         return ASCIILiteral("insertFromPaste");
71     case EditActionDelete:
72     case EditActionTypingDeleteSelection:
73         return ASCIILiteral("deleteContent");
74     case EditActionTypingDeleteBackward:
75         return ASCIILiteral("deleteContentBackward");
76     case EditActionTypingDeleteForward:
77         return ASCIILiteral("deleteContentForward");
78     case EditActionTypingDeleteWordBackward:
79         return ASCIILiteral("deleteWordBackward");
80     case EditActionTypingDeleteWordForward:
81         return ASCIILiteral("deleteWordForward");
82     case EditActionTypingDeleteLineBackward:
83         return ASCIILiteral("deleteHardLineBackward");
84     case EditActionTypingDeleteLineForward:
85         return ASCIILiteral("deleteHardLineForward");
86     case EditActionTypingDeletePendingComposition:
87         return ASCIILiteral("deleteCompositionText");
88     case EditActionTypingDeleteFinalComposition:
89         return ASCIILiteral("deleteByComposition");
90     case EditActionInsert:
91     case EditActionTypingInsertText:
92         return ASCIILiteral("insertText");
93     case EditActionInsertReplacement:
94         return ASCIILiteral("insertReplacementText");
95     case EditActionInsertFromDrop:
96         return ASCIILiteral("insertFromDrop");
97     case EditActionTypingInsertLineBreak:
98         return ASCIILiteral("insertLineBreak");
99     case EditActionTypingInsertParagraph:
100         return ASCIILiteral("insertParagraph");
101     case EditActionInsertOrderedList:
102         return ASCIILiteral("insertOrderedList");
103     case EditActionInsertUnorderedList:
104         return ASCIILiteral("insertUnorderedList");
105     case EditActionTypingInsertPendingComposition:
106         return ASCIILiteral("insertCompositionText");
107     case EditActionTypingInsertFinalComposition:
108         return ASCIILiteral("insertFromComposition");
109     case EditActionIndent:
110         return ASCIILiteral("formatIndent");
111     case EditActionOutdent:
112         return ASCIILiteral("formatOutdent");
113     case EditActionSetWritingDirection:
114         return ASCIILiteral("formatSetInlineTextDirection");
115     default:
116         return emptyString();
117     }
118 }
119
120 EditCommand::EditCommand(Document& document, EditAction editingAction)
121     : m_document(document)
122     , m_editingAction(editingAction)
123 {
124     ASSERT(document.frame());
125     setStartingSelection(m_document->frame()->selection().selection());
126     setEndingSelection(m_startingSelection);
127 }
128
129 EditCommand::EditCommand(Document& document, const VisibleSelection& startingSelection, const VisibleSelection& endingSelection)
130     : m_document(document)
131 {
132     ASSERT(document.frame());
133     setStartingSelection(startingSelection);
134     setEndingSelection(endingSelection);
135 }
136
137 EditCommand::~EditCommand() = default;
138
139 Frame& EditCommand::frame()
140 {
141     ASSERT(document().frame());
142     return *document().frame();
143 }
144
145 const Frame& EditCommand::frame() const
146 {
147     ASSERT(document().frame());
148     return *document().frame();
149 }
150
151 EditAction EditCommand::editingAction() const
152 {
153     return m_editingAction;
154 }
155
156 static inline EditCommandComposition* compositionIfPossible(EditCommand* command)
157 {
158     if (!command->isCompositeEditCommand())
159         return 0;
160     return toCompositeEditCommand(command)->composition();
161 }
162
163 bool EditCommand::isEditingTextAreaOrTextInput() const
164 {
165     auto* frame = m_document->frame();
166     if (!frame)
167         return false;
168
169     auto* container = frame->selection().selection().start().containerNode();
170     if (!container)
171         return false;
172
173     auto* ancestor = container->shadowHost();
174     if (!ancestor)
175         return false;
176
177     return is<HTMLTextAreaElement>(*ancestor) || (is<HTMLInputElement>(*ancestor) && downcast<HTMLInputElement>(*ancestor).isText());
178 }
179
180 void EditCommand::setStartingSelection(const VisibleSelection& s)
181 {
182     for (EditCommand* cmd = this; ; cmd = cmd->m_parent) {
183         if (auto* composition = compositionIfPossible(cmd))
184             composition->setStartingSelection(s);
185         cmd->m_startingSelection = s;
186         if (!cmd->m_parent || cmd->m_parent->isFirstCommand(cmd))
187             break;
188     }
189 }
190
191 void EditCommand::setEndingSelection(const VisibleSelection &s)
192 {
193     for (EditCommand* cmd = this; cmd; cmd = cmd->m_parent) {
194         if (auto* composition = compositionIfPossible(cmd))
195             composition->setEndingSelection(s);
196         cmd->m_endingSelection = s;
197     }
198 }
199
200 void EditCommand::setParent(CompositeEditCommand* parent)
201 {
202     ASSERT((parent && !m_parent) || (!parent && m_parent));
203     m_parent = parent;
204     if (parent) {
205         m_startingSelection = parent->m_endingSelection;
206         m_endingSelection = parent->m_endingSelection;
207     }
208 }
209
210 void EditCommand::postTextStateChangeNotification(AXTextEditType type, const String& text)
211 {
212     if (!AXObjectCache::accessibilityEnabled())
213         return;
214     postTextStateChangeNotification(type, text, frame().selection().selection().start());
215 }
216
217 void EditCommand::postTextStateChangeNotification(AXTextEditType type, const String& text, const VisiblePosition& position)
218 {
219     if (!AXObjectCache::accessibilityEnabled())
220         return;
221     if (!text.length())
222         return;
223     auto* cache = document().existingAXObjectCache();
224     if (!cache)
225         return;
226     auto* node = highestEditableRoot(position.deepEquivalent(), HasEditableAXRole);
227     cache->postTextStateChangeNotification(node, type, text, position);
228 }
229
230 SimpleEditCommand::SimpleEditCommand(Document& document, EditAction editingAction)
231     : EditCommand(document, editingAction)
232 {
233 }
234
235 void SimpleEditCommand::doReapply()
236 {
237     doApply();
238 }
239
240 #ifndef NDEBUG
241 void SimpleEditCommand::addNodeAndDescendants(Node* startNode, HashSet<Node*>& nodes)
242 {
243     for (Node* node = startNode; node; node = NodeTraversal::next(*node, startNode))
244         nodes.add(node);
245 }
246 #endif
247
248 } // namespace WebCore