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