Fix inconsistent text selection behavior with option-shift-left/right/up/down.
[WebKit-https.git] / Source / WebCore / editing / FrameSelection.cpp
index 3f2e45f..539cc5b 100644 (file)
@@ -936,6 +936,19 @@ bool FrameSelection::modify(EAlteration alter, SelectionDirection direction, Tex
         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))