0

我有一个按钮,可以用作 Twitter 引导程序的弹出框。

button(data.content := h1("an example of html").render, data.toggle="popover")("click here")

我想在弹出框的内容中有一些 html 代码,所以我需要将 html 传递给data.toggle属性,但是这会将 html 代码打印出来,因为 scalatags 阻止了 XSS。我怎样才能防止这种情况/我怎样才能得到这种效果?

4

1 回答 1

0

这很粗糙,但它会做你想做的事。我不确定有没有更好的方法。

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")
于 2017-03-22T10:31:18.243 回答