UNPKG

sanitize-xml

Version:

‘sanitize-xml’ simply sanitizes xml. Works in both Node.js and the browser. Lightweight. No dependencies.

118 lines (116 loc) 3.42 kB
const assert = require('assert') const sanitizeXml = require('./sanitize-xml'); [{ xml: `one <a >two</a > three`, expected: `one <a>two</a> three` }, { xml: `one<Br/>two<bR />three`, expected: `one<br />two<br />three` }, { xml: `one <img src="url" \n alt="alt"/> two`, expected: `one <img src="url" alt="alt" /> two` }, { xml: `one <a ref="url">two</a> three`, expected: `one <a>two</a> three` }, { xml: `one <a>two <c>three</c> four</a> five`, expected: `one <a>two four</a> five` }, { xml: `one <a>two <c>three<c> four</a> <c>five</c> six`, expected: `one <a>two </a> six` }, { xml: `one <a>two <b>three<b> four</a> five`, expected: `one <a>two <b>three<b> four</b></b></a> five` }, { xml: `one <a>two <b>three<b> four`, expected: `one <a>two <b>three<b> four</b></b></a>` }, { xml: `one </a href="url" /> two`, expected: `one &lt;/a href="url" /&gt; two` }, { xml: `one <a href>url</a> two`, expected: `one <a href>url</a> two` }, { xml: `one <img src="url" alt='alt' /> two`, expected: `one <img src="url" alt='alt' /> two` }, { xml: `one <a href="url<url">two</a> three`, expected: `one &lt;a href="url` }, { xml: `one <a href="url>url">two</a> three`, expected: `one <a href="url>url">two</a> three` }, { xml: `one <a href="url<url>url">two</a> three`, expected: `one &lt;a href="url` }, { xml: ` one <a>two</a> three `, expected: ` one <a>two</a> three ` }, { xml: `one <!--comment--> two`, expected: `one two` }, { options: { removeComments: false }, xml: `one <!--comment--> two`, expected: `one <!--comment--> two` }, { options: { allowedTags: { a: { 'at-tri-bute': true, 'at:tr': true } } }, xml: `one <a at-tri-bute="dash" at:tr="colon">two</a> three`, expected: `one <a at-tri-bute="dash" at:tr="colon">two</a> three` }, { options: { caseInsensitiveTags: false }, xml: `one <a href="url">two</A> three`, expected: `one <a href="url">two three</a>` }, { xml: `one <a href="url">two</A> three`, expected: `one <a href="url">two</a> three` }, { options: { caseInsensitiveAttributes: false }, xml: `one <a href="url" HREF="URL">two</a> three`, expected: `one <a href="url">two</a> three` }, { xml: `one <a href="url" HREF="URL">two</a> three`, expected: `one <a href="url" href="URL">two</a> three` }, { xml: `<?xml version="1.0" encoding="UTF-8" not="allowed" ?><b>bold</b>`, expected: `&lt;?xml version="1.0" encoding="UTF-8" not="allowed" ?&gt;<b>bold</b>` }, { options: { disableProlog: false }, xml: `<?xml version="1.0" encoding="UTF-8" not="allowed" ?><b>bold</b>`, expected: `<?xml version="1.0" encoding="UTF-8" ?><b>bold</b>` }, { options: { disableProlog: false }, xml: `<?xml version="1.0"?><b><?xml not="allowed"?></b>`, expected: `<?xml version="1.0" ?><b>&lt;?xml not="allowed"?&gt;</b>` }, { options: { disableProlog: false }, xml: `<?xml version="1.0" encoding="UTF-8" not="allowed" ?>text`, expected: `<?xml version="1.0" encoding="UTF-8" ?>text` }, { xml: `pseudo #text node`, expected: `pseudo #text node` }, { xml: `<a href=""></a>`, expected: `<a href=""></a>` }, { options: { removeComments: false }, xml: `<a><b><!-- comment -->text</b></a>`, expected: `<a><b><!-- comment -->text</b></a>` }].forEach(function (test) { assert.deepStrictEqual(sanitizeXml(test.xml, test.options), test.expected) })