com-lihaoyi / scalatags

ScalaTags is a small XML/HTML construction library for Scala.
https://com-lihaoyi.github.io/scalatags/
MIT License
757 stars 117 forks source link

Fix for empty string class name #214

Closed tbje closed 2 years ago

tbje commented 3 years ago
bwbecker commented 3 years ago

I'd like to speak in favour of merging this commit ASAP. In my case, I got bit by creating a set of classes to add with code similar to the following:

      def myClasses(cond1: Boolean, cond2: Boolean) =
        cls := Seq(
            if (cond1) "cond1" else "",
            if (cond2) "cond2" else ""
          ).mkString(" ")

Of the four possible ways to call myClasses, one of them fails when the html is rendered:

        p(myClasses(false, true), "my paragraph").toString() ==> "<p class=\"cond2\">my paragraph</p>"

Or, in a simpler form (note the leading space in " cond2"):

        p(cls := " cond2", "my paragraph").toString() ==> "<p class=\"cond2\">my paragraph</p>"

Curiously, a trailing space in the class renders just fine.

sake92 commented 2 years ago

@bwbecker IDK if this is more readable to you, but you can use Option[AttrValue] and Option[AttrPair]: https://scastie.scala-lang.org/9BDfZS9xQpq8xgrHcJityA (the last example)

Also, the 2.13's Option.when is really handy for these. 😃