Skip to content

Commit 19ffee9

Browse files
committed
Allow selectors replacements and disallow with . or #
1 parent 4fd013f commit 19ffee9

4 files changed

+36
-27
lines changed

src/replaceSymbols.js

+12-10
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1-
import replaceValueSymbols from './replaceValueSymbols.js'
1+
import replaceValueSymbols from "./replaceValueSymbols.js";
22

33
const replaceSymbols = (css, replacements) => {
4-
css.walkDecls(
5-
decl => (decl.value = replaceValueSymbols(decl.value, replacements))
6-
)
7-
css.walkAtRules(
8-
'media',
9-
atRule => (atRule.params = replaceValueSymbols(atRule.params, replacements))
10-
)
11-
}
4+
css.walk(node => {
5+
if (node.type === "decl") {
6+
node.value = replaceValueSymbols(node.value, replacements);
7+
} else if (node.type === "rule") {
8+
node.selector = replaceValueSymbols(node.selector, replacements);
9+
} else if (node.type === "atrule" && node.name === "media") {
10+
node.params = replaceValueSymbols(node.params, replacements);
11+
}
12+
});
13+
};
1214

13-
export default replaceSymbols
15+
export default replaceSymbols;

src/replaceValueSymbols.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const matchValueName = /[$]?[\w-]+[\w.]*/g;
1+
const matchValueName = /[$]?[\w-]+/g;
22

33
const replaceValueSymbols = (value, replacements) => {
44
let matches;

test/replaceSymbols.test.js

+15-6
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,16 @@ test("return empty CSS unchanged", () => {
1212
expect(replace("", {})).toEqual("");
1313
});
1414

15-
test("not change class names", () => {
16-
expect(replace(".foo { color: red }", { foo: "bar" })).toEqual(
17-
".foo { color: red }"
18-
);
19-
});
20-
2115
test("not change property names", () => {
2216
expect(replace(".foo { color: red }", { color: "background" })).toEqual(
2317
".foo { color: red }"
2418
);
2519
});
2620

21+
test("not change non-media at-rules", () => {
22+
expect(replace("@import url;", { url: "otherUrl" })).toEqual("@import url;");
23+
});
24+
2725
test("change declaration values", () => {
2826
expect(replace(".foo { color: red }", { red: "blue" })).toEqual(
2927
".foo { color: blue }"
@@ -37,3 +35,14 @@ test("should change media queries", () => {
3735
})
3836
).toEqual("@media (max-width: 599px) { .foo { color: red } }");
3937
});
38+
39+
test("should replace class names and id in selectors", () => {
40+
expect(
41+
replace(".className1.className2 #id1#id2 { color: red }", {
42+
className1: "__className",
43+
id1: "__id",
44+
"className1.className2": "__badClass",
45+
"id1#id2": "__badId"
46+
})
47+
).toEqual(".__className.className2 #__id#id2 { color: red }");
48+
});

test/replaceValueSymbols.test.js

+8-10
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,12 @@ test("change multiple symbols within values", () => {
2727

2828
test("change complex symbols, if you feel like trolling yourself", () => {
2929
expect(
30-
replace("1px 0.5em 3px $sass-a", {
30+
replace("1px 3px $sass-a", {
3131
"1px": "1rem",
32-
"0.5em": "10px",
3332
"3px": "$sass-b",
3433
"$sass-a": "4px"
3534
})
36-
).toEqual("1rem 10px $sass-b 4px");
35+
).toEqual("1rem $sass-b 4px");
3736
});
3837

3938
test("rewrite custom properties", () => {
@@ -42,12 +41,11 @@ test("rewrite custom properties", () => {
4241

4342
test("not replace half a variable", () => {
4443
expect(
45-
replace("colors.red red.blue", {
46-
red: "green",
47-
blue: "white",
48-
colors: "weights"
44+
replace("colors red", {
45+
re: "green",
46+
color: "weights"
4947
})
50-
).toEqual("colors.red red.blue");
48+
).toEqual("colors red");
5149
});
5250

5351
test("not replace a replacement", () => {
@@ -56,13 +54,13 @@ test("not replace a replacement", () => {
5654
);
5755
});
5856

59-
test("replace selectors identifiers started with . or #", () => {
57+
test("replace selectors identifiers contained . or #", () => {
6058
expect(
6159
replace(".className #id", { className: "otherClassName", id: "otherId" })
6260
).toEqual(".otherClassName #otherId");
6361
});
6462

65-
test("not replace with values started with . or #", () => {
63+
test("not replace with values contained . or #", () => {
6664
expect(
6765
replace(".className #id", {
6866
".className": "otherClassName",

0 commit comments

Comments
 (0)