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