Skip to content

Unexpected behaviour for falsy values when using arrays in class binding #48473

Closed

Description

@undsoft

Which @angular/* package(s) are the source of the bug?

common

Is this a regression?

No

Description

<div 
  [class]="['as-array', 'as-array2']" 
  [class.specific]="true"
></div>
// Applied: All classes are applied

<div
  [class]="['as-array', '', 'as-array2']" 
  [class.specific]="true"
></div>
// Applied: .specific

<div 
  [class]="['as-array', false, 'as-array2']" 
  [class.specific]="true"
></div>
// Applied: .as-array .false .as-array2 .specific

<div 
  [class]="['as-array', undefined, 'as-array2']" 
  [class.specific]="true"
></div>
// Applied: .as-array

<div 
  [class]="['as-array', null, 'as-array2']" 
  [class.specific]="true"
></div>
// Applied: No classes were applied.

My expectation is basically that falsy values in an array will not be printed and will not affect other class declarations.
Basically I always expect to get .as-array .as-array2 .specific.

I cannot find logic in how it current works. There are 4 different behaviours based on what falsy value is used.
This can lead to unexpected behaviour and hard to diagnose errors.

Please provide a link to a minimal reproduction of the bug

https://stackblitz.com/edit/angular-ivy-amqsbq?file=src/app/app.component.htmlpp%2Fapp.component.html

Please provide the exception or error you saw

No response

Please provide the environment you discovered this bug in (run ng version)

Angular 15.0.2
Node v16.16.0

Anything else?

No response

Activity

  1. added
    area: common
    on Dec 13, 2022
  2. added this to the needsTriage milestone on Dec 13, 2022
  3. pkozlowski-opensource commented on Dec 14, 2022

    @pkozlowski-opensource
    Member

    Fixed stackblitz: https://stackblitz.com/edit/angular-ivy-g2fcyh?file=src%2Fapp%2Fapp.module.ts,src%2Fapp%2Fapp.component.html,src%2Fapp%2Fapp.component.ts

    This is a confirmed bug. What is going on here is that the styling algorithm doesn't seem to account for non-string and empty string values. Those unchecked values are propagated to the other parts of the framework and throw exceptions in other parts (DOM etc.).

    While this might look like a corner cases, I can understand how people might want to use ternary expressions to have conditional classes (and evaluate to an empty string / null / undefined if a given class should not be applied).

    We should fix it, although I'm not sure what is the best approach here: throw for "invalid" values or just ignore them.

  4. added
    area: core
    P3
    and removed
    area: common
    on Dec 14, 2022
  5. modified the milestones: needsTriage, Backlog on Dec 14, 2022
  6. pritam-chowdhury commented on Dec 27, 2022

    @pritam-chowdhury

    @undsoft I think that any null or falsy value shouldn't affect the other class declaration. We can still show warning like "class declaration shouldn't contain any null or falsy values".. After showing this warning we should ignore falsy values and continue to apply other class declaration

  7. added a commit that references this issue on Apr 19, 2023
    39b9beb
  8. self-assigned this
    on Apr 19, 2023
  9. added a commit that references this issue on Apr 19, 2023
    a3c3ab7
  10. added a commit that references this issue on Apr 19, 2023
    2fff8fa
  11. angular-automatic-lock-bot commented on May 20, 2023

    @angular-automatic-lock-bot

    This issue has been automatically locked due to inactivity.
    Please file a new issue if you are encountering a similar or related problem.

    Read more about our automatic conversation locking policy.

    This action has been performed automatically by a bot.

  12. locked and limited conversation to collaborators on May 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

P3area: corebugcore: styling bindingsstate: has PR

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions