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 28, 2023
1 parent 4fab958 commit ae18f20
Show file tree
Hide file tree
Showing 2 changed files with 31 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("") => baseSuffix
case Some(value) => value + "-" + baseSuffix
case None => defaultSheetName.replace(".", "-") + "-" + baseSuffix
}
}

/**
Expand Down
24 changes: 24 additions & 0 deletions scalatags/test/src/scalatags/generic/StyleSheetTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,18 @@ abstract class StyleSheetTests[Builder, Output <: FragT, FragT]
def y = cls.hover(
opacity := 0.5
)

}

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 @@ -181,6 +191,7 @@ abstract class StyleSheetTests[Builder, Output <: FragT, FragT]
"""
)
}

test("defs"){
check(
Defs.styleSheetText,
Expand All @@ -192,9 +203,22 @@ abstract class StyleSheetTests[Builder, Output <: FragT, FragT]
.$pkg-Defs-y:hover{
opacity: 0.5;
}
"""
)
}

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 ae18f20

Please sign in to comment.