2014-05-23 09:57:58 +00:00
|
|
|
/*!
|
|
|
|
* VisualEditor MediaWiki test utilities.
|
|
|
|
*
|
2020-01-08 17:13:04 +00:00
|
|
|
* @copyright 2011-2020 VisualEditor Team and others; see AUTHORS.txt
|
2014-05-23 09:57:58 +00:00
|
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
|
|
*/
|
|
|
|
|
2018-04-30 17:53:50 +00:00
|
|
|
( function () {
|
|
|
|
var getDomElementSummaryCore;
|
2017-04-13 00:17:29 +00:00
|
|
|
|
2018-04-30 17:53:50 +00:00
|
|
|
function MWDummyTarget() {
|
|
|
|
MWDummyTarget.super.call( this );
|
2017-04-13 00:17:29 +00:00
|
|
|
}
|
2018-04-30 17:53:50 +00:00
|
|
|
OO.inheritClass( MWDummyTarget, ve.test.utils.DummyTarget );
|
|
|
|
MWDummyTarget.prototype.setDefaultMode = function () {};
|
2019-04-15 22:39:56 +00:00
|
|
|
MWDummyTarget.prototype.isSaveable = function () {
|
|
|
|
return true;
|
|
|
|
};
|
2018-04-30 17:53:50 +00:00
|
|
|
MWDummyTarget.prototype.parseWikitextFragment = function () {
|
|
|
|
// Ensure a mock server is used (e.g. as in ve.ui.MWWikitextStringTransferHandler)
|
|
|
|
return new mw.Api().post();
|
|
|
|
};
|
2019-11-11 20:15:16 +00:00
|
|
|
MWDummyTarget.prototype.getContentApi = function () {
|
|
|
|
return 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;
|
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
|
|
|
|
2018-06-19 12:30:21 +00:00
|
|
|
function MWDummyPlatform() {
|
|
|
|
MWDummyPlatform.super.apply( this, arguments );
|
|
|
|
// Disable some API requests from platform
|
|
|
|
this.imageInfoCache = null;
|
|
|
|
}
|
|
|
|
OO.inheritClass( MWDummyPlatform, ve.init.mw.Platform );
|
2018-10-29 23:19:16 +00:00
|
|
|
MWDummyPlatform.prototype.getMessage = function () {
|
|
|
|
return Array.prototype.join.call( arguments, ',' );
|
|
|
|
};
|
2018-06-19 12:30:21 +00:00
|
|
|
MWDummyPlatform.prototype.getHtmlMessage = function () {
|
|
|
|
var $wrapper = $( '<div>' );
|
|
|
|
Array.prototype.forEach.call( arguments, function ( arg, i, args ) {
|
|
|
|
$wrapper.append( arg );
|
|
|
|
if ( i < args.length - 1 ) {
|
|
|
|
$wrapper.append( ',' );
|
|
|
|
}
|
|
|
|
} );
|
2018-11-05 15:17:47 +00:00
|
|
|
// Merge text nodes
|
2018-11-21 12:33:32 +00:00
|
|
|
// eslint-disable-next-line no-restricted-properties
|
2018-11-05 15:17:47 +00:00
|
|
|
$wrapper[ 0 ].normalize();
|
|
|
|
return $wrapper.contents().toArray();
|
2018-06-19 12:30:21 +00:00
|
|
|
};
|
|
|
|
ve.test.utils.MWDummyPlatform = MWDummyPlatform;
|
|
|
|
|
2018-04-30 17:53:50 +00:00
|
|
|
ve.test.utils.mwEnvironment = ( function () {
|
|
|
|
var mwPlatform, corePlatform, mwTarget, coreTarget,
|
2018-09-04 12:28:50 +00:00
|
|
|
setEditorPreference = mw.libs.ve.setEditorPreference,
|
2019-11-02 05:06:28 +00:00
|
|
|
dummySetEditorPreference = function () { return 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
|
|
|
|
];
|
|
|
|
|
|
|
|
corePlatform = ve.init.platform;
|
|
|
|
coreTarget = ve.init.target;
|
2018-06-19 12:30:21 +00:00
|
|
|
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
|
|
|
|
2018-04-30 17:53:50 +00:00
|
|
|
mwTarget = new ve.test.utils.MWDummyTarget();
|
|
|
|
// Unregister mwTarget
|
|
|
|
ve.init.target = coreTarget;
|
2016-10-11 19:48:02 +00:00
|
|
|
|
2018-04-30 17:53:50 +00:00
|
|
|
function setupOverrides() {
|
|
|
|
var i;
|
|
|
|
for ( i = 0; i < overrides.length; i++ ) {
|
|
|
|
ve.dm.modelRegistry.register( overrides[ i ] );
|
|
|
|
}
|
|
|
|
for ( i = 0; i < overridden.length; i++ ) {
|
|
|
|
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 );
|
2015-05-17 09:37:34 +00:00
|
|
|
}
|
|
|
|
|
2018-04-30 17:53:50 +00:00
|
|
|
function teardownOverrides() {
|
|
|
|
var i;
|
|
|
|
for ( i = 0; i < overrides.length; i++ ) {
|
|
|
|
ve.dm.modelRegistry.unregister( overrides[ i ] );
|
|
|
|
}
|
|
|
|
for ( i = 0; i < overridden.length; i++ ) {
|
|
|
|
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;
|
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
|
|
|
|
2018-04-30 17:53:50 +00:00
|
|
|
return {
|
|
|
|
setup: setupOverrides,
|
|
|
|
teardown: teardownOverrides
|
|
|
|
};
|
|
|
|
}() );
|
2015-09-24 15:37:21 +00:00
|
|
|
|
2018-04-30 17:53:50 +00:00
|
|
|
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
|
|
|
|
*/
|
|
|
|
ve.getDomElementSummary = function ( element, includeHtml ) {
|
|
|
|
// "Parent" method
|
|
|
|
return getDomElementSummaryCore( element, includeHtml, function ( name, value ) {
|
|
|
|
var obj, html;
|
|
|
|
if ( name === 'data-mw' ) {
|
|
|
|
obj = JSON.parse( value );
|
|
|
|
html = ve.getProp( obj, 'body', 'html' );
|
|
|
|
if ( html ) {
|
|
|
|
obj.body.html = ve.getDomElementSummary( $( '<div>' ).html( html )[ 0 ] );
|
|
|
|
}
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
} );
|
|
|
|
};
|
2016-11-12 14:43:43 +00:00
|
|
|
}() );
|