-
Notifications
You must be signed in to change notification settings - Fork 14
/
mylib-textrange-min.js
1 lines (1 loc) · 6.76 KB
/
mylib-textrange-min.js
1
var API,D,global=this;if(API&&API.attachDocumentReadyListener){API.attachDocumentReadyListener(function(){var L=API.isHostMethod,e=API.isHostObjectProperty,U=API.createElement,V=API.getBodyElement,W=API.toArray;var Y=API.selectionToHostRange,G=API.getHostSelection,F=API.getControlSelection;var I=API.getHostRangeText;if(Y&&G&&F){var S=function(h,i){var f,g,j=h;while(!f&&j){g=i;while(g&&j!=g){g=g.parentNode}if(j==g){if(j.nodeType==3){j=j.parentNode}f=true}else{j=j.parentNode}}return j};var M="EndToEnd",Q="StartToEnd",R="StartToStart",a="EndToStart";var X=[0,1,2,3];var T=[R,Q,M,a];if(L(global.document,"createRange")){var J=global.document.createRange();J.selectNode(V());if(J.compareBoundaryPoints(1,J)==-1){X=[0,3,2,1]}}var B=function(g){var f=0;while((g=g.previousSibling)){f++}return f};var K=/^(input|textarea)$/i;var E=function(f){return K.test(f.tagName)&&(!f.type||f.type=="text")};var P=function(f){if(typeof f.canHaveChildren=="boolean"&&!f.canHaveChildren){return false}if(!f.childNodes.length){return false}if(f.childNodes.length==1&&f.childNodes[0].nodeType!=1){return false}return true};var H=function(k,j,f,h,g){var i;h.setEndPoint(k?R:M,f);if(j){i=h.text.length}else{i=g.nodeValue.length-h.text.length}return i};var C=function(i,l,o,r,t){var m,q,p,k,j;r.collapse(i);var h=API.toArray(l.childNodes),s=0,n=h.length-1;l.appendChild(t);while(!m&&s<=n){q=Math.floor((s+n)/2);l.insertBefore(t,h[q]);r.moveToElementText(t);p=o.compareEndPoints(i?R:M,r);if(p>0){s=q+1}else{if(p<0){n=q-1}else{m=true}}}var g=(p!=-1||!q)?"nextSibling":"previousSibling";j=t;do{j=j[g]}while(j&&j.nodeType>4);var f=p!=-1||!q;if(j.nodeType==1){k=B(j)}else{k=H(i,f,o,r,j)}return{node:j,offset:k,after:f}};var N=function(f,j,m){var i=0;var n=U("a",m);var g=j.parentElement();var l=j.duplicate(),p,h;if(E(g)){return{node:g,offset:F(g)[f?0:1]}}if(g.tagName.toLowerCase()=="input"){return{node:m||global.document,offset:0}}if(g.nodeType==1){l.moveToElementText(g);if(j.compareEndPoints(R,l)==-1||j.compareEndPoints(M,l)==1){g=g.parentNode}}while(!h&&g.nodeType==1&&P(g)){p=C(f,g,j,l,n);if(g==p.node){h=true}else{g=p.node}i=p.offset}var o=g.childNodes;if(g.nodeType==1&&o.length==1&&(o[0].nodeType==3||o[0].nodeType==4)){if(!p||!p.after){g.appendChild(n);g=g.childNodes[0]}else{g.insertBefore(n,g.childNodes[0]);g=g.childNodes[1]}try{l.moveToElementText(n)}catch(k){}i=H(f,p&&p.after,j,l,g)}else{if(g.nodeType==1){n.parentNode.removeChild(n);n=null;i=B(g);g=g.parentNode}}if(n){n.parentNode.removeChild(n)}return{node:g,offset:i}};var b=function(g,f){if(g==9){if(f==9){return 0}return -1}return null};var Z=function(s,h,p,v,q){var n,m,o=this,u=typeof h!="undefined";if(!u){m=Y(G(s));if(m&&typeof m.length=="number"){for(var l=m.length;l--;){this[l]=m[l]}return }}if(!m){if(!s){s=global.document}if(L(s,"createRange")){m=s.createRange()}else{n=V(s);if(L(n,"createTextRange")){m=n.createTextRange()}n=null}}var g=e(m,"commonAncestorContainer");if(!u){if(g){h=m.startContainer;v=m.endContainer;p=m.startOffset;q=m.endOffset}else{var f=N(true,m,s);var j=N(false,m,s);h=f.node;p=f.offset;v=j.node;q=j.offset}}var k=function(){o.startContainer=h;o.startOffset=p;o.endContainer=v;o.endOffset=q;if(g){if(u){m.setEnd(v,q);m.setStart(h,p)}o.commonAncestorContainer=m.commonAncestorContainer}else{if(u){var w,z=U("a",s);var y=m.duplicate();var x=m.duplicate();var i=function(AA,AB){w.insertBefore(z,AB?h:v);x.moveToElementText(z);AA.setEndPoint(R,x);w.removeChild(z)};if(h.nodeType==1){m.moveToElementText(h.childNodes[p])}else{w=h.parentNode;if(w){m.moveToElementText(w);i(m,true);m.moveStart("character",p)}}if(v.nodeType==1){y.moveToElementText(v.childNodes[q])}else{w=v.parentNode;if(w){y.moveToElementText(v.parentNode);i(y);y.moveStart("character",q)}}m.setEndPoint(a,y);z=y=x=null}if(h==v){o.commonAncestorContainer=h}else{o.commonAncestorContainer=S(h,v)||s}}o.collapsed=o.startContainer==o.endContainer&&o.startOffset==o.endOffset};if(L(m,"select")){this.select=function(){return m.select()}}else{var r=G(s);if(L(r,"addRange")){this.select=function(){var i=G(this.getDocument());i.removeAllRanges();i.addRange(m)}}r=null}if(L(m,"isPointInRange")){this.isPointInRange=function(i,w){return m.isPointInRange(i,w)}}else{if(L(m,"inRange")){this.isPointInRange=function(i,w){return m.inRange(new Z(s,i,w,i,i.nodeValue.length).getHostRange())}}}this.cloneRange=function(){return new Z(s,this.startContainer,this.startOffset,this.endContainer,this.endOffset)};var t=function(i){return function(w){u=false;m[i](w);h=v=w;p=q=B(w);k()}};if(L(m,"selectNodeContents")){this.selectNodeContents=t("selectNodeContents")}else{if(L(m,"moveToElementText")){this.selectNodeContents=t("moveToElementText")}}if(L(m,"compareBoundaryPoints")){this.compareBoundaryPoints=function(i,w){return m.compareBoundaryPoints(X[i],w.getHostRange())}}else{if(L(m,"compareEndPoints")){this.compareBoundaryPoints=function(x,AB){var z=h.nodeType;var AA=v.nodeType;var w=AB.startContainer.nodeType;var y=AB.endContainer.nodeType;var i=null;switch(x){case 0:i=b(z,w);break;case 2:i=b(AA,y);break;case 1:if(z==9){if(y==9){return 0}i=-1}if(w==9){if(AA==9){return 0}i=1}break;case 3:if(AA==9){if(w==9){return 0}i=-1}if(y==9){if(z==9){return 0}i=1}}return i===null?m.compareEndPoints(T[x],AB.getHostRange()):i}}}if(L(m,"detach")){this.detach=function(){m.detach();m=null}}else{this.detach=function(){m=null}}this.setStart=function(i,w){h=this.startContainer=i;p=this.startOffset=w;u=true;k()};this.setEnd=function(i,w){v=this.endContainer=i;q=this.endOffset=w;u=true;k()};if(L(m,"insertNode")){this.insertNode=function(i){m.insertNode(i);h=i;p=B(i);k()}}else{this.insertNode=function(y){var x;if(h.nodeType==1){h.insertBefore(y,h.childNodes[p]);this.setStart(y,0)}else{var w=h.cloneNode(false);var z=h.nodeValue;x=h.parentNode;h.nodeValue=z.slice(p);w.nodeValue=z.substring(0,p);x.insertBefore(w,h);x.insertBefore(y,h);var i=h;var AA=p;if(y.nodeType!=1){this.setStart(y,0)}else{this.setStart(y.parentNode,B(y))}if(i==v){this.setEnd(i,q-AA)}}}}this.getHostRange=function(){return m};this.getDocument=function(){return s||global.document};k()};Z.prototype.START_TO_START=0;Z.prototype.START_TO_END=1;Z.prototype.END_TO_END=2;Z.prototype.END_TO_START=3;Z.prototype.collapse=function(f){if(f){this.setEnd(this.startContainer,this.startOffset)}else{this.setStart(this.endContainer,this.endOffset)}};Z.prototype.toString=function(){return I(this.getHostRange())};var A,d,c;d=API.getRange=function(g){var f=new Z(g);return(typeof f.length=="number")?null:f};A=API.getSelectedControls=function(g){var f=new Z(g);if(typeof f.length=="number"){return W(f)}return null};var O=API.getElementDocument;c=API.createRange=function(h,f,i,g,j){if(!j){j=O(h)}return new Z(j,h,f,i,g)};if(D&&D.prototype){D.prototype.getRange=function(){return d(this.node())};D.prototype.getSelectedControls=function(){return A(this.node())};D.prototype.createRange=function(h,f,i,g){return c(h,f,i,g,this.node())}}}})}