Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 2637d4e

Browse files
author
Misko Hevery
committed
removed Meta and allowed binding of HTML
1 parent 843bd35 commit 2637d4e

9 files changed

+192
-177
lines changed

angular-debug.js

Lines changed: 76 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,12 @@ function jqLiteWrap(element) {
110110
if (isString(element)) {
111111
var div = document.createElement('div');
112112
div.innerHTML = element;
113-
element = div.childNodes[0];
113+
element = new JQLite(div.childNodes);
114+
} else if (element instanceof JQLite) {
115+
} else if (isElement(element)) {
116+
element = new JQLite(element);
114117
}
115-
return element instanceof JQLite ? element : new JQLite(element);
118+
return element;
116119
}
117120
function isUndefined(value){ return typeof value == 'undefined'; }
118121
function isDefined(value){ return typeof value != 'undefined'; }
@@ -126,6 +129,10 @@ function lowercase(value){ return isString(value) ? value.toLowerCase() : value;
126129
function uppercase(value){ return isString(value) ? value.toUpperCase() : value; }
127130
function trim(value) { return isString(value) ? value.replace(/^\s*/, '').replace(/\s*$/, '') : value; }
128131
function nodeName(element) { return (element[0] || element).nodeName; }
132+
function isElement(node) {
133+
if (node && node[0]) node = node[0];
134+
return node && node.nodeName;
135+
}
129136

130137
function isVisible(element) {
131138
var rect = element[0].getBoundingClientRect();
@@ -209,14 +216,6 @@ function consoleLog(level, objs) {
209216
consoleNode.appendChild(log);
210217
}
211218

212-
function isNode(inp) {
213-
return inp &&
214-
inp.tagName &&
215-
inp.nodeName &&
216-
inp.ownerDocument &&
217-
inp.removeAttribute;
218-
}
219-
220219
function isLeafNode (node) {
221220
if (node) {
222221
switch (node.nodeName) {
@@ -1843,7 +1842,15 @@ function jqClearData(element) {
18431842
}
18441843

18451844
function JQLite(element) {
1846-
this[0] = element;
1845+
if (element.length && element.item) {
1846+
for(var i=0; i < element.length; i++) {
1847+
this[i] = element[i];
1848+
}
1849+
this.length = element.length;
1850+
} else {
1851+
this[0] = element;
1852+
this.length = 1;
1853+
}
18471854
}
18481855

18491856
JQLite.prototype = {
@@ -1920,7 +1927,11 @@ JQLite.prototype = {
19201927
},
19211928

19221929
append: function(node) {
1923-
this[0].appendChild(jqLite(node)[0]);
1930+
var self = this[0];
1931+
node = jqLite(node);
1932+
foreach(node, function(child){
1933+
self.appendChild(child);
1934+
});
19241935
},
19251936

19261937
remove: function() {
@@ -2351,28 +2362,6 @@ defineApi('Date', [angularGlobal, angularDate], []);
23512362
angular['Date']['toString'] = angularDate['toString'];
23522363
defineApi('Function', [angularGlobal, angularCollection, angularFunction],
23532364
['bind', 'bindAll', 'delay', 'defer', 'wrap', 'compose']);
2354-
angularFilter.Meta = function(obj){
2355-
if (obj) {
2356-
for ( var key in obj) {
2357-
this[key] = obj[key];
2358-
}
2359-
}
2360-
};
2361-
angularFilter.Meta.get = function(obj, attr){
2362-
attr = attr || 'text';
2363-
switch(typeof obj) {
2364-
case "string":
2365-
return attr == "text" ? obj : undefined;
2366-
case "object":
2367-
if (obj && typeof obj[attr] !== "undefined") {
2368-
return obj[attr];
2369-
}
2370-
return undefined;
2371-
default:
2372-
return obj;
2373-
}
2374-
};
2375-
23762365
var angularFilterGoogleChartApi;
23772366

23782367
foreach({
@@ -2445,31 +2434,33 @@ foreach({
24452434
if (!returnValue && regexp.test(tNo)) {
24462435
var text = carrier.name + ": " + trackingNo;
24472436
var url = carrier.url + trackingNo;
2448-
returnValue = new angularFilter.Meta({
2449-
text:text,
2450-
url:url,
2451-
html: '<a href="' + escapeAttr(url) + '">' + text + '</a>',
2452-
trackingNo:trackingNo});
2437+
returnValue = jqLite('<a></a>');
2438+
returnValue.text(text);
2439+
returnValue.attr('href', url);
24532440
}
24542441
});
24552442
});
24562443
if (returnValue)
24572444
return returnValue;
24582445
else if (trackingNo)
2459-
return noMatch || new angularFilter.Meta({text:trackingNo + " is not recognized"});
2446+
return noMatch || trackingNo + " is not recognized";
24602447
else
24612448
return null;
24622449
};})(),
24632450

24642451
'link': function(obj, title) {
2465-
var text = title || angularFilter.Meta.get(obj);
2466-
var url = angularFilter.Meta.get(obj, "url") || angularFilter.Meta.get(obj);
2467-
if (url) {
2468-
if (angular.validator.email(url) === null) {
2469-
url = "mailto:" + url;
2452+
if (obj) {
2453+
var text = title || obj.text || obj;
2454+
var url = obj.url || obj;
2455+
if (url) {
2456+
if (angular.validator.email(url) === null) {
2457+
url = "mailto:" + url;
2458+
}
2459+
var a = jqLite('<a></a>');
2460+
a.attr('href', url);
2461+
a.text(text);
2462+
return a;
24702463
}
2471-
var html = '<a href="' + escapeHtml(url) + '">' + text + '</a>';
2472-
return new angularFilter.Meta({text:text, url:url, html:html});
24732464
}
24742465
return obj;
24752466
},
@@ -2496,31 +2487,27 @@ foreach({
24962487

24972488
'image': function(obj, width, height) {
24982489
if (obj && obj.url) {
2499-
var style = "";
2490+
var style = "", img = jqLite('<img>');
25002491
if (width) {
2501-
style = ' style="max-width: ' + width +
2502-
'px; max-height: ' + (height || width) + 'px;"';
2492+
img.css('max-width', width + 'px');
2493+
img.css('max-height', (height || width) + 'px');
25032494
}
2504-
return new angularFilter.Meta({url:obj.url, text:obj.url,
2505-
html:'<img src="'+obj.url+'"' + style + '/>'});
2495+
img.attr('src', obj.url);
2496+
return img;
25062497
}
25072498
return null;
25082499
},
25092500

2510-
'lowercase': function (obj) {
2511-
var text = angularFilter.Meta.get(obj);
2512-
return text ? ("" + text).toLowerCase() : text;
2513-
},
2501+
'lowercase': lowercase,
25142502

2515-
'uppercase': function (obj) {
2516-
var text = angularFilter.Meta.get(obj);
2517-
return text ? ("" + text).toUpperCase() : text;
2518-
},
2503+
'uppercase': uppercase,
25192504

25202505
'linecount': function (obj) {
2521-
var text = angularFilter.Meta.get(obj);
2522-
if (text==='' || !text) return 1;
2523-
return text.split(/\n|\f/).length;
2506+
if (isString(obj)) {
2507+
if (obj==='') return 1;
2508+
return obj.split(/\n|\f/).length;
2509+
}
2510+
return 1;
25242511
},
25252512

25262513
'if': function (result, expression) {
@@ -2589,8 +2576,9 @@ foreach({
25892576
'encode': function(params, width, height) {
25902577
width = width || 200;
25912578
height = height || width;
2592-
var url = "http://chart.apis.google.com/chart?";
2593-
var urlParam = [];
2579+
var url = "http://chart.apis.google.com/chart?",
2580+
urlParam = [],
2581+
img = jqLite('<img>');
25942582
params['chs'] = width + "x" + height;
25952583
foreach(params, function(value, key){
25962584
if (value) {
@@ -2599,8 +2587,9 @@ foreach({
25992587
});
26002588
urlParam.sort();
26012589
url += urlParam.join("&");
2602-
return new angularFilter.Meta({url:url,
2603-
html:'<img width="' + width + '" height="' + height + '" src="'+url+'"/>'});
2590+
img.attr('src', url);
2591+
img.css({width: width + 'px', height: height + 'px'});
2592+
return img;
26042593
}
26052594
}
26062595
),
@@ -2644,7 +2633,7 @@ foreach({
26442633
},
26452634

26462635
'html': function(html){
2647-
return new angularFilter.Meta({html:html});
2636+
return jqLite(html);
26482637
},
26492638

26502639
'linky': function(text){
@@ -2666,7 +2655,7 @@ foreach({
26662655
raw = raw.substring(i + url.length);
26672656
}
26682657
html.push(escapeHtml(raw));
2669-
return new angularFilter.Meta({text:text, html:html.join('')});
2658+
return jqLite(html.join(''));
26702659
}
26712660
}, function(v,k){angularFilter[k] = v;});
26722661

@@ -2832,14 +2821,23 @@ angularDirective("ng-eval", function(expression){
28322821
});
28332822

28342823
angularDirective("ng-bind", function(expression){
2835-
var templateFn = compileBindTemplate("{{" + expression + "}}");
28362824
return function(element) {
2837-
var lastValue;
2825+
var lastValue, lastError;
28382826
this.$onEval(function() {
2839-
var value = templateFn.call(this, element);
2840-
if (value != lastValue) {
2841-
element.text(value);
2827+
var error, value = this.$tryEval(expression, function(e){
2828+
error = toJson(e);
2829+
});
2830+
if (value != lastValue || error != lastError) {
28422831
lastValue = value;
2832+
lastError = error;
2833+
elementError(element, NG_EXCEPTION, error);
2834+
if (error) value = error;
2835+
if (isElement(value)) {
2836+
element.html('');
2837+
element.append(value);
2838+
} else {
2839+
element.text(value);
2840+
}
28432841
}
28442842
}, element);
28452843
};
@@ -2866,7 +2864,10 @@ function compileBindTemplate(template){
28662864
var parts = [], self = this;
28672865
foreach(bindings, function(fn){
28682866
var value = fn.call(self, element);
2869-
if (isObject(value)) value = toJson(value, true);
2867+
if (isElement(value))
2868+
value = '';
2869+
else if (isObject(value))
2870+
value = toJson(value, true);
28702871
parts.push(value);
28712872
});
28722873
return parts.join('');

server.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
java -jar lib/jstestdriver/JsTestDriver.jar --port 9876 --runnerMode DEBUG
1+
java -jar lib/jstestdriver/JsTestDriver.jar --port 9876

src/Angular.js

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,12 @@ function jqLiteWrap(element) {
8686
if (isString(element)) {
8787
var div = document.createElement('div');
8888
div.innerHTML = element;
89-
element = div.childNodes[0];
89+
element = new JQLite(div.childNodes);
90+
} else if (element instanceof JQLite) {
91+
} else if (isElement(element)) {
92+
element = new JQLite(element);
9093
}
91-
return element instanceof JQLite ? element : new JQLite(element);
94+
return element;
9295
}
9396
function isUndefined(value){ return typeof value == 'undefined'; }
9497
function isDefined(value){ return typeof value != 'undefined'; }
@@ -102,6 +105,10 @@ function lowercase(value){ return isString(value) ? value.toLowerCase() : value;
102105
function uppercase(value){ return isString(value) ? value.toUpperCase() : value; }
103106
function trim(value) { return isString(value) ? value.replace(/^\s*/, '').replace(/\s*$/, '') : value; }
104107
function nodeName(element) { return (element[0] || element).nodeName; }
108+
function isElement(node) {
109+
if (node && node[0]) node = node[0];
110+
return node && node.nodeName;
111+
}
105112

106113
function isVisible(element) {
107114
var rect = element[0].getBoundingClientRect();
@@ -185,14 +192,6 @@ function consoleLog(level, objs) {
185192
consoleNode.appendChild(log);
186193
}
187194

188-
function isNode(inp) {
189-
return inp &&
190-
inp.tagName &&
191-
inp.nodeName &&
192-
inp.ownerDocument &&
193-
inp.removeAttribute;
194-
}
195-
196195
function isLeafNode (node) {
197196
if (node) {
198197
switch (node.nodeName) {

src/directives.js

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,23 @@ angularDirective("ng-eval", function(expression){
2323
});
2424

2525
angularDirective("ng-bind", function(expression){
26-
var templateFn = compileBindTemplate("{{" + expression + "}}");
2726
return function(element) {
28-
var lastValue;
27+
var lastValue, lastError;
2928
this.$onEval(function() {
30-
var value = templateFn.call(this, element);
31-
if (value != lastValue) {
32-
element.text(value);
29+
var error, value = this.$tryEval(expression, function(e){
30+
error = toJson(e);
31+
});
32+
if (value != lastValue || error != lastError) {
3333
lastValue = value;
34+
lastError = error;
35+
elementError(element, NG_EXCEPTION, error);
36+
if (error) value = error;
37+
if (isElement(value)) {
38+
element.html('');
39+
element.append(value);
40+
} else {
41+
element.text(value);
42+
}
3443
}
3544
}, element);
3645
};
@@ -57,7 +66,10 @@ function compileBindTemplate(template){
5766
var parts = [], self = this;
5867
foreach(bindings, function(fn){
5968
var value = fn.call(self, element);
60-
if (isObject(value)) value = toJson(value, true);
69+
if (isElement(value))
70+
value = '';
71+
else if (isObject(value))
72+
value = toJson(value, true);
6173
parts.push(value);
6274
});
6375
return parts.join('');

0 commit comments

Comments
 (0)