1/*
2 * Copyright (C) 2006, 2014 Apple Inc. All rights reserved.
3 * Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies)
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1.  Redistributions of source code must retain the above copyright
10 *     notice, this list of conditions and the following disclaimer.
11 * 2.  Redistributions in binary form must reproduce the above copyright
12 *     notice, this list of conditions and the following disclaimer in the
13 *     documentation and/or other materials provided with the distribution.
14 * 3.  Neither the name of Apple Inc. ("Apple") nor the names of
15 *     its contributors may be used to endorse or promote products derived
16 *     from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
19 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#import <WebCore/EditorClient.h>
31#import <WebCore/TextCheckerClient.h>
32#import <wtf/Forward.h>
33#import <wtf/RetainPtr.h>
34#import <wtf/Vector.h>
35#import <wtf/text/StringView.h>
36
37@class WebView;
38@class WebEditorUndoTarget;
39
40class WebEditorClient : public WebCore::EditorClient, public WebCore::TextCheckerClient {
41public:
42    WebEditorClient(WebView *);
43    virtual ~WebEditorClient();
44
45    void didCheckSucceed(int sequence, NSArray *results);
46
47private:
48    virtual void pageDestroyed() override;
49
50    virtual bool isGrammarCheckingEnabled() override;
51    virtual void toggleGrammarChecking() override;
52    virtual bool isContinuousSpellCheckingEnabled() override;
53    virtual void toggleContinuousSpellChecking() override;
54    virtual int spellCheckerDocumentTag() override;
55
56    virtual bool smartInsertDeleteEnabled() override;
57    virtual bool isSelectTrailingWhitespaceEnabled() override;
58
59    virtual bool shouldDeleteRange(WebCore::Range*) override;
60
61    virtual bool shouldBeginEditing(WebCore::Range*) override;
62    virtual bool shouldEndEditing(WebCore::Range*) override;
63    virtual bool shouldInsertNode(WebCore::Node*, WebCore::Range*, WebCore::EditorInsertAction) override;
64    virtual bool shouldInsertText(const String&, WebCore::Range*, WebCore::EditorInsertAction) override;
65    virtual bool shouldChangeSelectedRange(WebCore::Range* fromRange, WebCore::Range* toRange, WebCore::EAffinity, bool stillSelecting) override;
66
67    virtual bool shouldApplyStyle(WebCore::StyleProperties*, WebCore::Range*) override;
68
69    virtual bool shouldMoveRangeAfterDelete(WebCore::Range*, WebCore::Range* rangeToBeReplaced) override;
70
71    virtual void didBeginEditing() override;
72    virtual void didEndEditing() override;
73    virtual void willWriteSelectionToPasteboard(WebCore::Range*) override;
74    virtual void didWriteSelectionToPasteboard() override;
75    virtual void getClientPasteboardDataForRange(WebCore::Range*, Vector<String>& pasteboardTypes, Vector<RefPtr<WebCore::SharedBuffer>>& pasteboardData) override;
76
77    virtual NSString *userVisibleString(NSURL *) override;
78    virtual WebCore::DocumentFragment* documentFragmentFromAttributedString(NSAttributedString *, Vector< RefPtr<WebCore::ArchiveResource>>&) override;
79    virtual void setInsertionPasteboard(const String&) override;
80    virtual NSURL *canonicalizeURL(NSURL *) override;
81    virtual NSURL *canonicalizeURLString(NSString *) override;
82
83#if USE(APPKIT)
84    virtual void uppercaseWord() override;
85    virtual void lowercaseWord() override;
86    virtual void capitalizeWord() override;
87#endif
88
89#if USE(AUTOMATIC_TEXT_REPLACEMENT)
90    virtual void showSubstitutionsPanel(bool show) override;
91    virtual bool substitutionsPanelIsShowing() override;
92    virtual void toggleSmartInsertDelete() override;
93    virtual bool isAutomaticQuoteSubstitutionEnabled() override;
94    virtual void toggleAutomaticQuoteSubstitution() override;
95    virtual bool isAutomaticLinkDetectionEnabled() override;
96    virtual void toggleAutomaticLinkDetection() override;
97    virtual bool isAutomaticDashSubstitutionEnabled() override;
98    virtual void toggleAutomaticDashSubstitution() override;
99    virtual bool isAutomaticTextReplacementEnabled() override;
100    virtual void toggleAutomaticTextReplacement() override;
101    virtual bool isAutomaticSpellingCorrectionEnabled() override;
102    virtual void toggleAutomaticSpellingCorrection() override;
103#endif
104
105#if ENABLE(DELETION_UI)
106    virtual bool shouldShowDeleteInterface(WebCore::HTMLElement*) override;
107#endif
108
109    virtual TextCheckerClient* textChecker() override { return this; }
110
111    virtual void respondToChangedContents() override;
112    virtual void respondToChangedSelection(WebCore::Frame*) override;
113
114    virtual void registerUndoStep(PassRefPtr<WebCore::UndoStep>) override;
115    virtual void registerRedoStep(PassRefPtr<WebCore::UndoStep>) override;
116    virtual void clearUndoRedoOperations() override;
117
118    virtual bool canCopyCut(WebCore::Frame*, bool defaultValue) const override;
119    virtual bool canPaste(WebCore::Frame*, bool defaultValue) const override;
120    virtual bool canUndo() const override;
121    virtual bool canRedo() const override;
122
123    virtual void undo() override;
124    virtual void redo() override;
125
126    virtual void handleKeyboardEvent(WebCore::KeyboardEvent*) override;
127    virtual void handleInputMethodKeydown(WebCore::KeyboardEvent*) override;
128
129    virtual void textFieldDidBeginEditing(WebCore::Element*) override;
130    virtual void textFieldDidEndEditing(WebCore::Element*) override;
131    virtual void textDidChangeInTextField(WebCore::Element*) override;
132    virtual bool doTextFieldCommandFromEvent(WebCore::Element*, WebCore::KeyboardEvent*) override;
133    virtual void textWillBeDeletedInTextField(WebCore::Element*) override;
134    virtual void textDidChangeInTextArea(WebCore::Element*) override;
135    virtual void overflowScrollPositionChanged() override { };
136
137#if PLATFORM(IOS)
138    virtual void startDelayingAndCoalescingContentChangeNotifications() override;
139    virtual void stopDelayingAndCoalescingContentChangeNotifications() override;
140    virtual void writeDataToPasteboard(NSDictionary*) override;
141    virtual NSArray* supportedPasteboardTypesForCurrentSelection() override;
142    virtual NSArray* readDataFromPasteboard(NSString* type, int index) override;
143    virtual bool hasRichlyEditableSelection() override;
144    virtual int getPasteboardItemsCount() override;
145    virtual WebCore::DocumentFragment* documentFragmentFromDelegate(int index) override;
146    virtual bool performsTwoStepPaste(WebCore::DocumentFragment*) override;
147    virtual int pasteboardChangeCount() override;
148#endif
149
150    virtual bool shouldEraseMarkersAfterChangeSelection(WebCore::TextCheckingType) const override;
151    virtual void ignoreWordInSpellDocument(const String&) override;
152    virtual void learnWord(const String&) override;
153    virtual void checkSpellingOfString(StringView, int* misspellingLocation, int* misspellingLength) override;
154    virtual String getAutoCorrectSuggestionForMisspelledWord(const String&) override;
155    virtual void checkGrammarOfString(StringView, Vector<WebCore::GrammarDetail>&, int* badGrammarLocation, int* badGrammarLength) override;
156    virtual Vector<WebCore::TextCheckingResult> checkTextOfParagraph(StringView, WebCore::TextCheckingTypeMask checkingTypes) override;
157    virtual void updateSpellingUIWithGrammarString(const String&, const WebCore::GrammarDetail&) override;
158    virtual void updateSpellingUIWithMisspelledWord(const String&) override;
159    virtual void showSpellingUI(bool show) override;
160    virtual bool spellingUIIsShowing() override;
161    virtual void getGuessesForWord(const String& word, const String& context, Vector<String>& guesses) override;
162
163    virtual void willSetInputMethodState() override;
164    virtual void setInputMethodState(bool enabled) override;
165    virtual void requestCheckingOfString(PassRefPtr<WebCore::TextCheckingRequest>) override;
166
167    void registerUndoOrRedoStep(PassRefPtr<WebCore::UndoStep>, bool isRedo);
168
169    WebView *m_webView;
170    RetainPtr<WebEditorUndoTarget> m_undoTarget;
171    bool m_haveUndoRedoOperations;
172    RefPtr<WebCore::TextCheckingRequest> m_textCheckingRequest;
173#if PLATFORM(IOS)
174    bool m_delayingContentChangeNotifications;
175    bool m_hasDelayedContentChangeNotification;
176#endif
177};
178
179#if PLATFORM(IOS)
180
181inline bool WebEditorClient::isGrammarCheckingEnabled()
182{
183    return false;
184}
185
186inline void WebEditorClient::toggleGrammarChecking()
187{
188}
189
190inline void WebEditorClient::toggleContinuousSpellChecking()
191{
192}
193
194inline int WebEditorClient::spellCheckerDocumentTag()
195{
196    return 0;
197}
198
199inline bool WebEditorClient::shouldEraseMarkersAfterChangeSelection(WebCore::TextCheckingType) const
200{
201    return true;
202}
203
204inline void WebEditorClient::ignoreWordInSpellDocument(const String&)
205{
206}
207
208inline void WebEditorClient::learnWord(const String&)
209{
210}
211
212inline void WebEditorClient::checkSpellingOfString(StringView, int* misspellingLocation, int* misspellingLength)
213{
214}
215
216inline String WebEditorClient::getAutoCorrectSuggestionForMisspelledWord(const String&)
217{
218    return "";
219}
220
221inline void WebEditorClient::checkGrammarOfString(StringView, Vector<WebCore::GrammarDetail>&, int* badGrammarLocation, int* badGrammarLength)
222{
223}
224
225inline void WebEditorClient::updateSpellingUIWithGrammarString(const String&, const WebCore::GrammarDetail&)
226{
227}
228
229inline void WebEditorClient::updateSpellingUIWithMisspelledWord(const String&)
230{
231}
232
233inline void WebEditorClient::showSpellingUI(bool show)
234{
235}
236
237inline bool WebEditorClient::spellingUIIsShowing()
238{
239    return false;
240}
241
242inline void WebEditorClient::getGuessesForWord(const String&, const String&, Vector<String>&)
243{
244}
245
246#endif
247