0bb7acf5bee94a874690e9d25e8d58df.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. ace.define("ace/ext/static.css",["require","exports","module"], function(require, exports, module){module.exports = ".ace_static_highlight {\n font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', 'Consolas', 'source-code-pro', 'Droid Sans Mono', monospace;\n font-size: 12px;\n white-space: pre-wrap\n}\n\n.ace_static_highlight .ace_gutter {\n width: 2em;\n text-align: right;\n padding: 0 3px 0 0;\n margin-right: 3px;\n contain: none;\n}\n\n.ace_static_highlight.ace_show_gutter .ace_line {\n padding-left: 2.6em;\n}\n\n.ace_static_highlight .ace_line { position: relative; }\n\n.ace_static_highlight .ace_gutter-cell {\n -moz-user-select: -moz-none;\n -khtml-user-select: none;\n -webkit-user-select: none;\n user-select: none;\n top: 0;\n bottom: 0;\n left: 0;\n position: absolute;\n}\n\n\n.ace_static_highlight .ace_gutter-cell:before {\n content: counter(ace_line, decimal);\n counter-increment: ace_line;\n}\n.ace_static_highlight {\n counter-reset: ace_line;\n}\n";
  2. });
  3. ace.define("ace/ext/static_highlight",["require","exports","module","ace/edit_session","ace/layer/text","ace/ext/static.css","ace/config","ace/lib/dom","ace/lib/lang"], function(require, exports, module){"use strict";
  4. var EditSession = require("../edit_session").EditSession;
  5. var TextLayer = require("../layer/text").Text;
  6. var baseStyles = require("./static.css");
  7. var config = require("../config");
  8. var dom = require("../lib/dom");
  9. var escapeHTML = require("../lib/lang").escapeHTML;
  10. function Element(type) {
  11. this.type = type;
  12. this.style = {};
  13. this.textContent = "";
  14. }
  15. Element.prototype.cloneNode = function () {
  16. return this;
  17. };
  18. Element.prototype.appendChild = function (child) {
  19. this.textContent += child.toString();
  20. };
  21. Element.prototype.toString = function () {
  22. var stringBuilder = [];
  23. if (this.type != "fragment") {
  24. stringBuilder.push("<", this.type);
  25. if (this.className)
  26. stringBuilder.push(" class='", this.className, "'");
  27. var styleStr = [];
  28. for (var key in this.style) {
  29. styleStr.push(key, ":", this.style[key]);
  30. }
  31. if (styleStr.length)
  32. stringBuilder.push(" style='", styleStr.join(""), "'");
  33. stringBuilder.push(">");
  34. }
  35. if (this.textContent) {
  36. stringBuilder.push(this.textContent);
  37. }
  38. if (this.type != "fragment") {
  39. stringBuilder.push("</", this.type, ">");
  40. }
  41. return stringBuilder.join("");
  42. };
  43. var simpleDom = {
  44. createTextNode: function (textContent, element) {
  45. return escapeHTML(textContent);
  46. },
  47. createElement: function (type) {
  48. return new Element(type);
  49. },
  50. createFragment: function () {
  51. return new Element("fragment");
  52. }
  53. };
  54. var SimpleTextLayer = function () {
  55. this.config = {};
  56. this.dom = simpleDom;
  57. };
  58. SimpleTextLayer.prototype = TextLayer.prototype;
  59. var highlight = function (el, opts, callback) {
  60. var m = el.className.match(/lang-(\w+)/);
  61. var mode = opts.mode || m && ("ace/mode/" + m[1]);
  62. if (!mode)
  63. return false;
  64. var theme = opts.theme || "ace/theme/textmate";
  65. var data = "";
  66. var nodes = [];
  67. if (el.firstElementChild) {
  68. var textLen = 0;
  69. for (var i = 0; i < el.childNodes.length; i++) {
  70. var ch = el.childNodes[i];
  71. if (ch.nodeType == 3) {
  72. textLen += ch.data.length;
  73. data += ch.data;
  74. }
  75. else {
  76. nodes.push(textLen, ch);
  77. }
  78. }
  79. }
  80. else {
  81. data = el.textContent;
  82. if (opts.trim)
  83. data = data.trim();
  84. }
  85. highlight.render(data, mode, theme, opts.firstLineNumber, !opts.showGutter, function (highlighted) {
  86. dom.importCssString(highlighted.css, "ace_highlight");
  87. el.innerHTML = highlighted.html;
  88. var container = el.firstChild.firstChild;
  89. for (var i = 0; i < nodes.length; i += 2) {
  90. var pos = highlighted.session.doc.indexToPosition(nodes[i]);
  91. var node = nodes[i + 1];
  92. var lineEl = container.children[pos.row];
  93. lineEl && lineEl.appendChild(node);
  94. }
  95. callback && callback();
  96. });
  97. };
  98. highlight.render = function (input, mode, theme, lineStart, disableGutter, callback) {
  99. var waiting = 1;
  100. var modeCache = EditSession.prototype.$modes;
  101. if (typeof theme == "string") {
  102. waiting++;
  103. config.loadModule(['theme', theme], function (m) {
  104. theme = m;
  105. --waiting || done();
  106. });
  107. }
  108. var modeOptions;
  109. if (mode && typeof mode === "object" && !mode.getTokenizer) {
  110. modeOptions = mode;
  111. mode = modeOptions.path;
  112. }
  113. if (typeof mode == "string") {
  114. waiting++;
  115. config.loadModule(['mode', mode], function (m) {
  116. if (!modeCache[mode] || modeOptions)
  117. modeCache[mode] = new m.Mode(modeOptions);
  118. mode = modeCache[mode];
  119. --waiting || done();
  120. });
  121. }
  122. function done() {
  123. var result = highlight.renderSync(input, mode, theme, lineStart, disableGutter);
  124. return callback ? callback(result) : result;
  125. }
  126. return --waiting || done();
  127. };
  128. highlight.renderSync = function (input, mode, theme, lineStart, disableGutter) {
  129. lineStart = parseInt(lineStart || 1, 10);
  130. var session = new EditSession("");
  131. session.setUseWorker(false);
  132. session.setMode(mode);
  133. var textLayer = new SimpleTextLayer();
  134. textLayer.setSession(session);
  135. Object.keys(textLayer.$tabStrings).forEach(function (k) {
  136. if (typeof textLayer.$tabStrings[k] == "string") {
  137. var el = simpleDom.createFragment();
  138. el.textContent = textLayer.$tabStrings[k];
  139. textLayer.$tabStrings[k] = el;
  140. }
  141. });
  142. session.setValue(input);
  143. var length = session.getLength();
  144. var outerEl = simpleDom.createElement("div");
  145. outerEl.className = theme.cssClass;
  146. var innerEl = simpleDom.createElement("div");
  147. innerEl.className = "ace_static_highlight" + (disableGutter ? "" : " ace_show_gutter");
  148. innerEl.style["counter-reset"] = "ace_line " + (lineStart - 1);
  149. for (var ix = 0; ix < length; ix++) {
  150. var lineEl = simpleDom.createElement("div");
  151. lineEl.className = "ace_line";
  152. if (!disableGutter) {
  153. var gutterEl = simpleDom.createElement("span");
  154. gutterEl.className = "ace_gutter ace_gutter-cell";
  155. gutterEl.textContent = "";
  156. lineEl.appendChild(gutterEl);
  157. }
  158. textLayer.$renderLine(lineEl, ix, false);
  159. lineEl.textContent += "\n";
  160. innerEl.appendChild(lineEl);
  161. }
  162. outerEl.appendChild(innerEl);
  163. return {
  164. css: baseStyles + theme.cssText,
  165. html: outerEl.toString(),
  166. session: session
  167. };
  168. };
  169. module.exports = highlight;
  170. module.exports.highlight = highlight;
  171. }); (function() {
  172. ace.require(["ace/ext/static_highlight"], function(m) {
  173. if (typeof module == "object" && typeof exports == "object" && module) {
  174. module.exports = m;
  175. }
  176. });
  177. })();