From b84d3ff4566b0ff5dd36662ca534eb9e4ff20a26 Mon Sep 17 00:00:00 2001 From: Danny Lin Date: Sat, 6 Apr 2024 16:15:22 +0800 Subject: [PATCH] Fix isValidInContext to allow a declaration with a standalone {}-block - ref: https://drafts.csswg.org/css-syntax/#consume-declaration --- parse-css.js | 11 ++++++--- tests.js | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 4 deletions(-) diff --git a/parse-css.js b/parse-css.js index b574ee4..e6674cc 100644 --- a/parse-css.js +++ b/parse-css.js @@ -1135,13 +1135,16 @@ function isValidInContext(construct, context) { return true; } - // Exclude properties that ended up with a {}-block - // in their value, unless they're custom. + // Exclude properties that ended up with a {}-block plus + // a non-whitespace component in their value, unless they're custom. if(construct.type == "DECLARATION") { if(construct.name.slice(0, 2) == "--") return true; - for(const val of construct.value) { - if(val.type == "BLOCK" && val.name == "{") return false; + + const block = construct.value.find(t => t.type === "BLOCK" && t.name === "{"); + if (block && construct.value.some(t => t.type !== "WHITESPACE" && t !== block)) { + return false; } + return true; } } diff --git a/tests.js b/tests.js index 239561b..5231d99 100644 --- a/tests.js +++ b/tests.js @@ -3600,6 +3600,75 @@ return [ "important": false } }, + { + "parser": "parseADeclaration", + "css": "foo:{}", + "expected": { + "type": "DECLARATION", + "name": "foo", + "value": [ + { + "type": "BLOCK", + "name": "{", + "value": [] + } + ], + "important": false + } + }, + { + "parser": "parseADeclaration", + "css": "foo: {}", + "expected": { + "type": "DECLARATION", + "name": "foo", + "value": [ + { + "type": "BLOCK", + "name": "{", + "value": [] + } + ], + "important": false + } + }, + { + "parser": "parseADeclaration", + "css": "foo:{} ", + "expected": { + "type": "DECLARATION", + "name": "foo", + "value": [ + { + "type": "BLOCK", + "name": "{", + "value": [] + } + ], + "important": false + } + }, + { + "parser": "parseADeclaration", + "css": "foo:bar{}", + "expectedThrow": { + "name": "SyntaxError" + } + }, + { + "parser": "parseADeclaration", + "css": "foo:{}bar", + "expectedThrow": { + "name": "SyntaxError" + } + }, + { + "parser": "parseADeclaration", + "css": "foo:{}{}", + "expectedThrow": { + "name": "SyntaxError" + } + }, // parseAComponentValue() {