From 937522fb70be522378268d04e6bb20d8dc401c0b Mon Sep 17 00:00:00 2001 From: Marvin <272077995@qq.com> Date: Thu, 2 Nov 2023 05:02:00 +0800 Subject: [PATCH] fix: toStyleString omit nullish values (#8940) Co-authored-by: Bjorn Lu Co-authored-by: Nate Moore --- .changeset/long-roses-shave.md | 5 +++++ packages/astro/src/runtime/server/render/util.ts | 1 + 2 files changed, 6 insertions(+) create mode 100644 .changeset/long-roses-shave.md diff --git a/.changeset/long-roses-shave.md b/.changeset/long-roses-shave.md new file mode 100644 index 0000000000..65c6d09227 --- /dev/null +++ b/.changeset/long-roses-shave.md @@ -0,0 +1,5 @@ +--- +"astro": patch +--- + +Omit nullish and falsy (non-zero) values when stringifying object-form `style` attributes in Astro files diff --git a/packages/astro/src/runtime/server/render/util.ts b/packages/astro/src/runtime/server/render/util.ts index fca449fcf0..89d51ba6cb 100644 --- a/packages/astro/src/runtime/server/render/util.ts +++ b/packages/astro/src/runtime/server/render/util.ts @@ -28,6 +28,7 @@ const kebab = (k: string) => k.toLowerCase() === k ? k : k.replace(/[A-Z]/g, (match) => `-${match.toLowerCase()}`); const toStyleString = (obj: Record) => Object.entries(obj) + .filter(([k, v]) => typeof v === 'string' && v.trim() || typeof v === 'number') .map(([k, v]) => { if (k[0] !== '-' && k[1] !== '-') return `${kebab(k)}:${v}`; return `${k}:${v}`;