2017-01-26 39 views
0

我有一個按鈕,可用作Twitter的引導程序的彈出窗口。如何在scalatags屬性中插入html代碼?

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

我想有在酥料餅的一些內容的HTML代碼,所以我需要通過HTML到data.toggle屬性附加傷害,但是這一點普通打印的HTML代碼,因爲scalatags防止XSS的。我怎樣才能防止這個/我怎麼能得到這個效果?

+0

你讀過http://www.lihaoyi.com/scalatags/#Auto-escapingandunsanitizedInput? – danielnixon

+0

@danielnixon我還沒有找到一種方法來使用屬性,如我的問題所述。 – rusins

回答

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")