@projectwallace/format-css 2.2.3 → 2.2.5

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/README.md CHANGED
@@ -91,10 +91,6 @@ let formatted = format("a { color: red; }", {
91
91
  });
92
92
  ```
93
93
 
94
- ## Acknowledgements
95
-
96
- - Thanks to [CSSTree](https://github.com/csstree/csstree) for providing the necessary parser and the interfaces for our CSS Types (the **bold** elements in the list above)
97
-
98
94
  ## Related projects
99
95
 
100
96
  - [Format CSS online](https://www.projectwallace.com/prettify-css?utm_source=github&utm_medium=wallace_format_css_related_projects) - See this formatter in action online!
@@ -1,103 +1,87 @@
1
- import { parse as X, NODE_TYPES as u, ATTR_FLAG_CASE_INSENSITIVE as z, ATTR_FLAG_CASE_SENSITIVE as tt, ATTR_OPERATOR_EQUAL as et, ATTR_OPERATOR_STAR_EQUAL as rt, ATTR_OPERATOR_DOLLAR_EQUAL as st, ATTR_OPERATOR_CARET_EQUAL as it, ATTR_OPERATOR_PIPE_EQUAL as lt, ATTR_OPERATOR_TILDE_EQUAL as nt } from "@projectwallace/css-parser";
2
- const _ = " ", p = "", k = ":", b = ";", w = '"', P = "(", N = ")", ut = "[", pt = "]", d = "{", g = "}", D = ",";
3
- function ct(R, { minify: O = !1, tab_size: L = void 0 } = /* @__PURE__ */ Object.create(null)) {
4
- if (L !== void 0 && Number(L) < 1)
1
+ import { parse as V, NODE_TYPES as u, ATTR_OPERATOR_NAMES as X, ATTR_FLAG_NAMES as z } from "@projectwallace/css-parser";
2
+ const _ = " ", p = "", y = ":", A = ";", d = '"', v = "(", g = ")", tt = "[", et = "]", M = "{", N = "}", U = ",";
3
+ function rt(L, { minify: O = !1, tab_size: m = void 0 } = /* @__PURE__ */ Object.create(null)) {
4
+ if (m !== void 0 && Number(m) < 1)
5
5
  throw new TypeError("tab_size must be a number greater than 0");
6
6
  const E = O ? p : `
7
- `, c = O ? p : _, M = O ? p : b;
8
- let A = [], Q = X(R, {
9
- on_comment: ({ start: t, end: e }) => {
10
- A.push(t, e);
7
+ `, c = O ? p : _, D = O ? p : A;
8
+ let S = [], B = V(L, {
9
+ parse_atrule_preludes: !1,
10
+ on_comment: O ? void 0 : ({ start: t, end: e }) => {
11
+ S.push(t, e);
11
12
  }
12
13
  }), a = 0;
13
14
  function o(t) {
14
- return O === !0 ? p : L !== void 0 ? _.repeat(L * t) : " ".repeat(t);
15
+ return O === !0 ? p : m !== void 0 ? _.repeat(m * t) : " ".repeat(t);
15
16
  }
16
17
  function f(t, e, s = a) {
17
18
  if (O || t === void 0 || e === void 0)
18
19
  return p;
19
20
  let r = p;
20
- for (let l = 0; l < A.length; l += 2) {
21
- let i = A[l];
22
- if (i === void 0 || i < t) continue;
23
- let n = A[l + 1];
21
+ for (let i = 0; i < S.length; i += 2) {
22
+ let l = S[i];
23
+ if (l === void 0 || l < t) continue;
24
+ let n = S[i + 1];
24
25
  if (n === void 0 || n > e) break;
25
- r.length > 0 && (r += E + o(s)), r += R.slice(i, n);
26
+ r.length > 0 && (r += E + o(s)), r += L.slice(l, n);
26
27
  }
27
28
  return r;
28
29
  }
29
- function I(t) {
30
+ function x(t) {
30
31
  return t.replace(/(?:^['"])|(?:['"]$)/g, p);
31
32
  }
32
- function S(t) {
33
- return t = (t == null ? void 0 : t.toString()) || "", w + I(t) + w;
33
+ function C(t) {
34
+ return t = (t == null ? void 0 : t.toString()) || "", d + x(t) + d;
34
35
  }
35
- function B(t) {
36
+ function H(t) {
36
37
  let e = [], s = t.text, r = s.charCodeAt(0);
37
38
  return r === 43 || r === 45 ? e.push(_) : r !== 44 && e.push(c), e.push(s), r === 43 || r === 45 ? e.push(_) : e.push(c), e.join(p);
38
39
  }
39
- function m(t) {
40
- var s, r, l;
40
+ function b(t) {
41
+ var s, r, i;
41
42
  let e = [];
42
- for (let i of t) {
43
- if (i.type === u.FUNCTION) {
44
- let n = (s = i.name) == null ? void 0 : s.toLowerCase();
45
- e.push(n, P), e.push(m(i.children)), e.push(N);
46
- } else if (i.type === u.DIMENSION)
47
- e.push(i.value, (r = i.unit) == null ? void 0 : r.toLowerCase());
48
- else if (i.type === u.STRING)
49
- e.push(S(i.text));
50
- else if (i.type === u.OPERATOR)
51
- e.push(B(i));
52
- else if (i.type === u.PARENTHESIS)
53
- e.push(P, m(i.children), N);
54
- else if (i.type === u.URL && typeof i.value == "string") {
43
+ for (let l of t) {
44
+ if (l.type === u.FUNCTION) {
45
+ let n = (s = l.name) == null ? void 0 : s.toLowerCase();
46
+ e.push(n, v), e.push(b(l.children)), e.push(g);
47
+ } else if (l.type === u.DIMENSION)
48
+ e.push(l.value, (r = l.unit) == null ? void 0 : r.toLowerCase());
49
+ else if (l.type === u.STRING)
50
+ e.push(C(l.text));
51
+ else if (l.type === u.OPERATOR)
52
+ e.push(H(l));
53
+ else if (l.type === u.PARENTHESIS)
54
+ e.push(v, b(l.children), g);
55
+ else if (l.type === u.URL && typeof l.value == "string") {
55
56
  e.push("url(");
56
- let { value: n } = i;
57
- /^['"]?data:/i.test(n) ? e.push(I(n)) : e.push(S(n)), e.push(N);
57
+ let { value: n } = l;
58
+ /^['"]?data:/i.test(n) ? e.push(x(n)) : e.push(C(n)), e.push(g);
58
59
  } else
59
- e.push(i.text);
60
- i.type !== u.OPERATOR && i.has_next && ((l = i.next_sibling) == null ? void 0 : l.type) !== u.OPERATOR && e.push(_);
60
+ e.push(l.text);
61
+ l.type !== u.OPERATOR && l.has_next && ((i = l.next_sibling) == null ? void 0 : i.type) !== u.OPERATOR && e.push(_);
61
62
  }
62
63
  return e.join(p);
63
64
  }
64
- function H(t) {
65
- return t === null ? p : m(t);
65
+ function Y(t) {
66
+ return t === null ? p : b(t);
66
67
  }
67
68
  function F(t) {
68
69
  let e = [];
69
70
  if (t.is_important) {
70
- let l = t.text, i = l.endsWith(b), n = l.lastIndexOf("!"), T = i ? -1 : void 0;
71
- e.push(c, l.slice(n, T).toLowerCase());
71
+ let i = t.text, l = i.endsWith(A), n = i.lastIndexOf("!"), T = l ? -1 : void 0;
72
+ e.push(c, i.slice(n, T).toLowerCase());
72
73
  }
73
- let s = H(t.value), r = t.property;
74
- return r === "font" && (s = s.replace(/\s*\/\s*/, "/")), s === p && O === !0 && (s += _), r.startsWith("--") || (r = r.toLowerCase()), r + k + c + s + e.join(p);
74
+ let s = Y(t.value), r = t.property;
75
+ return r === "font" && (s = s.replace(/\s*\/\s*/, "/")), s === p && O === !0 && (s += _), r.startsWith("--") || (r = r.toLowerCase()), r + y + c + s + e.join(p);
75
76
  }
76
- function G(t) {
77
- switch (t) {
78
- case nt:
79
- return "~=";
80
- case lt:
81
- return "|=";
82
- case it:
83
- return "^=";
84
- case st:
85
- return "$=";
86
- case rt:
87
- return "*=";
88
- case et:
89
- default:
90
- return "=";
91
- }
92
- }
93
- function v(t) {
77
+ function P(t) {
94
78
  let e = [], s = t.nth_a, r = t.nth_b;
95
79
  return s && e.push(s), s && r && e.push(c), r && (s && !r.startsWith("-") && e.push("+", c), e.push(parseFloat(r))), e.join(p);
96
80
  }
97
- function Y(t) {
81
+ function G(t) {
98
82
  var s, r;
99
83
  let e = [];
100
- return ((s = t.children[0]) == null ? void 0 : s.type) === u.NTH_SELECTOR && (e.push(v(t.children[0])), e.push(_, "of", _)), ((r = t.children[1]) == null ? void 0 : r.type) === u.SELECTOR_LIST && e.push(y(t.children[1])), e.join(p);
84
+ return ((s = t.children[0]) == null ? void 0 : s.type) === u.NTH_SELECTOR && (e.push(P(t.children[0])), e.push(_, "of", _)), ((r = t.children[1]) == null ? void 0 : r.type) === u.SELECTOR_LIST && e.push(k(t.children[1])), e.join(p);
101
85
  }
102
86
  function W(t, e = !1) {
103
87
  let s = t.name ?? "";
@@ -110,47 +94,47 @@ function ct(R, { minify: O = !1, tab_size: L = void 0 } = /* @__PURE__ */ Object
110
94
  }
111
95
  case u.PSEUDO_ELEMENT_SELECTOR:
112
96
  case u.PSEUDO_CLASS_SELECTOR: {
113
- let r = [k];
114
- return s = s.toLowerCase(), (s === "before" || s === "after" || t.type === u.PSEUDO_ELEMENT_SELECTOR) && r.push(k), r.push(s), t.has_children && (r.push(P), t.children.length > 0 && (s === "highlight" ? r.push(m(t.children)) : r.push(y(t))), r.push(N)), r.join(p);
97
+ let r = [y];
98
+ return s = s.toLowerCase(), (s === "before" || s === "after" || t.type === u.PSEUDO_ELEMENT_SELECTOR) && r.push(y), r.push(s), t.has_children && (r.push(v), t.children.length > 0 && (s === "highlight" ? r.push(b(t.children)) : r.push(k(t))), r.push(g)), r.join(p);
115
99
  }
116
100
  case u.ATTRIBUTE_SELECTOR: {
117
- let r = [ut, s.toLowerCase()];
118
- return t.attr_operator && (r.push(G(t.attr_operator)), typeof t.value == "string" && r.push(S(t.value)), t.attr_flags === z ? r.push(_, "i") : t.attr_flags === tt && r.push(_, "s")), r.push(pt), r.join(p);
101
+ let r = [tt, s.toLowerCase()];
102
+ return t.attr_operator && (r.push(X[t.attr_operator] ?? ""), typeof t.value == "string" && r.push(C(t.value)), t.attr_flags && r.push(_, z[t.attr_flags] ?? "")), r.push(et), r.join(p);
119
103
  }
120
104
  default:
121
105
  return t.text;
122
106
  }
123
107
  }
124
- function x(t) {
108
+ function I(t) {
125
109
  if (t.type === u.NTH_SELECTOR)
126
- return v(t);
110
+ return P(t);
127
111
  if (t.type === u.NTH_OF_SELECTOR)
128
- return Y(t);
112
+ return G(t);
129
113
  if (t.type === u.SELECTOR_LIST)
130
- return y(t);
114
+ return k(t);
131
115
  if (t.type === u.LANG_SELECTOR)
132
- return S(t.text);
116
+ return C(t.text);
133
117
  let e = [];
134
118
  return t.children.forEach((s, r) => {
135
- const l = W(s, r === 0);
136
- e.push(l);
119
+ const i = W(s, r === 0);
120
+ e.push(i);
137
121
  }), e.join(p);
138
122
  }
139
- function y(t) {
123
+ function k(t) {
140
124
  let e = [];
141
125
  for (let s of t)
142
- e.push(x(s)), s.has_next && e.push(D, c);
126
+ e.push(I(s)), s.has_next && e.push(U, c);
143
127
  return e.join(p);
144
128
  }
145
129
  function K(t) {
146
130
  let e = [], s;
147
131
  for (let r of t) {
148
132
  if (s !== void 0) {
149
- let i = f(s, r.start);
150
- i && e.push(o(a) + i);
133
+ let l = f(s, r.start);
134
+ l && e.push(o(a) + l);
151
135
  }
152
- let l = x(r);
153
- r.has_next && (l += D), e.push(o(a) + l), s = r.end;
136
+ let i = I(r);
137
+ r.has_next && (i += U), e.push(o(a) + i), s = r.end;
154
138
  }
155
139
  return e.join(E);
156
140
  }
@@ -162,46 +146,46 @@ function ct(R, { minify: O = !1, tab_size: L = void 0 } = /* @__PURE__ */ Object
162
146
  if (s.length === 0) {
163
147
  let h = f(t.start, t.end);
164
148
  if (h)
165
- return e.push(o(a) + h), a--, e.push(o(a) + g), e.join(E);
149
+ return e.push(o(a) + h), a--, e.push(o(a) + N), e.join(E);
166
150
  }
167
- let r = s[0], l = f(t.start, r == null ? void 0 : r.start);
168
- l && e.push(o(a) + l);
169
- let i;
151
+ let r = s[0], i = f(t.start, r == null ? void 0 : r.start);
152
+ i && e.push(o(a) + i);
153
+ let l;
170
154
  for (let h of s) {
171
- if (i !== void 0) {
172
- let C = f(i, h.start);
173
- C && e.push(o(a) + C);
155
+ if (l !== void 0) {
156
+ let R = f(l, h.start);
157
+ R && e.push(o(a) + R);
174
158
  }
175
159
  let Z = ((T = h.next_sibling) == null ? void 0 : T.type) !== u.DECLARATION;
176
160
  if (h.type === u.DECLARATION) {
177
- let C = F(h), J = Z ? M : b;
178
- e.push(o(a) + C + J);
179
- } else h.type === u.STYLE_RULE ? (i !== void 0 && e.length !== 0 && e.push(p), e.push(U(h))) : h.type === u.AT_RULE && (i !== void 0 && e.length !== 0 && e.push(p), e.push(o(a) + $(h)));
180
- i = h.end;
161
+ let R = F(h), J = Z ? D : A;
162
+ e.push(o(a) + R + J);
163
+ } else h.type === u.STYLE_RULE ? (l !== void 0 && e.length !== 0 && e.push(p), e.push($(h))) : h.type === u.AT_RULE && (l !== void 0 && e.length !== 0 && e.push(p), e.push(o(a) + w(h)));
164
+ l = h.end;
181
165
  }
182
- let n = f(i, t.end);
183
- return n && e.push(o(a) + n), a--, e.push(o(a) + g), e.join(E);
166
+ let n = f(l, t.end);
167
+ return n && e.push(o(a) + n), a--, e.push(o(a) + N), e.join(E);
184
168
  }
185
- function U(t) {
169
+ function $(t) {
186
170
  var s, r;
187
171
  let e = [];
188
172
  if (((s = t.first_child) == null ? void 0 : s.type) === u.SELECTOR_LIST) {
189
- let l = K(t.first_child), i = f(t.first_child.end, (r = t.block) == null ? void 0 : r.start);
190
- i && (l += E + o(a) + i), l += c + d, t.block && (t.block.has_children || f(t.block.start, t.block.end)) || (l += g), e.push(l);
173
+ let i = K(t.first_child), l = f(t.first_child.end, (r = t.block) == null ? void 0 : r.start);
174
+ l && (i += E + o(a) + l), i += c + M, t.block && (t.block.has_children || f(t.block.start, t.block.end)) || (i += N), e.push(i);
191
175
  }
192
176
  return t.block && (t.block.has_children || f(t.block.start, t.block.end)) && e.push(j(t.block)), e.join(E);
193
177
  }
194
- function V(t) {
195
- return t.replace(/\s*([:,])/g, t.toLowerCase().includes("selector(") ? "$1" : "$1 ").replace(/\)([a-zA-Z])/g, ") $1").replace(/\s*(=>|<=)\s*/g, " $1 ").replace(/([^<>=\s])([<>])([^<>=\s])/g, `$1${c}$2${c}$3`).replace(/\s+/g, c).replace(/calc\(\s*([^()+\-*/]+)\s*([*/+-])\s*([^()+\-*/]+)\s*\)/g, (e, s, r, l) => {
196
- let i = r === "+" || r === "-" ? _ : c;
197
- return `calc(${s.trim()}${i}${r}${i}${l.trim()})`;
178
+ function q(t) {
179
+ return t.replace(/\s*([:,])/g, t.toLowerCase().includes("selector(") ? "$1" : "$1 ").replace(/\)([a-zA-Z])/g, ") $1").replace(/\s*(=>|<=)\s*/g, " $1 ").replace(/([^<>=\s])([<>])([^<>=\s])/g, `$1${c}$2${c}$3`).replace(/\s+/g, c).replace(/calc\(\s*([^()+\-*/]+)\s*([*/+-])\s*([^()+\-*/]+)\s*\)/g, (e, s, r, i) => {
180
+ let l = r === "+" || r === "-" ? _ : c;
181
+ return `calc(${s.trim()}${l}${r}${l}${i.trim()})`;
198
182
  }).replace(/selector|url|supports|layer\(/gi, (e) => e.toLowerCase());
199
183
  }
200
- function $(t) {
184
+ function w(t) {
201
185
  let e = [], s = ["@", t.name.toLowerCase()];
202
- return t.prelude && s.push(_, V(t.prelude.text)), t.block === null ? s.push(b) : (s.push(c, d), !t.block.is_empty || f(t.block.start, t.block.end) || s.push(g)), e.push(s.join(p)), t.block !== null && (!t.block.is_empty || f(t.block.start, t.block.end)) && e.push(j(t.block)), e.join(E);
186
+ return t.prelude && s.push(_, q(t.prelude.text)), t.block === null ? s.push(A) : (s.push(c, M), !t.block.is_empty || f(t.block.start, t.block.end) || s.push(N)), e.push(s.join(p)), t.block !== null && (!t.block.is_empty || f(t.block.start, t.block.end)) && e.push(j(t.block)), e.join(E);
203
187
  }
204
- function q(t) {
188
+ function Q(t) {
205
189
  let e = [], s = t.children;
206
190
  if (s.length === 0)
207
191
  return f(0, t.end, 0);
@@ -210,23 +194,23 @@ function ct(R, { minify: O = !1, tab_size: L = void 0 } = /* @__PURE__ */ Object
210
194
  let n = f(0, r.start, 0);
211
195
  n && e.push(n);
212
196
  }
213
- let l;
197
+ let i;
214
198
  for (let n of t) {
215
- if (l !== void 0) {
216
- let T = f(l, n.start, 0);
199
+ if (i !== void 0) {
200
+ let T = f(i, n.start, 0);
217
201
  T && e.push(T);
218
202
  }
219
- n.type === u.STYLE_RULE ? e.push(U(n)) : n.type === u.AT_RULE && e.push($(n)), l = n.end, n.has_next && (n.next_sibling && f(n.end, n.next_sibling.start, 0) || e.push(p));
203
+ n.type === u.STYLE_RULE ? e.push($(n)) : n.type === u.AT_RULE && e.push(w(n)), i = n.end, n.has_next && (n.next_sibling && f(n.end, n.next_sibling.start, 0) || e.push(p));
220
204
  }
221
- let i = f(l, t.end, 0);
222
- return i && e.push(i), e.join(E);
205
+ let l = f(i, t.end, 0);
206
+ return l && e.push(l), e.join(E);
223
207
  }
224
- return q(Q).trimEnd();
208
+ return Q(B).trimEnd();
225
209
  }
226
- function ft(R) {
227
- return ct(R, { minify: !0 });
210
+ function lt(L) {
211
+ return rt(L, { minify: !0 });
228
212
  }
229
213
  export {
230
- ct as format,
231
- ft as minify
214
+ rt as format,
215
+ lt as minify
232
216
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@projectwallace/format-css",
3
- "version": "2.2.3",
3
+ "version": "2.2.5",
4
4
  "description": "Fast, small, zero-config library to format or minify CSS with basic rules.",
5
5
  "repository": {
6
6
  "type": "git",
@@ -60,6 +60,6 @@
60
60
  "singleQuote": true
61
61
  },
62
62
  "dependencies": {
63
- "@projectwallace/css-parser": "^0.13.0"
63
+ "@projectwallace/css-parser": "^0.13.5"
64
64
  }
65
65
  }