1/*
2*  $Id: documentselection.js 124 2006-11-27 19:21:36Z wingedfox $
3*  $HeadURL: https://svn.debugger.ru/repos/jslibs/BrowserExtensions/tags/BrowserExtensions.001/documentselection.js $
4*
5*  Class implements cross-browser work with text selection
6*
7*  @author Ilya Lebedev
8*  @author $Author: wingedfox $
9*  @modified $Date: 2006-11-27 22:21:36 +0300 (Пнд, 27 Ноя 2006) $
10*  @version $Rev: 124 $
11*  @license LGPL
12*/
13DocumentSelection=new function(){var i=this;var I={'cursor':'__cursorPosition'};var l,o;this.setRange=function(O,Q,_,c){if(c){var C=i.getStart(O);_=C+_;Q=C+Q}if('function'==typeof O.setSelectionRange){try{O.setSelectionRange(Q,_)}catch(e){}}else{var v;try{v=O.createTextRange();}catch(e){try{v=document.body.createTextRange();v.moveToElementText(O);}catch(e){v=false}}if(!v)return false;v.collapse(true);v.moveStart("character",Q);v.moveEnd("character",_-Q);v.select();}i.setCursorPosition(O,Q);};this.setCursorPosition=function(O,Q){O[I['cursor']]=parseInt(Q);if(l)O.scrollTop=l;if(o)O.scrollLeft=o;l=null;o=null};this.getSelection=function(O){var Q=i.getCursorPosition(O),_=i.getEnd(O);if(_<Q)_=Q;var c=document.selection?O.value.replace(/\r/g,""):O.value;return c.substring(Q,_);};this.getStart=function(O){try{var Q=Math.abs(document.selection.createRange().moveStart("character",-100000000));try{var _=Math.abs(O.createTextRange().moveEnd("character",-100000000));var c=document.body.createTextRange();c.moveToElementText(O);var C=Math.abs(c.moveStart("character",-100000000));var v=Math.abs(c.moveEnd("character",-100000000));if(O.tagName.toLowerCase()!='input'&&v-_==C){Q-=C}}catch(err){};return Q}catch(e){try{return Number(O.selectionStart)}catch(e){return-1}}};this.getEnd=function(O){try{O.focus();var Q=Math.abs(document.selection.createRange().moveEnd("character",-100000000));try{var _=Math.abs(O.createTextRange().moveEnd("character",-100000000));var c=document.body.createTextRange();c.moveToElementText(O);var C=Math.abs(c.moveStart("character",-100000000));var v=Math.abs(c.moveEnd("character",-100000000));if(O.tagName.toLowerCase()!='input'&&v-_==C){Q-=C}}catch(err){};return Q}catch(e){try{return Number(O.selectionEnd)}catch(e){return-1}}};this.getCursorPosition=function(O){l=O.scrollTop;o=O.scrollLeft;if(i.getStart(O)!=i.getEnd(O))return i.getStart(O);return i.getStart(O)||(parseInt(O[I['cursor']])?parseInt(O[I['cursor']]):0);};this.saveCursorPosition=function(O){var Q=O.srcElement||O.target;if(!Q||Q.tagName.toLowerCase()=='select')return false;i.setCursorPosition(Q,i.getStart(Q));};this.insertAtCursor=function(O,Q){var _=i.getCursorPosition(O);var c=document.selection?O.value.replace(/\r/g,""):O.value;O.value=c.substring(0,_)+Q+c.substring(_,c.length);i.setRange(O,_+Q.length,_+Q.length);};this.deleteAtCursor=function(O,Q){if(!Q)Q=false;var _=i.getCursorPosition(O),c=i.getEnd(O);if(c<_)c=_;if(_==c){_=Q?_:_-1<0?0:_-1;c=Q?c+1:c}var C=document.selection?O.value.replace(/\r/g,""):O.value;O.value=C.substring(0,_)+C.substring(c,C.length);i.setRange(O,_,_);};this.deleteSelection=function(O){var Q=i.getCursorPosition(O),_=i.getEnd(O);if(Q==_)return;var c=document.selection?O.value.replace(/\r/g,""):O.value;O.value=c.substring(0,Q)+c.substring(_,c.length);i.setRange(O,Q,Q);}};if(document.attachEvent){document.attachEvent('onmouseup',DocumentSelection.saveCursorPosition);document.attachEvent('onkeyup',DocumentSelection.saveCursorPosition);}else if(document.addEventListener){document.addEventListener('mouseup',DocumentSelection.saveCursorPosition);document.addEventListener('keyup',DocumentSelection.saveCursorPosition);}
14