Skip to content

Commit

Permalink
Throw on non-numeric opacity values
Browse files Browse the repository at this point in the history
  • Loading branch information
tschaub committed Aug 15, 2019
1 parent e8e7c46 commit 2c69ad2
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
4 changes: 4 additions & 0 deletions doc/errors/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
7 changes: 6 additions & 1 deletion src/ol/layer/Base.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';


/**
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

Expand Down
20 changes: 20 additions & 0 deletions test/spec/ol/layer/layer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 2c69ad2

Please sign in to comment.