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