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

Commit 7a3fdda

Browse files
Di PengIgorMinar
Di Peng
authored andcommitted
feat(jqlite): added show(),hide() and eq() methods to jqlite
- add those three methods to jqlite
1 parent b4f18fc commit 7a3fdda

File tree

3 files changed

+106
-2
lines changed

3 files changed

+106
-2
lines changed

src/jqLite.js

+35-1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@
4444
* - [replaceWith()](http://api.jquery.com/replaceWith/)
4545
* - [text()](http://api.jquery.com/text/)
4646
* - [trigger()](http://api.jquery.com/trigger/)
47+
* - [eq()](http://api.jquery.com/eq/)
48+
* - [show()](http://api.jquery.com/show/)
49+
* - [hide()](http://api.jquery.com/hide/)
4750
*
4851
* ## Additionally these methods extend the jQuery and are available in both jQuery and jQuery lite
4952
* version:
@@ -211,11 +214,16 @@ var JQLitePrototype = JQLite.prototype = {
211214
// we can not use jqLite since we are not done loading and jQuery could be loaded later.
212215
jqLiteWrap(window).bind('load', trigger); // fallback to window.onload for others
213216
},
214-
toString: function(){
217+
toString: function() {
215218
var value = [];
216219
forEach(this, function(e){ value.push('' + e);});
217220
return '[' + value.join(', ') + ']';
218221
},
222+
223+
eq: function(index) {
224+
return (index >= 0) ? jqLite(this[index]) : jqLite(this[this.length + index]);
225+
},
226+
219227
length: 0,
220228
push: push,
221229
sort: [].sort,
@@ -463,6 +471,32 @@ forEach({
463471
return element.getElementsByTagName(selector);
464472
},
465473

474+
hide: function(element) {
475+
if (element.style) {
476+
if(element.style.display !=="none" && !JQLiteData(element,"olddisplay")) {
477+
JQLiteData( element, "olddisplay", element.style.display);
478+
}
479+
element.style.display = "none";
480+
}
481+
},
482+
483+
show: function(element) {
484+
if(element.style) {
485+
var display = element.style.display;
486+
if ( display === "" || display === "none" ) {
487+
488+
// restore the original value overwritten by hide if present or default to nothing (which
489+
// will let browser correctly choose between 'inline' or 'block')
490+
element.style.display = JQLiteData(element, "olddisplay") || "";
491+
492+
// if the previous didn't make the element visible then there are some cascading rules that
493+
// are still hiding it, so let's default to 'block', which might be incorrect in case of
494+
// elmenents that should be 'inline' by default, but oh well :-)
495+
if (!isVisible([element])) element.style.display = "block";
496+
}
497+
}
498+
},
499+
466500
clone: JQLiteClone
467501
}, function(fn, name){
468502
/**

test/jqLiteSpec.js

+66
Original file line numberDiff line numberDiff line change
@@ -507,4 +507,70 @@ describe('jqLite', function(){
507507
expect(innerDiv.html()).toEqual('text');
508508
});
509509
});
510+
511+
512+
describe('hide', function() {
513+
var element;
514+
515+
afterEach(function() {
516+
if (element) dealoc(element);
517+
});
518+
519+
it('should hide the element', function() {
520+
element = jqLite('<div></div>');
521+
expect(isCssVisible(element)).toBe(true);
522+
element.hide();
523+
expect(isCssVisible(element)).toBe(false);
524+
});
525+
});
526+
527+
528+
describe('show', function() {
529+
var element;
530+
531+
afterEach(function() {
532+
if (element) dealoc(element);
533+
element.remove();
534+
});
535+
536+
537+
it('should show the element ', function() {
538+
element = jqLite('<div></div>');
539+
element[0].style.display = 'none';
540+
expect(isCssVisible(element)).toBe(false);
541+
element.show();
542+
expect(isCssVisible(element)).toBe(true);
543+
});
544+
545+
546+
it('should show previously hidden element and preserve the display value', function() {
547+
element = jqLite('<div style="display:inline">xx</div>');
548+
jqLite(document.body).append(element);
549+
element.hide();
550+
expect(isCssVisible(element)).toBe(false);
551+
element.show();
552+
expect(element[0].style.display).toBe('inline');
553+
expect(isCssVisible(element)).toBe(true);
554+
555+
element[0].style.display = 'block';
556+
element.hide();
557+
expect(isCssVisible(element)).toBe(false);
558+
element.show();
559+
expect(isCssVisible(element)).toBe(true);
560+
561+
// this totally doesn't make sense, it should be 'block', but jquery (1.4.2+1.6.2) behaves
562+
// this way.
563+
expect(element[0].style.display).toBe('inline');
564+
});
565+
});
566+
567+
568+
describe('eq', function() {
569+
it('should select the nth element ', function() {
570+
var element = jqLite('<div><span>aa</span></div><div><span>bb</span></div>');
571+
expect(element.find('span').eq(0).html()).toBe('aa');
572+
expect(element.find('span').eq(-1).html()).toBe('bb');
573+
expect(element.find('span').eq(20).length).toBe(0);;
574+
});
575+
});
510576
});

test/testabilityPatch.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -274,9 +274,13 @@ function sortedHtml(element, showNgClass) {
274274
return html;
275275
}
276276

277+
278+
/**
279+
* This method is a cheap way of testing if css for a given node is not set to 'none'. It doesn't
280+
* actually test if an element is displayed by the browser. Be aware!!!
281+
*/
277282
function isCssVisible(node) {
278283
var display = node.css('display');
279-
if (display == 'block') display = "";
280284
return display != 'none';
281285
}
282286

0 commit comments

Comments
 (0)