Skip to content

Commit adc36cd

Browse files
committedJun 16, 2020
validate input when calling from coerce2
- fixup lib and axes tests - add info about Lib.coerce2 function
1 parent 2b911d1 commit adc36cd

File tree

3 files changed

+24
-18
lines changed

3 files changed

+24
-18
lines changed
 

‎src/lib/coerce.js

+11-5
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,9 @@ exports.coerce = function(containerIn, containerOut, attributes, attribute, dflt
364364
return _coerce(containerIn, containerOut, attributes, attribute, dflt).val;
365365
};
366366

367-
function _coerce(containerIn, containerOut, attributes, attribute, dflt) {
367+
function _coerce(containerIn, containerOut, attributes, attribute, dflt, opts) {
368+
var shouldValidate = (opts || {}).shouldValidate;
369+
368370
var attr = nestedProperty(attributes, attribute).get();
369371
if(dflt === undefined) dflt = attr.dflt;
370372
var src = ''; // i.e. default
@@ -376,7 +378,7 @@ function _coerce(containerIn, containerOut, attributes, attribute, dflt) {
376378
var template = containerOut._template;
377379
if(valIn === undefined && template) {
378380
valIn = nestedProperty(template, attribute).get();
379-
if(valIn !== undefined) src = 't'; // template
381+
if(valIn !== undefined && shouldValidate && validate(valIn, attr)) src = 't'; // template
380382

381383
// already used the template value, so short-circuit the second check
382384
template = 0;
@@ -401,7 +403,7 @@ function _coerce(containerIn, containerOut, attributes, attribute, dflt) {
401403
coerceFunction(valIn, propOut, dflt, attr);
402404

403405
var valOut = propOut.get();
404-
if(valOut !== undefined) src = 'c'; // container
406+
if(valOut !== undefined && shouldValidate && validate(valIn, attr)) src = 'c'; // container
405407

406408
// in case v was provided but invalid, try the template again so it still
407409
// overrides the regular default
@@ -410,7 +412,7 @@ function _coerce(containerIn, containerOut, attributes, attribute, dflt) {
410412
coerceFunction(valIn, propOut, dflt, attr);
411413
valOut = propOut.get();
412414

413-
if(valOut !== undefined) src = 't'; // template
415+
if(valOut !== undefined && shouldValidate && validate(valIn, attr)) src = 't'; // template
414416
}
415417

416418
return {
@@ -422,13 +424,17 @@ function _coerce(containerIn, containerOut, attributes, attribute, dflt) {
422424

423425
/**
424426
* Variation on coerce
427+
* useful when setting an attribute to a valid value
428+
* can change the default for another attribute.
425429
*
426430
* Uses coerce to get attribute value if user input is valid,
427431
* returns attribute default if user input it not valid or
428432
* returns false if there is no user input.
429433
*/
430434
exports.coerce2 = function(containerIn, containerOut, attributes, attribute, dflt) {
431-
var out = _coerce(containerIn, containerOut, attributes, attribute, dflt);
435+
var out = _coerce(containerIn, containerOut, attributes, attribute, dflt, {
436+
shouldValidate: true
437+
});
432438
return (out.src && out.inp !== undefined) ? out.val : false;
433439
};
434440

‎test/jasmine/tests/axes_test.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1803,10 +1803,10 @@ describe('Test axes', function() {
18031803
Plotly.plot(gd, data, layout);
18041804

18051805
var yaxis = gd._fullLayout.yaxis;
1806-
expect(yaxis.ticklen).toBe(5);
1807-
expect(yaxis.tickwidth).toBe(1);
1808-
expect(yaxis.tickcolor).toBe('#444');
1809-
expect(yaxis.ticks).toBe('outside');
1806+
expect(yaxis.ticklen).toBe(undefined);
1807+
expect(yaxis.tickwidth).toBe(undefined);
1808+
expect(yaxis.tickcolor).toBe(undefined);
1809+
expect(yaxis.ticks).toBe('');
18101810
expect(yaxis.showticklabels).toBe(true);
18111811
expect(yaxis.tickfont).toEqual({ family: '"Open Sans", verdana, arial, sans-serif', size: 12, color: '#444' });
18121812
expect(yaxis.tickangle).toBe('auto');

‎test/jasmine/tests/lib_test.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -792,10 +792,10 @@ describe('Test lib.js:', function() {
792792
var colOut = coerce2(obj, outObj, attrs, 'testMarker.testColor');
793793
var sizeOut = coerce2(obj, outObj, attrs, 'testMarker.testSize');
794794

795-
expect(colOut).toBe('rgba(0, 0, 0, 0)');
796-
expect(colOut).toBe(outObj.testMarker.testColor);
797-
expect(sizeOut).toBe(20);
798-
expect(sizeOut).toBe(outObj.testMarker.testSize);
795+
expect(colOut).toBe(false);
796+
expect(outObj.testMarker.testColor).toBe('rgba(0, 0, 0, 0)');
797+
expect(sizeOut).toBe(false);
798+
expect(outObj.testMarker.testSize).toBe(20);
799799
});
800800

801801
it('should set the user input', function() {
@@ -842,7 +842,7 @@ describe('Test lib.js:', function() {
842842
expect(sizeOut).toBe(outObj.testMarker.testSize);
843843
});
844844

845-
it('should set to default if the both container and template inputs are not valid', function() {
845+
it('should set to default and return false if the both container and template inputs are not valid', function() {
846846
var attrs = {
847847
testMarker: {
848848
testColor: {valType: 'color', dflt: 'rgba(0, 0, 0, 0)'},
@@ -860,10 +860,10 @@ describe('Test lib.js:', function() {
860860
var colOut = coerce2(obj, outObj, attrs, 'testMarker.testColor');
861861
var sizeOut = coerce2(obj, outObj, attrs, 'testMarker.testSize');
862862

863-
expect(colOut).toBe('rgba(0, 0, 0, 0)');
864-
expect(colOut).toBe(outObj.testMarker.testColor);
865-
expect(sizeOut).toBe(20);
866-
expect(sizeOut).toBe(outObj.testMarker.testSize);
863+
expect(colOut).toBe(false);
864+
expect(outObj.testMarker.testColor).toBe('rgba(0, 0, 0, 0)');
865+
expect(sizeOut).toBe(false);
866+
expect(outObj.testMarker.testSize).toBe(20);
867867
});
868868

869869
it('should return false if there is no user input', function() {

0 commit comments

Comments
 (0)
Please sign in to comment.