2 * Copyright (C) 2006, 2007, 2008 Apple Inc. All rights reserved.
3 * Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include "AtomicString.h"
30 #include "CSSPropertyNames.h"
31 #include "CSSValueKeywords.h"
32 #include "CreateLinkCommand.h"
33 #include "DocumentFragment.h"
35 #include "EditorClient.h"
37 #include "EventHandler.h"
38 #include "FormatBlockCommand.h"
40 #include "HTMLFontElement.h"
41 #include "HTMLImageElement.h"
42 #include "IndentOutdentCommand.h"
43 #include "InsertListCommand.h"
45 #include "RenderBox.h"
46 #include "ReplaceSelectionCommand.h"
47 #include "Scrollbar.h"
50 #include "TypingCommand.h"
51 #include "UnlinkCommand.h"
52 #include "htmlediting.h"
57 using namespace HTMLNames;
59 class EditorInternalCommand {
61 bool (*execute)(Frame*, Event*, EditorCommandSource, const String&);
62 bool (*isSupported)(Frame*, EditorCommandSource);
63 bool (*isEnabled)(Frame*, Event*, EditorCommandSource);
64 TriState (*state)(Frame*, Event*);
65 String (*value)(Frame*, Event*);
67 bool allowExecutionWhenDisabled;
70 typedef HashMap<String, const EditorInternalCommand*, CaseFoldingHash> CommandMap;
72 static const bool notTextInsertion = false;
73 static const bool isTextInsertion = true;
75 static const bool allowExecutionWhenDisabled = true;
76 static const bool doNotAllowExecutionWhenDisabled = false;
78 // Related to Editor::selectionForCommand.
79 // Certain operations continue to use the target control's selection even if the event handler
80 // already moved the selection outside of the text control.
81 static Frame* targetFrame(Frame* frame, Event* event)
85 Node* node = event->target()->toNode();
88 return node->document()->frame();
91 static bool executeApplyStyle(Frame* frame, EditorCommandSource source, EditAction action, int propertyID, const String& propertyValue)
93 RefPtr<CSSMutableStyleDeclaration> style = CSSMutableStyleDeclaration::create();
94 style->setProperty(propertyID, propertyValue);
95 // FIXME: We don't call shouldApplyStyle when the source is DOM; is there a good reason for that?
97 case CommandFromMenuOrKeyBinding:
98 frame->editor()->applyStyleToSelection(style.get(), action);
101 case CommandFromDOMWithUserInterface:
102 frame->editor()->applyStyle(style.get());
105 ASSERT_NOT_REACHED();
109 static bool executeApplyStyle(Frame* frame, EditorCommandSource source, EditAction action, int propertyID, const char* propertyValue)
111 return executeApplyStyle(frame, source, action, propertyID, String(propertyValue));
114 static bool executeApplyStyle(Frame* frame, EditorCommandSource source, EditAction action, int propertyID, int propertyValue)
116 RefPtr<CSSMutableStyleDeclaration> style = CSSMutableStyleDeclaration::create();
117 style->setProperty(propertyID, propertyValue);
118 // FIXME: We don't call shouldApplyStyle when the source is DOM; is there a good reason for that?
120 case CommandFromMenuOrKeyBinding:
121 frame->editor()->applyStyleToSelection(style.get(), action);
124 case CommandFromDOMWithUserInterface:
125 frame->editor()->applyStyle(style.get());
128 ASSERT_NOT_REACHED();
132 static bool executeToggleStyle(Frame* frame, EditorCommandSource source, EditAction action, int propertyID, const char* offValue, const char* onValue)
134 RefPtr<CSSMutableStyleDeclaration> style = CSSMutableStyleDeclaration::create();
135 style->setProperty(propertyID, onValue);
136 style->setProperty(propertyID, frame->editor()->selectionStartHasStyle(style.get()) ? offValue : onValue);
137 // FIXME: We don't call shouldApplyStyle when the source is DOM; is there a good reason for that?
139 case CommandFromMenuOrKeyBinding:
140 frame->editor()->applyStyleToSelection(style.get(), action);
143 case CommandFromDOMWithUserInterface:
144 frame->editor()->applyStyle(style.get());
147 ASSERT_NOT_REACHED();
151 static bool executeApplyParagraphStyle(Frame* frame, EditorCommandSource source, EditAction action, int propertyID, const String& propertyValue)
153 RefPtr<CSSMutableStyleDeclaration> style = CSSMutableStyleDeclaration::create();
154 style->setProperty(propertyID, propertyValue);
155 // FIXME: We don't call shouldApplyStyle when the source is DOM; is there a good reason for that?
157 case CommandFromMenuOrKeyBinding:
158 frame->editor()->applyParagraphStyleToSelection(style.get(), action);
161 case CommandFromDOMWithUserInterface:
162 frame->editor()->applyParagraphStyle(style.get());
165 ASSERT_NOT_REACHED();
169 static bool executeInsertFragment(Frame* frame, PassRefPtr<DocumentFragment> fragment)
171 applyCommand(ReplaceSelectionCommand::create(frame->document(), fragment,
172 false, false, false, true, false, EditActionUnspecified));
176 static bool executeInsertNode(Frame* frame, PassRefPtr<Node> content)
178 RefPtr<DocumentFragment> fragment = new DocumentFragment(frame->document());
179 ExceptionCode ec = 0;
180 fragment->appendChild(content, ec);
183 return executeInsertFragment(frame, fragment.release());
186 static bool expandSelectionToGranularity(Frame* frame, TextGranularity granularity)
188 VisibleSelection selection = frame->selection()->selection();
189 selection.expandUsingGranularity(granularity);
190 RefPtr<Range> newRange = selection.toNormalizedRange();
193 ExceptionCode ec = 0;
194 if (newRange->collapsed(ec))
196 RefPtr<Range> oldRange = frame->selection()->selection().toNormalizedRange();
197 EAffinity affinity = frame->selection()->affinity();
198 if (!frame->editor()->client()->shouldChangeSelectedRange(oldRange.get(), newRange.get(), affinity, false))
200 frame->selection()->setSelectedRange(newRange.get(), affinity, true);
204 static TriState stateStyle(Frame* frame, int propertyID, const char* desiredValue)
206 RefPtr<CSSMutableStyleDeclaration> style = CSSMutableStyleDeclaration::create();
207 style->setProperty(propertyID, desiredValue);
208 return frame->editor()->selectionHasStyle(style.get());
211 static String valueStyle(Frame* frame, int propertyID)
213 return frame->selectionStartStylePropertyValue(propertyID);
216 static TriState stateTextWritingDirection(Frame* frame, WritingDirection direction)
218 bool hasNestedOrMultipleEmbeddings;
219 WritingDirection selectionDirection = frame->editor()->textDirectionForSelection(hasNestedOrMultipleEmbeddings);
220 return (selectionDirection == direction && !hasNestedOrMultipleEmbeddings) ? TrueTriState : FalseTriState;
223 static int verticalScrollDistance(Frame* frame)
225 Node* focusedNode = frame->document()->focusedNode();
228 RenderObject* renderer = focusedNode->renderer();
229 if (!renderer || !renderer->isBox())
231 RenderStyle* style = renderer->style();
234 if (!(style->overflowY() == OSCROLL || style->overflowY() == OAUTO || renderer->isTextArea()))
236 int height = toRenderBox(renderer)->clientHeight();
237 return max((height + 1) / 2, height - cAmountToKeepWhenPaging);
240 static RefPtr<Range> unionDOMRanges(Range* a, Range* b)
242 ExceptionCode ec = 0;
243 Range* start = a->compareBoundaryPoints(Range::START_TO_START, b, ec) <= 0 ? a : b;
245 Range* end = a->compareBoundaryPoints(Range::END_TO_END, b, ec) <= 0 ? b : a;
248 return Range::create(a->startContainer(ec)->ownerDocument(), start->startContainer(ec), start->startOffset(ec), end->endContainer(ec), end->endOffset(ec));
251 // Execute command functions
253 static bool executeBackColor(Frame* frame, Event*, EditorCommandSource source, const String& value)
255 return executeApplyStyle(frame, source, EditActionSetBackgroundColor, CSSPropertyBackgroundColor, value);
258 static bool executeCopy(Frame* frame, Event*, EditorCommandSource, const String&)
260 frame->editor()->copy();
264 static bool executeCreateLink(Frame* frame, Event*, EditorCommandSource, const String& value)
266 // FIXME: If userInterface is true, we should display a dialog box to let the user enter a URL.
269 applyCommand(CreateLinkCommand::create(frame->document(), value));
273 static bool executeCut(Frame* frame, Event*, EditorCommandSource, const String&)
275 frame->editor()->cut();
279 static bool executeDelete(Frame* frame, Event*, EditorCommandSource source, const String&)
282 case CommandFromMenuOrKeyBinding:
283 // Doesn't modify the text if the current selection isn't a range.
284 frame->editor()->performDelete();
287 case CommandFromDOMWithUserInterface:
288 // If the current selection is a caret, delete the preceding character. IE performs forwardDelete, but we currently side with Firefox.
289 // Doesn't scroll to make the selection visible, or modify the kill ring (this time, siding with IE, not Firefox).
290 TypingCommand::deleteKeyPressed(frame->document(), frame->selectionGranularity() == WordGranularity);
293 ASSERT_NOT_REACHED();
297 static bool executeDeleteBackward(Frame* frame, Event*, EditorCommandSource, const String&)
299 frame->editor()->deleteWithDirection(SelectionController::BACKWARD, CharacterGranularity, false, true);
303 static bool executeDeleteBackwardByDecomposingPreviousCharacter(Frame* frame, Event*, EditorCommandSource, const String&)
305 LOG_ERROR("DeleteBackwardByDecomposingPreviousCharacter is not implemented, doing DeleteBackward instead");
306 frame->editor()->deleteWithDirection(SelectionController::BACKWARD, CharacterGranularity, false, true);
310 static bool executeDeleteForward(Frame* frame, Event*, EditorCommandSource, const String&)
312 frame->editor()->deleteWithDirection(SelectionController::FORWARD, CharacterGranularity, false, true);
316 static bool executeDeleteToBeginningOfLine(Frame* frame, Event*, EditorCommandSource, const String&)
318 frame->editor()->deleteWithDirection(SelectionController::BACKWARD, LineBoundary, true, false);
322 static bool executeDeleteToBeginningOfParagraph(Frame* frame, Event*, EditorCommandSource, const String&)
324 frame->editor()->deleteWithDirection(SelectionController::BACKWARD, ParagraphBoundary, true, false);
328 static bool executeDeleteToEndOfLine(Frame* frame, Event*, EditorCommandSource, const String&)
330 // Despite its name, this command should delete the newline at the end of
331 // a paragraph if you are at the end of a paragraph (like DeleteToEndOfParagraph).
332 frame->editor()->deleteWithDirection(SelectionController::FORWARD, LineBoundary, true, false);
336 static bool executeDeleteToEndOfParagraph(Frame* frame, Event*, EditorCommandSource, const String&)
338 // Despite its name, this command should delete the newline at the end of
339 // a paragraph if you are at the end of a paragraph.
340 frame->editor()->deleteWithDirection(SelectionController::FORWARD, ParagraphBoundary, true, false);
344 static bool executeDeleteToMark(Frame* frame, Event*, EditorCommandSource, const String&)
346 RefPtr<Range> mark = frame->mark().toNormalizedRange();
348 SelectionController* selection = frame->selection();
349 bool selected = selection->setSelectedRange(unionDOMRanges(mark.get(), frame->editor()->selectedRange().get()).get(), DOWNSTREAM, true);
354 frame->editor()->performDelete();
355 frame->setMark(frame->selection()->selection());
359 static bool executeDeleteWordBackward(Frame* frame, Event*, EditorCommandSource, const String&)
361 frame->editor()->deleteWithDirection(SelectionController::BACKWARD, WordGranularity, true, false);
365 static bool executeDeleteWordForward(Frame* frame, Event*, EditorCommandSource, const String&)
367 frame->editor()->deleteWithDirection(SelectionController::FORWARD, WordGranularity, true, false);
371 static bool executeFindString(Frame* frame, Event*, EditorCommandSource, const String& value)
373 return frame->findString(value, true, false, true, false);
376 static bool executeFontName(Frame* frame, Event*, EditorCommandSource source, const String& value)
378 return executeApplyStyle(frame, source, EditActionSetFont, CSSPropertyFontFamily, value);
381 static bool executeFontSize(Frame* frame, Event*, EditorCommandSource source, const String& value)
384 if (!HTMLFontElement::cssValueFromFontSizeNumber(value, size))
386 return executeApplyStyle(frame, source, EditActionChangeAttributes, CSSPropertyFontSize, size);
389 static bool executeFontSizeDelta(Frame* frame, Event*, EditorCommandSource source, const String& value)
391 return executeApplyStyle(frame, source, EditActionChangeAttributes, CSSPropertyWebkitFontSizeDelta, value);
394 static bool executeForeColor(Frame* frame, Event*, EditorCommandSource source, const String& value)
396 return executeApplyStyle(frame, source, EditActionSetColor, CSSPropertyColor, value);
399 static bool executeFormatBlock(Frame* frame, Event*, EditorCommandSource, const String& value)
401 String tagName = value.lower();
402 if (tagName[0] == '<' && tagName[tagName.length() - 1] == '>')
403 tagName = tagName.substring(1, tagName.length() - 2);
404 if (!validBlockTag(tagName))
406 applyCommand(FormatBlockCommand::create(frame->document(), tagName));
410 static bool executeForwardDelete(Frame* frame, Event*, EditorCommandSource source, const String&)
413 case CommandFromMenuOrKeyBinding:
414 frame->editor()->deleteWithDirection(SelectionController::FORWARD, CharacterGranularity, false, true);
417 case CommandFromDOMWithUserInterface:
418 // Doesn't scroll to make the selection visible, or modify the kill ring.
419 // ForwardDelete is not implemented in IE or Firefox, so this behavior is only needed for
420 // backward compatibility with ourselves, and for consistency with Delete.
421 TypingCommand::forwardDeleteKeyPressed(frame->document());
424 ASSERT_NOT_REACHED();
428 static bool executeIgnoreSpelling(Frame* frame, Event*, EditorCommandSource, const String&)
430 frame->editor()->ignoreSpelling();
434 static bool executeIndent(Frame* frame, Event*, EditorCommandSource, const String&)
436 applyCommand(IndentOutdentCommand::create(frame->document(), IndentOutdentCommand::Indent));
440 static bool executeInsertBacktab(Frame* frame, Event* event, EditorCommandSource, const String&)
442 return targetFrame(frame, event)->eventHandler()->handleTextInputEvent("\t", event, false, true);
445 static bool executeInsertHorizontalRule(Frame* frame, Event*, EditorCommandSource, const String& value)
447 RefPtr<HTMLElement> hr = new HTMLElement(hrTag, frame->document());
448 if (!value.isEmpty())
450 return executeInsertNode(frame, hr.release());
453 static bool executeInsertHTML(Frame* frame, Event*, EditorCommandSource, const String& value)
455 return executeInsertFragment(frame, createFragmentFromMarkup(frame->document(), value, ""));
458 static bool executeInsertImage(Frame* frame, Event*, EditorCommandSource, const String& value)
460 // FIXME: If userInterface is true, we should display a dialog box and let the user choose a local image.
461 RefPtr<HTMLImageElement> image = new HTMLImageElement(imgTag, frame->document());
462 image->setSrc(value);
463 return executeInsertNode(frame, image.release());
466 static bool executeInsertLineBreak(Frame* frame, Event* event, EditorCommandSource source, const String&)
469 case CommandFromMenuOrKeyBinding:
470 return targetFrame(frame, event)->eventHandler()->handleTextInputEvent("\n", event, true);
472 case CommandFromDOMWithUserInterface:
473 // Doesn't scroll to make the selection visible, or modify the kill ring.
474 // InsertLineBreak is not implemented in IE or Firefox, so this behavior is only needed for
475 // backward compatibility with ourselves, and for consistency with other commands.
476 TypingCommand::insertLineBreak(frame->document());
479 ASSERT_NOT_REACHED();
483 static bool executeInsertNewline(Frame* frame, Event* event, EditorCommandSource, const String&)
485 Frame* targetFrame = WebCore::targetFrame(frame, event);
486 return targetFrame->eventHandler()->handleTextInputEvent("\n", event, !targetFrame->editor()->canEditRichly());
489 static bool executeInsertNewlineInQuotedContent(Frame* frame, Event*, EditorCommandSource, const String&)
491 TypingCommand::insertParagraphSeparatorInQuotedContent(frame->document());
495 static bool executeInsertOrderedList(Frame* frame, Event*, EditorCommandSource, const String&)
497 applyCommand(InsertListCommand::create(frame->document(), InsertListCommand::OrderedList));
501 static bool executeInsertParagraph(Frame* frame, Event*, EditorCommandSource, const String&)
503 TypingCommand::insertParagraphSeparator(frame->document());
507 static bool executeInsertTab(Frame* frame, Event* event, EditorCommandSource, const String&)
509 return targetFrame(frame, event)->eventHandler()->handleTextInputEvent("\t", event, false, false);
512 static bool executeInsertText(Frame* frame, Event*, EditorCommandSource, const String& value)
514 TypingCommand::insertText(frame->document(), value);
518 static bool executeInsertUnorderedList(Frame* frame, Event*, EditorCommandSource, const String&)
520 applyCommand(InsertListCommand::create(frame->document(), InsertListCommand::UnorderedList));
524 static bool executeJustifyCenter(Frame* frame, Event*, EditorCommandSource source, const String&)
526 return executeApplyParagraphStyle(frame, source, EditActionCenter, CSSPropertyTextAlign, "center");
529 static bool executeJustifyFull(Frame* frame, Event*, EditorCommandSource source, const String&)
531 return executeApplyParagraphStyle(frame, source, EditActionJustify, CSSPropertyTextAlign, "justify");
534 static bool executeJustifyLeft(Frame* frame, Event*, EditorCommandSource source, const String&)
536 return executeApplyParagraphStyle(frame, source, EditActionAlignLeft, CSSPropertyTextAlign, "left");
539 static bool executeJustifyRight(Frame* frame, Event*, EditorCommandSource source, const String&)
541 return executeApplyParagraphStyle(frame, source, EditActionAlignRight, CSSPropertyTextAlign, "right");
544 static bool executeMakeTextWritingDirectionLeftToRight(Frame* frame, Event*, EditorCommandSource, const String&)
546 RefPtr<CSSMutableStyleDeclaration> style = CSSMutableStyleDeclaration::create();
547 style->setProperty(CSSPropertyUnicodeBidi, CSSValueEmbed);
548 style->setProperty(CSSPropertyDirection, CSSValueLtr);
549 frame->editor()->applyStyle(style.get(), EditActionSetWritingDirection);
553 static bool executeMakeTextWritingDirectionNatural(Frame* frame, Event*, EditorCommandSource, const String&)
555 RefPtr<CSSMutableStyleDeclaration> style = CSSMutableStyleDeclaration::create();
556 style->setProperty(CSSPropertyUnicodeBidi, CSSValueNormal);
557 frame->editor()->applyStyle(style.get(), EditActionSetWritingDirection);
561 static bool executeMakeTextWritingDirectionRightToLeft(Frame* frame, Event*, EditorCommandSource, const String&)
563 RefPtr<CSSMutableStyleDeclaration> style = CSSMutableStyleDeclaration::create();
564 style->setProperty(CSSPropertyUnicodeBidi, CSSValueEmbed);
565 style->setProperty(CSSPropertyDirection, CSSValueRtl);
566 frame->editor()->applyStyle(style.get(), EditActionSetWritingDirection);
570 static bool executeMoveBackward(Frame* frame, Event*, EditorCommandSource, const String&)
572 frame->selection()->modify(SelectionController::MOVE, SelectionController::BACKWARD, CharacterGranularity, true);
576 static bool executeMoveBackwardAndModifySelection(Frame* frame, Event*, EditorCommandSource, const String&)
578 frame->selection()->modify(SelectionController::EXTEND, SelectionController::BACKWARD, CharacterGranularity, true);
582 static bool executeMoveDown(Frame* frame, Event*, EditorCommandSource, const String&)
584 frame->selection()->modify(SelectionController::MOVE, SelectionController::FORWARD, LineGranularity, true);
588 static bool executeMoveDownAndModifySelection(Frame* frame, Event*, EditorCommandSource, const String&)
590 frame->selection()->modify(SelectionController::EXTEND, SelectionController::FORWARD, LineGranularity, true);
594 static bool executeMoveForward(Frame* frame, Event*, EditorCommandSource, const String&)
596 frame->selection()->modify(SelectionController::MOVE, SelectionController::FORWARD, CharacterGranularity, true);
600 static bool executeMoveForwardAndModifySelection(Frame* frame, Event*, EditorCommandSource, const String&)
602 frame->selection()->modify(SelectionController::EXTEND, SelectionController::FORWARD, CharacterGranularity, true);
606 static bool executeMoveLeft(Frame* frame, Event*, EditorCommandSource, const String&)
608 frame->selection()->modify(SelectionController::MOVE, SelectionController::LEFT, CharacterGranularity, true);
612 static bool executeMoveLeftAndModifySelection(Frame* frame, Event*, EditorCommandSource, const String&)
614 frame->selection()->modify(SelectionController::EXTEND, SelectionController::LEFT, CharacterGranularity, true);
618 static bool executeMovePageDown(Frame* frame, Event*, EditorCommandSource, const String&)
620 int distance = verticalScrollDistance(frame);
623 return frame->selection()->modify(SelectionController::MOVE, distance, true);
626 static bool executeMovePageDownAndModifySelection(Frame* frame, Event*, EditorCommandSource, const String&)
628 int distance = verticalScrollDistance(frame);
631 return frame->selection()->modify(SelectionController::EXTEND, distance, true);
634 static bool executeMovePageUp(Frame* frame, Event*, EditorCommandSource, const String&)
636 int distance = verticalScrollDistance(frame);
639 return frame->selection()->modify(SelectionController::MOVE, -distance, true);
642 static bool executeMovePageUpAndModifySelection(Frame* frame, Event*, EditorCommandSource, const String&)
644 int distance = verticalScrollDistance(frame);
647 return frame->selection()->modify(SelectionController::EXTEND, -distance, true);
650 static bool executeMoveRight(Frame* frame, Event*, EditorCommandSource, const String&)
652 frame->selection()->modify(SelectionController::MOVE, SelectionController::RIGHT, CharacterGranularity, true);
656 static bool executeMoveRightAndModifySelection(Frame* frame, Event*, EditorCommandSource, const String&)
658 frame->selection()->modify(SelectionController::EXTEND, SelectionController::RIGHT, CharacterGranularity, true);
662 static bool executeMoveToBeginningOfDocument(Frame* frame, Event*, EditorCommandSource, const String&)
664 frame->selection()->modify(SelectionController::MOVE, SelectionController::BACKWARD, DocumentBoundary, true);
668 static bool executeMoveToBeginningOfDocumentAndModifySelection(Frame* frame, Event*, EditorCommandSource, const String&)
670 frame->selection()->modify(SelectionController::EXTEND, SelectionController::BACKWARD, DocumentBoundary, true);
674 static bool executeMoveToBeginningOfLine(Frame* frame, Event*, EditorCommandSource, const String&)
676 frame->selection()->modify(SelectionController::MOVE, SelectionController::BACKWARD, LineBoundary, true);
680 static bool executeMoveToBeginningOfLineAndModifySelection(Frame* frame, Event*, EditorCommandSource, const String&)
682 frame->selection()->modify(SelectionController::EXTEND, SelectionController::BACKWARD, LineBoundary, true);
686 static bool executeMoveToBeginningOfParagraph(Frame* frame, Event*, EditorCommandSource, const String&)
688 frame->selection()->modify(SelectionController::MOVE, SelectionController::BACKWARD, ParagraphBoundary, true);
692 static bool executeMoveToBeginningOfParagraphAndModifySelection(Frame* frame, Event*, EditorCommandSource, const String&)
694 frame->selection()->modify(SelectionController::EXTEND, SelectionController::BACKWARD, ParagraphBoundary, true);
698 static bool executeMoveToBeginningOfSentence(Frame* frame, Event*, EditorCommandSource, const String&)
700 frame->selection()->modify(SelectionController::MOVE, SelectionController::BACKWARD, SentenceBoundary, true);
704 static bool executeMoveToBeginningOfSentenceAndModifySelection(Frame* frame, Event*, EditorCommandSource, const String&)
706 frame->selection()->modify(SelectionController::EXTEND, SelectionController::BACKWARD, SentenceBoundary, true);
710 static bool executeMoveToEndOfDocument(Frame* frame, Event*, EditorCommandSource, const String&)
712 frame->selection()->modify(SelectionController::MOVE, SelectionController::FORWARD, DocumentBoundary, true);
716 static bool executeMoveToEndOfDocumentAndModifySelection(Frame* frame, Event*, EditorCommandSource, const String&)
718 frame->selection()->modify(SelectionController::EXTEND, SelectionController::FORWARD, DocumentBoundary, true);
722 static bool executeMoveToEndOfSentence(Frame* frame, Event*, EditorCommandSource, const String&)
724 frame->selection()->modify(SelectionController::MOVE, SelectionController::FORWARD, SentenceBoundary, true);
728 static bool executeMoveToEndOfSentenceAndModifySelection(Frame* frame, Event*, EditorCommandSource, const String&)
730 frame->selection()->modify(SelectionController::EXTEND, SelectionController::FORWARD, SentenceBoundary, true);
734 static bool executeMoveToEndOfLine(Frame* frame, Event*, EditorCommandSource, const String&)
736 frame->selection()->modify(SelectionController::MOVE, SelectionController::FORWARD, LineBoundary, true);
740 static bool executeMoveToEndOfLineAndModifySelection(Frame* frame, Event*, EditorCommandSource, const String&)
742 frame->selection()->modify(SelectionController::EXTEND, SelectionController::FORWARD, LineBoundary, true);
746 static bool executeMoveToEndOfParagraph(Frame* frame, Event*, EditorCommandSource, const String&)
748 frame->selection()->modify(SelectionController::MOVE, SelectionController::FORWARD, ParagraphBoundary, true);
752 static bool executeMoveToEndOfParagraphAndModifySelection(Frame* frame, Event*, EditorCommandSource, const String&)
754 frame->selection()->modify(SelectionController::EXTEND, SelectionController::FORWARD, ParagraphBoundary, true);
758 static bool executeMoveParagraphBackwardAndModifySelection(Frame* frame, Event*, EditorCommandSource, const String&)
760 frame->selection()->modify(SelectionController::EXTEND, SelectionController::BACKWARD, ParagraphGranularity, true);
764 static bool executeMoveParagraphForwardAndModifySelection(Frame* frame, Event*, EditorCommandSource, const String&)
766 frame->selection()->modify(SelectionController::EXTEND, SelectionController::FORWARD, ParagraphGranularity, true);
770 static bool executeMoveUp(Frame* frame, Event*, EditorCommandSource, const String&)
772 frame->selection()->modify(SelectionController::MOVE, SelectionController::BACKWARD, LineGranularity, true);
776 static bool executeMoveUpAndModifySelection(Frame* frame, Event*, EditorCommandSource, const String&)
778 frame->selection()->modify(SelectionController::EXTEND, SelectionController::BACKWARD, LineGranularity, true);
782 static bool executeMoveWordBackward(Frame* frame, Event*, EditorCommandSource, const String&)
784 frame->selection()->modify(SelectionController::MOVE, SelectionController::BACKWARD, WordGranularity, true);
788 static bool executeMoveWordBackwardAndModifySelection(Frame* frame, Event*, EditorCommandSource, const String&)
790 frame->selection()->modify(SelectionController::EXTEND, SelectionController::BACKWARD, WordGranularity, true);
794 static bool executeMoveWordForward(Frame* frame, Event*, EditorCommandSource, const String&)
796 frame->selection()->modify(SelectionController::MOVE, SelectionController::FORWARD, WordGranularity, true);
800 static bool executeMoveWordForwardAndModifySelection(Frame* frame, Event*, EditorCommandSource, const String&)
802 frame->selection()->modify(SelectionController::EXTEND, SelectionController::FORWARD, WordGranularity, true);
806 static bool executeMoveWordLeft(Frame* frame, Event*, EditorCommandSource, const String&)
808 frame->selection()->modify(SelectionController::MOVE, SelectionController::LEFT, WordGranularity, true);
812 static bool executeMoveWordLeftAndModifySelection(Frame* frame, Event*, EditorCommandSource, const String&)
814 frame->selection()->modify(SelectionController::EXTEND, SelectionController::LEFT, WordGranularity, true);
818 static bool executeMoveWordRight(Frame* frame, Event*, EditorCommandSource, const String&)
820 frame->selection()->modify(SelectionController::MOVE, SelectionController::RIGHT, WordGranularity, true);
824 static bool executeMoveWordRightAndModifySelection(Frame* frame, Event*, EditorCommandSource, const String&)
826 frame->selection()->modify(SelectionController::EXTEND, SelectionController::RIGHT, WordGranularity, true);
830 static bool executeOutdent(Frame* frame, Event*, EditorCommandSource, const String&)
832 applyCommand(IndentOutdentCommand::create(frame->document(), IndentOutdentCommand::Outdent));
836 static bool executePaste(Frame* frame, Event*, EditorCommandSource, const String&)
838 frame->editor()->paste();
842 static bool executePasteAndMatchStyle(Frame* frame, Event*, EditorCommandSource, const String&)
844 frame->editor()->pasteAsPlainText();
848 static bool executePrint(Frame* frame, Event*, EditorCommandSource, const String&)
850 Page* page = frame->page();
853 page->chrome()->print(frame);
857 static bool executeRedo(Frame* frame, Event*, EditorCommandSource, const String&)
859 frame->editor()->redo();
863 static bool executeRemoveFormat(Frame* frame, Event*, EditorCommandSource, const String&)
865 frame->editor()->removeFormattingAndStyle();
869 static bool executeSelectAll(Frame* frame, Event*, EditorCommandSource, const String&)
871 frame->selection()->selectAll();
875 static bool executeSelectLine(Frame* frame, Event*, EditorCommandSource, const String&)
877 return expandSelectionToGranularity(frame, LineGranularity);
880 static bool executeSelectParagraph(Frame* frame, Event*, EditorCommandSource, const String&)
882 return expandSelectionToGranularity(frame, ParagraphGranularity);
885 static bool executeSelectSentence(Frame* frame, Event*, EditorCommandSource, const String&)
887 return expandSelectionToGranularity(frame, SentenceGranularity);
890 static bool executeSelectToMark(Frame* frame, Event*, EditorCommandSource, const String&)
892 RefPtr<Range> mark = frame->mark().toNormalizedRange();
893 RefPtr<Range> selection = frame->editor()->selectedRange();
894 if (!mark || !selection) {
898 frame->selection()->setSelectedRange(unionDOMRanges(mark.get(), selection.get()).get(), DOWNSTREAM, true);
902 static bool executeSelectWord(Frame* frame, Event*, EditorCommandSource, const String&)
904 return expandSelectionToGranularity(frame, WordGranularity);
907 static bool executeSetMark(Frame* frame, Event*, EditorCommandSource, const String&)
909 frame->setMark(frame->selection()->selection());
913 static bool executeStrikethrough(Frame* frame, Event*, EditorCommandSource source, const String&)
915 return executeToggleStyle(frame, source, EditActionChangeAttributes, CSSPropertyWebkitTextDecorationsInEffect, "none", "line-through");
918 static bool executeStyleWithCSS(Frame* frame, Event*, EditorCommandSource, const String& value)
920 if (value != "false" && value != "true")
923 frame->editor()->setShouldStyleWithCSS(value == "true" ? true : false);
927 static bool executeSubscript(Frame* frame, Event*, EditorCommandSource source, const String&)
929 return executeToggleStyle(frame, source, EditActionSubscript, CSSPropertyVerticalAlign, "baseline", "sub");
932 static bool executeSuperscript(Frame* frame, Event*, EditorCommandSource source, const String&)
934 return executeToggleStyle(frame, source, EditActionSuperscript, CSSPropertyVerticalAlign, "baseline", "super");
937 static bool executeSwapWithMark(Frame* frame, Event*, EditorCommandSource, const String&)
939 const VisibleSelection& mark = frame->mark();
940 const VisibleSelection& selection = frame->selection()->selection();
941 if (mark.isNone() || selection.isNone()) {
945 frame->selection()->setSelection(mark);
946 frame->setMark(selection);
950 static bool executeToggleBold(Frame* frame, Event*, EditorCommandSource source, const String&)
952 return executeToggleStyle(frame, source, EditActionChangeAttributes, CSSPropertyFontWeight, "normal", "bold");
955 static bool executeToggleItalic(Frame* frame, Event*, EditorCommandSource source, const String&)
957 return executeToggleStyle(frame, source, EditActionChangeAttributes, CSSPropertyFontStyle, "normal", "italic");
960 static bool executeTranspose(Frame* frame, Event*, EditorCommandSource, const String&)
962 frame->editor()->transpose();
966 static bool executeUnderline(Frame* frame, Event*, EditorCommandSource source, const String&)
968 // FIXME: This currently clears overline, line-through, and blink as an unwanted side effect.
969 return executeToggleStyle(frame, source, EditActionUnderline, CSSPropertyWebkitTextDecorationsInEffect, "none", "underline");
972 static bool executeUndo(Frame* frame, Event*, EditorCommandSource, const String&)
974 frame->editor()->undo();
978 static bool executeUnlink(Frame* frame, Event*, EditorCommandSource, const String&)
980 applyCommand(UnlinkCommand::create(frame->document()));
984 static bool executeUnscript(Frame* frame, Event*, EditorCommandSource source, const String&)
986 return executeApplyStyle(frame, source, EditActionUnscript, CSSPropertyVerticalAlign, "baseline");
989 static bool executeUnselect(Frame* frame, Event*, EditorCommandSource, const String&)
991 frame->selection()->clear();
995 static bool executeYank(Frame* frame, Event*, EditorCommandSource, const String&)
997 frame->editor()->insertTextWithoutSendingTextEvent(frame->editor()->yankFromKillRing(), false, 0);
998 frame->editor()->setKillRingToYankedState();
1002 static bool executeYankAndSelect(Frame* frame, Event*, EditorCommandSource, const String&)
1004 frame->editor()->insertTextWithoutSendingTextEvent(frame->editor()->yankFromKillRing(), true, 0);
1005 frame->editor()->setKillRingToYankedState();
1009 // Supported functions
1011 static bool supported(Frame*, EditorCommandSource)
1016 static bool supportedFromMenuOrKeyBinding(Frame*, EditorCommandSource source)
1018 return source == CommandFromMenuOrKeyBinding;
1021 static bool supportedPaste(Frame* frame, EditorCommandSource source)
1024 case CommandFromMenuOrKeyBinding:
1026 case CommandFromDOM:
1027 case CommandFromDOMWithUserInterface: {
1028 Settings* settings = frame ? frame->settings() : 0;
1029 return settings && settings->isDOMPasteAllowed();
1032 ASSERT_NOT_REACHED();
1036 // Enabled functions
1038 static bool enabled(Frame*, Event*, EditorCommandSource)
1043 static bool enabledVisibleSelection(Frame* frame, Event* event, EditorCommandSource)
1045 // The term "visible" here includes a caret in editable text or a range in any text.
1046 const VisibleSelection& selection = frame->editor()->selectionForCommand(event);
1047 return (selection.isCaret() && selection.isContentEditable()) || selection.isRange();
1050 static bool enabledVisibleSelectionAndMark(Frame* frame, Event* event, EditorCommandSource)
1052 const VisibleSelection& selection = frame->editor()->selectionForCommand(event);
1053 return ((selection.isCaret() && selection.isContentEditable()) || selection.isRange())
1054 && frame->mark().isCaretOrRange();
1057 static bool enableCaretInEditableText(Frame* frame, Event* event, EditorCommandSource)
1059 const VisibleSelection& selection = frame->editor()->selectionForCommand(event);
1060 return selection.isCaret() && selection.isContentEditable();
1063 static bool enabledCopy(Frame* frame, Event*, EditorCommandSource)
1065 return frame->editor()->canDHTMLCopy() || frame->editor()->canCopy();
1068 static bool enabledCut(Frame* frame, Event*, EditorCommandSource)
1070 return frame->editor()->canDHTMLCut() || frame->editor()->canCut();
1073 static bool enabledDelete(Frame* frame, Event* event, EditorCommandSource source)
1076 case CommandFromMenuOrKeyBinding:
1077 // "Delete" from menu only affects selected range, just like Cut but without affecting pasteboard
1078 return frame->editor()->canDHTMLCut() || frame->editor()->canCut();
1079 case CommandFromDOM:
1080 case CommandFromDOMWithUserInterface:
1081 // "Delete" from DOM is like delete/backspace keypress, affects selected range if non-empty,
1082 // otherwise removes a character
1083 return frame->editor()->selectionForCommand(event).isContentEditable();
1085 ASSERT_NOT_REACHED();
1089 static bool enabledInEditableText(Frame* frame, Event* event, EditorCommandSource)
1091 return frame->editor()->selectionForCommand(event).isContentEditable();
1094 static bool enabledInRichlyEditableText(Frame* frame, Event*, EditorCommandSource)
1096 return frame->selection()->isCaretOrRange() && frame->selection()->isContentRichlyEditable();
1099 static bool enabledPaste(Frame* frame, Event*, EditorCommandSource)
1101 return frame->editor()->canPaste();
1104 static bool enabledRangeInEditableText(Frame* frame, Event*, EditorCommandSource)
1106 return frame->selection()->isRange() && frame->selection()->isContentEditable();
1109 static bool enabledRangeInRichlyEditableText(Frame* frame, Event*, EditorCommandSource)
1111 return frame->selection()->isRange() && frame->selection()->isContentRichlyEditable();
1114 static bool enabledRedo(Frame* frame, Event*, EditorCommandSource)
1116 return frame->editor()->canRedo();
1119 static bool enabledUndo(Frame* frame, Event*, EditorCommandSource)
1121 return frame->editor()->canUndo();
1126 static TriState stateNone(Frame*, Event*)
1128 return FalseTriState;
1131 static TriState stateBold(Frame* frame, Event*)
1133 return stateStyle(frame, CSSPropertyFontWeight, "bold");
1136 static TriState stateItalic(Frame* frame, Event*)
1138 return stateStyle(frame, CSSPropertyFontStyle, "italic");
1141 static TriState stateOrderedList(Frame* frame, Event*)
1143 return frame->editor()->selectionOrderedListState();
1146 static TriState stateStrikethrough(Frame* frame, Event*)
1148 return stateStyle(frame, CSSPropertyTextDecoration, "line-through");
1151 static TriState stateStyleWithCSS(Frame* frame, Event*)
1153 return frame->editor()->shouldStyleWithCSS() ? TrueTriState : FalseTriState;
1156 static TriState stateSubscript(Frame* frame, Event*)
1158 return stateStyle(frame, CSSPropertyVerticalAlign, "sub");
1161 static TriState stateSuperscript(Frame* frame, Event*)
1163 return stateStyle(frame, CSSPropertyVerticalAlign, "super");
1166 static TriState stateTextWritingDirectionLeftToRight(Frame* frame, Event*)
1168 return stateTextWritingDirection(frame, LeftToRightWritingDirection);
1171 static TriState stateTextWritingDirectionNatural(Frame* frame, Event*)
1173 return stateTextWritingDirection(frame, NaturalWritingDirection);
1176 static TriState stateTextWritingDirectionRightToLeft(Frame* frame, Event*)
1178 return stateTextWritingDirection(frame, RightToLeftWritingDirection);
1181 static TriState stateUnderline(Frame* frame, Event*)
1183 return stateStyle(frame, CSSPropertyTextDecoration, "underline");
1186 static TriState stateUnorderedList(Frame* frame, Event*)
1188 return frame->editor()->selectionUnorderedListState();
1193 static String valueNull(Frame*, Event*)
1198 static String valueBackColor(Frame* frame, Event*)
1200 return valueStyle(frame, CSSPropertyBackgroundColor);
1203 static String valueFontName(Frame* frame, Event*)
1205 return valueStyle(frame, CSSPropertyFontFamily);
1208 static String valueFontSize(Frame* frame, Event*)
1210 return valueStyle(frame, CSSPropertyFontSize);
1213 static String valueFontSizeDelta(Frame* frame, Event*)
1215 return valueStyle(frame, CSSPropertyWebkitFontSizeDelta);
1218 static String valueForeColor(Frame* frame, Event*)
1220 return valueStyle(frame, CSSPropertyColor);
1225 static const CommandMap& createCommandMap()
1227 struct CommandEntry { const char* name; EditorInternalCommand command; };
1229 static const CommandEntry commands[] = {
1230 { "AlignCenter", { executeJustifyCenter, supportedFromMenuOrKeyBinding, enabledInRichlyEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1231 { "AlignJustified", { executeJustifyFull, supportedFromMenuOrKeyBinding, enabledInRichlyEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1232 { "AlignLeft", { executeJustifyLeft, supportedFromMenuOrKeyBinding, enabledInRichlyEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1233 { "AlignRight", { executeJustifyRight, supportedFromMenuOrKeyBinding, enabledInRichlyEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1234 { "BackColor", { executeBackColor, supported, enabledInRichlyEditableText, stateNone, valueBackColor, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1235 { "BackwardDelete", { executeDeleteBackward, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } }, // FIXME: remove BackwardDelete when Safari for Windows stops using it.
1236 { "Bold", { executeToggleBold, supported, enabledInRichlyEditableText, stateBold, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1237 { "Copy", { executeCopy, supported, enabledCopy, stateNone, valueNull, notTextInsertion, allowExecutionWhenDisabled } },
1238 { "CreateLink", { executeCreateLink, supported, enabledInRichlyEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1239 { "Cut", { executeCut, supported, enabledCut, stateNone, valueNull, notTextInsertion, allowExecutionWhenDisabled } },
1240 { "Delete", { executeDelete, supported, enabledDelete, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1241 { "DeleteBackward", { executeDeleteBackward, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1242 { "DeleteBackwardByDecomposingPreviousCharacter", { executeDeleteBackwardByDecomposingPreviousCharacter, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1243 { "DeleteForward", { executeDeleteForward, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1244 { "DeleteToBeginningOfLine", { executeDeleteToBeginningOfLine, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1245 { "DeleteToBeginningOfParagraph", { executeDeleteToBeginningOfParagraph, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1246 { "DeleteToEndOfLine", { executeDeleteToEndOfLine, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1247 { "DeleteToEndOfParagraph", { executeDeleteToEndOfParagraph, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1248 { "DeleteToMark", { executeDeleteToMark, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1249 { "DeleteWordBackward", { executeDeleteWordBackward, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1250 { "DeleteWordForward", { executeDeleteWordForward, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1251 { "FindString", { executeFindString, supported, enabled, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1252 { "FontName", { executeFontName, supported, enabledInEditableText, stateNone, valueFontName, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1253 { "FontSize", { executeFontSize, supported, enabledInEditableText, stateNone, valueFontSize, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1254 { "FontSizeDelta", { executeFontSizeDelta, supported, enabledInEditableText, stateNone, valueFontSizeDelta, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1255 { "ForeColor", { executeForeColor, supported, enabledInRichlyEditableText, stateNone, valueForeColor, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1256 { "FormatBlock", { executeFormatBlock, supported, enabledInRichlyEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1257 { "ForwardDelete", { executeForwardDelete, supported, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1258 { "HiliteColor", { executeBackColor, supported, enabledInRichlyEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1259 { "IgnoreSpelling", { executeIgnoreSpelling, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1260 { "Indent", { executeIndent, supported, enabledInRichlyEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1261 { "InsertBacktab", { executeInsertBacktab, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, isTextInsertion, doNotAllowExecutionWhenDisabled } },
1262 { "InsertHorizontalRule", { executeInsertHorizontalRule, supported, enabledInRichlyEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1263 { "InsertHTML", { executeInsertHTML, supported, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1264 { "InsertImage", { executeInsertImage, supported, enabledInRichlyEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1265 { "InsertLineBreak", { executeInsertLineBreak, supported, enabledInEditableText, stateNone, valueNull, isTextInsertion, doNotAllowExecutionWhenDisabled } },
1266 { "InsertNewline", { executeInsertNewline, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, isTextInsertion, doNotAllowExecutionWhenDisabled } },
1267 { "InsertNewlineInQuotedContent", { executeInsertNewlineInQuotedContent, supported, enabledInRichlyEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1268 { "InsertOrderedList", { executeInsertOrderedList, supported, enabledInRichlyEditableText, stateOrderedList, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1269 { "InsertParagraph", { executeInsertParagraph, supported, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1270 { "InsertTab", { executeInsertTab, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, isTextInsertion, doNotAllowExecutionWhenDisabled } },
1271 { "InsertText", { executeInsertText, supported, enabledInEditableText, stateNone, valueNull, isTextInsertion, doNotAllowExecutionWhenDisabled } },
1272 { "InsertUnorderedList", { executeInsertUnorderedList, supported, enabledInRichlyEditableText, stateUnorderedList, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1273 { "Italic", { executeToggleItalic, supported, enabledInRichlyEditableText, stateItalic, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1274 { "JustifyCenter", { executeJustifyCenter, supported, enabledInRichlyEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1275 { "JustifyFull", { executeJustifyFull, supported, enabledInRichlyEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1276 { "JustifyLeft", { executeJustifyLeft, supported, enabledInRichlyEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1277 { "JustifyNone", { executeJustifyLeft, supported, enabledInRichlyEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1278 { "JustifyRight", { executeJustifyRight, supported, enabledInRichlyEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1279 { "MakeTextWritingDirectionLeftToRight", { executeMakeTextWritingDirectionLeftToRight, supportedFromMenuOrKeyBinding, enabledInRichlyEditableText, stateTextWritingDirectionLeftToRight, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1280 { "MakeTextWritingDirectionNatural", { executeMakeTextWritingDirectionNatural, supportedFromMenuOrKeyBinding, enabledInRichlyEditableText, stateTextWritingDirectionNatural, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1281 { "MakeTextWritingDirectionRightToLeft", { executeMakeTextWritingDirectionRightToLeft, supportedFromMenuOrKeyBinding, enabledInRichlyEditableText, stateTextWritingDirectionRightToLeft, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1282 { "MoveBackward", { executeMoveBackward, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1283 { "MoveBackwardAndModifySelection", { executeMoveBackwardAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelection, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1284 { "MoveDown", { executeMoveDown, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1285 { "MoveDownAndModifySelection", { executeMoveDownAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelection, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1286 { "MoveForward", { executeMoveForward, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1287 { "MoveForwardAndModifySelection", { executeMoveForwardAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelection, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1288 { "MoveLeft", { executeMoveLeft, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1289 { "MoveLeftAndModifySelection", { executeMoveLeftAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelection, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1290 { "MovePageDown", { executeMovePageDown, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1291 { "MovePageDownAndModifySelection", { executeMovePageDownAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelection, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1292 { "MovePageUp", { executeMovePageUp, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1293 { "MovePageUpAndModifySelection", { executeMovePageUpAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelection, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1294 { "MoveParagraphBackwardAndModifySelection", { executeMoveParagraphBackwardAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelection, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1295 { "MoveParagraphForwardAndModifySelection", { executeMoveParagraphForwardAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelection, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1296 { "MoveRight", { executeMoveRight, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1297 { "MoveRightAndModifySelection", { executeMoveRightAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelection, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1298 { "MoveToBeginningOfDocument", { executeMoveToBeginningOfDocument, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1299 { "MoveToBeginningOfDocumentAndModifySelection", { executeMoveToBeginningOfDocumentAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelection, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1300 { "MoveToBeginningOfLine", { executeMoveToBeginningOfLine, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1301 { "MoveToBeginningOfLineAndModifySelection", { executeMoveToBeginningOfLineAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelection, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1302 { "MoveToBeginningOfParagraph", { executeMoveToBeginningOfParagraph, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1303 { "MoveToBeginningOfParagraphAndModifySelection", { executeMoveToBeginningOfParagraphAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelection, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1304 { "MoveToBeginningOfSentence", { executeMoveToBeginningOfSentence, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1305 { "MoveToBeginningOfSentenceAndModifySelection", { executeMoveToBeginningOfSentenceAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelection, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1306 { "MoveToEndOfDocument", { executeMoveToEndOfDocument, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1307 { "MoveToEndOfDocumentAndModifySelection", { executeMoveToEndOfDocumentAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelection, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1308 { "MoveToEndOfLine", { executeMoveToEndOfLine, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1309 { "MoveToEndOfLineAndModifySelection", { executeMoveToEndOfLineAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelection, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1310 { "MoveToEndOfParagraph", { executeMoveToEndOfParagraph, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1311 { "MoveToEndOfParagraphAndModifySelection", { executeMoveToEndOfParagraphAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelection, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1312 { "MoveToEndOfSentence", { executeMoveToEndOfSentence, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1313 { "MoveToEndOfSentenceAndModifySelection", { executeMoveToEndOfSentenceAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelection, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1314 { "MoveUp", { executeMoveUp, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1315 { "MoveUpAndModifySelection", { executeMoveUpAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelection, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1316 { "MoveWordBackward", { executeMoveWordBackward, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1317 { "MoveWordBackwardAndModifySelection", { executeMoveWordBackwardAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelection, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1318 { "MoveWordForward", { executeMoveWordForward, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1319 { "MoveWordForwardAndModifySelection", { executeMoveWordForwardAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelection, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1320 { "MoveWordLeft", { executeMoveWordLeft, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1321 { "MoveWordLeftAndModifySelection", { executeMoveWordLeftAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelection, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1322 { "MoveWordRight", { executeMoveWordRight, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1323 { "MoveWordRightAndModifySelection", { executeMoveWordRightAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelection, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1324 { "Outdent", { executeOutdent, supported, enabledInRichlyEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1325 { "Paste", { executePaste, supportedPaste, enabledPaste, stateNone, valueNull, notTextInsertion, allowExecutionWhenDisabled } },
1326 { "PasteAndMatchStyle", { executePasteAndMatchStyle, supportedPaste, enabledPaste, stateNone, valueNull, notTextInsertion, allowExecutionWhenDisabled } },
1327 { "Print", { executePrint, supported, enabled, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1328 { "Redo", { executeRedo, supported, enabledRedo, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1329 { "RemoveFormat", { executeRemoveFormat, supported, enabledRangeInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1330 { "SelectAll", { executeSelectAll, supported, enabled, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1331 { "SelectLine", { executeSelectLine, supportedFromMenuOrKeyBinding, enabledVisibleSelection, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1332 { "SelectParagraph", { executeSelectParagraph, supportedFromMenuOrKeyBinding, enabledVisibleSelection, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1333 { "SelectSentence", { executeSelectSentence, supportedFromMenuOrKeyBinding, enabledVisibleSelection, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1334 { "SelectToMark", { executeSelectToMark, supportedFromMenuOrKeyBinding, enabledVisibleSelectionAndMark, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1335 { "SelectWord", { executeSelectWord, supportedFromMenuOrKeyBinding, enabledVisibleSelection, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1336 { "SetMark", { executeSetMark, supportedFromMenuOrKeyBinding, enabledVisibleSelection, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1337 { "Strikethrough", { executeStrikethrough, supported, enabledInRichlyEditableText, stateStrikethrough, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1338 { "StyleWithCSS", { executeStyleWithCSS, supported, enabledInRichlyEditableText, stateStyleWithCSS, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1339 { "Subscript", { executeSubscript, supported, enabledInRichlyEditableText, stateSubscript, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1340 { "Superscript", { executeSuperscript, supported, enabledInRichlyEditableText, stateSuperscript, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1341 { "SwapWithMark", { executeSwapWithMark, supportedFromMenuOrKeyBinding, enabledVisibleSelectionAndMark, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1342 { "ToggleBold", { executeToggleBold, supportedFromMenuOrKeyBinding, enabledInRichlyEditableText, stateBold, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1343 { "ToggleItalic", { executeToggleItalic, supportedFromMenuOrKeyBinding, enabledInRichlyEditableText, stateItalic, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1344 { "ToggleUnderline", { executeUnderline, supportedFromMenuOrKeyBinding, enabledInRichlyEditableText, stateUnderline, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1345 { "Transpose", { executeTranspose, supported, enableCaretInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1346 { "Underline", { executeUnderline, supported, enabledInRichlyEditableText, stateUnderline, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1347 { "Undo", { executeUndo, supported, enabledUndo, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1348 { "Unlink", { executeUnlink, supported, enabledRangeInRichlyEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1349 { "Unscript", { executeUnscript, supportedFromMenuOrKeyBinding, enabledInRichlyEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1350 { "Unselect", { executeUnselect, supported, enabledVisibleSelection, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1351 { "Yank", { executeYank, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1352 { "YankAndSelect", { executeYankAndSelect, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1355 // These unsupported commands are listed here since they appear in the Microsoft
1356 // documentation used as the starting point for our DOM executeCommand support.
1358 // 2D-Position (not supported)
1359 // AbsolutePosition (not supported)
1360 // BlockDirLTR (not supported)
1361 // BlockDirRTL (not supported)
1362 // BrowseMode (not supported)
1363 // ClearAuthenticationCache (not supported)
1364 // CreateBookmark (not supported)
1365 // DirLTR (not supported)
1366 // DirRTL (not supported)
1367 // EditMode (not supported)
1368 // InlineDirLTR (not supported)
1369 // InlineDirRTL (not supported)
1370 // InsertButton (not supported)
1371 // InsertFieldSet (not supported)
1372 // InsertIFrame (not supported)
1373 // InsertInputButton (not supported)
1374 // InsertInputCheckbox (not supported)
1375 // InsertInputFileUpload (not supported)
1376 // InsertInputHidden (not supported)
1377 // InsertInputImage (not supported)
1378 // InsertInputPassword (not supported)
1379 // InsertInputRadio (not supported)
1380 // InsertInputReset (not supported)
1381 // InsertInputSubmit (not supported)
1382 // InsertInputText (not supported)
1383 // InsertMarquee (not supported)
1384 // InsertSelectDropDown (not supported)
1385 // InsertSelectListBox (not supported)
1386 // InsertTextArea (not supported)
1387 // LiveResize (not supported)
1388 // MultipleSelection (not supported)
1389 // Open (not supported)
1390 // Overwrite (not supported)
1391 // PlayImage (not supported)
1392 // Refresh (not supported)
1393 // RemoveParaFormat (not supported)
1394 // SaveAs (not supported)
1395 // SizeToControl (not supported)
1396 // SizeToControlHeight (not supported)
1397 // SizeToControlWidth (not supported)
1398 // Stop (not supported)
1399 // StopImage (not supported)
1400 // Unbookmark (not supported)
1402 CommandMap& commandMap = *new CommandMap;
1404 const unsigned numCommands = sizeof(commands) / sizeof(commands[0]);
1405 for (unsigned i = 0; i < numCommands; i++) {
1406 ASSERT(!commandMap.get(commands[i].name));
1407 commandMap.set(commands[i].name, &commands[i].command);
1413 Editor::Command Editor::command(const String& commandName)
1415 return command(commandName, CommandFromMenuOrKeyBinding);
1418 Editor::Command Editor::command(const String& commandName, EditorCommandSource source)
1420 if (commandName.isEmpty())
1423 static const CommandMap& commandMap = createCommandMap();
1424 const EditorInternalCommand* internalCommand = commandMap.get(commandName);
1425 return internalCommand ? Command(m_frame, internalCommand, source) : Command();
1428 Editor::Command::Command()
1434 Editor::Command::Command(PassRefPtr<Frame> frame, const EditorInternalCommand* command, EditorCommandSource source)
1436 , m_command(command)
1443 bool Editor::Command::execute(const String& parameter, Event* triggeringEvent) const
1445 if (!isEnabled(triggeringEvent)) {
1446 // Let certain commands be executed when performed explicitly even if they are disabled.
1447 if (!isSupported() || !m_frame || !m_command->allowExecutionWhenDisabled)
1450 m_frame->document()->updateLayoutIgnorePendingStylesheets();
1451 return m_command->execute(m_frame.get(), triggeringEvent, m_source, parameter);
1454 bool Editor::Command::execute(Event* triggeringEvent) const
1456 return execute(String(), triggeringEvent);
1459 bool Editor::Command::isSupported() const
1461 return m_command && m_command->isSupported(m_frame.get(), m_source);
1464 bool Editor::Command::isEnabled(Event* triggeringEvent) const
1466 if (!isSupported() || !m_frame)
1468 return m_command->isEnabled(m_frame.get(), triggeringEvent, m_source);
1471 TriState Editor::Command::state(Event* triggeringEvent) const
1473 if (!isSupported() || !m_frame)
1474 return FalseTriState;
1475 return m_command->state(m_frame.get(), triggeringEvent);
1478 String Editor::Command::value(Event* triggeringEvent) const
1480 if (!isSupported() || !m_frame)
1482 return m_command->value(m_frame.get(), triggeringEvent);
1485 bool Editor::Command::isTextInsertion() const
1487 return m_command && m_command->isTextInsertion;
1490 } // namespace WebCore