Skip to content

Commit

Permalink
Merge pull request #30 from bboyle/hidden-polyfill
Browse files Browse the repository at this point in the history
fix detection of `@hidden` support
  • Loading branch information
bboyle committed Feb 27, 2015
2 parents b033b3e + fc28c59 commit f0a0d67
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 12 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "relevance",
"description": "Progressive disclosure (relevance) for HTML elements",
"version": "2.0.0",
"version": "2.0.1",
"homepage": "https://github.com/bboyle/relevance",
"authors": [
"Ben Boyle <benjamins.boyle@gmail.com>"
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "relevance",
"title": "relevance",
"description": "Progressive disclosure (relevance) for HTML elements",
"version": "2.0.0",
"version": "2.0.1",
"homepage": "https://github.com/bboyle/relevance",
"author": {
"name": "Ben Boyle",
Expand Down
8 changes: 4 additions & 4 deletions src/relevance.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ if ( jQuery !== 'undefined' ) {
elementsToDisable = 'button, input, select, textarea',
polyfillHidden = (function() {
var hidden = $( '<div hidden></div>' );
var isHidden = hidden.appendTo( 'body' ).is( ':visible' );
var hiddenSupported = hidden.appendTo( 'body' ).is( ':hidden' );
hidden.remove();
return ! isHidden;
return ! hiddenSupported;
}()),

formElementsByName = function( form, name ) {
Expand Down Expand Up @@ -97,9 +97,9 @@ if ( jQuery !== 'undefined' ) {
relevant: function( makeRelevant ) {
var targets;
if ( makeRelevant ) {
targets = this.filter( '[hidden]' ).trigger( relevantEvent );
targets = this.filter( ':hidden' ).trigger( relevantEvent );
} else {
targets = this.not( '[hidden]' ).trigger( irrelevantEvent );
targets = this.not( ':hidden' ).trigger( irrelevantEvent );
}
if ( targets.length ) {
recalculateDependents.call( targets, makeRelevant );
Expand Down
7 changes: 7 additions & 0 deletions test/events.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@
color: #111;
}

[hidden] {
display: none;
}
input {
_display: expression(this.hidden?'none':'block');
}

#qunit-fixture {
position: static;
}
Expand Down
29 changes: 23 additions & 6 deletions test/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,33 +42,48 @@

});


test( 'relevant event bubbles', 3, function() {

$( '#bar' ).parent().bind( 'relevant', function() {
$( '#bar' ).parent().bind( 'relevant.test', function() {
ok( true, 'relevant event detected on #bar parent with .bind()' );
});
$( 'body' ).bind( 'relevant', function() {
$( 'body' ).bind( 'relevant.test', function() {
ok( true, 'relevant event detected with body.bind()' );
});
$( document ).bind( 'relevant', function() {
$( document ).bind( 'relevant.test', function() {
ok( true, 'relevant event detected with document.bind()' );
});

$( '#bar' ).relevance( 'relevant', true );

$( '#foo, body' ).unbind( 'relevant.test' );
$( '#bar, body' ).unbind( 'relevant.test' );
$( document ).unbind( 'relevant.test' );

});


test( 'relevant event triggered when nested', 1, function() {
var eventDetected = 0;
$( '#foo' ).bind( 'relevant.test', function() {
eventDetected += 1;
strictEqual( eventDetected, 1, 'relevant event detected for nested element' );
});

$( '#foo' ).closest( '.group' ).relevance( 'relevant', false );
$( '#foo' ).relevance( 'relevant', true );

$( '#foo' ).unbind( 'relevant.test' );
});


module( 'irrelevant events' );

test( 'irrelevant event is triggered for visible elements', 1, function() {

var eventDetected = 0;

$( '#foo' ).bind( 'irrelevant', function() {
$( '#foo' ).bind( 'irrelevant.test', function() {
eventDetected += 1;
strictEqual( eventDetected, 1, 'irrelevant event detected' );
});
Expand All @@ -77,16 +92,18 @@
// should not fire event now element is irrelevant
$( '#foo' ).relevance( 'relevant', false );

$( '#foo' ).unbind( 'irrelevant.test' );
});

test( 'irrelevant event not triggered for hidden elements', 0, function() {

$( '#bar' ).bind( 'irrelevant', function() {
$( '#bar' ).bind( 'irrelevant.test', function() {
ok( true, 'irrelevant event detected' );
});

$( '#bar' ).relevance( 'relevant', false );

$( '#bar' ).unbind( 'irrelevant.test' );
});

test( 'irrelevant event bubbles', 3, function() {
Expand Down

0 comments on commit f0a0d67

Please sign in to comment.