1 /*
2 * Copyright (C) 2006, 2007, 2008 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 #ifndef THIRD_PARTY_BLINK_RENDERER_CORE_EDITING_EDITOR_H_
27 #define THIRD_PARTY_BLINK_RENDERER_CORE_EDITING_EDITOR_H_
28
29 #include <memory>
30
31 #include "base/macros.h"
32 #include "third_party/blink/renderer/core/core_export.h"
33 #include "third_party/blink/renderer/core/editing/editing_style.h"
34 #include "third_party/blink/renderer/core/editing/finder/find_options.h"
35 #include "third_party/blink/renderer/core/editing/forward.h"
36 #include "third_party/blink/renderer/core/editing/visible_selection.h"
37 #include "third_party/blink/renderer/core/events/input_event.h"
38 #include "third_party/blink/renderer/core/scroll/scroll_alignment.h"
39 #include "third_party/blink/renderer/platform/heap/handle.h"
40
41 namespace blink {
42
43 class CompositeEditCommand;
44 class DragData;
45 class EditingBehavior;
46 class EditorCommand;
47 class FrameSelection;
48 class LocalFrame;
49 class HitTestResult;
50 class KillRing;
51 class SpellChecker;
52 class CSSPropertyValueSet;
53 class TextEvent;
54 class UndoStack;
55
56 enum class DeleteDirection;
57 enum class DeleteMode { kSimple, kSmart };
58 enum class InsertMode { kSimple, kSmart };
59 enum class DragSourceType { kHTMLSource, kPlainTextSource };
60 enum class EditorParagraphSeparator { kIsDiv, kIsP };
61 enum class EditorCommandSource { kMenuOrKeyBinding, kDOM };
62 enum class WritingDirection;
63
64 class CORE_EXPORT Editor final : public GarbageCollected<Editor> {
65 public:
66 explicit Editor(LocalFrame&);
67 ~Editor();
68
LastEditCommand()69 CompositeEditCommand* LastEditCommand() { return last_edit_command_.Get(); }
SetLastEditCommand(CompositeEditCommand * last_edit_command)70 void SetLastEditCommand(CompositeEditCommand* last_edit_command) {
71 last_edit_command_ = last_edit_command;
72 }
73
74 void HandleKeyboardEvent(KeyboardEvent*);
75 bool HandleTextEvent(TextEvent*);
76
77 bool CanEdit() const;
78 bool CanEditRichly() const;
79
80 bool CanCut() const;
81 bool CanCopy() const;
82 bool CanPaste() const;
83 bool CanDelete() const;
84
85 static void CountEvent(ExecutionContext*, const Event&);
86 void CopyImage(const HitTestResult&);
87
88 void RespondToChangedContents(const Position&);
89
90 void RegisterCommandGroup(CompositeEditCommand* command_group_wrapper);
91
92 void DeleteSelectionWithSmartDelete(
93 DeleteMode,
94 InputEvent::InputType,
95 const Position& reference_move_position = Position());
96
97 void ApplyParagraphStyle(CSSPropertyValueSet*, InputEvent::InputType);
98 void ApplyParagraphStyleToSelection(CSSPropertyValueSet*,
99 InputEvent::InputType);
100
SetShouldStyleWithCSS(bool flag)101 void SetShouldStyleWithCSS(bool flag) { should_style_with_css_ = flag; }
ShouldStyleWithCSS()102 bool ShouldStyleWithCSS() const { return should_style_with_css_; }
103
104 EditorCommand CreateCommand(const String& command_name)
105 const; // Command source is CommandFromMenuOrKeyBinding.
106 EditorCommand CreateCommand(const String& command_name,
107 EditorCommandSource) const;
108
109 // |Editor::executeCommand| is implementation of |WebFrame::executeCommand|
110 // rather than |Document::execCommand|.
111 bool ExecuteCommand(const String&);
112 bool ExecuteCommand(const String& command_name, const String& value);
113 bool IsCommandEnabled(const String&) const;
114
115 bool InsertText(const String&, KeyboardEvent* triggering_event);
116 bool InsertTextWithoutSendingTextEvent(
117 const String&,
118 bool select_inserted_text,
119 TextEvent* triggering_event,
120 InputEvent::InputType = InputEvent::InputType::kInsertText);
121 bool InsertLineBreak();
122 bool InsertParagraphSeparator();
123
IsOverwriteModeEnabled()124 bool IsOverwriteModeEnabled() const { return overwrite_mode_enabled_; }
125 void ToggleOverwriteModeEnabled();
126
127 bool CanUndo();
128 void Undo();
129 bool CanRedo();
130 void Redo();
131
132 // Exposed for IdleSpellCheckController only.
133 // Supposed to be used as |const UndoStack&|.
GetUndoStack()134 UndoStack& GetUndoStack() const { return *undo_stack_; }
135
136 void SetBaseWritingDirection(WritingDirection);
137
138 // smartInsertDeleteEnabled and selectTrailingWhitespaceEnabled are
139 // mutually exclusive, meaning that enabling one will disable the other.
140 bool SmartInsertDeleteEnabled() const;
141 bool IsSelectTrailingWhitespaceEnabled() const;
142
PreventRevealSelection()143 bool PreventRevealSelection() const { return prevent_reveal_selection_; }
IncreasePreventRevealSelection()144 void IncreasePreventRevealSelection() { ++prevent_reveal_selection_; }
DecreasePreventRevealSelection()145 void DecreasePreventRevealSelection() { --prevent_reveal_selection_; }
146
147 void SetStartNewKillRingSequence(bool);
148
149 void Clear();
150
151 SelectionInDOMTree SelectionForCommand(Event*);
152
GetKillRing()153 KillRing& GetKillRing() const { return *kill_ring_; }
154
155 EditingBehavior Behavior() const;
156
157 EphemeralRange SelectedRange();
158
159 void AddToKillRing(const EphemeralRange&);
160
161 static bool FindString(LocalFrame&, const String&, FindOptions);
162
163 static Range* FindRangeOfString(
164 Document&,
165 const String& target,
166 const EphemeralRangeInFlatTree& reference_range,
167 FindOptions);
168
169 const VisibleSelection& Mark() const; // Mark, to be used as emacs uses it.
170 bool MarkIsDirectional() const;
171 void SetMark();
172
173 void ComputeAndSetTypingStyle(CSSPropertyValueSet*, InputEvent::InputType);
174
175 EphemeralRange RangeForPoint(const IntPoint&) const;
176
177 void RespondToChangedSelection();
178
179 bool MarkedTextMatchesAreHighlighted() const;
180 void SetMarkedTextMatchesAreHighlighted(bool);
181
182 void ReplaceSelectionWithFragment(DocumentFragment*,
183 bool select_replacement,
184 bool smart_replace,
185 bool match_style,
186 InputEvent::InputType);
187 void ReplaceSelectionWithText(const String&,
188 bool select_replacement,
189 bool smart_replace,
190 InputEvent::InputType);
191
192 // Implementation of WebLocalFrameImpl::ReplaceSelection. Does not use smart
193 // replacement.
194 void ReplaceSelection(const String&);
195
196 void ReplaceSelectionAfterDragging(DocumentFragment*,
197 InsertMode,
198 DragSourceType);
199
200 // Return false if frame was destroyed by event handler, should stop executing
201 // remaining actions.
202 bool DeleteSelectionAfterDraggingWithEvents(
203 Element* drag_source,
204 DeleteMode,
205 const Position& reference_move_position);
206 bool ReplaceSelectionAfterDraggingWithEvents(Element* drop_target,
207 DragData*,
208 DocumentFragment*,
209 Range* drop_caret_range,
210 InsertMode,
211 DragSourceType);
212
DefaultParagraphSeparator()213 EditorParagraphSeparator DefaultParagraphSeparator() const {
214 return default_paragraph_separator_;
215 }
SetDefaultParagraphSeparator(EditorParagraphSeparator separator)216 void SetDefaultParagraphSeparator(EditorParagraphSeparator separator) {
217 default_paragraph_separator_ = separator;
218 }
219
220 EditingStyle* TypingStyle() const;
221 void SetTypingStyle(EditingStyle*);
222 void ClearTypingStyle();
223
224 void Trace(Visitor*);
225
226 void RevealSelectionAfterEditingOperation(
227 const mojom::blink::ScrollAlignment& = ScrollAlignment::ToEdgeIfNeeded());
228
229 private:
230 Member<LocalFrame> frame_;
231 Member<CompositeEditCommand> last_edit_command_;
232 const Member<UndoStack> undo_stack_;
233 int prevent_reveal_selection_;
234 bool should_start_new_kill_ring_sequence_;
235 bool should_style_with_css_;
236 const std::unique_ptr<KillRing> kill_ring_;
237 VisibleSelection mark_;
238 bool are_marked_text_matches_highlighted_;
239 EditorParagraphSeparator default_paragraph_separator_;
240 bool overwrite_mode_enabled_;
241 Member<EditingStyle> typing_style_;
242 bool mark_is_directional_ = false;
243
GetFrame()244 LocalFrame& GetFrame() const {
245 DCHECK(frame_);
246 return *frame_;
247 }
248
249 SpellChecker& GetSpellChecker() const;
250 FrameSelection& GetFrameSelection() const;
251
252 bool HandleEditingKeyboardEvent(KeyboardEvent*);
253
254 DISALLOW_COPY_AND_ASSIGN(Editor);
255 };
256
SetStartNewKillRingSequence(bool flag)257 inline void Editor::SetStartNewKillRingSequence(bool flag) {
258 should_start_new_kill_ring_sequence_ = flag;
259 }
260
Mark()261 inline const VisibleSelection& Editor::Mark() const {
262 return mark_;
263 }
264
MarkIsDirectional()265 inline bool Editor::MarkIsDirectional() const {
266 return mark_is_directional_;
267 }
268
MarkedTextMatchesAreHighlighted()269 inline bool Editor::MarkedTextMatchesAreHighlighted() const {
270 return are_marked_text_matches_highlighted_;
271 }
272
TypingStyle()273 inline EditingStyle* Editor::TypingStyle() const {
274 return typing_style_.Get();
275 }
276
ClearTypingStyle()277 inline void Editor::ClearTypingStyle() {
278 typing_style_.Clear();
279 }
280
SetTypingStyle(EditingStyle * style)281 inline void Editor::SetTypingStyle(EditingStyle* style) {
282 typing_style_ = style;
283 }
284
285 } // namespace blink
286
287 #endif // THIRD_PARTY_BLINK_RENDERER_CORE_EDITING_EDITOR_H_
288