Class/Object

org.lrng.binding

html

Related Docs: object html | package binding

Permalink

macro class html extends Annotation with StaticAnnotation

An annotation to convert XHTML literals to data-bindable DOM nodes.

Author:

杨博 (Yang Bo) <[email protected]>

Annotations
@compileTimeOnly( ... )
Source
html.scala
Examples:
  1. XML namespaces

    import scala.language.dynamics
    import org.scalajs.dom.document
    import org.scalajs.dom.raw._
    import com.thoughtworks.binding.Binding
    import com.thoughtworks.binding.Binding.BindingInstances.monadSyntax._
    object svg {
      object texts extends Dynamic {
        @inline def selectDynamic(data: String) = new html.NodeBinding.Constant.TextBuilder(data)
      }
      object elements {
        object svg extends Curried {
          @inline def applyBegin = new html.NodeBinding.Constant.ElementBuilder(document.createElementNS("http://www.w3.org/2000/svg", "svg").asInstanceOf[SVGSVGElement])
        }
        object text extends Curried {
          @inline def applyBegin = new html.NodeBinding.Constant.ElementBuilder(document.createElementNS("http://www.w3.org/2000/svg", "text").asInstanceOf[SVGTextElement])
        }
      }
      @inline def interpolation = Binding
      object values {
        sealed trait FontStyle
        case object normal extends FontStyle
        case object italic extends FontStyle
        case object oblique extends FontStyle
      }
      object attributes {
        def font$minusstyle(value: values.FontStyle) = {
          new html.NodeBinding.Constant.AttributeBuilder.Untyped(_.setAttribute("font-style", value.toString))
        }
        def font$minusstyle(binding: Binding[values.FontStyle]) = {
          new html.NodeBinding.Interpolated.AttributeBuilder( element =>
            binding.map { value =>
              element.setAttribute("font-style", value.toString)
            }
          )
        }
        }
    }
    implicit final class SvgUriOps(uriFactory: html.autoImports.xml.uris.type) {
      @inline def http$colon$div$divwww$u002Ew3$u002Eorg$div2000$divsvg = svg
    }
    @html
    val mySvg1 = <svg xmlns="http://www.w3.org/2000/svg"><text font-style="normal">my text</text></svg>
    mySvg1.watch()
    mySvg1.value.outerHTML should be("""">my text""")
    import svg.values.normal
    @html
    val mySvg2 = <svg:svg xmlns:svg="http://www.w3.org/2000/svg"><svg:text font-style={normal}>my text</svg:text></svg:svg>
    mySvg2.watch()
    mySvg2.value.outerHTML should be("""">my text""")
  2. ,
  3. CDATA sections are not supported in HTML documents

    import scala.scalajs.js
    a[js.JavaScriptException] should be thrownBy {
      @html val myCData = <![CDATA[my data]]>
      myCData.watch()
    }
  4. ,
  5. Process instructions

    @html val myXmlStylesheet = <?my-instruction my data?>
    myXmlStylesheet.watch()
    myXmlStylesheet.value.target should be("my-instruction")
    myXmlStylesheet.value.data should be("my data")
  6. ,
  7. Entity references

    import org.scalajs.dom.document
    @html def entity =
    my text &lt; &gt; &copy; &lambda;
    val div = document.createElement("div")
    html.render(div, entity)
    assert(div.innerHTML == """
     © λ my-class">my text &lt; &gt; © λ""")
  8. ,
  9. Escape

    import org.scalajs.dom.document
    @html def escaped =
    &#32;$minus
    val div = document.createElement("div")
    html.render(div, escaped)
    assert(div.innerHTML == "
     $minus")
  10. ,
  11. XHTML comments

    import org.scalajs.dom.document
    @html def comment =
    val div = document.createElement("div")
    html.render(div, comment)
    assert(div.innerHTML == "
    ")
  12. ,
  13. Seq in DOM

    import org.scalajs.dom.document
    @html def myUl = {
      {Seq(
     - data1,
     - data2)}
    }
    val div = document.createElement("div")
    html.render(div, myUl)
    div.firstChild.childNodes.length should be(2)
  14. ,
  15. A child node must not be inserted more than once

    an[IllegalStateException] should be thrownBy {
      @html
      val child = <hr/>
      @html
      val parent =
    <span>{child.bind}</span><span>{child.bind}</span>
      parent.watch()
    }
  16. ,
  17. Changing attribute values

    import com.thoughtworks.binding.Binding.Var
    val id = Var("oldId")
    @html val myInput = <input data:applyDynamic={id.bind} data:custom={id.bind} name={id.bind} id={id.bind} onclick={ _: Any => id.value = "newId" }/>
    myInput.watch()
    assert(myInput.value.outerHTML == """" custom="oldId" name="oldId" id="oldId">""")
    myInput.value.onclick(null)
    assert(myInput.value.outerHTML == """" custom="newId" name="newId" id="newId">""")
  18. ,
  19. Dynamc attributes

    @html
    val myBr = <br data:toString="+©" data:applyDynamic="value"/>
    myBr.watch()
    myBr.value.outerHTML should be("""
    " applydynamic="
    value">""")
  20. ,
  21. for / yield / if expressions in XHTML interpolation

    import com.thoughtworks.binding.Binding
    import com.thoughtworks.binding.Binding.Var
    import com.thoughtworks.binding.Binding.Vars
    final case class User(firstName: Var[String], lastName: Var[String], age: Var[Int])
    val filterPattern = Var("")
    val users = Vars(
      User(Var("Steve"), Var("Jobs"), Var(10)),
      User(Var("Tim"), Var("Cook"), Var(12)),
      User(Var("Jeff"), Var("Lauren"), Var(13))
    )
    def shouldShow(user: User): Binding[Boolean] = Binding {
      val pattern = filterPattern.bind
      if (pattern == "") {
        true
      } else if (user.firstName.bind.toLowerCase.contains(pattern)) {
        true
      } else if (user.lastName.bind.toLowerCase.contains(pattern)) {
        true
      } else {
        false
      }
    }
    @html
    def tbodyBinding = {
      <tbody>{
        for {
          user <- users
          if shouldShow(user).bind
        } yield <tr><td>{user.firstName.bind}</td><td>{user.lastName.bind}</td><td>{user.age.bind.toString}</td></tr>
      }</tbody>
    }
    @html
    val tableBinding = {
      <table class="my-table" title="My Tooltip"><thead><tr><td>First Name</td><td>Second Name</td><td>Age</td></tr></thead>{tbodyBinding.bind}</table>
    }
    tableBinding.watch()
    assert(tableBinding.value.outerHTML == """" title="My Tooltip">
    First NameSecond NameAge
    SteveJobs10
    TimCook12
    JeffLauren13
    "
    "") filterPattern.value = "o" assert(tableBinding.value.outerHTML == """" title="My Tooltip">
    First NameSecond NameAge
    SteveJobs10
    TimCook12
    "
    "")
  22. ,
  23. for / yield expressions in XHTML interpolation

    import com.thoughtworks.binding.Binding.Vars
    val v0 = Vars("original text 0","original text 1")
    @html val monadicDiv =
     <span> { for (s <- v0) yield <b>{s}</b> } </span>
    monadicDiv.watch()
    val div = monadicDiv.value
    assert(monadicDiv.value.outerHTML == "
      original text 0original text 1  ")
    v0.value.prepend("prepended")
    assert(div eq monadicDiv.value)
    assert(monadicDiv.value.outerHTML == "
      prependedoriginal text 0original text 1  ")
    v0.value.remove(1)
    assert(div eq monadicDiv.value)
    assert(monadicDiv.value.outerHTML == "
      prependedoriginal text 1  ")
  24. ,
  25. Changing text

    import com.thoughtworks.binding.Binding.Var
    val v0 = Var("original text")
    @html val monadicDiv =
     <span> {v0.bind} </span>
    monadicDiv.watch()
    assert(monadicDiv.value.outerHTML == "
      original text  ")
    v0.value = "changed"
    assert(monadicDiv.value.outerHTML == "
      changed  ")
  26. ,
  27. Text interpolation in an element

    @html val monadicDiv =
    {"text"}
    monadicDiv.watch()
    assert(monadicDiv.value.outerHTML == "
    text")
  28. ,
  29. Element list of XHTML literals

    @html
    val myDivs =
    myDivs.watch()
    import org.scalajs.dom.html.Div
    inside(myDivs.value) {
      case collection.Seq(div1: Div, div2: Div, div3: Div) =>
        div1.nodeName should be("DIV")
        div1.hasAttribute("class") should be(false)
        div1.className should be("")
        div2.title should be("my title")
        div2.hasAttribute("class") should be(true)
        div2.className should be("")
        div3.className should be("my-class")
    }
  30. ,
  31. Opaque type aliases of HTMLInputElement

    @html
    val myMeta = <input name="myInput" type="radio"/>
    myMeta.watch()
    myMeta.value.outerHTML should be("""" type="radio">""")
  32. ,
  33. Special character in attribute names

    @html
    val myMeta = <meta http-equiv="refresh" content="30"/>
    myMeta.watch()
    myMeta.value.outerHTML should be("""" content="30">""")
  34. ,
  35. XHTML literals with text attributes

    @html
    val myDiv =
    myDiv.value.nodeName should be("DIV")
    myDiv.value.className should be("my-class")
    myDiv.value.tabIndex should be(42)

    Nested XHTML literals with interpolation:

    @html
    val myDiv2 =
    text<span style={"color: blue;"} tabIndex={99}></span>
    {myDiv.bind}
    myDiv2.watch()
    myDiv2.value.outerHTML should be("""
    text" tabindex="99">
    html
    """)
Linear Supertypes
Type Hierarchy
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. html
  2. StaticAnnotation
  3. Annotation
  4. AnyRef
  5. Any
Implicitly
  1. by any2stringadd
  2. by StringFormat
  3. by Ensuring
  4. by ArrowAssoc
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. All

Instance Constructors

  1. new html()

    Permalink

Value Members

  1. final def !=(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  2. final def ##(): Int

    Permalink
    Definition Classes
    AnyRef → Any
  3. def +(other: String): String

    Permalink
    Implicit information
    This member is added by an implicit conversion from html to any2stringadd[html] performed by method any2stringadd in scala.Predef.
    Definition Classes
    any2stringadd
  4. def ->[B](y: B): (html, B)

    Permalink
    Implicit information
    This member is added by an implicit conversion from html to ArrowAssoc[html] performed by method ArrowAssoc in scala.Predef.
    Definition Classes
    ArrowAssoc
    Annotations
    @inline()
  5. final def ==(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  6. final def asInstanceOf[T0]: T0

    Permalink
    Definition Classes
    Any
  7. def clone(): AnyRef

    Permalink
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  8. def ensuring(cond: (html) ⇒ Boolean, msg: ⇒ Any): html

    Permalink
    Implicit information
    This member is added by an implicit conversion from html to Ensuring[html] performed by method Ensuring in scala.Predef.
    Definition Classes
    Ensuring
  9. def ensuring(cond: (html) ⇒ Boolean): html

    Permalink
    Implicit information
    This member is added by an implicit conversion from html to Ensuring[html] performed by method Ensuring in scala.Predef.
    Definition Classes
    Ensuring
  10. def ensuring(cond: Boolean, msg: ⇒ Any): html

    Permalink
    Implicit information
    This member is added by an implicit conversion from html to Ensuring[html] performed by method Ensuring in scala.Predef.
    Definition Classes
    Ensuring
  11. def ensuring(cond: Boolean): html

    Permalink
    Implicit information
    This member is added by an implicit conversion from html to Ensuring[html] performed by method Ensuring in scala.Predef.
    Definition Classes
    Ensuring
  12. final def eq(arg0: AnyRef): Boolean

    Permalink
    Definition Classes
    AnyRef
  13. def equals(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  14. def finalize(): Unit

    Permalink
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( classOf[java.lang.Throwable] )
  15. def formatted(fmtstr: String): String

    Permalink
    Implicit information
    This member is added by an implicit conversion from html to StringFormat[html] performed by method StringFormat in scala.Predef.
    Definition Classes
    StringFormat
    Annotations
    @inline()
  16. final def getClass(): Class[_]

    Permalink
    Definition Classes
    AnyRef → Any
  17. def hashCode(): Int

    Permalink
    Definition Classes
    AnyRef → Any
  18. final def isInstanceOf[T0]: Boolean

    Permalink
    Definition Classes
    Any
  19. macro def macroTransform(annottees: Any*): Any

    Permalink
  20. final def ne(arg0: AnyRef): Boolean

    Permalink
    Definition Classes
    AnyRef
  21. final def notify(): Unit

    Permalink
    Definition Classes
    AnyRef
  22. final def notifyAll(): Unit

    Permalink
    Definition Classes
    AnyRef
  23. final def synchronized[T0](arg0: ⇒ T0): T0

    Permalink
    Definition Classes
    AnyRef
  24. def toString(): String

    Permalink
    Definition Classes
    AnyRef → Any
  25. final def wait(): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  26. final def wait(arg0: Long, arg1: Int): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  27. final def wait(arg0: Long): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  28. def [B](y: B): (html, B)

    Permalink
    Implicit information
    This member is added by an implicit conversion from html to ArrowAssoc[html] performed by method ArrowAssoc in scala.Predef.
    Definition Classes
    ArrowAssoc

Inherited from StaticAnnotation

Inherited from Annotation

Inherited from AnyRef

Inherited from Any

Inherited by implicit conversion any2stringadd from html to any2stringadd[html]

Inherited by implicit conversion StringFormat from html to StringFormat[html]

Inherited by implicit conversion Ensuring from html to Ensuring[html]

Inherited by implicit conversion ArrowAssoc from html to ArrowAssoc[html]

Ungrouped