Skip to content

Commit

Permalink
port htmlmixed code
Browse files Browse the repository at this point in the history
  • Loading branch information
redmunds authored and marijnh committed Jan 31, 2013
1 parent d3a97e0 commit a8b5188
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions mode/htmlmixed/htmlmixed.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
CodeMirror.defineMode("htmlmixed", function(config) {
var htmlMode = CodeMirror.getMode(config, {name: "xml", htmlMode: true});
var unknownScriptMode = CodeMirror.getMode(config, "text/plain");
var jsMode = CodeMirror.getMode(config, "javascript");
var cssMode = CodeMirror.getMode(config, "css");

function html(stream, state) {
var style = htmlMode.token(stream, state.htmlState);
if (/(?:^|\s)tag(?:\s|$)/.test(style) && stream.current() == ">" && state.htmlState.context) {
if (/^script$/i.test(state.htmlState.context.tagName)) {
state.token = javascript;
state.localState = jsMode.startState(htmlMode.indent(state.htmlState, ""));
// Script block: mode to change to depends on type attribute
var scriptType = stream.string.match(/type\s*=\s*["'](.+)["']/i);
scriptType = scriptType && scriptType[1];
if (!scriptType || scriptType.match(/(text|application)\/(java|ecma)script/i)) {
state.token = javascript;
state.localState = jsMode.startState(htmlMode.indent(state.htmlState, ""));
} else if (scriptType.match(/\/x-handlebars-template/i) || scriptType.match(/\/x-mustache/i)) {
// Handlebars or Mustache template: leave it in HTML mode
} else {
state.token = unknownScript;
state.localState = null;
}
}
else if (/^style$/i.test(state.htmlState.context.tagName)) {
state.token = css;
Expand All @@ -27,6 +38,15 @@ CodeMirror.defineMode("htmlmixed", function(config) {
}
return style;
}
function unknownScript(stream, state) {
if (stream.match(/^<\/\s*script\s*>/i, false)) {
state.token = html;
state.localState = null;
return html(stream, state);
}
return maybeBackup(stream, /<\/\s*script\s*>/,
unknownScriptMode.token(stream, state.localState));
}
function javascript(stream, state) {
if (stream.match(/^<\/\s*script\s*>/i, false)) {
state.token = html;
Expand Down Expand Up @@ -68,8 +88,10 @@ CodeMirror.defineMode("htmlmixed", function(config) {
return htmlMode.indent(state.htmlState, textAfter);
else if (state.token == javascript)
return jsMode.indent(state.localState, textAfter);
else
else if (state.token == css)
return cssMode.indent(state.localState, textAfter);
else // unknownScriptMode
return 0;
},

electricChars: "/{}:",
Expand Down

0 comments on commit a8b5188

Please sign in to comment.