diff options
Diffstat (limited to 'src/js/fiveui')
-rw-r--r-- | src/js/fiveui/injected/jquery-plugins.js | 61 | ||||
-rw-r--r-- | src/js/fiveui/injected/prelude.js | 35 |
2 files changed, 76 insertions, 20 deletions
diff --git a/src/js/fiveui/injected/jquery-plugins.js b/src/js/fiveui/injected/jquery-plugins.js index a7dedbd..a8c99f2 100644 --- a/src/js/fiveui/injected/jquery-plugins.js +++ b/src/js/fiveui/injected/jquery-plugins.js @@ -33,6 +33,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 */ @@ -41,6 +45,42 @@ fiveui.jquery.hasText = function (text) { }; /** + * Filter for elements which lack of the given attribute. + * + * Example: + * + * This object will be non-empty: + * + * $('<table></table>').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 + * + * $('<div><p>hello</p></div>').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. * @@ -48,17 +88,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 @@ -80,7 +123,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)); @@ -96,10 +142,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 9f84c96..cb1422a 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) { |