async-svg-read-write.tentative.https.html (3483B)
1 <!doctype html> 2 <meta charset="utf-8"> 3 <title> 4 Async Clipboard write ([image/svg+xml ClipboardItem]) -> read and write svg tests 5 </title> 6 <link rel="help" href="https://w3c.github.io/clipboard-apis/#async-clipboard-api"> 7 <body>Body needed for test_driver.click()</body> 8 <script src="/resources/testharness.js"></script> 9 <script src="/resources/testharnessreport.js"></script> 10 <script src="/resources/testdriver.js"></script> 11 <script src="/resources/testdriver-vendor.js"></script> 12 <script src="resources/user-activation.js"></script> 13 <script> 14 'use strict'; 15 // This function removes extra spaces between tags in svg. For example, the 16 // following svg: "<svg> <g> </g> </svg>" would turn into this 17 // svg: "<svg> <g> </g> </svg>" 18 // We remove the extra spaces because in svg they are considered equivalent, 19 // but when we are comparing for equality the spaces make a difference. 20 function reformatSvg(svgString) { 21 return svgString.replace(/\>\s*\</g, '> <'); 22 } 23 24 async function readWriteTest(textInput) { 25 await test_driver.set_permission({name: 'clipboard-read'}, 'granted'); 26 await test_driver.set_permission({name: 'clipboard-write'}, 'granted'); 27 const blobInput = new Blob([textInput.input], {type: 'image/svg+xml'}); 28 const clipboardItem = new ClipboardItem({'image/svg+xml': blobInput}); 29 await waitForUserActivation(); 30 await navigator.clipboard.write([clipboardItem]); 31 await waitForUserActivation(); 32 const clipboardItems = 33 await navigator.clipboard.read({type: 'image/svg+xml'}); 34 35 const svg = clipboardItems[0]; 36 assert_equals(svg.types.length, 1); 37 assert_equals(svg.types[0], 'image/svg+xml'); 38 39 const blobOutput = await svg.getType('image/svg+xml'); 40 assert_equals(blobOutput.type, 'image/svg+xml'); 41 42 const blobText = await (new Response(blobOutput)).text(); 43 const outputSvg = reformatSvg(blobText); 44 if ("expected" in textInput) { 45 assert_equals(outputSvg, reformatSvg(textInput.expected)); 46 } else { 47 assert_equals(outputSvg, reformatSvg(textInput.input)); 48 } 49 } 50 const testCases = [ 51 { input: '<svg></svg>' }, 52 { input: '<svg> <circle cx="50" cy="50" r="40"/> </svg>', 53 expected: '<svg> <circle cx="50" cy="50" r="40"> </circle> </svg>' }, 54 { input: '<svg> <script>const a = 5;\<\/script> <a href="javascript:alert(2)"> test </a> </svg>', 55 expected: '<svg> <a> test </a> </svg>' }, 56 { input: '<svg> <style>.foo {fill: green;}</style>' + 57 '<circle class=\"foo\" cx=\"50\" cy=\"50\" r=\"40\" /> </svg>', 58 // The extra style attributes is due to read-time sanitization. 59 // We would like to improve the expectation but need a better sanitizer first. 60 expected: '<svg style=\"color: rgb(0, 0, 0); font-size: medium;' + 61 ' font-style: normal; font-variant-ligatures: normal;' + 62 ' font-variant-caps: normal; font-weight: 400;' + 63 ' letter-spacing: normal; orphans: 2; text-align: start;' + 64 ' text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px;' + 65 ' -webkit-text-stroke-width: 0px; white-space: normal;' + 66 ' text-decoration-thickness: initial; text-decoration-style: initial;' + 67 ' text-decoration-color: initial;\"> <circle class=\"foo\" cx=\"50\"' + 68 ' cy=\"50\" r=\"40\"> </circle> </svg>' }, 69 ]; 70 71 promise_test(async t => { 72 for (const testCase of testCases) { 73 await readWriteTest(testCase); 74 } 75 }, 'Verify read and write of some image/svg+xml content'); 76 </script>