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