runmode-standalone.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: https://codemirror.net/LICENSE
  3. var root = typeof globalThis !== 'undefined' ? globalThis : window;
  4. root.CodeMirror = {};
  5. (function() {
  6. "use strict";
  7. function splitLines(string){ return string.split(/\r?\n|\r/); };
  8. function StringStream(strings, i) {
  9. this.pos = this.start = 0;
  10. this.string = strings[i];
  11. this.strings = strings
  12. this.i = i
  13. this.lineStart = 0;
  14. }
  15. StringStream.prototype = {
  16. eol: function() {return this.pos >= this.string.length;},
  17. sol: function() {return this.pos == 0;},
  18. peek: function() {return this.string.charAt(this.pos) || null;},
  19. next: function() {
  20. if (this.pos < this.string.length)
  21. return this.string.charAt(this.pos++);
  22. },
  23. eat: function(match) {
  24. var ch = this.string.charAt(this.pos);
  25. if (typeof match == "string") var ok = ch == match;
  26. else var ok = ch && (match.test ? match.test(ch) : match(ch));
  27. if (ok) {++this.pos; return ch;}
  28. },
  29. eatWhile: function(match) {
  30. var start = this.pos;
  31. while (this.eat(match)){}
  32. return this.pos > start;
  33. },
  34. eatSpace: function() {
  35. var start = this.pos;
  36. while (/[\s\u00a0]/.test(this.string.charAt(this.pos))) ++this.pos;
  37. return this.pos > start;
  38. },
  39. skipToEnd: function() {this.pos = this.string.length;},
  40. skipTo: function(ch) {
  41. var found = this.string.indexOf(ch, this.pos);
  42. if (found > -1) {this.pos = found; return true;}
  43. },
  44. backUp: function(n) {this.pos -= n;},
  45. column: function() {return this.start - this.lineStart;},
  46. indentation: function() {return 0;},
  47. match: function(pattern, consume, caseInsensitive) {
  48. if (typeof pattern == "string") {
  49. var cased = function(str) {return caseInsensitive ? str.toLowerCase() : str;};
  50. var substr = this.string.substr(this.pos, pattern.length);
  51. if (cased(substr) == cased(pattern)) {
  52. if (consume !== false) this.pos += pattern.length;
  53. return true;
  54. }
  55. } else {
  56. var match = this.string.slice(this.pos).match(pattern);
  57. if (match && match.index > 0) return null;
  58. if (match && consume !== false) this.pos += match[0].length;
  59. return match;
  60. }
  61. },
  62. current: function(){return this.string.slice(this.start, this.pos);},
  63. hideFirstChars: function(n, inner) {
  64. this.lineStart += n;
  65. try { return inner(); }
  66. finally { this.lineStart -= n; }
  67. },
  68. lookAhead: function(n) { return this.strings[this.i + n] }
  69. };
  70. CodeMirror.StringStream = StringStream;
  71. CodeMirror.startState = function (mode, a1, a2) {
  72. return mode.startState ? mode.startState(a1, a2) : true;
  73. };
  74. var modes = CodeMirror.modes = {}, mimeModes = CodeMirror.mimeModes = {};
  75. CodeMirror.defineMode = function (name, mode) {
  76. if (arguments.length > 2)
  77. mode.dependencies = Array.prototype.slice.call(arguments, 2);
  78. modes[name] = mode;
  79. };
  80. CodeMirror.defineMIME = function (mime, spec) { mimeModes[mime] = spec; };
  81. CodeMirror.resolveMode = function(spec) {
  82. if (typeof spec == "string" && mimeModes.hasOwnProperty(spec)) {
  83. spec = mimeModes[spec];
  84. } else if (spec && typeof spec.name == "string" && mimeModes.hasOwnProperty(spec.name)) {
  85. spec = mimeModes[spec.name];
  86. }
  87. if (typeof spec == "string") return {name: spec};
  88. else return spec || {name: "null"};
  89. };
  90. CodeMirror.getMode = function (options, spec) {
  91. spec = CodeMirror.resolveMode(spec);
  92. var mfactory = modes[spec.name];
  93. if (!mfactory) throw new Error("Unknown mode: " + spec);
  94. return mfactory(options, spec);
  95. };
  96. CodeMirror.registerHelper = CodeMirror.registerGlobalHelper = Math.min;
  97. CodeMirror.defineMode("null", function() {
  98. return {token: function(stream) {stream.skipToEnd();}};
  99. });
  100. CodeMirror.defineMIME("text/plain", "null");
  101. CodeMirror.runMode = function (string, modespec, callback, options) {
  102. var mode = CodeMirror.getMode({ indentUnit: 2 }, modespec);
  103. var ie = /MSIE \d/.test(navigator.userAgent);
  104. var ie_lt9 = ie && (document.documentMode == null || document.documentMode < 9);
  105. if (callback.appendChild) {
  106. var tabSize = (options && options.tabSize) || 4;
  107. var node = callback, col = 0;
  108. node.innerHTML = "";
  109. callback = function (text, style) {
  110. if (text == "\n") {
  111. // Emitting LF or CRLF on IE8 or earlier results in an incorrect display.
  112. // Emitting a carriage return makes everything ok.
  113. node.appendChild(document.createTextNode(ie_lt9 ? '\r' : text));
  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. if (style) {
  135. var sp = node.appendChild(document.createElement("span"));
  136. sp.className = "cm-" + style.replace(/ +/g, " cm-");
  137. sp.appendChild(document.createTextNode(content));
  138. } else {
  139. node.appendChild(document.createTextNode(content));
  140. }
  141. };
  142. }
  143. var lines = splitLines(string), state = (options && options.state) || CodeMirror.startState(mode);
  144. for (var i = 0, e = lines.length; i < e; ++i) {
  145. if (i) callback("\n");
  146. var stream = new CodeMirror.StringStream(lines, i);
  147. if (!stream.string && mode.blankLine) mode.blankLine(state);
  148. while (!stream.eol()) {
  149. var style = mode.token(stream, state);
  150. callback(stream.current(), style, i, stream.start, state);
  151. stream.start = stream.pos;
  152. }
  153. }
  154. };
  155. })();