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

Unified Diff: third_party/WebKit/Source/core/editing/DOMSelection.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 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..a996b0d08f707d896cbea772d80af70739f36f68 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 (documentCachedRange())
+ return 1;
return frame()->selection().isNone() ? 0 : 1;
}
@@ -286,6 +288,8 @@ void DOMSelection::setBaseAndExtent(Node* baseNode,
if (!isValidForPosition(baseNode) || !isValidForPosition(extentNode))
return;
+ clearCachedRangeIfSelectionOfDocument();
+
// 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 +387,7 @@ void DOMSelection::extend(Node* node,
if (!isValidForPosition(node))
return;
+ clearCachedRangeIfSelectionOfDocument();
const Position& base = frame()->selection().base();
if (base.isNull()) {
// TODO(editing-dev): We should throw |InvalidStateError| if selection is
@@ -413,6 +418,15 @@ 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 (Range* cachedRange = documentCachedRange())
+ return cachedRange;
+
+ Range* range = createRangeFromSelectionEditor();
+ cacheRangeIfSelectionOfDocument(range);
+ return range;
+}
+
+Range* DOMSelection::createRangeFromSelectionEditor() {
Position anchor = anchorPosition(visibleSelection());
if (!anchor.anchorNode()->isInShadowTree())
return frame()->selection().firstRange();
@@ -427,6 +441,31 @@ Range* DOMSelection::getRangeAt(int index, ExceptionState& exceptionState) {
focusOffset());
}
+bool DOMSelection::isSelectionOfDocument() const {
+ return m_treeScope == m_treeScope->document();
+}
+
+void DOMSelection::cacheRangeIfSelectionOfDocument(Range* range) {
+ if (!isSelectionOfDocument())
+ return;
+
yosin_UTC9 2017/02/09 09:32:09 nit: Please get rid of an extra blank line.
yoichio 2017/02/09 09:36:04 Done.
+ frame()->selection().cacheRangeOfDocument(range);
+}
+
+Range* DOMSelection::documentCachedRange() const {
+ if (!isSelectionOfDocument())
+ return nullptr;
+
yosin_UTC9 2017/02/09 09:32:09 nit: Please get rid of an extra blank line.
yoichio 2017/02/09 09:36:04 Done.
+ return frame()->selection().documentCachedRange();
+}
+
+void DOMSelection::clearCachedRangeIfSelectionOfDocument() {
+ if (!isSelectionOfDocument())
+ return;
+
yosin_UTC9 2017/02/09 09:32:09 nit: Please get rid of an extra blank line.
yoichio 2017/02/09 09:36:04 Done.
+ frame()->selection().clearDocumentCachedRange();
+}
+
void DOMSelection::removeAllRanges() {
if (!isAvailable())
return;
@@ -463,6 +502,7 @@ void DOMSelection::addRange(Range* newRange) {
if (selection.isNone()) {
selection.setSelectedRange(EphemeralRange(newRange), VP_DEFAULT_AFFINITY);
+ cacheRangeIfSelectionOfDocument(newRange);
return;
}
@@ -510,6 +550,7 @@ void DOMSelection::addRange(Range* newRange) {
EphemeralRange(start->startPosition(), end->endPosition());
TextAffinity affinity = selection.selection().affinity();
selection.setSelectedRange(merged, affinity);
+ cacheRangeIfSelectionOfDocument(createRange(merged));
}
void DOMSelection::deleteFromDocument() {
« no previous file with comments | « third_party/WebKit/Source/core/editing/DOMSelection.h ('k') | third_party/WebKit/Source/core/editing/FrameSelection.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698