Throw on non-numeric opacity values

This commit is contained in:
Tim Schaub
2019-08-15 09:44:30 -06:00
parent e8e7c46463
commit 2c69ad2bb4
3 changed files with 30 additions and 1 deletions

View File

@@ -236,3 +236,7 @@ A `WebGLArrayBuffer` must either be of type `ELEMENT_ARRAY_BUFFER` or `ARRAY_BUF
### 63
Support for the `OES_element_index_uint` WebGL extension is mandatory for WebGL layers.
### 64
Layer opacity must be a number.

View File

@@ -6,6 +6,7 @@ import BaseObject from '../Object.js';
import LayerProperty from './Property.js';
import {clamp} from '../math.js';
import {assign} from '../obj.js';
import {assert} from '../asserts.js';
/**
@@ -52,8 +53,11 @@ class BaseLayer extends BaseObject {
* @type {Object<string, *>}
*/
const properties = assign({}, options);
properties[LayerProperty.OPACITY] =
options.opacity !== undefined ? options.opacity : 1;
options.opacity !== undefined ? options.opacity : 1;
assert(typeof properties[LayerProperty.OPACITY] === 'number', 64); // Layer opacity must be a number
properties[LayerProperty.VISIBLE] =
options.visible !== undefined ? options.visible : true;
properties[LayerProperty.Z_INDEX] = options.zIndex;
@@ -292,6 +296,7 @@ class BaseLayer extends BaseObject {
* @api
*/
setOpacity(opacity) {
assert(typeof opacity === 'number', 64); // Layer opacity must be a number
this.set(LayerProperty.OPACITY, opacity);
}

View File

@@ -112,6 +112,19 @@ describe('ol.layer.Layer', function() {
layer.dispose();
});
it('throws on non-numeric opacity', function() {
function create() {
new Layer({
source: new Source({
projection: 'EPSG:4326'
}),
opacity: 'foo'
});
}
expect(create).to.throwException();
});
it('accepts a custom render function', function() {
let called = false;
const layer = new Layer({
@@ -511,6 +524,13 @@ describe('ol.layer.Layer', function() {
expect(layer.getOpacity()).to.be(0.3);
});
it('throws on types other than number', function() {
function set() {
layer.setOpacity('foo');
}
expect(set).to.throwException();
});
it('triggers a change event', function() {
const listener = sinon.spy();
layer.on('propertychange', listener);