Skip to content
Browse files

Updated codemirror

  • Loading branch information...
1 parent d41a046 commit f4622788681a21a3e4ec73bf228182f8b3609958 @pyromaniac pyromaniac committed May 16, 2012
View
952 app/assets/javascripts/puffer/codemirror.js
@@ -1,12 +1,12 @@
-// CodeMirror version 2.2
+// CodeMirror version 2.24
//
// All functions that need access to the editor's state live inside
// the CodeMirror function. Below that, at the bottom of the file,
// some utilities are defined.
// CodeMirror is the only global var we claim
var CodeMirror = (function() {
- // This is the function that produces an editor instance. It's
+ // This is the function that produces an editor instance. Its
// closure is used to store the editor state.
function CodeMirror(place, givenOptions) {
// Determine effective options based on given values and defaults.
@@ -15,38 +15,42 @@ var CodeMirror = (function() {
if (defaults.hasOwnProperty(opt))
options[opt] = (givenOptions && givenOptions.hasOwnProperty(opt) ? givenOptions : defaults)[opt];
- var targetDocument = options["document"];
// The element in which the editor lives.
- var wrapper = targetDocument.createElement("div");
+ var wrapper = document.createElement("div");
wrapper.className = "CodeMirror" + (options.lineWrapping ? " CodeMirror-wrap" : "");
// This mess creates the base DOM structure for the editor.
wrapper.innerHTML =
'<div style="overflow: hidden; position: relative; width: 3px; height: 0px;">' + // Wraps and hides input textarea
- '<textarea style="position: absolute; padding: 0; width: 1px;" wrap="off" ' +
+ '<textarea style="position: absolute; padding: 0; width: 1px; height: 1em" wrap="off" ' +
'autocorrect="off" autocapitalize="off"></textarea></div>' +
'<div class="CodeMirror-scroll" tabindex="-1">' +
'<div style="position: relative">' + // Set to the height of the text, causes scrolling
'<div style="position: relative">' + // Moved around its parent to cover visible view
'<div class="CodeMirror-gutter"><div class="CodeMirror-gutter-text"></div></div>' +
// Provides positioning relative to (visible) text origin
- '<div class="CodeMirror-lines"><div style="position: relative">' +
- '<div style="position: absolute; width: 100%; height: 0; overflow: hidden; visibility: hidden"></div>' +
+ '<div class="CodeMirror-lines"><div style="position: relative; z-index: 0">' +
+ '<div style="position: absolute; width: 100%; height: 0; overflow: hidden; visibility: hidden;"></div>' +
'<pre class="CodeMirror-cursor">&#160;</pre>' + // Absolutely positioned blinky cursor
- '<div></div>' + // This DIV contains the actual code
+ '<div style="position: relative; z-index: -1"></div><div></div>' + // DIVs containing the selection and the actual code
'</div></div></div></div></div>';
if (place.appendChild) place.appendChild(wrapper); else place(wrapper);
// I've never seen more elegant code in my life.
var inputDiv = wrapper.firstChild, input = inputDiv.firstChild,
scroller = wrapper.lastChild, code = scroller.firstChild,
mover = code.firstChild, gutter = mover.firstChild, gutterText = gutter.firstChild,
lineSpace = gutter.nextSibling.firstChild, measure = lineSpace.firstChild,
- cursor = measure.nextSibling, lineDiv = cursor.nextSibling;
+ cursor = measure.nextSibling, selectionDiv = cursor.nextSibling,
+ lineDiv = selectionDiv.nextSibling;
themeChanged();
// Needed to hide big blue blinking cursor on Mobile Safari
- if (/AppleWebKit/.test(navigator.userAgent) && /Mobile\/\w+/.test(navigator.userAgent)) input.style.width = "0px";
+ if (ios) input.style.width = "0px";
if (!webkit) lineSpace.draggable = true;
+ lineSpace.style.outline = "none";
if (options.tabindex != null) input.tabIndex = options.tabindex;
+ if (options.autofocus) focusInput();
if (!options.gutter && !options.lineNumbers) gutter.style.display = "none";
+ // Needed to handle Tab key in KHTML
+ if (khtml) inputDiv.style.height = "1px", inputDiv.style.position = "absolute";
// Check for problem with IE innerHTML not working when we have a
// P (or similar) parent node.
@@ -71,19 +75,21 @@ var CodeMirror = (function() {
var sel = {from: {line: 0, ch: 0}, to: {line: 0, ch: 0}, inverted: false};
// Selection-related flags. shiftSelecting obviously tracks
// whether the user is holding shift.
- var shiftSelecting, lastClick, lastDoubleClick, draggingText, overwrite = false;
+ var shiftSelecting, lastClick, lastDoubleClick, lastScrollPos = 0, draggingText,
+ overwrite = false, suppressEdits = false;
// Variables used by startOperation/endOperation to track what
// happened during the operation.
var updateInput, userSelChange, changes, textChanged, selectionChanged, leaveInputAlone,
gutterDirty, callbacks;
// Current visible range (may be bigger than the view window).
var displayOffset = 0, showingFrom = 0, showingTo = 0, lastSizeC = 0;
- // bracketHighlighted is used to remember that a backet has been
+ // bracketHighlighted is used to remember that a bracket has been
// marked.
var bracketHighlighted;
// Tracks the maximum line length so that the horizontal scrollbar
// can be kept static when scrolling.
- var maxLine = "", maxWidth, tabText = computeTabText();
+ var maxLine = "", maxWidth;
+ var tabCache = {};
// Initialize the content.
operation(function(){setValue(options.value || ""); updateInput = false;})();
@@ -92,13 +98,13 @@ var CodeMirror = (function() {
// Register our event handlers.
connect(scroller, "mousedown", operation(onMouseDown));
connect(scroller, "dblclick", operation(onDoubleClick));
- connect(lineSpace, "dragstart", onDragStart);
connect(lineSpace, "selectstart", e_preventDefault);
// Gecko browsers fire contextmenu *after* opening the menu, at
// which point we can't mess with it anymore. Context menu is
// handled in onMouseDown for Gecko.
if (!gecko) connect(scroller, "contextmenu", onContextMenu);
connect(scroller, "scroll", function() {
+ lastScrollPos = scroller.scrollTop;
updateDisplay([]);
if (options.fixedGutter) gutter.style.left = scroller.scrollLeft + "px";
if (options.onScroll) options.onScroll(instance);
@@ -111,17 +117,32 @@ var CodeMirror = (function() {
connect(input, "focus", onFocus);
connect(input, "blur", onBlur);
- connect(scroller, "dragenter", e_stop);
- connect(scroller, "dragover", e_stop);
- connect(scroller, "drop", operation(onDrop));
+ if (options.dragDrop) {
+ connect(lineSpace, "dragstart", onDragStart);
+ function drag_(e) {
+ if (options.onDragEvent && options.onDragEvent(instance, addStop(e))) return;
+ e_stop(e);
+ }
+ connect(scroller, "dragenter", drag_);
+ connect(scroller, "dragover", drag_);
+ connect(scroller, "drop", operation(onDrop));
+ }
connect(scroller, "paste", function(){focusInput(); fastPoll();});
connect(input, "paste", fastPoll);
- connect(input, "cut", operation(function(){replaceSelection("");}));
+ connect(input, "cut", operation(function(){
+ if (!options.readOnly) replaceSelection("");
+ }));
+
+ // Needed to handle Tab key in KHTML
+ if (khtml) connect(code, "mouseup", function() {
+ if (document.activeElement == input) input.blur();
+ focusInput();
+ });
// IE throws unspecified error in certain cases, when
// trying to access activeElement before onload
- var hasFocus; try { hasFocus = (targetDocument.activeElement == input); } catch(e) { }
- if (hasFocus) setTimeout(onFocus, 20);
+ var hasFocus; try { hasFocus = (document.activeElement == input); } catch(e) { }
+ if (hasFocus || options.autofocus) setTimeout(onFocus, 20);
else onBlur();
function isLine(l) {return l >= 0 && l < doc.size;}
@@ -135,23 +156,30 @@ var CodeMirror = (function() {
setValue: operation(setValue),
getSelection: getSelection,
replaceSelection: operation(replaceSelection),
- focus: function(){focusInput(); onFocus(); fastPoll();},
+ focus: function(){window.focus(); focusInput(); onFocus(); fastPoll();},
setOption: function(option, value) {
var oldVal = options[option];
options[option] = value;
if (option == "mode" || option == "indentUnit") loadMode();
- else if (option == "readOnly" && value) {onBlur(); input.blur();}
+ else if (option == "readOnly" && value == "nocursor") {onBlur(); input.blur();}
+ else if (option == "readOnly" && !value) {resetInput(true);}
else if (option == "theme") themeChanged();
else if (option == "lineWrapping" && oldVal != value) operation(wrappingChanged)();
- else if (option == "tabSize") operation(tabsChanged)();
- if (option == "lineNumbers" || option == "gutter" || option == "firstLineNumber" || option == "theme")
- operation(gutterChanged)();
+ else if (option == "tabSize") updateDisplay(true);
+ if (option == "lineNumbers" || option == "gutter" || option == "firstLineNumber" || option == "theme") {
+ gutterChanged();
+ updateDisplay(true);
+ }
},
getOption: function(option) {return options[option];},
undo: operation(undo),
redo: operation(redo),
indentLine: operation(function(n, dir) {
- if (isLine(n)) indentLine(n, dir == null ? "smart" : dir ? "add" : "subtract");
+ if (typeof dir != "string") {
+ if (dir == null) dir = options.smartIndent ? "smart" : "prev";
+ else dir = dir ? "add" : "subtract";
+ }
+ if (isLine(n)) indentLine(n, dir);
}),
indentSelection: operation(indentSelected),
historySize: function() {return {undo: history.done.length, redo: history.undone.length};},
@@ -165,17 +193,23 @@ var CodeMirror = (function() {
line = clipLine(line == null ? doc.size - 1: line);
return getStateBefore(line + 1);
},
- cursorCoords: function(start){
+ cursorCoords: function(start, mode) {
if (start == null) start = sel.inverted;
- return pageCoords(start ? sel.from : sel.to);
+ return this.charCoords(start ? sel.from : sel.to, mode);
+ },
+ charCoords: function(pos, mode) {
+ pos = clipPos(pos);
+ if (mode == "local") return localCoords(pos, false);
+ if (mode == "div") return localCoords(pos, true);
+ return pageCoords(pos);
},
- charCoords: function(pos){return pageCoords(clipPos(pos));},
coordsChar: function(coords) {
var off = eltOffset(lineSpace);
return coordsChar(coords.x - off.left, coords.y - off.top);
},
markText: operation(markText),
setBookmark: setBookmark,
+ findMarksAt: findMarksAt,
setMarker: operation(addGutterMarker),
clearMarker: operation(removeGutterMarker),
setLineClass: operation(setLineClass),
@@ -243,12 +277,21 @@ var CodeMirror = (function() {
replaceRange: operation(replaceRange),
getRange: function(from, to) {return getRange(clipPos(from), clipPos(to));},
+ triggerOnKeyDown: operation(onKeyDown),
execCommand: function(cmd) {return commands[cmd](instance);},
// Stuff used by commands, probably not much use to outside code.
moveH: operation(moveH),
deleteH: operation(deleteH),
moveV: operation(moveV),
- toggleOverwrite: function() {overwrite = !overwrite;},
+ toggleOverwrite: function() {
+ if(overwrite){
+ overwrite = false;
+ cursor.className = cursor.className.replace(" CodeMirror-overwrite", "");
+ } else {
+ overwrite = true;
+ cursor.className += " CodeMirror-overwrite";
+ }
+ },
posFromIndex: function(off) {
var lineNo = 0, ch;
@@ -268,9 +311,19 @@ var CodeMirror = (function() {
});
return index;
},
+ scrollTo: function(x, y) {
+ if (x != null) scroller.scrollLeft = x;
+ if (y != null) scroller.scrollTop = y;
+ updateDisplay([]);
+ },
operation: function(f){return operation(f)();},
- refresh: function(){updateDisplay(true);},
+ compoundChange: function(f){return compoundChange(f);},
+ refresh: function(){
+ updateDisplay(true);
+ if (scroller.scrollHeight > lastScrollPos)
+ scroller.scrollTop = lastScrollPos;
+ },
getInputField: function(){return input;},
getWrapperElement: function(){return wrapper;},
getScrollerElement: function(){return scroller;},
@@ -290,14 +343,14 @@ var CodeMirror = (function() {
splitLines(code), top, top);
updateInput = true;
}
- function getValue(code) {
+ function getValue() {
var text = [];
doc.iter(0, doc.size, function(line) { text.push(line.text); });
return text.join("\n");
}
function onMouseDown(e) {
- setShift(e.shiftKey);
+ setShift(e_prop(e, "shiftKey"));
// Check whether this is a click in a widget
for (var n = e_target(e); n != wrapper; n = n.parentNode)
if (n.parentNode == code && n != mover) return;
@@ -339,21 +392,25 @@ var CodeMirror = (function() {
} else { lastClick = {time: now, pos: start}; }
var last = start, going;
- if (dragAndDrop && !posEq(sel.from, sel.to) &&
+ if (options.dragDrop && dragAndDrop && !options.readOnly && !posEq(sel.from, sel.to) &&
!posLess(start, sel.from) && !posLess(sel.to, start)) {
// Let the drag handler handle this.
if (webkit) lineSpace.draggable = true;
- var up = connect(targetDocument, "mouseup", operation(function(e2) {
+ function dragEnd(e2) {
if (webkit) lineSpace.draggable = false;
draggingText = false;
- up();
+ up(); drop();
if (Math.abs(e.clientX - e2.clientX) + Math.abs(e.clientY - e2.clientY) < 10) {
e_preventDefault(e2);
setCursor(start.line, start.ch, true);
focusInput();
}
- }), true);
+ }
+ var up = connect(document, "mouseup", operation(dragEnd), true);
+ var drop = connect(scroller, "drop", operation(dragEnd), true);
draggingText = true;
+ // IE's approach to draggable
+ if (lineSpace.dragDrop) lineSpace.dragDrop();
return;
}
e_preventDefault(e);
@@ -372,20 +429,22 @@ var CodeMirror = (function() {
}
}
- var move = connect(targetDocument, "mousemove", operation(function(e) {
- clearTimeout(going);
- e_preventDefault(e);
- extend(e);
- }), true);
- var up = connect(targetDocument, "mouseup", operation(function(e) {
+ function done(e) {
clearTimeout(going);
var cur = posFromMouse(e);
if (cur) setSelectionUser(start, cur);
e_preventDefault(e);
focusInput();
updateInput = true;
move(); up();
+ }
+ var move = connect(document, "mousemove", operation(function(e) {
+ clearTimeout(going);
+ e_preventDefault(e);
+ if (!ie && !e_button(e)) done(e);
+ else extend(e);
}), true);
+ var up = connect(document, "mouseup", operation(done), true);
}
function onDoubleClick(e) {
for (var n = e_target(e); n != wrapper; n = n.parentNode)
@@ -397,6 +456,7 @@ var CodeMirror = (function() {
selectWordAt(start);
}
function onDrop(e) {
+ if (options.onDragEvent && options.onDragEvent(instance, addStop(e))) return;
e.preventDefault();
var pos = posFromMouse(e, true), files = e.dataTransfer.files;
if (!pos || options.readOnly) return;
@@ -406,12 +466,12 @@ var CodeMirror = (function() {
reader.onload = function() {
text[i] = reader.result;
if (++read == n) {
- pos = clipPos(pos);
- operation(function() {
+ pos = clipPos(pos);
+ operation(function() {
var end = replaceRange(text.join(""), pos, pos);
setSelectionUser(pos, end);
})();
- }
+ }
};
reader.readAsText(file);
}
@@ -422,87 +482,130 @@ var CodeMirror = (function() {
try {
var text = e.dataTransfer.getData("Text");
if (text) {
- var end = replaceRange(text, pos, pos);
- var curFrom = sel.from, curTo = sel.to;
- setSelectionUser(pos, end);
- if (draggingText) replaceRange("", curFrom, curTo);
- focusInput();
- }
+ compoundChange(function() {
+ var curFrom = sel.from, curTo = sel.to;
+ setSelectionUser(pos, pos);
+ if (draggingText) replaceRange("", curFrom, curTo);
+ replaceSelection(text);
+ focusInput();
+ });
+ }
}
catch(e){}
}
}
function onDragStart(e) {
var txt = getSelection();
- // This will reset escapeElement
- htmlEscape(txt);
- e.dataTransfer.setDragImage(escapeElement, 0, 0);
e.dataTransfer.setData("Text", txt);
- }
- function handleKeyBinding(e) {
- var name = keyNames[e.keyCode], next = keyMap[options.keyMap].auto, bound, dropShift;
- if (name == null || e.altGraphKey) {
- if (next) options.keyMap = next;
- return null;
- }
- if (e.altKey) name = "Alt-" + name;
- if (e.ctrlKey) name = "Ctrl-" + name;
- if (e.metaKey) name = "Cmd-" + name;
- if (e.shiftKey && (bound = lookupKey("Shift-" + name, options.extraKeys, options.keyMap))) {
- dropShift = true;
- } else {
- bound = lookupKey(name, options.extraKeys, options.keyMap);
+
+ // Use dummy image instead of default browsers image.
+ if (gecko || chrome) {
+ var img = document.createElement('img');
+ img.scr = 'data:image/gif;base64,R0lGODdhAgACAIAAAAAAAP///ywAAAAAAgACAAACAoRRADs='; //1x1 image
+ e.dataTransfer.setDragImage(img, 0, 0);
}
+ }
+
+ function doHandleBinding(bound, dropShift) {
if (typeof bound == "string") {
- if (commands.propertyIsEnumerable(bound)) bound = commands[bound];
- else bound = null;
- }
- if (next && (bound || !isModifierKey(e))) options.keyMap = next;
- if (!bound) return false;
- if (dropShift) {
- var prevShift = shiftSelecting;
- shiftSelecting = null;
+ bound = commands[bound];
+ if (!bound) return false;
+ }
+ var prevShift = shiftSelecting;
+ try {
+ if (options.readOnly) suppressEdits = true;
+ if (dropShift) shiftSelecting = null;
bound(instance);
+ } catch(e) {
+ if (e != Pass) throw e;
+ return false;
+ } finally {
shiftSelecting = prevShift;
- } else bound(instance);
- e_preventDefault(e);
+ suppressEdits = false;
+ }
return true;
}
- var lastStoppedKey = null;
+ function handleKeyBinding(e) {
+ // Handle auto keymap transitions
+ var startMap = getKeyMap(options.keyMap), next = startMap.auto;
+ clearTimeout(maybeTransition);
+ if (next && !isModifierKey(e)) maybeTransition = setTimeout(function() {
+ if (getKeyMap(options.keyMap) == startMap) {
+ options.keyMap = (next.call ? next.call(null, instance) : next);
+ }
+ }, 50);
+
+ var name = keyNames[e_prop(e, "keyCode")], handled = false;
+ if (name == null || e.altGraphKey) return false;
+ if (e_prop(e, "altKey")) name = "Alt-" + name;
+ if (e_prop(e, "ctrlKey")) name = "Ctrl-" + name;
+ if (e_prop(e, "metaKey")) name = "Cmd-" + name;
+
+ var stopped = false;
+ function stop() { stopped = true; }
+
+ if (e_prop(e, "shiftKey")) {
+ handled = lookupKey("Shift-" + name, options.extraKeys, options.keyMap,
+ function(b) {return doHandleBinding(b, true);}, stop)
+ || lookupKey(name, options.extraKeys, options.keyMap, function(b) {
+ if (typeof b == "string" && /^go[A-Z]/.test(b)) return doHandleBinding(b);
+ }, stop);
+ } else {
+ handled = lookupKey(name, options.extraKeys, options.keyMap, doHandleBinding, stop);
+ }
+ if (stopped) handled = false;
+ if (handled) {
+ e_preventDefault(e);
+ if (ie) { e.oldKeyCode = e.keyCode; e.keyCode = 0; }
+ }
+ return handled;
+ }
+ function handleCharBinding(e, ch) {
+ var handled = lookupKey("'" + ch + "'", options.extraKeys,
+ options.keyMap, function(b) { return doHandleBinding(b, true); });
+ if (handled) e_preventDefault(e);
+ return handled;
+ }
+
+ var lastStoppedKey = null, maybeTransition;
function onKeyDown(e) {
if (!focused) onFocus();
- var code = e.keyCode;
+ if (ie && e.keyCode == 27) { e.returnValue = false; }
+ if (pollingFast) { if (readInput()) pollingFast = false; }
+ if (options.onKeyEvent && options.onKeyEvent(instance, addStop(e))) return;
+ var code = e_prop(e, "keyCode");
// IE does strange things with escape.
- if (ie && code == 27) { e.returnValue = false; }
- setShift(code == 16 || e.shiftKey);
+ setShift(code == 16 || e_prop(e, "shiftKey"));
// First give onKeyEvent option a chance to handle this.
- if (options.onKeyEvent && options.onKeyEvent(instance, addStop(e))) return;
var handled = handleKeyBinding(e);
if (window.opera) {
- lastStoppedKey = handled ? e.keyCode : null;
+ lastStoppedKey = handled ? code : null;
// Opera has no cut event... we try to at least catch the key combo
- if (!handled && (mac ? e.metaKey : e.ctrlKey) && e.keyCode == 88)
+ if (!handled && code == 88 && e_prop(e, mac ? "metaKey" : "ctrlKey"))
replaceSelection("");
}
}
function onKeyPress(e) {
- if (window.opera && e.keyCode == lastStoppedKey) {lastStoppedKey = null; e_preventDefault(e); return;}
+ if (pollingFast) readInput();
if (options.onKeyEvent && options.onKeyEvent(instance, addStop(e))) return;
- if (window.opera && !e.which && handleKeyBinding(e)) return;
- if (options.electricChars && mode.electricChars) {
- var ch = String.fromCharCode(e.charCode == null ? e.keyCode : e.charCode);
+ var keyCode = e_prop(e, "keyCode"), charCode = e_prop(e, "charCode");
+ if (window.opera && keyCode == lastStoppedKey) {lastStoppedKey = null; e_preventDefault(e); return;}
+ if (((window.opera && !e.which) || khtml) && handleKeyBinding(e)) return;
+ var ch = String.fromCharCode(charCode == null ? keyCode : charCode);
+ if (options.electricChars && mode.electricChars && options.smartIndent && !options.readOnly) {
if (mode.electricChars.indexOf(ch) > -1)
setTimeout(operation(function() {indentLine(sel.to.line, "smart");}), 75);
}
+ if (handleCharBinding(e, ch)) return;
fastPoll();
}
function onKeyUp(e) {
if (options.onKeyEvent && options.onKeyEvent(instance, addStop(e))) return;
- if (e.keyCode == 16) shiftSelecting = null;
+ if (e_prop(e, "keyCode") == 16) shiftSelecting = null;
}
function onFocus() {
- if (options.readOnly) return;
+ if (options.readOnly == "nocursor") return;
if (!focused) {
if (options.onFocus) options.onFocus(instance);
focused = true;
@@ -517,6 +620,10 @@ var CodeMirror = (function() {
if (focused) {
if (options.onBlur) options.onBlur(instance);
focused = false;
+ if (bracketHighlighted)
+ operation(function(){
+ if (bracketHighlighted) { bracketHighlighted(); bracketHighlighted = null; }
+ })();
wrapper.className = wrapper.className.replace(" CodeMirror-focused", "");
}
clearInterval(blinker);
@@ -526,6 +633,7 @@ var CodeMirror = (function() {
// Replace the range from from to to by the strings in newText.
// Afterwards, set the selection to selFrom, selTo.
function updateLines(from, to, newText, selFrom, selTo) {
+ if (suppressEdits) return;
if (history) {
var old = [];
doc.iter(from.line, to.line + 1, function(line) { old.push(line.text); });
@@ -535,24 +643,28 @@ var CodeMirror = (function() {
updateLinesNoUndo(from, to, newText, selFrom, selTo);
}
function unredoHelper(from, to) {
- var change = from.pop();
- if (change) {
+ if (!from.length) return;
+ var set = from.pop(), out = [];
+ for (var i = set.length - 1; i >= 0; i -= 1) {
+ var change = set[i];
var replaced = [], end = change.start + change.added;
doc.iter(change.start, end, function(line) { replaced.push(line.text); });
- to.push({start: change.start, added: change.old.length, old: replaced});
+ out.push({start: change.start, added: change.old.length, old: replaced});
var pos = clipPos({line: change.start + change.old.length - 1,
ch: editEnd(replaced[replaced.length-1], change.old[change.old.length-1])});
updateLinesNoUndo({line: change.start, ch: 0}, {line: end - 1, ch: getLine(end-1).text.length}, change.old, pos, pos);
- updateInput = true;
}
+ updateInput = true;
+ to.push(out);
}
function undo() {unredoHelper(history.done, history.undone);}
function redo() {unredoHelper(history.undone, history.done);}
function updateLinesNoUndo(from, to, newText, selFrom, selTo) {
+ if (suppressEdits) return;
var recomputeMaxLength = false, maxLineLength = maxLine.length;
if (!options.lineWrapping)
- doc.iter(from.line, to.line, function(line) {
+ doc.iter(from.line, to.line + 1, function(line) {
if (line.text.length == maxLineLength) {recomputeMaxLength = true; return true;}
});
if (from.line != to.line || newText.length > 1) gutterDirty = true;
@@ -600,14 +712,14 @@ var CodeMirror = (function() {
doc.insert(from.line + 1, added);
}
if (options.lineWrapping) {
- var perLine = scroller.clientWidth / charWidth() - 3;
+ var perLine = Math.max(5, scroller.clientWidth / charWidth() - 3);
doc.iter(from.line, from.line + newText.length, function(line) {
if (line.hidden) return;
var guess = Math.ceil(line.text.length / perLine) || 1;
if (guess != line.height) updateLineHeight(line, guess);
});
} else {
- doc.iter(from.line, i + newText.length, function(line) {
+ doc.iter(from.line, from.line + newText.length, function(line) {
var l = line.text;
if (l.length > maxLineLength) {
maxLine = l; maxLineLength = l.length; maxWidth = null;
@@ -651,7 +763,8 @@ var CodeMirror = (function() {
setSelection(selFrom, selTo, updateLine(sel.from.line), updateLine(sel.to.line));
// Make sure the scroll-size div has the correct height.
- code.style.height = (doc.height * textHeight() + 2 * paddingTop()) + "px";
+ if (scroller.clientHeight)
+ code.style.height = (doc.height * textHeight() + 2 * paddingTop()) + "px";
}
function replaceRange(code, from, to) {
@@ -729,7 +842,7 @@ var CodeMirror = (function() {
// supported or compatible enough yet to rely on.)
var prevInput = "";
function readInput() {
- if (leaveInputAlone || !focused || hasSelection(input)) return false;
+ if (leaveInputAlone || !focused || hasSelection(input) || options.readOnly) return false;
var text = input.value;
if (text == prevInput) return false;
shiftSelecting = null;
@@ -747,12 +860,12 @@ var CodeMirror = (function() {
if (!posEq(sel.from, sel.to)) {
prevInput = "";
input.value = getSelection();
- input.select();
+ selectInput(input);
} else if (user) prevInput = input.value = "";
}
function focusInput() {
- if (!options.readOnly) input.focus();
+ if (options.readOnly != "nocursor") input.focus();
}
function scrollEditorIntoView() {
@@ -769,16 +882,17 @@ var CodeMirror = (function() {
return scrollIntoView(x, cursor.y, x, cursor.yBot);
}
function scrollIntoView(x1, y1, x2, y2) {
- var pl = paddingLeft(), pt = paddingTop(), lh = textHeight();
+ var pl = paddingLeft(), pt = paddingTop();
y1 += pt; y2 += pt; x1 += pl; x2 += pl;
var screen = scroller.clientHeight, screentop = scroller.scrollTop, scrolled = false, result = true;
- if (y1 < screentop) {scroller.scrollTop = Math.max(0, y1 - 2*lh); scrolled = true;}
- else if (y2 > screentop + screen) {scroller.scrollTop = y2 + lh - screen; scrolled = true;}
+ if (y1 < screentop) {scroller.scrollTop = Math.max(0, y1); scrolled = true;}
+ else if (y2 > screentop + screen) {scroller.scrollTop = y2 - screen; scrolled = true;}
var screenw = scroller.clientWidth, screenleft = scroller.scrollLeft;
var gutterw = options.fixedGutter ? gutter.clientWidth : 0;
- if (x1 < screenleft + gutterw) {
- if (x1 < 50) x1 = 0;
+ var atLeft = x1 < gutterw + pl + 10;
+ if (x1 < screenleft + gutterw || atLeft) {
+ if (atLeft) x1 = 0;
scroller.scrollLeft = Math.max(0, x1 - 10 - gutterw);
scrolled = true;
}
@@ -793,10 +907,10 @@ var CodeMirror = (function() {
function visibleLines() {
var lh = textHeight(), top = scroller.scrollTop - paddingTop();
- var from_height = Math.max(0, Math.floor(top / lh));
- var to_height = Math.ceil((top + scroller.clientHeight) / lh);
- return {from: lineAtHeight(doc, from_height),
- to: lineAtHeight(doc, to_height)};
+ var fromHeight = Math.max(0, Math.floor(top / lh));
+ var toHeight = Math.ceil((top + scroller.clientHeight) / lh);
+ return {from: lineAtHeight(doc, fromHeight),
+ to: lineAtHeight(doc, toHeight)};
}
// Uses a set of changes plus the current scroll position to
// determine which DOM updates have to be made, and makes the
@@ -809,7 +923,7 @@ var CodeMirror = (function() {
// Compute the new visible window
var visible = visibleLines();
// Bail out if the visible area is already rendered and nothing changed.
- if (changes !== true && changes.length == 0 && visible.from >= showingFrom && visible.to <= showingTo) return;
+ if (changes !== true && changes.length == 0 && visible.from > showingFrom && visible.to < showingTo) return;
var from = Math.max(visible.from - 100, 0), to = Math.min(doc.size, visible.to + 100);
if (showingFrom < from && from - showingFrom < 20) from = showingFrom;
if (showingTo > to && showingTo - to < 20) to = Math.min(doc.size, showingTo);
@@ -827,13 +941,13 @@ var CodeMirror = (function() {
if (range.from >= range.to) intact.splice(i--, 1);
else intactLines += range.to - range.from;
}
- if (intactLines == to - from) return;
+ if (intactLines == to - from && from == showingFrom && to == showingTo) return;
intact.sort(function(a, b) {return a.domStart - b.domStart;});
var th = textHeight(), gutterDisplay = gutter.style.display;
- lineDiv.style.display = gutter.style.display = "none";
+ lineDiv.style.display = "none";
patchDisplay(from, to, intact);
- lineDiv.style.display = "";
+ lineDiv.style.display = gutter.style.display = "";
// Position the mover div to align with the lines it's supposed
// to be showing (which will cover the visible display)
@@ -844,24 +958,35 @@ var CodeMirror = (function() {
showingFrom = from; showingTo = to;
displayOffset = heightAtLine(doc, from);
mover.style.top = (displayOffset * th) + "px";
- code.style.height = (doc.height * th + 2 * paddingTop()) + "px";
+ if (scroller.clientHeight)
+ code.style.height = (doc.height * th + 2 * paddingTop()) + "px";
// Since this is all rather error prone, it is honoured with the
// only assertion in the whole file.
if (lineDiv.childNodes.length != showingTo - showingFrom)
throw new Error("BAD PATCH! " + JSON.stringify(intact) + " size=" + (showingTo - showingFrom) +
" nodes=" + lineDiv.childNodes.length);
- if (options.lineWrapping) {
+ function checkHeights() {
maxWidth = scroller.clientWidth;
- var curNode = lineDiv.firstChild;
+ var curNode = lineDiv.firstChild, heightChanged = false;
doc.iter(showingFrom, showingTo, function(line) {
if (!line.hidden) {
var height = Math.round(curNode.offsetHeight / th) || 1;
- if (line.height != height) {updateLineHeight(line, height); gutterDirty = true;}
+ if (line.height != height) {
+ updateLineHeight(line, height);
+ gutterDirty = heightChanged = true;
+ }
}
curNode = curNode.nextSibling;
});
+ if (heightChanged)
+ code.style.height = (doc.height * th + 2 * paddingTop()) + "px";
+ return heightChanged;
+ }
+
+ if (options.lineWrapping) {
+ checkHeights();
} else {
if (maxWidth == null) maxWidth = stringWidth(maxLine);
if (maxWidth > scroller.clientWidth) {
@@ -873,9 +998,13 @@ var CodeMirror = (function() {
lineSpace.style.width = code.style.width = "";
}
}
+
gutter.style.display = gutterDisplay;
- if (different || gutterDirty) updateGutter();
- updateCursor();
+ if (different || gutterDirty) {
+ // If the gutter grew in size, re-check heights. If those changed, re-draw gutter.
+ updateGutter() && options.lineWrapping && checkHeights() && updateGutter();
+ }
+ updateSelection();
if (!suppressCallback && options.onUpdate) options.onUpdate(instance);
return true;
}
@@ -922,21 +1051,20 @@ var CodeMirror = (function() {
}
// This pass fills in the lines that actually changed.
var nextIntact = intact.shift(), curNode = lineDiv.firstChild, j = from;
- var sfrom = sel.from.line, sto = sel.to.line, inSel = sfrom < from && sto >= from;
- var scratch = targetDocument.createElement("div"), newElt;
+ var scratch = document.createElement("div");
doc.iter(from, to, function(line) {
- var ch1 = null, ch2 = null;
- if (inSel) {
- ch1 = 0;
- if (sto == j) {inSel = false; ch2 = sel.to.ch;}
- } else if (sfrom == j) {
- if (sto == j) {ch1 = sel.from.ch; ch2 = sel.to.ch;}
- else {inSel = true; ch1 = sel.from.ch;}
- }
if (nextIntact && nextIntact.to == j) nextIntact = intact.shift();
if (!nextIntact || nextIntact.from > j) {
- if (line.hidden) scratch.innerHTML = "<pre></pre>";
- else scratch.innerHTML = line.getHTML(ch1, ch2, true, tabText);
+ if (line.hidden) var html = scratch.innerHTML = "<pre></pre>";
+ else {
+ var html = '<pre' + (line.className ? ' class="' + line.className + '"' : '') + '>'
+ + line.getHTML(makeTab) + '</pre>';
+ // Kludge to make sure the styled element lies behind the selection (by z-index)
+ if (line.bgClassName)
+ html = '<div style="position: relative"><pre class="' + line.bgClassName +
+ '" style="position: absolute; left: 0; right: 0; top: 0; bottom: 0; z-index: -2">&#160;</pre>' + html + "</div>";
+ }
+ scratch.innerHTML = html;
lineDiv.insertBefore(scratch.firstChild, curNode);
} else {
curNode = curNode.nextSibling;
@@ -949,7 +1077,7 @@ var CodeMirror = (function() {
if (!options.gutter && !options.lineNumbers) return;
var hText = mover.offsetHeight, hEditor = scroller.clientHeight;
gutter.style.height = (hText - hEditor < 2 ? hEditor : hText) + "px";
- var html = [], i = showingFrom;
+ var html = [], i = showingFrom, normalNode;
doc.iter(showingFrom, Math.max(showingTo, showingFrom + 1), function(line) {
if (line.hidden) {
html.push("<pre></pre>");
@@ -963,30 +1091,62 @@ var CodeMirror = (function() {
html.push((marker && marker.style ? '<pre class="' + marker.style + '">' : "<pre>"), text);
for (var j = 1; j < line.height; ++j) html.push("<br/>&#160;");
html.push("</pre>");
+ if (!marker) normalNode = i;
}
++i;
});
gutter.style.display = "none";
gutterText.innerHTML = html.join("");
- var minwidth = String(doc.size).length, firstNode = gutterText.firstChild, val = eltText(firstNode), pad = "";
- while (val.length + pad.length < minwidth) pad += "\u00a0";
- if (pad) firstNode.insertBefore(targetDocument.createTextNode(pad), firstNode.firstChild);
+ // Make sure scrolling doesn't cause number gutter size to pop
+ if (normalNode != null) {
+ var node = gutterText.childNodes[normalNode - showingFrom];
+ var minwidth = String(doc.size).length, val = eltText(node), pad = "";
+ while (val.length + pad.length < minwidth) pad += "\u00a0";
+ if (pad) node.insertBefore(document.createTextNode(pad), node.firstChild);
+ }
gutter.style.display = "";
+ var resized = Math.abs((parseInt(lineSpace.style.marginLeft) || 0) - gutter.offsetWidth) > 2;
lineSpace.style.marginLeft = gutter.offsetWidth + "px";
gutterDirty = false;
+ return resized;
}
- function updateCursor() {
- var head = sel.inverted ? sel.from : sel.to, lh = textHeight();
- var pos = localCoords(head, true);
+ function updateSelection() {
+ var collapsed = posEq(sel.from, sel.to);
+ var fromPos = localCoords(sel.from, true);
+ var toPos = collapsed ? fromPos : localCoords(sel.to, true);
+ var headPos = sel.inverted ? fromPos : toPos, th = textHeight();
var wrapOff = eltOffset(wrapper), lineOff = eltOffset(lineDiv);
- inputDiv.style.top = (pos.y + lineOff.top - wrapOff.top) + "px";
- inputDiv.style.left = (pos.x + lineOff.left - wrapOff.left) + "px";
- if (posEq(sel.from, sel.to)) {
- cursor.style.top = pos.y + "px";
- cursor.style.left = (options.lineWrapping ? Math.min(pos.x, lineSpace.offsetWidth) : pos.x) + "px";
+ inputDiv.style.top = Math.max(0, Math.min(scroller.offsetHeight, headPos.y + lineOff.top - wrapOff.top)) + "px";
+ inputDiv.style.left = Math.max(0, Math.min(scroller.offsetWidth, headPos.x + lineOff.left - wrapOff.left)) + "px";
+ if (collapsed) {
+ cursor.style.top = headPos.y + "px";
+ cursor.style.left = (options.lineWrapping ? Math.min(headPos.x, lineSpace.offsetWidth) : headPos.x) + "px";
cursor.style.display = "";
+ selectionDiv.style.display = "none";
+ } else {
+ var sameLine = fromPos.y == toPos.y, html = "";
+ var clientWidth = lineSpace.clientWidth || lineSpace.offsetWidth;
+ var clientHeight = lineSpace.clientHeight || lineSpace.offsetHeight;
+ function add(left, top, right, height) {
+ var rstyle = quirksMode ? "width: " + (!right ? clientWidth : clientWidth - right - left) + "px"
+ : "right: " + right + "px";
+ html += '<div class="CodeMirror-selected" style="position: absolute; left: ' + left +
+ 'px; top: ' + top + 'px; ' + rstyle + '; height: ' + height + 'px"></div>';
+ }
+ if (sel.from.ch && fromPos.y >= 0) {
+ var right = sameLine ? clientWidth - toPos.x : 0;
+ add(fromPos.x, fromPos.y, right, th);
+ }
+ var middleStart = Math.max(0, fromPos.y + (sel.from.ch ? th : 0));
+ var middleHeight = Math.min(toPos.y, clientHeight) - middleStart;
+ if (middleHeight > 0.2 * th)
+ add(0, middleStart, 0, middleHeight);
+ if ((!sameLine || !sel.from.ch) && toPos.y < clientHeight - .5 * th)
+ add(0, toPos.y, clientWidth - toPos.x, th);
+ selectionDiv.innerHTML = html;
+ cursor.style.display = "none";
+ selectionDiv.style.display = "";
}
- else cursor.style.display = "none";
}
function setShift(val) {
@@ -1012,37 +1172,32 @@ var CodeMirror = (function() {
if (posLess(to, from)) {var tmp = to; to = from; from = tmp;}
// Skip over hidden lines.
- if (from.line != oldFrom) from = skipHidden(from, oldFrom, sel.from.ch);
+ if (from.line != oldFrom) {
+ var from1 = skipHidden(from, oldFrom, sel.from.ch);
+ // If there is no non-hidden line left, force visibility on current line
+ if (!from1) setLineHidden(from.line, false);
+ else from = from1;
+ }
if (to.line != oldTo) to = skipHidden(to, oldTo, sel.to.ch);
if (posEq(from, to)) sel.inverted = false;
else if (posEq(from, sel.to)) sel.inverted = false;
else if (posEq(to, sel.from)) sel.inverted = true;
- // Some ugly logic used to only mark the lines that actually did
- // see a change in selection as changed, rather than the whole
- // selected range.
- if (posEq(from, to)) {
- if (!posEq(sel.from, sel.to))
- changes.push({from: oldFrom, to: oldTo + 1});
- }
- else if (posEq(sel.from, sel.to)) {
- changes.push({from: from.line, to: to.line + 1});
- }
- else {
- if (!posEq(from, sel.from)) {
- if (from.line < oldFrom)
- changes.push({from: from.line, to: Math.min(to.line, oldFrom) + 1});
- else
- changes.push({from: oldFrom, to: Math.min(oldTo, from.line) + 1});
- }
- if (!posEq(to, sel.to)) {
- if (to.line < oldTo)
- changes.push({from: Math.max(oldFrom, from.line), to: oldTo + 1});
- else
- changes.push({from: Math.max(from.line, oldTo), to: to.line + 1});
+ if (options.autoClearEmptyLines && posEq(sel.from, sel.to)) {
+ var head = sel.inverted ? from : to;
+ if (head.line != sel.from.line && sel.from.line < doc.size) {
+ var oldLine = getLine(sel.from.line);
+ if (/^\s+$/.test(oldLine.text))
+ setTimeout(operation(function() {
+ if (oldLine.parent && /^\s+$/.test(oldLine.text)) {
+ var no = lineNo(oldLine);
+ replaceRange("", {line: no, ch: 0}, {line: no, ch: oldLine.text.length});
+ }
+ }, 10));
}
}
+
sel.from = from; sel.to = to;
selectionChanged = true;
}
@@ -1053,13 +1208,14 @@ var CodeMirror = (function() {
var line = getLine(lNo);
if (!line.hidden) {
var ch = pos.ch;
- if (ch > oldCh || ch > line.text.length) ch = line.text.length;
+ if (toEnd || ch > oldCh || ch > line.text.length) ch = line.text.length;
return {line: lNo, ch: ch};
}
lNo += dir;
}
}
var line = getLine(pos.line);
+ var toEnd = pos.ch == line.text.length && pos.ch != oldCh;
if (!line.hidden) return pos;
if (pos.line >= oldLine) return getNonHidden(1) || getNonHidden(-1);
else return getNonHidden(-1) || getNonHidden(1);
@@ -1123,9 +1279,10 @@ var CodeMirror = (function() {
function moveV(dir, unit) {
var dist = 0, pos = localCoords(sel.inverted ? sel.from : sel.to, true);
if (goalColumn != null) pos.x = goalColumn;
- if (unit == "page") dist = scroller.clientHeight;
+ if (unit == "page") dist = Math.min(scroller.clientHeight, window.innerHeight || document.documentElement.clientHeight);
else if (unit == "line") dist = textHeight();
var target = coordsChar(pos.x, pos.y + dist * dir + 2);
+ if (unit == "page") scroller.scrollTop += localCoords(target, true).y - pos.y;
setCursor(target.line, target.ch, true);
goalColumn = pos.x;
}
@@ -1138,7 +1295,7 @@ var CodeMirror = (function() {
setSelectionUser({line: pos.line, ch: start}, {line: pos.line, ch: end});
}
function selectLine(line) {
- setSelectionUser({line: line, ch: 0}, {line: line, ch: getLine(line).text.length});
+ setSelectionUser({line: line, ch: 0}, clipPos({line: line + 1, ch: 0}));
}
function indentSelected(mode) {
if (posEq(sel.from, sel.to)) return indentLine(sel.from.line, mode);
@@ -1211,16 +1368,14 @@ var CodeMirror = (function() {
}
changes.push({from: 0, to: doc.size});
}
- function computeTabText() {
- for (var str = '<span class="cm-tab">', i = 0; i < options.tabSize; ++i) str += " ";
- return str + "</span>";
- }
- function tabsChanged() {
- tabText = computeTabText();
- updateDisplay(true);
+ function makeTab(col) {
+ var w = options.tabSize - col % options.tabSize, cached = tabCache[w];
+ if (cached) return cached;
+ for (var str = '<span class="cm-tab">', i = 0; i < w; ++i) str += " ";
+ return (tabCache[w] = {html: str + "</span>", width: w});
}
function themeChanged() {
- scroller.className = scroller.className.replace(/\s*cm-s-\w+/g, "") +
+ scroller.className = scroller.className.replace(/\s*cm-s-\S+/g, "") +
options.theme.replace(/(^|\s)\s*/g, " cm-s-");
}
@@ -1233,7 +1388,7 @@ var CodeMirror = (function() {
var lineN = lineNo(line);
min = Math.min(min, lineN); max = Math.max(max, lineN);
for (var j = 0; j < mk.length; ++j)
- if (mk[j].set == this.set) mk.splice(j--, 1);
+ if (mk[j].marker == this) mk.splice(j--, 1);
}
if (min != Infinity)
changes.push({from: min, to: max + 1});
@@ -1244,7 +1399,7 @@ var CodeMirror = (function() {
var line = this.set[i], mk = line.marked;
for (var j = 0; j < mk.length; ++j) {
var mark = mk[j];
- if (mark.set == this.set) {
+ if (mark.marker == this) {
if (mark.from != null || mark.to != null) {
var found = lineNo(line);
if (found != null) {
@@ -1261,8 +1416,9 @@ var CodeMirror = (function() {
function markText(from, to, className) {
from = clipPos(from); to = clipPos(to);
var tm = new TextMarker();
+ if (!posLess(from, to)) return tm;
function add(line, from, to, className) {
- getLine(line).addMark(new MarkedText(from, to, className, tm.set));
+ getLine(line).addMark(new MarkedText(from, to, className, tm));
}
if (from.line == to.line) add(from.line, from.ch, to.ch, className);
else {
@@ -1282,6 +1438,19 @@ var CodeMirror = (function() {
return bm;
}
+ function findMarksAt(pos) {
+ pos = clipPos(pos);
+ var markers = [], marked = getLine(pos.line).marked;
+ if (!marked) return markers;
+ for (var i = 0, e = marked.length; i < e; ++i) {
+ var m = marked[i];
+ if ((m.from == null || m.from <= pos.ch) &&
+ (m.to == null || m.to >= pos.ch))
+ markers.push(m.marker || m);
+ }
+ return markers;
+ }
+
function addGutterMarker(line, text, className) {
if (typeof line == "number") line = getLine(clipLine(line));
line.gutterMarker = {text: text, style: className};
@@ -1303,10 +1472,11 @@ var CodeMirror = (function() {
else return null;
return line;
}
- function setLineClass(handle, className) {
+ function setLineClass(handle, className, bgClassName) {
return changeLine(handle, function(line) {
- if (line.className != className) {
+ if (line.className != className || line.bgClassName != bgClassName) {
line.className = className;
+ line.bgClassName = bgClassName;
return true;
}
});
@@ -1316,9 +1486,14 @@ var CodeMirror = (function() {
if (line.hidden != hidden) {
line.hidden = hidden;
updateLineHeight(line, hidden ? 0 : 1);
- if (hidden && (sel.from.line == no || sel.to.line == no))
- setSelection(skipHidden(sel.from, sel.from.line, sel.from.ch),
- skipHidden(sel.to, sel.to.line, sel.to.ch));
+ var fline = sel.from.line, tline = sel.to.line;
+ if (hidden && (fline == no || tline == no)) {
+ var from = fline == no ? skipHidden({line: fline, ch: 0}, fline, 0) : sel.from;
+ var to = tline == no ? skipHidden({line: tline, ch: 0}, tline, 0) : sel.to;
+ // Can't hide the last visible line, we'd have no place to put the cursor
+ if (!to) return;
+ setSelection(from, to);
+ }
return (gutterDirty = true);
}
});
@@ -1337,7 +1512,7 @@ var CodeMirror = (function() {
}
var marker = line.gutterMarker;
return {line: n, handle: line, text: line.text, markerText: marker && marker.text,
- markerClass: marker && marker.style, lineClass: line.className};
+ markerClass: marker && marker.style, lineClass: line.className, bgClass: line.bgClassName};
}
function stringWidth(str) {
@@ -1351,8 +1526,7 @@ var CodeMirror = (function() {
if (x <= 0) return 0;
var lineObj = getLine(line), text = lineObj.text;
function getX(len) {
- measure.innerHTML = "<pre><span>" + lineObj.getHTML(null, null, false, tabText, len) + "</span></pre>";
- return measure.firstChild.firstChild.offsetWidth;
+ return measureLine(lineObj, len).left;
}
var from = 0, fromX = 0, to = text.length, toX;
// Guess a suitable upper bound for our search.
@@ -1375,21 +1549,16 @@ var CodeMirror = (function() {
}
}
- var tempId = Math.floor(Math.random() * 0xffffff).toString(16);
+ var tempId = "CodeMirror-temp-" + Math.floor(Math.random() * 0xffffff).toString(16);
function measureLine(line, ch) {
- var extra = "";
- // Include extra text at the end to make sure the measured line is wrapped in the right way.
- if (options.lineWrapping) {
- var end = line.text.indexOf(" ", ch + 2);
- extra = htmlEscape(line.text.slice(ch + 1, end < 0 ? line.text.length : end + (ie ? 5 : 0)));
- }
- measure.innerHTML = "<pre>" + line.getHTML(null, null, false, tabText, ch) +
- '<span id="CodeMirror-temp-' + tempId + '">' + htmlEscape(line.text.charAt(ch) || " ") + "</span>" +
- extra + "</pre>";
- var elt = document.getElementById("CodeMirror-temp-" + tempId);
+ if (ch == 0) return {top: 0, left: 0};
+ var wbr = options.lineWrapping && ch < line.text.length &&
+ spanAffectsWrapping.test(line.text.slice(ch - 1, ch + 1));
+ measure.innerHTML = "<pre>" + line.getHTML(makeTab, ch, tempId, wbr) + "</pre>";
+ var elt = document.getElementById(tempId);
var top = elt.offsetTop, left = elt.offsetLeft;
// Older IEs report zero offsets for spans directly after a wrap
- if (ie && ch && top == 0 && left == 0) {
+ if (ie && top == 0 && left == 0) {
var backup = document.createElement("span");
backup.innerHTML = "x";
elt.parentNode.insertBefore(backup, elt.nextSibling);
@@ -1486,7 +1655,7 @@ var CodeMirror = (function() {
return coordsChar(x - offL.left, y - offL.top);
}
function onContextMenu(e) {
- var pos = posFromMouse(e);
+ var pos = posFromMouse(e), scrollPos = scroller.scrollTop;
if (!pos || window.opera) return; // Opera is difficult.
if (posEq(sel.from, sel.to) || posLess(pos, sel.from) || !posLess(pos, sel.to))
operation(setCursor)(pos.line, pos.ch);
@@ -1499,12 +1668,13 @@ var CodeMirror = (function() {
leaveInputAlone = true;
var val = input.value = getSelection();
focusInput();
- input.select();
+ selectInput(input);
function rehide() {
var newVal = splitLines(input.value).join("\n");
if (newVal != val) operation(replaceSelection)(newVal, "end");
inputDiv.style.position = "relative";
input.style.cssText = oldCSS;
+ if (ie_lt9) scroller.scrollTop = scrollPos;
leaveInputAlone = false;
resetInput(true);
slowPoll();
@@ -1516,8 +1686,7 @@ var CodeMirror = (function() {
mouseup();
setTimeout(rehide, 20);
}, true);
- }
- else {
+ } else {
setTimeout(rehide, 50);
}
}
@@ -1634,13 +1803,17 @@ var CodeMirror = (function() {
var changed = line.highlight(mode, state, options.tabSize);
if (changed) realChange = true;
line.stateAfter = copyState(mode, state);
+ var done = null;
if (compare) {
- if (hadState && compare(hadState, state)) return true;
- } else {
+ var same = hadState && compare(hadState, state);
+ if (same != Pass) done = !!same;
+ }
+ if (done == null) {
if (changed !== false || !hadState) unchanged = 0;
else if (++unchanged > 3 && (!mode.indent || mode.indent(hadState, "") == mode.indent(state, "")))
- return true;
+ done = true;
}
+ if (done) return true;
++i;
});
if (bail) return;
@@ -1667,7 +1840,7 @@ var CodeMirror = (function() {
if (selectionChanged) reScroll = !scrollCursorIntoView();
if (changes.length) updated = updateDisplay(changes, true);
else {
- if (selectionChanged) updateCursor();
+ if (selectionChanged) updateSelection();
if (gutterDirty) updateGutter();
}
if (reScroll) scrollCursorIntoView();
@@ -1700,6 +1873,11 @@ var CodeMirror = (function() {
};
}
+ function compoundChange(f) {
+ history.startCompound();
+ try { return f(); } finally { history.endCompound(); }
+ }
+
for (var ext in extensions)
if (extensions.propertyIsEnumerable(ext) &&
!instance.propertyIsEnumerable(ext))
@@ -1714,17 +1892,21 @@ var CodeMirror = (function() {
theme: "default",
indentUnit: 2,
indentWithTabs: false,
+ smartIndent: true,
tabSize: 4,
keyMap: "default",
extraKeys: null,
electricChars: true,
+ autoClearEmptyLines: false,
onKeyEvent: null,
+ onDragEvent: null,
lineWrapping: false,
lineNumbers: false,
gutter: false,
fixedGutter: false,
firstLineNumber: 1,
readOnly: false,
+ dragDrop: true,
onChange: null,
onCursorActivity: null,
onGutterClick: null,
@@ -1737,34 +1919,39 @@ var CodeMirror = (function() {
pollInterval: 100,
undoDepth: 40,
tabindex: null,
- document: window.document
+ autofocus: null
};
- var mac = /Mac/.test(navigator.platform);
+ var ios = /AppleWebKit/.test(navigator.userAgent) && /Mobile\/\w+/.test(navigator.userAgent);
+ var mac = ios || /Mac/.test(navigator.platform);
var win = /Win/.test(navigator.platform);
// Known modes, by name and by MIME
- var modes = {}, mimeModes = {};
+ var modes = CodeMirror.modes = {}, mimeModes = CodeMirror.mimeModes = {};
CodeMirror.defineMode = function(name, mode) {
if (!CodeMirror.defaults.mode && name != "null") CodeMirror.defaults.mode = name;
+ if (arguments.length > 2) {
+ mode.dependencies = [];
+ for (var i = 2; i < arguments.length; ++i) mode.dependencies.push(arguments[i]);
+ }
modes[name] = mode;
};
CodeMirror.defineMIME = function(mime, spec) {
mimeModes[mime] = spec;
};
- CodeMirror.getMode = function(options, spec) {
+ CodeMirror.resolveMode = function(spec) {
if (typeof spec == "string" && mimeModes.hasOwnProperty(spec))
spec = mimeModes[spec];
- if (typeof spec == "string")
- var mname = spec, config = {};
- else if (spec != null)
- var mname = spec.name, config = spec;
- var mfactory = modes[mname];
- if (!mfactory) {
- if (window.console) console.warn("No mode " + mname + " found, falling back to plain text.");
- return CodeMirror.getMode(options, "text/plain");
- }
- return mfactory(options, config || {});
+ else if (typeof spec == "string" && /^[\w\-]+\/[\w\-]+\+xml$/.test(spec))
+ return CodeMirror.resolveMode("application/xml");
+ if (typeof spec == "string") return {name: spec};
+ else return spec || {name: "null"};
+ };
+ CodeMirror.getMode = function(options, spec) {
+ var spec = CodeMirror.resolveMode(spec);
+ var mfactory = modes[spec.name];
+ if (!mfactory) return CodeMirror.getMode(options, "text/plain");
+ return mfactory(options, spec);
};
CodeMirror.listModes = function() {
var list = [];
@@ -1838,7 +2025,7 @@ var CodeMirror = (function() {
keyMap.basic = {
"Left": "goCharLeft", "Right": "goCharRight", "Up": "goLineUp", "Down": "goLineDown",
"End": "goLineEnd", "Home": "goLineStartSmart", "PageUp": "goPageUp", "PageDown": "goPageDown",
- "Delete": "delCharRight", "Backspace": "delCharLeft", "Tab": "indentMore", "Shift-Tab": "indentLess",
+ "Delete": "delCharRight", "Backspace": "delCharLeft", "Tab": "insertTab", "Shift-Tab": "indentAuto",
"Enter": "newlineAndIndent", "Insert": "toggleOverwrite"
};
// Note that the save and find-related commands aren't defined by
@@ -1849,6 +2036,7 @@ var CodeMirror = (function() {
"Ctrl-Left": "goWordLeft", "Ctrl-Right": "goWordRight", "Alt-Left": "goLineStart", "Alt-Right": "goLineEnd",
"Ctrl-Backspace": "delWordLeft", "Ctrl-Delete": "delWordRight", "Ctrl-S": "save", "Ctrl-F": "find",
"Ctrl-G": "findNext", "Shift-Ctrl-G": "findPrev", "Shift-Ctrl-F": "replace", "Shift-Ctrl-R": "replaceAll",
+ "Ctrl-[": "indentLess", "Ctrl-]": "indentMore",
fallthrough: "basic"
};
keyMap.macDefault = {
@@ -1857,6 +2045,7 @@ var CodeMirror = (function() {
"Alt-Right": "goWordRight", "Cmd-Left": "goLineStart", "Cmd-Right": "goLineEnd", "Alt-Backspace": "delWordLeft",
"Ctrl-Alt-Backspace": "delWordRight", "Alt-Delete": "delWordRight", "Cmd-S": "save", "Cmd-F": "find",
"Cmd-G": "findNext", "Shift-Cmd-G": "findPrev", "Cmd-Alt-F": "replace", "Shift-Cmd-Alt-F": "replaceAll",
+ "Cmd-[": "indentLess", "Cmd-]": "indentMore",
fallthrough: ["basic", "emacsy"]
};
keyMap["default"] = mac ? keyMap.macDefault : keyMap.pcDefault;
@@ -1867,23 +2056,33 @@ var CodeMirror = (function() {
"Alt-D": "delWordRight", "Alt-Backspace": "delWordLeft", "Ctrl-K": "killLine", "Ctrl-T": "transposeChars"
};
- function lookupKey(name, extraMap, map) {
- function lookup(name, map, ft) {
+ function getKeyMap(val) {
+ if (typeof val == "string") return keyMap[val];
+ else return val;
+ }
+ function lookupKey(name, extraMap, map, handle, stop) {
+ function lookup(map) {
+ map = getKeyMap(map);
var found = map[name];
- if (found != null) return found;
- if (ft == null) ft = map.fallthrough;
- if (ft == null) return map.catchall;
- if (typeof ft == "string") return lookup(name, keyMap[ft]);
- for (var i = 0, e = ft.length; i < e; ++i) {
- found = lookup(name, keyMap[ft[i]]);
- if (found != null) return found;
+ if (found != null && handle(found)) return true;
+ if (map.nofallthrough) {
+ if (stop) stop();
+ return true;
}
- return null;
+ var fallthrough = map.fallthrough;
+ if (fallthrough == null) return false;
+ if (Object.prototype.toString.call(fallthrough) != "[object Array]")
+ return lookup(fallthrough);
+ for (var i = 0, e = fallthrough.length; i < e; ++i) {
+ if (lookup(fallthrough[i])) return true;
+ }
+ return false;
}
- return extraMap ? lookup(name, extraMap, map) : lookup(name, keyMap[map]);
+ if (extraMap && lookup(extraMap)) return true;
+ return lookup(map);
}
function isModifierKey(event) {
- var name = keyNames[event.keyCode];
+ var name = keyNames[e_prop(event, "keyCode")];
return name == "Ctrl" || name == "Alt" || name == "Shift" || name == "Mod";
}
@@ -1892,6 +2091,8 @@ var CodeMirror = (function() {
options.value = textarea.value;
if (!options.tabindex && textarea.tabindex)
options.tabindex = textarea.tabindex;
+ if (options.autofocus == null && textarea.getAttribute("autofocus") != null)
+ options.autofocus = true;
function save() {textarea.value = instance.getValue();}
if (textarea.form) {
@@ -2003,34 +2204,34 @@ var CodeMirror = (function() {
};
CodeMirror.StringStream = StringStream;
- function MarkedText(from, to, className, set) {
- this.from = from; this.to = to; this.style = className; this.set = set;
+ function MarkedText(from, to, className, marker) {
+ this.from = from; this.to = to; this.style = className; this.marker = marker;
}
MarkedText.prototype = {
- attach: function(line) { this.set.push(line); },
+ attach: function(line) { this.marker.set.push(line); },
detach: function(line) {
- var ix = indexOf(this.set, line);
- if (ix > -1) this.set.splice(ix, 1);
+ var ix = indexOf(this.marker.set, line);
+ if (ix > -1) this.marker.set.splice(ix, 1);
},
split: function(pos, lenBefore) {
if (this.to <= pos && this.to != null) return null;
var from = this.from < pos || this.from == null ? null : this.from - pos + lenBefore;
var to = this.to == null ? null : this.to - pos + lenBefore;
- return new MarkedText(from, to, this.style, this.set);
+ return new MarkedText(from, to, this.style, this.marker);
},
- dup: function() { return new MarkedText(null, null, this.style, this.set); },
+ dup: function() { return new MarkedText(null, null, this.style, this.marker); },
clipTo: function(fromOpen, from, toOpen, to, diff) {
- if (this.from != null && this.from >= from)
- this.from = Math.max(to, this.from) + diff;
- if (this.to != null && this.to > from)
- this.to = to < this.to ? this.to + diff : from;
if (fromOpen && to > this.from && (to < this.to || this.to == null))
this.from = null;
+ else if (this.from != null && this.from >= from)
+ this.from = Math.max(to, this.from) + diff;
if (toOpen && (from < this.to || this.to == null) && (from > this.from || this.from == null))
this.to = null;
+ else if (this.to != null && this.to > from)
+ this.to = to < this.to ? this.to + diff : from;
},
isDead: function() { return this.from != null && this.to != null && this.from >= this.to; },
- sameSet: function(x) { return this.set == x.set; }
+ sameSet: function(x) { return this.marker == x.marker; }
};
function Bookmark(pos) {
@@ -2073,7 +2274,7 @@ var CodeMirror = (function() {
this.styles = styles || [text, null];
this.text = text;
this.height = 1;
- this.marked = this.gutterMarker = this.className = this.handlers = null;
+ this.marked = this.gutterMarker = this.className = this.bgClassName = this.handlers = null;
this.stateAfter = this.parent = this.hidden = null;
}
Line.inheritMarks = function(text, orig) {
@@ -2100,7 +2301,8 @@ var CodeMirror = (function() {
this.stateAfter = null;
if (mk) {
var diff = text.length - (to - from);
- for (var i = 0, mark = mk[i]; i < mk.length; ++i) {
+ for (var i = 0; i < mk.length; ++i) {
+ var mark = mk[i];
mark.clipTo(from == null, from || 0, to_ == null, to, diff);
if (mark.isDead()) {mark.detach(this); mk.splice(i--, 1);}
}
@@ -2118,6 +2320,7 @@ var CodeMirror = (function() {
if (newmark) {
if (!taken.marked) taken.marked = [];
taken.marked.push(newmark); newmark.attach(taken);
+ if (newmark == mark) mk.splice(i--, 1);
}
}
}
@@ -2227,73 +2430,114 @@ var CodeMirror = (function() {
indentation: function(tabSize) {return countColumn(this.text, null, tabSize);},
// Produces an HTML fragment for the line, taking selection,
// marking, and highlighting into account.
- getHTML: function(sfrom, sto, includePre, tabText, endAt) {
- var html = [], first = true;
- if (includePre)
- html.push(this.className ? '<pre class="' + this.className + '">': "<pre>");
- function span(text, style) {
+ getHTML: function(makeTab, wrapAt, wrapId, wrapWBR) {
+ var html = [], first = true, col = 0;
+ function span_(text, style) {
if (!text) return;
// Work around a bug where, in some compat modes, IE ignores leading spaces
if (first && ie && text.charAt(0) == " ") text = "\u00a0" + text.slice(1);
first = false;
- if (style) html.push('<span class="', style, '">', htmlEscape(text).replace(/\t/g, tabText), "</span>");
- else html.push(htmlEscape(text).replace(/\t/g, tabText));
+ if (text.indexOf("\t") == -1) {
+ col += text.length;
+ var escaped = htmlEscape(text);
+ } else {
+ var escaped = "";
+ for (var pos = 0;;) {
+ var idx = text.indexOf("\t", pos);
+ if (idx == -1) {
+ escaped += htmlEscape(text.slice(pos));
+ col += text.length - pos;
+ break;
+ } else {
+ col += idx - pos;
+ var tab = makeTab(col);
+ escaped += htmlEscape(text.slice(pos, idx)) + tab.html;
+ col += tab.width;
+ pos = idx + 1;
+ }
+ }
+ }
+ if (style) html.push('<span class="', style, '">', escaped, "</span>");
+ else html.push(escaped);
+ }
+ var span = span_;
+ if (wrapAt != null) {
+ var outPos = 0, open = "<span id=\"" + wrapId + "\">";
+ span = function(text, style) {
+ var l = text.length;
+ if (wrapAt >= outPos && wrapAt < outPos + l) {
+ if (wrapAt > outPos) {
+ span_(text.slice(0, wrapAt - outPos), style);
+ // See comment at the definition of spanAffectsWrapping
+ if (wrapWBR) html.push("<wbr>");
+ }
+ html.push(open);
+ span_(text.slice(wrapAt - outPos), style);
+ html.push("</span>");
+ wrapAt--;
+ outPos += l;
+ } else {
+ outPos += l;
+ span_(text, style);
+ // Output empty wrapper when at end of line
+ if (outPos == wrapAt && outPos == len) html.push(open + "</span>");
+ // Stop outputting HTML when gone sufficiently far beyond measure
+ else if (outPos > wrapAt + 10 && /\s/.test(text)) span = function(){};
+ }
+ }
}
+
var st = this.styles, allText = this.text, marked = this.marked;
- if (sfrom == sto) sfrom = null;
var len = allText.length;
- if (endAt != null) len = Math.min(endAt, len);
+ function styleToClass(style) {
+ if (!style) return null;
+ return "cm-" + style.replace(/ +/g, " cm-");
+ }
- if (!allText && endAt == null)
- span(" ", sfrom != null && sto == null ? "CodeMirror-selected" : null);
- else if (!marked && sfrom == null)
+ if (!allText && wrapAt == null) {
+ span(" ");
+ } else if (!marked || !marked.length) {
for (var i = 0, ch = 0; ch < len; i+=2) {
var str = st[i], style = st[i+1], l = str.length;
if (ch + l > len) str = str.slice(0, len - ch);
ch += l;
- span(str, style && "cm-" + style);
+ span(str, styleToClass(style));
}
- else {
+ } else {
var pos = 0, i = 0, text = "", style, sg = 0;
- var markpos = -1, mark = null;
- function nextMark() {
- if (marked) {
- markpos += 1;
- mark = (markpos < marked.length) ? marked[markpos] : null;
+ var nextChange = marked[0].from || 0, marks = [], markpos = 0;
+ function advanceMarks() {
+ var m;
+ while (markpos < marked.length &&
+ ((m = marked[markpos]).from == pos || m.from == null)) {
+ if (m.style != null) marks.push(m);
+ ++markpos;
+ }
+ nextChange = markpos < marked.length ? marked[markpos].from : Infinity;
+ for (var i = 0; i < marks.length; ++i) {
+ var to = marks[i].to || Infinity;
+ if (to == pos) marks.splice(i--, 1);
+ else nextChange = Math.min(to, nextChange);
}
}
- nextMark();
+ var m = 0;
while (pos < len) {
- var upto = len;
- var extraStyle = "";
- if (sfrom != null) {
- if (sfrom > pos) upto = sfrom;
- else if (sto == null || sto > pos) {
- extraStyle = " CodeMirror-selected";
- if (sto != null) upto = Math.min(upto, sto);
- }
- }
- while (mark && mark.to != null && mark.to <= pos) nextMark();
- if (mark) {
- if (mark.from > pos) upto = Math.min(upto, mark.from);
- else {
- extraStyle += " " + mark.style;
- if (mark.to != null) upto = Math.min(upto, mark.to);
+ if (nextChange == pos) advanceMarks();
+ var upto = Math.min(len, nextChange);
+ while (true) {
+ if (text) {
+ var end = pos + text.length;
+ var appliedStyle = style;
+ for (var j = 0; j < marks.length; ++j)
+ appliedStyle = (appliedStyle ? appliedStyle + " " : "") + marks[j].style;
+ span(end > upto ? text.slice(0, upto - pos) : text, appliedStyle);
+ if (end >= upto) {text = text.slice(upto - pos); pos = upto; break;}
+ pos = end;
}
- }
- for (;;) {
- var end = pos + text.length;
- var appliedStyle = style;
- if (extraStyle) appliedStyle = style ? style + extraStyle : extraStyle;
- span(end > upto ? text.slice(0, upto - pos) : text, appliedStyle);
- if (end >= upto) {text = text.slice(upto - pos); pos = upto; break;}
- pos = end;
- text = st[i++]; style = "cm-" + st[i++];
+ text = st[i++]; style = styleToClass(st[i++]);
}
}
- if (sfrom != null && sto == null) span(" ", "CodeMirror-selected");
}
- if (includePre) html.push("</pre>");
return html.join("");
},
cleanUp: function() {
@@ -2345,7 +2589,11 @@ var CodeMirror = (function() {
},
insertHeight: function(at, lines, height) {
this.height += height;
- this.lines.splice.apply(this.lines, [at, 0].concat(lines));
+ // The trick below is apparently too advanced for IE, which
+ // occasionally corrupts this.lines (duplicating elements) when
+ // it is used.
+ if (ie) this.lines = this.lines.slice(0, at).concat(lines).concat(this.lines.slice(at));
+ else this.lines.splice.apply(this.lines, [at, 0].concat(lines));
for (var i = 0, e = lines.length; i < e; ++i) lines[i].parent = this;
},
iterN: function(at, n, op) {
@@ -2384,6 +2632,7 @@ var CodeMirror = (function() {
var lines = [];
this.collapse(lines);
this.children = [new LeafChunk(lines)];
+ this.children[0].parent = this;
}
},
collapse: function(lines) {
@@ -2510,31 +2759,36 @@ var CodeMirror = (function() {
function History() {
this.time = 0;
this.done = []; this.undone = [];
+ this.compound = 0;
+ this.closed = false;
}
History.prototype = {
addChange: function(start, added, old) {
this.undone.length = 0;
- var time = +new Date, last = this.done[this.done.length - 1];
- if (time - this.time > 400 || !last ||
- last.start > start + added || last.start + last.added < start - last.added + last.old.length)
- this.done.push({start: start, added: added, old: old});
- else {
- var oldoff = 0;
- if (start < last.start) {
- for (var i = last.start - start - 1; i >= 0; --i)
- last.old.unshift(old[i]);
- last.added += last.start - start;
- last.start = start;
- }
- else if (last.start < start) {
- oldoff = start - last.start;
- added += oldoff;
- }
- for (var i = last.added - oldoff, e = old.length; i < e; ++i)
- last.old.push(old[i]);
- if (last.added < added) last.added = added;
+ var time = +new Date, cur = this.done[this.done.length - 1], last = cur && cur[cur.length - 1];
+ var dtime = time - this.time;
+
+ if (this.compound && cur && !this.closed) {
+ cur.push({start: start, added: added, old: old});
+ } else if (dtime > 400 || !last || this.closed ||
+ last.start > start + old.length || last.start + last.added < start) {
+ this.done.push([{start: start, added: added, old: old}]);
+ this.closed = false;
+ } else {
+ var startBefore = Math.max(0, last.start - start),
+ endAfter = Math.max(0, (start + old.length) - (last.start + last.added));
+ for (var i = startBefore; i > 0; --i) last.old.unshift(old[i - 1]);
+ for (var i = endAfter; i > 0; --i) last.old.push(old[old.length - i]);
+ if (startBefore) last.start = start;
+ last.added += added - (old.length - startBefore - endAfter);
}
this.time = time;
+ },
+ startCompound: function() {
+ if (!this.compound++) this.closed = true;
+ },
+ endCompound: function() {
+ if (!--this.compound) this.closed = true;
}
};
@@ -2566,6 +2820,13 @@ var CodeMirror = (function() {
else if (e.button & 4) return 2;
}
+ // Allow 3rd-party code to override event properties by adding an override
+ // object to an event object.
+ function e_prop(e, prop) {
+ var overridden = e.override && e.override.hasOwnProperty(prop);
+ return overridden ? e.override[prop] : e[prop];
+ }
+
// Event handler registration. If disconnect is true, it'll return a
// function that unregisters the handler.
function connect(node, type, handler, disconnect) {
@@ -2584,26 +2845,45 @@ var CodeMirror = (function() {
function Delayed() {this.id = null;}
Delayed.prototype = {set: function(ms, f) {clearTimeout(this.id); this.id = setTimeout(f, ms);}};
- // Detect drag-and-drop
- var dragAndDrop = function() {
- // IE8 has ondragstart and ondrop properties, but doesn't seem to
- // actually support ondragstart the way it's supposed to work.
- if (/MSIE [1-8]\b/.test(navigator.userAgent)) return false;
- var div = document.createElement('div');
- return "draggable" in div;
- }();
+ var Pass = CodeMirror.Pass = {toString: function(){return "CodeMirror.Pass";}};
var gecko = /gecko\/\d{7}/i.test(navigator.userAgent);
var ie = /MSIE \d/.test(navigator.userAgent);
+ var ie_lt9 = /MSIE [1-8]\b/.test(navigator.userAgent);
+ var quirksMode = ie && document.documentMode == 5;
var webkit = /WebKit\//.test(navigator.userAgent);
+ var chrome = /Chrome\//.test(navigator.userAgent);
+ var safari = /Apple Computer/.test(navigator.vendor);
+ var khtml = /KHTML\//.test(navigator.userAgent);
+
+ // Detect drag-and-drop
+ var dragAndDrop = function() {
+ // There is *some* kind of drag-and-drop support in IE6-8, but I
+ // couldn't get it to work yet.
+ if (ie_lt9) return false;
+ var div = document.createElement('div');
+ return "draggable" in div || "dragDrop" in div;
+ }();
- var lineSep = "\n";
// Feature-detect whether newlines in textareas are converted to \r\n
- (function () {
+ var lineSep = function () {
var te = document.createElement("textarea");
te.value = "foo\nbar";
- if (te.value.indexOf("\r") > -1) lineSep = "\r\n";
- }());
+ if (te.value.indexOf("\r") > -1) return "\r\n";
+ return "\n";
+ }();
+
+ // For a reason I have yet to figure out, some browsers disallow
+ // word wrapping between certain characters *only* if a new inline
+ // element is started between them. This makes it hard to reliably
+ // measure the position of things, since that requires inserting an
+ // extra span. This terribly fragile set of regexps matches the
+ // character combinations that suffer from this phenomenon on the
+ // various browsers.
+ var spanAffectsWrapping = /^$/; // Won't match any two-character string
+ if (gecko) spanAffectsWrapping = /$'/;
+ else if (safari) spanAffectsWrapping = /\-[^ \-?]|\?[^ !'\"\),.\-\/:;\?\]\}]/;
+ else if (chrome) spanAffectsWrapping = /\-[^ \-\.?]|\?[^ \-\.?\]\}:;!'\"\),\/]|[\.!\"#&%\)*+,:;=>\]|\}~][\(\{\[<]|\$'/;
// Counts the column offset in a string, taking tabs into account.
// Used mostly to find indentation.
@@ -2665,6 +2945,12 @@ var CodeMirror = (function() {
function eltText(node) {
return node.textContent || node.innerText || node.nodeValue || "";
}
+ function selectInput(node) {
+ if (ios) { // Mobile Safari apparently has a bug where select() is broken.
+ node.selectionStart = 0;
+ node.selectionEnd = node.value.length;
+ } else node.select();
+ }
// Operations on {line, ch} objects.
function posEq(a, b) {return a.line == b.line && a.ch == b.ch;}
@@ -2695,7 +2981,7 @@ var CodeMirror = (function() {
// Used to position the cursor after an undo/redo by finding the
// last edited character.
function editEnd(from, to) {
- if (!to) return from ? from.length : 0;
+ if (!to) return 0;
if (!from) return to.length;
for (var i = from.length, j = to.length; i >= 0 && j >= 0; --i, --j)
if (from.charAt(i) != to.charAt(j)) break;