Commit | Line | Data |
---|---|---|
44f4c339 | 1 | // CodeMirror, copyright (c) by Marijn Haverbeke and others |
373d1232 | 2 | // Distributed under an MIT license: https://codemirror.net/LICENSE |
44f4c339 | 3 | |
837afb80 TD |
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 | ||
77b7b761 TD |
14 | CodeMirror.runMode = function(string, modespec, callback, options) { |
15 | var mode = CodeMirror.getMode(CodeMirror.defaults, modespec); | |
704cb1bf | 16 | var tabSize = (options && options.tabSize) || CodeMirror.defaults.tabSize; |
77b7b761 | 17 | |
704cb1bf | 18 | // Create a tokenizing callback function if passed-in callback is a DOM element. |
44f4c339 | 19 | if (callback.appendChild) { |
704cb1bf MS |
20 | var ie = /MSIE \d/.test(navigator.userAgent); |
21 | var ie_lt9 = ie && (document.documentMode == null || document.documentMode < 9); | |
77b7b761 TD |
22 | var node = callback, col = 0; |
23 | node.innerHTML = ""; | |
24 | callback = function(text, style) { | |
25 | if (text == "\n") { | |
26 | // Emitting LF or CRLF on IE8 or earlier results in an incorrect display. | |
27 | // Emitting a carriage return makes everything ok. | |
28 | node.appendChild(document.createTextNode(ie_lt9 ? '\r' : text)); | |
29 | col = 0; | |
30 | return; | |
31 | } | |
32 | var content = ""; | |
33 | // replace tabs | |
34 | for (var pos = 0;;) { | |
35 | var idx = text.indexOf("\t", pos); | |
36 | if (idx == -1) { | |
37 | content += text.slice(pos); | |
38 | col += text.length - pos; | |
39 | break; | |
40 | } else { | |
41 | col += idx - pos; | |
42 | content += text.slice(pos, idx); | |
43 | var size = tabSize - col % tabSize; | |
44 | col += size; | |
45 | for (var i = 0; i < size; ++i) content += " "; | |
46 | pos = idx + 1; | |
47 | } | |
48 | } | |
704cb1bf | 49 | // Create a node with token style and append it to the callback DOM element. |
77b7b761 TD |
50 | if (style) { |
51 | var sp = node.appendChild(document.createElement("span")); | |
52 | sp.className = "cm-" + style.replace(/ +/g, " cm-"); | |
53 | sp.appendChild(document.createTextNode(content)); | |
54 | } else { | |
55 | node.appendChild(document.createTextNode(content)); | |
56 | } | |
57 | }; | |
58 | } | |
59 | ||
837afb80 | 60 | var lines = CodeMirror.splitLines(string), state = (options && options.state) || CodeMirror.startState(mode); |
77b7b761 TD |
61 | for (var i = 0, e = lines.length; i < e; ++i) { |
62 | if (i) callback("\n"); | |
704cb1bf MS |
63 | var stream = new CodeMirror.StringStream(lines[i], null, { |
64 | lookAhead: function(n) { return lines[i + n] }, | |
65 | baseToken: function() {} | |
66 | }); | |
44f4c339 | 67 | if (!stream.string && mode.blankLine) mode.blankLine(state); |
77b7b761 TD |
68 | while (!stream.eol()) { |
69 | var style = mode.token(stream, state); | |
704cb1bf | 70 | callback(stream.current(), style, i, stream.start, state, mode); |
77b7b761 TD |
71 | stream.start = stream.pos; |
72 | } | |
73 | } | |
74 | }; | |
837afb80 TD |
75 | |
76 | }); |