TextPanel: Prevent panel crashing when interpolating variables results in empty content (#92019)

fix text panel crashing when interpolating variables results in empty content
This commit is contained in:
Ashley Harrison 2024-08-19 09:45:58 +01:00 committed by GitHub
parent 134467fc4a
commit dcdef1a02d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 13 additions and 3 deletions

View File

@ -60,6 +60,15 @@ describe('TextPanel', () => {
expect(() => setup()).not.toThrow(); expect(() => setup()).not.toThrow();
}); });
it('should not throw an error when interpolating variables results in empty content', () => {
const contentTest = '${__all_variables}';
const props = Object.assign({}, defaultProps, {
options: { content: contentTest, mode: TextMode.HTML },
});
expect(() => setup(props)).not.toThrow();
});
it('sanitizes content in html mode', () => { it('sanitizes content in html mode', () => {
const contentTest = '<form><p>Form tags are sanitized.</p></form>\n<script>Script tags are sanitized.</script>'; const contentTest = '<form><p>Form tags are sanitized.</p></form>\n<script>Script tags are sanitized.</script>';
replaceVariablesMock.mockReturnValueOnce(contentTest); replaceVariablesMock.mockReturnValueOnce(contentTest);

View File

@ -64,14 +64,15 @@ export function TextPanel(props: Props) {
function processContent(options: Options, interpolate: InterpolateFunction, disableSanitizeHtml: boolean): string { function processContent(options: Options, interpolate: InterpolateFunction, disableSanitizeHtml: boolean): string {
let { mode, content } = options; let { mode, content } = options;
if (!content) {
return ' ';
}
// Variables must be interpolated before content is converted to markdown so using variables // Variables must be interpolated before content is converted to markdown so using variables
// in URLs work properly // in URLs work properly
content = interpolate(content, {}, options.code?.language === 'json' ? 'json' : 'html'); content = interpolate(content, {}, options.code?.language === 'json' ? 'json' : 'html');
if (!content) {
return ' ';
}
switch (mode) { switch (mode) {
case TextMode.Code: case TextMode.Code:
break; // nothing break; // nothing