Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it ok to output `\xA0` or `\xC2A0` in my HTML pages, or do I need to escape it as ` `?

I have a large JS codebase, not all of which I can control, where we use non-breaking spaces in locale files, for what non-breaking spaces are actually meant to be used (to prevent wrapping).

Currently, these non-breaking spaces are specified in .yml files as \_, which our YAML loader is replacing with \xA0 (the actual byte 160, not the escape sequence, which is ASCII for nbsp), and that \xA0 is getting output directly to the browser.

As far as I can see, these do work. They're not getting wrapped. But given the existence of  , it feels "wrong" to output the ASCII directly.

I've been googling for quite a while, but I can't find any information on this...

Is it ok to output \xA0 (the actual byte 160, not the escape sequence) as part of my HTML? Or should I always encode as  ? (and if the latter, why? What's the downside?)

like image 577
Daniel Magliola Avatar asked Oct 25 '25 10:10

Daniel Magliola


1 Answers

Printing no-break spaces in HTML as \xA0 escape sequences via JavaScript strings is in fact printing the actual no-break space characters themselves.

Since no-break spaces don't have any special meaning in HTML, it makes no meaningful difference whether you print the characters directly or use the character references. (User agents that can't handle UTF-8 encoded HTML may beg to differ, but I suspect those are a theoretical problem.)

like image 188
BoltClock Avatar answered Oct 27 '25 00:10

BoltClock