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