2 * Copyright (C) 2006-2016 Apple Inc. All rights reserved.
3 * Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include "AXObjectCache.h"
31 #include "AlternativeTextController.h"
32 #include "ApplyStyleCommand.h"
33 #include "CSSComputedStyleDeclaration.h"
34 #include "CSSPropertyNames.h"
35 #include "CachedResourceLoader.h"
36 #include "ClipboardEvent.h"
37 #include "CompositionEvent.h"
38 #include "CreateLinkCommand.h"
39 #include "DataTransfer.h"
40 #include "DeleteSelectionCommand.h"
41 #include "DictationAlternative.h"
42 #include "DictationCommand.h"
43 #include "DocumentFragment.h"
44 #include "DocumentMarkerController.h"
46 #include "EditorClient.h"
47 #include "EventHandler.h"
48 #include "EventNames.h"
49 #include "FocusController.h"
51 #include "FrameLoader.h"
52 #include "FrameTree.h"
53 #include "FrameView.h"
54 #include "GraphicsContext.h"
55 #include "HTMLCollection.h"
56 #include "HTMLFormControlElement.h"
57 #include "HTMLFrameOwnerElement.h"
58 #include "HTMLImageElement.h"
59 #include "HTMLInputElement.h"
60 #include "HTMLNames.h"
61 #include "HTMLSpanElement.h"
62 #include "HitTestResult.h"
63 #include "IndentOutdentCommand.h"
64 #include "InputEvent.h"
65 #include "InsertListCommand.h"
66 #include "InsertTextCommand.h"
67 #include "KeyboardEvent.h"
69 #include "MainFrame.h"
70 #include "ModifySelectionListLevel.h"
72 #include "NodeTraversal.h"
74 #include "Pasteboard.h"
76 #include "RemoveFormatCommand.h"
77 #include "RenderBlock.h"
78 #include "RenderTextControl.h"
79 #include "RenderedDocumentMarker.h"
80 #include "RenderedPosition.h"
81 #include "ReplaceRangeWithTextCommand.h"
82 #include "ReplaceSelectionCommand.h"
84 #include "ShadowRoot.h"
85 #include "SimplifyMarkupCommand.h"
86 #include "SpellChecker.h"
87 #include "SpellingCorrectionCommand.h"
88 #include "StyleProperties.h"
89 #include "TelephoneNumberDetector.h"
91 #include "TextCheckerClient.h"
92 #include "TextCheckingHelper.h"
93 #include "TextEvent.h"
94 #include "TextIterator.h"
95 #include "TypingCommand.h"
96 #include "UserTypingGestureIndicator.h"
97 #include "VisibleUnits.h"
99 #include <pal/system/Sound.h>
100 #include <pal/text/KillRing.h>
101 #include <wtf/unicode/CharacterNames.h>
104 #include "ServicesOverlayController.h"
109 static bool dispatchBeforeInputEvent(Element& element, const AtomicString& inputType, const String& data = { }, RefPtr<DataTransfer>&& dataTransfer = nullptr, const Vector<RefPtr<StaticRange>>& targetRanges = { }, bool cancelable = true)
111 if (!element.document().settings().inputEventsEnabled())
114 return element.dispatchEvent(InputEvent::create(eventNames().beforeinputEvent, inputType, true, cancelable, element.document().defaultView(), data, WTFMove(dataTransfer), targetRanges, 0));
117 static void dispatchInputEvent(Element& element, const AtomicString& inputType, const String& data = { }, RefPtr<DataTransfer>&& dataTransfer = nullptr, const Vector<RefPtr<StaticRange>>& targetRanges = { })
119 if (element.document().settings().inputEventsEnabled()) {
120 // FIXME: We should not be dispatching to the scoped queue here. Normally, input events are dispatched in CompositeEditCommand::apply after the end of the scope,
121 // but TypingCommands are special in that existing TypingCommands that are applied again fire input events *from within* the scope by calling typingAddedToOpenCommand.
122 // Instead, TypingCommands should always dispatch events synchronously after the end of the scoped queue in CompositeEditCommand::apply. To work around this for the
123 // time being, just revert back to calling dispatchScopedEvent.
124 element.dispatchScopedEvent(InputEvent::create(eventNames().inputEvent, inputType, true, false, element.document().defaultView(), data, WTFMove(dataTransfer), targetRanges, 0));
126 element.dispatchInputEvent();
129 static String inputEventDataForEditingStyleAndAction(const StyleProperties* style, EditAction action)
135 case EditActionSetColor:
136 return style->getPropertyValue(CSSPropertyColor);
137 case EditActionSetWritingDirection:
138 return style->getPropertyValue(CSSPropertyDirection);
144 static String inputEventDataForEditingStyleAndAction(EditingStyle& style, EditAction action)
146 return inputEventDataForEditingStyleAndAction(style.style(), action);
149 class ClearTextCommand : public DeleteSelectionCommand {
151 ClearTextCommand(Document& document);
152 static void CreateAndApply(const RefPtr<Frame> frame);
155 EditAction editingAction() const override;
158 ClearTextCommand::ClearTextCommand(Document& document)
159 : DeleteSelectionCommand(document, false, true, false, false, true)
163 EditAction ClearTextCommand::editingAction() const
165 return EditActionDelete;
168 void ClearTextCommand::CreateAndApply(const RefPtr<Frame> frame)
170 if (frame->selection().isNone())
173 // Don't leave around stale composition state.
174 frame->editor().clear();
176 const VisibleSelection oldSelection = frame->selection().selection();
177 frame->selection().selectAll();
178 auto clearCommand = adoptRef(*new ClearTextCommand(*frame->document()));
179 clearCommand->setStartingSelection(oldSelection);
180 clearCommand->apply();
183 using namespace HTMLNames;
185 using namespace Unicode;
187 TemporarySelectionChange::TemporarySelectionChange(Frame& frame, std::optional<VisibleSelection> temporarySelection, TemporarySelectionOptions options)
190 , m_wasIgnoringSelectionChanges(frame.editor().ignoreSelectionChanges())
192 , m_appearanceUpdatesWereEnabled(frame.selection().isUpdateAppearanceEnabled())
196 if (options & TemporarySelectionOptionEnableAppearanceUpdates)
197 frame.selection().setUpdateAppearanceEnabled(true);
200 if (options & TemporarySelectionOptionIgnoreSelectionChanges)
201 frame.editor().setIgnoreSelectionChanges(true);
203 if (temporarySelection) {
204 m_selectionToRestore = frame.selection().selection();
205 frame.selection().setSelection(temporarySelection.value());
209 TemporarySelectionChange::~TemporarySelectionChange()
211 if (m_selectionToRestore)
212 m_frame->selection().setSelection(m_selectionToRestore.value());
214 if (m_options & TemporarySelectionOptionIgnoreSelectionChanges) {
215 auto revealSelection = m_options & TemporarySelectionOptionRevealSelection ? Editor::RevealSelection::Yes : Editor::RevealSelection::No;
216 m_frame->editor().setIgnoreSelectionChanges(m_wasIgnoringSelectionChanges, revealSelection);
220 if (m_options & TemporarySelectionOptionEnableAppearanceUpdates)
221 m_frame->selection().setUpdateAppearanceEnabled(m_appearanceUpdatesWereEnabled);
225 // When an event handler has moved the selection outside of a text control
226 // we should use the target control's selection for this editing operation.
227 VisibleSelection Editor::selectionForCommand(Event* event)
229 VisibleSelection selection = m_frame.selection().selection();
232 // If the target is a text control, and the current selection is outside of its shadow tree,
233 // then use the saved selection for that text control.
234 HTMLTextFormControlElement* textFormControlOfSelectionStart = enclosingTextFormControl(selection.start());
235 HTMLTextFormControlElement* textFromControlOfTarget = is<HTMLTextFormControlElement>(*event->target()->toNode()) ? downcast<HTMLTextFormControlElement>(event->target()->toNode()) : nullptr;
236 if (textFromControlOfTarget && (selection.start().isNull() || textFromControlOfTarget != textFormControlOfSelectionStart)) {
237 if (RefPtr<Range> range = textFromControlOfTarget->selection())
238 return VisibleSelection(*range, DOWNSTREAM, selection.isDirectional());
243 // Function considers Mac editing behavior a fallback when Page or Settings is not available.
244 EditingBehavior Editor::behavior() const
246 return EditingBehavior(m_frame.settings().editingBehaviorType());
249 EditorClient* Editor::client() const
251 if (Page* page = m_frame.page())
252 return &page->editorClient();
256 TextCheckerClient* Editor::textChecker() const
258 if (EditorClient* owner = client())
259 return owner->textChecker();
263 void Editor::handleKeyboardEvent(KeyboardEvent& event)
265 if (EditorClient* c = client())
266 c->handleKeyboardEvent(&event);
269 void Editor::handleInputMethodKeydown(KeyboardEvent& event)
271 if (EditorClient* c = client())
272 c->handleInputMethodKeydown(&event);
275 bool Editor::handleTextEvent(TextEvent& event)
277 LOG(Editing, "Editor %p handleTextEvent (data %s)", this, event.data().utf8().data());
279 // Default event handling for Drag and Drop will be handled by DragController
280 // so we leave the event for it.
284 if (event.isPaste()) {
285 if (event.pastingFragment()) {
287 if (client()->performsTwoStepPaste(event.pastingFragment()))
290 replaceSelectionWithFragment(*event.pastingFragment(), false, event.shouldSmartReplace(), event.shouldMatchStyle(), EditActionPaste, event.mailBlockquoteHandling());
292 replaceSelectionWithText(event.data(), false, event.shouldSmartReplace(), EditActionPaste);
296 String data = event.data();
298 if (event.isLineBreak())
299 return insertLineBreak();
300 return insertParagraphSeparator();
303 return insertTextWithoutSendingTextEvent(data, false, &event);
306 bool Editor::canEdit() const
308 return m_frame.selection().selection().rootEditableElement();
311 bool Editor::canEditRichly() const
313 return m_frame.selection().selection().isContentRichlyEditable();
316 // Returns whether caller should continue with "the default processing", which is the same as
317 // the event handler NOT setting the return value to false
318 // https://w3c.github.io/clipboard-apis/#fire-a-clipboard-event
319 static bool dispatchClipboardEvent(RefPtr<Element>&& target, const AtomicString& eventType)
321 // FIXME: Move the target selection code here.
325 DataTransfer::StoreMode storeMode;
326 if (eventType == eventNames().pasteEvent)
327 storeMode = DataTransfer::StoreMode::Readonly;
328 else if (eventType == eventNames().copyEvent || eventType == eventNames().cutEvent)
329 storeMode = DataTransfer::StoreMode::ReadWrite;
331 ASSERT(eventType == eventNames().beforecutEvent || eventType == eventNames().beforecopyEvent || eventType == eventNames().beforepasteEvent);
332 storeMode = DataTransfer::StoreMode::Invalid;
335 auto dataTransfer = DataTransfer::createForCopyAndPaste(storeMode);
337 ClipboardEvent::Init init;
339 init.cancelable = true;
340 init.clipboardData = dataTransfer.ptr();
341 auto event = ClipboardEvent::create(eventType, init, Event::IsTrusted::Yes);
343 target->dispatchEvent(event);
344 bool noDefaultProcessing = event->defaultPrevented();
345 if (noDefaultProcessing && storeMode == DataTransfer::StoreMode::ReadWrite) {
346 auto pasteboard = Pasteboard::createForCopyAndPaste();
348 pasteboard->writePasteboard(dataTransfer->pasteboard());
351 dataTransfer->makeInvalidForSecurity();
353 return !noDefaultProcessing;
356 // WinIE uses onbeforecut and onbeforepaste to enables the cut and paste menu items. They
357 // also send onbeforecopy, apparently for symmetry, but it doesn't affect the menu items.
358 // We need to use onbeforecopy as a real menu enabler because we allow elements that are not
359 // normally selectable to implement copy/paste (like divs, or a document body).
361 bool Editor::canDHTMLCut()
363 if (m_frame.selection().selection().isInPasswordField())
366 return !dispatchClipboardEvent(findEventTargetFromSelection(), eventNames().beforecutEvent);
369 bool Editor::canDHTMLCopy()
371 if (m_frame.selection().selection().isInPasswordField())
373 return !dispatchClipboardEvent(findEventTargetFromSelection(), eventNames().beforecopyEvent);
376 bool Editor::canDHTMLPaste()
378 return !dispatchClipboardEvent(findEventTargetFromSelection(), eventNames().beforepasteEvent);
381 bool Editor::canCut() const
383 return canCopy() && canDelete();
386 static HTMLImageElement* imageElementFromImageDocument(Document& document)
388 if (!document.isImageDocument())
391 HTMLElement* body = document.bodyOrFrameset();
395 Node* node = body->firstChild();
396 if (!is<HTMLImageElement>(node))
398 return downcast<HTMLImageElement>(node);
401 bool Editor::canCopy() const
403 if (imageElementFromImageDocument(document()))
405 const VisibleSelection& selection = m_frame.selection().selection();
406 return selection.isRange() && !selection.isInPasswordField();
409 bool Editor::canPaste() const
414 bool Editor::canDelete() const
416 const VisibleSelection& selection = m_frame.selection().selection();
417 return selection.isRange() && selection.rootEditableElement();
420 bool Editor::canDeleteRange(Range* range) const
422 Node& startContainer = range->startContainer();
423 Node& endContainer = range->endContainer();
425 if (!startContainer.hasEditableStyle() || !endContainer.hasEditableStyle())
428 if (range->collapsed()) {
429 VisiblePosition start(range->startPosition(), DOWNSTREAM);
430 VisiblePosition previous = start.previous();
431 // FIXME: We sometimes allow deletions at the start of editable roots, like when the caret is in an empty list item.
432 if (previous.isNull() || previous.deepEquivalent().deprecatedNode()->rootEditableElement() != startContainer.rootEditableElement())
438 bool Editor::smartInsertDeleteEnabled()
440 return client() && client()->smartInsertDeleteEnabled();
443 bool Editor::canSmartCopyOrDelete()
445 return client() && client()->smartInsertDeleteEnabled() && m_frame.selection().granularity() == WordGranularity;
448 bool Editor::isSelectTrailingWhitespaceEnabled() const
450 return client() && client()->isSelectTrailingWhitespaceEnabled();
453 bool Editor::deleteWithDirection(SelectionDirection direction, TextGranularity granularity, bool shouldAddToKillRing, bool isTypingAction)
458 if (m_frame.selection().isRange()) {
459 if (isTypingAction) {
460 TypingCommand::deleteKeyPressed(document(), canSmartCopyOrDelete() ? TypingCommand::SmartDelete : 0, granularity);
461 revealSelectionAfterEditingOperation();
463 if (shouldAddToKillRing)
464 addRangeToKillRing(*selectedRange().get(), KillRingInsertionMode::AppendText);
465 deleteSelectionWithSmartDelete(canSmartCopyOrDelete());
466 // Implicitly calls revealSelectionAfterEditingOperation().
469 TypingCommand::Options options = 0;
470 if (canSmartCopyOrDelete())
471 options |= TypingCommand::SmartDelete;
472 if (shouldAddToKillRing)
473 options |= TypingCommand::AddsToKillRing;
475 case DirectionForward:
477 TypingCommand::forwardDeleteKeyPressed(document(), options, granularity);
479 case DirectionBackward:
481 TypingCommand::deleteKeyPressed(document(), options, granularity);
484 revealSelectionAfterEditingOperation();
487 // FIXME: We should to move this down into deleteKeyPressed.
488 // clear the "start new kill ring sequence" setting, because it was set to true
489 // when the selection was updated by deleting the range
490 if (shouldAddToKillRing)
491 setStartNewKillRingSequence(false);
496 void Editor::deleteSelectionWithSmartDelete(bool smartDelete, EditAction editingAction)
498 if (m_frame.selection().isNone())
501 DeleteSelectionCommand::create(document(), smartDelete, true, false, false, true, editingAction)->apply();
504 void Editor::clearText()
506 ClearTextCommand::CreateAndApply(&m_frame);
509 void Editor::pasteAsPlainText(const String& pastingText, bool smartReplace)
511 Element* target = findEventTargetFromSelection();
514 target->dispatchEvent(TextEvent::createForPlainTextPaste(document().domWindow(), pastingText, smartReplace));
517 void Editor::pasteAsFragment(Ref<DocumentFragment>&& pastingFragment, bool smartReplace, bool matchStyle, MailBlockquoteHandling respectsMailBlockquote)
519 Element* target = findEventTargetFromSelection();
522 target->dispatchEvent(TextEvent::createForFragmentPaste(document().domWindow(), WTFMove(pastingFragment), smartReplace, matchStyle, respectsMailBlockquote));
525 void Editor::pasteAsPlainTextBypassingDHTML()
527 pasteAsPlainTextWithPasteboard(*Pasteboard::createForCopyAndPaste());
530 void Editor::pasteAsPlainTextWithPasteboard(Pasteboard& pasteboard)
532 String text = readPlainTextFromPasteboard(pasteboard);
533 if (client() && client()->shouldInsertText(text, selectedRange().get(), EditorInsertAction::Pasted))
534 pasteAsPlainText(text, canSmartReplaceWithPasteboard(pasteboard));
537 String Editor::readPlainTextFromPasteboard(Pasteboard& pasteboard)
539 PasteboardPlainText text;
540 pasteboard.read(text);
541 return plainTextFromPasteboard(text);
546 String Editor::plainTextFromPasteboard(const PasteboardPlainText& text)
553 bool Editor::canSmartReplaceWithPasteboard(Pasteboard& pasteboard)
555 return client() && client()->smartInsertDeleteEnabled() && pasteboard.canSmartReplace();
558 bool Editor::shouldInsertFragment(DocumentFragment& fragment, Range* replacingDOMRange, EditorInsertAction givenAction)
563 auto* child = fragment.firstChild();
564 if (is<CharacterData>(child) && fragment.lastChild() == child)
565 return client()->shouldInsertText(downcast<CharacterData>(*child).data(), replacingDOMRange, givenAction);
567 return client()->shouldInsertNode(&fragment, replacingDOMRange, givenAction);
570 void Editor::replaceSelectionWithFragment(DocumentFragment& fragment, bool selectReplacement, bool smartReplace, bool matchStyle, EditAction editingAction, MailBlockquoteHandling mailBlockquoteHandling)
572 VisibleSelection selection = m_frame.selection().selection();
573 if (selection.isNone() || !selection.isContentEditable())
576 AccessibilityReplacedText replacedText;
577 if (AXObjectCache::accessibilityEnabled() && editingAction == EditActionPaste)
578 replacedText = AccessibilityReplacedText(selection);
580 ReplaceSelectionCommand::CommandOptions options = ReplaceSelectionCommand::PreventNesting | ReplaceSelectionCommand::SanitizeFragment;
581 if (selectReplacement)
582 options |= ReplaceSelectionCommand::SelectReplacement;
584 options |= ReplaceSelectionCommand::SmartReplace;
586 options |= ReplaceSelectionCommand::MatchStyle;
587 if (mailBlockquoteHandling == MailBlockquoteHandling::IgnoreBlockquote)
588 options |= ReplaceSelectionCommand::IgnoreMailBlockquote;
590 auto command = ReplaceSelectionCommand::create(document(), &fragment, options, editingAction);
592 revealSelectionAfterEditingOperation();
594 selection = m_frame.selection().selection();
595 if (selection.isInPasswordField())
598 if (AXObjectCache::accessibilityEnabled() && editingAction == EditActionPaste) {
599 String text = AccessibilityObject::stringForVisiblePositionRange(command->visibleSelectionForInsertedText());
600 replacedText.postTextStateChangeNotification(document().existingAXObjectCache(), AXTextEditTypePaste, text, m_frame.selection().selection());
601 command->composition()->setRangeDeletedByUnapply(replacedText.replacedRange());
604 if (!isContinuousSpellCheckingEnabled())
607 Node* nodeToCheck = selection.rootEditableElement();
611 auto rangeToCheck = Range::create(document(), firstPositionInNode(nodeToCheck), lastPositionInNode(nodeToCheck));
612 if (auto request = SpellCheckRequest::create(resolveTextCheckingTypeMask(*nodeToCheck, TextCheckingTypeSpelling | TextCheckingTypeGrammar), TextCheckingProcessBatch, rangeToCheck.copyRef(), rangeToCheck.copyRef()))
613 m_spellChecker->requestCheckingFor(request.releaseNonNull());
616 void Editor::replaceSelectionWithText(const String& text, bool selectReplacement, bool smartReplace, EditAction editingAction)
618 RefPtr<Range> range = selectedRange();
622 replaceSelectionWithFragment(createFragmentFromText(*range, text), selectReplacement, smartReplace, true, editingAction);
625 RefPtr<Range> Editor::selectedRange()
627 return m_frame.selection().toNormalizedRange();
630 bool Editor::shouldDeleteRange(Range* range) const
632 if (!range || range->collapsed())
635 if (!canDeleteRange(range))
638 return client() && client()->shouldDeleteRange(range);
641 bool Editor::tryDHTMLCopy()
643 if (m_frame.selection().selection().isInPasswordField())
646 return !dispatchClipboardEvent(findEventTargetFromSelection(), eventNames().copyEvent);
649 bool Editor::tryDHTMLCut()
651 if (m_frame.selection().selection().isInPasswordField())
654 return !dispatchClipboardEvent(findEventTargetFromSelection(), eventNames().cutEvent);
657 bool Editor::tryDHTMLPaste()
659 return !dispatchClipboardEvent(findEventTargetFromSelection(), eventNames().pasteEvent);
662 bool Editor::shouldInsertText(const String& text, Range* range, EditorInsertAction action) const
664 if (m_frame.mainFrame().loader().shouldSuppressKeyboardInput() && action == EditorInsertAction::Typed)
667 return client() && client()->shouldInsertText(text, range, action);
670 void Editor::respondToChangedContents(const VisibleSelection& endingSelection)
672 if (AXObjectCache::accessibilityEnabled()) {
673 Node* node = endingSelection.start().deprecatedNode();
674 if (AXObjectCache* cache = document().existingAXObjectCache())
675 cache->postNotification(node, AXObjectCache::AXValueChanged, TargetObservableParent);
678 updateMarkersForWordsAffectedByEditing(true);
681 client()->respondToChangedContents();
684 bool Editor::hasBidiSelection() const
686 if (m_frame.selection().isNone())
690 if (m_frame.selection().isRange()) {
691 startNode = m_frame.selection().selection().start().downstream().deprecatedNode();
692 Node* endNode = m_frame.selection().selection().end().upstream().deprecatedNode();
693 if (enclosingBlock(startNode) != enclosingBlock(endNode))
696 startNode = m_frame.selection().selection().visibleStart().deepEquivalent().deprecatedNode();
701 auto renderer = startNode->renderer();
702 while (renderer && !is<RenderBlockFlow>(*renderer))
703 renderer = renderer->parent();
708 if (!renderer->style().isLeftToRightDirection())
711 return downcast<RenderBlockFlow>(*renderer).containsNonZeroBidiLevel();
714 TriState Editor::selectionUnorderedListState() const
716 if (m_frame.selection().isCaret()) {
717 if (enclosingElementWithTag(m_frame.selection().selection().start(), ulTag))
719 } else if (m_frame.selection().isRange()) {
720 auto* startNode = enclosingElementWithTag(m_frame.selection().selection().start(), ulTag);
721 auto* endNode = enclosingElementWithTag(m_frame.selection().selection().end(), ulTag);
722 if (startNode && endNode && startNode == endNode)
726 return FalseTriState;
729 TriState Editor::selectionOrderedListState() const
731 if (m_frame.selection().isCaret()) {
732 if (enclosingElementWithTag(m_frame.selection().selection().start(), olTag))
734 } else if (m_frame.selection().isRange()) {
735 auto* startNode = enclosingElementWithTag(m_frame.selection().selection().start(), olTag);
736 auto* endNode = enclosingElementWithTag(m_frame.selection().selection().end(), olTag);
737 if (startNode && endNode && startNode == endNode)
741 return FalseTriState;
744 RefPtr<Node> Editor::insertOrderedList()
746 if (!canEditRichly())
749 RefPtr<Node> newList = InsertListCommand::insertList(document(), InsertListCommand::OrderedList);
750 revealSelectionAfterEditingOperation();
754 RefPtr<Node> Editor::insertUnorderedList()
756 if (!canEditRichly())
759 RefPtr<Node> newList = InsertListCommand::insertList(document(), InsertListCommand::UnorderedList);
760 revealSelectionAfterEditingOperation();
764 bool Editor::canIncreaseSelectionListLevel()
766 return canEditRichly() && IncreaseSelectionListLevelCommand::canIncreaseSelectionListLevel(&document());
769 bool Editor::canDecreaseSelectionListLevel()
771 return canEditRichly() && DecreaseSelectionListLevelCommand::canDecreaseSelectionListLevel(&document());
774 RefPtr<Node> Editor::increaseSelectionListLevel()
776 if (!canEditRichly() || m_frame.selection().isNone())
779 RefPtr<Node> newList = IncreaseSelectionListLevelCommand::increaseSelectionListLevel(&document());
780 revealSelectionAfterEditingOperation();
784 RefPtr<Node> Editor::increaseSelectionListLevelOrdered()
786 if (!canEditRichly() || m_frame.selection().isNone())
789 RefPtr<Node> newList = IncreaseSelectionListLevelCommand::increaseSelectionListLevelOrdered(&document());
790 revealSelectionAfterEditingOperation();
794 RefPtr<Node> Editor::increaseSelectionListLevelUnordered()
796 if (!canEditRichly() || m_frame.selection().isNone())
799 RefPtr<Node> newList = IncreaseSelectionListLevelCommand::increaseSelectionListLevelUnordered(&document());
800 revealSelectionAfterEditingOperation();
804 void Editor::decreaseSelectionListLevel()
806 if (!canEditRichly() || m_frame.selection().isNone())
809 DecreaseSelectionListLevelCommand::decreaseSelectionListLevel(&document());
810 revealSelectionAfterEditingOperation();
813 void Editor::removeFormattingAndStyle()
815 RemoveFormatCommand::create(document())->apply();
818 void Editor::clearLastEditCommand()
820 m_lastEditCommand = nullptr;
823 Element* Editor::findEventTargetFrom(const VisibleSelection& selection) const
825 Element* target = selection.start().element();
827 target = document().bodyOrFrameset();
834 Element* Editor::findEventTargetFromSelection() const
836 return findEventTargetFrom(m_frame.selection().selection());
839 void Editor::applyStyle(StyleProperties* style, EditAction editingAction)
842 applyStyle(EditingStyle::create(style), editingAction);
845 void Editor::applyStyle(RefPtr<EditingStyle>&& style, EditAction editingAction)
850 auto selectionType = m_frame.selection().selection().selectionType();
851 if (selectionType == VisibleSelection::NoSelection)
854 String inputTypeName = inputTypeNameForEditingAction(editingAction);
855 String inputEventData = inputEventDataForEditingStyleAndAction(*style, editingAction);
856 RefPtr<Element> element = m_frame.selection().selection().rootEditableElement();
857 if (element && !dispatchBeforeInputEvent(*element, inputTypeName, inputEventData))
860 switch (selectionType) {
861 case VisibleSelection::CaretSelection:
862 computeAndSetTypingStyle(*style, editingAction);
864 case VisibleSelection::RangeSelection:
865 ApplyStyleCommand::create(document(), style.get(), editingAction)->apply();
871 client()->didApplyStyle();
873 dispatchInputEvent(*element, inputTypeName, inputEventData);
876 bool Editor::shouldApplyStyle(StyleProperties* style, Range* range)
878 return client()->shouldApplyStyle(style, range);
881 void Editor::applyParagraphStyle(StyleProperties* style, EditAction editingAction)
886 auto selectionType = m_frame.selection().selection().selectionType();
887 if (selectionType == VisibleSelection::NoSelection)
890 String inputTypeName = inputTypeNameForEditingAction(editingAction);
891 String inputEventData = inputEventDataForEditingStyleAndAction(style, editingAction);
892 RefPtr<Element> element = m_frame.selection().selection().rootEditableElement();
893 if (element && !dispatchBeforeInputEvent(*element, inputTypeName, inputEventData))
896 ApplyStyleCommand::create(document(), EditingStyle::create(style).ptr(), editingAction, ApplyStyleCommand::ForceBlockProperties)->apply();
897 client()->didApplyStyle();
899 dispatchInputEvent(*element, inputTypeName, inputEventData);
902 void Editor::applyStyleToSelection(StyleProperties* style, EditAction editingAction)
904 if (!style || style->isEmpty() || !canEditRichly())
907 if (!client() || !client()->shouldApplyStyle(style, m_frame.selection().toNormalizedRange().get()))
909 applyStyle(style, editingAction);
912 void Editor::applyStyleToSelection(Ref<EditingStyle>&& style, EditAction editingAction)
914 if (style->isEmpty() || !canEditRichly())
917 // FIXME: This is wrong for text decorations since m_mutableStyle is empty.
918 if (!client() || !client()->shouldApplyStyle(style->styleWithResolvedTextDecorations().ptr(), m_frame.selection().toNormalizedRange().get()))
921 applyStyle(WTFMove(style), editingAction);
924 void Editor::applyParagraphStyleToSelection(StyleProperties* style, EditAction editingAction)
926 if (!style || style->isEmpty() || !canEditRichly())
929 if (client() && client()->shouldApplyStyle(style, m_frame.selection().toNormalizedRange().get()))
930 applyParagraphStyle(style, editingAction);
933 bool Editor::selectionStartHasStyle(CSSPropertyID propertyID, const String& value) const
935 return EditingStyle::create(propertyID, value)->triStateOfStyle(
936 EditingStyle::styleAtSelectionStart(m_frame.selection().selection(), propertyID == CSSPropertyBackgroundColor).get());
939 TriState Editor::selectionHasStyle(CSSPropertyID propertyID, const String& value) const
941 return EditingStyle::create(propertyID, value)->triStateOfStyle(m_frame.selection().selection());
944 String Editor::selectionStartCSSPropertyValue(CSSPropertyID propertyID)
946 RefPtr<EditingStyle> selectionStyle = EditingStyle::styleAtSelectionStart(m_frame.selection().selection(),
947 propertyID == CSSPropertyBackgroundColor);
948 if (!selectionStyle || !selectionStyle->style())
951 if (propertyID == CSSPropertyFontSize)
952 return String::number(selectionStyle->legacyFontSize(&document()));
953 return selectionStyle->style()->getPropertyValue(propertyID);
956 void Editor::indent()
958 IndentOutdentCommand::create(document(), IndentOutdentCommand::Indent)->apply();
961 void Editor::outdent()
963 IndentOutdentCommand::create(document(), IndentOutdentCommand::Outdent)->apply();
966 static void notifyTextFromControls(Element* startRoot, Element* endRoot)
968 HTMLTextFormControlElement* startingTextControl = enclosingTextFormControl(firstPositionInOrBeforeNode(startRoot));
969 HTMLTextFormControlElement* endingTextControl = enclosingTextFormControl(firstPositionInOrBeforeNode(endRoot));
970 if (startingTextControl)
971 startingTextControl->didEditInnerTextValue();
972 if (endingTextControl && startingTextControl != endingTextControl)
973 endingTextControl->didEditInnerTextValue();
976 static bool dispatchBeforeInputEvents(RefPtr<Element> startRoot, RefPtr<Element> endRoot, const AtomicString& inputTypeName, const String& data = { }, RefPtr<DataTransfer>&& dataTransfer = nullptr, const Vector<RefPtr<StaticRange>>& targetRanges = { }, bool cancelable = true)
978 bool continueWithDefaultBehavior = true;
980 continueWithDefaultBehavior &= dispatchBeforeInputEvent(*startRoot, inputTypeName, data, WTFMove(dataTransfer), targetRanges, cancelable);
981 if (endRoot && endRoot != startRoot)
982 continueWithDefaultBehavior &= dispatchBeforeInputEvent(*endRoot, inputTypeName, data, WTFMove(dataTransfer), targetRanges, cancelable);
983 return continueWithDefaultBehavior;
986 static void dispatchInputEvents(RefPtr<Element> startRoot, RefPtr<Element> endRoot, const AtomicString& inputTypeName, const String& data = { }, RefPtr<DataTransfer>&& dataTransfer = nullptr, const Vector<RefPtr<StaticRange>>& targetRanges = { })
989 dispatchInputEvent(*startRoot, inputTypeName, data, WTFMove(dataTransfer), targetRanges);
990 if (endRoot && endRoot != startRoot)
991 dispatchInputEvent(*endRoot, inputTypeName, data, WTFMove(dataTransfer), targetRanges);
994 bool Editor::willApplyEditing(CompositeEditCommand& command, Vector<RefPtr<StaticRange>>&& targetRanges) const
996 if (!command.shouldDispatchInputEvents())
999 auto* composition = command.composition();
1003 return dispatchBeforeInputEvents(composition->startingRootEditableElement(), composition->endingRootEditableElement(), command.inputEventTypeName(), command.inputEventData(), command.inputEventDataTransfer(), targetRanges, command.isBeforeInputEventCancelable());
1006 void Editor::appliedEditing(CompositeEditCommand& command)
1008 LOG(Editing, "Editor %p appliedEditing", this);
1010 document().updateLayout();
1012 ASSERT(command.composition());
1013 auto& composition = *command.composition();
1014 VisibleSelection newSelection(command.endingSelection());
1016 notifyTextFromControls(composition.startingRootEditableElement(), composition.endingRootEditableElement());
1018 if (command.isTopLevelCommand()) {
1019 // Don't clear the typing style with this selection change. We do those things elsewhere if necessary.
1020 FrameSelection::SetSelectionOptions options = command.isDictationCommand() ? FrameSelection::DictationTriggered : 0;
1022 changeSelectionAfterCommand(newSelection, options);
1025 if (command.shouldDispatchInputEvents())
1026 dispatchInputEvents(composition.startingRootEditableElement(), composition.endingRootEditableElement(), command.inputEventTypeName(), command.inputEventData(), command.inputEventDataTransfer());
1028 if (command.isTopLevelCommand()) {
1029 updateEditorUINowIfScheduled();
1031 m_alternativeTextController->respondToAppliedEditing(&command);
1033 if (!command.preservesTypingStyle())
1034 m_frame.selection().clearTypingStyle();
1036 // Command will be equal to last edit command only in the case of typing
1037 if (m_lastEditCommand.get() == &command)
1038 ASSERT(command.isTypingCommand());
1040 // Only register a new undo command if the command passed in is
1041 // different from the last command
1042 m_lastEditCommand = &command;
1044 client()->registerUndoStep(m_lastEditCommand->ensureComposition());
1046 respondToChangedContents(newSelection);
1050 bool Editor::willUnapplyEditing(const EditCommandComposition& composition) const
1052 return dispatchBeforeInputEvents(composition.startingRootEditableElement(), composition.endingRootEditableElement(), "historyUndo");
1055 void Editor::unappliedEditing(EditCommandComposition& composition)
1057 document().updateLayout();
1059 notifyTextFromControls(composition.startingRootEditableElement(), composition.endingRootEditableElement());
1061 VisibleSelection newSelection(composition.startingSelection());
1062 changeSelectionAfterCommand(newSelection, FrameSelection::defaultSetSelectionOptions());
1063 dispatchInputEvents(composition.startingRootEditableElement(), composition.endingRootEditableElement(), "historyUndo");
1065 updateEditorUINowIfScheduled();
1067 m_alternativeTextController->respondToUnappliedEditing(&composition);
1069 m_lastEditCommand = nullptr;
1070 if (auto* client = this->client())
1071 client->registerRedoStep(composition);
1072 respondToChangedContents(newSelection);
1075 bool Editor::willReapplyEditing(const EditCommandComposition& composition) const
1077 return dispatchBeforeInputEvents(composition.startingRootEditableElement(), composition.endingRootEditableElement(), "historyRedo");
1080 void Editor::reappliedEditing(EditCommandComposition& composition)
1082 document().updateLayout();
1084 notifyTextFromControls(composition.startingRootEditableElement(), composition.endingRootEditableElement());
1086 VisibleSelection newSelection(composition.endingSelection());
1087 changeSelectionAfterCommand(newSelection, FrameSelection::defaultSetSelectionOptions());
1088 dispatchInputEvents(composition.startingRootEditableElement(), composition.endingRootEditableElement(), "historyRedo");
1090 updateEditorUINowIfScheduled();
1092 m_lastEditCommand = nullptr;
1093 if (auto* client = this->client())
1094 client->registerUndoStep(composition);
1095 respondToChangedContents(newSelection);
1098 Editor::Editor(Frame& frame)
1100 , m_killRing(std::make_unique<PAL::KillRing>())
1101 , m_spellChecker(std::make_unique<SpellChecker>(frame))
1102 , m_alternativeTextController(std::make_unique<AlternativeTextController>(frame))
1103 , m_editorUIUpdateTimer(*this, &Editor::editorUIUpdateTimerFired)
1104 #if ENABLE(TELEPHONE_NUMBER_DETECTION) && !PLATFORM(IOS)
1105 , m_telephoneNumberDetectionUpdateTimer(*this, &Editor::scanSelectionForTelephoneNumbers)
1114 void Editor::clear()
1116 if (m_compositionNode) {
1117 m_compositionNode = nullptr;
1118 if (EditorClient* client = this->client())
1119 client->discardedComposition(&m_frame);
1121 m_customCompositionUnderlines.clear();
1122 m_shouldStyleWithCSS = false;
1123 m_defaultParagraphSeparator = EditorParagraphSeparatorIsDiv;
1126 bool Editor::insertText(const String& text, Event* triggeringEvent, TextEventInputType inputType)
1128 return m_frame.eventHandler().handleTextInputEvent(text, triggeringEvent, inputType);
1131 bool Editor::insertTextForConfirmedComposition(const String& text)
1133 return m_frame.eventHandler().handleTextInputEvent(text, 0, TextEventInputComposition);
1136 bool Editor::insertDictatedText(const String& text, const Vector<DictationAlternative>& dictationAlternatives, Event* triggeringEvent)
1138 return m_alternativeTextController->insertDictatedText(text, dictationAlternatives, triggeringEvent);
1141 bool Editor::insertTextWithoutSendingTextEvent(const String& text, bool selectInsertedText, TextEvent* triggeringEvent)
1146 VisibleSelection selection = selectionForCommand(triggeringEvent);
1147 if (!selection.isContentEditable())
1149 RefPtr<Range> range = selection.toNormalizedRange();
1151 if (!shouldInsertText(text, range.get(), EditorInsertAction::Typed))
1154 updateMarkersForWordsAffectedByEditing(isSpaceOrNewline(text[0]));
1156 bool shouldConsiderApplyingAutocorrection = false;
1157 if (text == " " || text == "\t")
1158 shouldConsiderApplyingAutocorrection = true;
1160 if (text.length() == 1 && u_ispunct(text[0]) && !isAmbiguousBoundaryCharacter(text[0]))
1161 shouldConsiderApplyingAutocorrection = true;
1163 bool autocorrectionWasApplied = shouldConsiderApplyingAutocorrection && m_alternativeTextController->applyAutocorrectionBeforeTypingIfAppropriate();
1165 // Get the selection to use for the event that triggered this insertText.
1166 // If the event handler changed the selection, we may want to use a different selection
1167 // that is contained in the event target.
1168 selection = selectionForCommand(triggeringEvent);
1169 if (selection.isContentEditable()) {
1170 if (Node* selectionStart = selection.start().deprecatedNode()) {
1171 Ref<Document> document(selectionStart->document());
1174 if (triggeringEvent && triggeringEvent->isDictation())
1175 DictationCommand::insertText(document, text, triggeringEvent->dictationAlternatives(), selection);
1177 TypingCommand::Options options = 0;
1178 if (selectInsertedText)
1179 options |= TypingCommand::SelectInsertedText;
1180 if (autocorrectionWasApplied)
1181 options |= TypingCommand::RetainAutocorrectionIndicator;
1182 if (triggeringEvent && triggeringEvent->isAutocompletion())
1183 options |= TypingCommand::IsAutocompletion;
1184 TypingCommand::insertText(document, text, selection, options, triggeringEvent && triggeringEvent->isComposition() ? TypingCommand::TextCompositionFinal : TypingCommand::TextCompositionNone);
1187 // Reveal the current selection
1188 if (Frame* editedFrame = document->frame())
1189 if (Page* page = editedFrame->page()) {
1191 SelectionRevealMode revealMode = SelectionRevealMode::RevealUpToMainFrame;
1193 SelectionRevealMode revealMode = SelectionRevealMode::Reveal;
1195 page->focusController().focusedOrMainFrame().selection().revealSelection(revealMode, ScrollAlignment::alignCenterIfNeeded);
1203 bool Editor::insertLineBreak()
1208 if (!shouldInsertText("\n", m_frame.selection().toNormalizedRange().get(), EditorInsertAction::Typed))
1211 VisiblePosition caret = m_frame.selection().selection().visibleStart();
1212 bool alignToEdge = isEndOfEditableOrNonEditableContent(caret);
1213 bool autocorrectionIsApplied = m_alternativeTextController->applyAutocorrectionBeforeTypingIfAppropriate();
1214 TypingCommand::insertLineBreak(document(), autocorrectionIsApplied ? TypingCommand::RetainAutocorrectionIndicator : 0);
1215 revealSelectionAfterEditingOperation(alignToEdge ? ScrollAlignment::alignToEdgeIfNeeded : ScrollAlignment::alignCenterIfNeeded);
1220 bool Editor::insertParagraphSeparator()
1225 if (!canEditRichly())
1226 return insertLineBreak();
1228 if (!shouldInsertText("\n", m_frame.selection().toNormalizedRange().get(), EditorInsertAction::Typed))
1231 VisiblePosition caret = m_frame.selection().selection().visibleStart();
1232 bool alignToEdge = isEndOfEditableOrNonEditableContent(caret);
1233 bool autocorrectionIsApplied = m_alternativeTextController->applyAutocorrectionBeforeTypingIfAppropriate();
1234 TypingCommand::insertParagraphSeparator(document(), autocorrectionIsApplied ? TypingCommand::RetainAutocorrectionIndicator : 0);
1235 revealSelectionAfterEditingOperation(alignToEdge ? ScrollAlignment::alignToEdgeIfNeeded : ScrollAlignment::alignCenterIfNeeded);
1240 bool Editor::insertParagraphSeparatorInQuotedContent()
1242 // FIXME: Why is this missing calls to canEdit, canEditRichly, etc.?
1243 TypingCommand::insertParagraphSeparatorInQuotedContent(document());
1244 revealSelectionAfterEditingOperation();
1251 return; // DHTML did the whole operation
1257 performCutOrCopy(CutAction);
1263 return; // DHTML did the whole operation
1269 performCutOrCopy(CopyAction);
1272 void Editor::postTextStateChangeNotificationForCut(const String& text, const VisibleSelection& selection)
1274 if (!AXObjectCache::accessibilityEnabled())
1278 AXObjectCache* cache = document().existingAXObjectCache();
1281 cache->postTextStateChangeNotification(selection.start().anchorNode(), AXTextEditTypeCut, text, selection.start());
1284 void Editor::performCutOrCopy(EditorActionSpecifier action)
1286 RefPtr<Range> selection = selectedRange();
1287 willWriteSelectionToPasteboard(selection.get());
1288 if (action == CutAction) {
1289 if (!shouldDeleteRange(selection.get()))
1292 updateMarkersForWordsAffectedByEditing(true);
1295 if (enclosingTextFormControl(m_frame.selection().selection().start()))
1296 Pasteboard::createForCopyAndPaste()->writePlainText(selectedTextForDataTransfer(), canSmartCopyOrDelete() ? Pasteboard::CanSmartReplace : Pasteboard::CannotSmartReplace);
1298 HTMLImageElement* imageElement = nullptr;
1299 if (action == CopyAction)
1300 imageElement = imageElementFromImageDocument(document());
1303 #if PLATFORM(COCOA) || PLATFORM(GTK) || PLATFORM(WPE)
1304 writeImageToPasteboard(*Pasteboard::createForCopyAndPaste(), *imageElement, document().url(), document().title());
1306 Pasteboard::createForCopyAndPaste()->writeImage(*imageElement, document().url(), document().title());
1309 #if PLATFORM(COCOA) || PLATFORM(GTK) || PLATFORM(WPE)
1310 writeSelectionToPasteboard(*Pasteboard::createForCopyAndPaste());
1312 // FIXME: Convert all other platforms to match Mac and delete this.
1313 Pasteboard::createForCopyAndPaste()->writeSelection(*selection, canSmartCopyOrDelete(), m_frame, IncludeImageAltTextForDataTransfer);
1318 didWriteSelectionToPasteboard();
1319 if (action == CutAction) {
1321 if (AXObjectCache::accessibilityEnabled())
1322 text = AccessibilityObject::stringForVisiblePositionRange(m_frame.selection().selection());
1323 deleteSelectionWithSmartDelete(canSmartCopyOrDelete(), EditActionCut);
1324 if (AXObjectCache::accessibilityEnabled())
1325 postTextStateChangeNotificationForCut(text, m_frame.selection().selection());
1329 void Editor::paste()
1331 paste(*Pasteboard::createForCopyAndPaste());
1334 void Editor::paste(Pasteboard& pasteboard)
1336 if (tryDHTMLPaste())
1337 return; // DHTML did the whole operation
1340 updateMarkersForWordsAffectedByEditing(false);
1341 ResourceCacheValidationSuppressor validationSuppressor(document().cachedResourceLoader());
1342 if (m_frame.selection().selection().isContentRichlyEditable())
1343 pasteWithPasteboard(&pasteboard, true);
1345 pasteAsPlainTextWithPasteboard(pasteboard);
1348 void Editor::pasteAsPlainText()
1350 if (tryDHTMLPaste())
1354 updateMarkersForWordsAffectedByEditing(false);
1355 pasteAsPlainTextWithPasteboard(*Pasteboard::createForCopyAndPaste());
1358 void Editor::performDelete()
1365 addRangeToKillRing(*selectedRange().get(), KillRingInsertionMode::AppendText);
1366 deleteSelectionWithSmartDelete(canSmartCopyOrDelete());
1368 // clear the "start new kill ring sequence" setting, because it was set to true
1369 // when the selection was updated by deleting the range
1370 setStartNewKillRingSequence(false);
1373 void Editor::simplifyMarkup(Node* startNode, Node* endNode)
1378 if (&startNode->document() != &endNode->document())
1380 // check if start node is before endNode
1381 Node* node = startNode;
1382 while (node && node != endNode)
1383 node = NodeTraversal::next(*node);
1388 SimplifyMarkupCommand::create(document(), startNode, endNode ? NodeTraversal::next(*endNode) : nullptr)->apply();
1391 void Editor::copyURL(const URL& url, const String& title)
1393 copyURL(url, title, *Pasteboard::createForCopyAndPaste());
1396 void Editor::copyURL(const URL& url, const String& title, Pasteboard& pasteboard)
1398 PasteboardURL pasteboardURL;
1399 pasteboardURL.url = url;
1400 pasteboardURL.title = title;
1403 pasteboardURL.userVisibleForm = userVisibleString(url);
1406 pasteboard.write(pasteboardURL);
1409 PasteboardWriterData::URL Editor::pasteboardWriterURL(const URL& url, const String& title)
1411 PasteboardWriterData::URL result;
1414 result.title = title;
1416 result.userVisibleForm = userVisibleString(url);
1424 void Editor::copyImage(const HitTestResult& result)
1426 Element* element = result.innerNonSharedElement();
1430 URL url = result.absoluteLinkURL();
1432 url = result.absoluteImageURL();
1434 #if PLATFORM(COCOA) || PLATFORM(GTK) || PLATFORM(WPE)
1435 writeImageToPasteboard(*Pasteboard::createForCopyAndPaste(), *element, url, result.altDisplayString());
1437 Pasteboard::createForCopyAndPaste()->writeImage(*element, url, result.altDisplayString());
1443 bool Editor::isContinuousSpellCheckingEnabled() const
1445 return client() && client()->isContinuousSpellCheckingEnabled();
1448 void Editor::toggleContinuousSpellChecking()
1451 client()->toggleContinuousSpellChecking();
1454 bool Editor::isGrammarCheckingEnabled()
1456 return client() && client()->isGrammarCheckingEnabled();
1459 void Editor::toggleGrammarChecking()
1462 client()->toggleGrammarChecking();
1465 int Editor::spellCheckerDocumentTag()
1467 return client() ? client()->spellCheckerDocumentTag() : 0;
1472 void Editor::uppercaseWord()
1475 client()->uppercaseWord();
1478 void Editor::lowercaseWord()
1481 client()->lowercaseWord();
1484 void Editor::capitalizeWord()
1487 client()->capitalizeWord();
1492 #if USE(AUTOMATIC_TEXT_REPLACEMENT)
1494 void Editor::showSubstitutionsPanel()
1497 LOG_ERROR("No NSSpellChecker");
1501 if (client()->substitutionsPanelIsShowing()) {
1502 client()->showSubstitutionsPanel(false);
1505 client()->showSubstitutionsPanel(true);
1508 bool Editor::substitutionsPanelIsShowing()
1512 return client()->substitutionsPanelIsShowing();
1515 void Editor::toggleSmartInsertDelete()
1518 client()->toggleSmartInsertDelete();
1521 bool Editor::isAutomaticQuoteSubstitutionEnabled()
1523 return client() && client()->isAutomaticQuoteSubstitutionEnabled();
1526 void Editor::toggleAutomaticQuoteSubstitution()
1529 client()->toggleAutomaticQuoteSubstitution();
1532 bool Editor::isAutomaticLinkDetectionEnabled()
1534 return client() && client()->isAutomaticLinkDetectionEnabled();
1537 void Editor::toggleAutomaticLinkDetection()
1540 client()->toggleAutomaticLinkDetection();
1543 bool Editor::isAutomaticDashSubstitutionEnabled()
1545 return client() && client()->isAutomaticDashSubstitutionEnabled();
1548 void Editor::toggleAutomaticDashSubstitution()
1551 client()->toggleAutomaticDashSubstitution();
1554 bool Editor::isAutomaticTextReplacementEnabled()
1556 return client() && client()->isAutomaticTextReplacementEnabled();
1559 void Editor::toggleAutomaticTextReplacement()
1562 client()->toggleAutomaticTextReplacement();
1565 bool Editor::isAutomaticSpellingCorrectionEnabled()
1567 return m_alternativeTextController->isAutomaticSpellingCorrectionEnabled();
1570 void Editor::toggleAutomaticSpellingCorrection()
1573 client()->toggleAutomaticSpellingCorrection();
1578 bool Editor::shouldEndEditing(Range* range)
1580 return client() && client()->shouldEndEditing(range);
1583 bool Editor::shouldBeginEditing(Range* range)
1585 return client() && client()->shouldBeginEditing(range);
1588 void Editor::clearUndoRedoOperations()
1591 client()->clearUndoRedoOperations();
1594 bool Editor::canUndo()
1596 return client() && client()->canUndo();
1605 bool Editor::canRedo()
1607 return client() && client()->canRedo();
1616 void Editor::didBeginEditing()
1619 client()->didBeginEditing();
1622 void Editor::didEndEditing()
1625 client()->didEndEditing();
1628 void Editor::willWriteSelectionToPasteboard(Range* range)
1631 client()->willWriteSelectionToPasteboard(range);
1634 void Editor::didWriteSelectionToPasteboard()
1637 client()->didWriteSelectionToPasteboard();
1640 void Editor::toggleBold()
1642 command("ToggleBold").execute();
1645 void Editor::toggleUnderline()
1647 command("ToggleUnderline").execute();
1650 void Editor::setBaseWritingDirection(WritingDirection direction)
1653 if (inSameParagraph(m_frame.selection().selection().visibleStart(), m_frame.selection().selection().visibleEnd()) &&
1654 baseWritingDirectionForSelectionStart() == direction)
1658 Element* focusedElement = document().focusedElement();
1659 if (is<HTMLTextFormControlElement>(focusedElement)) {
1660 if (direction == NaturalWritingDirection)
1663 auto& focusedFormElement = downcast<HTMLTextFormControlElement>(*focusedElement);
1664 auto directionValue = direction == LeftToRightWritingDirection ? "ltr" : "rtl";
1665 auto writingDirectionInputTypeName = inputTypeNameForEditingAction(EditActionSetWritingDirection);
1666 if (!dispatchBeforeInputEvent(focusedFormElement, writingDirectionInputTypeName, directionValue))
1669 focusedFormElement.setAttributeWithoutSynchronization(dirAttr, directionValue);
1670 dispatchInputEvent(focusedFormElement, writingDirectionInputTypeName, directionValue);
1671 document().updateStyleIfNeeded();
1675 RefPtr<MutableStyleProperties> style = MutableStyleProperties::create();
1676 style->setProperty(CSSPropertyDirection, direction == LeftToRightWritingDirection ? "ltr" : direction == RightToLeftWritingDirection ? "rtl" : "inherit", false);
1677 applyParagraphStyleToSelection(style.get(), EditActionSetWritingDirection);
1680 WritingDirection Editor::baseWritingDirectionForSelectionStart() const
1682 WritingDirection result = LeftToRightWritingDirection;
1684 Position pos = m_frame.selection().selection().visibleStart().deepEquivalent();
1685 Node* node = pos.deprecatedNode();
1689 auto renderer = node->renderer();
1693 if (!renderer->isRenderBlockFlow()) {
1694 renderer = renderer->containingBlock();
1699 switch (renderer->style().direction()) {
1701 return LeftToRightWritingDirection;
1703 return RightToLeftWritingDirection;
1709 void Editor::selectComposition()
1711 RefPtr<Range> range = compositionRange();
1715 // The composition can start inside a composed character sequence, so we have to override checks.
1716 // See <http://bugs.webkit.org/show_bug.cgi?id=15781>
1717 VisibleSelection selection;
1718 selection.setWithoutValidation(range->startPosition(), range->endPosition());
1719 m_frame.selection().setSelection(selection, 0);
1722 void Editor::confirmComposition()
1724 if (!m_compositionNode)
1726 setComposition(m_compositionNode->data().substring(m_compositionStart, m_compositionEnd - m_compositionStart), ConfirmComposition);
1729 void Editor::cancelComposition()
1731 if (!m_compositionNode)
1733 setComposition(emptyString(), CancelComposition);
1736 bool Editor::cancelCompositionIfSelectionIsInvalid()
1740 if (!hasComposition() || ignoreSelectionChanges() || getCompositionSelection(start, end))
1743 cancelComposition();
1747 void Editor::confirmComposition(const String& text)
1749 setComposition(text, ConfirmComposition);
1752 void Editor::setComposition(const String& text, SetCompositionMode mode)
1754 ASSERT(mode == ConfirmComposition || mode == CancelComposition);
1755 UserTypingGestureIndicator typingGestureIndicator(m_frame);
1757 setIgnoreSelectionChanges(true);
1759 if (mode == CancelComposition)
1760 ASSERT(text == emptyString());
1762 selectComposition();
1764 m_compositionNode = nullptr;
1765 m_customCompositionUnderlines.clear();
1767 if (m_frame.selection().isNone()) {
1768 setIgnoreSelectionChanges(false);
1772 // Always delete the current composition before inserting the finalized composition text if we're confirming our composition.
1773 // Our default behavior (if the beforeinput event is not prevented) is to insert the finalized composition text back in.
1774 // We pass TypingCommand::TextCompositionPending here to indicate that we are deleting the pending composition.
1775 if (mode != CancelComposition)
1776 TypingCommand::deleteSelection(document(), 0, TypingCommand::TextCompositionPending);
1778 insertTextForConfirmedComposition(text);
1780 if (auto* target = document().focusedElement())
1781 target->dispatchEvent(CompositionEvent::create(eventNames().compositionendEvent, document().domWindow(), text));
1783 if (mode == CancelComposition) {
1784 // An open typing command that disagrees about current selection would cause issues with typing later on.
1785 TypingCommand::closeTyping(&m_frame);
1788 setIgnoreSelectionChanges(false);
1791 void Editor::setComposition(const String& text, const Vector<CompositionUnderline>& underlines, unsigned selectionStart, unsigned selectionEnd)
1793 Ref<Frame> protection(m_frame);
1795 UserTypingGestureIndicator typingGestureIndicator(m_frame);
1797 setIgnoreSelectionChanges(true);
1799 // Updates styles before setting selection for composition to prevent
1800 // inserting the previous composition text into text nodes oddly.
1801 // See https://bugs.webkit.org/show_bug.cgi?id=46868
1802 document().updateStyleIfNeeded();
1804 selectComposition();
1806 if (m_frame.selection().isNone()) {
1807 setIgnoreSelectionChanges(false);
1811 String originalText = selectedText();
1812 bool isStartingToRecomposeExistingRange = !text.isEmpty() && selectionStart < selectionEnd && !hasComposition();
1813 if (isStartingToRecomposeExistingRange) {
1814 // We pass TypingCommand::TextCompositionFinal here to indicate that we are removing composition text that has been finalized.
1815 TypingCommand::deleteSelection(document(), 0, TypingCommand::TextCompositionFinal);
1816 const VisibleSelection& currentSelection = m_frame.selection().selection();
1817 if (currentSelection.isRange()) {
1818 // If deletion was prevented, then we need to collapse the selection to the end so that the original text will not be recomposed.
1819 m_frame.selection().setSelection({ currentSelection.end(), currentSelection.end() });
1824 client()->startDelayingAndCoalescingContentChangeNotifications();
1827 Element* target = document().focusedElement();
1829 // Dispatch an appropriate composition event to the focused node.
1830 // We check the composition status and choose an appropriate composition event since this
1831 // function is used for three purposes:
1832 // 1. Starting a new composition.
1833 // Send a compositionstart and a compositionupdate event when this function creates
1834 // a new composition node, i.e.
1835 // m_compositionNode == 0 && !text.isEmpty().
1836 // Sending a compositionupdate event at this time ensures that at least one
1837 // compositionupdate event is dispatched.
1838 // 2. Updating the existing composition node.
1839 // Send a compositionupdate event when this function updates the existing composition
1840 // node, i.e. m_compositionNode != 0 && !text.isEmpty().
1841 // 3. Canceling the ongoing composition.
1842 // Send a compositionend event when function deletes the existing composition node, i.e.
1843 // m_compositionNode != 0 && test.isEmpty().
1844 RefPtr<CompositionEvent> event;
1845 if (!m_compositionNode) {
1846 // We should send a compositionstart event only when the given text is not empty because this
1847 // function doesn't create a composition node when the text is empty.
1848 if (!text.isEmpty()) {
1849 target->dispatchEvent(CompositionEvent::create(eventNames().compositionstartEvent, document().domWindow(), originalText));
1850 event = CompositionEvent::create(eventNames().compositionupdateEvent, document().domWindow(), text);
1852 } else if (!text.isEmpty())
1853 event = CompositionEvent::create(eventNames().compositionupdateEvent, document().domWindow(), text);
1856 target->dispatchEvent(*event);
1859 // If text is empty, then delete the old composition here. If text is non-empty, InsertTextCommand::input
1860 // will delete the old composition with an optimized replace operation.
1861 if (text.isEmpty()) {
1862 TypingCommand::deleteSelection(document(), TypingCommand::PreventSpellChecking, TypingCommand::TextCompositionPending);
1864 target->dispatchEvent(CompositionEvent::create(eventNames().compositionendEvent, document().domWindow(), text));
1867 m_compositionNode = nullptr;
1868 m_customCompositionUnderlines.clear();
1870 if (!text.isEmpty()) {
1871 TypingCommand::insertText(document(), text, TypingCommand::SelectInsertedText | TypingCommand::PreventSpellChecking, TypingCommand::TextCompositionPending);
1873 // Find out what node has the composition now.
1874 Position base = m_frame.selection().selection().base().downstream();
1875 Position extent = m_frame.selection().selection().extent();
1876 Node* baseNode = base.deprecatedNode();
1877 unsigned baseOffset = base.deprecatedEditingOffset();
1878 Node* extentNode = extent.deprecatedNode();
1879 unsigned extentOffset = extent.deprecatedEditingOffset();
1881 if (is<Text>(baseNode) && baseNode == extentNode && baseOffset + text.length() == extentOffset) {
1882 m_compositionNode = downcast<Text>(baseNode);
1883 m_compositionStart = baseOffset;
1884 m_compositionEnd = extentOffset;
1885 m_customCompositionUnderlines = underlines;
1886 for (auto& underline : m_customCompositionUnderlines) {
1887 underline.startOffset += baseOffset;
1888 underline.endOffset += baseOffset;
1890 if (baseNode->renderer())
1891 baseNode->renderer()->repaint();
1893 unsigned start = std::min(baseOffset + selectionStart, extentOffset);
1894 unsigned end = std::min(std::max(start, baseOffset + selectionEnd), extentOffset);
1895 RefPtr<Range> selectedRange = Range::create(baseNode->document(), baseNode, start, baseNode, end);
1896 m_frame.selection().setSelectedRange(selectedRange.get(), DOWNSTREAM, false);
1900 setIgnoreSelectionChanges(false);
1903 client()->stopDelayingAndCoalescingContentChangeNotifications();
1907 void Editor::ignoreSpelling()
1912 RefPtr<Range> selectedRange = m_frame.selection().toNormalizedRange();
1914 document().markers().removeMarkers(selectedRange.get(), DocumentMarker::Spelling);
1916 String text = selectedText();
1917 ASSERT(text.length());
1918 textChecker()->ignoreWordInSpellDocument(text);
1921 void Editor::learnSpelling()
1926 // FIXME: On Mac OS X, when use "learn" button on "Spelling and Grammar" panel, we don't call this function. It should remove misspelling markers around the learned word, see <rdar://problem/5396072>.
1928 RefPtr<Range> selectedRange = m_frame.selection().toNormalizedRange();
1930 document().markers().removeMarkers(selectedRange.get(), DocumentMarker::Spelling);
1932 String text = selectedText();
1933 ASSERT(text.length());
1934 textChecker()->learnWord(text);
1939 void Editor::advanceToNextMisspelling(bool startBeforeSelection)
1941 Ref<Frame> protection(m_frame);
1943 // The basic approach is to search in two phases - from the selection end to the end of the doc, and
1944 // then we wrap and search from the doc start to (approximately) where we started.
1946 // Start at the end of the selection, search to edge of document. Starting at the selection end makes
1947 // repeated "check spelling" commands work.
1948 VisibleSelection selection(m_frame.selection().selection());
1949 Ref<Range> spellingSearchRange = rangeOfContents(document());
1951 bool startedWithSelection = false;
1952 if (selection.start().deprecatedNode()) {
1953 startedWithSelection = true;
1954 if (startBeforeSelection) {
1955 VisiblePosition start(selection.visibleStart());
1956 // We match AppKit's rule: Start 1 character before the selection.
1957 VisiblePosition oneBeforeStart = start.previous();
1958 setStart(spellingSearchRange.ptr(), oneBeforeStart.isNotNull() ? oneBeforeStart : start);
1960 setStart(spellingSearchRange.ptr(), selection.visibleEnd());
1963 Position position = spellingSearchRange->startPosition();
1964 if (!isEditablePosition(position)) {
1965 // This shouldn't happen in very often because the Spelling menu items aren't enabled unless the
1966 // selection is editable.
1967 // This can happen in Mail for a mix of non-editable and editable content (like Stationary),
1968 // when spell checking the whole document before sending the message.
1969 // In that case the document might not be editable, but there are editable pockets that need to be spell checked.
1971 position = VisiblePosition(firstEditablePositionAfterPositionInRoot(position, document().documentElement())).deepEquivalent();
1972 if (position.isNull())
1975 Position rangeCompliantPosition = position.parentAnchoredEquivalent();
1976 if (rangeCompliantPosition.deprecatedNode())
1977 spellingSearchRange->setStart(*rangeCompliantPosition.deprecatedNode(), rangeCompliantPosition.deprecatedEditingOffset());
1978 startedWithSelection = false; // won't need to wrap
1981 // topNode defines the whole range we want to operate on
1982 auto* topNode = highestEditableRoot(position);
1983 // FIXME: lastOffsetForEditing() is wrong here if editingIgnoresContent(highestEditableRoot()) returns true (e.g. a <table>)
1985 spellingSearchRange->setEnd(*topNode, lastOffsetForEditing(*topNode));
1987 // If spellingSearchRange starts in the middle of a word, advance to the next word so we start checking
1988 // at a word boundary. Going back by one char and then forward by a word does the trick.
1989 if (startedWithSelection) {
1990 VisiblePosition oneBeforeStart = startVisiblePosition(spellingSearchRange.ptr(), DOWNSTREAM).previous();
1991 if (oneBeforeStart.isNotNull())
1992 setStart(spellingSearchRange.ptr(), endOfWord(oneBeforeStart));
1993 // else we were already at the start of the editable node
1996 if (spellingSearchRange->collapsed())
1997 return; // nothing to search in
1999 // Get the spell checker if it is available
2003 // We go to the end of our first range instead of the start of it, just to be sure
2004 // we don't get foiled by any word boundary problems at the start. It means we might
2005 // do a tiny bit more searching.
2006 Node& searchEndNodeAfterWrap = spellingSearchRange->endContainer();
2007 int searchEndOffsetAfterWrap = spellingSearchRange->endOffset();
2009 int misspellingOffset = 0;
2010 GrammarDetail grammarDetail;
2011 int grammarPhraseOffset = 0;
2012 RefPtr<Range> grammarSearchRange;
2013 String badGrammarPhrase;
2014 String misspelledWord;
2016 bool isSpelling = true;
2017 int foundOffset = 0;
2019 RefPtr<Range> firstMisspellingRange;
2020 if (unifiedTextCheckerEnabled()) {
2021 grammarSearchRange = spellingSearchRange->cloneRange();
2022 foundItem = TextCheckingHelper(*client(), spellingSearchRange).findFirstMisspellingOrBadGrammar(isGrammarCheckingEnabled(), isSpelling, foundOffset, grammarDetail);
2024 misspelledWord = foundItem;
2025 misspellingOffset = foundOffset;
2027 badGrammarPhrase = foundItem;
2028 grammarPhraseOffset = foundOffset;
2031 misspelledWord = TextCheckingHelper(*client(), spellingSearchRange).findFirstMisspelling(misspellingOffset, false, firstMisspellingRange);
2033 #if USE(GRAMMAR_CHECKING)
2034 grammarSearchRange = spellingSearchRange->cloneRange();
2035 if (!misspelledWord.isEmpty()) {
2036 // Stop looking at start of next misspelled word
2037 CharacterIterator chars(*grammarSearchRange);
2038 chars.advance(misspellingOffset);
2039 grammarSearchRange->setEnd(chars.range()->startContainer(), chars.range()->startOffset());
2042 if (isGrammarCheckingEnabled())
2043 badGrammarPhrase = TextCheckingHelper(*client(), *grammarSearchRange).findFirstBadGrammar(grammarDetail, grammarPhraseOffset, false);
2047 // If we found neither bad grammar nor a misspelled word, wrap and try again (but don't bother if we started at the beginning of the
2048 // block rather than at a selection).
2049 if (startedWithSelection && !misspelledWord && !badGrammarPhrase) {
2051 spellingSearchRange->setStart(*topNode, 0);
2052 // going until the end of the very first chunk we tested is far enough
2053 spellingSearchRange->setEnd(searchEndNodeAfterWrap, searchEndOffsetAfterWrap);
2055 if (unifiedTextCheckerEnabled()) {
2056 grammarSearchRange = spellingSearchRange->cloneRange();
2057 foundItem = TextCheckingHelper(*client(), spellingSearchRange).findFirstMisspellingOrBadGrammar(isGrammarCheckingEnabled(), isSpelling, foundOffset, grammarDetail);
2059 misspelledWord = foundItem;
2060 misspellingOffset = foundOffset;
2062 badGrammarPhrase = foundItem;
2063 grammarPhraseOffset = foundOffset;
2066 misspelledWord = TextCheckingHelper(*client(), spellingSearchRange).findFirstMisspelling(misspellingOffset, false, firstMisspellingRange);
2068 #if USE(GRAMMAR_CHECKING)
2069 grammarSearchRange = spellingSearchRange->cloneRange();
2070 if (!misspelledWord.isEmpty()) {
2071 // Stop looking at start of next misspelled word
2072 CharacterIterator chars(*grammarSearchRange);
2073 chars.advance(misspellingOffset);
2074 grammarSearchRange->setEnd(chars.range()->startContainer(), chars.range()->startOffset());
2077 if (isGrammarCheckingEnabled())
2078 badGrammarPhrase = TextCheckingHelper(*client(), *grammarSearchRange).findFirstBadGrammar(grammarDetail, grammarPhraseOffset, false);
2083 #if !USE(GRAMMAR_CHECKING)
2084 ASSERT(badGrammarPhrase.isEmpty());
2085 UNUSED_PARAM(grammarPhraseOffset);
2087 if (!badGrammarPhrase.isEmpty()) {
2088 // We found bad grammar. Since we only searched for bad grammar up to the first misspelled word, the bad grammar
2089 // takes precedence and we ignore any potential misspelled word. Select the grammar detail, update the spelling
2090 // panel, and store a marker so we draw the green squiggle later.
2092 ASSERT(badGrammarPhrase.length() > 0);
2093 ASSERT(grammarDetail.location != -1 && grammarDetail.length > 0);
2095 // FIXME 4859190: This gets confused with doubled punctuation at the end of a paragraph
2096 RefPtr<Range> badGrammarRange = TextIterator::subrange(*grammarSearchRange, grammarPhraseOffset + grammarDetail.location, grammarDetail.length);
2097 m_frame.selection().setSelection(VisibleSelection(*badGrammarRange, SEL_DEFAULT_AFFINITY));
2098 m_frame.selection().revealSelection();
2100 client()->updateSpellingUIWithGrammarString(badGrammarPhrase, grammarDetail);
2101 document().markers().addMarker(badGrammarRange.get(), DocumentMarker::Grammar, grammarDetail.userDescription);
2104 if (!misspelledWord.isEmpty()) {
2105 // We found a misspelling, but not any earlier bad grammar. Select the misspelling, update the spelling panel, and store
2106 // a marker so we draw the red squiggle later.
2108 auto misspellingRange = TextIterator::subrange(spellingSearchRange, misspellingOffset, misspelledWord.length());
2109 m_frame.selection().setSelection(VisibleSelection(misspellingRange, DOWNSTREAM));
2110 m_frame.selection().revealSelection();
2112 client()->updateSpellingUIWithMisspelledWord(misspelledWord);
2113 document().markers().addMarker(misspellingRange.ptr(), DocumentMarker::Spelling);
2117 #endif // !PLATFORM(IOS)
2119 String Editor::misspelledWordAtCaretOrRange(Node* clickedNode) const
2121 if (!isContinuousSpellCheckingEnabled() || !clickedNode || !isSpellCheckingEnabledFor(clickedNode))
2124 VisibleSelection selection = m_frame.selection().selection();
2125 if (!selection.isContentEditable() || selection.isNone())
2128 VisibleSelection wordSelection(selection.base());
2129 wordSelection.expandUsingGranularity(WordGranularity);
2130 RefPtr<Range> wordRange = wordSelection.toNormalizedRange();
2132 // In compliance with GTK+ applications, additionally allow to provide suggestions when the current
2133 // selection exactly match the word selection.
2134 if (selection.isRange() && !areRangesEqual(wordRange.get(), selection.toNormalizedRange().get()))
2137 String word = wordRange->text();
2138 if (word.isEmpty() || !client())
2141 int wordLength = word.length();
2142 int misspellingLocation = -1;
2143 int misspellingLength = 0;
2144 textChecker()->checkSpellingOfString(word, &misspellingLocation, &misspellingLength);
2146 return misspellingLength == wordLength ? word : String();
2149 String Editor::misspelledSelectionString() const
2151 String selectedString = selectedText();
2152 int length = selectedString.length();
2153 if (!length || !client())
2156 int misspellingLocation = -1;
2157 int misspellingLength = 0;
2158 textChecker()->checkSpellingOfString(selectedString, &misspellingLocation, &misspellingLength);
2160 // The selection only counts as misspelled if the selected text is exactly one misspelled word
2161 if (misspellingLength != length)
2164 // Update the spelling panel to be displaying this error (whether or not the spelling panel is on screen).
2165 // This is necessary to make a subsequent call to [NSSpellChecker ignoreWord:inSpellDocumentWithTag:] work
2166 // correctly; that call behaves differently based on whether the spelling panel is displaying a misspelling
2167 // or a grammar error.
2168 client()->updateSpellingUIWithMisspelledWord(selectedString);
2170 return selectedString;
2173 bool Editor::isSelectionUngrammatical()
2175 #if USE(GRAMMAR_CHECKING)
2176 RefPtr<Range> range = m_frame.selection().toNormalizedRange();
2177 if (!range || !client())
2179 return TextCheckingHelper(*client(), *range).isUngrammatical();
2185 Vector<String> Editor::guessesForMisspelledWord(const String& word) const
2187 ASSERT(word.length());
2189 Vector<String> guesses;
2191 textChecker()->getGuessesForWord(word, String(), m_frame.selection().selection(), guesses);
2195 Vector<String> Editor::guessesForMisspelledOrUngrammatical(bool& misspelled, bool& ungrammatical)
2197 if (unifiedTextCheckerEnabled()) {
2198 RefPtr<Range> range;
2199 VisibleSelection selection = m_frame.selection().selection();
2200 if (selection.isCaret() && behavior().shouldAllowSpellingSuggestionsWithoutSelection()) {
2201 VisibleSelection wordSelection = VisibleSelection(selection.base());
2202 wordSelection.expandUsingGranularity(WordGranularity);
2203 range = wordSelection.toNormalizedRange();
2205 range = selection.toNormalizedRange();
2206 if (!range || !client())
2207 return Vector<String>();
2208 return TextCheckingHelper(*client(), *range).guessesForMisspelledOrUngrammaticalRange(isGrammarCheckingEnabled(), misspelled, ungrammatical);
2211 String misspelledWord = behavior().shouldAllowSpellingSuggestionsWithoutSelection() ? misspelledWordAtCaretOrRange(document().focusedElement()) : misspelledSelectionString();
2212 misspelled = !misspelledWord.isEmpty();
2213 // Only unified text checker supports guesses for ungrammatical phrases.
2214 ungrammatical = false;
2217 return guessesForMisspelledWord(misspelledWord);
2218 return Vector<String>();
2221 void Editor::showSpellingGuessPanel()
2224 LOG_ERROR("No NSSpellChecker");
2228 if (client()->spellingUIIsShowing()) {
2229 client()->showSpellingUI(false);
2234 advanceToNextMisspelling(true);
2236 client()->showSpellingUI(true);
2239 bool Editor::spellingPanelIsShowing()
2243 return client()->spellingUIIsShowing();
2246 void Editor::clearMisspellingsAndBadGrammar(const VisibleSelection &movingSelection)
2248 RefPtr<Range> selectedRange = movingSelection.toNormalizedRange();
2249 if (selectedRange) {
2250 document().markers().removeMarkers(selectedRange.get(), DocumentMarker::Spelling);
2251 document().markers().removeMarkers(selectedRange.get(), DocumentMarker::Grammar);
2255 void Editor::markMisspellingsAndBadGrammar(const VisibleSelection &movingSelection)
2257 markMisspellingsAndBadGrammar(movingSelection, isContinuousSpellCheckingEnabled() && isGrammarCheckingEnabled(), movingSelection);
2260 void Editor::markMisspellingsAfterTypingToWord(const VisiblePosition &wordStart, const VisibleSelection& selectionAfterTyping, bool doReplacement)
2262 Ref<Frame> protection(m_frame);
2265 UNUSED_PARAM(selectionAfterTyping);
2266 UNUSED_PARAM(doReplacement);
2267 TextCheckingTypeMask textCheckingOptions = 0;
2268 if (isContinuousSpellCheckingEnabled())
2269 textCheckingOptions |= TextCheckingTypeSpelling;
2270 if (!(textCheckingOptions & TextCheckingTypeSpelling))
2273 VisibleSelection adjacentWords = VisibleSelection(startOfWord(wordStart, LeftWordIfOnBoundary), endOfWord(wordStart, RightWordIfOnBoundary));
2274 markAllMisspellingsAndBadGrammarInRanges(textCheckingOptions, adjacentWords.toNormalizedRange().get(), adjacentWords.toNormalizedRange().get());
2276 #if !USE(AUTOMATIC_TEXT_REPLACEMENT)
2277 UNUSED_PARAM(doReplacement);
2280 if (unifiedTextCheckerEnabled()) {
2281 m_alternativeTextController->applyPendingCorrection(selectionAfterTyping);
2283 TextCheckingTypeMask textCheckingOptions = 0;
2285 if (isContinuousSpellCheckingEnabled())
2286 textCheckingOptions |= TextCheckingTypeSpelling;
2288 #if USE(AUTOMATIC_TEXT_REPLACEMENT)
2290 && (isAutomaticQuoteSubstitutionEnabled()
2291 || isAutomaticLinkDetectionEnabled()
2292 || isAutomaticDashSubstitutionEnabled()
2293 || isAutomaticTextReplacementEnabled()
2294 || ((textCheckingOptions & TextCheckingTypeSpelling) && isAutomaticSpellingCorrectionEnabled())))
2295 textCheckingOptions |= TextCheckingTypeReplacement;
2297 if (!(textCheckingOptions & (TextCheckingTypeSpelling | TextCheckingTypeReplacement)))
2300 if (isGrammarCheckingEnabled())
2301 textCheckingOptions |= TextCheckingTypeGrammar;
2303 VisibleSelection adjacentWords = VisibleSelection(startOfWord(wordStart, LeftWordIfOnBoundary), endOfWord(wordStart, RightWordIfOnBoundary));
2304 if (textCheckingOptions & TextCheckingTypeGrammar) {
2305 VisibleSelection selectedSentence = VisibleSelection(startOfSentence(wordStart), endOfSentence(wordStart));
2306 markAllMisspellingsAndBadGrammarInRanges(textCheckingOptions, adjacentWords.toNormalizedRange().get(), selectedSentence.toNormalizedRange().get());
2308 markAllMisspellingsAndBadGrammarInRanges(textCheckingOptions, adjacentWords.toNormalizedRange().get(), adjacentWords.toNormalizedRange().get());
2312 if (!isContinuousSpellCheckingEnabled())
2315 // Check spelling of one word
2316 RefPtr<Range> misspellingRange;
2317 markMisspellings(VisibleSelection(startOfWord(wordStart, LeftWordIfOnBoundary), endOfWord(wordStart, RightWordIfOnBoundary)), misspellingRange);
2319 // Autocorrect the misspelled word.
2320 if (!misspellingRange)
2323 // Get the misspelled word.
2324 const String misspelledWord = plainText(misspellingRange.get());
2325 String autocorrectedString = textChecker()->getAutoCorrectSuggestionForMisspelledWord(misspelledWord);
2327 // If autocorrected word is non empty, replace the misspelled word by this word.
2328 if (!autocorrectedString.isEmpty()) {
2329 VisibleSelection newSelection(*misspellingRange, DOWNSTREAM);
2330 if (newSelection != m_frame.selection().selection()) {
2331 if (!m_frame.selection().shouldChangeSelection(newSelection))
2333 m_frame.selection().setSelection(newSelection);
2336 if (!m_frame.editor().shouldInsertText(autocorrectedString, misspellingRange.get(), EditorInsertAction::Typed))
2338 m_frame.editor().replaceSelectionWithText(autocorrectedString, false, false, EditActionInsert);
2340 // Reset the charet one character further.
2341 m_frame.selection().moveTo(m_frame.selection().selection().end());
2342 m_frame.selection().modify(FrameSelection::AlterationMove, DirectionForward, CharacterGranularity);
2345 if (!isGrammarCheckingEnabled())
2348 // Check grammar of entire sentence
2349 markBadGrammar(VisibleSelection(startOfSentence(wordStart), endOfSentence(wordStart)));
2353 void Editor::markMisspellingsOrBadGrammar(const VisibleSelection& selection, bool checkSpelling, RefPtr<Range>& firstMisspellingRange)
2356 // This function is called with a selection already expanded to word boundaries.
2357 // Might be nice to assert that here.
2359 // This function is used only for as-you-type checking, so if that's off we do nothing. Note that
2360 // grammar checking can only be on if spell checking is also on.
2361 if (!isContinuousSpellCheckingEnabled())
2364 RefPtr<Range> searchRange(selection.toNormalizedRange());
2368 // If we're not in an editable node, bail.
2369 Node& editableNode = searchRange->startContainer();
2370 if (!editableNode.hasEditableStyle())
2373 if (!isSpellCheckingEnabledFor(&editableNode))
2376 // Get the spell checker if it is available
2380 TextCheckingHelper checker(*client(), *searchRange);
2382 checker.markAllMisspellings(firstMisspellingRange);
2384 #if USE(GRAMMAR_CHECKING)
2385 if (isGrammarCheckingEnabled())
2386 checker.markAllBadGrammar();
2388 ASSERT_NOT_REACHED();
2392 UNUSED_PARAM(selection);
2393 UNUSED_PARAM(checkSpelling);
2394 UNUSED_PARAM(firstMisspellingRange);
2395 #endif // !PLATFORM(IOS)
2398 bool Editor::isSpellCheckingEnabledFor(Node* node) const
2402 Element* element = is<Element>(*node) ? downcast<Element>(node) : node->parentElement();
2405 if (element->isInUserAgentShadowTree()) {
2406 if (HTMLTextFormControlElement* textControl = enclosingTextFormControl(firstPositionInOrBeforeNode(element)))
2407 return textControl->isSpellCheckingEnabled();
2409 return element->isSpellCheckingEnabled();
2412 bool Editor::isSpellCheckingEnabledInFocusedNode() const
2414 return isSpellCheckingEnabledFor(m_frame.selection().selection().start().deprecatedNode());
2417 void Editor::markMisspellings(const VisibleSelection& selection, RefPtr<Range>& firstMisspellingRange)
2419 markMisspellingsOrBadGrammar(selection, true, firstMisspellingRange);
2422 void Editor::markBadGrammar(const VisibleSelection& selection)
2424 #if USE(GRAMMAR_CHECKING)
2425 RefPtr<Range> firstMisspellingRange;
2426 markMisspellingsOrBadGrammar(selection, false, firstMisspellingRange);
2428 ASSERT_NOT_REACHED();
2432 void Editor::markAllMisspellingsAndBadGrammarInRanges(TextCheckingTypeMask textCheckingOptions, Range* spellingRange, Range* grammarRange)
2434 ASSERT(unifiedTextCheckerEnabled());
2436 // There shouldn't be pending autocorrection at this moment.
2437 ASSERT(!m_alternativeTextController->hasPendingCorrection());
2439 bool shouldMarkGrammar = textCheckingOptions & TextCheckingTypeGrammar;
2440 bool shouldShowCorrectionPanel = textCheckingOptions & TextCheckingTypeShowCorrectionPanel;
2442 // This function is called with selections already expanded to word boundaries.
2443 if (!client() || !spellingRange || (shouldMarkGrammar && !grammarRange))
2446 // If we're not in an editable node, bail.
2447 Node& editableNode = spellingRange->startContainer();
2448 if (!editableNode.hasEditableStyle())
2451 if (!isSpellCheckingEnabledFor(&editableNode))
2454 Range& rangeToCheck = shouldMarkGrammar ? *grammarRange : *spellingRange;
2455 TextCheckingParagraph paragraphToCheck(rangeToCheck);
2456 if (paragraphToCheck.isEmpty())
2458 Ref<Range> paragraphRange = paragraphToCheck.paragraphRange();
2460 bool asynchronous = m_frame.settings().asynchronousSpellCheckingEnabled() && !shouldShowCorrectionPanel;
2462 // In asynchronous mode, we intentionally check paragraph-wide sentence.
2463 const auto resolvedOptions = resolveTextCheckingTypeMask(editableNode, textCheckingOptions);
2464 auto request = SpellCheckRequest::create(resolvedOptions, TextCheckingProcessIncremental, asynchronous ? paragraphRange.get() : rangeToCheck, paragraphRange.copyRef());
2469 m_spellChecker->requestCheckingFor(request.releaseNonNull());
2473 Vector<TextCheckingResult> results;
2474 checkTextOfParagraph(*textChecker(), paragraphToCheck.text(), resolvedOptions, results, m_frame.selection().selection());
2475 markAndReplaceFor(request.releaseNonNull(), results);
2478 static bool isAutomaticTextReplacementType(TextCheckingType type)
2481 case TextCheckingTypeNone:
2482 case TextCheckingTypeSpelling:
2483 case TextCheckingTypeGrammar:
2485 case TextCheckingTypeLink:
2486 case TextCheckingTypeQuote:
2487 case TextCheckingTypeDash:
2488 case TextCheckingTypeReplacement:
2489 case TextCheckingTypeCorrection:
2490 case TextCheckingTypeShowCorrectionPanel:
2493 ASSERT_NOT_REACHED();
2497 static void correctSpellcheckingPreservingTextCheckingParagraph(TextCheckingParagraph& paragraph, Range& rangeToReplace, const String& replacement, int resultLocation, int resultLength)
2499 auto& scope = downcast<ContainerNode>(paragraph.paragraphRange().startContainer().rootNode());
2501 size_t paragraphLocation;
2502 size_t paragraphLength;
2503 TextIterator::getLocationAndLengthFromRange(&scope, ¶graph.paragraphRange(), paragraphLocation, paragraphLength);
2505 SpellingCorrectionCommand::create(rangeToReplace, replacement)->apply();
2507 // TextCheckingParagraph may be orphaned after SpellingCorrectionCommand mutated DOM.
2508 // See <rdar://10305315>, http://webkit.org/b/89526.
2510 RefPtr<Range> newParagraphRange = TextIterator::rangeFromLocationAndLength(&scope, paragraphLocation, paragraphLength + replacement.length() - resultLength);
2512 paragraph = TextCheckingParagraph(TextIterator::subrange(*newParagraphRange, resultLocation, replacement.length()), newParagraphRange.get());
2515 void Editor::markAndReplaceFor(const SpellCheckRequest& request, const Vector<TextCheckingResult>& results)
2517 Ref<Frame> protection(m_frame);
2519 TextCheckingTypeMask textCheckingOptions = request.data().mask();
2520 TextCheckingParagraph paragraph(request.checkingRange(), &request.paragraphRange());
2522 const bool shouldMarkSpelling = textCheckingOptions & TextCheckingTypeSpelling;
2523 const bool shouldMarkGrammar = textCheckingOptions & TextCheckingTypeGrammar;
2524 const bool shouldMarkLink = textCheckingOptions & TextCheckingTypeLink;
2525 const bool shouldPerformReplacement = textCheckingOptions & (TextCheckingTypeQuote | TextCheckingTypeDash | TextCheckingTypeReplacement);
2526 const bool shouldShowCorrectionPanel = textCheckingOptions & TextCheckingTypeShowCorrectionPanel;
2527 const bool shouldCheckForCorrection = shouldShowCorrectionPanel || (textCheckingOptions & TextCheckingTypeCorrection);
2528 #if !USE(AUTOCORRECTION_PANEL)
2529 ASSERT(!shouldShowCorrectionPanel);
2532 // Expand the range to encompass entire paragraphs, since text checking needs that much context.
2533 int selectionOffset = 0;
2534 bool useAmbiguousBoundaryOffset = false;
2535 bool selectionChanged = false;
2536 bool restoreSelectionAfterChange = false;
2537 bool adjustSelectionForParagraphBoundaries = false;
2539 if (shouldPerformReplacement || shouldMarkSpelling || shouldCheckForCorrection) {
2540 if (m_frame.selection().selection().selectionType() == VisibleSelection::CaretSelection) {
2541 // Attempt to save the caret position so we can restore it later if needed
2542 Position caretPosition = m_frame.selection().selection().end();
2543 selectionOffset = paragraph.offsetTo(caretPosition).releaseReturnValue();
2544 restoreSelectionAfterChange = true;
2545 if (selectionOffset > 0 && (selectionOffset > paragraph.textLength() || paragraph.textCharAt(selectionOffset - 1) == newlineCharacter))
2546 adjustSelectionForParagraphBoundaries = true;
2547 if (selectionOffset > 0 && selectionOffset <= paragraph.textLength() && isAmbiguousBoundaryCharacter(paragraph.textCharAt(selectionOffset - 1)))
2548 useAmbiguousBoundaryOffset = true;
2552 int offsetDueToReplacement = 0;
2554 for (unsigned i = 0; i < results.size(); i++) {
2555 const int spellingRangeEndOffset = paragraph.checkingEnd() + offsetDueToReplacement;
2556 const TextCheckingType resultType = results[i].type;
2557 const int resultLocation = results[i].location + offsetDueToReplacement;
2558 const int resultLength = results[i].length;
2559 const int resultEndLocation = resultLocation + resultLength;
2560 const String& replacement = results[i].replacement;
2561 const bool resultEndsAtAmbiguousBoundary = useAmbiguousBoundaryOffset && selectionOffset - 1 <= resultEndLocation;
2563 // Only mark misspelling if:
2564 // 1. Current text checking isn't done for autocorrection, in which case shouldMarkSpelling is false.
2565 // 2. Result falls within spellingRange.
2566 // 3. The word in question doesn't end at an ambiguous boundary. For instance, we would not mark
2567 // "wouldn'" as misspelled right after apostrophe is typed.
2568 if (shouldMarkSpelling && !shouldShowCorrectionPanel && resultType == TextCheckingTypeSpelling
2569 && resultLocation >= paragraph.checkingStart() && resultEndLocation <= spellingRangeEndOffset && !resultEndsAtAmbiguousBoundary) {
2570 ASSERT(resultLength > 0 && resultLocation >= 0);
2571 auto misspellingRange = paragraph.subrange(resultLocation, resultLength);
2572 if (!m_alternativeTextController->isSpellingMarkerAllowed(misspellingRange))
2574 misspellingRange->startContainer().document().markers().addMarker(misspellingRange.ptr(), DocumentMarker::Spelling, replacement);
2575 } else if (shouldMarkGrammar && resultType == TextCheckingTypeGrammar && paragraph.checkingRangeCovers(resultLocation, resultLength)) {
2576 ASSERT(resultLength > 0 && resultLocation >= 0);
2577 for (auto& detail : results[i].details) {
2578 ASSERT(detail.length > 0 && detail.location >= 0);
2579 if (paragraph.checkingRangeCovers(resultLocation + detail.location, detail.length)) {
2580 auto badGrammarRange = paragraph.subrange(resultLocation + detail.location, detail.length);
2581 badGrammarRange->startContainer().document().markers().addMarker(badGrammarRange.ptr(), DocumentMarker::Grammar, detail.userDescription);
2584 } else if (resultEndLocation <= spellingRangeEndOffset && resultEndLocation >= paragraph.checkingStart()
2585 && isAutomaticTextReplacementType(resultType)) {
2586 // In this case the result range just has to touch the spelling range, so we can handle replacing non-word text such as punctuation.
2587 ASSERT(resultLength > 0 && resultLocation >= 0);
2589 if (shouldShowCorrectionPanel && (resultEndLocation < spellingRangeEndOffset
2590 || !(resultType & (TextCheckingTypeReplacement | TextCheckingTypeCorrection))))
2593 // Apply replacement if:
2594 // 1. The replacement length is non-zero.
2595 // 2. The result doesn't end at an ambiguous boundary.
2596 // (FIXME: this is required until 6853027 is fixed and text checking can do this for us
2597 bool doReplacement = replacement.length() > 0 && !resultEndsAtAmbiguousBoundary;
2598 auto rangeToReplace = paragraph.subrange(resultLocation, resultLength);
2600 // adding links should be done only immediately after they are typed
2601 if (resultType == TextCheckingTypeLink && selectionOffset != resultEndLocation + 1)
2604 if (!(shouldPerformReplacement || shouldCheckForCorrection || shouldMarkLink) || !doReplacement)
2607 String replacedString = plainText(rangeToReplace.ptr());
2608 const bool existingMarkersPermitReplacement = m_alternativeTextController->processMarkersOnTextToBeReplacedByResult(results[i], rangeToReplace, replacedString);
2609 if (!existingMarkersPermitReplacement)
2612 if (shouldShowCorrectionPanel) {
2613 if (resultEndLocation == spellingRangeEndOffset) {
2614 // We only show the correction panel on the last word.
2615 m_alternativeTextController->show(rangeToReplace, replacement);
2618 // If this function is called for showing correction panel, we ignore other correction or replacement.
2622 VisibleSelection selectionToReplace(rangeToReplace, DOWNSTREAM);
2623 if (selectionToReplace != m_frame.selection().selection()) {
2624 if (!m_frame.selection().shouldChangeSelection(selectionToReplace))
2628 if (resultType == TextCheckingTypeLink) {
2629 m_frame.selection().setSelection(selectionToReplace);
2630 selectionChanged = true;
2631 restoreSelectionAfterChange = false;
2632 if (canEditRichly())
2633 CreateLinkCommand::create(document(), replacement)->apply();
2634 } else if (canEdit() && shouldInsertText(replacement, rangeToReplace.ptr(), EditorInsertAction::Typed)) {
2635 correctSpellcheckingPreservingTextCheckingParagraph(paragraph, rangeToReplace, replacement, resultLocation, resultLength);
2637 if (AXObjectCache* cache = document().existingAXObjectCache()) {
2638 if (Element* root = m_frame.selection().selection().rootEditableElement())
2639 cache->postNotification(root, AXObjectCache::AXAutocorrectionOccured);
2642 // Skip all other results for the replaced text.
2643 while (i + 1 < results.size() && results[i + 1].location + offsetDueToReplacement <= resultLocation)
2646 selectionChanged = true;
2647 offsetDueToReplacement += replacement.length() - resultLength;
2648 if (resultLocation < selectionOffset)
2649 selectionOffset += replacement.length() - resultLength;
2651 if (resultType == TextCheckingTypeCorrection) {
2652 auto replacementRange = paragraph.subrange(resultLocation, replacement.length());
2653 m_alternativeTextController->recordAutocorrectionResponse(AutocorrectionResponse::Accepted, replacedString, replacementRange.ptr());
2655 // Add a marker so that corrections can easily be undone and won't be re-corrected.
2656 m_alternativeTextController->markCorrection(replacementRange, replacedString);
2662 if (selectionChanged) {
2663 TextCheckingParagraph extendedParagraph(WTFMove(paragraph));
2664 // Restore the caret position if we have made any replacements
2665 extendedParagraph.expandRangeToNextEnd();
2666 if (restoreSelectionAfterChange && selectionOffset >= 0 && selectionOffset <= extendedParagraph.rangeLength()) {
2667 auto selectionRange = extendedParagraph.subrange(0, selectionOffset);
2668 m_frame.selection().moveTo(selectionRange->endPosition(), DOWNSTREAM);
2669 if (adjustSelectionForParagraphBoundaries)
2670 m_frame.selection().modify(FrameSelection::AlterationMove, DirectionForward, CharacterGranularity);
2672 // If this fails for any reason, the fallback is to go one position beyond the last replacement
2673 m_frame.selection().moveTo(m_frame.selection().selection().end());
2674 m_frame.selection().modify(FrameSelection::AlterationMove, DirectionForward, CharacterGranularity);
2679 void Editor::changeBackToReplacedString(const String& replacedString)
2682 ASSERT(unifiedTextCheckerEnabled());
2684 if (replacedString.isEmpty())
2687 RefPtr<Range> selection = selectedRange();
2688 if (!shouldInsertText(replacedString, selection.get(), EditorInsertAction::Pasted))
2691 m_alternativeTextController->recordAutocorrectionResponse(AutocorrectionResponse::Reverted, replacedString, selection.get());
2692 TextCheckingParagraph paragraph(*selection);
2693 replaceSelectionWithText(replacedString, false, false, EditActionInsert);
2694 auto changedRange = paragraph.subrange(paragraph.checkingStart(), replacedString.length());
2695 changedRange->startContainer().document().markers().addMarker(changedRange.ptr(), DocumentMarker::Replacement, String());
2696 m_alternativeTextController->markReversed(changedRange);
2698 ASSERT_NOT_REACHED();
2699 UNUSED_PARAM(replacedString);
2700 #endif // !PLATFORM(IOS)
2704 void Editor::markMisspellingsAndBadGrammar(const VisibleSelection& spellingSelection, bool markGrammar, const VisibleSelection& grammarSelection)
2706 if (unifiedTextCheckerEnabled()) {
2707 if (!isContinuousSpellCheckingEnabled())
2710 // markMisspellingsAndBadGrammar() is triggered by selection change, in which case we check spelling and grammar, but don't autocorrect misspellings.
2711 TextCheckingTypeMask textCheckingOptions = TextCheckingTypeSpelling;
2712 if (markGrammar && isGrammarCheckingEnabled())
2713 textCheckingOptions |= TextCheckingTypeGrammar;
2714 markAllMisspellingsAndBadGrammarInRanges(textCheckingOptions, spellingSelection.toNormalizedRange().get(), grammarSelection.toNormalizedRange().get());
2718 RefPtr<Range> firstMisspellingRange;
2719 markMisspellings(spellingSelection, firstMisspellingRange);
2721 markBadGrammar(grammarSelection);
2724 void Editor::unappliedSpellCorrection(const VisibleSelection& selectionOfCorrected, const String& corrected, const String& correction)
2726 m_alternativeTextController->respondToUnappliedSpellCorrection(selectionOfCorrected, corrected, correction);
2729 void Editor::updateMarkersForWordsAffectedByEditing(bool doNotRemoveIfSelectionAtWordBoundary)
2731 if (!document().markers().hasMarkers())
2734 if (!m_alternativeTextController->shouldRemoveMarkersUponEditing() && (!textChecker() || textChecker()->shouldEraseMarkersAfterChangeSelection(TextCheckingTypeSpelling)))
2737 // We want to remove the markers from a word if an editing command will change the word. This can happen in one of
2738 // several scenarios:
2739 // 1. Insert in the middle of a word.
2740 // 2. Appending non whitespace at the beginning of word.
2741 // 3. Appending non whitespace at the end of word.
2742 // Note that, appending only whitespaces at the beginning or end of word won't change the word, so we don't need to
2743 // remove the markers on that word.
2744 // Of course, if current selection is a range, we potentially will edit two words that fall on the boundaries of
2745 // selection, and remove words between the selection boundaries.
2747 VisiblePosition startOfSelection = m_frame.selection().selection().start();
2748 VisiblePosition endOfSelection = m_frame.selection().selection().end();
2749 if (startOfSelection.isNull())
2751 // First word is the word that ends after or on the start of selection.
2752 VisiblePosition startOfFirstWord = startOfWord(startOfSelection, LeftWordIfOnBoundary);
2753 VisiblePosition endOfFirstWord = endOfWord(startOfSelection, LeftWordIfOnBoundary);
2754 // Last word is the word that begins before or on the end of selection
2755 VisiblePosition startOfLastWord = startOfWord(endOfSelection, RightWordIfOnBoundary);
2756 VisiblePosition endOfLastWord = endOfWord(endOfSelection, RightWordIfOnBoundary);
2758 if (startOfFirstWord.isNull()) {
2759 startOfFirstWord = startOfWord(startOfSelection, RightWordIfOnBoundary);
2760 endOfFirstWord = endOfWord(startOfSelection, RightWordIfOnBoundary);
2763 if (endOfLastWord.isNull()) {
2764 startOfLastWord = startOfWord(endOfSelection, LeftWordIfOnBoundary);
2765 endOfLastWord = endOfWord(endOfSelection, LeftWordIfOnBoundary);
2768 // If doNotRemoveIfSelectionAtWordBoundary is true, and first word ends at the start of selection,
2769 // we choose next word as the first word.
2770 if (doNotRemoveIfSelectionAtWordBoundary && endOfFirstWord == startOfSelection) {
2771 startOfFirstWord = nextWordPosition(startOfFirstWord);
2772 endOfFirstWord = endOfWord(startOfFirstWord, RightWordIfOnBoundary);
2773 if (startOfFirstWord == endOfSelection)
2777 // If doNotRemoveIfSelectionAtWordBoundary is true, and last word begins at the end of selection,
2778 // we choose previous word as the last word.
2779 if (doNotRemoveIfSelectionAtWordBoundary && startOfLastWord == endOfSelection) {
2780 startOfLastWord = previousWordPosition(startOfLastWord);
2781 endOfLastWord = endOfWord(startOfLastWord, RightWordIfOnBoundary);
2782 if (endOfLastWord == startOfSelection)
2786 if (startOfFirstWord.isNull() || endOfFirstWord.isNull() || startOfLastWord.isNull() || endOfLastWord.isNull())
2789 // Now we remove markers on everything between startOfFirstWord and endOfLastWord.
2790 // However, if an autocorrection change a single word to multiple words, we want to remove correction mark from all the
2791 // resulted words even we only edit one of them. For example, assuming autocorrection changes "avantgarde" to "avant
2792 // garde", we will have CorrectionIndicator marker on both words and on the whitespace between them. If we then edit garde,
2793 // we would like to remove the marker from word "avant" and whitespace as well. So we need to get the continous range of
2794 // of marker that contains the word in question, and remove marker on that whole range.
2795 auto wordRange = Range::create(document(), startOfFirstWord.deepEquivalent(), endOfLastWord.deepEquivalent());
2797 Vector<RenderedDocumentMarker*> markers = document().markers().markersInRange(wordRange, DocumentMarker::DictationAlternatives);
2798 for (auto* marker : markers)
2799 m_alternativeTextController->removeDictationAlternativesForMarker(*marker);
2801 OptionSet<DocumentMarker::MarkerType> markerTypesToRemove {
2802 DocumentMarker::CorrectionIndicator,
2803 DocumentMarker::DictationAlternatives,
2804 DocumentMarker::SpellCheckingExemption,
2805 DocumentMarker::Spelling,
2807 DocumentMarker::Grammar,
2810 document().markers().removeMarkers(wordRange.ptr(), markerTypesToRemove, DocumentMarkerController::RemovePartiallyOverlappingMarker);
2811 document().markers().clearDescriptionOnMarkersIntersectingRange(wordRange, DocumentMarker::Replacement);
2814 void Editor::deletedAutocorrectionAtPosition(const Position& position, const String& originalString)
2816 m_alternativeTextController->deletedAutocorrectionAtPosition(position, originalString);
2819 RefPtr<Range> Editor::rangeForPoint(const IntPoint& windowPoint)
2821 Document* document = m_frame.documentAtPoint(windowPoint);
2825 Frame* frame = document->frame();
2827 FrameView* frameView = frame->view();
2830 IntPoint framePoint = frameView->windowToContents(windowPoint);
2831 VisibleSelection selection(frame->visiblePositionForPoint(framePoint));
2833 return selection.toNormalizedRange();
2836 void Editor::revealSelectionAfterEditingOperation(const ScrollAlignment& alignment, RevealExtentOption revealExtentOption)
2838 if (m_ignoreSelectionChanges)
2842 SelectionRevealMode revealMode = SelectionRevealMode::RevealUpToMainFrame;
2844 SelectionRevealMode revealMode = SelectionRevealMode::Reveal;
2847 m_frame.selection().revealSelection(revealMode, alignment, revealExtentOption);
2850 void Editor::setIgnoreSelectionChanges(bool ignore, RevealSelection shouldRevealExistingSelection)
2852 if (m_ignoreSelectionChanges == ignore)
2855 m_ignoreSelectionChanges = ignore;
2857 // FIXME: Should suppress selection change notifications during a composition change <https://webkit.org/b/38830>
2859 respondToChangedSelection(m_frame.selection().selection(), 0);
2861 if (!ignore && shouldRevealExistingSelection == RevealSelection::Yes)
2862 revealSelectionAfterEditingOperation(ScrollAlignment::alignToEdgeIfNeeded, RevealExtent);
2865 RefPtr<Range> Editor::compositionRange() const
2867 if (!m_compositionNode)
2869 unsigned length = m_compositionNode->length();
2870 unsigned start = std::min(m_compositionStart, length);
2871 unsigned end = std::min(std::max(start, m_compositionEnd), length);
2874 return Range::create(m_compositionNode->document(), m_compositionNode.get(), start, m_compositionNode.get(), end);
2877 bool Editor::getCompositionSelection(unsigned& selectionStart, unsigned& selectionEnd) const
2879 if (!m_compositionNode)
2881 const VisibleSelection& selection = m_frame.selection().selection();
2882 Position start = selection.start();
2883 if (start.deprecatedNode() != m_compositionNode)
2885 Position end = selection.end();
2886 if (end.deprecatedNode() != m_compositionNode)
2889 if (static_cast<unsigned>(start.deprecatedEditingOffset()) < m_compositionStart)
2891 if (static_cast<unsigned>(end.deprecatedEditingOffset()) > m_compositionEnd)
2894 selectionStart = start.deprecatedEditingOffset() - m_compositionStart;
2895 selectionEnd = start.deprecatedEditingOffset() - m_compositionEnd;
2899 void Editor::transpose()
2904 VisibleSelection selection = m_frame.selection().selection();
2905 if (!selection.isCaret())
2908 // Make a selection that goes back one character and forward two characters.
2909 VisiblePosition caret = selection.visibleStart();
2910 VisiblePosition next = isEndOfParagraph(caret) ? caret : caret.next();
2911 VisiblePosition previous = next.previous();
2912 if (next == previous)
2914 previous = previous.previous();
2915 if (!inSameParagraph(next, previous))
2917 RefPtr<Range> range = makeRange(previous, next);
2920 VisibleSelection newSelection(*range, DOWNSTREAM);
2922 // Transpose the two characters.
2923 String text = plainText(range.get());
2924 if (text.length() != 2)
2926 String transposed = text.right(1) + text.left(1);
2928 // Select the two characters.
2929 if (newSelection != m_frame.selection().selection()) {
2930 if (!m_frame.selection().shouldChangeSelection(newSelection))
2932 m_frame.selection().setSelection(newSelection);
2935 // Insert the transposed characters.
2936 if (!shouldInsertText(transposed, range.get(), EditorInsertAction::Typed))
2938 replaceSelectionWithText(transposed, false, false, EditActionInsert);
2941 void Editor::addRangeToKillRing(const Range& range, KillRingInsertionMode mode)
2943 addTextToKillRing(plainText(&range), mode);
2946 void Editor::addTextToKillRing(const String& text, KillRingInsertionMode mode)
2948 if (m_shouldStartNewKillRingSequence)
2949 killRing().startNewSequence();
2951 m_shouldStartNewKillRingSequence = false;
2953 // If the kill was from a backwards motion, prepend to the kill ring.
2954 // This will ensure that alternating forward and backward kills will
2955 // build up the original string in the kill ring without permuting it.
2957 case KillRingInsertionMode::PrependText:
2958 killRing().prepend(text);
2960 case KillRingInsertionMode::AppendText:
2961 killRing().append(text);
2966 void Editor::startAlternativeTextUITimer()
2968 m_alternativeTextController->startAlternativeTextUITimer(AlternativeTextTypeCorrection);
2971 void Editor::handleAlternativeTextUIResult(const String& correction)
2973 m_alternativeTextController->handleAlternativeTextUIResult(correction);
2977 void Editor::dismissCorrectionPanelAsIgnored()
2979 m_alternativeTextController->dismiss(ReasonForDismissingAlternativeTextIgnored);
2982 void Editor::changeSelectionAfterCommand(const VisibleSelection& newSelection, FrameSelection::SetSelectionOptions options)
2984 Ref<Frame> protection(m_frame);
2986 // If the new selection is orphaned, then don't update the selection.
2987 if (newSelection.start().isOrphan() || newSelection.end().isOrphan())
2990 // If there is no selection change, don't bother sending shouldChangeSelection, but still call setSelection,
2991 // because there is work that it must do in this situation.
2992 // The old selection can be invalid here and calling shouldChangeSelection can produce some strange calls.
2993 // See <rdar://problem/5729315> Some shouldChangeSelectedDOMRange contain Ranges for selections that are no longer valid
2994 bool selectionDidNotChangeDOMPosition = newSelection == m_frame.selection().selection();
2995 if (selectionDidNotChangeDOMPosition || m_frame.selection().shouldChangeSelection(newSelection))
2996 m_frame.selection().setSelection(newSelection, options);
2998 // Some editing operations change the selection visually without affecting its position within the DOM.
2999 // For example when you press return in the following (the caret is marked by ^):
3000 // <div contentEditable="true"><div>^Hello</div></div>
3001 // WebCore inserts <div><br></div> *before* the current block, which correctly moves the paragraph down but which doesn't
3002 // change the caret's DOM position (["hello", 0]). In these situations the above FrameSelection::setSelection call
3003 // does not call EditorClient::respondToChangedSelection(), which, on the Mac, sends selection change notifications and
3004 // starts a new kill ring sequence, but we want to do these things (matches AppKit).
3006 // FIXME: Should suppress selection change notifications during a composition change <https://webkit.org/b/38830>
3007 if (m_ignoreSelectionChanges)
3010 if (selectionDidNotChangeDOMPosition && client())
3011 client()->respondToChangedSelection(&m_frame);
3014 String Editor::selectedText() const
3016 return selectedText(TextIteratorDefaultBehavior);
3019 String Editor::selectedTextForDataTransfer() const
3021 return selectedText(TextIteratorEmitsImageAltText);
3024 String Editor::selectedText(TextIteratorBehavior behavior) const
3026 // We remove '\0' characters because they are not visibly rendered to the user.
3027 return plainText(m_frame.selection().toNormalizedRange().get(), behavior).replaceWithLiteral('\0', "");
3030 static inline void collapseCaretWidth(IntRect& rect)
3032 // FIXME: Width adjustment doesn't work for rotated text.
3033 if (rect.width() == caretWidth)
3035 else if (rect.height() == caretWidth)
3039 IntRect Editor::firstRectForRange(Range* range) const
3041 VisiblePosition startVisiblePosition(range->startPosition(), DOWNSTREAM);
3043 if (range->collapsed()) {
3044 // FIXME: Getting caret rect and removing caret width is a very roundabout way to get collapsed range location.
3045 // In particular, width adjustment doesn't work for rotated text.
3046 IntRect startCaretRect = RenderedPosition(startVisiblePosition).absoluteRect();
3047 collapseCaretWidth(startCaretRect);
3048 return startCaretRect;
3051 VisiblePosition endVisiblePosition(range->endPosition(), UPSTREAM);
3053 if (inSameLine(startVisiblePosition, endVisiblePosition))
3054 return enclosingIntRect(RenderObject::absoluteBoundingBoxRectForRange(range));
3056 LayoutUnit extraWidthToEndOfLine = 0;
3057 IntRect startCaretRect = RenderedPosition(startVisiblePosition).absoluteRect(&extraWidthToEndOfLine);
3058 if (startCaretRect == IntRect())
3061 // When start and end aren't on the same line, we want to go from start to the end of its line.
3062 bool textIsHorizontal = startCaretRect.width() == caretWidth;
3063 return textIsHorizontal ?
3064 IntRect(startCaretRect.x(),
3066 startCaretRect.width() + extraWidthToEndOfLine,
3067 startCaretRect.height()) :
3068 IntRect(startCaretRect.x(),
3070 startCaretRect.width(),
3071 startCaretRect.height() + extraWidthToEndOfLine);
3074 bool Editor::shouldChangeSelection(const VisibleSelection& oldSelection, const VisibleSelection& newSelection, EAffinity affinity, bool stillSelecting) const
3077 if (m_frame.selectionChangeCallbacksDisabled())
3080 return client() && client()->shouldChangeSelectedRange(oldSelection.toNormalizedRange().get(), newSelection.toNormalizedRange().get(), affinity, stillSelecting);
3083 void Editor::computeAndSetTypingStyle(EditingStyle& style, EditAction editingAction)
3085 if (style.isEmpty()) {
3086 m_frame.selection().clearTypingStyle();
3090 // Calculate the current typing style.
3091 RefPtr<EditingStyle> typingStyle;
3092 if (auto existingTypingStyle = m_frame.selection().typingStyle())
3093 typingStyle = existingTypingStyle->copy();
3095 typingStyle = EditingStyle::create();
3096 typingStyle->overrideTypingStyleAt(style, m_frame.selection().selection().visibleStart().deepEquivalent());
3098 // Handle block styles, substracting these from the typing style.
3099 RefPtr<EditingStyle> blockStyle = typingStyle->extractAndRemoveBlockProperties();
3100 if (!blockStyle->isEmpty())
3101 ApplyStyleCommand::create(document(), blockStyle.get(), editingAction)->apply();
3103 // Set the remaining style as the typing style.
3104 m_frame.selection().setTypingStyle(WTFMove(typingStyle));
3107 void Editor::computeAndSetTypingStyle(StyleProperties& properties, EditAction editingAction)
3109 return computeAndSetTypingStyle(EditingStyle::create(&properties), editingAction);
3112 void Editor::textFieldDidBeginEditing(Element* e)
3115 client()->textFieldDidBeginEditing(e);
3118 void Editor::textFieldDidEndEditing(Element* e)
3120 dismissCorrectionPanelAsIgnored();
3122 client()->textFieldDidEndEditing(e);
3125 void Editor::textDidChangeInTextField(Element* e)
3128 client()->textDidChangeInTextField(e);
3131 bool Editor::doTextFieldCommandFromEvent(Element* e, KeyboardEvent* ke)
3134 return client()->doTextFieldCommandFromEvent(e, ke);
3139 void Editor::textWillBeDeletedInTextField(Element* input)
3142 client()->textWillBeDeletedInTextField(input);
3145 void Editor::textDidChangeInTextArea(Element* e)
3148 client()->textDidChangeInTextArea(e);
3151 void Editor::applyEditingStyleToBodyElement() const
3153 auto collection = document().getElementsByTagName(HTMLNames::bodyTag.localName());
3154 unsigned length = collection->length();
3155 for (unsigned i = 0; i < length; ++i)
3156 applyEditingStyleToElement(collection->item(i));
3159 void Editor::applyEditingStyleToElement(Element* element) const
3161 ASSERT(!element || is<StyledElement>(*element));
3162 if (!is<StyledElement>(element))
3165 // Mutate using the CSSOM wrapper so we get the same event behavior as a script.
3166 auto& style = downcast<StyledElement>(*element).cssomStyle();
3167 style.setPropertyInternal(CSSPropertyWordWrap, "break-word", false);
3168 style.setPropertyInternal(CSSPropertyWebkitNbspMode, "space", false);
3169 style.setPropertyInternal(CSSPropertyLineBreak, "after-white-space", false);
3172 bool Editor::findString(const String& target, FindOptions options)
3174 Ref<Frame> protection(m_frame);
3176 VisibleSelection selection = m_frame.selection().selection();
3178 RefPtr<Range> resultRange = rangeOfString(target, selection.firstRange().get(), options);
3183 m_frame.selection().setSelection(VisibleSelection(*resultRange, DOWNSTREAM));
3185 if (!(options & DoNotRevealSelection))
3186 m_frame.selection().revealSelection();
3191 RefPtr<Range> Editor::rangeOfString(const String& target, Range* referenceRange, FindOptions options)
3193 if (target.isEmpty())
3196 // Start from an edge of the reference range, if there's a reference range that's not in shadow content. Which edge
3197 // is used depends on whether we're searching forward or backward, and whether startInSelection is set.
3198 RefPtr<Range> searchRange(rangeOfContents(document()));
3200 bool forward = !(options & Backwards);
3201 bool startInReferenceRange = referenceRange && (options & StartInSelection);
3202 if (referenceRange) {
3204 searchRange->setStart(startInReferenceRange ? referenceRange->startPosition() : referenceRange->endPosition());
3206 searchRange->setEnd(startInReferenceRange ? referenceRange->endPosition() : referenceRange->startPosition());
3209 RefPtr<ShadowRoot> shadowTreeRoot = referenceRange ? referenceRange->startContainer().containingShadowRoot() : nullptr;
3210 if (shadowTreeRoot) {
3212 searchRange->setEnd(*shadowTreeRoot, shadowTreeRoot->countChildNodes());
3214 searchRange->setStart(*shadowTreeRoot, 0);
3217 RefPtr<Range> resultRange = findPlainText(*searchRange, target, options);
3218 // If we started in the reference range and the found range exactly matches the reference range, find again.
3219 // Build a selection with the found range to remove collapsed whitespace.
3220 // Compare ranges instead of selection objects to ignore the way that the current selection was made.
3221 if (startInReferenceRange && areRangesEqual(VisibleSelection(*resultRange).toNormalizedRange().get(), referenceRange)) {
3222 searchRange = rangeOfContents(document());
3224 searchRange->setStart(referenceRange->endPosition());
3226 searchRange->setEnd(referenceRange->startPosition());
3228 if (shadowTreeRoot) {
3230 searchRange->setEnd(*shadowTreeRoot, shadowTreeRoot->countChildNodes());
3232 searchRange->setStart(*shadowTreeRoot, 0);
3235 resultRange = findPlainText(*searchRange, target, options);
3238 // If nothing was found in the shadow tree, search in main content following the shadow tree.
3239 if (resultRange->collapsed() && shadowTreeRoot) {
3240 searchRange = rangeOfContents(document());
3241 if (shadowTreeRoot->shadowHost()) {
3243 searchRange->setStartAfter(*shadowTreeRoot->shadowHost());
3245 searchRange->setEndBefore(*shadowTreeRoot->shadowHost());
3248 resultRange = findPlainText(*searchRange, target, options);
3251 // If we didn't find anything and we're wrapping, search again in the entire document (this will
3252 // redundantly re-search the area already searched in some cases).
3253 if (resultRange->collapsed() && options & WrapAround) {
3254 searchRange = rangeOfContents(document());
3255 resultRange = findPlainText(*searchRange, target, options);
3256 // We used to return false here if we ended up with the same range that we started with
3257 // (e.g., the reference range was already the only instance of this text). But we decided that
3258 // this should be a success case instead, so we'll just fall through in that case.
3261 return resultRange->collapsed() ? nullptr : resultRange;
3264 static bool isFrameInRange(Frame& frame, Range& range)
3266 for (auto* ownerElement = frame.ownerElement(); ownerElement; ownerElement = ownerElement->document().ownerElement()) {
3267 if (&ownerElement->document() == &range.ownerDocument()) {
3268 auto result = range.intersectsNode(*ownerElement);
3269 return !result.hasException() && result.releaseReturnValue();
3275 unsigned Editor::countMatchesForText(const String& target, Range* range, FindOptions options, unsigned limit, bool markMatches, Vector<RefPtr<Range>>* matches)
3277 if (target.isEmpty())
3280 RefPtr<Range> searchRange;
3282 if (&range->ownerDocument() == &document())
3283 searchRange = range;
3284 else if (!isFrameInRange(m_frame, *range))
3288 searchRange = rangeOfContents(document());
3290 Node& originalEndContainer = searchRange->endContainer();
3291 int originalEndOffset = searchRange->endOffset();
3293 unsigned matchCount = 0;
3295 RefPtr<Range> resultRange(findPlainText(*searchRange, target, options & ~Backwards));
3296 if (resultRange->collapsed()) {
3297 if (!resultRange->startContainer().isInShadowTree())
3300 searchRange->setStartAfter(*resultRange->startContainer().shadowHost());
3301 searchRange->setEnd(originalEndContainer, originalEndOffset);
3307 matches->append(resultRange);
3310 document().markers().addMarker(resultRange.get(), DocumentMarker::TextMatch);
3312 // Stop looking if we hit the specified limit. A limit of 0 means no limit.
3313 if (limit > 0 && matchCount >= limit)
3316 // Set the new start for the search range to be the end of the previous
3317 // result range. There is no need to use a VisiblePosition here,
3318 // since findPlainText will use a TextIterator to go over the visible
3320 searchRange->setStart(resultRange->endContainer(), resultRange->endOffset());
3322 Node* shadowTreeRoot = searchRange->shadowRoot();
3323 if (searchRange->collapsed() && shadowTreeRoot)
3324 searchRange->setEnd(*shadowTreeRoot, shadowTreeRoot->countChildNodes());
3330 void Editor::setMarkedTextMatchesAreHighlighted(bool flag)
3332 if (flag == m_areMarkedTextMatchesHighlighted)
3335 m_areMarkedTextMatchesHighlighted = flag;
3336 document().markers().repaintMarkers(DocumentMarker::TextMatch);
3340 void Editor::selectionWillChange()
3345 void Editor::respondToChangedSelection(const VisibleSelection&, FrameSelection::SetSelectionOptions options)
3348 // FIXME: Should suppress selection change notifications during a composition change <https://webkit.org/b/38830>
3349 if (m_ignoreSelectionChanges)
3354 client()->respondToChangedSelection(&m_frame);
3356 #if ENABLE(TELEPHONE_NUMBER_DETECTION) && !PLATFORM(IOS)
3357 if (shouldDetectTelephoneNumbers())
3358 m_telephoneNumberDetectionUpdateTimer.startOneShot(0_s);
3361 setStartNewKillRingSequence(true);
3363 if (m_editorUIUpdateTimer.isActive())
3366 // Don't check spelling and grammar if the change of selection is triggered by spelling correction itself.
3367 m_editorUIUpdateTimerShouldCheckSpellingAndGrammar = options & FrameSelection::CloseTyping
3368 && !(options & FrameSelection::SpellCorrectionTriggered);
3369 m_editorUIUpdateTimerWasTriggeredByDictation = options & FrameSelection::DictationTriggered;
3370 m_editorUIUpdateTimer.startOneShot(0_s);
3373 #if ENABLE(TELEPHONE_NUMBER_DETECTION) && !PLATFORM(IOS)
3375 bool Editor::shouldDetectTelephoneNumbers()
3377 if (!m_frame.document())
3379 return document().isTelephoneNumberParsingEnabled() && TelephoneNumberDetector::isSupported();
3382 void Editor::scanSelectionForTelephoneNumbers()
3384 if (!shouldDetectTelephoneNumbers() || !client())
3387 m_detectedTelephoneNumberRanges.clear();
3389 Vector<RefPtr<Range>> markedRanges;
3391 FrameSelection& frameSelection = m_frame.selection();
3392 if (!frameSelection.isRange()) {
3393 m_frame.mainFrame().servicesOverlayController().selectedTelephoneNumberRangesChanged();
3396 RefPtr<Range> selectedRange = frameSelection.toNormalizedRange();
3398 // Extend the range a few characters in each direction to detect incompletely selected phone numbers.
3399 static const int charactersToExtend = 15;
3400 const VisibleSelection& visibleSelection = frameSelection.selection();
3401 Position start = visibleSelection.start();
3402 Position end = visibleSelection.end();
3403 for (int i = 0; i < charactersToExtend; ++i) {
3404 start = start.previous(Character);
3405 end = end.next(Character);
3408 FrameSelection extendedSelection;
3409 extendedSelection.setStart(start);
3410 extendedSelection.setEnd(end);
3411 RefPtr<Range> extendedRange = extendedSelection.toNormalizedRange();
3413 if (!extendedRange) {
3414 m_frame.mainFrame().servicesOverlayController().selectedTelephoneNumberRangesChanged();
3418 scanRangeForTelephoneNumbers(*extendedRange, extendedRange->text(), markedRanges);
3420 // Only consider ranges with a detected telephone number if they overlap with the actual selection range.
3421 for (auto& range : markedRanges) {
3422 if (rangesOverlap(range.get(), selectedRange.get()))
3423 m_detectedTelephoneNumberRanges.append(range);
3426 m_frame.mainFrame().servicesOverlayController().selectedTelephoneNumberRangesChanged();
3429 void Editor::scanRangeForTelephoneNumbers(Range& range, const StringView& stringView, Vector<RefPtr<Range>>& markedRanges)
3431 // Don't scan for phone numbers inside editable regions.
3432 Node& startNode = range.startContainer();
3433 if (startNode.hasEditableStyle())
3436 // relativeStartPosition and relativeEndPosition are the endpoints of the phone number range,
3437 // relative to the scannerPosition
3438 unsigned length = stringView.length();
3439 unsigned scannerPosition = 0;
3440 int relativeStartPosition = 0;
3441 int relativeEndPosition = 0;
3443 auto characters = stringView.upconvertedCharacters();
3445 while (scannerPosition < length && TelephoneNumberDetector::find(&characters[scannerPosition], length - scannerPosition, &relativeStartPosition, &relativeEndPosition)) {
3446 // The convention in the Data Detectors framework is that the end position is the first character NOT in the phone number
3447 // (that is, the length of the range is relativeEndPosition - relativeStartPosition). So subtract 1 to get the same
3448 // convention as the old WebCore phone number parser (so that the rest of the code is still valid if we want to go back
3449 // to the old parser).
3450 --relativeEndPosition;
3452 ASSERT(scannerPosition + relativeEndPosition < length);
3454 unsigned subrangeOffset = scannerPosition + relativeStartPosition;
3455 unsigned subrangeLength = relativeEndPosition - relativeStartPosition + 1;
3457 RefPtr<Range> subrange = TextIterator::subrange(range, subrangeOffset, subrangeLength);
3459 markedRanges.append(subrange);
3460 range.ownerDocument().markers().addMarker(subrange.get(), DocumentMarker::TelephoneNumber);
3462 scannerPosition += relativeEndPosition + 1;
3466 #endif // ENABLE(TELEPHONE_NUMBER_DETECTION) && !PLATFORM(IOS)
3468 void Editor::updateEditorUINowIfScheduled()
3470 if (!m_editorUIUpdateTimer.isActive())
3472 m_editorUIUpdateTimer.stop();
3473 editorUIUpdateTimerFired();
3476 void Editor::editorUIUpdateTimerFired()
3478 VisibleSelection oldSelection = m_oldSelectionForEditorUIUpdate;
3480 m_alternativeTextController->stopPendingCorrection(oldSelection);
3482 bool isContinuousSpellCheckingEnabled = this->isContinuousSpellCheckingEnabled();
3483 bool isContinuousGrammarCheckingEnabled = isContinuousSpellCheckingEnabled && isGrammarCheckingEnabled();
3484 if (isContinuousSpellCheckingEnabled) {
3485 VisibleSelection newAdjacentWords;
3486 VisibleSelection newSelectedSentence;
3487 bool caretBrowsing = m_frame.settings().caretBrowsingEnabled();
3488 if (m_frame.selection().selection().isContentEditable() || caretBrowsing) {
3489 VisiblePosition newStart(m_frame.selection().selection().visibleStart());
3491 newAdjacentWords = VisibleSelection(startOfWord(newStart, LeftWordIfOnBoundary), endOfWord(newStart, RightWordIfOnBoundary));
3493 // If this bug gets fixed, this PLATFORM(IOS) code could be removed:
3494 // <rdar://problem/7259611> Word boundary code on iPhone gives different results than desktop
3495 EWordSide startWordSide = LeftWordIfOnBoundary;
3496 UChar32 c = newStart.characterBefore();
3497 // FIXME: VisiblePosition::characterAfter() and characterBefore() do not emit newlines the same
3498 // way as TextIterator, so we do an isStartOfParagraph check here.
3499 if (isSpaceOrNewline(c) || c == 0xA0 || isStartOfParagraph(newStart)) {
3500 startWordSide = RightWordIfOnBoundary;
3502 newAdjacentWords = VisibleSelection(startOfWord(newStart, startWordSide), endOfWord(newStart, RightWordIfOnBoundary));
3503 #endif // !PLATFORM(IOS)
3504 if (isContinuousGrammarCheckingEnabled)
3505 newSelectedSentence = VisibleSelection(startOfSentence(newStart), endOfSentence(newStart));
3508 // When typing we check spelling elsewhere, so don't redo it here.
3509 // If this is a change in selection resulting from a delete operation,
3510 // oldSelection may no longer be in the document.
3511 if (m_editorUIUpdateTimerShouldCheckSpellingAndGrammar && oldSelection.isContentEditable() && oldSelection.start().deprecatedNode() && oldSelection.start().anchorNode()->isConnected()) {
3512 VisiblePosition oldStart(oldSelection.visibleStart());
3513 VisibleSelection oldAdjacentWords = VisibleSelection(startOfWord(oldStart, LeftWordIfOnBoundary), endOfWord(oldStart, RightWordIfOnBoundary));
3514 if (oldAdjacentWords != newAdjacentWords) {
3515 if (isContinuousGrammarCheckingEnabled) {
3516 VisibleSelection oldSelectedSentence = VisibleSelection(startOfSentence(oldStart), endOfSentence(oldStart));
3517 markMisspellingsAndBadGrammar(oldAdjacentWords, oldSelectedSentence != newSelectedSentence, oldSelectedSentence);
3519 markMisspellingsAndBadGrammar(oldAdjacentWords, false, oldAdjacentWords);
3523 if (!textChecker() || textChecker()->shouldEraseMarkersAfterChangeSelection(TextCheckingTypeSpelling)) {
3524 if (RefPtr<Range> wordRange = newAdjacentWords.toNormalizedRange())
3525 document().markers().removeMarkers(wordRange.get(), DocumentMarker::Spelling);
3527 if (!textChecker() || textChecker()->shouldEraseMarkersAfterChangeSelection(TextCheckingTypeGrammar)) {
3528 if (RefPtr<Range> sentenceRange = newSelectedSentence.toNormalizedRange())
3529 document().markers().removeMarkers(sentenceRange.get(), DocumentMarker::Grammar);
3533 // When continuous spell checking is off, existing markers disappear after the selection changes.
3534 if (!isContinuousSpellCheckingEnabled)
3535 document().markers().removeMarkers(DocumentMarker::Spelling);
3536 if (!isContinuousGrammarCheckingEnabled)
3537 document().markers().removeMarkers(DocumentMarker::Grammar);
3539 if (!m_editorUIUpdateTimerWasTriggeredByDictation)
3540 m_alternativeTextController->respondToChangedSelection(oldSelection);
3542 m_oldSelectionForEditorUIUpdate = m_frame.selection().selection();