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