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