Skip to content

Commit 37df2ae

Browse files
committed
Support specifying processors during processing CSS declarations
1 parent f2e3c96 commit 37df2ae

File tree

6 files changed

+707
-4
lines changed

6 files changed

+707
-4
lines changed

README.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,7 @@ All the options are optional, and a default value will be used if any of them is
382382
| stringMap | `PluginStringMap[]` | Check below | An array of strings maps that will be used to make the replacements of the declarations' URLs and to match the names of the rules if `processRuleNames` is `true` |
383383
| greedy | `boolean` | `false` | When greedy is `true`, the matches of `stringMap` will not take into account word boundaries |
384384
| aliases | `Record<string, string>` | `{}` | A strings map to treat some declarations as others |
385+
| processDeclarationPlugins | `Array<{name: string, priority: number, processors: []}>` | `[]` | Plugins applied when processing CSS declarations |
385386

386387
---
387388

@@ -1447,6 +1448,62 @@ const options = {
14471448

14481449
---
14491450

1451+
#### processDeclarationPlugins
1452+
1453+
<details><summary>Expand</summary>
1454+
<p>
1455+
1456+
Sometimes, we can register some plugins when processing CSS declarations via the `processDeclarationPlugins` options, which is helpful when we need to avoid unexpected flipping situations like `background-position`.
1457+
1458+
##### input
1459+
1460+
```css
1461+
.test {
1462+
background-position: 0 100%;
1463+
}
1464+
```
1465+
1466+
##### Convert `0` to `100%` (default)
1467+
1468+
##### output
1469+
1470+
```css
1471+
.test {
1472+
background-position: 100% 100%;
1473+
}
1474+
```
1475+
1476+
##### Set a plugin to avoid flipping
1477+
1478+
```javascript
1479+
const options = {
1480+
processDeclarationPlugins: [
1481+
{
1482+
name: 'avoid-flipping-background',
1483+
priority: 99, // above the core RTLCSS plugin which has a priority value of 100
1484+
processors: [{
1485+
expr: /(background|object)(-position(-x)?|-image)?$/i,
1486+
action: (prop, value) => ({prop, value})}
1487+
]
1488+
}
1489+
]
1490+
};
1491+
```
1492+
1493+
##### output
1494+
1495+
```css
1496+
.test {
1497+
background-position: 0 100%;
1498+
}
1499+
```
1500+
1501+
</p>
1502+
1503+
</details>
1504+
1505+
---
1506+
14501507
Control Directives
14511508
---
14521509

src/@types/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ export interface PluginStringMap {
3939
replace: strings;
4040
}
4141

42+
export interface PluginProcessor {
43+
expr: RegExp;
44+
action: (prop: string, value: string, context: object) => object;
45+
}
46+
4247
export type PrefixSelectorTransformer = (prefix: string, selector: string) => string | void;
4348

4449
export interface PluginOptions {
@@ -58,6 +63,7 @@ export interface PluginOptions {
5863
stringMap?: PluginStringMap[];
5964
greedy?: boolean;
6065
aliases?: Record<string, string>;
66+
processDeclarationPlugins?: Array<{ name: string, priority: number, processors: PluginProcessor[] }>;
6167
}
6268

6369
export interface PluginOptionsNormalized extends Omit<Required<PluginOptions>, 'stringMap' | 'prefixSelectorTransformer'> {

src/data/store.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,8 @@ const defaultOptions = (): PluginOptionsNormalized => ({
143143
useCalc: false,
144144
stringMap: getRTLCSSStringMap(defaultStringMap),
145145
greedy: false,
146-
aliases: {}
146+
aliases: {},
147+
processDeclarationPlugins: []
147148
});
148149

149150
const store: Store = {
@@ -217,7 +218,10 @@ const normalizeOptions = (options: PluginOptions): PluginOptionsNormalized => {
217218
if (options.aliases && isObjectWithStringKeys(options.aliases)) {
218219
returnOptions.aliases = options.aliases;
219220
}
220-
return returnOptions;
221+
return {
222+
...returnOptions,
223+
processDeclarationPlugins: (options.processDeclarationPlugins || []).map(plugin => ({...plugin, directives: {control: {}, value: []}}))
224+
};
221225
};
222226

223227
const initStore = (options: PluginOptions): void => {

src/parsers/declarations.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ export const parseDeclarations = (
5959
useCalc,
6060
stringMap,
6161
greedy,
62-
aliases
62+
aliases,
63+
processDeclarationPlugins
6364
} = store.options;
6465

6566
const deleteDeclarations: Declaration[] = [];
@@ -160,7 +161,7 @@ export const parseDeclarations = (
160161
stringMap,
161162
greedy,
162163
aliases
163-
});
164+
}, processDeclarationPlugins);
164165

165166
/* the source could be undefined in certain cases but not during the tests */
166167
/* istanbul ignore next */

0 commit comments

Comments
 (0)