
Security News
Crates.io Users Targeted by Phishing Emails
The Rust Security Response WG is warning of phishing emails from rustfoundation.dev targeting crates.io users.
Straightforward DOM implementation to make SVG.js run headless on Node.js
While this dom implementation was designed to run svg.js on node, it now is much more feature complete and can be used by anyone needing an xml, svg or html dom.
for older versions of svg.js checkout older versions of svgdom
npm install @svgdotjs/svg.js svgdom
import { createSVGWindow } from 'svgdom'
import { SVG, registerWindow } from '@svgdotjs/svg.js'
// returns a window with a document and an svg root node
const window = createSVGWindow()
const document = window.document
// register window and document
registerWindow(window, document)
// create canvas
const canvas = SVG(document.documentElement)
// use svg.js as normal
canvas.rect(100, 100).fill('yellow').move(50,50)
// get your svg as string
console.log(canvas.svg())
// or
console.log(canvas.node.outerHTML)
// create HTML window with a document and an html root node
import { createHTMLWindow } from 'svgdom'
const window = createHTMLWindow()
// create XML window with a document and a given xml root node
import { createWindow } from 'svgdom'
const window = createWindow(namespaceURI, rootNode)
// e.g. createWindow('http://www.w3.org/1998/Math/MathML', 'math')
svgdom is used best as esm module. However, if you still require cjs, you have to import the module via the async import function:
const main = async () => {
const { createSVGWindow } = await import('svgdom')
}
main()
In order to calculate bounding boxes for text the font needs to be loaded first. svgdom loads Open Sans-Regular by default when no font file for the specified font was found.
The following options must be set in order to load your own fonts:
import { config } from 'svgdom'
config.
// your font directory
.setFontDir('./fonts')
// map the font-family to the file
.setFontFamilyMappings({'Arial': 'arial.ttf'})
// you can preload your fonts to avoid the loading delay
// when the font is used the first time
.preloadFonts()
// Alternatively you can import the functions itself and use them
const {setFontDir, setFontFamilyMappings, preloadFonts} = require('svgdom')
setFontDir('./fonts')
setFontFamilyMappings({'Arial': 'arial.ttf'})
preloadFonts()
Almost all functions of svg.js work properly with svgdom. However there are a few known limitations:
setFontFamilyMappings({'Arial-italic': 'arial_italic.ttf'})
querySelector only supports the following pseudo classes:
first-childlast-childnth-childnth-last-childfirst-of-typelast-of-typenth-of-typenth-last-of-typeonly-childonly-of-typerootnotmatchesscope# and . are allowed but things like : or [] will break the selectorAlbeit this dom implementation aims to work with svgjs, it is of course possible to use it in your own projects. Keep in mind, that some functions are just not needed in svgjs and therefore not implemented or tested. If you need a certain feature don't hesistate to open an issue or submit a pull request.
Last thing to say: childNodes is an array! (yet)
FAQs
Straightforward DOM implementation for SVG, HTML and XML
The npm package svgdom receives a total of 16,869 weekly downloads. As such, svgdom popularity was classified as popular.
We found that svgdom demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
The Rust Security Response WG is warning of phishing emails from rustfoundation.dev targeting crates.io users.

Product
Socket now lets you customize pull request alert headers, helping security teams share clear guidance right in PRs to speed reviews and reduce back-and-forth.

Product
Socket's Rust support is moving to Beta: all users can scan Cargo projects and generate SBOMs, including Cargo.toml-only crates, with Rust-aware supply chain checks.