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