Bug 498240 - Range.insertNode() causing NS_ERROR_DOM_INDEX_SIZE_ERR. r+sr=peterv
authorMats Palmgren <matspal@gmail.com>
Wed, 19 Aug 2009 02:18:24 +0200
changeset 31645 0dfc4dfaa37084a8985c9971f5d9b58acae1820d
parent 31644 aef07655f47fc440ec7e382c03de26b35c9dc851
child 31646 77a4f69750e6b7a7c5e6f5c6459320a1c8d89668
push id8639
push usermpalmgren@mozilla.com
push dateWed, 19 Aug 2009 00:18:46 +0000
treeherdermozilla-central@0dfc4dfaa370 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
bugs498240
milestone1.9.3a1pre
Bug 498240 - Range.insertNode() causing NS_ERROR_DOM_INDEX_SIZE_ERR. r+sr=peterv
content/base/src/nsRange.cpp
content/base/test/Makefile.in
content/base/test/test_bug498240.html
--- a/content/base/src/nsRange.cpp
+++ b/content/base/src/nsRange.cpp
@@ -1799,37 +1799,36 @@ nsresult nsRange::InsertNode(nsIDOMNode*
 
   nsCOMPtr<nsIDOMText> startTextNode(do_QueryInterface(tStartContainer));
   if (startTextNode)
   {
     nsCOMPtr<nsIDOMNode> tSCParentNode;
     res = tStartContainer->GetParentNode(getter_AddRefs(tSCParentNode));
     if(NS_FAILED(res)) return res;
     NS_ENSURE_STATE(tSCParentNode);
-    
-    PRBool isCollapsed;
-    res = GetCollapsed(&isCollapsed);
-    if(NS_FAILED(res)) return res;
 
     PRInt32 tEndOffset;
     GetEndOffset(&tEndOffset);
 
+    nsCOMPtr<nsIDOMNode> tEndContainer;
+    res = this->GetEndContainer(getter_AddRefs(tEndContainer));
+    if(NS_FAILED(res)) return res;
+
     nsCOMPtr<nsIDOMText> secondPart;
     res = startTextNode->SplitText(tStartOffset, getter_AddRefs(secondPart));
     if (NS_FAILED(res)) return res;
-    
-    // SplitText collapses the range; fix that (bug 253609)
-    if (!isCollapsed)
-    {
+
+    nsCOMPtr<nsIDOMNode> tResultNode;
+    res = tSCParentNode->InsertBefore(aN, secondPart, getter_AddRefs(tResultNode));
+    if (NS_FAILED(res)) return res;
+
+    if (tEndContainer == tStartContainer && tEndOffset != tStartOffset)
       res = SetEnd(secondPart, tEndOffset - tStartOffset);
-      if(NS_FAILED(res)) return res;
-    }
-    
-    nsCOMPtr<nsIDOMNode> tResultNode;
-    return tSCParentNode->InsertBefore(aN, secondPart, getter_AddRefs(tResultNode));
+
+    return res;
   }  
 
   nsCOMPtr<nsIDOMNodeList>tChildList;
   res = tStartContainer->GetChildNodes(getter_AddRefs(tChildList));
   if(NS_FAILED(res)) return res;
   PRUint32 tChildListLength;
   res = tChildList->GetLength(&tChildListLength);
   if(NS_FAILED(res)) return res;
--- a/content/base/test/Makefile.in
+++ b/content/base/test/Makefile.in
@@ -306,16 +306,17 @@ include $(topsrcdir)/config/rules.mk
 		test_bug473162-2.html \
 		test_XHRSendData.html \
 		file_XHRSendData.sjs \
 		file_XHRSendData_doc.xml \
 		file_XHRSendData_doc.xml^headers^ \
 		test_bug466751.xhtml \
 		test_bug461555.html \
 		test_sync_xhr_timer.xhtml \
+		test_bug498240.html \
 		file_htmlserializer_ipv6.html \
 		file_htmlserializer_ipv6_out.html \
 		test_bug498433.html \
 		test_bug498897.html \
 		file_bug498897.html \
 		file_bug498897.html^headers^ \
 		file_bug498897.css \
 		test_bug493881.js \
new file mode 100644
--- /dev/null
+++ b/content/base/test/test_bug498240.html
@@ -0,0 +1,258 @@
+<!DOCTYPE HTML>
+<html>
+<!--
+https://bugzilla.mozilla.org/show_bug.cgi?id=498240
+-->
+<head>
+  <title>Test for Bug 498240</title>
+  <script type="application/javascript" src="/MochiKit/packed.js"></script>
+  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
+  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
+<style>
+ .container { border: 1px solid blue; display:block; }
+ b { color:blue; }
+ i { color:magenta; }
+</style>
+</head>
+<body>
+<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=498240">Mozilla Bug 498240</a>
+<p id="display"></p>
+<div id="content" style="display: none">
+  
+</div>
+<pre id="test">
+<script type="application/javascript">
+
+/** Test for Bug 498240 **/
+
+
+function create(s) {
+  var p = document.createElement('span');
+  p.innerHTML = s;
+  p.setAttribute("class","container");
+  document.body.appendChild(p);
+  return p;
+}
+function select(start, startOffset, end, endOffset) {
+  var sel = getSelection();
+  sel.removeAllRanges();
+  var range = document.createRange();
+  range.setStart(start, startOffset);
+  range.setEnd(end, endOffset);
+  sel.addRange(range);
+}
+
+function insertClone(node) {
+  var sel = getSelection();
+  var range = sel.getRangeAt(0);
+  range.insertNode(node.cloneNode(true));
+}
+function insertCloneAtEnd(node) {
+  var sel = getSelection();
+  var range = sel.getRangeAt(0);
+  range.endContainer.insertBefore(node.cloneNode(true),range.endContainer.childNodes[range.endOffset]);
+}
+
+function check(start, startOffset, end, endOffset, s) {
+  var sel = getSelection();
+  var range = sel.getRangeAt(0);
+  is(range.startContainer, start, "wrong start node for range: '"+range.toString()+"'");
+  is(range.startOffset, startOffset, "wrong start offset for range: '"+range.toString()+"'");
+  is(range.endContainer, end, "wrong end node for range: '"+range.toString()+"'");
+  is(range.endOffset, endOffset, "wrong end offset for range: '"+range.toString()+"'");
+}
+
+function testInsertNode(node) {
+  var p;
+
+  p = create('a<b>bc</b>');
+  select(p.childNodes[0],0,p.childNodes[1],0);
+  insertClone(node);
+  check(p.childNodes[0],0,p.childNodes[3],0);
+
+  p = create('d<b>ef</b>');
+  select(p.childNodes[0],0,p.childNodes[1],1);
+  insertClone(node);
+  check(p.childNodes[0],0,p.childNodes[3],1);
+
+  p = create('g<b>h</b>');
+  select(p.childNodes[0],0,p.childNodes[0],0);
+  insertClone(node);
+  check(p.childNodes[0],0,p.childNodes[0],0);
+
+  p = create('i<b>j</b>');
+  select(p.childNodes[0],1,p.childNodes[0],1);
+  insertClone(node);
+  check(p.childNodes[0],1,p.childNodes[0],1);
+
+  p = create('k<b>l</b>');
+  select(p.childNodes[0],0,p.childNodes[1].childNodes[0],0);
+  insertClone(node);
+  check(p.childNodes[0],0,p.childNodes[3].childNodes[0],0);
+
+  p = create('m<b>no</b>');
+  select(p.childNodes[0],1,p.childNodes[1].childNodes[0],0);
+  insertClone(node);
+  check(p.childNodes[0],1,p.childNodes[3].childNodes[0],0);
+
+  p = create('p<b>qr</b>');
+  select(p.childNodes[0],1,p.childNodes[1].childNodes[0],1);
+  insertClone(node);
+  check(p.childNodes[0],1,p.childNodes[3].childNodes[0],1);
+
+  p = create('s<b>tu</b>');
+  select(p.childNodes[0],1,p.childNodes[1],0);
+  insertClone(node);
+  check(p.childNodes[0],1,p.childNodes[3],0);
+
+  p = create('<i>A</i><b>BC</b>');
+  select(p.childNodes[0],0,p.childNodes[1],0);
+  insertClone(node);
+  check(p.childNodes[0],0,p.childNodes[1],0);
+
+  p = create('<i>D</i><b>EF</b>');
+  select(p.childNodes[0],1,p.childNodes[1],1);
+  insertClone(node);
+  check(p.childNodes[0],1,p.childNodes[1],1);
+
+  p = create('<i></i><b>GH</b>');
+  select(p.childNodes[0],0,p.childNodes[1],0);
+  insertClone(node);
+  check(p.childNodes[0],0,p.childNodes[1],0);
+
+  p = create('<i>I</i><b>J</b>');
+  select(p,0,p.childNodes[1],0);
+  insertClone(node);
+  check(p,0,p.childNodes[2],0);
+
+  p = create('<i>K</i><b>L</b>');
+  select(p,0,p,2);
+  insertClone(node);
+  check(p,0,p,3);
+
+  p = create('<i>M</i><b>N</b>');
+  select(p,1,p,2);
+  insertClone(node);
+  check(p,1,p,3);
+
+  p = create('<i>O</i><b>P</b>');
+  select(p,1,p,1);
+  insertClone(node);
+  check(p,1,p,1);
+
+  p = create('<i>Q</i><b>R</b>');
+  select(p,2,p,2);
+  insertClone(node);
+  check(p,2,p,2);
+
+  p = create('<i>S</i><b>T</b>');
+  select(p,1,p,1);
+  insertCloneAtEnd(node);
+  check(p,1,p,1);
+
+  p = create('<i>U</i><b>V</b>');
+  select(p,2,p,2);
+  insertCloneAtEnd(node);
+  check(p,2,p,2);
+
+  p = create('<i>X</i><b>Y</b>');
+  select(p,0,p,1);
+  insertCloneAtEnd(node);
+  check(p,0,p,1);
+
+  p = create('<i>X</i><b><s>Y</s></b>');
+  select(p,0,p.childNodes[1],1);
+  insertCloneAtEnd(node);
+  check(p,0,p.childNodes[1],1);
+
+  p = create('<i>Z</i><b></b>');
+  select(p,0,p.childNodes[1],0);
+  insertCloneAtEnd(node);
+  check(p,0,p.childNodes[1],0);
+
+  p = create('<i>ZA</i><b><s>ZB</s><u>ZC</u></b>');
+  select(p,0,p.childNodes[1],1);
+  insertCloneAtEnd(node);
+  check(p,0,p.childNodes[1],1);
+}
+function testInvalidNodeType(node) {
+  const INVALID_NODE_TYPE_ERR = 2;
+  try { 
+    testInsertNode(node); 
+    ok(false,"Expected a RangeException::INVALID_NODE_TYPE_ERR"); 
+  } catch(e) {
+    ok(e instanceof RangeException, "Wrong type of exception: " + e);
+    is(e.code, INVALID_NODE_TYPE_ERR, "Wrong exception code, expected INVALID_NODE_TYPE_ERR");
+  }
+}
+
+function runTest() {
+  testInsertNode(document.createTextNode('123'));
+
+  var i = document.createElement('SPAN')
+  i.innerHTML='456'
+  testInsertNode(i);
+
+  i = document.createDocumentFragment();
+  i.appendChild(document.createTextNode('789'));
+  testInsertNode(i);
+
+  /// DOM2 Traversal and Range Specification 2.13 "insertNode":
+  ///   RangeException INVALID_NODE_TYPE_ERR: Raised if newNode is an Attr, Entity, Notation, or Document node.
+  // BUG: testInvalidNodeType(document.createAttribute('a'));
+  todo(false, "Test insertion of Entity node into range");
+  // TODO: testInvalidNodeType(document.createEntity());
+  todo(false, "Test insertion of Notation node into range");
+  // TODO: testInvalidNodeType(document.createNotation());
+  // BUG: testInvalidNodeType(document.implementation.createDocument('http://www.w3.org/1999/xhtml', 'html', null));
+
+  todo(false, "test that Range::insertNode() throws DOMException::WRONG_DOCUMENT_ERR when it should");
+/* TODO: This works but shouldn't:
+  const WRONG_DOCUMENT_ERR = 4;
+  i = document.implementation.createDocument('http://www.w3.org/1999/xhtml', 'html', null).createElement('html');
+  try { 
+    testInsertNode(i);
+    ok(false,"Expected a DOMException::WRONG_DOCUMENT_ERR"); 
+  } catch(e) {
+    ok(e instanceof DOMException, "Wrong type of exception: " + e);
+    is(e.code, WRONG_DOCUMENT_ERR, "Wrong exception code, expected WRONG_DOCUMENT_ERR");
+  }
+*/
+
+  // Inserting an ancestor of the start container should throw HIERARCHY_REQUEST_ERR
+  todo(false, "test that Range::insertNode() throws HIERARCHY_REQUEST_ERR when it should");
+/* TODO: This works but shouldn't:
+  const HIERARCHY_REQUEST_ERR = 3;
+  var p = create('<b>IJK</b>');
+  select(p.childNodes[0],0,p.childNodes[0],1);
+  var sel = getSelection();
+  var range = sel.getRangeAt(0);
+  try { 
+    range.insertNode(p);
+    ok(false,"Expected a DOMException::HIERARCHY_REQUEST_ERR"); 
+  } catch(e) {
+    ok(e instanceof DOMException, "Wrong type of exception: " + e);
+    is(e.code, HIERARCHY_REQUEST_ERR, "Wrong exception code, expected HIERARCHY_REQUEST_ERR");
+  }
+*/
+
+  // TODO: we should also have a test for:
+  /// "HIERARCHY_REQUEST_ERR: Raised if the container of the start of the Range is of a type
+  ///   that does not allow children of the type of newNode"
+
+  todo(false, "INVALID_STATE_ERR test goes here...");
+
+  var sel = getSelection();
+  sel.removeAllRanges();
+
+  SimpleTest.finish();
+}
+
+SimpleTest.waitForExplicitFinish();
+addLoadEvent(runTest);
+
+
+</script>
+</pre>
+</body>
+</html>