3 <title>Test for bug
525389</title>
4 <style src=
"/tests/SimpleTest/test.css" type=
"text/css"></style>
5 <script src=
"/tests/SimpleTest/SimpleTest.js"></script>
6 <script src=
"/tests/SimpleTest/EventUtils.js"></script>
8 <script class=
"testbody" type=
"application/javascript">
10 var utils = SpecialPowers.getDOMWindowUtils(window);
11 var Cc = SpecialPowers.Cc;
12 var Ci = SpecialPowers.Ci;
14 function getLoadContext() {
15 return SpecialPowers.wrap(window).docShell.QueryInterface(Ci.nsILoadContext);
18 async function runTest() {
20 var pasteFunc = function() { pasteCount++; };
22 function verifyContent(s) {
23 var e = document.getElementById(
"i1");
24 var doc = e.contentDocument;
25 if (navigator.platform.includes(
"Win")) {
26 // On Windows ignore \n which got left over from the removal of the fragment tags
27 //
<html><body>\n
<!--StartFragment--> and
<!--EndFragment-->\n
</body>\n
</html>.
28 is(doc.body.innerHTML.replace(/\n/g,
""), s,
"");
30 is(doc.body.innerHTML, s,
"");
34 function pasteInto(trans, html, target_id) {
35 var e = document.getElementById(
"i1");
36 var doc = e.contentDocument;
37 doc.designMode =
"on";
38 doc.body.innerHTML = html;
39 doc.defaultView.focus();
41 e = doc.getElementById(target_id);
44 var selection = doc.defaultView.getSelection();
45 selection.removeAllRanges();
46 selection.selectAllChildren(e);
47 selection.collapseToEnd();
50 e.addEventListener(
"paste", pasteFunc);
51 utils.sendContentCommandEvent(
"pasteTransferable", trans);
52 e.removeEventListener(
"paste", pasteFunc);
57 function getTransferableFromClipboard(asHTML) {
58 var trans = Cc[
"@mozilla.org/widget/transferable;1"].createInstance(Ci.nsITransferable);
59 trans.init(getLoadContext());
61 trans.addDataFlavor(
"text/html");
63 trans.addDataFlavor(
"text/plain");
65 var clip = SpecialPowers.Services.clipboard;
66 clip.getData(trans, Ci.nsIClipboard.kGlobalClipboard, SpecialPowers.wrap(window).browsingContext.currentWindowContext);
70 // Commented out as the test for it below is also commented out.
71 // function makeTransferable(s, asHTML, target_id) {
72 // var e = document.getElementById(
"i2");
73 // var doc = e.contentDocument;
75 // doc.body.innerHTML = s;
77 // var text = doc.createTextNode(s);
78 // doc.body.appendChild(text);
80 // doc.designMode =
"on";
81 // doc.defaultView.focus();
82 // var selection = doc.defaultView.getSelection();
83 // selection.removeAllRanges();
85 // selection.selectAllChildren(doc.body);
87 // var range = document.createRange();
88 // range.selectNode(doc.getElementById(target_id));
89 // selection.addRange(range);
92 // // We cannot use plain strings, we have to use nsSupportsString.
93 // var supportsStringClass = SpecialPowers.Components.classes[
"@mozilla.org/supports-string;1"];
94 // var ssData = supportsStringClass.createInstance(Ci.nsISupportsString);
96 // // Create the transferable.
97 // var trans = Cc[
"@mozilla.org/widget/transferable;1"].createInstance(Ci.nsITransferable);
98 // trans.init(getLoadContext());
100 // // Add the data to the transferable.
102 // trans.addDataFlavor(
"text/html");
103 // ssData.data = doc.body.innerHTML;
104 // trans.setTransferData(
"text/html", ssData);
106 // trans.addDataFlavor(
"text/plain");
107 // ssData.data = doc.body.innerHTML;
108 // trans.setTransferData(
"text/plain", ssData);
114 async function copyToClipBoard(s, asHTML, target_id) {
115 var e = document.getElementById(
"i2");
116 var doc = e.contentDocument;
118 doc.body.innerHTML = s;
120 var text = doc.createTextNode(s);
121 doc.body.appendChild(text);
123 doc.designMode =
"on";
124 doc.defaultView.focus();
125 var selection = doc.defaultView.getSelection();
126 selection.removeAllRanges();
128 selection.selectAllChildren(doc.body);
130 var range = document.createRange();
131 range.selectNode(doc.getElementById(target_id));
132 selection.addRange(range);
135 await SimpleTest.promiseClipboardChange(() =
> true,
136 () =
> { SpecialPowers.wrap(doc).execCommand(
"copy", false, null); });
141 await copyToClipBoard(
"<span>Hello</span><span>Kitty</span>", true);
142 var trans = getTransferableFromClipboard(true);
143 pasteInto(trans,
"");
144 verifyContent(
"<span>Hello</span><span>Kitty</span>");
145 is(pasteCount,
1,
"paste event was not triggered");
147 // this test is not working out exactly like the clipboard test
148 // has to do with generating the nsITransferable above
149 // trans = makeTransferable('
<span>Hello
</span><span>Kitty
</span>', true);
150 // pasteInto(trans, '');
151 // verifyContent('
<span>Hello
</span><span>Kitty
</span>');
153 await copyToClipBoard(
"<dl><dd>Hello Kitty</dd></dl><span>Hello</span><span>Kitty</span>", true);
154 trans = getTransferableFromClipboard(true);
155 pasteInto(trans, '
<ol><li id=
"paste_here">X
</li></ol>',
"paste_here");
156 verifyContent('
<ol><li id=
"paste_here">X
<dl><dd>Hello Kitty
</dd></dl><span>Hello
</span><span>Kitty
</span></li></ol>');
157 is(pasteCount,
1,
"paste event was not triggered");
159 // The following test doesn't do what I expected, because the special handling
160 // of IsList nodes in nsHTMLEditor::InsertHTMLWithContext simply removes
161 // non-list/item children. See bug
481177.
162 // await copyToClipBoard(
"<ol><li>Hello Kitty</li><span>Hello</span></ol>", true);
163 // pasteInto('
<ol><li id=
"paste_here">X
</li></ol>',
"paste_here");
164 // verifyContent('
<ol><li id=
"paste_here">X
</li><li>Hello Kitty
</li><span>Hello
</span></ol>');
166 await copyToClipBoard(
"<pre>Kitty</pre><span>Hello</span>", true);
167 trans = getTransferableFromClipboard(true);
168 pasteInto(trans, '
<pre id=
"paste_here">Hello
</pre>',
"paste_here");
169 verifyContent('
<pre id=
"paste_here">Hello Kitty
<span>Hello
</span></pre>');
170 is(pasteCount,
1,
"paste event was not triggered");
172 await copyToClipBoard('
1<span style=
"display: contents">2</span>3', true);
173 trans = getTransferableFromClipboard(true);
174 pasteInto(trans, '
<div id=
"paste_here"></div>',
"paste_here");
175 verifyContent('
<div id=
"paste_here">1<span style=
"display: contents">2</span>3</div>');
176 is(pasteCount,
1,
"paste event was not triggered");
178 // test that we can preventDefault pastes
179 pasteFunc = function(event) { event.preventDefault(); return false; };
180 await copyToClipBoard(
"<pre>Kitty</pre><span>Hello</span>", true);
181 trans = getTransferableFromClipboard(true);
182 pasteInto(trans, '
<pre id=
"paste_here">Hello
</pre>',
"paste_here");
183 verifyContent('
<pre id=
"paste_here">Hello
</pre>');
184 is(pasteCount,
0,
"paste event was triggered");
187 SimpleTest.waitForExplicitFinish();
189 add_task(async function test_copy() {
197 <a target=
"_blank" href=
"https://bugzilla.mozilla.org/show_bug.cgi?id=525389">Mozilla Bug
525389</a>
203 <iframe id=
"i1" width=
"200" height=
"100" src=
"about:blank"></iframe><br>
204 <iframe id=
"i2" width=
"200" height=
"100" src=
"about:blank"></iframe><br>