Skip to content

Commit

Permalink
Improved custom styles cleanup logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Clooos authored Feb 14, 2025
1 parent 76aac0f commit 148f342
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 14 deletions.
2 changes: 1 addition & 1 deletion dist/bubble-card.js

Large diffs are not rendered by default.

Binary file modified dist/bubble-card.js.gz
Binary file not shown.
27 changes: 14 additions & 13 deletions src/tools/style-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,20 +178,21 @@ export function evalStyles(context, styles = "") {
function cleanCSS(css) {
return css
.replace(/\/\*[\s\S]*?\*\//g, "") // Remove CSS comments
.replace(/,\s*\n/g, ", ") // Ensure selectors stay on the same line
.replace(/\s+/g, " ") // Reduce multiple spaces
.replace(/\s*([{};,])\s*/g, "$1") // Normalize spacing around special characters
.replace(/([a-zA-Z0-9_-]+)\s*:\s*;/g, "") // Remove empty declarations
.replace(/undefined(?=(?:(?:[^"]*"){2})*[^"]*$)/g, "") // Remove "undefined"
.replace(/[^{};]\s*{\s*}/g, "") // Remove empty blocks
.replace(/,(?=\s*[}\n])/g, "") // Remove trailing commas in selectors
.split("\n")
.filter(line =>
line.includes("{") ||
line.includes("}") ||
line.includes(":") ||
line.trim().match(/['"]/g)?.length === 2 || // Keep grid-template-areas
line.includes("${") // Keep Home Assistant interpolations
.filter(line =>
line.includes("{") ||
line.includes("}") ||
line.includes(":") ||
line.trim().match(/['"]{2}/g) || // Keep grid-template-areas
line.includes("${") || // Keep Home Assistant interpolations
line.match(/^@supports|^@media|^@keyframes|^@layer/) // Keep special at-rules
)
.join("\n")
.replace(/undefined(?=(?:(?:[^"]*"){2})*[^"]*$)/g, "") // Remove "undefined"
.replace(/[^{};]\s*{\s*}/g, "") // Remove empty blocks
.replace(/([a-z-]+)\s*:\s*;/g, "") // Remove empty declarations
.replace(/\s+/g, " ") // Reduce multiple spaces
.trim()
.match(/(@keyframes\s+[^{]+\{(?:[^{}]*\{[^{}]*\})+[^{}]*\}|@[^{]*?\{(?:[^{}]*?\{[^{}]*?\})*?[^{}]*?\}|[^{}]*?\{[^{}]*?\})/g)?.join("\n") || "";
.match(/(@[^{]*?\{(?:[^{}]*?\{[^{}]*?\})*?[^{}]*?\}|[^{}]*?\{[^{}]*?\})/g)?.join("\n") || "";
}

0 comments on commit 148f342

Please sign in to comment.