Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 36 additions & 15 deletions dom/src/main/scala/com/thoughtworks/binding/dom.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ package com.thoughtworks.binding
import Binding.{BindingSeq, Constants, MultiMountPoint, SingletonBindingSeq}
import dom.Runtime.NodeSeqMountPoint
import com.thoughtworks.Extractor._
import com.thoughtworks.binding.XmlExtractor.{PrefixedName, UnprefixedName}
import com.thoughtworks.binding.XmlExtractor.{PrefixedName, QName, UnprefixedName}
import com.thoughtworks.sde.core.Preprocessor
import macrocompat.bundle
import org.scalajs.dom.raw._
Expand All @@ -39,6 +39,8 @@ import scala.language.experimental.macros
import scalatags.JsDom
import scalatags.jsdom
import org.scalajs.dom.document
import scalatags.JsDom.TypedTag
import scalatags.generic.Namespace

import scala.collection.immutable.Queue
import scala.reflect.NameTransformer
Expand Down Expand Up @@ -140,7 +142,21 @@ object dom {

}

object TagsAndTags2 extends JsDom.Cap with jsdom.Tags with jsdom.Tags2
object TagsAndTags2 extends JsDom.Cap with jsdom.Tags with jsdom.Tags2 {

import scala.language.dynamics

final class DynamicDataTag private[TagsAndTags2] ()
extends TypedTag[HTMLElement]("data", Nil, false, Namespace.htmlNamespaceConfig)
with Dynamic {
final def selectDynamic(tagName: String): ConcreteHtmlTag[Element] = {
TagsAndTags2.tag(tagName)
}
}

override lazy val data = new DynamicDataTag()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A good hack 👍
If you like it, I like too 😄


}

@inline
def domBindingSeq(bindingSeq: BindingSeq[Node]) = bindingSeq
Expand Down Expand Up @@ -308,7 +324,7 @@ object dom {
private def transformedWithValDefs: PartialFunction[Tree, (Queue[ValDef], Tree)] = {
case tree @ NodeBuffer(children) =>
nodeSeq(children)
case tree @ Elem(UnprefixedName(label), attributes, _, children) =>
case tree @ Elem(tag, attributes, _, children) =>
val idOption = findTextAttribute("local-id", attributes).orElse(findTextAttribute("id", attributes))
val elementName = idOption match {
case None => TermName(c.freshName("element"))
Expand All @@ -323,16 +339,7 @@ object dom {
}
}
} yield {
val attributeAccess = key match {
case UnprefixedName(localPart) =>
val keyName = TermName(NameTransformer.encode(localPart))
q"""$elementName.$keyName"""
case PrefixedName(prefix, localPart) =>
localPart.split(':').foldLeft(q"""$elementName.${TermName(NameTransformer.encode(prefix))}""") {
(prefixExpr, propertyName) =>
q"""$prefixExpr.${TermName(NameTransformer.encode(propertyName))}"""
}
}
val attributeAccess = propertyAccess(key, q"$elementName")

atPos(value.pos) {
value match {
Expand Down Expand Up @@ -381,8 +388,10 @@ object dom {
"""
})
}
val elementDef =
q"val $elementName = _root_.com.thoughtworks.binding.dom.Runtime.TagsAndTags2.${TermName(label)}.render"

val tagAccess = propertyAccess(tag, q"_root_.com.thoughtworks.binding.dom.Runtime.TagsAndTags2")

val elementDef = q"val $elementName = $tagAccess.render"
idOption match {
case None =>
valDefs -> q"""
Expand All @@ -405,6 +414,18 @@ object dom {
attributes.collectFirst { case (UnprefixedName(`unprefixedName`), Text(text)) => text }
}

private def propertyAccess(xmlName: QName, objectAccess: RefTree): Select = {
xmlName match {
case UnprefixedName(localPart) =>
q"$objectAccess.${TermName(NameTransformer.encode(localPart))}"
case PrefixedName(prefix, localPart) =>
localPart.split(':').foldLeft(q"$objectAccess.${TermName(NameTransformer.encode(prefix))}") {
(prefixExpr, segmentName) =>
q"$prefixExpr.${TermName(NameTransformer.encode(segmentName))}"
Copy link
Collaborator

@Atry Atry Oct 21, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change enables svg tags in the future by simply adding an additional val svg: scalatags.jsdom.SvgTags.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh! Very good! I am glad to hear it.
But when I was writing this, I was thinking about different thing (I am planning to propose a discussion about this in near future 😉). In any case, I suppose that support of an element namespace is a useful feature for Binding.scala

}
}
}

private def transformed: PartialFunction[Tree, Tree] = {
case Block(stats, expr) =>
super.transform(Block(stats.flatMap {
Expand Down
10 changes: 10 additions & 0 deletions dom/src/test/scala/com/thoughtworks/binding/domTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -437,5 +437,15 @@ final class domTest extends FreeSpec with Matchers {
assert(div.value.className == "DIV-1")
}

"CustomTag" in {
@dom def tag = {
<data:custom-tag local-id="customTag" id="custom" data:custom-key="value"><data id="123">{customTag.tagName}</data></data:custom-tag>
}
val div = document.createElement("div")
dom.render(div, tag)

assert(div.outerHTML == """<div><custom-tag custom-key="value" id="custom"><data id="123">CUSTOM-TAG</data></custom-tag></div>""")
}

}