Merge pull request #334 from bartvde/sldwhitespace

ignore whitespace in filter values (r=@marcjansen,@ahocevar)
This commit is contained in:
Bart van den Eijnden
2012-03-21 05:16:45 -07:00
4 changed files with 91 additions and 27 deletions
+7 -2
View File
@@ -189,13 +189,18 @@ OpenLayers.String = {
* *
* Parameters: * Parameters:
* value - {String} * value - {String}
* trimWhitespace - {Boolean}
* *
* Returns: * Returns:
* {Number|String} a Number if the passed value is a number, a String * {Number|String} a Number if the passed value is a number, a String
* otherwise. * otherwise.
*/ */
numericIf: function(value) { numericIf: function(value, trimWhitespace) {
return OpenLayers.String.isNumeric(value) ? parseFloat(value) : value; var originalValue = value;
if (trimWhitespace === true && value != null && value.replace) {
value = value.replace(/^\s*|\s*$/g, "");
}
return OpenLayers.String.isNumeric(value) ? parseFloat(value) : originalValue;
} }
}; };
+3 -3
View File
@@ -180,18 +180,18 @@ OpenLayers.Format.Filter.v1 = OpenLayers.Class(OpenLayers.Format.XML, {
}, },
"Literal": function(node, obj) { "Literal": function(node, obj) {
obj.value = OpenLayers.String.numericIf( obj.value = OpenLayers.String.numericIf(
this.getChildValue(node)); this.getChildValue(node), true);
}, },
"PropertyName": function(node, filter) { "PropertyName": function(node, filter) {
filter.property = this.getChildValue(node); filter.property = this.getChildValue(node);
}, },
"LowerBoundary": function(node, filter) { "LowerBoundary": function(node, filter) {
filter.lowerBoundary = OpenLayers.String.numericIf( filter.lowerBoundary = OpenLayers.String.numericIf(
this.readers.ogc._expression.call(this, node)); this.readers.ogc._expression.call(this, node), true);
}, },
"UpperBoundary": function(node, filter) { "UpperBoundary": function(node, filter) {
filter.upperBoundary = OpenLayers.String.numericIf( filter.upperBoundary = OpenLayers.String.numericIf(
this.readers.ogc._expression.call(this, node)); this.readers.ogc._expression.call(this, node), true);
}, },
"Intersects": function(node, obj) { "Intersects": function(node, obj) {
this.readSpatial(node, obj, OpenLayers.Filter.Spatial.INTERSECTS); this.readSpatial(node, obj, OpenLayers.Filter.Spatial.INTERSECTS);
+26 -21
View File
@@ -201,28 +201,30 @@
function test_Number_numericIf(t) { function test_Number_numericIf(t) {
var cases = [ var cases = [
{value: "3", expect: 3}, {value: "3", expect: 3, expectWithTrim: 3},
{value: "+3", expect: 3}, {value: "+3", expect: 3, expectWithTrim: 3},
{value: "-3", expect: -3}, {value: "-3", expect: -3, expectWithTrim: -3},
{value: "3.0", expect: 3}, {value: "3.0", expect: 3, expectWithTrim: 3},
{value: "+3.0", expect: 3}, {value: "+3.0", expect: 3, expectWithTrim: 3},
{value: "-3.0", expect: -3}, {value: "-3.0", expect: -3, expectWithTrim: -3},
{value: "6.02e23", expect: 6.02e23}, {value: "6.02e23", expect: 6.02e23, expectWithTrim: 6.02e23},
{value: "+1.0e-100", expect: 1e-100}, {value: "+1.0e-100", expect: 1e-100, expectWithTrim: 1e-100},
{value: "-1.0e+100", expect: -1e100}, {value: "-1.0e+100", expect: -1e100, expectWithTrim: -1e100},
{value: "1E100", expect: 1e100}, {value: "1E100", expect: 1e100, expectWithTrim: 1e100},
{value: null, expect: null}, {value: null, expect: null, expectWithTrim: null},
{value: true, expect: true}, {value: true, expect: true, expectWithTrim: true},
{value: false, expect: false}, {value: false, expect: false, expectWithTrim: false},
{value: undefined, expect: undefined}, {value: undefined, expect: undefined, expectWithTrim: undefined},
{value: "", expect: ""}, {value: "", expect: "", expectWithTrim: ""},
{value: "3 ", expect: "3 "}, {value: "3 ", expect: "3 ", expectWithTrim: 3},
{value: " 3", expect: " 3"}, {value: " 3", expect: " 3", expectWithTrim: 3},
{value: "1e", expect: "1e"}, {value: "1e", expect: "1e", expectWithTrim: "1e"},
{value: "1+e", expect: "1+e"}, {value: "1+e", expect: "1+e", expectWithTrim: "1+e"},
{value: "1-e", expect: "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 func = OpenLayers.String.numericIf;
var obj, val, got, exp; var obj, val, got, exp;
@@ -232,6 +234,9 @@
exp = obj.expect; exp = obj.expect;
got = func(val); got = func(val);
t.eq(got, exp, "'" + val + "' returns " + exp); t.eq(got, exp, "'" + val + "' returns " + exp);
got = func(val, true);
exp = obj.expectWithTrim;
t.eq(got, exp, "'" + val + "' returns " + exp + " with trimWhitespace true");
} }
} }
+54
View File
@@ -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) { function test_label_LinePlacement(t) {
t.plan(1); t.plan(1);
var format = new OpenLayers.Format.SLD.v1_0_0({ var format = new OpenLayers.Format.SLD.v1_0_0({
@@ -970,5 +978,51 @@
</sld:FeatureTypeStyle> </sld:FeatureTypeStyle>
</sld:UserStyle> </sld:UserStyle>
--></div> --></div>
<div id="propertyisbetweenwhitespace.sld"><!--
<sld:StyledLayerDescriptor xmlns="http://www.opengis.net/sld" xmlns:sld="http://www.opengis.net/sld" xmlns:ogc="http://www.opengis.net/ogc" xmlns:gml="http://www.opengis.net/gml" version="1.0.0">
<sld:NamedLayer>
<sld:Name>geonode:US_Stat0</sld:Name>
<sld:UserStyle>
<sld:Name>US_Stat0_5cbbe918</sld:Name>
<sld:Title>BMI&lt;25</sld:Title>
<sld:FeatureTypeStyle>
<sld:Name>name</sld:Name>
<sld:Rule>
<sld:Title>BMI&lt;25</sld:Title>
<ogc:Filter>
<ogc:PropertyIsBetween>
<ogc:PropertyName>Hlt_st_BMI</ogc:PropertyName>
<ogc:LowerBoundary>
<ogc:Literal>
29.7
</ogc:Literal>
</ogc:LowerBoundary>
<ogc:UpperBoundary>
<ogc:Literal>
36.2
</ogc:Literal>
</ogc:UpperBoundary>
</ogc:PropertyIsBetween>
</ogc:Filter>
<sld:PolygonSymbolizer>
<sld:Fill>
<sld:CssParameter name="fill">#C0F58C</sld:CssParameter>
</sld:Fill>
<sld:Stroke/>
</sld:PolygonSymbolizer>
</sld:Rule>
</sld:FeatureTypeStyle>
</sld:UserStyle>
</sld:NamedLayer>
</sld:StyledLayerDescriptor>
--></div>
</body> </body>
</html> </html>