scalatags JsDom vs Text

时间:2016-09-25 08:08:12

标签: html scala scalatags

scalatags.Text.all._scalatags.JsDom.all._包的差异和目的是什么?

official scalatags tutorial中,您可以阅读:

// import scalatags.Text.all._
// OR
// import scalatags.JsDom.all._
html(
  head(
    script(src:="..."),
    script(
      "alert('Hello World')"
    )
  ),
  body(
    div(
      h1(id:="title", "This is a title"),
      p("This is a big paragraph of text")
    )
  )
)
And turns them into HTML like this:

<html>
    <head>
        <script src="..."></script>
        <script>alert('Hello World')</script>
    </head>
    <body>
        <div>
            <h1 id="title">This is a title</h1>
            <p>This is a big paragraph of text</p>
        </div>
    </body>
</html>

1 个答案:

答案 0 :(得分:2)

部分DOMBackendInternals中的scalatags文档中描述了差异。

在使用scalatags.Text包时,结构会直接呈现给String,但在使用scalatags.JsDOM包时,结构呈现为org.scalajs.dom.raw.Element的子类型(在...之外) scalatags - 它是 scalajs 库的一部分。在处理Element时,可以进一步manipulate dom structure very low level of abstraction

此处,使用scalatags.Text.时,h1呈现给String

    import scalatags.Text.all._
    val x: String = h1("some header").render
    //x is a String

但是在这里,当使用scalatags.JsDom时,h1呈现给org.scalajs.dom.raw.HTMLHeadingElement

    import scalatags.JsDom.all._

    val x: Heading = h1("some header").render
    //x is type of Heading, which is defined as:
    //type Heading = raw.HTMLHeadingElement
    //raw.HTMLHeadingElement is org.scalajs.dom.raw.HTMLHeadingElement