You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

72 lines
2.4 KiB

3 years ago
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: http://codemirror.net/LICENSE
  3. (function(mod) {
  4. if (typeof exports == "object" && typeof module == "object") // CommonJS
  5. mod(require("../../lib/codemirror"));
  6. else if (typeof define == "function" && define.amd) // AMD
  7. define(["../../lib/codemirror"], mod);
  8. else // Plain browser env
  9. mod(CodeMirror);
  10. })(function(CodeMirror) {
  11. "use strict";
  12. CodeMirror.runMode = function(string, modespec, callback, options) {
  13. var mode = CodeMirror.getMode(CodeMirror.defaults, modespec);
  14. var ie = /MSIE \d/.test(navigator.userAgent);
  15. var ie_lt9 = ie && (document.documentMode == null || document.documentMode < 9);
  16. if (callback.nodeType == 1) {
  17. var tabSize = (options && options.tabSize) || CodeMirror.defaults.tabSize;
  18. var node = callback, col = 0;
  19. node.innerHTML = "";
  20. callback = function(text, style) {
  21. if (text == "\n") {
  22. // Emitting LF or CRLF on IE8 or earlier results in an incorrect display.
  23. // Emitting a carriage return makes everything ok.
  24. node.appendChild(document.createTextNode(ie_lt9 ? '\r' : text));
  25. col = 0;
  26. return;
  27. }
  28. var content = "";
  29. // replace tabs
  30. for (var pos = 0;;) {
  31. var idx = text.indexOf("\t", pos);
  32. if (idx == -1) {
  33. content += text.slice(pos);
  34. col += text.length - pos;
  35. break;
  36. } else {
  37. col += idx - pos;
  38. content += text.slice(pos, idx);
  39. var size = tabSize - col % tabSize;
  40. col += size;
  41. for (var i = 0; i < size; ++i) content += " ";
  42. pos = idx + 1;
  43. }
  44. }
  45. if (style) {
  46. var sp = node.appendChild(document.createElement("span"));
  47. sp.className = "cm-" + style.replace(/ +/g, " cm-");
  48. sp.appendChild(document.createTextNode(content));
  49. } else {
  50. node.appendChild(document.createTextNode(content));
  51. }
  52. };
  53. }
  54. var lines = CodeMirror.splitLines(string), state = (options && options.state) || CodeMirror.startState(mode);
  55. for (var i = 0, e = lines.length; i < e; ++i) {
  56. if (i) callback("\n");
  57. var stream = new CodeMirror.StringStream(lines[i]);
  58. if (!stream.string && mode.blankLine) mode.blankLine(state);
  59. while (!stream.eol()) {
  60. var style = mode.token(stream, state);
  61. callback(stream.current(), style, i, stream.start, state);
  62. stream.start = stream.pos;
  63. }
  64. }
  65. };
  66. });