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