3 QUnit
.module( 'jquery.textSelection', QUnit
.newMwEnvironment() );
6 * Test factory for $.fn.textSelection( 'encapsulateText' )
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' )
16 function encapsulateTest( options
) {
24 opt
.before
= $.extend( {
29 opt
.after
= $.extend( {
34 QUnit
.test( opt
.description
, function ( assert
) {
35 var $textarea
, start
, end
, options
, text
, selected
;
37 $textarea
= $( '<textarea>' );
39 $( '#qunit-fixture' ).append( $textarea
);
41 $textarea
.textSelection( 'setContents', opt
.before
.text
);
43 start
= opt
.before
.start
;
47 options
= $.extend( {}, opt
.replace
);
48 options
.selectionStart
= start
;
49 options
.selectionEnd
= end
;
50 $textarea
.textSelection( 'encapsulateSelection', options
);
52 text
= $textarea
.textSelection( 'getContents' ).replace( /\r\n/g, '\n' );
54 assert
.equal( text
, opt
.after
.text
, 'Checking full text after encapsulation' );
56 if ( opt
.after
.selected
!== null ) {
57 selected
= $textarea
.textSelection( 'getSelection' );
58 assert
.equal( selected
, opt
.after
.selected
, 'Checking selected text after encapsulation.' );
77 regex
: /^(\s*)(={1,6})(.*?)\2(\s*)$/,
78 regexReplace
: '$1==$3==$4',
83 peri
: 'Bulleted list item',
90 description
: 'Adding sig to end of text',
92 text
: 'Wikilove dude! ',
97 text
: 'Wikilove dude! --~~~~',
104 description
: 'Adding bold to empty',
111 text
: '\'\'\'Bold text\'\'\'',
112 selected
: 'Bold text' // selected because it's the default
118 description
: 'Adding bold to existing text',
120 text
: 'Now is the time for all good men to come to the aid of their country',
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'
132 description
: 'ownline option: adding new h2',
134 text
: 'Before\nAfter',
139 text
: 'Before\n== Heading 2 ==\nAfter',
140 selected
: 'Heading 2'
146 description
: 'ownline option: turn a whole line into new h2',
148 text
: 'Before\nMy heading\nAfter',
153 text
: 'Before\n== My heading ==\nAfter',
160 description
: 'ownline option: turn a partial line into new h2',
162 text
: 'BeforeMy headingAfter',
167 text
: 'Before\n== My heading ==\nAfter',
174 description
: 'splitlines option: no selection, insert new list item',
176 text
: 'Before\nAfter',
181 text
: 'Before\n* Bulleted list item\nAfter'
187 description
: 'splitlines option: single partial line selection, insert new list item',
189 text
: 'BeforeMy List ItemAfter',
194 text
: 'Before\n* My List Item\nAfter'
200 description
: 'splitlines option: multiple lines',
202 text
: 'Before\nFirst\nSecond\nThird\nAfter',
207 text
: 'Before\n* First\n* Second\n* Third\nAfter'
212 function caretTest( options
) {
213 QUnit
.test( options
.description
, function ( assert
) {
215 $textarea
= $( '<textarea>' ).text( options
.text
);
217 $( '#qunit-fixture' ).append( $textarea
);
219 if ( options
.mode
=== 'set' ) {
220 $textarea
.textSelection( 'setSelection', {
221 start
: options
.start
,
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( ', ' ) + ')' );
230 assert
.equal( actual
, expected
, message
);
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.' );
240 caretSample
= 'Some big text that we like to work with. Nothing fancy... you know what I mean?';
242 /* @broken: Disabled per T36820
244 description: 'getCaretPosition with original/empty selection - T33847 with IE 6/7/8',
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.
253 description
: 'set/getCaretPosition with forced empty selection',
261 description
: 'set/getCaretPosition with small selection',