Skip to content

Commit ed940bc

Browse files
committed
Added SVG validation: width and height must be specified #2195 #2062
1 parent 0e1fe17 commit ed940bc

4 files changed

Lines changed: 15 additions & 1 deletion

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## Unreleased
44

55
- Fixed non-working `open()` and `print()` methods in browser
6+
- Added SVG validation: width and height must be specified (in SVG string/element or `svg` node)
67

78
## 0.3.1 - 2026-01-07
89

src/DocMeasure.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,15 @@ class DocMeasure {
154154

155155
node.font = this.styleStack.getProperty('font');
156156

157+
// SVG requires a defined width and height
158+
if (!isNumber(node._width) && !isNumber(node._height)) {
159+
throw new Error('SVG is missing defined width and height.');
160+
} else if (!isNumber(node._width)) {
161+
throw new Error('SVG is missing defined width.');
162+
} else if (!isNumber(node._height)) {
163+
throw new Error('SVG is missing defined height.');
164+
}
165+
157166
// scale SVG based on final dimension
158167
node.svg = this.svgMeasure.writeDimensions(node.svg, { width: node._width, height: node._height });
159168

src/helpers/variableType.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export function isString(variable) {
1111
* @returns {boolean}
1212
*/
1313
export function isNumber(variable) {
14-
return (typeof variable === 'number') || (variable instanceof Number);
14+
return ((typeof variable === 'number') || (variable instanceof Number)) && !Number.isNaN(variable);
1515
}
1616

1717
/**

tests/unit/helpers/variableType.spec.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ const variableCheckMap = [
4444
value: new Number(78),
4545
type: [isNumber, isValue]
4646
},
47+
{
48+
value: NaN,
49+
type: [isValue]
50+
},
4751
{
4852
value: true,
4953
type: [isValue]

0 commit comments

Comments
 (0)