2 * Copyright (C) 2010 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
27 #include "WebEditorClient.h"
29 #include "EditorState.h"
30 #include "WebCoreArgumentCoders.h"
31 #include "WebFrameLoaderClient.h"
33 #include "WebPageProxy.h"
34 #include "WebPageProxyMessages.h"
35 #include "WebProcess.h"
36 #include <WebCore/ArchiveResource.h>
37 #include <WebCore/DocumentFragment.h>
38 #include <WebCore/FocusController.h>
39 #include <WebCore/Frame.h>
40 #include <WebCore/FrameView.h>
41 #include <WebCore/HTMLInputElement.h>
42 #include <WebCore/HTMLNames.h>
43 #include <WebCore/HTMLTextAreaElement.h>
44 #include <WebCore/KeyboardEvent.h>
45 #include <WebCore/NotImplemented.h>
46 #include <WebCore/Page.h>
47 #include <WebCore/TextIterator.h>
48 #include <WebCore/UndoStep.h>
49 #include <WebCore/UserTypingGestureIndicator.h>
51 using namespace WebCore;
52 using namespace HTMLNames;
56 void WebEditorClient::pageDestroyed()
61 bool WebEditorClient::shouldDeleteRange(Range* range)
63 bool result = m_page->injectedBundleEditorClient().shouldDeleteRange(m_page, range);
68 #if OS(TIZEN) // https://bugs.webkit.org/show_bug.cgi?id=86615
69 bool WebEditorClient::shouldShowDeleteInterface(HTMLElement* element)
71 return element->getAttribute(HTMLNames::classAttr) == "needsDeletionUI";
74 bool WebEditorClient::shouldShowDeleteInterface(HTMLElement*)
81 bool WebEditorClient::smartInsertDeleteEnabled()
83 // FIXME: Why isn't this Mac specific like toggleSmartInsertDeleteEnabled?
85 return m_page->isSmartInsertDeleteEnabled();
91 bool WebEditorClient::isSelectTrailingWhitespaceEnabled()
97 bool WebEditorClient::isContinuousSpellCheckingEnabled()
99 return WebProcess::shared().textCheckerState().isContinuousSpellCheckingEnabled;
102 void WebEditorClient::toggleContinuousSpellChecking()
107 bool WebEditorClient::isGrammarCheckingEnabled()
109 return WebProcess::shared().textCheckerState().isGrammarCheckingEnabled;
112 void WebEditorClient::toggleGrammarChecking()
117 int WebEditorClient::spellCheckerDocumentTag()
123 bool WebEditorClient::shouldBeginEditing(Range* range)
125 bool result = m_page->injectedBundleEditorClient().shouldBeginEditing(m_page, range);
130 bool WebEditorClient::shouldEndEditing(Range* range)
132 bool result = m_page->injectedBundleEditorClient().shouldEndEditing(m_page, range);
137 bool WebEditorClient::shouldInsertNode(Node* node, Range* rangeToReplace, EditorInsertAction action)
139 bool result = m_page->injectedBundleEditorClient().shouldInsertNode(m_page, node, rangeToReplace, action);
144 bool WebEditorClient::shouldInsertText(const String& text, Range* rangeToReplace, EditorInsertAction action)
146 bool result = m_page->injectedBundleEditorClient().shouldInsertText(m_page, text.impl(), rangeToReplace, action);
151 bool WebEditorClient::shouldChangeSelectedRange(Range* fromRange, Range* toRange, EAffinity affinity, bool stillSelecting)
153 bool result = m_page->injectedBundleEditorClient().shouldChangeSelectedRange(m_page, fromRange, toRange, affinity, stillSelecting);
158 bool WebEditorClient::shouldApplyStyle(StylePropertySet* style, Range* range)
160 bool result = m_page->injectedBundleEditorClient().shouldApplyStyle(m_page, style->ensureCSSStyleDeclaration(), range);
165 bool WebEditorClient::shouldMoveRangeAfterDelete(Range*, Range*)
171 void WebEditorClient::didBeginEditing()
173 // FIXME: What good is a notification name, if it's always the same?
174 DEFINE_STATIC_LOCAL(String, WebViewDidBeginEditingNotification, ("WebViewDidBeginEditingNotification"));
175 m_page->injectedBundleEditorClient().didBeginEditing(m_page, WebViewDidBeginEditingNotification.impl());
179 void WebEditorClient::respondToChangedContents()
181 DEFINE_STATIC_LOCAL(String, WebViewDidChangeNotification, ("WebViewDidChangeNotification"));
182 m_page->injectedBundleEditorClient().didChange(m_page, WebViewDidChangeNotification.impl());
186 void WebEditorClient::respondToChangedSelection(Frame* frame)
188 DEFINE_STATIC_LOCAL(String, WebViewDidChangeSelectionNotification, ("WebViewDidChangeSelectionNotification"));
189 m_page->injectedBundleEditorClient().didChangeSelection(m_page, WebViewDidChangeSelectionNotification.impl());
193 EditorState state = m_page->editorState();
195 #if ENABLE(TIZEN_ISF_PORT) || ENABLE(TIZEN_WEBKIT2_TEXT_SELECTION)
196 const EditorState& currentState = m_page->currentEditorState();
197 if (currentState.hasComposition && currentState.inputMethodContextID != state.inputMethodContextID)
198 didCancelComposition(reinterpret_cast<Node*>(currentState.inputMethodContextID));
200 m_page->setEditorState(state);
203 m_page->send(Messages::WebPageProxy::EditorStateChanged(state));
206 // FIXME: This should also go into the selection state.
207 if (!frame->editor()->hasComposition() || frame->editor()->ignoreCompositionSelectionChange())
212 m_page->send(Messages::WebPageProxy::DidChangeCompositionSelection(frame->editor()->getCompositionSelection(start, end)));
214 setSelectionPrimaryClipboardIfNeeded(frame);
218 void WebEditorClient::didEndEditing()
220 DEFINE_STATIC_LOCAL(String, WebViewDidEndEditingNotification, ("WebViewDidEndEditingNotification"));
221 m_page->injectedBundleEditorClient().didEndEditing(m_page, WebViewDidEndEditingNotification.impl());
225 void WebEditorClient::didWriteSelectionToPasteboard()
230 void WebEditorClient::didSetSelectionTypesForPasteboard()
235 void WebEditorClient::registerUndoStep(PassRefPtr<UndoStep> step)
237 // FIXME: Add assertion that the command being reapplied is the same command that is
238 // being passed to us.
239 if (m_page->isInRedo())
242 RefPtr<WebUndoStep> webStep = WebUndoStep::create(step);
243 m_page->addWebUndoStep(webStep->stepID(), webStep.get());
244 uint32_t editAction = static_cast<uint32_t>(webStep->step()->editingAction());
246 m_page->send(Messages::WebPageProxy::RegisterEditCommandForUndo(webStep->stepID(), editAction));
249 void WebEditorClient::registerRedoStep(PassRefPtr<UndoStep>)
253 void WebEditorClient::clearUndoRedoOperations()
255 m_page->send(Messages::WebPageProxy::ClearAllEditCommands());
258 bool WebEditorClient::canCopyCut(Frame*, bool defaultValue) const
263 bool WebEditorClient::canPaste(Frame*, bool defaultValue) const
268 bool WebEditorClient::canUndo() const
271 m_page->sendSync(Messages::WebPageProxy::CanUndoRedo(static_cast<uint32_t>(WebPageProxy::Undo)), Messages::WebPageProxy::CanUndoRedo::Reply(result));
275 bool WebEditorClient::canRedo() const
278 m_page->sendSync(Messages::WebPageProxy::CanUndoRedo(static_cast<uint32_t>(WebPageProxy::Redo)), Messages::WebPageProxy::CanUndoRedo::Reply(result));
282 void WebEditorClient::undo()
285 m_page->sendSync(Messages::WebPageProxy::ExecuteUndoRedo(static_cast<uint32_t>(WebPageProxy::Undo)), Messages::WebPageProxy::ExecuteUndoRedo::Reply(result));
288 void WebEditorClient::redo()
291 m_page->sendSync(Messages::WebPageProxy::ExecuteUndoRedo(static_cast<uint32_t>(WebPageProxy::Redo)), Messages::WebPageProxy::ExecuteUndoRedo::Reply(result));
294 #if !PLATFORM(GTK) && !PLATFORM(MAC) && !PLATFORM(EFL)
295 void WebEditorClient::handleKeyboardEvent(KeyboardEvent* event)
297 if (m_page->handleEditingKeyboardEvent(event))
298 event->setDefaultHandled();
301 void WebEditorClient::handleInputMethodKeydown(KeyboardEvent*)
307 void WebEditorClient::textFieldDidBeginEditing(Element* element)
309 if (!element->hasTagName(inputTag))
312 WebFrame* webFrame = static_cast<WebFrameLoaderClient*>(element->document()->frame()->loader()->client())->webFrame();
313 m_page->injectedBundleFormClient().textFieldDidBeginEditing(m_page, static_cast<HTMLInputElement*>(element), webFrame);
316 void WebEditorClient::textFieldDidEndEditing(Element* element)
318 if (!element->hasTagName(inputTag))
321 WebFrame* webFrame = static_cast<WebFrameLoaderClient*>(element->document()->frame()->loader()->client())->webFrame();
322 m_page->injectedBundleFormClient().textFieldDidEndEditing(m_page, static_cast<HTMLInputElement*>(element), webFrame);
325 void WebEditorClient::textDidChangeInTextField(Element* element)
327 if (!element->hasTagName(inputTag))
330 #if ENABLE(TIZEN_WEBKIT2_FORM_DATABASE)
331 HTMLInputElement* inputElement = static_cast<HTMLInputElement*>(element);
332 if (inputElement->shouldAutocomplete() && !(inputElement->isPasswordField()))
333 m_page->send(Messages::WebPageProxy::TextChangeInTextField(inputElement->name(), inputElement->value()));
336 if (!UserTypingGestureIndicator::processingUserTypingGesture() || UserTypingGestureIndicator::focusedElementAtGestureStart() != element)
339 WebFrame* webFrame = static_cast<WebFrameLoaderClient*>(element->document()->frame()->loader()->client())->webFrame();
340 m_page->injectedBundleFormClient().textDidChangeInTextField(m_page, static_cast<HTMLInputElement*>(element), webFrame);
343 void WebEditorClient::textDidChangeInTextArea(Element* element)
345 if (!element->hasTagName(textareaTag))
348 WebFrame* webFrame = static_cast<WebFrameLoaderClient*>(element->document()->frame()->loader()->client())->webFrame();
349 m_page->injectedBundleFormClient().textDidChangeInTextArea(m_page, static_cast<HTMLTextAreaElement*>(element), webFrame);
352 static bool getActionTypeForKeyEvent(KeyboardEvent* event, WKInputFieldActionType& type)
354 String key = event->keyIdentifier();
356 type = WKInputFieldActionTypeMoveUp;
357 else if (key == "Down")
358 type = WKInputFieldActionTypeMoveDown;
359 else if (key == "U+001B")
360 type = WKInputFieldActionTypeCancel;
361 else if (key == "U+0009") {
362 if (event->shiftKey())
363 type = WKInputFieldActionTypeInsertBacktab;
365 type = WKInputFieldActionTypeInsertTab;
366 } else if (key == "Enter")
367 type = WKInputFieldActionTypeInsertNewline;
374 bool WebEditorClient::doTextFieldCommandFromEvent(Element* element, KeyboardEvent* event)
376 if (!element->hasTagName(inputTag))
379 WKInputFieldActionType actionType = static_cast<WKInputFieldActionType>(0);
380 if (!getActionTypeForKeyEvent(event, actionType))
383 WebFrame* webFrame = static_cast<WebFrameLoaderClient*>(element->document()->frame()->loader()->client())->webFrame();
384 return m_page->injectedBundleFormClient().shouldPerformActionInTextField(m_page, static_cast<HTMLInputElement*>(element), actionType, webFrame);
387 void WebEditorClient::textWillBeDeletedInTextField(Element* element)
389 if (!element->hasTagName(inputTag))
392 WebFrame* webFrame = static_cast<WebFrameLoaderClient*>(element->document()->frame()->loader()->client())->webFrame();
393 m_page->injectedBundleFormClient().shouldPerformActionInTextField(m_page, static_cast<HTMLInputElement*>(element), WKInputFieldActionTypeInsertDelete, webFrame);
396 bool WebEditorClient::shouldEraseMarkersAfterChangeSelection(WebCore::TextCheckingType type) const
398 // This prevents erasing spelling markers on OS X Lion or later to match AppKit on these Mac OS X versions.
399 #if PLATFORM(MAC) && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070
400 return type != TextCheckingTypeSpelling;
406 void WebEditorClient::ignoreWordInSpellDocument(const String& word)
408 m_page->send(Messages::WebPageProxy::IgnoreWord(word));
411 void WebEditorClient::learnWord(const String& word)
413 m_page->send(Messages::WebPageProxy::LearnWord(word));
416 void WebEditorClient::checkSpellingOfString(const UChar* text, int length, int* misspellingLocation, int* misspellingLength)
418 int32_t resultLocation = -1;
419 int32_t resultLength = 0;
420 // FIXME: It would be nice if we wouldn't have to copy the text here.
421 m_page->sendSync(Messages::WebPageProxy::CheckSpellingOfString(String(text, length)),
422 Messages::WebPageProxy::CheckSpellingOfString::Reply(resultLocation, resultLength));
423 *misspellingLocation = resultLocation;
424 *misspellingLength = resultLength;
427 String WebEditorClient::getAutoCorrectSuggestionForMisspelledWord(const String&)
433 void WebEditorClient::checkGrammarOfString(const UChar* text, int length, Vector<WebCore::GrammarDetail>& grammarDetails, int* badGrammarLocation, int* badGrammarLength)
435 int32_t resultLocation = -1;
436 int32_t resultLength = 0;
437 // FIXME: It would be nice if we wouldn't have to copy the text here.
438 m_page->sendSync(Messages::WebPageProxy::CheckGrammarOfString(String(text, length)),
439 Messages::WebPageProxy::CheckGrammarOfString::Reply(grammarDetails, resultLocation, resultLength));
440 *badGrammarLocation = resultLocation;
441 *badGrammarLength = resultLength;
444 void WebEditorClient::updateSpellingUIWithGrammarString(const String& badGrammarPhrase, const GrammarDetail& grammarDetail)
446 m_page->send(Messages::WebPageProxy::UpdateSpellingUIWithGrammarString(badGrammarPhrase, grammarDetail));
449 void WebEditorClient::updateSpellingUIWithMisspelledWord(const String& misspelledWord)
451 m_page->send(Messages::WebPageProxy::UpdateSpellingUIWithMisspelledWord(misspelledWord));
454 void WebEditorClient::showSpellingUI(bool)
459 bool WebEditorClient::spellingUIIsShowing()
461 bool isShowing = false;
462 m_page->sendSync(Messages::WebPageProxy::SpellingUIIsShowing(), Messages::WebPageProxy::SpellingUIIsShowing::Reply(isShowing));
466 void WebEditorClient::getGuessesForWord(const String& word, const String& context, Vector<String>& guesses)
468 m_page->sendSync(Messages::WebPageProxy::GetGuessesForWord(word, context), Messages::WebPageProxy::GetGuessesForWord::Reply(guesses));
471 void WebEditorClient::willSetInputMethodState()
476 void WebEditorClient::setInputMethodState(bool active)
481 void WebEditorClient::requestCheckingOfString(WTF::PassRefPtr<WebCore::TextCheckingRequest>)
486 } // namespace WebKit