2015-08-07 18:16:09 +00:00
|
|
|
/*!
|
|
|
|
* VisualEditor UserInterface MWWikitextStringTransferHandler tests.
|
|
|
|
*
|
2023-12-01 16:06:11 +00:00
|
|
|
* @copyright See AUTHORS.txt
|
2015-08-07 18:16:09 +00:00
|
|
|
*/
|
2017-04-29 11:37:53 +00:00
|
|
|
|
2022-03-04 13:48:14 +00:00
|
|
|
QUnit.module( 've.ui.MWWikitextStringTransferHandler', ve.test.utils.newMwEnvironment( {
|
2021-04-30 09:33:22 +00:00
|
|
|
beforeEach() {
|
2015-08-07 18:16:09 +00:00
|
|
|
// 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
|
|
|
},
|
2021-04-30 09:33:22 +00:00
|
|
|
afterEach() {
|
2018-09-12 13:52:54 +00:00
|
|
|
this.randomStub.restore();
|
|
|
|
}
|
2015-08-07 18:16:09 +00:00
|
|
|
} ) );
|
|
|
|
|
2023-01-21 17:00:26 +00:00
|
|
|
ve.test.utils.runWikitextStringHandlerTest = ( assert, server, string, mimeType, expectedResponse, expectedData, annotations, assertDom, base, msg ) => {
|
2021-04-29 14:42:18 +00:00
|
|
|
const done = assert.async(),
|
2015-08-07 18:16:09 +00:00
|
|
|
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 = {
|
2024-06-04 03:39:48 +00:00
|
|
|
getModel: () => ( {
|
2021-04-30 09:33:22 +00:00
|
|
|
getDocument: () => doc
|
2024-06-04 03:39:48 +00:00
|
|
|
} ),
|
2021-04-30 09:33:22 +00:00
|
|
|
createProgress: () => ve.createDeferred().promise()
|
2015-08-07 18:16:09 +00:00
|
|
|
};
|
|
|
|
|
2023-01-21 17:00:26 +00:00
|
|
|
ve.fixBase( doc.getHtmlDocument(), doc.getHtmlDocument(), base );
|
|
|
|
|
2015-08-07 18:16:09 +00:00
|
|
|
// Preprocess the expectedData array
|
2021-04-29 14:42:18 +00:00
|
|
|
for ( let i = 0; i < expectedData.length; i++ ) {
|
2015-08-07 18:16:09 +00:00
|
|
|
if ( Array.isArray( expectedData[ i ] ) ) {
|
2021-04-29 14:42:18 +00:00
|
|
|
for ( let j = 0; j < expectedData[ i ][ 1 ].length; j++ ) {
|
2015-08-07 18:16:09 +00:00
|
|
|
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
|
2021-04-29 14:42:18 +00:00
|
|
|
const name = ve.ui.dataTransferHandlerFactory.getHandlerNameForItem( item );
|
2015-08-23 10:12:44 +00:00
|
|
|
assert.strictEqual( name, 'wikitextString', msg + ': triggers match function' );
|
|
|
|
|
2015-08-07 18:16:09 +00:00
|
|
|
// Invoke the handler
|
2021-04-29 14:42:18 +00:00
|
|
|
const handler = ve.ui.dataTransferHandlerFactory.create( 'wikitextString', mockSurface, item );
|
2015-08-07 18:16:09 +00:00
|
|
|
|
2021-04-30 09:33:22 +00:00
|
|
|
handler.getInsertableData().done( ( docOrData ) => {
|
2021-04-29 14:42:18 +00:00
|
|
|
let actualData, store;
|
2017-03-07 15:44:50 +00:00
|
|
|
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',
|
2023-01-22 11:00:24 +00:00
|
|
|
content: expectedResponse
|
2015-08-07 18:16:09 +00:00
|
|
|
}
|
|
|
|
} ) ] );
|
|
|
|
}
|
2017-04-20 16:07:46 +00:00
|
|
|
};
|
2015-08-07 18:16:09 +00:00
|
|
|
|
2024-05-01 17:25:33 +00:00
|
|
|
QUnit.test.each( 'convert', [
|
|
|
|
{
|
|
|
|
msg: 'Simple link',
|
|
|
|
// Put link in the middle of text to verify that the
|
|
|
|
// start-of-line and end-of-line anchors on the heading
|
|
|
|
// identification pattern don't affect link identification
|
|
|
|
pasteString: 'some [[Foo]] text',
|
|
|
|
pasteType: 'text/plain',
|
|
|
|
parsoidResponse: '<p>some <a rel="mw:WikiLink" href="./Foo" title="Foo">Foo</a> text</p>',
|
|
|
|
annotations: [ {
|
|
|
|
type: 'link/mwInternal',
|
|
|
|
attributes: {
|
|
|
|
lookupTitle: 'Foo',
|
|
|
|
normalizedTitle: 'Foo',
|
|
|
|
title: 'Foo'
|
|
|
|
}
|
|
|
|
} ],
|
|
|
|
expectedData: [
|
|
|
|
{ type: 'paragraph' },
|
2024-05-24 21:23:42 +00:00
|
|
|
...'some ',
|
2024-05-01 17:25:33 +00:00
|
|
|
[ 'F', [ 0 ] ],
|
|
|
|
[ 'o', [ 0 ] ],
|
|
|
|
[ 'o', [ 0 ] ],
|
2024-05-24 21:23:42 +00:00
|
|
|
...' text',
|
2024-05-01 17:25:33 +00:00
|
|
|
{ type: '/paragraph' },
|
|
|
|
{ type: 'internalList' },
|
|
|
|
{ type: '/internalList' }
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
msg: 'Simple link with no p-wrapping',
|
|
|
|
pasteString: '*[[Foo]]',
|
|
|
|
pasteType: 'text/plain',
|
|
|
|
parsoidResponse: '<ul><li><a rel="mw:WikiLink" href="./Foo" title="Foo">Foo</a></li></ul>',
|
|
|
|
annotations: [ {
|
|
|
|
type: 'link/mwInternal',
|
|
|
|
attributes: {
|
|
|
|
lookupTitle: 'Foo',
|
|
|
|
normalizedTitle: 'Foo',
|
|
|
|
title: 'Foo'
|
|
|
|
}
|
|
|
|
} ],
|
|
|
|
expectedData: [
|
2015-08-07 18:16:09 +00:00
|
|
|
{
|
2024-05-01 17:25:33 +00:00
|
|
|
type: 'list',
|
|
|
|
attributes: { style: 'bullet' }
|
2015-08-07 18:16:09 +00:00
|
|
|
},
|
2024-05-01 17:25:33 +00:00
|
|
|
{ type: 'listItem' },
|
2015-08-07 18:16:09 +00:00
|
|
|
{
|
2024-05-01 17:25:33 +00:00
|
|
|
type: 'paragraph',
|
|
|
|
internal: { generated: 'wrapper' }
|
2015-08-07 18:16:09 +00:00
|
|
|
},
|
2024-05-01 17:25:33 +00:00
|
|
|
[ 'F', [ 0 ] ],
|
|
|
|
[ 'o', [ 0 ] ],
|
|
|
|
[ 'o', [ 0 ] ],
|
|
|
|
{ type: '/paragraph' },
|
|
|
|
{ type: '/listItem' },
|
|
|
|
{ type: '/list' },
|
|
|
|
{ type: 'internalList' },
|
|
|
|
{ type: '/internalList' }
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
msg: 'Simple template',
|
|
|
|
pasteString: '{{Template}}',
|
|
|
|
pasteType: 'text/plain',
|
|
|
|
parsoidResponse: '<div typeof="mw:Transclusion" about="#mwt1">Template</div>',
|
|
|
|
assertDom: true,
|
|
|
|
expectedData: [
|
2018-09-12 13:52:54 +00:00
|
|
|
{
|
2024-05-01 17:25:33 +00:00
|
|
|
type: 'mwTransclusionBlock',
|
|
|
|
attributes: {
|
|
|
|
mw: {}
|
|
|
|
},
|
|
|
|
originalDomElements: $.parseHTML( '<div typeof="mw:Transclusion" about="#mwt40000000">Template</div>' )
|
2018-09-12 13:52:54 +00:00
|
|
|
},
|
2024-05-01 17:25:33 +00:00
|
|
|
{ type: '/mwTransclusionBlock' },
|
|
|
|
{ type: 'internalList' },
|
|
|
|
{ type: '/internalList' }
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
msg: 'Headings, only RESTBase IDs stripped',
|
|
|
|
pasteString: '==heading==',
|
|
|
|
pasteType: 'text/plain',
|
|
|
|
parsoidResponse: '<h2 id="mwAB">foo</h2><h2 id="mw-meaningful-id">bar</h2>',
|
|
|
|
annotations: [],
|
|
|
|
assertDom: true,
|
|
|
|
expectedData: [
|
|
|
|
{ type: 'mwHeading', attributes: { level: 2 }, originalDomElements: $.parseHTML( '<h2>foo</h2>' ) },
|
2024-05-24 21:23:42 +00:00
|
|
|
...'foo',
|
2024-05-01 17:25:33 +00:00
|
|
|
{ type: '/mwHeading' },
|
|
|
|
{ type: 'mwHeading', attributes: { level: 2 }, originalDomElements: $.parseHTML( '<h2 id="mw-meaningful-id">bar</h2>' ) },
|
2024-05-24 21:23:42 +00:00
|
|
|
...'bar',
|
2024-05-01 17:25:33 +00:00
|
|
|
{ type: '/mwHeading' },
|
|
|
|
{ type: 'internalList' },
|
|
|
|
{ type: '/internalList' }
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
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: [
|
|
|
|
{ type: 'mwHeading', attributes: { level: 2 }, originalDomElements: $.parseHTML( '<h2 id="Tudnivalók"> Tudnivalók </h2>' ) },
|
2024-05-24 21:23:42 +00:00
|
|
|
...'Tudnivalók',
|
2024-05-01 17:25:33 +00:00
|
|
|
{ type: '/mwHeading' },
|
|
|
|
{ type: 'internalList' },
|
|
|
|
{ type: '/internalList' }
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
msg: 'Magic link (RFC)',
|
|
|
|
pasteString: 'RFC 1234',
|
|
|
|
pasteType: 'text/plain',
|
|
|
|
parsoidResponse: false,
|
|
|
|
annotations: [],
|
|
|
|
expectedData: [
|
2015-08-07 18:16:09 +00:00
|
|
|
{
|
2024-05-01 17:25:33 +00:00
|
|
|
type: 'link/mwMagic',
|
|
|
|
attributes: {
|
|
|
|
content: 'RFC 1234'
|
|
|
|
}
|
2015-08-07 18:16:09 +00:00
|
|
|
},
|
2018-01-22 20:34:06 +00:00
|
|
|
{
|
2024-05-01 17:25:33 +00:00
|
|
|
type: '/link/mwMagic'
|
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
msg: 'Magic link (PMID)',
|
|
|
|
pasteString: 'PMID 1234',
|
|
|
|
pasteType: 'text/plain',
|
|
|
|
parsoidResponse: false,
|
|
|
|
annotations: [],
|
|
|
|
expectedData: [
|
2015-08-07 18:16:09 +00:00
|
|
|
{
|
2024-05-01 17:25:33 +00:00
|
|
|
type: 'link/mwMagic',
|
|
|
|
attributes: {
|
|
|
|
content: 'PMID 1234'
|
|
|
|
}
|
2015-08-07 18:16:09 +00:00
|
|
|
},
|
|
|
|
{
|
2024-05-01 17:25:33 +00:00
|
|
|
type: '/link/mwMagic'
|
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
msg: 'Magic link (ISBN)',
|
|
|
|
pasteString: 'ISBN 123456789X',
|
|
|
|
pasteType: 'text/plain',
|
|
|
|
parsoidResponse: false,
|
|
|
|
annotations: [],
|
|
|
|
expectedData: [
|
|
|
|
{
|
|
|
|
type: 'link/mwMagic',
|
|
|
|
attributes: {
|
|
|
|
content: 'ISBN 123456789X'
|
|
|
|
}
|
2015-08-07 18:16:09 +00:00
|
|
|
},
|
|
|
|
{
|
2024-05-01 17:25:33 +00:00
|
|
|
type: '/link/mwMagic'
|
2015-08-07 18:16:09 +00:00
|
|
|
}
|
2024-05-01 17:25:33 +00:00
|
|
|
]
|
|
|
|
}
|
|
|
|
], function ( assert, caseItem ) {
|
2023-01-30 14:57:56 +00:00
|
|
|
mw.config.set( {
|
|
|
|
wgArticlePath: '/wiki/$1'
|
|
|
|
} );
|
2024-05-01 17:25:33 +00:00
|
|
|
|
|
|
|
ve.test.utils.runWikitextStringHandlerTest(
|
|
|
|
assert, this.server, caseItem.pasteString, caseItem.pasteType, caseItem.parsoidResponse,
|
|
|
|
caseItem.expectedData, caseItem.annotations, caseItem.assertDom, ve.dm.mwExample.baseUri,
|
|
|
|
caseItem.msg
|
|
|
|
);
|
2015-08-07 18:16:09 +00:00
|
|
|
} );
|