Wed, 31 Dec 2014 06:55:50 +0100
Added tag UPSTREAM_283F7C6 for changeset ca08bd8f51b2
michael@0 | 1 | <!DOCTYPE HTML> |
michael@0 | 2 | <html><head> |
michael@0 | 3 | <title>Test for bug 525389</title> |
michael@0 | 4 | <style src="/tests/SimpleTest/test.css" type="text/css"></style> |
michael@0 | 5 | <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script> |
michael@0 | 6 | <script type="text/javascript" src="/tests/SimpleTest/EventUtils.js"></script> |
michael@0 | 7 | |
michael@0 | 8 | <script class="testbody" type="application/javascript"> |
michael@0 | 9 | |
michael@0 | 10 | var utils = SpecialPowers.wrap(window) |
michael@0 | 11 | .QueryInterface(SpecialPowers.Ci.nsIInterfaceRequestor).getInterface(SpecialPowers.Ci.nsIDOMWindowUtils); |
michael@0 | 12 | var Cc = SpecialPowers.Cc; |
michael@0 | 13 | var Ci = SpecialPowers.Ci; |
michael@0 | 14 | |
michael@0 | 15 | function getLoadContext() { |
michael@0 | 16 | return SpecialPowers.wrap(window) |
michael@0 | 17 | .QueryInterface(Ci.nsIInterfaceRequestor) |
michael@0 | 18 | .getInterface(Ci.nsIWebNavigation) |
michael@0 | 19 | .QueryInterface(Ci.nsILoadContext); |
michael@0 | 20 | } |
michael@0 | 21 | |
michael@0 | 22 | function runTest() { |
michael@0 | 23 | var pasteCount = 0; |
michael@0 | 24 | var pasteFunc = function (event) { pasteCount++; }; |
michael@0 | 25 | |
michael@0 | 26 | function verifyContent(s) { |
michael@0 | 27 | var e = document.getElementById('i1'); |
michael@0 | 28 | var doc = e.contentDocument; |
michael@0 | 29 | is(doc.body.innerHTML, s, ""); |
michael@0 | 30 | } |
michael@0 | 31 | |
michael@0 | 32 | function pasteInto(trans, html, target_id) { |
michael@0 | 33 | var e = document.getElementById('i1'); |
michael@0 | 34 | var doc = e.contentDocument; |
michael@0 | 35 | doc.designMode = "on"; |
michael@0 | 36 | doc.body.innerHTML = html; |
michael@0 | 37 | doc.defaultView.focus(); |
michael@0 | 38 | if (target_id) |
michael@0 | 39 | e = doc.getElementById(target_id); |
michael@0 | 40 | else |
michael@0 | 41 | e = doc.body; |
michael@0 | 42 | var selection = doc.defaultView.getSelection(); |
michael@0 | 43 | selection.removeAllRanges(); |
michael@0 | 44 | selection.selectAllChildren(e); |
michael@0 | 45 | selection.collapseToEnd(); |
michael@0 | 46 | |
michael@0 | 47 | pasteCount = 0; |
michael@0 | 48 | e.addEventListener("paste", pasteFunc, false); |
michael@0 | 49 | utils.sendContentCommandEvent("pasteTransferable", trans); |
michael@0 | 50 | e.removeEventListener("paste", pasteFunc, false); |
michael@0 | 51 | |
michael@0 | 52 | return e; |
michael@0 | 53 | } |
michael@0 | 54 | |
michael@0 | 55 | function getTransferableFromClipboard(asHTML) { |
michael@0 | 56 | var trans = Cc["@mozilla.org/widget/transferable;1"].createInstance(Ci.nsITransferable); |
michael@0 | 57 | trans.init(getLoadContext()); |
michael@0 | 58 | if (asHTML) { |
michael@0 | 59 | trans.addDataFlavor("text/html"); |
michael@0 | 60 | } else { |
michael@0 | 61 | trans.addDataFlavor("text/unicode"); |
michael@0 | 62 | } |
michael@0 | 63 | var clip = Cc["@mozilla.org/widget/clipboard;1"].getService(Ci.nsIClipboard); |
michael@0 | 64 | clip.getData(trans, Ci.nsIClipboard.kGlobalClipboard); |
michael@0 | 65 | return trans; |
michael@0 | 66 | } |
michael@0 | 67 | |
michael@0 | 68 | function makeTransferable(s,asHTML,target_id) { |
michael@0 | 69 | var e = document.getElementById('i2'); |
michael@0 | 70 | var doc = e.contentDocument; |
michael@0 | 71 | if (asHTML) { |
michael@0 | 72 | doc.body.innerHTML = s; |
michael@0 | 73 | } else { |
michael@0 | 74 | var text = doc.createTextNode(s); |
michael@0 | 75 | doc.body.appendChild(text); |
michael@0 | 76 | } |
michael@0 | 77 | doc.designMode = "on"; |
michael@0 | 78 | doc.defaultView.focus(); |
michael@0 | 79 | var selection = doc.defaultView.getSelection(); |
michael@0 | 80 | selection.removeAllRanges(); |
michael@0 | 81 | if (!target_id) { |
michael@0 | 82 | selection.selectAllChildren(doc.body); |
michael@0 | 83 | } else { |
michael@0 | 84 | var range = document.createRange(); |
michael@0 | 85 | range.selectNode(doc.getElementById(target_id)); |
michael@0 | 86 | selection.addRange(range); |
michael@0 | 87 | } |
michael@0 | 88 | |
michael@0 | 89 | // We cannot use plain strings, we have to use nsSupportsString. |
michael@0 | 90 | var supportsStringClass = SpecialPowers.Components.classes["@mozilla.org/supports-string;1"]; |
michael@0 | 91 | var ssData = supportsStringClass.createInstance(Ci.nsISupportsString); |
michael@0 | 92 | |
michael@0 | 93 | // Create the transferable. |
michael@0 | 94 | var trans = Cc["@mozilla.org/widget/transferable;1"].createInstance(Ci.nsITransferable); |
michael@0 | 95 | trans.init(getLoadContext()); |
michael@0 | 96 | |
michael@0 | 97 | // Add the data to the transferable. |
michael@0 | 98 | if (asHTML) { |
michael@0 | 99 | trans.addDataFlavor("text/html"); |
michael@0 | 100 | ssData.data = doc.body.innerHTML; |
michael@0 | 101 | trans.setTransferData("text/html", ssData, ssData.length * 2); |
michael@0 | 102 | } else { |
michael@0 | 103 | trans.addDataFlavor("text/unicode"); |
michael@0 | 104 | ssData.data = doc.body.innerHTML; |
michael@0 | 105 | trans.setTransferData("text/unicode", ssData, ssData.length * 2); |
michael@0 | 106 | } |
michael@0 | 107 | |
michael@0 | 108 | return trans; |
michael@0 | 109 | } |
michael@0 | 110 | |
michael@0 | 111 | function copyToClipBoard(s,asHTML,target_id) { |
michael@0 | 112 | var e = document.getElementById('i2'); |
michael@0 | 113 | var doc = e.contentDocument; |
michael@0 | 114 | if (asHTML) { |
michael@0 | 115 | doc.body.innerHTML = s; |
michael@0 | 116 | } else { |
michael@0 | 117 | var text = doc.createTextNode(s); |
michael@0 | 118 | doc.body.appendChild(text); |
michael@0 | 119 | } |
michael@0 | 120 | doc.designMode = "on"; |
michael@0 | 121 | doc.defaultView.focus(); |
michael@0 | 122 | var selection = doc.defaultView.getSelection(); |
michael@0 | 123 | selection.removeAllRanges(); |
michael@0 | 124 | if (!target_id) { |
michael@0 | 125 | selection.selectAllChildren(doc.body); |
michael@0 | 126 | } else { |
michael@0 | 127 | var range = document.createRange(); |
michael@0 | 128 | range.selectNode(doc.getElementById(target_id)); |
michael@0 | 129 | selection.addRange(range); |
michael@0 | 130 | } |
michael@0 | 131 | SpecialPowers.wrap(doc).execCommand("copy", false, null); |
michael@0 | 132 | return e; |
michael@0 | 133 | } |
michael@0 | 134 | |
michael@0 | 135 | copyToClipBoard('<span>Hello</span><span>Kitty</span>', true); |
michael@0 | 136 | var trans = getTransferableFromClipboard(true); |
michael@0 | 137 | pasteInto(trans, ''); |
michael@0 | 138 | verifyContent('<span>Hello</span><span>Kitty</span>'); |
michael@0 | 139 | is(pasteCount, 1, "paste event was not triggered"); |
michael@0 | 140 | |
michael@0 | 141 | // this test is not working out exactly like the clipboard test |
michael@0 | 142 | // has to do with generating the nsITransferable above |
michael@0 | 143 | //trans = makeTransferable('<span>Hello</span><span>Kitty</span>', true); |
michael@0 | 144 | //pasteInto(trans, ''); |
michael@0 | 145 | //verifyContent('<span>Hello</span><span>Kitty</span>'); |
michael@0 | 146 | |
michael@0 | 147 | copyToClipBoard("<dl><dd>Hello Kitty</dd></dl><span>Hello</span><span>Kitty</span>", true); |
michael@0 | 148 | trans = getTransferableFromClipboard(true); |
michael@0 | 149 | pasteInto(trans, '<ol><li id="paste_here">X</li></ol>',"paste_here"); |
michael@0 | 150 | verifyContent('<ol><li id="paste_here">X<dl><dd>Hello Kitty</dd></dl><span>Hello</span><span>Kitty</span></li></ol>'); |
michael@0 | 151 | is(pasteCount, 1, "paste event was not triggered"); |
michael@0 | 152 | |
michael@0 | 153 | // The following test doesn't do what I expected, because the special handling |
michael@0 | 154 | // of IsList nodes in nsHTMLEditor::InsertHTMLWithContext simply removes |
michael@0 | 155 | // non-list/item children. See bug 481177. |
michael@0 | 156 | // copyToClipBoard("<ol><li>Hello Kitty</li><span>Hello</span></ol>", true); |
michael@0 | 157 | // pasteInto('<ol><li id="paste_here">X</li></ol>',"paste_here"); |
michael@0 | 158 | // verifyContent('<ol><li id="paste_here">X</li><li>Hello Kitty</li><span>Hello</span></ol>'); |
michael@0 | 159 | |
michael@0 | 160 | copyToClipBoard("<pre>Kitty</pre><span>Hello</span>", true); |
michael@0 | 161 | trans = getTransferableFromClipboard(true); |
michael@0 | 162 | pasteInto(trans, '<pre id="paste_here">Hello </pre>',"paste_here"); |
michael@0 | 163 | verifyContent('<pre id="paste_here">Hello Kitty<span>Hello</span></pre>'); |
michael@0 | 164 | is(pasteCount, 1, "paste event was not triggered"); |
michael@0 | 165 | |
michael@0 | 166 | // test that we can preventDefault pastes |
michael@0 | 167 | pasteFunc = function (event) { event.preventDefault(); return false; }; |
michael@0 | 168 | copyToClipBoard("<pre>Kitty</pre><span>Hello</span>", true); |
michael@0 | 169 | trans = getTransferableFromClipboard(true); |
michael@0 | 170 | pasteInto(trans, '<pre id="paste_here">Hello </pre>',"paste_here"); |
michael@0 | 171 | verifyContent('<pre id="paste_here">Hello </pre>'); |
michael@0 | 172 | is(pasteCount, 0, "paste event was triggered"); |
michael@0 | 173 | |
michael@0 | 174 | SimpleTest.finish(); |
michael@0 | 175 | } |
michael@0 | 176 | |
michael@0 | 177 | SimpleTest.waitForExplicitFinish(); |
michael@0 | 178 | addLoadEvent(runTest); |
michael@0 | 179 | </script> |
michael@0 | 180 | </head> |
michael@0 | 181 | <body> |
michael@0 | 182 | <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=525389">Mozilla Bug 525389</a> |
michael@0 | 183 | <p id="display"></p> |
michael@0 | 184 | |
michael@0 | 185 | <pre id="test"> |
michael@0 | 186 | </pre> |
michael@0 | 187 | |
michael@0 | 188 | <iframe id="i1" width="200" height="100" src="about:blank"></iframe><br> |
michael@0 | 189 | <iframe id="i2" width="200" height="100" src="about:blank"></iframe><br> |
michael@0 | 190 | |
michael@0 | 191 | </body> |
michael@0 | 192 | </html> |