SXI.Selection = {}; if (window.getSelection && document.createRange) { SXI.Selection.save = function($peNode) { var doc = $peNode.ownerDocument, win = doc.defaultView; var $iSPos = $peNode.selectionStart, $iEPos = $peNode.selectionEnd; if (typeof $iSPos !== "number" && win.getSelection().rangeCount) { var $range = win.getSelection().getRangeAt(0); var $dummyRange = $range.cloneRange(); $dummyRange.selectNodeContents($peNode); $dummyRange.setEnd($range.startContainer, $range.startOffset); $iSPos = $dummyRange.toString().length; $iEPos = $iSPos + $range.toString().length; } return { container: $peNode, start: $iSPos, end: $iEPos } }; SXI.Selection.restore = function($poSelection) { var doc = $poSelection.container.ownerDocument, win = doc.defaultView; var $iCharIndex = 0, $range = doc.createRange(); $range.setStart($poSelection.container, 0); $range.collapse(true); var $aNodeStack = [$poSelection.container], $node, $bFoundStart = false, $bStopFind = false; while (!$bStopFind && ($node = $aNodeStack.pop())) { if ($node.nodeType == 3) { var $iNextCharIndex = $iCharIndex + $node.length; if (!$bFoundStart && $poSelection.start >= $iCharIndex && $poSelection.start <= $iNextCharIndex) { $range.setStart($node, $poSelection.start - $iCharIndex); $bFoundStart = true; } if ($bFoundStart && $poSelection.end >= $iCharIndex && $poSelection.end <= $iNextCharIndex) { $range.setEnd($node, $poSelection.end - $iCharIndex); $bStopFind = true; } $iCharIndex = $iNextCharIndex; } else { var $i = $node.childNodes.length; while ($i--) { $aNodeStack.push($node.childNodes[$i]); } } } var $selection = win.getSelection(); $selection.removeAllRanges(); $selection.addRange($range); } } else if (document.selection) { SXI.Selection.save = function($peNode) { var doc = $peNode.ownerDocument, win = doc.defaultView || doc.parentWindow; var $textRange = doc.selection.createRange(); var $dummyTextRange = doc.body.createTextRange(); $dummyTextRange.moveToElementText($peNode); $dummyTextRange.setEndPoint("EndToStart", $textRange); var $iSPos = $dummyTextRange.text.length; return { container: $peNode, start: $iSPos, end: $iSPos + $textRange.text.length } }; SXI.Selection.restore = function($poSelection) { var doc = $poSelection.container.ownerDocument, win = doc.defaultView || doc.parentWindow; var $textRange = doc.body.createTextRange(); $textRange.moveToElementText($poSelection.container); $textRange.collapse(true); $textRange.moveEnd("character", $poSelection.end); $textRange.moveStart("character", $poSelection.start); $textRange.select(); }; } SXI.Selection2 = {}; if (window.getSelection) { // IE 9 and non-IE SXI.Selection2.save = function(win) { var sel = win.getSelection(), ranges = []; if (sel.rangeCount) { for (var i = 0, len = sel.rangeCount; i < len; ++i) { ranges.push(sel.getRangeAt(i)); } } return ranges; }; SXI.Selection2.restore = function(win, savedSelection) { var sel = win.getSelection(); sel.removeAllRanges(); for (var i = 0, len = savedSelection.length; i < len; ++i) { sel.addRange(savedSelection[i]); } }; } else if (document.selection && document.selection.createRange) { // IE <= 8 SXI.Selection2.save = function(win) { var sel = win.document.selection; return (sel.type != "None") ? sel.createRange() : null; }; SXI.Selection2.restore = function(win, savedSelection) { if (savedSelection) { savedSelection.select(); } }; }