This repository was 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.4k
/
Copy pathcompileSpec.js
2557 lines (2125 loc) · 89.2 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';
describe('$compile', function() {
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
}));
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 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: log.fn('1')
};
});
directive('div', function(log) {
return {
restrict: 'ECA',
link: log.fn('2')
};
});
});
inject(function($compile, $rootScope, log) {
element = $compile('<div></div>')($rootScope);
expect(log).toEqual('1; 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 wrap root text nodes in spans', inject(function($compile, $rootScope) {
element = jqLite('<div>A<a>B</a>C</div>');
var text = element.contents();
expect(text[0].nodeName).toEqual('#text');
text = $compile(text)($rootScope);
expect(text[0].nodeName).toEqual('SPAN');
expect(element.find('span').text()).toEqual('A<a>B</a>C');
}));
it('should not wrap root whitespace text nodes in spans', function() {
element = jqLite(
'<div> <div>A</div>\n '+ // The spaces and newlines here should not get wrapped
'<div>B</div>C\t\n '+ // The "C", tabs and spaces here will be wrapped
'</div>');
$compile(element.contents())($rootScope);
var spans = element.find('span');
expect(spans.length).toEqual(1);
expect(spans.text().indexOf('C')).toEqual(0);
});
it('should not leak memory when there are top level empty text nodes', function() {
var calcCacheSize = function() {
var size = 0;
forEach(jqLite.cache, function(item, key) { size++; });
return size;
};
// 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.html('');
expect(calcCacheSize()).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.html('');
expect(calcCacheSize()).toEqual(0);
// Next with comment nodes at the top level
element = jqLite('<div><!-- comment --></div>');
$compile(element.contents())($rootScope);
element.html('');
expect(calcCacheSize()).toEqual(0);
// Finally with empty text nodes at the top level
element = jqLite('<div> \n<div></div> </div>');
$compile(element.contents())($rootScope);
element.html('');
expect(calcCacheSize()).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 an DOM element (typically <object>),
// the plugin's context rather than the usual DOM apis are exposed on this element, so
// childNodes might not exist.
if (msie < 9) return;
element = jqLite('<div>{{1+2}}</div>');
element[0].childNodes[1] = {nodeType: 3, nodeName: 'OBJECT', textContent: 'fake node'};
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");
}));
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('H; M; L');
}));
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 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 allow directives in comments', inject(
function($compile, $rootScope, log) {
element = $compile('<div>0<!-- directive: log angular -->1</div>')($rootScope);
expect(log).toEqual('angular');
}
));
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(ie($exceptionHandler.errors[1][1])).
toEqual('<div factory-error linking-error template-error>');
expect($exceptionHandler.errors[2][0]).toEqual('LinkingError');
expect(ie($exceptionHandler.errors[2][1])).
toEqual('<div class="ng-scope" factory-error linking-error template-error>');
// crazy stuff to make IE happy
function ie(text) {
var list = [],
parts, elementName;
parts = lowercase(text).
replace('<', '').
replace('>', '').
split(' ');
elementName = parts.shift();
parts.sort();
parts.unshift(elementName);
forEach(parts, function(value, key){
if (value.substring(0,3) == 'ng-') {
} else {
value = value.replace('=""', '');
var match = value.match(/=(.*)/);
if (match && match[1].charAt(0) != '"') {
value = value.replace(/=(.*)/, '="$1"');
}
list.push(value);
}
});
return '<' + list.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('H; M; L');
}));
});
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 attributes', function() {
module(function() {
forEach({div:'E', attr:'A', clazz:'C', all:'EAC'}, 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('<all class="all" all></all>')($rootScope));
expect(log).toEqual('all; all; all');
});
});
});
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');
expect(element).toBe(attr.$$element);
}
}));
directive('append', valueFn({
restrict: 'CAM',
template: '<div class="log" style="width: 10px" high-log>Append!</div>',
compile: function(element, attr) {
attr.$set('compiled', 'COMPILED');
expect(element).toBe(attr.$$element);
}
}));
directive('replaceWithInterpolatedClass', valueFn({
replace: true,
template: '<div class="class_{{1+1}}">Replace with interpolated class!</div>',
compile: function(element, attr) {
attr.$set('compiled', 'COMPILED');
expect(element).toBe(attr.$$element);
}
}));
}));
it('should replace element with template', inject(function($compile, $rootScope) {
element = $compile('<div><div replace>ignore</div><div>')($rootScope);
expect(element.text()).toEqual('Replace!');
expect(element.find('div').attr('compiled')).toEqual('COMPILED');
}));
it('should append element with template', inject(function($compile, $rootScope) {
element = $compile('<div><div append>ignore</div><div>')($rootScope);
expect(element.text()).toEqual('Append!');
expect(element.find('div').attr('compiled')).toEqual('COMPILED');
}));
it('should compile template when replacing', inject(function($compile, $rootScope, log) {
element = $compile('<div><div replace medium-log>ignore</div><div>')
($rootScope);
$rootScope.$digest();
expect(element.text()).toEqual('Replace!');
// HIGH goes after MEDIUM since it executes as part of replaced template
expect(log).toEqual('MEDIUM; HIGH; LOG');
}));
it('should compile template when appending', inject(function($compile, $rootScope, log) {
element = $compile('<div><div append medium-log>ignore</div><div>')
($rootScope);
$rootScope.$digest();
expect(element.text()).toEqual('Append!');
expect(log).toEqual('HIGH; LOG; MEDIUM');
}));
it('should merge attributes including style attr', inject(function($compile, $rootScope) {
element = $compile(
'<div><div replace class="medium-log" style="height: 20px" ></div><div>')
($rootScope);
var div = element.find('div');
expect(div.hasClass('medium-log')).toBe(true);
expect(div.hasClass('log')).toBe(true);
expect(div.css('width')).toBe('10px');
expect(div.css('height')).toBe('20px');
expect(div.attr('replace')).toEqual('');
expect(div.attr('high-log')).toEqual('');
}));
it('should prevent multiple templates per element', inject(function($compile) {
try {
$compile('<div><span replace class="replace"></span></div>')
fail();
} catch(e) {
expect(e.message).toMatch(/Multiple directives .* asking for template/);
}
}));
it('should play nice with repeater when replacing', inject(function($compile, $rootScope) {
element = $compile(
'<div>' +
'<div ng-repeat="i in [1,2]" replace></div>' +
'</div>')($rootScope);
$rootScope.$digest();
expect(element.text()).toEqual('Replace!Replace!');
}));
it('should play nice with repeater when appending', inject(function($compile, $rootScope) {
element = $compile(
'<div>' +
'<div ng-repeat="i in [1,2]" append></div>' +
'</div>')($rootScope);
$rootScope.$digest();
expect(element.text()).toEqual('Append!Append!');
}));
it('should handle interpolated css from replacing directive', inject(
function($compile, $rootScope) {
element = $compile('<div replace-with-interpolated-class></div>')($rootScope);
$rootScope.$digest();
expect(element).toHaveClass('class_2');
}));
it('should merge interpolated css class', inject(function($compile, $rootScope) {
element = $compile('<div class="one {{cls}} three" replace></div>')($rootScope);
$rootScope.$apply(function() {
$rootScope.cls = 'two';
});
expect(element).toHaveClass('one');
expect(element).toHaveClass('two'); // interpolated
expect(element).toHaveClass('three');
expect(element).toHaveClass('log'); // merged from replace directive template
}));
it('should merge interpolated css class with ngRepeat',
inject(function($compile, $rootScope) {
element = $compile(
'<div>' +
'<div ng-repeat="i in [1]" class="one {{cls}} three" replace></div>' +
'</div>')($rootScope);
$rootScope.$apply(function() {
$rootScope.cls = 'two';
});
var child = element.find('div').eq(0);
expect(child).toHaveClass('one');
expect(child).toHaveClass('two'); // interpolated
expect(child).toHaveClass('three');
expect(child).toHaveClass('log'); // merged from replace directive template
}));
it("should fail if replacing and template doesn't have a single root element", function() {
module(function() {
directive('noRootElem', function() {
return {
replace: true,
template: 'dada'
}
});
directive('multiRootElem', function() {
return {
replace: true,
template: '<div></div><div></div>'
}
});
directive('singleRootWithWhiteSpace', function() {
return {
replace: true,
template: ' <div></div> \n'
}
});
});
inject(function($compile) {
expect(function() {
$compile('<p no-root-elem></p>');
}).toThrow('Template must have exactly one root element. was: dada');
expect(function() {
$compile('<p multi-root-elem></p>');
}).toThrow('Template must have exactly one root element. was: <div></div><div></div>');
// ws is ok
expect(function() {
$compile('<p single-root-with-white-space></p>');
}).not.toThrow();
});
});
});
describe('templateUrl', function() {
beforeEach(module(
function() {
directive('hello', valueFn({
restrict: 'CAM', templateUrl: 'hello.html', transclude: true
}));
directive('cau', valueFn({
restrict: 'CAM', templateUrl:'cau.html'
}));
directive('cError', valueFn({
restrict: 'CAM',
templateUrl:'error.html',
compile: function() {
throw Error('cError');
}
}));
directive('lError', valueFn({
restrict: 'CAM',
templateUrl: 'error.html',
compile: function() {
throw Error('lError');
}
}));
directive('iHello', valueFn({
restrict: 'CAM',
replace: true,
templateUrl: 'hello.html'
}));
directive('iCau', valueFn({
restrict: 'CAM',
replace: true,
templateUrl:'cau.html'
}));
directive('iCError', valueFn({
restrict: 'CAM',
replace: true,
templateUrl:'error.html',
compile: function() {
throw Error('cError');
}
}));
directive('iLError', valueFn({
restrict: 'CAM',
replace: true,
templateUrl: 'error.html',
compile: function() {
throw Error('lError');
}
}));
}
));
it('should append template via $http and cache it in $templateCache', inject(
function($compile, $httpBackend, $templateCache, $rootScope, $browser) {
$httpBackend.expect('GET', 'hello.html').respond('<span>Hello!</span> World!');
$templateCache.put('cau.html', '<span>Cau!</span>');
element = $compile('<div><b class="hello">ignore</b><b class="cau">ignore</b></div>')($rootScope);
expect(sortedHtml(element)).
toEqual('<div><b class="hello"></b><b class="cau"></b></div>');
$rootScope.$digest();
expect(sortedHtml(element)).
toEqual('<div><b class="hello"></b><b class="cau"><span>Cau!</span></b></div>');
$httpBackend.flush();
expect(sortedHtml(element)).toEqual(
'<div>' +
'<b class="hello"><span>Hello!</span> World!</b>' +
'<b class="cau"><span>Cau!</span></b>' +
'</div>');
}
));
it('should inline template via $http and cache it in $templateCache', inject(
function($compile, $httpBackend, $templateCache, $rootScope) {
$httpBackend.expect('GET', 'hello.html').respond('<span>Hello!</span>');
$templateCache.put('cau.html', '<span>Cau!</span>');
element = $compile('<div><b class=i-hello>ignore</b><b class=i-cau>ignore</b></div>')($rootScope);
expect(sortedHtml(element)).
toEqual('<div><b class="i-hello"></b><b class="i-cau"></b></div>');
$rootScope.$digest();
expect(sortedHtml(element)).
toEqual('<div><b class="i-hello"></b><span class="i-cau">Cau!</span></div>');
$httpBackend.flush();
expect(sortedHtml(element)).
toEqual('<div><span class="i-hello">Hello!</span><span class="i-cau">Cau!</span></div>');
}
));
it('should compile, link and flush the template append', inject(
function($compile, $templateCache, $rootScope, $browser) {
$templateCache.put('hello.html', '<span>Hello, {{name}}!</span>');
$rootScope.name = 'Elvis';
element = $compile('<div><b class="hello"></b></div>')($rootScope);
$rootScope.$digest();
expect(sortedHtml(element)).
toEqual('<div><b class="hello"><span>Hello, Elvis!</span></b></div>');
}
));
it('should compile, link and flush the template inline', inject(
function($compile, $templateCache, $rootScope) {
$templateCache.put('hello.html', '<span>Hello, {{name}}!</span>');
$rootScope.name = 'Elvis';
element = $compile('<div><b class=i-hello></b></div>')($rootScope);
$rootScope.$digest();
expect(sortedHtml(element)).
toEqual('<div><span class="i-hello">Hello, Elvis!</span></div>');
}
));
it('should compile, flush and link the template append', inject(
function($compile, $templateCache, $rootScope) {
$templateCache.put('hello.html', '<span>Hello, {{name}}!</span>');
$rootScope.name = 'Elvis';
var template = $compile('<div><b class="hello"></b></div>');
element = template($rootScope);
$rootScope.$digest();
expect(sortedHtml(element)).
toEqual('<div><b class="hello"><span>Hello, Elvis!</span></b></div>');
}
));
it('should compile, flush and link the template inline', inject(
function($compile, $templateCache, $rootScope) {
$templateCache.put('hello.html', '<span>Hello, {{name}}!</span>');
$rootScope.name = 'Elvis';
var template = $compile('<div><b class=i-hello></b></div>');
element = template($rootScope);
$rootScope.$digest();
expect(sortedHtml(element)).
toEqual('<div><span class="i-hello">Hello, Elvis!</span></div>');
}
));
it('should resolve widgets after cloning in append mode', function() {
module(function($exceptionHandlerProvider) {
$exceptionHandlerProvider.mode('log');
});
inject(function($compile, $templateCache, $rootScope, $httpBackend, $browser,
$exceptionHandler) {
$httpBackend.expect('GET', 'hello.html').respond('<span>{{greeting}} </span>');
$httpBackend.expect('GET', 'error.html').respond('<div></div>');
$templateCache.put('cau.html', '<span>{{name}}</span>');
$rootScope.greeting = 'Hello';
$rootScope.name = 'Elvis';
var template = $compile(
'<div>' +
'<b class="hello"></b>' +
'<b class="cau"></b>' +
'<b class=c-error></b>' +
'<b class=l-error></b>' +
'</div>');
var e1;
var e2;
e1 = template($rootScope.$new(), noop); // clone
expect(e1.text()).toEqual('');
$httpBackend.flush();
e2 = template($rootScope.$new(), noop); // clone
$rootScope.$digest();
expect(e1.text()).toEqual('Hello Elvis');
expect(e2.text()).toEqual('Hello Elvis');
expect($exceptionHandler.errors.length).toEqual(2);
expect($exceptionHandler.errors[0][0].message).toEqual('cError');
expect($exceptionHandler.errors[1][0].message).toEqual('lError');
dealoc(e1);
dealoc(e2);
});
});
it('should resolve widgets after cloning in inline mode', function() {
module(function($exceptionHandlerProvider) {
$exceptionHandlerProvider.mode('log');
});
inject(function($compile, $templateCache, $rootScope, $httpBackend, $browser,
$exceptionHandler) {
$httpBackend.expect('GET', 'hello.html').respond('<span>{{greeting}} </span>');
$httpBackend.expect('GET', 'error.html').respond('<div></div>');
$templateCache.put('cau.html', '<span>{{name}}</span>');
$rootScope.greeting = 'Hello';
$rootScope.name = 'Elvis';
var template = $compile(
'<div>' +
'<b class=i-hello></b>' +
'<b class=i-cau></b>' +
'<b class=i-c-error></b>' +
'<b class=i-l-error></b>' +
'</div>');
var e1;
var e2;
e1 = template($rootScope.$new(), noop); // clone
expect(e1.text()).toEqual('');
$httpBackend.flush();
e2 = template($rootScope.$new(), noop); // clone
$rootScope.$digest();
expect(e1.text()).toEqual('Hello Elvis');
expect(e2.text()).toEqual('Hello Elvis');
expect($exceptionHandler.errors.length).toEqual(2);
expect($exceptionHandler.errors[0][0].message).toEqual('cError');
expect($exceptionHandler.errors[1][0].message).toEqual('lError');
dealoc(e1);
dealoc(e2);
});
});
it('should be implicitly terminal and not compile placeholder content in append', inject(
function($compile, $templateCache, $rootScope, log) {
// we can't compile the contents because that would result in a memory leak
$templateCache.put('hello.html', 'Hello!');
element = $compile('<div><b class="hello"><div log></div></b></div>')($rootScope);
expect(log).toEqual('');
}
));
it('should be implicitly terminal and not compile placeholder content in inline', inject(
function($compile, $templateCache, $rootScope, log) {
// we can't compile the contents because that would result in a memory leak
$templateCache.put('hello.html', 'Hello!');
element = $compile('<div><b class=i-hello><div log></div></b></div>')($rootScope);
expect(log).toEqual('');
}
));
it('should throw an error and clear element content if the template fails to load', inject(
function($compile, $httpBackend, $rootScope) {
$httpBackend.expect('GET', 'hello.html').respond(404, 'Not Found!');
element = $compile('<div><b class="hello">content</b></div>')($rootScope);
expect(function() {
$httpBackend.flush();
}).toThrow('Failed to load template: hello.html');
expect(sortedHtml(element)).toBe('<div><b class="hello"></b></div>');
}
));
it('should prevent multiple templates per element', function() {
module(function() {
directive('sync', valueFn({
restrict: 'C',
template: '<span></span>'
}));
directive('async', valueFn({
restrict: 'C',
templateUrl: 'template.html'
}));
});
inject(function($compile){
expect(function() {
$compile('<div><div class="sync async"></div></div>');
}).toThrow('Multiple directives [sync, async] asking for template on: '+
'<div class="sync async">');
});
});
describe('delay compile / linking functions until after template is resolved', function(){
var template;
beforeEach(module(function() {
function logDirective (name, priority, options) {
directive(name, function(log) {
return (extend({
priority: priority,
compile: function() {
log(name + '-C');
return function() { log(name + '-L'); }
}
}, options || {}));
});
}
logDirective('first', 10);
logDirective('second', 5, { templateUrl: 'second.html' });
logDirective('third', 3);
logDirective('last', 0);
logDirective('iFirst', 10, {replace: true});
logDirective('iSecond', 5, {replace: true, templateUrl: 'second.html' });
logDirective('iThird', 3, {replace: true});
logDirective('iLast', 0, {replace: true});
}));
it('should flush after link append', inject(
function($compile, $rootScope, $httpBackend, log) {
$httpBackend.expect('GET', 'second.html').respond('<div third>{{1+2}}</div>');
template = $compile('<div><span first second last></span></div>');
element = template($rootScope);
expect(log).toEqual('first-C');
log('FLUSH');
$httpBackend.flush();
$rootScope.$digest();
expect(log).toEqual(
'first-C; FLUSH; second-C; last-C; third-C; ' +
'third-L; first-L; second-L; last-L');
var span = element.find('span');
expect(span.attr('first')).toEqual('');
expect(span.attr('second')).toEqual('');
expect(span.find('div').attr('third')).toEqual('');
expect(span.attr('last')).toEqual('');
expect(span.text()).toEqual('3');
}));