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 +++++--- src/js/tests/PhantomJSJasmineRunner.html | 3 + src/js/tests/SpecRunner.html | 2 + src/js/tests/specs/jquery-plugins.js | 135 +++++++++++++++++++++++++++++++ 5 files changed, 216 insertions(+), 20 deletions(-) create mode 100644 src/js/tests/specs/jquery-plugins.js 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) { diff --git a/src/js/tests/PhantomJSJasmineRunner.html b/src/js/tests/PhantomJSJasmineRunner.html index 606a251..907e176 100644 --- a/src/js/tests/PhantomJSJasmineRunner.html +++ b/src/js/tests/PhantomJSJasmineRunner.html @@ -26,6 +26,7 @@ + @@ -35,6 +36,8 @@ + + diff --git a/src/js/tests/SpecRunner.html b/src/js/tests/SpecRunner.html index 606967e..caf0248 100644 --- a/src/js/tests/SpecRunner.html +++ b/src/js/tests/SpecRunner.html @@ -24,6 +24,7 @@ + @@ -32,6 +33,7 @@ + diff --git a/src/js/tests/specs/jquery-plugins.js b/src/js/tests/specs/jquery-plugins.js new file mode 100644 index 0000000..7fc196b --- /dev/null +++ b/src/js/tests/specs/jquery-plugins.js @@ -0,0 +1,135 @@ +describe('jQuery plugins', function () { + + describe('fiveui.jquery.hasText', function () { + it('finds an element containing "text foo bar"', function () { + var $t = $('
text foo bar
'); + expect($t.hasText('text foo bar').length).toEqual(1); + }); + + it('finds no element containing "quux"', function () { + var $t = $('
text foo bar
'); + expect($t.hasText('quux').length).toEqual(0); + }); + + it('finds a nested element containing "hobbit"', function () { + $t = $('

golum

hobbit
'); + expect($t.hasText('hobbit').length).toEqual(1); + }); + }); + + describe('fiveui.jquery.noAttr', function () { + it('returns elements having no summary attribute', function () { + var $t = $('
').noAttr('summary'); + expect($t.length).toEqual(1); + }); + + it('returns no elements on empty input', function () { + var $t = $('').noAttr('summary'); + expect($t.length).toEqual(0); + }); + + it('doesn\'t return elements with attributes other than the given', function () { + var $t = $('
').noAttr('summary'); + expect($t.length).toEqual(1); + }); + + it('returns multiple elements having no summary attribute', function () { + var htm = '
' + + '
' + + '
' + + '
' + + '
'; + var $t = $(htm).noAttr('summary'); + expect($t.length).toEqual(3); + }); + + }); + + describe('fiveui.jquery.noSubElt', function () { + var $t = $('

red hering

blue hering

') + + it('filters out elements with a sub-heading', function () { + expect($t.noSubElt('h1').length).toEqual(0); + expect($t.noSubElt(':header').length).toEqual(0); + }); + + it('filters out elements with a

', function () { + expect($t.noSubElt('p').length).toEqual(0); + }); + + it('retains elements without

  • ', function () { + expect($t.noSubElt('li').length).toEqual(1); + }); + + it('accepts arbitrary jQuery seclectors', function () { + expect($t.noSubElt('p[name=bob]').length).toEqual(1); + }); + }); + + describe('fiveui.jquery.notColorSet', function () { + var htm = '

    foo

    ' + + '

    foo

    ' + + '

    foo

    ' + + '

    foo

    '; + var $t = $(htm); + + it('filters out black', function () { + expect($t.notColorSet(['#000000']).length).toEqual(3); + }); + + it('filters out white', function () { + expect($t.notColorSet(['#ffffff']).length).toEqual(2); + }); + + it('filters out black and white', function () { + expect($t.notColorSet(['#ffffff', '#000000']).length).toEqual(1); + }); + + it('filters out everything', function () { + expect($t.notColorSet(['#ffffff', '#000000', '#e1e1e1']).length).toEqual(0); + }); + }); + + describe('fiveui.jquery.cssIsNot', function () { + var htm = '

    foo

    ' + + '

    foo

    ' + + '

    foo

    ' + + '

    foo

    ' + + '

    big

    '; + var $t = $(htm); + + it('filters out colors', function () { + 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); + }); + + it('filters out elements of different type', function () { + expect($t.cssIsNot('font-size', ['5em']).length).toEqual(3); + }); + }); + + describe('fiveui.jquery.linksTo', function () { + it('filters out elements with no href', function () { + expect($('

    foo

    ').linksTo('bar').length).toEqual(0); + }); + + it('filters out elements with wrong href', function () { + expect($('foo').linksTo('bar').length).toEqual(0); + }); + + var htm = 'foo foo2 foo3'; + var $t = $(htm); + + it('filters in among various hrefs', function () { + expect($t.linksTo('bar').length).toEqual(1); + }); + + it('filters out among various hrefs', function () { + expect($t.linksTo('quux').length).toEqual(2); + }); + }); + +}); -- cgit v1.2.3