@tbela99/css-parser 1.6.0 → 1.6.1

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.
@@ -50,7 +50,6 @@ function parseGridTemplate(template) {
50
50
  * @param errors
51
51
  */
52
52
  function parseDeclaration(tokens, parent, options, errors) {
53
- // console.error(tokens);
54
53
  const name = tokens.shift();
55
54
  let i;
56
55
  let rules = null;
@@ -384,10 +383,19 @@ function parseDeclaration(tokens, parent, options, errors) {
384
383
  }
385
384
  if (trimTokenSpace.has(token.typ)) {
386
385
  if (tokens[i + 1]?.typ === EnumToken.WhitespaceTokenType) {
387
- tokens.splice(i + 1, 1);
386
+ for (let index = i + 1; index < tokens.length - 1; index++) {
387
+ tokens[index] = tokens[index + 1];
388
+ }
389
+ tokens.length--;
390
+ i--;
391
+ continue;
388
392
  }
389
393
  if (tokens[i - 1]?.typ == EnumToken.WhitespaceTokenType) {
390
- tokens.splice(--i, 1);
394
+ for (let index = i - 1; index < tokens.length - 1; index++) {
395
+ tokens[index] = tokens[index + 1];
396
+ }
397
+ tokens.length--;
398
+ i--;
391
399
  }
392
400
  }
393
401
  }
@@ -74,7 +74,6 @@ function parseSelector(tokens, context, options, errors) {
74
74
  const stack = [];
75
75
  const uniq = new Map();
76
76
  let allowed = true;
77
- let i = 0;
78
77
  let index;
79
78
  let parent = context;
80
79
  let nested = false;
@@ -108,119 +107,130 @@ function parseSelector(tokens, context, options, errors) {
108
107
  nested = parent?.typ == EnumToken.RuleNodeType;
109
108
  parent = parent?.[PARENT];
110
109
  } while (!nested && parent != null);
111
- for (; i < tokens.length; i++) {
112
- if (tokens[i].typ == EnumToken.ColonTokenType) {
113
- if (tokens[i + 1]?.typ == EnumToken.IdenTokenType) {
114
- Object.assign(tokens[i], {
110
+ const all = new Array(tokens.length);
111
+ let write = 0;
112
+ for (let read = 0; read < tokens.length; read++) {
113
+ let token = tokens[read];
114
+ if (token.typ == EnumToken.ColonTokenType) {
115
+ const next = tokens[read + 1];
116
+ if (next?.typ == EnumToken.IdenTokenType) {
117
+ all[write++] = {
118
+ ...token,
115
119
  typ: EnumToken.PseudoElementTokenType,
116
- val: ":" + tokens[i + 1].val,
117
- });
118
- tokens[i][LOCEND] = tokens[i + 1][LOCEND];
119
- tokens.splice(i + 1, 1);
120
+ val: ":" + next.val,
121
+ [LOCEND]: next[LOCEND],
122
+ };
123
+ read++;
120
124
  continue;
121
125
  }
122
- if (tokens[i + 1]?.typ == EnumToken.FunctionTokenDefType) {
123
- val = ":" + tokens[i + 1].val;
124
- Object.assign(tokens[i], {
126
+ if (next?.typ == EnumToken.FunctionTokenDefType) {
127
+ val = ":" + next.val;
128
+ all[write++] = {
129
+ ...token,
125
130
  typ: val + "()" in getSyntaxConfig().selectors
126
131
  ? EnumToken.PseudoClassFunctionTokenDefType
127
- : tokens[i + 1].typ,
132
+ : next.typ,
128
133
  val,
129
- });
130
- tokens[i][LOCEND] = tokens[i + 1][LOCEND];
131
- tokens.splice(i + 1, 1);
134
+ [LOCEND]: next[LOCEND],
135
+ };
136
+ read++;
132
137
  continue;
133
138
  }
134
139
  }
135
- if (tokens[i].typ == EnumToken.DoubleColonTokenType) {
136
- val = ":" + tokens[i + 1].val;
137
- if (tokens[i + 1]?.typ == EnumToken.IdenTokenType) {
138
- Object.assign(tokens[i], {
140
+ if (token.typ == EnumToken.DoubleColonTokenType) {
141
+ const next = tokens[read + 1];
142
+ val = ":" + next.val;
143
+ if (next?.typ == EnumToken.IdenTokenType) {
144
+ all[write++] = {
145
+ ...token,
139
146
  typ: EnumToken.PseudoClassTokenType,
140
147
  val: (pseudoElements.includes(val) ? "" : ":") + val,
141
- });
142
- tokens[i][LOCEND] = tokens[i + 1][LOCEND];
143
- tokens.splice(i + 1, 1);
148
+ [LOCEND]: next[LOCEND],
149
+ };
150
+ read++;
144
151
  continue;
145
152
  }
146
- if (tokens[i + 1]?.typ == EnumToken.FunctionTokenDefType) {
147
- val = "::" + tokens[i + 1].val;
148
- Object.assign(tokens[i], {
153
+ if (next?.typ == EnumToken.FunctionTokenDefType) {
154
+ val = "::" + next.val;
155
+ all[write++] = {
156
+ ...token,
149
157
  typ: val + "()" in getSyntaxConfig().selectors
150
158
  ? EnumToken.PseudoClassFunctionTokenDefType
151
159
  : EnumToken.FunctionTokenDefType,
152
160
  val,
153
- });
154
- tokens[i][LOCEND] = tokens[i + 1][LOCEND];
155
- tokens.splice(i + 1, 1);
161
+ [LOCEND]: next[LOCEND],
162
+ };
163
+ read++;
156
164
  continue;
157
165
  }
158
166
  }
159
- if (tokens[i].typ == EnumToken.ColorTokenType) {
160
- if (isHash(tokens[i].val)) {
161
- Object.assign(tokens[i], {
162
- typ: EnumToken.HashTokenType,
163
- });
167
+ if (token.typ == EnumToken.ColorTokenType) {
168
+ if (isHash(token.val)) {
169
+ token.typ = EnumToken.HashTokenType;
170
+ all[write++] = token;
171
+ continue;
164
172
  }
165
- else {
166
- return {
167
- typ: EnumToken.RuleNodeType,
168
- sel: [
169
- ...tokens
170
- .reduce((acc, curr, index, array) => {
171
- // if (curr.typ == EnumToken.CommentTokenType) {
172
- // return acc;
173
- // }
174
- if (curr.typ == EnumToken.WhitespaceTokenType) {
175
- if (trimWhiteSpace.includes(array[index - 1]?.typ) ||
176
- trimWhiteSpace.includes(array[index + 1]?.typ) ||
177
- combinators.includes(array[index - 1]?.val) ||
178
- combinators.includes(array[index + 1]?.val)) {
179
- return acc;
180
- }
181
- }
182
- let t = renderValue(curr, { minify: false });
183
- if (t == ",") {
184
- acc.push([]);
185
- }
186
- else {
187
- acc[acc.length - 1].push(t);
173
+ return {
174
+ typ: EnumToken.RuleNodeType,
175
+ sel: [
176
+ ...tokens
177
+ .reduce((acc, curr, index, array) => {
178
+ if (curr.typ == EnumToken.WhitespaceTokenType) {
179
+ if (trimWhiteSpace.includes(array[index - 1]?.typ) ||
180
+ trimWhiteSpace.includes(array[index + 1]?.typ) ||
181
+ combinators.includes(array[index - 1]?.val) ||
182
+ combinators.includes(array[index + 1]?.val)) {
183
+ return acc;
188
184
  }
189
- return acc;
190
- }, [[]])
191
- .reduce((acc, curr) => {
192
- let i = 0;
193
- for (; i < curr.length; i++) {
194
- if (i + 1 < curr.length && curr[i] == "*") {
195
- if (curr[i] == "*") {
196
- let index = curr[i + 1] == " " ? 2 : 1;
197
- if (![">", "~", "+"].includes(curr[index])) {
198
- curr.splice(i, index);
199
- }
185
+ }
186
+ let t = renderValue(curr, { minify: false });
187
+ if (t == ",") {
188
+ acc.push([]);
189
+ }
190
+ else {
191
+ acc[acc.length - 1].push(t);
192
+ }
193
+ return acc;
194
+ }, [[]])
195
+ .reduce((acc, curr) => {
196
+ let i = 0;
197
+ for (; i < curr.length; i++) {
198
+ if (i + 1 < curr.length && curr[i] == "*") {
199
+ if (curr[i] == "*") {
200
+ let index = curr[i + 1] == " " ? 2 : 1;
201
+ if (![">", "~", "+"].includes(curr[index])) {
202
+ curr.splice(i, index);
200
203
  }
201
204
  }
202
205
  }
203
- acc.set(curr.join(""), curr);
204
- return acc;
205
- }, uniq)
206
- .keys(),
207
- ].join(","),
208
- chi: [],
209
- [LOCSRCID]: tokens[0][LOCSRCID],
210
- [LOCSTA]: tokens[0][LOCSTA],
211
- [LOCEND]: tokens[tokens.length - 1][LOCEND],
212
- [TOKENS]: tokens,
213
- [STATE]: EnumAstNodeStatus.Invalid,
214
- [ERRORS]: [
215
- {
216
- action: "drop",
217
- node: tokens[i],
218
- message: "invalid hash id",
219
- },
220
- ],
221
- };
222
- }
206
+ }
207
+ acc.set(curr.join(""), curr);
208
+ return acc;
209
+ }, uniq)
210
+ .keys(),
211
+ ].join(","),
212
+ chi: [],
213
+ [LOCSRCID]: tokens[0][LOCSRCID],
214
+ [LOCSTA]: tokens[0][LOCSTA],
215
+ [LOCEND]: tokens[tokens.length - 1][LOCEND],
216
+ [TOKENS]: tokens,
217
+ [STATE]: EnumAstNodeStatus.Invalid,
218
+ [ERRORS]: [
219
+ {
220
+ action: "drop",
221
+ node: token,
222
+ message: "invalid hash id",
223
+ },
224
+ ],
225
+ };
226
+ }
227
+ all[write++] = token;
228
+ }
229
+ if (write !== tokens.length) {
230
+ for (let index = 0; index < write; index++) {
231
+ tokens[index] = all[index];
223
232
  }
233
+ tokens.length = write;
224
234
  }
225
235
  const result = matchSelectorSyntax(tokens, errors, options, nested === true);
226
236
  trimArray(tokens);
@@ -515,8 +515,8 @@ function renderValue(token, options = {}, cache = Object.create(null), reducer,
515
515
  return token.val;
516
516
  }
517
517
  if (Array.isArray(token.chi)) {
518
- const fnName = token.val.toLowerCase();
519
- const isLegacy = ["rgb", "rgba", "hsl", "hsla"].includes(token.val.toLowerCase());
518
+ const fnName = token.val;
519
+ const isLegacy = ["rgb", "rgba", "hsl", "hsla"].includes(token.val);
520
520
  const hasAlpha = [
521
521
  "rgb",
522
522
  "rgba",
@@ -47,9 +47,9 @@ function decode(str) {
47
47
  * @returns
48
48
  */
49
49
  function encode(value) {
50
- if (typeof value === 'number') {
51
- return encode_integer(value);
52
- }
50
+ // if (typeof value === 'number') {
51
+ // return encode_integer(value);
52
+ // }
53
53
  let result = '';
54
54
  for (let i = 0; i < value.length; i += 1) {
55
55
  result += encode_integer(value[i]);
@@ -61,9 +61,10 @@ function color2cmykToken(token) {
61
61
  }
62
62
  function srgb2cmykvalues(r, g, b, a = null) {
63
63
  const k = 1 - Math.max(r, g, b);
64
- const c = k == 1 ? 0 : (1 - r - k) / (1 - k);
65
- const m = k == 1 ? 0 : (1 - g - k) / (1 - k);
66
- const y = k == 1 ? 0 : (1 - b - k) / (1 - k);
64
+ const div = 1 - k;
65
+ const c = k == 1 ? 0 : (1 - r - k) / div;
66
+ const m = k == 1 ? 0 : (1 - g - k) / div;
67
+ const y = k == 1 ? 0 : (1 - b - k) / div;
67
68
  const result = [c, m, y, k];
68
69
  if (a != null && a < 1) {
69
70
  result.push(a);
@@ -116,10 +116,11 @@ function cmyk2srgbvalues(token) {
116
116
  t = components[3];
117
117
  // @ts-ignore
118
118
  const k = getNumber(t);
119
+ const mul = 1 - k;
119
120
  const rgb = [
120
- 1 - Math.min(1, c * (1 - k) + k),
121
- 1 - Math.min(1, m * (1 - k) + k),
122
- 1 - Math.min(1, y * (1 - k) + k),
121
+ 1 - Math.min(1, c * mul + k),
122
+ 1 - Math.min(1, m * mul + k),
123
+ 1 - Math.min(1, y * mul + k),
123
124
  ];
124
125
  if (components.length == 5) {
125
126
  rgb.push(getNumber(components[4]));
@@ -1,5 +1,5 @@
1
1
  import { EnumToken } from '../ast/types.js';
2
- import config from '../validation/config.json.js';
2
+ import config from '../data/validation.json.js';
3
3
 
4
4
  /**
5
5
  * Location source id
@@ -1,4 +1,4 @@
1
- import config from './config.json.js';
1
+ import config from '../data/validation.json.js';
2
2
  import { trimSyntaxArray, parseSyntax } from './parser/parse.js';
3
3
  import { ValidationTokenEnum } from './parser/typedef.js';
4
4
  import { memoize } from '../parser/utils/cache.js';
@@ -677,9 +677,13 @@ function parseSyntax(syntax) {
677
677
  stack.pop();
678
678
  }
679
679
  }
680
- if (stack.length > 0) {
681
- throw new SyntaxError(`Unexpected token ${ValidationTokenEnum[stack.at(-1)?.typ]} at ${stack.at(-1)?.[LOC]?.lin}:${stack.at(-1)?.[LOC]?.col}`);
682
- }
680
+ // if (stack.length > 0) {
681
+ // throw new SyntaxError(
682
+ // `Unexpected token ${ValidationTokenEnum[stack.at(-1)?.typ as ValidationTokenEnum]} at ${
683
+ // stack.at(-1)?.[LOC]?.lin
684
+ // }:${stack.at(-1)?.[LOC]?.col}`,
685
+ // );
686
+ // }
683
687
  return trimSyntaxArray(tokens);
684
688
  }
685
689
 
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@tbela99/css-parser",
3
3
  "description": "CSS parser, minifier and validator for node and the browser",
4
- "version": "1.6.0",
4
+ "version": "1.6.1",
5
5
  "exports": {
6
6
  ".": "./dist/node.js",
7
7
  "./node": "./dist/node.js",
@@ -66,16 +66,16 @@
66
66
  "@rollup/plugin-typescript": "^12.3.0",
67
67
  "@types/chai": "^5.2.3",
68
68
  "@types/mocha": "^10.0.10",
69
- "@types/node": "^26.2.0",
69
+ "@types/node": "^26.4.1",
70
70
  "@types/web": "^0.0.354",
71
71
  "@web/test-runner": "^1.0.0",
72
72
  "@web/test-runner-playwright": "^1.0.0",
73
73
  "chai": "^6.2.2",
74
74
  "esno": "^4.8.0",
75
75
  "markdown-it-task-lists": "^2.1.1",
76
- "mocha": "^11.8.0",
76
+ "mocha": "^12.0.0",
77
77
  "playwright": "^1.62.1",
78
- "rollup": "^4.62.4",
78
+ "rollup": "^4.63.1",
79
79
  "rollup-plugin-dts": "^6.5.1",
80
80
  "superc8": "^12.7.0",
81
81
  "tslib": "^2.8.1",