Skip to content

Commit 2af6687

Browse files
committed
fix(postcss-reduce-idents): fix line names grid templates
Handle grid and column names enclosed in square brackets. Cover both the case where the brackets contain a single grid line name and the case where the brackets contain multiple separate grid line names. Fix #529 (again)
1 parent eb9a9a1 commit 2af6687

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

packages/postcss-reduce-idents/src/lib/grid-template.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ const RESERVED_KEYWORDS = new Set([
1414
const gridTemplateProperties = new Set([
1515
'grid-template',
1616
'grid-template-areas',
17+
'grid-template-columns',
18+
'grid-template-rows',
1719
]);
1820

1921
const gridChildProperties = new Set([
@@ -53,6 +55,20 @@ module.exports = function () {
5355
}
5456
});
5557
}
58+
/* handle gridline name lists like [name1 name2] */
59+
if (child.type === 'word') {
60+
const word = child.value;
61+
if (word.startsWith('[') && word.endsWith(']')) {
62+
const gridLine = word.slice(1, -1);
63+
addToCache(gridLine, encoder, cache);
64+
} else if (word.startsWith('[')) {
65+
const gridLine = word.slice(1);
66+
addToCache(gridLine, encoder, cache);
67+
} else if (word.endsWith(']')) {
68+
const gridLine = word.slice(0, -1);
69+
addToCache(gridLine, encoder, cache);
70+
}
71+
}
5672
});
5773

5874
declCache.push(node);
@@ -79,6 +95,30 @@ module.exports = function () {
7995
if (word in cache) {
8096
node.value = node.value.replace(word, cache[word].ident);
8197
}
98+
/* replace gridline names inside lists like [name] */
99+
if (
100+
word.startsWith('[') &&
101+
word.endsWith(']') &&
102+
word.slice(1, -1) in cache
103+
) {
104+
const gridLine = word.slice(1, -1);
105+
node.value = node.value.replace(
106+
gridLine,
107+
cache[gridLine].ident
108+
);
109+
} else if (word.startsWith('[') && word.slice(1) in cache) {
110+
const gridLine = word.slice(1);
111+
node.value = node.value.replace(
112+
gridLine,
113+
cache[gridLine].ident
114+
);
115+
} else if (word.endsWith(']') && word.slice(0, -1) in cache) {
116+
const gridLine = word.slice(0, -1);
117+
node.value = node.value.replace(
118+
gridLine,
119+
cache[gridLine].ident
120+
);
121+
}
82122
});
83123
node.value = node.value.replace(/\s+/g, ' '); // merge white-spaces
84124
}

packages/postcss-reduce-idents/test/index.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,49 @@ test(
408408
)
409409
);
410410

411+
test(
412+
'should rename grid-template-columns',
413+
processCSS(
414+
`.grid {
415+
display: grid;
416+
grid-template-columns:
417+
[kw] 2.5em
418+
[day] 3em
419+
[description] 10em;
420+
}
421+
.grid > .kw {
422+
grid-column: kw;
423+
}
424+
.grid > .day {
425+
grid-column: day;
426+
}
427+
.grid > .description {
428+
grid-column: description;
429+
}`,
430+
`.grid {
431+
display: grid;
432+
grid-template-columns:
433+
[a] 2.5em [b] 3em [c] 10em;
434+
}
435+
.grid > .kw {
436+
grid-column: a;
437+
}
438+
.grid > .day {
439+
grid-column: b;
440+
}
441+
.grid > .description {
442+
grid-column: c;
443+
}`
444+
)
445+
);
446+
447+
test(
448+
'should rename a list of grid-template-rows',
449+
processCSS(
450+
'.grid {grid-template-rows: [linename1 linename2] 100px;} .a { grid-row: linename1;}',
451+
'.grid {grid-template-rows: [a b] 100px;} .a { grid-row: a;}'
452+
)
453+
);
411454
test(
412455
'should not rename uppercase reserved keywords in grid-row, grid-row-start and grid-row-end',
413456
processCSS(

0 commit comments

Comments
 (0)