question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow SVG edition using jsdom + SVG.js

See original GitHub issue

Basic info:

  • Node.js version: 11.13.0
  • jsdom version: 15.1.1

The Problem

To easily edit SVG on node using jsdom + SVG.js, we need jsdom to implement (at least) this methods:

  • SVGGraphicsElement::getBBox()
  • SVGSVGElement::createSVGRect()
  • SVGSVGElement::createSVGPoint()
  • SVGSVGElement::createSVGMatrix()

Minimal reproduction case (pure jsdom)

const { JSDOM } = require('jsdom')

dom = new JSDOM(`
<svg id="test" xmlns="http://www.w3.org/2000/svg" width="50px" height="50px">
<path d="M0,0 L50,50"/>
</svg>
`)
elSVG = dom.window.document.body.children[0]

// All this will fail:
elSVG.firstElementChild.getBBox()
elSVG.createSVGRect()
elSVG.createSVGPoint()
elSVG.createSVGMatrix()

Minimal reproduction case (With svg.js)

This is going to crash by the same reason, but it is a (near to) real use case.

const { JSDOM } = require('jsdom')
const svgFactory = require('svg.js')

dom = new JSDOM(`
<svg id="test" xmlns="http://www.w3.org/2000/svg" width="50px" height="50px">
<path d="M0,0 L50,50"/>
</svg>
`)

SVG = svgFactory(dom.window)
elSVG = dom.window.document.body.children[0]
draw = SVG(elSVG)

line = SVG.select('#test path').members[0]
line.move(10, 20)

How does similar code behave in browsers?

It simply retun values from getBBox(), createSVGRect(), createSVGPoint(), createSVGMatrix() as expected.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:22
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
jcubiccommented, Apr 29, 2020

Good news that there is some progress, if you want to look at my code (mixed ES5 and ES6 I don’t like to use ES6 classes if I don’t need to)

https://gist.github.com/jcubic/2a31f3d123f2044ceeffa70d7831fc75

I was not sure how to throw DOM exception the w3c spec only say about error code.

Read more comments on GitHub >

github_iconTop Results From Across the Web

jsdom not support svg? - Stack Overflow
So, I use jQuery switch it to typeof 'Node' and try function createSVGPoint: $(svg. outerHtml)[0]. createSVGPoint , the result is: undefined -- ...
Read more >
Draw SVG with NodeJS in backend Server - Google Groups
I used JSDOM library to emulate a dom and draw svg but an error occured : --> jQuery requires a window with a...
Read more >
SVG.js (@svg_js) / Twitter
A lightweight library for manipulating and animating SVG. ... And what do you wish for in a version 4? 2. 1. 4. SVG.js...
Read more >
Love Generating SVG With JavaScript? Move It To The Server!
To enable the user to download a graphic. If we don't want to scare the user with this “unknown” format, then we should...
Read more >
dom-to-svg-cjs - npm
Take SVG screenshots of DOM elements. Latest version: 0.1.3, last published: a year ago. Start using dom-to-svg-cjs in your project by ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found