@uking/marmot
Advanced tools
Comparing version 1.1.9 to 1.2.0
@@ -1,10 +0,565 @@ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).marmot=t()}(this,(function(){"use strict";function e(e){return e&&e.__esModule&&Object.prototype.hasOwnProperty.call(e,"default")?e.default:e} | ||
/** | ||
(function (global, factory) { | ||
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : | ||
typeof define === 'function' && define.amd ? define(factory) : | ||
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.marmot = factory()); | ||
})(this, (function () { 'use strict'; | ||
function getDefaultExportFromCjs (x) { | ||
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x; | ||
} | ||
/** | ||
* [@uking/marmot]{@link https://github.com/An-uking/marmot.git} | ||
* | ||
* @namespace marmot | ||
* @version 1.1.9 | ||
* @version 1.2.0 | ||
* @author uking [ptvile@live.com] | ||
* @copyright uking 2024 | ||
* @license MIT | ||
*/const t="v-if",n="v-else-if",s="v-else",r="v-for",l={script:1,style:1,template:1,textarea:1,pre:1,code:1,noscript:1,noframes:1,iframe:1},i={area:1,base:1,br:1,col:1,embed:1,hr:1,img:1,input:1,link:1,meta:1,param:1,command:1,keygen:1,source:1,track:1,wbr:1,menuitem:1,frame:1},o=/\{\{(.*?)}}/g,a=["Template","Slot"],u=new Error("v-for must have source and value.");const c=(e,t,n,s,r)=>{let{s:l,v:i}=e;if(i.length)throw Error("not support dynamic value in template");return f(l[0].replace(/\s{2,}/g," ").replace(/[\r\t\n]/g,"").replace(/>(\s*)</g,"><").replace(/^\s+|\s+$/g,""),t,n,s,r)};function f(e,t,n,s,r){let o="";r=r||{},s=s||{},t=t||{},n=n||{};let a=/<([A-Za-z!][A-Za-z0-9-]*)/g,c=a.exec(e);if(c){let h=null;for(;c;){if(c.index>0){let l=e.substring(0,c.index).replace(/^\s+|\s+$/g,"");l&&(o+=f(l,t,n,s,r)),e=e.substring(c.index),a.lastIndex=0,c=a.exec(e);continue}let O=c[1],y=-1,A="",I="",W=!1,k=!1;if(O.startsWith("!--")){A="--\x3e",y=e.indexOf(A)+A.length,o+=e.substring(0,y),e=e.substring(y),a.lastIndex=0,c=a.exec(e);continue}"!DOCTYPE"===O?(k=!1,A=">",y=e.indexOf(A)+A.length,I=e.substring(0,y),W=!0):(y=v(e),k=/^[A-Z]/.test(O),I=e.substring(0,y),W=!!i[O]||I.endsWith("/>"),A=W?"/>":">");let C=$(I),S=x(C.if,h,n,s);if(h=S.ifCondition,S.condition){if(W)if("Slot"===O){let e=C.attrs.name?C.attrs.name.value:"default";o+=r[e]||"",r[e]=""}else if(C.for){let e=g(C.for);if(!e.sourceExp||!e.valueExp)throw u;let l=n[e.sourceExp]||d(e.sourceExp);for(let i in l){let a={};e.indexExp&&(a[e.indexExp]=m(i)),a[e.valueExp]=l[i];let u=j(C.attrs,a,s);a=Object.assign(a,u),o+=k?b(O,t,a,s,r):w(O,C.attrs,A,Object.assign(n,a),s)}}else if(k){let e=j(C.attrs,n,s);o+=b(O,t,Object.assign({},n,e),s,r)}else o+=w(O,C.attrs,A,n,s);else{let i="Template"===O,a=`</${O}>`,c=E(e.substring(y),O);if(-1===c)throw new Error(`End tag ${a} not found.`);c+=y;let h=e.substring(y,c);if(k){let e=C.attrs.slot?C.attrs.slot.value:"default",l="";if(C.for){let e=g(C.for);if(!e.sourceExp||!e.valueExp)throw u;let i=n[e.sourceExp]||d(e.sourceExp);for(let o in i){let a={};e.indexExp&&(a[e.indexExp]=m(o)),a[e.valueExp]=i[o];let u=j(C.attrs,a,s);a=Object.assign(n,a,u),l+=f(h,t,a,s,r)}}else l=f(h,t,n,s,r);if(r[e]=l,!i){let e=j(C.attrs,n,s);o+=b(O,t,Object.assign({},n,e),s,r)}}else if(1===l[O])o+=w(O,C.attrs,A,n,s),o+=p(h,n,s),o+=`</${O}>`;else if(C.for){let e=g(C.for);if(!e.sourceExp||!e.valueExp)throw u;let l=n[e.sourceExp]||d(e.sourceExp);for(let n in l){let i={};e.indexExp&&(i[e.indexExp]=m(n)),i[e.valueExp]=l[n];let a=j(C.attrs,i,s);i=Object.assign(i,a),o+=w(O,C.attrs,A,i,s),o+=f(h,t,i,s,r),o+=`</${O}>`}}else o+=w(O,C.attrs,A,n,s),o+=f(h,t,n,s,r),o+=`</${O}>`;y=c+a.length}e=e.substring(y),a.lastIndex=0,c=a.exec(e),!c&&e&&(o+=f(e,t,n,s,r))}else e=e.substring(y),a.lastIndex=0,c=a.exec(e)}}else o+=p(e,n,s);return o}function p(e,t,n){return e.replace(o,((e,s)=>{try{return h(s,t,n)}catch(t){return e}}))}function h(e,t,n){let s={__c:n};s=Object.assign(t,s);let r=Object.keys(s),l=Object.values(s);return new Function(...r,`return ${e}`)(...l)}function x(e,s,r,l){let i=!0;if(e)if(e.name===t)i=s=h(e.value,r,l);else if(e.name===n){if(null===s)throw new Error("previous node missing v-if");i=s?!s:s=h(e.value,r,l)}else{if(null===s)throw new Error("previous node missing v-if or v-else-if");i=!s}else s=null;return{ifCondition:s,condition:i}}function g(e){let t=/^\(|\)$/g,n=/,([^,}\]]*)(?:,([^,}\]]*))?$/,s=e.match(/([\s\S]*?)\s+(?:in|of)\s+([\s\S]*)/),r=null,l=null,i=null;if(s){r=s[2].trim(),l=s[1].trim().replace(t,"").trim();let e=l.match(n);e&&(l=l.replace(n,"").trim(),i=e[1].trim())}return{sourceExp:r,valueExp:l,indexExp:i}}function d(e){let t=parseInt(e);return isNaN(t)||t<1?[]:Array.from({length:t},((e,t)=>t+1))}function m(e){let t=parseInt(e);return isNaN(t)?e:t}function b(e,t,n,s,r){let l=t[e];if(!l)throw new Error(`Component ${e} not found.`);let i=new l(s),o=Object.assign({},i.data,n);return c(i.render(),i.components(),o,i.context,r)}function E(e,t){let n=new RegExp(`<${t}(?![A-Za-z0-9-])`,"g"),s=new RegExp(`</${t}>`,"g"),r=new RegExp(`</${t}>`,"g"),l=s.exec(e);if(!l)return-1;for(;l;){let t=e.slice(0,l.index),i=t.match(n),o=t.match(r);if(!i&&(i=[]),!o&&(o=[]),i.length===o.length)return l.index;l=s.exec(e)}return-1}function v(e){let t=-1,n=/(\/)?>/g,s=/(")/g,r=/(')/g,l=n.exec(e);for(;l;){let i=e.substring(0,l.index+l[0].length),o=i.match(s),a=i.match(r);if(!o&&(o=[]),!a&&(a=[]),o.length%2==0&&a.length%2==0){t=l.index+l[0].length;break}l=n.exec(e)}if(-1===t)throw new Error("Invalid HTML");return t}function $(e){let t={hasExp:!1,if:null,for:null,attrs:{}},n=e.match(/([^\s"'<>\/=]+)(?:\s*(=)\s*(?:"([^"]*)"|'([^']*)'|([^\s"'=<>`]+)))?/g);for(let e=1;e<n.length;e++){let l=n[e],i=[],o=l.indexOf("=");-1===o?i.push(l):(i.push(l.substring(0,o)),i.push(l.substring(o+1)));let a=i[0],u=a===s?null:!(i.length>1)||i[1].slice(1,-1);a.startsWith(":")?(!t.hasExp&&(t.hasExp=!0),t.attrs[a]={value:u,isExp:!0}):a.startsWith("v-")?a===r?t.for=u:t.if={name:a,value:u}:t.attrs[a]={value:u,isExp:!1}}return t}function w(e,t,n,s,r){let l=[],i=[];l.push(`<${e}`);for(let e in t)if(e.includes("class"))if(e.startsWith(":")){let n=t[e].value,l="class"===n?"":h(n,s,r),o=Object.prototype.toString.call(l);if("[object Object]"===o)for(let e in l)l[e]&&i.push(e);else if("[object Array]"===o)i.push(...l);else{let e=l.replace(/^\s+|\s+$/g,"").split(/\s+/g);i.push(...e)}}else{let n=t[e].value.replace(/^\s+|\s+$/g,"").split(/\s+/g);i.push(...n)}else if(e.startsWith(":")){let n=t[e].value;e=e.slice(1);let i=h(n,s,r),o=Object.prototype.toString.call(i);if("[object Boolean]"===o)i&&l.push(e);else if("[object Array]"===o)l.push(...i.map((t=>`${e}="${t}"`)));else if("[object Object]"===o)for(let t in i)i[t]&&l.push(`${e}="${t}"`);else l.push(`${e}="${i}"`)}else{let n=t[e].value;"boolean"===typeof n?n&&l.push(e):l.push(`${e}="${n}"`)}return i.length&&l.push(`class="${i.join(" ")}"`),l.join(" ")+n}function j(e,t,n){let s={};for(let r in e){let l=e[r].value;r.startsWith(":")?s[r.slice(1)]=h(l,t,n):s[r]=l}return s}return e({render:c,Component:class{constructor(e={}){this.context=e||{};let t=this.constructor.name;if(a.includes(t))throw new Error(`Component ${t} is a reserved keyword.`);this.data=this.data()||{},this.data=Object.assign(this.data,this.functions()||{})}functions(){}data(){}components(){}render(){}},html:(e,...t)=>({s:e,v:t})})})); | ||
*/ | ||
var src; | ||
var hasRequiredSrc; | ||
function requireSrc () { | ||
if (hasRequiredSrc) return src; | ||
hasRequiredSrc = 1; | ||
const ExpType = { | ||
V_IF: 'v-if', | ||
V_ELSE_IF: 'v-else-if', | ||
V_ELSE: 'v-else', | ||
V_FOR: 'v-for', | ||
}; | ||
const notRenderTag = { | ||
script: 1, | ||
style: 1, | ||
template: 1, | ||
textarea: 1, | ||
pre: 1, | ||
code: 1, | ||
noscript: 1, | ||
noframes: 1, | ||
iframe: 1, | ||
}; | ||
const selfClosingTag = { | ||
area: 1, | ||
base: 1, | ||
br: 1, | ||
col: 1, | ||
embed: 1, | ||
hr: 1, | ||
img: 1, | ||
input: 1, | ||
link: 1, | ||
meta: 1, | ||
param: 1, | ||
command: 1, | ||
keygen: 1, | ||
source: 1, | ||
track: 1, | ||
wbr: 1, | ||
menuitem: 1, | ||
frame: 1 | ||
}; | ||
const varReg = /\{\{(.*?)}}/g; | ||
const reservedKeys = ['Template', 'Slot']; | ||
// const htmlTagRegex = /<\/?\w+(\s+("[^"]*"|'[^']*'|[^>])+)?>/g; | ||
// const attributeRegex = /([^\s"'<>\/=]+)(?:\s*(=)\s*(?:"([^"]*)"|'([^']*)'|([^\s"'=<>`]+)))?/g; | ||
const err = new Error(`v-for must have source and value.`); | ||
class Component { | ||
constructor(context = {}) { | ||
this.context = context || {}; | ||
let name = this.constructor.name; | ||
if (reservedKeys.includes(name)) { | ||
throw new Error(`Component ${name} is a reserved keyword.`) | ||
} | ||
this.data = this.data() || {}; | ||
this.data = Object.assign(this.data, this.functions() || {}); | ||
} | ||
functions() { | ||
} | ||
data() { | ||
} | ||
components() { | ||
} | ||
render() { | ||
} | ||
} | ||
const html = (s, ...v) => ({s, v}); | ||
const render = (node, components, data, context, slots) => { | ||
let {s, v} = node; | ||
if (v.length) throw Error(`not support dynamic value in template`) | ||
let str = s[0].replace(/\s{2,}/g, " ") | ||
.replace(/[\r\t\n]/g, "") | ||
.replace(/>(\s*)</g, "><") | ||
.replace(/^\s+|\s+$/g, ''); | ||
return _renderToString(str, components, data, context, slots) | ||
}; | ||
function _renderToString(strings, components, data, context, slots) { | ||
let html = ''; | ||
slots = slots || {}; | ||
context = context || {}; | ||
components = components || {}; | ||
data = data || {}; | ||
let tagReg = /<([A-Za-z!][A-Za-z0-9-]*)/g; | ||
// let componentReg = /<([A-Z]\w*)/g | ||
let match = tagReg.exec(strings); | ||
if (match) { | ||
let ifCondition = null; | ||
while (match) { | ||
if (match.index > 0) { | ||
let text = strings.substring(0, match.index).replace(/^\s+|\s+$/g, ''); | ||
text&&(html += _renderToString(text, components, data, context, slots)); | ||
// if(text){ | ||
// html += _renderToString(text, components, data, context, slots) | ||
// } | ||
strings = strings.substring(match.index); | ||
tagReg.lastIndex = 0; | ||
match = tagReg.exec(strings); | ||
continue | ||
} | ||
// let node = {name: match[1], endTagMarkPosition: -1} | ||
let nodeName = match[1]; | ||
let nodeTagEndMarkPosition = -1; | ||
let nodeTagEndMark = ''; | ||
let nodeRaw = ''; | ||
let isCloseTag = false; | ||
let isComponent = false; | ||
if (nodeName.startsWith("!--")) { | ||
// node.type = NodeType.COMMENT_NODE | ||
nodeTagEndMark = '-->'; | ||
nodeTagEndMarkPosition = strings.indexOf(nodeTagEndMark) + nodeTagEndMark.length; | ||
// nodeRaw = strings.substring(0, nodeTagEndMarkPosition) | ||
// isCloseTag = true | ||
html += strings.substring(0, nodeTagEndMarkPosition); | ||
strings = strings.substring(nodeTagEndMarkPosition); | ||
tagReg.lastIndex = 0; | ||
match = tagReg.exec(strings); | ||
continue | ||
} else if (nodeName === '!DOCTYPE') { | ||
// node.type = NodeType.DOCUMENT_TYPE_NODE | ||
isComponent = false; | ||
nodeTagEndMark = '>'; | ||
nodeTagEndMarkPosition = strings.indexOf(nodeTagEndMark) + nodeTagEndMark.length; | ||
nodeRaw = strings.substring(0, nodeTagEndMarkPosition); | ||
isCloseTag = true; | ||
} else { | ||
nodeTagEndMarkPosition = findBeginTagMarkPosition(strings); | ||
isComponent = /^[A-Z]/.test(nodeName); | ||
nodeRaw = strings.substring(0, nodeTagEndMarkPosition); | ||
isCloseTag = selfClosingTag[nodeName] ? true : nodeRaw.endsWith('/>'); | ||
nodeTagEndMark = isCloseTag ? '/>' : '>'; | ||
} | ||
let prop = findHtmlAttributes(nodeRaw); | ||
// node = Object.assign(node, prop) | ||
let vif = buildIfCondition(prop.if, ifCondition, data, context); | ||
ifCondition = vif.ifCondition; | ||
if (!vif.condition) { | ||
strings = strings.substring(nodeTagEndMarkPosition); | ||
tagReg.lastIndex = 0; | ||
match = tagReg.exec(strings); | ||
continue | ||
} | ||
if (isCloseTag) { | ||
if (nodeName === 'Slot') { | ||
let slotName = prop.attrs.name ? prop.attrs.name.value : 'default'; | ||
html += slots[slotName] || ''; | ||
slots[slotName] = ''; | ||
} else { | ||
if (prop.for) { | ||
let vFor = buildForCondition(prop.for); | ||
if (!vFor.sourceExp || !vFor.valueExp) throw err | ||
let d = data[vFor.sourceExp] || buildRange(vFor.sourceExp); | ||
for (let key in d) { | ||
let obj = {}; | ||
vFor.indexExp && (obj[vFor.indexExp] = transformIndex(key)); | ||
obj[vFor.valueExp] = d[key]; | ||
let attrs = buildAttributes(prop.attrs, obj, context); | ||
obj = Object.assign(obj, attrs); | ||
if (isComponent) { | ||
html += _renderComponent(nodeName, components, obj, context, slots); | ||
} else { | ||
html += buildTagByProp(nodeName, prop.attrs, nodeTagEndMark, Object.assign(data,obj), context); | ||
} | ||
} | ||
} else { | ||
if (isComponent) { | ||
let attrs = buildAttributes(prop.attrs, data, context); | ||
let obj = Object.assign({}, data, attrs); | ||
html += _renderComponent(nodeName, components, obj, context, slots); | ||
} else { | ||
html += buildTagByProp(nodeName, prop.attrs, nodeTagEndMark, data, context); | ||
} | ||
} | ||
} | ||
} else { | ||
let isTemplate = nodeName === 'Template'; | ||
let endTag = `</${nodeName}>`; | ||
let endIndex = findNodeEndTagPosition(strings.substring(nodeTagEndMarkPosition), nodeName); | ||
if (endIndex === -1) { | ||
throw new Error(`End tag ${endTag} not found.`) | ||
} | ||
endIndex += nodeTagEndMarkPosition; | ||
let tmp = strings.substring(nodeTagEndMarkPosition, endIndex); | ||
if (isComponent) { | ||
let slotName = prop.attrs.slot ? prop.attrs.slot.value : 'default'; | ||
let tmpHtml = ''; | ||
if (prop.for) { | ||
let vFor = buildForCondition(prop.for); | ||
if (!vFor.sourceExp || !vFor.valueExp) throw err | ||
let d = data[vFor.sourceExp] || buildRange(vFor.sourceExp); | ||
for (let key in d) { | ||
let obj = {}; | ||
vFor.indexExp && (obj[vFor.indexExp] = transformIndex(key)); | ||
obj[vFor.valueExp] = d[key]; | ||
let attrs = buildAttributes(prop.attrs, obj, context); | ||
obj = Object.assign(data,obj, attrs); | ||
tmpHtml += _renderToString(tmp, components, obj, context, slots); | ||
} | ||
} else { | ||
tmpHtml = _renderToString(tmp, components, data, context, slots); | ||
} | ||
slots[slotName] = tmpHtml; | ||
if (!isTemplate) { //如果是组件 | ||
let attrs = buildAttributes(prop.attrs, data, context); | ||
let obj = Object.assign({}, data, attrs); | ||
html += _renderComponent(nodeName, components, obj, context, slots); | ||
} | ||
} else { | ||
if(notRenderTag[nodeName] === 1){ | ||
html += buildTagByProp(nodeName, prop.attrs, nodeTagEndMark, data, context); | ||
html += tryEvaluateExp(tmp, data, context); | ||
html += `</${nodeName}>`; | ||
}else { | ||
if (prop.for) { | ||
let vFor = buildForCondition(prop.for); | ||
if (!vFor.sourceExp || !vFor.valueExp) throw err | ||
let d = data[vFor.sourceExp] || buildRange(vFor.sourceExp); | ||
for (let key in d) { | ||
let obj = {}; | ||
vFor.indexExp && (obj[vFor.indexExp] = transformIndex(key)); | ||
obj[vFor.valueExp] = d[key]; | ||
let attrs = buildAttributes(prop.attrs, obj, context); | ||
obj = Object.assign(obj, attrs); | ||
html += buildTagByProp(nodeName, prop.attrs, nodeTagEndMark, obj, context); | ||
html += _renderToString(tmp, components, obj, context, slots); | ||
html += `</${nodeName}>`; | ||
} | ||
} else { | ||
html += buildTagByProp(nodeName, prop.attrs, nodeTagEndMark, data, context); | ||
html += _renderToString(tmp, components, data, context, slots); | ||
html += `</${nodeName}>`; | ||
} | ||
} | ||
} | ||
nodeTagEndMarkPosition = endIndex + endTag.length; | ||
// strings = strings.substring(node.endTagMarkPosition) | ||
} | ||
strings = strings.substring(nodeTagEndMarkPosition); | ||
tagReg.lastIndex = 0; | ||
// componentReg.lastIndex = 0 | ||
match = tagReg.exec(strings); | ||
if (!match && strings) { | ||
html += _renderToString(strings, components, data, context, slots); | ||
} | ||
} | ||
} else { | ||
// let reg = /\{\{(.*?)}}/g; | ||
html += tryEvaluateExp(strings, data, context); | ||
} | ||
return html | ||
} | ||
/** | ||
* try to evaluate expression, if error return original string | ||
* @param str | ||
* @param data | ||
* @param context | ||
* @returns {*} | ||
*/ | ||
function tryEvaluateExp(str,data,context){ | ||
return str.replace(varReg, (match, p1) => { | ||
try { | ||
return evaluateExp(p1, data, context) | ||
}catch (e){ | ||
//console.error("rename class to className") | ||
return match | ||
} | ||
}); | ||
} | ||
function evaluateExp(exp, data, context) { | ||
// data._c = context | ||
let obj = {__c: context}; | ||
obj = Object.assign(data,obj); | ||
//console.log(obj) | ||
let names = Object.keys(obj); | ||
let vals = Object.values(obj); | ||
// console.log(exp) | ||
return new Function(...names, `return ${exp}`)(...vals) | ||
} | ||
function buildIfCondition(vExp, ifCondition, data, context) { | ||
let condition = true; | ||
if (vExp) { | ||
if (vExp.name === ExpType.V_IF) { | ||
ifCondition = evaluateExp(vExp.value, data, context); | ||
condition = ifCondition; | ||
} else if (vExp.name === ExpType.V_ELSE_IF) { | ||
if (ifCondition === null) { | ||
throw new Error(`previous node missing v-if`) | ||
} | ||
if (ifCondition) { | ||
condition = !ifCondition; | ||
} else { | ||
ifCondition = evaluateExp(vExp.value, data, context); | ||
condition = ifCondition; | ||
} | ||
} else { | ||
if (ifCondition === null) { | ||
throw new Error(`previous node missing v-if or v-else-if`) | ||
} | ||
condition = !ifCondition; | ||
} | ||
} else { | ||
ifCondition = null; | ||
} | ||
return {ifCondition, condition} | ||
} | ||
function buildForCondition(vExp) { | ||
let exp = vExp; | ||
let forAliasRE = /([\s\S]*?)\s+(?:in|of)\s+([\s\S]*)/; | ||
let stripParentRE = /^\(|\)$/g; | ||
let forIteratorRE = /,([^,}\]]*)(?:,([^,}\]]*))?$/; | ||
let match = exp.match(forAliasRE); | ||
let sourceExp = null; | ||
let valueExp = null; | ||
let indexExp = null; | ||
if (match) { | ||
sourceExp = match[2].trim(); // 获取`(item, index) in value`中`value` | ||
valueExp = match[1].trim().replace(stripParentRE, '').trim(); | ||
let indexMatch = valueExp.match(forIteratorRE); | ||
if (indexMatch) { | ||
valueExp = valueExp.replace(forIteratorRE, '').trim(); // 获取`item, index`中的item | ||
indexExp = indexMatch[1].trim(); | ||
} | ||
} | ||
return {sourceExp, valueExp, indexExp} | ||
} | ||
function buildRange(end) { | ||
let x = parseInt(end); | ||
if (isNaN(x) || x < 1) { | ||
return [] | ||
} | ||
return Array.from({length: x}, (_, index) => index + 1) | ||
} | ||
function transformIndex(key) { | ||
let x = parseInt(key); | ||
if (isNaN(x)) { | ||
return key | ||
} | ||
return x | ||
} | ||
function _renderComponent(name, components, props, context, slots) { | ||
let MyComponent = components[name]; | ||
if (!MyComponent) throw new Error(`Component ${name} not found.`) | ||
let myComponent = new MyComponent(context); | ||
let obj = Object.assign({}, myComponent.data,props); | ||
return render(myComponent.render(), myComponent.components(), obj, myComponent.context, slots) | ||
} | ||
function findNodeEndTagPosition(html, tag) { | ||
let beginTagReg = new RegExp(`<${tag}(?![A-Za-z0-9-])`, 'g'); | ||
let endTagReg = new RegExp(`</${tag}>`, 'g'); | ||
let endTagReg2 = new RegExp(`</${tag}>`, 'g'); | ||
let match = endTagReg.exec(html); | ||
if (!match) return -1 | ||
while (match) { | ||
let str = html.slice(0, match.index); | ||
let beginMatch = str.match(beginTagReg); | ||
let endMatch = str.match(endTagReg2); | ||
!beginMatch && (beginMatch = []); | ||
!endMatch && (endMatch = []); | ||
if (beginMatch.length === endMatch.length) { | ||
return match.index | ||
} | ||
match = endTagReg.exec(html); | ||
} | ||
return -1 | ||
} | ||
function findBeginTagMarkPosition(html) { | ||
let position = -1; | ||
let endTag = /(\/)?>/g; | ||
let doubleQuote = /(")/g; | ||
let singleQuote = /(')/g; | ||
let matches = endTag.exec(html); | ||
while (matches) { | ||
let str = html.substring(0, matches.index + matches[0].length); | ||
let double = str.match(doubleQuote); | ||
let single = str.match(singleQuote); | ||
!double && (double = []); | ||
!single && (single = []); | ||
if (double.length % 2 === 0 && single.length % 2 === 0) { | ||
position = matches.index + matches[0].length; | ||
break | ||
} | ||
matches = endTag.exec(html); | ||
} | ||
if (position === -1) { | ||
throw new Error("Invalid HTML") | ||
} | ||
return position | ||
} | ||
function findHtmlAttributes(node) { | ||
let prop = { | ||
hasExp: false, | ||
// hasIf: false, | ||
if: null, | ||
// hasFor: false, | ||
for: null, | ||
attrs: {}, | ||
// endTagMark: '>', | ||
}; | ||
// node.endsWith('/>') && (prop.endTagMark = '/>') | ||
let attributeRegex = /([^\s"'<>\/=]+)(?:\s*(=)\s*(?:"([^"]*)"|'([^']*)'|([^\s"'=<>`]+)))?/g; | ||
let matches = node.match(attributeRegex); | ||
// prop.name = matches[0] | ||
for (let i = 1; i < matches.length; i++) { | ||
let str = matches[i]; | ||
let arr = []; | ||
let index = str.indexOf('='); | ||
if (index === -1) { | ||
arr.push(str); | ||
} else { | ||
arr.push(str.substring(0, index)); | ||
arr.push(str.substring(index + 1)); | ||
} | ||
// let arr = str.split('=') | ||
let name = arr[0]; | ||
let value = name === ExpType.V_ELSE ? null : arr.length > 1 ? arr[1].slice(1, -1) : true; | ||
if (name.startsWith(':')) { | ||
!prop.hasExp && (prop.hasExp = true); | ||
prop.attrs[name] = {value, isExp: true}; | ||
} else if (name.startsWith('v-')) { | ||
if (name === ExpType.V_FOR) { | ||
prop.for = value; | ||
} else { | ||
prop.if = {name, value}; | ||
} | ||
} else { | ||
prop.attrs[name] = {value, isExp: false}; | ||
} | ||
} | ||
return prop | ||
} | ||
function buildTagByProp(name, attrs, endTagMark, data, context) { | ||
let arr = []; | ||
let classArr = []; | ||
arr.push(`<${name}`); | ||
for (let key in attrs) { | ||
if (key.includes('class')) { | ||
if (key.startsWith(":")) { | ||
let val = attrs[key].value; | ||
// if(val==='class') val = `''` | ||
let res = val === 'class' ? '' : evaluateExp(val, data, context); | ||
let type = Object.prototype.toString.call(res); | ||
// let t = Object.prototype.toString.call(res) | ||
// console.log(t) | ||
if (type === '[object Object]') { | ||
for (let k in res) { | ||
res[k] && (classArr.push(k)); | ||
} | ||
} else if( type === '[object Array]' ){ //array | ||
classArr.push(...res); | ||
} else { | ||
let tmp = res.replace(/^\s+|\s+$/g, '').split(/\s+/g); | ||
classArr.push(...tmp); | ||
} | ||
} else { | ||
let val = attrs[key].value; | ||
let arr = val.replace(/^\s+|\s+$/g, '').split(/\s+/g); | ||
classArr.push(...arr); | ||
} | ||
} else { | ||
if (key.startsWith(":")) { | ||
let val = attrs[key].value; | ||
key = key.slice(1); | ||
let res = evaluateExp(val, data, context); | ||
let type = Object.prototype.toString.call(res); | ||
if (type === '[object Boolean]') { | ||
res && arr.push(key); | ||
} else if( type === '[object Array]'){ | ||
arr.push(...res.map(v=> `${key}="${v}"`)); | ||
} else if( type === '[object Object]' ){ | ||
for (let k in res) { | ||
res[k] && (arr.push(`${key}="${k}"`)); | ||
} | ||
} else { | ||
arr.push(`${key}="${res}"`); | ||
} | ||
} else { | ||
let val = attrs[key].value; | ||
let type = typeof val; | ||
if (type === 'boolean') { | ||
val && arr.push(key); | ||
} else { | ||
arr.push(`${key}="${val}"`); | ||
} | ||
} | ||
} | ||
} | ||
if (classArr.length) { | ||
arr.push(`class="${classArr.join(' ')}"`); | ||
} | ||
return arr.join(' ') + endTagMark | ||
} | ||
function buildAttributes(attrs, data, context) { | ||
let obj = {}; | ||
for (let key in attrs) { | ||
let val = attrs[key].value; | ||
if (key.startsWith(':')) { | ||
obj[key.slice(1)] = evaluateExp(val, data, context); | ||
} else { | ||
obj[key] = val; | ||
} | ||
} | ||
return obj | ||
} | ||
src = { | ||
render, | ||
Component, | ||
html | ||
}; | ||
return src; | ||
} | ||
var srcExports = requireSrc(); | ||
var index = /*@__PURE__*/getDefaultExportFromCjs(srcExports); | ||
return index; | ||
})); |
{ | ||
"name": "@uking/marmot", | ||
"version": "1.1.9", | ||
"version": "1.2.0", | ||
"description": "A lightweight and fast javascript native template rendering engine that supports browsers, FibJS and NodeJS.", | ||
@@ -24,6 +24,6 @@ "repository": "https://github.com/An-uking/marmot.git", | ||
"devDependencies": { | ||
"@rollup/plugin-commonjs": "^25.0.7", | ||
"@rollup/plugin-commonjs": "^28.0.0", | ||
"@rollup/plugin-terser": "^0.4.4", | ||
"rollup": "^4.17.2", | ||
"terser": "^5.31.0" | ||
"rollup": "^4.24.0", | ||
"terser": "^5.34.1" | ||
}, | ||
@@ -30,0 +30,0 @@ "author": "uking <<ptvile@live.com>>", |
@@ -14,4 +14,4 @@ | ||
}, | ||
plugins: [commonjs(), terser()], | ||
plugins: [commonjs()], | ||
} | ||
]; |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
26035
5
543