From d5013d6df52b7e34e1e9b5faded3cddd66c15960 Mon Sep 17 00:00:00 2001 From: Matt Walker Date: Fri, 12 Oct 2012 22:40:56 +0100 Subject: [PATCH 1/9] Added support for PropertyIsNull filter. Added a simple comparison and read/write for PropertyIsNull encoded as XML including tests for each. --- lib/OpenLayers/Filter/Comparison.js | 8 +++++- lib/OpenLayers/Format/Filter/v1.js | 13 ++++++++++ tests/Filter/Comparison.html | 28 +++++++++++++++++++++ tests/Format/Filter/v1.html | 38 +++++++++++++++++++++++++++++ 4 files changed, 86 insertions(+), 1 deletion(-) diff --git a/lib/OpenLayers/Filter/Comparison.js b/lib/OpenLayers/Filter/Comparison.js index c57c868aea..bb40900995 100644 --- a/lib/OpenLayers/Filter/Comparison.js +++ b/lib/OpenLayers/Filter/Comparison.js @@ -26,7 +26,8 @@ OpenLayers.Filter.Comparison = OpenLayers.Class(OpenLayers.Filter, { * - OpenLayers.Filter.Comparison.LESS_THAN_OR_EQUAL_TO = "<="; * - OpenLayers.Filter.Comparison.GREATER_THAN_OR_EQUAL_TO = ">="; * - OpenLayers.Filter.Comparison.BETWEEN = ".."; - * - OpenLayers.Filter.Comparison.LIKE = "~"; + * - OpenLayers.Filter.Comparison.LIKE = "~"; + * - OpenLayers.Filter.Comparison.IS_NULL = "NULL"; */ type: null, @@ -154,6 +155,10 @@ OpenLayers.Filter.Comparison = OpenLayers.Class(OpenLayers.Filter, { var regexp = new RegExp(this.value, "gi"); result = regexp.test(got); break; + case OpenLayers.Filter.Comparison.IS_NULL: + var got = context[this.property]; + result = (got === null || got === undefined); + break; } return result; }, @@ -260,3 +265,4 @@ OpenLayers.Filter.Comparison.LESS_THAN_OR_EQUAL_TO = "<="; OpenLayers.Filter.Comparison.GREATER_THAN_OR_EQUAL_TO = ">="; OpenLayers.Filter.Comparison.BETWEEN = ".."; OpenLayers.Filter.Comparison.LIKE = "~"; +OpenLayers.Filter.Comparison.IS_NULL = "NULL"; diff --git a/lib/OpenLayers/Format/Filter/v1.js b/lib/OpenLayers/Format/Filter/v1.js index 4798c9b3ae..bd7cc32f09 100644 --- a/lib/OpenLayers/Format/Filter/v1.js +++ b/lib/OpenLayers/Format/Filter/v1.js @@ -212,6 +212,13 @@ OpenLayers.Format.Filter.v1 = OpenLayers.Class(OpenLayers.Format.XML, { "Function": function(node, obj) { //TODO write decoder for it return; + }, + "PropertyIsNull": function(node, obj) { + var filter = new OpenLayers.Filter.Comparison({ + type: OpenLayers.Filter.Comparison.IS_NULL + }); + this.readChildNodes(node, filter); + obj.filters.push(filter); } } }, @@ -448,6 +455,11 @@ OpenLayers.Format.Filter.v1 = OpenLayers.Class(OpenLayers.Format.XML, { this.writeOgcExpression(params[i], node); } return node; + }, + "PropertyIsNull": function(filter) { + var node = this.createElementNSPlus("ogc:PropertyIsNull"); + this.writeNode("PropertyName", filter, node); + return node; } } }, @@ -480,6 +492,7 @@ OpenLayers.Format.Filter.v1 = OpenLayers.Class(OpenLayers.Format.XML, { ">=": "PropertyIsGreaterThanOrEqualTo", "..": "PropertyIsBetween", "~": "PropertyIsLike", + "NULL": "PropertyIsNull", "BBOX": "BBOX", "DWITHIN": "DWITHIN", "WITHIN": "WITHIN", diff --git a/tests/Filter/Comparison.html b/tests/Filter/Comparison.html index a0c2da35be..5dd1cc1bf2 100644 --- a/tests/Filter/Comparison.html +++ b/tests/Filter/Comparison.html @@ -164,6 +164,34 @@ }), context: {prop: "FOO"}, expect: false + }, { + filter: new OpenLayers.Filter.Comparison({ + type: OpenLayers.Filter.Comparison.IS_NULL, + property: "prop" + }), + context: {prop: null}, + expect: true + }, { + filter: new OpenLayers.Filter.Comparison({ + type: OpenLayers.Filter.Comparison.IS_NULL, + property: "prop" + }), + context: {prop: undefined}, + expect: true + }, { + filter: new OpenLayers.Filter.Comparison({ + type: OpenLayers.Filter.Comparison.IS_NULL, + property: "prop" + }), + context: {prop: "foo"}, + expect: false + }, { + filter: new OpenLayers.Filter.Comparison({ + type: OpenLayers.Filter.Comparison.IS_NULL, + property: "prop" + }), + context: {prop: 0}, + expect: false }]; t.plan(cases.length); diff --git a/tests/Format/Filter/v1.html b/tests/Format/Filter/v1.html index b109b31325..ab43d8cb51 100644 --- a/tests/Format/Filter/v1.html +++ b/tests/Format/Filter/v1.html @@ -51,6 +51,44 @@ "[1] record correct upper boundary value"); } + function test_PropertyIsNull(t) { + + t.plan(6); + + var test_xml, format, xml; + + format = new OpenLayers.Format.Filter.v1(); + xml = new OpenLayers.Format.XML(); + + test_xml = + '' + + '' + + 'prop' + + '' + + ''; + + var filter = format.read(xml.read(test_xml).documentElement); + t.eq(filter.type, OpenLayers.Filter.Comparison.IS_NULL, + "[0] read correct type"); + console.log(filter); + t.eq(filter.property, 'prop', + "[0] record correct property name"); + + var filter = new OpenLayers.Filter.Comparison({ + type: OpenLayers.Filter.Comparison.IS_NULL, + property: "prop" + }); + element = format.write(filter); + t.eq(element.firstChild.nodeName, 'ogc:PropertyIsNull', + "[1] write correct filter node name"); + t.eq(element.firstChild.childNodes.length, 1, + "[1] write correct number of filter arguments"); + t.eq(element.firstChild.firstChild.nodeName, 'ogc:PropertyName', + "[1] write correct filter argument name"); + t.eq(element.firstChild.firstChild.textContent, 'prop', + "[1] write correct filter argument value"); + + } function test_Intersects(t) { From 6a37e72ae38154050bfa132e53bebaba72f71975 Mon Sep 17 00:00:00 2001 From: Matt Walker Date: Fri, 12 Oct 2012 22:49:30 +0100 Subject: [PATCH 2/9] Added some comments to Format/Filter tests. --- tests/Format/Filter/v1.html | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/Format/Filter/v1.html b/tests/Format/Filter/v1.html index ab43d8cb51..a8e1282bfa 100644 --- a/tests/Format/Filter/v1.html +++ b/tests/Format/Filter/v1.html @@ -58,6 +58,8 @@ var test_xml, format, xml; format = new OpenLayers.Format.Filter.v1(); + + // Test reading a PropertyIsNull filter from an XML doc xml = new OpenLayers.Format.XML(); test_xml = @@ -74,6 +76,7 @@ t.eq(filter.property, 'prop', "[0] record correct property name"); + // Test writing a PropertyIsNull filter out to XML var filter = new OpenLayers.Filter.Comparison({ type: OpenLayers.Filter.Comparison.IS_NULL, property: "prop" From f08c562955d4a18b8d60d34000e48dc91541f79e Mon Sep 17 00:00:00 2001 From: Matt Walker Date: Sat, 13 Oct 2012 09:47:23 +0100 Subject: [PATCH 3/9] Added IS_NULL evaluate feature tests --- tests/Filter/Comparison.html | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/tests/Filter/Comparison.html b/tests/Filter/Comparison.html index 5dd1cc1bf2..e8f9a9c2b0 100644 --- a/tests/Filter/Comparison.html +++ b/tests/Filter/Comparison.html @@ -302,6 +302,34 @@ }), context: new OpenLayers.Feature.Vector(null, {prop: "FOO"}), expect: false + }, { + filter: new OpenLayers.Filter.Comparison({ + type: OpenLayers.Filter.Comparison.IS_NULL, + property: "prop" + }), + context: new OpenLayers.Feature.Vector(null, {prop: null}), + expect: true + }, { + filter: new OpenLayers.Filter.Comparison({ + type: OpenLayers.Filter.Comparison.IS_NULL, + property: "prop" + }), + context: new OpenLayers.Feature.Vector(null, {}), + expect: true + }, { + filter: new OpenLayers.Filter.Comparison({ + type: OpenLayers.Filter.Comparison.IS_NULL, + property: "prop" + }), + context: new OpenLayers.Feature.Vector(null, {prop: "foo"}), + expect: false + }, { + filter: new OpenLayers.Filter.Comparison({ + type: OpenLayers.Filter.Comparison.IS_NULL, + property: "prop" + }), + context: new OpenLayers.Feature.Vector(null, {prop: 0}), + expect: false }]; t.plan(cases.length); From 8f860e516e682d60a06c1a13743138410eed5afa Mon Sep 17 00:00:00 2001 From: Matt Walker Date: Sat, 13 Oct 2012 20:05:43 +0100 Subject: [PATCH 4/9] Removed stray console.log in PropertyIsNull test --- tests/Format/Filter/v1.html | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/Format/Filter/v1.html b/tests/Format/Filter/v1.html index a8e1282bfa..9b617cf521 100644 --- a/tests/Format/Filter/v1.html +++ b/tests/Format/Filter/v1.html @@ -72,7 +72,6 @@ var filter = format.read(xml.read(test_xml).documentElement); t.eq(filter.type, OpenLayers.Filter.Comparison.IS_NULL, "[0] read correct type"); - console.log(filter); t.eq(filter.property, 'prop', "[0] record correct property name"); From bccd0824b9f9405bba3de45788706e31c0c6034a Mon Sep 17 00:00:00 2001 From: Matt Walker Date: Mon, 15 Oct 2012 09:47:10 +0100 Subject: [PATCH 5/9] Removed duplicate line that gets the filter value --- lib/OpenLayers/Filter/Comparison.js | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/OpenLayers/Filter/Comparison.js b/lib/OpenLayers/Filter/Comparison.js index bb40900995..68d8cada8f 100644 --- a/lib/OpenLayers/Filter/Comparison.js +++ b/lib/OpenLayers/Filter/Comparison.js @@ -156,7 +156,6 @@ OpenLayers.Filter.Comparison = OpenLayers.Class(OpenLayers.Filter, { result = regexp.test(got); break; case OpenLayers.Filter.Comparison.IS_NULL: - var got = context[this.property]; result = (got === null || got === undefined); break; } From 41dac08cfae44dd7fd1a4df21ed9dc00245bc918 Mon Sep 17 00:00:00 2001 From: Matt Walker Date: Mon, 15 Oct 2012 09:51:56 +0100 Subject: [PATCH 6/9] Resolved redefinition of filter variable in test --- tests/Format/Filter/v1.html | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/Format/Filter/v1.html b/tests/Format/Filter/v1.html index 9b617cf521..7a5230a1bb 100644 --- a/tests/Format/Filter/v1.html +++ b/tests/Format/Filter/v1.html @@ -55,7 +55,7 @@ t.plan(6); - var test_xml, format, xml; + var test_xml, format, xml, filter; format = new OpenLayers.Format.Filter.v1(); @@ -69,18 +69,18 @@ '' + ''; - var filter = format.read(xml.read(test_xml).documentElement); + filter = format.read(xml.read(test_xml).documentElement); t.eq(filter.type, OpenLayers.Filter.Comparison.IS_NULL, "[0] read correct type"); t.eq(filter.property, 'prop', "[0] record correct property name"); // Test writing a PropertyIsNull filter out to XML - var filter = new OpenLayers.Filter.Comparison({ + filter = new OpenLayers.Filter.Comparison({ type: OpenLayers.Filter.Comparison.IS_NULL, property: "prop" }); - element = format.write(filter); + var element = format.write(filter); t.eq(element.firstChild.nodeName, 'ogc:PropertyIsNull', "[1] write correct filter node name"); t.eq(element.firstChild.childNodes.length, 1, From a9494394da097ecd4c5032af4c28a44c83ef6854 Mon Sep 17 00:00:00 2001 From: Matt Walker Date: Mon, 15 Oct 2012 11:09:52 +0100 Subject: [PATCH 7/9] Comparing the filter XML written using t.xml_eq. The PropertyIsNull test now used the t.xml_eq method to verify the XML output when the filter is written. Tided up variable names to be consistent with other tests. --- tests/Format/Filter/v1.html | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/tests/Format/Filter/v1.html b/tests/Format/Filter/v1.html index 7a5230a1bb..fcf5c67044 100644 --- a/tests/Format/Filter/v1.html +++ b/tests/Format/Filter/v1.html @@ -53,15 +53,12 @@ function test_PropertyIsNull(t) { - t.plan(6); + t.plan(3); - var test_xml, format, xml, filter; + var format, test_xml, xml, filter; format = new OpenLayers.Format.Filter.v1(); - // Test reading a PropertyIsNull filter from an XML doc - xml = new OpenLayers.Format.XML(); - test_xml = '' + '' + @@ -69,6 +66,8 @@ '' + ''; + // Test reading a PropertyIsNull filter from an XML doc + xml = new OpenLayers.Format.XML(); filter = format.read(xml.read(test_xml).documentElement); t.eq(filter.type, OpenLayers.Filter.Comparison.IS_NULL, "[0] read correct type"); @@ -80,15 +79,8 @@ type: OpenLayers.Filter.Comparison.IS_NULL, property: "prop" }); - var element = format.write(filter); - t.eq(element.firstChild.nodeName, 'ogc:PropertyIsNull', - "[1] write correct filter node name"); - t.eq(element.firstChild.childNodes.length, 1, - "[1] write correct number of filter arguments"); - t.eq(element.firstChild.firstChild.nodeName, 'ogc:PropertyName', - "[1] write correct filter argument name"); - t.eq(element.firstChild.firstChild.textContent, 'prop', - "[1] write correct filter argument value"); + var node = format.write(filter); + t.xml_eq(node, test_xml, "filter correctly written"); } From ea5a510d45f2dd295460f9bebf90fcb43b90352b Mon Sep 17 00:00:00 2001 From: Matt Walker Date: Mon, 15 Oct 2012 11:16:00 +0100 Subject: [PATCH 8/9] Moved node var declaration to the top of the function --- tests/Format/Filter/v1.html | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Format/Filter/v1.html b/tests/Format/Filter/v1.html index fcf5c67044..dc4eec2fc0 100644 --- a/tests/Format/Filter/v1.html +++ b/tests/Format/Filter/v1.html @@ -55,7 +55,7 @@ t.plan(3); - var format, test_xml, xml, filter; + var format, test_xml, xml, filter, node; format = new OpenLayers.Format.Filter.v1(); @@ -79,7 +79,7 @@ type: OpenLayers.Filter.Comparison.IS_NULL, property: "prop" }); - var node = format.write(filter); + node = format.write(filter); t.xml_eq(node, test_xml, "filter correctly written"); } From a65a231da3c5737012b40e33073ed87b98c8330a Mon Sep 17 00:00:00 2001 From: Matt Walker Date: Tue, 16 Oct 2012 08:11:59 +0100 Subject: [PATCH 9/9] Updated IS_NULL filter to check for strict null. The IS_NULL filter evaluates to true only when the property is null. Updated tests to reflect this change. --- lib/OpenLayers/Filter/Comparison.js | 2 +- tests/Filter/Comparison.html | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/OpenLayers/Filter/Comparison.js b/lib/OpenLayers/Filter/Comparison.js index 68d8cada8f..c38d60f3a5 100644 --- a/lib/OpenLayers/Filter/Comparison.js +++ b/lib/OpenLayers/Filter/Comparison.js @@ -156,7 +156,7 @@ OpenLayers.Filter.Comparison = OpenLayers.Class(OpenLayers.Filter, { result = regexp.test(got); break; case OpenLayers.Filter.Comparison.IS_NULL: - result = (got === null || got === undefined); + result = (got === null); break; } return result; diff --git a/tests/Filter/Comparison.html b/tests/Filter/Comparison.html index e8f9a9c2b0..04e192a348 100644 --- a/tests/Filter/Comparison.html +++ b/tests/Filter/Comparison.html @@ -176,8 +176,8 @@ type: OpenLayers.Filter.Comparison.IS_NULL, property: "prop" }), - context: {prop: undefined}, - expect: true + context: {}, + expect: false }, { filter: new OpenLayers.Filter.Comparison({ type: OpenLayers.Filter.Comparison.IS_NULL, @@ -315,7 +315,7 @@ property: "prop" }), context: new OpenLayers.Feature.Vector(null, {}), - expect: true + expect: false }, { filter: new OpenLayers.Filter.Comparison({ type: OpenLayers.Filter.Comparison.IS_NULL,