| 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
 | 'use strict';
describe("markups", function() {
  it('should translate {{}} in text', inject(function($rootScope, $compile) {
    var element = $compile('<div>hello {{name}}!</div>')($rootScope)
    expect(sortedHtml(element)).toEqual('<div>hello <span ng:bind="name"></span>!</div>');
    $rootScope.name = 'Misko';
    $rootScope.$digest();
    expect(sortedHtml(element)).toEqual('<div>hello <span ng:bind="name">Misko</span>!</div>');
  }));
  it('should translate {{}} in terminal nodes', inject(function($rootScope, $compile) {
    var element = $compile('<select ng:model="x"><option value="">Greet {{name}}!</option></select>')($rootScope)
    $rootScope.$digest();
    expect(sortedHtml(element).replace(' selected="true"', '')).
      toEqual('<select ng:model="x">' +
                '<option ng:bind-template="Greet {{name}}!">Greet !</option>' +
              '</select>');
    $rootScope.name = 'Misko';
    $rootScope.$digest();
    expect(sortedHtml(element).replace(' selected="true"', '')).
      toEqual('<select ng:model="x">' +
                '<option ng:bind-template="Greet {{name}}!">Greet Misko!</option>' +
              '</select>');
  }));
  it('should translate {{}} in attributes', inject(function($rootScope, $compile) {
    var element = $compile('<div src="http://server/{{path}}.png"/>')($rootScope)
    expect(element.attr('ng:bind-attr')).toEqual('{"src":"http://server/{{path}}.png"}');
    $rootScope.path = 'a/b';
    $rootScope.$digest();
    expect(element.attr('src')).toEqual("http://server/a/b.png");
  }));
  describe('OPTION value', function() {
    beforeEach(function() {
      this.addMatchers({
        toHaveValue: function(expected){
          this.message = function() {
            return 'Expected "' + this.actual.html() + '" to have value="' + expected + '".';
          };
          var value;
          htmlParser(this.actual.html(), {
            start:function(tag, attrs){
              value = attrs.value;
            },
            end:noop,
            chars:noop
          });
          return trim(value) == trim(expected);
        }
      });
    });
    it('should populate value attribute on OPTION', inject(function($rootScope, $compile) {
      var element = $compile('<select ng:model="x"><option>abc</option></select>')($rootScope)
      expect(element).toHaveValue('abc');
    }));
    it('should ignore value if already exists', inject(function($rootScope, $compile) {
      var element = $compile('<select ng:model="x"><option value="abc">xyz</option></select>')($rootScope)
      expect(element).toHaveValue('abc');
    }));
    it('should set value even if newlines present', inject(function($rootScope, $compile) {
      var element = $compile('<select ng:model="x"><option attr="\ntext\n" \n>\nabc\n</option></select>')($rootScope)
      expect(element).toHaveValue('\nabc\n');
    }));
    it('should set value even if self closing HTML', inject(function($rootScope, $compile) {
      // IE removes the \n from option, which makes this test pointless
      if (msie) return;
      var element = $compile('<select ng:model="x"><option>\n</option></select>')($rootScope)
      expect(element).toHaveValue('\n');
    }));
  });
  it('should bind href', inject(function($rootScope, $compile) {
    var element = $compile('<a ng:href="{{url}}"></a>')($rootScope)
    expect(sortedHtml(element)).toEqual('<a ng:bind-attr="{"href":"{{url}}"}"></a>');
  }));
  it('should bind disabled', inject(function($rootScope, $compile) {
    var element = $compile('<button ng:disabled="{{isDisabled}}">Button</button>')($rootScope)
    $rootScope.isDisabled = false;
    $rootScope.$digest();
    expect(element.attr('disabled')).toBeFalsy();
    $rootScope.isDisabled = true;
    $rootScope.$digest();
    expect(element.attr('disabled')).toBeTruthy();
  }));
  it('should bind checked', inject(function($rootScope, $compile) {
    var element = $compile('<input type="checkbox" ng:checked="{{isChecked}}" />')($rootScope)
    $rootScope.isChecked = false;
    $rootScope.$digest();
    expect(element.attr('checked')).toBeFalsy();
    $rootScope.isChecked=true;
    $rootScope.$digest();
    expect(element.attr('checked')).toBeTruthy();
  }));
  it('should bind selected', inject(function($rootScope, $compile) {
    var element = $compile('<select><option value=""></option><option ng:selected="{{isSelected}}">Greetings!</option></select>')($rootScope)
    jqLite(document.body).append(element)
    $rootScope.isSelected=false;
    $rootScope.$digest();
    expect(element.children()[1].selected).toBeFalsy();
    $rootScope.isSelected=true;
    $rootScope.$digest();
    expect(element.children()[1].selected).toBeTruthy();
  }));
  it('should bind readonly', inject(function($rootScope, $compile) {
    var element = $compile('<input type="text" ng:readonly="{{isReadonly}}" />')($rootScope)
    $rootScope.isReadonly=false;
    $rootScope.$digest();
    expect(element.attr('readOnly')).toBeFalsy();
    $rootScope.isReadonly=true;
    $rootScope.$digest();
    expect(element.attr('readOnly')).toBeTruthy();
  }));
  it('should bind multiple', inject(function($rootScope, $compile) {
    var element = $compile('<select ng:multiple="{{isMultiple}}"></select>')($rootScope)
    $rootScope.isMultiple=false;
    $rootScope.$digest();
    expect(element.attr('multiple')).toBeFalsy();
    $rootScope.isMultiple='multiple';
    $rootScope.$digest();
    expect(element.attr('multiple')).toBeTruthy();
  }));
  it('should bind src', inject(function($rootScope, $compile) {
    var element = $compile('<div ng:src="{{url}}" />')($rootScope)
    $rootScope.url = 'http://localhost/';
    $rootScope.$digest();
    expect(element.attr('src')).toEqual('http://localhost/');
  }));
  it('should bind href and merge with other attrs', inject(function($rootScope, $compile) {
    var element = $compile('<a ng:href="{{url}}" rel="{{rel}}"></a>')($rootScope)
    expect(sortedHtml(element)).toEqual('<a ng:bind-attr="{"href":"{{url}}","rel":"{{rel}}"}"></a>');
  }));
  it('should bind Text with no Bindings', inject(function($compile) {
    var $rootScope;
    function newScope (){
      return $rootScope = angular.injector(['ng']).get('$rootScope');
    }
    forEach(['checked', 'disabled', 'multiple', 'readonly', 'selected'], function(name) {
      var element = $compile('<div ng:' + name + '="some"></div>')(newScope())
      expect(element.attr('ng:bind-attr')).toBe('{"' + name +'":"some"}');
      $rootScope.$digest();
      expect(element.attr(name)).toBe(name);
      dealoc(element);
    });
    var element = $compile('<div ng:src="some"></div>')(newScope())
    $rootScope.$digest();
    expect(sortedHtml(element)).toEqual('<div ng:bind-attr="{"src":"some"}" src="some"></div>');
    dealoc(element);
    var element = $compile('<div ng:href="some"></div>')(newScope())
    $rootScope.$digest();
    expect(sortedHtml(element)).toEqual('<div href="some" ng:bind-attr="{"href":"some"}"></div>');
    dealoc(element);
  }));
  it('should Parse Text With No Bindings', inject(function($rootScope, $compile) {
    var parts = parseBindings("a");
    expect(parts.length).toBe(1);
    expect(parts[0]).toBe("a");
    expect(binding(parts[0])).toBeFalsy();
  }));
  it('should Parse Empty Text', inject(function($rootScope, $compile) {
    var parts = parseBindings("");
    expect(parts.length).toBe(1);
    expect(parts[0]).toBe("");
    expect(binding(parts[0])).toBeFalsy();
  }));
  it('should Parse Inner Binding', inject(function($rootScope, $compile) {
    var parts = parseBindings("a{{b}}C");
    expect(parts.length).toBe(3);
    expect(parts[0]).toBe("a");
    expect(binding(parts[0])).toBeFalsy();
    expect(parts[1]).toBe("{{b}}");
    expect(binding(parts[1])).toBe("b");
    expect(parts[2]).toBe("C");
    expect(binding(parts[2])).toBeFalsy();
  }));
  it('should Parse Ending Binding', inject(function($rootScope, $compile) {
    var parts = parseBindings("a{{b}}");
    expect(parts.length).toBe(2);
    expect(parts[0]).toBe("a");
    expect(binding(parts[0])).toBeFalsy();
    expect(parts[1]).toBe("{{b}}");
    expect(binding(parts[1])).toBe("b");
  }));
  it('should Parse Begging Binding', inject(function($rootScope, $compile) {
    var parts = parseBindings("{{b}}c");
    expect(parts.length).toBe(2);
    expect(parts[0]).toBe("{{b}}");
    expect(binding(parts[0])).toBe("b");
    expect(parts[1]).toBe("c");
    expect(binding(parts[1])).toBeFalsy();
  }));
  it('should Parse Loan Binding', inject(function($rootScope, $compile) {
    var parts = parseBindings("{{b}}");
    expect(parts.length).toBe(1);
    expect(parts[0]).toBe("{{b}}");
    expect(binding(parts[0])).toBe("b");
  }));
  it('should Parse Two Bindings', inject(function($rootScope, $compile) {
    var parts = parseBindings("{{b}}{{c}}");
    expect(parts.length).toBe(2);
    expect(parts[0]).toBe("{{b}}");
    expect(binding(parts[0])).toBe("b");
    expect(parts[1]).toBe("{{c}}");
    expect(binding(parts[1])).toBe("c");
  }));
  it('should Parse Two Bindings With Text In Middle', inject(function($rootScope, $compile) {
    var parts = parseBindings("{{b}}x{{c}}");
    expect(parts.length).toBe(3);
    expect(parts[0]).toBe("{{b}}");
    expect(binding(parts[0])).toBe("b");
    expect(parts[1]).toBe("x");
    expect(binding(parts[1])).toBeFalsy();
    expect(parts[2]).toBe("{{c}}");
    expect(binding(parts[2])).toBe("c");
  }));
  it('should Parse Multiline', inject(function($rootScope, $compile) {
    var parts = parseBindings('"X\nY{{A\nB}}C\nD"');
    expect(binding('{{A\nB}}')).toBeTruthy();
    expect(parts.length).toBe(3);
    expect(parts[0]).toBe('"X\nY');
    expect(parts[1]).toBe('{{A\nB}}');
    expect(parts[2]).toBe('C\nD"');
  }));
  it('should Has Binding', inject(function($rootScope, $compile) {
    expect(hasBindings(parseBindings("{{a}}"))).toBe(true);
    expect(hasBindings(parseBindings("a"))).toBeFalsy();
    expect(hasBindings(parseBindings("{{b}}x{{c}}"))).toBe(true);
  }));
});
 |