Fix: merge conflict
[myslice.git] / to-be-integrated / third-party / codemirror-3.15 / addon / hint / xml-hint.js
1 (function() {
2   "use strict";
3
4   var Pos = CodeMirror.Pos;
5
6   function getHints(cm, options) {
7     var tags = options && options.schemaInfo;
8     var quote = (options && options.quoteChar) || '"';
9     if (!tags) return;
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
16       if (isTag) {
17         prefix = token.string.slice(1);
18         replaceToken = true;
19       }
20       var cx = inner.state.context, curTag = cx && tags[cx.tagName];
21       var childList = cx ? curTag && curTag.children : tags["!top"];
22       if (childList) {
23         for (var i = 0; i < childList.length; ++i) if (!prefix || childList[i].indexOf(prefix) == 0)
24           result.push("<" + childList[i]);
25       } else {
26         for (var name in tags) if (tags.hasOwnProperty(name) && name != "!top" && (!prefix || name.indexOf(prefix) == 0))
27           result.push("<" + name);
28       }
29       if (cx && (!prefix || ("/" + cx.tagName).indexOf(prefix) == 0))
30         result.push("</" + cx.tagName + ">");
31     } else {
32       // Attribute completion
33       var curTag = tags[inner.state.tagName], attrs = curTag && curTag.attrs;
34       if (!attrs) return;
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);
45           }
46           replaceToken = true;
47         }
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;
53           replaceToken = true;
54         }
55         for (var attr in attrs) if (attrs.hasOwnProperty(attr) && (!prefix || attr.indexOf(prefix) == 0))
56           result.push(attr);
57       }
58     }
59     return {
60       list: result,
61       from: replaceToken ? Pos(cur.line, token.start) : cur,
62       to: replaceToken ? Pos(cur.line, token.end) : cur
63     };
64   }
65
66   CodeMirror.xmlHint = getHints; // deprecated
67   CodeMirror.registerHelper("hint", "xml", getHints);
68 })();