Skip to content

Encode the predefined XML entities (amp, lt, gt, apos, quot). Opt-out CharRef 39 for HTML.

Notifications You must be signed in to change notification settings

mk-pmb/xmldefuse-js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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
X &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
X &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

About

Encode the predefined XML entities (amp, lt, gt, apos, quot). Opt-out CharRef 39 for HTML.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published