Skip to content

Commit 4fd013f

Browse files
committed
Disallow . and # replacements and allow class name and id replacement
1 parent 2411980 commit 4fd013f

File tree

2 files changed

+26
-12
lines changed

2 files changed

+26
-12
lines changed

src/replaceValueSymbols.js

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

33
const replaceValueSymbols = (value, replacements) => {
4-
let matches
4+
let matches;
55
while ((matches = matchValueName.exec(value))) {
6-
const replacement = replacements[matches[0]]
6+
const replacement = replacements[matches[0]];
77
if (replacement) {
88
value =
99
value.slice(0, matches.index) +
1010
replacement +
11-
value.slice(matchValueName.lastIndex)
12-
matchValueName.lastIndex -= matches[0].length - replacement.length
11+
value.slice(matchValueName.lastIndex);
12+
matchValueName.lastIndex -= matches[0].length - replacement.length;
1313
}
1414
}
15-
return value
16-
}
15+
return value;
16+
};
1717

18-
export default replaceValueSymbols
18+
export default replaceValueSymbols;

test/replaceValueSymbols.test.js

+18-4
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,13 @@ 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 #f00", {
30+
replace("1px 0.5em 3px $sass-a", {
3131
"1px": "1rem",
3232
"0.5em": "10px",
3333
"3px": "$sass-b",
34-
"$sass-a": "4px",
35-
"#f00": "green"
34+
"$sass-a": "4px"
3635
})
37-
).toEqual("1rem 10px $sass-b 4px green");
36+
).toEqual("1rem 10px $sass-b 4px");
3837
});
3938

4039
test("rewrite custom properties", () => {
@@ -56,3 +55,18 @@ test("not replace a replacement", () => {
5655
"green blue"
5756
);
5857
});
58+
59+
test("replace selectors identifiers started with . or #", () => {
60+
expect(
61+
replace(".className #id", { className: "otherClassName", id: "otherId" })
62+
).toEqual(".otherClassName #otherId");
63+
});
64+
65+
test("not replace with values started with . or #", () => {
66+
expect(
67+
replace(".className #id", {
68+
".className": "otherClassName",
69+
"#id": "otherId"
70+
})
71+
).toEqual(".className #id");
72+
});

0 commit comments

Comments
 (0)