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