Skip to content

Commit 0f95d95

Browse files
committed
fix parseComponentValue
1 parent 4da8b05 commit 0f95d95

File tree

8 files changed

+63
-13
lines changed

8 files changed

+63
-13
lines changed

packages/css-parser-algorithms/dist/index.cjs

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

packages/css-parser-algorithms/dist/index.d.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -362,10 +362,10 @@ export declare function parseCommaSeparatedListOfComponentValues(tokens: Array<C
362362
* @example
363363
* ```js
364364
* import { tokenize } from '@csstools/css-tokenizer';
365-
* import { parseCommaSeparatedListOfComponentValues } from '@csstools/css-parser-algorithms';
365+
* import { parseComponentValue } from '@csstools/css-parser-algorithms';
366366
*
367-
* parseCommaSeparatedListOfComponentValues(tokenize({ css: `10px` }));
368-
* parseCommaSeparatedListOfComponentValues(tokenize({ css: `calc((10px + 1x) * 4)` }));
367+
* parseComponentValue(tokenize({ css: `10px` }));
368+
* parseComponentValue(tokenize({ css: `calc((10px + 1x) * 4)` }));
369369
* ```
370370
*/
371371
export declare function parseComponentValue(tokens: Array<CSSToken>, options?: {

packages/css-parser-algorithms/dist/index.mjs

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

packages/css-parser-algorithms/docs/css-parser-algorithms.api.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2046,7 +2046,7 @@
20462046
{
20472047
"kind": "Function",
20482048
"canonicalReference": "@csstools/css-parser-algorithms!parseComponentValue:function(1)",
2049-
"docComment": "/**\n * Parse a single component value.\n *\n * @example\n * ```js\n * import { tokenize } from '@csstools/css-tokenizer';\n * import { parseCommaSeparatedListOfComponentValues } from '@csstools/css-parser-algorithms';\n *\n * parseCommaSeparatedListOfComponentValues(tokenize({ css: `10px` }));\n * parseCommaSeparatedListOfComponentValues(tokenize({ css: `calc((10px + 1x) * 4)` }));\n * ```\n *\n */\n",
2049+
"docComment": "/**\n * Parse a single component value.\n *\n * @example\n * ```js\n * import { tokenize } from '@csstools/css-tokenizer';\n * import { parseComponentValue } from '@csstools/css-parser-algorithms';\n *\n * parseComponentValue(tokenize({ css: `10px` }));\n * parseComponentValue(tokenize({ css: `calc((10px + 1x) * 4)` }));\n * ```\n *\n */\n",
20502050
"excerptTokens": [
20512051
{
20522052
"kind": "Content",

packages/css-parser-algorithms/docs/css-parser-algorithms.parsecomponentvalue.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,9 @@ _(Optional)_
7373

7474
```js
7575
import { tokenize } from '@csstools/css-tokenizer';
76-
import { parseCommaSeparatedListOfComponentValues } from '@csstools/css-parser-algorithms';
76+
import { parseComponentValue } from '@csstools/css-parser-algorithms';
7777

78-
parseCommaSeparatedListOfComponentValues(tokenize({ css: `10px` }));
79-
parseCommaSeparatedListOfComponentValues(tokenize({ css: `calc((10px + 1x) * 4)` }));
78+
parseComponentValue(tokenize({ css: `10px` }));
79+
parseComponentValue(tokenize({ css: `calc((10px + 1x) * 4)` }));
8080
```
8181

packages/css-parser-algorithms/src/parse/component-value.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ import { consumeComponentValue } from '../consume/component-block-function';
99
* @example
1010
* ```js
1111
* import { tokenize } from '@csstools/css-tokenizer';
12-
* import { parseCommaSeparatedListOfComponentValues } from '@csstools/css-parser-algorithms';
12+
* import { parseComponentValue } from '@csstools/css-parser-algorithms';
1313
*
14-
* parseCommaSeparatedListOfComponentValues(tokenize({ css: `10px` }));
15-
* parseCommaSeparatedListOfComponentValues(tokenize({ css: `calc((10px + 1x) * 4)` }));
14+
* parseComponentValue(tokenize({ css: `10px` }));
15+
* parseComponentValue(tokenize({ css: `calc((10px + 1x) * 4)` }));
1616
* ```
1717
*/
1818
export function parseComponentValue(tokens: Array<CSSToken>, options?: { onParseError?: (error: ParseError) => void }): ComponentValue|undefined {
@@ -26,7 +26,7 @@ export function parseComponentValue(tokens: Array<CSSToken>, options?: { onParse
2626

2727
// We expect the last token to be an EOF token.
2828
// Passing slices of tokens to this function can easily cause the EOF token to be missing.
29-
if (isTokenEOF(tokensCopy[tokensCopy.length - 1])) {
29+
if (!isTokenEOF(tokensCopy[tokensCopy.length - 1])) {
3030
tokensCopy.push([
3131
TokenType.EOF,
3232
'',
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { tokenize } from '@csstools/css-tokenizer';
2+
import test from 'node:test';
3+
import { parseComponentValue } from '@csstools/css-parser-algorithms';
4+
5+
const onParseError = (err) => {
6+
throw err;
7+
};
8+
9+
test('component-value', (t) => {
10+
const cases = [
11+
{
12+
tokens: tokenize({ css: 'foo()' }, { onParseError: onParseError }),
13+
expectSuccess: true,
14+
},
15+
{
16+
tokens: tokenize({ css: 'foo()' }, { onParseError: onParseError }).slice(0, -1),
17+
expectSuccess: true,
18+
},
19+
{
20+
tokens: 'foo() bar',
21+
expectSuccess: false,
22+
},
23+
];
24+
25+
cases.forEach((testCase, index) => {
26+
t.test(index, () => {
27+
let didError = false;
28+
29+
const result = parseComponentValue(testCase.tokens, {
30+
onParseError: (err) => {
31+
didError = true;
32+
33+
if (testCase.expectSuccess) {
34+
throw err;
35+
}
36+
},
37+
});
38+
39+
if (testCase.expectSuccess && !result) {
40+
throw new Error('Expected test to pass and have a result');
41+
}
42+
43+
if (!testCase.expectSuccess && !didError) {
44+
throw new Error('Expected test to fail');
45+
}
46+
});
47+
});
48+
});

packages/css-parser-algorithms/test/test.mjs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import './cases/component-value/0001.mjs';
2+
13
import './cases/media-not/0001.mjs';
24

35
import './cases/mf-boolean/0001.mjs';

0 commit comments

Comments
 (0)