Skip to content

Commit a20d5a7

Browse files
committed
refactor: allow for customSheetName to be an empty string
While you could do this before, if you had a class `a` and you wanted to keep it simple and not have anything for your `customSheetName` you'd end up with `-a` which was a bit annoying. This small change allows users to still use this the way the were before, but now if they are using an empty string as their `customSheetName`, they'll just get `a` instead of `-a`.
1 parent 8840abc commit a20d5a7

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

scalatags/src/scalatags/stylesheet/StyleSheet.scala

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ abstract class CascadingStyleSheet(implicit sourceName: sourcecode.FullName) ext
2121
abstract class StyleSheet(implicit sourceName: sourcecode.FullName){
2222
/**
2323
* The name of this CSS stylesheet. Defaults to the name of the trait,
24-
* but you can override
24+
* but you can override.
2525
*/
2626
def customSheetName: Option[String] = None
2727

@@ -30,7 +30,12 @@ abstract class StyleSheet(implicit sourceName: sourcecode.FullName){
3030
* any applicable pseudo-selectors into the name of the CSS class.
3131
*/
3232
protected[this] def nameFor(memberName: String, pseudoSelectors: String) = {
33-
customSheetName.getOrElse(defaultSheetName.replace(".", "-")) + "-" + memberName + pseudoSelectors
33+
val baseSuffix = memberName + pseudoSelectors
34+
customSheetName match {
35+
case Some("") => baseSuffix
36+
case Some(value) => value + "-" + baseSuffix
37+
case None => defaultSheetName.replace(".", "-") + "-" + baseSuffix
38+
}
3439
}
3540

3641
/**

scalatags/test/src/scalatags/generic/StyleSheetTests.scala

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,18 @@ abstract class StyleSheetTests[Builder, Output <: FragT, FragT]
9494
def y = cls.hover(
9595
opacity := 0.5
9696
)
97+
9798
}
9899

100+
object CustomEmpty extends CascadingStyleSheet{
101+
initStyleSheet()
102+
103+
override def customSheetName = Some("")
104+
val x = cls(
105+
backgroundColor := "red",
106+
height := 125
107+
)
108+
}
99109

100110
def check(txt: String, expected: String) = {
101111
// augmentString = work around scala/bug#11125 on JDK 11
@@ -181,6 +191,7 @@ abstract class StyleSheetTests[Builder, Output <: FragT, FragT]
181191
"""
182192
)
183193
}
194+
184195
test("defs"){
185196
check(
186197
Defs.styleSheetText,
@@ -192,9 +203,22 @@ abstract class StyleSheetTests[Builder, Output <: FragT, FragT]
192203
.$pkg-Defs-y:hover{
193204
opacity: 0.5;
194205
}
206+
"""
207+
)
208+
}
209+
210+
test("customEmpty"){
211+
check(
212+
CustomEmpty.styleSheetText,
213+
s"""
214+
.x{
215+
background-color: red;
216+
height: 125px;
217+
}
195218
"""
196219
)
197220
}
221+
198222
test("failure"){
199223
test("noDirectInstantiation"){
200224
// This doesn't seem to work, even though that snippet does indeed

0 commit comments

Comments
 (0)