-
Notifications
You must be signed in to change notification settings - Fork 3.4k
fix(chips): use empty chip buffer if not a string #9885
Conversation
* Sometimes the ngModel of the custom input element (provided by the user) does not have any initial $viewValue. * The chips component always expects the chipBuffer to be a string and a non-string reference can cause Runtime Exceptions. Fixes angular#9867.
this.userInputElement[0].value; | ||
|
||
// Ensure that the chip buffer is always a string. For example, the input element buffer might be falsy. | ||
return angular.isString(chipBuffer) ? chipBuffer : ''; |
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.
Is it ever possible for chipBuffer
to be something like 3
instead of the string '3'
? If so, this may fail?
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.
Yeah, it could happen if the developer uses a custom input element or for example the md-autocomplete
(and does have some custom ng-model
s)
This is kind of a safety check to ensure that there will be not Runtime errors ever.
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.
Indeed; I'm just wondering if we could alter this to cast the input to a string instead of returning an empty string.
So, perhaps something like:
return angular.isDefined(chipBuffer) ? chipBuffer + '' : ''
This will ensure that it is always a string, or the empty string, but should handle more cases. Thoughts?
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 thought a lot about this, and figured that just returning an empty string is the safest & cleanest approach IMO
I think casting would open more questions than it would solve, because then users might get weird chips like [Object]
, rather than not showing any chip.
I used the
isString
to avoid objects being used as truthy chip buffers.
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 👍
return !this.userInputElement ? this.chipBuffer : | ||
this.userInputNgModelCtrl ? this.userInputNgModelCtrl.$viewValue : | ||
this.userInputElement[0].value; | ||
var chipBuffer = !this.userInputElement ? this.chipBuffer : |
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.
Seems like this code was here from before, but it might be a good chance to get rid of the nested ternary expression since it's a little hard to read/follow. Also the this.userInputElement[0].value
can be reduced to this.userInputElement.val()
.
Should this JSDoc annotation be changed if this.chipBuffer is not necessarily a string? |
@ppham27 I think that's okay the way it is. Although you can technically force it to be something other than a string, it should always be a string. |
@ppham27 Yeah this is fine. Also the |
Fixes #9867.