mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
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:
parent
134467fc4a
commit
dcdef1a02d
@ -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);
|
||||||
|
@ -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
|
||||||
|
Loading…
Reference in New Issue
Block a user