Skip to content

Commit 190b3b5

Browse files
authored
Merge pull request #1376 from plotly/relayout-annotations-mutation-guard
Extend input relayout & restyle objects before looping over key-vals
2 parents fd032af + 9a065b3 commit 190b3b5

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

src/plot_api/plot_api.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1183,7 +1183,7 @@ Plotly.restyle = function restyle(gd, astr, val, traces) {
11831183
if(typeof astr === 'string') aobj[astr] = val;
11841184
else if(Lib.isPlainObject(astr)) {
11851185
// the 3-arg form
1186-
aobj = astr;
1186+
aobj = Lib.extendFlat({}, astr);
11871187
if(traces === undefined) traces = val;
11881188
}
11891189
else {
@@ -1702,7 +1702,7 @@ Plotly.relayout = function relayout(gd, astr, val) {
17021702
if(typeof astr === 'string') {
17031703
aobj[astr] = val;
17041704
} else if(Lib.isPlainObject(astr)) {
1705-
aobj = astr;
1705+
aobj = Lib.extendFlat({}, astr);
17061706
} else {
17071707
Lib.warn('Relayout fail.', astr, val);
17081708
return Promise.reject();
@@ -2092,10 +2092,10 @@ Plotly.update = function update(gd, traceUpdate, layoutUpdate, traces) {
20922092
if(Object.keys(traceUpdate).length) gd.changed = true;
20932093
if(Object.keys(layoutUpdate).length) gd.changed = true;
20942094

2095-
var restyleSpecs = _restyle(gd, traceUpdate, traces),
2095+
var restyleSpecs = _restyle(gd, Lib.extendFlat({}, traceUpdate), traces),
20962096
restyleFlags = restyleSpecs.flags;
20972097

2098-
var relayoutSpecs = _relayout(gd, layoutUpdate),
2098+
var relayoutSpecs = _relayout(gd, Lib.extendFlat({}, layoutUpdate)),
20992099
relayoutFlags = relayoutSpecs.flags;
21002100

21012101
// clear calcdata if required

test/jasmine/tests/annotations_test.js

+24-1
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ describe('annotations relayout', function() {
185185
});
186186

187187
it('should be able update annotations', function(done) {
188+
var updateObj = { 'annotations[0].text': 'hello' };
188189

189190
function assertText(index, expected) {
190191
var query = '.annotation[data-index="' + index + '"]',
@@ -193,6 +194,12 @@ describe('annotations relayout', function() {
193194
expect(actual).toEqual(expected);
194195
}
195196

197+
function assertUpdateObj() {
198+
// w/o mutating relayout update object
199+
expect(Object.keys(updateObj)).toEqual(['annotations[0].text']);
200+
expect(updateObj['annotations[0].text']).toEqual('hello');
201+
}
202+
196203
assertText(0, 'left top');
197204

198205
Plotly.relayout(gd, 'annotations[0].text', 'hello').then(function() {
@@ -202,9 +209,25 @@ describe('annotations relayout', function() {
202209
})
203210
.then(function() {
204211
assertText(0, 'new text');
212+
213+
return Plotly.relayout(gd, updateObj);
205214
})
206-
.then(done);
215+
.then(function() {
216+
assertText(0, 'hello');
217+
assertUpdateObj();
218+
219+
return Plotly.relayout(gd, 'annotations[0].text', null);
220+
})
221+
.then(function() {
222+
assertText(0, 'new text');
207223

224+
return Plotly.update(gd, {}, updateObj);
225+
})
226+
.then(function() {
227+
assertText(0, 'hello');
228+
assertUpdateObj();
229+
})
230+
.then(done);
208231
});
209232
});
210233

0 commit comments

Comments
 (0)