2014-05-23 09:57:58 +00:00
|
|
|
/*!
|
|
|
|
* VisualEditor MediaWiki test utilities.
|
|
|
|
*
|
2019-01-01 13:24:23 +00:00
|
|
|
* @copyright 2011-2019 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();
|
|
|
|
};
|
|
|
|
ve.test.utils.MWDummyTarget = MWDummyTarget;
|
2019-09-10 13:13:42 +00:00
|
|
|
// Copy import rules from mw target, for paste tests.
|
|
|
|
ve.test.utils.MWDummyTarget.static.importRules = ve.init.mw.Target.static.importRules;
|
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
|
|
|
// Unregister MW override nodes.
|
|
|
|
// They are temporarily registered in setup/teardown.
|
|
|
|
ve.dm.modelRegistry.unregister( ve.dm.MWHeadingNode );
|
|
|
|
ve.dm.modelRegistry.unregister( ve.dm.MWPreformattedNode );
|
|
|
|
ve.dm.modelRegistry.unregister( ve.dm.MWTableNode );
|
|
|
|
ve.dm.modelRegistry.unregister( ve.dm.MWExternalLinkAnnotation );
|
|
|
|
// Re-register unregistered nodes.
|
|
|
|
ve.dm.modelRegistry.register( ve.dm.InlineImageNode );
|
|
|
|
ve.dm.modelRegistry.register( ve.dm.BlockImageNode );
|
|
|
|
|
|
|
|
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 ] );
|
|
|
|
}
|
|
|
|
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 ] );
|
|
|
|
}
|
|
|
|
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
|
|
|
}() );
|