Skip to content

Commit

Permalink
fix: [#1601] Strings should be converted into numbers in setters for …
Browse files Browse the repository at this point in the history
…HTMLMeterElement and HTMLProgressElement
  • Loading branch information
capricorn86 committed Nov 13, 2024
1 parent 9f8f084 commit de313de
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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."
);
Expand Down Expand Up @@ -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."
);
Expand Down Expand Up @@ -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."
);
Expand Down Expand Up @@ -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."
);
Expand Down Expand Up @@ -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."
);
Expand Down Expand Up @@ -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."
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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."
);
Expand Down Expand Up @@ -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."
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ describe('HTMLProgressElement', () => {
expect(element.getAttribute('max')).toBe('0.5');
});

it('Should convert strings to numbers.', () => {
element.max = <number>(<unknown>'2');
expect(element.getAttribute('max')).toBe('2');
element.max = <number>(<unknown>'0.5');
expect(element.getAttribute('max')).toBe('0.5');
});

it('Should throw an error when the value is not a number.', () => {
expect(() => {
element.max = <number>(<unknown>'invalid');
Expand Down Expand Up @@ -103,6 +110,13 @@ describe('HTMLProgressElement', () => {
expect(element.getAttribute('value')).toBe('0.5');
});

it('Should convert strings to numbers.', () => {
element.value = <number>(<unknown>'2');
expect(element.getAttribute('value')).toBe('2');
element.value = <number>(<unknown>'0.5');
expect(element.getAttribute('value')).toBe('0.5');
});

it('Should throw an error when the value is not a number.', () => {
expect(() => {
element.value = <number>(<unknown>'invalid');
Expand Down

0 comments on commit de313de

Please sign in to comment.