editor/libeditor/html/tests/test_bug611182.html
author Ehsan Akhgari <ehsan@mozilla.com>
Thu, 11 Nov 2010 16:40:52 -0500
changeset 57481 54d00e3411ab46ca27d20bfe9528aebcc10bcb32
parent 57479 ff35e522f880a6bb6c08b7b9e36747c157e7fcce
child 57482 553642bc2cfacead753059b8da9951c24acc3da3
permissions -rw-r--r--
Bug 611182 - Part 3: Handle dynamic changes to the editable documents and create/remove the bogus node if needed; r=bzbarsky a=blocking-beta8+

<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=611182
-->
<head>
  <title>Test for Bug 611182</title>
  <script type="application/javascript" src="/MochiKit/packed.js"></script>
  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <script type="text/javascript" src="/tests/SimpleTest/EventUtils.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=611182">Mozilla Bug 611182</a>
<p id="display"></p>
<div id="content">
  <iframe></iframe>
</div>
<pre id="test">
<script type="application/javascript">

/** Test for Bug 611182 **/
SimpleTest.waitForExplicitFinish();
SimpleTest.waitForFocus(function() {
  var iframe = document.querySelector("iframe");

  function findTextNode(doc) {
    var body = doc.body;
    var result = findTextNodeWorker(body);
    ok(result, "Failed to find the text node");
    return result;
  }

  function findTextNodeWorker(root) {
    for (var i = 0; i < root.childNodes.length; ++i) {
      var node = root.childNodes[i];
      if (node.nodeType == node.TEXT_NODE &&
          node.nodeValue == "fooz bar") {
        root.focus();
        return node;
      }
      if (node.nodeType == node.ELEMENT_NODE) {
        node = findTextNodeWorker(node);
        if (node) {
          root.focus();
          return node;
        }
      }
    }
    ok(false, "Failed to find the text node");
    return null;
  }

  function testBackspace(src, callback) {
    ok(true, "Testing " + src);
    iframe.addEventListener("load", function() {
      iframe.removeEventListener("load", arguments.callee, false);

      var doc = iframe.contentDocument;
      var win = iframe.contentWindow;
      iframe.focus();
      var textNode = findTextNode(doc);
      var sel = win.getSelection();
      sel.collapse(textNode, 4);
      synthesizeKey("VK_BACK_SPACE", {});
      is(textNode.textContent, "foo bar", "Backspace should work correctly");

      // TODO: compare screenshots to make sure that no bogus node exists in the document

      callback();
    }, false);
    iframe.src = src;
  }

  const TEST_URIS = [
    "data:text/html,<body contenteditable>fooz bar</body>",
    "data:text/html,<body contenteditable><p>fooz bar</p></body>",
    "data:text/html,<body contenteditable><div>fooz bar</div></body>",
    "data:text/html,<body contenteditable><span>fooz bar</span></body>",
    "data:text/html,<p contenteditable>fooz bar</p>",
    "data:text/html,<!DOCTYPE html><html><body contenteditable>fooz bar</body></html>",
    'data:application/xhtml+xml,<html xmlns="http://www.w3.org/1999/xhtml"><body contenteditable="true">fooz bar</body></html>',
    "data:text/html,<body onload=\"document.designMode='on'\">fooz bar</body>",
    'data:text/html,<html><script>' +
                     'onload = function() {' +
                       'var old = document.body;' +
                       'old.parentNode.removeChild(old);' +
                       'var r = document.documentElement;' +
                       'var b = document.createElement("body");' +
                       'r.appendChild(b);' +
                       'b.appendChild(document.createTextNode("fooz bar"));' +
                       'b.contentEditable = "true";' +
                     '};' +
                   '<\/script><body></body></html>',
    'data:text/html,<html><script>' +
                     'onload = function() {' +
                       'var old = document.body;' +
                       'old.parentNode.removeChild(old);' +
                       'var r = document.documentElement;' +
                       'var b = document.createElement("body");' +
                       'b.appendChild(document.createTextNode("fooz bar"));' +
                       'b.contentEditable = "true";' +
                       'r.appendChild(b);' +
                     '};' +
                   '<\/script><body></body></html>',
    'data:text/html,<html><script>' +
                     'onload = function() {' +
                       'var old = document.body;' +
                       'old.parentNode.removeChild(old);' +
                       'var r = document.documentElement;' +
                       'var b = document.createElement("body");' +
                       'r.appendChild(b);' +
                       'b.appendChild(document.createTextNode("fooz bar"));' +
                       'b.setAttribute("contenteditable", "true");' +
                     '};' +
                   '<\/script><body></body></html>',
    'data:text/html,<html><script>' +
                     'onload = function() {' +
                       'var old = document.body;' +
                       'old.parentNode.removeChild(old);' +
                       'var r = document.documentElement;' +
                       'var b = document.createElement("body");' +
                       'b.appendChild(document.createTextNode("fooz bar"));' +
                       'b.setAttribute("contenteditable", "true");' +
                       'r.appendChild(b);' +
                     '};' +
                   '<\/script><body></body></html>',
    'data:text/html,<html><script>' +
                     'onload = function() {' +
                       'var old = document.body;' +
                       'old.parentNode.removeChild(old);' +
                       'var r = document.documentElement;' +
                       'var b = document.createElement("body");' +
                       'r.appendChild(b);' +
                       'b.contentEditable = "true";' +
                       'b.appendChild(document.createTextNode("fooz bar"));' +
                     '};' +
                   '<\/script><body></body></html>',
    'data:text/html,<html><script>' +
                     'onload = function() {' +
                       'var old = document.body;' +
                       'old.parentNode.removeChild(old);' +
                       'var r = document.documentElement;' +
                       'var b = document.createElement("body");' +
                       'b.contentEditable = "true";' +
                       'r.appendChild(b);' +
                       'b.appendChild(document.createTextNode("fooz bar"));' +
                     '};' +
                   '<\/script><body></body></html>',
    'data:text/html,<html><script>' +
                     'onload = function() {' +
                       'var old = document.body;' +
                       'old.parentNode.removeChild(old);' +
                       'var r = document.documentElement;' +
                       'var b = document.createElement("body");' +
                       'r.appendChild(b);' +
                       'b.setAttribute("contenteditable", "true");' +
                       'b.appendChild(document.createTextNode("fooz bar"));' +
                     '};' +
                   '<\/script><body></body></html>',
    'data:text/html,<html><script>' +
                     'onload = function() {' +
                       'var old = document.body;' +
                       'old.parentNode.removeChild(old);' +
                       'var r = document.documentElement;' +
                       'var b = document.createElement("body");' +
                       'b.setAttribute("contenteditable", "true");' +
                       'r.appendChild(b);' +
                       'b.appendChild(document.createTextNode("fooz bar"));' +
                     '};' +
                   '<\/script><body></body></html>',
  ];
  var currentTest = 0;
  function runAllTests() {
    if (currentTest == TEST_URIS.length) {
      SimpleTest.finish();
      return;
    }
    testBackspace(TEST_URIS[currentTest++], runAllTests);
  }
  runAllTests();
});

</script>
</pre>
</body>
</html>