nornj
Version:
A powerful template engine that can works with React, JSX enhancement or alternative tools.
225 lines (168 loc) • 6.42 kB
JavaScript
;
function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getXmlOpenTag = getXmlOpenTag;
exports.isXmlSelfCloseTag = isXmlSelfCloseTag;
exports.verifySelfCloseTag = verifySelfCloseTag;
exports.getOpenTagParams = getOpenTagParams;
exports.isXmlCloseTag = isXmlCloseTag;
exports.getInsideBraceParam = getInsideBraceParam;
exports.isEx = isEx;
exports.isExAll = isExAll;
exports.fixExTagName = fixExTagName;
exports.isParamsEx = isParamsEx;
exports.addParamsEx = addParamsEx;
exports.exCompileConfig = exCompileConfig;
exports.isPropS = isPropS;
exports.isStrPropS = isStrPropS;
exports.OMITTED_CLOSE_TAGS = void 0;
var _core = _interopRequireDefault(require("../core"));
var tools = _interopRequireWildcard(require("../utils/tools"));
var _extension = require("../helpers/extension");
function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
//提取xml open tag
function getXmlOpenTag(obj, tmplRule) {
return tmplRule.xmlOpenTag.exec(obj);
} //验证xml self close tag
var REGEX_XML_SELF_CLOSE_TAG = /^<[^>]+\/>$/i;
function isXmlSelfCloseTag(obj) {
return REGEX_XML_SELF_CLOSE_TAG.test(obj);
} //Verify self close tag name
var OMITTED_CLOSE_TAGS = {
area: true,
base: true,
br: true,
col: true,
embed: true,
hr: true,
img: true,
input: true,
keygen: true,
link: true,
meta: true,
param: true,
source: true,
track: true,
wbr: true
};
exports.OMITTED_CLOSE_TAGS = OMITTED_CLOSE_TAGS;
function verifySelfCloseTag(tagName) {
return OMITTED_CLOSE_TAGS[tagName.toLowerCase()];
} //Extract parameters inside the xml open tag
function getOpenTagParams(tag, tmplRule) {
var pattern = tmplRule.openTagParams;
var matchArr, ret;
while (matchArr = pattern.exec(tag)) {
var key = matchArr[1];
if (key === '/') {
//If match to the last of "/", then continue the loop.
continue;
}
if (!ret) {
ret = [];
}
var value = matchArr[8],
onlyKey = false;
var onlyBrace = matchArr[4] != null ? matchArr[4] : matchArr[6];
if (value != null) {
value = tools.clearQuot(value); //Remove quotation marks
} else {
value = key; //Match to Similar to "checked" or "disabled" attribute.
if (!onlyBrace) {
onlyKey = true;
}
} //Removed at the end of "/>", ">" or "/".
if (!matchArr[9] && !matchArr[10]) {
if (/\/>$/.test(value)) {
value = value.substr(0, value.length - 2);
} else if (/>$/.test(value) || /\/$/.test(value)) {
value = value.substr(0, value.length - 1);
}
} //Transform special key
var hasColon = void 0;
if (key[0] === ':') {
key = key.substr(1);
hasColon = true;
}
ret.push({
key: key,
value: value,
onlyBrace: onlyBrace,
hasColon: hasColon,
onlyKey: onlyKey
});
}
return ret;
} //判断xml close tag
function isXmlCloseTag(obj, tagName) {
return tools.isString(obj) && obj.toLowerCase() === '</' + tagName + '>';
} //get inside brace param
function getInsideBraceParam(obj, tmplRule) {
return tmplRule.braceParam.exec(obj);
} //判断扩展标签并返回参数
function isEx(obj, tmplRule, noParams) {
var ret;
var ret1 = tmplRule.extension.exec(obj);
if (ret1) {
ret = [ret1[1]];
if (!noParams) {
var params = getOpenTagParams(obj, tmplRule); //提取各参数
if (params) {
ret.push(params);
}
}
}
return ret;
}
function isExAll(obj, tmplRule) {
return obj.match(tmplRule.exAll);
}
var REGEX_LOWER_CASE = /^[a-z]/;
var REGEX_UPPER_CASE = /^[A-Z]/;
function fixExTagName(tagName, tmplRule) {
var ret;
if (!_core["default"].fixTagName) {
return ret;
}
var _tagName = tools.lowerFirst(tagName),
config = _extension.extensionConfig[_tagName];
if (config && (!config.needPrefix || config.needPrefix == 'onlyUpperCase' && REGEX_LOWER_CASE.test(tagName) || config.needPrefix == 'onlyLowerCase' && REGEX_UPPER_CASE.test(tagName))) {
ret = tmplRule.extensionRule + _tagName;
}
return ret;
} //Test whether as parameters extension
function isParamsEx(name) {
return name === 'params' || name === 'props';
} //Add to the "paramsEx" property of the parent node
function addParamsEx(node, parent, isDirective, isSubTag) {
var exPropsName = 'paramsEx';
if (!parent[exPropsName]) {
var exPropsNode;
if (isDirective || isSubTag) {
exPropsNode = {
type: 'nj_ex',
ex: 'props',
content: [node]
};
} else {
exPropsNode = node;
}
exPropsNode.parentType = parent.type;
parent[exPropsName] = exPropsNode;
} else {
tools.arrayPush(parent[exPropsName].content, isDirective || isSubTag ? [node] : node.content);
}
}
function exCompileConfig(name) {
return _extension.extensionConfig[name] || {};
}
function isPropS(elemName, tmplRule) {
return elemName.indexOf(tmplRule.propRule) === 0;
}
function isStrPropS(elemName, tmplRule) {
return elemName.indexOf(tmplRule.strPropRule + tmplRule.propRule) === 0;
}