From effa97cd0e5a038cbd9c0ab0d208598bde219301 Mon Sep 17 00:00:00 2001 From: Leon Zandman Date: Mon, 25 May 2026 17:55:18 +0200 Subject: [PATCH 1/3] Handle invalid coordinate input to prevent TypeError. --- src/core/operations/ShowOnMap.mjs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/core/operations/ShowOnMap.mjs b/src/core/operations/ShowOnMap.mjs index d75c2aa6a9..be606d18dd 100644 --- a/src/core/operations/ShowOnMap.mjs +++ b/src/core/operations/ShowOnMap.mjs @@ -71,6 +71,16 @@ class ShowOnMap extends Operation { } latLong = latLong.replace(/[,]$/, ""); latLong = latLong.replace(/°/g, ""); + + // The map requires a latitude and longitude pair. If the conversion only produced a + // single value (e.g. because the chosen input delimiter didn't match the input), bail + // out with a helpful message rather than passing it on to the map, which would throw an + // uncaught TypeError in the browser. + const coords = latLong.split(",").map(v => v.trim()); + if (coords.length !== 2 || coords.some(v => v === "" || isNaN(Number(v)))) { + throw new OperationError(`Could not show co-ordinates '${latLong}' on the map. Expected a latitude and longitude pair - check that the input format and delimiter are correct.`); + } + return latLong; } return input; From b683f0239c82289fa4a1bec7717867bd37821ce5 Mon Sep 17 00:00:00 2001 From: Leon Zandman Date: Mon, 25 May 2026 17:55:34 +0200 Subject: [PATCH 2/3] Add tests for Show on map operation to validate coordinate input --- tests/operations/index.mjs | 1 + tests/operations/tests/ShowOnMap.mjs | 39 ++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 tests/operations/tests/ShowOnMap.mjs diff --git a/tests/operations/index.mjs b/tests/operations/index.mjs index c12c271098..ed6a26168f 100644 --- a/tests/operations/index.mjs +++ b/tests/operations/index.mjs @@ -159,6 +159,7 @@ import "./tests/SeqUtils.mjs"; import "./tests/SetDifference.mjs"; import "./tests/SetIntersection.mjs"; import "./tests/SetUnion.mjs"; +import "./tests/ShowOnMap.mjs"; import "./tests/Shuffle.mjs"; import "./tests/SIGABA.mjs"; import "./tests/SM2.mjs"; diff --git a/tests/operations/tests/ShowOnMap.mjs b/tests/operations/tests/ShowOnMap.mjs new file mode 100644 index 0000000000..f9af64effe --- /dev/null +++ b/tests/operations/tests/ShowOnMap.mjs @@ -0,0 +1,39 @@ +/** + * Show on map tests + * + * @author Leon Zandman [leon@wirwar.com] + * + * @copyright Crown Copyright 2026 + * @license Apache-2.0 + */ +import TestRegister from "../../lib/TestRegister.mjs"; + +TestRegister.addTests([ + { + name: "Show on map: valid co-ordinate pair", + input: "51.5007, -0.1246", + // The presented output is the Leaflet map HTML; just check the co-ordinates made it through. + expectedMatch: /51\.5007,-0\.1246/, + recipeConfig: [ + { + op: "Show on map", + args: [13, "Auto", "Auto"] + }, + ], + }, + { + // Regression test: a comma-separated input with the delimiter set to "\n" used to be + // mis-detected as a single Degrees Decimal Minutes value (1° 24' = 1.4°), producing a single + // co-ordinate. That single value was then passed to Leaflet's setView([1.4], ...), throwing + // an uncaught "Cannot read properties of null (reading 'lat')" TypeError in the browser. + name: "Show on map: single value is rejected with a helpful error", + input: "1, 24", + expectedOutput: "Could not show co-ordinates '1.4' on the map. Expected a latitude and longitude pair - check that the input format and delimiter are correct.", + recipeConfig: [ + { + op: "Show on map", + args: [13, "Auto", "\\n"] + }, + ], + }, +]); From aae2312a5a7ff51ceba7db9473c986412b1896d1 Mon Sep 17 00:00:00 2001 From: Leon Zandman Date: Mon, 25 May 2026 18:07:12 +0200 Subject: [PATCH 3/3] Spelling. --- src/core/operations/ShowOnMap.mjs | 2 +- tests/operations/tests/ShowOnMap.mjs | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/core/operations/ShowOnMap.mjs b/src/core/operations/ShowOnMap.mjs index be606d18dd..2eab5140f6 100644 --- a/src/core/operations/ShowOnMap.mjs +++ b/src/core/operations/ShowOnMap.mjs @@ -78,7 +78,7 @@ class ShowOnMap extends Operation { // uncaught TypeError in the browser. const coords = latLong.split(",").map(v => v.trim()); if (coords.length !== 2 || coords.some(v => v === "" || isNaN(Number(v)))) { - throw new OperationError(`Could not show co-ordinates '${latLong}' on the map. Expected a latitude and longitude pair - check that the input format and delimiter are correct.`); + throw new OperationError(`Could not show coordinates '${latLong}' on the map. Expected a latitude and longitude pair - check that the input format and delimiter are correct.`); } return latLong; diff --git a/tests/operations/tests/ShowOnMap.mjs b/tests/operations/tests/ShowOnMap.mjs index f9af64effe..8605ae704e 100644 --- a/tests/operations/tests/ShowOnMap.mjs +++ b/tests/operations/tests/ShowOnMap.mjs @@ -10,9 +10,9 @@ import TestRegister from "../../lib/TestRegister.mjs"; TestRegister.addTests([ { - name: "Show on map: valid co-ordinate pair", + name: "Show on map: valid coordinate pair", input: "51.5007, -0.1246", - // The presented output is the Leaflet map HTML; just check the co-ordinates made it through. + // The presented output is the Leaflet map HTML; just check the coordinates made it through. expectedMatch: /51\.5007,-0\.1246/, recipeConfig: [ { @@ -24,11 +24,11 @@ TestRegister.addTests([ { // Regression test: a comma-separated input with the delimiter set to "\n" used to be // mis-detected as a single Degrees Decimal Minutes value (1° 24' = 1.4°), producing a single - // co-ordinate. That single value was then passed to Leaflet's setView([1.4], ...), throwing + // coordinate. That single value was then passed to Leaflet's setView([1.4], ...), throwing // an uncaught "Cannot read properties of null (reading 'lat')" TypeError in the browser. name: "Show on map: single value is rejected with a helpful error", input: "1, 24", - expectedOutput: "Could not show co-ordinates '1.4' on the map. Expected a latitude and longitude pair - check that the input format and delimiter are correct.", + expectedOutput: "Could not show coordinates '1.4' on the map. Expected a latitude and longitude pair - check that the input format and delimiter are correct.", recipeConfig: [ { op: "Show on map",