8000 Fix setting tickwidth, tickcolor, ticklen, linecolor and possibly more attributes via template by archmoj · Pull Request #4904 · plotly/plotly.js · GitHub
[go: up one dir, main page]

Skip to content

Fix setting tickwidth, tickcolor, ticklen, linecolor and possibly more attributes via template #4904

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Jun 17, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
keep track of source and input in _coerce function
  • Loading branch information
archmoj committed Jun 9, 2020
commit 1d9cc4a91d33b69490f9ee7d51e008c6571e9392
35 changes: 18 additions & 17 deletions src/lib/coerce.js
Original file line number Diff line number Diff line change
Expand Up @@ -361,24 +361,27 @@ exports.valObjectMeta = {
* as a convenience, returns the value it finally set
*/
exports.coerce = function(containerIn, containerOut, attributes, attribute, dflt) {
return _coerce(containerIn, containerOut, attributes, attribute, dflt).value;
return _coerce(containerIn, containerOut, attributes, attribute, dflt).val;
};

function _coerce(containerIn, containerOut, attributes, attribute, dflt) {
var opts = nestedProperty(attributes, attribute).get();
if(dflt === undefined) dflt = opts.dflt;
var src = ''; // i.e. default

var propIn = nestedProperty(containerIn, attribute);
var propOut = nestedProperty(containerOut, attribute);
var valIn = propIn.get();

var template = containerOut._template;
if(valIn === undefined && template) {
valIn = nestedProperty(template, attribute).get();
if(valIn !== undefined) src = 't'; // template

// already used the template value, so short-circuit the second check
template = 0;
}

if(dflt === undefined) dflt = opts.dflt;

/**
* arrayOk: value MAY be an array, then we do no value checking
* at this point, because it can be more complicated than the
Expand All @@ -388,25 +391,32 @@ function _coerce(containerIn, containerOut, attributes, attribute, dflt) {
if(opts.arrayOk && isArrayOrTypedArray(valIn)) {
propOut.set(valIn);
return {
value: valIn,
default: dflt
inp: valIn,
val: valIn,
src: 'c' // container
};
}

var coerceFunction = exports.valObjectMeta[opts.valType].coerceFunction;
coerceFunction(valIn, propOut, dflt, opts);

var valOut = propOut.get();
if(valOut !== undefined) src = 'c'; // container

// in case v was provided but invalid, try the template again so it still
// overrides the regular default
if(template && valOut === dflt && !validate(valIn, opts)) {
valIn = nestedProperty(template, attribute).get();
coerceFunction(valIn, propOut, dflt, opts);
valOut = propOut.get();

if(valOut !== undefined) src = 't'; // template
}

return {
value: valOut,
default: dflt
inp: valIn,
val: valOut,
src: src
};
}

Expand All @@ -419,16 +429,7 @@ function _coerce(containerIn, containerOut, attributes, attribute, dflt) {
*/
exports.coerce2 = function(containerIn, containerOut, attributes, attribute, dflt) {
var out = _coerce(containerIn, containerOut, attributes, attribute, dflt);
var valOut = out.value;
if(
valOut !== undefined &&
valOut !== out.default
) {
return valOut;
}

var valIn = nestedProperty(containerIn, attribute).get();
return (valIn !== undefined && valIn !== null) ? valOut : false;
return (out.src && out.inp !== undefined) ? out.val : false;
Copy link
Coll 8000 aborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I'm not sure about this, but at this point I think the best way to handle it will be extensive jasmine testing.

I see we actually do have a test that contradicts one thing I was hoping to "fix":

it('should set and return the default if the user input is not valid', function() {

Do we have a good reason for that behavior? It seems contrary to what we do with templates, as well as contrary to what I think the purpose of coerce2 should be, which would be stated something like:

should set the default and return false if the user input is not valid

@archmoj do you see any issue if we make such a change? I feel like that test is locking down a bug rather than useful behavior.

(while we're at it there's a piece of copypasta in that test

expect(colOut).toBe('rgba(0, 0, 0, 0)');
expect(sizeOut).toBe(outObj.testMarker.testSize);
expect(sizeOut).toBe(20);
expect(sizeOut).toBe(outObj.testMarker.testSize);
- should be 2 colOut lines and 2 sizeOut lines)

So what I'd like to see is a test covering each of the valTypes used by coerce2, and for each one:

  • valid input in the container
  • valid input in the container but changing type (ie '2' to 2) if such a thing exists for this valType
  • invalid input in the container OR no input in the container, with:
    • no input in the template
    • invalid input in the template
    • valid input in the template
    • valid input in the template but changing type (ie '2' to 2) if such a thing exists for this valType

I suspect getting this to work for all valTypes may not be worthwhile - especially since there are things like colorscale where even the default may be "fixed" by the coercion process, and subplotid where the default affects which input values are allowed. But we should be able to find a way to make it work for the ones we actually use for coerce2

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alexcjohnson thanks for the review. Please see cf7c061 and 21b86cd.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alexcjohnson wondering what should be the result of this test?
https://github.com/plotly/plotly.js/blob/master/test/jasmine/tests/axes_test.js#L1791-L1797
Shouldn't it be

expect(yaxis.ticklen).toBe(undefined);
expect(yaxis.tickwidth).toBe(undefined);
expect(yaxis.tickcolor).toBe(undefined);
expect(yaxis.ticks).toBe('');

?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alexcjohnson this PR is ready for another review.
Please see adc36cd.

};

/*
Expand Down
0