Created
June 2, 2021 13:41
-
-
Save fabiospampinato/a2a38148f76188cebaabd1bea41dddb0 to your computer and use it in GitHub Desktop.
The fastest way to escape HTML strings known to me~~n~~, if you need to do so with JS and you are inside a browser.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Can you make this faster? Ping me. | |
const escapeHtml = (function () { | |
const serializer = new XMLSerializer (); | |
const attr = document.createAttribute ( 'attr' ); | |
const re = /[&<>"]/; | |
return function escapeHtml ( str ) { | |
if ( !re.test ( str ) ) return str; | |
attr.value = str; | |
return serializer.serializeToString ( attr ); | |
} | |
})() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment