Skip to content

Commit a8b5188

Browse files
redmundsmarijnh
authored andcommitted
port htmlmixed code
1 parent d3a97e0 commit a8b5188

File tree

1 file changed

+25
-3
lines changed

1 file changed

+25
-3
lines changed

mode/htmlmixed/htmlmixed.js

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,25 @@
11
CodeMirror.defineMode("htmlmixed", function(config) {
22
var htmlMode = CodeMirror.getMode(config, {name: "xml", htmlMode: true});
3+
var unknownScriptMode = CodeMirror.getMode(config, "text/plain");
34
var jsMode = CodeMirror.getMode(config, "javascript");
45
var cssMode = CodeMirror.getMode(config, "css");
56

67
function html(stream, state) {
78
var style = htmlMode.token(stream, state.htmlState);
89
if (/(?:^|\s)tag(?:\s|$)/.test(style) && stream.current() == ">" && state.htmlState.context) {
910
if (/^script$/i.test(state.htmlState.context.tagName)) {
10-
state.token = javascript;
11-
state.localState = jsMode.startState(htmlMode.indent(state.htmlState, ""));
11+
// Script block: mode to change to depends on type attribute
12+
var scriptType = stream.string.match(/type\s*=\s*["'](.+)["']/i);
13+
scriptType = scriptType && scriptType[1];
14+
if (!scriptType || scriptType.match(/(text|application)\/(java|ecma)script/i)) {
15+
state.token = javascript;
16+
state.localState = jsMode.startState(htmlMode.indent(state.htmlState, ""));
17+
} else if (scriptType.match(/\/x-handlebars-template/i) || scriptType.match(/\/x-mustache/i)) {
18+
// Handlebars or Mustache template: leave it in HTML mode
19+
} else {
20+
state.token = unknownScript;
21+
state.localState = null;
22+
}
1223
}
1324
else if (/^style$/i.test(state.htmlState.context.tagName)) {
1425
state.token = css;
@@ -27,6 +38,15 @@ CodeMirror.defineMode("htmlmixed", function(config) {
2738
}
2839
return style;
2940
}
41+
function unknownScript(stream, state) {
42+
if (stream.match(/^<\/\s*script\s*>/i, false)) {
43+
state.token = html;
44+
state.localState = null;
45+
return html(stream, state);
46+
}
47+
return maybeBackup(stream, /<\/\s*script\s*>/,
48+
unknownScriptMode.token(stream, state.localState));
49+
}
3050
function javascript(stream, state) {
3151
if (stream.match(/^<\/\s*script\s*>/i, false)) {
3252
state.token = html;
@@ -68,8 +88,10 @@ CodeMirror.defineMode("htmlmixed", function(config) {
6888
return htmlMode.indent(state.htmlState, textAfter);
6989
else if (state.token == javascript)
7090
return jsMode.indent(state.localState, textAfter);
71-
else
91+
else if (state.token == css)
7292
return cssMode.indent(state.localState, textAfter);
93+
else // unknownScriptMode
94+
return 0;
7395
},
7496

7597
electricChars: "/{}:",

0 commit comments

Comments
 (0)