Skip to content

Commit 4633162

Browse files
fix(mf2): When serialising, escape literal {} in quoted patterns
1 parent 3784e74 commit 4633162

File tree

1 file changed

+1
-6
lines changed

1 file changed

+1
-6
lines changed

mf2/messageformat/src/data-model/stringify.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,18 +103,13 @@ function stringifyPattern(pattern: Pattern, quoted: boolean) {
103103
quoted = true;
104104
}
105105
for (const el of pattern) {
106-
if (typeof el === 'string') res += stringifyString(el, quoted);
106+
if (typeof el === 'string') res += el.replace(/[\\{}]/g, '\\$&');
107107
else if (el.type === 'markup') res += stringifyMarkup(el);
108108
else res += stringifyExpression(el);
109109
}
110110
return quoted ? `{{${res}}}` : res;
111111
}
112112

113-
function stringifyString(str: string, quoted: boolean) {
114-
const esc = quoted ? /[\\|]/g : /[\\{}]/g;
115-
return str.replace(esc, '\\$&');
116-
}
117-
118113
function stringifyExpression({ arg, attributes, functionRef }: Expression) {
119114
let res: string;
120115
switch (arg?.type) {

0 commit comments

Comments
 (0)