Skip to content

Commit 622dec5

Browse files
committed
Fix selection bug on Safari
See comment in code for details. Fixes #90
1 parent 1ff1ee5 commit 622dec5

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

lib/codemirror.js

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1893,6 +1893,7 @@ var CodeMirror = (function() {
18931893

18941894
var gecko = /gecko\/\d{7}/i.test(navigator.userAgent);
18951895
var ie = /MSIE \d/.test(navigator.userAgent);
1896+
var safari = /Apple Computer/.test(navigator.vendor);
18961897

18971898
var lineSep = "\n";
18981899
// Feature-detect whether newlines in textareas are converted to \r\n
@@ -1993,10 +1994,26 @@ var CodeMirror = (function() {
19931994
try {return {start: te.selectionStart, end: te.selectionEnd};}
19941995
catch(e) {return null;}
19951996
};
1996-
var setSelRange = function(te, start, end) {
1997-
try {te.setSelectionRange(start, end);}
1998-
catch(e) {} // Fails on Firefox when textarea isn't part of the document
1999-
};
1997+
if (safari)
1998+
// On Safari, selection set with setSelectionRange are in a sort
1999+
// of limbo wrt their anchor. If you press shift-left in them,
2000+
// the anchor is put at the end, and the selection expanded to
2001+
// the left. If you press shift-right, the anchor ends up at the
2002+
// front. This is not what CodeMirror wants, so it does a
2003+
// spurious modify() call to get out of limbo.
2004+
var setSelRange = function(te, start, end) {
2005+
if (start == end)
2006+
te.setSelectionRange(start, end);
2007+
else {
2008+
te.setSelectionRange(start, end - 1);
2009+
window.getSelection().modify("expand", "forward", "character");
2010+
}
2011+
};
2012+
else
2013+
var setSelRange = function(te, start, end) {
2014+
try {te.setSelectionRange(start, end);}
2015+
catch(e) {} // Fails on Firefox when textarea isn't part of the document
2016+
};
20002017
}
20012018
// IE model. Don't ask.
20022019
else {

0 commit comments

Comments
 (0)