Debian Codegraph @

extensions/VisualEditor/lib/ve/tests/ce/ve.ce.ContentBranchNode.test.js

raw symbols SCIP
1 /*!
2 * VisualEditor ContentEditable ContentBranchNode tests.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 QUnit.module( 've.ce.ContentBranchNode' );
8
9 /* Tests */
10
11 QUnit.test( 'getRenderedContents', ( assert ) => {
12 const cases = [
13 {
14 msg: 'Plain text without annotations',
15 data: [
16 { type: 'paragraph' },
17 ...'abc',
18 { type: '/paragraph' }
19 ],
20 html: 'abc'
21 },
22 {
23 msg: 'Bold text',
24 data: [
25 { type: 'paragraph' },
26 [ 'a', [ { type: 'textStyle/bold' } ] ],
27 [ 'b', [ { type: 'textStyle/bold' } ] ],
28 [ 'c', [ { type: 'textStyle/bold' } ] ],
29 { type: '/paragraph' }
30 ],
31 html: '<b>abc</b>'
32 },
33 {
34 msg: 'Italic text',
35 data: [
36 { type: 'paragraph' },
37 [ 'a', [ { type: 'textStyle/italic' } ] ],
38 [ 'b', [ { type: 'textStyle/italic' } ] ],
39 [ 'c', [ { type: 'textStyle/italic' } ] ],
40 { type: '/paragraph' }
41 ],
42 html: '<i>abc</i>'
43 },
44 {
45 msg: 'Underline text',
46 data: [
47 { type: 'paragraph' },
48 [ 'a', [ { type: 'textStyle/underline' } ] ],
49 [ 'b', [ { type: 'textStyle/underline' } ] ],
50 [ 'c', [ { type: 'textStyle/underline' } ] ],
51 { type: '/paragraph' }
52 ],
53 html: '<u>abc</u>'
54 },
55 {
56 msg: 'Strikethrough text',
57 data: [
58 { type: 'paragraph' },
59 [ 'a', [ { type: 'textStyle/strikethrough' } ] ],
60 [ 'b', [ { type: 'textStyle/strikethrough' } ] ],
61 [ 'c', [ { type: 'textStyle/strikethrough' } ] ],
62 { type: '/paragraph' }
63 ],
64 html: '<s>abc</s>'
65 },
66 {
67 msg: 'Deleted text',
68 data: [
69 { type: 'paragraph' },
70 [ 'a', [ { type: 'textStyle/strikethrough', attributes: { nodeName: 'del' } } ] ],
71 [ 'b', [ { type: 'textStyle/strikethrough', attributes: { nodeName: 'del' } } ] ],
72 [ 'c', [ { type: 'textStyle/strikethrough', attributes: { nodeName: 'del' } } ] ],
73 { type: '/paragraph' }
74 ],
75 html: '<del>abc</del>'
76 },
77 {
78 msg: 'Small text',
79 data: [
80 { type: 'paragraph' },
81 [ 'a', [ { type: 'textStyle/small' } ] ],
82 [ 'b', [ { type: 'textStyle/small' } ] ],
83 [ 'c', [ { type: 'textStyle/small' } ] ],
84 { type: '/paragraph' }
85 ],
86 html: '<small>abc</small>'
87 },
88 {
89 msg: 'Big text',
90 data: [
91 { type: 'paragraph' },
92 [ 'a', [ { type: 'textStyle/big' } ] ],
93 [ 'b', [ { type: 'textStyle/big' } ] ],
94 [ 'c', [ { type: 'textStyle/big' } ] ],
95 { type: '/paragraph' }
96 ],
97 html: '<big>abc</big>'
98 },
99 {
100 msg: 'Strong text',
101 data: [
102 { type: 'paragraph' },
103 [ 'a', [ { type: 'textStyle/bold', attributes: { nodeName: 'strong' } } ] ],
104 [ 'b', [ { type: 'textStyle/bold', attributes: { nodeName: 'strong' } } ] ],
105 [ 'c', [ { type: 'textStyle/bold', attributes: { nodeName: 'strong' } } ] ],
106 { type: '/paragraph' }
107 ],
108 html: '<strong>abc</strong>'
109 },
110 {
111 msg: 'Emphasized text',
112 data: [
113 { type: 'paragraph' },
114 [ 'a', [ { type: 'textStyle/italic', attributes: { nodeName: 'em' } } ] ],
115 [ 'b', [ { type: 'textStyle/italic', attributes: { nodeName: 'em' } } ] ],
116 [ 'c', [ { type: 'textStyle/italic', attributes: { nodeName: 'em' } } ] ],
117 { type: '/paragraph' }
118 ],
119 html: '<em>abc</em>'
120 },
121 {
122 msg: 'Superscript text',
123 data: [
124 { type: 'paragraph' },
125 [ 'a', [ { type: 'textStyle/superscript' } ] ],
126 [ 'b', [ { type: 'textStyle/superscript' } ] ],
127 [ 'c', [ { type: 'textStyle/superscript' } ] ],
128 { type: '/paragraph' }
129 ],
130 html: '<sup>abc</sup>'
131 },
132 {
133 msg: 'Subscript text',
134 data: [
135 { type: 'paragraph' },
136 [ 'a', [ { type: 'textStyle/subscript' } ] ],
137 [ 'b', [ { type: 'textStyle/subscript' } ] ],
138 [ 'c', [ { type: 'textStyle/subscript' } ] ],
139 { type: '/paragraph' }
140 ],
141 html: '<sub>abc</sub>'
142 },
143 {
144 msg: 'Code text',
145 data: [
146 { type: 'paragraph' },
147 [ 'a', [ { type: 'textStyle/code' } ] ],
148 [ 'b', [ { type: 'textStyle/code' } ] ],
149 [ 'c', [ { type: 'textStyle/code' } ] ],
150 { type: '/paragraph' }
151 ],
152 html: '<code>abc</code>'
153 },
154 {
155 msg: 'Teletype text',
156 data: [
157 { type: 'paragraph' },
158 [ 'a', [ { type: 'textStyle/code', attributes: { nodeName: 'tt' } } ] ],
159 [ 'b', [ { type: 'textStyle/code', attributes: { nodeName: 'tt' } } ] ],
160 [ 'c', [ { type: 'textStyle/code', attributes: { nodeName: 'tt' } } ] ],
161 { type: '/paragraph' }
162 ],
163 html: '<tt>abc</tt>'
164 },
165 {
166 msg: 'Bold character, plain character, italic character',
167 data: [
168 { type: 'paragraph' },
169 [ 'a', [ { type: 'textStyle/bold' } ] ],
170 'b',
171 [ 'c', [ { type: 'textStyle/italic' } ] ],
172 { type: '/paragraph' }
173 ],
174 html: '<b>a</b>b<i>c</i>'
175 },
176 {
177 msg: 'Comparable annotations: strong, bold',
178 data: [
179 { type: 'paragraph' },
180 [ 'a', [ { type: 'textStyle/bold', attributes: { nodeName: 'strong' } } ] ],
181 [ 'b', [ { type: 'textStyle/bold' } ] ],
182 { type: '/paragraph' }
183 ],
184 html: '<strong>ab</strong>'
185 },
186 {
187 msg: 'Bold, italic and underlined text (same order)',
188 data: [
189 { type: 'paragraph' },
190 [ 'a', [
191 { type: 'textStyle/bold' },
192 { type: 'textStyle/italic' },
193 { type: 'textStyle/underline' }
194 ] ],
195 [ 'b', [
196 { type: 'textStyle/bold' },
197 { type: 'textStyle/italic' },
198 { type: 'textStyle/underline' }
199 ] ],
200 [ 'c', [
201 { type: 'textStyle/bold' },
202 { type: 'textStyle/italic' },
203 { type: 'textStyle/underline' }
204 ] ],
205 { type: '/paragraph' }
206 ],
207 html: '<b><i><u>abc</u></i></b>'
208 },
209 {
210 msg: 'Varying order in consecutive range doesn\'t affect rendering',
211 data: [
212 { type: 'paragraph' },
213 [ 'a', [
214 { type: 'textStyle/bold' },
215 { type: 'textStyle/italic' },
216 { type: 'textStyle/underline' }
217 ] ],
218 [ 'b', [
219 { type: 'textStyle/italic' },
220 { type: 'textStyle/underline' },
221 { type: 'textStyle/bold' }
222 ] ],
223 [ 'c', [
224 { type: 'textStyle/underline' },
225 { type: 'textStyle/bold' },
226 { type: 'textStyle/italic' }
227 ] ],
228 { type: '/paragraph' }
229 ],
230 html: '<b><i><u>abc</u></i></b>'
231 },
232 {
233 msg: 'Varying order in non-consecutive range does affect rendering',
234 data: [
235 { type: 'paragraph' },
236 [ 'a', [
237 { type: 'textStyle/bold' },
238 { type: 'textStyle/italic' },
239 { type: 'textStyle/underline' }
240 ] ],
241 'b',
242 [ 'c', [
243 { type: 'textStyle/underline' },
244 { type: 'textStyle/bold' },
245 { type: 'textStyle/italic' }
246 ] ],
247 { type: '/paragraph' }
248 ],
249 html: '<b><i><u>a</u></i></b>b<u><b><i>c</i></b></u>'
250 },
251 {
252 msg: 'Text annotated in varying order, surrounded by plain text',
253 data: [
254 { type: 'paragraph' },
255 ...'abc',
256 [ 'd', [
257 { type: 'textStyle/bold' },
258 { type: 'textStyle/italic' },
259 { type: 'textStyle/underline' }
260 ] ],
261 [ 'e', [
262 { type: 'textStyle/italic' },
263 { type: 'textStyle/underline' },
264 { type: 'textStyle/bold' }
265 ] ],
266 [ 'f', [
267 { type: 'textStyle/underline' },
268 { type: 'textStyle/bold' },
269 { type: 'textStyle/italic' }
270 ] ],
271 ...'ghi',
272 { type: '/paragraph' }
273 ],
274 html: 'abc<b><i><u>def</u></i></b>ghi'
275 },
276 {
277 msg: 'Out-of-order closings do not produce misnested tags',
278 data: [
279 { type: 'paragraph' },
280 ...'abc',
281 [ 'd', [
282 { type: 'textStyle/bold' },
283 { type: 'textStyle/italic' },
284 { type: 'textStyle/underline' }
285 ] ],
286 [ 'e', [
287 { type: 'textStyle/italic' },
288 { type: 'textStyle/underline' }
289 ] ],
290 [ 'f', [
291 { type: 'textStyle/underline' },
292 { type: 'textStyle/bold' },
293 { type: 'textStyle/italic' }
294 ] ],
295 ...'ghi',
296 { type: '/paragraph' }
297 ],
298 html: 'abc<b><i><u>d</u></i></b><i><u>e<b>f</b></u></i>ghi'
299 },
300 {
301 msg: 'Additional openings are added inline, even when out of order',
302 data: [
303 { type: 'paragraph' },
304 ...'abc',
305 [ 'd', [
306 { type: 'textStyle/italic' },
307 { type: 'textStyle/underline' },
308 { type: 'textStyle/bold' }
309 ] ],
310 [ 'e', [
311 { type: 'textStyle/italic' },
312 { type: 'textStyle/underline' }
313 ] ],
314 [ 'f', [
315 { type: 'textStyle/underline' },
316 { type: 'textStyle/bold' },
317 { type: 'textStyle/italic' }
318 ] ],
319 ...'ghi',
320 { type: '/paragraph' }
321 ],
322 html: 'abc<i><u><b>d</b>e<b>f</b></u></i>ghi'
323 },
324 {
325 msg: 'Out-of-order closings surrounded by plain text',
326 data: [
327 { type: 'paragraph' },
328 ...'abc',
329 [ 'd', [
330 { type: 'textStyle/italic' },
331 { type: 'textStyle/underline' },
332 { type: 'textStyle/bold' }
333 ] ],
334 [ 'e', [
335 { type: 'textStyle/bold' },
336 { type: 'textStyle/underline' }
337 ] ],
338 [ 'f', [
339 { type: 'textStyle/underline' },
340 { type: 'textStyle/bold' }
341 ] ],
342 ...'ghi',
343 { type: '/paragraph' }
344 ],
345 html: 'abc<i><u><b>d</b></u></i><b><u>ef</u></b>ghi'
346 }
347 ];
348
349 cases.forEach( ( caseItem ) => {
350 const doc = new ve.dm.Document( ve.dm.example.preprocessAnnotations( caseItem.data ) );
351 const $wrapper = $( new ve.ce.ParagraphNode( doc.getDocumentNode().getChildren()[ 0 ] ).getRenderedContents() );
352 // HACK strip out all the class="ve-ce-textStyleAnnotation ve-ce-textStyleBoldAnnotation" crap
353 $wrapper.find( '.ve-ce-textStyleAnnotation' ).removeAttr( 'class' );
354 assert.equalDomElement( $wrapper[ 0 ], $( '<div>' ).html( caseItem.html )[ 0 ], caseItem.msg );
355 } );
356 } );