Merge "SpecialMovepage: Convert form to use OOUI controls"
[lhc/web/wiklou.git] / tests / qunit / suites / resources / jquery / jquery.textSelection.test.js
1 ( function ( $ ) {
2
3 QUnit.module( 'jquery.textSelection', QUnit.newMwEnvironment() );
4
5 /**
6 * Test factory for $.fn.textSelection( 'encapsulateText' )
7 *
8 * @param {Object} options Associative configuration array
9 * @param {string} options.description Description
10 * @param {string} options.input Input
11 * @param {string} options.output Output
12 * @param {int} options.start Starting char for selection
13 * @param {int} options.end Ending char for selection
14 * @param {object} options.params Additional parameters for $().textSelection( 'encapsulateText' )
15 */
16 function encapsulateTest( options ) {
17 var opt = $.extend( {
18 description: '',
19 before: {},
20 after: {},
21 replace: {}
22 }, options );
23
24 opt.before = $.extend( {
25 text: '',
26 start: 0,
27 end: 0
28 }, opt.before );
29 opt.after = $.extend( {
30 text: '',
31 selected: null
32 }, opt.after );
33
34 QUnit.test( opt.description, function ( assert ) {
35 var $textarea, start, end, options, text, selected,
36 tests = 1;
37 if ( opt.after.selected !== null ) {
38 tests++;
39 }
40 QUnit.expect( tests );
41
42 $textarea = $( '<textarea>' );
43
44 $( '#qunit-fixture' ).append( $textarea );
45
46 $textarea.textSelection( 'setContents', opt.before.text );
47
48 start = opt.before.start;
49 end = opt.before.end;
50
51 // Clone opt.replace
52 options = $.extend( {}, opt.replace );
53 options.selectionStart = start;
54 options.selectionEnd = end;
55 $textarea.textSelection( 'encapsulateSelection', options );
56
57 text = $textarea.textSelection( 'getContents' ).replace( /\r\n/g, '\n' );
58
59 assert.equal( text, opt.after.text, 'Checking full text after encapsulation' );
60
61 if ( opt.after.selected !== null ) {
62 selected = $textarea.textSelection( 'getSelection' );
63 assert.equal( selected, opt.after.selected, 'Checking selected text after encapsulation.' );
64 }
65
66 } );
67 }
68
69 var caretSample,
70 sig = {
71 pre: '--~~~~'
72 },
73 bold = {
74 pre: '\'\'\'',
75 peri: 'Bold text',
76 post: '\'\'\''
77 },
78 h2 = {
79 pre: '== ',
80 peri: 'Heading 2',
81 post: ' ==',
82 regex: /^(\s*)(={1,6})(.*?)\2(\s*)$/,
83 regexReplace: '$1==$3==$4',
84 ownline: true
85 },
86 ulist = {
87 pre: '* ',
88 peri: 'Bulleted list item',
89 post: '',
90 ownline: true,
91 splitlines: true
92 };
93
94 encapsulateTest( {
95 description: 'Adding sig to end of text',
96 before: {
97 text: 'Wikilove dude! ',
98 start: 15,
99 end: 15
100 },
101 after: {
102 text: 'Wikilove dude! --~~~~',
103 selected: ''
104 },
105 replace: sig
106 } );
107
108 encapsulateTest( {
109 description: 'Adding bold to empty',
110 before: {
111 text: '',
112 start: 0,
113 end: 0
114 },
115 after: {
116 text: '\'\'\'Bold text\'\'\'',
117 selected: 'Bold text' // selected because it's the default
118 },
119 replace: bold
120 } );
121
122 encapsulateTest( {
123 description: 'Adding bold to existing text',
124 before: {
125 text: 'Now is the time for all good men to come to the aid of their country',
126 start: 20,
127 end: 32
128 },
129 after: {
130 text: 'Now is the time for \'\'\'all good men\'\'\' to come to the aid of their country',
131 selected: '' // empty because it's not the default'
132 },
133 replace: bold
134 } );
135
136 encapsulateTest( {
137 description: 'ownline option: adding new h2',
138 before: {
139 text: 'Before\nAfter',
140 start: 7,
141 end: 7
142 },
143 after: {
144 text: 'Before\n== Heading 2 ==\nAfter',
145 selected: 'Heading 2'
146 },
147 replace: h2
148 } );
149
150 encapsulateTest( {
151 description: 'ownline option: turn a whole line into new h2',
152 before: {
153 text: 'Before\nMy heading\nAfter',
154 start: 7,
155 end: 17
156 },
157 after: {
158 text: 'Before\n== My heading ==\nAfter',
159 selected: ''
160 },
161 replace: h2
162 } );
163
164 encapsulateTest( {
165 description: 'ownline option: turn a partial line into new h2',
166 before: {
167 text: 'BeforeMy headingAfter',
168 start: 6,
169 end: 16
170 },
171 after: {
172 text: 'Before\n== My heading ==\nAfter',
173 selected: ''
174 },
175 replace: h2
176 } );
177
178 encapsulateTest( {
179 description: 'splitlines option: no selection, insert new list item',
180 before: {
181 text: 'Before\nAfter',
182 start: 7,
183 end: 7
184 },
185 after: {
186 text: 'Before\n* Bulleted list item\nAfter'
187 },
188 replace: ulist
189 } );
190
191 encapsulateTest( {
192 description: 'splitlines option: single partial line selection, insert new list item',
193 before: {
194 text: 'BeforeMy List ItemAfter',
195 start: 6,
196 end: 18
197 },
198 after: {
199 text: 'Before\n* My List Item\nAfter'
200 },
201 replace: ulist
202 } );
203
204 encapsulateTest( {
205 description: 'splitlines option: multiple lines',
206 before: {
207 text: 'Before\nFirst\nSecond\nThird\nAfter',
208 start: 7,
209 end: 25
210 },
211 after: {
212 text: 'Before\n* First\n* Second\n* Third\nAfter'
213 },
214 replace: ulist
215 } );
216
217 function caretTest( options ) {
218 QUnit.test( options.description, 2, function ( assert ) {
219 var pos,
220 $textarea = $( '<textarea>' ).text( options.text );
221
222 $( '#qunit-fixture' ).append( $textarea );
223
224 if ( options.mode === 'set' ) {
225 $textarea.textSelection( 'setSelection', {
226 start: options.start,
227 end: options.end
228 } );
229 }
230
231 function among( actual, expected, message ) {
232 if ( $.isArray( expected ) ) {
233 assert.ok( $.inArray( actual, expected ) !== -1, message + ' (got ' + actual + '; expected one of ' + expected.join( ', ' ) + ')' );
234 } else {
235 assert.equal( actual, expected, message );
236 }
237 }
238
239 pos = $textarea.textSelection( 'getCaretPosition', { startAndEnd: true } );
240 among( pos[ 0 ], options.start, 'Caret start should be where we set it.' );
241 among( pos[ 1 ], options.end, 'Caret end should be where we set it.' );
242 } );
243 }
244
245 caretSample = 'Some big text that we like to work with. Nothing fancy... you know what I mean?';
246
247 /* @broken: Disabled per bug 34820
248 caretTest({
249 description: 'getCaretPosition with original/empty selection - bug 31847 with IE 6/7/8',
250 text: caretSample,
251 start: [0, caretSample.length], // Opera and Firefox (prior to FF 6.0) default caret to the end of the box (caretSample.length)
252 end: [0, caretSample.length], // Other browsers default it to the beginning (0), so check both.
253 mode: 'get'
254 });
255 */
256
257 caretTest( {
258 description: 'set/getCaretPosition with forced empty selection',
259 text: caretSample,
260 start: 7,
261 end: 7,
262 mode: 'set'
263 } );
264
265 caretTest( {
266 description: 'set/getCaretPosition with small selection',
267 text: caretSample,
268 start: 6,
269 end: 11,
270 mode: 'set'
271 } );
272 }( jQuery ) );