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

Unified Diff: third_party/WebKit/Source/core/editing/DOMSelection.cpp

Issue 2653523003: Make DOMSelection cache Range (Closed)
Patch Set: cache Range in SelectionEditor 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 side-by-side diff with in-line comments
Download patch
Index: third_party/WebKit/Source/core/editing/DOMSelection.cpp
diff --git a/third_party/WebKit/Source/core/editing/DOMSelection.cpp b/third_party/WebKit/Source/core/editing/DOMSelection.cpp
index 34c9a1c8e64719d26bfc41d5b133c9a5c5b780e6..048d52dae57b7bdfddffb85d11e4b290ff5fce2f 100644
--- a/third_party/WebKit/Source/core/editing/DOMSelection.cpp
+++ b/third_party/WebKit/Source/core/editing/DOMSelection.cpp
@@ -175,6 +175,8 @@ String DOMSelection::type() const {
int DOMSelection::rangeCount() const {
if (!isAvailable())
return 0;
+ if (isTreeScopeDocument() && documentCachedRange())
+ return 1;
return frame()->selection().isNone() ? 0 : 1;
}
@@ -286,6 +288,9 @@ void DOMSelection::setBaseAndExtent(Node* baseNode,
if (!isValidForPosition(baseNode) || !isValidForPosition(extentNode))
return;
+ if (isTreeScopeDocument())
+ clearDocumentCachedRange();
+
// TODO(editing-dev): The use of updateStyleAndLayoutIgnorePendingStylesheets
// needs to be audited. See http://crbug.com/590369 for more details.
// In the long term, we should change FrameSelection::setSelection to take a
@@ -383,6 +388,8 @@ void DOMSelection::extend(Node* node,
if (!isValidForPosition(node))
return;
+ if (isTreeScopeDocument())
+ clearDocumentCachedRange();
const Position& base = frame()->selection().base();
if (base.isNull()) {
// TODO(editing-dev): We should throw |InvalidStateError| if selection is
@@ -413,6 +420,18 @@ Range* DOMSelection::getRangeAt(int index, ExceptionState& exceptionState) {
// If you're hitting this, you've added broken multi-range selection support
DCHECK_EQ(rangeCount(), 1);
+ if (!isTreeScopeDocument())
yosin_UTC9 2017/02/09 06:52:06 If we have |cachedRangeIfDocumentSlection()| and |
yoichio 2017/02/09 09:31:19 Done.
+ return createRangeFromSelectionEditor();
+
+ if (Range* cachedRange = documentCachedRange())
+ return cachedRange;
+
+ Range* range = createRangeFromSelectionEditor();
+ cacheRangeOfDocument(range);
+ return range;
+}
+
+Range* DOMSelection::createRangeFromSelectionEditor() {
Position anchor = anchorPosition(visibleSelection());
if (!anchor.anchorNode()->isInShadowTree())
return frame()->selection().firstRange();
@@ -427,6 +446,24 @@ Range* DOMSelection::getRangeAt(int index, ExceptionState& exceptionState) {
focusOffset());
}
+bool DOMSelection::isTreeScopeDocument() const {
yosin_UTC9 2017/02/09 06:52:06 nit: s/isTreeScopeDocument/isSelectionForDocument/
yoichio 2017/02/09 09:31:19 Done.
+ return m_treeScope == m_treeScope->document();
+}
+
+void DOMSelection::cacheRangeOfDocument(Range* range) {
+ DCHECK(isTreeScopeDocument());
+ frame()->selection().cacheRangeOfDocument(range);
+}
+
+Range* DOMSelection::documentCachedRange() const {
+ DCHECK(isTreeScopeDocument());
+ return frame()->selection().documentCachedRange();
+}
+
+void DOMSelection::clearDocumentCachedRange() {
+ frame()->selection().clearDocumentCachedRange();
yosin_UTC9 2017/02/09 06:52:06 Let's add |DCHECK(isTreeScopeDocument())|. It see
yoichio 2017/02/09 09:31:19 Done.
+}
+
void DOMSelection::removeAllRanges() {
if (!isAvailable())
return;
@@ -463,6 +500,8 @@ void DOMSelection::addRange(Range* newRange) {
if (selection.isNone()) {
selection.setSelectedRange(EphemeralRange(newRange), VP_DEFAULT_AFFINITY);
+ if (isTreeScopeDocument())
yosin_UTC9 2017/02/09 06:52:06 |cacheRangeIfDocumentSlection(newRange)| is simple
yoichio 2017/02/09 09:31:19 Done.
+ cacheRangeOfDocument(newRange);
return;
}
@@ -510,6 +549,8 @@ void DOMSelection::addRange(Range* newRange) {
EphemeralRange(start->startPosition(), end->endPosition());
TextAffinity affinity = selection.selection().affinity();
selection.setSelectedRange(merged, affinity);
+ if (isTreeScopeDocument())
+ cacheRangeOfDocument(newRange);
}
void DOMSelection::deleteFromDocument() {

Powered by Google App Engine
This is Rietveld 408576698