Skip to content

Commit 15090e3

Browse files
committed
feat(core utils): parseLength: handle unitless lengths as pixels.
1 parent c2e1dad commit 15090e3

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

src/core/utils.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ function parseTime(time) {
447447
* parseLength - Parse a length from a string and return the parsed length in
448448
* pixels.
449449
450-
* @param {String} length - A length string like `1px` or `25%`.
450+
* @param {String} length - A length string like `1px` or `25%`. Lengths without a unit are treated as pixels.
451451
* @param {Number} reference_length - The reference length to use for percentage lengths.
452452
*
453453
* @returns {Number} - A integer which represents the parsed length in pixels.
@@ -486,7 +486,7 @@ function parseLength(length, reference_length = null) {
486486
(amount * Math.max(window.innerWidth, window.innerHeight)) / 100
487487
);
488488
default:
489-
return null;
489+
return Math.round(amount);
490490
}
491491
}
492492

src/core/utils.test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,24 @@ describe("parseLength", function () {
480480
expect(p).toThrow();
481481
});
482482

483+
it("handles unitless lengths", function () {
484+
// As strings
485+
expect(utils.parseLength("0")).toBe(0);
486+
expect(utils.parseLength("1")).toBe(1);
487+
expect(utils.parseLength("10")).toBe(10);
488+
expect(utils.parseLength("100")).toBe(100);
489+
expect(utils.parseLength("1000.1")).toBe(1000);
490+
expect(utils.parseLength("1000.9")).toBe(1001);
491+
492+
// As numbers
493+
expect(utils.parseLength(0)).toBe(0);
494+
expect(utils.parseLength(1)).toBe(1);
495+
expect(utils.parseLength(10)).toBe(10);
496+
expect(utils.parseLength(100)).toBe(100);
497+
expect(utils.parseLength(1000.1)).toBe(1000);
498+
expect(utils.parseLength(1000.9)).toBe(1001);
499+
});
500+
483501
it("handles pixel lengths", function () {
484502
expect(utils.parseLength("10px")).toBe(10);
485503
expect(utils.parseLength("100px")).toBe(100);

0 commit comments

Comments
 (0)