Move URL from WebCore to WTF
[WebKit-https.git] / Source / WebCore / editing / Editor.h
1 /*
2  * Copyright (C) 2006, 2007, 2008, 2013, 2014 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 #pragma once
27
28 #include "CompositionUnderline.h"
29 #include "DictationAlternative.h"
30 #include "DocumentMarker.h"
31 #include "EditAction.h"
32 #include "EditingBehavior.h"
33 #include "EditingStyle.h"
34 #include "EditorInsertAction.h"
35 #include "FindOptions.h"
36 #include "FrameSelection.h"
37 #include "PasteboardWriterData.h"
38 #include "TextChecking.h"
39 #include "TextEventInputType.h"
40 #include "TextIteratorBehavior.h"
41 #include "VisibleSelection.h"
42 #include "WritingDirection.h"
43 #include <memory>
44
45 #if PLATFORM(COCOA)
46 OBJC_CLASS NSAttributedString;
47 OBJC_CLASS NSDictionary;
48 OBJC_CLASS NSMutableDictionary;
49 #endif
50
51 namespace PAL {
52 class KillRing;
53 }
54
55 namespace WebCore {
56
57 class AlternativeTextController;
58 class ArchiveResource;
59 class DataTransfer;
60 class CompositeEditCommand;
61 class DeleteButtonController;
62 class EditCommand;
63 class EditCommandComposition;
64 class EditorClient;
65 class EditorInternalCommand;
66 class File;
67 class Frame;
68 class HTMLElement;
69 class HitTestResult;
70 class KeyboardEvent;
71 class KillRing;
72 class Pasteboard;
73 class PasteboardWriterData;
74 class SharedBuffer;
75 class Font;
76 class SpellCheckRequest;
77 class SpellChecker;
78 class StaticRange;
79 class StyleProperties;
80 class Text;
81 class TextCheckerClient;
82 class TextEvent;
83
84 struct FontAttributes;
85 struct PasteboardPlainText;
86 struct PasteboardURL;
87 struct TextCheckingResult;
88
89 #if ENABLE(ATTACHMENT_ELEMENT)
90 struct SerializedAttachmentData;
91 #endif
92
93 enum EditorCommandSource { CommandFromMenuOrKeyBinding, CommandFromDOM, CommandFromDOMWithUserInterface };
94 enum EditorParagraphSeparator { EditorParagraphSeparatorIsDiv, EditorParagraphSeparatorIsP };
95
96 enum class MailBlockquoteHandling {
97     RespectBlockquote,
98     IgnoreBlockquote,
99 };
100
101 #if ENABLE(ATTACHMENT_ELEMENT)
102 class HTMLAttachmentElement;
103 #endif
104
105 enum class TemporarySelectionOption : uint8_t {
106     RevealSelection = 1 << 0,
107     DoNotSetFocus = 1 << 1,
108
109     // Don't propagate selection changes to the client layer.
110     IgnoreSelectionChanges = 1 << 2,
111
112     // Force the render tree to update selection state. Only respected on iOS.
113     EnableAppearanceUpdates = 1 << 3,
114 };
115
116 class TemporarySelectionChange {
117 public:
118     TemporarySelectionChange(Frame&, std::optional<VisibleSelection> = std::nullopt, OptionSet<TemporarySelectionOption> = { });
119     ~TemporarySelectionChange();
120
121 private:
122     void setSelection(const VisibleSelection&);
123
124     Ref<Frame> m_frame;
125     OptionSet<TemporarySelectionOption> m_options;
126     bool m_wasIgnoringSelectionChanges;
127 #if PLATFORM(IOS_FAMILY)
128     bool m_appearanceUpdatesWereEnabled;
129 #endif
130     std::optional<VisibleSelection> m_selectionToRestore;
131 };
132
133 class Editor {
134     WTF_MAKE_FAST_ALLOCATED;
135 public:
136     explicit Editor(Frame&);
137     ~Editor();
138
139     enum class PasteOption : uint8_t {
140         AllowPlainText = 1 << 0,
141         IgnoreMailBlockquote = 1 << 1,
142         AsQuotation = 1 << 2,
143     };
144
145     WEBCORE_EXPORT EditorClient* client() const;
146     WEBCORE_EXPORT TextCheckerClient* textChecker() const;
147
148     CompositeEditCommand* lastEditCommand() { return m_lastEditCommand.get(); }
149
150     void handleKeyboardEvent(KeyboardEvent&);
151     void handleInputMethodKeydown(KeyboardEvent&);
152     bool handleTextEvent(TextEvent&);
153
154     WEBCORE_EXPORT bool canEdit() const;
155     WEBCORE_EXPORT bool canEditRichly() const;
156
157     bool canDHTMLCut();
158     bool canDHTMLCopy();
159     WEBCORE_EXPORT bool canDHTMLPaste();
160     bool tryDHTMLCopy();
161     bool tryDHTMLCut();
162
163     WEBCORE_EXPORT bool canCut() const;
164     WEBCORE_EXPORT bool canCopy() const;
165     WEBCORE_EXPORT bool canPaste() const;
166     WEBCORE_EXPORT bool canDelete() const;
167     bool canSmartCopyOrDelete();
168
169     WEBCORE_EXPORT void cut();
170     WEBCORE_EXPORT void copy();
171     WEBCORE_EXPORT void paste();
172     void paste(Pasteboard&);
173     WEBCORE_EXPORT void pasteAsPlainText();
174     void pasteAsQuotation();
175     WEBCORE_EXPORT void performDelete();
176
177     WEBCORE_EXPORT void copyURL(const URL&, const String& title);
178     void copyURL(const URL&, const String& title, Pasteboard&);
179     PasteboardWriterData::URLData pasteboardWriterURL(const URL&, const String& title);
180 #if !PLATFORM(IOS_FAMILY)
181     WEBCORE_EXPORT void copyImage(const HitTestResult&);
182 #endif
183
184     String readPlainTextFromPasteboard(Pasteboard&);
185
186     WEBCORE_EXPORT void indent();
187     WEBCORE_EXPORT void outdent();
188     void transpose();
189
190     bool shouldInsertFragment(DocumentFragment&, Range*, EditorInsertAction);
191     bool shouldInsertText(const String&, Range*, EditorInsertAction) const;
192     WEBCORE_EXPORT bool shouldDeleteRange(Range*) const;
193     bool shouldApplyStyle(StyleProperties*, Range*);
194
195     void respondToChangedContents(const VisibleSelection& endingSelection);
196
197     bool selectionStartHasStyle(CSSPropertyID, const String& value) const;
198     WEBCORE_EXPORT TriState selectionHasStyle(CSSPropertyID, const String& value) const;
199     String selectionStartCSSPropertyValue(CSSPropertyID);
200     
201     TriState selectionUnorderedListState() const;
202     TriState selectionOrderedListState() const;
203     WEBCORE_EXPORT RefPtr<Node> insertOrderedList();
204     WEBCORE_EXPORT RefPtr<Node> insertUnorderedList();
205     WEBCORE_EXPORT bool canIncreaseSelectionListLevel();
206     WEBCORE_EXPORT bool canDecreaseSelectionListLevel();
207     WEBCORE_EXPORT RefPtr<Node> increaseSelectionListLevel();
208     WEBCORE_EXPORT RefPtr<Node> increaseSelectionListLevelOrdered();
209     WEBCORE_EXPORT RefPtr<Node> increaseSelectionListLevelUnordered();
210     WEBCORE_EXPORT void decreaseSelectionListLevel();
211     WEBCORE_EXPORT void changeSelectionListType();
212    
213     void removeFormattingAndStyle();
214
215     void clearLastEditCommand();
216 #if PLATFORM(IOS_FAMILY)
217     WEBCORE_EXPORT void ensureLastEditCommandHasCurrentSelectionIfOpenForMoreTyping();
218 #endif
219
220     WEBCORE_EXPORT bool deleteWithDirection(SelectionDirection, TextGranularity, bool killRing, bool isTypingAction);
221     WEBCORE_EXPORT void deleteSelectionWithSmartDelete(bool smartDelete, EditAction = EditAction::Delete);
222     void clearText();
223 #if PLATFORM(IOS_FAMILY)
224     WEBCORE_EXPORT void removeUnchangeableStyles();
225 #endif
226
227     WEBCORE_EXPORT void applyStyle(StyleProperties*, EditAction = EditAction::Unspecified);
228     enum class ColorFilterMode { InvertColor, UseOriginalColor };
229     void applyStyle(RefPtr<EditingStyle>&&, EditAction, ColorFilterMode);
230     void applyParagraphStyle(StyleProperties*, EditAction = EditAction::Unspecified);
231     WEBCORE_EXPORT void applyStyleToSelection(StyleProperties*, EditAction);
232     WEBCORE_EXPORT void applyStyleToSelection(Ref<EditingStyle>&&, EditAction, ColorFilterMode);
233     void applyParagraphStyleToSelection(StyleProperties*, EditAction);
234
235     // Returns whether or not we should proceed with editing.
236     bool willApplyEditing(CompositeEditCommand&, Vector<RefPtr<StaticRange>>&&) const;
237     bool willUnapplyEditing(const EditCommandComposition&) const;
238     bool willReapplyEditing(const EditCommandComposition&) const;
239
240     void appliedEditing(CompositeEditCommand&);
241     void unappliedEditing(EditCommandComposition&);
242     void reappliedEditing(EditCommandComposition&);
243     void unappliedSpellCorrection(const VisibleSelection& selectionOfCorrected, const String& corrected, const String& correction);
244
245     // This is off by default, since most editors want this behavior (originally matched IE but not Firefox).
246     void setShouldStyleWithCSS(bool flag) { m_shouldStyleWithCSS = flag; }
247     bool shouldStyleWithCSS() const { return m_shouldStyleWithCSS; }
248
249     class Command {
250     public:
251         WEBCORE_EXPORT Command();
252         Command(const EditorInternalCommand*, EditorCommandSource, Frame&);
253
254         WEBCORE_EXPORT bool execute(const String& parameter = String(), Event* triggeringEvent = nullptr) const;
255         WEBCORE_EXPORT bool execute(Event* triggeringEvent) const;
256
257         WEBCORE_EXPORT bool isSupported() const;
258         WEBCORE_EXPORT bool isEnabled(Event* triggeringEvent = nullptr) const;
259
260         WEBCORE_EXPORT TriState state(Event* triggeringEvent = nullptr) const;
261         String value(Event* triggeringEvent = nullptr) const;
262
263         WEBCORE_EXPORT bool isTextInsertion() const;
264         WEBCORE_EXPORT bool allowExecutionWhenDisabled() const;
265
266     private:
267         const EditorInternalCommand* m_command { nullptr };
268         EditorCommandSource m_source;
269         RefPtr<Frame> m_frame;
270     };
271     WEBCORE_EXPORT Command command(const String& commandName); // Command source is CommandFromMenuOrKeyBinding.
272     Command command(const String& commandName, EditorCommandSource);
273     WEBCORE_EXPORT static bool commandIsSupportedFromMenuOrKeyBinding(const String& commandName); // Works without a frame.
274
275     WEBCORE_EXPORT bool insertText(const String&, Event* triggeringEvent, TextEventInputType = TextEventInputKeyboard);
276     bool insertTextForConfirmedComposition(const String& text);
277     WEBCORE_EXPORT bool insertDictatedText(const String&, const Vector<DictationAlternative>& dictationAlternatives, Event* triggeringEvent);
278     bool insertTextWithoutSendingTextEvent(const String&, bool selectInsertedText, TextEvent* triggeringEvent);
279     bool insertLineBreak();
280     bool insertParagraphSeparator();
281     WEBCORE_EXPORT bool insertParagraphSeparatorInQuotedContent();
282
283     WEBCORE_EXPORT bool isContinuousSpellCheckingEnabled() const;
284     WEBCORE_EXPORT void toggleContinuousSpellChecking();
285     bool isGrammarCheckingEnabled();
286     void toggleGrammarChecking();
287     void ignoreSpelling();
288     void learnSpelling();
289     int spellCheckerDocumentTag();
290     WEBCORE_EXPORT bool isSelectionUngrammatical();
291     String misspelledSelectionString() const;
292     String misspelledWordAtCaretOrRange(Node* clickedNode) const;
293     Vector<String> guessesForMisspelledWord(const String&) const;
294     Vector<String> guessesForMisspelledOrUngrammatical(bool& misspelled, bool& ungrammatical);
295     bool isSpellCheckingEnabledInFocusedNode() const;
296     bool isSpellCheckingEnabledFor(Node*) const;
297     WEBCORE_EXPORT void markMisspellingsAfterTypingToWord(const VisiblePosition &wordStart, const VisibleSelection& selectionAfterTyping, bool doReplacement);
298     void markMisspellings(const VisibleSelection&, RefPtr<Range>& firstMisspellingRange);
299     void markBadGrammar(const VisibleSelection&);
300     void markMisspellingsAndBadGrammar(const VisibleSelection& spellingSelection, bool markGrammar, const VisibleSelection& grammarSelection);
301     void markAndReplaceFor(const SpellCheckRequest&, const Vector<TextCheckingResult>&);
302
303     bool isOverwriteModeEnabled() const { return m_overwriteModeEnabled; }
304     WEBCORE_EXPORT void toggleOverwriteModeEnabled();
305
306     void markAllMisspellingsAndBadGrammarInRanges(OptionSet<TextCheckingType>, RefPtr<Range>&& spellingRange, RefPtr<Range>&& automaticReplacementRange, RefPtr<Range>&& grammarRange);
307 #if PLATFORM(IOS_FAMILY)
308     NO_RETURN_DUE_TO_ASSERT
309 #endif
310     void changeBackToReplacedString(const String& replacedString);
311
312 #if !PLATFORM(IOS_FAMILY)
313     WEBCORE_EXPORT void advanceToNextMisspelling(bool startBeforeSelection = false);
314 #endif
315     void showSpellingGuessPanel();
316     bool spellingPanelIsShowing();
317
318     bool shouldBeginEditing(Range*);
319     bool shouldEndEditing(Range*);
320
321     void clearUndoRedoOperations();
322     bool canUndo() const;
323     void undo();
324     bool canRedo() const;
325     void redo();
326
327     void didBeginEditing();
328     void didEndEditing();
329     void willWriteSelectionToPasteboard(Range*);
330     void didWriteSelectionToPasteboard();
331
332     void showFontPanel();
333     void showStylesPanel();
334     void showColorPanel();
335     void toggleBold();
336     void toggleUnderline();
337     WEBCORE_EXPORT void setBaseWritingDirection(WritingDirection);
338
339     // smartInsertDeleteEnabled and selectTrailingWhitespaceEnabled are 
340     // mutually exclusive, meaning that enabling one will disable the other.
341     bool smartInsertDeleteEnabled();
342     bool isSelectTrailingWhitespaceEnabled() const;
343     
344     WEBCORE_EXPORT bool hasBidiSelection() const;
345
346     // international text input composition
347     bool hasComposition() const { return m_compositionNode; }
348     WEBCORE_EXPORT void setComposition(const String&, const Vector<CompositionUnderline>&, unsigned selectionStart, unsigned selectionEnd);
349     WEBCORE_EXPORT void confirmComposition();
350     WEBCORE_EXPORT void confirmComposition(const String&); // if no existing composition, replaces selection
351     WEBCORE_EXPORT void cancelComposition();
352     bool cancelCompositionIfSelectionIsInvalid();
353     WEBCORE_EXPORT RefPtr<Range> compositionRange() const;
354     WEBCORE_EXPORT bool getCompositionSelection(unsigned& selectionStart, unsigned& selectionEnd) const;
355
356     // getting international text input composition state (for use by InlineTextBox)
357     Text* compositionNode() const { return m_compositionNode.get(); }
358     unsigned compositionStart() const { return m_compositionStart; }
359     unsigned compositionEnd() const { return m_compositionEnd; }
360     bool compositionUsesCustomUnderlines() const { return !m_customCompositionUnderlines.isEmpty(); }
361     const Vector<CompositionUnderline>& customCompositionUnderlines() const { return m_customCompositionUnderlines; }
362
363     enum class RevealSelection { No, Yes };
364     WEBCORE_EXPORT void setIgnoreSelectionChanges(bool, RevealSelection shouldRevealExistingSelection = RevealSelection::Yes);
365     bool ignoreSelectionChanges() const { return m_ignoreSelectionChanges; }
366
367     WEBCORE_EXPORT RefPtr<Range> rangeForPoint(const IntPoint& windowPoint);
368
369     void clear();
370
371     VisibleSelection selectionForCommand(Event*);
372
373     PAL::KillRing& killRing() const { return *m_killRing; }
374     SpellChecker& spellChecker() const { return *m_spellChecker; }
375
376     EditingBehavior behavior() const;
377
378     RefPtr<Range> selectedRange();
379
380 #if PLATFORM(IOS_FAMILY)
381     WEBCORE_EXPORT void confirmMarkedText();
382     WEBCORE_EXPORT void setTextAsChildOfElement(const String&, Element&);
383     WEBCORE_EXPORT void setTextAlignmentForChangedBaseWritingDirection(WritingDirection);
384     WEBCORE_EXPORT void insertDictationPhrases(Vector<Vector<String>>&& dictationPhrases, RetainPtr<id> metadata);
385     WEBCORE_EXPORT void setDictationPhrasesAsChildOfElement(const Vector<Vector<String>>& dictationPhrases, RetainPtr<id> metadata, Element&);
386 #endif
387     
388     enum class KillRingInsertionMode { PrependText, AppendText };
389     void addRangeToKillRing(const Range&, KillRingInsertionMode);
390     void addTextToKillRing(const String&, KillRingInsertionMode);
391     void setStartNewKillRingSequence(bool);
392
393     void startAlternativeTextUITimer();
394     // If user confirmed a correction in the correction panel, correction has non-zero length, otherwise it means that user has dismissed the panel.
395     WEBCORE_EXPORT void handleAlternativeTextUIResult(const String& correction);
396     void dismissCorrectionPanelAsIgnored();
397
398     WEBCORE_EXPORT void pasteAsFragment(Ref<DocumentFragment>&&, bool smartReplace, bool matchStyle, MailBlockquoteHandling = MailBlockquoteHandling::RespectBlockquote);
399     WEBCORE_EXPORT void pasteAsPlainText(const String&, bool smartReplace);
400
401     // This is only called on the mac where paste is implemented primarily at the WebKit level.
402     WEBCORE_EXPORT void pasteAsPlainTextBypassingDHTML();
403  
404     void clearMisspellingsAndBadGrammar(const VisibleSelection&);
405     void markMisspellingsAndBadGrammar(const VisibleSelection&);
406
407     Element* findEventTargetFrom(const VisibleSelection& selection) const;
408
409     WEBCORE_EXPORT String selectedText() const;
410     String selectedTextForDataTransfer() const;
411     WEBCORE_EXPORT bool findString(const String&, FindOptions);
412
413     WEBCORE_EXPORT RefPtr<Range> rangeOfString(const String&, Range*, FindOptions);
414
415     const VisibleSelection& mark() const; // Mark, to be used as emacs uses it.
416     void setMark(const VisibleSelection&);
417
418     void computeAndSetTypingStyle(EditingStyle& , EditAction = EditAction::Unspecified);
419     WEBCORE_EXPORT void computeAndSetTypingStyle(StyleProperties& , EditAction = EditAction::Unspecified);
420     WEBCORE_EXPORT void applyEditingStyleToBodyElement() const;
421     void applyEditingStyleToElement(Element*) const;
422
423     WEBCORE_EXPORT IntRect firstRectForRange(Range*) const;
424
425     void selectionWillChange();
426     void respondToChangedSelection(const VisibleSelection& oldSelection, OptionSet<FrameSelection::SetSelectionOption>);
427     WEBCORE_EXPORT void updateEditorUINowIfScheduled();
428     bool shouldChangeSelection(const VisibleSelection& oldSelection, const VisibleSelection& newSelection, EAffinity, bool stillSelecting) const;
429     WEBCORE_EXPORT unsigned countMatchesForText(const String&, Range*, FindOptions, unsigned limit, bool markMatches, Vector<RefPtr<Range>>*);
430     bool markedTextMatchesAreHighlighted() const;
431     WEBCORE_EXPORT void setMarkedTextMatchesAreHighlighted(bool);
432
433     void textFieldDidBeginEditing(Element*);
434     void textFieldDidEndEditing(Element*);
435     void textDidChangeInTextField(Element*);
436     bool doTextFieldCommandFromEvent(Element*, KeyboardEvent*);
437     void textWillBeDeletedInTextField(Element* input);
438     void textDidChangeInTextArea(Element*);
439     WEBCORE_EXPORT WritingDirection baseWritingDirectionForSelectionStart() const;
440
441     enum class SelectReplacement : bool { No, Yes };
442     enum class SmartReplace : bool { No, Yes };
443     enum class MatchStyle : bool { No, Yes };
444     WEBCORE_EXPORT void replaceSelectionWithFragment(DocumentFragment&, SelectReplacement, SmartReplace, MatchStyle, EditAction = EditAction::Insert, MailBlockquoteHandling = MailBlockquoteHandling::RespectBlockquote);
445     WEBCORE_EXPORT void replaceSelectionWithText(const String&, SelectReplacement, SmartReplace, EditAction = EditAction::Insert);
446     WEBCORE_EXPORT bool selectionStartHasMarkerFor(DocumentMarker::MarkerType, int from, int length) const;
447     void updateMarkersForWordsAffectedByEditing(bool onlyHandleWordsContainingSelection);
448     void deletedAutocorrectionAtPosition(const Position&, const String& originalString);
449     
450     WEBCORE_EXPORT void simplifyMarkup(Node* startNode, Node* endNode);
451
452     EditorParagraphSeparator defaultParagraphSeparator() const { return m_defaultParagraphSeparator; }
453     void setDefaultParagraphSeparator(EditorParagraphSeparator separator) { m_defaultParagraphSeparator = separator; }
454     Vector<String> dictationAlternativesForMarker(const DocumentMarker&);
455     void applyDictationAlternativelternative(const String& alternativeString);
456
457 #if USE(APPKIT)
458     WEBCORE_EXPORT void uppercaseWord();
459     WEBCORE_EXPORT void lowercaseWord();
460     WEBCORE_EXPORT void capitalizeWord();
461 #endif
462
463 #if USE(AUTOMATIC_TEXT_REPLACEMENT)
464     WEBCORE_EXPORT void showSubstitutionsPanel();
465     WEBCORE_EXPORT bool substitutionsPanelIsShowing();
466     WEBCORE_EXPORT void toggleSmartInsertDelete();
467     WEBCORE_EXPORT bool isAutomaticQuoteSubstitutionEnabled();
468     WEBCORE_EXPORT void toggleAutomaticQuoteSubstitution();
469     WEBCORE_EXPORT bool isAutomaticLinkDetectionEnabled();
470     WEBCORE_EXPORT void toggleAutomaticLinkDetection();
471     WEBCORE_EXPORT bool isAutomaticDashSubstitutionEnabled();
472     WEBCORE_EXPORT void toggleAutomaticDashSubstitution();
473     WEBCORE_EXPORT bool isAutomaticTextReplacementEnabled();
474     WEBCORE_EXPORT void toggleAutomaticTextReplacement();
475     WEBCORE_EXPORT bool isAutomaticSpellingCorrectionEnabled();
476     WEBCORE_EXPORT void toggleAutomaticSpellingCorrection();
477 #endif
478
479     RefPtr<DocumentFragment> webContentFromPasteboard(Pasteboard&, Range& context, bool allowPlainText, bool& chosePlainText);
480
481     WEBCORE_EXPORT const Font* fontForSelection(bool& hasMultipleFonts) const;
482     WEBCORE_EXPORT static const RenderStyle* styleForSelectionStart(Frame* , Node *&nodeToRemove);
483     WEBCORE_EXPORT FontAttributes fontAttributesAtSelectionStart() const;
484
485 #if PLATFORM(COCOA)
486     WEBCORE_EXPORT String stringSelectionForPasteboard();
487     String stringSelectionForPasteboardWithImageAltText();
488 #if !PLATFORM(IOS_FAMILY)
489     bool canCopyExcludingStandaloneImages();
490     void takeFindStringFromSelection();
491     WEBCORE_EXPORT void readSelectionFromPasteboard(const String& pasteboardName);
492     WEBCORE_EXPORT void replaceNodeFromPasteboard(Node*, const String& pasteboardName);
493     WEBCORE_EXPORT RefPtr<SharedBuffer> dataSelectionForPasteboard(const String& pasteboardName);
494 #endif // !PLATFORM(IOS_FAMILY)
495     WEBCORE_EXPORT void replaceSelectionWithAttributedString(NSAttributedString *, MailBlockquoteHandling = MailBlockquoteHandling::RespectBlockquote);
496 #endif
497
498     String clientReplacementURLForResource(Ref<SharedBuffer>&& resourceData, const String& mimeType);
499
500 #if !PLATFORM(WIN)
501     WEBCORE_EXPORT void writeSelectionToPasteboard(Pasteboard&);
502     WEBCORE_EXPORT void writeImageToPasteboard(Pasteboard&, Element& imageElement, const URL&, const String& title);
503     void writeSelection(PasteboardWriterData&);
504 #endif
505
506 #if ENABLE(TELEPHONE_NUMBER_DETECTION) && !PLATFORM(IOS_FAMILY)
507     void scanSelectionForTelephoneNumbers();
508     const Vector<RefPtr<Range>>& detectedTelephoneNumberRanges() const { return m_detectedTelephoneNumberRanges; }
509 #endif
510
511     WEBCORE_EXPORT String stringForCandidateRequest() const;
512     WEBCORE_EXPORT void handleAcceptedCandidate(TextCheckingResult);
513     WEBCORE_EXPORT RefPtr<Range> contextRangeForCandidateRequest() const;
514     RefPtr<Range> rangeForTextCheckingResult(const TextCheckingResult&) const;
515     bool isHandlingAcceptedCandidate() const { return m_isHandlingAcceptedCandidate; }
516
517     void setIsGettingDictionaryPopupInfo(bool b) { m_isGettingDictionaryPopupInfo = b; }
518     bool isGettingDictionaryPopupInfo() const { return m_isGettingDictionaryPopupInfo; }
519
520 #if ENABLE(ATTACHMENT_ELEMENT)
521     WEBCORE_EXPORT void insertAttachment(const String& identifier, std::optional<uint64_t>&& fileSize, const String& fileName, const String& contentType);
522     void registerAttachmentIdentifier(const String&, const String& contentType, const String& preferredFileName, Ref<SharedBuffer>&& fileData);
523     void registerAttachments(Vector<SerializedAttachmentData>&&);
524     void registerAttachmentIdentifier(const String&, const String& contentType, const String& filePath);
525     void registerAttachmentIdentifier(const String&);
526     void cloneAttachmentData(const String& fromIdentifier, const String& toIdentifier);
527     void didInsertAttachmentElement(HTMLAttachmentElement&);
528     void didRemoveAttachmentElement(HTMLAttachmentElement&);
529
530 #if PLATFORM(COCOA)
531     void getPasteboardTypesAndDataForAttachment(Element&, Vector<String>& outTypes, Vector<RefPtr<SharedBuffer>>& outData);
532 #endif
533 #endif
534
535     WEBCORE_EXPORT RefPtr<HTMLImageElement> insertEditableImage();
536
537 private:
538     Document& document() const;
539
540     bool canDeleteRange(Range*) const;
541     bool canSmartReplaceWithPasteboard(Pasteboard&);
542     void pasteAsPlainTextWithPasteboard(Pasteboard&);
543     void pasteWithPasteboard(Pasteboard*, OptionSet<PasteOption>);
544     String plainTextFromPasteboard(const PasteboardPlainText&);
545
546     void quoteFragmentForPasting(DocumentFragment&);
547
548     void revealSelectionAfterEditingOperation(const ScrollAlignment& = ScrollAlignment::alignCenterIfNeeded, RevealExtentOption = DoNotRevealExtent);
549     void markMisspellingsOrBadGrammar(const VisibleSelection&, bool checkSpelling, RefPtr<Range>& firstMisspellingRange);
550     OptionSet<TextCheckingType> resolveTextCheckingTypeMask(const Node& rootEditableElement, OptionSet<TextCheckingType>);
551
552     WEBCORE_EXPORT String selectedText(TextIteratorBehavior) const;
553
554     void selectComposition();
555     enum SetCompositionMode { ConfirmComposition, CancelComposition };
556     void setComposition(const String&, SetCompositionMode);
557
558     void changeSelectionAfterCommand(const VisibleSelection& newSelection, OptionSet<FrameSelection::SetSelectionOption>);
559
560     enum EditorActionSpecifier { CutAction, CopyAction };
561     void performCutOrCopy(EditorActionSpecifier);
562
563     void editorUIUpdateTimerFired();
564
565     Element* findEventTargetFromSelection() const;
566
567     bool unifiedTextCheckerEnabled() const;
568
569     RefPtr<Range> adjustedSelectionRange();
570
571 #if PLATFORM(COCOA)
572     RefPtr<SharedBuffer> selectionInWebArchiveFormat();
573     String selectionInHTMLFormat();
574     RefPtr<SharedBuffer> imageInWebArchiveFormat(Element&);
575     static String userVisibleString(const URL&);
576     static RefPtr<SharedBuffer> dataInRTFDFormat(NSAttributedString *);
577     static RefPtr<SharedBuffer> dataInRTFFormat(NSAttributedString *);
578 #endif
579     void platformFontAttributesAtSelectionStart(FontAttributes&, const RenderStyle&) const;
580
581     void scheduleEditorUIUpdate();
582
583 #if ENABLE(ATTACHMENT_ELEMENT)
584     void notifyClientOfAttachmentUpdates();
585 #endif
586
587     void postTextStateChangeNotificationForCut(const String&, const VisibleSelection&);
588
589     Frame& m_frame;
590     RefPtr<CompositeEditCommand> m_lastEditCommand;
591     RefPtr<Text> m_compositionNode;
592     unsigned m_compositionStart;
593     unsigned m_compositionEnd;
594     Vector<CompositionUnderline> m_customCompositionUnderlines;
595     bool m_ignoreSelectionChanges { false };
596     bool m_shouldStartNewKillRingSequence { false };
597     bool m_shouldStyleWithCSS { false };
598     const std::unique_ptr<PAL::KillRing> m_killRing;
599     const std::unique_ptr<SpellChecker> m_spellChecker;
600     const std::unique_ptr<AlternativeTextController> m_alternativeTextController;
601     EditorParagraphSeparator m_defaultParagraphSeparator { EditorParagraphSeparatorIsDiv };
602     bool m_overwriteModeEnabled { false };
603
604 #if ENABLE(ATTACHMENT_ELEMENT)
605     HashSet<String> m_insertedAttachmentIdentifiers;
606     HashSet<String> m_removedAttachmentIdentifiers;
607 #endif
608
609     VisibleSelection m_mark;
610     bool m_areMarkedTextMatchesHighlighted { false };
611
612     VisibleSelection m_oldSelectionForEditorUIUpdate;
613     Timer m_editorUIUpdateTimer;
614     bool m_editorUIUpdateTimerShouldCheckSpellingAndGrammar { false };
615     bool m_editorUIUpdateTimerWasTriggeredByDictation { false };
616     bool m_isHandlingAcceptedCandidate { false };
617
618 #if ENABLE(TELEPHONE_NUMBER_DETECTION) && !PLATFORM(IOS_FAMILY)
619     bool shouldDetectTelephoneNumbers();
620     void scanRangeForTelephoneNumbers(Range&, const StringView&, Vector<RefPtr<Range>>& markedRanges);
621
622     Timer m_telephoneNumberDetectionUpdateTimer;
623     Vector<RefPtr<Range>> m_detectedTelephoneNumberRanges;
624 #endif
625
626     bool m_isGettingDictionaryPopupInfo { false };
627 };
628
629 inline void Editor::setStartNewKillRingSequence(bool flag)
630 {
631     m_shouldStartNewKillRingSequence = flag;
632 }
633
634 inline const VisibleSelection& Editor::mark() const
635 {
636     return m_mark;
637 }
638
639 inline void Editor::setMark(const VisibleSelection& selection)
640 {
641     m_mark = selection;
642 }
643
644 inline bool Editor::markedTextMatchesAreHighlighted() const
645 {
646     return m_areMarkedTextMatchesHighlighted;
647 }
648
649 } // namespace WebCore