Skip to content

Instantly share code, notes, and snippets.

@sermunar
Last active October 17, 2019 11:15
Show Gist options
  • Save sermunar/2625a4557796e50c2d1e121341610dc7 to your computer and use it in GitHub Desktop.
Save sermunar/2625a4557796e50c2d1e121341610dc7 to your computer and use it in GitHub Desktop.
getComputedStyle() does not return default values, e.g svg.fill is "" instead of "rgb(0, 0, 0)"
//compiled by typescript^3.6.4 see #file-ts-3-6-4-for-jsdom-15-2-0.ts
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
//"jsdom": "^15.2.0"
const jsdom_1 = __importDefault(require("jsdom"));
const dom = new jsdom_1.default.JSDOM('<html><body></body></html>');
const win = dom.window;
const doc = win.document;
const node = new win.DOMParser().parseFromString('<svg><g></g></svg>', 'image/svg+xml');
const svgNode = node.childNodes[0];
doc.body.appendChild(svgNode);
const cssStyleDeclaration = win.getComputedStyle(svgNode); //<- crashes because svgNode.style is undefined
console.log(`fill: ${cssStyleDeclaration.fill}`);
"use strict";
//"jsdom": "^9.4.0"
const jsdom = require('jsdom');
const dom = jsdom.jsdom('<html><body></body></html>');
const win = dom.defaultView;
const doc = win.document;
const node = new win.DOMParser().parseFromString('<svg><g></g></svg>', 'image/svg+xml');
const svgNode = node.childNodes[0];
doc.body.appendChild(svgNode);
const cssStyleDeclaration = win.getComputedStyle(svgNode);
//cssStyleDeclaration.fill = ""
console.log(`fill: ${cssStyleDeclaration.fill}`); //"fill: "
//"jsdom": "^15.2.0"
import jsdom from 'jsdom';
const dom = new jsdom.JSDOM('<html><body></body></html>');
const win = dom.window;
const doc = win.document;
const node = new win.DOMParser().parseFromString('<svg><g></g></svg>', 'image/svg+xml');
const svgNode = node.childNodes[0];
doc.body.appendChild(svgNode);
const cssStyleDeclaration = win.getComputedStyle(svgNode as Element); //<- crashes because svgNode is undefined
console.log(`fill: ${cssStyleDeclaration.fill}`);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment