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

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

Issue 2653523003: Make DOMSelection cache Range (Closed)
Patch Set: update 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
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
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/editing/DOMSelection.h"
28 #include "core/editing/EditingUtilities.h" 29 #include "core/editing/EditingUtilities.h"
29 #include "core/editing/Editor.h" 30 #include "core/editing/Editor.h"
30 #include "core/editing/SelectionAdjuster.h" 31 #include "core/editing/SelectionAdjuster.h"
31 #include "core/frame/LocalFrame.h" 32 #include "core/frame/LocalFrame.h"
32 33
33 namespace blink { 34 namespace blink {
34 35
35 SelectionEditor::SelectionEditor(LocalFrame& frame) 36 SelectionEditor::SelectionEditor(LocalFrame& frame)
36 : m_frame(frame), m_observingVisibleSelection(false) { 37 : m_frame(frame), m_observingVisibleSelection(false) {
37 clearVisibleSelection(); 38 clearVisibleSelection();
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
73 const VisibleSelectionInFlatTree& 74 const VisibleSelectionInFlatTree&
74 SelectionEditor::visibleSelection<EditingInFlatTreeStrategy>() const { 75 SelectionEditor::visibleSelection<EditingInFlatTreeStrategy>() const {
75 DCHECK_EQ(frame()->document(), document()); 76 DCHECK_EQ(frame()->document(), document());
76 DCHECK_EQ(frame(), document().frame()); 77 DCHECK_EQ(frame(), document().frame());
77 if (m_selectionInFlatTree.isNone()) 78 if (m_selectionInFlatTree.isNone())
78 return m_selectionInFlatTree; 79 return m_selectionInFlatTree;
79 DCHECK_EQ(m_selectionInFlatTree.base().document(), document()); 80 DCHECK_EQ(m_selectionInFlatTree.base().document(), document());
80 return m_selectionInFlatTree; 81 return m_selectionInFlatTree;
81 } 82 }
82 83
84 static void markRangeDirty(const TreeScope* treeScope) {
85 for (const TreeScope* runner = treeScope; runner;
86 runner = runner->parentTreeScope()) {
87 if (runner->hasSelection())
88 runner->getSelection()->markRangeDirty();
89 }
90 }
91
92 static void markDOMSelectionDirty(const VisibleSelection& selection) {
93 if (selection.isNone())
94 return;
95
96 const TreeScope& startScope = selection.start().anchorNode()->treeScope();
97 markRangeDirty(&startScope);
yosin_UTC9 2017/02/08 06:20:26 We should reset all ranges in whole tree-of-tree n
98 if (selection.isCaret())
99 return;
100
101 const TreeScope& endScope = selection.end().anchorNode()->treeScope();
102 if (startScope != endScope)
103 markRangeDirty(&endScope);
104 }
105
83 void SelectionEditor::setVisibleSelection( 106 void SelectionEditor::setVisibleSelection(
84 const VisibleSelection& newSelection, 107 const VisibleSelection& newSelection,
85 FrameSelection::SetSelectionOptions options) { 108 FrameSelection::SetSelectionOptions options) {
86 DCHECK(newSelection.isValidFor(document())) << newSelection; 109 DCHECK(newSelection.isValidFor(document())) << newSelection;
87 resetLogicalRange(); 110 resetLogicalRange();
111 markDOMSelectionDirty(m_selection);
112
88 m_selection = newSelection; 113 m_selection = newSelection;
89 if (options & FrameSelection::DoNotAdjustInFlatTree) { 114 if (options & FrameSelection::DoNotAdjustInFlatTree) {
90 m_selectionInFlatTree.setWithoutValidation( 115 m_selectionInFlatTree.setWithoutValidation(
91 toPositionInFlatTree(m_selection.base()), 116 toPositionInFlatTree(m_selection.base()),
92 toPositionInFlatTree(m_selection.extent())); 117 toPositionInFlatTree(m_selection.extent()));
93 return; 118 return;
94 } 119 }
95 120
96 SelectionAdjuster::adjustSelectionInFlatTree(&m_selectionInFlatTree, 121 SelectionAdjuster::adjustSelectionInFlatTree(&m_selectionInFlatTree,
97 m_selection); 122 m_selection);
123
124 markDOMSelectionDirty(m_selection);
98 } 125 }
99 126
100 void SelectionEditor::setVisibleSelection( 127 void SelectionEditor::setVisibleSelection(
101 const VisibleSelectionInFlatTree& newSelection, 128 const VisibleSelectionInFlatTree& newSelection,
102 FrameSelection::SetSelectionOptions options) { 129 FrameSelection::SetSelectionOptions options) {
103 DCHECK(newSelection.isValidFor(document())) << newSelection; 130 DCHECK(newSelection.isValidFor(document())) << newSelection;
104 DCHECK(!(options & FrameSelection::DoNotAdjustInFlatTree)); 131 DCHECK(!(options & FrameSelection::DoNotAdjustInFlatTree));
105 resetLogicalRange(); 132 resetLogicalRange();
133 markDOMSelectionDirty(m_selection);
134
106 m_selectionInFlatTree = newSelection; 135 m_selectionInFlatTree = newSelection;
107 SelectionAdjuster::adjustSelectionInDOMTree(&m_selection, 136 SelectionAdjuster::adjustSelectionInDOMTree(&m_selection,
108 m_selectionInFlatTree); 137 m_selectionInFlatTree);
138
139 markDOMSelectionDirty(m_selection);
yosin_UTC9 2017/02/08 06:20:26 Why do we need to call markDOMSelectionDirty() twi
109 } 140 }
110 141
111 void SelectionEditor::setWithoutValidation(const Position& base, 142 void SelectionEditor::setWithoutValidation(const Position& base,
112 const Position& extent) { 143 const Position& extent) {
113 resetLogicalRange(); 144 resetLogicalRange();
114 if (base.isNotNull()) 145 if (base.isNotNull())
115 DCHECK_EQ(base.document(), document()); 146 DCHECK_EQ(base.document(), document());
116 if (extent.isNotNull()) 147 if (extent.isNotNull())
117 DCHECK_EQ(extent.document(), document()); 148 DCHECK_EQ(extent.document(), document());
149 markDOMSelectionDirty(m_selection);
150
118 m_selection.setWithoutValidation(base, extent); 151 m_selection.setWithoutValidation(base, extent);
119 m_selectionInFlatTree.setWithoutValidation(toPositionInFlatTree(base), 152 m_selectionInFlatTree.setWithoutValidation(toPositionInFlatTree(base),
120 toPositionInFlatTree(extent)); 153 toPositionInFlatTree(extent));
154 markDOMSelectionDirty(m_selection);
121 } 155 }
122 156
123 void SelectionEditor::documentAttached(Document* document) { 157 void SelectionEditor::documentAttached(Document* document) {
124 DCHECK(document); 158 DCHECK(document);
125 DCHECK(!m_document) << m_document; 159 DCHECK(!m_document) << m_document;
126 m_document = document; 160 m_document = document;
127 } 161 }
128 162
129 void SelectionEditor::documentDetached(const Document& document) { 163 void SelectionEditor::documentDetached(const Document& document) {
130 DCHECK_EQ(m_document, &document); 164 DCHECK_EQ(m_document, &document);
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
166 200
167 DEFINE_TRACE(SelectionEditor) { 201 DEFINE_TRACE(SelectionEditor) {
168 visitor->trace(m_document); 202 visitor->trace(m_document);
169 visitor->trace(m_frame); 203 visitor->trace(m_frame);
170 visitor->trace(m_selection); 204 visitor->trace(m_selection);
171 visitor->trace(m_selectionInFlatTree); 205 visitor->trace(m_selectionInFlatTree);
172 visitor->trace(m_logicalRange); 206 visitor->trace(m_logicalRange);
173 } 207 }
174 208
175 } // namespace blink 209 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698