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