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
JavaScript
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 </a href="url" /> 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 <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 <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: `<b>bold</b>`,
expected: `<?xml version="1.0" encoding="UTF-8" not="allowed" ?><b>bold</b>`
}, {
options: {
disableProlog: false
},
xml: `<b>bold</b>`,
expected: `<b>bold</b>`
}, {
options: {
disableProlog: false
},
xml: `<b></b>`,
expected: `<b><?xml not="allowed"?></b>`
}, {
options: {
disableProlog: false
},
xml: `text`,
expected: `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)
})