(bug 30130) Add selectionStart and selectionEnd parameters to encapsulateSelection...
[lhc/web/wiklou.git] / tests / qunit / suites / resources / jquery / jquery.textSelection.test.js
1 module( 'jquery.textSelection' );
2
3 test( '-- Initial check', function() {
4 expect(1);
5 ok( $.fn.textSelection, 'jQuery.fn.textSelection defined' );
6 } );
7
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);
26
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);
36
37 test( opt.description, function() {
38 var tests = 1;
39 if (opt.after.selected !== null) {
40 tests++;
41 }
42 expect(tests);
43
44 var $fixture = $( '<div id="qunit-fixture"></div>' );
45 var $textarea = $( '<textarea>' );
46
47 $fixture.append($textarea);
48 $( 'body' ).append($fixture);
49
50 //$textarea.textSelection( 'setContents', opt.before.text); // this method is actually missing atm...
51 $textarea.val( opt.before.text ); // won't work with the WikiEditor iframe?
52
53 var start = opt.before.start,
54 end = opt.before.end;
55 if ( window.opera ) {
56 // Compensate for Opera's craziness converting "\n" to "\r\n" and counting that as two chars
57 var newLinesBefore = opt.before.text.substring( 0, start ).split( "\n" ).length - 1,
58 newLinesInside = opt.before.text.substring( start, end ).split( "\n" ).length - 1;
59 start += newLinesBefore;
60 end += newLinesBefore + newLinesInside;
61 }
62
63 var options = $.extend( {}, opt.replace ); // Clone opt.replace
64 options.selectionStart = start;
65 options.selectionEnd = end;
66 $textarea.textSelection( 'encapsulateSelection', options );
67
68 var text = $textarea.textSelection( 'getContents' ).replace( /\r\n/g, "\n" );
69
70 equal( text, opt.after.text, 'Checking full text after encapsulation' );
71
72 if (opt.after.selected !== null) {
73 var selected = $textarea.textSelection( 'getSelection' );
74 equal( selected, opt.after.selected, 'Checking selected text after encapsulation.' );
75 }
76
77 } );
78 };
79
80 var sig = {
81 'pre': "--~~~~"
82 }, bold = {
83 pre: "'''",
84 peri: 'Bold text',
85 post: "'''"
86 }, h2 = {
87 'pre': '== ',
88 'peri': 'Heading 2',
89 'post': ' ==',
90 'regex': /^(\s*)(={1,6})(.*?)\2(\s*)$/,
91 'regexReplace': "\$1==\$3==\$4",
92 'ownline': true
93 }, ulist = {
94 'pre': "* ",
95 'peri': 'Bulleted list item',
96 'post': "",
97 'ownline': true,
98 'splitlines': true
99 };
100
101 encapsulateTest({
102 description: "Adding sig to end of text",
103 before: {
104 text: "Wikilove dude! ",
105 start: 15,
106 end: 15
107 },
108 after: {
109 text: "Wikilove dude! --~~~~",
110 selected: ""
111 },
112 replace: sig
113 });
114
115 encapsulateTest({
116 description: "Adding bold to empty",
117 before: {
118 text: "",
119 start: 0,
120 end: 0
121 },
122 after: {
123 text: "'''Bold text'''",
124 selected: "Bold text" // selected because it's the default
125 },
126 replace: bold
127 });
128
129 encapsulateTest({
130 description: "Adding bold to existing text",
131 before: {
132 text: "Now is the time for all good men to come to the aid of their country",
133 start: 20,
134 end: 32
135 },
136 after: {
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'
139 },
140 replace: bold
141 });
142
143 encapsulateTest({
144 description: "ownline option: adding new h2",
145 before: {
146 text:"Before\nAfter",
147 start: 7,
148 end: 7
149 },
150 after: {
151 text: "Before\n== Heading 2 ==\nAfter",
152 selected: "Heading 2"
153 },
154 replace: h2
155 });
156
157 encapsulateTest({
158 description: "ownline option: turn a whole line into new h2",
159 before: {
160 text:"Before\nMy heading\nAfter",
161 start: 7,
162 end: 17
163 },
164 after: {
165 text: "Before\n== My heading ==\nAfter",
166 selected: ""
167 },
168 replace: h2
169 });
170
171
172 encapsulateTest({
173 description: "ownline option: turn a partial line into new h2",
174 before: {
175 text:"BeforeMy headingAfter",
176 start: 6,
177 end: 16
178 },
179 after: {
180 text: "Before\n== My heading ==\nAfter",
181 selected: ""
182 },
183 replace: h2
184 });
185
186
187 encapsulateTest({
188 description: "splitlines option: no selection, insert new list item",
189 before: {
190 text: "Before\nAfter",
191 start: 7,
192 end: 7
193 },
194 after: {
195 text: "Before\n* Bulleted list item\nAfter"
196 },
197 replace: ulist
198 });
199
200 encapsulateTest({
201 description: "splitlines option: single partial line selection, insert new list item",
202 before: {
203 text: "BeforeMy List ItemAfter",
204 start: 6,
205 end: 18
206 },
207 after: {
208 text: "Before\n* My List Item\nAfter"
209 },
210 replace: ulist
211 });
212
213 encapsulateTest({
214 description: "splitlines option: multiple lines",
215 before: {
216 text: "Before\nFirst\nSecond\nThird\nAfter",
217 start: 7,
218 end: 25
219 },
220 after: {
221 text: "Before\n* First\n* Second\n* Third\nAfter"
222 },
223 replace: ulist
224 });