Skip to content

fix(bidi): default invalid directionality values to ltr #12396

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/cdk/bidi/dir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ export class Dir implements Directionality, AfterContentInit, OnDestroy {
/** @docs-private */
@Input()
get dir(): Direction { return this._dir; }
set dir(v: Direction) {
set dir(value: Direction) {
const old = this._dir;
this._dir = v;
this._dir = (value === 'ltr' || value === 'rtl') ? value : 'ltr';
if (old !== this._dir && this._isInitialized) {
this.change.emit(this._dir);
}
Expand Down
25 changes: 23 additions & 2 deletions src/cdk/bidi/directionality.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {async, fakeAsync, TestBed} from '@angular/core/testing';
import {Component} from '@angular/core';
import {Component, ViewChild} from '@angular/core';
import {By} from '@angular/platform-browser';
import {BidiModule, Directionality, Direction, DIR_DOCUMENT} from './index';
import {BidiModule, Directionality, Dir, Direction, DIR_DOCUMENT} from './index';

describe('Directionality', () => {
let fakeDocument: FakeDocument;
Expand Down Expand Up @@ -55,6 +55,15 @@ describe('Directionality', () => {
subscription.unsubscribe();
});

it('should default to ltr if an invalid direction is set on the body', () => {
fakeDocument.body.dir = 'not-valid';

const fixture = TestBed.createComponent(InjectsDirectionality);
const testComponent = fixture.debugElement.componentInstance;

expect(testComponent.dir.value).toBe('ltr');
});

});

describe('Dir directive', () => {
Expand Down Expand Up @@ -103,6 +112,17 @@ describe('Directionality', () => {
subscription.unsubscribe();
}));

it('should default to ltr if an invalid value is passed in', () => {
const fixture = TestBed.createComponent(ElementWithDir);

fixture.detectChanges();
expect(fixture.componentInstance.dir.value).toBe('rtl');

fixture.componentInstance.direction = 'not-valid';
fixture.detectChanges();
expect(fixture.componentInstance.dir.value).toBe('ltr');
});

});
});

Expand All @@ -115,6 +135,7 @@ describe('Directionality', () => {
`
})
class ElementWithDir {
@ViewChild(Dir) dir: Dir;
direction = 'rtl';
changeCount = 0;
}
Expand Down
3 changes: 2 additions & 1 deletion src/cdk/bidi/directionality.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ export class Directionality implements OnDestroy {
// but getComputedStyle return either "ltr" or "rtl". avoiding getComputedStyle for now
const bodyDir = _document.body ? _document.body.dir : null;
const htmlDir = _document.documentElement ? _document.documentElement.dir : null;
this.value = (bodyDir || htmlDir || 'ltr') as Direction;
const value = bodyDir || htmlDir;
this.value = (value === 'ltr' || value === 'rtl') ? value : 'ltr';
}
}

Expand Down