2010-12-09 Maciej Stachowiak <mjs@apple.com>
[WebKit.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
436     ExceptionCode ec;
437     String localName, prefix;
438     if (!Document::parseQualifiedName(tagName, prefix, localName, ec))
439         return false;
440     QualifiedName qualifiedTagName(prefix, localName, xhtmlNamespaceURI);
441
442     RefPtr<FormatBlockCommand> command = FormatBlockCommand::create(frame->document(), qualifiedTagName);
443     applyCommand(command);
444     return command->didApply();
445 }
446
447 static bool executeForwardDelete(Frame* frame, Event*, EditorCommandSource source, const String&)
448 {
449     switch (source) {
450     case CommandFromMenuOrKeyBinding:
451         frame->editor()->deleteWithDirection(SelectionController::DirectionForward, CharacterGranularity, false, true);
452         return true;
453     case CommandFromDOM:
454     case CommandFromDOMWithUserInterface:
455         // Doesn't scroll to make the selection visible, or modify the kill ring.
456         // ForwardDelete is not implemented in IE or Firefox, so this behavior is only needed for
457         // backward compatibility with ourselves, and for consistency with Delete.
458         TypingCommand::forwardDeleteKeyPressed(frame->document());
459         return true;
460     }
461     ASSERT_NOT_REACHED();
462     return false;
463 }
464
465 static bool executeIgnoreSpelling(Frame* frame, Event*, EditorCommandSource, const String&)
466 {
467     frame->editor()->ignoreSpelling();
468     return true;
469 }
470
471 static bool executeIndent(Frame* frame, Event*, EditorCommandSource, const String&)
472 {
473     applyCommand(IndentOutdentCommand::create(frame->document(), IndentOutdentCommand::Indent));
474     return true;
475 }
476
477 static bool executeInsertBacktab(Frame* frame, Event* event, EditorCommandSource, const String&)
478 {
479     return targetFrame(frame, event)->eventHandler()->handleTextInputEvent("\t", event, false, true);
480 }
481
482 static bool executeInsertHorizontalRule(Frame* frame, Event*, EditorCommandSource, const String& value)
483 {
484     RefPtr<HTMLHRElement> rule = HTMLHRElement::create(frame->document());
485     if (!value.isEmpty())
486         rule->setIdAttribute(value);
487     return executeInsertNode(frame, rule.release());
488 }
489
490 static bool executeInsertHTML(Frame* frame, Event*, EditorCommandSource, const String& value)
491 {
492     return executeInsertFragment(frame, createFragmentFromMarkup(frame->document(), value, ""));
493 }
494
495 static bool executeInsertImage(Frame* frame, Event*, EditorCommandSource, const String& value)
496 {
497     // FIXME: If userInterface is true, we should display a dialog box and let the user choose a local image.
498     RefPtr<HTMLImageElement> image = HTMLImageElement::create(frame->document());
499     image->setSrc(value);
500     return executeInsertNode(frame, image.release());
501 }
502
503 static bool executeInsertLineBreak(Frame* frame, Event* event, EditorCommandSource source, const String&)
504 {
505     switch (source) {
506     case CommandFromMenuOrKeyBinding:
507         return targetFrame(frame, event)->eventHandler()->handleTextInputEvent("\n", event, true);
508     case CommandFromDOM:
509     case CommandFromDOMWithUserInterface:
510         // Doesn't scroll to make the selection visible, or modify the kill ring.
511         // InsertLineBreak is not implemented in IE or Firefox, so this behavior is only needed for
512         // backward compatibility with ourselves, and for consistency with other commands.
513         TypingCommand::insertLineBreak(frame->document());
514         return true;
515     }
516     ASSERT_NOT_REACHED();
517     return false;
518 }
519
520 static bool executeInsertNewline(Frame* frame, Event* event, EditorCommandSource, const String&)
521 {
522     Frame* targetFrame = WebCore::targetFrame(frame, event);
523     return targetFrame->eventHandler()->handleTextInputEvent("\n", event, !targetFrame->editor()->canEditRichly());
524 }
525
526 static bool executeInsertNewlineInQuotedContent(Frame* frame, Event*, EditorCommandSource, const String&)
527 {
528     TypingCommand::insertParagraphSeparatorInQuotedContent(frame->document());
529     return true;
530 }
531
532 static bool executeInsertOrderedList(Frame* frame, Event*, EditorCommandSource, const String&)
533 {
534     applyCommand(InsertListCommand::create(frame->document(), InsertListCommand::OrderedList));
535     return true;
536 }
537
538 static bool executeInsertParagraph(Frame* frame, Event*, EditorCommandSource, const String&)
539 {
540     TypingCommand::insertParagraphSeparator(frame->document());
541     return true;
542 }
543
544 static bool executeInsertTab(Frame* frame, Event* event, EditorCommandSource, const String&)
545 {
546     return targetFrame(frame, event)->eventHandler()->handleTextInputEvent("\t", event, false, false);
547 }
548
549 static bool executeInsertText(Frame* frame, Event*, EditorCommandSource, const String& value)
550 {
551     TypingCommand::insertText(frame->document(), value);
552     return true;
553 }
554
555 static bool executeInsertUnorderedList(Frame* frame, Event*, EditorCommandSource, const String&)
556 {
557     applyCommand(InsertListCommand::create(frame->document(), InsertListCommand::UnorderedList));
558     return true;
559 }
560
561 static bool executeJustifyCenter(Frame* frame, Event*, EditorCommandSource source, const String&)
562 {
563     return executeApplyParagraphStyle(frame, source, EditActionCenter, CSSPropertyTextAlign, "center");
564 }
565
566 static bool executeJustifyFull(Frame* frame, Event*, EditorCommandSource source, const String&)
567 {
568     return executeApplyParagraphStyle(frame, source, EditActionJustify, CSSPropertyTextAlign, "justify");
569 }
570
571 static bool executeJustifyLeft(Frame* frame, Event*, EditorCommandSource source, const String&)
572 {
573     return executeApplyParagraphStyle(frame, source, EditActionAlignLeft, CSSPropertyTextAlign, "left");
574 }
575
576 static bool executeJustifyRight(Frame* frame, Event*, EditorCommandSource source, const String&)
577 {
578     return executeApplyParagraphStyle(frame, source, EditActionAlignRight, CSSPropertyTextAlign, "right");
579 }
580
581 static bool executeMakeTextWritingDirectionLeftToRight(Frame* frame, Event*, EditorCommandSource, const String&)
582 {
583     RefPtr<CSSMutableStyleDeclaration> style = CSSMutableStyleDeclaration::create();
584     style->setProperty(CSSPropertyUnicodeBidi, CSSValueEmbed);
585     style->setProperty(CSSPropertyDirection, CSSValueLtr);
586     frame->editor()->applyStyle(style.get(), EditActionSetWritingDirection);
587     return true;
588 }
589
590 static bool executeMakeTextWritingDirectionNatural(Frame* frame, Event*, EditorCommandSource, const String&)
591 {
592     RefPtr<CSSMutableStyleDeclaration> style = CSSMutableStyleDeclaration::create();
593     style->setProperty(CSSPropertyUnicodeBidi, CSSValueNormal);
594     frame->editor()->applyStyle(style.get(), EditActionSetWritingDirection);
595     return true;
596 }
597
598 static bool executeMakeTextWritingDirectionRightToLeft(Frame* frame, Event*, EditorCommandSource, const String&)
599 {
600     RefPtr<CSSMutableStyleDeclaration> style = CSSMutableStyleDeclaration::create();
601     style->setProperty(CSSPropertyUnicodeBidi, CSSValueEmbed);
602     style->setProperty(CSSPropertyDirection, CSSValueRtl);
603     frame->editor()->applyStyle(style.get(), EditActionSetWritingDirection);
604     return true;
605 }
606
607 static bool executeMoveBackward(Frame* frame, Event*, EditorCommandSource, const String&)
608 {
609     frame->selection()->modify(SelectionController::AlterationMove, SelectionController::DirectionBackward, CharacterGranularity, true);
610     return true;
611 }
612
613 static bool executeMoveBackwardAndModifySelection(Frame* frame, Event*, EditorCommandSource, const String&)
614 {
615     frame->selection()->modify(SelectionController::AlterationExtend, SelectionController::DirectionBackward, CharacterGranularity, true);
616     return true;
617 }
618
619 static bool executeMoveDown(Frame* frame, Event*, EditorCommandSource, const String&)
620 {
621     return frame->selection()->modify(SelectionController::AlterationMove, SelectionController::DirectionForward, LineGranularity, true);
622 }
623
624 static bool executeMoveDownAndModifySelection(Frame* frame, Event*, EditorCommandSource, const String&)
625 {
626     frame->selection()->modify(SelectionController::AlterationExtend, SelectionController::DirectionForward, LineGranularity, true);
627     return true;
628 }
629
630 static bool executeMoveForward(Frame* frame, Event*, EditorCommandSource, const String&)
631 {
632     frame->selection()->modify(SelectionController::AlterationMove, SelectionController::DirectionForward, CharacterGranularity, true);
633     return true;
634 }
635
636 static bool executeMoveForwardAndModifySelection(Frame* frame, Event*, EditorCommandSource, const String&)
637 {
638     frame->selection()->modify(SelectionController::AlterationExtend, SelectionController::DirectionForward, CharacterGranularity, true);
639     return true;
640 }
641
642 static bool executeMoveLeft(Frame* frame, Event*, EditorCommandSource, const String&)
643 {
644     return frame->selection()->modify(SelectionController::AlterationMove, SelectionController::DirectionLeft, CharacterGranularity, true);
645 }
646
647 static bool executeMoveLeftAndModifySelection(Frame* frame, Event*, EditorCommandSource, const String&)
648 {
649     frame->selection()->modify(SelectionController::AlterationExtend, SelectionController::DirectionLeft, CharacterGranularity, true);
650     return true;
651 }
652
653 static bool executeMovePageDown(Frame* frame, Event*, EditorCommandSource, const String&)
654 {
655     int distance = verticalScrollDistance(frame);
656     if (!distance)
657         return false;
658     return frame->selection()->modify(SelectionController::AlterationMove, distance, true, SelectionController::AlignCursorOnScrollAlways);
659 }
660
661 static bool executeMovePageDownAndModifySelection(Frame* frame, Event*, EditorCommandSource, const String&)
662 {
663     int distance = verticalScrollDistance(frame);
664     if (!distance)
665         return false;
666     return frame->selection()->modify(SelectionController::AlterationExtend, distance, true, SelectionController::AlignCursorOnScrollAlways);
667 }
668
669 static bool executeMovePageUp(Frame* frame, Event*, EditorCommandSource, const String&)
670 {
671     int distance = verticalScrollDistance(frame);
672     if (!distance)
673         return false;
674     return frame->selection()->modify(SelectionController::AlterationMove, -distance, true, SelectionController::AlignCursorOnScrollAlways);
675 }
676
677 static bool executeMovePageUpAndModifySelection(Frame* frame, Event*, EditorCommandSource, const String&)
678 {
679     int distance = verticalScrollDistance(frame);
680     if (!distance)
681         return false;
682     return frame->selection()->modify(SelectionController::AlterationExtend, -distance, true, SelectionController::AlignCursorOnScrollAlways);
683 }
684
685 static bool executeMoveRight(Frame* frame, Event*, EditorCommandSource, const String&)
686 {
687     return frame->selection()->modify(SelectionController::AlterationMove, SelectionController::DirectionRight, CharacterGranularity, true);
688 }
689
690 static bool executeMoveRightAndModifySelection(Frame* frame, Event*, EditorCommandSource, const String&)
691 {
692     frame->selection()->modify(SelectionController::AlterationExtend, SelectionController::DirectionRight, CharacterGranularity, true);
693     return true;
694 }
695
696 static bool executeMoveToBeginningOfDocument(Frame* frame, Event*, EditorCommandSource, const String&)
697 {
698     frame->selection()->modify(SelectionController::AlterationMove, SelectionController::DirectionBackward, DocumentBoundary, true);
699     return true;
700 }
701
702 static bool executeMoveToBeginningOfDocumentAndModifySelection(Frame* frame, Event*, EditorCommandSource, const String&)
703 {
704     frame->selection()->modify(SelectionController::AlterationExtend, SelectionController::DirectionBackward, DocumentBoundary, true);
705     return true;
706 }
707
708 static bool executeMoveToBeginningOfLine(Frame* frame, Event*, EditorCommandSource, const String&)
709 {
710     frame->selection()->modify(SelectionController::AlterationMove, SelectionController::DirectionBackward, LineBoundary, true);
711     return true;
712 }
713
714 static bool executeMoveToBeginningOfLineAndModifySelection(Frame* frame, Event*, EditorCommandSource, const String&)
715 {
716     frame->selection()->modify(SelectionController::AlterationExtend, SelectionController::DirectionBackward, LineBoundary, true);
717     return true;
718 }
719
720 static bool executeMoveToBeginningOfParagraph(Frame* frame, Event*, EditorCommandSource, const String&)
721 {
722     frame->selection()->modify(SelectionController::AlterationMove, SelectionController::DirectionBackward, ParagraphBoundary, true);
723     return true;
724 }
725
726 static bool executeMoveToBeginningOfParagraphAndModifySelection(Frame* frame, Event*, EditorCommandSource, const String&)
727 {
728     frame->selection()->modify(SelectionController::AlterationExtend, SelectionController::DirectionBackward, ParagraphBoundary, true);
729     return true;
730 }
731
732 static bool executeMoveToBeginningOfSentence(Frame* frame, Event*, EditorCommandSource, const String&)
733 {
734     frame->selection()->modify(SelectionController::AlterationMove, SelectionController::DirectionBackward, SentenceBoundary, true);
735     return true;
736 }
737
738 static bool executeMoveToBeginningOfSentenceAndModifySelection(Frame* frame, Event*, EditorCommandSource, const String&)
739 {
740     frame->selection()->modify(SelectionController::AlterationExtend, SelectionController::DirectionBackward, SentenceBoundary, true);
741     return true;
742 }
743
744 static bool executeMoveToEndOfDocument(Frame* frame, Event*, EditorCommandSource, const String&)
745 {
746     frame->selection()->modify(SelectionController::AlterationMove, SelectionController::DirectionForward, DocumentBoundary, true);
747     return true;
748 }
749
750 static bool executeMoveToEndOfDocumentAndModifySelection(Frame* frame, Event*, EditorCommandSource, const String&)
751 {
752     frame->selection()->modify(SelectionController::AlterationExtend, SelectionController::DirectionForward, DocumentBoundary, true);
753     return true;
754 }
755
756 static bool executeMoveToEndOfSentence(Frame* frame, Event*, EditorCommandSource, const String&)
757 {
758     frame->selection()->modify(SelectionController::AlterationMove, SelectionController::DirectionForward, SentenceBoundary, true);
759     return true;
760 }
761
762 static bool executeMoveToEndOfSentenceAndModifySelection(Frame* frame, Event*, EditorCommandSource, const String&)
763 {
764     frame->selection()->modify(SelectionController::AlterationExtend, SelectionController::DirectionForward, SentenceBoundary, true);
765     return true;
766 }
767
768 static bool executeMoveToEndOfLine(Frame* frame, Event*, EditorCommandSource, const String&)
769 {
770     frame->selection()->modify(SelectionController::AlterationMove, SelectionController::DirectionForward, LineBoundary, true);
771     return true;
772 }
773
774 static bool executeMoveToEndOfLineAndModifySelection(Frame* frame, Event*, EditorCommandSource, const String&)
775 {
776     frame->selection()->modify(SelectionController::AlterationExtend, SelectionController::DirectionForward, LineBoundary, true);
777     return true;
778 }
779
780 static bool executeMoveToEndOfParagraph(Frame* frame, Event*, EditorCommandSource, const String&)
781 {
782     frame->selection()->modify(SelectionController::AlterationMove, SelectionController::DirectionForward, ParagraphBoundary, true);
783     return true;
784 }
785
786 static bool executeMoveToEndOfParagraphAndModifySelection(Frame* frame, Event*, EditorCommandSource, const String&)
787 {
788     frame->selection()->modify(SelectionController::AlterationExtend, SelectionController::DirectionForward, ParagraphBoundary, true);
789     return true;
790 }
791
792 static bool executeMoveParagraphBackwardAndModifySelection(Frame* frame, Event*, EditorCommandSource, const String&)
793 {
794     frame->selection()->modify(SelectionController::AlterationExtend, SelectionController::DirectionBackward, ParagraphGranularity, true);
795     return true;
796 }
797
798 static bool executeMoveParagraphForwardAndModifySelection(Frame* frame, Event*, EditorCommandSource, const String&)
799 {
800     frame->selection()->modify(SelectionController::AlterationExtend, SelectionController::DirectionForward, ParagraphGranularity, true);
801     return true;
802 }
803
804 static bool executeMoveUp(Frame* frame, Event*, EditorCommandSource, const String&)
805 {
806     return frame->selection()->modify(SelectionController::AlterationMove, SelectionController::DirectionBackward, LineGranularity, true);
807 }
808
809 static bool executeMoveUpAndModifySelection(Frame* frame, Event*, EditorCommandSource, const String&)
810 {
811     frame->selection()->modify(SelectionController::AlterationExtend, SelectionController::DirectionBackward, LineGranularity, true);
812     return true;
813 }
814
815 static bool executeMoveWordBackward(Frame* frame, Event*, EditorCommandSource, const String&)
816 {
817     frame->selection()->modify(SelectionController::AlterationMove, SelectionController::DirectionBackward, WordGranularity, true);
818     return true;
819 }
820
821 static bool executeMoveWordBackwardAndModifySelection(Frame* frame, Event*, EditorCommandSource, const String&)
822 {
823     frame->selection()->modify(SelectionController::AlterationExtend, SelectionController::DirectionBackward, WordGranularity, true);
824     return true;
825 }
826
827 static bool executeMoveWordForward(Frame* frame, Event*, EditorCommandSource, const String&)
828 {
829     frame->selection()->modify(SelectionController::AlterationMove, SelectionController::DirectionForward, WordGranularity, true);
830     return true;
831 }
832
833 static bool executeMoveWordForwardAndModifySelection(Frame* frame, Event*, EditorCommandSource, const String&)
834 {
835     frame->selection()->modify(SelectionController::AlterationExtend, SelectionController::DirectionForward, WordGranularity, true);
836     return true;
837 }
838
839 static bool executeMoveWordLeft(Frame* frame, Event*, EditorCommandSource, const String&)
840 {
841     frame->selection()->modify(SelectionController::AlterationMove, SelectionController::DirectionLeft, WordGranularity, true);
842     return true;
843 }
844
845 static bool executeMoveWordLeftAndModifySelection(Frame* frame, Event*, EditorCommandSource, const String&)
846 {
847     frame->selection()->modify(SelectionController::AlterationExtend, SelectionController::DirectionLeft, WordGranularity, true);
848     return true;
849 }
850
851 static bool executeMoveWordRight(Frame* frame, Event*, EditorCommandSource, const String&)
852 {
853     frame->selection()->modify(SelectionController::AlterationMove, SelectionController::DirectionRight, WordGranularity, true);
854     return true;
855 }
856
857 static bool executeMoveWordRightAndModifySelection(Frame* frame, Event*, EditorCommandSource, const String&)
858 {
859     frame->selection()->modify(SelectionController::AlterationExtend, SelectionController::DirectionRight, WordGranularity, true);
860     return true;
861 }
862
863 static bool executeMoveToLeftEndOfLine(Frame* frame, Event*, EditorCommandSource, const String&)
864 {
865     frame->selection()->modify(SelectionController::AlterationMove, SelectionController::DirectionLeft, LineBoundary, true);
866     return true;
867 }
868
869 static bool executeMoveToLeftEndOfLineAndModifySelection(Frame* frame, Event*, EditorCommandSource, const String&)
870 {
871     frame->selection()->modify(SelectionController::AlterationExtend, SelectionController::DirectionLeft, LineBoundary, true);
872     return true;
873 }
874
875 static bool executeMoveToRightEndOfLine(Frame* frame, Event*, EditorCommandSource, const String&)
876 {
877     frame->selection()->modify(SelectionController::AlterationMove, SelectionController::DirectionRight, LineBoundary, true);
878     return true;
879 }
880
881 static bool executeMoveToRightEndOfLineAndModifySelection(Frame* frame, Event*, EditorCommandSource, const String&)
882 {
883     frame->selection()->modify(SelectionController::AlterationExtend, SelectionController::DirectionRight, LineBoundary, true);
884     return true;
885 }
886
887 static bool executeOutdent(Frame* frame, Event*, EditorCommandSource, const String&)
888 {
889     applyCommand(IndentOutdentCommand::create(frame->document(), IndentOutdentCommand::Outdent));
890     return true;
891 }
892
893 static bool executePaste(Frame* frame, Event*, EditorCommandSource, const String&)
894 {
895     frame->editor()->paste();
896     return true;
897 }
898
899 static bool executePasteAndMatchStyle(Frame* frame, Event*, EditorCommandSource, const String&)
900 {
901     frame->editor()->pasteAsPlainText();
902     return true;
903 }
904
905 static bool executePasteAsPlainText(Frame* frame, Event*, EditorCommandSource, const String&)
906 {
907     frame->editor()->pasteAsPlainText();
908     return true;
909 }
910
911 static bool executePrint(Frame* frame, Event*, EditorCommandSource, const String&)
912 {
913     Page* page = frame->page();
914     if (!page)
915         return false;
916     page->chrome()->print(frame);
917     return true;
918 }
919
920 static bool executeRedo(Frame* frame, Event*, EditorCommandSource, const String&)
921 {
922     frame->editor()->redo();
923     return true;
924 }
925
926 static bool executeRemoveFormat(Frame* frame, Event*, EditorCommandSource, const String&)
927 {
928     frame->editor()->removeFormattingAndStyle();
929     return true;
930 }
931
932 static bool executeSelectAll(Frame* frame, Event*, EditorCommandSource, const String&)
933 {
934     frame->selection()->selectAll();
935     return true;
936 }
937
938 static bool executeSelectLine(Frame* frame, Event*, EditorCommandSource, const String&)
939 {
940     return expandSelectionToGranularity(frame, LineGranularity);
941 }
942
943 static bool executeSelectParagraph(Frame* frame, Event*, EditorCommandSource, const String&)
944 {
945     return expandSelectionToGranularity(frame, ParagraphGranularity);
946 }
947
948 static bool executeSelectSentence(Frame* frame, Event*, EditorCommandSource, const String&)
949 {
950     return expandSelectionToGranularity(frame, SentenceGranularity);
951 }
952
953 static bool executeSelectToMark(Frame* frame, Event*, EditorCommandSource, const String&)
954 {
955     RefPtr<Range> mark = frame->editor()->mark().toNormalizedRange();
956     RefPtr<Range> selection = frame->editor()->selectedRange();
957     if (!mark || !selection) {
958         systemBeep();
959         return false;
960     }
961     frame->selection()->setSelectedRange(unionDOMRanges(mark.get(), selection.get()).get(), DOWNSTREAM, true);
962     return true;
963 }
964
965 static bool executeSelectWord(Frame* frame, Event*, EditorCommandSource, const String&)
966 {
967     return expandSelectionToGranularity(frame, WordGranularity);
968 }
969
970 static bool executeSetMark(Frame* frame, Event*, EditorCommandSource, const String&)
971 {
972     frame->editor()->setMark(frame->selection()->selection());
973     return true;
974 }
975
976 static bool executeStrikethrough(Frame* frame, Event*, EditorCommandSource source, const String&)
977 {
978     RefPtr<CSSPrimitiveValue> lineThrough = CSSPrimitiveValue::createIdentifier(CSSValueLineThrough);
979     return executeToggleStyleInList(frame, source, EditActionUnderline, CSSPropertyWebkitTextDecorationsInEffect, lineThrough.get());
980 }
981
982 static bool executeStyleWithCSS(Frame* frame, Event*, EditorCommandSource, const String& value)
983 {
984     if (value != "false" && value != "true")
985         return false;
986     
987     frame->editor()->setShouldStyleWithCSS(value == "true" ? true : false);
988     return true;
989 }
990
991 static bool executeSubscript(Frame* frame, Event*, EditorCommandSource source, const String&)
992 {
993     return executeToggleStyle(frame, source, EditActionSubscript, CSSPropertyVerticalAlign, "baseline", "sub");
994 }
995
996 static bool executeSuperscript(Frame* frame, Event*, EditorCommandSource source, const String&)
997 {
998     return executeToggleStyle(frame, source, EditActionSuperscript, CSSPropertyVerticalAlign, "baseline", "super");
999 }
1000
1001 static bool executeSwapWithMark(Frame* frame, Event*, EditorCommandSource, const String&)
1002 {
1003     const VisibleSelection& mark = frame->editor()->mark();
1004     const VisibleSelection& selection = frame->selection()->selection();
1005     if (mark.isNone() || selection.isNone()) {
1006         systemBeep();
1007         return false;
1008     }
1009     frame->selection()->setSelection(mark);
1010     frame->editor()->setMark(selection);
1011     return true;
1012 }
1013
1014 #if PLATFORM(MAC)
1015 static bool executeTakeFindStringFromSelection(Frame* frame, Event*, EditorCommandSource, const String&)
1016 {
1017     frame->editor()->takeFindStringFromSelection();
1018     return true;
1019 }
1020 #endif
1021
1022 static bool executeToggleBold(Frame* frame, Event*, EditorCommandSource source, const String&)
1023 {
1024     return executeToggleStyle(frame, source, EditActionChangeAttributes, CSSPropertyFontWeight, "normal", "bold");
1025 }
1026
1027 static bool executeToggleItalic(Frame* frame, Event*, EditorCommandSource source, const String&)
1028 {
1029     return executeToggleStyle(frame, source, EditActionChangeAttributes, CSSPropertyFontStyle, "normal", "italic");
1030 }
1031
1032 static bool executeTranspose(Frame* frame, Event*, EditorCommandSource, const String&)
1033 {
1034     frame->editor()->transpose();
1035     return true;
1036 }
1037
1038 static bool executeUnderline(Frame* frame, Event*, EditorCommandSource source, const String&)
1039 {
1040     RefPtr<CSSPrimitiveValue> underline = CSSPrimitiveValue::createIdentifier(CSSValueUnderline);
1041     return executeToggleStyleInList(frame, source, EditActionUnderline, CSSPropertyWebkitTextDecorationsInEffect, underline.get());
1042 }
1043
1044 static bool executeUndo(Frame* frame, Event*, EditorCommandSource, const String&)
1045 {
1046     frame->editor()->undo();
1047     return true;
1048 }
1049
1050 static bool executeUnlink(Frame* frame, Event*, EditorCommandSource, const String&)
1051 {
1052     applyCommand(UnlinkCommand::create(frame->document()));
1053     return true;
1054 }
1055
1056 static bool executeUnscript(Frame* frame, Event*, EditorCommandSource source, const String&)
1057 {
1058     return executeApplyStyle(frame, source, EditActionUnscript, CSSPropertyVerticalAlign, "baseline");
1059 }
1060
1061 static bool executeUnselect(Frame* frame, Event*, EditorCommandSource, const String&)
1062 {
1063     frame->selection()->clear();
1064     return true;
1065 }
1066
1067 static bool executeYank(Frame* frame, Event*, EditorCommandSource, const String&)
1068 {
1069     frame->editor()->insertTextWithoutSendingTextEvent(frame->editor()->killRing()->yank(), false, 0);
1070     frame->editor()->killRing()->setToYankedState();
1071     return true;
1072 }
1073
1074 static bool executeYankAndSelect(Frame* frame, Event*, EditorCommandSource, const String&)
1075 {
1076     frame->editor()->insertTextWithoutSendingTextEvent(frame->editor()->killRing()->yank(), true, 0);
1077     frame->editor()->killRing()->setToYankedState();
1078     return true;
1079 }
1080
1081 #if SUPPORT_AUTOCORRECTION_PANEL
1082 static bool executeCancelOperation(Frame* frame, Event*, EditorCommandSource, const String&)
1083 {
1084     frame->editor()->handleCancelOperation();
1085     return true;
1086 }
1087 #endif
1088
1089 // Supported functions
1090
1091 static bool supported(Frame*, EditorCommandSource)
1092 {
1093     return true;
1094 }
1095
1096 static bool supportedFromMenuOrKeyBinding(Frame*, EditorCommandSource source)
1097 {
1098     return source == CommandFromMenuOrKeyBinding;
1099 }
1100
1101 static bool supportedCopyCut(Frame* frame, EditorCommandSource source)
1102 {
1103     switch (source) {
1104     case CommandFromMenuOrKeyBinding:
1105         return true;
1106     case CommandFromDOM:
1107     case CommandFromDOMWithUserInterface: {
1108         Settings* settings = frame ? frame->settings() : 0;
1109         return settings && settings->javaScriptCanAccessClipboard();
1110     }
1111     }
1112     ASSERT_NOT_REACHED();
1113     return false;
1114 }
1115
1116 static bool supportedPaste(Frame* frame, EditorCommandSource source)
1117 {
1118     switch (source) {
1119     case CommandFromMenuOrKeyBinding:
1120         return true;
1121     case CommandFromDOM:
1122     case CommandFromDOMWithUserInterface: {
1123         Settings* settings = frame ? frame->settings() : 0;
1124         return settings && (settings->javaScriptCanAccessClipboard() ? settings->isDOMPasteAllowed() : 0);
1125     }
1126     }
1127     ASSERT_NOT_REACHED();
1128     return false;
1129 }
1130
1131 #if SUPPORT_AUTOCORRECTION_PANEL
1132 static bool supportedDismissCorrectionPanel(Frame* frame, EditorCommandSource source)
1133 {
1134     return supportedFromMenuOrKeyBinding(frame, source) && frame->editor()->isShowingCorrectionPanel();
1135 }
1136 #endif
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 // State functions
1253
1254 static TriState stateNone(Frame*, Event*)
1255 {
1256     return FalseTriState;
1257 }
1258
1259 static TriState stateBold(Frame* frame, Event*)
1260 {
1261     return stateStyle(frame, CSSPropertyFontWeight, "bold");
1262 }
1263
1264 static TriState stateItalic(Frame* frame, Event*)
1265 {
1266     return stateStyle(frame, CSSPropertyFontStyle, "italic");
1267 }
1268
1269 static TriState stateOrderedList(Frame* frame, Event*)
1270 {
1271     return frame->editor()->selectionOrderedListState();
1272 }
1273
1274 static TriState stateStrikethrough(Frame* frame, Event*)
1275 {
1276     return stateStyle(frame, CSSPropertyWebkitTextDecorationsInEffect, "line-through");
1277 }
1278
1279 static TriState stateStyleWithCSS(Frame* frame, Event*)
1280 {
1281     return frame->editor()->shouldStyleWithCSS() ? TrueTriState : FalseTriState;
1282 }
1283
1284 static TriState stateSubscript(Frame* frame, Event*)
1285 {
1286     return stateStyle(frame, CSSPropertyVerticalAlign, "sub");
1287 }
1288
1289 static TriState stateSuperscript(Frame* frame, Event*)
1290 {
1291     return stateStyle(frame, CSSPropertyVerticalAlign, "super");
1292 }
1293
1294 static TriState stateTextWritingDirectionLeftToRight(Frame* frame, Event*)
1295 {
1296     return stateTextWritingDirection(frame, LeftToRightWritingDirection);
1297 }
1298
1299 static TriState stateTextWritingDirectionNatural(Frame* frame, Event*)
1300 {
1301     return stateTextWritingDirection(frame, NaturalWritingDirection);
1302 }
1303
1304 static TriState stateTextWritingDirectionRightToLeft(Frame* frame, Event*)
1305 {
1306     return stateTextWritingDirection(frame, RightToLeftWritingDirection);
1307 }
1308
1309 static TriState stateUnderline(Frame* frame, Event*)
1310 {
1311     return stateStyle(frame, CSSPropertyWebkitTextDecorationsInEffect, "underline");
1312 }
1313
1314 static TriState stateUnorderedList(Frame* frame, Event*)
1315 {
1316     return frame->editor()->selectionUnorderedListState();
1317 }
1318
1319 static TriState stateJustifyCenter(Frame* frame, Event*)
1320 {
1321     return stateStyle(frame, CSSPropertyTextAlign, "center");
1322 }
1323
1324 static TriState stateJustifyFull(Frame* frame, Event*)
1325 {
1326     return stateStyle(frame, CSSPropertyTextAlign, "justify");
1327 }
1328
1329 static TriState stateJustifyLeft(Frame* frame, Event*)
1330 {
1331     return stateStyle(frame, CSSPropertyTextAlign, "left");
1332 }
1333
1334 static TriState stateJustifyRight(Frame* frame, Event*)
1335 {
1336     return stateStyle(frame, CSSPropertyTextAlign, "right");
1337 }
1338
1339 // Value functions
1340
1341 static String valueNull(Frame*, Event*)
1342 {
1343     return String();
1344 }
1345
1346 static String valueBackColor(Frame* frame, Event*)
1347 {
1348     return valueStyle(frame, CSSPropertyBackgroundColor);
1349 }
1350
1351 static String valueFontName(Frame* frame, Event*)
1352 {
1353     return valueStyle(frame, CSSPropertyFontFamily);
1354 }
1355
1356 static String valueFontSize(Frame* frame, Event*)
1357 {
1358     return valueStyle(frame, CSSPropertyFontSize);
1359 }
1360
1361 static String valueFontSizeDelta(Frame* frame, Event*)
1362 {
1363     return valueStyle(frame, CSSPropertyWebkitFontSizeDelta);
1364 }
1365
1366 static String valueForeColor(Frame* frame, Event*)
1367 {
1368     return valueStyle(frame, CSSPropertyColor);
1369 }
1370
1371 static String valueFormatBlock(Frame* frame, Event*)
1372 {
1373     const VisibleSelection& selection = frame->selection()->selection();
1374     if (!selection.isNonOrphanedCaretOrRange() || !selection.isContentEditable())
1375         return "";
1376     Element* formatBlockElement = FormatBlockCommand::elementForFormatBlockCommand(selection.firstRange().get());
1377     if (!formatBlockElement)
1378         return "";
1379     return formatBlockElement->localName();
1380 }
1381
1382 // Map of functions
1383
1384 struct CommandEntry {
1385     const char* name;
1386     EditorInternalCommand command;
1387 };
1388
1389 static const CommandMap& createCommandMap()
1390 {
1391     static const CommandEntry commands[] = {
1392         { "AlignCenter", { executeJustifyCenter, supportedFromMenuOrKeyBinding, enabledInRichlyEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1393         { "AlignJustified", { executeJustifyFull, supportedFromMenuOrKeyBinding, enabledInRichlyEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1394         { "AlignLeft", { executeJustifyLeft, supportedFromMenuOrKeyBinding, enabledInRichlyEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1395         { "AlignRight", { executeJustifyRight, supportedFromMenuOrKeyBinding, enabledInRichlyEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1396         { "BackColor", { executeBackColor, supported, enabledInRichlyEditableText, stateNone, valueBackColor, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1397         { "BackwardDelete", { executeDeleteBackward, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } }, // FIXME: remove BackwardDelete when Safari for Windows stops using it.
1398         { "Bold", { executeToggleBold, supported, enabledInRichlyEditableText, stateBold, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1399         { "Copy", { executeCopy, supportedCopyCut, enabledCopy, stateNone, valueNull, notTextInsertion, allowExecutionWhenDisabled } },
1400         { "CreateLink", { executeCreateLink, supported, enabledInRichlyEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1401         { "Cut", { executeCut, supportedCopyCut, enabledCut, stateNone, valueNull, notTextInsertion, allowExecutionWhenDisabled } },
1402         { "Delete", { executeDelete, supported, enabledDelete, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1403         { "DeleteBackward", { executeDeleteBackward, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1404         { "DeleteBackwardByDecomposingPreviousCharacter", { executeDeleteBackwardByDecomposingPreviousCharacter, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1405         { "DeleteForward", { executeDeleteForward, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1406         { "DeleteToBeginningOfLine", { executeDeleteToBeginningOfLine, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1407         { "DeleteToBeginningOfParagraph", { executeDeleteToBeginningOfParagraph, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1408         { "DeleteToEndOfLine", { executeDeleteToEndOfLine, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1409         { "DeleteToEndOfParagraph", { executeDeleteToEndOfParagraph, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1410         { "DeleteToMark", { executeDeleteToMark, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1411         { "DeleteWordBackward", { executeDeleteWordBackward, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1412         { "DeleteWordForward", { executeDeleteWordForward, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1413         { "FindString", { executeFindString, supported, enabled, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1414         { "FontName", { executeFontName, supported, enabledInEditableText, stateNone, valueFontName, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1415         { "FontSize", { executeFontSize, supported, enabledInEditableText, stateNone, valueFontSize, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1416         { "FontSizeDelta", { executeFontSizeDelta, supported, enabledInEditableText, stateNone, valueFontSizeDelta, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1417         { "ForeColor", { executeForeColor, supported, enabledInRichlyEditableText, stateNone, valueForeColor, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1418         { "FormatBlock", { executeFormatBlock, supported, enabledInRichlyEditableText, stateNone, valueFormatBlock, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1419         { "ForwardDelete", { executeForwardDelete, supported, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1420         { "HiliteColor", { executeBackColor, supported, enabledInRichlyEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1421         { "IgnoreSpelling", { executeIgnoreSpelling, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1422         { "Indent", { executeIndent, supported, enabledInRichlyEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1423         { "InsertBacktab", { executeInsertBacktab, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, isTextInsertion, doNotAllowExecutionWhenDisabled } },
1424         { "InsertHTML", { executeInsertHTML, supported, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1425         { "InsertHorizontalRule", { executeInsertHorizontalRule, supported, enabledInRichlyEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1426         { "InsertImage", { executeInsertImage, supported, enabledInRichlyEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1427         { "InsertLineBreak", { executeInsertLineBreak, supported, enabledInEditableText, stateNone, valueNull, isTextInsertion, doNotAllowExecutionWhenDisabled } },
1428         { "InsertNewline", { executeInsertNewline, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, isTextInsertion, doNotAllowExecutionWhenDisabled } },    
1429         { "InsertNewlineInQuotedContent", { executeInsertNewlineInQuotedContent, supported, enabledInRichlyEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1430         { "InsertOrderedList", { executeInsertOrderedList, supported, enabledInRichlyEditableText, stateOrderedList, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1431         { "InsertParagraph", { executeInsertParagraph, supported, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1432         { "InsertTab", { executeInsertTab, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, isTextInsertion, doNotAllowExecutionWhenDisabled } },
1433         { "InsertText", { executeInsertText, supported, enabledInEditableText, stateNone, valueNull, isTextInsertion, doNotAllowExecutionWhenDisabled } },
1434         { "InsertUnorderedList", { executeInsertUnorderedList, supported, enabledInRichlyEditableText, stateUnorderedList, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1435         { "Italic", { executeToggleItalic, supported, enabledInRichlyEditableText, stateItalic, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1436         { "JustifyCenter", { executeJustifyCenter, supported, enabledInRichlyEditableText, stateJustifyCenter, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1437         { "JustifyFull", { executeJustifyFull, supported, enabledInRichlyEditableText, stateJustifyFull, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1438         { "JustifyLeft", { executeJustifyLeft, supported, enabledInRichlyEditableText, stateJustifyLeft, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1439         { "JustifyNone", { executeJustifyLeft, supported, enabledInRichlyEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1440         { "JustifyRight", { executeJustifyRight, supported, enabledInRichlyEditableText, stateJustifyRight, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1441         { "MakeTextWritingDirectionLeftToRight", { executeMakeTextWritingDirectionLeftToRight, supportedFromMenuOrKeyBinding, enabledInRichlyEditableText, stateTextWritingDirectionLeftToRight, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1442         { "MakeTextWritingDirectionNatural", { executeMakeTextWritingDirectionNatural, supportedFromMenuOrKeyBinding, enabledInRichlyEditableText, stateTextWritingDirectionNatural, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1443         { "MakeTextWritingDirectionRightToLeft", { executeMakeTextWritingDirectionRightToLeft, supportedFromMenuOrKeyBinding, enabledInRichlyEditableText, stateTextWritingDirectionRightToLeft, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1444         { "MoveBackward", { executeMoveBackward, supportedFromMenuOrKeyBinding, enabledInEditableTextOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1445         { "MoveBackwardAndModifySelection", { executeMoveBackwardAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelectionOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1446         { "MoveDown", { executeMoveDown, supportedFromMenuOrKeyBinding, enabledInEditableTextOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1447         { "MoveDownAndModifySelection", { executeMoveDownAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelectionOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1448         { "MoveForward", { executeMoveForward, supportedFromMenuOrKeyBinding, enabledInEditableTextOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1449         { "MoveForwardAndModifySelection", { executeMoveForwardAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelectionOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1450         { "MoveLeft", { executeMoveLeft, supportedFromMenuOrKeyBinding, enabledInEditableTextOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1451         { "MoveLeftAndModifySelection", { executeMoveLeftAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelectionOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1452         { "MovePageDown", { executeMovePageDown, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1453         { "MovePageDownAndModifySelection", { executeMovePageDownAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelection, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1454         { "MovePageUp", { executeMovePageUp, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1455         { "MovePageUpAndModifySelection", { executeMovePageUpAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelection, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1456         { "MoveParagraphBackwardAndModifySelection", { executeMoveParagraphBackwardAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelectionOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1457         { "MoveParagraphForwardAndModifySelection", { executeMoveParagraphForwardAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelectionOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1458         { "MoveRight", { executeMoveRight, supportedFromMenuOrKeyBinding, enabledInEditableTextOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1459         { "MoveRightAndModifySelection", { executeMoveRightAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelectionOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1460         { "MoveToBeginningOfDocument", { executeMoveToBeginningOfDocument, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1461         { "MoveToBeginningOfDocumentAndModifySelection", { executeMoveToBeginningOfDocumentAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelection, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1462         { "MoveToBeginningOfLine", { executeMoveToBeginningOfLine, supportedFromMenuOrKeyBinding, enabledInEditableTextOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1463         { "MoveToBeginningOfLineAndModifySelection", { executeMoveToBeginningOfLineAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelectionOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1464         { "MoveToBeginningOfParagraph", { executeMoveToBeginningOfParagraph, supportedFromMenuOrKeyBinding, enabledInEditableTextOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1465         { "MoveToBeginningOfParagraphAndModifySelection", { executeMoveToBeginningOfParagraphAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelectionOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1466         { "MoveToBeginningOfSentence", { executeMoveToBeginningOfSentence, supportedFromMenuOrKeyBinding, enabledInEditableTextOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1467         { "MoveToBeginningOfSentenceAndModifySelection", { executeMoveToBeginningOfSentenceAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelectionOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1468         { "MoveToEndOfDocument", { executeMoveToEndOfDocument, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1469         { "MoveToEndOfDocumentAndModifySelection", { executeMoveToEndOfDocumentAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelection, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1470         { "MoveToEndOfLine", { executeMoveToEndOfLine, supportedFromMenuOrKeyBinding, enabledInEditableTextOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1471         { "MoveToEndOfLineAndModifySelection", { executeMoveToEndOfLineAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelectionOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1472         { "MoveToEndOfParagraph", { executeMoveToEndOfParagraph, supportedFromMenuOrKeyBinding, enabledInEditableTextOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1473         { "MoveToEndOfParagraphAndModifySelection", { executeMoveToEndOfParagraphAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelectionOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1474         { "MoveToEndOfSentence", { executeMoveToEndOfSentence, supportedFromMenuOrKeyBinding, enabledInEditableTextOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1475         { "MoveToEndOfSentenceAndModifySelection", { executeMoveToEndOfSentenceAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelectionOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1476         { "MoveToLeftEndOfLine", { executeMoveToLeftEndOfLine, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1477         { "MoveToLeftEndOfLineAndModifySelection", { executeMoveToLeftEndOfLineAndModifySelection, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1478         { "MoveToRightEndOfLine", { executeMoveToRightEndOfLine, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1479         { "MoveToRightEndOfLineAndModifySelection", { executeMoveToRightEndOfLineAndModifySelection, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1480         { "MoveUp", { executeMoveUp, supportedFromMenuOrKeyBinding, enabledInEditableTextOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1481         { "MoveUpAndModifySelection", { executeMoveUpAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelectionOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1482         { "MoveWordBackward", { executeMoveWordBackward, supportedFromMenuOrKeyBinding, enabledInEditableTextOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1483         { "MoveWordBackwardAndModifySelection", { executeMoveWordBackwardAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelectionOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1484         { "MoveWordForward", { executeMoveWordForward, supportedFromMenuOrKeyBinding, enabledInEditableTextOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1485         { "MoveWordForwardAndModifySelection", { executeMoveWordForwardAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelectionOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1486         { "MoveWordLeft", { executeMoveWordLeft, supportedFromMenuOrKeyBinding, enabledInEditableTextOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1487         { "MoveWordLeftAndModifySelection", { executeMoveWordLeftAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelectionOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1488         { "MoveWordRight", { executeMoveWordRight, supportedFromMenuOrKeyBinding, enabledInEditableTextOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1489         { "MoveWordRightAndModifySelection", { executeMoveWordRightAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelectionOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1490         { "Outdent", { executeOutdent, supported, enabledInRichlyEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1491         { "Paste", { executePaste, supportedPaste, enabledPaste, stateNone, valueNull, notTextInsertion, allowExecutionWhenDisabled } },
1492         { "PasteAndMatchStyle", { executePasteAndMatchStyle, supportedPaste, enabledPaste, stateNone, valueNull, notTextInsertion, allowExecutionWhenDisabled } },
1493         { "PasteAsPlainText", { executePasteAsPlainText, supportedPaste, enabledPaste, stateNone, valueNull, notTextInsertion, allowExecutionWhenDisabled } },
1494         { "Print", { executePrint, supported, enabled, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1495         { "Redo", { executeRedo, supported, enabledRedo, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1496         { "RemoveFormat", { executeRemoveFormat, supported, enabledRangeInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1497         { "SelectAll", { executeSelectAll, supported, enabled, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1498         { "SelectLine", { executeSelectLine, supportedFromMenuOrKeyBinding, enabledVisibleSelection, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1499         { "SelectParagraph", { executeSelectParagraph, supportedFromMenuOrKeyBinding, enabledVisibleSelection, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1500         { "SelectSentence", { executeSelectSentence, supportedFromMenuOrKeyBinding, enabledVisibleSelection, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1501         { "SelectToMark", { executeSelectToMark, supportedFromMenuOrKeyBinding, enabledVisibleSelectionAndMark, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1502         { "SelectWord", { executeSelectWord, supportedFromMenuOrKeyBinding, enabledVisibleSelection, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1503         { "SetMark", { executeSetMark, supportedFromMenuOrKeyBinding, enabledVisibleSelection, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1504         { "Strikethrough", { executeStrikethrough, supported, enabledInRichlyEditableText, stateStrikethrough, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1505         { "StyleWithCSS", { executeStyleWithCSS, supported, enabledInRichlyEditableText, stateStyleWithCSS, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1506         { "Subscript", { executeSubscript, supported, enabledInRichlyEditableText, stateSubscript, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1507         { "Superscript", { executeSuperscript, supported, enabledInRichlyEditableText, stateSuperscript, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1508         { "SwapWithMark", { executeSwapWithMark, supportedFromMenuOrKeyBinding, enabledVisibleSelectionAndMark, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1509 #if PLATFORM(MAC)
1510         { "TakeFindStringFromSelection", { executeTakeFindStringFromSelection, supportedFromMenuOrKeyBinding, enabledTakeFindStringFromSelection, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1511 #endif
1512         { "ToggleBold", { executeToggleBold, supportedFromMenuOrKeyBinding, enabledInRichlyEditableText, stateBold, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1513         { "ToggleItalic", { executeToggleItalic, supportedFromMenuOrKeyBinding, enabledInRichlyEditableText, stateItalic, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1514         { "ToggleUnderline", { executeUnderline, supportedFromMenuOrKeyBinding, enabledInRichlyEditableText, stateUnderline, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1515         { "Transpose", { executeTranspose, supported, enableCaretInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1516         { "Underline", { executeUnderline, supported, enabledInRichlyEditableText, stateUnderline, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1517         { "Undo", { executeUndo, supported, enabledUndo, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1518         { "Unlink", { executeUnlink, supported, enabledRangeInRichlyEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1519         { "Unscript", { executeUnscript, supportedFromMenuOrKeyBinding, enabledInRichlyEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1520         { "Unselect", { executeUnselect, supported, enabledVisibleSelection, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1521         { "Yank", { executeYank, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1522         { "YankAndSelect", { executeYankAndSelect, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1523 #if SUPPORT_AUTOCORRECTION_PANEL
1524         { "CancelOperation", { executeCancelOperation, supportedDismissCorrectionPanel, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1525 #endif
1526     };
1527
1528     // These unsupported commands are listed here since they appear in the Microsoft
1529     // documentation used as the starting point for our DOM executeCommand support.
1530     //
1531     // 2D-Position (not supported)
1532     // AbsolutePosition (not supported)
1533     // BlockDirLTR (not supported)
1534     // BlockDirRTL (not supported)
1535     // BrowseMode (not supported)
1536     // ClearAuthenticationCache (not supported)
1537     // CreateBookmark (not supported)
1538     // DirLTR (not supported)
1539     // DirRTL (not supported)
1540     // EditMode (not supported)
1541     // InlineDirLTR (not supported)
1542     // InlineDirRTL (not supported)
1543     // InsertButton (not supported)
1544     // InsertFieldSet (not supported)
1545     // InsertIFrame (not supported)
1546     // InsertInputButton (not supported)
1547     // InsertInputCheckbox (not supported)
1548     // InsertInputFileUpload (not supported)
1549     // InsertInputHidden (not supported)
1550     // InsertInputImage (not supported)
1551     // InsertInputPassword (not supported)
1552     // InsertInputRadio (not supported)
1553     // InsertInputReset (not supported)
1554     // InsertInputSubmit (not supported)
1555     // InsertInputText (not supported)
1556     // InsertMarquee (not supported)
1557     // InsertSelectDropDown (not supported)
1558     // InsertSelectListBox (not supported)
1559     // InsertTextArea (not supported)
1560     // LiveResize (not supported)
1561     // MultipleSelection (not supported)
1562     // Open (not supported)
1563     // Overwrite (not supported)
1564     // PlayImage (not supported)
1565     // Refresh (not supported)
1566     // RemoveParaFormat (not supported)
1567     // SaveAs (not supported)
1568     // SizeToControl (not supported)
1569     // SizeToControlHeight (not supported)
1570     // SizeToControlWidth (not supported)
1571     // Stop (not supported)
1572     // StopImage (not supported)
1573     // Unbookmark (not supported)
1574
1575     CommandMap& commandMap = *new CommandMap;
1576
1577     for (size_t i = 0; i < WTF_ARRAY_LENGTH(commands); ++i) {
1578         ASSERT(!commandMap.get(commands[i].name));
1579         commandMap.set(commands[i].name, &commands[i].command);
1580     }
1581
1582     return commandMap;
1583 }
1584
1585 Editor::Command Editor::command(const String& commandName)
1586 {
1587     return command(commandName, CommandFromMenuOrKeyBinding);
1588 }
1589
1590 Editor::Command Editor::command(const String& commandName, EditorCommandSource source)
1591 {
1592     if (commandName.isEmpty())
1593         return Command();
1594
1595     static const CommandMap& commandMap = createCommandMap();
1596     const EditorInternalCommand* internalCommand = commandMap.get(commandName);
1597     return internalCommand ? Command(m_frame, internalCommand, source) : Command();
1598 }
1599
1600 Editor::Command::Command()
1601     : m_command(0)
1602     , m_source()
1603 {
1604 }
1605
1606 Editor::Command::Command(PassRefPtr<Frame> frame, const EditorInternalCommand* command, EditorCommandSource source)
1607     : m_frame(frame)
1608     , m_command(command)
1609     , m_source(source)
1610 {
1611     ASSERT(m_frame);
1612     ASSERT(m_command);
1613 }
1614
1615 bool Editor::Command::execute(const String& parameter, Event* triggeringEvent) const
1616 {
1617     if (!isEnabled(triggeringEvent)) {
1618         // Let certain commands be executed when performed explicitly even if they are disabled.
1619         if (!isSupported() || !m_frame || !m_command->allowExecutionWhenDisabled)
1620             return false;
1621     }
1622     m_frame->document()->updateLayoutIgnorePendingStylesheets();
1623     return m_command->execute(m_frame.get(), triggeringEvent, m_source, parameter);
1624 }
1625
1626 bool Editor::Command::execute(Event* triggeringEvent) const
1627 {
1628     return execute(String(), triggeringEvent);
1629 }
1630
1631 bool Editor::Command::isSupported() const
1632 {
1633     return m_command && m_command->isSupported(m_frame.get(), m_source);
1634 }
1635
1636 bool Editor::Command::isEnabled(Event* triggeringEvent) const
1637 {
1638     if (!isSupported() || !m_frame)
1639         return false;
1640     return m_command->isEnabled(m_frame.get(), triggeringEvent, m_source);
1641 }
1642
1643 TriState Editor::Command::state(Event* triggeringEvent) const
1644 {
1645     if (!isSupported() || !m_frame)
1646         return FalseTriState;
1647     return m_command->state(m_frame.get(), triggeringEvent);
1648 }
1649
1650 String Editor::Command::value(Event* triggeringEvent) const
1651 {
1652     if (!isSupported() || !m_frame)
1653         return String();
1654     if (m_command->value == valueNull && m_command->state != stateNone)
1655         return m_command->state(m_frame.get(), triggeringEvent) == TrueTriState ? "true" : "false";
1656     return m_command->value(m_frame.get(), triggeringEvent);
1657 }
1658
1659 bool Editor::Command::isTextInsertion() const
1660 {
1661     return m_command && m_command->isTextInsertion;
1662 }
1663
1664 } // namespace WebCore