diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..239ecff --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +node_modules +yarn.lock diff --git a/README.md b/README.md index 06d5cbb..310ad1f 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ The engine support the following data-attribute (the order isn't casual, is the * data-tdal-repeat="repeat-variabile path-expression" * data-tdal-content="path-expression-string-allowed" OR * data-tdal-replace="path-expression-string-allowed" -* data-tdal-attribute="attribute path-expression-string-allowed[; attributes path-expression-string-allowed]" +* data-tdal-attributes="attribute path-expression-string-allowed[; attributes path-expression-string-allowed]" * data-tdal-omittag="path-expression-boolean-mod-allowed" I plan the implement the jTDAL equivalent of the TAL's METAL (partials in mustache) in a future update. @@ -88,7 +88,7 @@ Replace the tag and its content with the result of the path-expression. Mutually If the result of the path expression is "false", the tag and its contents will be removed (like a false data-tdal-condition). If is "true", the default content will be kept. ### Attribute - data-tdal-attribute="href link | STRING:https://www.example.org/{page}; class STRING:link-color-blue" + data-tdal-attributes="href link | STRING:https://www.example.org/{page}; class STRING:link-color-blue" Add an attribute or replace the content of an attribute with the result of the path expression. If the expression value is "true" current attribute content will be kept. diff --git a/jTDAL.js b/jTDAL.js index 4d68184..0bf8cf4 100644 --- a/jTDAL.js +++ b/jTDAL.js @@ -280,14 +280,14 @@ var jTDAL; let tmpValue = ParsePath(tmpTDALrules[2]); if ("false" === tmpValue) { if ('undefined' !== (typeof attributes[tmpTDALrules[1]])) { - current[1] = current[1].replace(new RegExp('\\s*' + tmpTDALrules[1] + '(?:=([\'"]).*\\1)?'), ''); + current[1] = current[1].replace(new RegExp('\\s*' + tmpTDALrules[1] + '(?:=([\'"]).*?\\1)?'), ''); } } else if ("true" !== tmpValue) { current[2] += '+(false!==(t[i]=' + tmpValue + ')&&("string"===typeof t[i]||("number"===typeof t[i]&&!isNaN(t[i])))?" ' + tmpTDALrules[1] + '=\\""+t[i]+"\\"":(true!==t[i]?"":"'; if ('undefined' !== (typeof attributes[tmpTDALrules[1]])) { - current[1] = current[1].replace(new RegExp('\\s*' + tmpTDALrules[1] + '(?:=([\'"]).*\\1)?'), ''); + current[1] = current[1].replace(new RegExp('\\s*' + tmpTDALrules[1] + '(?:=([\'"]).*?\\1)?'), ''); current[2] += tmpTDALrules[1] + '"' + ((('undefined' !== (typeof attributes[tmpTDALrules[1]][3])) && ('' != attributes[tmpTDALrules[1]][3])) ? '+"="+' + JSON.stringify(String(attributes[tmpTDALrules[1]][2] + attributes[tmpTDALrules[1]][3] + diff --git a/jTDAL.min.js b/jTDAL.min.js index 0b7f8ed..b63863e 100644 --- a/jTDAL.min.js +++ b/jTDAL.min.js @@ -1 +1 @@ -"use strict";var jTDAL;!function(t){const e="(?:[\\w-\\/]*[\\w](?:[\\s]*\\|[\\s]*[\\w-\\/]*[\\w])*)",i="(STRING:[^;]+|(?:(?:!)?[\\w-\\/]*[\\w](?:[\\s]*\\|[\\s]*[\\w-\\/]*[\\w])*))",r={pathString:new RegExp("^[\\s]*STRING:(.*)$"),tagWithTDAL:new RegExp("<((?:\\w+:)?\\w+)(\\s+[^<>]+?)??\\s+data-tdal-(?:"+["condition","repeat","content","replace","attributes","omittag"].join("|")+")=(['\"])(.*?)\\3(\\s+[^<>]+?)??\\s*(/)?>","i"),tagWithAttribute:new RegExp("<((?:w+:)?w+)(s+[^<>]+?)??s+%s=(['\"])(.*?)\\3(s+[^<>]+?)??s*(/)?>","i"),tagAttributes:new RegExp("(?<=\\s)((?:[\\w-]+:)?[\\w-]+)=(?:(['\"])(.*?)\\2|([^>\\s'\"]+))","gi"),pathInString:new RegExp("{("+e+")}","g"),condition:new RegExp("^[\\s]*(\\!?)[\\s]*"+i+"[\\s]*$"),repeat:new RegExp("^[\\s]*([\\w-]+?)[\\s]+("+e+")[\\s]*$"),content:new RegExp("^[\\s]*(?:(text|structure)[\\s]+)?("+i+")[\\s]*$"),attributes:new RegExp("[\\s]*(?:(?:([\\w-]+?)[\\s]+"+i+"[\\s]*)(?:;[\\s]*|$))","g"),attributesTDAL:new RegExp("\\s*(data-tdal-[\\w-]+)=(?:(['\"])(.*?)\\2|([^>\\s'\"]+))","gi")};function n(t){let e="";switch(t){case"true":case"false":e=t;break;default:e=t.startsWith("!(")?"!"+t:"!("+a(t)+")"}return e}function a(t){let e='"undefined"===typeof (t[i++]='+t+')||false===t[--i]||null===t[i]||(Array.isArray(t[i])&&1>t[i].length)||("object"===typeof t[i]&&1>Object.keys(t[i]).length)';switch(t){case"true":e="false";break;case"false":e="true"}return e}function s(t){let e="",i=0;t:{const n=t.split("|"),l=n.length;for(let t=0;ta&&(0]*(?","gi");let i=1;for(;void 0===o[0]&&null!==(l=t.exec(e));)l[1]?i--:i++,0==i&&(o=[l.index,l[0].length])}let d=["",a[0],"","","","","",""];void 0!==o[0]&&(d[4]+=t(e.substr(0,o[0])),d[6]+=e.substr(o[0],o[1]));t:{if(f||void 0!==o[0]){if(void 0!==o[0]&&(e=e.substr(o[0]+o[1])),u["data-tdal-condition"]&&r.condition.exec(u["data-tdal-condition"][3])){let t=n(s(u["data-tdal-condition"][3]));if("false"==t)break t;"true"!=t&&(d[0]+="+(true==="+t+'?""',d[7]=':"")'+d[7])}let t;if(u["data-tdal-repeat"]&&(t=r.repeat.exec(u["data-tdal-repeat"][3]))){let e=s(t[2]);if("false"==e||'""'==e||"true"==e)break t;d[0]+="+(",d[0]+="false!==(t[i]="+e+")&&",d[0]+="(!Array.isArray(t[i])||(t[i]=Object.assign({},t[i])))&&",d[0]+='("object"===typeof t[i]&&0$/,""),!f||void 0!==o[0]||""==d[4]&&""==d[3]&&""==d[5]||(d[6]="",f=!1),i+=d[0]+"+"+JSON.stringify(String(d[1]))+d[2]+(""!=d[1]?'+"'+(f?"/":"")+'>"':"")+d[3]+d[4]+d[5]+"+"+JSON.stringify(String(d[6]))+d[7]}return i+="+"+JSON.stringify(String(e))}(t)).replace(/(?]+?)??\\s+data-tdal-(?:"+["condition","repeat","content","replace","attributes","omittag"].join("|")+")=(['\"])(.*?)\\3(\\s+[^<>]+?)??\\s*(/)?>","i"),tagWithAttribute:new RegExp("<((?:w+:)?w+)(s+[^<>]+?)??s+%s=(['\"])(.*?)\\3(s+[^<>]+?)??s*(/)?>","i"),tagAttributes:new RegExp("(?<=\\s)((?:[\\w-]+:)?[\\w-]+)=(?:(['\"])(.*?)\\2|([^>\\s'\"]+))","gi"),pathInString:new RegExp("{("+t+")}","g"),condition:new RegExp("^[\\s]*(\\!?)[\\s]*"+r+"[\\s]*$"),repeat:new RegExp("^[\\s]*([\\w-]+?)[\\s]+("+t+")[\\s]*$"),content:new RegExp("^[\\s]*(?:(text|structure)[\\s]+)?("+r+")[\\s]*$"),attributes:new RegExp("[\\s]*(?:(?:([\\w-]+?)[\\s]+"+r+"[\\s]*)(?:;[\\s]*|$))","g"),attributesTDAL:new RegExp("\\s*(data-tdal-[\\w-]+)=(?:(['\"])(.*?)\\2|([^>\\s'\"]+))","gi")};function n(e){let t="";switch(e){case"true":case"false":t=e;break;default:t=e.startsWith("!(")?"!"+e:"!("+s(e)+")"}return t}function s(e){let t='"undefined"===typeof (t[i++]='+e+')||false===t[--i]||null===t[i]||(Array.isArray(t[i])&&1>t[i].length)||("object"===typeof t[i]&&1>Object.keys(t[i]).length)';switch(e){case"true":t="false";break;case"false":t="true"}return t}function l(e){let t="",r=0;e:{const n=e.split("|"),f=n.length;for(let e=0;es&&(0]*(?","gi");let i=1;for(;void 0===o[0]&&null!==(s=t.exec(e));)s[1]?i--:i++,0==i&&(o=[s.index,s[0].length])}let g=["",r[0],"","","","","",""];void 0!==o[0]&&(g[4]+=f(e.substr(0,o[0])),g[6]+=e.substr(o[0],o[1]));e:{if(u||void 0!==o[0]){if(void 0!==o[0]&&(e=e.substr(o[0]+o[1])),a["data-tdal-condition"]&&i.condition.exec(a["data-tdal-condition"][3])){let e=n(l(a["data-tdal-condition"][3]));if("false"==e)break e;"true"!=e&&(g[0]+="+(true==="+e+'?""',g[7]=':"")'+g[7])}let t;if(a["data-tdal-repeat"]&&(t=i.repeat.exec(a["data-tdal-repeat"][3]))){let e=l(t[2]);if("false"==e||'""'==e||"true"==e)break e;g[0]+="+(",g[0]+="false!==(t[i]="+e+")&&",g[0]+="(!Array.isArray(t[i])||(t[i]=Object.assign({},t[i])))&&",g[0]+='("object"===typeof t[i]&&0$/,""),!u||void 0!==o[0]||""==g[4]&&""==g[3]&&""==g[5]||(g[6]="",u=!1),t+=g[0]+"+"+JSON.stringify(String(g[1]))+g[2]+(""!=g[1]?'+"'+(u?"/":"")+'>"':"")+g[3]+g[4]+g[5]+"+"+JSON.stringify(String(g[6]))+g[7]}return t+="+"+JSON.stringify(String(e)),t}e.Compile=function(e){let t=('let r={"REPEAT":{}},i=0,t=[]; return ""'+f(e)).replace(/(?