Merge branch '2.0'
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / js / 3rdParty / codemirror / addon / runmode / runmode.js
1 (function(mod) {
2 if (typeof exports == "object" && typeof module == "object") // CommonJS
3 mod(require("../../lib/codemirror"));
4 else if (typeof define == "function" && define.amd) // AMD
5 define(["../../lib/codemirror"], mod);
6 else // Plain browser env
7 mod(CodeMirror);
8 })(function(CodeMirror) {
9 "use strict";
10
11 CodeMirror.runMode = function(string, modespec, callback, options) {
12 var mode = CodeMirror.getMode(CodeMirror.defaults, modespec);
13 var ie = /MSIE \d/.test(navigator.userAgent);
14 var ie_lt9 = ie && (document.documentMode == null || document.documentMode < 9);
15
16 if (callback.nodeType == 1) {
17 var tabSize = (options && options.tabSize) || CodeMirror.defaults.tabSize;
18 var node = callback, col = 0;
19 node.innerHTML = "";
20 callback = function(text, style) {
21 if (text == "\n") {
22 // Emitting LF or CRLF on IE8 or earlier results in an incorrect display.
23 // Emitting a carriage return makes everything ok.
24 node.appendChild(document.createTextNode(ie_lt9 ? '\r' : text));
25 col = 0;
26 return;
27 }
28 var content = "";
29 // replace tabs
30 for (var pos = 0;;) {
31 var idx = text.indexOf("\t", pos);
32 if (idx == -1) {
33 content += text.slice(pos);
34 col += text.length - pos;
35 break;
36 } else {
37 col += idx - pos;
38 content += text.slice(pos, idx);
39 var size = tabSize - col % tabSize;
40 col += size;
41 for (var i = 0; i < size; ++i) content += " ";
42 pos = idx + 1;
43 }
44 }
45
46 if (style) {
47 var sp = node.appendChild(document.createElement("span"));
48 sp.className = "cm-" + style.replace(/ +/g, " cm-");
49 sp.appendChild(document.createTextNode(content));
50 } else {
51 node.appendChild(document.createTextNode(content));
52 }
53 };
54 }
55
56 var lines = CodeMirror.splitLines(string), state = (options && options.state) || CodeMirror.startState(mode);
57 for (var i = 0, e = lines.length; i < e; ++i) {
58 if (i) callback("\n");
59 var stream = new CodeMirror.StringStream(lines[i]);
60 while (!stream.eol()) {
61 var style = mode.token(stream, state);
62 callback(stream.current(), style, i, stream.start, state);
63 stream.start = stream.pos;
64 }
65 }
66 };
67
68 });