Bug 191864 - Range and Selection broken with splitText() and normalize(). r=smaug
authorMats Palmgren <matspal@gmail.com>
Sat, 24 Sep 2011 02:56:38 +0200
changeset 77485 55f620801bc72e2ee6cca51de759733be546259f
parent 77484 8e0213fd66986ee7635dc759277645797e7a1096
child 77486 eafaff0efeefa2a069c724c864684e274371ed92
push id3
push userfelipc@gmail.com
push dateFri, 30 Sep 2011 20:09:13 +0000
reviewerssmaug
bugs191864
milestone9.0a1
Bug 191864 - Range and Selection broken with splitText() and normalize(). r=smaug
layout/generic/test/Makefile.in
layout/generic/test/test_selection_splitText-normalize.html
--- a/layout/generic/test/Makefile.in
+++ b/layout/generic/test/Makefile.in
@@ -120,16 +120,17 @@ include $(topsrcdir)/config/rules.mk
   file_bug579767_2.html \
   test_bug522632.html \
   test_bug589621.html \
   test_bug589623.html \
   test_bug633762.html \
   bug633762_iframe.html \
   $(srcdir)/../../reftests/backgrounds/blue-32x32.png \
   $(srcdir)/../../reftests/backgrounds/fuchsia-32x32.png \
+  test_selection_splitText-normalize.html \
   $(NULL)
 
 _CHROME_FILES = \
   test_bug632379.xul \
   test_bug508115.xul \
   test_bug469613.xul \
   test_bug469774.xul \
   test_backspace_delete.xul \
new file mode 100644
--- /dev/null
+++ b/layout/generic/test/test_selection_splitText-normalize.html
@@ -0,0 +1,173 @@
+<!DOCTYPE HTML>
+<html>
+<!--
+https://bugzilla.mozilla.org/show_bug.cgi?id=191864
+-->
+<head>
+  <title>Test for Bug 191864</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"/>
+</head>
+<body>
+<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=191864">Mozilla Bug 191864</a>
+<p id="display">
+<span id="col1" style="float:left; height:800px; width:180px;"></span>
+<span id="col2" style="float:left; height:800px; width:180px;"></span>
+</p>
+<div id="content" style="display: none">
+  
+</div>
+<pre id="test">
+<script type="application/javascript;version=1.7">
+var tests = [
+ [ {}, [0,4], "012345678" ],
+ [ {}, [0,0], "012345678" ],
+ [ {}, [0,9], "012345678" ],
+ [ {startOffset:4}, [0,4], "012345678" ],
+ [ {startOffset:5}, [0,4], "012345678" ],
+ [ {startOffset:5,endOffset:6}, [0,4], "012345678" ],
+ [ {endOffset:5}, [0,4], "012345678" ],
+ [ {endOffset:4}, [0,4], "012345678" ],
+ [ {endOffset:3}, [0,4], "012345678" ],
+ [ {startOffset:1,endOffset:3}, [0,4], "012345678" ],
+ [ {startOffset:7,endOffset:7}, [0,4], "012345678" ],
+ [ {startOffset:4,endOffset:4}, [0,4], "012345678" ],
+ [ {endNode:1}, [0,4], "012345678", "" ],
+ [ {endNode:1}, [0,4], "01234567", "8" ],
+ [ {endNode:1}, [1,4], "0", "12345678" ],
+ [ {endNode:2}, [1,4], "0", "12345", "678" ],
+]
+
+function runtest(r,p,t) {
+  // create content
+  document.original_nodes = [];
+  for (let i = 2; i < t.length; ++i) {
+    c = document.createTextNode(t[i]);
+    p.appendChild(c);
+    document.original_nodes.push(c);
+  }
+
+  // setup the range
+  let sel = t[0]
+  let startNode = p.firstChild;
+  let startOffset = sel.startOffset === undefined ? 0 : sel.startOffset;
+  let endNode = sel.endNode === undefined ? startNode : p.childNodes[sel.endNode];
+  let endOffset = sel.endOffset === undefined ? endNode.length : sel.endOffset;
+  r.setStart(startNode, startOffset);
+  r.setEnd(endNode, endOffset);
+
+  // splitText
+  let split = t[1]
+  p.childNodes[split[0]].splitText(split[1])
+}
+function test_split(r,p,t) {
+    runtest(r,p,t);
+}
+function test_split_merge(r,p,t) {
+    runtest(r,p,t);
+    p.normalize();
+}
+</script>
+
+<script type="application/javascript;version=1.7">
+
+/** Test for Bug 191864 **/
+
+var results = [
+/* test_split */
+ [ {},                                        [ [0, "0123"], "45678" ]],
+ [ {},                                        [ [0, ""], "012345678" ]],
+ [ {endNode:0, endOffset:9},                  [ [0, "012345678"], "" ]],
+ [ {startOffset:4},                           [ [0, "0123"], "45678" ]],
+ [ {startNode:1, startOffset:1},              [ [0, "0123"], "45678" ]],
+ [ {startNode:1, startOffset:1, endOffset:2}, [ [0, "0123"], "45678" ]],
+ [ {endOffset:1},                             [ [0, "0123"], "45678" ]],
+ [ {endNode:0},                               [ [0, "0123"], "45678" ]],
+ [ {endNode:0, endOffset:3},                  [ [0, "0123"], "45678" ]],
+ [ {startOffset:1, endNode:0, endOffset:3},   [ [0, "0123"], "45678" ]],
+ [ {startNode:1, startOffset:3, endOffset:3}, [ [0, "0123"], "45678" ]],
+ [ {startOffset:4, endNode:0},                [ [0, "0123"], "45678" ]],
+ [ {endNode:2, endOffset:0},                  [ [0, "0123"], "45678", [1, ""] ]],
+ [ {endNode:2},                               [ [0, "0123"], "4567", [1, "8"] ]],
+ [ {endNode:2},                               [ [0, "0"], [1, "1234"], "5678" ]],
+ [ {endNode:3},                               [ [0, "0"], [1, "1234"], "5", [2, "678"] ]],
+/* test_split_merge */
+ [ {},                                        [ [0, "012345678" ] ]],
+ [ {startParent:true},                        [ "012345678" ]],     /* splitText() creates an empty first child which is removed by normalize() */
+ [ {},                                        [ [0, "012345678" ] ]],
+ [ {startOffset:4},                           [ [0, "012345678" ] ]],
+ [ {startOffset:5},                           [ [0, "012345678" ] ]],
+ [ {startOffset:5,endOffset:6},               [ [0, "012345678" ] ]],
+ [ {endOffset:5},                             [ [0, "012345678" ] ]],
+ [ {endOffset:4},                             [ [0, "012345678" ] ]],
+ [ {endOffset:3},                             [ [0, "012345678" ] ]],
+ [ {startOffset:1,endOffset:3},               [ [0, "012345678" ] ]],
+ [ {startOffset:7,endOffset:7},               [ [0, "012345678" ] ]],
+ [ {startOffset:4,endOffset:4},               [ [0, "012345678" ] ]],
+ [ {endParent:true},                          [ [0, "012345678" ] ]],
+ [ {},                                        [ [0, "012345678" ] ]],
+ [ {},                                        [ [0, "012345678" ] ]],
+ [ {},                                        [ [0, "012345678" ] ]],
+]
+
+function verifyResults(r,p,i) {
+  let nodes = results[i][1];
+  is(p.childNodes.length, nodes.length, "same number of DOM nodes" + " (test " + i + ")");
+  for (let j = 0; j < nodes.length; ++j) {
+    let a = nodes[j];
+    let b = p.childNodes[j];
+    if (a instanceof Array) {
+      is(b, document.original_nodes[a[0]], "same node" + " (test " + i + " child " + j + ")");
+      is(b.textContent, a[1], "contents2" + " (test " + i + " child " + j + ")");
+    } else {
+      is(b.nodeType, Node.TEXT_NODE, "text node" + " (test " + i + " child " + j + ")");
+      is(b.textContent, a, "contents1" + " (test " + i + " child " + j + ")");
+    }
+  }
+  let sel = results[i][0];
+  if (sel.todo) {
+    alert(r.startContainer + '\n' + r.startOffset + '\n' + r.endContainer + '\n' + r.endOffset + '\n')
+    return;
+  }
+  let startNode = sel.startNode === undefined ? p.firstChild : p.childNodes[sel.startNode];
+  let startOffset = sel.startOffset === undefined ? 0 : sel.startOffset;
+  if (sel.startParent) { startNode = p; startOffset = 0; }
+  let endNode = sel.endNode === undefined ? p.childNodes[p.childNodes.length>1 ? 1 : 0] : p.childNodes[sel.endNode];
+  let endOffset = sel.endOffset === undefined ? endNode.length : sel.endOffset;
+  if (sel.endParent) { endNode = p; endOffset = 1; }
+  is(r.startContainer, startNode, "range start node" + " (test " + i + ")");
+  is(r.startOffset, startOffset, "range start offset" + " (test " + i + ")");
+  is(r.endContainer, endNode, "range end node" + " (test " + i + ")");
+  is(r.endOffset, endOffset, "range end offset" + " (test " + i + ")");
+}
+
+function runTest() {
+  let col1 = document.getElementById('col1');
+  let col2 = document.getElementById('col2');
+  for (let i=0; i < tests.length; ++i) {
+    let t = tests[i];
+    let p = document.createElement('p')
+    col1.appendChild(p);
+    let r = document.createRange();
+    test_split(r,p,t);
+    verifyResults(r,p,i);
+  }
+  for (let i=0; i < tests.length; ++i) {
+    let t = tests[i];
+    let p = document.createElement('p')
+    col2.appendChild(p);
+    let r = document.createRange();
+    test_split_merge(r,p,t);
+    verifyResults(r,p,i+tests.length);
+  }
+  SimpleTest.finish();
+}
+
+window.onload = function() { setTimeout(runTest, 0); };
+SimpleTest.waitForExplicitFinish();
+
+</script>
+</pre>
+</body>
+</html>