Skip to content

Commit c534748

Browse files
committed
Added SVG validation: width and height must be specified #2195 #2062
1 parent 4a4bc05 commit c534748

3 files changed

Lines changed: 14 additions & 1 deletion

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## Unreleased
4+
5+
- Added SVG validation: width and height must be specified (in SVG string/element or `svg` node)
6+
37
## 0.2.22 - 2026-01-07
48

59
- Added object type validation for parameters in method `createPdf` and `createPdfKitDocument`

src/docMeasure.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,15 @@ DocMeasure.prototype.measureSVG = function (node) {
215215

216216
node.font = this.styleStack.getProperty('font');
217217

218+
// SVG requires a defined width and height
219+
if (!isNumber(node._width) && !isNumber(node._height)) {
220+
throw new Error('SVG is missing defined width and height.');
221+
} else if (!isNumber(node._width)) {
222+
throw new Error('SVG is missing defined width.');
223+
} else if (!isNumber(node._height)) {
224+
throw new Error('SVG is missing defined height.');
225+
}
226+
218227
// scale SVG based on final dimension
219228
node.svg = this.svgMeasure.writeDimensions(node.svg, {
220229
width: node._width,

src/helpers.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ function isString(variable) {
55
}
66

77
function isNumber(variable) {
8-
return typeof variable === 'number' || variable instanceof Number;
8+
return ((typeof variable === 'number') || (variable instanceof Number)) && !Number.isNaN(variable);
99
}
1010

1111
function isBoolean(variable) {

0 commit comments

Comments
 (0)