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

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

Issue 2616623002: Do not send redundant selectionchange-events (decouple focus) (Closed)
Patch Set: Fix LayoutTests that create selections within SVG documents Created 3 years, 9 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) 1999 Lars Knoll (knoll@kde.org) 2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights 3 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights
4 * reserved. 4 * reserved.
5 * 5 *
6 * This library is free software; you can redistribute it and/or 6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public 7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either 8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version. 9 * version 2 of the License, or (at your option) any later version.
10 * 10 *
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
99 ? TextAffinity::Upstream 99 ? TextAffinity::Upstream
100 : affinity); 100 : affinity);
101 if (visibleEnd.isNull()) 101 if (visibleEnd.isNull())
102 return SelectionInFlatTree(); 102 return SelectionInFlatTree();
103 SelectionInFlatTree::Builder builder; 103 SelectionInFlatTree::Builder builder;
104 builder.collapse(visibleStart.toPositionWithAffinity()); 104 builder.collapse(visibleStart.toPositionWithAffinity());
105 builder.extend(visibleEnd.deepEquivalent()); 105 builder.extend(visibleEnd.deepEquivalent());
106 return builder.build(); 106 return builder.build();
107 } 107 }
108 108
109 static bool selectionHasFocus(const LayoutView& layoutView,
110 const VisibleSelectionInFlatTree& selection) {
111 if (!isSelectionInDocument(selection, layoutView.document()))
112 return false;
113
114 const LocalFrame* const frame = layoutView.document().frame();
115 const Element* focus = frame->document()->focusedElement();
116 if (!focus) {
117 // No focused element means document has focus.
118 focus = layoutView.document().documentElement();
hugoh_UTC2 2017/03/10 10:10:37 I use document().documentElement(), not document.b
119 }
120
121 const Node* const nodeWhereSelectionStarts =
122 selection.base().computeContainerNode();
123 const Node* const nodeWhereSelectionEnds =
124 selection.extent().computeContainerNode();
125
126 return focus->containsIncludingHostElements(*nodeWhereSelectionStarts) ||
127 focus->containsIncludingHostElements(*nodeWhereSelectionEnds);
128 }
129
109 void PendingSelection::commit(LayoutView& layoutView) { 130 void PendingSelection::commit(LayoutView& layoutView) {
110 if (!hasPendingSelection()) 131 if (!hasPendingSelection())
111 return; 132 return;
112 DCHECK(!layoutView.needsLayout()); 133 DCHECK(!layoutView.needsLayout());
113 m_hasPendingSelection = false; 134 m_hasPendingSelection = false;
114 135
115 const VisibleSelectionInFlatTree& originalSelection = 136 const VisibleSelectionInFlatTree& originalSelection =
116 m_frameSelection->computeVisibleSelectionInFlatTree(); 137 m_frameSelection->computeVisibleSelectionInFlatTree();
117 138
118 // Skip if pending VisibilePositions became invalid before we reach here. 139 // Skip if pending VisibilePositions became invalid before we reach here.
119 if (!isSelectionInDocument(originalSelection, layoutView.document())) 140 if (!isSelectionInDocument(originalSelection, layoutView.document()))
120 return; 141 return;
121 142
122 // Construct a new VisibleSolution, since visibleSelection() is not 143 // Construct a new VisibleSolution, since visibleSelection() is not
123 // necessarily valid, and the following steps assume a valid selection. See 144 // necessarily valid, and the following steps assume a valid selection. See
124 // <https://bugs.webkit.org/show_bug.cgi?id=69563> and 145 // <https://bugs.webkit.org/show_bug.cgi?id=69563> and
125 // <rdar://problem/10232866>. 146 // <rdar://problem/10232866>.
126 const VisibleSelectionInFlatTree& selection = 147 const VisibleSelectionInFlatTree& selection =
127 createVisibleSelection(calcVisibleSelection(originalSelection)); 148 createVisibleSelection(calcVisibleSelection(originalSelection));
128 149
129 if (!selection.isRange()) { 150 if (!selection.isRange() || !selectionHasFocus(layoutView, selection)) {
130 layoutView.clearSelection(); 151 layoutView.clearSelection();
131 return; 152 return;
132 } 153 }
133 154
134 // Use the rightmost candidate for the start of the selection, and the 155 // Use the rightmost candidate for the start of the selection, and the
135 // leftmost candidate for the end of the selection. Example: foo <a>bar</a>. 156 // leftmost candidate for the end of the selection. Example: foo <a>bar</a>.
136 // Imagine that a line wrap occurs after 'foo', and that 'bar' is selected. 157 // Imagine that a line wrap occurs after 'foo', and that 'bar' is selected.
137 // If we pass [foo, 3] as the start of the selection, the selection painting 158 // If we pass [foo, 3] as the start of the selection, the selection painting
138 // code will think that content on the line containing 'foo' is selected 159 // code will think that content on the line containing 'foo' is selected
139 // and will fill the gap before 'bar'. 160 // and will fill the gap before 'bar'.
(...skipping 21 matching lines...) Expand all
161 DCHECK(layoutView == endLayoutObject->view()); 182 DCHECK(layoutView == endLayoutObject->view());
162 layoutView.setSelection(startLayoutObject, startPos.computeEditingOffset(), 183 layoutView.setSelection(startLayoutObject, startPos.computeEditingOffset(),
163 endLayoutObject, endPos.computeEditingOffset()); 184 endLayoutObject, endPos.computeEditingOffset());
164 } 185 }
165 186
166 DEFINE_TRACE(PendingSelection) { 187 DEFINE_TRACE(PendingSelection) {
167 visitor->trace(m_frameSelection); 188 visitor->trace(m_frameSelection);
168 } 189 }
169 190
170 } // namespace blink 191 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698