Skip to content

Commit a518cb3

Browse files
crisbetojelbourn
authored andcommitted
fix(bidi): default invalid directionality values to ltr (#12396)
Fixes invalid directionality values being accepted as they are, rather than defaulting to ltr. Browsers fall back to ltr anyway, however having an invalid value can break some of our component that could be checking for one of the correct values explicitly.
1 parent 82228bd commit a518cb3

File tree

3 files changed

+27
-5
lines changed

3 files changed

+27
-5
lines changed

src/cdk/bidi/dir.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ export class Dir implements Directionality, AfterContentInit, OnDestroy {
4141
/** @docs-private */
4242
@Input()
4343
get dir(): Direction { return this._dir; }
44-
set dir(v: Direction) {
44+
set dir(value: Direction) {
4545
const old = this._dir;
46-
this._dir = v;
46+
this._dir = (value === 'ltr' || value === 'rtl') ? value : 'ltr';
4747
if (old !== this._dir && this._isInitialized) {
4848
this.change.emit(this._dir);
4949
}

src/cdk/bidi/directionality.spec.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {async, fakeAsync, TestBed} from '@angular/core/testing';
2-
import {Component} from '@angular/core';
2+
import {Component, ViewChild} from '@angular/core';
33
import {By} from '@angular/platform-browser';
4-
import {BidiModule, Directionality, Direction, DIR_DOCUMENT} from './index';
4+
import {BidiModule, Directionality, Dir, Direction, DIR_DOCUMENT} from './index';
55

66
describe('Directionality', () => {
77
let fakeDocument: FakeDocument;
@@ -55,6 +55,15 @@ describe('Directionality', () => {
5555
subscription.unsubscribe();
5656
});
5757

58+
it('should default to ltr if an invalid direction is set on the body', () => {
59+
fakeDocument.body.dir = 'not-valid';
60+
61+
const fixture = TestBed.createComponent(InjectsDirectionality);
62+
const testComponent = fixture.debugElement.componentInstance;
63+
64+
expect(testComponent.dir.value).toBe('ltr');
65+
});
66+
5867
});
5968

6069
describe('Dir directive', () => {
@@ -103,6 +112,17 @@ describe('Directionality', () => {
103112
subscription.unsubscribe();
104113
}));
105114

115+
it('should default to ltr if an invalid value is passed in', () => {
116+
const fixture = TestBed.createComponent(ElementWithDir);
117+
118+
fixture.detectChanges();
119+
expect(fixture.componentInstance.dir.value).toBe('rtl');
120+
121+
fixture.componentInstance.direction = 'not-valid';
122+
fixture.detectChanges();
123+
expect(fixture.componentInstance.dir.value).toBe('ltr');
124+
});
125+
106126
});
107127
});
108128

@@ -115,6 +135,7 @@ describe('Directionality', () => {
115135
`
116136
})
117137
class ElementWithDir {
138+
@ViewChild(Dir) dir: Dir;
118139
direction = 'rtl';
119140
changeCount = 0;
120141
}

src/cdk/bidi/directionality.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ export class Directionality implements OnDestroy {
3333
// but getComputedStyle return either "ltr" or "rtl". avoiding getComputedStyle for now
3434
const bodyDir = _document.body ? _document.body.dir : null;
3535
const htmlDir = _document.documentElement ? _document.documentElement.dir : null;
36-
this.value = (bodyDir || htmlDir || 'ltr') as Direction;
36+
const value = bodyDir || htmlDir;
37+
this.value = (value === 'ltr' || value === 'rtl') ? value : 'ltr';
3738
}
3839
}
3940

0 commit comments

Comments
 (0)