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

[RFC] Namespace support in knete #185

Closed
wants to merge 13 commits into from

Conversation

planetis-m
Copy link
Contributor

@planetis-m planetis-m commented Feb 7, 2021

example:

import std / dom

window.onLoad = proc (e: Event) =
  let svgdiv = document.getElementById("svg1")
  for k in 1 .. 2:
    let svg = buildHtml svg(width = "100", height = "100"):
      circle(cx = "50", cy = "50", r = "40", stroke = "blue",
             stroke-width = "4", fill = "currentColor", class = "icon")
    svgdiv.add(svg)
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
  <script src="tsvg.js"></script>
  <style>
    .icon {
      color: red
    }
  </style>
</head>
<body>
</head>
<body>
  <div id="svg1"></div>
</body>
</html>

@planetis-m
Copy link
Contributor Author

For it to work, setting the className was replaced with setAttribute("class", x). See also facebook/react#1590

@planetis-m
Copy link
Contributor Author

Problems with it:

  • needs an buildNS parentNamespace overload

@Araq
Copy link
Collaborator

Araq commented Feb 8, 2021

I don't understand this PR. Knete is code duplication and should be refactored. It's also not documented, is it? Do you want to use Knete?

@planetis-m
Copy link
Contributor Author

planetis-m commented Feb 8, 2021

I don't understand this PR. Knete is code duplication and should be refactored. It's also not documented, is it? Do you want to use Knete?

I'm trying it yes. My previous attempt #179 to add svg support to karax failed, I tried to work on knete instead, being just a thin dsl on top of the dom.

So my example produces:

let ns_369098792 = getChildNamespace(Namespace.none, Tag.svg)
let tmp_369098791 = newNode(ns_369098792, Tag.svg)
setAttr(tmp_369098791, "width", "100")
setAttr(tmp_369098791, "height", "100")
let ns_369098799 = getChildNamespace(ns_369098792, Tag.circle)
let tmp_369098798 = newNode(ns_369098799, Tag.circle)
setAttr(tmp_369098798, "cx", "50")
setAttr(tmp_369098798, "cy", "50")
setAttr(tmp_369098798, "r", "40")
setAttr(tmp_369098798, "stroke", "blue")
setAttr(tmp_369098798, "stroke-width", "4")
setAttr(tmp_369098798, "fill", "currentColor")
setAttr(tmp_369098798, "class", "icon")
add(tmp_369098791, tmp_369098798)

which is equivalent to:

let svg = document.createElementNS("http://www.w3.org/2000/svg", "svg")
svg.setAttribute("width", "100")
svg.setAttribute("height", "100")
let c = document.createElementNS("http://www.w3.org/2000/svg", "circle")
c.setAttribute("cx", "50")
c.setAttribute("cy", "50")
c.setAttribute("r", "40")
c.setAttribute("stroke", "green")
c.setAttribute("stroke-width", "4")
c.setAttribute("fill", "yellow")
svg.appendChild(c)

...but the logic for determining the correct namespace is moved at runtime (see the getChildNamespace proc). That makes svg as well as mathml elements render properly.

@planetis-m
Copy link
Contributor Author

planetis-m commented Feb 8, 2021

I think it would be better to add a buildSvg overload for karax, but I'm waiting for your feedback and suggestions.

Main problems with that are: nodeName doesn't return all uppercase when inside the svg/mathml namespace, which breaks karax.same. Moreover the xml standard is case-sensitive, meaning createElementNS("SVG") or createElementNS("MATH") don't work.

@geotre
Copy link
Collaborator

geotre commented Oct 20, 2023

Closing this as we added SVG support in #271

@geotre geotre closed this Oct 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants