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

Unified Diff: third_party/WebKit/Source/core/dom/Range.cpp

Issue 2416963007: Range object should check root node identify instead of inActiveDocument(). (Closed)
Patch Set: Use isInTreeScope Created 4 years, 2 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
« no previous file with comments | « third_party/WebKit/Source/core/dom/Range.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: third_party/WebKit/Source/core/dom/Range.cpp
diff --git a/third_party/WebKit/Source/core/dom/Range.cpp b/third_party/WebKit/Source/core/dom/Range.cpp
index 6021e30bf9b6297558fb3eae9767502956ad1880..16b3bb865efefaae628b8b53b2c36d81146921b8 100644
--- a/third_party/WebKit/Source/core/dom/Range.cpp
+++ b/third_party/WebKit/Source/core/dom/Range.cpp
@@ -247,6 +247,17 @@ bool Range::isNodeFullyContained(Node& node) const {
// points.
}
+bool Range::hasSameRoot(const Node& node) const {
+ if (node.document() != m_ownerDocument)
+ return false;
+ // commonAncestorContainer() is O(depth). We should avoid to call it in common
+ // cases.
+ if (node.isInTreeScope() && m_start.container()->isInTreeScope() &&
+ &node.treeScope() == &m_start.container()->treeScope())
+ return true;
+ return node.commonAncestor(*m_start.container(), NodeTraversal::parent);
+}
+
bool Range::isPointInRange(Node* refNode,
int offset,
ExceptionState& exceptionState) const {
@@ -256,10 +267,8 @@ bool Range::isPointInRange(Node* refNode,
exceptionState.throwTypeError("The node provided is null.");
return false;
}
-
- if (!refNode->inActiveDocument() || refNode->document() != m_ownerDocument) {
+ if (!hasSameRoot(*refNode))
return false;
- }
checkNodeWOffset(refNode, offset, exceptionState);
if (exceptionState.hadException())
@@ -281,16 +290,10 @@ short Range::comparePoint(Node* refNode,
// refNode node and an offset within the node is before, same as, or after the
// range respectively.
- if (!refNode->inActiveDocument()) {
- exceptionState.throwDOMException(
- WrongDocumentError, "The node provided is not in an active document.");
- return 0;
- }
-
- if (refNode->document() != m_ownerDocument) {
+ if (!hasSameRoot(*refNode)) {
exceptionState.throwDOMException(
WrongDocumentError,
- "The node provided is not in this Range's Document.");
+ "The node provided and the Range are not in the same tree.");
return 0;
}
@@ -405,28 +408,16 @@ void Range::deleteContents(ExceptionState& exceptionState) {
}
}
-static bool nodeValidForIntersects(Node* refNode,
- Document* expectedDocument,
- ExceptionState& exceptionState) {
+bool Range::intersectsNode(Node* refNode, ExceptionState& exceptionState) {
+ // http://developer.mozilla.org/en/docs/DOM:range.intersectsNode
+ // Returns a bool if the node intersects the range.
if (!refNode) {
// FIXME: Generated bindings code never calls with null, and neither should
// other callers!
exceptionState.throwTypeError("The node provided is null.");
return false;
}
-
- if (!refNode->inActiveDocument() || refNode->document() != expectedDocument) {
- // Firefox doesn't throw an exception for these cases; it returns false.
- return false;
- }
-
- return true;
-}
-
-bool Range::intersectsNode(Node* refNode, ExceptionState& exceptionState) {
- // http://developer.mozilla.org/en/docs/DOM:range.intersectsNode
- // Returns a bool if the node intersects the range.
- if (!nodeValidForIntersects(refNode, m_ownerDocument.get(), exceptionState))
+ if (!hasSameRoot(*refNode))
return false;
ContainerNode* parentNode = refNode->parentNode();
« no previous file with comments | « third_party/WebKit/Source/core/dom/Range.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698