Skip to content

Commit ef7036a

Browse files
committed
Update jquery-textrange.js to 1.4.0
1 parent e2e6663 commit ef7036a

File tree

2 files changed

+254
-4
lines changed

2 files changed

+254
-4
lines changed

files/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ bundled 3rd-party code :
66

77
Name | Version | URL
88
-----------------|----------|-------------------------------------------
9-
jquery-textrange | 89d11560 | https://github.com/dwieeb/jquery-textrange
9+
jquery-textrange | 1.4.0 | https://github.com/dwieeb/jquery-textrange
1010
Simpletip | 1.3.1 | http://craigsworks.com/projects/simpletip

files/jquery-textrange.js

Lines changed: 253 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,257 @@
11
/**
22
* jquery-textrange
3+
*
34
* A jQuery plugin for getting, setting and replacing the selected text in input fields and textareas.
4-
* See the [wiki](https://github.com/dwieeb/jquery-textrange/wiki) for usage and examples.
5+
* See the [README](https://github.com/dwieeb/jquery-textrange/blob/1.x/README.md) for usage and examples.
56
*
6-
* (c) 2013 Daniel Imhoff <[email protected]> - danielimhoff.com
7-
*/(function(e){var t,n={get:function(e){return r[t].get.apply(this,[e])},set:function(e,n){var i=parseInt(e),s=parseInt(n),o;return typeof e=="undefined"?i=0:e<0&&(i=this.val().length+i),typeof n=="undefined"?o=this.val().length:n>=0?o=i+s:o=this.val().length+s,r[t].set.apply(this,[i,o]),this},setcursor:function(e){return this.textrange("set",e,0)},replace:function(e){return r[t].replace.apply(this,[e]),this},insert:function(e){return this.textrange("replace",e)}},r={xul:{get:function(e){var t={position:this[0].selectionStart,start:this[0].selectionStart,end:this[0].selectionEnd,length:this[0].selectionEnd-this[0].selectionStart,text:this.val().substring(this[0].selectionStart,this[0].selectionEnd)};return typeof e=="undefined"?t:t[e]},set:function(e,t){this[0].selectionStart=e,this[0].selectionEnd=t},replace:function(e){var t=this[0].selectionStart;this.val(this.val().substring(0,this[0].selectionStart)+e+this.val().substring(this[0].selectionEnd,this.val().length)),this[0].selectionStart=t,this[0].selectionEnd=t+e.length}},msie:{get:function(e){var t=document.selection.createRange();if(typeof t=="undefined")return{position:0,start:0,end:this[0].val().length,length:this[0].val().length,text:this.val()};var n=this[0].createTextRange(),r=n.duplicate();return n.moveToBookmark(t.getBookmark()),r.setEndPoint("EndToStart",n),{position:r.text.length,start:r.text.length,end:r.text.length+t.text.length,length:t.text.length,text:t.text}},set:function(e,t){var n=this[0].createTextRange();if(typeof n=="undefined")return this;typeof e!="undefined"&&(n.moveStart("character",e),n.collapse()),typeof t!="undefined"&&n.moveEnd("character",t-e),n.select()},replace:function(e){document.selection.createRange().text=e}}};e.fn.textrange=function(r){typeof t=="undefined"&&(t="selectionStart"in this[0]?"xul":document.selection?"msie":"unknown");if(t==="unknown")return this;document.activeElement!==this[0]&&this[0].focus();if(typeof r=="undefined"||typeof r!="string")return n.get.apply(this);if(typeof n[r]=="function")return n[r].apply(this,Array.prototype.slice.call(arguments,1));e.error("Method "+r+" does not exist in jQuery.textrange")}})(jQuery);
7+
* (c) 2012-2017 Daniel Imhoff <[email protected]> - dwieeb.com
8+
*/
9+
10+
(function(factory) {
11+
12+
if (typeof define === 'function' && define.amd) {
13+
define(['jquery'], factory);
14+
} else if (typeof exports === 'object') {
15+
factory(require('jquery'));
16+
} else {
17+
factory(jQuery);
18+
}
19+
20+
})(function($) {
21+
22+
var browserType,
23+
24+
textrange = {
25+
26+
/**
27+
* $().textrange() or $().textrange('get')
28+
*
29+
* Retrieves an object containing the start and end location of the text range, the length of the range and the
30+
* substring of the range.
31+
*
32+
* @param (optional) property
33+
* @return An object of properties including position, start, end, length, and text or a specific property.
34+
*/
35+
get: function(property) {
36+
return _textrange[browserType].get.apply(this, [property]);
37+
},
38+
39+
/**
40+
* $().textrange('set')
41+
*
42+
* Sets the selected text of an object by specifying the start and length of the selection.
43+
*
44+
* The start and length parameters are identical to PHP's substr() function with the following changes:
45+
* - excluding start will select all the text in the field.
46+
* - passing 0 for length will set the cursor at start. See $().textrange('setcursor')
47+
*
48+
* @param (optional) start
49+
* @param (optional) length
50+
*
51+
* @see https://secure.php.net/manual/en/function.substr.php
52+
*/
53+
set: function(start, length) {
54+
var s = parseInt(start),
55+
l = parseInt(length),
56+
e;
57+
58+
if (typeof start === 'undefined') {
59+
s = 0;
60+
} else if (start < 0) {
61+
s = this[0].value.length + s;
62+
}
63+
64+
if (typeof length !== 'undefined') {
65+
if (length >= 0) {
66+
e = s + l;
67+
} else {
68+
e = this[0].value.length + l;
69+
}
70+
}
71+
72+
_textrange[browserType].set.apply(this, [s, e]);
73+
74+
return this;
75+
},
76+
77+
/**
78+
* $().textrange('setcursor')
79+
*
80+
* Sets the cursor at a position of the text field.
81+
*
82+
* @param position
83+
*/
84+
setcursor: function(position) {
85+
return this.textrange('set', position, 0);
86+
},
87+
88+
/**
89+
* $().textrange('replace')
90+
* Replaces the selected text in the input field or textarea with text.
91+
*
92+
* @param text The text to replace the selection with.
93+
*/
94+
replace: function(text) {
95+
_textrange[browserType].replace.apply(this, [String(text)]);
96+
97+
return this;
98+
},
99+
100+
/**
101+
* Alias for $().textrange('replace')
102+
*/
103+
insert: function(text) {
104+
return this.textrange('replace', text);
105+
}
106+
},
107+
108+
_textrange = {
109+
xul: {
110+
get: function(property) {
111+
var props = {
112+
position: this[0].selectionStart,
113+
start: this[0].selectionStart,
114+
end: this[0].selectionEnd,
115+
length: this[0].selectionEnd - this[0].selectionStart,
116+
text: this.val().substring(this[0].selectionStart, this[0].selectionEnd)
117+
};
118+
119+
return typeof property === 'undefined' ? props : props[property];
120+
},
121+
122+
set: function(start, end) {
123+
if (typeof end === 'undefined') {
124+
end = this[0].value.length;
125+
}
126+
127+
this[0].selectionStart = start;
128+
this[0].selectionEnd = end;
129+
},
130+
131+
replace: function(text) {
132+
var start = this[0].selectionStart;
133+
var end = this[0].selectionEnd;
134+
var val = this.val();
135+
this.val(val.substring(0, start) + text + val.substring(end, val.length));
136+
this[0].selectionStart = start;
137+
this[0].selectionEnd = start + text.length;
138+
}
139+
},
140+
141+
msie: {
142+
get: function(property) {
143+
var range = document.selection.createRange();
144+
145+
if (typeof range === 'undefined') {
146+
var props = {
147+
position: 0,
148+
start: 0,
149+
end: this.val().length,
150+
length: this.val().length,
151+
text: this.val()
152+
};
153+
154+
return typeof property === 'undefined' ? props : props[property];
155+
}
156+
157+
var start = 0;
158+
var end = 0;
159+
var length = this[0].value.length;
160+
var lfValue = this[0].value.replace(/\r\n/g, '\n');
161+
var rangeText = this[0].createTextRange();
162+
var rangeTextEnd = this[0].createTextRange();
163+
rangeText.moveToBookmark(range.getBookmark());
164+
rangeTextEnd.collapse(false);
165+
166+
if (rangeText.compareEndPoints('StartToEnd', rangeTextEnd) === -1) {
167+
start = -rangeText.moveStart('character', -length);
168+
start += lfValue.slice(0, start).split('\n').length - 1;
169+
170+
if (rangeText.compareEndPoints('EndToEnd', rangeTextEnd) === -1) {
171+
end = -rangeText.moveEnd('character', -length);
172+
end += lfValue.slice(0, end).split('\n').length - 1;
173+
} else {
174+
end = length;
175+
}
176+
} else {
177+
start = length;
178+
end = length;
179+
}
180+
181+
var props = {
182+
position: start,
183+
start: start,
184+
end: end,
185+
length: length,
186+
text: range.text
187+
};
188+
189+
return typeof property === 'undefined' ? props : props[property];
190+
},
191+
192+
set: function(start, end) {
193+
var range = this[0].createTextRange();
194+
195+
if (typeof range === 'undefined') {
196+
return;
197+
}
198+
199+
if (typeof end === 'undefined') {
200+
end = this[0].value.length;
201+
}
202+
203+
var ieStart = start - (this[0].value.slice(0, start).split("\r\n").length - 1);
204+
var ieEnd = end - (this[0].value.slice(0, end).split("\r\n").length - 1);
205+
206+
range.collapse(true);
207+
208+
range.moveEnd('character', ieEnd);
209+
range.moveStart('character', ieStart);
210+
211+
range.select();
212+
},
213+
214+
replace: function(text) {
215+
document.selection.createRange().text = text;
216+
}
217+
}
218+
};
219+
220+
$.fn.extend({
221+
textrange: function(arg) {
222+
var method = 'get';
223+
var options = {};
224+
225+
if (typeof this[0] === 'undefined') {
226+
return this;
227+
}
228+
229+
if (typeof arg === 'string') {
230+
method = arg;
231+
} else if (typeof arg === 'object') {
232+
method = arg.method || method;
233+
options = arg;
234+
}
235+
236+
if (typeof browserType === 'undefined') {
237+
browserType = 'selectionStart' in this[0] ? 'xul' : document.selection ? 'msie' : 'unknown';
238+
}
239+
240+
// I don't know how to support this browser. :c
241+
if (browserType === 'unknown') {
242+
return this;
243+
}
244+
245+
// Focus on the element before operating upon it.
246+
if (!options.nofocus && document.activeElement !== this[0]) {
247+
this[0].focus();
248+
}
249+
250+
if (typeof textrange[method] === 'function') {
251+
return textrange[method].apply(this, Array.prototype.slice.call(arguments, 1));
252+
} else {
253+
$.error("Method " + method + " does not exist in jQuery.textrange");
254+
}
255+
}
256+
});
257+
});

0 commit comments

Comments
 (0)