aliascss 1.4.7 → 1.5.0

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.
@@ -11,13 +11,82 @@ import { createCompilerObj, extractProperty } from "./utils/createCompilerObj.js
11
11
  import config from "./config.js";
12
12
  import cssProps from "./css-properties-all.js";
13
13
  import { staticClassNamesAlias } from "./static/staticClassNamesAlias.js";
14
- import postcss from "postcss";
15
14
  import { splitByCommaOutsideParens, matchTrailingGroup, splitTopLevel, joinGroupItem } from "./utils/helper.js";
16
15
  import cssCustomCompilers from "./custom-css-compilers.js";
17
16
  // import customGroups from './prebuild.js';
18
17
  const [staticClassNamesWithAlias, cssPropsWithAlias] = createCompilerObj(cssProps, config.globalValues);
19
18
  const NAME = '[a-zA-Z_][\\w-]*(?:\\/[a-zA-Z_][\\w-]*)*';
20
19
  const AS_EXPORT = new RegExp(`--as-(?<names>${NAME}|\\[${NAME}(?:,${NAME})*\\])$`);
20
+ // Built once per prefix value instead of on every class
21
+ let prefixCache = null;
22
+ const prefixRegex = (prefix) => {
23
+ if (!prefixCache || prefixCache.prefix !== prefix) {
24
+ prefixCache = { prefix, re: new RegExp('^' + prefix.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') + '-') };
25
+ }
26
+ return prefixCache.re;
27
+ };
28
+ // Classes that clearly tried to be AliasCSS (used for warnings only)
29
+ const ACSS_SYNTAX = /^[@_\[]|^--|--as-|\[.*\]$/;
30
+ // Safety check for a generated statement before it is emitted.
31
+ // A single bad statement must never damage the CSS that comes after it, so we reject:
32
+ // - unbalanced (), [], {} → would swallow or close the following rules / layers
33
+ // - an unclosed quote → the rest of the stylesheet becomes part of a string
34
+ // - "/*" outside quotes → opens a comment that hides everything after it
35
+ // - "</style" anywhere → ends an inline <style> tag in server-rendered HTML
36
+ // - no "property:value" in {} → empty or meaningless rule
37
+ // Escaped characters (\( \[ \{ …) in selectors are skipped.
38
+ // (postcss.parse checked only syntax too, but put the whole parser in the browser bundle.)
39
+ export function isSafeCss(css) {
40
+ if (/<\/style/i.test(css))
41
+ return false;
42
+ const stack = [];
43
+ const pairs = { ')': '(', ']': '[', '}': '{' };
44
+ let quote = '';
45
+ for (let i = 0; i < css.length; i++) {
46
+ const c = css[i];
47
+ if (c === '\\') {
48
+ i++;
49
+ continue;
50
+ }
51
+ if (quote) {
52
+ if (c === quote)
53
+ quote = '';
54
+ else if (c === '\n')
55
+ return false; // CSS strings can't span lines
56
+ continue;
57
+ }
58
+ if (c === '"' || c === "'") {
59
+ quote = c;
60
+ continue;
61
+ }
62
+ if (c === '/' && css[i + 1] === '*')
63
+ return false;
64
+ if (c === '(' || c === '[' || c === '{')
65
+ stack.push(c);
66
+ else if (c in pairs && stack.pop() !== pairs[c])
67
+ return false;
68
+ }
69
+ return !quote && stack.length === 0 && /\{[^{}]*:[^{}]*\}/.test(css);
70
+ }
71
+ // ---- Authoring warnings (the CSS is still emitted; these point at likely mistakes) ----
72
+ // "+" or "-" without spaces inside calc()/min()/max()/clamp(): calc(100%-32px) is invalid CSS.
73
+ // var(...) is removed first so names like --space-2-5 don't trigger it.
74
+ const CALC_FN = /\b(?:calc|min|max|clamp)\(/;
75
+ const NO_SPACE_MATH = /(?:\d(?:%|[a-z]+)?|\))[+-](?=[\d.(]|var\b)/i;
76
+ function calcSpacingProblem(declaration) {
77
+ if (!CALC_FN.test(declaration))
78
+ return false;
79
+ let d = declaration;
80
+ let prev = '';
81
+ while (d !== prev) {
82
+ prev = d;
83
+ d = d.replace(/var\([^()]*\)/g, '0');
84
+ } // innermost var() first, kept as a number
85
+ return NO_SPACE_MATH.test(d);
86
+ }
87
+ // A pseudo-class after these pseudo-elements never matches (::before:hover).
88
+ // Not included on purpose: ::file-selector-button, ::-webkit-scrollbar*, ::part() — those allow it.
89
+ const PSEUDO_ELEMENT_THEN_CLASS = /::(?:before|after|placeholder|marker|selection|first-line|first-letter|backdrop)(?=:[a-z])/;
21
90
  export const compiler = {
22
91
  // cache will cache propertyAndValue
23
92
  cache: { propertyAndValue: Object.assign({}, customStaticClassNames) },
@@ -62,11 +131,9 @@ export const compiler = {
62
131
  }
63
132
  }
64
133
  // Budle atomice class name in new
65
- if (AS_EXPORT.test(className)) {
66
- const asMatch = (_a = className.match(AS_EXPORT)) !== null && _a !== void 0 ? _a : [null, null];
67
- if (asMatch[0] && asMatch[1]) {
68
- return this.group(className.replace(asMatch[0], ''), asMatch[1]);
69
- }
134
+ const asMatch = className.match(AS_EXPORT);
135
+ if ((_a = asMatch === null || asMatch === void 0 ? void 0 : asMatch.groups) === null || _a === void 0 ? void 0 : _a.names) {
136
+ return this.group(className.slice(0, asMatch.index), asMatch.groups.names);
70
137
  }
71
138
  let unPrefixedClassName = className;
72
139
  // check for [group-acss,]
@@ -88,11 +155,11 @@ export const compiler = {
88
155
  }
89
156
  // EONEW
90
157
  let beforeClassNameSelector = '';
91
- if (this.prefix && !className.match(new RegExp('^' + this.prefix + "[-]"))) {
92
- return null;
93
- }
94
- else if (this.prefix) {
95
- unPrefixedClassName = className.replace(new RegExp('^' + this.prefix + "[-]"), '');
158
+ if (this.prefix) {
159
+ const re = prefixRegex(this.prefix);
160
+ if (!re.test(className))
161
+ return null;
162
+ unPrefixedClassName = className.replace(re, '');
96
163
  }
97
164
  if (!this.mediaTest)
98
165
  this.mediaTest = createRegexForMedia(this.mediaSelector);
@@ -166,6 +233,15 @@ export const compiler = {
166
233
  // ------------PostCSSParse
167
234
  if (result) {
168
235
  let stm = '';
236
+ if (bool !== true) {
237
+ const shown = as ? `${as} (${className})` : className;
238
+ if (calcSpacingProblem(result)) {
239
+ console.warn(`AliasCSS: "${shown}": "+" and "-" need spaces in calc(), write _-_ e.g. calc(100%_-_32px)`);
240
+ }
241
+ if (PSEUDO_ELEMENT_THEN_CLASS.test(elementAndPseudo)) {
242
+ console.warn(`AliasCSS: "${shown}": "${elementAndPseudo.trim()}" never matches; put the state first, e.g. --hover--before`);
243
+ }
244
+ }
169
245
  className = (as ? as : className).replace(/([$.&%=\]\[@~,:*#+\(\)\/^/</>\\|])/g, '\\$1');
170
246
  if (bool === true)
171
247
  return result;
@@ -189,16 +265,16 @@ export const compiler = {
189
265
  else {
190
266
  stm = `${beforeClassNameSelector}.${className}${elementAndPseudo}{${result}${important}}`;
191
267
  }
192
- try {
193
- postcss.parse(stm);
268
+ if (isSafeCss(stm))
194
269
  return stm;
195
- }
196
- catch (error) {
197
- console.log('Not a Valid CSS Statement, PostCSS Parse:', stm);
198
- }
270
+ console.warn(`AliasCSS: "${as ? as : className}" produced invalid CSS:`, stm);
199
271
  }
200
272
  else {
201
- console.log(`Unable to process ${className} [media:${mediaEX},pseudoSelector:${elementAndPseudo},imp:${important}]`);
273
+ const looksLikeAcss = ACSS_SYNTAX.test(className) || !!extractProperty(pnv, this.cssProps);
274
+ if (!this.groupDepth && (looksLikeAcss || config.debug)) {
275
+ console.warn(`AliasCSS: can't compile "${className}"`);
276
+ }
277
+ // console.log(`Unable to process ${className} [media:${mediaEX},pseudoSelector:${elementAndPseudo},imp:${important}]`)
202
278
  }
203
279
  },
204
280
  makeNested(className, as, bool) {
@@ -332,13 +408,9 @@ export const compiler = {
332
408
  else {
333
409
  stm = `${beforeClassNameSelector}.${className}${elementAndPseudo}{${result}${important}}`;
334
410
  }
335
- try {
336
- postcss.parse(stm);
411
+ if (isSafeCss(stm))
337
412
  return stm;
338
- }
339
- catch (error) {
340
- console.log('Not a Valid CSS Statement, PostCSS Parse:', stm);
341
- }
413
+ console.log('Not a Valid CSS Statement:', stm);
342
414
  }
343
415
  else {
344
416
  console.log(`Unable to process ${className} [media:${mediaEX},pseudoSelector:${elementAndPseudo},imp:${important}]`);
@@ -349,11 +421,26 @@ export const compiler = {
349
421
  // },
350
422
  group(str, as) {
351
423
  let statement = '';
352
- [...str.trim().split(/\s+/)].forEach((e) => {
353
- const getEachClassNameCompiled = this.make(e, as);
354
- if (getEachClassNameCompiled)
355
- statement += getEachClassNameCompiled + ' \n';
356
- });
424
+ const failed = [];
425
+ this.groupDepth = (this.groupDepth || 0) + 1;
426
+ try {
427
+ [...str.trim().split(/\s+/)].forEach((e) => {
428
+ if (!e)
429
+ return;
430
+ const getEachClassNameCompiled = this.make(e, as);
431
+ if (getEachClassNameCompiled)
432
+ statement += getEachClassNameCompiled + ' \n';
433
+ else
434
+ failed.push(e);
435
+ });
436
+ }
437
+ finally {
438
+ this.groupDepth--;
439
+ }
440
+ // Items inside a group or statement are always meant as AliasCSS, so always report them
441
+ if (failed.length) {
442
+ console.warn(`AliasCSS: in "${as}", could not compile: ${failed.join(', ')}`);
443
+ }
357
444
  return statement;
358
445
  },
359
446
  groupMulti(str, as) {
@@ -1,4 +1,30 @@
1
1
  declare const xComponents: {
2
+ 'x-row': {
3
+ type: string;
4
+ compiler: (value: string) => string;
5
+ groups: {
6
+ '': string;
7
+ xs: string;
8
+ sm: string;
9
+ md: string;
10
+ lg: string;
11
+ xl: string;
12
+ };
13
+ };
14
+ 'x-col': {
15
+ type: string;
16
+ compiler: (value: string) => string;
17
+ groups: {
18
+ [k: string]: string;
19
+ };
20
+ };
21
+ 'x-col-offset': {
22
+ type: string;
23
+ compiler: (value: string) => string;
24
+ groups: {
25
+ [k: string]: string;
26
+ };
27
+ };
2
28
  'x-absolute-0': {
3
29
  type: string;
4
30
  statement: string;
@@ -7,5 +7,6 @@ import moreComponents from "./x-comps.js";
7
7
  import layout from "./x-layout-components.js";
8
8
  import menuComponents from "./x-menu.js";
9
9
  import absoluteComponents from "./x-helper-components.js";
10
- const xComponents = Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({}, components), formComponents), moreComponents), navContent), themeComponents), tableComponents), layout), menuComponents), absoluteComponents);
10
+ import xRowCol from "./x-row-col.js";
11
+ const xComponents = Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({}, components), formComponents), moreComponents), navContent), themeComponents), tableComponents), layout), menuComponents), absoluteComponents), xRowCol);
11
12
  export default xComponents;
@@ -52,7 +52,7 @@ const components = {
52
52
  },
53
53
  'x-card-elevated': {
54
54
  type: 'statement',
55
- statement: `--x-card-border-color:transparent --x-card-shadow:(0_1px_3px_rgb(16_24_40_/_0.1),0_1px_2px_rgb(16_24_40_/_0.06))`
55
+ statement: `--x-card-border-color:(light-dark(transparent,rgb(255_255_255_/_0.08))) --x-card-shadow:(0_1px_3px_rgb(16_24_40_/_0.1),0_1px_2px_rgb(16_24_40_/_0.06))`
56
56
  },
57
57
  'x-card-flat': {
58
58
  type: 'statement',
@@ -0,0 +1,29 @@
1
+ declare const _default: {
2
+ 'x-row': {
3
+ type: string;
4
+ compiler: (value: string) => string;
5
+ groups: {
6
+ '': string;
7
+ xs: string;
8
+ sm: string;
9
+ md: string;
10
+ lg: string;
11
+ xl: string;
12
+ };
13
+ };
14
+ 'x-col': {
15
+ type: string;
16
+ compiler: (value: string) => string;
17
+ groups: {
18
+ [k: string]: string;
19
+ };
20
+ };
21
+ 'x-col-offset': {
22
+ type: string;
23
+ compiler: (value: string) => string;
24
+ groups: {
25
+ [k: string]: string;
26
+ };
27
+ };
28
+ };
29
+ export default _default;
@@ -0,0 +1,52 @@
1
+ // ------------------------------------------------------------------
2
+ // x-row / x-col: 12-column grid (Bootstrap-style), built on flex + gap.
3
+ //
4
+ // <div class="x-row"> gap 16px (default)
5
+ // <div class="x-row-lg"> gap 24px (xs 4, sm 8, md 16, lg 24, xl 32)
6
+ // <div class="x-row-20px"> any gap
7
+ // <div class="x-col-4">…</div> 4 of 12 columns
8
+ // <div class="x-col">…</div> fill the remaining space (equal share)
9
+ // <div class="x-col-auto">…</div> as wide as its content
10
+ // <div class="x-col-240px">…</div> fixed width
11
+ // <div class="x-col-offset-2 x-col-8">…</div> skip 2 columns
12
+ //
13
+ // Responsive: write every breakpoint explicitly (plain classes beat breakpoint classes):
14
+ // <div class="@xs-x-col-12 @sm-x-col-6 @lg-x-col-4">
15
+ //
16
+ // vars: --x-row-gap (16px), --x-row-gap-y (vertical gap, defaults to --x-row-gap)
17
+ // ------------------------------------------------------------------
18
+ const GAP = 'var(--x-row-gap,16px)';
19
+ const clean = (v) => v.trim().replace(/^-/, '');
20
+ // Width of N columns out of 12, with (12 - 1) gaps shared exactly
21
+ const spanWidth = (n) => `calc((100% - 11 * ${GAP}) / 12 * ${n} + (${n} - 1) * ${GAP})`;
22
+ const row = (gap) => `--x-row-gap:${gap};display:flex;flex-wrap:wrap;gap:var(--x-row-gap-y,var(--x-row-gap)) var(--x-row-gap);box-sizing:border-box;`;
23
+ const col = (v) => {
24
+ if (v === '' || v === 'fill')
25
+ return 'flex:1 1 0%;min-width:0;box-sizing:border-box;';
26
+ if (v === 'auto')
27
+ return 'flex:0 0 auto;width:auto;max-width:100%;box-sizing:border-box;';
28
+ if (/^([1-9]|1[0-2])$/.test(v))
29
+ return `flex:0 0 ${spanWidth(v)};max-width:100%;min-width:0;box-sizing:border-box;`;
30
+ return `flex:0 0 ${v};max-width:100%;min-width:0;box-sizing:border-box;`; // 240px, 30%, 20rem…
31
+ };
32
+ const offset = (v) => /^([0-9]|1[01])$/.test(v)
33
+ ? `margin-inline-start:${v === '0' ? '0' : `calc((100% - 11 * ${GAP}) / 12 * ${v} + ${v} * ${GAP})`};`
34
+ : `margin-inline-start:${v};`;
35
+ const range12 = Array.from({ length: 12 }, (_, i) => String(i + 1));
36
+ export default {
37
+ 'x-row': {
38
+ type: 'group',
39
+ compiler: (value) => row(clean(value) || '16px'),
40
+ groups: { '': row('16px'), xs: row('4px'), sm: row('8px'), md: row('16px'), lg: row('24px'), xl: row('32px') },
41
+ },
42
+ 'x-col': {
43
+ type: 'group',
44
+ compiler: (value) => col(clean(value)),
45
+ groups: Object.fromEntries([...range12, '', 'fill', 'auto'].map(k => [k, col(k)])),
46
+ },
47
+ 'x-col-offset': {
48
+ type: 'group',
49
+ compiler: (value) => offset(clean(value)),
50
+ groups: Object.fromEntries(['0', ...range12.slice(0, 11)].map(k => [k, offset(k)])),
51
+ },
52
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "aliascss",
3
- "version": "1.4.7",
3
+ "version": "1.5.0",
4
4
  "description": "AliasCSS is a CSS post processor.",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",