aliascss 1.5.0 → 1.5.2

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.
@@ -1,55 +1,59 @@
1
1
  // custom.color will be provided {primary:'#2d3f3e....}
2
+ //
3
+ // Alpha is read once here and applied once at the end, so every form supports it:
4
+ // names, hex, tokens, variables, 255000000/0.5, rgb-255-0-0/0.5, rgb(1,2,3)/0.5, p3-1-0-0/0.5.
5
+ // (Before, the number/function/p3 forms parsed the alpha and then dropped it.)
2
6
  export default function color(color, custom) {
3
- var _a, _b;
4
- // check for alpha
5
7
  let alpha = null;
6
- // const match=color.match(/\/([0][.][1-9][0-9]?$)/);
7
- const match = color.match(/\/(0\.[0-9][0-9]?|1(\.0+)?)$/);
8
- const matchPercentage = color.match(/\/(100%|[1-9]?[0-9]%)$/);
8
+ // /0.5 /.5 /0.125 /0 /1 /0d5 and percentages /50% /50p
9
+ const match = color.match(/\/(0?\.[0-9]+|0d[0-9]+|0|1(?:\.0+)?)$/);
10
+ const matchPercentage = color.match(/\/(100|[1-9]?[0-9])(%|p)$/);
9
11
  if (match) {
10
- alpha = (_a = match[1]) !== null && _a !== void 0 ? _a : null;
12
+ alpha = match[1].replace('d', '.');
11
13
  color = color.replace(match[0], '');
12
14
  }
13
15
  else if (matchPercentage) {
14
- alpha = (_b = matchPercentage[1]) !== null && _b !== void 0 ? _b : null;
16
+ alpha = matchPercentage[1] + '%';
15
17
  color = color.replace(matchPercentage[0], '');
16
18
  }
19
+ const base = baseColor(color, custom);
20
+ if (base === undefined || alpha === null)
21
+ return base;
22
+ return `oklch(from ${base} l c h/${alpha} )`;
23
+ }
24
+ function baseColor(color, custom) {
17
25
  // ----check for css variables
18
26
  if (color.match(/^--[a-zA-Z]/)) {
19
- if (alpha !== null)
20
- return `oklch(from var(${color}) l c h/${alpha} )`;
27
+ // --brand:red -> var(--brand,red)
28
+ const fb = color.match(/^(--[\w-]+):(.+)$/);
29
+ if (fb) {
30
+ const fallback = baseColor(fb[2], custom);
31
+ return `var(${fb[1]},${fallback !== undefined ? fallback : fb[2]})`;
32
+ }
21
33
  return `var(${color})`;
22
34
  }
23
35
  // remove - before the color
24
36
  color = color.replace(/^[-]/, '');
25
37
  // check if is a raw function
26
38
  if (color.match(/^(rgb|hsl|hsla|rgba|hwb|hwba|lab|oklab|lch|oklch)[(]/)) {
27
- return color.replace(/--([\w][\w-]*)/g, 'var(--$1)');
39
+ return color.replace(/--([\w][\w-]*)/g, 'var(--$1)').replace(/_/g, ' ');
28
40
  }
29
41
  // custom-color
30
42
  if (custom.hasOwnProperty('colors') && typeof custom.colors === 'object') {
31
43
  if (custom.colors.hasOwnProperty(color)) {
32
- if (alpha !== null)
33
- return `oklch(from ${custom.colors[color]} l c h/${alpha} )`;
34
44
  return custom.colors[color];
35
45
  }
36
46
  // custom-color try replacing -
37
47
  if (custom.colors.hasOwnProperty(color.replace(/-/g, ''))) {
38
- if (alpha !== null)
39
- return `oklch(from ${custom.colors[color.replace(/-/g, '')]} l c h/${alpha} )`;
40
48
  return custom.colors[color.replace(/-/g, '')];
41
49
  }
42
50
  }
43
51
  // hexadecimal =+ alpha 00-ff
44
- if (/^[#]?[0-9a-fA-F]{3,8}$/.test(color)) {
45
- if (alpha !== null)
46
- return `oklch(from #${color.replace("#", '')} l c h/${alpha} )`;
52
+ if (/^[#]?([0-9a-fA-F]{3,4}|[0-9a-fA-F]{6}|[0-9a-fA-F]{8})$/.test(color)) {
47
53
  return `#${color.replace("#", '')}`;
48
54
  // Name
49
55
  }
50
56
  else if (/^[a-zA-Z]+$/.test(color)) {
51
- if (alpha !== null)
52
- return `oklch(from ${color} l c h/${alpha} )`;
53
57
  return color;
54
58
  // rgb/a in number
55
59
  }
@@ -94,8 +98,8 @@ export default function color(color, custom) {
94
98
  }
95
99
  else if (/^p3-/.test(color)) {
96
100
  if (color.replace(/p3/, '').match(/^--/))
97
- return `color(display-p3 var(${color.replace(/p3/, '')})`;
98
- return replaceLast(`color(display-p3 ${color.replace(/p3/, '').replace(/^-/, '').replace(/-/g, ' ').replace('d', '.')
101
+ return `color(display-p3 var(${color.replace(/p3/, '')}))`;
102
+ return replaceLast(`color(display-p3 ${color.replace(/p3/, '').replace(/^-/, '').replace(/-/g, ' ').replace(/([0-9])d([0-9])/g, '$1.$2')
99
103
  .replace(/([0-9])p/g, '$1%')})`, 4, " ", " / ");
100
104
  }
101
105
  else {
@@ -114,3 +118,128 @@ function replaceLast(str, occurs, target, replacement) {
114
118
  return str;
115
119
  }
116
120
  }
121
+ /*
122
+ // custom.color will be provided {primary:'#2d3f3e....}
123
+ export default function color(color: string, custom: { [key: string]: { [key: string]: string } }): string | undefined {
124
+
125
+
126
+ // check for alpha
127
+ let alpha=null;
128
+
129
+ // const match=color.match(/\/([0][.][1-9][0-9]?$)/);
130
+ const match=color.match(/\/(0\.[0-9][0-9]?|1(\.0+)?)$/);
131
+ const matchPercentage=color.match(/\/(100%|[1-9]?[0-9]%)$/);
132
+ if(match){
133
+ alpha = match[1] ?? null;
134
+ color = color.replace(match[0], '');
135
+ }else if(matchPercentage){
136
+ alpha = matchPercentage[1] ?? null;
137
+ color = color.replace(matchPercentage[0], '');
138
+ }
139
+
140
+ // ----check for css variables
141
+
142
+ if(color.match(/^--[a-zA-Z]/)){
143
+ if(alpha!==null) return `oklch(from var(${color}) l c h/${alpha} )`
144
+
145
+ return `var(${color})`
146
+ }
147
+
148
+ // remove - before the color
149
+ color = color.replace(/^[-]/, '');
150
+
151
+ // check if is a raw function
152
+ if(color.match(/^(rgb|hsl|hsla|rgba|hwb|hwba|lab|oklab|lch|oklch)[(]/)){
153
+ return color.replace(/--([\w][\w-]*)/g,'var(--$1)');
154
+ }
155
+
156
+
157
+ // custom-color
158
+ if (custom.hasOwnProperty('colors') && typeof custom.colors === 'object') {
159
+
160
+ if (custom.colors.hasOwnProperty(color)){
161
+ if(alpha!==null) return `oklch(from ${custom.colors[color]} l c h/${alpha} )`
162
+ return custom.colors[color];
163
+ }
164
+
165
+ // custom-color try replacing -
166
+
167
+ if (custom.colors.hasOwnProperty(color.replace(/-/g,''))){
168
+ if(alpha!==null) return `oklch(from ${custom.colors[color.replace(/-/g,'')]} l c h/${alpha} )`
169
+
170
+ return custom.colors[color.replace(/-/g,'')];
171
+
172
+ }
173
+ }
174
+
175
+ // hexadecimal =+ alpha 00-ff
176
+ if (/^[#]?[0-9a-fA-F]{3,8}$/.test(color)) {
177
+ if(alpha!==null) return `oklch(from #${color.replace("#",'')} l c h/${alpha} )`
178
+ return `#${color.replace("#",'')}`;
179
+ // Name
180
+ } else if (/^[a-zA-Z]+$/.test(color)) {
181
+ if(alpha!==null) return `oklch(from ${color} l c h/${alpha} )`
182
+
183
+ return color;
184
+ // rgb/a in number
185
+ } else if (/^[0-9]{9}/.test(color)) {
186
+ if (color.length === 9) return `rgb(${color.replace(/([\d]{3})([\d]{3})([\d]{3})/, '$1,$2,$3')})`;
187
+ else
188
+ return `rgba(${color
189
+ .replace(/([\d]{3})([\d]{3})([\d]{3})([\d]+)/, '$1,$2,$3,$4')
190
+ .replace('d', '.')
191
+ .replace('p', '%')})`;
192
+
193
+ } else if (/^([\d]{3})([\d]{1,3}p)([\d]{1,3}p)/.test(color)) {
194
+ if (/^([\d]{3})([\d]{1,3}p)([\d]{1,3}p)$/.test(color))
195
+ return 'hsl(' + color.replace(/^([\d]{1,3})([\d]{1,3}p)([\d]{1,3}p)/, '$1,$2,$3').replace(/p/g, '%') + ')';
196
+ else
197
+ return (
198
+ 'hsla(' +
199
+ color
200
+ .replace(/^([\d]{1,3})([\d]{1,3}p)([\d]{1,3}p)([\d]+)/, '$1,$2,$3,$4')
201
+ .replace('d', '.')
202
+ .replace(/p/g, '%') +
203
+ ')'
204
+ );
205
+ } else if (/^([\d]{1,3}p)([\d]{1,3}p)([\d]{1,3}p)/.test(color)) {
206
+ if (/^([\d]{1,3}p)([\d]{1,3}p)([\d]{1,3}p$)/.test(color))
207
+ return 'rgb(' + color.replace(/^([\d]{1,3}p)([\d]{1,3}p)([\d]{1,3}p)/, '$1,$2,$3').replace(/p/g, '%') + ')';
208
+ else
209
+ return (
210
+ 'rgba(' +
211
+ color
212
+ .replace(/^([\d]{1,3}p)([\d]{1,3}p)([\d]{1,3}p)([\d]+)/, '$1,$2,$3,$4')
213
+ .replace('d', '.')
214
+ .replace(/p/g, '%') +
215
+ ')'
216
+ );
217
+
218
+
219
+ }else if(/^(rgb|hsl|hsla|rgba|hwb|hwba|lab|oklab|lch|oklch)-/.test(color)){
220
+ const match=color.match(/^(rgb|hsl|hsla|rgba|hwb|hwba|lab|oklab|lch|oklch)-/);
221
+ const colorFunc=match?match[1]:'';
222
+ if(color.replace(colorFunc,'').match(/^--/)) return `${colorFunc}(var(${color.replace(colorFunc,'')}))`
223
+ return replaceLast(`${colorFunc}(${color.replace(colorFunc,'').replace(/^-/,'').replace(/-/g,' ').replace(/([0-9])d([0-9])/g, '$1.$2')
224
+ .replace(/p/g, '%')})`, 3," "," / ");
225
+
226
+
227
+ }else if(/^p3-/.test(color)){
228
+ if(color.replace(/p3/,'').match(/^--/)) return `color(display-p3 var(${color.replace(/p3/,'')})`
229
+ return replaceLast(`color(display-p3 ${color.replace(/p3/,'').replace(/^-/,'').replace(/-/g,' ').replace('d', '.')
230
+ .replace(/([0-9])p/g, '$1%')})`, 4," "," / ");
231
+ } else {
232
+ return;
233
+ }
234
+ }
235
+
236
+ function replaceLast(str:string, occurs:number, target:string, replacement:string) {
237
+ if(str.match(new RegExp(target,'g'))?.length===occurs){
238
+ const lastIndex = str.lastIndexOf(target);
239
+ if (lastIndex === -1) return str;
240
+ return str.slice(0, lastIndex) + replacement + str.slice(lastIndex + target.length);
241
+ }else{
242
+ return str;
243
+ }
244
+ }
245
+ */
@@ -1,7 +1,8 @@
1
1
  import shadow from './shadow.js';
2
2
  export default function filter(valuePortion, custom) {
3
3
  let compileValue = '';
4
- const matchRegEx = /^[-]?([a-zA-Z]+([-][\w]+)?)(?=[-])/;
4
+ // second word must start with a letter (hue-rotate, drop-shadow): with [\w]+ "ds-0-4px…" read "ds-0" as the name
5
+ const matchRegEx = /^[-]?([a-zA-Z]+([-][a-zA-Z]+)?)(?=[-])/;
5
6
  valuePortion.split(/__/).forEach((each) => {
6
7
  var _a;
7
8
  // check for raw-css funcion
@@ -14,7 +15,10 @@ export default function filter(valuePortion, custom) {
14
15
  if (each.match(/--[a-zA-Z]/)) {
15
16
  const [functionPortion, functionValuePortion] = [(_a = each.split(/--/)[0]) === null || _a === void 0 ? void 0 : _a.replace(/^-/, ''), each.split(/--/)[1]];
16
17
  if (funcAlias.hasOwnProperty(functionPortion)) {
17
- compileValue += `${functionPortion}(var(--${functionValuePortion})) `;
18
+ // b--x -> blur(var(--x)), not b(var(--x)); b--x:4px -> blur(var(--x,4px))
19
+ const [name, fallback] = functionValuePortion.split(':');
20
+ const fb = fallback !== undefined ? ',' + number(fallback) : '';
21
+ compileValue += `${funcAlias[functionPortion]}(var(--${name}${fb})) `;
18
22
  return;
19
23
  }
20
24
  }
@@ -28,13 +32,20 @@ export default function filter(valuePortion, custom) {
28
32
  value = value ? value : '';
29
33
  }
30
34
  else {
31
- value = each.replace(matchRegEx, '').replace(/-(-)?/g, '$1').replace(/p$/, '%').replace(/(?<=[0-9])[d](?=[0-9])/, '.');
35
+ value = number(each.replace(matchRegEx, ''));
32
36
  }
33
37
  }
38
+ // unknown function or no value: leave it out instead of writing "(4px)" or "()"
39
+ if (!fn || !value)
40
+ return;
34
41
  compileValue += `${fn}(${value}) `;
35
42
  });
36
43
  return compileValue.replace(/[\s]$/, '');
37
44
  }
45
+ // -1d5 -> 1.5, -50p -> 50%, --90deg -> -90deg (every decimal, not just the first)
46
+ function number(v) {
47
+ return v.replace(/-(-)?/g, '$1').replace(/p$/, '%').replace(/(?<=[0-9])[d](?=[0-9])/g, '.');
48
+ }
38
49
  const funcAlias = {
39
50
  b: 'blur',
40
51
  blur: 'blur',
@@ -57,3 +68,79 @@ const funcAlias = {
57
68
  se: 'sepia',
58
69
  sepia: 'sepia',
59
70
  };
71
+ /*
72
+ import shadow from './shadow.js';
73
+
74
+ type Custom = { [key: string]: { [key: string]: string } };
75
+
76
+ export default function filter(valuePortion: string, custom: Custom) {
77
+ let compileValue='';
78
+ const matchRegEx=/^[-]?([a-zA-Z]+([-][\w]+)?)(?=[-])/;
79
+ valuePortion.split(/__/).forEach((each)=>{
80
+ // check for raw-css funcion
81
+ if(each.match(/[(]/)){
82
+ compileValue+=each.replace(/^-/,'')+" ";
83
+ return;
84
+ }
85
+
86
+
87
+ let [fn,value]=['',''];
88
+ // css-var
89
+ if(each.match(/--[a-zA-Z]/)){
90
+ const [functionPortion,functionValuePortion]:[string,string]=[each.split(/--/)[0]?.replace(/^-/,''),each.split(/--/)[1]];
91
+
92
+ if(funcAlias.hasOwnProperty(functionPortion)){
93
+
94
+ compileValue+=`${functionPortion}(var(--${functionValuePortion})) `
95
+ return
96
+ }
97
+ }
98
+
99
+ const extractFnV = each.match(matchRegEx);
100
+
101
+ if(extractFnV){
102
+ if(funcAlias.hasOwnProperty(extractFnV[1])){
103
+ fn=funcAlias[extractFnV[1]];
104
+ }
105
+
106
+ if(fn==='drop-shadow'){
107
+ value=shadow(each.replace(matchRegEx,''),custom)
108
+ value=value?value:''
109
+ }else{
110
+ value=each.replace(matchRegEx,'').replace(/-(-)?/g,'$1').replace(/p$/, '%').replace(/(?<=[0-9])[d](?=[0-9])/, '.');
111
+ }
112
+ }
113
+
114
+ compileValue+= `${fn}(${value}) `;
115
+
116
+
117
+
118
+
119
+ })
120
+
121
+ return compileValue.replace(/[\s]$/, '');
122
+ }
123
+
124
+ const funcAlias: { [key: string]: string } = {
125
+ b: 'blur',
126
+ blur: 'blur',
127
+ br: 'brightness',
128
+ brightness: 'brightness',
129
+ 'drop-shadow': 'drop-shadow',
130
+ ds: 'drop-shadow',
131
+ c: 'contrast',
132
+ contrast: 'contrast',
133
+ g: 'grayscale',
134
+ grayscale: 'grayscale',
135
+ hr: 'hue-rotate',
136
+ 'hue-rotate': 'hue-rotate',
137
+ i: 'invert',
138
+ invert: 'invert',
139
+ o: 'opacity',
140
+ opacity: 'opacity',
141
+ saturate: 'saturate',
142
+ s: 'saturate',
143
+ se: 'sepia',
144
+ sepia: 'sepia',
145
+ };
146
+ */
@@ -2,18 +2,46 @@ export default function font(font, custom) {
2
2
  font = font.replace(/^[-_]/, '');
3
3
  let holder = '';
4
4
  font.split(/__/).forEach((e) => {
5
- if (custom.hasOwnProperty('font') && typeof custom.font === 'object') {
6
- if (custom.font.hasOwnProperty(e)) {
7
- holder += custom.font[e] + ', ';
8
- return;
9
- }
10
- }
11
- if (e.search(/_/) !== -1) {
12
- holder += '"' + e.replace(/_/g, ' ') + '", ';
13
- }
14
- else {
15
- holder += e + ', ';
5
+ // CSS variable, with an optional fallback family: --x-font / --x-font:Segoe_UI
6
+ // (before, "--x-font" became a font *named* --x-font: valid CSS, so nothing warned)
7
+ const v = e.match(/^(--[a-zA-Z][\w-]*)(?::(.+))?$/);
8
+ if (v) {
9
+ holder += `var(${v[1]}${v[2] ? ',' + family(v[2], custom) : ''}), `;
10
+ return;
16
11
  }
12
+ holder += family(e, custom) + ', ';
17
13
  });
18
14
  return holder.replace(/,[\s]$/, '');
19
15
  }
16
+ // One family: a custom font alias, "Quoted Name" when it has spaces (_), or as written
17
+ function family(e, custom) {
18
+ if (custom.hasOwnProperty('font') && typeof custom.font === 'object' && custom.font.hasOwnProperty(e)) {
19
+ return custom.font[e];
20
+ }
21
+ if (e.search(/_/) !== -1)
22
+ return '"' + e.replace(/_/g, ' ') + '"';
23
+ return e;
24
+ }
25
+ /*
26
+ export default function font(font: string, custom: { [key: string]: { [key: string]: string } }): string {
27
+ font = font.replace(/^[-_]/, '');
28
+
29
+ let holder = '';
30
+
31
+ font.split(/__/).forEach((e) => {
32
+ if (custom.hasOwnProperty('font') && typeof custom.font === 'object') {
33
+ if (custom.font.hasOwnProperty(e)){
34
+ holder+= custom.font[e]+ ', ';
35
+ return;
36
+ }
37
+ }
38
+ if (e.search(/_/) !== -1) {
39
+ holder += '"' + e.replace(/_/g, ' ') + '", ';
40
+ } else {
41
+ holder += e + ', ';
42
+ }
43
+ });
44
+
45
+ return holder.replace(/,[\s]$/, '');
46
+ }
47
+ */
@@ -1,4 +1,5 @@
1
1
  import color from './color.js';
2
+ const escape = (s) => s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
2
3
  export default function gradient(datas, custom) {
3
4
  datas = datas.replace(/^[-]/, '');
4
5
  let masterHolder = '';
@@ -6,61 +7,66 @@ export default function gradient(datas, custom) {
6
7
  if (data.match(/[-]?(((repeating-)?(conic|linear|radial)-gradient)|(rrg|rg|lg|rcg|cg|rcg|rlg))[\(]/)) {
7
8
  masterHolder += data.replace(/[_]/g, ' ') + ', ';
8
9
  }
9
- const matched = data.match(/[-]?(((repeating-)?(conic|linear|radial)-gradient)|(rrg|rg|lg|rcg|cg|rcg|rlg))([-_][\w_-]+)/);
10
- // console.warn(matched, data);
10
+ // ".", "/" and "%" are allowed so stops like red/0.5, 0.5turn and 30% aren't cut off (fix 4)
11
+ const matched = data.match(/[-]?(((repeating-)?(conic|linear|radial)-gradient)|(rrg|rg|lg|rcg|cg|rcg|rlg))([-_][\w_.\/#%-]+)/);
11
12
  if (matched) {
12
13
  const grad = matched[1];
13
14
  let gradientFunc = '';
14
15
  let holder = '';
15
- data = matched[6].replace(/^[-]([a-z])/, '$1');
16
+ // "%" works exactly like "p", and "." between digits exactly like "d" (12.5p = 12d5p)
17
+ data = matched[6].replace(/^[-]([a-z#])/, '$1').replace(/%/g, 'p').replace(/(?<!\/\d*)(\d)\.(\d)/g, '$1d$2'); // not in alpha: red/0.5 stays as is
16
18
  if (alias.hasOwnProperty(grad)) {
17
19
  // set gradient to gradient func e.g lineargradient()
18
20
  gradientFunc = alias[grad];
21
+ const isLinear = /linear/.test(gradientFunc);
22
+ const isConic = /conic/.test(gradientFunc);
19
23
  // For Css-Var
20
24
  if (data.match(/^--[a-zA-Z]/)) {
21
25
  masterHolder += `${gradientFunc}(var(${data})), `;
22
26
  return;
23
27
  }
24
28
  // setting direction/angle/position/shape e.g right/45deg/center/circle
25
- const m1 = /^[-]?-[0-9]+[d]?[0-9]*(deg|grad|rad|turn)/;
26
- const m2 = /^((to-)?(right|left|top|bottom)?[-]?(right|left|top|bottom))|^([t]?[rltb]?[rltb])(?=[0-9-])/;
29
+ const m1 = /^[-]?-?[0-9]+[d]?[0-9]*(deg|grad|rad|turn)/;
27
30
  if (m1.test(data)) {
28
31
  const m = m1.exec(data);
29
32
  if (m) {
30
- holder += m[0].replace(/[-]([-])?/, ' $1').replace(/[d](?=[0-9])/, '.') + ', ';
33
+ const angle = m[0].replace(/[-]([-])?/, ' $1').replace(/[d](?=[0-9])/, '.');
34
+ holder += (isConic ? ' from' : '') + angle + ', '; // conic needs "from 45deg" (fix 5)
31
35
  data = data.replace(m[0], '');
32
36
  }
33
37
  }
34
- else if (m2.test(data)) {
35
- const m = m2.exec(data);
38
+ else {
39
+ const m = DIRECTION.exec(data); // every alias key, longest first (fix 1)
36
40
  if (m) {
37
- // console.log(m2.match()[1]);
38
- if (alias.hasOwnProperty(m[0])) {
39
- holder += alias[m[0]] + ', ';
40
- }
41
- data = data.replace(m[0], '');
41
+ let dir = alias[m[1]];
42
+ // a linear gradient takes "to <side>", radial/conic take "at <position>" (fix 2)
43
+ if (isLinear && !/^to /.test(dir))
44
+ dir = 'to ' + dir;
45
+ else if (!isLinear && !/^to /.test(dir))
46
+ dir = 'at ' + dir;
47
+ holder += dir + ', ';
48
+ data = data.replace(m[1], '');
42
49
  }
43
50
  }
44
51
  data = data
45
52
  .replace(/_([0-9]+)[d]([0-9])/g, ' _$1.$2')
46
53
  .replace(/_/g, ' ')
47
54
  .replace(/-/g, ',');
48
- // console.log(data);
49
- data = data
50
- .replace(/^[A-Za-z0-9]+/g, (mch) => {
51
- // console.log(mch);
52
- const proccessedColor = color(mch.replace(/[,]/, ''), custom);
53
- return proccessedColor ? ', ' + proccessedColor : mch;
54
- })
55
- .replace(/[,][A-Za-z0-9]+/g, (mch) => {
56
- // console.log(mch);
57
- const proccessedColor = color(mch.replace(/[,]/, ''), custom);
58
- return proccessedColor ? ', ' + proccessedColor : mch;
55
+ // Colours first, exactly as before: any token (red, f00, 00f, slateTheme3, 25500000080p…),
56
+ // now also with alpha (red/0.5) (fix 4). Converted colours are parked behind placeholders so
57
+ // the number fixes below can't touch them: #d9d9e0 used to become #d9.9e0 (fix 3).
58
+ const colors = [];
59
+ data = data.replace(/(^|[,])(#?[A-Za-z0-9]+(?:\/[0-9.]+)?)/g, (mch, sep, value) => {
60
+ const processedColor = color(value, custom);
61
+ if (!processedColor)
62
+ return mch;
63
+ colors.push(processedColor);
64
+ return ', \u0000' + (colors.length - 1) + '\u0000';
59
65
  });
60
66
  data = data.replace(/(?<=[0-9])[p](?=[\W]|$)/g, '%').replace(/([0-9])[d]([0-9])/g, '$1.$2');
61
- // .replace(/(?<=[\W]([0-9])+)[d](?=[0-9])/g, ".");
67
+ data = data.replace(/\u0000(\d+)\u0000/g, (_m, i) => colors[Number(i)]);
62
68
  holder += data.replace(/^[,]/, '').replace(/[_]/g, '');
63
- // return gradientFunc + "( " + holder.replace(/,$/, "") + " )";
69
+ // keep the original spacing: the final ",50%" -> " 50%" step below relies on " )"
64
70
  masterHolder += gradientFunc + '(' + holder.replace(/,$/, '') + ' ),';
65
71
  }
66
72
  else {
@@ -131,7 +137,7 @@ const alias = {
131
137
  tbr: 'to bottom right',
132
138
  tbl: 'to bottom left',
133
139
  tlt: 'to left top',
134
- trt: 'to right top',
140
+ trt: 'to right top',
135
141
  trb: 'to right bottom',
136
142
  tlb: 'to left bottom',
137
143
  'closest-side': 'cs',
@@ -139,3 +145,13 @@ const alias = {
139
145
  'farthest-side': 'fs',
140
146
  'farthest-corner': 'fc',
141
147
  };
148
+ // Direction/position keys (not the gradient names or the shape keywords), longest first so
149
+ // "to-top-left" wins over "to-top", and a key must be followed by "-", "_" or the end:
150
+ // "r" doesn't match the start of "red".
151
+ const DIRECTION = new RegExp('^(' +
152
+ Object.keys(alias)
153
+ .filter((k) => !/gradient$|^(cg|rcg|lg|rg|rlg|rrg)$|^(closest|farthest)-/.test(k))
154
+ .sort((a, b) => b.length - a.length)
155
+ .map(escape)
156
+ .join('|') +
157
+ ')(?=[-_]|$)');
@@ -1,17 +1,42 @@
1
1
  export default function length(data) {
2
2
  if (data.match(/^-clamp\(/)) {
3
- return data.replace(/^-/, '')
4
- .replace(/([,+-/*])/g, '$1 ')
5
- .replace(/([+-/*])/g, ' $1');
3
+ // Note: [,+-/*] is a RANGE from "+" to "/" (it includes "," "-" "." "/"), so every
4
+ // decimal became "1 . 5rem" and a negative "-1rem" became " - 1rem".
5
+ const vars = [];
6
+ const v = data.replace(/^-/, '')
7
+ .replace(/var\(--[\w-]+(?:,[^()]*)?\)/g, (m) => { vars.push(m); return '\u0000' + (vars.length - 1) + '\u0000'; }) // keep --var-names intact
8
+ .replace(/(\d)d(\d)/g, '$1.$2') // 1d5rem -> 1.5rem
9
+ .replace(/(\d)p(?=[,)\s+*/-]|$)/g, '$1%') // 50p -> 50%
10
+ .replace(/\s*,\s*/g, ' , ') // "a , b", as before
11
+ .replace(/\s*([+*/])\s*/g, ' $1 ') // operators get spaces
12
+ .replace(/([\w%)\u0000])\s*-\s*(?=[\w.(\u0000])/g, '$1 - '); // "-" is subtraction only after a value
13
+ return v.replace(/\u0000(\d+)\u0000/g, (_m, i) => vars[Number(i)]);
6
14
  }
7
15
  else {
8
- return data.replace(/[-]?fit-content-([-]?\w+)/g, ' fit-content( $1 )')
16
+ // "." between digits works exactly like "d" (12.5rem = 12d5rem)
17
+ return data.replace(/(\d)\.(\d)/g, '$1d$2').replace(/[-]?fit-content-([-]?\w+)/g, ' fit-content( $1 )')
9
18
  .replace(/[-]([-]?[0-9])/g, ' $1').replace(/([\d])d([\d])/g, '$1.$2').replace(/-auto/g, ' auto')
10
- .replace(/([\d])p[\s]/g, "$1% ").replace(/([\d])p$/, "$1%").replace(/-(f|m)/g, '$1');
19
+ .replace(/([\d])p[\s]/g, "$1% ").replace(/([\d])p$/, "$1%").replace(/([\d])p(?=[,)])/g, "$1%").replace(/-(f|m)/g, '$1'); // 100p inside min()/max()
11
20
  }
12
21
  }
13
22
  export const lenByNumPer = (value) => value.replace(/[-]([-]?[\w])/g, ' $1').replace(/([\d])d([\d])/g, '$1.$2').replace(/([\d])p[\s]/g, "$1% ").replace(/([\d])p$/, "$1%").replace(/[\s]by[\s]/g, ' / ').replace(/auto flow/g, 'auto-flow');
14
- // const lenNumPer=(value:string)=>value.replace(/[-]([-]?[\w])/g,' $1').replace(/([\d])d([\d])/g,'$1.$2').replace(/([\d])p[\s]/g,"$1% ").replace(/([\d])p$/,"$1%").replace(/([a-z])([\d-])/g,'$1 $2');
15
- // const signedLenNumPer=(value:string)=>value.replace(/([\d])d([\d])/g,'$1.$2').replace(/([a-z])([\d-])/g,'$1 $2').replace(/([\d])p[\s]/g,"$1% ").replace(/([\d])p$/,"$1%").replace(/[-]([a-z])/g,'$1');
16
- // const lenByNumPer=(value:string)=>value.replace(/[-]([-]?[\w])/g,' $1').replace(/([\d])d([\d])/g,'$1.$2').replace(/([\d])p[\s]/g,"$1% ").replace(/([\d])p$/,"$1%").replace(/[\s]by[\s]/g,' / ');
17
- // const lenFitContent=(value:string)=>value.replace(/fit-content-([-]?\w+)/,'fit-content( $1 )').replace(/[-]([-]?[\w])/g,' $1').replace(/([\d])d([\d])/g,' $1.$2 ').replace(/([\d])p[\s]/g,"$1% ").replace(/([\d])p$/,"$1%");
23
+ /*
24
+ export default function length(data:string){
25
+ if(data.match(/^-clamp\(/)){
26
+ return data.replace(/^-/,'')
27
+ .replace(/([,+-/*])/g,'$1 ')
28
+ .replace(/([+-/*])/g,' $1')
29
+ }else{
30
+ return data.replace(/[-]?fit-content-([-]?\w+)/g,' fit-content( $1 )')
31
+ .replace(/[-]([-]?[0-9])/g,' $1').replace(/([\d])d([\d])/g,'$1.$2').replace(/-auto/g,' auto')
32
+ .replace(/([\d])p[\s]/g,"$1% ").replace(/([\d])p$/,"$1%").replace(/-(f|m)/g,'$1')
33
+ }
34
+ }
35
+ export const lenByNumPer=(value:string)=>value.replace(/[-]([-]?[\w])/g,' $1').replace(/([\d])d([\d])/g,'$1.$2').replace(/([\d])p[\s]/g,"$1% ").replace(/([\d])p$/,"$1%").replace(/[\s]by[\s]/g,' / ').replace(/auto flow/g,'auto-flow');
36
+
37
+
38
+ // const lenNumPer=(value:string)=>value.replace(/[-]([-]?[\w])/g,' $1').replace(/([\d])d([\d])/g,'$1.$2').replace(/([\d])p[\s]/g,"$1% ").replace(/([\d])p$/,"$1%").replace(/([a-z])([\d-])/g,'$1 $2');
39
+ // const signedLenNumPer=(value:string)=>value.replace(/([\d])d([\d])/g,'$1.$2').replace(/([a-z])([\d-])/g,'$1 $2').replace(/([\d])p[\s]/g,"$1% ").replace(/([\d])p$/,"$1%").replace(/[-]([a-z])/g,'$1');
40
+ // const lenByNumPer=(value:string)=>value.replace(/[-]([-]?[\w])/g,' $1').replace(/([\d])d([\d])/g,'$1.$2').replace(/([\d])p[\s]/g,"$1% ").replace(/([\d])p$/,"$1%").replace(/[\s]by[\s]/g,' / ');
41
+ // const lenFitContent=(value:string)=>value.replace(/fit-content-([-]?\w+)/,'fit-content( $1 )').replace(/[-]([-]?[\w])/g,' $1').replace(/([\d])d([\d])/g,' $1.$2 ').replace(/([\d])p[\s]/g,"$1% ").replace(/([\d])p$/,"$1%");
42
+ */