Localisation updates from http://translatewiki.net.
[mediawiki.git] / tests / qunit / suites / resources / jquery / jquery.textSelection.test.js
blobe01f217a04dd9f71b79621f42eb824444cc25a08
1 module( 'jquery.textSelection', QUnit.newMwEnvironment() );
3 test( '-- Initial check', function() {
4         expect(1);
5         ok( $.fn.textSelection, 'jQuery.fn.textSelection defined' );
6 } );
8 /**
9  * Test factory for $.fn.textSelection( 'encapsulateText' )
10  *
11  * @param options {object} associative array containing:
12  *   description {string}
13  *   input {string}
14  *   output {string}
15  *   start {int} starting char for selection
16  *   end {int} ending char for selection
17  *   params {object} add'l parameters for $().textSelection( 'encapsulateText' )
18  */
19 var encapsulateTest = function( options ) {
20         var opt = $.extend({
21                 description: '',
22                 before: {},
23                 after: {},
24                 replace: {}
25         }, options);
27         opt.before = $.extend({
28                 text: '',
29                 start: 0,
30                 end: 0
31         }, opt.before);
32         opt.after = $.extend({
33                 text: '',
34                 selected: null
35         }, opt.after);
37         test( opt.description, function() {
38                 var tests = 1;
39                 if ( opt.after.selected !== null ) {
40                         tests++;
41                 }
42                 expect( tests );
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,
52                         end = opt.before.end;
53                 if ( window.opera ) {
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;
59                 }
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.' );
73                 }
75         } );
78 var sig = {
79         'pre': "--~~~~"
80 }, bold = {
81         pre: "'''",
82         peri: 'Bold text',
83         post: "'''"
84 }, h2 = {
85         'pre': '== ',
86         'peri': 'Heading 2',
87         'post': ' ==',
88         'regex': /^(\s*)(={1,6})(.*?)\2(\s*)$/,
89         'regexReplace': "\$1==\$3==\$4",
90         'ownline': true
91 }, ulist = {
92         'pre': "* ",
93         'peri': 'Bulleted list item',
94         'post': "",
95         'ownline': true,
96         'splitlines': true
99 encapsulateTest({
100         description: "Adding sig to end of text",
101         before: {
102                 text: "Wikilove dude! ",
103                 start: 15,
104                 end: 15
105         },
106         after: {
107                 text: "Wikilove dude! --~~~~",
108                 selected: ""
109         },
110         replace: sig
113 encapsulateTest({
114         description: "Adding bold to empty",
115         before: {
116                 text: "",
117                 start: 0,
118                 end: 0
119         },
120         after: {
121                 text: "'''Bold text'''",
122                 selected: "Bold text" // selected because it's the default
123         },
124         replace: bold
127 encapsulateTest({
128         description: "Adding bold to existing text",
129         before: {
130                 text: "Now is the time for all good men to come to the aid of their country",
131                 start: 20,
132                 end: 32
133         },
134         after: {
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'
137         },
138         replace: bold
141 encapsulateTest({
142         description: "ownline option: adding new h2",
143         before: {
144                 text:"Before\nAfter",
145                 start: 7,
146                 end: 7
147         },
148         after: {
149                 text: "Before\n== Heading 2 ==\nAfter",
150                 selected: "Heading 2"
151         },
152         replace: h2
155 encapsulateTest({
156         description: "ownline option: turn a whole line into new h2",
157         before: {
158                 text:"Before\nMy heading\nAfter",
159                 start: 7,
160                 end: 17
161         },
162         after: {
163                 text: "Before\n== My heading ==\nAfter",
164                 selected: ""
165         },
166         replace: h2
170 encapsulateTest({
171         description: "ownline option: turn a partial line into new h2",
172         before: {
173                 text:"BeforeMy headingAfter",
174                 start: 6,
175                 end: 16
176         },
177         after: {
178                 text: "Before\n== My heading ==\nAfter",
179                 selected: ""
180         },
181         replace: h2
185 encapsulateTest({
186         description: "splitlines option: no selection, insert new list item",
187         before: {
188                 text: "Before\nAfter",
189                 start: 7,
190                 end: 7
191         },
192         after: {
193                 text: "Before\n* Bulleted list item\nAfter"
194         },
195         replace: ulist
198 encapsulateTest({
199         description: "splitlines option: single partial line selection, insert new list item",
200         before: {
201                 text: "BeforeMy List ItemAfter",
202                 start: 6,
203                 end: 18
204         },
205         after: {
206                 text: "Before\n* My List Item\nAfter"
207         },
208         replace: ulist
211 encapsulateTest({
212         description: "splitlines option: multiple lines",
213         before: {
214                 text: "Before\nFirst\nSecond\nThird\nAfter",
215                 start: 7,
216                 end: 25
217         },
218         after: {
219                 text: "Before\n* First\n* Second\n* Third\nAfter"
220         },
221         replace: ulist
225 var caretTest = function(options) {
226         test(options.description, function() {
227                 expect(2);
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,
236                                 end: options.end
237                         });
238                 }
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(', ') + ')');
243                         } else {
244                                 equal(actual, expected, message);
245                         }
246                 };
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.');
251         });
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
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'
267 caretTest({
268         description: 'set/getCaretPosition with forced empty selection',
269         text: caretSample,
270         start: 7,
271         end: 7,
272         mode: 'set'
275 caretTest({
276         description: 'set/getCaretPosition with small selection',
277         text: caretSample,
278         start: 6,
279         end: 11,
280         mode: 'set'