|
1 /* Any copyright is dedicated to the Public Domain. |
|
2 * http://creativecommons.org/publicdomain/zero/1.0/ */ |
|
3 |
|
4 function modifySelection(s) { |
|
5 var g = window.getSelection(); |
|
6 var l = g.getRangeAt(0); |
|
7 var d = document.createElement("p"); |
|
8 d.innerHTML = s; |
|
9 d.appendChild(l.cloneContents()); |
|
10 |
|
11 var e = document.createElement("div"); |
|
12 document.body.appendChild(e); |
|
13 e.appendChild(d); |
|
14 var a = document.createRange(); |
|
15 a.selectNode(d); |
|
16 g.removeAllRanges(); |
|
17 g.addRange(a); |
|
18 window.setTimeout(function () { |
|
19 e.parentNode.removeChild(e); |
|
20 g.removeAllRanges(); |
|
21 g.addRange(l); |
|
22 }, 0) |
|
23 } |
|
24 |
|
25 function getLoadContext() { |
|
26 var Ci = SpecialPowers.Ci; |
|
27 return SpecialPowers.wrap(window).QueryInterface(Ci.nsIInterfaceRequestor) |
|
28 .getInterface(Ci.nsIWebNavigation) |
|
29 .QueryInterface(Ci.nsILoadContext); |
|
30 } |
|
31 |
|
32 function testCopyPaste (isXHTML) { |
|
33 var suppressUnicodeCheckIfHidden = !!isXHTML; |
|
34 var suppressHTMLCheck = !!isXHTML; |
|
35 |
|
36 var webnav = SpecialPowers.wrap(window).QueryInterface(SpecialPowers.Ci.nsIInterfaceRequestor) |
|
37 .getInterface(SpecialPowers.Ci.nsIWebNavigation) |
|
38 |
|
39 var docShell = webnav.QueryInterface(SpecialPowers.Ci.nsIDocShell); |
|
40 |
|
41 var documentViewer = docShell.contentViewer |
|
42 .QueryInterface(SpecialPowers.Ci.nsIContentViewerEdit); |
|
43 |
|
44 var clipboard = SpecialPowers.Services.clipboard; |
|
45 |
|
46 var textarea = SpecialPowers.wrap(document.getElementById('input')); |
|
47 |
|
48 function copySelectionToClipboard(suppressUnicodeCheck) { |
|
49 documentViewer.copySelection(); |
|
50 if (!suppressUnicodeCheck) |
|
51 ok(clipboard.hasDataMatchingFlavors(["text/unicode"], 1,1), "check text/unicode"); |
|
52 if (!suppressHTMLCheck) |
|
53 ok(clipboard.hasDataMatchingFlavors(["text/html"], 1,1), "check text/html"); |
|
54 } |
|
55 function copyToClipboard(node, suppressUnicodeCheck) { |
|
56 textarea.blur(); |
|
57 clipboard.emptyClipboard(1); |
|
58 var sel = window.getSelection(); |
|
59 sel.removeAllRanges(); |
|
60 var r = document.createRange(); |
|
61 r.selectNode(node); |
|
62 window.getSelection().addRange(r); |
|
63 copySelectionToClipboard(suppressUnicodeCheck); |
|
64 } |
|
65 function copyRangeToClipboard(startNode,startIndex,endNode,endIndex,suppressUnicodeCheck) { |
|
66 textarea.blur(); |
|
67 clipboard.emptyClipboard(1); |
|
68 var sel = window.getSelection(); |
|
69 sel.removeAllRanges(); |
|
70 var r = document.createRange(); |
|
71 r.setStart(startNode,startIndex) |
|
72 r.setEnd(endNode,endIndex) |
|
73 window.getSelection().addRange(r); |
|
74 copySelectionToClipboard(suppressUnicodeCheck); |
|
75 } |
|
76 function copyChildrenToClipboard(id) { |
|
77 textarea.blur(); |
|
78 clipboard.emptyClipboard(1); |
|
79 window.getSelection().selectAllChildren(document.getElementById(id)); |
|
80 copySelectionToClipboard(); |
|
81 } |
|
82 function getClipboardData(mime) { |
|
83 var transferable = SpecialPowers.Cc['@mozilla.org/widget/transferable;1'] |
|
84 .createInstance(SpecialPowers.Ci.nsITransferable); |
|
85 transferable.init(getLoadContext()); |
|
86 transferable.addDataFlavor(mime); |
|
87 clipboard.getData(transferable, 1); |
|
88 var data = SpecialPowers.createBlankObject(); |
|
89 transferable.getTransferData(mime, data, {}) ; |
|
90 return data; |
|
91 } |
|
92 function testClipboardValue(mime, expected) { |
|
93 if (suppressHTMLCheck && mime == "text/html") |
|
94 return null; |
|
95 var data = SpecialPowers.wrap(getClipboardData(mime)); |
|
96 is (data.value == null ? data.value : |
|
97 data.value.QueryInterface(SpecialPowers.Ci.nsISupportsString).data, |
|
98 expected, |
|
99 mime + " value in the clipboard"); |
|
100 return data.value; |
|
101 } |
|
102 function testPasteText(expected) { |
|
103 textarea.value=""; |
|
104 textarea.focus(); |
|
105 textarea.editor.paste(1); |
|
106 is(textarea.value, expected, "value of the textarea after the paste"); |
|
107 } |
|
108 function testSelectionToString(expected) { |
|
109 is(window.getSelection().toString().replace(/\r\n/g,"\n"), expected, "Selection.toString"); |
|
110 } |
|
111 function testInnerHTML(id, expected) { |
|
112 var value = document.getElementById(id).innerHTML; |
|
113 is(value, expected, id + ".innerHTML"); |
|
114 } |
|
115 function testEmptyChildren(id) { |
|
116 copyChildrenToClipboard(id); |
|
117 testSelectionToString(""); |
|
118 testClipboardValue("text/unicode", null); |
|
119 testClipboardValue("text/html", null); |
|
120 testPasteText(""); |
|
121 } |
|
122 |
|
123 copyChildrenToClipboard("draggable"); |
|
124 testSelectionToString("This is a draggable bit of text."); |
|
125 testClipboardValue("text/unicode", |
|
126 "This is a draggable bit of text."); |
|
127 testClipboardValue("text/html", |
|
128 "<div id=\"draggable\" title=\"title to have a long HTML line\">This is a <em>draggable</em> bit of text.</div>"); |
|
129 testPasteText("This is a draggable bit of text."); |
|
130 |
|
131 copyChildrenToClipboard("alist"); |
|
132 testSelectionToString(" bla\n\n foo\n bar\n\n"); |
|
133 testClipboardValue("text/unicode", " bla\n\n foo\n bar\n\n"); |
|
134 testClipboardValue("text/html", "<div id=\"alist\">\n bla\n <ul>\n <li>foo</li>\n \n <li>bar</li>\n </ul>\n </div>"); |
|
135 testPasteText(" bla\n\n foo\n bar\n\n"); |
|
136 |
|
137 copyChildrenToClipboard("blist"); |
|
138 testSelectionToString(" mozilla\n\n foo\n bar\n\n"); |
|
139 testClipboardValue("text/unicode", " mozilla\n\n foo\n bar\n\n"); |
|
140 testClipboardValue("text/html", "<div id=\"blist\">\n mozilla\n <ol>\n <li>foo</li>\n \n <li>bar</li>\n </ol>\n </div>"); |
|
141 testPasteText(" mozilla\n\n foo\n bar\n\n"); |
|
142 |
|
143 copyChildrenToClipboard("clist"); |
|
144 testSelectionToString(" mzla\n\n foo\n bazzinga!\n bar\n\n"); |
|
145 testClipboardValue("text/unicode", " mzla\n\n foo\n bazzinga!\n bar\n\n"); |
|
146 testClipboardValue("text/html", "<div id=\"clist\">\n mzla\n <ul>\n <li>foo<ul>\n <li>bazzinga!</li>\n </ul></li>\n \n <li>bar</li>\n </ul>\n </div>"); |
|
147 testPasteText(" mzla\n\n foo\n bazzinga!\n bar\n\n"); |
|
148 |
|
149 copyChildrenToClipboard("div4"); |
|
150 testSelectionToString(" Tt t t "); |
|
151 testClipboardValue("text/unicode", " Tt t t "); |
|
152 if (isXHTML) { |
|
153 testClipboardValue("text/html", "<div id=\"div4\">\n T<textarea xmlns=\"http://www.w3.org/1999/xhtml\">t t t</textarea>\n</div>"); |
|
154 testInnerHTML("div4", "\n T<textarea xmlns=\"http://www.w3.org/1999/xhtml\">t t t</textarea>\n"); |
|
155 } |
|
156 else { |
|
157 testClipboardValue("text/html", "<div id=\"div4\">\n T<textarea>t t t</textarea>\n</div>"); |
|
158 testInnerHTML("div4", "\n T<textarea>t t t</textarea>\n"); |
|
159 } |
|
160 testPasteText(" Tt t t "); |
|
161 |
|
162 copyChildrenToClipboard("div5"); |
|
163 testSelectionToString(" T "); |
|
164 testClipboardValue("text/unicode", " T "); |
|
165 if (isXHTML) { |
|
166 testClipboardValue("text/html", "<div id=\"div5\">\n T<textarea xmlns=\"http://www.w3.org/1999/xhtml\"> </textarea>\n</div>"); |
|
167 testInnerHTML("div5", "\n T<textarea xmlns=\"http://www.w3.org/1999/xhtml\"> </textarea>\n"); |
|
168 } |
|
169 else { |
|
170 testClipboardValue("text/html", "<div id=\"div5\">\n T<textarea> </textarea>\n</div>"); |
|
171 testInnerHTML("div5", "\n T<textarea> </textarea>\n"); |
|
172 } |
|
173 testPasteText(" T "); |
|
174 |
|
175 copyRangeToClipboard($("div6").childNodes[0],0, $("div6").childNodes[1],1,suppressUnicodeCheckIfHidden); |
|
176 testSelectionToString(""); |
|
177 // START Disabled due to bug 564688 |
|
178 if (false) { |
|
179 testClipboardValue("text/unicode", ""); |
|
180 testClipboardValue("text/html", ""); |
|
181 } |
|
182 // END Disabled due to bug 564688 |
|
183 testInnerHTML("div6", "div6"); |
|
184 |
|
185 copyRangeToClipboard($("div7").childNodes[0],0, $("div7").childNodes[0],4,suppressUnicodeCheckIfHidden); |
|
186 testSelectionToString(""); |
|
187 // START Disabled due to bug 564688 |
|
188 if (false) { |
|
189 testClipboardValue("text/unicode", ""); |
|
190 testClipboardValue("text/html", ""); |
|
191 } |
|
192 // END Disabled due to bug 564688 |
|
193 testInnerHTML("div7", "div7"); |
|
194 |
|
195 copyRangeToClipboard($("div8").childNodes[0],0, $("div8").childNodes[0],4,suppressUnicodeCheckIfHidden); |
|
196 testSelectionToString(""); |
|
197 // START Disabled due to bug 564688 |
|
198 if (false) { |
|
199 testClipboardValue("text/unicode", ""); |
|
200 testClipboardValue("text/html", ""); |
|
201 } |
|
202 // END Disabled due to bug 564688 |
|
203 testInnerHTML("div8", "div8"); |
|
204 |
|
205 copyRangeToClipboard($("div9").childNodes[0],0, $("div9").childNodes[0],4,suppressUnicodeCheckIfHidden); |
|
206 testSelectionToString("div9"); |
|
207 testClipboardValue("text/unicode", "div9"); |
|
208 testClipboardValue("text/html", "div9"); |
|
209 testInnerHTML("div9", "div9"); |
|
210 |
|
211 copyToClipboard($("div10"), suppressUnicodeCheckIfHidden); |
|
212 testSelectionToString(""); |
|
213 testInnerHTML("div10", "div10"); |
|
214 |
|
215 copyToClipboard($("div10").firstChild, suppressUnicodeCheckIfHidden); |
|
216 testSelectionToString(""); |
|
217 |
|
218 copyRangeToClipboard($("div10").childNodes[0],0, $("div10").childNodes[0],1,suppressUnicodeCheckIfHidden); |
|
219 testSelectionToString(""); |
|
220 |
|
221 copyRangeToClipboard($("div10").childNodes[1],0, $("div10").childNodes[1],1,suppressUnicodeCheckIfHidden); |
|
222 testSelectionToString(""); |
|
223 |
|
224 // ============ copy/paste test from/to a textarea |
|
225 |
|
226 var val = "1\n 2\n 3"; |
|
227 textarea.value=val; |
|
228 textarea.select(); |
|
229 textarea.editor.copy(); |
|
230 |
|
231 textarea.value=""; |
|
232 textarea.editor.paste(1); |
|
233 is(textarea.value, val); |
|
234 textarea.value=""; |
|
235 |
|
236 // ============ NOSCRIPT should not be copied |
|
237 |
|
238 copyChildrenToClipboard("div13"); |
|
239 testSelectionToString("__"); |
|
240 testClipboardValue("text/unicode", "__"); |
|
241 testClipboardValue("text/html", "<div id=\"div13\">__</div>"); |
|
242 testPasteText("__"); |
|
243 |
|
244 // ============ converting cell boundaries to tabs in tables |
|
245 |
|
246 copyToClipboard($("tr1")); |
|
247 testClipboardValue("text/unicode", "foo\tbar"); |
|
248 |
|
249 // ============ manipulating Selection in oncopy |
|
250 |
|
251 copyRangeToClipboard($("div11").childNodes[0],0, $("div11").childNodes[1],2); |
|
252 testClipboardValue("text/unicode", "Xdiv11"); |
|
253 testClipboardValue("text/html", "<div><p>X<span>div</span>11</p></div>"); |
|
254 setTimeout(function(){testSelectionToString("div11")},0); |
|
255 |
|
256 setTimeout(function(){ |
|
257 copyRangeToClipboard($("div12").childNodes[0],0, $("div12").childNodes[1],2); |
|
258 testClipboardValue("text/unicode", "Xdiv12"); |
|
259 testClipboardValue("text/html", "<div><p>X<span>div</span>12</p></div>"); |
|
260 setTimeout(function(){ |
|
261 testSelectionToString("div12"); |
|
262 setTimeout(SimpleTest.finish,0); |
|
263 },0); |
|
264 },0); |
|
265 } |