OSDN Git Service

Merge WebKit at r71558: Initial merge by git.
[android-x86/external-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 static bool executeToggleBold(Frame* frame, Event*, EditorCommandSource source, const String&)
1015 {
1016     return executeToggleStyle(frame, source, EditActionChangeAttributes, CSSPropertyFontWeight, "normal", "bold");
1017 }
1018
1019 static bool executeToggleItalic(Frame* frame, Event*, EditorCommandSource source, const String&)
1020 {
1021     return executeToggleStyle(frame, source, EditActionChangeAttributes, CSSPropertyFontStyle, "normal", "italic");
1022 }
1023
1024 static bool executeTranspose(Frame* frame, Event*, EditorCommandSource, const String&)
1025 {
1026     frame->editor()->transpose();
1027     return true;
1028 }
1029
1030 static bool executeUnderline(Frame* frame, Event*, EditorCommandSource source, const String&)
1031 {
1032     RefPtr<CSSPrimitiveValue> underline = CSSPrimitiveValue::createIdentifier(CSSValueUnderline);
1033     return executeToggleStyleInList(frame, source, EditActionUnderline, CSSPropertyWebkitTextDecorationsInEffect, underline.get());
1034 }
1035
1036 static bool executeUndo(Frame* frame, Event*, EditorCommandSource, const String&)
1037 {
1038     frame->editor()->undo();
1039     return true;
1040 }
1041
1042 static bool executeUnlink(Frame* frame, Event*, EditorCommandSource, const String&)
1043 {
1044     applyCommand(UnlinkCommand::create(frame->document()));
1045     return true;
1046 }
1047
1048 static bool executeUnscript(Frame* frame, Event*, EditorCommandSource source, const String&)
1049 {
1050     return executeApplyStyle(frame, source, EditActionUnscript, CSSPropertyVerticalAlign, "baseline");
1051 }
1052
1053 static bool executeUnselect(Frame* frame, Event*, EditorCommandSource, const String&)
1054 {
1055     frame->selection()->clear();
1056     return true;
1057 }
1058
1059 static bool executeYank(Frame* frame, Event*, EditorCommandSource, const String&)
1060 {
1061     frame->editor()->insertTextWithoutSendingTextEvent(frame->editor()->killRing()->yank(), false, 0);
1062     frame->editor()->killRing()->setToYankedState();
1063     return true;
1064 }
1065
1066 static bool executeYankAndSelect(Frame* frame, Event*, EditorCommandSource, const String&)
1067 {
1068     frame->editor()->insertTextWithoutSendingTextEvent(frame->editor()->killRing()->yank(), true, 0);
1069     frame->editor()->killRing()->setToYankedState();
1070     return true;
1071 }
1072
1073 #if SUPPORT_AUTOCORRECTION_PANEL
1074 static bool executeCancelOperation(Frame* frame, Event*, EditorCommandSource, const String&)
1075 {
1076     frame->editor()->handleCancelOperation();
1077     return true;
1078 }
1079 #endif
1080
1081 // Supported functions
1082
1083 static bool supported(Frame*, EditorCommandSource)
1084 {
1085     return true;
1086 }
1087
1088 static bool supportedFromMenuOrKeyBinding(Frame*, EditorCommandSource source)
1089 {
1090     return source == CommandFromMenuOrKeyBinding;
1091 }
1092
1093 static bool supportedCopyCut(Frame* frame, EditorCommandSource source)
1094 {
1095     switch (source) {
1096     case CommandFromMenuOrKeyBinding:
1097         return true;
1098     case CommandFromDOM:
1099     case CommandFromDOMWithUserInterface: {
1100         Settings* settings = frame ? frame->settings() : 0;
1101         return settings && settings->javaScriptCanAccessClipboard();
1102     }
1103     }
1104     ASSERT_NOT_REACHED();
1105     return false;
1106 }
1107
1108 static bool supportedPaste(Frame* frame, EditorCommandSource source)
1109 {
1110     switch (source) {
1111     case CommandFromMenuOrKeyBinding:
1112         return true;
1113     case CommandFromDOM:
1114     case CommandFromDOMWithUserInterface: {
1115         Settings* settings = frame ? frame->settings() : 0;
1116         return settings && (settings->javaScriptCanAccessClipboard() ? settings->isDOMPasteAllowed() : 0);
1117     }
1118     }
1119     ASSERT_NOT_REACHED();
1120     return false;
1121 }
1122
1123 #if SUPPORT_AUTOCORRECTION_PANEL
1124 static bool supportedDismissCorrectionPanel(Frame* frame, EditorCommandSource source)
1125 {
1126     return supportedFromMenuOrKeyBinding(frame, source) && frame->editor()->isShowingCorrectionPanel();
1127 }
1128 #endif
1129
1130 // Enabled functions
1131
1132 static bool enabled(Frame*, Event*, EditorCommandSource)
1133 {
1134     return true;
1135 }
1136
1137 static bool enabledVisibleSelection(Frame* frame, Event* event, EditorCommandSource)
1138 {
1139     // The term "visible" here includes a caret in editable text or a range in any text.
1140     const VisibleSelection& selection = frame->editor()->selectionForCommand(event);
1141     return (selection.isCaret() && selection.isContentEditable()) || selection.isRange();
1142 }
1143
1144 static bool caretBrowsingEnabled(Frame* frame)
1145 {
1146     return frame->settings() && frame->settings()->caretBrowsingEnabled();
1147 }
1148
1149 static EditorCommandSource dummyEditorCommandSource = static_cast<EditorCommandSource>(0);
1150
1151 static bool enabledVisibleSelectionOrCaretBrowsing(Frame* frame, Event* event, EditorCommandSource)
1152 {
1153     // The EditorCommandSource parameter is unused in enabledVisibleSelection, so just pass a dummy variable
1154     return caretBrowsingEnabled(frame) || enabledVisibleSelection(frame, event, dummyEditorCommandSource);
1155 }
1156
1157 static bool enabledVisibleSelectionAndMark(Frame* frame, Event* event, EditorCommandSource)
1158 {
1159     const VisibleSelection& selection = frame->editor()->selectionForCommand(event);
1160     return ((selection.isCaret() && selection.isContentEditable()) || selection.isRange())
1161         && frame->editor()->mark().isCaretOrRange();
1162 }
1163
1164 static bool enableCaretInEditableText(Frame* frame, Event* event, EditorCommandSource)
1165 {
1166     const VisibleSelection& selection = frame->editor()->selectionForCommand(event);
1167     return selection.isCaret() && selection.isContentEditable();
1168 }
1169
1170 static bool enabledCopy(Frame* frame, Event*, EditorCommandSource)
1171 {
1172     return frame->editor()->canDHTMLCopy() || frame->editor()->canCopy();
1173 }
1174
1175 static bool enabledCut(Frame* frame, Event*, EditorCommandSource)
1176 {
1177     return frame->editor()->canDHTMLCut() || frame->editor()->canCut();
1178 }
1179
1180 static bool enabledDelete(Frame* frame, Event* event, EditorCommandSource source)
1181 {
1182     switch (source) {
1183     case CommandFromMenuOrKeyBinding:
1184         // "Delete" from menu only affects selected range, just like Cut but without affecting pasteboard
1185         return frame->editor()->canDHTMLCut() || frame->editor()->canCut();
1186     case CommandFromDOM:
1187     case CommandFromDOMWithUserInterface:
1188         // "Delete" from DOM is like delete/backspace keypress, affects selected range if non-empty,
1189         // otherwise removes a character
1190         return frame->editor()->selectionForCommand(event).isContentEditable();
1191     }
1192     ASSERT_NOT_REACHED();
1193     return false;
1194 }
1195
1196 static bool enabledInEditableText(Frame* frame, Event* event, EditorCommandSource)
1197 {
1198     return frame->editor()->selectionForCommand(event).isContentEditable();
1199 }
1200
1201 static bool enabledInEditableTextOrCaretBrowsing(Frame* frame, Event* event, EditorCommandSource)
1202 {
1203     // The EditorCommandSource parameter is unused in enabledInEditableText, so just pass a dummy variable
1204     return caretBrowsingEnabled(frame) || enabledInEditableText(frame, event, dummyEditorCommandSource);
1205 }
1206
1207 static bool enabledInRichlyEditableText(Frame* frame, Event*, EditorCommandSource)
1208 {
1209     return frame->selection()->isCaretOrRange() && frame->selection()->isContentRichlyEditable();
1210 }
1211
1212 static bool enabledPaste(Frame* frame, Event*, EditorCommandSource)
1213 {
1214     return frame->editor()->canPaste();
1215 }
1216
1217 static bool enabledRangeInEditableText(Frame* frame, Event*, EditorCommandSource)
1218 {
1219     return frame->selection()->isRange() && frame->selection()->isContentEditable();
1220 }
1221
1222 static bool enabledRangeInRichlyEditableText(Frame* frame, Event*, EditorCommandSource)
1223 {
1224     return frame->selection()->isRange() && frame->selection()->isContentRichlyEditable();
1225 }
1226
1227 static bool enabledRedo(Frame* frame, Event*, EditorCommandSource)
1228 {
1229     return frame->editor()->canRedo();
1230 }
1231
1232 static bool enabledUndo(Frame* frame, Event*, EditorCommandSource)
1233 {
1234     return frame->editor()->canUndo();
1235 }
1236
1237 // State functions
1238
1239 static TriState stateNone(Frame*, Event*)
1240 {
1241     return FalseTriState;
1242 }
1243
1244 static TriState stateBold(Frame* frame, Event*)
1245 {
1246     return stateStyle(frame, CSSPropertyFontWeight, "bold");
1247 }
1248
1249 static TriState stateItalic(Frame* frame, Event*)
1250 {
1251     return stateStyle(frame, CSSPropertyFontStyle, "italic");
1252 }
1253
1254 static TriState stateOrderedList(Frame* frame, Event*)
1255 {
1256     return frame->editor()->selectionOrderedListState();
1257 }
1258
1259 static TriState stateStrikethrough(Frame* frame, Event*)
1260 {
1261     return stateStyle(frame, CSSPropertyWebkitTextDecorationsInEffect, "line-through");
1262 }
1263
1264 static TriState stateStyleWithCSS(Frame* frame, Event*)
1265 {
1266     return frame->editor()->shouldStyleWithCSS() ? TrueTriState : FalseTriState;
1267 }
1268
1269 static TriState stateSubscript(Frame* frame, Event*)
1270 {
1271     return stateStyle(frame, CSSPropertyVerticalAlign, "sub");
1272 }
1273
1274 static TriState stateSuperscript(Frame* frame, Event*)
1275 {
1276     return stateStyle(frame, CSSPropertyVerticalAlign, "super");
1277 }
1278
1279 static TriState stateTextWritingDirectionLeftToRight(Frame* frame, Event*)
1280 {
1281     return stateTextWritingDirection(frame, LeftToRightWritingDirection);
1282 }
1283
1284 static TriState stateTextWritingDirectionNatural(Frame* frame, Event*)
1285 {
1286     return stateTextWritingDirection(frame, NaturalWritingDirection);
1287 }
1288
1289 static TriState stateTextWritingDirectionRightToLeft(Frame* frame, Event*)
1290 {
1291     return stateTextWritingDirection(frame, RightToLeftWritingDirection);
1292 }
1293
1294 static TriState stateUnderline(Frame* frame, Event*)
1295 {
1296     return stateStyle(frame, CSSPropertyWebkitTextDecorationsInEffect, "underline");
1297 }
1298
1299 static TriState stateUnorderedList(Frame* frame, Event*)
1300 {
1301     return frame->editor()->selectionUnorderedListState();
1302 }
1303
1304 static TriState stateJustifyCenter(Frame* frame, Event*)
1305 {
1306     return stateStyle(frame, CSSPropertyTextAlign, "center");
1307 }
1308
1309 static TriState stateJustifyFull(Frame* frame, Event*)
1310 {
1311     return stateStyle(frame, CSSPropertyTextAlign, "justify");
1312 }
1313
1314 static TriState stateJustifyLeft(Frame* frame, Event*)
1315 {
1316     return stateStyle(frame, CSSPropertyTextAlign, "left");
1317 }
1318
1319 static TriState stateJustifyRight(Frame* frame, Event*)
1320 {
1321     return stateStyle(frame, CSSPropertyTextAlign, "right");
1322 }
1323
1324 // Value functions
1325
1326 static String valueNull(Frame*, Event*)
1327 {
1328     return String();
1329 }
1330
1331 static String valueBackColor(Frame* frame, Event*)
1332 {
1333     return valueStyle(frame, CSSPropertyBackgroundColor);
1334 }
1335
1336 static String valueFontName(Frame* frame, Event*)
1337 {
1338     return valueStyle(frame, CSSPropertyFontFamily);
1339 }
1340
1341 static String valueFontSize(Frame* frame, Event*)
1342 {
1343     return valueStyle(frame, CSSPropertyFontSize);
1344 }
1345
1346 static String valueFontSizeDelta(Frame* frame, Event*)
1347 {
1348     return valueStyle(frame, CSSPropertyWebkitFontSizeDelta);
1349 }
1350
1351 static String valueForeColor(Frame* frame, Event*)
1352 {
1353     return valueStyle(frame, CSSPropertyColor);
1354 }
1355
1356 static String valueFormatBlock(Frame* frame, Event*)
1357 {
1358     const VisibleSelection& selection = frame->selection()->selection();
1359     if (!selection.isNonOrphanedCaretOrRange() || !selection.isContentEditable())
1360         return "";
1361     Element* formatBlockElement = FormatBlockCommand::elementForFormatBlockCommand(selection.firstRange().get());
1362     if (!formatBlockElement)
1363         return "";
1364     return formatBlockElement->localName();
1365 }
1366
1367 // Map of functions
1368
1369 struct CommandEntry {
1370     const char* name;
1371     EditorInternalCommand command;
1372 };
1373
1374 static const CommandMap& createCommandMap()
1375 {
1376     static const CommandEntry commands[] = {
1377         { "AlignCenter", { executeJustifyCenter, supportedFromMenuOrKeyBinding, enabledInRichlyEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1378         { "AlignJustified", { executeJustifyFull, supportedFromMenuOrKeyBinding, enabledInRichlyEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1379         { "AlignLeft", { executeJustifyLeft, supportedFromMenuOrKeyBinding, enabledInRichlyEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1380         { "AlignRight", { executeJustifyRight, supportedFromMenuOrKeyBinding, enabledInRichlyEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1381         { "BackColor", { executeBackColor, supported, enabledInRichlyEditableText, stateNone, valueBackColor, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1382         { "BackwardDelete", { executeDeleteBackward, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } }, // FIXME: remove BackwardDelete when Safari for Windows stops using it.
1383         { "Bold", { executeToggleBold, supported, enabledInRichlyEditableText, stateBold, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1384         { "Copy", { executeCopy, supportedCopyCut, enabledCopy, stateNone, valueNull, notTextInsertion, allowExecutionWhenDisabled } },
1385         { "CreateLink", { executeCreateLink, supported, enabledInRichlyEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1386         { "Cut", { executeCut, supportedCopyCut, enabledCut, stateNone, valueNull, notTextInsertion, allowExecutionWhenDisabled } },
1387         { "Delete", { executeDelete, supported, enabledDelete, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1388         { "DeleteBackward", { executeDeleteBackward, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1389         { "DeleteBackwardByDecomposingPreviousCharacter", { executeDeleteBackwardByDecomposingPreviousCharacter, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1390         { "DeleteForward", { executeDeleteForward, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1391         { "DeleteToBeginningOfLine", { executeDeleteToBeginningOfLine, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1392         { "DeleteToBeginningOfParagraph", { executeDeleteToBeginningOfParagraph, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1393         { "DeleteToEndOfLine", { executeDeleteToEndOfLine, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1394         { "DeleteToEndOfParagraph", { executeDeleteToEndOfParagraph, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1395         { "DeleteToMark", { executeDeleteToMark, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1396         { "DeleteWordBackward", { executeDeleteWordBackward, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1397         { "DeleteWordForward", { executeDeleteWordForward, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1398         { "FindString", { executeFindString, supported, enabled, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1399         { "FontName", { executeFontName, supported, enabledInEditableText, stateNone, valueFontName, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1400         { "FontSize", { executeFontSize, supported, enabledInEditableText, stateNone, valueFontSize, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1401         { "FontSizeDelta", { executeFontSizeDelta, supported, enabledInEditableText, stateNone, valueFontSizeDelta, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1402         { "ForeColor", { executeForeColor, supported, enabledInRichlyEditableText, stateNone, valueForeColor, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1403         { "FormatBlock", { executeFormatBlock, supported, enabledInRichlyEditableText, stateNone, valueFormatBlock, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1404         { "ForwardDelete", { executeForwardDelete, supported, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1405         { "HiliteColor", { executeBackColor, supported, enabledInRichlyEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1406         { "IgnoreSpelling", { executeIgnoreSpelling, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1407         { "Indent", { executeIndent, supported, enabledInRichlyEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1408         { "InsertBacktab", { executeInsertBacktab, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, isTextInsertion, doNotAllowExecutionWhenDisabled } },
1409         { "InsertHTML", { executeInsertHTML, supported, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1410         { "InsertHorizontalRule", { executeInsertHorizontalRule, supported, enabledInRichlyEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1411         { "InsertImage", { executeInsertImage, supported, enabledInRichlyEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1412         { "InsertLineBreak", { executeInsertLineBreak, supported, enabledInEditableText, stateNone, valueNull, isTextInsertion, doNotAllowExecutionWhenDisabled } },
1413         { "InsertNewline", { executeInsertNewline, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, isTextInsertion, doNotAllowExecutionWhenDisabled } },    
1414         { "InsertNewlineInQuotedContent", { executeInsertNewlineInQuotedContent, supported, enabledInRichlyEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1415         { "InsertOrderedList", { executeInsertOrderedList, supported, enabledInRichlyEditableText, stateOrderedList, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1416         { "InsertParagraph", { executeInsertParagraph, supported, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1417         { "InsertTab", { executeInsertTab, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, isTextInsertion, doNotAllowExecutionWhenDisabled } },
1418         { "InsertText", { executeInsertText, supported, enabledInEditableText, stateNone, valueNull, isTextInsertion, doNotAllowExecutionWhenDisabled } },
1419         { "InsertUnorderedList", { executeInsertUnorderedList, supported, enabledInRichlyEditableText, stateUnorderedList, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1420         { "Italic", { executeToggleItalic, supported, enabledInRichlyEditableText, stateItalic, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1421         { "JustifyCenter", { executeJustifyCenter, supported, enabledInRichlyEditableText, stateJustifyCenter, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1422         { "JustifyFull", { executeJustifyFull, supported, enabledInRichlyEditableText, stateJustifyFull, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1423         { "JustifyLeft", { executeJustifyLeft, supported, enabledInRichlyEditableText, stateJustifyLeft, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1424         { "JustifyNone", { executeJustifyLeft, supported, enabledInRichlyEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1425         { "JustifyRight", { executeJustifyRight, supported, enabledInRichlyEditableText, stateJustifyRight, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1426         { "MakeTextWritingDirectionLeftToRight", { executeMakeTextWritingDirectionLeftToRight, supportedFromMenuOrKeyBinding, enabledInRichlyEditableText, stateTextWritingDirectionLeftToRight, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1427         { "MakeTextWritingDirectionNatural", { executeMakeTextWritingDirectionNatural, supportedFromMenuOrKeyBinding, enabledInRichlyEditableText, stateTextWritingDirectionNatural, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1428         { "MakeTextWritingDirectionRightToLeft", { executeMakeTextWritingDirectionRightToLeft, supportedFromMenuOrKeyBinding, enabledInRichlyEditableText, stateTextWritingDirectionRightToLeft, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1429         { "MoveBackward", { executeMoveBackward, supportedFromMenuOrKeyBinding, enabledInEditableTextOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1430         { "MoveBackwardAndModifySelection", { executeMoveBackwardAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelectionOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1431         { "MoveDown", { executeMoveDown, supportedFromMenuOrKeyBinding, enabledInEditableTextOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1432         { "MoveDownAndModifySelection", { executeMoveDownAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelectionOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1433         { "MoveForward", { executeMoveForward, supportedFromMenuOrKeyBinding, enabledInEditableTextOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1434         { "MoveForwardAndModifySelection", { executeMoveForwardAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelectionOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1435         { "MoveLeft", { executeMoveLeft, supportedFromMenuOrKeyBinding, enabledInEditableTextOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1436         { "MoveLeftAndModifySelection", { executeMoveLeftAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelectionOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1437         { "MovePageDown", { executeMovePageDown, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1438         { "MovePageDownAndModifySelection", { executeMovePageDownAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelection, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1439         { "MovePageUp", { executeMovePageUp, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1440         { "MovePageUpAndModifySelection", { executeMovePageUpAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelection, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1441         { "MoveParagraphBackwardAndModifySelection", { executeMoveParagraphBackwardAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelectionOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1442         { "MoveParagraphForwardAndModifySelection", { executeMoveParagraphForwardAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelectionOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1443         { "MoveRight", { executeMoveRight, supportedFromMenuOrKeyBinding, enabledInEditableTextOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1444         { "MoveRightAndModifySelection", { executeMoveRightAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelectionOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1445         { "MoveToBeginningOfDocument", { executeMoveToBeginningOfDocument, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1446         { "MoveToBeginningOfDocumentAndModifySelection", { executeMoveToBeginningOfDocumentAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelection, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1447         { "MoveToBeginningOfLine", { executeMoveToBeginningOfLine, supportedFromMenuOrKeyBinding, enabledInEditableTextOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1448         { "MoveToBeginningOfLineAndModifySelection", { executeMoveToBeginningOfLineAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelectionOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1449         { "MoveToBeginningOfParagraph", { executeMoveToBeginningOfParagraph, supportedFromMenuOrKeyBinding, enabledInEditableTextOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1450         { "MoveToBeginningOfParagraphAndModifySelection", { executeMoveToBeginningOfParagraphAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelectionOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1451         { "MoveToBeginningOfSentence", { executeMoveToBeginningOfSentence, supportedFromMenuOrKeyBinding, enabledInEditableTextOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1452         { "MoveToBeginningOfSentenceAndModifySelection", { executeMoveToBeginningOfSentenceAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelectionOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1453         { "MoveToEndOfDocument", { executeMoveToEndOfDocument, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1454         { "MoveToEndOfDocumentAndModifySelection", { executeMoveToEndOfDocumentAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelection, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1455         { "MoveToEndOfLine", { executeMoveToEndOfLine, supportedFromMenuOrKeyBinding, enabledInEditableTextOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1456         { "MoveToEndOfLineAndModifySelection", { executeMoveToEndOfLineAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelectionOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1457         { "MoveToEndOfParagraph", { executeMoveToEndOfParagraph, supportedFromMenuOrKeyBinding, enabledInEditableTextOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1458         { "MoveToEndOfParagraphAndModifySelection", { executeMoveToEndOfParagraphAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelectionOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1459         { "MoveToEndOfSentence", { executeMoveToEndOfSentence, supportedFromMenuOrKeyBinding, enabledInEditableTextOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1460         { "MoveToEndOfSentenceAndModifySelection", { executeMoveToEndOfSentenceAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelectionOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1461         { "MoveToLeftEndOfLine", { executeMoveToLeftEndOfLine, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1462         { "MoveToLeftEndOfLineAndModifySelection", { executeMoveToLeftEndOfLineAndModifySelection, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1463         { "MoveToRightEndOfLine", { executeMoveToRightEndOfLine, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1464         { "MoveToRightEndOfLineAndModifySelection", { executeMoveToRightEndOfLineAndModifySelection, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1465         { "MoveUp", { executeMoveUp, supportedFromMenuOrKeyBinding, enabledInEditableTextOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1466         { "MoveUpAndModifySelection", { executeMoveUpAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelectionOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1467         { "MoveWordBackward", { executeMoveWordBackward, supportedFromMenuOrKeyBinding, enabledInEditableTextOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1468         { "MoveWordBackwardAndModifySelection", { executeMoveWordBackwardAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelectionOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1469         { "MoveWordForward", { executeMoveWordForward, supportedFromMenuOrKeyBinding, enabledInEditableTextOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1470         { "MoveWordForwardAndModifySelection", { executeMoveWordForwardAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelectionOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1471         { "MoveWordLeft", { executeMoveWordLeft, supportedFromMenuOrKeyBinding, enabledInEditableTextOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1472         { "MoveWordLeftAndModifySelection", { executeMoveWordLeftAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelectionOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1473         { "MoveWordRight", { executeMoveWordRight, supportedFromMenuOrKeyBinding, enabledInEditableTextOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1474         { "MoveWordRightAndModifySelection", { executeMoveWordRightAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelectionOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1475         { "Outdent", { executeOutdent, supported, enabledInRichlyEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1476         { "Paste", { executePaste, supportedPaste, enabledPaste, stateNone, valueNull, notTextInsertion, allowExecutionWhenDisabled } },
1477         { "PasteAndMatchStyle", { executePasteAndMatchStyle, supportedPaste, enabledPaste, stateNone, valueNull, notTextInsertion, allowExecutionWhenDisabled } },
1478         { "PasteAsPlainText", { executePasteAsPlainText, supportedPaste, enabledPaste, stateNone, valueNull, notTextInsertion, allowExecutionWhenDisabled } },
1479         { "Print", { executePrint, supported, enabled, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1480         { "Redo", { executeRedo, supported, enabledRedo, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1481         { "RemoveFormat", { executeRemoveFormat, supported, enabledRangeInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1482         { "SelectAll", { executeSelectAll, supported, enabled, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1483         { "SelectLine", { executeSelectLine, supportedFromMenuOrKeyBinding, enabledVisibleSelection, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1484         { "SelectParagraph", { executeSelectParagraph, supportedFromMenuOrKeyBinding, enabledVisibleSelection, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1485         { "SelectSentence", { executeSelectSentence, supportedFromMenuOrKeyBinding, enabledVisibleSelection, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1486         { "SelectToMark", { executeSelectToMark, supportedFromMenuOrKeyBinding, enabledVisibleSelectionAndMark, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1487         { "SelectWord", { executeSelectWord, supportedFromMenuOrKeyBinding, enabledVisibleSelection, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1488         { "SetMark", { executeSetMark, supportedFromMenuOrKeyBinding, enabledVisibleSelection, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1489         { "Strikethrough", { executeStrikethrough, supported, enabledInRichlyEditableText, stateStrikethrough, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1490         { "StyleWithCSS", { executeStyleWithCSS, supported, enabledInRichlyEditableText, stateStyleWithCSS, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1491         { "Subscript", { executeSubscript, supported, enabledInRichlyEditableText, stateSubscript, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1492         { "Superscript", { executeSuperscript, supported, enabledInRichlyEditableText, stateSuperscript, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1493         { "SwapWithMark", { executeSwapWithMark, supportedFromMenuOrKeyBinding, enabledVisibleSelectionAndMark, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1494         { "ToggleBold", { executeToggleBold, supportedFromMenuOrKeyBinding, enabledInRichlyEditableText, stateBold, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1495         { "ToggleItalic", { executeToggleItalic, supportedFromMenuOrKeyBinding, enabledInRichlyEditableText, stateItalic, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1496         { "ToggleUnderline", { executeUnderline, supportedFromMenuOrKeyBinding, enabledInRichlyEditableText, stateUnderline, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1497         { "Transpose", { executeTranspose, supported, enableCaretInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1498         { "Underline", { executeUnderline, supported, enabledInRichlyEditableText, stateUnderline, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1499         { "Undo", { executeUndo, supported, enabledUndo, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1500         { "Unlink", { executeUnlink, supported, enabledRangeInRichlyEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1501         { "Unscript", { executeUnscript, supportedFromMenuOrKeyBinding, enabledInRichlyEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1502         { "Unselect", { executeUnselect, supported, enabledVisibleSelection, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1503         { "Yank", { executeYank, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1504         { "YankAndSelect", { executeYankAndSelect, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1505 #if SUPPORT_AUTOCORRECTION_PANEL
1506         { "CancelOperation", { executeCancelOperation, supportedDismissCorrectionPanel, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1507 #endif
1508     };
1509
1510     // These unsupported commands are listed here since they appear in the Microsoft
1511     // documentation used as the starting point for our DOM executeCommand support.
1512     //
1513     // 2D-Position (not supported)
1514     // AbsolutePosition (not supported)
1515     // BlockDirLTR (not supported)
1516     // BlockDirRTL (not supported)
1517     // BrowseMode (not supported)
1518     // ClearAuthenticationCache (not supported)
1519     // CreateBookmark (not supported)
1520     // DirLTR (not supported)
1521     // DirRTL (not supported)
1522     // EditMode (not supported)
1523     // InlineDirLTR (not supported)
1524     // InlineDirRTL (not supported)
1525     // InsertButton (not supported)
1526     // InsertFieldSet (not supported)
1527     // InsertIFrame (not supported)
1528     // InsertInputButton (not supported)
1529     // InsertInputCheckbox (not supported)
1530     // InsertInputFileUpload (not supported)
1531     // InsertInputHidden (not supported)
1532     // InsertInputImage (not supported)
1533     // InsertInputPassword (not supported)
1534     // InsertInputRadio (not supported)
1535     // InsertInputReset (not supported)
1536     // InsertInputSubmit (not supported)
1537     // InsertInputText (not supported)
1538     // InsertMarquee (not supported)
1539     // InsertSelectDropDown (not supported)
1540     // InsertSelectListBox (not supported)
1541     // InsertTextArea (not supported)
1542     // LiveResize (not supported)
1543     // MultipleSelection (not supported)
1544     // Open (not supported)
1545     // Overwrite (not supported)
1546     // PlayImage (not supported)
1547     // Refresh (not supported)
1548     // RemoveParaFormat (not supported)
1549     // SaveAs (not supported)
1550     // SizeToControl (not supported)
1551     // SizeToControlHeight (not supported)
1552     // SizeToControlWidth (not supported)
1553     // Stop (not supported)
1554     // StopImage (not supported)
1555     // Unbookmark (not supported)
1556
1557     CommandMap& commandMap = *new CommandMap;
1558
1559     const unsigned numCommands = sizeof(commands) / sizeof(commands[0]);
1560     for (unsigned i = 0; i < numCommands; i++) {
1561         ASSERT(!commandMap.get(commands[i].name));
1562         commandMap.set(commands[i].name, &commands[i].command);
1563     }
1564
1565     return commandMap;
1566 }
1567
1568 Editor::Command Editor::command(const String& commandName)
1569 {
1570     return command(commandName, CommandFromMenuOrKeyBinding);
1571 }
1572
1573 Editor::Command Editor::command(const String& commandName, EditorCommandSource source)
1574 {
1575     if (commandName.isEmpty())
1576         return Command();
1577
1578     static const CommandMap& commandMap = createCommandMap();
1579     const EditorInternalCommand* internalCommand = commandMap.get(commandName);
1580     return internalCommand ? Command(m_frame, internalCommand, source) : Command();
1581 }
1582
1583 Editor::Command::Command()
1584     : m_command(0)
1585     , m_source()
1586 {
1587 }
1588
1589 Editor::Command::Command(PassRefPtr<Frame> frame, const EditorInternalCommand* command, EditorCommandSource source)
1590     : m_frame(frame)
1591     , m_command(command)
1592     , m_source(source)
1593 {
1594     ASSERT(m_frame);
1595     ASSERT(m_command);
1596 }
1597
1598 bool Editor::Command::execute(const String& parameter, Event* triggeringEvent) const
1599 {
1600     if (!isEnabled(triggeringEvent)) {
1601         // Let certain commands be executed when performed explicitly even if they are disabled.
1602         if (!isSupported() || !m_frame || !m_command->allowExecutionWhenDisabled)
1603             return false;
1604     }
1605     m_frame->document()->updateLayoutIgnorePendingStylesheets();
1606     return m_command->execute(m_frame.get(), triggeringEvent, m_source, parameter);
1607 }
1608
1609 bool Editor::Command::execute(Event* triggeringEvent) const
1610 {
1611     return execute(String(), triggeringEvent);
1612 }
1613
1614 bool Editor::Command::isSupported() const
1615 {
1616     return m_command && m_command->isSupported(m_frame.get(), m_source);
1617 }
1618
1619 bool Editor::Command::isEnabled(Event* triggeringEvent) const
1620 {
1621     if (!isSupported() || !m_frame)
1622         return false;
1623     return m_command->isEnabled(m_frame.get(), triggeringEvent, m_source);
1624 }
1625
1626 TriState Editor::Command::state(Event* triggeringEvent) const
1627 {
1628     if (!isSupported() || !m_frame)
1629         return FalseTriState;
1630     return m_command->state(m_frame.get(), triggeringEvent);
1631 }
1632
1633 String Editor::Command::value(Event* triggeringEvent) const
1634 {
1635     if (!isSupported() || !m_frame)
1636         return String();
1637     if (m_command->value == valueNull && m_command->state != stateNone)
1638         return m_command->state(m_frame.get(), triggeringEvent) == TrueTriState ? "true" : "false";
1639     return m_command->value(m_frame.get(), triggeringEvent);
1640 }
1641
1642 bool Editor::Command::isTextInsertion() const
1643 {
1644     return m_command && m_command->isTextInsertion;
1645 }
1646
1647 } // namespace WebCore