Skip to content

Commit 4542e7a

Browse files
committed
feat: 🎸 add URI encoding Handlebars helpers
1 parent ccdb9fb commit 4542e7a

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

‎x-pack/plugins/drilldowns/url_drilldown/public/lib/url_drilldown.test.ts‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,7 @@ describe('encoding', () => {
461461
openInNewTab: false,
462462
};
463463
const url = await urlDrilldown.getHref(config, context);
464+
464465
expect(url).toBe('https://elastic.co?foo=head%2526shoulders');
465466
});
466467

@@ -473,6 +474,7 @@ describe('encoding', () => {
473474
encodeUrl: true,
474475
};
475476
const url = await urlDrilldown.getHref(config, context);
477+
476478
expect(url).toBe('https://elastic.co?foo=head%2526shoulders');
477479
});
478480

@@ -485,6 +487,33 @@ describe('encoding', () => {
485487
encodeUrl: false,
486488
};
487489
const url = await urlDrilldown.getHref(config, context);
490+
488491
expect(url).toBe('https://elastic.co?foo=head%26shoulders');
489492
});
493+
494+
test('can encode URI component using "encodeURIComponent" Handlebars helper', async () => {
495+
const config: Config = {
496+
url: {
497+
template: 'https://elastic.co?foo={{encodeURIComponent "head%26shoulders@gmail.com"}}',
498+
},
499+
openInNewTab: false,
500+
encodeUrl: false,
501+
};
502+
const url = await urlDrilldown.getHref(config, context);
503+
504+
expect(url).toBe('https://elastic.co?foo=head%2526shoulders%40gmail.com');
505+
});
506+
507+
test('can encode URI component using "encodeURIQuery" Handlebars helper', async () => {
508+
const config: Config = {
509+
url: {
510+
template: 'https://elastic.co?foo={{encodeURIQuery "head%26shoulders@gmail.com"}}',
511+
},
512+
openInNewTab: false,
513+
encodeUrl: false,
514+
};
515+
const url = await urlDrilldown.getHref(config, context);
516+
517+
expect(url).toBe('https://elastic.co?foo=head%2526shoulders@gmail.com');
518+
});
490519
});

‎x-pack/plugins/ui_actions_enhanced/public/drilldowns/url_drilldown/url_template.ts‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { encode, RisonValue } from 'rison-node';
99
import dateMath from '@elastic/datemath';
1010
import moment, { Moment } from 'moment';
1111
import numeral from '@elastic/numeral';
12+
import { url } from '../../../../../../src/plugins/kibana_utils/public';
1213

1314
const handlebars = createHandlebars();
1415

@@ -116,13 +117,22 @@ handlebars.registerHelper('replace', (...args) => {
116117
return String(str).split(searchString).join(valueString);
117118
});
118119

120+
handlebars.registerHelper('encodeURIComponent', (component: unknown) => {
121+
const str = String(component);
122+
return encodeURIComponent(str);
123+
});
124+
handlebars.registerHelper('encodeURIQuery', (component: unknown) => {
125+
const str = String(component);
126+
return url.encodeUriQuery(str);
127+
});
128+
119129
export function compile(urlTemplate: string, context: object, doEncode: boolean = true): string {
120130
const handlebarsTemplate = handlebars.compile(urlTemplate, { strict: true, noEscape: true });
121-
let url: string = handlebarsTemplate(context);
131+
let processedUrl: string = handlebarsTemplate(context);
122132

123133
if (doEncode) {
124-
url = encodeURI(url);
134+
processedUrl = encodeURI(processedUrl);
125135
}
126136

127-
return url;
137+
return processedUrl;
128138
}

0 commit comments

Comments
 (0)