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

Side by Side Diff: third_party/WebKit/Source/core/editing/SelectionModifier.cpp

Issue 2708823003: Make SelecitonModifier class not to use VisibleSelection::setBase() and setExtent() (Closed)
Patch Set: 2017-02-21T16:54:44 Created 3 years, 10 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
« no previous file with comments | « third_party/WebKit/Source/core/editing/SelectionModifier.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010 Apple Inc. All rights 2 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010 Apple Inc. All rights
3 * reserved. 3 * reserved.
4 * 4 *
5 * Redistribution and use in source and binary forms, with or without 5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions 6 * modification, are permitted provided that the following conditions
7 * are met: 7 * are met:
8 * 1. Redistributions of source code must retain the above copyright 8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright 10 * 2. Redistributions in binary form must reproduce the above copyright
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
50 const VisibleSelection& selection, 50 const VisibleSelection& selection,
51 LayoutUnit xPosForVerticalArrowNavigation) 51 LayoutUnit xPosForVerticalArrowNavigation)
52 : m_frame(const_cast<LocalFrame*>(&frame)), 52 : m_frame(const_cast<LocalFrame*>(&frame)),
53 m_selection(selection), 53 m_selection(selection),
54 m_xPosForVerticalArrowNavigation(xPosForVerticalArrowNavigation) {} 54 m_xPosForVerticalArrowNavigation(xPosForVerticalArrowNavigation) {}
55 55
56 SelectionModifier::SelectionModifier(const LocalFrame& frame, 56 SelectionModifier::SelectionModifier(const LocalFrame& frame,
57 const VisibleSelection& selection) 57 const VisibleSelection& selection)
58 : SelectionModifier(frame, selection, NoXPosForVerticalArrowNavigation()) {} 58 : SelectionModifier(frame, selection, NoXPosForVerticalArrowNavigation()) {}
59 59
60 TextDirection SelectionModifier::directionOfEnclosingBlock() const { 60 static TextDirection directionOfEnclosingBlockOf(const Position& position) {
tkent 2017/02/21 23:34:20 What's the benefit of this function?
yosin_UTC9 2017/02/22 05:11:05 Oops, this function is useless. it may be introduc
61 return blink::directionOfEnclosingBlock(m_selection.extent()); 61 return directionOfEnclosingBlock(position);
62 } 62 }
63 63
64 TextDirection SelectionModifier::directionOfSelection() const { 64 TextDirection SelectionModifier::directionOfEnclosingBlock() const {
65 return directionOfEnclosingBlockOf(m_selection.extent());
66 }
67
68 static TextDirection directionOf(const VisibleSelection& visibleSelection) {
65 InlineBox* startBox = nullptr; 69 InlineBox* startBox = nullptr;
66 InlineBox* endBox = nullptr; 70 InlineBox* endBox = nullptr;
67 // Cache the VisiblePositions because visibleStart() and visibleEnd() 71 // Cache the VisiblePositions because visibleStart() and visibleEnd()
68 // can cause layout, which has the potential to invalidate lineboxes. 72 // can cause layout, which has the potential to invalidate lineboxes.
69 VisiblePosition startPosition = m_selection.visibleStart(); 73 const VisiblePosition startPosition = visibleSelection.visibleStart();
70 VisiblePosition endPosition = m_selection.visibleEnd(); 74 const VisiblePosition endPosition = visibleSelection.visibleEnd();
71 if (startPosition.isNotNull()) 75 if (startPosition.isNotNull())
72 startBox = computeInlineBoxPosition(startPosition).inlineBox; 76 startBox = computeInlineBoxPosition(startPosition).inlineBox;
73 if (endPosition.isNotNull()) 77 if (endPosition.isNotNull())
74 endBox = computeInlineBoxPosition(endPosition).inlineBox; 78 endBox = computeInlineBoxPosition(endPosition).inlineBox;
75 if (startBox && endBox && startBox->direction() == endBox->direction()) 79 if (startBox && endBox && startBox->direction() == endBox->direction())
76 return startBox->direction(); 80 return startBox->direction();
77 81
78 return directionOfEnclosingBlock(); 82 return directionOfEnclosingBlock(visibleSelection.extent());
79 } 83 }
80 84
81 void SelectionModifier::willBeModified(EAlteration alter, 85 TextDirection SelectionModifier::directionOfSelection() const {
tkent 2017/02/21 23:34:20 What's the benefit of this function?
Xiaocheng 2017/02/22 01:06:19 This function seems necessary as it's called by Se
tkent 2017/02/22 01:10:20 Oh, right. This is an existing function, and this
yosin_UTC9 2017/02/22 05:11:05 Thanks xiaochengh@ for explanation.
82 SelectionDirection direction) { 86 return directionOf(m_selection);
83 if (alter != FrameSelection::AlterationExtend) 87 }
84 return;
85 88
86 Position start = m_selection.start(); 89 static bool isBaseStart(const VisibleSelection& visibleSelection,
87 Position end = m_selection.end(); 90 SelectionDirection direction) {
88 91 if (visibleSelection.isDirectional()) {
89 bool baseIsStart = true;
90
91 if (m_selection.isDirectional()) {
92 // Make base and extent match start and end so we extend the user-visible 92 // Make base and extent match start and end so we extend the user-visible
93 // selection. This only matters for cases where base and extend point to 93 // selection. This only matters for cases where base and extend point to
94 // different positions than start and end (e.g. after a double-click to 94 // different positions than start and end (e.g. after a double-click to
95 // select a word). 95 // select a word).
96 if (m_selection.isBaseFirst()) 96 return visibleSelection.isBaseFirst();
97 baseIsStart = true;
98 else
99 baseIsStart = false;
100 } else {
101 switch (direction) {
102 case DirectionRight:
103 if (directionOfSelection() == TextDirection::kLtr)
104 baseIsStart = true;
105 else
106 baseIsStart = false;
107 break;
108 case DirectionForward:
109 baseIsStart = true;
110 break;
111 case DirectionLeft:
112 if (directionOfSelection() == TextDirection::kLtr)
113 baseIsStart = false;
114 else
115 baseIsStart = true;
116 break;
117 case DirectionBackward:
118 baseIsStart = false;
119 break;
120 }
121 } 97 }
122 if (baseIsStart) { 98 switch (direction) {
123 m_selection.setBase(start); 99 case DirectionRight:
124 m_selection.setExtent(end); 100 return directionOf(visibleSelection) == TextDirection::kLtr;
125 } else { 101 case DirectionForward:
126 m_selection.setBase(end); 102 return true;
127 m_selection.setExtent(start); 103 case DirectionLeft:
104 return directionOf(visibleSelection) != TextDirection::kLtr;
105 case DirectionBackward:
106 return false;
128 } 107 }
108 NOTREACHED() << "We should handle " << direction;
109 return true;
110 }
111
112 static SelectionInDOMTree prepareForExtend(
tkent 2017/02/21 23:34:20 nit: prepareForExtend ->prepareForExtent, prepareF
yosin_UTC9 2017/02/22 05:11:05 Done. Rename to |prepareToExtendSeelction()|
113 const VisibleSelection& visibleSelection,
114 SelectionDirection direction) {
115 if (visibleSelection.start().isNull())
116 return visibleSelection.asSelection();
117 const bool baseIsStart = isBaseStart(visibleSelection, direction);
118 return SelectionInDOMTree::Builder(visibleSelection.asSelection())
119 .collapse(baseIsStart ? visibleSelection.start() : visibleSelection.end())
120 .extend(baseIsStart ? visibleSelection.end() : visibleSelection.start())
121 .build();
129 } 122 }
130 123
131 VisiblePosition SelectionModifier::positionForPlatform(bool isGetStart) const { 124 VisiblePosition SelectionModifier::positionForPlatform(bool isGetStart) const {
132 Settings* settings = frame() ? frame()->settings() : nullptr; 125 Settings* settings = frame() ? frame()->settings() : nullptr;
133 if (settings && settings->getEditingBehaviorType() == EditingMacBehavior) 126 if (settings && settings->getEditingBehaviorType() == EditingMacBehavior)
134 return isGetStart ? m_selection.visibleStart() : m_selection.visibleEnd(); 127 return isGetStart ? m_selection.visibleStart() : m_selection.visibleEnd();
135 // Linux and Windows always extend selections from the extent endpoint. 128 // Linux and Windows always extend selections from the extent endpoint.
136 // FIXME: VisibleSelection should be fixed to ensure as an invariant that 129 // FIXME: VisibleSelection should be fixed to ensure as an invariant that
137 // base/extent always point to the same nodes as start/end, but which points 130 // base/extent always point to the same nodes as start/end, but which points
138 // to which depends on the value of isBaseFirst. Then this can be changed 131 // to which depends on the value of isBaseFirst. Then this can be changed
(...skipping 447 matching lines...) Expand 10 before | Expand all | Expand 10 after
586 selection->setExtent(newStart); 579 selection->setExtent(newStart);
587 } 580 }
588 581
589 bool SelectionModifier::modify(EAlteration alter, 582 bool SelectionModifier::modify(EAlteration alter,
590 SelectionDirection direction, 583 SelectionDirection direction,
591 TextGranularity granularity) { 584 TextGranularity granularity) {
592 DCHECK(!frame()->document()->needsLayoutTreeUpdate()); 585 DCHECK(!frame()->document()->needsLayoutTreeUpdate());
593 DocumentLifecycle::DisallowTransitionScope disallowTransition( 586 DocumentLifecycle::DisallowTransitionScope disallowTransition(
594 frame()->document()->lifecycle()); 587 frame()->document()->lifecycle());
595 588
596 willBeModified(alter, direction); 589 if (alter == FrameSelection::AlterationExtend) {
590 m_selection =
591 createVisibleSelection(prepareForExtend(m_selection, direction));
592 }
597 593
598 bool wasRange = m_selection.isRange(); 594 bool wasRange = m_selection.isRange();
599 VisiblePosition originalStartPosition = m_selection.visibleStart(); 595 VisiblePosition originalStartPosition = m_selection.visibleStart();
600 VisiblePosition position; 596 VisiblePosition position;
601 switch (direction) { 597 switch (direction) {
602 case DirectionRight: 598 case DirectionRight:
603 if (alter == FrameSelection::AlterationMove) 599 if (alter == FrameSelection::AlterationMove)
604 position = modifyMovingRight(granularity); 600 position = modifyMovingRight(granularity);
605 else 601 else
606 position = modifyExtendingRight(granularity); 602 position = modifyExtendingRight(granularity);
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
714 bool SelectionModifier::modifyWithPageGranularity(EAlteration alter, 710 bool SelectionModifier::modifyWithPageGranularity(EAlteration alter,
715 unsigned verticalDistance, 711 unsigned verticalDistance,
716 VerticalDirection direction) { 712 VerticalDirection direction) {
717 if (!verticalDistance) 713 if (!verticalDistance)
718 return false; 714 return false;
719 715
720 DCHECK(!frame()->document()->needsLayoutTreeUpdate()); 716 DCHECK(!frame()->document()->needsLayoutTreeUpdate());
721 DocumentLifecycle::DisallowTransitionScope disallowTransition( 717 DocumentLifecycle::DisallowTransitionScope disallowTransition(
722 frame()->document()->lifecycle()); 718 frame()->document()->lifecycle());
723 719
724 willBeModified(alter, direction == FrameSelection::DirectionUp 720 if (alter == FrameSelection::AlterationExtend) {
725 ? DirectionBackward 721 m_selection = createVisibleSelection(
726 : DirectionForward); 722 prepareForExtend(m_selection, direction == FrameSelection::DirectionUp
723 ? DirectionBackward
724 : DirectionForward));
725 }
727 726
728 VisiblePosition pos; 727 VisiblePosition pos;
729 LayoutUnit xPos; 728 LayoutUnit xPos;
730 switch (alter) { 729 switch (alter) {
731 case FrameSelection::AlterationMove: 730 case FrameSelection::AlterationMove:
732 pos = createVisiblePosition(direction == FrameSelection::DirectionUp 731 pos = createVisiblePosition(direction == FrameSelection::DirectionUp
733 ? m_selection.start() 732 ? m_selection.start()
734 : m_selection.end(), 733 : m_selection.end(),
735 m_selection.affinity()); 734 m_selection.affinity());
736 xPos = lineDirectionPointForBlockDirectionNavigation( 735 xPos = lineDirectionPointForBlockDirectionNavigation(
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after
863 x = lineDirectionPointForBlockDirectionNavigationOf(visiblePosition); 862 x = lineDirectionPointForBlockDirectionNavigationOf(visiblePosition);
864 m_xPosForVerticalArrowNavigation = x; 863 m_xPosForVerticalArrowNavigation = x;
865 } else { 864 } else {
866 x = m_xPosForVerticalArrowNavigation; 865 x = m_xPosForVerticalArrowNavigation;
867 } 866 }
868 867
869 return x; 868 return x;
870 } 869 }
871 870
872 } // namespace blink 871 } // namespace blink
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/core/editing/SelectionModifier.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698