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

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

Issue 23404003: Make Range methods to work with detached node (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: 2013-08-27T11:16:22 Created 7 years, 4 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 | « 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: Source/core/dom/Range.cpp
diff --git a/Source/core/dom/Range.cpp b/Source/core/dom/Range.cpp
index 8d1ad9dbd007768d2f3207bf8de2efef9a856474..ba27e67625fe27f0ed5ea2219d17fb5badc158aa 100644
--- a/Source/core/dom/Range.cpp
+++ b/Source/core/dom/Range.cpp
@@ -25,6 +25,7 @@
#include "config.h"
#include "core/dom/Range.h"
+#include "bindings/v8/ExceptionMessages.h"
#include "bindings/v8/ExceptionState.h"
#include "bindings/v8/ExceptionStatePlaceholder.h"
#include "core/dom/ClientRect.h"
@@ -1159,12 +1160,27 @@ Node* Range::checkNodeWOffset(Node* n, int offset, ExceptionState& es) const
return 0;
}
-void Range::checkNodeBA(Node* n, ExceptionState& es) const
+void Range::checkNodeBA(Node* n, ExceptionState& es, const char* methodName) const
{
+ if (!m_start.container()) {
+ es.throwDOMException(InvalidStateError);
+ return;
+ }
+
+ if (!n) {
+ es.throwDOMException(NotFoundError);
+ return;
+ }
+
// InvalidNodeTypeError: Raised if the root container of refNode is not an
// Attr, Document, DocumentFragment or ShadowRoot node, or part of a SVG shadow DOM tree,
// or if refNode is a Document, DocumentFragment, ShadowRoot, Attr, Entity, or Notation node.
+ if (!n->parentNode()) {
+ es.throwDOMException(InvalidNodeTypeError, ExceptionMessages::failedToExecute(methodName, "Range", "the given Node has no parent."));
+ return;
+ }
+
switch (n->nodeType()) {
case Node::ATTRIBUTE_NODE:
case Node::DOCUMENT_FRAGMENT_NODE:
@@ -1191,11 +1207,11 @@ void Range::checkNodeBA(Node* n, ExceptionState& es) const
case Node::ATTRIBUTE_NODE:
case Node::DOCUMENT_NODE:
case Node::DOCUMENT_FRAGMENT_NODE:
+ case Node::ELEMENT_NODE:
break;
case Node::CDATA_SECTION_NODE:
case Node::COMMENT_NODE:
case Node::DOCUMENT_TYPE_NODE:
- case Node::ELEMENT_NODE:
case Node::ENTITY_NODE:
case Node::NOTATION_NODE:
case Node::PROCESSING_INSTRUCTION_NODE:
@@ -1218,17 +1234,7 @@ PassRefPtr<Range> Range::cloneRange(ExceptionState& es) const
void Range::setStartAfter(Node* refNode, ExceptionState& es)
{
- if (!m_start.container()) {
- es.throwDOMException(InvalidStateError);
- return;
- }
-
- if (!refNode) {
- es.throwDOMException(NotFoundError);
- return;
- }
-
- checkNodeBA(refNode, es);
+ checkNodeBA(refNode, es, "setStartAfter");
if (es.hadException())
return;
@@ -1237,17 +1243,7 @@ void Range::setStartAfter(Node* refNode, ExceptionState& es)
void Range::setEndBefore(Node* refNode, ExceptionState& es)
{
- if (!m_start.container()) {
- es.throwDOMException(InvalidStateError);
- return;
- }
-
- if (!refNode) {
- es.throwDOMException(NotFoundError);
- return;
- }
-
- checkNodeBA(refNode, es);
+ checkNodeBA(refNode, es, "setEndBefore");
if (es.hadException())
return;
@@ -1256,17 +1252,7 @@ void Range::setEndBefore(Node* refNode, ExceptionState& es)
void Range::setEndAfter(Node* refNode, ExceptionState& es)
{
- if (!m_start.container()) {
- es.throwDOMException(InvalidStateError);
- return;
- }
-
- if (!refNode) {
- es.throwDOMException(NotFoundError);
- return;
- }
-
- checkNodeBA(refNode, es);
+ checkNodeBA(refNode, es, "setEndAfter");
if (es.hadException())
return;
@@ -1285,6 +1271,11 @@ void Range::selectNode(Node* refNode, ExceptionState& es)
return;
}
+ if (!refNode->parentNode()) {
+ es.throwDOMException(InvalidNodeTypeError, ExceptionMessages::failedToExecute("selectNode", "Range", "the given Node has no parent."));
+ return;
+ }
+
// InvalidNodeTypeError: Raised if an ancestor of refNode is an Entity, Notation or
// DocumentType node or if refNode is a Document, DocumentFragment, ShadowRoot, Attr, Entity, or Notation
// node.
@@ -1329,10 +1320,8 @@ void Range::selectNode(Node* refNode, ExceptionState& es)
if (m_ownerDocument != refNode->document())
setDocument(refNode->document());
- setStartBefore(refNode, es);
- if (es.hadException())
- return;
- setEndAfter(refNode, es);
+ setStartBefore(refNode);
+ setEndAfter(refNode);
}
void Range::selectNodeContents(Node* refNode, ExceptionState& es)
@@ -1462,17 +1451,7 @@ void Range::surroundContents(PassRefPtr<Node> passNewParent, ExceptionState& es)
void Range::setStartBefore(Node* refNode, ExceptionState& es)
{
- if (!m_start.container()) {
- es.throwDOMException(InvalidStateError);
- return;
- }
-
- if (!refNode) {
- es.throwDOMException(NotFoundError);
- return;
- }
-
- checkNodeBA(refNode, es);
+ checkNodeBA(refNode, es, "setStartBefore");
if (es.hadException())
return;
« no previous file with comments | « Source/core/dom/Range.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698