test_clipboard_nbsp.html (4733B)
1 <!DOCTYPE HTML> 2 <html> 3 <!-- 4 https://bugzilla.mozilla.org/show_bug.cgi?id=359303 5 --> 6 <head> 7 <meta charset="utf-8" /> 8 <title>Test for copying non-breaking spaces to the clipboard</title> 9 <script src="/tests/SimpleTest/SimpleTest.js"></script> 10 <script src="/tests/SimpleTest/EventUtils.js"></script> 11 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/> 12 </head> 13 <body> 14 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=359303">Mozilla Bug 359303</a> 15 16 <p id="display"></p> 17 18 <div id="content"> 19 <!-- In a plain-text editable control (such as a textarea or textinput), copying to clipboard should 20 preserve non-breaking spaces. --> 21 <input 22 id="input-with-non-breaking-spaces" 23 value="Input content: This town is 100 km away / « Est-ce Paris ? » / Consecutive non-breaking spaces: ' '"> 24 <textarea id="textarea-with-non-breaking-spaces"> 25 Textarea content: 26 - This town is 100 km away. 27 - « Est-ce Paris ? » 28 - Consecutive non-breaking spaces: " " 29 </textarea> 30 31 <!-- In a content-editable div, copying to clipboard should preserve non-breaking spaces. 32 However, for compatibility with what other browsers currently do, the behavior of replacing non-breaking spaces by spaces is preserved for now. 33 See https://bugzilla.mozilla.org/show_bug.cgi?id=359303#c145 34 --> 35 <div contenteditable="true" id="content-editable-with-non-breaking-spaces"> 36 Content-editable content: 37 - This town is 100 km away. 38 - « Est-ce Paris ? » 39 - Consecutive non-breaking spaces: " " 40 </div> 41 42 <!-- In non-editable HTML nodes, like this paragraph, copying to clipboard should preserve non-breaking 43 spaces. 44 However, for compatibility with what other browsers currently do, the behavior of replacing non-breaking spaces by spaces is preserved for now. 45 See https://bugzilla.mozilla.org/show_bug.cgi?id=359303#c145 46 --> 47 <p id="paragraph-with-non-breaking-spaces"> 48 Paragraph content: 49 - This town is 100 km away. 50 - « Est-ce Paris ? » 51 - Consecutive non-breaking spaces: " " 52 </p> 53 </div> 54 55 <pre id="test"> 56 <script class="testbody" type="application/javascript"> 57 58 // Helper: for the given element, select all its content, execute a "Copy" command, 59 // and return the text put into the clipboard. 60 async function clipboardTextForElementId(aDomId, aExpectedString) { 61 let textContainer = document.getElementById(aDomId); 62 let sel = window.getSelection(); 63 sel.removeAllRanges(); 64 65 if (textContainer.select) { 66 // Select the entire text in the input or textarea 67 textContainer.select(); 68 } else { 69 // Select text node in element. 70 let r = document.createRange(); 71 r.setStart(textContainer, 0); 72 r.setEnd(textContainer, 1); 73 sel.addRange(r); 74 } 75 76 let copiedText = await SimpleTest.promiseClipboardChange( 77 function compare(aValue) { 78 return aValue.includes(aExpectedString); 79 }, 80 function setup() { 81 synthesizeKey("C", {accelKey: true}); 82 }, 83 "text/plain"); 84 return copiedText; 85 } 86 87 /** Test for Bug 359303 */ 88 SimpleTest.waitForExplicitFinish(); 89 SimpleTest.waitForFocus(async function() { 90 let iValue = await clipboardTextForElementId("input-with-non-breaking-spaces", "Input"); 91 ok(iValue.includes("100 km"), "NBSP between two characters should be preserved"); 92 ok(iValue.includes("« "), "A single NBSP near a punctuation mark should be preserved"); 93 ok(iValue.includes(" »"), "A single NBSP near a punctuation mark should be preserved"); 94 ok(iValue.includes(" ? "), "NBSPs before *and* after a character should be preserved"); 95 ok(iValue.includes(" "), "Consecutive NBSPs should be preserved"); 96 97 let tValue = await clipboardTextForElementId("textarea-with-non-breaking-spaces", "Textarea"); 98 ok(tValue.includes("100 km"), "NBSP between two characters should be preserved"); 99 ok(tValue.includes("« "), "A single NBSP near a punctuation mark should be preserved"); 100 ok(tValue.includes(" »"), "A single NBSP near a punctuation mark should be preserved"); 101 ok(tValue.includes(" ? "), "NBSPs before *and* after a character should be preserved"); 102 ok(tValue.includes(" "), "Consecutive NBSPs should be preserved"); 103 104 let cValue = await clipboardTextForElementId("content-editable-with-non-breaking-spaces", "Content-editable"); 105 ok(cValue.includes("100 km"), "NBSP should be replaced by spaces, until brower compatibility issues are sorted out"); 106 107 let pValue = await clipboardTextForElementId("paragraph-with-non-breaking-spaces", "Paragraph"); 108 ok(pValue.includes("100 km"), "NBSP should be replaced by spaces, until brower compatibility issues are sorted out"); 109 110 SimpleTest.finish(); 111 }); 112 113 </script> 114 </pre> 115 </body> 116 </html>