Merge branch '2.0'
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / js / 3rdParty / codemirror / addon / runmode / runmode-standalone.js
1 window.CodeMirror = {};
2
3 (function() {
4 "use strict";
5
6 function splitLines(string){ return string.split(/\r?\n|\r/); };
7
8 function StringStream(string) {
9 this.pos = this.start = 0;
10 this.string = string;
11 this.lineStart = 0;
12 }
13 StringStream.prototype = {
14 eol: function() {return this.pos >= this.string.length;},
15 sol: function() {return this.pos == 0;},
16 peek: function() {return this.string.charAt(this.pos) || null;},
17 next: function() {
18 if (this.pos < this.string.length)
19 return this.string.charAt(this.pos++);
20 },
21 eat: function(match) {
22 var ch = this.string.charAt(this.pos);
23 if (typeof match == "string") var ok = ch == match;
24 else var ok = ch && (match.test ? match.test(ch) : match(ch));
25 if (ok) {++this.pos; return ch;}
26 },
27 eatWhile: function(match) {
28 var start = this.pos;
29 while (this.eat(match)){}
30 return this.pos > start;
31 },
32 eatSpace: function() {
33 var start = this.pos;
34 while (/[\s\u00a0]/.test(this.string.charAt(this.pos))) ++this.pos;
35 return this.pos > start;
36 },
37 skipToEnd: function() {this.pos = this.string.length;},
38 skipTo: function(ch) {
39 var found = this.string.indexOf(ch, this.pos);
40 if (found > -1) {this.pos = found; return true;}
41 },
42 backUp: function(n) {this.pos -= n;},
43 column: function() {return this.start - this.lineStart;},
44 indentation: function() {return 0;},
45 match: function(pattern, consume, caseInsensitive) {
46 if (typeof pattern == "string") {
47 var cased = function(str) {return caseInsensitive ? str.toLowerCase() : str;};
48 var substr = this.string.substr(this.pos, pattern.length);
49 if (cased(substr) == cased(pattern)) {
50 if (consume !== false) this.pos += pattern.length;
51 return true;
52 }
53 } else {
54 var match = this.string.slice(this.pos).match(pattern);
55 if (match && match.index > 0) return null;
56 if (match && consume !== false) this.pos += match[0].length;
57 return match;
58 }
59 },
60 current: function(){return this.string.slice(this.start, this.pos);},
61 hideFirstChars: function(n, inner) {
62 this.lineStart += n;
63 try { return inner(); }
64 finally { this.lineStart -= n; }
65 }
66 };
67 CodeMirror.StringStream = StringStream;
68
69 CodeMirror.startState = function (mode, a1, a2) {
70 return mode.startState ? mode.startState(a1, a2) : true;
71 };
72
73 var modes = CodeMirror.modes = {}, mimeModes = CodeMirror.mimeModes = {};
74 CodeMirror.defineMode = function (name, mode) { modes[name] = mode; };
75 CodeMirror.defineMIME = function (mime, spec) { mimeModes[mime] = spec; };
76 CodeMirror.resolveMode = function(spec) {
77 if (typeof spec == "string" && mimeModes.hasOwnProperty(spec)) {
78 spec = mimeModes[spec];
79 } else if (spec && typeof spec.name == "string" && mimeModes.hasOwnProperty(spec.name)) {
80 spec = mimeModes[spec.name];
81 }
82 if (typeof spec == "string") return {name: spec};
83 else return spec || {name: "null"};
84 };
85 CodeMirror.getMode = function (options, spec) {
86 spec = CodeMirror.resolveMode(spec);
87 var mfactory = modes[spec.name];
88 if (!mfactory) throw new Error("Unknown mode: " + spec);
89 return mfactory(options, spec);
90 };
91 CodeMirror.registerHelper = CodeMirror.registerGlobalHelper = Math.min;
92 CodeMirror.defineMode("null", function() {
93 return {token: function(stream) {stream.skipToEnd();}};
94 });
95 CodeMirror.defineMIME("text/plain", "null");
96
97 CodeMirror.runMode = function (string, modespec, callback, options) {
98 var mode = CodeMirror.getMode({ indentUnit: 2 }, modespec);
99
100 if (callback.nodeType == 1) {
101 var tabSize = (options && options.tabSize) || 4;
102 var node = callback, col = 0;
103 node.innerHTML = "";
104 callback = function (text, style) {
105 if (text == "\n") {
106 node.appendChild(document.createElement("br"));
107 col = 0;
108 return;
109 }
110 var content = "";
111 // replace tabs
112 for (var pos = 0; ;) {
113 var idx = text.indexOf("\t", pos);
114 if (idx == -1) {
115 content += text.slice(pos);
116 col += text.length - pos;
117 break;
118 } else {
119 col += idx - pos;
120 content += text.slice(pos, idx);
121 var size = tabSize - col % tabSize;
122 col += size;
123 for (var i = 0; i < size; ++i) content += " ";
124 pos = idx + 1;
125 }
126 }
127
128 if (style) {
129 var sp = node.appendChild(document.createElement("span"));
130 sp.className = "cm-" + style.replace(/ +/g, " cm-");
131 sp.appendChild(document.createTextNode(content));
132 } else {
133 node.appendChild(document.createTextNode(content));
134 }
135 };
136 }
137
138 var lines = splitLines(string), state = (options && options.state) || CodeMirror.startState(mode);
139 for (var i = 0, e = lines.length; i < e; ++i) {
140 if (i) callback("\n");
141 var stream = new CodeMirror.StringStream(lines[i]);
142 while (!stream.eol()) {
143 var style = mode.token(stream, state);
144 callback(stream.current(), style, i, stream.start, state);
145 stream.start = stream.pos;
146 }
147 }
148 };
149 })();