runmode-standalone.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. ;(function () {
  2. 'use strict'
  3. function copyObj(obj, target, overwrite) {
  4. if (!target) {
  5. target = {}
  6. }
  7. for (var prop in obj) {
  8. if (obj.hasOwnProperty(prop) && (overwrite !== false || !target.hasOwnProperty(prop))) {
  9. target[prop] = obj[prop]
  10. }
  11. }
  12. return target
  13. }
  14. // Counts the column offset in a string, taking tabs into account.
  15. // Used mostly to find indentation.
  16. function countColumn(string, end, tabSize, startIndex, startValue) {
  17. if (end == null) {
  18. end = string.search(/[^\s\u00a0]/)
  19. if (end == -1) {
  20. end = string.length
  21. }
  22. }
  23. for (var i = startIndex || 0, n = startValue || 0; ; ) {
  24. var nextTab = string.indexOf('\t', i)
  25. if (nextTab < 0 || nextTab >= end) {
  26. return n + (end - i)
  27. }
  28. n += nextTab - i
  29. n += tabSize - (n % tabSize)
  30. i = nextTab + 1
  31. }
  32. }
  33. function nothing() {}
  34. function createObj(base, props) {
  35. var inst
  36. if (Object.create) {
  37. inst = Object.create(base)
  38. } else {
  39. nothing.prototype = base
  40. inst = new nothing()
  41. }
  42. if (props) {
  43. copyObj(props, inst)
  44. }
  45. return inst
  46. }
  47. // STRING STREAM
  48. // Fed to the mode parsers, provides helper functions to make
  49. // parsers more succinct.
  50. var StringStream = function (string, tabSize, lineOracle) {
  51. this.pos = this.start = 0
  52. this.string = string
  53. this.tabSize = tabSize || 8
  54. this.lastColumnPos = this.lastColumnValue = 0
  55. this.lineStart = 0
  56. this.lineOracle = lineOracle
  57. }
  58. StringStream.prototype.eol = function () {
  59. return this.pos >= this.string.length
  60. }
  61. StringStream.prototype.sol = function () {
  62. return this.pos == this.lineStart
  63. }
  64. StringStream.prototype.peek = function () {
  65. return this.string.charAt(this.pos) || undefined
  66. }
  67. StringStream.prototype.next = function () {
  68. if (this.pos < this.string.length) {
  69. return this.string.charAt(this.pos++)
  70. }
  71. }
  72. StringStream.prototype.eat = function (match) {
  73. var ch = this.string.charAt(this.pos)
  74. var ok
  75. if (typeof match == 'string') {
  76. ok = ch == match
  77. } else {
  78. ok = ch && (match.test ? match.test(ch) : match(ch))
  79. }
  80. if (ok) {
  81. ++this.pos
  82. return ch
  83. }
  84. }
  85. StringStream.prototype.eatWhile = function (match) {
  86. var start = this.pos
  87. while (this.eat(match)) {}
  88. return this.pos > start
  89. }
  90. StringStream.prototype.eatSpace = function () {
  91. var start = this.pos
  92. while (/[\s\u00a0]/.test(this.string.charAt(this.pos))) {
  93. ++this.pos
  94. }
  95. return this.pos > start
  96. }
  97. StringStream.prototype.skipToEnd = function () {
  98. this.pos = this.string.length
  99. }
  100. StringStream.prototype.skipTo = function (ch) {
  101. var found = this.string.indexOf(ch, this.pos)
  102. if (found > -1) {
  103. this.pos = found
  104. return true
  105. }
  106. }
  107. StringStream.prototype.backUp = function (n) {
  108. this.pos -= n
  109. }
  110. StringStream.prototype.column = function () {
  111. if (this.lastColumnPos < this.start) {
  112. this.lastColumnValue = countColumn(this.string, this.start, this.tabSize, this.lastColumnPos, this.lastColumnValue)
  113. this.lastColumnPos = this.start
  114. }
  115. return this.lastColumnValue - (this.lineStart ? countColumn(this.string, this.lineStart, this.tabSize) : 0)
  116. }
  117. StringStream.prototype.indentation = function () {
  118. return countColumn(this.string, null, this.tabSize) - (this.lineStart ? countColumn(this.string, this.lineStart, this.tabSize) : 0)
  119. }
  120. StringStream.prototype.match = function (pattern, consume, caseInsensitive) {
  121. if (typeof pattern == 'string') {
  122. var cased = function (str) {
  123. return caseInsensitive ? str.toLowerCase() : str
  124. }
  125. var substr = this.string.substr(this.pos, pattern.length)
  126. if (cased(substr) == cased(pattern)) {
  127. if (consume !== false) {
  128. this.pos += pattern.length
  129. }
  130. return true
  131. }
  132. } else {
  133. var match = this.string.slice(this.pos).match(pattern)
  134. if (match && match.index > 0) {
  135. return null
  136. }
  137. if (match && consume !== false) {
  138. this.pos += match[0].length
  139. }
  140. return match
  141. }
  142. }
  143. StringStream.prototype.current = function () {
  144. return this.string.slice(this.start, this.pos)
  145. }
  146. StringStream.prototype.hideFirstChars = function (n, inner) {
  147. this.lineStart += n
  148. try {
  149. return inner()
  150. } finally {
  151. this.lineStart -= n
  152. }
  153. }
  154. StringStream.prototype.lookAhead = function (n) {
  155. var oracle = this.lineOracle
  156. return oracle && oracle.lookAhead(n)
  157. }
  158. StringStream.prototype.baseToken = function () {
  159. var oracle = this.lineOracle
  160. return oracle && oracle.baseToken(this.pos)
  161. }
  162. // Known modes, by name and by MIME
  163. var modes = {},
  164. mimeModes = {}
  165. // Extra arguments are stored as the mode's dependencies, which is
  166. // used by (legacy) mechanisms like loadmode.js to automatically
  167. // load a mode. (Preferred mechanism is the require/define calls.)
  168. function defineMode(name, mode) {
  169. if (arguments.length > 2) {
  170. mode.dependencies = Array.prototype.slice.call(arguments, 2)
  171. }
  172. modes[name] = mode
  173. }
  174. function defineMIME(mime, spec) {
  175. mimeModes[mime] = spec
  176. }
  177. // Given a MIME type, a {name, ...options} config object, or a name
  178. // string, return a mode config object.
  179. function resolveMode(spec) {
  180. if (typeof spec == 'string' && mimeModes.hasOwnProperty(spec)) {
  181. spec = mimeModes[spec]
  182. } else if (spec && typeof spec.name == 'string' && mimeModes.hasOwnProperty(spec.name)) {
  183. var found = mimeModes[spec.name]
  184. if (typeof found == 'string') {
  185. found = { name: found }
  186. }
  187. spec = createObj(found, spec)
  188. spec.name = found.name
  189. } else if (typeof spec == 'string' && /^[\w\-]+\/[\w\-]+\+xml$/.test(spec)) {
  190. return resolveMode('application/xml')
  191. } else if (typeof spec == 'string' && /^[\w\-]+\/[\w\-]+\+json$/.test(spec)) {
  192. return resolveMode('application/json')
  193. }
  194. if (typeof spec == 'string') {
  195. return { name: spec }
  196. } else {
  197. return spec || { name: 'null' }
  198. }
  199. }
  200. // Given a mode spec (anything that resolveMode accepts), find and
  201. // initialize an actual mode object.
  202. function getMode(options, spec) {
  203. spec = resolveMode(spec)
  204. var mfactory = modes[spec.name]
  205. if (!mfactory) {
  206. return getMode(options, 'text/plain')
  207. }
  208. var modeObj = mfactory(options, spec)
  209. if (modeExtensions.hasOwnProperty(spec.name)) {
  210. var exts = modeExtensions[spec.name]
  211. for (var prop in exts) {
  212. if (!exts.hasOwnProperty(prop)) {
  213. continue
  214. }
  215. if (modeObj.hasOwnProperty(prop)) {
  216. modeObj['_' + prop] = modeObj[prop]
  217. }
  218. modeObj[prop] = exts[prop]
  219. }
  220. }
  221. modeObj.name = spec.name
  222. if (spec.helperType) {
  223. modeObj.helperType = spec.helperType
  224. }
  225. if (spec.modeProps) {
  226. for (var prop$1 in spec.modeProps) {
  227. modeObj[prop$1] = spec.modeProps[prop$1]
  228. }
  229. }
  230. return modeObj
  231. }
  232. // This can be used to attach properties to mode objects from
  233. // outside the actual mode definition.
  234. var modeExtensions = {}
  235. function extendMode(mode, properties) {
  236. var exts = modeExtensions.hasOwnProperty(mode) ? modeExtensions[mode] : (modeExtensions[mode] = {})
  237. copyObj(properties, exts)
  238. }
  239. function copyState(mode, state) {
  240. if (state === true) {
  241. return state
  242. }
  243. if (mode.copyState) {
  244. return mode.copyState(state)
  245. }
  246. var nstate = {}
  247. for (var n in state) {
  248. var val = state[n]
  249. if (val instanceof Array) {
  250. val = val.concat([])
  251. }
  252. nstate[n] = val
  253. }
  254. return nstate
  255. }
  256. // Given a mode and a state (for that mode), find the inner mode and
  257. // state at the position that the state refers to.
  258. function innerMode(mode, state) {
  259. var info
  260. while (mode.innerMode) {
  261. info = mode.innerMode(state)
  262. if (!info || info.mode == mode) {
  263. break
  264. }
  265. state = info.state
  266. mode = info.mode
  267. }
  268. return info || { mode: mode, state: state }
  269. }
  270. function startState(mode, a1, a2) {
  271. return mode.startState ? mode.startState(a1, a2) : true
  272. }
  273. var modeMethods = {
  274. __proto__: null,
  275. modes: modes,
  276. mimeModes: mimeModes,
  277. defineMode: defineMode,
  278. defineMIME: defineMIME,
  279. resolveMode: resolveMode,
  280. getMode: getMode,
  281. modeExtensions: modeExtensions,
  282. extendMode: extendMode,
  283. copyState: copyState,
  284. innerMode: innerMode,
  285. startState: startState,
  286. }
  287. // declare global: globalThis, CodeMirror
  288. // Create a minimal CodeMirror needed to use runMode, and assign to root.
  289. var root = typeof globalThis !== 'undefined' ? globalThis : window
  290. root.CodeMirror = {}
  291. // Copy StringStream and mode methods into CodeMirror object.
  292. CodeMirror.StringStream = StringStream
  293. for (var exported in modeMethods) {
  294. CodeMirror[exported] = modeMethods[exported]
  295. }
  296. // Minimal default mode.
  297. CodeMirror.defineMode('null', function () {
  298. return {
  299. token: function (stream) {
  300. return stream.skipToEnd()
  301. },
  302. }
  303. })
  304. CodeMirror.defineMIME('text/plain', 'null')
  305. CodeMirror.registerHelper = CodeMirror.registerGlobalHelper = Math.min
  306. CodeMirror.splitLines = function (string) {
  307. return string.split(/\r?\n|\r/)
  308. }
  309. CodeMirror.countColumn = countColumn
  310. CodeMirror.defaults = { indentUnit: 2 }
  311. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  312. // Distributed under an MIT license: https://codemirror.net/LICENSE
  313. ;(function (mod) {
  314. if (typeof exports == 'object' && typeof module == 'object') {
  315. // CommonJS
  316. mod(require('../../lib/codemirror'))
  317. } else if (typeof define == 'function' && define.amd) {
  318. // AMD
  319. define(['../../lib/codemirror'], mod)
  320. } // Plain browser env
  321. else {
  322. mod(CodeMirror)
  323. }
  324. })(function (CodeMirror) {
  325. CodeMirror.runMode = function (string, modespec, callback, options) {
  326. var mode = CodeMirror.getMode(CodeMirror.defaults, modespec)
  327. var tabSize = (options && options.tabSize) || CodeMirror.defaults.tabSize
  328. // Create a tokenizing callback function if passed-in callback is a DOM element.
  329. if (callback.appendChild) {
  330. var ie = /MSIE \d/.test(navigator.userAgent)
  331. var ie_lt9 = ie && (document.documentMode == null || document.documentMode < 9)
  332. var node = callback,
  333. col = 0
  334. node.innerHTML = ''
  335. callback = function (text, style) {
  336. if (text == '\n') {
  337. // Emitting LF or CRLF on IE8 or earlier results in an incorrect display.
  338. // Emitting a carriage return makes everything ok.
  339. node.appendChild(document.createTextNode(ie_lt9 ? '\r' : text))
  340. col = 0
  341. return
  342. }
  343. var content = ''
  344. // replace tabs
  345. for (var pos = 0; ; ) {
  346. var idx = text.indexOf('\t', pos)
  347. if (idx == -1) {
  348. content += text.slice(pos)
  349. col += text.length - pos
  350. break
  351. } else {
  352. col += idx - pos
  353. content += text.slice(pos, idx)
  354. var size = tabSize - (col % tabSize)
  355. col += size
  356. for (var i = 0; i < size; ++i) {
  357. content += ' '
  358. }
  359. pos = idx + 1
  360. }
  361. }
  362. // Create a node with token style and append it to the callback DOM element.
  363. if (style) {
  364. var sp = node.appendChild(document.createElement('span'))
  365. sp.className = 'cm-' + style.replace(/ +/g, ' cm-')
  366. sp.appendChild(document.createTextNode(content))
  367. } else {
  368. node.appendChild(document.createTextNode(content))
  369. }
  370. }
  371. }
  372. var lines = CodeMirror.splitLines(string),
  373. state = (options && options.state) || CodeMirror.startState(mode)
  374. for (var i = 0, e = lines.length; i < e; ++i) {
  375. if (i) {
  376. callback('\n')
  377. }
  378. var stream = new CodeMirror.StringStream(lines[i], null, {
  379. lookAhead: function (n) {
  380. return lines[i + n]
  381. },
  382. baseToken: function () {},
  383. })
  384. if (!stream.string && mode.blankLine) {
  385. mode.blankLine(state)
  386. }
  387. while (!stream.eol()) {
  388. var style = mode.token(stream, state)
  389. callback(stream.current(), style, i, stream.start, state, mode)
  390. stream.start = stream.pos
  391. }
  392. }
  393. }
  394. })
  395. })()