forked from timdown/rangyinputs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrangyinputs-jquery.js
More file actions
17 lines (16 loc) · 4.53 KB
/
rangyinputs-jquery.js
File metadata and controls
17 lines (16 loc) · 4.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/**
* @license Rangy Inputs, a jQuery plug-in for selection and caret manipulation within textareas and text inputs.
*
* https://github.com/timdown/rangyinputs
*
* For range and selection features for contenteditable, see Rangy.
* http://code.google.com/p/rangy/
*
* Depends on jQuery 1.0 or later.
*
* Copyright 2014, Tim Down
* Licensed under the MIT license.
* Version: 1.2.0
* Build date: 30 November 2014
*/
!function(e){function t(e,t){var n=typeof e[t];return"function"===n||!("object"!=n||!e[t])||"unknown"==n}function n(e,t){return typeof e[t]!=g}function o(e,t){return!("object"!=typeof e[t]||!e[t])}function r(e){window.console&&window.console.log&&window.console.log("RangyInputs not supported in your browser. Reason: "+e)}function c(e,t,n){return 0>t&&(t+=e.value.length),typeof n==g&&(n=t),0>n&&(n+=e.value.length),{start:t,end:n}}function a(e,t,n){return{start:t,end:n,length:n-t,text:e.value.slice(t,n)}}function l(){return o(document,"body")?document.body:document.getElementsByTagName("body")[0]}var i,u,d,s,v,f,p,m,x,g="undefined",h=void 0===e,T=h?function(e){document.addEventListener?document.addEventListener("DOMContentLoaded",e):document.attachEvent("onload",e,!1)}:e(document).ready;T(function(){function T(e,t){var n=e.value,o=i(e),r=o.start;return{value:n.slice(0,r)+t+n.slice(o.end),index:r,replaced:o.text}}function y(e,t){e.focus();var n=i(e);return u(e,n.start,n.end),""==t?document.execCommand("delete",!1,null):document.execCommand("insertText",!1,t),{replaced:n.text,index:n.start}}function S(e,t){e.focus();var n=T(e,t);return e.value=n.value,n}function E(e,t){return function(){var n=this.jquery?this[0]:this,o=n.nodeName.toLowerCase();if(1==n.nodeType&&("textarea"==o||"input"==o&&/^(?:text|email|number|search|tel|url|password)$/i.test(n.type))){var r=[n].concat(Array.prototype.slice.call(arguments)),c=e.apply(this,r);if(!t)return c}return t?this:void 0}}var w=document.createElement("textarea");if(l().appendChild(w),n(w,"selectionStart")&&n(w,"selectionEnd"))i=function(e){var t=e.selectionStart,n=e.selectionEnd;return a(e,t,n)},u=function(e,t,n){var o=c(e,t,n);e.selectionStart=o.start,e.selectionEnd=o.end},x=function(e,t){t?e.selectionEnd=e.selectionStart:e.selectionStart=e.selectionEnd};else{if(!(t(w,"createTextRange")&&o(document,"selection")&&t(document.selection,"createRange")))return l().removeChild(w),void r("No means of finding text input caret position");i=function(e){var t,n,o,r,c=0,l=0,i=document.selection.createRange();return i&&i.parentElement()==e&&(o=e.value.length,t=e.value.replace(/\r\n/g,"\n"),n=e.createTextRange(),n.moveToBookmark(i.getBookmark()),r=e.createTextRange(),r.collapse(!1),n.compareEndPoints("StartToEnd",r)>-1?c=l=o:(c=-n.moveStart("character",-o),c+=t.slice(0,c).split("\n").length-1,n.compareEndPoints("EndToEnd",r)>-1?l=o:(l=-n.moveEnd("character",-o),l+=t.slice(0,l).split("\n").length-1))),a(e,c,l)};var b=function(e,t){return t-(e.value.slice(0,t).split("\r\n").length-1)};u=function(e,t,n){var o=c(e,t,n),r=e.createTextRange(),a=b(e,o.start);r.collapse(!0),o.start==o.end?r.move("character",a):(r.moveEnd("character",b(e,o.end)),r.moveStart("character",a)),r.select()},x=function(e,t){var n=document.selection.createRange();n.collapse(t),n.select()}}l().removeChild(w);var R=function(e,t){var n=T(e,t);try{var o=y(e,t);if(e.value==n.value)return R=y,o}catch(r){}return R=S,e.value=n.value,n};s=function(e,t,n,o){t!=n&&(u(e,t,n),R(e,"")),o&&u(e,t)},d=function(e){u(e,R(e,"").index)},m=function(e){var t=R(e,"");return u(e,t.index),t.replaced};var C=function(e,t,n,o){var r=t+n.length;if(o="string"==typeof o?o.toLowerCase():"",("collapsetoend"==o||"select"==o)&&/[\r\n]/.test(n)){var c=n.replace(/\r\n/g,"\n").replace(/\r/g,"\n");r=t+c.length;var a=t+c.indexOf("\n");"\r\n"==e.value.slice(a,a+2)&&(r+=c.match(/\n/g).length)}switch(o){case"collapsetostart":u(e,t,t);break;case"collapsetoend":u(e,r,r);break;case"select":u(e,t,r)}};v=function(e,t,n,o){u(e,n),R(e,t),"boolean"==typeof o&&(o=o?"collapseToEnd":""),C(e,n,t,o)},f=function(e,t,n){var o=R(e,t);C(e,o.index,t,n||"collapseToEnd")},p=function(e,t,n,o){typeof n==g&&(n=t);var r=i(e),c=R(e,t+r.text+n);C(e,c.index+t.length,r.text,o||"select")},h?window.RangyInputs={getSelection:i,setSelection:u,collapseSelection:x,deleteSelectedText:d,deleteText:s,extractSelectedText:m,insertText:v,replaceSelectedText:f,surroundSelectedText:p}:e.fn.extend({getSelection:E(i,!1),setSelection:E(u,!0),collapseSelection:E(x,!0),deleteSelectedText:E(d,!0),deleteText:E(s,!0),extractSelectedText:E(m,!1),insertText:E(v,!0),replaceSelectedText:E(f,!0),surroundSelectedText:E(p,!0)})})}(window.jQuery);