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