Skip to content

use simple ADT to encode attribute values #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
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
57 changes: 57 additions & 0 deletions snabbdom/src/main/scala/snabbdom/AttrValue.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright 2022 buntec
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/*
* Copyright (c) 2015 Simon Friis Vindum
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/

package snabbdom

sealed trait AttrValue

object AttrValue {

def apply(s: String): AttrValue = StringAttrValue(s)
def apply(b: Boolean): AttrValue = BooleanAttrValue(b)

case class StringAttrValue(value: String) extends AttrValue
case class BooleanAttrValue(value: Boolean) extends AttrValue

implicit def stringToAttrValue(value: String): AttrValue =
Copy link
Owner Author

Choose a reason for hiding this comment

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

I know this is controversial but still seems to be quite common for such cases, see e.g., here

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yeah. I think it goes to @cornerman's question in #14 (comment)

Do we consider this library directly user facing or is it more like a low level library powering, e.g., ff4s and outwatch.

If it's a low-level library, we probably wouldn't need this, right?

StringAttrValue(value)

implicit def booleanToAttrValue(value: Boolean): AttrValue =
BooleanAttrValue(value)

}
33 changes: 19 additions & 14 deletions snabbdom/src/main/scala/snabbdom/modules/Attributes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ package snabbdom.modules

import snabbdom._
import org.scalajs.dom
import snabbdom.AttrValue.BooleanAttrValue
import snabbdom.AttrValue.StringAttrValue

object Attributes {

Expand Down Expand Up @@ -68,20 +70,23 @@ object Attributes {
attrs.foreach { case (key, cur) =>
val old = oldAttrs.get(key)
if (old.forall(_ != cur)) {
if (cur == true) {
elm.setAttribute(key, "")
} else if (cur == false) {
elm.removeAttribute(key)
} else {
if (key.charAt(0) != 'x') {
elm.setAttribute(key, cur.toString)
} else if (key.length > 3 && key.charAt(3) == ':') {
elm.setAttributeNS(xmlNS, key, cur.toString)
} else if (key.length > 5 && key.charAt(5) == ':') {
elm.setAttributeNS(xlinkNS, key, cur.toString)
} else {
elm.setAttribute(key, cur.toString)
}
cur match {
case BooleanAttrValue(value) =>
if (value) {
elm.setAttribute(key, "")
} else {
elm.removeAttribute(key)
}
case StringAttrValue(value) =>
if (key.charAt(0) != 'x') {
elm.setAttribute(key, value)
} else if (key.length > 3 && key.charAt(3) == ':') {
elm.setAttributeNS(xmlNS, key, value)
} else if (key.length > 5 && key.charAt(5) == ':') {
elm.setAttributeNS(xlinkNS, key, value)
} else {
elm.setAttribute(key, value)
}
}
}
}
Expand Down
1 change: 0 additions & 1 deletion snabbdom/src/main/scala/snabbdom/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ package object snabbdom {
type VNodeQueue = mutable.ArrayBuffer[VNode]

type PropValue = Any
type AttrValue = Any // JS snabbdom uses string | number | boolean
type ClassValue = Boolean
type StyleValue = String
type KeyValue = String
Expand Down
6 changes: 5 additions & 1 deletion snabbdom/src/main/scala/snabbdom/toVNode.scala
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,11 @@ object toVNode {
children.append(toVNode(childNode, domApi))
}

if (attrs.nonEmpty) { data.attrs = Some(attrs.toMap) }
if (attrs.nonEmpty) {
data.attrs = Some(attrs.map { case (key, value) =>
key -> AttrValue(value)
}.toMap)
}
if (datasets.nonEmpty) { data.dataset = Some(datasets.toMap) }

if (
Expand Down
41 changes: 31 additions & 10 deletions snabbdom/src/test/scala/snabbdom/AttributesSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class AttributesSuite extends BaseSuite {
VNodeData.builder
.withAttrs(
"href" -> "/foo",
"minlength" -> 1,
"minlength" -> "1",
"selected" -> true,
"disabled" -> false
)
Expand All @@ -75,7 +75,11 @@ class AttributesSuite extends BaseSuite {

vnode0.test("can be memoized") { vnode0 =>
val cachedAttrs = VNodeData.builder
.withAttrs("href" -> "/foo", "minlength" -> 1, "selected" -> true)
.withAttrs(
"href" -> "/foo",
"minlength" -> "1",
"selected" -> true
)
.build
val vnode1 = h("div", cachedAttrs)
val vnode2 = h("div", cachedAttrs)
Expand All @@ -96,8 +100,8 @@ class AttributesSuite extends BaseSuite {
"div",
VNodeData.builder
.withAttrs(
"href" -> None,
"minlength" -> 0,
"href" -> "",
"minlength" -> "0",
"value" -> "",
"title" -> "undefined"
)
Expand All @@ -112,7 +116,10 @@ class AttributesSuite extends BaseSuite {

vnode0.test("are set correctly when namespaced") { vnode0 =>
val vnode1 =
h("div", VNodeData.builder.withAttrs("xlink:href" -> "#foo").build)
h(
"div",
VNodeData.builder.withAttrs("xlink:href" -> "#foo").build
)
val elm = patch(vnode0, vnode1).elm.get.asInstanceOf[dom.Element]
assertEquals(
elm.getAttributeNS("http://www.w3.org/1999/xlink", "href"),
Expand Down Expand Up @@ -148,7 +155,7 @@ class AttributesSuite extends BaseSuite {
VNodeData.builder
.withAttrs(
"required" -> true,
"readonly" -> 1,
"readonly" -> "1",
"noresize" -> "truthy"
)
.build
Expand All @@ -164,7 +171,10 @@ class AttributesSuite extends BaseSuite {

vnode0.test("is omitted if the value is false") { vnode0 =>
val vnode1 =
h("div", VNodeData.builder.withAttrs("required" -> false).build)
h(
"div",
VNodeData.builder.withAttrs("required" -> false).build
)
val elm = patch(vnode0, vnode1).elm.get.asInstanceOf[dom.HTMLElement]
assertEquals(elm.hasAttribute("required"), false)
assertEquals(elm.getAttribute("required"), null)
Expand All @@ -174,7 +184,12 @@ class AttributesSuite extends BaseSuite {
val vnode1 =
h(
"div",
VNodeData.builder.withAttrs("readonly" -> 0, "noresize" -> "").build
VNodeData.builder
.withAttrs(
"readonly" -> "0",
"noresize" -> ""
)
.build
)
val elm = patch(vnode0, vnode1).elm.get.asInstanceOf[dom.HTMLElement]
assertEquals(elm.hasAttribute("readonly"), true)
Expand All @@ -190,12 +205,18 @@ class AttributesSuite extends BaseSuite {
"is not considered as a boolean attribute and shouldn't be omitted"
) { vnode0 =>
val vnode1 =
h("div", VNodeData.builder.withAttrs("constructor" -> true).build)
h(
"div",
VNodeData.builder.withAttrs("constructor" -> true).build
)
val elm1 = patch(vnode0, vnode1).elm.get.asInstanceOf[dom.Element]
assertEquals(elm1.hasAttribute("constructor"), true)
assertEquals(elm1.getAttribute("constructor"), "")
val vnode2 =
h("div", VNodeData.builder.withAttrs("constructor" -> false).build)
h(
"div",
VNodeData.builder.withAttrs("constructor" -> false).build
)
val elm2 = patch(vnode1, vnode2).elm.get.asInstanceOf[dom.Element]
assertEquals(elm2.hasAttribute("constructor"), false)

Expand Down
4 changes: 2 additions & 2 deletions snabbdom/src/test/scala/snabbdom/SnabbdomSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,7 @@ class SnabbdomSuite extends BaseSuite {
onlyAttrs.setAttribute("foo", "bar")
assertEquals(
toVNode(onlyAttrs).data.flatMap(_.attrs).flatMap(_.get("foo")),
Some("bar")
Some(AttrValue("bar"))
)

val onlyDatasets = dom.document.createElement("div")
Expand All @@ -646,7 +646,7 @@ class SnabbdomSuite extends BaseSuite {
bothAttrsAndDatasets.setAttribute("data-foo", "bar")
bothAttrsAndDatasets.dataset("again") = "again"
val data = toVNode(bothAttrsAndDatasets).data.get
assertEquals(data.attrs.flatMap(_.get("foo")), Some("bar"))
assertEquals(data.attrs.flatMap(_.get("foo")), Some(AttrValue("bar")))
assertEquals(data.dataset.flatMap(_.get("foo")), Some("bar"))
assertEquals(data.dataset.flatMap(_.get("again")), Some("again"))

Expand Down