提交 | 用户 | 时间
|
e7c126
|
1 |
/* |
H |
2 |
* Copyright 1999-2018 Alibaba Group Holding Ltd. |
|
3 |
* |
|
4 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
5 |
* you may not use this file except in compliance with the License. |
|
6 |
* You may obtain a copy of the License at |
|
7 |
* |
|
8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
|
9 |
* |
|
10 |
* Unless required by applicable law or agreed to in writing, software |
|
11 |
* distributed under the License is distributed on an "AS IS" BASIS, |
|
12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13 |
* See the License for the specific language governing permissions and |
|
14 |
* limitations under the License. |
|
15 |
*/ |
|
16 |
|
|
17 |
(function(mod) { |
|
18 |
if (typeof exports == "object" && typeof module == "object") // CommonJS |
|
19 |
mod(require("../../lib/codemirror")); |
|
20 |
else if (typeof define == "function" && define.amd) // AMD |
|
21 |
define(["../../lib/codemirror"], mod); |
|
22 |
else // Plain browser env |
|
23 |
mod(CodeMirror); |
|
24 |
})(function(CodeMirror) { |
|
25 |
"use strict"; |
|
26 |
|
|
27 |
var htmlConfig = { |
|
28 |
autoSelfClosers: {'area': true, 'base': true, 'br': true, 'col': true, 'command': true, |
|
29 |
'embed': true, 'frame': true, 'hr': true, 'img': true, 'input': true, |
|
30 |
'keygen': true, 'link': true, 'meta': true, 'param': true, 'source': true, |
|
31 |
'track': true, 'wbr': true, 'menuitem': true}, |
|
32 |
implicitlyClosed: {'dd': true, 'li': true, 'optgroup': true, 'option': true, 'p': true, |
|
33 |
'rp': true, 'rt': true, 'tbody': true, 'td': true, 'tfoot': true, |
|
34 |
'th': true, 'tr': true}, |
|
35 |
contextGrabbers: { |
|
36 |
'dd': {'dd': true, 'dt': true}, |
|
37 |
'dt': {'dd': true, 'dt': true}, |
|
38 |
'li': {'li': true}, |
|
39 |
'option': {'option': true, 'optgroup': true}, |
|
40 |
'optgroup': {'optgroup': true}, |
|
41 |
'p': {'address': true, 'article': true, 'aside': true, 'blockquote': true, 'dir': true, |
|
42 |
'div': true, 'dl': true, 'fieldset': true, 'footer': true, 'form': true, |
|
43 |
'h1': true, 'h2': true, 'h3': true, 'h4': true, 'h5': true, 'h6': true, |
|
44 |
'header': true, 'hgroup': true, 'hr': true, 'menu': true, 'nav': true, 'ol': true, |
|
45 |
'p': true, 'pre': true, 'section': true, 'table': true, 'ul': true}, |
|
46 |
'rp': {'rp': true, 'rt': true}, |
|
47 |
'rt': {'rp': true, 'rt': true}, |
|
48 |
'tbody': {'tbody': true, 'tfoot': true}, |
|
49 |
'td': {'td': true, 'th': true}, |
|
50 |
'tfoot': {'tbody': true}, |
|
51 |
'th': {'td': true, 'th': true}, |
|
52 |
'thead': {'tbody': true, 'tfoot': true}, |
|
53 |
'tr': {'tr': true} |
|
54 |
}, |
|
55 |
doNotIndent: {"pre": true}, |
|
56 |
allowUnquoted: true, |
|
57 |
allowMissing: true, |
|
58 |
caseFold: true |
|
59 |
} |
|
60 |
|
|
61 |
var xmlConfig = { |
|
62 |
autoSelfClosers: {}, |
|
63 |
implicitlyClosed: {}, |
|
64 |
contextGrabbers: {}, |
|
65 |
doNotIndent: {}, |
|
66 |
allowUnquoted: false, |
|
67 |
allowMissing: false, |
|
68 |
caseFold: false |
|
69 |
} |
|
70 |
|
|
71 |
CodeMirror.defineMode("xml", function(editorConf, config_) { |
|
72 |
var indentUnit = editorConf.indentUnit |
|
73 |
var config = {} |
|
74 |
var defaults = config_.htmlMode ? htmlConfig : xmlConfig |
|
75 |
for (var prop in defaults) config[prop] = defaults[prop] |
|
76 |
for (var prop in config_) config[prop] = config_[prop] |
|
77 |
|
|
78 |
// Return variables for tokenizers |
|
79 |
var type, setStyle; |
|
80 |
|
|
81 |
function inText(stream, state) { |
|
82 |
function chain(parser) { |
|
83 |
state.tokenize = parser; |
|
84 |
return parser(stream, state); |
|
85 |
} |
|
86 |
|
|
87 |
var ch = stream.next(); |
|
88 |
if (ch == "<") { |
|
89 |
if (stream.eat("!")) { |
|
90 |
if (stream.eat("[")) { |
|
91 |
if (stream.match("CDATA[")) return chain(inBlock("atom", "]]>")); |
|
92 |
else return null; |
|
93 |
} else if (stream.match("--")) { |
|
94 |
return chain(inBlock("comment", "-->")); |
|
95 |
} else if (stream.match("DOCTYPE", true, true)) { |
|
96 |
stream.eatWhile(/[\w\._\-]/); |
|
97 |
return chain(doctype(1)); |
|
98 |
} else { |
|
99 |
return null; |
|
100 |
} |
|
101 |
} else if (stream.eat("?")) { |
|
102 |
stream.eatWhile(/[\w\._\-]/); |
|
103 |
state.tokenize = inBlock("meta", "?>"); |
|
104 |
return "meta"; |
|
105 |
} else { |
|
106 |
type = stream.eat("/") ? "closeTag" : "openTag"; |
|
107 |
state.tokenize = inTag; |
|
108 |
return "tag bracket"; |
|
109 |
} |
|
110 |
} else if (ch == "&") { |
|
111 |
var ok; |
|
112 |
if (stream.eat("#")) { |
|
113 |
if (stream.eat("x")) { |
|
114 |
ok = stream.eatWhile(/[a-fA-F\d]/) && stream.eat(";"); |
|
115 |
} else { |
|
116 |
ok = stream.eatWhile(/[\d]/) && stream.eat(";"); |
|
117 |
} |
|
118 |
} else { |
|
119 |
ok = stream.eatWhile(/[\w\.\-:]/) && stream.eat(";"); |
|
120 |
} |
|
121 |
return ok ? "atom" : "error"; |
|
122 |
} else { |
|
123 |
stream.eatWhile(/[^&<]/); |
|
124 |
return null; |
|
125 |
} |
|
126 |
} |
|
127 |
inText.isInText = true; |
|
128 |
|
|
129 |
function inTag(stream, state) { |
|
130 |
var ch = stream.next(); |
|
131 |
if (ch == ">" || (ch == "/" && stream.eat(">"))) { |
|
132 |
state.tokenize = inText; |
|
133 |
type = ch == ">" ? "endTag" : "selfcloseTag"; |
|
134 |
return "tag bracket"; |
|
135 |
} else if (ch == "=") { |
|
136 |
type = "equals"; |
|
137 |
return null; |
|
138 |
} else if (ch == "<") { |
|
139 |
state.tokenize = inText; |
|
140 |
state.state = baseState; |
|
141 |
state.tagName = state.tagStart = null; |
|
142 |
var next = state.tokenize(stream, state); |
|
143 |
return next ? next + " tag error" : "tag error"; |
|
144 |
} else if (/[\'\"]/.test(ch)) { |
|
145 |
state.tokenize = inAttribute(ch); |
|
146 |
state.stringStartCol = stream.column(); |
|
147 |
return state.tokenize(stream, state); |
|
148 |
} else { |
|
149 |
stream.match(/^[^\s\u00a0=<>\"\']*[^\s\u00a0=<>\"\'\/]/); |
|
150 |
return "word"; |
|
151 |
} |
|
152 |
} |
|
153 |
|
|
154 |
function inAttribute(quote) { |
|
155 |
var closure = function(stream, state) { |
|
156 |
while (!stream.eol()) { |
|
157 |
if (stream.next() == quote) { |
|
158 |
state.tokenize = inTag; |
|
159 |
break; |
|
160 |
} |
|
161 |
} |
|
162 |
return "string"; |
|
163 |
}; |
|
164 |
closure.isInAttribute = true; |
|
165 |
return closure; |
|
166 |
} |
|
167 |
|
|
168 |
function inBlock(style, terminator) { |
|
169 |
return function(stream, state) { |
|
170 |
while (!stream.eol()) { |
|
171 |
if (stream.match(terminator)) { |
|
172 |
state.tokenize = inText; |
|
173 |
break; |
|
174 |
} |
|
175 |
stream.next(); |
|
176 |
} |
|
177 |
return style; |
|
178 |
}; |
|
179 |
} |
|
180 |
function doctype(depth) { |
|
181 |
return function(stream, state) { |
|
182 |
var ch; |
|
183 |
while ((ch = stream.next()) != null) { |
|
184 |
if (ch == "<") { |
|
185 |
state.tokenize = doctype(depth + 1); |
|
186 |
return state.tokenize(stream, state); |
|
187 |
} else if (ch == ">") { |
|
188 |
if (depth == 1) { |
|
189 |
state.tokenize = inText; |
|
190 |
break; |
|
191 |
} else { |
|
192 |
state.tokenize = doctype(depth - 1); |
|
193 |
return state.tokenize(stream, state); |
|
194 |
} |
|
195 |
} |
|
196 |
} |
|
197 |
return "meta"; |
|
198 |
}; |
|
199 |
} |
|
200 |
|
|
201 |
function Context(state, tagName, startOfLine) { |
|
202 |
this.prev = state.context; |
|
203 |
this.tagName = tagName; |
|
204 |
this.indent = state.indented; |
|
205 |
this.startOfLine = startOfLine; |
|
206 |
if (config.doNotIndent.hasOwnProperty(tagName) || (state.context && state.context.noIndent)) |
|
207 |
this.noIndent = true; |
|
208 |
} |
|
209 |
function popContext(state) { |
|
210 |
if (state.context) state.context = state.context.prev; |
|
211 |
} |
|
212 |
function maybePopContext(state, nextTagName) { |
|
213 |
var parentTagName; |
|
214 |
while (true) { |
|
215 |
if (!state.context) { |
|
216 |
return; |
|
217 |
} |
|
218 |
parentTagName = state.context.tagName; |
|
219 |
if (!config.contextGrabbers.hasOwnProperty(parentTagName) || |
|
220 |
!config.contextGrabbers[parentTagName].hasOwnProperty(nextTagName)) { |
|
221 |
return; |
|
222 |
} |
|
223 |
popContext(state); |
|
224 |
} |
|
225 |
} |
|
226 |
|
|
227 |
function baseState(type, stream, state) { |
|
228 |
if (type == "openTag") { |
|
229 |
state.tagStart = stream.column(); |
|
230 |
return tagNameState; |
|
231 |
} else if (type == "closeTag") { |
|
232 |
return closeTagNameState; |
|
233 |
} else { |
|
234 |
return baseState; |
|
235 |
} |
|
236 |
} |
|
237 |
function tagNameState(type, stream, state) { |
|
238 |
if (type == "word") { |
|
239 |
state.tagName = stream.current(); |
|
240 |
setStyle = "tag"; |
|
241 |
return attrState; |
|
242 |
} else { |
|
243 |
setStyle = "error"; |
|
244 |
return tagNameState; |
|
245 |
} |
|
246 |
} |
|
247 |
function closeTagNameState(type, stream, state) { |
|
248 |
if (type == "word") { |
|
249 |
var tagName = stream.current(); |
|
250 |
if (state.context && state.context.tagName != tagName && |
|
251 |
config.implicitlyClosed.hasOwnProperty(state.context.tagName)) |
|
252 |
popContext(state); |
|
253 |
if ((state.context && state.context.tagName == tagName) || config.matchClosing === false) { |
|
254 |
setStyle = "tag"; |
|
255 |
return closeState; |
|
256 |
} else { |
|
257 |
setStyle = "tag error"; |
|
258 |
return closeStateErr; |
|
259 |
} |
|
260 |
} else { |
|
261 |
setStyle = "error"; |
|
262 |
return closeStateErr; |
|
263 |
} |
|
264 |
} |
|
265 |
|
|
266 |
function closeState(type, _stream, state) { |
|
267 |
if (type != "endTag") { |
|
268 |
setStyle = "error"; |
|
269 |
return closeState; |
|
270 |
} |
|
271 |
popContext(state); |
|
272 |
return baseState; |
|
273 |
} |
|
274 |
function closeStateErr(type, stream, state) { |
|
275 |
setStyle = "error"; |
|
276 |
return closeState(type, stream, state); |
|
277 |
} |
|
278 |
|
|
279 |
function attrState(type, _stream, state) { |
|
280 |
if (type == "word") { |
|
281 |
setStyle = "attribute"; |
|
282 |
return attrEqState; |
|
283 |
} else if (type == "endTag" || type == "selfcloseTag") { |
|
284 |
var tagName = state.tagName, tagStart = state.tagStart; |
|
285 |
state.tagName = state.tagStart = null; |
|
286 |
if (type == "selfcloseTag" || |
|
287 |
config.autoSelfClosers.hasOwnProperty(tagName)) { |
|
288 |
maybePopContext(state, tagName); |
|
289 |
} else { |
|
290 |
maybePopContext(state, tagName); |
|
291 |
state.context = new Context(state, tagName, tagStart == state.indented); |
|
292 |
} |
|
293 |
return baseState; |
|
294 |
} |
|
295 |
setStyle = "error"; |
|
296 |
return attrState; |
|
297 |
} |
|
298 |
function attrEqState(type, stream, state) { |
|
299 |
if (type == "equals") return attrValueState; |
|
300 |
if (!config.allowMissing) setStyle = "error"; |
|
301 |
return attrState(type, stream, state); |
|
302 |
} |
|
303 |
function attrValueState(type, stream, state) { |
|
304 |
if (type == "string") return attrContinuedState; |
|
305 |
if (type == "word" && config.allowUnquoted) {setStyle = "string"; return attrState;} |
|
306 |
setStyle = "error"; |
|
307 |
return attrState(type, stream, state); |
|
308 |
} |
|
309 |
function attrContinuedState(type, stream, state) { |
|
310 |
if (type == "string") return attrContinuedState; |
|
311 |
return attrState(type, stream, state); |
|
312 |
} |
|
313 |
|
|
314 |
return { |
|
315 |
startState: function(baseIndent) { |
|
316 |
var state = {tokenize: inText, |
|
317 |
state: baseState, |
|
318 |
indented: baseIndent || 0, |
|
319 |
tagName: null, tagStart: null, |
|
320 |
context: null} |
|
321 |
if (baseIndent != null) state.baseIndent = baseIndent |
|
322 |
return state |
|
323 |
}, |
|
324 |
|
|
325 |
token: function(stream, state) { |
|
326 |
if (!state.tagName && stream.sol()) |
|
327 |
state.indented = stream.indentation(); |
|
328 |
|
|
329 |
if (stream.eatSpace()) return null; |
|
330 |
type = null; |
|
331 |
var style = state.tokenize(stream, state); |
|
332 |
if ((style || type) && style != "comment") { |
|
333 |
setStyle = null; |
|
334 |
state.state = state.state(type || style, stream, state); |
|
335 |
if (setStyle) |
|
336 |
style = setStyle == "error" ? style + " error" : setStyle; |
|
337 |
} |
|
338 |
return style; |
|
339 |
}, |
|
340 |
|
|
341 |
indent: function(state, textAfter, fullLine) { |
|
342 |
var context = state.context; |
|
343 |
// Indent multi-line strings (e.g. css). |
|
344 |
if (state.tokenize.isInAttribute) { |
|
345 |
if (state.tagStart == state.indented) |
|
346 |
return state.stringStartCol + 1; |
|
347 |
else |
|
348 |
return state.indented + indentUnit; |
|
349 |
} |
|
350 |
if (context && context.noIndent) return CodeMirror.Pass; |
|
351 |
if (state.tokenize != inTag && state.tokenize != inText) |
|
352 |
return fullLine ? fullLine.match(/^(\s*)/)[0].length : 0; |
|
353 |
// Indent the starts of attribute names. |
|
354 |
if (state.tagName) { |
|
355 |
if (config.multilineTagIndentPastTag !== false) |
|
356 |
return state.tagStart + state.tagName.length + 2; |
|
357 |
else |
|
358 |
return state.tagStart + indentUnit * (config.multilineTagIndentFactor || 1); |
|
359 |
} |
|
360 |
if (config.alignCDATA && /<!\[CDATA\[/.test(textAfter)) return 0; |
|
361 |
var tagAfter = textAfter && /^<(\/)?([\w_:\.-]*)/.exec(textAfter); |
|
362 |
if (tagAfter && tagAfter[1]) { // Closing tag spotted |
|
363 |
while (context) { |
|
364 |
if (context.tagName == tagAfter[2]) { |
|
365 |
context = context.prev; |
|
366 |
break; |
|
367 |
} else if (config.implicitlyClosed.hasOwnProperty(context.tagName)) { |
|
368 |
context = context.prev; |
|
369 |
} else { |
|
370 |
break; |
|
371 |
} |
|
372 |
} |
|
373 |
} else if (tagAfter) { // Opening tag spotted |
|
374 |
while (context) { |
|
375 |
var grabbers = config.contextGrabbers[context.tagName]; |
|
376 |
if (grabbers && grabbers.hasOwnProperty(tagAfter[2])) |
|
377 |
context = context.prev; |
|
378 |
else |
|
379 |
break; |
|
380 |
} |
|
381 |
} |
|
382 |
while (context && context.prev && !context.startOfLine) |
|
383 |
context = context.prev; |
|
384 |
if (context) return context.indent + indentUnit; |
|
385 |
else return state.baseIndent || 0; |
|
386 |
}, |
|
387 |
|
|
388 |
electricInput: /<\/[\s\w:]+>$/, |
|
389 |
blockCommentStart: "<!--", |
|
390 |
blockCommentEnd: "-->", |
|
391 |
|
|
392 |
configuration: config.htmlMode ? "html" : "xml", |
|
393 |
helperType: config.htmlMode ? "html" : "xml", |
|
394 |
|
|
395 |
skipAttribute: function(state) { |
|
396 |
if (state.state == attrValueState) |
|
397 |
state.state = attrState |
|
398 |
} |
|
399 |
}; |
|
400 |
}); |
|
401 |
|
|
402 |
CodeMirror.defineMIME("text/xml", "xml"); |
|
403 |
CodeMirror.defineMIME("application/xml", "xml"); |
|
404 |
if (!CodeMirror.mimeModes.hasOwnProperty("text/html")) |
|
405 |
CodeMirror.defineMIME("text/html", {name: "xml", htmlMode: true}); |
|
406 |
|
|
407 |
}); |