Skip to content

Commit c4d46b5

Browse files
authored
postcss-light-dark-function (#1283)
1 parent 809dcaa commit c4d46b5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+2325
-49
lines changed

.github/ISSUE_TEMPLATE/css-issue.yml

+1
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ body:
9797
- PostCSS Initial
9898
- PostCSS Is Pseudo Class
9999
- PostCSS Lab Function
100+
- PostCSS Light Dark Function
100101
- PostCSS Logical
101102
- PostCSS Logical Float and Clear
102103
- PostCSS Logical Overflow

.github/ISSUE_TEMPLATE/plugin-issue.yml

+1
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ body:
9494
- PostCSS Initial
9595
- PostCSS Is Pseudo Class
9696
- PostCSS Lab Function
97+
- PostCSS Light Dark Function
9798
- PostCSS Logical
9899
- PostCSS Logical Float and Clear
99100
- PostCSS Logical Overflow

.github/labeler.yml

+6
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,12 @@
251251
- plugins/postcss-lab-function/**
252252
- experimental/postcss-lab-function/**
253253

254+
"plugins/postcss-light-dark-function":
255+
- changed-files:
256+
- any-glob-to-any-file:
257+
- plugins/postcss-light-dark-function/**
258+
- experimental/postcss-light-dark-function/**
259+
254260
"plugins/postcss-logical":
255261
- changed-files:
256262
- any-glob-to-any-file:

package-lock.json

+34
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/css-parser-algorithms/CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changes to CSS Parser Algorithms
22

3+
### Unreleased (minor)
4+
5+
- Add support for multiple replacement values in `replaceComponentValues`
6+
37
### 2.5.0
48

59
_December 31, 2023_

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

+1-1
Large diffs are not rendered by default.

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ export declare function parseListOfComponentValues(tokens: Array<CSSToken>, opti
388388
* Replace specific component values in a list of component values.
389389
* A helper for the most common and simplistic cases when mutating an AST.
390390
*/
391-
export declare function replaceComponentValues(componentValuesList: Array<Array<ComponentValue>>, replaceWith: (componentValue: ComponentValue) => ComponentValue | void): ComponentValue[][];
391+
export declare function replaceComponentValues(componentValuesList: Array<Array<ComponentValue>>, replaceWith: (componentValue: ComponentValue) => Array<ComponentValue> | ComponentValue | void): ComponentValue[][];
392392

393393
/**
394394
* A simple block node.

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

+1-1
Large diffs are not rendered by default.

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

+21-3
Original file line numberDiff line numberDiff line change
@@ -2189,6 +2189,24 @@
21892189
"kind": "Content",
21902190
"text": ") => "
21912191
},
2192+
{
2193+
"kind": "Reference",
2194+
"text": "Array",
2195+
"canonicalReference": "!Array:interface"
2196+
},
2197+
{
2198+
"kind": "Content",
2199+
"text": "<"
2200+
},
2201+
{
2202+
"kind": "Reference",
2203+
"text": "ComponentValue",
2204+
"canonicalReference": "@csstools/css-parser-algorithms!ComponentValue:type"
2205+
},
2206+
{
2207+
"kind": "Content",
2208+
"text": "> | "
2209+
},
21922210
{
21932211
"kind": "Reference",
21942212
"text": "ComponentValue",
@@ -2218,8 +2236,8 @@
22182236
],
22192237
"fileUrlPath": "dist/_types/util/replace-component-values.d.ts",
22202238
"returnTypeTokenRange": {
2221-
"startIndex": 14,
2222-
"endIndex": 16
2239+
"startIndex": 18,
2240+
"endIndex": 20
22232241
},
22242242
"releaseTag": "Public",
22252243
"overloadIndex": 1,
@@ -2236,7 +2254,7 @@
22362254
"parameterName": "replaceWith",
22372255
"parameterTypeTokenRange": {
22382256
"startIndex": 8,
2239-
"endIndex": 13
2257+
"endIndex": 17
22402258
},
22412259
"isOptional": false
22422260
}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ Replace specific component values in a list of component values. A helper for th
99
**Signature:**
1010

1111
```typescript
12-
export declare function replaceComponentValues(componentValuesList: Array<Array<ComponentValue>>, replaceWith: (componentValue: ComponentValue) => ComponentValue | void): ComponentValue[][];
12+
export declare function replaceComponentValues(componentValuesList: Array<Array<ComponentValue>>, replaceWith: (componentValue: ComponentValue) => Array<ComponentValue> | ComponentValue | void): ComponentValue[][];
1313
```
1414

1515
## Parameters
1616

1717
| Parameter | Type | Description |
1818
| --- | --- | --- |
1919
| componentValuesList | Array&lt;Array&lt;[ComponentValue](./css-parser-algorithms.componentvalue.md)<!-- -->&gt;&gt; | |
20-
| replaceWith | (componentValue: [ComponentValue](./css-parser-algorithms.componentvalue.md)<!-- -->) =&gt; [ComponentValue](./css-parser-algorithms.componentvalue.md) \| void | |
20+
| replaceWith | (componentValue: [ComponentValue](./css-parser-algorithms.componentvalue.md)<!-- -->) =&gt; Array&lt;[ComponentValue](./css-parser-algorithms.componentvalue.md)<!-- -->&gt; \| [ComponentValue](./css-parser-algorithms.componentvalue.md) \| void | |
2121

2222
**Returns:**
2323

packages/css-parser-algorithms/src/util/replace-component-values.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { walk } from './walk';
77
*/
88
export function replaceComponentValues(
99
componentValuesList: Array<Array<ComponentValue>>,
10-
replaceWith: (componentValue: ComponentValue) => ComponentValue | void,
10+
replaceWith: (componentValue: ComponentValue) => Array<ComponentValue> | ComponentValue | void,
1111
) {
1212
for (let i = 0; i < componentValuesList.length; i++) {
1313
const componentValues = componentValuesList[i];
@@ -22,7 +22,11 @@ export function replaceComponentValues(
2222
return;
2323
}
2424

25-
entry.parent.value.splice(index, 1, replacement);
25+
if (Array.isArray(replacement)) {
26+
entry.parent.value.splice(index, 1, ...replacement);
27+
} else {
28+
entry.parent.value.splice(index, 1, replacement);
29+
}
2630
});
2731
}
2832

packages/css-parser-algorithms/test/cases/replacer/0001.mjs

+127-36
Original file line numberDiff line numberDiff line change
@@ -14,50 +14,141 @@ const commentReplacer = (x) => {
1414
}
1515
};
1616

17+
const twoCommentsReplacer = (x) => {
18+
if (isCommentNode(x)) {
19+
return [
20+
new CommentNode([
21+
TokenType.Comment,
22+
'/* generated comment (1) */',
23+
-1,
24+
-1,
25+
undefined,
26+
]),
27+
new CommentNode([
28+
TokenType.Comment,
29+
'/* generated comment (2) */',
30+
-1,
31+
-1,
32+
undefined,
33+
]),
34+
];
35+
}
36+
};
37+
1738
const onParseError = (err) => {
1839
throw err;
1940
};
2041

2142
{
22-
const tokens = tokenize({ css: '/* a comment */' }, { onParseError: onParseError });
23-
const resultAST = parseCommaSeparatedListOfComponentValues(tokens, { onParseError: onParseError });
24-
const afterReplacement = replaceComponentValues(resultAST, commentReplacer);
25-
26-
assert.deepStrictEqual(
27-
stringify(afterReplacement),
28-
'/* generated comment */',
29-
);
30-
}
43+
{
44+
const tokens = tokenize({ css: '/* a comment */' }, { onParseError: onParseError });
45+
const resultAST = parseCommaSeparatedListOfComponentValues(tokens, { onParseError: onParseError });
46+
const afterReplacement = replaceComponentValues(resultAST, commentReplacer);
3147

32-
{
33-
const tokens = tokenize({ css: '(/* a comment */foo ) something else' }, { onParseError: onParseError });
34-
const resultAST = parseCommaSeparatedListOfComponentValues(tokens, { onParseError: onParseError });
35-
const afterReplacement = replaceComponentValues(resultAST, commentReplacer);
36-
37-
assert.deepStrictEqual(
38-
stringify(afterReplacement),
39-
'(/* generated comment */foo ) something else',
40-
);
41-
}
48+
assert.deepStrictEqual(
49+
stringify(afterReplacement),
50+
'/* generated comment */',
51+
);
52+
}
4253

43-
{
44-
const tokens = tokenize({ css: '(/* a comment */foo /* a comment */ /* a comment */) something else /* a comment */' }, { onParseError: onParseError });
45-
const resultAST = parseCommaSeparatedListOfComponentValues(tokens, { onParseError: onParseError });
46-
const afterReplacement = replaceComponentValues(resultAST, commentReplacer);
47-
48-
assert.deepStrictEqual(
49-
stringify(afterReplacement),
50-
'(/* generated comment */foo /* generated comment */ /* generated comment */) something else /* generated comment */',
51-
);
54+
{
55+
const tokens = tokenize({ css: '/* a comment *//* a comment */' }, { onParseError: onParseError });
56+
const resultAST = parseCommaSeparatedListOfComponentValues(tokens, { onParseError: onParseError });
57+
const afterReplacement = replaceComponentValues(resultAST, commentReplacer);
58+
59+
assert.deepStrictEqual(
60+
stringify(afterReplacement),
61+
'/* generated comment *//* generated comment */',
62+
);
63+
}
64+
65+
{
66+
const tokens = tokenize({ css: '(/* a comment */foo ) something else' }, { onParseError: onParseError });
67+
const resultAST = parseCommaSeparatedListOfComponentValues(tokens, { onParseError: onParseError });
68+
const afterReplacement = replaceComponentValues(resultAST, commentReplacer);
69+
70+
assert.deepStrictEqual(
71+
stringify(afterReplacement),
72+
'(/* generated comment */foo ) something else',
73+
);
74+
}
75+
76+
{
77+
const tokens = tokenize({ css: '(/* a comment */foo /* a comment */ /* a comment */) something else /* a comment */' }, { onParseError: onParseError });
78+
const resultAST = parseCommaSeparatedListOfComponentValues(tokens, { onParseError: onParseError });
79+
const afterReplacement = replaceComponentValues(resultAST, commentReplacer);
80+
81+
assert.deepStrictEqual(
82+
stringify(afterReplacement),
83+
'(/* generated comment */foo /* generated comment */ /* generated comment */) something else /* generated comment */',
84+
);
85+
}
86+
87+
{
88+
const tokens = tokenize({ css: '/* foo */, [/* bar */],, ,,(/* baz */ {/* fooz */ calc(/* last one */)})' }, { onParseError: onParseError });
89+
const resultAST = parseCommaSeparatedListOfComponentValues(tokens, { onParseError: onParseError });
90+
const afterReplacement = replaceComponentValues(resultAST, commentReplacer);
91+
92+
assert.deepStrictEqual(
93+
stringify(afterReplacement),
94+
'/* generated comment */, [/* generated comment */],, ,,(/* generated comment */ {/* generated comment */ calc(/* generated comment */)})',
95+
);
96+
}
5297
}
5398

5499
{
55-
const tokens = tokenize({ css: '/* foo */, [/* bar */],, ,,(/* baz */ {/* fooz */ calc(/* last one */)})' }, { onParseError: onParseError });
56-
const resultAST = parseCommaSeparatedListOfComponentValues(tokens, { onParseError: onParseError });
57-
const afterReplacement = replaceComponentValues(resultAST, commentReplacer);
58-
59-
assert.deepStrictEqual(
60-
stringify(afterReplacement),
61-
'/* generated comment */, [/* generated comment */],, ,,(/* generated comment */ {/* generated comment */ calc(/* generated comment */)})',
62-
);
100+
{
101+
const tokens = tokenize({ css: '/* a comment */' }, { onParseError: onParseError });
102+
const resultAST = parseCommaSeparatedListOfComponentValues(tokens, { onParseError: onParseError });
103+
const afterReplacement = replaceComponentValues(resultAST, twoCommentsReplacer);
104+
105+
assert.deepStrictEqual(
106+
stringify(afterReplacement),
107+
'/* generated comment (1) *//* generated comment (2) */',
108+
);
109+
}
110+
111+
{
112+
const tokens = tokenize({ css: '/* a comment *//* a comment */' }, { onParseError: onParseError });
113+
const resultAST = parseCommaSeparatedListOfComponentValues(tokens, { onParseError: onParseError });
114+
const afterReplacement = replaceComponentValues(resultAST, twoCommentsReplacer);
115+
116+
assert.deepStrictEqual(
117+
stringify(afterReplacement),
118+
'/* generated comment (1) *//* generated comment (2) *//* generated comment (1) *//* generated comment (2) */',
119+
);
120+
}
121+
122+
{
123+
const tokens = tokenize({ css: '(/* a comment */foo ) something else' }, { onParseError: onParseError });
124+
const resultAST = parseCommaSeparatedListOfComponentValues(tokens, { onParseError: onParseError });
125+
const afterReplacement = replaceComponentValues(resultAST, twoCommentsReplacer);
126+
127+
assert.deepStrictEqual(
128+
stringify(afterReplacement),
129+
'(/* generated comment (1) *//* generated comment (2) */foo ) something else',
130+
);
131+
}
132+
133+
{
134+
const tokens = tokenize({ css: '(/* a comment */foo /* a comment */ /* a comment */) something else /* a comment */' }, { onParseError: onParseError });
135+
const resultAST = parseCommaSeparatedListOfComponentValues(tokens, { onParseError: onParseError });
136+
const afterReplacement = replaceComponentValues(resultAST, twoCommentsReplacer);
137+
138+
assert.deepStrictEqual(
139+
stringify(afterReplacement),
140+
'(/* generated comment (1) *//* generated comment (2) */foo /* generated comment (1) *//* generated comment (2) */ /* generated comment (1) *//* generated comment (2) */) something else /* generated comment (1) *//* generated comment (2) */',
141+
);
142+
}
143+
144+
{
145+
const tokens = tokenize({ css: '/* foo */, [/* bar */],, ,,(/* baz */ {/* fooz */ calc(/* last one */)})' }, { onParseError: onParseError });
146+
const resultAST = parseCommaSeparatedListOfComponentValues(tokens, { onParseError: onParseError });
147+
const afterReplacement = replaceComponentValues(resultAST, twoCommentsReplacer);
148+
149+
assert.deepStrictEqual(
150+
stringify(afterReplacement),
151+
'/* generated comment (1) *//* generated comment (2) */, [/* generated comment (1) *//* generated comment (2) */],, ,,(/* generated comment (1) *//* generated comment (2) */ {/* generated comment (1) *//* generated comment (2) */ calc(/* generated comment (1) *//* generated comment (2) */)})',
152+
);
153+
}
63154
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
node_modules
2+
package-lock.json
3+
yarn.lock
4+
*.result.css
5+
*.result.css.map
6+
*.result.html
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
v20.2.0
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Changes to PostCSS Light Dark Function
2+
3+
### Unreleased (major)
4+
5+
- Initial version

0 commit comments

Comments
 (0)