Skip to content

Commit eb9e939

Browse files
vbfoxdayongkr
andauthored
fix(compat/template): disable ES interpolation with custom 'interpolate' (toss#1527)
This behaviour is documented in Lodash examples at https://lodash.com/docs/4.17.15#template : ``` // Use the ES template literal delimiter as an "interpolate" delimiter. // Disable support by replacing the "interpolate" delimiter. ``` Co-authored-by: Dayong Lee <[email protected]>
1 parent 335011b commit eb9e939

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

src/compat/string/template.spec.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,14 @@ describe('template', () => {
121121
expect(template('${"{" + value + "\\}"}')(data)).toBe('{2}');
122122
});
123123

124+
it('should disable ES6 template delimiters when "interpolate" is set', () => {
125+
const data = { value: 2 };
126+
expect(template('1${value}3', { interpolate: /<%=([\s\S]+?)%>/g })(data)).toBe('1${value}3');
127+
expect(template('${"{" + value + "\\}"}', { interpolate: /<%=([\s\S]+?)%>/g })(data)).toBe(
128+
'${"{" + value + "\\}"}'
129+
);
130+
});
131+
124132
it('should support the "imports" option', () => {
125133
const compiled = template('<%= a %>', { imports: { a: 1 } });
126134
expect(compiled({})).toBe('1');

src/compat/string/template.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,13 @@ function escapeString(match: string): string {
2525
return `\\${escapeMap.get(match)}`;
2626
}
2727

28+
const defaultInterpolateRegExp = /<%=([\s\S]+?)%>/g;
29+
2830
// Only import the necessary functions for preventing circular dependencies.(lodash-es also does this)
2931
export const templateSettings = {
3032
escape: /<%-([\s\S]+?)%>/g,
3133
evaluate: /<%([\s\S]+?)%>/g,
32-
interpolate: /<%=([\s\S]+?)%>/g,
34+
interpolate: defaultInterpolateRegExp,
3335
variable: '',
3436
imports: {
3537
_: {
@@ -130,7 +132,7 @@ export function template(string?: string, options?: TemplateOptions, guard?: obj
130132
[
131133
options.escape?.source ?? noMatchExp.source,
132134
options.interpolate?.source ?? noMatchExp.source,
133-
options.interpolate ? esTemplateRegExp.source : noMatchExp.source,
135+
options.interpolate === defaultInterpolateRegExp ? esTemplateRegExp.source : noMatchExp.source,
134136
options.evaluate?.source ?? noMatchExp.source,
135137
'$',
136138
].join('|'),

0 commit comments

Comments
 (0)