// snippets/Loading/Content-Visibility.js | sha256:018cf5490aea014a | https://github.com/nucliweb/webperf-snippets/blob/main/snippets/Loading/Content-Visibility.js (()=>{function e(e){const t=e.getRootNode(),n=e=>{try{return 1===t.querySelectorAll(e).length}catch{return!1}},i=[];let o=e;for(;o&&1===o.nodeType;){if(o.id&&n(`#${window.CSS.escape(o.id)}`)){i.unshift(`#${window.CSS.escape(o.id)}`);break}const e=o.localName;let t=e+Array.from(o.classList).slice(0,2).map(e=>`.${window.CSS.escape(e)}`).join("");const s=o.parentNode?Array.from(o.parentNode.children):[o];if(s.filter(e=>e.matches(t)).length>1&&(t+=`:nth-of-type(${s.filter(t=>t.localName===e).indexOf(o)+1})`),i.unshift(t),n(i.join(" > ")))break;o=o.parentElement}return i.join(" > ")}function t(e){return!/\d/.test(e)}window.analyzeContentVisibilityOpportunities=function(t={}){const{threshold:n=0,minHeight:i=100,minChildren:o=5}=t,s=window.innerHeight,r=[],l=new Set;function d(e){const t=.2*e;return t<5?"Low (~"+t.toFixed(1)+"ms)":t<20?"Medium (~"+t.toFixed(1)+"ms)":"High (~"+t.toFixed(1)+"ms)"}function c(t){if(l.has(t)||function(e){let t=e.parentElement;for(;t;){if(l.has(t))return!0;t=t.parentElement}return!1}(t))return;const c=t.getBoundingClientRect(),h=window.getComputedStyle(t);if(h["content-visibility"]&&"visible"!==h["content-visibility"])return;if(c.heightt.childElements-e.childElements),0===r.length||(r.slice(0,20).map(e=>({selector:e.selector,height:e.height,distanceFromViewport:e.distanceFromViewport,childElements:e.childElements,estimatedSavings:e.estimatedSavings})),r.slice(0,10).forEach((e,t)=>{})),{script:"Content-Visibility",status:"ok",count:r.length,details:{highImpact:r.filter(e=>e.estimatedSavings.startsWith("High")).length,threshold:n,minHeight:i,minChildren:o},items:r.slice(0,50).map(e=>({selector:e.selector,height:e.height,distanceFromViewport:e.distanceFromViewport,childElements:e.childElements,estimatedSavings:e.estimatedSavings})),issues:[]}};const n=function(){const n={autoElements:[],hiddenElements:[],visibleElements:[],nodeArray:[]};function i(e){const t=e.getBoundingClientRect();return t.top0&&t.left0}return function t(o){const s=window.getComputedStyle(o)["content-visibility"];if(s&&"visible"!==s){const t=function(t){const n=t.getBoundingClientRect(),o=window.getComputedStyle(t);return{selector:e(t),contentVisibility:o["content-visibility"],containIntrinsicSize:o["contain-intrinsic-size"]||"not set",width:Math.round(n.width),height:Math.round(n.height),top:Math.round(n.top+window.scrollY),inViewport:i(t)}}(o);"auto"===s?(n.autoElements.push(t),n.nodeArray.push(o)):"hidden"===s&&n.hiddenElements.push(t)}for(let e=0;et(e.containIntrinsicSize)),n.nodeArray.forEach((e,t)=>{})),n}();return{script:"Content-Visibility",status:"ok",count:n.autoElements.length+n.hiddenElements.length,details:{autoCount:n.autoElements.length,hiddenCount:n.hiddenElements.length},items:[...n.autoElements.map(e=>({...e,type:"auto"})),...n.hiddenElements.map(e=>({...e,type:"hidden"}))],issues:[...n.autoElements.filter(e=>t(e.containIntrinsicSize)).map(e=>({code:"missing-intrinsic-size",severity:"warning",message:`${e.selector}: missing contain-intrinsic-size (CLS risk)`,target:e.selector})),...n.autoElements.length+n.hiddenElements.length===0?[{code:"not-used",severity:"info",message:"No content-visibility usage found. Consider content-visibility: auto on below-the-fold sections to reduce the initial render cost."}]:[]]}})();