aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Benjamin Jones <bjones@galois.com>2013-07-23 18:11:28 -0700
committerGravatar Benjamin Jones <bjones@galois.com>2013-07-23 18:11:28 -0700
commit03f6dcbb143849faf318bb6a716aa2e39beb7117 (patch)
tree8ba465213a74f16a1ff56d92b53de1bfeed33465
parent4645e497ef84875c201a2a6d69accf23f6e2d0bb (diff)
added new jQuery plugin called attrFilter along with unit tests for it
-rw-r--r--src/js/fiveui/injected/jquery-plugins.js21
-rw-r--r--src/js/tests/specs/jquery-plugins.js37
2 files changed, 54 insertions, 4 deletions
diff --git a/src/js/fiveui/injected/jquery-plugins.js b/src/js/fiveui/injected/jquery-plugins.js
index 5a32031..1589972 100644
--- a/src/js/fiveui/injected/jquery-plugins.js
+++ b/src/js/fiveui/injected/jquery-plugins.js
@@ -43,7 +43,8 @@ fiveui.jquery.hasText = function (text) {
};
/**
- * Filter for elements which lack of the given attribute.
+ * Filter for elements which lack of the given attribute. (see also
+ * fiveui.jquery.attrFilter)
*
* Example:
*
@@ -133,6 +134,24 @@ fiveui.jquery.cssIsNot = function (prop, set, fn) {
}
/**
+ * General attribute filter
+ *
+ * @description This plugin filters for elements whose attribute `a` pass
+ * the predicate `fn`, which should take a string and return true or false.
+ * Elements that don't have the attribute are automatically filtered out.
+ *
+ * @param {String} a element attribute name
+ * @param {Function} fn a predicate to run on the element attribute
+ * @returns {Object} jQuery object
+ */
+fiveui.jquery.attrFilter = function (a, fn) {
+ return this.filter(function () {
+ var x = $(this).attr(a);
+ return x != undefined && fn(x);
+ });
+}
+
+/**
* Filter out elements that do not contain the attribute
* href=`href`.
*
diff --git a/src/js/tests/specs/jquery-plugins.js b/src/js/tests/specs/jquery-plugins.js
index 7fc196b..04eb0fb 100644
--- a/src/js/tests/specs/jquery-plugins.js
+++ b/src/js/tests/specs/jquery-plugins.js
@@ -86,7 +86,9 @@ describe('jQuery plugins', function () {
});
it('filters out everything', function () {
- expect($t.notColorSet(['#ffffff', '#000000', '#e1e1e1']).length).toEqual(0);
+ expect(
+ $t.notColorSet(['#ffffff', '#000000', '#e1e1e1']).length).toEqual(0
+ );
});
});
@@ -99,11 +101,15 @@ describe('jQuery plugins', function () {
var $t = $(htm);
it('filters out colors', function () {
- expect($t.cssIsNot('color', ['#ffffff', '#000000'], fiveui.color.colorToHexWithDefault).length).toEqual(1);
+ expect($t.cssIsNot('color',
+ ['#ffffff', '#000000'],
+ fiveui.color.colorToHexWithDefault).length).toEqual(1);
});
it('filters out background-colors', function () {
- expect($t.cssIsNot('background-color', ['#141414', '#232323'], fiveui.color.colorToHexWithDefault).length).toEqual(3);
+ expect($t.cssIsNot('background-color',
+ ['#141414', '#232323'],
+ fiveui.color.colorToHexWithDefault).length).toEqual(3);
});
it('filters out elements of different type', function () {
@@ -111,6 +117,31 @@ describe('jQuery plugins', function () {
});
});
+ describe('fiveui.jquery.attrFilter', function () {
+ var isEmpty = function (s) { return $.trim(s) == ""; };
+
+ it('filters for zero elements with empty alt', function () {
+ var $elt = $('<a href="foo" alt="bar"><a href="beel" alt="bar">');
+ expect($elt.attrFilter('alt', isEmpty).length).toEqual(0);
+ });
+
+ it('filters for one element with empty alt', function () {
+ var $elt = $('<a href="foo" alt=""><a href="beel" alt="bar">');
+ expect($elt.attrFilter('alt', isEmpty).length).toEqual(1);
+ });
+
+ it('filters out elements that don\'t have the attribute', function () {
+ var $elt = $('<a href="foo"><a href="beel">');
+ expect($elt.attrFilter('alt', isEmpty).length).toEqual(0);
+ });
+
+ it('filters for elements with alt containing foo', function () {
+ var hasFoo = function (s) { return /foo/.test(s); };
+ var $elt = $('<a href="foo" alt="barfoo"><a href="beel" alt="bar">');
+ expect($elt.attrFilter('alt', hasFoo).length).toEqual(1);
+ });
+ });
+
describe('fiveui.jquery.linksTo', function () {
it('filters out elements with no href', function () {
expect($('<p>foo</p>').linksTo('bar').length).toEqual(0);