diff options
author | Rogan Creswick <creswick@gmail.com> | 2013-06-14 15:29:50 -0700 |
---|---|---|
committer | Rogan Creswick <creswick@gmail.com> | 2013-06-14 15:29:50 -0700 |
commit | f8c37c2ca06b3185ce988b6fd3552f0ea2ea27fa (patch) | |
tree | b13abc784d2b7262d2f4ffb73b66ae337da363fe /src/js | |
parent | a62026d7e1e0327b741d74c8a02bfcb3765f8b86 (diff) | |
parent | 038b25568753863542b2571c71b7f42ccd70fa3b (diff) |
Merge remote-tracking branch 'origin/master' into nlp-rules
Diffstat (limited to 'src/js')
-rw-r--r-- | src/js/fiveui/injected/prelude.js | 29 | ||||
-rw-r--r-- | src/js/fiveui/js/background.js | 3 | ||||
-rw-r--r-- | src/js/fiveui/js/options.js | 8 | ||||
-rw-r--r-- | src/js/fiveui/js/rules.js | 26 | ||||
-rw-r--r-- | src/js/tests/specs/prelude.js | 43 |
5 files changed, 88 insertions, 21 deletions
diff --git a/src/js/fiveui/injected/prelude.js b/src/js/fiveui/injected/prelude.js index fbe5307..c073c9f 100644 --- a/src/js/fiveui/injected/prelude.js +++ b/src/js/fiveui/injected/prelude.js @@ -271,6 +271,14 @@ shortHexToHex = function (color) { return stdColor.substr(0, 7); }; +// Compare RGBA objects +equalRGBA = function (c1, c2) { + return (c1.r == c2.r && + c1.g == c2.g && + c1.b == c2.b && + c1.a == c2.a); +}; + /** * Convert RGB values to Hex. */ @@ -329,13 +337,13 @@ fiveui.color.colorToRGB = function(color) { return fiveui.color.hexToRGB(fiveui.color.colorToHex(color)); } - var digits = /rgba?\((\d+), (\d+), (\d+)(, (\d.\d+))?/.exec(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 alpha = 1; - if (digits[5]) { + if (digits[5] != undefined) { alpha = parseFloat(digits[5]); } @@ -363,11 +371,12 @@ fiveui.color.findBGColor = function(obj) { var none = fc.colorToRGB('rgba(0, 0, 0, 0)'); if (real.a != 1) { + // find parents with a non-default bg color: var parents = obj.parents().filter( function() { var color = fc.colorToRGB($(this).css('background-color')); - return color != none; + return !equalRGBA(color, none); }).map( function(i) { return fc.colorToRGB($(this).css('background-color')); @@ -376,6 +385,7 @@ fiveui.color.findBGColor = function(obj) { // push a white element onto the end of parents parents.push({ r: 255, g: 255, b: 255, a: 1}); + // takeWhile alpha != 1 var colors = []; for (var i=0; i < parents.length; i++) { colors.push(parents[i]); @@ -384,9 +394,16 @@ fiveui.color.findBGColor = function(obj) { } } - // compose the colors and return: - return _.reduce(colors, fc.alphaCombine, none); - } else { + // Compose the colors and return. Note that fc.alphaCombine is + // neither commutative, nor associative, so we need to be carefull + // of the order in which parent colors are combined. + var res = real; + for (var i=0; i < colors.length; i++) { + res = fc.alphaCombine(res, colors[i]); + } + return res; + } + else { return real; } }; diff --git a/src/js/fiveui/js/background.js b/src/js/fiveui/js/background.js index 268526b..3d2a2f1 100644 --- a/src/js/fiveui/js/background.js +++ b/src/js/fiveui/js/background.js @@ -145,7 +145,8 @@ fiveui.Background.prototype.pageLoad = function(tabId, url, data) { } var computeScripts = _.flatten( - [ [ this.dataLoader('jquery/jquery-1.8.3.js') + [ [ this.dataLoader('underscore.js') + , this.dataLoader('jquery/jquery-1.8.3.js') , this.dataLoader('md5.js') , this.dataLoader('injected/prelude.js') , this.dataLoader('injected/jquery-plugins.js') diff --git a/src/js/fiveui/js/options.js b/src/js/fiveui/js/options.js index 5a4e48d..c4ec16d 100644 --- a/src/js/fiveui/js/options.js +++ b/src/js/fiveui/js/options.js @@ -318,7 +318,7 @@ fiveui.RuleSetEntry = Backbone.View.extend({ , ' <button class="edit">edit</button>' , ' <button class="reload">reload</button>' , '</div>' - , '<span class="title"><%= name %></span>' + , '<span class="title"><%= name %><%= license %></span>' ].join('')), // render the rule set as its title, with some buttons to edit/remove/reload @@ -327,6 +327,12 @@ fiveui.RuleSetEntry = Backbone.View.extend({ // render the rule set var attrs = _.clone(this.model.attributes); + + // add a comma to the license field, if it's present. + if(!_.isEmpty(attrs.license)) { + attrs.license = ', ' + attrs.license; + } + this.$rs.html(this.viewRsTemplate(attrs)); button(this.$rs.find('.edit'), { primary: 'ui-icon-pencil' }); diff --git a/src/js/fiveui/js/rules.js b/src/js/fiveui/js/rules.js index 7bd2a10..7e55b4f 100644 --- a/src/js/fiveui/js/rules.js +++ b/src/js/fiveui/js/rules.js @@ -41,10 +41,12 @@ fiveui.RuleSet.sanitize = function(obj) { rules: [], patterns: [], dependencies: [], + license: '', }; // scrub out any values that aren't in the defaults list, fill in any that are - // missing. + // missing. pick will implicitly return a copy, so it's OK to not clone obj + // here. return _.defaults(_.pick(obj, _.keys(defs)), defs); }; @@ -118,16 +120,20 @@ fiveui.RuleSet.load = function(manifest_url, options) { return; } + // set defaults in the parsed manifest var manifest = fiveui.RuleSet.sanitize(obj); - // explicitly zero out the patterns, they shouldn't be part of the // manifest. manifest.patterns = []; - var rules = manifest.rules; - manifest.rules = []; + // remove the rules, as they'll be added back once processed. + var rules = manifest.rules; + manifest.rules = []; + + // overwrite any source present with the one given by the user. manifest.source = manifest_url; + loadRules(manifest, rules); }, @@ -231,15 +237,9 @@ fiveui.RuleSetModel = Backbone.Model.extend({ * Generate a RuleSetModel from a RuleSet */ fromRuleSet: function(ruleSet,msg) { - return new fiveui.RuleSetModel({ - id: ruleSet.id, - name: ruleSet.name, - description: ruleSet.description, - rules: ruleSet.rules, - dependencies:ruleSet.dependencies, - source: ruleSet.source, - patterns: ruleSet.patterns, - }, { url : msg }); + return new fiveui.RuleSetModel(fiveui.RuleSet.sanitize(ruleSet), { + url: msg + }); }, }); diff --git a/src/js/tests/specs/prelude.js b/src/js/tests/specs/prelude.js index 93a12cb..84f6859 100644 --- a/src/js/tests/specs/prelude.js +++ b/src/js/tests/specs/prelude.js @@ -133,6 +133,49 @@ describe('prelude', function() { ]); + // to test fiveui.color.findBGColor, we need a jQuery obj for input + describe("fiveui.color.findBGColor", function () { + + beforeEach(function () { + // insert new div id=test to add test html content to + $("body").append($("<div id=test></div>")); + }); + + afterEach(function () { + // remove the test div + $("#test").remove(); + }); + + it("should report a white background", function () { + $("#test").append($('<div id=fbgc1 style="background-color: #FFFFFF">fbgc1</div>')); + obj = $("#fbgc1"); + oracle = { r: 255, g: 255, b: 255, a: 1 }; + expect(fiveui.color.findBGColor(obj)).toEqual(oracle); + }); + + it("should report a red background", function () { + $("#test").append($('<div id=fbgc2 style="background-color: #FF0000">fbgc2</div>')); + obj = $("#fbgc2"); + oracle = { r: 255, g: 0, b: 0, a: 1 }; + expect(fiveui.color.findBGColor(obj)).toEqual(oracle); + }); + + it("should report a transparent red over white background", function () { + $("#test").append($('<div id=fbgc3 style="background-color: #FFFFFF">' + + 'this bg is white ' + + '<span id=pinkspan ' + + 'style="background-color: rgba(255, 0, 0, 0.5)">' + + 'this bg is pink' + + '</span>' + + '</div>')); + obj = $("#pinkspan"); + oracle = { r: 255, g: 128, b: 128, a: 1 }; + expect(fiveui.color.findBGColor(obj)).toEqual(oracle); + }); + + }); + + var getFontTests = [ // CSS ID, Family, Weight, Size ['#getFontTest1', 'Arial', 'normal', '12'], |