Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

fix `fn.closest()` and add context argument

old implementation started from the parent, while the correct
implementation should start from the current element
  • Loading branch information...
commit 116c79b18185b88381df1b26e4ac9fdb73498445 1 parent dbe33a2
@mislav mislav authored
Showing with 29 additions and 10 deletions.
  1. +1 −1  README.rdoc
  2. +5 −4 src/zepto.js
  3. +23 −5 test/zepto.html
View
2  README.rdoc
@@ -47,7 +47,7 @@ Context and .find calls are equivalent:
last(): new collection containing only the last matched element
find('selector'): find all children/grandchildren that match the given selector
- closest('selector'): traverses the DOM upwards to find the first matching element
+ closest('selector'): find the first matching element by going upwards starting from the current element
next(): next siblings
prev(): previous siblings
is('selector'): returns true/false if first element matches the selector
View
9 src/zepto.js
@@ -66,10 +66,11 @@ var Zepto = (function() {
find: function(selector){
return $(this.dom.map(function(el){ return $$(el, selector) }).reduce(function(a,b){ return a.concat(b) }, []));
},
- closest: function(selector){
- var node = this.dom[0].parentNode, nodes = $$(document, selector);
- while(node && nodes.indexOf(node) < 0) node = node.parentNode;
- return $(node && !(node === document) ? node : []);
+ closest: function(selector, context){
+ var node = this.dom[0], nodes = $$(context !== undefined ? context : document, selector);
+ if (nodes.length === 0) node = null;
+ while(node && node !== document && nodes.indexOf(node) < 0) node = node.parentNode;
+ return $(node);
},
pluck: function(property){ return this.dom.map(function(element){ return element[property] }) },
show: function(){ return this.css('display', 'block') },
View
28 test/zepto.html
@@ -51,10 +51,12 @@
<div style="position:absolute;width:100px;height:50px" id="offset">test</div>
- <ul>
+ <ul id="parents">
<li id="li1">
- <ul>
- <li id="li2">
+ <ul id="nested">
+ <li id="li2">one</li>
+ <li>two</li>
+ <li>three</li>
</ul>
</li>
</ul>
@@ -121,6 +123,16 @@
'Expected length %d, got %d.', expected, actual);
}
+ Evidence.Assertions.assertEqualCollection = function(expectedCollection, actualCollection, message) {
+ var expected = expectedCollection.get(), actual = actualCollection.get(),
+ passed = expected.length == actual.length;
+
+ if (passed) for (var i=0; i<expected.length; i++) passed = expected[i] == actual[i];
+
+ this._assertExpression(passed, message || 'Failed assertion.',
+ 'Expected %o, got %o.', expected, actual);
+ }
+
Evidence.TestCase.extend('ZeptoTest', {
// test to see if we augment iOS 3.2 with String#trim()
@@ -320,8 +332,14 @@
},
testClosest: function(t){
- t.assertIdentical($('#li1').get(0), $('#li2').closest('li').get(0));
- t.assertLength(0, $('#li2').closest('div'));
+ var el = $('#li2');
+ t.assertEqualCollection(el, el.closest('li'));
+ t.assertEqualCollection($('#nested'), el.closest('ul'));
+ // with context
+ t.assertEqualCollection($('#nested'), el.closest('ul', $('#li1').get(0)));
+ t.assertLength(0, el.closest('#parents', $('#li1').get(0)));
+ // no ancestor matched
+ t.assertLength(0, el.closest('div'));
},
testFind: function(t){
Please sign in to comment.
Something went wrong with that request. Please try again.