EditorCommand.cpp 79.9 KB
Newer Older
darin@apple.com's avatar
darin@apple.com committed
1
/*
darin@apple.com's avatar
darin@apple.com committed
2
 * Copyright (C) 2006, 2007, 2008 Apple Inc. All rights reserved.
3
 * Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies)
xan@webkit.org's avatar
xan@webkit.org committed
4
 * Copyright (C) 2009 Igalia S.L.
darin@apple.com's avatar
darin@apple.com committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 */

#include "config.h"
29
#include "Editor.h"
darin@apple.com's avatar
darin@apple.com committed
30

rniwa@webkit.org's avatar
rniwa@webkit.org committed
31
#include "CSSComputedStyleDeclaration.h"
darin@apple.com's avatar
darin@apple.com committed
32
#include "CSSMutableStyleDeclaration.h"
darin@apple.com's avatar
darin@apple.com committed
33
#include "CSSPropertyNames.h"
mitz@apple.com's avatar
mitz@apple.com committed
34
#include "CSSValueKeywords.h"
35
#include "CSSValueList.h"
36
#include "Chrome.h"
darin@apple.com's avatar
darin@apple.com committed
37 38
#include "CreateLinkCommand.h"
#include "DocumentFragment.h"
darin@apple.com's avatar
darin@apple.com committed
39
#include "EditorClient.h"
darin@apple.com's avatar
darin@apple.com committed
40 41 42
#include "Event.h"
#include "EventHandler.h"
#include "FormatBlockCommand.h"
43
#include "Frame.h"
44
#include "FrameView.h"
darin@apple.com's avatar
darin@apple.com committed
45
#include "HTMLFontElement.h"
46
#include "HTMLHRElement.h"
darin@apple.com's avatar
darin@apple.com committed
47
#include "HTMLImageElement.h"
48
#include "HTMLNames.h"
darin@apple.com's avatar
darin@apple.com committed
49 50
#include "IndentOutdentCommand.h"
#include "InsertListCommand.h"
51
#include "KillRing.h"
darin@apple.com's avatar
darin@apple.com committed
52
#include "Page.h"
53
#include "RenderBox.h"
darin@apple.com's avatar
darin@apple.com committed
54
#include "ReplaceSelectionCommand.h"
55
#include "Scrollbar.h"
darin@apple.com's avatar
darin@apple.com committed
56 57 58 59
#include "Settings.h"
#include "Sound.h"
#include "TypingCommand.h"
#include "UnlinkCommand.h"
60
#include "UserTypingGestureIndicator.h"
darin@apple.com's avatar
darin@apple.com committed
61 62
#include "htmlediting.h"
#include "markup.h"
63
#include <wtf/text/AtomicString.h>
darin@apple.com's avatar
darin@apple.com committed
64 65 66 67 68 69 70 71

namespace WebCore {

using namespace HTMLNames;

class EditorInternalCommand {
public:
    bool (*execute)(Frame*, Event*, EditorCommandSource, const String&);
72
    bool (*isSupportedFromDOM)(Frame*);
sullivan@apple.com's avatar
sullivan@apple.com committed
73
    bool (*isEnabled)(Frame*, Event*, EditorCommandSource);
darin@apple.com's avatar
darin@apple.com committed
74 75 76
    TriState (*state)(Frame*, Event*);
    String (*value)(Frame*, Event*);
    bool isTextInsertion;
justin.garcia@apple.com's avatar
justin.garcia@apple.com committed
77
    bool allowExecutionWhenDisabled;
darin@apple.com's avatar
darin@apple.com committed
78 79 80 81 82 83 84
};

typedef HashMap<String, const EditorInternalCommand*, CaseFoldingHash> CommandMap;

static const bool notTextInsertion = false;
static const bool isTextInsertion = true;

justin.garcia@apple.com's avatar
justin.garcia@apple.com committed
85 86 87
static const bool allowExecutionWhenDisabled = true;
static const bool doNotAllowExecutionWhenDisabled = false;

darin@apple.com's avatar
darin@apple.com committed
88 89 90 91 92 93 94 95 96 97 98 99 100
// Related to Editor::selectionForCommand.
// Certain operations continue to use the target control's selection even if the event handler
// already moved the selection outside of the text control.
static Frame* targetFrame(Frame* frame, Event* event)
{
    if (!event)
        return frame;
    Node* node = event->target()->toNode();
    if (!node)
        return frame;
    return node->document()->frame();
}

rniwa@webkit.org's avatar
rniwa@webkit.org committed
101
static bool applyCommandToFrame(Frame* frame, EditorCommandSource source, EditAction action, CSSMutableStyleDeclaration* style)
darin@apple.com's avatar
darin@apple.com committed
102 103 104
{
    // FIXME: We don't call shouldApplyStyle when the source is DOM; is there a good reason for that?
    switch (source) {
105 106 107 108 109 110 111
    case CommandFromMenuOrKeyBinding:
        frame->editor()->applyStyleToSelection(style, action);
        return true;
    case CommandFromDOM:
    case CommandFromDOMWithUserInterface:
        frame->editor()->applyStyle(style);
        return true;
darin@apple.com's avatar
darin@apple.com committed
112 113 114 115 116
    }
    ASSERT_NOT_REACHED();
    return false;
}

rniwa@webkit.org's avatar
rniwa@webkit.org committed
117
static bool executeApplyStyle(Frame* frame, EditorCommandSource source, EditAction action, int propertyID, const String& propertyValue)
darin@apple.com's avatar
darin@apple.com committed
118
{
rniwa@webkit.org's avatar
rniwa@webkit.org committed
119 120 121
    RefPtr<CSSMutableStyleDeclaration> style = CSSMutableStyleDeclaration::create();
    style->setProperty(propertyID, propertyValue);
    return applyCommandToFrame(frame, source, action, style.get());
darin@apple.com's avatar
darin@apple.com committed
122 123 124 125
}

static bool executeApplyStyle(Frame* frame, EditorCommandSource source, EditAction action, int propertyID, int propertyValue)
{
darin@apple.com's avatar
darin@apple.com committed
126
    RefPtr<CSSMutableStyleDeclaration> style = CSSMutableStyleDeclaration::create();
darin@apple.com's avatar
darin@apple.com committed
127
    style->setProperty(propertyID, propertyValue);
rniwa@webkit.org's avatar
rniwa@webkit.org committed
128
    return applyCommandToFrame(frame, source, action, style.get());
darin@apple.com's avatar
darin@apple.com committed
129 130
}

131 132 133
// FIXME: executeToggleStyleInList does not handle complicated cases such as <b><u>hello</u>world</b> properly.
//        This function must use Editor::selectionHasStyle to determine the current style but we cannot fix this
//        until https://bugs.webkit.org/show_bug.cgi?id=27818 is resolved.
rniwa@webkit.org's avatar
rniwa@webkit.org committed
134 135 136
static bool executeToggleStyleInList(Frame* frame, EditorCommandSource source, EditAction action, int propertyID, CSSValue* value)
{
    ExceptionCode ec = 0;
137 138
    RefPtr<EditingStyle> selectionStyle = frame->editor()->selectionStartStyle();
    if (!selectionStyle || !selectionStyle->style())
139 140
        return false;

141
    RefPtr<CSSValue> selectedCSSValue = selectionStyle->style()->getPropertyCSSValue(propertyID);
rniwa@webkit.org's avatar
rniwa@webkit.org committed
142 143 144 145 146 147 148 149 150 151 152 153 154
    String newStyle = "none";
    if (selectedCSSValue->isValueList()) {
        RefPtr<CSSValueList> selectedCSSValueList = static_cast<CSSValueList*>(selectedCSSValue.get());
        if (!selectedCSSValueList->removeAll(value))
            selectedCSSValueList->append(value);
        if (selectedCSSValueList->length())
            newStyle = selectedCSSValueList->cssText();

    } else if (selectedCSSValue->cssText() == "none")
        newStyle = value->cssText();

    // FIXME: We shouldn't be having to convert new style into text.  We should have setPropertyCSSValue.
    RefPtr<CSSMutableStyleDeclaration> newMutableStyle = CSSMutableStyleDeclaration::create();
155
    newMutableStyle->setProperty(propertyID, newStyle, ec);
rniwa@webkit.org's avatar
rniwa@webkit.org committed
156 157
    return applyCommandToFrame(frame, source, action, newMutableStyle.get());
}
158

darin@apple.com's avatar
darin@apple.com committed
159 160
static bool executeToggleStyle(Frame* frame, EditorCommandSource source, EditAction action, int propertyID, const char* offValue, const char* onValue)
{
161
    // Style is considered present when
162
    // Mac: present at the beginning of selection
163
    // other: present throughout the selection
164

165
    bool styleIsPresent;
166
    if (frame->editor()->behavior().shouldToggleStyleBasedOnStartOfSelection())
167
        styleIsPresent = frame->editor()->selectionStartHasStyle(propertyID, onValue);
168
    else
169
        styleIsPresent = frame->editor()->selectionHasStyle(propertyID, onValue) == TrueTriState;
170

171 172
    RefPtr<EditingStyle> style = EditingStyle::create(propertyID, styleIsPresent ? offValue : onValue);
    return applyCommandToFrame(frame, source, action, style->style());
darin@apple.com's avatar
darin@apple.com committed
173 174 175 176
}

static bool executeApplyParagraphStyle(Frame* frame, EditorCommandSource source, EditAction action, int propertyID, const String& propertyValue)
{
darin@apple.com's avatar
darin@apple.com committed
177
    RefPtr<CSSMutableStyleDeclaration> style = CSSMutableStyleDeclaration::create();
darin@apple.com's avatar
darin@apple.com committed
178 179 180
    style->setProperty(propertyID, propertyValue);
    // FIXME: We don't call shouldApplyStyle when the source is DOM; is there a good reason for that?
    switch (source) {
181 182 183 184 185 186 187
    case CommandFromMenuOrKeyBinding:
        frame->editor()->applyParagraphStyleToSelection(style.get(), action);
        return true;
    case CommandFromDOM:
    case CommandFromDOMWithUserInterface:
        frame->editor()->applyParagraphStyle(style.get());
        return true;
darin@apple.com's avatar
darin@apple.com committed
188 189 190 191 192 193 194
    }
    ASSERT_NOT_REACHED();
    return false;
}

static bool executeInsertFragment(Frame* frame, PassRefPtr<DocumentFragment> fragment)
{
195
    applyCommand(ReplaceSelectionCommand::create(frame->document(), fragment, ReplaceSelectionCommand::PreventNesting, EditActionUnspecified));
darin@apple.com's avatar
darin@apple.com committed
196 197 198 199 200
    return true;
}

static bool executeInsertNode(Frame* frame, PassRefPtr<Node> content)
{
201
    RefPtr<DocumentFragment> fragment = DocumentFragment::create(frame->document());
darin@apple.com's avatar
darin@apple.com committed
202 203 204 205 206 207 208
    ExceptionCode ec = 0;
    fragment->appendChild(content, ec);
    if (ec)
        return false;
    return executeInsertFragment(frame, fragment.release());
}

darin@apple.com's avatar
darin@apple.com committed
209 210
static bool expandSelectionToGranularity(Frame* frame, TextGranularity granularity)
{
211
    VisibleSelection selection = frame->selection()->selection();
darin@apple.com's avatar
darin@apple.com committed
212
    selection.expandUsingGranularity(granularity);
eric@webkit.org's avatar
eric@webkit.org committed
213
    RefPtr<Range> newRange = selection.toNormalizedRange();
darin@apple.com's avatar
darin@apple.com committed
214 215 216 217 218
    if (!newRange)
        return false;
    ExceptionCode ec = 0;
    if (newRange->collapsed(ec))
        return false;
eric@webkit.org's avatar
eric@webkit.org committed
219
    RefPtr<Range> oldRange = frame->selection()->selection().toNormalizedRange();
darin@apple.com's avatar
darin@apple.com committed
220
    EAffinity affinity = frame->selection()->affinity();
darin@apple.com's avatar
darin@apple.com committed
221 222
    if (!frame->editor()->client()->shouldChangeSelectedRange(oldRange.get(), newRange.get(), affinity, false))
        return false;
darin@apple.com's avatar
darin@apple.com committed
223
    frame->selection()->setSelectedRange(newRange.get(), affinity, true);
darin@apple.com's avatar
darin@apple.com committed
224 225 226
    return true;
}

darin@apple.com's avatar
darin@apple.com committed
227 228
static TriState stateStyle(Frame* frame, int propertyID, const char* desiredValue)
{
229
    if (frame->editor()->behavior().shouldToggleStyleBasedOnStartOfSelection())
230 231
        return frame->editor()->selectionStartHasStyle(propertyID, desiredValue) ? TrueTriState : FalseTriState;
    return frame->editor()->selectionHasStyle(propertyID, desiredValue);
darin@apple.com's avatar
darin@apple.com committed
232 233 234 235
}

static String valueStyle(Frame* frame, int propertyID)
{
236 237 238
    // FIXME: Rather than retrieving the style at the start of the current selection,
    // we should retrieve the style present throughout the selection for non-Mac platforms.
    return frame->editor()->selectionStartCSSPropertyValue(propertyID);
darin@apple.com's avatar
darin@apple.com committed
239 240
}

mitz@apple.com's avatar
mitz@apple.com committed
241 242 243 244 245 246 247
static TriState stateTextWritingDirection(Frame* frame, WritingDirection direction)
{
    bool hasNestedOrMultipleEmbeddings;
    WritingDirection selectionDirection = frame->editor()->textDirectionForSelection(hasNestedOrMultipleEmbeddings);
    return (selectionDirection == direction && !hasNestedOrMultipleEmbeddings) ? TrueTriState : FalseTriState;
}

darin@apple.com's avatar
darin@apple.com committed
248
static int verticalScrollDistance(Frame* frame)
darin@apple.com's avatar
darin@apple.com committed
249
{
darin@apple.com's avatar
darin@apple.com committed
250 251 252 253
    Node* focusedNode = frame->document()->focusedNode();
    if (!focusedNode)
        return 0;
    RenderObject* renderer = focusedNode->renderer();
hyatt@apple.com's avatar
hyatt@apple.com committed
254
    if (!renderer || !renderer->isBox())
darin@apple.com's avatar
darin@apple.com committed
255
        return 0;
darin@apple.com's avatar
darin@apple.com committed
256 257
    RenderStyle* style = renderer->style();
    if (!style)
darin@apple.com's avatar
darin@apple.com committed
258
        return 0;
259
    if (!(style->overflowY() == OSCROLL || style->overflowY() == OAUTO || focusedNode->rendererIsEditable()))
darin@apple.com's avatar
darin@apple.com committed
260
        return 0;
261 262
    int height = std::min<int>(toRenderBox(renderer)->clientHeight(),
                               frame->view()->visibleHeight());
263
    return max(max<int>(height * Scrollbar::minFractionToStepWhenPaging(), height - Scrollbar::maxOverlapBetweenPages()), 1);
darin@apple.com's avatar
darin@apple.com committed
264 265 266 267 268 269 270 271 272 273
}

static RefPtr<Range> unionDOMRanges(Range* a, Range* b)
{
    ExceptionCode ec = 0;
    Range* start = a->compareBoundaryPoints(Range::START_TO_START, b, ec) <= 0 ? a : b;
    ASSERT(!ec);
    Range* end = a->compareBoundaryPoints(Range::END_TO_END, b, ec) <= 0 ? b : a;
    ASSERT(!ec);

darin@apple.com's avatar
darin@apple.com committed
274
    return Range::create(a->startContainer(ec)->ownerDocument(), start->startContainer(ec), start->startOffset(ec), end->endContainer(ec), end->endOffset(ec));
darin@apple.com's avatar
darin@apple.com committed
275 276 277 278 279 280
}

// Execute command functions

static bool executeBackColor(Frame* frame, Event*, EditorCommandSource source, const String& value)
{
281
    return executeApplyStyle(frame, source, EditActionSetBackgroundColor, CSSPropertyBackgroundColor, value);
darin@apple.com's avatar
darin@apple.com committed
282 283 284 285 286 287 288 289 290 291 292 293 294
}

static bool executeCopy(Frame* frame, Event*, EditorCommandSource, const String&)
{
    frame->editor()->copy();
    return true;
}

static bool executeCreateLink(Frame* frame, Event*, EditorCommandSource, const String& value)
{
    // FIXME: If userInterface is true, we should display a dialog box to let the user enter a URL.
    if (value.isEmpty())
        return false;
darin@apple.com's avatar
darin@apple.com committed
295
    applyCommand(CreateLinkCommand::create(frame->document(), value));
darin@apple.com's avatar
darin@apple.com committed
296 297 298
    return true;
}

299
static bool executeCut(Frame* frame, Event*, EditorCommandSource source, const String&)
darin@apple.com's avatar
darin@apple.com committed
300
{
301 302 303 304 305
    if (source == CommandFromMenuOrKeyBinding) {
        UserTypingGestureIndicator typingGestureIndicator(frame);
        frame->editor()->cut();
    } else
        frame->editor()->cut();
darin@apple.com's avatar
darin@apple.com committed
306 307 308 309 310 311
    return true;
}

static bool executeDelete(Frame* frame, Event*, EditorCommandSource source, const String&)
{
    switch (source) {
312
    case CommandFromMenuOrKeyBinding: {
313
        // Doesn't modify the text if the current selection isn't a range.
314
        UserTypingGestureIndicator typingGestureIndicator(frame);
315 316
        frame->editor()->performDelete();
        return true;
317
    }
318 319 320 321
    case CommandFromDOM:
    case CommandFromDOMWithUserInterface:
        // If the current selection is a caret, delete the preceding character. IE performs forwardDelete, but we currently side with Firefox.
        // Doesn't scroll to make the selection visible, or modify the kill ring (this time, siding with IE, not Firefox).
322
        TypingCommand::deleteKeyPressed(frame->document(), frame->selection()->granularity() == WordGranularity ? TypingCommand::SmartDelete : 0);
323
        return true;
darin@apple.com's avatar
darin@apple.com committed
324 325 326 327 328
    }
    ASSERT_NOT_REACHED();
    return false;
}

darin@apple.com's avatar
darin@apple.com committed
329 330
static bool executeDeleteBackward(Frame* frame, Event*, EditorCommandSource, const String&)
{
331
    frame->editor()->deleteWithDirection(DirectionBackward, CharacterGranularity, false, true);
darin@apple.com's avatar
darin@apple.com committed
332 333 334 335 336 337
    return true;
}

static bool executeDeleteBackwardByDecomposingPreviousCharacter(Frame* frame, Event*, EditorCommandSource, const String&)
{
    LOG_ERROR("DeleteBackwardByDecomposingPreviousCharacter is not implemented, doing DeleteBackward instead");
338
    frame->editor()->deleteWithDirection(DirectionBackward, CharacterGranularity, false, true);
darin@apple.com's avatar
darin@apple.com committed
339 340 341
    return true;
}

342
static bool executeDeleteForward(Frame* frame, Event*, EditorCommandSource, const String&)
darin@apple.com's avatar
darin@apple.com committed
343
{
344
    frame->editor()->deleteWithDirection(DirectionForward, CharacterGranularity, false, true);
darin@apple.com's avatar
darin@apple.com committed
345 346 347 348 349
    return true;
}

static bool executeDeleteToBeginningOfLine(Frame* frame, Event*, EditorCommandSource, const String&)
{
350
    frame->editor()->deleteWithDirection(DirectionBackward, LineBoundary, true, false);
darin@apple.com's avatar
darin@apple.com committed
351 352 353 354 355
    return true;
}

static bool executeDeleteToBeginningOfParagraph(Frame* frame, Event*, EditorCommandSource, const String&)
{
356
    frame->editor()->deleteWithDirection(DirectionBackward, ParagraphBoundary, true, false);
darin@apple.com's avatar
darin@apple.com committed
357 358 359 360 361 362 363
    return true;
}

static bool executeDeleteToEndOfLine(Frame* frame, Event*, EditorCommandSource, const String&)
{
    // Despite its name, this command should delete the newline at the end of
    // a paragraph if you are at the end of a paragraph (like DeleteToEndOfParagraph).
364
    frame->editor()->deleteWithDirection(DirectionForward, LineBoundary, true, false);
darin@apple.com's avatar
darin@apple.com committed
365 366 367 368 369 370 371
    return true;
}

static bool executeDeleteToEndOfParagraph(Frame* frame, Event*, EditorCommandSource, const String&)
{
    // Despite its name, this command should delete the newline at the end of
    // a paragraph if you are at the end of a paragraph.
372
    frame->editor()->deleteWithDirection(DirectionForward, ParagraphBoundary, true, false);
darin@apple.com's avatar
darin@apple.com committed
373 374 375
    return true;
}

darin@apple.com's avatar
darin@apple.com committed
376 377
static bool executeDeleteToMark(Frame* frame, Event*, EditorCommandSource, const String&)
{
378
    RefPtr<Range> mark = frame->editor()->mark().toNormalizedRange();
darin@apple.com's avatar
darin@apple.com committed
379
    if (mark) {
380
        FrameSelection* selection = frame->selection();
darin@apple.com's avatar
darin@apple.com committed
381
        bool selected = selection->setSelectedRange(unionDOMRanges(mark.get(), frame->editor()->selectedRange().get()).get(), DOWNSTREAM, true);
darin@apple.com's avatar
darin@apple.com committed
382 383 384 385 386
        ASSERT(selected);
        if (!selected)
            return false;
    }
    frame->editor()->performDelete();
387
    frame->editor()->setMark(frame->selection()->selection());
darin@apple.com's avatar
darin@apple.com committed
388 389 390 391 392
    return true;
}

static bool executeDeleteWordBackward(Frame* frame, Event*, EditorCommandSource, const String&)
{
393
    frame->editor()->deleteWithDirection(DirectionBackward, WordGranularity, true, false);
darin@apple.com's avatar
darin@apple.com committed
394 395 396 397 398
    return true;
}

static bool executeDeleteWordForward(Frame* frame, Event*, EditorCommandSource, const String&)
{
399
    frame->editor()->deleteWithDirection(DirectionForward, WordGranularity, true, false);
darin@apple.com's avatar
darin@apple.com committed
400 401 402 403 404
    return true;
}

static bool executeFindString(Frame* frame, Event*, EditorCommandSource, const String& value)
{
405
    return frame->editor()->findString(value, true, false, true, false);
darin@apple.com's avatar
darin@apple.com committed
406 407 408 409
}

static bool executeFontName(Frame* frame, Event*, EditorCommandSource source, const String& value)
{
410
    return executeApplyStyle(frame, source, EditActionSetFont, CSSPropertyFontFamily, value);
darin@apple.com's avatar
darin@apple.com committed
411 412 413 414 415 416 417
}

static bool executeFontSize(Frame* frame, Event*, EditorCommandSource source, const String& value)
{
    int size;
    if (!HTMLFontElement::cssValueFromFontSizeNumber(value, size))
        return false;
418
    return executeApplyStyle(frame, source, EditActionChangeAttributes, CSSPropertyFontSize, size);
darin@apple.com's avatar
darin@apple.com committed
419 420 421 422
}

static bool executeFontSizeDelta(Frame* frame, Event*, EditorCommandSource source, const String& value)
{
423
    return executeApplyStyle(frame, source, EditActionChangeAttributes, CSSPropertyWebkitFontSizeDelta, value);
darin@apple.com's avatar
darin@apple.com committed
424 425 426 427
}

static bool executeForeColor(Frame* frame, Event*, EditorCommandSource source, const String& value)
{
428
    return executeApplyStyle(frame, source, EditActionSetColor, CSSPropertyColor, value);
darin@apple.com's avatar
darin@apple.com committed
429 430 431 432 433 434 435
}

static bool executeFormatBlock(Frame* frame, Event*, EditorCommandSource, const String& value)
{
    String tagName = value.lower();
    if (tagName[0] == '<' && tagName[tagName.length() - 1] == '>')
        tagName = tagName.substring(1, tagName.length() - 2);
436 437 438 439 440 441 442

    ExceptionCode ec;
    String localName, prefix;
    if (!Document::parseQualifiedName(tagName, prefix, localName, ec))
        return false;
    QualifiedName qualifiedTagName(prefix, localName, xhtmlNamespaceURI);

443 444 445
    RefPtr<FormatBlockCommand> command = FormatBlockCommand::create(frame->document(), qualifiedTagName);
    applyCommand(command);
    return command->didApply();
darin@apple.com's avatar
darin@apple.com committed
446 447 448 449 450
}

static bool executeForwardDelete(Frame* frame, Event*, EditorCommandSource source, const String&)
{
    switch (source) {
451
    case CommandFromMenuOrKeyBinding:
452
        frame->editor()->deleteWithDirection(DirectionForward, CharacterGranularity, false, true);
453 454 455 456 457 458 459 460
        return true;
    case CommandFromDOM:
    case CommandFromDOMWithUserInterface:
        // Doesn't scroll to make the selection visible, or modify the kill ring.
        // ForwardDelete is not implemented in IE or Firefox, so this behavior is only needed for
        // backward compatibility with ourselves, and for consistency with Delete.
        TypingCommand::forwardDeleteKeyPressed(frame->document());
        return true;
darin@apple.com's avatar
darin@apple.com committed
461 462 463 464 465
    }
    ASSERT_NOT_REACHED();
    return false;
}

justin.garcia@apple.com's avatar
justin.garcia@apple.com committed
466 467 468 469 470 471
static bool executeIgnoreSpelling(Frame* frame, Event*, EditorCommandSource, const String&)
{
    frame->editor()->ignoreSpelling();
    return true;
}

darin@apple.com's avatar
darin@apple.com committed
472 473
static bool executeIndent(Frame* frame, Event*, EditorCommandSource, const String&)
{
darin@apple.com's avatar
darin@apple.com committed
474
    applyCommand(IndentOutdentCommand::create(frame->document(), IndentOutdentCommand::Indent));
darin@apple.com's avatar
darin@apple.com committed
475 476 477 478 479
    return true;
}

static bool executeInsertBacktab(Frame* frame, Event* event, EditorCommandSource, const String&)
{
480
    return targetFrame(frame, event)->eventHandler()->handleTextInputEvent("\t", event, TextEventInputBackTab);
darin@apple.com's avatar
darin@apple.com committed
481 482 483 484
}

static bool executeInsertHorizontalRule(Frame* frame, Event*, EditorCommandSource, const String& value)
{
485
    RefPtr<HTMLHRElement> rule = HTMLHRElement::create(frame->document());
darin@apple.com's avatar
darin@apple.com committed
486
    if (!value.isEmpty())
487
        rule->setIdAttribute(value);
488
    return executeInsertNode(frame, rule.release());
darin@apple.com's avatar
darin@apple.com committed
489 490 491 492 493 494 495 496 497 498
}

static bool executeInsertHTML(Frame* frame, Event*, EditorCommandSource, const String& value)
{
    return executeInsertFragment(frame, createFragmentFromMarkup(frame->document(), value, ""));
}

static bool executeInsertImage(Frame* frame, Event*, EditorCommandSource, const String& value)
{
    // FIXME: If userInterface is true, we should display a dialog box and let the user choose a local image.
499
    RefPtr<HTMLImageElement> image = HTMLImageElement::create(frame->document());
darin@apple.com's avatar
darin@apple.com committed
500 501 502 503 504 505 506
    image->setSrc(value);
    return executeInsertNode(frame, image.release());
}

static bool executeInsertLineBreak(Frame* frame, Event* event, EditorCommandSource source, const String&)
{
    switch (source) {
507
    case CommandFromMenuOrKeyBinding:
508
        return targetFrame(frame, event)->eventHandler()->handleTextInputEvent("\n", event, TextEventInputLineBreak);
509 510 511 512 513
    case CommandFromDOM:
    case CommandFromDOMWithUserInterface:
        // Doesn't scroll to make the selection visible, or modify the kill ring.
        // InsertLineBreak is not implemented in IE or Firefox, so this behavior is only needed for
        // backward compatibility with ourselves, and for consistency with other commands.
514
        TypingCommand::insertLineBreak(frame->document(), 0);
515
        return true;
darin@apple.com's avatar
darin@apple.com committed
516 517 518 519 520 521 522 523
    }
    ASSERT_NOT_REACHED();
    return false;
}

static bool executeInsertNewline(Frame* frame, Event* event, EditorCommandSource, const String&)
{
    Frame* targetFrame = WebCore::targetFrame(frame, event);
524
    return targetFrame->eventHandler()->handleTextInputEvent("\n", event, targetFrame->editor()->canEditRichly() ? TextEventInputKeyboard : TextEventInputLineBreak);
darin@apple.com's avatar
darin@apple.com committed
525 526 527 528 529 530 531 532
}

static bool executeInsertNewlineInQuotedContent(Frame* frame, Event*, EditorCommandSource, const String&)
{
    TypingCommand::insertParagraphSeparatorInQuotedContent(frame->document());
    return true;
}

justin.garcia@apple.com's avatar
justin.garcia@apple.com committed
533
static bool executeInsertOrderedList(Frame* frame, Event*, EditorCommandSource, const String&)
darin@apple.com's avatar
darin@apple.com committed
534
{
justin.garcia@apple.com's avatar
justin.garcia@apple.com committed
535
    applyCommand(InsertListCommand::create(frame->document(), InsertListCommand::OrderedList));
darin@apple.com's avatar
darin@apple.com committed
536 537 538 539 540
    return true;
}

static bool executeInsertParagraph(Frame* frame, Event*, EditorCommandSource, const String&)
{
541
    TypingCommand::insertParagraphSeparator(frame->document(), 0);
darin@apple.com's avatar
darin@apple.com committed
542 543 544 545 546
    return true;
}

static bool executeInsertTab(Frame* frame, Event* event, EditorCommandSource, const String&)
{
547
    return targetFrame(frame, event)->eventHandler()->handleTextInputEvent("\t", event);
darin@apple.com's avatar
darin@apple.com committed
548 549 550 551
}

static bool executeInsertText(Frame* frame, Event*, EditorCommandSource, const String& value)
{
552
    TypingCommand::insertText(frame->document(), value, 0);
darin@apple.com's avatar
darin@apple.com committed
553 554 555
    return true;
}

justin.garcia@apple.com's avatar
justin.garcia@apple.com committed
556
static bool executeInsertUnorderedList(Frame* frame, Event*, EditorCommandSource, const String&)
darin@apple.com's avatar
darin@apple.com committed
557
{
justin.garcia@apple.com's avatar
justin.garcia@apple.com committed
558
    applyCommand(InsertListCommand::create(frame->document(), InsertListCommand::UnorderedList));
darin@apple.com's avatar
darin@apple.com committed
559 560 561 562 563
    return true;
}

static bool executeJustifyCenter(Frame* frame, Event*, EditorCommandSource source, const String&)
{
564
    return executeApplyParagraphStyle(frame, source, EditActionCenter, CSSPropertyTextAlign, "center");
darin@apple.com's avatar
darin@apple.com committed
565 566 567 568
}

static bool executeJustifyFull(Frame* frame, Event*, EditorCommandSource source, const String&)
{
569
    return executeApplyParagraphStyle(frame, source, EditActionJustify, CSSPropertyTextAlign, "justify");
darin@apple.com's avatar
darin@apple.com committed
570 571 572 573
}

static bool executeJustifyLeft(Frame* frame, Event*, EditorCommandSource source, const String&)
{
574
    return executeApplyParagraphStyle(frame, source, EditActionAlignLeft, CSSPropertyTextAlign, "left");
darin@apple.com's avatar
darin@apple.com committed
575 576 577 578
}

static bool executeJustifyRight(Frame* frame, Event*, EditorCommandSource source, const String&)
{
579
    return executeApplyParagraphStyle(frame, source, EditActionAlignRight, CSSPropertyTextAlign, "right");
darin@apple.com's avatar
darin@apple.com committed
580 581
}

582
static bool executeMakeTextWritingDirectionLeftToRight(Frame* frame, Event*, EditorCommandSource, const String&)
mitz@apple.com's avatar
mitz@apple.com committed
583 584 585 586 587 588 589 590
{
    RefPtr<CSSMutableStyleDeclaration> style = CSSMutableStyleDeclaration::create();
    style->setProperty(CSSPropertyUnicodeBidi, CSSValueEmbed);
    style->setProperty(CSSPropertyDirection, CSSValueLtr);
    frame->editor()->applyStyle(style.get(), EditActionSetWritingDirection);
    return true;
}

591
static bool executeMakeTextWritingDirectionNatural(Frame* frame, Event*, EditorCommandSource, const String&)
mitz@apple.com's avatar
mitz@apple.com committed
592 593 594 595 596 597 598
{
    RefPtr<CSSMutableStyleDeclaration> style = CSSMutableStyleDeclaration::create();
    style->setProperty(CSSPropertyUnicodeBidi, CSSValueNormal);
    frame->editor()->applyStyle(style.get(), EditActionSetWritingDirection);
    return true;
}

599
static bool executeMakeTextWritingDirectionRightToLeft(Frame* frame, Event*, EditorCommandSource, const String&)
mitz@apple.com's avatar
mitz@apple.com committed
600 601 602 603 604 605 606 607
{
    RefPtr<CSSMutableStyleDeclaration> style = CSSMutableStyleDeclaration::create();
    style->setProperty(CSSPropertyUnicodeBidi, CSSValueEmbed);
    style->setProperty(CSSPropertyDirection, CSSValueRtl);
    frame->editor()->applyStyle(style.get(), EditActionSetWritingDirection);
    return true;
}

darin@apple.com's avatar
darin@apple.com committed
608 609
static bool executeMoveBackward(Frame* frame, Event*, EditorCommandSource, const String&)
{
610
    frame->selection()->modify(FrameSelection::AlterationMove, DirectionBackward, CharacterGranularity, true);
darin@apple.com's avatar
darin@apple.com committed
611 612 613 614 615
    return true;
}

static bool executeMoveBackwardAndModifySelection(Frame* frame, Event*, EditorCommandSource, const String&)
{
616
    frame->selection()->modify(FrameSelection::AlterationExtend, DirectionBackward, CharacterGranularity, true);
darin@apple.com's avatar
darin@apple.com committed
617 618 619 620 621
    return true;
}

static bool executeMoveDown(Frame* frame, Event*, EditorCommandSource, const String&)
{
622
    return frame->selection()->modify(FrameSelection::AlterationMove, DirectionForward, LineGranularity, true);
darin@apple.com's avatar
darin@apple.com committed
623 624 625 626
}

static bool executeMoveDownAndModifySelection(Frame* frame, Event*, EditorCommandSource, const String&)
{
627
    frame->selection()->modify(FrameSelection::AlterationExtend, DirectionForward, LineGranularity, true);
darin@apple.com's avatar
darin@apple.com committed
628 629 630 631 632
    return true;
}

static bool executeMoveForward(Frame* frame, Event*, EditorCommandSource, const String&)
{
633
    frame->selection()->modify(FrameSelection::AlterationMove, DirectionForward, CharacterGranularity, true);
darin@apple.com's avatar
darin@apple.com committed
634 635 636 637 638
    return true;
}

static bool executeMoveForwardAndModifySelection(Frame* frame, Event*, EditorCommandSource, const String&)
{
639
    frame->selection()->modify(FrameSelection::AlterationExtend, DirectionForward, CharacterGranularity, true);
darin@apple.com's avatar
darin@apple.com committed
640 641 642 643 644
    return true;
}

static bool executeMoveLeft(Frame* frame, Event*, EditorCommandSource, const String&)
{
645
    return frame->selection()->modify(FrameSelection::AlterationMove, DirectionLeft, CharacterGranularity, true);
darin@apple.com's avatar
darin@apple.com committed
646 647 648 649
}

static bool executeMoveLeftAndModifySelection(Frame* frame, Event*, EditorCommandSource, const String&)
{
650
    frame->selection()->modify(FrameSelection::AlterationExtend, DirectionLeft, CharacterGranularity, true);
darin@apple.com's avatar
darin@apple.com committed
651 652 653
    return true;
}

darin@apple.com's avatar
darin@apple.com committed
654 655 656 657 658
static bool executeMovePageDown(Frame* frame, Event*, EditorCommandSource, const String&)
{
    int distance = verticalScrollDistance(frame);
    if (!distance)
        return false;
659
    return frame->selection()->modify(FrameSelection::AlterationMove, distance, true, FrameSelection::AlignCursorOnScrollAlways);
darin@apple.com's avatar
darin@apple.com committed
660 661 662 663 664 665 666
}

static bool executeMovePageDownAndModifySelection(Frame* frame, Event*, EditorCommandSource, const String&)
{
    int distance = verticalScrollDistance(frame);
    if (!distance)
        return false;
667
    return frame->selection()->modify(FrameSelection::AlterationExtend, distance, true, FrameSelection::AlignCursorOnScrollAlways);
darin@apple.com's avatar
darin@apple.com committed
668 669 670 671 672 673 674
}

static bool executeMovePageUp(Frame* frame, Event*, EditorCommandSource, const String&)
{
    int distance = verticalScrollDistance(frame);
    if (!distance)
        return false;
675
    return frame->selection()->modify(FrameSelection::AlterationMove, -distance, true, FrameSelection::AlignCursorOnScrollAlways);
darin@apple.com's avatar
darin@apple.com committed
676 677 678 679 680 681 682
}

static bool executeMovePageUpAndModifySelection(Frame* frame, Event*, EditorCommandSource, const String&)
{
    int distance = verticalScrollDistance(frame);
    if (!distance)
        return false;
683
    return frame->selection()->modify(FrameSelection::AlterationExtend, -distance, true, FrameSelection::AlignCursorOnScrollAlways);
darin@apple.com's avatar
darin@apple.com committed
684 685
}

darin@apple.com's avatar
darin@apple.com committed
686 687
static bool executeMoveRight(Frame* frame, Event*, EditorCommandSource, const String&)
{
688
    return frame->selection()->modify(FrameSelection::AlterationMove, DirectionRight, CharacterGranularity, true);
darin@apple.com's avatar
darin@apple.com committed
689 690 691 692
}

static bool executeMoveRightAndModifySelection(Frame* frame, Event*, EditorCommandSource, const String&)
{
693
    frame->selection()->modify(FrameSelection::AlterationExtend, DirectionRight, CharacterGranularity, true);
darin@apple.com's avatar
darin@apple.com committed
694 695 696 697 698
    return true;
}

static bool executeMoveToBeginningOfDocument(Frame* frame, Event*, EditorCommandSource, const String&)
{
699
    frame->selection()->modify(FrameSelection::AlterationMove, DirectionBackward, DocumentBoundary, true);
darin@apple.com's avatar
darin@apple.com committed
700 701 702 703 704
    return true;
}

static bool executeMoveToBeginningOfDocumentAndModifySelection(Frame* frame, Event*, EditorCommandSource, const String&)
{
705
    frame->selection()->modify(FrameSelection::AlterationExtend, DirectionBackward, DocumentBoundary, true);
darin@apple.com's avatar
darin@apple.com committed
706 707 708 709 710
    return true;
}

static bool executeMoveToBeginningOfLine(Frame* frame, Event*, EditorCommandSource, const String&)
{
711
    frame->selection()->modify(FrameSelection::AlterationMove, DirectionBackward, LineBoundary, true);
darin@apple.com's avatar
darin@apple.com committed
712 713 714 715 716
    return true;
}

static bool executeMoveToBeginningOfLineAndModifySelection(Frame* frame, Event*, EditorCommandSource, const String&)
{
717
    frame->selection()->modify(FrameSelection::AlterationExtend, DirectionBackward, LineBoundary, true);
darin@apple.com's avatar
darin@apple.com committed
718 719 720 721 722
    return true;
}

static bool executeMoveToBeginningOfParagraph(Frame* frame, Event*, EditorCommandSource, const String&)
{
723
    frame->selection()->modify(FrameSelection::AlterationMove, DirectionBackward, ParagraphBoundary, true);
darin@apple.com's avatar
darin@apple.com committed
724 725 726 727 728
    return true;
}

static bool executeMoveToBeginningOfParagraphAndModifySelection(Frame* frame, Event*, EditorCommandSource, const String&)
{
729
    frame->selection()->modify(FrameSelection::AlterationExtend, DirectionBackward, ParagraphBoundary, true);
darin@apple.com's avatar
darin@apple.com committed
730 731 732 733 734
    return true;
}

static bool executeMoveToBeginningOfSentence(Frame* frame, Event*, EditorCommandSource, const String&)
{
735
    frame->selection()->modify(FrameSelection::AlterationMove, DirectionBackward, SentenceBoundary, true);
darin@apple.com's avatar
darin@apple.com committed
736 737 738 739 740
    return true;
}

static bool executeMoveToBeginningOfSentenceAndModifySelection(Frame* frame, Event*, EditorCommandSource, const String&)
{
741
    frame->selection()->modify(FrameSelection::AlterationExtend, DirectionBackward, SentenceBoundary, true);
darin@apple.com's avatar
darin@apple.com committed
742 743 744 745 746
    return true;
}

static bool executeMoveToEndOfDocument(Frame* frame, Event*, EditorCommandSource, const String&)
{
747
    frame->selection()->modify(FrameSelection::AlterationMove, DirectionForward, DocumentBoundary, true);
darin@apple.com's avatar
darin@apple.com committed
748 749 750 751 752
    return true;
}

static bool executeMoveToEndOfDocumentAndModifySelection(Frame* frame, Event*, EditorCommandSource, const String&)
{
753
    frame->selection()->modify(FrameSelection::AlterationExtend, DirectionForward, DocumentBoundary, true);
darin@apple.com's avatar
darin@apple.com committed
754 755 756 757 758
    return true;
}

static bool executeMoveToEndOfSentence(Frame* frame, Event*, EditorCommandSource, const String&)
{
759
    frame->selection()->modify(FrameSelection::AlterationMove, DirectionForward, SentenceBoundary, true);
darin@apple.com's avatar
darin@apple.com committed
760 761 762 763 764
    return true;
}

static bool executeMoveToEndOfSentenceAndModifySelection(Frame* frame, Event*, EditorCommandSource, const String&)
{
765
    frame->selection()->modify(FrameSelection::AlterationExtend, DirectionForward, SentenceBoundary, true);
darin@apple.com's avatar
darin@apple.com committed
766 767 768 769 770
    return true;
}

static bool executeMoveToEndOfLine(Frame* frame, Event*, EditorCommandSource, const String&)
{
771
    frame->selection()->modify(FrameSelection::AlterationMove, DirectionForward, LineBoundary, true);
darin@apple.com's avatar
darin@apple.com committed
772 773 774 775 776
    return true;
}

static bool executeMoveToEndOfLineAndModifySelection(Frame* frame, Event*, EditorCommandSource, const String&)
{
777
    frame->selection()->modify(FrameSelection::AlterationExtend, DirectionForward, LineBoundary, true);
darin@apple.com's avatar
darin@apple.com committed
778 779 780 781 782
    return true;
}

static bool executeMoveToEndOfParagraph(Frame* frame, Event*, EditorCommandSource, const String&)
{
783
    frame->selection()->modify(FrameSelection::AlterationMove, DirectionForward, ParagraphBoundary, true);
darin@apple.com's avatar
darin@apple.com committed
784 785 786 787 788
    return true;
}

static bool executeMoveToEndOfParagraphAndModifySelection(Frame* frame, Event*, EditorCommandSource, const String&)
{
789
    frame->selection()->modify(FrameSelection::AlterationExtend, DirectionForward, ParagraphBoundary, true);
darin@apple.com's avatar
darin@apple.com committed
790 791 792 793 794
    return true;
}

static bool executeMoveParagraphBackwardAndModifySelection(Frame* frame, Event*, EditorCommandSource, const String&)
{
795
    frame->selection()->modify(FrameSelection::AlterationExtend, DirectionBackward, ParagraphGranularity, true);
darin@apple.com's avatar
darin@apple.com committed
796 797 798 799 800
    return true;
}

static bool executeMoveParagraphForwardAndModifySelection(Frame* frame, Event*, EditorCommandSource, const String&)
{
801
    frame->selection()->modify(FrameSelection::AlterationExtend, DirectionForward, ParagraphGranularity, true);
darin@apple.com's avatar
darin@apple.com committed
802 803 804 805 806
    return true;
}

static bool executeMoveUp(Frame* frame, Event*, EditorCommandSource, const String&)
{
807
    return frame->selection()->modify(FrameSelection::AlterationMove, DirectionBackward, LineGranularity, true);
darin@apple.com's avatar
darin@apple.com committed
808 809 810 811
}

static bool executeMoveUpAndModifySelection(Frame* frame, Event*, EditorCommandSource, const String&)
{
812
    frame->selection()->modify(FrameSelection::AlterationExtend, DirectionBackward, LineGranularity, true);
darin@apple.com's avatar
darin@apple.com committed
813 814 815 816 817
    return true;
}

static bool executeMoveWordBackward(Frame* frame, Event*, EditorCommandSource, const String&)
{
818
    frame->selection()->modify(FrameSelection::AlterationMove, DirectionBackward, WordGranularity, true);
darin@apple.com's avatar
darin@apple.com committed
819 820 821 822 823
    return true;
}

static bool executeMoveWordBackwardAndModifySelection(Frame* frame, Event*, EditorCommandSource, const String&)
{
824
    frame->selection()->modify(FrameSelection::AlterationExtend, DirectionBackward, WordGranularity, true);
darin@apple.com's avatar
darin@apple.com committed
825 826 827 828 829
    return true;
}

static bool executeMoveWordForward(Frame* frame, Event*, EditorCommandSource, const String&)
{
830
    frame->selection()->modify(FrameSelection::AlterationMove, DirectionForward, WordGranularity, true);
darin@apple.com's avatar
darin@apple.com committed
831 832 833 834 835
    return true;
}

static bool executeMoveWordForwardAndModifySelection(Frame* frame, Event*, EditorCommandSource, const String&)
{
836
    frame->selection()->modify(FrameSelection::AlterationExtend, DirectionForward, WordGranularity, true);
darin@apple.com's avatar
darin@apple.com committed
837 838 839 840 841
    return true;
}

static bool executeMoveWordLeft(Frame* frame, Event*, EditorCommandSource, const String&)
{
842
    frame->selection()->modify(FrameSelection::AlterationMove, DirectionLeft, WordGranularity, true);
darin@apple.com's avatar
darin@apple.com committed
843 844 845 846 847
    return true;
}

static bool executeMoveWordLeftAndModifySelection(Frame* frame, Event*, EditorCommandSource, const String&)
{
848
    frame->selection()->modify(FrameSelection::AlterationExtend, DirectionLeft, WordGranularity, true);
darin@apple.com's avatar
darin@apple.com committed
849 850 851 852 853
    return true;
}

static bool executeMoveWordRight(Frame* frame, Event*, EditorCommandSource, const String&)
{
854
    frame->selection()->modify(FrameSelection::AlterationMove, DirectionRight, WordGranularity, true);
darin@apple.com's avatar
darin@apple.com committed
855 856 857 858 859
    return true;
}

static bool executeMoveWordRightAndModifySelection(Frame* frame, Event*, EditorCommandSource, const String&)
{
860
    frame->selection()->modify(FrameSelection::AlterationExtend, DirectionRight, WordGranularity, true);
darin@apple.com's avatar
darin@apple.com committed
861 862 863
    return true;
}

darin@apple.com's avatar
darin@apple.com committed
864 865
static bool executeMoveToLeftEndOfLine(Frame* frame, Event*, EditorCommandSource, const String&)
{
866
    frame->selection()->modify(FrameSelection::AlterationMove, DirectionLeft, LineBoundary, true);
darin@apple.com's avatar
darin@apple.com committed
867 868 869 870 871
    return true;
}

static bool executeMoveToLeftEndOfLineAndModifySelection(Frame* frame, Event*, EditorCommandSource, const String&)
{
872
    frame->selection()->modify(FrameSelection::AlterationExtend, DirectionLeft, LineBoundary, true);
darin@apple.com's avatar
darin@apple.com committed
873 874 875 876 877
    return true;
}

static bool executeMoveToRightEndOfLine(Frame* frame, Event*, EditorCommandSource, const String&)
{
878
    frame->selection()->modify(FrameSelection::AlterationMove, DirectionRight, LineBoundary, true);
darin@apple.com's avatar
darin@apple.com committed
879 880 881 882 883
    return true;
}

static bool executeMoveToRightEndOfLineAndModifySelection(Frame* frame, Event*, EditorCommandSource, const String&)
{
884
    frame->selection()->modify(FrameSelection::AlterationExtend, DirectionRight, LineBoundary, true);
darin@apple.com's avatar
darin@apple.com committed
885 886 887
    return true;
}

darin@apple.com's avatar
darin@apple.com committed
888 889
static bool executeOutdent(Frame* frame, Event*, EditorCommandSource, const String&)
{
darin@apple.com's avatar
darin@apple.com committed
890
    applyCommand(IndentOutdentCommand::create(frame->document(), IndentOutdentCommand::Outdent));
darin@apple.com's avatar
darin@apple.com committed
891 892 893
    return true;
}

894
static bool executePaste(Frame* frame, Event*, EditorCommandSource source, const String&)
darin@apple.com's avatar
darin@apple.com committed
895
{
896 897 898 899 900
    if (source == CommandFromMenuOrKeyBinding) {
        UserTypingGestureIndicator typingGestureIndicator(frame);
        frame->editor()->paste();
    } else
        frame->editor()->paste();
darin@apple.com's avatar
darin@apple.com committed
901 902 903
    return true;
}

904
static bool executePasteAndMatchStyle(Frame* frame, Event*, EditorCommandSource source, const String&)
darin@apple.com's avatar
darin@apple.com committed
905
{
906 907 908 909 910
    if (source == CommandFromMenuOrKeyBinding) {
        UserTypingGestureIndicator typingGestureIndicator(frame);
        frame->editor()->pasteAsPlainText();
    } else
        frame->editor()->pasteAsPlainText();
darin@apple.com's avatar
darin@apple.com committed
911 912 913
    return true;
}

914
static bool executePasteAsPlainText(Frame* frame, Event*, EditorCommandSource source, const String&)
915
{
916 917 918 919 920
    if (source == CommandFromMenuOrKeyBinding) {
        UserTypingGestureIndicator typingGestureIndicator(frame);
        frame->editor()->pasteAsPlainText();
    } else
        frame->editor()->pasteAsPlainText();
921 922 923
    return true;
}

darin@apple.com's avatar
darin@apple.com committed
924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944
static bool executePrint(Frame* frame, Event*, EditorCommandSource, const String&)
{
    Page* page = frame->page();
    if (!page)
        return false;
    page->chrome()->print(frame);
    return true;
}

static bool executeRedo(Frame* frame, Event*, EditorCommandSource, const String&)
{
    frame->editor()->redo();
    return true;
}

static bool executeRemoveFormat(Frame* frame, Event*, EditorCommandSource, const String&)
{
    frame->editor()->removeFormattingAndStyle();
    return true;
}

945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964
static bool executeScrollPageBackward(Frame* frame, Event*, EditorCommandSource, const String&)
{
    return frame->eventHandler()->logicalScrollRecursively(ScrollBlockDirectionBackward, ScrollByPage);
}

static bool executeScrollPageForward(Frame* frame, Event*, EditorCommandSource, const String&)
{
    return frame->eventHandler()->logicalScrollRecursively(ScrollBlockDirectionForward, ScrollByPage);
}

static bool executeScrollToBeginningOfDocument(Frame* frame, Event*, EditorCommandSource, const String&)
{
    return frame->eventHandler()->logicalScrollRecursively(ScrollBlockDirectionBackward, ScrollByDocument);
}

static bool executeScrollToEndOfDocument(Frame* frame, Event*, EditorCommandSource, const String&)
{
    return frame->eventHandler()->logicalScrollRecursively(ScrollBlockDirectionForward, ScrollByDocument);
}

darin@apple.com's avatar
darin@apple.com committed
965 966
static bool executeSelectAll(Frame* frame, Event*, EditorCommandSource, const String&)
{
darin@apple.com's avatar
darin@apple.com committed
967
    frame->selection()->selectAll();
darin@apple.com's avatar
darin@apple.com committed
968 969 970
    return true;
}

darin@apple.com's avatar
darin@apple.com committed
971 972 973 974 975 976 977 978 979 980 981 982 983 984 985
static bool executeSelectLine(Frame* frame, Event*, EditorCommandSource, const String&)
{
    return expandSelectionToGranularity(frame, LineGranularity);
}

static bool executeSelectParagraph(Frame* frame, Event*, EditorCommandSource, const String&)
{
    return expandSelectionToGranularity(frame, ParagraphGranularity);
}

static bool executeSelectSentence(Frame* frame, Event*, EditorCommandSource, const String&)
{
    return expandSelectionToGranularity(frame, SentenceGranularity);
}

darin@apple.com's avatar
darin@apple.com committed
986 987
static bool executeSelectToMark(Frame* frame, Event*, EditorCommandSource, const String&)
{
988
    RefPtr<Range> mark = frame->editor()->mark().toNormalizedRange();
darin@apple.com's avatar
darin@apple.com committed
989 990 991 992 993
    RefPtr<Range> selection = frame->editor()->selectedRange();
    if (!mark || !selection) {
        systemBeep();
        return false;
    }
darin@apple.com's avatar
darin@apple.com committed
994
    frame->selection()->setSelectedRange(unionDOMRanges(mark.get(), selection.get()).get(), DOWNSTREAM, true);
darin@apple.com's avatar
darin@apple.com committed
995 996 997
    return true;
}