Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added width property to selection plugin. #11

Merged
merged 2 commits into from Jun 8, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 10 additions & 5 deletions dom/selection/selection.html
Expand Up @@ -32,24 +32,29 @@
id='multi'>Select Across Multiple Elements</a></td>
<td><div id='2'>012<div>3<span>4</span>5</div></div></td>
</tr>
</table>
</table>
<p>Selection width: <span id="selectionwidth">0</span></p>
<script type='text/javascript'
src='../../../steal/steal.js'></script>
<script type='text/javascript'>
steal('jquery/dom/selection', function(){

$('#textarea').click(function(){
$('textarea').selection(1,5);
$('textarea').selection(1,5).mouseup();
})
$('#input').click(function(){
$('input').selection(1,5);
$('input').selection(1,5).mouseup();
})
$('#p').click(function(){
$('#1').selection(1,5);
$('#1').selection(1,5).mouseup();
})
$('#multi').click(function(){
$('#2').selection(1,5);
$('#2').selection(1,5).mouseup();
})

$('textarea,input,#1,#2').mouseup(function(){
$('#selectionwidth').text($(this).selection().width);
});
});
</script>
</body>
Expand Down
15 changes: 9 additions & 6 deletions dom/selection/selection.js
Expand Up @@ -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
Expand All @@ -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){
Expand All @@ -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
Expand Down Expand Up @@ -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};
}
}
},
Expand Down Expand Up @@ -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
Expand All @@ -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.
*/
Expand Down
3 changes: 2 additions & 1 deletion dom/selection/selection.md
Expand Up @@ -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:

Expand Down
16 changes: 3 additions & 13 deletions dom/selection/selection_test.js
Expand Up @@ -11,25 +11,15 @@ test("getCharElement", function(){
stop();
setTimeout(function(){
var types = ['textarea','#inp','#1','#2'];
for(var i =0; i< types.length; i++){
//console.log(types[i])
$(types[i]).selection(1,5);
}
/*
$('textarea').selection(1,5);
$('input').selection(1,5);
$('#1').selection(1,5);
$('#2').selection(1,5);
*/
var res = [];
for(var i =0; i< types.length; i++){
res.push( $(types[i]).selection() );
}



for(var i =0; i< res.length; i++){
same(res[i],{start: 1, end: 5},types[i])
for(var i = 0; i < types.length; i++){
$(types[i]).selection(1, 5);
same($(types[i]).selection(), {start: 1, end: 5, width: 4}, types[i]);
}

start();
Expand Down