escape-pre-content
v1.0.0
Published
Escapes only & and < - the minimal escaping needed for text placed inside an HTML <pre> element.
Downloads
189
Maintainers
Readme
escape-pre-content
escape-pre-content escapes only & and < - the bare minimum
required to safely drop text inside an HTML <pre> element. No quotes, no
>, no Unicode entity tables you'll never use. Just the two
characters that actually matter here.
That's the whole package.
Why would you need this?
Because why not? Per the WHATWG HTML Standard,
normal element content only requires escaping < and ambiguous &
characters. Everything else your htmlEncode is doing to your <pre>
content is well-intentioned overkill.
See also MDN's take on <pre>
content if you
want the receipts.
Install
npm install escape-pre-contentUsage
const { escapePreContent } = require('escape-pre-content');
// or: import { escapePreContent } from 'escape-pre-content';
const code = 'if (a < b && b < c) { return "ok"; }';
document.querySelector('pre').innerHTML = escapePreContent(code);API
escapePreContent(input: string): string
Replaces & with &, then < with < (in that order, so the
& in a freshly-minted < doesn't get double-escaped).
Throws a TypeError if you feed it anything that isn't a
string.
What this package will never do
- Escape quotes. This isn't an attribute value.
- Escape
>. It's neither ambiguous nor dangerous. - Grow an options object.
- Decode anything.
License
MIT
