htmlmixed.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. CodeMirror.defineMode("htmlmixed", function(config, parserConfig) {
  13. var htmlMode = CodeMirror.getMode(config, {name: "xml",
  14. htmlMode: true,
  15. multilineTagIndentFactor: parserConfig.multilineTagIndentFactor,
  16. multilineTagIndentPastTag: parserConfig.multilineTagIndentPastTag});
  17. var cssMode = CodeMirror.getMode(config, "css");
  18. var scriptTypes = [], scriptTypesConf = parserConfig && parserConfig.scriptTypes;
  19. scriptTypes.push({matches: /^(?:text|application)\/(?:x-)?(?:java|ecma)script$|^$/i,
  20. mode: CodeMirror.getMode(config, "javascript")});
  21. if (scriptTypesConf) for (var i = 0; i < scriptTypesConf.length; ++i) {
  22. var conf = scriptTypesConf[i];
  23. scriptTypes.push({matches: conf.matches, mode: conf.mode && CodeMirror.getMode(config, conf.mode)});
  24. }
  25. scriptTypes.push({matches: /./,
  26. mode: CodeMirror.getMode(config, "text/plain")});
  27. function html(stream, state) {
  28. var tagName = state.htmlState.tagName;
  29. if (tagName) tagName = tagName.toLowerCase();
  30. var style = htmlMode.token(stream, state.htmlState);
  31. if (tagName == "script" && /\btag\b/.test(style) && stream.current() == ">") {
  32. // Script block: mode to change to depends on type attribute
  33. var scriptType = stream.string.slice(Math.max(0, stream.pos - 100), stream.pos).match(/\btype\s*=\s*("[^"]+"|'[^']+'|\S+)[^<]*$/i);
  34. scriptType = scriptType ? scriptType[1] : "";
  35. if (scriptType && /[\"\']/.test(scriptType.charAt(0))) scriptType = scriptType.slice(1, scriptType.length - 1);
  36. for (var i = 0; i < scriptTypes.length; ++i) {
  37. var tp = scriptTypes[i];
  38. if (typeof tp.matches == "string" ? scriptType == tp.matches : tp.matches.test(scriptType)) {
  39. if (tp.mode) {
  40. state.token = script;
  41. state.localMode = tp.mode;
  42. state.localState = tp.mode.startState && tp.mode.startState(htmlMode.indent(state.htmlState, ""));
  43. }
  44. break;
  45. }
  46. }
  47. } else if (tagName == "style" && /\btag\b/.test(style) && stream.current() == ">") {
  48. state.token = css;
  49. state.localMode = cssMode;
  50. state.localState = cssMode.startState(htmlMode.indent(state.htmlState, ""));
  51. }
  52. return style;
  53. }
  54. function maybeBackup(stream, pat, style) {
  55. var cur = stream.current();
  56. var close = cur.search(pat), m;
  57. if (close > -1) stream.backUp(cur.length - close);
  58. else if (m = cur.match(/<\/?$/)) {
  59. stream.backUp(cur.length);
  60. if (!stream.match(pat, false)) stream.match(cur);
  61. }
  62. return style;
  63. }
  64. function script(stream, state) {
  65. if (stream.match(/^<\/\s*script\s*>/i, false)) {
  66. state.token = html;
  67. state.localState = state.localMode = null;
  68. return null;
  69. }
  70. return maybeBackup(stream, /<\/\s*script\s*>/,
  71. state.localMode.token(stream, state.localState));
  72. }
  73. function css(stream, state) {
  74. if (stream.match(/^<\/\s*style\s*>/i, false)) {
  75. state.token = html;
  76. state.localState = state.localMode = null;
  77. return null;
  78. }
  79. return maybeBackup(stream, /<\/\s*style\s*>/,
  80. cssMode.token(stream, state.localState));
  81. }
  82. return {
  83. startState: function() {
  84. var state = htmlMode.startState();
  85. return {token: html, localMode: null, localState: null, htmlState: state};
  86. },
  87. copyState: function(state) {
  88. if (state.localState)
  89. var local = CodeMirror.copyState(state.localMode, state.localState);
  90. return {token: state.token, localMode: state.localMode, localState: local,
  91. htmlState: CodeMirror.copyState(htmlMode, state.htmlState)};
  92. },
  93. token: function(stream, state) {
  94. return state.token(stream, state);
  95. },
  96. indent: function(state, textAfter) {
  97. if (!state.localMode || /^\s*<\//.test(textAfter))
  98. return htmlMode.indent(state.htmlState, textAfter);
  99. else if (state.localMode.indent)
  100. return state.localMode.indent(state.localState, textAfter);
  101. else
  102. return CodeMirror.Pass;
  103. },
  104. innerMode: function(state) {
  105. return {state: state.localState || state.htmlState, mode: state.localMode || htmlMode};
  106. }
  107. };
  108. }, "xml", "javascript", "css");
  109. CodeMirror.defineMIME("text/html", "htmlmixed");
  110. });