/*!
* Parsoid utilities tests.
*
* @copyright 2011-2020 VisualEditor Team and others; see AUTHORS.txt
* @license The MIT License (MIT); see LICENSE.txt
*/
QUnit.module( 've.utils.parsoid', ve.test.utils.newMwEnvironment() );
QUnit.test( 'reduplicateStyles/deduplicateStyles', ( assert ) => {
// Test cases based on this page and the templates there:
// https://en.wikipedia.beta.wmflabs.org/wiki/Table_templated
const stylesCases = [
{
msg: 'styles are deduplicated',
deduplicated: `
`,
reduplicated: `
`
},
{
msg: 'styles in fosterable positions are NOT deduplicated, but they are emptied',
deduplicated: `
`,
reduplicated: `
`
}
];
stylesCases.forEach( ( caseItem ) => {
const doc = ve.parseXhtml( caseItem.deduplicated );
// Test that we can re-duplicate styles, which were de-duplicated in Parsoid HTML
mw.libs.ve.reduplicateStyles( doc.body );
assert.equalDomElement(
doc.body,
ve.parseXhtml( caseItem.reduplicated ).body,
caseItem.msg + ' (reduplicated)'
);
// Test that we can de-duplicate styles again, producing a result identical to the Parsoid HTML
mw.libs.ve.deduplicateStyles( doc.body );
assert.equalDomElement(
doc.body,
ve.parseXhtml( caseItem.deduplicated ).body,
caseItem.msg + ' (deduplicated)'
);
} );
} );
QUnit.test( 'getTargetDataFromHref', ( assert ) => {
const doc = ve.parseXhtml( '' );
const hrefCases = [
{
/* eslint-disable no-script-url */
msg: 'Invalid protocol is handled as internal link',
href: 'javascript:alert()',
expectedInfo: {
title: 'javascript:alert()',
rawTitle: 'javascript:alert()',
isInternal: true
}
/* eslint-enable no-script-url */
},
{
msg: 'Invalid protocol is handled as internal link',
href: 'not-a-protocol:Some%20text',
expectedInfo: {
title: 'not-a-protocol:Some text',
rawTitle: 'not-a-protocol:Some%20text',
isInternal: true
}
},
{
msg: 'Valid protocol is handled as external link',
href: 'https://example.net/',
expectedInfo: {
title: 'https://example.net/',
rawTitle: 'https://example.net/',
isInternal: false
}
},
{
msg: 'Valid protocol is handled as external link',
href: 'mailto:example@example.net',
expectedInfo: {
title: 'mailto:example@example.net',
rawTitle: 'mailto:example@example.net',
isInternal: false
}
}
];
hrefCases.forEach( ( caseItem ) => {
const actualInfo = mw.libs.ve.getTargetDataFromHref( caseItem.href, doc );
assert.deepEqual( actualInfo, caseItem.expectedInfo, caseItem.msg );
} );
} );