xmldefuse
Encode the predefined XML entities (amp, lt, gt, apos, quot).
Opt-out CharRef 39 for HTML. Bonus: Encode CDATA.
Usage
From test.js:
var xmldefuse = require("xmldefuse"),
rawXY = "X && <lt< >gt> 'apos' \"quot\" Y",
rawCD = "Have <![CDATA[ marks ]]> in ]]> text",
eq = require("assert").strictEqual;
eq(xmldefuse(rawXY),
"X &amp& <lt< >gt> 'apos' "quot" Y");
eq(xmldefuse.apos(rawXY),
"X &amp& <lt< >gt> 'apos' "quot" Y");
eq(xmldefuse.cdata(rawXY),
"<![CDATA[X && <lt< >gt> 'apos' \"quot\" Y]]>");
eq(xmldefuse.cdata(rawCD),
"<![CDATA[Have <![CDATA[ marks ]]]]><![CDATA[> in ]]]]><![CDATA[> text]]>");
CLI mode:
$ grep -oPe 'X.+Y' -m 1 test.js | xmldefuse
X &amp& <lt< >gt> 'apos' \"quot\" Y
$ grep -oPe 'X.+Y' -m 1 test.js | xmldefuse.apos
X &amp& <lt< >gt> 'apos' \"quot\" Y
Why is apos opt-in?
The default is HTML compatibility mode because it will help visitors of
beginner webmasters who just plug the module in their HTML generator without
reading this and ignore that XML's "apos" entity is not included in the
list of valid HTML 4 entities.
Related
- xmlunidefuse:
Convert some additional, easily overlooked Unicode characters to CharRefs.
- xmldecode:
Decode the predefined XML entities, CharRefs and CDATA sections.
License
ISC