Skip to content

Commit a8dc366

Browse files
authored
Merge pull request #7 from node-projects/copilot/add-css-rule-support
Enable escaped curly brace test case and preserve empty CSS rules in output
2 parents 8bfe430 + 59c6ed1 commit a8dc366

File tree

5 files changed

+57
-29
lines changed

5 files changed

+57
-29
lines changed

src/stringify/compiler.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -560,9 +560,6 @@ class Compiler {
560560
*/
561561
rule(node: CssRuleAST) {
562562
const decls = node.declarations;
563-
if (!decls.length) {
564-
return '';
565-
}
566563

567564
if (this.compress) {
568565
return (
@@ -574,6 +571,19 @@ class Compiler {
574571
}
575572
const indent = this.indent();
576573

574+
if (!decls.length) {
575+
return (
576+
this.emit(
577+
node.selectors
578+
.map((s) => {
579+
return indent + s;
580+
})
581+
.join(',\n'),
582+
node.position,
583+
) + this.emit(' {}')
584+
);
585+
}
586+
577587
return (
578588
this.emit(
579589
node.selectors

test/cases/escapes/ast.json

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"type": "stylesheet",
33
"stylesheet": {
4+
"source": "input.css",
45
"rules": [
56
{
67
"type": "comment",
@@ -1522,35 +1523,41 @@
15221523
}
15231524
},
15241525
{
1525-
"type": "comment",
1526-
"comment": " css-parse does not yet pass this test ",
1526+
"type": "rule",
1527+
"selectors": [
1528+
"#\\{\\}"
1529+
],
1530+
"declarations": [
1531+
{
1532+
"type": "declaration",
1533+
"property": "background",
1534+
"value": "lime",
1535+
"position": {
1536+
"start": {
1537+
"line": 53,
1538+
"column": 7
1539+
},
1540+
"end": {
1541+
"line": 53,
1542+
"column": 22
1543+
},
1544+
"source": "input.css"
1545+
}
1546+
}
1547+
],
15271548
"position": {
15281549
"start": {
15291550
"line": 53,
15301551
"column": 1
15311552
},
15321553
"end": {
15331554
"line": 53,
1534-
"column": 44
1535-
},
1536-
"source": "input.css"
1537-
}
1538-
},
1539-
{
1540-
"type": "comment",
1541-
"comment": "#\\{\\}{background:lime;}",
1542-
"position": {
1543-
"start": {
1544-
"line": 54,
1545-
"column": 1
1546-
},
1547-
"end": {
1548-
"line": 54,
1549-
"column": 28
1555+
"column": 24
15501556
},
15511557
"source": "input.css"
15521558
}
15531559
}
1554-
]
1560+
],
1561+
"parsingErrors": []
15551562
}
1556-
}
1563+
}

test/cases/escapes/compressed.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/cases/escapes/input.css

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,4 @@ li{background:orange;}
5050
#f\~o\~o{background:lime;}
5151
#f\+o\+o{background:lime;}
5252

53-
/* css-parse does not yet pass this test */
54-
/*#\{\}{background:lime;}*/
53+
#\{\}{background:lime;}

test/cases/escapes/output.css

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,28 @@
44

55
/* will match elements with class=":`(" */
66

7+
.\3A \`\( {}
8+
79
/* will match elements with class="1a2b3c" */
810

11+
.\31 a2b3c {}
12+
913
/* will match the element with id="#fake-id" */
1014

15+
#\#fake-id {}
16+
1117
/* will match the element with id="---" */
1218

19+
#\--- {}
20+
1321
/* will match the element with id="-a-b-c-" */
1422

23+
#-a-b-c- {}
24+
1525
/* will match the element with id="©" */
1626

27+
#© {}
28+
1729
/* More tests from http://mathiasbynens.be/demo/html5-id */
1830

1931
html {
@@ -161,6 +173,6 @@ li {
161173
background: lime;
162174
}
163175

164-
/* css-parse does not yet pass this test */
165-
166-
/*#\{\}{background:lime;}*/
176+
#\{\} {
177+
background: lime;
178+
}

0 commit comments

Comments
 (0)