国内流行的内容管理系统(CMS)多端全媒体解决方案 https://www.dedebiz.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

153 lines
5.5KB

  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: http://codemirror.net/LICENSE
  3. (function(mod) {
  4. if (typeof exports == "object" && typeof module == "object") // CommonJS
  5. mod(require("../../lib/codemirror"), require("../xml/xml"), require("../javascript/javascript"), require("../css/css"));
  6. else if (typeof define == "function" && define.amd) // AMD
  7. define(["../../lib/codemirror", "../xml/xml", "../javascript/javascript", "../css/css"], mod);
  8. else // Plain browser env
  9. mod(CodeMirror);
  10. })(function(CodeMirror) {
  11. "use strict";
  12. var defaultTags = {
  13. script: [
  14. ["lang", /(javascript|babel)/i, "javascript"],
  15. ["type", /^(?:text|application)\/(?:x-)?(?:java|ecma)script$|^module$|^$/i, "javascript"],
  16. ["type", /./, "text/plain"],
  17. [null, null, "javascript"]
  18. ],
  19. style: [
  20. ["lang", /^css$/i, "css"],
  21. ["type", /^(text\/)?(x-)?(stylesheet|css)$/i, "css"],
  22. ["type", /./, "text/plain"],
  23. [null, null, "css"]
  24. ]
  25. };
  26. function maybeBackup(stream, pat, style) {
  27. var cur = stream.current(), close = cur.search(pat);
  28. if (close > -1) {
  29. stream.backUp(cur.length - close);
  30. } else if (cur.match(/<\/?$/)) {
  31. stream.backUp(cur.length);
  32. if (!stream.match(pat, false)) stream.match(cur);
  33. }
  34. return style;
  35. }
  36. var attrRegexpCache = {};
  37. function getAttrRegexp(attr) {
  38. var regexp = attrRegexpCache[attr];
  39. if (regexp) return regexp;
  40. return attrRegexpCache[attr] = new RegExp("\\s+" + attr + "\\s*=\\s*('|\")?([^'\"]+)('|\")?\\s*");
  41. }
  42. function getAttrValue(text, attr) {
  43. var match = text.match(getAttrRegexp(attr))
  44. return match ? /^\s*(.*?)\s*$/.exec(match[2])[1] : ""
  45. }
  46. function getTagRegexp(tagName, anchored) {
  47. return new RegExp((anchored ? "^" : "") + "<\/\s*" + tagName + "\s*>", "i");
  48. }
  49. function addTags(from, to) {
  50. for (var tag in from) {
  51. var dest = to[tag] || (to[tag] = []);
  52. var source = from[tag];
  53. for (var i = source.length - 1; i >= 0; i--)
  54. dest.unshift(source[i])
  55. }
  56. }
  57. function findMatchingMode(tagInfo, tagText) {
  58. for (var i = 0; i < tagInfo.length; i++) {
  59. var spec = tagInfo[i];
  60. if (!spec[0] || spec[1].test(getAttrValue(tagText, spec[0]))) return spec[2];
  61. }
  62. }
  63. CodeMirror.defineMode("htmlmixed", function (config, parserConfig) {
  64. var htmlMode = CodeMirror.getMode(config, {
  65. name: "xml",
  66. htmlMode: true,
  67. multilineTagIndentFactor: parserConfig.multilineTagIndentFactor,
  68. multilineTagIndentPastTag: parserConfig.multilineTagIndentPastTag
  69. });
  70. var tags = {};
  71. var configTags = parserConfig && parserConfig.tags, configScript = parserConfig && parserConfig.scriptTypes;
  72. addTags(defaultTags, tags);
  73. if (configTags) addTags(configTags, tags);
  74. if (configScript) for (var i = configScript.length - 1; i >= 0; i--)
  75. tags.script.unshift(["type", configScript[i].matches, configScript[i].mode])
  76. function html(stream, state) {
  77. var style = htmlMode.token(stream, state.htmlState), tag = /\btag\b/.test(style), tagName
  78. if (tag && !/[<>\s\/]/.test(stream.current()) &&
  79. (tagName = state.htmlState.tagName && state.htmlState.tagName.toLowerCase()) &&
  80. tags.hasOwnProperty(tagName)) {
  81. state.inTag = tagName + " "
  82. } else if (state.inTag && tag && />$/.test(stream.current())) {
  83. var inTag = /^([\S]+) (.*)/.exec(state.inTag)
  84. state.inTag = null
  85. var modeSpec = stream.current() == ">" && findMatchingMode(tags[inTag[1]], inTag[2])
  86. var mode = CodeMirror.getMode(config, modeSpec)
  87. var endTagA = getTagRegexp(inTag[1], true), endTag = getTagRegexp(inTag[1], false);
  88. state.token = function (stream, state) {
  89. if (stream.match(endTagA, false)) {
  90. state.token = html;
  91. state.localState = state.localMode = null;
  92. return null;
  93. }
  94. return maybeBackup(stream, endTag, state.localMode.token(stream, state.localState));
  95. };
  96. state.localMode = mode;
  97. state.localState = CodeMirror.startState(mode, htmlMode.indent(state.htmlState, ""));
  98. } else if (state.inTag) {
  99. state.inTag += stream.current()
  100. if (stream.eol()) state.inTag += " "
  101. }
  102. return style;
  103. };
  104. return {
  105. startState: function () {
  106. var state = CodeMirror.startState(htmlMode);
  107. return {token: html, inTag: null, localMode: null, localState: null, htmlState: state};
  108. },
  109. copyState: function (state) {
  110. var local;
  111. if (state.localState) {
  112. local = CodeMirror.copyState(state.localMode, state.localState);
  113. }
  114. return {token: state.token, inTag: state.inTag,
  115. localMode: state.localMode, localState: local,
  116. htmlState: CodeMirror.copyState(htmlMode, state.htmlState)};
  117. },
  118. token: function (stream, state) {
  119. return state.token(stream, state);
  120. },
  121. indent: function (state, textAfter) {
  122. if (!state.localMode || /^\s*<\//.test(textAfter))
  123. return htmlMode.indent(state.htmlState, textAfter);
  124. else if (state.localMode.indent)
  125. return state.localMode.indent(state.localState, textAfter);
  126. else
  127. return CodeMirror.Pass;
  128. },
  129. innerMode: function (state) {
  130. return {state: state.localState || state.htmlState, mode: state.localMode || htmlMode};
  131. }
  132. };
  133. }, "xml", "javascript", "css");
  134. CodeMirror.defineMIME("text/html", "htmlmixed");
  135. });