import{tokenize as e,isTokenURL as r,isTokenString as t}from"@csstools/css-tokenizer";import{parseCommaSeparatedListOfComponentValues as s,replaceComponentValues as i,isTokenNode as o,isFunctionNode as a,isWhitespaceNode as n,isCommentNode as u,stringify as l}from"@csstools/css-parser-algorithms";import c from"node:path";const p=/^([-_a-z0-9]+:)?\/\//i;function rebase(e,r,t){if(e.startsWith("data:"))return!1;if(p.test(e))return!1;if(e.startsWith("/"))return e;if(e.startsWith("#"))return e;try{const r=new URL(e);if(r.port||r.protocol)return!1}catch{}const s=c.posix.resolve(c.posix.join(r,e));return c.posix.relative(t,s)}function serializeString(e){let r="";for(const t of e){const e=t.codePointAt(0);if(void 0!==e)switch(e){case 0:r+=String.fromCodePoint(65533);break;case 127:r+=`\\${e.toString(16)}`;break;case 34:case 39:case 92:r+=`\\${t}`;break;default:if(1<=e&&e<=31){r+=`\\${e.toString(16)} `;break}r+=t}else r+=String.fromCodePoint(65533)}return r}function normalizedDir(e){return c.parse(c.resolve(e.trim())).dir.split(c.sep).join(c.posix.sep)}const f=/^initial-value$/i,m=/^property$/i,v=/^syntax$/i,d=/url\(/i,b=/^url$/i,g=/<url>/i,creator=()=>({postcssPlugin:"postcss-rebase-url",prepare(){const c=new WeakSet,p=new Set;return{postcssPlugin:"postcss-rebase-url",Once(e){e.walkAtRules(m,e=>{if(!e.nodes)return;const r=e.nodes.find(e=>{if("decl"===e.type&&v.test(e.prop))return!0});r&&g.test(r.value)&&p.add(e.params.trim())})},Declaration(v,{result:g}){if(c.has(v))return;if(v.variable&&!p.has(v.prop))return;if(f.test(v.prop)&&"atrule"===v.parent?.type&&m.test(v.parent.name))return;const{from:h}=g.opts;if(!h)return;if(!v.source?.input.from)return;if(!d.test(v.value))return;const S=normalizedDir(h),$=v.source.input.from.trim();if(!$)return;const k=normalizedDir($),z=s(e({css:v.value})),x=i(z,e=>{if(o(e)&&r(e.value)){const r=rebase(e.value[4].value.trim(),k,S);if(r)return e.value[4].value=r,e.value[1]=`url(${serializeString(r)})`,e}if(a(e)&&b.test(e.getName()))for(const r of e.value)if(!n(r)&&!u(r)&&o(r)&&t(r.value)){const t=rebase(r.value[4].value.trim(),k,S);if(t)return r.value[4].value=t,r.value[1]=`"${serializeString(t)}"`,e;break}}),w=l(x);w!==v.value&&(v.value=w,c.add(v))}}}});creator.postcss=!0;export{creator as default,creator as"module.exports"};
