2 * Copyright (C) 2006, 2007 Apple, Inc. All rights reserved.
3 * Copyright (C) 2010 Google, Inc. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include "EditorClientImpl.h"
31 #include "EditCommand.h"
33 #include "EventHandler.h"
34 #include "EventNames.h"
36 #include "HTMLInputElement.h"
37 #include "HTMLNames.h"
38 #include "KeyboardCodes.h"
39 #include "KeyboardEvent.h"
40 #include "PlatformKeyboardEvent.h"
41 #include "PlatformString.h"
42 #include "RenderObject.h"
43 #include "SpellChecker.h"
45 #include "DOMUtilitiesPrivate.h"
46 #include "WebAutofillClient.h"
47 #include "WebEditingAction.h"
48 #include "WebElement.h"
49 #include "WebFrameClient.h"
50 #include "WebFrameImpl.h"
52 #include "WebInputElement.h"
53 #include "WebInputEventConversion.h"
55 #include "WebPasswordAutocompleteListener.h"
56 #include "WebPermissionClient.h"
58 #include "WebSpellCheckClient.h"
59 #include "WebTextAffinity.h"
60 #include "WebTextCheckingCompletionImpl.h"
61 #include "WebViewClient.h"
62 #include "WebViewImpl.h"
64 using namespace WebCore;
68 // Arbitrary depth limit for the undo stack, to keep it from using
69 // unbounded memory. This is the maximum number of distinct undoable
70 // actions -- unbroken stretches of typed characters are coalesced
71 // into a single action.
72 static const size_t maximumUndoStackDepth = 1000;
74 // The size above which we stop triggering autofill for an input text field
75 // (so to avoid sending long strings through IPC).
76 static const size_t maximumTextSizeForAutofill = 1000;
78 EditorClientImpl::EditorClientImpl(WebViewImpl* webview)
81 , m_backspaceOrDeletePressed(false)
82 , m_spellCheckThisFieldStatus(SpellCheckAutomatic)
83 , m_autofillTimer(this, &EditorClientImpl::doAutofill)
87 EditorClientImpl::~EditorClientImpl()
91 void EditorClientImpl::pageDestroyed()
93 // Our lifetime is bound to the WebViewImpl.
96 bool EditorClientImpl::shouldShowDeleteInterface(HTMLElement* elem)
98 // Normally, we don't care to show WebCore's deletion UI, so we only enable
99 // it if in testing mode and the test specifically requests it by using this
101 return layoutTestMode()
102 && elem->getAttribute(HTMLNames::classAttr) == "needsDeletionUI";
105 bool EditorClientImpl::smartInsertDeleteEnabled()
107 if (m_webView->client())
108 return m_webView->client()->isSmartInsertDeleteEnabled();
112 bool EditorClientImpl::isSelectTrailingWhitespaceEnabled()
114 if (m_webView->client())
115 return m_webView->client()->isSelectTrailingWhitespaceEnabled();
123 bool EditorClientImpl::shouldSpellcheckByDefault()
125 // Spellcheck should be enabled for all editable areas (such as textareas,
126 // contentEditable regions, and designMode docs), except text inputs.
127 const Frame* frame = m_webView->focusedWebCoreFrame();
130 const Editor* editor = frame->editor();
133 if (editor->isSpellCheckingEnabledInFocusedNode())
135 const Document* document = frame->document();
138 const Node* node = document->focusedNode();
139 // If |node| is null, we default to allowing spellchecking. This is done in
140 // order to mitigate the issue when the user clicks outside the textbox, as a
141 // result of which |node| becomes null, resulting in all the spell check
142 // markers being deleted. Also, the Frame will decide not to do spellchecking
143 // if the user can't edit - so returning true here will not cause any problems
144 // to the Frame's behavior.
147 const RenderObject* renderer = node->renderer();
151 return !renderer->isTextField();
154 bool EditorClientImpl::isContinuousSpellCheckingEnabled()
156 if (m_spellCheckThisFieldStatus == SpellCheckForcedOff)
158 if (m_spellCheckThisFieldStatus == SpellCheckForcedOn)
160 return shouldSpellcheckByDefault();
163 void EditorClientImpl::toggleContinuousSpellChecking()
165 if (isContinuousSpellCheckingEnabled())
166 m_spellCheckThisFieldStatus = SpellCheckForcedOff;
168 m_spellCheckThisFieldStatus = SpellCheckForcedOn;
171 bool EditorClientImpl::isGrammarCheckingEnabled()
176 void EditorClientImpl::toggleGrammarChecking()
181 int EditorClientImpl::spellCheckerDocumentTag()
183 ASSERT_NOT_REACHED();
187 bool EditorClientImpl::shouldBeginEditing(Range* range)
189 if (m_webView->client())
190 return m_webView->client()->shouldBeginEditing(WebRange(range));
194 bool EditorClientImpl::shouldEndEditing(Range* range)
196 if (m_webView->client())
197 return m_webView->client()->shouldEndEditing(WebRange(range));
201 bool EditorClientImpl::shouldInsertNode(Node* node,
203 EditorInsertAction action)
205 if (m_webView->client()) {
206 return m_webView->client()->shouldInsertNode(WebNode(node),
208 static_cast<WebEditingAction>(action));
213 bool EditorClientImpl::shouldInsertText(const String& text,
215 EditorInsertAction action)
217 if (m_webView->client()) {
218 return m_webView->client()->shouldInsertText(WebString(text),
220 static_cast<WebEditingAction>(action));
226 bool EditorClientImpl::shouldDeleteRange(Range* range)
228 if (m_webView->client())
229 return m_webView->client()->shouldDeleteRange(WebRange(range));
233 bool EditorClientImpl::shouldChangeSelectedRange(Range* fromRange,
238 if (m_webView->client()) {
239 return m_webView->client()->shouldChangeSelectedRange(WebRange(fromRange),
241 static_cast<WebTextAffinity>(affinity),
247 bool EditorClientImpl::shouldApplyStyle(CSSStyleDeclaration* style,
250 if (m_webView->client()) {
251 // FIXME: Pass a reference to the CSSStyleDeclaration somehow.
252 return m_webView->client()->shouldApplyStyle(WebString(),
258 bool EditorClientImpl::shouldMoveRangeAfterDelete(Range* range,
259 Range* rangeToBeReplaced)
264 void EditorClientImpl::didBeginEditing()
266 if (m_webView->client())
267 m_webView->client()->didBeginEditing();
270 void EditorClientImpl::respondToChangedSelection()
272 if (m_webView->client()) {
273 Frame* frame = m_webView->focusedWebCoreFrame();
275 m_webView->client()->didChangeSelection(!frame->selection()->isRange());
279 void EditorClientImpl::respondToChangedContents()
281 if (m_webView->client())
282 m_webView->client()->didChangeContents();
285 void EditorClientImpl::didEndEditing()
287 if (m_webView->client())
288 m_webView->client()->didEndEditing();
291 void EditorClientImpl::didWriteSelectionToPasteboard()
295 void EditorClientImpl::didSetSelectionTypesForPasteboard()
299 void EditorClientImpl::registerCommandForUndo(PassRefPtr<EditCommand> command)
301 if (m_undoStack.size() == maximumUndoStackDepth)
302 m_undoStack.removeFirst(); // drop oldest item off the far end
305 m_undoStack.append(command);
308 void EditorClientImpl::registerCommandForRedo(PassRefPtr<EditCommand> command)
310 m_redoStack.append(command);
313 void EditorClientImpl::clearUndoRedoOperations()
319 bool EditorClientImpl::canCopyCut(Frame* frame, bool defaultValue) const
321 if (!m_webView->permissionClient())
323 return m_webView->permissionClient()->allowWriteToClipboard(WebFrameImpl::fromFrame(frame), defaultValue);
326 bool EditorClientImpl::canPaste(Frame* frame, bool defaultValue) const
328 if (!m_webView->permissionClient())
330 return m_webView->permissionClient()->allowReadFromClipboard(WebFrameImpl::fromFrame(frame), defaultValue);
333 bool EditorClientImpl::canUndo() const
335 return !m_undoStack.isEmpty();
338 bool EditorClientImpl::canRedo() const
340 return !m_redoStack.isEmpty();
343 void EditorClientImpl::undo()
346 EditCommandStack::iterator back = --m_undoStack.end();
347 RefPtr<EditCommand> command(*back);
348 m_undoStack.remove(back);
350 // unapply will call us back to push this command onto the redo stack.
354 void EditorClientImpl::redo()
357 EditCommandStack::iterator back = --m_redoStack.end();
358 RefPtr<EditCommand> command(*back);
359 m_redoStack.remove(back);
364 // reapply will call us back to push this command onto the undo stack.
370 // The below code was adapted from the WebKit file webview.cpp
373 static const unsigned CtrlKey = 1 << 0;
374 static const unsigned AltKey = 1 << 1;
375 static const unsigned ShiftKey = 1 << 2;
376 static const unsigned MetaKey = 1 << 3;
378 // Aliases for the generic key defintions to make kbd shortcuts definitions more
380 static const unsigned OptionKey = AltKey;
382 // Do not use this constant for anything but cursor movement commands. Keys
383 // with cmd set have their |isSystemKey| bit set, so chances are the shortcut
384 // will not be executed. Another, less important, reason is that shortcuts
385 // defined in the renderer do not blink the menu item that they triggered. See
386 // http://crbug.com/25856 and the bugs linked from there for details.
387 static const unsigned CommandKey = MetaKey;
390 // Keys with special meaning. These will be delegated to the editor using
391 // the execCommand() method
392 struct KeyDownEntry {
398 struct KeyPressEntry {
404 static const KeyDownEntry keyDownEntries[] = {
405 { VKEY_LEFT, 0, "MoveLeft" },
406 { VKEY_LEFT, ShiftKey, "MoveLeftAndModifySelection" },
408 { VKEY_LEFT, OptionKey, "MoveWordLeft" },
409 { VKEY_LEFT, OptionKey | ShiftKey,
410 "MoveWordLeftAndModifySelection" },
412 { VKEY_LEFT, CtrlKey, "MoveWordLeft" },
413 { VKEY_LEFT, CtrlKey | ShiftKey,
414 "MoveWordLeftAndModifySelection" },
416 { VKEY_RIGHT, 0, "MoveRight" },
417 { VKEY_RIGHT, ShiftKey, "MoveRightAndModifySelection" },
419 { VKEY_RIGHT, OptionKey, "MoveWordRight" },
420 { VKEY_RIGHT, OptionKey | ShiftKey,
421 "MoveWordRightAndModifySelection" },
423 { VKEY_RIGHT, CtrlKey, "MoveWordRight" },
424 { VKEY_RIGHT, CtrlKey | ShiftKey,
425 "MoveWordRightAndModifySelection" },
427 { VKEY_UP, 0, "MoveUp" },
428 { VKEY_UP, ShiftKey, "MoveUpAndModifySelection" },
429 { VKEY_PRIOR, ShiftKey, "MovePageUpAndModifySelection" },
430 { VKEY_DOWN, 0, "MoveDown" },
431 { VKEY_DOWN, ShiftKey, "MoveDownAndModifySelection" },
432 { VKEY_NEXT, ShiftKey, "MovePageDownAndModifySelection" },
434 { VKEY_PRIOR, 0, "MovePageUp" },
435 { VKEY_NEXT, 0, "MovePageDown" },
437 { VKEY_HOME, 0, "MoveToBeginningOfLine" },
438 { VKEY_HOME, ShiftKey,
439 "MoveToBeginningOfLineAndModifySelection" },
441 { VKEY_LEFT, CommandKey, "MoveToBeginningOfLine" },
442 { VKEY_LEFT, CommandKey | ShiftKey,
443 "MoveToBeginningOfLineAndModifySelection" },
444 { VKEY_PRIOR, OptionKey, "MovePageUp" },
445 { VKEY_NEXT, OptionKey, "MovePageDown" },
448 { VKEY_UP, CommandKey, "MoveToBeginningOfDocument" },
449 { VKEY_UP, CommandKey | ShiftKey,
450 "MoveToBeginningOfDocumentAndModifySelection" },
452 { VKEY_HOME, CtrlKey, "MoveToBeginningOfDocument" },
453 { VKEY_HOME, CtrlKey | ShiftKey,
454 "MoveToBeginningOfDocumentAndModifySelection" },
456 { VKEY_END, 0, "MoveToEndOfLine" },
457 { VKEY_END, ShiftKey, "MoveToEndOfLineAndModifySelection" },
459 { VKEY_DOWN, CommandKey, "MoveToEndOfDocument" },
460 { VKEY_DOWN, CommandKey | ShiftKey,
461 "MoveToEndOfDocumentAndModifySelection" },
463 { VKEY_END, CtrlKey, "MoveToEndOfDocument" },
464 { VKEY_END, CtrlKey | ShiftKey,
465 "MoveToEndOfDocumentAndModifySelection" },
468 { VKEY_RIGHT, CommandKey, "MoveToEndOfLine" },
469 { VKEY_RIGHT, CommandKey | ShiftKey,
470 "MoveToEndOfLineAndModifySelection" },
472 { VKEY_BACK, 0, "DeleteBackward" },
473 { VKEY_BACK, ShiftKey, "DeleteBackward" },
474 { VKEY_DELETE, 0, "DeleteForward" },
476 { VKEY_BACK, OptionKey, "DeleteWordBackward" },
477 { VKEY_DELETE, OptionKey, "DeleteWordForward" },
479 { VKEY_BACK, CtrlKey, "DeleteWordBackward" },
480 { VKEY_DELETE, CtrlKey, "DeleteWordForward" },
482 { 'B', CtrlKey, "ToggleBold" },
483 { 'I', CtrlKey, "ToggleItalic" },
484 { 'U', CtrlKey, "ToggleUnderline" },
485 { VKEY_ESCAPE, 0, "Cancel" },
486 { VKEY_OEM_PERIOD, CtrlKey, "Cancel" },
487 { VKEY_TAB, 0, "InsertTab" },
488 { VKEY_TAB, ShiftKey, "InsertBacktab" },
489 { VKEY_RETURN, 0, "InsertNewline" },
490 { VKEY_RETURN, CtrlKey, "InsertNewline" },
491 { VKEY_RETURN, AltKey, "InsertNewline" },
492 { VKEY_RETURN, AltKey | ShiftKey, "InsertNewline" },
493 { VKEY_RETURN, ShiftKey, "InsertLineBreak" },
494 { VKEY_INSERT, CtrlKey, "Copy" },
495 { VKEY_INSERT, ShiftKey, "Paste" },
496 { VKEY_DELETE, ShiftKey, "Cut" },
498 // On OS X, we pipe these back to the browser, so that it can do menu item
500 { 'C', CtrlKey, "Copy" },
501 { 'V', CtrlKey, "Paste" },
502 { 'V', CtrlKey | ShiftKey, "PasteAndMatchStyle" },
503 { 'X', CtrlKey, "Cut" },
504 { 'A', CtrlKey, "SelectAll" },
505 { 'Z', CtrlKey, "Undo" },
506 { 'Z', CtrlKey | ShiftKey, "Redo" },
507 { 'Y', CtrlKey, "Redo" },
511 static const KeyPressEntry keyPressEntries[] = {
512 { '\t', 0, "InsertTab" },
513 { '\t', ShiftKey, "InsertBacktab" },
514 { '\r', 0, "InsertNewline" },
515 { '\r', CtrlKey, "InsertNewline" },
516 { '\r', ShiftKey, "InsertLineBreak" },
517 { '\r', AltKey, "InsertNewline" },
518 { '\r', AltKey | ShiftKey, "InsertNewline" },
521 const char* EditorClientImpl::interpretKeyEvent(const KeyboardEvent* evt)
523 const PlatformKeyboardEvent* keyEvent = evt->keyEvent();
527 static HashMap<int, const char*>* keyDownCommandsMap = 0;
528 static HashMap<int, const char*>* keyPressCommandsMap = 0;
530 if (!keyDownCommandsMap) {
531 keyDownCommandsMap = new HashMap<int, const char*>;
532 keyPressCommandsMap = new HashMap<int, const char*>;
534 for (unsigned i = 0; i < arraysize(keyDownEntries); i++) {
535 keyDownCommandsMap->set(keyDownEntries[i].modifiers << 16 | keyDownEntries[i].virtualKey,
536 keyDownEntries[i].name);
539 for (unsigned i = 0; i < arraysize(keyPressEntries); i++) {
540 keyPressCommandsMap->set(keyPressEntries[i].modifiers << 16 | keyPressEntries[i].charCode,
541 keyPressEntries[i].name);
545 unsigned modifiers = 0;
546 if (keyEvent->shiftKey())
547 modifiers |= ShiftKey;
548 if (keyEvent->altKey())
550 if (keyEvent->ctrlKey())
551 modifiers |= CtrlKey;
552 if (keyEvent->metaKey())
553 modifiers |= MetaKey;
555 if (keyEvent->type() == PlatformKeyboardEvent::RawKeyDown) {
556 int mapKey = modifiers << 16 | evt->keyCode();
557 return mapKey ? keyDownCommandsMap->get(mapKey) : 0;
560 int mapKey = modifiers << 16 | evt->charCode();
561 return mapKey ? keyPressCommandsMap->get(mapKey) : 0;
564 bool EditorClientImpl::handleEditingKeyboardEvent(KeyboardEvent* evt)
566 const PlatformKeyboardEvent* keyEvent = evt->keyEvent();
567 // do not treat this as text input if it's a system key event
568 if (!keyEvent || keyEvent->isSystemKey())
571 Frame* frame = evt->target()->toNode()->document()->frame();
575 String commandName = interpretKeyEvent(evt);
576 Editor::Command command = frame->editor()->command(commandName);
578 if (keyEvent->type() == PlatformKeyboardEvent::RawKeyDown) {
579 // WebKit doesn't have enough information about mode to decide how
580 // commands that just insert text if executed via Editor should be treated,
581 // so we leave it upon WebCore to either handle them immediately
582 // (e.g. Tab that changes focus) or let a keypress event be generated
583 // (e.g. Tab that inserts a Tab character, or Enter).
584 if (command.isTextInsertion() || commandName.isEmpty())
586 if (command.execute(evt)) {
587 if (m_webView->client())
588 m_webView->client()->didExecuteCommand(WebString(commandName));
594 if (command.execute(evt)) {
595 if (m_webView->client())
596 m_webView->client()->didExecuteCommand(WebString(commandName));
600 // Here we need to filter key events.
601 // On Gtk/Linux, it emits key events with ASCII text and ctrl on for ctrl-<x>.
602 // In Webkit, EditorClient::handleKeyboardEvent in
603 // WebKit/gtk/WebCoreSupport/EditorClientGtk.cpp drop such events.
604 // On Mac, it emits key events with ASCII text and meta on for Command-<x>.
605 // These key events should not emit text insert event.
606 // Alt key would be used to insert alternative character, so we should let
607 // through. Also note that Ctrl-Alt combination equals to AltGr key which is
608 // also used to insert alternative character.
609 // http://code.google.com/p/chromium/issues/detail?id=10846
610 // Windows sets both alt and meta are on when "Alt" key pressed.
611 // http://code.google.com/p/chromium/issues/detail?id=2215
612 // Also, we should not rely on an assumption that keyboards don't
613 // send ASCII characters when pressing a control key on Windows,
614 // which may be configured to do it so by user.
615 // See also http://en.wikipedia.org/wiki/Keyboard_Layout
616 // FIXME(ukai): investigate more detail for various keyboard layout.
617 if (evt->keyEvent()->text().length() == 1) {
618 UChar ch = evt->keyEvent()->text()[0U];
620 // Don't insert null or control characters as they can result in
621 // unexpected behaviour
625 // Don't insert ASCII character if ctrl w/o alt or meta is on.
626 // On Mac, we should ignore events when meta is on (Command-<x>).
628 if (evt->keyEvent()->ctrlKey() && !evt->keyEvent()->altKey())
631 if (evt->keyEvent()->metaKey())
638 if (!frame->editor()->canEdit())
641 return frame->editor()->insertText(evt->keyEvent()->text(), evt);
644 void EditorClientImpl::handleKeyboardEvent(KeyboardEvent* evt)
646 if (evt->keyCode() == VKEY_DOWN
647 || evt->keyCode() == VKEY_UP) {
648 ASSERT(evt->target()->toNode());
649 showFormAutofillForNode(evt->target()->toNode());
652 // Give the embedder a chance to handle the keyboard event.
653 if ((m_webView->client()
654 && m_webView->client()->handleCurrentKeyboardEvent())
655 || handleEditingKeyboardEvent(evt))
656 evt->setDefaultHandled();
659 void EditorClientImpl::handleInputMethodKeydown(KeyboardEvent* keyEvent)
661 // We handle IME within chrome.
664 void EditorClientImpl::textFieldDidBeginEditing(Element* element)
666 HTMLInputElement* inputElement = toHTMLInputElement(element);
667 if (m_webView->autofillClient() && inputElement)
668 m_webView->autofillClient()->textFieldDidBeginEditing(WebInputElement(inputElement));
671 void EditorClientImpl::textFieldDidEndEditing(Element* element)
673 HTMLInputElement* inputElement = toHTMLInputElement(element);
674 if (m_webView->autofillClient() && inputElement)
675 m_webView->autofillClient()->textFieldDidEndEditing(WebInputElement(inputElement));
677 // Notification that focus was lost. Be careful with this, it's also sent
678 // when the page is being closed.
680 // Cancel any pending DoAutofill call.
681 m_autofillArgs.clear();
682 m_autofillTimer.stop();
684 // Hide any showing popup.
685 m_webView->hideAutofillPopup();
687 if (!m_webView->client())
688 return; // The page is getting closed, don't fill the password.
690 // Notify any password-listener of the focus change.
694 WebFrameImpl* webframe = WebFrameImpl::fromFrame(inputElement->document()->frame());
698 WebPasswordAutocompleteListener* listener = webframe->getPasswordListener(inputElement);
702 listener->didBlurInputElement(inputElement->value());
705 void EditorClientImpl::textDidChangeInTextField(Element* element)
707 ASSERT(element->hasLocalName(HTMLNames::inputTag));
708 HTMLInputElement* inputElement = static_cast<HTMLInputElement*>(element);
709 if (m_webView->autofillClient())
710 m_webView->autofillClient()->textFieldDidChange(WebInputElement(inputElement));
712 // Note that we only show the autofill popup in this case if the caret is at
713 // the end. This matches FireFox and Safari but not IE.
714 autofill(inputElement, false, false, true);
717 bool EditorClientImpl::showFormAutofillForNode(Node* node)
719 HTMLInputElement* inputElement = toHTMLInputElement(node);
721 return autofill(inputElement, true, true, false);
725 bool EditorClientImpl::autofill(HTMLInputElement* inputElement,
726 bool autofillFormOnly,
727 bool autofillOnEmptyValue,
728 bool requireCaretAtEnd)
730 // Cancel any pending DoAutofill call.
731 m_autofillArgs.clear();
732 m_autofillTimer.stop();
734 // Let's try to trigger autofill for that field, if applicable.
735 if (!inputElement->isEnabledFormControl() || !inputElement->isTextField()
736 || inputElement->isPasswordField() || !inputElement->shouldAutocomplete()
737 || inputElement->isReadOnlyFormControl())
740 WebString name = WebInputElement(inputElement).nameForAutofill();
741 if (name.isEmpty()) // If the field has no name, then we won't have values.
744 // Don't attempt to autofill with values that are too large.
745 if (inputElement->value().length() > maximumTextSizeForAutofill)
748 m_autofillArgs = adoptPtr(new AutofillArgs);
749 m_autofillArgs->inputElement = inputElement;
750 m_autofillArgs->autofillFormOnly = autofillFormOnly;
751 m_autofillArgs->autofillOnEmptyValue = autofillOnEmptyValue;
752 m_autofillArgs->requireCaretAtEnd = requireCaretAtEnd;
753 m_autofillArgs->backspaceOrDeletePressed = m_backspaceOrDeletePressed;
755 if (!requireCaretAtEnd)
758 // We post a task for doing the autofill as the caret position is not set
759 // properly at this point (http://bugs.webkit.org/show_bug.cgi?id=16976)
760 // and we need it to determine whether or not to trigger autofill.
761 m_autofillTimer.startOneShot(0.0);
766 void EditorClientImpl::doAutofill(Timer<EditorClientImpl>* timer)
768 OwnPtr<AutofillArgs> args(m_autofillArgs.release());
769 HTMLInputElement* inputElement = args->inputElement.get();
771 const String& value = inputElement->value();
773 // Enforce autofill_on_empty_value and caret_at_end.
775 bool isCaretAtEnd = true;
776 if (args->requireCaretAtEnd)
777 isCaretAtEnd = inputElement->selectionStart() == inputElement->selectionEnd()
778 && inputElement->selectionEnd() == static_cast<int>(value.length());
780 if ((!args->autofillOnEmptyValue && value.isEmpty()) || !isCaretAtEnd) {
781 m_webView->hideAutofillPopup();
785 // First let's see if there is a password listener for that element.
786 // We won't trigger form autofill in that case, as having both behavior on
787 // a node would be confusing.
788 WebFrameImpl* webframe = WebFrameImpl::fromFrame(inputElement->document()->frame());
791 WebPasswordAutocompleteListener* listener = webframe->getPasswordListener(inputElement);
793 if (args->autofillFormOnly)
796 listener->performInlineAutocomplete(value,
797 args->backspaceOrDeletePressed,
803 void EditorClientImpl::cancelPendingAutofill()
805 m_autofillArgs.clear();
806 m_autofillTimer.stop();
809 bool EditorClientImpl::doTextFieldCommandFromEvent(Element* element,
810 KeyboardEvent* event)
812 HTMLInputElement* inputElement = toHTMLInputElement(element);
813 if (m_webView->autofillClient() && inputElement) {
814 m_webView->autofillClient()->textFieldDidReceiveKeyDown(WebInputElement(inputElement),
815 WebKeyboardEventBuilder(*event));
818 // Remember if backspace was pressed for the autofill. It is not clear how to
819 // find if backspace was pressed from textFieldDidBeginEditing and
820 // textDidChangeInTextField as when these methods are called the value of the
821 // input element already contains the type character.
822 m_backspaceOrDeletePressed = event->keyCode() == VKEY_BACK || event->keyCode() == VKEY_DELETE;
824 // The Mac code appears to use this method as a hook to implement special
825 // keyboard commands specific to Safari's auto-fill implementation. We
826 // just return false to allow the default action.
830 void EditorClientImpl::textWillBeDeletedInTextField(Element*)
834 void EditorClientImpl::textDidChangeInTextArea(Element*)
838 void EditorClientImpl::ignoreWordInSpellDocument(const String&)
843 void EditorClientImpl::learnWord(const String&)
848 void EditorClientImpl::checkSpellingOfString(const UChar* text, int length,
849 int* misspellingLocation,
850 int* misspellingLength)
852 // SpellCheckWord will write (0, 0) into the output vars, which is what our
853 // caller expects if the word is spelled correctly.
854 int spellLocation = -1;
857 // Check to see if the provided text is spelled correctly.
858 if (isContinuousSpellCheckingEnabled() && m_webView->spellCheckClient())
859 m_webView->spellCheckClient()->spellCheck(WebString(text, length), spellLocation, spellLength, 0);
865 // Note: the Mac code checks if the pointers are null before writing to them,
867 if (misspellingLocation)
868 *misspellingLocation = spellLocation;
869 if (misspellingLength)
870 *misspellingLength = spellLength;
873 void EditorClientImpl::requestCheckingOfString(SpellChecker* sender, int identifier, TextCheckingTypeMask, const String& text)
875 if (m_webView->spellCheckClient())
876 m_webView->spellCheckClient()->requestCheckingOfText(text, new WebTextCheckingCompletionImpl(identifier, sender));
879 String EditorClientImpl::getAutoCorrectSuggestionForMisspelledWord(const String& misspelledWord)
881 if (!(isContinuousSpellCheckingEnabled() && m_webView->client()))
884 // Do not autocorrect words with capital letters in it except the
885 // first letter. This will remove cases changing "IMB" to "IBM".
886 for (size_t i = 1; i < misspelledWord.length(); i++) {
887 if (u_isupper(static_cast<UChar32>(misspelledWord[i])))
891 if (m_webView->spellCheckClient())
892 return m_webView->spellCheckClient()->autoCorrectWord(WebString(misspelledWord));
896 void EditorClientImpl::checkGrammarOfString(const UChar*, int length,
897 WTF::Vector<GrammarDetail>&,
898 int* badGrammarLocation,
899 int* badGrammarLength)
902 if (badGrammarLocation)
903 *badGrammarLocation = 0;
904 if (badGrammarLength)
905 *badGrammarLength = 0;
908 void EditorClientImpl::updateSpellingUIWithGrammarString(const String&,
909 const GrammarDetail& detail)
914 void EditorClientImpl::updateSpellingUIWithMisspelledWord(const String& misspelledWord)
916 if (m_webView->spellCheckClient())
917 m_webView->spellCheckClient()->updateSpellingUIWithMisspelledWord(WebString(misspelledWord));
920 void EditorClientImpl::showSpellingUI(bool show)
922 if (m_webView->spellCheckClient())
923 m_webView->spellCheckClient()->showSpellingUI(show);
926 bool EditorClientImpl::spellingUIIsShowing()
928 if (m_webView->spellCheckClient())
929 return m_webView->spellCheckClient()->isShowingSpellingUI();
933 void EditorClientImpl::getGuessesForWord(const String& word,
934 const String& context,
935 WTF::Vector<String>& guesses)
940 void EditorClientImpl::willSetInputMethodState()
942 if (m_webView->client())
943 m_webView->client()->resetInputMethod();
946 void EditorClientImpl::setInputMethodState(bool)