@panphora/clayjs 1.2.0 → 1.3.0

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.
@@ -2,7 +2,7 @@
2
2
  // hyper-html-api/dist/hyper-html-api.engine.min.js via hyper-html-api
3
3
  // `npm run copy-to-clayjs`. Edit the hyper-html-api source and re-run.
4
4
  (function () {
5
- var hyperHtmlApiData=(()=>{var q=Object.defineProperty;var Rt=Object.getOwnPropertyDescriptor;var At=Object.getOwnPropertyNames;var Ot=Object.prototype.hasOwnProperty;var nt=(t,e)=>{for(var n in e)q(t,n,{get:e[n],enumerable:!0})},wt=(t,e,n,o)=>{if(e&&typeof e=="object"||typeof e=="function")for(let r of At(e))!Ot.call(t,r)&&r!==n&&q(t,r,{get:()=>e[r],enumerable:!(o=Rt(e,r))||o.enumerable});return t};var St=t=>wt(q({},"__esModule",{value:!0}),t);var vt={};nt(vt,{applyData:()=>et,default:()=>Ut,engine:()=>k,extractData:()=>tt});var W=["textContent","innerText","innerHTML","outerHTML","value","checked","selected","disabled","readOnly","type","tagName","nodeName","nodeType","nodeValue","childElementCount","id","className","classList","baseURI","offsetWidth","offsetHeight","clientWidth","clientHeight","scrollWidth","scrollHeight","dataset","currentSrc","duration","paused","title","documentURI","contentType"],F=new Set(W),rt=new Set(["textContent","innerText","innerHTML","value","checked","selected","disabled","readOnly","type","id","className","title"]),ot=new Set(["tagName","nodeName","nodeType","nodeValue","childElementCount","classList","baseURI","documentURI","contentType","offsetWidth","offsetHeight","clientWidth","clientHeight","scrollWidth","scrollHeight","currentSrc","duration","paused","dataset"]);var B={};nt(B,{EmptyListInsert:()=>P,MAX_RULE_DEPTH:()=>b,MaxRuleDepthExceeded:()=>S,RuleTargetReadOnly:()=>j,RulesParseError:()=>T,ShapeMismatch:()=>D,UnknownRulesVersion:()=>_});var T=class extends Error{constructor(e,n){super(e),this.name="RulesParseError",this.cause=n}},_=class extends Error{constructor(e){super(`unknown rules version: ${e}. Library supports "1".`),this.name="UnknownRulesVersion",this.version=e}},b=20,S=class extends Error{constructor(e){super(`rule depth exceeded ${b} at path: ${e.join(".")}`),this.name="MaxRuleDepthExceeded",this.path=e}},D=class extends Error{constructor(e){super(`shape mismatch: ${e.length} field(s) failed validation`),this.name="ShapeMismatch",this.mismatches=e}},P=class extends Error{constructor(e){super(`cannot add items to empty list at "${e.join(".")}" \u2014 no sibling to clone as template. Seed the list with a hidden item first.`),this.name="EmptyListInsert",this.path=e}},j=class extends Error{constructor(e){super(`cannot write to read-only DOM property "${e}"`),this.name="RuleTargetReadOnly",this.target=e}};function A(t){let e=0,n=null,o=-1;for(let r=0;r<t.length;r++){let i=t[r];i==="\\"?r++:n?i===n&&(n=null):i==='"'||i==="'"?n=i:i==="["||i==="("?e++:i==="]"||i===")"?e>0&&e--:i==="@"&&e===0&&(o=r)}return o}function K(t){let e=A(t);return e===-1?{selector:t,prop:null}:{selector:t.slice(0,e),prop:t.slice(e+1)||null}}function O(t,e,n,o={}){return G(t,e,n,{depth:0,path:[]},o)}function G(t,e,n,o,r){if(o.depth>b)throw new S(o.path);if(typeof n=="string")return xt(t,e,n,o,r);if(Array.isArray(n)){let[i,f]=n,s=t.find(e,i,r);return st(r,o,s),s.map((l,c)=>G(t,l,f,{depth:o.depth+1,path:[...o.path,c]},r))}if(typeof n=="object"&&n!==null){let i={};for(let[f,s]of Object.entries(n))i[f]=G(t,e,s,{depth:o.depth+1,path:[...o.path,f]},r);return i}return null}function xt(t,e,n,o,r){if(n.endsWith("[]")){let s=n.slice(0,-2),l=t.find(e,s,r);return st(r,o,l),l.map(c=>t.text(c))}if(n.startsWith("@"))return it(t,e,n.slice(1));let i=A(n);if(i!==-1){let s=n.slice(0,i),l=n.slice(i+1),c=s?t.find(e,s,r):[e];return c.length===0?null:it(t,c[0],l)}if(n===".")return t.text(e);let f=t.find(e,n,r);return f.length===0?null:t.text(f[0])}function st(t,e,n){if(typeof t.onRowsRead=="function")try{t.onRowsRead(e.path.slice(),n)}catch(o){console.warn(`[hyper-html-api] onRowsRead threw at "${e.path.join(".")||"(root)"}"`,o)}}function it(t,e,n){if(F.has(n)){let r=t.prop(e,n);return r==null?null:String(r)}let o=t.attr(e,n);return o||null}function ct(t,e,n,o){let r=t.length,i=e.length,f=new Array(r).fill(-1);if(o){let u=new Set;for(let d=0;d<r;d++){let g=o[d];!(g>=0&&g<i)||u.has(g)||(f[d]=g,u.add(g))}}if(r===0||i===0)return f;let s=t.map(u=>lt(u,n)),l=e.map(u=>lt(u,n)),c=new Array(i).fill(!1);for(let u of f)u>=0&&(c[u]=!0);let p=new Map;l.forEach((u,d)=>{c[d]||p.set(u,p.has(u)?-1:d)});let a=new Map;s.forEach((u,d)=>{f[d]>=0||a.set(u,(a.get(u)||0)+1)}),s.forEach((u,d)=>{if(f[d]>=0||a.get(u)!==1)return;let g=p.get(u);g===void 0||g===-1||c[g]||(f[d]=g,c[g]=!0)});let h=[];for(let u=0;u<r;u++)f[u]<0&&h.push(u);let E=[];for(let u=0;u<i;u++)c[u]||E.push(u);if(h.length===0||E.length===0)return f;let y=r*i+1,w=(u,d)=>Tt(t[u],e[d],n)*y+Math.abs(u-d);for(let[u,d]of bt(h,E,w))f[u]=d;return f}var ut=t=>typeof t=="object"&&t!==null;function lt(t,e){if(!ut(e))return t==null?" null":String(t);let n=Object.keys(e);return JSON.stringify(n.map(o=>{let r=JSON.stringify(t?.[o]);return r===void 0?" undef":r}))}function Tt(t,e,n){if(!ut(n))return t===e?0:1;let o=Object.keys(n);if(o.length===0)return 0;let r=0;for(let i of o){let f=JSON.stringify(t?.[i]),s=JSON.stringify(e?.[i]);f!==s&&r++}return r}function bt(t,e,n){return t.length<=e.length?ft(t,e,n,!1):ft(e,t,(o,r)=>n(r,o),!0)}function ft(t,e,n,o){let r=t.length,i=e.length,f=[];for(let a=0;a<=r;a++)f.push(new Float64Array(i+1).fill(1/0));let s=[];for(let a=0;a<=r;a++)s.push(new Uint8Array(i+1));for(let a=0;a<=i;a++)f[r][a]=0;for(let a=r-1;a>=0;a--)for(let h=i-1;h>=0;h--){let E=n(t[a],e[h])+f[a+1][h+1],y=f[a][h+1];E<=y?(f[a][h]=E,s[a][h]=1):f[a][h]=y}let l=[],c=0,p=0;for(;c<r&&p<i;)s[c][p]&&(l.push(o?[e[p],t[c]]:[t[c],e[p]]),c++),p++;return l}function X(t,e,n,o,r,i,f,s={}){let l=t.find(e,n,s);if(r.length===0){l.forEach(m=>t.remove(m)),V(s,"onRowsApplied",i.path,[]);return}let c=r.length>l.length,p=l[0]||null;if(c&&!p&&(p=_t(t,e,n,s),!p))throw new P(i.path);let a=l.map(m=>Mt(t,m,o,s)),h=null;if(c&&p){h=t.clone(p),s.templateAttr&&t.removeAttr(h,s.templateAttr);let m=t.stripIds(h);m>0&&console.warn(`[hyper-html-api] stripped ${m} id attribute(s) from cloned template at "${i.path.join(".")||"(root)"}"`)}let E=ct(r,a,o,Nt(t,l,r,i,s)),y=l[0]||p,w=t.parent(y),u=l.length>0?at(t,w,y):0,d=Dt(t,l),g=new Set,v=[],H=r.map((m,M)=>{let x=E[M];if(x>=0)return g.add(x),v.push(!1),l[x];v.push(!0);let J=t.clone(h);return t.stripIds(J),J});l.forEach((m,M)=>{g.has(M)||t.remove(m)}),d?H.forEach((m,M)=>{let x=u+M;t.children(w).findIndex(Et=>t.sameNode(Et,m))!==x&&t.insertAt(w,m,x)}):Pt(t,H,v,w,u),It(t,H,o,r,i,f,s),V(s,"onRowsApplied",i.path,H)}function V(t,e,n,o){if(typeof t[e]=="function")try{return t[e](n.slice(),o)}catch(r){console.warn(`[hyper-html-api] ${e} threw at "${n.join(".")||"(root)"}"`,r);return}}function Nt(t,e,n,o,r){let i=V(r,"identifyRows",o.path,n);if(!Array.isArray(i))return null;let f=new Array(n.length).fill(-1),s=new Set;for(let l=0;l<n.length;l++)if(i[l]){for(let c=0;c<e.length;c++)if(!(s.has(c)||!t.sameNode(e[c],i[l]))){f[l]=c,s.add(c);break}}return f}function It(t,e,n,o,r,i,f){e.forEach((s,l)=>{if(n===null){let c=o[l],p=c==null?"":String(c);t.text(s)!==p&&t.text(s,p)}else{let c=i(t,s,n,o[l],{depth:r.depth+1,path:[...r.path,l]},f);c&&c!==s&&(e[l]=c)}})}function Mt(t,e,n,o){return n===null?t.text(e):O(t,e,n,o.onRowsRead?{...o,onRowsRead:void 0}:o)}function at(t,e,n){let o=t.children(e);for(let r=0;r<o.length;r++)if(t.sameNode(o[r],n))return r;return-1}function _t(t,e,n,o){if(!o.templateAttr)return null;let r=e;for(;r;){let i=t.find(r,n,{includeRulesTag:!1,templateAttr:null});for(let f of i)if(t.attr(f,o.templateAttr)!=null)return f;r=t.parent(r)}return null}function Dt(t,e){if(e.length<=1)return!0;let n=t.parent(e[0]);if(!n)return!1;let o=t.children(n),r=[];for(let i of e){let f=o.findIndex(s=>t.sameNode(s,i));if(f===-1)return!1;r.push(f)}return r.sort((i,f)=>i-f),r[r.length-1]-r[0]===r.length-1}function Pt(t,e,n,o,r){let i=null,f=r;for(let s=0;s<e.length;s++){if(!n[s]){i=e[s];continue}let l=i?t.parent(i):o;if(!l)continue;let c=i?at(t,l,i)+1:f++;t.insertAt(l,e[s],c),i=e[s]}}var ht=new Set(["checked","selected","disabled","readOnly","paused"]);function N(t,e,n,o,r={}){let i=[];if(z(n,o,[],i),i.length)throw new D(i);U(t,e,n,o,{depth:0,path:[]},r)}function U(t,e,n,o,r,i={}){if(r.depth>b)throw new S(r.path);if(o===void 0)return e;if(typeof n=="string")return jt(t,e,n,o,r,i);if(Array.isArray(n)){let[f,s]=n;return X(t,e,f,s,o,r,U,i),e}if(typeof n=="object"&&n!==null){for(let[f,s]of Object.entries(n)){let l=U(t,e,s,o==null?o:o[f],{depth:r.depth+1,path:[...r.path,f]},i);l&&l!==e&&(e=l)}return e}return e}function jt(t,e,n,o,r,i){if(n.endsWith("[]")){let l=n.slice(0,-2);return X(t,e,l,null,o,r,U,i),e}if(n.startsWith("@"))return dt(t,e,n.slice(1),o);let f=A(n);if(f!==-1){let l=n.slice(0,f),c=n.slice(f+1),p=l?t.find(e,l,i):[e];return p.length===0||dt(t,p[0],c,o),e}if(n===".")return pt(t,e,o),e;let s=t.find(e,n,i);return s.length===0||pt(t,s[0],o),e}function pt(t,e,n){let o=n==null?"":String(n);t.text(e)!==o&&t.text(e,o)}function dt(t,e,n,o){if(ot.has(n))throw new j(n);if(n==="outerHTML"){let r=o==null?"":String(o);return t.replaceWith(e,r)}return rt.has(n)?(t.prop(e,n,Ct(n,o)),e):(t.attr(e,n,o==null?"":String(o)),e)}function Ct(t,e){return e==null?ht.has(t)?!1:"":ht.has(t)?e==="false"?!1:!!e:e}function z(t,e,n,o){if(e!==void 0){if(typeof t=="string"){if(t.endsWith("[]")){Array.isArray(e)?e.forEach((r,i)=>{typeof r=="object"&&r!==null&&o.push({path:L([...n,i]),expected:"scalar",got:C(r)})}):o.push({path:L(n),expected:"array",got:C(e)});return}e!==null&&typeof e=="object"&&o.push({path:L(n),expected:"scalar",got:C(e)});return}if(Array.isArray(t)){if(!Array.isArray(e)){o.push({path:L(n),expected:"array",got:C(e)});return}let r=t[1];e.forEach((i,f)=>z(r,i,[...n,f],o));return}if(typeof t=="object"&&t!==null){if(e===null||Array.isArray(e)||typeof e!="object"){o.push({path:L(n),expected:"object",got:C(e)});return}for(let[r,i]of Object.entries(t))z(i,e[r],[...n,r],o)}}}function C(t){return t===null?"null":Array.isArray(t)?"array":typeof t}function L(t){return t.join(".")}function Q(t){try{return JSON.parse(t)}catch(e){throw new T(`Invalid strict JSON: ${e.message}`,e)}}function $(t){try{return JSON.parse(t)}catch{}let e={BRACE_OPEN:"{",BRACE_CLOSE:"}",BRACKET_OPEN:"[",BRACKET_CLOSE:"]",COLON:":",COMMA:",",STRING:"STRING",SELECTOR:"SELECTOR",IDENTIFIER:"IDENTIFIER",NUMBER:"NUMBER",BOOLEAN:"BOOLEAN"};function n(r){let i=[],f=0;for(;f<r.length;){let s=r[f];if(/\s/.test(s)){f++;continue}if("{}".includes(s)){i.push({type:s,value:s}),f++;continue}if(s==="["){let a=!1,h=f+1;for(;h<r.length&&/\s/.test(r[h]);)h++;if(h<r.length&&/[a-zA-Z_]/.test(r[h])&&(a=!0),!a){i.push({type:s,value:s}),f++;continue}}if(s==="]"){i.push({type:s,value:s}),f++;continue}if(s===":"){i.push({type:e.COLON,value:s}),f++;continue}if(s===","){i.push({type:e.COMMA,value:s}),f++;continue}if(s==='"'||s==="'"){let a=s,h=f+1;for(;h<r.length&&r[h]!==a;)r[h]==="\\"&&h++,h++;i.push({type:e.STRING,value:r.substring(f+1,h),quoted:!0,sourceQuote:a}),f=h+1;continue}let l=f,c;for(;l<r.length&&!/[{},]/.test(r[l]);)if(r[l]===":"){let a=[":first",":last",":nth-child",":nth-of-type",":first-child",":last-child",":first-of-type",":last-of-type",":only-child",":only-of-type",":hover",":focus",":active",":visited",":disabled",":enabled",":checked",":empty",":root",":target",":not",":before",":after",":nth-last-child",":nth-last-of-type"],h=!1;for(let E of a){let y=E.substring(1);if(r.substring(l+1,l+1+y.length)===y){h=!0,l+=y.length;break}}if(!h)break}else if(r[l]==="["){for(l++;l<r.length&&r[l]!=="]";){if(r[l]==='"'||r[l]==="'"){let a=r[l];for(l++;l<r.length&&r[l]!==a;)r[l]==="\\"&&l++,l++}l++}l<r.length&&r[l]==="]"&&l++}else l++;c=r.substring(f,l);let p=e.IDENTIFIER;/^-?\d+(\.\d+)?$/.test(c)?p=e.NUMBER:c==="true"||c==="false"||c==="null"?p=e.BOOLEAN:/^[.#@\[]|[.#@\[]| /.test(c)&&(p=e.SELECTOR),i.push({type:p,value:c,quoted:!1}),f=l}return i}function o(r){let i="";for(let f=0;f<r.length;f++){let s=r[f];if("{}".includes(s.type)||"[]".includes(s.type)){i+=s.value;continue}if(s.type===e.COLON){i+=s.value;continue}if(s.type===e.COMMA){let l=r[f+1];if(l&&(l.type==="}"||l.type==="]"))continue;i+=s.value;continue}if(s.type===e.STRING&&s.quoted){let l=s.value;s.sourceQuote==="'"&&(l=l.replace(/\\'/g,"'"),l=l.replace(/(\\*)"/g,(c,p)=>p.length%2===0?p+'\\"':c)),i+=`"${l}"`;continue}if(s.type===e.NUMBER||s.type===e.BOOLEAN){i+=s.value;continue}if(s.type===e.SELECTOR||s.type===e.IDENTIFIER){i+=`"${s.value}"`;continue}i+=`"${s.value}"`}return i}try{let r=n(t),i=o(r);return JSON.parse(i)}catch(r){throw new T("Invalid extraction rules syntax: "+r.message,r)}}var Lt="1",gt=/^[a-zA-Z0-9_-]+$/;function I(t,e,n){let o;if(n===void 0)o="script[data-rules-name]";else{if(typeof n!="string"||!gt.test(n))throw new Error(`hyper-html-api: invalid rules token ${JSON.stringify(n)} (must match ${gt})`);o=`script[data-rules-name~="${n}"]`}let r=t.find(e,o,{includeRulesTag:!0});if(r.length===0)return null;n!==void 0&&r.length>1&&console.warn(`hyper-html-api: ${r.length} rules tags match data-rules-name~="${n}"; using the first.`);let i=r[0],f=t.attr(i,"data-rules-version");if(f!==Lt)throw new _(f);return{rules:$(t.text(i)),tagNode:i}}function Y(t,e,n){if(n&&typeof n=="object")return{rules:n,tagNode:null};if(typeof n=="string"){let o=e&&e.ownerDocument?e.ownerDocument:e;return I(t,o,n)}return null}function mt(t,e,n,o){let r=Y(t,e,n);if(!r){let s=typeof n=="string"?`data-rules-name~="${n}"`:"the provided rules object";throw new Error(`hyper-html-api: could not resolve rules for ${s}`)}let{rules:i,tagNode:f}=r;return{rules:i,tagNode:f,get:()=>O(t,e,i,o),set:s=>N(t,e,i,s,o)}}function kt(t){return t&&t.nodeType===1&&t.tagName==="SCRIPT"&&t.hasAttribute&&t.hasAttribute("data-rules-name")}function Ht(t){return t?(t.nodeType===9||t.nodeType===11,t):null}var Wt={find(t,e,n={}){let o=Ht(t);if(!o||!o.querySelectorAll)return[];let r=Array.from(o.querySelectorAll(e));n.includeRulesTag||(r=r.filter(s=>!kt(s)));let i=[];n.skip&&i.push(n.skip);let f=n.templateAttr===null?null:n.templateAttr||"cms-template";if(f&&i.push("["+f+"]"),i.length){let s=i.join(", ");r=r.filter(l=>!l.closest||!l.closest(s))}return r},parent(t){return t?t.parentElement:null},children(t){return t?Array.from(t.children):[]},text(t,e){if(e===void 0)return(t.textContent||"").trim();t.textContent=e},attr(t,e,n){if(n===void 0)return t.hasAttribute&&t.hasAttribute(e)?t.getAttribute(e):null;t.setAttribute(e,n)},removeAttr(t,e){t&&t.removeAttribute&&t.removeAttribute(e)},prop(t,e,n){if(n===void 0){let o=t?t[e]:void 0;return o!==void 0?o:null}t[e]=n},clone(t){return t.cloneNode(!0)},insertAt(t,e,n){let o=t.children[n]||null;t.insertBefore(e,o)},remove(t){t&&t.parentNode&&t.parentNode.removeChild(t)},replaceWith(t,e){if(!t||!t.parentNode)throw new Error("dom.replaceWith: node has no parent");let o=t.ownerDocument.createElement("template");o.innerHTML=e;let r=o.content.firstElementChild;if(!r)throw new Error("dom.replaceWith: html did not parse to an element");return t.parentNode.replaceChild(r,t),r},stripIds(t){let e=0;return t.id&&(t.removeAttribute("id"),e++),(t.querySelectorAll?t.querySelectorAll("[id]"):[]).forEach(o=>{o.removeAttribute("id"),e++}),e},sameNode(t,e){return t===e}},R=Wt;var k={extract:(t,e,n)=>O(R,t,e,n),apply:(t,e,n,o)=>N(R,t,e,n,o),findRulesIn:(t,e)=>I(R,t,e),findRules:(t,e)=>Y(R,t,e),bind:(t,e,n)=>mt(R,t,e,n),parseStrict:Q,parseRelaxed:$,ruleAttrIndex:A,splitRule:K,errors:B,DOM_PROPERTIES:W},Z=t=>!!t&&typeof t.nodeType=="number";function yt(t){let e=t&&t.ownerDocument?t.ownerDocument:t,n=R.find(e,"script[data-rules-name]",{includeRulesTag:!0});if(n.length===0)throw new Error('hyper-html-api: no rules tag found. Add <script type="application/json" data-rules-name="\u2026" data-rules-version="1"> or pass rules.');if(n.length>1)throw new Error('hyper-html-api: multiple rules tags found; pass a name, e.g. extractData("api").');return I(R,e).rules}function tt(t,e){let n=Z(t)?t:document,o=Z(t)?e:t;return o===void 0?O(R,n,yt(n)):k.bind(n,o).get()}function et(t,e,n){if(!Z(t))throw new Error("hyper-html-api: applyData(root, data, source?) needs a DOM root as the first argument.");return n===void 0?N(R,t,yt(t),e):k.bind(t,n).set(e),t}var Bt={engine:k,extractData:tt,applyData:et},Ut=Bt;return St(vt);})();
5
+ var hyperHtmlApiData=(()=>{var K=Object.defineProperty;var Ie=Object.getOwnPropertyDescriptor;var je=Object.getOwnPropertyNames;var Pe=Object.prototype.hasOwnProperty;var fe=(e,t)=>{for(var n in t)K(e,n,{get:t[n],enumerable:!0})},De=(e,t,n,o)=>{if(t&&typeof t=="object"||typeof t=="function")for(let r of je(t))!Pe.call(e,r)&&r!==n&&K(e,r,{get:()=>t[r],enumerable:!(o=Ie(t,r))||o.enumerable});return e};var Le=e=>De(K({},"__esModule",{value:!0}),e);var tt={};fe(tt,{applyData:()=>se,default:()=>et,engine:()=>z,extractData:()=>le});var F=["textContent","innerText","innerHTML","outerHTML","value","checked","selected","disabled","readOnly","type","tagName","nodeName","nodeType","nodeValue","childElementCount","id","className","classList","baseURI","offsetWidth","offsetHeight","clientWidth","clientHeight","scrollWidth","scrollHeight","dataset","currentSrc","duration","paused","title","documentURI","contentType"],G=new Set(F),ue=new Set(["textContent","innerText","innerHTML","value","checked","selected","disabled","readOnly","type","id","className","title"]),X=new Set(["tagName","nodeName","nodeType","nodeValue","childElementCount","classList","baseURI","documentURI","contentType","offsetWidth","offsetHeight","clientWidth","clientHeight","scrollWidth","scrollHeight","currentSrc","duration","paused","dataset"]);var V={};fe(V,{EmptyListInsert:()=>k,MAX_RULE_DEPTH:()=>j,MaxRuleDepthExceeded:()=>C,RuleTargetReadOnly:()=>$,RulesParseError:()=>I,ShapeMismatch:()=>v,UnknownRulesVersion:()=>_});var I=class extends Error{constructor(t,n){super(t),this.name="RulesParseError",this.cause=n}},_=class extends Error{constructor(t){super(`unknown rules version: ${t}. Library supports "1".`),this.name="UnknownRulesVersion",this.version=t}},j=20,C=class extends Error{constructor(t){super(`rule depth exceeded ${j} at path: ${t.join(".")}`),this.name="MaxRuleDepthExceeded",this.path=t}},v=class extends Error{constructor(t){super(`shape mismatch: ${t.length} field(s) failed validation`),this.name="ShapeMismatch",this.mismatches=t}},k=class extends Error{constructor(t){super(`cannot add items to empty list at "${t.join(".")}" \u2014 no sibling to clone as template. Seed the list with a hidden item first.`),this.name="EmptyListInsert",this.path=t}},$=class extends Error{constructor(t){super(`cannot write to read-only DOM property "${t}"`),this.name="RuleTargetReadOnly",this.target=t}};function b(e){let t=0,n=null,o=-1;for(let r=0;r<e.length;r++){let i=e[r];i==="\\"?r++:n?i===n&&(n=null):i==='"'||i==="'"?n=i:i==="["||i==="("?t++:i==="]"||i===")"?t>0&&t--:i==="@"&&t===0&&(o=r)}return o}function Y(e){let t=b(e);return t===-1?{selector:e,prop:null}:{selector:e.slice(0,t),prop:e.slice(t+1)||null}}function N(e,t,n,o={}){if(Object.prototype.hasOwnProperty.call(o,"exclude")&&e.semanticExclude===!1)throw new Error("This adapter does not support semantic exclude queries; omit exclude or use the DOM adapter.");return Q(e,t,n,{depth:0,path:[]},o)}function Q(e,t,n,o,r){if(o.depth>j)throw new C(o.path);if(typeof n=="string")return _e(e,t,n,o,r);if(Array.isArray(n)){let[i,c]=n,l=e.find(t,i,r);return de(r,o,l),l.map((s,u)=>Q(e,s,c,{depth:o.depth+1,path:[...o.path,u]},r))}if(typeof n=="object"&&n!==null){let i={};for(let[c,l]of Object.entries(n))i[c]=Q(e,t,l,{depth:o.depth+1,path:[...o.path,c]},r);return i}return null}function _e(e,t,n,o,r){if(n.endsWith("[]")){let l=n.slice(0,-2),s=e.find(t,l,r);return de(r,o,s),s.map(u=>e.text(u))}if(n.startsWith("@"))return ae(e,t,n.slice(1));let i=b(n);if(i!==-1){let l=n.slice(0,i),s=n.slice(i+1),u=l?e.find(t,l,r):[t];return u.length===0?null:ae(e,u[0],s)}if(n===".")return e.text(t);let c=e.find(t,n,r);return c.length===0?null:e.text(c[0])}function de(e,t,n){if(typeof e.onRowsRead=="function")try{e.onRowsRead(t.path.slice(),n)}catch(o){console.warn(`[hyper-html-api] onRowsRead threw at "${t.path.join(".")||"(root)"}"`,o)}}function ae(e,t,n){if(G.has(n)){let r=e.prop(t,n);return r==null?null:String(r)}let o=e.attr(t,n);return o||null}function me(e,t,n,o){let r=e.length,i=t.length,c=new Array(r).fill(-1);if(o){let f=new Set;for(let h=0;h<r;h++){let y=o[h];!(y>=0&&y<i)||f.has(y)||(c[h]=y,f.add(y))}}if(r===0||i===0)return c;let l=e.map(f=>he(f,n)),s=t.map(f=>he(f,n)),u=new Array(i).fill(!1);for(let f of c)f>=0&&(u[f]=!0);let a=new Map;s.forEach((f,h)=>{u[h]||a.set(f,a.has(f)?-1:h)});let g=new Map;l.forEach((f,h)=>{c[h]>=0||g.set(f,(g.get(f)||0)+1)}),l.forEach((f,h)=>{if(c[h]>=0||g.get(f)!==1)return;let y=a.get(f);y===void 0||y===-1||u[y]||(c[h]=y,u[y]=!0)});let d=[];for(let f=0;f<r;f++)c[f]<0&&d.push(f);let x=[];for(let f=0;f<i;f++)u[f]||x.push(f);if(d.length===0||x.length===0)return c;let m=r*i+1,p=(f,h)=>ve(e[f],t[h],n)*m+Math.abs(f-h);for(let[f,h]of ke(d,x,p))c[f]=h;return c}var ye=e=>typeof e=="object"&&e!==null;function he(e,t){if(!ye(t))return e==null?" null":String(e);let n=Object.keys(t);return JSON.stringify(n.map(o=>{let r=JSON.stringify(e?.[o]);return r===void 0?" undef":r}))}function ve(e,t,n){if(!ye(n))return e===t?0:1;let o=Object.keys(n);if(o.length===0)return 0;let r=0;for(let i of o){let c=JSON.stringify(e?.[i]),l=JSON.stringify(t?.[i]);c!==l&&r++}return r}function ke(e,t,n){return e.length<=t.length?pe(e,t,n,!1):pe(t,e,(o,r)=>n(r,o),!0)}function pe(e,t,n,o){let r=e.length,i=t.length,c=[];for(let g=0;g<=r;g++)c.push(new Float64Array(i+1).fill(1/0));let l=[];for(let g=0;g<=r;g++)l.push(new Uint8Array(i+1));for(let g=0;g<=i;g++)c[r][g]=0;for(let g=r-1;g>=0;g--)for(let d=i-1;d>=0;d--){let x=n(e[g],t[d])+c[g+1][d+1],m=c[g][d+1];x<=m?(c[g][d]=x,l[g][d]=1):c[g][d]=m}let s=[],u=0,a=0;for(;u<r&&a<i;)l[u][a]&&(s.push(o?[t[a],e[u]]:[e[u],t[a]]),u++),a++;return s}function ee(e,t,n,o,r,i,c,l={}){let s=e.find(t,n,l);if(r.length===0){s.forEach(E=>e.remove(E)),Z(l,"onRowsApplied",i.path,[]);return}let u=r.length>s.length,a=s[0]||null;if(u&&!a&&(a=Be(e,t,n,l),!a))throw new k(i.path);let g=s.map(E=>We(e,E,o,l)),d=null;if(u&&a){d=e.clone(a),l.templateAttr&&e.removeAttr(d,l.templateAttr);let E=e.stripIds(d);E>0&&console.warn(`[hyper-html-api] stripped ${E} id attribute(s) from cloned template at "${i.path.join(".")||"(root)"}"`)}let x=me(r,g,o,$e(e,s,r,i,l)),m=s[0]||a,p=e.parent(m),f=s.length>0?ge(e,p,m):0,h=qe(e,s),y=new Set,A=[],O=r.map((E,w)=>{let T=x[w];if(T>=0)return y.add(T),A.push(!1),s[T];A.push(!0);let S=e.clone(d);return e.stripIds(S),S});s.forEach((E,w)=>{y.has(w)||e.remove(E)}),h?O.forEach((E,w)=>{let T=f+w;e.children(p).findIndex(Me=>e.sameNode(Me,E))!==T&&e.insertAt(p,E,T)}):Ue(e,O,A,p,f),He(e,O,o,r,i,c,l),Z(l,"onRowsApplied",i.path,O)}function Z(e,t,n,o){if(typeof e[t]=="function")try{return e[t](n.slice(),o)}catch(r){console.warn(`[hyper-html-api] ${t} threw at "${n.join(".")||"(root)"}"`,r);return}}function $e(e,t,n,o,r){let i=Z(r,"identifyRows",o.path,n);if(!Array.isArray(i))return null;let c=new Array(n.length).fill(-1),l=new Set;for(let s=0;s<n.length;s++)if(i[s]){for(let u=0;u<t.length;u++)if(!(l.has(u)||!e.sameNode(t[u],i[s]))){c[s]=u,l.add(u);break}}return c}function He(e,t,n,o,r,i,c){t.forEach((l,s)=>{if(n===null){let u=o[s],a=u==null?"":String(u);e.text(l)!==a&&e.text(l,a)}else{let u=i(e,l,n,o[s],{depth:r.depth+1,path:[...r.path,s]},c);u&&u!==l&&(t[s]=u)}})}function We(e,t,n,o){return n===null?e.text(t):N(e,t,n,o.onRowsRead?{...o,onRowsRead:void 0}:o)}function ge(e,t,n){let o=e.children(t);for(let r=0;r<o.length;r++)if(e.sameNode(o[r],n))return r;return-1}function Be(e,t,n,o){if(!o.templateAttr)return null;let r=t;for(;r;){let i=e.find(r,n,{includeRulesTag:!1,templateAttr:null});for(let c of i)if(e.attr(c,o.templateAttr)!=null)return c;r=e.parent(r)}return null}function qe(e,t){if(t.length<=1)return!0;let n=e.parent(t[0]);if(!n)return!1;let o=e.children(n),r=[];for(let i of t){let c=o.findIndex(l=>e.sameNode(l,i));if(c===-1)return!1;r.push(c)}return r.sort((i,c)=>i-c),r[r.length-1]-r[0]===r.length-1}function Ue(e,t,n,o,r){let i=null,c=r;for(let l=0;l<t.length;l++){if(!n[l]){i=t[l];continue}let s=i?e.parent(i):o;if(!s)continue;let u=i?ge(e,s,i)+1:c++;e.insertAt(s,t[l],u),i=t[l]}}var Ee=new Set(["checked","selected","disabled","readOnly","paused"]);function P(e,t,n,o,r={}){if(Object.prototype.hasOwnProperty.call(r,"exclude")&&e.semanticExclude===!1)throw new Error("This adapter does not support semantic exclude queries; omit exclude or use the DOM adapter.");let i=[];if(te(n,o,[],i),i.length)throw new v(i);J(e,t,n,o,{depth:0,path:[]},r)}function J(e,t,n,o,r,i={}){if(r.depth>j)throw new C(r.path);if(o===void 0)return t;if(typeof n=="string")return ze(e,t,n,o,r,i);if(Array.isArray(n)){let[c,l]=n;return ee(e,t,c,l,o,r,J,i),t}if(typeof n=="object"&&n!==null){for(let[c,l]of Object.entries(n)){let s=J(e,t,l,o==null?o:o[c],{depth:r.depth+1,path:[...r.path,c]},i);s&&s!==t&&(t=s)}return t}return t}function ze(e,t,n,o,r,i){if(n.endsWith("[]")){let s=n.slice(0,-2);return ee(e,t,s,null,o,r,J,i),t}if(n.startsWith("@"))return we(e,t,n.slice(1),o);let c=b(n);if(c!==-1){let s=n.slice(0,c),u=n.slice(c+1),a=s?e.find(t,s,i):[t];return a.length===0||we(e,a[0],u,o),t}if(n===".")return Te(e,t,o),t;let l=e.find(t,n,i);return l.length===0||Te(e,l[0],o),t}function Te(e,t,n){let o=n==null?"":String(n);e.text(t)!==o&&e.text(t,o)}function we(e,t,n,o){if(X.has(n))throw new $(n);if(n==="outerHTML"){let i=o==null?"":String(o);return e.replaceWith(t,i)}if(ue.has(n)){let i=Fe(n,o);return e.prop(t,n)!==i&&e.prop(t,n,i),t}let r=o==null?"":String(o);return e.attr(t,n)!==r&&e.attr(t,n,r),t}function Fe(e,t){return t==null?Ee.has(e)?!1:"":Ee.has(e)?t==="false"?!1:!!t:t}function te(e,t,n,o){if(t!==void 0){if(typeof e=="string"){if(e.endsWith("[]")){Array.isArray(t)?t.forEach((r,i)=>{typeof r=="object"&&r!==null&&o.push({path:W([...n,i]),expected:"scalar",got:H(r)})}):o.push({path:W(n),expected:"array",got:H(t)});return}t!==null&&typeof t=="object"&&o.push({path:W(n),expected:"scalar",got:H(t)});return}if(Array.isArray(e)){if(!Array.isArray(t)){o.push({path:W(n),expected:"array",got:H(t)});return}let r=e[1];t.forEach((i,c)=>te(r,i,[...n,c],o));return}if(typeof e=="object"&&e!==null){if(t===null||Array.isArray(t)||typeof t!="object"){o.push({path:W(n),expected:"object",got:H(t)});return}for(let[r,i]of Object.entries(e))te(i,t[r],[...n,r],o)}}}function H(e){return e===null?"null":Array.isArray(e)?"array":typeof e}function W(e){return e.join(".")}function ne(e){try{return JSON.parse(e)}catch(t){throw new I(`Invalid strict JSON: ${t.message}`,t)}}function B(e){try{return JSON.parse(e)}catch{}let t={BRACE_OPEN:"{",BRACE_CLOSE:"}",BRACKET_OPEN:"[",BRACKET_CLOSE:"]",COLON:":",COMMA:",",STRING:"STRING",SELECTOR:"SELECTOR",IDENTIFIER:"IDENTIFIER",NUMBER:"NUMBER",BOOLEAN:"BOOLEAN"};function n(r){let i=[],c=0;for(;c<r.length;){let l=r[c];if(/\s/.test(l)){c++;continue}if("{}".includes(l)){i.push({type:l,value:l}),c++;continue}if(l==="["){let g=!1,d=c+1;for(;d<r.length&&/\s/.test(r[d]);)d++;if(d<r.length&&/[a-zA-Z_]/.test(r[d])&&(g=!0),!g){i.push({type:l,value:l}),c++;continue}}if(l==="]"){i.push({type:l,value:l}),c++;continue}if(l===":"){i.push({type:t.COLON,value:l}),c++;continue}if(l===","){i.push({type:t.COMMA,value:l}),c++;continue}if(l==='"'||l==="'"){let g=l,d=c+1;for(;d<r.length&&r[d]!==g;)r[d]==="\\"&&d++,d++;i.push({type:t.STRING,value:r.substring(c+1,d),quoted:!0,sourceQuote:g}),c=d+1;continue}let s=c,u;for(;s<r.length&&!/[{},]/.test(r[s]);)if(r[s]===":"){let g=[":first",":last",":nth-child",":nth-of-type",":first-child",":last-child",":first-of-type",":last-of-type",":only-child",":only-of-type",":hover",":focus",":active",":visited",":disabled",":enabled",":checked",":empty",":root",":target",":not",":before",":after",":nth-last-child",":nth-last-of-type"],d=!1;for(let x of g){let m=x.substring(1);if(r.substring(s+1,s+1+m.length)===m){d=!0,s+=m.length;break}}if(!d)break}else if(r[s]==="["){for(s++;s<r.length&&r[s]!=="]";){if(r[s]==='"'||r[s]==="'"){let g=r[s];for(s++;s<r.length&&r[s]!==g;)r[s]==="\\"&&s++,s++}s++}s<r.length&&r[s]==="]"&&s++}else s++;u=r.substring(c,s);let a=t.IDENTIFIER;/^-?\d+(\.\d+)?$/.test(u)?a=t.NUMBER:u==="true"||u==="false"||u==="null"?a=t.BOOLEAN:/^[.#@\[]|[.#@\[]| /.test(u)&&(a=t.SELECTOR),i.push({type:a,value:u,quoted:!1}),c=s}return i}function o(r){let i="";for(let c=0;c<r.length;c++){let l=r[c];if("{}".includes(l.type)||"[]".includes(l.type)){i+=l.value;continue}if(l.type===t.COLON){i+=l.value;continue}if(l.type===t.COMMA){let s=r[c+1];if(s&&(s.type==="}"||s.type==="]"))continue;i+=l.value;continue}if(l.type===t.STRING&&l.quoted){let s=l.value;l.sourceQuote==="'"&&(s=s.replace(/\\'/g,"'"),s=s.replace(/(\\*)"/g,(u,a)=>a.length%2===0?a+'\\"':u)),i+=`"${s}"`;continue}if(l.type===t.NUMBER||l.type===t.BOOLEAN){i+=l.value;continue}if(l.type===t.SELECTOR||l.type===t.IDENTIFIER){i+=`"${l.value}"`;continue}i+=`"${l.value}"`}return i}try{let r=n(e),i=o(r);return JSON.parse(i)}catch(r){throw new I("Invalid extraction rules syntax: "+r.message,r)}}var Ve="1",Oe=/^[a-zA-Z0-9_-]+$/;function D(e,t,n){let o;if(n===void 0)o="script[data-rules-name]";else{if(typeof n!="string"||!Oe.test(n))throw new Error(`hyper-html-api: invalid rules token ${JSON.stringify(n)} (must match ${Oe})`);o=`script[data-rules-name~="${n}"]`}let r=e.find(t,o,{includeRulesTag:!0});if(r.length===0)return null;n!==void 0&&r.length>1&&console.warn(`hyper-html-api: ${r.length} rules tags match data-rules-name~="${n}"; using the first.`);let i=r[0],c=e.attr(i,"data-rules-version");if(c!==Ve)throw new _(c);return{rules:B(e.text(i)),tagNode:i}}function re(e,t,n){if(n&&typeof n=="object")return{rules:n,tagNode:null};if(typeof n=="string"){let o=t&&t.ownerDocument?t.ownerDocument:t;return D(e,o,n)}return null}var Ae=Object.freeze({data:Object.freeze({tokens:["no-data"],bundles:["editor-ui"]}),save:Object.freeze({tokens:["no-save"],bundles:["editor-ui"]}),snapshot:Object.freeze({tokens:["no-snapshot"],bundles:["editor-ui"]}),watch:Object.freeze({tokens:["no-watch"],bundles:["editor-ui"]}),undo:Object.freeze({tokens:["no-undo"],bundles:["editor-ui"]}),history:Object.freeze({tokens:[],bundles:["editor-ui"]})}),Ke=Object.freeze({"editor-ui":Object.freeze(["no-data","no-save","no-snapshot","no-watch","no-undo"])}),Dt=Object.freeze(["no-save","no-snapshot","no-trigger-autosave","no-dirty","no-watch","no-undo","no-data","freeze","editor-ui"]);function oe(e,t){if(!e||e.nodeType!==1)return!1;let n=e.getAttribute?.("clay");return!!(n&&n.split(/\s+/).includes(t)||e.hasAttribute?.(t))}function Ge(e,t){if(oe(e,t))return!0;for(let[n,o]of Object.entries(Ke))if(o.includes(t)&&oe(e,n))return!0;return!1}function q(e){let t=Ae[e];if(!t)throw new Error(`Unknown region capability: ${e}`);return[...t.tokens,...t.bundles].flatMap(n=>[`[clay~="${n}"]`,`[${n}]`]).join(", ")}function Xe(e,t){let n=e&&e.nodeType===1?e:e?.parentElement;for(;n&&n.nodeType===1;){let o=Ae[t];if(!o)throw new Error(`Unknown region capability: ${t}`);if(o.tokens.some(r=>Ge(n,r))||o.bundles.some(r=>oe(n,r)))return n;n=n.parentElement}return null}function xe(e,t){return!!Xe(e,t)}var Ye=/:(?:focus(?:-within|-visible)?|hover|active|visited|defined)\b/i;function Re(e,t){if(!/^(INPUT|TEXTAREA|SELECT|OPTION)$/.test(e.tagName||""))return;let n=(e.getAttribute?.("type")||"").toLowerCase();if("value"in e&&"value"in t&&e.tagName!=="OPTION"&&n!=="checkbox"&&n!=="radio"&&(t.value=e.value),"checked"in e&&"checked"in t&&(t.checked=e.checked),"selected"in e&&"selected"in t&&(t.selected=e.selected),e.tagName==="SELECT")for(let o=0;o<e.options.length;o++)t.options[o].selected=e.options[o].selected;"indeterminate"in e&&"indeterminate"in t&&(t.indeterminate=e.indeterminate)}function Se(e,t,n,o){if(o)return!!e.closest?.(t);let r=e;for(;r?.nodeType===1;){if(r.matches(t))return!0;if(r===n)break;r=r.parentElement}return!1}function be(e,t,n,o,r,i,c,l){if(e.nodeType===1&&((c?xe(e,n):Se(e,o,i,!1))||r&&Se(e,r,i,c)))return null;let s=t.importNode(e,!1);l.cloneToLive.set(s,e),l.liveToClone.set(e,s);let u=e.nodeType===1&&e.tagName==="TEMPLATE"?e.content:e,a=s.nodeType===1&&s.tagName==="TEMPLATE"?s.content:s;u!==e&&(l.cloneToLive.set(a,u),l.liveToClone.set(u,a));for(let g of u.childNodes||[]){let d=be(g,t,n,o,r,i,c,l);d&&(a.appendChild(d),g.nodeType===1&&Re(g,d))}return e.nodeType===1&&Re(e,s),s}function U(e,{capability:t="data",exclude:n=null,inherit:o=!0}={}){if(!e)throw new TypeError("createContentView requires a DOM context");let r=e.nodeType===9?e:e.ownerDocument;if(!r?.implementation?.createHTMLDocument)throw new TypeError("createContentView requires an HTML DOM implementation");let i=r.implementation.createHTMLDocument(""),c=new WeakMap,l=new WeakMap,s=e.nodeType===9?e.documentElement:e;n&&i.documentElement.matches(n);let u=q(t),a=be(s,i,t,u,n,s,o,{cloneToLive:c,liveToClone:l});e.nodeType===9&&a&&(i.replaceChild(a,i.documentElement),l.set(e,i),c.set(i,e));let g=d=>{if(Ye.test(d))throw new Error(`Filtered content queries do not support stateful selector: ${d}`)};return{root:a,document:i,capability:t,selector:q(t),cloneToLive:c,liveToClone:l,original(d){return c.get(d)||null},cloneOf(d){return l.get(d)||null},query(d,x=a){return g(d),Array.from(x.querySelectorAll(d),m=>c.get(m)||m)},text(d=a){return(d===a?a:l.get(d))?.textContent||""},html(d=a){return(d===a?a:l.get(d))?.innerHTML??""},clone(d=a){let x=d===a?a:l.get(d);return x?i.importNode(x,!0):null}}}function Ne(e){return e&&e.nodeType===1&&e.tagName==="SCRIPT"&&e.hasAttribute&&e.hasAttribute("data-rules-name")}function Qe(e){return e?(e.nodeType===9||e.nodeType===11,e):null}var R={semanticExclude:!0,find(e,t,n={}){let o=Qe(e);if(!o||!o.querySelectorAll)return[];let r=Array.from(o.querySelectorAll(t));n.includeRulesTag||(r=r.filter(l=>!Ne(l)));let i=[];n.skip&&i.push(n.skip);let c=n.templateAttr===null?null:n.templateAttr||"cms-template";if(c&&i.push("["+c+"]"),i.length){let l=i.join(", ");r=r.filter(s=>!s.closest||!s.closest(l))}return r},parent(e){return e?e.parentElement:null},children(e){return e?Array.from(e.children):[]},text(e,t){if(t===void 0)return(e.textContent||"").trim();e.textContent=t},attr(e,t,n){if(n===void 0)return e.hasAttribute&&e.hasAttribute(t)?e.getAttribute(t):null;e.setAttribute(t,n)},removeAttr(e,t){e&&e.removeAttribute&&e.removeAttribute(t)},prop(e,t,n){if(n===void 0){let o=e?e[t]:void 0;return o!==void 0?o:null}e[t]=n},clone(e){return e.cloneNode(!0)},insertAt(e,t,n){let o=e.children[n]||null;e.insertBefore(t,o)},remove(e){e&&e.parentNode&&e.parentNode.removeChild(e)},replaceWith(e,t){if(!e||!e.parentNode)throw new Error("dom.replaceWith: node has no parent");let o=e.ownerDocument.createElement("template");o.innerHTML=t;let r=o.content.firstElementChild;if(!r)throw new Error("dom.replaceWith: html did not parse to an element");return e.parentNode.replaceChild(r,e),r},stripIds(e){let t=0;return e.id&&(e.removeAttribute("id"),t++),(e.querySelectorAll?e.querySelectorAll("[id]"):[]).forEach(o=>{o.removeAttribute("id"),t++}),t},sameNode(e,t){return e===t}};function M(e,t={}){if(t.exclude===null)return R;let n=[q("data"),t.exclude].filter(Boolean).join(", "),o=e.nodeType===9?e:e.ownerDocument||e,r=e.nodeType===9?e.documentElement:e;if(!t._write&&!r?.matches?.(n)&&!r?.querySelector?.(n)&&!o?.querySelector?.(n))return R;let i=e.nodeType===9||!e.isConnected?e:e.ownerDocument,c=e.nodeType===9?e:e.ownerDocument,l=null,s=()=>l||(l=U(i,{capability:"data",exclude:t.exclude||null})),u=m=>{let p=s();return m===e?p.cloneOf(e):p.cloneOf(m)||m},a=m=>l?.original(m)||m,g=(m,p)=>{s().liveToClone.set(m,p),s().cloneToLive.set(p,m);let f=m.childNodes||[],h=p.childNodes||[];for(let y=0;y<Math.min(f.length,h.length);y++)g(f[y],h[y])},d=(m,p,f,h)=>{let y=Array.from(m.childNodes).filter(T=>l.cloneOf(T)),O=y[0];for(;O&&y.includes(O);)O=O.nextSibling;for(let T of y)T.remove();if(f==="innerHTML"){let T=c.createElement("template");T.innerHTML=h;for(let S of Array.from(T.content.childNodes))m.insertBefore(S,O||null)}else m.insertBefore(c.createTextNode(h),O||null);let E=U(m,{capability:"data",exclude:t.exclude||null});p.replaceChildren(...Array.from(E.root?.childNodes||[]));let w=T=>{let S=E.original(T);S&&(l.liveToClone.set(S,T),l.cloneToLive.set(T,S));for(let ce of Array.from(T.childNodes||[]))w(ce)};for(let T of Array.from(p.childNodes))w(T)},x={...R,find(m,p,f={}){let h=u(m);if(!h?.querySelectorAll)return[];let y=s().query(p,h);f.includeRulesTag||(y=y.filter(E=>!Ne(E)));let A=[];f.skip&&A.push(f.skip);let O=f.templateAttr===null?null:f.templateAttr||"cms-template";if(O&&A.push(`[${O}]`),A.length){let E=A.join(", ");y=y.filter(w=>!w.closest?.(E))}return y},parent(m){let p=u(m)?.parentElement;return p?a(p):null},children(m){return Array.from(u(m)?.children||[],a)},text(m,p){if(p===void 0){let y=a(m);return!y?.matches?.(n)&&!y?.querySelector?.(n)?R.text(y):(u(m)?.textContent||"").trim()}let f=a(m);if(!f?.matches?.(n)&&!f?.querySelector?.(n)&&!l){R.text(f,p);return}let h=s().cloneOf(f);h?d(f,h,"textContent",p):R.text(f,p)},attr(m,p,f){let h=a(m);if(f===void 0)return R.attr(h,p);R.attr(h,p,f);let y=l?.cloneOf(h);y&&(R.attr(y,p,f),U(h,{capability:"data",exclude:t.exclude||null}).root===null&&(y.remove(),l.liveToClone.delete(h),l.cloneToLive.delete(y)))},removeAttr(m,p){let f=a(m);R.removeAttr(f,p);let h=l?.cloneOf(f);h&&R.removeAttr(h,p)},prop(m,p,f){let h=a(m);if(f===void 0)return p==="innerHTML"||p==="outerHTML"||p==="textContent"||p==="innerText"?R.prop(u(h),p):R.prop(h,p);let y=p==="innerHTML"||p==="textContent"||p==="innerText",A=y?s().cloneOf(h):l?.cloneOf(h);A&&y?d(h,A,p,f):(R.prop(h,p,f),A&&R.prop(A,p,f))},clone(m){let p=u(m);return p===m?R.clone(m):c.importNode(p,!0)},insertAt(m,p,f){let h=a(m),A=x.children(h)[f]||(()=>{for(let w=h.children.length-1;w>=0;w--){let T=h.children[w];if(s().cloneOf(T))return T.nextElementSibling}return h.firstElementChild})(),O=a(p);h.insertBefore(O,A||null);let E=s().cloneOf(h);if(E){let w=s().cloneOf(O);w||(w=c.importNode(O,!0),g(O,w));let T=Array.from(E.children);E.insertBefore(w,T[f]||null)}},remove(m){let p=a(m),f=s().cloneOf(p);R.remove(p),f&&R.remove(f)},replaceWith(m,p){let f=a(m),h=s().cloneOf(f),y=R.replaceWith(f,p);if(h){let A=U(y,{capability:"data",exclude:t.exclude||null});if(!A.root)h.remove();else{h.replaceWith(A.root);let O=E=>{let w=A.original(E);w&&(l.liveToClone.set(w,E),l.cloneToLive.set(E,w));for(let T of Array.from(E.childNodes||[]))O(T)};O(A.root)}}return y}};return x}var L=R;var z={extract:(e,t,n={})=>N(M(e,n),e,t,n),apply:(e,t,n,o={})=>P(M(e,{...o,_write:!0}),e,t,n,o),findRulesIn:(e,t)=>D(L,e,t),findRules:(e,t)=>re(L,e,t),bind:(e,t,n={})=>{let o=re(L,e,t);if(!o){let r=typeof t=="string"?`data-rules-name~="${t}"`:"the provided rules object";throw new Error(`hyper-html-api: could not resolve rules for ${r}`)}return{...o,get:()=>N(M(e,n),e,o.rules,n),set:r=>P(M(e,{...n,_write:!0}),e,o.rules,r,n)}},parseStrict:ne,parseRelaxed:B,ruleAttrIndex:b,splitRule:Y,errors:V,DOM_PROPERTIES:F},ie=e=>!!e&&typeof e.nodeType=="number";function Ce(e){let t=e&&e.ownerDocument?e.ownerDocument:e,n=L.find(t,"script[data-rules-name]",{includeRulesTag:!0});if(n.length===0)throw new Error('hyper-html-api: no rules tag found. Add <script type="application/json" data-rules-name="\u2026" data-rules-version="1"> or pass rules.');if(n.length>1)throw new Error('hyper-html-api: multiple rules tags found; pass a name, e.g. extractData("api").');return D(L,t).rules}function le(e,t){let n=ie(e)?e:document,o=ie(e)?t:e;return o===void 0?N(M(n),n,Ce(n)):z.bind(n,o).get()}function se(e,t,n){if(!ie(e))throw new Error("hyper-html-api: applyData(root, data, source?) needs a DOM root as the first argument.");return n===void 0?P(M(e),e,Ce(e),t):z.bind(e,n).set(t),e}var Ze={engine:z,extractData:le,applyData:se},et=Ze;return Le(tt);})();
6
6
  window.clay = window.clay || {};
7
7
  window.clay.extractData = hyperHtmlApiData.extractData;
8
8
  // clayjs-level sugar: applyData(data) writes into the whole document, matching
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@panphora/clayjs",
3
- "version": "1.2.0",
3
+ "version": "1.3.0",
4
4
  "type": "module",
5
5
  "description": "clayjs: malleable HTML files. Save lifecycle for self-saving HTML.",
6
6
  "license": "MIT-0",
@@ -36,6 +36,7 @@
36
36
  "./*": "./entries/*"
37
37
  },
38
38
  "files": [
39
+ "packed-contract.json",
39
40
  "entries/",
40
41
  "dist/",
41
42
  "THIRD-PARTY-NOTICES.md",
@@ -54,7 +55,10 @@
54
55
  "prepublishOnly": "npm test && npm run test:conformance && npm run test:standalone",
55
56
  "dev": "node tests/stub-server.mjs",
56
57
  "build": "node build.js",
57
- "deploy": "node build.js && wrangler deploy && git rev-parse HEAD > .deploy"
58
+ "propagate:foundation": "node scripts/propagate-foundation.mjs",
59
+ "check:foundation": "node scripts/propagate-foundation.mjs --check",
60
+ "deploy": "node build.js && wrangler deploy && git rev-parse HEAD > .deploy",
61
+ "build:parse5": "node scripts/build-parse5-vendor.mjs"
58
62
  },
59
63
  "devDependencies": {
60
64
  "@esm-bundle/chai": "^4.3.4-fix.0",
@@ -64,6 +68,7 @@
64
68
  "esbuild": "^0.28.2",
65
69
  "jest": "^29.7.0",
66
70
  "jest-environment-jsdom": "^29.7.0",
71
+ "parse5": "^7.3.0",
67
72
  "playwright": "1.62.1"
68
73
  },
69
74
  "hyper": {
@@ -0,0 +1,17 @@
1
+ {
2
+ "load": [
3
+ "@panphora/clayjs/entries/all.js",
4
+ "@panphora/clayjs/entries/clay.js",
5
+ "@panphora/clayjs/entries/clay-data.js",
6
+ "@panphora/clayjs/entries/clay-dom.js",
7
+ "@panphora/clayjs/entries/clay-events.js",
8
+ "@panphora/clayjs/entries/clay-internals.js",
9
+ "@panphora/clayjs/entries/clay-options.js",
10
+ "@panphora/clayjs/entries/clay-ui.js",
11
+ "@panphora/clayjs/entries/clay-utils.js",
12
+ "@panphora/clayjs/entries/sap.js",
13
+ "@panphora/clayjs/clay.standalone.js"
14
+ ],
15
+ "members": ["THIRD-PARTY-NOTICES.md"],
16
+ "families": ["./entries/*", "./dist/*", "./src/*"]
17
+ }
@@ -54,29 +54,21 @@ let pending = new WeakMap();
54
54
 
55
55
  // Phase 2. The clone is a verbatim cloneNode of the live root and nothing has been
56
56
  // stripped from it yet, so pairing by position is sound HERE and nowhere else.
57
- function freezeSnapshot(clone) {
57
+ function freezeSnapshot(clone, provenance) {
58
58
  pending = new WeakMap();
59
59
 
60
- const liveElements = document.querySelectorAll(FREEZE_SELECTOR);
61
60
  const cloneElements = clone.querySelectorAll(FREEZE_SELECTOR);
62
61
 
63
- log('freezing clone — live:', liveElements.length, 'clone:', cloneElements.length);
62
+ log('freezing clone:', cloneElements.length);
64
63
 
65
- // Counts can only differ if an earlier onSnapshot hook added or removed
66
- // elements, which means alignment is already gone. Restoring nothing beats
67
- // restoring one element's authored content into a different element.
68
- if (liveElements.length !== cloneElements.length) {
69
- console.warn('[save-freeze] live/clone freeze counts differ, skipping freeze restore');
70
- return;
71
- }
72
-
73
- for (let i = 0; i < cloneElements.length; i++) {
74
- if (!originals.has(liveElements[i])) continue;
75
- const original = originals.get(liveElements[i]);
76
- pending.set(cloneElements[i], original);
77
- if (cloneElements[i].innerHTML !== original) {
78
- log('element', i, '— restoring original');
79
- cloneElements[i].innerHTML = original;
64
+ for (const cloneElement of cloneElements) {
65
+ const liveElement = provenance.original(cloneElement);
66
+ if (!liveElement || !originals.has(liveElement)) continue;
67
+ const original = originals.get(liveElement);
68
+ pending.set(cloneElement, original);
69
+ if (cloneElement.innerHTML !== original) {
70
+ log('restoring original');
71
+ cloneElement.innerHTML = original;
80
72
  }
81
73
  }
82
74
  }
@@ -8,7 +8,7 @@ export function disableContentEditableBeforeSave () {
8
8
  addDocumentTransform(docElem => {
9
9
  docElem.querySelectorAll(SELECTOR).forEach(resource => {
10
10
  const originalValue = resource.getAttribute("contenteditable");
11
- resource.setAttribute("inert-contenteditable", originalValue);
11
+ resource.setAttribute("inert-contenteditable", originalValue === null ? "true" : originalValue);
12
12
  resource.removeAttribute("contenteditable");
13
13
  });
14
14
  });
@@ -26,17 +26,16 @@ export function enableContentEditableForAdminOnPageLoad () {
26
26
  // incoming document the same way boot activates the live one.
27
27
  export function enableContentEditable(root = document) {
28
28
  root.querySelectorAll(SELECTOR).forEach(el => {
29
- let val = el.getAttribute("inert-contenteditable");
30
- if (!["false", "plaintext-only"].includes(val)) val = "true";
31
- el.setAttribute("contenteditable", val);
29
+ const val = el.getAttribute("inert-contenteditable");
30
+ el.setAttribute("contenteditable", ["", "false", "plaintext-only"].includes(val) ? val : "true");
32
31
  el.removeAttribute("inert-contenteditable");
33
32
  });
34
33
  }
35
34
 
36
35
  export function disableContentEditable() {
37
36
  document.querySelectorAll(SELECTOR).forEach(el => {
38
- const val = el.getAttribute("contenteditable") || "true";
39
- el.setAttribute("inert-contenteditable", val);
37
+ const val = el.getAttribute("contenteditable");
38
+ el.setAttribute("inert-contenteditable", val === null ? "true" : val);
40
39
  el.removeAttribute("contenteditable");
41
40
  });
42
41
  }
@@ -86,18 +86,13 @@ export default function enablePersistentFormInputValues(filterBySelector = PERSI
86
86
  // programmatically without firing an input event, and resolves textarea
87
87
  // data-value into real textContent). Matched by index per selector; another
88
88
  // hook mutating the clone could diverge the lists, hence the per-type loops.
89
- onSnapshot((doc) => {
89
+ onSnapshot((doc, provenance) => {
90
90
  const finalize = (selector) => {
91
- const live = document.querySelectorAll(selector);
92
91
  const cloned = doc.querySelectorAll(selector);
93
- // Index pairing only holds while the two lists describe the same tree. If a
94
- // hook ever diverges them, writing one control's value into another's is
95
- // worse than writing none, so fail loudly instead of silently.
96
- if (live.length !== cloned.length) {
97
- console.warn('[persist] live/clone counts differ for', selector, '— skipping');
98
- return;
99
- }
100
- cloned.forEach((c, i) => finalizeControlForSave(c, live[i]));
92
+ cloned.forEach((control) => {
93
+ const live = provenance.original(control);
94
+ if (live) finalizeControlForSave(control, live);
95
+ });
101
96
  };
102
97
  finalize(inputSelector);
103
98
  finalize(textareaSelector);
@@ -26,6 +26,17 @@ let saveInProgress = false;
26
26
  const SAVE_PATH = '/_/save';
27
27
  const SAVE_TIMEOUT_MS = 12000;
28
28
 
29
+ // Listeners for "the host took these exact bytes". This is the only place in the
30
+ // library that knows both the body that went out and that the host accepted it, and
31
+ // the two together are what make the bytes a fact about the file rather than a guess:
32
+ // a capture says what we would send, a success says a write happened, and only their
33
+ // conjunction says what is on disk. The source map uses it to re-model, so the next
34
+ // save is measured from the file as it now is.
35
+ //
36
+ // Not a DOM event, deliberately. The whole document rides in the argument, and a
37
+ // CustomEvent would put it on a bus any script on the page can listen to.
38
+ const saveAcceptedHooks = [];
39
+
29
40
  /**
30
41
  * Check if a save is currently in progress.
31
42
  * @returns {boolean}
@@ -34,6 +45,29 @@ export function isSaveInProgress() {
34
45
  return saveInProgress;
35
46
  }
36
47
 
48
+ /**
49
+ * Run a callback with the bytes the host just accepted.
50
+ *
51
+ * Called once per accepted save, after the response, with the exact body that was
52
+ * sent. Never called for a refused, failed or skipped save, and never in test mode,
53
+ * where nothing reached a host.
54
+ *
55
+ * @param {Function} callback - Receives the accepted HTML string
56
+ */
57
+ export function onSaveAccepted(callback) {
58
+ saveAcceptedHooks.push(callback);
59
+ }
60
+
61
+ function notifySaveAccepted(html) {
62
+ for (const hook of saveAcceptedHooks) {
63
+ try {
64
+ hook(html);
65
+ } catch (err) {
66
+ console.error('clayjs: onSaveAccepted hook failed:', err);
67
+ }
68
+ }
69
+ }
70
+
37
71
  // =============================================================================
38
72
  // RE-EXPORTS FROM SNAPSHOT (for backwards compat)
39
73
  // =============================================================================
@@ -569,6 +603,7 @@ export function saveHtml(html, callback = () => {}) {
569
603
  // is busy by the save that just finished.
570
604
  .then(result => {
571
605
  saveInProgress = false;
606
+ if (result.ok) notifySaveAccepted(html);
572
607
  done(result);
573
608
  });
574
609
  });
@@ -30,7 +30,9 @@
30
30
  * ┌─────────────────────────┐
31
31
  * │ 4. SERIALIZE │
32
32
  * │ "<!DOCTYPE html>" │
33
- * │ + outerHTML │
33
+ * │ + outerHTML, unless a │
34
+ * │ save renderer is set │
35
+ * │ (setSaveRenderer) │
34
36
  * │ │
35
37
  * │ → sent to server │
36
38
  * └─────────────────────────┘
@@ -40,6 +42,7 @@ import { stripExtensionNoise } from '../lib/extension-noise.js';
40
42
  import { restoreAuthoredUrls } from '../lib/authored-url.js';
41
43
  import { STRIP_FROM_SAVE, STRIP_FROM_COMPARISON, STRIP_FROM_DIRTY_CHECK, NO_TRIGGER_AUTOSAVE_SELECTOR, SNAPSHOT_REMOVE_SELECTOR } from '../lib/region-policy.js';
42
44
  import { TAB_LOCAL_ROOT_ATTRS } from '../lib/root-attrs.js';
45
+ import { createContentView } from '../lib/content-dom.js';
43
46
 
44
47
  // =============================================================================
45
48
  // HOOK REGISTRIES
@@ -47,6 +50,20 @@ import { TAB_LOCAL_ROOT_ATTRS } from '../lib/root-attrs.js';
47
50
 
48
51
  const snapshotHooks = []; // Phase 2: Always run (form sync)
49
52
  const documentTransforms = []; // Phase 3a: Save and change check (strip admin)
53
+ const snapshotProvenance = new WeakMap();
54
+
55
+ // Phase 4, and there is only ever one. Everything else in this file is a list
56
+ // because many things want a turn; serializing the document is one decision, so a
57
+ // second renderer would be two answers to "what bytes does this save send".
58
+ let saveRenderer = null;
59
+
60
+ export function originalSnapshotNode(cloneNode) {
61
+ return snapshotProvenance.get(cloneNode) || null;
62
+ }
63
+
64
+ function stripSnapshotRegions(clone) {
65
+ for (const el of clone.querySelectorAll(SNAPSHOT_REMOVE_SELECTOR)) el.remove();
66
+ }
50
67
 
51
68
  /**
52
69
  * Run every authored handler of one kind over a clone, and never let one of them
@@ -63,6 +80,8 @@ const documentTransforms = []; // Phase 3a: Save and change check (strip admin)
63
80
  */
64
81
  function runAuthoredHandlers(clone, attr) {
65
82
  for (const el of clone.querySelectorAll(`[${attr}]`)) {
83
+ stripSnapshotRegions(clone);
84
+ if (el !== clone && !clone.contains(el)) continue;
66
85
  try {
67
86
  new Function(el.getAttribute(attr)).call(el);
68
87
  } catch (err) {
@@ -81,6 +100,51 @@ export function onSnapshot(callback) {
81
100
  snapshotHooks.push(callback);
82
101
  }
83
102
 
103
+ /**
104
+ * Replace the save-domain serializer.
105
+ *
106
+ * Given the prepared save clone and the bytes `clone.outerHTML` would have sent, it
107
+ * returns the bytes to send instead. The source map (plugins/source.js) is the only
108
+ * caller: it renders the file's own bytes back out with only the changed regions
109
+ * reprinted, so a save stops rewriting 88% of an authored document's lines.
110
+ *
111
+ * It runs on the save path with a person's document in its hands, so the contract is
112
+ * narrow. It must return a complete document. It must not touch the clone, which is
113
+ * also the source of both comparison baselines. And it must be able to answer with
114
+ * `today` unchanged, because that is what it has to do whenever it is not certain:
115
+ * the caller here treats a throw the same way, so the floor of any renderer is the
116
+ * serialization it replaced.
117
+ *
118
+ * Comparison and dirty-check captures deliberately do NOT go through it. Those
119
+ * strings are only ever compared against each other, so they gain nothing from
120
+ * source fidelity and would pay a full render on every keystroke's dirty check.
121
+ *
122
+ * @param {?Function} renderer - (clone, today) => string, or null to restore outerHTML
123
+ */
124
+ export function setSaveRenderer(renderer) {
125
+ saveRenderer = renderer;
126
+ }
127
+
128
+ /**
129
+ * The bytes a save sends: the prepared clone, through the save renderer if one is
130
+ * installed.
131
+ *
132
+ * The try/catch is not defensive noise around a renderer that already catches its
133
+ * own errors. That one catches in order to COUNT, and to fire the event that says a
134
+ * document reprinted; this one is the guarantee that no bug in an optional plugin can
135
+ * cost somebody a save.
136
+ */
137
+ function serializeSaveClone(clone) {
138
+ const today = "<!DOCTYPE html>" + clone.outerHTML;
139
+ if (!saveRenderer) return today;
140
+ try {
141
+ return saveRenderer(clone, today);
142
+ } catch (err) {
143
+ console.error('clayjs: save renderer failed, sending the full serialization', err);
144
+ return today;
145
+ }
146
+ }
147
+
84
148
  /**
85
149
  * Register a transform that runs over a detached clone when preparing to save.
86
150
  * Use for: stripping admin elements, cleanup.
@@ -116,7 +180,7 @@ function clonePreventingOnclone(node, deep = true) {
116
180
  finally { window.__preventOnclone = prev; }
117
181
  }
118
182
 
119
- export function captureSnapshot({ flushUndo = true } = {}) {
183
+ export function captureSnapshot({ flushUndo = true, authored = true } = {}) {
120
184
  // Force-close any pending undo idle batch BEFORE cloning the DOM, so the
121
185
  // snapshot reflects a clean undo boundary. Without this, a save that fires
122
186
  // mid-typing would leave the idle batch open across the save boundary, and
@@ -129,10 +193,27 @@ export function captureSnapshot({ flushUndo = true } = {}) {
129
193
  window.clay.undo.flush();
130
194
  }
131
195
 
132
- const clone = clonePreventingOnclone(document.documentElement);
196
+ const view = createContentView(document.documentElement, { capability: 'snapshot' });
197
+ const clone = view.root;
198
+ // A <template>'s children are not its childNodes, they are its content fragment's,
199
+ // so walking childNodes alone leaves everything inside a template with no provenance.
200
+ // Nothing noticed until the source map asked which live node a clone node came from
201
+ // and got null for every node in a template, which reprinted the whole template on
202
+ // every save. createContentView already maps the fragment itself, so descending into
203
+ // it is all that was missing.
204
+ const remember = (node) => {
205
+ const live = view.original(node);
206
+ if (live) snapshotProvenance.set(node, live);
207
+ const children = node.nodeType === 1 && node.tagName === 'TEMPLATE' && node.content
208
+ ? node.content.childNodes
209
+ : node.childNodes;
210
+ for (const child of children || []) remember(child);
211
+ };
212
+ remember(clone);
133
213
 
134
214
  for (const hook of snapshotHooks) {
135
- hook(clone);
215
+ stripSnapshotRegions(clone);
216
+ hook(clone, { original: originalSnapshotNode });
136
217
  }
137
218
 
138
219
  // Put back any URL clay rewrote at runtime (cache-bust, refetch-on-save) so
@@ -140,11 +221,13 @@ export function captureSnapshot({ flushUndo = true } = {}) {
140
221
  // authored handler sees the same URLs the file will.
141
222
  restoreAuthoredUrls(clone);
142
223
 
143
- runAuthoredHandlers(clone, 'onbeforesnapshot');
224
+ stripSnapshotRegions(clone);
225
+ // authored: false is for a capture that is not a save and not a frame the page will
226
+ // see. The attributes hold page-author JavaScript, so running them is a side effect
227
+ // the author asked for once per save, not once per capture.
228
+ if (authored) runAuthoredHandlers(clone, 'onbeforesnapshot');
144
229
 
145
- for (const el of clone.querySelectorAll(SNAPSHOT_REMOVE_SELECTOR)) {
146
- el.remove();
147
- }
230
+ stripSnapshotRegions(clone);
148
231
 
149
232
  // Browser-extension noise (password-manager menus, Grammarly overlays, and
150
233
  // marker attributes on real inputs) is not page content. Drop it from every
@@ -159,17 +242,21 @@ export function captureSnapshot({ flushUndo = true } = {}) {
159
242
  * Mutates the clone — only call once per snapshot.
160
243
  *
161
244
  * @param {HTMLElement} clone - A snapshot from captureSnapshot()
162
- * @returns {string} Full HTML string ready for server
245
+ * @returns {HTMLElement} The same clone, in the save domain
163
246
  */
164
- function prepareCloneForSave(clone) {
247
+ function prepareCloneForSave(clone, { authored = true } = {}) {
248
+ stripSnapshotRegions(clone);
165
249
  // Run inline [onbeforesave] handlers
166
- runAuthoredHandlers(clone, 'onbeforesave');
250
+ if (authored) runAuthoredHandlers(clone, 'onbeforesave');
167
251
 
168
252
  // Run registered prepare hooks ([freeze]/[save-freeze] innerHTML restore lives here)
169
253
  for (const hook of documentTransforms) {
254
+ stripSnapshotRegions(clone);
170
255
  hook(clone);
171
256
  }
172
257
 
258
+ stripSnapshotRegions(clone);
259
+
173
260
  // Strip [no-save] / legacy [save-remove] LAST (snapshot-algorithm step 7): a
174
261
  // prepare hook (freeze restore) can re-inject [no-save] content into the clone,
175
262
  // so the strip must run after the hooks or that content leaks to disk.
@@ -177,7 +264,29 @@ function prepareCloneForSave(clone) {
177
264
  el.remove();
178
265
  }
179
266
 
180
- return "<!DOCTYPE html>" + clone.outerHTML;
267
+ return clone;
268
+ }
269
+
270
+ /**
271
+ * The save-domain clone, for a caller that needs the tree rather than the bytes.
272
+ *
273
+ * The source map pairs against this rather than against the live DOM, because this is
274
+ * the tree in the file's own domain: edit mode deactivated back to the inert
275
+ * attribute forms, [no-save] gone, every transform run. Every node in it that came
276
+ * from the page can be traced back with originalSnapshotNode.
277
+ *
278
+ * Emits no snapshot-ready event: this capture must never feed the send pipeline.
279
+ *
280
+ * A caller that is only INSPECTING the tree passes `{ flushUndo: false, authored:
281
+ * false }`. Both defaults are right for a save and wrong for anything else: flushing
282
+ * closes the undo batch, so a capture taken mid-typing splits the user's undo history
283
+ * at a point they did not make, and the authored handlers are page JavaScript that is
284
+ * meant to run once per save, not once per capture.
285
+ *
286
+ * @returns {HTMLElement}
287
+ */
288
+ export function captureSaveClone({ flushUndo = true, authored = true } = {}) {
289
+ return prepareCloneForSave(captureSnapshot({ flushUndo, authored }), { authored });
181
290
  }
182
291
 
183
292
  /**
@@ -202,9 +311,12 @@ export function captureForComparison({ flushUndo = true } = {}) {
202
311
 
203
312
  // Run registered prepare hooks
204
313
  for (const hook of documentTransforms) {
314
+ stripSnapshotRegions(clone);
205
315
  hook(clone);
206
316
  }
207
317
 
318
+ stripSnapshotRegions(clone);
319
+
208
320
  return "<!DOCTYPE html>" + clone.outerHTML;
209
321
  }
210
322
 
@@ -232,9 +344,12 @@ export function captureForDirtyCheck({ flushUndo = true } = {}) {
232
344
  }
233
345
 
234
346
  for (const hook of documentTransforms) {
347
+ stripSnapshotRegions(clone);
235
348
  hook(clone);
236
349
  }
237
350
 
351
+ stripSnapshotRegions(clone);
352
+
238
353
  return "<!DOCTYPE html>" + clone.outerHTML;
239
354
  }
240
355
 
@@ -262,8 +377,10 @@ export function captureForComparisonAndDirty({ flushUndo = true } = {}) {
262
377
  el.remove();
263
378
  }
264
379
  for (const hook of documentTransforms) {
380
+ stripSnapshotRegions(clone);
265
381
  hook(clone);
266
382
  }
383
+ stripSnapshotRegions(clone);
267
384
  const forComparison = "<!DOCTYPE html>" + clone.outerHTML;
268
385
 
269
386
  let forDirty = forComparison;
@@ -272,8 +389,10 @@ export function captureForComparisonAndDirty({ flushUndo = true } = {}) {
272
389
  el.remove();
273
390
  }
274
391
  for (const hook of documentTransforms) {
392
+ stripSnapshotRegions(dirtyClone);
275
393
  hook(dirtyClone);
276
394
  }
395
+ stripSnapshotRegions(dirtyClone);
277
396
  forDirty = "<!DOCTYPE html>" + dirtyClone.outerHTML;
278
397
  }
279
398
 
@@ -324,20 +443,24 @@ export function captureForSaveAndComparison({ emitForSync = true } = {}) {
324
443
  // Save clone: run hooks (freeze restore lives here), THEN strip [no-save]/[save-remove]
325
444
  // LAST (snapshot-algorithm step 7) so freeze-restored [no-save] content can't leak to disk.
326
445
  for (const hook of documentTransforms) {
446
+ stripSnapshotRegions(clone);
327
447
  hook(clone);
328
448
  }
449
+ stripSnapshotRegions(clone);
329
450
  for (const el of clone.querySelectorAll(STRIP_FROM_SAVE)) {
330
451
  el.remove();
331
452
  }
332
- const forSave = "<!DOCTYPE html>" + clone.outerHTML;
453
+ const forSave = serializeSaveClone(clone);
333
454
 
334
455
  // Compare clone: strip every autosave-off region, then run hooks
335
456
  for (const el of compareClone.querySelectorAll(STRIP_FROM_COMPARISON)) {
336
457
  el.remove();
337
458
  }
338
459
  for (const hook of documentTransforms) {
460
+ stripSnapshotRegions(compareClone);
339
461
  hook(compareClone);
340
462
  }
463
+ stripSnapshotRegions(compareClone);
341
464
  const forComparison = "<!DOCTYPE html>" + compareClone.outerHTML;
342
465
 
343
466
  // Dirty clone: same shape as the compare clone, one selector weaker.
@@ -347,8 +470,10 @@ export function captureForSaveAndComparison({ emitForSync = true } = {}) {
347
470
  el.remove();
348
471
  }
349
472
  for (const hook of documentTransforms) {
473
+ stripSnapshotRegions(dirtyClone);
350
474
  hook(dirtyClone);
351
475
  }
476
+ stripSnapshotRegions(dirtyClone);
352
477
  forDirty = "<!DOCTYPE html>" + dirtyClone.outerHTML;
353
478
  }
354
479
 
@@ -400,8 +525,10 @@ export function captureForMerge() {
400
525
  })(compareClone, clone);
401
526
 
402
527
  for (const hook of documentTransforms) {
528
+ stripSnapshotRegions(clone);
403
529
  hook(clone);
404
530
  }
531
+ stripSnapshotRegions(clone);
405
532
  for (const el of clone.querySelectorAll(STRIP_FROM_SAVE)) {
406
533
  el.remove();
407
534
  }
@@ -410,8 +537,10 @@ export function captureForMerge() {
410
537
  el.remove();
411
538
  }
412
539
  for (const hook of documentTransforms) {
540
+ stripSnapshotRegions(compareClone);
413
541
  hook(compareClone);
414
542
  }
543
+ stripSnapshotRegions(compareClone);
415
544
 
416
545
  return { saveClone: clone, compareClone, pairMap };
417
546
  }
@@ -437,7 +566,7 @@ export function captureForSave({ emitForSync = true } = {}) {
437
566
  }));
438
567
  }
439
568
 
440
- return prepareCloneForSave(clone);
569
+ return serializeSaveClone(prepareCloneForSave(clone));
441
570
  }
442
571
 
443
572
  /**