From fe28c0bef169cc168d215f3e1b0ad6b945af87f1 Mon Sep 17 00:00:00 2001 From: Doug Ross Date: Fri, 14 Feb 2014 11:46:01 -0500 Subject: [PATCH 1/3] fix minor issues --- jquery-textrange.js | 39 +++++++++++++++++++++++---------------- 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/jquery-textrange.js b/jquery-textrange.js index 609cf5e..8916f1a 100644 --- a/jquery-textrange.js +++ b/jquery-textrange.js @@ -6,7 +6,6 @@ * (c) 2013 Daniel Imhoff - danielimhoff.com */ (function($) { - var browserType, textrange = { @@ -83,7 +82,7 @@ * @param text The text to replace the selection with. */ replace: function(text) { - _textrange[browserType].replace.apply(this, [text]); + _textrange[browserType].replace.apply(this, [String(text)]); return this; }, @@ -99,6 +98,7 @@ _textrange = { xul: { get: function(property) { + this[0].focus(); var props = { position: this[0].selectionStart, start: this[0].selectionStart, @@ -111,20 +111,33 @@ }, set: function(start, end) { + this[0].focus(); this[0].selectionStart = start; this[0].selectionEnd = end; }, replace: function(text) { - var start = this[0].selectionStart; - this.val(this.val().substring(0, this[0].selectionStart) + text + this.val().substring(this[0].selectionEnd, this.val().length)); - this[0].selectionStart = start; - this[0].selectionEnd = start + text.length; + this[0].focus(); + var val, end, start, length; + val = this.val(); + end = this[0].selectionEnd; + start = this[0].selectionStart; + length = end - start; + this.val(val.substring(0, start) + String(text) + val.substring(end, val.length)); + if (length) { + this[0].selectionStart = start; + this[0].selectionEnd = start + text.length; + } else { + this[0].selectionStart = start + text.length; + this[0].selectionEnd = start + text.length; + } } }, msie: { get: function(property) { + this[0].focus(); + var range = document.selection.createRange(); if (typeof range === 'undefined') { @@ -153,6 +166,8 @@ }, set: function(start, end) { + this[0].focus(); + var range = this[0].createTextRange(); if (typeof range === 'undefined') { @@ -172,6 +187,8 @@ }, replace: function(text) { + this[0].focus(); + document.selection.createRange().text = text; } } @@ -187,16 +204,6 @@ return this; } - // Prevents unpleasant behaviour for textareas in IE: - // If you have a textarea which is too wide to be displayed entirely and therfore has to be scrolled horizontally, - // then typing one character after another will scroll the page automatically to the right at the moment you reach - // the right border of the visible part. But calling the focus function causes the page to be scrolled to the left - // edge of the textarea. Immediately after that jump to the left side, the content is scrolled back to the cursor - // position, which leads to a flicker page every time you type a character. - if (document.activeElement !== this[0]) { - this[0].focus(); - } - if (typeof method === 'undefined' || typeof method !== 'string') { return textrange.get.apply(this); } From a3213b5a0c0dafb1eb54489b43c05fef137556a8 Mon Sep 17 00:00:00 2001 From: Doug Ross Date: Fri, 14 Feb 2014 11:50:09 -0500 Subject: [PATCH 2/3] Update textrange.jquery.json --- textrange.jquery.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/textrange.jquery.json b/textrange.jquery.json index f3aa7d9..d515ef9 100644 --- a/textrange.jquery.json +++ b/textrange.jquery.json @@ -18,7 +18,7 @@ "substring", "substr" ], - "version": "1.1.0", + "version": "1.1.1", "author": { "name": "Daniel Imhoff", "email": "dwieeb@gmail.com", From 904a575d18c32e58044d9491a1044893bbb28f17 Mon Sep 17 00:00:00 2001 From: Doug Ross Date: Sat, 1 Mar 2014 01:49:40 -0500 Subject: [PATCH 3/3] fix text.length is undefined --- jquery-textrange.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/jquery-textrange.js b/jquery-textrange.js index 8916f1a..59e586c 100644 --- a/jquery-textrange.js +++ b/jquery-textrange.js @@ -126,10 +126,10 @@ this.val(val.substring(0, start) + String(text) + val.substring(end, val.length)); if (length) { this[0].selectionStart = start; - this[0].selectionEnd = start + text.length; + this[0].selectionEnd = start + String(text).length; } else { - this[0].selectionStart = start + text.length; - this[0].selectionEnd = start + text.length; + this[0].selectionStart = start + String(text).length; + this[0].selectionEnd = start + String(text).length; } } },