Debian Codegraph @

extensions/VisualEditor/modules/ve-mw/tests/ui/datatransferhandlers/ve.ui.MWWikitextStringTransferHandler.test.js

raw symbols SCIP
1 /*!
2 * VisualEditor UserInterface MWWikitextStringTransferHandler tests.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 QUnit.module( 've.ui.MWWikitextStringTransferHandler', ve.test.utils.newMwEnvironment( {
8 beforeEach() {
9 // Mock XHR for mw.Api()
10 this.server = this.sandbox.useFakeServer();
11 // Random number, chosen by a fair dice roll.
12 // Used to make #mwt ID deterministic
13 this.randomStub = sinon.stub( Math, 'random' ).returns( 0.04 );
14 },
15 afterEach() {
16 this.randomStub.restore();
17 }
18 } ) );
19
20 ve.test.utils.runWikitextStringHandlerTest = ( assert, server, string, mimeType, expectedResponse, expectedData, annotations, assertDom, base, msg ) => {
21 const done = assert.async(),
22 item = ve.ui.DataTransferItem.static.newFromString( string, mimeType ),
23 doc = ve.dm.Document.static.newBlankDocument(),
24 mockSurface = {
25 getModel: () => ( {
26 getDocument: () => doc
27 } ),
28 createProgress: () => ve.createDeferred().promise()
29 };
30
31 ve.fixBase( doc.getHtmlDocument(), doc.getHtmlDocument(), base );
32
33 // Preprocess the expectedData array
34 for ( let i = 0; i < expectedData.length; i++ ) {
35 if ( Array.isArray( expectedData[ i ] ) ) {
36 for ( let j = 0; j < expectedData[ i ][ 1 ].length; j++ ) {
37 if ( typeof expectedData[ i ][ 1 ][ j ] === 'number' ) {
38 expectedData[ i ][ 1 ][ j ] = annotations[ expectedData[ i ][ 1 ][ j ] ];
39 }
40 }
41 }
42 }
43
44 // Check we match the wikitext string handler
45 const name = ve.ui.dataTransferHandlerFactory.getHandlerNameForItem( item );
46 assert.strictEqual( name, 'wikitextString', msg + ': triggers match function' );
47
48 // Invoke the handler
49 const handler = ve.ui.dataTransferHandlerFactory.create( 'wikitextString', mockSurface, item );
50
51 handler.getInsertableData().done( ( docOrData ) => {
52 let actualData, store;
53 if ( docOrData instanceof ve.dm.Document ) {
54 actualData = docOrData.getData();
55 store = docOrData.getStore();
56 } else {
57 actualData = docOrData;
58 store = new ve.dm.HashValueStore();
59 }
60 ve.dm.example.postprocessAnnotations( actualData, store );
61 if ( assertDom ) {
62 assert.equalLinearDataWithDom( store, actualData, expectedData, msg + ': data match (with DOM)' );
63 } else {
64 assert.equalLinearData( actualData, expectedData, msg + ': data match' );
65 }
66 done();
67 } );
68
69 if ( server && expectedResponse ) {
70 server.respond( [ 200, { 'Content-Type': 'application/json' }, JSON.stringify( {
71 visualeditor: {
72 result: 'success',
73 content: expectedResponse
74 }
75 } ) ] );
76 }
77 };
78
79 QUnit.test.each( 'convert', [
80 {
81 msg: 'Simple link',
82 // Put link in the middle of text to verify that the
83 // start-of-line and end-of-line anchors on the heading
84 // identification pattern don't affect link identification
85 pasteString: 'some [[Foo]] text',
86 pasteType: 'text/plain',
87 parsoidResponse: '<p>some <a rel="mw:WikiLink" href="./Foo" title="Foo">Foo</a> text</p>',
88 annotations: [ {
89 type: 'link/mwInternal',
90 attributes: {
91 lookupTitle: 'Foo',
92 normalizedTitle: 'Foo',
93 title: 'Foo'
94 }
95 } ],
96 expectedData: [
97 { type: 'paragraph' },
98 ...'some ',
99 [ 'F', [ 0 ] ],
100 [ 'o', [ 0 ] ],
101 [ 'o', [ 0 ] ],
102 ...' text',
103 { type: '/paragraph' },
104 { type: 'internalList' },
105 { type: '/internalList' }
106 ]
107 },
108 {
109 msg: 'Simple link with no p-wrapping',
110 pasteString: '*[[Foo]]',
111 pasteType: 'text/plain',
112 parsoidResponse: '<ul><li><a rel="mw:WikiLink" href="./Foo" title="Foo">Foo</a></li></ul>',
113 annotations: [ {
114 type: 'link/mwInternal',
115 attributes: {
116 lookupTitle: 'Foo',
117 normalizedTitle: 'Foo',
118 title: 'Foo'
119 }
120 } ],
121 expectedData: [
122 {
123 type: 'list',
124 attributes: { style: 'bullet' }
125 },
126 { type: 'listItem' },
127 {
128 type: 'paragraph',
129 internal: { generated: 'wrapper' }
130 },
131 [ 'F', [ 0 ] ],
132 [ 'o', [ 0 ] ],
133 [ 'o', [ 0 ] ],
134 { type: '/paragraph' },
135 { type: '/listItem' },
136 { type: '/list' },
137 { type: 'internalList' },
138 { type: '/internalList' }
139 ]
140 },
141 {
142 msg: 'Simple template',
143 pasteString: '{{Template}}',
144 pasteType: 'text/plain',
145 parsoidResponse: '<div typeof="mw:Transclusion" about="#mwt1">Template</div>',
146 assertDom: true,
147 expectedData: [
148 {
149 type: 'mwTransclusionBlock',
150 attributes: {
151 mw: {}
152 },
153 originalDomElements: $.parseHTML( '<div typeof="mw:Transclusion" about="#mwt40000000">Template</div>' )
154 },
155 { type: '/mwTransclusionBlock' },
156 { type: 'internalList' },
157 { type: '/internalList' }
158 ]
159 },
160 {
161 msg: 'Headings, only RESTBase IDs stripped',
162 pasteString: '==heading==',
163 pasteType: 'text/plain',
164 parsoidResponse: '<h2 id="mwAB">foo</h2><h2 id="mw-meaningful-id">bar</h2>',
165 annotations: [],
166 assertDom: true,
167 expectedData: [
168 { type: 'mwHeading', attributes: { level: 2 }, originalDomElements: $.parseHTML( '<h2>foo</h2>' ) },
169 ...'foo',
170 { type: '/mwHeading' },
171 { type: 'mwHeading', attributes: { level: 2 }, originalDomElements: $.parseHTML( '<h2 id="mw-meaningful-id">bar</h2>' ) },
172 ...'bar',
173 { type: '/mwHeading' },
174 { type: 'internalList' },
175 { type: '/internalList' }
176 ]
177 },
178 {
179 msg: 'Headings, parsoid fallback ids don\'t interfere with whitespace stripping',
180 pasteString: '== Tudnivalók ==',
181 pasteType: 'text/plain',
182 parsoidResponse: '<h2 id="Tudnivalók"><span id="Tudnival.C3.B3k" typeof="mw:FallbackId"></span> Tudnivalók </h2>',
183 annotations: [],
184 assertDom: true,
185 expectedData: [
186 { type: 'mwHeading', attributes: { level: 2 }, originalDomElements: $.parseHTML( '<h2 id="Tudnivalók"> Tudnivalók </h2>' ) },
187 ...'Tudnivalók',
188 { type: '/mwHeading' },
189 { type: 'internalList' },
190 { type: '/internalList' }
191 ]
192 },
193 {
194 msg: 'Magic link (RFC)',
195 pasteString: 'RFC 1234',
196 pasteType: 'text/plain',
197 parsoidResponse: false,
198 annotations: [],
199 expectedData: [
200 {
201 type: 'link/mwMagic',
202 attributes: {
203 content: 'RFC 1234'
204 }
205 },
206 {
207 type: '/link/mwMagic'
208 }
209 ]
210 },
211 {
212 msg: 'Magic link (PMID)',
213 pasteString: 'PMID 1234',
214 pasteType: 'text/plain',
215 parsoidResponse: false,
216 annotations: [],
217 expectedData: [
218 {
219 type: 'link/mwMagic',
220 attributes: {
221 content: 'PMID 1234'
222 }
223 },
224 {
225 type: '/link/mwMagic'
226 }
227 ]
228 },
229 {
230 msg: 'Magic link (ISBN)',
231 pasteString: 'ISBN 123456789X',
232 pasteType: 'text/plain',
233 parsoidResponse: false,
234 annotations: [],
235 expectedData: [
236 {
237 type: 'link/mwMagic',
238 attributes: {
239 content: 'ISBN 123456789X'
240 }
241 },
242 {
243 type: '/link/mwMagic'
244 }
245 ]
246 }
247 ], function ( assert, caseItem ) {
248 mw.config.set( {
249 wgArticlePath: '/wiki/$1'
250 } );
251
252 ve.test.utils.runWikitextStringHandlerTest(
253 assert, this.server, caseItem.pasteString, caseItem.pasteType, caseItem.parsoidResponse,
254 caseItem.expectedData, caseItem.annotations, caseItem.assertDom, ve.dm.mwExample.baseUri,
255 caseItem.msg
256 );
257 } );