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