Path: blob/master/web-gui/buildyourownbotnet/assets/js/codemirror/addon/hint/xml-hint.js
2076 views
(function() {1"use strict";23var Pos = CodeMirror.Pos;45function getHints(cm, options) {6var tags = options && options.schemaInfo;7var quote = (options && options.quoteChar) || '"';8if (!tags) return;9var cur = cm.getCursor(), token = cm.getTokenAt(cur);10var inner = CodeMirror.innerMode(cm.getMode(), token.state);11if (inner.mode.name != "xml") return;12var result = [], replaceToken = false, prefix;13var isTag = token.string.charAt(0) == "<";14if (!inner.state.tagName || isTag) { // Tag completion15if (isTag) {16prefix = token.string.slice(1);17replaceToken = true;18}19var cx = inner.state.context, curTag = cx && tags[cx.tagName];20var childList = cx ? curTag && curTag.children : tags["!top"];21if (childList) {22for (var i = 0; i < childList.length; ++i) if (!prefix || childList[i].lastIndexOf(prefix, 0) == 0)23result.push("<" + childList[i]);24} else {25for (var name in tags) if (tags.hasOwnProperty(name) && name != "!top" && (!prefix || name.lastIndexOf(prefix, 0) == 0))26result.push("<" + name);27}28if (cx && (!prefix || ("/" + cx.tagName).lastIndexOf(prefix, 0) == 0))29result.push("</" + cx.tagName + ">");30} else {31// Attribute completion32var curTag = tags[inner.state.tagName], attrs = curTag && curTag.attrs;33if (!attrs) return;34if (token.type == "string" || token.string == "=") { // A value35var before = cm.getRange(Pos(cur.line, Math.max(0, cur.ch - 60)),36Pos(cur.line, token.type == "string" ? token.start : token.end));37var atName = before.match(/([^\s\u00a0=<>\"\']+)=$/), atValues;38if (!atName || !attrs.hasOwnProperty(atName[1]) || !(atValues = attrs[atName[1]])) return;39if (typeof atValues == 'function') atValues = atValues.call(this, cm); // Functions can be used to supply values for autocomplete widget40if (token.type == "string") {41prefix = token.string;42if (/['"]/.test(token.string.charAt(0))) {43quote = token.string.charAt(0);44prefix = token.string.slice(1);45}46replaceToken = true;47}48for (var i = 0; i < atValues.length; ++i) if (!prefix || atValues[i].lastIndexOf(prefix, 0) == 0)49result.push(quote + atValues[i] + quote);50} else { // An attribute name51if (token.type == "attribute") {52prefix = token.string;53replaceToken = true;54}55for (var attr in attrs) if (attrs.hasOwnProperty(attr) && (!prefix || attr.lastIndexOf(prefix, 0) == 0))56result.push(attr);57}58}59return {60list: result,61from: replaceToken ? Pos(cur.line, token.start) : cur,62to: replaceToken ? Pos(cur.line, token.end) : cur63};64}6566CodeMirror.xmlHint = getHints; // deprecated67CodeMirror.registerHelper("hint", "xml", getHints);68})();697071