5
if (typeof exports == "object" && typeof module == "object")
6
mod(require("../../lib/codemirror"), require("../xml/xml"), require("../javascript/javascript"), require("../css/css"));
7
else if (typeof define == "function" && define.amd)
8
define(["../../lib/codemirror", "../xml/xml", "../javascript/javascript", "../css/css"], mod);
11
})(function(CodeMirror) {
16
["lang", /(javascript|babel)/i, "javascript"],
17
["type", /^(?:text|application)\/(?:x-)?(?:java|ecma)script$|^module$|^$/i, "javascript"],
18
["type", /./, "text/plain"],
19
[null, null, "javascript"]
22
["lang", /^css$/i, "css"],
23
["type", /^(text\/)?(x-)?(stylesheet|css)$/i, "css"],
24
["type", /./, "text/plain"],
29
function maybeBackup(stream, pat, style) {
30
var cur = stream.current(), close = cur.search(pat);
32
stream.backUp(cur.length - close);
33
} else if (cur.match(/<\/?$/)) {
34
stream.backUp(cur.length);
35
if (!stream.match(pat, false)) stream.match(cur);
40
var attrRegexpCache = {};
41
function getAttrRegexp(attr) {
42
var regexp = attrRegexpCache[attr];
43
if (regexp) return regexp;
44
return attrRegexpCache[attr] = new RegExp("\\s+" + attr + "\\s*=\\s*('|\")?([^'\"]+)('|\")?\\s*");
47
function getAttrValue(text, attr) {
48
var match = text.match(getAttrRegexp(attr))
49
return match ? /^\s*(.*?)\s*$/.exec(match[2])[1] : ""
52
function getTagRegexp(tagName, anchored) {
53
return new RegExp((anchored ? "^" : "") + "<\/\s*" + tagName + "\s*>", "i");
56
function addTags(from, to) {
57
for (var tag in from) {
58
var dest = to[tag] || (to[tag] = []);
59
var source = from[tag];
60
for (var i = source.length - 1; i >= 0; i--)
61
dest.unshift(source[i])
65
function findMatchingMode(tagInfo, tagText) {
66
for (var i = 0; i < tagInfo.length; i++) {
67
var spec = tagInfo[i];
68
if (!spec[0] || spec[1].test(getAttrValue(tagText, spec[0]))) return spec[2];
72
CodeMirror.defineMode("htmlmixed", function (config, parserConfig) {
73
var htmlMode = CodeMirror.getMode(config, {
76
multilineTagIndentFactor: parserConfig.multilineTagIndentFactor,
77
multilineTagIndentPastTag: parserConfig.multilineTagIndentPastTag,
78
allowMissingTagName: parserConfig.allowMissingTagName,
82
var configTags = parserConfig && parserConfig.tags, configScript = parserConfig && parserConfig.scriptTypes;
83
addTags(defaultTags, tags);
84
if (configTags) addTags(configTags, tags);
85
if (configScript) for (var i = configScript.length - 1; i >= 0; i--)
86
tags.script.unshift(["type", configScript[i].matches, configScript[i].mode])
88
function html(stream, state) {
89
var style = htmlMode.token(stream, state.htmlState), tag = /\btag\b/.test(style), tagName
90
if (tag && !/[<>\s\/]/.test(stream.current()) &&
91
(tagName = state.htmlState.tagName && state.htmlState.tagName.toLowerCase()) &&
92
tags.hasOwnProperty(tagName)) {
93
state.inTag = tagName + " "
94
} else if (state.inTag && tag && />$/.test(stream.current())) {
95
var inTag = /^([\S]+) (.*)/.exec(state.inTag)
97
var modeSpec = stream.current() == ">" && findMatchingMode(tags[inTag[1]], inTag[2])
98
var mode = CodeMirror.getMode(config, modeSpec)
99
var endTagA = getTagRegexp(inTag[1], true), endTag = getTagRegexp(inTag[1], false);
100
state.token = function (stream, state) {
101
if (stream.match(endTagA, false)) {
103
state.localState = state.localMode = null;
106
return maybeBackup(stream, endTag, state.localMode.token(stream, state.localState));
108
state.localMode = mode;
109
state.localState = CodeMirror.startState(mode, htmlMode.indent(state.htmlState, "", ""));
110
} else if (state.inTag) {
111
state.inTag += stream.current()
112
if (stream.eol()) state.inTag += " "
118
startState: function () {
119
var state = CodeMirror.startState(htmlMode);
120
return {token: html, inTag: null, localMode: null, localState: null, htmlState: state};
123
copyState: function (state) {
125
if (state.localState) {
126
local = CodeMirror.copyState(state.localMode, state.localState);
128
return {token: state.token, inTag: state.inTag,
129
localMode: state.localMode, localState: local,
130
htmlState: CodeMirror.copyState(htmlMode, state.htmlState)};
133
token: function (stream, state) {
134
return state.token(stream, state);
137
indent: function (state, textAfter, line) {
138
if (!state.localMode || /^\s*<\//.test(textAfter))
139
return htmlMode.indent(state.htmlState, textAfter, line);
140
else if (state.localMode.indent)
141
return state.localMode.indent(state.localState, textAfter, line);
143
return CodeMirror.Pass;
146
innerMode: function (state) {
147
return {state: state.localState || state.htmlState, mode: state.localMode || htmlMode};
150
}, "xml", "javascript", "css");
152
CodeMirror.defineMIME("text/html", "htmlmixed");