solely 0.4.16 → 0.4.17
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var
|
|
1
|
+
"use strict";var t=Object.defineProperty,e=(e,s,n)=>((e,s,n)=>s in e?t(e,s,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[s]=n)(e,"symbol"!=typeof s?s+"":s,n);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const s=require("../../types/ast.cjs"),n=require("../../shared/templateError.cjs"),o=require("../../shared/env.cjs"),r=require("./html-decode.cjs"),i=Symbol("solely.irCtx"),a=Symbol("solely.irEvents"),c=Symbol("solely.prevClassObj"),l=Symbol("solely.prevStyleObj"),h={xml:"http://www.w3.org/XML/1998/namespace",xlink:"http://www.w3.org/1999/xlink",xmlns:"http://www.w3.org/2000/xmlns/"},p={readonly:"readOnly",for:"htmlFor",tabindex:"tabIndex",contenteditable:"contentEditable",maxlength:"maxLength"},d=new Set(["data-","aria-","role","form"]),u=new Set(["value","selectedIndex"]);function f(t,e){var s;const n=null==(s=e.a)?void 0:s.find(t=>"class"===t.k&&!t.d);(null==n?void 0:n.v)&&t.setAttribute("class",n.v)}function m(t,e){var s;if(!t.style)return;const n=null==(s=e.a)?void 0:s.find(t=>"style"===t.k&&!t.d),o=n?S(n.v):{};for(const r in o){const e=o[r];r.startsWith("--")?t.style.setProperty(r,e):t.style[r]=e}}const y=t=>!(!t||0===t.length||/\s/.test(t)&&(o.IS_DEV&&console.warn(`[Solely] Invalid class name "${t}" contains whitespace, will be ignored`),1));function S(t){const e={},s=[[t]];for(;s.length;){const[t,n]=s.pop()||[];if(t){if("string"==typeof t){const s=/([^:;]+)\s*:\s*([^;]*\(.*?\)[^;]*|[^;]+)/g;let n;for(;null!==(n=s.exec(t));){const t=n[1].trim(),s=n[2].trim();t&&s&&(e[t]=s)}continue}if(Array.isArray(t))for(let e=t.length-1;e>=0;e--)s.push([t[e]]);else if("object"==typeof t)for(const o in t){const r=t[o];if(null==r)continue;const i=n?`${n}-${o}`:o;"object"!=typeof r||Array.isArray(r)?e[i]=String(r):s.push([r,i])}}}return e}function v(t,e,s){const n=null==s?"":String(s),o=e.indexOf(":");if(o>0){const s=e.slice(0,o),r=h[s];if(r)return void(t.getAttributeNS(r,e)!==n&&t.setAttributeNS(r,e,n))}t.getAttribute(e)!==n&&t.setAttribute(e,n)}function b(t,e,s,n){const o=p[e]||e;if(e.startsWith("data-")||e.startsWith("aria-")||d.has(e))return void v(t,e,s);const r=t,i=r.tagName;if(null!=s){if("src"===o||"href"===o){const e=String(s);if(r[o]===e||t.getAttribute(o)===e)return}if("value"!==o||"INPUT"!==i&&"TEXTAREA"!==i||r.value!==s)if("boolean"!=typeof s)(n??(t instanceof SVGElement||void 0!==r.ownerSVGElement))&&!p[e]?v(t,e,s):r[o]!==s&&(r[o]=s);else{if(r[o]===s)return;r[o]=s}}else if("boolean"==typeof r[o])!1!==r[o]&&(r[o]=!1);else{const s=r[o];""!==s&&void 0!==s&&(r[o]=""),t.hasAttribute(e)&&t.removeAttribute(e)}}class g{constructor(t,s,n={}){e(this,"nodeMap",new Map),e(this,"prevNodeMap",new Map),e(this,"pathStack",[]),e(this,"staticSubtreeCache",new Map),this.ir=t,this.container=s,this.scope=n}evalIR(t,e,s){var r;const i=this.ir.fns[t-1];if(o.IS_DEV)try{return i?i.apply(this.scope,e):""}catch(a){return n.showTemplateError(a.message||a.toString(),(null==(r=this.ir.m)?void 0:r.src)||"",s,this.scope.tagName),""}return i?i.apply(this.scope,e):""}createElement(t,e){const s=t.g??"";return e instanceof SVGElement||"svg"===s?document.createElementNS("http://www.w3.org/2000/svg",s):document.createElement(s)}createText(t,e){const s=t.d?this.evalIR(t.f??-1,[e],t.__m):r.default(t.x||"");return document.createTextNode(s)}createComment(t,e){const s=t.d?this.evalIR(t.f??-1,[e],t.__m):t.x||"";return document.createComment(s)}applyAttrs(t,e,s,n=!1){const o=[],r=t,h=t instanceof SVGElement||void 0!==r.ownerSVGElement;let p;r[i]||(r[i]={loops:null}),r[i].loops=s,r[a]||(r[a]=Object.create(null));let d,f=!1,m=!1;for(const c of e){const{k:e,f:l,d:y,v:S}=c;if(":class"===e){p=this.evalIR(l??-1,[s],c.__m),f=!0;continue}if(":style"===e){d=this.evalIR(l??-1,[s],c.__m),m=!0;continue}if("class"===e||"style"===e)continue;const g=e[0];if("@"===g){const s=e.slice(1),o=c.r??"user";let h=r[a][s];if(h||(h=r[a][s]={model:[],user:[]},t.addEventListener(s,t=>{for(const e of h.model)e(t);for(const e of h.user)e(t)})),!n){const t=t=>this.evalIR(l??-1,[t,r[i].loops],c.__m);h[o].push(t)}continue}if("mounted"===e){n||requestAnimationFrame(()=>this.evalIR(l??-1,[t,s],c.__m));continue}if("updated"===e){n&&requestAnimationFrame(()=>this.evalIR(l??-1,[t,s],c.__m));continue}if("ref"===e&&S){this.scope.$refs=this.scope.$refs||{},this.scope.$refs[S]=t;continue}const N=y?this.evalIR(l??-1,[s],c.__m):S??"";if(":"===g){const s=e.slice(1);"SELECT"===t.tagName&&u.has(s)?o.push(()=>b(t,s,N,h)):b(t,s,N,h)}else n||v(t,e,String(N))}return f&&((t,e)=>{const s=t,n=s[c],o=function(t){const e={},s=[t];for(;s.length;){const t=s.pop();if(t)if("string"==typeof t){const s=t.split(" ");for(let t=0;t<s.length;t++){const n=s[t].trim();n&&(e[n]=!0)}}else if(Array.isArray(t))for(let e=t.length-1;e>=0;e--)s.push(t[e]);else if("object"==typeof t)for(const s in t)t[s]&&(e[s]=!0)}return e}(e),r={};for(const a in o)y(a)&&(r[a]=o[a]);if(!n){for(const e in r)t.classList.add(e);return void(s[c]=r)}let i=!1;for(const a in n)r[a]||(t.classList.remove(a),i=!0);for(const a in r)n[a]||(t.classList.add(a),i=!0);i&&(s[c]=r)})(t,p),m&&((t,e)=>{const s=t,n=S(e),o=s[l];if(!o){for(const e in n){const s=n[e];e.startsWith("--")?t.style.setProperty(e,s):t.style[e]=s}return void(s[l]=n)}let r=!1;for(const i in o)i in n||(i.startsWith("--")?t.style.removeProperty(i):t.style[i]="",r=!0);for(const i in n){const e=n[i];e!==o[i]&&(i.startsWith("--")?t.style.setProperty(i,e):t.style[i]=e,r=!0)}r&&(s[l]=n)})(t,d),o}updateNode(t,e,s){const n=e.d?this.evalIR(e.f??-1,[s],e.__m):e.x||"";e.d&&t.textContent!==n&&(t.textContent=n)}createStaticSubtree(t,e,n){var o;switch(t.t){case s.ASTType.Element:{const s=this.createElement(t,e);f(s,t),m(s,t);const r=this.applyAttrs(s,t.a??[],n);return null==(o=t.c)||o.forEach(t=>{const e=this.createStaticSubtree(t,s,n);s.appendChild(e)}),r.forEach(t=>t()),s}case s.ASTType.Text:return this.createText(t,n);case s.ASTType.Comment:return this.createComment(t,n);default:return document.createComment("unknown")}}handleStaticSubtree(t,e,s,n){const o=this.staticSubtreeCache.get(e);if(o){const t=o.cloneNode(!0);return s.appendChild(t),t}const r=this.createStaticSubtree(t,s,n);return s.appendChild(r),this.staticSubtreeCache.set(e,r.cloneNode(!0)),r}currentKey(){return this.pathStack.join(".")}findEntry(t){let e=this.nodeMap.get(t);return e||(e=this.prevNodeMap.get(t),e&&(this.prevNodeMap.delete(t),this.nodeMap.set(t,e))),e}irToNode(t,e,s,n){this.pathStack.push(e),this.processNode(t,s,n),this.pathStack.pop()}processNode(t,e,n){var o,r,i,a,c,l,h;const p=this.currentKey(),d=this.findEntry(p);let u;if(1!==t.s)if(d){u=d.node;let e=[];t.d&&(t.t===s.ASTType.Element?e=this.applyAttrs(u,t.a??[],n,!0):this.updateNode(u,t,n)),null==(o=t.c)||o.forEach((t,e)=>{this.pathStack.push(e),this.processNode(t,u,n),this.pathStack.pop()}),e.forEach(t=>t())}else switch(t.t){case s.ASTType.Element:u=this.createElement(t,e),f(u,t),m(u,t);const o=this.applyAttrs(u,t.a??[],n);e.appendChild(u),this.nodeMap.set(p,{irNode:t,node:u,loops:n}),null==(r=t.c)||r.forEach((t,e)=>{this.pathStack.push(e),this.processNode(t,u,n),this.pathStack.pop()}),o.forEach(t=>t());break;case s.ASTType.Text:u=this.createText(t,n),e.appendChild(u),this.nodeMap.set(p,{irNode:t,node:u,loops:n});break;case s.ASTType.Comment:u=this.createComment(t,n),e.appendChild(u),this.nodeMap.set(p,{irNode:t,node:u,loops:n});break;case s.ASTType.For:{this.pathStack.push(-1);const o=this.currentKey();this.pathStack.pop();let r=null==(i=this.findEntry(o))?void 0:i.node;r||(r=document.createComment("for"),e.appendChild(r),this.nodeMap.set(o,{irNode:{t:s.ASTType.Comment},node:r,loops:n}));const c=this.evalIR(t.f??-1,[n],t.__m)||[],l=document.createDocumentFragment();c.forEach((e,s)=>{var o;const r=[...n,{itmVal:e,idxVal:s}];null==(o=t.c)||o.forEach((t,e)=>{this.pathStack.push(s,e),this.processNode(t,l,r),this.pathStack.pop(),this.pathStack.pop()})}),null==(a=r.parentNode)||a.insertBefore(l,r);break}case s.ASTType.Conditional:{this.pathStack.push(-1);const s=this.currentKey();this.pathStack.pop();let o=null==(c=this.findEntry(s))?void 0:c.node;o||(o=document.createComment("if"),e.appendChild(o),this.nodeMap.set(s,{irNode:{},node:o,loops:n}));for(let e=0;e<(t.b||[]).length;e++){const s=null==(l=t.b)?void 0:l[e];if(null===s.f||this.evalIR(s.f,[n],s.__m)){const t=document.createDocumentFragment();s.c.forEach((s,o)=>{this.pathStack.push(e,o),this.processNode(s,t,n),this.pathStack.pop(),this.pathStack.pop()}),null==(h=o.parentNode)||h.insertBefore(t,o);break}}break}}else d?u=d.node:(u=this.handleStaticSubtree(t,p,e,n),this.nodeMap.set(p,{irNode:t,node:u,loops:n}))}cleanupOldNodes(){const t=this.prevNodeMap;for(const[,e]of t.entries()){const{irNode:t,node:s,loops:n}=e;if(t.a){const e=t.a.find(t=>"unmounted"===t.k);e&&requestAnimationFrame(()=>{this.evalIR(e.f??-1,[s,n],e.__m)});const o=t.a.find(t=>"ref"===t.k);o&&o.v&&delete this.scope.$refs[o.v]}null==s||s.remove()}t.clear()}mount(){this.prevNodeMap=this.nodeMap,this.nodeMap=new Map,this.pathStack.length=0,this.ir.n.forEach((t,e)=>{this.irToNode(t,e,this.container,[])}),this.cleanupOldNodes()}update(){this.prevNodeMap=this.nodeMap,this.nodeMap=new Map,this.pathStack.length=0,this.ir.n.forEach((t,e)=>{this.irToNode(t,e,this.container,[])}),this.cleanupOldNodes()}destroy(){this.container.innerHTML="",this.nodeMap.clear(),this.prevNodeMap.clear(),this.staticSubtreeCache.clear()}}exports.IRRenderer=g,exports.createRender=(t,e,s={})=>{const n=new g(t,e,s);return n.mount(),{update:()=>n.update(),destroy:()=>n.destroy()}};
|
|
@@ -1,14 +1,11 @@
|
|
|
1
1
|
import { IRRoot, Meta } from '../../types';
|
|
2
|
-
/**
|
|
3
|
-
* IR 渲染器类 - 负责将 IR(中间表示)渲染到 DOM
|
|
4
|
-
*/
|
|
5
2
|
export declare class IRRenderer {
|
|
6
3
|
private ir;
|
|
7
4
|
private container;
|
|
8
5
|
private scope;
|
|
9
6
|
private nodeMap;
|
|
10
|
-
private
|
|
11
|
-
|
|
7
|
+
private prevNodeMap;
|
|
8
|
+
private pathStack;
|
|
12
9
|
private staticSubtreeCache;
|
|
13
10
|
constructor(ir: IRRoot, container: HTMLElement, scope?: any);
|
|
14
11
|
evalIR(fid: number, args: any[], meta?: Meta): any;
|
|
@@ -27,7 +24,10 @@ export declare class IRRenderer {
|
|
|
27
24
|
* 首次渲染创建并缓存,后续直接克隆
|
|
28
25
|
*/
|
|
29
26
|
private handleStaticSubtree;
|
|
27
|
+
private currentKey;
|
|
28
|
+
private findEntry;
|
|
30
29
|
private irToNode;
|
|
30
|
+
private processNode;
|
|
31
31
|
private cleanupOldNodes;
|
|
32
32
|
mount(): void;
|
|
33
33
|
update(): void;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
var
|
|
1
|
+
var t=Object.defineProperty,e=(e,s,o)=>((e,s,o)=>s in e?t(e,s,{enumerable:!0,configurable:!0,writable:!0,value:o}):e[s]=o)(e,"symbol"!=typeof s?s+"":s,o);import{ASTType as s}from"../../types/ast.js";import{showTemplateError as o}from"../../shared/templateError.js";import{IS_DEV as n}from"../../shared/env.js";import r from"./html-decode.js";const i=Symbol("solely.irCtx"),a=Symbol("solely.irEvents"),c=Symbol("solely.prevClassObj"),l=Symbol("solely.prevStyleObj"),h={xml:"http://www.w3.org/XML/1998/namespace",xlink:"http://www.w3.org/1999/xlink",xmlns:"http://www.w3.org/2000/xmlns/"},p={readonly:"readOnly",for:"htmlFor",tabindex:"tabIndex",contenteditable:"contentEditable",maxlength:"maxLength"},d=new Set(["data-","aria-","role","form"]),u=new Set(["value","selectedIndex"]);function f(t,e){var s;const o=null==(s=e.a)?void 0:s.find(t=>"class"===t.k&&!t.d);(null==o?void 0:o.v)&&t.setAttribute("class",o.v)}function m(t,e){var s;if(!t.style)return;const o=null==(s=e.a)?void 0:s.find(t=>"style"===t.k&&!t.d),n=o?y(o.v):{};for(const r in n){const e=n[r];r.startsWith("--")?t.style.setProperty(r,e):t.style[r]=e}}const v=t=>!(!t||0===t.length||/\s/.test(t)&&(n&&console.warn(`[Solely] Invalid class name "${t}" contains whitespace, will be ignored`),1));function y(t){const e={},s=[[t]];for(;s.length;){const[t,o]=s.pop()||[];if(t){if("string"==typeof t){const s=/([^:;]+)\s*:\s*([^;]*\(.*?\)[^;]*|[^;]+)/g;let o;for(;null!==(o=s.exec(t));){const t=o[1].trim(),s=o[2].trim();t&&s&&(e[t]=s)}continue}if(Array.isArray(t))for(let e=t.length-1;e>=0;e--)s.push([t[e]]);else if("object"==typeof t)for(const n in t){const r=t[n];if(null==r)continue;const i=o?`${o}-${n}`:n;"object"!=typeof r||Array.isArray(r)?e[i]=String(r):s.push([r,i])}}}return e}function S(t,e,s){const o=null==s?"":String(s),n=e.indexOf(":");if(n>0){const s=e.slice(0,n),r=h[s];if(r)return void(t.getAttributeNS(r,e)!==o&&t.setAttributeNS(r,e,o))}t.getAttribute(e)!==o&&t.setAttribute(e,o)}function b(t,e,s,o){const n=p[e]||e;if(e.startsWith("data-")||e.startsWith("aria-")||d.has(e))return void S(t,e,s);const r=t,i=r.tagName;if(null!=s){if("src"===n||"href"===n){const e=String(s);if(r[n]===e||t.getAttribute(n)===e)return}if("value"!==n||"INPUT"!==i&&"TEXTAREA"!==i||r.value!==s)if("boolean"!=typeof s)(o??(t instanceof SVGElement||void 0!==r.ownerSVGElement))&&!p[e]?S(t,e,s):r[n]!==s&&(r[n]=s);else{if(r[n]===s)return;r[n]=s}}else if("boolean"==typeof r[n])!1!==r[n]&&(r[n]=!1);else{const s=r[n];""!==s&&void 0!==s&&(r[n]=""),t.hasAttribute(e)&&t.removeAttribute(e)}}class g{constructor(t,s,o={}){e(this,"nodeMap",new Map),e(this,"prevNodeMap",new Map),e(this,"pathStack",[]),e(this,"staticSubtreeCache",new Map),this.ir=t,this.container=s,this.scope=o}evalIR(t,e,s){var r;const i=this.ir.fns[t-1];if(n)try{return i?i.apply(this.scope,e):""}catch(a){return o(a.message||a.toString(),(null==(r=this.ir.m)?void 0:r.src)||"",s,this.scope.tagName),""}return i?i.apply(this.scope,e):""}createElement(t,e){const s=t.g??"";return e instanceof SVGElement||"svg"===s?document.createElementNS("http://www.w3.org/2000/svg",s):document.createElement(s)}createText(t,e){const s=t.d?this.evalIR(t.f??-1,[e],t.__m):r(t.x||"");return document.createTextNode(s)}createComment(t,e){const s=t.d?this.evalIR(t.f??-1,[e],t.__m):t.x||"";return document.createComment(s)}applyAttrs(t,e,s,o=!1){const n=[],r=t,h=t instanceof SVGElement||void 0!==r.ownerSVGElement;let p;r[i]||(r[i]={loops:null}),r[i].loops=s,r[a]||(r[a]=Object.create(null));let d,f=!1,m=!1;for(const c of e){const{k:e,f:l,d:v,v:y}=c;if(":class"===e){p=this.evalIR(l??-1,[s],c.__m),f=!0;continue}if(":style"===e){d=this.evalIR(l??-1,[s],c.__m),m=!0;continue}if("class"===e||"style"===e)continue;const g=e[0];if("@"===g){const s=e.slice(1),n=c.r??"user";let h=r[a][s];if(h||(h=r[a][s]={model:[],user:[]},t.addEventListener(s,t=>{for(const e of h.model)e(t);for(const e of h.user)e(t)})),!o){const t=t=>this.evalIR(l??-1,[t,r[i].loops],c.__m);h[n].push(t)}continue}if("mounted"===e){o||requestAnimationFrame(()=>this.evalIR(l??-1,[t,s],c.__m));continue}if("updated"===e){o&&requestAnimationFrame(()=>this.evalIR(l??-1,[t,s],c.__m));continue}if("ref"===e&&y){this.scope.$refs=this.scope.$refs||{},this.scope.$refs[y]=t;continue}const N=v?this.evalIR(l??-1,[s],c.__m):y??"";if(":"===g){const s=e.slice(1);"SELECT"===t.tagName&&u.has(s)?n.push(()=>b(t,s,N,h)):b(t,s,N,h)}else o||S(t,e,String(N))}return f&&((t,e)=>{const s=t,o=s[c],n=function(t){const e={},s=[t];for(;s.length;){const t=s.pop();if(t)if("string"==typeof t){const s=t.split(" ");for(let t=0;t<s.length;t++){const o=s[t].trim();o&&(e[o]=!0)}}else if(Array.isArray(t))for(let e=t.length-1;e>=0;e--)s.push(t[e]);else if("object"==typeof t)for(const s in t)t[s]&&(e[s]=!0)}return e}(e),r={};for(const a in n)v(a)&&(r[a]=n[a]);if(!o){for(const e in r)t.classList.add(e);return void(s[c]=r)}let i=!1;for(const a in o)r[a]||(t.classList.remove(a),i=!0);for(const a in r)o[a]||(t.classList.add(a),i=!0);i&&(s[c]=r)})(t,p),m&&((t,e)=>{const s=t,o=y(e),n=s[l];if(!n){for(const e in o){const s=o[e];e.startsWith("--")?t.style.setProperty(e,s):t.style[e]=s}return void(s[l]=o)}let r=!1;for(const i in n)i in o||(i.startsWith("--")?t.style.removeProperty(i):t.style[i]="",r=!0);for(const i in o){const e=o[i];e!==n[i]&&(i.startsWith("--")?t.style.setProperty(i,e):t.style[i]=e,r=!0)}r&&(s[l]=o)})(t,d),n}updateNode(t,e,s){const o=e.d?this.evalIR(e.f??-1,[s],e.__m):e.x||"";e.d&&t.textContent!==o&&(t.textContent=o)}createStaticSubtree(t,e,o){var n;switch(t.t){case s.Element:{const s=this.createElement(t,e);f(s,t),m(s,t);const r=this.applyAttrs(s,t.a??[],o);return null==(n=t.c)||n.forEach(t=>{const e=this.createStaticSubtree(t,s,o);s.appendChild(e)}),r.forEach(t=>t()),s}case s.Text:return this.createText(t,o);case s.Comment:return this.createComment(t,o);default:return document.createComment("unknown")}}handleStaticSubtree(t,e,s,o){const n=this.staticSubtreeCache.get(e);if(n){const t=n.cloneNode(!0);return s.appendChild(t),t}const r=this.createStaticSubtree(t,s,o);return s.appendChild(r),this.staticSubtreeCache.set(e,r.cloneNode(!0)),r}currentKey(){return this.pathStack.join(".")}findEntry(t){let e=this.nodeMap.get(t);return e||(e=this.prevNodeMap.get(t),e&&(this.prevNodeMap.delete(t),this.nodeMap.set(t,e))),e}irToNode(t,e,s,o){this.pathStack.push(e),this.processNode(t,s,o),this.pathStack.pop()}processNode(t,e,o){var n,r,i,a,c,l,h;const p=this.currentKey(),d=this.findEntry(p);let u;if(1!==t.s)if(d){u=d.node;let e=[];t.d&&(t.t===s.Element?e=this.applyAttrs(u,t.a??[],o,!0):this.updateNode(u,t,o)),null==(n=t.c)||n.forEach((t,e)=>{this.pathStack.push(e),this.processNode(t,u,o),this.pathStack.pop()}),e.forEach(t=>t())}else switch(t.t){case s.Element:u=this.createElement(t,e),f(u,t),m(u,t);const n=this.applyAttrs(u,t.a??[],o);e.appendChild(u),this.nodeMap.set(p,{irNode:t,node:u,loops:o}),null==(r=t.c)||r.forEach((t,e)=>{this.pathStack.push(e),this.processNode(t,u,o),this.pathStack.pop()}),n.forEach(t=>t());break;case s.Text:u=this.createText(t,o),e.appendChild(u),this.nodeMap.set(p,{irNode:t,node:u,loops:o});break;case s.Comment:u=this.createComment(t,o),e.appendChild(u),this.nodeMap.set(p,{irNode:t,node:u,loops:o});break;case s.For:{this.pathStack.push(-1);const n=this.currentKey();this.pathStack.pop();let r=null==(i=this.findEntry(n))?void 0:i.node;r||(r=document.createComment("for"),e.appendChild(r),this.nodeMap.set(n,{irNode:{t:s.Comment},node:r,loops:o}));const c=this.evalIR(t.f??-1,[o],t.__m)||[],l=document.createDocumentFragment();c.forEach((e,s)=>{var n;const r=[...o,{itmVal:e,idxVal:s}];null==(n=t.c)||n.forEach((t,e)=>{this.pathStack.push(s,e),this.processNode(t,l,r),this.pathStack.pop(),this.pathStack.pop()})}),null==(a=r.parentNode)||a.insertBefore(l,r);break}case s.Conditional:{this.pathStack.push(-1);const s=this.currentKey();this.pathStack.pop();let n=null==(c=this.findEntry(s))?void 0:c.node;n||(n=document.createComment("if"),e.appendChild(n),this.nodeMap.set(s,{irNode:{},node:n,loops:o}));for(let e=0;e<(t.b||[]).length;e++){const s=null==(l=t.b)?void 0:l[e];if(null===s.f||this.evalIR(s.f,[o],s.__m)){const t=document.createDocumentFragment();s.c.forEach((s,n)=>{this.pathStack.push(e,n),this.processNode(s,t,o),this.pathStack.pop(),this.pathStack.pop()}),null==(h=n.parentNode)||h.insertBefore(t,n);break}}break}}else d?u=d.node:(u=this.handleStaticSubtree(t,p,e,o),this.nodeMap.set(p,{irNode:t,node:u,loops:o}))}cleanupOldNodes(){const t=this.prevNodeMap;for(const[,e]of t.entries()){const{irNode:t,node:s,loops:o}=e;if(t.a){const e=t.a.find(t=>"unmounted"===t.k);e&&requestAnimationFrame(()=>{this.evalIR(e.f??-1,[s,o],e.__m)});const n=t.a.find(t=>"ref"===t.k);n&&n.v&&delete this.scope.$refs[n.v]}null==s||s.remove()}t.clear()}mount(){this.prevNodeMap=this.nodeMap,this.nodeMap=new Map,this.pathStack.length=0,this.ir.n.forEach((t,e)=>{this.irToNode(t,e,this.container,[])}),this.cleanupOldNodes()}update(){this.prevNodeMap=this.nodeMap,this.nodeMap=new Map,this.pathStack.length=0,this.ir.n.forEach((t,e)=>{this.irToNode(t,e,this.container,[])}),this.cleanupOldNodes()}destroy(){this.container.innerHTML="",this.nodeMap.clear(),this.prevNodeMap.clear(),this.staticSubtreeCache.clear()}}const N=(t,e,s={})=>{const o=new g(t,e,s);return o.mount(),{update:()=>o.update(),destroy:()=>o.destroy()}};export{g as IRRenderer,N as createRender};
|
package/package.json
CHANGED