This repository has been archived by the owner on Apr 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27.5k
/
compileSpec.js
13158 lines (11312 loc) · 480 KB
/
compileSpec.js
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
'use strict';
/* eslint-disable no-script-url */
describe('$compile', function() {
var document = window.document;
function isUnknownElement(el) {
return !!el.toString().match(/Unknown/);
}
function isSVGElement(el) {
return !!el.toString().match(/SVG/);
}
function isHTMLElement(el) {
return !!el.toString().match(/HTML/);
}
function supportsMathML() {
var d = document.createElement('div');
d.innerHTML = '<math></math>';
return !isUnknownElement(d.firstChild);
}
// IE9-11 do not support foreignObject in svg...
function supportsForeignObject() {
var d = document.createElementNS('http://www.w3.org/2000/svg', 'foreignObject');
return !!d.toString().match(/SVGForeignObject/);
}
function getChildScopes(scope) {
var children = [];
if (!scope.$$childHead) { return children; }
var childScope = scope.$$childHead;
do {
children.push(childScope);
children = children.concat(getChildScopes(childScope));
} while ((childScope = childScope.$$nextSibling));
return children;
}
var element, directive, $compile, $rootScope;
beforeEach(module(provideLog, function($provide, $compileProvider) {
element = null;
directive = $compileProvider.directive;
directive('log', function(log) {
return {
restrict: 'CAM',
priority:0,
compile: valueFn(function(scope, element, attrs) {
log(attrs.log || 'LOG');
})
};
});
directive('highLog', function(log) {
return { restrict: 'CAM', priority:3, compile: valueFn(function(scope, element, attrs) {
log(attrs.highLog || 'HIGH');
})};
});
directive('mediumLog', function(log) {
return { restrict: 'CAM', priority:2, compile: valueFn(function(scope, element, attrs) {
log(attrs.mediumLog || 'MEDIUM');
})};
});
directive('greet', function() {
return { restrict: 'CAM', priority:10, compile: valueFn(function(scope, element, attrs) {
element.text('Hello ' + attrs.greet);
})};
});
directive('set', function() {
return function(scope, element, attrs) {
element.text(attrs.set);
};
});
directive('mediumStop', valueFn({
priority: 2,
terminal: true
}));
directive('stop', valueFn({
terminal: true
}));
directive('negativeStop', valueFn({
priority: -100, // even with negative priority we still should be able to stop descend
terminal: true
}));
directive('svgContainer', function() {
return {
template: '<svg width="400" height="400" ng-transclude></svg>',
replace: true,
transclude: true
};
});
directive('svgCustomTranscludeContainer', function() {
return {
template: '<svg width="400" height="400"></svg>',
transclude: true,
link: function(scope, element, attr, ctrls, $transclude) {
var futureParent = element.children().eq(0);
$transclude(function(clone) {
futureParent.append(clone);
}, futureParent);
}
};
});
directive('svgCircle', function() {
return {
template: '<circle cx="2" cy="2" r="1"></circle>',
templateNamespace: 'svg',
replace: true
};
});
directive('myForeignObject', function() {
return {
template: '<foreignObject width="100" height="100" ng-transclude></foreignObject>',
templateNamespace: 'svg',
replace: true,
transclude: true
};
});
return function(_$compile_, _$rootScope_) {
$rootScope = _$rootScope_;
$compile = _$compile_;
};
}));
function compile(html) {
element = angular.element(html);
$compile(element)($rootScope);
}
afterEach(function() {
dealoc(element);
});
describe('configuration', function() {
it('should use $$sanitizeUriProvider for reconfiguration of the `aHrefSanitizationTrustedUrlList`', function() {
module(function($compileProvider, $$sanitizeUriProvider) {
var newRe = /safe:/, returnVal;
expect($compileProvider.aHrefSanitizationTrustedUrlList()).toBe($$sanitizeUriProvider.aHrefSanitizationTrustedUrlList());
returnVal = $compileProvider.aHrefSanitizationTrustedUrlList(newRe);
expect(returnVal).toBe($compileProvider);
expect($$sanitizeUriProvider.aHrefSanitizationTrustedUrlList()).toBe(newRe);
expect($compileProvider.aHrefSanitizationTrustedUrlList()).toBe(newRe);
});
inject(function() {
// needed to the module definition above is run...
});
});
it('should use $$sanitizeUriProvider for reconfiguration of the `imgSrcSanitizationTrustedUrlList`', function() {
module(function($compileProvider, $$sanitizeUriProvider) {
var newRe = /safe:/, returnVal;
expect($compileProvider.imgSrcSanitizationTrustedUrlList()).toBe($$sanitizeUriProvider.imgSrcSanitizationTrustedUrlList());
returnVal = $compileProvider.imgSrcSanitizationTrustedUrlList(newRe);
expect(returnVal).toBe($compileProvider);
expect($$sanitizeUriProvider.imgSrcSanitizationTrustedUrlList()).toBe(newRe);
expect($compileProvider.imgSrcSanitizationTrustedUrlList()).toBe(newRe);
});
inject(function() {
// needed to the module definition above is run...
});
});
it('should allow debugInfoEnabled to be configured', function() {
module(function($compileProvider) {
expect($compileProvider.debugInfoEnabled()).toBe(true); // the default
$compileProvider.debugInfoEnabled(false);
expect($compileProvider.debugInfoEnabled()).toBe(false);
});
inject();
});
it('should allow strictComponentBindingsEnabled to be configured', function() {
module(function($compileProvider) {
expect($compileProvider.strictComponentBindingsEnabled()).toBe(false); // the default
$compileProvider.strictComponentBindingsEnabled(true);
expect($compileProvider.strictComponentBindingsEnabled()).toBe(true);
});
inject();
});
it('should allow onChangesTtl to be configured', function() {
module(function($compileProvider) {
expect($compileProvider.onChangesTtl()).toBe(10); // the default
$compileProvider.onChangesTtl(2);
expect($compileProvider.onChangesTtl()).toBe(2);
});
inject();
});
it('should allow commentDirectivesEnabled to be configured', function() {
module(function($compileProvider) {
expect($compileProvider.commentDirectivesEnabled()).toBe(true); // the default
$compileProvider.commentDirectivesEnabled(false);
expect($compileProvider.commentDirectivesEnabled()).toBe(false);
});
inject();
});
it('should allow cssClassDirectivesEnabled to be configured', function() {
module(function($compileProvider) {
expect($compileProvider.cssClassDirectivesEnabled()).toBe(true); // the default
$compileProvider.cssClassDirectivesEnabled(false);
expect($compileProvider.cssClassDirectivesEnabled()).toBe(false);
});
inject();
});
it('should register a directive', function() {
module(function() {
directive('div', function(log) {
return {
restrict: 'ECA',
link: function(scope, element) {
log('OK');
element.text('SUCCESS');
}
};
});
});
inject(function($compile, $rootScope, log) {
element = $compile('<div></div>')($rootScope);
expect(element.text()).toEqual('SUCCESS');
expect(log).toEqual('OK');
});
});
it('should allow registration of multiple directives with same name', function() {
module(function() {
directive('div', function(log) {
return {
restrict: 'ECA',
link: {
pre: log.fn('pre1'),
post: log.fn('post1')
}
};
});
directive('div', function(log) {
return {
restrict: 'ECA',
link: {
pre: log.fn('pre2'),
post: log.fn('post2')
}
};
});
});
inject(function($compile, $rootScope, log) {
element = $compile('<div></div>')($rootScope);
expect(log).toEqual('pre1; pre2; post2; post1');
});
});
it('should throw an exception if a directive is called "hasOwnProperty"', function() {
module(function() {
expect(function() {
directive('hasOwnProperty', function() { });
}).toThrowMinErr('ng','badname', 'hasOwnProperty is not a valid directive name');
});
inject(function($compile) {});
});
it('should throw an exception if a directive name starts with a non-lowercase letter', function() {
module(function() {
expect(function() {
directive('BadDirectiveName', function() { });
}).toThrowMinErr('$compile','baddir', 'Directive/Component name \'BadDirectiveName\' is invalid. The first character must be a lowercase letter');
});
inject(function($compile) {});
});
it('should throw an exception if a directive name has leading or trailing whitespace', function() {
module(function() {
function assertLeadingOrTrailingWhitespaceInDirectiveName(name) {
expect(function() {
directive(name, function() { });
}).toThrowMinErr(
'$compile','baddir', 'Directive/Component name \'' + name + '\' is invalid. ' +
'The name should not contain leading or trailing whitespaces');
}
assertLeadingOrTrailingWhitespaceInDirectiveName(' leadingWhitespaceDirectiveName');
assertLeadingOrTrailingWhitespaceInDirectiveName('trailingWhitespaceDirectiveName ');
assertLeadingOrTrailingWhitespaceInDirectiveName(' leadingAndTrailingWhitespaceDirectiveName ');
});
inject(function($compile) {});
});
it('should throw an exception if the directive name is not defined', function() {
module(function() {
expect(function() {
directive();
}).toThrowMinErr('ng','areq');
});
inject(function($compile) {});
});
it('should ignore special chars before processing attribute directive name', function() {
// a regression https://github.com/angular/angular.js/issues/16278
module(function() {
directive('t', function(log) {
return {
restrict: 'A',
link: {
pre: log.fn('pre'),
post: log.fn('post')
}
};
});
});
inject(function($compile, $rootScope, log) {
$compile('<div _t></div>')($rootScope);
$compile('<div -t></div>')($rootScope);
$compile('<div :t></div>')($rootScope);
expect(log).toEqual('pre; post; pre; post; pre; post');
});
});
it('should throw an exception if the directive factory is not defined', function() {
module(function() {
expect(function() {
directive('myDir');
}).toThrowMinErr('ng','areq');
});
inject(function($compile) {});
});
it('should preserve context within declaration', function() {
module(function() {
directive('ff', function(log) {
var declaration = {
restrict: 'E',
template: function() {
log('ff template: ' + (this === declaration));
},
compile: function() {
log('ff compile: ' + (this === declaration));
return function() {
log('ff post: ' + (this === declaration));
};
}
};
return declaration;
});
directive('fff', function(log) {
var declaration = {
restrict: 'E',
link: {
pre: function() {
log('fff pre: ' + (this === declaration));
},
post: function() {
log('fff post: ' + (this === declaration));
}
}
};
return declaration;
});
directive('ffff', function(log) {
var declaration = {
restrict: 'E',
compile: function() {
return {
pre: function() {
log('ffff pre: ' + (this === declaration));
},
post: function() {
log('ffff post: ' + (this === declaration));
}
};
}
};
return declaration;
});
directive('fffff', function(log) {
var declaration = {
restrict: 'E',
templateUrl: function() {
log('fffff templateUrl: ' + (this === declaration));
return 'fffff.html';
},
link: function() {
log('fffff post: ' + (this === declaration));
}
};
return declaration;
});
});
inject(function($compile, $rootScope, $templateCache, log) {
$templateCache.put('fffff.html', '');
$compile('<ff></ff>')($rootScope);
$compile('<fff></fff>')($rootScope);
$compile('<ffff></ffff>')($rootScope);
$compile('<fffff></fffff>')($rootScope);
$rootScope.$digest();
expect(log).toEqual(
'ff template: true; ' +
'ff compile: true; ' +
'ff post: true; ' +
'fff pre: true; ' +
'fff post: true; ' +
'ffff pre: true; ' +
'ffff post: true; ' +
'fffff templateUrl: true; ' +
'fffff post: true'
);
});
});
});
describe('svg namespace transcludes', function() {
var ua = window.navigator.userAgent;
var isEdge = /Edge/.test(ua);
// this method assumes some sort of sized SVG element is being inspected.
function assertIsValidSvgCircle(elem) {
expect(isUnknownElement(elem)).toBe(false);
expect(isSVGElement(elem)).toBe(true);
var box = elem.getBoundingClientRect();
expect(box.width === 0 && box.height === 0).toBe(false);
}
it('should handle transcluded svg elements', inject(function($compile) {
element = jqLite('<div><svg-container>' +
'<circle cx="4" cy="4" r="2"></circle>' +
'</svg-container></div>');
$compile(element.contents())($rootScope);
document.body.appendChild(element[0]);
var circle = element.find('circle');
assertIsValidSvgCircle(circle[0]);
}));
it('should handle custom svg elements inside svg tag', inject(function() {
element = jqLite('<div><svg width="300" height="300">' +
'<svg-circle></svg-circle>' +
'</svg></div>');
$compile(element.contents())($rootScope);
document.body.appendChild(element[0]);
var circle = element.find('circle');
assertIsValidSvgCircle(circle[0]);
}));
it('should handle transcluded custom svg elements', inject(function() {
element = jqLite('<div><svg-container>' +
'<svg-circle></svg-circle>' +
'</svg-container></div>');
$compile(element.contents())($rootScope);
document.body.appendChild(element[0]);
var circle = element.find('circle');
assertIsValidSvgCircle(circle[0]);
}));
if (supportsForeignObject()) {
// Supports: Chrome 53-57+
// Since Chrome 53-57+, the reported size of `<foreignObject>` elements and their descendants
// is affected by global display settings (e.g. font size) and browser settings (e.g. default
// zoom level). In order to avoid false negatives, we compare against the size of the
// equivalent, hand-written SVG instead of fixed widths/heights.
var HAND_WRITTEN_SVG =
'<svg width="400" height="400">' +
'<foreignObject width="100" height="100">' +
'<div style="position:absolute;width:20px;height:20px">test</div>' +
'</foreignObject>' +
'</svg>';
it('should handle foreignObject', inject(function() {
element = jqLite(
'<div>' +
// By hand (for reference)
HAND_WRITTEN_SVG +
// By directive
'<svg-container>' +
'<foreignObject width="100" height="100">' +
'<div style="position:absolute;width:20px;height:20px">test</div>' +
'</foreignObject>' +
'</svg-container>' +
'</div>');
$compile(element.contents())($rootScope);
document.body.appendChild(element[0]);
var referenceElem = element.find('div')[0];
var testElem = element.find('div')[1];
var referenceBounds = referenceElem.getBoundingClientRect();
var testBounds = testElem.getBoundingClientRect();
expect(isHTMLElement(testElem)).toBe(true);
expect(referenceBounds.width).toBeGreaterThan(0);
expect(referenceBounds.height).toBeGreaterThan(0);
expect(testBounds.width).toBe(referenceBounds.width);
expect(testBounds.height).toBe(referenceBounds.height);
}));
it('should handle custom svg containers that transclude to foreignObject that transclude html', inject(function() {
element = jqLite(
'<div>' +
// By hand (for reference)
HAND_WRITTEN_SVG +
// By directive
'<svg-container>' +
'<my-foreign-object>' +
'<div style="width:20px;height:20px">test</div>' +
'</my-foreign-object>' +
'</svg-container>' +
'</div>');
$compile(element.contents())($rootScope);
document.body.appendChild(element[0]);
var referenceElem = element.find('div')[0];
var testElem = element.find('div')[1];
var referenceBounds = referenceElem.getBoundingClientRect();
var testBounds = testElem.getBoundingClientRect();
expect(isHTMLElement(testElem)).toBe(true);
expect(referenceBounds.width).toBeGreaterThan(0);
expect(referenceBounds.height).toBeGreaterThan(0);
expect(testBounds.width).toBe(referenceBounds.width);
expect(testBounds.height).toBe(referenceBounds.height);
}));
// NOTE: This test may be redundant.
// Support: Edge 14-15+
// An `<svg>` element inside a `<foreignObject>` element on MS Edge has no
// size, causing the included `<circle>` element to also have no size and thus fails an
// assertion (relying on the element having a non-zero size).
if (!isEdge) {
it('should handle custom svg containers that transclude to foreignObject' +
' that transclude to custom svg containers that transclude to custom elements', inject(function() {
element = jqLite('<div><svg-container>' +
'<my-foreign-object><svg-container><svg-circle></svg-circle></svg-container></my-foreign-object>' +
'</svg-container></div>');
$compile(element.contents())($rootScope);
document.body.appendChild(element[0]);
var circle = element.find('circle');
assertIsValidSvgCircle(circle[0]);
}));
}
}
it('should handle directives with templates that manually add the transclude further down', inject(function() {
element = jqLite('<div><svg-custom-transclude-container>' +
'<circle cx="2" cy="2" r="1"></circle></svg-custom-transclude-container>' +
'</div>');
$compile(element.contents())($rootScope);
document.body.appendChild(element[0]);
var circle = element.find('circle');
assertIsValidSvgCircle(circle[0]);
}));
it('should support directives with SVG templates and a slow url ' +
'that are stamped out later by a transcluding directive', function() {
module(function() {
directive('svgCircleUrl', valueFn({
replace: true,
templateUrl: 'template.html',
templateNamespace: 'SVG'
}));
});
inject(function($compile, $rootScope, $httpBackend) {
$httpBackend.expect('GET', 'template.html').respond('<circle></circle>');
element = $compile('<svg><g ng-repeat="l in list"><svg-circle-url></svg-circle-url></g></svg>')($rootScope);
// initially the template is not yet loaded
$rootScope.$apply(function() {
$rootScope.list = [1];
});
expect(element.find('svg-circle-url').length).toBe(1);
expect(element.find('circle').length).toBe(0);
// template is loaded and replaces the existing nodes
$httpBackend.flush();
expect(element.find('svg-circle-url').length).toBe(0);
expect(element.find('circle').length).toBe(1);
// new entry should immediately use the loaded template
$rootScope.$apply(function() {
$rootScope.list.push(2);
});
expect(element.find('svg-circle-url').length).toBe(0);
expect(element.find('circle').length).toBe(2);
});
});
});
describe('compile phase', function() {
it('should attach scope to the document node when it is compiled explicitly', inject(function($document) {
$compile($document)($rootScope);
expect($document.scope()).toBe($rootScope);
}));
it('should not wrap root text nodes in spans', function() {
element = jqLite(
'<div> <div>A</div>\n ' +
'<div>B</div>C\t\n ' +
'</div>');
$compile(element.contents())($rootScope);
var spans = element.find('span');
expect(spans.length).toEqual(0);
});
it('should be able to compile text nodes at the root', inject(function($rootScope) {
element = jqLite('<div>Name: {{name}}<br />\nColor: {{color}}</div>');
$rootScope.name = 'Lucas';
$rootScope.color = 'blue';
$compile(element.contents())($rootScope);
$rootScope.$digest();
expect(element.text()).toEqual('Name: Lucas\nColor: blue');
}));
it('should not leak memory when there are top level empty text nodes', function() {
// We compile the contents of element (i.e. not element itself)
// Then delete these contents and check the cache has been reset to zero
// First with only elements at the top level
element = jqLite('<div><div></div></div>');
$compile(element.contents())($rootScope);
element.empty();
expect(jqLiteCacheSize()).toEqual(0);
// Next with non-empty text nodes at the top level
// (in this case the compiler will wrap them in a <span>)
element = jqLite('<div>xxx</div>');
$compile(element.contents())($rootScope);
element.empty();
expect(jqLiteCacheSize()).toEqual(0);
// Next with comment nodes at the top level
element = jqLite('<div><!-- comment --></div>');
$compile(element.contents())($rootScope);
element.empty();
expect(jqLiteCacheSize()).toEqual(0);
// Finally with empty text nodes at the top level
element = jqLite('<div> \n<div></div> </div>');
$compile(element.contents())($rootScope);
element.empty();
expect(jqLiteCacheSize()).toEqual(0);
});
it('should not blow up when elements with no childNodes property are compiled', inject(
function($compile, $rootScope) {
// it turns out that when a browser plugin is bound to a DOM element (typically <object>),
// the plugin's context rather than the usual DOM apis are exposed on this element, so
// childNodes might not exist.
element = jqLite('<div>{{1+2}}</div>');
try {
element[0].childNodes[1] = {nodeType: 3, nodeName: 'OBJECT', textContent: 'fake node'};
} catch (e) { /* empty */ }
if (!element[0].childNodes[1]) return; // browser doesn't support this kind of mocking
expect(element[0].childNodes[1].textContent).toBe('fake node');
$compile(element)($rootScope);
$rootScope.$apply();
// object's children can't be compiled in this case, so we expect them to be raw
expect(element.html()).toBe('3');
}));
it('should detect anchor elements with the string "SVG" in the `href` attribute as an anchor', inject(function($compile, $rootScope) {
element = jqLite('<div><a href="/ID_SVG_ID">' +
'<span ng-if="true">Should render</span>' +
'</a></div>');
$compile(element.contents())($rootScope);
$rootScope.$digest();
document.body.appendChild(element[0]);
expect(element.find('span').text()).toContain('Should render');
}));
describe('multiple directives per element', function() {
it('should allow multiple directives per element', inject(function($compile, $rootScope, log) {
element = $compile(
'<span greet="angular" log="L" x-high-log="H" data-medium-log="M"></span>')($rootScope);
expect(element.text()).toEqual('Hello angular');
expect(log).toEqual('L; M; H');
}));
it('should recurse to children', inject(function($compile, $rootScope) {
element = $compile('<div>0<a set="hello">1</a>2<b set="angular">3</b>4</div>')($rootScope);
expect(element.text()).toEqual('0hello2angular4');
}));
it('should allow directives in classes', inject(function($compile, $rootScope, log) {
element = $compile('<div class="greet: angular; log:123;"></div>')($rootScope);
expect(element.html()).toEqual('Hello angular');
expect(log).toEqual('123');
}));
it('should allow directives in SVG element classes', inject(function($compile, $rootScope, log) {
if (!window.SVGElement) return;
element = $compile('<svg><text class="greet: angular; log:123;"></text></svg>')($rootScope);
var text = element.children().eq(0);
// In old Safari, SVG elements don't have innerHTML, so element.html() won't work
// (https://bugs.webkit.org/show_bug.cgi?id=136903)
expect(text.text()).toEqual('Hello angular');
expect(log).toEqual('123');
}));
it('should ignore not set CSS classes on SVG elements', inject(function($compile, $rootScope, log) {
if (!window.SVGElement) return;
// According to spec SVG element className property is readonly, but only FF
// implements it this way which causes compile exceptions.
element = $compile('<svg><text>{{1}}</text></svg>')($rootScope);
$rootScope.$digest();
expect(element.text()).toEqual('1');
}));
it('should receive scope, element, and attributes', function() {
var injector;
module(function() {
directive('log', function($injector, $rootScope) {
injector = $injector;
return {
restrict: 'CA',
compile: function(element, templateAttr) {
expect(typeof templateAttr.$normalize).toBe('function');
expect(typeof templateAttr.$set).toBe('function');
expect(isElement(templateAttr.$$element)).toBeTruthy();
expect(element.text()).toEqual('unlinked');
expect(templateAttr.exp).toEqual('abc');
expect(templateAttr.aa).toEqual('A');
expect(templateAttr.bb).toEqual('B');
expect(templateAttr.cc).toEqual('C');
return function(scope, element, attr) {
expect(element.text()).toEqual('unlinked');
expect(attr).toBe(templateAttr);
expect(scope).toEqual($rootScope);
element.text('worked');
};
}
};
});
});
inject(function($rootScope, $compile, $injector) {
element = $compile(
'<div class="log" exp="abc" aa="A" x-Bb="B" daTa-cC="C">unlinked</div>')($rootScope);
expect(element.text()).toEqual('worked');
expect(injector).toBe($injector); // verify that directive is injectable
});
});
});
describe('error handling', function() {
it('should handle exceptions', function() {
module(function($exceptionHandlerProvider) {
$exceptionHandlerProvider.mode('log');
directive('factoryError', function() { throw 'FactoryError'; });
directive('templateError',
valueFn({ compile: function() { throw 'TemplateError'; } }));
directive('linkingError',
valueFn(function() { throw 'LinkingError'; }));
});
inject(function($rootScope, $compile, $exceptionHandler) {
element = $compile('<div factory-error template-error linking-error></div>')($rootScope);
expect($exceptionHandler.errors[0]).toEqual('FactoryError');
expect($exceptionHandler.errors[1][0]).toEqual('TemplateError');
expect(sortTag($exceptionHandler.errors[1][1])).
toEqual('<div factory-error="" linking-error="" template-error="">');
expect($exceptionHandler.errors[2][0]).toEqual('LinkingError');
expect(sortTag($exceptionHandler.errors[2][1])).
toEqual('<div class="ng-scope" factory-error="" linking-error="" template-error="">');
// Support: IE 9-11 only, Edge 15+
// IE/Edge sort attributes in a different order.
function sortTag(text) {
var parts, elementName;
parts = text
.replace('<', '')
.replace('>', '')
.split(' ');
elementName = parts.shift();
parts.sort();
parts.unshift(elementName);
return '<' + parts.join(' ') + '>';
}
});
});
it('should allow changing the template structure after the current node', function() {
module(function() {
directive('after', valueFn({
compile: function(element) {
element.after('<span log>B</span>');
}
}));
});
inject(function($compile, $rootScope, log) {
element = jqLite('<div><div after>A</div></div>');
$compile(element)($rootScope);
expect(element.text()).toBe('AB');
expect(log).toEqual('LOG');
});
});
it('should allow changing the template structure after the current node inside ngRepeat', function() {
module(function() {
directive('after', valueFn({
compile: function(element) {
element.after('<span log>B</span>');
}
}));
});
inject(function($compile, $rootScope, log) {
element = jqLite('<div><div ng-repeat="i in [1,2]"><div after>A</div></div></div>');
$compile(element)($rootScope);
$rootScope.$digest();
expect(element.text()).toBe('ABAB');
expect(log).toEqual('LOG; LOG');
});
});
it('should allow modifying the DOM structure in post link fn', function() {
module(function() {
directive('removeNode', valueFn({
link: function($scope, $element) {
$element.remove();
}
}));
});
inject(function($compile, $rootScope) {
element = jqLite('<div><div remove-node></div><div>{{test}}</div></div>');
$rootScope.test = 'Hello';
$compile(element)($rootScope);
$rootScope.$digest();
expect(element.children().length).toBe(1);
expect(element.text()).toBe('Hello');
});
});
});
describe('compiler control', function() {
describe('priority', function() {
it('should honor priority', inject(function($compile, $rootScope, log) {
element = $compile(
'<span log="L" x-high-log="H" data-medium-log="M"></span>')($rootScope);
expect(log).toEqual('L; M; H');
}));
});
describe('terminal', function() {
it('should prevent further directives from running', inject(function($rootScope, $compile) {
element = $compile('<div negative-stop><a set="FAIL">OK</a></div>')($rootScope);
expect(element.text()).toEqual('OK');
}
));
it('should prevent further directives from running, but finish current priority level',
inject(function($rootScope, $compile, log) {
// class is processed after attrs, so putting log in class will put it after
// the stop in the current level. This proves that the log runs after stop
element = $compile(
'<div high-log medium-stop log class="medium-log"><a set="FAIL">OK</a></div>')($rootScope);
expect(element.text()).toEqual('OK');
expect(log.toArray().sort()).toEqual(['HIGH', 'MEDIUM']);
})
);
});
describe('restrict', function() {
it('should allow restriction of availability', function() {
module(function() {
forEach({div: 'E', attr: 'A', clazz: 'C', comment: 'M', all: 'EACM'},
function(restrict, name) {
directive(name, function(log) {
return {
restrict: restrict,
compile: valueFn(function(scope, element, attr) {
log(name);
})
};
});
});
});
inject(function($rootScope, $compile, log) {
dealoc($compile('<span div class="div"></span>')($rootScope));
expect(log).toEqual('');
log.reset();
dealoc($compile('<div></div>')($rootScope));
expect(log).toEqual('div');
log.reset();
dealoc($compile('<attr class="attr"></attr>')($rootScope));
expect(log).toEqual('');
log.reset();
dealoc($compile('<span attr></span>')($rootScope));
expect(log).toEqual('attr');
log.reset();
dealoc($compile('<clazz clazz></clazz>')($rootScope));
expect(log).toEqual('');
log.reset();
dealoc($compile('<span class="clazz"></span>')($rootScope));
expect(log).toEqual('clazz');
log.reset();
dealoc($compile('<!-- directive: comment -->')($rootScope));
expect(log).toEqual('comment');
log.reset();
dealoc($compile('<all class="all" all><!-- directive: all --></all>')($rootScope));
expect(log).toEqual('all; all; all; all');
});
});
it('should use EA rule as the default', function() {
module(function() {
directive('defaultDir', function(log) {
return {
compile: function() {
log('defaultDir');
}
};
});
});
inject(function($rootScope, $compile, log) {
dealoc($compile('<span default-dir ></span>')($rootScope));
expect(log).toEqual('defaultDir');
log.reset();
dealoc($compile('<default-dir></default-dir>')($rootScope));
expect(log).toEqual('defaultDir');
log.reset();
dealoc($compile('<span class="default-dir"></span>')($rootScope));
expect(log).toEqual('');
log.reset();
});
});
});
describe('template', function() {
beforeEach(module(function() {
directive('replace', valueFn({
restrict: 'CAM',
replace: true,
template: '<div class="log" style="width: 10px" high-log>Replace!</div>',
compile: function(element, attr) {
attr.$set('compiled', 'COMPILED');