-
Notifications
You must be signed in to change notification settings - Fork 24.7k
/
styling_spec.ts
3508 lines (2940 loc) Β· 123 KB
/
styling_spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {CommonModule} from '@angular/common';
import {Component, ComponentFactoryResolver, ComponentRef, Directive, ElementRef, HostBinding, Input, NgModule, Renderer2, ViewChild, ViewContainerRef} from '@angular/core';
import {bypassSanitizationTrustStyle} from '@angular/core/src/sanitization/bypass';
import {ngDevModeResetPerfCounters} from '@angular/core/src/util/ng_dev_mode';
import {TestBed} from '@angular/core/testing';
import {getElementClasses, getElementStyles, getSortedClassName, getSortedStyle} from '@angular/core/testing/src/styling';
import {By, DomSanitizer, SafeStyle} from '@angular/platform-browser';
import {expect} from '@angular/platform-browser/testing/src/matchers';
import {ivyEnabled, modifiedInIvy, onlyInIvy} from '@angular/private/testing';
describe('styling', () => {
beforeEach(ngDevModeResetPerfCounters);
describe('apply in prioritization order', () => {
it('should perform static bindings', () => {
@Component({template: `<div class="STATIC" style="color: blue"></div>`})
class Cmp {
}
TestBed.configureTestingModule({declarations: [Cmp]});
const fixture = TestBed.createComponent(Cmp);
const [staticDiv] = fixture.nativeElement.querySelectorAll('div');
expect(getSortedClassName(staticDiv)).toEqual('STATIC');
expect(getSortedStyle(staticDiv)).toEqual('color: blue;');
});
it('should perform prop bindings', () => {
@Component({
template: `<div [class.dynamic]="true"
[style.color]="'blue'"
[style.width.px]="100"></div>`
})
class Cmp {
}
TestBed.configureTestingModule({declarations: [Cmp]});
const fixture = TestBed.createComponent(Cmp);
fixture.detectChanges();
const div = fixture.nativeElement.querySelector('div');
expect(getSortedClassName(div)).toEqual('dynamic');
expect(getSortedStyle(div)).toEqual('color: blue; width: 100px;');
});
onlyInIvy('style merging is ivy only feature').it('should perform map bindings', () => {
@Component({
template: `<div [class]="{dynamic: true}"
[style]="{color: 'blue', width: '100px'}"></div>`
})
class Cmp {
}
TestBed.configureTestingModule({declarations: [Cmp]});
const fixture = TestBed.createComponent(Cmp);
fixture.detectChanges();
const div = fixture.nativeElement.querySelector('div');
expect(getSortedClassName(div)).toEqual('dynamic');
expect(getSortedStyle(div)).toEqual('color: blue; width: 100px;');
});
onlyInIvy('style merging is ivy only feature')
.it('should perform interpolation bindings', () => {
@Component({
// TODO(misko): change `style-x` to `style` once #34202 lands
template: `<div class="static {{'dynamic'}}"
style.color="blu{{'e'}}"
style-x="width: {{'100'}}px"></div>`
})
class Cmp {
}
TestBed.configureTestingModule({declarations: [Cmp]});
const fixture = TestBed.createComponent(Cmp);
fixture.detectChanges();
const div = fixture.nativeElement.querySelector('div');
expect(getSortedClassName(div)).toEqual('dynamic static');
expect(getSortedStyle(div)).toEqual('color: blue;');
});
onlyInIvy('style merging is ivy only feature').it('should support hostBindings', () => {
@Component({
template:
`<div my-host-bindings-2 my-host-bindings-1 class="STATIC" style="color: blue"></div>`
})
class Cmp {
}
@Directive({
selector: '[my-host-bindings-1]',
host: {'class': 'HOST_STATIC_1', 'style': 'font-family: "c1"'}
})
class Dir1 {
}
@Directive({
selector: '[my-host-bindings-2]',
host: {'class': 'HOST_STATIC_2', 'style': 'font-family: "c2"'}
})
class Dir2 {
}
TestBed.configureTestingModule({
declarations: [
// Order of directives in the template does not matter.
// Order of declarations matters as it determines the relative priority for overrides.
Dir1,
Dir2,
// Even thought component is at the end, it will still have lowest priority because
// components are special that way.
Cmp,
]
});
const fixture = TestBed.createComponent(Cmp);
fixture.detectChanges();
const div = fixture.nativeElement.querySelector('div');
expect(getSortedClassName(div)).toEqual('HOST_STATIC_1 HOST_STATIC_2 STATIC');
expect(getSortedStyle(div)).toEqual('color: blue; font-family: c2;');
});
it('should support hostBindings inheritance', () => {
@Component({template: `<div my-host-bindings class="STATIC" style="color: blue;"></div>`})
class Cmp {
}
@Directive({host: {'class': 'SUPER_STATIC', 'style': 'font-family: "super"; width: "1px";'}})
class SuperDir {
}
@Directive({
selector: '[my-host-bindings]',
host: {'class': 'HOST_STATIC', 'style': 'font-family: "host font"'}
})
class Dir extends SuperDir {
}
TestBed.configureTestingModule({declarations: [Cmp, Dir]});
const fixture = TestBed.createComponent(Cmp);
fixture.detectChanges();
const div = fixture.nativeElement.querySelector('div');
expect(getSortedClassName(div))
.toEqual(ivyEnabled ? 'HOST_STATIC STATIC SUPER_STATIC' : 'HOST_STATIC STATIC');
// Browsers keep the '"' around the font name, but Domino removes it some we do search and
// replace. Yes we could do `replace(/"/g, '')` but that fails on android.
expect(getSortedStyle(div).replace('"', '').replace('"', ''))
.toEqual(
ivyEnabled ? 'color: blue; font-family: host font; width: 1px;' :
'color: blue; font-family: host font;');
});
onlyInIvy('style merging is ivy only feature')
.it('should apply template classes in correct order', () => {
@Component({
template: `
<div class="STATIC DELETE_MAP_A DELETE_PROP_B"
[class]="{foo: true, DELETE_MAP_A: false}"
[class.bar]="true"
[class.DELETE_PROP_B]="false"></div>
`
})
class Cmp {
}
TestBed.configureTestingModule({declarations: [Cmp]});
const fixture = TestBed.createComponent(Cmp);
fixture.detectChanges();
const classDiv = fixture.nativeElement.querySelector('div');
expect(getSortedClassName(classDiv)).toEqual('STATIC bar foo');
});
onlyInIvy('style merging is ivy only feature')
.it('should apply template styles in correct order', () => {
@Component({
template: `
<div style="width: 100px; height: 200px: color: red; background-color: yellow"
[style]="{width: '110px', height: null}"
[style.color]=" 'blue' "
[style.height.px]="undefined"></div>
`
})
class Cmp {
}
TestBed.configureTestingModule({declarations: [Cmp]});
const fixture = TestBed.createComponent(Cmp);
fixture.detectChanges();
const styleDiv = fixture.nativeElement.querySelector('div');
expect(getSortedStyle(styleDiv))
.toEqual('background-color: yellow; color: blue; width: 110px;');
});
it('should work with ngClass/ngStyle', () => {
@Component(
{template: `<div [ngClass]="['dynamic']" [ngStyle]="{'font-family': 'dynamic'}"></div>`})
class Cmp {
}
TestBed.configureTestingModule({declarations: [Cmp]});
const fixture = TestBed.createComponent(Cmp);
fixture.detectChanges();
const div = fixture.nativeElement.querySelector('div');
expect(getSortedClassName(div)).toEqual('dynamic');
expect(getSortedStyle(div)).toEqual('font-family: dynamic;');
});
});
describe('css variables', () => {
onlyInIvy('css variables').it('should support css variables', () => {
// This test only works in browsers which support CSS variables.
if (!(typeof getComputedStyle !== 'undefined' && typeof CSS !== 'undefined' &&
typeof CSS.supports !== 'undefined' && CSS.supports('color', 'var(--fake-var)')))
return;
@Component({
template: `
<div [style.--my-var]=" '100px' ">
<span style="width: var(--my-var)">CONTENT</span>
</div>`
})
class Cmp {
}
TestBed.configureTestingModule({declarations: [Cmp]});
const fixture = TestBed.createComponent(Cmp);
// document.body.appendChild(fixture.nativeElement);
fixture.detectChanges();
const span = fixture.nativeElement.querySelector('span') as HTMLElement;
expect(getComputedStyle(span).getPropertyValue('width')).toEqual('100px');
});
});
modifiedInIvy('shadow bindings include static portion')
.it('should bind [class] as input to directive', () => {
// VE Behavior https://stackblitz.com/edit/angular-cycpsf
// IVY behavior is slightly different see next test with same name.
@Component({
template: `
<div class="s1" [class]=" 'd1' " dir-shadows-class-input></div>
<div class="s2 {{'d2'}}" dir-shadows-class-input></div>
`
})
class Cmp {
}
@Directive({selector: '[dir-shadows-class-input]'})
class DirectiveShadowsClassInput {
constructor(private elementRef: ElementRef) {}
@Input('class')
set klass(value: string) {
this.elementRef.nativeElement.setAttribute('shadow-class', value);
}
}
TestBed.configureTestingModule({declarations: [Cmp, DirectiveShadowsClassInput]});
const fixture = TestBed.createComponent(Cmp);
fixture.detectChanges();
const [div1, div2] = fixture.nativeElement.querySelectorAll('div');
// Static value `class="s1"` is always written to the DOM.
expect(div1.className).toEqual('s1');
// VE passes the dynamic portion of `class` to the directive.
expect(div1.getAttribute('shadow-class')).toEqual('d1');
// Interpolation `class="s2 {{'d2'}}"` does not have a static portion and so no value is
// written to DOM.
expect(div2.className).toEqual('');
expect(div2.getAttribute('shadow-class')).toEqual('s2 d2');
});
onlyInIvy('shadow bindings include static portion')
.it('should bind [class] as input to directive', () => {
// VE Behavior https://stackblitz.com/edit/angular-cycpsf
// IVY behavior is slightly different see next test with same name.
@Component({
template: `
<div class="s1" [class]=" 'd1' " dir-shadows-class-input></div>
<div class="s2 {{'d2'}}" dir-shadows-class-input></div>
`
})
class Cmp {
}
@Directive({selector: '[dir-shadows-class-input]'})
class DirectiveShadowsClassInput {
constructor(private elementRef: ElementRef) {}
@Input('class')
set klass(value: string) {
this.elementRef.nativeElement.setAttribute('shadow-class', value);
}
}
TestBed.configureTestingModule({declarations: [Cmp, DirectiveShadowsClassInput]});
const fixture = TestBed.createComponent(Cmp);
fixture.detectChanges();
const [div1, div2] = fixture.nativeElement.querySelectorAll('div');
// Static value `class="s1"` is always written to the DOM.
expect(div1.className).toEqual('s1');
// VE has weird behavior where it calls the @Input('class') with either `class="static` or
// `[class]="dynamic"` but never both. This is determined at compile time. Due to locality
// we don't know if `[class]` is coming if we see `class` only. So we need to combine the
// static and dynamic parte. This results in slightly different calling sequence, but should
// result in the same final DOM.
expect(div1.getAttribute('shadow-class')).toEqual('s1 d1');
expect(div2.className).toEqual('');
expect(div2.getAttribute('shadow-class')).toEqual('s2 d2');
});
onlyInIvy('shadow bindings include static portion')
.it('should bind [class] as input to directive when both static and falsy dynamic values are present',
() => {
@Component({
template: `
<div class="s1" [class]="classBinding" dir-shadows-class-input></div>
`
})
class Cmp {
classBinding: any = undefined;
}
@Directive({selector: '[dir-shadows-class-input]'})
class DirectiveShadowsClassInput {
constructor(private elementRef: ElementRef) {}
@Input('class')
set klass(value: string) {
this.elementRef.nativeElement.setAttribute('shadow-class', value);
}
}
TestBed.configureTestingModule({declarations: [Cmp, DirectiveShadowsClassInput]});
const fixture = TestBed.createComponent(Cmp);
fixture.detectChanges();
const div = fixture.nativeElement.querySelector('div');
expect(div.className).toEqual('s1');
expect(div.getAttribute('shadow-class')).toEqual('s1');
fixture.componentInstance.classBinding = null;
fixture.detectChanges();
expect(div.className).toEqual('s1');
expect(div.getAttribute('shadow-class')).toEqual('s1');
fixture.componentInstance.classBinding = false;
fixture.detectChanges();
expect(div.className).toEqual('s1');
expect(div.getAttribute('shadow-class')).toEqual('s1');
fixture.componentInstance.classBinding = {toString: () => 'd1'};
fixture.detectChanges();
expect(div.className).toEqual('s1');
expect(div.getAttribute('shadow-class')).toEqual('s1 d1');
});
modifiedInIvy('shadow bindings include static portion')
.it('should bind [style] as input to directive', () => {
// VE Behavior https://stackblitz.com/edit/angular-cycpsf
@Component({
template: `
<div style="color: red;" [style]=" 'width: 100px;' " dir-shadows-style-input></div>
`
})
class Cmp {
}
@Directive({selector: '[dir-shadows-style-input]'})
class DirectiveShadowsClassInput {
constructor(private elementRef: ElementRef) {}
@Input('style')
set style(value: string) {
this.elementRef.nativeElement.setAttribute('shadow-style', value);
}
}
TestBed.configureTestingModule({declarations: [Cmp, DirectiveShadowsClassInput]});
const fixture = TestBed.createComponent(Cmp);
fixture.detectChanges();
const div = fixture.nativeElement.querySelector('div');
expect(div.style.cssText).toEqual('color: red;');
// VE has weird behavior where it calls the @Input('class') with either `class="static` or
// `[class]="dynamic"` but never both. This is determined at compile time. Due to locality
// we
// don't know if `[class]` is coming if we see `class` only. So we need to combine the two
// This results in slightly different calling sequence, but should result in the same final
// DOM.
expect(div.getAttribute('shadow-style')).toEqual('width: 100px;');
});
onlyInIvy('shadow bindings include static portion')
.it('should bind [style] as input to directive', () => {
// VE Behavior https://stackblitz.com/edit/angular-cycpsf
@Component({
template: `
<div style="color: red;" [style]=" 'width: 100px;' " dir-shadows-style-input></div>
`
})
class Cmp {
}
@Directive({selector: '[dir-shadows-style-input]'})
class DirectiveShadowsClassInput {
constructor(private elementRef: ElementRef) {}
@Input('style')
set style(value: string) {
this.elementRef.nativeElement.setAttribute('shadow-style', value);
}
}
TestBed.configureTestingModule({declarations: [Cmp, DirectiveShadowsClassInput]});
const fixture = TestBed.createComponent(Cmp);
fixture.detectChanges();
const div = fixture.nativeElement.querySelector('div');
expect(div.style.cssText).toEqual('color: red;');
// VE has weird behavior where it calls the @Input('class') with either `class="static` or
// `[class]="dynamic"` but never both. This is determined at compile time. Due to locality
// we
// don't know if `[class]` is coming if we see `class` only. So we need to combine the two
// This results in slightly different calling sequence, but should result in the same final
// DOM.
expect(div.getAttribute('shadow-style')).toEqual('color: red; width: 100px;');
});
it('should prevent circular ExpressionChangedAfterItHasBeenCheckedError on shadow inputs', () => {
@Component({template: `<div class="s1" dir-shadows-class-input></div>`})
class Cmp {
}
@Directive({selector: '[dir-shadows-class-input]'})
class DirectiveShadowsClassInput {
@Input('class')
klass: string|undefined;
@HostBinding('class')
get hostClasses() { return `${this.klass} SUFFIX`; }
}
TestBed.configureTestingModule({declarations: [Cmp, DirectiveShadowsClassInput]});
const fixture = TestBed.createComponent(Cmp);
expect(() => fixture.detectChanges()).not.toThrow();
const div = fixture.nativeElement.querySelector('div');
expect(div.className).toEqual('s1 SUFFIX');
});
it('should recover from exceptions', () => {
@Component({
template: `
<div [id]="maybeThrow(id)">
<span my-dir [class]="maybeThrow(klass)" [class.foo]="maybeThrow(foo)"></span>
</div>
`
})
class Cmp {
id = 'throw_id';
klass: string|string[] = 'throw_klass';
foo = `throw_foo`;
maybeThrow(value: any) {
if (typeof value === 'string' && value.indexOf('throw') === 0) {
throw new Error(value);
}
return value;
}
}
let myDirHostBinding = false;
@Directive({selector: '[my-dir]'})
class MyDirective {
@HostBinding('class.myDir')
get myDir(): boolean {
if (myDirHostBinding === false) {
throw new Error('class.myDir');
}
return myDirHostBinding;
}
}
TestBed.configureTestingModule({declarations: [Cmp, MyDirective]});
const fixture = TestBed.createComponent(Cmp);
const cmp = fixture.componentInstance;
const div = fixture.nativeElement.querySelector('div');
const span = fixture.nativeElement.querySelector('span');
expect(() => fixture.detectChanges()).toThrowError(/throw_id/);
expect(div.id).toBeFalsy();
expectClass(span).toEqual({});
cmp.id = 'myId';
expect(() => fixture.detectChanges()).toThrowError(/throw_klass/);
expect(div.id).toEqual('myId');
expectClass(span).toEqual({});
cmp.klass = ['BAR'];
expect(() => fixture.detectChanges()).toThrowError(/throw_foo/);
expect(div.id).toEqual('myId');
expectClass(span).toEqual(ivyEnabled ? {BAR: true} : {});
cmp.foo = 'foo';
expect(() => fixture.detectChanges()).toThrowError(/class.myDir/);
expect(div.id).toEqual('myId');
expectClass(span).toEqual(ivyEnabled ? {BAR: true, foo: true} : {});
myDirHostBinding = true;
fixture.detectChanges();
expect(div.id).toEqual('myId');
expectClass(span).toEqual({BAR: true, foo: true, myDir: true});
});
it('should render inline style and class attribute values on the element before a directive is instantiated',
() => {
@Component({
template: `
<div directive-expecting-styling style="width:200px" class="abc xyz"></div>
`
})
class Cmp {
}
@Directive({selector: '[directive-expecting-styling]'})
class DirectiveExpectingStyling {
constructor(elm: ElementRef) {
const native = elm.nativeElement;
native.setAttribute('data-captured-width', native.style.width);
native.setAttribute('data-captured-classes', native.className);
}
}
TestBed.configureTestingModule({declarations: [Cmp, DirectiveExpectingStyling]});
const fixture = TestBed.createComponent(Cmp);
fixture.detectChanges();
const element = fixture.nativeElement.querySelector('div');
expect(element.style.width).toEqual('200px');
expect(element.getAttribute('data-captured-width')).toEqual('200px');
expect(element.className.trim()).toEqual('abc xyz');
expect(element.getAttribute('data-captured-classes')).toEqual('abc xyz');
});
it('should only render the same initial styling values once before a directive runs', () => {
@Component({
template: `
<div directive-expecting-styling style="width:200px" class="abc"></div>
`
})
class Cmp {
}
@Directive({selector: '[directive-expecting-styling]'})
class DirectiveExpectingStyling {
constructor(elm: ElementRef) {
const native = elm.nativeElement;
native.style.width = '300px';
native.classList.remove('abc');
}
}
TestBed.configureTestingModule({declarations: [Cmp, DirectiveExpectingStyling]});
const fixture = TestBed.createComponent(Cmp);
fixture.detectChanges();
const element = fixture.nativeElement.querySelector('div');
expect(element.style.width).toEqual('300px');
expect(element.classList.contains('abc')).toBeFalsy();
});
it('should ensure that static classes are assigned to ng-container elements and picked up for content projection',
() => {
@Component({
template: `
<project>
outer
<ng-container class="inner">
inner
</ng-container>
</project>
`
})
class MyApp {
}
@Component({
selector: 'project',
template: `
<div class="outer-area">
<ng-content></ng-content>
</div>
<div class="inner-area">
<ng-content select=".inner"></ng-content>
</div>
`
})
class ProjectCmp {
}
TestBed.configureTestingModule({declarations: [MyApp, ProjectCmp]});
const fixture = TestBed.createComponent(MyApp);
const element = fixture.nativeElement;
fixture.detectChanges();
const inner = element.querySelector('.inner-area');
expect(inner.textContent.trim()).toEqual('inner');
const outer = element.querySelector('.outer-area');
expect(outer.textContent.trim()).toEqual('outer');
});
it('should render initial styling for repeated nodes that a component host', () => {
@Component({
selector: '[comp]',
template: '',
})
class Comp {
}
@Component({
template: `
<ng-template ngFor [ngForOf]="items" let-item>
<p comp class="a">A</p>
</ng-template>
`
})
class App {
items = [1, 2, 3];
}
TestBed.configureTestingModule({
declarations: [App, Comp],
});
const fixture = TestBed.createComponent(App);
fixture.detectChanges();
expect(fixture.debugElement.queryAll(By.css('.a')).length).toBe(3);
});
it('should do nothing for empty style bindings', () => {
@Component({template: '<div [style.color]></div>'})
class App {
}
TestBed.configureTestingModule({declarations: [App]});
const fixture = TestBed.createComponent(App);
fixture.detectChanges();
expect(fixture.nativeElement.innerHTML).toBe('<div></div>');
});
it('should do nothing for empty class bindings', () => {
@Component({template: '<div [class.is-open]></div>'})
class App {
}
TestBed.configureTestingModule({declarations: [App]});
const fixture = TestBed.createComponent(App);
fixture.detectChanges();
expect(fixture.nativeElement.innerHTML).toBe('<div></div>');
});
it('should be able to bind zero', () => {
@Component({template: '<div #div [style.opacity]="opacity"></div>'})
class App {
@ViewChild('div') div !: ElementRef<HTMLElement>;
opacity = 0;
}
TestBed.configureTestingModule({declarations: [App]});
const fixture = TestBed.createComponent(App);
fixture.detectChanges();
expect(fixture.componentInstance.div.nativeElement.style.opacity).toBe('0');
});
it('should be able to bind a SafeValue to backgroundImage', () => {
@Component({template: '<div [style.backgroundImage]="image"></div>'})
class Cmp {
image !: SafeStyle;
}
TestBed.configureTestingModule({declarations: [Cmp]});
const fixture = TestBed.createComponent(Cmp);
const sanitizer: DomSanitizer = TestBed.inject(DomSanitizer);
fixture.componentInstance.image = sanitizer.bypassSecurityTrustStyle('url("#test")');
fixture.detectChanges();
const div = fixture.nativeElement.querySelector('div') as HTMLDivElement;
expect(div.style.backgroundImage).toBe('url("#test")');
onlyInIvy('perf counters').expectPerfCounters({
rendererSetStyle: 1,
tNode: 3,
});
});
it('should not write to the native element if a directive shadows the class input', () => {
// This ex is a bit contrived. In real apps, you might have a shared class that is extended
// both by components with host elements and by directives on template nodes. In that case, the
// host styles for the template directives should just be ignored.
@Directive({selector: 'ng-template[styleDir]', host: {'[style.display]': 'display'}})
class StyleDir {
display = 'block';
}
@Component({selector: 'app-comp', template: `<ng-template styleDir></ng-template>`})
class MyApp {
}
TestBed.configureTestingModule({declarations: [MyApp, StyleDir]});
TestBed.createComponent(MyApp).detectChanges();
});
it('should be able to bind a SafeValue to clip-path', () => {
@Component({template: '<div [style.clip-path]="path"></div>'})
class Cmp {
path !: SafeStyle;
}
TestBed.configureTestingModule({declarations: [Cmp]});
const fixture = TestBed.createComponent(Cmp);
const sanitizer: DomSanitizer = TestBed.inject(DomSanitizer);
fixture.componentInstance.path = sanitizer.bypassSecurityTrustStyle('url("#test")');
fixture.detectChanges();
const html = fixture.nativeElement.innerHTML;
// Note that check the raw HTML, because (at the time of writing) the Node-based renderer
// that we use to run tests doesn't support `clip-path` in `CSSStyleDeclaration`.
expect(html).toMatch(/style=["|']clip-path:\s*url\(.*#test.*\)/);
});
it('should support interpolations inside a class binding', () => {
@Component({
template: `
<div class="a{{one}}b{{two}}c{{three}}d{{four}}e{{five}}f{{six}}g{{seven}}h{{eight}}i{{nine}}j"></div>
<div class="a{{one}}b{{two}}c{{three}}d{{four}}e{{five}}f{{six}}g{{seven}}h{{eight}}i"></div>
<div class="a{{one}}b{{two}}c{{three}}d{{four}}e{{five}}f{{six}}g{{seven}}h"></div>
<div class="a{{one}}b{{two}}c{{three}}d{{four}}e{{five}}f{{six}}g"></div>
<div class="a{{one}}b{{two}}c{{three}}d{{four}}e{{five}}f"></div>
<div class="a{{one}}b{{two}}c{{three}}d{{four}}e"></div>
<div class="a{{one}}b{{two}}c{{three}}d"></div>
<div class="a{{one}}b{{two}}c"></div>
<div class="a{{one}}b"></div>
<div class="{{one}}"></div>
`
})
class Cmp {
one = '1';
two = '2';
three = '3';
four = '4';
five = '5';
six = '6';
seven = '7';
eight = '8';
nine = '9';
}
TestBed.configureTestingModule({declarations: [Cmp]});
const fixture = TestBed.createComponent(Cmp);
const instance = fixture.componentInstance;
fixture.detectChanges();
const divs = fixture.nativeElement.querySelectorAll('div');
expect(divs[0].getAttribute('class')).toBe('a1b2c3d4e5f6g7h8i9j');
expect(divs[1].getAttribute('class')).toBe('a1b2c3d4e5f6g7h8i');
expect(divs[2].getAttribute('class')).toBe('a1b2c3d4e5f6g7h');
expect(divs[3].getAttribute('class')).toBe('a1b2c3d4e5f6g');
expect(divs[4].getAttribute('class')).toBe('a1b2c3d4e5f');
expect(divs[5].getAttribute('class')).toBe('a1b2c3d4e');
expect(divs[6].getAttribute('class')).toBe('a1b2c3d');
expect(divs[7].getAttribute('class')).toBe('a1b2c');
expect(divs[8].getAttribute('class')).toBe('a1b');
expect(divs[9].getAttribute('class')).toBe('1');
instance.one = instance.two = instance.three = instance.four = instance.five = instance.six =
instance.seven = instance.eight = instance.nine = '';
fixture.detectChanges();
expect(divs[0].getAttribute('class')).toBe('abcdefghij');
expect(divs[1].getAttribute('class')).toBe('abcdefghi');
expect(divs[2].getAttribute('class')).toBe('abcdefgh');
expect(divs[3].getAttribute('class')).toBe('abcdefg');
expect(divs[4].getAttribute('class')).toBe('abcdef');
expect(divs[5].getAttribute('class')).toBe('abcde');
expect(divs[6].getAttribute('class')).toBe('abcd');
expect(divs[7].getAttribute('class')).toBe('abc');
expect(divs[8].getAttribute('class')).toBe('ab');
expect(divs[9].getAttribute('class')).toBeFalsy();
});
onlyInIvy('only Ivy supports style interpolation')
.it('should support interpolations inside a style binding', () => {
@Component({
template: `
<div style="content: "a{{one}}b{{two}}c{{three}}d{{four}}e{{five}}f{{six}}g{{seven}}h{{eight}}i{{nine}}j""></div>
<div style="content: "a{{one}}b{{two}}c{{three}}d{{four}}e{{five}}f{{six}}g{{seven}}h{{eight}}i""></div>
<div style="content: "a{{one}}b{{two}}c{{three}}d{{four}}e{{five}}f{{six}}g{{seven}}h""></div>
<div style="content: "a{{one}}b{{two}}c{{three}}d{{four}}e{{five}}f{{six}}g""></div>
<div style="content: "a{{one}}b{{two}}c{{three}}d{{four}}e{{five}}f""></div>
<div style="content: "a{{one}}b{{two}}c{{three}}d{{four}}e""></div>
<div style="content: "a{{one}}b{{two}}c{{three}}d""></div>
<div style="content: "a{{one}}b{{two}}c""></div>
<div style="content: "a{{one}}b""></div>
<div style="{{self}}"></div>
`
})
class Cmp {
self = 'content: "self"';
one = '1';
two = '2';
three = '3';
four = '4';
five = '5';
six = '6';
seven = '7';
eight = '8';
nine = '9';
}
TestBed.configureTestingModule({declarations: [Cmp]});
const fixture = TestBed.createComponent(Cmp);
const instance = fixture.componentInstance;
fixture.detectChanges();
const divs = fixture.nativeElement.querySelectorAll('div');
expect(divs[0].style.getPropertyValue('content')).toBe('"a1b2c3d4e5f6g7h8i9j"');
expect(divs[1].style.getPropertyValue('content')).toBe('"a1b2c3d4e5f6g7h8i"');
expect(divs[2].style.getPropertyValue('content')).toBe('"a1b2c3d4e5f6g7h"');
expect(divs[3].style.getPropertyValue('content')).toBe('"a1b2c3d4e5f6g"');
expect(divs[4].style.getPropertyValue('content')).toBe('"a1b2c3d4e5f"');
expect(divs[5].style.getPropertyValue('content')).toBe('"a1b2c3d4e"');
expect(divs[6].style.getPropertyValue('content')).toBe('"a1b2c3d"');
expect(divs[7].style.getPropertyValue('content')).toBe('"a1b2c"');
expect(divs[8].style.getPropertyValue('content')).toBe('"a1b"');
expect(divs[9].style.getPropertyValue('content')).toBe('"self"');
instance.one = instance.two = instance.three = instance.four = instance.five =
instance.six = instance.seven = instance.eight = instance.nine = instance.self = '';
fixture.detectChanges();
expect(divs[0].style.getPropertyValue('content')).toBe('"abcdefghij"');
expect(divs[1].style.getPropertyValue('content')).toBe('"abcdefghi"');
expect(divs[2].style.getPropertyValue('content')).toBe('"abcdefgh"');
expect(divs[3].style.getPropertyValue('content')).toBe('"abcdefg"');
expect(divs[4].style.getPropertyValue('content')).toBe('"abcdef"');
expect(divs[5].style.getPropertyValue('content')).toBe('"abcde"');
expect(divs[6].style.getPropertyValue('content')).toBe('"abcd"');
expect(divs[7].style.getPropertyValue('content')).toBe('"abc"');
expect(divs[8].style.getPropertyValue('content')).toBe('"ab"');
expect(divs[9].style.getPropertyValue('content')).toBeFalsy();
});
it('should support interpolations inside a class binding when other classes are present', () => {
@Component({template: '<div class="zero i-{{one}} {{two}} three"></div>'})
class Cmp {
one = 'one';
two = 'two';
}
TestBed.configureTestingModule({declarations: [Cmp]});
const fixture = TestBed.createComponent(Cmp);
fixture.detectChanges();
const classList = fixture.nativeElement.querySelector('div').classList;
expect(classList).toContain('zero');
expect(classList).toContain('i-one');
expect(classList).toContain('two');
expect(classList).toContain('three');
fixture.componentInstance.one = fixture.componentInstance.two = '';
fixture.detectChanges();
expect(classList).toContain('zero');
expect(classList).toContain('i-');
expect(classList).toContain('three');
expect(classList).not.toContain('i-one');
expect(classList).not.toContain('two');
});
it('should support interpolations inside a style property binding', () => {
@Component({
template: `
<div style.font-family="f{{one}}{{two}}{{three}}{{four}}{{five}}{{six}}{{seven}}{{eight}}{{nine}}"></div>
<div style.font-family="f{{one}}{{two}}{{three}}{{four}}{{five}}{{six}}{{seven}}{{eight}}"></div>
<div style.font-family="f{{one}}{{two}}{{three}}{{four}}{{five}}{{six}}{{seven}}"></div>
<div style.font-family="f{{one}}{{two}}{{three}}{{four}}{{five}}{{six}}"></div>
<div style.font-family="f{{one}}{{two}}{{three}}{{four}}{{five}}"></div>
<div style.font-family="f{{one}}{{two}}{{three}}{{four}}"></div>
<div style.font-family="f{{one}}{{two}}{{three}}"></div>
<div style.font-family="f{{one}}{{two}}"></div>
<div style.font-family="f{{one}}"></div>
<div style.width="{{singleBinding}}"></div>
`
})
class Cmp {
singleBinding: string|null = '1337px';
one = 1;
two = 2;
three = 3;
four = 4;
five = 5;
six = 6;
seven = 7;
eight = 8;
nine = 9;
}
TestBed.configureTestingModule({declarations: [Cmp]});
const fixture = TestBed.createComponent(Cmp);
const instance = fixture.componentInstance;
fixture.detectChanges();
const divs: NodeListOf<HTMLElement> = fixture.nativeElement.querySelectorAll('div');
expect(divs[0].style.fontFamily).toBe('f123456789');
expect(divs[1].style.fontFamily).toBe('f12345678');
expect(divs[2].style.fontFamily).toBe('f1234567');
expect(divs[3].style.fontFamily).toBe('f123456');
expect(divs[4].style.fontFamily).toBe('f12345');
expect(divs[5].style.fontFamily).toBe('f1234');
expect(divs[6].style.fontFamily).toBe('f123');
expect(divs[7].style.fontFamily).toBe('f12');
expect(divs[8].style.fontFamily).toBe('f1');
expect(divs[9].style.width).toBe('1337px');
instance.singleBinding = null;
instance.one = instance.two = instance.three = instance.four = instance.five = instance.six =
instance.seven = instance.eight = instance.nine = 1;
fixture.detectChanges();
expect(divs[0].style.fontFamily).toBe('f111111111');
expect(divs[1].style.fontFamily).toBe('f11111111');
expect(divs[2].style.fontFamily).toBe('f1111111');
expect(divs[3].style.fontFamily).toBe('f111111');
expect(divs[4].style.fontFamily).toBe('f11111');
expect(divs[5].style.fontFamily).toBe('f1111');
expect(divs[6].style.fontFamily).toBe('f111');
expect(divs[7].style.fontFamily).toBe('f11');
expect(divs[8].style.fontFamily).toBe('f1');
expect(divs[9].style.width).toBeFalsy();
});
it('should support interpolations when a style property has a unit suffix', () => {
@Component({template: '<div style.width.px="{{one}}{{three}}{{three}}7"></div>'})
class Cmp {
one = 1;
three = 3;
}
TestBed.configureTestingModule({declarations: [Cmp]});
const fixture = TestBed.createComponent(Cmp);
fixture.detectChanges();
const div = fixture.nativeElement.querySelector('div');
expect(div.style.width).toBe('1337px');
fixture.componentInstance.one = 2;
fixture.componentInstance.three = 6;
fixture.detectChanges();
expect(div.style.width).toBe('2667px');
});
it('should not write to a `class` input binding in the event that there is no static class value',
() => {
let capturedClassBindingCount = 0;
let capturedClassBindingValue: string|null|undefined = undefined;
let capturedMyClassBindingCount = 0;
let capturedMyClassBindingValue: string|null|undefined = undefined;
@Component({template: '<div [class]="c" [my-class-dir]="x"></div>'})
class Cmp {
c: any = null;
x = 'foo';
}
@Directive({selector: '[my-class-dir]'})
class MyClassDir {
@Input('class')
set classVal(v: string) {
capturedClassBindingCount++;
capturedClassBindingValue = v;
}
@Input('my-class-dir')