Skip to content

Commit ffbc547

Browse files
authored
propper svg/math elements (#271)
* propper svg/math elements * make namespaces var * simplify toTag/toEventName defs, and dont uppercase toTag * fix class-name update of svg elements
1 parent 8e0f471 commit ffbc547

File tree

3 files changed

+38
-22
lines changed

3 files changed

+38
-22
lines changed

karax.nimble

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Package
22

3-
version = "1.3.0"
3+
version = "1.3.1"
44
author = "Andreas Rumpf"
55
description = "Karax is a framework for developing single page applications in Nim."
66
license = "MIT"

karax/karax.nim

+18-3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ export kdom.Event, kdom.Blob
77
when defined(nimNoNil):
88
{.experimental: "notnil".}
99

10+
proc createElementNS(document: Document, namespace, tag: cstring): Node {.importjs: "#.createElementNS(@)".}
11+
proc `classBaseVal=`(n: Node, v: cstring) {.importjs: "#.className.baseVal = #".}
12+
1013
proc kout*[T](x: T) {.importc: "console.log", varargs.}
1114
## The preferred way of debugging karax applications.
1215

@@ -196,7 +199,13 @@ proc toDom*(n: VNode; useAttachedNode: bool; kxi: KaraxInstance = nil): Node =
196199
attach n
197200
return result
198201
else:
199-
result = document.createElement(toTag[n.kind])
202+
result =
203+
if n.kind in svgElements:
204+
document.createElementNS(svgNamespace, toTag[n.kind])
205+
elif n.kind in mathElements:
206+
document.createElementNS(mathNamespace, toTag[n.kind])
207+
else:
208+
document.createElement(toTag[n.kind])
200209
attach n
201210
for k in n:
202211
appendChild(result, toDom(k, useAttachedNode, kxi))
@@ -206,7 +215,10 @@ proc toDom*(n: VNode; useAttachedNode: bool; kxi: KaraxInstance = nil): Node =
206215
if n.id != nil:
207216
result.id = n.id
208217
if n.class != nil:
209-
result.class = n.class
218+
if n.kind in svgElements:
219+
result.classBaseVal = n.class.cstring
220+
else:
221+
result.class = n.class
210222
#if n.key >= 0:
211223
# result.key = n.key
212224
for k, v in attrs(n):
@@ -329,7 +341,10 @@ proc updateStyles(newNode, oldNode: VNode) =
329341
applyStyle(oldNode.dom, newNode.style)
330342
newNode.styleVersion = newNode.style.version
331343
else: oldNode.dom.style = Style()
332-
oldNode.dom.class = newNode.class
344+
if oldNode.kind in svgElements:
345+
oldNode.dom.classBaseVal = newNode.class
346+
else:
347+
oldNode.dom.class = newNode.class
333348
oldNode.style = newNode.style
334349
oldNode.class = newNode.class
335350

karax/vdom.nim

+19-18
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,13 @@ const
6868
selfClosing = {area, base, br, col, embed, hr, img, input,
6969
link, meta, param, source, track, wbr}
7070

71+
svgElements* = {animate .. view}
72+
mathElements* = {maction .. semantics}
73+
74+
var
75+
svgNamespace* = "http://www.w3.org/2000/svg"
76+
mathNamespace* = "http://www.w3.org/1998/Math/MathML"
77+
7178
type
7279
EventKind* {.pure.} = enum ## The events supported by the virtual DOM.
7380
onclick, ## An element is clicked.
@@ -123,24 +130,18 @@ type
123130

124131
onwheel ## fires when the user rotates a wheel button on a pointing device.
125132

126-
macro buildLookupTables(): untyped =
127-
var a = newTree(nnkBracket)
128-
for i in low(VNodeKind)..high(VNodeKind):
129-
let x = $i
130-
let y = if x[0] == '#': x else: toUpperAscii(x)
131-
a.add(newCall("kstring", newLit(y)))
132-
var e = newTree(nnkBracket)
133-
for i in low(EventKind)..high(EventKind):
134-
e.add(newCall("kstring", newLit(substr($i, 2))))
135-
136-
template tmpl(a, e) {.dirty.} =
137-
const
138-
toTag*: array[VNodeKind, kstring] = a
139-
toEventName*: array[EventKind, kstring] = e
140-
141-
result = getAst tmpl(a, e)
142-
143-
buildLookupTables()
133+
const
134+
toTag* = block:
135+
var res: array[VNodeKind, kstring]
136+
for kind in VNodeKind:
137+
res[kind] = kstring($kind)
138+
res
139+
140+
toEventName* = block:
141+
var res: array[EventKind, kstring]
142+
for kind in EventKind:
143+
res[kind] = kstring(($kind)[2..^1])
144+
res
144145

145146
type
146147
EventHandler* = proc (ev: Event; target: VNode) {.closure.}

0 commit comments

Comments
 (0)