From 18d3bc05bbb98b4cdbfcd057b996101cb8dc0453 Mon Sep 17 00:00:00 2001 From: crisbeto Date: Wed, 9 Jan 2019 22:30:12 +0100 Subject: [PATCH] fix(bidi): handle uppercase values correctly The native `dir` attribute is case-insensitive, however our `dir` directive will normalize something like `RTL` to `ltr`. These changes account for different cases of the values. --- src/cdk/bidi/dir.ts | 3 ++- src/cdk/bidi/directionality.spec.ts | 22 +++++++++++++++++++++- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/cdk/bidi/dir.ts b/src/cdk/bidi/dir.ts index ba76bb27af33..2244cea3c8bb 100644 --- a/src/cdk/bidi/dir.ts +++ b/src/cdk/bidi/dir.ts @@ -47,9 +47,10 @@ export class Dir implements Directionality, AfterContentInit, OnDestroy { get dir(): Direction { return this._dir; } set dir(value: Direction) { const old = this._dir; + const normalizedValue = value ? value.toLowerCase() : value; this._rawDir = value; - this._dir = (value === 'ltr' || value === 'rtl') ? value : 'ltr'; + this._dir = (normalizedValue === 'ltr' || normalizedValue === 'rtl') ? normalizedValue : 'ltr'; if (old !== this._dir && this._isInitialized) { this.change.emit(this._dir); diff --git a/src/cdk/bidi/directionality.spec.ts b/src/cdk/bidi/directionality.spec.ts index da0c3dc87838..9a3c25b66928 100644 --- a/src/cdk/bidi/directionality.spec.ts +++ b/src/cdk/bidi/directionality.spec.ts @@ -11,7 +11,12 @@ describe('Directionality', () => { TestBed.configureTestingModule({ imports: [BidiModule], - declarations: [ElementWithDir, ElementWithPredefinedAutoDir, InjectsDirectionality], + declarations: [ + ElementWithDir, + ElementWithPredefinedAutoDir, + InjectsDirectionality, + ElementWithPredefinedUppercaseDir, + ], providers: [{provide: DIR_DOCUMENT, useFactory: () => fakeDocument}], }).compileComponents(); })); @@ -134,6 +139,13 @@ describe('Directionality', () => { expect(fixture.componentInstance.dir.value).toBe('ltr'); }); + it('should be case-insensitive', () => { + const fixture = TestBed.createComponent(ElementWithPredefinedUppercaseDir); + fixture.detectChanges(); + + expect(fixture.componentInstance.dir.value).toBe('rtl'); + }); + }); }); @@ -158,6 +170,14 @@ class ElementWithPredefinedAutoDir { @ViewChild(Dir) dir: Dir; } +@Component({ + template: '
' +}) +class ElementWithPredefinedUppercaseDir { + @ViewChild(Dir) dir: Dir; +} + + /** Test component with Dir directive. */ @Component({ selector: 'injects-directionality',