Path: blob/master/web-gui/buildyourownbotnet/assets/js/codemirror/mode/htmlmixed/htmlmixed.js
1294 views
CodeMirror.defineMode("htmlmixed", function(config, parserConfig) {1var htmlMode = CodeMirror.getMode(config, {name: "xml", htmlMode: true});2var cssMode = CodeMirror.getMode(config, "css");34var scriptTypes = [], scriptTypesConf = parserConfig && parserConfig.scriptTypes;5scriptTypes.push({matches: /^(?:text|application)\/(?:x-)?(?:java|ecma)script$|^$/i,6mode: CodeMirror.getMode(config, "javascript")});7if (scriptTypesConf) for (var i = 0; i < scriptTypesConf.length; ++i) {8var conf = scriptTypesConf[i];9scriptTypes.push({matches: conf.matches, mode: conf.mode && CodeMirror.getMode(config, conf.mode)});10}11scriptTypes.push({matches: /./,12mode: CodeMirror.getMode(config, "text/plain")});1314function html(stream, state) {15var tagName = state.htmlState.tagName;16var style = htmlMode.token(stream, state.htmlState);17if (tagName == "script" && /\btag\b/.test(style) && stream.current() == ">") {18// Script block: mode to change to depends on type attribute19var scriptType = stream.string.slice(Math.max(0, stream.pos - 100), stream.pos).match(/\btype\s*=\s*("[^"]+"|'[^']+'|\S+)[^<]*$/i);20scriptType = scriptType ? scriptType[1] : "";21if (scriptType && /[\"\']/.test(scriptType.charAt(0))) scriptType = scriptType.slice(1, scriptType.length - 1);22for (var i = 0; i < scriptTypes.length; ++i) {23var tp = scriptTypes[i];24if (typeof tp.matches == "string" ? scriptType == tp.matches : tp.matches.test(scriptType)) {25if (tp.mode) {26state.token = script;27state.localMode = tp.mode;28state.localState = tp.mode.startState && tp.mode.startState(htmlMode.indent(state.htmlState, ""));29}30break;31}32}33} else if (tagName == "style" && /\btag\b/.test(style) && stream.current() == ">") {34state.token = css;35state.localMode = cssMode;36state.localState = cssMode.startState(htmlMode.indent(state.htmlState, ""));37}38return style;39}40function maybeBackup(stream, pat, style) {41var cur = stream.current();42var close = cur.search(pat), m;43if (close > -1) stream.backUp(cur.length - close);44else if (m = cur.match(/<\/?$/)) {45stream.backUp(cur.length);46if (!stream.match(pat, false)) stream.match(cur);47}48return style;49}50function script(stream, state) {51if (stream.match(/^<\/\s*script\s*>/i, false)) {52state.token = html;53state.localState = state.localMode = null;54return html(stream, state);55}56return maybeBackup(stream, /<\/\s*script\s*>/,57state.localMode.token(stream, state.localState));58}59function css(stream, state) {60if (stream.match(/^<\/\s*style\s*>/i, false)) {61state.token = html;62state.localState = state.localMode = null;63return html(stream, state);64}65return maybeBackup(stream, /<\/\s*style\s*>/,66cssMode.token(stream, state.localState));67}6869return {70startState: function() {71var state = htmlMode.startState();72return {token: html, localMode: null, localState: null, htmlState: state};73},7475copyState: function(state) {76if (state.localState)77var local = CodeMirror.copyState(state.localMode, state.localState);78return {token: state.token, localMode: state.localMode, localState: local,79htmlState: CodeMirror.copyState(htmlMode, state.htmlState)};80},8182token: function(stream, state) {83return state.token(stream, state);84},8586indent: function(state, textAfter) {87if (!state.localMode || /^\s*<\//.test(textAfter))88return htmlMode.indent(state.htmlState, textAfter);89else if (state.localMode.indent)90return state.localMode.indent(state.localState, textAfter);91else92return CodeMirror.Pass;93},9495innerMode: function(state) {96return {state: state.localState || state.htmlState, mode: state.localMode || htmlMode};97}98};99}, "xml", "javascript", "css");100101CodeMirror.defineMIME("text/html", "htmlmixed");102103104