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

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

Issue 2694043004: Selection API: anchorNode, anchorOffset, focusNode, focusOffset, isCollapsed, and type should be ba… (Closed)
Patch Set: . 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 20b6dfd4de3acc5c7105b96853e749e006df6086..679549835d6f52797f6431e7053480efeaca286a 100644
--- a/third_party/WebKit/Source/core/editing/DOMSelection.cpp
+++ b/third_party/WebKit/Source/core/editing/DOMSelection.cpp
@@ -95,31 +95,39 @@ static Position extentPosition(const VisibleSelection& selection) {
}
Node* DOMSelection::anchorNode() const {
- if (!isAvailable())
- return 0;
-
- return shadowAdjustedNode(anchorPosition(visibleSelection()));
+ if (Range* range = primaryRangeOrNull()) {
+ if (!frame() || visibleSelection().isBaseFirst())
+ return range->startContainer();
+ return range->endContainer();
+ }
+ return nullptr;
}
int DOMSelection::anchorOffset() const {
- if (!isAvailable())
- return 0;
-
- return shadowAdjustedOffset(anchorPosition(visibleSelection()));
+ if (Range* range = primaryRangeOrNull()) {
+ if (!frame() || visibleSelection().isBaseFirst())
+ return range->startOffset();
+ return range->endOffset();
+ }
+ return 0;
}
Node* DOMSelection::focusNode() const {
- if (!isAvailable())
- return 0;
-
- return shadowAdjustedNode(focusPosition(visibleSelection()));
+ if (Range* range = primaryRangeOrNull()) {
+ if (!frame() || visibleSelection().isBaseFirst())
+ return range->endContainer();
+ return range->startContainer();
+ }
+ return nullptr;
}
int DOMSelection::focusOffset() const {
- if (!isAvailable())
- return 0;
-
- return shadowAdjustedOffset(focusPosition(visibleSelection()));
+ if (Range* range = primaryRangeOrNull()) {
+ if (!frame() || visibleSelection().isBaseFirst())
+ return range->endOffset();
+ return range->startOffset();
+ }
+ return 0;
}
Node* DOMSelection::baseNode() const {
@@ -153,21 +161,20 @@ int DOMSelection::extentOffset() const {
bool DOMSelection::isCollapsed() const {
if (!isAvailable() || selectionShadowAncestor(frame()))
return true;
- return !frame()->selection().isRange();
+ if (Range* range = primaryRangeOrNull())
+ return range->collapsed();
+ return true;
}
String DOMSelection::type() const {
if (!isAvailable())
return String();
-
- FrameSelection& selection = frame()->selection();
-
// This is a WebKit DOM extension, incompatible with an IE extension
// IE has this same attribute, but returns "none", "text" and "control"
// http://msdn.microsoft.com/en-us/library/ms534692(VS.85).aspx
- if (selection.isNone())
+ if (rangeCount() == 0)
return "None";
- if (selection.isCaret())
+ if (isCollapsed())
return "Caret";
return "Range";
}
@@ -414,7 +421,8 @@ void DOMSelection::extend(Node* node,
.build());
}
-Range* DOMSelection::getRangeAt(int index, ExceptionState& exceptionState) {
+Range* DOMSelection::getRangeAt(int index,
+ ExceptionState& exceptionState) const {
if (!isAvailable())
return nullptr;
@@ -435,26 +443,33 @@ Range* DOMSelection::getRangeAt(int index, ExceptionState& exceptionState) {
return range;
}
-Range* DOMSelection::createRangeFromSelectionEditor() {
+Range* DOMSelection::primaryRangeOrNull() const {
yosin_UTC9 2017/02/14 04:52:21 Thanks for better name! (^_^)b
+ return rangeCount() > 0 ? getRangeAt(0, ASSERT_NO_EXCEPTION) : nullptr;
+}
+
+Range* DOMSelection::createRangeFromSelectionEditor() const {
Position anchor = anchorPosition(visibleSelection());
- if (!anchor.anchorNode()->isInShadowTree())
+ if (isSelectionOfDocument() && !anchor.anchorNode()->isInShadowTree())
return frame()->selection().firstRange();
Node* node = shadowAdjustedNode(anchor);
if (!node) // crbug.com/595100
return nullptr;
- if (!visibleSelection().isBaseFirst())
- return Range::create(*anchor.document(), focusNode(), focusOffset(), node,
- anchorOffset());
- return Range::create(*anchor.document(), node, anchorOffset(), focusNode(),
- focusOffset());
+ Position focus = focusPosition(visibleSelection());
yosin_UTC9 2017/02/14 04:52:21 nit: s/Position/const Position&/
+ if (!visibleSelection().isBaseFirst()) {
+ return Range::create(*anchor.document(), shadowAdjustedNode(focus),
+ shadowAdjustedOffset(focus), node,
+ shadowAdjustedOffset(anchor));
+ }
+ return Range::create(*anchor.document(), node, shadowAdjustedOffset(anchor),
+ shadowAdjustedNode(focus), shadowAdjustedOffset(focus));
}
bool DOMSelection::isSelectionOfDocument() const {
return m_treeScope == m_treeScope->document();
}
-void DOMSelection::cacheRangeIfSelectionOfDocument(Range* range) {
+void DOMSelection::cacheRangeIfSelectionOfDocument(Range* range) const {
if (!isSelectionOfDocument())
return;
frame()->selection().cacheRangeOfDocument(range);

Powered by Google App Engine
This is Rietveld 408576698