首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在scalatags属性中插入html代码?

如何在scalatags属性中插入html代码?
EN

Stack Overflow用户
提问于 2017-01-26 20:53:13
回答 1查看 174关注 0票数 0

我有一个按钮,可以作为Twitter的bootstrap的弹出窗口。

代码语言:javascript
复制
button(data.content := h1("an example of html").render, data.toggle="popover")("click here")

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

EN

回答 1

Stack Overflow用户

发布于 2017-03-22 18:31:18

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

代码语言:javascript
复制
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")
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41873892

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档