2015-08-07 18:16:09 +00:00
|
|
|
/*!
|
|
|
|
* VisualEditor UserInterface MWWikitextStringTransferHandler tests.
|
|
|
|
*
|
2020-01-08 17:13:04 +00:00
|
|
|
* @copyright 2011-2020 VisualEditor Team and others; see http://ve.mit-license.org
|
2015-08-07 18:16:09 +00:00
|
|
|
*/
|
2017-04-29 11:37:53 +00:00
|
|
|
|
2015-08-07 18:16:09 +00:00
|
|
|
QUnit.module( 've.ui.MWWikitextStringTransferHandler', QUnit.newMwEnvironment( {
|
|
|
|
setup: function () {
|
|
|
|
// Mock XHR for mw.Api()
|
2020-05-20 14:54:04 +00:00
|
|
|
this.server = this.sandbox.useFakeServer();
|
|
|
|
// Random number, chosen by a fair dice roll.
|
|
|
|
// Used to make #mwt ID deterministic
|
2018-09-12 13:52:54 +00:00
|
|
|
this.randomStub = sinon.stub( Math, 'random' ).returns( 0.04 );
|
2016-09-22 03:57:58 +00:00
|
|
|
ve.test.utils.mwEnvironment.setup.call( this );
|
|
|
|
},
|
2018-09-12 13:52:54 +00:00
|
|
|
teardown: function () {
|
|
|
|
this.randomStub.restore();
|
|
|
|
ve.test.utils.mwEnvironment.teardown.call( this );
|
|
|
|
}
|
2015-08-07 18:16:09 +00:00
|
|
|
} ) );
|
|
|
|
|
|
|
|
/* Tests */
|
|
|
|
|
2017-04-20 16:07:46 +00:00
|
|
|
ve.test.utils.runWikitextStringHandlerTest = function ( assert, server, string, mimeType, expectedResponse, expectedData, annotations, assertDom, msg ) {
|
2015-08-23 10:12:44 +00:00
|
|
|
var handler, i, j, name,
|
2015-08-07 18:16:09 +00:00
|
|
|
done = assert.async(),
|
|
|
|
item = ve.ui.DataTransferItem.static.newFromString( string, mimeType ),
|
2017-03-02 03:50:21 +00:00
|
|
|
doc = ve.dm.Document.static.newBlankDocument(),
|
2015-08-07 18:16:09 +00:00
|
|
|
mockSurface = {
|
|
|
|
getModel: function () {
|
|
|
|
return {
|
|
|
|
getDocument: function () {
|
|
|
|
return doc;
|
|
|
|
}
|
|
|
|
};
|
2015-08-23 10:54:21 +00:00
|
|
|
},
|
|
|
|
createProgress: function () {
|
2019-11-02 05:06:28 +00:00
|
|
|
return ve.createDeferred().promise();
|
2015-08-07 18:16:09 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Preprocess the expectedData array
|
|
|
|
for ( i = 0; i < expectedData.length; i++ ) {
|
|
|
|
if ( Array.isArray( expectedData[ i ] ) ) {
|
|
|
|
for ( j = 0; j < expectedData[ i ][ 1 ].length; j++ ) {
|
|
|
|
if ( typeof expectedData[ i ][ 1 ][ j ] === 'number' ) {
|
|
|
|
expectedData[ i ][ 1 ][ j ] = annotations[ expectedData[ i ][ 1 ][ j ] ];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-23 10:12:44 +00:00
|
|
|
// Check we match the wikitext string handler
|
|
|
|
name = ve.ui.dataTransferHandlerFactory.getHandlerNameForItem( item );
|
|
|
|
assert.strictEqual( name, 'wikitextString', msg + ': triggers match function' );
|
|
|
|
|
2015-08-07 18:16:09 +00:00
|
|
|
// Invoke the handler
|
|
|
|
handler = ve.ui.dataTransferHandlerFactory.create( 'wikitextString', mockSurface, item );
|
|
|
|
|
2017-03-07 15:44:50 +00:00
|
|
|
handler.getInsertableData().done( function ( docOrData ) {
|
|
|
|
var actualData, store;
|
|
|
|
if ( docOrData instanceof ve.dm.Document ) {
|
|
|
|
actualData = docOrData.getData();
|
|
|
|
store = docOrData.getStore();
|
|
|
|
} else {
|
|
|
|
actualData = docOrData;
|
2018-03-06 12:44:37 +00:00
|
|
|
store = new ve.dm.HashValueStore();
|
2017-03-07 15:44:50 +00:00
|
|
|
}
|
|
|
|
ve.dm.example.postprocessAnnotations( actualData, store );
|
2016-09-22 03:57:58 +00:00
|
|
|
if ( assertDom ) {
|
2017-03-07 15:44:50 +00:00
|
|
|
assert.equalLinearDataWithDom( store, actualData, expectedData, msg + ': data match (with DOM)' );
|
2016-09-22 03:57:58 +00:00
|
|
|
} else {
|
|
|
|
assert.equalLinearData( actualData, expectedData, msg + ': data match' );
|
|
|
|
}
|
2015-08-07 18:16:09 +00:00
|
|
|
done();
|
|
|
|
} );
|
|
|
|
|
2017-03-07 15:44:50 +00:00
|
|
|
if ( server && expectedResponse ) {
|
2015-08-07 18:16:09 +00:00
|
|
|
server.respond( [ 200, { 'Content-Type': 'application/json' }, JSON.stringify( {
|
|
|
|
visualeditor: {
|
|
|
|
result: 'success',
|
2016-03-29 13:31:32 +00:00
|
|
|
content: '<body lang="en" class="mw-content-ltr sitedir-ltr ltr mw-body mw-body-content mediawiki" dir="ltr">' +
|
|
|
|
expectedResponse +
|
|
|
|
'</body>'
|
2015-08-07 18:16:09 +00:00
|
|
|
}
|
|
|
|
} ) ] );
|
|
|
|
}
|
2017-04-20 16:07:46 +00:00
|
|
|
};
|
2015-08-07 18:16:09 +00:00
|
|
|
|
|
|
|
QUnit.test( 'convert', function ( assert ) {
|
|
|
|
var i,
|
|
|
|
cases = [
|
|
|
|
{
|
|
|
|
msg: 'Simple link',
|
2015-08-23 10:12:44 +00:00
|
|
|
// Put link in the middle of text to verify that the
|
|
|
|
// start-of-line and end-or-line anchors on the heading
|
|
|
|
// identification pattern don't affect link identification
|
|
|
|
pasteString: 'some [[Foo]] text',
|
2015-08-07 18:16:09 +00:00
|
|
|
pasteType: 'text/plain',
|
2016-03-29 13:31:32 +00:00
|
|
|
parsoidResponse: '<p>some <a rel="mw:WikiLink" href="./Foo" title="Foo">Foo</a> text</p>',
|
2015-08-07 18:16:09 +00:00
|
|
|
annotations: [ {
|
|
|
|
type: 'link/mwInternal',
|
|
|
|
attributes: {
|
|
|
|
lookupTitle: 'Foo',
|
|
|
|
normalizedTitle: 'Foo',
|
|
|
|
origTitle: 'Foo',
|
|
|
|
title: 'Foo'
|
|
|
|
}
|
|
|
|
} ],
|
|
|
|
expectedData: [
|
2019-09-17 08:21:51 +00:00
|
|
|
{ type: 'paragraph' },
|
2015-08-23 10:12:44 +00:00
|
|
|
's',
|
|
|
|
'o',
|
|
|
|
'm',
|
|
|
|
'e',
|
|
|
|
' ',
|
2015-08-07 18:16:09 +00:00
|
|
|
[ 'F', [ 0 ] ],
|
|
|
|
[ 'o', [ 0 ] ],
|
|
|
|
[ 'o', [ 0 ] ],
|
2015-08-23 10:12:44 +00:00
|
|
|
' ',
|
|
|
|
't',
|
|
|
|
'e',
|
|
|
|
'x',
|
|
|
|
't',
|
2019-09-17 08:21:51 +00:00
|
|
|
{ type: '/paragraph' },
|
2015-08-07 18:16:09 +00:00
|
|
|
{ type: 'internalList' },
|
|
|
|
{ type: '/internalList' }
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
msg: 'Simple link with no p-wrapping',
|
|
|
|
pasteString: '*[[Foo]]',
|
|
|
|
pasteType: 'text/plain',
|
2016-03-29 13:31:32 +00:00
|
|
|
parsoidResponse: '<ul><li><a rel="mw:WikiLink" href="./Foo" title="Foo">Foo</a></li></ul>',
|
2015-08-07 18:16:09 +00:00
|
|
|
annotations: [ {
|
|
|
|
type: 'link/mwInternal',
|
|
|
|
attributes: {
|
|
|
|
lookupTitle: 'Foo',
|
|
|
|
normalizedTitle: 'Foo',
|
|
|
|
origTitle: 'Foo',
|
|
|
|
title: 'Foo'
|
|
|
|
}
|
|
|
|
} ],
|
|
|
|
expectedData: [
|
|
|
|
{
|
|
|
|
type: 'list',
|
|
|
|
attributes: { style: 'bullet' }
|
|
|
|
},
|
|
|
|
{ type: 'listItem' },
|
|
|
|
{
|
|
|
|
type: 'paragraph',
|
|
|
|
internal: { generated: 'wrapper' }
|
|
|
|
},
|
|
|
|
[ 'F', [ 0 ] ],
|
|
|
|
[ 'o', [ 0 ] ],
|
|
|
|
[ 'o', [ 0 ] ],
|
|
|
|
{ type: '/paragraph' },
|
|
|
|
{ type: '/listItem' },
|
|
|
|
{ type: '/list' },
|
|
|
|
{ type: 'internalList' },
|
|
|
|
{ type: '/internalList' }
|
|
|
|
]
|
|
|
|
},
|
2018-09-12 13:52:54 +00:00
|
|
|
{
|
|
|
|
msg: 'Simple template',
|
|
|
|
pasteString: '{{Template}}',
|
|
|
|
pasteType: 'text/plain',
|
|
|
|
parsoidResponse: '<div typeof="mw:Transclusion" about="#mwt1">Template</div>',
|
|
|
|
assertDom: true,
|
|
|
|
expectedData: [
|
|
|
|
{
|
|
|
|
type: 'mwTransclusionBlock',
|
|
|
|
attributes: {
|
|
|
|
mw: {}
|
|
|
|
},
|
|
|
|
originalDomElements: $( '<div typeof="mw:Transclusion" about="#mwt40000000">Template</div>' ).toArray()
|
|
|
|
},
|
|
|
|
{ type: '/mwTransclusionBlock' },
|
|
|
|
{ type: 'internalList' },
|
|
|
|
{ type: '/internalList' }
|
|
|
|
]
|
|
|
|
},
|
2015-08-07 18:16:09 +00:00
|
|
|
{
|
2016-09-22 03:57:58 +00:00
|
|
|
msg: 'Headings, only RESTBase IDs stripped',
|
2015-08-07 18:16:09 +00:00
|
|
|
pasteString: '==heading==',
|
|
|
|
pasteType: 'text/plain',
|
2016-09-22 03:57:58 +00:00
|
|
|
parsoidResponse: '<h2 id="mwAB">foo</h2><h2 id="mw-meaningful-id">bar</h2>',
|
2015-08-07 18:16:09 +00:00
|
|
|
annotations: [],
|
2016-09-22 03:57:58 +00:00
|
|
|
assertDom: true,
|
2015-08-07 18:16:09 +00:00
|
|
|
expectedData: [
|
2018-01-22 18:59:45 +00:00
|
|
|
{ type: 'mwHeading', attributes: { level: 2 }, internal: { changesSinceLoad: 0, metaItems: [] }, originalDomElements: $( '<h2>foo</h2>' ).toArray() },
|
2016-09-22 03:57:58 +00:00
|
|
|
'f', 'o', 'o',
|
|
|
|
{ type: '/mwHeading' },
|
2018-01-22 18:59:45 +00:00
|
|
|
{ type: 'mwHeading', attributes: { level: 2 }, internal: { changesSinceLoad: 0, metaItems: [] }, originalDomElements: $( '<h2 id="mw-meaningful-id">bar</h2>' ).toArray() },
|
2016-09-22 03:57:58 +00:00
|
|
|
'b', 'a', 'r',
|
|
|
|
{ type: '/mwHeading' },
|
2015-08-07 18:16:09 +00:00
|
|
|
{ type: 'internalList' },
|
|
|
|
{ type: '/internalList' }
|
|
|
|
]
|
|
|
|
},
|
2018-01-22 20:34:06 +00:00
|
|
|
{
|
|
|
|
msg: 'Headings, parsoid fallback ids don\'t interfere with whitespace stripping',
|
|
|
|
pasteString: '== Tudnivalók ==',
|
|
|
|
pasteType: 'text/plain',
|
|
|
|
parsoidResponse: '<h2 id="Tudnivalók"><span id="Tudnival.C3.B3k" typeof="mw:FallbackId"></span> Tudnivalók </h2>',
|
|
|
|
annotations: [],
|
|
|
|
assertDom: true,
|
|
|
|
expectedData: [
|
2018-01-22 18:59:45 +00:00
|
|
|
{ type: 'mwHeading', attributes: { level: 2 }, internal: { changesSinceLoad: 0, metaItems: [] }, originalDomElements: $( '<h2 id="Tudnivalók"> Tudnivalók </h2>' ).toArray() },
|
2018-01-22 20:34:06 +00:00
|
|
|
'T', 'u', 'd', 'n', 'i', 'v', 'a', 'l', 'ó', 'k',
|
|
|
|
{ type: '/mwHeading' },
|
|
|
|
{ type: 'internalList' },
|
|
|
|
{ type: '/internalList' }
|
|
|
|
]
|
|
|
|
},
|
2015-08-07 18:16:09 +00:00
|
|
|
{
|
|
|
|
msg: 'Magic link (RFC)',
|
|
|
|
pasteString: 'RFC 1234',
|
|
|
|
pasteType: 'text/plain',
|
2017-03-07 15:44:50 +00:00
|
|
|
parsoidResponse: false,
|
2015-08-25 05:48:16 +00:00
|
|
|
annotations: [],
|
2015-08-07 18:16:09 +00:00
|
|
|
expectedData: [
|
2015-08-25 05:48:16 +00:00
|
|
|
{
|
|
|
|
type: 'link/mwMagic',
|
|
|
|
attributes: {
|
2017-03-07 15:44:50 +00:00
|
|
|
content: 'RFC 1234'
|
2015-08-25 05:48:16 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: '/link/mwMagic'
|
2017-03-07 15:44:50 +00:00
|
|
|
}
|
2015-08-07 18:16:09 +00:00
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
msg: 'Magic link (PMID)',
|
|
|
|
pasteString: 'PMID 1234',
|
|
|
|
pasteType: 'text/plain',
|
2017-03-07 15:44:50 +00:00
|
|
|
parsoidResponse: false,
|
2015-08-25 05:48:16 +00:00
|
|
|
annotations: [],
|
2015-08-07 18:16:09 +00:00
|
|
|
expectedData: [
|
2015-08-25 05:48:16 +00:00
|
|
|
{
|
|
|
|
type: 'link/mwMagic',
|
|
|
|
attributes: {
|
2017-03-07 15:44:50 +00:00
|
|
|
content: 'PMID 1234'
|
2015-08-25 05:48:16 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: '/link/mwMagic'
|
2017-03-07 15:44:50 +00:00
|
|
|
}
|
2015-08-07 18:16:09 +00:00
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
msg: 'Magic link (ISBN)',
|
|
|
|
pasteString: 'ISBN 123456789X',
|
|
|
|
pasteType: 'text/plain',
|
2017-03-07 15:44:50 +00:00
|
|
|
parsoidResponse: false,
|
2015-08-25 05:48:16 +00:00
|
|
|
annotations: [],
|
2015-08-07 18:16:09 +00:00
|
|
|
expectedData: [
|
2015-08-25 05:48:16 +00:00
|
|
|
{
|
|
|
|
type: 'link/mwMagic',
|
|
|
|
attributes: {
|
2017-03-07 15:44:50 +00:00
|
|
|
content: 'ISBN 123456789X'
|
2015-08-25 05:48:16 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: '/link/mwMagic'
|
2017-03-07 15:44:50 +00:00
|
|
|
}
|
2015-08-07 18:16:09 +00:00
|
|
|
]
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
|
|
|
for ( i = 0; i < cases.length; i++ ) {
|
2017-04-20 16:07:46 +00:00
|
|
|
ve.test.utils.runWikitextStringHandlerTest(
|
2016-09-22 03:57:58 +00:00
|
|
|
assert, this.server, cases[ i ].pasteString, cases[ i ].pasteType, cases[ i ].parsoidResponse,
|
|
|
|
cases[ i ].expectedData, cases[ i ].annotations, cases[ i ].assertDom, cases[ i ].msg
|
|
|
|
);
|
2015-08-07 18:16:09 +00:00
|
|
|
}
|
|
|
|
} );
|