3

I tried to declare some embedded css with Binding.scala

import com.thoughtworks.binding._, Binding._
import org.scalajs.dom._

@dom def css = <style>
  body {
    background-color: lightblue;
  }
</style>

dom.render(document.head, css)

However, I got the error message:

ScalaFiddle.scala:6: error: not found: type lightblue
      background-color: lightblue;
                        ^
ScalaFiddle.scala:6: error: not found: value background
      background-color: lightblue;
      ^
ScalaFiddle.scala:6: error: not found: value color
      background-color: lightblue;
                 ^

How can I fix it?

Yang Bo
  • 3,586
  • 3
  • 22
  • 35

2 Answers2

4

You see the error message because { is a special character in Scala's XML literal.

Use a CDATA section in the style element.

@dom def css = <style>
  <![CDATA[
    body {
      background-color: lightblue;
    }
  ]]>
</style>

The { does not have special meaning in the CDATA section any more.


Note this CDATA approach only works when coalescing flag is on. See https://github.com/ThoughtWorksInc/Binding.scala/issues/30 and https://github.com/ThoughtWorksInc/Binding.scala/issues/58 if you accidentally turns the flag off.

Yang Bo
  • 3,586
  • 3
  • 22
  • 35
  • This question and answer is moved from https://github.com/ThoughtWorksInc/Binding.scala/wiki/FAQ – Yang Bo Mar 20 '17 at 16:52
1

Using the answer of Yang Bo:

@dom def css = <style>
  <![CDATA[
    body {
      background-color: lightblue;
    }
  ]]>
</style>

Gives me an Exception:

ScalaFiddle.scala:22: error: overloaded method value domBindingSeq with alternatives:
  ( text: String)binding.this.Binding.Constants[raw.this.Text] 
...

See https://scalafiddle.io/sf/ATMVpjV/0

This solved it:

  @dom def css = <style>
  {"""
    body {
      background-color: lightblue;
    }
    """
  }
</style>

See https://scalafiddle.io/sf/ATMVpjV/1

pme
  • 14,156
  • 3
  • 52
  • 95
  • It seems that ScalaFiddle turned off `coalescing` flag. See https://github.com/ThoughtWorksInc/Binding.scala/issues/30 and https://github.com/ThoughtWorksInc/Binding.scala/issues/58 – Yang Bo Mar 28 '18 at 17:02