Permalink
Browse files

[util/runmode] Move from innerHTML to document.create*

To work around IE innerHTML bugs
  • Loading branch information...
marijnh committed Dec 10, 2012
1 parent 99ed2b4 commit 40e5bae3fcc0b58cb241ac301b08669820fbe647
Showing with 19 additions and 20 deletions.
  1. +19 −20 lib/util/runmode.js
View
@@ -1,43 +1,44 @@
CodeMirror.runMode = function(string, modespec, callback, options) {
- function esc(str) {
- return str.replace(/[<&]/g, function(ch) { return ch == "<" ? "&lt;" : "&amp;"; });
- }
-
var mode = CodeMirror.getMode(CodeMirror.defaults, modespec);
- var isNode = callback.nodeType == 1;
- var tabSize = (options && options.tabSize) || CodeMirror.defaults.tabSize;
- if (isNode) {
- var node = callback, accum = [], col = 0;
+
+ if (callback.nodeType == 1) {
+ var tabSize = (options && options.tabSize) || CodeMirror.defaults.tabSize;
+ var node = callback, col = 0;
+ node.innerHTML = "";
callback = function(text, style) {
if (text == "\n") {
- accum.push("<br>");
+ node.appendChild(document.createElement("br"));
col = 0;
return;
}
- var escaped = "";
- // HTML-escape and replace tabs
+ var content = "";
+ // replace tabs
for (var pos = 0;;) {
var idx = text.indexOf("\t", pos);
if (idx == -1) {
- escaped += esc(text.slice(pos));
+ content += text.slice(pos);
col += text.length - pos;
break;
} else {
col += idx - pos;
- escaped += esc(text.slice(pos, idx));
+ content += text.slice(pos, idx);
var size = tabSize - col % tabSize;
col += size;
- for (var i = 0; i < size; ++i) escaped += " ";
+ for (var i = 0; i < size; ++i) content += " ";
pos = idx + 1;
}
}
- if (style)
- accum.push("<span class=\"cm-" + esc(style) + "\">" + escaped + "</span>");
- else
- accum.push(escaped);
+ if (style) {
+ var sp = node.appendChild(document.createElement("span"));
+ sp.className = "cm-" + style.replace(/ +/g, " cm-");
+ sp.appendChild(document.createTextNode(content));
+ } else {
+ node.appendChild(document.createTextNode(content));
+ }
};
}
+
var lines = CodeMirror.splitLines(string), state = CodeMirror.startState(mode);
for (var i = 0, e = lines.length; i < e; ++i) {
if (i) callback("\n");
@@ -48,6 +49,4 @@ CodeMirror.runMode = function(string, modespec, callback, options) {
stream.start = stream.pos;
}
}
- if (isNode)
- node.innerHTML = accum.join("");
};

0 comments on commit 40e5bae

Please sign in to comment.