5 function assertEq(left, right)
8 log('PASS: ' + left + " === " + right);
10 log('FAIL: ' + left + "(of type " + (typeof left) + ") !== " + right + "(of type " + (typeof right) + ")");
14 var result = document.getElementById('result');
15 result.appendChild(document.createTextNode(str));
16 result.appendChild(document.createElement('br'));
19 function legacyCopyStart(dataTransfer)
21 dataTransfer.setData('text', 'sample');
22 dataTransfer.setData('url', 'http://www.google.com/');
23 dataTransfer.setData('text/html', '<em>Markup</em>');
24 dataTransfer.setData('custom-data', 'hello world');
27 function itemListCopyStart(dataTransfer)
29 dataTransfer.items.add('sample', 'text/plain');
30 dataTransfer.items.add('http://www.google.com/', 'text/uri-list');
31 dataTransfer.items.add('<em>Markup</em>', 'text/html');
32 dataTransfer.items.add('hello world', 'custom-data');
37 event.preventDefault();
38 var copyMethod = document.getElementById('copyMethod');
39 if (copyMethod.selectedIndex == 0)
40 legacyCopyStart(event.clipboardData);
41 else if (copyMethod.selectedIndex == 1)
42 itemListCopyStart(event.clipboardData);
45 function legacyPaste(dataTransfer)
47 assertEq(4, dataTransfer.types.length);
48 if (dataTransfer.types.indexOf('text/plain') < 0)
49 log('FAIL: types array did not contain "text"');
50 if (dataTransfer.types.indexOf('text/uri-list') < 0)
51 log('FAIL: types array did not contain "text/uri-list"');
52 if (dataTransfer.types.indexOf('text/html') < 0)
53 log('FAIL: types array did not contain "text/html"');
54 if (dataTransfer.types.indexOf('custom-data') < 0)
55 log('FAIL: types array did not contain "custom-data"');
56 assertEq('sample', dataTransfer.getData('text'));
57 assertEq('http://www.google.com/', dataTransfer.getData('url'));
58 assertEq('<em>Markup</em>', dataTransfer.getData('text/html'));
59 assertEq('hello world', dataTransfer.getData('custom-data'));
63 var outstandingRequests;
64 function itemListPaste(dataTransfer)
66 outstandingRequests = 0;
67 assertEq(4, dataTransfer.items.length);
69 for (var i = 0; i < dataTransfer.items.length; ++i) {
70 types.push({kind: dataTransfer.items[i].kind, type: dataTransfer.items[i].type});
72 types.sort(function (a, b) { return a.type.localeCompare(b.type); });
74 { kind: 'string', type: 'custom-data'},
75 { kind: 'string', type: 'text/html'},
76 { kind: 'string', type: 'text/plain'},
77 { kind: 'string', type: 'text/uri-list'},
79 assertEq(JSON.stringify(expectedTypes), JSON.stringify(types));
80 var expectedResults = {
81 'custom-data': 'hello world',
82 'text/html': '<em>Markup</em>',
83 'text/plain': 'sample',
84 'text/uri-list': 'http://www.google.com/',
86 function makeClosure(expectedData)
88 ++outstandingRequests;
89 return function (data) {
90 assertEq(expectedData, data);
91 if (--outstandingRequests == 0)
92 window.setTimeout(runNext, 0);
95 // We use this funky loop to make sure we always print out results in the same order.
96 for (var i = 0; i < types.length; ++i) {
97 for (var j = 0; j < dataTransfer.items.length; ++j) {
98 if (types[i].type == dataTransfer.items[j].type) {
99 dataTransfer.items[j].getAsString(makeClosure(expectedResults[types[i].type]));
106 function paste(event)
108 var pasteMethod= document.getElementById('pasteMethod');
109 if (pasteMethod.selectedIndex == 0)
110 legacyPaste(event.clipboardData);
111 else if (pasteMethod.selectedIndex == 1)
112 itemListPaste(event.clipboardData);
115 function runTest(copyMethodIndex, pasteMethodIndex)
117 var copyMethod = document.getElementById('copyMethod');
118 var pasteMethod = document.getElementById('pasteMethod');
119 copyMethod.selectedIndex = copyMethodIndex;
120 pasteMethod.selectedIndex = pasteMethodIndex;
121 log('Running test with ' + copyMethod.value + ' copy handler and ' + pasteMethod.value + ' paste handler');
123 document.execCommand('copy');
124 document.execCommand('paste');
135 if (!window.layoutTestController)
137 var testCase = testCases.shift();
139 runTest.apply(null, testCase);
141 layoutTestController.notifyDone();
144 window.onload = function()
146 if (!window.layoutTestController)
148 layoutTestController.dumpAsText();
149 layoutTestController.waitUntilDone();
155 <body oncopy="copy(event)" onpaste="paste(event)">
156 <p>To manually test, press your browser shortcut for copy and then for paste. Several lines that say 'PASS' should appear below.
157 <div>Copy handler: <select id="copyMethod"><option>Legacy</option><option>DataTransferItemList</option></select></div>
158 <div>Paste handler: <select id="pasteMethod"><option>Legacy</option><option>DataTransferItemList</option></select></div>
159 <div id="result"></div>