-
Notifications
You must be signed in to change notification settings - Fork 14
/
mylib-textselection-min.js
1 lines (1 loc) · 2.55 KB
/
mylib-textselection-min.js
1
var global=this,API,D,C;if(API&&API.attachDocumentReadyListener){API.attachDocumentReadyListener(function(){var R=API.isHostMethod,P=API.isHostObjectProperty,E=API.getDocumentWindow;var F,L,A,H,O,N,K;var G,B,M=global.document;if(R(global,"getSelection")&&E){F=function(S){return E(S).getSelection()}}else{if(P(global.document,"selection")){F=function(S){return(S||global.document).selection}}}API.getHostSelection=F;var J=API.getHostRangeText=function(S){if(typeof S.text=="string"){return S.text}if(R(S,"toString")){return S.toString()}};if(F){K=function(T){var S=A(T);if(S){return J(S)}if(R(T,"toString")){return T.toString()}return""};A=API.selectionToHostRange=function(S){if(R(S,"getRangeAt")){return S.rangeCount?S.getRangeAt(0):null}if(R(S,"createRange")){return S.createRange()}return null}}if(A){L=function(U){var T=F(U);if(R(T,"empty")){T.empty()}else{if(R(T,"collapseToStart")){T.collapseToStart()}}var S=A(T);if(S&&R(S,"collapse")){S.collapse(true);S.select()}};API.getHostRange=function(S){return A(F(S))}}if(L){API.clearDocumentSelection=function(S){L(S)}}API.getDocumentSelectionText=function(S){return K(F(S))};B=M.createElement("input");G=API.getBodyElement();var I=/\r\n/g;var Q=/\r\n$/;if(R(M,"createElement")&&G&&R(G,"appendChild")&&B){G.appendChild(B);if(typeof B.selectionStart=="number"){H=function(T){var U=T.selectionStart,S=T.selectionEnd;return[U,S,T.value.substring(U,S)]};O=function(T,U,S){T.selectionStart=U;T.selectionEnd=S}}else{if(A){H=function(V){var W,U,T,Y,S,X;if(R(V,"focus")){V.focus()}W=A(F());if(W){if(R(W,"duplicate")){U=W.duplicate();if(V.tagName=="INPUT"){U.expand("textedit")}else{if(Q.test(V.value)){U.collapse(false);U.text=" ";S=U.duplicate()}U.moveToElementText(V)}X=U.text.replace(I,"\n");T=X.length;U.setEndPoint("StartToStart",W);X=U.text.replace(I,"\n");Y=T-X.length;X=W.text.replace(I,"\n");if(S){S.moveStart("character",-1);S.text=""}return([Y,Y+X.length,X])}}return null};if(R(B,"createTextRange")){O=function(U,V,S){var T=U.createTextRange();T.collapse();T.moveStart("character",V);T.moveEnd("character",S-V);T.select()}}}}G.removeChild(B)}API.getControlSelection=H;API.setControlSelection=O;if(O){N=API.clearControlSelection=function(S){O(S,0,0)}}if(C&&C.prototype&&H){if(O){C.prototype.getSelection=function(){return H(this.element())};C.prototype.setSelection=function(T,S){O(this.element(),T,S);return this};C.prototype.clearSelection=function(){N(this.element());return this}}}if(D&&D.prototype&&K){D.prototype.getSelectionText=function(){return K(F(this.node()))};if(L){D.prototype.clearSelection=function(){L(this.node());return this}}}M=B=null})}