Skip to content

Commit d8ef152

Browse files
committed
incorrectly expand css nesting rules #38
1 parent 9e832ba commit d8ef152

File tree

9 files changed

+100
-13
lines changed

9 files changed

+100
-13
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## V0.5.3
4+
5+
- [x] incorrectly expand css nesting rules
6+
37
## V0.5.1
48

59
- [x] failed to flatten @import when using url() syntax

dist/index-umd-web.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1481,7 +1481,7 @@
14811481
return val / 4.5;
14821482
}
14831483
return sign * (Math.pow((abs + α - 1) / α, 1 / 0.45));
1484-
}).concat(a == null || a == 1 ? [] : [a]);
1484+
}).concat([] );
14851485
}
14861486
function lrec20202rec2020(r, g, b, a) {
14871487
// convert an array of linear-light rec2020 RGB in the range 0.0-1.0
@@ -7264,7 +7264,8 @@
72647264
for (const t of walkValues(tokens)) {
72657265
if (t.value.typ == exports.EnumToken.LiteralTokenType) {
72667266
if (t.value.val == '&') {
7267-
t.value.val = replace;
7267+
const rule = splitRule(replace);
7268+
t.value.val = rule.length > 1 ? ':is(' + replace + ')' : replace;
72687269
}
72697270
else if (t.value.val.length > 1 && t.value.val.charAt(0) == '&') {
72707271
t.value.val = replaceCompoundLiteral(t.value.val, replace);

dist/index.cjs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1479,7 +1479,7 @@ function rec20202lrec2020(r, g, b, a) {
14791479
return val / 4.5;
14801480
}
14811481
return sign * (Math.pow((abs + α - 1) / α, 1 / 0.45));
1482-
}).concat(a == null || a == 1 ? [] : [a]);
1482+
}).concat([] );
14831483
}
14841484
function lrec20202rec2020(r, g, b, a) {
14851485
// convert an array of linear-light rec2020 RGB in the range 0.0-1.0
@@ -7262,7 +7262,8 @@ function replaceCompound(input, replace) {
72627262
for (const t of walkValues(tokens)) {
72637263
if (t.value.typ == exports.EnumToken.LiteralTokenType) {
72647264
if (t.value.val == '&') {
7265-
t.value.val = replace;
7265+
const rule = splitRule(replace);
7266+
t.value.val = rule.length > 1 ? ':is(' + replace + ')' : replace;
72667267
}
72677268
else if (t.value.val.length > 1 && t.value.val.charAt(0) == '&') {
72687269
t.value.val = replaceCompoundLiteral(t.value.val, replace);

dist/lib/ast/expand.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,8 @@ function replaceCompound(input, replace) {
121121
for (const t of walkValues(tokens)) {
122122
if (t.value.typ == EnumToken.LiteralTokenType) {
123123
if (t.value.val == '&') {
124-
t.value.val = replace;
124+
const rule = splitRule(replace);
125+
t.value.val = rule.length > 1 ? ':is(' + replace + ')' : replace;
125126
}
126127
else if (t.value.val.length > 1 && t.value.val.charAt(0) == '&') {
127128
t.value.val = replaceCompoundLiteral(t.value.val, replace);

dist/lib/renderer/color/rec2020.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ function rec20202lrec2020(r, g, b, a) {
2828
return val / 4.5;
2929
}
3030
return sign * (Math.pow((abs + α - 1) / α, 1 / 0.45));
31-
}).concat(a == null || a == 1 ? [] : [a]);
31+
}).concat([] );
3232
}
3333
function lrec20202rec2020(r, g, b, a) {
3434
// convert an array of linear-light rec2020 RGB in the range 0.0-1.0

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@tbela99/css-parser",
33
"description": "CSS parser for node and the browser",
4-
"version": "0.5.2",
4+
"version": "0.5.3",
55
"exports": {
66
".": "./dist/node/index.js",
77
"./umd": "./dist/index-umd-web.js",

src/lib/ast/expand.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,10 @@ function expandRule(node: AstRule): Array<AstRule | AstAtRule> {
8181

8282
if (!rule.sel.includes('&')) {
8383

84-
const selRule = splitRule(rule.sel);
84+
const selRule: string[][] = splitRule(rule.sel);
8585
selRule.forEach(arr => combinators.includes(arr[0].charAt(0)) ? arr.unshift(ast.sel) : arr.unshift(ast.sel, ' '));
8686

87-
rule.sel = selRule.reduce((acc, curr) => {
87+
rule.sel = selRule.reduce((acc: string[], curr: string[]) => {
8888

8989
acc.push(curr.join(''));
9090

@@ -102,7 +102,7 @@ function expandRule(node: AstRule): Array<AstRule | AstAtRule> {
102102

103103

104104
let astAtRule: AstAtRule = <AstAtRule>ast.chi[i];
105-
const values = <Array<AstRule | AstAtRule>>[];
105+
const values: Array<AstRule | AstAtRule> = <Array<AstRule | AstAtRule>>[];
106106

107107
if (astAtRule.nam == 'scope') {
108108

@@ -171,7 +171,9 @@ export function replaceCompound(input: string, replace: string) {
171171

172172
if (t.value.val == '&') {
173173

174-
t.value.val = replace;
174+
const rule = splitRule(replace);
175+
176+
t.value.val = rule.length > 1 ? ':is(' + replace + ')' : replace;
175177
} else if (t.value.val.length > 1 && t.value.val.charAt(0) == '&') {
176178

177179
t.value.val = replaceCompoundLiteral(t.value.val, replace);

test/specs/code/nesting.js

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,6 @@ export function run(describe, expect, transform, parse, render, dirname, readFil
333333
}).then((result) => expect(result.code).equals(`.nav-pills{.nav-link.active,.show>.nav-link{color:var(--bs-nav-pills-link-active-color);background-color:var(--bs-nav-pills-link-active-bg)}}`));
334334
});
335335

336-
337336
it('merge selectors #16', function () {
338337
const file = `
339338
@@ -356,6 +355,86 @@ a {
356355
}).then(result => expect(result.code).equals(`a{color:rgb(var(--bs-link-color-rgb) var(--bs-link-opacity,1));text-decoration:underline;:hover,& span{--bs-link-color-rgb:var(--bs-link-hover-color-rgb)}}`));
357356
});
358357

358+
it('merge selectors #17', function () {
359+
const file = `
360+
table.colortable {
361+
width: 100%;
362+
text-shadow: none;
363+
border-collapse: collapse;
364+
& td {
365+
text-align: center;
366+
&.c {
367+
text-transform: uppercase;
368+
background: #ff0
369+
}
370+
}
371+
& th {
372+
text-align: center;
373+
color: green;
374+
font-weight: 400;
375+
padding: 2px 3px
376+
}
377+
& td,& th {
378+
border: 1px solid #d9dadd;
379+
padding: 5px
380+
}
381+
}
382+
.foo {
383+
color: blue;
384+
& {
385+
padding: 2ch;
386+
color: blue;
387+
&& {
388+
padding: 2ch
389+
}
390+
}
391+
}
392+
.error,#404 {
393+
&:hover>.baz {
394+
color: red
395+
}
396+
}
397+
`;
398+
return transform(file, {
399+
expandNestingRules: true,
400+
minify: false
401+
}).then(result => expect(result.code).equals(`table.colortable {
402+
width: 100%;
403+
text-shadow: none;
404+
border-collapse: collapse
405+
}
406+
table.colortable td {
407+
text-align: center
408+
}
409+
table.colortable td.c {
410+
text-transform: uppercase;
411+
background: #ff0
412+
}
413+
table.colortable th {
414+
text-align: center;
415+
color: green;
416+
font-weight: 400;
417+
padding: 2px 3px
418+
}
419+
table.colortable td,table.colortable th {
420+
border: 1px solid #d9dadd;
421+
padding: 5px
422+
}
423+
.foo {
424+
color: blue
425+
}
426+
.foo {
427+
padding: 2ch;
428+
color: blue
429+
}
430+
.foo.foo {
431+
padding: 2ch
432+
}
433+
:is(.error,#404):hover>.baz {
434+
color: red
435+
}`));
436+
});
437+
359438
// see https://www.w3.org/TR/css-nesting-1/#conditionals
360439
/*
361440
.header {

tools/shorthand.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ function createProperties(data: ShorthandPropertyType) {
1313
return {
1414
[data.shorthand]: {...data}, ...data.properties.reduce((acc, property: string) => {
1515

16-
1716
return Object.assign(acc, {
1817
[property]: {
1918
map,

0 commit comments

Comments
 (0)