Skip to content
Snippets Groups Projects
Verified Commit 602c1604 authored by Volker Schukai's avatar Volker Schukai :alien:
Browse files

fix: Updated regex to capture the negative sign

parent 7fd44c5f
No related branches found
No related tags found
No related merge requests found
...@@ -74,7 +74,7 @@ function getDeviceDPI() { ...@@ -74,7 +74,7 @@ function getDeviceDPI() {
*/ */
function convertToPixels(value, parentElement = document.documentElement, fontSizeElement = document.documentElement) { function convertToPixels(value, parentElement = document.documentElement, fontSizeElement = document.documentElement) {
const regex = /^([\d.]+)(.*)$/; const regex = /^(-?[\d.]+)(.*)$/;
const matchResult = value.match(regex); const matchResult = value.match(regex);
if (!matchResult) { if (!matchResult) {
...@@ -82,7 +82,6 @@ function convertToPixels(value, parentElement = document.documentElement, fontSi ...@@ -82,7 +82,6 @@ function convertToPixels(value, parentElement = document.documentElement, fontSi
} }
const [, num, unit] = matchResult; const [, num, unit] = matchResult;
const number = parseFloat(num); const number = parseFloat(num);
const dpi = getDeviceDPI(); const dpi = getDeviceDPI();
......
...@@ -71,6 +71,15 @@ describe('dimension', () => { ...@@ -71,6 +71,15 @@ describe('dimension', () => {
expect(errorFn).to.throw(Error, `Invalid value format: ${invalidValue}`); 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', () => { it('should correctly convert em values', () => {
const testElement = document.createElement('div'); const testElement = document.createElement('div');
testElement.style.fontSize = '16px'; testElement.style.fontSize = '16px';
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment