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