forked from timdown/rangyinputs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rangyinputs-jquery.js
17 lines (16 loc) · 4.22 KB
/
rangyinputs-jquery.js
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){"object"==typeof module&&"object"==typeof module.exports?module.exports=e:e(jQuery,window,document)}(function(e,t,n){function r(e,t){var n=typeof e[t];return"function"===n||!("object"!=n||!e[t])||"unknown"==n}function o(e,t){return typeof e[t]!=y}function a(e,t){return!("object"!=typeof e[t]||!e[t])}function c(e){t.console&&t.console.log&&t.console.log("RangyInputs not supported in your browser. Reason: "+e)}function l(e,t,n){return 0>t&&(t+=e.value.length),typeof n==y&&(n=t),0>n&&(n+=e.value.length),{start:t,end:n}}function i(e,t,n){return{start:t,end:n,length:n-t,text:e.value.slice(t,n)}}function s(){return a(n,"body")?n.body:n.getElementsByTagName("body")[0]}var u,d,f,v,p,x,g,m,h,y="undefined";e(n).ready(function(){function t(e,t){var n=e.value,r=u(e),o=r.start;return{value:n.slice(0,o)+t+n.slice(r.end),index:o,replaced:r.text}}function T(e,t){e.focus();var r=u(e);return d(e,r.start,r.end),""==t?n.execCommand("delete",!1,null):n.execCommand("insertText",!1,t),{replaced:r.text,index:r.start}}function E(e,n){e.focus();var r=t(e,n);return e.value=r.value,r}function S(e,t){return function(){var n=this.jquery?this[0]:this,r=n.nodeName.toLowerCase();if(1==n.nodeType&&("textarea"==r||"input"==r&&/^(?:text|email|number|search|tel|url|password)$/i.test(n.type))){var o=[n].concat(Array.prototype.slice.call(arguments)),a=e.apply(this,o);if(!t)return a}return t?this:void 0}}var b=n.createElement("textarea");if(s().appendChild(b),o(b,"selectionStart")&&o(b,"selectionEnd"))u=function(e){var t=e.selectionStart,n=e.selectionEnd;return i(e,t,n)},d=function(e,t,n){var r=l(e,t,n);e.selectionStart=r.start,e.selectionEnd=r.end},h=function(e,t){t?e.selectionEnd=e.selectionStart:e.selectionStart=e.selectionEnd};else{if(!(r(b,"createTextRange")&&a(n,"selection")&&r(n.selection,"createRange")))return s().removeChild(b),void c("No means of finding text input caret position");u=function(e){var t,r,o,a,c=0,l=0,s=n.selection.createRange();return s&&s.parentElement()==e&&(o=e.value.length,t=e.value.replace(/\r\n/g,"\n"),r=e.createTextRange(),r.moveToBookmark(s.getBookmark()),a=e.createTextRange(),a.collapse(!1),r.compareEndPoints("StartToEnd",a)>-1?c=l=o:(c=-r.moveStart("character",-o),c+=t.slice(0,c).split("\n").length-1,r.compareEndPoints("EndToEnd",a)>-1?l=o:(l=-r.moveEnd("character",-o),l+=t.slice(0,l).split("\n").length-1))),i(e,c,l)};var R=function(e,t){return t-(e.value.slice(0,t).split("\r\n").length-1)};d=function(e,t,n){var r=l(e,t,n),o=e.createTextRange(),a=R(e,r.start);o.collapse(!0),r.start==r.end?o.move("character",a):(o.moveEnd("character",R(e,r.end)),o.moveStart("character",a)),o.select()},h=function(e,t){var r=n.selection.createRange();r.collapse(t),r.select()}}s().removeChild(b);var w=function(e,n){var r=t(e,n);try{var o=T(e,n);if(e.value==r.value)return w=T,o}catch(a){}return w=E,e.value=r.value,r};v=function(e,t,n,r){t!=n&&(d(e,t,n),w(e,"")),r&&d(e,t)},f=function(e){d(e,w(e,"").index)},m=function(e){var t=w(e,"");return d(e,t.index),t.replaced};var k=function(e,t,n,r){var o=t+n.length;if(r="string"==typeof r?r.toLowerCase():"",("collapsetoend"==r||"select"==r)&&/[\r\n]/.test(n)){var a=n.replace(/\r\n/g,"\n").replace(/\r/g,"\n");o=t+a.length;var c=t+a.indexOf("\n");"\r\n"==e.value.slice(c,c+2)&&(o+=a.match(/\n/g).length)}switch(r){case"collapsetostart":d(e,t,t);break;case"collapsetoend":d(e,o,o);break;case"select":d(e,t,o)}};p=function(e,t,n,r){d(e,n),w(e,t),"boolean"==typeof r&&(r=r?"collapseToEnd":""),k(e,n,t,r)},x=function(e,t,n){var r=w(e,t);k(e,r.index,t,n||"collapseToEnd")},g=function(e,t,n,r){typeof n==y&&(n=t);var o=u(e),a=w(e,t+o.text+n);k(e,a.index+t.length,o.text,r||"select")},e.fn.extend({getSelection:S(u,!1),setSelection:S(d,!0),collapseSelection:S(h,!0),deleteSelectedText:S(f,!0),deleteText:S(v,!0),extractSelectedText:S(m,!1),insertText:S(p,!0),replaceSelectedText:S(x,!0),surroundSelectedText:S(g,!0)})})});