OLD | NEW |
1 // CodeMirror, copyright (c) by Marijn Haverbeke and others | 1 // CodeMirror, copyright (c) by Marijn Haverbeke and others |
2 // Distributed under an MIT license: http://codemirror.net/LICENSE | 2 // Distributed under an MIT license: http://codemirror.net/LICENSE |
3 | 3 |
4 (function(mod) { | 4 (function(mod) { |
5 if (typeof exports == "object" && typeof module == "object") // CommonJS | 5 if (typeof exports == "object" && typeof module == "object") // CommonJS |
6 mod(require("../../lib/codemirror"), require("../xml/xml"), require("../java
script/javascript"), require("../css/css")); | 6 mod(require("../../lib/codemirror"), require("../xml/xml"), require("../java
script/javascript"), require("../css/css")); |
7 else if (typeof define == "function" && define.amd) // AMD | 7 else if (typeof define == "function" && define.amd) // AMD |
8 define(["../../lib/codemirror", "../xml/xml", "../javascript/javascript", ".
./css/css"], mod); | 8 define(["../../lib/codemirror", "../xml/xml", "../javascript/javascript", ".
./css/css"], mod); |
9 else // Plain browser env | 9 else // Plain browser env |
10 mod(CodeMirror); | 10 mod(CodeMirror); |
11 })(function(CodeMirror) { | 11 })(function(CodeMirror) { |
12 "use strict"; | 12 "use strict"; |
13 | 13 |
14 CodeMirror.defineMode("htmlmixed", function(config, parserConfig) { | 14 var defaultTags = { |
15 var htmlMode = CodeMirror.getMode(config, {name: "xml", | 15 script: [ |
16 htmlMode: true, | 16 ["lang", /(javascript|babel)/i, "javascript"], |
17 multilineTagIndentFactor: parserCon
fig.multilineTagIndentFactor, | 17 ["type", /^(?:text|application)\/(?:x-)?(?:java|ecma)script$|^$/i, "javasc
ript"], |
18 multilineTagIndentPastTag: parserCo
nfig.multilineTagIndentPastTag}); | 18 ["type", /./, "text/plain"], |
19 var cssMode = CodeMirror.getMode(config, "css"); | 19 [null, null, "javascript"] |
| 20 ], |
| 21 style: [ |
| 22 ["lang", /^css$/i, "css"], |
| 23 ["type", /^(text\/)?(x-)?(stylesheet|css)$/i, "css"], |
| 24 ["type", /./, "text/plain"], |
| 25 [null, null, "css"] |
| 26 ] |
| 27 }; |
20 | 28 |
21 var scriptTypes = [], scriptTypesConf = parserConfig && parserConfig.scriptTyp
es; | |
22 scriptTypes.push({matches: /^(?:text|application)\/(?:x-)?(?:java|ecma)script$
|^$/i, | |
23 mode: CodeMirror.getMode(config, "javascript")}); | |
24 if (scriptTypesConf) for (var i = 0; i < scriptTypesConf.length; ++i) { | |
25 var conf = scriptTypesConf[i]; | |
26 scriptTypes.push({matches: conf.matches, mode: conf.mode && CodeMirror.getMo
de(config, conf.mode)}); | |
27 } | |
28 scriptTypes.push({matches: /./, | |
29 mode: CodeMirror.getMode(config, "text/plain")}); | |
30 | |
31 function html(stream, state) { | |
32 var tagName = state.htmlState.tagName; | |
33 if (tagName) tagName = tagName.toLowerCase(); | |
34 var style = htmlMode.token(stream, state.htmlState); | |
35 if (tagName == "script" && /\btag\b/.test(style) && stream.current() == ">")
{ | |
36 // Script block: mode to change to depends on type attribute | |
37 var scriptType = stream.string.slice(Math.max(0, stream.pos - 100), stream
.pos).match(/\btype\s*=\s*("[^"]+"|'[^']+'|\S+)[^<]*$/i); | |
38 scriptType = scriptType ? scriptType[1] : ""; | |
39 if (scriptType && /[\"\']/.test(scriptType.charAt(0))) scriptType = script
Type.slice(1, scriptType.length - 1); | |
40 for (var i = 0; i < scriptTypes.length; ++i) { | |
41 var tp = scriptTypes[i]; | |
42 if (typeof tp.matches == "string" ? scriptType == tp.matches : tp.matche
s.test(scriptType)) { | |
43 if (tp.mode) { | |
44 state.token = script; | |
45 state.localMode = tp.mode; | |
46 state.localState = tp.mode.startState && tp.mode.startState(htmlMode
.indent(state.htmlState, "")); | |
47 } | |
48 break; | |
49 } | |
50 } | |
51 } else if (tagName == "style" && /\btag\b/.test(style) && stream.current() =
= ">") { | |
52 state.token = css; | |
53 state.localMode = cssMode; | |
54 state.localState = cssMode.startState(htmlMode.indent(state.htmlState, "")
); | |
55 } | |
56 return style; | |
57 } | |
58 function maybeBackup(stream, pat, style) { | 29 function maybeBackup(stream, pat, style) { |
59 var cur = stream.current(); | 30 var cur = stream.current(), close = cur.search(pat); |
60 var close = cur.search(pat), m; | 31 if (close > -1) { |
61 if (close > -1) stream.backUp(cur.length - close); | 32 stream.backUp(cur.length - close); |
62 else if (m = cur.match(/<\/?$/)) { | 33 } else if (cur.match(/<\/?$/)) { |
63 stream.backUp(cur.length); | 34 stream.backUp(cur.length); |
64 if (!stream.match(pat, false)) stream.match(cur); | 35 if (!stream.match(pat, false)) stream.match(cur); |
65 } | 36 } |
66 return style; | 37 return style; |
67 } | 38 } |
68 function script(stream, state) { | 39 |
69 if (stream.match(/^<\/\s*script\s*>/i, false)) { | 40 var attrRegexpCache = {}; |
70 state.token = html; | 41 function getAttrRegexp(attr) { |
71 state.localState = state.localMode = null; | 42 var regexp = attrRegexpCache[attr]; |
72 return html(stream, state); | 43 if (regexp) return regexp; |
73 } | 44 return attrRegexpCache[attr] = new RegExp("\\s+" + attr + "\\s*=\\s*('|\")?(
[^'\"]+)('|\")?\\s*"); |
74 return maybeBackup(stream, /<\/\s*script\s*>/, | |
75 state.localMode.token(stream, state.localState)); | |
76 } | |
77 function css(stream, state) { | |
78 if (stream.match(/^<\/\s*style\s*>/i, false)) { | |
79 state.token = html; | |
80 state.localState = state.localMode = null; | |
81 return html(stream, state); | |
82 } | |
83 return maybeBackup(stream, /<\/\s*style\s*>/, | |
84 cssMode.token(stream, state.localState)); | |
85 } | 45 } |
86 | 46 |
87 return { | 47 function getAttrValue(text, attr) { |
88 startState: function() { | 48 var match = text.match(getAttrRegexp(attr)) |
89 var state = htmlMode.startState(); | 49 return match ? match[2] : "" |
90 return {token: html, localMode: null, localState: null, htmlState: state}; | 50 } |
91 }, | |
92 | 51 |
93 copyState: function(state) { | 52 function getTagRegexp(tagName, anchored) { |
94 if (state.localState) | 53 return new RegExp((anchored ? "^" : "") + "<\/\s*" + tagName + "\s*>", "i"); |
95 var local = CodeMirror.copyState(state.localMode, state.localState); | 54 } |
96 return {token: state.token, localMode: state.localMode, localState: local, | |
97 htmlState: CodeMirror.copyState(htmlMode, state.htmlState)}; | |
98 }, | |
99 | 55 |
100 token: function(stream, state) { | 56 function addTags(from, to) { |
101 return state.token(stream, state); | 57 for (var tag in from) { |
102 }, | 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]) |
| 62 } |
| 63 } |
103 | 64 |
104 indent: function(state, textAfter) { | 65 function findMatchingMode(tagInfo, tagText) { |
105 if (!state.localMode || /^\s*<\//.test(textAfter)) | 66 for (var i = 0; i < tagInfo.length; i++) { |
106 return htmlMode.indent(state.htmlState, textAfter); | 67 var spec = tagInfo[i]; |
107 else if (state.localMode.indent) | 68 if (!spec[0] || spec[1].test(getAttrValue(tagText, spec[0]))) return spec[
2]; |
108 return state.localMode.indent(state.localState, textAfter); | 69 } |
109 else | 70 } |
110 return CodeMirror.Pass; | |
111 }, | |
112 | 71 |
113 innerMode: function(state) { | 72 CodeMirror.defineMode("htmlmixed", function (config, parserConfig) { |
114 return {state: state.localState || state.htmlState, mode: state.localMode
|| htmlMode}; | 73 var htmlMode = CodeMirror.getMode(config, { |
115 } | 74 name: "xml", |
116 }; | 75 htmlMode: true, |
117 }, "xml", "javascript", "css"); | 76 multilineTagIndentFactor: parserConfig.multilineTagIndentFactor, |
| 77 multilineTagIndentPastTag: parserConfig.multilineTagIndentPastTag |
| 78 }); |
118 | 79 |
119 CodeMirror.defineMIME("text/html", "htmlmixed"); | 80 var tags = {}; |
| 81 var configTags = parserConfig && parserConfig.tags, configScript = parserCon
fig && parserConfig.scriptTypes; |
| 82 addTags(defaultTags, tags); |
| 83 if (configTags) addTags(configTags, tags); |
| 84 if (configScript) for (var i = configScript.length - 1; i >= 0; i--) |
| 85 tags.script.unshift(["type", configScript[i].matches, configScript[i].mode
]) |
120 | 86 |
| 87 function html(stream, state) { |
| 88 var style = htmlMode.token(stream, state.htmlState), tag = /\btag\b/.test(
style), tagName |
| 89 if (tag && !/[<>\s\/]/.test(stream.current()) && |
| 90 (tagName = state.htmlState.tagName && state.htmlState.tagName.toLowerC
ase()) && |
| 91 tags.hasOwnProperty(tagName)) { |
| 92 state.inTag = tagName + " " |
| 93 } else if (state.inTag && tag && />$/.test(stream.current())) { |
| 94 var inTag = /^([\S]+) (.*)/.exec(state.inTag) |
| 95 state.inTag = null |
| 96 var modeSpec = stream.current() == ">" && findMatchingMode(tags[inTag[1]
], inTag[2]) |
| 97 var mode = CodeMirror.getMode(config, modeSpec) |
| 98 var endTagA = getTagRegexp(inTag[1], true), endTag = getTagRegexp(inTag[
1], false); |
| 99 state.token = function (stream, state) { |
| 100 if (stream.match(endTagA, false)) { |
| 101 state.token = html; |
| 102 state.localState = state.localMode = null; |
| 103 return null; |
| 104 } |
| 105 return maybeBackup(stream, endTag, state.localMode.token(stream, state
.localState)); |
| 106 }; |
| 107 state.localMode = mode; |
| 108 state.localState = CodeMirror.startState(mode, htmlMode.indent(state.htm
lState, "")); |
| 109 } else if (state.inTag) { |
| 110 state.inTag += stream.current() |
| 111 if (stream.eol()) state.inTag += " " |
| 112 } |
| 113 return style; |
| 114 }; |
| 115 |
| 116 return { |
| 117 startState: function () { |
| 118 var state = CodeMirror.startState(htmlMode); |
| 119 return {token: html, inTag: null, localMode: null, localState: null, htm
lState: state}; |
| 120 }, |
| 121 |
| 122 copyState: function (state) { |
| 123 var local; |
| 124 if (state.localState) { |
| 125 local = CodeMirror.copyState(state.localMode, state.localState); |
| 126 } |
| 127 return {token: state.token, inTag: state.inTag, |
| 128 localMode: state.localMode, localState: local, |
| 129 htmlState: CodeMirror.copyState(htmlMode, state.htmlState)}; |
| 130 }, |
| 131 |
| 132 token: function (stream, state) { |
| 133 return state.token(stream, state); |
| 134 }, |
| 135 |
| 136 indent: function (state, textAfter) { |
| 137 if (!state.localMode || /^\s*<\//.test(textAfter)) |
| 138 return htmlMode.indent(state.htmlState, textAfter); |
| 139 else if (state.localMode.indent) |
| 140 return state.localMode.indent(state.localState, textAfter); |
| 141 else |
| 142 return CodeMirror.Pass; |
| 143 }, |
| 144 |
| 145 innerMode: function (state) { |
| 146 return {state: state.localState || state.htmlState, mode: state.localMod
e || htmlMode}; |
| 147 } |
| 148 }; |
| 149 }, "xml", "javascript", "css"); |
| 150 |
| 151 CodeMirror.defineMIME("text/html", "htmlmixed"); |
121 }); | 152 }); |
OLD | NEW |