@tbela99/css-parser 0.0.1-rc3 → 0.0.1-rc5

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.
@@ -13,4 +13,4 @@ function matchType(val, properties) {
13
13
  return false;
14
14
  }
15
15
 
16
- export { matchType };
16
+ export { funcList, matchType };
@@ -1,5 +1,23 @@
1
- import { COLORS_NAMES, rgb2Hex, hsl2Hex, hwb2hex, cmyk2hex, NAMES_COLORS } from './utils/color.js';
1
+ import { getAngle, COLORS_NAMES, rgb2Hex, hsl2Hex, hwb2hex, cmyk2hex, NAMES_COLORS } from './utils/color.js';
2
2
 
3
+ const colorsFunc = ['rgb', 'rgba', 'hsl', 'hsla', 'hwb', 'device-cmyk'];
4
+ function reduceNumber(val) {
5
+ val = (+val).toString();
6
+ if (val === '0') {
7
+ return '0';
8
+ }
9
+ const chr = val.charAt(0);
10
+ if (chr == '-') {
11
+ const slice = val.slice(0, 2);
12
+ if (slice == '-0') {
13
+ return val.length == 2 ? '0' : '-' + val.slice(2);
14
+ }
15
+ }
16
+ if (chr == '0') {
17
+ return val.slice(1);
18
+ }
19
+ return val;
20
+ }
3
21
  function render(data, opt = {}) {
4
22
  const startTime = performance.now();
5
23
  const options = Object.assign(opt.minify ?? true ? {
@@ -12,17 +30,19 @@ function render(data, opt = {}) {
12
30
  compress: false,
13
31
  removeComments: false,
14
32
  }, { colorConvert: true, preserveLicense: false }, opt);
15
- function reducer(acc, curr, index, original) {
16
- if (curr.typ == 'Comment' && options.removeComments) {
17
- if (!options.preserveLicense || !curr.val.startsWith('/*!')) {
18
- return acc;
33
+ return {
34
+ code: doRender(data, options, function reducer(acc, curr) {
35
+ if (curr.typ == 'Comment' && options.removeComments) {
36
+ if (!options.preserveLicense || !curr.val.startsWith('/*!')) {
37
+ return acc;
38
+ }
39
+ return acc + curr.val;
19
40
  }
20
- }
21
- return acc + renderToken(curr, options);
22
- }
23
- return { code: doRender(data, options, reducer, 0), stats: {
41
+ return acc + renderToken(curr, options, reducer);
42
+ }, 0), stats: {
24
43
  total: `${(performance.now() - startTime).toFixed(2)}ms`
25
- } };
44
+ }
45
+ };
26
46
  }
27
47
  // @ts-ignore
28
48
  function doRender(data, options, reducer, level = 0, indents = []) {
@@ -36,9 +56,9 @@ function doRender(data, options, reducer, level = 0, indents = []) {
36
56
  const indentSub = indents[level + 1];
37
57
  switch (data.typ) {
38
58
  case 'Declaration':
39
- return `${data.nam}:${options.indent}${data.val.reduce((acc, curr) => acc + renderToken(curr), '')}`;
59
+ return `${data.nam}:${options.indent}${data.val.reduce(reducer, '')}`;
40
60
  case 'Comment':
41
- return options.removeComments ? '' : data.val;
61
+ return !options.removeComments || (options.preserveLicense && data.val.startsWith('/*!')) ? data.val : '';
42
62
  case 'StyleSheet':
43
63
  return data.chi.reduce((css, node) => {
44
64
  const str = doRender(node, options, reducer, level, indents);
@@ -59,7 +79,7 @@ function doRender(data, options, reducer, level = 0, indents = []) {
59
79
  let children = data.chi.reduce((css, node) => {
60
80
  let str;
61
81
  if (node.typ == 'Comment') {
62
- str = options.removeComments ? '' : node.val;
82
+ str = options.removeComments && (!options.preserveLicense || !node.val.startsWith('/*!')) ? '' : node.val;
63
83
  }
64
84
  else if (node.typ == 'Declaration') {
65
85
  if (node.val.length == 0) {
@@ -92,7 +112,18 @@ function doRender(data, options, reducer, level = 0, indents = []) {
92
112
  }
93
113
  return '';
94
114
  }
95
- function renderToken(token, options = {}) {
115
+ function renderToken(token, options = {}, reducer) {
116
+ if (reducer == null) {
117
+ reducer = function (acc, curr) {
118
+ if (curr.typ == 'Comment' && options.removeComments) {
119
+ if (!options.preserveLicense || !curr.val.startsWith('/*!')) {
120
+ return acc;
121
+ }
122
+ return acc + curr.val;
123
+ }
124
+ return acc + renderToken(curr, options, reducer);
125
+ };
126
+ }
96
127
  switch (token.typ) {
97
128
  case 'Color':
98
129
  if (options.minify || options.colorConvert) {
@@ -143,22 +174,19 @@ function renderToken(token, options = {}) {
143
174
  case 'UrlFunc':
144
175
  case 'Pseudo-class-func':
145
176
  // @ts-ignore
146
- return ( /* options.minify && 'Pseudo-class-func' == token.typ && token.val.slice(0, 2) == '::' ? token.val.slice(1) :*/token.val ?? '') + '(' + token.chi.reduce((acc, curr) => {
147
- if (options.removeComments && curr.typ == 'Comment') {
148
- if (!options.preserveLicense || !curr.val.startsWith('/*!')) {
149
- return acc;
150
- }
151
- }
152
- return acc + renderToken(curr, options);
153
- }, '') + ')';
177
+ return ( /* options.minify && 'Pseudo-class-func' == token.typ && token.val.slice(0, 2) == '::' ? token.val.slice(1) :*/token.val ?? '') + '(' + token.chi.reduce(reducer, '') + ')';
154
178
  case 'Includes':
155
179
  return '~=';
156
180
  case 'Dash-match':
157
181
  return '|=';
158
182
  case 'Lt':
159
183
  return '<';
184
+ case 'Lte':
185
+ return '<=';
160
186
  case 'Gt':
161
187
  return '>';
188
+ case 'Gte':
189
+ return '>=';
162
190
  case 'End-parens':
163
191
  return ')';
164
192
  case 'Attr-start':
@@ -176,13 +204,59 @@ function renderToken(token, options = {}) {
176
204
  case 'Important':
177
205
  return '!important';
178
206
  case 'Attr':
179
- return '[' + token.chi.reduce((acc, curr) => acc + renderToken(curr, options), '') + ']';
207
+ return '[' + token.chi.reduce(reducer, '') + ']';
180
208
  case 'Time':
181
- case 'Frequency':
182
209
  case 'Angle':
183
210
  case 'Length':
184
211
  case 'Dimension':
185
- const val = (+token.val).toString();
212
+ case 'Frequency':
213
+ case 'Resolution':
214
+ let val = reduceNumber(token.val);
215
+ let unit = token.unit;
216
+ if (token.typ == 'Angle') {
217
+ const angle = getAngle(token);
218
+ let v;
219
+ let value = val + unit;
220
+ for (const u of ['turn', 'deg', 'rad', 'grad']) {
221
+ if (token.unit == u) {
222
+ continue;
223
+ }
224
+ switch (u) {
225
+ case 'turn':
226
+ v = reduceNumber(angle);
227
+ if (v.length + 4 < value.length) {
228
+ val = v;
229
+ unit = u;
230
+ value = v + u;
231
+ }
232
+ break;
233
+ case 'deg':
234
+ v = reduceNumber(angle * 360);
235
+ if (v.length + 3 < value.length) {
236
+ val = v;
237
+ unit = u;
238
+ value = v + u;
239
+ }
240
+ break;
241
+ case 'rad':
242
+ v = reduceNumber(angle * (2 * Math.PI));
243
+ if (v.length + 3 < value.length) {
244
+ val = v;
245
+ unit = u;
246
+ value = v + u;
247
+ }
248
+ break;
249
+ case 'grad':
250
+ v = reduceNumber(angle * 400);
251
+ if (v.length + 4 < value.length) {
252
+ val = v;
253
+ unit = u;
254
+ value = v + u;
255
+ }
256
+ break;
257
+ }
258
+ }
259
+ }
186
260
  if (val === '0') {
187
261
  if (token.typ == 'Time') {
188
262
  return '0s';
@@ -196,17 +270,7 @@ function renderToken(token, options = {}) {
196
270
  }
197
271
  return '0';
198
272
  }
199
- const chr = val.charAt(0);
200
- if (chr == '-') {
201
- const slice = val.slice(0, 2);
202
- if (slice == '-0') {
203
- return (val.length == 2 ? '0' : '-' + val.slice(2)) + token.unit;
204
- }
205
- }
206
- else if (chr == '0') {
207
- return val.slice(1) + token.unit;
208
- }
209
- return val + token.unit;
273
+ return val + unit;
210
274
  case 'Perc':
211
275
  return token.val + '%';
212
276
  case 'Number':
@@ -223,7 +287,7 @@ function renderToken(token, options = {}) {
223
287
  }
224
288
  return num;
225
289
  case 'Comment':
226
- if (options.removeComments) {
290
+ if (options.removeComments && (!options.preserveLicense || !token.val.startsWith('/*!'))) {
227
291
  return '';
228
292
  }
229
293
  case 'Url-token':
@@ -241,4 +305,4 @@ function renderToken(token, options = {}) {
241
305
  return '';
242
306
  }
243
307
 
244
- export { render, renderToken };
308
+ export { colorsFunc, render, renderToken };
@@ -418,7 +418,7 @@ function cmyk2hex(token) {
418
418
  return `#${rgb.reduce((acc, curr) => acc + curr.toString(16).padStart(2, '0'), '')}`;
419
419
  }
420
420
  function getAngle(token) {
421
- if (token.typ == 'Dimension') {
421
+ if (token.typ == 'Angle') {
422
422
  switch (token.unit) {
423
423
  case 'deg':
424
424
  // @ts-ignore
@@ -491,4 +491,4 @@ function hsl2rgb(h, s, l, a = null) {
491
491
  return values;
492
492
  }
493
493
 
494
- export { COLORS_NAMES, NAMES_COLORS, cmyk2hex, hsl2Hex, hwb2hex, rgb2Hex };
494
+ export { COLORS_NAMES, NAMES_COLORS, cmyk2hex, getAngle, hsl2Hex, hwb2hex, rgb2Hex };
@@ -1,5 +1,6 @@
1
1
  import { parse } from './parser/parse.js';
2
2
  import { render } from './renderer/render.js';
3
+ import './renderer/utils/color.js';
3
4
 
4
5
  async function transform(css, options = {}) {
5
6
  options = { minify: true, removeEmpty: true, ...options };
@@ -1,15 +1,21 @@
1
1
  import { parse as parse$1 } from '../lib/parser/parse.js';
2
2
  export { parseString, urlTokenMatcher } from '../lib/parser/parse.js';
3
- import '../lib/renderer/utils/color.js';
3
+ export { tokenize } from '../lib/parser/tokenize.js';
4
+ export { isAngle, isAtKeyword, isColor, isDigit, isDimension, isFrequency, isFunction, isHash, isHexColor, isHexDigit, isIdent, isIdentCodepoint, isIdentStart, isLength, isNewLine, isNumber, isPercentage, isPseudo, isResolution, isTime, isWhiteSpace, parseDimension } from '../lib/parser/utils/syntax.js';
5
+ export { getConfig } from '../lib/parser/utils/config.js';
6
+ export { funcList, matchType } from '../lib/parser/utils/type.js';
7
+ export { colorsFunc, render, renderToken } from '../lib/renderer/render.js';
4
8
  import { transform as transform$1 } from '../lib/transform.js';
9
+ export { combinators, hasDeclaration, minify, minifyRule, reduceSelector } from '../lib/ast/minify.js';
10
+ export { walk } from '../lib/ast/walk.js';
5
11
  import { load } from './load.js';
6
12
  import { resolve } from '../lib/fs/resolve.js';
7
13
  export { dirname, matchUrl } from '../lib/fs/resolve.js';
8
14
 
9
- function parse(iterator, opt = {}) {
15
+ async function parse(iterator, opt = {}) {
10
16
  return parse$1(iterator, Object.assign(opt, { load, resolve, cwd: opt.cwd ?? process.cwd() }));
11
17
  }
12
- function transform(css, options = {}) {
18
+ async function transform(css, options = {}) {
13
19
  return transform$1(css, Object.assign(options, { load, resolve, cwd: options.cwd ?? process.cwd() }));
14
20
  }
15
21
 
package/dist/web/index.js CHANGED
@@ -1,10 +1,10 @@
1
1
  import { parse as parse$1 } from '../lib/parser/parse.js';
2
2
  export { parseString, urlTokenMatcher } from '../lib/parser/parse.js';
3
3
  export { tokenize } from '../lib/parser/tokenize.js';
4
- export { isAngle, isAtKeyword, isDigit, isDimension, isFrequency, isFunction, isHash, isHexColor, isHexDigit, isIdent, isIdentCodepoint, isIdentStart, isLength, isNewLine, isNumber, isPercentage, isPseudo, isResolution, isTime, isWhiteSpace, parseDimension } from '../lib/parser/utils/syntax.js';
4
+ export { isAngle, isAtKeyword, isColor, isDigit, isDimension, isFrequency, isFunction, isHash, isHexColor, isHexDigit, isIdent, isIdentCodepoint, isIdentStart, isLength, isNewLine, isNumber, isPercentage, isPseudo, isResolution, isTime, isWhiteSpace, parseDimension } from '../lib/parser/utils/syntax.js';
5
5
  export { getConfig } from '../lib/parser/utils/config.js';
6
- export { matchType } from '../lib/parser/utils/type.js';
7
- export { render, renderToken } from '../lib/renderer/render.js';
6
+ export { funcList, matchType } from '../lib/parser/utils/type.js';
7
+ export { colorsFunc, render, renderToken } from '../lib/renderer/render.js';
8
8
  import { transform as transform$1 } from '../lib/transform.js';
9
9
  export { combinators, hasDeclaration, minify, minifyRule, reduceSelector } from '../lib/ast/minify.js';
10
10
  export { walk } from '../lib/ast/walk.js';
@@ -12,14 +12,14 @@ import { load } from './load.js';
12
12
  import { resolve, dirname } from '../lib/fs/resolve.js';
13
13
  export { matchUrl } from '../lib/fs/resolve.js';
14
14
 
15
- function parse(iterator, opt = {}) {
15
+ async function parse(iterator, opt = {}) {
16
16
  return parse$1(iterator, Object.assign(opt, {
17
17
  load,
18
18
  resolve,
19
19
  cwd: opt.cwd ?? self.location.pathname.endsWith('/') ? self.location.pathname : dirname(self.location.pathname)
20
20
  }));
21
21
  }
22
- function transform(css, options = {}) {
22
+ async function transform(css, options = {}) {
23
23
  return transform$1(css, Object.assign(options, {
24
24
  load,
25
25
  resolve,
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "@tbela99/css-parser",
3
3
  "description": "CSS parser for node and the browser",
4
- "version": "0.0.1-rc3",
4
+ "version": "0.0.1-rc5",
5
5
  "exports": {
6
- ".": "./dist/index.js",
6
+ ".": "./dist/node/index.js",
7
7
  "./umd": "./dist/index-umd-web.js",
8
8
  "./web": "./dist/web/index.js",
9
9
  "./cjs": "./dist/index.cjs"
@@ -24,14 +24,16 @@
24
24
  "keywords": [
25
25
  "parser",
26
26
  "css",
27
- "css parser",
28
27
  "css-parser",
29
28
  "node",
30
29
  "ast",
30
+ "nesting",
31
+ "nested",
32
+ "compiler",
31
33
  "browser",
32
- "css nesting",
33
- "css compiler",
34
- "nested css"
34
+ "css-nesting",
35
+ "css-compiler",
36
+ "nested-css"
35
37
  ],
36
38
  "author": "Thierry Bela",
37
39
  "license": "MIT OR LGPL-3.0",
@@ -44,7 +46,6 @@
44
46
  "@rollup/plugin-commonjs": "^25.0.4",
45
47
  "@rollup/plugin-json": "^6.0.0",
46
48
  "@rollup/plugin-node-resolve": "^15.1.0",
47
- "@rollup/plugin-terser": "^0.4.3",
48
49
  "@rollup/plugin-typescript": "^11.1.2",
49
50
  "@types/chai": "^4.3.5",
50
51
  "@types/mocha": "^10.0.1",
package/dist/index.js DELETED
@@ -1,11 +0,0 @@
1
- export { parse, transform } from './node/index.js';
2
- export { parseString, urlTokenMatcher } from './lib/parser/parse.js';
3
- export { tokenize } from './lib/parser/tokenize.js';
4
- export { isAngle, isAtKeyword, isDigit, isDimension, isFrequency, isFunction, isHash, isHexColor, isHexDigit, isIdent, isIdentCodepoint, isIdentStart, isLength, isNewLine, isNumber, isPercentage, isPseudo, isResolution, isTime, isWhiteSpace, parseDimension } from './lib/parser/utils/syntax.js';
5
- export { getConfig } from './lib/parser/utils/config.js';
6
- export { matchType } from './lib/parser/utils/type.js';
7
- export { render, renderToken } from './lib/renderer/render.js';
8
- export { combinators, hasDeclaration, minify, minifyRule, reduceSelector } from './lib/ast/minify.js';
9
- export { walk } from './lib/ast/walk.js';
10
- export { load } from './node/load.js';
11
- export { dirname, matchUrl, resolve } from './lib/fs/resolve.js';