Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit5898b19

Browse files
committed
Fix bug in recursion so that when multiple child nodes match the criteria, only the first match is returned
1 parentf7d3fec commit5898b19

File tree

4 files changed

+65
-21
lines changed

4 files changed

+65
-21
lines changed

‎dist/jquery.caret.js‎

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,29 +40,35 @@ EditableCaret = (function() {
4040
}
4141

4242
EditableCaret.prototype.setPos=function(pos){
43-
varfn,offset,sel;
43+
varfn,found,offset,sel;
4444
if(sel=oWindow.getSelection()){
4545
offset=0;
46+
found=false;
4647
(fn=function(pos,parent){
47-
varnode,range,_i,_len,_ref;
48+
varnode,range,_i,_len,_ref,_results;
4849
_ref=parent.childNodes;
50+
_results=[];
4951
for(_i=0,_len=_ref.length;_i<_len;_i++){
5052
node=_ref[_i];
53+
if(found){
54+
break;
55+
}
5156
if(node.nodeType===3){
5257
if(offset+node.length>=pos){
58+
found=true;
5359
range=oDocument.createRange();
5460
range.setStart(node,pos-offset);
5561
sel.removeAllRanges();
5662
sel.addRange(range);
57-
returntrue;
58-
}
59-
offset+=node.length;
60-
}else{
61-
if(fn(pos,node)){
6263
break;
64+
}else{
65+
_results.push(offset+=node.length);
6366
}
67+
}else{
68+
_results.push(fn(pos,node));
6469
}
6570
}
71+
return_results;
6672
})(pos,this.domInputor);
6773
}
6874
returnthis.domInputor;

‎spec/javascripts/caret.js‎

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,28 @@ describe('jquery.caret', function() {
3232
// $('#inputor').caret('offset', fixPos);
3333
});
3434

35+
describe('Edge case from Gmail',function(){
36+
beforeEach(function(){
37+
varcontent=''
38+
+'<div id="inputor" contenteditable="true">'
39+
+'Hello '
40+
+'<span>just</span> want'
41+
+'<ul><li>Hi</li></ul>'
42+
+'<div>---</div>'
43+
+'</div>';
44+
varfixture=setFixtures(content);
45+
$inputor=fixture.find('#inputor');
46+
});
47+
48+
it('sets the caret position when two child nodes match the condition',function(){
49+
$inputor.caret('pos',16);
50+
varselection=window.getSelection();
51+
expect(selection.anchorNode.nodeValue).toBe('Hi');
52+
expect(selection.anchorOffset).toBe(1);
53+
expect($inputor.caret('pos')).toBe(16);
54+
});
55+
});
56+
3557
describe('EditableCaret',function(){
3658
beforeEach(function(){
3759
varcontentEditable=''
@@ -43,8 +65,9 @@ describe('jquery.caret', function() {
4365
+'<div>'
4466
+'<ul>'
4567
+'<li>Testing 1</li>'
46-
+'<li>Testing 2</li>'
68+
+'<li>Testin 2</li>'
4769
+'</ul>'
70+
+'<div>--</div>'
4871
+'</div>'
4972
+'<div><br></div>'
5073
+'</div>';
@@ -58,27 +81,31 @@ describe('jquery.caret', function() {
5881
varselection=window.getSelection();
5982
expect(selection.anchorNode.nodeValue).toBe('Hello ');
6083
expect(selection.anchorOffset).toBe(3);
84+
expect($inputor.caret('pos')).toBe(3);
6185
});
6286

6387
it('sets the caret position in a span',function(){
6488
$inputor.caret('pos',8);
6589
varselection=window.getSelection();
6690
expect(selection.anchorNode.nodeValue).toBe('World');
6791
expect(selection.anchorOffset).toBe(2);
92+
expect($inputor.caret('pos')).toBe(8);
6893
});
6994

7095
it('sets the caret position in a list item',function(){
7196
$inputor.caret('pos',16);
7297
varselection=window.getSelection();
7398
expect(selection.anchorNode.nodeValue).toBe('Testing 1');
7499
expect(selection.anchorOffset).toBe(3);
100+
expect($inputor.caret('pos')).toBe(16);
75101
});
76102

77103
it('sets the caret position at the end of a list item',function(){
78-
$inputor.caret('pos',31);
104+
$inputor.caret('pos',30);
79105
varselection=window.getSelection();
80-
expect(selection.anchorNode.nodeValue).toBe('Testing 2');
81-
expect(selection.anchorOffset).toBe(9);
106+
expect(selection.anchorNode.nodeValue).toBe('Testin 2');
107+
expect(selection.anchorOffset).toBe(8);
108+
expect($inputor.caret('pos')).toBe(30);
82109
});
83110
});
84111
});

‎src/jquery.caret.coffee‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,23 @@ class EditableCaret
2323
setPos: (pos)->
2424
ifsel=oWindow.getSelection()
2525
offset=0
26+
found=false
2627
dofn= (pos,parent=@domInputor)->
2728
for nodeinparent.childNodes
29+
if found
30+
break
2831
ifnode.nodeType==3
2932
if offset+node.length>= pos
33+
found=true
3034
range=oDocument.createRange()
3135
range.setStart(node, pos- offset)
3236
sel.removeAllRanges()
3337
sel.addRange(range)
34-
returntrue
35-
offset+=node.length
38+
break
39+
else
40+
offset+=node.length
3641
else
37-
iffn(pos, node)thenbreak
42+
fn(pos, node)
3843

3944
@domInputor
4045

‎src/jquery.caret.js‎

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,29 +41,35 @@ EditableCaret = (function() {
4141
}
4242

4343
EditableCaret.prototype.setPos=function(pos){
44-
varfn,offset,sel;
44+
varfn,found,offset,sel;
4545
if(sel=oWindow.getSelection()){
4646
offset=0;
47+
found=false;
4748
(fn=function(pos,parent){
48-
varnode,range,_i,_len,_ref;
49+
varnode,range,_i,_len,_ref,_results;
4950
_ref=parent.childNodes;
51+
_results=[];
5052
for(_i=0,_len=_ref.length;_i<_len;_i++){
5153
node=_ref[_i];
54+
if(found){
55+
break;
56+
}
5257
if(node.nodeType===3){
5358
if(offset+node.length>=pos){
59+
found=true;
5460
range=oDocument.createRange();
5561
range.setStart(node,pos-offset);
5662
sel.removeAllRanges();
5763
sel.addRange(range);
58-
returntrue;
59-
}
60-
offset+=node.length;
61-
}else{
62-
if(fn(pos,node)){
6364
break;
65+
}else{
66+
_results.push(offset+=node.length);
6467
}
68+
}else{
69+
_results.push(fn(pos,node));
6570
}
6671
}
72+
return_results;
6773
})(pos,this.domInputor);
6874
}
6975
returnthis.domInputor;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp