這是粗糙的,但它會做你想做的。我不確定有更好的方法。
import scalatags.Text.{Attr, TypedTag}
import scalatags.Text.all._
import scalatags.text.Builder
// Warning: this extends a "more-or-less internal trait" so may stop working after a ScalaTags update.
final case class RawValueSource(v: String) extends Builder.ValueSource {
override def appendAttrValue(strb: StringBuilder): Unit = strb.append(v)
}
// We need an AttrValue for tags. This one just renders the tag *without* escaping the result.
class TagAttrValue extends AttrValue[TypedTag[String]] {
override def apply(t: Builder, a: Attr, v: TypedTag[String]): Unit = {
t.setAttr(a.name, RawValueSource(v.render))
}
}
// We need this implicit in scope so that we can use tags on the rhs of the := operator.
implicit val tagAttr = new TagAttrValue()
button(data.content := h1("an example of html"), data.toggle := "popover")("click here")
你讀過http://www.lihaoyi.com/scalatags/#Auto-escapingandunsanitizedInput? – danielnixon
@danielnixon我還沒有找到一種方法來使用屬性,如我的問題所述。 – rusins