4 var Pos = CodeMirror.Pos;
6 function getHints(cm, options) {
7 var tags = options && options.schemaInfo;
8 var quote = (options && options.quoteChar) || '"';
10 var cur = cm.getCursor(), token = cm.getTokenAt(cur);
11 var inner = CodeMirror.innerMode(cm.getMode(), token.state);
12 if (inner.mode.name != "xml") return;
13 var result = [], replaceToken = false, prefix;
14 var isTag = token.string.charAt(0) == "<";
15 if (!inner.state.tagName || isTag) { // Tag completion
17 prefix = token.string.slice(1);
20 var cx = inner.state.context, curTag = cx && tags[cx.tagName];
21 var childList = cx ? curTag && curTag.children : tags["!top"];
23 for (var i = 0; i < childList.length; ++i) if (!prefix || childList[i].indexOf(prefix) == 0)
24 result.push("<" + childList[i]);
26 for (var name in tags) if (tags.hasOwnProperty(name) && name != "!top" && (!prefix || name.indexOf(prefix) == 0))
27 result.push("<" + name);
29 if (cx && (!prefix || ("/" + cx.tagName).indexOf(prefix) == 0))
30 result.push("</" + cx.tagName + ">");
32 // Attribute completion
33 var curTag = tags[inner.state.tagName], attrs = curTag && curTag.attrs;
35 if (token.type == "string" || token.string == "=") { // A value
36 var before = cm.getRange(Pos(cur.line, Math.max(0, cur.ch - 60)),
37 Pos(cur.line, token.type == "string" ? token.start : token.end));
38 var atName = before.match(/([^\s\u00a0=<>\"\']+)=$/), atValues;
39 if (!atName || !attrs.hasOwnProperty(atName[1]) || !(atValues = attrs[atName[1]])) return;
40 if (token.type == "string") {
41 prefix = token.string;
42 if (/['"]/.test(token.string.charAt(0))) {
43 quote = token.string.charAt(0);
44 prefix = token.string.slice(1);
48 for (var i = 0; i < atValues.length; ++i) if (!prefix || atValues[i].indexOf(prefix) == 0)
49 result.push(quote + atValues[i] + quote);
50 } else { // An attribute name
51 if (token.type == "attribute") {
52 prefix = token.string;
55 for (var attr in attrs) if (attrs.hasOwnProperty(attr) && (!prefix || attr.indexOf(prefix) == 0))
61 from: replaceToken ? Pos(cur.line, token.start) : cur,
62 to: replaceToken ? Pos(cur.line, token.end) : cur
66 CodeMirror.xmlHint = getHints; // deprecated
67 CodeMirror.registerHelper("hint", "xml", getHints);