OLD | NEW |
---|---|
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 |
11 * documentation and/or other materials provided with the distribution. | 11 * documentation and/or other materials provided with the distribution. |
12 * | 12 * |
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY | 13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY |
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR | 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR |
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | 17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | 18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | 19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY | 20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
24 */ | 24 */ |
25 | 25 |
26 #include "core/editing/SelectionEditor.h" | 26 #include "core/editing/SelectionEditor.h" |
27 | 27 |
28 #include "core/dom/NodeWithIndex.h" | |
29 #include "core/dom/Text.h" | |
28 #include "core/editing/EditingUtilities.h" | 30 #include "core/editing/EditingUtilities.h" |
29 #include "core/editing/Editor.h" | 31 #include "core/editing/Editor.h" |
30 #include "core/editing/SelectionAdjuster.h" | 32 #include "core/editing/SelectionAdjuster.h" |
31 #include "core/frame/LocalFrame.h" | 33 #include "core/frame/LocalFrame.h" |
32 | 34 |
33 namespace blink { | 35 namespace blink { |
34 | 36 |
35 SelectionEditor::SelectionEditor(LocalFrame& frame) | 37 SelectionEditor::SelectionEditor(LocalFrame& frame) : m_frame(frame) { |
36 : m_frame(frame), m_observingVisibleSelection(false) { | |
37 clearVisibleSelection(); | 38 clearVisibleSelection(); |
38 } | 39 } |
39 | 40 |
40 SelectionEditor::~SelectionEditor() {} | 41 SelectionEditor::~SelectionEditor() {} |
41 | 42 |
43 void SelectionEditor::assertSelectionValid() const { | |
44 #if DCHECK_IS_ON() | |
45 // Since We don't track dom tree version during attribute changes, we can't | |
46 // use it for validity of |m_selection|. | |
47 const_cast<SelectionEditor*>(this)->m_selection.m_domTreeVersion = | |
48 document().domTreeVersion(); | |
49 #endif | |
50 m_selection.assertValidFor(document()); | |
51 } | |
52 | |
42 void SelectionEditor::clearVisibleSelection() { | 53 void SelectionEditor::clearVisibleSelection() { |
43 m_selection = VisibleSelection(); | 54 m_selection = SelectionInDOMTree(); |
44 m_selectionInFlatTree = VisibleSelectionInFlatTree(); | 55 m_cachedVisibleSelectionInDOMTree = VisibleSelection(); |
56 m_cachedVisibleSelectionInFlatTree = VisibleSelectionInFlatTree(); | |
57 m_cacheIsDirty = false; | |
45 if (!shouldAlwaysUseDirectionalSelection()) | 58 if (!shouldAlwaysUseDirectionalSelection()) |
46 return; | 59 return; |
47 m_selection.setIsDirectional(true); | 60 m_selection.m_isDirectional = true; |
48 m_selectionInFlatTree.setIsDirectional(true); | |
49 } | 61 } |
50 | 62 |
51 void SelectionEditor::dispose() { | 63 void SelectionEditor::dispose() { |
52 resetLogicalRange(); | 64 resetLogicalRange(); |
53 clearDocumentCachedRange(); | 65 clearDocumentCachedRange(); |
54 clearVisibleSelection(); | 66 clearVisibleSelection(); |
55 } | 67 } |
56 | 68 |
57 const Document& SelectionEditor::document() const { | 69 const Document& SelectionEditor::document() const { |
58 DCHECK(m_document); | 70 DCHECK(lifecycleContext()); |
59 return *m_document; | 71 return *lifecycleContext(); |
72 } | |
73 | |
74 Document& SelectionEditor::document() { | |
75 DCHECK(lifecycleContext()); | |
76 return *lifecycleContext(); | |
60 } | 77 } |
61 | 78 |
62 template <> | 79 template <> |
63 const VisibleSelection& SelectionEditor::visibleSelection<EditingStrategy>() | 80 const VisibleSelection& SelectionEditor::visibleSelection<EditingStrategy>() |
64 const { | 81 const { |
65 DCHECK_EQ(frame()->document(), document()); | 82 return computeVisibleSelectionInDOMTree(); |
66 DCHECK_EQ(frame(), document().frame()); | |
67 if (m_selection.isNone()) | |
68 return m_selection; | |
69 DCHECK_EQ(m_selection.base().document(), document()); | |
70 return m_selection; | |
71 } | 83 } |
72 | 84 |
73 template <> | 85 template <> |
74 const VisibleSelectionInFlatTree& | 86 const VisibleSelectionInFlatTree& |
75 SelectionEditor::visibleSelection<EditingInFlatTreeStrategy>() const { | 87 SelectionEditor::visibleSelection<EditingInFlatTreeStrategy>() const { |
88 return computeVisibleSelectionInFlatTree(); | |
89 } | |
90 | |
91 const VisibleSelection& SelectionEditor::computeVisibleSelectionInDOMTree() | |
92 const { | |
76 DCHECK_EQ(frame()->document(), document()); | 93 DCHECK_EQ(frame()->document(), document()); |
77 DCHECK_EQ(frame(), document().frame()); | 94 DCHECK_EQ(frame(), document().frame()); |
78 if (m_selectionInFlatTree.isNone()) | 95 const_cast<SelectionEditor*>(this)->updateCachedVisibleSelectionIfNeeded(); |
79 return m_selectionInFlatTree; | 96 if (m_cachedVisibleSelectionInDOMTree.isNone()) |
80 DCHECK_EQ(m_selectionInFlatTree.base().document(), document()); | 97 return m_cachedVisibleSelectionInDOMTree; |
81 return m_selectionInFlatTree; | 98 DCHECK_EQ(m_cachedVisibleSelectionInDOMTree.base().document(), document()); |
99 return m_cachedVisibleSelectionInDOMTree; | |
82 } | 100 } |
83 | 101 |
84 void SelectionEditor::setVisibleSelection( | 102 const VisibleSelectionInFlatTree& |
85 const VisibleSelection& newSelection, | 103 SelectionEditor::computeVisibleSelectionInFlatTree() const { |
86 FrameSelection::SetSelectionOptions options) { | 104 DCHECK_EQ(frame()->document(), document()); |
87 DCHECK(newSelection.isValidFor(document())) << newSelection; | 105 DCHECK_EQ(frame(), document().frame()); |
106 const_cast<SelectionEditor*>(this)->updateCachedVisibleSelectionIfNeeded(); | |
tkent
2017/02/10 08:47:41
Can we make updateCachedVisibleSelectionIfNeeded()
yosin_UTC9
2017/02/10 10:13:20
Done.
| |
107 if (m_cachedVisibleSelectionInFlatTree.isNone()) | |
108 return m_cachedVisibleSelectionInFlatTree; | |
109 DCHECK_EQ(m_cachedVisibleSelectionInFlatTree.base().document(), document()); | |
110 return m_cachedVisibleSelectionInFlatTree; | |
111 } | |
112 | |
113 const SelectionInDOMTree& SelectionEditor::selectionInDOMTree() const { | |
114 assertSelectionValid(); | |
115 return m_selection; | |
116 } | |
117 | |
118 bool SelectionEditor::hasEditableStyle() const { | |
119 return computeVisibleSelectionInDOMTree().hasEditableStyle(); | |
120 } | |
121 | |
122 bool SelectionEditor::isContentEditable() const { | |
123 return computeVisibleSelectionInDOMTree().isContentEditable(); | |
124 } | |
125 | |
126 bool SelectionEditor::isContentRichlyEditable() const { | |
127 return computeVisibleSelectionInDOMTree().isContentRichlyEditable(); | |
128 } | |
129 | |
130 void SelectionEditor::markCacheDirty() { | |
131 m_cachedVisibleSelectionInFlatTree = VisibleSelectionInFlatTree(); | |
132 m_cachedVisibleSelectionInDOMTree = VisibleSelection(); | |
133 m_cacheIsDirty = true; | |
134 } | |
135 | |
136 void SelectionEditor::setSelection(const SelectionInDOMTree& newSelection) { | |
137 newSelection.assertValidFor(document()); | |
138 if (m_selection == newSelection) | |
139 return; | |
88 resetLogicalRange(); | 140 resetLogicalRange(); |
89 clearDocumentCachedRange(); | 141 clearDocumentCachedRange(); |
142 markCacheDirty(); | |
143 m_selection = newSelection; | |
144 } | |
90 | 145 |
91 m_selection = newSelection; | 146 void SelectionEditor::didChangeChildren(const ContainerNode&) { |
92 if (options & FrameSelection::DoNotAdjustInFlatTree) { | 147 markCacheDirty(); |
93 m_selectionInFlatTree.setWithoutValidation( | 148 didFinishDOMMutation(); |
94 toPositionInFlatTree(m_selection.base()), | 149 } |
95 toPositionInFlatTree(m_selection.extent())); | 150 |
151 static Position positionAfterPreviousSiblingOf(const Node& node) { | |
152 Node* const previousSibling = NodeTraversal::previousSibling(node); | |
153 if (!previousSibling) { | |
154 // Returns |BeforeChildren| of a parent of |node| since |node| is the | |
155 // first child. | |
156 return Position::firstPositionInNode(NodeTraversal::parent(node)); | |
157 } | |
158 if (previousSibling->isCharacterDataNode()) { | |
159 return Position(previousSibling, | |
160 toCharacterData(previousSibling)->length()); | |
161 } | |
162 return Position::afterNode(previousSibling); | |
163 } | |
164 | |
165 static Position computePositionForNodeRemoval(const Position& position, | |
166 Node& nodeToBeRemoved) { | |
167 Node* const anchorNode = position.anchorNode(); | |
168 if (anchorNode == nodeToBeRemoved) | |
169 return positionAfterPreviousSiblingOf(nodeToBeRemoved); | |
170 if (position.isOffsetInAnchor() && | |
171 anchorNode == nodeToBeRemoved.parentNode()) { | |
172 const int positionOffset = position.offsetInContainerNode(); | |
173 if (positionOffset == 0) | |
174 return position; | |
175 // |nodeToBeRemoved| is sibling or itself of |position|. | |
176 int offset = 0; | |
177 for (Node& child : | |
178 NodeTraversal::childrenOf(*nodeToBeRemoved.parentNode())) { | |
179 if (nodeToBeRemoved == child) { | |
180 // |position| is after |nodeToBeRemoved|. | |
181 return Position(anchorNode, positionOffset - 1); | |
182 } | |
183 if (positionOffset == offset) { | |
184 // |position| is before |nodeToBeRemoved|. | |
185 return position; | |
186 } | |
187 ++offset; | |
188 } | |
189 NOTREACHED() << position << " should be handled."; | |
190 } | |
191 if (!nodeToBeRemoved.isContainerNode()) | |
192 return position; | |
193 if (toContainerNode(nodeToBeRemoved) | |
194 .containsIncludingHostElements(*anchorNode)) | |
195 return positionAfterPreviousSiblingOf(nodeToBeRemoved); | |
196 return position; | |
197 } | |
198 | |
199 void SelectionEditor::didFinishTextChange(const Position& newBase, | |
200 const Position& newExtent) { | |
201 if (newBase == m_selection.m_base && newExtent == m_selection.m_extent) { | |
202 didFinishDOMMutation(); | |
96 return; | 203 return; |
97 } | 204 } |
98 | 205 m_selection.m_base = newBase; |
99 SelectionAdjuster::adjustSelectionInFlatTree(&m_selectionInFlatTree, | 206 m_selection.m_extent = newExtent; |
100 m_selection); | 207 markCacheDirty(); |
208 didFinishDOMMutation(); | |
101 } | 209 } |
102 | 210 |
103 void SelectionEditor::setVisibleSelection( | 211 void SelectionEditor::didFinishDOMMutation() { |
104 const VisibleSelectionInFlatTree& newSelection, | 212 assertSelectionValid(); |
105 FrameSelection::SetSelectionOptions options) { | |
106 DCHECK(newSelection.isValidFor(document())) << newSelection; | |
107 DCHECK(!(options & FrameSelection::DoNotAdjustInFlatTree)); | |
108 resetLogicalRange(); | |
109 clearDocumentCachedRange(); | |
110 | |
111 m_selectionInFlatTree = newSelection; | |
112 SelectionAdjuster::adjustSelectionInDOMTree(&m_selection, | |
113 m_selectionInFlatTree); | |
114 } | 213 } |
115 | 214 |
116 void SelectionEditor::setWithoutValidation(const Position& base, | 215 void SelectionEditor::nodeWillBeRemoved(Node& nodeToBeRemoved) { |
117 const Position& extent) { | 216 if (m_selection.isNone()) |
118 resetLogicalRange(); | 217 return; |
119 if (base.isNotNull()) | 218 const Position oldBase = m_selection.m_base; |
120 DCHECK_EQ(base.document(), document()); | 219 const Position oldExtent = m_selection.m_extent; |
121 if (extent.isNotNull()) | 220 const Position& newBase = |
122 DCHECK_EQ(extent.document(), document()); | 221 computePositionForNodeRemoval(oldBase, nodeToBeRemoved); |
123 clearDocumentCachedRange(); | 222 const Position& newExtent = |
124 | 223 computePositionForNodeRemoval(oldExtent, nodeToBeRemoved); |
125 m_selection.setWithoutValidation(base, extent); | 224 if (newBase == oldBase && newExtent == oldExtent) |
126 m_selectionInFlatTree.setWithoutValidation(toPositionInFlatTree(base), | 225 return; |
127 toPositionInFlatTree(extent)); | 226 m_selection = SelectionInDOMTree::Builder() |
227 .setBaseAndExtent(newBase, newExtent) | |
228 .build(); | |
229 markCacheDirty(); | |
128 } | 230 } |
129 | 231 |
130 void SelectionEditor::documentAttached(Document* document) { | 232 void SelectionEditor::documentAttached(Document* document) { |
131 DCHECK(document); | 233 DCHECK(document); |
132 DCHECK(!m_document) << m_document; | 234 DCHECK(!lifecycleContext()) << lifecycleContext(); |
133 m_document = document; | 235 m_styleVersion = static_cast<uint64_t>(-1); |
236 clearVisibleSelection(); | |
237 setContext(document); | |
134 } | 238 } |
135 | 239 |
136 void SelectionEditor::documentDetached(const Document& document) { | 240 void SelectionEditor::contextDestroyed(Document*) { |
137 DCHECK_EQ(m_document, &document); | |
138 dispose(); | 241 dispose(); |
139 m_document = nullptr; | 242 m_styleVersion = static_cast<uint64_t>(-1); |
243 m_selection = SelectionInDOMTree(); | |
244 m_cachedVisibleSelectionInDOMTree = VisibleSelection(); | |
245 m_cachedVisibleSelectionInFlatTree = VisibleSelectionInFlatTree(); | |
246 m_cacheIsDirty = false; | |
140 } | 247 } |
141 | 248 |
142 void SelectionEditor::resetLogicalRange() { | 249 void SelectionEditor::resetLogicalRange() { |
143 // Non-collapsed ranges are not allowed to start at the end of a line that | 250 // Non-collapsed ranges are not allowed to start at the end of a line that |
144 // is wrapped, they start at the beginning of the next line instead | 251 // is wrapped, they start at the beginning of the next line instead |
145 if (!m_logicalRange) | 252 if (!m_logicalRange) |
146 return; | 253 return; |
147 m_logicalRange->dispose(); | 254 m_logicalRange->dispose(); |
148 m_logicalRange = nullptr; | 255 m_logicalRange = nullptr; |
149 } | 256 } |
150 | 257 |
151 void SelectionEditor::setLogicalRange(Range* range) { | 258 void SelectionEditor::setLogicalRange(Range* range) { |
152 DCHECK_EQ(range->ownerDocument(), document()); | 259 DCHECK_EQ(range->ownerDocument(), document()); |
153 DCHECK(!m_logicalRange) << "A logical range should be one."; | 260 DCHECK(!m_logicalRange) << "A logical range should be one."; |
154 m_logicalRange = range; | 261 m_logicalRange = range; |
155 } | 262 } |
156 | 263 |
157 Range* SelectionEditor::firstRange() const { | 264 Range* SelectionEditor::firstRange() const { |
158 if (m_logicalRange) | 265 if (m_logicalRange) |
159 return m_logicalRange->cloneRange(); | 266 return m_logicalRange->cloneRange(); |
160 return firstRangeOf(m_selection); | 267 return firstRangeOf(computeVisibleSelectionInDOMTree()); |
161 } | 268 } |
162 | 269 |
163 bool SelectionEditor::shouldAlwaysUseDirectionalSelection() const { | 270 bool SelectionEditor::shouldAlwaysUseDirectionalSelection() const { |
164 return frame()->editor().behavior().shouldConsiderSelectionAsDirectional(); | 271 return frame()->editor().behavior().shouldConsiderSelectionAsDirectional(); |
165 } | 272 } |
166 | 273 |
167 void SelectionEditor::updateIfNeeded() { | 274 void SelectionEditor::updateIfNeeded() { |
168 DCHECK(m_selection.isValidFor(document())) << m_selection; | 275 // TODO(yosin): We should unify |SelectionEditor::updateIfNeeded()| and |
169 DCHECK(m_selectionInFlatTree.isValidFor(document())) << m_selection; | 276 // |updateCachedVisibleSelectionIfNeeded()| |
170 m_selection.updateIfNeeded(); | 277 updateCachedVisibleSelectionIfNeeded(); |
171 m_selectionInFlatTree.updateIfNeeded(); | 278 } |
279 | |
280 bool SelectionEditor::needsUpdateVisibleSelection() const { | |
281 return m_cacheIsDirty || m_styleVersion != document().styleVersion(); | |
282 } | |
283 | |
284 void SelectionEditor::updateCachedVisibleSelectionIfNeeded() { | |
285 // Note: Since we |FrameCaret::updateApperance()| is called from | |
286 // |FrameView::performPostLayoutTasks()|, we check lifecycle against | |
287 // |AfterPerformLayout| instead of |LayoutClean|. | |
288 DCHECK_GE(document().lifecycle().state(), | |
289 DocumentLifecycle::AfterPerformLayout); | |
290 assertSelectionValid(); | |
291 if (!needsUpdateVisibleSelection()) | |
292 return; | |
293 | |
294 m_cachedVisibleSelectionInDOMTree = createVisibleSelection(m_selection); | |
295 m_cachedVisibleSelectionInFlatTree = createVisibleSelection( | |
296 SelectionInFlatTree::Builder() | |
297 .setBaseAndExtent(toPositionInFlatTree(m_selection.base()), | |
298 toPositionInFlatTree(m_selection.extent())) | |
299 .setAffinity(m_selection.affinity()) | |
300 .setHasTrailingWhitespace(m_selection.hasTrailingWhitespace()) | |
301 .setGranularity(m_selection.granularity()) | |
302 .setIsDirectional(m_selection.isDirectional()) | |
303 .build()); | |
304 m_styleVersion = document().styleVersion(); | |
305 m_cacheIsDirty = false; | |
172 } | 306 } |
173 | 307 |
174 void SelectionEditor::cacheRangeOfDocument(Range* range) { | 308 void SelectionEditor::cacheRangeOfDocument(Range* range) { |
175 m_cachedRange = range; | 309 m_cachedRange = range; |
176 } | 310 } |
177 | 311 |
178 Range* SelectionEditor::documentCachedRange() const { | 312 Range* SelectionEditor::documentCachedRange() const { |
179 return m_cachedRange; | 313 return m_cachedRange; |
180 } | 314 } |
181 | 315 |
182 void SelectionEditor::clearDocumentCachedRange() { | 316 void SelectionEditor::clearDocumentCachedRange() { |
183 m_cachedRange = nullptr; | 317 m_cachedRange = nullptr; |
184 } | 318 } |
185 | 319 |
186 DEFINE_TRACE(SelectionEditor) { | 320 DEFINE_TRACE(SelectionEditor) { |
187 visitor->trace(m_document); | |
188 visitor->trace(m_frame); | 321 visitor->trace(m_frame); |
189 visitor->trace(m_selection); | 322 visitor->trace(m_selection); |
190 visitor->trace(m_selectionInFlatTree); | 323 visitor->trace(m_cachedVisibleSelectionInDOMTree); |
324 visitor->trace(m_cachedVisibleSelectionInFlatTree); | |
191 visitor->trace(m_logicalRange); | 325 visitor->trace(m_logicalRange); |
192 visitor->trace(m_cachedRange); | 326 visitor->trace(m_cachedRange); |
327 SynchronousMutationObserver::trace(visitor); | |
193 } | 328 } |
194 | 329 |
195 } // namespace blink | 330 } // namespace blink |
OLD | NEW |