xmldefuse

0.1.2 • Public • Published

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 &amp& <lt< >gt> 'apos' \"quot\" Y",
  rawCD = "Have <![CDATA[ marks ]]> in ]]> text",
  eq = require("assert").strictEqual;
 
eq(xmldefuse(rawXY),
  "X &amp;amp&amp; &lt;lt&lt; &gt;gt&gt; &#39;apos&#39; &quot;quot&quot; Y");
 
eq(xmldefuse.apos(rawXY),
  "X &amp;amp&amp; &lt;lt&lt; &gt;gt&gt; &apos;apos&apos; &quot;quot&quot; Y");
 
eq(xmldefuse.cdata(rawXY),
  "<![CDATA[X &amp& <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
&amp;amp&amp; &lt;lt&lt; &gt;gt&gt; &#39;apos&#39; \&quot;quot\&quot; Y
 
$ grep -oPe 'X.+Y' -m 1 test.js | xmldefuse.apos
&amp;amp&amp; &lt;lt&lt; &gt;gt&gt; &apos;apos&apos; \&quot;quot\&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

Package Sidebar

Install

npm i xmldefuse

Weekly Downloads

1

Version

0.1.2

License

ISC

Last publish

Collaborators

  • mk-pmb