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