Skip to content

Commit f8c2077

Browse files
authored
Merge pull request princed#56 from cmrn/fix-definition-regex
2 parents 276a83e + 77c71c5 commit f8c2077

File tree

2 files changed

+21
-18
lines changed

2 files changed

+21
-18
lines changed

index.js

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const { urlToRequest } = require('loader-utils');
88
const ICSSUtils = require('icss-utils');
99

1010
const matchImports = /^(.+?|\([\s\S]+?\))\s+from\s+("[^"]*"|'[^']*'|[\w-]+)$/;
11-
const matchValueDefinition = /(?:\s+|^)([\w-]+)(:?\s+)(.+?)(\s*)$/g;
11+
const matchValueDefinition = /(?:\s+|^)([\w-]+)(:?\s+)(.+?)(\s*)$/;
1212
const matchImport = /^([\w-]+)(?:\s+as\s+([\w-]+))?/;
1313
const matchPath = /"[^"]*"|'[^']*'/;
1414

@@ -38,22 +38,15 @@ const replaceValueSymbols = (valueString, replacements) => {
3838
};
3939

4040
const getDefinition = (atRule, existingDefinitions, requiredDefinitions) => {
41-
let matches;
42-
const definition = {};
41+
const [/* match */, name, middle, value, end] = matchValueDefinition.exec(atRule.params);
42+
const valueWithReplacements = replaceValueSymbols(value, existingDefinitions);
4343

44-
// eslint-disable-next-line no-cond-assign
45-
while (matches = matchValueDefinition.exec(atRule.params)) {
46-
const [/* match */, requiredName, middle, value, end] = matches;
47-
// Add to the definitions, knowing that values can refer to each other
48-
definition[requiredName] = replaceValueSymbols(value, existingDefinitions);
49-
50-
if (!requiredDefinitions) {
51-
// eslint-disable-next-line no-param-reassign
52-
atRule.params = requiredName + middle + definition[requiredName] + end;
53-
}
44+
if (!requiredDefinitions) {
45+
// eslint-disable-next-line no-param-reassign
46+
atRule.params = name + middle + valueWithReplacements + end;
5447
}
5548

56-
return definition;
49+
return { [name]: valueWithReplacements };
5750
};
5851

5952
const getImports = (aliases) => {

index.test.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,16 @@ test('should allow transitive values within calc without spaces', async (t) => {
255255
);
256256
});
257257

258+
test('should allow transitive values containing spaces, like CSS relative colors', async (t) => {
259+
await run(
260+
t,
261+
'@value mixedColor: color-mix(in hsl, hsl(200 50 80), coral 80%);\n'
262+
+ '@value mixedColorA90: hsl(from mixedColor h s l / 90%);',
263+
'@value mixedColor: color-mix(in hsl, hsl(200 50 80), coral 80%);\n'
264+
+ '@value mixedColorA90: hsl(from color-mix(in hsl, hsl(200 50 80), coral 80%) h s l / 90%);',
265+
);
266+
});
267+
258268
test('should replace inside custom properties', async (t) => {
259269
await run(
260270
t,
@@ -300,10 +310,10 @@ test('should allow custom-property-style names', async (t) => {
300310
test('should allow all colour types', async (t) => {
301311
await run(
302312
t,
303-
'@value named: red; @value hex3char #0f0; @value hex6char #00ff00; @value rgba rgba(34, 12, 64, 0.3); @value hsla hsla(220, 13.0%, 18.0%, 1);\n'
304-
+ '.foo { color: named; background-color: hex3char; border-top-color: hex6char; border-bottom-color: rgba; outline-color: hsla; }',
305-
'@value named: red; @value hex3char #0f0; @value hex6char #00ff00; @value rgba rgba(34, 12, 64, 0.3); @value hsla hsla(220, 13.0%, 18.0%, 1);\n'
306-
+ '.foo { color: red; background-color: #0f0; border-top-color: #00ff00; border-bottom-color: rgba(34, 12, 64, 0.3); outline-color: hsla(220, 13.0%, 18.0%, 1); }',
313+
'@value named: red; @value hex3char #0f0; @value hex6char #00ff00; @value rgba rgba(34, 12, 64, 0.3); @value hsla hsla(220, 13.0%, 18.0%, 1); @value hsl hsl(264 100% 62% / 70%);\n'
314+
+ '.foo { color: named; background-color: hex3char; border-top-color: hex6char; border-bottom-color: rgba; outline-color: hsla; border-left-color: hsl; }',
315+
'@value named: red; @value hex3char #0f0; @value hex6char #00ff00; @value rgba rgba(34, 12, 64, 0.3); @value hsla hsla(220, 13.0%, 18.0%, 1); @value hsl hsl(264 100% 62% / 70%);\n'
316+
+ '.foo { color: red; background-color: #0f0; border-top-color: #00ff00; border-bottom-color: rgba(34, 12, 64, 0.3); outline-color: hsla(220, 13.0%, 18.0%, 1); border-left-color: hsl(264 100% 62% / 70%); }',
307317
);
308318
});
309319

0 commit comments

Comments
 (0)