Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: [#1601] Strings should be converted into numbers in setters for HTMLMeterElement and HTMLProgressElement #1607

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading