Skip to content

Commit 7f4f496

Browse files
committed
fix(inline-svg): better dimension better
1 parent 6ee403a commit 7f4f496

2 files changed

Lines changed: 9 additions & 2 deletions

File tree

.changeset/twenty-knives-punch.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@svelte-put/inline-svg': patch
3+
---
4+
5+
support wider range of possible dimension syntaxes (#372)

packages/inline-svg/src/action/internals.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@
44
* @typedef {{ width: number } | { height: number }} DimensionConstraint
55
*/
66

7+
// Matches a CSS / SVG dimension: signed decimal or scientific number + optional unit (letters or %).
8+
const DIMENSION_REGEX = /^([+-]?(?:\d+(?:\.\d+)?|\.\d+)(?:[eE][+-]?\d+)?)\s*([a-zA-Z%]+)?$/;
9+
710
/**
811
* @package
912
* @param {string} dimension
1013
* @returns {{ number: number; unit: string }}
1114
*/
1215
export function extractDimensionNumberAndUnit(dimension) {
13-
const regex = /(\d+)(\D*)/;
14-
const [, number = '0', unit = ''] = dimension.match(regex) || [];
16+
const [, number = '0', unit = ''] = dimension.match(DIMENSION_REGEX) || [];
1517
return { number: parseInt(number, 10), unit };
1618
}
1719

0 commit comments

Comments
 (0)