EditorClientImpl.cpp [plain text]
#include "config.h"
#include "EditorClientImpl.h"
#include "Document.h"
#include "EditCommand.h"
#include "Editor.h"
#include "EventHandler.h"
#include "EventNames.h"
#include "Frame.h"
#include "HTMLInputElement.h"
#include "HTMLNames.h"
#include "KeyboardCodes.h"
#include "KeyboardEvent.h"
#include "PlatformKeyboardEvent.h"
#include "PlatformString.h"
#include "RenderObject.h"
#include "DOMUtilitiesPrivate.h"
#include "WebEditingAction.h"
#include "WebFrameImpl.h"
#include "WebKit.h"
#include "WebInputElement.h"
#include "WebNode.h"
#include "WebPasswordAutocompleteListener.h"
#include "WebRange.h"
#include "WebTextAffinity.h"
#include "WebViewClient.h"
#include "WebViewImpl.h"
using namespace WebCore;
namespace WebKit {
static const size_t maximumUndoStackDepth = 1000;
static const size_t maximumTextSizeForAutofill = 1000;
EditorClientImpl::EditorClientImpl(WebViewImpl* webview)
: m_webView(webview)
, m_inRedo(false)
, m_backspaceOrDeletePressed(false)
, m_spellCheckThisFieldStatus(SpellCheckAutomatic)
, m_autofillTimer(this, &EditorClientImpl::doAutofill)
{
}
EditorClientImpl::~EditorClientImpl()
{
}
void EditorClientImpl::pageDestroyed()
{
}
bool EditorClientImpl::shouldShowDeleteInterface(HTMLElement* elem)
{
return WebKit::layoutTestMode()
&& elem->getAttribute(HTMLNames::classAttr) == "needsDeletionUI";
}
bool EditorClientImpl::smartInsertDeleteEnabled()
{
if (m_webView->client())
return m_webView->client()->isSmartInsertDeleteEnabled();
return true;
}
bool EditorClientImpl::isSelectTrailingWhitespaceEnabled()
{
if (m_webView->client())
return m_webView->client()->isSelectTrailingWhitespaceEnabled();
#if OS(WINDOWS)
return true;
#else
return false;
#endif
}
bool EditorClientImpl::shouldSpellcheckByDefault()
{
const Frame* frame = m_webView->focusedWebCoreFrame();
if (!frame)
return false;
const Editor* editor = frame->editor();
if (!editor)
return false;
if (editor->spellCheckingEnabledInFocusedNode())
return true;
const Document* document = frame->document();
if (!document)
return false;
const Node* node = document->focusedNode();
if (!node)
return true;
const RenderObject* renderer = node->renderer();
if (!renderer)
return false;
return !renderer->isTextField();
}
bool EditorClientImpl::isContinuousSpellCheckingEnabled()
{
if (m_spellCheckThisFieldStatus == SpellCheckForcedOff)
return false;
if (m_spellCheckThisFieldStatus == SpellCheckForcedOn)
return true;
return shouldSpellcheckByDefault();
}
void EditorClientImpl::toggleContinuousSpellChecking()
{
if (isContinuousSpellCheckingEnabled())
m_spellCheckThisFieldStatus = SpellCheckForcedOff;
else
m_spellCheckThisFieldStatus = SpellCheckForcedOn;
}
bool EditorClientImpl::isGrammarCheckingEnabled()
{
return false;
}
void EditorClientImpl::toggleGrammarChecking()
{
notImplemented();
}
int EditorClientImpl::spellCheckerDocumentTag()
{
ASSERT_NOT_REACHED();
return 0;
}
bool EditorClientImpl::isEditable()
{
return false;
}
bool EditorClientImpl::shouldBeginEditing(Range* range)
{
if (m_webView->client())
return m_webView->client()->shouldBeginEditing(WebRange(range));
return true;
}
bool EditorClientImpl::shouldEndEditing(Range* range)
{
if (m_webView->client())
return m_webView->client()->shouldEndEditing(WebRange(range));
return true;
}
bool EditorClientImpl::shouldInsertNode(Node* node,
Range* range,
EditorInsertAction action)
{
if (m_webView->client()) {
return m_webView->client()->shouldInsertNode(WebNode(node),
WebRange(range),
static_cast<WebEditingAction>(action));
}
return true;
}
bool EditorClientImpl::shouldInsertText(const String& text,
Range* range,
EditorInsertAction action)
{
if (m_webView->client()) {
return m_webView->client()->shouldInsertText(WebString(text),
WebRange(range),
static_cast<WebEditingAction>(action));
}
return true;
}
bool EditorClientImpl::shouldDeleteRange(Range* range)
{
if (m_webView->client())
return m_webView->client()->shouldDeleteRange(WebRange(range));
return true;
}
bool EditorClientImpl::shouldChangeSelectedRange(Range* fromRange,
Range* toRange,
EAffinity affinity,
bool stillSelecting)
{
if (m_webView->client()) {
return m_webView->client()->shouldChangeSelectedRange(WebRange(fromRange),
WebRange(toRange),
static_cast<WebTextAffinity>(affinity),
stillSelecting);
}
return true;
}
bool EditorClientImpl::shouldApplyStyle(CSSStyleDeclaration* style,
Range* range)
{
if (m_webView->client()) {
return m_webView->client()->shouldApplyStyle(WebString(),
WebRange(range));
}
return true;
}
bool EditorClientImpl::shouldMoveRangeAfterDelete(Range* range,
Range* rangeToBeReplaced)
{
return true;
}
void EditorClientImpl::didBeginEditing()
{
if (m_webView->client())
m_webView->client()->didBeginEditing();
}
void EditorClientImpl::respondToChangedSelection()
{
if (m_webView->client()) {
Frame* frame = m_webView->focusedWebCoreFrame();
if (frame)
m_webView->client()->didChangeSelection(!frame->selection()->isRange());
}
}
void EditorClientImpl::respondToChangedContents()
{
if (m_webView->client())
m_webView->client()->didChangeContents();
}
void EditorClientImpl::didEndEditing()
{
if (m_webView->client())
m_webView->client()->didEndEditing();
}
void EditorClientImpl::didWriteSelectionToPasteboard()
{
}
void EditorClientImpl::didSetSelectionTypesForPasteboard()
{
}
void EditorClientImpl::registerCommandForUndo(PassRefPtr<EditCommand> command)
{
if (m_undoStack.size() == maximumUndoStackDepth)
m_undoStack.removeFirst(); if (!m_inRedo)
m_redoStack.clear();
m_undoStack.append(command);
}
void EditorClientImpl::registerCommandForRedo(PassRefPtr<EditCommand> command)
{
m_redoStack.append(command);
}
void EditorClientImpl::clearUndoRedoOperations()
{
m_undoStack.clear();
m_redoStack.clear();
}
bool EditorClientImpl::canUndo() const
{
return !m_undoStack.isEmpty();
}
bool EditorClientImpl::canRedo() const
{
return !m_redoStack.isEmpty();
}
void EditorClientImpl::undo()
{
if (canUndo()) {
EditCommandStack::iterator back = --m_undoStack.end();
RefPtr<EditCommand> command(*back);
m_undoStack.remove(back);
command->unapply();
}
}
void EditorClientImpl::redo()
{
if (canRedo()) {
EditCommandStack::iterator back = --m_redoStack.end();
RefPtr<EditCommand> command(*back);
m_redoStack.remove(back);
ASSERT(!m_inRedo);
m_inRedo = true;
command->reapply();
m_inRedo = false;
}
}
static const unsigned CtrlKey = 1 << 0;
static const unsigned AltKey = 1 << 1;
static const unsigned ShiftKey = 1 << 2;
static const unsigned MetaKey = 1 << 3;
#if OS(DARWIN)
static const unsigned OptionKey = AltKey;
static const unsigned CommandKey = MetaKey;
#endif
struct KeyDownEntry {
unsigned virtualKey;
unsigned modifiers;
const char* name;
};
struct KeyPressEntry {
unsigned charCode;
unsigned modifiers;
const char* name;
};
static const KeyDownEntry keyDownEntries[] = {
{ VKEY_LEFT, 0, "MoveLeft" },
{ VKEY_LEFT, ShiftKey, "MoveLeftAndModifySelection" },
#if OS(DARWIN)
{ VKEY_LEFT, OptionKey, "MoveWordLeft" },
{ VKEY_LEFT, OptionKey | ShiftKey,
"MoveWordLeftAndModifySelection" },
#else
{ VKEY_LEFT, CtrlKey, "MoveWordLeft" },
{ VKEY_LEFT, CtrlKey | ShiftKey,
"MoveWordLeftAndModifySelection" },
#endif
{ VKEY_RIGHT, 0, "MoveRight" },
{ VKEY_RIGHT, ShiftKey, "MoveRightAndModifySelection" },
#if OS(DARWIN)
{ VKEY_RIGHT, OptionKey, "MoveWordRight" },
{ VKEY_RIGHT, OptionKey | ShiftKey,
"MoveWordRightAndModifySelection" },
#else
{ VKEY_RIGHT, CtrlKey, "MoveWordRight" },
{ VKEY_RIGHT, CtrlKey | ShiftKey,
"MoveWordRightAndModifySelection" },
#endif
{ VKEY_UP, 0, "MoveUp" },
{ VKEY_UP, ShiftKey, "MoveUpAndModifySelection" },
{ VKEY_PRIOR, ShiftKey, "MovePageUpAndModifySelection" },
{ VKEY_DOWN, 0, "MoveDown" },
{ VKEY_DOWN, ShiftKey, "MoveDownAndModifySelection" },
{ VKEY_NEXT, ShiftKey, "MovePageDownAndModifySelection" },
#if !OS(DARWIN)
{ VKEY_PRIOR, 0, "MovePageUp" },
{ VKEY_NEXT, 0, "MovePageDown" },
#endif
{ VKEY_HOME, 0, "MoveToBeginningOfLine" },
{ VKEY_HOME, ShiftKey,
"MoveToBeginningOfLineAndModifySelection" },
#if OS(DARWIN)
{ VKEY_LEFT, CommandKey, "MoveToBeginningOfLine" },
{ VKEY_LEFT, CommandKey | ShiftKey,
"MoveToBeginningOfLineAndModifySelection" },
{ VKEY_PRIOR, OptionKey, "MovePageUp" },
{ VKEY_NEXT, OptionKey, "MovePageDown" },
#endif
#if OS(DARWIN)
{ VKEY_UP, CommandKey, "MoveToBeginningOfDocument" },
{ VKEY_UP, CommandKey | ShiftKey,
"MoveToBeginningOfDocumentAndModifySelection" },
#else
{ VKEY_HOME, CtrlKey, "MoveToBeginningOfDocument" },
{ VKEY_HOME, CtrlKey | ShiftKey,
"MoveToBeginningOfDocumentAndModifySelection" },
#endif
{ VKEY_END, 0, "MoveToEndOfLine" },
{ VKEY_END, ShiftKey, "MoveToEndOfLineAndModifySelection" },
#if OS(DARWIN)
{ VKEY_DOWN, CommandKey, "MoveToEndOfDocument" },
{ VKEY_DOWN, CommandKey | ShiftKey,
"MoveToEndOfDocumentAndModifySelection" },
#else
{ VKEY_END, CtrlKey, "MoveToEndOfDocument" },
{ VKEY_END, CtrlKey | ShiftKey,
"MoveToEndOfDocumentAndModifySelection" },
#endif
#if OS(DARWIN)
{ VKEY_RIGHT, CommandKey, "MoveToEndOfLine" },
{ VKEY_RIGHT, CommandKey | ShiftKey,
"MoveToEndOfLineAndModifySelection" },
#endif
{ VKEY_BACK, 0, "DeleteBackward" },
{ VKEY_BACK, ShiftKey, "DeleteBackward" },
{ VKEY_DELETE, 0, "DeleteForward" },
#if OS(DARWIN)
{ VKEY_BACK, OptionKey, "DeleteWordBackward" },
{ VKEY_DELETE, OptionKey, "DeleteWordForward" },
#else
{ VKEY_BACK, CtrlKey, "DeleteWordBackward" },
{ VKEY_DELETE, CtrlKey, "DeleteWordForward" },
#endif
{ 'B', CtrlKey, "ToggleBold" },
{ 'I', CtrlKey, "ToggleItalic" },
{ 'U', CtrlKey, "ToggleUnderline" },
{ VKEY_ESCAPE, 0, "Cancel" },
{ VKEY_OEM_PERIOD, CtrlKey, "Cancel" },
{ VKEY_TAB, 0, "InsertTab" },
{ VKEY_TAB, ShiftKey, "InsertBacktab" },
{ VKEY_RETURN, 0, "InsertNewline" },
{ VKEY_RETURN, CtrlKey, "InsertNewline" },
{ VKEY_RETURN, AltKey, "InsertNewline" },
{ VKEY_RETURN, AltKey | ShiftKey, "InsertNewline" },
{ VKEY_RETURN, ShiftKey, "InsertLineBreak" },
{ VKEY_INSERT, CtrlKey, "Copy" },
{ VKEY_INSERT, ShiftKey, "Paste" },
{ VKEY_DELETE, ShiftKey, "Cut" },
#if !OS(DARWIN)
{ 'C', CtrlKey, "Copy" },
{ 'V', CtrlKey, "Paste" },
{ 'V', CtrlKey | ShiftKey, "PasteAndMatchStyle" },
{ 'X', CtrlKey, "Cut" },
{ 'A', CtrlKey, "SelectAll" },
{ 'Z', CtrlKey, "Undo" },
{ 'Z', CtrlKey | ShiftKey, "Redo" },
{ 'Y', CtrlKey, "Redo" },
#endif
};
static const KeyPressEntry keyPressEntries[] = {
{ '\t', 0, "InsertTab" },
{ '\t', ShiftKey, "InsertBacktab" },
{ '\r', 0, "InsertNewline" },
{ '\r', CtrlKey, "InsertNewline" },
{ '\r', ShiftKey, "InsertLineBreak" },
{ '\r', AltKey, "InsertNewline" },
{ '\r', AltKey | ShiftKey, "InsertNewline" },
};
const char* EditorClientImpl::interpretKeyEvent(const KeyboardEvent* evt)
{
const PlatformKeyboardEvent* keyEvent = evt->keyEvent();
if (!keyEvent)
return "";
static HashMap<int, const char*>* keyDownCommandsMap = 0;
static HashMap<int, const char*>* keyPressCommandsMap = 0;
if (!keyDownCommandsMap) {
keyDownCommandsMap = new HashMap<int, const char*>;
keyPressCommandsMap = new HashMap<int, const char*>;
for (unsigned i = 0; i < arraysize(keyDownEntries); i++) {
keyDownCommandsMap->set(keyDownEntries[i].modifiers << 16 | keyDownEntries[i].virtualKey,
keyDownEntries[i].name);
}
for (unsigned i = 0; i < arraysize(keyPressEntries); i++) {
keyPressCommandsMap->set(keyPressEntries[i].modifiers << 16 | keyPressEntries[i].charCode,
keyPressEntries[i].name);
}
}
unsigned modifiers = 0;
if (keyEvent->shiftKey())
modifiers |= ShiftKey;
if (keyEvent->altKey())
modifiers |= AltKey;
if (keyEvent->ctrlKey())
modifiers |= CtrlKey;
if (keyEvent->metaKey())
modifiers |= MetaKey;
if (keyEvent->type() == PlatformKeyboardEvent::RawKeyDown) {
int mapKey = modifiers << 16 | evt->keyCode();
return mapKey ? keyDownCommandsMap->get(mapKey) : 0;
}
int mapKey = modifiers << 16 | evt->charCode();
return mapKey ? keyPressCommandsMap->get(mapKey) : 0;
}
bool EditorClientImpl::handleEditingKeyboardEvent(KeyboardEvent* evt)
{
const PlatformKeyboardEvent* keyEvent = evt->keyEvent();
if (!keyEvent || keyEvent->isSystemKey())
return false;
Frame* frame = evt->target()->toNode()->document()->frame();
if (!frame)
return false;
String commandName = interpretKeyEvent(evt);
Editor::Command command = frame->editor()->command(commandName);
if (keyEvent->type() == PlatformKeyboardEvent::RawKeyDown) {
if (command.isTextInsertion() || commandName.isEmpty())
return false;
if (command.execute(evt)) {
if (m_webView->client())
m_webView->client()->didExecuteCommand(WebString(commandName));
return true;
}
return false;
}
if (command.execute(evt)) {
if (m_webView->client())
m_webView->client()->didExecuteCommand(WebString(commandName));
return true;
}
if (evt->keyEvent()->text().length() == 1) {
UChar ch = evt->keyEvent()->text()[0U];
if (ch < ' ')
return false;
#if !OS(WINDOWS)
if (ch < 0x80) {
if (evt->keyEvent()->ctrlKey() && !evt->keyEvent()->altKey())
return false;
#if OS(DARWIN)
if (evt->keyEvent()->metaKey())
return false;
#endif
}
#endif
}
if (!frame->editor()->canEdit())
return false;
return frame->editor()->insertText(evt->keyEvent()->text(), evt);
}
void EditorClientImpl::handleKeyboardEvent(KeyboardEvent* evt)
{
if (evt->keyCode() == VKEY_DOWN
|| evt->keyCode() == VKEY_UP) {
ASSERT(evt->target()->toNode());
showFormAutofillForNode(evt->target()->toNode());
}
if ((m_webView->client()
&& m_webView->client()->handleCurrentKeyboardEvent())
|| handleEditingKeyboardEvent(evt))
evt->setDefaultHandled();
}
void EditorClientImpl::handleInputMethodKeydown(KeyboardEvent* keyEvent)
{
}
void EditorClientImpl::textFieldDidBeginEditing(Element*)
{
}
void EditorClientImpl::textFieldDidEndEditing(Element* element)
{
m_autofillArgs.clear();
m_autofillTimer.stop();
m_webView->hideSuggestionsPopup();
if (!m_webView->client())
return;
HTMLInputElement* inputElement = WebKit::toHTMLInputElement(element);
if (!inputElement)
return;
WebFrameImpl* webframe = WebFrameImpl::fromFrame(inputElement->document()->frame());
if (!webframe)
return;
WebPasswordAutocompleteListener* listener = webframe->getPasswordListener(inputElement);
if (!listener)
return;
listener->didBlurInputElement(inputElement->value());
}
void EditorClientImpl::textDidChangeInTextField(Element* element)
{
ASSERT(element->hasLocalName(HTMLNames::inputTag));
autofill(static_cast<HTMLInputElement*>(element), false, false,
true);
}
bool EditorClientImpl::showFormAutofillForNode(Node* node)
{
HTMLInputElement* inputElement = WebKit::toHTMLInputElement(node);
if (inputElement)
return autofill(inputElement, true, true, false);
return false;
}
bool EditorClientImpl::autofill(HTMLInputElement* inputElement,
bool autofillFormOnly,
bool autofillOnEmptyValue,
bool requireCaretAtEnd)
{
m_autofillArgs.clear();
m_autofillTimer.stop();
if (!inputElement->isEnabledFormControl() || !inputElement->isTextField()
|| inputElement->isPasswordField() || !inputElement->autoComplete()
|| !inputElement->isEnabledFormControl()
|| inputElement->isReadOnlyFormControl())
return false;
WebString name = WebInputElement(inputElement).nameForAutofill();
if (name.isEmpty()) return false;
if (inputElement->value().length() > maximumTextSizeForAutofill)
return false;
m_autofillArgs = new AutofillArgs();
m_autofillArgs->inputElement = inputElement;
m_autofillArgs->autofillFormOnly = autofillFormOnly;
m_autofillArgs->autofillOnEmptyValue = autofillOnEmptyValue;
m_autofillArgs->requireCaretAtEnd = requireCaretAtEnd;
m_autofillArgs->backspaceOrDeletePressed = m_backspaceOrDeletePressed;
if (!requireCaretAtEnd)
doAutofill(0);
else {
m_autofillTimer.startOneShot(0.0);
}
return true;
}
void EditorClientImpl::doAutofill(Timer<EditorClientImpl>* timer)
{
OwnPtr<AutofillArgs> args(m_autofillArgs.release());
HTMLInputElement* inputElement = args->inputElement.get();
const String& value = inputElement->value();
bool isCaretAtEnd = true;
if (args->requireCaretAtEnd)
isCaretAtEnd = inputElement->selectionStart() == inputElement->selectionEnd()
&& inputElement->selectionEnd() == static_cast<int>(value.length());
if ((!args->autofillOnEmptyValue && value.isEmpty()) || !isCaretAtEnd) {
m_webView->hideSuggestionsPopup();
return;
}
WebFrameImpl* webframe = WebFrameImpl::fromFrame(inputElement->document()->frame());
if (!webframe)
return;
WebPasswordAutocompleteListener* listener = webframe->getPasswordListener(inputElement);
if (listener) {
if (args->autofillFormOnly)
return;
listener->performInlineAutocomplete(value,
args->backspaceOrDeletePressed,
true);
return;
}
WebString name = WebInputElement(inputElement).nameForAutofill();
ASSERT(static_cast<int>(name.length()) > 0);
if (m_webView->client())
m_webView->client()->queryAutofillSuggestions(WebNode(inputElement),
name, WebString(value));
}
void EditorClientImpl::cancelPendingAutofill()
{
m_autofillArgs.clear();
m_autofillTimer.stop();
}
void EditorClientImpl::onAutocompleteSuggestionAccepted(HTMLInputElement* textField)
{
WebFrameImpl* webframe = WebFrameImpl::fromFrame(textField->document()->frame());
if (!webframe)
return;
WebPasswordAutocompleteListener* listener = webframe->getPasswordListener(textField);
if (listener)
listener->performInlineAutocomplete(textField->value(), false, false);
}
bool EditorClientImpl::doTextFieldCommandFromEvent(Element* element,
KeyboardEvent* event)
{
m_backspaceOrDeletePressed = event->keyCode() == VKEY_BACK || event->keyCode() == VKEY_DELETE;
return false;
}
void EditorClientImpl::textWillBeDeletedInTextField(Element*)
{
}
void EditorClientImpl::textDidChangeInTextArea(Element*)
{
}
void EditorClientImpl::ignoreWordInSpellDocument(const String&)
{
notImplemented();
}
void EditorClientImpl::learnWord(const String&)
{
notImplemented();
}
void EditorClientImpl::checkSpellingOfString(const UChar* text, int length,
int* misspellingLocation,
int* misspellingLength)
{
int spellLocation = -1;
int spellLength = 0;
if (isContinuousSpellCheckingEnabled() && m_webView->client())
m_webView->client()->spellCheck(WebString(text, length), spellLocation, spellLength);
else {
spellLocation = 0;
spellLength = 0;
}
if (misspellingLocation)
*misspellingLocation = spellLocation;
if (misspellingLength)
*misspellingLength = spellLength;
}
String EditorClientImpl::getAutoCorrectSuggestionForMisspelledWord(const String& misspelledWord)
{
if (!(isContinuousSpellCheckingEnabled() && m_webView->client()))
return String();
for (size_t i = 1; i < misspelledWord.length(); i++) {
if (u_isupper(static_cast<UChar32>(misspelledWord[i])))
return String();
}
return m_webView->client()->autoCorrectWord(WebString(misspelledWord));
}
void EditorClientImpl::checkGrammarOfString(const UChar*, int length,
WTF::Vector<GrammarDetail>&,
int* badGrammarLocation,
int* badGrammarLength)
{
notImplemented();
if (badGrammarLocation)
*badGrammarLocation = 0;
if (badGrammarLength)
*badGrammarLength = 0;
}
void EditorClientImpl::updateSpellingUIWithGrammarString(const String&,
const GrammarDetail& detail)
{
notImplemented();
}
void EditorClientImpl::updateSpellingUIWithMisspelledWord(const String& misspelledWord)
{
if (m_webView->client())
m_webView->client()->updateSpellingUIWithMisspelledWord(WebString(misspelledWord));
}
void EditorClientImpl::showSpellingUI(bool show)
{
if (m_webView->client())
m_webView->client()->showSpellingUI(show);
}
bool EditorClientImpl::spellingUIIsShowing()
{
if (m_webView->client())
return m_webView->client()->isShowingSpellingUI();
return false;
}
void EditorClientImpl::getGuessesForWord(const String&,
WTF::Vector<String>& guesses)
{
notImplemented();
}
void EditorClientImpl::setInputMethodState(bool enabled)
{
if (m_webView->client())
m_webView->client()->setInputMethodEnabled(enabled);
}
}