github.com/elliott5/community@v0.14.1-0.20160709191136-823126fb026a/app/public/codemirror/addon/runmode/runmode.js (about)

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