async-unsanitized-html-formats-write-read.tentative.https.html (3508B)
1 <!doctype html> 2 <meta charset="utf-8"> 3 <title>Async Clipboard unsanitized HTML write -> Async Clipboard unsanitized HTML read test</title> 4 <link rel="help" href="https://w3c.github.io/clipboard-apis/#async-clipboard-api"> 5 <body>Body needed for test_driver.click()</body> 6 <script src="/resources/testharness.js"></script> 7 <script src="/resources/testharnessreport.js"></script> 8 <script src="/resources/testdriver.js"></script> 9 <script src="/resources/testdriver-vendor.js"></script> 10 <script src="resources/user-activation.js"></script> 11 <script> 12 'use strict'; 13 14 // This function removes extra spaces between tags in html. For example, the 15 // following html: "<p> Hello </p> <body> World </body>" would turn into this 16 // html: "<p> Hello </p> <body> World </body>" 17 // We remove the extra spaces because in html they are considered equivalent, 18 // but when we are comparing for equality the spaces make a difference. 19 function reformatHtml(html) { 20 const parser = new DOMParser(); 21 const htmlString = 22 parser.parseFromString(html, 'text/html').documentElement.innerHTML; 23 const reformattedString = htmlString.replace(/\>\s*\</g, '> <'); 24 return reformattedString; 25 } 26 27 // Writes a payload with custom content and checks to ensure the correct data 28 // was written successfully. 29 promise_test(async t => { 30 await tryGrantReadPermission(); 31 await tryGrantWritePermission(); 32 33 // Create and write unsanitized version of standard HTML and custom formats. 34 const format1 = 'text/html'; 35 const format2 = 'web text/html'; 36 const textInput = '<style>p {color:blue}</style><p>Hello World</p>'; 37 const blobInput1 = new Blob([textInput], {type: format1}); 38 const blobInput2 = new Blob([textInput], {type: format2}); 39 const clipboardItemInput = new ClipboardItem( 40 {[format1]: blobInput1, [format2]: blobInput2}); 41 await waitForUserActivation(); 42 await navigator.clipboard.write([clipboardItemInput]); 43 44 // Read unsanitized version of HTML format. 45 await waitForUserActivation(); 46 const clipboardItems = await navigator.clipboard.read(); 47 48 assert_equals(clipboardItems.length, 1); 49 const clipboardItem = clipboardItems[0]; 50 assert_true(clipboardItem instanceof ClipboardItem); 51 52 const blobOutput1 = await clipboardItem.getType(format1); 53 assert_equals(blobOutput1.type, format1); 54 const data1 = await (new Response(blobOutput1)).text(); 55 const outputHtml = reformatHtml(data1); 56 const expectedHtml = '<p style="color: blue; font-size: medium; font-style: normal; ' + 57 'font-variant-ligatures: normal; font-variant-caps: normal; ' + 58 'font-weight: 400; letter-spacing: normal; orphans: 2; ' + 59 'text-align: start; text-indent: 0px; text-transform: none; '+ 60 'widows: 2; word-spacing: 0px; ' + 61 '-webkit-text-stroke-width: 0px; ' + 62 'white-space: normal; ' + 63 'text-decoration-thickness: initial; ' + 64 'text-decoration-style: initial; text-decoration-color: initial;">' + 65 'Hello World</p>'; 66 const inputHtml = reformatHtml(expectedHtml); 67 assert_equals(outputHtml, inputHtml); 68 69 const blobOutput2 = await clipboardItem.getType(format2); 70 assert_equals(blobOutput2.type, format2); 71 const data2 = await (new Response(blobOutput2)).text(); 72 assert_equals(data2, textInput); 73 }, 'Verify write and read unsanitized content to the clipboard given text/html format as input'); 74 </script>