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