Merge pull request #1168 from umap-project/invalid-latlng

Be more strict when coordinates are set manually
This commit is contained in:
Yohan Boniface 2023-06-23 21:03:55 +02:00 committed by GitHub
commit 001b79795a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 36 additions and 4 deletions

View file

@ -40,7 +40,7 @@
"leaflet-contextmenu": "^1.4.0", "leaflet-contextmenu": "^1.4.0",
"leaflet-editable": "^1.2.0", "leaflet-editable": "^1.2.0",
"leaflet-editinosm": "0.2.3", "leaflet-editinosm": "0.2.3",
"leaflet-formbuilder": "0.2.4", "leaflet-formbuilder": "0.2.6",
"leaflet-fullscreen": "1.0.2", "leaflet-fullscreen": "1.0.2",
"leaflet-hash": "0.2.1", "leaflet-hash": "0.2.1",
"leaflet-i18n": "0.3.1", "leaflet-i18n": "0.3.1",

View file

@ -656,5 +656,10 @@ L.U.Orderable = L.Evented.extend({
}) })
L.LatLng.prototype.isValid = function () { L.LatLng.prototype.isValid = function () {
return !isNaN(this.lat) && !isNaN(this.lng) return (
isFinite(this.lat) &&
Math.abs(this.lat) <= 90 &&
isFinite(this.lng) &&
Math.abs(this.lng) <= 180
)
} }

View file

@ -627,11 +627,14 @@ L.U.Marker = L.Marker.extend({
] ]
const builder = new L.U.FormBuilder(this, coordinatesOptions, { const builder = new L.U.FormBuilder(this, coordinatesOptions, {
callback: function () { callback: function () {
if (!this._latlng.isValid()) if (!this._latlng.isValid()) {
return this.map.ui.alert({ this.map.ui.alert({
content: L._('Invalid latitude or longitude'), content: L._('Invalid latitude or longitude'),
level: 'error', level: 'error',
}) })
builder.resetField('_latlng.lat')
builder.resetField('_latlng.lng')
}
this._redraw() this._redraw()
this.zoomTo({ easing: false }) this.zoomTo({ easing: false })
}, },

View file

@ -95,4 +95,28 @@ describe('L.U.Marker', function () {
assert.equal(L.Util.formatNum(layer._latlng.lng), other._latlng.lng) assert.equal(L.Util.formatNum(layer._latlng.lng), other._latlng.lng)
}) })
}) })
describe('#edit()', function (done) {
it('should allow changing coordinates manually', function () {
var layer = new L.U.Marker(this.map, [10, 20], {
datalayer: this.datalayer,
}).addTo(this.datalayer)
enableEdit()
layer.edit()
changeInputValue(qs('form.umap-form input[name="lat"]'), '54.43')
assert.equal(layer._latlng.lat, 54.43)
})
it('should not allow invalid latitude nor longitude', function () {
var layer = new L.U.Marker(this.map, [10, 20], {
datalayer: this.datalayer,
}).addTo(this.datalayer)
enableEdit()
layer.edit()
changeInputValue(qs('form.umap-form input[name="lat"]'), '5443')
assert.equal(layer._latlng.lat, 10)
changeInputValue(qs('form.umap-form input[name="lng"]'), '5443')
assert.equal(layer._latlng.lng, 20)
})
})
}) })