From de313de8f9d001f97bc9a8e3bc12a6cd11fa1c1a Mon Sep 17 00:00:00 2001 From: David Ortner Date: Wed, 13 Nov 2024 01:50:18 +0100 Subject: [PATCH] fix: [#1601] Strings should be converted into numbers in setters for HTMLMeterElement and HTMLProgressElement --- .../html-meter-element/HTMLMeterElement.ts | 18 ++++++++++++------ .../HTMLProgressElement.ts | 6 ++++-- .../HTMLMeterElement.test.ts | 5 +++++ .../HTMLProgressElement.test.ts | 14 ++++++++++++++ 4 files changed, 35 insertions(+), 8 deletions(-) diff --git a/packages/happy-dom/src/nodes/html-meter-element/HTMLMeterElement.ts b/packages/happy-dom/src/nodes/html-meter-element/HTMLMeterElement.ts index 91c3cf887..f2ce12440 100644 --- a/packages/happy-dom/src/nodes/html-meter-element/HTMLMeterElement.ts +++ b/packages/happy-dom/src/nodes/html-meter-element/HTMLMeterElement.ts @@ -32,7 +32,8 @@ export default class HTMLMeterElement extends HTMLElement { * @param high High. */ public set high(high: number) { - if (typeof high !== 'number') { + high = typeof high !== 'number' ? Number(high) : high; + if (isNaN(high)) { throw new this[PropertySymbol.window].TypeError( "Failed to set the 'high' property on 'HTMLMeterElement': The provided double value is non-finite." ); @@ -63,7 +64,8 @@ export default class HTMLMeterElement extends HTMLElement { * @param low Low. */ public set low(low: number) { - if (typeof low !== 'number') { + low = typeof low !== 'number' ? Number(low) : low; + if (isNaN(low)) { throw new this[PropertySymbol.window].TypeError( "Failed to set the 'low' property on 'HTMLMeterElement': The provided double value is non-finite." ); @@ -94,7 +96,8 @@ export default class HTMLMeterElement extends HTMLElement { * @param max Max. */ public set max(max: number) { - if (typeof max !== 'number') { + max = typeof max !== 'number' ? Number(max) : max; + if (isNaN(max)) { throw new this[PropertySymbol.window].TypeError( "Failed to set the 'max' property on 'HTMLMeterElement': The provided double value is non-finite." ); @@ -125,7 +128,8 @@ export default class HTMLMeterElement extends HTMLElement { * @param min Min. */ public set min(min: number) { - if (typeof min !== 'number') { + min = typeof min !== 'number' ? Number(min) : min; + if (isNaN(min)) { throw new this[PropertySymbol.window].TypeError( "Failed to set the 'min' property on 'HTMLMeterElement': The provided double value is non-finite." ); @@ -159,7 +163,8 @@ export default class HTMLMeterElement extends HTMLElement { * @param optimum Optimum. */ public set optimum(optimum: number) { - if (typeof optimum !== 'number') { + optimum = typeof optimum !== 'number' ? Number(optimum) : optimum; + if (isNaN(optimum)) { throw new this[PropertySymbol.window].TypeError( "Failed to set the 'optimum' property on 'HTMLMeterElement': The provided double value is non-finite." ); @@ -190,7 +195,8 @@ export default class HTMLMeterElement extends HTMLElement { * @param value Value. */ public set value(value: number) { - if (typeof value !== 'number') { + value = typeof value !== 'number' ? Number(value) : value; + if (isNaN(value)) { throw new this[PropertySymbol.window].TypeError( "Failed to set the 'value' property on 'HTMLMeterElement': The provided double value is non-finite." ); diff --git a/packages/happy-dom/src/nodes/html-progress-element/HTMLProgressElement.ts b/packages/happy-dom/src/nodes/html-progress-element/HTMLProgressElement.ts index 60928e444..96fa9ff9a 100644 --- a/packages/happy-dom/src/nodes/html-progress-element/HTMLProgressElement.ts +++ b/packages/happy-dom/src/nodes/html-progress-element/HTMLProgressElement.ts @@ -32,7 +32,8 @@ export default class HTMLProgressElement extends HTMLElement { * @param max Max. */ public set max(max: number) { - if (typeof max !== 'number') { + max = typeof max !== 'number' ? Number(max) : max; + if (isNaN(max)) { throw new this[PropertySymbol.window].TypeError( "Failed to set the 'max' property on 'HTMLProgressElement': The provided double value is non-finite." ); @@ -63,7 +64,8 @@ export default class HTMLProgressElement extends HTMLElement { * @param value Value. */ public set value(value: number) { - if (typeof value !== 'number') { + value = typeof value !== 'number' ? Number(value) : value; + if (isNaN(value)) { throw new this[PropertySymbol.window].TypeError( "Failed to set the 'value' property on 'HTMLProgressElement': The provided double value is non-finite." ); diff --git a/packages/happy-dom/test/nodes/html-meter-element/HTMLMeterElement.test.ts b/packages/happy-dom/test/nodes/html-meter-element/HTMLMeterElement.test.ts index aa1117f14..92e6ad6e8 100644 --- a/packages/happy-dom/test/nodes/html-meter-element/HTMLMeterElement.test.ts +++ b/packages/happy-dom/test/nodes/html-meter-element/HTMLMeterElement.test.ts @@ -94,6 +94,11 @@ describe('HTMLMeterElement', () => { expect(element.getAttribute(property.name)).toBe('0.5'); }); + it('Should convert strings to number.', () => { + element[property.name] = '0.5'; + expect(element.getAttribute(property.name)).toBe('0.5'); + }); + it('Should throw an error when the value is not a number.', () => { expect(() => { element[property.name] = 'invalid'; diff --git a/packages/happy-dom/test/nodes/html-progress-element/HTMLProgressElement.test.ts b/packages/happy-dom/test/nodes/html-progress-element/HTMLProgressElement.test.ts index 0ba991664..7a750787a 100644 --- a/packages/happy-dom/test/nodes/html-progress-element/HTMLProgressElement.test.ts +++ b/packages/happy-dom/test/nodes/html-progress-element/HTMLProgressElement.test.ts @@ -56,6 +56,13 @@ describe('HTMLProgressElement', () => { expect(element.getAttribute('max')).toBe('0.5'); }); + it('Should convert strings to numbers.', () => { + element.max = ('2'); + expect(element.getAttribute('max')).toBe('2'); + element.max = ('0.5'); + expect(element.getAttribute('max')).toBe('0.5'); + }); + it('Should throw an error when the value is not a number.', () => { expect(() => { element.max = ('invalid'); @@ -103,6 +110,13 @@ describe('HTMLProgressElement', () => { expect(element.getAttribute('value')).toBe('0.5'); }); + it('Should convert strings to numbers.', () => { + element.value = ('2'); + expect(element.getAttribute('value')).toBe('2'); + element.value = ('0.5'); + expect(element.getAttribute('value')).toBe('0.5'); + }); + it('Should throw an error when the value is not a number.', () => { expect(() => { element.value = ('invalid');