Skip to content
Permalink
Browse files

fix: use a safer regex for finding @values (#552)

"\bval\b" will match "-val" which is a problem for folks using hyphens to separate classes. Now using "^val$" which when combined with postcss-value-parser should match almost everything the old one did but w/o being too greedy.

Fixes #548
  • Loading branch information...
tivac committed Jan 24, 2019
1 parent 443ce55 commit c6a684c03e891323fd67488b4a80046a0290461a
@@ -36,7 +36,7 @@ module.exports = (css, { opts, messages }) => {
const matchRegex = new RegExp(
Object.keys(values)
.sort((a, b) => b.length - a.length)
.map((v) => `\\b${escape(v)}\\b`)
.map((v) => `^${escape(v)}$`)
.join("|"),
"g"
);
@@ -102,3 +102,30 @@ exports[`/processor.js values should support value replacement in :external(...)
}
"
`;

exports[`/processor.js values shouldn't replace values unless they're safe 1`] = `
"/* values.css */
.a {
color: foo-a;
color: foo(red);
color: foo_a;
color: fooa;
color: foo, red;
color: foo, red, woo;
width: foopx;
color:red;
color:foo-a;
color: red;
}

@media red { }
@media red, b {}
@media foo-a, b {}
@media foo-a { }
@media (min-width: red) { }
@media (min-width: foo-a) { }
@media not red {}
@media not (red) {}
@media not foo-a {}
@media not (foo-a) {}"
`;
@@ -38,6 +38,45 @@ describe("/processor.js", () => {
);
}
});

it("shouldn't replace values unless they're safe", async () => {
await processor.string(
"./values.css",
dedent(`
@value a: red;
@value c: foo-a;
@value d:a;
.a {
color: foo-a;
color: foo(a);
color: foo_a;
color: fooa;
color: foo, a;
color: foo, a, woo;
width: foopx;
color:a;
color:foo-a;
color: d;
}
@media a { }
@media a, b {}
@media foo-a, b {}
@media foo-a { }
@media (min-width: a) { }
@media (min-width: foo-a) { }
@media not a {}
@media not (a) {}
@media not foo-a {}
@media not (foo-a) {}
`)
);

const { css } = await processor.output();

expect(css).toMatchSnapshot();
});

it("should support simple values", async () => {
await processor.string(

0 comments on commit c6a684c

Please sign in to comment.
You can’t perform that action at this time.