scalatags
scalatags copied to clipboard
Empty class throws Uncaught DOMException
The code:
package tutorial.webapp
import org.scalajs.dom
import org.scalajs.dom.document
import scalatags.JsDom.all._
object TutorialApp {
def main(args: Array[String]): Unit = {
appendPar(document.body, "Hello World")
}
def appendPar(targetNode: dom.Node, text: String): Unit = {
targetNode.appendChild(p(cls := "", text).render)
}
}
Will result in:
JsDom.scala:152 Uncaught DOMException: Failed to execute 'add' on 'DOMTokenList': The token provided must not be empty.
at http://localhost:63342/Scala.js%20Tutorial/target/scala-2.13/scala-js-tutorial-fastopt/main.js:4504:25
at $c_Lscalatags_JsDom$GenericAttr.apply__Lorg_scalajs_dom_raw_Element__Lscalatags_generic_Attr__O__V (http://localhost:63342/Scala.js%20Tutorial/target/scala-2.13/scala-js-tutorial-fastopt/main.js:4511:13)
at $c_Lscalatags_generic_AttrPair.applyTo__O__V (http://localhost:63342/Scala.js%20Tutorial/target/scala-2.13/scala-js-tutorial-fastopt/main.js:8323:10)
at $f_Lscalatags_generic_TypedTag__build__O__V (http://localhost:63342/Scala.js%20Tutorial/target/scala-2.13/scala-js-tutorial-fastopt/main.js:5277:62)
at $c_Lscalatags_JsDom$TypedTag.render__Lorg_scalajs_dom_raw_Element (http://localhost:63342/Scala.js%20Tutorial/target/scala-2.13/scala-js-tutorial-fastopt/main.js:9211:3)
at $c_Ltutorial_webapp_TutorialApp$.appendPar__Lorg_scalajs_dom_raw_Node__T__V (http://localhost:63342/Scala.js%20Tutorial/target/scala-2.13/scala-js-tutorial-fastopt/main.js:2874:108)
at $c_Ltutorial_webapp_TutorialApp$.main__AT__V (http://localhost:63342/Scala.js%20Tutorial/target/scala-2.13/scala-js-tutorial-fastopt/main.js:2868:8)
at $s_Ltutorial_webapp_TutorialApp__main__AT__V (http://localhost:63342/Scala.js%20Tutorial/target/scala-2.13/scala-js-tutorial-fastopt/main.js:2854:38)
at http://localhost:63342/Scala.js%20Tutorial/target/scala-2.13/scala-js-tutorial-fastopt/main.js:13999:1
at http://localhost:63342/Scala.js%20Tutorial/target/scala-2.13/scala-js-tutorial-fastopt/main.js:14000:4
Trying to add an empty class is probably a bug but I think Scalatags could be a bit more helpful in this case, especially since the error is not entirely obvious what is going on.
I just got caught by this as well. It would be really helpful for ScalaTags to be more proactive in avoiding it. This is a regression between version 0.8.6 (which I was using previously) and 0.9.3 (just started using it when I found this issue).
There are also a number of inconsistencies, in the DOM and ScalaTags.
div(cls := "", "my div").toString fails when using scalatags.JsDom.
div(cls := " ", "my div").toString (note the space in the string given as the class) does not fail when using scalatags.JsDom and produces <div>my div</div>.
div(cls := "", "my div").toString produces <div class="">my div</div> when using scalatags.Text.
div(cls := " ", "my div").toString (the space is back) produces <div class=" ">my div</div> when using scalatags.Text (which is different than using scalatags.JsDom).
In my ideal world, div(cls := "", "my div").toString would produce <div>my div</div> with both JsDom and Text -- and the same if cls := " ".