editor/libeditor/html/tests/test_bug525389.html

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/editor/libeditor/html/tests/test_bug525389.html	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,192 @@
     1.4 +<!DOCTYPE HTML>
     1.5 +<html><head>
     1.6 +<title>Test for bug 525389</title>
     1.7 +<style src="/tests/SimpleTest/test.css" type="text/css"></style>
     1.8 +<script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
     1.9 +<script type="text/javascript" src="/tests/SimpleTest/EventUtils.js"></script>
    1.10 +
    1.11 +<script class="testbody" type="application/javascript">
    1.12 +
    1.13 +  var utils = SpecialPowers.wrap(window)
    1.14 +                .QueryInterface(SpecialPowers.Ci.nsIInterfaceRequestor).getInterface(SpecialPowers.Ci.nsIDOMWindowUtils);
    1.15 +  var Cc = SpecialPowers.Cc;
    1.16 +  var Ci = SpecialPowers.Ci;
    1.17 +
    1.18 +function getLoadContext() {
    1.19 +  return SpecialPowers.wrap(window)
    1.20 +               .QueryInterface(Ci.nsIInterfaceRequestor)
    1.21 +               .getInterface(Ci.nsIWebNavigation)
    1.22 +               .QueryInterface(Ci.nsILoadContext);
    1.23 +}
    1.24 +
    1.25 +function runTest() {
    1.26 +  var pasteCount = 0;
    1.27 +  var pasteFunc = function (event) { pasteCount++; };
    1.28 +
    1.29 +  function verifyContent(s) {
    1.30 +    var e = document.getElementById('i1');
    1.31 +    var doc = e.contentDocument;
    1.32 +    is(doc.body.innerHTML, s, "");
    1.33 +  }
    1.34 +
    1.35 +  function pasteInto(trans, html, target_id) {
    1.36 +    var e = document.getElementById('i1');
    1.37 +    var doc = e.contentDocument;
    1.38 +    doc.designMode = "on";
    1.39 +    doc.body.innerHTML = html;
    1.40 +    doc.defaultView.focus();
    1.41 +    if (target_id)
    1.42 +      e = doc.getElementById(target_id);
    1.43 +    else
    1.44 +      e = doc.body;
    1.45 +    var selection = doc.defaultView.getSelection();
    1.46 +    selection.removeAllRanges();
    1.47 +    selection.selectAllChildren(e);
    1.48 +    selection.collapseToEnd();
    1.49 +
    1.50 +    pasteCount = 0;
    1.51 +    e.addEventListener("paste", pasteFunc, false);
    1.52 +    utils.sendContentCommandEvent("pasteTransferable", trans);
    1.53 +    e.removeEventListener("paste", pasteFunc, false);
    1.54 +
    1.55 +    return e;
    1.56 +  }
    1.57 +
    1.58 +  function getTransferableFromClipboard(asHTML) {
    1.59 +    var trans = Cc["@mozilla.org/widget/transferable;1"].createInstance(Ci.nsITransferable);
    1.60 +    trans.init(getLoadContext());
    1.61 +    if (asHTML) {
    1.62 +      trans.addDataFlavor("text/html");
    1.63 +    } else {
    1.64 +      trans.addDataFlavor("text/unicode");
    1.65 +    }    
    1.66 +    var clip = Cc["@mozilla.org/widget/clipboard;1"].getService(Ci.nsIClipboard);
    1.67 +    clip.getData(trans, Ci.nsIClipboard.kGlobalClipboard);
    1.68 +    return trans;
    1.69 +  }
    1.70 +
    1.71 +  function makeTransferable(s,asHTML,target_id) {
    1.72 +    var e = document.getElementById('i2');
    1.73 +    var doc = e.contentDocument;
    1.74 +    if (asHTML) {
    1.75 +      doc.body.innerHTML = s;
    1.76 +    } else {
    1.77 +      var text = doc.createTextNode(s);
    1.78 +      doc.body.appendChild(text);
    1.79 +    }
    1.80 +    doc.designMode = "on";
    1.81 +    doc.defaultView.focus();
    1.82 +    var selection = doc.defaultView.getSelection();
    1.83 +    selection.removeAllRanges();
    1.84 +    if (!target_id) {
    1.85 +      selection.selectAllChildren(doc.body);
    1.86 +    } else {
    1.87 +      var range = document.createRange();
    1.88 +      range.selectNode(doc.getElementById(target_id));
    1.89 +      selection.addRange(range);
    1.90 +    }
    1.91 +
    1.92 +    // We cannot use plain strings, we have to use nsSupportsString.
    1.93 +    var supportsStringClass = SpecialPowers.Components.classes["@mozilla.org/supports-string;1"];
    1.94 +    var ssData = supportsStringClass.createInstance(Ci.nsISupportsString);
    1.95 +
    1.96 +    // Create the transferable.
    1.97 +    var trans = Cc["@mozilla.org/widget/transferable;1"].createInstance(Ci.nsITransferable);  
    1.98 +    trans.init(getLoadContext());
    1.99 +
   1.100 +    // Add the data to the transferable.
   1.101 +    if (asHTML) {
   1.102 +      trans.addDataFlavor("text/html");
   1.103 +      ssData.data = doc.body.innerHTML;
   1.104 +      trans.setTransferData("text/html", ssData, ssData.length * 2);
   1.105 +    } else {
   1.106 +      trans.addDataFlavor("text/unicode");
   1.107 +      ssData.data = doc.body.innerHTML;
   1.108 +      trans.setTransferData("text/unicode", ssData, ssData.length * 2);
   1.109 +    }
   1.110 +
   1.111 +    return trans;
   1.112 +  }
   1.113 +
   1.114 +  function copyToClipBoard(s,asHTML,target_id) {
   1.115 +    var e = document.getElementById('i2');
   1.116 +    var doc = e.contentDocument;
   1.117 +    if (asHTML) {
   1.118 +      doc.body.innerHTML = s;
   1.119 +    } else {
   1.120 +      var text = doc.createTextNode(s);
   1.121 +      doc.body.appendChild(text);
   1.122 +    }
   1.123 +    doc.designMode = "on";
   1.124 +    doc.defaultView.focus();
   1.125 +    var selection = doc.defaultView.getSelection();
   1.126 +    selection.removeAllRanges();
   1.127 +    if (!target_id) {
   1.128 +      selection.selectAllChildren(doc.body);
   1.129 +    } else {
   1.130 +      var range = document.createRange();
   1.131 +      range.selectNode(doc.getElementById(target_id));
   1.132 +      selection.addRange(range);
   1.133 +    }
   1.134 +    SpecialPowers.wrap(doc).execCommand("copy", false, null);
   1.135 +    return e;
   1.136 +  }
   1.137 +
   1.138 +  copyToClipBoard('<span>Hello</span><span>Kitty</span>', true);
   1.139 +  var trans = getTransferableFromClipboard(true);
   1.140 +  pasteInto(trans, '');
   1.141 +  verifyContent('<span>Hello</span><span>Kitty</span>');
   1.142 +  is(pasteCount, 1, "paste event was not triggered");
   1.143 +
   1.144 +  // this test is not working out exactly like the clipboard test
   1.145 +  // has to do with generating the nsITransferable above
   1.146 +  //trans = makeTransferable('<span>Hello</span><span>Kitty</span>', true);
   1.147 +  //pasteInto(trans, '');
   1.148 +  //verifyContent('<span>Hello</span><span>Kitty</span>');
   1.149 +
   1.150 +  copyToClipBoard("<dl><dd>Hello Kitty</dd></dl><span>Hello</span><span>Kitty</span>", true);
   1.151 +  trans = getTransferableFromClipboard(true);
   1.152 +  pasteInto(trans, '<ol><li id="paste_here">X</li></ol>',"paste_here");
   1.153 +  verifyContent('<ol><li id="paste_here">X<dl><dd>Hello Kitty</dd></dl><span>Hello</span><span>Kitty</span></li></ol>');
   1.154 +  is(pasteCount, 1, "paste event was not triggered");
   1.155 +
   1.156 +// The following test doesn't do what I expected, because the special handling
   1.157 +// of IsList nodes in nsHTMLEditor::InsertHTMLWithContext simply removes
   1.158 +// non-list/item children.  See bug 481177.
   1.159 +//  copyToClipBoard("<ol><li>Hello Kitty</li><span>Hello</span></ol>", true);
   1.160 +//  pasteInto('<ol><li id="paste_here">X</li></ol>',"paste_here");
   1.161 +//  verifyContent('<ol><li id="paste_here">X</li><li>Hello Kitty</li><span>Hello</span></ol>');
   1.162 +
   1.163 +  copyToClipBoard("<pre>Kitty</pre><span>Hello</span>", true);
   1.164 +  trans = getTransferableFromClipboard(true);
   1.165 +  pasteInto(trans, '<pre id="paste_here">Hello </pre>',"paste_here");
   1.166 +  verifyContent('<pre id="paste_here">Hello Kitty<span>Hello</span></pre>');
   1.167 +  is(pasteCount, 1, "paste event was not triggered");
   1.168 +
   1.169 +  // test that we can preventDefault pastes
   1.170 +  pasteFunc = function (event) { event.preventDefault(); return false; };
   1.171 +  copyToClipBoard("<pre>Kitty</pre><span>Hello</span>", true);
   1.172 +  trans = getTransferableFromClipboard(true);
   1.173 +  pasteInto(trans, '<pre id="paste_here">Hello </pre>',"paste_here");
   1.174 +  verifyContent('<pre id="paste_here">Hello </pre>');
   1.175 +  is(pasteCount, 0, "paste event was triggered");
   1.176 +
   1.177 +  SimpleTest.finish();
   1.178 +}
   1.179 +
   1.180 +SimpleTest.waitForExplicitFinish();
   1.181 +addLoadEvent(runTest);
   1.182 +</script>
   1.183 +</head>
   1.184 +<body>
   1.185 +<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=525389">Mozilla Bug 525389</a>
   1.186 +<p id="display"></p>
   1.187 +
   1.188 +<pre id="test">
   1.189 +</pre>
   1.190 +
   1.191 +<iframe id="i1" width="200" height="100" src="about:blank"></iframe><br>
   1.192 +<iframe id="i2" width="200" height="100" src="about:blank"></iframe><br>
   1.193 +
   1.194 +</body>
   1.195 +</html>

mercurial