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