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

Commitf7d3fec

Browse files
committed
Implement setPos for ContentEditable elements with tests
Based on code in this comment:ichord#5 (comment)
1 parent6d4cc56 commitf7d3fec

File tree

4 files changed

+149
-31
lines changed

4 files changed

+149
-31
lines changed

‎dist/jquery.caret.js‎

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
(function(root,factory){
22
if(typeofdefine==='function'&&define.amd){
3-
// AMD. Register as an anonymous module.
4-
define(["jquery"],function($){
5-
return(root.returnExportsGlobal=factory($));
3+
// AMD. Register as an anonymous module unless amdModuleId is set
4+
define(["jquery"],function(a0){
5+
return(factory(a0));
66
});
77
}elseif(typeofexports==='object'){
88
// Node. Does not work with strict CommonJS, but
9-
// only CommonJS-likeenviroments that support module.exports,
9+
// only CommonJS-likeenvironments that support module.exports,
1010
// like Node.
1111
module.exports=factory(require("jquery"));
1212
}else{
@@ -40,6 +40,31 @@ EditableCaret = (function() {
4040
}
4141

4242
EditableCaret.prototype.setPos=function(pos){
43+
varfn,offset,sel;
44+
if(sel=oWindow.getSelection()){
45+
offset=0;
46+
(fn=function(pos,parent){
47+
varnode,range,_i,_len,_ref;
48+
_ref=parent.childNodes;
49+
for(_i=0,_len=_ref.length;_i<_len;_i++){
50+
node=_ref[_i];
51+
if(node.nodeType===3){
52+
if(offset+node.length>=pos){
53+
range=oDocument.createRange();
54+
range.setStart(node,pos-offset);
55+
sel.removeAllRanges();
56+
sel.addRange(range);
57+
returntrue;
58+
}
59+
offset+=node.length;
60+
}else{
61+
if(fn(pos,node)){
62+
break;
63+
}
64+
}
65+
}
66+
})(pos,this.domInputor);
67+
}
4368
returnthis.domInputor;
4469
};
4570

‎spec/javascripts/caret.js‎

Lines changed: 73 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,84 @@
1-
describe("jquery.caret",function(){
1+
describe('jquery.caret',function(){
22
var$inputor;
33
$inputor=null;
44
varfixPos=0;
55

6-
beforeEach(function(){
7-
varhtml=''
8-
+'<textarea id="inputor" name="at" rows="8" cols="40">'
9-
+' Stay Foolish, Stay Hungry. @Jobs'
10-
+'</textarea>'
6+
describe('InputCaret',function(){
7+
beforeEach(function(){
8+
varhtml=''
9+
+'<textarea id="inputor" name="at" rows="8" cols="40">'
10+
+' Stay Foolish, Stay Hungry. @Jobs'
11+
+'</textarea>';
1112

12-
varfixture=setFixtures(html);
13-
$inputor=fixture.find('#inputor');
13+
varfixture=setFixtures(html);
14+
$inputor=fixture.find('#inputor');
1415

15-
varfixPos=20;
16-
});
16+
varfixPos=20;
17+
});
18+
19+
it('Set/Get caret pos',function(){
20+
$inputor.caret('pos',15);
21+
expect($inputor.caret('pos')).toBe(15);
22+
});
1723

18-
it('Set/Get caret pos',function(){
19-
$inputor.caret('pos',15);
20-
expect($inputor.caret('pos')).toBe(15);
24+
// TODO: I don't know how to test this functions yet. = =.
25+
// it("Set/Get caret position", function() {
26+
// $inputor.caret('position', 20);
27+
// pos = $inputor.caret('position'); // => {left: 15, top: 30, height: 20}
28+
// expect(pos).toBe({ left : 2, top : 2, height : 17 });
29+
// });
30+
31+
// $('#inputor').caret('offset'); // => {left: 300, top: 400, height: 20}
32+
// $('#inputor').caret('offset', fixPos);
2133
});
2234

23-
// TODO: I don't know how to test this functions yet. = =.
24-
// it("Set/Get caret position", function() {
25-
// $inputor.caret('position', 20);
26-
// pos = $inputor.caret('position'); // => {left: 15, top: 30, height: 20}
27-
// expect(pos).toBe({ left : 2, top : 2, height : 17 });
28-
// });
35+
describe('EditableCaret',function(){
36+
beforeEach(function(){
37+
varcontentEditable=''
38+
+'<div id="inputor" contentEditable="true">'
39+
+'Hello '
40+
+'<span id="test">World</span>'
41+
+'! '
42+
+'<div><br></div>'
43+
+'<div>'
44+
+'<ul>'
45+
+'<li>Testing 1</li>'
46+
+'<li>Testing 2</li>'
47+
+'</ul>'
48+
+'</div>'
49+
+'<div><br></div>'
50+
+'</div>';
51+
52+
varfixture=setFixtures(contentEditable);
53+
$inputor=fixture.find('#inputor');
54+
});
2955

30-
// $('#inputor').caret('offset'); // => {left: 300, top: 400, height: 20}
31-
// $('#inputor').caret('offset', fixPos);
56+
it('sets the caret position at the top-level',function(){
57+
$inputor.caret('pos',3);
58+
varselection=window.getSelection();
59+
expect(selection.anchorNode.nodeValue).toBe('Hello ');
60+
expect(selection.anchorOffset).toBe(3);
61+
});
3262

33-
});
63+
it('sets the caret position in a span',function(){
64+
$inputor.caret('pos',8);
65+
varselection=window.getSelection();
66+
expect(selection.anchorNode.nodeValue).toBe('World');
67+
expect(selection.anchorOffset).toBe(2);
68+
});
69+
70+
it('sets the caret position in a list item',function(){
71+
$inputor.caret('pos',16);
72+
varselection=window.getSelection();
73+
expect(selection.anchorNode.nodeValue).toBe('Testing 1');
74+
expect(selection.anchorOffset).toBe(3);
75+
});
76+
77+
it('sets the caret position at the end of a list item',function(){
78+
$inputor.caret('pos',31);
79+
varselection=window.getSelection();
80+
expect(selection.anchorNode.nodeValue).toBe('Testing 2');
81+
expect(selection.anchorOffset).toBe(9);
82+
});
83+
});
84+
});

‎src/jquery.caret.coffee‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,24 @@ class EditableCaret
2020
@domInputor=@$inputor[0]
2121

2222
# NOTE: Duck type
23-
setPos: (pos)->@domInputor
23+
setPos: (pos)->
24+
ifsel=oWindow.getSelection()
25+
offset=0
26+
dofn= (pos,parent=@domInputor)->
27+
for nodeinparent.childNodes
28+
ifnode.nodeType==3
29+
if offset+node.length>= pos
30+
range=oDocument.createRange()
31+
range.setStart(node, pos- offset)
32+
sel.removeAllRanges()
33+
sel.addRange(range)
34+
returntrue
35+
offset+=node.length
36+
else
37+
iffn(pos, node)thenbreak
38+
39+
@domInputor
40+
2441
getIEPosition:->this.getPosition()
2542
getPosition:->
2643
offset=this.getOffset()

‎src/jquery.caret.js‎

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
(function(root,factory){
22
if(typeofdefine==='function'&&define.amd){
3-
// AMD. Register as an anonymous module.
4-
define(["jquery"],function($){
5-
return(root.returnExportsGlobal=factory($));
3+
// AMD. Register as an anonymous module unless amdModuleId is set
4+
define(["jquery"],function(a0){
5+
return(factory(a0));
66
});
77
}elseif(typeofexports==='object'){
88
// Node. Does not work with strict CommonJS, but
9-
// only CommonJS-likeenviroments that support module.exports,
9+
// only CommonJS-likeenvironments that support module.exports,
1010
// like Node.
1111
module.exports=factory(require("jquery"));
1212
}else{
@@ -41,6 +41,31 @@ EditableCaret = (function() {
4141
}
4242

4343
EditableCaret.prototype.setPos=function(pos){
44+
varfn,offset,sel;
45+
if(sel=oWindow.getSelection()){
46+
offset=0;
47+
(fn=function(pos,parent){
48+
varnode,range,_i,_len,_ref;
49+
_ref=parent.childNodes;
50+
for(_i=0,_len=_ref.length;_i<_len;_i++){
51+
node=_ref[_i];
52+
if(node.nodeType===3){
53+
if(offset+node.length>=pos){
54+
range=oDocument.createRange();
55+
range.setStart(node,pos-offset);
56+
sel.removeAllRanges();
57+
sel.addRange(range);
58+
returntrue;
59+
}
60+
offset+=node.length;
61+
}else{
62+
if(fn(pos,node)){
63+
break;
64+
}
65+
}
66+
}
67+
})(pos,this.domInputor);
68+
}
4469
returnthis.domInputor;
4570
};
4671

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp