Skip to content

Commit

Permalink
fix: improve input validation in View.moveTo (#58)
Browse files Browse the repository at this point in the history
* fix(Label): convert parsed font size to number

Everything seems to expect it but it returns string.
Numbers got concatenated, sometimes it ended up as NaN…

* fix(View): better validate inputs

Throws if offset or position are not finite numbers.

* chore(dist): update
  • Loading branch information
Thomaash committed Aug 9, 2019
1 parent 43c9459 commit 9114da5
Show file tree
Hide file tree
Showing 10 changed files with 180 additions and 61 deletions.
75 changes: 55 additions & 20 deletions dist/vis-network.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
*
* A dynamic, browser-based visualization library.
*
* @version 5.1.1
* @date 2019-08-02T11:55:44Z
* @version 0.0.0-no-version
* @date 2019-08-08T15:18:29Z
*
* @copyright (c) 2011-2017 Almende B.V, http://almende.com
* @copyright (c) 2018-2019 visjs contributors, https://github.com/visjs
Expand Down Expand Up @@ -21538,7 +21538,6 @@ function () {
* If in-variable is a string, parse it as a font specifier.
*
* Note that following is not done here and have to be done after the call:
* - No number conversion (size)
* - Not all font options are set (vadjust, mod)
*
* @param {Object} outOptions out-parameter, object in which to store the parse results (if any)
Expand Down Expand Up @@ -22275,7 +22274,7 @@ function () {
value: function parseFontString(outOptions, inOptions) {
if (!inOptions || typeof inOptions !== 'string') return false;
var newOptionsArray = inOptions.split(" ");
outOptions.size = newOptionsArray[0].replace("px", '');
outOptions.size = +newOptionsArray[0].replace("px", '');
outOptions.face = newOptionsArray[1];
outOptions.color = newOptionsArray[2];
return true;
Expand Down Expand Up @@ -34698,38 +34697,74 @@ function () {
if (options === undefined) {
options = {};
return;
} // Coerce and verify that the scale is valid.
}

if (options.offset != null) {
if (options.offset.x != null) {
// Coerce and verify that x is valid.
options.offset.x = +options.offset.x;

options.scale = +options.scale;
if (!Number.isFinite(options.offset.x)) {
throw new TypeError('The option "offset.x" has to be a finite number.');
}
} else {
options.offset.x = 0;
}

if (!(options.scale > 0)) {
throw new TypeError('The option "scale" has to be a number greater than zero.');
}
if (options.offset.y != null) {
// Coerce and verify that y is valid.
options.offset.y = +options.offset.y;

if (options.offset === undefined) {
if (!Number.isFinite(options.offset.y)) {
throw new TypeError('The option "offset.y" has to be a finite number.');
}
} else {
options.offset.x = 0;
}
} else {
options.offset = {
x: 0,
y: 0
};
}

if (options.offset.x === undefined) {
options.offset.x = 0;
}
if (options.position != null) {
if (options.position.x != null) {
// Coerce and verify that x is valid.
options.position.x = +options.position.x;

if (options.offset.y === undefined) {
options.offset.y = 0;
}
if (!Number.isFinite(options.position.x)) {
throw new TypeError('The option "position.x" has to be a finite number.');
}
} else {
options.position.x = 0;
}

if (options.scale === undefined) {
options.scale = this.body.view.scale;
}
if (options.position.y != null) {
// Coerce and verify that y is valid.
options.position.y = +options.position.y;

if (options.position === undefined) {
if (!Number.isFinite(options.position.y)) {
throw new TypeError('The option "position.y" has to be a finite number.');
}
} else {
options.position.x = 0;
}
} else {
options.position = this.getViewPosition();
}

if (options.scale != null) {
// Coerce and verify that the scale is valid.
options.scale = +options.scale;

if (!(options.scale > 0)) {
throw new TypeError('The option "scale" has to be a number greater than zero.');
} else {
options.scale = this.body.view.scale;
}
}

if (options.animation === undefined) {
options.animation = {
duration: 0
Expand Down
2 changes: 1 addition & 1 deletion dist/vis-network.esm.js.map

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions dist/vis-network.esm.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/vis-network.esm.min.js.map

Large diffs are not rendered by default.

75 changes: 55 additions & 20 deletions dist/vis-network.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
*
* A dynamic, browser-based visualization library.
*
* @version 5.1.1
* @date 2019-08-02T11:55:44Z
* @version 0.0.0-no-version
* @date 2019-08-08T15:18:29Z
*
* @copyright (c) 2011-2017 Almende B.V, http://almende.com
* @copyright (c) 2018-2019 visjs contributors, https://github.com/visjs
Expand Down Expand Up @@ -21544,7 +21544,6 @@
* If in-variable is a string, parse it as a font specifier.
*
* Note that following is not done here and have to be done after the call:
* - No number conversion (size)
* - Not all font options are set (vadjust, mod)
*
* @param {Object} outOptions out-parameter, object in which to store the parse results (if any)
Expand Down Expand Up @@ -22281,7 +22280,7 @@
value: function parseFontString(outOptions, inOptions) {
if (!inOptions || typeof inOptions !== 'string') return false;
var newOptionsArray = inOptions.split(" ");
outOptions.size = newOptionsArray[0].replace("px", '');
outOptions.size = +newOptionsArray[0].replace("px", '');
outOptions.face = newOptionsArray[1];
outOptions.color = newOptionsArray[2];
return true;
Expand Down Expand Up @@ -34704,38 +34703,74 @@
if (options === undefined) {
options = {};
return;
} // Coerce and verify that the scale is valid.
}

if (options.offset != null) {
if (options.offset.x != null) {
// Coerce and verify that x is valid.
options.offset.x = +options.offset.x;

options.scale = +options.scale;
if (!Number.isFinite(options.offset.x)) {
throw new TypeError('The option "offset.x" has to be a finite number.');
}
} else {
options.offset.x = 0;
}

if (!(options.scale > 0)) {
throw new TypeError('The option "scale" has to be a number greater than zero.');
}
if (options.offset.y != null) {
// Coerce and verify that y is valid.
options.offset.y = +options.offset.y;

if (options.offset === undefined) {
if (!Number.isFinite(options.offset.y)) {
throw new TypeError('The option "offset.y" has to be a finite number.');
}
} else {
options.offset.x = 0;
}
} else {
options.offset = {
x: 0,
y: 0
};
}

if (options.offset.x === undefined) {
options.offset.x = 0;
}
if (options.position != null) {
if (options.position.x != null) {
// Coerce and verify that x is valid.
options.position.x = +options.position.x;

if (options.offset.y === undefined) {
options.offset.y = 0;
}
if (!Number.isFinite(options.position.x)) {
throw new TypeError('The option "position.x" has to be a finite number.');
}
} else {
options.position.x = 0;
}

if (options.scale === undefined) {
options.scale = this.body.view.scale;
}
if (options.position.y != null) {
// Coerce and verify that y is valid.
options.position.y = +options.position.y;

if (options.position === undefined) {
if (!Number.isFinite(options.position.y)) {
throw new TypeError('The option "position.y" has to be a finite number.');
}
} else {
options.position.x = 0;
}
} else {
options.position = this.getViewPosition();
}

if (options.scale != null) {
// Coerce and verify that the scale is valid.
options.scale = +options.scale;

if (!(options.scale > 0)) {
throw new TypeError('The option "scale" has to be a number greater than zero.');
} else {
options.scale = this.body.view.scale;
}
}

if (options.animation === undefined) {
options.animation = {
duration: 0
Expand Down
2 changes: 1 addition & 1 deletion dist/vis-network.js.map

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions dist/vis-network.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/vis-network.min.js.map

Large diffs are not rendered by default.

68 changes: 59 additions & 9 deletions lib/network/modules/View.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,17 +139,67 @@ class View {
return;
}

// Coerce and verify that the scale is valid.
options.scale = +options.scale
if (!(options.scale > 0)) {
throw new TypeError('The option "scale" has to be a number greater than zero.')
if (options.offset != null) {
if (options.offset.x != null) {
// Coerce and verify that x is valid.
options.offset.x = +options.offset.x
if (!Number.isFinite(options.offset.x)) {
throw new TypeError('The option "offset.x" has to be a finite number.')
}
} else {
options.offset.x = 0
}

if (options.offset.y != null) {
// Coerce and verify that y is valid.
options.offset.y = +options.offset.y
if (!Number.isFinite(options.offset.y)) {
throw new TypeError('The option "offset.y" has to be a finite number.')
}
} else {
options.offset.x = 0
}
} else {
options.offset = {
x: 0,
y: 0
}
}

if (options.position != null) {
if (options.position.x != null) {
// Coerce and verify that x is valid.
options.position.x = +options.position.x
if (!Number.isFinite(options.position.x)) {
throw new TypeError('The option "position.x" has to be a finite number.')
}
} else {
options.position.x = 0
}

if (options.position.y != null) {
// Coerce and verify that y is valid.
options.position.y = +options.position.y
if (!Number.isFinite(options.position.y)) {
throw new TypeError('The option "position.y" has to be a finite number.')
}
} else {
options.position.x = 0
}
} else {
options.position = this.getViewPosition()
}

if (options.scale != null) {
// Coerce and verify that the scale is valid.
options.scale = +options.scale
if (!(options.scale > 0)) {
throw new TypeError('The option "scale" has to be a number greater than zero.')
} else {
options.scale = this.body.view.scale
}
}

if (options.offset === undefined) {options.offset = {x: 0, y: 0}; }
if (options.offset.x === undefined) {options.offset.x = 0; }
if (options.offset.y === undefined) {options.offset.y = 0; }
if (options.scale === undefined) {options.scale = this.body.view.scale; }
if (options.position === undefined) {options.position = this.getViewPosition();}
if (options.animation === undefined) {options.animation = {duration:0}; }
if (options.animation === false ) {options.animation = {duration:0}; }
if (options.animation === true ) {options.animation = {}; }
Expand Down
3 changes: 1 addition & 2 deletions lib/network/modules/components/shared/Label.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ class Label {
* If in-variable is a string, parse it as a font specifier.
*
* Note that following is not done here and have to be done after the call:
* - No number conversion (size)
* - Not all font options are set (vadjust, mod)
*
* @param {Object} outOptions out-parameter, object in which to store the parse results (if any)
Expand All @@ -109,7 +108,7 @@ class Label {

let newOptionsArray = inOptions.split(" ");

outOptions.size = newOptionsArray[0].replace("px",'');
outOptions.size = +newOptionsArray[0].replace("px",'');
outOptions.face = newOptionsArray[1];
outOptions.color = newOptionsArray[2];

Expand Down

0 comments on commit 9114da5

Please sign in to comment.