Skip to content

Commit

Permalink
refactor: allow for customSheetName to be an empty string
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
ckipp01 committed Jan 11, 2023
1 parent a1aeb6b commit e356cc4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
9 changes: 7 additions & 2 deletions scalatags/src/scalatags/stylesheet/StyleSheet.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ abstract class CascadingStyleSheet(implicit sourceName: sourcecode.FullName) ext
abstract class StyleSheet(implicit sourceName: sourcecode.FullName){
/**
* The name of this CSS stylesheet. Defaults to the name of the trait,
* but you can override
* but you can override.
*/
def customSheetName: Option[String] = None

Expand All @@ -30,7 +30,12 @@ abstract class StyleSheet(implicit sourceName: sourcecode.FullName){
* any applicable pseudo-selectors into the name of the CSS class.
*/
protected[this] def nameFor(memberName: String, pseudoSelectors: String) = {
customSheetName.getOrElse(defaultSheetName.replace(".", "-")) + "-" + memberName + pseudoSelectors
val baseSuffix = memberName + pseudoSelectors
customSheetName match {
case Some(value) if value.isEmpty() => baseSuffix
case Some(value) => value + "-" + baseSuffix
case None => defaultSheetName.replace(".", "-") + "-" + baseSuffix
}
}

/**
Expand Down
21 changes: 21 additions & 0 deletions scalatags/test/src/scalatags/generic/StyleSheetTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,16 @@ abstract class StyleSheetTests[Builder, Output <: FragT, FragT]

}

object CustomEmpty extends CascadingStyleSheet{
initStyleSheet()

override def customSheetName = Some("")
val x = cls(
backgroundColor := "red",
height := 125
)
}


def check(txt: String, expected: String) = {
// augmentString = work around scala/bug#11125 on JDK 11
Expand Down Expand Up @@ -168,6 +178,17 @@ abstract class StyleSheetTests[Builder, Output <: FragT, FragT]
"""
)
}
test("customEmpty"){
check(
CustomEmpty.styleSheetText,
s"""
.x{
background-color: red;
height: 125px;
}
"""
)
}
test("failure"){
test("noDirectInstantiation"){
// This doesn't seem to work, even though that snippet does indeed
Expand Down

0 comments on commit e356cc4

Please sign in to comment.