Skip to content

Commit 97e44b7

Browse files
committedJan 17, 2018
Do not replace url() function content, closes #22
1 parent 015f7ca commit 97e44b7

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed
 

‎index.js

+18-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const postcss = require('postcss');
22
const path = require('path');
33
const promisify = require('es6-promisify');
44
const { CachedInputFileSystem, NodeJsInputFileSystem, ResolverFactory } = require('enhanced-resolve');
5-
const { replaceValueSymbols } = require('icss-utils');
5+
const valueParser = require('postcss-value-parser');
66

77
const matchImports = /^(.+?|\([\s\S]+?\))\s+from\s+("[^"]*"|'[^']*'|[\w-]+)$/;
88
const matchValueDefinition = /(?:\s+|^)([\w-]+)(:?\s+)(.+?)(\s*)$/g;
@@ -16,6 +16,23 @@ const INNER_PLUGIN = 'postcss-modules-values-replace-bind';
1616
const nodeFs = new CachedInputFileSystem(new NodeJsInputFileSystem(), 4000);
1717
const concordContext = {};
1818

19+
const replaceValueSymbols = (valueString, replacements) => {
20+
const value = valueParser(valueString);
21+
22+
value.walk((node) => {
23+
if (node.type !== 'word') return;
24+
25+
const replacement = replacements[node.value];
26+
27+
if (replacement != null) {
28+
// eslint-disable-next-line no-param-reassign
29+
node.value = replacement;
30+
}
31+
});
32+
33+
return value.toString();
34+
};
35+
1936
const getDefinition = (atRule, existingDefinitions, requiredDefinitions) => {
2037
let matches;
2138
const definition = {};

‎package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
"dependencies": {
2222
"enhanced-resolve": "^3.1.0",
2323
"es6-promisify": "^5.0.0",
24-
"icss-utils": "^3.0.1",
25-
"postcss": "^6.0.1"
24+
"postcss": "^6.0.1",
25+
"postcss-value-parser": "^3.3.0"
2626
},
2727
"devDependencies": {
2828
"ava": "^0.24.0",

‎test.js

+16
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,14 @@ test('shouldn\'t replace selector', async (t) => {
6464
);
6565
});
6666

67+
test('shouldn\'t replace inside url', async (t) => {
68+
await run(
69+
t,
70+
'@value blue red; .blue { background-image: url(blue.png); }',
71+
'@value blue red; .blue { background-image: url(blue.png); }',
72+
);
73+
});
74+
6775
test('should replace two constants with same name within the file and the latter should win', async (t) => {
6876
await run(
6977
t,
@@ -167,6 +175,14 @@ test('should allow transitive values', async (t) => {
167175
);
168176
});
169177

178+
test('shouldn\'t allow transitive values in urls', async (t) => {
179+
await run(
180+
t,
181+
'@value aaa: red;\n@value bbb: url(aaa.png); \n.a { background-image: url(aaa.png); }',
182+
'@value aaa: red;\n@value bbb: url(aaa.png); \n.a { background-image: url(aaa.png); }',
183+
);
184+
});
185+
170186
test('should allow transitive values within calc', async (t) => {
171187
await run(
172188
t,

0 commit comments

Comments
 (0)
Please sign in to comment.