#include "config.h"
#include "Editor.h"
#include "AXObjectCache.h"
#include "AlternativeTextController.h"
#include "ApplyStyleCommand.h"
#include "CSSComputedStyleDeclaration.h"
#include "CSSPropertyNames.h"
#include "CachedResourceLoader.h"
#include "ClipboardEvent.h"
#include "CompositionEvent.h"
#include "CreateLinkCommand.h"
#include "DataTransfer.h"
#include "DeleteSelectionCommand.h"
#include "DictationAlternative.h"
#include "DictationCommand.h"
#include "DocumentFragment.h"
#include "DocumentMarkerController.h"
#include "EditorClient.h"
#include "EventHandler.h"
#include "EventNames.h"
#include "ExceptionCodePlaceholder.h"
#include "FocusController.h"
#include "Frame.h"
#include "FrameTree.h"
#include "FrameView.h"
#include "GraphicsContext.h"
#include "HTMLCollection.h"
#include "HTMLFormControlElement.h"
#include "HTMLFrameOwnerElement.h"
#include "HTMLImageElement.h"
#include "HTMLNames.h"
#include "HTMLTextAreaElement.h"
#include "HitTestResult.h"
#include "IndentOutdentCommand.h"
#include "InsertListCommand.h"
#include "KeyboardEvent.h"
#include "KillRing.h"
#include "MainFrame.h"
#include "ModifySelectionListLevel.h"
#include "NodeList.h"
#include "NodeTraversal.h"
#include "Page.h"
#include "Pasteboard.h"
#include "Range.h"
#include "RemoveFormatCommand.h"
#include "RenderBlock.h"
#include "RenderTextControl.h"
#include "RenderedDocumentMarker.h"
#include "RenderedPosition.h"
#include "ReplaceRangeWithTextCommand.h"
#include "ReplaceSelectionCommand.h"
#include "Settings.h"
#include "ShadowRoot.h"
#include "SimplifyMarkupCommand.h"
#include "Sound.h"
#include "SpellChecker.h"
#include "SpellingCorrectionCommand.h"
#include "StyleProperties.h"
#include "TelephoneNumberDetector.h"
#include "Text.h"
#include "TextCheckerClient.h"
#include "TextCheckingHelper.h"
#include "TextEvent.h"
#include "TextIterator.h"
#include "TypingCommand.h"
#include "UserTypingGestureIndicator.h"
#include "VisibleUnits.h"
#include "htmlediting.h"
#include "markup.h"
#include <wtf/unicode/CharacterNames.h>
#if PLATFORM(IOS)
#include "DictationCommandIOS.h"
#include <wtf/text/StringBuilder.h>
#include <wtf/text/WTFString.h>
#endif
#if PLATFORM(MAC)
#include "ServicesOverlayController.h"
#endif
namespace WebCore {
class ClearTextCommand : public DeleteSelectionCommand {
public:
ClearTextCommand(Document& document);
static void CreateAndApply(const RefPtr<Frame> frame);
private:
virtual EditAction editingAction() const;
};
ClearTextCommand::ClearTextCommand(Document& document)
: DeleteSelectionCommand(document, false, true, false, false, true)
{
}
EditAction ClearTextCommand::editingAction() const
{
return EditActionDelete;
}
void ClearTextCommand::CreateAndApply(const RefPtr<Frame> frame)
{
if (frame->selection().isNone())
return;
frame->editor().clear();
const VisibleSelection oldSelection = frame->selection().selection();
frame->selection().selectAll();
auto clearCommand = adoptRef(*new ClearTextCommand(*frame->document()));
clearCommand->setStartingSelection(oldSelection);
applyCommand(WTFMove(clearCommand));
}
using namespace HTMLNames;
using namespace WTF;
using namespace Unicode;
VisibleSelection Editor::selectionForCommand(Event* event)
{
VisibleSelection selection = m_frame.selection().selection();
if (!event)
return selection;
HTMLTextFormControlElement* textFormControlOfSelectionStart = enclosingTextFormControl(selection.start());
HTMLTextFormControlElement* textFromControlOfTarget = is<HTMLTextFormControlElement>(*event->target()->toNode()) ? downcast<HTMLTextFormControlElement>(event->target()->toNode()) : nullptr;
if (textFromControlOfTarget && (selection.start().isNull() || textFromControlOfTarget != textFormControlOfSelectionStart)) {
if (RefPtr<Range> range = textFromControlOfTarget->selection())
return VisibleSelection(*range, DOWNSTREAM, selection.isDirectional());
}
return selection;
}
EditingBehavior Editor::behavior() const
{
return EditingBehavior(m_frame.settings().editingBehaviorType());
}
EditorClient* Editor::client() const
{
if (Page* page = m_frame.page())
return &page->editorClient();
return nullptr;
}
TextCheckerClient* Editor::textChecker() const
{
if (EditorClient* owner = client())
return owner->textChecker();
return 0;
}
void Editor::handleKeyboardEvent(KeyboardEvent* event)
{
if (EditorClient* c = client())
c->handleKeyboardEvent(event);
}
void Editor::handleInputMethodKeydown(KeyboardEvent* event)
{
if (EditorClient* c = client())
c->handleInputMethodKeydown(event);
}
bool Editor::handleTextEvent(TextEvent* event)
{
if (event->isDrop())
return false;
if (event->isPaste()) {
if (event->pastingFragment())
#if PLATFORM(IOS)
{
if (client()->performsTwoStepPaste(event->pastingFragment()))
return true;
#endif
replaceSelectionWithFragment(event->pastingFragment(), false, event->shouldSmartReplace(), event->shouldMatchStyle(), EditActionPaste, event->mailBlockquoteHandling());
#if PLATFORM(IOS)
}
#endif
else
replaceSelectionWithText(event->data(), false, event->shouldSmartReplace(), EditActionPaste);
return true;
}
String data = event->data();
if (data == "\n") {
if (event->isLineBreak())
return insertLineBreak();
return insertParagraphSeparator();
}
return insertTextWithoutSendingTextEvent(data, false, event);
}
bool Editor::canEdit() const
{
return m_frame.selection().selection().rootEditableElement();
}
bool Editor::canEditRichly() const
{
return m_frame.selection().selection().isContentRichlyEditable();
}
bool Editor::canDHTMLCut()
{
return !m_frame.selection().selection().isInPasswordField() && !dispatchCPPEvent(eventNames().beforecutEvent, DataTransferAccessPolicy::Numb);
}
bool Editor::canDHTMLCopy()
{
return !m_frame.selection().selection().isInPasswordField() && !dispatchCPPEvent(eventNames().beforecopyEvent, DataTransferAccessPolicy::Numb);
}
bool Editor::canDHTMLPaste()
{
return !dispatchCPPEvent(eventNames().beforepasteEvent, DataTransferAccessPolicy::Numb);
}
bool Editor::canCut() const
{
return canCopy() && canDelete();
}
static HTMLImageElement* imageElementFromImageDocument(Document& document)
{
if (!document.isImageDocument())
return nullptr;
HTMLElement* body = document.bodyOrFrameset();
if (!body)
return nullptr;
Node* node = body->firstChild();
if (!is<HTMLImageElement>(node))
return nullptr;
return downcast<HTMLImageElement>(node);
}
bool Editor::canCopy() const
{
if (imageElementFromImageDocument(document()))
return true;
const VisibleSelection& selection = m_frame.selection().selection();
return selection.isRange() && !selection.isInPasswordField();
}
bool Editor::canPaste() const
{
return canEdit();
}
bool Editor::canDelete() const
{
const VisibleSelection& selection = m_frame.selection().selection();
return selection.isRange() && selection.rootEditableElement();
}
bool Editor::canDeleteRange(Range* range) const
{
Node& startContainer = range->startContainer();
Node& endContainer = range->endContainer();
if (!startContainer.hasEditableStyle() || !endContainer.hasEditableStyle())
return false;
if (range->collapsed()) {
VisiblePosition start(range->startPosition(), DOWNSTREAM);
VisiblePosition previous = start.previous();
if (previous.isNull() || previous.deepEquivalent().deprecatedNode()->rootEditableElement() != startContainer.rootEditableElement())
return false;
}
return true;
}
bool Editor::smartInsertDeleteEnabled()
{
return client() && client()->smartInsertDeleteEnabled();
}
bool Editor::canSmartCopyOrDelete()
{
return client() && client()->smartInsertDeleteEnabled() && m_frame.selection().granularity() == WordGranularity;
}
bool Editor::isSelectTrailingWhitespaceEnabled()
{
return client() && client()->isSelectTrailingWhitespaceEnabled();
}
bool Editor::deleteWithDirection(SelectionDirection direction, TextGranularity granularity, bool shouldAddToKillRing, bool isTypingAction)
{
if (!canEdit())
return false;
if (m_frame.selection().isRange()) {
if (isTypingAction) {
TypingCommand::deleteKeyPressed(document(), canSmartCopyOrDelete() ? TypingCommand::SmartDelete : 0, granularity);
revealSelectionAfterEditingOperation();
} else {
if (shouldAddToKillRing)
addRangeToKillRing(*selectedRange().get(), KillRingInsertionMode::AppendText);
deleteSelectionWithSmartDelete(canSmartCopyOrDelete());
}
} else {
TypingCommand::Options options = 0;
if (canSmartCopyOrDelete())
options |= TypingCommand::SmartDelete;
if (shouldAddToKillRing)
options |= TypingCommand::AddsToKillRing;
switch (direction) {
case DirectionForward:
case DirectionRight:
TypingCommand::forwardDeleteKeyPressed(document(), options, granularity);
break;
case DirectionBackward:
case DirectionLeft:
TypingCommand::deleteKeyPressed(document(), options, granularity);
break;
}
revealSelectionAfterEditingOperation();
}
if (shouldAddToKillRing)
setStartNewKillRingSequence(false);
return true;
}
void Editor::deleteSelectionWithSmartDelete(bool smartDelete, EditAction editingAction)
{
if (m_frame.selection().isNone())
return;
applyCommand(DeleteSelectionCommand::create(document(), smartDelete, true, false, false, true, editingAction));
}
void Editor::clearText()
{
ClearTextCommand::CreateAndApply(&m_frame);
}
#if PLATFORM(IOS)
void Editor::insertDictationPhrases(Vector<Vector<String>>&& dictationPhrases, RetainPtr<id> metadata)
{
if (m_frame.selection().isNone())
return;
if (dictationPhrases.isEmpty())
return;
applyCommand(DictationCommandIOS::create(document(), WTFMove(dictationPhrases), WTFMove(metadata)));
}
void Editor::setDictationPhrasesAsChildOfElement(const Vector<Vector<String>>& dictationPhrases, RetainPtr<id> metadata, Element& element)
{
clear();
clearUndoRedoOperations();
m_frame.selection().clear();
element.removeChildren();
if (dictationPhrases.isEmpty()) {
client()->respondToChangedContents();
return;
}
ExceptionCode ec;
RefPtr<Range> context = document().createRange();
context->selectNodeContents(element, ec);
StringBuilder dictationPhrasesBuilder;
for (auto& interpretations : dictationPhrases)
dictationPhrasesBuilder.append(interpretations[0]);
element.appendChild(createFragmentFromText(*context, dictationPhrasesBuilder.toString()), ec);
document().updateLayout();
if (!element.firstChild()->isTextNode()) {
ASSERT(element.firstChild()->isTextNode());
return;
}
Text& textNode = downcast<Text>(*element.firstChild());
int previousDictationPhraseStart = 0;
for (auto& interpretations : dictationPhrases) {
int dictationPhraseLength = interpretations[0].length();
int dictationPhraseEnd = previousDictationPhraseStart + dictationPhraseLength;
if (interpretations.size() > 1) {
auto dictationPhraseRange = Range::create(document(), &textNode, previousDictationPhraseStart, &textNode, dictationPhraseEnd);
document().markers().addDictationPhraseWithAlternativesMarker(dictationPhraseRange.ptr(), interpretations);
}
previousDictationPhraseStart = dictationPhraseEnd;
}
auto resultRange = Range::create(document(), &textNode, 0, &textNode, textNode.length());
document().markers().addDictationResultMarker(resultRange.ptr(), metadata);
client()->respondToChangedContents();
}
#endif
void Editor::pasteAsPlainText(const String& pastingText, bool smartReplace)
{
Node* target = findEventTargetFromSelection();
if (!target)
return;
target->dispatchEvent(TextEvent::createForPlainTextPaste(document().domWindow(), pastingText, smartReplace));
}
void Editor::pasteAsFragment(Ref<DocumentFragment>&& pastingFragment, bool smartReplace, bool matchStyle, MailBlockquoteHandling respectsMailBlockquote)
{
Node* target = findEventTargetFromSelection();
if (!target)
return;
target->dispatchEvent(TextEvent::createForFragmentPaste(document().domWindow(), WTFMove(pastingFragment), smartReplace, matchStyle, respectsMailBlockquote));
}
void Editor::pasteAsPlainTextBypassingDHTML()
{
pasteAsPlainTextWithPasteboard(*Pasteboard::createForCopyAndPaste());
}
void Editor::pasteAsPlainTextWithPasteboard(Pasteboard& pasteboard)
{
String text = readPlainTextFromPasteboard(pasteboard);
if (client() && client()->shouldInsertText(text, selectedRange().get(), EditorInsertActionPasted))
pasteAsPlainText(text, canSmartReplaceWithPasteboard(pasteboard));
}
String Editor::readPlainTextFromPasteboard(Pasteboard& pasteboard)
{
PasteboardPlainText text;
pasteboard.read(text);
return plainTextFromPasteboard(text);
}
#if !PLATFORM(MAC)
String Editor::plainTextFromPasteboard(const PasteboardPlainText& text)
{
return text.text;
}
#endif
bool Editor::canSmartReplaceWithPasteboard(Pasteboard& pasteboard)
{
return client() && client()->smartInsertDeleteEnabled() && pasteboard.canSmartReplace();
}
bool Editor::shouldInsertFragment(PassRefPtr<DocumentFragment> fragment, PassRefPtr<Range> replacingDOMRange, EditorInsertAction givenAction)
{
if (!client())
return false;
if (fragment) {
Node* child = fragment->firstChild();
if (is<CharacterData>(child) && fragment->lastChild() == child)
return client()->shouldInsertText(downcast<CharacterData>(*child).data(), replacingDOMRange.get(), givenAction);
}
return client()->shouldInsertNode(fragment.get(), replacingDOMRange.get(), givenAction);
}
void Editor::replaceSelectionWithFragment(PassRefPtr<DocumentFragment> fragment, bool selectReplacement, bool smartReplace, bool matchStyle, EditAction editingAction, MailBlockquoteHandling mailBlockquoteHandling)
{
VisibleSelection selection = m_frame.selection().selection();
if (selection.isNone() || !selection.isContentEditable() || !fragment)
return;
AccessibilityReplacedText replacedText;
if (AXObjectCache::accessibilityEnabled() && editingAction == EditActionPaste)
replacedText = AccessibilityReplacedText(selection);
ReplaceSelectionCommand::CommandOptions options = ReplaceSelectionCommand::PreventNesting | ReplaceSelectionCommand::SanitizeFragment;
if (selectReplacement)
options |= ReplaceSelectionCommand::SelectReplacement;
if (smartReplace)
options |= ReplaceSelectionCommand::SmartReplace;
if (matchStyle)
options |= ReplaceSelectionCommand::MatchStyle;
if (mailBlockquoteHandling == MailBlockquoteHandling::IgnoreBlockquote)
options |= ReplaceSelectionCommand::IgnoreMailBlockquote;
RefPtr<ReplaceSelectionCommand> command = ReplaceSelectionCommand::create(document(), fragment, options, editingAction);
applyCommand(command);
revealSelectionAfterEditingOperation();
selection = m_frame.selection().selection();
if (selection.isInPasswordField())
return;
if (AXObjectCache::accessibilityEnabled() && editingAction == EditActionPaste) {
String text = AccessibilityObject::stringForVisiblePositionRange(command->visibleSelectionForInsertedText());
replacedText.postTextStateChangeNotification(document().existingAXObjectCache(), AXTextEditTypePaste, text, m_frame.selection().selection());
command->composition()->setRangeDeletedByUnapply(replacedText.replacedRange());
}
if (!isContinuousSpellCheckingEnabled())
return;
Node* nodeToCheck = selection.rootEditableElement();
if (!nodeToCheck)
return;
RefPtr<Range> rangeToCheck = Range::create(document(), firstPositionInNode(nodeToCheck), lastPositionInNode(nodeToCheck));
m_spellChecker->requestCheckingFor(SpellCheckRequest::create(resolveTextCheckingTypeMask(TextCheckingTypeSpelling | TextCheckingTypeGrammar), TextCheckingProcessBatch, rangeToCheck, rangeToCheck));
}
void Editor::replaceSelectionWithText(const String& text, bool selectReplacement, bool smartReplace, EditAction editingAction)
{
RefPtr<Range> range = selectedRange();
if (!range)
return;
replaceSelectionWithFragment(createFragmentFromText(*range, text), selectReplacement, smartReplace, true, editingAction);
}
RefPtr<Range> Editor::selectedRange()
{
return m_frame.selection().toNormalizedRange();
}
#if PLATFORM(IOS)
void Editor::confirmMarkedText()
{
Element* focused = document().focusedElement();
Node* composition = compositionNode();
if (composition && focused && focused != composition && !composition->isDescendantOrShadowDescendantOf(focused)) {
cancelComposition();
document().setFocusedElement(focused);
} else
confirmComposition();
}
void Editor::setTextAsChildOfElement(const String& text, Element& element)
{
clear();
clearUndoRedoOperations();
if (!element.firstChild() && text.isEmpty())
return;
m_frame.selection().clear();
element.removeChildren();
if (text.length()) {
ExceptionCode ec;
RefPtr<Node> parent = element.parentNode();
RefPtr<Node> siblingAfter = element.nextSibling();
if (parent)
element.remove(ec);
auto context = document().createRange();
context->selectNodeContents(element, ec);
element.appendChild(createFragmentFromText(context, text), ec);
if (parent) {
if (siblingAfter)
parent->insertBefore(element, siblingAfter.get(), ec);
else
parent->appendChild(element, ec);
}
}
VisibleSelection selection;
Position pos = createLegacyEditingPosition(&element, element.countChildNodes());
VisiblePosition visiblePos(pos, VP_DEFAULT_AFFINITY);
if (visiblePos.isNull())
return;
selection.setBase(visiblePos);
selection.setExtent(visiblePos);
m_frame.selection().setSelection(selection);
client()->respondToChangedContents();
}
#endif
bool Editor::shouldDeleteRange(Range* range) const
{
if (!range || range->collapsed())
return false;
if (!canDeleteRange(range))
return false;
return client() && client()->shouldDeleteRange(range);
}
bool Editor::tryDHTMLCopy()
{
if (m_frame.selection().selection().isInPasswordField())
return false;
return !dispatchCPPEvent(eventNames().copyEvent, DataTransferAccessPolicy::Writable);
}
bool Editor::tryDHTMLCut()
{
if (m_frame.selection().selection().isInPasswordField())
return false;
return !dispatchCPPEvent(eventNames().cutEvent, DataTransferAccessPolicy::Writable);
}
bool Editor::tryDHTMLPaste()
{
return !dispatchCPPEvent(eventNames().pasteEvent, DataTransferAccessPolicy::Readable);
}
bool Editor::shouldInsertText(const String& text, Range* range, EditorInsertAction action) const
{
return client() && client()->shouldInsertText(text, range, action);
}
void Editor::respondToChangedContents(const VisibleSelection& endingSelection)
{
if (AXObjectCache::accessibilityEnabled()) {
Node* node = endingSelection.start().deprecatedNode();
if (AXObjectCache* cache = document().existingAXObjectCache())
cache->postNotification(node, AXObjectCache::AXValueChanged, TargetObservableParent);
}
updateMarkersForWordsAffectedByEditing(true);
if (client())
client()->respondToChangedContents();
}
bool Editor::hasBidiSelection() const
{
if (m_frame.selection().isNone())
return false;
Node* startNode;
if (m_frame.selection().isRange()) {
startNode = m_frame.selection().selection().start().downstream().deprecatedNode();
Node* endNode = m_frame.selection().selection().end().upstream().deprecatedNode();
if (enclosingBlock(startNode) != enclosingBlock(endNode))
return false;
} else
startNode = m_frame.selection().selection().visibleStart().deepEquivalent().deprecatedNode();
if (!startNode)
return false;
auto renderer = startNode->renderer();
while (renderer && !is<RenderBlockFlow>(*renderer))
renderer = renderer->parent();
if (!renderer)
return false;
if (!renderer->style().isLeftToRightDirection())
return true;
return downcast<RenderBlockFlow>(*renderer).containsNonZeroBidiLevel();
}
TriState Editor::selectionUnorderedListState() const
{
if (m_frame.selection().isCaret()) {
if (enclosingElementWithTag(m_frame.selection().selection().start(), ulTag))
return TrueTriState;
} else if (m_frame.selection().isRange()) {
auto* startNode = enclosingElementWithTag(m_frame.selection().selection().start(), ulTag);
auto* endNode = enclosingElementWithTag(m_frame.selection().selection().end(), ulTag);
if (startNode && endNode && startNode == endNode)
return TrueTriState;
}
return FalseTriState;
}
TriState Editor::selectionOrderedListState() const
{
if (m_frame.selection().isCaret()) {
if (enclosingElementWithTag(m_frame.selection().selection().start(), olTag))
return TrueTriState;
} else if (m_frame.selection().isRange()) {
auto* startNode = enclosingElementWithTag(m_frame.selection().selection().start(), olTag);
auto* endNode = enclosingElementWithTag(m_frame.selection().selection().end(), olTag);
if (startNode && endNode && startNode == endNode)
return TrueTriState;
}
return FalseTriState;
}
PassRefPtr<Node> Editor::insertOrderedList()
{
if (!canEditRichly())
return 0;
RefPtr<Node> newList = InsertListCommand::insertList(document(), InsertListCommand::OrderedList);
revealSelectionAfterEditingOperation();
return newList;
}
PassRefPtr<Node> Editor::insertUnorderedList()
{
if (!canEditRichly())
return 0;
RefPtr<Node> newList = InsertListCommand::insertList(document(), InsertListCommand::UnorderedList);
revealSelectionAfterEditingOperation();
return newList;
}
bool Editor::canIncreaseSelectionListLevel()
{
return canEditRichly() && IncreaseSelectionListLevelCommand::canIncreaseSelectionListLevel(&document());
}
bool Editor::canDecreaseSelectionListLevel()
{
return canEditRichly() && DecreaseSelectionListLevelCommand::canDecreaseSelectionListLevel(&document());
}
RefPtr<Node> Editor::increaseSelectionListLevel()
{
if (!canEditRichly() || m_frame.selection().isNone())
return nullptr;
RefPtr<Node> newList = IncreaseSelectionListLevelCommand::increaseSelectionListLevel(&document());
revealSelectionAfterEditingOperation();
return newList;
}
RefPtr<Node> Editor::increaseSelectionListLevelOrdered()
{
if (!canEditRichly() || m_frame.selection().isNone())
return nullptr;
RefPtr<Node> newList = IncreaseSelectionListLevelCommand::increaseSelectionListLevelOrdered(&document());
revealSelectionAfterEditingOperation();
return newList;
}
RefPtr<Node> Editor::increaseSelectionListLevelUnordered()
{
if (!canEditRichly() || m_frame.selection().isNone())
return nullptr;
RefPtr<Node> newList = IncreaseSelectionListLevelCommand::increaseSelectionListLevelUnordered(&document());
revealSelectionAfterEditingOperation();
return newList;
}
void Editor::decreaseSelectionListLevel()
{
if (!canEditRichly() || m_frame.selection().isNone())
return;
DecreaseSelectionListLevelCommand::decreaseSelectionListLevel(&document());
revealSelectionAfterEditingOperation();
}
void Editor::removeFormattingAndStyle()
{
applyCommand(RemoveFormatCommand::create(document()));
}
void Editor::clearLastEditCommand()
{
m_lastEditCommand = nullptr;
}
#if PLATFORM(IOS)
void Editor::ensureLastEditCommandHasCurrentSelectionIfOpenForMoreTyping()
{
TypingCommand::ensureLastEditCommandHasCurrentSelectionIfOpenForMoreTyping(&m_frame, m_frame.selection().selection());
}
#endif
bool Editor::dispatchCPPEvent(const AtomicString& eventType, DataTransferAccessPolicy policy)
{
Node* target = findEventTargetFromSelection();
if (!target)
return true;
RefPtr<DataTransfer> dataTransfer = DataTransfer::createForCopyAndPaste(policy);
Ref<Event> event = ClipboardEvent::create(eventType, true, true, dataTransfer.get());
target->dispatchEvent(event);
bool noDefaultProcessing = event->defaultPrevented();
if (noDefaultProcessing && policy == DataTransferAccessPolicy::Writable) {
auto pasteboard = Pasteboard::createForCopyAndPaste();
pasteboard->clear();
pasteboard->writePasteboard(dataTransfer->pasteboard());
}
dataTransfer->setAccessPolicy(DataTransferAccessPolicy::Numb);
return !noDefaultProcessing;
}
Node* Editor::findEventTargetFrom(const VisibleSelection& selection) const
{
Node* target = selection.start().element();
if (!target)
target = document().bodyOrFrameset();
if (!target)
return nullptr;
return target;
}
Node* Editor::findEventTargetFromSelection() const
{
return findEventTargetFrom(m_frame.selection().selection());
}
void Editor::applyStyle(StyleProperties* style, EditAction editingAction)
{
switch (m_frame.selection().selection().selectionType()) {
case VisibleSelection::NoSelection:
return;
case VisibleSelection::CaretSelection:
computeAndSetTypingStyle(EditingStyle::create(style), editingAction);
break;
case VisibleSelection::RangeSelection:
if (style)
applyCommand(ApplyStyleCommand::create(document(), EditingStyle::create(style).ptr(), editingAction));
break;
}
client()->didApplyStyle();
}
void Editor::applyStyle(RefPtr<EditingStyle>&& style, EditAction editingAction)
{
switch (m_frame.selection().selection().selectionType()) {
case VisibleSelection::NoSelection:
return;
case VisibleSelection::CaretSelection:
computeAndSetTypingStyle(*style, editingAction);
break;
case VisibleSelection::RangeSelection:
if (style)
applyCommand(ApplyStyleCommand::create(document(), style.get(), editingAction));
break;
}
client()->didApplyStyle();
}
bool Editor::shouldApplyStyle(StyleProperties* style, Range* range)
{
return client()->shouldApplyStyle(style, range);
}
void Editor::applyParagraphStyle(StyleProperties* style, EditAction editingAction)
{
switch (m_frame.selection().selection().selectionType()) {
case VisibleSelection::NoSelection:
return;
case VisibleSelection::CaretSelection:
case VisibleSelection::RangeSelection:
if (style)
applyCommand(ApplyStyleCommand::create(document(), EditingStyle::create(style).ptr(), editingAction, ApplyStyleCommand::ForceBlockProperties));
break;
}
client()->didApplyStyle();
}
void Editor::applyStyleToSelection(StyleProperties* style, EditAction editingAction)
{
if (!style || style->isEmpty() || !canEditRichly())
return;
if (!client() || !client()->shouldApplyStyle(style, m_frame.selection().toNormalizedRange().get()))
return;
applyStyle(style, editingAction);
}
void Editor::applyStyleToSelection(Ref<EditingStyle>&& style, EditAction editingAction)
{
if (style->isEmpty() || !canEditRichly())
return;
if (!client() || !client()->shouldApplyStyle(style->styleWithResolvedTextDecorations().ptr(), m_frame.selection().toNormalizedRange().get()))
return;
applyStyle(WTFMove(style), editingAction);
}
void Editor::applyParagraphStyleToSelection(StyleProperties* style, EditAction editingAction)
{
if (!style || style->isEmpty() || !canEditRichly())
return;
if (client() && client()->shouldApplyStyle(style, m_frame.selection().toNormalizedRange().get()))
applyParagraphStyle(style, editingAction);
}
bool Editor::selectionStartHasStyle(CSSPropertyID propertyID, const String& value) const
{
return EditingStyle::create(propertyID, value)->triStateOfStyle(
EditingStyle::styleAtSelectionStart(m_frame.selection().selection(), propertyID == CSSPropertyBackgroundColor).get());
}
TriState Editor::selectionHasStyle(CSSPropertyID propertyID, const String& value) const
{
return EditingStyle::create(propertyID, value)->triStateOfStyle(m_frame.selection().selection());
}
String Editor::selectionStartCSSPropertyValue(CSSPropertyID propertyID)
{
RefPtr<EditingStyle> selectionStyle = EditingStyle::styleAtSelectionStart(m_frame.selection().selection(),
propertyID == CSSPropertyBackgroundColor);
if (!selectionStyle || !selectionStyle->style())
return String();
if (propertyID == CSSPropertyFontSize)
return String::number(selectionStyle->legacyFontSize(&document()));
return selectionStyle->style()->getPropertyValue(propertyID);
}
void Editor::indent()
{
applyCommand(IndentOutdentCommand::create(document(), IndentOutdentCommand::Indent));
}
void Editor::outdent()
{
applyCommand(IndentOutdentCommand::create(document(), IndentOutdentCommand::Outdent));
}
static void notifyTextFromControls(Element* startRoot, Element* endRoot)
{
HTMLTextFormControlElement* startingTextControl = enclosingTextFormControl(firstPositionInOrBeforeNode(startRoot));
HTMLTextFormControlElement* endingTextControl = enclosingTextFormControl(firstPositionInOrBeforeNode(endRoot));
if (startingTextControl)
startingTextControl->didEditInnerTextValue();
if (endingTextControl && startingTextControl != endingTextControl)
endingTextControl->didEditInnerTextValue();
}
static void dispatchEditableContentChangedEvents(PassRefPtr<Element> prpStartRoot, PassRefPtr<Element> prpEndRoot)
{
RefPtr<Element> startRoot = prpStartRoot;
RefPtr<Element> endRoot = prpEndRoot;
if (startRoot)
startRoot->dispatchEvent(Event::create(eventNames().webkitEditableContentChangedEvent, false, false));
if (endRoot && endRoot != startRoot)
endRoot->dispatchEvent(Event::create(eventNames().webkitEditableContentChangedEvent, false, false));
}
void Editor::appliedEditing(PassRefPtr<CompositeEditCommand> cmd)
{
document().updateLayout();
EditCommandComposition* composition = cmd->composition();
ASSERT(composition);
VisibleSelection newSelection(cmd->endingSelection());
notifyTextFromControls(composition->startingRootEditableElement(), composition->endingRootEditableElement());
FrameSelection::SetSelectionOptions options = cmd->isDictationCommand() ? FrameSelection::DictationTriggered : 0;
changeSelectionAfterCommand(newSelection, options);
dispatchEditableContentChangedEvents(composition->startingRootEditableElement(), composition->endingRootEditableElement());
updateEditorUINowIfScheduled();
m_alternativeTextController->respondToAppliedEditing(cmd.get());
if (!cmd->preservesTypingStyle())
m_frame.selection().clearTypingStyle();
if (m_lastEditCommand.get() == cmd)
ASSERT(cmd->isTypingCommand());
else {
m_lastEditCommand = cmd;
if (client())
client()->registerUndoStep(m_lastEditCommand->ensureComposition());
}
respondToChangedContents(newSelection);
}
void Editor::unappliedEditing(PassRefPtr<EditCommandComposition> cmd)
{
document().updateLayout();
notifyTextFromControls(cmd->startingRootEditableElement(), cmd->endingRootEditableElement());
VisibleSelection newSelection(cmd->startingSelection());
changeSelectionAfterCommand(newSelection, FrameSelection::defaultSetSelectionOptions());
dispatchEditableContentChangedEvents(cmd->startingRootEditableElement(), cmd->endingRootEditableElement());
updateEditorUINowIfScheduled();
m_alternativeTextController->respondToUnappliedEditing(cmd.get());
m_lastEditCommand = nullptr;
if (client())
client()->registerRedoStep(cmd);
respondToChangedContents(newSelection);
}
void Editor::reappliedEditing(PassRefPtr<EditCommandComposition> cmd)
{
document().updateLayout();
notifyTextFromControls(cmd->startingRootEditableElement(), cmd->endingRootEditableElement());
VisibleSelection newSelection(cmd->endingSelection());
changeSelectionAfterCommand(newSelection, FrameSelection::defaultSetSelectionOptions());
dispatchEditableContentChangedEvents(cmd->startingRootEditableElement(), cmd->endingRootEditableElement());
updateEditorUINowIfScheduled();
m_lastEditCommand = nullptr;
if (client())
client()->registerUndoStep(cmd);
respondToChangedContents(newSelection);
}
Editor::Editor(Frame& frame)
: m_frame(frame)
, m_ignoreCompositionSelectionChange(false)
, m_shouldStyleWithCSS(false)
, m_killRing(std::make_unique<KillRing>())
, m_spellChecker(std::make_unique<SpellChecker>(frame))
, m_alternativeTextController(std::make_unique<AlternativeTextController>(frame))
, m_areMarkedTextMatchesHighlighted(false)
, m_defaultParagraphSeparator(EditorParagraphSeparatorIsDiv)
, m_overwriteModeEnabled(false)
, m_editorUIUpdateTimer(*this, &Editor::editorUIUpdateTimerFired)
, m_editorUIUpdateTimerShouldCheckSpellingAndGrammar(false)
, m_editorUIUpdateTimerWasTriggeredByDictation(false)
#if ENABLE(TELEPHONE_NUMBER_DETECTION) && !PLATFORM(IOS)
, m_telephoneNumberDetectionUpdateTimer(*this, &Editor::scanSelectionForTelephoneNumbers)
#endif
{
}
Editor::~Editor()
{
}
void Editor::clear()
{
if (m_compositionNode) {
m_compositionNode = nullptr;
if (EditorClient* client = this->client())
client->discardedComposition(&m_frame);
}
m_customCompositionUnderlines.clear();
m_shouldStyleWithCSS = false;
m_defaultParagraphSeparator = EditorParagraphSeparatorIsDiv;
}
bool Editor::insertText(const String& text, Event* triggeringEvent)
{
return m_frame.eventHandler().handleTextInputEvent(text, triggeringEvent);
}
bool Editor::insertTextForConfirmedComposition(const String& text)
{
return m_frame.eventHandler().handleTextInputEvent(text, 0, TextEventInputComposition);
}
bool Editor::insertDictatedText(const String& text, const Vector<DictationAlternative>& dictationAlternatives, Event* triggeringEvent)
{
return m_alternativeTextController->insertDictatedText(text, dictationAlternatives, triggeringEvent);
}
bool Editor::insertTextWithoutSendingTextEvent(const String& text, bool selectInsertedText, TextEvent* triggeringEvent)
{
if (text.isEmpty())
return false;
VisibleSelection selection = selectionForCommand(triggeringEvent);
if (!selection.isContentEditable())
return false;
RefPtr<Range> range = selection.toNormalizedRange();
if (!shouldInsertText(text, range.get(), EditorInsertActionTyped))
return true;
updateMarkersForWordsAffectedByEditing(isSpaceOrNewline(text[0]));
bool shouldConsiderApplyingAutocorrection = false;
if (text == " " || text == "\t")
shouldConsiderApplyingAutocorrection = true;
if (text.length() == 1 && u_ispunct(text[0]) && !isAmbiguousBoundaryCharacter(text[0]))
shouldConsiderApplyingAutocorrection = true;
bool autocorrectionWasApplied = shouldConsiderApplyingAutocorrection && m_alternativeTextController->applyAutocorrectionBeforeTypingIfAppropriate();
selection = selectionForCommand(triggeringEvent);
if (selection.isContentEditable()) {
if (Node* selectionStart = selection.start().deprecatedNode()) {
Ref<Document> document(selectionStart->document());
if (triggeringEvent && triggeringEvent->isDictation())
DictationCommand::insertText(document, text, triggeringEvent->dictationAlternatives(), selection);
else {
TypingCommand::Options options = 0;
if (selectInsertedText)
options |= TypingCommand::SelectInsertedText;
if (autocorrectionWasApplied)
options |= TypingCommand::RetainAutocorrectionIndicator;
TypingCommand::insertText(document, text, selection, options, triggeringEvent && triggeringEvent->isComposition() ? TypingCommand::TextCompositionConfirm : TypingCommand::TextCompositionNone);
}
if (Frame* editedFrame = document->frame())
if (Page* page = editedFrame->page()) {
#if PLATFORM(IOS)
SelectionRevealMode revealMode = SelectionRevealMode::RevealUpToMainFrame;
#else
SelectionRevealMode revealMode = SelectionRevealMode::Reveal;
#endif
page->focusController().focusedOrMainFrame().selection().revealSelection(revealMode, ScrollAlignment::alignCenterIfNeeded);
}
}
}
return true;
}
bool Editor::insertLineBreak()
{
if (!canEdit())
return false;
if (!shouldInsertText("\n", m_frame.selection().toNormalizedRange().get(), EditorInsertActionTyped))
return true;
VisiblePosition caret = m_frame.selection().selection().visibleStart();
bool alignToEdge = isEndOfEditableOrNonEditableContent(caret);
bool autocorrectionIsApplied = m_alternativeTextController->applyAutocorrectionBeforeTypingIfAppropriate();
TypingCommand::insertLineBreak(document(), autocorrectionIsApplied ? TypingCommand::RetainAutocorrectionIndicator : 0);
revealSelectionAfterEditingOperation(alignToEdge ? ScrollAlignment::alignToEdgeIfNeeded : ScrollAlignment::alignCenterIfNeeded);
return true;
}
bool Editor::insertParagraphSeparator()
{
if (!canEdit())
return false;
if (!canEditRichly())
return insertLineBreak();
if (!shouldInsertText("\n", m_frame.selection().toNormalizedRange().get(), EditorInsertActionTyped))
return true;
VisiblePosition caret = m_frame.selection().selection().visibleStart();
bool alignToEdge = isEndOfEditableOrNonEditableContent(caret);
bool autocorrectionIsApplied = m_alternativeTextController->applyAutocorrectionBeforeTypingIfAppropriate();
TypingCommand::insertParagraphSeparator(document(), autocorrectionIsApplied ? TypingCommand::RetainAutocorrectionIndicator : 0);
revealSelectionAfterEditingOperation(alignToEdge ? ScrollAlignment::alignToEdgeIfNeeded : ScrollAlignment::alignCenterIfNeeded);
return true;
}
bool Editor::insertParagraphSeparatorInQuotedContent()
{
TypingCommand::insertParagraphSeparatorInQuotedContent(document());
revealSelectionAfterEditingOperation();
return true;
}
void Editor::cut()
{
if (tryDHTMLCut())
return; if (!canCut()) {
systemBeep();
return;
}
performCutOrCopy(CutAction);
}
void Editor::copy()
{
if (tryDHTMLCopy())
return; if (!canCopy()) {
systemBeep();
return;
}
performCutOrCopy(CopyAction);
}
void Editor::postTextStateChangeNotificationForCut(const String& text, const VisibleSelection& selection)
{
if (!AXObjectCache::accessibilityEnabled())
return;
if (!text.length())
return;
AXObjectCache* cache = document().existingAXObjectCache();
if (!cache)
return;
cache->postTextStateChangeNotification(selection.start().anchorNode(), AXTextEditTypeCut, text, selection.start());
}
void Editor::performCutOrCopy(EditorActionSpecifier action)
{
RefPtr<Range> selection = selectedRange();
willWriteSelectionToPasteboard(selection);
if (action == CutAction) {
if (!shouldDeleteRange(selection.get()))
return;
updateMarkersForWordsAffectedByEditing(true);
}
if (enclosingTextFormControl(m_frame.selection().selection().start()))
Pasteboard::createForCopyAndPaste()->writePlainText(selectedTextForDataTransfer(), canSmartCopyOrDelete() ? Pasteboard::CanSmartReplace : Pasteboard::CannotSmartReplace);
else {
HTMLImageElement* imageElement = nullptr;
if (action == CopyAction)
imageElement = imageElementFromImageDocument(document());
if (imageElement) {
#if PLATFORM(COCOA) || PLATFORM(EFL) || PLATFORM(GTK)
writeImageToPasteboard(*Pasteboard::createForCopyAndPaste(), *imageElement, document().url(), document().title());
#else
Pasteboard::createForCopyAndPaste()->writeImage(*imageElement, document().url(), document().title());
#endif
} else {
#if PLATFORM(COCOA) || PLATFORM(EFL) || PLATFORM(GTK)
writeSelectionToPasteboard(*Pasteboard::createForCopyAndPaste());
#else
Pasteboard::createForCopyAndPaste()->writeSelection(*selection, canSmartCopyOrDelete(), m_frame, IncludeImageAltTextForDataTransfer);
#endif
}
}
didWriteSelectionToPasteboard();
if (action == CutAction) {
String text;
if (AXObjectCache::accessibilityEnabled())
text = AccessibilityObject::stringForVisiblePositionRange(m_frame.selection().selection());
deleteSelectionWithSmartDelete(canSmartCopyOrDelete(), EditActionCut);
if (AXObjectCache::accessibilityEnabled())
postTextStateChangeNotificationForCut(text, m_frame.selection().selection());
}
}
void Editor::paste()
{
paste(*Pasteboard::createForCopyAndPaste());
}
void Editor::paste(Pasteboard& pasteboard)
{
if (tryDHTMLPaste())
return; if (!canPaste())
return;
updateMarkersForWordsAffectedByEditing(false);
ResourceCacheValidationSuppressor validationSuppressor(document().cachedResourceLoader());
if (m_frame.selection().selection().isContentRichlyEditable())
pasteWithPasteboard(&pasteboard, true);
else
pasteAsPlainTextWithPasteboard(pasteboard);
}
void Editor::pasteAsPlainText()
{
if (tryDHTMLPaste())
return;
if (!canPaste())
return;
updateMarkersForWordsAffectedByEditing(false);
pasteAsPlainTextWithPasteboard(*Pasteboard::createForCopyAndPaste());
}
void Editor::performDelete()
{
if (!canDelete()) {
systemBeep();
return;
}
addRangeToKillRing(*selectedRange().get(), KillRingInsertionMode::AppendText);
deleteSelectionWithSmartDelete(canSmartCopyOrDelete());
setStartNewKillRingSequence(false);
}
void Editor::simplifyMarkup(Node* startNode, Node* endNode)
{
if (!startNode)
return;
if (endNode) {
if (&startNode->document() != &endNode->document())
return;
Node* node = startNode;
while (node && node != endNode)
node = NodeTraversal::next(*node);
if (!node)
return;
}
applyCommand(SimplifyMarkupCommand::create(document(), startNode, endNode ? NodeTraversal::next(*endNode) : nullptr));
}
void Editor::copyURL(const URL& url, const String& title)
{
copyURL(url, title, *Pasteboard::createForCopyAndPaste());
}
void Editor::copyURL(const URL& url, const String& title, Pasteboard& pasteboard)
{
PasteboardURL pasteboardURL;
pasteboardURL.url = url;
pasteboardURL.title = title;
#if PLATFORM(MAC)
fillInUserVisibleForm(pasteboardURL);
#endif
pasteboard.write(pasteboardURL);
}
#if !PLATFORM(IOS)
void Editor::copyImage(const HitTestResult& result)
{
Element* element = result.innerNonSharedElement();
if (!element)
return;
URL url = result.absoluteLinkURL();
if (url.isEmpty())
url = result.absoluteImageURL();
#if PLATFORM(COCOA) || PLATFORM(EFL) || PLATFORM(GTK)
writeImageToPasteboard(*Pasteboard::createForCopyAndPaste(), *element, url, result.altDisplayString());
#else
Pasteboard::createForCopyAndPaste()->writeImage(*element, url, result.altDisplayString());
#endif
}
#endif
bool Editor::isContinuousSpellCheckingEnabled() const
{
return client() && client()->isContinuousSpellCheckingEnabled();
}
void Editor::toggleContinuousSpellChecking()
{
if (client())
client()->toggleContinuousSpellChecking();
}
bool Editor::isGrammarCheckingEnabled()
{
return client() && client()->isGrammarCheckingEnabled();
}
void Editor::toggleGrammarChecking()
{
if (client())
client()->toggleGrammarChecking();
}
int Editor::spellCheckerDocumentTag()
{
return client() ? client()->spellCheckerDocumentTag() : 0;
}
#if USE(APPKIT)
void Editor::uppercaseWord()
{
if (client())
client()->uppercaseWord();
}
void Editor::lowercaseWord()
{
if (client())
client()->lowercaseWord();
}
void Editor::capitalizeWord()
{
if (client())
client()->capitalizeWord();
}
#endif
#if USE(AUTOMATIC_TEXT_REPLACEMENT)
void Editor::showSubstitutionsPanel()
{
if (!client()) {
LOG_ERROR("No NSSpellChecker");
return;
}
if (client()->substitutionsPanelIsShowing()) {
client()->showSubstitutionsPanel(false);
return;
}
client()->showSubstitutionsPanel(true);
}
bool Editor::substitutionsPanelIsShowing()
{
if (!client())
return false;
return client()->substitutionsPanelIsShowing();
}
void Editor::toggleSmartInsertDelete()
{
if (client())
client()->toggleSmartInsertDelete();
}
bool Editor::isAutomaticQuoteSubstitutionEnabled()
{
return client() && client()->isAutomaticQuoteSubstitutionEnabled();
}
void Editor::toggleAutomaticQuoteSubstitution()
{
if (client())
client()->toggleAutomaticQuoteSubstitution();
}
bool Editor::isAutomaticLinkDetectionEnabled()
{
return client() && client()->isAutomaticLinkDetectionEnabled();
}
void Editor::toggleAutomaticLinkDetection()
{
if (client())
client()->toggleAutomaticLinkDetection();
}
bool Editor::isAutomaticDashSubstitutionEnabled()
{
return client() && client()->isAutomaticDashSubstitutionEnabled();
}
void Editor::toggleAutomaticDashSubstitution()
{
if (client())
client()->toggleAutomaticDashSubstitution();
}
bool Editor::isAutomaticTextReplacementEnabled()
{
return client() && client()->isAutomaticTextReplacementEnabled();
}
void Editor::toggleAutomaticTextReplacement()
{
if (client())
client()->toggleAutomaticTextReplacement();
}
bool Editor::isAutomaticSpellingCorrectionEnabled()
{
return m_alternativeTextController->isAutomaticSpellingCorrectionEnabled();
}
void Editor::toggleAutomaticSpellingCorrection()
{
if (client())
client()->toggleAutomaticSpellingCorrection();
}
#endif
bool Editor::shouldEndEditing(Range* range)
{
return client() && client()->shouldEndEditing(range);
}
bool Editor::shouldBeginEditing(Range* range)
{
return client() && client()->shouldBeginEditing(range);
}
void Editor::clearUndoRedoOperations()
{
if (client())
client()->clearUndoRedoOperations();
}
bool Editor::canUndo()
{
return client() && client()->canUndo();
}
void Editor::undo()
{
if (client())
client()->undo();
}
bool Editor::canRedo()
{
return client() && client()->canRedo();
}
void Editor::redo()
{
if (client())
client()->redo();
}
void Editor::didBeginEditing()
{
if (client())
client()->didBeginEditing();
}
void Editor::didEndEditing()
{
if (client())
client()->didEndEditing();
}
void Editor::willWriteSelectionToPasteboard(PassRefPtr<Range> range)
{
if (client())
client()->willWriteSelectionToPasteboard(range.get());
}
void Editor::didWriteSelectionToPasteboard()
{
if (client())
client()->didWriteSelectionToPasteboard();
}
void Editor::toggleBold()
{
command("ToggleBold").execute();
}
void Editor::toggleUnderline()
{
command("ToggleUnderline").execute();
}
void Editor::setBaseWritingDirection(WritingDirection direction)
{
#if PLATFORM(IOS)
if (inSameParagraph(m_frame.selection().selection().visibleStart(), m_frame.selection().selection().visibleEnd()) &&
baseWritingDirectionForSelectionStart() == direction)
return;
#endif
Element* focusedElement = document().focusedElement();
if (is<HTMLTextFormControlElement>(focusedElement)) {
if (direction == NaturalWritingDirection)
return;
downcast<HTMLTextFormControlElement>(*focusedElement).setAttributeWithoutSynchronization(dirAttr, direction == LeftToRightWritingDirection ? "ltr" : "rtl");
focusedElement->dispatchInputEvent();
document().updateStyleIfNeeded();
return;
}
RefPtr<MutableStyleProperties> style = MutableStyleProperties::create();
style->setProperty(CSSPropertyDirection, direction == LeftToRightWritingDirection ? "ltr" : direction == RightToLeftWritingDirection ? "rtl" : "inherit", false);
applyParagraphStyleToSelection(style.get(), EditActionSetWritingDirection);
}
WritingDirection Editor::baseWritingDirectionForSelectionStart() const
{
WritingDirection result = LeftToRightWritingDirection;
Position pos = m_frame.selection().selection().visibleStart().deepEquivalent();
Node* node = pos.deprecatedNode();
if (!node)
return result;
auto renderer = node->renderer();
if (!renderer)
return result;
if (!renderer->isRenderBlockFlow()) {
renderer = renderer->containingBlock();
if (!renderer)
return result;
}
switch (renderer->style().direction()) {
case LTR:
return LeftToRightWritingDirection;
case RTL:
return RightToLeftWritingDirection;
}
return result;
}
void Editor::selectComposition()
{
RefPtr<Range> range = compositionRange();
if (!range)
return;
VisibleSelection selection;
selection.setWithoutValidation(range->startPosition(), range->endPosition());
m_frame.selection().setSelection(selection, 0);
}
void Editor::confirmComposition()
{
if (!m_compositionNode)
return;
setComposition(m_compositionNode->data().substring(m_compositionStart, m_compositionEnd - m_compositionStart), ConfirmComposition);
}
void Editor::cancelComposition()
{
if (!m_compositionNode)
return;
setComposition(emptyString(), CancelComposition);
}
bool Editor::cancelCompositionIfSelectionIsInvalid()
{
unsigned start;
unsigned end;
if (!hasComposition() || ignoreCompositionSelectionChange() || getCompositionSelection(start, end))
return false;
cancelComposition();
return true;
}
void Editor::confirmComposition(const String& text)
{
setComposition(text, ConfirmComposition);
}
void Editor::setComposition(const String& text, SetCompositionMode mode)
{
ASSERT(mode == ConfirmComposition || mode == CancelComposition);
UserTypingGestureIndicator typingGestureIndicator(m_frame);
setIgnoreCompositionSelectionChange(true);
if (mode == CancelComposition)
ASSERT(text == emptyString());
else
selectComposition();
if (m_frame.selection().isNone()) {
setIgnoreCompositionSelectionChange(false);
return;
}
if (Element* target = document().focusedElement()) {
Ref<CompositionEvent> event = CompositionEvent::create(eventNames().compositionendEvent, document().domWindow(), text);
target->dispatchEvent(event);
}
if (text.isEmpty() && mode != CancelComposition)
TypingCommand::deleteSelection(document(), 0);
m_compositionNode = nullptr;
m_customCompositionUnderlines.clear();
insertTextForConfirmedComposition(text);
if (mode == CancelComposition) {
TypingCommand::closeTyping(&m_frame);
}
setIgnoreCompositionSelectionChange(false);
}
void Editor::setComposition(const String& text, const Vector<CompositionUnderline>& underlines, unsigned selectionStart, unsigned selectionEnd)
{
UserTypingGestureIndicator typingGestureIndicator(m_frame);
setIgnoreCompositionSelectionChange(true);
document().updateStyleIfNeeded();
selectComposition();
if (m_frame.selection().isNone()) {
setIgnoreCompositionSelectionChange(false);
return;
}
#if PLATFORM(IOS)
client()->startDelayingAndCoalescingContentChangeNotifications();
#endif
Element* target = document().focusedElement();
if (target) {
RefPtr<CompositionEvent> event;
if (!m_compositionNode) {
if (!text.isEmpty()) {
target->dispatchEvent(CompositionEvent::create(eventNames().compositionstartEvent, document().domWindow(), selectedText()));
event = CompositionEvent::create(eventNames().compositionupdateEvent, document().domWindow(), text);
}
} else {
if (!text.isEmpty())
event = CompositionEvent::create(eventNames().compositionupdateEvent, document().domWindow(), text);
else
event = CompositionEvent::create(eventNames().compositionendEvent, document().domWindow(), text);
}
if (event)
target->dispatchEvent(*event);
}
if (text.isEmpty())
TypingCommand::deleteSelection(document(), TypingCommand::PreventSpellChecking);
m_compositionNode = nullptr;
m_customCompositionUnderlines.clear();
if (!text.isEmpty()) {
TypingCommand::insertText(document(), text, TypingCommand::SelectInsertedText | TypingCommand::PreventSpellChecking, TypingCommand::TextCompositionUpdate);
Position base = m_frame.selection().selection().base().downstream();
Position extent = m_frame.selection().selection().extent();
Node* baseNode = base.deprecatedNode();
unsigned baseOffset = base.deprecatedEditingOffset();
Node* extentNode = extent.deprecatedNode();
unsigned extentOffset = extent.deprecatedEditingOffset();
if (is<Text>(baseNode) && baseNode == extentNode && baseOffset + text.length() == extentOffset) {
m_compositionNode = downcast<Text>(baseNode);
m_compositionStart = baseOffset;
m_compositionEnd = extentOffset;
m_customCompositionUnderlines = underlines;
for (auto& underline : m_customCompositionUnderlines) {
underline.startOffset += baseOffset;
underline.endOffset += baseOffset;
}
if (baseNode->renderer())
baseNode->renderer()->repaint();
unsigned start = std::min(baseOffset + selectionStart, extentOffset);
unsigned end = std::min(std::max(start, baseOffset + selectionEnd), extentOffset);
RefPtr<Range> selectedRange = Range::create(baseNode->document(), baseNode, start, baseNode, end);
m_frame.selection().setSelectedRange(selectedRange.get(), DOWNSTREAM, false);
}
}
setIgnoreCompositionSelectionChange(false);
#if PLATFORM(IOS)
client()->stopDelayingAndCoalescingContentChangeNotifications();
#endif
}
void Editor::ignoreSpelling()
{
if (!client())
return;
RefPtr<Range> selectedRange = m_frame.selection().toNormalizedRange();
if (selectedRange)
document().markers().removeMarkers(selectedRange.get(), DocumentMarker::Spelling);
String text = selectedText();
ASSERT(text.length());
textChecker()->ignoreWordInSpellDocument(text);
}
void Editor::learnSpelling()
{
if (!client())
return;
RefPtr<Range> selectedRange = m_frame.selection().toNormalizedRange();
if (selectedRange)
document().markers().removeMarkers(selectedRange.get(), DocumentMarker::Spelling);
String text = selectedText();
ASSERT(text.length());
textChecker()->learnWord(text);
}
#if !PLATFORM(IOS)
void Editor::advanceToNextMisspelling(bool startBeforeSelection)
{
VisibleSelection selection(m_frame.selection().selection());
RefPtr<Range> spellingSearchRange(rangeOfContents(document()));
bool startedWithSelection = false;
if (selection.start().deprecatedNode()) {
startedWithSelection = true;
if (startBeforeSelection) {
VisiblePosition start(selection.visibleStart());
VisiblePosition oneBeforeStart = start.previous();
setStart(spellingSearchRange.get(), oneBeforeStart.isNotNull() ? oneBeforeStart : start);
} else
setStart(spellingSearchRange.get(), selection.visibleEnd());
}
Position position = spellingSearchRange->startPosition();
if (!isEditablePosition(position)) {
position = VisiblePosition(firstEditablePositionAfterPositionInRoot(position, document().documentElement())).deepEquivalent();
if (position.isNull())
return;
Position rangeCompliantPosition = position.parentAnchoredEquivalent();
if (rangeCompliantPosition.deprecatedNode())
spellingSearchRange->setStart(*rangeCompliantPosition.deprecatedNode(), rangeCompliantPosition.deprecatedEditingOffset(), IGNORE_EXCEPTION);
startedWithSelection = false; }
auto* topNode = highestEditableRoot(position);
if (topNode)
spellingSearchRange->setEnd(*topNode, lastOffsetForEditing(*topNode), IGNORE_EXCEPTION);
if (startedWithSelection) {
VisiblePosition oneBeforeStart = startVisiblePosition(spellingSearchRange.get(), DOWNSTREAM).previous();
if (oneBeforeStart.isNotNull())
setStart(spellingSearchRange.get(), endOfWord(oneBeforeStart));
}
if (spellingSearchRange->collapsed())
return;
if (!client())
return;
Node& searchEndNodeAfterWrap = spellingSearchRange->endContainer();
int searchEndOffsetAfterWrap = spellingSearchRange->endOffset();
int misspellingOffset = 0;
GrammarDetail grammarDetail;
int grammarPhraseOffset = 0;
RefPtr<Range> grammarSearchRange;
String badGrammarPhrase;
String misspelledWord;
bool isSpelling = true;
int foundOffset = 0;
String foundItem;
RefPtr<Range> firstMisspellingRange;
if (unifiedTextCheckerEnabled()) {
grammarSearchRange = spellingSearchRange->cloneRange();
foundItem = TextCheckingHelper(client(), spellingSearchRange).findFirstMisspellingOrBadGrammar(isGrammarCheckingEnabled(), isSpelling, foundOffset, grammarDetail);
if (isSpelling) {
misspelledWord = foundItem;
misspellingOffset = foundOffset;
} else {
badGrammarPhrase = foundItem;
grammarPhraseOffset = foundOffset;
}
} else {
misspelledWord = TextCheckingHelper(client(), spellingSearchRange).findFirstMisspelling(misspellingOffset, false, firstMisspellingRange);
#if USE(GRAMMAR_CHECKING)
grammarSearchRange = spellingSearchRange->cloneRange();
if (!misspelledWord.isEmpty()) {
CharacterIterator chars(*grammarSearchRange);
chars.advance(misspellingOffset);
grammarSearchRange->setEnd(chars.range()->startContainer(), chars.range()->startOffset(), IGNORE_EXCEPTION);
}
if (isGrammarCheckingEnabled())
badGrammarPhrase = TextCheckingHelper(client(), grammarSearchRange).findFirstBadGrammar(grammarDetail, grammarPhraseOffset, false);
#endif
}
if (startedWithSelection && !misspelledWord && !badGrammarPhrase) {
if (topNode)
spellingSearchRange->setStart(*topNode, 0, IGNORE_EXCEPTION);
spellingSearchRange->setEnd(searchEndNodeAfterWrap, searchEndOffsetAfterWrap, IGNORE_EXCEPTION);
if (unifiedTextCheckerEnabled()) {
grammarSearchRange = spellingSearchRange->cloneRange();
foundItem = TextCheckingHelper(client(), spellingSearchRange).findFirstMisspellingOrBadGrammar(isGrammarCheckingEnabled(), isSpelling, foundOffset, grammarDetail);
if (isSpelling) {
misspelledWord = foundItem;
misspellingOffset = foundOffset;
} else {
badGrammarPhrase = foundItem;
grammarPhraseOffset = foundOffset;
}
} else {
misspelledWord = TextCheckingHelper(client(), spellingSearchRange).findFirstMisspelling(misspellingOffset, false, firstMisspellingRange);
#if USE(GRAMMAR_CHECKING)
grammarSearchRange = spellingSearchRange->cloneRange();
if (!misspelledWord.isEmpty()) {
CharacterIterator chars(*grammarSearchRange);
chars.advance(misspellingOffset);
grammarSearchRange->setEnd(chars.range()->startContainer(), chars.range()->startOffset(), IGNORE_EXCEPTION);
}
if (isGrammarCheckingEnabled())
badGrammarPhrase = TextCheckingHelper(client(), grammarSearchRange).findFirstBadGrammar(grammarDetail, grammarPhraseOffset, false);
#endif
}
}
#if !USE(GRAMMAR_CHECKING)
ASSERT(badGrammarPhrase.isEmpty());
UNUSED_PARAM(grammarPhraseOffset);
#else
if (!badGrammarPhrase.isEmpty()) {
ASSERT(badGrammarPhrase.length() > 0);
ASSERT(grammarDetail.location != -1 && grammarDetail.length > 0);
RefPtr<Range> badGrammarRange = TextIterator::subrange(grammarSearchRange.get(), grammarPhraseOffset + grammarDetail.location, grammarDetail.length);
m_frame.selection().setSelection(VisibleSelection(*badGrammarRange, SEL_DEFAULT_AFFINITY));
m_frame.selection().revealSelection();
client()->updateSpellingUIWithGrammarString(badGrammarPhrase, grammarDetail);
document().markers().addMarker(badGrammarRange.get(), DocumentMarker::Grammar, grammarDetail.userDescription);
} else
#endif
if (!misspelledWord.isEmpty()) {
RefPtr<Range> misspellingRange = TextIterator::subrange(spellingSearchRange.get(), misspellingOffset, misspelledWord.length());
m_frame.selection().setSelection(VisibleSelection(*misspellingRange, DOWNSTREAM));
m_frame.selection().revealSelection();
client()->updateSpellingUIWithMisspelledWord(misspelledWord);
document().markers().addMarker(misspellingRange.get(), DocumentMarker::Spelling);
}
}
#endif // !PLATFORM(IOS)
String Editor::misspelledWordAtCaretOrRange(Node* clickedNode) const
{
if (!isContinuousSpellCheckingEnabled() || !clickedNode || !isSpellCheckingEnabledFor(clickedNode))
return String();
VisibleSelection selection = m_frame.selection().selection();
if (!selection.isContentEditable() || selection.isNone())
return String();
VisibleSelection wordSelection(selection.base());
wordSelection.expandUsingGranularity(WordGranularity);
RefPtr<Range> wordRange = wordSelection.toNormalizedRange();
if (selection.isRange() && !areRangesEqual(wordRange.get(), selection.toNormalizedRange().get()))
return String();
String word = wordRange->text();
if (word.isEmpty() || !client())
return String();
int wordLength = word.length();
int misspellingLocation = -1;
int misspellingLength = 0;
textChecker()->checkSpellingOfString(word, &misspellingLocation, &misspellingLength);
return misspellingLength == wordLength ? word : String();
}
String Editor::misspelledSelectionString() const
{
String selectedString = selectedText();
int length = selectedString.length();
if (!length || !client())
return String();
int misspellingLocation = -1;
int misspellingLength = 0;
textChecker()->checkSpellingOfString(selectedString, &misspellingLocation, &misspellingLength);
if (misspellingLength != length)
return String();
client()->updateSpellingUIWithMisspelledWord(selectedString);
return selectedString;
}
bool Editor::isSelectionUngrammatical()
{
#if USE(GRAMMAR_CHECKING)
RefPtr<Range> range = m_frame.selection().toNormalizedRange();
if (!range)
return false;
return TextCheckingHelper(client(), range).isUngrammatical();
#else
return false;
#endif
}
Vector<String> Editor::guessesForMisspelledWord(const String& word) const
{
ASSERT(word.length());
Vector<String> guesses;
if (client())
textChecker()->getGuessesForWord(word, String(), m_frame.selection().selection(), guesses);
return guesses;
}
Vector<String> Editor::guessesForMisspelledOrUngrammatical(bool& misspelled, bool& ungrammatical)
{
if (unifiedTextCheckerEnabled()) {
RefPtr<Range> range;
VisibleSelection selection = m_frame.selection().selection();
if (selection.isCaret() && behavior().shouldAllowSpellingSuggestionsWithoutSelection()) {
VisibleSelection wordSelection = VisibleSelection(selection.base());
wordSelection.expandUsingGranularity(WordGranularity);
range = wordSelection.toNormalizedRange();
} else
range = selection.toNormalizedRange();
if (!range)
return Vector<String>();
return TextCheckingHelper(client(), range).guessesForMisspelledOrUngrammaticalRange(isGrammarCheckingEnabled(), misspelled, ungrammatical);
}
String misspelledWord = behavior().shouldAllowSpellingSuggestionsWithoutSelection() ? misspelledWordAtCaretOrRange(document().focusedElement()) : misspelledSelectionString();
misspelled = !misspelledWord.isEmpty();
ungrammatical = false;
if (misspelled)
return guessesForMisspelledWord(misspelledWord);
return Vector<String>();
}
void Editor::showSpellingGuessPanel()
{
if (!client()) {
LOG_ERROR("No NSSpellChecker");
return;
}
if (client()->spellingUIIsShowing()) {
client()->showSpellingUI(false);
return;
}
#if !PLATFORM(IOS)
advanceToNextMisspelling(true);
#endif
client()->showSpellingUI(true);
}
bool Editor::spellingPanelIsShowing()
{
if (!client())
return false;
return client()->spellingUIIsShowing();
}
void Editor::clearMisspellingsAndBadGrammar(const VisibleSelection &movingSelection)
{
RefPtr<Range> selectedRange = movingSelection.toNormalizedRange();
if (selectedRange) {
document().markers().removeMarkers(selectedRange.get(), DocumentMarker::Spelling);
document().markers().removeMarkers(selectedRange.get(), DocumentMarker::Grammar);
}
}
void Editor::markMisspellingsAndBadGrammar(const VisibleSelection &movingSelection)
{
markMisspellingsAndBadGrammar(movingSelection, isContinuousSpellCheckingEnabled() && isGrammarCheckingEnabled(), movingSelection);
}
void Editor::markMisspellingsAfterTypingToWord(const VisiblePosition &wordStart, const VisibleSelection& selectionAfterTyping, bool doReplacement)
{
#if PLATFORM(IOS)
UNUSED_PARAM(selectionAfterTyping);
UNUSED_PARAM(doReplacement);
TextCheckingTypeMask textCheckingOptions = 0;
if (isContinuousSpellCheckingEnabled())
textCheckingOptions |= TextCheckingTypeSpelling;
if (!(textCheckingOptions & TextCheckingTypeSpelling))
return;
VisibleSelection adjacentWords = VisibleSelection(startOfWord(wordStart, LeftWordIfOnBoundary), endOfWord(wordStart, RightWordIfOnBoundary));
markAllMisspellingsAndBadGrammarInRanges(textCheckingOptions, adjacentWords.toNormalizedRange().get(), adjacentWords.toNormalizedRange().get());
#else
#if !USE(AUTOMATIC_TEXT_REPLACEMENT)
UNUSED_PARAM(doReplacement);
#endif
if (unifiedTextCheckerEnabled()) {
m_alternativeTextController->applyPendingCorrection(selectionAfterTyping);
TextCheckingTypeMask textCheckingOptions = 0;
if (isContinuousSpellCheckingEnabled())
textCheckingOptions |= TextCheckingTypeSpelling;
#if USE(AUTOMATIC_TEXT_REPLACEMENT)
if (doReplacement
&& (isAutomaticQuoteSubstitutionEnabled()
|| isAutomaticLinkDetectionEnabled()
|| isAutomaticDashSubstitutionEnabled()
|| isAutomaticTextReplacementEnabled()
|| ((textCheckingOptions & TextCheckingTypeSpelling) && isAutomaticSpellingCorrectionEnabled())))
textCheckingOptions |= TextCheckingTypeReplacement;
#endif
if (!(textCheckingOptions & (TextCheckingTypeSpelling | TextCheckingTypeReplacement)))
return;
if (isGrammarCheckingEnabled())
textCheckingOptions |= TextCheckingTypeGrammar;
VisibleSelection adjacentWords = VisibleSelection(startOfWord(wordStart, LeftWordIfOnBoundary), endOfWord(wordStart, RightWordIfOnBoundary));
if (textCheckingOptions & TextCheckingTypeGrammar) {
VisibleSelection selectedSentence = VisibleSelection(startOfSentence(wordStart), endOfSentence(wordStart));
markAllMisspellingsAndBadGrammarInRanges(textCheckingOptions, adjacentWords.toNormalizedRange().get(), selectedSentence.toNormalizedRange().get());
} else
markAllMisspellingsAndBadGrammarInRanges(textCheckingOptions, adjacentWords.toNormalizedRange().get(), adjacentWords.toNormalizedRange().get());
return;
}
if (!isContinuousSpellCheckingEnabled())
return;
RefPtr<Range> misspellingRange;
markMisspellings(VisibleSelection(startOfWord(wordStart, LeftWordIfOnBoundary), endOfWord(wordStart, RightWordIfOnBoundary)), misspellingRange);
if (!misspellingRange)
return;
const String misspelledWord = plainText(misspellingRange.get());
String autocorrectedString = textChecker()->getAutoCorrectSuggestionForMisspelledWord(misspelledWord);
if (!autocorrectedString.isEmpty()) {
VisibleSelection newSelection(*misspellingRange, DOWNSTREAM);
if (newSelection != m_frame.selection().selection()) {
if (!m_frame.selection().shouldChangeSelection(newSelection))
return;
m_frame.selection().setSelection(newSelection);
}
if (!m_frame.editor().shouldInsertText(autocorrectedString, misspellingRange.get(), EditorInsertActionTyped))
return;
m_frame.editor().replaceSelectionWithText(autocorrectedString, false, false, EditActionInsert);
m_frame.selection().moveTo(m_frame.selection().selection().end());
m_frame.selection().modify(FrameSelection::AlterationMove, DirectionForward, CharacterGranularity);
}
if (!isGrammarCheckingEnabled())
return;
markBadGrammar(VisibleSelection(startOfSentence(wordStart), endOfSentence(wordStart)));
#endif
}
void Editor::markMisspellingsOrBadGrammar(const VisibleSelection& selection, bool checkSpelling, RefPtr<Range>& firstMisspellingRange)
{
#if !PLATFORM(IOS)
if (!isContinuousSpellCheckingEnabled())
return;
RefPtr<Range> searchRange(selection.toNormalizedRange());
if (!searchRange)
return;
Node& editableNode = searchRange->startContainer();
if (!editableNode.hasEditableStyle())
return;
if (!isSpellCheckingEnabledFor(&editableNode))
return;
if (!client())
return;
TextCheckingHelper checker(client(), searchRange);
if (checkSpelling)
checker.markAllMisspellings(firstMisspellingRange);
else {
#if USE(GRAMMAR_CHECKING)
if (isGrammarCheckingEnabled())
checker.markAllBadGrammar();
#else
ASSERT_NOT_REACHED();
#endif
}
#else
UNUSED_PARAM(selection);
UNUSED_PARAM(checkSpelling);
UNUSED_PARAM(firstMisspellingRange);
#endif // !PLATFORM(IOS)
}
bool Editor::isSpellCheckingEnabledFor(Node* node) const
{
if (!node)
return false;
Element* element = is<Element>(*node) ? downcast<Element>(node) : node->parentElement();
if (!element)
return false;
if (element->isInUserAgentShadowTree()) {
if (HTMLTextFormControlElement* textControl = enclosingTextFormControl(firstPositionInOrBeforeNode(element)))
return textControl->isSpellCheckingEnabled();
}
return element->isSpellCheckingEnabled();
}
bool Editor::isSpellCheckingEnabledInFocusedNode() const
{
return isSpellCheckingEnabledFor(m_frame.selection().selection().start().deprecatedNode());
}
void Editor::markMisspellings(const VisibleSelection& selection, RefPtr<Range>& firstMisspellingRange)
{
markMisspellingsOrBadGrammar(selection, true, firstMisspellingRange);
}
void Editor::markBadGrammar(const VisibleSelection& selection)
{
#if USE(GRAMMAR_CHECKING)
RefPtr<Range> firstMisspellingRange;
markMisspellingsOrBadGrammar(selection, false, firstMisspellingRange);
#else
ASSERT_NOT_REACHED();
#endif
}
void Editor::markAllMisspellingsAndBadGrammarInRanges(TextCheckingTypeMask textCheckingOptions, Range* spellingRange, Range* grammarRange)
{
ASSERT(unifiedTextCheckerEnabled());
ASSERT(!m_alternativeTextController->hasPendingCorrection());
bool shouldMarkGrammar = textCheckingOptions & TextCheckingTypeGrammar;
bool shouldShowCorrectionPanel = textCheckingOptions & TextCheckingTypeShowCorrectionPanel;
if (!client() || !spellingRange || (shouldMarkGrammar && !grammarRange))
return;
Node& editableNode = spellingRange->startContainer();
if (!editableNode.hasEditableStyle())
return;
if (!isSpellCheckingEnabledFor(&editableNode))
return;
Range* rangeToCheck = shouldMarkGrammar ? grammarRange : spellingRange;
TextCheckingParagraph paragraphToCheck(rangeToCheck);
if (paragraphToCheck.isEmpty())
return;
RefPtr<Range> paragraphRange = paragraphToCheck.paragraphRange();
bool asynchronous = m_frame.settings().asynchronousSpellCheckingEnabled() && !shouldShowCorrectionPanel;
auto request = SpellCheckRequest::create(resolveTextCheckingTypeMask(textCheckingOptions), TextCheckingProcessIncremental, asynchronous ? paragraphRange : rangeToCheck, paragraphRange);
if (asynchronous) {
m_spellChecker->requestCheckingFor(WTFMove(request));
return;
}
Vector<TextCheckingResult> results;
checkTextOfParagraph(*textChecker(), paragraphToCheck.text(), resolveTextCheckingTypeMask(textCheckingOptions), results, m_frame.selection().selection());
markAndReplaceFor(WTFMove(request), results);
}
static bool isAutomaticTextReplacementType(TextCheckingType type)
{
switch (type) {
case TextCheckingTypeNone:
case TextCheckingTypeSpelling:
case TextCheckingTypeGrammar:
return false;
case TextCheckingTypeLink:
case TextCheckingTypeQuote:
case TextCheckingTypeDash:
case TextCheckingTypeReplacement:
case TextCheckingTypeCorrection:
case TextCheckingTypeShowCorrectionPanel:
return true;
}
ASSERT_NOT_REACHED();
return false;
}
static void correctSpellcheckingPreservingTextCheckingParagraph(TextCheckingParagraph& paragraph, PassRefPtr<Range> rangeToReplace, const String& replacement, int resultLocation, int resultLength)
{
ContainerNode* scope = downcast<ContainerNode>(paragraph.paragraphRange()->startContainer().rootNode());
size_t paragraphLocation;
size_t paragraphLength;
TextIterator::getLocationAndLengthFromRange(scope, paragraph.paragraphRange().get(), paragraphLocation, paragraphLength);
applyCommand(SpellingCorrectionCommand::create(rangeToReplace, replacement));
RefPtr<Range> newParagraphRange = TextIterator::rangeFromLocationAndLength(scope, paragraphLocation, paragraphLength + replacement.length() - resultLength);
paragraph = TextCheckingParagraph(TextIterator::subrange(newParagraphRange.get(), resultLocation, replacement.length()), newParagraphRange);
}
void Editor::markAndReplaceFor(PassRefPtr<SpellCheckRequest> request, const Vector<TextCheckingResult>& results)
{
ASSERT(request);
TextCheckingTypeMask textCheckingOptions = request->data().mask();
TextCheckingParagraph paragraph(request->checkingRange(), request->paragraphRange());
const bool shouldMarkSpelling = textCheckingOptions & TextCheckingTypeSpelling;
const bool shouldMarkGrammar = textCheckingOptions & TextCheckingTypeGrammar;
const bool shouldMarkLink = textCheckingOptions & TextCheckingTypeLink;
const bool shouldPerformReplacement = textCheckingOptions & (TextCheckingTypeQuote | TextCheckingTypeDash | TextCheckingTypeReplacement);
const bool shouldShowCorrectionPanel = textCheckingOptions & TextCheckingTypeShowCorrectionPanel;
const bool shouldCheckForCorrection = shouldShowCorrectionPanel || (textCheckingOptions & TextCheckingTypeCorrection);
#if !USE(AUTOCORRECTION_PANEL)
ASSERT(!shouldShowCorrectionPanel);
#endif
int selectionOffset = 0;
bool useAmbiguousBoundaryOffset = false;
bool selectionChanged = false;
bool restoreSelectionAfterChange = false;
bool adjustSelectionForParagraphBoundaries = false;
if (shouldPerformReplacement || shouldMarkSpelling || shouldCheckForCorrection) {
if (m_frame.selection().selection().selectionType() == VisibleSelection::CaretSelection) {
Position caretPosition = m_frame.selection().selection().end();
selectionOffset = paragraph.offsetTo(caretPosition, ASSERT_NO_EXCEPTION);
restoreSelectionAfterChange = true;
if (selectionOffset > 0 && (selectionOffset > paragraph.textLength() || paragraph.textCharAt(selectionOffset - 1) == newlineCharacter))
adjustSelectionForParagraphBoundaries = true;
if (selectionOffset > 0 && selectionOffset <= paragraph.textLength() && isAmbiguousBoundaryCharacter(paragraph.textCharAt(selectionOffset - 1)))
useAmbiguousBoundaryOffset = true;
}
}
int offsetDueToReplacement = 0;
for (unsigned i = 0; i < results.size(); i++) {
const int spellingRangeEndOffset = paragraph.checkingEnd() + offsetDueToReplacement;
const TextCheckingType resultType = results[i].type;
const int resultLocation = results[i].location + offsetDueToReplacement;
const int resultLength = results[i].length;
const int resultEndLocation = resultLocation + resultLength;
const String& replacement = results[i].replacement;
const bool resultEndsAtAmbiguousBoundary = useAmbiguousBoundaryOffset && selectionOffset - 1 <= resultEndLocation;
if (shouldMarkSpelling && !shouldShowCorrectionPanel && resultType == TextCheckingTypeSpelling
&& resultLocation >= paragraph.checkingStart() && resultEndLocation <= spellingRangeEndOffset && !resultEndsAtAmbiguousBoundary) {
ASSERT(resultLength > 0 && resultLocation >= 0);
RefPtr<Range> misspellingRange = paragraph.subrange(resultLocation, resultLength);
if (!m_alternativeTextController->isSpellingMarkerAllowed(misspellingRange))
continue;
misspellingRange->startContainer().document().markers().addMarker(misspellingRange.get(), DocumentMarker::Spelling, replacement);
} else if (shouldMarkGrammar && resultType == TextCheckingTypeGrammar && paragraph.checkingRangeCovers(resultLocation, resultLength)) {
ASSERT(resultLength > 0 && resultLocation >= 0);
for (auto& detail : results[i].details) {
ASSERT(detail.length > 0 && detail.location >= 0);
if (paragraph.checkingRangeCovers(resultLocation + detail.location, detail.length)) {
RefPtr<Range> badGrammarRange = paragraph.subrange(resultLocation + detail.location, detail.length);
badGrammarRange->startContainer().document().markers().addMarker(badGrammarRange.get(), DocumentMarker::Grammar, detail.userDescription);
}
}
} else if (resultEndLocation <= spellingRangeEndOffset && resultEndLocation >= paragraph.checkingStart()
&& isAutomaticTextReplacementType(resultType)) {
ASSERT(resultLength > 0 && resultLocation >= 0);
if (shouldShowCorrectionPanel && (resultEndLocation < spellingRangeEndOffset
|| !(resultType & (TextCheckingTypeReplacement | TextCheckingTypeCorrection))))
continue;
bool doReplacement = replacement.length() > 0 && !resultEndsAtAmbiguousBoundary;
RefPtr<Range> rangeToReplace = paragraph.subrange(resultLocation, resultLength);
if (resultType == TextCheckingTypeLink && selectionOffset != resultEndLocation + 1)
continue;
if (!(shouldPerformReplacement || shouldCheckForCorrection || shouldMarkLink) || !doReplacement)
continue;
String replacedString = plainText(rangeToReplace.get());
const bool existingMarkersPermitReplacement = m_alternativeTextController->processMarkersOnTextToBeReplacedByResult(&results[i], rangeToReplace.get(), replacedString);
if (!existingMarkersPermitReplacement)
continue;
if (shouldShowCorrectionPanel) {
if (resultEndLocation == spellingRangeEndOffset) {
m_alternativeTextController->show(rangeToReplace, replacement);
break;
}
continue;
}
VisibleSelection selectionToReplace(*rangeToReplace, DOWNSTREAM);
if (selectionToReplace != m_frame.selection().selection()) {
if (!m_frame.selection().shouldChangeSelection(selectionToReplace))
continue;
}
if (resultType == TextCheckingTypeLink) {
m_frame.selection().setSelection(selectionToReplace);
selectionChanged = true;
restoreSelectionAfterChange = false;
if (canEditRichly())
applyCommand(CreateLinkCommand::create(document(), replacement));
} else if (canEdit() && shouldInsertText(replacement, rangeToReplace.get(), EditorInsertActionTyped)) {
correctSpellcheckingPreservingTextCheckingParagraph(paragraph, rangeToReplace, replacement, resultLocation, resultLength);
if (AXObjectCache* cache = document().existingAXObjectCache()) {
if (Element* root = m_frame.selection().selection().rootEditableElement())
cache->postNotification(root, AXObjectCache::AXAutocorrectionOccured);
}
while (i + 1 < results.size() && results[i + 1].location + offsetDueToReplacement <= resultLocation)
i++;
selectionChanged = true;
offsetDueToReplacement += replacement.length() - resultLength;
if (resultLocation < selectionOffset)
selectionOffset += replacement.length() - resultLength;
if (resultType == TextCheckingTypeCorrection)
m_alternativeTextController->markCorrection(paragraph.subrange(resultLocation, replacement.length()), replacedString);
}
}
}
if (selectionChanged) {
TextCheckingParagraph extendedParagraph(paragraph);
extendedParagraph.expandRangeToNextEnd();
if (restoreSelectionAfterChange && selectionOffset >= 0 && selectionOffset <= extendedParagraph.rangeLength()) {
RefPtr<Range> selectionRange = extendedParagraph.subrange(0, selectionOffset);
m_frame.selection().moveTo(selectionRange->endPosition(), DOWNSTREAM);
if (adjustSelectionForParagraphBoundaries)
m_frame.selection().modify(FrameSelection::AlterationMove, DirectionForward, CharacterGranularity);
} else {
m_frame.selection().moveTo(m_frame.selection().selection().end());
m_frame.selection().modify(FrameSelection::AlterationMove, DirectionForward, CharacterGranularity);
}
}
}
void Editor::changeBackToReplacedString(const String& replacedString)
{
#if !PLATFORM(IOS)
ASSERT(unifiedTextCheckerEnabled());
if (replacedString.isEmpty())
return;
RefPtr<Range> selection = selectedRange();
if (!shouldInsertText(replacedString, selection.get(), EditorInsertActionPasted))
return;
m_alternativeTextController->recordAutocorrectionResponseReversed(replacedString, selection);
TextCheckingParagraph paragraph(selection);
replaceSelectionWithText(replacedString, false, false, EditActionInsert);
RefPtr<Range> changedRange = paragraph.subrange(paragraph.checkingStart(), replacedString.length());
changedRange->startContainer().document().markers().addMarker(changedRange.get(), DocumentMarker::Replacement, String());
m_alternativeTextController->markReversed(changedRange.get());
#else
ASSERT_NOT_REACHED();
UNUSED_PARAM(replacedString);
#endif // !PLATFORM(IOS)
}
void Editor::markMisspellingsAndBadGrammar(const VisibleSelection& spellingSelection, bool markGrammar, const VisibleSelection& grammarSelection)
{
if (unifiedTextCheckerEnabled()) {
if (!isContinuousSpellCheckingEnabled())
return;
TextCheckingTypeMask textCheckingOptions = TextCheckingTypeSpelling;
if (markGrammar && isGrammarCheckingEnabled())
textCheckingOptions |= TextCheckingTypeGrammar;
markAllMisspellingsAndBadGrammarInRanges(textCheckingOptions, spellingSelection.toNormalizedRange().get(), grammarSelection.toNormalizedRange().get());
return;
}
RefPtr<Range> firstMisspellingRange;
markMisspellings(spellingSelection, firstMisspellingRange);
if (markGrammar)
markBadGrammar(grammarSelection);
}
void Editor::unappliedSpellCorrection(const VisibleSelection& selectionOfCorrected, const String& corrected, const String& correction)
{
m_alternativeTextController->respondToUnappliedSpellCorrection(selectionOfCorrected, corrected, correction);
}
void Editor::updateMarkersForWordsAffectedByEditing(bool doNotRemoveIfSelectionAtWordBoundary)
{
if (!document().markers().hasMarkers())
return;
if (!m_alternativeTextController->shouldRemoveMarkersUponEditing() && (!textChecker() || textChecker()->shouldEraseMarkersAfterChangeSelection(TextCheckingTypeSpelling)))
return;
VisiblePosition startOfSelection = m_frame.selection().selection().start();
VisiblePosition endOfSelection = m_frame.selection().selection().end();
if (startOfSelection.isNull())
return;
VisiblePosition startOfFirstWord = startOfWord(startOfSelection, LeftWordIfOnBoundary);
VisiblePosition endOfFirstWord = endOfWord(startOfSelection, LeftWordIfOnBoundary);
VisiblePosition startOfLastWord = startOfWord(endOfSelection, RightWordIfOnBoundary);
VisiblePosition endOfLastWord = endOfWord(endOfSelection, RightWordIfOnBoundary);
if (startOfFirstWord.isNull()) {
startOfFirstWord = startOfWord(startOfSelection, RightWordIfOnBoundary);
endOfFirstWord = endOfWord(startOfSelection, RightWordIfOnBoundary);
}
if (endOfLastWord.isNull()) {
startOfLastWord = startOfWord(endOfSelection, LeftWordIfOnBoundary);
endOfLastWord = endOfWord(endOfSelection, LeftWordIfOnBoundary);
}
if (doNotRemoveIfSelectionAtWordBoundary && endOfFirstWord == startOfSelection) {
startOfFirstWord = nextWordPosition(startOfFirstWord);
endOfFirstWord = endOfWord(startOfFirstWord, RightWordIfOnBoundary);
if (startOfFirstWord == endOfSelection)
return;
}
if (doNotRemoveIfSelectionAtWordBoundary && startOfLastWord == endOfSelection) {
startOfLastWord = previousWordPosition(startOfLastWord);
endOfLastWord = endOfWord(startOfLastWord, RightWordIfOnBoundary);
if (endOfLastWord == startOfSelection)
return;
}
if (startOfFirstWord.isNull() || endOfFirstWord.isNull() || startOfLastWord.isNull() || endOfLastWord.isNull())
return;
RefPtr<Range> wordRange = Range::create(document(), startOfFirstWord.deepEquivalent(), endOfLastWord.deepEquivalent());
Vector<RenderedDocumentMarker*> markers = document().markers().markersInRange(wordRange.get(), DocumentMarker::DictationAlternatives);
for (auto* marker : markers)
m_alternativeTextController->removeDictationAlternativesForMarker(marker);
#if PLATFORM(IOS)
document().markers().removeMarkers(wordRange.get(), DocumentMarker::Spelling | DocumentMarker::CorrectionIndicator | DocumentMarker::SpellCheckingExemption | DocumentMarker::DictationAlternatives | DocumentMarker::DictationPhraseWithAlternatives, DocumentMarkerController::RemovePartiallyOverlappingMarker);
#else
document().markers().removeMarkers(wordRange.get(), DocumentMarker::Spelling | DocumentMarker::Grammar | DocumentMarker::CorrectionIndicator | DocumentMarker::SpellCheckingExemption | DocumentMarker::DictationAlternatives, DocumentMarkerController::RemovePartiallyOverlappingMarker);
#endif
document().markers().clearDescriptionOnMarkersIntersectingRange(wordRange.get(), DocumentMarker::Replacement);
}
void Editor::deletedAutocorrectionAtPosition(const Position& position, const String& originalString)
{
m_alternativeTextController->deletedAutocorrectionAtPosition(position, originalString);
}
PassRefPtr<Range> Editor::rangeForPoint(const IntPoint& windowPoint)
{
Document* document = m_frame.documentAtPoint(windowPoint);
if (!document)
return 0;
Frame* frame = document->frame();
ASSERT(frame);
FrameView* frameView = frame->view();
if (!frameView)
return 0;
IntPoint framePoint = frameView->windowToContents(windowPoint);
VisibleSelection selection(frame->visiblePositionForPoint(framePoint));
return selection.toNormalizedRange();
}
void Editor::revealSelectionAfterEditingOperation(const ScrollAlignment& alignment, RevealExtentOption revealExtentOption)
{
if (m_ignoreCompositionSelectionChange)
return;
#if PLATFORM(IOS)
SelectionRevealMode revealMode = SelectionRevealMode::RevealUpToMainFrame;
#else
SelectionRevealMode revealMode = SelectionRevealMode::Reveal;
#endif
m_frame.selection().revealSelection(revealMode, alignment, revealExtentOption);
}
void Editor::setIgnoreCompositionSelectionChange(bool ignore, RevealSelection shouldRevealExistingSelection)
{
if (m_ignoreCompositionSelectionChange == ignore)
return;
m_ignoreCompositionSelectionChange = ignore;
#if PLATFORM(IOS)
if (!ignore)
respondToChangedSelection(m_frame.selection().selection(), 0);
#endif
if (!ignore && shouldRevealExistingSelection == RevealSelection::Yes)
revealSelectionAfterEditingOperation(ScrollAlignment::alignToEdgeIfNeeded, RevealExtent);
}
RefPtr<Range> Editor::compositionRange() const
{
if (!m_compositionNode)
return nullptr;
unsigned length = m_compositionNode->length();
unsigned start = std::min(m_compositionStart, length);
unsigned end = std::min(std::max(start, m_compositionEnd), length);
if (start >= end)
return nullptr;
return Range::create(m_compositionNode->document(), m_compositionNode.get(), start, m_compositionNode.get(), end);
}
bool Editor::getCompositionSelection(unsigned& selectionStart, unsigned& selectionEnd) const
{
if (!m_compositionNode)
return false;
const VisibleSelection& selection = m_frame.selection().selection();
Position start = selection.start();
if (start.deprecatedNode() != m_compositionNode)
return false;
Position end = selection.end();
if (end.deprecatedNode() != m_compositionNode)
return false;
if (static_cast<unsigned>(start.deprecatedEditingOffset()) < m_compositionStart)
return false;
if (static_cast<unsigned>(end.deprecatedEditingOffset()) > m_compositionEnd)
return false;
selectionStart = start.deprecatedEditingOffset() - m_compositionStart;
selectionEnd = start.deprecatedEditingOffset() - m_compositionEnd;
return true;
}
void Editor::transpose()
{
if (!canEdit())
return;
VisibleSelection selection = m_frame.selection().selection();
if (!selection.isCaret())
return;
VisiblePosition caret = selection.visibleStart();
VisiblePosition next = isEndOfParagraph(caret) ? caret : caret.next();
VisiblePosition previous = next.previous();
if (next == previous)
return;
previous = previous.previous();
if (!inSameParagraph(next, previous))
return;
RefPtr<Range> range = makeRange(previous, next);
if (!range)
return;
VisibleSelection newSelection(*range, DOWNSTREAM);
String text = plainText(range.get());
if (text.length() != 2)
return;
String transposed = text.right(1) + text.left(1);
if (newSelection != m_frame.selection().selection()) {
if (!m_frame.selection().shouldChangeSelection(newSelection))
return;
m_frame.selection().setSelection(newSelection);
}
if (!shouldInsertText(transposed, range.get(), EditorInsertActionTyped))
return;
replaceSelectionWithText(transposed, false, false, EditActionInsert);
}
void Editor::addRangeToKillRing(const Range& range, KillRingInsertionMode mode)
{
addTextToKillRing(plainText(&range), mode);
}
void Editor::addTextToKillRing(const String& text, KillRingInsertionMode mode)
{
if (m_shouldStartNewKillRingSequence)
killRing().startNewSequence();
m_shouldStartNewKillRingSequence = false;
switch (mode) {
case KillRingInsertionMode::PrependText:
killRing().prepend(text);
break;
case KillRingInsertionMode::AppendText:
killRing().append(text);
break;
}
}
void Editor::startAlternativeTextUITimer()
{
m_alternativeTextController->startAlternativeTextUITimer(AlternativeTextTypeCorrection);
}
void Editor::handleAlternativeTextUIResult(const String& correction)
{
m_alternativeTextController->handleAlternativeTextUIResult(correction);
}
void Editor::dismissCorrectionPanelAsIgnored()
{
m_alternativeTextController->dismiss(ReasonForDismissingAlternativeTextIgnored);
}
void Editor::changeSelectionAfterCommand(const VisibleSelection& newSelection, FrameSelection::SetSelectionOptions options)
{
if (newSelection.start().isOrphan() || newSelection.end().isOrphan())
return;
bool selectionDidNotChangeDOMPosition = newSelection == m_frame.selection().selection();
if (selectionDidNotChangeDOMPosition || m_frame.selection().shouldChangeSelection(newSelection))
m_frame.selection().setSelection(newSelection, options);
#if PLATFORM(IOS)
if (m_ignoreCompositionSelectionChange)
return;
#endif
if (selectionDidNotChangeDOMPosition && client())
client()->respondToChangedSelection(&m_frame);
}
String Editor::selectedText() const
{
return selectedText(TextIteratorDefaultBehavior);
}
String Editor::selectedTextForDataTransfer() const
{
if (m_frame.settings().selectionIncludesAltImageText())
return selectedText(TextIteratorEmitsImageAltText);
return selectedText();
}
String Editor::selectedText(TextIteratorBehavior behavior) const
{
return plainText(m_frame.selection().toNormalizedRange().get(), behavior).replaceWithLiteral('\0', "");
}
static inline void collapseCaretWidth(IntRect& rect)
{
if (rect.width() == caretWidth)
rect.setWidth(0);
else if (rect.height() == caretWidth)
rect.setHeight(0);
}
IntRect Editor::firstRectForRange(Range* range) const
{
VisiblePosition startVisiblePosition(range->startPosition(), DOWNSTREAM);
if (range->collapsed()) {
IntRect startCaretRect = RenderedPosition(startVisiblePosition).absoluteRect();
collapseCaretWidth(startCaretRect);
return startCaretRect;
}
VisiblePosition endVisiblePosition(range->endPosition(), UPSTREAM);
if (inSameLine(startVisiblePosition, endVisiblePosition))
return enclosingIntRect(RenderObject::absoluteBoundingBoxRectForRange(range));
LayoutUnit extraWidthToEndOfLine = 0;
IntRect startCaretRect = RenderedPosition(startVisiblePosition).absoluteRect(&extraWidthToEndOfLine);
if (startCaretRect == IntRect())
return IntRect();
bool textIsHorizontal = startCaretRect.width() == caretWidth;
return textIsHorizontal ?
IntRect(startCaretRect.x(),
startCaretRect.y(),
startCaretRect.width() + extraWidthToEndOfLine,
startCaretRect.height()) :
IntRect(startCaretRect.x(),
startCaretRect.y(),
startCaretRect.width(),
startCaretRect.height() + extraWidthToEndOfLine);
}
bool Editor::shouldChangeSelection(const VisibleSelection& oldSelection, const VisibleSelection& newSelection, EAffinity affinity, bool stillSelecting) const
{
#if PLATFORM(IOS)
if (m_frame.selectionChangeCallbacksDisabled())
return true;
#endif
return client() && client()->shouldChangeSelectedRange(oldSelection.toNormalizedRange().get(), newSelection.toNormalizedRange().get(), affinity, stillSelecting);
}
void Editor::computeAndSetTypingStyle(EditingStyle& style, EditAction editingAction)
{
if (style.isEmpty()) {
m_frame.selection().clearTypingStyle();
return;
}
RefPtr<EditingStyle> typingStyle;
if (auto existingTypingStyle = m_frame.selection().typingStyle())
typingStyle = existingTypingStyle->copy();
else
typingStyle = EditingStyle::create();
typingStyle->overrideTypingStyleAt(style, m_frame.selection().selection().visibleStart().deepEquivalent());
RefPtr<EditingStyle> blockStyle = typingStyle->extractAndRemoveBlockProperties();
if (!blockStyle->isEmpty())
applyCommand(ApplyStyleCommand::create(document(), blockStyle.get(), editingAction));
m_frame.selection().setTypingStyle(typingStyle);
}
void Editor::computeAndSetTypingStyle(StyleProperties& properties, EditAction editingAction)
{
return computeAndSetTypingStyle(EditingStyle::create(&properties), editingAction);
}
void Editor::textFieldDidBeginEditing(Element* e)
{
if (client())
client()->textFieldDidBeginEditing(e);
}
void Editor::textFieldDidEndEditing(Element* e)
{
dismissCorrectionPanelAsIgnored();
if (client())
client()->textFieldDidEndEditing(e);
}
void Editor::textDidChangeInTextField(Element* e)
{
if (client())
client()->textDidChangeInTextField(e);
}
bool Editor::doTextFieldCommandFromEvent(Element* e, KeyboardEvent* ke)
{
if (client())
return client()->doTextFieldCommandFromEvent(e, ke);
return false;
}
void Editor::textWillBeDeletedInTextField(Element* input)
{
if (client())
client()->textWillBeDeletedInTextField(input);
}
void Editor::textDidChangeInTextArea(Element* e)
{
if (client())
client()->textDidChangeInTextArea(e);
}
void Editor::applyEditingStyleToBodyElement() const
{
auto collection = document().getElementsByTagName(HTMLNames::bodyTag.localName());
unsigned length = collection->length();
for (unsigned i = 0; i < length; ++i)
applyEditingStyleToElement(collection->item(i));
}
void Editor::applyEditingStyleToElement(Element* element) const
{
ASSERT(!element || is<StyledElement>(*element));
if (!is<StyledElement>(element))
return;
CSSStyleDeclaration* style = downcast<StyledElement>(*element).cssomStyle();
style->setPropertyInternal(CSSPropertyWordWrap, "break-word", false, IGNORE_EXCEPTION);
style->setPropertyInternal(CSSPropertyWebkitNbspMode, "space", false, IGNORE_EXCEPTION);
style->setPropertyInternal(CSSPropertyWebkitLineBreak, "after-white-space", false, IGNORE_EXCEPTION);
}
bool Editor::findString(const String& target, FindOptions options)
{
VisibleSelection selection = m_frame.selection().selection();
RefPtr<Range> resultRange = rangeOfString(target, selection.firstRange().get(), options);
if (!resultRange)
return false;
m_frame.selection().setSelection(VisibleSelection(*resultRange, DOWNSTREAM));
if (!(options & DoNotRevealSelection))
m_frame.selection().revealSelection();
return true;
}
RefPtr<Range> Editor::findStringAndScrollToVisible(const String& target, Range* previousMatch, FindOptions options)
{
RefPtr<Range> nextMatch = rangeOfString(target, previousMatch, options);
if (!nextMatch)
return nullptr;
nextMatch->firstNode()->renderer()->scrollRectToVisible(SelectionRevealMode::Reveal, nextMatch->absoluteBoundingBox(),
ScrollAlignment::alignCenterIfNeeded, ScrollAlignment::alignCenterIfNeeded);
return nextMatch;
}
RefPtr<Range> Editor::rangeOfString(const String& target, Range* referenceRange, FindOptions options)
{
if (target.isEmpty())
return nullptr;
RefPtr<Range> searchRange(rangeOfContents(document()));
bool forward = !(options & Backwards);
bool startInReferenceRange = referenceRange && (options & StartInSelection);
if (referenceRange) {
if (forward)
searchRange->setStart(startInReferenceRange ? referenceRange->startPosition() : referenceRange->endPosition());
else
searchRange->setEnd(startInReferenceRange ? referenceRange->endPosition() : referenceRange->startPosition());
}
RefPtr<Node> shadowTreeRoot = referenceRange ? referenceRange->startContainer().nonBoundaryShadowTreeRootNode() : nullptr;
if (shadowTreeRoot) {
if (forward)
searchRange->setEnd(*shadowTreeRoot, shadowTreeRoot->countChildNodes());
else
searchRange->setStart(*shadowTreeRoot, 0);
}
RefPtr<Range> resultRange = findPlainText(*searchRange, target, options);
if (startInReferenceRange && areRangesEqual(VisibleSelection(*resultRange).toNormalizedRange().get(), referenceRange)) {
searchRange = rangeOfContents(document());
if (forward)
searchRange->setStart(referenceRange->endPosition());
else
searchRange->setEnd(referenceRange->startPosition());
if (shadowTreeRoot) {
if (forward)
searchRange->setEnd(*shadowTreeRoot, shadowTreeRoot->countChildNodes());
else
searchRange->setStart(*shadowTreeRoot, 0);
}
resultRange = findPlainText(*searchRange, target, options);
}
if (resultRange->collapsed() && shadowTreeRoot) {
searchRange = rangeOfContents(document());
if (shadowTreeRoot->shadowHost()) {
if (forward)
searchRange->setStartAfter(*shadowTreeRoot->shadowHost());
else
searchRange->setEndBefore(*shadowTreeRoot->shadowHost());
}
resultRange = findPlainText(*searchRange, target, options);
}
if (resultRange->collapsed() && options & WrapAround) {
searchRange = rangeOfContents(document());
resultRange = findPlainText(*searchRange, target, options);
}
return resultRange->collapsed() ? nullptr : resultRange;
}
static bool isFrameInRange(Frame* frame, Range* range)
{
bool inRange = false;
for (HTMLFrameOwnerElement* ownerElement = frame->ownerElement(); ownerElement; ownerElement = ownerElement->document().ownerElement()) {
if (&ownerElement->document() == &range->ownerDocument()) {
inRange = range->intersectsNode(*ownerElement, IGNORE_EXCEPTION);
break;
}
}
return inRange;
}
unsigned Editor::countMatchesForText(const String& target, Range* range, FindOptions options, unsigned limit, bool markMatches, Vector<RefPtr<Range>>* matches)
{
if (target.isEmpty())
return 0;
RefPtr<Range> searchRange;
if (range) {
if (&range->ownerDocument() == &document())
searchRange = range;
else if (!isFrameInRange(&m_frame, range))
return 0;
}
if (!searchRange)
searchRange = rangeOfContents(document());
Node& originalEndContainer = searchRange->endContainer();
int originalEndOffset = searchRange->endOffset();
unsigned matchCount = 0;
do {
RefPtr<Range> resultRange(findPlainText(*searchRange, target, options & ~Backwards));
if (resultRange->collapsed()) {
if (!resultRange->startContainer().isInShadowTree())
break;
searchRange->setStartAfter(*resultRange->startContainer().shadowHost(), IGNORE_EXCEPTION);
searchRange->setEnd(originalEndContainer, originalEndOffset, IGNORE_EXCEPTION);
continue;
}
++matchCount;
if (matches)
matches->append(resultRange);
if (markMatches)
document().markers().addMarker(resultRange.get(), DocumentMarker::TextMatch);
if (limit > 0 && matchCount >= limit)
break;
searchRange->setStart(resultRange->endContainer(), resultRange->endOffset(), IGNORE_EXCEPTION);
Node* shadowTreeRoot = searchRange->shadowRoot();
if (searchRange->collapsed() && shadowTreeRoot)
searchRange->setEnd(*shadowTreeRoot, shadowTreeRoot->countChildNodes(), IGNORE_EXCEPTION);
} while (true);
return matchCount;
}
void Editor::setMarkedTextMatchesAreHighlighted(bool flag)
{
if (flag == m_areMarkedTextMatchesHighlighted)
return;
m_areMarkedTextMatchesHighlighted = flag;
document().markers().repaintMarkers(DocumentMarker::TextMatch);
}
void Editor::respondToChangedSelection(const VisibleSelection&, FrameSelection::SetSelectionOptions options)
{
#if PLATFORM(IOS)
if (m_ignoreCompositionSelectionChange)
return;
#endif
if (client())
client()->respondToChangedSelection(&m_frame);
#if ENABLE(TELEPHONE_NUMBER_DETECTION) && !PLATFORM(IOS)
if (shouldDetectTelephoneNumbers())
m_telephoneNumberDetectionUpdateTimer.startOneShot(0);
#endif
setStartNewKillRingSequence(true);
if (m_editorUIUpdateTimer.isActive())
return;
m_editorUIUpdateTimerShouldCheckSpellingAndGrammar = options & FrameSelection::CloseTyping
&& !(options & FrameSelection::SpellCorrectionTriggered);
m_editorUIUpdateTimerWasTriggeredByDictation = options & FrameSelection::DictationTriggered;
m_editorUIUpdateTimer.startOneShot(0);
}
#if ENABLE(TELEPHONE_NUMBER_DETECTION) && !PLATFORM(IOS)
bool Editor::shouldDetectTelephoneNumbers()
{
if (!m_frame.document())
return false;
return document().isTelephoneNumberParsingEnabled() && TelephoneNumberDetector::isSupported();
}
void Editor::scanSelectionForTelephoneNumbers()
{
if (!shouldDetectTelephoneNumbers() || !client())
return;
m_detectedTelephoneNumberRanges.clear();
Vector<RefPtr<Range>> markedRanges;
FrameSelection& frameSelection = m_frame.selection();
if (!frameSelection.isRange()) {
m_frame.mainFrame().servicesOverlayController().selectedTelephoneNumberRangesChanged();
return;
}
RefPtr<Range> selectedRange = frameSelection.toNormalizedRange();
static const int charactersToExtend = 15;
const VisibleSelection& visibleSelection = frameSelection.selection();
Position start = visibleSelection.start();
Position end = visibleSelection.end();
for (int i = 0; i < charactersToExtend; ++i) {
start = start.previous(Character);
end = end.next(Character);
}
FrameSelection extendedSelection;
extendedSelection.setStart(start);
extendedSelection.setEnd(end);
RefPtr<Range> extendedRange = extendedSelection.toNormalizedRange();
if (!extendedRange) {
m_frame.mainFrame().servicesOverlayController().selectedTelephoneNumberRangesChanged();
return;
}
scanRangeForTelephoneNumbers(*extendedRange, extendedRange->text(), markedRanges);
for (auto& range : markedRanges) {
if (rangesOverlap(range.get(), selectedRange.get()))
m_detectedTelephoneNumberRanges.append(range);
}
m_frame.mainFrame().servicesOverlayController().selectedTelephoneNumberRangesChanged();
}
void Editor::scanRangeForTelephoneNumbers(Range& range, const StringView& stringView, Vector<RefPtr<Range>>& markedRanges)
{
Node& startNode = range.startContainer();
if (startNode.hasEditableStyle())
return;
unsigned length = stringView.length();
unsigned scannerPosition = 0;
int relativeStartPosition = 0;
int relativeEndPosition = 0;
auto characters = stringView.upconvertedCharacters();
while (scannerPosition < length && TelephoneNumberDetector::find(&characters[scannerPosition], length - scannerPosition, &relativeStartPosition, &relativeEndPosition)) {
--relativeEndPosition;
ASSERT(scannerPosition + relativeEndPosition < length);
unsigned subrangeOffset = scannerPosition + relativeStartPosition;
unsigned subrangeLength = relativeEndPosition - relativeStartPosition + 1;
RefPtr<Range> subrange = TextIterator::subrange(&range, subrangeOffset, subrangeLength);
markedRanges.append(subrange);
range.ownerDocument().markers().addMarker(subrange.get(), DocumentMarker::TelephoneNumber);
scannerPosition += relativeEndPosition + 1;
}
}
#endif // ENABLE(TELEPHONE_NUMBER_DETECTION) && !PLATFORM(IOS)
void Editor::updateEditorUINowIfScheduled()
{
if (!m_editorUIUpdateTimer.isActive())
return;
m_editorUIUpdateTimer.stop();
editorUIUpdateTimerFired();
}
void Editor::editorUIUpdateTimerFired()
{
VisibleSelection oldSelection = m_oldSelectionForEditorUIUpdate;
m_alternativeTextController->stopPendingCorrection(oldSelection);
bool isContinuousSpellCheckingEnabled = this->isContinuousSpellCheckingEnabled();
bool isContinuousGrammarCheckingEnabled = isContinuousSpellCheckingEnabled && isGrammarCheckingEnabled();
if (isContinuousSpellCheckingEnabled) {
VisibleSelection newAdjacentWords;
VisibleSelection newSelectedSentence;
bool caretBrowsing = m_frame.settings().caretBrowsingEnabled();
if (m_frame.selection().selection().isContentEditable() || caretBrowsing) {
VisiblePosition newStart(m_frame.selection().selection().visibleStart());
#if !PLATFORM(IOS)
newAdjacentWords = VisibleSelection(startOfWord(newStart, LeftWordIfOnBoundary), endOfWord(newStart, RightWordIfOnBoundary));
#else
EWordSide startWordSide = LeftWordIfOnBoundary;
UChar32 c = newStart.characterBefore();
if (isSpaceOrNewline(c) || c == 0xA0 || isStartOfParagraph(newStart)) {
startWordSide = RightWordIfOnBoundary;
}
newAdjacentWords = VisibleSelection(startOfWord(newStart, startWordSide), endOfWord(newStart, RightWordIfOnBoundary));
#endif // !PLATFORM(IOS)
if (isContinuousGrammarCheckingEnabled)
newSelectedSentence = VisibleSelection(startOfSentence(newStart), endOfSentence(newStart));
}
if (m_editorUIUpdateTimerShouldCheckSpellingAndGrammar && oldSelection.isContentEditable() && oldSelection.start().deprecatedNode() && oldSelection.start().anchorNode()->inDocument()) {
VisiblePosition oldStart(oldSelection.visibleStart());
VisibleSelection oldAdjacentWords = VisibleSelection(startOfWord(oldStart, LeftWordIfOnBoundary), endOfWord(oldStart, RightWordIfOnBoundary));
if (oldAdjacentWords != newAdjacentWords) {
if (isContinuousGrammarCheckingEnabled) {
VisibleSelection oldSelectedSentence = VisibleSelection(startOfSentence(oldStart), endOfSentence(oldStart));
markMisspellingsAndBadGrammar(oldAdjacentWords, oldSelectedSentence != newSelectedSentence, oldSelectedSentence);
} else
markMisspellingsAndBadGrammar(oldAdjacentWords, false, oldAdjacentWords);
}
}
if (!textChecker() || textChecker()->shouldEraseMarkersAfterChangeSelection(TextCheckingTypeSpelling)) {
if (RefPtr<Range> wordRange = newAdjacentWords.toNormalizedRange())
document().markers().removeMarkers(wordRange.get(), DocumentMarker::Spelling);
}
if (!textChecker() || textChecker()->shouldEraseMarkersAfterChangeSelection(TextCheckingTypeGrammar)) {
if (RefPtr<Range> sentenceRange = newSelectedSentence.toNormalizedRange())
document().markers().removeMarkers(sentenceRange.get(), DocumentMarker::Grammar);
}
}
if (!isContinuousSpellCheckingEnabled)
document().markers().removeMarkers(DocumentMarker::Spelling);
if (!isContinuousGrammarCheckingEnabled)
document().markers().removeMarkers(DocumentMarker::Grammar);
if (!m_editorUIUpdateTimerWasTriggeredByDictation)
m_alternativeTextController->respondToChangedSelection(oldSelection);
m_oldSelectionForEditorUIUpdate = m_frame.selection().selection();
}
static Node* findFirstMarkable(Node* node)
{
while (node) {
if (!node->renderer())
return nullptr;
if (node->renderer()->isTextOrLineBreak())
return node;
if (is<HTMLTextFormControlElement>(*node))
node = downcast<HTMLTextFormControlElement>(*node).visiblePositionForIndex(1).deepEquivalent().deprecatedNode();
else if (node->firstChild())
node = node->firstChild();
else
node = node->nextSibling();
}
return nullptr;
}
bool Editor::selectionStartHasMarkerFor(DocumentMarker::MarkerType markerType, int from, int length) const
{
Node* node = findFirstMarkable(m_frame.selection().selection().start().deprecatedNode());
if (!node)
return false;
unsigned int startOffset = static_cast<unsigned int>(from);
unsigned int endOffset = static_cast<unsigned int>(from + length);
Vector<RenderedDocumentMarker*> markers = document().markers().markersFor(node);
for (auto* marker : markers) {
if (marker->startOffset() <= startOffset && endOffset <= marker->endOffset() && marker->type() == markerType)
return true;
}
return false;
}
TextCheckingTypeMask Editor::resolveTextCheckingTypeMask(TextCheckingTypeMask textCheckingOptions)
{
bool shouldMarkSpelling = textCheckingOptions & TextCheckingTypeSpelling;
bool shouldMarkGrammar = textCheckingOptions & TextCheckingTypeGrammar;
#if !PLATFORM(IOS)
bool shouldShowCorrectionPanel = textCheckingOptions & TextCheckingTypeShowCorrectionPanel;
bool shouldCheckForCorrection = shouldShowCorrectionPanel || (textCheckingOptions & TextCheckingTypeCorrection);
#endif
TextCheckingTypeMask checkingTypes = 0;
if (shouldMarkSpelling)
checkingTypes |= TextCheckingTypeSpelling;
if (shouldMarkGrammar)
checkingTypes |= TextCheckingTypeGrammar;
#if !PLATFORM(IOS)
if (shouldCheckForCorrection)
checkingTypes |= TextCheckingTypeCorrection;
if (shouldShowCorrectionPanel)
checkingTypes |= TextCheckingTypeShowCorrectionPanel;
#if USE(AUTOMATIC_TEXT_REPLACEMENT)
bool shouldPerformReplacement = textCheckingOptions & TextCheckingTypeReplacement;
if (shouldPerformReplacement) {
if (isAutomaticLinkDetectionEnabled())
checkingTypes |= TextCheckingTypeLink;
if (isAutomaticQuoteSubstitutionEnabled())
checkingTypes |= TextCheckingTypeQuote;
if (isAutomaticDashSubstitutionEnabled())
checkingTypes |= TextCheckingTypeDash;
if (isAutomaticTextReplacementEnabled())
checkingTypes |= TextCheckingTypeReplacement;
if (shouldMarkSpelling && isAutomaticSpellingCorrectionEnabled())
checkingTypes |= TextCheckingTypeCorrection;
}
#endif
#endif // !PLATFORM(IOS)
return checkingTypes;
}
static RefPtr<Range> candidateRangeForSelection(Frame& frame)
{
const VisibleSelection& selection = frame.selection().selection();
return selection.isCaret() ? wordRangeFromPosition(selection.start()) : frame.selection().toNormalizedRange();
}
static bool candidateWouldReplaceText(const VisibleSelection& selection)
{
UChar32 characterAfterSelection, characterBeforeSelection, twoCharacterBeforeSelection = 0;
charactersAroundPosition(selection.visibleStart(), characterAfterSelection, characterBeforeSelection, twoCharacterBeforeSelection);
return !(characterBeforeSelection == '\0' || characterBeforeSelection == '\n' || characterBeforeSelection == ' ');
}
String Editor::stringForCandidateRequest() const
{
const VisibleSelection& selection = m_frame.selection().selection();
RefPtr<Range> rangeForCurrentlyTypedString = candidateRangeForSelection(m_frame);
if (rangeForCurrentlyTypedString && candidateWouldReplaceText(selection))
return plainText(rangeForCurrentlyTypedString.get());
return String();
}
RefPtr<Range> Editor::contextRangeForCandidateRequest() const
{
const VisibleSelection& selection = m_frame.selection().selection();
return makeRange(startOfParagraph(selection.visibleStart()), endOfParagraph(selection.visibleEnd()));
}
RefPtr<Range> Editor::rangeForTextCheckingResult(const TextCheckingResult& result) const
{
if (!result.length)
return nullptr;
RefPtr<Range> contextRange = contextRangeForCandidateRequest();
if (!contextRange)
return nullptr;
return TextIterator::subrange(contextRange.get(), result.location, result.length);
}
void Editor::handleAcceptedCandidate(TextCheckingResult acceptedCandidate)
{
const VisibleSelection& selection = m_frame.selection().selection();
m_isHandlingAcceptedCandidate = true;
if (auto range = rangeForTextCheckingResult(acceptedCandidate)) {
if (shouldInsertText(acceptedCandidate.replacement, range.get(), EditorInsertActionTyped)) {
Ref<ReplaceRangeWithTextCommand> replaceCommand = ReplaceRangeWithTextCommand::create(range.get(), acceptedCandidate.replacement);
applyCommand(replaceCommand.ptr());
}
} else
insertText(acceptedCandidate.replacement, nullptr);
RefPtr<Range> insertedCandidateRange = rangeExpandedByCharactersInDirectionAtWordBoundary(selection.visibleStart(), acceptedCandidate.replacement.length(), DirectionBackward);
if (insertedCandidateRange)
insertedCandidateRange->startContainer().document().markers().addMarker(insertedCandidateRange.get(), DocumentMarker::AcceptedCandidate, acceptedCandidate.replacement);
m_isHandlingAcceptedCandidate = false;
}
bool Editor::unifiedTextCheckerEnabled() const
{
return WebCore::unifiedTextCheckerEnabled(&m_frame);
}
Vector<String> Editor::dictationAlternativesForMarker(const DocumentMarker* marker)
{
return m_alternativeTextController->dictationAlternativesForMarker(marker);
}
void Editor::applyDictationAlternativelternative(const String& alternativeString)
{
m_alternativeTextController->applyDictationAlternative(alternativeString);
}
void Editor::toggleOverwriteModeEnabled()
{
m_overwriteModeEnabled = !m_overwriteModeEnabled;
m_frame.selection().setShouldShowBlockCursor(m_overwriteModeEnabled);
}
Document& Editor::document() const
{
ASSERT(m_frame.document());
return *m_frame.document();
}
}