Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions packages/compiler/src/i18n/i18n_parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -453,8 +453,11 @@ ${nodes.map((node) => `"${node.sourceSpan.toString()}"`).join('\n')}
}
}

const _CUSTOM_PH_EXP =
/\/\/[\s\S]*i18n[\s\S]*\([\s\S]*ph[\s\S]*=[\s\S]*("|')([\s\S]*?)\1[\s\S]*\)/g;
// The separators inside a `//i18n(ph="name")` annotation are only ever whitespace, so bind them
// to `\s*` and stop the name at the delimiter quote. The previous `[\s\S]*` between each token let
// a run of `i18n(ph=` characters be partitioned in many ways, so an unterminated annotation made
// this match backtrack catastrophically.
const _CUSTOM_PH_EXP = /\/\/\s*i18n\s*\(\s*ph\s*=\s*("|')((?:(?!\1)[\s\S])*)\1\s*\)/g;

function extractPlaceholderName(input: string): string {
return input.split(_CUSTOM_PH_EXP)[2];
Expand Down
11 changes: 11 additions & 0 deletions packages/compiler/test/i18n/i18n_parser_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,17 @@ describe('I18nParser', () => {
[[`[before, <ph name="TEST"> exp //i18n(ph='teSt') </ph>, after]`], 'm', 'd', ''],
]);
});

it('should not backtrack on an unterminated placeholder annotation', () => {
// A `//i18n(ph=...` annotation that never closes must fall back to the default
// INTERPOLATION placeholder quickly instead of hanging while extracting the name.
const expression = '//' + 'i18n(ph='.repeat(200);
const start = Date.now();
expect(_humanizeMessages(`<div i18n="m|d">before{{ ${expression} }}after</div>`)).toEqual([
[[`[before, <ph name="INTERPOLATION"> ${expression} </ph>, after]`], 'm', 'd', ''],
]);
expect(Date.now() - start).toBeLessThan(2000);
});
});

describe('blocks', () => {
Expand Down
Loading