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.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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.
29 #include "core/editing/Editor.h"
31 #include "bindings/core/v8/ExceptionState.h"
32 #include "bindings/core/v8/ExceptionStatePlaceholder.h"
33 #include "core/CSSPropertyNames.h"
34 #include "core/CSSValueKeywords.h"
35 #include "core/HTMLNames.h"
36 #include "core/clipboard/Pasteboard.h"
37 #include "core/css/CSSValueList.h"
38 #include "core/css/StylePropertySet.h"
39 #include "core/dom/DocumentFragment.h"
40 #include "core/editing/CreateLinkCommand.h"
41 #include "core/editing/FormatBlockCommand.h"
42 #include "core/editing/FrameSelection.h"
43 #include "core/editing/IndentOutdentCommand.h"
44 #include "core/editing/InsertListCommand.h"
45 #include "core/editing/ReplaceSelectionCommand.h"
46 #include "core/editing/SpellChecker.h"
47 #include "core/editing/TypingCommand.h"
48 #include "core/editing/UnlinkCommand.h"
49 #include "core/editing/htmlediting.h"
50 #include "core/editing/markup.h"
51 #include "core/events/Event.h"
52 #include "core/frame/FrameHost.h"
53 #include "core/frame/FrameView.h"
54 #include "core/frame/LocalFrame.h"
55 #include "core/frame/Settings.h"
56 #include "core/html/HTMLFontElement.h"
57 #include "core/html/HTMLHRElement.h"
58 #include "core/html/HTMLImageElement.h"
59 #include "core/page/Chrome.h"
60 #include "core/page/EditorClient.h"
61 #include "core/page/EventHandler.h"
62 #include "core/rendering/RenderBox.h"
63 #include "platform/KillRing.h"
64 #include "platform/scroll/Scrollbar.h"
65 #include "public/platform/Platform.h"
66 #include "wtf/text/AtomicString.h"
70 using namespace HTMLNames;
72 class EditorInternalCommand {
75 bool (*execute)(LocalFrame&, Event*, EditorCommandSource, const String&);
76 bool (*isSupportedFromDOM)(LocalFrame*);
77 bool (*isEnabled)(LocalFrame&, Event*, EditorCommandSource);
78 TriState (*state)(LocalFrame&, Event*);
79 String (*value)(LocalFrame&, Event*);
81 bool allowExecutionWhenDisabled;
84 typedef HashMap<String, const EditorInternalCommand*, CaseFoldingHash> CommandMap;
86 static const bool notTextInsertion = false;
87 static const bool isTextInsertion = true;
89 static const bool allowExecutionWhenDisabled = true;
90 static const bool doNotAllowExecutionWhenDisabled = false;
92 // Related to Editor::selectionForCommand.
93 // Certain operations continue to use the target control's selection even if the event handler
94 // already moved the selection outside of the text control.
95 static LocalFrame* targetFrame(LocalFrame& frame, Event* event)
99 Node* node = event->target()->toNode();
102 return node->document().frame();
105 static bool applyCommandToFrame(LocalFrame& frame, EditorCommandSource source, EditAction action, StylePropertySet* style)
107 // FIXME: We don't call shouldApplyStyle when the source is DOM; is there a good reason for that?
109 case CommandFromMenuOrKeyBinding:
110 frame.editor().applyStyleToSelection(style, action);
113 case CommandFromDOMWithUserInterface:
114 frame.editor().applyStyle(style);
117 ASSERT_NOT_REACHED();
121 static bool executeApplyStyle(LocalFrame& frame, EditorCommandSource source, EditAction action, CSSPropertyID propertyID, const String& propertyValue)
123 RefPtrWillBeRawPtr<MutableStylePropertySet> style = MutableStylePropertySet::create();
124 style->setProperty(propertyID, propertyValue);
125 return applyCommandToFrame(frame, source, action, style.get());
128 static bool executeApplyStyle(LocalFrame& frame, EditorCommandSource source, EditAction action, CSSPropertyID propertyID, CSSValueID propertyValue)
130 RefPtrWillBeRawPtr<MutableStylePropertySet> style = MutableStylePropertySet::create();
131 style->setProperty(propertyID, propertyValue);
132 return applyCommandToFrame(frame, source, action, style.get());
135 // FIXME: executeToggleStyleInList does not handle complicated cases such as <b><u>hello</u>world</b> properly.
136 // This function must use Editor::selectionHasStyle to determine the current style but we cannot fix this
137 // until https://bugs.webkit.org/show_bug.cgi?id=27818 is resolved.
138 static bool executeToggleStyleInList(LocalFrame& frame, EditorCommandSource source, EditAction action, CSSPropertyID propertyID, CSSValue* value)
140 RefPtrWillBeRawPtr<EditingStyle> selectionStyle = EditingStyle::styleAtSelectionStart(frame.selection().selection());
141 if (!selectionStyle || !selectionStyle->style())
144 RefPtrWillBeRawPtr<CSSValue> selectedCSSValue = selectionStyle->style()->getPropertyCSSValue(propertyID);
145 String newStyle("none");
146 if (selectedCSSValue->isValueList()) {
147 RefPtrWillBeRawPtr<CSSValueList> selectedCSSValueList = toCSSValueList(selectedCSSValue.get());
148 if (!selectedCSSValueList->removeAll(value))
149 selectedCSSValueList->append(value);
150 if (selectedCSSValueList->length())
151 newStyle = selectedCSSValueList->cssText();
153 } else if (selectedCSSValue->cssText() == "none")
154 newStyle = value->cssText();
156 // FIXME: We shouldn't be having to convert new style into text. We should have setPropertyCSSValue.
157 RefPtrWillBeRawPtr<MutableStylePropertySet> newMutableStyle = MutableStylePropertySet::create();
158 newMutableStyle->setProperty(propertyID, newStyle);
159 return applyCommandToFrame(frame, source, action, newMutableStyle.get());
162 static bool executeToggleStyle(LocalFrame& frame, EditorCommandSource source, EditAction action, CSSPropertyID propertyID, const char* offValue, const char* onValue)
164 // Style is considered present when
165 // Mac: present at the beginning of selection
166 // other: present throughout the selection
169 if (frame.editor().behavior().shouldToggleStyleBasedOnStartOfSelection())
170 styleIsPresent = frame.editor().selectionStartHasStyle(propertyID, onValue);
172 styleIsPresent = frame.editor().selectionHasStyle(propertyID, onValue) == TrueTriState;
174 RefPtrWillBeRawPtr<EditingStyle> style = EditingStyle::create(propertyID, styleIsPresent ? offValue : onValue);
175 return applyCommandToFrame(frame, source, action, style->style());
178 static bool executeApplyParagraphStyle(LocalFrame& frame, EditorCommandSource source, EditAction action, CSSPropertyID propertyID, const String& propertyValue)
180 RefPtrWillBeRawPtr<MutableStylePropertySet> style = MutableStylePropertySet::create();
181 style->setProperty(propertyID, propertyValue);
182 // FIXME: We don't call shouldApplyStyle when the source is DOM; is there a good reason for that?
184 case CommandFromMenuOrKeyBinding:
185 frame.editor().applyParagraphStyleToSelection(style.get(), action);
188 case CommandFromDOMWithUserInterface:
189 frame.editor().applyParagraphStyle(style.get());
192 ASSERT_NOT_REACHED();
196 static bool executeInsertFragment(LocalFrame& frame, PassRefPtrWillBeRawPtr<DocumentFragment> fragment)
198 ASSERT(frame.document());
199 ReplaceSelectionCommand::create(*frame.document(), fragment, ReplaceSelectionCommand::PreventNesting, EditActionUnspecified)->apply();
203 static bool executeInsertElement(LocalFrame& frame, PassRefPtrWillBeRawPtr<HTMLElement> content)
205 ASSERT(frame.document());
206 RefPtrWillBeRawPtr<DocumentFragment> fragment = DocumentFragment::create(*frame.document());
207 TrackExceptionState exceptionState;
208 fragment->appendChild(content, exceptionState);
209 if (exceptionState.hadException())
211 return executeInsertFragment(frame, fragment.release());
214 static bool expandSelectionToGranularity(LocalFrame& frame, TextGranularity granularity)
216 VisibleSelection selection = frame.selection().selection();
217 selection.expandUsingGranularity(granularity);
218 RefPtrWillBeRawPtr<Range> newRange = selection.toNormalizedRange();
221 if (newRange->collapsed())
223 EAffinity affinity = frame.selection().affinity();
224 frame.selection().setSelectedRange(newRange.get(), affinity, FrameSelection::NonDirectional, FrameSelection::CloseTyping);
228 static TriState selectionListState(const FrameSelection& selection, const QualifiedName& tagName)
230 if (selection.isCaret()) {
231 if (enclosingElementWithTag(selection.selection().start(), tagName))
233 } else if (selection.isRange()) {
234 Element* startElement = enclosingElementWithTag(selection.selection().start(), tagName);
235 Element* endElement = enclosingElementWithTag(selection.selection().end(), tagName);
236 if (startElement && endElement && startElement == endElement)
240 return FalseTriState;
243 static TriState stateStyle(LocalFrame& frame, CSSPropertyID propertyID, const char* desiredValue)
245 if (frame.editor().behavior().shouldToggleStyleBasedOnStartOfSelection())
246 return frame.editor().selectionStartHasStyle(propertyID, desiredValue) ? TrueTriState : FalseTriState;
247 return frame.editor().selectionHasStyle(propertyID, desiredValue);
250 static String valueStyle(LocalFrame& frame, CSSPropertyID propertyID)
252 // FIXME: Rather than retrieving the style at the start of the current selection,
253 // we should retrieve the style present throughout the selection for non-Mac platforms.
254 return frame.editor().selectionStartCSSPropertyValue(propertyID);
257 static TriState stateTextWritingDirection(LocalFrame& frame, WritingDirection direction)
259 bool hasNestedOrMultipleEmbeddings;
260 WritingDirection selectionDirection = EditingStyle::textDirectionForSelection(frame.selection().selection(),
261 frame.selection().typingStyle(), hasNestedOrMultipleEmbeddings);
262 // FXIME: We should be returning MixedTriState when selectionDirection == direction && hasNestedOrMultipleEmbeddings
263 return (selectionDirection == direction && !hasNestedOrMultipleEmbeddings) ? TrueTriState : FalseTriState;
266 static unsigned verticalScrollDistance(LocalFrame& frame)
268 Element* focusedElement = frame.document()->focusedElement();
271 RenderObject* renderer = focusedElement->renderer();
272 if (!renderer || !renderer->isBox())
274 RenderBox& renderBox = toRenderBox(*renderer);
275 RenderStyle* style = renderBox.style();
278 if (!(style->overflowY() == OSCROLL || style->overflowY() == OAUTO || focusedElement->hasEditableStyle()))
280 int height = std::min<int>(renderBox.clientHeight(), frame.view()->visibleHeight());
281 return static_cast<unsigned>(max(max<int>(height * ScrollableArea::minFractionToStepWhenPaging(), height - ScrollableArea::maxOverlapBetweenPages()), 1));
284 static PassRefPtrWillBeRawPtr<Range> unionDOMRanges(Range* a, Range* b)
286 Range* start = a->compareBoundaryPoints(Range::START_TO_START, b, ASSERT_NO_EXCEPTION) <= 0 ? a : b;
287 Range* end = a->compareBoundaryPoints(Range::END_TO_END, b, ASSERT_NO_EXCEPTION) <= 0 ? b : a;
289 return Range::create(a->ownerDocument(), start->startContainer(), start->startOffset(), end->endContainer(), end->endOffset());
292 // Execute command functions
294 static bool executeBackColor(LocalFrame& frame, Event*, EditorCommandSource source, const String& value)
296 return executeApplyStyle(frame, source, EditActionSetBackgroundColor, CSSPropertyBackgroundColor, value);
299 static bool executeCopy(LocalFrame& frame, Event*, EditorCommandSource, const String&)
301 frame.editor().copy();
305 static bool executeCreateLink(LocalFrame& frame, Event*, EditorCommandSource, const String& value)
307 // FIXME: If userInterface is true, we should display a dialog box to let the user enter a URL.
310 ASSERT(frame.document());
311 CreateLinkCommand::create(*frame.document(), value)->apply();
315 static bool executeCut(LocalFrame& frame, Event*, EditorCommandSource, const String&)
317 frame.editor().cut();
321 static bool executeDefaultParagraphSeparator(LocalFrame& frame, Event*, EditorCommandSource, const String& value)
323 if (equalIgnoringCase(value, "div"))
324 frame.editor().setDefaultParagraphSeparator(EditorParagraphSeparatorIsDiv);
325 else if (equalIgnoringCase(value, "p"))
326 frame.editor().setDefaultParagraphSeparator(EditorParagraphSeparatorIsP);
331 static bool executeDelete(LocalFrame& frame, Event*, EditorCommandSource source, const String&)
334 case CommandFromMenuOrKeyBinding: {
335 // Doesn't modify the text if the current selection isn't a range.
336 frame.editor().performDelete();
340 case CommandFromDOMWithUserInterface:
341 // If the current selection is a caret, delete the preceding character. IE performs forwardDelete, but we currently side with Firefox.
342 // Doesn't scroll to make the selection visible, or modify the kill ring (this time, siding with IE, not Firefox).
343 ASSERT(frame.document());
344 TypingCommand::deleteKeyPressed(*frame.document(), frame.selection().granularity() == WordGranularity ? TypingCommand::SmartDelete : 0);
347 ASSERT_NOT_REACHED();
351 static bool executeDeleteBackward(LocalFrame& frame, Event*, EditorCommandSource, const String&)
353 frame.editor().deleteWithDirection(DirectionBackward, CharacterGranularity, false, true);
357 static bool executeDeleteBackwardByDecomposingPreviousCharacter(LocalFrame& frame, Event*, EditorCommandSource, const String&)
359 WTF_LOG_ERROR("DeleteBackwardByDecomposingPreviousCharacter is not implemented, doing DeleteBackward instead");
360 frame.editor().deleteWithDirection(DirectionBackward, CharacterGranularity, false, true);
364 static bool executeDeleteForward(LocalFrame& frame, Event*, EditorCommandSource, const String&)
366 frame.editor().deleteWithDirection(DirectionForward, CharacterGranularity, false, true);
370 static bool executeDeleteToBeginningOfLine(LocalFrame& frame, Event*, EditorCommandSource, const String&)
372 frame.editor().deleteWithDirection(DirectionBackward, LineBoundary, true, false);
376 static bool executeDeleteToBeginningOfParagraph(LocalFrame& frame, Event*, EditorCommandSource, const String&)
378 frame.editor().deleteWithDirection(DirectionBackward, ParagraphBoundary, true, false);
382 static bool executeDeleteToEndOfLine(LocalFrame& frame, Event*, EditorCommandSource, const String&)
384 // Despite its name, this command should delete the newline at the end of
385 // a paragraph if you are at the end of a paragraph (like DeleteToEndOfParagraph).
386 frame.editor().deleteWithDirection(DirectionForward, LineBoundary, true, false);
390 static bool executeDeleteToEndOfParagraph(LocalFrame& frame, Event*, EditorCommandSource, const String&)
392 // Despite its name, this command should delete the newline at the end of
393 // a paragraph if you are at the end of a paragraph.
394 frame.editor().deleteWithDirection(DirectionForward, ParagraphBoundary, true, false);
398 static bool executeDeleteToMark(LocalFrame& frame, Event*, EditorCommandSource, const String&)
400 RefPtrWillBeRawPtr<Range> mark = frame.editor().mark().toNormalizedRange();
402 bool selected = frame.selection().setSelectedRange(unionDOMRanges(mark.get(), frame.editor().selectedRange().get()).get(), DOWNSTREAM, FrameSelection::NonDirectional, FrameSelection::CloseTyping);
407 frame.editor().performDelete();
408 frame.editor().setMark(frame.selection().selection());
412 static bool executeDeleteWordBackward(LocalFrame& frame, Event*, EditorCommandSource, const String&)
414 frame.editor().deleteWithDirection(DirectionBackward, WordGranularity, true, false);
418 static bool executeDeleteWordForward(LocalFrame& frame, Event*, EditorCommandSource, const String&)
420 frame.editor().deleteWithDirection(DirectionForward, WordGranularity, true, false);
424 static bool executeFindString(LocalFrame& frame, Event*, EditorCommandSource, const String& value)
426 return frame.editor().findString(value, true, false, true, false);
429 static bool executeFontName(LocalFrame& frame, Event*, EditorCommandSource source, const String& value)
431 return executeApplyStyle(frame, source, EditActionSetFont, CSSPropertyFontFamily, value);
434 static bool executeFontSize(LocalFrame& frame, Event*, EditorCommandSource source, const String& value)
437 if (!HTMLFontElement::cssValueFromFontSizeNumber(value, size))
439 return executeApplyStyle(frame, source, EditActionChangeAttributes, CSSPropertyFontSize, size);
442 static bool executeFontSizeDelta(LocalFrame& frame, Event*, EditorCommandSource source, const String& value)
444 return executeApplyStyle(frame, source, EditActionChangeAttributes, CSSPropertyWebkitFontSizeDelta, value);
447 static bool executeForeColor(LocalFrame& frame, Event*, EditorCommandSource source, const String& value)
449 return executeApplyStyle(frame, source, EditActionSetColor, CSSPropertyColor, value);
452 static bool executeFormatBlock(LocalFrame& frame, Event*, EditorCommandSource, const String& value)
454 String tagName = value.lower();
455 if (tagName[0] == '<' && tagName[tagName.length() - 1] == '>')
456 tagName = tagName.substring(1, tagName.length() - 2);
458 AtomicString localName, prefix;
459 if (!Document::parseQualifiedName(AtomicString(tagName), prefix, localName, IGNORE_EXCEPTION))
461 QualifiedName qualifiedTagName(prefix, localName, xhtmlNamespaceURI);
463 ASSERT(frame.document());
464 RefPtrWillBeRawPtr<FormatBlockCommand> command = FormatBlockCommand::create(*frame.document(), qualifiedTagName);
466 return command->didApply();
469 static bool executeForwardDelete(LocalFrame& frame, Event*, EditorCommandSource source, const String&)
472 case CommandFromMenuOrKeyBinding:
473 frame.editor().deleteWithDirection(DirectionForward, CharacterGranularity, false, true);
476 case CommandFromDOMWithUserInterface:
477 // Doesn't scroll to make the selection visible, or modify the kill ring.
478 // ForwardDelete is not implemented in IE or Firefox, so this behavior is only needed for
479 // backward compatibility with ourselves, and for consistency with Delete.
480 ASSERT(frame.document());
481 TypingCommand::forwardDeleteKeyPressed(*frame.document());
484 ASSERT_NOT_REACHED();
488 static bool executeIgnoreSpelling(LocalFrame& frame, Event*, EditorCommandSource, const String&)
490 frame.spellChecker().ignoreSpelling();
494 static bool executeIndent(LocalFrame& frame, Event*, EditorCommandSource, const String&)
496 ASSERT(frame.document());
497 IndentOutdentCommand::create(*frame.document(), IndentOutdentCommand::Indent)->apply();
501 static bool executeInsertBacktab(LocalFrame& frame, Event* event, EditorCommandSource, const String&)
503 return targetFrame(frame, event)->eventHandler().handleTextInputEvent("\t", event, TextEventInputBackTab);
506 static bool executeInsertHorizontalRule(LocalFrame& frame, Event*, EditorCommandSource, const String& value)
508 ASSERT(frame.document());
509 RefPtrWillBeRawPtr<HTMLHRElement> rule = HTMLHRElement::create(*frame.document());
510 if (!value.isEmpty())
511 rule->setIdAttribute(AtomicString(value));
512 return executeInsertElement(frame, rule.release());
515 static bool executeInsertHTML(LocalFrame& frame, Event*, EditorCommandSource, const String& value)
517 ASSERT(frame.document());
518 return executeInsertFragment(frame, createFragmentFromMarkup(*frame.document(), value, ""));
521 static bool executeInsertImage(LocalFrame& frame, Event*, EditorCommandSource, const String& value)
523 // FIXME: If userInterface is true, we should display a dialog box and let the user choose a local image.
524 ASSERT(frame.document());
525 RefPtrWillBeRawPtr<HTMLImageElement> image = HTMLImageElement::create(*frame.document());
526 image->setSrc(value);
527 return executeInsertElement(frame, image.release());
530 static bool executeInsertLineBreak(LocalFrame& frame, Event* event, EditorCommandSource source, const String&)
533 case CommandFromMenuOrKeyBinding:
534 return targetFrame(frame, event)->eventHandler().handleTextInputEvent("\n", event, TextEventInputLineBreak);
536 case CommandFromDOMWithUserInterface:
537 // Doesn't scroll to make the selection visible, or modify the kill ring.
538 // InsertLineBreak is not implemented in IE or Firefox, so this behavior is only needed for
539 // backward compatibility with ourselves, and for consistency with other commands.
540 ASSERT(frame.document());
541 TypingCommand::insertLineBreak(*frame.document(), 0);
544 ASSERT_NOT_REACHED();
548 static bool executeInsertNewline(LocalFrame& frame, Event* event, EditorCommandSource, const String&)
550 LocalFrame* targetFrame = blink::targetFrame(frame, event);
551 return targetFrame->eventHandler().handleTextInputEvent("\n", event, targetFrame->editor().canEditRichly() ? TextEventInputKeyboard : TextEventInputLineBreak);
554 static bool executeInsertNewlineInQuotedContent(LocalFrame& frame, Event*, EditorCommandSource, const String&)
556 ASSERT(frame.document());
557 TypingCommand::insertParagraphSeparatorInQuotedContent(*frame.document());
561 static bool executeInsertOrderedList(LocalFrame& frame, Event*, EditorCommandSource, const String&)
563 ASSERT(frame.document());
564 InsertListCommand::create(*frame.document(), InsertListCommand::OrderedList)->apply();
568 static bool executeInsertParagraph(LocalFrame& frame, Event*, EditorCommandSource, const String&)
570 ASSERT(frame.document());
571 TypingCommand::insertParagraphSeparator(*frame.document(), 0);
575 static bool executeInsertTab(LocalFrame& frame, Event* event, EditorCommandSource, const String&)
577 return targetFrame(frame, event)->eventHandler().handleTextInputEvent("\t", event);
580 static bool executeInsertText(LocalFrame& frame, Event*, EditorCommandSource, const String& value)
582 ASSERT(frame.document());
583 TypingCommand::insertText(*frame.document(), value, 0);
587 static bool executeInsertUnorderedList(LocalFrame& frame, Event*, EditorCommandSource, const String&)
589 ASSERT(frame.document());
590 InsertListCommand::create(*frame.document(), InsertListCommand::UnorderedList)->apply();
594 static bool executeJustifyCenter(LocalFrame& frame, Event*, EditorCommandSource source, const String&)
596 return executeApplyParagraphStyle(frame, source, EditActionCenter, CSSPropertyTextAlign, "center");
599 static bool executeJustifyFull(LocalFrame& frame, Event*, EditorCommandSource source, const String&)
601 return executeApplyParagraphStyle(frame, source, EditActionJustify, CSSPropertyTextAlign, "justify");
604 static bool executeJustifyLeft(LocalFrame& frame, Event*, EditorCommandSource source, const String&)
606 return executeApplyParagraphStyle(frame, source, EditActionAlignLeft, CSSPropertyTextAlign, "left");
609 static bool executeJustifyRight(LocalFrame& frame, Event*, EditorCommandSource source, const String&)
611 return executeApplyParagraphStyle(frame, source, EditActionAlignRight, CSSPropertyTextAlign, "right");
614 static bool executeMakeTextWritingDirectionLeftToRight(LocalFrame& frame, Event*, EditorCommandSource, const String&)
616 RefPtrWillBeRawPtr<MutableStylePropertySet> style = MutableStylePropertySet::create();
617 style->setProperty(CSSPropertyUnicodeBidi, CSSValueEmbed);
618 style->setProperty(CSSPropertyDirection, CSSValueLtr);
619 frame.editor().applyStyle(style.get(), EditActionSetWritingDirection);
623 static bool executeMakeTextWritingDirectionNatural(LocalFrame& frame, Event*, EditorCommandSource, const String&)
625 RefPtrWillBeRawPtr<MutableStylePropertySet> style = MutableStylePropertySet::create();
626 style->setProperty(CSSPropertyUnicodeBidi, CSSValueNormal);
627 frame.editor().applyStyle(style.get(), EditActionSetWritingDirection);
631 static bool executeMakeTextWritingDirectionRightToLeft(LocalFrame& frame, Event*, EditorCommandSource, const String&)
633 RefPtrWillBeRawPtr<MutableStylePropertySet> style = MutableStylePropertySet::create();
634 style->setProperty(CSSPropertyUnicodeBidi, CSSValueEmbed);
635 style->setProperty(CSSPropertyDirection, CSSValueRtl);
636 frame.editor().applyStyle(style.get(), EditActionSetWritingDirection);
640 static bool executeMoveBackward(LocalFrame& frame, Event*, EditorCommandSource, const String&)
642 frame.selection().modify(FrameSelection::AlterationMove, DirectionBackward, CharacterGranularity, UserTriggered);
646 static bool executeMoveBackwardAndModifySelection(LocalFrame& frame, Event*, EditorCommandSource, const String&)
648 frame.selection().modify(FrameSelection::AlterationExtend, DirectionBackward, CharacterGranularity, UserTriggered);
652 static bool executeMoveDown(LocalFrame& frame, Event*, EditorCommandSource, const String&)
654 return frame.selection().modify(FrameSelection::AlterationMove, DirectionForward, LineGranularity, UserTriggered);
657 static bool executeMoveDownAndModifySelection(LocalFrame& frame, Event*, EditorCommandSource, const String&)
659 frame.selection().modify(FrameSelection::AlterationExtend, DirectionForward, LineGranularity, UserTriggered);
663 static bool executeMoveForward(LocalFrame& frame, Event*, EditorCommandSource, const String&)
665 frame.selection().modify(FrameSelection::AlterationMove, DirectionForward, CharacterGranularity, UserTriggered);
669 static bool executeMoveForwardAndModifySelection(LocalFrame& frame, Event*, EditorCommandSource, const String&)
671 frame.selection().modify(FrameSelection::AlterationExtend, DirectionForward, CharacterGranularity, UserTriggered);
675 static bool executeMoveLeft(LocalFrame& frame, Event*, EditorCommandSource, const String&)
677 return frame.selection().modify(FrameSelection::AlterationMove, DirectionLeft, CharacterGranularity, UserTriggered);
680 static bool executeMoveLeftAndModifySelection(LocalFrame& frame, Event*, EditorCommandSource, const String&)
682 frame.selection().modify(FrameSelection::AlterationExtend, DirectionLeft, CharacterGranularity, UserTriggered);
686 static bool executeMovePageDown(LocalFrame& frame, Event*, EditorCommandSource, const String&)
688 unsigned distance = verticalScrollDistance(frame);
691 return frame.selection().modify(FrameSelection::AlterationMove, distance, FrameSelection::DirectionDown,
692 UserTriggered, FrameSelection::AlignCursorOnScrollAlways);
695 static bool executeMovePageDownAndModifySelection(LocalFrame& frame, Event*, EditorCommandSource, const String&)
697 unsigned distance = verticalScrollDistance(frame);
700 return frame.selection().modify(FrameSelection::AlterationExtend, distance, FrameSelection::DirectionDown,
701 UserTriggered, FrameSelection::AlignCursorOnScrollAlways);
704 static bool executeMovePageUp(LocalFrame& frame, Event*, EditorCommandSource, const String&)
706 unsigned distance = verticalScrollDistance(frame);
709 return frame.selection().modify(FrameSelection::AlterationMove, distance, FrameSelection::DirectionUp,
710 UserTriggered, FrameSelection::AlignCursorOnScrollAlways);
713 static bool executeMovePageUpAndModifySelection(LocalFrame& frame, Event*, EditorCommandSource, const String&)
715 unsigned distance = verticalScrollDistance(frame);
718 return frame.selection().modify(FrameSelection::AlterationExtend, distance, FrameSelection::DirectionUp,
719 UserTriggered, FrameSelection::AlignCursorOnScrollAlways);
722 static bool executeMoveRight(LocalFrame& frame, Event*, EditorCommandSource, const String&)
724 return frame.selection().modify(FrameSelection::AlterationMove, DirectionRight, CharacterGranularity, UserTriggered);
727 static bool executeMoveRightAndModifySelection(LocalFrame& frame, Event*, EditorCommandSource, const String&)
729 frame.selection().modify(FrameSelection::AlterationExtend, DirectionRight, CharacterGranularity, UserTriggered);
733 static bool executeMoveToBeginningOfDocument(LocalFrame& frame, Event*, EditorCommandSource, const String&)
735 frame.selection().modify(FrameSelection::AlterationMove, DirectionBackward, DocumentBoundary, UserTriggered);
739 static bool executeMoveToBeginningOfDocumentAndModifySelection(LocalFrame& frame, Event*, EditorCommandSource, const String&)
741 frame.selection().modify(FrameSelection::AlterationExtend, DirectionBackward, DocumentBoundary, UserTriggered);
745 static bool executeMoveToBeginningOfLine(LocalFrame& frame, Event*, EditorCommandSource, const String&)
747 frame.selection().modify(FrameSelection::AlterationMove, DirectionBackward, LineBoundary, UserTriggered);
751 static bool executeMoveToBeginningOfLineAndModifySelection(LocalFrame& frame, Event*, EditorCommandSource, const String&)
753 frame.selection().modify(FrameSelection::AlterationExtend, DirectionBackward, LineBoundary, UserTriggered);
757 static bool executeMoveToBeginningOfParagraph(LocalFrame& frame, Event*, EditorCommandSource, const String&)
759 frame.selection().modify(FrameSelection::AlterationMove, DirectionBackward, ParagraphBoundary, UserTriggered);
763 static bool executeMoveToBeginningOfParagraphAndModifySelection(LocalFrame& frame, Event*, EditorCommandSource, const String&)
765 frame.selection().modify(FrameSelection::AlterationExtend, DirectionBackward, ParagraphBoundary, UserTriggered);
769 static bool executeMoveToBeginningOfSentence(LocalFrame& frame, Event*, EditorCommandSource, const String&)
771 frame.selection().modify(FrameSelection::AlterationMove, DirectionBackward, SentenceBoundary, UserTriggered);
775 static bool executeMoveToBeginningOfSentenceAndModifySelection(LocalFrame& frame, Event*, EditorCommandSource, const String&)
777 frame.selection().modify(FrameSelection::AlterationExtend, DirectionBackward, SentenceBoundary, UserTriggered);
781 static bool executeMoveToEndOfDocument(LocalFrame& frame, Event*, EditorCommandSource, const String&)
783 frame.selection().modify(FrameSelection::AlterationMove, DirectionForward, DocumentBoundary, UserTriggered);
787 static bool executeMoveToEndOfDocumentAndModifySelection(LocalFrame& frame, Event*, EditorCommandSource, const String&)
789 frame.selection().modify(FrameSelection::AlterationExtend, DirectionForward, DocumentBoundary, UserTriggered);
793 static bool executeMoveToEndOfSentence(LocalFrame& frame, Event*, EditorCommandSource, const String&)
795 frame.selection().modify(FrameSelection::AlterationMove, DirectionForward, SentenceBoundary, UserTriggered);
799 static bool executeMoveToEndOfSentenceAndModifySelection(LocalFrame& frame, Event*, EditorCommandSource, const String&)
801 frame.selection().modify(FrameSelection::AlterationExtend, DirectionForward, SentenceBoundary, UserTriggered);
805 static bool executeMoveToEndOfLine(LocalFrame& frame, Event*, EditorCommandSource, const String&)
807 frame.selection().modify(FrameSelection::AlterationMove, DirectionForward, LineBoundary, UserTriggered);
811 static bool executeMoveToEndOfLineAndModifySelection(LocalFrame& frame, Event*, EditorCommandSource, const String&)
813 frame.selection().modify(FrameSelection::AlterationExtend, DirectionForward, LineBoundary, UserTriggered);
817 static bool executeMoveToEndOfParagraph(LocalFrame& frame, Event*, EditorCommandSource, const String&)
819 frame.selection().modify(FrameSelection::AlterationMove, DirectionForward, ParagraphBoundary, UserTriggered);
823 static bool executeMoveToEndOfParagraphAndModifySelection(LocalFrame& frame, Event*, EditorCommandSource, const String&)
825 frame.selection().modify(FrameSelection::AlterationExtend, DirectionForward, ParagraphBoundary, UserTriggered);
829 static bool executeMoveParagraphBackward(LocalFrame& frame, Event*, EditorCommandSource, const String&)
831 frame.selection().modify(FrameSelection::AlterationMove, DirectionBackward, ParagraphGranularity, UserTriggered);
835 static bool executeMoveParagraphBackwardAndModifySelection(LocalFrame& frame, Event*, EditorCommandSource, const String&)
837 frame.selection().modify(FrameSelection::AlterationExtend, DirectionBackward, ParagraphGranularity, UserTriggered);
841 static bool executeMoveParagraphForward(LocalFrame& frame, Event*, EditorCommandSource, const String&)
843 frame.selection().modify(FrameSelection::AlterationMove, DirectionForward, ParagraphGranularity, UserTriggered);
847 static bool executeMoveParagraphForwardAndModifySelection(LocalFrame& frame, Event*, EditorCommandSource, const String&)
849 frame.selection().modify(FrameSelection::AlterationExtend, DirectionForward, ParagraphGranularity, UserTriggered);
853 static bool executeMoveUp(LocalFrame& frame, Event*, EditorCommandSource, const String&)
855 return frame.selection().modify(FrameSelection::AlterationMove, DirectionBackward, LineGranularity, UserTriggered);
858 static bool executeMoveUpAndModifySelection(LocalFrame& frame, Event*, EditorCommandSource, const String&)
860 frame.selection().modify(FrameSelection::AlterationExtend, DirectionBackward, LineGranularity, UserTriggered);
864 static bool executeMoveWordBackward(LocalFrame& frame, Event*, EditorCommandSource, const String&)
866 frame.selection().modify(FrameSelection::AlterationMove, DirectionBackward, WordGranularity, UserTriggered);
870 static bool executeMoveWordBackwardAndModifySelection(LocalFrame& frame, Event*, EditorCommandSource, const String&)
872 frame.selection().modify(FrameSelection::AlterationExtend, DirectionBackward, WordGranularity, UserTriggered);
876 static bool executeMoveWordForward(LocalFrame& frame, Event*, EditorCommandSource, const String&)
878 frame.selection().modify(FrameSelection::AlterationMove, DirectionForward, WordGranularity, UserTriggered);
882 static bool executeMoveWordForwardAndModifySelection(LocalFrame& frame, Event*, EditorCommandSource, const String&)
884 frame.selection().modify(FrameSelection::AlterationExtend, DirectionForward, WordGranularity, UserTriggered);
888 static bool executeMoveWordLeft(LocalFrame& frame, Event*, EditorCommandSource, const String&)
890 frame.selection().modify(FrameSelection::AlterationMove, DirectionLeft, WordGranularity, UserTriggered);
894 static bool executeMoveWordLeftAndModifySelection(LocalFrame& frame, Event*, EditorCommandSource, const String&)
896 frame.selection().modify(FrameSelection::AlterationExtend, DirectionLeft, WordGranularity, UserTriggered);
900 static bool executeMoveWordRight(LocalFrame& frame, Event*, EditorCommandSource, const String&)
902 frame.selection().modify(FrameSelection::AlterationMove, DirectionRight, WordGranularity, UserTriggered);
906 static bool executeMoveWordRightAndModifySelection(LocalFrame& frame, Event*, EditorCommandSource, const String&)
908 frame.selection().modify(FrameSelection::AlterationExtend, DirectionRight, WordGranularity, UserTriggered);
912 static bool executeMoveToLeftEndOfLine(LocalFrame& frame, Event*, EditorCommandSource, const String&)
914 frame.selection().modify(FrameSelection::AlterationMove, DirectionLeft, LineBoundary, UserTriggered);
918 static bool executeMoveToLeftEndOfLineAndModifySelection(LocalFrame& frame, Event*, EditorCommandSource, const String&)
920 frame.selection().modify(FrameSelection::AlterationExtend, DirectionLeft, LineBoundary, UserTriggered);
924 static bool executeMoveToRightEndOfLine(LocalFrame& frame, Event*, EditorCommandSource, const String&)
926 frame.selection().modify(FrameSelection::AlterationMove, DirectionRight, LineBoundary, UserTriggered);
930 static bool executeMoveToRightEndOfLineAndModifySelection(LocalFrame& frame, Event*, EditorCommandSource, const String&)
932 frame.selection().modify(FrameSelection::AlterationExtend, DirectionRight, LineBoundary, UserTriggered);
936 static bool executeOutdent(LocalFrame& frame, Event*, EditorCommandSource, const String&)
938 ASSERT(frame.document());
939 IndentOutdentCommand::create(*frame.document(), IndentOutdentCommand::Outdent)->apply();
943 static bool executeToggleOverwrite(LocalFrame& frame, Event*, EditorCommandSource, const String&)
945 frame.editor().toggleOverwriteModeEnabled();
949 static bool executePaste(LocalFrame& frame, Event*, EditorCommandSource, const String&)
951 frame.editor().paste();
955 static bool executePasteGlobalSelection(LocalFrame& frame, Event*, EditorCommandSource source, const String&)
957 if (!frame.editor().behavior().supportsGlobalSelection())
959 ASSERT_UNUSED(source, source == CommandFromMenuOrKeyBinding);
961 bool oldSelectionMode = Pasteboard::generalPasteboard()->isSelectionMode();
962 Pasteboard::generalPasteboard()->setSelectionMode(true);
963 frame.editor().paste();
964 Pasteboard::generalPasteboard()->setSelectionMode(oldSelectionMode);
968 static bool executePasteAndMatchStyle(LocalFrame& frame, Event*, EditorCommandSource, const String&)
970 frame.editor().pasteAsPlainText();
974 static bool executePrint(LocalFrame& frame, Event*, EditorCommandSource, const String&)
976 FrameHost* host = frame.host();
979 host->chrome().print(&frame);
983 static bool executeRedo(LocalFrame& frame, Event*, EditorCommandSource, const String&)
985 frame.editor().redo();
989 static bool executeRemoveFormat(LocalFrame& frame, Event*, EditorCommandSource, const String&)
991 frame.editor().removeFormattingAndStyle();
995 static bool executeScrollPageBackward(LocalFrame& frame, Event*, EditorCommandSource, const String&)
997 return frame.eventHandler().bubblingScroll(ScrollBlockDirectionBackward, ScrollByPage);
1000 static bool executeScrollPageForward(LocalFrame& frame, Event*, EditorCommandSource, const String&)
1002 return frame.eventHandler().bubblingScroll(ScrollBlockDirectionForward, ScrollByPage);
1005 static bool executeScrollLineUp(LocalFrame& frame, Event*, EditorCommandSource, const String&)
1007 return frame.eventHandler().bubblingScroll(ScrollUp, ScrollByLine);
1010 static bool executeScrollLineDown(LocalFrame& frame, Event*, EditorCommandSource, const String&)
1012 return frame.eventHandler().bubblingScroll(ScrollDown, ScrollByLine);
1015 static bool executeScrollToBeginningOfDocument(LocalFrame& frame, Event*, EditorCommandSource, const String&)
1017 return frame.eventHandler().bubblingScroll(ScrollBlockDirectionBackward, ScrollByDocument);
1020 static bool executeScrollToEndOfDocument(LocalFrame& frame, Event*, EditorCommandSource, const String&)
1022 return frame.eventHandler().bubblingScroll(ScrollBlockDirectionForward, ScrollByDocument);
1025 static bool executeSelectAll(LocalFrame& frame, Event*, EditorCommandSource, const String&)
1027 frame.selection().selectAll();
1031 static bool executeSelectLine(LocalFrame& frame, Event*, EditorCommandSource, const String&)
1033 return expandSelectionToGranularity(frame, LineGranularity);
1036 static bool executeSelectParagraph(LocalFrame& frame, Event*, EditorCommandSource, const String&)
1038 return expandSelectionToGranularity(frame, ParagraphGranularity);
1041 static bool executeSelectSentence(LocalFrame& frame, Event*, EditorCommandSource, const String&)
1043 return expandSelectionToGranularity(frame, SentenceGranularity);
1046 static bool executeSelectToMark(LocalFrame& frame, Event*, EditorCommandSource, const String&)
1048 RefPtrWillBeRawPtr<Range> mark = frame.editor().mark().toNormalizedRange();
1049 RefPtrWillBeRawPtr<Range> selection = frame.editor().selectedRange();
1050 if (!mark || !selection)
1052 frame.selection().setSelectedRange(unionDOMRanges(mark.get(), selection.get()).get(), DOWNSTREAM, FrameSelection::NonDirectional, FrameSelection::CloseTyping);
1056 static bool executeSelectWord(LocalFrame& frame, Event*, EditorCommandSource, const String&)
1058 return expandSelectionToGranularity(frame, WordGranularity);
1061 static bool executeSetMark(LocalFrame& frame, Event*, EditorCommandSource, const String&)
1063 frame.editor().setMark(frame.selection().selection());
1067 static bool executeStrikethrough(LocalFrame& frame, Event*, EditorCommandSource source, const String&)
1069 RefPtrWillBeRawPtr<CSSPrimitiveValue> lineThrough = CSSPrimitiveValue::createIdentifier(CSSValueLineThrough);
1070 return executeToggleStyleInList(frame, source, EditActionUnderline, CSSPropertyWebkitTextDecorationsInEffect, lineThrough.get());
1073 static bool executeStyleWithCSS(LocalFrame& frame, Event*, EditorCommandSource, const String& value)
1075 frame.editor().setShouldStyleWithCSS(!equalIgnoringCase(value, "false"));
1079 static bool executeUseCSS(LocalFrame& frame, Event*, EditorCommandSource, const String& value)
1081 frame.editor().setShouldStyleWithCSS(equalIgnoringCase(value, "false"));
1085 static bool executeSubscript(LocalFrame& frame, Event*, EditorCommandSource source, const String&)
1087 return executeToggleStyle(frame, source, EditActionSubscript, CSSPropertyVerticalAlign, "baseline", "sub");
1090 static bool executeSuperscript(LocalFrame& frame, Event*, EditorCommandSource source, const String&)
1092 return executeToggleStyle(frame, source, EditActionSuperscript, CSSPropertyVerticalAlign, "baseline", "super");
1095 static bool executeSwapWithMark(LocalFrame& frame, Event*, EditorCommandSource, const String&)
1097 const VisibleSelection& mark = frame.editor().mark();
1098 const VisibleSelection& selection = frame.selection().selection();
1099 if (mark.isNone() || selection.isNone())
1101 frame.selection().setSelection(mark);
1102 frame.editor().setMark(selection);
1106 static bool executeToggleBold(LocalFrame& frame, Event*, EditorCommandSource source, const String&)
1108 return executeToggleStyle(frame, source, EditActionBold, CSSPropertyFontWeight, "normal", "bold");
1111 static bool executeToggleItalic(LocalFrame& frame, Event*, EditorCommandSource source, const String&)
1113 return executeToggleStyle(frame, source, EditActionItalics, CSSPropertyFontStyle, "normal", "italic");
1116 static bool executeTranspose(LocalFrame& frame, Event*, EditorCommandSource, const String&)
1118 frame.editor().transpose();
1122 static bool executeUnderline(LocalFrame& frame, Event*, EditorCommandSource source, const String&)
1124 RefPtrWillBeRawPtr<CSSPrimitiveValue> underline = CSSPrimitiveValue::createIdentifier(CSSValueUnderline);
1125 return executeToggleStyleInList(frame, source, EditActionUnderline, CSSPropertyWebkitTextDecorationsInEffect, underline.get());
1128 static bool executeUndo(LocalFrame& frame, Event*, EditorCommandSource, const String&)
1130 frame.editor().undo();
1134 static bool executeUnlink(LocalFrame& frame, Event*, EditorCommandSource, const String&)
1136 ASSERT(frame.document());
1137 UnlinkCommand::create(*frame.document())->apply();
1141 static bool executeUnscript(LocalFrame& frame, Event*, EditorCommandSource source, const String&)
1143 return executeApplyStyle(frame, source, EditActionUnscript, CSSPropertyVerticalAlign, "baseline");
1146 static bool executeUnselect(LocalFrame& frame, Event*, EditorCommandSource, const String&)
1148 frame.selection().clear();
1152 static bool executeYank(LocalFrame& frame, Event*, EditorCommandSource, const String&)
1154 frame.editor().insertTextWithoutSendingTextEvent(frame.editor().killRing().yank(), false, 0);
1155 frame.editor().killRing().setToYankedState();
1159 static bool executeYankAndSelect(LocalFrame& frame, Event*, EditorCommandSource, const String&)
1161 frame.editor().insertTextWithoutSendingTextEvent(frame.editor().killRing().yank(), true, 0);
1162 frame.editor().killRing().setToYankedState();
1166 // Supported functions
1168 static bool supported(LocalFrame*)
1173 static bool supportedFromMenuOrKeyBinding(LocalFrame*)
1178 static bool supportedCopyCut(LocalFrame* frame)
1183 Settings* settings = frame->settings();
1184 bool defaultValue = settings && settings->javaScriptCanAccessClipboard();
1185 return frame->editor().client().canCopyCut(frame, defaultValue);
1188 static bool supportedPaste(LocalFrame* frame)
1193 Settings* settings = frame->settings();
1194 bool defaultValue = settings && settings->javaScriptCanAccessClipboard() && settings->DOMPasteAllowed();
1195 return frame->editor().client().canPaste(frame, defaultValue);
1198 // Enabled functions
1200 static bool enabled(LocalFrame&, Event*, EditorCommandSource)
1205 static bool enabledVisibleSelection(LocalFrame& frame, Event* event, EditorCommandSource)
1207 // The term "visible" here includes a caret in editable text or a range in any text.
1208 const VisibleSelection& selection = frame.editor().selectionForCommand(event);
1209 return (selection.isCaret() && selection.isContentEditable()) || selection.isRange();
1212 static bool caretBrowsingEnabled(LocalFrame& frame)
1214 return frame.settings() && frame.settings()->caretBrowsingEnabled();
1217 static EditorCommandSource dummyEditorCommandSource = static_cast<EditorCommandSource>(0);
1219 static bool enabledVisibleSelectionOrCaretBrowsing(LocalFrame& frame, Event* event, EditorCommandSource)
1221 // The EditorCommandSource parameter is unused in enabledVisibleSelection, so just pass a dummy variable
1222 return caretBrowsingEnabled(frame) || enabledVisibleSelection(frame, event, dummyEditorCommandSource);
1225 static bool enabledVisibleSelectionAndMark(LocalFrame& frame, Event* event, EditorCommandSource)
1227 const VisibleSelection& selection = frame.editor().selectionForCommand(event);
1228 return ((selection.isCaret() && selection.isContentEditable()) || selection.isRange())
1229 && frame.editor().mark().isCaretOrRange();
1232 static bool enableCaretInEditableText(LocalFrame& frame, Event* event, EditorCommandSource)
1234 const VisibleSelection& selection = frame.editor().selectionForCommand(event);
1235 return selection.isCaret() && selection.isContentEditable();
1238 static bool enabledCopy(LocalFrame& frame, Event*, EditorCommandSource)
1240 return frame.editor().canDHTMLCopy() || frame.editor().canCopy();
1243 static bool enabledCut(LocalFrame& frame, Event*, EditorCommandSource)
1245 return frame.editor().canDHTMLCut() || frame.editor().canCut();
1248 static bool enabledInEditableText(LocalFrame& frame, Event* event, EditorCommandSource)
1250 return frame.editor().selectionForCommand(event).rootEditableElement();
1253 static bool enabledDelete(LocalFrame& frame, Event* event, EditorCommandSource source)
1256 case CommandFromMenuOrKeyBinding:
1257 return frame.editor().canDelete();
1258 case CommandFromDOM:
1259 case CommandFromDOMWithUserInterface:
1260 // "Delete" from DOM is like delete/backspace keypress, affects selected range if non-empty,
1261 // otherwise removes a character
1262 return enabledInEditableText(frame, event, source);
1264 ASSERT_NOT_REACHED();
1268 static bool enabledInEditableTextOrCaretBrowsing(LocalFrame& frame, Event* event, EditorCommandSource)
1270 // The EditorCommandSource parameter is unused in enabledInEditableText, so just pass a dummy variable
1271 return caretBrowsingEnabled(frame) || enabledInEditableText(frame, event, dummyEditorCommandSource);
1274 static bool enabledInRichlyEditableText(LocalFrame& frame, Event*, EditorCommandSource)
1276 return frame.selection().isCaretOrRange() && frame.selection().isContentRichlyEditable() && frame.selection().rootEditableElement();
1279 static bool enabledPaste(LocalFrame& frame, Event*, EditorCommandSource)
1281 return frame.editor().canPaste();
1284 static bool enabledRangeInEditableText(LocalFrame& frame, Event*, EditorCommandSource)
1286 return frame.selection().isRange() && frame.selection().isContentEditable();
1289 static bool enabledRangeInRichlyEditableText(LocalFrame& frame, Event*, EditorCommandSource)
1291 return frame.selection().isRange() && frame.selection().isContentRichlyEditable();
1294 static bool enabledRedo(LocalFrame& frame, Event*, EditorCommandSource)
1296 return frame.editor().canRedo();
1299 static bool enabledUndo(LocalFrame& frame, Event*, EditorCommandSource)
1301 return frame.editor().canUndo();
1306 static TriState stateNone(LocalFrame&, Event*)
1308 return FalseTriState;
1311 static TriState stateBold(LocalFrame& frame, Event*)
1313 return stateStyle(frame, CSSPropertyFontWeight, "bold");
1316 static TriState stateItalic(LocalFrame& frame, Event*)
1318 return stateStyle(frame, CSSPropertyFontStyle, "italic");
1321 static TriState stateOrderedList(LocalFrame& frame, Event*)
1323 return selectionListState(frame.selection(), olTag);
1326 static TriState stateStrikethrough(LocalFrame& frame, Event*)
1328 return stateStyle(frame, CSSPropertyWebkitTextDecorationsInEffect, "line-through");
1331 static TriState stateStyleWithCSS(LocalFrame& frame, Event*)
1333 return frame.editor().shouldStyleWithCSS() ? TrueTriState : FalseTriState;
1336 static TriState stateSubscript(LocalFrame& frame, Event*)
1338 return stateStyle(frame, CSSPropertyVerticalAlign, "sub");
1341 static TriState stateSuperscript(LocalFrame& frame, Event*)
1343 return stateStyle(frame, CSSPropertyVerticalAlign, "super");
1346 static TriState stateTextWritingDirectionLeftToRight(LocalFrame& frame, Event*)
1348 return stateTextWritingDirection(frame, LeftToRightWritingDirection);
1351 static TriState stateTextWritingDirectionNatural(LocalFrame& frame, Event*)
1353 return stateTextWritingDirection(frame, NaturalWritingDirection);
1356 static TriState stateTextWritingDirectionRightToLeft(LocalFrame& frame, Event*)
1358 return stateTextWritingDirection(frame, RightToLeftWritingDirection);
1361 static TriState stateUnderline(LocalFrame& frame, Event*)
1363 return stateStyle(frame, CSSPropertyWebkitTextDecorationsInEffect, "underline");
1366 static TriState stateUnorderedList(LocalFrame& frame, Event*)
1368 return selectionListState(frame.selection(), ulTag);
1371 static TriState stateJustifyCenter(LocalFrame& frame, Event*)
1373 return stateStyle(frame, CSSPropertyTextAlign, "center");
1376 static TriState stateJustifyFull(LocalFrame& frame, Event*)
1378 return stateStyle(frame, CSSPropertyTextAlign, "justify");
1381 static TriState stateJustifyLeft(LocalFrame& frame, Event*)
1383 return stateStyle(frame, CSSPropertyTextAlign, "left");
1386 static TriState stateJustifyRight(LocalFrame& frame, Event*)
1388 return stateStyle(frame, CSSPropertyTextAlign, "right");
1393 static String valueNull(LocalFrame&, Event*)
1398 static String valueBackColor(LocalFrame& frame, Event*)
1400 return valueStyle(frame, CSSPropertyBackgroundColor);
1403 static String valueDefaultParagraphSeparator(LocalFrame& frame, Event*)
1405 switch (frame.editor().defaultParagraphSeparator()) {
1406 case EditorParagraphSeparatorIsDiv:
1407 return divTag.localName();
1408 case EditorParagraphSeparatorIsP:
1409 return pTag.localName();
1412 ASSERT_NOT_REACHED();
1416 static String valueFontName(LocalFrame& frame, Event*)
1418 return valueStyle(frame, CSSPropertyFontFamily);
1421 static String valueFontSize(LocalFrame& frame, Event*)
1423 return valueStyle(frame, CSSPropertyFontSize);
1426 static String valueFontSizeDelta(LocalFrame& frame, Event*)
1428 return valueStyle(frame, CSSPropertyWebkitFontSizeDelta);
1431 static String valueForeColor(LocalFrame& frame, Event*)
1433 return valueStyle(frame, CSSPropertyColor);
1436 static String valueFormatBlock(LocalFrame& frame, Event*)
1438 const VisibleSelection& selection = frame.selection().selection();
1439 if (!selection.isNonOrphanedCaretOrRange() || !selection.isContentEditable())
1441 Element* formatBlockElement = FormatBlockCommand::elementForFormatBlockCommand(selection.firstRange().get());
1442 if (!formatBlockElement)
1444 return formatBlockElement->localName();
1449 struct CommandEntry {
1451 EditorInternalCommand command;
1454 static const CommandMap& createCommandMap()
1456 // If you add new commands, you should assign new Id to each idForUserMetrics and update MappedEditingCommands
1457 // in chrome/trunk/src/tools/metrics/histograms/histograms.xml.
1458 static const CommandEntry commands[] = {
1459 { "AlignCenter", {139, executeJustifyCenter, supportedFromMenuOrKeyBinding, enabledInRichlyEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1460 { "AlignJustified", {1, executeJustifyFull, supportedFromMenuOrKeyBinding, enabledInRichlyEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1461 { "AlignLeft", {2, executeJustifyLeft, supportedFromMenuOrKeyBinding, enabledInRichlyEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1462 { "AlignRight", {3, executeJustifyRight, supportedFromMenuOrKeyBinding, enabledInRichlyEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1463 { "BackColor", {4, executeBackColor, supported, enabledInRichlyEditableText, stateNone, valueBackColor, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1464 { "BackwardDelete", {5, executeDeleteBackward, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } }, // FIXME: remove BackwardDelete when Safari for Windows stops using it.
1465 { "Bold", {6, executeToggleBold, supported, enabledInRichlyEditableText, stateBold, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1466 { "Copy", {7, executeCopy, supportedCopyCut, enabledCopy, stateNone, valueNull, notTextInsertion, allowExecutionWhenDisabled } },
1467 { "CreateLink", {8, executeCreateLink, supported, enabledInRichlyEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1468 { "Cut", {9, executeCut, supportedCopyCut, enabledCut, stateNone, valueNull, notTextInsertion, allowExecutionWhenDisabled } },
1469 { "DefaultParagraphSeparator", {10, executeDefaultParagraphSeparator, supported, enabled, stateNone, valueDefaultParagraphSeparator, notTextInsertion, doNotAllowExecutionWhenDisabled} },
1470 { "Delete", {11, executeDelete, supported, enabledDelete, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1471 { "DeleteBackward", {12, executeDeleteBackward, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1472 { "DeleteBackwardByDecomposingPreviousCharacter", {13, executeDeleteBackwardByDecomposingPreviousCharacter, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1473 { "DeleteForward", {14, executeDeleteForward, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1474 { "DeleteToBeginningOfLine", {15, executeDeleteToBeginningOfLine, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1475 { "DeleteToBeginningOfParagraph", {16, executeDeleteToBeginningOfParagraph, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1476 { "DeleteToEndOfLine", {17, executeDeleteToEndOfLine, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1477 { "DeleteToEndOfParagraph", {18, executeDeleteToEndOfParagraph, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1478 { "DeleteToMark", {19, executeDeleteToMark, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1479 { "DeleteWordBackward", {20, executeDeleteWordBackward, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1480 { "DeleteWordForward", {21, executeDeleteWordForward, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1481 { "FindString", {22, executeFindString, supported, enabled, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1482 { "FontName", {23, executeFontName, supported, enabledInEditableText, stateNone, valueFontName, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1483 { "FontSize", {24, executeFontSize, supported, enabledInEditableText, stateNone, valueFontSize, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1484 { "FontSizeDelta", {25, executeFontSizeDelta, supported, enabledInEditableText, stateNone, valueFontSizeDelta, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1485 { "ForeColor", {26, executeForeColor, supported, enabledInRichlyEditableText, stateNone, valueForeColor, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1486 { "FormatBlock", {27, executeFormatBlock, supported, enabledInRichlyEditableText, stateNone, valueFormatBlock, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1487 { "ForwardDelete", {28, executeForwardDelete, supported, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1488 { "HiliteColor", {29, executeBackColor, supported, enabledInRichlyEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1489 { "IgnoreSpelling", {30, executeIgnoreSpelling, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1490 { "Indent", {31, executeIndent, supported, enabledInRichlyEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1491 { "InsertBacktab", {32, executeInsertBacktab, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, isTextInsertion, doNotAllowExecutionWhenDisabled } },
1492 { "InsertHTML", {33, executeInsertHTML, supported, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1493 { "InsertHorizontalRule", {34, executeInsertHorizontalRule, supported, enabledInRichlyEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1494 { "InsertImage", {35, executeInsertImage, supported, enabledInRichlyEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1495 { "InsertLineBreak", {36, executeInsertLineBreak, supported, enabledInEditableText, stateNone, valueNull, isTextInsertion, doNotAllowExecutionWhenDisabled } },
1496 { "InsertNewline", {37, executeInsertNewline, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, isTextInsertion, doNotAllowExecutionWhenDisabled } },
1497 { "InsertNewlineInQuotedContent", {38, executeInsertNewlineInQuotedContent, supported, enabledInRichlyEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1498 { "InsertOrderedList", {39, executeInsertOrderedList, supported, enabledInRichlyEditableText, stateOrderedList, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1499 { "InsertParagraph", {40, executeInsertParagraph, supported, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1500 { "InsertTab", {41, executeInsertTab, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, isTextInsertion, doNotAllowExecutionWhenDisabled } },
1501 { "InsertText", {42, executeInsertText, supported, enabledInEditableText, stateNone, valueNull, isTextInsertion, doNotAllowExecutionWhenDisabled } },
1502 { "InsertUnorderedList", {43, executeInsertUnorderedList, supported, enabledInRichlyEditableText, stateUnorderedList, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1503 { "Italic", {44, executeToggleItalic, supported, enabledInRichlyEditableText, stateItalic, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1504 { "JustifyCenter", {45, executeJustifyCenter, supported, enabledInRichlyEditableText, stateJustifyCenter, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1505 { "JustifyFull", {46, executeJustifyFull, supported, enabledInRichlyEditableText, stateJustifyFull, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1506 { "JustifyLeft", {47, executeJustifyLeft, supported, enabledInRichlyEditableText, stateJustifyLeft, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1507 { "JustifyNone", {48, executeJustifyLeft, supported, enabledInRichlyEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1508 { "JustifyRight", {49, executeJustifyRight, supported, enabledInRichlyEditableText, stateJustifyRight, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1509 { "MakeTextWritingDirectionLeftToRight", {50, executeMakeTextWritingDirectionLeftToRight, supportedFromMenuOrKeyBinding, enabledInRichlyEditableText, stateTextWritingDirectionLeftToRight, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1510 { "MakeTextWritingDirectionNatural", {51, executeMakeTextWritingDirectionNatural, supportedFromMenuOrKeyBinding, enabledInRichlyEditableText, stateTextWritingDirectionNatural, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1511 { "MakeTextWritingDirectionRightToLeft", {52, executeMakeTextWritingDirectionRightToLeft, supportedFromMenuOrKeyBinding, enabledInRichlyEditableText, stateTextWritingDirectionRightToLeft, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1512 { "MoveBackward", {53, executeMoveBackward, supportedFromMenuOrKeyBinding, enabledInEditableTextOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1513 { "MoveBackwardAndModifySelection", {54, executeMoveBackwardAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelectionOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1514 { "MoveDown", {55, executeMoveDown, supportedFromMenuOrKeyBinding, enabledInEditableTextOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1515 { "MoveDownAndModifySelection", {56, executeMoveDownAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelectionOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1516 { "MoveForward", {57, executeMoveForward, supportedFromMenuOrKeyBinding, enabledInEditableTextOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1517 { "MoveForwardAndModifySelection", {58, executeMoveForwardAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelectionOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1518 { "MoveLeft", {59, executeMoveLeft, supportedFromMenuOrKeyBinding, enabledInEditableTextOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1519 { "MoveLeftAndModifySelection", {60, executeMoveLeftAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelectionOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1520 { "MovePageDown", {61, executeMovePageDown, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1521 { "MovePageDownAndModifySelection", {62, executeMovePageDownAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelection, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1522 { "MovePageUp", {63, executeMovePageUp, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1523 { "MovePageUpAndModifySelection", {64, executeMovePageUpAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelection, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1524 { "MoveParagraphBackward", {65, executeMoveParagraphBackward, supportedFromMenuOrKeyBinding, enabledInEditableTextOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1525 { "MoveParagraphBackwardAndModifySelection", {66, executeMoveParagraphBackwardAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelectionOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1526 { "MoveParagraphForward", {67, executeMoveParagraphForward, supportedFromMenuOrKeyBinding, enabledInEditableTextOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1527 { "MoveParagraphForwardAndModifySelection", {68, executeMoveParagraphForwardAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelectionOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1528 { "MoveRight", {69, executeMoveRight, supportedFromMenuOrKeyBinding, enabledInEditableTextOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1529 { "MoveRightAndModifySelection", {70, executeMoveRightAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelectionOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1530 { "MoveToBeginningOfDocument", {71, executeMoveToBeginningOfDocument, supportedFromMenuOrKeyBinding, enabledInEditableTextOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1531 { "MoveToBeginningOfDocumentAndModifySelection", {72, executeMoveToBeginningOfDocumentAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelectionOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1532 { "MoveToBeginningOfLine", {73, executeMoveToBeginningOfLine, supportedFromMenuOrKeyBinding, enabledInEditableTextOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1533 { "MoveToBeginningOfLineAndModifySelection", {74, executeMoveToBeginningOfLineAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelectionOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1534 { "MoveToBeginningOfParagraph", {75, executeMoveToBeginningOfParagraph, supportedFromMenuOrKeyBinding, enabledInEditableTextOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1535 { "MoveToBeginningOfParagraphAndModifySelection", {76, executeMoveToBeginningOfParagraphAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelectionOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1536 { "MoveToBeginningOfSentence", {77, executeMoveToBeginningOfSentence, supportedFromMenuOrKeyBinding, enabledInEditableTextOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1537 { "MoveToBeginningOfSentenceAndModifySelection", {78, executeMoveToBeginningOfSentenceAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelectionOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1538 { "MoveToEndOfDocument", {79, executeMoveToEndOfDocument, supportedFromMenuOrKeyBinding, enabledInEditableTextOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1539 { "MoveToEndOfDocumentAndModifySelection", {80, executeMoveToEndOfDocumentAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelectionOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1540 { "MoveToEndOfLine", {81, executeMoveToEndOfLine, supportedFromMenuOrKeyBinding, enabledInEditableTextOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1541 { "MoveToEndOfLineAndModifySelection", {82, executeMoveToEndOfLineAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelectionOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1542 { "MoveToEndOfParagraph", {83, executeMoveToEndOfParagraph, supportedFromMenuOrKeyBinding, enabledInEditableTextOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1543 { "MoveToEndOfParagraphAndModifySelection", {84, executeMoveToEndOfParagraphAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelectionOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1544 { "MoveToEndOfSentence", {85, executeMoveToEndOfSentence, supportedFromMenuOrKeyBinding, enabledInEditableTextOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1545 { "MoveToEndOfSentenceAndModifySelection", {86, executeMoveToEndOfSentenceAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelectionOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1546 { "MoveToLeftEndOfLine", {87, executeMoveToLeftEndOfLine, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1547 { "MoveToLeftEndOfLineAndModifySelection", {88, executeMoveToLeftEndOfLineAndModifySelection, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1548 { "MoveToRightEndOfLine", {89, executeMoveToRightEndOfLine, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1549 { "MoveToRightEndOfLineAndModifySelection", {90, executeMoveToRightEndOfLineAndModifySelection, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1550 { "MoveUp", {91, executeMoveUp, supportedFromMenuOrKeyBinding, enabledInEditableTextOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1551 { "MoveUpAndModifySelection", {92, executeMoveUpAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelectionOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1552 { "MoveWordBackward", {93, executeMoveWordBackward, supportedFromMenuOrKeyBinding, enabledInEditableTextOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1553 { "MoveWordBackwardAndModifySelection", {94, executeMoveWordBackwardAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelectionOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1554 { "MoveWordForward", {95, executeMoveWordForward, supportedFromMenuOrKeyBinding, enabledInEditableTextOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1555 { "MoveWordForwardAndModifySelection", {96, executeMoveWordForwardAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelectionOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1556 { "MoveWordLeft", {97, executeMoveWordLeft, supportedFromMenuOrKeyBinding, enabledInEditableTextOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1557 { "MoveWordLeftAndModifySelection", {98, executeMoveWordLeftAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelectionOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1558 { "MoveWordRight", {99, executeMoveWordRight, supportedFromMenuOrKeyBinding, enabledInEditableTextOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1559 { "MoveWordRightAndModifySelection", {100, executeMoveWordRightAndModifySelection, supportedFromMenuOrKeyBinding, enabledVisibleSelectionOrCaretBrowsing, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1560 { "Outdent", {101, executeOutdent, supported, enabledInRichlyEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1561 { "OverWrite", {102, executeToggleOverwrite, supportedFromMenuOrKeyBinding, enabledInRichlyEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1562 { "Paste", {103, executePaste, supportedPaste, enabledPaste, stateNone, valueNull, notTextInsertion, allowExecutionWhenDisabled } },
1563 { "PasteAndMatchStyle", {104, executePasteAndMatchStyle, supportedPaste, enabledPaste, stateNone, valueNull, notTextInsertion, allowExecutionWhenDisabled } },
1564 { "PasteGlobalSelection", {105, executePasteGlobalSelection, supportedFromMenuOrKeyBinding, enabledPaste, stateNone, valueNull, notTextInsertion, allowExecutionWhenDisabled } },
1565 { "Print", {106, executePrint, supported, enabled, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1566 { "Redo", {107, executeRedo, supported, enabledRedo, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1567 { "RemoveFormat", {108, executeRemoveFormat, supported, enabledRangeInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1568 { "ScrollPageBackward", {109, executeScrollPageBackward, supportedFromMenuOrKeyBinding, enabled, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1569 { "ScrollPageForward", {110, executeScrollPageForward, supportedFromMenuOrKeyBinding, enabled, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1570 { "ScrollLineUp", {111, executeScrollLineUp, supportedFromMenuOrKeyBinding, enabled, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1571 { "ScrollLineDown", {112, executeScrollLineDown, supportedFromMenuOrKeyBinding, enabled, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1572 { "ScrollToBeginningOfDocument", {113, executeScrollToBeginningOfDocument, supportedFromMenuOrKeyBinding, enabled, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1573 { "ScrollToEndOfDocument", {114, executeScrollToEndOfDocument, supportedFromMenuOrKeyBinding, enabled, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1574 { "SelectAll", {115, executeSelectAll, supported, enabled, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1575 { "SelectLine", {116, executeSelectLine, supportedFromMenuOrKeyBinding, enabledVisibleSelection, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1576 { "SelectParagraph", {117, executeSelectParagraph, supportedFromMenuOrKeyBinding, enabledVisibleSelection, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1577 { "SelectSentence", {118, executeSelectSentence, supportedFromMenuOrKeyBinding, enabledVisibleSelection, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1578 { "SelectToMark", {119, executeSelectToMark, supportedFromMenuOrKeyBinding, enabledVisibleSelectionAndMark, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1579 { "SelectWord", {120, executeSelectWord, supportedFromMenuOrKeyBinding, enabledVisibleSelection, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1580 { "SetMark", {121, executeSetMark, supportedFromMenuOrKeyBinding, enabledVisibleSelection, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1581 { "Strikethrough", {122, executeStrikethrough, supported, enabledInRichlyEditableText, stateStrikethrough, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1582 { "StyleWithCSS", {123, executeStyleWithCSS, supported, enabled, stateStyleWithCSS, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1583 { "Subscript", {124, executeSubscript, supported, enabledInRichlyEditableText, stateSubscript, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1584 { "Superscript", {125, executeSuperscript, supported, enabledInRichlyEditableText, stateSuperscript, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1585 { "SwapWithMark", {126, executeSwapWithMark, supportedFromMenuOrKeyBinding, enabledVisibleSelectionAndMark, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1586 { "ToggleBold", {127, executeToggleBold, supportedFromMenuOrKeyBinding, enabledInRichlyEditableText, stateBold, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1587 { "ToggleItalic", {128, executeToggleItalic, supportedFromMenuOrKeyBinding, enabledInRichlyEditableText, stateItalic, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1588 { "ToggleUnderline", {129, executeUnderline, supportedFromMenuOrKeyBinding, enabledInRichlyEditableText, stateUnderline, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1589 { "Transpose", {130, executeTranspose, supported, enableCaretInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1590 { "Underline", {131, executeUnderline, supported, enabledInRichlyEditableText, stateUnderline, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1591 { "Undo", {132, executeUndo, supported, enabledUndo, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1592 { "Unlink", {133, executeUnlink, supported, enabledRangeInRichlyEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1593 { "Unscript", {134, executeUnscript, supportedFromMenuOrKeyBinding, enabledInRichlyEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1594 { "Unselect", {135, executeUnselect, supported, enabledVisibleSelection, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1595 { "UseCSS", {136, executeUseCSS, supported, enabled, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1596 { "Yank", {137, executeYank, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1597 { "YankAndSelect", {138, executeYankAndSelect, supportedFromMenuOrKeyBinding, enabledInEditableText, stateNone, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },
1600 // These unsupported commands are listed here since they appear in the Microsoft
1601 // documentation used as the starting point for our DOM executeCommand support.
1603 // 2D-Position (not supported)
1604 // AbsolutePosition (not supported)
1605 // BlockDirLTR (not supported)
1606 // BlockDirRTL (not supported)
1607 // BrowseMode (not supported)
1608 // ClearAuthenticationCache (not supported)
1609 // CreateBookmark (not supported)
1610 // DirLTR (not supported)
1611 // DirRTL (not supported)
1612 // EditMode (not supported)
1613 // InlineDirLTR (not supported)
1614 // InlineDirRTL (not supported)
1615 // InsertButton (not supported)
1616 // InsertFieldSet (not supported)
1617 // InsertIFrame (not supported)
1618 // InsertInputButton (not supported)
1619 // InsertInputCheckbox (not supported)
1620 // InsertInputFileUpload (not supported)
1621 // InsertInputHidden (not supported)
1622 // InsertInputImage (not supported)
1623 // InsertInputPassword (not supported)
1624 // InsertInputRadio (not supported)
1625 // InsertInputReset (not supported)
1626 // InsertInputSubmit (not supported)
1627 // InsertInputText (not supported)
1628 // InsertMarquee (not supported)
1629 // InsertSelectDropDown (not supported)
1630 // InsertSelectListBox (not supported)
1631 // InsertTextArea (not supported)
1632 // LiveResize (not supported)
1633 // MultipleSelection (not supported)
1634 // Open (not supported)
1635 // PlayImage (not supported)
1636 // Refresh (not supported)
1637 // RemoveParaFormat (not supported)
1638 // SaveAs (not supported)
1639 // SizeToControl (not supported)
1640 // SizeToControlHeight (not supported)
1641 // SizeToControlWidth (not supported)
1642 // Stop (not supported)
1643 // StopImage (not supported)
1644 // Unbookmark (not supported)
1646 CommandMap& commandMap = *new CommandMap;
1650 for (size_t i = 0; i < WTF_ARRAY_LENGTH(commands); ++i) {
1651 const CommandEntry& command = commands[i];
1652 ASSERT(!commandMap.get(command.name));
1653 commandMap.set(command.name, &command.command);
1655 ASSERT(!idSet.contains(command.command.idForUserMetrics));
1656 idSet.add(command.command.idForUserMetrics);
1663 static const EditorInternalCommand* internalCommand(const String& commandName)
1665 static const CommandMap& commandMap = createCommandMap();
1666 return commandName.isEmpty() ? 0 : commandMap.get(commandName);
1669 Editor::Command Editor::command(const String& commandName)
1671 return Command(internalCommand(commandName), CommandFromMenuOrKeyBinding, m_frame);
1674 Editor::Command Editor::command(const String& commandName, EditorCommandSource source)
1676 return Command(internalCommand(commandName), source, m_frame);
1679 bool Editor::executeCommand(const String& commandName)
1681 // Specially handling commands that Editor::execCommand does not directly
1683 if (commandName == "DeleteToEndOfParagraph") {
1684 if (!deleteWithDirection(DirectionForward, ParagraphBoundary, true, false))
1685 deleteWithDirection(DirectionForward, CharacterGranularity, true, false);
1688 if (commandName == "DeleteBackward")
1689 return command(AtomicString("BackwardDelete")).execute();
1690 if (commandName == "DeleteForward")
1691 return command(AtomicString("ForwardDelete")).execute();
1692 if (commandName == "AdvanceToNextMisspelling") {
1693 // Wee need to pass false here or else the currently selected word will never be skipped.
1694 spellChecker().advanceToNextMisspelling(false);
1697 if (commandName == "ToggleSpellPanel") {
1698 spellChecker().showSpellingGuessPanel();
1701 return command(commandName).execute();
1704 bool Editor::executeCommand(const String& commandName, const String& value)
1706 // moveToBeginningOfDocument and moveToEndfDocument are only handled by WebKit for editable nodes.
1707 if (!canEdit() && commandName == "moveToBeginningOfDocument")
1708 return frame().eventHandler().bubblingScroll(ScrollUp, ScrollByDocument);
1710 if (!canEdit() && commandName == "moveToEndOfDocument")
1711 return frame().eventHandler().bubblingScroll(ScrollDown, ScrollByDocument);
1713 if (commandName == "showGuessPanel") {
1714 spellChecker().showSpellingGuessPanel();
1718 return command(commandName).execute(value);
1721 Editor::Command::Command()
1726 Editor::Command::Command(const EditorInternalCommand* command, EditorCommandSource source, PassRefPtrWillBeRawPtr<LocalFrame> frame)
1727 : m_command(command)
1729 , m_frame(command ? frame : nullptr)
1731 // Use separate assertions so we can tell which bad thing happened.
1738 bool Editor::Command::execute(const String& parameter, Event* triggeringEvent) const
1740 if (!isEnabled(triggeringEvent)) {
1741 // Let certain commands be executed when performed explicitly even if they are disabled.
1742 if (!isSupported() || !m_frame || !m_command->allowExecutionWhenDisabled)
1745 frame().document()->updateLayoutIgnorePendingStylesheets();
1746 blink::Platform::current()->histogramSparse("WebCore.Editing.Commands", m_command->idForUserMetrics);
1747 return m_command->execute(*m_frame, triggeringEvent, m_source, parameter);
1750 bool Editor::Command::execute(Event* triggeringEvent) const
1752 return execute(String(), triggeringEvent);
1755 bool Editor::Command::isSupported() const
1760 case CommandFromMenuOrKeyBinding:
1762 case CommandFromDOM:
1763 case CommandFromDOMWithUserInterface:
1764 return m_command->isSupportedFromDOM(m_frame.get());
1766 ASSERT_NOT_REACHED();
1770 bool Editor::Command::isEnabled(Event* triggeringEvent) const
1772 if (!isSupported() || !m_frame)
1774 return m_command->isEnabled(*m_frame, triggeringEvent, m_source);
1777 TriState Editor::Command::state(Event* triggeringEvent) const
1779 if (!isSupported() || !m_frame)
1780 return FalseTriState;
1781 return m_command->state(*m_frame, triggeringEvent);
1784 String Editor::Command::value(Event* triggeringEvent) const
1786 if (!isSupported() || !m_frame)
1788 if (m_command->value == valueNull && m_command->state != stateNone)
1789 return m_command->state(*m_frame, triggeringEvent) == TrueTriState ? "true" : "false";
1790 return m_command->value(*m_frame, triggeringEvent);
1793 bool Editor::Command::isTextInsertion() const
1795 return m_command && m_command->isTextInsertion;
1798 int Editor::Command::idForHistogram() const
1800 return isSupported() ? m_command->idForUserMetrics : 0;
1803 } // namespace blink