本文整理汇总了C++中VisibleSelection::toNormalizedRange方法的典型用法代码示例。如果您正苦于以下问题:C++ VisibleSelection::toNormalizedRange方法的具体用法?C++ VisibleSelection::toNormalizedRange怎么用?C++ VisibleSelection::toNormalizedRange使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类VisibleSelection
的用法示例。
在下文中一共展示了VisibleSelection::toNormalizedRange方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: selectMisspellingAsync
static String selectMisspellingAsync(Frame* selectedFrame, DocumentMarker& marker)
{
VisibleSelection selection = selectedFrame->selection()->selection();
if (!selection.isCaretOrRange())
return String();
// Caret and range selections always return valid normalized ranges.
RefPtr<Range> selectionRange = selection.toNormalizedRange();
Vector<DocumentMarker*> markers = selectedFrame->document()->markers()->markersInRange(selectionRange.get(), DocumentMarker::Spelling | DocumentMarker::Grammar);
if (markers.size() != 1)
return String();
marker = *markers[0];
// Cloning a range fails only for invalid ranges.
RefPtr<Range> markerRange = selectionRange->cloneRange(ASSERT_NO_EXCEPTION);
markerRange->setStart(markerRange->startContainer(), marker.startOffset());
markerRange->setEnd(markerRange->endContainer(), marker.endOffset());
if (selection.isCaret()) {
selection = VisibleSelection(markerRange.get());
selectedFrame->selection()->setSelection(selection, WordGranularity);
selectionRange = selection.toNormalizedRange();
}
if (markerRange->text().stripWhiteSpace(&IsWhiteSpaceOrPunctuation) != selectionRange->text().stripWhiteSpace(&IsWhiteSpaceOrPunctuation))
return String();
return markerRange->text();
}
示例2: markMisspellingsAfterTypingToWord
void SpellChecker::markMisspellingsAfterTypingToWord(const VisiblePosition &wordStart, const VisibleSelection& selectionAfterTyping)
{
if (unifiedTextCheckerEnabled()) {
TextCheckingTypeMask textCheckingOptions = 0;
if (isContinuousSpellCheckingEnabled())
textCheckingOptions |= TextCheckingTypeSpelling;
if (!(textCheckingOptions & TextCheckingTypeSpelling))
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;
// Check spelling of one word
RefPtr<Range> misspellingRange = nullptr;
markMisspellings(VisibleSelection(startOfWord(wordStart, LeftWordIfOnBoundary), endOfWord(wordStart, RightWordIfOnBoundary)), misspellingRange);
// Autocorrect the misspelled word.
if (!misspellingRange)
return;
// Get the misspelled word.
const String misspelledWord = plainText(misspellingRange.get());
String autocorrectedString = textChecker().getAutoCorrectSuggestionForMisspelledWord(misspelledWord);
// If autocorrected word is non empty, replace the misspelled word by this word.
if (!autocorrectedString.isEmpty()) {
VisibleSelection newSelection(misspellingRange.get(), DOWNSTREAM);
if (newSelection != m_frame.selection().selection()) {
m_frame.selection().setSelection(newSelection);
}
m_frame.editor().replaceSelectionWithText(autocorrectedString, false, false);
// Reset the charet one character further.
m_frame.selection().moveTo(m_frame.selection().selection().visibleEnd());
m_frame.selection().modify(FrameSelection::AlterationMove, DirectionForward, CharacterGranularity);
}
if (!isGrammarCheckingEnabled())
return;
// Check grammar of entire sentence
markBadGrammar(VisibleSelection(startOfSentence(wordStart), endOfSentence(wordStart)));
}
示例3: respondToChangedSelection
void SpellChecker::respondToChangedSelection(const VisibleSelection& oldSelection, FrameSelection::SetSelectionOptions options)
{
bool closeTyping = options & FrameSelection::CloseTyping;
bool isContinuousSpellCheckingEnabled = this->isContinuousSpellCheckingEnabled();
bool isContinuousGrammarCheckingEnabled = isContinuousSpellCheckingEnabled && isGrammarCheckingEnabled();
if (isContinuousSpellCheckingEnabled) {
VisibleSelection newAdjacentWords;
VisibleSelection newSelectedSentence;
const VisibleSelection newSelection = m_frame.selection().selection();
VisiblePosition newStart(newSelection.visibleStart());
newAdjacentWords = VisibleSelection(startOfWord(newStart, LeftWordIfOnBoundary), endOfWord(newStart, RightWordIfOnBoundary));
if (isContinuousGrammarCheckingEnabled)
newSelectedSentence = VisibleSelection(startOfSentence(newStart), endOfSentence(newStart));
// Don't check spelling and grammar if the change of selection is triggered by spelling correction itself.
bool shouldCheckSpellingAndGrammar = !(options & FrameSelection::SpellCorrectionTriggered);
// When typing we check spelling elsewhere, so don't redo it here.
// If this is a change in selection resulting from a delete operation,
// oldSelection may no longer be in the document.
// FIXME(http://crbug.com/382809): if oldSelection is on a textarea
// element, we cause synchronous layout.
if (shouldCheckSpellingAndGrammar
&& closeTyping
&& !isSelectionInTextField(oldSelection)
&& (isSelectionInTextArea(oldSelection) || oldSelection.isContentEditable())
&& oldSelection.start().inDocument()) {
spellCheckOldSelection(oldSelection, newAdjacentWords);
}
// FIXME(http://crbug.com/382809):
// shouldEraseMarkersAfterChangeSelection is true, we cause synchronous
// layout.
if (textChecker().shouldEraseMarkersAfterChangeSelection(TextCheckingTypeSpelling)) {
if (RefPtr<Range> wordRange = newAdjacentWords.toNormalizedRange())
m_frame.document()->markers().removeMarkers(wordRange.get(), DocumentMarker::Spelling);
}
if (textChecker().shouldEraseMarkersAfterChangeSelection(TextCheckingTypeGrammar)) {
if (RefPtr<Range> sentenceRange = newSelectedSentence.toNormalizedRange())
m_frame.document()->markers().removeMarkers(sentenceRange.get(), DocumentMarker::Grammar);
}
}
// When continuous spell checking is off, existing markers disappear after the selection changes.
if (!isContinuousSpellCheckingEnabled)
m_frame.document()->markers().removeMarkers(DocumentMarker::Spelling);
if (!isContinuousGrammarCheckingEnabled)
m_frame.document()->markers().removeMarkers(DocumentMarker::Grammar);
}
示例4: PLATFORM
RefPtr<TextIndicator> TextIndicator::createWithRange(const Range& range, TextIndicatorPresentationTransition presentationTransition, unsigned margin)
{
Frame* frame = range.startContainer()->document().frame();
if (!frame)
return nullptr;
#if PLATFORM(IOS)
frame->editor().setIgnoreCompositionSelectionChange(true);
frame->selection().setUpdateAppearanceEnabled(true);
#endif
VisibleSelection oldSelection = frame->selection().selection();
frame->selection().setSelection(range);
RefPtr<TextIndicator> indicator = TextIndicator::createWithSelectionInFrame(*frame, presentationTransition, margin);
frame->selection().setSelection(oldSelection);
if (indicator)
indicator->setWantsMargin(!areRangesEqual(&range, oldSelection.toNormalizedRange().get()));
#if PLATFORM(IOS)
frame->editor().setIgnoreCompositionSelectionChange(false, Editor::RevealSelection::No);
frame->selection().setUpdateAppearanceEnabled(false);
#endif
return indicator.release();
}
示例5: expandSelectionToGranularity
static void expandSelectionToGranularity(Frame* frame, int x, int y, TextGranularity granularity, bool isInputMode)
{
ASSERT(frame);
ASSERT(frame->selection());
VisibleSelection selection;
if (x < 0 || y < 0) {
if (!isInputMode)
return; // Invalid request
// Input mode based selection, use the current selection as the selection point.
ASSERT(frame->selection()->selectionType() != VisibleSelection::NoSelection);
selection = frame->selection()->selection();
} else {
VisiblePosition pointLocation(frame->visiblePositionForPoint(WebCore::IntPoint(x, y)));
selection = VisibleSelection(pointLocation, pointLocation);
}
if (!(selection.start().anchorNode() && selection.start().anchorNode()->isTextNode()))
return;
selection.expandUsingGranularity(granularity);
RefPtr<Range> newRange = selection.toNormalizedRange();
if (!newRange)
return;
ExceptionCode ec = 0;
if (newRange->collapsed(ec))
return;
RefPtr<Range> oldRange = frame->selection()->selection().toNormalizedRange();
EAffinity affinity = frame->selection()->affinity();
if (isInputMode && !frame->editor()->client()->shouldChangeSelectedRange(oldRange.get(), newRange.get(), affinity, false))
return;
frame->selection()->setSelectedRange(newRange.get(), affinity, true);
}
示例6: selectionBelongsToObject
bool selectionBelongsToObject(AccessibilityObject* coreObject, VisibleSelection& selection)
{
if (!coreObject || !coreObject->isAccessibilityRenderObject())
return false;
if (selection.isNone())
return false;
RefPtr<Range> range = selection.toNormalizedRange();
if (!range)
return false;
// We want to check that both the selection intersects the node
// AND that the selection is not just "touching" one of the
// boundaries for the selected node. We want to check whether the
// node is actually inside the region, at least partially.
auto& node = *coreObject->node();
auto* lastDescendant = node.lastDescendant();
unsigned lastOffset = lastOffsetInNode(lastDescendant);
auto intersectsResult = range->intersectsNode(node);
return !intersectsResult.hasException()
&& intersectsResult.releaseReturnValue()
&& (&range->endContainer() != &node || range->endOffset())
&& (&range->startContainer() != lastDescendant || range->startOffset() != lastOffset);
}
示例7: markMisspellingsOrBadGrammar
void SpellChecker::markMisspellingsOrBadGrammar(const VisibleSelection& selection, bool checkSpelling, RefPtr<Range>& firstMisspellingRange)
{
// This function is called with a selection already expanded to word boundaries.
// Might be nice to assert that here.
// This function is used only for as-you-type checking, so if that's off we do nothing. Note that
// grammar checking can only be on if spell checking is also on.
if (!isContinuousSpellCheckingEnabled())
return;
RefPtr<Range> searchRange(selection.toNormalizedRange());
if (!searchRange)
return;
// If we're not in an editable node, bail.
Node* editableNode = searchRange->startContainer();
if (!editableNode || !editableNode->hasEditableStyle())
return;
if (!isSpellCheckingEnabledFor(editableNode))
return;
TextCheckingHelper checker(spellCheckerClient(), searchRange);
if (checkSpelling)
checker.markAllMisspellings(firstMisspellingRange);
else if (isGrammarCheckingEnabled())
checker.markAllBadGrammar();
}
示例8: setSelectionToDragCaret
static bool setSelectionToDragCaret(Frame* frame, VisibleSelection& dragCaret, RefPtr<Range>& range, const IntPoint& point)
{
frame->selection()->setSelection(dragCaret);
if (frame->selection()->isNone()) {
dragCaret = frame->visiblePositionForPoint(point);
frame->selection()->setSelection(dragCaret);
range = dragCaret.toNormalizedRange();
}
return !frame->selection()->isNone() && frame->selection()->isContentEditable();
}
示例9: getSelectionOffsetsForObject
static void getSelectionOffsetsForObject(AccessibilityObject* coreObject, VisibleSelection& selection, gint& startOffset, gint& endOffset)
{
if (!coreObject->isAccessibilityRenderObject())
return;
// Early return if the selection doesn't affect the selected node.
if (!selectionBelongsToObject(coreObject, selection))
return;
// We need to find the exact start and end positions in the
// selected node that intersects the selection, to later on get
// the right values for the effective start and end offsets.
ExceptionCode ec = 0;
Position nodeRangeStart;
Position nodeRangeEnd;
Node* node = coreObject->node();
RefPtr<Range> selRange = selection.toNormalizedRange();
// If the selection affects the selected node and its first
// possible position is also in the selection, we must set
// nodeRangeStart to that position, otherwise to the selection's
// start position (it would belong to the node anyway).
Node* firstLeafNode = node->firstDescendant();
if (selRange->isPointInRange(firstLeafNode, 0, ec))
nodeRangeStart = firstPositionInOrBeforeNode(firstLeafNode);
else
nodeRangeStart = selRange->startPosition();
// If the selection affects the selected node and its last
// possible position is also in the selection, we must set
// nodeRangeEnd to that position, otherwise to the selection's
// end position (it would belong to the node anyway).
Node* lastLeafNode = node->lastDescendant();
if (selRange->isPointInRange(lastLeafNode, lastOffsetInNode(lastLeafNode), ec))
nodeRangeEnd = lastPositionInOrAfterNode(lastLeafNode);
else
nodeRangeEnd = selRange->endPosition();
// Calculate position of the selected range inside the object.
Position parentFirstPosition = firstPositionInOrBeforeNode(node);
RefPtr<Range> rangeInParent = Range::create(node->document(), parentFirstPosition, nodeRangeStart);
// Set values for start and end offsets.
startOffset = TextIterator::rangeLength(rangeInParent.get(), true);
// We need to adjust the offsets for the list item marker.
RenderObject* renderer = coreObject->renderer();
if (renderer && renderer->isListItem()) {
String markerText = toRenderListItem(renderer)->markerTextWithSuffix();
startOffset += markerText.length();
}
RefPtr<Range> nodeRange = Range::create(node->document(), nodeRangeStart, nodeRangeEnd);
endOffset = startOffset + TextIterator::rangeLength(nodeRange.get(), true);
}
示例10: markMisspellingsAndBadGrammar
void SpellChecker::markMisspellingsAndBadGrammar(const VisibleSelection& spellingSelection, bool markGrammar, const VisibleSelection& grammarSelection)
{
if (unifiedTextCheckerEnabled()) {
if (!isContinuousSpellCheckingEnabled())
return;
// markMisspellingsAndBadGrammar() is triggered by selection change, in which case we check spelling and grammar, but don't autocorrect misspellings.
TextCheckingTypeMask textCheckingOptions = TextCheckingTypeSpelling;
if (markGrammar && isGrammarCheckingEnabled())
textCheckingOptions |= TextCheckingTypeGrammar;
markAllMisspellingsAndBadGrammarInRanges(textCheckingOptions, spellingSelection.toNormalizedRange().get(), grammarSelection.toNormalizedRange().get());
return;
}
RefPtr<Range> firstMisspellingRange = nullptr;
markMisspellings(spellingSelection, firstMisspellingRange);
if (markGrammar)
markBadGrammar(grammarSelection);
}
示例11: visibleSelectionForClosestActualWordStart
VisibleSelection visibleSelectionForClosestActualWordStart(const VisibleSelection& selection)
{
// VisibleSelection validation has a special case when the caret is at the end of a paragraph where
// it selects the paragraph marker. As well, if the position is at the end of a word, it will select
// only the space between words. We want to select an actual word so we move the selection to
// the start of the leftmost word if the character after the selection point is whitespace.
if (selection.selectionType() != VisibleSelection::RangeSelection) {
int leftDistance = 0;
int rightDistance = 0;
VisibleSelection leftSelection(previousWordPosition(selection.start()));
bool leftSelectionIsOnWord = !isWhitespace(leftSelection.visibleStart().characterAfter()) && leftSelection.start().containerNode() == selection.start().containerNode();
if (leftSelectionIsOnWord) {
VisibleSelection rangeSelection(endOfWord(leftSelection.start()), selection.visibleStart());
leftDistance = TextIterator::rangeLength(rangeSelection.toNormalizedRange().get());
}
VisibleSelection rightSelection = previousWordPosition(nextWordPosition(selection.start()));
bool rightSelectionIsOnWord = !isWhitespace(rightSelection.visibleStart().characterAfter()) && rightSelection.start().containerNode() == selection.start().containerNode();
if (rightSelectionIsOnWord) {
VisibleSelection rangeSelection = VisibleSelection(rightSelection.visibleStart(), selection.visibleStart());
rightDistance = TextIterator::rangeLength(rangeSelection.toNormalizedRange().get());
}
// Make sure we found an actual word. If not, return the original selection.
if (!leftSelectionIsOnWord && !rightSelectionIsOnWord)
return selection;
if (!rightSelectionIsOnWord || (leftSelectionIsOnWord && leftDistance <= rightDistance)) {
// Left is closer or right is invalid.
return leftSelection;
}
// Right is closer or equal, or left was invalid.
return rightSelection;
}
// No adjustment required.
return selection;
}
示例12: markMisspellingsAfterLineBreak
void SpellChecker::markMisspellingsAfterLineBreak(const VisibleSelection& wordSelection)
{
if (unifiedTextCheckerEnabled()) {
TextCheckingTypeMask textCheckingOptions = 0;
if (isContinuousSpellCheckingEnabled())
textCheckingOptions |= TextCheckingTypeSpelling;
if (isGrammarCheckingEnabled())
textCheckingOptions |= TextCheckingTypeGrammar;
VisibleSelection wholeParagraph(
startOfParagraph(wordSelection.visibleStart()),
endOfParagraph(wordSelection.visibleEnd()));
markAllMisspellingsAndBadGrammarInRanges(
textCheckingOptions, wordSelection.toNormalizedRange().get(),
wholeParagraph.toNormalizedRange().get());
} else {
RefPtr<Range> misspellingRange = wordSelection.firstRange();
markMisspellings(wordSelection, misspellingRange);
}
}
示例13: selectMisspellingAsync
static String selectMisspellingAsync(LocalFrame* selectedFrame, DocumentMarker& marker)
{
VisibleSelection selection = selectedFrame->selection().selection();
if (!selection.isCaretOrRange())
return String();
// Caret and range selections always return valid normalized ranges.
RefPtrWillBeRawPtr<Range> selectionRange = selection.toNormalizedRange();
WillBeHeapVector<DocumentMarker*> markers = selectedFrame->document()->markers().markersInRange(selectionRange.get(), DocumentMarker::MisspellingMarkers());
if (markers.size() != 1)
return String();
marker = *markers[0];
// Cloning a range fails only for invalid ranges.
RefPtrWillBeRawPtr<Range> markerRange = selectionRange->cloneRange();
markerRange->setStart(markerRange->startContainer(), marker.startOffset());
markerRange->setEnd(markerRange->endContainer(), marker.endOffset());
if (markerRange->text().stripWhiteSpace(&IsWhiteSpaceOrPunctuation) != selectionRange->text().stripWhiteSpace(&IsWhiteSpaceOrPunctuation))
return String();
return markerRange->text();
}
示例14: PLATFORM
RefPtr<TextIndicator> TextIndicator::createWithRange(const Range& range, TextIndicatorOptions options, TextIndicatorPresentationTransition presentationTransition, FloatSize margin)
{
Frame* frame = range.startContainer().document().frame();
if (!frame)
return nullptr;
#if PLATFORM(IOS)
frame->editor().setIgnoreCompositionSelectionChange(true);
frame->selection().setUpdateAppearanceEnabled(true);
#endif
VisibleSelection oldSelection = frame->selection().selection();
frame->selection().setSelection(range);
TextIndicatorData data;
data.presentationTransition = presentationTransition;
data.options = options;
bool indicatesCurrentSelection = areRangesEqual(&range, oldSelection.toNormalizedRange().get());
if (!initializeIndicator(data, *frame, range, margin, indicatesCurrentSelection))
return nullptr;
RefPtr<TextIndicator> indicator = TextIndicator::create(data);
frame->selection().setSelection(oldSelection);
#if PLATFORM(IOS)
frame->editor().setIgnoreCompositionSelectionChange(false, Editor::RevealSelection::No);
frame->selection().setUpdateAppearanceEnabled(false);
#endif
return indicator;
}
示例15: forwardDeleteKeyPressed
void TypingCommand::forwardDeleteKeyPressed(TextGranularity granularity, bool killRing)
{
Frame* frame = document()->frame();
if (!frame)
return;
frame->editor()->updateMarkersForWordsAffectedByEditing(false);
VisibleSelection selectionToDelete;
VisibleSelection selectionAfterUndo;
switch (endingSelection().selectionType()) {
case VisibleSelection::RangeSelection:
selectionToDelete = endingSelection();
selectionAfterUndo = selectionToDelete;
break;
case VisibleSelection::CaretSelection: {
m_smartDelete = false;
// Handle delete at beginning-of-block case.
// Do nothing in the case that the caret is at the start of a
// root editable element or at the start of a document.
FrameSelection selection;
selection.setSelection(endingSelection());
selection.modify(FrameSelection::AlterationExtend, DirectionForward, granularity);
if (killRing && selection.isCaret() && granularity != CharacterGranularity)
selection.modify(FrameSelection::AlterationExtend, DirectionForward, CharacterGranularity);
Position downstreamEnd = endingSelection().end().downstream();
VisiblePosition visibleEnd = endingSelection().visibleEnd();
Node* enclosingTableCell = enclosingNodeOfType(visibleEnd.deepEquivalent(), &isTableCell);
if (enclosingTableCell && visibleEnd == lastPositionInNode(enclosingTableCell))
return;
if (visibleEnd == endOfParagraph(visibleEnd))
downstreamEnd = visibleEnd.next(CannotCrossEditingBoundary).deepEquivalent().downstream();
// When deleting tables: Select the table first, then perform the deletion
if (downstreamEnd.containerNode() && downstreamEnd.containerNode()->renderer() && downstreamEnd.containerNode()->renderer()->isTable()
&& downstreamEnd.computeOffsetInContainerNode() <= caretMinOffset(downstreamEnd.containerNode())) {
setEndingSelection(VisibleSelection(endingSelection().end(), positionAfterNode(downstreamEnd.containerNode()), DOWNSTREAM, endingSelection().isDirectional()));
typingAddedToOpenCommand(ForwardDeleteKey);
return;
}
// deleting to end of paragraph when at end of paragraph needs to merge the next paragraph (if any)
if (granularity == ParagraphBoundary && selection.selection().isCaret() && isEndOfParagraph(selection.selection().visibleEnd()))
selection.modify(FrameSelection::AlterationExtend, DirectionForward, CharacterGranularity);
selectionToDelete = selection.selection();
if (!startingSelection().isRange() || selectionToDelete.base() != startingSelection().start())
selectionAfterUndo = selectionToDelete;
else {
// It's a little tricky to compute what the starting selection would have been in the original document.
// We can't let the VisibleSelection class's validation kick in or it'll adjust for us based on
// the current state of the document and we'll get the wrong result.
Position extent = startingSelection().end();
if (extent.containerNode() != selectionToDelete.end().containerNode())
extent = selectionToDelete.extent();
else {
int extraCharacters;
if (selectionToDelete.start().containerNode() == selectionToDelete.end().containerNode())
extraCharacters = selectionToDelete.end().computeOffsetInContainerNode() - selectionToDelete.start().computeOffsetInContainerNode();
else
extraCharacters = selectionToDelete.end().computeOffsetInContainerNode();
extent = Position(extent.containerNode(), extent.computeOffsetInContainerNode() + extraCharacters, Position::PositionIsOffsetInAnchor);
}
selectionAfterUndo.setWithoutValidation(startingSelection().start(), extent);
}
break;
}
case VisibleSelection::NoSelection:
ASSERT_NOT_REACHED();
break;
}
ASSERT(!selectionToDelete.isNone());
if (selectionToDelete.isNone())
return;
if (selectionToDelete.isCaret() || !frame->selection()->shouldDeleteSelection(selectionToDelete))
return;
if (killRing)
frame->editor()->addToKillRing(selectionToDelete.toNormalizedRange().get(), false);
// make undo select what was deleted
setStartingSelection(selectionAfterUndo);
CompositeEditCommand::deleteSelection(selectionToDelete, m_smartDelete);
setSmartDelete(false);
typingAddedToOpenCommand(ForwardDeleteKey);
}