Skip to content
Permalink
Browse files

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
  • Loading branch information
petebacondarwin authored and mhevery committed Dec 3, 2019
1 parent 5cf5843 commit f4d714ca0bc538860480048cf2ea2f3a8419973a
@@ -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);
@@ -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()', () => {

0 comments on commit f4d714c

Please sign in to comment.
You can’t perform that action at this time.