forked from ajaxorg/ace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatic_highlight.js
255 lines (224 loc) · 7.99 KB
/
static_highlight.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
"use strict";
/**
* @typedef {import("../../ace-internal").Ace.SyntaxMode} SyntaxMode
* @typedef {import("../../ace-internal").Ace.Theme} Theme
*/
var EditSession = require("../edit_session").EditSession;
var TextLayer = require("../layer/text").Text;
var baseStyles = require("./static-css");
var config = require("../config");
var dom = require("../lib/dom");
var escapeHTML = require("../lib/lang").escapeHTML;
class Element {
/**
* @param {string} type
*/
constructor(type) {
/** @type{string} */this.className;
this.type = type;
this.style = {};
this.textContent = "";
}
cloneNode() {
return this;
}
appendChild(child) {
this.textContent += child.toString();
}
toString() {
var stringBuilder = [];
if (this.type != "fragment") {
stringBuilder.push("<", this.type);
if (this.className)
stringBuilder.push(" class='", this.className, "'");
var styleStr = [];
for (var key in this.style) {
styleStr.push(key, ":", this.style[key]);
}
if (styleStr.length)
stringBuilder.push(" style='", styleStr.join(""), "'");
stringBuilder.push(">");
}
if (this.textContent) {
stringBuilder.push(this.textContent);
}
if (this.type != "fragment") {
stringBuilder.push("</", this.type, ">");
}
return stringBuilder.join("");
}
}
var simpleDom = {
createTextNode: function(/** @type {string} */ textContent, /** @type {any} */ element) {
return escapeHTML(textContent);
},
createElement: function(/** @type {string} */ type) {
return new Element(type);
},
createFragment: function() {
return new Element("fragment");
}
};
/**@type {any}*/
var SimpleTextLayer = function() {
this.config = {};
this.dom = simpleDom;
};
SimpleTextLayer.prototype = TextLayer.prototype;
/**
*
* @param {HTMLElement} el
* @param {import("../../ace-internal").Ace.StaticHighlightOptions} opts
* @param [callback]
* @returns {boolean}
*/
var highlight = function(el, opts, callback) {
var m = el.className.match(/lang-(\w+)/);
var mode = opts.mode || m && ("ace/mode/" + m[1]);
if (!mode)
return false;
var theme = opts.theme || "ace/theme/textmate";
var data = "";
var nodes = [];
if (el.firstElementChild) {
var textLen = 0;
for (var i = 0; i < el.childNodes.length; i++) {
/**@type {any}*/
var ch = el.childNodes[i];
if (ch.nodeType == 3) {
textLen += ch.data.length;
data += ch.data;
} else {
nodes.push(textLen, ch);
}
}
} else {
data = el.textContent;
if (opts.trim)
data = data.trim();
}
highlight.render(data, mode, theme, opts.firstLineNumber, !opts.showGutter, function (highlighted) {
dom.importCssString(highlighted.css, "ace_highlight", true);
el.innerHTML = highlighted.html;
/**
* TODO: check if child exists
* @type {any}
*/
var container = el.firstChild.firstChild;
for (var i = 0; i < nodes.length; i += 2) {
var pos = highlighted.session.doc.indexToPosition(nodes[i]);
var node = nodes[i + 1];
var lineEl = container.children[pos.row];
lineEl && lineEl.appendChild(node);
}
callback && callback();
});
};
/**
* Transforms a given input code snippet into HTML using the given mode
*
* @param {string} input Code snippet
* @param {string | SyntaxMode} mode String specifying the mode to load such as
* `ace/mode/javascript` or, a mode loaded from `/ace/mode`
* (use 'ServerSideHiglighter.getMode').
* @param {string | Theme} theme String specifying the theme to load such as
* `ace/theme/twilight` or, a theme loaded from `/ace/theme`.
* @param {number} lineStart A number indicating the first line number. Defaults
* to 1.
* @param {boolean} disableGutter Specifies whether or not to disable the gutter.
* `true` disables the gutter, `false` enables the gutter. Defaults to `false`.
* @param {function} [callback] When specifying the mode or theme as a string,
* this method has no return value and you must specify a callback function. The
* callback will receive the rendered object containing the properties `html`
* and `css`.
* @returns {object} An object containing the properties `html` and `css`.
*/
highlight.render = function(input, mode, theme, lineStart, disableGutter, callback) {
var waiting = 1;
var modeCache = EditSession.prototype.$modes;
// if either the theme or the mode were specified as objects
// then we need to lazily load them.
if (typeof theme == "string") {
waiting++;
config.loadModule(['theme', theme], function(m) {
theme = m;
--waiting || done();
});
}
// allow setting mode options e.h {path: "ace/mode/php", inline:true}
var modeOptions;
if (mode && typeof mode === "object" && !mode.getTokenizer) {
modeOptions = mode;
mode = modeOptions.path;
}
if (typeof mode == "string") {
waiting++;
config.loadModule(['mode', mode], function(m) {
if (!modeCache[/**@type{string}*/(mode)] || modeOptions)
modeCache[/**@type{string}*/(mode)] = new m.Mode(modeOptions);
mode = modeCache[/**@type{string}*/(mode)];
--waiting || done();
});
}
// loads or passes the specified mode module then calls renderer
function done() {
var result = highlight.renderSync(input, mode, /**@type{Theme}*/(theme), lineStart, disableGutter);
return callback ? callback(result) : result;
}
return --waiting || done();
};
/**
* Transforms a given input code snippet into HTML using the given mode
* @param {string} input Code snippet
* @param {SyntaxMode | string} mode Mode loaded from /ace/mode (use 'ServerSideHiglighter.getMode')
* @param {Theme} theme
* @param {any} lineStart
* @param {boolean} disableGutter
* @returns {object} An object containing: html, css
*/
highlight.renderSync = function(input, mode, theme, lineStart, disableGutter) {
lineStart = parseInt(lineStart || 1, 10);
var session = new EditSession("");
session.setUseWorker(false);
session.setMode(mode);
/**@type {TextLayer}*/
var textLayer = new SimpleTextLayer();
textLayer.setSession(session);
Object.keys(textLayer.$tabStrings).forEach(function(k) {
if (typeof textLayer.$tabStrings[k] == "string") {
var el = simpleDom.createFragment();
el.textContent = textLayer.$tabStrings[k];
textLayer.$tabStrings[k] = el;
}
});
session.setValue(input);
var length = session.getLength();
var outerEl = simpleDom.createElement("div");
outerEl.className = theme.cssClass;
var innerEl = simpleDom.createElement("div");
innerEl.className = "ace_static_highlight" + (disableGutter ? "" : " ace_show_gutter");
innerEl.style["counter-reset"] = "ace_line " + (lineStart - 1);
for (var ix = 0; ix < length; ix++) {
var lineEl = simpleDom.createElement("div");
lineEl.className = "ace_line";
if (!disableGutter) {
var gutterEl = simpleDom.createElement("span");
gutterEl.className ="ace_gutter ace_gutter-cell";
gutterEl.textContent = ""; /*(ix + lineStart) + */
lineEl.appendChild(gutterEl);
}
textLayer.$renderLine(lineEl, ix, false);
lineEl.textContent += "\n";
innerEl.appendChild(lineEl);
}
//console.log(JSON.stringify(outerEl, null, 2));
//console.log(outerEl.toString());
outerEl.appendChild(innerEl);
return {
css: baseStyles + theme.cssText,
html: outerEl.toString(),
session: session
};
};
module.exports = highlight;
module.exports.highlight = highlight;