Skip to content

Commit 28936cd

Browse files
authored
Fix nested elements in SVG (#57)
* Fix nested elements in SVG * Check for null children not just undefined * Test to verify correct namespace is applied to the whole svg tree
1 parent 54de15e commit 28936cd

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

src/Flame/Html/Element.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,12 @@ exports.createSingleSvgNode = function (nodeData) {
167167
};
168168

169169
function asSvg(elements) {
170-
for (let e of elements)
170+
for (let e of elements) {
171171
if (e.nodeType === elementNode)
172172
e.nodeType = svgNode;
173+
if (e.children !== null && typeof e.children !== 'undefined')
174+
e.children = asSvg(e.children);
175+
}
173176

174177
return elements;
175178
}
@@ -222,4 +225,4 @@ function fromNodeData(allData) {
222225
}
223226

224227
return nodeData;
225-
}
228+
}

test/Main.purs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,23 @@ main = TUM.runTest do
387387
text <- textContent "#mount-point"
388388
TUA.equal "oi" text
389389

390+
TU.test "nested svg elements have correct namespace" do
391+
let html = HE.svg
392+
[ HA.viewBox "0 0 100 100"
393+
, HA.createAttribute "xmlns" "http://www.w3.org/2000/svg"
394+
]
395+
[ HE.g [ HA.fill "white", HA.stroke "green", HA.strokeWidth "5" ]
396+
[ HE.circle' [ HA.cx "40", HA.cy "40", HA.r "25" ], HE.circle' [ HA.cx "60", HA.cy "60", HA.r "25" ] ]
397+
]
398+
void $ mountHtml html
399+
let verifyNodeAndChildren node = do
400+
TUA.equal (Just "http://www.w3.org/2000/svg") (WDE.namespaceURI node)
401+
children <- liftEffect $ childrenNode' node
402+
DT.for_ children verifyNodeAndChildren
403+
svg <- liftEffect $ unsafeQuerySelector "svg"
404+
verifyNodeAndChildren svg
405+
406+
390407
TU.suite "dom node update" do
391408
TU.test "update text nodes" do
392409
let html = HE.text "oi"
@@ -986,6 +1003,10 @@ main = TUM.runTest do
9861003
children <- WDP.children $ WDE.toParentNode mountPoint
9871004
WDHC.toArray children
9881005

1006+
childrenNode' parent = do
1007+
children <- WDP.children $ WDE.toParentNode parent
1008+
WDHC.toArray children
1009+
9891010
textContent selector = liftEffect do
9901011
element <- unsafeQuerySelector selector
9911012
WDN.textContent $ WDE.toNode element

0 commit comments

Comments
 (0)