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