@tbela99/css-parser 0.0.1-rc4 → 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.
package/dist/index.d.ts CHANGED
@@ -119,7 +119,7 @@ interface CDOCommentToken {
119
119
  val: string;
120
120
  }
121
121
  interface BadCDOCommentToken {
122
- typ: 'BADCDO';
122
+ typ: 'Bad-cdo';
123
123
  val: string;
124
124
  }
125
125
  interface IncludesToken {
@@ -642,6 +642,7 @@ declare function isResolution(dimension: DimensionToken): boolean;
642
642
  declare function isAngle(dimension: DimensionToken): boolean;
643
643
  declare function isTime(dimension: DimensionToken): boolean;
644
644
  declare function isFrequency(dimension: DimensionToken): boolean;
645
+ declare function isColor(token: Token): boolean;
645
646
  declare function isIdentStart(codepoint: number): boolean;
646
647
  declare function isDigit(codepoint: number): boolean;
647
648
  declare function isIdentCodepoint(codepoint: number): boolean;
@@ -661,8 +662,10 @@ declare function isWhiteSpace(codepoint: number): boolean;
661
662
 
662
663
  declare const getConfig: () => PropertiesConfig;
663
664
 
665
+ declare const funcList: string[];
664
666
  declare function matchType(val: Token, properties: PropertyMapType): boolean;
665
667
 
668
+ declare const colorsFunc: string[];
666
669
  declare function render(data: AstNode, opt?: RenderOptions): RenderResult;
667
670
  declare function renderToken(token: Token, options?: RenderOptions, reducer?: (acc: string, curr: Token) => string): string;
668
671
 
@@ -695,4 +698,4 @@ declare function resolve(url: string, currentDirectory: string, cwd?: string): {
695
698
  declare function parse(iterator: string, opt?: ParserOptions): Promise<ParseResult>;
696
699
  declare function transform(css: string, options?: TransformOptions): Promise<TransformResult>;
697
700
 
698
- export { combinators, dirname, getConfig, hasDeclaration, isAngle, isAtKeyword, isDigit, isDimension, isFrequency, isFunction, isHash, isHexColor, isHexDigit, isIdent, isIdentCodepoint, isIdentStart, isLength, isNewLine, isNumber, isPercentage, isPseudo, isResolution, isTime, isWhiteSpace, load, matchType, matchUrl, minify, minifyRule, parse, parseDimension, parseString, reduceSelector, render, renderToken, resolve, tokenize, transform, urlTokenMatcher, walk };
701
+ export { colorsFunc, combinators, dirname, funcList, getConfig, hasDeclaration, isAngle, isAtKeyword, isColor, isDigit, isDimension, isFrequency, isFunction, isHash, isHexColor, isHexDigit, isIdent, isIdentCodepoint, isIdentStart, isLength, isNewLine, isNumber, isPercentage, isPseudo, isResolution, isTime, isWhiteSpace, load, matchType, matchUrl, minify, minifyRule, parse, parseDimension, parseString, reduceSelector, render, renderToken, resolve, tokenize, transform, urlTokenMatcher, walk };
@@ -1,4 +1,5 @@
1
1
  import { PropertySet } from './set.js';
2
+ import '../../renderer/utils/color.js';
2
3
  import { getConfig } from '../utils/config.js';
3
4
  import { PropertyMap } from './map.js';
4
5
  import { parseString } from '../parse.js';
@@ -1,7 +1,8 @@
1
1
  import { eq } from '../utils/eq.js';
2
+ import { renderToken } from '../../renderer/render.js';
3
+ import '../../renderer/utils/color.js';
2
4
  import { getConfig } from '../utils/config.js';
3
5
  import { matchType } from '../utils/type.js';
4
- import { renderToken } from '../../renderer/render.js';
5
6
  import { parseString } from '../parse.js';
6
7
  import { PropertySet } from './set.js';
7
8
 
@@ -1,4 +1,4 @@
1
- import { isPseudo, isAtKeyword, isFunction, isNumber, isDimension, parseDimension, isPercentage, isIdent, isHexColor, isHash, isIdentStart } from './utils/syntax.js';
1
+ import { isPseudo, isAtKeyword, isFunction, isNumber, isDimension, parseDimension, isPercentage, isIdent, isHexColor, isHash, isIdentStart, isColor } from './utils/syntax.js';
2
2
  import { renderToken } from '../renderer/render.js';
3
3
  import { COLORS_NAMES } from '../renderer/utils/color.js';
4
4
  import { minify, combinators } from '../ast/minify.js';
@@ -52,10 +52,6 @@ async function parse(iterator, opt = {}) {
52
52
  let tokens = results.map(mapToken);
53
53
  let i;
54
54
  let loc;
55
- // if ((<Token>tokens.at(-1))?.typ == 'EOF') {
56
- //
57
- // tokens.pop();
58
- // }
59
55
  for (i = 0; i < tokens.length; i++) {
60
56
  if (tokens[i].typ == 'Comment') {
61
57
  // @ts-ignore
@@ -332,6 +328,11 @@ async function parse(iterator, opt = {}) {
332
328
  if (item == null) {
333
329
  break;
334
330
  }
331
+ // console.debug({item});
332
+ if (item.hint != null && item.hint.startsWith('Bad-')) {
333
+ // bad token
334
+ continue;
335
+ }
335
336
  tokens.push(item);
336
337
  bytesIn = item.bytesIn;
337
338
  if (item.token == ';' || item.token == '{') {
@@ -641,41 +642,33 @@ function parseTokens(tokens, options = {}) {
641
642
  // @ts-ignore
642
643
  t.chi.pop();
643
644
  }
644
- let isColor = true;
645
645
  // @ts-ignore
646
- if (options.parseColor && ['rgb', 'rgba', 'hsl', 'hsla', 'hwb', 'device-cmyk'].includes(t.val)) {
646
+ if (options.parseColor && t.typ == 'Func' && isColor(t)) {
647
+ // if (isColor) {
647
648
  // @ts-ignore
648
- for (const v of t.chi) {
649
- if (v.typ == 'Func' && v.val == 'var') {
650
- isColor = false;
651
- break;
652
- }
653
- }
654
- if (isColor) {
655
- // @ts-ignore
656
- t.typ = 'Color';
657
- // @ts-ignore
658
- t.kin = t.val;
649
+ t.typ = 'Color';
650
+ // @ts-ignore
651
+ t.kin = t.val;
652
+ // @ts-ignore
653
+ let m = t.chi.length;
654
+ while (m-- > 0) {
659
655
  // @ts-ignore
660
- let m = t.chi.length;
661
- while (m-- > 0) {
656
+ if (['Literal'].concat(trimWhiteSpace).includes(t.chi[m].typ)) {
662
657
  // @ts-ignore
663
- if (['Literal'].concat(trimWhiteSpace).includes(t.chi[m].typ)) {
658
+ if (t.chi[m + 1]?.typ == 'Whitespace') {
664
659
  // @ts-ignore
665
- if (t.chi[m + 1]?.typ == 'Whitespace') {
666
- // @ts-ignore
667
- t.chi.splice(m + 1, 1);
668
- }
660
+ t.chi.splice(m + 1, 1);
661
+ }
662
+ // @ts-ignore
663
+ if (t.chi[m - 1]?.typ == 'Whitespace') {
669
664
  // @ts-ignore
670
- if (t.chi[m - 1]?.typ == 'Whitespace') {
671
- // @ts-ignore
672
- t.chi.splice(m - 1, 1);
673
- m--;
674
- }
665
+ t.chi.splice(m - 1, 1);
666
+ m--;
675
667
  }
676
668
  }
677
- continue;
678
669
  }
670
+ continue;
671
+ // }
679
672
  }
680
673
  if (t.typ == 'UrlFunc') {
681
674
  // @ts-ignore
@@ -700,7 +693,7 @@ function parseTokens(tokens, options = {}) {
700
693
  // @ts-ignore
701
694
  if (t.chi.length > 0) {
702
695
  // @ts-ignore
703
- parseTokens(t.chi, t.typ);
696
+ parseTokens(t.chi, options);
704
697
  if (t.typ == 'Pseudo-class-func' && t.val == ':is' && options.minify) {
705
698
  //
706
699
  const count = t.chi.filter(t => t.typ != 'Comment').length;
@@ -719,7 +712,7 @@ function parseTokens(tokens, options = {}) {
719
712
  if (t.typ == 'Iden') {
720
713
  // named color
721
714
  const value = t.val.toLowerCase();
722
- if (COLORS_NAMES[value] != null) {
715
+ if (value in COLORS_NAMES) {
723
716
  Object.assign(t, {
724
717
  typ: 'Color',
725
718
  val: COLORS_NAMES[value].length < value.length ? COLORS_NAMES[value] : value,
@@ -154,31 +154,18 @@ function* tokenize(iterator) {
154
154
  return char;
155
155
  }
156
156
  while (value = next()) {
157
- if (ind >= iterator.length) {
158
- if (buffer.length > 0) {
159
- yield pushToken(buffer);
160
- buffer = '';
161
- }
162
- break;
163
- }
164
157
  if (isWhiteSpace(value.charCodeAt(0))) {
165
158
  if (buffer.length > 0) {
166
159
  yield pushToken(buffer);
167
160
  buffer = '';
168
161
  }
169
162
  while (value = next()) {
170
- if (ind >= iterator.length) {
171
- break;
172
- }
173
163
  if (!isWhiteSpace(value.charCodeAt(0))) {
174
164
  break;
175
165
  }
176
166
  }
177
167
  yield pushToken('', 'Whitespace');
178
168
  buffer = '';
179
- if (ind >= iterator.length) {
180
- break;
181
- }
182
169
  }
183
170
  switch (value) {
184
171
  case '/':
@@ -192,34 +179,12 @@ function* tokenize(iterator) {
192
179
  }
193
180
  buffer += value;
194
181
  if (peek() == '*') {
195
- buffer += '*';
196
- // i++;
197
- next();
182
+ buffer += next();
198
183
  while (value = next()) {
199
- if (ind >= iterator.length) {
200
- yield pushToken(buffer, 'Bad-comment');
201
- break;
202
- }
203
- if (value == '\\') {
204
- buffer += value;
205
- value = next();
206
- if (ind >= iterator.length) {
207
- yield pushToken(buffer, 'Bad-comment');
208
- break;
209
- }
210
- buffer += value;
211
- continue;
212
- }
213
184
  if (value == '*') {
214
185
  buffer += value;
215
- value = next();
216
- if (ind >= iterator.length) {
217
- yield pushToken(buffer, 'Bad-comment');
218
- break;
219
- }
220
- buffer += value;
221
- if (value == '/') {
222
- yield pushToken(buffer, 'Comment');
186
+ if (peek() == '/') {
187
+ yield pushToken(buffer + next(), 'Comment');
223
188
  buffer = '';
224
189
  break;
225
190
  }
@@ -228,6 +193,8 @@ function* tokenize(iterator) {
228
193
  buffer += value;
229
194
  }
230
195
  }
196
+ yield pushToken(buffer, 'Bad-comment');
197
+ buffer = '';
231
198
  }
232
199
  break;
233
200
  case '<':
@@ -241,15 +208,9 @@ function* tokenize(iterator) {
241
208
  break;
242
209
  }
243
210
  buffer += value;
244
- value = next();
245
- if (ind >= iterator.length) {
246
- break;
247
- }
248
211
  if (peek(3) == '!--') {
212
+ buffer += next(3);
249
213
  while (value = next()) {
250
- if (ind >= iterator.length) {
251
- break;
252
- }
253
214
  buffer += value;
254
215
  if (value == '>' && prev(2) == '--') {
255
216
  yield pushToken(buffer, 'CDOCOMM');
@@ -258,15 +219,14 @@ function* tokenize(iterator) {
258
219
  }
259
220
  }
260
221
  }
261
- if (ind >= iterator.length) {
262
- yield pushToken(buffer, 'BADCDO');
263
- buffer = '';
264
- }
222
+ // if (!peek()) {
223
+ yield pushToken(buffer, 'Bad-cdo');
224
+ buffer = '';
225
+ // }
265
226
  break;
266
227
  case '\\':
267
- value = next();
268
228
  // EOF
269
- if (ind + 1 >= iterator.length) {
229
+ if (!(value = next())) {
270
230
  // end of stream ignore \\
271
231
  yield pushToken(buffer);
272
232
  buffer = '';
@@ -285,8 +245,7 @@ function* tokenize(iterator) {
285
245
  buffer = '';
286
246
  }
287
247
  buffer += value;
288
- value = next();
289
- if (ind >= iterator.length) {
248
+ if (!(value = next())) {
290
249
  yield pushToken(buffer);
291
250
  buffer = '';
292
251
  break;
@@ -453,8 +412,7 @@ function* tokenize(iterator) {
453
412
  yield pushToken(buffer);
454
413
  buffer = '';
455
414
  }
456
- const important = peek(9);
457
- if (important == 'important') {
415
+ if (peek(9) == 'important') {
458
416
  yield pushToken('', 'Important');
459
417
  next(9);
460
418
  buffer = '';
@@ -1,3 +1,6 @@
1
+ import { colorsFunc } from '../../renderer/render.js';
2
+ import { COLORS_NAMES } from '../../renderer/utils/color.js';
3
+
1
4
  // https://www.w3.org/TR/CSS21/syndata.html#syntax
2
5
  // https://www.w3.org/TR/2021/CRD-css-syntax-3-20211224/#typedef-ident-token
3
6
  // '\\'
@@ -23,6 +26,25 @@ function isTime(dimension) {
23
26
  function isFrequency(dimension) {
24
27
  return 'unit' in dimension && ['hz', 'khz'].includes(dimension.unit.toLowerCase());
25
28
  }
29
+ function isColor(token) {
30
+ if (token.typ == 'Color') {
31
+ return true;
32
+ }
33
+ if (token.typ == 'Iden') {
34
+ // named color
35
+ return token.val.toLowerCase() in COLORS_NAMES;
36
+ }
37
+ if (token.typ == 'Func' && token.chi.length > 0 && colorsFunc.includes(token.val)) {
38
+ // @ts-ignore
39
+ for (const v of token.chi) {
40
+ if (!['Number', 'Perc', 'Comma', 'Whitespace'].includes(v.typ)) {
41
+ return false;
42
+ }
43
+ }
44
+ return true;
45
+ }
46
+ return false;
47
+ }
26
48
  function isLetter(codepoint) {
27
49
  // lowercase
28
50
  return (codepoint >= 0x61 && codepoint <= 0x7a) ||
@@ -75,19 +97,12 @@ function isIdent(name) {
75
97
  return true;
76
98
  }
77
99
  function isPseudo(name) {
78
- if (name.charAt(0) != ':') {
79
- return false;
80
- }
81
- if (name.endsWith('(')) {
82
- return isIdent(name.charAt(1) == ':' ? name.slice(2, -1) : name.slice(1, -1));
83
- }
84
- return isIdent(name.charAt(1) == ':' ? name.slice(2) : name.slice(1));
100
+ return name.charAt(0) == ':' &&
101
+ ((name.endsWith('(') && isIdent(name.charAt(1) == ':' ? name.slice(2, -1) : name.slice(1, -1))) ||
102
+ isIdent(name.charAt(1) == ':' ? name.slice(2) : name.slice(1)));
85
103
  }
86
104
  function isHash(name) {
87
- if (name.charAt(0) != '#') {
88
- return false;
89
- }
90
- return isIdent(name.charAt(1));
105
+ return name.charAt(0) == '#' && isIdent(name.charAt(1));
91
106
  }
92
107
  function isNumber(name) {
93
108
  if (name.length == 0) {
@@ -256,4 +271,4 @@ function isWhiteSpace(codepoint) {
256
271
  codepoint == 0xa || codepoint == 0xc || codepoint == 0xd;
257
272
  }
258
273
 
259
- export { isAngle, isAtKeyword, isDigit, isDimension, isFrequency, isFunction, isHash, isHexColor, isHexDigit, isIdent, isIdentCodepoint, isIdentStart, isLength, isNewLine, isNumber, isPercentage, isPseudo, isResolution, isTime, isWhiteSpace, parseDimension };
274
+ export { isAngle, isAtKeyword, isColor, isDigit, isDimension, isFrequency, isFunction, isHash, isHexColor, isHexDigit, isIdent, isIdentCodepoint, isIdentStart, isLength, isNewLine, isNumber, isPercentage, isPseudo, isResolution, isTime, isWhiteSpace, parseDimension };
@@ -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,6 @@
1
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'];
3
4
  function reduceNumber(val) {
4
5
  val = (+val).toString();
5
6
  if (val === '0') {
@@ -257,14 +258,14 @@ function renderToken(token, options = {}, reducer) {
257
258
  }
258
259
  }
259
260
  if (val === '0') {
260
- if (unit == 'Time') {
261
+ if (token.typ == 'Time') {
261
262
  return '0s';
262
263
  }
263
- if (unit == 'Frequency') {
264
+ if (token.typ == 'Frequency') {
264
265
  return '0Hz';
265
266
  }
266
267
  // @ts-ignore
267
- if (unit == 'Resolution') {
268
+ if (token.typ == 'Resolution') {
268
269
  return '0x';
269
270
  }
270
271
  return '0';
@@ -304,4 +305,4 @@ function renderToken(token, options = {}, reducer) {
304
305
  return '';
305
306
  }
306
307
 
307
- export { render, renderToken };
308
+ export { colorsFunc, render, renderToken };
@@ -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,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,10 +12,10 @@ import { load } from './load.js';
12
12
  import { resolve } from '../lib/fs/resolve.js';
13
13
  export { dirname, 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, { load, resolve, cwd: opt.cwd ?? process.cwd() }));
17
17
  }
18
- function transform(css, options = {}) {
18
+ async function transform(css, options = {}) {
19
19
  return transform$1(css, Object.assign(options, { load, resolve, cwd: options.cwd ?? process.cwd() }));
20
20
  }
21
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,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-rc4",
4
+ "version": "0.0.1-rc5",
5
5
  "exports": {
6
6
  ".": "./dist/node/index.js",
7
7
  "./umd": "./dist/index-umd-web.js",