Bug 453521, range.extractContents should return empty document fragment if range is collapsed, r+sr=sicking
authorOlli Pettay <Olli.Pettay@helsinki.fi>
Tue, 09 Sep 2008 12:17:35 +0300
changeset 19010 4a3db9fd347995b214ae7bfd7e54837b2d1b1fc7
parent 19009 f5c472568bf38d3936a570df1a30e8abe1002e7a
child 19011 e605cabea867745982097355375632c4ecd74e0f
push id1905
push useropettay@mozilla.com
push dateTue, 09 Sep 2008 09:17:52 +0000
treeherderautoland@4a3db9fd3479 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
bugs453521
milestone1.9.1b1pre
Bug 453521, range.extractContents should return empty document fragment if range is collapsed, r+sr=sicking
content/base/src/nsRange.cpp
content/base/test/Makefile.in
content/base/test/test_bug453521.html
--- a/content/base/src/nsRange.cpp
+++ b/content/base/src/nsRange.cpp
@@ -1152,16 +1152,20 @@ static nsresult SplitDataNode(nsIDOMChar
   rv = dataNode->SplitData(aStartIndex, getter_AddRefs(newData),
                            aCloneAfterOriginal);
   NS_ENSURE_SUCCESS(rv, rv);
   return CallQueryInterface(newData, aMiddleNode);
 }
 
 nsresult nsRange::CutContents(nsIDOMDocumentFragment** aFragment)
 { 
+  if (aFragment) {
+    *aFragment = nsnull;
+  }
+
   if (IsDetached())
     return NS_ERROR_DOM_INVALID_STATE_ERR;
 
   nsresult rv;
 
   nsCOMPtr<nsIDocument> doc =
     do_QueryInterface(mStartParent->GetOwnerDoc());
   if (!doc) return NS_ERROR_UNEXPECTED;
@@ -1191,17 +1195,21 @@ nsresult nsRange::CutContents(nsIDOMDocu
   RangeSubtreeIterator iter;
 
   rv = iter.Init(this);
   if (NS_FAILED(rv)) return rv;
 
   if (iter.IsDone())
   {
     // There's nothing for us to delete.
-    return CollapseRangeAfterDelete(this);
+    rv = CollapseRangeAfterDelete(this);
+    if (NS_SUCCEEDED(rv) && aFragment) {
+      NS_ADDREF(*aFragment = retval);
+    }
+    return rv;
   }
 
   // We delete backwards to avoid iterator problems!
 
   iter.Last();
   nsCOMPtr<nsIDOMNode> lastFragmentNode = nsnull;
 
   PRBool handled = PR_FALSE;
@@ -1350,21 +1358,21 @@ nsresult nsRange::CutContents(nsIDOMDocu
   // XXX_kin: and to use Normalize() to do the merging, but
   // XXX_kin: calling Normalize() on the common parent to accomplish
   // XXX_kin: this might also normalize nodes that are outside the
   // XXX_kin: range but under the common parent. Need to verify
   // XXX_kin: with the range commitee members that this was the
   // XXX_kin: desired behavior. For now we don't merge anything!
   // XXX ajvincent Filed as https://bugzilla.mozilla.org/show_bug.cgi?id=401276
 
-  if (aFragment) {
+  rv = CollapseRangeAfterDelete(this);
+  if (NS_SUCCEEDED(rv) && aFragment) {
     NS_ADDREF(*aFragment = retval);
   }
-
-  return CollapseRangeAfterDelete(this);
+  return rv;
 }
 
 nsresult nsRange::DeleteContents()
 {
   return CutContents(nsnull);
 }
 
 nsresult nsRange::ExtractContents(nsIDOMDocumentFragment** aReturn)
--- a/content/base/test/Makefile.in
+++ b/content/base/test/Makefile.in
@@ -194,16 +194,17 @@ include $(topsrcdir)/config/rules.mk
 		wholeTexty-helper.xml \
 		test_bug444030.xhtml \
 		test_NodeIterator_basics_filters.xhtml \
 		test_NodeIterator_mutations_1.xhtml \
 		test_NodeIterator_mutations_2.html \
 		test_bug28293.html \
 		file_bug28293.sjs \
 		test_title.html \
+		test_bug453521.html \
 		$(NULL)
 
 # bug 451664
 #		test_bug435425.html \
 #		bug435425.sjs \
 #		bug435425_redirect.sjs \
 
 libs:: $(_TEST_FILES)
new file mode 100644
--- /dev/null
+++ b/content/base/test/test_bug453521.html
@@ -0,0 +1,37 @@
+<!DOCTYPE HTML>
+<html>
+<!--
+https://bugzilla.mozilla.org/show_bug.cgi?id=453521
+-->
+<head>
+  <title>Test for Bug 453521</title>
+  <script type="text/javascript" src="/MochiKit/MochiKit.js"></script>
+  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
+  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
+</head>
+<body>
+<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=453521">Mozilla Bug 453521</a>
+<p id="display"></p>
+<div id="content" style="display: none">
+  
+</div>
+<pre id="test">
+<script class="testbody" type="text/javascript">
+
+/** Test for Bug 453521 **/
+
+
+var r = document.createRange();
+r.setStart(document.documentElement, 0);
+r.setEnd(document.documentElement, 0);
+ok(r.extractContents() != null,
+   "range.extractContents() shouldn't return null.");
+is(r.extractContents().nodeType, Node.DOCUMENT_FRAGMENT_NODE,
+   "range.extractContents() should return a document fragment.");
+
+
+</script>
+</pre>
+</body>
+</html>
+