From b6eb51db609d3011bac008dfe08340a26b67dc1c Mon Sep 17 00:00:00 2001 From: Dennis Tsar <45399002+DennisTsar@users.noreply.github.com> Date: Sat, 27 Jul 2024 01:48:32 -0400 Subject: [PATCH] Fix empty css layer name being registered as anonymous layer If a user passes an empty string when registering a style, or in an `@CssLayer` annotation, the style should be registered without any layers - not in an anonymous, nameless layer. --- .../com/varabyte/kobweb/silk/theme/SilkTheme.kt | 6 ++++-- .../com/varabyte/kobweb/silk/CssStyleTest.kt | 15 +++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/frontend/silk-foundation/src/jsMain/kotlin/com/varabyte/kobweb/silk/theme/SilkTheme.kt b/frontend/silk-foundation/src/jsMain/kotlin/com/varabyte/kobweb/silk/theme/SilkTheme.kt index 252b367ea..b9a588420 100644 --- a/frontend/silk-foundation/src/jsMain/kotlin/com/varabyte/kobweb/silk/theme/SilkTheme.kt +++ b/frontend/silk-foundation/src/jsMain/kotlin/com/varabyte/kobweb/silk/theme/SilkTheme.kt @@ -107,8 +107,10 @@ class MutableSilkTheme { RestrictedKind::class -> SilkLayer.RESTRICTED_STYLES GeneralKind::class -> SilkLayer.GENERAL_STYLES else -> error("Unknown kind: $kind") - }.layerName.takeIf { it.isNotEmpty() } // In case user passes in "" - finalLayer?.let { _cssLayersFor[name] = it } + }.layerName + finalLayer + .takeIf { it.isNotEmpty() } // If the user passes in "", no layer should be registered + ?.let { _cssLayersFor[name] = it } if (style is ExtendingCssStyle) { _cssStyleDependencies.getOrPut(style) { mutableListOf() }.add(style.baseStyle) diff --git a/frontend/silk-foundation/src/jsTest/kotlin/com/varabyte/kobweb/silk/CssStyleTest.kt b/frontend/silk-foundation/src/jsTest/kotlin/com/varabyte/kobweb/silk/CssStyleTest.kt index a199cc310..fba6e65c6 100644 --- a/frontend/silk-foundation/src/jsTest/kotlin/com/varabyte/kobweb/silk/CssStyleTest.kt +++ b/frontend/silk-foundation/src/jsTest/kotlin/com/varabyte/kobweb/silk/CssStyleTest.kt @@ -73,6 +73,21 @@ class CssStyleTest { .inOrder() } + + @Test + fun cssStyleEmptyLayerNameResultsInNoLayer() { + val stylesheet = StyleSheet() + // Styles must be non-empty to be registered + val BaseStyle = CssStyle.base { Modifier.color(Colors.Red) } + _SilkTheme = MutableSilkTheme().apply { + registerStyle("base-style", BaseStyle, layer = "") + }.let(::ImmutableSilkTheme) + SilkTheme.registerStylesInto(stylesheet) + val styleRule = stylesheet.cssRules.first() + assertThat(styleRule).isInstanceOf() + assertThat(styleRule.header).isEqualTo(".base-style") + } + @Test fun cssStyleExtendedClasses() { val BaseStyle = CssStyle { }