aboutsummaryrefslogtreecommitdiffstats
path: root/test/markupSpec.js
blob: b2d04fcdeefe084a1c2811a8a00a5c7eee091903 (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
describe("markups", function(){

  var compile, element, scope;

  beforeEach(function() {
    scope = null;
    element = null;
    var compiler = new Compiler(angularTextMarkup, angularAttrMarkup, angularDirective, angularWidget);
    compile = function(html) {
      element = jqLite(html);
      scope = compiler.compile(element)(element);
      scope.$init();
    };
  });

  afterEach(function(){
    if (element) element.remove();
    expect(size(jqCache)).toEqual(0);
  });

  it('should translate {{}} in text', function(){
    compile('<div>hello {{name}}!</div>');
    expect(sortedHtml(element)).toEqual('<div>hello <span ng-bind="name"></span>!</div>');
    scope.$set('name', 'Misko');
    scope.$eval();
    expect(sortedHtml(element)).toEqual('<div>hello <span ng-bind="name">Misko</span>!</div>');
  });

  it('should translate {{}} in terminal nodes', function(){
    compile('<select name="x"><option value="">Greet {{name}}!</option></select>');
    expect(sortedHtml(element).replace(' selected="true"', '')).toEqual('<select name="x"><option ng-bind-template="Greet {{name}}!">Greet !</option></select>');
    scope.$set('name', 'Misko');
    scope.$eval();
    expect(sortedHtml(element).replace(' selected="true"', '')).toEqual('<select name="x"><option ng-bind-template="Greet {{name}}!">Greet Misko!</option></select>');
  });

  it('should translate {{}} in attributes', function(){
    compile('<img src="http://server/{{path}}.png"/>');
    expect(element.attr('ng-bind-attr')).toEqual('{"src":"http://server/{{path}}.png"}');
    scope.$set('path', 'a/b');
    scope.$eval();
    expect(element.attr('src')).toEqual("http://server/a/b.png");
  });

  it('should populate value attribute on OPTION', function(){
    compile('<select name="x"><option>a</option></select>');
    expect(sortedHtml(element).replace(' selected="true"', '')).toEqual('<select name="x"><option value="a">a</option></select>');
  });

  it('should process all bindings when we have leading space', function(){
    var e = jqLite('<a> {{a}}<br/>{{b}}</a>');
    if (sortedHtml(e).indexOf('<a>{{') != 0) {
      // can only run this test if browser respects leading spaces
      compile(e);
      var space = msie ? '<span>' + NBSP + '</span>': ' ';
      expect(sortedHtml(scope.$element)).toEqual('<a>' + space + '<span ng-bind="a"></span><br></br><span ng-bind="b"></span></a>');
    }
  });

});


var BindingMarkupTest = TestCase("BindingMarkupTest");

BindingMarkupTest.prototype.testParseTextWithNoBindings = function(){
  var parts = parseBindings("a");
  assertEquals(parts.length, 1);
  assertEquals(parts[0], "a");
  assertTrue(!binding(parts[0]));
};

BindingMarkupTest.prototype.testParseEmptyText = function(){
  var parts = parseBindings("");
  assertEquals(parts.length, 1);
  assertEquals(parts[0], "");
  assertTrue(!binding(parts[0]));
};

BindingMarkupTest.prototype.testParseInnerBinding = function(){
  var parts = parseBindings("a{{b}}c");
  assertEquals(parts.length, 3);
  assertEquals(parts[0], "a");
  assertTrue(!binding(parts[0]));
  assertEquals(parts[1], "{{b}}");
  assertEquals(binding(parts[1]), "b");
  assertEquals(parts[2], "c");
  assertTrue(!binding(parts[2]));
};

BindingMarkupTest.prototype.testParseEndingBinding = function(){
  var parts = parseBindings("a{{b}}");
  assertEquals(parts.length, 2);
  assertEquals(parts[0], "a");
  assertTrue(!binding(parts[0]));
  assertEquals(parts[1], "{{b}}");
  assertEquals(binding(parts[1]), "b");
};

BindingMarkupTest.prototype.testParseBeggingBinding = function(){
  var parts = parseBindings("{{b}}c");
  assertEquals(parts.length, 2);
  assertEquals(parts[0], "{{b}}");
  assertEquals(binding(parts[0]), "b");
  assertEquals(parts[1], "c");
  assertTrue(!binding(parts[1]));
};

BindingMarkupTest.prototype.testParseLoanBinding = function(){
  var parts = parseBindings("{{b}}");
  assertEquals(parts.length, 1);
  assertEquals(parts[0], "{{b}}");
  assertEquals(binding(parts[0]), "b");
};

BindingMarkupTest.prototype.testParseTwoBindings = function(){
  var parts = parseBindings("{{b}}{{c}}");
  assertEquals(parts.length, 2);
  assertEquals(parts[0], "{{b}}");
  assertEquals(binding(parts[0]), "b");
  assertEquals(parts[1], "{{c}}");
  assertEquals(binding(parts[1]), "c");
};

BindingMarkupTest.prototype.testParseTwoBindingsWithTextInMiddle = function(){
  var parts = parseBindings("{{b}}x{{c}}");
  assertEquals(parts.length, 3);
  assertEquals(parts[0], "{{b}}");
  assertEquals(binding(parts[0]), "b");
  assertEquals(parts[1], "x");
  assertTrue(!binding(parts[1]));
  assertEquals(parts[2], "{{c}}");
  assertEquals(binding(parts[2]), "c");
};

BindingMarkupTest.prototype.testParseMultiline = function(){
  var parts = parseBindings('"X\nY{{A\nB}}C\nD"');
  assertTrue(!!binding('{{A\nB}}'));
  assertEquals(parts.length, 3);
  assertEquals(parts[0], '"X\nY');
  assertEquals(parts[1], '{{A\nB}}');
  assertEquals(parts[2], 'C\nD"');
};

BindingMarkupTest.prototype.testHasBinding = function(){
  assertTrue(hasBindings(parseBindings("{{a}}")));
  assertTrue(!hasBindings(parseBindings("a")));
  assertTrue(hasBindings(parseBindings("{{b}}x{{c}}")));
};