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.6 KiB

7 months ago
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: https://codemirror.net/5/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. allowMissingTagName: parserConfig.allowMissingTagName,
  70. });
  71. var tags = {};
  72. var configTags = parserConfig && parserConfig.tags, configScript = parserConfig && parserConfig.scriptTypes;
  73. addTags(defaultTags, tags);
  74. if (configTags) addTags(configTags, tags);
  75. if (configScript) for (var i = configScript.length - 1; i >= 0; i--)
  76. tags.script.unshift(["type", configScript[i].matches, configScript[i].mode])
  77. function html(stream, state) {
  78. var style = htmlMode.token(stream, state.htmlState), tag = /\btag\b/.test(style), tagName
  79. if (tag && !/[<>\s\/]/.test(stream.current()) &&
  80. (tagName = state.htmlState.tagName && state.htmlState.tagName.toLowerCase()) &&
  81. tags.hasOwnProperty(tagName)) {
  82. state.inTag = tagName + " "
  83. } else if (state.inTag && tag && />$/.test(stream.current())) {
  84. var inTag = /^([\S]+) (.*)/.exec(state.inTag)
  85. state.inTag = null
  86. var modeSpec = stream.current() == ">" && findMatchingMode(tags[inTag[1]], inTag[2])
  87. var mode = CodeMirror.getMode(config, modeSpec)
  88. var endTagA = getTagRegexp(inTag[1], true), endTag = getTagRegexp(inTag[1], false);
  89. state.token = function (stream, state) {
  90. if (stream.match(endTagA, false)) {
  91. state.token = html;
  92. state.localState = state.localMode = null;
  93. return null;
  94. }
  95. return maybeBackup(stream, endTag, state.localMode.token(stream, state.localState));
  96. };
  97. state.localMode = mode;
  98. state.localState = CodeMirror.startState(mode, htmlMode.indent(state.htmlState, "", ""));
  99. } else if (state.inTag) {
  100. state.inTag += stream.current()
  101. if (stream.eol()) state.inTag += " "
  102. }
  103. return style;
  104. };
  105. return {
  106. startState: function () {
  107. var state = CodeMirror.startState(htmlMode);
  108. return {token: html, inTag: null, localMode: null, localState: null, htmlState: state};
  109. },
  110. copyState: function (state) {
  111. var local;
  112. if (state.localState) {
  113. local = CodeMirror.copyState(state.localMode, state.localState);
  114. }
  115. return {token: state.token, inTag: state.inTag,
  116. localMode: state.localMode, localState: local,
  117. htmlState: CodeMirror.copyState(htmlMode, state.htmlState)};
  118. },
  119. token: function (stream, state) {
  120. return state.token(stream, state);
  121. },
  122. indent: function (state, textAfter, line) {
  123. if (!state.localMode || /^\s*<\//.test(textAfter))
  124. return htmlMode.indent(state.htmlState, textAfter, line);
  125. else if (state.localMode.indent)
  126. return state.localMode.indent(state.localState, textAfter, line);
  127. else
  128. return CodeMirror.Pass;
  129. },
  130. innerMode: function (state) {
  131. return {state: state.localState || state.htmlState, mode: state.localMode || htmlMode};
  132. }
  133. };
  134. }, "xml", "javascript", "css");
  135. CodeMirror.defineMIME("text/html", "htmlmixed");
  136. });