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