aboutsummaryrefslogtreecommitdiffstats
path: root/test/service/compilerSpec.js
blob: ccf70aaff04240e07ddff3fe8e39c854e56818bb (plain)
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
'use strict';

describe('compiler', function() {
  var compiler, textMmarkup, attrMarkup, directives, widgets, compile, log, $rootScope;

  beforeEach(inject(function($provide){
    textMmarkup = [];
    attrMarkup = [];
    widgets = extensionMap({}, 'widget');
    directives = {
      hello: function(expression, element){
        log += "hello ";
        return function() {
          log += expression;
        };
      },

      observe: function(expression, element){
        return function() {
          this.$watch(expression, function(scope, val){
            if (val)
              log += ":" + val;
          });
        };
      }

    };
    log = "";
    $provide.value('$textMarkup', textMmarkup);
    $provide.value('$attrMarkup', attrMarkup);
    $provide.value('$directive', directives);
    $provide.value('$widget', widgets);
  }));


  it('should not allow compilation of multiple roots', inject(function($rootScope, $compile) {
    expect(function() {
      $compile('<div>A</div><span></span>');
    }).toThrow("Cannot compile multiple element roots: " + ie("<div>A</div><span></span>"));
    function ie(text) {
      return msie < 9 ? uppercase(text) : text;
    }
  }));


  it('should recognize a directive', inject(function($rootScope, $compile) {
    var e = jqLite('<div directive="expr" ignore="me"></div>');
    directives.directive = function(expression, element){
      log += "found";
      expect(expression).toEqual("expr");
      expect(element).toEqual(e);
      return function initFn() {
        log += ":init";
      };
    };
    var linkFn = $compile(e);
    expect(log).toEqual("found");
    linkFn($rootScope);
    expect(e.hasClass('ng-directive')).toEqual(true);
    expect(log).toEqual("found:init");
  }));


  it('should recurse to children', inject(function($rootScope, $compile) {
    $compile('<div><span hello="misko"/></div>')($rootScope);
    expect(log).toEqual("hello misko");
  }));


  it('should observe scope', inject(function($rootScope, $compile) {
    $compile('<span observe="name"></span>')($rootScope);
    expect(log).toEqual("");
    $rootScope.$digest();
    $rootScope.name = 'misko';
    $rootScope.$digest();
    $rootScope.$digest();
    $rootScope.name = 'adam';
    $rootScope.$digest();
    $rootScope.$digest();
    expect(log).toEqual(":misko:adam");
  }));


  it('should prevent descend', inject(function($rootScope, $compile) {
    directives.stop = function() { this.descend(false); };
    $compile('<span hello="misko" stop="true"><span hello="adam"/></span>')($rootScope);
    expect(log).toEqual("hello misko");
  }));


  it('should allow creation of templates', inject(function($rootScope, $compile) {
    directives.duplicate = function(expr, element){
      element.replaceWith(document.createComment("marker"));
      element.removeAttr("duplicate");
      var linker = this.compile(element);
      return function(marker) {
        this.$watch('value', function() {
          var scope = $rootScope.$new;
          linker(scope, noop);
          marker.after(scope.$element);
        });
      };
    };
    $compile('<div>before<span duplicate="expr">x</span>after</div>')($rootScope);
    expect(sortedHtml($rootScope.$element)).
      toEqual('<div>' +
                'before<#comment></#comment>' +
                'after' +
              '</div>');
    $rootScope.value = 1;
    $rootScope.$digest();
    expect(sortedHtml($rootScope.$element)).
      toEqual('<div>' +
          'before<#comment></#comment>' +
          '<span>x</span>' +
          'after' +
        '</div>');
    $rootScope.value = 2;
    $rootScope.$digest();
    expect(sortedHtml($rootScope.$element)).
      toEqual('<div>' +
          'before<#comment></#comment>' +
          '<span>x</span>' +
          '<span>x</span>' +
          'after' +
        '</div>');
    $rootScope.value = 3;
    $rootScope.$digest();
    expect(sortedHtml($rootScope.$element)).
      toEqual('<div>' +
          'before<#comment></#comment>' +
          '<span>x</span>' +
          '<span>x</span>' +
          '<span>x</span>' +
          'after' +
        '</div>');
  }));


  it('should process markup before directives', inject(function($rootScope, $compile) {
    textMmarkup.push(function(text, textNode, parentNode) {
      if (text == 'middle') {
        expect(textNode.text()).toEqual(text);
        parentNode.attr('hello', text);
        textNode[0].nodeValue = 'replaced';
      }
    });
    $compile('<div>before<span>middle</span>after</div>')($rootScope);
    expect(sortedHtml($rootScope.$element[0], true)).
      toEqual('<div>before<span class="ng-directive" hello="middle">replaced</span>after</div>');
    expect(log).toEqual("hello middle");
  }));


  it('should replace widgets', inject(function($rootScope, $compile) {
    widgets['NG:BUTTON'] = function(element) {
      expect(element.hasClass('ng-widget')).toEqual(true);
      element.replaceWith('<div>button</div>');
      return function(element) {
        log += 'init';
      };
    };
    $compile('<div><ng:button>push me</ng:button></div>')($rootScope);
    expect(lowercase($rootScope.$element[0].innerHTML)).toEqual('<div>button</div>');
    expect(log).toEqual('init');
  }));


  it('should use the replaced element after calling widget', inject(function($rootScope, $compile) {
    widgets['H1'] = function(element) {
      // HTML elements which are augmented by acting as widgets, should not be marked as so
      expect(element.hasClass('ng-widget')).toEqual(false);
      var span = angular.element('<span>{{1+2}}</span>');
      element.replaceWith(span);
      this.descend(true);
      this.directives(true);
      return noop;
    };
    textMmarkup.push(function(text, textNode, parent){
      if (text == '{{1+2}}')
        parent.text('3');
    });
    $compile('<div><h1>ignore me</h1></div>')($rootScope);
    expect($rootScope.$element.text()).toEqual('3');
  }));


  it('should allow multiple markups per text element', inject(function($rootScope, $compile) {
    textMmarkup.push(function(text, textNode, parent){
      var index = text.indexOf('---');
      if (index > -1) {
        textNode.after(text.substring(index + 3));
        textNode.after("<hr/>");
        textNode.after(text.substring(0, index));
        textNode.remove();
      }
    });
    textMmarkup.push(function(text, textNode, parent){
      var index = text.indexOf('===');
      if (index > -1) {
        textNode.after(text.substring(index + 3));
        textNode.after("<p>");
        textNode.after(text.substring(0, index));
        textNode.remove();
      }
    });
    $compile('<div>A---B---C===D</div>')($rootScope);
    expect(sortedHtml($rootScope.$element)).toEqual('<div>A<hr></hr>B<hr></hr>C<p></p>D</div>');
  }));


  it('should add class for namespace elements', inject(function($rootScope, $compile) {
    var element = $compile('<ng:space>abc</ng:space>')($rootScope);
    expect(element.hasClass('ng-space')).toEqual(true);
  }));
});