diff --git a/application/source/dom/dimension.mjs b/application/source/dom/dimension.mjs index e8fdb5e5ea158dc700cb6d22d939a1afa96c6303..5148e754d49061844c8021381db2256226fed6e8 100644 --- a/application/source/dom/dimension.mjs +++ b/application/source/dom/dimension.mjs @@ -74,7 +74,7 @@ function getDeviceDPI() { */ function convertToPixels(value, parentElement = document.documentElement, fontSizeElement = document.documentElement) { - const regex = /^([\d.]+)(.*)$/; + const regex = /^(-?[\d.]+)(.*)$/; const matchResult = value.match(regex); if (!matchResult) { @@ -82,7 +82,6 @@ function convertToPixels(value, parentElement = document.documentElement, fontSi } const [, num, unit] = matchResult; - const number = parseFloat(num); const dpi = getDeviceDPI(); diff --git a/development/test/cases/dom/dimension.mjs b/development/test/cases/dom/dimension.mjs index 6cf31dd23f74787c74e066390be27907b1845383..5ca418e28d50e8644627b34f8c818e813b4d7b18 100644 --- a/development/test/cases/dom/dimension.mjs +++ b/development/test/cases/dom/dimension.mjs @@ -69,7 +69,16 @@ describe('dimension', () => { }; expect(errorFn).to.throw(Error, `Invalid value format: ${invalidValue}`); - }); + }); + + it("should handle negative values correctly", () => { + const negativeValue = "-10px"; + const expectedResult = -10; + + const result = convertToPixels(negativeValue); + + expect(result).to.equal(expectedResult); + }); it('should correctly convert em values', () => { const testElement = document.createElement('div');