diff --git a/src/ol/style/fillsymbolizer.js b/src/ol/style/fillsymbolizer.js index 603aa34a5a..b5e079117e 100644 --- a/src/ol/style/fillsymbolizer.js +++ b/src/ol/style/fillsymbolizer.js @@ -64,9 +64,8 @@ ol.style.Fill.prototype.createLiteral = function(featureOrType) { goog.asserts.assertString( color, 'color must be a string'); - var opacity = ol.expr.evaluateFeature(this.opacity_, feature); - goog.asserts.assertNumber( - opacity, 'color must be a number'); + var opacity = Number(ol.expr.evaluateFeature(this.opacity_, feature)); + goog.asserts.assert(!isNaN(opacity), 'opacity must be a number'); literal = new ol.style.PolygonLiteral({ fillColor: color, diff --git a/src/ol/style/iconsymbolizer.js b/src/ol/style/iconsymbolizer.js index c112f341de..781c6eb73d 100644 --- a/src/ol/style/iconsymbolizer.js +++ b/src/ol/style/iconsymbolizer.js @@ -90,21 +90,21 @@ ol.style.Icon.prototype.createLiteral = function(featureOrType) { var width; if (!goog.isNull(this.width_)) { - width = ol.expr.evaluateFeature(this.width_, feature); - goog.asserts.assertNumber(width, 'width must be a number'); + width = Number(ol.expr.evaluateFeature(this.width_, feature)); + goog.asserts.assert(!isNaN(width), 'width must be a number'); } var height; if (!goog.isNull(this.height_)) { - height = ol.expr.evaluateFeature(this.height_, feature); + height = Number(ol.expr.evaluateFeature(this.height_, feature)); goog.asserts.assertNumber(height, 'height must be a number'); } - var opacity = ol.expr.evaluateFeature(this.opacity_, feature); - goog.asserts.assertNumber(opacity, 'opacity must be a number'); + var opacity = Number(ol.expr.evaluateFeature(this.opacity_, feature)); + goog.asserts.assert(!isNaN(opacity), 'opacity must be a number'); - var rotation = ol.expr.evaluateFeature(this.rotation_, feature); - goog.asserts.assertNumber(rotation, 'rotation must be a number'); + var rotation = Number(ol.expr.evaluateFeature(this.rotation_, feature)); + goog.asserts.assert(!isNaN(rotation), 'rotation must be a number'); literal = new ol.style.IconLiteral({ url: url, diff --git a/src/ol/style/shapesymbolizer.js b/src/ol/style/shapesymbolizer.js index 29281bb002..2e7b00fd1c 100644 --- a/src/ol/style/shapesymbolizer.js +++ b/src/ol/style/shapesymbolizer.js @@ -73,17 +73,17 @@ ol.style.Shape.prototype.createLiteral = function(featureOrType) { var literal = null; if (type === ol.geom.GeometryType.POINT || type === ol.geom.GeometryType.MULTIPOINT) { - var size = ol.expr.evaluateFeature(this.size_, feature); - goog.asserts.assertNumber(size, 'size must be a number'); + var size = Number(ol.expr.evaluateFeature(this.size_, feature)); + goog.asserts.assert(!isNaN(size), 'size must be a number'); var fillColor, fillOpacity; if (!goog.isNull(this.fill_)) { fillColor = ol.expr.evaluateFeature(this.fill_.getColor(), feature); goog.asserts.assertString( fillColor, 'fillColor must be a string'); - fillOpacity = ol.expr.evaluateFeature(this.fill_.getOpacity(), feature); - goog.asserts.assertNumber( - fillOpacity, 'fillOpacity must be a number'); + fillOpacity = Number(ol.expr.evaluateFeature( + this.fill_.getOpacity(), feature)); + goog.asserts.assert(!isNaN(fillOpacity), 'fillOpacity must be a number'); } var strokeColor, strokeOpacity, strokeWidth; @@ -91,13 +91,13 @@ ol.style.Shape.prototype.createLiteral = function(featureOrType) { strokeColor = ol.expr.evaluateFeature(this.stroke_.getColor(), feature); goog.asserts.assertString( strokeColor, 'strokeColor must be a string'); - strokeOpacity = ol.expr.evaluateFeature(this.stroke_.getOpacity(), - feature); - goog.asserts.assertNumber( - strokeOpacity, 'strokeOpacity must be a number'); - strokeWidth = ol.expr.evaluateFeature(this.stroke_.getWidth(), feature); - goog.asserts.assertNumber( - strokeWidth, 'strokeWidth must be a number'); + strokeOpacity = Number(ol.expr.evaluateFeature( + this.stroke_.getOpacity(), feature)); + goog.asserts.assert(!isNaN(strokeOpacity), + 'strokeOpacity must be a number'); + strokeWidth = Number(ol.expr.evaluateFeature( + this.stroke_.getWidth(), feature)); + goog.asserts.assert(!isNaN(strokeWidth), 'strokeWidth must be a number'); } literal = new ol.style.ShapeLiteral({ diff --git a/src/ol/style/strokesymbolizer.js b/src/ol/style/strokesymbolizer.js index ef14a01f54..f1f0b389aa 100644 --- a/src/ol/style/strokesymbolizer.js +++ b/src/ol/style/strokesymbolizer.js @@ -71,13 +71,13 @@ ol.style.Stroke.prototype.createLiteral = function(featureOrType) { this.color_, feature); goog.asserts.assertString(color, 'color must be a string'); - var opacity = ol.expr.evaluateFeature( - this.opacity_, feature); - goog.asserts.assertNumber(opacity, 'opacity must be a number'); + var opacity = Number(ol.expr.evaluateFeature( + this.opacity_, feature)); + goog.asserts.assert(!isNaN(opacity), 'opacity must be a number'); - var width = ol.expr.evaluateFeature( - this.width_, feature); - goog.asserts.assertNumber(width, 'width must be a number'); + var width = Number(ol.expr.evaluateFeature( + this.width_, feature)); + goog.asserts.assert(!isNaN(width), 'width must be a number'); var literal = null; if (type === ol.geom.GeometryType.LINESTRING || diff --git a/src/ol/style/textsymbolizer.js b/src/ol/style/textsymbolizer.js index 9cc4d9bb83..39230ed944 100644 --- a/src/ol/style/textsymbolizer.js +++ b/src/ol/style/textsymbolizer.js @@ -84,14 +84,14 @@ ol.style.Text.prototype.createLiteral = function(featureOrType) { var fontFamily = ol.expr.evaluateFeature(this.fontFamily_, feature); goog.asserts.assertString(fontFamily, 'fontFamily must be a string'); - var fontSize = ol.expr.evaluateFeature(this.fontSize_, feature); - goog.asserts.assertNumber(fontSize, 'fontSize must be a number'); + var fontSize = Number(ol.expr.evaluateFeature(this.fontSize_, feature)); + goog.asserts.assert(!isNaN(fontSize), 'fontSize must be a number'); var text = ol.expr.evaluateFeature(this.text_, feature); goog.asserts.assertString(text, 'text must be a string'); - var opacity = ol.expr.evaluateFeature(this.opacity_, feature); - goog.asserts.assertNumber(opacity, 'opacity must be a number'); + var opacity = Number(ol.expr.evaluateFeature(this.opacity_, feature)); + goog.asserts.assert(!isNaN(opacity), 'opacity must be a number'); return new ol.style.TextLiteral({ color: color, diff --git a/test/spec/ol/style/fillsymbolizer.test.js b/test/spec/ol/style/fillsymbolizer.test.js index 14797ced49..b83d2064ca 100644 --- a/test/spec/ol/style/fillsymbolizer.test.js +++ b/test/spec/ol/style/fillsymbolizer.test.js @@ -64,6 +64,23 @@ describe('ol.style.Fill', function() { expect(literal.fillOpacity).to.be(0.8); }); + it('casts opacity to number', function() { + var symbolizer = new ol.style.Fill({ + opacity: ol.expr.parse('opacity'), + color: '#ff00ff' + }); + + var feature = new ol.Feature({ + opacity: '0.55', + geometry: new ol.geom.Polygon( + [[[0, 0], [1, 0], [1, 1], [0, 1], [0, 0]]]) + }); + + var literal = symbolizer.createLiteral(feature); + expect(literal).to.be.a(ol.style.PolygonLiteral); + expect(literal.fillOpacity).to.be(0.55); + }); + }); describe('#getColor()', function() { diff --git a/test/spec/ol/style/iconsymbolizer.test.js b/test/spec/ol/style/iconsymbolizer.test.js index 8433a29435..10cc573af4 100644 --- a/test/spec/ol/style/iconsymbolizer.test.js +++ b/test/spec/ol/style/iconsymbolizer.test.js @@ -75,19 +75,82 @@ describe('ol.style.Icon', function() { expect(literal.url).to.be('http://example.com/1.png'); }); - it('applies default type if none provided', function() { + it('applies default opacity if none provided', function() { var symbolizer = new ol.style.Icon({ - height: ol.expr.parse('10'), - width: ol.expr.parse('20'), - url: ol.expr.parse('"http://example.com/1.png"') + height: 10, + width: 20, + url: 'http://example.com/1.png' }); var literal = symbolizer.createLiteral(ol.geom.GeometryType.POINT); expect(literal).to.be.a(ol.style.IconLiteral); expect(literal.opacity).to.be(1); + }); + + it('applies default rotation if none provided', function() { + var symbolizer = new ol.style.Icon({ + height: 10, + width: 20, + url: 'http://example.com/1.png' + }); + + var literal = symbolizer.createLiteral(ol.geom.GeometryType.POINT); + expect(literal).to.be.a(ol.style.IconLiteral); expect(literal.rotation).to.be(0); }); + it('casts opacity to number', function() { + var symbolizer = new ol.style.Icon({ + opacity: ol.expr.parse('opacity'), + height: 10, + width: 20, + url: 'http://example.com/1.png' + }); + + var feature = new ol.Feature({ + opacity: '0.53', + geometry: new ol.geom.Point([1, 2]) + }); + + var literal = symbolizer.createLiteral(feature); + expect(literal).to.be.a(ol.style.IconLiteral); + expect(literal.opacity).to.be(0.53); + }); + + it('casts width to number', function() { + var symbolizer = new ol.style.Icon({ + width: ol.expr.parse('width'), + height: 10, + url: 'http://example.com/1.png' + }); + + var feature = new ol.Feature({ + width: '42', + geometry: new ol.geom.Point([1, 2]) + }); + + var literal = symbolizer.createLiteral(feature); + expect(literal).to.be.a(ol.style.IconLiteral); + expect(literal.width).to.be(42); + }); + + it('casts height to number', function() { + var symbolizer = new ol.style.Icon({ + height: ol.expr.parse('height'), + width: 10, + url: 'http://example.com/1.png' + }); + + var feature = new ol.Feature({ + height: '42', + geometry: new ol.geom.Point([1, 2]) + }); + + var literal = symbolizer.createLiteral(feature); + expect(literal).to.be.a(ol.style.IconLiteral); + expect(literal.height).to.be(42); + }); + }); describe('#getHeight()', function() { diff --git a/test/spec/ol/style/shapesymbolizer.test.js b/test/spec/ol/style/shapesymbolizer.test.js index 7831c81d00..b20cfb7303 100644 --- a/test/spec/ol/style/shapesymbolizer.test.js +++ b/test/spec/ol/style/shapesymbolizer.test.js @@ -71,6 +71,95 @@ describe('ol.style.Shape', function() { expect(literal.strokeWidth).to.be(2); }); + it('casts size to number', function() { + var symbolizer = new ol.style.Shape({ + size: ol.expr.parse('size'), + fill: new ol.style.Fill({ + color: '#BADA55' + }), + stroke: new ol.style.Stroke({ + color: '#013', + opacity: 1, + width: 2 + }) + }); + + var feature = new ol.Feature({ + size: '42', + geometry: new ol.geom.Point([1, 2]) + }); + + var literal = symbolizer.createLiteral(feature); + expect(literal).to.be.a(ol.style.ShapeLiteral); + expect(literal.size).to.be(42); + }); + + it('casts stroke width to number', function() { + var symbolizer = new ol.style.Shape({ + fill: new ol.style.Fill({ + color: '#BADA55' + }), + stroke: new ol.style.Stroke({ + color: '#013', + opacity: 1, + width: ol.expr.parse('strokeWidth') + }) + }); + + var feature = new ol.Feature({ + strokeWidth: '4.2', + geometry: new ol.geom.Point([1, 2]) + }); + + var literal = symbolizer.createLiteral(feature); + expect(literal).to.be.a(ol.style.ShapeLiteral); + expect(literal.strokeWidth).to.be(4.2); + }); + + it('casts stroke opacity to number', function() { + var symbolizer = new ol.style.Shape({ + fill: new ol.style.Fill({ + color: '#BADA55' + }), + stroke: new ol.style.Stroke({ + color: '#013', + opacity: ol.expr.parse('strokeOpacity'), + width: 3 + }) + }); + + var feature = new ol.Feature({ + strokeOpacity: '.2', + geometry: new ol.geom.Point([1, 2]) + }); + + var literal = symbolizer.createLiteral(feature); + expect(literal).to.be.a(ol.style.ShapeLiteral); + expect(literal.strokeOpacity).to.be(0.2); + }); + + it('casts fill opacity to number', function() { + var symbolizer = new ol.style.Shape({ + fill: new ol.style.Fill({ + opacity: ol.expr.parse('fillOpacity'), + color: '#BADA55' + }), + stroke: new ol.style.Stroke({ + color: '#013', + width: 3 + }) + }); + + var feature = new ol.Feature({ + fillOpacity: '.42', + geometry: new ol.geom.Point([1, 2]) + }); + + var literal = symbolizer.createLiteral(feature); + expect(literal).to.be.a(ol.style.ShapeLiteral); + expect(literal.fillOpacity).to.be(0.42); + }); + }); describe('#getFill()', function() { diff --git a/test/spec/ol/style/textsymbolizer.test.js b/test/spec/ol/style/textsymbolizer.test.js index 85a4113fb9..8319a85782 100644 --- a/test/spec/ol/style/textsymbolizer.test.js +++ b/test/spec/ol/style/textsymbolizer.test.js @@ -74,7 +74,7 @@ describe('ol.style.Text', function() { expect(literal.opacity).to.be(0.6); }); - it('applies default type if none provided', function() { + it('applies defaults if none provided', function() { var symbolizer = new ol.style.Text({ text: 'Test' }); @@ -88,6 +88,34 @@ describe('ol.style.Text', function() { expect(literal.opacity).to.be(1); }); + it('casts size to number', function() { + var symbolizer = new ol.style.Text({ + text: 'test', + fontSize: ol.expr.parse('size') + }); + + var feature = new ol.Feature({ + size: '42' + }); + + var literal = symbolizer.createLiteral(feature); + expect(literal.fontSize).to.be(42); + }); + + it('casts opacity to number', function() { + var symbolizer = new ol.style.Text({ + text: 'test', + opacity: ol.expr.parse('opacity') + }); + + var feature = new ol.Feature({ + opacity: '0.42' + }); + + var literal = symbolizer.createLiteral(feature); + expect(literal.opacity).to.be(0.42); + }); + }); describe('#getColor()', function() {