async-navigator-clipboard-write-domstring.https.html (4924B)
1 <!doctype html> 2 <meta charset="utf-8"> 3 <title>Async Clipboard input type validation tests - DOMString input in write API</title> 4 <link rel="help" href="https://w3c.github.io/clipboard-apis/#typedefdef-clipboarditemdata"> 5 6 <body>Body needed for test_driver.click()</body> 7 <script src="/resources/testharness.js"></script> 8 <script src="/resources/testharnessreport.js"></script> 9 <script src="/resources/testdriver.js"></script> 10 <script src="/resources/testdriver-vendor.js"></script> 11 <script src="resources/user-activation.js"></script> 12 <script> 13 14 // Permissions are required in order to invoke navigator.clipboard functions in 15 // an automated test. 16 async function getPermissions() { 17 await tryGrantReadPermission(); 18 await tryGrantWritePermission(); 19 await waitForUserActivation(); 20 } 21 22 test(() => { 23 assert_not_equals(navigator.clipboard, undefined); 24 assert_true(navigator.clipboard instanceof Clipboard); 25 assert_equals(navigator.clipboard, navigator.clipboard); 26 }, 'navigator.clipboard exists'); 27 28 promise_test(async t => { 29 await getPermissions(); 30 const text_plain = "This text was copied using `Clipboard.prototype.write`."; 31 const html_text = "<p style='color: red; font-style: oblique;'>Test</p>"; 32 await navigator.clipboard.write([ 33 new ClipboardItem({ 34 "text/plain": text_plain, 35 "text/html": html_text 36 }), 37 ]); 38 }, 'navigator.clipboard.write(DOMString) succeeds'); 39 40 promise_test(async () => { 41 await getPermissions(); 42 const promise_text_string = Promise.resolve('hello'); 43 const promise_html_string = Promise.resolve("<p style='color: red; font-style: oblique;'>hello</p>"); 44 const item = new ClipboardItem({ 45 'text/plain': promise_text_string, 46 'text/html': promise_html_string 47 }); 48 await navigator.clipboard.write([item]); 49 }, 'navigator.clipboard.write(Promise<DOMString>) succeeds'); 50 51 promise_test(async () => { 52 await getPermissions(); 53 const promise_html_string = ` 54 <table> 55 <tbody> 56 <tr> 57 <td>0,00€</td> 58 </tr> 59 <tr> 60 <td>0,00€</td> 61 </tr> 62 </tbody> 63 </table> 64 `; 65 const item = new ClipboardItem({ 66 'text/html': promise_html_string 67 }); 68 await navigator.clipboard.write([item]); 69 }, 'navigator.clipboard.write(Promise<DOMString>) with utf-16 string succeeds'); 70 71 promise_test(async t => { 72 await getPermissions(); 73 const text_plain = 'hello'; 74 const html_text = "<p style='color: red; font-style: oblique;'>hello</p>"; 75 const image = await fetch("/clipboard-apis/resources/greenbox.png"); 76 const item = new ClipboardItem({ 77 'text/plain': text_plain, 78 'text/html': new Blob([html_text], {type: 'text/html'}), 79 'image/png': image.blob(), // Promise<Blob> 80 'web text/csv': 'hello,world' 81 }); 82 await navigator.clipboard.write([item]); 83 }, 'navigator.clipboard.write(web_custom_format) succeeds'); 84 85 promise_test(async () => { 86 await getPermissions(); 87 const html_text = "<p style='color: red; font-style: oblique;'>Test</p>"; 88 const item = new ClipboardItem({ 89 'text/plain': 'hello', 90 'text/html': new Blob([html_text], {type: 'text/html'}) 91 }); 92 const text = await item.getType('text/plain'); 93 const blob = await item.getType('text/html'); 94 assert_true(text instanceof Blob, "item.getType('text/plain') should return a Blob"); 95 assert_true(blob instanceof Blob, "item.getType('text/html') should return a Blob"); 96 }, 'validate GetType(type) on a constructed ClipboardItem returns Blob'); 97 98 promise_test(async () => { 99 await getPermissions(); 100 // Test string with various non-Latin characters: Chinese, Arabic, Cyrillic, emoji 101 const nonLatinText = "你好 مرحبا Привет 👋🌍"; 102 const item = new ClipboardItem({ 103 'text/plain': nonLatinText 104 }); 105 const textBlob = await item.getType('text/plain'); 106 107 // Read back the text and verify it matches 108 const readText = await textBlob.text(); 109 assert_equals(readText, nonLatinText, 110 "Text read from ClipboardItem should match the non-Latin text that was written"); 111 }, 'write non-Latin characters with DOMString and verify getType returns a Blob with the same string'); 112 113 promise_test(async () => { 114 await getPermissions(); 115 // Test string with various non-Latin characters: Chinese, Arabic, Cyrillic, emoji 116 const nonLatinText = "你好 مرحبا Привет 👋🌍"; 117 const item = new ClipboardItem({ 118 'text/plain': nonLatinText 119 }); 120 await navigator.clipboard.write([item]); 121 122 // Read back the text and verify it matches 123 const readText = await navigator.clipboard.readText(); 124 assert_equals(readText, nonLatinText, 125 "Text read from clipboard should match the non-Latin text that was written"); 126 }, 'write non-Latin characters with DOMString and verify readText returns the same string'); 127 </script>