diff --git a/lib/OpenLayers/BaseTypes.js b/lib/OpenLayers/BaseTypes.js index ed8650ec08..da5562725f 100644 --- a/lib/OpenLayers/BaseTypes.js +++ b/lib/OpenLayers/BaseTypes.js @@ -189,13 +189,18 @@ OpenLayers.String = { * * Parameters: * value - {String} + * trimWhitespace - {Boolean} * * Returns: * {Number|String} a Number if the passed value is a number, a String * otherwise. */ - numericIf: function(value) { - return OpenLayers.String.isNumeric(value) ? parseFloat(value) : value; + numericIf: function(value, trimWhitespace) { + var originalValue = value; + if (trimWhitespace === true && value != null && value.replace) { + value = value.replace(/^\s*|\s*$/g, ""); + } + return OpenLayers.String.isNumeric(value) ? parseFloat(value) : originalValue; } }; diff --git a/lib/OpenLayers/Format/Filter/v1.js b/lib/OpenLayers/Format/Filter/v1.js index 6757456d1e..851fa94755 100644 --- a/lib/OpenLayers/Format/Filter/v1.js +++ b/lib/OpenLayers/Format/Filter/v1.js @@ -27,7 +27,7 @@ OpenLayers.Format.Filter.v1 = OpenLayers.Class(OpenLayers.Format.XML, { xlink: "http://www.w3.org/1999/xlink", xsi: "http://www.w3.org/2001/XMLSchema-instance" }, - + /** * Property: defaultPrefix */ @@ -180,18 +180,18 @@ OpenLayers.Format.Filter.v1 = OpenLayers.Class(OpenLayers.Format.XML, { }, "Literal": function(node, obj) { obj.value = OpenLayers.String.numericIf( - this.getChildValue(node)); + this.getChildValue(node), true); }, "PropertyName": function(node, filter) { filter.property = this.getChildValue(node); }, "LowerBoundary": function(node, filter) { filter.lowerBoundary = OpenLayers.String.numericIf( - this.readers.ogc._expression.call(this, node)); + this.readers.ogc._expression.call(this, node), true); }, "UpperBoundary": function(node, filter) { filter.upperBoundary = OpenLayers.String.numericIf( - this.readers.ogc._expression.call(this, node)); + this.readers.ogc._expression.call(this, node), true); }, "Intersects": function(node, obj) { this.readSpatial(node, obj, OpenLayers.Filter.Spatial.INTERSECTS); diff --git a/tests/BaseTypes.html b/tests/BaseTypes.html index c264d4a54f..cc391003da 100644 --- a/tests/BaseTypes.html +++ b/tests/BaseTypes.html @@ -201,28 +201,30 @@ function test_Number_numericIf(t) { var cases = [ - {value: "3", expect: 3}, - {value: "+3", expect: 3}, - {value: "-3", expect: -3}, - {value: "3.0", expect: 3}, - {value: "+3.0", expect: 3}, - {value: "-3.0", expect: -3}, - {value: "6.02e23", expect: 6.02e23}, - {value: "+1.0e-100", expect: 1e-100}, - {value: "-1.0e+100", expect: -1e100}, - {value: "1E100", expect: 1e100}, - {value: null, expect: null}, - {value: true, expect: true}, - {value: false, expect: false}, - {value: undefined, expect: undefined}, - {value: "", expect: ""}, - {value: "3 ", expect: "3 "}, - {value: " 3", expect: " 3"}, - {value: "1e", expect: "1e"}, - {value: "1+e", expect: "1+e"}, - {value: "1-e", expect: "1-e"} + {value: "3", expect: 3, expectWithTrim: 3}, + {value: "+3", expect: 3, expectWithTrim: 3}, + {value: "-3", expect: -3, expectWithTrim: -3}, + {value: "3.0", expect: 3, expectWithTrim: 3}, + {value: "+3.0", expect: 3, expectWithTrim: 3}, + {value: "-3.0", expect: -3, expectWithTrim: -3}, + {value: "6.02e23", expect: 6.02e23, expectWithTrim: 6.02e23}, + {value: "+1.0e-100", expect: 1e-100, expectWithTrim: 1e-100}, + {value: "-1.0e+100", expect: -1e100, expectWithTrim: -1e100}, + {value: "1E100", expect: 1e100, expectWithTrim: 1e100}, + {value: null, expect: null, expectWithTrim: null}, + {value: true, expect: true, expectWithTrim: true}, + {value: false, expect: false, expectWithTrim: false}, + {value: undefined, expect: undefined, expectWithTrim: undefined}, + {value: "", expect: "", expectWithTrim: ""}, + {value: "3 ", expect: "3 ", expectWithTrim: 3}, + {value: " 3", expect: " 3", expectWithTrim: 3}, + {value: "1e", expect: "1e", expectWithTrim: "1e"}, + {value: "1+e", expect: "1+e", expectWithTrim: "1+e"}, + {value: "1-e", expect: "1-e", expectWithTrim: "1-e"}, + {value: " 27 ", expect: " 27 ", expectWithTrim: 27}, + {value: " abc ", expect: " abc ", expectWithTrim: " abc "} ]; - t.plan(cases.length); + t.plan(cases.length*2); var func = OpenLayers.String.numericIf; var obj, val, got, exp; @@ -232,6 +234,9 @@ exp = obj.expect; got = func(val); t.eq(got, exp, "'" + val + "' returns " + exp); + got = func(val, true); + exp = obj.expectWithTrim; + t.eq(got, exp, "'" + val + "' returns " + exp + " with trimWhitespace true"); } } diff --git a/tests/Format/SLD/v1_0_0.html b/tests/Format/SLD/v1_0_0.html index a295a746b8..fbc18a6552 100644 --- a/tests/Format/SLD/v1_0_0.html +++ b/tests/Format/SLD/v1_0_0.html @@ -523,6 +523,14 @@ } + function test_whitespace(t) { + t.plan(1); + var xml = readXML("propertyisbetweenwhitespace.sld"); + var output = new OpenLayers.Format.SLD().read(xml); + var filter = output.namedLayers['geonode:US_Stat0'].userStyles[0].rules[0].filter; + t.eq(filter.lowerBoundary, 29.7, "whitespace ignored in values and value transformed to number"); + } + function test_label_LinePlacement(t) { t.plan(1); var format = new OpenLayers.Format.SLD.v1_0_0({ @@ -970,5 +978,51 @@ --> +