diff options
author | Benjamin Jones <bjones@galois.com> | 2013-07-24 15:28:15 -0700 |
---|---|---|
committer | Benjamin Jones <bjones@galois.com> | 2013-07-24 15:28:15 -0700 |
commit | e2cd166c98a9cb712579d4b150015f038f713131 (patch) | |
tree | d33b724639ef5a04295f7388f2585ad528c090fa | |
parent | 03f6dcbb143849faf318bb6a716aa2e39beb7117 (diff) |
added cssIs jQuery plugin
-rw-r--r-- | src/js/fiveui/injected/jquery-plugins.js | 47 | ||||
-rw-r--r-- | 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 = '<p style="color: #000000; background-color: #232323">foo</p>' + + '<p style="color: #ffffff; font-size: 5em">foo</p>' + + '<p style="color: #e1e1e1; background-color: #141414">foo</p>' + + '<p style="color: #ffffff">foo</p>' + + '<h1 style="color: #ffffff; font-size: 5em">big</h1>'; + 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 = '<p style="color: #000000; background-color: #232323">foo</p>' + '<p style="color: #ffffff; font-size: 5em">foo</p>' + |