Skip to content

Commit d94d176

Browse files
crisbetovivian-hu-zz
authored andcommittedNov 7, 2018
fix(bidi): preserve user dir attribute (#13859)
Currently we normalize any values that aren't `ltr` or `rtl` to `ltr` in order to have a predictable value when using the directionality in components, however this ends up overwriting the consumer's value which might not necessarily be used by Angular (e.g. setting it to `auto`). These changes switch to preserving the attribute while normalizing the value for when it gets injected. Fixes #13855.
1 parent 4ce5ee0 commit d94d176

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed
 

‎src/cdk/bidi/dir.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,19 @@ import {Direction, Directionality} from './directionality';
2626
@Directive({
2727
selector: '[dir]',
2828
providers: [{provide: Directionality, useExisting: Dir}],
29-
host: {'[dir]': 'dir'},
29+
host: {'[attr.dir]': '_rawDir'},
3030
exportAs: 'dir',
3131
})
3232
export class Dir implements Directionality, AfterContentInit, OnDestroy {
33-
_dir: Direction = 'ltr';
33+
/** Normalized direction that accounts for invalid/unsupported values. */
34+
private _dir: Direction = 'ltr';
3435

3536
/** Whether the `value` has been set to its initial value. */
3637
private _isInitialized: boolean = false;
3738

39+
/** Direction as passed in by the consumer. */
40+
_rawDir: string;
41+
3842
/** Event emitted when the direction changes. */
3943
@Output('dirChange') change = new EventEmitter<Direction>();
4044

@@ -43,7 +47,10 @@ export class Dir implements Directionality, AfterContentInit, OnDestroy {
4347
get dir(): Direction { return this._dir; }
4448
set dir(value: Direction) {
4549
const old = this._dir;
50+
51+
this._rawDir = value;
4652
this._dir = (value === 'ltr' || value === 'rtl') ? value : 'ltr';
53+
4754
if (old !== this._dir && this._isInitialized) {
4855
this.change.emit(this._dir);
4956
}

‎src/cdk/bidi/directionality.spec.ts

+19-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ describe('Directionality', () => {
1111

1212
TestBed.configureTestingModule({
1313
imports: [BidiModule],
14-
declarations: [ElementWithDir, InjectsDirectionality],
14+
declarations: [ElementWithDir, ElementWithPredefinedAutoDir, InjectsDirectionality],
1515
providers: [{provide: DIR_DOCUMENT, useFactory: () => fakeDocument}],
1616
}).compileComponents();
1717
}));
@@ -123,6 +123,17 @@ describe('Directionality', () => {
123123
expect(fixture.componentInstance.dir.value).toBe('ltr');
124124
});
125125

126+
it('should preserve the consumer-provided `dir` attribute while ' +
127+
'normalizing the directive value', () => {
128+
const fixture = TestBed.createComponent(ElementWithPredefinedAutoDir);
129+
fixture.detectChanges();
130+
131+
const element = fixture.nativeElement.querySelector('div');
132+
133+
expect(element.getAttribute('dir')).toBe('auto');
134+
expect(fixture.componentInstance.dir.value).toBe('ltr');
135+
});
136+
126137
});
127138
});
128139

@@ -140,6 +151,13 @@ class ElementWithDir {
140151
changeCount = 0;
141152
}
142153

154+
@Component({
155+
template: '<div dir="auto"></div>'
156+
})
157+
class ElementWithPredefinedAutoDir {
158+
@ViewChild(Dir) dir: Dir;
159+
}
160+
143161
/** Test component with Dir directive. */
144162
@Component({
145163
selector: 'injects-directionality',

0 commit comments

Comments
 (0)
Please sign in to comment.