From 7a769830fe0eb2b8b4caf8b23d1eed52c7cd27c6 Mon Sep 17 00:00:00 2001 From: Bart van den Eijnden Date: Mon, 19 Mar 2012 11:13:27 +0100 Subject: [PATCH 1/4] ignore whitespace in filter values --- lib/OpenLayers/Format/Filter/v1.js | 8 ++--- tests/Format/SLD/v1_0_0.html | 54 ++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 4 deletions(-) diff --git a/lib/OpenLayers/Format/Filter/v1.js b/lib/OpenLayers/Format/Filter/v1.js index 6757456d1e..fcbe0a49a7 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).replace(this.regExes.trimSpace, "")); }, "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).replace(this.regExes.trimSpace, "")); }, "UpperBoundary": function(node, filter) { filter.upperBoundary = OpenLayers.String.numericIf( - this.readers.ogc._expression.call(this, node)); + this.readers.ogc._expression.call(this, node).replace(this.regExes.trimSpace, "")); }, "Intersects": function(node, obj) { this.readSpatial(node, obj, OpenLayers.Filter.Spatial.INTERSECTS); diff --git a/tests/Format/SLD/v1_0_0.html b/tests/Format/SLD/v1_0_0.html index a295a746b8..1f50e4fa14 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 @@ --> +
From 94bd37031d508845a85238a77da354f79eb56a0b Mon Sep 17 00:00:00 2001 From: Bart van den Eijnden Date: Mon, 19 Mar 2012 13:07:49 +0100 Subject: [PATCH 2/4] instead, add a trimWhitespace option to OpenLayers.String.numericIf as discussed with @ahocevar --- lib/OpenLayers/BaseTypes.js | 6 +++++- lib/OpenLayers/Format/Filter/v1.js | 6 +++--- tests/BaseTypes.html | 8 ++++++-- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/lib/OpenLayers/BaseTypes.js b/lib/OpenLayers/BaseTypes.js index ed8650ec08..0d78335817 100644 --- a/lib/OpenLayers/BaseTypes.js +++ b/lib/OpenLayers/BaseTypes.js @@ -189,12 +189,16 @@ 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) { + numericIf: function(value, trimWhitespace) { + if (trimWhitespace === true) { + value = value.replace(/^\s*|\s*$/g, ""); + } return OpenLayers.String.isNumeric(value) ? parseFloat(value) : value; } diff --git a/lib/OpenLayers/Format/Filter/v1.js b/lib/OpenLayers/Format/Filter/v1.js index fcbe0a49a7..851fa94755 100644 --- a/lib/OpenLayers/Format/Filter/v1.js +++ b/lib/OpenLayers/Format/Filter/v1.js @@ -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).replace(this.regExes.trimSpace, "")); + 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).replace(this.regExes.trimSpace, "")); + this.readers.ogc._expression.call(this, node), true); }, "UpperBoundary": function(node, filter) { filter.upperBoundary = OpenLayers.String.numericIf( - this.readers.ogc._expression.call(this, node).replace(this.regExes.trimSpace, "")); + 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..a8af69be2b 100644 --- a/tests/BaseTypes.html +++ b/tests/BaseTypes.html @@ -220,9 +220,10 @@ {value: " 3", expect: " 3"}, {value: "1e", expect: "1e"}, {value: "1+e", expect: "1+e"}, - {value: "1-e", expect: "1-e"} + {value: "1-e", expect: "1-e"}, + {value: " 27 ", expect: " 27 "} ]; - t.plan(cases.length); + t.plan(cases.length + 1); var func = OpenLayers.String.numericIf; var obj, val, got, exp; @@ -233,6 +234,9 @@ got = func(val); t.eq(got, exp, "'" + val + "' returns " + exp); } + + // test the trimWhitespace option + t.eq(OpenLayers.String.numericIf(" 27 ", true), 27, "if trimWhitespace is true, we expect to get a Number"); } From 430d09eb55f2b3f18cb64ebf3a53e8c850076d5c Mon Sep 17 00:00:00 2001 From: Bart van den Eijnden Date: Wed, 21 Mar 2012 12:17:44 +0100 Subject: [PATCH 3/4] incorporate review by @marcjansen --- lib/OpenLayers/BaseTypes.js | 3 ++- tests/BaseTypes.html | 21 ++++++++++++++++----- tests/Format/SLD/v1_0_0.html | 4 ++-- 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/lib/OpenLayers/BaseTypes.js b/lib/OpenLayers/BaseTypes.js index 0d78335817..0856ad22a6 100644 --- a/lib/OpenLayers/BaseTypes.js +++ b/lib/OpenLayers/BaseTypes.js @@ -196,10 +196,11 @@ OpenLayers.String = { * otherwise. */ numericIf: function(value, trimWhitespace) { + var originalValue = value; if (trimWhitespace === true) { value = value.replace(/^\s*|\s*$/g, ""); } - return OpenLayers.String.isNumeric(value) ? parseFloat(value) : value; + return OpenLayers.String.isNumeric(value) ? parseFloat(value) : originalValue; } }; diff --git a/tests/BaseTypes.html b/tests/BaseTypes.html index a8af69be2b..3f1f87cfd2 100644 --- a/tests/BaseTypes.html +++ b/tests/BaseTypes.html @@ -221,9 +221,18 @@ {value: "1e", expect: "1e"}, {value: "1+e", expect: "1+e"}, {value: "1-e", expect: "1-e"}, - {value: " 27 ", expect: " 27 "} + {value: " 27 ", expect: " 27 ", expectWithTrim: 27}, + {value: " abc ", expect: " abc ", expectWithTrim: " abc "} ]; - t.plan(cases.length + 1); + var count = 0; + for (var i=0, ii=cases.length; i - 29.7 + 29.7 @@ -1005,7 +1005,7 @@ - 36.2 + 36.2 From 221292436afe23651c4ab7b8db3b749dc921865b Mon Sep 17 00:00:00 2001 From: Bart van den Eijnden Date: Wed, 21 Mar 2012 13:15:16 +0100 Subject: [PATCH 4/4] add expectWithTrim to all test cases and protect numericIf against failure --- lib/OpenLayers/BaseTypes.js | 2 +- tests/BaseTypes.html | 58 +++++++++++++++---------------------- 2 files changed, 25 insertions(+), 35 deletions(-) diff --git a/lib/OpenLayers/BaseTypes.js b/lib/OpenLayers/BaseTypes.js index 0856ad22a6..da5562725f 100644 --- a/lib/OpenLayers/BaseTypes.js +++ b/lib/OpenLayers/BaseTypes.js @@ -197,7 +197,7 @@ OpenLayers.String = { */ numericIf: function(value, trimWhitespace) { var originalValue = value; - if (trimWhitespace === true) { + if (trimWhitespace === true && value != null && value.replace) { value = value.replace(/^\s*|\s*$/g, ""); } return OpenLayers.String.isNumeric(value) ? parseFloat(value) : originalValue; diff --git a/tests/BaseTypes.html b/tests/BaseTypes.html index 3f1f87cfd2..cc391003da 100644 --- a/tests/BaseTypes.html +++ b/tests/BaseTypes.html @@ -201,38 +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 "} ]; - var count = 0; - for (var i=0, ii=cases.length; i