Merge branch '2.0'
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / js / 3rdParty / codemirror / addon / runmode / runmode-standalone.js
CommitLineData
77b7b761
TD
1window.CodeMirror = {};
2
837afb80
TD
3(function() {
4"use strict";
5
77b7b761
TD
6function splitLines(string){ return string.split(/\r?\n|\r/); };
7
8function StringStream(string) {
9 this.pos = this.start = 0;
10 this.string = string;
837afb80 11 this.lineStart = 0;
77b7b761
TD
12}
13StringStream.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;},
837afb80 43 column: function() {return this.start - this.lineStart;},
77b7b761
TD
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;};
837afb80
TD
48 var substr = this.string.substr(this.pos, pattern.length);
49 if (cased(substr) == cased(pattern)) {
77b7b761
TD
50 if (consume !== false) this.pos += pattern.length;
51 return true;
52 }
53 } else {
54 var match = this.string.slice(this.pos).match(pattern);
837afb80 55 if (match && match.index > 0) return null;
77b7b761
TD
56 if (match && consume !== false) this.pos += match[0].length;
57 return match;
58 }
59 },
837afb80
TD
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 }
77b7b761
TD
66};
67CodeMirror.StringStream = StringStream;
68
69CodeMirror.startState = function (mode, a1, a2) {
70 return mode.startState ? mode.startState(a1, a2) : true;
71};
72
73var modes = CodeMirror.modes = {}, mimeModes = CodeMirror.mimeModes = {};
74CodeMirror.defineMode = function (name, mode) { modes[name] = mode; };
75CodeMirror.defineMIME = function (mime, spec) { mimeModes[mime] = spec; };
837afb80
TD
76CodeMirror.resolveMode = function(spec) {
77 if (typeof spec == "string" && mimeModes.hasOwnProperty(spec)) {
77b7b761 78 spec = mimeModes[spec];
837afb80
TD
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};
85CodeMirror.getMode = function (options, spec) {
86 spec = CodeMirror.resolveMode(spec);
87 var mfactory = modes[spec.name];
77b7b761 88 if (!mfactory) throw new Error("Unknown mode: " + spec);
837afb80 89 return mfactory(options, spec);
77b7b761 90};
837afb80
TD
91CodeMirror.registerHelper = CodeMirror.registerGlobalHelper = Math.min;
92CodeMirror.defineMode("null", function() {
93 return {token: function(stream) {stream.skipToEnd();}};
94});
95CodeMirror.defineMIME("text/plain", "null");
77b7b761
TD
96
97CodeMirror.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
837afb80 138 var lines = splitLines(string), state = (options && options.state) || CodeMirror.startState(mode);
77b7b761
TD
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);
837afb80 144 callback(stream.current(), style, i, stream.start, state);
77b7b761
TD
145 stream.start = stream.pos;
146 }
147 }
148};
837afb80 149})();