Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #107 from ekhaled/zepto

---
  • Loading branch information...
commit 6f1db47804786d4bd28288e2238c70dc0a87a97c 2 parents a7bfe48 + 37d2ed6
@madrobby authored
View
30 src/ajax.js
@@ -6,8 +6,11 @@
$.ajaxJSONP = function(options){
var jsonpString = 'jsonp' + ++jsonpID,
script = document.createElement('script');
- window[jsonpString] = options.success;
- script.src = options.url.replace(/callback=\?/, 'callback=' + jsonpString);
+ window[jsonpString] = function(data){
+ options.success(data);
+ delete window.jsonpString;
+ };
+ script.src = options.url.replace(/=\?/, '=' + jsonpString);
$('head').append(script);
};
@@ -22,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(){
@@ -44,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);
@@ -78,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);
View
2  src/event.js
@@ -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);
View
6 src/fx.js
@@ -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(' '),
View
38 src/zepto.js
@@ -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]) },
@@ -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') },
@@ -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 ?
@@ -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) {
View
51 test/ajax.html
@@ -37,14 +37,16 @@
});
},
- 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();
@@ -176,9 +178,10 @@
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) {
@@ -206,7 +209,7 @@
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) {
@@ -245,5 +248,29 @@
}
});
</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>
View
21 test/fx.html
@@ -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>
@@ -16,13 +17,13 @@
<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',
@@ -47,8 +48,24 @@
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>
View
76 test/zepto.html
@@ -78,6 +78,27 @@
</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>
@@ -400,6 +421,54 @@
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);
@@ -616,6 +685,13 @@
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){
Please sign in to comment.
Something went wrong with that request. Please try again.