Fastest method to escape HTML tags as HTML entities?

前端 未结 12 1395
-上瘾入骨i
-上瘾入骨i 2020-11-22 09:24

I\'m writing a Chrome extension that involves doing a lot of the following job: sanitizing strings that might contain HTML tags, by converting

12条回答
  •  渐次进展
    2020-11-22 09:45

    An even quicker/shorter solution is:

    escaped = new Option(html).innerHTML
    

    This is related to some weird vestige of JavaScript whereby the Option element retains a constructor that does this sort of escaping automatically.

    Credit to https://github.com/jasonmoo/t.js/blob/master/t.js

提交回复
热议问题