| 1 |
CodeMirror.runMode = function(string, modespec, callback, options) { |
| 2 |
var mode = CodeMirror.getMode(CodeMirror.defaults, modespec); |
| 3 |
var ie = /MSIE \d/.test(navigator.userAgent); |
| 4 |
var ie_lt9 = ie && (document.documentMode == null || document.documentMode < 9); |
| 5 |
|
| 6 |
if (callback.nodeType == 1) { |
| 7 |
var tabSize = (options && options.tabSize) || CodeMirror.defaults.tabSize; |
| 8 |
var node = callback, col = 0; |
| 9 |
node.innerHTML = ""; |
| 10 |
callback = function(text, style) { |
| 11 |
if (text == "\n") { |
| 12 |
// Emitting LF or CRLF on IE8 or earlier results in an incorrect display. |
| 13 |
// Emitting a carriage return makes everything ok. |
| 14 |
node.appendChild(document.createTextNode(ie_lt9 ? '\r' : text)); |
| 15 |
col = 0; |
| 16 |
return; |
| 17 |
} |
| 18 |
var content = ""; |
| 19 |
// replace tabs |
| 20 |
for (var pos = 0;;) { |
| 21 |
var idx = text.indexOf("\t", pos); |
| 22 |
if (idx == -1) { |
| 23 |
content += text.slice(pos); |
| 24 |
col += text.length - pos; |
| 25 |
break; |
| 26 |
} else { |
| 27 |
col += idx - pos; |
| 28 |
content += text.slice(pos, idx); |
| 29 |
var size = tabSize - col % tabSize; |
| 30 |
col += size; |
| 31 |
for (var i = 0; i < size; ++i) content += " "; |
| 32 |
pos = idx + 1; |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
if (style) { |
| 37 |
var sp = node.appendChild(document.createElement("span")); |
| 38 |
sp.className = "cm-" + style.replace(/ +/g, " cm-"); |
| 39 |
sp.appendChild(document.createTextNode(content)); |
| 40 |
} else { |
| 41 |
node.appendChild(document.createTextNode(content)); |
| 42 |
} |
| 43 |
}; |
| 44 |
} |
| 45 |
|
| 46 |
var lines = CodeMirror.splitLines(string), state = CodeMirror.startState(mode); |
| 47 |
for (var i = 0, e = lines.length; i < e; ++i) { |
| 48 |
if (i) callback("\n"); |
| 49 |
var stream = new CodeMirror.StringStream(lines[i]); |
| 50 |
while (!stream.eol()) { |
| 51 |
var style = mode.token(stream, state); |
| 52 |
callback(stream.current(), style, i, stream.start, state); |
| 53 |
stream.start = stream.pos; |
| 54 |
} |
| 55 |
} |
| 56 |
}; |
| 57 |
|