This redditor found an interesting Chrome UX behavior on paste. Chrome squashes spaces when pasted into the address bar or in window.prompt
. But does not squash when pasting into input fields or in the console.
Here is a runnable snippet with sample inputs and outputs.
On Chrome:
input | typed | copy/pasted
"h e l l o w o r l d" | 9 spaces | 9 spaces
"hello world" | 9 spaces | 1 spaces
On Firefox:
input | typed | copy/pasted
"h e l l o w o r l d" | 9 spaces | 9 spaces
"hello world" | 9 spaces | 9 spaces
var str = window.prompt('Enter a few spaces.');
var spaces = (str.match(/ /g) || []).length;
console.log(`You entered ${spaces} spaces`);
Is this behavior documented anywhere in Chrome? I can't find this through a cursory search. Should this be considered a Chrome bug? On one hand, it could be nice to hand-hold certain users, but on the other hand, Chrome is not respecting the users' explicit instructions.
Is there a way to prevent this behavior or work around?
More findings, on Chrome 65 on Linux:
Using the X selection copy buffer thing (select, middle-click), the spaces are preserved when pasting into a window.prompt
input. But, the spaces are not preserved when pasting into the address bar.
Using ctrl+c, ctrl+v, spaces are not preserved in either window.prompt
nor the address bar.
Commentors report that this behavior is not present in OSX.
window.prompt()
example, it seems to respect the amount of spaces I type. My User-Agent string isMozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36
if that matters to anyone.hello world
, you have You entered 1 spaces in the console? I do have You entered 9 spaces on chrome65 macOs flavor. Ah funny SO's comment text wrapping rules..."1 9spaces"
. (Stackoverflow also seems to squash that... I have nine spaces there)