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

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.
@@ -1,4 +1,5 @@
1
1
  import { getAngle, COLORS_NAMES, rgb2Hex, hsl2Hex, hwb2hex, cmyk2hex, NAMES_COLORS } from './utils/color.js';
2
+ import { expand } from '../ast/expand.js';
2
3
 
3
4
  const colorsFunc = ['rgb', 'rgba', 'hsl', 'hsla', 'hwb', 'device-cmyk'];
4
5
  function reduceNumber(val) {
@@ -20,6 +21,7 @@ function reduceNumber(val) {
20
21
  }
21
22
  function render(data, opt = {}) {
22
23
  const startTime = performance.now();
24
+ const errors = [];
23
25
  const options = Object.assign(opt.minify ?? true ? {
24
26
  indent: '',
25
27
  newLine: '',
@@ -29,23 +31,23 @@ function render(data, opt = {}) {
29
31
  newLine: '\n',
30
32
  compress: false,
31
33
  removeComments: false,
32
- }, { colorConvert: true, preserveLicense: false }, opt);
34
+ }, { colorConvert: true, expandNestingRules: false, preserveLicense: false }, opt);
33
35
  return {
34
- code: doRender(data, options, function reducer(acc, curr) {
36
+ code: doRender(options.expandNestingRules ? expand(data) : data, options, errors, function reducer(acc, curr) {
35
37
  if (curr.typ == 'Comment' && options.removeComments) {
36
38
  if (!options.preserveLicense || !curr.val.startsWith('/*!')) {
37
39
  return acc;
38
40
  }
39
41
  return acc + curr.val;
40
42
  }
41
- return acc + renderToken(curr, options, reducer);
42
- }, 0), stats: {
43
+ return acc + renderToken(curr, options, reducer, errors);
44
+ }, 0), errors, stats: {
43
45
  total: `${(performance.now() - startTime).toFixed(2)}ms`
44
46
  }
45
47
  };
46
48
  }
47
49
  // @ts-ignore
48
- function doRender(data, options, reducer, level = 0, indents = []) {
50
+ function doRender(data, options, errors, reducer, level = 0, indents = []) {
49
51
  if (indents.length < level + 1) {
50
52
  indents.push(options.indent.repeat(level));
51
53
  }
@@ -58,10 +60,11 @@ function doRender(data, options, reducer, level = 0, indents = []) {
58
60
  case 'Declaration':
59
61
  return `${data.nam}:${options.indent}${data.val.reduce(reducer, '')}`;
60
62
  case 'Comment':
63
+ case 'CDOCOMM':
61
64
  return !options.removeComments || (options.preserveLicense && data.val.startsWith('/*!')) ? data.val : '';
62
65
  case 'StyleSheet':
63
66
  return data.chi.reduce((css, node) => {
64
- const str = doRender(node, options, reducer, level, indents);
67
+ const str = doRender(node, options, errors, reducer, level, indents);
65
68
  if (str === '') {
66
69
  return css;
67
70
  }
@@ -83,7 +86,8 @@ function doRender(data, options, reducer, level = 0, indents = []) {
83
86
  }
84
87
  else if (node.typ == 'Declaration') {
85
88
  if (node.val.length == 0) {
86
- console.error(`invalid declaration`, node);
89
+ // @ts-ignore
90
+ errors.push({ action: 'ignore', message: `render: invalid declaration ${JSON.stringify(node)}`, location: node.loc });
87
91
  return '';
88
92
  }
89
93
  str = `${node.nam}:${options.indent}${node.val.reduce(reducer, '').trimEnd()};`;
@@ -92,7 +96,7 @@ function doRender(data, options, reducer, level = 0, indents = []) {
92
96
  str = `${data.val === '' ? '' : options.indent || ' '}${data.val};`;
93
97
  }
94
98
  else {
95
- str = doRender(node, options, reducer, level + 1, indents);
99
+ str = doRender(node, options, errors, reducer, level + 1, indents);
96
100
  }
97
101
  if (css === '') {
98
102
  return str;
@@ -112,7 +116,7 @@ function doRender(data, options, reducer, level = 0, indents = []) {
112
116
  }
113
117
  return '';
114
118
  }
115
- function renderToken(token, options = {}, reducer) {
119
+ function renderToken(token, options = {}, reducer, errors) {
116
120
  if (reducer == null) {
117
121
  reducer = function (acc, curr) {
118
122
  if (curr.typ == 'Comment' && options.removeComments) {
@@ -121,7 +125,7 @@ function renderToken(token, options = {}, reducer) {
121
125
  }
122
126
  return acc + curr.val;
123
127
  }
124
- return acc + renderToken(curr, options, reducer);
128
+ return acc + renderToken(curr, options, reducer, errors);
125
129
  };
126
130
  }
127
131
  switch (token.typ) {
@@ -272,20 +276,10 @@ function renderToken(token, options = {}, reducer) {
272
276
  }
273
277
  return val + unit;
274
278
  case 'Perc':
275
- return token.val + '%';
279
+ const perc = reduceNumber(token.val);
280
+ return options.minify && perc == '0' ? '0' : perc + '%';
276
281
  case 'Number':
277
- const num = (+token.val).toString();
278
- if (token.val.length < num.length) {
279
- return token.val;
280
- }
281
- if (num.charAt(0) === '0' && num.length > 1) {
282
- return num.slice(1);
283
- }
284
- const slice = num.slice(0, 2);
285
- if (slice == '-0') {
286
- return '-' + num.slice(2);
287
- }
288
- return num;
282
+ return reduceNumber(token.val);
289
283
  case 'Comment':
290
284
  if (options.removeComments && (!options.preserveLicense || !token.val.startsWith('/*!'))) {
291
285
  return '';
@@ -300,8 +294,7 @@ function renderToken(token, options = {}, reducer) {
300
294
  case 'Delim':
301
295
  return /* options.minify && 'Pseudo-class' == token.typ && '::' == token.val.slice(0, 2) ? token.val.slice(1) : */ token.val;
302
296
  }
303
- console.error(`unexpected token ${JSON.stringify(token, null, 1)}`);
304
- // throw new Error(`unexpected token ${JSON.stringify(token, null, 1)}`);
297
+ errors?.push({ action: 'ignore', message: `render: unexpected token ${JSON.stringify(token, null, 1)}` });
305
298
  return '';
306
299
  }
307
300
 
@@ -8,7 +8,10 @@ async function transform(css, options = {}) {
8
8
  return parse(css, options).then((parseResult) => {
9
9
  const rendered = render(parseResult.ast, options);
10
10
  return {
11
- ...parseResult, ...rendered, stats: {
11
+ ...parseResult,
12
+ ...rendered,
13
+ errors: parseResult.errors.concat(rendered.errors),
14
+ stats: {
12
15
  bytesOut: rendered.code.length,
13
16
  ...parseResult.stats,
14
17
  render: rendered.stats.total,
@@ -1,13 +1,14 @@
1
+ export { combinators, hasDeclaration, minify, minifyRule, reduceSelector, splitRule } from '../lib/ast/minify.js';
2
+ export { walk, walkValues } from '../lib/ast/walk.js';
3
+ export { expand, replaceCompound } from '../lib/ast/expand.js';
4
+ export { colorsFunc, render, renderToken } from '../lib/renderer/render.js';
1
5
  import { parse as parse$1 } from '../lib/parser/parse.js';
2
6
  export { parseString, urlTokenMatcher } from '../lib/parser/parse.js';
3
7
  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';
8
+ export { isAngle, isAtKeyword, isColor, isDigit, isDimension, isFrequency, isFunction, isHash, isHexColor, isIdent, isIdentCodepoint, isIdentStart, isLength, isNewLine, isNonPrintable, isNumber, isPercentage, isPseudo, isResolution, isTime, isWhiteSpace, parseDimension } from '../lib/parser/utils/syntax.js';
5
9
  export { getConfig } from '../lib/parser/utils/config.js';
6
10
  export { funcList, matchType } from '../lib/parser/utils/type.js';
7
- export { colorsFunc, render, renderToken } from '../lib/renderer/render.js';
8
11
  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';
11
12
  import { load } from './load.js';
12
13
  import { resolve } from '../lib/fs/resolve.js';
13
14
  export { dirname, matchUrl } from '../lib/fs/resolve.js';
package/dist/web/index.js CHANGED
@@ -1,13 +1,14 @@
1
+ export { combinators, hasDeclaration, minify, minifyRule, reduceSelector, splitRule } from '../lib/ast/minify.js';
2
+ export { walk, walkValues } from '../lib/ast/walk.js';
3
+ export { expand, replaceCompound } from '../lib/ast/expand.js';
4
+ export { colorsFunc, render, renderToken } from '../lib/renderer/render.js';
1
5
  import { parse as parse$1 } from '../lib/parser/parse.js';
2
6
  export { parseString, urlTokenMatcher } from '../lib/parser/parse.js';
3
7
  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';
8
+ export { isAngle, isAtKeyword, isColor, isDigit, isDimension, isFrequency, isFunction, isHash, isHexColor, isIdent, isIdentCodepoint, isIdentStart, isLength, isNewLine, isNonPrintable, isNumber, isPercentage, isPseudo, isResolution, isTime, isWhiteSpace, parseDimension } from '../lib/parser/utils/syntax.js';
5
9
  export { getConfig } from '../lib/parser/utils/config.js';
6
10
  export { funcList, matchType } from '../lib/parser/utils/type.js';
7
- export { colorsFunc, render, renderToken } from '../lib/renderer/render.js';
8
11
  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';
11
12
  import { load } from './load.js';
12
13
  import { resolve, dirname } from '../lib/fs/resolve.js';
13
14
  export { matchUrl } from '../lib/fs/resolve.js';
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@tbela99/css-parser",
3
3
  "description": "CSS parser for node and the browser",
4
- "version": "0.0.1-rc5",
4
+ "version": "0.0.1-rc7",
5
5
  "exports": {
6
6
  ".": "./dist/node/index.js",
7
7
  "./umd": "./dist/index-umd-web.js",