Skip to content

Commit 4468b1b

Browse files
authored
fix: setViewport can cause a crash if voi or scale are not provided (#493)
* be safer when merging options in setViewport * added a test * Update comment in test * remove yarn.lock so travis can run
1 parent 0629c93 commit 4468b1b

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

src/setViewport.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@ export default function (element, viewport) {
3535
// Prevent window width from being too small (note that values close to zero are valid and can occur with
3636
// PET images in particular)
3737
if (enabledElement.viewport.voi.windowWidth) {
38-
enabledElement.viewport.voi.windowWidth = Math.max(viewport.voi.windowWidth, MIN_WINDOW_WIDTH);
38+
enabledElement.viewport.voi.windowWidth = Math.max(enabledElement.viewport.voi.windowWidth, MIN_WINDOW_WIDTH);
3939
}
4040

4141
// Prevent scale from getting too small
4242
if (enabledElement.viewport.scale) {
43-
enabledElement.viewport.scale = Math.max(viewport.scale, MIN_VIEWPORT_SCALE);
43+
enabledElement.viewport.scale = Math.max(enabledElement.viewport.scale, MIN_VIEWPORT_SCALE);
4444
}
4545

4646
// Normalize the rotation value to a positive rotation in degrees

test/setViewport_test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,28 @@ describe('Set an enabled element\'s viewport', function () {
9393
});
9494
});
9595

96+
it('all properties should be optional', function () {
97+
98+
// Act
99+
displayImage(this.element, this.image, this.viewport);
100+
101+
const element = this.element;
102+
const initialViewport = getViewport(element);
103+
104+
const newViewport = {};
105+
106+
// Act
107+
setViewport(element, newViewport);
108+
109+
// Assert
110+
const viewport = getViewport(element);
111+
112+
// We expect all the properties to be the same but we're really testing that the method did
113+
// not throw by unintentionally relying on a property existing on the input.
114+
assert.deepEqual(initialViewport, viewport);
115+
116+
});
117+
96118
it('should prevent windowWidth from getting too small', function () {
97119

98120
// Act

0 commit comments

Comments
 (0)