sendkeys: Undo formatting and unnecessary changes

This partially reverts the following commits:
  * 04e9fc3a2f
  * 3d8452b143
  * 07182bb716
  * 68ed9b219f
This commit is contained in:
Richard Hansen 2021-08-20 19:33:56 -04:00 committed by SamTV12345
parent 75591c9946
commit 7147195ac6

View file

@ -24,281 +24,281 @@
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
(function($){ (function(){
bililiteRange = function(el, debug){ bililiteRange = function(el, debug){
var ret; var ret;
if (debug){ if (debug){
ret = new NothingRange(); // Easier to force it to use the no-selection type than to try to find an old browser ret = new NothingRange(); // Easier to force it to use the no-selection type than to try to find an old browser
}else if (document.selection && !document.addEventListener){ }else if (document.selection && !document.addEventListener){
// Internet Explorer 8 and lower // Internet Explorer 8 and lower
ret = new IERange(); ret = new IERange();
}else if (window.getSelection && el.setSelectionRange){ }else if (window.getSelection && el.setSelectionRange){
// Standards. Element is an input or textarea // Standards. Element is an input or textarea
ret = new InputRange(); ret = new InputRange();
}else if (window.getSelection){ }else if (window.getSelection){
// Standards, with any other kind of element // Standards, with any other kind of element
ret = new W3CRange() ret = new W3CRange()
}else{ }else{
// doesn't support selection // doesn't support selection
ret = new NothingRange(); ret = new NothingRange();
} }
ret._el = el; ret._el = el;
ret._doc = el.ownerDocument; ret._doc = el.ownerDocument;
ret._win = 'defaultView' in ret._doc ? ret._doc.defaultView : ret._doc.parentWindow; ret._win = 'defaultView' in ret._doc ? ret._doc.defaultView : ret._doc.parentWindow;
ret._textProp = textProp(el); ret._textProp = textProp(el);
ret._bounds = [0, ret.length()]; ret._bounds = [0, ret.length()];
return ret; return ret;
} }
function textProp(el){ function textProp(el){
// returns the property that contains the text of the element // returns the property that contains the text of the element
if (typeof el.value != 'undefined') return 'value'; if (typeof el.value != 'undefined') return 'value';
if (typeof el.text != 'undefined') return 'text'; if (typeof el.text != 'undefined') return 'text';
if (typeof el.textContent != 'undefined') return 'textContent'; if (typeof el.textContent != 'undefined') return 'textContent';
return 'innerText'; return 'innerText';
} }
// base class // base class
function Range(){} function Range(){}
Range.prototype = { Range.prototype = {
length: function() { length: function() {
return this._el[this._textProp].replace(/\r/g, '').length; // need to correct for IE's CrLf weirdness return this._el[this._textProp].replace(/\r/g, '').length; // need to correct for IE's CrLf weirdness
}, },
bounds: function(s){ bounds: function(s){
if (s === 'all'){ if (s === 'all'){
this._bounds = [0, this.length()]; this._bounds = [0, this.length()];
}else if (s === 'start'){ }else if (s === 'start'){
this._bounds = [0, 0]; this._bounds = [0, 0];
}else if (s === 'end'){ }else if (s === 'end'){
this._bounds = [this.length(), this.length()]; this._bounds = [this.length(), this.length()];
}else if (s === 'selection'){ }else if (s === 'selection'){
this.bounds ('all'); // first select the whole thing for constraining this.bounds ('all'); // first select the whole thing for constraining
this._bounds = this._nativeSelection(); this._bounds = this._nativeSelection();
}else if (s){ }else if (s){
this._bounds = s; // don't error check now; the element may change at any moment, so constrain it when we need it. this._bounds = s; // don't error check now; the element may change at any moment, so constrain it when we need it.
}else{ }else{
var b = [ var b = [
Math.max(0, Math.min (this.length(), this._bounds[0])), Math.max(0, Math.min (this.length(), this._bounds[0])),
Math.max(0, Math.min (this.length(), this._bounds[1])) Math.max(0, Math.min (this.length(), this._bounds[1]))
]; ];
return b; // need to constrain it to fit return b; // need to constrain it to fit
} }
return this; // allow for chaining return this; // allow for chaining
}, },
select: function(){ select: function(){
this._nativeSelect(this._nativeRange(this.bounds())); this._nativeSelect(this._nativeRange(this.bounds()));
return this; // allow for chaining return this; // allow for chaining
}, },
text: function(text, select){ text: function(text, select){
if (arguments.length){ if (arguments.length){
this._nativeSetText(text, this._nativeRange(this.bounds())); this._nativeSetText(text, this._nativeRange(this.bounds()));
if (select == 'start'){ if (select == 'start'){
this.bounds ([this._bounds[0], this._bounds[0]]); this.bounds ([this._bounds[0], this._bounds[0]]);
this.select(); this.select();
}else if (select == 'end'){ }else if (select == 'end'){
this.bounds ([this._bounds[0]+text.length, this._bounds[0]+text.length]); this.bounds ([this._bounds[0]+text.length, this._bounds[0]+text.length]);
this.select(); this.select();
}else if (select == 'all'){ }else if (select == 'all'){
this.bounds ([this._bounds[0], this._bounds[0]+text.length]); this.bounds ([this._bounds[0], this._bounds[0]+text.length]);
this.select(); this.select();
} }
return this; // allow for chaining return this; // allow for chaining
}else{ }else{
return this._nativeGetText(this._nativeRange(this.bounds())); return this._nativeGetText(this._nativeRange(this.bounds()));
} }
}, },
insertEOL: function (){ insertEOL: function (){
this._nativeEOL(); this._nativeEOL();
this._bounds = [this._bounds[0]+1, this._bounds[0]+1]; // move past the EOL marker this._bounds = [this._bounds[0]+1, this._bounds[0]+1]; // move past the EOL marker
return this; return this;
} }
}; };
function IERange(){} function IERange(){}
IERange.prototype = new Range(); IERange.prototype = new Range();
IERange.prototype._nativeRange = function (bounds){ IERange.prototype._nativeRange = function (bounds){
var rng; var rng;
if (this._el.tagName == 'INPUT'){ if (this._el.tagName == 'INPUT'){
// IE 8 is very inconsistent; textareas have createTextRange but it doesn't work // IE 8 is very inconsistent; textareas have createTextRange but it doesn't work
rng = this._el.createTextRange(); rng = this._el.createTextRange();
}else{ }else{
rng = this._doc.body.createTextRange (); rng = this._doc.body.createTextRange ();
rng.moveToElementText(this._el); rng.moveToElementText(this._el);
} }
if (bounds){ if (bounds){
if (bounds[1] < 0) bounds[1] = 0; // IE tends to run elements out of bounds if (bounds[1] < 0) bounds[1] = 0; // IE tends to run elements out of bounds
if (bounds[0] > this.length()) bounds[0] = this.length(); if (bounds[0] > this.length()) bounds[0] = this.length();
if (bounds[1] < rng.text.replace(/\r/g, '').length){ // correct for IE's CrLf wierdness if (bounds[1] < rng.text.replace(/\r/g, '').length){ // correct for IE's CrLf wierdness
// block-display elements have an invisible, uncounted end of element marker, so we move an extra one and use the current length of the range // block-display elements have an invisible, uncounted end of element marker, so we move an extra one and use the current length of the range
rng.moveEnd ('character', -1); rng.moveEnd ('character', -1);
rng.moveEnd ('character', bounds[1]-rng.text.replace(/\r/g, '').length); rng.moveEnd ('character', bounds[1]-rng.text.replace(/\r/g, '').length);
} }
if (bounds[0] > 0) rng.moveStart('character', bounds[0]); if (bounds[0] > 0) rng.moveStart('character', bounds[0]);
} }
return rng; return rng;
}; };
IERange.prototype._nativeSelect = function (rng){ IERange.prototype._nativeSelect = function (rng){
rng.select(); rng.select();
}; };
IERange.prototype._nativeSelection = function (){ IERange.prototype._nativeSelection = function (){
// returns [start, end] for the selection constrained to be in element // returns [start, end] for the selection constrained to be in element
var rng = this._nativeRange(); // range of the element to constrain to var rng = this._nativeRange(); // range of the element to constrain to
var len = this.length(); var len = this.length();
if (this._doc.selection.type != 'Text') return [0,0]; // append to the end if (this._doc.selection.type != 'Text') return [len, len]; // append to the end
var sel = this._doc.selection.createRange(); var sel = this._doc.selection.createRange();
try{ try{
return [ return [
iestart(sel, rng), iestart(sel, rng),
ieend (sel, rng) ieend (sel, rng)
]; ];
}catch (e){ }catch (e){
// IE gets upset sometimes about comparing text to input elements, but the selections cannot overlap, so make a best guess // IE gets upset sometimes about comparing text to input elements, but the selections cannot overlap, so make a best guess
return (sel.parentElement().sourceIndex < this._el.sourceIndex) ? [0,0] : [len, len]; return (sel.parentElement().sourceIndex < this._el.sourceIndex) ? [0,0] : [len, len];
} }
}; };
IERange.prototype._nativeGetText = function (rng){ IERange.prototype._nativeGetText = function (rng){
return rng.text.replace(/\r/g, ''); // correct for IE's CrLf weirdness return rng.text.replace(/\r/g, ''); // correct for IE's CrLf weirdness
}; };
IERange.prototype._nativeSetText = function (text, rng){ IERange.prototype._nativeSetText = function (text, rng){
rng.text = text; rng.text = text;
}; };
IERange.prototype._nativeEOL = function(){ IERange.prototype._nativeEOL = function(){
if (typeof this._el.value != 'undefined'){ if (typeof this._el.value != 'undefined'){
this.text('\n'); // for input and textarea, insert it straight this.text('\n'); // for input and textarea, insert it straight
}else{ }else{
this._nativeRange(this.bounds()).pasteHTML('<br/>'); this._nativeRange(this.bounds()).pasteHTML('<br/>');
} }
}; };
// IE internals // IE internals
function iestart(rng, constraint){ function iestart(rng, constraint){
// returns the position (in character) of the start of rng within constraint. If it's not in constraint, returns 0 if it's before, length if it's after // returns the position (in character) of the start of rng within constraint. If it's not in constraint, returns 0 if it's before, length if it's after
var len = constraint.text.replace(/\r/g, '').length; // correct for IE's CrLf wierdness var len = constraint.text.replace(/\r/g, '').length; // correct for IE's CrLf wierdness
if (rng.compareEndPoints ('StartToStart', constraint) <= 0) return 0; // at or before the beginning if (rng.compareEndPoints ('StartToStart', constraint) <= 0) return 0; // at or before the beginning
if (rng.compareEndPoints ('StartToEnd', constraint) >= 0) return len; if (rng.compareEndPoints ('StartToEnd', constraint) >= 0) return len;
for (var i = 0; rng.compareEndPoints ('StartToStart', constraint) > 0; ++i, rng.moveStart('character', -1)); for (var i = 0; rng.compareEndPoints ('StartToStart', constraint) > 0; ++i, rng.moveStart('character', -1));
return i; return i;
} }
function ieend (rng, constraint){ function ieend (rng, constraint){
// returns the position (in character) of the end of rng within constraint. If it's not in constraint, returns 0 if it's before, length if it's after // returns the position (in character) of the end of rng within constraint. If it's not in constraint, returns 0 if it's before, length if it's after
var len = constraint.text.replace(/\r/g, '').length; // correct for IE's CrLf wierdness var len = constraint.text.replace(/\r/g, '').length; // correct for IE's CrLf wierdness
if (rng.compareEndPoints ('EndToEnd', constraint) >= 0) return len; // at or after the end if (rng.compareEndPoints ('EndToEnd', constraint) >= 0) return len; // at or after the end
if (rng.compareEndPoints ('EndToStart', constraint) <= 0) return 0; if (rng.compareEndPoints ('EndToStart', constraint) <= 0) return 0;
for (var i = 0; rng.compareEndPoints ('EndToStart', constraint) > 0; ++i, rng.moveEnd('character', -1)); for (var i = 0; rng.compareEndPoints ('EndToStart', constraint) > 0; ++i, rng.moveEnd('character', -1));
return i; return i;
} }
// an input element in a standards document. "Native Range" is just the bounds array // an input element in a standards document. "Native Range" is just the bounds array
function InputRange(){} function InputRange(){}
InputRange.prototype = new Range(); InputRange.prototype = new Range();
InputRange.prototype._nativeRange = function(bounds) { InputRange.prototype._nativeRange = function(bounds) {
return bounds || [0, this.length()]; return bounds || [0, this.length()];
}; };
InputRange.prototype._nativeSelect = function (rng){ InputRange.prototype._nativeSelect = function (rng){
this._el.setSelectionRange(rng[0], rng[1]); this._el.setSelectionRange(rng[0], rng[1]);
}; };
InputRange.prototype._nativeSelection = function(){ InputRange.prototype._nativeSelection = function(){
return [this._el.selectionStart, this._el.selectionEnd]; return [this._el.selectionStart, this._el.selectionEnd];
}; };
InputRange.prototype._nativeGetText = function(rng){ InputRange.prototype._nativeGetText = function(rng){
return this._el.value.substring(rng[0], rng[1]); return this._el.value.substring(rng[0], rng[1]);
}; };
InputRange.prototype._nativeSetText = function(text, rng){ InputRange.prototype._nativeSetText = function(text, rng){
var val = this._el.value; var val = this._el.value;
this._el.value = val.substring(0, rng[0]) + text + val.substring(rng[1]); this._el.value = val.substring(0, rng[0]) + text + val.substring(rng[1]);
}; };
InputRange.prototype._nativeEOL = function(){ InputRange.prototype._nativeEOL = function(){
this.text('\n'); this.text('\n');
}; };
function W3CRange(){} function W3CRange(){}
W3CRange.prototype = new Range(); W3CRange.prototype = new Range();
W3CRange.prototype._nativeRange = function (bounds){ W3CRange.prototype._nativeRange = function (bounds){
var rng = this._doc.createRange(); var rng = this._doc.createRange();
rng.selectNodeContents(this._el); rng.selectNodeContents(this._el);
if (bounds){ if (bounds){
w3cmoveBoundary (rng, bounds[0], true, this._el); w3cmoveBoundary (rng, bounds[0], true, this._el);
rng.collapse (true); rng.collapse (true);
w3cmoveBoundary (rng, bounds[1]-bounds[0], false, this._el); w3cmoveBoundary (rng, bounds[1]-bounds[0], false, this._el);
} }
return rng; return rng;
}; };
W3CRange.prototype._nativeSelect = function (rng){ W3CRange.prototype._nativeSelect = function (rng){
this._win.getSelection().removeAllRanges(); this._win.getSelection().removeAllRanges();
this._win.getSelection().addRange (rng); this._win.getSelection().addRange (rng);
}; };
W3CRange.prototype._nativeSelection = function (){ W3CRange.prototype._nativeSelection = function (){
// returns [start, end] for the selection constrained to be in element // returns [start, end] for the selection constrained to be in element
var rng = this._nativeRange(); // range of the element to constrain to var rng = this._nativeRange(); // range of the element to constrain to
if (this._win.getSelection().rangeCount == 0) return [this.length(), this.length()]; // append to the end if (this._win.getSelection().rangeCount == 0) return [this.length(), this.length()]; // append to the end
var sel = this._win.getSelection().getRangeAt(0); var sel = this._win.getSelection().getRangeAt(0);
return [ return [
w3cstart(sel, rng), w3cstart(sel, rng),
w3cend (sel, rng) w3cend (sel, rng)
]; ];
} }
W3CRange.prototype._nativeGetText = function (rng){ W3CRange.prototype._nativeGetText = function (rng){
return rng.toString(); return rng.toString();
}; };
W3CRange.prototype._nativeSetText = function (text, rng){ W3CRange.prototype._nativeSetText = function (text, rng){
rng.deleteContents(); rng.deleteContents();
rng.insertNode (this._doc.createTextNode(text)); rng.insertNode (this._doc.createTextNode(text));
this._el.normalize(); // merge the text with the surrounding text this._el.normalize(); // merge the text with the surrounding text
}; };
W3CRange.prototype._nativeEOL = function(){ W3CRange.prototype._nativeEOL = function(){
var rng = this._nativeRange(this.bounds()); var rng = this._nativeRange(this.bounds());
rng.deleteContents(); rng.deleteContents();
var br = this._doc.createElement('br'); var br = this._doc.createElement('br');
br.setAttribute ('_moz_dirty', ''); // for Firefox br.setAttribute ('_moz_dirty', ''); // for Firefox
rng.insertNode (br); rng.insertNode (br);
rng.insertNode (this._doc.createTextNode('\n')); rng.insertNode (this._doc.createTextNode('\n'));
rng.collapse (false); rng.collapse (false);
}; };
// W3C internals // W3C internals
function nextnode (node, root){ function nextnode (node, root){
// in-order traversal // in-order traversal
// we've already visited node, so get kids then siblings // we've already visited node, so get kids then siblings
if (node.firstChild) return node.firstChild; if (node.firstChild) return node.firstChild;
if (node.nextSibling) return node.nextSibling; if (node.nextSibling) return node.nextSibling;
if (node===root) return null; if (node===root) return null;
while (node.parentNode){ while (node.parentNode){
// get uncles // get uncles
node = node.parentNode; node = node.parentNode;
if (node == root) return null; if (node == root) return null;
if (node.nextSibling) return node.nextSibling; if (node.nextSibling) return node.nextSibling;
} }
return null; return null;
} }
function w3cmoveBoundary (rng, n, bStart, el){ function w3cmoveBoundary (rng, n, bStart, el){
// move the boundary (bStart == true ? start : end) n characters forward, up to the end of element el. Forward only! // move the boundary (bStart == true ? start : end) n characters forward, up to the end of element el. Forward only!
// if the start is moved after the end, then an exception is raised // if the start is moved after the end, then an exception is raised
if (n <= 0) return; if (n <= 0) return;
var node = rng[bStart ? 'startContainer' : 'endContainer']; var node = rng[bStart ? 'startContainer' : 'endContainer'];
if (node.nodeType == 3){ if (node.nodeType == 3){
// we may be starting somewhere into the text // we may be starting somewhere into the text
n += rng[bStart ? 'startOffset' : 'endOffset']; n += rng[bStart ? 'startOffset' : 'endOffset'];
} }
while (node){ while (node){
if (node.nodeType == 3){ if (node.nodeType == 3){
if (n <= node.nodeValue.length){ if (n <= node.nodeValue.length){
rng[bStart ? 'setStart' : 'setEnd'](node, n); rng[bStart ? 'setStart' : 'setEnd'](node, n);
// special case: if we end next to a <br>, include that node. // special case: if we end next to a <br>, include that node.
if (n == node.nodeValue.length){ if (n == node.nodeValue.length){
// skip past zero-length text nodes // skip past zero-length text nodes
for (var next = nextnode (node, el); next && next.nodeType==3 && next.nodeValue.length == 0; next = nextnode(next, el)){ for (var next = nextnode (node, el); next && next.nodeType==3 && next.nodeValue.length == 0; next = nextnode(next, el)){
rng[bStart ? 'setStartAfter' : 'setEndAfter'](next); rng[bStart ? 'setStartAfter' : 'setEndAfter'](next);
} }
if (next && next.nodeType == 1 && next.nodeName == "BR") rng[bStart ? 'setStartAfter' : 'setEndAfter'](next); if (next && next.nodeType == 1 && next.nodeName == "BR") rng[bStart ? 'setStartAfter' : 'setEndAfter'](next);
} }
return; return;
}else{ }else{
rng[bStart ? 'setStartAfter' : 'setEndAfter'](node); // skip past this one rng[bStart ? 'setStartAfter' : 'setEndAfter'](node); // skip past this one
n -= node.nodeValue.length; // and eat these characters n -= node.nodeValue.length; // and eat these characters
} }
} }
node = nextnode (node, el); node = nextnode (node, el);
} }
} }
var START_TO_START = 0; // from the w3c definitions var START_TO_START = 0; // from the w3c definitions
var START_TO_END = 1; var START_TO_END = 1;
@ -311,42 +311,42 @@ var END_TO_START = 3;
// * Range.START_TO_END compares the start boundary-point of sourceRange to the end boundary-point of range. // * Range.START_TO_END compares the start boundary-point of sourceRange to the end boundary-point of range.
// * Range.START_TO_START compares the start boundary-point of sourceRange to the start boundary-point of range. // * Range.START_TO_START compares the start boundary-point of sourceRange to the start boundary-point of range.
function w3cstart(rng, constraint){ function w3cstart(rng, constraint){
if (rng.compareBoundaryPoints (START_TO_START, constraint) <= 0) return 0; // at or before the beginning if (rng.compareBoundaryPoints (START_TO_START, constraint) <= 0) return 0; // at or before the beginning
if (rng.compareBoundaryPoints (END_TO_START, constraint) >= 0) return constraint.toString().length; if (rng.compareBoundaryPoints (END_TO_START, constraint) >= 0) return constraint.toString().length;
rng = rng.cloneRange(); // don't change the original rng = rng.cloneRange(); // don't change the original
rng.setEnd (constraint.endContainer, constraint.endOffset); // they now end at the same place rng.setEnd (constraint.endContainer, constraint.endOffset); // they now end at the same place
return constraint.toString().length - rng.toString().length; return constraint.toString().length - rng.toString().length;
} }
function w3cend (rng, constraint){ function w3cend (rng, constraint){
if (rng.compareBoundaryPoints (END_TO_END, constraint) >= 0) return constraint.toString().length; // at or after the end if (rng.compareBoundaryPoints (END_TO_END, constraint) >= 0) return constraint.toString().length; // at or after the end
if (rng.compareBoundaryPoints (START_TO_END, constraint) <= 0) return 0; if (rng.compareBoundaryPoints (START_TO_END, constraint) <= 0) return 0;
rng = rng.cloneRange(); // don't change the original rng = rng.cloneRange(); // don't change the original
rng.setStart (constraint.startContainer, constraint.startOffset); // they now start at the same place rng.setStart (constraint.startContainer, constraint.startOffset); // they now start at the same place
return rng.toString().length; return rng.toString().length;
} }
function NothingRange(){} function NothingRange(){}
NothingRange.prototype = new Range(); NothingRange.prototype = new Range();
NothingRange.prototype._nativeRange = function(bounds) { NothingRange.prototype._nativeRange = function(bounds) {
return bounds || [0,this.length()]; return bounds || [0,this.length()];
}; };
NothingRange.prototype._nativeSelect = function (rng){ // do nothing NothingRange.prototype._nativeSelect = function (rng){ // do nothing
}; };
NothingRange.prototype._nativeSelection = function(){ NothingRange.prototype._nativeSelection = function(){
return [0,0]; return [0,0];
}; };
NothingRange.prototype._nativeGetText = function (rng){ NothingRange.prototype._nativeGetText = function (rng){
return this._el[this._textProp].substring(rng[0], rng[1]); return this._el[this._textProp].substring(rng[0], rng[1]);
}; };
NothingRange.prototype._nativeSetText = function (text, rng){ NothingRange.prototype._nativeSetText = function (text, rng){
var val = this._el[this._textProp]; var val = this._el[this._textProp];
this._el[this._textProp] = val.substring(0, rng[0]) + text + val.substring(rng[1]); this._el[this._textProp] = val.substring(0, rng[0]) + text + val.substring(rng[1]);
}; };
NothingRange.prototype._nativeEOL = function(){ NothingRange.prototype._nativeEOL = function(){
this.text('\n'); this.text('\n');
}; };
})(jQuery); })();
// insert characters in a textarea or text input field // insert characters in a textarea or text input field
// special characters are enclosed in {}; use {{} for the { character itself // special characters are enclosed in {}; use {{} for the { character itself
@ -378,90 +378,89 @@ NothingRange.prototype._nativeEOL = function(){
(function($){ (function($){
$.fn.sendkeys = function (x, opts){ $.fn.sendkeys = function (x, opts){
return this.each( function(){ return this.each( function(){
var localkeys = $.extend({}, opts, $(this).data('sendkeys')); // allow for element-specific key functions var localkeys = $.extend({}, opts, $(this).data('sendkeys')); // allow for element-specific key functions
// most elements to not keep track of their selection when they lose focus, so we have to do it for them // most elements to not keep track of their selection when they lose focus, so we have to do it for them
var rng = $.data (this, 'sendkeys.selection'); var rng = $.data (this, 'sendkeys.selection');
if (!rng){ if (!rng){
rng = bililiteRange(this).bounds('selection'); rng = bililiteRange(this).bounds('selection');
$.data(this, 'sendkeys.selection', rng); $.data(this, 'sendkeys.selection', rng);
$(this).bind('mouseup.sendkeys', function(){ $(this).bind('mouseup.sendkeys', function(){
// we have to update the saved range. The routines here update the bounds with each press, but actual keypresses and mouseclicks do not // we have to update the saved range. The routines here update the bounds with each press, but actual keypresses and mouseclicks do not
$.data(this, 'sendkeys.selection').bounds('selection'); $.data(this, 'sendkeys.selection').bounds('selection');
}).bind('keyup.sendkeys', function(evt){ }).bind('keyup.sendkeys', function(evt){
// restore the selection if we got here with a tab (a click should select what was clicked on) // restore the selection if we got here with a tab (a click should select what was clicked on)
if (evt.which == 9){ if (evt.which == 9){
// there's a flash of selection when we restore the focus, but I don't know how to avoid that // there's a flash of selection when we restore the focus, but I don't know how to avoid that.
$.data(this, 'sendkeys.selection').select(); $.data(this, 'sendkeys.selection').select();
}else{ }else{
$.data(this, 'sendkeys.selection').bounds('selection'); $.data(this, 'sendkeys.selection').bounds('selection');
} }
}); });
} }
this.focus(); this.focus();
if (typeof x === 'undefined') return; // no string, so we just set up the event handlers if (typeof x === 'undefined') return; // no string, so we just set up the event handlers
$.data(this, 'sendkeys.originalText', rng.text()); $.data(this, 'sendkeys.originalText', rng.text());
x.replace(/\n/g, '{enter}'). // turn line feeds into explicit break insertions x.replace(/\n/g, '{enter}'). // turn line feeds into explicit break insertions
replace(/{[^}]*}|[^{]+/g, function(s){ replace(/{[^}]*}|[^{]+/g, function(s){
(localkeys[s] || $.fn.sendkeys.defaults[s] || $.fn.sendkeys.defaults.simplechar)(rng, s); (localkeys[s] || $.fn.sendkeys.defaults[s] || $.fn.sendkeys.defaults.simplechar)(rng, s);
}); });
$(this).trigger({type: 'sendkeys', which: x}); $(this).trigger({type: 'sendkeys', which: x});
}); });
}; // sendkeys }; // sendkeys
// add the functions publicly so they can be overridden // add the functions publicly so they can be overridden
$.fn.sendkeys.defaults = { $.fn.sendkeys.defaults = {
simplechar: function (rng, s){ simplechar: function (rng, s){
rng.text(s, 'end'); rng.text(s, 'end');
for (var i =0; i < s.length; ++i){ for (var i =0; i < s.length; ++i){
var x = s.charCodeAt(i); var x = s.charCodeAt(i);
// a bit of cheating: rng._el is the element associated with rng. // a bit of cheating: rng._el is the element associated with rng.
$(rng._el).trigger({type: 'keypress', keyCode: x, which: x, charCode: x}); $(rng._el).trigger({type: 'keypress', keyCode: x, which: x, charCode: x});
} }
}, },
'{{}': function (rng){ '{{}': function (rng){
$.fn.sendkeys.defaults.simplechar (rng, '{') $.fn.sendkeys.defaults.simplechar (rng, '{')
}, },
'{enter}': function (rng){ '{enter}': function (rng){
rng.insertEOL(); rng.insertEOL();
rng.select(); rng.select();
$(rng._el).trigger( $(rng._el).trigger({type: 'keypress', keyCode: 13, which: 13, charCode: 13, code: 'Enter', key: 'Enter'});
{type: 'keypress', keyCode: 13, which: 13, charCode: 13, code: 'Enter', key: 'Enter'}); },
}, '{backspace}': function (rng){
'{backspace}': function (rng){ var b = rng.bounds();
var b = rng.bounds(); if (b[0] == b[1]) rng.bounds([b[0]-1, b[0]]); // no characters selected; it's just an insertion point. Remove the previous character
if (b[0] == b[1]) rng.bounds([b[0]-1, b[0]]); // no characters selected; it's just an insertion point. Remove the previous character rng.text('', 'end'); // delete the characters and update the selection
rng.text('', 'end'); // delete the characters and update the selection },
}, '{del}': function (rng){
'{del}': function (rng){ var b = rng.bounds();
var b = rng.bounds(); if (b[0] == b[1]) rng.bounds([b[0], b[0]+1]); // no characters selected; it's just an insertion point. Remove the next character
if (b[0] == b[1]) rng.bounds([b[0], b[0]+1]); // no characters selected; it's just an insertion point. Remove the next character rng.text('', 'end'); // delete the characters and update the selection
rng.text('', 'end'); // delete the characters and update the selection },
}, '{rightarrow}': function (rng){
'{rightarrow}': function (rng){ var b = rng.bounds();
var b = rng.bounds(); if (b[0] == b[1]) ++b[1]; // no characters selected; it's just an insertion point. Move to the right
if (b[0] == b[1]) ++b[1]; // no characters selected; it's just an insertion point. Move to the right rng.bounds([b[1], b[1]]).select();
rng.bounds([b[1], b[1]]).select(); },
}, '{leftarrow}': function (rng){
'{leftarrow}': function (rng){ var b = rng.bounds();
var b = rng.bounds(); if (b[0] == b[1]) --b[0]; // no characters selected; it's just an insertion point. Move to the left
if (b[0] == b[1]) --b[0]; // no characters selected; it's just an insertion point. Move to the left rng.bounds([b[0], b[0]]).select();
rng.bounds([b[0], b[0]]).select(); },
}, '{selectall}' : function (rng){
'{selectall}' : function (rng){ rng.bounds('all').select();
rng.bounds('all').select(); },
}, '{selection}': function (rng){
'{selection}': function (rng){ $.fn.sendkeys.defaults.simplechar(rng, $.data(rng._el, 'sendkeys.originalText'));
$.fn.sendkeys.defaults.simplechar(rng, $.data(rng._el, 'sendkeys.originalText')); },
}, '{mark}' : function (rng){
'{mark}' : function (rng){ var bounds = rng.bounds();
var bounds = rng.bounds(); $(rng._el).one('sendkeys', function(){
$(rng._el).one('sendkeys', function(){ // set up the event listener to change the selection after the sendkeys is done
// set up the event listener to change the selection after the sendkeys is done rng.bounds(bounds).select();
rng.bounds(bounds).select(); });
}); }
}
}; };
})(jQuery) })(jQuery)