diff options
author | Benjamin Jones <bjones@galois.com> | 2012-10-05 17:49:40 -0700 |
---|---|---|
committer | Benjamin Jones <bjones@galois.com> | 2012-10-05 17:49:40 -0700 |
commit | ce511cd19b0b9a321aa740d75ef4a41ee57b7345 (patch) | |
tree | 7a833b54097a2228ee971286b4b5fd425a5c85ce | |
parent | 087f2dce8ff273eaee33afaf7af353d6cd2d98ec (diff) |
updates to fiveui.prelude
added fiveui.color.colorCheck -- simple rule compiler
added exampleData/ruleSets/colorRulesRF.json -- refactored color check
rule
improved fiveui.color.colorToHex
added tests to prelude-test.js
-rw-r--r-- | contexts/data/fiveui/injected/prelude-test.js | 20 | ||||
-rw-r--r-- | contexts/data/fiveui/injected/prelude.js | 46 | ||||
-rw-r--r-- | contexts/data/fiveui/options.js | 3 | ||||
-rw-r--r-- | exampleData/ruleSets/colorRulesRF.json | 17 |
4 files changed, 85 insertions, 1 deletions
diff --git a/contexts/data/fiveui/injected/prelude-test.js b/contexts/data/fiveui/injected/prelude-test.js index b97f698..f911419 100644 --- a/contexts/data/fiveui/injected/prelude-test.js +++ b/contexts/data/fiveui/injected/prelude-test.js @@ -20,6 +20,8 @@ */ goog.require('fiveui.prelude.string'); +goog.require('fiveui.prelude.word'); +goog.require('fiveui.prelude.color'); goog.require('goog.asserts'); goog.require('goog.testing'); @@ -120,5 +122,23 @@ var runTests = function() { }); }); + // custom assertions for rule compiler + goog.asserts.assert(typeof fiveui.color.colorCheck('', ['']) === 'function', + 'Wrong type for: fiveui.color.colorCheck( ... )'); + + var colorToHexTests = [ + ['colorToHex: full white' , '#000000', '#000000'], + ['colorToHex: abreviated white 1' , '#0', '#000000'], + ['colorToHex: abreviated white 2' , '#00', '#000000'], + ['colorToHex: black' , '#FFFFFF', '#FFFFFF'], + ['colorToHex: abreviated black' , '#FF', '#FFFFFF'], + ['colorToHex: abreviated C7 grey' , '#C7', '#C7C7C7'], + ['colorToHex: rgb(0, 0, 0)' , 'rgb(0, 0, 0)', '#000000'], + ['colorToHex: rgb(255, 255, 255)' , 'rgb(255, 255, 255)', '#FFFFFF'], + ['colorToHex: rgb(222, 173, 190)' , 'rgb(222, 173, 190)', '#DEADBE'], + ['colorToHex: rgba(255, 255, 255, 100)', 'rgba(255, 255, 255, 100)', '#FFFFFF'] // alpha is ignored + ]; + test.addTestSet(fiveui.color.colorToHex, colorToHexTests); + test.run(); }; diff --git a/contexts/data/fiveui/injected/prelude.js b/contexts/data/fiveui/injected/prelude.js index bd342e6..4e37148 100644 --- a/contexts/data/fiveui/injected/prelude.js +++ b/contexts/data/fiveui/injected/prelude.js @@ -21,6 +21,11 @@ if (typeof goog != 'undefined') { goog.provide('fiveui.prelude.string'); + goog.provide('fiveui.prelude.word'); + goog.provide('fiveui.prelude.color'); + goog.provide('fiveui.prelude.font'); + + goog.require('goog.json'); } /** @@ -191,11 +196,45 @@ fiveui.word.allCaps = function(word) { */ fiveui.color = {}; +/* Color check compiler. + * @param {!string} selector The HTML element selector to check. + * @param {!array} colorSet An array of strings containing the HEX values of + * colors in the desired color set. + * @returns {!function} A function which checks the rule + */ +fiveui.color.colorCheck = function (selector, colorSet) { + var allowable, i, fnStr, forEachFuncStr; + allowable = {}; + for (i = 0; i < colorSet.length; i += 1) { allowable[colorSet[i]] = true; } + forEachFuncStr = 'function (j, elt) {\n' + + ' var allowable = ' + goog.json.serialize(allowable) + ';\n' + + ' var color = fiveui.color.colorToHex($(elt).css("color"));\n' + + ' if (!(color in allowable)) {\n' + + ' report("Disallowed color " + color + " in element matching " + ' + goog.json.serialize(selector) + ', $(elt));\n' + + ' }\n' + + '}\n'; + fnStr = 'function () { fiveui.query("' + selector + '").each(' + forEachFuncStr + '); }'; + return eval('false||'+fnStr); // the `false||` trick is required for eval to parse a + // function expression ?!? +}; + /* covert rgb(a) colors to hex */ fiveui.color.colorToHex = function(color) { + var have, need; if (color.substr(0, 1) === '#') { + if (color.length === 7) { return color; + } + else { // deal with #0 or #F7 cases + var have = color.length - 1; + var haveDigits = color.substr(1, color.length); + var need = 6 - have; + var reps = Math.ceil(need / have); + for (i = 0, stdColor = color; i < reps; i += 1) { stdColor += haveDigits; } + return stdColor.substr(0, 7); + } } + var digits = /rgb(a)?\((\d+), (\d+), (\d+)/.exec(color); var red = parseInt(digits[2]); @@ -203,7 +242,12 @@ fiveui.color.colorToHex = function(color) { var blue = parseInt(digits[4]); var rgb = blue | (green << 8) | (red << 16); - return '#' + rgb.toString(16).toUpperCase(); + if (rgb === 0) { + return '#000000'; // normalized form + } + else { + return '#' + rgb.toString(16).toUpperCase(); + } }; diff --git a/contexts/data/fiveui/options.js b/contexts/data/fiveui/options.js index 71bc2e3..f8aaac8 100644 --- a/contexts/data/fiveui/options.js +++ b/contexts/data/fiveui/options.js @@ -28,6 +28,9 @@ goog.require('fiveui.RuleSetEntry'); goog.require('fiveui.UpdateManager'); goog.require('fiveui.UrlPat'); goog.require('fiveui.UrlPatEntry'); + +goog.require('fiveui.prelude.color'); + goog.require('goog.dom'); goog.require('goog.dom.forms'); goog.require('goog.dom.query'); diff --git a/exampleData/ruleSets/colorRulesRF.json b/exampleData/ruleSets/colorRulesRF.json new file mode 100644 index 0000000..1bd41a5 --- /dev/null +++ b/exampleData/ruleSets/colorRulesRF.json @@ -0,0 +1,17 @@ +/* colorRules.json + * Author: Benjamin Jones <bjones@galois.com> + * + * Simple rules for checking that the colors of specific elements are in a specific set. + * Test using exampleData/basic/testColorRules.html + */ + +{ 'name': "Colors are in a specified set" +, 'description': "All background and foreground colors used on a page should be in a specificed set" +, 'rules': [ + { 'name': "Foregrounds" + , 'description': "Foreground colors should be in the set:"+ + "'#0, #FFFFFF, #3D3D3D, #B4B4B4, #4E4E4E, #C2C2C2, #F7F7F7" + , 'rule': fiveui.color.colorCheck(':visible', [ '#0', '#000000', '#FFFFFF', '#3D3D3D', '#B4B4B4', '#4E4E4E', '#C2C2C2', '#F7F7F7' ]) + } + ] +} |