Selection problem in Firefox 24 (rangy version)
Focus must be on the element getting the selection. This fiddle uses rangy to show the problem.
by lddubeau
HTML
<script src="http://rangy.googlecode.com/svn/trunk/dev/uncompressed/rangy-core.js"></script>
<p id="q" contenteditable="true">text</p>
<input id="input"></input>
<button id="forward">Forward</button>
<button id="backwards">Backwards</button>
<button id="backwards-wa">Backwards Workaround</button>
JavaScript
//
// This fiddle demonstrates an error that I got in Firefox 24.
// (Note that all of it works fine in Chrome.)
// Forward works fine. Backwards generates:
//
// NS_ERROR_FAILURE: Component returned failure code: 0x80004005 (NS_ERROR_FAILURE) [nsISelection.extend] @ http://rangy.googlecode.com/svn/trunk/dev/uncompressed/rangy-core.js:3129
//
// Backwards Workaround shows how to avoid the problem.
//
$(function () {
var $q = $("#q");
var $input = $("#input");
var text = $q[0].firstChild;
$("#forward").click(function () {
$input.focus();
var range = rangy.createRange();
range.setStart(text, 0);
range.setEnd(text, 2);
rangy.getSelection().setSingleRange(range);
});
$("#backwards").click(function () {
$input.focus();
var range = document.createRange();
range.setStart(text, 2);
range.setEnd(text, 3);
rangy.getSelection().removeAllRanges();
rangy.getSelection().addRange(range, true);
});
$("#backwards-wa").click(function () {
$input.focus();
var range = document.createRange();
$(text.parentNode).focus();
range.setStart(text, 2);
range.setEnd(text, 3);
rangy.getSelection().setSingleRange(range);
rangy.getSelection().removeAllRanges();
rangy.getSelection().addRange(range, true);
});
});