Skip to content

Commit

Permalink
Merge remote branch 'origin/master' into local
Browse files Browse the repository at this point in the history
  • Loading branch information
joaodubas committed Mar 1, 2011
2 parents 5213585 + 6f1db47 commit 00cdc7d
Show file tree
Hide file tree
Showing 7 changed files with 195 additions and 22 deletions.
23 changes: 20 additions & 3 deletions src/ajax.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
callback = options.success || empty,
errback = options.error || empty,
mime = mimeTypes[options.dataType],
content = options.contentType,
type = options.type || "GET",
content = options.contentType || (type === "POST" ? "application/x-www-form-urlencoded" : ""),
xhr = new XMLHttpRequest();

xhr.onreadystatechange = function(){
Expand All @@ -47,9 +48,9 @@
}
};

xhr.open(options.type || 'GET', options.url || window.location, true);
xhr.open(type, options.url || window.location, true);
if (mime) xhr.setRequestHeader('Accept', mime);
if (data instanceof Object) data = JSON.stringify(data), content = content || 'application/json';
if (data instanceof Object) data = $.param(data);
if (content) xhr.setRequestHeader('Content-Type', content);
xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
xhr.send(data);
Expand Down Expand Up @@ -81,4 +82,20 @@
});
return this;
};

$.param = function(obj, v){
var s = [],
rec = '',
add = function(key, value){
if(v) s[s.length] = encodeURIComponent(v + "[" + key +"]") + '=' + encodeURIComponent(value);
else s[s.length] = encodeURIComponent(key) + '=' + encodeURIComponent(value);
};
for(var i in obj){
if(obj[i] instanceof Array || obj[i] instanceof Object)
rec += (s.length + rec.length > 0 ? '&' : '') + $.param(obj[i], (v ? v + "[" + i + "]" : i));
else
add(obj instanceof Array ? '' : i, obj[i]);
};
return s.join("&").replace(/%20/g, "+") + rec;
};
})(Zepto);
2 changes: 1 addition & 1 deletion src/event.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@
$.fn.trigger = function(event){
return this.each(function(){
var e = document.createEvent('Events');
this.dispatchEvent(e, e.initEvent(event, true, false));
this.dispatchEvent(e, e.initEvent(event, true, true));
});
};
})(Zepto);
6 changes: 4 additions & 2 deletions src/fx.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
(function($){
$.fn.anim = function(properties, duration, ease){
$.fn.anim = function(properties, duration, ease, callback){
var transforms = [], opacity, key;
for (key in properties)
if (key === 'opacity') opacity = properties[key];
else transforms.push(key + '(' + properties[key] + ')');


typeof callback == 'function' && this.one('webkitTransitionEnd', callback);

return this.css({
'-webkit-transition': 'all ' + (duration !== undefined ? duration : 0.5) + 's ' + (ease || ''),
'-webkit-transform': transforms.join(' '),
Expand Down
38 changes: 36 additions & 2 deletions src/zepto.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,22 @@ var Zepto = (function() {
is: function(selector){
return this.length > 0 && $(this[0]).filter(selector).length > 0;
},
not: function(selector){
var nodes=[];
if (typeof selector == 'function' && selector.call !== undefined){
this.each(function(idx){
if (!selector.call(this,idx)) nodes.push(this);
});
}else{
var ignores=slice.call(typeof selector === "string" ?
this.filter(selector) :
selector instanceof NodeList ? selector : $(selector));
slice.call(this).forEach(function(el){
if (ignores.indexOf(el) < 0) nodes.push(el);
});
}
return $(nodes);
},
eq: function(idx){ return $(this[idx]) },
first: function(){ return $(this[0]) },
last: function(){ return $(this[this.length - 1]) },
Expand Down Expand Up @@ -101,6 +117,24 @@ var Zepto = (function() {
nodes = $(nodes);
return selector === undefined ? nodes : nodes.filter(selector);
},
children: function(selector){
var nodes=[];
this.each(function(){
slice.call(this.children).forEach(function(el){
nodes.push(el);
})
});
return selector === undefined ? $(nodes) : $(nodes).filter(selector);
},
siblings: function(selector){
var node, nodes=[];
this.each(function(){
slice.call((node = this).parentNode.children).forEach(function(el){
if (node !== el) nodes.push(el);
})
});
return selector === undefined ? $(nodes) : $(nodes).filter(selector);
},
pluck: function(property){ return this.map(function(element){ return element[property] }) },
show: function(){ return this.css('display', 'block') },
hide: function(){ return this.css('display', 'none') },
Expand All @@ -109,7 +143,7 @@ var Zepto = (function() {
html: function(html){
return html === undefined ?
(this.length > 0 ? this[0].innerHTML : null) :
this.each(function(idx){ this.innerHTML = typeof html == 'function' ? html(idx, this.innerHTML) : html });
this.each(function(idx){ this.innerHTML = typeof html == 'function' ? html.call(this, idx, this.innerHTML) : html });
},
text: function(text){
return text === undefined ?
Expand All @@ -122,7 +156,7 @@ var Zepto = (function() {
(this.length > 0 ? this[0].getAttribute(name) || (name in this[0] ? this[0][name] : undefined) : null) :
this.each(function(idx){
if (typeof name == 'object') for (key in name) this.setAttribute(key, name[key])
else this.setAttribute(name, typeof value == 'function' ? value(idx, this.getAttribute(name)) : value);
else this.setAttribute(name, typeof value == 'function' ? value.call(this, idx, this.getAttribute(name)) : value);
});
},
removeAttr: function(name) {
Expand Down
51 changes: 39 additions & 12 deletions test/ajax.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,16 @@ <h1>Zepto Ajax unit tests</h1>
});
},

testAjaxPostWithData: function(t) {
t.pause();
$.post('fixtures/ajax_load_simple.html', { sample: 'data' }, function(response) {
t.resume(function() {
this.assert(response);
});
});
},
// TODO: It's time to integrate a real webserver (webrick) to test POSTS
//
//testAjaxPostWithData: function(t) {
// t.pause();
// $.post('fixtures/ajax_post_json_echo.php', { sample: 'data', letters: ['a', 'b', 'c'] }, function(response) {
// t.resume(function() {
// this.assertEqual(response, '{"sample":"data","letters":["a","b","c"]}');
// });
// });
//},

testAjaxPostWithAcceptType: function(t) {
t.pause();
Expand Down Expand Up @@ -176,9 +178,10 @@ <h1>Zepto Ajax unit tests</h1>
t.assertEqual("world", result.hello);
},

testDataOptionIsConvertedToJSONString: function(t) {
$.ajax({ data: {hello: 'world'} });
t.assertEqual('{"hello":"world"}', MockXHR.last.data);
testDataOptionIsConvertedToSerializedForm: function(t) {
$.ajax({ data: {hello: 'world', array: [1,2,3], object: { prop1: 'val', prop2: 2 } } });
MockXHR.last.data = decodeURIComponent(MockXHR.last.data);
t.assertEqual('hello=world&array[]=1&array[]=2&array[]=3&object[prop1]=val&object[prop2]=2', MockXHR.last.data);
},

testErrorCallback: function(t) {
Expand Down Expand Up @@ -206,7 +209,7 @@ <h1>Zepto Ajax unit tests</h1>
t.assert(!successFired);
t.assertEqual(MockXHR.last, xhr);
t.assertEqual('parsererror', status);
t.assert(exception.toString().match(/JSON/));
t.assert(exception.toString().match(/SyntaxError/));
},

test201ResponseIsSuccess: function(t) {
Expand Down Expand Up @@ -245,5 +248,29 @@ <h1>Zepto Ajax unit tests</h1>
}
});
</script>

<script>
Evidence.TestCase.extend('ZeptoAjaxHelperMethodsTest',{

testParamMethod: function(t) {
result = $.param({ libs: ['jQuery', 'script.aculo.us', 'Prototype', 'Dojo'] });
result = decodeURIComponent(result);
t.assertEqual(result, "libs[]=jQuery&libs[]=script.aculo.us&libs[]=Prototype&libs[]=Dojo");

result = $.param({ jquery: 'Javascript', rails: 'Ruby', django: 'Python' });
result = decodeURIComponent(result);
t.assertEqual(result, "jquery=Javascript&rails=Ruby&django=Python");

result = $.param({
title: "Some Countries",
list: ['Ecuador', 'Austria', 'England'],
capitals: { ecuador: 'Quito', austria: 'Vienna', GB: { england: 'London', scotland: 'Edinburgh'} }
});
result = decodeURIComponent(result);
t.assertEqual(result, "title=Some+Countries&list[]=Ecuador&list[]=Austria&list[]=England&capitals[ecuador]=Quito&capitals[austria]=Vienna&capitals[GB][england]=London&capitals[GB][scotland]=Edinburgh");
}

});
</script>
</body>
</html>
21 changes: 19 additions & 2 deletions test/fx.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<meta charset="utf-8">
<title>Zepto FX unit tests</title>
<script src="../src/zepto.js"></script>
<script src="../src/event.js"></script>
<script src="../src/fx.js"></script>
<script src="../vendor/evidence.js"></script>
</head>
Expand All @@ -16,13 +17,13 @@ <h1>Zepto FX unit tests</h1>
<div id="animtest" style="width:40px;height:40px;background:red"></div>
<div id="durationtest_1" style="width:40px;height:40px;background:red"></div>
<div id="durationtest_2" style="width:40px;height:40px;background:red"></div>
<div id="callbacktest" style="width:40px;height:40px;background:red"></div>

<script>
Evidence.TestCase.extend('ZeptoFXTest', {

testAnim: function(t){
var el = $('#animtest').get(0);

$('#animtest').anim({
translate3d: '100px, 100px, 100px',
rotateZ: '90deg',
Expand All @@ -47,8 +48,24 @@ <h1>Zepto FX unit tests</h1>
opacity: 0.5
}, 0);
t.assertEqual('0s', $('#durationtest_2').get(0).style.webkitTransitionDuration);
},

testCallback: function(t){
t.pause();
var duration = 250; //Milliseconds
var start = new Date().getTime();
$('#callbacktest').anim({
translate3d: '100px, 100px, 100px',
rotateZ: '90deg',
opacity: 0.5
}, duration / 1000, 'linear',
function(){
t.resume(
function(){
this.assert((new Date().getTime() - start) >= duration);
});
});
}

});
</script>
</body>
Expand Down
76 changes: 76 additions & 0 deletions test/zepto.html
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,27 @@ <h1>Zepto DOM unit tests</h1>
</li>
</ul>

<ul id="childrenTest">
<li class="child one"><a class="childOfOne" href="#">gchild1</a></li>
<li class="child two"><a class="childOfTwo" href="#">gchild2</a></li>
<li class="child three"><a class="childOfThree" href="#">gchild3</a></li>
<li class="child four"><a class="childOfFour" href="#">gchild4</a></li>
</ul>

<ul id="siblingsTest">
<li class="child one"><span class="b"></span><em></em><b></b></li>
<li class="child two"><span class="c"></span><em></em><b></b></li>
<li class="child three"><span class="d"></span><em></em><b></b></li>
<li class="child four"><span class="e"></span></li>
</ul>

<ul id="notTest">
<li class="child one"><span class="b"></span></li>
<li class="child two"><span class="c"></span></li>
<li class="child three"><span class="d" id="notTestExclude"></span></li>
<li class="child four"><span class="e"></span></li>
</ul>

<style>
.hidden { display: none; }
</style>
Expand Down Expand Up @@ -400,6 +421,54 @@ <h1>Zepto DOM unit tests</h1>
t.assertLength(0, $(document.createElement('div')).parent());
},

testChildren: function(t){
var el=$("#childrenTest"), lis=$("li.child",el);

//basic form
t.assertEqualCollection(lis, el.children());
//filtered by selector
t.assertEqualCollection(lis.filter(".two"), el.children(".two"));
//across multiple parents
t.assertEqualCollection(el.find("li a"), lis.children("a"));
//chainabilty
t.assertEqual(el.find("li a.childOfTwo").text(), lis.children(".childOfTwo").text());
//non-existent children
t.assertLength(0,lis.children(".childOfTwo").children());
},

testSiblings: function(t){
var el=$("#siblingsTest");

//basic form
t.assertEqualCollection($("li.one,li.three,li.four",el), $("li.two",el).siblings());
//filtered by selector
t.assertEqualCollection($("li.three",el), $("li.two",el).siblings(".three"));
//across multiple parents
t.assertEqualCollection(el.find("li b"), $("li em",el).siblings("b"));
t.assertLength(6,$("li span",el).siblings());
//non-existent siblings
t.assertLength(0,$("li span.e",el).siblings());
},

testNot: function(t){
var el=$("#notTest");

//selector form
t.assertEqualCollection($("li.one,li.three,li.four",el), $("li",el).not(".two"));
//element or NodeList form
t.assertEqualCollection($("span.b,span.c,span.e",el), $("span",el).not(document.getElementById("notTestExclude")));
t.assertEqualCollection($("li",el), $("li, span",el).not(document.getElementsByTagName("span")));
//function form
t.assertEqualCollection($("span.b,span.c",el),$("span",el).not(function(i){
var $this=$(this);
$this.html(i);
return ($this.hasClass("d") || $this.hasClass("e")) ? true : false;
}));
//test the index was passed in properly in previous test
t.assertEqual("0",$("span.b",el).text());
t.assertEqual("1",$("span.c",el).text());
},

testFind: function(t){
var found = $('p#find1').find('span.findme');
t.assertLength(4, found);
Expand Down Expand Up @@ -616,6 +685,13 @@ <h1>Zepto DOM unit tests</h1>
click($('div#delegate_test a').get(0));
t.assert(fired);
t.refuteEqual('#foo', window.location.hash);

fired = false;
if (window.location.hash.length) window.location.hash = '';
$('div#delegate_test').html('<a href="#bar"></a>');
$('div#delegate_test a').trigger('click');
t.assert(fired);
t.refuteEqual('#bar', window.location.hash);
},

testUndelegate: function(t){
Expand Down

0 comments on commit 00cdc7d

Please sign in to comment.