aliascss 1.5.2 → 1.5.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.
@@ -0,0 +1,216 @@
1
+ diff --git a/src/__tests__/dashedIdentTest.ts b/src/__tests__/dashedIdentTest.ts
2
+ new file mode 100644
3
+ index 0000000..25bb3bd
4
+ --- /dev/null
5
+ +++ b/src/__tests__/dashedIdentTest.ts
6
+ @@ -0,0 +1,52 @@
7
+ +import {describe, test, expect} from '@jest/globals';
8
+ +import compileDashedIdent from '../compilers/dashedIdent.js';
9
+ +import {createCompilerObj, extractProperty} from "../utils/createCompilerObj.js";
10
+ +import cssProps from '../css-properties-all.js';
11
+ +import cssCustomCompilers from "../custom-css-compilers.js";
12
+ +import getPropertyAndValue from '../returnPropertyAndValue.js'
13
+ +import config from "../config.js";
14
+ +
15
+ +const [staticClassNames, compilerObj] = createCompilerObj({...cssCustomCompilers, ...cssProps}, config.globalValues);
16
+ +const pv = (c: string) => getPropertyAndValue(c, compilerObj, staticClassNames, {}, extractProperty);
17
+ +
18
+ +describe('Dashed-ident properties (anchor-name, timelines, position-try)', () => {
19
+ + test('a bare word becomes a --name', () => {
20
+ + expect(compileDashedIdent('anchor-name', '-menu')).toBe('--menu');
21
+ + expect(compileDashedIdent('position-anchor', '-x-menu')).toBe('--x-menu');
22
+ + });
23
+ + test('names already written with dashes are not doubled', () => {
24
+ + expect(compileDashedIdent('anchor-name', '--menu')).toBe('--menu');
25
+ + expect(compileDashedIdent('anchor-name', '---menu')).toBe('--menu');
26
+ + });
27
+ + test('__ separates names', () => {
28
+ + expect(compileDashedIdent('timeline-scope', '-a__b')).toBe('--a, --b');
29
+ + });
30
+ + test('keywords stay keywords', () => {
31
+ + expect(compileDashedIdent('position-anchor', '-auto')).toBe('auto');
32
+ + expect(compileDashedIdent('timeline-scope', '-all')).toBe('all');
33
+ + expect(compileDashedIdent('anchor-name', '-inherit')).toBe('inherit');
34
+ + });
35
+ + test('function form: names are not wrapped in var()', () => {
36
+ + expect(compileDashedIdent('anchor-name', '(--a,--b)')).toBe('--a, --b');
37
+ + expect(compileDashedIdent('scroll-timeline', '(--hero_y)')).toBe('--hero y');
38
+ + expect(compileDashedIdent('view-timeline', '(--card_block_auto_20%)')).toBe('--card block auto 20%');
39
+ + expect(compileDashedIdent('position-try-fallbacks', '(--below,flip-block)')).toBe('--below, flip-block');
40
+ + expect(compileDashedIdent('animation-timeline', '(scroll(root_block))')).toBe('scroll(root block)');
41
+ + });
42
+ + test('keep-mode properties never add dashes', () => {
43
+ + expect(compileDashedIdent('position-try-fallbacks', '-flip-block')).toBe('flip-block');
44
+ + expect(compileDashedIdent('font-palette', '-dark')).toBe('dark');
45
+ + });
46
+ + test('explicit variables are left to the variable handling', () => {
47
+ + expect(compileDashedIdent('anchor-name', '--var--x')).toBeNull();
48
+ + expect(compileDashedIdent('anchor-name', '--x:--y')).toBeNull();
49
+ + expect(compileDashedIdent('width', '-10px')).toBeNull();
50
+ + });
51
+ + test('wired into getPropertyAndValue', () => {
52
+ + expect(pv('anchor-name-menu')).toBe('anchor-name:--menu');
53
+ + expect(pv('anchor-name--menu')).toBe('anchor-name:--menu');
54
+ + expect(pv('position-anchor-auto')).toBe('position-anchor:auto');
55
+ + expect(pv('scroll-timeline(--hero_y)')).toBe('scroll-timeline:--hero y');
56
+ + expect(pv('anchor-name--var--x')).toBe('anchor-name: var(--x)');
57
+ + });
58
+ +});
59
+ diff --git a/src/compilers/dashedIdent.ts b/src/compilers/dashedIdent.ts
60
+ new file mode 100644
61
+ index 0000000..702193d
62
+ --- /dev/null
63
+ +++ b/src/compilers/dashedIdent.ts
64
+ @@ -0,0 +1,130 @@
65
+ +/**
66
+ + * Properties whose values are names written with two leading dashes (CSS
67
+ + * "dashed idents"): anchor names, scroll/view timeline names, custom
68
+ + * @position-try rules, custom font palettes.
69
+ + *
70
+ + * In these properties `--name` is a name, never a CSS variable, so the usual
71
+ + * `--x` → `var(--x)` rule must not apply. This compiler runs after static class
72
+ + * names and before the variable handling in getPropertyAndValue.
73
+ + *
74
+ + * anchor-name-menu anchor-name: --menu
75
+ + * anchor-name--menu anchor-name: --menu (was var(--menu))
76
+ + * anchor-name---menu anchor-name: --menu (was ----menu)
77
+ + * anchor-name-menu__card anchor-name: --menu, --card
78
+ + * anchor-name(--menu,--card) anchor-name: --menu, --card (was var(--menu), var(--card))
79
+ + * position-anchor-auto position-anchor: auto (was --auto)
80
+ + * timeline-scope-all timeline-scope: all (was --all)
81
+ + * animation-timeline(scroll(root_block)) animation-timeline: scroll(root block)
82
+ + * scroll-timeline(--hero_y) scroll-timeline: --hero y (was var(--hero y))
83
+ + * scroll-timeline-hero_y scroll-timeline: --hero y
84
+ + * position-try-fallbacks(--below,flip-block) position-try-fallbacks: --below, flip-block
85
+ + *
86
+ + * A real variable is still possible with the explicit forms, which this
87
+ + * compiler leaves alone: anchor-name--var--x → anchor-name: var(--x),
88
+ + * and var(...) written inside a value.
89
+ + */
90
+ +
91
+ +type Mode =
92
+ + /** Every item is one name: bare words get "--" added (anchor-name-menu → --menu) */
93
+ + | 'name'
94
+ + /** Each item is a name followed by other values (scroll-timeline: --hero y) */
95
+ + | 'shorthand'
96
+ + /** Values are written as they are; "--x" stays a name, nothing is added */
97
+ + | 'keep';
98
+ +
99
+ +interface Rule {
100
+ + mode: Mode;
101
+ + /** Words that are keywords, not names */
102
+ + keywords: string[];
103
+ +}
104
+ +
105
+ +const GLOBAL = ['inherit', 'initial', 'unset', 'revert', 'revert-layer'];
106
+ +
107
+ +export const DASHED_IDENT_PROPERTIES: { [property: string]: Rule } = {
108
+ + 'anchor-name': { mode: 'name', keywords: ['none'] },
109
+ + 'anchor-scope': { mode: 'name', keywords: ['none', 'all'] },
110
+ + 'position-anchor': { mode: 'name', keywords: ['auto', 'none'] },
111
+ + 'animation-timeline': { mode: 'name', keywords: ['auto', 'none'] },
112
+ + 'scroll-timeline-name': { mode: 'name', keywords: ['none'] },
113
+ + 'view-timeline-name': { mode: 'name', keywords: ['none'] },
114
+ + 'timeline-scope': { mode: 'name', keywords: ['none', 'all'] },
115
+ + 'scroll-timeline': { mode: 'shorthand', keywords: ['none'] },
116
+ + 'view-timeline': { mode: 'shorthand', keywords: ['none'] },
117
+ + 'position-try-fallbacks': { mode: 'keep', keywords: [] },
118
+ + 'position-try': { mode: 'keep', keywords: [] },
119
+ + 'font-palette': { mode: 'keep', keywords: [] },
120
+ +};
121
+ +
122
+ +export function isDashedIdentProperty(property: string): boolean {
123
+ + return Object.prototype.hasOwnProperty.call(DASHED_IDENT_PROPERTIES, property);
124
+ +}
125
+ +
126
+ +/** `_` is a space; `\_` stays an underscore (same rule as other function values). */
127
+ +const spaces = (s: string) => s.replace(/\\_|_/g, (m) => (m === '_' ? ' ' : '_'));
128
+ +
129
+ +/** Splits on commas that aren't inside parentheses. */
130
+ +function splitTopLevel(s: string, sep: string): string[] {
131
+ + const out: string[] = [];
132
+ + let depth = 0;
133
+ + let start = 0;
134
+ + for (let i = 0; i < s.length; i++) {
135
+ + const ch = s[i];
136
+ + if (ch === '(') depth++;
137
+ + else if (ch === ')') depth--;
138
+ + else if (depth === 0 && s.startsWith(sep, i)) {
139
+ + out.push(s.slice(start, i));
140
+ + start = i + sep.length;
141
+ + i += sep.length - 1;
142
+ + }
143
+ + }
144
+ + out.push(s.slice(start));
145
+ + return out.map((x) => x.trim()).filter(Boolean);
146
+ +}
147
+ +
148
+ +/** One name: keywords, functions and var() stay as written; anything else gets exactly two leading dashes. */
149
+ +function asName(word: string, rule: Rule): string {
150
+ + if (rule.keywords.indexOf(word) !== -1 || GLOBAL.indexOf(word) !== -1) return word;
151
+ + if (/^[a-z-]*\(/i.test(word)) return word; // scroll(), view(), var(), palette-mix()…
152
+ + return '--' + word.replace(/^-+/, '');
153
+ +}
154
+ +
155
+ +function compileItem(item: string, rule: Rule): string {
156
+ + if (rule.mode === 'keep') return item;
157
+ + if (rule.mode === 'name') return asName(item, rule);
158
+ + // shorthand: the first word is the name, the rest (axis, inset) as written
159
+ + const [first, ...rest] = splitTopLevel(item, ' ');
160
+ + return [asName(first, rule), ...rest].join(' ');
161
+ +}
162
+ +
163
+ +/**
164
+ + * @param property the full CSS property name (after alias lookup), e.g. "anchor-name"
165
+ + * @param valuePortion the class name minus the property key, as getPropertyAndValue has it:
166
+ + * "-menu", "--menu", "-menu__card", "(--menu,--card)", "(scroll(root_block))"
167
+ + * @returns the CSS value, or null to let the normal processing continue
168
+ + */
169
+ +export default function compileDashedIdent(property: string, valuePortion: string): string | null {
170
+ + const rule = DASHED_IDENT_PROPERTIES[property];
171
+ + if (!rule || !valuePortion) return null;
172
+ +
173
+ + // Explicit variables keep their existing meaning
174
+ + if (/^--var--/.test(valuePortion)) return null;
175
+ + // A fallback-style variable (anchor-name--x:value) is a variable too
176
+ + if (/^--[a-zA-Z][\w-]*:/.test(valuePortion)) return null;
177
+ +
178
+ + // Function form: property(value) — commas separate items, _ is a space
179
+ + const fn = valuePortion.match(/^\((.+)\)$/);
180
+ + if (fn) {
181
+ + const items = splitTopLevel(spaces(fn[1]), ',');
182
+ + return items.map((item) => compileItem(item, rule)).join(', ') || null;
183
+ + }
184
+ +
185
+ + // Class form: the leading dash is the separator; __ separates items, _ is a space
186
+ + const raw = valuePortion.replace(/^-/, '');
187
+ + if (!raw) return null;
188
+ + const items = raw.split('__').map((x) => spaces(x).trim()).filter(Boolean);
189
+ + if (rule.mode === 'keep') {
190
+ + // Hyphens belong to the value here (flip-block, --my-try); only __ and _ are separators
191
+ + return items.join(', ');
192
+ + }
193
+ + return items.map((item) => compileItem(item, rule)).join(', ');
194
+ +}
195
+ diff --git a/src/returnPropertyAndValue.ts b/src/returnPropertyAndValue.ts
196
+ index 383c577..2661e87 100644
197
+ --- a/src/returnPropertyAndValue.ts
198
+ +++ b/src/returnPropertyAndValue.ts
199
+ @@ -1,3 +1,4 @@
200
+ +import compileDashedIdent, { isDashedIdentProperty } from './compilers/dashedIdent.js';
201
+ type Property = {
202
+ alias?:string,
203
+ property?: string,
204
+ @@ -148,6 +149,12 @@ export default function getPropertyAndValue(
205
+ // --End of Group type
206
+ const prop=property.property?property.property:propertyKey;
207
+
208
+ + // Properties whose values are --names, not CSS variables (anchor-name, timelines, …)
209
+ + if(isDashedIdentProperty(prop)){
210
+ + const value=compileDashedIdent(prop, className.replace(propertyKey,''));
211
+ + if(value) return bool?[prop,value]:prop+":"+value;
212
+ + }
213
+ +
214
+ // Case 2: CSS Variables
215
+ if (/--var--/.test(className)) {
216
+ const splits: string[] = className.split(/--var--/);