Merge pull request #2497 from BeksOmega/fixes/ValidColourFieldValues

Changed Colour Field Class Validator to Accept all Documented Values
This commit is contained in:
Rachel Fenichel
2019-05-28 16:07:52 -07:00
committed by GitHub
2 changed files with 68 additions and 6 deletions

View File

@@ -32,6 +32,7 @@ goog.require('Blockly.Events.BlockChange');
goog.require('Blockly.Field');
goog.require('goog.math.Size');
goog.require('goog.color');
/**
@@ -176,8 +177,11 @@ Blockly.FieldColour.prototype.render_ = function() {
* @protected
*/
Blockly.FieldColour.prototype.doClassValidation_ = function(newValue) {
if (Blockly.FieldColour.COLOUR_REGEX.test(newValue)) {
return newValue.toLowerCase();
if (typeof newValue != 'string') {
return null;
}
if (goog.color.isValidColor(newValue)) {
return goog.color.parse(newValue).hex;
}
return null;
};

View File

@@ -58,7 +58,7 @@ suite ('Colour Fields', function() {
assertValueDefault(colourField);
});
test('Non-Parsable String', function() {
var colourField = new Blockly.FieldColour('bad');
var colourField = new Blockly.FieldColour('not_a_colour');
assertValueDefault(colourField);
});
test('#AAAAAA', function() {
@@ -85,6 +85,22 @@ suite ('Colour Fields', function() {
var colourField = new Blockly.FieldColour('#bcbcbc');
assertValue(colourField, '#bcbcbc', '#bcbcbc');
});
test('#AA0', function() {
var colourField = new Blockly.FieldColour('#AA0');
assertValue(colourField, '#aaaa00', '#aa0');
});
test('#aa0', function() {
var colourField = new Blockly.FieldColour('#aa0');
assertValue(colourField, '#aaaa00', '#aa0');
});
test('rgb(170, 170, 0)', function() {
var colourField = new Blockly.FieldColour('rgb(170, 170, 0)');
assertValue(colourField, '#aaaa00', '#aa0');
});
test('red', function() {
var colourField = new Blockly.FieldColour('red');
assertValue(colourField, '#ff0000', '#f00');
});
});
suite('fromJson', function() {
test('Empty', function() {
@@ -100,7 +116,8 @@ suite ('Colour Fields', function() {
assertValueDefault(colourField);
});
test('Non-Parsable String', function() {
var colourField = new Blockly.FieldColour.fromJson({ colour:'bad' });
var colourField = new Blockly.FieldColour.fromJson(
{ colour:'not_a_colour' });
assertValueDefault(colourField);
});
test('#AAAAAA', function() {
@@ -127,6 +144,23 @@ suite ('Colour Fields', function() {
var colourField = Blockly.FieldColour.fromJson({ colour: '#bcbcbc' });
assertValue(colourField, '#bcbcbc', '#bcbcbc');
});
test('#AA0', function() {
var colourField = Blockly.FieldColour.fromJson({ colour: '#AA0' });
assertValue(colourField, '#aaaa00', '#aa0');
});
test('#aa0', function() {
var colourField = Blockly.FieldColour.fromJson({ colour: '#aa0' });
assertValue(colourField, '#aaaa00', '#aa0');
});
test('rgb(170, 170, 0)', function() {
var colourField = Blockly.FieldColour.fromJson(
{ colour: 'rgb(170, 170, 0)' });
assertValue(colourField, '#aaaa00', '#aa0');
});
test('red', function() {
var colourField = Blockly.FieldColour.fromJson({ colour: 'red' });
assertValue(colourField, '#ff0000', '#f00');
});
});
suite('setValue', function() {
suite('Empty -> New Value', function() {
@@ -142,7 +176,7 @@ suite ('Colour Fields', function() {
assertValueDefault(this.colourField);
});
test('Non-Parsable String', function() {
this.colourField.setValue('bad');
this.colourField.setValue('not_a_colour');
assertValueDefault(this.colourField);
});
test('#000000', function() {
@@ -153,6 +187,18 @@ suite ('Colour Fields', function() {
this.colourField.setValue('#bcbcbc');
assertValue(this.colourField, '#bcbcbc', '#bcbcbc');
});
test('#aa0', function() {
this.colourField.setValue('#aa0');
assertValue(this.colourField, '#aaaa00', '#aa0');
});
test('rgb(170, 170, 0)', function() {
this.colourField.setValue('rgb(170, 170, 0)');
assertValue(this.colourField, '#aaaa00', '#aa0');
});
test('red', function() {
this.colourField.setValue('red');
assertValue(this.colourField, '#ff0000', '#f00');
});
});
suite('Value -> New Value', function() {
setup(function() {
@@ -167,7 +213,7 @@ suite ('Colour Fields', function() {
assertValue(this.colourField, '#aaaaaa', '#aaa');
});
test('Non-Parsable String', function() {
this.colourField.setValue('bad');
this.colourField.setValue('not_a_colour');
assertValue(this.colourField, '#aaaaaa', '#aaa');
});
test('#000000', function() {
@@ -178,6 +224,18 @@ suite ('Colour Fields', function() {
this.colourField.setValue('#bcbcbc');
assertValue(this.colourField, '#bcbcbc', '#bcbcbc');
});
test('#aa0', function() {
this.colourField.setValue('#aa0');
assertValue(this.colourField, '#aaaa00', '#aa0');
});
test('rgb(170, 170, 0)', function() {
this.colourField.setValue('rgb(170, 170, 0)');
assertValue(this.colourField, '#aaaa00', '#aa0');
});
test('red', function() {
this.colourField.setValue('red');
assertValue(this.colourField, '#ff0000', '#f00');
});
});
});
suite('Validators', function() {