Skip to content
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

converters from/to XmlNode #229

Merged
merged 5 commits into from
Aug 24, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
24 changes: 15 additions & 9 deletions karax/xdom.nim
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
import std/[xmltree, strtabs, strutils]
import std/[xmltree, htmlparser, strtabs, strutils]
import ./vdom

converter toXmlNode*(el: VNode): XmlNode =
let xAttrs = @[].toXmlAttributes
for k, v in el.attrs:
xAttrs[k] = v
var kids: seq[XmlNode]
if el.len > 0:
for k in el:
kids.add k.toXmlNode
result = newXmlTree($el.kind, kids, attributes = xAttrs)
case el.kind:
of VNodeKind.verbatim:
parseHtml($el)
of VNodeKind.text:
newText(el.text)
else:
let xAttrs = @[].toXmlAttributes
for k, v in el.attrs:
xAttrs[k] = v
var kids: seq[XmlNode]
if el.len > 0:
for k in el:
kids.add k.toXmlNode
newXmlTree($el.kind, kids, attributes = xAttrs)
Araq marked this conversation as resolved.
Show resolved Hide resolved

converter toVNode*(el: XmlNode): VNode =
try:
Expand Down
13 changes: 10 additions & 3 deletions tests/xmlNodeConversionTests.nim
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
import xmltree
import std/xmltree
import "../karax"/[vdom, xdom]

let xn = newXmlTree("div", @[], attributes = [("a", "b")].toXmlAttributes)
let vn = tree(tdiv, attrs = (@[("a", "b"), ("c", "d")]))
vn.add newVNode(a)
vn[0].add vn("bbb")
vn.add verbatim("<app>abc</app>")
xn.add newXmlTree("i", @[], attributes = [("a", "b")].toXmlAttributes)

doassert $vn.toXmlNode == """<div c="d" a="b">
<a />
let vnX = vn.toXmlNode

doassert vnX[0].len == 1 and vnX[1][0].kind == xnText
doassert $vnX[1].tag == "app"
doassert $vnX == """<div c="d" a="b">
<a>bbb</a>
<app>abc</app>
</div>"""
doassert $xn.toVNode == """<div a="b"><i a="b"></i></div>"""