浏览代码

Add validator and test suite for hsla() color validation

Emil Rømer Christensen 11 年之前
父节点
当前提交
9e2310af97

+ 4 - 0
dist/css/bootstrapValidator.min.css

@@ -9,6 +9,7 @@
 <<<<<<< HEAD
 <<<<<<< HEAD
 <<<<<<< HEAD
+<<<<<<< HEAD
  * @version     v0.5.3-dev, built on 2014-10-06 8:02:42 AM
 =======
  * @version     v0.5.2-dev, built on 2014-09-18 9:55:01 PM
@@ -31,6 +32,9 @@
 =======
  * @version     v0.5.2-dev, built on 2014-09-18 9:57:27 PM
 >>>>>>> Fix bug in form names for test suites for rgb, rgba and hsl
+=======
+ * @version     v0.5.2-dev, built on 2014-09-18 9:57:34 PM
+>>>>>>> Add validator and test suite for hsla() color validation
  * @author      https://twitter.com/nghuuphuoc
  * @copyright   (c) 2013 - 2014 Nguyen Huu Phuoc
  * @license     MIT

+ 30 - 0
dist/js/bootstrapValidator.js

@@ -9,6 +9,7 @@
 <<<<<<< HEAD
 <<<<<<< HEAD
 <<<<<<< HEAD
+<<<<<<< HEAD
  * @version     v0.5.3-dev, built on 2014-10-06 8:02:42 AM
 =======
  * @version     v0.5.2-dev, built on 2014-09-18 9:55:01 PM
@@ -31,6 +32,9 @@
 =======
  * @version     v0.5.2-dev, built on 2014-09-18 9:57:27 PM
 >>>>>>> Fix bug in form names for test suites for rgb, rgba and hsl
+=======
+ * @version     v0.5.2-dev, built on 2014-09-18 9:57:34 PM
+>>>>>>> Add validator and test suite for hsla() color validation
  * @author      https://twitter.com/nghuuphuoc
  * @copyright   (c) 2013 - 2014 Nguyen Huu Phuoc
  * @license     MIT
@@ -3191,6 +3195,32 @@ if (typeof jQuery === 'undefined') {
 }(window.jQuery));
 
 ;(function($) {
+    $.fn.bootstrapValidator.i18n.hslaColor = $.extend($.fn.bootstrapValidator.i18n.hslaColor || {}, {
+        'default': 'Please enter a valid hsla color'
+    });
+
+    $.fn.bootstrapValidator.validators.hslaColor = {
+
+        /**
+         * Return true if the input value is a valid hsla() color
+         *
+         * @param {BootstrapValidator} validator The validator plugin instance
+         * @param {jQuery} $field Field element
+         * @param {Object} options Can consist of the following keys:
+         * - message: The invalid message
+         * @returns {Boolean}
+         */
+        validate: function(validator, $field, options) {
+            var value = $field.val();
+            if (value === '') {
+                return true;
+            }
+            return /^hsla\((\s*(-?\d+)\s*,)(\s*(\b(0?\d{1,2}|100)\b%)\s*,){2}(\s*(0?(\.\d+)?|1(\.0+)?)\s*)\)$/.test(value);
+        }
+    };
+}(window.jQuery));
+
+;(function($) {
     $.fn.bootstrapValidator.i18n.iban = $.extend($.fn.bootstrapValidator.i18n.iban || {}, {
         'default': 'Please enter a valid IBAN number',
         countryNotSupported: 'The country code %s is not supported',

文件差异内容过多而无法显示
+ 10 - 0
dist/js/bootstrapValidator.min.js


+ 26 - 0
src/js/validator/hslaColor.js

@@ -0,0 +1,26 @@
+(function($) {
+    $.fn.bootstrapValidator.i18n.hslaColor = $.extend($.fn.bootstrapValidator.i18n.hslaColor || {}, {
+        'default': 'Please enter a valid hsla color'
+    });
+
+    $.fn.bootstrapValidator.validators.hslaColor = {
+
+        /**
+         * Return true if the input value is a valid hsla() color
+         *
+         * @param {BootstrapValidator} validator The validator plugin instance
+         * @param {jQuery} $field Field element
+         * @param {Object} options Can consist of the following keys:
+         * - message: The invalid message
+         * @returns {Boolean}
+         */
+        validate: function(validator, $field, options) {
+            var value = $field.val();
+            if (value === '') {
+                return true;
+            }
+            return /^hsla\((\s*(-?\d+)\s*,)(\s*(\b(0?\d{1,2}|100)\b%)\s*,){2}(\s*(0?(\.\d+)?|1(\.0+)?)\s*)\)$/.test(value);
+        }
+    };
+}(window.jQuery));
+

+ 181 - 0
test/spec.js

@@ -3646,6 +3646,187 @@ describe('hslColor', function() {
     });
 });
 
+describe('hslaColor', function() {
+
+    beforeEach(function() {
+        var html = [
+            '<div class="container">',
+                '<form class="form-horizontal" id="hslaColorForm">',
+                    '<div class="form-group">',
+                        '<input type="text" name="hsla" data-bv-hslacolor />',
+                    '</div>',
+                '</form>',
+            '</div>'
+        ].join('\n');
+
+        $(html).appendTo('body');
+        $('#hslaColorForm').bootstrapValidator();
+
+        this.bv          = $('#hslaColorForm').data('bootstrapValidator');
+        this.$hslaColor = this.bv.getFieldElements('hsla');
+    });
+
+    afterEach(function() {
+        $('#hslaColorForm').bootstrapValidator('destroy').parent().remove();
+    });
+
+    it('accept hsla()', function() {
+        this.$hslaColor.val('hsla(120,50%,50%,1)');
+        this.bv.validate();
+        expect(this.bv.isValidField('hsla')).toBeTruthy();
+    });
+
+    it('accept spaces around values', function() {
+        this.$hslaColor.val('hsla( 120 , 50% , 50%, 1 )');
+        this.bv.validate();
+        expect(this.bv.isValidField('hsla')).toBeTruthy();
+    });
+
+    it('accept multiple spaces around values', function() {
+        this.$hslaColor.val('hsla(  120,  50%,       50% ,   1  )');
+        this.bv.validate();
+        expect(this.bv.isValidField('hsla')).toBeTruthy();
+    });
+
+    it('accept negative hue value', function() {
+        this.$hslaColor.val('hsla(-120,50%,50%,1)');
+        this.bv.validate();
+        expect(this.bv.isValidField('hsla')).toBeTruthy();
+    });
+
+    it('accept hue values larger than 360', function() {
+        this.$hslaColor.val('hsla(480,50%,50%,1)');
+        this.bv.validate();
+        expect(this.bv.isValidField('hsla')).toBeTruthy();
+    });
+
+    it('accept integer alpha channel value of 0', function() {
+        this.$hslaColor.val('hsla(120,50%,100%,0)');
+        this.bv.validate();
+        expect(this.bv.isValidField('hsla')).toBeTruthy();
+    });
+
+    it('accept integer alpha channel value of 1', function() {
+        this.$hslaColor.val('hsla(120,50%,100%,1)');
+        this.bv.validate();
+        expect(this.bv.isValidField('hsla')).toBeTruthy();
+    });
+
+    it('accept floating alpha channel with leading 0', function() {
+        this.$hslaColor.val('hsla(120,50%,100%,0.5)');
+        this.bv.validate();
+        expect(this.bv.isValidField('hsla')).toBeTruthy();
+    });
+
+    it('accept floating alpha channel without leading 0', function() {
+        this.$hslaColor.val('hsla(120,50%,100%,.5)');
+        this.bv.validate();
+        expect(this.bv.isValidField('hsla')).toBeTruthy();
+    });
+
+    it('accept floating alpha channel with more than 1 decimal place', function() {
+        this.$hslaColor.val('hsla(120,50%,100%,.524141)');
+        this.bv.validate();
+        expect(this.bv.isValidField('hsla')).toBeTruthy();
+    });
+
+    it('reject percentage value for alpha channel', function() {
+        this.$hslaColor.val('hsla(120,50%,100%,50%)');
+        this.bv.validate();
+        expect(this.bv.isValidField('hsla')).toEqual(false);
+    });
+
+    it('reject integers larger than 1 for alpha channel', function() {
+        this.$hslaColor.val('hsla(120,50%,100%,2)');
+        this.bv.validate();
+        expect(this.bv.isValidField('hsla')).toEqual(false);
+    });
+
+    it('reject negative integers for alpha channel', function() {
+        this.$hslaColor.val('hsla(120,50%,100%,-1)');
+        this.bv.validate();
+        expect(this.bv.isValidField('hsla')).toEqual(false);
+    });
+
+    it('reject floats larger than 1 for alpha channel', function() {
+        this.$hslaColor.val('hsla(120,50%,100%,1.000000000001)');
+        this.bv.validate();
+        expect(this.bv.isValidField('hsla')).toEqual(false);
+    });
+
+    it('reject negative floats for alpha channel', function() {
+        this.$hslaColor.val('hsla(120,50%,100%,-0.5)');
+        this.bv.validate();
+        expect(this.bv.isValidField('hsla')).toEqual(false);
+    });
+
+    it('reject more floats larger than 1 for alpha channel', function() {
+        this.$hslaColor.val('hsla(120,50%,100%,2.3)');
+        this.bv.validate();
+        expect(this.bv.isValidField('hsla')).toEqual(false);
+    });
+
+    it('reject negative saturation value', function() {
+        this.$hslaColor.val('hsla(10,-50%,50%,1)');
+        this.bv.validate();
+        expect(this.bv.isValidField('hsla')).toEqual(false);
+    });
+
+    it('reject negative lightness', function() {
+        this.$hslaColor.val('hsla(10,50%,-50%,1)');
+        this.bv.validate();
+        expect(this.bv.isValidField('hsla')).toEqual(false);
+    });
+
+    it('require hsla()', function() {
+        this.$hslaColor.val('120,50%,50%,1');
+        this.bv.validate();
+        expect(this.bv.isValidField('hsla')).toEqual(false);
+    });
+
+    it('reject percentages above 100%', function() {
+        this.$hslaColor.val('hsla(120,100%,101%,1)');
+        this.bv.validate();
+        expect(this.bv.isValidField('hsla')).toEqual(false);
+    });
+
+    it('reject space between hsla and (', function() {
+        this.$hslaColor.val('hsla (120,50%,50%,1)');
+        this.bv.validate();
+        expect(this.bv.isValidField('hsla')).toEqual(false);
+    });
+
+    it('reject leading space', function() {
+        this.$hslaColor.val(' hsla(120,50%,50%,1)');
+        this.bv.validate();
+        expect(this.bv.isValidField('hsla')).toEqual(false);
+    });
+
+    it('reject trailing space', function() {
+        this.$hslaColor.val('hsla(120,50%,50%,1) ');
+        this.bv.validate();
+        expect(this.bv.isValidField('hsla')).toEqual(false);
+    });
+
+    it('reject percentages in hue value', function() {
+        this.$hslaColor.val('hsla(50%, 50%, 100%,1)');
+        this.bv.validate();
+        expect(this.bv.isValidField('hsla')).toEqual(false);
+    });
+
+    it('reject integers in saturation value', function() {
+        this.$hslaColor.val('hsla(120, 50, 100%,1)');
+        this.bv.validate();
+        expect(this.bv.isValidField('hsla')).toEqual(false);
+    });
+
+    it('reject integers in lightness value', function() {
+        this.$hslaColor.val('hsla(120, 50%, 100,1)');
+        this.bv.validate();
+        expect(this.bv.isValidField('hsla')).toEqual(false);
+    });
+});
+
 describe('iban', function() {
     beforeEach(function() {
         $([

+ 180 - 0
test/spec/validator/hslaColor.js

@@ -0,0 +1,180 @@
+describe('hslaColor', function() {
+
+    beforeEach(function() {
+        var html = [
+            '<div class="container">',
+                '<form class="form-horizontal" id="hslaColorForm">',
+                    '<div class="form-group">',
+                        '<input type="text" name="hsla" data-bv-hslacolor />',
+                    '</div>',
+                '</form>',
+            '</div>'
+        ].join('\n');
+
+        $(html).appendTo('body');
+        $('#hslaColorForm').bootstrapValidator();
+
+        this.bv          = $('#hslaColorForm').data('bootstrapValidator');
+        this.$hslaColor = this.bv.getFieldElements('hsla');
+    });
+
+    afterEach(function() {
+        $('#hslaColorForm').bootstrapValidator('destroy').parent().remove();
+    });
+
+    it('accept hsla()', function() {
+        this.$hslaColor.val('hsla(120,50%,50%,1)');
+        this.bv.validate();
+        expect(this.bv.isValidField('hsla')).toBeTruthy();
+    });
+
+    it('accept spaces around values', function() {
+        this.$hslaColor.val('hsla( 120 , 50% , 50%, 1 )');
+        this.bv.validate();
+        expect(this.bv.isValidField('hsla')).toBeTruthy();
+    });
+
+    it('accept multiple spaces around values', function() {
+        this.$hslaColor.val('hsla(  120,  50%,       50% ,   1  )');
+        this.bv.validate();
+        expect(this.bv.isValidField('hsla')).toBeTruthy();
+    });
+
+    it('accept negative hue value', function() {
+        this.$hslaColor.val('hsla(-120,50%,50%,1)');
+        this.bv.validate();
+        expect(this.bv.isValidField('hsla')).toBeTruthy();
+    });
+
+    it('accept hue values larger than 360', function() {
+        this.$hslaColor.val('hsla(480,50%,50%,1)');
+        this.bv.validate();
+        expect(this.bv.isValidField('hsla')).toBeTruthy();
+    });
+
+    it('accept integer alpha channel value of 0', function() {
+        this.$hslaColor.val('hsla(120,50%,100%,0)');
+        this.bv.validate();
+        expect(this.bv.isValidField('hsla')).toBeTruthy();
+    });
+
+    it('accept integer alpha channel value of 1', function() {
+        this.$hslaColor.val('hsla(120,50%,100%,1)');
+        this.bv.validate();
+        expect(this.bv.isValidField('hsla')).toBeTruthy();
+    });
+
+    it('accept floating alpha channel with leading 0', function() {
+        this.$hslaColor.val('hsla(120,50%,100%,0.5)');
+        this.bv.validate();
+        expect(this.bv.isValidField('hsla')).toBeTruthy();
+    });
+
+    it('accept floating alpha channel without leading 0', function() {
+        this.$hslaColor.val('hsla(120,50%,100%,.5)');
+        this.bv.validate();
+        expect(this.bv.isValidField('hsla')).toBeTruthy();
+    });
+
+    it('accept floating alpha channel with more than 1 decimal place', function() {
+        this.$hslaColor.val('hsla(120,50%,100%,.524141)');
+        this.bv.validate();
+        expect(this.bv.isValidField('hsla')).toBeTruthy();
+    });
+
+    it('reject percentage value for alpha channel', function() {
+        this.$hslaColor.val('hsla(120,50%,100%,50%)');
+        this.bv.validate();
+        expect(this.bv.isValidField('hsla')).toEqual(false);
+    });
+
+    it('reject integers larger than 1 for alpha channel', function() {
+        this.$hslaColor.val('hsla(120,50%,100%,2)');
+        this.bv.validate();
+        expect(this.bv.isValidField('hsla')).toEqual(false);
+    });
+
+    it('reject negative integers for alpha channel', function() {
+        this.$hslaColor.val('hsla(120,50%,100%,-1)');
+        this.bv.validate();
+        expect(this.bv.isValidField('hsla')).toEqual(false);
+    });
+
+    it('reject floats larger than 1 for alpha channel', function() {
+        this.$hslaColor.val('hsla(120,50%,100%,1.000000000001)');
+        this.bv.validate();
+        expect(this.bv.isValidField('hsla')).toEqual(false);
+    });
+
+    it('reject negative floats for alpha channel', function() {
+        this.$hslaColor.val('hsla(120,50%,100%,-0.5)');
+        this.bv.validate();
+        expect(this.bv.isValidField('hsla')).toEqual(false);
+    });
+
+    it('reject more floats larger than 1 for alpha channel', function() {
+        this.$hslaColor.val('hsla(120,50%,100%,2.3)');
+        this.bv.validate();
+        expect(this.bv.isValidField('hsla')).toEqual(false);
+    });
+
+    it('reject negative saturation value', function() {
+        this.$hslaColor.val('hsla(10,-50%,50%,1)');
+        this.bv.validate();
+        expect(this.bv.isValidField('hsla')).toEqual(false);
+    });
+
+    it('reject negative lightness', function() {
+        this.$hslaColor.val('hsla(10,50%,-50%,1)');
+        this.bv.validate();
+        expect(this.bv.isValidField('hsla')).toEqual(false);
+    });
+
+    it('require hsla()', function() {
+        this.$hslaColor.val('120,50%,50%,1');
+        this.bv.validate();
+        expect(this.bv.isValidField('hsla')).toEqual(false);
+    });
+
+    it('reject percentages above 100%', function() {
+        this.$hslaColor.val('hsla(120,100%,101%,1)');
+        this.bv.validate();
+        expect(this.bv.isValidField('hsla')).toEqual(false);
+    });
+
+    it('reject space between hsla and (', function() {
+        this.$hslaColor.val('hsla (120,50%,50%,1)');
+        this.bv.validate();
+        expect(this.bv.isValidField('hsla')).toEqual(false);
+    });
+
+    it('reject leading space', function() {
+        this.$hslaColor.val(' hsla(120,50%,50%,1)');
+        this.bv.validate();
+        expect(this.bv.isValidField('hsla')).toEqual(false);
+    });
+
+    it('reject trailing space', function() {
+        this.$hslaColor.val('hsla(120,50%,50%,1) ');
+        this.bv.validate();
+        expect(this.bv.isValidField('hsla')).toEqual(false);
+    });
+
+    it('reject percentages in hue value', function() {
+        this.$hslaColor.val('hsla(50%, 50%, 100%,1)');
+        this.bv.validate();
+        expect(this.bv.isValidField('hsla')).toEqual(false);
+    });
+
+    it('reject integers in saturation value', function() {
+        this.$hslaColor.val('hsla(120, 50, 100%,1)');
+        this.bv.validate();
+        expect(this.bv.isValidField('hsla')).toEqual(false);
+    });
+
+    it('reject integers in lightness value', function() {
+        this.$hslaColor.val('hsla(120, 50%, 100,1)');
+        this.bv.validate();
+        expect(this.bv.isValidField('hsla')).toEqual(false);
+    });
+});