From a6a8b6b1d0e45b9ba5142646f5effc6466c8ac8d Mon Sep 17 00:00:00 2001 From: sufyanAbbasi Date: Fri, 11 Aug 2023 00:53:23 -0700 Subject: [PATCH] Sanitize style attributes in data attribute in DrawControl.js for upstream serialization. - Fix for https://github.com/jupyter-widgets/ipyleaflet/issues/1119 - The main issue is that a `style()` function is added to the GeoJSON object in `create_obj()`, which triggers an error at serialization time: https://github.com/jupyter-widgets/ipywidgets/blob/e1718c2b3bf0b143580ef87f71c55fbc6ed50a77/packages/base/src/widget.ts#L587 - This is because `structuredClone` throws an error for functions: https://web.dev/structured-clone/#features-and-limitations. While the article mentions drawbacks for using `JSON.parse(JSON.stringify(...))`, I believe that this component does not produce any non-primitive properties, so will remove any offending lines. - Tested by placing a breakpoint and testing directly on the browser. --- js/src/controls/DrawControl.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/js/src/controls/DrawControl.js b/js/src/controls/DrawControl.js index 2c35fc0ad..8f07a22db 100644 --- a/js/src/controls/DrawControl.js +++ b/js/src/controls/DrawControl.js @@ -141,7 +141,10 @@ export class LeafletDrawControlView extends control.LeafletControlView { let newData = []; this.feature_group.eachLayer((layer) => { const geoJson = layer.toGeoJSON(); - geoJson.properties.style = layer.options; + // Sanitize layer options for serialization via `structuredClone`: + // https://web.dev/structured-clone/#features-and-limitations + const sanitizedLayerOptions = JSON.parse(JSON.stringify(layer.options)); + geoJson.properties.style = sanitizedLayerOptions; newData.push(geoJson); }); this.model.set('data', newData);