pawel.panasewicz pawel.panasewicz - 2 months ago 7
Scala Question

scalatags TypedTag from HTMLElement

Is it possible to create

TypedTag
from
HTMLElement
in scalatags?

Building
HTMLElement
from
TypedTag
is straightforward:

import scalatags.JsDom.all._

val typedTag: TypedTag[Div] = div("abc")
val d: Div = typedTag.render
//somwhere `type Div = raw.HTMLDivElement`


Is it possible to create conversion in opposit direction?
Given instance of
Div
I want
TypedTag[Div]
in order to manipulate it using scalatags goodies and then render it back to
HTMLElement
. Does it make sense?

Answer

Sadly, no -- that doesn't actually make any sense if you think it through, at least not that way. The thing is, a TypedTag isn't an HTML element, it's something that builds an HTML element. By contrast, the HTMLElement is an actual element. So basically, you're trying to get a bag of cocoa powder from an already-mixed cup.

I suspect it would be possible to build what you're asking for, but it would be a bit involved -- you'd have to build something that takes an HTMLElement and maps back to a TypedTag parameterized for that element type, then work with the TypedTag, render it and replace the existing HTMLElement in-page. It could be done, but I suspect it's more work than it's worth...

Comments