Skip to content

Commit f7ab4ba

Browse files
authored
fix: the css parser should handle empty nodes (#8503)
1 parent bd9828a commit f7ab4ba

2 files changed

Lines changed: 16 additions & 2 deletions

File tree

nativescript-core/css/css-tree-parser.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ function transformAst(node, css, type = null) {
3939
return {
4040
type: "stylesheet",
4141
stylesheet: {
42-
rules: node.children.map(child => transformAst(child, css)).toArray(),
42+
rules: node.children.map(child => transformAst(child, css)).filter(child => child !== null).toArray(),
4343
parsingErrors: []
4444
}
4545
};
@@ -78,7 +78,7 @@ function transformAst(node, css, type = null) {
7878
}
7979

8080
if (node.type === "Block") {
81-
return node.children.map(child => transformAst(child, css, type)).toArray();
81+
return node.children.map(child => transformAst(child, css, type)).filter(child => child !== null).toArray();
8282
}
8383

8484
if (node.type === "Rule") {
@@ -116,6 +116,10 @@ function transformAst(node, css, type = null) {
116116
};
117117
}
118118

119+
if (node.type === "Raw") {
120+
return null;
121+
}
122+
119123
throw Error(`Unknown node type ${node.type}`);
120124
}
121125

unit-tests/css-tree-parser/css-tree-parser.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,16 @@ describe("css-tree parser compatible with rework ", () => {
1111
assert.deepEqual(cssTreeAST, reworkAST);
1212
});
1313

14+
it("empty rule", () => {
15+
const css = `.test {
16+
color: red;
17+
;
18+
}`;
19+
const reworkAST = reworkCssParse(css, { source: "file.css" });
20+
const cssTreeAST = cssTreeParse(css, "file.css");
21+
assert.deepEqual(cssTreeAST, reworkAST);
22+
});
23+
1424
it("@keyframes", () => {
1525
const testCase = ".test { animation-name: test; } @keyframes test { from { background-color: red; } to { background-color: blue; } } .test { color: red; }";
1626
const reworkAST = reworkCssParse(testCase, { source: "file.css" });

0 commit comments

Comments
 (0)