a22196c9b93bf39ae74f32d5db582485f9804b79
[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 executeOutdent(Frame* frame, Event*, EditorCommandSource, const String&)
831 {
832     applyCommand(IndentOutdentCommand::create(frame->document(), IndentOutdentCommand::Outdent));
833     return true;
834 }
835
836 static bool executePaste(Frame* frame, Event*, EditorCommandSource, const String&)
837 {
838     frame->editor()->paste();
839     return true;
840 }
841
842 static bool executePasteAndMatchStyle(Frame* frame, Event*, EditorCommandSource, const String&)
843 {
844     frame->editor()->pasteAsPlainText();
845     return true;
846 }
847
848 static bool executePrint(Frame* frame, Event*, EditorCommandSource, const String&)
849 {
850     Page* page = frame->page();
851     if (!page)
852         return false;
853     page->chrome()->print(frame);
854     return true;
855 }
856
857 static bool executeRedo(Frame* frame, Event*, EditorCommandSource, const String&)
858 {
859     frame->editor()->redo();
860     return true;
861 }
862
863 static bool executeRemoveFormat(Frame* frame, Event*, EditorCommandSource, const String&)
864 {
865     frame->editor()->removeFormattingAndStyle();
866     return true;
867 }
868
869 static bool executeSelectAll(Frame* frame, Event*, EditorCommandSource, const String&)
870 {
871     frame->selection()->selectAll();
872     return true;
873 }
874
875 static bool executeSelectLine(Frame* frame, Event*, EditorCommandSource, const String&)
876 {
877     return expandSelectionToGranularity(frame, LineGranularity);
878 }
879
880 static bool executeSelectParagraph(Frame* frame, Event*, EditorCommandSource, const String&)
881 {
882     return expandSelectionToGranularity(frame, ParagraphGranularity);
883 }
884
885 static bool executeSelectSentence(Frame* frame, Event*, EditorCommandSource, const String&)
886 {
887     return expandSelectionToGranularity(frame, SentenceGranularity);
888 }
889
890 static bool executeSelectToMark(Frame* frame, Event*, EditorCommandSource, const String&)
891 {
892     RefPtr<Range> mark = frame->mark().toNormalizedRange();
893     RefPtr<Range> selection = frame->editor()->selectedRange();
894     if (!mark || !selection) {
895         systemBeep();
896         return false;
897     }
898     frame->selection()->setSelectedRange(unionDOMRanges(mark.get(), selection.get()).get(), DOWNSTREAM, true);
899     return true;
900 }
901
902 static bool executeSelectWord(Frame* frame, Event*, EditorCommandSource, const String&)
903 {
904     return expandSelectionToGranularity(frame, WordGranularity);
905 }
906
907 static bool executeSetMark(Frame* frame, Event*, EditorCommandSource, const String&)
908 {
909     frame->setMark(frame->selection()->selection());
910     return true;
911 }
912
913 static bool executeStrikethrough(Frame* frame, Event*, EditorCommandSource source, const String&)
914 {
915     return executeToggleStyle(frame, source, EditActionChangeAttributes, CSSPropertyWebkitTextDecorationsInEffect, "none", "line-through");
916 }
917
918 static bool executeStyleWithCSS(Frame* frame, Event*, EditorCommandSource, const String& value)
919 {
920     if (value != "false" && value != "true")
921         return false;
922     
923     frame->editor()->setShouldStyleWithCSS(value == "true" ? true : false);
924     return true;
925 }
926
927 static bool executeSubscript(Frame* frame, Event*, EditorCommandSource source, const String&)
928 {
929     return executeToggleStyle(frame, source, EditActionSubscript, CSSPropertyVerticalAlign, "baseline", "sub");
930 }
931
932 static bool executeSuperscript(Frame* frame, Event*, EditorCommandSource source, const String&)
933 {
934     return executeToggleStyle(frame, source, EditActionSuperscript, CSSPropertyVerticalAlign, "baseline", "super");
935 }
936
937 static bool executeSwapWithMark(Frame* frame, Event*, EditorCommandSource, const String&)
938 {
939     const VisibleSelection& mark = frame->mark();
940     const VisibleSelection& selection = frame->selection()->selection();
941     if (mark.isNone() || selection.isNone()) {
942         systemBeep();
943         return false;
944     }
945     frame->selection()->setSelection(mark);
946     frame->setMark(selection);
947     return true;
948 }
949
950 static bool executeToggleBold(Frame* frame, Event*, EditorCommandSource source, const String&)
951 {
952     return executeToggleStyle(frame, source, EditActionChangeAttributes, CSSPropertyFontWeight, "normal", "bold");
953 }
954
955 static bool executeToggleItalic(Frame* frame, Event*, EditorCommandSource source, const String&)
956 {
957     return executeToggleStyle(frame, source, EditActionChangeAttributes, CSSPropertyFontStyle, "normal", "italic");
958 }
959
960 static bool executeTranspose(Frame* frame, Event*, EditorCommandSource, const String&)
961 {
962     frame->editor()->transpose();
963     return true;
964 }
965
966 static bool executeUnderline(Frame* frame, Event*, EditorCommandSource source, const String&)
967 {
968     // FIXME: This currently clears overline, line-through, and blink as an unwanted side effect.
969     return executeToggleStyle(frame, source, EditActionUnderline, CSSPropertyWebkitTextDecorationsInEffect, "none", "underline");
970 }
971
972 static bool executeUndo(Frame* frame, Event*, EditorCommandSource, const String&)
973 {
974     frame->editor()->undo();
975     return true;
976 }
977
978 static bool executeUnlink(Frame* frame, Event*, EditorCommandSource, const String&)
979 {
980     applyCommand(UnlinkCommand::create(frame->document()));
981     return true;
982 }
983
984 static bool executeUnscript(Frame* frame, Event*, EditorCommandSource source, const String&)
985 {
986     return executeApplyStyle(frame, source, EditActionUnscript, CSSPropertyVerticalAlign, "baseline");
987 }
988
989 static bool executeUnselect(Frame* frame, Event*, EditorCommandSource, const String&)
990 {
991     frame->selection()->clear();
992     return true;
993 }
994
995 static bool executeYank(Frame* frame, Event*, EditorCommandSource, const String&)
996 {
997     frame->editor()->insertTextWithoutSendingTextEvent(frame->editor()->yankFromKillRing(), false, 0);
998     frame->editor()->setKillRingToYankedState();
999     return true;
1000 }
1001
1002 static bool executeYankAndSelect(Frame* frame, Event*, EditorCommandSource, const String&)
1003 {
1004     frame->editor()->insertTextWithoutSendingTextEvent(frame->editor()->yankFromKillRing(), true, 0);
1005     frame->editor()->setKillRingToYankedState();
1006     return true;
1007 }
1008
1009 // Supported functions
1010
1011 static bool supported(Frame*, EditorCommandSource)
1012 {
1013     return true;
1014 }
1015
1016 static bool supportedFromMenuOrKeyBinding(Frame*, EditorCommandSource source)
1017 {
1018     return source == CommandFromMenuOrKeyBinding;
1019 }
1020
1021 static bool supportedPaste(Frame* frame, EditorCommandSource source)
1022 {
1023     switch (source) {
1024         case CommandFromMenuOrKeyBinding:
1025             return true;
1026         case CommandFromDOM:
1027         case CommandFromDOMWithUserInterface: {
1028             Settings* settings = frame ? frame->settings() : 0;
1029             return settings && settings->isDOMPasteAllowed();
1030         }
1031     }
1032     ASSERT_NOT_REACHED();
1033     return false;
1034 }
1035
1036 // Enabled functions
1037
1038 static bool enabled(Frame*, Event*, EditorCommandSource)
1039 {
1040     return true;
1041 }
1042
1043 static bool enabledVisibleSelection(Frame* frame, Event* event, EditorCommandSource)
1044 {
1045     // The term "visible" here includes a caret in editable text or a range in any text.
1046     const VisibleSelection& selection = frame->editor()->selectionForCommand(event);
1047     return (selection.isCaret() && selection.isContentEditable()) || selection.isRange();
1048 }
1049
1050 static bool enabledVisibleSelectionAndMark(Frame* frame, Event* event, EditorCommandSource)
1051 {
1052     const VisibleSelection& selection = frame->editor()->selectionForCommand(event);
1053     return ((selection.isCaret() && selection.isContentEditable()) || selection.isRange())
1054         && frame->mark().isCaretOrRange();
1055 }
1056
1057 static bool enableCaretInEditableText(Frame* frame, Event* event, EditorCommandSource)
1058 {
1059     const VisibleSelection& selection = frame->editor()->selectionForCommand(event);
1060     return selection.isCaret() && selection.isContentEditable();
1061 }
1062
1063 static bool enabledCopy(Frame* frame, Event*, EditorCommandSource)
1064 {
1065     return frame->editor()->canDHTMLCopy() || frame->editor()->canCopy();
1066 }
1067
1068 static bool enabledCut(Frame* frame, Event*, EditorCommandSource)
1069 {
1070     return frame->editor()->canDHTMLCut() || frame->editor()->canCut();
1071 }
1072
1073 static bool enabledDelete(Frame* frame, Event* event, EditorCommandSource source)
1074 {
1075     switch (source) {
1076         case CommandFromMenuOrKeyBinding:
1077             // "Delete" from menu only affects selected range, just like Cut but without affecting pasteboard
1078             return frame->editor()->canDHTMLCut() || frame->editor()->canCut();
1079         case CommandFromDOM:
1080         case CommandFromDOMWithUserInterface:
1081             // "Delete" from DOM is like delete/backspace keypress, affects selected range if non-empty,
1082             // otherwise removes a character
1083             return frame->editor()->selectionForCommand(event).isContentEditable();
1084     }
1085     ASSERT_NOT_REACHED();
1086     return false;
1087 }
1088
1089 static bool enabledInEditableText(Frame* frame, Event* event, EditorCommandSource)
1090 {
1091     return frame->editor()->selectionForCommand(event).isContentEditable();
1092 }
1093
1094 static bool enabledInRichlyEditableText(Frame* frame, Event*, EditorCommandSource)
1095 {
1096     return frame->selection()->isCaretOrRange() && frame->selection()->isContentRichlyEditable();
1097 }
1098
1099 static bool enabledPaste(Frame* frame, Event*, EditorCommandSource)
1100 {
1101     return frame->editor()->canPaste();
1102 }
1103
1104 static bool enabledRangeInEditableText(Frame* frame, Event*, EditorCommandSource)
1105 {
1106     return frame->selection()->isRange() && frame->selection()->isContentEditable();
1107 }
1108
1109 static bool enabledRangeInRichlyEditableText(Frame* frame, Event*, EditorCommandSource)
1110 {
1111     return frame->selection()->isRange() && frame->selection()->isContentRichlyEditable();
1112 }
1113
1114 static bool enabledRedo(Frame* frame, Event*, EditorCommandSource)
1115 {
1116     return frame->editor()->canRedo();
1117 }
1118
1119 static bool enabledUndo(Frame* frame, Event*, EditorCommandSource)
1120 {
1121     return frame->editor()->canUndo();
1122 }
1123
1124 // State functions
1125
1126 static TriState stateNone(Frame*, Event*)
1127 {
1128     return FalseTriState;
1129 }
1130
1131 static TriState stateBold(Frame* frame, Event*)
1132 {
1133     return stateStyle(frame, CSSPropertyFontWeight, "bold");
1134 }
1135
1136 static TriState stateItalic(Frame* frame, Event*)
1137 {
1138     return stateStyle(frame, CSSPropertyFontStyle, "italic");
1139 }
1140
1141 static TriState stateOrderedList(Frame* frame, Event*)
1142 {
1143     return frame->editor()->selectionOrderedListState();
1144 }
1145
1146 static TriState stateStrikethrough(Frame* frame, Event*)
1147 {
1148     return stateStyle(frame, CSSPropertyTextDecoration, "line-through");
1149 }
1150
1151 static TriState stateStyleWithCSS(Frame* frame, Event*)
1152 {
1153     return frame->editor()->shouldStyleWithCSS() ? TrueTriState : FalseTriState;
1154 }
1155
1156 static TriState stateSubscript(Frame* frame, Event*)
1157 {
1158     return stateStyle(frame, CSSPropertyVerticalAlign, "sub");
1159 }
1160
1161 static TriState stateSuperscript(Frame* frame, Event*)
1162 {
1163     return stateStyle(frame, CSSPropertyVerticalAlign, "super");
1164 }
1165
1166 static TriState stateTextWritingDirectionLeftToRight(Frame* frame, Event*)
1167 {
1168     return stateTextWritingDirection(frame, LeftToRightWritingDirection);
1169 }
1170
1171 static TriState stateTextWritingDirectionNatural(Frame* frame, Event*)
1172 {
1173     return stateTextWritingDirection(frame, NaturalWritingDirection);
1174 }
1175
1176 static TriState stateTextWritingDirectionRightToLeft(Frame* frame, Event*)
1177 {
1178     return stateTextWritingDirection(frame, RightToLeftWritingDirection);
1179 }
1180
1181 static TriState stateUnderline(Frame* frame, Event*)
1182 {
1183     return stateStyle(frame, CSSPropertyTextDecoration, "underline");
1184 }
1185
1186 static TriState stateUnorderedList(Frame* frame, Event*)
1187 {
1188     return frame->editor()->selectionUnorderedListState();
1189 }
1190
1191 // Value functions
1192
1193 static String valueNull(Frame*, Event*)
1194 {
1195     return String();
1196 }
1197
1198 static String valueBackColor(Frame* frame, Event*)
1199 {
1200     return valueStyle(frame, CSSPropertyBackgroundColor);
1201 }
1202
1203 static String valueFontName(Frame* frame, Event*)
1204 {
1205     return valueStyle(frame, CSSPropertyFontFamily);
1206 }
1207
1208 static String valueFontSize(Frame* frame, Event*)
1209 {
1210     return valueStyle(frame, CSSPropertyFontSize);
1211 }
1212
1213 static String valueFontSizeDelta(Frame* frame, Event*)
1214 {
1215     return valueStyle(frame, CSSPropertyWebkitFontSizeDelta);
1216 }
1217
1218 static String valueForeColor(Frame* frame, Event*)
1219 {
1220     return valueStyle(frame, CSSPropertyColor);
1221 }
1222
1223 // Map of functions
1224
1225 static const CommandMap& createCommandMap()
1226 {
1227     struct CommandEntry { const char* name; EditorInternalCommand command; };
1228     
1229     static const CommandEntry commands[] = {
1230         { "AlignCenter", { executeJustifyCenter, supportedFromMenuOrKeyBinding, enabledInRichlyEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1231         { "AlignJustified", { executeJustifyFull, supportedFromMenuOrKeyBinding, enabledInRichlyEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1232         { "AlignLeft", { executeJustifyLeft, supportedFromMenuOrKeyBinding, enabledInRichlyEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1233         { "AlignRight", { executeJustifyRight, supportedFromMenuOrKeyBinding, enabledInRichlyEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1234         { "BackColor", { executeBackColor, supported, enabledInRichlyEditableText, stateNone, valueBackColor, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1235         { "BackwardDelete", { executeDeleteBackward, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } }, // FIXME: remove BackwardDelete when Safari for Windows stops using it.
1236         { "Bold", { executeToggleBold, supported, enabledInRichlyEditableText, stateBold, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1237         { "Copy", { executeCopy, supported, enabledCopy, stateNone, valueNull, notTextInsertion, allowExecutionWhenDisabled } },
1238         { "CreateLink", { executeCreateLink, supported, enabledInRichlyEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1239         { "Cut", { executeCut, supported, enabledCut, stateNone, valueNull, notTextInsertion, allowExecutionWhenDisabled } },
1240         { "Delete", { executeDelete, supported, enabledDelete, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1241         { "DeleteBackward", { executeDeleteBackward, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1242         { "DeleteBackwardByDecomposingPreviousCharacter", { executeDeleteBackwardByDecomposingPreviousCharacter, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1243         { "DeleteForward", { executeDeleteForward, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1244         { "DeleteToBeginningOfLine", { executeDeleteToBeginningOfLine, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1245         { "DeleteToBeginningOfParagraph", { executeDeleteToBeginningOfParagraph, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1246         { "DeleteToEndOfLine", { executeDeleteToEndOfLine, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1247         { "DeleteToEndOfParagraph", { executeDeleteToEndOfParagraph, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1248         { "DeleteToMark", { executeDeleteToMark, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1249         { "DeleteWordBackward", { executeDeleteWordBackward, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1250         { "DeleteWordForward", { executeDeleteWordForward, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1251         { "FindString", { executeFindString, supported, enabled, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1252         { "FontName", { executeFontName, supported, enabledInEditableText, stateNone, valueFontName, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1253         { "FontSize", { executeFontSize, supported, enabledInEditableText, stateNone, valueFontSize, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1254         { "FontSizeDelta", { executeFontSizeDelta, supported, enabledInEditableText, stateNone, valueFontSizeDelta, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1255         { "ForeColor", { executeForeColor, supported, enabledInRichlyEditableText, stateNone, valueForeColor, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1256         { "FormatBlock", { executeFormatBlock, supported, enabledInRichlyEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1257         { "ForwardDelete", { executeForwardDelete, supported, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1258         { "HiliteColor", { executeBackColor, supported, enabledInRichlyEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1259         { "IgnoreSpelling", { executeIgnoreSpelling, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1260         { "Indent", { executeIndent, supported, enabledInRichlyEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1261         { "InsertBacktab", { executeInsertBacktab, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, isTextInsertion, doNotAllowExecutionWhenDisabled } },
1262         { "InsertHorizontalRule", { executeInsertHorizontalRule, supported, enabledInRichlyEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1263         { "InsertHTML", { executeInsertHTML, supported, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1264         { "InsertImage", { executeInsertImage, supported, enabledInRichlyEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1265         { "InsertLineBreak", { executeInsertLineBreak, supported, enabledInEditableText, stateNone, valueNull, isTextInsertion, doNotAllowExecutionWhenDisabled } },
1266         { "InsertNewline", { executeInsertNewline, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, isTextInsertion, doNotAllowExecutionWhenDisabled } },    
1267         { "InsertNewlineInQuotedContent", { executeInsertNewlineInQuotedContent, supported, enabledInRichlyEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1268         { "InsertOrderedList", { executeInsertOrderedList, supported, enabledInRichlyEditableText, stateOrderedList, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1269         { "InsertParagraph", { executeInsertParagraph, supported, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1270         { "InsertTab", { executeInsertTab, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, isTextInsertion, doNotAllowExecutionWhenDisabled } },
1271         { "InsertText", { executeInsertText, supported, enabledInEditableText, stateNone, valueNull, isTextInsertion, doNotAllowExecutionWhenDisabled } },
1272         { "InsertUnorderedList", { executeInsertUnorderedList, supported, enabledInRichlyEditableText, stateUnorderedList, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1273         { "Italic", { executeToggleItalic, supported, enabledInRichlyEditableText, stateItalic, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1274         { "JustifyCenter", { executeJustifyCenter, supported, enabledInRichlyEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1275         { "JustifyFull", { executeJustifyFull, supported, enabledInRichlyEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1276         { "JustifyLeft", { executeJustifyLeft, supported, enabledInRichlyEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1277         { "JustifyNone", { executeJustifyLeft, supported, enabledInRichlyEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1278         { "JustifyRight", { executeJustifyRight, supported, enabledInRichlyEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1279         { "MakeTextWritingDirectionLeftToRight", { executeMakeTextWritingDirectionLeftToRight, supportedFromMenuOrKeyBinding, enabledInRichlyEditableText, stateTextWritingDirectionLeftToRight, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1280         { "MakeTextWritingDirectionNatural", { executeMakeTextWritingDirectionNatural, supportedFromMenuOrKeyBinding, enabledInRichlyEditableText, stateTextWritingDirectionNatural, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1281         { "MakeTextWritingDirectionRightToLeft", { executeMakeTextWritingDirectionRightToLeft, supportedFromMenuOrKeyBinding, enabledInRichlyEditableText, stateTextWritingDirectionRightToLeft, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1282         { "MoveBackward", { executeMoveBackward, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1283         { "MoveBackwardAndModifySelection", { executeMoveBackwardAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelection, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1284         { "MoveDown", { executeMoveDown, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1285         { "MoveDownAndModifySelection", { executeMoveDownAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelection, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1286         { "MoveForward", { executeMoveForward, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1287         { "MoveForwardAndModifySelection", { executeMoveForwardAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelection, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1288         { "MoveLeft", { executeMoveLeft, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1289         { "MoveLeftAndModifySelection", { executeMoveLeftAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelection, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1290         { "MovePageDown", { executeMovePageDown, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1291         { "MovePageDownAndModifySelection", { executeMovePageDownAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelection, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1292         { "MovePageUp", { executeMovePageUp, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1293         { "MovePageUpAndModifySelection", { executeMovePageUpAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelection, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1294         { "MoveParagraphBackwardAndModifySelection", { executeMoveParagraphBackwardAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelection, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1295         { "MoveParagraphForwardAndModifySelection", { executeMoveParagraphForwardAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelection, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1296         { "MoveRight", { executeMoveRight, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1297         { "MoveRightAndModifySelection", { executeMoveRightAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelection, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1298         { "MoveToBeginningOfDocument", { executeMoveToBeginningOfDocument, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1299         { "MoveToBeginningOfDocumentAndModifySelection", { executeMoveToBeginningOfDocumentAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelection, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1300         { "MoveToBeginningOfLine", { executeMoveToBeginningOfLine, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1301         { "MoveToBeginningOfLineAndModifySelection", { executeMoveToBeginningOfLineAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelection, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1302         { "MoveToBeginningOfParagraph", { executeMoveToBeginningOfParagraph, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1303         { "MoveToBeginningOfParagraphAndModifySelection", { executeMoveToBeginningOfParagraphAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelection, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1304         { "MoveToBeginningOfSentence", { executeMoveToBeginningOfSentence, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1305         { "MoveToBeginningOfSentenceAndModifySelection", { executeMoveToBeginningOfSentenceAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelection, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1306         { "MoveToEndOfDocument", { executeMoveToEndOfDocument, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1307         { "MoveToEndOfDocumentAndModifySelection", { executeMoveToEndOfDocumentAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelection, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1308         { "MoveToEndOfLine", { executeMoveToEndOfLine, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1309         { "MoveToEndOfLineAndModifySelection", { executeMoveToEndOfLineAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelection, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1310         { "MoveToEndOfParagraph", { executeMoveToEndOfParagraph, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1311         { "MoveToEndOfParagraphAndModifySelection", { executeMoveToEndOfParagraphAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelection, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1312         { "MoveToEndOfSentence", { executeMoveToEndOfSentence, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1313         { "MoveToEndOfSentenceAndModifySelection", { executeMoveToEndOfSentenceAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelection, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1314         { "MoveUp", { executeMoveUp, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1315         { "MoveUpAndModifySelection", { executeMoveUpAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelection, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1316         { "MoveWordBackward", { executeMoveWordBackward, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1317         { "MoveWordBackwardAndModifySelection", { executeMoveWordBackwardAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelection, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1318         { "MoveWordForward", { executeMoveWordForward, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1319         { "MoveWordForwardAndModifySelection", { executeMoveWordForwardAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelection, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1320         { "MoveWordLeft", { executeMoveWordLeft, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1321         { "MoveWordLeftAndModifySelection", { executeMoveWordLeftAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelection, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1322         { "MoveWordRight", { executeMoveWordRight, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1323         { "MoveWordRightAndModifySelection", { executeMoveWordRightAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelection, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1324         { "Outdent", { executeOutdent, supported, enabledInRichlyEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1325         { "Paste", { executePaste, supportedPaste, enabledPaste, stateNone, valueNull, notTextInsertion, allowExecutionWhenDisabled } },
1326         { "PasteAndMatchStyle", { executePasteAndMatchStyle, supportedPaste, enabledPaste, stateNone, valueNull, notTextInsertion, allowExecutionWhenDisabled } },
1327         { "Print", { executePrint, supported, enabled, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1328         { "Redo", { executeRedo, supported, enabledRedo, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1329         { "RemoveFormat", { executeRemoveFormat, supported, enabledRangeInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1330         { "SelectAll", { executeSelectAll, supported, enabled, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1331         { "SelectLine", { executeSelectLine, supportedFromMenuOrKeyBinding, enabledVisibleSelection, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1332         { "SelectParagraph", { executeSelectParagraph, supportedFromMenuOrKeyBinding, enabledVisibleSelection, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1333         { "SelectSentence", { executeSelectSentence, supportedFromMenuOrKeyBinding, enabledVisibleSelection, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1334         { "SelectToMark", { executeSelectToMark, supportedFromMenuOrKeyBinding, enabledVisibleSelectionAndMark, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1335         { "SelectWord", { executeSelectWord, supportedFromMenuOrKeyBinding, enabledVisibleSelection, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1336         { "SetMark", { executeSetMark, supportedFromMenuOrKeyBinding, enabledVisibleSelection, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1337         { "Strikethrough", { executeStrikethrough, supported, enabledInRichlyEditableText, stateStrikethrough, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1338         { "StyleWithCSS", { executeStyleWithCSS, supported, enabledInRichlyEditableText, stateStyleWithCSS, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1339         { "Subscript", { executeSubscript, supported, enabledInRichlyEditableText, stateSubscript, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1340         { "Superscript", { executeSuperscript, supported, enabledInRichlyEditableText, stateSuperscript, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1341         { "SwapWithMark", { executeSwapWithMark, supportedFromMenuOrKeyBinding, enabledVisibleSelectionAndMark, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1342         { "ToggleBold", { executeToggleBold, supportedFromMenuOrKeyBinding, enabledInRichlyEditableText, stateBold, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1343         { "ToggleItalic", { executeToggleItalic, supportedFromMenuOrKeyBinding, enabledInRichlyEditableText, stateItalic, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1344         { "ToggleUnderline", { executeUnderline, supportedFromMenuOrKeyBinding, enabledInRichlyEditableText, stateUnderline, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1345         { "Transpose", { executeTranspose, supported, enableCaretInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1346         { "Underline", { executeUnderline, supported, enabledInRichlyEditableText, stateUnderline, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1347         { "Undo", { executeUndo, supported, enabledUndo, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1348         { "Unlink", { executeUnlink, supported, enabledRangeInRichlyEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1349         { "Unscript", { executeUnscript, supportedFromMenuOrKeyBinding, enabledInRichlyEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1350         { "Unselect", { executeUnselect, supported, enabledVisibleSelection, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1351         { "Yank", { executeYank, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1352         { "YankAndSelect", { executeYankAndSelect, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1353     };
1354
1355     // These unsupported commands are listed here since they appear in the Microsoft
1356     // documentation used as the starting point for our DOM executeCommand support.
1357     //
1358     // 2D-Position (not supported)
1359     // AbsolutePosition (not supported)
1360     // BlockDirLTR (not supported)
1361     // BlockDirRTL (not supported)
1362     // BrowseMode (not supported)
1363     // ClearAuthenticationCache (not supported)
1364     // CreateBookmark (not supported)
1365     // DirLTR (not supported)
1366     // DirRTL (not supported)
1367     // EditMode (not supported)
1368     // InlineDirLTR (not supported)
1369     // InlineDirRTL (not supported)
1370     // InsertButton (not supported)
1371     // InsertFieldSet (not supported)
1372     // InsertIFrame (not supported)
1373     // InsertInputButton (not supported)
1374     // InsertInputCheckbox (not supported)
1375     // InsertInputFileUpload (not supported)
1376     // InsertInputHidden (not supported)
1377     // InsertInputImage (not supported)
1378     // InsertInputPassword (not supported)
1379     // InsertInputRadio (not supported)
1380     // InsertInputReset (not supported)
1381     // InsertInputSubmit (not supported)
1382     // InsertInputText (not supported)
1383     // InsertMarquee (not supported)
1384     // InsertSelectDropDown (not supported)
1385     // InsertSelectListBox (not supported)
1386     // InsertTextArea (not supported)
1387     // LiveResize (not supported)
1388     // MultipleSelection (not supported)
1389     // Open (not supported)
1390     // Overwrite (not supported)
1391     // PlayImage (not supported)
1392     // Refresh (not supported)
1393     // RemoveParaFormat (not supported)
1394     // SaveAs (not supported)
1395     // SizeToControl (not supported)
1396     // SizeToControlHeight (not supported)
1397     // SizeToControlWidth (not supported)
1398     // Stop (not supported)
1399     // StopImage (not supported)
1400     // Unbookmark (not supported)
1401
1402     CommandMap& commandMap = *new CommandMap;
1403
1404     const unsigned numCommands = sizeof(commands) / sizeof(commands[0]);
1405     for (unsigned i = 0; i < numCommands; i++) {
1406         ASSERT(!commandMap.get(commands[i].name));
1407         commandMap.set(commands[i].name, &commands[i].command);
1408     }
1409
1410     return commandMap;
1411 }
1412
1413 Editor::Command Editor::command(const String& commandName)
1414 {
1415     return command(commandName, CommandFromMenuOrKeyBinding);
1416 }
1417
1418 Editor::Command Editor::command(const String& commandName, EditorCommandSource source)
1419 {
1420     if (commandName.isEmpty())
1421         return Command();
1422
1423     static const CommandMap& commandMap = createCommandMap();
1424     const EditorInternalCommand* internalCommand = commandMap.get(commandName);
1425     return internalCommand ? Command(m_frame, internalCommand, source) : Command();
1426 }
1427
1428 Editor::Command::Command()
1429     : m_command(0)
1430     , m_source()
1431 {
1432 }
1433
1434 Editor::Command::Command(PassRefPtr<Frame> frame, const EditorInternalCommand* command, EditorCommandSource source)
1435     : m_frame(frame)
1436     , m_command(command)
1437     , m_source(source)
1438 {
1439     ASSERT(m_frame);
1440     ASSERT(m_command);
1441 }
1442
1443 bool Editor::Command::execute(const String& parameter, Event* triggeringEvent) const
1444 {
1445     if (!isEnabled(triggeringEvent)) {
1446         // Let certain commands be executed when performed explicitly even if they are disabled.
1447         if (!isSupported() || !m_frame || !m_command->allowExecutionWhenDisabled)
1448             return false;
1449     }
1450     m_frame->document()->updateLayoutIgnorePendingStylesheets();
1451     return m_command->execute(m_frame.get(), triggeringEvent, m_source, parameter);
1452 }
1453
1454 bool Editor::Command::execute(Event* triggeringEvent) const
1455 {
1456     return execute(String(), triggeringEvent);
1457 }
1458
1459 bool Editor::Command::isSupported() const
1460 {
1461     return m_command && m_command->isSupported(m_frame.get(), m_source);
1462 }
1463
1464 bool Editor::Command::isEnabled(Event* triggeringEvent) const
1465 {
1466     if (!isSupported() || !m_frame)
1467         return false;
1468     return m_command->isEnabled(m_frame.get(), triggeringEvent, m_source);
1469 }
1470
1471 TriState Editor::Command::state(Event* triggeringEvent) const
1472 {
1473     if (!isSupported() || !m_frame)
1474         return FalseTriState;
1475     return m_command->state(m_frame.get(), triggeringEvent);
1476 }
1477
1478 String Editor::Command::value(Event* triggeringEvent) const
1479 {
1480     if (!isSupported() || !m_frame)
1481         return String();
1482     return m_command->value(m_frame.get(), triggeringEvent);
1483 }
1484
1485 bool Editor::Command::isTextInsertion() const
1486 {
1487     return m_command && m_command->isTextInsertion;
1488 }
1489
1490 } // namespace WebCore