jenkins-bot has submitted this change and it was merged.

Change subject: Validate the editable GeoJSON against the sanitize-mapdata api 
before applying it.
......................................................................


Validate the editable GeoJSON against the
sanitize-mapdata api before applying it.

Change-Id: I7dae1a52ad720bfc1ccd7935920df89bcdf48a53
---
M modules/kartographer.js
M modules/ve-maps/ve.ce.MWMapsNode.js
M modules/ve-maps/ve.ui.MWMapsDialog.js
3 files changed, 47 insertions(+), 21 deletions(-)

Approvals:
  MaxSem: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/modules/kartographer.js b/modules/kartographer.js
index 40f2f18..ee8d7db 100644
--- a/modules/kartographer.js
+++ b/modules/kartographer.js
@@ -157,31 +157,51 @@
        };
 
        /**
-        * Update "editable" geojson layer from a string
+        * Updates "editable" GeoJSON layer from a string.
+        *
+        * Validates the GeoJSON against the `sanitize-mapdata` api
+        * before executing it.
+        *
+        * The deferred object will be resolved with a `boolean` flag
+        * indicating whether the GeoJSON was valid and was applied.
         *
         * @param {L.mapbox.Map} map Map to set the GeoJSON for
         * @param {string} geoJsonString GeoJSON data, empty string to clear
-        * @return {boolean} The GeoJSON string provided was valid as was 
applied
+        * @return {jQuery.Promise}
         */
        mw.kartographer.updateKartographerLayer = function ( map, geoJsonString 
) {
-               var geoJson, layer, isValid = true;
+               return $.Deferred( function ( deferred ) {
+                       var isValid = true;
 
-               if ( geoJsonString ) {
-                       try {
-                               geoJson = JSON.parse( geoJsonString );
-                       } catch ( e ) {
-                               // Invalid JSON, clear it
-                               isValid = false;
+                       if ( geoJsonString === '' ) {
+                               deferred.resolve( isValid );
                        }
-               }
 
-               try {
-                       layer = mw.kartographer.getKartographerLayer( map );
-                       layer.setGeoJSON( !isValid || geoJson === undefined ? 
[] : geoJson );
-                       return isValid;
-               } catch ( e ) {
-                       return false;
-               }
+                       new mw.Api().post( {
+                               action: 'sanitize-mapdata',
+                               text: geoJsonString,
+                               title: mw.config.get( 'wgPageName' )
+                       } ).done( function ( resp ) {
+                               var data = resp[ 'sanitize-mapdata' ],
+                                       geoJson,
+                                       layer;
+
+                               geoJsonString = data.sanitized;
+                               isValid = !!( geoJsonString && !data.error );
+
+                               if ( isValid ) {
+                                       try {
+                                               geoJson = JSON.parse( 
geoJsonString );
+                                               layer = 
mw.kartographer.getKartographerLayer( map );
+                                               layer.setGeoJSON( geoJson );
+                                       } catch ( e ) {
+                                               isValid = false;
+                                       }
+                               }
+                               deferred.resolve( isValid );
+                       } );
+
+               } ).promise();
        };
 
        function getWindowManager() {
diff --git a/modules/ve-maps/ve.ce.MWMapsNode.js 
b/modules/ve-maps/ve.ce.MWMapsNode.js
index 441605d..612af79 100644
--- a/modules/ve-maps/ve.ce.MWMapsNode.js
+++ b/modules/ve-maps/ve.ce.MWMapsNode.js
@@ -27,6 +27,8 @@
        this.$imageLoader = null;
        this.geoJson = null;
 
+       this.updateGeoJson = $.debounce( 300, $.proxy( this.updateGeoJson, this 
) );
+
        // Events
        this.model.connect( this, { attributeChange: 'onAttributeChange' } );
        this.connect( this, { focus: 'onMapFocus' } );
diff --git a/modules/ve-maps/ve.ui.MWMapsDialog.js 
b/modules/ve-maps/ve.ui.MWMapsDialog.js
index 9357b76..612ae44 100644
--- a/modules/ve-maps/ve.ui.MWMapsDialog.js
+++ b/modules/ve-maps/ve.ui.MWMapsDialog.js
@@ -17,6 +17,8 @@
 ve.ui.MWMapsDialog = function VeUiMWMapsDialog() {
        // Parent constructor
        ve.ui.MWMapsDialog.super.apply( this, arguments );
+
+       this.updateGeoJson = $.debounce( 300, $.proxy( this.updateGeoJson, this 
) );
 };
 
 /* Inheritance */
@@ -321,15 +323,17 @@
  * Update the GeoJSON layer from the current input state
  */
 ve.ui.MWMapsDialog.prototype.updateGeoJson = function () {
-       var isValid;
+       var self = this;
 
        if ( !this.map || this.updatingGeoJson ) {
                return;
        }
 
-       isValid = mw.kartographer.updateKartographerLayer( this.map, 
this.input.getValue() );
-       this.input.setValidityFlag( isValid );
-       this.updateActions();
+       mw.kartographer.updateKartographerLayer( this.map, 
this.input.getValue() )
+               .done( function ( isValid ) {
+                       self.input.setValidityFlag( isValid );
+                       self.updateActions();
+               } );
 };
 
 /**

-- 
To view, visit https://gerrit.wikimedia.org/r/283592
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I7dae1a52ad720bfc1ccd7935920df89bcdf48a53
Gerrit-PatchSet: 2
Gerrit-Project: mediawiki/extensions/Kartographer
Gerrit-Branch: master
Gerrit-Owner: JGirault <[email protected]>
Gerrit-Reviewer: Esanders <[email protected]>
Gerrit-Reviewer: MaxSem <[email protected]>
Gerrit-Reviewer: Yurik <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to