-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathselection.min.js
More file actions
11 lines (11 loc) · 3.83 KB
/
Copy pathselection.min.js
File metadata and controls
11 lines (11 loc) · 3.83 KB
1
2
3
4
5
6
7
8
9
10
11
/*
selection.js Copyright (c) 2011-12 Tim Cameron Ryan. MIT licensed.
*/
(function(){var f={isPreceding:function(a,b){return b.compareDocumentPosition(a)&2},contains:function(a,b){return null!=a.compareDocumentPosition?a.compareDocumentPosition(b)&16:a.contains(b)},isCursorPreceding:function(a,b,d,c){return a===d?b<=c:f.isText(a)&&f.isText(d)?f.isPreceding(a,d):f.isText(a)&&!f.isText(d)?!f.isCursorPreceding(d,c,a,b):!f.contains(a,d)?f.isPreceding(a,d):a.childNodes.length<=b?!1:a.childNodes[b]===d?0<=c:f.isPreceding(a.childNodes[b],d)},isText:function(a){return null!=a?
3==a.nodeType:!1},getChildIndex:function(a){for(var b=0;a=a.previousSibling;)b++;return b}},e=this.Selection=function(){function a(a){this.win=a}a.prototype.hasSelection=function(){return a.hasSelection(this.win)};a.prototype.getOrigin=function(){return a.getOrigin(this.win)};a.prototype.getFocus=function(){return a.getFocus(this.win)};a.prototype.getStart=function(){return a.getStart(this.win)};a.prototype.getEnd=function(){return a.getEnd(this.win)};a.prototype.setSelection=function(b,d,c,e){return a.setSelection(this.win,
b,d,c,e)};a.prototype.clearSelection=function(){return a.clearSelection(this.win)};return a}();if(this.getSelection)e.supported=!0,e.hasSelection=function(a){var b;return(b=a.getSelection())&&null!=b.focusNode&&null!=b.anchorNode},e.getOrigin=function(a){var b;return!((b=a.getSelection())&&null!=b.anchorNode)?null:[b.anchorNode,b.anchorOffset]},e.getFocus=function(a){var b;return!((b=a.getSelection())&&null!=b.focusNode)?null:[b.focusNode,b.focusOffset]},e.getStart=function(a){var b,d,c;if(!e.hasSelection(a))return null;
d=e.getOrigin(a);b=d[0];d=d[1];c=e.getFocus(a);a=c[0];c=c[1];return f.isCursorPreceding(b,d,a,c)?[b,d]:[a,c]},e.getEnd=function(a){var b,d,c;if(!e.hasSelection(a))return null;d=e.getOrigin(a);b=d[0];d=d[1];c=e.getFocus(a);a=c[0];c=c[1];return f.isCursorPreceding(b,d,a,c)?[a,c]:[b,d]},e.setSelection=function(a,b,d,c,e){var f=a.getSelection();if(f)if(null==c&&(c=b),null==e&&(e=d),f.collapse&&f.extend)f.collapse(b,d),f.extend(c,e);else{r=a.document.createRange();r.setStart(b,d);r.setEnd(c,e);try{f.removeAllRanges()}catch(g){}f.addRange(r)}},
e.clearSelection=function(a){try{var b=a.getSelection();b&&b.removeAllRanges()}catch(d){}};else if(this.document.selection){var j=function(a,b,d){var c,e,a=a.createElement("a");c=b.duplicate();c.collapse(d);for(e=c.parentElement();!(e.insertBefore(a,a.previousSibling),c.moveToElementText(a),!(0<c.compareEndPoints(d?"StartToStart":"StartToEnd",b)&&null!=a.previousSibling)););-1===c.compareEndPoints(d?"StartToStart":"StartToEnd",b)&&a.nextSibling?(c.setEndPoint(d?"EndToStart":"EndToEnd",b),b=a.nextSibling,
d=c.text.length):(b=a.parentNode,d=f.getChildIndex(a));a.parentNode.removeChild(a);return[b,d]},k=function(a,b,d,c,e){var i,g,h;h=0;i=f.isText(c)?c:c.childNodes[e];g=f.isText(c)?c.parentNode:c;f.isText(c)&&(h=e);c=a.createElement("a");g.insertBefore(c,i||null);a=a.body.createTextRange();a.moveToElementText(c);c.parentNode.removeChild(c);b.setEndPoint(d?"StartToStart":"EndToEnd",a);return b[d?"moveStart":"moveEnd"]("character",h)};e.supported=!0;e.hasSelection=function(a){var b;a.focus();return!a.document.selection?
!1:(b=a.document.selection.createRange())&&b.parentElement().document===a.document};e.getStart=function(a){var b;a.focus();if(!e.hasSelection(a))return null;b=a.document.selection.createRange();return j(a.document,b,!0)};e.getEnd=function(a){var b;a.focus();if(!e.hasSelection(a))return null;b=a.document.selection.createRange();return j(a.document,b,!1)};e.getOrigin=function(a){return e.getStart(a)};e.getFocus=function(a){return e.getEnd(a)};e.setSelection=function(a,b,d,c,e){null==c&&(c=b);null==
e&&(e=d);var f=a.document.body.createTextRange();k(a.document,f,!1,c,e);k(a.document,f,!0,b,d);return f.select()};e.clearSelection=function(a){return a.document.selection.empty()}}else e.supported=!1}).call(this);