7443f423c0be71864a4ea34c27b322a4970c155b
[WebKit-https.git] / WebCore / editing / EditorCommand.cpp
1 /*
2  * Copyright (C) 2006, 2007, 2008 Apple Inc. All rights reserved.
3  * Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies)
4  * Copyright (C) 2009 Igalia S.L.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
16  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
19  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
23  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
26  */
27
28 #include "config.h"
29 #include "Editor.h"
30
31 #include "CSSComputedStyleDeclaration.h"
32 #include "CSSMutableStyleDeclaration.h"
33 #include "CSSPropertyNames.h"
34 #include "CSSValueKeywords.h"
35 #include "Chrome.h"
36 #include "CreateLinkCommand.h"
37 #include "DocumentFragment.h"
38 #include "EditorClient.h"
39 #include "Event.h"
40 #include "EventHandler.h"
41 #include "FormatBlockCommand.h"
42 #include "Frame.h"
43 #include "FrameView.h"
44 #include "HTMLFontElement.h"
45 #include "HTMLHRElement.h"
46 #include "HTMLImageElement.h"
47 #include "IndentOutdentCommand.h"
48 #include "InsertListCommand.h"
49 #include "KillRing.h"
50 #include "Page.h"
51 #include "RenderBox.h"
52 #include "ReplaceSelectionCommand.h"
53 #include "Scrollbar.h"
54 #include "Settings.h"
55 #include "Sound.h"
56 #include "TypingCommand.h"
57 #include "UnlinkCommand.h"
58 #include "htmlediting.h"
59 #include "markup.h"
60 #include <wtf/text/AtomicString.h>
61
62 namespace WebCore {
63
64 using namespace HTMLNames;
65
66 class EditorInternalCommand {
67 public:
68     bool (*execute)(Frame*, Event*, EditorCommandSource, const String&);
69     bool (*isSupported)(Frame*, EditorCommandSource);
70     bool (*isEnabled)(Frame*, Event*, EditorCommandSource);
71     TriState (*state)(Frame*, Event*);
72     String (*value)(Frame*, Event*);
73     bool isTextInsertion;
74     bool allowExecutionWhenDisabled;
75 };
76
77 typedef HashMap<String, const EditorInternalCommand*, CaseFoldingHash> CommandMap;
78
79 static const bool notTextInsertion = false;
80 static const bool isTextInsertion = true;
81
82 static const bool allowExecutionWhenDisabled = true;
83 static const bool doNotAllowExecutionWhenDisabled = false;
84
85 // Related to Editor::selectionForCommand.
86 // Certain operations continue to use the target control's selection even if the event handler
87 // already moved the selection outside of the text control.
88 static Frame* targetFrame(Frame* frame, Event* event)
89 {
90     if (!event)
91         return frame;
92     Node* node = event->target()->toNode();
93     if (!node)
94         return frame;
95     return node->document()->frame();
96 }
97
98 static bool applyCommandToFrame(Frame* frame, EditorCommandSource source, EditAction action, CSSMutableStyleDeclaration* style)
99 {
100     // FIXME: We don't call shouldApplyStyle when the source is DOM; is there a good reason for that?
101     switch (source) {
102     case CommandFromMenuOrKeyBinding:
103         frame->editor()->applyStyleToSelection(style, action);
104         return true;
105     case CommandFromDOM:
106     case CommandFromDOMWithUserInterface:
107         frame->editor()->applyStyle(style);
108         return true;
109     }
110     ASSERT_NOT_REACHED();
111     return false;
112 }
113
114 static bool executeApplyStyle(Frame* frame, EditorCommandSource source, EditAction action, int propertyID, const String& propertyValue)
115 {
116     RefPtr<CSSMutableStyleDeclaration> style = CSSMutableStyleDeclaration::create();
117     style->setProperty(propertyID, propertyValue);
118     return applyCommandToFrame(frame, source, action, style.get());
119 }
120
121 static bool executeApplyStyle(Frame* frame, EditorCommandSource source, EditAction action, int propertyID, int propertyValue)
122 {
123     RefPtr<CSSMutableStyleDeclaration> style = CSSMutableStyleDeclaration::create();
124     style->setProperty(propertyID, propertyValue);
125     return applyCommandToFrame(frame, source, action, style.get());
126 }
127
128 // FIXME: executeToggleStyleInList does not handle complicated cases such as <b><u>hello</u>world</b> properly.
129 //        This function must use Editor::selectionHasStyle to determine the current style but we cannot fix this
130 //        until https://bugs.webkit.org/show_bug.cgi?id=27818 is resolved.
131 static bool executeToggleStyleInList(Frame* frame, EditorCommandSource source, EditAction action, int propertyID, CSSValue* value)
132 {
133     ExceptionCode ec = 0;
134     bool shouldUseFixedFontDefaultSize;
135     RefPtr<CSSMutableStyleDeclaration> selectionStyle = frame->editor()->selectionComputedStyle(shouldUseFixedFontDefaultSize);
136     if (!selectionStyle)
137         return false;
138
139     RefPtr<CSSValue> selectedCSSValue = selectionStyle->getPropertyCSSValue(propertyID);
140     String newStyle = "none";
141     if (selectedCSSValue->isValueList()) {
142         RefPtr<CSSValueList> selectedCSSValueList = static_cast<CSSValueList*>(selectedCSSValue.get());
143         if (!selectedCSSValueList->removeAll(value))
144             selectedCSSValueList->append(value);
145         if (selectedCSSValueList->length())
146             newStyle = selectedCSSValueList->cssText();
147
148     } else if (selectedCSSValue->cssText() == "none")
149         newStyle = value->cssText();
150
151     // FIXME: We shouldn't be having to convert new style into text.  We should have setPropertyCSSValue.
152     RefPtr<CSSMutableStyleDeclaration> newMutableStyle = CSSMutableStyleDeclaration::create();
153     newMutableStyle->setProperty(propertyID, newStyle, ec);
154     return applyCommandToFrame(frame, source, action, newMutableStyle.get());
155 }
156
157 static bool executeToggleStyle(Frame* frame, EditorCommandSource source, EditAction action, int propertyID, const char* offValue, const char* onValue)
158 {
159     RefPtr<CSSMutableStyleDeclaration> style = CSSMutableStyleDeclaration::create();
160     style->setProperty(propertyID, onValue); // We need to add this style to pass it to selectionStartHasStyle / selectionHasStyle
161
162     // Style is considered present when
163     // Mac: present at the beginning of selection
164     // other: present throughout the selection
165
166     bool styleIsPresent;
167     if (frame->editor()->behavior().shouldToggleStyleBasedOnStartOfSelection())
168         styleIsPresent = frame->editor()->selectionStartHasStyle(style.get());
169     else
170         styleIsPresent = frame->editor()->selectionHasStyle(style.get()) == TrueTriState;
171
172     style->setProperty(propertyID, styleIsPresent ? offValue : onValue);
173     return applyCommandToFrame(frame, source, action, style.get());
174 }
175
176 static bool executeApplyParagraphStyle(Frame* frame, EditorCommandSource source, EditAction action, int propertyID, const String& propertyValue)
177 {
178     RefPtr<CSSMutableStyleDeclaration> style = CSSMutableStyleDeclaration::create();
179     style->setProperty(propertyID, propertyValue);
180     // FIXME: We don't call shouldApplyStyle when the source is DOM; is there a good reason for that?
181     switch (source) {
182     case CommandFromMenuOrKeyBinding:
183         frame->editor()->applyParagraphStyleToSelection(style.get(), action);
184         return true;
185     case CommandFromDOM:
186     case CommandFromDOMWithUserInterface:
187         frame->editor()->applyParagraphStyle(style.get());
188         return true;
189     }
190     ASSERT_NOT_REACHED();
191     return false;
192 }
193
194 static bool executeInsertFragment(Frame* frame, PassRefPtr<DocumentFragment> fragment)
195 {
196     applyCommand(ReplaceSelectionCommand::create(frame->document(), fragment,
197         false, false, false, true, false, EditActionUnspecified));
198     return true;
199 }
200
201 static bool executeInsertNode(Frame* frame, PassRefPtr<Node> content)
202 {
203     RefPtr<DocumentFragment> fragment = DocumentFragment::create(frame->document());
204     ExceptionCode ec = 0;
205     fragment->appendChild(content, ec);
206     if (ec)
207         return false;
208     return executeInsertFragment(frame, fragment.release());
209 }
210
211 static bool expandSelectionToGranularity(Frame* frame, TextGranularity granularity)
212 {
213     VisibleSelection selection = frame->selection()->selection();
214     selection.expandUsingGranularity(granularity);
215     RefPtr<Range> newRange = selection.toNormalizedRange();
216     if (!newRange)
217         return false;
218     ExceptionCode ec = 0;
219     if (newRange->collapsed(ec))
220         return false;
221     RefPtr<Range> oldRange = frame->selection()->selection().toNormalizedRange();
222     EAffinity affinity = frame->selection()->affinity();
223     if (!frame->editor()->client()->shouldChangeSelectedRange(oldRange.get(), newRange.get(), affinity, false))
224         return false;
225     frame->selection()->setSelectedRange(newRange.get(), affinity, true);
226     return true;
227 }
228
229 static TriState stateStyle(Frame* frame, int propertyID, const char* desiredValue)
230 {
231     RefPtr<CSSMutableStyleDeclaration> style = CSSMutableStyleDeclaration::create();
232     style->setProperty(propertyID, desiredValue);
233
234     if (frame->editor()->behavior().shouldToggleStyleBasedOnStartOfSelection())
235         return frame->editor()->selectionStartHasStyle(style.get()) ? TrueTriState : FalseTriState;
236     return frame->editor()->selectionHasStyle(style.get());
237 }
238
239 static String valueStyle(Frame* frame, int propertyID)
240 {
241     // FIXME: Rather than retrieving the style at the start of the current selection,
242     // we should retrieve the style present throughout the selection for non-Mac platforms.
243     return frame->editor()->selectionStartCSSPropertyValue(propertyID);
244 }
245
246 static TriState stateTextWritingDirection(Frame* frame, WritingDirection direction)
247 {
248     bool hasNestedOrMultipleEmbeddings;
249     WritingDirection selectionDirection = frame->editor()->textDirectionForSelection(hasNestedOrMultipleEmbeddings);
250     return (selectionDirection == direction && !hasNestedOrMultipleEmbeddings) ? TrueTriState : FalseTriState;
251 }
252
253 static int verticalScrollDistance(Frame* frame)
254 {
255     Node* focusedNode = frame->document()->focusedNode();
256     if (!focusedNode)
257         return 0;
258     RenderObject* renderer = focusedNode->renderer();
259     if (!renderer || !renderer->isBox())
260         return 0;
261     RenderStyle* style = renderer->style();
262     if (!style)
263         return 0;
264     if (!(style->overflowY() == OSCROLL || style->overflowY() == OAUTO || focusedNode->isContentEditable()))
265         return 0;
266     int height = std::min<int>(toRenderBox(renderer)->clientHeight(),
267                                frame->view()->visibleHeight());
268     return max(max<int>(height * Scrollbar::minFractionToStepWhenPaging(), height - Scrollbar::maxOverlapBetweenPages()), 1);
269 }
270
271 static RefPtr<Range> unionDOMRanges(Range* a, Range* b)
272 {
273     ExceptionCode ec = 0;
274     Range* start = a->compareBoundaryPoints(Range::START_TO_START, b, ec) <= 0 ? a : b;
275     ASSERT(!ec);
276     Range* end = a->compareBoundaryPoints(Range::END_TO_END, b, ec) <= 0 ? b : a;
277     ASSERT(!ec);
278
279     return Range::create(a->startContainer(ec)->ownerDocument(), start->startContainer(ec), start->startOffset(ec), end->endContainer(ec), end->endOffset(ec));
280 }
281
282 // Execute command functions
283
284 static bool executeBackColor(Frame* frame, Event*, EditorCommandSource source, const String& value)
285 {
286     return executeApplyStyle(frame, source, EditActionSetBackgroundColor, CSSPropertyBackgroundColor, value);
287 }
288
289 static bool executeCopy(Frame* frame, Event*, EditorCommandSource, const String&)
290 {
291     frame->editor()->copy();
292     return true;
293 }
294
295 static bool executeCreateLink(Frame* frame, Event*, EditorCommandSource, const String& value)
296 {
297     // FIXME: If userInterface is true, we should display a dialog box to let the user enter a URL.
298     if (value.isEmpty())
299         return false;
300     applyCommand(CreateLinkCommand::create(frame->document(), value));
301     return true;
302 }
303
304 static bool executeCut(Frame* frame, Event*, EditorCommandSource, const String&)
305 {
306     frame->editor()->cut();
307     return true;
308 }
309
310 static bool executeDelete(Frame* frame, Event*, EditorCommandSource source, const String&)
311 {
312     switch (source) {
313     case CommandFromMenuOrKeyBinding:
314         // Doesn't modify the text if the current selection isn't a range.
315         frame->editor()->performDelete();
316         return true;
317     case CommandFromDOM:
318     case CommandFromDOMWithUserInterface:
319         // If the current selection is a caret, delete the preceding character. IE performs forwardDelete, but we currently side with Firefox.
320         // Doesn't scroll to make the selection visible, or modify the kill ring (this time, siding with IE, not Firefox).
321         TypingCommand::deleteKeyPressed(frame->document(), frame->selection()->granularity() == WordGranularity);
322         return true;
323     }
324     ASSERT_NOT_REACHED();
325     return false;
326 }
327
328 static bool executeDeleteBackward(Frame* frame, Event*, EditorCommandSource, const String&)
329 {
330     frame->editor()->deleteWithDirection(SelectionController::DirectionBackward, CharacterGranularity, false, true);
331     return true;
332 }
333
334 static bool executeDeleteBackwardByDecomposingPreviousCharacter(Frame* frame, Event*, EditorCommandSource, const String&)
335 {
336     LOG_ERROR("DeleteBackwardByDecomposingPreviousCharacter is not implemented, doing DeleteBackward instead");
337     frame->editor()->deleteWithDirection(SelectionController::DirectionBackward, CharacterGranularity, false, true);
338     return true;
339 }
340
341 static bool executeDeleteForward(Frame* frame, Event*, EditorCommandSource, const String&)
342 {
343     frame->editor()->deleteWithDirection(SelectionController::DirectionForward, CharacterGranularity, false, true);
344     return true;
345 }
346
347 static bool executeDeleteToBeginningOfLine(Frame* frame, Event*, EditorCommandSource, const String&)
348 {
349     frame->editor()->deleteWithDirection(SelectionController::DirectionBackward, LineBoundary, true, false);
350     return true;
351 }
352
353 static bool executeDeleteToBeginningOfParagraph(Frame* frame, Event*, EditorCommandSource, const String&)
354 {
355     frame->editor()->deleteWithDirection(SelectionController::DirectionBackward, ParagraphBoundary, true, false);
356     return true;
357 }
358
359 static bool executeDeleteToEndOfLine(Frame* frame, Event*, EditorCommandSource, const String&)
360 {
361     // Despite its name, this command should delete the newline at the end of
362     // a paragraph if you are at the end of a paragraph (like DeleteToEndOfParagraph).
363     frame->editor()->deleteWithDirection(SelectionController::DirectionForward, LineBoundary, true, false);
364     return true;
365 }
366
367 static bool executeDeleteToEndOfParagraph(Frame* frame, Event*, EditorCommandSource, const String&)
368 {
369     // Despite its name, this command should delete the newline at the end of
370     // a paragraph if you are at the end of a paragraph.
371     frame->editor()->deleteWithDirection(SelectionController::DirectionForward, ParagraphBoundary, true, false);
372     return true;
373 }
374
375 static bool executeDeleteToMark(Frame* frame, Event*, EditorCommandSource, const String&)
376 {
377     RefPtr<Range> mark = frame->editor()->mark().toNormalizedRange();
378     if (mark) {
379         SelectionController* selection = frame->selection();
380         bool selected = selection->setSelectedRange(unionDOMRanges(mark.get(), frame->editor()->selectedRange().get()).get(), DOWNSTREAM, true);
381         ASSERT(selected);
382         if (!selected)
383             return false;
384     }
385     frame->editor()->performDelete();
386     frame->editor()->setMark(frame->selection()->selection());
387     return true;
388 }
389
390 static bool executeDeleteWordBackward(Frame* frame, Event*, EditorCommandSource, const String&)
391 {
392     frame->editor()->deleteWithDirection(SelectionController::DirectionBackward, WordGranularity, true, false);
393     return true;
394 }
395
396 static bool executeDeleteWordForward(Frame* frame, Event*, EditorCommandSource, const String&)
397 {
398     frame->editor()->deleteWithDirection(SelectionController::DirectionForward, WordGranularity, true, false);
399     return true;
400 }
401
402 static bool executeFindString(Frame* frame, Event*, EditorCommandSource, const String& value)
403 {
404     return frame->editor()->findString(value, true, false, true, false);
405 }
406
407 static bool executeFontName(Frame* frame, Event*, EditorCommandSource source, const String& value)
408 {
409     return executeApplyStyle(frame, source, EditActionSetFont, CSSPropertyFontFamily, value);
410 }
411
412 static bool executeFontSize(Frame* frame, Event*, EditorCommandSource source, const String& value)
413 {
414     int size;
415     if (!HTMLFontElement::cssValueFromFontSizeNumber(value, size))
416         return false;
417     return executeApplyStyle(frame, source, EditActionChangeAttributes, CSSPropertyFontSize, size);
418 }
419
420 static bool executeFontSizeDelta(Frame* frame, Event*, EditorCommandSource source, const String& value)
421 {
422     return executeApplyStyle(frame, source, EditActionChangeAttributes, CSSPropertyWebkitFontSizeDelta, value);
423 }
424
425 static bool executeForeColor(Frame* frame, Event*, EditorCommandSource source, const String& value)
426 {
427     return executeApplyStyle(frame, source, EditActionSetColor, CSSPropertyColor, value);
428 }
429
430 static bool executeFormatBlock(Frame* frame, Event*, EditorCommandSource, const String& value)
431 {
432     String tagName = value.lower();
433     if (tagName[0] == '<' && tagName[tagName.length() - 1] == '>')
434         tagName = tagName.substring(1, tagName.length() - 2);
435     if (!validBlockTag(tagName))
436         return false;
437     applyCommand(FormatBlockCommand::create(frame->document(), tagName));
438     return true;
439 }
440
441 static bool executeForwardDelete(Frame* frame, Event*, EditorCommandSource source, const String&)
442 {
443     switch (source) {
444     case CommandFromMenuOrKeyBinding:
445         frame->editor()->deleteWithDirection(SelectionController::DirectionForward, CharacterGranularity, false, true);
446         return true;
447     case CommandFromDOM:
448     case CommandFromDOMWithUserInterface:
449         // Doesn't scroll to make the selection visible, or modify the kill ring.
450         // ForwardDelete is not implemented in IE or Firefox, so this behavior is only needed for
451         // backward compatibility with ourselves, and for consistency with Delete.
452         TypingCommand::forwardDeleteKeyPressed(frame->document());
453         return true;
454     }
455     ASSERT_NOT_REACHED();
456     return false;
457 }
458
459 static bool executeIgnoreSpelling(Frame* frame, Event*, EditorCommandSource, const String&)
460 {
461     frame->editor()->ignoreSpelling();
462     return true;
463 }
464
465 static bool executeIndent(Frame* frame, Event*, EditorCommandSource, const String&)
466 {
467     applyCommand(IndentOutdentCommand::create(frame->document(), IndentOutdentCommand::Indent));
468     return true;
469 }
470
471 static bool executeInsertBacktab(Frame* frame, Event* event, EditorCommandSource, const String&)
472 {
473     return targetFrame(frame, event)->eventHandler()->handleTextInputEvent("\t", event, false, true);
474 }
475
476 static bool executeInsertHorizontalRule(Frame* frame, Event*, EditorCommandSource, const String& value)
477 {
478     RefPtr<HTMLHRElement> rule = HTMLHRElement::create(frame->document());
479     if (!value.isEmpty())
480         rule->setIdAttribute(value);
481     return executeInsertNode(frame, rule.release());
482 }
483
484 static bool executeInsertHTML(Frame* frame, Event*, EditorCommandSource, const String& value)
485 {
486     return executeInsertFragment(frame, createFragmentFromMarkup(frame->document(), value, ""));
487 }
488
489 static bool executeInsertImage(Frame* frame, Event*, EditorCommandSource, const String& value)
490 {
491     // FIXME: If userInterface is true, we should display a dialog box and let the user choose a local image.
492     RefPtr<HTMLImageElement> image = HTMLImageElement::create(frame->document());
493     image->setSrc(value);
494     return executeInsertNode(frame, image.release());
495 }
496
497 static bool executeInsertLineBreak(Frame* frame, Event* event, EditorCommandSource source, const String&)
498 {
499     switch (source) {
500     case CommandFromMenuOrKeyBinding:
501         return targetFrame(frame, event)->eventHandler()->handleTextInputEvent("\n", event, true);
502     case CommandFromDOM:
503     case CommandFromDOMWithUserInterface:
504         // Doesn't scroll to make the selection visible, or modify the kill ring.
505         // InsertLineBreak is not implemented in IE or Firefox, so this behavior is only needed for
506         // backward compatibility with ourselves, and for consistency with other commands.
507         TypingCommand::insertLineBreak(frame->document());
508         return true;
509     }
510     ASSERT_NOT_REACHED();
511     return false;
512 }
513
514 static bool executeInsertNewline(Frame* frame, Event* event, EditorCommandSource, const String&)
515 {
516     Frame* targetFrame = WebCore::targetFrame(frame, event);
517     return targetFrame->eventHandler()->handleTextInputEvent("\n", event, !targetFrame->editor()->canEditRichly());
518 }
519
520 static bool executeInsertNewlineInQuotedContent(Frame* frame, Event*, EditorCommandSource, const String&)
521 {
522     TypingCommand::insertParagraphSeparatorInQuotedContent(frame->document());
523     return true;
524 }
525
526 static bool executeInsertOrderedList(Frame* frame, Event*, EditorCommandSource, const String&)
527 {
528     applyCommand(InsertListCommand::create(frame->document(), InsertListCommand::OrderedList));
529     return true;
530 }
531
532 static bool executeInsertParagraph(Frame* frame, Event*, EditorCommandSource, const String&)
533 {
534     TypingCommand::insertParagraphSeparator(frame->document());
535     return true;
536 }
537
538 static bool executeInsertTab(Frame* frame, Event* event, EditorCommandSource, const String&)
539 {
540     return targetFrame(frame, event)->eventHandler()->handleTextInputEvent("\t", event, false, false);
541 }
542
543 static bool executeInsertText(Frame* frame, Event*, EditorCommandSource, const String& value)
544 {
545     TypingCommand::insertText(frame->document(), value);
546     return true;
547 }
548
549 static bool executeInsertUnorderedList(Frame* frame, Event*, EditorCommandSource, const String&)
550 {
551     applyCommand(InsertListCommand::create(frame->document(), InsertListCommand::UnorderedList));
552     return true;
553 }
554
555 static bool executeJustifyCenter(Frame* frame, Event*, EditorCommandSource source, const String&)
556 {
557     return executeApplyParagraphStyle(frame, source, EditActionCenter, CSSPropertyTextAlign, "center");
558 }
559
560 static bool executeJustifyFull(Frame* frame, Event*, EditorCommandSource source, const String&)
561 {
562     return executeApplyParagraphStyle(frame, source, EditActionJustify, CSSPropertyTextAlign, "justify");
563 }
564
565 static bool executeJustifyLeft(Frame* frame, Event*, EditorCommandSource source, const String&)
566 {
567     return executeApplyParagraphStyle(frame, source, EditActionAlignLeft, CSSPropertyTextAlign, "left");
568 }
569
570 static bool executeJustifyRight(Frame* frame, Event*, EditorCommandSource source, const String&)
571 {
572     return executeApplyParagraphStyle(frame, source, EditActionAlignRight, CSSPropertyTextAlign, "right");
573 }
574
575 static bool executeMakeTextWritingDirectionLeftToRight(Frame* frame, Event*, EditorCommandSource, const String&)
576 {
577     RefPtr<CSSMutableStyleDeclaration> style = CSSMutableStyleDeclaration::create();
578     style->setProperty(CSSPropertyUnicodeBidi, CSSValueEmbed);
579     style->setProperty(CSSPropertyDirection, CSSValueLtr);
580     frame->editor()->applyStyle(style.get(), EditActionSetWritingDirection);
581     return true;
582 }
583
584 static bool executeMakeTextWritingDirectionNatural(Frame* frame, Event*, EditorCommandSource, const String&)
585 {
586     RefPtr<CSSMutableStyleDeclaration> style = CSSMutableStyleDeclaration::create();
587     style->setProperty(CSSPropertyUnicodeBidi, CSSValueNormal);
588     frame->editor()->applyStyle(style.get(), EditActionSetWritingDirection);
589     return true;
590 }
591
592 static bool executeMakeTextWritingDirectionRightToLeft(Frame* frame, Event*, EditorCommandSource, const String&)
593 {
594     RefPtr<CSSMutableStyleDeclaration> style = CSSMutableStyleDeclaration::create();
595     style->setProperty(CSSPropertyUnicodeBidi, CSSValueEmbed);
596     style->setProperty(CSSPropertyDirection, CSSValueRtl);
597     frame->editor()->applyStyle(style.get(), EditActionSetWritingDirection);
598     return true;
599 }
600
601 static bool executeMoveBackward(Frame* frame, Event*, EditorCommandSource, const String&)
602 {
603     frame->selection()->modify(SelectionController::AlterationMove, SelectionController::DirectionBackward, CharacterGranularity, true);
604     return true;
605 }
606
607 static bool executeMoveBackwardAndModifySelection(Frame* frame, Event*, EditorCommandSource, const String&)
608 {
609     frame->selection()->modify(SelectionController::AlterationExtend, SelectionController::DirectionBackward, CharacterGranularity, true);
610     return true;
611 }
612
613 static bool executeMoveDown(Frame* frame, Event*, EditorCommandSource, const String&)
614 {
615     frame->selection()->modify(SelectionController::AlterationMove, SelectionController::DirectionForward, LineGranularity, true);
616     return true;
617 }
618
619 static bool executeMoveDownAndModifySelection(Frame* frame, Event*, EditorCommandSource, const String&)
620 {
621     frame->selection()->modify(SelectionController::AlterationExtend, SelectionController::DirectionForward, LineGranularity, true);
622     return true;
623 }
624
625 static bool executeMoveForward(Frame* frame, Event*, EditorCommandSource, const String&)
626 {
627     frame->selection()->modify(SelectionController::AlterationMove, SelectionController::DirectionForward, CharacterGranularity, true);
628     return true;
629 }
630
631 static bool executeMoveForwardAndModifySelection(Frame* frame, Event*, EditorCommandSource, const String&)
632 {
633     frame->selection()->modify(SelectionController::AlterationExtend, SelectionController::DirectionForward, CharacterGranularity, true);
634     return true;
635 }
636
637 static bool executeMoveLeft(Frame* frame, Event*, EditorCommandSource, const String&)
638 {
639     frame->selection()->modify(SelectionController::AlterationMove, SelectionController::DirectionLeft, CharacterGranularity, true);
640     return true;
641 }
642
643 static bool executeMoveLeftAndModifySelection(Frame* frame, Event*, EditorCommandSource, const String&)
644 {
645     frame->selection()->modify(SelectionController::AlterationExtend, SelectionController::DirectionLeft, CharacterGranularity, true);
646     return true;
647 }
648
649 static bool executeMovePageDown(Frame* frame, Event*, EditorCommandSource, const String&)
650 {
651     int distance = verticalScrollDistance(frame);
652     if (!distance)
653         return false;
654     return frame->selection()->modify(SelectionController::AlterationMove, distance, true, SelectionController::AlignCursorOnScrollAlways);
655 }
656
657 static bool executeMovePageDownAndModifySelection(Frame* frame, Event*, EditorCommandSource, const String&)
658 {
659     int distance = verticalScrollDistance(frame);
660     if (!distance)
661         return false;
662     return frame->selection()->modify(SelectionController::AlterationExtend, distance, true, SelectionController::AlignCursorOnScrollAlways);
663 }
664
665 static bool executeMovePageUp(Frame* frame, Event*, EditorCommandSource, const String&)
666 {
667     int distance = verticalScrollDistance(frame);
668     if (!distance)
669         return false;
670     return frame->selection()->modify(SelectionController::AlterationMove, -distance, true, SelectionController::AlignCursorOnScrollAlways);
671 }
672
673 static bool executeMovePageUpAndModifySelection(Frame* frame, Event*, EditorCommandSource, const String&)
674 {
675     int distance = verticalScrollDistance(frame);
676     if (!distance)
677         return false;
678     return frame->selection()->modify(SelectionController::AlterationExtend, -distance, true, SelectionController::AlignCursorOnScrollAlways);
679 }
680
681 static bool executeMoveRight(Frame* frame, Event*, EditorCommandSource, const String&)
682 {
683     frame->selection()->modify(SelectionController::AlterationMove, SelectionController::DirectionRight, CharacterGranularity, true);
684     return true;
685 }
686
687 static bool executeMoveRightAndModifySelection(Frame* frame, Event*, EditorCommandSource, const String&)
688 {
689     frame->selection()->modify(SelectionController::AlterationExtend, SelectionController::DirectionRight, CharacterGranularity, true);
690     return true;
691 }
692
693 static bool executeMoveToBeginningOfDocument(Frame* frame, Event*, EditorCommandSource, const String&)
694 {
695     frame->selection()->modify(SelectionController::AlterationMove, SelectionController::DirectionBackward, DocumentBoundary, true);
696     return true;
697 }
698
699 static bool executeMoveToBeginningOfDocumentAndModifySelection(Frame* frame, Event*, EditorCommandSource, const String&)
700 {
701     frame->selection()->modify(SelectionController::AlterationExtend, SelectionController::DirectionBackward, DocumentBoundary, true);
702     return true;
703 }
704
705 static bool executeMoveToBeginningOfLine(Frame* frame, Event*, EditorCommandSource, const String&)
706 {
707     frame->selection()->modify(SelectionController::AlterationMove, SelectionController::DirectionBackward, LineBoundary, true);
708     return true;
709 }
710
711 static bool executeMoveToBeginningOfLineAndModifySelection(Frame* frame, Event*, EditorCommandSource, const String&)
712 {
713     frame->selection()->modify(SelectionController::AlterationExtend, SelectionController::DirectionBackward, LineBoundary, true);
714     return true;
715 }
716
717 static bool executeMoveToBeginningOfParagraph(Frame* frame, Event*, EditorCommandSource, const String&)
718 {
719     frame->selection()->modify(SelectionController::AlterationMove, SelectionController::DirectionBackward, ParagraphBoundary, true);
720     return true;
721 }
722
723 static bool executeMoveToBeginningOfParagraphAndModifySelection(Frame* frame, Event*, EditorCommandSource, const String&)
724 {
725     frame->selection()->modify(SelectionController::AlterationExtend, SelectionController::DirectionBackward, ParagraphBoundary, true);
726     return true;
727 }
728
729 static bool executeMoveToBeginningOfSentence(Frame* frame, Event*, EditorCommandSource, const String&)
730 {
731     frame->selection()->modify(SelectionController::AlterationMove, SelectionController::DirectionBackward, SentenceBoundary, true);
732     return true;
733 }
734
735 static bool executeMoveToBeginningOfSentenceAndModifySelection(Frame* frame, Event*, EditorCommandSource, const String&)
736 {
737     frame->selection()->modify(SelectionController::AlterationExtend, SelectionController::DirectionBackward, SentenceBoundary, true);
738     return true;
739 }
740
741 static bool executeMoveToEndOfDocument(Frame* frame, Event*, EditorCommandSource, const String&)
742 {
743     frame->selection()->modify(SelectionController::AlterationMove, SelectionController::DirectionForward, DocumentBoundary, true);
744     return true;
745 }
746
747 static bool executeMoveToEndOfDocumentAndModifySelection(Frame* frame, Event*, EditorCommandSource, const String&)
748 {
749     frame->selection()->modify(SelectionController::AlterationExtend, SelectionController::DirectionForward, DocumentBoundary, true);
750     return true;
751 }
752
753 static bool executeMoveToEndOfSentence(Frame* frame, Event*, EditorCommandSource, const String&)
754 {
755     frame->selection()->modify(SelectionController::AlterationMove, SelectionController::DirectionForward, SentenceBoundary, true);
756     return true;
757 }
758
759 static bool executeMoveToEndOfSentenceAndModifySelection(Frame* frame, Event*, EditorCommandSource, const String&)
760 {
761     frame->selection()->modify(SelectionController::AlterationExtend, SelectionController::DirectionForward, SentenceBoundary, true);
762     return true;
763 }
764
765 static bool executeMoveToEndOfLine(Frame* frame, Event*, EditorCommandSource, const String&)
766 {
767     frame->selection()->modify(SelectionController::AlterationMove, SelectionController::DirectionForward, LineBoundary, true);
768     return true;
769 }
770
771 static bool executeMoveToEndOfLineAndModifySelection(Frame* frame, Event*, EditorCommandSource, const String&)
772 {
773     frame->selection()->modify(SelectionController::AlterationExtend, SelectionController::DirectionForward, LineBoundary, true);
774     return true;
775 }
776
777 static bool executeMoveToEndOfParagraph(Frame* frame, Event*, EditorCommandSource, const String&)
778 {
779     frame->selection()->modify(SelectionController::AlterationMove, SelectionController::DirectionForward, ParagraphBoundary, true);
780     return true;
781 }
782
783 static bool executeMoveToEndOfParagraphAndModifySelection(Frame* frame, Event*, EditorCommandSource, const String&)
784 {
785     frame->selection()->modify(SelectionController::AlterationExtend, SelectionController::DirectionForward, ParagraphBoundary, true);
786     return true;
787 }
788
789 static bool executeMoveParagraphBackwardAndModifySelection(Frame* frame, Event*, EditorCommandSource, const String&)
790 {
791     frame->selection()->modify(SelectionController::AlterationExtend, SelectionController::DirectionBackward, ParagraphGranularity, true);
792     return true;
793 }
794
795 static bool executeMoveParagraphForwardAndModifySelection(Frame* frame, Event*, EditorCommandSource, const String&)
796 {
797     frame->selection()->modify(SelectionController::AlterationExtend, SelectionController::DirectionForward, ParagraphGranularity, true);
798     return true;
799 }
800
801 static bool executeMoveUp(Frame* frame, Event*, EditorCommandSource, const String&)
802 {
803     frame->selection()->modify(SelectionController::AlterationMove, SelectionController::DirectionBackward, LineGranularity, true);
804     return true;
805 }
806
807 static bool executeMoveUpAndModifySelection(Frame* frame, Event*, EditorCommandSource, const String&)
808 {
809     frame->selection()->modify(SelectionController::AlterationExtend, SelectionController::DirectionBackward, LineGranularity, true);
810     return true;
811 }
812
813 static bool executeMoveWordBackward(Frame* frame, Event*, EditorCommandSource, const String&)
814 {
815     frame->selection()->modify(SelectionController::AlterationMove, SelectionController::DirectionBackward, WordGranularity, true);
816     return true;
817 }
818
819 static bool executeMoveWordBackwardAndModifySelection(Frame* frame, Event*, EditorCommandSource, const String&)
820 {
821     frame->selection()->modify(SelectionController::AlterationExtend, SelectionController::DirectionBackward, WordGranularity, true);
822     return true;
823 }
824
825 static bool executeMoveWordForward(Frame* frame, Event*, EditorCommandSource, const String&)
826 {
827     frame->selection()->modify(SelectionController::AlterationMove, SelectionController::DirectionForward, WordGranularity, true);
828     return true;
829 }
830
831 static bool executeMoveWordForwardAndModifySelection(Frame* frame, Event*, EditorCommandSource, const String&)
832 {
833     frame->selection()->modify(SelectionController::AlterationExtend, SelectionController::DirectionForward, WordGranularity, true);
834     return true;
835 }
836
837 static bool executeMoveWordLeft(Frame* frame, Event*, EditorCommandSource, const String&)
838 {
839     frame->selection()->modify(SelectionController::AlterationMove, SelectionController::DirectionLeft, WordGranularity, true);
840     return true;
841 }
842
843 static bool executeMoveWordLeftAndModifySelection(Frame* frame, Event*, EditorCommandSource, const String&)
844 {
845     frame->selection()->modify(SelectionController::AlterationExtend, SelectionController::DirectionLeft, WordGranularity, true);
846     return true;
847 }
848
849 static bool executeMoveWordRight(Frame* frame, Event*, EditorCommandSource, const String&)
850 {
851     frame->selection()->modify(SelectionController::AlterationMove, SelectionController::DirectionRight, WordGranularity, true);
852     return true;
853 }
854
855 static bool executeMoveWordRightAndModifySelection(Frame* frame, Event*, EditorCommandSource, const String&)
856 {
857     frame->selection()->modify(SelectionController::AlterationExtend, SelectionController::DirectionRight, WordGranularity, true);
858     return true;
859 }
860
861 static bool executeMoveToLeftEndOfLine(Frame* frame, Event*, EditorCommandSource, const String&)
862 {
863     frame->selection()->modify(SelectionController::AlterationMove, SelectionController::DirectionLeft, LineBoundary, true);
864     return true;
865 }
866
867 static bool executeMoveToLeftEndOfLineAndModifySelection(Frame* frame, Event*, EditorCommandSource, const String&)
868 {
869     frame->selection()->modify(SelectionController::AlterationExtend, SelectionController::DirectionLeft, LineBoundary, true);
870     return true;
871 }
872
873 static bool executeMoveToRightEndOfLine(Frame* frame, Event*, EditorCommandSource, const String&)
874 {
875     frame->selection()->modify(SelectionController::AlterationMove, SelectionController::DirectionRight, LineBoundary, true);
876     return true;
877 }
878
879 static bool executeMoveToRightEndOfLineAndModifySelection(Frame* frame, Event*, EditorCommandSource, const String&)
880 {
881     frame->selection()->modify(SelectionController::AlterationExtend, SelectionController::DirectionRight, LineBoundary, true);
882     return true;
883 }
884
885 static bool executeOutdent(Frame* frame, Event*, EditorCommandSource, const String&)
886 {
887     applyCommand(IndentOutdentCommand::create(frame->document(), IndentOutdentCommand::Outdent));
888     return true;
889 }
890
891 static bool executePaste(Frame* frame, Event*, EditorCommandSource, const String&)
892 {
893     frame->editor()->paste();
894     return true;
895 }
896
897 static bool executePasteAndMatchStyle(Frame* frame, Event*, EditorCommandSource, const String&)
898 {
899     frame->editor()->pasteAsPlainText();
900     return true;
901 }
902
903 static bool executePasteAsPlainText(Frame* frame, Event*, EditorCommandSource, const String&)
904 {
905     frame->editor()->pasteAsPlainText();
906     return true;
907 }
908
909 static bool executePrint(Frame* frame, Event*, EditorCommandSource, const String&)
910 {
911     Page* page = frame->page();
912     if (!page)
913         return false;
914     page->chrome()->print(frame);
915     return true;
916 }
917
918 static bool executeRedo(Frame* frame, Event*, EditorCommandSource, const String&)
919 {
920     frame->editor()->redo();
921     return true;
922 }
923
924 static bool executeRemoveFormat(Frame* frame, Event*, EditorCommandSource, const String&)
925 {
926     frame->editor()->removeFormattingAndStyle();
927     return true;
928 }
929
930 static bool executeSelectAll(Frame* frame, Event*, EditorCommandSource, const String&)
931 {
932     frame->selection()->selectAll();
933     return true;
934 }
935
936 static bool executeSelectLine(Frame* frame, Event*, EditorCommandSource, const String&)
937 {
938     return expandSelectionToGranularity(frame, LineGranularity);
939 }
940
941 static bool executeSelectParagraph(Frame* frame, Event*, EditorCommandSource, const String&)
942 {
943     return expandSelectionToGranularity(frame, ParagraphGranularity);
944 }
945
946 static bool executeSelectSentence(Frame* frame, Event*, EditorCommandSource, const String&)
947 {
948     return expandSelectionToGranularity(frame, SentenceGranularity);
949 }
950
951 static bool executeSelectToMark(Frame* frame, Event*, EditorCommandSource, const String&)
952 {
953     RefPtr<Range> mark = frame->editor()->mark().toNormalizedRange();
954     RefPtr<Range> selection = frame->editor()->selectedRange();
955     if (!mark || !selection) {
956         systemBeep();
957         return false;
958     }
959     frame->selection()->setSelectedRange(unionDOMRanges(mark.get(), selection.get()).get(), DOWNSTREAM, true);
960     return true;
961 }
962
963 static bool executeSelectWord(Frame* frame, Event*, EditorCommandSource, const String&)
964 {
965     return expandSelectionToGranularity(frame, WordGranularity);
966 }
967
968 static bool executeSetMark(Frame* frame, Event*, EditorCommandSource, const String&)
969 {
970     frame->editor()->setMark(frame->selection()->selection());
971     return true;
972 }
973
974 static bool executeStrikethrough(Frame* frame, Event*, EditorCommandSource source, const String&)
975 {
976     RefPtr<CSSPrimitiveValue> lineThrough = CSSPrimitiveValue::createIdentifier(CSSValueLineThrough);
977     return executeToggleStyleInList(frame, source, EditActionUnderline, CSSPropertyWebkitTextDecorationsInEffect, lineThrough.get());
978 }
979
980 static bool executeStyleWithCSS(Frame* frame, Event*, EditorCommandSource, const String& value)
981 {
982     if (value != "false" && value != "true")
983         return false;
984     
985     frame->editor()->setShouldStyleWithCSS(value == "true" ? true : false);
986     return true;
987 }
988
989 static bool executeSubscript(Frame* frame, Event*, EditorCommandSource source, const String&)
990 {
991     return executeToggleStyle(frame, source, EditActionSubscript, CSSPropertyVerticalAlign, "baseline", "sub");
992 }
993
994 static bool executeSuperscript(Frame* frame, Event*, EditorCommandSource source, const String&)
995 {
996     return executeToggleStyle(frame, source, EditActionSuperscript, CSSPropertyVerticalAlign, "baseline", "super");
997 }
998
999 static bool executeSwapWithMark(Frame* frame, Event*, EditorCommandSource, const String&)
1000 {
1001     const VisibleSelection& mark = frame->editor()->mark();
1002     const VisibleSelection& selection = frame->selection()->selection();
1003     if (mark.isNone() || selection.isNone()) {
1004         systemBeep();
1005         return false;
1006     }
1007     frame->selection()->setSelection(mark);
1008     frame->editor()->setMark(selection);
1009     return true;
1010 }
1011
1012 static bool executeToggleBold(Frame* frame, Event*, EditorCommandSource source, const String&)
1013 {
1014     return executeToggleStyle(frame, source, EditActionChangeAttributes, CSSPropertyFontWeight, "normal", "bold");
1015 }
1016
1017 static bool executeToggleItalic(Frame* frame, Event*, EditorCommandSource source, const String&)
1018 {
1019     return executeToggleStyle(frame, source, EditActionChangeAttributes, CSSPropertyFontStyle, "normal", "italic");
1020 }
1021
1022 static bool executeTranspose(Frame* frame, Event*, EditorCommandSource, const String&)
1023 {
1024     frame->editor()->transpose();
1025     return true;
1026 }
1027
1028 static bool executeUnderline(Frame* frame, Event*, EditorCommandSource source, const String&)
1029 {
1030     RefPtr<CSSPrimitiveValue> underline = CSSPrimitiveValue::createIdentifier(CSSValueUnderline);
1031     return executeToggleStyleInList(frame, source, EditActionUnderline, CSSPropertyWebkitTextDecorationsInEffect, underline.get());
1032 }
1033
1034 static bool executeUndo(Frame* frame, Event*, EditorCommandSource, const String&)
1035 {
1036     frame->editor()->undo();
1037     return true;
1038 }
1039
1040 static bool executeUnlink(Frame* frame, Event*, EditorCommandSource, const String&)
1041 {
1042     applyCommand(UnlinkCommand::create(frame->document()));
1043     return true;
1044 }
1045
1046 static bool executeUnscript(Frame* frame, Event*, EditorCommandSource source, const String&)
1047 {
1048     return executeApplyStyle(frame, source, EditActionUnscript, CSSPropertyVerticalAlign, "baseline");
1049 }
1050
1051 static bool executeUnselect(Frame* frame, Event*, EditorCommandSource, const String&)
1052 {
1053     frame->selection()->clear();
1054     return true;
1055 }
1056
1057 static bool executeYank(Frame* frame, Event*, EditorCommandSource, const String&)
1058 {
1059     frame->editor()->insertTextWithoutSendingTextEvent(frame->editor()->killRing()->yank(), false, 0);
1060     frame->editor()->killRing()->setToYankedState();
1061     return true;
1062 }
1063
1064 static bool executeYankAndSelect(Frame* frame, Event*, EditorCommandSource, const String&)
1065 {
1066     frame->editor()->insertTextWithoutSendingTextEvent(frame->editor()->killRing()->yank(), true, 0);
1067     frame->editor()->killRing()->setToYankedState();
1068     return true;
1069 }
1070
1071 #if PLATFORM(MAC) && !defined(BUILDING_ON_TIGER) && !defined(BUILDING_ON_LEOPARD) && !defined(BUILDING_ON_SNOW_LEOPARD)
1072 static bool executeCancelOperation(Frame* frame, Event*, EditorCommandSource, const String&)
1073 {
1074     frame->editor()->handleCancelOperation();
1075     return true;
1076 }
1077 #endif
1078
1079 // Supported functions
1080
1081 static bool supported(Frame*, EditorCommandSource)
1082 {
1083     return true;
1084 }
1085
1086 static bool supportedFromMenuOrKeyBinding(Frame*, EditorCommandSource source)
1087 {
1088     return source == CommandFromMenuOrKeyBinding;
1089 }
1090
1091 static bool supportedCopyCut(Frame* frame, EditorCommandSource source)
1092 {
1093     switch (source) {
1094     case CommandFromMenuOrKeyBinding:
1095         return true;
1096     case CommandFromDOM:
1097     case CommandFromDOMWithUserInterface: {
1098         Settings* settings = frame ? frame->settings() : 0;
1099         return settings && settings->javaScriptCanAccessClipboard();
1100     }
1101     }
1102     ASSERT_NOT_REACHED();
1103     return false;
1104 }
1105
1106 static bool supportedPaste(Frame* frame, EditorCommandSource source)
1107 {
1108     switch (source) {
1109     case CommandFromMenuOrKeyBinding:
1110         return true;
1111     case CommandFromDOM:
1112     case CommandFromDOMWithUserInterface: {
1113         Settings* settings = frame ? frame->settings() : 0;
1114         return settings && (settings->javaScriptCanAccessClipboard() ? settings->isDOMPasteAllowed() : 0);
1115     }
1116     }
1117     ASSERT_NOT_REACHED();
1118     return false;
1119 }
1120
1121 #if PLATFORM(MAC) && !defined(BUILDING_ON_TIGER) && !defined(BUILDING_ON_LEOPARD) && !defined(BUILDING_ON_SNOW_LEOPARD)
1122 static bool supportedDismissCorrectionPanel(Frame* frame, EditorCommandSource source)
1123 {
1124     return supportedFromMenuOrKeyBinding(frame, source) && frame->editor()->isShowingCorrectionPanel();
1125 }
1126 #endif
1127
1128 // Enabled functions
1129
1130 static bool enabled(Frame*, Event*, EditorCommandSource)
1131 {
1132     return true;
1133 }
1134
1135 static bool enabledVisibleSelection(Frame* frame, Event* event, EditorCommandSource)
1136 {
1137     // The term "visible" here includes a caret in editable text or a range in any text.
1138     const VisibleSelection& selection = frame->editor()->selectionForCommand(event);
1139     return (selection.isCaret() && selection.isContentEditable()) || selection.isRange();
1140 }
1141
1142 static bool caretBrowsingEnabled(Frame* frame)
1143 {
1144     return frame->settings() && frame->settings()->caretBrowsingEnabled();
1145 }
1146
1147 static EditorCommandSource dummyEditorCommandSource = static_cast<EditorCommandSource>(0);
1148
1149 static bool enabledVisibleSelectionOrCaretBrowsing(Frame* frame, Event* event, EditorCommandSource)
1150 {
1151     // The EditorCommandSource parameter is unused in enabledVisibleSelection, so just pass a dummy variable
1152     return caretBrowsingEnabled(frame) || enabledVisibleSelection(frame, event, dummyEditorCommandSource);
1153 }
1154
1155 static bool enabledVisibleSelectionAndMark(Frame* frame, Event* event, EditorCommandSource)
1156 {
1157     const VisibleSelection& selection = frame->editor()->selectionForCommand(event);
1158     return ((selection.isCaret() && selection.isContentEditable()) || selection.isRange())
1159         && frame->editor()->mark().isCaretOrRange();
1160 }
1161
1162 static bool enableCaretInEditableText(Frame* frame, Event* event, EditorCommandSource)
1163 {
1164     const VisibleSelection& selection = frame->editor()->selectionForCommand(event);
1165     return selection.isCaret() && selection.isContentEditable();
1166 }
1167
1168 static bool enabledCopy(Frame* frame, Event*, EditorCommandSource)
1169 {
1170     return frame->editor()->canDHTMLCopy() || frame->editor()->canCopy();
1171 }
1172
1173 static bool enabledCut(Frame* frame, Event*, EditorCommandSource)
1174 {
1175     return frame->editor()->canDHTMLCut() || frame->editor()->canCut();
1176 }
1177
1178 static bool enabledDelete(Frame* frame, Event* event, EditorCommandSource source)
1179 {
1180     switch (source) {
1181     case CommandFromMenuOrKeyBinding:
1182         // "Delete" from menu only affects selected range, just like Cut but without affecting pasteboard
1183         return frame->editor()->canDHTMLCut() || frame->editor()->canCut();
1184     case CommandFromDOM:
1185     case CommandFromDOMWithUserInterface:
1186         // "Delete" from DOM is like delete/backspace keypress, affects selected range if non-empty,
1187         // otherwise removes a character
1188         return frame->editor()->selectionForCommand(event).isContentEditable();
1189     }
1190     ASSERT_NOT_REACHED();
1191     return false;
1192 }
1193
1194 static bool enabledInEditableText(Frame* frame, Event* event, EditorCommandSource)
1195 {
1196     return frame->editor()->selectionForCommand(event).isContentEditable();
1197 }
1198
1199 static bool enabledInEditableTextOrCaretBrowsing(Frame* frame, Event* event, EditorCommandSource)
1200 {
1201     // The EditorCommandSource parameter is unused in enabledInEditableText, so just pass a dummy variable
1202     return caretBrowsingEnabled(frame) || enabledInEditableText(frame, event, dummyEditorCommandSource);
1203 }
1204
1205 static bool enabledInRichlyEditableText(Frame* frame, Event*, EditorCommandSource)
1206 {
1207     return frame->selection()->isCaretOrRange() && frame->selection()->isContentRichlyEditable();
1208 }
1209
1210 static bool enabledPaste(Frame* frame, Event*, EditorCommandSource)
1211 {
1212     return frame->editor()->canPaste();
1213 }
1214
1215 static bool enabledRangeInEditableText(Frame* frame, Event*, EditorCommandSource)
1216 {
1217     return frame->selection()->isRange() && frame->selection()->isContentEditable();
1218 }
1219
1220 static bool enabledRangeInRichlyEditableText(Frame* frame, Event*, EditorCommandSource)
1221 {
1222     return frame->selection()->isRange() && frame->selection()->isContentRichlyEditable();
1223 }
1224
1225 static bool enabledRedo(Frame* frame, Event*, EditorCommandSource)
1226 {
1227     return frame->editor()->canRedo();
1228 }
1229
1230 static bool enabledUndo(Frame* frame, Event*, EditorCommandSource)
1231 {
1232     return frame->editor()->canUndo();
1233 }
1234
1235 // State functions
1236
1237 static TriState stateNone(Frame*, Event*)
1238 {
1239     return FalseTriState;
1240 }
1241
1242 static TriState stateBold(Frame* frame, Event*)
1243 {
1244     return stateStyle(frame, CSSPropertyFontWeight, "bold");
1245 }
1246
1247 static TriState stateItalic(Frame* frame, Event*)
1248 {
1249     return stateStyle(frame, CSSPropertyFontStyle, "italic");
1250 }
1251
1252 static TriState stateOrderedList(Frame* frame, Event*)
1253 {
1254     return frame->editor()->selectionOrderedListState();
1255 }
1256
1257 static TriState stateStrikethrough(Frame* frame, Event*)
1258 {
1259     return stateStyle(frame, CSSPropertyWebkitTextDecorationsInEffect, "line-through");
1260 }
1261
1262 static TriState stateStyleWithCSS(Frame* frame, Event*)
1263 {
1264     return frame->editor()->shouldStyleWithCSS() ? TrueTriState : FalseTriState;
1265 }
1266
1267 static TriState stateSubscript(Frame* frame, Event*)
1268 {
1269     return stateStyle(frame, CSSPropertyVerticalAlign, "sub");
1270 }
1271
1272 static TriState stateSuperscript(Frame* frame, Event*)
1273 {
1274     return stateStyle(frame, CSSPropertyVerticalAlign, "super");
1275 }
1276
1277 static TriState stateTextWritingDirectionLeftToRight(Frame* frame, Event*)
1278 {
1279     return stateTextWritingDirection(frame, LeftToRightWritingDirection);
1280 }
1281
1282 static TriState stateTextWritingDirectionNatural(Frame* frame, Event*)
1283 {
1284     return stateTextWritingDirection(frame, NaturalWritingDirection);
1285 }
1286
1287 static TriState stateTextWritingDirectionRightToLeft(Frame* frame, Event*)
1288 {
1289     return stateTextWritingDirection(frame, RightToLeftWritingDirection);
1290 }
1291
1292 static TriState stateUnderline(Frame* frame, Event*)
1293 {
1294     return stateStyle(frame, CSSPropertyWebkitTextDecorationsInEffect, "underline");
1295 }
1296
1297 static TriState stateUnorderedList(Frame* frame, Event*)
1298 {
1299     return frame->editor()->selectionUnorderedListState();
1300 }
1301
1302 static TriState stateJustifyCenter(Frame* frame, Event*)
1303 {
1304     return stateStyle(frame, CSSPropertyTextAlign, "center");
1305 }
1306
1307 static TriState stateJustifyFull(Frame* frame, Event*)
1308 {
1309     return stateStyle(frame, CSSPropertyTextAlign, "justify");
1310 }
1311
1312 static TriState stateJustifyLeft(Frame* frame, Event*)
1313 {
1314     return stateStyle(frame, CSSPropertyTextAlign, "left");
1315 }
1316
1317 static TriState stateJustifyRight(Frame* frame, Event*)
1318 {
1319     return stateStyle(frame, CSSPropertyTextAlign, "right");
1320 }
1321
1322 // Value functions
1323
1324 static String valueNull(Frame*, Event*)
1325 {
1326     return String();
1327 }
1328
1329 static String valueBackColor(Frame* frame, Event*)
1330 {
1331     return valueStyle(frame, CSSPropertyBackgroundColor);
1332 }
1333
1334 static String valueFontName(Frame* frame, Event*)
1335 {
1336     return valueStyle(frame, CSSPropertyFontFamily);
1337 }
1338
1339 static String valueFontSize(Frame* frame, Event*)
1340 {
1341     return valueStyle(frame, CSSPropertyFontSize);
1342 }
1343
1344 static String valueFontSizeDelta(Frame* frame, Event*)
1345 {
1346     return valueStyle(frame, CSSPropertyWebkitFontSizeDelta);
1347 }
1348
1349 static String valueForeColor(Frame* frame, Event*)
1350 {
1351     return valueStyle(frame, CSSPropertyColor);
1352 }
1353
1354 static String valueFormatBlock(Frame* frame, Event*)
1355 {
1356     Element* formatBlockElement = frame->editor()->elementForFormatBlockCommand();
1357     if (!formatBlockElement)
1358         return "";
1359     return formatBlockElement->localName();
1360 }
1361
1362 // Map of functions
1363
1364 struct CommandEntry {
1365     const char* name;
1366     EditorInternalCommand command;
1367 };
1368
1369 static const CommandMap& createCommandMap()
1370 {
1371     static const CommandEntry commands[] = {
1372         { "AlignCenter", { executeJustifyCenter, supportedFromMenuOrKeyBinding, enabledInRichlyEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1373         { "AlignJustified", { executeJustifyFull, supportedFromMenuOrKeyBinding, enabledInRichlyEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1374         { "AlignLeft", { executeJustifyLeft, supportedFromMenuOrKeyBinding, enabledInRichlyEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1375         { "AlignRight", { executeJustifyRight, supportedFromMenuOrKeyBinding, enabledInRichlyEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1376         { "BackColor", { executeBackColor, supported, enabledInRichlyEditableText, stateNone, valueBackColor, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1377         { "BackwardDelete", { executeDeleteBackward, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } }, // FIXME: remove BackwardDelete when Safari for Windows stops using it.
1378         { "Bold", { executeToggleBold, supported, enabledInRichlyEditableText, stateBold, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1379         { "Copy", { executeCopy, supportedCopyCut, enabledCopy, stateNone, valueNull, notTextInsertion, allowExecutionWhenDisabled } },
1380         { "CreateLink", { executeCreateLink, supported, enabledInRichlyEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1381         { "Cut", { executeCut, supportedCopyCut, enabledCut, stateNone, valueNull, notTextInsertion, allowExecutionWhenDisabled } },
1382         { "Delete", { executeDelete, supported, enabledDelete, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1383         { "DeleteBackward", { executeDeleteBackward, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1384         { "DeleteBackwardByDecomposingPreviousCharacter", { executeDeleteBackwardByDecomposingPreviousCharacter, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1385         { "DeleteForward", { executeDeleteForward, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1386         { "DeleteToBeginningOfLine", { executeDeleteToBeginningOfLine, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1387         { "DeleteToBeginningOfParagraph", { executeDeleteToBeginningOfParagraph, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1388         { "DeleteToEndOfLine", { executeDeleteToEndOfLine, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1389         { "DeleteToEndOfParagraph", { executeDeleteToEndOfParagraph, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1390         { "DeleteToMark", { executeDeleteToMark, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1391         { "DeleteWordBackward", { executeDeleteWordBackward, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1392         { "DeleteWordForward", { executeDeleteWordForward, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1393         { "FindString", { executeFindString, supported, enabled, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1394         { "FontName", { executeFontName, supported, enabledInEditableText, stateNone, valueFontName, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1395         { "FontSize", { executeFontSize, supported, enabledInEditableText, stateNone, valueFontSize, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1396         { "FontSizeDelta", { executeFontSizeDelta, supported, enabledInEditableText, stateNone, valueFontSizeDelta, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1397         { "ForeColor", { executeForeColor, supported, enabledInRichlyEditableText, stateNone, valueForeColor, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1398         { "FormatBlock", { executeFormatBlock, supported, enabledInRichlyEditableText, stateNone, valueFormatBlock, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1399         { "ForwardDelete", { executeForwardDelete, supported, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1400         { "HiliteColor", { executeBackColor, supported, enabledInRichlyEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1401         { "IgnoreSpelling", { executeIgnoreSpelling, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1402         { "Indent", { executeIndent, supported, enabledInRichlyEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1403         { "InsertBacktab", { executeInsertBacktab, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, isTextInsertion, doNotAllowExecutionWhenDisabled } },
1404         { "InsertHTML", { executeInsertHTML, supported, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1405         { "InsertHorizontalRule", { executeInsertHorizontalRule, supported, enabledInRichlyEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1406         { "InsertImage", { executeInsertImage, supported, enabledInRichlyEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1407         { "InsertLineBreak", { executeInsertLineBreak, supported, enabledInEditableText, stateNone, valueNull, isTextInsertion, doNotAllowExecutionWhenDisabled } },
1408         { "InsertNewline", { executeInsertNewline, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, isTextInsertion, doNotAllowExecutionWhenDisabled } },    
1409         { "InsertNewlineInQuotedContent", { executeInsertNewlineInQuotedContent, supported, enabledInRichlyEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1410         { "InsertOrderedList", { executeInsertOrderedList, supported, enabledInRichlyEditableText, stateOrderedList, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1411         { "InsertParagraph", { executeInsertParagraph, supported, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1412         { "InsertTab", { executeInsertTab, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, isTextInsertion, doNotAllowExecutionWhenDisabled } },
1413         { "InsertText", { executeInsertText, supported, enabledInEditableText, stateNone, valueNull, isTextInsertion, doNotAllowExecutionWhenDisabled } },
1414         { "InsertUnorderedList", { executeInsertUnorderedList, supported, enabledInRichlyEditableText, stateUnorderedList, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1415         { "Italic", { executeToggleItalic, supported, enabledInRichlyEditableText, stateItalic, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1416         { "JustifyCenter", { executeJustifyCenter, supported, enabledInRichlyEditableText, stateJustifyCenter, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1417         { "JustifyFull", { executeJustifyFull, supported, enabledInRichlyEditableText, stateJustifyFull, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1418         { "JustifyLeft", { executeJustifyLeft, supported, enabledInRichlyEditableText, stateJustifyLeft, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1419         { "JustifyNone", { executeJustifyLeft, supported, enabledInRichlyEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1420         { "JustifyRight", { executeJustifyRight, supported, enabledInRichlyEditableText, stateJustifyRight, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1421         { "MakeTextWritingDirectionLeftToRight", { executeMakeTextWritingDirectionLeftToRight, supportedFromMenuOrKeyBinding, enabledInRichlyEditableText, stateTextWritingDirectionLeftToRight, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1422         { "MakeTextWritingDirectionNatural", { executeMakeTextWritingDirectionNatural, supportedFromMenuOrKeyBinding, enabledInRichlyEditableText, stateTextWritingDirectionNatural, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1423         { "MakeTextWritingDirectionRightToLeft", { executeMakeTextWritingDirectionRightToLeft, supportedFromMenuOrKeyBinding, enabledInRichlyEditableText, stateTextWritingDirectionRightToLeft, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1424         { "MoveBackward", { executeMoveBackward, supportedFromMenuOrKeyBinding, enabledInEditableTextOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1425         { "MoveBackwardAndModifySelection", { executeMoveBackwardAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelectionOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1426         { "MoveDown", { executeMoveDown, supportedFromMenuOrKeyBinding, enabledInEditableTextOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1427         { "MoveDownAndModifySelection", { executeMoveDownAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelectionOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1428         { "MoveForward", { executeMoveForward, supportedFromMenuOrKeyBinding, enabledInEditableTextOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1429         { "MoveForwardAndModifySelection", { executeMoveForwardAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelectionOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1430         { "MoveLeft", { executeMoveLeft, supportedFromMenuOrKeyBinding, enabledInEditableTextOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1431         { "MoveLeftAndModifySelection", { executeMoveLeftAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelectionOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1432         { "MovePageDown", { executeMovePageDown, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1433         { "MovePageDownAndModifySelection", { executeMovePageDownAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelection, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1434         { "MovePageUp", { executeMovePageUp, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1435         { "MovePageUpAndModifySelection", { executeMovePageUpAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelection, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1436         { "MoveParagraphBackwardAndModifySelection", { executeMoveParagraphBackwardAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelectionOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1437         { "MoveParagraphForwardAndModifySelection", { executeMoveParagraphForwardAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelectionOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1438         { "MoveRight", { executeMoveRight, supportedFromMenuOrKeyBinding, enabledInEditableTextOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1439         { "MoveRightAndModifySelection", { executeMoveRightAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelectionOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1440         { "MoveToBeginningOfDocument", { executeMoveToBeginningOfDocument, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1441         { "MoveToBeginningOfDocumentAndModifySelection", { executeMoveToBeginningOfDocumentAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelection, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1442         { "MoveToBeginningOfLine", { executeMoveToBeginningOfLine, supportedFromMenuOrKeyBinding, enabledInEditableTextOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1443         { "MoveToBeginningOfLineAndModifySelection", { executeMoveToBeginningOfLineAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelectionOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1444         { "MoveToBeginningOfParagraph", { executeMoveToBeginningOfParagraph, supportedFromMenuOrKeyBinding, enabledInEditableTextOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1445         { "MoveToBeginningOfParagraphAndModifySelection", { executeMoveToBeginningOfParagraphAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelectionOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1446         { "MoveToBeginningOfSentence", { executeMoveToBeginningOfSentence, supportedFromMenuOrKeyBinding, enabledInEditableTextOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1447         { "MoveToBeginningOfSentenceAndModifySelection", { executeMoveToBeginningOfSentenceAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelectionOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1448         { "MoveToEndOfDocument", { executeMoveToEndOfDocument, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1449         { "MoveToEndOfDocumentAndModifySelection", { executeMoveToEndOfDocumentAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelection, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1450         { "MoveToEndOfLine", { executeMoveToEndOfLine, supportedFromMenuOrKeyBinding, enabledInEditableTextOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1451         { "MoveToEndOfLineAndModifySelection", { executeMoveToEndOfLineAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelectionOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1452         { "MoveToEndOfParagraph", { executeMoveToEndOfParagraph, supportedFromMenuOrKeyBinding, enabledInEditableTextOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1453         { "MoveToEndOfParagraphAndModifySelection", { executeMoveToEndOfParagraphAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelectionOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1454         { "MoveToEndOfSentence", { executeMoveToEndOfSentence, supportedFromMenuOrKeyBinding, enabledInEditableTextOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1455         { "MoveToEndOfSentenceAndModifySelection", { executeMoveToEndOfSentenceAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelectionOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1456         { "MoveToLeftEndOfLine", { executeMoveToLeftEndOfLine, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1457         { "MoveToLeftEndOfLineAndModifySelection", { executeMoveToLeftEndOfLineAndModifySelection, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1458         { "MoveToRightEndOfLine", { executeMoveToRightEndOfLine, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1459         { "MoveToRightEndOfLineAndModifySelection", { executeMoveToRightEndOfLineAndModifySelection, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1460         { "MoveUp", { executeMoveUp, supportedFromMenuOrKeyBinding, enabledInEditableTextOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1461         { "MoveUpAndModifySelection", { executeMoveUpAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelectionOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1462         { "MoveWordBackward", { executeMoveWordBackward, supportedFromMenuOrKeyBinding, enabledInEditableTextOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1463         { "MoveWordBackwardAndModifySelection", { executeMoveWordBackwardAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelectionOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1464         { "MoveWordForward", { executeMoveWordForward, supportedFromMenuOrKeyBinding, enabledInEditableTextOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1465         { "MoveWordForwardAndModifySelection", { executeMoveWordForwardAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelectionOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1466         { "MoveWordLeft", { executeMoveWordLeft, supportedFromMenuOrKeyBinding, enabledInEditableTextOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1467         { "MoveWordLeftAndModifySelection", { executeMoveWordLeftAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelectionOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1468         { "MoveWordRight", { executeMoveWordRight, supportedFromMenuOrKeyBinding, enabledInEditableTextOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1469         { "MoveWordRightAndModifySelection", { executeMoveWordRightAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelectionOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1470         { "Outdent", { executeOutdent, supported, enabledInRichlyEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1471         { "Paste", { executePaste, supportedPaste, enabledPaste, stateNone, valueNull, notTextInsertion, allowExecutionWhenDisabled } },
1472         { "PasteAndMatchStyle", { executePasteAndMatchStyle, supportedPaste, enabledPaste, stateNone, valueNull, notTextInsertion, allowExecutionWhenDisabled } },
1473         { "PasteAsPlainText", { executePasteAsPlainText, supportedPaste, enabledPaste, stateNone, valueNull, notTextInsertion, allowExecutionWhenDisabled } },
1474         { "Print", { executePrint, supported, enabled, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1475         { "Redo", { executeRedo, supported, enabledRedo, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1476         { "RemoveFormat", { executeRemoveFormat, supported, enabledRangeInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1477         { "SelectAll", { executeSelectAll, supported, enabled, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1478         { "SelectLine", { executeSelectLine, supportedFromMenuOrKeyBinding, enabledVisibleSelection, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1479         { "SelectParagraph", { executeSelectParagraph, supportedFromMenuOrKeyBinding, enabledVisibleSelection, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1480         { "SelectSentence", { executeSelectSentence, supportedFromMenuOrKeyBinding, enabledVisibleSelection, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1481         { "SelectToMark", { executeSelectToMark, supportedFromMenuOrKeyBinding, enabledVisibleSelectionAndMark, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1482         { "SelectWord", { executeSelectWord, supportedFromMenuOrKeyBinding, enabledVisibleSelection, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1483         { "SetMark", { executeSetMark, supportedFromMenuOrKeyBinding, enabledVisibleSelection, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1484         { "Strikethrough", { executeStrikethrough, supported, enabledInRichlyEditableText, stateStrikethrough, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1485         { "StyleWithCSS", { executeStyleWithCSS, supported, enabledInRichlyEditableText, stateStyleWithCSS, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1486         { "Subscript", { executeSubscript, supported, enabledInRichlyEditableText, stateSubscript, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1487         { "Superscript", { executeSuperscript, supported, enabledInRichlyEditableText, stateSuperscript, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1488         { "SwapWithMark", { executeSwapWithMark, supportedFromMenuOrKeyBinding, enabledVisibleSelectionAndMark, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1489         { "ToggleBold", { executeToggleBold, supportedFromMenuOrKeyBinding, enabledInRichlyEditableText, stateBold, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1490         { "ToggleItalic", { executeToggleItalic, supportedFromMenuOrKeyBinding, enabledInRichlyEditableText, stateItalic, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1491         { "ToggleUnderline", { executeUnderline, supportedFromMenuOrKeyBinding, enabledInRichlyEditableText, stateUnderline, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1492         { "Transpose", { executeTranspose, supported, enableCaretInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1493         { "Underline", { executeUnderline, supported, enabledInRichlyEditableText, stateUnderline, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1494         { "Undo", { executeUndo, supported, enabledUndo, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1495         { "Unlink", { executeUnlink, supported, enabledRangeInRichlyEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1496         { "Unscript", { executeUnscript, supportedFromMenuOrKeyBinding, enabledInRichlyEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1497         { "Unselect", { executeUnselect, supported, enabledVisibleSelection, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1498         { "Yank", { executeYank, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1499         { "YankAndSelect", { executeYankAndSelect, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1500 #if PLATFORM(MAC) && !defined(BUILDING_ON_TIGER) && !defined(BUILDING_ON_LEOPARD) && !defined(BUILDING_ON_SNOW_LEOPARD)
1501         { "CancelOperation", { executeCancelOperation, supportedDismissCorrectionPanel, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1502 #endif
1503     };
1504
1505     // These unsupported commands are listed here since they appear in the Microsoft
1506     // documentation used as the starting point for our DOM executeCommand support.
1507     //
1508     // 2D-Position (not supported)
1509     // AbsolutePosition (not supported)
1510     // BlockDirLTR (not supported)
1511     // BlockDirRTL (not supported)
1512     // BrowseMode (not supported)
1513     // ClearAuthenticationCache (not supported)
1514     // CreateBookmark (not supported)
1515     // DirLTR (not supported)
1516     // DirRTL (not supported)
1517     // EditMode (not supported)
1518     // InlineDirLTR (not supported)
1519     // InlineDirRTL (not supported)
1520     // InsertButton (not supported)
1521     // InsertFieldSet (not supported)
1522     // InsertIFrame (not supported)
1523     // InsertInputButton (not supported)
1524     // InsertInputCheckbox (not supported)
1525     // InsertInputFileUpload (not supported)
1526     // InsertInputHidden (not supported)
1527     // InsertInputImage (not supported)
1528     // InsertInputPassword (not supported)
1529     // InsertInputRadio (not supported)
1530     // InsertInputReset (not supported)
1531     // InsertInputSubmit (not supported)
1532     // InsertInputText (not supported)
1533     // InsertMarquee (not supported)
1534     // InsertSelectDropDown (not supported)
1535     // InsertSelectListBox (not supported)
1536     // InsertTextArea (not supported)
1537     // LiveResize (not supported)
1538     // MultipleSelection (not supported)
1539     // Open (not supported)
1540     // Overwrite (not supported)
1541     // PlayImage (not supported)
1542     // Refresh (not supported)
1543     // RemoveParaFormat (not supported)
1544     // SaveAs (not supported)
1545     // SizeToControl (not supported)
1546     // SizeToControlHeight (not supported)
1547     // SizeToControlWidth (not supported)
1548     // Stop (not supported)
1549     // StopImage (not supported)
1550     // Unbookmark (not supported)
1551
1552     CommandMap& commandMap = *new CommandMap;
1553
1554     const unsigned numCommands = sizeof(commands) / sizeof(commands[0]);
1555     for (unsigned i = 0; i < numCommands; i++) {
1556         ASSERT(!commandMap.get(commands[i].name));
1557         commandMap.set(commands[i].name, &commands[i].command);
1558     }
1559
1560     return commandMap;
1561 }
1562
1563 Editor::Command Editor::command(const String& commandName)
1564 {
1565     return command(commandName, CommandFromMenuOrKeyBinding);
1566 }
1567
1568 Editor::Command Editor::command(const String& commandName, EditorCommandSource source)
1569 {
1570     if (commandName.isEmpty())
1571         return Command();
1572
1573     static const CommandMap& commandMap = createCommandMap();
1574     const EditorInternalCommand* internalCommand = commandMap.get(commandName);
1575     return internalCommand ? Command(m_frame, internalCommand, source) : Command();
1576 }
1577
1578 Editor::Command::Command()
1579     : m_command(0)
1580     , m_source()
1581 {
1582 }
1583
1584 Editor::Command::Command(PassRefPtr<Frame> frame, const EditorInternalCommand* command, EditorCommandSource source)
1585     : m_frame(frame)
1586     , m_command(command)
1587     , m_source(source)
1588 {
1589     ASSERT(m_frame);
1590     ASSERT(m_command);
1591 }
1592
1593 bool Editor::Command::execute(const String& parameter, Event* triggeringEvent) const
1594 {
1595     if (!isEnabled(triggeringEvent)) {
1596         // Let certain commands be executed when performed explicitly even if they are disabled.
1597         if (!isSupported() || !m_frame || !m_command->allowExecutionWhenDisabled)
1598             return false;
1599     }
1600     m_frame->document()->updateLayoutIgnorePendingStylesheets();
1601     return m_command->execute(m_frame.get(), triggeringEvent, m_source, parameter);
1602 }
1603
1604 bool Editor::Command::execute(Event* triggeringEvent) const
1605 {
1606     return execute(String(), triggeringEvent);
1607 }
1608
1609 bool Editor::Command::isSupported() const
1610 {
1611     return m_command && m_command->isSupported(m_frame.get(), m_source);
1612 }
1613
1614 bool Editor::Command::isEnabled(Event* triggeringEvent) const
1615 {
1616     if (!isSupported() || !m_frame)
1617         return false;
1618     return m_command->isEnabled(m_frame.get(), triggeringEvent, m_source);
1619 }
1620
1621 TriState Editor::Command::state(Event* triggeringEvent) const
1622 {
1623     if (!isSupported() || !m_frame)
1624         return FalseTriState;
1625     return m_command->state(m_frame.get(), triggeringEvent);
1626 }
1627
1628 String Editor::Command::value(Event* triggeringEvent) const
1629 {
1630     if (!isSupported() || !m_frame)
1631         return String();
1632     return m_command->value(m_frame.get(), triggeringEvent);
1633 }
1634
1635 bool Editor::Command::isTextInsertion() const
1636 {
1637     return m_command && m_command->isTextInsertion;
1638 }
1639
1640 } // namespace WebCore