Repository navigation
Open
Labels
Description
If we use builtin isEmpty on empty SafeString instance, we getting incorrect result.
import { htmlSafe } from '@ember/template';
import { isEmpty } from '@ember/utils';
isEmpty(htmlSafe('')) === falseWondering if we should update isEmpty to support SafeString...
Real life use case for error:
{{#if (isEmpty this.someEmptySafeString)}}
it's not rendered, but ideally it should be
{{/if}}Why issue created? Because ember in-tempalte if already counting empty SafeString as false, but there is no official js way to check for empty safe string.
I think, we could workaround this case, extending SafeString class, adding length getter.
export class SafeString implements GlimmerSafeString {
private __string: string;
constructor(string: string) {
this.__string = string;
}
/**
getter to fix isEmpty case
*/
get length() {
return this.__string.length;
}
/**
Get the string back to use as a string.
@public
@method toString
@returns {String} The string marked as trusted
*/
toString(): string {
return `${this.__string}`;
}
/**
Get the wrapped string as HTML to use without escaping.
@public
@method toHTML
@returns {String} the trusted string, without any escaping applied
*/
toHTML(): string {
return this.toString();
}
}Reacted by Javier Gracia Gimeno
Activity
kategengler commentedon Nov 25, 2024
I believe we are unlikely to touch
isEmptyand other similar utilities; I'd generally prefer utilities from other generic libraries.lifeart commentedon Nov 25, 2024
@kategengler how about adding
lengthtoSafeString?kategengler commentedon Nov 25, 2024
That seems more likely.
[-]Should `isEmpty` consider "empty" SafeString?[/-][+]Add `length` to `SafeString`[/+]