From 4ebbcc5cd6a797d0a1a2e9534f5dbc557b086166 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 | 96 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 103 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..cdbbbc6 100644 --- a/tests.js +++ b/tests.js @@ -3600,6 +3600,102 @@ 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" + } + }, + { + "parser": "parseADeclaration", + "css": "-foo:bar{}", + "expectedThrow": { + "name": "SyntaxError" + } + }, + { + "parser": "parseADeclaration", + "css": "--foo:bar{}", + "expected": { + "type": "DECLARATION", + "name": "--foo", + "value": [ + { + "type": "IDENT", + "value": "bar" + }, + { + "type": "BLOCK", + "name": "{", + "value": [] + } + ], + "important": false + } + }, // parseAComponentValue() {