3 ms·
I think part of the reason that we don't have such a function yet is that it is essentially a one-liner if you use decimal notation for entities: function esca
by mark_and_sweep 5y ago
I think part of the reason that we don't have such a function yet is that it is essentially a one-liner if you use decimal notation for entities:
function escape_html(s) { return s.replace(/[&<>"']/g, m => `&#${m.charCodeAt(0)};`) }
That being said, a built-in function would be convenient and faster.
- herodoturtle 5y agoThere are many built-in functions that could also be accomplished with a one-liner akin to your example. I hear what you're saying - there's an easy manual workaround for this requirement - but a built-in function would still add value nonetheless.
- mark_and_sweep 5y agoAgreed. As I said, it would be convenient, faster, and most importantly, guaranteed to be error-free.
- felixfbecker 5y agoThe `textContent` setter also does this. E.g. you can do this: const span = document.createElement('span') span.textContent = text return span.innerHTML
- czechdeveloper 5y agoThis does not handle quotes as far as I remember
- ufo 5y agoOne problem is that it's just as simple to implement an incirrect version of this function