From caca0732a976f18b016f4629d6c727861896b3ce Mon Sep 17 00:00:00 2001 From: Justas Brazauskas Date: Fri, 12 Apr 2019 17:57:28 +0300 Subject: [PATCH 1/4] Fix an edge case where 0 target.id would fail to clear the feature state --- src/style/style.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/style/style.js b/src/style/style.js index deff109dd11..24a0acf9ef2 100644 --- a/src/style/style.js +++ b/src/style/style.js @@ -878,12 +878,12 @@ class Style extends Evented { return; } - if (target.id && isNaN(featureId) || featureId < 0) { + if (target.id !== undefined && isNaN(featureId) || featureId < 0) { this.fire(new ErrorEvent(new Error(`The feature id parameter must be non-negative.`))); return; } - if (key && !target.id) { + if (key && typeof target.id !== 'number') { this.fire(new ErrorEvent(new Error(`A feature id is requred to remove its specific state property.`))); return; } From c1ca634f4a8f93772347e2e1f3128a3a7f3e176b Mon Sep 17 00:00:00 2001 From: Justas Brazauskas Date: Fri, 12 Apr 2019 18:46:51 +0300 Subject: [PATCH 2/4] Check for number & string explicitly --- src/style/style.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/style/style.js b/src/style/style.js index 24a0acf9ef2..102a49bbf02 100644 --- a/src/style/style.js +++ b/src/style/style.js @@ -883,7 +883,7 @@ class Style extends Evented { return; } - if (key && typeof target.id !== 'number') { + if (key && (typeof target.id !== 'string' || typeof target.id !== 'number')) { this.fire(new ErrorEvent(new Error(`A feature id is requred to remove its specific state property.`))); return; } From 7405bb46ae57ef19f747ec3aaeff174e941b241d Mon Sep 17 00:00:00 2001 From: Justas Brazauskas Date: Fri, 12 Apr 2019 18:52:28 +0300 Subject: [PATCH 3/4] Add tests for 0 feature ID --- test/unit/ui/map.test.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/test/unit/ui/map.test.js b/test/unit/ui/map.test.js index 2c95c36c98b..8cd3605fb49 100755 --- a/test/unit/ui/map.test.js +++ b/test/unit/ui/map.test.js @@ -1524,6 +1524,27 @@ test('Map', (t) => { t.end(); }); }); + t.test('remove properties for zero-based feature IDs.', (t) => { + const map = createMap(t, { + style: { + "version": 8, + "sources": { + "geojson": createStyleSource() + }, + "layers": [] + } + }); + map.on('load', () => { + map.setFeatureState({ source: 'geojson', id: 0}, {'hover': true, 'foo': true}); + map.removeFeatureState({ source: 'geojson', id: 0}); + + const fState = map.getFeatureState({ source: 'geojson', id: 0}); + t.equal(fState.hover, undefined); + t.equal(fState.foo, undefined); + + t.end(); + }); + }); t.test('other properties persist when removing specific property', (t) => { const map = createMap(t, { style: { From 41920f876d5b6876546be2173866fdf38c416ea9 Mon Sep 17 00:00:00 2001 From: Justas Brazauskas Date: Fri, 12 Apr 2019 18:54:24 +0300 Subject: [PATCH 4/4] Fix logical check --- src/style/style.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/style/style.js b/src/style/style.js index 102a49bbf02..45417adefce 100644 --- a/src/style/style.js +++ b/src/style/style.js @@ -883,7 +883,7 @@ class Style extends Evented { return; } - if (key && (typeof target.id !== 'string' || typeof target.id !== 'number')) { + if (key && (typeof target.id !== 'string' && typeof target.id !== 'number')) { this.fire(new ErrorEvent(new Error(`A feature id is requred to remove its specific state property.`))); return; }