Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(905)

Side by Side Diff: Source/core/editing/GranularityStrategy.cpp

Issue 988023005: Implementing directional selection strategy in Blink. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Putting GranularityStrategy into separate files. Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
yosin_UTC9 2015/04/21 01:59:51 nit: Let's use the short version of licence commen
mfomitchev 2015/04/21 18:44:25 Done.
2 * Copyright 2015, Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #include "config.h"
32 #include "core/editing/GranularityStrategy.h"
33
34 #include "core/editing/htmlediting.h"
35
36 namespace blink {
37
38 VisibleSelection CharacterGranularityStrategy::updateExtent(const VisiblePositio n& extentPosition, const VisibleSelection& selection)
39 {
40 return VisibleSelection(selection.visibleBase(), extentPosition);
41 }
42
43 DirectionGranularityStrategy::DirectionGranularityStrategy()
44 : m_granularity(CharacterGranularity)
45 , m_lastMoveShrunkSelection(false) { }
46
47 void DirectionGranularityStrategy::Clear()
48 {
49 m_granularity = CharacterGranularity;
50 m_lastMoveShrunkSelection = false;
51 }
52
53 VisiblePosition DirectionGranularityStrategy::nextWordBound(
54 const VisiblePosition& pos,
55 ESearchDirection direction,
56 EBoundAdjust wordBoundAdjust)
57 {
58 if (direction == ESearchDirection::SearchForward)
yosin_UTC9 2015/04/21 01:59:51 Q: How about using if-statement rather than ternar
mfomitchev 2015/04/21 18:44:25 I moved the ternary operator out and added a bool
59 return endOfWord(pos, wordBoundAdjust == EBoundAdjust::CurrentPosIfOnBo und ? LeftWordIfOnBoundary : RightWordIfOnBoundary);
60 return startOfWord(pos, wordBoundAdjust == EBoundAdjust::CurrentPosIfOnBoun d ? RightWordIfOnBoundary : LeftWordIfOnBoundary);
61 }
62
63 VisibleSelection DirectionGranularityStrategy::updateExtent(const VisiblePositio n& extentPosition, const VisibleSelection& selection)
64 {
65 if (extentPosition == selection.visibleExtent())
66 return selection;
67
68 const VisiblePosition base = selection.visibleBase();
69 const VisiblePosition oldExtentWithGranularity = selection.isBaseFirst() ? s election.visibleEnd() : selection.visibleStart();
70
71 int extentBaseOrder = comparePositions(extentPosition, base);
72 int oldExtentBaseOrder = comparePositions(oldExtentWithGranularity, base);
73
74 bool extentBaseOrderSwitched = (extentBaseOrder > 0 && oldExtentBaseOrder < 0)
75 || (extentBaseOrder < 0 && oldExtentBaseOrder > 0);
76
77 // Determine the boundary of the 'current word', i.e. the boundary extending beyond which
78 // should change the granularity to WordGranularity.
79 // If the last move has shrunk the selection and is now exactly on the word boundary -
80 // we need to take the next bound as the bound of the "current word".
81 VisiblePosition currentWordBoundary = nextWordBound(
82 oldExtentWithGranularity,
83 oldExtentBaseOrder > 0 ? ESearchDirection::SearchForward : ESearchDirect ion::SearchBackwards,
84 m_lastMoveShrunkSelection ? EBoundAdjust::NextBoundIfOnBound : EBoundAdj ust::CurrentPosIfOnBound);
85
86 bool thisMoveShrunkSelection = (extentBaseOrder > 0 && comparePositions(exte ntPosition, selection.visibleExtent()) < 0)
87 || (extentBaseOrder < 0 && comparePositions(extentPosition, selection.vi sibleExtent()) > 0);
88 // If the extent-base order was switched, then the selection is now expandin g in a different
89 // direction than before. Therefore we need to calculate the boundary of the 'current word'
90 // in this new direction in order to be able to tell if the selection expand ed beyond it.
91 if (extentBaseOrderSwitched) {
92 currentWordBoundary = nextWordBound(
93 base,
94 extentBaseOrder > 0 ? ESearchDirection::SearchForward : ESearchDirec tion::SearchBackwards,
95 EBoundAdjust::NextBoundIfOnBound);
96 m_granularity = CharacterGranularity;
97 // When the base/extent order switches it doesn't count as shrinking sel ection.
98 thisMoveShrunkSelection = false;
99 }
100
101 bool expandedBeyondWordBoundary;
102 if (extentBaseOrder > 0)
103 expandedBeyondWordBoundary = comparePositions(extentPosition, currentWor dBoundary) > 0;
104 else
105 expandedBeyondWordBoundary = comparePositions(extentPosition, currentWor dBoundary) < 0;
106 if (expandedBeyondWordBoundary) {
107 m_granularity = WordGranularity;
108 } else if (thisMoveShrunkSelection) {
109 m_granularity = CharacterGranularity;
110 m_lastMoveShrunkSelection = true;
111 }
112
113 m_lastMoveShrunkSelection = thisMoveShrunkSelection;
114 VisibleSelection newSelection = selection;
115 newSelection.setExtent(extentPosition);
116 if (m_granularity == WordGranularity) {
117 if (extentBaseOrder > 0)
118 newSelection.setEndRespectingGranularity(m_granularity, LeftWordIfOn Boundary);
119 else
120 newSelection.setStartRespectingGranularity(m_granularity, RightWordI fOnBoundary);
121 }
122
123 return newSelection;
124 }
125
126 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698