fix: the css parser should handle empty nodes (#8503)

This commit is contained in:
Darin Dimitrov
2020-04-06 08:53:12 +03:00
committed by GitHub
parent bd9828a036
commit f7ab4ba3c9
2 changed files with 16 additions and 2 deletions

View File

@ -39,7 +39,7 @@ function transformAst(node, css, type = null) {
return { return {
type: "stylesheet", type: "stylesheet",
stylesheet: { stylesheet: {
rules: node.children.map(child => transformAst(child, css)).toArray(), rules: node.children.map(child => transformAst(child, css)).filter(child => child !== null).toArray(),
parsingErrors: [] parsingErrors: []
} }
}; };
@ -78,7 +78,7 @@ function transformAst(node, css, type = null) {
} }
if (node.type === "Block") { if (node.type === "Block") {
return node.children.map(child => transformAst(child, css, type)).toArray(); return node.children.map(child => transformAst(child, css, type)).filter(child => child !== null).toArray();
} }
if (node.type === "Rule") { if (node.type === "Rule") {
@ -116,6 +116,10 @@ function transformAst(node, css, type = null) {
}; };
} }
if (node.type === "Raw") {
return null;
}
throw Error(`Unknown node type ${node.type}`); throw Error(`Unknown node type ${node.type}`);
} }

View File

@ -11,6 +11,16 @@ describe("css-tree parser compatible with rework ", () => {
assert.deepEqual(cssTreeAST, reworkAST); assert.deepEqual(cssTreeAST, reworkAST);
}); });
it("empty rule", () => {
const css = `.test {
color: red;
;
}`;
const reworkAST = reworkCssParse(css, { source: "file.css" });
const cssTreeAST = cssTreeParse(css, "file.css");
assert.deepEqual(cssTreeAST, reworkAST);
});
it("@keyframes", () => { it("@keyframes", () => {
const testCase = ".test { animation-name: test; } @keyframes test { from { background-color: red; } to { background-color: blue; } } .test { color: red; }"; const testCase = ".test { animation-name: test; } @keyframes test { from { background-color: red; } to { background-color: blue; } } .test { color: red; }";
const reworkAST = reworkCssParse(testCase, { source: "file.css" }); const reworkAST = reworkCssParse(testCase, { source: "file.css" });