fix(ivy): i18n - trim whitespace when parsing metadata (#34154)

It is possible for HTML formatters to add whitespace
around the content of `i18n` attribute values. This can
make the meaning and custom ids brittle to simple
whitespace formatting.

This commit ensures that the metadata string extracted
from HTML `i18n` attributes is trimmed before being parsed
into meaning, description and custom id.

PR Close #34154
This commit is contained in:
Pete Bacon Darwin 2019-12-03 08:36:56 +00:00 committed by Miško Hevery
parent 41485599f3
commit 6b1a47183d
2 changed files with 9 additions and 1 deletions

View File

@ -201,11 +201,12 @@ const I18N_ID_SEPARATOR = '@@';
* @param meta String that represents i18n meta
* @returns Object with id, meaning and description fields
*/
export function parseI18nMeta(meta?: string): I18nMeta {
export function parseI18nMeta(meta: string = ''): I18nMeta {
let customId: string|undefined;
let meaning: string|undefined;
let description: string|undefined;
meta = meta.trim();
if (meta) {
const idIndex = meta.indexOf(I18N_ID_SEPARATOR);
const descIndex = meta.indexOf(I18N_MEANING_SEPARATOR);

View File

@ -207,6 +207,13 @@ describe('Utils', () => {
expect(parseI18nMeta('meaning|desc')).toEqual(meta('', 'meaning', 'desc'));
expect(parseI18nMeta('meaning|desc@@id')).toEqual(meta('id', 'meaning', 'desc'));
expect(parseI18nMeta('@@id')).toEqual(meta('id', '', ''));
expect(parseI18nMeta('\n ')).toEqual(meta());
expect(parseI18nMeta('\n desc\n ')).toEqual(meta('', '', 'desc'));
expect(parseI18nMeta('\n desc@@id\n ')).toEqual(meta('id', '', 'desc'));
expect(parseI18nMeta('\n meaning|desc\n ')).toEqual(meta('', 'meaning', 'desc'));
expect(parseI18nMeta('\n meaning|desc@@id\n ')).toEqual(meta('id', 'meaning', 'desc'));
expect(parseI18nMeta('\n @@id\n ')).toEqual(meta('id', '', ''));
});
it('serializeI18nHead()', () => {