I wrote a simple test to change the text in an editable div's content. The html structure is changed but the text is the same.
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Hello</title>
<script type="text/javascript" src="rangy-core.js"></script>
<script type="text/javascript" src="rangy-
selectionsaverestore.js"></script>
</head>
<body>
<div id="show" class="code" contenteditable="true"><span
style="color:red">12345</span>12345</div>
<script type="text/javascript">
window.setTimeout(function () {
// save selection / caret position
var show = document.getElementById("show");
show.innerHTML = "1234512345";
// restore select / caret position
}, 5000)
</script>
</body>
</html>
I've tried rangy like this:
var s = rangy.saveSelection();
var show = document.getElementById("show");
show.innerHTML = "1234512345";
rangy.restoreSelection(s);
But it report an error:
Rangy warning: Module SaveRestore: Marker element has been removed. Cannot restore selection.
Does rangy support the feature I mentioned above? If yes, how should I use it? If no, what should I do to implement that?
UPDATE: In my scenario I have to replace all the innerHTML since the text would be formatted into a very rich style. But in my case the text is always the same without the styles. Is that any possible way to record the selection and caret position and set it back? What kind of API I should use?