1 module( 'jquery.textSelection', QUnit
.newMwEnvironment() );
3 test( '-- Initial check', function() {
5 ok( $.fn
.textSelection
, 'jQuery.fn.textSelection defined' );
9 * Test factory for $.fn.textSelection( 'encapsulateText' )
11 * @param options {object} associative array containing:
12 * description {string}
15 * start {int} starting char for selection
16 * end {int} ending char for selection
17 * params {object} add'l parameters for $().textSelection( 'encapsulateText' )
19 var encapsulateTest = function( options
) {
27 opt
.before
= $.extend({
32 opt
.after
= $.extend({
37 test( opt
.description
, function() {
39 if ( opt
.after
.selected
!== null ) {
44 var $textarea
= $( '<textarea>' );
46 $( '#qunit-fixture' ).append( $textarea
);
48 //$textarea.textSelection( 'setContents', opt.before.text); // this method is actually missing atm...
49 $textarea
.val( opt
.before
.text
); // won't work with the WikiEditor iframe?
51 var start
= opt
.before
.start
,
54 // Compensate for Opera's craziness converting "\n" to "\r\n" and counting that as two chars
55 var newLinesBefore
= opt
.before
.text
.substring( 0, start
).split( "\n" ).length
- 1,
56 newLinesInside
= opt
.before
.text
.substring( start
, end
).split( "\n" ).length
- 1;
57 start
+= newLinesBefore
;
58 end
+= newLinesBefore
+ newLinesInside
;
61 var options
= $.extend( {}, opt
.replace
); // Clone opt.replace
62 options
.selectionStart
= start
;
63 options
.selectionEnd
= end
;
64 $textarea
.textSelection( 'encapsulateSelection', options
);
66 var text
= $textarea
.textSelection( 'getContents' ).replace( /\r\n/g, "\n" );
68 equal( text
, opt
.after
.text
, 'Checking full text after encapsulation' );
70 if (opt
.after
.selected
!== null) {
71 var selected
= $textarea
.textSelection( 'getSelection' );
72 equal( selected
, opt
.after
.selected
, 'Checking selected text after encapsulation.' );
88 'regex': /^(\s*)(={1,6})(.*?)\2(\s*)$/,
89 'regexReplace': "\$1==\$3==\$4",
93 'peri': 'Bulleted list item',
100 description
: "Adding sig to end of text",
102 text
: "Wikilove dude! ",
107 text
: "Wikilove dude! --~~~~",
114 description
: "Adding bold to empty",
121 text
: "'''Bold text'''",
122 selected
: "Bold text" // selected because it's the default
128 description
: "Adding bold to existing text",
130 text
: "Now is the time for all good men to come to the aid of their country",
135 text
: "Now is the time for '''all good men''' to come to the aid of their country",
136 selected
: "" // empty because it's not the default'
142 description
: "ownline option: adding new h2",
144 text
:"Before\nAfter",
149 text
: "Before\n== Heading 2 ==\nAfter",
150 selected
: "Heading 2"
156 description
: "ownline option: turn a whole line into new h2",
158 text
:"Before\nMy heading\nAfter",
163 text
: "Before\n== My heading ==\nAfter",
171 description
: "ownline option: turn a partial line into new h2",
173 text
:"BeforeMy headingAfter",
178 text
: "Before\n== My heading ==\nAfter",
186 description
: "splitlines option: no selection, insert new list item",
188 text
: "Before\nAfter",
193 text
: "Before\n* Bulleted list item\nAfter"
199 description
: "splitlines option: single partial line selection, insert new list item",
201 text
: "BeforeMy List ItemAfter",
206 text
: "Before\n* My List Item\nAfter"
212 description
: "splitlines option: multiple lines",
214 text
: "Before\nFirst\nSecond\nThird\nAfter",
219 text
: "Before\n* First\n* Second\n* Third\nAfter"
225 var caretTest = function(options
) {
226 test(options
.description
, function() {
229 var $textarea
= $( '<textarea>' ).text(options
.text
);
231 $( '#qunit-fixture' ).append( $textarea
);
233 if (options
.mode
== 'set') {
234 $textarea
.textSelection('setSelection', {
235 start
: options
.start
,
240 var among = function(actual
, expected
, message
) {
241 if ($.isArray(expected
)) {
242 ok($.inArray(actual
, expected
) !== -1 , message
+ ' (got ' + actual
+ '; expected one of ' + expected
.join(', ') + ')');
244 equal(actual
, expected
, message
);
248 var 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 var 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',