UNPKG

nornj

Version:

A powerful template engine that can works with React, JSX enhancement or alternative tools.

246 lines (190 loc) 8.75 kB
"use strict"; 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.checkElem = checkElem; var _core = _interopRequireDefault(require("../core")); var tools = _interopRequireWildcard(require("../utils/tools")); var tranParam = _interopRequireWildcard(require("../transforms/transformParam")); var tranElem = _interopRequireWildcard(require("../transforms/transformElement")); 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 }; } var NO_SPLIT_NEWLINE = ['style', 'script', 'textarea', 'pre', 'xmp', 'template', 'noscript', _core["default"].textTag]; function _plainTextNode(obj, parent, parentContent, noSplitNewline, tmplRule) { var node = {}; node.type = 'nj_plaintext'; node.content = [tranParam.compiledParam(obj, tmplRule, null, null, parent.ex != null ? tranElem.exCompileConfig(parent.ex).isBindable : null)]; node.allowNewline = noSplitNewline; parent[parentContent].push(node); } var REGEX_REPLACE_BP = /_njBp(\d+)_/g; //检测元素节点 function checkElem(obj, parent, tmplRule, hasExProps, noSplitNewline, isLast) { var parentContent = 'content'; if (!tools.isArray(obj)) { //判断是否为文本节点 if (tools.isString(obj)) { if (!noSplitNewline) { var braceParams = []; var strs = obj.replace(tmplRule.braceParamG, function (match) { braceParams.push(match); return '_njBp' + (braceParams.length - 1) + '_'; }).split(/\n/g); strs.forEach(function (str, i) { str = str.trim(); str !== '' && _plainTextNode(str.replace(REGEX_REPLACE_BP, function (all, g1) { return braceParams[g1]; }), parent, parentContent, noSplitNewline, tmplRule); }); } else { _plainTextNode(isLast && parent.allowNewline === 'nlElem' && !_core["default"].textMode ? tools.trimRight(obj) : obj, parent, parentContent, noSplitNewline, tmplRule); } } else { _plainTextNode(obj, parent, parentContent, noSplitNewline, tmplRule); } return; } var node = {}, first = obj[0]; if (tools.isString(first)) { //第一个子节点为字符串 var len = obj.length, last = obj[len - 1]; var isElemNode = false, exParams; //判断是否为xml标签 var openTagName, hasCloseTag = false, isParamsEx, isDirective, isSubTag, needAddToProps; var ex = tranElem.isEx(first, tmplRule); if (!ex) { var xmlOpenTag = tranElem.getXmlOpenTag(first, tmplRule); if (xmlOpenTag) { //tagname为xml标签时,则认为是元素节点 openTagName = xmlOpenTag[1]; if (/\/$/.test(openTagName)) { openTagName = openTagName.substr(0, openTagName.length - 1); } if (!tranElem.isXmlSelfCloseTag(first)) { //非自闭合标签才验证是否存在关闭标签 hasCloseTag = tranElem.isXmlCloseTag(last, openTagName); } else { //自闭合标签 node.selfCloseTag = true; } isElemNode = true; } } else { //为扩展标签,也可视为一个元素节点 var exName = ex[0]; exParams = ex[1]; isParamsEx = tranElem.isParamsEx(exName); if (!isParamsEx) { var exConfig = tranElem.exCompileConfig(exName); isDirective = exConfig.isDirective; isSubTag = exConfig.isSubTag; needAddToProps = isDirective ? !hasExProps : isSubTag; if (exConfig.useString) { node.useString = exConfig.useString; } } node.type = 'nj_ex'; node.ex = exName; if (exParams != null && !isParamsEx) { if (!node.args) { node.args = []; } tools.each(exParams, function (param) { var key = param.key, value = param.value; if (key === 'useString') { node.useString = !(value === 'false'); return; } else if (key === '_njIsDirective') { node.isDirective = isDirective = true; needAddToProps = !hasExProps; return; } var paramV = tranParam.compiledParam(value, tmplRule, param.hasColon, param.onlyKey); if (param.onlyBrace) { //提取匿名参数 node.args.push(paramV); } else { if (!node.params) { node.params = tools.obj(); } node.params[key] = paramV; } }, true); } isElemNode = true; } if (isElemNode) { //判断是否为元素节点 var pushContent = true; if (noSplitNewline) { node.allowNewline = true; } if (!ex) { node.type = openTagName; //If open tag has a brace,add the typeRefer param. var typeRefer = tranElem.getInsideBraceParam(openTagName, tmplRule); if (typeRefer) { node.typeRefer = tranParam.compiledParam(typeRefer[0], tmplRule); } //获取openTag内参数 var tagParams = tranElem.getOpenTagParams(first, tmplRule); if (tagParams) { if (!node.params) { node.params = tools.obj(); } tools.each(tagParams, function (param) { //The parameter like "{prop}" needs to be replaced. node.params[param.onlyBrace ? param.onlyBrace.replace(/\.\.\//g, '') : param.key] = tranParam.compiledParam(param.value, tmplRule, param.hasColon, param.onlyKey); }, true); } //Verify if self closing tag again, because the tag may be similar to "<br></br>". if (!node.selfCloseTag) { node.selfCloseTag = tranElem.verifySelfCloseTag(openTagName); } if (noSplitNewline == null && NO_SPLIT_NEWLINE.indexOf(openTagName.toLowerCase()) > -1) { noSplitNewline = true; node.allowNewline = 'nlElem'; } } else { if (isParamsEx || needAddToProps) { pushContent = false; } if (noSplitNewline == null && node.ex === 'pre') { noSplitNewline = true; node.allowNewline = 'nlElem'; } } //放入父节点content内 if (pushContent) { parent[parentContent].push(node); } //取出子节点集合 var end = len - (hasCloseTag ? 1 : 0), content = obj.slice(1, end); if (content && content.length) { _checkContentElem(content, node, tmplRule, isParamsEx || hasExProps && !isDirective, noSplitNewline); } //If this is params block, set on the "paramsEx" property of the parent node. if (isParamsEx || needAddToProps) { tranElem.addParamsEx(node, parent, isDirective, isSubTag); } } else { //如果不是元素节点,则为节点集合 _checkContentElem(obj, parent, tmplRule, hasExProps, noSplitNewline); } } else if (tools.isArray(first)) { //如果第一个子节点为数组,则该节点一定为节点集合(可以是多层数组嵌套的集合) _checkContentElem(obj, parent, tmplRule, hasExProps, noSplitNewline); } } //检测子元素节点 function _checkContentElem(obj, parent, tmplRule, hasExProps, noSplitNewline) { if (!parent.content) { parent.content = []; } tools.each(obj, function (item, i, l) { checkElem(item, parent, tmplRule, hasExProps, noSplitNewline, i == l - 1); }, true); }