From 3e633839c73448c3c6fdb92978aba4b4578bd6b3 Mon Sep 17 00:00:00 2001
From: callumacrae Selection width: 0
-
+
+
diff --git a/dom/selection/selection.js b/dom/selection/selection.js
index fb8fd365..5c11b4bb 100644
--- a/dom/selection/selection.js
+++ b/dom/selection/selection.js
@@ -40,7 +40,8 @@ getElementsSelection = function(el, win){
}
return {
start: startPos,
- end : endPos
+ end : endPos,
+ width : endPos - startPos
};
},
// Text selection works differently for selection in an input vs
@@ -56,9 +57,9 @@ getSelection = function(el){
&& document.activeElement != el
&& el.selectionStart == el.selectionEnd
&& el.selectionStart == 0){
- return {start: el.value.length, end: el.value.length};
+ return {start: el.value.length, end: el.value.length, width: 0};
}
- return {start: el.selectionStart, end: el.selectionEnd}
+ return {start: el.selectionStart, end: el.selectionEnd, width: el.selectionEnd - el.selectionStart};
}
// getSelection means a 'normal' element in a standards browser.
else if(win.getSelection){
@@ -75,7 +76,8 @@ getSelection = function(el){
var start = r.text.length
return {
start: start,
- end: start + real.text.length
+ end: start + real.text.length,
+ width: real.text.length
}
}
// This works on textareas and other elements
@@ -104,7 +106,7 @@ getSelection = function(el){
return res
}
}catch(e){
- return {start: el.value.length, end: el.value.length};
+ return {start: el.value.length, end: el.value.length, width: 0};
}
}
},
@@ -199,7 +201,7 @@ getCharElement = function( elems , range, len ) {
* Set or retrieve the currently selected text range. It works on all elements:
*
* $('#text').selection(8, 12)
- * $('#text').selection() // -> { start : 8, end : 12 }
+ * $('#text').selection() // -> { start : 8, end : 12, width: 4 }
*
* @param {Number} [start] Start position of the selection range
* @param {Number} [end] End position of the selection range
@@ -208,6 +210,7 @@ getCharElement = function( elems , range, len ) {
*
* - __start__ - The number of characters from the start of the element to the start of the selection.
* - __end__ - The number of characters from the start of the element to the end of the selection.
+ * - __width__ - The width of the selection range.
*
* when no arguments are passed.
*/
diff --git a/dom/selection/selection.md b/dom/selection/selection.md
index 717c0c1a..c1fd5b0e 100644
--- a/dom/selection/selection.md
+++ b/dom/selection/selection.md
@@ -17,12 +17,13 @@ Using `jQuery.fn.selection` by providing a start and end offset:
A call without any parameters will return the current selection:
- $('#text').selection() // -> { start : 8, end : 12 }
+ $('#text').selection() // -> { start : 8, end : 12, width : 4 }
Where the returned object contains:
- __start__ - The number of characters from the start of the element to the start of the selection.
- __end__ - The number of characters from the start of the element to the end of the selection.
+- __width__ - The width of the selection range.
The selected text can be retrieved like this:
diff --git a/dom/selection/selection_test.js b/dom/selection/selection_test.js
index 28886444..9319c976 100644
--- a/dom/selection/selection_test.js
+++ b/dom/selection/selection_test.js
@@ -29,7 +29,7 @@ test("getCharElement", function(){
for(var i =0; i< res.length; i++){
- same(res[i],{start: 1, end: 5},types[i])
+ same(res[i],{start: 1, end: 5, width: 4},types[i])
}
start();
From 6993e3a453f0595771eae71fb6ec51dc27a5ca25 Mon Sep 17 00:00:00 2001
From: callumacrae