2014-05-23 09:57:58 +00:00
|
|
|
/*!
|
|
|
|
* VisualEditor MediaWiki test utilities.
|
|
|
|
*
|
2023-12-01 16:06:11 +00:00
|
|
|
* @copyright See AUTHORS.txt
|
2014-05-23 09:57:58 +00:00
|
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
|
|
*/
|
|
|
|
|
2021-10-13 13:21:04 +00:00
|
|
|
{
|
|
|
|
const MWDummyTarget = function MWDummyTarget() {
|
2018-04-30 17:53:50 +00:00
|
|
|
MWDummyTarget.super.call( this );
|
2021-10-13 13:21:04 +00:00
|
|
|
};
|
2018-04-30 17:53:50 +00:00
|
|
|
OO.inheritClass( MWDummyTarget, ve.test.utils.DummyTarget );
|
2021-04-30 09:33:22 +00:00
|
|
|
MWDummyTarget.prototype.setDefaultMode = () => {};
|
|
|
|
MWDummyTarget.prototype.isSaveable = () => true;
|
|
|
|
// Ensure a mock server is used (e.g. as in ve.ui.MWWikitextStringTransferHandler)
|
|
|
|
MWDummyTarget.prototype.parseWikitextFragment = () => new mw.Api().post();
|
|
|
|
MWDummyTarget.prototype.getContentApi = () => new mw.Api();
|
2019-11-11 20:21:42 +00:00
|
|
|
MWDummyTarget.prototype.createSurface = ve.init.mw.Target.prototype.createSurface;
|
|
|
|
MWDummyTarget.prototype.getSurfaceConfig = ve.init.mw.Target.prototype.getSurfaceConfig;
|
2021-05-17 21:39:22 +00:00
|
|
|
MWDummyTarget.prototype.getSurfaceClasses = ve.init.mw.Target.prototype.getSurfaceClasses;
|
2019-09-10 13:13:42 +00:00
|
|
|
// Copy import rules from mw target, for paste tests.
|
2019-11-11 20:21:42 +00:00
|
|
|
MWDummyTarget.static.importRules = ve.init.mw.Target.static.importRules;
|
|
|
|
|
|
|
|
ve.test.utils.MWDummyTarget = MWDummyTarget;
|
2018-04-30 17:53:50 +00:00
|
|
|
|
2021-10-13 13:21:04 +00:00
|
|
|
const MWDummyPlatform = function MWDummyPlatform() {
|
2018-06-19 12:30:21 +00:00
|
|
|
MWDummyPlatform.super.apply( this, arguments );
|
|
|
|
// Disable some API requests from platform
|
|
|
|
this.imageInfoCache = null;
|
2021-10-13 13:21:04 +00:00
|
|
|
};
|
2018-06-19 12:30:21 +00:00
|
|
|
OO.inheritClass( MWDummyPlatform, ve.init.mw.Platform );
|
2021-04-30 09:33:22 +00:00
|
|
|
MWDummyPlatform.prototype.getMessage = ( ...args ) => args.join( ',' );
|
|
|
|
MWDummyPlatform.prototype.getHtmlMessage = ( ...args ) => {
|
2021-04-29 14:42:18 +00:00
|
|
|
const $wrapper = $( '<div>' );
|
2021-04-30 09:33:22 +00:00
|
|
|
args.forEach( ( arg, i ) => {
|
|
|
|
if ( i > 0 ) {
|
2021-10-25 15:51:29 +00:00
|
|
|
$wrapper[ 0 ].appendChild( document.createTextNode( ',' ) );
|
2018-06-19 12:30:21 +00:00
|
|
|
}
|
2021-10-25 15:51:29 +00:00
|
|
|
// Strings are converted to text nodes
|
|
|
|
// eslint-disable-next-line no-jquery/no-append-html
|
|
|
|
$wrapper.append( typeof arg === 'string' ? document.createTextNode( arg ) : arg );
|
2018-06-19 12:30:21 +00:00
|
|
|
} );
|
2018-11-05 15:17:47 +00:00
|
|
|
// Merge text nodes
|
|
|
|
$wrapper[ 0 ].normalize();
|
|
|
|
return $wrapper.contents().toArray();
|
2018-06-19 12:30:21 +00:00
|
|
|
};
|
|
|
|
ve.test.utils.MWDummyPlatform = MWDummyPlatform;
|
|
|
|
|
2021-10-13 13:21:04 +00:00
|
|
|
{
|
2021-04-29 14:42:18 +00:00
|
|
|
const setEditorPreference = mw.libs.ve.setEditorPreference,
|
2021-04-30 09:33:22 +00:00
|
|
|
dummySetEditorPreference = () => ve.createDeferred().resolve().promise(),
|
2018-04-30 17:53:50 +00:00
|
|
|
overrides = [
|
|
|
|
ve.dm.MWHeadingNode,
|
|
|
|
ve.dm.MWPreformattedNode,
|
|
|
|
ve.dm.MWTableNode,
|
|
|
|
ve.dm.MWExternalLinkAnnotation
|
|
|
|
],
|
|
|
|
overridden = [
|
|
|
|
ve.dm.InlineImageNode,
|
|
|
|
ve.dm.BlockImageNode
|
|
|
|
];
|
|
|
|
|
2021-04-29 14:42:18 +00:00
|
|
|
const corePlatform = ve.init.platform,
|
|
|
|
coreTarget = ve.init.target,
|
|
|
|
mwPlatform = new ve.test.utils.MWDummyPlatform();
|
2018-04-30 17:53:50 +00:00
|
|
|
// Unregister mwPlatform
|
|
|
|
ve.init.platform = corePlatform;
|
2015-05-17 09:37:34 +00:00
|
|
|
|
2021-04-29 14:42:18 +00:00
|
|
|
const mwTarget = new ve.test.utils.MWDummyTarget();
|
2018-04-30 17:53:50 +00:00
|
|
|
// Unregister mwTarget
|
|
|
|
ve.init.target = coreTarget;
|
2016-10-11 19:48:02 +00:00
|
|
|
|
2021-10-13 13:21:04 +00:00
|
|
|
const setupOverrides = function () {
|
2021-04-29 14:42:18 +00:00
|
|
|
for ( let i = 0; i < overrides.length; i++ ) {
|
2018-04-30 17:53:50 +00:00
|
|
|
ve.dm.modelRegistry.register( overrides[ i ] );
|
|
|
|
}
|
2021-04-29 14:42:18 +00:00
|
|
|
for ( let i = 0; i < overridden.length; i++ ) {
|
2018-04-30 17:53:50 +00:00
|
|
|
ve.dm.modelRegistry.unregister( overridden[ i ] );
|
|
|
|
}
|
2019-11-12 13:49:58 +00:00
|
|
|
ve.ui.windowFactory.unregister( ve.ui.LinkAnnotationInspector );
|
|
|
|
ve.ui.windowFactory.register( ve.ui.MWLinkAnnotationInspector );
|
|
|
|
|
2018-04-30 17:53:50 +00:00
|
|
|
ve.init.platform = mwPlatform;
|
|
|
|
ve.init.target = mwTarget;
|
2018-09-04 12:28:50 +00:00
|
|
|
mw.libs.ve.setEditorPreference = dummySetEditorPreference;
|
2018-04-30 17:53:50 +00:00
|
|
|
// Ensure the current target is appended to the current fixture
|
2019-02-20 20:23:43 +00:00
|
|
|
// eslint-disable-next-line no-jquery/no-global-selector
|
2018-04-30 17:53:50 +00:00
|
|
|
$( '#qunit-fixture' ).append( ve.init.target.$element );
|
2021-10-13 13:21:04 +00:00
|
|
|
};
|
2015-05-17 09:37:34 +00:00
|
|
|
|
2021-10-13 13:21:04 +00:00
|
|
|
const teardownOverrides = function () {
|
2021-04-29 14:42:18 +00:00
|
|
|
for ( let i = 0; i < overrides.length; i++ ) {
|
2018-04-30 17:53:50 +00:00
|
|
|
ve.dm.modelRegistry.unregister( overrides[ i ] );
|
|
|
|
}
|
2021-04-29 14:42:18 +00:00
|
|
|
for ( let i = 0; i < overridden.length; i++ ) {
|
2018-04-30 17:53:50 +00:00
|
|
|
ve.dm.modelRegistry.register( overridden[ i ] );
|
|
|
|
}
|
2019-11-12 13:49:58 +00:00
|
|
|
ve.ui.windowFactory.unregister( ve.ui.MWLinkAnnotationInspector );
|
|
|
|
ve.ui.windowFactory.register( ve.ui.LinkAnnotationInspector );
|
|
|
|
|
2018-04-30 17:53:50 +00:00
|
|
|
ve.init.platform = corePlatform;
|
|
|
|
ve.init.target = coreTarget;
|
2018-09-04 12:28:50 +00:00
|
|
|
mw.libs.ve.setEditorPreference = setEditorPreference;
|
2021-10-13 13:21:04 +00:00
|
|
|
};
|
2015-05-17 09:37:34 +00:00
|
|
|
|
2018-04-30 17:53:50 +00:00
|
|
|
// On load, teardown overrides so the first core tests run correctly
|
|
|
|
teardownOverrides();
|
2015-05-17 09:37:34 +00:00
|
|
|
|
2022-03-04 13:27:29 +00:00
|
|
|
// Deprecated, use ve.test.utils.newMwEnvironment
|
2021-10-13 13:21:04 +00:00
|
|
|
ve.test.utils.mwEnvironment = {
|
2020-10-27 17:08:11 +00:00
|
|
|
beforeEach: setupOverrides,
|
|
|
|
afterEach: teardownOverrides
|
2018-04-30 17:53:50 +00:00
|
|
|
};
|
2022-03-04 13:27:29 +00:00
|
|
|
ve.test.utils.newMwEnvironment = function ( env ) {
|
|
|
|
env = env || {};
|
|
|
|
return QUnit.newMwEnvironment( ve.extendObject( {}, env, {
|
|
|
|
beforeEach: function () {
|
|
|
|
setupOverrides.call( this );
|
|
|
|
if ( env.beforeEach ) {
|
|
|
|
env.beforeEach.call( this );
|
|
|
|
}
|
|
|
|
},
|
|
|
|
afterEach: function () {
|
|
|
|
teardownOverrides.call( this );
|
|
|
|
if ( env.afterEach ) {
|
|
|
|
env.afterEach.call( this );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} ) );
|
|
|
|
};
|
2021-10-13 13:21:04 +00:00
|
|
|
}
|
2015-09-24 15:37:21 +00:00
|
|
|
|
2021-04-29 14:42:18 +00:00
|
|
|
const getDomElementSummaryCore = ve.getDomElementSummary;
|
2015-09-24 15:37:21 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Override getDomElementSummary to extract HTML from data-mw/body.html
|
|
|
|
* and make it comparable.
|
|
|
|
*
|
|
|
|
* @inheritdoc ve#getDomElementSummary
|
|
|
|
*/
|
2021-04-30 09:33:22 +00:00
|
|
|
ve.getDomElementSummary = ( element, includeHtml ) =>
|
2015-09-24 15:37:21 +00:00
|
|
|
// "Parent" method
|
2021-04-30 09:33:22 +00:00
|
|
|
getDomElementSummaryCore( element, includeHtml, ( name, value ) => {
|
2015-09-24 15:37:21 +00:00
|
|
|
if ( name === 'data-mw' ) {
|
2021-04-29 14:42:18 +00:00
|
|
|
const obj = JSON.parse( value ),
|
|
|
|
html = ve.getProp( obj, 'body', 'html' );
|
2015-09-24 15:37:21 +00:00
|
|
|
if ( html ) {
|
|
|
|
obj.body.html = ve.getDomElementSummary( $( '<div>' ).html( html )[ 0 ] );
|
|
|
|
}
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
} );
|
2021-10-13 13:21:04 +00:00
|
|
|
}
|