@projectwallace/format-css 2.0.0 → 2.0.1
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/format-css.js +69 -66
- package/package.json +8 -10
package/dist/format-css.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import h from "css-tree/parser";
|
|
2
|
-
const o = " ",
|
|
3
|
-
function
|
|
4
|
-
return /[A-Z]/.test(
|
|
2
|
+
const o = " ", c = "", N = ":", $ = ";", L = '"', x = "(", g = ")", d = "[", ee = "]", H = "{", V = "}", te = "{}", W = ",", Z = "Atrule", z = "Rule", F = "Block", G = "SelectorList", I = "Selector", Y = "Declaration", B = "Operator";
|
|
3
|
+
function C(y) {
|
|
4
|
+
return /[A-Z]/.test(y) ? y.toLowerCase() : y;
|
|
5
5
|
}
|
|
6
|
-
function re(
|
|
7
|
-
let O = [], v = h(
|
|
6
|
+
function re(y, { minify: P = !1 } = {}) {
|
|
7
|
+
let O = [], v = h(y, {
|
|
8
8
|
positions: !0,
|
|
9
9
|
parseAtrulePrelude: !1,
|
|
10
10
|
parseCustomProperty: !0,
|
|
@@ -13,55 +13,55 @@ function re(_, { minify: P = !1 } = {}) {
|
|
|
13
13
|
O.push(e.start.offset, e.end.offset);
|
|
14
14
|
}
|
|
15
15
|
});
|
|
16
|
-
const u = P ?
|
|
17
|
-
`,
|
|
18
|
-
let
|
|
19
|
-
function
|
|
20
|
-
return P ?
|
|
16
|
+
const u = P ? c : `
|
|
17
|
+
`, p = P ? c : o, Q = P ? c : $;
|
|
18
|
+
let s = 0;
|
|
19
|
+
function a(n) {
|
|
20
|
+
return P ? c : " ".repeat(n);
|
|
21
21
|
}
|
|
22
22
|
function A(n) {
|
|
23
23
|
let e = n.loc;
|
|
24
|
-
return e ?
|
|
24
|
+
return e ? y.slice(e.start.offset, e.end.offset) : c;
|
|
25
25
|
}
|
|
26
|
-
function
|
|
26
|
+
function b(n) {
|
|
27
27
|
return /** @type {import('css-tree').CssLocation} */ n.loc.start.offset;
|
|
28
28
|
}
|
|
29
|
-
function
|
|
29
|
+
function E(n) {
|
|
30
30
|
return /** @type {import('css-tree').CssLocation} */ n.loc.end.offset;
|
|
31
31
|
}
|
|
32
|
-
function
|
|
32
|
+
function m(n, e) {
|
|
33
33
|
if (P || n === void 0 || e === void 0)
|
|
34
|
-
return
|
|
34
|
+
return c;
|
|
35
35
|
let t = "";
|
|
36
36
|
for (let r = 0; r < O.length; r += 2) {
|
|
37
37
|
let l = O[r];
|
|
38
38
|
if (l === void 0 || l < n) continue;
|
|
39
39
|
let f = O[r + 1];
|
|
40
40
|
if (f === void 0 || f > e) break;
|
|
41
|
-
t.length > 0 && (t += u + s
|
|
41
|
+
t.length > 0 && (t += u + a(s)), t += y.slice(l, f);
|
|
42
42
|
}
|
|
43
43
|
return t;
|
|
44
44
|
}
|
|
45
45
|
function w(n) {
|
|
46
46
|
let e, t = n.prelude, r = n.block;
|
|
47
47
|
t.type === G && (e = j(t));
|
|
48
|
-
let l = E(
|
|
49
|
-
return l && (e += u + s
|
|
48
|
+
let l = m(E(t), b(r));
|
|
49
|
+
return l && (e += u + a(s) + l), r.type === F && (e += M(r)), e;
|
|
50
50
|
}
|
|
51
51
|
function j(n) {
|
|
52
|
-
let e =
|
|
52
|
+
let e = c;
|
|
53
53
|
return n.children.forEach((t, r) => {
|
|
54
|
-
t.type ===
|
|
55
|
-
let l = r.next !== null ?
|
|
56
|
-
f && (e += s
|
|
54
|
+
t.type === I && (e += a(s) + S(t)), r.next !== null && (e += W + u);
|
|
55
|
+
let l = r.next !== null ? b(r.next.data) : E(n), f = m(E(t), l);
|
|
56
|
+
f && (e += a(s) + f + u);
|
|
57
57
|
}), e;
|
|
58
58
|
}
|
|
59
59
|
function S(n) {
|
|
60
|
-
let e =
|
|
60
|
+
let e = c;
|
|
61
61
|
return (n.children || []).forEach((r) => {
|
|
62
62
|
switch (r.type) {
|
|
63
63
|
case "TypeSelector": {
|
|
64
|
-
e +=
|
|
64
|
+
e += C(r.name);
|
|
65
65
|
break;
|
|
66
66
|
}
|
|
67
67
|
case "Combinator": {
|
|
@@ -71,13 +71,13 @@ function re(_, { minify: P = !1 } = {}) {
|
|
|
71
71
|
case "PseudoClassSelector":
|
|
72
72
|
case "PseudoElementSelector": {
|
|
73
73
|
e += N;
|
|
74
|
-
let l =
|
|
75
|
-
(l === "before" || l === "after" || r.type === "PseudoElementSelector") && (e += N), e += l, r.children && (e +=
|
|
74
|
+
let l = C(r.name);
|
|
75
|
+
(l === "before" || l === "after" || r.type === "PseudoElementSelector") && (e += N), e += l, r.children && (e += x + S(r) + g);
|
|
76
76
|
break;
|
|
77
77
|
}
|
|
78
78
|
case G: {
|
|
79
79
|
r.children.forEach((l, f) => {
|
|
80
|
-
l.type ===
|
|
80
|
+
l.type === I && (e += S(l)), f.next && f.next.data.type === I && (e += W + p);
|
|
81
81
|
});
|
|
82
82
|
break;
|
|
83
83
|
}
|
|
@@ -85,8 +85,8 @@ function re(_, { minify: P = !1 } = {}) {
|
|
|
85
85
|
let l = r.nth;
|
|
86
86
|
if (l)
|
|
87
87
|
if (l.type === "AnPlusB") {
|
|
88
|
-
let f = l.a,
|
|
89
|
-
f !== null && (e += f + "n"), f !== null &&
|
|
88
|
+
let f = l.a, i = l.b;
|
|
89
|
+
f !== null && (e += f + "n"), f !== null && i !== null && (e += o), i !== null && (f !== null && !i.startsWith("-") && (e += "+" + o), e += i);
|
|
90
90
|
} else
|
|
91
91
|
e += A(l);
|
|
92
92
|
r.selector !== null && (e += o + "of" + o + S(r.selector));
|
|
@@ -104,80 +104,83 @@ function re(_, { minify: P = !1 } = {}) {
|
|
|
104
104
|
}), e;
|
|
105
105
|
}
|
|
106
106
|
function M(n) {
|
|
107
|
-
let e = n.children, t =
|
|
107
|
+
let e = n.children, t = p;
|
|
108
108
|
if (e.isEmpty) {
|
|
109
|
-
let f =
|
|
110
|
-
return f ? (t += H + u, t += s
|
|
109
|
+
let f = m(b(n), E(n));
|
|
110
|
+
return f ? (t += H + u, t += a(s + 1) + f, t += u + a(s) + V, t) : t + te;
|
|
111
111
|
}
|
|
112
|
-
t += H + u,
|
|
113
|
-
let r =
|
|
112
|
+
t += H + u, s++;
|
|
113
|
+
let r = m(b(n), b(
|
|
114
114
|
/** @type {import('css-tree').CssNode} */
|
|
115
115
|
e.first
|
|
116
116
|
));
|
|
117
|
-
r && (t += s
|
|
118
|
-
if (
|
|
119
|
-
let D = E(
|
|
120
|
-
D && (t += s
|
|
117
|
+
r && (t += a(s) + r + u), e.forEach((f, i) => {
|
|
118
|
+
if (i.prev !== null) {
|
|
119
|
+
let D = m(E(i.prev.data), b(f));
|
|
120
|
+
D && (t += a(s) + D + u);
|
|
121
121
|
}
|
|
122
|
-
f.type ===
|
|
122
|
+
f.type === Y ? (t += J(f), i.next === null ? t += Q : t += $) : (i.prev !== null && i.prev.data.type === Y && (t += u), f.type === z ? t += w(f) : f.type === Z ? t += K(f) : t += k(f, s)), i.next !== null && (t += u, f.type !== Y && (t += u));
|
|
123
123
|
});
|
|
124
|
-
let l = E(
|
|
124
|
+
let l = m(E(
|
|
125
125
|
/** @type {import('css-tree').CssNode} */
|
|
126
126
|
e.last
|
|
127
|
-
),
|
|
128
|
-
return l && (t += u + s
|
|
127
|
+
), E(n));
|
|
128
|
+
return l && (t += u + a(s) + l), s--, t += u + a(s) + V, t;
|
|
129
129
|
}
|
|
130
130
|
function K(n) {
|
|
131
|
-
let e = s
|
|
132
|
-
return e +=
|
|
131
|
+
let e = a(s) + "@", t = n.prelude, r = n.block;
|
|
132
|
+
return e += C(n.name), t !== null && (e += o + q(t)), r === null ? e += $ : r.type === F && (e += M(r)), e;
|
|
133
133
|
}
|
|
134
134
|
function q(n) {
|
|
135
135
|
let e = A(n);
|
|
136
|
-
return e.replace(/\s*([:,])/g, e.toLowerCase().includes("selector(") ? "$1" : "$1 ").replace(/\
|
|
136
|
+
return e.replace(/\s*([:,])/g, e.toLowerCase().includes("selector(") ? "$1" : "$1 ").replace(/\)([a-zA-Z])/g, ") $1").replace(/\s*(=>|<=)\s*/g, " $1 ").replace(/([^<>=\s])([<>])([^<>=\s])/g, `$1${p}$2${p}$3`).replace(/\s+/g, p).replace(/calc\(\s*([^()+\-*/]+)\s*([*/+-])\s*([^()+\-*/]+)\s*\)/g, (t, r, l, f) => {
|
|
137
|
+
let i = l === "+" || l === "-" ? o : p;
|
|
138
|
+
return `calc(${r.trim()}${i}${l}${i}${f.trim()})`;
|
|
139
|
+
}).replace(/selector|url|supports|layer\(/ig, (t) => C(t));
|
|
137
140
|
}
|
|
138
141
|
function J(n) {
|
|
139
142
|
let e = n.property;
|
|
140
|
-
e.charCodeAt(0) === 45 && e.charCodeAt(1) === 45 || (e =
|
|
143
|
+
e.charCodeAt(0) === 45 && e.charCodeAt(1) === 45 || (e = C(e));
|
|
141
144
|
let t = U(n.value);
|
|
142
|
-
return e === "font" && (t = t.replace(/\s*\/\s*/, "/")), t ===
|
|
145
|
+
return e === "font" && (t = t.replace(/\s*\/\s*/, "/")), t === c && P && (t += o), n.important === !0 ? t += p + "!important" : typeof n.important == "string" && (t += p + "!" + C(n.important)), a(s) + e + N + p + t;
|
|
143
146
|
}
|
|
144
147
|
function R(n) {
|
|
145
|
-
let e =
|
|
148
|
+
let e = c;
|
|
146
149
|
return n.forEach((t, r) => {
|
|
147
|
-
t.type === "Identifier" ? e += t.name : t.type === "Function" ? e +=
|
|
150
|
+
t.type === "Identifier" ? e += t.name : t.type === "Function" ? e += C(t.name) + x + R(t.children) + g : t.type === "Dimension" ? e += t.value + C(t.unit) : t.type === "Value" ? e += U(t) : t.type === B ? e += X(t) : t.type === "Parentheses" ? e += x + R(t.children) + g : t.type === "Url" ? e += "url(" + L + t.value + L + g : e += A(t), t.type !== B && r.next !== null && r.next.data.type !== B && (e += o);
|
|
148
151
|
}), e;
|
|
149
152
|
}
|
|
150
153
|
function X(n) {
|
|
151
|
-
let e =
|
|
152
|
-
return r === 43 || r === 45 ? e += o : r !== 44 && (e +=
|
|
154
|
+
let e = c, t = n.value.trim(), r = t.charCodeAt(0);
|
|
155
|
+
return r === 43 || r === 45 ? e += o : r !== 44 && (e += p), e += t, r === 43 || r === 45 ? e += o : e += p, e;
|
|
153
156
|
}
|
|
154
157
|
function U(n) {
|
|
155
158
|
return n.type === "Raw" ? k(n, 0) : R(n.children);
|
|
156
159
|
}
|
|
157
160
|
function k(n, e) {
|
|
158
|
-
return
|
|
161
|
+
return a(e) + A(n).trim();
|
|
159
162
|
}
|
|
160
|
-
let T = v.children,
|
|
163
|
+
let T = v.children, _ = c;
|
|
161
164
|
if (T.first) {
|
|
162
|
-
let n =
|
|
163
|
-
n && (
|
|
164
|
-
if (t.type === z ?
|
|
165
|
-
|
|
166
|
-
let l = E(
|
|
167
|
-
l && (
|
|
165
|
+
let n = m(0, b(T.first));
|
|
166
|
+
n && (_ += n + u), T.forEach((t, r) => {
|
|
167
|
+
if (t.type === z ? _ += w(t) : t.type === Z ? _ += K(t) : _ += k(t, s), r.next !== null) {
|
|
168
|
+
_ += u;
|
|
169
|
+
let l = m(E(t), b(r.next.data));
|
|
170
|
+
l && (_ += a(s) + l), _ += u;
|
|
168
171
|
}
|
|
169
172
|
});
|
|
170
|
-
let e = E(
|
|
173
|
+
let e = m(E(
|
|
171
174
|
/** @type {import('css-tree').CssNode} */
|
|
172
175
|
T.last
|
|
173
|
-
),
|
|
174
|
-
e && (
|
|
176
|
+
), E(v));
|
|
177
|
+
e && (_ += u + e);
|
|
175
178
|
} else
|
|
176
|
-
|
|
177
|
-
return
|
|
179
|
+
_ += m(0, E(v));
|
|
180
|
+
return _;
|
|
178
181
|
}
|
|
179
|
-
function le(
|
|
180
|
-
return re(
|
|
182
|
+
function le(y) {
|
|
183
|
+
return re(y, { minify: !0 });
|
|
181
184
|
}
|
|
182
185
|
export {
|
|
183
186
|
re as format,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@projectwallace/format-css",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.1",
|
|
4
4
|
"description": "Fast, small, zero-config library to format or minify CSS with basic rules.",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -26,16 +26,14 @@
|
|
|
26
26
|
"prettier": "prettier --check index.js test/**/*.js"
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|
|
29
|
-
"@codecov/vite-plugin": "^1.
|
|
30
|
-
"@
|
|
31
|
-
"
|
|
32
|
-
"
|
|
33
|
-
"
|
|
34
|
-
"tinybench": "^2.8.0",
|
|
35
|
-
"typescript": "^5.6.3",
|
|
29
|
+
"@codecov/vite-plugin": "^1.9.0",
|
|
30
|
+
"@types/css-tree": "^2.3.10",
|
|
31
|
+
"c8": "^10.1.3",
|
|
32
|
+
"prettier": "^3.5.3",
|
|
33
|
+
"typescript": "^5.8.3",
|
|
36
34
|
"uvu": "^0.5.6",
|
|
37
|
-
"vite": "^
|
|
38
|
-
"vite-plugin-dts": "^4.3
|
|
35
|
+
"vite": "^6.2.6",
|
|
36
|
+
"vite-plugin-dts": "^4.5.3"
|
|
39
37
|
},
|
|
40
38
|
"dependencies": {
|
|
41
39
|
"css-tree": "^3.0.1"
|