From 91980382e899f645c49f89a772b13a0db4c7e036 Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Thu, 18 Aug 2016 13:31:33 -0700 Subject: [PATCH] fix(pipes): remove bidi control chars (#10870) Fix inconsistent results in Edge vs. other browsers. Closes #10080. --- modules/@angular/common/test/pipes/date_pipe_spec.ts | 3 +++ modules/@angular/facade/src/intl.ts | 12 +++++++----- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/modules/@angular/common/test/pipes/date_pipe_spec.ts b/modules/@angular/common/test/pipes/date_pipe_spec.ts index e45dede9ae..5b10845b4f 100644 --- a/modules/@angular/common/test/pipes/date_pipe_spec.ts +++ b/modules/@angular/common/test/pipes/date_pipe_spec.ts @@ -97,6 +97,9 @@ export function main() { expect(pipe.transform(date, 'mediumTime')).toEqual('9:03:01 AM'); expect(pipe.transform(date, 'shortTime')).toEqual('9:03 AM'); }); + + it('should remove bidi control characters', + () => { expect(pipe.transform(date, 'MM/dd/yyyy').length).toEqual(10); }); }); } }); diff --git a/modules/@angular/facade/src/intl.ts b/modules/@angular/facade/src/intl.ts index 7d171f492d..0ebe6b69b5 100644 --- a/modules/@angular/facade/src/intl.ts +++ b/modules/@angular/facade/src/intl.ts @@ -140,11 +140,15 @@ function hourExtracter(inner: (date: Date, locale: string) => string): ( }; } +function intlDateFormat(date: Date, locale: string, options: Intl.DateTimeFormatOptions): string { + return new Intl.DateTimeFormat(locale, options).format(date).replace(/[\u200e\u200f]/g, ''); +} + function timeZoneGetter(timezone: string): (date: Date, locale: string) => string { // To workaround `Intl` API restriction for single timezone let format with 24 hours - const format = {hour: '2-digit', hour12: false, timeZoneName: timezone}; + const options = {hour: '2-digit', hour12: false, timeZoneName: timezone}; return function(date: Date, locale: string): string { - const result = new Intl.DateTimeFormat(locale, format).format(date); + const result = intlDateFormat(date, locale, options); // Then extract first 3 letters that related to hours return result ? result.substring(3) : ''; }; @@ -177,9 +181,7 @@ function combine(options: Intl.DateTimeFormatOptions[]): Intl.DateTimeFormatOpti function datePartGetterFactory(ret: Intl.DateTimeFormatOptions): (date: Date, locale: string) => string { - return function(date: Date, locale: string): string { - return new Intl.DateTimeFormat(locale, ret).format(date); - }; + return (date: Date, locale: string): string => intlDateFormat(date, locale, ret); }