From e2cd166c98a9cb712579d4b150015f038f713131 Mon Sep 17 00:00:00 2001 From: Benjamin Jones Date: Wed, 24 Jul 2013 15:28:15 -0700 Subject: added cssIs jQuery plugin --- src/js/fiveui/injected/jquery-plugins.js | 47 +++++++++++++++++++------------- src/js/tests/specs/jquery-plugins.js | 25 +++++++++++++++++ 2 files changed, 53 insertions(+), 19 deletions(-) diff --git a/src/js/fiveui/injected/jquery-plugins.js b/src/js/fiveui/injected/jquery-plugins.js index 1589972..d210de6 100644 --- a/src/js/fiveui/injected/jquery-plugins.js +++ b/src/js/fiveui/injected/jquery-plugins.js @@ -102,6 +102,27 @@ fiveui.jquery.notColorSet = function (cset) { }); }; + +fiveui.jquery._makeCss = function (pos) { + return function (prop, set, fn) { + var allowable = {}; + fn = fn || function (x) { return x; }; // default is Id + if (typeof set === "string") { + allowable[fn(set)] = true; + } + else { // assume `set` is an array of strings + // array -> object + for (var i = 0; i < set.length; i += 1) { + allowable[fn(set[i])] = true; + } + } + return this.filter(function (index) { + var cssProp = fn($(this).css(prop)); + return pos ? (cssProp in allowable) : !(cssProp in allowable); + }); + }; +}; + /** * General CSS propetry checker plugin * @@ -111,27 +132,15 @@ fiveui.jquery.notColorSet = function (cset) { * browser returns so they can be compared to values in `cset`. * * @param {String} prop CSS property selector - * @param {String|String[]} set allowable values (either a string or an array of strings) - * @param {function(String):String} [fn] Function to apply to return values of $(this).css(prop), fn defaults to the identity function. + * @param {String|String[]} set allowable values (either a string or an array + * of strings) + * @param {function(String):String} [fn] Function to apply to return values + * of $(this).css(prop), fn defaults to + * the identity function. * @returns {Object} jQuery object */ -fiveui.jquery.cssIsNot = function (prop, set, fn) { - var allowable = {}; - fn = fn || function (x) { return x; }; // default is Id - if (typeof set === "string") { - allowable[fn(set)] = true; - } - else { // assume `set` is an array of strings - // array -> object - for (var i = 0; i < set.length; i += 1) { - allowable[fn(set[i])] = true; - } - } - return this.filter(function (index) { - var cssProp = fn($(this).css(prop)); - return !(cssProp in allowable); - }); -} +fiveui.jquery.cssIs = fiveui.jquery._makeCss(true); +fiveui.jquery.cssIsNot = fiveui.jquery._makeCss(false); /** * General attribute filter diff --git a/src/js/tests/specs/jquery-plugins.js b/src/js/tests/specs/jquery-plugins.js index 04eb0fb..2f33b1a 100644 --- a/src/js/tests/specs/jquery-plugins.js +++ b/src/js/tests/specs/jquery-plugins.js @@ -92,6 +92,31 @@ describe('jQuery plugins', function () { }); }); + describe('fiveui.jquery.cssIs', function () { + var htm = '

foo

' + + '

foo

' + + '

foo

' + + '

foo

' + + '

big

'; + var $t = $(htm); + + it('filters out colors', function () { + expect($t.cssIs('color', + ['#ffffff', '#000000'], + fiveui.color.colorToHexWithDefault).length).toEqual(4); + }); + + it('filters out background-colors', function () { + expect($t.cssIs('background-color', + ['#141414', '#232323'], + fiveui.color.colorToHexWithDefault).length).toEqual(2); + }); + + it('filters out elements of different type', function () { + expect($t.cssIs('font-size', ['5em']).length).toEqual(2); + }); + }); + describe('fiveui.jquery.cssIsNot', function () { var htm = '

foo

' + '

foo

' + -- cgit v1.2.3