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

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

Issue 1630903005: Introduce SelectionAdjuster to adjust selections between DOM tree version and composed tree version (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: 2016-01-26T18:27:24 Update selection type in SelectionEditor Created 4 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 | « no previous file | third_party/WebKit/Source/core/editing/VisibleSelection.h » ('j') | 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, 2008, 2009, 2010 Apple Inc. All rights reserved. 2 * Copyright (C) 2004, 2008, 2009, 2010 Apple Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions 5 * modification, are permitted provided that the following conditions
6 * are met: 6 * are met:
7 * 1. Redistributions of source code must retain the above copyright 7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer. 8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright 9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the 10 * notice, this list of conditions and the following disclaimer in the
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
80 } 80 }
81 81
82 void SelectionEditor::setVisibleSelection(const VisibleSelection& newSelection, FrameSelection::SetSelectionOptions options) 82 void SelectionEditor::setVisibleSelection(const VisibleSelection& newSelection, FrameSelection::SetSelectionOptions options)
83 { 83 {
84 m_selection = newSelection; 84 m_selection = newSelection;
85 if (options & FrameSelection::DoNotAdjustInComposedTree) { 85 if (options & FrameSelection::DoNotAdjustInComposedTree) {
86 m_selectionInComposedTree.setWithoutValidation(toPositionInComposedTree( m_selection.base()), toPositionInComposedTree(m_selection.extent())); 86 m_selectionInComposedTree.setWithoutValidation(toPositionInComposedTree( m_selection.base()), toPositionInComposedTree(m_selection.extent()));
87 return; 87 return;
88 } 88 }
89 89
90 adjustVisibleSelectionInComposedTree(); 90 SelectionAdjuster::adjustSelectionInComposedTree(&m_selectionInComposedTree, m_selection);
91 } 91 }
92 92
93 void SelectionEditor::setVisibleSelection(const VisibleSelectionInComposedTree& newSelection, FrameSelection::SetSelectionOptions options) 93 void SelectionEditor::setVisibleSelection(const VisibleSelectionInComposedTree& newSelection, FrameSelection::SetSelectionOptions options)
94 { 94 {
95 ASSERT(!(options & FrameSelection::DoNotAdjustInComposedTree)); 95 ASSERT(!(options & FrameSelection::DoNotAdjustInComposedTree));
96 m_selectionInComposedTree = newSelection; 96 m_selectionInComposedTree = newSelection;
97 adjustVisibleSelectionInDOMTree(); 97 SelectionAdjuster::adjustSelectionInDOMTree(&m_selection, m_selectionInCompo sedTree);
98 } 98 }
99 99
100 // Updates |m_selectionInComposedTree| to match with |m_selection|. 100 // TODO(yosin): We should move
101 void SelectionEditor::adjustVisibleSelectionInComposedTree() 101 // |SelectionAdjuster::adjustSelectionInComposedTree()| to
102 // "SelectionAdjuster.cpp"
103 // Updates |selectionInComposedTree| to match with |selection|.
104 void SelectionAdjuster::adjustSelectionInComposedTree(VisibleSelectionInComposed Tree* selectionInComposedTree, const VisibleSelection& selection)
102 { 105 {
103 if (m_selection.isNone()) { 106 if (selection.isNone()) {
104 m_selectionInComposedTree = VisibleSelectionInComposedTree(); 107 *selectionInComposedTree = VisibleSelectionInComposedTree();
105 return; 108 return;
106 } 109 }
107 110
108 const PositionInComposedTree base = toPositionInComposedTree(m_selection.bas e()); 111 const PositionInComposedTree& base = toPositionInComposedTree(selection.base ());
109 const PositionInComposedTree extent = toPositionInComposedTree(m_selection.e xtent()); 112 const PositionInComposedTree& extent = toPositionInComposedTree(selection.ex tent());
110 const PositionInComposedTree position1 = toPositionInComposedTree(m_selectio n.start()); 113 const PositionInComposedTree& position1 = toPositionInComposedTree(selection .start());
111 const PositionInComposedTree position2 = toPositionInComposedTree(m_selectio n.end()); 114 const PositionInComposedTree& position2 = toPositionInComposedTree(selection .end());
112 position1.anchorNode()->updateDistribution(); 115 position1.anchorNode()->updateDistribution();
113 position2.anchorNode()->updateDistribution(); 116 position2.anchorNode()->updateDistribution();
117 selectionInComposedTree->m_base = base;
118 selectionInComposedTree->m_extent = extent;
119 selectionInComposedTree->m_affinity = selection.m_affinity;
120 selectionInComposedTree->m_isDirectional = selection.m_isDirectional;
121 selectionInComposedTree->m_baseIsFirst = base.isNull() || base.compareTo(ext ent) <= 0;
114 if (position1.compareTo(position2) <= 0) { 122 if (position1.compareTo(position2) <= 0) {
115 m_selectionInComposedTree = VisibleSelectionInComposedTree::createWithou tValidation(base, extent, position1, position2, m_selection.affinity(), m_select ion.isDirectional()); 123 selectionInComposedTree->m_start = position1;
116 return; 124 selectionInComposedTree->m_end = position2;
125 } else {
126 selectionInComposedTree->m_start = position2;
127 selectionInComposedTree->m_end = position1;
117 } 128 }
118 m_selectionInComposedTree = VisibleSelectionInComposedTree::createWithoutVal idation(base, extent, position2, position1, m_selection.affinity(), m_selection. isDirectional()); 129 selectionInComposedTree->updateSelectionType();
119 } 130 }
120 131
121 static bool isCrossingShadowBoundaries(const VisibleSelectionInComposedTree& sel ection) 132 static bool isCrossingShadowBoundaries(const VisibleSelectionInComposedTree& sel ection)
122 { 133 {
123 if (!selection.isRange()) 134 if (!selection.isRange())
124 return false; 135 return false;
125 TreeScope& treeScope = selection.base().anchorNode()->treeScope(); 136 TreeScope& treeScope = selection.base().anchorNode()->treeScope();
126 return selection.extent().anchorNode()->treeScope() != treeScope 137 return selection.extent().anchorNode()->treeScope() != treeScope
127 || selection.start().anchorNode()->treeScope() != treeScope 138 || selection.start().anchorNode()->treeScope() != treeScope
128 || selection.end().anchorNode()->treeScope() != treeScope; 139 || selection.end().anchorNode()->treeScope() != treeScope;
129 } 140 }
130 141
131 void SelectionEditor::adjustVisibleSelectionInDOMTree() 142 // TODO(yosin): We should move
143 // |SelectionAdjuster::adjustSelectionInDOMTree()| to
144 // "SelectionAdjuster.cpp"
145 void SelectionAdjuster::adjustSelectionInDOMTree(VisibleSelection* selection, co nst VisibleSelectionInComposedTree& selectionInComposedTree)
132 { 146 {
133 if (m_selectionInComposedTree.isNone()) { 147 if (selectionInComposedTree.isNone()) {
134 m_selection = VisibleSelection(); 148 *selection = VisibleSelection();
135 return; 149 return;
136 } 150 }
137 151
138 const Position base = toPositionInDOMTree(m_selectionInComposedTree.base()); 152 const Position& base = toPositionInDOMTree(selectionInComposedTree.base());
139 const Position extent = toPositionInDOMTree(m_selectionInComposedTree.extent ()); 153 const Position& extent = toPositionInDOMTree(selectionInComposedTree.extent( ));
140 154
141 if (isCrossingShadowBoundaries(m_selectionInComposedTree)) { 155 if (isCrossingShadowBoundaries(selectionInComposedTree)) {
142 m_selection = VisibleSelection(base, extent); 156 *selection = VisibleSelection(base, extent);
143 return; 157 return;
144 } 158 }
145 159
146 const Position start = toPositionInDOMTree(m_selectionInComposedTree.start() ); 160 const Position& position1 = toPositionInDOMTree(selectionInComposedTree.star t());
147 const Position end = toPositionInDOMTree(m_selectionInComposedTree.end()); 161 const Position& position2 = toPositionInDOMTree(selectionInComposedTree.end( ));
148 const TextAffinity affinity = m_selectionInComposedTree.affinity(); 162 selection->m_base = base;
149 const bool isDirectional = m_selectionInComposedTree.isDirectional(); 163 selection->m_extent = extent;
150 if (start.compareTo(end) <= 0) { 164 selection->m_affinity = selectionInComposedTree.m_affinity;
151 m_selection = VisibleSelection::createWithoutValidation(base, extent, st art, end, affinity, isDirectional); 165 selection->m_isDirectional = selectionInComposedTree.m_isDirectional;
152 return; 166 selection->m_baseIsFirst = base.isNull() || base.compareTo(extent) <= 0;
167 if (position1.compareTo(position2) <= 0) {
168 selection->m_start = position1;
169 selection->m_end = position2;
170 } else {
171 selection->m_start = position2;
172 selection->m_end = position1;
153 } 173 }
154 m_selection = VisibleSelection::createWithoutValidation(base, extent, end, s tart, affinity, isDirectional); 174 selection->updateSelectionType();
155 } 175 }
156 176
157 void SelectionEditor::resetXPosForVerticalArrowNavigation() 177 void SelectionEditor::resetXPosForVerticalArrowNavigation()
158 { 178 {
159 m_xPosForVerticalArrowNavigation = NoXPosForVerticalArrowNavigation(); 179 m_xPosForVerticalArrowNavigation = NoXPosForVerticalArrowNavigation();
160 } 180 }
161 181
162 void SelectionEditor::setIsDirectional(bool isDirectional) 182 void SelectionEditor::setIsDirectional(bool isDirectional)
163 { 183 {
164 m_selection.setIsDirectional(isDirectional); 184 m_selection.setIsDirectional(isDirectional);
(...skipping 777 matching lines...) Expand 10 before | Expand all | Expand 10 after
942 DEFINE_TRACE(SelectionEditor) 962 DEFINE_TRACE(SelectionEditor)
943 { 963 {
944 visitor->trace(m_frameSelection); 964 visitor->trace(m_frameSelection);
945 visitor->trace(m_selection); 965 visitor->trace(m_selection);
946 visitor->trace(m_selectionInComposedTree); 966 visitor->trace(m_selectionInComposedTree);
947 visitor->trace(m_logicalRange); 967 visitor->trace(m_logicalRange);
948 VisibleSelectionChangeObserver::trace(visitor); 968 VisibleSelectionChangeObserver::trace(visitor);
949 } 969 }
950 970
951 } // namespace blink 971 } // namespace blink
OLDNEW
« no previous file with comments | « no previous file | third_party/WebKit/Source/core/editing/VisibleSelection.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698