mirror of
https://github.com/angular/angular.git
synced 2026-12-06 05:09:14 -05:00
According to the HTML specification most attributes are defined as strings, however some can be interpreted as different types like booleans or numbers. [In the HTML standard](https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#boolean-attributes), boolean attributes are considered `true` if they are present on a DOM node and `false` if they are omitted. Common examples of boolean attributes are `disabled` on interactive elements like `<button>` or `checked` on `<input type="checkbox">`. Another example of an attribute that is defined as a string, but interpreted as a different type is the `value` attribute of `<input type="number">` which logs a warning and ignores the value if it can't be parsed as a number. Historically, authoring Angular inputs that match the native behavior in a type-safe way has been difficult for developers, because Angular interprets all static attributes as strings. While some recent TypeScript versions made this easier by allowing setters and getters to have different types, supporting this pattern still requires a lot of boilerplate and additional properties to be declared. For example, currently developers have to write something like this to have a `disabled` input that behaves like the native one: ```typescript import {Directive, Input} from '@angular/core'; @Directive({selector: 'mat-checkbox'}) export class MatCheckbox { @Input() get disabled() { return this._disabled; } set disabled(value: any) { this._disabled = typeof value === 'boolean' ? value : (value != null && value !== 'false'); } private _disabled = false; } ``` This feature aims to address the issue by introducing a `transform` property on inputs. If an input has a `transform` function, any values set through the template will be passed through the function before being assigned to the directive instance. The example from above can be rewritten to the following: ```typescript import {Directive, Input, booleanAttribute} from '@angular/core'; @Directive({selector: 'mat-checkbox'}) export class MatCheckbox { @Input({transform: booleanAttribute}) disabled: boolean = false; } ``` These changes also add the `booleanAttribute` and `numberAttribute` utilities to `@angular/core` since they're common enough to be useful for most projects. Fixes #8968. Fixes #14761. PR Close #50420
134 lines
4.2 KiB
TypeScript
134 lines
4.2 KiB
TypeScript
/**
|
|
* @license
|
|
* Copyright Google LLC All Rights Reserved.
|
|
*
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
* found in the LICENSE file at https://angular.io/license
|
|
*/
|
|
import {booleanAttribute, numberAttribute} from '@angular/core';
|
|
|
|
describe('coercion functions', () => {
|
|
describe('booleanAttribute', () => {
|
|
it('should coerce undefined to false', () => {
|
|
expect(booleanAttribute(undefined)).toBe(false);
|
|
});
|
|
|
|
it('should coerce null to false', () => {
|
|
expect(booleanAttribute(null)).toBe(false);
|
|
});
|
|
|
|
it('should coerce the empty string to true', () => {
|
|
expect(booleanAttribute('')).toBe(true);
|
|
});
|
|
|
|
it('should coerce zero to true', () => {
|
|
expect(booleanAttribute(0)).toBe(true);
|
|
});
|
|
|
|
it('should coerce the string "false" to false', () => {
|
|
expect(booleanAttribute('false')).toBe(false);
|
|
});
|
|
|
|
it('should coerce the boolean false to false', () => {
|
|
expect(booleanAttribute(false)).toBe(false);
|
|
});
|
|
|
|
it('should coerce the boolean true to true', () => {
|
|
expect(booleanAttribute(true)).toBe(true);
|
|
});
|
|
|
|
it('should coerce the string "true" to true', () => {
|
|
expect(booleanAttribute('true')).toBe(true);
|
|
});
|
|
|
|
it('should coerce an arbitrary string to true', () => {
|
|
expect(booleanAttribute('pink')).toBe(true);
|
|
});
|
|
|
|
it('should coerce an object to true', () => {
|
|
expect(booleanAttribute({})).toBe(true);
|
|
});
|
|
|
|
it('should coerce an array to true', () => {
|
|
expect(booleanAttribute([])).toBe(true);
|
|
});
|
|
});
|
|
|
|
describe('numberAttribute', () => {
|
|
it('should coerce undefined to the default value', () => {
|
|
expect(numberAttribute(undefined)).toBeNaN();
|
|
expect(numberAttribute(undefined, 111)).toBe(111);
|
|
});
|
|
|
|
it('should coerce null to the default value', () => {
|
|
expect(numberAttribute(null)).toBeNaN();
|
|
expect(numberAttribute(null, 111)).toBe(111);
|
|
});
|
|
|
|
it('should coerce true to the default value', () => {
|
|
expect(numberAttribute(true)).toBeNaN();
|
|
expect(numberAttribute(true, 111)).toBe(111);
|
|
});
|
|
|
|
it('should coerce false to the default value', () => {
|
|
expect(numberAttribute(false)).toBeNaN();
|
|
expect(numberAttribute(false, 111)).toBe(111);
|
|
});
|
|
|
|
it('should coerce the empty string to the default value', () => {
|
|
expect(numberAttribute('')).toBeNaN();
|
|
expect(numberAttribute('', 111)).toBe(111);
|
|
});
|
|
|
|
it('should coerce the string "1" to 1', () => {
|
|
expect(numberAttribute('1')).toBe(1);
|
|
expect(numberAttribute('1', 111)).toBe(1);
|
|
});
|
|
|
|
it('should coerce the string "123.456" to 123.456', () => {
|
|
expect(numberAttribute('123.456')).toBe(123.456);
|
|
expect(numberAttribute('123.456', 111)).toBe(123.456);
|
|
});
|
|
|
|
it('should coerce the string "-123.456" to -123.456', () => {
|
|
expect(numberAttribute('-123.456')).toBe(-123.456);
|
|
expect(numberAttribute('-123.456', 111)).toBe(-123.456);
|
|
});
|
|
|
|
it('should coerce an arbitrary string to the default value', () => {
|
|
expect(numberAttribute('pink')).toBeNaN();
|
|
expect(numberAttribute('pink', 111)).toBe(111);
|
|
});
|
|
|
|
it('should coerce an arbitrary string prefixed with a number to the default value', () => {
|
|
expect(numberAttribute('123pink')).toBeNaN();
|
|
expect(numberAttribute('123pink', 111)).toBe(111);
|
|
});
|
|
|
|
it('should coerce the number 1 to 1', () => {
|
|
expect(numberAttribute(1)).toBe(1);
|
|
expect(numberAttribute(1, 111)).toBe(1);
|
|
});
|
|
|
|
it('should coerce the number 123.456 to 123.456', () => {
|
|
expect(numberAttribute(123.456)).toBe(123.456);
|
|
expect(numberAttribute(123.456, 111)).toBe(123.456);
|
|
});
|
|
|
|
it('should coerce the number -123.456 to -123.456', () => {
|
|
expect(numberAttribute(-123.456)).toBe(-123.456);
|
|
expect(numberAttribute(-123.456, 111)).toBe(-123.456);
|
|
});
|
|
|
|
it('should coerce an object to the default value', () => {
|
|
expect(numberAttribute({})).toBeNaN();
|
|
expect(numberAttribute({}, 111)).toBe(111);
|
|
});
|
|
|
|
it('should coerce an array to the default value', () => {
|
|
expect(numberAttribute([])).toBeNaN();
|
|
expect(numberAttribute([], 111)).toBe(111);
|
|
});
|
|
});
|
|
});
|