/*!
* VisualEditor HTML sanitization utilities tests.
*
* @copyright See AUTHORS.txt
*/
QUnit.module( 've.sanitize' );
QUnit.test( 've.sanitizeHtml', ( assert ) => {
const cases = [
{
msg: 'Unsafe link href removed',
html: 'Foo',
expected: 'Foo'
},
{
msg: 'Unsafe onclick removed',
html: 'Foo',
expected: 'Foo'
},
{
msg: 'script and style elements removed',
html: '',
expected: ''
},
{
msg: 'style attribute allowed',
html: 'Foo',
expected: 'Foo'
},
{
msg: 'RDFa attributes allowed',
html: 'Foo',
expected: 'Foo'
},
{
msg: 'figure-inline element allowed',
html: '',
expected: ''
},
{
msg: 'HTML tag in attribute allowed',
html: 'Foo',
expected: 'Foo'
},
{
msg: 'Self-closing HTML tag in attribute allowed',
html: 'Foo',
expected: 'Foo'
}
];
cases.forEach( ( caseItem ) => {
const actual = document.createElement( 'div' );
const nodes = ve.sanitizeHtml( caseItem.html );
Array.prototype.forEach.call( nodes, ( node ) => {
actual.appendChild( node );
} );
const expected = document.createElement( 'div' );
expected.innerHTML = caseItem.expected;
assert.equalDomElement( actual, expected, caseItem.msg );
} );
} );
QUnit.test( 've.sanitizeHtmlToDocument', ( assert ) => {
const nodes = ve.sanitizeHtml( 'Foo' );
const body = document.createElement( 'body' );
Array.prototype.forEach.call( nodes, ( node ) => {
body.appendChild( node );
} );
const sanitizedDocument = ve.sanitizeHtmlToDocument( 'Foo' );
assert.equalDomElement( sanitizedDocument.body, body, 'Body node is the same as if we used ve.sanitizeHtml' );
} );
QUnit.test( 've.setAttributeSafe', ( assert ) => {
const cases = [
{
msg: 'Unsafe link href sets fallback instead',
element: 'Foo',
attr: 'href',
// eslint-disable-next-line no-script-url
val: 'javascript:alert(1)',
fallbackVal: '#',
expected: 'Foo'
},
{
msg: 'Unsafe link href without fallback is no-op',
element: 'Foo',
attr: 'href',
// eslint-disable-next-line no-script-url
val: 'javascript:alert(1)',
expected: 'Foo'
},
{
msg: 'Safe link is set',
element: 'Foo',
attr: 'href',
val: '#javascript',
expected: 'Foo'
},
{
msg: 'onclick is not set',
element: '
Foo
',
attr: 'onclick',
val: 'alert()',
expected: 'Foo
'
},
{
msg: 'RDFa attribute is set',
element: 'Foo
',
attr: 'about',
val: '#1',
expected: 'Foo
'
}
];
cases.forEach( ( caseItem ) => {
const actual = document.createElement( 'div' );
actual.innerHTML = caseItem.element;
ve.setAttributeSafe(
actual.childNodes[ 0 ],
caseItem.attr,
caseItem.val,
caseItem.fallbackVal
);
const expected = document.createElement( 'div' );
expected.innerHTML = caseItem.expected;
assert.equalDomElement( actual, expected, caseItem.msg );
} );
} );