3 QUnit
.module( 'jquery.textSelection', QUnit
.newMwEnvironment() );
6 * Test factory for $.fn.textSelection( 'encapsulateText' )
8 * @param options {object} associative array containing:
12 * start {int} starting char for selection
13 * end {int} ending char for selection
14 * params {object} add'l parameters for $().textSelection( 'encapsulateText' )
16 function encapsulateTest( options
) {
24 opt
.before
= $.extend({
29 opt
.after
= $.extend({
34 QUnit
.test( opt
.description
, function ( assert
) {
35 /*jshint onevar: false */
37 if ( opt
.after
.selected
!== null ) {
40 QUnit
.expect( tests
);
42 var $textarea
= $( '<textarea>' );
44 $( '#qunit-fixture' ).append( $textarea
);
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?
49 var start
= opt
.before
.start
,
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
;
59 var options
= $.extend( {}, opt
.replace
); // Clone opt.replace
60 options
.selectionStart
= start
;
61 options
.selectionEnd
= end
;
62 $textarea
.textSelection( 'encapsulateSelection', options
);
64 var text
= $textarea
.textSelection( 'getContents' ).replace( /\r\n/g, '\n' );
66 assert
.equal( text
, opt
.after
.text
, 'Checking full text after encapsulation' );
68 if ( opt
.after
.selected
!== null ) {
69 var selected
= $textarea
.textSelection( 'getSelection' );
70 assert
.equal( selected
, opt
.after
.selected
, 'Checking selected text after encapsulation.' );
89 regex
: /^(\s*)(={1,6})(.*?)\2(\s*)$/,
90 regexReplace
: '$1==$3==$4',
95 peri
: 'Bulleted list item',
102 description
: 'Adding sig to end of text',
104 text
: 'Wikilove dude! ',
109 text
: 'Wikilove dude! --~~~~',
116 description
: 'Adding bold to empty',
123 text
: '\'\'\'Bold text\'\'\'',
124 selected
: 'Bold text' // selected because it's the default
130 description
: 'Adding bold to existing text',
132 text
: 'Now is the time for all good men to come to the aid of their country',
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'
144 description
: 'ownline option: adding new h2',
146 text
:'Before\nAfter',
151 text
: 'Before\n== Heading 2 ==\nAfter',
152 selected
: 'Heading 2'
158 description
: 'ownline option: turn a whole line into new h2',
160 text
:'Before\nMy heading\nAfter',
165 text
: 'Before\n== My heading ==\nAfter',
173 description
: 'ownline option: turn a partial line into new h2',
175 text
:'BeforeMy headingAfter',
180 text
: 'Before\n== My heading ==\nAfter',
188 description
: 'splitlines option: no selection, insert new list item',
190 text
: 'Before\nAfter',
195 text
: 'Before\n* Bulleted list item\nAfter'
201 description
: 'splitlines option: single partial line selection, insert new list item',
203 text
: 'BeforeMy List ItemAfter',
208 text
: 'Before\n* My List Item\nAfter'
214 description
: 'splitlines option: multiple lines',
216 text
: 'Before\nFirst\nSecond\nThird\nAfter',
221 text
: 'Before\n* First\n* Second\n* Third\nAfter'
227 function caretTest( options
) {
228 QUnit
.test( options
.description
, 2, function ( assert
) {
229 var pos
, $textarea
= $( '<textarea>' ).text( options
.text
);
231 $( '#qunit-fixture' ).append( $textarea
);
233 if ( options
.mode
=== 'set' ) {
234 $textarea
.textSelection('setSelection', {
235 start
: options
.start
,
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(', ') + ')' );
244 assert
.equal( actual
, expected
, message
);
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.');
254 caretSample
= 'Some big text that we like to work with. Nothing fancy... you know what I mean?';
257 // @broken: Disabled per bug 34820
259 description: 'getCaretPosition with original/empty selection - bug 31847 with IE 6/7/8',
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.
268 description
: 'set/getCaretPosition with forced empty selection',
276 description
: 'set/getCaretPosition with small selection',