@teacss/directives 0.10.2 → 0.10.4

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.
package/dist/core.js CHANGED
@@ -1 +1 @@
1
- import{a as e,f as t,h as n,m as r,r as i,s as a,t as o,u as s}from"./screen-core-Dplqz5Ta.js";export{a as formatMissingApplyTokens,e as generateApplyPreflights,s as missingApplyTokens,t as prepareApplyUtilities,r as renderStandaloneApplyUtility,n as resolveApplyTokens,i as resolveCssFunction,o as resolveScreenQuery};
1
+ import{c as e,d as t,g as n,h as r,i,o as a,p as o,t as s}from"./screen-core-FmDUvprA.js";export{e as formatMissingApplyTokens,a as generateApplyPreflights,t as missingApplyTokens,o as prepareApplyUtilities,r as renderStandaloneApplyUtility,n as resolveApplyTokens,i as resolveCssFunction,s as resolveScreenQuery};
package/dist/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { MagicStringLike, SourceCodeTransformer, StringifiedUtil, TeacssGenerator } from "@teacss/core";
2
+ import { CssDialect } from "@teacss/config";
2
3
  //#region src/apply.d.ts
3
4
  /**
4
5
  * The rules written after an enclosing rule so far, and the at-rule heads still
@@ -158,13 +159,14 @@ export declare function generateEntry(entryCss: string, options: GenerateEntryOp
158
159
  *
159
160
  * - `@teacss;` → replaced by `layers`
160
161
  * - `@presets` / `@source` / `@shortcut` / `@safelist` / `@custom {}` → stripped (consumed)
161
- * - native `@charset` / `@import` → hoisted in CSS-required order
162
+ * - native `@charset` / `@import`, and the `@layer` statements leading the file → hoisted in CSS-required order;
163
+ * a final `@import` without its `;` gains one
162
164
  * - everything else (plain rules, comments, `@media`, …) → kept in place
163
165
  *
164
166
  * Pure, css-tree-free: exact top-level at-rule ranges are scanned structurally
165
167
  * and spliced out of the original.
166
168
  */
167
- export declare function spliceEntry(entryCss: string, layers: string): string;
169
+ export declare function spliceEntry(entryCss: string, layers: string, dialect?: CssDialect): string;
168
170
  //#endregion
169
171
  //#region src/transform.d.ts
170
172
  export declare function transformDirectives(code: MagicStringLike, generator: TeacssGenerator, options: TransformerDirectivesOptions, filename?: string): Promise<void>;
package/dist/index.js CHANGED
@@ -1,4 +1,5 @@
1
- import{S as e,_ as t,a as n,b as r,c as i,d as a,f as o,g as s,h as c,i as l,l as u,m as d,n as f,o as p,p as m,r as h,s as g,t as _,u as v,v as y,x as b,y as x}from"./screen-core-Dplqz5Ta.js";import{createGenerator as S,expandClassGroups as ee,expandVariantGroup as te,isCssId as C,splitClassTokens as w,toArray as T}from"@teacss/core";import{hasIconFn as E,hasThemeFn as D,transformThemeFn as ne}from"@teacss/rule-helpers";import{ROLE_DIRECTIVES as O,resolveEntryConfig as k,scanTopLevelAtRules as A}from"@teacss/config";import{generateSources as re}from"@teacss/runtime/source-generation";var ie=class e{moved;head;outer;depth;constructor(e,t,n,r){this.moved=e,this.head=t,this.outer=n,this.depth=r}static root(t){return new e({end:t,css:``,open:[]},``,void 0,0)}within(t){return new e(this.moved,t,this,this.depth+1)}place(e){let{moved:t}=this,n=[],r=this;for(;r.depth>0&&t.open[r.depth-1]!==r;)n.push(r),r=r.outer;t.css+=`}`.repeat(t.open.length-r.depth),t.open.length=r.depth;for(let e of n.reverse())t.css+=`${e.head}{`,t.open.push(e);t.css+=e}},ae=class e{hosts;inPlace;plain;nesting;constructor(e,t,n,r){this.hosts=e,this.inPlace=t,this.plain=n,this.nesting=r}static root(t,n,r){return new e([{condition:ie.root(n),selector:t,element:``}],void 0,!1,r)}within(t){let n=this.hosts.map(e=>({...e,condition:e.condition.within(t)}));return new e(n,this.inPlace===void 0?void 0:`&`,this.plain,this.nesting)}nested(n,r){let a=s(n);if(!a)return;let{taking:o,others:c}=a,l,u=this.plain;if(this.hosts.every(e=>e.nested===void 0)){let e=this.hosts[0],{groups:n,plain:a}=m(e.selector);u=!r&&a.length>0,r&&a.length&&n.push({bases:a,element:``}),l=n.map(({bases:n,element:a})=>{let s=t(o,this.anchored(n,o),r);return{condition:e.condition,selector:y(s,a),nested:i(s),element:a}})}else{let e=i(o);l=this.hosts.map(t=>({condition:t.condition.within(t.nested??``),selector:y(o,t.element),nested:e,element:t.element}))}let d=u?n.trim():c.length?i(c):void 0;return new e(l,d,u,this.nesting)}anchored(e,t){return this.nesting===`style-rule`?p(e,t,!1):this.nesting===`scope`?p(e,t,!0):e}standaloneHosts(){let e=this.hosts.map((e,t)=>[t,e.selector]);return this.inPlace===void 0?e:[...e,[void 0,this.inPlace]]}};function oe(e){return e.kind===`rule`?e.rule.block:e.block}function se(e,t){return t.enclosing?t.enclosing.standaloneHosts():[[void 0,t.kind===`rule`?e.slice(...t.rule.prelude):t.host]]}function j(e,t){return Error(`[@teacss] @apply: "${e}" is outside a style rule or @scope${t?` in ${t}`:``}`)}function ce(e,t){return Error(`[@teacss] @apply: "${e}" needs a selector or condition, which a @keyframes block cannot hold${t?` in ${t}`:``}`)}async function le(e,t,n){let i=[],a=[],o=!1,[s,c]=oe(t);for(let l of r(e.source,s,c,e.preprocessor)){if(l.kind===`rule`){await n(l);continue}let r=await fe(e,t,l);r&&(i.push(r.consumedRange),a.push(...r.standalone),o||=r.hasInlineUtility)}if(t.kind===`rule`&&!o&&i.length>0){let n=s;i.every(([t,r])=>{let i=!e.source.slice(n,t).trim();return n=r,i})&&!e.source.slice(n,c).trim()&&e.code.remove(t.rule.start,t.rule.end)}return a}function ue({code:e},t,n,r){let i=t.kind===`rule`?t.rule.end:t.block[1];if(t.kind===`rule`&&!t.enclosing&&n){let{moved:t}=n.hosts[0].condition;t.css&&(e.appendLeft(t.end,t.css+`}`.repeat(t.open.length)),t.css=``,t.open=[])}for(let n of r)n.moved!==void 0&&t.enclosing?t.enclosing.hosts[n.moved].condition.place(n.css):e.appendLeft(i,n.css)}const de=e=>e===`style-rule`||e===`keyframes`;async function fe(e,t,n){let{code:r,source:i,generator:s,applyVariable:u,options:f,filename:p,applyUtilities:m,hoisted:h,nesting:_}=e,y,b;if(n.kind===`at-rule`&&n.name===`apply`)b=n.prelude[1],y=N(i.slice(...n.prelude));else if(n.kind===`declaration`&&u.includes(n.name))b=i[n.end-1]===`;`?n.end-1:n.end,y=w(N(i.slice(...n.value).trim())).map(e=>N(e)).join(` `);else return;if(!y)return;y=pe(y),y=ne(y,s.config.theme,f.throwOnMissing??!0,l(s));let x=w(te(ee(y))),S=await c(x,s),C=v(S);if(C.length&&(f.throwOnMissing??!0))throw Error(`${g(C)}${p?` in ${p}`:``}`);let T=o(S,s.parentOrders,s.config.layers,e=>s.sheetOrder(e));m.set(n.start,S.flatMap(([,e])=>e??[]));let E=[n.start,b+ +(i[b]===`;`)];if(!T.length)return r.remove(...E),{consumedRange:E,hasInlineUtility:!1,standalone:[]};let D,O=!1,k=[];for(let e of T)if(e.kind===`top-level`)_===`top-level`||_===`scope`?k.push({css:e.body}):M(h,e.body);else if(e.kind===`standalone`){D??=se(i,t).map(([e,t])=>[e,a(t)]);let n=e.selector?.startsWith(`@`)??!1;for(let[t,r]of D){let i=d(r,e,p);if(i){if(_===`keyframes`&&!n)throw ce(y,p);n&&de(_)?M(h,i):k.push({css:i,moved:t})}}}else O=!0,r.appendLeft(n.start,e.body);return r.remove(...E),{consumedRange:E,hasInlineUtility:O,standalone:k}}function M(e,t){for(let n of r(t,0,t.length,!1)){let r=t.slice(n.start,n.end);e.includes(r)||e.push(r)}}function N(e){return e.replace(/^(['"])(.*)\1$/,`$2`)}function pe(e){return e.replace(/\/\*[\s\S]*?\*\//g,``)}async function P({code:e,source:t,generator:n,options:r,applyUtilities:i},a,o,s){let c=await h(n,a,(e,n)=>t.slice(e,n),r.throwOnMissing??!0,s);c!==void 0&&(e.overwrite(a.loc.start.offset,a.loc.end.offset,c),o!==void 0&&c.includes(`var(`)&&i.set(a.loc.start.offset,[[0,void 0,`${o}:${c};`,void 0,void 0,void 0,void 0]]))}function F({code:e,source:t,generator:n},r){let i=_(t.slice(...r.prelude),n.config.theme);i&&e.overwrite(r.start,r.block?r.block[0]-1:r.prelude[1],`@media ${i}`)}const I=new Set([`presets`,`source`,`shortcut`,`safelist`,...O]);function L(e,t){let n=new Set;if(!t.includes(`@`))return n;let r=A(t);if(r.some(e=>e.name===`teacss`&&e.prelude===``&&e.terminator!==`block`))return n;for(let t of r)(I.has(t.name)?t.terminator===`;`:t.name===`custom`&&(t.terminator===`block`||t.terminator===`;`))&&(e.remove(t.start,t.end),n.add(t.start));return n}function R(e){return O.some(t=>e.includes(`@${t}`))||e.includes(`@preset`)||e.includes(`@source`)||e.includes(`@shortcut`)||e.includes(`@safelist`)||e.includes(`@custom`)}function z(e){return/^(?:property|font-face|font-palette-values|counter-style)$/i.test(e)}function me(e){return e?.split(`?`).flatMap(e=>/\.(scss|sass|less|styl|stylus)$/.exec(e)?.[1]??[])[0]}function he(e,t){let n=[];for(let r=t+1;r<e.length;r++){let i=e[r];if(i===`\\`)r++;else if(i===e[t])return{close:r,lineBreaks:n};else(i===`
2
- `||i===`\r`||i===`\f`)&&n.push(r)}}function ge(t,n){let r=``,i=0,a=0,o=0,s=0,c=!1;for(let l=0;l<t.length;l++){let u=t[l];if(u===`/`&&t[l+1]===`*`){let e=t.indexOf(`*/`,l+2);l=e===-1?t.length:e+1}else if(n&&u===`/`&&t[l+1]===`/`){let e=t.indexOf(`
3
- `,l),n=e===-1?t.length:e;r+=t.slice(i,l)+` `.repeat(n-l),i=n,l=n-1}else if(u===`\\`)l++;else if(u===`"`||u===`'`){let e=he(t,l);if(e){for(let n of e.lineBreaks)r+=`${t.slice(i,n)} `,i=n+1;l=e.close}else l=b(t,l,t.length)-1}else if(x(t,l)){let r=e(t,l,t.length,n);c=r===-1,l=(c?t.length:r)-1}else if(u===`(`)s++;else if(u===`)`&&s)s--;else if(s&&!n)continue;else u===`}`?a=Math.max(0,a-1):u===`{`&&(o=Math.max(o,++a))}return{source:i?r+t.slice(i):t,blockDepth:o,unclosedUrl:c}}async function B(e,t,i,a){let o=me(a),{source:s,blockDepth:c,unclosedUrl:l}=ge(e.original,o!==void 0),u=T((i.applyVariable??[`--at-apply`])||[]),d=s.includes(`@apply`)||u.some(e=>s.includes(e)),f=s.includes(`@screen`),p=D(s)||E(s),m=R(s)?L(e,s):new Set;if(!d&&!p&&!f)return;if(o===`sass`)throw Error(`TeaCSS directives need braces; indented Sass is not supported, write it as SCSS${a?` in ${a}`:``}`);if(c>512)throw Error(`[@teacss] CSS blocks nest deeper than 512 levels${a?` in ${a}`:``}`);if(l)throw Error(`[@teacss] url() is not closed${a?` in ${a}`:``}`);let h=new Map,g=[],_={options:i,applyVariable:u,generator:t,code:e,source:s,filename:a,preprocessor:o!==void 0,applyUtilities:h,hoisted:g,nesting:`top-level`,literalColors:!1,hasApply:d,hasScreen:f,hasFn:p,stripped:m};await W(_,r(s,0,s.length,_.preprocessor),void 0),g.length&&e.appendLeft(e.original.length,`\n${g.join(``)}`);let v=[...h].sort(([e],[t])=>e-t).flatMap(([,e])=>e),y=await n(t,v,a);y&&e.appendLeft(e.original.length,`\n${y}`)}const V=({source:e,applyVariable:t},[n,r])=>{let i=e.slice(n,r);return i.includes(`@apply`)||t.some(e=>i.includes(e))},H=e=>e===`top-level`||e===`scope`?`style-rule`:e;async function U(e,t,n){let{nesting:r,enclosing:i,literalColors:a}=e;Object.assign(e,t);try{await n()}finally{Object.assign(e,{nesting:r,enclosing:i,literalColors:a})}}async function W(e,t,n){for(let r of t)if(!e.stripped.has(r.start)){if(r.kind===`rule`)await Z(e,r);else if(r.kind===`at-rule`){if(r.name===`apply`)throw j(e.source.slice(...r.prelude),e.filename);await Y(e,r,n)}else if(r.kind===`declaration`){if(n===void 0&&e.hasApply&&e.applyVariable.includes(r.name))throw j(e.source.slice(...r.value),e.filename);await $(e,r)}else await G(e,r.rulesets)}}async function G(e,t){for(let n of t)await q(e,n)}function _e({source:e},{name:t,prelude:n}){return t===`mixin`||t===`include`||e.slice(...n)===`:`}function K(e,t,n,r){let i=0;for(let a=t;a<e.length;a++){let t=e[a];if(t===`\\`)a++;else if(t===`"`||t===`'`)a=b(e,a,e.length)-1;else if(t===n)i++;else if(t===r&&--i===0)return a+1}return e.length}function ve(e){let t=``,n=0;for(let r=0;r<e.length;){let i=e[r],a;if(i===`\\`){r+=2;continue}if(i===`"`||i===`'`)a=b(e,r,e.length);else if(i===`[`)a=K(e,r,`[`,`]`);else if((i===`#`||i===`@`)&&e[r+1]===`{`)a=K(e,r+1,`{`,`}`);else if(e.startsWith(`/*`,r)){let t=e.indexOf(`*/`,r+2);a=t===-1?e.length:t+2}else{r++;continue}t+=e.slice(n,r)+` `.repeat(a-r),n=a,r=a}return t+e.slice(n)}function ye(e){let t=ve(e);return/^[.#]?[\w-]+\s*\(/.test(t)||/[\s)]when[\s(]/.test(t)||t.includes(`:extend(`)}function q(e,t){return U(e,{nesting:H(e.nesting),enclosing:void 0},()=>X(e,t,`&`))}function J({source:e,generator:t},{name:n,prelude:r}){let i=e.slice(...r);if(/^screen$/i.test(n)){let e=_(i,t.config.theme);return e===void 0?void 0:`@media ${e}`}return/^(?:media|supports|container|layer|starting-style)$/i.test(n)?`@${n} ${i}`:void 0}async function Y(e,t,n){e.hasScreen&&t.name===`screen`&&F(e,t);let{block:r}=t;if(!r)return;if(e.preprocessor&&_e(e,t))return q(e,r);if(/keyframes$/i.test(t.name))return U(e,{nesting:`keyframes`,enclosing:e.enclosing},()=>X(e,r,void 0));let i=t.name===`scope`,a=i?`:scope`:n,o;if(e.enclosing&&a===`&`){let n=J(e,t);n!==void 0&&(o=e.enclosing.within(n))}await U(e,{nesting:i&&!e.preprocessor&&e.nesting===`top-level`?`scope`:e.nesting,enclosing:o,literalColors:e.literalColors||z(t.name)},()=>X(e,r,a))}async function X(e,t,n){if(n!==void 0&&e.hasApply&&V(e,t))return Q(e,{kind:`nested`,block:t,host:n,enclosing:e.enclosing});await W(e,r(e.source,t[0],t[1],e.preprocessor),n)}async function Z(e,t){let n=e.source.slice(...t.prelude);if(e.preprocessor&&ye(n))return q(e,t.block);if(e.hasApply&&V(e,[t.start,t.end]))return Q(e,{kind:`rule`,rule:t,enclosing:e.enclosing?.nested(n,e.preprocessor)});await U(e,{nesting:H(e.nesting),enclosing:e.enclosing},()=>W(e,r(e.source,t.block[0],t.block[1],e.preprocessor),`&`))}function be({source:e,nesting:t},n){if(n.enclosing||n.kind!==`rule`)return n.enclosing;let r=e.slice(...n.rule.prelude);return u(r)?ae.root(r,n.rule.end,t):void 0}async function Q(e,t){let n=be(e,t),i=n!==void 0,a=t.kind===`rule`?H(e.nesting):e.nesting,o=await le(e,t,t=>i?Promise.resolve():U(e,{nesting:a,enclosing:void 0},()=>Z(e,t))),s=t.kind===`rule`?t.rule.block:t.block;await U(e,{nesting:a,enclosing:n},async()=>{for(let n of r(e.source,s[0],s[1],e.preprocessor))n.kind===`declaration`?e.applyVariable.includes(n.name)||await $(e,n):n.kind===`at-rule`?n.name!==`apply`&&await Y(e,n,t.kind===`rule`?`&`:t.host):n.kind===`call`?await G(e,n.rulesets):i&&await Z(e,n)}),ue(e,t,n,o)}async function $(e,{name:t,value:n}){if(!e.hasFn)return;let r=e.source.slice(...n);if(D(r)||E(r))for(let i of f(r,n[0]))await P(e,i,t,e.literalColors)}async function xe(e,t){let{base:n,entryFile:r,readSources:i,contents:a}=t,o=await k(e,n,r),s=await S(o.config),c=[],l=[];for(let e of o.config.content?.filesystem??[])e.startsWith(`!`)?l.push(e.slice(1)):c.push(e);let{files:u,sources:d}=a?{files:[],sources:a}:i?await i(c,l):{files:[],sources:[]},f=await re(s,u,d);return{layers:f.getLayers()||``,files:u,unknown:o.unknown,matched:f.matched.size,hasTeacss:o.hasTeacss,hasPreset:!!o.config.presets?.length}}function Se(e,t){let n=[],r=[],i=[];for(let a of A(e))a.name===`charset`&&a.terminator===`;`?(n.push(e.slice(a.start,a.end)),i.push({start:a.start,end:a.end,replacement:``})):a.name===`import`&&a.terminator===`;`?(r.push(e.slice(a.start,a.end)),i.push({start:a.start,end:a.end,replacement:``})):(O.includes(a.name)||a.name===`presets`||a.name===`source`||a.name===`shortcut`||a.name===`safelist`)&&a.terminator===`;`||a.name===`custom`&&(a.terminator===`block`||a.terminator===`;`)?i.push({start:a.start,end:a.end,replacement:``}):a.name===`teacss`&&a.prelude===``&&a.terminator!==`block`&&i.push({start:a.start,end:a.end,replacement:t});i.sort((e,t)=>e.start-t.start);let a=``,o=0;for(let t of i)a+=e.slice(o,t.start)+t.replacement,o=t.end;a+=e.slice(o);let s=a.startsWith(``)?``:``;s&&(a=a.slice(1));let c=[...n,...r];return s+(c.length?`${c.join(`
4
- `)}\n`:``)+a}function Ce(e={}){return{name:`@teacss/directives`,enforce:e.enforce,idFilter:C,transform:(t,n,r)=>B(t,r.generator,e,n)}}export{Ce as default,xe as generateEntry,Se as spliceEntry,B as transformDirectives};
1
+ import{C as e,S as t,_ as n,a as r,b as i,c as a,d as o,f as s,g as c,h as l,i as u,l as d,m as f,n as p,o as m,p as h,r as g,s as _,t as v,u as y,v as b,x,y as S}from"./screen-core-FmDUvprA.js";import{createGenerator as C,expandClassGroups as ee,expandVariantGroup as te,isCssId as w,splitClassTokens as T,toArray as E}from"@teacss/core";import{hasIconFn as D,hasThemeFn as O,transformThemeFn as ne,transformThemeString as k}from"@teacss/rule-helpers";import{ROLE_DIRECTIVES as A,blankLineComments as re,cssDialectOf as j,customPropertyValueEnd as ie,declarationStartsAfter as ae,resolveEntryConfig as oe,scanTopLevelAtRules as M}from"@teacss/config";import{generateSources as se}from"@teacss/runtime/source-generation";var ce=class e{moved;head;outer;depth;constructor(e,t,n,r){this.moved=e,this.head=t,this.outer=n,this.depth=r}static root(t){return new e({end:t,css:``,open:[]},``,void 0,0)}within(t){return new e(this.moved,t,this,this.depth+1)}place(e){let{moved:t}=this,n=[],r=this;for(;r.depth>0&&t.open[r.depth-1]!==r;)n.push(r),r=r.outer;t.css+=`}`.repeat(t.open.length-r.depth),t.open.length=r.depth;for(let e of n.reverse())t.css+=`${e.head}{`,t.open.push(e);t.css+=e}},le=class e{hosts;inPlace;plain;nesting;constructor(e,t,n,r){this.hosts=e,this.inPlace=t,this.plain=n,this.nesting=r}static root(t,n,r){return new e([{condition:ce.root(n),selector:t,element:``}],void 0,!1,r)}within(t){let n=this.hosts.map(e=>({...e,condition:e.condition.within(t)}));return new e(n,this.inPlace===void 0?void 0:`&`,this.plain,this.nesting)}nested(t,r){let i=n(t);if(!i)return;let{taking:a,others:o}=i,s,c=this.plain;if(this.hosts.every(e=>e.nested===void 0)){let e=this.hosts[0],{groups:t,plain:n}=f(e.selector);c=!r&&n.length>0,r&&n.length&&t.push({bases:n,element:``}),s=t.map(({bases:t,element:n})=>{let i=b(a,this.anchored(t,a),r);return{condition:e.condition,selector:S(i,n),nested:d(i),element:n}})}else{let e=d(a);s=this.hosts.map(t=>({condition:t.condition.within(t.nested??``),selector:S(a,t.element),nested:e,element:t.element}))}let l=c?t.trim():o.length?d(o):void 0;return new e(s,l,c,this.nesting)}anchored(e,t){return this.nesting===`style-rule`?_(e,t,!1):this.nesting===`scope`?_(e,t,!0):e}standaloneHosts(){let e=this.hosts.map((e,t)=>[t,e.selector]);return this.inPlace===void 0?e:[...e,[void 0,this.inPlace]]}};function ue(e){return e.kind===`rule`?e.rule.block:e.block}function de(e){return e.enclosing?e.enclosing.standaloneHosts():[[void 0,e.kind===`rule`?e.selector:e.host]]}function N(e,t){return Error(`[@teacss] @apply: "${e}" is outside a style rule or @scope${t?` in ${t}`:``}`)}function fe(e,t){return Error(`[@teacss] @apply: "${e}" needs a selector or condition, which a @keyframes block cannot hold${t?` in ${t}`:``}`)}async function pe(e,t,n){let r=[],i=[],a=!1,[o,s]=ue(t);for(let c of x(e.source,o,s,e.preprocessor)){if(c.kind===`rule`){await n(c);continue}let o=await ge(e,t,c);o&&(r.push(o.consumedRange),i.push(...o.standalone),a||=o.hasInlineUtility)}if(t.kind===`rule`&&!a&&r.length>0){let n=o;r.every(([t,r])=>{let i=!e.source.slice(n,t).trim();return n=r,i})&&!e.source.slice(n,s).trim()&&e.code.remove(t.rule.start,t.rule.end)}return i}function me({code:e},t,n,r){let i=t.kind===`rule`?t.rule.end:t.block[1];if(t.kind===`rule`&&!t.enclosing&&n){let{moved:t}=n.hosts[0].condition;t.css&&(e.appendLeft(t.end,t.css+`}`.repeat(t.open.length)),t.css=``,t.open=[])}for(let n of r)n.moved!==void 0&&t.enclosing?t.enclosing.hosts[n.moved].condition.place(n.css):e.appendLeft(i,n.css)}const he=e=>e===`style-rule`||e===`keyframes`;async function ge(e,t,n){let{code:i,source:u,generator:d,applyVariable:f,options:p,filename:m,applyUtilities:g,hoisted:_,nesting:v}=e,y,b;if(n.kind===`at-rule`&&n.name===`apply`)b=n.prelude[1],y=F(u.slice(...n.prelude));else if(n.kind===`declaration`&&f.includes(n.name))b=u[n.end-1]===`;`?n.end-1:n.end,y=T(F(u.slice(n.value[0],b).trim())).map(e=>F(e)).join(` `);else return;if(!y)return;y=_e(y),y=ne(y,d.config.theme,p.throwOnMissing??!0,r(d));let x=T(te(ee(y))),S=await c(x,d),C=o(S);if(C.length&&(p.throwOnMissing??!0))throw Error(`${a(C)}${m?` in ${m}`:``}`);let w=h(S,d.parentOrders,d.config.layers,e=>d.sheetOrder(e));g.set(n.start,S.flatMap(([,e])=>e??[]));let E=[n.start,u[n.end-1]===`;`?n.end:b];if(!w.length)return i.remove(...E),{consumedRange:E,hasInlineUtility:!1,standalone:[]};let D,O=!1,k=[];for(let e of w)if(e.kind===`top-level`)v===`top-level`||v===`scope`?k.push({css:e.body}):P(_,e.body);else if(e.kind===`standalone`){D??=de(t).map(([e,t])=>[e,s(t)]);let n=e.selector?.startsWith(`@`)??!1;for(let[t,r]of D){let i=l(r,e,m);if(i){if(v===`keyframes`&&!n)throw fe(y,m);n&&he(v)?P(_,i):k.push({css:i,moved:t})}}}else O=!0,i.appendLeft(n.start,e.body);return i.remove(...E),{consumedRange:E,hasInlineUtility:O,standalone:k}}function P(e,t){for(let n of x(t,0,t.length,!1)){let r=t.slice(n.start,n.end);e.includes(r)||e.push(r)}}function F(e){return e.replace(/^(['"])(.*)\1$/,`$2`)}function _e(e){return e.replace(/\/\*[\s\S]*?\*\//g,``)}async function I({code:e,source:t,generator:n,options:r,applyUtilities:i},a,o,s){let c=await u(n,a,(e,n)=>t.slice(e,n),r.throwOnMissing??!0,s);if(c!==void 0)return e.overwrite(a.loc.start.offset,a.loc.end.offset,c),o!==void 0&&c.includes(`var(`)&&i.set(a.loc.start.offset,[[0,void 0,`${o}:${c};`,void 0,void 0,void 0,void 0]]),c}function ve({code:e,source:t,generator:n},r){let i=v(t.slice(...r.prelude),n.config.theme);i&&e.overwrite(r.start,r.block?r.block[0]-1:r.prelude[1],`@media ${i}`)}const ye=new Set([`presets`,`source`,`shortcut`,`safelist`,...A]);function be(e,t,n=`css`){let r=new Set;if(!t.includes(`@`))return r;let i=M(t,!1,n);if(i.some(e=>e.name===`teacss`&&e.prelude===``&&e.terminator!==`block`))return r;for(let t of i)(ye.has(t.name)?t.terminator===`;`:t.name===`custom`&&(t.terminator===`block`||t.terminator===`;`))&&(e.remove(t.start,t.end),r.add(t.start));return r}function xe(e){return A.some(t=>e.includes(`@${t}`))||e.includes(`@preset`)||e.includes(`@source`)||e.includes(`@shortcut`)||e.includes(`@safelist`)||e.includes(`@custom`)}function Se(e){return/^(?:property|font-face|font-palette-values|counter-style)$/i.test(e)}function Ce(e){return e?.split(`?`).flatMap(e=>/\.(scss|sass|less|styl|stylus)$/.exec(e)?.[1]??[])[0]}function we(e,t){let n=[];for(let r=t+1;r<e.length;r++){let i=e[r];if(i===`\\`)r++;else if(i===e[t])return{close:r,lineBreaks:n};else(i===`
2
+ `||i===`\r`||i===`\f`)&&n.push(r)}}function Te(n,r,a){let o=r!==`css`,s=``,c=0,l=0,u=0,d=0,f=!1,p=!0;for(let m=0;m<n.length;m++){let h=n[m];if(h===`/`&&n[m+1]===`*`){let e=n.indexOf(`*/`,m+2);m=e===-1?n.length:e+1;continue}if(o&&h===`/`&&n[m+1]===`/`){let e=m;for(;e<n.length&&n[e]!==`
3
+ `&&n[e]!==`\r`&&n[e]!==`\f`;)e++;s+=n.slice(c,m)+` `.repeat(e-m),c=e,m=e-1;continue}if(o&&p&&ie(n,m,r)>m&&a(/^--[\w-]*/.exec(n.slice(m,m+256))[0])){m=ie(n,m,r)-1,p=!1;continue}if(p=ae(h,p,r),h===`\\`)m++;else if(h===`"`||h===`'`){let e=we(n,m);if(e){for(let t of e.lineBreaks)s+=`${n.slice(c,t)} `,c=t+1;m=e.close}else m=t(n,m,n.length)-1}else if(i(n,m)){let t=e(n,m,n.length,o);f=t===-1,m=(f?n.length:t)-1}else if(h===`(`)d++;else if(h===`)`&&d)d--;else if(d&&!o)continue;else h===`}`?l=Math.max(0,l-1):h===`{`&&(u=Math.max(u,++l))}return{source:c?s+n.slice(c):n,blockDepth:u,unclosedUrl:f}}function Ee(e,t){return e.startsWith(t)&&!/[\w-]/.test(e[t.length]??``)}function L(e,t,n,r){return x(e,t,n,!0).some(t=>{let n=t.kind===`rule`||t.kind===`at-rule`?t.block:void 0;return G(e.slice(t.start,n?.[0]??t.end)).split(`
4
+ `).slice(1).some(e=>r.some(t=>Ee(e.trimStart(),t)))||n!==void 0&&L(e,n[0],n[1],r)})}async function R(e,t,n,r){let i=Ce(r),a=E((n.applyVariable??[`--at-apply`])||[]),{source:o,blockDepth:s,unclosedUrl:c}=Te(e.original,j(r),e=>!a.includes(e)),l=o.includes(`@apply`)||a.some(e=>o.includes(e)),u=o.includes(`@screen`),d=O(o)||D(o),f=xe(o)?be(e,o,j(r)):new Set;if(!l&&!d&&!u)return;if(i===`sass`)throw Error(`TeaCSS directives need braces; indented Sass is not supported, write it as SCSS${r?` in ${r}`:``}`);if(s>512)throw Error(`[@teacss] CSS blocks nest deeper than 512 levels${r?` in ${r}`:``}`);if(c)throw Error(`[@teacss] url() is not closed${r?` in ${r}`:``}`);if((i===`styl`||i===`stylus`)&&L(o,0,o.length,[`@apply`,`@screen`,...a]))throw Error(`TeaCSS directives need braces; indented Stylus is not supported, write it with braces${r?` in ${r}`:``}`);let p=new Map,h=[],g={options:n,applyVariable:a,generator:t,code:e,source:o,filename:r,preprocessor:i!==void 0,applyUtilities:p,hoisted:h,nesting:`top-level`,literalColors:!1,hasApply:l,hasScreen:u,hasFn:d,stripped:f};await H(g,x(o,0,o.length,g.preprocessor),void 0),h.length&&e.appendLeft(e.original.length,`\n${h.join(``)}`);let _=[...p].sort(([e],[t])=>e-t).flatMap(([,e])=>e),v=await m(t,_,r);v&&e.appendLeft(e.original.length,`\n${v}`)}const z=({source:e,applyVariable:t},[n,r])=>{let i=e.slice(n,r);return i.includes(`@apply`)||t.some(e=>i.includes(e))},B=e=>e===`top-level`||e===`scope`?`style-rule`:e;async function V(e,t,n){let{nesting:r,enclosing:i,literalColors:a}=e;Object.assign(e,t);try{await n()}finally{Object.assign(e,{nesting:r,enclosing:i,literalColors:a})}}async function H(e,t,n){for(let r of t)if(!e.stripped.has(r.start)){if(r.kind===`rule`)await $(e,r);else if(r.kind===`at-rule`){if(r.name===`apply`)throw N(e.source.slice(...r.prelude),e.filename);await Me(e,r,n)}else if(r.kind===`declaration`){if(n===void 0&&e.hasApply&&e.applyVariable.includes(r.name))throw N(e.source.slice(...r.value),e.filename);await Fe(e,r)}else await U(e,r)}}async function U(e,{start:t,end:n,rulesets:r}){await Z(e,[t,r[0]?.[0]??n]);for(let t of r)await K(e,t)}function De({source:e},{name:t,prelude:n}){return t===`mixin`||t===`include`||e.slice(...n)===`:`}function W(e,n,r,i){let a=0;for(let o=n;o<e.length;o++){let n=e[o];if(n===`\\`)o++;else if(n===`"`||n===`'`)o=t(e,o,e.length)-1;else if(n===r)a++;else if(n===i&&--a===0)return o+1}return e.length}function G(e){let n=``,r=0;for(let i=0;i<e.length;){let a=e[i],o;if(a===`\\`){i+=2;continue}if(a===`"`||a===`'`)o=t(e,i,e.length);else if(a===`[`)o=W(e,i,`[`,`]`);else if((a===`#`||a===`@`)&&e[i+1]===`{`)o=W(e,i+1,`{`,`}`);else if(e.startsWith(`/*`,i)){let t=e.indexOf(`*/`,i+2);o=t===-1?e.length:t+2}else{i++;continue}n+=e.slice(r,i)+` `.repeat(o-i),r=o,i=o}return n+e.slice(r)}function Oe(e){let t=G(e);return/^[.#]?[\w-]+\s*\(/.test(t)||/[\s)]when[\s(]/.test(t)||t.includes(`:extend(`)}function K(e,t){return V(e,{nesting:B(e.nesting),enclosing:void 0},()=>Q(e,t,`&`))}function ke({generator:e},t,n){if(/^screen$/i.test(t)){let t=v(n,e.config.theme);return t===void 0?void 0:`@media ${t}`}return/^(?:media|supports|container|layer|starting-style)$/i.test(t)?`@${t} ${n}`:void 0}function Ae({generator:e},t){let n=/^theme\(\s*(?:"([^"]*)"|'([^']*)')\s*[,)]/.exec(t);return n!==null&&k(n[1]??n[2],e.config.theme,!1)!==void 0&&g(t)}function q(e,[t,n]){let{source:r,hasFn:i,preprocessor:a}=e;return!i||!O(r.slice(t,n))?[]:p(r.slice(t,n),t,(t,n)=>n!==`.`&&n!==`#`&&(a?Ae(e,t):/^theme\(\s*["']/.test(t)))}function J(e,t){if(q(e,t).length)throw Error(`[@teacss] theme() cannot be resolved in "${e.source.slice(...t).trim()}"; use it in a declaration value, an at-rule condition or a preprocessor value${e.filename?` in ${e.filename}`:``}`)}function Y({source:e,hasFn:t,filename:n},[r,i]){let a=e.slice(r,i);if(t&&D(a)&&p(a,r,(e,t)=>t!==`.`&&t!==`#`&&/^icon\(\s*["']/.test(e)).length)throw Error(`[@teacss] icon() cannot be resolved in "${a.trim()}"; use it in a declaration value${n?` in ${n}`:``}`)}async function X(e,t,n,r){let i=e.source.slice(...t);for(let a of q(e,t).reverse()){let o=await I(e,a,n,r);o!==void 0&&(i=`${i.slice(0,a.loc.start.offset-t[0])}${o}${i.slice(a.loc.end.offset-t[0])}`)}return i}async function Z(e,t){return e.preprocessor?X(e,t,`value`,e.literalColors):(J(e,t),Y(e,t),e.source.slice(...t))}async function je(e,t){let[n,r]=t.prelude,i=/^(?:include|mixin|function)$/.test(t.name)?/^(?:\s|\/\*[\s\S]*?\*\/)*[\w.-]+/.exec(e.source.slice(n,r))?.[0].length??0:0,a=/^(?:media|supports|container|custom-media|import)$/i.test(t.name);return e.preprocessor||Y(e,[n+i,r]),!a&&!e.preprocessor?(J(e,[n+i,r]),e.source.slice(n,r)):e.source.slice(n,n+i)+await X(e,[n+i,r],a?void 0:t.name,a||e.literalColors)}async function Me(e,t,n){let r=await je(e,t);e.hasScreen&&t.name===`screen`&&ve(e,t);let{block:i}=t;if(!i)return;if(e.preprocessor&&De(e,t))return K(e,i);if(/keyframes$/i.test(t.name))return V(e,{nesting:`keyframes`,enclosing:e.enclosing},()=>Q(e,i,void 0));let a=t.name===`scope`,o=a?`:scope`:n,s;if(e.enclosing&&o===`&`){let n=ke(e,t.name,r);n!==void 0&&(s=e.enclosing.within(n))}await V(e,{nesting:a&&!e.preprocessor&&e.nesting===`top-level`?`scope`:e.nesting,enclosing:s,literalColors:e.literalColors||Se(t.name)},()=>Q(e,i,o))}async function Q(e,t,n){if(n!==void 0&&e.hasApply&&z(e,t))return Pe(e,{kind:`nested`,block:t,host:n,enclosing:e.enclosing});await H(e,x(e.source,t[0],t[1],e.preprocessor),n)}async function $(e,t){let n=await Z(e,t.prelude);if(e.preprocessor&&Oe(n))return K(e,t.block);if(e.hasApply&&z(e,[t.start,t.end]))return Pe(e,{kind:`rule`,rule:t,selector:n,enclosing:e.enclosing?.nested(n,e.preprocessor)});await V(e,{nesting:B(e.nesting),enclosing:e.enclosing},()=>H(e,x(e.source,t.block[0],t.block[1],e.preprocessor),`&`))}function Ne({nesting:e},t){return t.enclosing||t.kind!==`rule`?t.enclosing:y(t.selector)?le.root(t.selector,t.rule.end,e):void 0}async function Pe(e,t){let n=Ne(e,t),r=n!==void 0,i=t.kind===`rule`?B(e.nesting):e.nesting,a=await pe(e,t,t=>r?Promise.resolve():V(e,{nesting:i,enclosing:void 0},()=>$(e,t))),o=t.kind===`rule`?t.rule.block:t.block;await V(e,{nesting:i,enclosing:n},async()=>{for(let n of x(e.source,o[0],o[1],e.preprocessor))n.kind===`declaration`?e.applyVariable.includes(n.name)||await Fe(e,n):n.kind===`at-rule`?n.name!==`apply`&&await Me(e,n,t.kind===`rule`?`&`:t.host):n.kind===`call`?await U(e,n):r&&await $(e,n)}),me(e,t,n,a)}async function Fe(e,{name:t,value:n}){if(!e.hasFn)return;let r=e.source.slice(...n);if(O(r)||D(r))for(let i of p(r,n[0]))await I(e,i,t,e.literalColors)}async function Ie(e,t){let{base:n,entryFile:r,readSources:i,contents:a}=t,o=await oe(e,n,r),s=await C(o.config),c=[],l=[];for(let e of o.config.content?.filesystem??[])e.startsWith(`!`)?l.push(e.slice(1)):c.push(e);let{files:u,sources:d}=a?{files:[],sources:a}:i?await i(c,l):{files:[],sources:[]},f=await se(s,u,d);return{layers:f.getLayers()||``,files:u,unknown:o.unknown,matched:f.matched.size,hasTeacss:o.hasTeacss,hasPreset:!!o.config.presets?.length}}const Le=/\/\*[\s\S]*?\*\//g;function Re(n){let r=0;for(let a=0;a<n.length;a++){let o=n[a];if(o===`\\`){if(a===n.length-1)return!1;a++}else if(n.startsWith(`/*`,a)){let e=n.indexOf(`*/`,a+2);if(e<0)return!1;a=e+1}else if(o===`"`||o===`'`){let e=t(n,a,n.length);if(e-1===a||n[e-1]!==o)return!1;a=e-1}else if(i(n,a)){let t=e(n,a,n.length,!1);if(t<0)return!1;a=t-1}else if(o===`(`)r++;else if(o===`)`&&--r<0)return!1}return r===0}function ze(e,t,n=`css`){let r=[],i=[],a=[],o=!0,s=0,c=n===`css`?e:re(e,n);for(let l of M(e,!1,n)){o&&=!e.slice(s,l.start).replace(Le,``).trim(),s=l.end;let n=l.name.toLowerCase();if(n===`charset`&&l.terminator===`;`)r.push(e.slice(l.start,l.end)),a.push({start:l.start,end:l.end,replacement:``});else if((n===`import`||n===`layer`&&o)&&l.terminator===`;`)i.push(e.slice(l.start,l.end)),a.push({start:l.start,end:l.end,replacement:``});else if(n===`import`&&l.terminator===`eof`&&Re(c.slice(l.start,l.end).trimEnd())){let t=l.start+c.slice(l.start,l.end).trimEnd().length;i.push(`${e.slice(l.start,t)};${e.slice(t,l.end).trimEnd()}`),a.push({start:l.start,end:l.end,replacement:``})}else(A.includes(l.name)||l.name===`presets`||l.name===`source`||l.name===`shortcut`||l.name===`safelist`)&&l.terminator===`;`||l.name===`custom`&&(l.terminator===`block`||l.terminator===`;`)?a.push({start:l.start,end:l.end,replacement:``}):(l.name===`teacss`&&l.prelude===``&&l.terminator!==`block`&&a.push({start:l.start,end:l.end,replacement:t}),o=!1)}a.sort((e,t)=>e.start-t.start);let l=``;s=0;for(let t of a)l+=e.slice(s,t.start)+t.replacement,s=t.end;l+=e.slice(s);let u=l.startsWith(``)?``:``;u&&(l=l.slice(1));let d=[...r,...i];return u+(d.length?`${d.join(`
5
+ `)}\n`:``)+l}function Be(e={}){return{name:`@teacss/directives`,enforce:e.enforce,idFilter:w,transform:(t,n,r)=>R(t,r.generator,e,n)}}export{Be as default,Ie as generateEntry,ze as spliceEntry,R as transformDirectives};
@@ -0,0 +1,2 @@
1
+ import{DEFAULT_LAYERS as e,LAYER_UTILITIES as t,clone as n,comparableWidth as r,compareParentAtRules as i,createGenerator as a,isPseudoElementChain as o,notNull as s,toArray as c}from"@teacss/core";import{List as l,clone as u,generate as d,ident as f,parse as p,walk as m}from"css-tree";import{calcMaxWidthBySize as h,findCssFunctionStart as ee,hasIconFn as te,hasThemeFn as ne,isCssNameCharacter as re,isCssWhitespace as ie,transformThemeFn as ae,transformThemeString as oe}from"@teacss/rule-helpers";function g(e,t){return(e[t]===`#`||e[t]===`@`)&&e[t+1]===`{`}function _(e,t,n){for(let r=t+1;r<n;r++){let n=e[r];if(n===`\\`)r++;else if(n===e[t])return r+1;else if(n===`
2
+ `||n===`\r`||n===`\f`)return r}return n}function v(e,t){if(e[t]!==`u`&&e[t]!==`U`||e.slice(t,t+4).toLowerCase()!==`url(`)return!1;let n=e[t-1];if(n===`.`||n===`#`||re(n))return!1;let r=t+4;for(;ie(e[r]);)r++;return e[r]!==`"`&&e[r]!==`'`}function y(e,t,n,r){let i=0;for(let a=t+4;a<n;a++){let t=e[a];if(t===`\\`)a++;else if(r&&g(e,a))i++,a++;else if(i)t===`}`?i--:(t===`"`||t===`'`)&&(a=_(e,a,n)-1);else if(t===`)`)return a+1}return-1}function b(e,t,n,r){let i=e[t];if(i===`\\`)return Math.min(t+2,n);if(i===`"`||i===`'`)return _(e,t,n);if(i===`/`&&e[t+1]===`*`){let r=e.indexOf(`*/`,t+2);return r===-1||r+2>n?n:r+2}if(v(e,t)){let i=y(e,t,n,r);return i===-1?n:i}return t}function x(e){if(e===`(`)return`)`;if(e===`[`)return`]`}function S(e,t,n,r,i){let a=[],o=t;for(;o<n;){let t=b(e,o,n,i);if(t!==o){o=t;continue}if(i&&g(e,o)){a.push(`}`),o+=2;continue}let s=e[o],c=x(s);if(c)a.push(c);else if(r&&s===`{`)a.push(`}`);else if(a.at(-1)===s)a.pop();else if(!a.length){if(s===`{`)return{type:`block`,at:o};if(s===`;`)return{type:`semicolon`,at:o};if(s===`}`)return{type:`end`,at:o}}o++}return{type:`end`,at:n}}function C(e,t,n,r){let i=[],a=1,o=t+1;for(;o<n;){let t=b(e,o,n,r);if(t!==o){o=t;continue}if(r&&g(e,o)){i.push(`}`),o+=2;continue}let s=e[o],c=x(s);if(c)i.push(c);else if(i.at(-1)===s)i.pop();else if(!i.length){if(s===`{`)a++;else if(s===`}`&&--a===0)return o}o++}return n}function w(e,t,n,r){let i=[],a=t;for(;a<n;){let t=b(e,a,n,r);if(t!==a){a=t;continue}if(r&&g(e,a)){i.push(`}`),a+=2;continue}let o=e[a],s=x(o);if(s)i.push(s);else if(i.at(-1)===o)i.pop();else if(!i.length&&o===`:`)return a;a++}return-1}function T(e,t,n){let r=e.slice(t,n),i=t+r.length-r.trimStart().length;return[i,i+r.trim().length]}function se(e,[t,n]){let r=/(?:\s*!\s*(?:important|default|global))+$/i.exec(e.slice(t,n));return r?[t,t+r.index]:[t,n]}function ce(e,t,n,r){let i=w(e,t,n,r);if(i===-1)return;let[a,o]=T(e,t,i),s=T(e,i+1,n),c=e.slice(a,o);if(c&&(s[0]!==s[1]||c.startsWith(`--`)))return{name:c,value:se(e,s)}}function le(e,t,n){let r=[],i=t;for(;i<n;){let t=b(e,i,n,!0);if(t!==i){i=t;continue}if(g(e,i)){i=C(e,i+1,n,!0)+1;continue}if(e[i]===`{`){let t=C(e,i,n,!0);r.push([i+1,t]),i=t}i++}return r}function E(e,t,n){let r=t;for(;r<n&&/[\w-]/.test(e[r]);)r++;return r}function ue(e,t,n,r){let i=[],a=t;for(;a<n;){if(e.startsWith(`/*`,a)){a=b(e,a,n,r);continue}if(/[\s;}]/.test(e[a])){a++;continue}let t=a,o=S(e,t,n,e.startsWith(`@apply`,t)&&E(e,t+1,n)===t+6,r);o.type===`block`&&e.startsWith(`--`,t)&&w(e,t,o.at,r)!==-1&&(o=S(e,t,n,!0,r));let s=o.at,c=o.type===`semicolon`?s+1:s,l;if(o.type===`block`){let t=C(e,s,n,r);l=[s+1,t],c=Math.min(t+1,n)}if(a=c,e[t]===`@`&&!(r&&g(e,t))){let n=E(e,t+1,s);n>t+1&&i.push({kind:`at-rule`,start:t,end:c,name:e.slice(t+1,n),prelude:T(e,n,s),block:l})}else if(l)i.push({kind:`rule`,start:t,end:c,prelude:T(e,t,s),block:l});else{let n=ce(e,t,s,r);n?i.push({kind:`declaration`,start:t,end:c,...n}):r&&i.push({kind:`call`,start:t,end:c,rulesets:le(e,t,s)})}}return i}function D(e){if(e.type!==`PseudoClassSelector`||e.name!==`is`)return!1;let t=e.children?.first;if(t?.type!==`SelectorList`)return!1;let n=t.children.toArray().map(e=>d(e));return n.length===2&&/^:root:where\(html[:[]/.test(n[0])&&n[1]===`${n[0].slice(5)} *`}function O(e,t,n){if(!/\.module\.(?:css|postcss|scss|sass|less|styl|stylus)$/i.test(t?.split(/[?#]/,1)[0]??``)||!n&&!/@scope\s*\(|:root:where\(html/i.test(e))return e;let r=p(e),i=e=>{e.children=e.children.map(e=>p(`:global(${d(e)})`,{context:`selector`}))};return m(r,{enter(e,t,r){if(e.type===`PseudoClassSelector`&&e.name===`global`)return this.skip;if(!n&&D(e)){let n=p(`:global(${d(e)})`,{context:`selector`});return r.replace(t,n.children),this.skip}if(e.type===`Scope`){e.root?.type===`SelectorList`&&i(e.root),e.limit?.type===`SelectorList`&&i(e.limit);return}n&&e.type===`Rule`&&(this.atrule&&/(?:^|-)keyframes$/i.test(this.atrule.name)||e.prelude.type===`SelectorList`&&i(e.prelude))}}),d(r)}const de=/([0-9a-f]*)/g;function fe(e,t){let n=0;for(let r=t+1;r<e.length;r++){let t=e[r];if(t===`\\`)r++;else if(t===`"`||t===`'`)r=_(e,r,e.length)-1;else if(t===`{`)n++;else if(t===`}`&&--n===0)return r+1}}function pe(e){if(!e.includes(`{`))return e;let t=``,n=0;for(let r=0;r<e.length;r++){let i=e[r];if(i===`\\`)r++;else if(i===`"`||i===`'`)r=_(e,r,e.length)-1;else if(g(e,r)){let i=fe(e,r);if(i===void 0)return e;let a=Array.from({length:i-r},(t,n)=>e.charCodeAt(r+n).toString(16).padStart(4,`0`));t+=`${e.slice(n,r)}${a.join(``)}`,n=i,r=i-1}}return t+e.slice(n)}function k(e){return e.replace(de,(e,t)=>String.fromCharCode(...t.match(/.{4}/g).map(e=>Number.parseInt(e,16))))}function A(e){try{return p(pe(e),{context:`selectorList`})}catch{let t={type:`TypeSelector`,name:e.trim()},n={type:`Selector`,children:new l().fromArray([t])};return{type:`SelectorList`,children:new l().fromArray([n])}}}function me(e){let t=A(e);return{prelude:t,selector:k(d(t)),replacementSelector:e}}function he(e){return H(A(e)).some(e=>B(e)<e.length)}function ge(e){try{let t=p(e,{context:`declarationList`});for(let e of t.children)if(e.type===`Atrule`)return!0}catch{}return!1}async function _e(e,t){return await Promise.all(e.map(async e=>[e,await t.parseToken(e,`-`)]))}function ve(e){return e.filter(([,e])=>e==null).map(([e])=>e)}function j(e){return`[@teacss] @apply: no rule matched ${e.map(e=>`"${e}"`).join(`, `)}`}function M(e){let t=!1,n=0,r,i=``;for(let a=0;a<e.length;a++){let o=e[a];if(t){o===`*`&&e[a+1]===`/`&&(t=!1,a++);continue}if(r){o===`\\`?a++:o===r&&(r=void 0);continue}if(o===`/`&&e[a+1]===`*`){t=!0,a++;continue}if(o===`\\`){a++;continue}if(o===`"`||o===`'`){r=o;continue}if(!/\s/.test(o)||e[a+1]!==`$`||e[a+2]!==`$`||!/\s/.test(e[a+3]??``))continue;let s=a+4;for(;s<e.length&&/\s/.test(e[s]);)s++;i+=`${e.slice(n,a)} `,n=s,a=s-1}return n?i+e.slice(n):e}function N(e){let t=[],n=0,r=!1,i=0,a,o=0;for(let s=0;s<e.length;s++){let c=e[s];r?c===`*`&&e[s+1]===`/`&&(r=!1,s++):a?c===`\\`?s++:c===a&&(a=void 0):c===`/`&&e[s+1]===`*`?(r=!0,s++):c===`"`||c===`'`?a=c:c===`(`?i++:c===`)`?i>0&&i--:c===`[`?n++:c===`]`?n>0&&n--:n===0&&i===0&&e.startsWith(` $$ `,s)&&(t.push(e.slice(o,s)),s+=3,o=s+1)}return t.push(e.slice(o)),t}function P(e){let n=new Set,r=[];for(let i=e.length-1;i>=0;i--){let[,a,o,s,c]=e[i],l=JSON.stringify([c?.layer??t,a,s,o,!!c?.noMerge]);n.has(l)||(n.add(l),r.push(e[i]))}return r.reverse()}function F(e){return[...e].sort((e,t)=>e[0]-t[0])}function ye(n,r,a=e,o=F){let c=e=>Object.hasOwn(a,e)?a[e]??0:0,l=new Map;for(let e of n.flatMap(([,e])=>e??[]).filter(s)){let n=e[4]?.layer??t,r=e[3]??``,i=JSON.stringify([n,r]),a=l.get(i);a?a.utilities.push(e):l.set(i,{layer:n,parent:r,utilities:[e]})}let u=[...l.values()].sort((e,t)=>{let n=e.layer===t.layer?0:e.layer<t.layer?-1:1;return c(e.layer)-c(t.layer)||n||i(r,e.parent,t.parent)}).flatMap(e=>P(o(e.utilities)));if(!u.length)return[];let d=n.map(([e])=>e),f=[],p=[],m,h;for(let e of u){let n=e[4]?.layer??t;if(n!==m&&(m=n,p=[],h=void 0),!h&&p.length===64){h=new Map;for(let e of p)R(h,e)}let r=e[1],i=r?.includes(`.\\-`)?be(r,d)??r:r,a=e[3],o=i===void 0&&a===void 0&&ge(e[2]),s=h?h.get(a)?.get(i)?.get(o):p.find(e=>e.value[1]===i&&e.value[3]===a&&e.topLevel===o);if(s){s.value[2]+=e[2];continue}let c=[...e];c[1]=i;let l={topLevel:o,value:c};f.push(l),p.push(l),h&&R(h,l)}return f.map(({topLevel:e,value:[,t,n,r,i]})=>{let a=t&&M(t),o=!!(a&&i?.noMerge);return e?{kind:`top-level`,body:n}:r||a&&a!==`.\\-`||o?{kind:`standalone`,selector:a,body:n,parent:r,noMerge:o}:{kind:`inline`,body:n}})}const I=e=>e.type===`ClassSelector`&&f.decode(e.name)===`-`;function be(e,t){let n=p(e,{context:`selectorList`});return L(n,t)?d(n):void 0}function L(e,t){let n=!1;return e.children.forEach(e=>{e.type===`Selector`&&(n=xe(e,t)||n)}),n}function xe(e,t){let n=e=>e.type===`ClassSelector`&&!I(e)&&t.includes(f.decode(e.name)),r=e.children.toArray(),i=[],a=!1;for(let e=0;e<r.length;){let o=r.findIndex((t,n)=>n>=e&&t.type===`Combinator`);o=o===-1?r.length:o+1;let s=r.slice(e,o);e=o;let c=s.filter(n).length;if(!c||!s.some(I)){for(let e of s)e.type===`PseudoClassSelector`&&e.children&&!D(e)&&e.children.forEach(e=>{e.type===`SelectorList`&&(a=L(e,t)||a)}),i.push(e);continue}a=!0;for(let e of s)if(!n(e)&&(i.push(e),I(e)))for(let e=0;e<c;e++)i.push(p(`:is(.\\-)`,{context:`selector`}).children.first)}return a&&(e.children=new l().fromArray(i)),a}function R(e,t){let n=t.value[3],r=e.get(n);r||(r=new Map,e.set(n,r));let i=t.value[1],a=r.get(i);a||(a=new Map,r.set(i,a)),a.set(t.topLevel,t)}function Se(e,t,n){let r=e.selector,i=!1;if(t.selector&&t.selector!==`.\\-`){let n=Ce(e.prelude,t.selector);if(r=n.selector,!n.selector)return``;i=!n.hasHost}let a=O(`${r}{${t.body}}`,n,i);if(t.parent){let n=N(t.parent);a=n.length>1?`${n.join(`{`)}{${a}${`}`.repeat(n.length)}`:t.parent===`.\\-`?`${e.replacementSelector}{${a}}`:`${t.parent}{${a}}`}return O(a,n,!1)}function Ce(e,t){let n=p(`${t}{}`,{context:`rule`}).prelude,r=!1;if(m(n,{enter(e){if(D(e))return this.skip;e.type===`ClassSelector`&&f.decode(e.name)===`-`&&(r=!0)}}),!r)return{selector:d(n),hasHost:r};let i=u(n);return i.children.clear(),e.children.forEach(e=>{let t=e.children.toArray(),r=B(t),a=t.slice(r);n.children.forEach(e=>{let n=u(e);if(m(n,{enter(e,n,i){if(D(e))return this.skip;e.type===`ClassSelector`&&f.decode(e.name)===`-`&&i.replace(n,new l().fromArray(t.slice(0,r).map(e=>u(e))))}}),a.length){let e=n.children.toArray(),t=B(e),r=[...a.map(e=>u(e)),...e.slice(t)];if(!we(r))return;n.children=new l().fromArray([...e.slice(0,t),...r])}i.children.appendData(n)})}),{selector:k(d(i)),hasHost:r}}function we(e){let t=e.filter(z).map(e=>f.decode(e.name).toLowerCase());return t.length===2&&(t[0]===`before`||t[0]===`after`)&&t[1]===`marker`||o(V(e))}const Te=new Set([`before`,`after`,`first-line`,`first-letter`]);function z(e){return e.type===`PseudoElementSelector`||e.type===`PseudoClassSelector`&&e.children===null&&Te.has(f.decode(e.name).toLowerCase())}function B(e){let t=e.length;for(;t>0&&e[t-1].type!==`Combinator`;)t--;let n=e.slice(t).findIndex(z);return n===-1?e.length:t+n}function V(e){return k(d({type:`Selector`,children:new l().fromArray([...e])}))}function H(e){return e.children.toArray().map(e=>e.children.toArray())}function Ee(e){return e.map(V).join(`,`)}function De(e){let t=e.length;for(;t>0&&e[t-1].type!==`Combinator`;)t--;return t}function U(e){if(e.type!==`PseudoClassSelector`)return;let t=e.children?.first;return t?.type===`SelectorList`?H(t):void 0}function W(e,t){return e.some(e=>e.some(e=>{if(e.type===`NestingSelector`)return!0;if(e.type!==`PseudoClassSelector`)return!1;if(e.children===null)return t&&f.decode(e.name).toLowerCase()===`scope`;let n=U(e);return n!==void 0&&W(n,t)}))}function G(e){let t=De(e),n=!1;for(let[r,i]of e.entries()){if(i.type===`Combinator`&&r===0)return!1;if(i.type===`NestingSelector`){let i=e[r+1]?.type;if(r<t||i===`TypeSelector`||i===`Percentage`)return!1;n=!0;continue}let a=U(i);if(a&&W(a,!1)||r>=t&&z(i))return!1}return n}function Oe(e){let t=H(A(e)),n=t.filter(G);return n.length?{taking:n,others:t.filter(e=>!G(e))}:void 0}function ke(e){let t=[],n=[];for(let r of H(A(e))){let e=B(r);if(e===r.length){n.push(r);continue}let i=V(r.slice(e)),a=r.slice(0,e);(!a.length||a.at(-1).type===`Combinator`)&&a.push({type:`TypeSelector`,name:`*`});let o=t.find(e=>e.element===i);o?o.bases.push(a):t.push({bases:[a],element:i})}return{groups:t,plain:n}}function Ae(e,t,n){return t.every(e=>e[0]?.type===`NestingSelector`)?[...e]:e.map(e=>W([e],n)?e:[{type:`NestingSelector`},{type:`Combinator`,name:` `},...e])}function K(e,t){return e.flatMap(e=>e.type===`NestingSelector`?[...t]:[e])}function je(e,t,n){if(n)return t.flatMap(t=>e.map(e=>K(e,t)));let[r]=t,i=e.every(e=>e.every((t,n)=>t.type!==`NestingSelector`||n===0||e[n-1].type===`Combinator`));if(t.length===1&&!r.some(e=>e.type===`Combinator`)&&(r[0]?.type!==`TypeSelector`||i))return e.map(e=>K(e,r));let a=t.map(e=>({type:`Selector`,children:new l().fromArray(e)})),o={type:`SelectorList`,children:new l().fromArray(a)},s={type:`PseudoClassSelector`,name:`is`,children:new l().fromArray([o])};return e.map(e=>K(e,[s]))}function Me(e,t){return e.map(e=>V(e)+t).join(`,`)}function q(e,t){let n=e;for(;n!==null&&n!==Object.prototype&&n!==Array.prototype&&n!==Function.prototype;){if(Object.hasOwn(n,t))return Reflect.get(e,t);n=Object.getPrototypeOf(n)}}async function Ne(e,t,r){if(!t.length)return``;let i=e.config,o=[],s=()=>{if(e.config!==i)throw Error(`[@teacss] Configuration changed while generating @apply dependencies; retry the stylesheet transform.`)};for(let r of c(i.preflights)){let a=q(r,`getCSS`);if(!a)continue;let c=await Reflect.apply(a,r,[{generator:e,generated:[],theme:i.theme}]);s();let l=await Reflect.apply(a,r,[{generator:e,generated:n(t),theme:i.theme}]);s(),l&&l!==c&&o.push({layer:q(r,`layer`),getCSS:()=>l})}if(!o.length)return``;let l=await(await a({layers:i.layers,sortLayers:i.sortLayers,outputToCssLayers:i.outputToCssLayers,preflights:o})).generate(new Set,{safelist:!1});return s(),O(l.css,r,!0)}function J(e){if(!e.config.presets?.flat().some(e=>e.name===`@teacss/preset-standard`))return;let t=e.config.theme,n=Object.hasOwn(t,`colors`)?t.colors:void 0;return(e,t)=>{let r=typeof n==`object`&&n&&Object.hasOwn(n,e)?n[e]:void 0;if(typeof r==`object`&&r&&Object.hasOwn(r,t)&&r[t]!=null)return`var(--color-${e}-${t})`}}function Y(e){let t=e.config.theme,n=Object.hasOwn(t,`colors`)&&t.colors!=null&&typeof t.colors==`object`?t.colors:{},r=(e,t)=>{let r=Object.hasOwn(n,e)?n[e]:void 0;return typeof r==`object`&&r&&Object.hasOwn(r,t)?r[t]:void 0};return(e,t)=>{let i=r(e,t);for(let e=0;e<=Object.keys(n).length&&typeof i==`string`;e++){let e=X(i,t);if(!e)break;let n=r(e.palette,t)??e.fallback;if(n===void 0)break;i=n}return typeof i==`string`?i:void 0}}function X(e,t){if(!e.startsWith(`var(--color-`)||!e.endsWith(`)`))return;let n=e.slice(12,-1),r=n.indexOf(`,`),i=r===-1?n:n.slice(0,r).trimEnd();if(i.endsWith(`-${t}`))return{palette:i.slice(0,-t.length-1),fallback:r===-1?void 0:n.slice(r+1).trim()}}function Pe(e,t,n){let r=e.slice(t,n),i=0;for(let t=n;t<e.length;){let n=e[t],a=t+1;if(n===`\\`?a=t+2:n===`"`||n===`'`?a=_(e,t,e.length):n===`(`?i++:n===`)`&&i--,r+=i>1&&(n!==`(`||i!==2)?`_`.repeat(a-t):e.slice(t,a),i===0)return r;t=a}}function Z(e,t=0,n=()=>!0){let r=[];for(let i=0;;){let[a,o]=[`theme`,`icon`].map(t=>[ee(e,t,i),t]).reduce((e,t)=>e[0]===-1||t[0]!==-1&&t[0]<e[0]?t:e);if(a===-1)return r;let s=Pe(e,a,a+o.length);if(s===void 0)return r;n(s,e[a-1])&&r.push(p(s,{context:`value`,positions:!0,offset:t+a}).children.first),i=a+s.length}}function Fe(e){try{return p(e,{context:`value`}),!0}catch{return!1}}function Ie(e,t,n,r=!0,i=!1){return Q(e,t,n,r,i,0)}async function Q(e,t,n,r,i,a){switch(t.name){case`theme`:{if(!t.children.size)throw Error(`theme() expect exact one argument`);if(t.children.first.type!==`String`)throw Error(`theme() expect a string argument`);let o;if(t.children.size>1){let e=t.children.toArray().slice(1);if(e[0].type!==`Operator`||e[0].value!==`,`)throw Error(`theme() expect a comma between expression string and default value`);e.length>1&&(o=[e[1].loc.start.offset,t.children.last.loc.end.offset])}let s=oe(t.children.first.value,e.config.theme,!o&&r,i?Y(e):J(e));return s===void 0&&o&&(s=await Le(e,n(o[0],o[1]),r,i,a+1)),s}case`icon`:{let n=t.children.toArray().filter(e=>e.type===`String`).map(e=>e.value);if(n.length===0)throw Error(`icon() expects at least one argument`);let[i,a]=n;return a&&=encodeURIComponent(ae(a,e.config.theme,r,Y(e))),Re(e,i,a)}}}async function Le(e,t,n,r,i){if(!ne(t)&&!te(t))return t;if(i>512)throw Error(`theme() fallbacks nest too deeply`);let a=t;for(let o of Z(t).reverse()){let s=await Q(e,o,(e,n)=>t.slice(e,n),n,r,i);s!==void 0&&(a=`${a.slice(0,o.loc.start.offset)}${s}${a.slice(o.loc.end.offset)}`)}return a}async function Re(e,t,n){let r=e.config.presets?.flat()?.find(e=>e.name===`@teacss/preset-icons`);if(!r){console.warn(`@teacss/preset-icons not found, icon() directive will be kept as-is`);return}let i=r.api,a=await i.resolveIcon(t);if(!a)return;let o=i.encodeSvgForCss(a.svg);return`url("data:image/svg+xml;utf8,${n?o.replace(/currentcolor/gi,n):o}")`}function $(e){return typeof e==`string`?r(e)??void 0:void 0}function ze(e,t){let n=e.replace(/\/\*[\s\S]*?\*\//g,``).trim();if(!n)return;let r=``,i=n.match(/^(?:(lt|at)-)?([\w-]+)$/);i&&(r=i[1],n=i[2]);let{breakpoint:a,breakpoints:o}=t,s=a??o??{};if(r!==`at`){if(!Object.hasOwn(s,n))throw Error(`breakpoint ${n} not found`);let e=s[n];return r===`lt`?`(max-width: ${h(e)})`:`(min-width: ${e})`}let c=Object.entries(s),l=c.find(([e])=>e===n);if(!l)throw Error(`breakpoint ${n} not found`);let u=l[1],d=$(u),f,p;if(d!==void 0)for(let[e,t]of c){if(e===n)continue;let r=$(t);r===void 0||r.unit!==d.unit||r.value<=d.value||p!==void 0&&(r.value>p.value||r.value===p.value&&t>=f)||(f=t,p=r)}return`(min-width: ${u})${f===void 0?``:` and (max-width: ${h(f)})`}`}export{y as C,_ as S,Oe as _,J as a,v as b,j as c,ve as d,me as f,_e as g,Se as h,Ie as i,Ee as l,ke as m,Z as n,Ne as o,ye as p,Fe as r,Ae as s,ze as t,he as u,je as v,ue as x,Me as y};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@teacss/directives",
3
- "version": "0.10.2",
3
+ "version": "0.10.4",
4
4
  "description": "Build-time CSS directive transformer for TeaCSS — @apply, theme(), icon(), and @screen.",
5
5
  "homepage": "https://css.teasim.com",
6
6
  "funding": "https://github.com/sponsors/billgo",
@@ -24,17 +24,17 @@
24
24
  "dev": "tsdown --watch"
25
25
  },
26
26
  "dependencies": {
27
- "@teacss/config": "0.10.2",
28
- "@teacss/core": "0.10.2",
29
- "@teacss/rule-helpers": "0.10.2",
30
- "@teacss/runtime": "0.10.2",
27
+ "@teacss/config": "0.10.4",
28
+ "@teacss/core": "0.10.4",
29
+ "@teacss/rule-helpers": "0.10.4",
30
+ "@teacss/runtime": "0.10.4",
31
31
  "@types/css-tree": "^3.2.0",
32
32
  "css-tree": "^3.2.1"
33
33
  },
34
34
  "devDependencies": {
35
- "@teacss/preset-icons": "0.10.2",
36
- "@teacss/preset-official": "0.10.2",
37
- "@teacss/preset-standard": "0.10.2",
35
+ "@teacss/preset-icons": "0.10.4",
36
+ "@teacss/preset-official": "0.10.4",
37
+ "@teacss/preset-standard": "0.10.4",
38
38
  "magic-string": "^1.2.3"
39
39
  },
40
40
  "engines": {
@@ -1,2 +0,0 @@
1
- import{DEFAULT_LAYERS as e,LAYER_UTILITIES as t,clone as n,comparableWidth as r,compareParentAtRules as i,createGenerator as a,isPseudoElementChain as o,notNull as s,toArray as c}from"@teacss/core";import{List as l,clone as u,generate as d,ident as f,parse as p,walk as m}from"css-tree";import{calcMaxWidthBySize as h,findCssFunctionStart as ee,hasIconFn as te,hasThemeFn as ne,isCssNameCharacter as re,isCssWhitespace as ie,transformThemeFn as ae,transformThemeString as oe}from"@teacss/rule-helpers";function g(e,t){return(e[t]===`#`||e[t]===`@`)&&e[t+1]===`{`}function _(e,t,n){for(let r=t+1;r<n;r++){let n=e[r];if(n===`\\`)r++;else if(n===e[t])return r+1;else if(n===`
2
- `||n===`\r`||n===`\f`)return r}return n}function v(e,t){if(e[t]!==`u`&&e[t]!==`U`||e.slice(t,t+4).toLowerCase()!==`url(`)return!1;let n=e[t-1];if(n===`.`||n===`#`||re(n))return!1;let r=t+4;for(;ie(e[r]);)r++;return e[r]!==`"`&&e[r]!==`'`}function y(e,t,n,r){let i=0;for(let a=t+4;a<n;a++){let t=e[a];if(t===`\\`)a++;else if(r&&g(e,a))i++,a++;else if(i)t===`}`?i--:(t===`"`||t===`'`)&&(a=_(e,a,n)-1);else if(t===`)`)return a+1}return-1}function b(e,t,n,r){let i=e[t];if(i===`\\`)return Math.min(t+2,n);if(i===`"`||i===`'`)return _(e,t,n);if(i===`/`&&e[t+1]===`*`){let r=e.indexOf(`*/`,t+2);return r===-1||r+2>n?n:r+2}if(v(e,t)){let i=y(e,t,n,r);return i===-1?n:i}return t}function x(e){if(e===`(`)return`)`;if(e===`[`)return`]`}function S(e,t,n,r,i){let a=[],o=t;for(;o<n;){let t=b(e,o,n,i);if(t!==o){o=t;continue}if(i&&g(e,o)){a.push(`}`),o+=2;continue}let s=e[o],c=x(s);if(c)a.push(c);else if(r&&s===`{`)a.push(`}`);else if(a.at(-1)===s)a.pop();else if(!a.length){if(s===`{`)return{type:`block`,at:o};if(s===`;`)return{type:`semicolon`,at:o};if(s===`}`)return{type:`end`,at:o}}o++}return{type:`end`,at:n}}function C(e,t,n,r){let i=[],a=1,o=t+1;for(;o<n;){let t=b(e,o,n,r);if(t!==o){o=t;continue}if(r&&g(e,o)){i.push(`}`),o+=2;continue}let s=e[o],c=x(s);if(c)i.push(c);else if(i.at(-1)===s)i.pop();else if(!i.length){if(s===`{`)a++;else if(s===`}`&&--a===0)return o}o++}return n}function w(e,t,n,r){let i=[],a=t;for(;a<n;){let t=b(e,a,n,r);if(t!==a){a=t;continue}if(r&&g(e,a)){i.push(`}`),a+=2;continue}let o=e[a],s=x(o);if(s)i.push(s);else if(i.at(-1)===o)i.pop();else if(!i.length&&o===`:`)return a;a++}return-1}function T(e,t,n){let r=e.slice(t,n),i=t+r.length-r.trimStart().length;return[i,i+r.trim().length]}function se(e,[t,n]){let r=/(?:\s*!\s*(?:important|default|global))+$/i.exec(e.slice(t,n));return r?[t,t+r.index]:[t,n]}function ce(e,t,n,r){let i=w(e,t,n,r);if(i===-1)return;let[a,o]=T(e,t,i),s=T(e,i+1,n),c=e.slice(a,o);if(c&&(s[0]!==s[1]||c.startsWith(`--`)))return{name:c,value:se(e,s)}}function le(e,t,n){let r=[],i=t;for(;i<n;){let t=b(e,i,n,!0);if(t!==i){i=t;continue}if(g(e,i)){i=C(e,i+1,n,!0)+1;continue}if(e[i]===`{`){let t=C(e,i,n,!0);r.push([i+1,t]),i=t}i++}return r}function E(e,t,n){let r=t;for(;r<n&&/[\w-]/.test(e[r]);)r++;return r}function ue(e,t,n,r){let i=[],a=t;for(;a<n;){if(e.startsWith(`/*`,a)){a=b(e,a,n,r);continue}if(/[\s;}]/.test(e[a])){a++;continue}let t=a,o=S(e,t,n,e.startsWith(`@apply`,t)&&E(e,t+1,n)===t+6,r);o.type===`block`&&e.startsWith(`--`,t)&&w(e,t,o.at,r)!==-1&&(o=S(e,t,n,!0,r));let s=o.at,c=o.type===`semicolon`?s+1:s,l;if(o.type===`block`){let t=C(e,s,n,r);l=[s+1,t],c=Math.min(t+1,n)}if(a=c,e[t]===`@`&&!(r&&g(e,t))){let n=E(e,t+1,s);n>t+1&&i.push({kind:`at-rule`,start:t,end:c,name:e.slice(t+1,n),prelude:T(e,n,s),block:l})}else if(l)i.push({kind:`rule`,start:t,end:c,prelude:T(e,t,s),block:l});else{let n=ce(e,t,s,r);if(n)i.push({kind:`declaration`,start:t,end:c,...n});else if(r){let n=le(e,t,s);n.length&&i.push({kind:`call`,start:t,end:c,rulesets:n})}}}return i}function D(e){if(e.type!==`PseudoClassSelector`||e.name!==`is`)return!1;let t=e.children?.first;if(t?.type!==`SelectorList`)return!1;let n=t.children.toArray().map(e=>d(e));return n.length===2&&/^:root:where\(html[:[]/.test(n[0])&&n[1]===`${n[0].slice(5)} *`}function O(e,t,n){if(!/\.module\.(?:css|postcss|scss|sass|less|styl|stylus)$/i.test(t?.split(/[?#]/,1)[0]??``)||!n&&!/@scope\s*\(|:root:where\(html/i.test(e))return e;let r=p(e),i=e=>{e.children=e.children.map(e=>p(`:global(${d(e)})`,{context:`selector`}))};return m(r,{enter(e,t,r){if(e.type===`PseudoClassSelector`&&e.name===`global`)return this.skip;if(!n&&D(e)){let n=p(`:global(${d(e)})`,{context:`selector`});return r.replace(t,n.children),this.skip}if(e.type===`Scope`){e.root?.type===`SelectorList`&&i(e.root),e.limit?.type===`SelectorList`&&i(e.limit);return}n&&e.type===`Rule`&&(this.atrule&&/(?:^|-)keyframes$/i.test(this.atrule.name)||e.prelude.type===`SelectorList`&&i(e.prelude))}}),d(r)}const de=/([0-9a-f]*)/g;function fe(e,t){let n=0;for(let r=t+1;r<e.length;r++){let t=e[r];if(t===`\\`)r++;else if(t===`"`||t===`'`)r=_(e,r,e.length)-1;else if(t===`{`)n++;else if(t===`}`&&--n===0)return r+1}}function pe(e){if(!e.includes(`{`))return e;let t=``,n=0;for(let r=0;r<e.length;r++){let i=e[r];if(i===`\\`)r++;else if(i===`"`||i===`'`)r=_(e,r,e.length)-1;else if(g(e,r)){let i=fe(e,r);if(i===void 0)return e;let a=Array.from({length:i-r},(t,n)=>e.charCodeAt(r+n).toString(16).padStart(4,`0`));t+=`${e.slice(n,r)}${a.join(``)}`,n=i,r=i-1}}return t+e.slice(n)}function k(e){return e.replace(de,(e,t)=>String.fromCharCode(...t.match(/.{4}/g).map(e=>Number.parseInt(e,16))))}function A(e){try{return p(pe(e),{context:`selectorList`})}catch{let t={type:`TypeSelector`,name:e.trim()},n={type:`Selector`,children:new l().fromArray([t])};return{type:`SelectorList`,children:new l().fromArray([n])}}}function me(e){let t=A(e);return{prelude:t,selector:k(d(t)),replacementSelector:e}}function he(e){return H(A(e)).some(e=>B(e)<e.length)}function ge(e){try{let t=p(e,{context:`declarationList`});for(let e of t.children)if(e.type===`Atrule`)return!0}catch{}return!1}async function _e(e,t){return await Promise.all(e.map(async e=>[e,await t.parseToken(e,`-`)]))}function ve(e){return e.filter(([,e])=>e==null).map(([e])=>e)}function j(e){return`[@teacss] @apply: no rule matched ${e.map(e=>`"${e}"`).join(`, `)}`}function M(e){let t=!1,n=0,r,i=``;for(let a=0;a<e.length;a++){let o=e[a];if(t){o===`*`&&e[a+1]===`/`&&(t=!1,a++);continue}if(r){o===`\\`?a++:o===r&&(r=void 0);continue}if(o===`/`&&e[a+1]===`*`){t=!0,a++;continue}if(o===`\\`){a++;continue}if(o===`"`||o===`'`){r=o;continue}if(!/\s/.test(o)||e[a+1]!==`$`||e[a+2]!==`$`||!/\s/.test(e[a+3]??``))continue;let s=a+4;for(;s<e.length&&/\s/.test(e[s]);)s++;i+=`${e.slice(n,a)} `,n=s,a=s-1}return n?i+e.slice(n):e}function N(e){let t=[],n=0,r=!1,i=0,a,o=0;for(let s=0;s<e.length;s++){let c=e[s];r?c===`*`&&e[s+1]===`/`&&(r=!1,s++):a?c===`\\`?s++:c===a&&(a=void 0):c===`/`&&e[s+1]===`*`?(r=!0,s++):c===`"`||c===`'`?a=c:c===`(`?i++:c===`)`?i>0&&i--:c===`[`?n++:c===`]`?n>0&&n--:n===0&&i===0&&e.startsWith(` $$ `,s)&&(t.push(e.slice(o,s)),s+=3,o=s+1)}return t.push(e.slice(o)),t}function P(e){let n=new Set,r=[];for(let i=e.length-1;i>=0;i--){let[,a,o,s,c]=e[i],l=JSON.stringify([c?.layer??t,a,s,o,!!c?.noMerge]);n.has(l)||(n.add(l),r.push(e[i]))}return r.reverse()}function F(e){return[...e].sort((e,t)=>e[0]-t[0])}function ye(n,r,a=e,o=F){let c=e=>Object.hasOwn(a,e)?a[e]??0:0,l=new Map;for(let e of n.flatMap(([,e])=>e??[]).filter(s)){let n=e[4]?.layer??t,r=e[3]??``,i=JSON.stringify([n,r]),a=l.get(i);a?a.utilities.push(e):l.set(i,{layer:n,parent:r,utilities:[e]})}let u=[...l.values()].sort((e,t)=>{let n=e.layer===t.layer?0:e.layer<t.layer?-1:1;return c(e.layer)-c(t.layer)||n||i(r,e.parent,t.parent)}).flatMap(e=>P(o(e.utilities)));if(!u.length)return[];let d=n.map(([e])=>e),f=[],p=[],m,h;for(let e of u){let n=e[4]?.layer??t;if(n!==m&&(m=n,p=[],h=void 0),!h&&p.length===64){h=new Map;for(let e of p)R(h,e)}let r=e[1],i=r?.includes(`.\\-`)?be(r,d)??r:r,a=e[3],o=i===void 0&&a===void 0&&ge(e[2]),s=h?h.get(a)?.get(i)?.get(o):p.find(e=>e.value[1]===i&&e.value[3]===a&&e.topLevel===o);if(s){s.value[2]+=e[2];continue}let c=[...e];c[1]=i;let l={topLevel:o,value:c};f.push(l),p.push(l),h&&R(h,l)}return f.map(({topLevel:e,value:[,t,n,r,i]})=>{let a=t&&M(t),o=!!(a&&i?.noMerge);return e?{kind:`top-level`,body:n}:r||a&&a!==`.\\-`||o?{kind:`standalone`,selector:a,body:n,parent:r,noMerge:o}:{kind:`inline`,body:n}})}const I=e=>e.type===`ClassSelector`&&f.decode(e.name)===`-`;function be(e,t){let n=p(e,{context:`selectorList`});return L(n,t)?d(n):void 0}function L(e,t){let n=!1;return e.children.forEach(e=>{e.type===`Selector`&&(n=xe(e,t)||n)}),n}function xe(e,t){let n=e=>e.type===`ClassSelector`&&!I(e)&&t.includes(f.decode(e.name)),r=e.children.toArray(),i=[],a=!1;for(let e=0;e<r.length;){let o=r.findIndex((t,n)=>n>=e&&t.type===`Combinator`);o=o===-1?r.length:o+1;let s=r.slice(e,o);e=o;let c=s.filter(n).length;if(!c||!s.some(I)){for(let e of s)e.type===`PseudoClassSelector`&&e.children&&!D(e)&&e.children.forEach(e=>{e.type===`SelectorList`&&(a=L(e,t)||a)}),i.push(e);continue}a=!0;for(let e of s)if(!n(e)&&(i.push(e),I(e)))for(let e=0;e<c;e++)i.push(p(`:is(.\\-)`,{context:`selector`}).children.first)}return a&&(e.children=new l().fromArray(i)),a}function R(e,t){let n=t.value[3],r=e.get(n);r||(r=new Map,e.set(n,r));let i=t.value[1],a=r.get(i);a||(a=new Map,r.set(i,a)),a.set(t.topLevel,t)}function Se(e,t,n){let r=e.selector,i=!1;if(t.selector&&t.selector!==`.\\-`){let n=Ce(e.prelude,t.selector);if(r=n.selector,!n.selector)return``;i=!n.hasHost}let a=O(`${r}{${t.body}}`,n,i);if(t.parent){let n=N(t.parent);a=n.length>1?`${n.join(`{`)}{${a}${`}`.repeat(n.length)}`:t.parent===`.\\-`?`${e.replacementSelector}{${a}}`:`${t.parent}{${a}}`}return O(a,n,!1)}function Ce(e,t){let n=p(`${t}{}`,{context:`rule`}).prelude,r=!1;if(m(n,{enter(e){if(D(e))return this.skip;e.type===`ClassSelector`&&f.decode(e.name)===`-`&&(r=!0)}}),!r)return{selector:d(n),hasHost:r};let i=u(n);return i.children.clear(),e.children.forEach(e=>{let t=e.children.toArray(),r=B(t),a=t.slice(r);n.children.forEach(e=>{let n=u(e);if(m(n,{enter(e,n,i){if(D(e))return this.skip;e.type===`ClassSelector`&&f.decode(e.name)===`-`&&i.replace(n,new l().fromArray(t.slice(0,r).map(e=>u(e))))}}),a.length){let e=n.children.toArray(),t=B(e),r=[...a.map(e=>u(e)),...e.slice(t)];if(!we(r))return;n.children=new l().fromArray([...e.slice(0,t),...r])}i.children.appendData(n)})}),{selector:k(d(i)),hasHost:r}}function we(e){let t=e.filter(z).map(e=>f.decode(e.name).toLowerCase());return t.length===2&&(t[0]===`before`||t[0]===`after`)&&t[1]===`marker`||o(V(e))}const Te=new Set([`before`,`after`,`first-line`,`first-letter`]);function z(e){return e.type===`PseudoElementSelector`||e.type===`PseudoClassSelector`&&e.children===null&&Te.has(f.decode(e.name).toLowerCase())}function B(e){let t=e.length;for(;t>0&&e[t-1].type!==`Combinator`;)t--;let n=e.slice(t).findIndex(z);return n===-1?e.length:t+n}function V(e){return k(d({type:`Selector`,children:new l().fromArray([...e])}))}function H(e){return e.children.toArray().map(e=>e.children.toArray())}function Ee(e){return e.map(V).join(`,`)}function De(e){let t=e.length;for(;t>0&&e[t-1].type!==`Combinator`;)t--;return t}function U(e){if(e.type!==`PseudoClassSelector`)return;let t=e.children?.first;return t?.type===`SelectorList`?H(t):void 0}function W(e,t){return e.some(e=>e.some(e=>{if(e.type===`NestingSelector`)return!0;if(e.type!==`PseudoClassSelector`)return!1;if(e.children===null)return t&&f.decode(e.name).toLowerCase()===`scope`;let n=U(e);return n!==void 0&&W(n,t)}))}function G(e){let t=De(e),n=!1;for(let[r,i]of e.entries()){if(i.type===`Combinator`&&r===0)return!1;if(i.type===`NestingSelector`){let i=e[r+1]?.type;if(r<t||i===`TypeSelector`||i===`Percentage`)return!1;n=!0;continue}let a=U(i);if(a&&W(a,!1)||r>=t&&z(i))return!1}return n}function Oe(e){let t=H(A(e)),n=t.filter(G);return n.length?{taking:n,others:t.filter(e=>!G(e))}:void 0}function ke(e){let t=[],n=[];for(let r of H(A(e))){let e=B(r);if(e===r.length){n.push(r);continue}let i=V(r.slice(e)),a=r.slice(0,e);(!a.length||a.at(-1).type===`Combinator`)&&a.push({type:`TypeSelector`,name:`*`});let o=t.find(e=>e.element===i);o?o.bases.push(a):t.push({bases:[a],element:i})}return{groups:t,plain:n}}function Ae(e,t,n){return t.every(e=>e[0]?.type===`NestingSelector`)?[...e]:e.map(e=>W([e],n)?e:[{type:`NestingSelector`},{type:`Combinator`,name:` `},...e])}function K(e,t){return e.flatMap(e=>e.type===`NestingSelector`?[...t]:[e])}function je(e,t,n){if(n)return t.flatMap(t=>e.map(e=>K(e,t)));let[r]=t,i=e.every(e=>e.every((t,n)=>t.type!==`NestingSelector`||n===0||e[n-1].type===`Combinator`));if(t.length===1&&!r.some(e=>e.type===`Combinator`)&&(r[0]?.type!==`TypeSelector`||i))return e.map(e=>K(e,r));let a=t.map(e=>({type:`Selector`,children:new l().fromArray(e)})),o={type:`SelectorList`,children:new l().fromArray(a)},s={type:`PseudoClassSelector`,name:`is`,children:new l().fromArray([o])};return e.map(e=>K(e,[s]))}function Me(e,t){return e.map(e=>V(e)+t).join(`,`)}function q(e,t){let n=e;for(;n!==null&&n!==Object.prototype&&n!==Array.prototype&&n!==Function.prototype;){if(Object.hasOwn(n,t))return Reflect.get(e,t);n=Object.getPrototypeOf(n)}}async function Ne(e,t,r){if(!t.length)return``;let i=e.config,o=[],s=()=>{if(e.config!==i)throw Error(`[@teacss] Configuration changed while generating @apply dependencies; retry the stylesheet transform.`)};for(let r of c(i.preflights)){let a=q(r,`getCSS`);if(!a)continue;let c=await Reflect.apply(a,r,[{generator:e,generated:[],theme:i.theme}]);s();let l=await Reflect.apply(a,r,[{generator:e,generated:n(t),theme:i.theme}]);s(),l&&l!==c&&o.push({layer:q(r,`layer`),getCSS:()=>l})}if(!o.length)return``;let l=await(await a({layers:i.layers,sortLayers:i.sortLayers,outputToCssLayers:i.outputToCssLayers,preflights:o})).generate(new Set,{safelist:!1});return s(),O(l.css,r,!0)}function J(e){if(!e.config.presets?.flat().some(e=>e.name===`@teacss/preset-standard`))return;let t=e.config.theme,n=Object.hasOwn(t,`colors`)?t.colors:void 0;return(e,t)=>{let r=typeof n==`object`&&n&&Object.hasOwn(n,e)?n[e]:void 0;if(typeof r==`object`&&r&&Object.hasOwn(r,t)&&r[t]!=null)return`var(--color-${e}-${t})`}}function Y(e){let t=e.config.theme,n=Object.hasOwn(t,`colors`)&&t.colors!=null&&typeof t.colors==`object`?t.colors:{},r=(e,t)=>{let r=Object.hasOwn(n,e)?n[e]:void 0;return typeof r==`object`&&r&&Object.hasOwn(r,t)?r[t]:void 0};return(e,t)=>{let i=r(e,t);for(let e=0;e<=Object.keys(n).length&&typeof i==`string`;e++){let e=X(i,t);if(!e)break;let n=r(e.palette,t)??e.fallback;if(n===void 0)break;i=n}return typeof i==`string`?i:void 0}}function X(e,t){if(!e.startsWith(`var(--color-`)||!e.endsWith(`)`))return;let n=e.slice(12,-1),r=n.indexOf(`,`),i=r===-1?n:n.slice(0,r).trimEnd();if(i.endsWith(`-${t}`))return{palette:i.slice(0,-t.length-1),fallback:r===-1?void 0:n.slice(r+1).trim()}}function Pe(e,t,n){let r=e.slice(t,n),i=0;for(let t=n;t<e.length;){let n=e[t],a=t+1;if(n===`\\`?a=t+2:n===`"`||n===`'`?a=_(e,t,e.length):n===`(`?i++:n===`)`&&i--,r+=i>1&&(n!==`(`||i!==2)?`_`.repeat(a-t):e.slice(t,a),i===0)return r;t=a}}function Z(e,t=0){let n=[];for(let r=0;;){let[i,a]=[`theme`,`icon`].map(t=>[ee(e,t,r),t]).reduce((e,t)=>e[0]===-1||t[0]!==-1&&t[0]<e[0]?t:e);if(i===-1)return n;let o=Pe(e,i,i+a.length);if(o===void 0)return n;n.push(p(o,{context:`value`,positions:!0,offset:t+i}).children.first),r=i+o.length}}function Fe(e,t,n,r=!0,i=!1){return Q(e,t,n,r,i,0)}async function Q(e,t,n,r,i,a){switch(t.name){case`theme`:{if(!t.children.size)throw Error(`theme() expect exact one argument`);if(t.children.first.type!==`String`)throw Error(`theme() expect a string argument`);let o;if(t.children.size>1){let e=t.children.toArray().slice(1);if(e[0].type!==`Operator`||e[0].value!==`,`)throw Error(`theme() expect a comma between expression string and default value`);e.length>1&&(o=[e[1].loc.start.offset,t.children.last.loc.end.offset])}let s=oe(t.children.first.value,e.config.theme,!o&&r,i?Y(e):J(e));return s===void 0&&o&&(s=await Ie(e,n(o[0],o[1]),r,i,a+1)),s}case`icon`:{let n=t.children.toArray().filter(e=>e.type===`String`).map(e=>e.value);if(n.length===0)throw Error(`icon() expects at least one argument`);let[i,a]=n;return a&&=encodeURIComponent(ae(a,e.config.theme,r,Y(e))),Le(e,i,a)}}}async function Ie(e,t,n,r,i){if(!ne(t)&&!te(t))return t;if(i>512)throw Error(`theme() fallbacks nest too deeply`);let a=t;for(let o of Z(t).reverse()){let s=await Q(e,o,(e,n)=>t.slice(e,n),n,r,i);s!==void 0&&(a=`${a.slice(0,o.loc.start.offset)}${s}${a.slice(o.loc.end.offset)}`)}return a}async function Le(e,t,n){let r=e.config.presets?.flat()?.find(e=>e.name===`@teacss/preset-icons`);if(!r){console.warn(`@teacss/preset-icons not found, icon() directive will be kept as-is`);return}let i=r.api,a=await i.resolveIcon(t);if(!a)return;let o=i.encodeSvgForCss(a.svg);return`url("data:image/svg+xml;utf8,${n?o.replace(/currentcolor/gi,n):o}")`}function $(e){return typeof e==`string`?r(e)??void 0:void 0}function Re(e,t){let n=e.replace(/\/\*[\s\S]*?\*\//g,``).trim();if(!n)return;let r=``,i=n.match(/^(?:(lt|at)-)?([\w-]+)$/);i&&(r=i[1],n=i[2]);let{breakpoint:a,breakpoints:o}=t,s=a??o??{};if(r!==`at`){if(!Object.hasOwn(s,n))throw Error(`breakpoint ${n} not found`);let e=s[n];return r===`lt`?`(max-width: ${h(e)})`:`(min-width: ${e})`}let c=Object.entries(s),l=c.find(([e])=>e===n);if(!l)throw Error(`breakpoint ${n} not found`);let u=l[1],d=$(u),f,p;if(d!==void 0)for(let[e,t]of c){if(e===n)continue;let r=$(t);r===void 0||r.unit!==d.unit||r.value<=d.value||p!==void 0&&(r.value>p.value||r.value===p.value&&t>=f)||(f=t,p=r)}return`(min-width: ${u})${f===void 0?``:` and (max-width: ${h(f)})`}`}export{y as S,je as _,Ne as a,ue as b,Ee as c,me as d,ye as f,Oe as g,_e as h,J as i,he as l,Se as m,Z as n,Ae as o,ke as p,Fe as r,j as s,Re as t,ve as u,Me as v,_ as x,v as y};