-
Notifications
You must be signed in to change notification settings - Fork 24.8k
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(core): handle invalid classes in class array bindings #49924
Conversation
When binding an array to `class` like `[class]="['foo', 'bar']"`, the runtime treats it the same as a literal binding with all the values being `true`, e.g. `{foo: true, bar: true}`. While object literals can only have string keys, arrays can have any value which can lead to errors if the array contains non-string values. These changes add some logic to stringify the keys and ignore invalid ones. Fixes angular#48473.
// the DOM. The only exceptions are empty strings and strings that contain spaces for which | ||
// the browser throws an error. We ignore such values, because the error is somewhat cryptic. | ||
const stringKey = String(key); | ||
if (stringKey !== '' && !stringKey.includes(' ')) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was on the fence here between throwing an assertion error and ignoring the values. I went with the latter, because the browser behavior currently is a little counter-intuitive. E.g. classList.add
with an array works because [1, 2, 3].toString()
produces an array that doesn't include spaces, but calling it with an object literal is an error, because it stringifies to [object Object]
which contains a space.
// the DOM. The only exceptions are empty strings and strings that contain spaces for which | ||
// the browser throws an error. We ignore such values, because the error is somewhat cryptic. | ||
const stringKey = String(key); | ||
if (stringKey !== '' && !stringKey.includes(' ')) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here I was also on the fence about whether to include the stringKey.includes(' ')
both in dev and production mode, or just in dev mode because there is a tiny performance impact. I went with the former for consistency, but I'm open to moving it to a dev mode assertion.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM I don't have strong opinions either way on the devmode assertion. I say we just go with it and update later if it's an issue.
When binding an array to `class` like `[class]="['foo', 'bar']"`, the runtime treats it the same as a literal binding with all the values being `true`, e.g. `{foo: true, bar: true}`. While object literals can only have string keys, arrays can have any value which can lead to errors if the array contains non-string values. These changes add some logic to stringify the keys and ignore invalid ones. Fixes #48473. PR Close #49924
When binding an array to `class` like `[class]="['foo', 'bar']"`, the runtime treats it the same as a literal binding with all the values being `true`, e.g. `{foo: true, bar: true}`. While object literals can only have string keys, arrays can have any value which can lead to errors if the array contains non-string values. These changes add some logic to stringify the keys and ignore invalid ones. Fixes #48473. PR Close #49924
const stringKey = String(key); | ||
if (stringKey !== '' && !stringKey.includes(' ')) { | ||
keyValueArraySet(keyValueArray, stringKey, value); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@crisbeto nit: should we consider throwing a runtime error or logging a warning (so that it's not breaking), highlighting incompatible values to developers?
This PR contains the following updates: | Package | Type | Update | Change | |---|---|---|---| | [@angular/animations](https://github.com/angular/angular) | dependencies | patch | [`15.2.6` -> `15.2.8`](https://renovatebot.com/diffs/npm/@angular%2fanimations/15.2.6/15.2.8) | | [@angular/common](https://github.com/angular/angular) | dependencies | patch | [`15.2.6` -> `15.2.8`](https://renovatebot.com/diffs/npm/@angular%2fcommon/15.2.6/15.2.8) | | [@angular/compiler](https://github.com/angular/angular) | dependencies | patch | [`15.2.6` -> `15.2.8`](https://renovatebot.com/diffs/npm/@angular%2fcompiler/15.2.6/15.2.8) | | [@angular/compiler-cli](https://github.com/angular/angular/tree/main/packages/compiler-cli) ([source](https://github.com/angular/angular)) | devDependencies | patch | [`15.2.6` -> `15.2.8`](https://renovatebot.com/diffs/npm/@angular%2fcompiler-cli/15.2.6/15.2.8) | | [@angular/core](https://github.com/angular/angular) | dependencies | patch | [`15.2.6` -> `15.2.8`](https://renovatebot.com/diffs/npm/@angular%2fcore/15.2.6/15.2.8) | | [@angular/forms](https://github.com/angular/angular) | dependencies | patch | [`15.2.6` -> `15.2.8`](https://renovatebot.com/diffs/npm/@angular%2fforms/15.2.6/15.2.8) | | [@angular/platform-browser](https://github.com/angular/angular) | dependencies | patch | [`15.2.6` -> `15.2.8`](https://renovatebot.com/diffs/npm/@angular%2fplatform-browser/15.2.6/15.2.8) | | [@angular/platform-browser-dynamic](https://github.com/angular/angular) | dependencies | patch | [`15.2.6` -> `15.2.8`](https://renovatebot.com/diffs/npm/@angular%2fplatform-browser-dynamic/15.2.6/15.2.8) | --- ### Release Notes <details> <summary>angular/angular</summary> ### [`v15.2.8`](https://github.com/angular/angular/blob/HEAD/CHANGELOG.md#​1528-2023-04-19) [Compare Source](angular/angular@15.2.7...15.2.8) ##### core | Commit | Type | Description | | -- | -- | -- | | [2fff8fadbe](angular/angular@2fff8fa) | fix | handle invalid classes in class array bindings ([#​49924](angular/angular#49924)) | ##### http | Commit | Type | Description | | -- | -- | -- | | [05a0225deb](angular/angular@05a0225) | fix | prevent headers from throwing an error when initializing numerical values ([#​49379](angular/angular#49379)) | ##### router | Commit | Type | Description | | -- | -- | -- | | [09a42d988e](angular/angular@09a42d9) | fix | canceledNavigationResolution: 'computed' with redirects to the current URL ([#​49793](angular/angular#49793)) | <!-- CHANGELOG SPLIT MARKER --> ### [`v15.2.7`](https://github.com/angular/angular/blob/HEAD/CHANGELOG.md#​1527-2023-04-12) [Compare Source](angular/angular@15.2.6...15.2.7) ##### compiler | Commit | Type | Description | | -- | -- | -- | | [b0c1a90f55](angular/angular@b0c1a90) | fix | Produce diagnositc if directive used in host binding is not exported ([#​49792](angular/angular#49792)) | ##### compiler-cli | Commit | Type | Description | | -- | -- | -- | | [a40529af2e](angular/angular@a40529a) | fix | Catch FatalDiagnosticError during template type checking ([#​49792](angular/angular#49792)) | ##### core | Commit | Type | Description | | -- | -- | -- | | [702ec90110](angular/angular@702ec90) | fix | When using setInput, mark view dirty in same way as `markForCheck` ([#​49747](angular/angular#49747)) | #### Special Thanks Alan Agius, Andrew Kushnir, Andrew Scott, Kristiyan Kostadinov, Matthieu Riegler and Nikola Kološnjaji <!-- CHANGELOG SPLIT MARKER --> </details> --- ### Configuration 📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined). 🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied. ♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox. 🔕 **Ignore**: Close this PR and you won't be reminded about these updates again. --- - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box --- This PR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate). <!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNS40MS4wIiwidXBkYXRlZEluVmVyIjoiMzUuNjEuMCJ9--> Co-authored-by: cabr2-bot <cabr2.help@gmail.com> Reviewed-on: https://codeberg.org/Calciumdibromid/CaBr2/pulls/1863 Reviewed-by: Epsilon_02 <epsilon_02@noreply.codeberg.org> Co-authored-by: Calciumdibromid Bot <cabr2_bot@noreply.codeberg.org> Co-committed-by: Calciumdibromid Bot <cabr2_bot@noreply.codeberg.org>
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
When binding an array to
class
like[class]="['foo', 'bar']"
, the runtime treats it the same as a literal binding with all the values beingtrue
, e.g.{foo: true, bar: true}
. While object literals can only have string keys, arrays can have any value which can lead to errors if the array contains non-string values.These changes add some logic to stringify the keys and ignore invalid ones.
Fixes #48473.