From c9d221eb73c9419f98ca3be6944ae8b00b8b9b58 Mon Sep 17 00:00:00 2001 From: Xiaojun Zhou <362896731@qq.com> Date: Tue, 21 Nov 2023 23:57:48 +0800 Subject: [PATCH] fix(postcss-convert-values): keep % unit in @property (#1531) Co-authored-by: Ludovico Fischer <43557+ludofischer@users.noreply.github.com> --- CONTRIBUTORS.md | 1 + packages/postcss-convert-values/src/index.js | 21 ++++++++++++++----- packages/postcss-convert-values/test/index.js | 8 +++++++ 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index a3404267f..54eb41daa 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -69,3 +69,4 @@ Thanks goes to these wonderful people: - Jakka Prihatna - James George - Jordy D'Hoker +- Xiaojun Zhou diff --git a/packages/postcss-convert-values/src/index.js b/packages/postcss-convert-values/src/index.js index 94872a55c..3590d9082 100644 --- a/packages/postcss-convert-values/src/index.js +++ b/packages/postcss-convert-values/src/index.js @@ -118,13 +118,24 @@ function shouldKeepZeroUnit(decl, browsers) { (decl.value.includes('%') && keepZeroPercent.has(lowerCasedProp) && browsers.includes('ie 11')) || - (parent && + (lowerCasedProp === 'stroke-dasharray' && + parent && parent.parent && parent.parent.type === 'atrule' && - /** @type {import('postcss').AtRule} */ ( - parent.parent - ).name.toLowerCase() === 'keyframes' && - lowerCasedProp === 'stroke-dasharray') || + /** @type {import('postcss').AtRule} */ + (parent.parent).name.toLowerCase() === 'keyframes') || + (lowerCasedProp === 'initial-value' && + parent && + parent.type === 'atrule' && + /** @type {import('postcss').AtRule} */ + (parent).name === 'property' && + /** @type {import('postcss').AtRule} */ + (parent).nodes.some( + (node) => + node.type === 'decl' && + node.prop.toLowerCase() === 'syntax' && + node.value === "''" + )) || keepWhenZero.has(lowerCasedProp) ); } diff --git a/packages/postcss-convert-values/test/index.js b/packages/postcss-convert-values/test/index.js index 3008ceac1..b88c330c7 100644 --- a/packages/postcss-convert-values/test/index.js +++ b/packages/postcss-convert-values/test/index.js @@ -430,5 +430,13 @@ test( ) ); +test( + `should not strip the percentage from 0 in @property, for initial-value`, + processCSS( + `@property --percent{syntax:'';inherits:false;initial-value:0%;}`, + `@property --percent{syntax:'';inherits:false;initial-value:0%;}`, + ) +); + test('should use the postcss plugin api', usePostCSSPlugin(plugin())); test.run();