/* * Copyright (C) 2004, 2008, 2009, 2010 Apple Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include "config.h" #include "FrameSelection.h" #include "CharacterData.h" #include "DeleteSelectionCommand.h" #include "Document.h" #include "Editor.h" #include "EditorClient.h" #include "Element.h" #include "EventHandler.h" #include "ExceptionCode.h" #include "FloatQuad.h" #include "FocusController.h" #include "Frame.h" #include "FrameTree.h" #include "FrameView.h" #include "GraphicsContext.h" #include "HTMLFormElement.h" #include "HTMLFrameElementBase.h" #include "HTMLInputElement.h" #include "HTMLSelectElement.h" #include "HTMLNames.h" #include "HitTestRequest.h" #include "HitTestResult.h" #include "InlineTextBox.h" #include "NodeTraversal.h" #include "Page.h" #include "Range.h" #include "RenderText.h" #include "RenderTextControl.h" #include "RenderTheme.h" #include "RenderView.h" #include "RenderWidget.h" #include "RenderedPosition.h" #include "Settings.h" #include "SpatialNavigation.h" #include "StylePropertySet.h" #include "TextIterator.h" #include "TypingCommand.h" #include "VisibleUnits.h" #include "htmlediting.h" #include #include #define EDIT_DEBUG 0 namespace WebCore { using namespace HTMLNames; static inline LayoutUnit NoXPosForVerticalArrowNavigation() { return LayoutUnit::min(); } CaretBase::CaretBase(CaretVisibility visibility) : m_caretRectNeedsUpdate(true) , m_caretVisibility(visibility) { } DragCaretController::DragCaretController() : CaretBase(Visible) { } PassOwnPtr DragCaretController::create() { return adoptPtr(new DragCaretController); } bool DragCaretController::isContentRichlyEditable() const { return isRichlyEditablePosition(m_position.deepEquivalent()); } static inline bool shouldAlwaysUseDirectionalSelection(Frame* frame) { return !frame || frame->editor().behavior().shouldConsiderSelectionAsDirectional(); } FrameSelection::FrameSelection(Frame* frame) : m_frame(frame) , m_xPosForVerticalArrowNavigation(NoXPosForVerticalArrowNavigation()) , m_granularity(CharacterGranularity) , m_caretBlinkTimer(this, &FrameSelection::caretBlinkTimerFired) , m_absCaretBoundsDirty(true) , m_caretPaint(true) , m_isCaretBlinkingSuspended(false) , m_focused(frame && frame->page() && frame->page()->focusController()->focusedFrame() == frame) , m_shouldShowBlockCursor(false) { if (shouldAlwaysUseDirectionalSelection(m_frame)) m_selection.setIsDirectional(true); } Element* FrameSelection::rootEditableElementOrDocumentElement() const { Element* selectionRoot = m_selection.rootEditableElement(); return selectionRoot ? selectionRoot : m_frame->document()->documentElement(); } Node* FrameSelection::rootEditableElementOrTreeScopeRootNode() const { Element* selectionRoot = m_selection.rootEditableElement(); if (selectionRoot) return selectionRoot; Node* node = m_selection.base().containerNode(); return node ? node->treeScope()->rootNode() : 0; } Element* FrameSelection::rootEditableElementRespectingShadowTree() const { Element* selectionRoot = m_selection.rootEditableElement(); if (selectionRoot && selectionRoot->isInShadowTree()) selectionRoot = selectionRoot->shadowHost(); return selectionRoot; } void FrameSelection::moveTo(const VisiblePosition &pos, EUserTriggered userTriggered, CursorAlignOnScroll align) { SetSelectionOptions options = CloseTyping | ClearTypingStyle | userTriggered; setSelection(VisibleSelection(pos.deepEquivalent(), pos.deepEquivalent(), pos.affinity(), m_selection.isDirectional()), options, align); } void FrameSelection::moveTo(const VisiblePosition &base, const VisiblePosition &extent, EUserTriggered userTriggered) { const bool selectionHasDirection = true; SetSelectionOptions options = CloseTyping | ClearTypingStyle | userTriggered; setSelection(VisibleSelection(base.deepEquivalent(), extent.deepEquivalent(), base.affinity(), selectionHasDirection), options); } void FrameSelection::moveTo(const Position &pos, EAffinity affinity, EUserTriggered userTriggered) { SetSelectionOptions options = CloseTyping | ClearTypingStyle | userTriggered; setSelection(VisibleSelection(pos, affinity, m_selection.isDirectional()), options); } void FrameSelection::moveTo(const Range *r, EAffinity affinity, EUserTriggered userTriggered) { SetSelectionOptions options = CloseTyping | ClearTypingStyle | userTriggered; VisibleSelection selection = r ? VisibleSelection(r->startPosition(), r->endPosition(), affinity) : VisibleSelection(Position(), Position(), affinity); setSelection(selection, options); } void FrameSelection::moveTo(const Position &base, const Position &extent, EAffinity affinity, EUserTriggered userTriggered) { const bool selectionHasDirection = true; SetSelectionOptions options = CloseTyping | ClearTypingStyle | userTriggered; setSelection(VisibleSelection(base, extent, affinity, selectionHasDirection), options); } void DragCaretController::setCaretPosition(const VisiblePosition& position) { if (Node* node = m_position.deepEquivalent().deprecatedNode()) invalidateCaretRect(node); m_position = position; setCaretRectNeedsUpdate(); Document* document = 0; if (Node* node = m_position.deepEquivalent().deprecatedNode()) { invalidateCaretRect(node); document = node->document(); } if (m_position.isNull() || m_position.isOrphan()) clearCaretRect(); else updateCaretRect(document, m_position); } static void adjustEndpointsAtBidiBoundary(VisiblePosition& visibleBase, VisiblePosition& visibleExtent) { RenderedPosition base(visibleBase); RenderedPosition extent(visibleExtent); if (base.isNull() || extent.isNull() || base.isEquivalent(extent)) return; if (base.atLeftBoundaryOfBidiRun()) { if (!extent.atRightBoundaryOfBidiRun(base.bidiLevelOnRight()) && base.isEquivalent(extent.leftBoundaryOfBidiRun(base.bidiLevelOnRight()))) { visibleBase = base.positionAtLeftBoundaryOfBiDiRun(); return; } return; } if (base.atRightBoundaryOfBidiRun()) { if (!extent.atLeftBoundaryOfBidiRun(base.bidiLevelOnLeft()) && base.isEquivalent(extent.rightBoundaryOfBidiRun(base.bidiLevelOnLeft()))) { visibleBase = base.positionAtRightBoundaryOfBiDiRun(); return; } return; } if (extent.atLeftBoundaryOfBidiRun() && extent.isEquivalent(base.leftBoundaryOfBidiRun(extent.bidiLevelOnRight()))) { visibleExtent = extent.positionAtLeftBoundaryOfBiDiRun(); return; } if (extent.atRightBoundaryOfBidiRun() && extent.isEquivalent(base.rightBoundaryOfBidiRun(extent.bidiLevelOnLeft()))) { visibleExtent = extent.positionAtRightBoundaryOfBiDiRun(); return; } } void FrameSelection::setNonDirectionalSelectionIfNeeded(const VisibleSelection& passedNewSelection, TextGranularity granularity, EndPointsAdjustmentMode endpointsAdjustmentMode) { VisibleSelection newSelection = passedNewSelection; bool isDirectional = shouldAlwaysUseDirectionalSelection(m_frame) || newSelection.isDirectional(); VisiblePosition base = m_originalBase.isNotNull() ? m_originalBase : newSelection.visibleBase(); VisiblePosition newBase = base; VisiblePosition newExtent = newSelection.visibleExtent(); if (endpointsAdjustmentMode == AdjustEndpointsAtBidiBoundary) adjustEndpointsAtBidiBoundary(newBase, newExtent); if (newBase != base || newExtent != newSelection.visibleExtent()) { m_originalBase = base; newSelection.setBase(newBase); newSelection.setExtent(newExtent); } else if (m_originalBase.isNotNull()) { if (m_selection.base() == newSelection.base()) newSelection.setBase(m_originalBase); m_originalBase.clear(); } newSelection.setIsDirectional(isDirectional); // Adjusting base and extent will make newSelection always directional if (m_selection == newSelection || !shouldChangeSelection(newSelection)) return; setSelection(newSelection, granularity); } void FrameSelection::setSelection(const VisibleSelection& newSelection, SetSelectionOptions options, CursorAlignOnScroll align, TextGranularity granularity) { bool closeTyping = options & CloseTyping; bool shouldClearTypingStyle = options & ClearTypingStyle; EUserTriggered userTriggered = selectionOptionsToUserTriggered(options); VisibleSelection s = newSelection; if (shouldAlwaysUseDirectionalSelection(m_frame)) s.setIsDirectional(true); if (!m_frame) { m_selection = s; return; } // : Infinite recursion at FrameSelection::setSelection // if document->frame() == m_frame we can get into an infinite loop if (s.base().anchorNode()) { Document* document = s.base().anchorNode()->document(); if (document && document->frame() && document->frame() != m_frame && document != m_frame->document()) { RefPtr guard = document->frame(); document->frame()->selection()->setSelection(s, options, align, granularity); // It's possible that during the above set selection, this FrameSelection has been modified by // selectFrameElementInParentIfFullySelected, but that the selection is no longer valid since // the frame is about to be destroyed. If this is the case, clear our selection. if (guard->hasOneRef() && !m_selection.isNonOrphanedCaretOrRange()) clear(); return; } } m_granularity = granularity; if (closeTyping) TypingCommand::closeTyping(m_frame); if (shouldClearTypingStyle) clearTypingStyle(); if (m_selection == s) { // Even if selection was not changed, selection offsets may have been changed. notifyRendererOfSelectionChange(userTriggered); return; } VisibleSelection oldSelection = m_selection; m_selection = s; setCaretRectNeedsUpdate(); if (!s.isNone() && !(options & DoNotSetFocus)) setFocusedElementIfNeeded(); if (!(options & DoNotUpdateAppearance)) { #if ENABLE(TEXT_CARET) m_frame->document()->updateLayoutIgnorePendingStylesheets(); #else m_frame->document()->updateStyleIfNeeded(); #endif updateAppearance(); } // Always clear the x position used for vertical arrow navigation. // It will be restored by the vertical arrow navigation code if necessary. m_xPosForVerticalArrowNavigation = NoXPosForVerticalArrowNavigation(); selectFrameElementInParentIfFullySelected(); notifyRendererOfSelectionChange(userTriggered); m_frame->editor().respondToChangedSelection(oldSelection, options); if (userTriggered == UserTriggered) { ScrollAlignment alignment; if (m_frame->editor().behavior().shouldCenterAlignWhenSelectionIsRevealed()) alignment = (align == AlignCursorOnScrollAlways) ? ScrollAlignment::alignCenterAlways : ScrollAlignment::alignCenterIfNeeded; else alignment = (align == AlignCursorOnScrollAlways) ? ScrollAlignment::alignTopAlways : ScrollAlignment::alignToEdgeIfNeeded; revealSelection(alignment, RevealExtent); } #if HAVE(ACCESSIBILITY) notifyAccessibilityForSelectionChange(); #endif m_frame->document()->enqueueDocumentEvent(Event::create(eventNames().selectionchangeEvent, false, false)); } static bool removingNodeRemovesPosition(Node* node, const Position& position) { if (!position.anchorNode()) return false; if (position.anchorNode() == node) return true; if (!node->isElementNode()) return false; Element* element = toElement(node); return element->containsIncludingShadowDOM(position.anchorNode()); } static void clearRenderViewSelection(const Position& position) { RefPtr document = position.anchorNode()->document(); document->updateStyleIfNeeded(); if (RenderView* view = document->renderView()) view->clearSelection(); } void DragCaretController::nodeWillBeRemoved(Node* node) { if (!hasCaret() || (node && !node->inDocument())) return; if (!removingNodeRemovesPosition(node, m_position.deepEquivalent())) return; clearRenderViewSelection(m_position.deepEquivalent()); clear(); } void FrameSelection::nodeWillBeRemoved(Node* node) { // There can't be a selection inside a fragment, so if a fragment's node is being removed, // the selection in the document that created the fragment needs no adjustment. if (isNone() || (node && !node->inDocument())) return; respondToNodeModification(node, removingNodeRemovesPosition(node, m_selection.base()), removingNodeRemovesPosition(node, m_selection.extent()), removingNodeRemovesPosition(node, m_selection.start()), removingNodeRemovesPosition(node, m_selection.end())); } void FrameSelection::respondToNodeModification(Node* node, bool baseRemoved, bool extentRemoved, bool startRemoved, bool endRemoved) { bool clearRenderTreeSelection = false; bool clearDOMTreeSelection = false; if (startRemoved || endRemoved) { Position start = m_selection.start(); Position end = m_selection.end(); if (startRemoved) updatePositionForNodeRemoval(start, node); if (endRemoved) updatePositionForNodeRemoval(end, node); if (start.isNotNull() && end.isNotNull()) { if (m_selection.isBaseFirst()) m_selection.setWithoutValidation(start, end); else m_selection.setWithoutValidation(end, start); } else clearDOMTreeSelection = true; clearRenderTreeSelection = true; } else if (baseRemoved || extentRemoved) { // The base and/or extent are about to be removed, but the start and end aren't. // Change the base and extent to the start and end, but don't re-validate the // selection, since doing so could move the start and end into the node // that is about to be removed. if (m_selection.isBaseFirst()) m_selection.setWithoutValidation(m_selection.start(), m_selection.end()); else m_selection.setWithoutValidation(m_selection.end(), m_selection.start()); } else if (RefPtr range = m_selection.firstRange()) { ExceptionCode ec = 0; Range::CompareResults compareResult = range->compareNode(node, ec); if (!ec && (compareResult == Range::NODE_BEFORE_AND_AFTER || compareResult == Range::NODE_INSIDE)) { // If we did nothing here, when this node's renderer was destroyed, the rect that it // occupied would be invalidated, but, selection gaps that change as a result of // the removal wouldn't be invalidated. // FIXME: Don't do so much unnecessary invalidation. clearRenderTreeSelection = true; } } if (clearRenderTreeSelection) clearRenderViewSelection(m_selection.start()); if (clearDOMTreeSelection) setSelection(VisibleSelection(), DoNotSetFocus); } static void updatePositionAfterAdoptingTextReplacement(Position& position, CharacterData* node, unsigned offset, unsigned oldLength, unsigned newLength) { if (!position.anchorNode() || position.anchorNode() != node || position.anchorType() != Position::PositionIsOffsetInAnchor) return; // See: http://www.w3.org/TR/DOM-Level-2-Traversal-Range/ranges.html#Level-2-Range-Mutation ASSERT(position.offsetInContainerNode() >= 0); unsigned positionOffset = static_cast(position.offsetInContainerNode()); // Replacing text can be viewed as a deletion followed by insertion. if (positionOffset >= offset && positionOffset <= offset + oldLength) position.moveToOffset(offset); // Adjust the offset if the position is after the end of the deleted contents // (positionOffset > offset + oldLength) to avoid having a stale offset. if (positionOffset > offset + oldLength) position.moveToOffset(positionOffset - oldLength + newLength); ASSERT(static_cast(position.offsetInContainerNode()) <= node->length()); } static inline bool nodeIsDetachedFromDocument(Node* node) { ASSERT(node); Node* highest = highestAncestor(node); return highest->nodeType() == Node::DOCUMENT_FRAGMENT_NODE && !highest->isShadowRoot(); } void FrameSelection::textWasReplaced(CharacterData* node, unsigned offset, unsigned oldLength, unsigned newLength) { // The fragment check is a performance optimization. See http://trac.webkit.org/changeset/30062. if (isNone() || !node || nodeIsDetachedFromDocument(node)) return; Position base = m_selection.base(); Position extent = m_selection.extent(); Position start = m_selection.start(); Position end = m_selection.end(); updatePositionAfterAdoptingTextReplacement(base, node, offset, oldLength, newLength); updatePositionAfterAdoptingTextReplacement(extent, node, offset, oldLength, newLength); updatePositionAfterAdoptingTextReplacement(start, node, offset, oldLength, newLength); updatePositionAfterAdoptingTextReplacement(end, node, offset, oldLength, newLength); if (base != m_selection.base() || extent != m_selection.extent() || start != m_selection.start() || end != m_selection.end()) { VisibleSelection newSelection; newSelection.setWithoutValidation(base, extent); m_frame->document()->updateLayout(); setSelection(newSelection, DoNotSetFocus); } } TextDirection FrameSelection::directionOfEnclosingBlock() { return WebCore::directionOfEnclosingBlock(m_selection.extent()); } TextDirection FrameSelection::directionOfSelection() { InlineBox* startBox = 0; InlineBox* endBox = 0; int unusedOffset; if (m_selection.start().isNotNull()) m_selection.visibleStart().getInlineBoxAndOffset(startBox, unusedOffset); if (m_selection.end().isNotNull()) m_selection.visibleEnd().getInlineBoxAndOffset(endBox, unusedOffset); if (startBox && endBox && startBox->direction() == endBox->direction()) return startBox->direction(); return directionOfEnclosingBlock(); } void FrameSelection::willBeModified(EAlteration alter, SelectionDirection direction) { if (alter != AlterationExtend) return; Position start = m_selection.start(); Position end = m_selection.end(); bool baseIsStart = true; if (m_selection.isDirectional()) { // Make base and extent match start and end so we extend the user-visible selection. // This only matters for cases where base and extend point to different positions than // start and end (e.g. after a double-click to select a word). if (m_selection.isBaseFirst()) baseIsStart = true; else baseIsStart = false; } else { switch (direction) { case DirectionRight: if (directionOfSelection() == LTR) baseIsStart = true; else baseIsStart = false; break; case DirectionForward: baseIsStart = true; break; case DirectionLeft: if (directionOfSelection() == LTR) baseIsStart = false; else baseIsStart = true; break; case DirectionBackward: baseIsStart = false; break; } } if (baseIsStart) { m_selection.setBase(start); m_selection.setExtent(end); } else { m_selection.setBase(end); m_selection.setExtent(start); } } VisiblePosition FrameSelection::positionForPlatform(bool isGetStart) const { Settings* settings = m_frame ? m_frame->settings() : 0; if (settings && settings->editingBehaviorType() == EditingMacBehavior) return isGetStart ? m_selection.visibleStart() : m_selection.visibleEnd(); // Linux and Windows always extend selections from the extent endpoint. // FIXME: VisibleSelection should be fixed to ensure as an invariant that // base/extent always point to the same nodes as start/end, but which points // to which depends on the value of isBaseFirst. Then this can be changed // to just return m_sel.extent(). return m_selection.isBaseFirst() ? m_selection.visibleEnd() : m_selection.visibleStart(); } VisiblePosition FrameSelection::startForPlatform() const { return positionForPlatform(true); } VisiblePosition FrameSelection::endForPlatform() const { return positionForPlatform(false); } VisiblePosition FrameSelection::nextWordPositionForPlatform(const VisiblePosition &originalPosition) { VisiblePosition positionAfterCurrentWord = nextWordPosition(originalPosition); if (m_frame && m_frame->editor().behavior().shouldSkipSpaceWhenMovingRight()) { // In order to skip spaces when moving right, we advance one // word further and then move one word back. Given the // semantics of previousWordPosition() this will put us at the // beginning of the word following. VisiblePosition positionAfterSpacingAndFollowingWord = nextWordPosition(positionAfterCurrentWord); if (positionAfterSpacingAndFollowingWord != positionAfterCurrentWord) positionAfterCurrentWord = previousWordPosition(positionAfterSpacingAndFollowingWord); bool movingBackwardsMovedPositionToStartOfCurrentWord = positionAfterCurrentWord == previousWordPosition(nextWordPosition(originalPosition)); if (movingBackwardsMovedPositionToStartOfCurrentWord) positionAfterCurrentWord = positionAfterSpacingAndFollowingWord; } return positionAfterCurrentWord; } #if ENABLE(USERSELECT_ALL) static void adjustPositionForUserSelectAll(VisiblePosition& pos, bool isForward) { if (Node* rootUserSelectAll = Position::rootUserSelectAllForNode(pos.deepEquivalent().anchorNode())) pos = isForward ? positionAfterNode(rootUserSelectAll).downstream(CanCrossEditingBoundary) : positionBeforeNode(rootUserSelectAll).upstream(CanCrossEditingBoundary); } #endif VisiblePosition FrameSelection::modifyExtendingRight(TextGranularity granularity) { VisiblePosition pos(m_selection.extent(), m_selection.affinity()); // The difference between modifyExtendingRight and modifyExtendingForward is: // modifyExtendingForward always extends forward logically. // modifyExtendingRight behaves the same as modifyExtendingForward except for extending character or word, // it extends forward logically if the enclosing block is LTR direction, // but it extends backward logically if the enclosing block is RTL direction. switch (granularity) { case CharacterGranularity: if (directionOfEnclosingBlock() == LTR) pos = pos.next(CannotCrossEditingBoundary); else pos = pos.previous(CannotCrossEditingBoundary); break; case WordGranularity: if (directionOfEnclosingBlock() == LTR) pos = nextWordPositionForPlatform(pos); else pos = previousWordPosition(pos); break; case LineBoundary: if (directionOfEnclosingBlock() == LTR) pos = modifyExtendingForward(granularity); else pos = modifyExtendingBackward(granularity); break; case SentenceGranularity: case LineGranularity: case ParagraphGranularity: case SentenceBoundary: case ParagraphBoundary: case DocumentBoundary: // FIXME: implement all of the above? pos = modifyExtendingForward(granularity); break; } #if ENABLE(USERSELECT_ALL) adjustPositionForUserSelectAll(pos, directionOfEnclosingBlock() == LTR); #endif return pos; } VisiblePosition FrameSelection::modifyExtendingForward(TextGranularity granularity) { VisiblePosition pos(m_selection.extent(), m_selection.affinity()); switch (granularity) { case CharacterGranularity: pos = pos.next(CannotCrossEditingBoundary); break; case WordGranularity: pos = nextWordPositionForPlatform(pos); break; case SentenceGranularity: pos = nextSentencePosition(pos); break; case LineGranularity: pos = nextLinePosition(pos, lineDirectionPointForBlockDirectionNavigation(EXTENT)); break; case ParagraphGranularity: pos = nextParagraphPosition(pos, lineDirectionPointForBlockDirectionNavigation(EXTENT)); break; case SentenceBoundary: pos = endOfSentence(endForPlatform()); break; case LineBoundary: pos = logicalEndOfLine(endForPlatform()); break; case ParagraphBoundary: pos = endOfParagraph(endForPlatform()); break; case DocumentBoundary: pos = endForPlatform(); if (isEditablePosition(pos.deepEquivalent())) pos = endOfEditableContent(pos); else pos = endOfDocument(pos); break; } #if ENABLE(USERSELECT_ALL) adjustPositionForUserSelectAll(pos, directionOfEnclosingBlock() == LTR); #endif return pos; } VisiblePosition FrameSelection::modifyMovingRight(TextGranularity granularity) { VisiblePosition pos; switch (granularity) { case CharacterGranularity: if (isRange()) { if (directionOfSelection() == LTR) pos = VisiblePosition(m_selection.end(), m_selection.affinity()); else pos = VisiblePosition(m_selection.start(), m_selection.affinity()); } else pos = VisiblePosition(m_selection.extent(), m_selection.affinity()).right(true); break; case WordGranularity: { #if USE(ICU_UNICODE) // Visual word movement relies on isWordTextBreak which is not implemented in WinCE and QT. // https://bugs.webkit.org/show_bug.cgi?id=81136. bool skipsSpaceWhenMovingRight = m_frame && m_frame->editor().behavior().shouldSkipSpaceWhenMovingRight(); pos = rightWordPosition(VisiblePosition(m_selection.extent(), m_selection.affinity()), skipsSpaceWhenMovingRight); break; #endif } case SentenceGranularity: case LineGranularity: case ParagraphGranularity: case SentenceBoundary: case ParagraphBoundary: case DocumentBoundary: // FIXME: Implement all of the above. pos = modifyMovingForward(granularity); break; case LineBoundary: pos = rightBoundaryOfLine(startForPlatform(), directionOfEnclosingBlock()); break; } return pos; } VisiblePosition FrameSelection::modifyMovingForward(TextGranularity granularity) { VisiblePosition pos; // FIXME: Stay in editable content for the less common granularities. switch (granularity) { case CharacterGranularity: if (isRange()) pos = VisiblePosition(m_selection.end(), m_selection.affinity()); else pos = VisiblePosition(m_selection.extent(), m_selection.affinity()).next(CannotCrossEditingBoundary); break; case WordGranularity: pos = nextWordPositionForPlatform(VisiblePosition(m_selection.extent(), m_selection.affinity())); break; case SentenceGranularity: pos = nextSentencePosition(VisiblePosition(m_selection.extent(), m_selection.affinity())); break; case LineGranularity: { // down-arrowing from a range selection that ends at the start of a line needs // to leave the selection at that line start (no need to call nextLinePosition!) pos = endForPlatform(); if (!isRange() || !isStartOfLine(pos)) pos = nextLinePosition(pos, lineDirectionPointForBlockDirectionNavigation(START)); break; } case ParagraphGranularity: pos = nextParagraphPosition(endForPlatform(), lineDirectionPointForBlockDirectionNavigation(START)); break; case SentenceBoundary: pos = endOfSentence(endForPlatform()); break; case LineBoundary: pos = logicalEndOfLine(endForPlatform()); break; case ParagraphBoundary: pos = endOfParagraph(endForPlatform()); break; case DocumentBoundary: pos = endForPlatform(); if (isEditablePosition(pos.deepEquivalent())) pos = endOfEditableContent(pos); else pos = endOfDocument(pos); break; } return pos; } VisiblePosition FrameSelection::modifyExtendingLeft(TextGranularity granularity) { VisiblePosition pos(m_selection.extent(), m_selection.affinity()); // The difference between modifyExtendingLeft and modifyExtendingBackward is: // modifyExtendingBackward always extends backward logically. // modifyExtendingLeft behaves the same as modifyExtendingBackward except for extending character or word, // it extends backward logically if the enclosing block is LTR direction, // but it extends forward logically if the enclosing block is RTL direction. switch (granularity) { case CharacterGranularity: if (directionOfEnclosingBlock() == LTR) pos = pos.previous(CannotCrossEditingBoundary); else pos = pos.next(CannotCrossEditingBoundary); break; case WordGranularity: if (directionOfEnclosingBlock() == LTR) pos = previousWordPosition(pos); else pos = nextWordPositionForPlatform(pos); break; case LineBoundary: if (directionOfEnclosingBlock() == LTR) pos = modifyExtendingBackward(granularity); else pos = modifyExtendingForward(granularity); break; case SentenceGranularity: case LineGranularity: case ParagraphGranularity: case SentenceBoundary: case ParagraphBoundary: case DocumentBoundary: pos = modifyExtendingBackward(granularity); break; } #if ENABLE(USERSELECT_ALL) adjustPositionForUserSelectAll(pos, !(directionOfEnclosingBlock() == LTR)); #endif return pos; } VisiblePosition FrameSelection::modifyExtendingBackward(TextGranularity granularity) { VisiblePosition pos(m_selection.extent(), m_selection.affinity()); // Extending a selection backward by word or character from just after a table selects // the table. This "makes sense" from the user perspective, esp. when deleting. // It was done here instead of in VisiblePosition because we want VPs to iterate // over everything. switch (granularity) { case CharacterGranularity: pos = pos.previous(CannotCrossEditingBoundary); break; case WordGranularity: pos = previousWordPosition(pos); break; case SentenceGranularity: pos = previousSentencePosition(pos); break; case LineGranularity: pos = previousLinePosition(pos, lineDirectionPointForBlockDirectionNavigation(EXTENT)); break; case ParagraphGranularity: pos = previousParagraphPosition(pos, lineDirectionPointForBlockDirectionNavigation(EXTENT)); break; case SentenceBoundary: pos = startOfSentence(startForPlatform()); break; case LineBoundary: pos = logicalStartOfLine(startForPlatform()); break; case ParagraphBoundary: pos = startOfParagraph(startForPlatform()); break; case DocumentBoundary: pos = startForPlatform(); if (isEditablePosition(pos.deepEquivalent())) pos = startOfEditableContent(pos); else pos = startOfDocument(pos); break; } #if ENABLE(USERSELECT_ALL) adjustPositionForUserSelectAll(pos, !(directionOfEnclosingBlock() == LTR)); #endif return pos; } VisiblePosition FrameSelection::modifyMovingLeft(TextGranularity granularity) { VisiblePosition pos; switch (granularity) { case CharacterGranularity: if (isRange()) if (directionOfSelection() == LTR) pos = VisiblePosition(m_selection.start(), m_selection.affinity()); else pos = VisiblePosition(m_selection.end(), m_selection.affinity()); else pos = VisiblePosition(m_selection.extent(), m_selection.affinity()).left(true); break; case WordGranularity: { #if USE(ICU_UNICODE) bool skipsSpaceWhenMovingRight = m_frame && m_frame->editor().behavior().shouldSkipSpaceWhenMovingRight(); pos = leftWordPosition(VisiblePosition(m_selection.extent(), m_selection.affinity()), skipsSpaceWhenMovingRight); break; #endif } case SentenceGranularity: case LineGranularity: case ParagraphGranularity: case SentenceBoundary: case ParagraphBoundary: case DocumentBoundary: // FIXME: Implement all of the above. pos = modifyMovingBackward(granularity); break; case LineBoundary: pos = leftBoundaryOfLine(startForPlatform(), directionOfEnclosingBlock()); break; } return pos; } VisiblePosition FrameSelection::modifyMovingBackward(TextGranularity granularity) { VisiblePosition pos; switch (granularity) { case CharacterGranularity: if (isRange()) pos = VisiblePosition(m_selection.start(), m_selection.affinity()); else pos = VisiblePosition(m_selection.extent(), m_selection.affinity()).previous(CannotCrossEditingBoundary); break; case WordGranularity: pos = previousWordPosition(VisiblePosition(m_selection.extent(), m_selection.affinity())); break; case SentenceGranularity: pos = previousSentencePosition(VisiblePosition(m_selection.extent(), m_selection.affinity())); break; case LineGranularity: pos = previousLinePosition(startForPlatform(), lineDirectionPointForBlockDirectionNavigation(START)); break; case ParagraphGranularity: pos = previousParagraphPosition(startForPlatform(), lineDirectionPointForBlockDirectionNavigation(START)); break; case SentenceBoundary: pos = startOfSentence(startForPlatform()); break; case LineBoundary: pos = logicalStartOfLine(startForPlatform()); break; case ParagraphBoundary: pos = startOfParagraph(startForPlatform()); break; case DocumentBoundary: pos = startForPlatform(); if (isEditablePosition(pos.deepEquivalent())) pos = startOfEditableContent(pos); else pos = startOfDocument(pos); break; } return pos; } static bool isBoundary(TextGranularity granularity) { return granularity == LineBoundary || granularity == ParagraphBoundary || granularity == DocumentBoundary; } bool FrameSelection::modify(EAlteration alter, SelectionDirection direction, TextGranularity granularity, EUserTriggered userTriggered) { if (userTriggered == UserTriggered) { FrameSelection trialFrameSelection; trialFrameSelection.setSelection(m_selection); trialFrameSelection.modify(alter, direction, granularity, NotUserTriggered); bool change = shouldChangeSelection(trialFrameSelection.selection()); if (!change) return false; if (trialFrameSelection.selection().isRange() && m_selection.isCaret() && !dispatchSelectStart()) return false; } willBeModified(alter, direction); bool wasRange = m_selection.isRange(); Position originalStartPosition = m_selection.start(); VisiblePosition position; switch (direction) { case DirectionRight: if (alter == AlterationMove) position = modifyMovingRight(granularity); else position = modifyExtendingRight(granularity); break; case DirectionForward: if (alter == AlterationExtend) position = modifyExtendingForward(granularity); else position = modifyMovingForward(granularity); break; case DirectionLeft: if (alter == AlterationMove) position = modifyMovingLeft(granularity); else position = modifyExtendingLeft(granularity); break; case DirectionBackward: if (alter == AlterationExtend) position = modifyExtendingBackward(granularity); else position = modifyMovingBackward(granularity); break; } if (position.isNull()) return false; if (isSpatialNavigationEnabled(m_frame)) if (!wasRange && alter == AlterationMove && position == originalStartPosition) return false; // Some of the above operations set an xPosForVerticalArrowNavigation. // Setting a selection will clear it, so save it to possibly restore later. // Note: the START position type is arbitrary because it is unused, it would be // the requested position type if there were no xPosForVerticalArrowNavigation set. LayoutUnit x = lineDirectionPointForBlockDirectionNavigation(START); m_selection.setIsDirectional(shouldAlwaysUseDirectionalSelection(m_frame) || alter == AlterationExtend); switch (alter) { case AlterationMove: moveTo(position, userTriggered); break; case AlterationExtend: if (!m_selection.isCaret() && (granularity == WordGranularity || granularity == ParagraphGranularity || granularity == LineGranularity) && m_frame && !m_frame->editor().behavior().shouldExtendSelectionByWordOrLineAcrossCaret()) { // Don't let the selection go across the base position directly. Needed to match mac // behavior when, for instance, word-selecting backwards starting with the caret in // the middle of a word and then word-selecting forward, leaving the caret in the // same place where it was, instead of directly selecting to the end of the word. VisibleSelection newSelection = m_selection; newSelection.setExtent(position); if (m_selection.isBaseFirst() != newSelection.isBaseFirst()) position = m_selection.base(); } // Standard Mac behavior when extending to a boundary is grow the selection rather than leaving the // base in place and moving the extent. Matches NSTextView. if (!m_frame || !m_frame->editor().behavior().shouldAlwaysGrowSelectionWhenExtendingToBoundary() || m_selection.isCaret() || !isBoundary(granularity)) setExtent(position, userTriggered); else { TextDirection textDirection = directionOfEnclosingBlock(); if (direction == DirectionForward || (textDirection == LTR && direction == DirectionRight) || (textDirection == RTL && direction == DirectionLeft)) setEnd(position, userTriggered); else setStart(position, userTriggered); } break; } if (granularity == LineGranularity || granularity == ParagraphGranularity) m_xPosForVerticalArrowNavigation = x; if (userTriggered == UserTriggered) m_granularity = CharacterGranularity; setCaretRectNeedsUpdate(); return true; } // FIXME: Maybe baseline would be better? static bool absoluteCaretY(const VisiblePosition &c, int &y) { IntRect rect = c.absoluteCaretBounds(); if (rect.isEmpty()) return false; y = rect.y() + rect.height() / 2; return true; } bool FrameSelection::modify(EAlteration alter, unsigned verticalDistance, VerticalDirection direction, EUserTriggered userTriggered, CursorAlignOnScroll align) { if (!verticalDistance) return false; if (userTriggered == UserTriggered) { FrameSelection trialFrameSelection; trialFrameSelection.setSelection(m_selection); trialFrameSelection.modify(alter, verticalDistance, direction, NotUserTriggered); bool change = shouldChangeSelection(trialFrameSelection.selection()); if (!change) return false; } willBeModified(alter, direction == DirectionUp ? DirectionBackward : DirectionForward); VisiblePosition pos; LayoutUnit xPos = 0; switch (alter) { case AlterationMove: pos = VisiblePosition(direction == DirectionUp ? m_selection.start() : m_selection.end(), m_selection.affinity()); xPos = lineDirectionPointForBlockDirectionNavigation(direction == DirectionUp ? START : END); m_selection.setAffinity(direction == DirectionUp ? UPSTREAM : DOWNSTREAM); break; case AlterationExtend: pos = VisiblePosition(m_selection.extent(), m_selection.affinity()); xPos = lineDirectionPointForBlockDirectionNavigation(EXTENT); m_selection.setAffinity(DOWNSTREAM); break; } int startY; if (!absoluteCaretY(pos, startY)) return false; if (direction == DirectionUp) startY = -startY; int lastY = startY; VisiblePosition result; VisiblePosition next; for (VisiblePosition p = pos; ; p = next) { if (direction == DirectionUp) next = previousLinePosition(p, xPos); else next = nextLinePosition(p, xPos); if (next.isNull() || next == p) break; int nextY; if (!absoluteCaretY(next, nextY)) break; if (direction == DirectionUp) nextY = -nextY; if (nextY - startY > static_cast(verticalDistance)) break; if (nextY >= lastY) { lastY = nextY; result = next; } } if (result.isNull()) return false; switch (alter) { case AlterationMove: moveTo(result, userTriggered, align); break; case AlterationExtend: setExtent(result, userTriggered); break; } if (userTriggered == UserTriggered) m_granularity = CharacterGranularity; m_selection.setIsDirectional(shouldAlwaysUseDirectionalSelection(m_frame) || alter == AlterationExtend); return true; } LayoutUnit FrameSelection::lineDirectionPointForBlockDirectionNavigation(EPositionType type) { LayoutUnit x = 0; if (isNone()) return x; Position pos; switch (type) { case START: pos = m_selection.start(); break; case END: pos = m_selection.end(); break; case BASE: pos = m_selection.base(); break; case EXTENT: pos = m_selection.extent(); break; } Frame* frame = pos.anchorNode()->document()->frame(); if (!frame) return x; if (m_xPosForVerticalArrowNavigation == NoXPosForVerticalArrowNavigation()) { VisiblePosition visiblePosition(pos, m_selection.affinity()); // VisiblePosition creation can fail here if a node containing the selection becomes visibility:hidden // after the selection is created and before this function is called. x = visiblePosition.isNotNull() ? visiblePosition.lineDirectionPointForBlockDirectionNavigation() : 0; m_xPosForVerticalArrowNavigation = x; } else x = m_xPosForVerticalArrowNavigation; return x; } void FrameSelection::clear() { m_granularity = CharacterGranularity; setSelection(VisibleSelection()); } void FrameSelection::prepareForDestruction() { m_granularity = CharacterGranularity; #if ENABLE(TEXT_CARET) m_caretBlinkTimer.stop(); #endif RenderView* view = m_frame->contentRenderer(); if (view) view->clearSelection(); setSelection(VisibleSelection(), CloseTyping | ClearTypingStyle | DoNotUpdateAppearance); m_previousCaretNode.clear(); } void FrameSelection::setStart(const VisiblePosition &pos, EUserTriggered trigger) { if (m_selection.isBaseFirst()) setBase(pos, trigger); else setExtent(pos, trigger); } void FrameSelection::setEnd(const VisiblePosition &pos, EUserTriggered trigger) { if (m_selection.isBaseFirst()) setExtent(pos, trigger); else setBase(pos, trigger); } void FrameSelection::setBase(const VisiblePosition &pos, EUserTriggered userTriggered) { const bool selectionHasDirection = true; setSelection(VisibleSelection(pos.deepEquivalent(), m_selection.extent(), pos.affinity(), selectionHasDirection), CloseTyping | ClearTypingStyle | userTriggered); } void FrameSelection::setExtent(const VisiblePosition &pos, EUserTriggered userTriggered) { const bool selectionHasDirection = true; setSelection(VisibleSelection(m_selection.base(), pos.deepEquivalent(), pos.affinity(), selectionHasDirection), CloseTyping | ClearTypingStyle | userTriggered); } void FrameSelection::setBase(const Position &pos, EAffinity affinity, EUserTriggered userTriggered) { const bool selectionHasDirection = true; setSelection(VisibleSelection(pos, m_selection.extent(), affinity, selectionHasDirection), CloseTyping | ClearTypingStyle | userTriggered); } void FrameSelection::setExtent(const Position &pos, EAffinity affinity, EUserTriggered userTriggered) { const bool selectionHasDirection = true; setSelection(VisibleSelection(m_selection.base(), pos, affinity, selectionHasDirection), CloseTyping | ClearTypingStyle | userTriggered); } void CaretBase::clearCaretRect() { m_caretLocalRect = LayoutRect(); } static inline bool caretRendersInsideNode(Node* node) { return node && !isTableElement(node) && !editingIgnoresContent(node); } static RenderObject* caretRenderer(Node* node) { if (!node) return 0; RenderObject* renderer = node->renderer(); if (!renderer) return 0; // if caretNode is a block and caret is inside it then caret should be painted by that block bool paintedByBlock = renderer->isBlockFlow() && caretRendersInsideNode(node); return paintedByBlock ? renderer : renderer->containingBlock(); } bool CaretBase::updateCaretRect(Document* document, const VisiblePosition& caretPosition) { document->updateStyleIfNeeded(); m_caretLocalRect = LayoutRect(); m_caretRectNeedsUpdate = false; if (caretPosition.isNull()) return false; ASSERT(caretPosition.deepEquivalent().deprecatedNode()->renderer()); // First compute a rect local to the renderer at the selection start. RenderObject* renderer; LayoutRect localRect = caretPosition.localCaretRect(renderer); // Get the renderer that will be responsible for painting the caret // (which is either the renderer we just found, or one of its containers). RenderObject* caretPainter = caretRenderer(caretPosition.deepEquivalent().deprecatedNode()); // Compute an offset between the renderer and the caretPainter. bool unrooted = false; while (renderer != caretPainter) { RenderObject* containerObject = renderer->container(); if (!containerObject) { unrooted = true; break; } localRect.move(renderer->offsetFromContainer(containerObject, localRect.location())); renderer = containerObject; } if (!unrooted) m_caretLocalRect = localRect; return true; } RenderObject* FrameSelection::caretRenderer() const { return WebCore::caretRenderer(m_selection.start().deprecatedNode()); } RenderObject* DragCaretController::caretRenderer() const { return WebCore::caretRenderer(m_position.deepEquivalent().deprecatedNode()); } static bool isNonOrphanedCaret(const VisibleSelection& selection) { return selection.isCaret() && !selection.start().isOrphan() && !selection.end().isOrphan(); } LayoutRect FrameSelection::localCaretRect() { if (shouldUpdateCaretRect()) { if (!isNonOrphanedCaret(m_selection)) clearCaretRect(); else if (updateCaretRect(m_frame->document(), VisiblePosition(m_selection.start(), m_selection.affinity()))) m_absCaretBoundsDirty = true; } return localCaretRectWithoutUpdate(); } IntRect CaretBase::absoluteBoundsForLocalRect(Node* node, const LayoutRect& rect) const { RenderObject* caretPainter = caretRenderer(node); if (!caretPainter) return IntRect(); LayoutRect localRect(rect); if (caretPainter->isBox()) toRenderBox(caretPainter)->flipForWritingMode(localRect); return caretPainter->localToAbsoluteQuad(FloatRect(localRect)).enclosingBoundingBox(); } IntRect FrameSelection::absoluteCaretBounds() { recomputeCaretRect(); return m_absCaretBounds; } static void repaintCaretForLocalRect(Node* node, const LayoutRect& rect) { RenderObject* caretPainter = caretRenderer(node); if (!caretPainter) return; caretPainter->repaintRectangle(rect); } bool FrameSelection::recomputeCaretRect() { if (!shouldUpdateCaretRect()) return false; if (!m_frame) return false; FrameView* v = m_frame->document()->view(); if (!v) return false; Node* caretNode = m_selection.start().deprecatedNode(); LayoutRect oldRect = localCaretRectWithoutUpdate(); LayoutRect newRect = localCaretRect(); if (caretNode == m_previousCaretNode && oldRect == newRect && !m_absCaretBoundsDirty) return false; IntRect oldAbsCaretBounds = m_absCaretBounds; m_absCaretBounds = absoluteBoundsForLocalRect(caretNode, localCaretRectWithoutUpdate()); m_absCaretBoundsDirty = false; if (caretNode == m_previousCaretNode && oldAbsCaretBounds == m_absCaretBounds) return false; #if ENABLE(TEXT_CARET) if (RenderView* view = m_frame->document()->renderView()) { bool previousOrNewCaretNodeIsContentEditable = isContentEditable() || (m_previousCaretNode && m_previousCaretNode->isContentEditable()); if (shouldRepaintCaret(view, previousOrNewCaretNodeIsContentEditable)) { if (m_previousCaretNode) repaintCaretForLocalRect(m_previousCaretNode.get(), oldRect); m_previousCaretNode = caretNode; repaintCaretForLocalRect(caretNode, newRect); } } #endif return true; } bool CaretBase::shouldRepaintCaret(const RenderView* view, bool isContentEditable) const { ASSERT(view); Frame* frame = view->frameView() ? view->frameView()->frame() : 0; // The frame where the selection started. bool caretBrowsing = frame && frame->settings() && frame->settings()->caretBrowsingEnabled(); return (caretBrowsing || isContentEditable); } void FrameSelection::invalidateCaretRect() { if (!isCaret()) return; CaretBase::invalidateCaretRect(m_selection.start().deprecatedNode(), recomputeCaretRect()); } void CaretBase::invalidateCaretRect(Node* node, bool caretRectChanged) { // EDIT FIXME: This is an unfortunate hack. // Basically, we can't trust this layout position since we // can't guarantee that the check to see if we are in unrendered // content will work at this point. We may have to wait for // a layout and re-render of the document to happen. So, resetting this // flag will cause another caret layout to happen the first time // that we try to paint the caret after this call. That one will work since // it happens after the document has accounted for any editing // changes which may have been done. // And, we need to leave this layout here so the caret moves right // away after clicking. m_caretRectNeedsUpdate = true; if (caretRectChanged) return; if (RenderView* view = node->document()->renderView()) { if (shouldRepaintCaret(view, node->isContentEditable(Node::UserSelectAllIsAlwaysNonEditable))) repaintCaretForLocalRect(node, localCaretRectWithoutUpdate()); } } void FrameSelection::paintCaret(GraphicsContext* context, const LayoutPoint& paintOffset, const LayoutRect& clipRect) { if (m_selection.isCaret() && m_caretPaint) CaretBase::paintCaret(m_selection.start().deprecatedNode(), context, paintOffset, clipRect); } void CaretBase::paintCaret(Node* node, GraphicsContext* context, const LayoutPoint& paintOffset, const LayoutRect& clipRect) const { #if ENABLE(TEXT_CARET) if (m_caretVisibility == Hidden) return; LayoutRect drawingRect = localCaretRectWithoutUpdate(); RenderObject* renderer = caretRenderer(node); if (renderer && renderer->isBox()) toRenderBox(renderer)->flipForWritingMode(drawingRect); drawingRect.moveBy(roundedIntPoint(paintOffset)); LayoutRect caret = intersection(drawingRect, clipRect); if (caret.isEmpty()) return; Color caretColor = Color::black; ColorSpace colorSpace = ColorSpaceDeviceRGB; Element* element = node->isElementNode() ? toElement(node) : node->parentElement(); if (element && element->renderer()) { caretColor = element->renderer()->style()->visitedDependentColor(CSSPropertyColor); colorSpace = element->renderer()->style()->colorSpace(); } context->fillRect(caret, caretColor, colorSpace); #else UNUSED_PARAM(node); UNUSED_PARAM(context); UNUSED_PARAM(paintOffset); UNUSED_PARAM(clipRect); #endif } void FrameSelection::debugRenderer(RenderObject *r, bool selected) const { if (r->node()->isElementNode()) { Element* element = static_cast(r->node()); fprintf(stderr, "%s%s\n", selected ? "==> " : " ", element->localName().string().utf8().data()); } else if (r->isText()) { RenderText* textRenderer = toRenderText(r); if (!textRenderer->textLength() || !textRenderer->firstTextBox()) { fprintf(stderr, "%s#text (empty)\n", selected ? "==> " : " "); return; } static const int max = 36; String text = textRenderer->text(); int textLength = text.length(); if (selected) { int offset = 0; if (r->node() == m_selection.start().containerNode()) offset = m_selection.start().computeOffsetInContainerNode(); else if (r->node() == m_selection.end().containerNode()) offset = m_selection.end().computeOffsetInContainerNode(); int pos; InlineTextBox* box = textRenderer->findNextInlineTextBox(offset, pos); text = text.substring(box->start(), box->len()); String show; int mid = max / 2; int caret = 0; // text is shorter than max if (textLength < max) { show = text; caret = pos; } else if (pos - mid < 0) { // too few characters to left show = text.left(max - 3) + "..."; caret = pos; } else if (pos - mid >= 0 && pos + mid <= textLength) { // enough characters on each side show = "..." + text.substring(pos - mid + 3, max - 6) + "..."; caret = mid; } else { // too few characters on right show = "..." + text.right(max - 3); caret = pos - (textLength - show.length()); } show.replace('\n', ' '); show.replace('\r', ' '); fprintf(stderr, "==> #text : \"%s\" at offset %d\n", show.utf8().data(), pos); fprintf(stderr, " "); for (int i = 0; i < caret; i++) fprintf(stderr, " "); fprintf(stderr, "^\n"); } else { if ((int)text.length() > max) text = text.left(max - 3) + "..."; else text = text.left(max); fprintf(stderr, " #text : \"%s\"\n", text.utf8().data()); } } } bool FrameSelection::contains(const LayoutPoint& point) { Document* document = m_frame->document(); // Treat a collapsed selection like no selection. if (!isRange()) return false; if (!document->renderer()) return false; HitTestRequest request(HitTestRequest::ReadOnly | HitTestRequest::Active | HitTestRequest::DisallowShadowContent); HitTestResult result(point); document->renderView()->hitTest(request, result); Node* innerNode = result.innerNode(); if (!innerNode || !innerNode->renderer()) return false; VisiblePosition visiblePos(innerNode->renderer()->positionForPoint(result.localPoint())); if (visiblePos.isNull()) return false; if (m_selection.visibleStart().isNull() || m_selection.visibleEnd().isNull()) return false; Position start(m_selection.visibleStart().deepEquivalent()); Position end(m_selection.visibleEnd().deepEquivalent()); Position p(visiblePos.deepEquivalent()); return comparePositions(start, p) <= 0 && comparePositions(p, end) <= 0; } // Workaround for the fact that it's hard to delete a frame. // Call this after doing user-triggered selections to make it easy to delete the frame you entirely selected. // Can't do this implicitly as part of every setSelection call because in some contexts it might not be good // for the focus to move to another frame. So instead we call it from places where we are selecting with the // mouse or the keyboard after setting the selection. void FrameSelection::selectFrameElementInParentIfFullySelected() { // Find the parent frame; if there is none, then we have nothing to do. Frame* parent = m_frame->tree()->parent(); if (!parent) return; Page* page = m_frame->page(); if (!page) return; // Check if the selection contains the entire frame contents; if not, then there is nothing to do. if (!isRange()) return; if (!isStartOfDocument(selection().visibleStart())) return; if (!isEndOfDocument(selection().visibleEnd())) return; // Get to the