+function getCursorPos() {
+ var cursorPos;
+ if (window.getSelection) {
+ var selObj = window.getSelection();
+ var selRange = selObj.getRangeAt(0);
+ //cursorPos = findNode(selObj.anchorNode.parentNode.childNodes, selObj.anchorNode) + selObj.anchorOffset;
+ cursorPos = findNode(unlocked.childNodes, selObj.anchorNode,0) + selObj.anchorOffset;
+ /* FIXME the following works wrong in Opera when the document is longer than 32767 chars */
+ return(cursorPos);
+ }
+ else if (document.selection) {
+ var range = document.selection.createRange();
+ var bookmark = range.getBookmark();
+ /* FIXME the following works wrong when the document is longer than 65535 chars */
+ cursorPos = bookmark.charCodeAt(2) - 11; /* Undocumented function [3] */
+ return(cursorPos);
+ }
+}
+
+function findNode(list, node, acc) {
+ for (var i = 0; i < list.length; i++) {
+ if (list[i] == node) {
+ // debug("success " + i);
+ return acc;
+ }
+ if (list[i].hasChildNodes()) {
+ try {
+ // debug("recursion on node " + i);
+ return (findNode(list[i].childNodes,node,acc))
+ }
+ catch (e) { /* debug("recursion failed"); */ }
+ }
+ sandbox = document.getElementById("sandbox");
+ dup = list[i].cloneNode(true);
+ sandbox.appendChild(dup);
+// debug("fail " + i + ": " + sandbox.innerHTML);
+ acc += sandbox.innerHTML.length;
+ sandbox.removeChild(dup);
+ }
+ throw "not found";
+}
+
+function test () {
+ debug("cursor test: " + unlocked.innerHTML.substr(0,getCursorPos()));
+}