~ubuntu-branches/ubuntu/utopic/codemirror-js/utopic

« back to all changes in this revision

Viewing changes to lib/util/runmode.js

  • Committer: Package Import Robot
  • Author(s): David Paleino
  • Date: 2012-04-12 12:25:28 UTC
  • Revision ID: package-import@ubuntu.com-20120412122528-8xp5a8frj4h1d3ee
Tags: upstream-2.23
ImportĀ upstreamĀ versionĀ 2.23

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
CodeMirror.runMode = function(string, modespec, callback, options) {
 
2
  var mode = CodeMirror.getMode(CodeMirror.defaults, modespec);
 
3
  var isNode = callback.nodeType == 1;
 
4
  var tabSize = (options && options.tabSize) || CodeMirror.defaults.tabSize;
 
5
  if (isNode) {
 
6
    var node = callback, accum = [], col = 0;
 
7
    callback = function(text, style) {
 
8
      if (text == "\n") {
 
9
        accum.push("<br>");
 
10
        col = 0;
 
11
        return;
 
12
      }
 
13
      var escaped = "";
 
14
      // HTML-escape and replace tabs
 
15
      for (var pos = 0;;) {
 
16
        var idx = text.indexOf("\t", pos);
 
17
        if (idx == -1) {
 
18
          escaped += CodeMirror.htmlEscape(text.slice(pos));
 
19
          col += text.length - pos;
 
20
          break;
 
21
        } else {
 
22
          col += idx - pos;
 
23
          escaped += CodeMirror.htmlEscape(text.slice(pos, idx));
 
24
          var size = tabSize - col % tabSize;
 
25
          col += size;
 
26
          for (var i = 0; i < size; ++i) escaped += " ";
 
27
          pos = idx + 1;
 
28
        }
 
29
      }
 
30
 
 
31
      if (style) 
 
32
        accum.push("<span class=\"cm-" + CodeMirror.htmlEscape(style) + "\">" + escaped + "</span>");
 
33
      else
 
34
        accum.push(escaped);
 
35
    }
 
36
  }
 
37
  var lines = CodeMirror.splitLines(string), state = CodeMirror.startState(mode);
 
38
  for (var i = 0, e = lines.length; i < e; ++i) {
 
39
    if (i) callback("\n");
 
40
    var stream = new CodeMirror.StringStream(lines[i]);
 
41
    while (!stream.eol()) {
 
42
      var style = mode.token(stream, state);
 
43
      callback(stream.current(), style, i, stream.start);
 
44
      stream.start = stream.pos;
 
45
    }
 
46
  }
 
47
  if (isNode)
 
48
    node.innerHTML = accum.join("");
 
49
};