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