From e089832e50c87a79a9872009fd47231473d81b7a Mon Sep 17 00:00:00 2001 From: Benjamin Jones Date: Tue, 16 Jul 2013 14:44:19 -0700 Subject: modified: src/js/fiveui/injected/jquery-plugins.js added two new plugins -- noAttr and noSubElt fixed bug in notColorSet modified: src/js/fiveui/injected/prelude.js fixed bugs and added better error handling to the fiveui.color functions new file: src/js/tests/specs/jquery-plugins.js added unit tests for all the (non-debugging) jQuery plugins --- src/js/fiveui/injected/jquery-plugins.js | 61 +++++++++++++++++++++++++++----- src/js/fiveui/injected/prelude.js | 35 ++++++++++++------ 2 files changed, 76 insertions(+), 20 deletions(-) (limited to 'src/js/fiveui') diff --git a/src/js/fiveui/injected/jquery-plugins.js b/src/js/fiveui/injected/jquery-plugins.js index 43cfbd6..5a32031 100644 --- a/src/js/fiveui/injected/jquery-plugins.js +++ b/src/js/fiveui/injected/jquery-plugins.js @@ -31,6 +31,10 @@ fiveui.jquery = fiveui.jquery || {}; /** * Wrapper for the :contains('text') selector * + * Example: + * + * $('div').hasText('to remove').remove(); + * * @param {!String} text Text to select for * @returns {!Object} A modified jQuery object */ @@ -38,6 +42,42 @@ fiveui.jquery.hasText = function (text) { return this.filter(":contains('" + text + "')"); }; +/** + * Filter for elements which lack of the given attribute. + * + * Example: + * + * This object will be non-empty: + * + * $('
').noAttr('summary') + * + * @param {!String} attribute name + * @returns {!Object} a filtered jQuery object + */ +fiveui.jquery.noAttr = function (name) { + return this.filter(function () { + $attr = $.trim($(this).attr(name)); + return $attr == undefined || $attr == ''; + }); +}; + + +/** + * Filter for elements having no sub-elements matching the given selector. + * + * Example: the following should contain no elements + * + * $('

hello

').noSubElt('p') + * + * @param {!String} sel a jQuery selector + * @param {!Object} A filtered jQuery object + */ +fiveui.jquery.noSubElt = function (sel) { + return this.filter(function () { + return $(this).find(sel).length == 0; + }); +}; + /** * Color checker plugin: filters for elements whose CSS color property is * not in the given set. @@ -46,17 +86,20 @@ fiveui.jquery.hasText = function (text) { * $(..).notColorSet(set) == $(..).cssIsNot("color", set, fiveui.color.colorToHex) * @see {fiveui.color.colorToHex} * - * @param {String[]} cset A set of allowable color strings + * @param {String[]} cset An array of allowable color strings * @returns {!Object} A modified jQuery object */ fiveui.jquery.notColorSet = function (cset) { var allowable = {}; - for (var i = 0; i < cset.length; i += 1) { allowable[cset[i]] = true; } // array -> object + // input array -> object + for (var i = 0; i < cset.length; i += 1) { + allowable[fiveui.color.colorToHex(cset[i])] = true; + } return this.filter(function (index) { - var color = fiveui.color.colorToHex($(this).css("color")); // .css("color") returns rgb(...) + var color = fiveui.color.colorToHexWithDefault($(this).css("color")); // .css("color") returns rgb(...) return !(color in allowable); }); -} +}; /** * General CSS propetry checker plugin @@ -78,7 +121,10 @@ fiveui.jquery.cssIsNot = function (prop, set, fn) { allowable[fn(set)] = true; } else { // assume `set` is an array of strings - for (var i = 0; i < set.length; i += 1) { allowable[fn(set[i])] = true; } // array -> object + // 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)); @@ -94,10 +140,7 @@ fiveui.jquery.cssIsNot = function (prop, set, fn) { * @returns {Object} jQuery object */ fiveui.jquery.linksTo = function (href) { - return this.filter(function (index) { - var addr = $(this).attr("href"); - return (addr == href); - }); + return this.filter('[href=' + href + ']'); } /** diff --git a/src/js/fiveui/injected/prelude.js b/src/js/fiveui/injected/prelude.js index ce242f6..db32d7f 100644 --- a/src/js/fiveui/injected/prelude.js +++ b/src/js/fiveui/injected/prelude.js @@ -300,7 +300,7 @@ fiveui.color.hexToRGB = function (hex) { /** * Covert rgb colors to hex and abreviated hex colors to their full 3 byte - * form. + * and uppercase normal form. * * In case there are parse errors during the conversion, i.e. color values * that are not understood, the input is returned unchanged. @@ -312,16 +312,29 @@ fiveui.color.hexToRGB = function (hex) { fiveui.color.colorToHex = function(color) { if (color.substr(0, 1) === '#') { if (color.length === 7) { - return color; + return color.toUpperCase(); } else { // deal with #0 or #F7 cases - return shortHexToHex(color); + return shortHexToHex(color).toUpperCase(); } } - else { // color == 'rgb...' + else if (color.substr(0,3) === 'rgb') { var c = fiveui.color.colorToRGB(color) return fiveui.color.rgbToHex(c.r, c.g, c.b); } + else { + throw new Error('could not convert color string "' + color + '"'); + } +}; + +fiveui.color.colorToHexWithDefault = function (color) { + try { + return fiveui.color.colorToHex(color); + } + catch (e) { + console.log(e); + return color; + } }; /** @@ -333,14 +346,14 @@ fiveui.color.colorToHex = function(color) { */ fiveui.color.colorToRGB = function(color) { - if (color.substr(0, 1) === '#') { - return fiveui.color.hexToRGB(fiveui.color.colorToHex(color)); - } + if (color.substr(0, 1) === '#') { + return fiveui.color.hexToRGB(fiveui.color.colorToHex(color)); + } - var digits = /rgba?\((\d+), (\d+), (\d+)(, ([-+]?[0-9]*\.?[0-9]+))?/.exec(color); - if (!digits) { - throw new ParseError('could not parse color string: ' + color); - } + var digits = /rgba?\((\d+), (\d+), (\d+)(, ([-+]?[0-9]*\.?[0-9]+))?/.exec(color); + if (!digits) { + throw new Error('could not parse color string: "' + color + '"'); + } var alpha = 1; if (digits[5] != undefined) { -- cgit v1.2.3