From 5e5930e9e8bb080d0be96c3499fe0cd752df9654 Mon Sep 17 00:00:00 2001 From: 01zulfi <85733202+01zulfi@users.noreply.github.com> Date: Thu, 12 Jun 2025 13:52:10 +0500 Subject: [PATCH] clipper: inline styles if cssRules.length > 0 Signed-off-by: 01zulfi <85733202+01zulfi@users.noreply.github.com> --- packages/clipper/src/styles.ts | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/packages/clipper/src/styles.ts b/packages/clipper/src/styles.ts index 747a34725..20ae8bb58 100644 --- a/packages/clipper/src/styles.ts +++ b/packages/clipper/src/styles.ts @@ -97,8 +97,26 @@ export async function addStylesToHead( if (styleNode) { head.appendChild(styleNode); } - } else if (node instanceof HTMLStyleElement) { + continue; + } + + if (sheet.cssRules.length) { + const styleNode = rulesToStyleNode(sheet.cssRules); + head.appendChild(styleNode); + continue; + } + + if (node instanceof HTMLStyleElement) { head.appendChild(node.cloneNode(true)); } } } + +function rulesToStyleNode(cssRules: CSSRuleList) { + const cssText = Array.from(cssRules) + .map((r) => r.cssText) + .reduce((acc, text) => acc + text); + const style = document.createElement("style"); + style.innerHTML = cssText; + return style; +}