From 89d11560e0e40387a13d2ce03e9903746ccb9ab7 Mon Sep 17 00:00:00 2001
From: Daniel Imhoff
Date: Sat, 7 Dec 2013 11:26:39 -0600
Subject: [PATCH 01/40] putting focus check that ran on all methods in entry
method
---
jquery-textrange.js | 34 +++++++++++-----------------------
1 file changed, 11 insertions(+), 23 deletions(-)
diff --git a/jquery-textrange.js b/jquery-textrange.js
index ff0bdad..609cf5e 100644
--- a/jquery-textrange.js
+++ b/jquery-textrange.js
@@ -6,23 +6,7 @@
* (c) 2013 Daniel Imhoff - danielimhoff.com
*/
(function($) {
-
- /**
- * Sets the focus to the specified element if it isn't focused yet.
- *
- * 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.
- */
- function focus(element) {
- if (document.activeElement !== element) {
- element.focus();
- }
- }
-
+
var browserType,
textrange = {
@@ -115,7 +99,6 @@
_textrange = {
xul: {
get: function(property) {
- focus(this[0]);
var props = {
position: this[0].selectionStart,
start: this[0].selectionStart,
@@ -128,13 +111,11 @@
},
set: function(start, end) {
- focus(this[0]);
this[0].selectionStart = start;
this[0].selectionEnd = end;
},
replace: function(text) {
- focus(this[0]);
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;
@@ -144,7 +125,6 @@
msie: {
get: function(property) {
- focus(this[0]);
var range = document.selection.createRange();
if (typeof range === 'undefined') {
@@ -173,7 +153,6 @@
},
set: function(start, end) {
- focus(this[0]);
var range = this[0].createTextRange();
if (typeof range === 'undefined') {
@@ -193,7 +172,6 @@
},
replace: function(text) {
- focus(this[0]);
document.selection.createRange().text = text;
}
}
@@ -209,6 +187,16 @@
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 e74ffbbd8f2410e28b33e4a44254d6e773524c66 Mon Sep 17 00:00:00 2001
From: Daniel Imhoff
Date: Fri, 1 Aug 2014 19:31:00 -0500
Subject: [PATCH 02/40] Fix for #11
---
jquery-textrange.js | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/jquery-textrange.js b/jquery-textrange.js
index 609cf5e..2229505 100644
--- a/jquery-textrange.js
+++ b/jquery-textrange.js
@@ -143,13 +143,15 @@
rangetext.moveToBookmark(range.getBookmark());
rangetextcopy.setEndPoint('EndToStart', rangetext);
- return {
+ var props = {
position: rangetextcopy.text.length,
start: rangetextcopy.text.length,
end: rangetextcopy.text.length + range.text.length,
length: range.text.length,
text: range.text
};
+
+ return typeof property === 'undefined' ? props : props[property];
},
set: function(start, end) {
From ef7c424d511513d6ea44b2df0625a0beff44d695 Mon Sep 17 00:00:00 2001
From: Daniel Imhoff
Date: Fri, 1 Aug 2014 19:35:00 -0500
Subject: [PATCH 03/40] cutting new release for bugfix #11
---
bower.json | 2 +-
textrange.jquery.json | 2 +-
2 files changed, 2 insertions(+), 2 deletions(-)
diff --git a/bower.json b/bower.json
index 3feb0fc..1e786f8 100644
--- a/bower.json
+++ b/bower.json
@@ -1,6 +1,6 @@
{
"name": "jquery-textrange",
- "version": "1.1.0",
+ "version": "1.1.1",
"main": "jquery-textrange.js",
"dependencies": {
"jquery": ">=1.3"
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 ba1ca29371f309dde71c296961cfc1eb481f5991 Mon Sep 17 00:00:00 2001
From: Daniel Imhoff
Date: Fri, 1 Aug 2014 20:36:11 -0500
Subject: [PATCH 04/40] achieves #10
---
jquery-textrange.js | 10 ++++++++--
1 file changed, 8 insertions(+), 2 deletions(-)
diff --git a/jquery-textrange.js b/jquery-textrange.js
index 2229505..2f12c89 100644
--- a/jquery-textrange.js
+++ b/jquery-textrange.js
@@ -5,7 +5,13 @@
*
* (c) 2013 Daniel Imhoff - danielimhoff.com
*/
-(function($) {
+(function(factory) {
+ if (typeof define === 'function' && define.amd) {
+ define(['jquery'], factory);
+ } else {
+ factory(jQuery);
+ }
+})(function($) {
var browserType,
@@ -209,4 +215,4 @@
$.error("Method " + method + " does not exist in jQuery.textrange");
}
};
-})(jQuery);
+});
From 6c881fc16a7769acaca27d8756cbb5af7567f02d Mon Sep 17 00:00:00 2001
From: Daniel Imhoff
Date: Fri, 1 Aug 2014 20:37:39 -0500
Subject: [PATCH 05/40] cutting new release for feature addition #10
---
bower.json | 2 +-
jquery-textrange.js | 2 +-
textrange.jquery.json | 2 +-
3 files changed, 3 insertions(+), 3 deletions(-)
diff --git a/bower.json b/bower.json
index 1e786f8..a164722 100644
--- a/bower.json
+++ b/bower.json
@@ -1,6 +1,6 @@
{
"name": "jquery-textrange",
- "version": "1.1.1",
+ "version": "1.2.0",
"main": "jquery-textrange.js",
"dependencies": {
"jquery": ">=1.3"
diff --git a/jquery-textrange.js b/jquery-textrange.js
index 2f12c89..561a8a1 100644
--- a/jquery-textrange.js
+++ b/jquery-textrange.js
@@ -3,7 +3,7 @@
* A jQuery plugin for getting, setting and replacing the selected text in input fields and textareas.
* See the [wiki](https://github.com/dwieeb/jquery-textrange/wiki) for usage and examples.
*
- * (c) 2013 Daniel Imhoff - danielimhoff.com
+ * (c) 2012-2014 Daniel Imhoff - danielimhoff.com
*/
(function(factory) {
if (typeof define === 'function' && define.amd) {
diff --git a/textrange.jquery.json b/textrange.jquery.json
index d515ef9..9cd9629 100644
--- a/textrange.jquery.json
+++ b/textrange.jquery.json
@@ -18,7 +18,7 @@
"substring",
"substr"
],
- "version": "1.1.1",
+ "version": "1.2.0",
"author": {
"name": "Daniel Imhoff",
"email": "dwieeb@gmail.com",
From 819e0cc0e7cb6be56749af4da9b8c1bc361c7200 Mon Sep 17 00:00:00 2001
From: Daniel Imhoff
Date: Fri, 1 Aug 2014 20:46:46 -0500
Subject: [PATCH 06/40] Update README.md
---
README.md | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++-----
1 file changed, 67 insertions(+), 6 deletions(-)
diff --git a/README.md b/README.md
index e870129..87f595b 100644
--- a/README.md
+++ b/README.md
@@ -1,14 +1,75 @@
This jQuery plugin is for easily finding the starting and ending positions of selected text in text fields and textareas. It can also be used to set the selection, and replace the selection with given text (or insert text wherever the cursor is in the text field or textarea).
-## Methods
-* [$().textrange('get')](https://github.com/dwieeb/jquery-textrange/wiki/Get)
-* [$().textrange('set')](https://github.com/dwieeb/jquery-textrange/wiki/Set)
-* [$().textrange('setcursor')](https://github.com/dwieeb/jquery-textrange/wiki/SetCursor)
-* [$().textrange('replace')](https://github.com/dwieeb/jquery-textrange/wiki/Replace)
-
## Demo
+
* http://dwieeb.github.com/jquery-textrange/
+## Methods
+
+You can use this method to get all the information on the selected text of an element or a specific bit of information.
+
+### 'get'
+
+##### Get everything
+
+ $('input[name="example"]').textrange('get');
+
+or for short:
+
+ $('input[name="example"]').textrange();
+
+This will return a JSON object with the following information:
+
+ {
+ position: (cursor location in the text field),
+ start: (starting position of the selected text in the text field),
+ end: (ending position of the selected text in the text field),
+ length: (the length of the selected text in the text field),
+ text: (the text that is selected)
+ }
+
+##### Get a particular property
+
+This function can also be used to get a particular property of the object.
+
+For example, this will obtain the starting location of the selected text:
+
+ var start = $('input[name="example"]').textrange('get', 'start');
+
+### 'set'
+
+You can use this method to set the starting and ending locations of the selected text in an element.
+
+It works much like [PHP's substr()](http://php.net/manual/en/function.substr.php) and [Ruby's String#slice](http://ruby-doc.org/core-2.0/String.html#method-i-slice), so if you're familiar with one of those, it should be a breeze! Here are some examples, anyway.
+
+For the following examples, let's say `input[name="example"]` contains the text `abcdef`.
+
+ $('input[name="example"]').textrange('set'); // selects "abcdef" (select all)
+ $('input[name="example"]').textrange('set', 2); // selects "cdef"
+ $('input[name="example"]').textrange('set', 2, 3); // selects "cde"
+ $('input[name="example"]').textrange('set', 2, -2); // selects "cd"
+ $('input[name="example"]').textrange('set', -3); // selects "def"
+ $('input[name="example"]').textrange('set', -2, 1); // selects "e"
+ $('input[name="example"]').textrange('set', -4, -1); // selects "cde"
+
+If you're looking to set the cursor at one specific location, you can use `0` for length, or you can use [$().textrange('setcursor')](https://github.com/dwieeb/jquery-textrange/wiki/SetCursor).
+
+### 'setcursor'
+
+You can use this method to set the location of the cursor in your text field.
+
+To set the cursor at the fifth character position:
+
+ $('input[name="example"]').textrange('setcursor', 5);
+
+### 'replace'
+
+You can use this method to replace the selection with given text.
+
+ $('input[name="example"]').textrange('replace', 'some text');
+
+There is also an `insert` alias for `replace` if you're using this method to insert text at the cursor location. They work the same way.
+
## Minified Version
A minified version of this plugin can be generated using Google's Closure Compiler (preferred).
From d58c9702a16050a6205d5e0a6cb1ccb64d340995 Mon Sep 17 00:00:00 2001
From: Daniel Imhoff
Date: Fri, 1 Aug 2014 20:59:03 -0500
Subject: [PATCH 07/40] Update README.md
---
README.md | 17 +++++++++++++++++
1 file changed, 17 insertions(+)
diff --git a/README.md b/README.md
index 87f595b..42f2eb0 100644
--- a/README.md
+++ b/README.md
@@ -4,6 +4,23 @@ This jQuery plugin is for easily finding the starting and ending positions of se
* http://dwieeb.github.com/jquery-textrange/
+## Include
+
+Works with
+
+
+
+
+Or, with RequireJS (**note**: jquery-textrange can be loaded through any [AMD](https://github.com/amdjs/amdjs-api/blob/master/AMD.md)-compatible Javascript Module Loader):
+
+ requirejs.config({
+ "baseUrl": "lib",
+ "paths": {
+ "jquery": "//code.jquery.com/jquery-latest.min.js",
+ "jquery-textrange": "jquery-plugins/jquery-textrange"
+ }
+ });
+
## Methods
You can use this method to get all the information on the selected text of an element or a specific bit of information.
From 2fdb4d35fa15463b1fa9fd83f3c380043d4fdedf Mon Sep 17 00:00:00 2001
From: Daniel Imhoff
Date: Fri, 1 Aug 2014 21:00:01 -0500
Subject: [PATCH 08/40] Update README.md
---
README.md | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/README.md b/README.md
index 42f2eb0..79b7fb2 100644
--- a/README.md
+++ b/README.md
@@ -6,7 +6,7 @@ This jQuery plugin is for easily finding the starting and ending positions of se
## Include
-Works with
+Include the file directly using
From 0539aae40f7efe39ecfa12b7a5e5b24fb1a51f60 Mon Sep 17 00:00:00 2001
From: Daniel Imhoff
Date: Fri, 1 Aug 2014 21:00:41 -0500
Subject: [PATCH 09/40] Update README.md
---
README.md | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/README.md b/README.md
index 79b7fb2..0a6884a 100644
--- a/README.md
+++ b/README.md
@@ -6,7 +6,7 @@ This jQuery plugin is for easily finding the starting and ending positions of se
## Include
-Include the file directly using
From 6c640d494dc7b19718cce7eafee09dfe3bc1bd05 Mon Sep 17 00:00:00 2001
From: Daniel Imhoff
Date: Sat, 2 Aug 2014 00:13:43 -0500
Subject: [PATCH 10/40] supporting Node/CommonJS and becoming UMD compliant.
achieves #12
---
jquery-textrange.js | 2 ++
1 file changed, 2 insertions(+)
diff --git a/jquery-textrange.js b/jquery-textrange.js
index 561a8a1..4f254c7 100644
--- a/jquery-textrange.js
+++ b/jquery-textrange.js
@@ -8,6 +8,8 @@
(function(factory) {
if (typeof define === 'function' && define.amd) {
define(['jquery'], factory);
+ } else if (typeof exports === 'object') {
+ factory(require('jquery'));
} else {
factory(jQuery);
}
From aeb38b6fef64f1b051216548a22286e426557b2a Mon Sep 17 00:00:00 2001
From: Daniel Imhoff
Date: Sat, 2 Aug 2014 00:15:59 -0500
Subject: [PATCH 11/40] cutting new release for feature addition #12
---
bower.json | 2 +-
textrange.jquery.json | 2 +-
2 files changed, 2 insertions(+), 2 deletions(-)
diff --git a/bower.json b/bower.json
index a164722..e1df092 100644
--- a/bower.json
+++ b/bower.json
@@ -1,6 +1,6 @@
{
"name": "jquery-textrange",
- "version": "1.2.0",
+ "version": "1.3.0",
"main": "jquery-textrange.js",
"dependencies": {
"jquery": ">=1.3"
diff --git a/textrange.jquery.json b/textrange.jquery.json
index 9cd9629..ba24b57 100644
--- a/textrange.jquery.json
+++ b/textrange.jquery.json
@@ -18,7 +18,7 @@
"substring",
"substr"
],
- "version": "1.2.0",
+ "version": "1.3.0",
"author": {
"name": "Daniel Imhoff",
"email": "dwieeb@gmail.com",
From deabac761375587a197832119b828e1f1b4c849e Mon Sep 17 00:00:00 2001
From: Daniel Imhoff
Date: Sat, 9 Aug 2014 18:13:44 -0500
Subject: [PATCH 12/40] Update README.md
---
README.md | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/README.md b/README.md
index 0a6884a..05cc7f0 100644
--- a/README.md
+++ b/README.md
@@ -11,7 +11,7 @@ Include the file directly using `
-Or, with RequireJS (**note**: jquery-textrange can be loaded through any [AMD](https://github.com/amdjs/amdjs-api/blob/master/AMD.md)-compatible Javascript Module Loader):
+Or, with RequireJS (**note**: jquery-textrange can be loaded through any [UMD](https://github.com/umdjs/umd/blob/master/README.md)-compatible Javascript Module Loader):
requirejs.config({
"baseUrl": "lib",
@@ -69,7 +69,7 @@ For the following examples, let's say `input[name="example"]` contains the text
$('input[name="example"]').textrange('set', -2, 1); // selects "e"
$('input[name="example"]').textrange('set', -4, -1); // selects "cde"
-If you're looking to set the cursor at one specific location, you can use `0` for length, or you can use [$().textrange('setcursor')](https://github.com/dwieeb/jquery-textrange/wiki/SetCursor).
+If you're looking to set the cursor at one specific location, you can use `0` for length, or you can use `$().textrange('setcursor')` (see below).
### 'setcursor'
From b9cfebb97d7d3ba171488e63a3565409e4b6873a Mon Sep 17 00:00:00 2001
From: Daniel Imhoff
Date: Tue, 12 Aug 2014 16:17:12 -0500
Subject: [PATCH 13/40] Update README.md
---
README.md | 2 ++
1 file changed, 2 insertions(+)
diff --git a/README.md b/README.md
index 05cc7f0..265294b 100644
--- a/README.md
+++ b/README.md
@@ -1,5 +1,7 @@
This jQuery plugin is for easily finding the starting and ending positions of selected text in text fields and textareas. It can also be used to set the selection, and replace the selection with given text (or insert text wherever the cursor is in the text field or textarea).
+* jquery-textrange may not work with WYSIWYG editors. See [issue #3](https://github.com/dwieeb/jquery-textrange/issues/3) and [this question](http://wordpress.stackexchange.com/questions/105961/insert-text-a-cursor-position-in-to-tinymce-text-editor).
+
## Demo
* http://dwieeb.github.com/jquery-textrange/
From e77c14dc75f5738013c550ccbe9979bfa0d4185d Mon Sep 17 00:00:00 2001
From: Daniel Imhoff
Date: Tue, 12 Aug 2014 16:22:03 -0500
Subject: [PATCH 14/40] Update README.md
---
README.md | 84 ++++++++++++++++++++++++++++++++++---------------------
1 file changed, 52 insertions(+), 32 deletions(-)
diff --git a/README.md b/README.md
index 265294b..0475102 100644
--- a/README.md
+++ b/README.md
@@ -10,18 +10,22 @@ This jQuery plugin is for easily finding the starting and ending positions of se
Include the file directly using `
-
+```html
+
+
+```
Or, with RequireJS (**note**: jquery-textrange can be loaded through any [UMD](https://github.com/umdjs/umd/blob/master/README.md)-compatible Javascript Module Loader):
- requirejs.config({
- "baseUrl": "lib",
- "paths": {
- "jquery": "//code.jquery.com/jquery-latest.min.js",
- "jquery-textrange": "jquery-plugins/jquery-textrange"
- }
- });
+```javascript
+requirejs.config({
+ "baseUrl": "lib",
+ "paths": {
+ "jquery": "//code.jquery.com/jquery-latest.min.js",
+ "jquery-textrange": "jquery-plugins/jquery-textrange"
+ }
+});
+```
## Methods
@@ -31,21 +35,27 @@ You can use this method to get all the information on the selected text of an el
##### Get everything
- $('input[name="example"]').textrange('get');
+```javascript
+$('input[name="example"]').textrange('get');
+```
or for short:
- $('input[name="example"]').textrange();
+```javascript
+$('input[name="example"]').textrange();
+```
This will return a JSON object with the following information:
- {
- position: (cursor location in the text field),
- start: (starting position of the selected text in the text field),
- end: (ending position of the selected text in the text field),
- length: (the length of the selected text in the text field),
- text: (the text that is selected)
- }
+```javascript
+{
+ "position": // cursor location in the text field)
+ "start": //starting position of the selected text in the text field
+ "end": // ending position of the selected text in the text field
+ "length": // the length of the selected text in the text field
+ "text": // the text that is selected
+}
+```
##### Get a particular property
@@ -53,7 +63,9 @@ This function can also be used to get a particular property of the object.
For example, this will obtain the starting location of the selected text:
- var start = $('input[name="example"]').textrange('get', 'start');
+```javascript
+var start = $('input[name="example"]').textrange('get', 'start');
+```
### 'set'
@@ -63,13 +75,15 @@ It works much like [PHP's substr()](http://php.net/manual/en/function.substr.php
For the following examples, let's say `input[name="example"]` contains the text `abcdef`.
- $('input[name="example"]').textrange('set'); // selects "abcdef" (select all)
- $('input[name="example"]').textrange('set', 2); // selects "cdef"
- $('input[name="example"]').textrange('set', 2, 3); // selects "cde"
- $('input[name="example"]').textrange('set', 2, -2); // selects "cd"
- $('input[name="example"]').textrange('set', -3); // selects "def"
- $('input[name="example"]').textrange('set', -2, 1); // selects "e"
- $('input[name="example"]').textrange('set', -4, -1); // selects "cde"
+```javascript
+$('input[name="example"]').textrange('set'); // selects "abcdef" (select all)
+$('input[name="example"]').textrange('set', 2); // selects "cdef"
+$('input[name="example"]').textrange('set', 2, 3); // selects "cde"
+$('input[name="example"]').textrange('set', 2, -2); // selects "cd"
+$('input[name="example"]').textrange('set', -3); // selects "def"
+$('input[name="example"]').textrange('set', -2, 1); // selects "e"
+$('input[name="example"]').textrange('set', -4, -1); // selects "cde"
+```
If you're looking to set the cursor at one specific location, you can use `0` for length, or you can use `$().textrange('setcursor')` (see below).
@@ -79,20 +93,26 @@ You can use this method to set the location of the cursor in your text field.
To set the cursor at the fifth character position:
- $('input[name="example"]').textrange('setcursor', 5);
+```javascript
+$('input[name="example"]').textrange('setcursor', 5);
+```
### 'replace'
You can use this method to replace the selection with given text.
- $('input[name="example"]').textrange('replace', 'some text');
+```javascript
+$('input[name="example"]').textrange('replace', 'some text');
+```
There is also an `insert` alias for `replace` if you're using this method to insert text at the cursor location. They work the same way.
## Minified Version
A minified version of this plugin can be generated using Google's Closure Compiler (preferred).
- wget http://closure-compiler.googlecode.com/files/compiler-latest.tar.gz
- tar -zvxf compiler-latest.tar.gz compiler.jar
- wget https://raw.github.com/dwieeb/jquery-textrange/master/jquery-textrange.js
- java -jar compiler.jar --js jquery-textrange.js --js_output_file jquery-textrange.min.js
+```bash
+wget http://closure-compiler.googlecode.com/files/compiler-latest.tar.gz
+tar -zvxf compiler-latest.tar.gz compiler.jar
+wget https://raw.github.com/dwieeb/jquery-textrange/master/jquery-textrange.js
+java -jar compiler.jar --js jquery-textrange.js --js_output_file jquery-textrange.min.js
+```
From ffc8eb12e9cd4f531384ffb173ef7dadc040bec3 Mon Sep 17 00:00:00 2001
From: Daniel Imhoff
Date: Tue, 12 Aug 2014 16:35:38 -0500
Subject: [PATCH 15/40] Style changes and updating comment block
---
jquery-textrange.js | 18 +++++++++---------
1 file changed, 9 insertions(+), 9 deletions(-)
diff --git a/jquery-textrange.js b/jquery-textrange.js
index 4f254c7..42676c3 100644
--- a/jquery-textrange.js
+++ b/jquery-textrange.js
@@ -1,18 +1,23 @@
/**
* jquery-textrange
* A jQuery plugin for getting, setting and replacing the selected text in input fields and textareas.
- * See the [wiki](https://github.com/dwieeb/jquery-textrange/wiki) for usage and examples.
+ * See the [README](https://github.com/dwieeb/jquery-textrange/blob/1.x/README.md) for usage and examples.
*
* (c) 2012-2014 Daniel Imhoff - danielimhoff.com
*/
+
(function(factory) {
+
if (typeof define === 'function' && define.amd) {
define(['jquery'], factory);
- } else if (typeof exports === 'object') {
+ }
+ else if (typeof exports === 'object') {
factory(require('jquery'));
- } else {
+ }
+ else {
factory(jQuery);
}
+
})(function($) {
var browserType,
@@ -197,12 +202,7 @@
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.
+ // Focus on the element before operating upon it.
if (document.activeElement !== this[0]) {
this[0].focus();
}
From 70ba6b71d4f878cf1aa7f7fce7bcad7d5fa40691 Mon Sep 17 00:00:00 2001
From: Daniel Imhoff
Date: Tue, 12 Aug 2014 16:36:00 -0500
Subject: [PATCH 16/40] Fixing undefined error if no selection is made.
---
jquery-textrange.js | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/jquery-textrange.js b/jquery-textrange.js
index 42676c3..a746e45 100644
--- a/jquery-textrange.js
+++ b/jquery-textrange.js
@@ -193,6 +193,10 @@
};
$.fn.textrange = function(method) {
+ if (typeof this[0] === 'undefined') {
+ return this;
+ }
+
if (typeof browserType === 'undefined') {
browserType = 'selectionStart' in this[0] ? 'xul' : document.selection ? 'msie' : 'unknown';
}
From f20fe4b816573a00a5abfcf1756454c580cf4d68 Mon Sep 17 00:00:00 2001
From: Daniel Imhoff
Date: Tue, 12 Aug 2014 16:42:04 -0500
Subject: [PATCH 17/40] optimizing replace method for xul browsers
---
jquery-textrange.js | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
diff --git a/jquery-textrange.js b/jquery-textrange.js
index a746e45..0355766 100644
--- a/jquery-textrange.js
+++ b/jquery-textrange.js
@@ -129,8 +129,11 @@
},
replace: function(text) {
+ text = String(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));
+ var end = this[0].selectionEnd;
+ var val = this.val();
+ this.val(val.substring(0, start) + text + val.substring(end, val.length));
this[0].selectionStart = start;
this[0].selectionEnd = start + text.length;
}
From 3ed8577b37e778d3c3e0440dd435b1df11b0cf14 Mon Sep 17 00:00:00 2001
From: Daniel Imhoff
Date: Tue, 12 Aug 2014 16:43:54 -0500
Subject: [PATCH 18/40] casting in entry method instead of xul browser method
---
jquery-textrange.js | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/jquery-textrange.js b/jquery-textrange.js
index 0355766..8130a2e 100644
--- a/jquery-textrange.js
+++ b/jquery-textrange.js
@@ -96,7 +96,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;
},
@@ -129,7 +129,6 @@
},
replace: function(text) {
- text = String(text);
var start = this[0].selectionStart;
var end = this[0].selectionEnd;
var val = this.val();
From 841083880909f2e8818f3bc4da7c11a40b0be6e5 Mon Sep 17 00:00:00 2001
From: Daniel Imhoff
Date: Tue, 12 Aug 2014 16:46:19 -0500
Subject: [PATCH 19/40] cutting new release for a few bug fixes
---
bower.json | 2 +-
textrange.jquery.json | 2 +-
2 files changed, 2 insertions(+), 2 deletions(-)
diff --git a/bower.json b/bower.json
index e1df092..565c5a3 100644
--- a/bower.json
+++ b/bower.json
@@ -1,6 +1,6 @@
{
"name": "jquery-textrange",
- "version": "1.3.0",
+ "version": "1.3.1",
"main": "jquery-textrange.js",
"dependencies": {
"jquery": ">=1.3"
diff --git a/textrange.jquery.json b/textrange.jquery.json
index ba24b57..b1c18eb 100644
--- a/textrange.jquery.json
+++ b/textrange.jquery.json
@@ -18,7 +18,7 @@
"substring",
"substr"
],
- "version": "1.3.0",
+ "version": "1.3.1",
"author": {
"name": "Daniel Imhoff",
"email": "dwieeb@gmail.com",
From b694b21f8d07beba134d1d370cfa59dbec124359 Mon Sep 17 00:00:00 2001
From: Daniel Imhoff
Date: Wed, 13 Aug 2014 23:12:33 -0500
Subject: [PATCH 20/40] fixing focus-on-hover issue with demo
---
demo.html | 6 +++++-
1 file changed, 5 insertions(+), 1 deletion(-)
diff --git a/demo.html b/demo.html
index a9c7774..a9ac206 100644
--- a/demo.html
+++ b/demo.html
@@ -31,7 +31,11 @@
$('h1').css('border-color', orange ? 'transparent' : '#fa4500');
}, 500);
- $('#textarea').bind('updateInfo keyup mousedown mousemove mouseup', function() {
+ $('#textarea').bind('updateInfo keyup mousedown mousemove mouseup', function(event) {
+ if (document.activeElement !== $(this)[0]) {
+ return;
+ }
+
var range = $(this).textrange();
$('#info .property').each(function() {
From 5656761976e555ece575da9dc4b6d0dd1578fad0 Mon Sep 17 00:00:00 2001
From: Daniel Imhoff
Date: Sun, 21 Sep 2014 23:29:25 -0500
Subject: [PATCH 21/40] Adding package.json for npm installation
---
package.json | 39 +++++++++++++++++++++++++++++++++++++++
1 file changed, 39 insertions(+)
create mode 100644 package.json
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..8f42224
--- /dev/null
+++ b/package.json
@@ -0,0 +1,39 @@
+{
+ "name": "jquery-textrange",
+ "version": "1.3.1",
+ "description": "A jQuery plugin for getting, setting and replacing the selected text in input fields and textareas.",
+ "main": "jquery-textrange.js",
+ "dependencies": {
+ "jquery": "*"
+ },
+ "devDependencies": {},
+ "scripts": {
+ "test": "echo \"Error: no test specified\" && exit 1"
+ },
+ "repository": {
+ "type": "git",
+ "url": "https://github.com/dwieeb/jquery-textrange.git"
+ },
+ "keywords": [
+ "jquery",
+ "caret",
+ "cursor",
+ "field",
+ "highlight",
+ "input",
+ "range",
+ "replace",
+ "select",
+ "selection",
+ "substr",
+ "substring",
+ "text",
+ "textarea",
+ "textrange"
+ ],
+ "author": "Daniel Imhoff",
+ "license": "MIT",
+ "bugs": {
+ "url": "https://github.com/dwieeb/jquery-textrange/issues"
+ }
+}
From b14db5f8144bc9fd91c148a22839a1674073211c Mon Sep 17 00:00:00 2001
From: Daniel Imhoff
Date: Mon, 22 Sep 2014 00:17:39 -0500
Subject: [PATCH 22/40] Fixing possible problem with fallback
---
jquery-textrange.js | 8 +++++---
1 file changed, 5 insertions(+), 3 deletions(-)
diff --git a/jquery-textrange.js b/jquery-textrange.js
index 8130a2e..85b3fc4 100644
--- a/jquery-textrange.js
+++ b/jquery-textrange.js
@@ -143,13 +143,15 @@
var range = document.selection.createRange();
if (typeof range === 'undefined') {
- return {
+ var props = {
position: 0,
start: 0,
- end: this[0].val().length,
- length: this[0].val().length,
+ end: this.val().length,
+ length: this.val().length,
text: this.val()
};
+
+ return typeof property === 'undefined' ? props : props[property];
}
var rangetext = this[0].createTextRange();
From 08146a538103635fb4e3e1cb196a92b5f7be75ec Mon Sep 17 00:00:00 2001
From: Daniel Imhoff
Date: Tue, 11 Nov 2014 00:08:52 -0600
Subject: [PATCH 23/40] fixing text selection for msie 5 through 8. achieves
#15
---
jquery-textrange.js | 35 ++++++++++++++++++++++++++---------
1 file changed, 26 insertions(+), 9 deletions(-)
diff --git a/jquery-textrange.js b/jquery-textrange.js
index 85b3fc4..bca94bb 100644
--- a/jquery-textrange.js
+++ b/jquery-textrange.js
@@ -154,17 +154,34 @@
return typeof property === 'undefined' ? props : props[property];
}
- var rangetext = this[0].createTextRange();
- var rangetextcopy = rangetext.duplicate();
-
- rangetext.moveToBookmark(range.getBookmark());
- rangetextcopy.setEndPoint('EndToStart', rangetext);
+ var start = 0;
+ var end = 0;
+ var length = this[0].value.length;
+ var lfValue = this[0].value.replace(/\r\n/g, '\n');
+ var rangeText = this[0].createTextRange();
+ var rangeTextEnd = this[0].createTextRange();
+ rangeText.moveToBookmark(range.getBookmark());
+ rangeTextEnd.collapse(false);
+
+ if (rangeText.compareEndPoints('StartToEnd', rangeTextEnd) === -1) {
+ start = -rangeText.moveStart('character', -length);
+ start += lfValue.slice(0, start).split('\n').length - 1;
+
+ if (rangeText.compareEndPoints('EndToEnd', rangeTextEnd) === -1) {
+ end = -rangeText.moveEnd('character', -length);
+ end += lfValue.slice(0, end).split('\n').length - 1;
+ } else {
+ end = length;
+ }
+ } else {
+ end = length;
+ }
var props = {
- position: rangetextcopy.text.length,
- start: rangetextcopy.text.length,
- end: rangetextcopy.text.length + range.text.length,
- length: range.text.length,
+ position: start,
+ start: start,
+ end: end,
+ length: length,
text: range.text
};
From f4e37fb744ab9e44fe497646594d8612f14078e8 Mon Sep 17 00:00:00 2001
From: Daniel Imhoff
Date: Tue, 11 Nov 2014 00:09:16 -0600
Subject: [PATCH 24/40] adding browser support section to readme
---
README.md | 8 ++++++++
1 file changed, 8 insertions(+)
diff --git a/README.md b/README.md
index 0475102..2ee905c 100644
--- a/README.md
+++ b/README.md
@@ -6,6 +6,14 @@ This jQuery plugin is for easily finding the starting and ending positions of se
* http://dwieeb.github.com/jquery-textrange/
+## Browser Support
+
+Tested in the following major browsers:
+
+* Internet Explorer 5.5+
+* Firefox 2+
+* Chrome
+
## Include
Include the file directly using `
+
```
-Or, with RequireJS (**note**: jquery-textrange can be loaded through any [UMD](https://github.com/umdjs/umd/blob/master/README.md)-compatible Javascript Module Loader):
-
-```javascript
-requirejs.config({
- "baseUrl": "lib",
- "paths": {
- "jquery": "//code.jquery.com/jquery-latest.min.js",
- "jquery-textrange": "jquery-plugins/jquery-textrange"
- }
-});
-```
+:memo: *note*: jquery-textrange can be loaded through any
+[UMD](https://github.com/umdjs/umd/blob/master/README.md)-compatible Javascript
+Module Loader.
## Methods
-You can use this method to get all the information on the selected text of an element or a specific bit of information.
+You can use this method to get all the information on the selected text of an
+element or a specific bit of information.
### 'get'
@@ -77,11 +74,15 @@ var start = $('input[name="example"]').textrange('get', 'start');
### 'set'
-You can use this method to set the starting and ending locations of the selected text in an element.
+You can use this method to set the starting and ending locations of the
+selected text in an element.
-It works much like [PHP's substr()](http://php.net/manual/en/function.substr.php) and [Ruby's String#slice](http://ruby-doc.org/core-2.0/String.html#method-i-slice), so if you're familiar with one of those, it should be a breeze! Here are some examples, anyway.
+It works much like [PHP's
+substr()](https://secure.php.net/manual/en/function.substr.php), so if you're
+familiar with that, it should be a breeze! Here are some examples, anyway.
-For the following examples, let's say `input[name="example"]` contains the text `abcdef`.
+For the following examples, let's say `input[name="example"]` contains the text
+`abcdef`.
```javascript
$('input[name="example"]').textrange('set'); // selects "abcdef" (select all)
@@ -93,7 +94,8 @@ $('input[name="example"]').textrange('set', -2, 1); // selects "e"
$('input[name="example"]').textrange('set', -4, -1); // selects "cde"
```
-If you're looking to set the cursor at one specific location, you can use `0` for length, or you can use `$().textrange('setcursor')` (see below).
+If you're looking to set the cursor at one specific location, you can use `0`
+for length, or you can use `$().textrange('setcursor')` (see below).
### 'setcursor'
@@ -107,18 +109,11 @@ $('input[name="example"]').textrange('setcursor', 5);
### 'replace'
-You can use this method to replace the selection with given text.
+You can use this method to replace the selection with given text.
```javascript
$('input[name="example"]').textrange('replace', 'some text');
```
-There is also an `insert` alias for `replace` if you're using this method to insert text at the cursor location. They work the same way.
-
-## Minified Version
-A minified version of this plugin can be generated using UglifyJS during your build process or via CLI tools:
-
-```bash
-npm i -g uglify-js
-uglifyjs jquery-textrange.js > jquery-textrange.min.js
-```
+There is also an `insert` alias for `replace` if you're using this method to
+insert text at the cursor location. They work the same way.
diff --git a/demo.html b/demo.html
index a9ac206..af378c5 100644
--- a/demo.html
+++ b/demo.html
@@ -3,7 +3,7 @@
jquery-textrange
-
+
body, h1 { margin:0; }
@@ -74,7 +74,7 @@
-created by Daniel Imhoff
+created by Daniel Imhoff
See the wiki for all features and documentation.