Do not notify editor state during executing command
[framework/web/webkit-efl.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 "RenderBox.h"
53 #include "ReplaceSelectionCommand.h"
54 #include "Scrollbar.h"
55 #include "Settings.h"
56 #include "Sound.h"
57 #include "StylePropertySet.h"
58 #include "TypingCommand.h"
59 #include "UnlinkCommand.h"
60 #include "UserTypingGestureIndicator.h"
61 #include "htmlediting.h"
62 #include "markup.h"
63 #include <wtf/text/AtomicString.h>
64
65 namespace WebCore {
66
67 using namespace HTMLNames;
68
69 class EditorInternalCommand {
70 public:
71     bool (*execute)(Frame*, Event*, EditorCommandSource, const String&);
72     bool (*isSupportedFromDOM)(Frame*);
73     bool (*isEnabled)(Frame*, Event*, EditorCommandSource);
74     TriState (*state)(Frame*, Event*);
75     String (*value)(Frame*, Event*);
76     bool isTextInsertion;
77     bool allowExecutionWhenDisabled;
78 };
79
80 typedef HashMap<String, const EditorInternalCommand*, CaseFoldingHash> CommandMap;
81
82 static const bool notTextInsertion = false;
83 static const bool isTextInsertion = true;
84
85 static const bool allowExecutionWhenDisabled = true;
86 static const bool doNotAllowExecutionWhenDisabled = false;
87
88 // Related to Editor::selectionForCommand.
89 // Certain operations continue to use the target control's selection even if the event handler
90 // already moved the selection outside of the text control.
91 static Frame* targetFrame(Frame* frame, Event* event)
92 {
93     if (!event)
94         return frame;
95     Node* node = event->target()->toNode();
96     if (!node)
97         return frame;
98     return node->document()->frame();
99 }
100
101 static bool applyCommandToFrame(Frame* frame, EditorCommandSource source, EditAction action, StylePropertySet* style)
102 {
103     // FIXME: We don't call shouldApplyStyle when the source is DOM; is there a good reason for that?
104     switch (source) {
105     case CommandFromMenuOrKeyBinding:
106         frame->editor()->applyStyleToSelection(style, action);
107         return true;
108     case CommandFromDOM:
109     case CommandFromDOMWithUserInterface:
110         frame->editor()->applyStyle(style);
111         return true;
112     }
113     ASSERT_NOT_REACHED();
114     return false;
115 }
116
117 static bool executeApplyStyle(Frame* frame, EditorCommandSource source, EditAction action, CSSPropertyID propertyID, const String& propertyValue)
118 {
119     RefPtr<StylePropertySet> style = StylePropertySet::create();
120     style->setProperty(propertyID, propertyValue);
121     return applyCommandToFrame(frame, source, action, style.get());
122 }
123
124 static bool executeApplyStyle(Frame* frame, EditorCommandSource source, EditAction action, CSSPropertyID propertyID, int propertyValue)
125 {
126     RefPtr<StylePropertySet> style = StylePropertySet::create();
127     style->setProperty(propertyID, propertyValue);
128     return applyCommandToFrame(frame, source, action, style.get());
129 }
130
131 // FIXME: executeToggleStyleInList does not handle complicated cases such as <b><u>hello</u>world</b> properly.
132 //        This function must use Editor::selectionHasStyle to determine the current style but we cannot fix this
133 //        until https://bugs.webkit.org/show_bug.cgi?id=27818 is resolved.
134 static bool executeToggleStyleInList(Frame* frame, EditorCommandSource source, EditAction action, CSSPropertyID propertyID, CSSValue* value)
135 {
136     ExceptionCode ec = 0;
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 = "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, ec);
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 executePasteAndMatchStyle(Frame* frame, Event*, EditorCommandSource source, const String&)
920 {
921     if (source == CommandFromMenuOrKeyBinding) {
922         UserTypingGestureIndicator typingGestureIndicator(frame);
923         frame->editor()->pasteAsPlainText();
924     } else
925         frame->editor()->pasteAsPlainText();
926     return true;
927 }
928
929 static bool executePasteAsPlainText(Frame* frame, Event*, EditorCommandSource source, const String&)
930 {
931     if (source == CommandFromMenuOrKeyBinding) {
932         UserTypingGestureIndicator typingGestureIndicator(frame);
933         frame->editor()->pasteAsPlainText();
934     } else
935         frame->editor()->pasteAsPlainText();
936     return true;
937 }
938
939 static bool executePrint(Frame* frame, Event*, EditorCommandSource, const String&)
940 {
941     Page* page = frame->page();
942     if (!page)
943         return false;
944     page->chrome()->print(frame);
945     return true;
946 }
947
948 static bool executeRedo(Frame* frame, Event*, EditorCommandSource, const String&)
949 {
950     frame->editor()->redo();
951     return true;
952 }
953
954 static bool executeRemoveFormat(Frame* frame, Event*, EditorCommandSource, const String&)
955 {
956     frame->editor()->removeFormattingAndStyle();
957     return true;
958 }
959
960 static bool executeScrollPageBackward(Frame* frame, Event*, EditorCommandSource, const String&)
961 {
962     return frame->eventHandler()->logicalScrollRecursively(ScrollBlockDirectionBackward, ScrollByPage);
963 }
964
965 static bool executeScrollPageForward(Frame* frame, Event*, EditorCommandSource, const String&)
966 {
967     return frame->eventHandler()->logicalScrollRecursively(ScrollBlockDirectionForward, ScrollByPage);
968 }
969
970 static bool executeScrollLineUp(Frame* frame, Event*, EditorCommandSource, const String&)
971 {
972     return frame->eventHandler()->scrollRecursively(ScrollUp, ScrollByLine);
973 }
974
975 static bool executeScrollLineDown(Frame* frame, Event*, EditorCommandSource, const String&)
976 {
977     return frame->eventHandler()->scrollRecursively(ScrollDown, ScrollByLine);
978 }
979
980 static bool executeScrollToBeginningOfDocument(Frame* frame, Event*, EditorCommandSource, const String&)
981 {
982     return frame->eventHandler()->logicalScrollRecursively(ScrollBlockDirectionBackward, ScrollByDocument);
983 }
984
985 static bool executeScrollToEndOfDocument(Frame* frame, Event*, EditorCommandSource, const String&)
986 {
987     return frame->eventHandler()->logicalScrollRecursively(ScrollBlockDirectionForward, ScrollByDocument);
988 }
989
990 static bool executeSelectAll(Frame* frame, Event*, EditorCommandSource, const String&)
991 {
992     frame->selection()->selectAll();
993     return true;
994 }
995
996 static bool executeSelectLine(Frame* frame, Event*, EditorCommandSource, const String&)
997 {
998     return expandSelectionToGranularity(frame, LineGranularity);
999 }
1000
1001 static bool executeSelectParagraph(Frame* frame, Event*, EditorCommandSource, const String&)
1002 {
1003     return expandSelectionToGranularity(frame, ParagraphGranularity);
1004 }
1005
1006 static bool executeSelectSentence(Frame* frame, Event*, EditorCommandSource, const String&)
1007 {
1008     return expandSelectionToGranularity(frame, SentenceGranularity);
1009 }
1010
1011 static bool executeSelectToMark(Frame* frame, Event*, EditorCommandSource, const String&)
1012 {
1013     RefPtr<Range> mark = frame->editor()->mark().toNormalizedRange();
1014     RefPtr<Range> selection = frame->editor()->selectedRange();
1015     if (!mark || !selection) {
1016         systemBeep();
1017         return false;
1018     }
1019     frame->selection()->setSelectedRange(unionDOMRanges(mark.get(), selection.get()).get(), DOWNSTREAM, true);
1020     return true;
1021 }
1022
1023 static bool executeSelectWord(Frame* frame, Event*, EditorCommandSource, const String&)
1024 {
1025     return expandSelectionToGranularity(frame, WordGranularity);
1026 }
1027
1028 static bool executeSetMark(Frame* frame, Event*, EditorCommandSource, const String&)
1029 {
1030     frame->editor()->setMark(frame->selection()->selection());
1031     return true;
1032 }
1033
1034 static bool executeStrikethrough(Frame* frame, Event*, EditorCommandSource source, const String&)
1035 {
1036     RefPtr<CSSPrimitiveValue> lineThrough = CSSPrimitiveValue::createIdentifier(CSSValueLineThrough);
1037     return executeToggleStyleInList(frame, source, EditActionUnderline, CSSPropertyWebkitTextDecorationsInEffect, lineThrough.get());
1038 }
1039
1040 static bool executeStyleWithCSS(Frame* frame, Event*, EditorCommandSource, const String& value)
1041 {
1042     frame->editor()->setShouldStyleWithCSS(!equalIgnoringCase(value, "false"));
1043     return true;
1044 }
1045
1046 static bool executeUseCSS(Frame* frame, Event*, EditorCommandSource, const String& value)
1047 {
1048     frame->editor()->setShouldStyleWithCSS(equalIgnoringCase(value, "false"));
1049     return true;
1050 }
1051
1052 static bool executeSubscript(Frame* frame, Event*, EditorCommandSource source, const String&)
1053 {
1054     return executeToggleStyle(frame, source, EditActionSubscript, CSSPropertyVerticalAlign, "baseline", "sub");
1055 }
1056
1057 static bool executeSuperscript(Frame* frame, Event*, EditorCommandSource source, const String&)
1058 {
1059     return executeToggleStyle(frame, source, EditActionSuperscript, CSSPropertyVerticalAlign, "baseline", "super");
1060 }
1061
1062 static bool executeSwapWithMark(Frame* frame, Event*, EditorCommandSource, const String&)
1063 {
1064     const VisibleSelection& mark = frame->editor()->mark();
1065     const VisibleSelection& selection = frame->selection()->selection();
1066     if (mark.isNone() || selection.isNone()) {
1067         systemBeep();
1068         return false;
1069     }
1070     frame->selection()->setSelection(mark);
1071     frame->editor()->setMark(selection);
1072     return true;
1073 }
1074
1075 #if PLATFORM(MAC)
1076 static bool executeTakeFindStringFromSelection(Frame* frame, Event*, EditorCommandSource, const String&)
1077 {
1078     frame->editor()->takeFindStringFromSelection();
1079     return true;
1080 }
1081 #endif
1082
1083 static bool executeToggleBold(Frame* frame, Event*, EditorCommandSource source, const String&)
1084 {
1085     return executeToggleStyle(frame, source, EditActionChangeAttributes, CSSPropertyFontWeight, "normal", "bold");
1086 }
1087
1088 static bool executeToggleItalic(Frame* frame, Event*, EditorCommandSource source, const String&)
1089 {
1090     return executeToggleStyle(frame, source, EditActionChangeAttributes, CSSPropertyFontStyle, "normal", "italic");
1091 }
1092
1093 static bool executeTranspose(Frame* frame, Event*, EditorCommandSource, const String&)
1094 {
1095     frame->editor()->transpose();
1096     return true;
1097 }
1098
1099 static bool executeUnderline(Frame* frame, Event*, EditorCommandSource source, const String&)
1100 {
1101     RefPtr<CSSPrimitiveValue> underline = CSSPrimitiveValue::createIdentifier(CSSValueUnderline);
1102     return executeToggleStyleInList(frame, source, EditActionUnderline, CSSPropertyWebkitTextDecorationsInEffect, underline.get());
1103 }
1104
1105 static bool executeUndo(Frame* frame, Event*, EditorCommandSource, const String&)
1106 {
1107     frame->editor()->undo();
1108     return true;
1109 }
1110
1111 static bool executeUnlink(Frame* frame, Event*, EditorCommandSource, const String&)
1112 {
1113     applyCommand(UnlinkCommand::create(frame->document()));
1114     return true;
1115 }
1116
1117 static bool executeUnscript(Frame* frame, Event*, EditorCommandSource source, const String&)
1118 {
1119     return executeApplyStyle(frame, source, EditActionUnscript, CSSPropertyVerticalAlign, "baseline");
1120 }
1121
1122 static bool executeUnselect(Frame* frame, Event*, EditorCommandSource, const String&)
1123 {
1124     frame->selection()->clear();
1125     return true;
1126 }
1127
1128 static bool executeYank(Frame* frame, Event*, EditorCommandSource, const String&)
1129 {
1130     frame->editor()->insertTextWithoutSendingTextEvent(frame->editor()->killRing()->yank(), false, 0);
1131     frame->editor()->killRing()->setToYankedState();
1132     return true;
1133 }
1134
1135 static bool executeYankAndSelect(Frame* frame, Event*, EditorCommandSource, const String&)
1136 {
1137     frame->editor()->insertTextWithoutSendingTextEvent(frame->editor()->killRing()->yank(), true, 0);
1138     frame->editor()->killRing()->setToYankedState();
1139     return true;
1140 }
1141
1142 // Supported functions
1143
1144 static bool supported(Frame*)
1145 {
1146     return true;
1147 }
1148
1149 static bool supportedFromMenuOrKeyBinding(Frame*)
1150 {
1151     return false;
1152 }
1153
1154 static bool supportedCopyCut(Frame* frame)
1155 {
1156     if (!frame)
1157         return false;
1158
1159     Settings* settings = frame->settings();
1160     bool defaultValue = settings && settings->javaScriptCanAccessClipboard();
1161
1162     EditorClient* client = frame->editor()->client();
1163     return client ? client->canCopyCut(frame, defaultValue) : defaultValue;
1164 }
1165
1166 static bool supportedPaste(Frame* frame)
1167 {
1168     if (!frame)
1169         return false;
1170
1171     Settings* settings = frame->settings();
1172     bool defaultValue = settings && settings->javaScriptCanAccessClipboard() && settings->isDOMPasteAllowed();
1173
1174     EditorClient* client = frame->editor()->client();
1175     return client ? client->canPaste(frame, defaultValue) : defaultValue;
1176 }
1177
1178 // Enabled functions
1179
1180 static bool enabled(Frame*, Event*, EditorCommandSource)
1181 {
1182     return true;
1183 }
1184
1185 static bool enabledVisibleSelection(Frame* frame, Event* event, EditorCommandSource)
1186 {
1187     // The term "visible" here includes a caret in editable text or a range in any text.
1188     const VisibleSelection& selection = frame->editor()->selectionForCommand(event);
1189     return (selection.isCaret() && selection.isContentEditable()) || selection.isRange();
1190 }
1191
1192 static bool caretBrowsingEnabled(Frame* frame)
1193 {
1194     return frame->settings() && frame->settings()->caretBrowsingEnabled();
1195 }
1196
1197 static EditorCommandSource dummyEditorCommandSource = static_cast<EditorCommandSource>(0);
1198
1199 static bool enabledVisibleSelectionOrCaretBrowsing(Frame* frame, Event* event, EditorCommandSource)
1200 {
1201     // The EditorCommandSource parameter is unused in enabledVisibleSelection, so just pass a dummy variable
1202     return caretBrowsingEnabled(frame) || enabledVisibleSelection(frame, event, dummyEditorCommandSource);
1203 }
1204
1205 static bool enabledVisibleSelectionAndMark(Frame* frame, Event* event, EditorCommandSource)
1206 {
1207     const VisibleSelection& selection = frame->editor()->selectionForCommand(event);
1208     return ((selection.isCaret() && selection.isContentEditable()) || selection.isRange())
1209         && frame->editor()->mark().isCaretOrRange();
1210 }
1211
1212 static bool enableCaretInEditableText(Frame* frame, Event* event, EditorCommandSource)
1213 {
1214     const VisibleSelection& selection = frame->editor()->selectionForCommand(event);
1215     return selection.isCaret() && selection.isContentEditable();
1216 }
1217
1218 static bool enabledCopy(Frame* frame, Event*, EditorCommandSource)
1219 {
1220     return frame->editor()->canDHTMLCopy() || frame->editor()->canCopy();
1221 }
1222
1223 static bool enabledCut(Frame* frame, Event*, EditorCommandSource)
1224 {
1225     return frame->editor()->canDHTMLCut() || frame->editor()->canCut();
1226 }
1227
1228 static bool enabledInEditableText(Frame* frame, Event* event, EditorCommandSource)
1229 {
1230     return frame->editor()->selectionForCommand(event).rootEditableElement();
1231 }
1232
1233 static bool enabledDelete(Frame* frame, Event* event, EditorCommandSource source)
1234 {
1235     switch (source) {
1236     case CommandFromMenuOrKeyBinding:
1237         // "Delete" from menu only affects selected range, just like Cut but without affecting pasteboard
1238         return enabledCut(frame, event, source);
1239     case CommandFromDOM:
1240     case CommandFromDOMWithUserInterface:
1241         // "Delete" from DOM is like delete/backspace keypress, affects selected range if non-empty,
1242         // otherwise removes a character
1243         return enabledInEditableText(frame, event, source);
1244     }
1245     ASSERT_NOT_REACHED();
1246     return false;
1247 }
1248
1249 static bool enabledInEditableTextOrCaretBrowsing(Frame* frame, Event* event, EditorCommandSource)
1250 {
1251     // The EditorCommandSource parameter is unused in enabledInEditableText, so just pass a dummy variable
1252     return caretBrowsingEnabled(frame) || enabledInEditableText(frame, event, dummyEditorCommandSource);
1253 }
1254
1255 static bool enabledInRichlyEditableText(Frame* frame, Event*, EditorCommandSource)
1256 {
1257     return frame->selection()->isCaretOrRange() && frame->selection()->isContentRichlyEditable() && frame->selection()->rootEditableElement();
1258 }
1259
1260 static bool enabledPaste(Frame* frame, Event*, EditorCommandSource)
1261 {
1262     return frame->editor()->canPaste();
1263 }
1264
1265 static bool enabledRangeInEditableText(Frame* frame, Event*, EditorCommandSource)
1266 {
1267     return frame->selection()->isRange() && frame->selection()->isContentEditable();
1268 }
1269
1270 static bool enabledRangeInRichlyEditableText(Frame* frame, Event*, EditorCommandSource)
1271 {
1272     return frame->selection()->isRange() && frame->selection()->isContentRichlyEditable();
1273 }
1274
1275 static bool enabledRedo(Frame* frame, Event*, EditorCommandSource)
1276 {
1277     return frame->editor()->canRedo();
1278 }
1279
1280 #if PLATFORM(MAC)
1281 static bool enabledTakeFindStringFromSelection(Frame* frame, Event*, EditorCommandSource)
1282 {
1283     return frame->editor()->canCopyExcludingStandaloneImages();
1284 }
1285 #endif
1286
1287 static bool enabledUndo(Frame* frame, Event*, EditorCommandSource)
1288 {
1289     return frame->editor()->canUndo();
1290 }
1291
1292 // State functions
1293
1294 static TriState stateNone(Frame*, Event*)
1295 {
1296     return FalseTriState;
1297 }
1298
1299 static TriState stateBold(Frame* frame, Event*)
1300 {
1301     return stateStyle(frame, CSSPropertyFontWeight, "bold");
1302 }
1303
1304 static TriState stateItalic(Frame* frame, Event*)
1305 {
1306     return stateStyle(frame, CSSPropertyFontStyle, "italic");
1307 }
1308
1309 static TriState stateOrderedList(Frame* frame, Event*)
1310 {
1311     return frame->editor()->selectionOrderedListState();
1312 }
1313
1314 static TriState stateStrikethrough(Frame* frame, Event*)
1315 {
1316     return stateStyle(frame, CSSPropertyWebkitTextDecorationsInEffect, "line-through");
1317 }
1318
1319 static TriState stateStyleWithCSS(Frame* frame, Event*)
1320 {
1321     return frame->editor()->shouldStyleWithCSS() ? TrueTriState : FalseTriState;
1322 }
1323
1324 static TriState stateSubscript(Frame* frame, Event*)
1325 {
1326     return stateStyle(frame, CSSPropertyVerticalAlign, "sub");
1327 }
1328
1329 static TriState stateSuperscript(Frame* frame, Event*)
1330 {
1331     return stateStyle(frame, CSSPropertyVerticalAlign, "super");
1332 }
1333
1334 static TriState stateTextWritingDirectionLeftToRight(Frame* frame, Event*)
1335 {
1336     return stateTextWritingDirection(frame, LeftToRightWritingDirection);
1337 }
1338
1339 static TriState stateTextWritingDirectionNatural(Frame* frame, Event*)
1340 {
1341     return stateTextWritingDirection(frame, NaturalWritingDirection);
1342 }
1343
1344 static TriState stateTextWritingDirectionRightToLeft(Frame* frame, Event*)
1345 {
1346     return stateTextWritingDirection(frame, RightToLeftWritingDirection);
1347 }
1348
1349 static TriState stateUnderline(Frame* frame, Event*)
1350 {
1351     return stateStyle(frame, CSSPropertyWebkitTextDecorationsInEffect, "underline");
1352 }
1353
1354 static TriState stateUnorderedList(Frame* frame, Event*)
1355 {
1356     return frame->editor()->selectionUnorderedListState();
1357 }
1358
1359 static TriState stateJustifyCenter(Frame* frame, Event*)
1360 {
1361     return stateStyle(frame, CSSPropertyTextAlign, "center");
1362 }
1363
1364 static TriState stateJustifyFull(Frame* frame, Event*)
1365 {
1366     return stateStyle(frame, CSSPropertyTextAlign, "justify");
1367 }
1368
1369 static TriState stateJustifyLeft(Frame* frame, Event*)
1370 {
1371     return stateStyle(frame, CSSPropertyTextAlign, "left");
1372 }
1373
1374 static TriState stateJustifyRight(Frame* frame, Event*)
1375 {
1376     return stateStyle(frame, CSSPropertyTextAlign, "right");
1377 }
1378
1379 // Value functions
1380
1381 static String valueNull(Frame*, Event*)
1382 {
1383     return String();
1384 }
1385
1386 static String valueBackColor(Frame* frame, Event*)
1387 {
1388     return valueStyle(frame, CSSPropertyBackgroundColor);
1389 }
1390
1391 static String valueDefaultParagraphSeparator(Frame* frame, Event*)
1392 {
1393     switch (frame->editor()->defaultParagraphSeparator()) {
1394     case EditorParagraphSeparatorIsDiv:
1395         return divTag.localName();
1396     case EditorParagraphSeparatorIsP:
1397         return pTag.localName();
1398     }
1399
1400     ASSERT_NOT_REACHED();
1401     return String();
1402 }
1403
1404 static String valueFontName(Frame* frame, Event*)
1405 {
1406     return valueStyle(frame, CSSPropertyFontFamily);
1407 }
1408
1409 static String valueFontSize(Frame* frame, Event*)
1410 {
1411     return valueStyle(frame, CSSPropertyFontSize);
1412 }
1413
1414 static String valueFontSizeDelta(Frame* frame, Event*)
1415 {
1416     return valueStyle(frame, CSSPropertyWebkitFontSizeDelta);
1417 }
1418
1419 static String valueForeColor(Frame* frame, Event*)
1420 {
1421     return valueStyle(frame, CSSPropertyColor);
1422 }
1423
1424 static String valueFormatBlock(Frame* frame, Event*)
1425 {
1426     const VisibleSelection& selection = frame->selection()->selection();
1427     if (!selection.isNonOrphanedCaretOrRange() || !selection.isContentEditable())
1428         return "";
1429     Element* formatBlockElement = FormatBlockCommand::elementForFormatBlockCommand(selection.firstRange().get());
1430     if (!formatBlockElement)
1431         return "";
1432     return formatBlockElement->localName();
1433 }
1434
1435 // Map of functions
1436
1437 struct CommandEntry {
1438     const char* name;
1439     EditorInternalCommand command;
1440 };
1441
1442 static const CommandMap& createCommandMap()
1443 {
1444     static const CommandEntry commands[] = {
1445         { "AlignCenter", { executeJustifyCenter, supportedFromMenuOrKeyBinding, enabledInRichlyEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1446         { "AlignJustified", { executeJustifyFull, supportedFromMenuOrKeyBinding, enabledInRichlyEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1447         { "AlignLeft", { executeJustifyLeft, supportedFromMenuOrKeyBinding, enabledInRichlyEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1448         { "AlignRight", { executeJustifyRight, supportedFromMenuOrKeyBinding, enabledInRichlyEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1449         { "BackColor", { executeBackColor, supported, enabledInRichlyEditableText, stateNone, valueBackColor, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1450         { "BackwardDelete", { executeDeleteBackward, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } }, // FIXME: remove BackwardDelete when Safari for Windows stops using it.
1451         { "Bold", { executeToggleBold, supported, enabledInRichlyEditableText, stateBold, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1452         { "Copy", { executeCopy, supportedCopyCut, enabledCopy, stateNone, valueNull, notTextInsertion, allowExecutionWhenDisabled } },
1453         { "CreateLink", { executeCreateLink, supported, enabledInRichlyEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1454         { "Cut", { executeCut, supportedCopyCut, enabledCut, stateNone, valueNull, notTextInsertion, allowExecutionWhenDisabled } },
1455         { "DefaultParagraphSeparator", { executeDefaultParagraphSeparator, supported, enabled, stateNone, valueDefaultParagraphSeparator, notTextInsertion, doNotAllowExecutionWhenDisabled} },
1456         { "Delete", { executeDelete, supported, enabledDelete, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1457         { "DeleteBackward", { executeDeleteBackward, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1458         { "DeleteBackwardByDecomposingPreviousCharacter", { executeDeleteBackwardByDecomposingPreviousCharacter, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1459         { "DeleteForward", { executeDeleteForward, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1460         { "DeleteToBeginningOfLine", { executeDeleteToBeginningOfLine, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1461         { "DeleteToBeginningOfParagraph", { executeDeleteToBeginningOfParagraph, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1462         { "DeleteToEndOfLine", { executeDeleteToEndOfLine, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1463         { "DeleteToEndOfParagraph", { executeDeleteToEndOfParagraph, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1464         { "DeleteToMark", { executeDeleteToMark, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1465         { "DeleteWordBackward", { executeDeleteWordBackward, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1466         { "DeleteWordForward", { executeDeleteWordForward, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1467         { "FindString", { executeFindString, supported, enabled, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1468         { "FontName", { executeFontName, supported, enabledInEditableText, stateNone, valueFontName, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1469         { "FontSize", { executeFontSize, supported, enabledInEditableText, stateNone, valueFontSize, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1470         { "FontSizeDelta", { executeFontSizeDelta, supported, enabledInEditableText, stateNone, valueFontSizeDelta, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1471         { "ForeColor", { executeForeColor, supported, enabledInRichlyEditableText, stateNone, valueForeColor, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1472         { "FormatBlock", { executeFormatBlock, supported, enabledInRichlyEditableText, stateNone, valueFormatBlock, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1473         { "ForwardDelete", { executeForwardDelete, supported, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1474         { "HiliteColor", { executeBackColor, supported, enabledInRichlyEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1475         { "IgnoreSpelling", { executeIgnoreSpelling, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1476         { "Indent", { executeIndent, supported, enabledInRichlyEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1477         { "InsertBacktab", { executeInsertBacktab, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, isTextInsertion, doNotAllowExecutionWhenDisabled } },
1478         { "InsertHTML", { executeInsertHTML, supported, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1479         { "InsertHorizontalRule", { executeInsertHorizontalRule, supported, enabledInRichlyEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1480         { "InsertImage", { executeInsertImage, supported, enabledInRichlyEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1481         { "InsertLineBreak", { executeInsertLineBreak, supported, enabledInEditableText, stateNone, valueNull, isTextInsertion, doNotAllowExecutionWhenDisabled } },
1482         { "InsertNewline", { executeInsertNewline, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, isTextInsertion, doNotAllowExecutionWhenDisabled } },    
1483         { "InsertNewlineInQuotedContent", { executeInsertNewlineInQuotedContent, supported, enabledInRichlyEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1484         { "InsertOrderedList", { executeInsertOrderedList, supported, enabledInRichlyEditableText, stateOrderedList, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1485         { "InsertParagraph", { executeInsertParagraph, supported, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1486         { "InsertTab", { executeInsertTab, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, isTextInsertion, doNotAllowExecutionWhenDisabled } },
1487         { "InsertText", { executeInsertText, supported, enabledInEditableText, stateNone, valueNull, isTextInsertion, doNotAllowExecutionWhenDisabled } },
1488         { "InsertUnorderedList", { executeInsertUnorderedList, supported, enabledInRichlyEditableText, stateUnorderedList, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1489         { "Italic", { executeToggleItalic, supported, enabledInRichlyEditableText, stateItalic, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1490         { "JustifyCenter", { executeJustifyCenter, supported, enabledInRichlyEditableText, stateJustifyCenter, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1491         { "JustifyFull", { executeJustifyFull, supported, enabledInRichlyEditableText, stateJustifyFull, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1492         { "JustifyLeft", { executeJustifyLeft, supported, enabledInRichlyEditableText, stateJustifyLeft, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1493         { "JustifyNone", { executeJustifyLeft, supported, enabledInRichlyEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1494         { "JustifyRight", { executeJustifyRight, supported, enabledInRichlyEditableText, stateJustifyRight, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1495         { "MakeTextWritingDirectionLeftToRight", { executeMakeTextWritingDirectionLeftToRight, supportedFromMenuOrKeyBinding, enabledInRichlyEditableText, stateTextWritingDirectionLeftToRight, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1496         { "MakeTextWritingDirectionNatural", { executeMakeTextWritingDirectionNatural, supportedFromMenuOrKeyBinding, enabledInRichlyEditableText, stateTextWritingDirectionNatural, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1497         { "MakeTextWritingDirectionRightToLeft", { executeMakeTextWritingDirectionRightToLeft, supportedFromMenuOrKeyBinding, enabledInRichlyEditableText, stateTextWritingDirectionRightToLeft, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1498         { "MoveBackward", { executeMoveBackward, supportedFromMenuOrKeyBinding, enabledInEditableTextOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1499         { "MoveBackwardAndModifySelection", { executeMoveBackwardAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelectionOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1500         { "MoveDown", { executeMoveDown, supportedFromMenuOrKeyBinding, enabledInEditableTextOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1501         { "MoveDownAndModifySelection", { executeMoveDownAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelectionOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1502         { "MoveForward", { executeMoveForward, supportedFromMenuOrKeyBinding, enabledInEditableTextOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1503         { "MoveForwardAndModifySelection", { executeMoveForwardAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelectionOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1504         { "MoveLeft", { executeMoveLeft, supportedFromMenuOrKeyBinding, enabledInEditableTextOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1505         { "MoveLeftAndModifySelection", { executeMoveLeftAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelectionOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1506         { "MovePageDown", { executeMovePageDown, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1507         { "MovePageDownAndModifySelection", { executeMovePageDownAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelection, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1508         { "MovePageUp", { executeMovePageUp, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1509         { "MovePageUpAndModifySelection", { executeMovePageUpAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelection, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1510         { "MoveParagraphBackwardAndModifySelection", { executeMoveParagraphBackwardAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelectionOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1511         { "MoveParagraphForwardAndModifySelection", { executeMoveParagraphForwardAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelectionOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1512         { "MoveRight", { executeMoveRight, supportedFromMenuOrKeyBinding, enabledInEditableTextOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1513         { "MoveRightAndModifySelection", { executeMoveRightAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelectionOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1514         { "MoveToBeginningOfDocument", { executeMoveToBeginningOfDocument, supportedFromMenuOrKeyBinding, enabledInEditableTextOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1515         { "MoveToBeginningOfDocumentAndModifySelection", { executeMoveToBeginningOfDocumentAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelectionOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1516         { "MoveToBeginningOfLine", { executeMoveToBeginningOfLine, supportedFromMenuOrKeyBinding, enabledInEditableTextOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1517         { "MoveToBeginningOfLineAndModifySelection", { executeMoveToBeginningOfLineAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelectionOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1518         { "MoveToBeginningOfParagraph", { executeMoveToBeginningOfParagraph, supportedFromMenuOrKeyBinding, enabledInEditableTextOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1519         { "MoveToBeginningOfParagraphAndModifySelection", { executeMoveToBeginningOfParagraphAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelectionOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1520         { "MoveToBeginningOfSentence", { executeMoveToBeginningOfSentence, supportedFromMenuOrKeyBinding, enabledInEditableTextOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1521         { "MoveToBeginningOfSentenceAndModifySelection", { executeMoveToBeginningOfSentenceAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelectionOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1522         { "MoveToEndOfDocument", { executeMoveToEndOfDocument, supportedFromMenuOrKeyBinding, enabledInEditableTextOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1523         { "MoveToEndOfDocumentAndModifySelection", { executeMoveToEndOfDocumentAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelectionOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1524         { "MoveToEndOfLine", { executeMoveToEndOfLine, supportedFromMenuOrKeyBinding, enabledInEditableTextOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1525         { "MoveToEndOfLineAndModifySelection", { executeMoveToEndOfLineAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelectionOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1526         { "MoveToEndOfParagraph", { executeMoveToEndOfParagraph, supportedFromMenuOrKeyBinding, enabledInEditableTextOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1527         { "MoveToEndOfParagraphAndModifySelection", { executeMoveToEndOfParagraphAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelectionOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1528         { "MoveToEndOfSentence", { executeMoveToEndOfSentence, supportedFromMenuOrKeyBinding, enabledInEditableTextOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1529         { "MoveToEndOfSentenceAndModifySelection", { executeMoveToEndOfSentenceAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelectionOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1530         { "MoveToLeftEndOfLine", { executeMoveToLeftEndOfLine, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1531         { "MoveToLeftEndOfLineAndModifySelection", { executeMoveToLeftEndOfLineAndModifySelection, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1532         { "MoveToRightEndOfLine", { executeMoveToRightEndOfLine, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1533         { "MoveToRightEndOfLineAndModifySelection", { executeMoveToRightEndOfLineAndModifySelection, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1534         { "MoveUp", { executeMoveUp, supportedFromMenuOrKeyBinding, enabledInEditableTextOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1535         { "MoveUpAndModifySelection", { executeMoveUpAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelectionOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1536         { "MoveWordBackward", { executeMoveWordBackward, supportedFromMenuOrKeyBinding, enabledInEditableTextOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1537         { "MoveWordBackwardAndModifySelection", { executeMoveWordBackwardAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelectionOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1538         { "MoveWordForward", { executeMoveWordForward, supportedFromMenuOrKeyBinding, enabledInEditableTextOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1539         { "MoveWordForwardAndModifySelection", { executeMoveWordForwardAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelectionOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1540         { "MoveWordLeft", { executeMoveWordLeft, supportedFromMenuOrKeyBinding, enabledInEditableTextOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1541         { "MoveWordLeftAndModifySelection", { executeMoveWordLeftAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelectionOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1542         { "MoveWordRight", { executeMoveWordRight, supportedFromMenuOrKeyBinding, enabledInEditableTextOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1543         { "MoveWordRightAndModifySelection", { executeMoveWordRightAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelectionOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1544         { "Outdent", { executeOutdent, supported, enabledInRichlyEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1545         { "Paste", { executePaste, supportedPaste, enabledPaste, stateNone, valueNull, notTextInsertion, allowExecutionWhenDisabled } },
1546         { "PasteAndMatchStyle", { executePasteAndMatchStyle, supportedPaste, enabledPaste, stateNone, valueNull, notTextInsertion, allowExecutionWhenDisabled } },
1547         { "PasteAsPlainText", { executePasteAsPlainText, supportedPaste, enabledPaste, stateNone, valueNull, notTextInsertion, allowExecutionWhenDisabled } },
1548         { "Print", { executePrint, supported, enabled, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1549         { "Redo", { executeRedo, supported, enabledRedo, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1550         { "RemoveFormat", { executeRemoveFormat, supported, enabledRangeInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1551         { "ScrollPageBackward", { executeScrollPageBackward, supportedFromMenuOrKeyBinding, enabled, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1552         { "ScrollPageForward", { executeScrollPageForward, supportedFromMenuOrKeyBinding, enabled, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1553         { "ScrollLineUp", { executeScrollLineUp, supportedFromMenuOrKeyBinding, enabled, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1554         { "ScrollLineDown", { executeScrollLineDown, supportedFromMenuOrKeyBinding, enabled, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1555         { "ScrollToBeginningOfDocument", { executeScrollToBeginningOfDocument, supportedFromMenuOrKeyBinding, enabled, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1556         { "ScrollToEndOfDocument", { executeScrollToEndOfDocument, supportedFromMenuOrKeyBinding, enabled, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1557         { "SelectAll", { executeSelectAll, supported, enabled, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1558         { "SelectLine", { executeSelectLine, supportedFromMenuOrKeyBinding, enabledVisibleSelection, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1559         { "SelectParagraph", { executeSelectParagraph, supportedFromMenuOrKeyBinding, enabledVisibleSelection, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1560         { "SelectSentence", { executeSelectSentence, supportedFromMenuOrKeyBinding, enabledVisibleSelection, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1561         { "SelectToMark", { executeSelectToMark, supportedFromMenuOrKeyBinding, enabledVisibleSelectionAndMark, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1562         { "SelectWord", { executeSelectWord, supportedFromMenuOrKeyBinding, enabledVisibleSelection, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1563         { "SetMark", { executeSetMark, supportedFromMenuOrKeyBinding, enabledVisibleSelection, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1564         { "Strikethrough", { executeStrikethrough, supported, enabledInRichlyEditableText, stateStrikethrough, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1565         { "StyleWithCSS", { executeStyleWithCSS, supported, enabled, stateStyleWithCSS, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1566         { "Subscript", { executeSubscript, supported, enabledInRichlyEditableText, stateSubscript, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1567         { "Superscript", { executeSuperscript, supported, enabledInRichlyEditableText, stateSuperscript, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1568         { "SwapWithMark", { executeSwapWithMark, supportedFromMenuOrKeyBinding, enabledVisibleSelectionAndMark, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1569         { "ToggleBold", { executeToggleBold, supportedFromMenuOrKeyBinding, enabledInRichlyEditableText, stateBold, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1570         { "ToggleItalic", { executeToggleItalic, supportedFromMenuOrKeyBinding, enabledInRichlyEditableText, stateItalic, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1571         { "ToggleUnderline", { executeUnderline, supportedFromMenuOrKeyBinding, enabledInRichlyEditableText, stateUnderline, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1572         { "Transpose", { executeTranspose, supported, enableCaretInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1573         { "Underline", { executeUnderline, supported, enabledInRichlyEditableText, stateUnderline, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1574         { "Undo", { executeUndo, supported, enabledUndo, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1575         { "Unlink", { executeUnlink, supported, enabledRangeInRichlyEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1576         { "Unscript", { executeUnscript, supportedFromMenuOrKeyBinding, enabledInRichlyEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1577         { "Unselect", { executeUnselect, supported, enabledVisibleSelection, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1578         { "UseCSS", { executeUseCSS, supported, enabled, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1579         { "Yank", { executeYank, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1580         { "YankAndSelect", { executeYankAndSelect, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1581
1582 #if PLATFORM(MAC)
1583         { "TakeFindStringFromSelection", { executeTakeFindStringFromSelection, supportedFromMenuOrKeyBinding, enabledTakeFindStringFromSelection, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1584 #endif
1585     };
1586
1587     // These unsupported commands are listed here since they appear in the Microsoft
1588     // documentation used as the starting point for our DOM executeCommand support.
1589     //
1590     // 2D-Position (not supported)
1591     // AbsolutePosition (not supported)
1592     // BlockDirLTR (not supported)
1593     // BlockDirRTL (not supported)
1594     // BrowseMode (not supported)
1595     // ClearAuthenticationCache (not supported)
1596     // CreateBookmark (not supported)
1597     // DirLTR (not supported)
1598     // DirRTL (not supported)
1599     // EditMode (not supported)
1600     // InlineDirLTR (not supported)
1601     // InlineDirRTL (not supported)
1602     // InsertButton (not supported)
1603     // InsertFieldSet (not supported)
1604     // InsertIFrame (not supported)
1605     // InsertInputButton (not supported)
1606     // InsertInputCheckbox (not supported)
1607     // InsertInputFileUpload (not supported)
1608     // InsertInputHidden (not supported)
1609     // InsertInputImage (not supported)
1610     // InsertInputPassword (not supported)
1611     // InsertInputRadio (not supported)
1612     // InsertInputReset (not supported)
1613     // InsertInputSubmit (not supported)
1614     // InsertInputText (not supported)
1615     // InsertMarquee (not supported)
1616     // InsertSelectDropDown (not supported)
1617     // InsertSelectListBox (not supported)
1618     // InsertTextArea (not supported)
1619     // LiveResize (not supported)
1620     // MultipleSelection (not supported)
1621     // Open (not supported)
1622     // Overwrite (not supported)
1623     // PlayImage (not supported)
1624     // Refresh (not supported)
1625     // RemoveParaFormat (not supported)
1626     // SaveAs (not supported)
1627     // SizeToControl (not supported)
1628     // SizeToControlHeight (not supported)
1629     // SizeToControlWidth (not supported)
1630     // Stop (not supported)
1631     // StopImage (not supported)
1632     // Unbookmark (not supported)
1633
1634     CommandMap& commandMap = *new CommandMap;
1635
1636     for (size_t i = 0; i < WTF_ARRAY_LENGTH(commands); ++i) {
1637         ASSERT(!commandMap.get(commands[i].name));
1638         commandMap.set(commands[i].name, &commands[i].command);
1639     }
1640
1641     return commandMap;
1642 }
1643
1644 static const EditorInternalCommand* internalCommand(const String& commandName)
1645 {
1646     static const CommandMap& commandMap = createCommandMap();
1647     return commandName.isEmpty() ? 0 : commandMap.get(commandName);
1648 }
1649
1650 Editor::Command Editor::command(const String& commandName)
1651 {
1652     return Command(internalCommand(commandName), CommandFromMenuOrKeyBinding, m_frame);
1653 }
1654
1655 Editor::Command Editor::command(const String& commandName, EditorCommandSource source)
1656 {
1657     return Command(internalCommand(commandName), source, m_frame);
1658 }
1659
1660 bool Editor::commandIsSupportedFromMenuOrKeyBinding(const String& commandName)
1661 {
1662     return internalCommand(commandName);
1663 }
1664
1665 Editor::Command::Command()
1666     : m_command(0)
1667 {
1668 }
1669
1670 Editor::Command::Command(const EditorInternalCommand* command, EditorCommandSource source, PassRefPtr<Frame> frame)
1671     : m_command(command)
1672     , m_source(source)
1673     , m_frame(command ? frame : 0)
1674 {
1675     // Use separate assertions so we can tell which bad thing happened.
1676     if (!command)
1677         ASSERT(!m_frame);
1678     else
1679         ASSERT(m_frame);
1680 }
1681
1682 bool Editor::Command::execute(const String& parameter, Event* triggeringEvent) const
1683 {
1684     if (!isEnabled(triggeringEvent)) {
1685         // Let certain commands be executed when performed explicitly even if they are disabled.
1686         if (!isSupported() || !m_frame || !m_command->allowExecutionWhenDisabled)
1687             return false;
1688     }
1689     m_frame->document()->updateLayoutIgnorePendingStylesheets();
1690 #if ENABLE(TIZEN_ISF_PORT)
1691     EditorClient* client = m_frame->editor()->client();
1692     if (client) {
1693         client->lockRespondToChangedSelection();
1694         bool result = m_command->execute(m_frame.get(), triggeringEvent, m_source, parameter);
1695         client->unlockRespondToChangedSelection();
1696         return result;
1697     }
1698 #endif
1699     return m_command->execute(m_frame.get(), triggeringEvent, m_source, parameter);
1700 }
1701
1702 bool Editor::Command::execute(Event* triggeringEvent) const
1703 {
1704     return execute(String(), triggeringEvent);
1705 }
1706
1707 bool Editor::Command::isSupported() const
1708 {
1709     if (!m_command)
1710         return false;
1711     switch (m_source) {
1712     case CommandFromMenuOrKeyBinding:
1713         return true;
1714     case CommandFromDOM:
1715     case CommandFromDOMWithUserInterface:
1716         return m_command->isSupportedFromDOM(m_frame.get());
1717     }
1718     ASSERT_NOT_REACHED();
1719     return false;
1720 }
1721
1722 bool Editor::Command::isEnabled(Event* triggeringEvent) const
1723 {
1724     if (!isSupported() || !m_frame)
1725         return false;
1726     return m_command->isEnabled(m_frame.get(), triggeringEvent, m_source);
1727 }
1728
1729 TriState Editor::Command::state(Event* triggeringEvent) const
1730 {
1731     if (!isSupported() || !m_frame)
1732         return FalseTriState;
1733     return m_command->state(m_frame.get(), triggeringEvent);
1734 }
1735
1736 String Editor::Command::value(Event* triggeringEvent) const
1737 {
1738     if (!isSupported() || !m_frame)
1739         return String();
1740     if (m_command->value == valueNull && m_command->state != stateNone)
1741         return m_command->state(m_frame.get(), triggeringEvent) == TrueTriState ? "true" : "false";
1742     return m_command->value(m_frame.get(), triggeringEvent);
1743 }
1744
1745 bool Editor::Command::isTextInsertion() const
1746 {
1747     return m_command && m_command->isTextInsertion;
1748 }
1749
1750 } // namespace WebCore