Splitting icon indicating copy to clipboard operation
Splitting copied to clipboard

Nonbreaking Whitespaces

Open shshaw opened this issue 7 years ago • 5 comments

Need to think through collapsing whitespace. 99% of the time, whitespace in the DOM should be collapsed/ignored, but perhaps with whitespace: true,  ’s could be treated like characters & preserved for instances like this?

screen shot 2018-08-22 at 4 51 40 pm

https://codepen.io/nexii/pen/VBVeMx/

shshaw avatar Aug 22 '18 21:08 shshaw

I'm not sure how to do this without making significant changes to the splitText function. The splitOn expression is collapsing sibling spaces. Given that it results an empty string as one of the split results, we might be able to convert that to 3 whitespaces. Perhaps?

notoriousb1t avatar Aug 22 '18 22:08 notoriousb1t

Does   show up differently in the DOM than a regular space?

shshaw avatar Aug 23 '18 01:08 shshaw

Yes, but we convert it regular spaces with "wholeText" I believe.

notoriousb1t avatar Aug 23 '18 01:08 notoriousb1t

shshaw [12:20 PM] https://codepen.io/shshaw/pen/c01954047c28ad93e173be88dd88943f?editors=1010

shshaw [12:27 PM] Not sure why the original DOM node’s innerText is trimmed & cleaned up, whereas the testNode isn’t

notoriousb1t [12:27 PM] it is saying appendChild is not available? (edited)

shshaw [12:28 PM] Refresh

notoriousb1t [12:35 PM] https://stackoverflow.com/questions/1495822/replacing-nbsp-from-javascript-dom-text-node check this out non breaking space is a different char code than space (edited)

shshaw [12:36 PM] Hm, yeah.

notoriousb1t [12:36 PM] so we can swap that out for a delimiter or change the split pattern to exclude it

shshaw [12:45 PM] delimiter? Yeah, the split regex is definitely aggressive right now with all whitespace /[\t\n\r ]+/ may be a better approach? https://regexr.com/3ucq8 /[\t\n\r\v\f ]+/gm Alternative: /[^ \S]+/gm match everything that’s NOT a nonbreaking space or NOT whitespace These would potentially group  s with the closest non-whitespace separated word, though. (edited)

notoriousb1t [1:02 PM] I was suggesting either to replace   with a different character and then do a /\s+/ replace to " " or use /[\t\n\r ]+/ or similar I am not sure what \f and \v are specifically other whitespace characters?

shshaw [1:03 PM] Yeah “feed” and “vertical tab”

notoriousb1t [1:04 PM] oh

shshaw [1:04 PM] feed = different type of return vertical tab, no idea

notoriousb1t [1:04 PM] but doing a regex replace on wholeText that will give us the proper amount of spaces and then replacing "" in split results with a whitespace sounds like a good strategy

shshaw avatar Aug 23 '18 18:08 shshaw

I'm encountering this issue right now. My users can add   in their text at various places and the lettering effect breaks or jumps when there's a line break in the middle of an otherwise nbsp;.

Has this ever been solved by someone so that I can get inspiration to solve it on my end? 🤔 Maybe with a custom plugin?

davidwebca avatar Jul 12 '22 21:07 davidwebca