FIX: Prevent producing "undefined" strings (#10042)

Fixes a bug in search-menu-results (type: "group"), where:

```javascript
const fullName = escapeExpression(group.fullName);
const name = escapeExpression(group.name);
const groupNames = [h("span.name", fullName || name)];
```

`groupNames` could end up having value "undefined" if a group doesn't have a `fullName`.
This commit is contained in:
Jarek Radosz 2020-06-17 12:37:06 +02:00 committed by GitHub
parent d66ccabdb1
commit a859d507e7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 0 deletions

View File

@ -25,6 +25,10 @@ export function translateSize(size) {
}
export function escapeExpression(string) {
if (!string) {
return "";
}
// don't escape SafeStrings, since they're already safe
if (string instanceof Handlebars.SafeString) {
return string.toString();

View File

@ -1,5 +1,6 @@
/* global Int8Array:true */
import {
escapeExpression,
emailValid,
extractDomainFromUrl,
avatarUrl,
@ -14,9 +15,30 @@ import {
fillMissingDates,
inCodeBlock
} from "discourse/lib/utilities";
import Handlebars from "handlebars";
QUnit.module("lib:utilities");
QUnit.test("escapeExpression", assert => {
assert.equal(
escapeExpression(">"),
">",
"escapes unsafe characters"
);
assert.equal(
escapeExpression(new Handlebars.SafeString(">")),
">",
"does not double-escape safe strings"
);
assert.equal(
escapeExpression(undefined),
"",
"returns a falsy string when given a falsy value"
);
});
QUnit.test("emailValid", assert => {
assert.ok(
emailValid("Bob@example.com"),