QUnit test cases for bug 31847: will trigger a fail on IE 6/7/8
authorBrion Vibber <brion@users.mediawiki.org>
Thu, 20 Oct 2011 22:50:04 +0000 (22:50 +0000)
committerBrion Vibber <brion@users.mediawiki.org>
Thu, 20 Oct 2011 22:50:04 +0000 (22:50 +0000)
jquery.textSelection's getCaretPosition() gives bogus results on IE 6/7/8 when there's not a selection set in the textarea; it seems to give us some value somewhere in the middle of the textarea, rather than what we expected.
If we actually set an empty selection, then it gets it back just fine!
But if the cursor is in initial state, or has been moved by the user, we get back garbage. This is causing WikiEditor to sometimes insert things in the wrong place, as it tries to pull the caret position and manipulate the selection to do insertions; we then end up using the bogus selection when it gets saved in via selection restoration.

tests/qunit/suites/resources/jquery/jquery.textSelection.test.js

index 5926409..5cd49de 100644 (file)
@@ -222,3 +222,54 @@ encapsulateTest({
        },
        replace: ulist
 });
+
+
+var caretTest = function(options) {
+       test(options.description, function() {
+               expect(2);
+
+               var $fixture = $( '<div id="qunit-fixture"></div>' );
+               var $textarea = $( '<textarea>' ).text(options.text);
+
+               $fixture.append($textarea);
+               $( 'body' ).append($fixture);
+
+               if (options.mode == 'set') {
+                       $textarea.textSelection('setSelection', {
+                               start: options.start,
+                               end: options.end
+                       });
+               }
+
+               var pos = $textarea.textSelection('getCaretPosition', {startAndEnd: true});
+               equal(pos[0], options.start, 'Caret start should be where we set it.');
+               equal(pos[1], options.end, 'Caret end should be where we set it.');
+       });
+}
+
+var caretSample = "Some big text that we like to work with. Nothing fancy... you know what I mean?";
+
+caretTest({
+       description: 'getCaretPosition with original/empty selection - bug 31847 with IE 6/7/8',
+       text: caretSample,
+       start: 0,
+       end: 0,
+       mode: 'get'
+});
+
+caretTest({
+       description: 'set/getCaretPosition with forced empty selection',
+       text: caretSample,
+       start: 7,
+       end: 7,
+       mode: 'set'
+});
+
+caretTest({
+       description: 'set/getCaretPosition with small selection',
+       text: caretSample,
+       start: 6,
+       end: 11,
+       mode: 'set'
+});
+