clipboard-copy-selection-line-break.https.html (3724B)
1 <!doctype html> 2 <meta charset="utf-8"> 3 <meta name="timeout" content="long"> 4 <meta name="variant" content="?1-10"> 5 <meta name="variant" content="?11-20"> 6 <meta name="variant" content="?21-30"> 7 <meta name="variant" content="?31-last"> 8 <title>Clipboard copy selection tests - Line breaks</title> 9 <script src="/resources/testharness.js"></script> 10 <script src="/resources/testharnessreport.js"></script> 11 <script src="/resources/testdriver.js"></script> 12 <script src="/resources/testdriver-vendor.js"></script> 13 <script src="/resources/testdriver-actions.js"></script> 14 <script src="/common/subset-tests.js"></script> 15 <script src="resources/user-activation.js"></script> 16 <body> 17 Body needed for test_driver.click()<br> 18 <div id="paragraph"></div> 19 <pre id="preformatted"></pre> 20 <script> 21 22 const CASES = [ 23 "\r", 24 "\n", 25 "\r\r", 26 "\r\n", 27 "\n\r", 28 "\n\n", 29 "\r\r\r", 30 "\r\r\n", 31 "\r\n\r", 32 "\r\n\n", 33 "\n\r\r", 34 "\n\r\n", 35 "\n\n\r", 36 "\n\n\n", 37 "\r \n", 38 "\n \r", 39 ]; 40 41 function normalizeForPrinting(s) { 42 return s.replace(/\r/g, "\\r").replace(/\n/g, "\\n"); 43 } 44 45 function normalizeForComparison(s) { 46 // Convert LF to CRLF on Windows. 47 return navigator.platform.includes('Win') ? s.replace(/\r?\n/g, '\r\n') : s; 48 } 49 50 // Permissions are required in order to invoke navigator.clipboard functions in 51 // an automated test. 52 async function getPermissions() { 53 await tryGrantReadPermission(); 54 await tryGrantWritePermission(); 55 await waitForUserActivation(); 56 } 57 58 async function selectTextAndCopy(id, text) { 59 const element = document.getElementById(id); 60 element.textContent = text; 61 62 await waitForUserActivation(); 63 64 // Select the text. 65 const selection = window.getSelection(); 66 selection.removeAllRanges(); 67 selection.selectAllChildren(element); 68 69 // Copy the selection to the clipboard. 70 await new Promise((resolve) => { 71 document.addEventListener("copy", e => { 72 assert_true(true, "selection copid to clipboard"); 73 resolve(); 74 }, { once: true }); 75 document.execCommand("copy"); 76 }); 77 } 78 79 async function getTextFromPasteEvent() { 80 return new Promise((resolve) => { 81 document.addEventListener("paste", e => { 82 e.preventDefault(); 83 resolve(e.clipboardData.getData("text/plain")); 84 }, { once: true }); 85 sendPasteShortcutKey(); 86 }); 87 } 88 89 async function getTextFromClipboardReadText() { 90 await waitForUserActivation(); 91 return navigator.clipboard.readText(); 92 } 93 94 CASES.forEach(testCase => { 95 const text = `First${testCase}Second`; 96 97 subsetTest(promise_test, async t => { 98 await getPermissions(); 99 await selectTextAndCopy("preformatted", text); 100 let clipboardText = await getTextFromPasteEvent(); 101 assert_equals(clipboardText, normalizeForComparison(text), "Check result from DataTransfer in paste event"); 102 103 clipboardText = await getTextFromClipboardReadText(); 104 assert_equals(clipboardText, normalizeForComparison(text), "Check result from navigator.clipboard.readText()"); 105 }, `test preformatted text with line breaks: "${normalizeForPrinting(testCase)}"`); 106 107 subsetTest(promise_test, async t => { 108 await getPermissions(); 109 await selectTextAndCopy("paragraph", text); 110 let clipboardText = await getTextFromPasteEvent(); 111 assert_equals(clipboardText, `First Second`, "Check result from DataTransfer in paste event"); 112 113 clipboardText = await getTextFromClipboardReadText(); 114 assert_equals(clipboardText, `First Second`, "Check result from navigator.clipboard.readText()"); 115 }, `test paragraph text with line breaks: "${normalizeForPrinting(testCase)}"`); 116 }); 117 118 </script> 119 </body>